patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / lm90.c
1 /*
2  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003-2004  Jean Delvare <khali@linux-fr.org>
5  *
6  * Based on the lm83 driver. The LM90 is a sensor chip made by National
7  * Semiconductor. It reports up to two temperatures (its own plus up to
8  * one external one) with a 0.125 deg resolution (1 deg for local
9  * temperature) and a 3-4 deg accuracy. Complete datasheet can be
10  * obtained from National's website at:
11  *   http://www.national.com/pf/LM/LM90.html
12  *
13  * This driver also supports the LM89 and LM99, two other sensor chips
14  * made by National Semiconductor. Both have an increased remote
15  * temperature measurement accuracy (1 degree), and the LM99
16  * additionally shifts remote temperatures (measured and limits) by 16
17  * degrees, which allows for higher temperatures measurement. The
18  * driver doesn't handle it since it can be done easily in user-space.
19  * Complete datasheets can be obtained from National's website at:
20  *   http://www.national.com/pf/LM/LM89.html
21  *   http://www.national.com/pf/LM/LM99.html
22  * Note that there is no way to differenciate between both chips.
23  *
24  * This driver also supports the ADM1032, a sensor chip made by Analog
25  * Devices. That chip is similar to the LM90, with a few differences
26  * that are not handled by this driver. Complete datasheet can be
27  * obtained from Analog's website at:
28  *   http://products.analog.com/products/info.asp?product=ADM1032
29  *
30  * Since the LM90 was the first chipset supported by this driver, most
31  * comments will refer to this chipset, but are actually general and
32  * concern all supported chipsets, unless mentioned otherwise.
33  *
34  * This program is free software; you can redistribute it and/or modify
35  * it under the terms of the GNU General Public License as published by
36  * the Free Software Foundation; either version 2 of the License, or
37  * (at your option) any later version.
38  *
39  * This program is distributed in the hope that it will be useful,
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42  * GNU General Public License for more details.
43  *
44  * You should have received a copy of the GNU General Public License
45  * along with this program; if not, write to the Free Software
46  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47  */
48
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/i2c.h>
54 #include <linux/i2c-sensor.h>
55
56 /*
57  * Addresses to scan
58  * Address is fully defined internally and cannot be changed.
59  * LM89, LM90, LM99 and ADM1032 have address 0x4c.
60  * LM89-1, and LM99-1 have address 0x4d.
61  */
62
63 static unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END };
64 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
65 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
66 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
67
68 /*
69  * Insmod parameters
70  */
71
72 SENSORS_INSMOD_3(lm90, adm1032, lm99);
73
74 /*
75  * The LM90 registers
76  */
77
78 #define LM90_REG_R_MAN_ID               0xFE
79 #define LM90_REG_R_CHIP_ID              0xFF
80 #define LM90_REG_R_CONFIG1              0x03
81 #define LM90_REG_W_CONFIG1              0x09
82 #define LM90_REG_R_CONFIG2              0xBF
83 #define LM90_REG_W_CONFIG2              0xBF
84 #define LM90_REG_R_CONVRATE             0x04
85 #define LM90_REG_W_CONVRATE             0x0A
86 #define LM90_REG_R_STATUS               0x02
87 #define LM90_REG_R_LOCAL_TEMP           0x00
88 #define LM90_REG_R_LOCAL_HIGH           0x05
89 #define LM90_REG_W_LOCAL_HIGH           0x0B
90 #define LM90_REG_R_LOCAL_LOW            0x06
91 #define LM90_REG_W_LOCAL_LOW            0x0C
92 #define LM90_REG_R_LOCAL_CRIT           0x20
93 #define LM90_REG_W_LOCAL_CRIT           0x20
94 #define LM90_REG_R_REMOTE_TEMPH         0x01
95 #define LM90_REG_R_REMOTE_TEMPL         0x10
96 #define LM90_REG_R_REMOTE_OFFSH         0x11
97 #define LM90_REG_W_REMOTE_OFFSH         0x11
98 #define LM90_REG_R_REMOTE_OFFSL         0x12
99 #define LM90_REG_W_REMOTE_OFFSL         0x12
100 #define LM90_REG_R_REMOTE_HIGHH         0x07
101 #define LM90_REG_W_REMOTE_HIGHH         0x0D
102 #define LM90_REG_R_REMOTE_HIGHL         0x13
103 #define LM90_REG_W_REMOTE_HIGHL         0x13
104 #define LM90_REG_R_REMOTE_LOWH          0x08
105 #define LM90_REG_W_REMOTE_LOWH          0x0E
106 #define LM90_REG_R_REMOTE_LOWL          0x14
107 #define LM90_REG_W_REMOTE_LOWL          0x14
108 #define LM90_REG_R_REMOTE_CRIT          0x19
109 #define LM90_REG_W_REMOTE_CRIT          0x19
110 #define LM90_REG_R_TCRIT_HYST           0x21
111 #define LM90_REG_W_TCRIT_HYST           0x21
112
113 /*
114  * Conversions and various macros
115  * The LM90 uses signed 8-bit values for the local temperatures,
116  * and signed 11-bit values for the remote temperatures (except
117  * T_CRIT). Note that TEMP2_TO_REG does not round values, but
118  * stick to the nearest lower value instead. Fixing it is just
119  * not worth it.
120  */
121
122 #define TEMP1_FROM_REG(val)     ((val & 0x80 ? val-0x100 : val) * 1000)
123 #define TEMP1_TO_REG(val)       ((val < 0 ? val+0x100*1000 : val) / 1000)
124 #define TEMP2_FROM_REG(val)     (((val & 0x8000 ? val-0x10000 : val) >> 5) * 125)
125 #define TEMP2_TO_REG(val)       ((((val / 125) << 5) + (val < 0 ? 0x10000 : 0)) & 0xFFE0)
126 #define HYST_FROM_REG(val)      (val * 1000)
127 #define HYST_TO_REG(val)        (val <= 0 ? 0 : val >= 31000 ? 31 : val / 1000)
128
129 /*
130  * Functions declaration
131  */
132
133 static int lm90_attach_adapter(struct i2c_adapter *adapter);
134 static int lm90_detect(struct i2c_adapter *adapter, int address,
135         int kind);
136 static void lm90_init_client(struct i2c_client *client);
137 static int lm90_detach_client(struct i2c_client *client);
138 static struct lm90_data *lm90_update_device(struct device *dev);
139
140 /*
141  * Driver data (common to all clients)
142  */
143
144 static struct i2c_driver lm90_driver = {
145         .owner          = THIS_MODULE,
146         .name           = "lm90",
147         .id             = I2C_DRIVERID_LM90,
148         .flags          = I2C_DF_NOTIFY,
149         .attach_adapter = lm90_attach_adapter,
150         .detach_client  = lm90_detach_client,
151 };
152
153 /*
154  * Client data (each client gets its own)
155  */
156
157 struct lm90_data {
158         struct i2c_client client;
159         struct semaphore update_lock;
160         char valid; /* zero until following fields are valid */
161         unsigned long last_updated; /* in jiffies */
162
163         /* registers values */
164         u8 temp_input1, temp_low1, temp_high1; /* local */
165         u16 temp_input2, temp_low2, temp_high2; /* remote, combined */
166         u8 temp_crit1, temp_crit2;
167         u8 temp_hyst;
168         u16 alarms; /* bitvector, combined */
169 };
170
171 /*
172  * Internal variables
173  */
174
175 static int lm90_id = 0;
176
177 /*
178  * Sysfs stuff
179  */
180
181 #define show_temp(value, converter) \
182 static ssize_t show_##value(struct device *dev, char *buf) \
183 { \
184         struct lm90_data *data = lm90_update_device(dev); \
185         return sprintf(buf, "%d\n", converter(data->value)); \
186 }
187 show_temp(temp_input1, TEMP1_FROM_REG);
188 show_temp(temp_input2, TEMP2_FROM_REG);
189 show_temp(temp_low1, TEMP1_FROM_REG);
190 show_temp(temp_low2, TEMP2_FROM_REG);
191 show_temp(temp_high1, TEMP1_FROM_REG);
192 show_temp(temp_high2, TEMP2_FROM_REG);
193 show_temp(temp_crit1, TEMP1_FROM_REG);
194 show_temp(temp_crit2, TEMP1_FROM_REG);
195
196 #define set_temp1(value, reg) \
197 static ssize_t set_##value(struct device *dev, const char *buf, \
198         size_t count) \
199 { \
200         struct i2c_client *client = to_i2c_client(dev); \
201         struct lm90_data *data = i2c_get_clientdata(client); \
202         data->value = TEMP1_TO_REG(simple_strtol(buf, NULL, 10)); \
203         i2c_smbus_write_byte_data(client, reg, data->value); \
204         return count; \
205 }
206 #define set_temp2(value, regh, regl) \
207 static ssize_t set_##value(struct device *dev, const char *buf, \
208         size_t count) \
209 { \
210         struct i2c_client *client = to_i2c_client(dev); \
211         struct lm90_data *data = i2c_get_clientdata(client); \
212         data->value = TEMP2_TO_REG(simple_strtol(buf, NULL, 10)); \
213         i2c_smbus_write_byte_data(client, regh, data->value >> 8); \
214         i2c_smbus_write_byte_data(client, regl, data->value & 0xff); \
215         return count; \
216 }
217 set_temp1(temp_low1, LM90_REG_W_LOCAL_LOW);
218 set_temp2(temp_low2, LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL);
219 set_temp1(temp_high1, LM90_REG_W_LOCAL_HIGH);
220 set_temp2(temp_high2, LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL);
221 set_temp1(temp_crit1, LM90_REG_W_LOCAL_CRIT);
222 set_temp1(temp_crit2, LM90_REG_W_REMOTE_CRIT);
223
224 #define show_temp_hyst(value, basereg) \
225 static ssize_t show_##value(struct device *dev, char *buf) \
226 { \
227         struct lm90_data *data = lm90_update_device(dev); \
228         return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->basereg) \
229                        - HYST_FROM_REG(data->temp_hyst)); \
230 }
231 show_temp_hyst(temp_hyst1, temp_crit1);
232 show_temp_hyst(temp_hyst2, temp_crit2);
233
234 static ssize_t set_temp_hyst1(struct device *dev, const char *buf,
235         size_t count)
236 {
237         struct i2c_client *client = to_i2c_client(dev);
238         struct lm90_data *data = i2c_get_clientdata(client);
239         int hyst = TEMP1_FROM_REG(data->temp_crit1) -
240                    simple_strtol(buf, NULL, 10);
241         i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
242                                   HYST_TO_REG(hyst));
243         return count;
244 }
245
246 static ssize_t show_alarms(struct device *dev, char *buf)
247 {
248         struct lm90_data *data = lm90_update_device(dev);
249         return sprintf(buf, "%d\n", data->alarms);
250 }
251
252 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
253 static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
254 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_low1,
255         set_temp_low1);
256 static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
257         set_temp_low2);
258 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
259         set_temp_high1);
260 static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
261         set_temp_high2);
262 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit1,
263         set_temp_crit1);
264 static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
265         set_temp_crit2);
266 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst1,
267         set_temp_hyst1);
268 static DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_hyst2, NULL);
269 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
270
271 /*
272  * Real code
273  */
274
275 static int lm90_attach_adapter(struct i2c_adapter *adapter)
276 {
277         if (!(adapter->class & I2C_CLASS_HWMON))
278                 return 0;
279         return i2c_detect(adapter, &addr_data, lm90_detect);
280 }
281
282 /*
283  * The following function does more than just detection. If detection
284  * succeeds, it also registers the new chip.
285  */
286 static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
287 {
288         struct i2c_client *new_client;
289         struct lm90_data *data;
290         int err = 0;
291         const char *name = "";
292         u8 reg_config1=0, reg_convrate=0;
293
294         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
295                 goto exit;
296
297         if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
298                 err = -ENOMEM;
299                 goto exit;
300         }
301         memset(data, 0, sizeof(struct lm90_data));
302
303         /* The common I2C client data is placed right before the
304            LM90-specific data. */
305         new_client = &data->client;
306         i2c_set_clientdata(new_client, data);
307         new_client->addr = address;
308         new_client->adapter = adapter;
309         new_client->driver = &lm90_driver;
310         new_client->flags = 0;
311
312         /*
313          * Now we do the remaining detection. A negative kind means that
314          * the driver was loaded with no force parameter (default), so we
315          * must both detect and identify the chip. A zero kind means that
316          * the driver was loaded with the force parameter, the detection
317          * step shall be skipped. A positive kind means that the driver
318          * was loaded with the force parameter and a given kind of chip is
319          * requested, so both the detection and the identification steps
320          * are skipped.
321          */
322         if (kind < 0) { /* detection */
323                 reg_config1 = i2c_smbus_read_byte_data(new_client,
324                               LM90_REG_R_CONFIG1);
325                 reg_convrate = i2c_smbus_read_byte_data(new_client,
326                                LM90_REG_R_CONVRATE);
327
328                 if ((reg_config1 & 0x2A) != 0x00
329                  || reg_convrate > 0x0A) {
330                         dev_dbg(&adapter->dev,
331                                 "LM90 detection failed at 0x%02x.\n",
332                                 address);
333                         goto exit_free;
334                 }
335         }
336
337         if (kind <= 0) { /* identification */
338                 u8 man_id, chip_id;
339
340                 man_id = i2c_smbus_read_byte_data(new_client,
341                          LM90_REG_R_MAN_ID);
342                 chip_id = i2c_smbus_read_byte_data(new_client,
343                           LM90_REG_R_CHIP_ID);
344                 
345                 if (man_id == 0x01) { /* National Semiconductor */
346                         u8 reg_config2;
347
348                         reg_config2 = i2c_smbus_read_byte_data(new_client,
349                                       LM90_REG_R_CONFIG2);
350
351                         if (kind == 0 /* skip detection */
352                          || ((reg_config2 & 0xF8) == 0x00
353                           && reg_convrate <= 0x09)) {
354                                 if (address == 0x4C
355                                  && (chip_id & 0xF0) == 0x20) { /* LM90 */
356                                         kind = lm90;
357                                 } else
358                                 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
359                                         kind = lm99;
360                                 }
361                         }
362                 } else
363                 if (man_id == 0x41) { /* Analog Devices */
364                         if (address == 0x4C
365                          && (chip_id & 0xF0) == 0x40 /* ADM1032 */
366                          && (kind == 0 /* skip detection */
367                           || (reg_config1 & 0x3F) == 0x00)) {
368                                 kind = adm1032;
369                         }
370                 }
371
372                 if (kind <= 0) { /* identification failed */
373                         dev_info(&adapter->dev,
374                             "Unsupported chip (man_id=0x%02X, "
375                             "chip_id=0x%02X).\n", man_id, chip_id);
376                         goto exit_free;
377                 }
378         }
379
380         if (kind == lm90) {
381                 name = "lm90";
382         } else if (kind == adm1032) {
383                 name = "adm1032";
384         } else if (kind == lm99) {
385                 name = "lm99";
386         }
387
388         /* We can fill in the remaining client fields */
389         strlcpy(new_client->name, name, I2C_NAME_SIZE);
390         new_client->id = lm90_id++;
391         data->valid = 0;
392         init_MUTEX(&data->update_lock);
393
394         /* Tell the I2C layer a new client has arrived */
395         if ((err = i2c_attach_client(new_client)))
396                 goto exit_free;
397
398         /* Initialize the LM90 chip */
399         lm90_init_client(new_client);
400
401         /* Register sysfs hooks */
402         device_create_file(&new_client->dev, &dev_attr_temp1_input);
403         device_create_file(&new_client->dev, &dev_attr_temp2_input);
404         device_create_file(&new_client->dev, &dev_attr_temp1_min);
405         device_create_file(&new_client->dev, &dev_attr_temp2_min);
406         device_create_file(&new_client->dev, &dev_attr_temp1_max);
407         device_create_file(&new_client->dev, &dev_attr_temp2_max);
408         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
409         device_create_file(&new_client->dev, &dev_attr_temp2_crit);
410         device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
411         device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
412         device_create_file(&new_client->dev, &dev_attr_alarms);
413
414         return 0;
415
416 exit_free:
417         kfree(data);
418 exit:
419         return err;
420 }
421
422 static void lm90_init_client(struct i2c_client *client)
423 {
424         u8 config;
425
426         /*
427          * Start the conversions.
428          */
429         i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
430                                   5); /* 2 Hz */
431         config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
432         if (config & 0x40)
433                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
434                                           config & 0xBF); /* run */
435 }
436
437 static int lm90_detach_client(struct i2c_client *client)
438 {
439         int err;
440
441         if ((err = i2c_detach_client(client))) {
442                 dev_err(&client->dev, "Client deregistration failed, "
443                         "client not detached.\n");
444                 return err;
445         }
446
447         kfree(i2c_get_clientdata(client));
448         return 0;
449 }
450
451 static struct lm90_data *lm90_update_device(struct device *dev)
452 {
453         struct i2c_client *client = to_i2c_client(dev);
454         struct lm90_data *data = i2c_get_clientdata(client);
455
456         down(&data->update_lock);
457
458         if ((jiffies - data->last_updated > HZ * 2) ||
459             (jiffies < data->last_updated) ||
460             !data->valid) {
461                 u8 oldh, newh;
462
463                 dev_dbg(&client->dev, "Updating lm90 data.\n");
464                 data->temp_input1 = i2c_smbus_read_byte_data(client,
465                                     LM90_REG_R_LOCAL_TEMP);
466                 data->temp_high1 = i2c_smbus_read_byte_data(client,
467                                    LM90_REG_R_LOCAL_HIGH);
468                 data->temp_low1 = i2c_smbus_read_byte_data(client,
469                                   LM90_REG_R_LOCAL_LOW);
470                 data->temp_crit1 = i2c_smbus_read_byte_data(client,
471                                    LM90_REG_R_LOCAL_CRIT);
472                 data->temp_crit2 = i2c_smbus_read_byte_data(client,
473                                    LM90_REG_R_REMOTE_CRIT);
474                 data->temp_hyst = i2c_smbus_read_byte_data(client,
475                                   LM90_REG_R_TCRIT_HYST);
476
477                 /*
478                  * There is a trick here. We have to read two registers to
479                  * have the remote sensor temperature, but we have to beware
480                  * a conversion could occur inbetween the readings. The
481                  * datasheet says we should either use the one-shot
482                  * conversion register, which we don't want to do (disables
483                  * hardware monitoring) or monitor the busy bit, which is
484                  * impossible (we can't read the values and monitor that bit
485                  * at the exact same time). So the solution used here is to
486                  * read the high byte once, then the low byte, then the high
487                  * byte again. If the new high byte matches the old one,
488                  * then we have a valid reading. Else we have to read the low
489                  * byte again, and now we believe we have a correct reading.
490                  */
491                 oldh = i2c_smbus_read_byte_data(client,
492                        LM90_REG_R_REMOTE_TEMPH);
493                 data->temp_input2 = i2c_smbus_read_byte_data(client,
494                                     LM90_REG_R_REMOTE_TEMPL);
495                 newh = i2c_smbus_read_byte_data(client,
496                        LM90_REG_R_REMOTE_TEMPH);
497                 if (newh != oldh) {
498                         data->temp_input2 = i2c_smbus_read_byte_data(client,
499                                             LM90_REG_R_REMOTE_TEMPL);
500 #ifdef DEBUG
501                         oldh = i2c_smbus_read_byte_data(client,
502                                LM90_REG_R_REMOTE_TEMPH);
503                         /* oldh is actually newer */
504                         if (newh != oldh)
505                                 dev_warn(&client->dev, "Remote temperature may be "
506                                          "wrong.\n");
507 #endif
508                 }
509                 data->temp_input2 |= (newh << 8);
510
511                 data->temp_high2 = (i2c_smbus_read_byte_data(client,
512                                    LM90_REG_R_REMOTE_HIGHH) << 8) +
513                                    i2c_smbus_read_byte_data(client,
514                                    LM90_REG_R_REMOTE_HIGHL);
515                 data->temp_low2 = (i2c_smbus_read_byte_data(client,
516                                   LM90_REG_R_REMOTE_LOWH) << 8) +
517                                   i2c_smbus_read_byte_data(client,
518                                   LM90_REG_R_REMOTE_LOWL);
519                 data->alarms = i2c_smbus_read_byte_data(client,
520                                LM90_REG_R_STATUS);
521
522                 data->last_updated = jiffies;
523                 data->valid = 1;
524         }
525
526         up(&data->update_lock);
527
528         return data;
529 }
530
531 static int __init sensors_lm90_init(void)
532 {
533         return i2c_add_driver(&lm90_driver);
534 }
535
536 static void __exit sensors_lm90_exit(void)
537 {
538         i2c_del_driver(&lm90_driver);
539 }
540
541 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
542 MODULE_DESCRIPTION("LM90/ADM1032 driver");
543 MODULE_LICENSE("GPL");
544
545 module_init(sensors_lm90_init);
546 module_exit(sensors_lm90_exit);