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