[PATCH] hwmon: adm9240 driver update - dynamic sysfs
[powerpc.git] / drivers / hwmon / adm9240.c
1 /*
2  * adm9240.c    Part of lm_sensors, Linux kernel modules for hardware
3  *              monitoring
4  *
5  * Copyright (C) 1999   Frodo Looijaard <frodol@dds.nl>
6  *                      Philip Edelbrock <phil@netroedge.com>
7  * Copyright (C) 2003   Michiel Rook <michiel@grendelproject.nl>
8  * Copyright (C) 2005   Grant Coady <gcoady@gmail.com> with valuable
9  *                              guidance from Jean Delvare
10  *
11  * Driver supports      Analog Devices          ADM9240
12  *                      Dallas Semiconductor    DS1780
13  *                      National Semiconductor  LM81
14  *
15  * ADM9240 is the reference, DS1780 and LM81 are register compatibles
16  *
17  * Voltage      Six inputs are scaled by chip, VID also reported
18  * Temperature  Chip temperature to 0.5'C, maximum and max_hysteris
19  * Fans         2 fans, low speed alarm, automatic fan clock divider
20  * Alarms       16-bit map of active alarms
21  * Analog Out   0..1250 mV output
22  *
23  * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
24  *
25  * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
26  *
27  * LM81 extended temp reading not implemented
28  *
29  * This program is free software; you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation; either version 2 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42  */
43
44 #include <linux/init.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/hwmon-sysfs.h>
49 #include <linux/hwmon.h>
50 #include <linux/hwmon-vid.h>
51 #include <linux/err.h>
52
53 /* Addresses to scan */
54 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
55                                         I2C_CLIENT_END };
56
57 /* Insmod parameters */
58 I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
59
60 /* ADM9240 registers */
61 #define ADM9240_REG_MAN_ID              0x3e
62 #define ADM9240_REG_DIE_REV             0x3f
63 #define ADM9240_REG_CONFIG              0x40
64
65 #define ADM9240_REG_IN(nr)              (0x20 + (nr))   /* 0..5 */
66 #define ADM9240_REG_IN_MAX(nr)          (0x2b + (nr) * 2)
67 #define ADM9240_REG_IN_MIN(nr)          (0x2c + (nr) * 2)
68 #define ADM9240_REG_FAN(nr)             (0x28 + (nr))   /* 0..1 */
69 #define ADM9240_REG_FAN_MIN(nr)         (0x3b + (nr))
70 #define ADM9240_REG_INT(nr)             (0x41 + (nr))
71 #define ADM9240_REG_INT_MASK(nr)        (0x43 + (nr))
72 #define ADM9240_REG_TEMP                0x27
73 #define ADM9240_REG_TEMP_MAX(nr)        (0x39 + (nr)) /* 0, 1 = high, hyst */
74 #define ADM9240_REG_ANALOG_OUT          0x19
75 #define ADM9240_REG_CHASSIS_CLEAR       0x46
76 #define ADM9240_REG_VID_FAN_DIV         0x47
77 #define ADM9240_REG_I2C_ADDR            0x48
78 #define ADM9240_REG_VID4                0x49
79 #define ADM9240_REG_TEMP_CONF           0x4b
80
81 /* generalised scaling with integer rounding */
82 static inline int SCALE(long val, int mul, int div)
83 {
84         if (val < 0)
85                 return (val * mul - div / 2) / div;
86         else
87                 return (val * mul + div / 2) / div;
88 }
89
90 /* adm9240 internally scales voltage measurements */
91 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
92
93 static inline unsigned int IN_FROM_REG(u8 reg, int n)
94 {
95         return SCALE(reg, nom_mv[n], 192);
96 }
97
98 static inline u8 IN_TO_REG(unsigned long val, int n)
99 {
100         return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
101 }
102
103 /* temperature range: -40..125, 127 disables temperature alarm */
104 static inline s8 TEMP_TO_REG(long val)
105 {
106         return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
107 }
108
109 /* two fans, each with low fan speed limit */
110 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
111 {
112         if (!reg) /* error */
113                 return -1;
114
115         if (reg == 255)
116                 return 0;
117
118         return SCALE(1350000, 1, reg * div);
119 }
120
121 /* analog out 0..1250mV */
122 static inline u8 AOUT_TO_REG(unsigned long val)
123 {
124         return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
125 }
126
127 static inline unsigned int AOUT_FROM_REG(u8 reg)
128 {
129         return SCALE(reg, 1250, 255);
130 }
131
132 static int adm9240_attach_adapter(struct i2c_adapter *adapter);
133 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
134 static void adm9240_init_client(struct i2c_client *client);
135 static int adm9240_detach_client(struct i2c_client *client);
136 static struct adm9240_data *adm9240_update_device(struct device *dev);
137
138 /* driver data */
139 static struct i2c_driver adm9240_driver = {
140         .owner          = THIS_MODULE,
141         .name           = "adm9240",
142         .id             = I2C_DRIVERID_ADM9240,
143         .flags          = I2C_DF_NOTIFY,
144         .attach_adapter = adm9240_attach_adapter,
145         .detach_client  = adm9240_detach_client,
146 };
147
148 /* per client data */
149 struct adm9240_data {
150         enum chips type;
151         struct i2c_client client;
152         struct class_device *class_dev;
153         struct semaphore update_lock;
154         char valid;
155         unsigned long last_updated_measure;
156         unsigned long last_updated_config;
157
158         u8 in[6];               /* ro   in0_input */
159         u8 in_max[6];           /* rw   in0_max */
160         u8 in_min[6];           /* rw   in0_min */
161         u8 fan[2];              /* ro   fan1_input */
162         u8 fan_min[2];          /* rw   fan1_min */
163         u8 fan_div[2];          /* rw   fan1_div, read-only accessor */
164         s16 temp;               /* ro   temp1_input, 9-bit sign-extended */
165         s8 temp_max[2];         /* rw   0 -> temp_max, 1 -> temp_max_hyst */
166         u16 alarms;             /* ro   alarms */
167         u8 aout;                /* rw   aout_output */
168         u8 vid;                 /* ro   vid */
169         u8 vrm;                 /* --   vrm set on startup, no accessor */
170 };
171
172 /*** sysfs accessors ***/
173
174 /* temperature */
175 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
176                 char *buf)
177 {
178         struct adm9240_data *data = adm9240_update_device(dev);
179         return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
180 }
181
182 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
183                 char *buf)
184 {
185         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
186         struct adm9240_data *data = adm9240_update_device(dev);
187         return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
188 }
189
190 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
191                 const char *buf, size_t count)
192 {
193         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194         struct i2c_client *client = to_i2c_client(dev);
195         struct adm9240_data *data = i2c_get_clientdata(client);
196         long val = simple_strtol(buf, NULL, 10);
197
198         down(&data->update_lock);
199         data->temp_max[attr->index] = TEMP_TO_REG(val);
200         i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
201                         data->temp_max[attr->index]);
202         up(&data->update_lock);
203         return count;
204 }
205
206 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
207 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
208                 show_max, set_max, 0);
209 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
210                 show_max, set_max, 1);
211
212 /* voltage */
213 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
214                 char *buf)
215 {
216         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
217         struct adm9240_data *data = adm9240_update_device(dev);
218         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
219                                 attr->index));
220 }
221
222 static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr,
223                 char *buf)
224 {
225         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
226         struct adm9240_data *data = adm9240_update_device(dev);
227         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
228                                 attr->index));
229 }
230
231 static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr,
232                 char *buf)
233 {
234         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
235         struct adm9240_data *data = adm9240_update_device(dev);
236         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
237                                 attr->index));
238 }
239
240 static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr,
241                 const char *buf, size_t count)
242 {
243         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
244         struct i2c_client *client = to_i2c_client(dev);
245         struct adm9240_data *data = i2c_get_clientdata(client);
246         unsigned long val = simple_strtoul(buf, NULL, 10);
247
248         down(&data->update_lock);
249         data->in_min[attr->index] = IN_TO_REG(val, attr->index);
250         i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
251                         data->in_min[attr->index]);
252         up(&data->update_lock);
253         return count;
254 }
255
256 static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr,
257                 const char *buf, size_t count)
258 {
259         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
260         struct i2c_client *client = to_i2c_client(dev);
261         struct adm9240_data *data = i2c_get_clientdata(client);
262         unsigned long val = simple_strtoul(buf, NULL, 10);
263
264         down(&data->update_lock);
265         data->in_max[attr->index] = IN_TO_REG(val, attr->index);
266         i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
267                         data->in_max[attr->index]);
268         up(&data->update_lock);
269         return count;
270 }
271
272 #define vin(nr)                                                 \
273 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO,              \
274                 show_in, NULL, nr);                             \
275 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR,      \
276                 show_in_min, set_in_min, nr);                   \
277 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR,      \
278                 show_in_max, set_in_max, nr);
279
280 vin(0);
281 vin(1);
282 vin(2);
283 vin(3);
284 vin(4);
285 vin(5);
286
287 /* fans */
288 static ssize_t show_fan(struct device *dev,
289                 struct device_attribute *devattr, char *buf)
290 {
291         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
292         struct adm9240_data *data = adm9240_update_device(dev);
293         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
294                                 1 << data->fan_div[attr->index]));
295 }
296
297 static ssize_t show_fan_min(struct device *dev,
298                 struct device_attribute *devattr, char *buf)
299 {
300         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
301         struct adm9240_data *data = adm9240_update_device(dev);
302         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
303                                 1 << data->fan_div[attr->index]));
304 }
305
306 static ssize_t show_fan_div(struct device *dev,
307                 struct device_attribute *devattr, char *buf)
308 {
309         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
310         struct adm9240_data *data = adm9240_update_device(dev);
311         return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
312 }
313
314 /* write new fan div, callers must hold data->update_lock */
315 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
316                 u8 fan_div)
317 {
318         u8 reg, old, shift = (nr + 2) * 2;
319
320         reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
321         old = (reg >> shift) & 3;
322         reg &= ~(3 << shift);
323         reg |= (fan_div << shift);
324         i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
325         dev_dbg(&client->dev, "fan%d clock divider changed from %u "
326                         "to %u\n", nr + 1, 1 << old, 1 << fan_div);
327 }
328
329 /* 
330  * set fan speed low limit:
331  *
332  * - value is zero: disable fan speed low limit alarm
333  *
334  * - value is below fan speed measurement range: enable fan speed low
335  *   limit alarm to be asserted while fan speed too slow to measure
336  *
337  * - otherwise: select fan clock divider to suit fan speed low limit,
338  *   measurement code may adjust registers to ensure fan speed reading
339  */
340 static ssize_t set_fan_min(struct device *dev,
341                 struct device_attribute *devattr,
342                 const char *buf, size_t count)
343 {
344         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
345         struct i2c_client *client = to_i2c_client(dev);
346         struct adm9240_data *data = i2c_get_clientdata(client);
347         unsigned long val = simple_strtoul(buf, NULL, 10);
348         int nr = attr->index;
349         u8 new_div;
350
351         down(&data->update_lock);
352
353         if (!val) {
354                 data->fan_min[nr] = 255;
355                 new_div = data->fan_div[nr];
356
357                 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
358                                 nr + 1);
359
360         } else if (val < 1350000 / (8 * 254)) {
361                 new_div = 3;
362                 data->fan_min[nr] = 254;
363
364                 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
365                                 nr + 1, FAN_FROM_REG(254, 1 << new_div));
366
367         } else {
368                 unsigned int new_min = 1350000 / val;
369
370                 new_div = 0;
371                 while (new_min > 192 && new_div < 3) {
372                         new_div++;
373                         new_min /= 2;
374                 }
375                 if (!new_min) /* keep > 0 */
376                         new_min++;
377
378                 data->fan_min[nr] = new_min;
379
380                 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
381                                 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
382         }
383
384         if (new_div != data->fan_div[nr]) {
385                 data->fan_div[nr] = new_div;
386                 adm9240_write_fan_div(client, nr, new_div);
387         }
388         i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
389                         data->fan_min[nr]);
390
391         up(&data->update_lock);
392         return count;
393 }
394
395 #define fan(nr)                                                 \
396 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO,             \
397                 show_fan, NULL, nr - 1);                        \
398 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO,               \
399                 show_fan_div, NULL, nr - 1);                    \
400 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR,     \
401                 show_fan_min, set_fan_min, nr - 1);
402
403 fan(1);
404 fan(2);
405
406 /* alarms */
407 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
408 {
409         struct adm9240_data *data = adm9240_update_device(dev);
410         return sprintf(buf, "%u\n", data->alarms);
411 }
412 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
413
414 /* vid */
415 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
416 {
417         struct adm9240_data *data = adm9240_update_device(dev);
418         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
419 }
420 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
421
422 /* analog output */
423 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
424 {
425         struct adm9240_data *data = adm9240_update_device(dev);
426         return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
427 }
428
429 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
430 {
431         struct i2c_client *client = to_i2c_client(dev);
432         struct adm9240_data *data = i2c_get_clientdata(client);
433         unsigned long val = simple_strtol(buf, NULL, 10);
434
435         down(&data->update_lock);
436         data->aout = AOUT_TO_REG(val);
437         i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
438         up(&data->update_lock);
439         return count;
440 }
441 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
442
443 /* chassis_clear */
444 static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
445 {
446         struct i2c_client *client = to_i2c_client(dev);
447         unsigned long val = simple_strtol(buf, NULL, 10);
448
449         if (val == 1) {
450                 i2c_smbus_write_byte_data(client,
451                                 ADM9240_REG_CHASSIS_CLEAR, 0x80);
452                 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
453         }
454         return count;
455 }
456 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
457
458
459 /*** sensor chip detect and driver install ***/
460
461 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
462 {
463         struct i2c_client *new_client;
464         struct adm9240_data *data;
465         int err = 0;
466         const char *name = "";
467         u8 man_id, die_rev;
468
469         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
470                 goto exit;
471
472         if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
473                 err = -ENOMEM;
474                 goto exit;
475         }
476
477         new_client = &data->client;
478         i2c_set_clientdata(new_client, data);
479         new_client->addr = address;
480         new_client->adapter = adapter;
481         new_client->driver = &adm9240_driver;
482         new_client->flags = 0;
483
484         if (kind == 0) {
485                 kind = adm9240;
486         }
487
488         if (kind < 0) {
489
490                 /* verify chip: reg address should match i2c address */
491                 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
492                                 != address) {
493                         dev_err(&adapter->dev, "detect fail: address match, "
494                                         "0x%02x\n", address);
495                         goto exit_free;
496                 }
497
498                 /* check known chip manufacturer */
499                 man_id = i2c_smbus_read_byte_data(new_client,
500                                 ADM9240_REG_MAN_ID);
501                 if (man_id == 0x23) {
502                         kind = adm9240;
503                 } else if (man_id == 0xda) {
504                         kind = ds1780;
505                 } else if (man_id == 0x01) {
506                         kind = lm81;
507                 } else {
508                         dev_err(&adapter->dev, "detect fail: unknown manuf, "
509                                         "0x%02x\n", man_id);
510                         goto exit_free;
511                 }
512
513                 /* successful detect, print chip info */
514                 die_rev = i2c_smbus_read_byte_data(new_client,
515                                 ADM9240_REG_DIE_REV);
516                 dev_info(&adapter->dev, "found %s revision %u\n",
517                                 man_id == 0x23 ? "ADM9240" :
518                                 man_id == 0xda ? "DS1780" : "LM81", die_rev);
519         }
520
521         /* either forced or detected chip kind */
522         if (kind == adm9240) {
523                 name = "adm9240";
524         } else if (kind == ds1780) {
525                 name = "ds1780";
526         } else if (kind == lm81) {
527                 name = "lm81";
528         }
529
530         /* fill in the remaining client fields and attach */
531         strlcpy(new_client->name, name, I2C_NAME_SIZE);
532         data->type = kind;
533         init_MUTEX(&data->update_lock);
534
535         if ((err = i2c_attach_client(new_client)))
536                 goto exit_free;
537
538         adm9240_init_client(new_client);
539
540         /* populate sysfs filesystem */
541         data->class_dev = hwmon_device_register(&new_client->dev);
542         if (IS_ERR(data->class_dev)) {
543                 err = PTR_ERR(data->class_dev);
544                 goto exit_detach;
545         }
546
547         device_create_file(&new_client->dev,
548                         &sensor_dev_attr_in0_input.dev_attr);
549         device_create_file(&new_client->dev,
550                         &sensor_dev_attr_in0_min.dev_attr);
551         device_create_file(&new_client->dev,
552                         &sensor_dev_attr_in0_max.dev_attr);
553         device_create_file(&new_client->dev,
554                         &sensor_dev_attr_in1_input.dev_attr);
555         device_create_file(&new_client->dev,
556                         &sensor_dev_attr_in1_min.dev_attr);
557         device_create_file(&new_client->dev,
558                         &sensor_dev_attr_in1_max.dev_attr);
559         device_create_file(&new_client->dev,
560                         &sensor_dev_attr_in2_input.dev_attr);
561         device_create_file(&new_client->dev,
562                         &sensor_dev_attr_in2_min.dev_attr);
563         device_create_file(&new_client->dev,
564                         &sensor_dev_attr_in2_max.dev_attr);
565         device_create_file(&new_client->dev,
566                         &sensor_dev_attr_in3_input.dev_attr);
567         device_create_file(&new_client->dev,
568                         &sensor_dev_attr_in3_min.dev_attr);
569         device_create_file(&new_client->dev,
570                         &sensor_dev_attr_in3_max.dev_attr);
571         device_create_file(&new_client->dev,
572                         &sensor_dev_attr_in4_input.dev_attr);
573         device_create_file(&new_client->dev,
574                         &sensor_dev_attr_in4_min.dev_attr);
575         device_create_file(&new_client->dev,
576                         &sensor_dev_attr_in4_max.dev_attr);
577         device_create_file(&new_client->dev,
578                         &sensor_dev_attr_in5_input.dev_attr);
579         device_create_file(&new_client->dev,
580                         &sensor_dev_attr_in5_min.dev_attr);
581         device_create_file(&new_client->dev,
582                         &sensor_dev_attr_in5_max.dev_attr);
583         device_create_file(&new_client->dev, &dev_attr_temp1_input);
584         device_create_file(&new_client->dev,
585                         &sensor_dev_attr_temp1_max.dev_attr);
586         device_create_file(&new_client->dev,
587                         &sensor_dev_attr_temp1_max_hyst.dev_attr);
588         device_create_file(&new_client->dev,
589                         &sensor_dev_attr_fan1_input.dev_attr);
590         device_create_file(&new_client->dev,
591                         &sensor_dev_attr_fan1_div.dev_attr);
592         device_create_file(&new_client->dev,
593                         &sensor_dev_attr_fan1_min.dev_attr);
594         device_create_file(&new_client->dev,
595                         &sensor_dev_attr_fan2_input.dev_attr);
596         device_create_file(&new_client->dev,
597                         &sensor_dev_attr_fan2_div.dev_attr);
598         device_create_file(&new_client->dev,
599                         &sensor_dev_attr_fan2_min.dev_attr);
600         device_create_file(&new_client->dev, &dev_attr_alarms);
601         device_create_file(&new_client->dev, &dev_attr_aout_output);
602         device_create_file(&new_client->dev, &dev_attr_chassis_clear);
603         device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
604
605         return 0;
606
607 exit_detach:
608         i2c_detach_client(new_client);
609 exit_free:
610         kfree(data);
611 exit:
612         return err;
613 }
614
615 static int adm9240_attach_adapter(struct i2c_adapter *adapter)
616 {
617         if (!(adapter->class & I2C_CLASS_HWMON))
618                 return 0;
619         return i2c_probe(adapter, &addr_data, adm9240_detect);
620 }
621
622 static int adm9240_detach_client(struct i2c_client *client)
623 {
624         struct adm9240_data *data = i2c_get_clientdata(client);
625         int err;
626
627         hwmon_device_unregister(data->class_dev);
628
629         if ((err = i2c_detach_client(client)))
630                 return err;
631
632         kfree(data);
633         return 0;
634 }
635
636 static void adm9240_init_client(struct i2c_client *client)
637 {
638         struct adm9240_data *data = i2c_get_clientdata(client);
639         u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
640         u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
641
642         data->vrm = vid_which_vrm(); /* need this to report vid as mV */
643
644         dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
645                         data->vrm % 10);
646
647         if (conf & 1) { /* measurement cycle running: report state */
648
649                 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
650                                 conf, mode);
651
652         } else { /* cold start: open limits before starting chip */
653                 int i;
654
655                 for (i = 0; i < 6; i++)
656                 {
657                         i2c_smbus_write_byte_data(client,
658                                         ADM9240_REG_IN_MIN(i), 0);
659                         i2c_smbus_write_byte_data(client,
660                                         ADM9240_REG_IN_MAX(i), 255);
661                 }
662                 i2c_smbus_write_byte_data(client,
663                                 ADM9240_REG_FAN_MIN(0), 255);
664                 i2c_smbus_write_byte_data(client,
665                                 ADM9240_REG_FAN_MIN(1), 255);
666                 i2c_smbus_write_byte_data(client,
667                                 ADM9240_REG_TEMP_MAX(0), 127);
668                 i2c_smbus_write_byte_data(client,
669                                 ADM9240_REG_TEMP_MAX(1), 127);
670
671                 /* start measurement cycle */
672                 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
673
674                 dev_info(&client->dev, "cold start: config was 0x%02x "
675                                 "mode %u\n", conf, mode);
676         }
677 }
678
679 static struct adm9240_data *adm9240_update_device(struct device *dev)
680 {
681         struct i2c_client *client = to_i2c_client(dev);
682         struct adm9240_data *data = i2c_get_clientdata(client);
683         int i;
684
685         down(&data->update_lock);
686
687         /* minimum measurement cycle: 1.75 seconds */
688         if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
689                         || !data->valid) {
690
691                 for (i = 0; i < 6; i++) /* read voltages */
692                 {
693                         data->in[i] = i2c_smbus_read_byte_data(client,
694                                         ADM9240_REG_IN(i));
695                 }
696                 data->alarms = i2c_smbus_read_byte_data(client,
697                                         ADM9240_REG_INT(0)) |
698                                         i2c_smbus_read_byte_data(client,
699                                         ADM9240_REG_INT(1)) << 8;
700
701                 /* read temperature: assume temperature changes less than
702                  * 0.5'C per two measurement cycles thus ignore possible
703                  * but unlikely aliasing error on lsb reading. --Grant */
704                 data->temp = ((i2c_smbus_read_byte_data(client,
705                                         ADM9240_REG_TEMP) << 8) |
706                                         i2c_smbus_read_byte_data(client,
707                                         ADM9240_REG_TEMP_CONF)) / 128;
708
709                 for (i = 0; i < 2; i++) /* read fans */
710                 {
711                         data->fan[i] = i2c_smbus_read_byte_data(client,
712                                         ADM9240_REG_FAN(i));
713
714                         /* adjust fan clock divider on overflow */
715                         if (data->valid && data->fan[i] == 255 &&
716                                         data->fan_div[i] < 3) {
717
718                                 adm9240_write_fan_div(client, i,
719                                                 ++data->fan_div[i]);
720
721                                 /* adjust fan_min if active, but not to 0 */
722                                 if (data->fan_min[i] < 255 &&
723                                                 data->fan_min[i] >= 2)
724                                         data->fan_min[i] /= 2;
725                         }
726                 }
727                 data->last_updated_measure = jiffies;
728         }
729
730         /* minimum config reading cycle: 300 seconds */
731         if (time_after(jiffies, data->last_updated_config + (HZ * 300))
732                         || !data->valid) {
733
734                 for (i = 0; i < 6; i++)
735                 {
736                         data->in_min[i] = i2c_smbus_read_byte_data(client,
737                                         ADM9240_REG_IN_MIN(i));
738                         data->in_max[i] = i2c_smbus_read_byte_data(client,
739                                         ADM9240_REG_IN_MAX(i));
740                 }
741                 for (i = 0; i < 2; i++)
742                 {
743                         data->fan_min[i] = i2c_smbus_read_byte_data(client,
744                                         ADM9240_REG_FAN_MIN(i));
745                 }
746                 data->temp_max[0] = i2c_smbus_read_byte_data(client,
747                                 ADM9240_REG_TEMP_MAX(0));
748                 data->temp_max[1] = i2c_smbus_read_byte_data(client,
749                                 ADM9240_REG_TEMP_MAX(1));
750
751                 /* read fan divs and 5-bit VID */
752                 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
753                 data->fan_div[0] = (i >> 4) & 3;
754                 data->fan_div[1] = (i >> 6) & 3;
755                 data->vid = i & 0x0f;
756                 data->vid |= (i2c_smbus_read_byte_data(client,
757                                         ADM9240_REG_VID4) & 1) << 4;
758                 /* read analog out */
759                 data->aout = i2c_smbus_read_byte_data(client,
760                                 ADM9240_REG_ANALOG_OUT);
761
762                 data->last_updated_config = jiffies;
763                 data->valid = 1;
764         }
765         up(&data->update_lock);
766         return data;
767 }
768
769 static int __init sensors_adm9240_init(void)
770 {
771         return i2c_add_driver(&adm9240_driver);
772 }
773
774 static void __exit sensors_adm9240_exit(void)
775 {
776         i2c_del_driver(&adm9240_driver);
777 }
778
779 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
780                 "Grant Coady <gcoady@gmail.com> and others");
781 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
782 MODULE_LICENSE("GPL");
783
784 module_init(sensors_adm9240_init);
785 module_exit(sensors_adm9240_exit);
786