Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[pandora-kernel.git] / drivers / hwmon / lm85.c
1 /*
2     lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5     Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
6     Copyright (c) 2003        Margit Schubert-While <margitsw@t-online.de>
7     Copyright (c) 2004        Justin Thiessen <jthiessen@penguincomputing.com>
8     Copyright (C) 2007--2009  Jean Delvare <khali@linux-fr.org>
9
10     Chip details at           <http://www.national.com/ds/LM/LM85.pdf>
11
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37
38 /* Addresses to scan */
39 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40
41 enum chips {
42         any_chip, lm85b, lm85c,
43         adm1027, adt7463, adt7468,
44         emc6d100, emc6d102
45 };
46
47 /* The LM85 registers */
48
49 #define LM85_REG_IN(nr)                 (0x20 + (nr))
50 #define LM85_REG_IN_MIN(nr)             (0x44 + (nr) * 2)
51 #define LM85_REG_IN_MAX(nr)             (0x45 + (nr) * 2)
52
53 #define LM85_REG_TEMP(nr)               (0x25 + (nr))
54 #define LM85_REG_TEMP_MIN(nr)           (0x4e + (nr) * 2)
55 #define LM85_REG_TEMP_MAX(nr)           (0x4f + (nr) * 2)
56
57 /* Fan speeds are LSB, MSB (2 bytes) */
58 #define LM85_REG_FAN(nr)                (0x28 + (nr) * 2)
59 #define LM85_REG_FAN_MIN(nr)            (0x54 + (nr) * 2)
60
61 #define LM85_REG_PWM(nr)                (0x30 + (nr))
62
63 #define LM85_REG_COMPANY                0x3e
64 #define LM85_REG_VERSTEP                0x3f
65
66 #define ADT7468_REG_CFG5                0x7c
67 #define         ADT7468_OFF64           (1 << 0)
68 #define         ADT7468_HFPWM           (1 << 1)
69 #define IS_ADT7468_OFF64(data)          \
70         ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
71 #define IS_ADT7468_HFPWM(data)          \
72         ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
73
74 /* These are the recognized values for the above regs */
75 #define LM85_COMPANY_NATIONAL           0x01
76 #define LM85_COMPANY_ANALOG_DEV         0x41
77 #define LM85_COMPANY_SMSC               0x5c
78 #define LM85_VERSTEP_VMASK              0xf0
79 #define LM85_VERSTEP_GENERIC            0x60
80 #define LM85_VERSTEP_GENERIC2           0x70
81 #define LM85_VERSTEP_LM85C              0x60
82 #define LM85_VERSTEP_LM85B              0x62
83 #define LM85_VERSTEP_LM96000_1          0x68
84 #define LM85_VERSTEP_LM96000_2          0x69
85 #define LM85_VERSTEP_ADM1027            0x60
86 #define LM85_VERSTEP_ADT7463            0x62
87 #define LM85_VERSTEP_ADT7463C           0x6A
88 #define LM85_VERSTEP_ADT7468_1          0x71
89 #define LM85_VERSTEP_ADT7468_2          0x72
90 #define LM85_VERSTEP_EMC6D100_A0        0x60
91 #define LM85_VERSTEP_EMC6D100_A1        0x61
92 #define LM85_VERSTEP_EMC6D102           0x65
93
94 #define LM85_REG_CONFIG                 0x40
95
96 #define LM85_REG_ALARM1                 0x41
97 #define LM85_REG_ALARM2                 0x42
98
99 #define LM85_REG_VID                    0x43
100
101 /* Automated FAN control */
102 #define LM85_REG_AFAN_CONFIG(nr)        (0x5c + (nr))
103 #define LM85_REG_AFAN_RANGE(nr)         (0x5f + (nr))
104 #define LM85_REG_AFAN_SPIKE1            0x62
105 #define LM85_REG_AFAN_MINPWM(nr)        (0x64 + (nr))
106 #define LM85_REG_AFAN_LIMIT(nr)         (0x67 + (nr))
107 #define LM85_REG_AFAN_CRITICAL(nr)      (0x6a + (nr))
108 #define LM85_REG_AFAN_HYST1             0x6d
109 #define LM85_REG_AFAN_HYST2             0x6e
110
111 #define ADM1027_REG_EXTEND_ADC1         0x76
112 #define ADM1027_REG_EXTEND_ADC2         0x77
113
114 #define EMC6D100_REG_ALARM3             0x7d
115 /* IN5, IN6 and IN7 */
116 #define EMC6D100_REG_IN(nr)             (0x70 + ((nr) - 5))
117 #define EMC6D100_REG_IN_MIN(nr)         (0x73 + ((nr) - 5) * 2)
118 #define EMC6D100_REG_IN_MAX(nr)         (0x74 + ((nr) - 5) * 2)
119 #define EMC6D102_REG_EXTEND_ADC1        0x85
120 #define EMC6D102_REG_EXTEND_ADC2        0x86
121 #define EMC6D102_REG_EXTEND_ADC3        0x87
122 #define EMC6D102_REG_EXTEND_ADC4        0x88
123
124
125 /* Conversions. Rounding and limit checking is only done on the TO_REG
126    variants. Note that you should be a bit careful with which arguments
127    these macros are called: arguments may be evaluated more than once.
128  */
129
130 /* IN are scaled acording to built-in resistors */
131 static const int lm85_scaling[] = {  /* .001 Volts */
132         2500, 2250, 3300, 5000, 12000,
133         3300, 1500, 1800 /*EMC6D100*/
134 };
135 #define SCALE(val, from, to)    (((val) * (to) + ((from) / 2)) / (from))
136
137 #define INS_TO_REG(n, val)      \
138                 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
139
140 #define INSEXT_FROM_REG(n, val, ext)    \
141                 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
142
143 #define INS_FROM_REG(n, val)    SCALE((val), 192, lm85_scaling[n])
144
145 /* FAN speed is measured using 90kHz clock */
146 static inline u16 FAN_TO_REG(unsigned long val)
147 {
148         if (!val)
149                 return 0xffff;
150         return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
151 }
152 #define FAN_FROM_REG(val)       ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
153                                  5400000 / (val))
154
155 /* Temperature is reported in .001 degC increments */
156 #define TEMP_TO_REG(val)        \
157                 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
158 #define TEMPEXT_FROM_REG(val, ext)      \
159                 SCALE(((val) << 4) + (ext), 16, 1000)
160 #define TEMP_FROM_REG(val)      ((val) * 1000)
161
162 #define PWM_TO_REG(val)                 SENSORS_LIMIT(val, 0, 255)
163 #define PWM_FROM_REG(val)               (val)
164
165
166 /* ZONEs have the following parameters:
167  *    Limit (low) temp,           1. degC
168  *    Hysteresis (below limit),   1. degC (0-15)
169  *    Range of speed control,     .1 degC (2-80)
170  *    Critical (high) temp,       1. degC
171  *
172  * FAN PWMs have the following parameters:
173  *    Reference Zone,                 1, 2, 3, etc.
174  *    Spinup time,                    .05 sec
175  *    PWM value at limit/low temp,    1 count
176  *    PWM Frequency,                  1. Hz
177  *    PWM is Min or OFF below limit,  flag
178  *    Invert PWM output,              flag
179  *
180  * Some chips filter the temp, others the fan.
181  *    Filter constant (or disabled)   .1 seconds
182  */
183
184 /* These are the zone temperature range encodings in .001 degree C */
185 static const int lm85_range_map[] = {
186         2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
187         13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
188 };
189
190 static int RANGE_TO_REG(int range)
191 {
192         int i;
193
194         /* Find the closest match */
195         for (i = 0; i < 15; ++i) {
196                 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
197                         break;
198         }
199
200         return i;
201 }
202 #define RANGE_FROM_REG(val)     lm85_range_map[(val) & 0x0f]
203
204 /* These are the PWM frequency encodings */
205 static const int lm85_freq_map[8] = { /* 1 Hz */
206         10, 15, 23, 30, 38, 47, 61, 94
207 };
208 static const int adm1027_freq_map[8] = { /* 1 Hz */
209         11, 15, 22, 29, 35, 44, 59, 88
210 };
211
212 static int FREQ_TO_REG(const int *map, int freq)
213 {
214         int i;
215
216         /* Find the closest match */
217         for (i = 0; i < 7; ++i)
218                 if (freq <= (map[i] + map[i + 1]) / 2)
219                         break;
220         return i;
221 }
222
223 static int FREQ_FROM_REG(const int *map, u8 reg)
224 {
225         return map[reg & 0x07];
226 }
227
228 /* Since we can't use strings, I'm abusing these numbers
229  *   to stand in for the following meanings:
230  *      1 -- PWM responds to Zone 1
231  *      2 -- PWM responds to Zone 2
232  *      3 -- PWM responds to Zone 3
233  *     23 -- PWM responds to the higher temp of Zone 2 or 3
234  *    123 -- PWM responds to highest of Zone 1, 2, or 3
235  *      0 -- PWM is always at 0% (ie, off)
236  *     -1 -- PWM is always at 100%
237  *     -2 -- PWM responds to manual control
238  */
239
240 static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
241 #define ZONE_FROM_REG(val)      lm85_zone_map[(val) >> 5]
242
243 static int ZONE_TO_REG(int zone)
244 {
245         int i;
246
247         for (i = 0; i <= 7; ++i)
248                 if (zone == lm85_zone_map[i])
249                         break;
250         if (i > 7)   /* Not found. */
251                 i = 3;  /* Always 100% */
252         return i << 5;
253 }
254
255 #define HYST_TO_REG(val)        SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
256 #define HYST_FROM_REG(val)      ((val) * 1000)
257
258 /* Chip sampling rates
259  *
260  * Some sensors are not updated more frequently than once per second
261  *    so it doesn't make sense to read them more often than that.
262  *    We cache the results and return the saved data if the driver
263  *    is called again before a second has elapsed.
264  *
265  * Also, there is significant configuration data for this chip
266  *    given the automatic PWM fan control that is possible.  There
267  *    are about 47 bytes of config data to only 22 bytes of actual
268  *    readings.  So, we keep the config data up to date in the cache
269  *    when it is written and only sample it once every 1 *minute*
270  */
271 #define LM85_DATA_INTERVAL  (HZ + HZ / 2)
272 #define LM85_CONFIG_INTERVAL  (1 * 60 * HZ)
273
274 /* LM85 can automatically adjust fan speeds based on temperature
275  * This structure encapsulates an entire Zone config.  There are
276  * three zones (one for each temperature input) on the lm85
277  */
278 struct lm85_zone {
279         s8 limit;       /* Low temp limit */
280         u8 hyst;        /* Low limit hysteresis. (0-15) */
281         u8 range;       /* Temp range, encoded */
282         s8 critical;    /* "All fans ON" temp limit */
283         u8 off_desired; /* Actual "off" temperature specified.  Preserved
284                          * to prevent "drift" as other autofan control
285                          * values change.
286                          */
287         u8 max_desired; /* Actual "max" temperature specified.  Preserved
288                          * to prevent "drift" as other autofan control
289                          * values change.
290                          */
291 };
292
293 struct lm85_autofan {
294         u8 config;      /* Register value */
295         u8 min_pwm;     /* Minimum PWM value, encoded */
296         u8 min_off;     /* Min PWM or OFF below "limit", flag */
297 };
298
299 /* For each registered chip, we need to keep some data in memory.
300    The structure is dynamically allocated. */
301 struct lm85_data {
302         struct device *hwmon_dev;
303         const int *freq_map;
304         enum chips type;
305
306         struct mutex update_lock;
307         int valid;              /* !=0 if following fields are valid */
308         unsigned long last_reading;     /* In jiffies */
309         unsigned long last_config;      /* In jiffies */
310
311         u8 in[8];               /* Register value */
312         u8 in_max[8];           /* Register value */
313         u8 in_min[8];           /* Register value */
314         s8 temp[3];             /* Register value */
315         s8 temp_min[3];         /* Register value */
316         s8 temp_max[3];         /* Register value */
317         u16 fan[4];             /* Register value */
318         u16 fan_min[4];         /* Register value */
319         u8 pwm[3];              /* Register value */
320         u8 pwm_freq[3];         /* Register encoding */
321         u8 temp_ext[3];         /* Decoded values */
322         u8 in_ext[8];           /* Decoded values */
323         u8 vid;                 /* Register value */
324         u8 vrm;                 /* VRM version */
325         u32 alarms;             /* Register encoding, combined */
326         u8 cfg5;                /* Config Register 5 on ADT7468 */
327         struct lm85_autofan autofan[3];
328         struct lm85_zone zone[3];
329 };
330
331 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
332 static int lm85_probe(struct i2c_client *client,
333                       const struct i2c_device_id *id);
334 static int lm85_remove(struct i2c_client *client);
335
336 static int lm85_read_value(struct i2c_client *client, u8 reg);
337 static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
338 static struct lm85_data *lm85_update_device(struct device *dev);
339
340
341 static const struct i2c_device_id lm85_id[] = {
342         { "adm1027", adm1027 },
343         { "adt7463", adt7463 },
344         { "adt7468", adt7468 },
345         { "lm85", any_chip },
346         { "lm85b", lm85b },
347         { "lm85c", lm85c },
348         { "emc6d100", emc6d100 },
349         { "emc6d101", emc6d100 },
350         { "emc6d102", emc6d102 },
351         { }
352 };
353 MODULE_DEVICE_TABLE(i2c, lm85_id);
354
355 static struct i2c_driver lm85_driver = {
356         .class          = I2C_CLASS_HWMON,
357         .driver = {
358                 .name   = "lm85",
359         },
360         .probe          = lm85_probe,
361         .remove         = lm85_remove,
362         .id_table       = lm85_id,
363         .detect         = lm85_detect,
364         .address_list   = normal_i2c,
365 };
366
367
368 /* 4 Fans */
369 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
370                 char *buf)
371 {
372         int nr = to_sensor_dev_attr(attr)->index;
373         struct lm85_data *data = lm85_update_device(dev);
374         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
375 }
376
377 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
378                 char *buf)
379 {
380         int nr = to_sensor_dev_attr(attr)->index;
381         struct lm85_data *data = lm85_update_device(dev);
382         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
383 }
384
385 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
386                 const char *buf, size_t count)
387 {
388         int nr = to_sensor_dev_attr(attr)->index;
389         struct i2c_client *client = to_i2c_client(dev);
390         struct lm85_data *data = i2c_get_clientdata(client);
391         unsigned long val = simple_strtoul(buf, NULL, 10);
392
393         mutex_lock(&data->update_lock);
394         data->fan_min[nr] = FAN_TO_REG(val);
395         lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
396         mutex_unlock(&data->update_lock);
397         return count;
398 }
399
400 #define show_fan_offset(offset)                                         \
401 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,                 \
402                 show_fan, NULL, offset - 1);                            \
403 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,         \
404                 show_fan_min, set_fan_min, offset - 1)
405
406 show_fan_offset(1);
407 show_fan_offset(2);
408 show_fan_offset(3);
409 show_fan_offset(4);
410
411 /* vid, vrm, alarms */
412
413 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
414                 char *buf)
415 {
416         struct lm85_data *data = lm85_update_device(dev);
417         int vid;
418
419         if ((data->type == adt7463 || data->type == adt7468) &&
420             (data->vid & 0x80)) {
421                 /* 6-pin VID (VRM 10) */
422                 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
423         } else {
424                 /* 5-pin VID (VRM 9) */
425                 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
426         }
427
428         return sprintf(buf, "%d\n", vid);
429 }
430
431 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
432
433 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
434                 char *buf)
435 {
436         struct lm85_data *data = dev_get_drvdata(dev);
437         return sprintf(buf, "%ld\n", (long) data->vrm);
438 }
439
440 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
441                 const char *buf, size_t count)
442 {
443         struct lm85_data *data = dev_get_drvdata(dev);
444         data->vrm = simple_strtoul(buf, NULL, 10);
445         return count;
446 }
447
448 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
449
450 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
451                 *attr, char *buf)
452 {
453         struct lm85_data *data = lm85_update_device(dev);
454         return sprintf(buf, "%u\n", data->alarms);
455 }
456
457 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
458
459 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
460                 char *buf)
461 {
462         int nr = to_sensor_dev_attr(attr)->index;
463         struct lm85_data *data = lm85_update_device(dev);
464         return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
465 }
466
467 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
468 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
469 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
470 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
471 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
472 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
473 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
474 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
475 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
476 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
477 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
478 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
479 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
480 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
481 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
482 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
483 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
484
485 /* pwm */
486
487 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
488                 char *buf)
489 {
490         int nr = to_sensor_dev_attr(attr)->index;
491         struct lm85_data *data = lm85_update_device(dev);
492         return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
493 }
494
495 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
496                 const char *buf, size_t count)
497 {
498         int nr = to_sensor_dev_attr(attr)->index;
499         struct i2c_client *client = to_i2c_client(dev);
500         struct lm85_data *data = i2c_get_clientdata(client);
501         long val = simple_strtol(buf, NULL, 10);
502
503         mutex_lock(&data->update_lock);
504         data->pwm[nr] = PWM_TO_REG(val);
505         lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
506         mutex_unlock(&data->update_lock);
507         return count;
508 }
509
510 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
511                 *attr, char *buf)
512 {
513         int nr = to_sensor_dev_attr(attr)->index;
514         struct lm85_data *data = lm85_update_device(dev);
515         int pwm_zone, enable;
516
517         pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
518         switch (pwm_zone) {
519         case -1:        /* PWM is always at 100% */
520                 enable = 0;
521                 break;
522         case 0:         /* PWM is always at 0% */
523         case -2:        /* PWM responds to manual control */
524                 enable = 1;
525                 break;
526         default:        /* PWM in automatic mode */
527                 enable = 2;
528         }
529         return sprintf(buf, "%d\n", enable);
530 }
531
532 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
533                 *attr, const char *buf, size_t count)
534 {
535         int nr = to_sensor_dev_attr(attr)->index;
536         struct i2c_client *client = to_i2c_client(dev);
537         struct lm85_data *data = i2c_get_clientdata(client);
538         long val = simple_strtol(buf, NULL, 10);
539         u8 config;
540
541         switch (val) {
542         case 0:
543                 config = 3;
544                 break;
545         case 1:
546                 config = 7;
547                 break;
548         case 2:
549                 /* Here we have to choose arbitrarily one of the 5 possible
550                    configurations; I go for the safest */
551                 config = 6;
552                 break;
553         default:
554                 return -EINVAL;
555         }
556
557         mutex_lock(&data->update_lock);
558         data->autofan[nr].config = lm85_read_value(client,
559                 LM85_REG_AFAN_CONFIG(nr));
560         data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
561                 | (config << 5);
562         lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
563                 data->autofan[nr].config);
564         mutex_unlock(&data->update_lock);
565         return count;
566 }
567
568 static ssize_t show_pwm_freq(struct device *dev,
569                 struct device_attribute *attr, char *buf)
570 {
571         int nr = to_sensor_dev_attr(attr)->index;
572         struct lm85_data *data = lm85_update_device(dev);
573         int freq;
574
575         if (IS_ADT7468_HFPWM(data))
576                 freq = 22500;
577         else
578                 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
579
580         return sprintf(buf, "%d\n", freq);
581 }
582
583 static ssize_t set_pwm_freq(struct device *dev,
584                 struct device_attribute *attr, const char *buf, size_t count)
585 {
586         int nr = to_sensor_dev_attr(attr)->index;
587         struct i2c_client *client = to_i2c_client(dev);
588         struct lm85_data *data = i2c_get_clientdata(client);
589         long val = simple_strtol(buf, NULL, 10);
590
591         mutex_lock(&data->update_lock);
592         /* The ADT7468 has a special high-frequency PWM output mode,
593          * where all PWM outputs are driven by a 22.5 kHz clock.
594          * This might confuse the user, but there's not much we can do. */
595         if (data->type == adt7468 && val >= 11300) {    /* High freq. mode */
596                 data->cfg5 &= ~ADT7468_HFPWM;
597                 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
598         } else {                                        /* Low freq. mode */
599                 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
600                 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
601                                  (data->zone[nr].range << 4)
602                                  | data->pwm_freq[nr]);
603                 if (data->type == adt7468) {
604                         data->cfg5 |= ADT7468_HFPWM;
605                         lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
606                 }
607         }
608         mutex_unlock(&data->update_lock);
609         return count;
610 }
611
612 #define show_pwm_reg(offset)                                            \
613 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,               \
614                 show_pwm, set_pwm, offset - 1);                         \
615 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR,      \
616                 show_pwm_enable, set_pwm_enable, offset - 1);           \
617 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR,        \
618                 show_pwm_freq, set_pwm_freq, offset - 1)
619
620 show_pwm_reg(1);
621 show_pwm_reg(2);
622 show_pwm_reg(3);
623
624 /* Voltages */
625
626 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
627                 char *buf)
628 {
629         int nr = to_sensor_dev_attr(attr)->index;
630         struct lm85_data *data = lm85_update_device(dev);
631         return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
632                                                     data->in_ext[nr]));
633 }
634
635 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
636                 char *buf)
637 {
638         int nr = to_sensor_dev_attr(attr)->index;
639         struct lm85_data *data = lm85_update_device(dev);
640         return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
641 }
642
643 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
644                 const char *buf, size_t count)
645 {
646         int nr = to_sensor_dev_attr(attr)->index;
647         struct i2c_client *client = to_i2c_client(dev);
648         struct lm85_data *data = i2c_get_clientdata(client);
649         long val = simple_strtol(buf, NULL, 10);
650
651         mutex_lock(&data->update_lock);
652         data->in_min[nr] = INS_TO_REG(nr, val);
653         lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
654         mutex_unlock(&data->update_lock);
655         return count;
656 }
657
658 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
659                 char *buf)
660 {
661         int nr = to_sensor_dev_attr(attr)->index;
662         struct lm85_data *data = lm85_update_device(dev);
663         return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
664 }
665
666 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
667                 const char *buf, size_t count)
668 {
669         int nr = to_sensor_dev_attr(attr)->index;
670         struct i2c_client *client = to_i2c_client(dev);
671         struct lm85_data *data = i2c_get_clientdata(client);
672         long val = simple_strtol(buf, NULL, 10);
673
674         mutex_lock(&data->update_lock);
675         data->in_max[nr] = INS_TO_REG(nr, val);
676         lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
677         mutex_unlock(&data->update_lock);
678         return count;
679 }
680
681 #define show_in_reg(offset)                                             \
682 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,                  \
683                 show_in, NULL, offset);                                 \
684 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,          \
685                 show_in_min, set_in_min, offset);                       \
686 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,          \
687                 show_in_max, set_in_max, offset)
688
689 show_in_reg(0);
690 show_in_reg(1);
691 show_in_reg(2);
692 show_in_reg(3);
693 show_in_reg(4);
694 show_in_reg(5);
695 show_in_reg(6);
696 show_in_reg(7);
697
698 /* Temps */
699
700 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
701                 char *buf)
702 {
703         int nr = to_sensor_dev_attr(attr)->index;
704         struct lm85_data *data = lm85_update_device(dev);
705         return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
706                                                      data->temp_ext[nr]));
707 }
708
709 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
710                 char *buf)
711 {
712         int nr = to_sensor_dev_attr(attr)->index;
713         struct lm85_data *data = lm85_update_device(dev);
714         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
715 }
716
717 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
718                 const char *buf, size_t count)
719 {
720         int nr = to_sensor_dev_attr(attr)->index;
721         struct i2c_client *client = to_i2c_client(dev);
722         struct lm85_data *data = i2c_get_clientdata(client);
723         long val = simple_strtol(buf, NULL, 10);
724
725         if (IS_ADT7468_OFF64(data))
726                 val += 64;
727
728         mutex_lock(&data->update_lock);
729         data->temp_min[nr] = TEMP_TO_REG(val);
730         lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
731         mutex_unlock(&data->update_lock);
732         return count;
733 }
734
735 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
736                 char *buf)
737 {
738         int nr = to_sensor_dev_attr(attr)->index;
739         struct lm85_data *data = lm85_update_device(dev);
740         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
741 }
742
743 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
744                 const char *buf, size_t count)
745 {
746         int nr = to_sensor_dev_attr(attr)->index;
747         struct i2c_client *client = to_i2c_client(dev);
748         struct lm85_data *data = i2c_get_clientdata(client);
749         long val = simple_strtol(buf, NULL, 10);
750
751         if (IS_ADT7468_OFF64(data))
752                 val += 64;
753
754         mutex_lock(&data->update_lock);
755         data->temp_max[nr] = TEMP_TO_REG(val);
756         lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
757         mutex_unlock(&data->update_lock);
758         return count;
759 }
760
761 #define show_temp_reg(offset)                                           \
762 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,                \
763                 show_temp, NULL, offset - 1);                           \
764 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,        \
765                 show_temp_min, set_temp_min, offset - 1);               \
766 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,        \
767                 show_temp_max, set_temp_max, offset - 1);
768
769 show_temp_reg(1);
770 show_temp_reg(2);
771 show_temp_reg(3);
772
773
774 /* Automatic PWM control */
775
776 static ssize_t show_pwm_auto_channels(struct device *dev,
777                 struct device_attribute *attr, char *buf)
778 {
779         int nr = to_sensor_dev_attr(attr)->index;
780         struct lm85_data *data = lm85_update_device(dev);
781         return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
782 }
783
784 static ssize_t set_pwm_auto_channels(struct device *dev,
785                 struct device_attribute *attr, const char *buf, size_t count)
786 {
787         int nr = to_sensor_dev_attr(attr)->index;
788         struct i2c_client *client = to_i2c_client(dev);
789         struct lm85_data *data = i2c_get_clientdata(client);
790         long val = simple_strtol(buf, NULL, 10);
791
792         mutex_lock(&data->update_lock);
793         data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
794                 | ZONE_TO_REG(val);
795         lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
796                 data->autofan[nr].config);
797         mutex_unlock(&data->update_lock);
798         return count;
799 }
800
801 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
802                 struct device_attribute *attr, char *buf)
803 {
804         int nr = to_sensor_dev_attr(attr)->index;
805         struct lm85_data *data = lm85_update_device(dev);
806         return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
807 }
808
809 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
810                 struct device_attribute *attr, const char *buf, size_t count)
811 {
812         int nr = to_sensor_dev_attr(attr)->index;
813         struct i2c_client *client = to_i2c_client(dev);
814         struct lm85_data *data = i2c_get_clientdata(client);
815         long val = simple_strtol(buf, NULL, 10);
816
817         mutex_lock(&data->update_lock);
818         data->autofan[nr].min_pwm = PWM_TO_REG(val);
819         lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
820                 data->autofan[nr].min_pwm);
821         mutex_unlock(&data->update_lock);
822         return count;
823 }
824
825 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
826                 struct device_attribute *attr, char *buf)
827 {
828         int nr = to_sensor_dev_attr(attr)->index;
829         struct lm85_data *data = lm85_update_device(dev);
830         return sprintf(buf, "%d\n", data->autofan[nr].min_off);
831 }
832
833 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
834                 struct device_attribute *attr, const char *buf, size_t count)
835 {
836         int nr = to_sensor_dev_attr(attr)->index;
837         struct i2c_client *client = to_i2c_client(dev);
838         struct lm85_data *data = i2c_get_clientdata(client);
839         long val = simple_strtol(buf, NULL, 10);
840         u8 tmp;
841
842         mutex_lock(&data->update_lock);
843         data->autofan[nr].min_off = val;
844         tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
845         tmp &= ~(0x20 << nr);
846         if (data->autofan[nr].min_off)
847                 tmp |= 0x20 << nr;
848         lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
849         mutex_unlock(&data->update_lock);
850         return count;
851 }
852
853 #define pwm_auto(offset)                                                \
854 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels,                  \
855                 S_IRUGO | S_IWUSR, show_pwm_auto_channels,              \
856                 set_pwm_auto_channels, offset - 1);                     \
857 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min,                   \
858                 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min,               \
859                 set_pwm_auto_pwm_min, offset - 1);                      \
860 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl,                \
861                 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl,            \
862                 set_pwm_auto_pwm_minctl, offset - 1)
863
864 pwm_auto(1);
865 pwm_auto(2);
866 pwm_auto(3);
867
868 /* Temperature settings for automatic PWM control */
869
870 static ssize_t show_temp_auto_temp_off(struct device *dev,
871                 struct device_attribute *attr, char *buf)
872 {
873         int nr = to_sensor_dev_attr(attr)->index;
874         struct lm85_data *data = lm85_update_device(dev);
875         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
876                 HYST_FROM_REG(data->zone[nr].hyst));
877 }
878
879 static ssize_t set_temp_auto_temp_off(struct device *dev,
880                 struct device_attribute *attr, const char *buf, size_t count)
881 {
882         int nr = to_sensor_dev_attr(attr)->index;
883         struct i2c_client *client = to_i2c_client(dev);
884         struct lm85_data *data = i2c_get_clientdata(client);
885         int min;
886         long val = simple_strtol(buf, NULL, 10);
887
888         mutex_lock(&data->update_lock);
889         min = TEMP_FROM_REG(data->zone[nr].limit);
890         data->zone[nr].off_desired = TEMP_TO_REG(val);
891         data->zone[nr].hyst = HYST_TO_REG(min - val);
892         if (nr == 0 || nr == 1) {
893                 lm85_write_value(client, LM85_REG_AFAN_HYST1,
894                         (data->zone[0].hyst << 4)
895                         | data->zone[1].hyst);
896         } else {
897                 lm85_write_value(client, LM85_REG_AFAN_HYST2,
898                         (data->zone[2].hyst << 4));
899         }
900         mutex_unlock(&data->update_lock);
901         return count;
902 }
903
904 static ssize_t show_temp_auto_temp_min(struct device *dev,
905                 struct device_attribute *attr, char *buf)
906 {
907         int nr = to_sensor_dev_attr(attr)->index;
908         struct lm85_data *data = lm85_update_device(dev);
909         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
910 }
911
912 static ssize_t set_temp_auto_temp_min(struct device *dev,
913                 struct device_attribute *attr, const char *buf, size_t count)
914 {
915         int nr = to_sensor_dev_attr(attr)->index;
916         struct i2c_client *client = to_i2c_client(dev);
917         struct lm85_data *data = i2c_get_clientdata(client);
918         long val = simple_strtol(buf, NULL, 10);
919
920         mutex_lock(&data->update_lock);
921         data->zone[nr].limit = TEMP_TO_REG(val);
922         lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
923                 data->zone[nr].limit);
924
925 /* Update temp_auto_max and temp_auto_range */
926         data->zone[nr].range = RANGE_TO_REG(
927                 TEMP_FROM_REG(data->zone[nr].max_desired) -
928                 TEMP_FROM_REG(data->zone[nr].limit));
929         lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
930                 ((data->zone[nr].range & 0x0f) << 4)
931                 | (data->pwm_freq[nr] & 0x07));
932
933 /* Update temp_auto_hyst and temp_auto_off */
934         data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
935                 data->zone[nr].limit) - TEMP_FROM_REG(
936                 data->zone[nr].off_desired));
937         if (nr == 0 || nr == 1) {
938                 lm85_write_value(client, LM85_REG_AFAN_HYST1,
939                         (data->zone[0].hyst << 4)
940                         | data->zone[1].hyst);
941         } else {
942                 lm85_write_value(client, LM85_REG_AFAN_HYST2,
943                         (data->zone[2].hyst << 4));
944         }
945         mutex_unlock(&data->update_lock);
946         return count;
947 }
948
949 static ssize_t show_temp_auto_temp_max(struct device *dev,
950                 struct device_attribute *attr, char *buf)
951 {
952         int nr = to_sensor_dev_attr(attr)->index;
953         struct lm85_data *data = lm85_update_device(dev);
954         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
955                 RANGE_FROM_REG(data->zone[nr].range));
956 }
957
958 static ssize_t set_temp_auto_temp_max(struct device *dev,
959                 struct device_attribute *attr, const char *buf, size_t count)
960 {
961         int nr = to_sensor_dev_attr(attr)->index;
962         struct i2c_client *client = to_i2c_client(dev);
963         struct lm85_data *data = i2c_get_clientdata(client);
964         int min;
965         long val = simple_strtol(buf, NULL, 10);
966
967         mutex_lock(&data->update_lock);
968         min = TEMP_FROM_REG(data->zone[nr].limit);
969         data->zone[nr].max_desired = TEMP_TO_REG(val);
970         data->zone[nr].range = RANGE_TO_REG(
971                 val - min);
972         lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
973                 ((data->zone[nr].range & 0x0f) << 4)
974                 | (data->pwm_freq[nr] & 0x07));
975         mutex_unlock(&data->update_lock);
976         return count;
977 }
978
979 static ssize_t show_temp_auto_temp_crit(struct device *dev,
980                 struct device_attribute *attr, char *buf)
981 {
982         int nr = to_sensor_dev_attr(attr)->index;
983         struct lm85_data *data = lm85_update_device(dev);
984         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
985 }
986
987 static ssize_t set_temp_auto_temp_crit(struct device *dev,
988                 struct device_attribute *attr, const char *buf, size_t count)
989 {
990         int nr = to_sensor_dev_attr(attr)->index;
991         struct i2c_client *client = to_i2c_client(dev);
992         struct lm85_data *data = i2c_get_clientdata(client);
993         long val = simple_strtol(buf, NULL, 10);
994
995         mutex_lock(&data->update_lock);
996         data->zone[nr].critical = TEMP_TO_REG(val);
997         lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
998                 data->zone[nr].critical);
999         mutex_unlock(&data->update_lock);
1000         return count;
1001 }
1002
1003 #define temp_auto(offset)                                               \
1004 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off,                 \
1005                 S_IRUGO | S_IWUSR, show_temp_auto_temp_off,             \
1006                 set_temp_auto_temp_off, offset - 1);                    \
1007 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min,                 \
1008                 S_IRUGO | S_IWUSR, show_temp_auto_temp_min,             \
1009                 set_temp_auto_temp_min, offset - 1);                    \
1010 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max,                 \
1011                 S_IRUGO | S_IWUSR, show_temp_auto_temp_max,             \
1012                 set_temp_auto_temp_max, offset - 1);                    \
1013 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit,                \
1014                 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit,            \
1015                 set_temp_auto_temp_crit, offset - 1);
1016
1017 temp_auto(1);
1018 temp_auto(2);
1019 temp_auto(3);
1020
1021 static struct attribute *lm85_attributes[] = {
1022         &sensor_dev_attr_fan1_input.dev_attr.attr,
1023         &sensor_dev_attr_fan2_input.dev_attr.attr,
1024         &sensor_dev_attr_fan3_input.dev_attr.attr,
1025         &sensor_dev_attr_fan4_input.dev_attr.attr,
1026         &sensor_dev_attr_fan1_min.dev_attr.attr,
1027         &sensor_dev_attr_fan2_min.dev_attr.attr,
1028         &sensor_dev_attr_fan3_min.dev_attr.attr,
1029         &sensor_dev_attr_fan4_min.dev_attr.attr,
1030         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1031         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1032         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1033         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1034
1035         &sensor_dev_attr_pwm1.dev_attr.attr,
1036         &sensor_dev_attr_pwm2.dev_attr.attr,
1037         &sensor_dev_attr_pwm3.dev_attr.attr,
1038         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1039         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1040         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1041         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1042         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1043         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1044
1045         &sensor_dev_attr_in0_input.dev_attr.attr,
1046         &sensor_dev_attr_in1_input.dev_attr.attr,
1047         &sensor_dev_attr_in2_input.dev_attr.attr,
1048         &sensor_dev_attr_in3_input.dev_attr.attr,
1049         &sensor_dev_attr_in0_min.dev_attr.attr,
1050         &sensor_dev_attr_in1_min.dev_attr.attr,
1051         &sensor_dev_attr_in2_min.dev_attr.attr,
1052         &sensor_dev_attr_in3_min.dev_attr.attr,
1053         &sensor_dev_attr_in0_max.dev_attr.attr,
1054         &sensor_dev_attr_in1_max.dev_attr.attr,
1055         &sensor_dev_attr_in2_max.dev_attr.attr,
1056         &sensor_dev_attr_in3_max.dev_attr.attr,
1057         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1058         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1059         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1060         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1061
1062         &sensor_dev_attr_temp1_input.dev_attr.attr,
1063         &sensor_dev_attr_temp2_input.dev_attr.attr,
1064         &sensor_dev_attr_temp3_input.dev_attr.attr,
1065         &sensor_dev_attr_temp1_min.dev_attr.attr,
1066         &sensor_dev_attr_temp2_min.dev_attr.attr,
1067         &sensor_dev_attr_temp3_min.dev_attr.attr,
1068         &sensor_dev_attr_temp1_max.dev_attr.attr,
1069         &sensor_dev_attr_temp2_max.dev_attr.attr,
1070         &sensor_dev_attr_temp3_max.dev_attr.attr,
1071         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1072         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1073         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1074         &sensor_dev_attr_temp1_fault.dev_attr.attr,
1075         &sensor_dev_attr_temp3_fault.dev_attr.attr,
1076
1077         &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1078         &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1079         &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1080         &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1081         &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1082         &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1083         &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1084         &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1085         &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1086
1087         &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1088         &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1089         &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1090         &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1091         &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1092         &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1093         &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1094         &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1095         &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1096         &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1097         &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1098         &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1099
1100         &dev_attr_vrm.attr,
1101         &dev_attr_cpu0_vid.attr,
1102         &dev_attr_alarms.attr,
1103         NULL
1104 };
1105
1106 static const struct attribute_group lm85_group = {
1107         .attrs = lm85_attributes,
1108 };
1109
1110 static struct attribute *lm85_attributes_in4[] = {
1111         &sensor_dev_attr_in4_input.dev_attr.attr,
1112         &sensor_dev_attr_in4_min.dev_attr.attr,
1113         &sensor_dev_attr_in4_max.dev_attr.attr,
1114         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1115         NULL
1116 };
1117
1118 static const struct attribute_group lm85_group_in4 = {
1119         .attrs = lm85_attributes_in4,
1120 };
1121
1122 static struct attribute *lm85_attributes_in567[] = {
1123         &sensor_dev_attr_in5_input.dev_attr.attr,
1124         &sensor_dev_attr_in6_input.dev_attr.attr,
1125         &sensor_dev_attr_in7_input.dev_attr.attr,
1126         &sensor_dev_attr_in5_min.dev_attr.attr,
1127         &sensor_dev_attr_in6_min.dev_attr.attr,
1128         &sensor_dev_attr_in7_min.dev_attr.attr,
1129         &sensor_dev_attr_in5_max.dev_attr.attr,
1130         &sensor_dev_attr_in6_max.dev_attr.attr,
1131         &sensor_dev_attr_in7_max.dev_attr.attr,
1132         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1133         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1134         &sensor_dev_attr_in7_alarm.dev_attr.attr,
1135         NULL
1136 };
1137
1138 static const struct attribute_group lm85_group_in567 = {
1139         .attrs = lm85_attributes_in567,
1140 };
1141
1142 static void lm85_init_client(struct i2c_client *client)
1143 {
1144         int value;
1145
1146         /* Start monitoring if needed */
1147         value = lm85_read_value(client, LM85_REG_CONFIG);
1148         if (!(value & 0x01)) {
1149                 dev_info(&client->dev, "Starting monitoring\n");
1150                 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1151         }
1152
1153         /* Warn about unusual configuration bits */
1154         if (value & 0x02)
1155                 dev_warn(&client->dev, "Device configuration is locked\n");
1156         if (!(value & 0x04))
1157                 dev_warn(&client->dev, "Device is not ready\n");
1158 }
1159
1160 static int lm85_is_fake(struct i2c_client *client)
1161 {
1162         /*
1163          * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1164          * emulate the former except that it has no hardware monitoring function
1165          * so the readings are always 0.
1166          */
1167         int i;
1168         u8 in_temp, fan;
1169
1170         for (i = 0; i < 8; i++) {
1171                 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1172                 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1173                 if (in_temp != 0x00 || fan != 0xff)
1174                         return 0;
1175         }
1176
1177         return 1;
1178 }
1179
1180 /* Return 0 if detection is successful, -ENODEV otherwise */
1181 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1182 {
1183         struct i2c_adapter *adapter = client->adapter;
1184         int address = client->addr;
1185         const char *type_name;
1186         int company, verstep;
1187
1188         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1189                 /* We need to be able to do byte I/O */
1190                 return -ENODEV;
1191         }
1192
1193         /* Determine the chip type */
1194         company = lm85_read_value(client, LM85_REG_COMPANY);
1195         verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1196
1197         dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1198                 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1199                 address, company, verstep);
1200
1201         /* All supported chips have the version in common */
1202         if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1203             (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1204                 dev_dbg(&adapter->dev,
1205                         "Autodetection failed: unsupported version\n");
1206                 return -ENODEV;
1207         }
1208         type_name = "lm85";
1209
1210         /* Now, refine the detection */
1211         if (company == LM85_COMPANY_NATIONAL) {
1212                 switch (verstep) {
1213                 case LM85_VERSTEP_LM85C:
1214                         type_name = "lm85c";
1215                         break;
1216                 case LM85_VERSTEP_LM85B:
1217                         type_name = "lm85b";
1218                         break;
1219                 case LM85_VERSTEP_LM96000_1:
1220                 case LM85_VERSTEP_LM96000_2:
1221                         /* Check for Winbond WPCD377I */
1222                         if (lm85_is_fake(client)) {
1223                                 dev_dbg(&adapter->dev,
1224                                         "Found Winbond WPCD377I, ignoring\n");
1225                                 return -ENODEV;
1226                         }
1227                         break;
1228                 }
1229         } else if (company == LM85_COMPANY_ANALOG_DEV) {
1230                 switch (verstep) {
1231                 case LM85_VERSTEP_ADM1027:
1232                         type_name = "adm1027";
1233                         break;
1234                 case LM85_VERSTEP_ADT7463:
1235                 case LM85_VERSTEP_ADT7463C:
1236                         type_name = "adt7463";
1237                         break;
1238                 case LM85_VERSTEP_ADT7468_1:
1239                 case LM85_VERSTEP_ADT7468_2:
1240                         type_name = "adt7468";
1241                         break;
1242                 }
1243         } else if (company == LM85_COMPANY_SMSC) {
1244                 switch (verstep) {
1245                 case LM85_VERSTEP_EMC6D100_A0:
1246                 case LM85_VERSTEP_EMC6D100_A1:
1247                         /* Note: we can't tell a '100 from a '101 */
1248                         type_name = "emc6d100";
1249                         break;
1250                 case LM85_VERSTEP_EMC6D102:
1251                         type_name = "emc6d102";
1252                         break;
1253                 }
1254         } else {
1255                 dev_dbg(&adapter->dev,
1256                         "Autodetection failed: unknown vendor\n");
1257                 return -ENODEV;
1258         }
1259
1260         strlcpy(info->type, type_name, I2C_NAME_SIZE);
1261
1262         return 0;
1263 }
1264
1265 static int lm85_probe(struct i2c_client *client,
1266                       const struct i2c_device_id *id)
1267 {
1268         struct lm85_data *data;
1269         int err;
1270
1271         data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1272         if (!data)
1273                 return -ENOMEM;
1274
1275         i2c_set_clientdata(client, data);
1276         data->type = id->driver_data;
1277         mutex_init(&data->update_lock);
1278
1279         /* Fill in the chip specific driver values */
1280         switch (data->type) {
1281         case adm1027:
1282         case adt7463:
1283         case adt7468:
1284         case emc6d100:
1285         case emc6d102:
1286                 data->freq_map = adm1027_freq_map;
1287                 break;
1288         default:
1289                 data->freq_map = lm85_freq_map;
1290         }
1291
1292         /* Set the VRM version */
1293         data->vrm = vid_which_vrm();
1294
1295         /* Initialize the LM85 chip */
1296         lm85_init_client(client);
1297
1298         /* Register sysfs hooks */
1299         err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1300         if (err)
1301                 goto err_kfree;
1302
1303         /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1304            as a sixth digital VID input rather than an analog input. */
1305         data->vid = lm85_read_value(client, LM85_REG_VID);
1306         if (!((data->type == adt7463 || data->type == adt7468) &&
1307             (data->vid & 0x80)))
1308                 if ((err = sysfs_create_group(&client->dev.kobj,
1309                                         &lm85_group_in4)))
1310                         goto err_remove_files;
1311
1312         /* The EMC6D100 has 3 additional voltage inputs */
1313         if (data->type == emc6d100)
1314                 if ((err = sysfs_create_group(&client->dev.kobj,
1315                                         &lm85_group_in567)))
1316                         goto err_remove_files;
1317
1318         data->hwmon_dev = hwmon_device_register(&client->dev);
1319         if (IS_ERR(data->hwmon_dev)) {
1320                 err = PTR_ERR(data->hwmon_dev);
1321                 goto err_remove_files;
1322         }
1323
1324         return 0;
1325
1326         /* Error out and cleanup code */
1327  err_remove_files:
1328         sysfs_remove_group(&client->dev.kobj, &lm85_group);
1329         sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1330         if (data->type == emc6d100)
1331                 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1332  err_kfree:
1333         kfree(data);
1334         return err;
1335 }
1336
1337 static int lm85_remove(struct i2c_client *client)
1338 {
1339         struct lm85_data *data = i2c_get_clientdata(client);
1340         hwmon_device_unregister(data->hwmon_dev);
1341         sysfs_remove_group(&client->dev.kobj, &lm85_group);
1342         sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1343         if (data->type == emc6d100)
1344                 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1345         kfree(data);
1346         return 0;
1347 }
1348
1349
1350 static int lm85_read_value(struct i2c_client *client, u8 reg)
1351 {
1352         int res;
1353
1354         /* What size location is it? */
1355         switch (reg) {
1356         case LM85_REG_FAN(0):  /* Read WORD data */
1357         case LM85_REG_FAN(1):
1358         case LM85_REG_FAN(2):
1359         case LM85_REG_FAN(3):
1360         case LM85_REG_FAN_MIN(0):
1361         case LM85_REG_FAN_MIN(1):
1362         case LM85_REG_FAN_MIN(2):
1363         case LM85_REG_FAN_MIN(3):
1364         case LM85_REG_ALARM1:   /* Read both bytes at once */
1365                 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1366                 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1367                 break;
1368         default:        /* Read BYTE data */
1369                 res = i2c_smbus_read_byte_data(client, reg);
1370                 break;
1371         }
1372
1373         return res;
1374 }
1375
1376 static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
1377 {
1378         switch (reg) {
1379         case LM85_REG_FAN(0):  /* Write WORD data */
1380         case LM85_REG_FAN(1):
1381         case LM85_REG_FAN(2):
1382         case LM85_REG_FAN(3):
1383         case LM85_REG_FAN_MIN(0):
1384         case LM85_REG_FAN_MIN(1):
1385         case LM85_REG_FAN_MIN(2):
1386         case LM85_REG_FAN_MIN(3):
1387         /* NOTE: ALARM is read only, so not included here */
1388                 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1389                 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
1390                 break;
1391         default:        /* Write BYTE data */
1392                 i2c_smbus_write_byte_data(client, reg, value);
1393                 break;
1394         }
1395 }
1396
1397 static struct lm85_data *lm85_update_device(struct device *dev)
1398 {
1399         struct i2c_client *client = to_i2c_client(dev);
1400         struct lm85_data *data = i2c_get_clientdata(client);
1401         int i;
1402
1403         mutex_lock(&data->update_lock);
1404
1405         if (!data->valid ||
1406              time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
1407                 /* Things that change quickly */
1408                 dev_dbg(&client->dev, "Reading sensor values\n");
1409
1410                 /* Have to read extended bits first to "freeze" the
1411                  * more significant bits that are read later.
1412                  * There are 2 additional resolution bits per channel and we
1413                  * have room for 4, so we shift them to the left.
1414                  */
1415                 if (data->type == adm1027 || data->type == adt7463 ||
1416                     data->type == adt7468) {
1417                         int ext1 = lm85_read_value(client,
1418                                                    ADM1027_REG_EXTEND_ADC1);
1419                         int ext2 =  lm85_read_value(client,
1420                                                     ADM1027_REG_EXTEND_ADC2);
1421                         int val = (ext1 << 8) + ext2;
1422
1423                         for (i = 0; i <= 4; i++)
1424                                 data->in_ext[i] =
1425                                         ((val >> (i * 2)) & 0x03) << 2;
1426
1427                         for (i = 0; i <= 2; i++)
1428                                 data->temp_ext[i] =
1429                                         (val >> ((i + 4) * 2)) & 0x0c;
1430                 }
1431
1432                 data->vid = lm85_read_value(client, LM85_REG_VID);
1433
1434                 for (i = 0; i <= 3; ++i) {
1435                         data->in[i] =
1436                             lm85_read_value(client, LM85_REG_IN(i));
1437                         data->fan[i] =
1438                             lm85_read_value(client, LM85_REG_FAN(i));
1439                 }
1440
1441                 if (!((data->type == adt7463 || data->type == adt7468) &&
1442                     (data->vid & 0x80))) {
1443                         data->in[4] = lm85_read_value(client,
1444                                       LM85_REG_IN(4));
1445                 }
1446
1447                 if (data->type == adt7468)
1448                         data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1449
1450                 for (i = 0; i <= 2; ++i) {
1451                         data->temp[i] =
1452                             lm85_read_value(client, LM85_REG_TEMP(i));
1453                         data->pwm[i] =
1454                             lm85_read_value(client, LM85_REG_PWM(i));
1455
1456                         if (IS_ADT7468_OFF64(data))
1457                                 data->temp[i] -= 64;
1458                 }
1459
1460                 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1461
1462                 if (data->type == emc6d100) {
1463                         /* Three more voltage sensors */
1464                         for (i = 5; i <= 7; ++i) {
1465                                 data->in[i] = lm85_read_value(client,
1466                                                         EMC6D100_REG_IN(i));
1467                         }
1468                         /* More alarm bits */
1469                         data->alarms |= lm85_read_value(client,
1470                                                 EMC6D100_REG_ALARM3) << 16;
1471                 } else if (data->type == emc6d102) {
1472                         /* Have to read LSB bits after the MSB ones because
1473                            the reading of the MSB bits has frozen the
1474                            LSBs (backward from the ADM1027).
1475                          */
1476                         int ext1 = lm85_read_value(client,
1477                                                    EMC6D102_REG_EXTEND_ADC1);
1478                         int ext2 = lm85_read_value(client,
1479                                                    EMC6D102_REG_EXTEND_ADC2);
1480                         int ext3 = lm85_read_value(client,
1481                                                    EMC6D102_REG_EXTEND_ADC3);
1482                         int ext4 = lm85_read_value(client,
1483                                                    EMC6D102_REG_EXTEND_ADC4);
1484                         data->in_ext[0] = ext3 & 0x0f;
1485                         data->in_ext[1] = ext4 & 0x0f;
1486                         data->in_ext[2] = ext4 >> 4;
1487                         data->in_ext[3] = ext3 >> 4;
1488                         data->in_ext[4] = ext2 >> 4;
1489
1490                         data->temp_ext[0] = ext1 & 0x0f;
1491                         data->temp_ext[1] = ext2 & 0x0f;
1492                         data->temp_ext[2] = ext1 >> 4;
1493                 }
1494
1495                 data->last_reading = jiffies;
1496         }  /* last_reading */
1497
1498         if (!data->valid ||
1499              time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
1500                 /* Things that don't change often */
1501                 dev_dbg(&client->dev, "Reading config values\n");
1502
1503                 for (i = 0; i <= 3; ++i) {
1504                         data->in_min[i] =
1505                             lm85_read_value(client, LM85_REG_IN_MIN(i));
1506                         data->in_max[i] =
1507                             lm85_read_value(client, LM85_REG_IN_MAX(i));
1508                         data->fan_min[i] =
1509                             lm85_read_value(client, LM85_REG_FAN_MIN(i));
1510                 }
1511
1512                 if (!((data->type == adt7463 || data->type == adt7468) &&
1513                     (data->vid & 0x80))) {
1514                         data->in_min[4] = lm85_read_value(client,
1515                                           LM85_REG_IN_MIN(4));
1516                         data->in_max[4] = lm85_read_value(client,
1517                                           LM85_REG_IN_MAX(4));
1518                 }
1519
1520                 if (data->type == emc6d100) {
1521                         for (i = 5; i <= 7; ++i) {
1522                                 data->in_min[i] = lm85_read_value(client,
1523                                                 EMC6D100_REG_IN_MIN(i));
1524                                 data->in_max[i] = lm85_read_value(client,
1525                                                 EMC6D100_REG_IN_MAX(i));
1526                         }
1527                 }
1528
1529                 for (i = 0; i <= 2; ++i) {
1530                         int val;
1531
1532                         data->temp_min[i] =
1533                             lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1534                         data->temp_max[i] =
1535                             lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1536
1537                         data->autofan[i].config =
1538                             lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1539                         val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1540                         data->pwm_freq[i] = val & 0x07;
1541                         data->zone[i].range = val >> 4;
1542                         data->autofan[i].min_pwm =
1543                             lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1544                         data->zone[i].limit =
1545                             lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1546                         data->zone[i].critical =
1547                             lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1548
1549                         if (IS_ADT7468_OFF64(data)) {
1550                                 data->temp_min[i] -= 64;
1551                                 data->temp_max[i] -= 64;
1552                                 data->zone[i].limit -= 64;
1553                                 data->zone[i].critical -= 64;
1554                         }
1555                 }
1556
1557                 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1558                 data->autofan[0].min_off = (i & 0x20) != 0;
1559                 data->autofan[1].min_off = (i & 0x40) != 0;
1560                 data->autofan[2].min_off = (i & 0x80) != 0;
1561
1562                 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1563                 data->zone[0].hyst = i >> 4;
1564                 data->zone[1].hyst = i & 0x0f;
1565
1566                 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1567                 data->zone[2].hyst = i >> 4;
1568
1569                 data->last_config = jiffies;
1570         }  /* last_config */
1571
1572         data->valid = 1;
1573
1574         mutex_unlock(&data->update_lock);
1575
1576         return data;
1577 }
1578
1579
1580 static int __init sm_lm85_init(void)
1581 {
1582         return i2c_add_driver(&lm85_driver);
1583 }
1584
1585 static void __exit sm_lm85_exit(void)
1586 {
1587         i2c_del_driver(&lm85_driver);
1588 }
1589
1590 MODULE_LICENSE("GPL");
1591 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1592         "Margit Schubert-While <margitsw@t-online.de>, "
1593         "Justin Thiessen <jthiessen@penguincomputing.com>");
1594 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1595
1596 module_init(sm_lm85_init);
1597 module_exit(sm_lm85_exit);