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