sdhci: use SG_MITER_TO_SG/SG_MITER_FROM_SG
[linux-2.6] / drivers / hwmon / adt7462.c
1 /*
2  * A hwmon driver for the Analog Devices ADT7462
3  * Copyright (C) 2008 IBM
4  *
5  * Author: Darrick J. Wong <djwong@us.ibm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
31
32 /* Addresses to scan */
33 static const unsigned short normal_i2c[] = { 0x58, 0x5C, I2C_CLIENT_END };
34
35 /* Insmod parameters */
36 I2C_CLIENT_INSMOD_1(adt7462);
37
38 /* ADT7462 registers */
39 #define ADT7462_REG_DEVICE                      0x3D
40 #define ADT7462_REG_VENDOR                      0x3E
41 #define ADT7462_REG_REVISION                    0x3F
42
43 #define ADT7462_REG_MIN_TEMP_BASE_ADDR          0x44
44 #define ADT7462_REG_MIN_TEMP_MAX_ADDR           0x47
45 #define ADT7462_REG_MAX_TEMP_BASE_ADDR          0x48
46 #define ADT7462_REG_MAX_TEMP_MAX_ADDR           0x4B
47 #define ADT7462_REG_TEMP_BASE_ADDR              0x88
48 #define ADT7462_REG_TEMP_MAX_ADDR               0x8F
49
50 #define ADT7462_REG_FAN_BASE_ADDR               0x98
51 #define ADT7462_REG_FAN_MAX_ADDR                0x9F
52 #define ADT7462_REG_FAN2_BASE_ADDR              0xA2
53 #define ADT7462_REG_FAN2_MAX_ADDR               0xA9
54 #define ADT7462_REG_FAN_ENABLE                  0x07
55 #define ADT7462_REG_FAN_MIN_BASE_ADDR           0x78
56 #define ADT7462_REG_FAN_MIN_MAX_ADDR            0x7F
57
58 #define ADT7462_REG_CFG2                        0x02
59 #define         ADT7462_FSPD_MASK               0x20
60
61 #define ADT7462_REG_PWM_BASE_ADDR               0xAA
62 #define ADT7462_REG_PWM_MAX_ADDR                0xAD
63 #define ADT7462_REG_PWM_MIN_BASE_ADDR           0x28
64 #define ADT7462_REG_PWM_MIN_MAX_ADDR            0x2B
65 #define ADT7462_REG_PWM_MAX                     0x2C
66 #define ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR      0x5C
67 #define ADT7462_REG_PWM_TEMP_MIN_MAX_ADDR       0x5F
68 #define ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR    0x60
69 #define ADT7462_REG_PWM_TEMP_RANGE_MAX_ADDR     0x63
70 #define         ADT7462_PWM_HYST_MASK           0x0F
71 #define         ADT7462_PWM_RANGE_MASK          0xF0
72 #define         ADT7462_PWM_RANGE_SHIFT         4
73 #define ADT7462_REG_PWM_CFG_BASE_ADDR           0x21
74 #define ADT7462_REG_PWM_CFG_MAX_ADDR            0x24
75 #define         ADT7462_PWM_CHANNEL_MASK        0xE0
76 #define         ADT7462_PWM_CHANNEL_SHIFT       5
77
78 #define ADT7462_REG_PIN_CFG_BASE_ADDR           0x10
79 #define ADT7462_REG_PIN_CFG_MAX_ADDR            0x13
80 #define         ADT7462_PIN7_INPUT              0x01    /* cfg0 */
81 #define         ADT7462_DIODE3_INPUT            0x20
82 #define         ADT7462_DIODE1_INPUT            0x40
83 #define         ADT7462_VID_INPUT               0x80
84 #define         ADT7462_PIN22_INPUT             0x04    /* cfg1 */
85 #define         ADT7462_PIN21_INPUT             0x08
86 #define         ADT7462_PIN19_INPUT             0x10
87 #define         ADT7462_PIN15_INPUT             0x20
88 #define         ADT7462_PIN13_INPUT             0x40
89 #define         ADT7462_PIN8_INPUT              0x80
90 #define         ADT7462_PIN23_MASK              0x03
91 #define         ADT7462_PIN23_SHIFT             0
92 #define         ADT7462_PIN26_MASK              0x0C    /* cfg2 */
93 #define         ADT7462_PIN26_SHIFT             2
94 #define         ADT7462_PIN25_MASK              0x30
95 #define         ADT7462_PIN25_SHIFT             4
96 #define         ADT7462_PIN24_MASK              0xC0
97 #define         ADT7462_PIN24_SHIFT             6
98 #define         ADT7462_PIN26_VOLT_INPUT        0x08
99 #define         ADT7462_PIN25_VOLT_INPUT        0x20
100 #define         ADT7462_PIN28_SHIFT             6       /* cfg3 */
101 #define         ADT7462_PIN28_VOLT              0x5
102
103 #define ADT7462_REG_ALARM1                      0xB8
104 #define         ADT7462_LT_ALARM                0x02
105 #define         ADT7462_R1T_ALARM               0x04
106 #define         ADT7462_R2T_ALARM               0x08
107 #define         ADT7462_R3T_ALARM               0x10
108 #define ADT7462_REG_ALARM2                      0xBB
109 #define         ADT7462_V0_ALARM                0x01
110 #define         ADT7462_V1_ALARM                0x02
111 #define         ADT7462_V2_ALARM                0x04
112 #define         ADT7462_V3_ALARM                0x08
113 #define         ADT7462_V4_ALARM                0x10
114 #define         ADT7462_V5_ALARM                0x20
115 #define         ADT7462_V6_ALARM                0x40
116 #define         ADT7462_V7_ALARM                0x80
117 #define ADT7462_REG_ALARM3                      0xBC
118 #define         ADT7462_V8_ALARM                0x08
119 #define         ADT7462_V9_ALARM                0x10
120 #define         ADT7462_V10_ALARM               0x20
121 #define         ADT7462_V11_ALARM               0x40
122 #define         ADT7462_V12_ALARM               0x80
123 #define ADT7462_REG_ALARM4                      0xBD
124 #define         ADT7462_F0_ALARM                0x01
125 #define         ADT7462_F1_ALARM                0x02
126 #define         ADT7462_F2_ALARM                0x04
127 #define         ADT7462_F3_ALARM                0x08
128 #define         ADT7462_F4_ALARM                0x10
129 #define         ADT7462_F5_ALARM                0x20
130 #define         ADT7462_F6_ALARM                0x40
131 #define         ADT7462_F7_ALARM                0x80
132 #define ADT7462_ALARM1                          0x0000
133 #define ADT7462_ALARM2                          0x0100
134 #define ADT7462_ALARM3                          0x0200
135 #define ADT7462_ALARM4                          0x0300
136 #define ADT7462_ALARM_REG_SHIFT                 8
137 #define ADT7462_ALARM_FLAG_MASK                 0x0F
138
139 #define ADT7462_TEMP_COUNT              4
140 #define ADT7462_TEMP_REG(x)             (ADT7462_REG_TEMP_BASE_ADDR + (x * 2))
141 #define ADT7462_TEMP_MIN_REG(x)         (ADT7462_REG_MIN_TEMP_BASE_ADDR + (x))
142 #define ADT7462_TEMP_MAX_REG(x)         (ADT7462_REG_MAX_TEMP_BASE_ADDR + (x))
143 #define TEMP_FRAC_OFFSET                6
144
145 #define ADT7462_FAN_COUNT               8
146 #define ADT7462_REG_FAN_MIN(x)          (ADT7462_REG_FAN_MIN_BASE_ADDR + (x))
147
148 #define ADT7462_PWM_COUNT               4
149 #define ADT7462_REG_PWM(x)              (ADT7462_REG_PWM_BASE_ADDR + (x))
150 #define ADT7462_REG_PWM_MIN(x)          (ADT7462_REG_PWM_MIN_BASE_ADDR + (x))
151 #define ADT7462_REG_PWM_TMIN(x)         \
152         (ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
153 #define ADT7462_REG_PWM_TRANGE(x)       \
154         (ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR + (x))
155
156 #define ADT7462_PIN_CFG_REG_COUNT       4
157 #define ADT7462_REG_PIN_CFG(x)          (ADT7462_REG_PIN_CFG_BASE_ADDR + (x))
158 #define ADT7462_REG_PWM_CFG(x)          (ADT7462_REG_PWM_CFG_BASE_ADDR + (x))
159
160 #define ADT7462_ALARM_REG_COUNT         4
161
162 /*
163  * The chip can measure 13 different voltage sources:
164  *
165  * 1. +12V1 (pin 7)
166  * 2. Vccp1/+2.5V/+1.8V/+1.5V (pin 23)
167  * 3. +12V3 (pin 22)
168  * 4. +5V (pin 21)
169  * 5. +1.25V/+0.9V (pin 19)
170  * 6. +2.5V/+1.8V (pin 15)
171  * 7. +3.3v (pin 13)
172  * 8. +12V2 (pin 8)
173  * 9. Vbatt/FSB_Vtt (pin 26)
174  * A. +3.3V/+1.2V1 (pin 25)
175  * B. Vccp2/+2.5V/+1.8V/+1.5V (pin 24)
176  * C. +1.5V ICH (only if BOTH pin 28/29 are set to +1.5V)
177  * D. +1.5V 3GPIO (only if BOTH pin 28/29 are set to +1.5V)
178  *
179  * Each of these 13 has a factor to convert raw to voltage.  Even better,
180  * the pins can be connected to other sensors (tach/gpio/hot/etc), which
181  * makes the bookkeeping tricky.
182  *
183  * Some, but not all, of these voltages have low/high limits.
184  */
185 #define ADT7462_VOLT_COUNT      12
186
187 #define ADT7462_VENDOR          0x41
188 #define ADT7462_DEVICE          0x62
189 /* datasheet only mentions a revision 4 */
190 #define ADT7462_REVISION        0x04
191
192 /* How often do we reread sensors values? (In jiffies) */
193 #define SENSOR_REFRESH_INTERVAL (2 * HZ)
194
195 /* How often do we reread sensor limit values? (In jiffies) */
196 #define LIMIT_REFRESH_INTERVAL  (60 * HZ)
197
198 /* datasheet says to divide this number by the fan reading to get fan rpm */
199 #define FAN_PERIOD_TO_RPM(x)    ((90000 * 60) / (x))
200 #define FAN_RPM_TO_PERIOD       FAN_PERIOD_TO_RPM
201 #define FAN_PERIOD_INVALID      65535
202 #define FAN_DATA_VALID(x)       ((x) && (x) != FAN_PERIOD_INVALID)
203
204 #define MASK_AND_SHIFT(value, prefix)   \
205         (((value) & prefix##_MASK) >> prefix##_SHIFT)
206
207 struct adt7462_data {
208         struct device           *hwmon_dev;
209         struct attribute_group  attrs;
210         struct mutex            lock;
211         char                    sensors_valid;
212         char                    limits_valid;
213         unsigned long           sensors_last_updated;   /* In jiffies */
214         unsigned long           limits_last_updated;    /* In jiffies */
215
216         u8                      temp[ADT7462_TEMP_COUNT];
217                                 /* bits 6-7 are quarter pieces of temp */
218         u8                      temp_frac[ADT7462_TEMP_COUNT];
219         u8                      temp_min[ADT7462_TEMP_COUNT];
220         u8                      temp_max[ADT7462_TEMP_COUNT];
221         u16                     fan[ADT7462_FAN_COUNT];
222         u8                      fan_enabled;
223         u8                      fan_min[ADT7462_FAN_COUNT];
224         u8                      cfg2;
225         u8                      pwm[ADT7462_PWM_COUNT];
226         u8                      pin_cfg[ADT7462_PIN_CFG_REG_COUNT];
227         u8                      voltages[ADT7462_VOLT_COUNT];
228         u8                      volt_max[ADT7462_VOLT_COUNT];
229         u8                      volt_min[ADT7462_VOLT_COUNT];
230         u8                      pwm_min[ADT7462_PWM_COUNT];
231         u8                      pwm_tmin[ADT7462_PWM_COUNT];
232         u8                      pwm_trange[ADT7462_PWM_COUNT];
233         u8                      pwm_max;        /* only one per chip */
234         u8                      pwm_cfg[ADT7462_PWM_COUNT];
235         u8                      alarms[ADT7462_ALARM_REG_COUNT];
236 };
237
238 static int adt7462_probe(struct i2c_client *client,
239                          const struct i2c_device_id *id);
240 static int adt7462_detect(struct i2c_client *client, int kind,
241                           struct i2c_board_info *info);
242 static int adt7462_remove(struct i2c_client *client);
243
244 static const struct i2c_device_id adt7462_id[] = {
245         { "adt7462", adt7462 },
246         { }
247 };
248 MODULE_DEVICE_TABLE(i2c, adt7462_id);
249
250 static struct i2c_driver adt7462_driver = {
251         .class          = I2C_CLASS_HWMON,
252         .driver = {
253                 .name   = "adt7462",
254         },
255         .probe          = adt7462_probe,
256         .remove         = adt7462_remove,
257         .id_table       = adt7462_id,
258         .detect         = adt7462_detect,
259         .address_data   = &addr_data,
260 };
261
262 /*
263  * 16-bit registers on the ADT7462 are low-byte first.  The data sheet says
264  * that the low byte must be read before the high byte.
265  */
266 static inline int adt7462_read_word_data(struct i2c_client *client, u8 reg)
267 {
268         u16 foo;
269         foo = i2c_smbus_read_byte_data(client, reg);
270         foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
271         return foo;
272 }
273
274 /* For some reason these registers are not contiguous. */
275 static int ADT7462_REG_FAN(int fan)
276 {
277         if (fan < 4)
278                 return ADT7462_REG_FAN_BASE_ADDR + (2 * fan);
279         return ADT7462_REG_FAN2_BASE_ADDR + (2 * (fan - 4));
280 }
281
282 /* Voltage registers are scattered everywhere */
283 static int ADT7462_REG_VOLT_MAX(struct adt7462_data *data, int which)
284 {
285         switch (which) {
286         case 0:
287                 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
288                         return 0x7C;
289                 break;
290         case 1:
291                 return 0x69;
292         case 2:
293                 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
294                         return 0x7F;
295                 break;
296         case 3:
297                 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
298                         return 0x7E;
299                 break;
300         case 4:
301                 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
302                         return 0x4B;
303                 break;
304         case 5:
305                 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
306                         return 0x49;
307                 break;
308         case 6:
309                 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
310                         return 0x68;
311                 break;
312         case 7:
313                 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
314                         return 0x7D;
315                 break;
316         case 8:
317                 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
318                         return 0x6C;
319                 break;
320         case 9:
321                 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
322                         return 0x6B;
323                 break;
324         case 10:
325                 return 0x6A;
326         case 11:
327                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
328                                         ADT7462_PIN28_VOLT &&
329                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
330                         return 0x50;
331                 break;
332         case 12:
333                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
334                                         ADT7462_PIN28_VOLT &&
335                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
336                         return 0x4C;
337                 break;
338         }
339         return -ENODEV;
340 }
341
342 static int ADT7462_REG_VOLT_MIN(struct adt7462_data *data, int which)
343 {
344         switch (which) {
345         case 0:
346                 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
347                         return 0x6D;
348                 break;
349         case 1:
350                 return 0x72;
351         case 2:
352                 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
353                         return 0x6F;
354                 break;
355         case 3:
356                 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
357                         return 0x71;
358                 break;
359         case 4:
360                 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
361                         return 0x47;
362                 break;
363         case 5:
364                 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
365                         return 0x45;
366                 break;
367         case 6:
368                 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
369                         return 0x70;
370                 break;
371         case 7:
372                 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
373                         return 0x6E;
374                 break;
375         case 8:
376                 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
377                         return 0x75;
378                 break;
379         case 9:
380                 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
381                         return 0x74;
382                 break;
383         case 10:
384                 return 0x73;
385         case 11:
386                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
387                                         ADT7462_PIN28_VOLT &&
388                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
389                         return 0x76;
390                 break;
391         case 12:
392                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
393                                         ADT7462_PIN28_VOLT &&
394                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
395                         return 0x77;
396                 break;
397         }
398         return -ENODEV;
399 }
400
401 static int ADT7462_REG_VOLT(struct adt7462_data *data, int which)
402 {
403         switch (which) {
404         case 0:
405                 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
406                         return 0xA3;
407                 break;
408         case 1:
409                 return 0x90;
410         case 2:
411                 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
412                         return 0xA9;
413                 break;
414         case 3:
415                 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
416                         return 0xA7;
417                 break;
418         case 4:
419                 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
420                         return 0x8F;
421                 break;
422         case 5:
423                 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
424                         return 0x8B;
425                 break;
426         case 6:
427                 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
428                         return 0x96;
429                 break;
430         case 7:
431                 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
432                         return 0xA5;
433                 break;
434         case 8:
435                 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
436                         return 0x93;
437                 break;
438         case 9:
439                 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
440                         return 0x92;
441                 break;
442         case 10:
443                 return 0x91;
444         case 11:
445                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
446                                         ADT7462_PIN28_VOLT &&
447                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
448                         return 0x94;
449                 break;
450         case 12:
451                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
452                                         ADT7462_PIN28_VOLT &&
453                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
454                         return 0x95;
455                 break;
456         }
457         return -ENODEV;
458 }
459
460 /* Provide labels for sysfs */
461 static const char *voltage_label(struct adt7462_data *data, int which)
462 {
463         switch (which) {
464         case 0:
465                 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
466                         return "+12V1";
467                 break;
468         case 1:
469                 switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
470                 case 0:
471                         return "Vccp1";
472                 case 1:
473                         return "+2.5V";
474                 case 2:
475                         return "+1.8V";
476                 case 3:
477                         return "+1.5V";
478                 }
479         case 2:
480                 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
481                         return "+12V3";
482                 break;
483         case 3:
484                 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
485                         return "+5V";
486                 break;
487         case 4:
488                 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
489                         if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
490                                 return "+0.9V";
491                         return "+1.25V";
492                 }
493                 break;
494         case 5:
495                 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
496                         if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
497                                 return "+1.8V";
498                         return "+2.5V";
499                 }
500                 break;
501         case 6:
502                 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
503                         return "+3.3V";
504                 break;
505         case 7:
506                 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
507                         return "+12V2";
508                 break;
509         case 8:
510                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
511                 case 0:
512                         return "Vbatt";
513                 case 1:
514                         return "FSB_Vtt";
515                 }
516                 break;
517         case 9:
518                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
519                 case 0:
520                         return "+3.3V";
521                 case 1:
522                         return "+1.2V1";
523                 }
524                 break;
525         case 10:
526                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
527                 case 0:
528                         return "Vccp2";
529                 case 1:
530                         return "+2.5V";
531                 case 2:
532                         return "+1.8V";
533                 case 3:
534                         return "+1.5";
535                 }
536         case 11:
537                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
538                                         ADT7462_PIN28_VOLT &&
539                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
540                         return "+1.5V ICH";
541                 break;
542         case 12:
543                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
544                                         ADT7462_PIN28_VOLT &&
545                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
546                         return "+1.5V 3GPIO";
547                 break;
548         }
549         return "N/A";
550 }
551
552 /* Multipliers are actually in uV, not mV. */
553 static int voltage_multiplier(struct adt7462_data *data, int which)
554 {
555         switch (which) {
556         case 0:
557                 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
558                         return 62500;
559                 break;
560         case 1:
561                 switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
562                 case 0:
563                         if (data->pin_cfg[0] & ADT7462_VID_INPUT)
564                                 return 12500;
565                         return 6250;
566                 case 1:
567                         return 13000;
568                 case 2:
569                         return 9400;
570                 case 3:
571                         return 7800;
572                 }
573         case 2:
574                 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
575                         return 62500;
576                 break;
577         case 3:
578                 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
579                         return 26000;
580                 break;
581         case 4:
582                 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
583                         if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
584                                 return 4690;
585                         return 6500;
586                 }
587                 break;
588         case 5:
589                 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
590                         if (data->pin_cfg[1] & ADT7462_PIN15_INPUT)
591                                 return 9400;
592                         return 13000;
593                 }
594                 break;
595         case 6:
596                 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
597                         return 17200;
598                 break;
599         case 7:
600                 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
601                         return 62500;
602                 break;
603         case 8:
604                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
605                 case 0:
606                         return 15600;
607                 case 1:
608                         return 6250;
609                 }
610                 break;
611         case 9:
612                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
613                 case 0:
614                         return 17200;
615                 case 1:
616                         return 6250;
617                 }
618                 break;
619         case 10:
620                 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
621                 case 0:
622                         return 6250;
623                 case 1:
624                         return 13000;
625                 case 2:
626                         return 9400;
627                 case 3:
628                         return 7800;
629                 }
630         case 11:
631         case 12:
632                 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
633                                         ADT7462_PIN28_VOLT &&
634                     !(data->pin_cfg[0] & ADT7462_VID_INPUT))
635                         return 7800;
636         }
637         return 0;
638 }
639
640 static int temp_enabled(struct adt7462_data *data, int which)
641 {
642         switch (which) {
643         case 0:
644         case 2:
645                 return 1;
646         case 1:
647                 if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
648                         return 1;
649                 break;
650         case 3:
651                 if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
652                         return 1;
653                 break;
654         }
655         return 0;
656 }
657
658 static const char *temp_label(struct adt7462_data *data, int which)
659 {
660         switch (which) {
661         case 0:
662                 return "local";
663         case 1:
664                 if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
665                         return "remote1";
666                 break;
667         case 2:
668                 return "remote2";
669         case 3:
670                 if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
671                         return "remote3";
672                 break;
673         }
674         return "N/A";
675 }
676
677 /* Map Trange register values to mC */
678 #define NUM_TRANGE_VALUES       16
679 static const int trange_values[NUM_TRANGE_VALUES] = {
680         2000,
681         2500,
682         3300,
683         4000,
684         5000,
685         6700,
686         8000,
687         10000,
688         13300,
689         16000,
690         20000,
691         26700,
692         32000,
693         40000,
694         53300,
695         80000
696 };
697
698 static int find_trange_value(int trange)
699 {
700         int i;
701
702         for (i = 0; i < NUM_TRANGE_VALUES; i++)
703                 if (trange_values[i] == trange)
704                         return i;
705
706         return -ENODEV;
707 }
708
709 static struct adt7462_data *adt7462_update_device(struct device *dev)
710 {
711         struct i2c_client *client = to_i2c_client(dev);
712         struct adt7462_data *data = i2c_get_clientdata(client);
713         unsigned long local_jiffies = jiffies;
714         int i;
715
716         mutex_lock(&data->lock);
717         if (time_before(local_jiffies, data->sensors_last_updated +
718                 SENSOR_REFRESH_INTERVAL)
719                 && data->sensors_valid)
720                 goto no_sensor_update;
721
722         for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
723                 /*
724                  * Reading the fractional register locks the integral
725                  * register until both have been read.
726                  */
727                 data->temp_frac[i] = i2c_smbus_read_byte_data(client,
728                                                 ADT7462_TEMP_REG(i));
729                 data->temp[i] = i2c_smbus_read_byte_data(client,
730                                                 ADT7462_TEMP_REG(i) + 1);
731         }
732
733         for (i = 0; i < ADT7462_FAN_COUNT; i++)
734                 data->fan[i] = adt7462_read_word_data(client,
735                                                 ADT7462_REG_FAN(i));
736
737         data->fan_enabled = i2c_smbus_read_byte_data(client,
738                                         ADT7462_REG_FAN_ENABLE);
739
740         for (i = 0; i < ADT7462_PWM_COUNT; i++)
741                 data->pwm[i] = i2c_smbus_read_byte_data(client,
742                                                 ADT7462_REG_PWM(i));
743
744         for (i = 0; i < ADT7462_PIN_CFG_REG_COUNT; i++)
745                 data->pin_cfg[i] = i2c_smbus_read_byte_data(client,
746                                 ADT7462_REG_PIN_CFG(i));
747
748         for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
749                 int reg = ADT7462_REG_VOLT(data, i);
750                 if (!reg)
751                         data->voltages[i] = 0;
752                 else
753                         data->voltages[i] = i2c_smbus_read_byte_data(client,
754                                                                      reg);
755         }
756
757         data->alarms[0] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM1);
758         data->alarms[1] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM2);
759         data->alarms[2] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM3);
760         data->alarms[3] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM4);
761
762         data->sensors_last_updated = local_jiffies;
763         data->sensors_valid = 1;
764
765 no_sensor_update:
766         if (time_before(local_jiffies, data->limits_last_updated +
767                 LIMIT_REFRESH_INTERVAL)
768                 && data->limits_valid)
769                 goto out;
770
771         for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
772                 data->temp_min[i] = i2c_smbus_read_byte_data(client,
773                                                 ADT7462_TEMP_MIN_REG(i));
774                 data->temp_max[i] = i2c_smbus_read_byte_data(client,
775                                                 ADT7462_TEMP_MAX_REG(i));
776         }
777
778         for (i = 0; i < ADT7462_FAN_COUNT; i++)
779                 data->fan_min[i] = i2c_smbus_read_byte_data(client,
780                                                 ADT7462_REG_FAN_MIN(i));
781
782         for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
783                 int reg = ADT7462_REG_VOLT_MAX(data, i);
784                 data->volt_max[i] =
785                         (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
786
787                 reg = ADT7462_REG_VOLT_MIN(data, i);
788                 data->volt_min[i] =
789                         (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
790         }
791
792         for (i = 0; i < ADT7462_PWM_COUNT; i++) {
793                 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
794                                                 ADT7462_REG_PWM_MIN(i));
795                 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
796                                                 ADT7462_REG_PWM_TMIN(i));
797                 data->pwm_trange[i] = i2c_smbus_read_byte_data(client,
798                                                 ADT7462_REG_PWM_TRANGE(i));
799                 data->pwm_cfg[i] = i2c_smbus_read_byte_data(client,
800                                                 ADT7462_REG_PWM_CFG(i));
801         }
802
803         data->pwm_max = i2c_smbus_read_byte_data(client, ADT7462_REG_PWM_MAX);
804
805         data->cfg2 = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
806
807         data->limits_last_updated = local_jiffies;
808         data->limits_valid = 1;
809
810 out:
811         mutex_unlock(&data->lock);
812         return data;
813 }
814
815 static ssize_t show_temp_min(struct device *dev,
816                              struct device_attribute *devattr,
817                              char *buf)
818 {
819         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
820         struct adt7462_data *data = adt7462_update_device(dev);
821
822         if (!temp_enabled(data, attr->index))
823                 return sprintf(buf, "0\n");
824
825         return sprintf(buf, "%d\n", 1000 * (data->temp_min[attr->index] - 64));
826 }
827
828 static ssize_t set_temp_min(struct device *dev,
829                             struct device_attribute *devattr,
830                             const char *buf,
831                             size_t count)
832 {
833         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
834         struct i2c_client *client = to_i2c_client(dev);
835         struct adt7462_data *data = i2c_get_clientdata(client);
836         long temp;
837
838         if (strict_strtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
839                 return -EINVAL;
840
841         temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
842         temp = SENSORS_LIMIT(temp, 0, 255);
843
844         mutex_lock(&data->lock);
845         data->temp_min[attr->index] = temp;
846         i2c_smbus_write_byte_data(client, ADT7462_TEMP_MIN_REG(attr->index),
847                                   temp);
848         mutex_unlock(&data->lock);
849
850         return count;
851 }
852
853 static ssize_t show_temp_max(struct device *dev,
854                              struct device_attribute *devattr,
855                              char *buf)
856 {
857         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
858         struct adt7462_data *data = adt7462_update_device(dev);
859
860         if (!temp_enabled(data, attr->index))
861                 return sprintf(buf, "0\n");
862
863         return sprintf(buf, "%d\n", 1000 * (data->temp_max[attr->index] - 64));
864 }
865
866 static ssize_t set_temp_max(struct device *dev,
867                             struct device_attribute *devattr,
868                             const char *buf,
869                             size_t count)
870 {
871         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
872         struct i2c_client *client = to_i2c_client(dev);
873         struct adt7462_data *data = i2c_get_clientdata(client);
874         long temp;
875
876         if (strict_strtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
877                 return -EINVAL;
878
879         temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
880         temp = SENSORS_LIMIT(temp, 0, 255);
881
882         mutex_lock(&data->lock);
883         data->temp_max[attr->index] = temp;
884         i2c_smbus_write_byte_data(client, ADT7462_TEMP_MAX_REG(attr->index),
885                                   temp);
886         mutex_unlock(&data->lock);
887
888         return count;
889 }
890
891 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
892                          char *buf)
893 {
894         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
895         struct adt7462_data *data = adt7462_update_device(dev);
896         u8 frac = data->temp_frac[attr->index] >> TEMP_FRAC_OFFSET;
897
898         if (!temp_enabled(data, attr->index))
899                 return sprintf(buf, "0\n");
900
901         return sprintf(buf, "%d\n", 1000 * (data->temp[attr->index] - 64) +
902                                      250 * frac);
903 }
904
905 static ssize_t show_temp_label(struct device *dev,
906                                struct device_attribute *devattr,
907                                char *buf)
908 {
909         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
910         struct adt7462_data *data = adt7462_update_device(dev);
911
912         return sprintf(buf, "%s\n", temp_label(data, attr->index));
913 }
914
915 static ssize_t show_volt_max(struct device *dev,
916                              struct device_attribute *devattr,
917                              char *buf)
918 {
919         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
920         struct adt7462_data *data = adt7462_update_device(dev);
921         int x = voltage_multiplier(data, attr->index);
922
923         x *= data->volt_max[attr->index];
924         x /= 1000; /* convert from uV to mV */
925
926         return sprintf(buf, "%d\n", x);
927 }
928
929 static ssize_t set_volt_max(struct device *dev,
930                             struct device_attribute *devattr,
931                             const char *buf,
932                             size_t count)
933 {
934         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
935         struct i2c_client *client = to_i2c_client(dev);
936         struct adt7462_data *data = i2c_get_clientdata(client);
937         int x = voltage_multiplier(data, attr->index);
938         long temp;
939
940         if (strict_strtol(buf, 10, &temp) || !x)
941                 return -EINVAL;
942
943         temp *= 1000; /* convert mV to uV */
944         temp = DIV_ROUND_CLOSEST(temp, x);
945         temp = SENSORS_LIMIT(temp, 0, 255);
946
947         mutex_lock(&data->lock);
948         data->volt_max[attr->index] = temp;
949         i2c_smbus_write_byte_data(client,
950                                   ADT7462_REG_VOLT_MAX(data, attr->index),
951                                   temp);
952         mutex_unlock(&data->lock);
953
954         return count;
955 }
956
957 static ssize_t show_volt_min(struct device *dev,
958                              struct device_attribute *devattr,
959                              char *buf)
960 {
961         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
962         struct adt7462_data *data = adt7462_update_device(dev);
963         int x = voltage_multiplier(data, attr->index);
964
965         x *= data->volt_min[attr->index];
966         x /= 1000; /* convert from uV to mV */
967
968         return sprintf(buf, "%d\n", x);
969 }
970
971 static ssize_t set_volt_min(struct device *dev,
972                             struct device_attribute *devattr,
973                             const char *buf,
974                             size_t count)
975 {
976         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
977         struct i2c_client *client = to_i2c_client(dev);
978         struct adt7462_data *data = i2c_get_clientdata(client);
979         int x = voltage_multiplier(data, attr->index);
980         long temp;
981
982         if (strict_strtol(buf, 10, &temp) || !x)
983                 return -EINVAL;
984
985         temp *= 1000; /* convert mV to uV */
986         temp = DIV_ROUND_CLOSEST(temp, x);
987         temp = SENSORS_LIMIT(temp, 0, 255);
988
989         mutex_lock(&data->lock);
990         data->volt_min[attr->index] = temp;
991         i2c_smbus_write_byte_data(client,
992                                   ADT7462_REG_VOLT_MIN(data, attr->index),
993                                   temp);
994         mutex_unlock(&data->lock);
995
996         return count;
997 }
998
999 static ssize_t show_voltage(struct device *dev,
1000                             struct device_attribute *devattr,
1001                             char *buf)
1002 {
1003         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1004         struct adt7462_data *data = adt7462_update_device(dev);
1005         int x = voltage_multiplier(data, attr->index);
1006
1007         x *= data->voltages[attr->index];
1008         x /= 1000; /* convert from uV to mV */
1009
1010         return sprintf(buf, "%d\n", x);
1011 }
1012
1013 static ssize_t show_voltage_label(struct device *dev,
1014                                   struct device_attribute *devattr,
1015                                   char *buf)
1016 {
1017         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1018         struct adt7462_data *data = adt7462_update_device(dev);
1019
1020         return sprintf(buf, "%s\n", voltage_label(data, attr->index));
1021 }
1022
1023 static ssize_t show_alarm(struct device *dev,
1024                           struct device_attribute *devattr,
1025                           char *buf)
1026 {
1027         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1028         struct adt7462_data *data = adt7462_update_device(dev);
1029         int reg = attr->index >> ADT7462_ALARM_REG_SHIFT;
1030         int mask = attr->index & ADT7462_ALARM_FLAG_MASK;
1031
1032         if (data->alarms[reg] & mask)
1033                 return sprintf(buf, "1\n");
1034         else
1035                 return sprintf(buf, "0\n");
1036 }
1037
1038 static int fan_enabled(struct adt7462_data *data, int fan)
1039 {
1040         return data->fan_enabled & (1 << fan);
1041 }
1042
1043 static ssize_t show_fan_min(struct device *dev,
1044                             struct device_attribute *devattr,
1045                             char *buf)
1046 {
1047         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1048         struct adt7462_data *data = adt7462_update_device(dev);
1049         u16 temp;
1050
1051         /* Only the MSB of the min fan period is stored... */
1052         temp = data->fan_min[attr->index];
1053         temp <<= 8;
1054
1055         if (!fan_enabled(data, attr->index) ||
1056             !FAN_DATA_VALID(temp))
1057                 return sprintf(buf, "0\n");
1058
1059         return sprintf(buf, "%d\n", FAN_PERIOD_TO_RPM(temp));
1060 }
1061
1062 static ssize_t set_fan_min(struct device *dev,
1063                            struct device_attribute *devattr,
1064                            const char *buf, size_t count)
1065 {
1066         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1067         struct i2c_client *client = to_i2c_client(dev);
1068         struct adt7462_data *data = i2c_get_clientdata(client);
1069         long temp;
1070
1071         if (strict_strtol(buf, 10, &temp) || !temp ||
1072             !fan_enabled(data, attr->index))
1073                 return -EINVAL;
1074
1075         temp = FAN_RPM_TO_PERIOD(temp);
1076         temp >>= 8;
1077         temp = SENSORS_LIMIT(temp, 1, 255);
1078
1079         mutex_lock(&data->lock);
1080         data->fan_min[attr->index] = temp;
1081         i2c_smbus_write_byte_data(client, ADT7462_REG_FAN_MIN(attr->index),
1082                                   temp);
1083         mutex_unlock(&data->lock);
1084
1085         return count;
1086 }
1087
1088 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
1089                         char *buf)
1090 {
1091         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1092         struct adt7462_data *data = adt7462_update_device(dev);
1093
1094         if (!fan_enabled(data, attr->index) ||
1095             !FAN_DATA_VALID(data->fan[attr->index]))
1096                 return sprintf(buf, "0\n");
1097
1098         return sprintf(buf, "%d\n",
1099                        FAN_PERIOD_TO_RPM(data->fan[attr->index]));
1100 }
1101
1102 static ssize_t show_force_pwm_max(struct device *dev,
1103                                   struct device_attribute *devattr,
1104                                   char *buf)
1105 {
1106         struct adt7462_data *data = adt7462_update_device(dev);
1107         return sprintf(buf, "%d\n", (data->cfg2 & ADT7462_FSPD_MASK ? 1 : 0));
1108 }
1109
1110 static ssize_t set_force_pwm_max(struct device *dev,
1111                                  struct device_attribute *devattr,
1112                                  const char *buf,
1113                                  size_t count)
1114 {
1115         struct i2c_client *client = to_i2c_client(dev);
1116         struct adt7462_data *data = i2c_get_clientdata(client);
1117         long temp;
1118         u8 reg;
1119
1120         if (strict_strtol(buf, 10, &temp))
1121                 return -EINVAL;
1122
1123         mutex_lock(&data->lock);
1124         reg = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
1125         if (temp)
1126                 reg |= ADT7462_FSPD_MASK;
1127         else
1128                 reg &= ~ADT7462_FSPD_MASK;
1129         data->cfg2 = reg;
1130         i2c_smbus_write_byte_data(client, ADT7462_REG_CFG2, reg);
1131         mutex_unlock(&data->lock);
1132
1133         return count;
1134 }
1135
1136 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
1137                         char *buf)
1138 {
1139         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1140         struct adt7462_data *data = adt7462_update_device(dev);
1141         return sprintf(buf, "%d\n", data->pwm[attr->index]);
1142 }
1143
1144 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
1145                         const char *buf, size_t count)
1146 {
1147         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1148         struct i2c_client *client = to_i2c_client(dev);
1149         struct adt7462_data *data = i2c_get_clientdata(client);
1150         long temp;
1151
1152         if (strict_strtol(buf, 10, &temp))
1153                 return -EINVAL;
1154
1155         temp = SENSORS_LIMIT(temp, 0, 255);
1156
1157         mutex_lock(&data->lock);
1158         data->pwm[attr->index] = temp;
1159         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM(attr->index), temp);
1160         mutex_unlock(&data->lock);
1161
1162         return count;
1163 }
1164
1165 static ssize_t show_pwm_max(struct device *dev,
1166                             struct device_attribute *devattr,
1167                             char *buf)
1168 {
1169         struct adt7462_data *data = adt7462_update_device(dev);
1170         return sprintf(buf, "%d\n", data->pwm_max);
1171 }
1172
1173 static ssize_t set_pwm_max(struct device *dev,
1174                            struct device_attribute *devattr,
1175                            const char *buf,
1176                            size_t count)
1177 {
1178         struct i2c_client *client = to_i2c_client(dev);
1179         struct adt7462_data *data = i2c_get_clientdata(client);
1180         long temp;
1181
1182         if (strict_strtol(buf, 10, &temp))
1183                 return -EINVAL;
1184
1185         temp = SENSORS_LIMIT(temp, 0, 255);
1186
1187         mutex_lock(&data->lock);
1188         data->pwm_max = temp;
1189         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MAX, temp);
1190         mutex_unlock(&data->lock);
1191
1192         return count;
1193 }
1194
1195 static ssize_t show_pwm_min(struct device *dev,
1196                             struct device_attribute *devattr,
1197                             char *buf)
1198 {
1199         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1200         struct adt7462_data *data = adt7462_update_device(dev);
1201         return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
1202 }
1203
1204 static ssize_t set_pwm_min(struct device *dev,
1205                            struct device_attribute *devattr,
1206                            const char *buf,
1207                            size_t count)
1208 {
1209         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1210         struct i2c_client *client = to_i2c_client(dev);
1211         struct adt7462_data *data = i2c_get_clientdata(client);
1212         long temp;
1213
1214         if (strict_strtol(buf, 10, &temp))
1215                 return -EINVAL;
1216
1217         temp = SENSORS_LIMIT(temp, 0, 255);
1218
1219         mutex_lock(&data->lock);
1220         data->pwm_min[attr->index] = temp;
1221         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MIN(attr->index),
1222                                   temp);
1223         mutex_unlock(&data->lock);
1224
1225         return count;
1226 }
1227
1228 static ssize_t show_pwm_hyst(struct device *dev,
1229                              struct device_attribute *devattr,
1230                              char *buf)
1231 {
1232         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1233         struct adt7462_data *data = adt7462_update_device(dev);
1234         return sprintf(buf, "%d\n", 1000 *
1235                       (data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK));
1236 }
1237
1238 static ssize_t set_pwm_hyst(struct device *dev,
1239                             struct device_attribute *devattr,
1240                             const char *buf,
1241                             size_t count)
1242 {
1243         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1244         struct i2c_client *client = to_i2c_client(dev);
1245         struct adt7462_data *data = i2c_get_clientdata(client);
1246         long temp;
1247
1248         if (strict_strtol(buf, 10, &temp))
1249                 return -EINVAL;
1250
1251         temp = DIV_ROUND_CLOSEST(temp, 1000);
1252         temp = SENSORS_LIMIT(temp, 0, 15);
1253
1254         /* package things up */
1255         temp &= ADT7462_PWM_HYST_MASK;
1256         temp |= data->pwm_trange[attr->index] & ADT7462_PWM_RANGE_MASK;
1257
1258         mutex_lock(&data->lock);
1259         data->pwm_trange[attr->index] = temp;
1260         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1261                                   temp);
1262         mutex_unlock(&data->lock);
1263
1264         return count;
1265 }
1266
1267 static ssize_t show_pwm_tmax(struct device *dev,
1268                              struct device_attribute *devattr,
1269                              char *buf)
1270 {
1271         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1272         struct adt7462_data *data = adt7462_update_device(dev);
1273
1274         /* tmax = tmin + trange */
1275         int trange = trange_values[data->pwm_trange[attr->index] >>
1276                                    ADT7462_PWM_RANGE_SHIFT];
1277         int tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1278
1279         return sprintf(buf, "%d\n", tmin + trange);
1280 }
1281
1282 static ssize_t set_pwm_tmax(struct device *dev,
1283                             struct device_attribute *devattr,
1284                             const char *buf,
1285                             size_t count)
1286 {
1287         int temp;
1288         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1289         struct i2c_client *client = to_i2c_client(dev);
1290         struct adt7462_data *data = i2c_get_clientdata(client);
1291         int tmin, trange_value;
1292         long trange;
1293
1294         if (strict_strtol(buf, 10, &trange))
1295                 return -EINVAL;
1296
1297         /* trange = tmax - tmin */
1298         tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1299         trange_value = find_trange_value(trange - tmin);
1300
1301         if (trange_value < 0)
1302                 return -EINVAL;
1303
1304         temp = trange_value << ADT7462_PWM_RANGE_SHIFT;
1305         temp |= data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK;
1306
1307         mutex_lock(&data->lock);
1308         data->pwm_trange[attr->index] = temp;
1309         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1310                                   temp);
1311         mutex_unlock(&data->lock);
1312
1313         return count;
1314 }
1315
1316 static ssize_t show_pwm_tmin(struct device *dev,
1317                              struct device_attribute *devattr,
1318                              char *buf)
1319 {
1320         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1321         struct adt7462_data *data = adt7462_update_device(dev);
1322         return sprintf(buf, "%d\n", 1000 * (data->pwm_tmin[attr->index] - 64));
1323 }
1324
1325 static ssize_t set_pwm_tmin(struct device *dev,
1326                             struct device_attribute *devattr,
1327                             const char *buf,
1328                             size_t count)
1329 {
1330         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1331         struct i2c_client *client = to_i2c_client(dev);
1332         struct adt7462_data *data = i2c_get_clientdata(client);
1333         long temp;
1334
1335         if (strict_strtol(buf, 10, &temp))
1336                 return -EINVAL;
1337
1338         temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
1339         temp = SENSORS_LIMIT(temp, 0, 255);
1340
1341         mutex_lock(&data->lock);
1342         data->pwm_tmin[attr->index] = temp;
1343         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TMIN(attr->index),
1344                                   temp);
1345         mutex_unlock(&data->lock);
1346
1347         return count;
1348 }
1349
1350 static ssize_t show_pwm_auto(struct device *dev,
1351                              struct device_attribute *devattr,
1352                              char *buf)
1353 {
1354         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1355         struct adt7462_data *data = adt7462_update_device(dev);
1356         int cfg = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1357
1358         switch (cfg) {
1359         case 4: /* off */
1360                 return sprintf(buf, "0\n");
1361         case 7: /* manual */
1362                 return sprintf(buf, "1\n");
1363         default: /* automatic */
1364                 return sprintf(buf, "2\n");
1365         }
1366 }
1367
1368 static void set_pwm_channel(struct i2c_client *client,
1369                             struct adt7462_data *data,
1370                             int which,
1371                             int value)
1372 {
1373         int temp = data->pwm_cfg[which] & ~ADT7462_PWM_CHANNEL_MASK;
1374         temp |= value << ADT7462_PWM_CHANNEL_SHIFT;
1375
1376         mutex_lock(&data->lock);
1377         data->pwm_cfg[which] = temp;
1378         i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_CFG(which), temp);
1379         mutex_unlock(&data->lock);
1380 }
1381
1382 static ssize_t set_pwm_auto(struct device *dev,
1383                             struct device_attribute *devattr,
1384                             const char *buf,
1385                             size_t count)
1386 {
1387         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1388         struct i2c_client *client = to_i2c_client(dev);
1389         struct adt7462_data *data = i2c_get_clientdata(client);
1390         long temp;
1391
1392         if (strict_strtol(buf, 10, &temp))
1393                 return -EINVAL;
1394
1395         switch (temp) {
1396         case 0: /* off */
1397                 set_pwm_channel(client, data, attr->index, 4);
1398                 return count;
1399         case 1: /* manual */
1400                 set_pwm_channel(client, data, attr->index, 7);
1401                 return count;
1402         default:
1403                 return -EINVAL;
1404         }
1405 }
1406
1407 static ssize_t show_pwm_auto_temp(struct device *dev,
1408                                   struct device_attribute *devattr,
1409                                   char *buf)
1410 {
1411         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1412         struct adt7462_data *data = adt7462_update_device(dev);
1413         int channel = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1414
1415         switch (channel) {
1416         case 0: /* temp[1234] only */
1417         case 1:
1418         case 2:
1419         case 3:
1420                 return sprintf(buf, "%d\n", (1 << channel));
1421         case 5: /* temp1 & temp4  */
1422                 return sprintf(buf, "9\n");
1423         case 6:
1424                 return sprintf(buf, "15\n");
1425         default:
1426                 return sprintf(buf, "0\n");
1427         }
1428 }
1429
1430 static int cvt_auto_temp(int input)
1431 {
1432         if (input == 0xF)
1433                 return 6;
1434         if (input == 0x9)
1435                 return 5;
1436         if (input < 1 || !is_power_of_2(input))
1437                 return -EINVAL;
1438         return ilog2(input);
1439 }
1440
1441 static ssize_t set_pwm_auto_temp(struct device *dev,
1442                                  struct device_attribute *devattr,
1443                                  const char *buf,
1444                                  size_t count)
1445 {
1446         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1447         struct i2c_client *client = to_i2c_client(dev);
1448         struct adt7462_data *data = i2c_get_clientdata(client);
1449         long temp;
1450
1451         if (strict_strtol(buf, 10, &temp))
1452                 return -EINVAL;
1453
1454         temp = cvt_auto_temp(temp);
1455         if (temp < 0)
1456                 return temp;
1457
1458         set_pwm_channel(client, data, attr->index, temp);
1459
1460         return count;
1461 }
1462
1463 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
1464                     set_temp_max, 0);
1465 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
1466                     set_temp_max, 1);
1467 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
1468                     set_temp_max, 2);
1469 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
1470                     set_temp_max, 3);
1471
1472 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
1473                     set_temp_min, 0);
1474 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
1475                     set_temp_min, 1);
1476 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
1477                     set_temp_min, 2);
1478 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
1479                     set_temp_min, 3);
1480
1481 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1482 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1483 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1484 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1485
1486 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0);
1487 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1);
1488 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2);
1489 static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3);
1490
1491 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1492                           ADT7462_ALARM1 | ADT7462_LT_ALARM);
1493 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1494                           ADT7462_ALARM1 | ADT7462_R1T_ALARM);
1495 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1496                           ADT7462_ALARM1 | ADT7462_R2T_ALARM);
1497 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1498                           ADT7462_ALARM1 | ADT7462_R3T_ALARM);
1499
1500 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
1501                     set_volt_max, 0);
1502 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
1503                     set_volt_max, 1);
1504 static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_volt_max,
1505                     set_volt_max, 2);
1506 static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_volt_max,
1507                     set_volt_max, 3);
1508 static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_volt_max,
1509                     set_volt_max, 4);
1510 static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_volt_max,
1511                     set_volt_max, 5);
1512 static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_volt_max,
1513                     set_volt_max, 6);
1514 static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_volt_max,
1515                     set_volt_max, 7);
1516 static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_volt_max,
1517                     set_volt_max, 8);
1518 static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_volt_max,
1519                     set_volt_max, 9);
1520 static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_volt_max,
1521                     set_volt_max, 10);
1522 static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_volt_max,
1523                     set_volt_max, 11);
1524 static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_volt_max,
1525                     set_volt_max, 12);
1526
1527 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
1528                     set_volt_min, 0);
1529 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
1530                     set_volt_min, 1);
1531 static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_volt_min,
1532                     set_volt_min, 2);
1533 static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_volt_min,
1534                     set_volt_min, 3);
1535 static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_volt_min,
1536                     set_volt_min, 4);
1537 static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_volt_min,
1538                     set_volt_min, 5);
1539 static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_volt_min,
1540                     set_volt_min, 6);
1541 static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_volt_min,
1542                     set_volt_min, 7);
1543 static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_volt_min,
1544                     set_volt_min, 8);
1545 static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_volt_min,
1546                     set_volt_min, 9);
1547 static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_volt_min,
1548                     set_volt_min, 10);
1549 static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_volt_min,
1550                     set_volt_min, 11);
1551 static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_volt_min,
1552                     set_volt_min, 12);
1553
1554 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 0);
1555 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 1);
1556 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 2);
1557 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 3);
1558 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 4);
1559 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 5);
1560 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 6);
1561 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_voltage, NULL, 7);
1562 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_voltage, NULL, 8);
1563 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_voltage, NULL, 9);
1564 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_voltage, NULL, 10);
1565 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_voltage, NULL, 11);
1566 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_voltage, NULL, 12);
1567
1568 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_voltage_label, NULL, 0);
1569 static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_voltage_label, NULL, 1);
1570 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_voltage_label, NULL, 2);
1571 static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_voltage_label, NULL, 3);
1572 static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_voltage_label, NULL, 4);
1573 static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_voltage_label, NULL, 5);
1574 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_voltage_label, NULL, 6);
1575 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_voltage_label, NULL, 7);
1576 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_voltage_label, NULL, 8);
1577 static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, show_voltage_label, NULL, 9);
1578 static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, show_voltage_label, NULL, 10);
1579 static SENSOR_DEVICE_ATTR(in12_label, S_IRUGO, show_voltage_label, NULL, 11);
1580 static SENSOR_DEVICE_ATTR(in13_label, S_IRUGO, show_voltage_label, NULL, 12);
1581
1582 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
1583                           ADT7462_ALARM2 | ADT7462_V0_ALARM);
1584 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
1585                           ADT7462_ALARM2 | ADT7462_V7_ALARM);
1586 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL,
1587                           ADT7462_ALARM2 | ADT7462_V2_ALARM);
1588 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL,
1589                           ADT7462_ALARM2 | ADT7462_V6_ALARM);
1590 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL,
1591                           ADT7462_ALARM2 | ADT7462_V5_ALARM);
1592 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL,
1593                           ADT7462_ALARM2 | ADT7462_V4_ALARM);
1594 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL,
1595                           ADT7462_ALARM2 | ADT7462_V3_ALARM);
1596 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL,
1597                           ADT7462_ALARM2 | ADT7462_V1_ALARM);
1598 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL,
1599                           ADT7462_ALARM3 | ADT7462_V10_ALARM);
1600 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL,
1601                           ADT7462_ALARM3 | ADT7462_V9_ALARM);
1602 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL,
1603                           ADT7462_ALARM3 | ADT7462_V8_ALARM);
1604 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL,
1605                           ADT7462_ALARM3 | ADT7462_V11_ALARM);
1606 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL,
1607                           ADT7462_ALARM3 | ADT7462_V12_ALARM);
1608
1609 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1610                     set_fan_min, 0);
1611 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1612                     set_fan_min, 1);
1613 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1614                     set_fan_min, 2);
1615 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1616                     set_fan_min, 3);
1617 static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1618                     set_fan_min, 4);
1619 static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO, show_fan_min,
1620                     set_fan_min, 5);
1621 static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO, show_fan_min,
1622                     set_fan_min, 6);
1623 static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO, show_fan_min,
1624                     set_fan_min, 7);
1625
1626 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1627 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1628 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1629 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1630 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4);
1631 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan, NULL, 5);
1632 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan, NULL, 6);
1633 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan, NULL, 7);
1634
1635 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1636                           ADT7462_ALARM4 | ADT7462_F0_ALARM);
1637 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1638                           ADT7462_ALARM4 | ADT7462_F1_ALARM);
1639 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1640                           ADT7462_ALARM4 | ADT7462_F2_ALARM);
1641 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1642                           ADT7462_ALARM4 | ADT7462_F3_ALARM);
1643 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL,
1644                           ADT7462_ALARM4 | ADT7462_F4_ALARM);
1645 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL,
1646                           ADT7462_ALARM4 | ADT7462_F5_ALARM);
1647 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL,
1648                           ADT7462_ALARM4 | ADT7462_F6_ALARM);
1649 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL,
1650                           ADT7462_ALARM4 | ADT7462_F7_ALARM);
1651
1652 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1653                     show_force_pwm_max, set_force_pwm_max, 0);
1654
1655 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1656 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1657 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1658 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1659
1660 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1661                     show_pwm_min, set_pwm_min, 0);
1662 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1663                     show_pwm_min, set_pwm_min, 1);
1664 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1665                     show_pwm_min, set_pwm_min, 2);
1666 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1667                     show_pwm_min, set_pwm_min, 3);
1668
1669 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1670                     show_pwm_max, set_pwm_max, 0);
1671 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1672                     show_pwm_max, set_pwm_max, 1);
1673 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1674                     show_pwm_max, set_pwm_max, 2);
1675 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1676                     show_pwm_max, set_pwm_max, 3);
1677
1678 static SENSOR_DEVICE_ATTR(temp1_auto_point1_hyst, S_IWUSR | S_IRUGO,
1679                     show_pwm_hyst, set_pwm_hyst, 0);
1680 static SENSOR_DEVICE_ATTR(temp2_auto_point1_hyst, S_IWUSR | S_IRUGO,
1681                     show_pwm_hyst, set_pwm_hyst, 1);
1682 static SENSOR_DEVICE_ATTR(temp3_auto_point1_hyst, S_IWUSR | S_IRUGO,
1683                     show_pwm_hyst, set_pwm_hyst, 2);
1684 static SENSOR_DEVICE_ATTR(temp4_auto_point1_hyst, S_IWUSR | S_IRUGO,
1685                     show_pwm_hyst, set_pwm_hyst, 3);
1686
1687 static SENSOR_DEVICE_ATTR(temp1_auto_point2_hyst, S_IWUSR | S_IRUGO,
1688                     show_pwm_hyst, set_pwm_hyst, 0);
1689 static SENSOR_DEVICE_ATTR(temp2_auto_point2_hyst, S_IWUSR | S_IRUGO,
1690                     show_pwm_hyst, set_pwm_hyst, 1);
1691 static SENSOR_DEVICE_ATTR(temp3_auto_point2_hyst, S_IWUSR | S_IRUGO,
1692                     show_pwm_hyst, set_pwm_hyst, 2);
1693 static SENSOR_DEVICE_ATTR(temp4_auto_point2_hyst, S_IWUSR | S_IRUGO,
1694                     show_pwm_hyst, set_pwm_hyst, 3);
1695
1696 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
1697                     show_pwm_tmin, set_pwm_tmin, 0);
1698 static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
1699                     show_pwm_tmin, set_pwm_tmin, 1);
1700 static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
1701                     show_pwm_tmin, set_pwm_tmin, 2);
1702 static SENSOR_DEVICE_ATTR(temp4_auto_point1_temp, S_IWUSR | S_IRUGO,
1703                     show_pwm_tmin, set_pwm_tmin, 3);
1704
1705 static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
1706                     show_pwm_tmax, set_pwm_tmax, 0);
1707 static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
1708                     show_pwm_tmax, set_pwm_tmax, 1);
1709 static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
1710                     show_pwm_tmax, set_pwm_tmax, 2);
1711 static SENSOR_DEVICE_ATTR(temp4_auto_point2_temp, S_IWUSR | S_IRUGO,
1712                     show_pwm_tmax, set_pwm_tmax, 3);
1713
1714 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1715                     set_pwm_auto, 0);
1716 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1717                     set_pwm_auto, 1);
1718 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1719                     set_pwm_auto, 2);
1720 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1721                     set_pwm_auto, 3);
1722
1723 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1724                     show_pwm_auto_temp, set_pwm_auto_temp, 0);
1725 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1726                     show_pwm_auto_temp, set_pwm_auto_temp, 1);
1727 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1728                     show_pwm_auto_temp, set_pwm_auto_temp, 2);
1729 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1730                     show_pwm_auto_temp, set_pwm_auto_temp, 3);
1731
1732 static struct attribute *adt7462_attr[] =
1733 {
1734         &sensor_dev_attr_temp1_max.dev_attr.attr,
1735         &sensor_dev_attr_temp2_max.dev_attr.attr,
1736         &sensor_dev_attr_temp3_max.dev_attr.attr,
1737         &sensor_dev_attr_temp4_max.dev_attr.attr,
1738
1739         &sensor_dev_attr_temp1_min.dev_attr.attr,
1740         &sensor_dev_attr_temp2_min.dev_attr.attr,
1741         &sensor_dev_attr_temp3_min.dev_attr.attr,
1742         &sensor_dev_attr_temp4_min.dev_attr.attr,
1743
1744         &sensor_dev_attr_temp1_input.dev_attr.attr,
1745         &sensor_dev_attr_temp2_input.dev_attr.attr,
1746         &sensor_dev_attr_temp3_input.dev_attr.attr,
1747         &sensor_dev_attr_temp4_input.dev_attr.attr,
1748
1749         &sensor_dev_attr_temp1_label.dev_attr.attr,
1750         &sensor_dev_attr_temp2_label.dev_attr.attr,
1751         &sensor_dev_attr_temp3_label.dev_attr.attr,
1752         &sensor_dev_attr_temp4_label.dev_attr.attr,
1753
1754         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1755         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1756         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1757         &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1758
1759         &sensor_dev_attr_in1_max.dev_attr.attr,
1760         &sensor_dev_attr_in2_max.dev_attr.attr,
1761         &sensor_dev_attr_in3_max.dev_attr.attr,
1762         &sensor_dev_attr_in4_max.dev_attr.attr,
1763         &sensor_dev_attr_in5_max.dev_attr.attr,
1764         &sensor_dev_attr_in6_max.dev_attr.attr,
1765         &sensor_dev_attr_in7_max.dev_attr.attr,
1766         &sensor_dev_attr_in8_max.dev_attr.attr,
1767         &sensor_dev_attr_in9_max.dev_attr.attr,
1768         &sensor_dev_attr_in10_max.dev_attr.attr,
1769         &sensor_dev_attr_in11_max.dev_attr.attr,
1770         &sensor_dev_attr_in12_max.dev_attr.attr,
1771         &sensor_dev_attr_in13_max.dev_attr.attr,
1772
1773         &sensor_dev_attr_in1_min.dev_attr.attr,
1774         &sensor_dev_attr_in2_min.dev_attr.attr,
1775         &sensor_dev_attr_in3_min.dev_attr.attr,
1776         &sensor_dev_attr_in4_min.dev_attr.attr,
1777         &sensor_dev_attr_in5_min.dev_attr.attr,
1778         &sensor_dev_attr_in6_min.dev_attr.attr,
1779         &sensor_dev_attr_in7_min.dev_attr.attr,
1780         &sensor_dev_attr_in8_min.dev_attr.attr,
1781         &sensor_dev_attr_in9_min.dev_attr.attr,
1782         &sensor_dev_attr_in10_min.dev_attr.attr,
1783         &sensor_dev_attr_in11_min.dev_attr.attr,
1784         &sensor_dev_attr_in12_min.dev_attr.attr,
1785         &sensor_dev_attr_in13_min.dev_attr.attr,
1786
1787         &sensor_dev_attr_in1_input.dev_attr.attr,
1788         &sensor_dev_attr_in2_input.dev_attr.attr,
1789         &sensor_dev_attr_in3_input.dev_attr.attr,
1790         &sensor_dev_attr_in4_input.dev_attr.attr,
1791         &sensor_dev_attr_in5_input.dev_attr.attr,
1792         &sensor_dev_attr_in6_input.dev_attr.attr,
1793         &sensor_dev_attr_in7_input.dev_attr.attr,
1794         &sensor_dev_attr_in8_input.dev_attr.attr,
1795         &sensor_dev_attr_in9_input.dev_attr.attr,
1796         &sensor_dev_attr_in10_input.dev_attr.attr,
1797         &sensor_dev_attr_in11_input.dev_attr.attr,
1798         &sensor_dev_attr_in12_input.dev_attr.attr,
1799         &sensor_dev_attr_in13_input.dev_attr.attr,
1800
1801         &sensor_dev_attr_in1_label.dev_attr.attr,
1802         &sensor_dev_attr_in2_label.dev_attr.attr,
1803         &sensor_dev_attr_in3_label.dev_attr.attr,
1804         &sensor_dev_attr_in4_label.dev_attr.attr,
1805         &sensor_dev_attr_in5_label.dev_attr.attr,
1806         &sensor_dev_attr_in6_label.dev_attr.attr,
1807         &sensor_dev_attr_in7_label.dev_attr.attr,
1808         &sensor_dev_attr_in8_label.dev_attr.attr,
1809         &sensor_dev_attr_in9_label.dev_attr.attr,
1810         &sensor_dev_attr_in10_label.dev_attr.attr,
1811         &sensor_dev_attr_in11_label.dev_attr.attr,
1812         &sensor_dev_attr_in12_label.dev_attr.attr,
1813         &sensor_dev_attr_in13_label.dev_attr.attr,
1814
1815         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1816         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1817         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1818         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1819         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1820         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1821         &sensor_dev_attr_in7_alarm.dev_attr.attr,
1822         &sensor_dev_attr_in8_alarm.dev_attr.attr,
1823         &sensor_dev_attr_in9_alarm.dev_attr.attr,
1824         &sensor_dev_attr_in10_alarm.dev_attr.attr,
1825         &sensor_dev_attr_in11_alarm.dev_attr.attr,
1826         &sensor_dev_attr_in12_alarm.dev_attr.attr,
1827         &sensor_dev_attr_in13_alarm.dev_attr.attr,
1828
1829         &sensor_dev_attr_fan1_min.dev_attr.attr,
1830         &sensor_dev_attr_fan2_min.dev_attr.attr,
1831         &sensor_dev_attr_fan3_min.dev_attr.attr,
1832         &sensor_dev_attr_fan4_min.dev_attr.attr,
1833         &sensor_dev_attr_fan5_min.dev_attr.attr,
1834         &sensor_dev_attr_fan6_min.dev_attr.attr,
1835         &sensor_dev_attr_fan7_min.dev_attr.attr,
1836         &sensor_dev_attr_fan8_min.dev_attr.attr,
1837
1838         &sensor_dev_attr_fan1_input.dev_attr.attr,
1839         &sensor_dev_attr_fan2_input.dev_attr.attr,
1840         &sensor_dev_attr_fan3_input.dev_attr.attr,
1841         &sensor_dev_attr_fan4_input.dev_attr.attr,
1842         &sensor_dev_attr_fan5_input.dev_attr.attr,
1843         &sensor_dev_attr_fan6_input.dev_attr.attr,
1844         &sensor_dev_attr_fan7_input.dev_attr.attr,
1845         &sensor_dev_attr_fan8_input.dev_attr.attr,
1846
1847         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1848         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1849         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1850         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1851         &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1852         &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1853         &sensor_dev_attr_fan7_alarm.dev_attr.attr,
1854         &sensor_dev_attr_fan8_alarm.dev_attr.attr,
1855
1856         &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1857         &sensor_dev_attr_pwm1.dev_attr.attr,
1858         &sensor_dev_attr_pwm2.dev_attr.attr,
1859         &sensor_dev_attr_pwm3.dev_attr.attr,
1860         &sensor_dev_attr_pwm4.dev_attr.attr,
1861
1862         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1863         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1864         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1865         &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1866
1867         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1868         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1869         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1870         &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1871
1872         &sensor_dev_attr_temp1_auto_point1_hyst.dev_attr.attr,
1873         &sensor_dev_attr_temp2_auto_point1_hyst.dev_attr.attr,
1874         &sensor_dev_attr_temp3_auto_point1_hyst.dev_attr.attr,
1875         &sensor_dev_attr_temp4_auto_point1_hyst.dev_attr.attr,
1876
1877         &sensor_dev_attr_temp1_auto_point2_hyst.dev_attr.attr,
1878         &sensor_dev_attr_temp2_auto_point2_hyst.dev_attr.attr,
1879         &sensor_dev_attr_temp3_auto_point2_hyst.dev_attr.attr,
1880         &sensor_dev_attr_temp4_auto_point2_hyst.dev_attr.attr,
1881
1882         &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1883         &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1884         &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1885         &sensor_dev_attr_temp4_auto_point1_temp.dev_attr.attr,
1886
1887         &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1888         &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1889         &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1890         &sensor_dev_attr_temp4_auto_point2_temp.dev_attr.attr,
1891
1892         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1893         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1894         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1895         &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1896
1897         &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1898         &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1899         &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1900         &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1901         NULL
1902 };
1903
1904 /* Return 0 if detection is successful, -ENODEV otherwise */
1905 static int adt7462_detect(struct i2c_client *client, int kind,
1906                           struct i2c_board_info *info)
1907 {
1908         struct i2c_adapter *adapter = client->adapter;
1909
1910         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1911                 return -ENODEV;
1912
1913         if (kind <= 0) {
1914                 int vendor, device, revision;
1915
1916                 vendor = i2c_smbus_read_byte_data(client, ADT7462_REG_VENDOR);
1917                 if (vendor != ADT7462_VENDOR)
1918                         return -ENODEV;
1919
1920                 device = i2c_smbus_read_byte_data(client, ADT7462_REG_DEVICE);
1921                 if (device != ADT7462_DEVICE)
1922                         return -ENODEV;
1923
1924                 revision = i2c_smbus_read_byte_data(client,
1925                                                     ADT7462_REG_REVISION);
1926                 if (revision != ADT7462_REVISION)
1927                         return -ENODEV;
1928         } else
1929                 dev_dbg(&adapter->dev, "detection forced\n");
1930
1931         strlcpy(info->type, "adt7462", I2C_NAME_SIZE);
1932
1933         return 0;
1934 }
1935
1936 static int adt7462_probe(struct i2c_client *client,
1937                          const struct i2c_device_id *id)
1938 {
1939         struct adt7462_data *data;
1940         int err;
1941
1942         data = kzalloc(sizeof(struct adt7462_data), GFP_KERNEL);
1943         if (!data) {
1944                 err = -ENOMEM;
1945                 goto exit;
1946         }
1947
1948         i2c_set_clientdata(client, data);
1949         mutex_init(&data->lock);
1950
1951         dev_info(&client->dev, "%s chip found\n", client->name);
1952
1953         /* Register sysfs hooks */
1954         data->attrs.attrs = adt7462_attr;
1955         err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1956         if (err)
1957                 goto exit_free;
1958
1959         data->hwmon_dev = hwmon_device_register(&client->dev);
1960         if (IS_ERR(data->hwmon_dev)) {
1961                 err = PTR_ERR(data->hwmon_dev);
1962                 goto exit_remove;
1963         }
1964
1965         return 0;
1966
1967 exit_remove:
1968         sysfs_remove_group(&client->dev.kobj, &data->attrs);
1969 exit_free:
1970         kfree(data);
1971 exit:
1972         return err;
1973 }
1974
1975 static int adt7462_remove(struct i2c_client *client)
1976 {
1977         struct adt7462_data *data = i2c_get_clientdata(client);
1978
1979         hwmon_device_unregister(data->hwmon_dev);
1980         sysfs_remove_group(&client->dev.kobj, &data->attrs);
1981         kfree(data);
1982         return 0;
1983 }
1984
1985 static int __init adt7462_init(void)
1986 {
1987         return i2c_add_driver(&adt7462_driver);
1988 }
1989
1990 static void __exit adt7462_exit(void)
1991 {
1992         i2c_del_driver(&adt7462_driver);
1993 }
1994
1995 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1996 MODULE_DESCRIPTION("ADT7462 driver");
1997 MODULE_LICENSE("GPL");
1998
1999 module_init(adt7462_init);
2000 module_exit(adt7462_exit);