2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2003 Jean Delvare <khali@linux-fr.org>
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
13 * This driver also supports the ADM1032, a sensor chip made by Analog
14 * Devices. That chip is similar to the LM90, with a few differences
15 * that are not handled by this driver. Complete datasheet can be
16 * obtained from Analog's website at:
17 * http://products.analog.com/products/info.asp?product=ADM1032
19 * Since the LM90 was the first chipset supported by this driver, most
20 * comments will refer to this chipset, but are actually general and
21 * concern all supported chipsets, unless mentioned otherwise.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/i2c.h>
43 #include <linux/i2c-sensor.h>
47 * Address is fully defined internally and cannot be changed.
50 static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
51 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
52 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
53 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
59 SENSORS_INSMOD_2(lm90, adm1032);
65 #define LM90_REG_R_MAN_ID 0xFE
66 #define LM90_REG_R_CHIP_ID 0xFF
67 #define LM90_REG_R_CONFIG1 0x03
68 #define LM90_REG_W_CONFIG1 0x09
69 #define LM90_REG_R_CONFIG2 0xBF
70 #define LM90_REG_W_CONFIG2 0xBF
71 #define LM90_REG_R_CONVRATE 0x04
72 #define LM90_REG_W_CONVRATE 0x0A
73 #define LM90_REG_R_STATUS 0x02
74 #define LM90_REG_R_LOCAL_TEMP 0x00
75 #define LM90_REG_R_LOCAL_HIGH 0x05
76 #define LM90_REG_W_LOCAL_HIGH 0x0B
77 #define LM90_REG_R_LOCAL_LOW 0x06
78 #define LM90_REG_W_LOCAL_LOW 0x0C
79 #define LM90_REG_R_LOCAL_CRIT 0x20
80 #define LM90_REG_W_LOCAL_CRIT 0x20
81 #define LM90_REG_R_REMOTE_TEMPH 0x01
82 #define LM90_REG_R_REMOTE_TEMPL 0x10
83 #define LM90_REG_R_REMOTE_OFFSH 0x11
84 #define LM90_REG_W_REMOTE_OFFSH 0x11
85 #define LM90_REG_R_REMOTE_OFFSL 0x12
86 #define LM90_REG_W_REMOTE_OFFSL 0x12
87 #define LM90_REG_R_REMOTE_HIGHH 0x07
88 #define LM90_REG_W_REMOTE_HIGHH 0x0D
89 #define LM90_REG_R_REMOTE_HIGHL 0x13
90 #define LM90_REG_W_REMOTE_HIGHL 0x13
91 #define LM90_REG_R_REMOTE_LOWH 0x08
92 #define LM90_REG_W_REMOTE_LOWH 0x0E
93 #define LM90_REG_R_REMOTE_LOWL 0x14
94 #define LM90_REG_W_REMOTE_LOWL 0x14
95 #define LM90_REG_R_REMOTE_CRIT 0x19
96 #define LM90_REG_W_REMOTE_CRIT 0x19
97 #define LM90_REG_R_TCRIT_HYST 0x21
98 #define LM90_REG_W_TCRIT_HYST 0x21
101 * Conversions and various macros
102 * The LM90 uses signed 8-bit values for the local temperatures,
103 * and signed 11-bit values for the remote temperatures (except
104 * T_CRIT). Note that TEMP2_TO_REG does not round values, but
105 * stick to the nearest lower value instead. Fixing it is just
109 #define TEMP1_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
110 #define TEMP1_TO_REG(val) ((val < 0 ? val+0x100*1000 : val) / 1000)
111 #define TEMP2_FROM_REG(val) (((val & 0x8000 ? val-0x10000 : val) >> 5) * 125)
112 #define TEMP2_TO_REG(val) ((((val / 125) << 5) + (val < 0 ? 0x10000 : 0)) & 0xFFE0)
113 #define HYST_FROM_REG(val) (val * 1000)
114 #define HYST_TO_REG(val) (val <= 0 ? 0 : val >= 31000 ? 31 : val / 1000)
117 * Functions declaration
120 static int lm90_attach_adapter(struct i2c_adapter *adapter);
121 static int lm90_detect(struct i2c_adapter *adapter, int address,
123 static void lm90_init_client(struct i2c_client *client);
124 static int lm90_detach_client(struct i2c_client *client);
125 static struct lm90_data *lm90_update_device(struct device *dev);
128 * Driver data (common to all clients)
131 static struct i2c_driver lm90_driver = {
132 .owner = THIS_MODULE,
134 .id = I2C_DRIVERID_LM90,
135 .flags = I2C_DF_NOTIFY,
136 .attach_adapter = lm90_attach_adapter,
137 .detach_client = lm90_detach_client,
141 * Client data (each client gets its own)
145 struct i2c_client client;
146 struct semaphore update_lock;
147 char valid; /* zero until following fields are valid */
148 unsigned long last_updated; /* in jiffies */
150 /* registers values */
151 u8 temp_input1, temp_low1, temp_high1; /* local */
152 u16 temp_input2, temp_low2, temp_high2; /* remote, combined */
153 u8 temp_crit1, temp_crit2;
155 u16 alarms; /* bitvector, combined */
162 static int lm90_id = 0;
168 #define show_temp(value, converter) \
169 static ssize_t show_##value(struct device *dev, char *buf) \
171 struct lm90_data *data = lm90_update_device(dev); \
172 return sprintf(buf, "%d\n", converter(data->value)); \
174 show_temp(temp_input1, TEMP1_FROM_REG);
175 show_temp(temp_input2, TEMP2_FROM_REG);
176 show_temp(temp_low1, TEMP1_FROM_REG);
177 show_temp(temp_low2, TEMP2_FROM_REG);
178 show_temp(temp_high1, TEMP1_FROM_REG);
179 show_temp(temp_high2, TEMP2_FROM_REG);
180 show_temp(temp_crit1, TEMP1_FROM_REG);
181 show_temp(temp_crit2, TEMP1_FROM_REG);
183 #define set_temp1(value, reg) \
184 static ssize_t set_##value(struct device *dev, const char *buf, \
187 struct i2c_client *client = to_i2c_client(dev); \
188 struct lm90_data *data = i2c_get_clientdata(client); \
189 data->value = TEMP1_TO_REG(simple_strtol(buf, NULL, 10)); \
190 i2c_smbus_write_byte_data(client, reg, data->value); \
193 #define set_temp2(value, regh, regl) \
194 static ssize_t set_##value(struct device *dev, const char *buf, \
197 struct i2c_client *client = to_i2c_client(dev); \
198 struct lm90_data *data = i2c_get_clientdata(client); \
199 data->value = TEMP2_TO_REG(simple_strtol(buf, NULL, 10)); \
200 i2c_smbus_write_byte_data(client, regh, data->value >> 8); \
201 i2c_smbus_write_byte_data(client, regl, data->value & 0xff); \
204 set_temp1(temp_low1, LM90_REG_W_LOCAL_LOW);
205 set_temp2(temp_low2, LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL);
206 set_temp1(temp_high1, LM90_REG_W_LOCAL_HIGH);
207 set_temp2(temp_high2, LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL);
208 set_temp1(temp_crit1, LM90_REG_W_LOCAL_CRIT);
209 set_temp1(temp_crit2, LM90_REG_W_REMOTE_CRIT);
211 #define show_temp_hyst(value, basereg) \
212 static ssize_t show_##value(struct device *dev, char *buf) \
214 struct lm90_data *data = lm90_update_device(dev); \
215 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->basereg) \
216 - HYST_FROM_REG(data->temp_hyst)); \
218 show_temp_hyst(temp_hyst1, temp_crit1);
219 show_temp_hyst(temp_hyst2, temp_crit2);
221 static ssize_t set_temp_hyst1(struct device *dev, const char *buf,
224 struct i2c_client *client = to_i2c_client(dev);
225 struct lm90_data *data = i2c_get_clientdata(client);
226 int hyst = TEMP1_FROM_REG(data->temp_crit1) -
227 simple_strtol(buf, NULL, 10);
228 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
233 static ssize_t show_alarms(struct device *dev, char *buf)
235 struct lm90_data *data = lm90_update_device(dev);
236 return sprintf(buf, "%d\n", data->alarms);
239 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
240 static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
241 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_low1,
243 static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
245 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
247 static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
249 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit1,
251 static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
253 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst1,
255 static DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_hyst2, NULL);
256 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
262 static int lm90_attach_adapter(struct i2c_adapter *adapter)
264 if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
266 return i2c_detect(adapter, &addr_data, lm90_detect);
270 * The following function does more than just detection. If detection
271 * succeeds, it also registers the new chip.
273 static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
275 struct i2c_client *new_client;
276 struct lm90_data *data;
278 const char *name = "";
279 u8 reg_config1=0, reg_convrate=0;
281 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
284 if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
288 memset(data, 0, sizeof(struct lm90_data));
290 /* The common I2C client data is placed right before the
291 LM90-specific data. */
292 new_client = &data->client;
293 i2c_set_clientdata(new_client, data);
294 new_client->addr = address;
295 new_client->adapter = adapter;
296 new_client->driver = &lm90_driver;
297 new_client->flags = 0;
300 * Now we do the remaining detection. A negative kind means that
301 * the driver was loaded with no force parameter (default), so we
302 * must both detect and identify the chip. A zero kind means that
303 * the driver was loaded with the force parameter, the detection
304 * step shall be skipped. A positive kind means that the driver
305 * was loaded with the force parameter and a given kind of chip is
306 * requested, so both the detection and the identification steps
309 if (kind < 0) { /* detection */
310 reg_config1 = i2c_smbus_read_byte_data(new_client,
312 reg_convrate = i2c_smbus_read_byte_data(new_client,
313 LM90_REG_R_CONVRATE);
315 if ((reg_config1 & 0x2A) != 0x00
316 || reg_convrate > 0x0A) {
317 dev_dbg(&adapter->dev,
318 "LM90 detection failed at 0x%02x.\n",
324 if (kind <= 0) { /* identification */
327 man_id = i2c_smbus_read_byte_data(new_client,
329 chip_id = i2c_smbus_read_byte_data(new_client,
332 if (man_id == 0x01) { /* National Semiconductor */
333 if (chip_id >= 0x21 && chip_id < 0x30 /* LM90 */
334 && (kind == 0 /* skip detection */
335 || ((i2c_smbus_read_byte_data(new_client,
336 LM90_REG_R_CONFIG2) & 0xF8) == 0x00
337 && reg_convrate <= 0x09))) {
341 else if (man_id == 0x41) { /* Analog Devices */
342 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
343 && (kind == 0 /* skip detection */
344 || (reg_config1 & 0x3F) == 0x00)) {
349 if (kind <= 0) { /* identification failed */
350 dev_info(&adapter->dev,
351 "Unsupported chip (man_id=0x%02X, "
352 "chip_id=0x%02X).\n", man_id, chip_id);
359 } else if (kind == adm1032) {
363 /* We can fill in the remaining client fields */
364 strlcpy(new_client->name, name, I2C_NAME_SIZE);
365 new_client->id = lm90_id++;
367 init_MUTEX(&data->update_lock);
369 /* Tell the I2C layer a new client has arrived */
370 if ((err = i2c_attach_client(new_client)))
373 /* Initialize the LM90 chip */
374 lm90_init_client(new_client);
376 /* Register sysfs hooks */
377 device_create_file(&new_client->dev, &dev_attr_temp1_input);
378 device_create_file(&new_client->dev, &dev_attr_temp2_input);
379 device_create_file(&new_client->dev, &dev_attr_temp1_min);
380 device_create_file(&new_client->dev, &dev_attr_temp2_min);
381 device_create_file(&new_client->dev, &dev_attr_temp1_max);
382 device_create_file(&new_client->dev, &dev_attr_temp2_max);
383 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
384 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
385 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
386 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
387 device_create_file(&new_client->dev, &dev_attr_alarms);
397 static void lm90_init_client(struct i2c_client *client)
402 * Start the conversions.
404 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
406 config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
408 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
409 config & 0xBF); /* run */
412 static int lm90_detach_client(struct i2c_client *client)
416 if ((err = i2c_detach_client(client))) {
417 dev_err(&client->dev, "Client deregistration failed, "
418 "client not detached.\n");
422 kfree(i2c_get_clientdata(client));
426 static struct lm90_data *lm90_update_device(struct device *dev)
428 struct i2c_client *client = to_i2c_client(dev);
429 struct lm90_data *data = i2c_get_clientdata(client);
431 down(&data->update_lock);
433 if ((jiffies - data->last_updated > HZ * 2) ||
434 (jiffies < data->last_updated) ||
438 dev_dbg(&client->dev, "Updating lm90 data.\n");
439 data->temp_input1 = i2c_smbus_read_byte_data(client,
440 LM90_REG_R_LOCAL_TEMP);
441 data->temp_high1 = i2c_smbus_read_byte_data(client,
442 LM90_REG_R_LOCAL_HIGH);
443 data->temp_low1 = i2c_smbus_read_byte_data(client,
444 LM90_REG_R_LOCAL_LOW);
445 data->temp_crit1 = i2c_smbus_read_byte_data(client,
446 LM90_REG_R_LOCAL_CRIT);
447 data->temp_crit2 = i2c_smbus_read_byte_data(client,
448 LM90_REG_R_REMOTE_CRIT);
449 data->temp_hyst = i2c_smbus_read_byte_data(client,
450 LM90_REG_R_TCRIT_HYST);
453 * There is a trick here. We have to read two registers to
454 * have the remote sensor temperature, but we have to beware
455 * a conversion could occur inbetween the readings. The
456 * datasheet says we should either use the one-shot
457 * conversion register, which we don't want to do (disables
458 * hardware monitoring) or monitor the busy bit, which is
459 * impossible (we can't read the values and monitor that bit
460 * at the exact same time). So the solution used here is to
461 * read the high byte once, then the low byte, then the high
462 * byte again. If the new high byte matches the old one,
463 * then we have a valid reading. Else we have to read the low
464 * byte again, and now we believe we have a correct reading.
466 oldh = i2c_smbus_read_byte_data(client,
467 LM90_REG_R_REMOTE_TEMPH);
468 data->temp_input2 = i2c_smbus_read_byte_data(client,
469 LM90_REG_R_REMOTE_TEMPL);
470 newh = i2c_smbus_read_byte_data(client,
471 LM90_REG_R_REMOTE_TEMPH);
473 data->temp_input2 = i2c_smbus_read_byte_data(client,
474 LM90_REG_R_REMOTE_TEMPL);
476 oldh = i2c_smbus_read_byte_data(client,
477 LM90_REG_R_REMOTE_TEMPH);
478 /* oldh is actually newer */
480 dev_warn(&client->dev, "Remote temperature may be "
484 data->temp_input2 |= (newh << 8);
486 data->temp_high2 = (i2c_smbus_read_byte_data(client,
487 LM90_REG_R_REMOTE_HIGHH) << 8) +
488 i2c_smbus_read_byte_data(client,
489 LM90_REG_R_REMOTE_HIGHL);
490 data->temp_low2 = (i2c_smbus_read_byte_data(client,
491 LM90_REG_R_REMOTE_LOWH) << 8) +
492 i2c_smbus_read_byte_data(client,
493 LM90_REG_R_REMOTE_LOWL);
494 data->alarms = i2c_smbus_read_byte_data(client,
497 data->last_updated = jiffies;
501 up(&data->update_lock);
506 static int __init sensors_lm90_init(void)
508 return i2c_add_driver(&lm90_driver);
511 static void __exit sensors_lm90_exit(void)
513 i2c_del_driver(&lm90_driver);
516 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
517 MODULE_DESCRIPTION("LM90/ADM1032 driver");
518 MODULE_LICENSE("GPL");
520 module_init(sensors_lm90_init);
521 module_exit(sensors_lm90_exit);