hwmon: New driver for Analog Devices ADT7473 sensor chip
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / hwmon / gl520sm.c
CommitLineData
1da177e4
LT
1/*
2 gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
96de0e25 5 Kyösti Mälkki <kmalkki@cc.hut.fi>
1da177e4
LT
6 Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
0cacdf29 27#include <linux/jiffies.h>
1da177e4 28#include <linux/i2c.h>
943b0830 29#include <linux/hwmon.h>
86d47f12 30#include <linux/hwmon-sysfs.h>
303760b4 31#include <linux/hwmon-vid.h>
943b0830 32#include <linux/err.h>
9a61bf63 33#include <linux/mutex.h>
87808be4 34#include <linux/sysfs.h>
1da177e4
LT
35
36/* Type of the extra sensor */
37static unsigned short extra_sensor_type;
38module_param(extra_sensor_type, ushort, 0);
39MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
40
41/* Addresses to scan */
42static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
1da177e4
LT
43
44/* Insmod parameters */
f4b50261 45I2C_CLIENT_INSMOD_1(gl520sm);
1da177e4 46
f28dc2f7 47/* Many GL520 constants specified below
1da177e4 48One of the inputs can be configured as either temp or voltage.
f28dc2f7 49That's why _TEMP2 and _IN4 access the same register
1da177e4
LT
50*/
51
52/* The GL520 registers */
53#define GL520_REG_CHIP_ID 0x00
54#define GL520_REG_REVISION 0x01
55#define GL520_REG_CONF 0x03
56#define GL520_REG_MASK 0x11
57
58#define GL520_REG_VID_INPUT 0x02
59
8b4b0ab4
JD
60static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e };
61static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b };
62static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 };
63static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 };
64
65static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e };
66static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 };
67static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 };
1da177e4
LT
68
69#define GL520_REG_FAN_INPUT 0x07
70#define GL520_REG_FAN_MIN 0x08
71#define GL520_REG_FAN_DIV 0x0f
72#define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
73
74#define GL520_REG_ALARMS 0x12
75#define GL520_REG_BEEP_MASK 0x10
76#define GL520_REG_BEEP_ENABLE GL520_REG_CONF
77
78/*
79 * Function declarations
80 */
81
82static int gl520_attach_adapter(struct i2c_adapter *adapter);
83static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
84static void gl520_init_client(struct i2c_client *client);
85static int gl520_detach_client(struct i2c_client *client);
86static int gl520_read_value(struct i2c_client *client, u8 reg);
87static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
88static struct gl520_data *gl520_update_device(struct device *dev);
89
90/* Driver data */
91static struct i2c_driver gl520_driver = {
cdaf7934 92 .driver = {
cdaf7934
LR
93 .name = "gl520sm",
94 },
1da177e4
LT
95 .attach_adapter = gl520_attach_adapter,
96 .detach_client = gl520_detach_client,
97};
98
99/* Client data */
100struct gl520_data {
101 struct i2c_client client;
1beeffe4 102 struct device *hwmon_dev;
9a61bf63 103 struct mutex update_lock;
1da177e4
LT
104 char valid; /* zero until the following fields are valid */
105 unsigned long last_updated; /* in jiffies */
106
107 u8 vid;
108 u8 vrm;
109 u8 in_input[5]; /* [0] = VVD */
110 u8 in_min[5]; /* [0] = VDD */
111 u8 in_max[5]; /* [0] = VDD */
112 u8 fan_input[2];
113 u8 fan_min[2];
114 u8 fan_div[2];
115 u8 fan_off;
116 u8 temp_input[2];
117 u8 temp_max[2];
118 u8 temp_max_hyst[2];
119 u8 alarms;
120 u8 beep_enable;
121 u8 beep_mask;
122 u8 alarm_mask;
123 u8 two_temps;
124};
125
126/*
127 * Sysfs stuff
128 */
129
86d47f12
JD
130static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
131 char *buf)
1da177e4 132{
86d47f12 133 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
134 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
135}
86d47f12 136static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
1da177e4
LT
137
138#define VDD_FROM_REG(val) (((val)*95+2)/4)
139#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
140
141#define IN_FROM_REG(val) ((val)*19)
142#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
143
86d47f12
JD
144static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
145 char *buf)
1da177e4 146{
86d47f12
JD
147 int n = to_sensor_dev_attr(attr)->index;
148 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
149 u8 r = data->in_input[n];
150
151 if (n == 0)
152 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
153 else
154 return sprintf(buf, "%d\n", IN_FROM_REG(r));
155}
156
86d47f12
JD
157static ssize_t get_in_min(struct device *dev, struct device_attribute *attr,
158 char *buf)
1da177e4 159{
86d47f12
JD
160 int n = to_sensor_dev_attr(attr)->index;
161 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
162 u8 r = data->in_min[n];
163
164 if (n == 0)
165 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
166 else
167 return sprintf(buf, "%d\n", IN_FROM_REG(r));
168}
169
86d47f12
JD
170static ssize_t get_in_max(struct device *dev, struct device_attribute *attr,
171 char *buf)
1da177e4 172{
86d47f12
JD
173 int n = to_sensor_dev_attr(attr)->index;
174 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
175 u8 r = data->in_max[n];
176
177 if (n == 0)
178 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
179 else
180 return sprintf(buf, "%d\n", IN_FROM_REG(r));
181}
182
86d47f12
JD
183static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
1da177e4 185{
86d47f12
JD
186 struct i2c_client *client = to_i2c_client(dev);
187 struct gl520_data *data = i2c_get_clientdata(client);
188 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
189 long v = simple_strtol(buf, NULL, 10);
190 u8 r;
191
9a61bf63 192 mutex_lock(&data->update_lock);
1da177e4
LT
193
194 if (n == 0)
195 r = VDD_TO_REG(v);
196 else
197 r = IN_TO_REG(v);
198
199 data->in_min[n] = r;
200
201 if (n < 4)
86d47f12
JD
202 gl520_write_value(client, GL520_REG_IN_MIN[n],
203 (gl520_read_value(client, GL520_REG_IN_MIN[n])
204 & ~0xff) | r);
1da177e4 205 else
86d47f12 206 gl520_write_value(client, GL520_REG_IN_MIN[n], r);
1da177e4 207
9a61bf63 208 mutex_unlock(&data->update_lock);
1da177e4
LT
209 return count;
210}
211
86d47f12
JD
212static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
1da177e4 214{
86d47f12
JD
215 struct i2c_client *client = to_i2c_client(dev);
216 struct gl520_data *data = i2c_get_clientdata(client);
217 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
218 long v = simple_strtol(buf, NULL, 10);
219 u8 r;
220
221 if (n == 0)
222 r = VDD_TO_REG(v);
223 else
224 r = IN_TO_REG(v);
225
9a61bf63 226 mutex_lock(&data->update_lock);
1da177e4
LT
227
228 data->in_max[n] = r;
229
230 if (n < 4)
86d47f12
JD
231 gl520_write_value(client, GL520_REG_IN_MAX[n],
232 (gl520_read_value(client, GL520_REG_IN_MAX[n])
233 & ~0xff00) | (r << 8));
1da177e4 234 else
86d47f12 235 gl520_write_value(client, GL520_REG_IN_MAX[n], r);
1da177e4 236
9a61bf63 237 mutex_unlock(&data->update_lock);
1da177e4
LT
238 return count;
239}
240
86d47f12
JD
241static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, get_in_input, NULL, 0);
242static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, get_in_input, NULL, 1);
243static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, get_in_input, NULL, 2);
244static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, get_in_input, NULL, 3);
245static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, get_in_input, NULL, 4);
246static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
247 get_in_min, set_in_min, 0);
248static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
249 get_in_min, set_in_min, 1);
250static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
251 get_in_min, set_in_min, 2);
252static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
253 get_in_min, set_in_min, 3);
254static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
255 get_in_min, set_in_min, 4);
256static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
257 get_in_max, set_in_max, 0);
258static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
259 get_in_max, set_in_max, 1);
260static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
261 get_in_max, set_in_max, 2);
262static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
263 get_in_max, set_in_max, 3);
264static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
265 get_in_max, set_in_max, 4);
266
1da177e4
LT
267#define DIV_FROM_REG(val) (1 << (val))
268#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
269#define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
270
86d47f12
JD
271static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr,
272 char *buf)
1da177e4 273{
86d47f12
JD
274 int n = to_sensor_dev_attr(attr)->index;
275 struct gl520_data *data = gl520_update_device(dev);
276
277 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
278 data->fan_div[n]));
1da177e4
LT
279}
280
86d47f12
JD
281static ssize_t get_fan_min(struct device *dev, struct device_attribute *attr,
282 char *buf)
1da177e4 283{
86d47f12
JD
284 int n = to_sensor_dev_attr(attr)->index;
285 struct gl520_data *data = gl520_update_device(dev);
286
287 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
288 data->fan_div[n]));
1da177e4
LT
289}
290
86d47f12
JD
291static ssize_t get_fan_div(struct device *dev, struct device_attribute *attr,
292 char *buf)
1da177e4 293{
86d47f12
JD
294 int n = to_sensor_dev_attr(attr)->index;
295 struct gl520_data *data = gl520_update_device(dev);
296
297 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
1da177e4
LT
298}
299
86d47f12
JD
300static ssize_t get_fan_off(struct device *dev, struct device_attribute *attr,
301 char *buf)
1da177e4 302{
86d47f12 303 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
304 return sprintf(buf, "%d\n", data->fan_off);
305}
306
86d47f12
JD
307static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
1da177e4 309{
86d47f12
JD
310 struct i2c_client *client = to_i2c_client(dev);
311 struct gl520_data *data = i2c_get_clientdata(client);
312 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
313 unsigned long v = simple_strtoul(buf, NULL, 10);
314 u8 r;
315
9a61bf63 316 mutex_lock(&data->update_lock);
86d47f12
JD
317 r = FAN_TO_REG(v, data->fan_div[n]);
318 data->fan_min[n] = r;
1da177e4 319
86d47f12
JD
320 if (n == 0)
321 gl520_write_value(client, GL520_REG_FAN_MIN,
322 (gl520_read_value(client, GL520_REG_FAN_MIN)
323 & ~0xff00) | (r << 8));
1da177e4 324 else
86d47f12
JD
325 gl520_write_value(client, GL520_REG_FAN_MIN,
326 (gl520_read_value(client, GL520_REG_FAN_MIN)
327 & ~0xff) | r);
1da177e4
LT
328
329 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
86d47f12
JD
330 if (data->fan_min[n] == 0)
331 data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
1da177e4 332 else
86d47f12 333 data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
1da177e4
LT
334 data->beep_mask &= data->alarm_mask;
335 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
336
9a61bf63 337 mutex_unlock(&data->update_lock);
1da177e4
LT
338 return count;
339}
340
86d47f12
JD
341static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
342 const char *buf, size_t count)
1da177e4 343{
86d47f12
JD
344 struct i2c_client *client = to_i2c_client(dev);
345 struct gl520_data *data = i2c_get_clientdata(client);
346 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
347 unsigned long v = simple_strtoul(buf, NULL, 10);
348 u8 r;
349
350 switch (v) {
351 case 1: r = 0; break;
352 case 2: r = 1; break;
353 case 4: r = 2; break;
354 case 8: r = 3; break;
355 default:
356 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
357 return -EINVAL;
358 }
359
9a61bf63 360 mutex_lock(&data->update_lock);
86d47f12 361 data->fan_div[n] = r;
1da177e4 362
86d47f12
JD
363 if (n == 0)
364 gl520_write_value(client, GL520_REG_FAN_DIV,
365 (gl520_read_value(client, GL520_REG_FAN_DIV)
366 & ~0xc0) | (r << 6));
1da177e4 367 else
86d47f12
JD
368 gl520_write_value(client, GL520_REG_FAN_DIV,
369 (gl520_read_value(client, GL520_REG_FAN_DIV)
370 & ~0x30) | (r << 4));
1da177e4 371
9a61bf63 372 mutex_unlock(&data->update_lock);
1da177e4
LT
373 return count;
374}
375
86d47f12
JD
376static ssize_t set_fan_off(struct device *dev, struct device_attribute *attr,
377 const char *buf, size_t count)
1da177e4 378{
86d47f12
JD
379 struct i2c_client *client = to_i2c_client(dev);
380 struct gl520_data *data = i2c_get_clientdata(client);
1da177e4
LT
381 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
382
9a61bf63 383 mutex_lock(&data->update_lock);
1da177e4 384 data->fan_off = r;
86d47f12
JD
385 gl520_write_value(client, GL520_REG_FAN_OFF,
386 (gl520_read_value(client, GL520_REG_FAN_OFF)
387 & ~0x0c) | (r << 2));
9a61bf63 388 mutex_unlock(&data->update_lock);
1da177e4
LT
389 return count;
390}
391
86d47f12
JD
392static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_input, NULL, 0);
393static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan_input, NULL, 1);
394static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
395 get_fan_min, set_fan_min, 0);
396static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
397 get_fan_min, set_fan_min, 1);
398static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
399 get_fan_div, set_fan_div, 0);
400static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
401 get_fan_div, set_fan_div, 1);
402static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
403 get_fan_off, set_fan_off);
404
1da177e4
LT
405#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
406#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
407
86d47f12
JD
408static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
409 char *buf)
1da177e4 410{
86d47f12
JD
411 int n = to_sensor_dev_attr(attr)->index;
412 struct gl520_data *data = gl520_update_device(dev);
413
414 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
1da177e4
LT
415}
416
86d47f12
JD
417static ssize_t get_temp_max(struct device *dev, struct device_attribute *attr,
418 char *buf)
1da177e4 419{
86d47f12
JD
420 int n = to_sensor_dev_attr(attr)->index;
421 struct gl520_data *data = gl520_update_device(dev);
422
423 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
1da177e4
LT
424}
425
86d47f12
JD
426static ssize_t get_temp_max_hyst(struct device *dev, struct device_attribute
427 *attr, char *buf)
1da177e4 428{
86d47f12
JD
429 int n = to_sensor_dev_attr(attr)->index;
430 struct gl520_data *data = gl520_update_device(dev);
431
432 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
1da177e4
LT
433}
434
86d47f12
JD
435static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
1da177e4 437{
86d47f12
JD
438 struct i2c_client *client = to_i2c_client(dev);
439 struct gl520_data *data = i2c_get_clientdata(client);
440 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
441 long v = simple_strtol(buf, NULL, 10);
442
9a61bf63 443 mutex_lock(&data->update_lock);
86d47f12
JD
444 data->temp_max[n] = TEMP_TO_REG(v);
445 gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
9a61bf63 446 mutex_unlock(&data->update_lock);
1da177e4
LT
447 return count;
448}
449
86d47f12
JD
450static ssize_t set_temp_max_hyst(struct device *dev, struct device_attribute
451 *attr, const char *buf, size_t count)
1da177e4 452{
86d47f12
JD
453 struct i2c_client *client = to_i2c_client(dev);
454 struct gl520_data *data = i2c_get_clientdata(client);
455 int n = to_sensor_dev_attr(attr)->index;
1da177e4
LT
456 long v = simple_strtol(buf, NULL, 10);
457
9a61bf63 458 mutex_lock(&data->update_lock);
86d47f12
JD
459 data->temp_max_hyst[n] = TEMP_TO_REG(v);
460 gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
461 data->temp_max_hyst[n]);
9a61bf63 462 mutex_unlock(&data->update_lock);
1da177e4
LT
463 return count;
464}
465
86d47f12
JD
466static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_temp_input, NULL, 0);
467static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_temp_input, NULL, 1);
468static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
469 get_temp_max, set_temp_max, 0);
470static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
471 get_temp_max, set_temp_max, 1);
472static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
473 get_temp_max_hyst, set_temp_max_hyst, 0);
474static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
475 get_temp_max_hyst, set_temp_max_hyst, 1);
476
477static ssize_t get_alarms(struct device *dev, struct device_attribute *attr,
478 char *buf)
1da177e4 479{
86d47f12 480 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
481 return sprintf(buf, "%d\n", data->alarms);
482}
483
86d47f12
JD
484static ssize_t get_beep_enable(struct device *dev, struct device_attribute
485 *attr, char *buf)
1da177e4 486{
86d47f12 487 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
488 return sprintf(buf, "%d\n", data->beep_enable);
489}
490
86d47f12
JD
491static ssize_t get_beep_mask(struct device *dev, struct device_attribute *attr,
492 char *buf)
1da177e4 493{
86d47f12 494 struct gl520_data *data = gl520_update_device(dev);
1da177e4
LT
495 return sprintf(buf, "%d\n", data->beep_mask);
496}
497
86d47f12
JD
498static ssize_t set_beep_enable(struct device *dev, struct device_attribute
499 *attr, const char *buf, size_t count)
1da177e4 500{
86d47f12
JD
501 struct i2c_client *client = to_i2c_client(dev);
502 struct gl520_data *data = i2c_get_clientdata(client);
1da177e4
LT
503 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
504
9a61bf63 505 mutex_lock(&data->update_lock);
1da177e4 506 data->beep_enable = !r;
86d47f12
JD
507 gl520_write_value(client, GL520_REG_BEEP_ENABLE,
508 (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
509 & ~0x04) | (r << 2));
9a61bf63 510 mutex_unlock(&data->update_lock);
1da177e4
LT
511 return count;
512}
513
86d47f12
JD
514static ssize_t set_beep_mask(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
1da177e4 516{
86d47f12
JD
517 struct i2c_client *client = to_i2c_client(dev);
518 struct gl520_data *data = i2c_get_clientdata(client);
1da177e4 519 u8 r = simple_strtoul(buf, NULL, 10);
f28dc2f7 520
9a61bf63 521 mutex_lock(&data->update_lock);
1da177e4
LT
522 r &= data->alarm_mask;
523 data->beep_mask = r;
86d47f12 524 gl520_write_value(client, GL520_REG_BEEP_MASK, r);
9a61bf63 525 mutex_unlock(&data->update_lock);
1da177e4
LT
526 return count;
527}
528
86d47f12
JD
529static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
530static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
531 get_beep_enable, set_beep_enable);
532static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
533 get_beep_mask, set_beep_mask);
534
e86a7760
JD
535static ssize_t get_alarm(struct device *dev, struct device_attribute *attr,
536 char *buf)
537{
538 int bit_nr = to_sensor_dev_attr(attr)->index;
539 struct gl520_data *data = gl520_update_device(dev);
540
541 return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
542}
543
544static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0);
545static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1);
546static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2);
547static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3);
548static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4);
549static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5);
550static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6);
551static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7);
552static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7);
553
554static ssize_t get_beep(struct device *dev, struct device_attribute *attr,
555 char *buf)
556{
557 int bitnr = to_sensor_dev_attr(attr)->index;
558 struct gl520_data *data = gl520_update_device(dev);
559
560 return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
561}
562
563static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
565{
566 struct i2c_client *client = to_i2c_client(dev);
567 struct gl520_data *data = i2c_get_clientdata(client);
568 int bitnr = to_sensor_dev_attr(attr)->index;
569 unsigned long bit;
570
571 bit = simple_strtoul(buf, NULL, 10);
572 if (bit & ~1)
573 return -EINVAL;
574
575 mutex_lock(&data->update_lock);
576 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
577 if (bit)
578 data->beep_mask |= (1 << bitnr);
579 else
580 data->beep_mask &= ~(1 << bitnr);
581 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
582 mutex_unlock(&data->update_lock);
583 return count;
584}
585
586static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0);
587static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1);
588static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2);
589static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3);
590static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4);
591static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5);
592static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6);
593static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
594static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
595
87808be4
JD
596static struct attribute *gl520_attributes[] = {
597 &dev_attr_cpu0_vid.attr,
598
86d47f12
JD
599 &sensor_dev_attr_in0_input.dev_attr.attr,
600 &sensor_dev_attr_in0_min.dev_attr.attr,
601 &sensor_dev_attr_in0_max.dev_attr.attr,
e86a7760
JD
602 &sensor_dev_attr_in0_alarm.dev_attr.attr,
603 &sensor_dev_attr_in0_beep.dev_attr.attr,
86d47f12
JD
604 &sensor_dev_attr_in1_input.dev_attr.attr,
605 &sensor_dev_attr_in1_min.dev_attr.attr,
606 &sensor_dev_attr_in1_max.dev_attr.attr,
e86a7760
JD
607 &sensor_dev_attr_in1_alarm.dev_attr.attr,
608 &sensor_dev_attr_in1_beep.dev_attr.attr,
86d47f12
JD
609 &sensor_dev_attr_in2_input.dev_attr.attr,
610 &sensor_dev_attr_in2_min.dev_attr.attr,
611 &sensor_dev_attr_in2_max.dev_attr.attr,
e86a7760
JD
612 &sensor_dev_attr_in2_alarm.dev_attr.attr,
613 &sensor_dev_attr_in2_beep.dev_attr.attr,
86d47f12
JD
614 &sensor_dev_attr_in3_input.dev_attr.attr,
615 &sensor_dev_attr_in3_min.dev_attr.attr,
616 &sensor_dev_attr_in3_max.dev_attr.attr,
e86a7760
JD
617 &sensor_dev_attr_in3_alarm.dev_attr.attr,
618 &sensor_dev_attr_in3_beep.dev_attr.attr,
86d47f12
JD
619
620 &sensor_dev_attr_fan1_input.dev_attr.attr,
621 &sensor_dev_attr_fan1_min.dev_attr.attr,
622 &sensor_dev_attr_fan1_div.dev_attr.attr,
e86a7760
JD
623 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
624 &sensor_dev_attr_fan1_beep.dev_attr.attr,
87808be4 625 &dev_attr_fan1_off.attr,
86d47f12
JD
626 &sensor_dev_attr_fan2_input.dev_attr.attr,
627 &sensor_dev_attr_fan2_min.dev_attr.attr,
628 &sensor_dev_attr_fan2_div.dev_attr.attr,
e86a7760
JD
629 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
630 &sensor_dev_attr_fan2_beep.dev_attr.attr,
87808be4 631
86d47f12
JD
632 &sensor_dev_attr_temp1_input.dev_attr.attr,
633 &sensor_dev_attr_temp1_max.dev_attr.attr,
634 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
e86a7760
JD
635 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
636 &sensor_dev_attr_temp1_beep.dev_attr.attr,
87808be4
JD
637
638 &dev_attr_alarms.attr,
639 &dev_attr_beep_enable.attr,
640 &dev_attr_beep_mask.attr,
641 NULL
642};
643
644static const struct attribute_group gl520_group = {
645 .attrs = gl520_attributes,
646};
647
648static struct attribute *gl520_attributes_opt[] = {
86d47f12
JD
649 &sensor_dev_attr_in4_input.dev_attr.attr,
650 &sensor_dev_attr_in4_min.dev_attr.attr,
651 &sensor_dev_attr_in4_max.dev_attr.attr,
e86a7760
JD
652 &sensor_dev_attr_in4_alarm.dev_attr.attr,
653 &sensor_dev_attr_in4_beep.dev_attr.attr,
87808be4 654
86d47f12
JD
655 &sensor_dev_attr_temp2_input.dev_attr.attr,
656 &sensor_dev_attr_temp2_max.dev_attr.attr,
657 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
e86a7760
JD
658 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
659 &sensor_dev_attr_temp2_beep.dev_attr.attr,
87808be4
JD
660 NULL
661};
662
663static const struct attribute_group gl520_group_opt = {
664 .attrs = gl520_attributes_opt,
665};
666
1da177e4
LT
667
668/*
669 * Real code
670 */
671
672static int gl520_attach_adapter(struct i2c_adapter *adapter)
673{
674 if (!(adapter->class & I2C_CLASS_HWMON))
675 return 0;
2ed2dc3c 676 return i2c_probe(adapter, &addr_data, gl520_detect);
1da177e4
LT
677}
678
679static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
680{
f28dc2f7 681 struct i2c_client *client;
1da177e4
LT
682 struct gl520_data *data;
683 int err = 0;
684
685 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
686 I2C_FUNC_SMBUS_WORD_DATA))
687 goto exit;
688
689 /* OK. For now, we presume we have a valid client. We now create the
690 client structure, even though we cannot fill it completely yet.
691 But it allows us to access gl520_{read,write}_value. */
692
ba9c2e8d 693 if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
1da177e4
LT
694 err = -ENOMEM;
695 goto exit;
696 }
1da177e4 697
f28dc2f7
JD
698 client = &data->client;
699 i2c_set_clientdata(client, data);
700 client->addr = address;
701 client->adapter = adapter;
702 client->driver = &gl520_driver;
1da177e4
LT
703
704 /* Determine the chip type. */
705 if (kind < 0) {
f28dc2f7
JD
706 if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
707 ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
708 ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
709 dev_dbg(&client->dev, "Unknown chip type, skipping\n");
1da177e4
LT
710 goto exit_free;
711 }
712 }
713
714 /* Fill in the remaining client fields */
f28dc2f7 715 strlcpy(client->name, "gl520sm", I2C_NAME_SIZE);
9a61bf63 716 mutex_init(&data->update_lock);
1da177e4
LT
717
718 /* Tell the I2C layer a new client has arrived */
f28dc2f7 719 if ((err = i2c_attach_client(client)))
1da177e4
LT
720 goto exit_free;
721
722 /* Initialize the GL520SM chip */
f28dc2f7 723 gl520_init_client(client);
1da177e4
LT
724
725 /* Register sysfs hooks */
f28dc2f7 726 if ((err = sysfs_create_group(&client->dev.kobj, &gl520_group)))
943b0830 727 goto exit_detach;
1da177e4 728
87808be4 729 if (data->two_temps) {
f28dc2f7 730 if ((err = device_create_file(&client->dev,
86d47f12 731 &sensor_dev_attr_temp2_input.dev_attr))
f28dc2f7 732 || (err = device_create_file(&client->dev,
86d47f12 733 &sensor_dev_attr_temp2_max.dev_attr))
f28dc2f7 734 || (err = device_create_file(&client->dev,
e86a7760
JD
735 &sensor_dev_attr_temp2_max_hyst.dev_attr))
736 || (err = device_create_file(&client->dev,
737 &sensor_dev_attr_temp2_alarm.dev_attr))
738 || (err = device_create_file(&client->dev,
739 &sensor_dev_attr_temp2_beep.dev_attr)))
87808be4
JD
740 goto exit_remove_files;
741 } else {
f28dc2f7 742 if ((err = device_create_file(&client->dev,
86d47f12 743 &sensor_dev_attr_in4_input.dev_attr))
f28dc2f7 744 || (err = device_create_file(&client->dev,
86d47f12 745 &sensor_dev_attr_in4_min.dev_attr))
f28dc2f7 746 || (err = device_create_file(&client->dev,
e86a7760
JD
747 &sensor_dev_attr_in4_max.dev_attr))
748 || (err = device_create_file(&client->dev,
749 &sensor_dev_attr_in4_alarm.dev_attr))
750 || (err = device_create_file(&client->dev,
751 &sensor_dev_attr_in4_beep.dev_attr)))
87808be4
JD
752 goto exit_remove_files;
753 }
1da177e4 754
1da177e4 755
f28dc2f7 756 data->hwmon_dev = hwmon_device_register(&client->dev);
1beeffe4
TJ
757 if (IS_ERR(data->hwmon_dev)) {
758 err = PTR_ERR(data->hwmon_dev);
87808be4
JD
759 goto exit_remove_files;
760 }
1da177e4
LT
761
762 return 0;
763
87808be4 764exit_remove_files:
f28dc2f7
JD
765 sysfs_remove_group(&client->dev.kobj, &gl520_group);
766 sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
943b0830 767exit_detach:
f28dc2f7 768 i2c_detach_client(client);
1da177e4
LT
769exit_free:
770 kfree(data);
771exit:
772 return err;
773}
774
775
776/* Called when we have found a new GL520SM. */
777static void gl520_init_client(struct i2c_client *client)
778{
779 struct gl520_data *data = i2c_get_clientdata(client);
780 u8 oldconf, conf;
781
782 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
783
784 data->alarm_mask = 0xff;
303760b4 785 data->vrm = vid_which_vrm();
1da177e4
LT
786
787 if (extra_sensor_type == 1)
788 conf &= ~0x10;
789 else if (extra_sensor_type == 2)
790 conf |= 0x10;
791 data->two_temps = !(conf & 0x10);
792
793 /* If IRQ# is disabled, we can safely force comparator mode */
794 if (!(conf & 0x20))
795 conf &= 0xf7;
796
797 /* Enable monitoring if needed */
798 conf |= 0x40;
799
800 if (conf != oldconf)
801 gl520_write_value(client, GL520_REG_CONF, conf);
802
803 gl520_update_device(&(client->dev));
804
805 if (data->fan_min[0] == 0)
806 data->alarm_mask &= ~0x20;
807 if (data->fan_min[1] == 0)
808 data->alarm_mask &= ~0x40;
809
810 data->beep_mask &= data->alarm_mask;
811 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
812}
813
814static int gl520_detach_client(struct i2c_client *client)
815{
943b0830 816 struct gl520_data *data = i2c_get_clientdata(client);
1da177e4
LT
817 int err;
818
1beeffe4 819 hwmon_device_unregister(data->hwmon_dev);
87808be4
JD
820 sysfs_remove_group(&client->dev.kobj, &gl520_group);
821 sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
943b0830 822
7bef5594 823 if ((err = i2c_detach_client(client)))
1da177e4 824 return err;
1da177e4 825
943b0830 826 kfree(data);
1da177e4
LT
827 return 0;
828}
829
830
f28dc2f7 831/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
1da177e4
LT
832 GL520 uses a high-byte first convention */
833static int gl520_read_value(struct i2c_client *client, u8 reg)
834{
835 if ((reg >= 0x07) && (reg <= 0x0c))
836 return swab16(i2c_smbus_read_word_data(client, reg));
837 else
838 return i2c_smbus_read_byte_data(client, reg);
839}
840
841static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
842{
843 if ((reg >= 0x07) && (reg <= 0x0c))
844 return i2c_smbus_write_word_data(client, reg, swab16(value));
845 else
846 return i2c_smbus_write_byte_data(client, reg, value);
847}
848
849
850static struct gl520_data *gl520_update_device(struct device *dev)
851{
852 struct i2c_client *client = to_i2c_client(dev);
853 struct gl520_data *data = i2c_get_clientdata(client);
8b4b0ab4 854 int val, i;
1da177e4 855
9a61bf63 856 mutex_lock(&data->update_lock);
1da177e4 857
0cacdf29 858 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1da177e4
LT
859
860 dev_dbg(&client->dev, "Starting gl520sm update\n");
861
862 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
863 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
864 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
865
8b4b0ab4
JD
866 for (i = 0; i < 4; i++) {
867 data->in_input[i] = gl520_read_value(client,
868 GL520_REG_IN_INPUT[i]);
869 val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]);
870 data->in_min[i] = val & 0xff;
871 data->in_max[i] = (val >> 8) & 0xff;
872 }
1da177e4
LT
873
874 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
875 data->fan_input[0] = (val >> 8) & 0xff;
876 data->fan_input[1] = val & 0xff;
877
878 val = gl520_read_value(client, GL520_REG_FAN_MIN);
879 data->fan_min[0] = (val >> 8) & 0xff;
880 data->fan_min[1] = val & 0xff;
881
8b4b0ab4
JD
882 data->temp_input[0] = gl520_read_value(client,
883 GL520_REG_TEMP_INPUT[0]);
884 data->temp_max[0] = gl520_read_value(client,
885 GL520_REG_TEMP_MAX[0]);
886 data->temp_max_hyst[0] = gl520_read_value(client,
887 GL520_REG_TEMP_MAX_HYST[0]);
1da177e4
LT
888
889 val = gl520_read_value(client, GL520_REG_FAN_DIV);
890 data->fan_div[0] = (val >> 6) & 0x03;
891 data->fan_div[1] = (val >> 4) & 0x03;
892 data->fan_off = (val >> 2) & 0x01;
893
894 data->alarms &= data->alarm_mask;
895
896 val = gl520_read_value(client, GL520_REG_CONF);
897 data->beep_enable = !((val >> 2) & 1);
898
1da177e4
LT
899 /* Temp1 and Vin4 are the same input */
900 if (data->two_temps) {
8b4b0ab4
JD
901 data->temp_input[1] = gl520_read_value(client,
902 GL520_REG_TEMP_INPUT[1]);
903 data->temp_max[1] = gl520_read_value(client,
904 GL520_REG_TEMP_MAX[1]);
905 data->temp_max_hyst[1] = gl520_read_value(client,
906 GL520_REG_TEMP_MAX_HYST[1]);
1da177e4 907 } else {
8b4b0ab4
JD
908 data->in_input[4] = gl520_read_value(client,
909 GL520_REG_IN_INPUT[4]);
910 data->in_min[4] = gl520_read_value(client,
911 GL520_REG_IN_MIN[4]);
912 data->in_max[4] = gl520_read_value(client,
913 GL520_REG_IN_MAX[4]);
1da177e4
LT
914 }
915
916 data->last_updated = jiffies;
917 data->valid = 1;
918 }
919
9a61bf63 920 mutex_unlock(&data->update_lock);
1da177e4
LT
921
922 return data;
923}
924
925
926static int __init sensors_gl520sm_init(void)
927{
928 return i2c_add_driver(&gl520_driver);
929}
930
931static void __exit sensors_gl520sm_exit(void)
932{
933 i2c_del_driver(&gl520_driver);
934}
935
936
937MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
96de0e25 938 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
1da177e4
LT
939 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
940MODULE_DESCRIPTION("GL520SM driver");
941MODULE_LICENSE("GPL");
942
943module_init(sensors_gl520sm_init);
944module_exit(sensors_gl520sm_exit);