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