vserver 1.9.5.x5
[linux-2.6.git] / drivers / i2c / chips / adm1025.c
1 /*
2  * adm1025.c
3  *
4  * Copyright (C) 2000       Chen-Yuan Wu <gwu@esoft.com>
5  * Copyright (C) 2003-2004  Jean Delvare <khali@linux-fr.org>
6  *
7  * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
8  * voltages (including its own power source) and up to two temperatures
9  * (its own plus up to one external one). Voltages are scaled internally
10  * (which is not the common way) with ratios such that the nominal value
11  * of each voltage correspond to a register value of 192 (which means a
12  * resolution of about 0.5% of the nominal value). Temperature values are
13  * reported with a 1 deg resolution and a 3 deg accuracy. Complete
14  * datasheet can be obtained from Analog's website at:
15  *   http://www.analog.com/Analog_Root/productPage/productHome/0,2121,ADM1025,00.html
16  *
17  * This driver also supports the ADM1025A, which differs from the ADM1025
18  * only in that it has "open-drain VID inputs while the ADM1025 has
19  * on-chip 100k pull-ups on the VID inputs". It doesn't make any
20  * difference for us.
21  *
22  * This driver also supports the NE1619, a sensor chip made by Philips.
23  * That chip is similar to the ADM1025A, with a few differences. The only
24  * difference that matters to us is that the NE1619 has only two possible
25  * addresses while the ADM1025A has a third one. Complete datasheet can be
26  * obtained from Philips's website at:
27  *   http://www.semiconductors.philips.com/pip/NE1619DS.html
28  *
29  * Since the ADM1025 was the first chipset supported by this driver, most
30  * comments will refer to this chipset, but are actually general and
31  * concern all supported chipsets, unless mentioned otherwise.
32  *
33  * This program is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2 of the License, or
36  * (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License
44  * along with this program; if not, write to the Free Software
45  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46  */
47
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/i2c.h>
53 #include <linux/i2c-sensor.h>
54 #include <linux/i2c-vid.h>
55
56 /*
57  * Addresses to scan
58  * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
59  * NE1619 has two possible addresses: 0x2c and 0x2d.
60  */
61
62 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
63 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
64
65 /*
66  * Insmod parameters
67  */
68
69 SENSORS_INSMOD_2(adm1025, ne1619);
70
71 /*
72  * The ADM1025 registers
73  */
74
75 #define ADM1025_REG_MAN_ID              0x3E
76 #define ADM1025_REG_CHIP_ID             0x3F
77 #define ADM1025_REG_CONFIG              0x40
78 #define ADM1025_REG_STATUS1             0x41
79 #define ADM1025_REG_STATUS2             0x42
80 #define ADM1025_REG_IN(nr)              (0x20 + (nr))
81 #define ADM1025_REG_IN_MAX(nr)          (0x2B + (nr) * 2)
82 #define ADM1025_REG_IN_MIN(nr)          (0x2C + (nr) * 2)
83 #define ADM1025_REG_TEMP(nr)            (0x26 + (nr))
84 #define ADM1025_REG_TEMP_HIGH(nr)       (0x37 + (nr) * 2)
85 #define ADM1025_REG_TEMP_LOW(nr)        (0x38 + (nr) * 2)
86 #define ADM1025_REG_VID                 0x47
87 #define ADM1025_REG_VID4                0x49
88
89 /*
90  * Conversions and various macros
91  * The ADM1025 uses signed 8-bit values for temperatures.
92  */
93
94 static int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
95
96 #define IN_FROM_REG(reg,scale)  (((reg) * (scale) + 96) / 192)
97 #define IN_TO_REG(val,scale)    ((val) <= 0 ? 0 : \
98                                  (val) * 192 >= (scale) * 255 ? 255 : \
99                                  ((val) * 192 + (scale)/2) / (scale))
100
101 #define TEMP_FROM_REG(reg)      ((reg) * 1000)
102 #define TEMP_TO_REG(val)        ((val) <= -127500 ? -128 : \
103                                  (val) >= 126500 ? 127 : \
104                                  (((val) < 0 ? (val)-500 : (val)+500) / 1000))
105
106 /*
107  * Functions declaration
108  */
109
110 static int adm1025_attach_adapter(struct i2c_adapter *adapter);
111 static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind);
112 static void adm1025_init_client(struct i2c_client *client);
113 static int adm1025_detach_client(struct i2c_client *client);
114 static struct adm1025_data *adm1025_update_device(struct device *dev);
115
116 /*
117  * Driver data (common to all clients)
118  */
119
120 static struct i2c_driver adm1025_driver = {
121         .owner          = THIS_MODULE,
122         .name           = "adm1025",
123         .id             = I2C_DRIVERID_ADM1025,
124         .flags          = I2C_DF_NOTIFY,
125         .attach_adapter = adm1025_attach_adapter,
126         .detach_client  = adm1025_detach_client,
127 };
128
129 /*
130  * Client data (each client gets its own)
131  */
132
133 struct adm1025_data {
134         struct i2c_client client;
135         struct semaphore update_lock;
136         char valid; /* zero until following fields are valid */
137         unsigned long last_updated; /* in jiffies */
138
139         u8 in[6];               /* register value */
140         u8 in_max[6];           /* register value */
141         u8 in_min[6];           /* register value */
142         s8 temp[2];             /* register value */
143         s8 temp_min[2];         /* register value */
144         s8 temp_max[2];         /* register value */
145         u16 alarms;             /* register values, combined */
146         u8 vid;                 /* register values, combined */
147         u8 vrm;
148 };
149
150 /*
151  * Internal variables
152  */
153
154 static int adm1025_id;
155
156 /*
157  * Sysfs stuff
158  */
159
160 #define show_in(offset) \
161 static ssize_t show_in##offset(struct device *dev, char *buf) \
162 { \
163         struct adm1025_data *data = adm1025_update_device(dev); \
164         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
165                        in_scale[offset])); \
166 } \
167 static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
168 { \
169         struct adm1025_data *data = adm1025_update_device(dev); \
170         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
171                        in_scale[offset])); \
172 } \
173 static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
174 { \
175         struct adm1025_data *data = adm1025_update_device(dev); \
176         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
177                        in_scale[offset])); \
178 } \
179 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
180 show_in(0);
181 show_in(1);
182 show_in(2);
183 show_in(3);
184 show_in(4);
185 show_in(5);
186
187 #define show_temp(offset) \
188 static ssize_t show_temp##offset(struct device *dev, char *buf) \
189 { \
190         struct adm1025_data *data = adm1025_update_device(dev); \
191         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
192 } \
193 static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
194 { \
195         struct adm1025_data *data = adm1025_update_device(dev); \
196         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
197 } \
198 static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
199 { \
200         struct adm1025_data *data = adm1025_update_device(dev); \
201         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
202 }\
203 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp##offset, NULL);
204 show_temp(1);
205 show_temp(2);
206
207 #define set_in(offset) \
208 static ssize_t set_in##offset##_min(struct device *dev, const char *buf, \
209         size_t count) \
210 { \
211         struct i2c_client *client = to_i2c_client(dev); \
212         struct adm1025_data *data = i2c_get_clientdata(client); \
213         long val = simple_strtol(buf, NULL, 10); \
214         data->in_min[offset] = IN_TO_REG(val, in_scale[offset]); \
215         i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(offset), \
216                                   data->in_min[offset]); \
217         return count; \
218 } \
219 static ssize_t set_in##offset##_max(struct device *dev, const char *buf, \
220         size_t count) \
221 { \
222         struct i2c_client *client = to_i2c_client(dev); \
223         struct adm1025_data *data = i2c_get_clientdata(client); \
224         long val = simple_strtol(buf, NULL, 10); \
225         data->in_max[offset] = IN_TO_REG(val, in_scale[offset]); \
226         i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(offset), \
227                                   data->in_max[offset]); \
228         return count; \
229 } \
230 static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
231         show_in##offset##_min, set_in##offset##_min); \
232 static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
233         show_in##offset##_max, set_in##offset##_max);
234 set_in(0);
235 set_in(1);
236 set_in(2);
237 set_in(3);
238 set_in(4);
239 set_in(5);
240
241 #define set_temp(offset) \
242 static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
243         size_t count) \
244 { \
245         struct i2c_client *client = to_i2c_client(dev); \
246         struct adm1025_data *data = i2c_get_clientdata(client); \
247         long val = simple_strtol(buf, NULL, 10); \
248         data->temp_min[offset-1] = TEMP_TO_REG(val); \
249         i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(offset-1), \
250                                   data->temp_min[offset-1]); \
251         return count; \
252 } \
253 static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
254         size_t count) \
255 { \
256         struct i2c_client *client = to_i2c_client(dev); \
257         struct adm1025_data *data = i2c_get_clientdata(client); \
258         long val = simple_strtol(buf, NULL, 10); \
259         data->temp_max[offset-1] = TEMP_TO_REG(val); \
260         i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(offset-1), \
261                                   data->temp_max[offset-1]); \
262         return count; \
263 } \
264 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
265         show_temp##offset##_min, set_temp##offset##_min); \
266 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
267         show_temp##offset##_max, set_temp##offset##_max);
268 set_temp(1);
269 set_temp(2);
270
271 static ssize_t show_alarms(struct device *dev, char *buf)
272 {
273         struct adm1025_data *data = adm1025_update_device(dev);
274         return sprintf(buf, "%u\n", data->alarms);
275 }
276 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
277
278 static ssize_t show_vid(struct device *dev, char *buf)
279 {
280         struct adm1025_data *data = adm1025_update_device(dev);
281         return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
282 }
283 static DEVICE_ATTR(in1_ref, S_IRUGO, show_vid, NULL);
284
285 static ssize_t show_vrm(struct device *dev, char *buf)
286 {
287         struct adm1025_data *data = adm1025_update_device(dev);
288         return sprintf(buf, "%u\n", data->vrm);
289 }
290 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
291 {
292         struct i2c_client *client = to_i2c_client(dev);
293         struct adm1025_data *data = i2c_get_clientdata(client);
294         data->vrm = simple_strtoul(buf, NULL, 10);
295         return count;
296 }
297 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
298
299 /*
300  * Real code
301  */
302
303 static int adm1025_attach_adapter(struct i2c_adapter *adapter)
304 {
305         if (!(adapter->class & I2C_CLASS_HWMON))
306                 return 0;
307         return i2c_detect(adapter, &addr_data, adm1025_detect);
308 }
309
310 /*
311  * The following function does more than just detection. If detection
312  * succeeds, it also registers the new chip.
313  */
314 static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind)
315 {
316         struct i2c_client *new_client;
317         struct adm1025_data *data;
318         int err = 0;
319         const char *name = "";
320         u8 config;
321
322         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
323                 goto exit;
324
325         if (!(data = kmalloc(sizeof(struct adm1025_data), GFP_KERNEL))) {
326                 err = -ENOMEM;
327                 goto exit;
328         }
329         memset(data, 0, sizeof(struct adm1025_data));
330
331         /* The common I2C client data is placed right before the
332            ADM1025-specific data. */
333         new_client = &data->client;
334         i2c_set_clientdata(new_client, data);
335         new_client->addr = address;
336         new_client->adapter = adapter;
337         new_client->driver = &adm1025_driver;
338         new_client->flags = 0;
339
340         /*
341          * Now we do the remaining detection. A negative kind means that
342          * the driver was loaded with no force parameter (default), so we
343          * must both detect and identify the chip. A zero kind means that
344          * the driver was loaded with the force parameter, the detection
345          * step shall be skipped. A positive kind means that the driver
346          * was loaded with the force parameter and a given kind of chip is
347          * requested, so both the detection and the identification steps
348          * are skipped.
349          */
350         config = i2c_smbus_read_byte_data(new_client, ADM1025_REG_CONFIG);
351         if (kind < 0) { /* detection */
352                 if ((config & 0x80) != 0x00
353                  || (i2c_smbus_read_byte_data(new_client,
354                      ADM1025_REG_STATUS1) & 0xC0) != 0x00
355                  || (i2c_smbus_read_byte_data(new_client,
356                      ADM1025_REG_STATUS2) & 0xBC) != 0x00) {
357                         dev_dbg(&adapter->dev,
358                                 "ADM1025 detection failed at 0x%02x.\n",
359                                 address);
360                         goto exit_free;
361                 }
362         }
363
364         if (kind <= 0) { /* identification */
365                 u8 man_id, chip_id;
366
367                 man_id = i2c_smbus_read_byte_data(new_client,
368                          ADM1025_REG_MAN_ID);
369                 chip_id = i2c_smbus_read_byte_data(new_client,
370                           ADM1025_REG_CHIP_ID);
371                 
372                 if (man_id == 0x41) { /* Analog Devices */
373                         if ((chip_id & 0xF0) == 0x20) { /* ADM1025/ADM1025A */
374                                 kind = adm1025;
375                         }
376                 } else
377                 if (man_id == 0xA1) { /* Philips */
378                         if (address != 0x2E
379                          && (chip_id & 0xF0) == 0x20) { /* NE1619 */
380                                 kind = ne1619;
381                         }
382                 }
383
384                 if (kind <= 0) { /* identification failed */
385                         dev_info(&adapter->dev,
386                             "Unsupported chip (man_id=0x%02X, "
387                             "chip_id=0x%02X).\n", man_id, chip_id);
388                         goto exit_free;
389                 }
390         }
391
392         if (kind == adm1025) {
393                 name = "adm1025";
394         } else if (kind == ne1619) {
395                 name = "ne1619";
396         }
397
398         /* We can fill in the remaining client fields */
399         strlcpy(new_client->name, name, I2C_NAME_SIZE);
400         new_client->id = adm1025_id++;
401         data->valid = 0;
402         init_MUTEX(&data->update_lock);
403
404         /* Tell the I2C layer a new client has arrived */
405         if ((err = i2c_attach_client(new_client)))
406                 goto exit_free;
407
408         /* Initialize the ADM1025 chip */
409         adm1025_init_client(new_client);
410
411         /* Register sysfs hooks */
412         device_create_file(&new_client->dev, &dev_attr_in0_input);
413         device_create_file(&new_client->dev, &dev_attr_in1_input);
414         device_create_file(&new_client->dev, &dev_attr_in2_input);
415         device_create_file(&new_client->dev, &dev_attr_in3_input);
416         device_create_file(&new_client->dev, &dev_attr_in5_input);
417         device_create_file(&new_client->dev, &dev_attr_in0_min);
418         device_create_file(&new_client->dev, &dev_attr_in1_min);
419         device_create_file(&new_client->dev, &dev_attr_in2_min);
420         device_create_file(&new_client->dev, &dev_attr_in3_min);
421         device_create_file(&new_client->dev, &dev_attr_in5_min);
422         device_create_file(&new_client->dev, &dev_attr_in0_max);
423         device_create_file(&new_client->dev, &dev_attr_in1_max);
424         device_create_file(&new_client->dev, &dev_attr_in2_max);
425         device_create_file(&new_client->dev, &dev_attr_in3_max);
426         device_create_file(&new_client->dev, &dev_attr_in5_max);
427         device_create_file(&new_client->dev, &dev_attr_temp1_input);
428         device_create_file(&new_client->dev, &dev_attr_temp2_input);
429         device_create_file(&new_client->dev, &dev_attr_temp1_min);
430         device_create_file(&new_client->dev, &dev_attr_temp2_min);
431         device_create_file(&new_client->dev, &dev_attr_temp1_max);
432         device_create_file(&new_client->dev, &dev_attr_temp2_max);
433         device_create_file(&new_client->dev, &dev_attr_alarms);
434         device_create_file(&new_client->dev, &dev_attr_in1_ref);
435         device_create_file(&new_client->dev, &dev_attr_vrm);
436
437         /* Pin 11 is either in4 (+12V) or VID4 */
438         if (!(config & 0x20)) {
439                 device_create_file(&new_client->dev, &dev_attr_in4_input);
440                 device_create_file(&new_client->dev, &dev_attr_in4_min);
441                 device_create_file(&new_client->dev, &dev_attr_in4_max);
442         }
443
444         return 0;
445
446 exit_free:
447         kfree(data);
448 exit:
449         return err;
450 }
451
452 static void adm1025_init_client(struct i2c_client *client)
453 {
454         u8 reg;
455         struct adm1025_data *data = i2c_get_clientdata(client);
456         int i;
457
458         data->vrm = i2c_which_vrm();
459
460         /*
461          * Set high limits
462          * Usually we avoid setting limits on driver init, but it happens
463          * that the ADM1025 comes with stupid default limits (all registers
464          * set to 0). In case the chip has not gone through any limit
465          * setting yet, we better set the high limits to the max so that
466          * no alarm triggers.
467          */
468         for (i=0; i<6; i++) {
469                 reg = i2c_smbus_read_byte_data(client,
470                                                ADM1025_REG_IN_MAX(i));
471                 if (reg == 0)
472                         i2c_smbus_write_byte_data(client,
473                                                   ADM1025_REG_IN_MAX(i),
474                                                   0xFF);
475         }
476         for (i=0; i<2; i++) {
477                 reg = i2c_smbus_read_byte_data(client,
478                                                ADM1025_REG_TEMP_HIGH(i));
479                 if (reg == 0)
480                         i2c_smbus_write_byte_data(client,
481                                                   ADM1025_REG_TEMP_HIGH(i),
482                                                   0x7F);
483         }
484
485         /*
486          * Start the conversions
487          */
488         reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
489         if (!(reg & 0x01))
490                 i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
491                                           (reg&0x7E)|0x01);
492 }
493
494 static int adm1025_detach_client(struct i2c_client *client)
495 {
496         int err;
497
498         if ((err = i2c_detach_client(client))) {
499                 dev_err(&client->dev, "Client deregistration failed, "
500                         "client not detached.\n");
501                 return err;
502         }
503
504         kfree(i2c_get_clientdata(client));
505         return 0;
506 }
507
508 static struct adm1025_data *adm1025_update_device(struct device *dev)
509 {
510         struct i2c_client *client = to_i2c_client(dev);
511         struct adm1025_data *data = i2c_get_clientdata(client);
512
513         down(&data->update_lock);
514
515         if ((jiffies - data->last_updated > HZ * 2) ||
516             (jiffies < data->last_updated) ||
517             !data->valid) {
518                 int i;
519
520                 dev_dbg(&client->dev, "Updating data.\n");
521                 for (i=0; i<6; i++) {
522                         data->in[i] = i2c_smbus_read_byte_data(client,
523                                       ADM1025_REG_IN(i));
524                         data->in_min[i] = i2c_smbus_read_byte_data(client,
525                                           ADM1025_REG_IN_MIN(i));
526                         data->in_max[i] = i2c_smbus_read_byte_data(client,
527                                           ADM1025_REG_IN_MAX(i));
528                 }
529                 for (i=0; i<2; i++) {
530                         data->temp[i] = i2c_smbus_read_byte_data(client,
531                                         ADM1025_REG_TEMP(i));
532                         data->temp_min[i] = i2c_smbus_read_byte_data(client,
533                                             ADM1025_REG_TEMP_LOW(i));
534                         data->temp_max[i] = i2c_smbus_read_byte_data(client,
535                                             ADM1025_REG_TEMP_HIGH(i));
536                 }
537                 data->alarms = i2c_smbus_read_byte_data(client,
538                                ADM1025_REG_STATUS1)
539                              | (i2c_smbus_read_byte_data(client,
540                                 ADM1025_REG_STATUS2) << 8);
541                 data->vid = (i2c_smbus_read_byte_data(client,
542                              ADM1025_REG_VID) & 0x0f)
543                           | ((i2c_smbus_read_byte_data(client,
544                               ADM1025_REG_VID4) & 0x01) << 4);
545
546                 data->last_updated = jiffies;
547                 data->valid = 1;
548         }
549
550         up(&data->update_lock);
551
552         return data;
553 }
554
555 static int __init sensors_adm1025_init(void)
556 {
557         return i2c_add_driver(&adm1025_driver);
558 }
559
560 static void __exit sensors_adm1025_exit(void)
561 {
562         i2c_del_driver(&adm1025_driver);
563 }
564
565 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
566 MODULE_DESCRIPTION("ADM1025 driver");
567 MODULE_LICENSE("GPL");
568
569 module_init(sensors_adm1025_init);
570 module_exit(sensors_adm1025_exit);