]> Pileus Git - ~andy/linux/blob - drivers/hwmon/adm9240.c
[PATCH] i2c: Drop i2c_driver.flags, 2 of 3
[~andy/linux] / 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         .attach_adapter = adm9240_attach_adapter,
144         .detach_client  = adm9240_detach_client,
145 };
146
147 /* per client data */
148 struct adm9240_data {
149         enum chips type;
150         struct i2c_client client;
151         struct class_device *class_dev;
152         struct semaphore update_lock;
153         char valid;
154         unsigned long last_updated_measure;
155         unsigned long last_updated_config;
156
157         u8 in[6];               /* ro   in0_input */
158         u8 in_max[6];           /* rw   in0_max */
159         u8 in_min[6];           /* rw   in0_min */
160         u8 fan[2];              /* ro   fan1_input */
161         u8 fan_min[2];          /* rw   fan1_min */
162         u8 fan_div[2];          /* rw   fan1_div, read-only accessor */
163         s16 temp;               /* ro   temp1_input, 9-bit sign-extended */
164         s8 temp_max[2];         /* rw   0 -> temp_max, 1 -> temp_max_hyst */
165         u16 alarms;             /* ro   alarms */
166         u8 aout;                /* rw   aout_output */
167         u8 vid;                 /* ro   vid */
168         u8 vrm;                 /* --   vrm set on startup, no accessor */
169 };
170
171 /*** sysfs accessors ***/
172
173 /* temperature */
174 static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
175                 char *buf)
176 {
177         struct adm9240_data *data = adm9240_update_device(dev);
178         return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
179 }
180
181 static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
182                 char *buf)
183 {
184         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
185         struct adm9240_data *data = adm9240_update_device(dev);
186         return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
187 }
188
189 static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
190                 const char *buf, size_t count)
191 {
192         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
193         struct i2c_client *client = to_i2c_client(dev);
194         struct adm9240_data *data = i2c_get_clientdata(client);
195         long val = simple_strtol(buf, NULL, 10);
196
197         down(&data->update_lock);
198         data->temp_max[attr->index] = TEMP_TO_REG(val);
199         i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
200                         data->temp_max[attr->index]);
201         up(&data->update_lock);
202         return count;
203 }
204
205 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
206 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
207                 show_max, set_max, 0);
208 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
209                 show_max, set_max, 1);
210
211 /* voltage */
212 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
213                 char *buf)
214 {
215         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
216         struct adm9240_data *data = adm9240_update_device(dev);
217         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
218                                 attr->index));
219 }
220
221 static ssize_t show_in_min(struct device *dev,
222                 struct device_attribute *devattr, char *buf)
223 {
224         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
225         struct adm9240_data *data = adm9240_update_device(dev);
226         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
227                                 attr->index));
228 }
229
230 static ssize_t show_in_max(struct device *dev,
231                 struct device_attribute *devattr, char *buf)
232 {
233         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
234         struct adm9240_data *data = adm9240_update_device(dev);
235         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
236                                 attr->index));
237 }
238
239 static ssize_t set_in_min(struct device *dev,
240                 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,
257                 struct device_attribute *devattr,
258                 const char *buf, size_t count)
259 {
260         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261         struct i2c_client *client = to_i2c_client(dev);
262         struct adm9240_data *data = i2c_get_clientdata(client);
263         unsigned long val = simple_strtoul(buf, NULL, 10);
264
265         down(&data->update_lock);
266         data->in_max[attr->index] = IN_TO_REG(val, attr->index);
267         i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
268                         data->in_max[attr->index]);
269         up(&data->update_lock);
270         return count;
271 }
272
273 #define vin(nr)                                                 \
274 static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO,              \
275                 show_in, NULL, nr);                             \
276 static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR,      \
277                 show_in_min, set_in_min, nr);                   \
278 static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR,      \
279                 show_in_max, set_in_max, nr);
280
281 vin(0);
282 vin(1);
283 vin(2);
284 vin(3);
285 vin(4);
286 vin(5);
287
288 /* fans */
289 static ssize_t show_fan(struct device *dev,
290                 struct device_attribute *devattr, char *buf)
291 {
292         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
293         struct adm9240_data *data = adm9240_update_device(dev);
294         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
295                                 1 << data->fan_div[attr->index]));
296 }
297
298 static ssize_t show_fan_min(struct device *dev,
299                 struct device_attribute *devattr, char *buf)
300 {
301         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
302         struct adm9240_data *data = adm9240_update_device(dev);
303         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
304                                 1 << data->fan_div[attr->index]));
305 }
306
307 static ssize_t show_fan_div(struct device *dev,
308                 struct device_attribute *devattr, char *buf)
309 {
310         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
311         struct adm9240_data *data = adm9240_update_device(dev);
312         return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
313 }
314
315 /* write new fan div, callers must hold data->update_lock */
316 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
317                 u8 fan_div)
318 {
319         u8 reg, old, shift = (nr + 2) * 2;
320
321         reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
322         old = (reg >> shift) & 3;
323         reg &= ~(3 << shift);
324         reg |= (fan_div << shift);
325         i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
326         dev_dbg(&client->dev, "fan%d clock divider changed from %u "
327                         "to %u\n", nr + 1, 1 << old, 1 << fan_div);
328 }
329
330 /*
331  * set fan speed low limit:
332  *
333  * - value is zero: disable fan speed low limit alarm
334  *
335  * - value is below fan speed measurement range: enable fan speed low
336  *   limit alarm to be asserted while fan speed too slow to measure
337  *
338  * - otherwise: select fan clock divider to suit fan speed low limit,
339  *   measurement code may adjust registers to ensure fan speed reading
340  */
341 static ssize_t set_fan_min(struct device *dev,
342                 struct device_attribute *devattr,
343                 const char *buf, size_t count)
344 {
345         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
346         struct i2c_client *client = to_i2c_client(dev);
347         struct adm9240_data *data = i2c_get_clientdata(client);
348         unsigned long val = simple_strtoul(buf, NULL, 10);
349         int nr = attr->index;
350         u8 new_div;
351
352         down(&data->update_lock);
353
354         if (!val) {
355                 data->fan_min[nr] = 255;
356                 new_div = data->fan_div[nr];
357
358                 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
359                                 nr + 1);
360
361         } else if (val < 1350000 / (8 * 254)) {
362                 new_div = 3;
363                 data->fan_min[nr] = 254;
364
365                 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
366                                 nr + 1, FAN_FROM_REG(254, 1 << new_div));
367
368         } else {
369                 unsigned int new_min = 1350000 / val;
370
371                 new_div = 0;
372                 while (new_min > 192 && new_div < 3) {
373                         new_div++;
374                         new_min /= 2;
375                 }
376                 if (!new_min) /* keep > 0 */
377                         new_min++;
378
379                 data->fan_min[nr] = new_min;
380
381                 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
382                                 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
383         }
384
385         if (new_div != data->fan_div[nr]) {
386                 data->fan_div[nr] = new_div;
387                 adm9240_write_fan_div(client, nr, new_div);
388         }
389         i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
390                         data->fan_min[nr]);
391
392         up(&data->update_lock);
393         return count;
394 }
395
396 #define fan(nr)                                                 \
397 static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO,             \
398                 show_fan, NULL, nr - 1);                        \
399 static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO,               \
400                 show_fan_div, NULL, nr - 1);                    \
401 static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR,     \
402                 show_fan_min, set_fan_min, nr - 1);
403
404 fan(1);
405 fan(2);
406
407 /* alarms */
408 static ssize_t show_alarms(struct device *dev,
409                 struct device_attribute *attr, char *buf)
410 {
411         struct adm9240_data *data = adm9240_update_device(dev);
412         return sprintf(buf, "%u\n", data->alarms);
413 }
414 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
415
416 /* vid */
417 static ssize_t show_vid(struct device *dev,
418                 struct device_attribute *attr, char *buf)
419 {
420         struct adm9240_data *data = adm9240_update_device(dev);
421         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
422 }
423 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
424
425 /* analog output */
426 static ssize_t show_aout(struct device *dev,
427                 struct device_attribute *attr, char *buf)
428 {
429         struct adm9240_data *data = adm9240_update_device(dev);
430         return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
431 }
432
433 static ssize_t set_aout(struct device *dev,
434                 struct device_attribute *attr,
435                 const char *buf, size_t count)
436 {
437         struct i2c_client *client = to_i2c_client(dev);
438         struct adm9240_data *data = i2c_get_clientdata(client);
439         unsigned long val = simple_strtol(buf, NULL, 10);
440
441         down(&data->update_lock);
442         data->aout = AOUT_TO_REG(val);
443         i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
444         up(&data->update_lock);
445         return count;
446 }
447 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
448
449 /* chassis_clear */
450 static ssize_t chassis_clear(struct device *dev,
451                 struct device_attribute *attr,
452                 const char *buf, size_t count)
453 {
454         struct i2c_client *client = to_i2c_client(dev);
455         unsigned long val = simple_strtol(buf, NULL, 10);
456
457         if (val == 1) {
458                 i2c_smbus_write_byte_data(client,
459                                 ADM9240_REG_CHASSIS_CLEAR, 0x80);
460                 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
461         }
462         return count;
463 }
464 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
465
466
467 /*** sensor chip detect and driver install ***/
468
469 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
470 {
471         struct i2c_client *new_client;
472         struct adm9240_data *data;
473         int err = 0;
474         const char *name = "";
475         u8 man_id, die_rev;
476
477         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
478                 goto exit;
479
480         if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
481                 err = -ENOMEM;
482                 goto exit;
483         }
484
485         new_client = &data->client;
486         i2c_set_clientdata(new_client, data);
487         new_client->addr = address;
488         new_client->adapter = adapter;
489         new_client->driver = &adm9240_driver;
490         new_client->flags = 0;
491
492         if (kind == 0) {
493                 kind = adm9240;
494         }
495
496         if (kind < 0) {
497
498                 /* verify chip: reg address should match i2c address */
499                 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
500                                 != address) {
501                         dev_err(&adapter->dev, "detect fail: address match, "
502                                         "0x%02x\n", address);
503                         goto exit_free;
504                 }
505
506                 /* check known chip manufacturer */
507                 man_id = i2c_smbus_read_byte_data(new_client,
508                                 ADM9240_REG_MAN_ID);
509                 if (man_id == 0x23) {
510                         kind = adm9240;
511                 } else if (man_id == 0xda) {
512                         kind = ds1780;
513                 } else if (man_id == 0x01) {
514                         kind = lm81;
515                 } else {
516                         dev_err(&adapter->dev, "detect fail: unknown manuf, "
517                                         "0x%02x\n", man_id);
518                         goto exit_free;
519                 }
520
521                 /* successful detect, print chip info */
522                 die_rev = i2c_smbus_read_byte_data(new_client,
523                                 ADM9240_REG_DIE_REV);
524                 dev_info(&adapter->dev, "found %s revision %u\n",
525                                 man_id == 0x23 ? "ADM9240" :
526                                 man_id == 0xda ? "DS1780" : "LM81", die_rev);
527         }
528
529         /* either forced or detected chip kind */
530         if (kind == adm9240) {
531                 name = "adm9240";
532         } else if (kind == ds1780) {
533                 name = "ds1780";
534         } else if (kind == lm81) {
535                 name = "lm81";
536         }
537
538         /* fill in the remaining client fields and attach */
539         strlcpy(new_client->name, name, I2C_NAME_SIZE);
540         data->type = kind;
541         init_MUTEX(&data->update_lock);
542
543         if ((err = i2c_attach_client(new_client)))
544                 goto exit_free;
545
546         adm9240_init_client(new_client);
547
548         /* populate sysfs filesystem */
549         data->class_dev = hwmon_device_register(&new_client->dev);
550         if (IS_ERR(data->class_dev)) {
551                 err = PTR_ERR(data->class_dev);
552                 goto exit_detach;
553         }
554
555         device_create_file(&new_client->dev,
556                         &sensor_dev_attr_in0_input.dev_attr);
557         device_create_file(&new_client->dev,
558                         &sensor_dev_attr_in0_min.dev_attr);
559         device_create_file(&new_client->dev,
560                         &sensor_dev_attr_in0_max.dev_attr);
561         device_create_file(&new_client->dev,
562                         &sensor_dev_attr_in1_input.dev_attr);
563         device_create_file(&new_client->dev,
564                         &sensor_dev_attr_in1_min.dev_attr);
565         device_create_file(&new_client->dev,
566                         &sensor_dev_attr_in1_max.dev_attr);
567         device_create_file(&new_client->dev,
568                         &sensor_dev_attr_in2_input.dev_attr);
569         device_create_file(&new_client->dev,
570                         &sensor_dev_attr_in2_min.dev_attr);
571         device_create_file(&new_client->dev,
572                         &sensor_dev_attr_in2_max.dev_attr);
573         device_create_file(&new_client->dev,
574                         &sensor_dev_attr_in3_input.dev_attr);
575         device_create_file(&new_client->dev,
576                         &sensor_dev_attr_in3_min.dev_attr);
577         device_create_file(&new_client->dev,
578                         &sensor_dev_attr_in3_max.dev_attr);
579         device_create_file(&new_client->dev,
580                         &sensor_dev_attr_in4_input.dev_attr);
581         device_create_file(&new_client->dev,
582                         &sensor_dev_attr_in4_min.dev_attr);
583         device_create_file(&new_client->dev,
584                         &sensor_dev_attr_in4_max.dev_attr);
585         device_create_file(&new_client->dev,
586                         &sensor_dev_attr_in5_input.dev_attr);
587         device_create_file(&new_client->dev,
588                         &sensor_dev_attr_in5_min.dev_attr);
589         device_create_file(&new_client->dev,
590                         &sensor_dev_attr_in5_max.dev_attr);
591         device_create_file(&new_client->dev, &dev_attr_temp1_input);
592         device_create_file(&new_client->dev,
593                         &sensor_dev_attr_temp1_max.dev_attr);
594         device_create_file(&new_client->dev,
595                         &sensor_dev_attr_temp1_max_hyst.dev_attr);
596         device_create_file(&new_client->dev,
597                         &sensor_dev_attr_fan1_input.dev_attr);
598         device_create_file(&new_client->dev,
599                         &sensor_dev_attr_fan1_div.dev_attr);
600         device_create_file(&new_client->dev,
601                         &sensor_dev_attr_fan1_min.dev_attr);
602         device_create_file(&new_client->dev,
603                         &sensor_dev_attr_fan2_input.dev_attr);
604         device_create_file(&new_client->dev,
605                         &sensor_dev_attr_fan2_div.dev_attr);
606         device_create_file(&new_client->dev,
607                         &sensor_dev_attr_fan2_min.dev_attr);
608         device_create_file(&new_client->dev, &dev_attr_alarms);
609         device_create_file(&new_client->dev, &dev_attr_aout_output);
610         device_create_file(&new_client->dev, &dev_attr_chassis_clear);
611         device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
612
613         return 0;
614
615 exit_detach:
616         i2c_detach_client(new_client);
617 exit_free:
618         kfree(data);
619 exit:
620         return err;
621 }
622
623 static int adm9240_attach_adapter(struct i2c_adapter *adapter)
624 {
625         if (!(adapter->class & I2C_CLASS_HWMON))
626                 return 0;
627         return i2c_probe(adapter, &addr_data, adm9240_detect);
628 }
629
630 static int adm9240_detach_client(struct i2c_client *client)
631 {
632         struct adm9240_data *data = i2c_get_clientdata(client);
633         int err;
634
635         hwmon_device_unregister(data->class_dev);
636
637         if ((err = i2c_detach_client(client)))
638                 return err;
639
640         kfree(data);
641         return 0;
642 }
643
644 static void adm9240_init_client(struct i2c_client *client)
645 {
646         struct adm9240_data *data = i2c_get_clientdata(client);
647         u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
648         u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
649
650         data->vrm = vid_which_vrm(); /* need this to report vid as mV */
651
652         dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
653                         data->vrm % 10);
654
655         if (conf & 1) { /* measurement cycle running: report state */
656
657                 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
658                                 conf, mode);
659
660         } else { /* cold start: open limits before starting chip */
661                 int i;
662
663                 for (i = 0; i < 6; i++)
664                 {
665                         i2c_smbus_write_byte_data(client,
666                                         ADM9240_REG_IN_MIN(i), 0);
667                         i2c_smbus_write_byte_data(client,
668                                         ADM9240_REG_IN_MAX(i), 255);
669                 }
670                 i2c_smbus_write_byte_data(client,
671                                 ADM9240_REG_FAN_MIN(0), 255);
672                 i2c_smbus_write_byte_data(client,
673                                 ADM9240_REG_FAN_MIN(1), 255);
674                 i2c_smbus_write_byte_data(client,
675                                 ADM9240_REG_TEMP_MAX(0), 127);
676                 i2c_smbus_write_byte_data(client,
677                                 ADM9240_REG_TEMP_MAX(1), 127);
678
679                 /* start measurement cycle */
680                 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
681
682                 dev_info(&client->dev, "cold start: config was 0x%02x "
683                                 "mode %u\n", conf, mode);
684         }
685 }
686
687 static struct adm9240_data *adm9240_update_device(struct device *dev)
688 {
689         struct i2c_client *client = to_i2c_client(dev);
690         struct adm9240_data *data = i2c_get_clientdata(client);
691         int i;
692
693         down(&data->update_lock);
694
695         /* minimum measurement cycle: 1.75 seconds */
696         if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
697                         || !data->valid) {
698
699                 for (i = 0; i < 6; i++) /* read voltages */
700                 {
701                         data->in[i] = i2c_smbus_read_byte_data(client,
702                                         ADM9240_REG_IN(i));
703                 }
704                 data->alarms = i2c_smbus_read_byte_data(client,
705                                         ADM9240_REG_INT(0)) |
706                                         i2c_smbus_read_byte_data(client,
707                                         ADM9240_REG_INT(1)) << 8;
708
709                 /* read temperature: assume temperature changes less than
710                  * 0.5'C per two measurement cycles thus ignore possible
711                  * but unlikely aliasing error on lsb reading. --Grant */
712                 data->temp = ((i2c_smbus_read_byte_data(client,
713                                         ADM9240_REG_TEMP) << 8) |
714                                         i2c_smbus_read_byte_data(client,
715                                         ADM9240_REG_TEMP_CONF)) / 128;
716
717                 for (i = 0; i < 2; i++) /* read fans */
718                 {
719                         data->fan[i] = i2c_smbus_read_byte_data(client,
720                                         ADM9240_REG_FAN(i));
721
722                         /* adjust fan clock divider on overflow */
723                         if (data->valid && data->fan[i] == 255 &&
724                                         data->fan_div[i] < 3) {
725
726                                 adm9240_write_fan_div(client, i,
727                                                 ++data->fan_div[i]);
728
729                                 /* adjust fan_min if active, but not to 0 */
730                                 if (data->fan_min[i] < 255 &&
731                                                 data->fan_min[i] >= 2)
732                                         data->fan_min[i] /= 2;
733                         }
734                 }
735                 data->last_updated_measure = jiffies;
736         }
737
738         /* minimum config reading cycle: 300 seconds */
739         if (time_after(jiffies, data->last_updated_config + (HZ * 300))
740                         || !data->valid) {
741
742                 for (i = 0; i < 6; i++)
743                 {
744                         data->in_min[i] = i2c_smbus_read_byte_data(client,
745                                         ADM9240_REG_IN_MIN(i));
746                         data->in_max[i] = i2c_smbus_read_byte_data(client,
747                                         ADM9240_REG_IN_MAX(i));
748                 }
749                 for (i = 0; i < 2; i++)
750                 {
751                         data->fan_min[i] = i2c_smbus_read_byte_data(client,
752                                         ADM9240_REG_FAN_MIN(i));
753                 }
754                 data->temp_max[0] = i2c_smbus_read_byte_data(client,
755                                 ADM9240_REG_TEMP_MAX(0));
756                 data->temp_max[1] = i2c_smbus_read_byte_data(client,
757                                 ADM9240_REG_TEMP_MAX(1));
758
759                 /* read fan divs and 5-bit VID */
760                 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
761                 data->fan_div[0] = (i >> 4) & 3;
762                 data->fan_div[1] = (i >> 6) & 3;
763                 data->vid = i & 0x0f;
764                 data->vid |= (i2c_smbus_read_byte_data(client,
765                                         ADM9240_REG_VID4) & 1) << 4;
766                 /* read analog out */
767                 data->aout = i2c_smbus_read_byte_data(client,
768                                 ADM9240_REG_ANALOG_OUT);
769
770                 data->last_updated_config = jiffies;
771                 data->valid = 1;
772         }
773         up(&data->update_lock);
774         return data;
775 }
776
777 static int __init sensors_adm9240_init(void)
778 {
779         return i2c_add_driver(&adm9240_driver);
780 }
781
782 static void __exit sensors_adm9240_exit(void)
783 {
784         i2c_del_driver(&adm9240_driver);
785 }
786
787 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
788                 "Grant Coady <gcoady@gmail.com> and others");
789 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
790 MODULE_LICENSE("GPL");
791
792 module_init(sensors_adm9240_init);
793 module_exit(sensors_adm9240_exit);
794