hwmon: (adm1031) Various cleanups
[safe/jmp/linux-2.6] / drivers / hwmon / adm1031.c
1 /*
2   adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3   monitoring
4   Based on lm75.c and lm85.c
5   Supports adm1030 / adm1031
6   Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7   Reworked by Jean Delvare <khali@linux-fr.org>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
32
33 /* Following macros takes channel parameter starting from 0 to 2 */
34 #define ADM1031_REG_FAN_SPEED(nr)       (0x08 + (nr))
35 #define ADM1031_REG_FAN_DIV(nr)         (0x20 + (nr))
36 #define ADM1031_REG_PWM                 (0x22)
37 #define ADM1031_REG_FAN_MIN(nr)         (0x10 + (nr))
38
39 #define ADM1031_REG_TEMP_MAX(nr)        (0x14 + 4 * (nr))
40 #define ADM1031_REG_TEMP_MIN(nr)        (0x15 + 4 * (nr))
41 #define ADM1031_REG_TEMP_CRIT(nr)       (0x16 + 4 * (nr))
42
43 #define ADM1031_REG_TEMP(nr)            (0x0a + (nr))
44 #define ADM1031_REG_AUTO_TEMP(nr)       (0x24 + (nr))
45
46 #define ADM1031_REG_STATUS(nr)          (0x2 + (nr))
47
48 #define ADM1031_REG_CONF1               0x00
49 #define ADM1031_REG_CONF2               0x01
50 #define ADM1031_REG_EXT_TEMP            0x06
51
52 #define ADM1031_CONF1_MONITOR_ENABLE    0x01    /* Monitoring enable */
53 #define ADM1031_CONF1_PWM_INVERT        0x08    /* PWM Invert */
54 #define ADM1031_CONF1_AUTO_MODE         0x80    /* Auto FAN */
55
56 #define ADM1031_CONF2_PWM1_ENABLE       0x01
57 #define ADM1031_CONF2_PWM2_ENABLE       0x02
58 #define ADM1031_CONF2_TACH1_ENABLE      0x04
59 #define ADM1031_CONF2_TACH2_ENABLE      0x08
60 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
61
62 /* Addresses to scan */
63 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
64
65 /* Insmod parameters */
66 I2C_CLIENT_INSMOD_2(adm1030, adm1031);
67
68 typedef u8 auto_chan_table_t[8][2];
69
70 /* Each client has this additional data */
71 struct adm1031_data {
72         struct i2c_client client;
73         struct device *hwmon_dev;
74         struct mutex update_lock;
75         int chip_type;
76         char valid;             /* !=0 if following fields are valid */
77         unsigned long last_updated;     /* In jiffies */
78         /* The chan_select_table contains the possible configurations for
79          * auto fan control.
80          */
81         const auto_chan_table_t *chan_select_table;
82         u16 alarm;
83         u8 conf1;
84         u8 conf2;
85         u8 fan[2];
86         u8 fan_div[2];
87         u8 fan_min[2];
88         u8 pwm[2];
89         u8 old_pwm[2];
90         s8 temp[3];
91         u8 ext_temp[3];
92         u8 auto_temp[3];
93         u8 auto_temp_min[3];
94         u8 auto_temp_off[3];
95         u8 auto_temp_max[3];
96         s8 temp_min[3];
97         s8 temp_max[3];
98         s8 temp_crit[3];
99 };
100
101 static int adm1031_attach_adapter(struct i2c_adapter *adapter);
102 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
103 static void adm1031_init_client(struct i2c_client *client);
104 static int adm1031_detach_client(struct i2c_client *client);
105 static struct adm1031_data *adm1031_update_device(struct device *dev);
106
107 /* This is the driver that will be inserted */
108 static struct i2c_driver adm1031_driver = {
109         .driver = {
110                 .name = "adm1031",
111         },
112         .attach_adapter = adm1031_attach_adapter,
113         .detach_client = adm1031_detach_client,
114 };
115
116 static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
117 {
118         return i2c_smbus_read_byte_data(client, reg);
119 }
120
121 static inline int
122 adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
123 {
124         return i2c_smbus_write_byte_data(client, reg, value);
125 }
126
127
128 #define TEMP_TO_REG(val)                (((val) < 0 ? ((val - 500) / 1000) : \
129                                         ((val + 500) / 1000)))
130
131 #define TEMP_FROM_REG(val)              ((val) * 1000)
132
133 #define TEMP_FROM_REG_EXT(val, ext)     (TEMP_FROM_REG(val) + (ext) * 125)
134
135 #define FAN_FROM_REG(reg, div)          ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
136
137 static int FAN_TO_REG(int reg, int div)
138 {
139         int tmp;
140         tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
141         return tmp > 255 ? 255 : tmp;
142 }
143
144 #define FAN_DIV_FROM_REG(reg)           (1<<(((reg)&0xc0)>>6))
145
146 #define PWM_TO_REG(val)                 (SENSORS_LIMIT((val), 0, 255) >> 4)
147 #define PWM_FROM_REG(val)               ((val) << 4)
148
149 #define FAN_CHAN_FROM_REG(reg)          (((reg) >> 5) & 7)
150 #define FAN_CHAN_TO_REG(val, reg)       \
151         (((reg) & 0x1F) | (((val) << 5) & 0xe0))
152
153 #define AUTO_TEMP_MIN_TO_REG(val, reg)  \
154         ((((val)/500) & 0xf8)|((reg) & 0x7))
155 #define AUTO_TEMP_RANGE_FROM_REG(reg)   (5000 * (1<< ((reg)&0x7)))
156 #define AUTO_TEMP_MIN_FROM_REG(reg)     (1000 * ((((reg) >> 3) & 0x1f) << 2))
157
158 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
159
160 #define AUTO_TEMP_OFF_FROM_REG(reg)             \
161         (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
162
163 #define AUTO_TEMP_MAX_FROM_REG(reg)             \
164         (AUTO_TEMP_RANGE_FROM_REG(reg) +        \
165         AUTO_TEMP_MIN_FROM_REG(reg))
166
167 static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
168 {
169         int ret;
170         int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
171
172         range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
173         ret = ((reg & 0xf8) |
174                (range < 10000 ? 0 :
175                 range < 20000 ? 1 :
176                 range < 40000 ? 2 : range < 80000 ? 3 : 4));
177         return ret;
178 }
179
180 /* FAN auto control */
181 #define GET_FAN_AUTO_BITFIELD(data, idx)        \
182         (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
183
184 /* The tables below contains the possible values for the auto fan
185  * control bitfields. the index in the table is the register value.
186  * MSb is the auto fan control enable bit, so the four first entries
187  * in the table disables auto fan control when both bitfields are zero.
188  */
189 static const auto_chan_table_t auto_channel_select_table_adm1031 = {
190         { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
191         { 2 /* 0b010 */ , 4 /* 0b100 */ },
192         { 2 /* 0b010 */ , 2 /* 0b010 */ },
193         { 4 /* 0b100 */ , 4 /* 0b100 */ },
194         { 7 /* 0b111 */ , 7 /* 0b111 */ },
195 };
196
197 static const auto_chan_table_t auto_channel_select_table_adm1030 = {
198         { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
199         { 2 /* 0b10 */          , 0 },
200         { 0xff /* invalid */    , 0 },
201         { 0xff /* invalid */    , 0 },
202         { 3 /* 0b11 */          , 0 },
203 };
204
205 /* That function checks if a bitfield is valid and returns the other bitfield
206  * nearest match if no exact match where found.
207  */
208 static int
209 get_fan_auto_nearest(struct adm1031_data *data,
210                      int chan, u8 val, u8 reg, u8 * new_reg)
211 {
212         int i;
213         int first_match = -1, exact_match = -1;
214         u8 other_reg_val =
215             (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
216
217         if (val == 0) {
218                 *new_reg = 0;
219                 return 0;
220         }
221
222         for (i = 0; i < 8; i++) {
223                 if ((val == (*data->chan_select_table)[i][chan]) &&
224                     ((*data->chan_select_table)[i][chan ? 0 : 1] ==
225                      other_reg_val)) {
226                         /* We found an exact match */
227                         exact_match = i;
228                         break;
229                 } else if (val == (*data->chan_select_table)[i][chan] &&
230                            first_match == -1) {
231                         /* Save the first match in case of an exact match has
232                          * not been found
233                          */
234                         first_match = i;
235                 }
236         }
237
238         if (exact_match >= 0) {
239                 *new_reg = exact_match;
240         } else if (first_match >= 0) {
241                 *new_reg = first_match;
242         } else {
243                 return -EINVAL;
244         }
245         return 0;
246 }
247
248 static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
249 {
250         struct adm1031_data *data = adm1031_update_device(dev);
251         return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
252 }
253
254 static ssize_t
255 set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
256 {
257         struct i2c_client *client = to_i2c_client(dev);
258         struct adm1031_data *data = i2c_get_clientdata(client);
259         int val = simple_strtol(buf, NULL, 10);
260         u8 reg;
261         int ret;
262         u8 old_fan_mode;
263
264         old_fan_mode = data->conf1;
265
266         mutex_lock(&data->update_lock);
267
268         if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
269                 mutex_unlock(&data->update_lock);
270                 return ret;
271         }
272         data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
273         if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
274             (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
275                 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
276                         /* Switch to Auto Fan Mode
277                          * Save PWM registers
278                          * Set PWM registers to 33% Both */
279                         data->old_pwm[0] = data->pwm[0];
280                         data->old_pwm[1] = data->pwm[1];
281                         adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
282                 } else {
283                         /* Switch to Manual Mode */
284                         data->pwm[0] = data->old_pwm[0];
285                         data->pwm[1] = data->old_pwm[1];
286                         /* Restore PWM registers */
287                         adm1031_write_value(client, ADM1031_REG_PWM,
288                                             data->pwm[0] | (data->pwm[1] << 4));
289                 }
290         }
291         data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
292         adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
293         mutex_unlock(&data->update_lock);
294         return count;
295 }
296
297 #define fan_auto_channel_offset(offset)                                         \
298 static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf)    \
299 {                                                                               \
300         return show_fan_auto_channel(dev, buf, offset - 1);                     \
301 }                                                                               \
302 static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr,                \
303         const char *buf, size_t count)                                          \
304 {                                                                               \
305         return set_fan_auto_channel(dev, buf, count, offset - 1);               \
306 }                                                                               \
307 static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR,               \
308                    show_fan_auto_channel_##offset,                              \
309                    set_fan_auto_channel_##offset)
310
311 fan_auto_channel_offset(1);
312 fan_auto_channel_offset(2);
313
314 /* Auto Temps */
315 static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
316 {
317         struct adm1031_data *data = adm1031_update_device(dev);
318         return sprintf(buf, "%d\n",
319                        AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
320 }
321 static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
322 {
323         struct adm1031_data *data = adm1031_update_device(dev);
324         return sprintf(buf, "%d\n",
325                        AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
326 }
327 static ssize_t
328 set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
329 {
330         struct i2c_client *client = to_i2c_client(dev);
331         struct adm1031_data *data = i2c_get_clientdata(client);
332         int val = simple_strtol(buf, NULL, 10);
333
334         mutex_lock(&data->update_lock);
335         data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
336         adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
337                             data->auto_temp[nr]);
338         mutex_unlock(&data->update_lock);
339         return count;
340 }
341 static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
342 {
343         struct adm1031_data *data = adm1031_update_device(dev);
344         return sprintf(buf, "%d\n",
345                        AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
346 }
347 static ssize_t
348 set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
349 {
350         struct i2c_client *client = to_i2c_client(dev);
351         struct adm1031_data *data = i2c_get_clientdata(client);
352         int val = simple_strtol(buf, NULL, 10);
353
354         mutex_lock(&data->update_lock);
355         data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
356         adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
357                             data->temp_max[nr]);
358         mutex_unlock(&data->update_lock);
359         return count;
360 }
361
362 #define auto_temp_reg(offset)                                                   \
363 static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf)     \
364 {                                                                               \
365         return show_auto_temp_off(dev, buf, offset - 1);                        \
366 }                                                                               \
367 static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)     \
368 {                                                                               \
369         return show_auto_temp_min(dev, buf, offset - 1);                        \
370 }                                                                               \
371 static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)     \
372 {                                                                               \
373         return show_auto_temp_max(dev, buf, offset - 1);                        \
374 }                                                                               \
375 static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr,         \
376                                              const char *buf, size_t count)     \
377 {                                                                               \
378         return set_auto_temp_min(dev, buf, count, offset - 1);          \
379 }                                                                               \
380 static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr,         \
381                                              const char *buf, size_t count)     \
382 {                                                                               \
383         return set_auto_temp_max(dev, buf, count, offset - 1);          \
384 }                                                                               \
385 static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,                            \
386                    show_auto_temp_##offset##_off, NULL);                        \
387 static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,                  \
388                    show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
389 static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,                  \
390                    show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
391
392 auto_temp_reg(1);
393 auto_temp_reg(2);
394 auto_temp_reg(3);
395
396 /* pwm */
397 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
398 {
399         struct adm1031_data *data = adm1031_update_device(dev);
400         return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
401 }
402 static ssize_t
403 set_pwm(struct device *dev, const char *buf, size_t count, int nr)
404 {
405         struct i2c_client *client = to_i2c_client(dev);
406         struct adm1031_data *data = i2c_get_clientdata(client);
407         int val = simple_strtol(buf, NULL, 10);
408         int reg;
409
410         mutex_lock(&data->update_lock);
411         if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
412             (((val>>4) & 0xf) != 5)) {
413                 /* In automatic mode, the only PWM accepted is 33% */
414                 mutex_unlock(&data->update_lock);
415                 return -EINVAL;
416         }
417         data->pwm[nr] = PWM_TO_REG(val);
418         reg = adm1031_read_value(client, ADM1031_REG_PWM);
419         adm1031_write_value(client, ADM1031_REG_PWM,
420                             nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
421                             : (data->pwm[nr] & 0xf) | (reg & 0xf0));
422         mutex_unlock(&data->update_lock);
423         return count;
424 }
425
426 #define pwm_reg(offset)                                                 \
427 static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
428 {                                                                       \
429         return show_pwm(dev, buf, offset - 1);                  \
430 }                                                                       \
431 static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr,                     \
432                                  const char *buf, size_t count)         \
433 {                                                                       \
434         return set_pwm(dev, buf, count, offset - 1);            \
435 }                                                                       \
436 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,                      \
437                    show_pwm_##offset, set_pwm_##offset)
438
439 pwm_reg(1);
440 pwm_reg(2);
441
442 /* Fans */
443
444 /*
445  * That function checks the cases where the fan reading is not
446  * relevant.  It is used to provide 0 as fan reading when the fan is
447  * not supposed to run
448  */
449 static int trust_fan_readings(struct adm1031_data *data, int chan)
450 {
451         int res = 0;
452
453         if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
454                 switch (data->conf1 & 0x60) {
455                 case 0x00:      /* remote temp1 controls fan1 remote temp2 controls fan2 */
456                         res = data->temp[chan+1] >=
457                               AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
458                         break;
459                 case 0x20:      /* remote temp1 controls both fans */
460                         res =
461                             data->temp[1] >=
462                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
463                         break;
464                 case 0x40:      /* remote temp2 controls both fans */
465                         res =
466                             data->temp[2] >=
467                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
468                         break;
469                 case 0x60:      /* max controls both fans */
470                         res =
471                             data->temp[0] >=
472                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
473                             || data->temp[1] >=
474                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
475                             || (data->chip_type == adm1031
476                                 && data->temp[2] >=
477                                 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
478                         break;
479                 }
480         } else {
481                 res = data->pwm[chan] > 0;
482         }
483         return res;
484 }
485
486
487 static ssize_t show_fan(struct device *dev, char *buf, int nr)
488 {
489         struct adm1031_data *data = adm1031_update_device(dev);
490         int value;
491
492         value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
493                                  FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
494         return sprintf(buf, "%d\n", value);
495 }
496
497 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
498 {
499         struct adm1031_data *data = adm1031_update_device(dev);
500         return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
501 }
502 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
503 {
504         struct adm1031_data *data = adm1031_update_device(dev);
505         return sprintf(buf, "%d\n",
506                        FAN_FROM_REG(data->fan_min[nr],
507                                     FAN_DIV_FROM_REG(data->fan_div[nr])));
508 }
509 static ssize_t
510 set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
511 {
512         struct i2c_client *client = to_i2c_client(dev);
513         struct adm1031_data *data = i2c_get_clientdata(client);
514         int val = simple_strtol(buf, NULL, 10);
515
516         mutex_lock(&data->update_lock);
517         if (val) {
518                 data->fan_min[nr] =
519                         FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
520         } else {
521                 data->fan_min[nr] = 0xff;
522         }
523         adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
524         mutex_unlock(&data->update_lock);
525         return count;
526 }
527 static ssize_t
528 set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
529 {
530         struct i2c_client *client = to_i2c_client(dev);
531         struct adm1031_data *data = i2c_get_clientdata(client);
532         int val = simple_strtol(buf, NULL, 10);
533         u8 tmp;
534         int old_div;
535         int new_min;
536
537         tmp = val == 8 ? 0xc0 :
538               val == 4 ? 0x80 :
539               val == 2 ? 0x40 :
540               val == 1 ? 0x00 :
541               0xff;
542         if (tmp == 0xff)
543                 return -EINVAL;
544
545         mutex_lock(&data->update_lock);
546         /* Get fresh readings */
547         data->fan_div[nr] = adm1031_read_value(client,
548                                                ADM1031_REG_FAN_DIV(nr));
549         data->fan_min[nr] = adm1031_read_value(client,
550                                                ADM1031_REG_FAN_MIN(nr));
551
552         /* Write the new clock divider and fan min */
553         old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
554         data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
555         new_min = data->fan_min[nr] * old_div / val;
556         data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
557
558         adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
559                             data->fan_div[nr]);
560         adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
561                             data->fan_min[nr]);
562
563         /* Invalidate the cache: fan speed is no longer valid */
564         data->valid = 0;
565         mutex_unlock(&data->update_lock);
566         return count;
567 }
568
569 #define fan_offset(offset)                                              \
570 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
571 {                                                                       \
572         return show_fan(dev, buf, offset - 1);                  \
573 }                                                                       \
574 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)   \
575 {                                                                       \
576         return show_fan_min(dev, buf, offset - 1);                      \
577 }                                                                       \
578 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf)   \
579 {                                                                       \
580         return show_fan_div(dev, buf, offset - 1);                      \
581 }                                                                       \
582 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr,               \
583         const char *buf, size_t count)                                  \
584 {                                                                       \
585         return set_fan_min(dev, buf, count, offset - 1);                \
586 }                                                                       \
587 static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr,               \
588         const char *buf, size_t count)                                  \
589 {                                                                       \
590         return set_fan_div(dev, buf, count, offset - 1);                \
591 }                                                                       \
592 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset,     \
593                    NULL);                                               \
594 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
595                    show_fan_##offset##_min, set_fan_##offset##_min);    \
596 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,                \
597                    show_fan_##offset##_div, set_fan_##offset##_div);    \
598 static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR,       \
599                    show_pwm_##offset, set_pwm_##offset)
600
601 fan_offset(1);
602 fan_offset(2);
603
604
605 /* Temps */
606 static ssize_t show_temp(struct device *dev, char *buf, int nr)
607 {
608         struct adm1031_data *data = adm1031_update_device(dev);
609         int ext;
610         ext = nr == 0 ?
611             ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
612             (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
613         return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
614 }
615 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
616 {
617         struct adm1031_data *data = adm1031_update_device(dev);
618         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
619 }
620 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
621 {
622         struct adm1031_data *data = adm1031_update_device(dev);
623         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
624 }
625 static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
626 {
627         struct adm1031_data *data = adm1031_update_device(dev);
628         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
629 }
630 static ssize_t
631 set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
632 {
633         struct i2c_client *client = to_i2c_client(dev);
634         struct adm1031_data *data = i2c_get_clientdata(client);
635         int val;
636
637         val = simple_strtol(buf, NULL, 10);
638         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
639         mutex_lock(&data->update_lock);
640         data->temp_min[nr] = TEMP_TO_REG(val);
641         adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
642                             data->temp_min[nr]);
643         mutex_unlock(&data->update_lock);
644         return count;
645 }
646 static ssize_t
647 set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
648 {
649         struct i2c_client *client = to_i2c_client(dev);
650         struct adm1031_data *data = i2c_get_clientdata(client);
651         int val;
652
653         val = simple_strtol(buf, NULL, 10);
654         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
655         mutex_lock(&data->update_lock);
656         data->temp_max[nr] = TEMP_TO_REG(val);
657         adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
658                             data->temp_max[nr]);
659         mutex_unlock(&data->update_lock);
660         return count;
661 }
662 static ssize_t
663 set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
664 {
665         struct i2c_client *client = to_i2c_client(dev);
666         struct adm1031_data *data = i2c_get_clientdata(client);
667         int val;
668
669         val = simple_strtol(buf, NULL, 10);
670         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
671         mutex_lock(&data->update_lock);
672         data->temp_crit[nr] = TEMP_TO_REG(val);
673         adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
674                             data->temp_crit[nr]);
675         mutex_unlock(&data->update_lock);
676         return count;
677 }
678
679 #define temp_reg(offset)                                                        \
680 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf)                \
681 {                                                                               \
682         return show_temp(dev, buf, offset - 1);                         \
683 }                                                                               \
684 static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)          \
685 {                                                                               \
686         return show_temp_min(dev, buf, offset - 1);                             \
687 }                                                                               \
688 static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)          \
689 {                                                                               \
690         return show_temp_max(dev, buf, offset - 1);                             \
691 }                                                                               \
692 static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
693 {                                                                               \
694         return show_temp_crit(dev, buf, offset - 1);                    \
695 }                                                                               \
696 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr,                      \
697                                         const char *buf, size_t count)          \
698 {                                                                               \
699         return set_temp_min(dev, buf, count, offset - 1);                       \
700 }                                                                               \
701 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr,                      \
702                                         const char *buf, size_t count)          \
703 {                                                                               \
704         return set_temp_max(dev, buf, count, offset - 1);                       \
705 }                                                                               \
706 static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr,                     \
707                                          const char *buf, size_t count)         \
708 {                                                                               \
709         return set_temp_crit(dev, buf, count, offset - 1);                      \
710 }                                                                               \
711 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset,           \
712                    NULL);                                                       \
713 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,                       \
714                    show_temp_##offset##_min, set_temp_##offset##_min);          \
715 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,                       \
716                    show_temp_##offset##_max, set_temp_##offset##_max);          \
717 static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,                      \
718                    show_temp_##offset##_crit, set_temp_##offset##_crit)
719
720 temp_reg(1);
721 temp_reg(2);
722 temp_reg(3);
723
724 /* Alarms */
725 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
726 {
727         struct adm1031_data *data = adm1031_update_device(dev);
728         return sprintf(buf, "%d\n", data->alarm);
729 }
730
731 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
732
733
734 static int adm1031_attach_adapter(struct i2c_adapter *adapter)
735 {
736         if (!(adapter->class & I2C_CLASS_HWMON))
737                 return 0;
738         return i2c_probe(adapter, &addr_data, adm1031_detect);
739 }
740
741 static struct attribute *adm1031_attributes[] = {
742         &dev_attr_fan1_input.attr,
743         &dev_attr_fan1_div.attr,
744         &dev_attr_fan1_min.attr,
745         &dev_attr_pwm1.attr,
746         &dev_attr_auto_fan1_channel.attr,
747         &dev_attr_temp1_input.attr,
748         &dev_attr_temp1_min.attr,
749         &dev_attr_temp1_max.attr,
750         &dev_attr_temp1_crit.attr,
751         &dev_attr_temp2_input.attr,
752         &dev_attr_temp2_min.attr,
753         &dev_attr_temp2_max.attr,
754         &dev_attr_temp2_crit.attr,
755
756         &dev_attr_auto_temp1_off.attr,
757         &dev_attr_auto_temp1_min.attr,
758         &dev_attr_auto_temp1_max.attr,
759
760         &dev_attr_auto_temp2_off.attr,
761         &dev_attr_auto_temp2_min.attr,
762         &dev_attr_auto_temp2_max.attr,
763
764         &dev_attr_auto_fan1_min_pwm.attr,
765
766         &dev_attr_alarms.attr,
767
768         NULL
769 };
770
771 static const struct attribute_group adm1031_group = {
772         .attrs = adm1031_attributes,
773 };
774
775 static struct attribute *adm1031_attributes_opt[] = {
776         &dev_attr_fan2_input.attr,
777         &dev_attr_fan2_div.attr,
778         &dev_attr_fan2_min.attr,
779         &dev_attr_pwm2.attr,
780         &dev_attr_auto_fan2_channel.attr,
781         &dev_attr_temp3_input.attr,
782         &dev_attr_temp3_min.attr,
783         &dev_attr_temp3_max.attr,
784         &dev_attr_temp3_crit.attr,
785         &dev_attr_auto_temp3_off.attr,
786         &dev_attr_auto_temp3_min.attr,
787         &dev_attr_auto_temp3_max.attr,
788         &dev_attr_auto_fan2_min_pwm.attr,
789         NULL
790 };
791
792 static const struct attribute_group adm1031_group_opt = {
793         .attrs = adm1031_attributes_opt,
794 };
795
796 /* This function is called by i2c_probe */
797 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
798 {
799         struct i2c_client *client;
800         struct adm1031_data *data;
801         int err = 0;
802         const char *name = "";
803
804         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
805                 goto exit;
806
807         if (!(data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
808                 err = -ENOMEM;
809                 goto exit;
810         }
811
812         client = &data->client;
813         i2c_set_clientdata(client, data);
814         client->addr = address;
815         client->adapter = adapter;
816         client->driver = &adm1031_driver;
817
818         if (kind < 0) {
819                 int id, co;
820                 id = i2c_smbus_read_byte_data(client, 0x3d);
821                 co = i2c_smbus_read_byte_data(client, 0x3e);
822
823                 if (!((id == 0x31 || id == 0x30) && co == 0x41))
824                         goto exit_free;
825                 kind = (id == 0x30) ? adm1030 : adm1031;
826         }
827
828         if (kind <= 0)
829                 kind = adm1031;
830
831         /* Given the detected chip type, set the chip name and the
832          * auto fan control helper table. */
833         if (kind == adm1030) {
834                 name = "adm1030";
835                 data->chan_select_table = &auto_channel_select_table_adm1030;
836         } else if (kind == adm1031) {
837                 name = "adm1031";
838                 data->chan_select_table = &auto_channel_select_table_adm1031;
839         }
840         data->chip_type = kind;
841
842         strlcpy(client->name, name, I2C_NAME_SIZE);
843         mutex_init(&data->update_lock);
844
845         /* Tell the I2C layer a new client has arrived */
846         if ((err = i2c_attach_client(client)))
847                 goto exit_free;
848
849         /* Initialize the ADM1031 chip */
850         adm1031_init_client(client);
851
852         /* Register sysfs hooks */
853         if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
854                 goto exit_detach;
855
856         if (kind == adm1031) {
857                 if ((err = sysfs_create_group(&client->dev.kobj,
858                                                 &adm1031_group_opt)))
859                         goto exit_remove;
860         }
861
862         data->hwmon_dev = hwmon_device_register(&client->dev);
863         if (IS_ERR(data->hwmon_dev)) {
864                 err = PTR_ERR(data->hwmon_dev);
865                 goto exit_remove;
866         }
867
868         return 0;
869
870 exit_remove:
871         sysfs_remove_group(&client->dev.kobj, &adm1031_group);
872         sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
873 exit_detach:
874         i2c_detach_client(client);
875 exit_free:
876         kfree(data);
877 exit:
878         return err;
879 }
880
881 static int adm1031_detach_client(struct i2c_client *client)
882 {
883         struct adm1031_data *data = i2c_get_clientdata(client);
884         int ret;
885
886         hwmon_device_unregister(data->hwmon_dev);
887         sysfs_remove_group(&client->dev.kobj, &adm1031_group);
888         sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
889         if ((ret = i2c_detach_client(client)) != 0) {
890                 return ret;
891         }
892         kfree(data);
893         return 0;
894 }
895
896 static void adm1031_init_client(struct i2c_client *client)
897 {
898         unsigned int read_val;
899         unsigned int mask;
900         struct adm1031_data *data = i2c_get_clientdata(client);
901
902         mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
903         if (data->chip_type == adm1031) {
904                 mask |= (ADM1031_CONF2_PWM2_ENABLE |
905                         ADM1031_CONF2_TACH2_ENABLE);
906         }
907         /* Initialize the ADM1031 chip (enables fan speed reading ) */
908         read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
909         if ((read_val | mask) != read_val) {
910             adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
911         }
912
913         read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
914         if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
915             adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
916                                 ADM1031_CONF1_MONITOR_ENABLE);
917         }
918
919 }
920
921 static struct adm1031_data *adm1031_update_device(struct device *dev)
922 {
923         struct i2c_client *client = to_i2c_client(dev);
924         struct adm1031_data *data = i2c_get_clientdata(client);
925         int chan;
926
927         mutex_lock(&data->update_lock);
928
929         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
930             || !data->valid) {
931
932                 dev_dbg(&client->dev, "Starting adm1031 update\n");
933                 for (chan = 0;
934                      chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
935                         u8 oldh, newh;
936
937                         oldh =
938                             adm1031_read_value(client, ADM1031_REG_TEMP(chan));
939                         data->ext_temp[chan] =
940                             adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
941                         newh =
942                             adm1031_read_value(client, ADM1031_REG_TEMP(chan));
943                         if (newh != oldh) {
944                                 data->ext_temp[chan] =
945                                     adm1031_read_value(client,
946                                                        ADM1031_REG_EXT_TEMP);
947 #ifdef DEBUG
948                                 oldh =
949                                     adm1031_read_value(client,
950                                                        ADM1031_REG_TEMP(chan));
951
952                                 /* oldh is actually newer */
953                                 if (newh != oldh)
954                                         dev_warn(&client->dev,
955                                                  "Remote temperature may be "
956                                                  "wrong.\n");
957 #endif
958                         }
959                         data->temp[chan] = newh;
960
961                         data->temp_min[chan] =
962                             adm1031_read_value(client,
963                                                ADM1031_REG_TEMP_MIN(chan));
964                         data->temp_max[chan] =
965                             adm1031_read_value(client,
966                                                ADM1031_REG_TEMP_MAX(chan));
967                         data->temp_crit[chan] =
968                             adm1031_read_value(client,
969                                                ADM1031_REG_TEMP_CRIT(chan));
970                         data->auto_temp[chan] =
971                             adm1031_read_value(client,
972                                                ADM1031_REG_AUTO_TEMP(chan));
973
974                 }
975
976                 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
977                 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
978
979                 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
980                              | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
981                                 << 8);
982                 if (data->chip_type == adm1030) {
983                         data->alarm &= 0xc0ff;
984                 }
985
986                 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
987                         data->fan_div[chan] =
988                             adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
989                         data->fan_min[chan] =
990                             adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
991                         data->fan[chan] =
992                             adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
993                         data->pwm[chan] =
994                             0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
995                                    (4*chan));
996                 }
997                 data->last_updated = jiffies;
998                 data->valid = 1;
999         }
1000
1001         mutex_unlock(&data->update_lock);
1002
1003         return data;
1004 }
1005
1006 static int __init sensors_adm1031_init(void)
1007 {
1008         return i2c_add_driver(&adm1031_driver);
1009 }
1010
1011 static void __exit sensors_adm1031_exit(void)
1012 {
1013         i2c_del_driver(&adm1031_driver);
1014 }
1015
1016 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1017 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1018 MODULE_LICENSE("GPL");
1019
1020 module_init(sensors_adm1031_init);
1021 module_exit(sensors_adm1031_exit);