hwmon: (adt7475) Move sysfs files removal to a separate function
[linux-2.6.git] / drivers / hwmon / adt7475.c
1 /*
2  * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3  * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5  * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6  * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
7  *
8  * Derived from the lm83 driver by Jean Delvare
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/err.h>
22
23 /* Indexes for the sysfs hooks */
24
25 #define INPUT           0
26 #define MIN             1
27 #define MAX             2
28 #define CONTROL         3
29 #define OFFSET          3
30 #define AUTOMIN         4
31 #define THERM           5
32 #define HYSTERSIS       6
33
34 /* These are unique identifiers for the sysfs functions - unlike the
35    numbers above, these are not also indexes into an array
36 */
37
38 #define ALARM           9
39 #define FAULT           10
40
41 /* 7475 Common Registers */
42
43 #define REG_VTT                 0x1E    /* ADT7490 only */
44 #define REG_EXTEND3             0x1F    /* ADT7490 only */
45
46 #define REG_VOLTAGE_BASE        0x20
47 #define REG_TEMP_BASE           0x25
48 #define REG_TACH_BASE           0x28
49 #define REG_PWM_BASE            0x30
50 #define REG_PWM_MAX_BASE        0x38
51
52 #define REG_DEVID               0x3D
53 #define REG_VENDID              0x3E
54 #define REG_DEVID2              0x3F
55
56 #define REG_STATUS1             0x41
57 #define REG_STATUS2             0x42
58
59 #define REG_VOLTAGE_MIN_BASE    0x44
60 #define REG_VOLTAGE_MAX_BASE    0x45
61
62 #define REG_TEMP_MIN_BASE       0x4E
63 #define REG_TEMP_MAX_BASE       0x4F
64
65 #define REG_TACH_MIN_BASE       0x54
66
67 #define REG_PWM_CONFIG_BASE     0x5C
68
69 #define REG_TEMP_TRANGE_BASE    0x5F
70
71 #define REG_PWM_MIN_BASE        0x64
72
73 #define REG_TEMP_TMIN_BASE      0x67
74 #define REG_TEMP_THERM_BASE     0x6A
75
76 #define REG_REMOTE1_HYSTERSIS   0x6D
77 #define REG_REMOTE2_HYSTERSIS   0x6E
78
79 #define REG_TEMP_OFFSET_BASE    0x70
80
81 #define REG_EXTEND1             0x76
82 #define REG_EXTEND2             0x77
83 #define REG_CONFIG5             0x7C
84 #define REG_CONFIG4             0x7D
85
86 #define REG_STATUS4             0x81    /* ADT7490 only */
87
88 #define REG_VTT_MIN             0x84    /* ADT7490 only */
89 #define REG_VTT_MAX             0x86    /* ADT7490 only */
90
91 #define CONFIG4_MAXDUTY         0x08
92
93 #define CONFIG5_TWOSCOMP        0x01
94 #define CONFIG5_TEMPOFFSET      0x02
95
96 /* ADT7475 Settings */
97
98 #define ADT7475_VOLTAGE_COUNT   5       /* Not counting Vtt */
99 #define ADT7475_TEMP_COUNT      3
100 #define ADT7475_TACH_COUNT      4
101 #define ADT7475_PWM_COUNT       3
102
103 /* Macro to read the registers */
104
105 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
106
107 /* Macros to easily index the registers */
108
109 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
110 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
111
112 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
113 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
114 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
115 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
116
117 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
118 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
119 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
120
121 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
122 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
123 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
124 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
125 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
126 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
127 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
128
129 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
130
131 I2C_CLIENT_INSMOD_3(adt7473, adt7475, adt7490);
132
133 static const struct i2c_device_id adt7475_id[] = {
134         { "adt7473", adt7473 },
135         { "adt7475", adt7475 },
136         { "adt7490", adt7490 },
137         { }
138 };
139 MODULE_DEVICE_TABLE(i2c, adt7475_id);
140
141 struct adt7475_data {
142         struct device *hwmon_dev;
143         struct mutex lock;
144
145         unsigned long measure_updated;
146         unsigned long limits_updated;
147         char valid;
148
149         u8 config4;
150         u8 config5;
151         u8 has_voltage;
152         u32 alarms;
153         u16 voltage[3][6];
154         u16 temp[7][3];
155         u16 tach[2][4];
156         u8 pwm[4][3];
157         u8 range[3];
158         u8 pwmctl[3];
159         u8 pwmchan[3];
160 };
161
162 static struct i2c_driver adt7475_driver;
163 static struct adt7475_data *adt7475_update_device(struct device *dev);
164 static void adt7475_read_hystersis(struct i2c_client *client);
165 static void adt7475_read_pwm(struct i2c_client *client, int index);
166
167 /* Given a temp value, convert it to register value */
168
169 static inline u16 temp2reg(struct adt7475_data *data, long val)
170 {
171         u16 ret;
172
173         if (!(data->config5 & CONFIG5_TWOSCOMP)) {
174                 val = SENSORS_LIMIT(val, -64000, 191000);
175                 ret = (val + 64500) / 1000;
176         } else {
177                 val = SENSORS_LIMIT(val, -128000, 127000);
178                 if (val < -500)
179                         ret = (256500 + val) / 1000;
180                 else
181                         ret = (val + 500) / 1000;
182         }
183
184         return ret << 2;
185 }
186
187 /* Given a register value, convert it to a real temp value */
188
189 static inline int reg2temp(struct adt7475_data *data, u16 reg)
190 {
191         if (data->config5 & CONFIG5_TWOSCOMP) {
192                 if (reg >= 512)
193                         return (reg - 1024) * 250;
194                 else
195                         return reg * 250;
196         } else
197                 return (reg - 256) * 250;
198 }
199
200 static inline int tach2rpm(u16 tach)
201 {
202         if (tach == 0 || tach == 0xFFFF)
203                 return 0;
204
205         return (90000 * 60) / tach;
206 }
207
208 static inline u16 rpm2tach(unsigned long rpm)
209 {
210         if (rpm == 0)
211                 return 0;
212
213         return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
214 }
215
216 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
217 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
218         { 45, 94 },     /* +2.5V */
219         { 175, 525 },   /* Vccp */
220         { 68, 71 },     /* Vcc */
221         { 93, 47 },     /* +5V */
222         { 120, 20 },    /* +12V */
223         { 45, 45 },     /* Vtt */
224 };
225
226 static inline int reg2volt(int channel, u16 reg)
227 {
228         const int *r = adt7473_in_scaling[channel];
229
230         return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
231 }
232
233 static inline u16 volt2reg(int channel, long volt)
234 {
235         const int *r = adt7473_in_scaling[channel];
236         long reg;
237
238         reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
239         return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
240 }
241
242 static u16 adt7475_read_word(struct i2c_client *client, int reg)
243 {
244         u16 val;
245
246         val = i2c_smbus_read_byte_data(client, reg);
247         val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
248
249         return val;
250 }
251
252 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
253 {
254         i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
255         i2c_smbus_write_byte_data(client, reg, val & 0xFF);
256 }
257
258 /* Find the nearest value in a table - used for pwm frequency and
259    auto temp range */
260 static int find_nearest(long val, const int *array, int size)
261 {
262         int i;
263
264         if (val < array[0])
265                 return 0;
266
267         if (val > array[size - 1])
268                 return size - 1;
269
270         for (i = 0; i < size - 1; i++) {
271                 int a, b;
272
273                 if (val > array[i + 1])
274                         continue;
275
276                 a = val - array[i];
277                 b = array[i + 1] - val;
278
279                 return (a <= b) ? i : i + 1;
280         }
281
282         return 0;
283 }
284
285 static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
286                             char *buf)
287 {
288         struct adt7475_data *data = adt7475_update_device(dev);
289         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
290         unsigned short val;
291
292         switch (sattr->nr) {
293         case ALARM:
294                 return sprintf(buf, "%d\n",
295                                (data->alarms >> sattr->index) & 1);
296         default:
297                 val = data->voltage[sattr->nr][sattr->index];
298                 return sprintf(buf, "%d\n", reg2volt(sattr->index, val));
299         }
300 }
301
302 static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
303                            const char *buf, size_t count)
304 {
305
306         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
307         struct i2c_client *client = to_i2c_client(dev);
308         struct adt7475_data *data = i2c_get_clientdata(client);
309         unsigned char reg;
310         long val;
311
312         if (strict_strtol(buf, 10, &val))
313                 return -EINVAL;
314
315         mutex_lock(&data->lock);
316
317         data->voltage[sattr->nr][sattr->index] = volt2reg(sattr->index, val);
318
319         if (sattr->index < ADT7475_VOLTAGE_COUNT) {
320                 if (sattr->nr == MIN)
321                         reg = VOLTAGE_MIN_REG(sattr->index);
322                 else
323                         reg = VOLTAGE_MAX_REG(sattr->index);
324         } else {
325                 if (sattr->nr == MIN)
326                         reg = REG_VTT_MIN;
327                 else
328                         reg = REG_VTT_MAX;
329         }
330
331         i2c_smbus_write_byte_data(client, reg,
332                                   data->voltage[sattr->nr][sattr->index] >> 2);
333         mutex_unlock(&data->lock);
334
335         return count;
336 }
337
338 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
339                          char *buf)
340 {
341         struct adt7475_data *data = adt7475_update_device(dev);
342         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
343         int out;
344
345         switch (sattr->nr) {
346         case HYSTERSIS:
347                 mutex_lock(&data->lock);
348                 out = data->temp[sattr->nr][sattr->index];
349                 if (sattr->index != 1)
350                         out = (out >> 4) & 0xF;
351                 else
352                         out = (out & 0xF);
353                 /* Show the value as an absolute number tied to
354                  * THERM */
355                 out = reg2temp(data, data->temp[THERM][sattr->index]) -
356                         out * 1000;
357                 mutex_unlock(&data->lock);
358                 break;
359
360         case OFFSET:
361                 /* Offset is always 2's complement, regardless of the
362                  * setting in CONFIG5 */
363                 mutex_lock(&data->lock);
364                 out = (s8)data->temp[sattr->nr][sattr->index];
365                 if (data->config5 & CONFIG5_TEMPOFFSET)
366                         out *= 1000;
367                 else
368                         out *= 500;
369                 mutex_unlock(&data->lock);
370                 break;
371
372         case ALARM:
373                 out = (data->alarms >> (sattr->index + 4)) & 1;
374                 break;
375
376         case FAULT:
377                 /* Note - only for remote1 and remote2 */
378                 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
379                 break;
380
381         default:
382                 /* All other temp values are in the configured format */
383                 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
384         }
385
386         return sprintf(buf, "%d\n", out);
387 }
388
389 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
390                         const char *buf, size_t count)
391 {
392         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
393         struct i2c_client *client = to_i2c_client(dev);
394         struct adt7475_data *data = i2c_get_clientdata(client);
395         unsigned char reg = 0;
396         u8 out;
397         int temp;
398         long val;
399
400         if (strict_strtol(buf, 10, &val))
401                 return -EINVAL;
402
403         mutex_lock(&data->lock);
404
405         /* We need the config register in all cases for temp <-> reg conv. */
406         data->config5 = adt7475_read(REG_CONFIG5);
407
408         switch (sattr->nr) {
409         case OFFSET:
410                 if (data->config5 & CONFIG5_TEMPOFFSET) {
411                         val = SENSORS_LIMIT(val, -63000, 127000);
412                         out = data->temp[OFFSET][sattr->index] = val / 1000;
413                 } else {
414                         val = SENSORS_LIMIT(val, -63000, 64000);
415                         out = data->temp[OFFSET][sattr->index] = val / 500;
416                 }
417                 break;
418
419         case HYSTERSIS:
420                 /* The value will be given as an absolute value, turn it
421                    into an offset based on THERM */
422
423                 /* Read fresh THERM and HYSTERSIS values from the chip */
424                 data->temp[THERM][sattr->index] =
425                         adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
426                 adt7475_read_hystersis(client);
427
428                 temp = reg2temp(data, data->temp[THERM][sattr->index]);
429                 val = SENSORS_LIMIT(val, temp - 15000, temp);
430                 val = (temp - val) / 1000;
431
432                 if (sattr->index != 1) {
433                         data->temp[HYSTERSIS][sattr->index] &= 0xF0;
434                         data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
435                 } else {
436                         data->temp[HYSTERSIS][sattr->index] &= 0x0F;
437                         data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
438                 }
439
440                 out = data->temp[HYSTERSIS][sattr->index];
441                 break;
442
443         default:
444                 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
445
446                 /* We maintain an extra 2 digits of precision for simplicity
447                  * - shift those back off before writing the value */
448                 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
449         }
450
451         switch (sattr->nr) {
452         case MIN:
453                 reg = TEMP_MIN_REG(sattr->index);
454                 break;
455         case MAX:
456                 reg = TEMP_MAX_REG(sattr->index);
457                 break;
458         case OFFSET:
459                 reg = TEMP_OFFSET_REG(sattr->index);
460                 break;
461         case AUTOMIN:
462                 reg = TEMP_TMIN_REG(sattr->index);
463                 break;
464         case THERM:
465                 reg = TEMP_THERM_REG(sattr->index);
466                 break;
467         case HYSTERSIS:
468                 if (sattr->index != 2)
469                         reg = REG_REMOTE1_HYSTERSIS;
470                 else
471                         reg = REG_REMOTE2_HYSTERSIS;
472
473                 break;
474         }
475
476         i2c_smbus_write_byte_data(client, reg, out);
477
478         mutex_unlock(&data->lock);
479         return count;
480 }
481
482 /* Table of autorange values - the user will write the value in millidegrees,
483    and we'll convert it */
484 static const int autorange_table[] = {
485         2000, 2500, 3330, 4000, 5000, 6670, 8000,
486         10000, 13330, 16000, 20000, 26670, 32000, 40000,
487         53330, 80000
488 };
489
490 static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
491                            char *buf)
492 {
493         struct adt7475_data *data = adt7475_update_device(dev);
494         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
495         int out, val;
496
497         mutex_lock(&data->lock);
498         out = (data->range[sattr->index] >> 4) & 0x0F;
499         val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
500         mutex_unlock(&data->lock);
501
502         return sprintf(buf, "%d\n", val + autorange_table[out]);
503 }
504
505 static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
506                           const char *buf, size_t count)
507 {
508         struct i2c_client *client = to_i2c_client(dev);
509         struct adt7475_data *data = i2c_get_clientdata(client);
510         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
511         int temp;
512         long val;
513
514         if (strict_strtol(buf, 10, &val))
515                 return -EINVAL;
516
517         mutex_lock(&data->lock);
518
519         /* Get a fresh copy of the needed registers */
520         data->config5 = adt7475_read(REG_CONFIG5);
521         data->temp[AUTOMIN][sattr->index] =
522                 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
523         data->range[sattr->index] =
524                 adt7475_read(TEMP_TRANGE_REG(sattr->index));
525
526         /* The user will write an absolute value, so subtract the start point
527            to figure the range */
528         temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
529         val = SENSORS_LIMIT(val, temp + autorange_table[0],
530                 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
531         val -= temp;
532
533         /* Find the nearest table entry to what the user wrote */
534         val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
535
536         data->range[sattr->index] &= ~0xF0;
537         data->range[sattr->index] |= val << 4;
538
539         i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
540                                   data->range[sattr->index]);
541
542         mutex_unlock(&data->lock);
543         return count;
544 }
545
546 static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
547                          char *buf)
548 {
549         struct adt7475_data *data = adt7475_update_device(dev);
550         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
551         int out;
552
553         if (sattr->nr == ALARM)
554                 out = (data->alarms >> (sattr->index + 10)) & 1;
555         else
556                 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
557
558         return sprintf(buf, "%d\n", out);
559 }
560
561 static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
562                         const char *buf, size_t count)
563 {
564
565         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
566         struct i2c_client *client = to_i2c_client(dev);
567         struct adt7475_data *data = i2c_get_clientdata(client);
568         unsigned long val;
569
570         if (strict_strtoul(buf, 10, &val))
571                 return -EINVAL;
572
573         mutex_lock(&data->lock);
574
575         data->tach[MIN][sattr->index] = rpm2tach(val);
576
577         adt7475_write_word(client, TACH_MIN_REG(sattr->index),
578                            data->tach[MIN][sattr->index]);
579
580         mutex_unlock(&data->lock);
581         return count;
582 }
583
584 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
585                         char *buf)
586 {
587         struct adt7475_data *data = adt7475_update_device(dev);
588         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
589
590         return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
591 }
592
593 static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
594                             char *buf)
595 {
596         struct adt7475_data *data = adt7475_update_device(dev);
597         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
598
599         return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
600 }
601
602 static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
603                             char *buf)
604 {
605         struct adt7475_data *data = adt7475_update_device(dev);
606         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
607
608         return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
609 }
610
611 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
612                        const char *buf, size_t count)
613 {
614
615         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
616         struct i2c_client *client = to_i2c_client(dev);
617         struct adt7475_data *data = i2c_get_clientdata(client);
618         unsigned char reg = 0;
619         long val;
620
621         if (strict_strtol(buf, 10, &val))
622                 return -EINVAL;
623
624         mutex_lock(&data->lock);
625
626         switch (sattr->nr) {
627         case INPUT:
628                 /* Get a fresh value for CONTROL */
629                 data->pwm[CONTROL][sattr->index] =
630                         adt7475_read(PWM_CONFIG_REG(sattr->index));
631
632                 /* If we are not in manual mode, then we shouldn't allow
633                  * the user to set the pwm speed */
634                 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
635                         mutex_unlock(&data->lock);
636                         return count;
637                 }
638
639                 reg = PWM_REG(sattr->index);
640                 break;
641
642         case MIN:
643                 reg = PWM_MIN_REG(sattr->index);
644                 break;
645
646         case MAX:
647                 reg = PWM_MAX_REG(sattr->index);
648                 break;
649         }
650
651         data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
652         i2c_smbus_write_byte_data(client, reg,
653                                   data->pwm[sattr->nr][sattr->index]);
654
655         mutex_unlock(&data->lock);
656
657         return count;
658 }
659
660 /* Called by set_pwmctrl and set_pwmchan */
661
662 static int hw_set_pwm(struct i2c_client *client, int index,
663                       unsigned int pwmctl, unsigned int pwmchan)
664 {
665         struct adt7475_data *data = i2c_get_clientdata(client);
666         long val = 0;
667
668         switch (pwmctl) {
669         case 0:
670                 val = 0x03;     /* Run at full speed */
671                 break;
672         case 1:
673                 val = 0x07;     /* Manual mode */
674                 break;
675         case 2:
676                 switch (pwmchan) {
677                 case 1:
678                         /* Remote1 controls PWM */
679                         val = 0x00;
680                         break;
681                 case 2:
682                         /* local controls PWM */
683                         val = 0x01;
684                         break;
685                 case 4:
686                         /* remote2 controls PWM */
687                         val = 0x02;
688                         break;
689                 case 6:
690                         /* local/remote2 control PWM */
691                         val = 0x05;
692                         break;
693                 case 7:
694                         /* All three control PWM */
695                         val = 0x06;
696                         break;
697                 default:
698                         return -EINVAL;
699                 }
700                 break;
701         default:
702                 return -EINVAL;
703         }
704
705         data->pwmctl[index] = pwmctl;
706         data->pwmchan[index] = pwmchan;
707
708         data->pwm[CONTROL][index] &= ~0xE0;
709         data->pwm[CONTROL][index] |= (val & 7) << 5;
710
711         i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
712                                   data->pwm[CONTROL][index]);
713
714         return 0;
715 }
716
717 static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
718                            const char *buf, size_t count)
719 {
720         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
721         struct i2c_client *client = to_i2c_client(dev);
722         struct adt7475_data *data = i2c_get_clientdata(client);
723         int r;
724         long val;
725
726         if (strict_strtol(buf, 10, &val))
727                 return -EINVAL;
728
729         mutex_lock(&data->lock);
730         /* Read Modify Write PWM values */
731         adt7475_read_pwm(client, sattr->index);
732         r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
733         if (r)
734                 count = r;
735         mutex_unlock(&data->lock);
736
737         return count;
738 }
739
740 static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
741                            const char *buf, size_t count)
742 {
743         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
744         struct i2c_client *client = to_i2c_client(dev);
745         struct adt7475_data *data = i2c_get_clientdata(client);
746         int r;
747         long val;
748
749         if (strict_strtol(buf, 10, &val))
750                 return -EINVAL;
751
752         mutex_lock(&data->lock);
753         /* Read Modify Write PWM values */
754         adt7475_read_pwm(client, sattr->index);
755         r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
756         if (r)
757                 count = r;
758         mutex_unlock(&data->lock);
759
760         return count;
761 }
762
763 /* List of frequencies for the PWM */
764 static const int pwmfreq_table[] = {
765         11, 14, 22, 29, 35, 44, 58, 88
766 };
767
768 static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
769                             char *buf)
770 {
771         struct adt7475_data *data = adt7475_update_device(dev);
772         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
773
774         return sprintf(buf, "%d\n",
775                        pwmfreq_table[data->range[sattr->index] & 7]);
776 }
777
778 static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
779                            const char *buf, size_t count)
780 {
781         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
782         struct i2c_client *client = to_i2c_client(dev);
783         struct adt7475_data *data = i2c_get_clientdata(client);
784         int out;
785         long val;
786
787         if (strict_strtol(buf, 10, &val))
788                 return -EINVAL;
789
790         out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
791
792         mutex_lock(&data->lock);
793
794         data->range[sattr->index] =
795                 adt7475_read(TEMP_TRANGE_REG(sattr->index));
796         data->range[sattr->index] &= ~7;
797         data->range[sattr->index] |= out;
798
799         i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
800                                   data->range[sattr->index]);
801
802         mutex_unlock(&data->lock);
803         return count;
804 }
805
806 static ssize_t show_pwm_at_crit(struct device *dev,
807                                 struct device_attribute *devattr, char *buf)
808 {
809         struct adt7475_data *data = adt7475_update_device(dev);
810         return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
811 }
812
813 static ssize_t set_pwm_at_crit(struct device *dev,
814                                struct device_attribute *devattr,
815                                const char *buf, size_t count)
816 {
817         struct i2c_client *client = to_i2c_client(dev);
818         struct adt7475_data *data = i2c_get_clientdata(client);
819         long val;
820
821         if (strict_strtol(buf, 10, &val))
822                 return -EINVAL;
823         if (val != 0 && val != 1)
824                 return -EINVAL;
825
826         mutex_lock(&data->lock);
827         data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
828         if (val)
829                 data->config4 |= CONFIG4_MAXDUTY;
830         else
831                 data->config4 &= ~CONFIG4_MAXDUTY;
832         i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
833         mutex_unlock(&data->lock);
834
835         return count;
836 }
837
838 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
839 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
840                             set_voltage, MAX, 0);
841 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
842                             set_voltage, MIN, 0);
843 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
844 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
845 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
846                             set_voltage, MAX, 1);
847 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
848                             set_voltage, MIN, 1);
849 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
850 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
851 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
852                             set_voltage, MAX, 2);
853 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
854                             set_voltage, MIN, 2);
855 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
856 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
857 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
858                             set_voltage, MAX, 3);
859 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
860                             set_voltage, MIN, 3);
861 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
862 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
863 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
864                             set_voltage, MAX, 4);
865 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
866                             set_voltage, MIN, 4);
867 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
868 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
869 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
870                             set_voltage, MAX, 5);
871 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
872                             set_voltage, MIN, 5);
873 static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
874 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
875 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
876 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
877 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
878                             MAX, 0);
879 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
880                             MIN, 0);
881 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
882                             set_temp, OFFSET, 0);
883 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
884                             show_temp, set_temp, AUTOMIN, 0);
885 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
886                             show_point2, set_point2, 0, 0);
887 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
888                             THERM, 0);
889 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
890                             set_temp, HYSTERSIS, 0);
891 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
892 static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
893 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
894                             MAX, 1);
895 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
896                             MIN, 1);
897 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
898                             set_temp, OFFSET, 1);
899 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
900                             show_temp, set_temp, AUTOMIN, 1);
901 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
902                             show_point2, set_point2, 0, 1);
903 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
904                             THERM, 1);
905 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
906                             set_temp, HYSTERSIS, 1);
907 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
908 static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
909 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
910 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
911                             MAX, 2);
912 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
913                             MIN, 2);
914 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
915                             set_temp, OFFSET, 2);
916 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
917                             show_temp, set_temp, AUTOMIN, 2);
918 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
919                             show_point2, set_point2, 0, 2);
920 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
921                             THERM, 2);
922 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
923                             set_temp, HYSTERSIS, 2);
924 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
925 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
926                             MIN, 0);
927 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
928 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
929 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
930                             MIN, 1);
931 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
932 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
933 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
934                             MIN, 2);
935 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
936 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
937 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
938                             MIN, 3);
939 static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
940 static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
941                             0);
942 static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
943                             set_pwmfreq, INPUT, 0);
944 static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
945                             set_pwmctrl, INPUT, 0);
946 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
947                             show_pwmchan, set_pwmchan, INPUT, 0);
948 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
949                             set_pwm, MIN, 0);
950 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
951                             set_pwm, MAX, 0);
952 static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
953                             1);
954 static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
955                             set_pwmfreq, INPUT, 1);
956 static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
957                             set_pwmctrl, INPUT, 1);
958 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
959                             show_pwmchan, set_pwmchan, INPUT, 1);
960 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
961                             set_pwm, MIN, 1);
962 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
963                             set_pwm, MAX, 1);
964 static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
965                             2);
966 static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
967                             set_pwmfreq, INPUT, 2);
968 static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
969                             set_pwmctrl, INPUT, 2);
970 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
971                             show_pwmchan, set_pwmchan, INPUT, 2);
972 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
973                             set_pwm, MIN, 2);
974 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
975                             set_pwm, MAX, 2);
976
977 /* Non-standard name, might need revisiting */
978 static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
979                    show_pwm_at_crit, set_pwm_at_crit);
980
981 static struct attribute *adt7475_attrs[] = {
982         &sensor_dev_attr_in1_input.dev_attr.attr,
983         &sensor_dev_attr_in1_max.dev_attr.attr,
984         &sensor_dev_attr_in1_min.dev_attr.attr,
985         &sensor_dev_attr_in1_alarm.dev_attr.attr,
986         &sensor_dev_attr_in2_input.dev_attr.attr,
987         &sensor_dev_attr_in2_max.dev_attr.attr,
988         &sensor_dev_attr_in2_min.dev_attr.attr,
989         &sensor_dev_attr_in2_alarm.dev_attr.attr,
990         &sensor_dev_attr_temp1_input.dev_attr.attr,
991         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
992         &sensor_dev_attr_temp1_fault.dev_attr.attr,
993         &sensor_dev_attr_temp1_max.dev_attr.attr,
994         &sensor_dev_attr_temp1_min.dev_attr.attr,
995         &sensor_dev_attr_temp1_offset.dev_attr.attr,
996         &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
997         &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
998         &sensor_dev_attr_temp1_crit.dev_attr.attr,
999         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1000         &sensor_dev_attr_temp2_input.dev_attr.attr,
1001         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1002         &sensor_dev_attr_temp2_max.dev_attr.attr,
1003         &sensor_dev_attr_temp2_min.dev_attr.attr,
1004         &sensor_dev_attr_temp2_offset.dev_attr.attr,
1005         &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1006         &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1007         &sensor_dev_attr_temp2_crit.dev_attr.attr,
1008         &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1009         &sensor_dev_attr_temp3_input.dev_attr.attr,
1010         &sensor_dev_attr_temp3_fault.dev_attr.attr,
1011         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1012         &sensor_dev_attr_temp3_max.dev_attr.attr,
1013         &sensor_dev_attr_temp3_min.dev_attr.attr,
1014         &sensor_dev_attr_temp3_offset.dev_attr.attr,
1015         &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1016         &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1017         &sensor_dev_attr_temp3_crit.dev_attr.attr,
1018         &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1019         &sensor_dev_attr_fan1_input.dev_attr.attr,
1020         &sensor_dev_attr_fan1_min.dev_attr.attr,
1021         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1022         &sensor_dev_attr_fan2_input.dev_attr.attr,
1023         &sensor_dev_attr_fan2_min.dev_attr.attr,
1024         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1025         &sensor_dev_attr_fan3_input.dev_attr.attr,
1026         &sensor_dev_attr_fan3_min.dev_attr.attr,
1027         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1028         &sensor_dev_attr_fan4_input.dev_attr.attr,
1029         &sensor_dev_attr_fan4_min.dev_attr.attr,
1030         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1031         &sensor_dev_attr_pwm1.dev_attr.attr,
1032         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1033         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1034         &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1035         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1036         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1037         &sensor_dev_attr_pwm2.dev_attr.attr,
1038         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1039         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1040         &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1041         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1042         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1043         &sensor_dev_attr_pwm3.dev_attr.attr,
1044         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1045         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1046         &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1047         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1048         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1049         &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1050         NULL,
1051 };
1052
1053 /* Attributes specific to the ADT7490 */
1054 static struct attribute *adt7490_attrs[] = {
1055         &sensor_dev_attr_in0_input.dev_attr.attr,
1056         &sensor_dev_attr_in0_max.dev_attr.attr,
1057         &sensor_dev_attr_in0_min.dev_attr.attr,
1058         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1059         &sensor_dev_attr_in3_input.dev_attr.attr,
1060         &sensor_dev_attr_in3_max.dev_attr.attr,
1061         &sensor_dev_attr_in3_min.dev_attr.attr,
1062         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1063         &sensor_dev_attr_in4_input.dev_attr.attr,
1064         &sensor_dev_attr_in4_max.dev_attr.attr,
1065         &sensor_dev_attr_in4_min.dev_attr.attr,
1066         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1067         &sensor_dev_attr_in5_input.dev_attr.attr,
1068         &sensor_dev_attr_in5_max.dev_attr.attr,
1069         &sensor_dev_attr_in5_min.dev_attr.attr,
1070         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1071         NULL
1072 };
1073
1074 static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1075 static struct attribute_group adt7490_attr_group = { .attrs = adt7490_attrs };
1076
1077 static int adt7475_detect(struct i2c_client *client, int kind,
1078                           struct i2c_board_info *info)
1079 {
1080         struct i2c_adapter *adapter = client->adapter;
1081         int vendid, devid, devid2;
1082         const char *name;
1083
1084         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1085                 return -ENODEV;
1086
1087         vendid = adt7475_read(REG_VENDID);
1088         devid2 = adt7475_read(REG_DEVID2);
1089         if (vendid != 0x41 ||           /* Analog Devices */
1090             (devid2 & 0xf8) != 0x68)
1091                 return -ENODEV;
1092
1093         devid = adt7475_read(REG_DEVID);
1094         if (devid == 0x73)
1095                 name = "adt7473";
1096         else if (devid == 0x75 && client->addr == 0x2e)
1097                 name = "adt7475";
1098         else if ((devid2 & 0xfc) == 0x6c)
1099                 name = "adt7490";
1100         else {
1101                 dev_dbg(&adapter->dev,
1102                         "Couldn't detect an ADT7473/75/90 part at "
1103                         "0x%02x\n", (unsigned int)client->addr);
1104                 return -ENODEV;
1105         }
1106
1107         strlcpy(info->type, name, I2C_NAME_SIZE);
1108
1109         return 0;
1110 }
1111
1112 static void adt7475_remove_files(struct i2c_client *client,
1113                                  struct adt7475_data *data)
1114 {
1115         sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1116         if (data->has_voltage & 0x39)
1117                 sysfs_remove_group(&client->dev.kobj, &adt7490_attr_group);
1118 }
1119
1120 static int adt7475_probe(struct i2c_client *client,
1121                          const struct i2c_device_id *id)
1122 {
1123         struct adt7475_data *data;
1124         int i, ret = 0;
1125
1126         data = kzalloc(sizeof(*data), GFP_KERNEL);
1127         if (data == NULL)
1128                 return -ENOMEM;
1129
1130         mutex_init(&data->lock);
1131         i2c_set_clientdata(client, data);
1132
1133         /* Initialize device-specific values */
1134         switch (id->driver_data) {
1135         case adt7490:
1136                 data->has_voltage = 0x3f;       /* in0 to in5 */
1137                 break;
1138         default:
1139                 data->has_voltage = 0x06;       /* in1, in2 */
1140         }
1141
1142         /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1143            pwm's which are disabled to manual mode with 0% duty cycle */
1144         for (i = 0; i < ADT7475_PWM_COUNT; i++)
1145                 adt7475_read_pwm(client, i);
1146
1147         ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1148         if (ret)
1149                 goto efree;
1150
1151         if (id->driver_data == adt7490) {
1152                 ret = sysfs_create_group(&client->dev.kobj,
1153                                          &adt7490_attr_group);
1154                 if (ret)
1155                         goto eremove;
1156         }
1157
1158         data->hwmon_dev = hwmon_device_register(&client->dev);
1159         if (IS_ERR(data->hwmon_dev)) {
1160                 ret = PTR_ERR(data->hwmon_dev);
1161                 goto eremove;
1162         }
1163
1164         return 0;
1165
1166 eremove:
1167         adt7475_remove_files(client, data);
1168 efree:
1169         kfree(data);
1170         return ret;
1171 }
1172
1173 static int adt7475_remove(struct i2c_client *client)
1174 {
1175         struct adt7475_data *data = i2c_get_clientdata(client);
1176
1177         hwmon_device_unregister(data->hwmon_dev);
1178         adt7475_remove_files(client, data);
1179         kfree(data);
1180
1181         return 0;
1182 }
1183
1184 static struct i2c_driver adt7475_driver = {
1185         .class          = I2C_CLASS_HWMON,
1186         .driver = {
1187                 .name   = "adt7475",
1188         },
1189         .probe          = adt7475_probe,
1190         .remove         = adt7475_remove,
1191         .id_table       = adt7475_id,
1192         .detect         = adt7475_detect,
1193         .address_data   = &addr_data,
1194 };
1195
1196 static void adt7475_read_hystersis(struct i2c_client *client)
1197 {
1198         struct adt7475_data *data = i2c_get_clientdata(client);
1199
1200         data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1201         data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1202         data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1203 }
1204
1205 static void adt7475_read_pwm(struct i2c_client *client, int index)
1206 {
1207         struct adt7475_data *data = i2c_get_clientdata(client);
1208         unsigned int v;
1209
1210         data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1211
1212         /* Figure out the internal value for pwmctrl and pwmchan
1213            based on the current settings */
1214         v = (data->pwm[CONTROL][index] >> 5) & 7;
1215
1216         if (v == 3)
1217                 data->pwmctl[index] = 0;
1218         else if (v == 7)
1219                 data->pwmctl[index] = 1;
1220         else if (v == 4) {
1221                 /* The fan is disabled - we don't want to
1222                    support that, so change to manual mode and
1223                    set the duty cycle to 0 instead
1224                 */
1225                 data->pwm[INPUT][index] = 0;
1226                 data->pwm[CONTROL][index] &= ~0xE0;
1227                 data->pwm[CONTROL][index] |= (7 << 5);
1228
1229                 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1230                                           data->pwm[INPUT][index]);
1231
1232                 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1233                                           data->pwm[CONTROL][index]);
1234
1235                 data->pwmctl[index] = 1;
1236         } else {
1237                 data->pwmctl[index] = 2;
1238
1239                 switch (v) {
1240                 case 0:
1241                         data->pwmchan[index] = 1;
1242                         break;
1243                 case 1:
1244                         data->pwmchan[index] = 2;
1245                         break;
1246                 case 2:
1247                         data->pwmchan[index] = 4;
1248                         break;
1249                 case 5:
1250                         data->pwmchan[index] = 6;
1251                         break;
1252                 case 6:
1253                         data->pwmchan[index] = 7;
1254                         break;
1255                 }
1256         }
1257 }
1258
1259 static struct adt7475_data *adt7475_update_device(struct device *dev)
1260 {
1261         struct i2c_client *client = to_i2c_client(dev);
1262         struct adt7475_data *data = i2c_get_clientdata(client);
1263         u16 ext;
1264         int i;
1265
1266         mutex_lock(&data->lock);
1267
1268         /* Measurement values update every 2 seconds */
1269         if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1270             !data->valid) {
1271                 data->alarms = adt7475_read(REG_STATUS2) << 8;
1272                 data->alarms |= adt7475_read(REG_STATUS1);
1273
1274                 ext = (adt7475_read(REG_EXTEND2) << 8) |
1275                         adt7475_read(REG_EXTEND1);
1276                 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1277                         if (!(data->has_voltage & (1 << i)))
1278                                 continue;
1279                         data->voltage[INPUT][i] =
1280                                 (adt7475_read(VOLTAGE_REG(i)) << 2) |
1281                                 ((ext >> (i * 2)) & 3);
1282                 }
1283
1284                 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1285                         data->temp[INPUT][i] =
1286                                 (adt7475_read(TEMP_REG(i)) << 2) |
1287                                 ((ext >> ((i + 5) * 2)) & 3);
1288
1289                 if (data->has_voltage & (1 << 5)) {
1290                         data->alarms |= adt7475_read(REG_STATUS4) << 24;
1291                         ext = adt7475_read(REG_EXTEND3);
1292                         data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1293                                 ((ext >> 4) & 3);
1294                 }
1295
1296                 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1297                         data->tach[INPUT][i] =
1298                                 adt7475_read_word(client, TACH_REG(i));
1299
1300                 /* Updated by hw when in auto mode */
1301                 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1302                         data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1303
1304                 data->measure_updated = jiffies;
1305         }
1306
1307         /* Limits and settings, should never change update every 60 seconds */
1308         if (time_after(jiffies, data->limits_updated + HZ * 60) ||
1309             !data->valid) {
1310                 data->config4 = adt7475_read(REG_CONFIG4);
1311                 data->config5 = adt7475_read(REG_CONFIG5);
1312
1313                 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1314                         if (!(data->has_voltage & (1 << i)))
1315                                 continue;
1316                         /* Adjust values so they match the input precision */
1317                         data->voltage[MIN][i] =
1318                                 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1319                         data->voltage[MAX][i] =
1320                                 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1321                 }
1322
1323                 if (data->has_voltage & (1 << 5)) {
1324                         data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1325                         data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1326                 }
1327
1328                 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1329                         /* Adjust values so they match the input precision */
1330                         data->temp[MIN][i] =
1331                                 adt7475_read(TEMP_MIN_REG(i)) << 2;
1332                         data->temp[MAX][i] =
1333                                 adt7475_read(TEMP_MAX_REG(i)) << 2;
1334                         data->temp[AUTOMIN][i] =
1335                                 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1336                         data->temp[THERM][i] =
1337                                 adt7475_read(TEMP_THERM_REG(i)) << 2;
1338                         data->temp[OFFSET][i] =
1339                                 adt7475_read(TEMP_OFFSET_REG(i));
1340                 }
1341                 adt7475_read_hystersis(client);
1342
1343                 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1344                         data->tach[MIN][i] =
1345                                 adt7475_read_word(client, TACH_MIN_REG(i));
1346
1347                 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1348                         data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1349                         data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1350                         /* Set the channel and control information */
1351                         adt7475_read_pwm(client, i);
1352                 }
1353
1354                 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1355                 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1356                 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1357
1358                 data->limits_updated = jiffies;
1359                 data->valid = 1;
1360         }
1361
1362         mutex_unlock(&data->lock);
1363
1364         return data;
1365 }
1366
1367 static int __init sensors_adt7475_init(void)
1368 {
1369         return i2c_add_driver(&adt7475_driver);
1370 }
1371
1372 static void __exit sensors_adt7475_exit(void)
1373 {
1374         i2c_del_driver(&adt7475_driver);
1375 }
1376
1377 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1378 MODULE_DESCRIPTION("adt7475 driver");
1379 MODULE_LICENSE("GPL");
1380
1381 module_init(sensors_adt7475_init);
1382 module_exit(sensors_adt7475_exit);