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