2 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
4 * The LM95241 is a sensor chip made by National Semiconductors.
5 * It reports up to three temperatures (its own plus up to two external ones).
6 * Complete datasheet can be obtained from National's website at:
7 * http://www.national.com/ds.cgi/LM/LM95241.pdf
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/bitops.h>
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/jiffies.h>
25 #include <linux/hwmon.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
30 #define DEVNAME "lm95241"
32 static const unsigned short normal_i2c[] = {
33 0x19, 0x2a, 0x2b, I2C_CLIENT_END };
35 /* LM95241 registers */
36 #define LM95241_REG_R_MAN_ID 0xFE
37 #define LM95241_REG_R_CHIP_ID 0xFF
38 #define LM95241_REG_R_STATUS 0x02
39 #define LM95241_REG_RW_CONFIG 0x03
40 #define LM95241_REG_RW_REM_FILTER 0x06
41 #define LM95241_REG_RW_TRUTHERM 0x07
42 #define LM95241_REG_W_ONE_SHOT 0x0F
43 #define LM95241_REG_R_LOCAL_TEMPH 0x10
44 #define LM95241_REG_R_REMOTE1_TEMPH 0x11
45 #define LM95241_REG_R_REMOTE2_TEMPH 0x12
46 #define LM95241_REG_R_LOCAL_TEMPL 0x20
47 #define LM95241_REG_R_REMOTE1_TEMPL 0x21
48 #define LM95241_REG_R_REMOTE2_TEMPL 0x22
49 #define LM95241_REG_RW_REMOTE_MODEL 0x30
51 /* LM95241 specific bitfields */
52 #define CFG_STOP BIT(6)
53 #define CFG_CR0076 0x00
54 #define CFG_CR0182 BIT(4)
55 #define CFG_CR1000 BIT(5)
56 #define CFG_CR2700 (BIT(4) | BIT(5))
57 #define CFG_CRMASK (BIT(4) | BIT(5))
58 #define R1MS_MASK BIT(0)
59 #define R2MS_MASK BIT(2)
60 #define R1DF_MASK BIT(1)
61 #define R2DF_MASK BIT(2)
62 #define R1FE_MASK BIT(0)
63 #define R2FE_MASK BIT(2)
71 #define NATSEMI_MAN_ID 0x01
72 #define LM95231_CHIP_ID 0xA1
73 #define LM95241_CHIP_ID 0xA4
75 static const u8 lm95241_reg_address[] = {
76 LM95241_REG_R_LOCAL_TEMPH,
77 LM95241_REG_R_LOCAL_TEMPL,
78 LM95241_REG_R_REMOTE1_TEMPH,
79 LM95241_REG_R_REMOTE1_TEMPL,
80 LM95241_REG_R_REMOTE2_TEMPH,
81 LM95241_REG_R_REMOTE2_TEMPL
84 /* Client data (each client gets its own) */
86 struct i2c_client *client;
87 struct mutex update_lock;
88 unsigned long last_updated; /* in jiffies */
89 unsigned long interval; /* in milli-seconds */
90 char valid; /* zero until following fields are valid */
91 /* registers values */
92 u8 temp[ARRAY_SIZE(lm95241_reg_address)];
93 u8 status, config, model, trutherm;
97 static int temp_from_reg_signed(u8 val_h, u8 val_l)
99 s16 val_hl = (val_h << 8) | val_l;
100 return val_hl * 1000 / 256;
103 static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
105 u16 val_hl = (val_h << 8) | val_l;
106 return val_hl * 1000 / 256;
109 static struct lm95241_data *lm95241_update_device(struct device *dev)
111 struct lm95241_data *data = dev_get_drvdata(dev);
112 struct i2c_client *client = data->client;
114 mutex_lock(&data->update_lock);
116 if (time_after(jiffies, data->last_updated
117 + msecs_to_jiffies(data->interval)) ||
121 dev_dbg(dev, "Updating lm95241 data.\n");
122 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
124 = i2c_smbus_read_byte_data(client,
125 lm95241_reg_address[i]);
127 data->status = i2c_smbus_read_byte_data(client,
128 LM95241_REG_R_STATUS);
129 data->last_updated = jiffies;
133 mutex_unlock(&data->update_lock);
138 static int lm95241_read_chip(struct device *dev, u32 attr, int channel,
141 struct lm95241_data *data = dev_get_drvdata(dev);
144 case hwmon_chip_update_interval:
145 *val = data->interval;
152 static int lm95241_read_temp(struct device *dev, u32 attr, int channel,
155 struct lm95241_data *data = lm95241_update_device(dev);
158 case hwmon_temp_input:
159 if (!channel || (data->config & BIT(channel - 1)))
160 *val = temp_from_reg_signed(data->temp[channel * 2],
161 data->temp[channel * 2 + 1]);
163 *val = temp_from_reg_unsigned(data->temp[channel * 2],
164 data->temp[channel * 2 + 1]);
168 *val = (data->config & R1DF_MASK) ? -128000 : 0;
170 *val = (data->config & R2DF_MASK) ? -128000 : 0;
174 *val = (data->config & R1DF_MASK) ? 127875 : 255875;
176 *val = (data->config & R2DF_MASK) ? 127875 : 255875;
178 case hwmon_temp_type:
180 *val = (data->model & R1MS_MASK) ? 1 : 2;
182 *val = (data->model & R2MS_MASK) ? 1 : 2;
184 case hwmon_temp_fault:
186 *val = !!(data->status & R1DM);
188 *val = !!(data->status & R2DM);
195 static int lm95241_read(struct device *dev, enum hwmon_sensor_types type,
196 u32 attr, int channel, long *val)
200 return lm95241_read_chip(dev, attr, channel, val);
202 return lm95241_read_temp(dev, attr, channel, val);
208 static int lm95241_write_chip(struct device *dev, u32 attr, int channel,
211 struct lm95241_data *data = dev_get_drvdata(dev);
216 mutex_lock(&data->update_lock);
219 case hwmon_chip_update_interval:
220 config = data->config & ~CFG_CRMASK;
223 config |= CFG_CR0076;
224 } else if (val < 590) {
226 config |= CFG_CR0182;
227 } else if (val < 1850) {
229 config |= CFG_CR1000;
232 config |= CFG_CR2700;
234 data->interval = convrate;
235 data->config = config;
236 ret = i2c_smbus_write_byte_data(data->client,
237 LM95241_REG_RW_CONFIG, config);
243 mutex_unlock(&data->update_lock);
247 static int lm95241_write_temp(struct device *dev, u32 attr, int channel,
250 struct lm95241_data *data = dev_get_drvdata(dev);
251 struct i2c_client *client = data->client;
254 mutex_lock(&data->update_lock);
260 data->config |= R1DF_MASK;
262 data->config &= ~R1DF_MASK;
265 data->config |= R2DF_MASK;
267 data->config &= ~R2DF_MASK;
270 ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
276 data->config |= R1DF_MASK;
278 data->config &= ~R1DF_MASK;
281 data->config |= R2DF_MASK;
283 data->config &= ~R2DF_MASK;
286 ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
289 case hwmon_temp_type:
290 if (val != 1 && val != 2) {
295 data->trutherm &= ~(TT_MASK << TT1_SHIFT);
297 data->model |= R1MS_MASK;
298 data->trutherm |= (TT_ON << TT1_SHIFT);
300 data->model &= ~R1MS_MASK;
301 data->trutherm |= (TT_OFF << TT1_SHIFT);
304 data->trutherm &= ~(TT_MASK << TT2_SHIFT);
306 data->model |= R2MS_MASK;
307 data->trutherm |= (TT_ON << TT2_SHIFT);
309 data->model &= ~R2MS_MASK;
310 data->trutherm |= (TT_OFF << TT2_SHIFT);
313 ret = i2c_smbus_write_byte_data(client,
314 LM95241_REG_RW_REMOTE_MODEL,
318 ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
326 mutex_unlock(&data->update_lock);
331 static int lm95241_write(struct device *dev, enum hwmon_sensor_types type,
332 u32 attr, int channel, long val)
336 return lm95241_write_chip(dev, attr, channel, val);
338 return lm95241_write_temp(dev, attr, channel, val);
344 static umode_t lm95241_is_visible(const void *data,
345 enum hwmon_sensor_types type,
346 u32 attr, int channel)
351 case hwmon_chip_update_interval:
352 return S_IRUGO | S_IWUSR;
357 case hwmon_temp_input:
359 case hwmon_temp_fault:
363 case hwmon_temp_type:
364 return S_IRUGO | S_IWUSR;
373 /* Return 0 if detection is successful, -ENODEV otherwise */
374 static int lm95241_detect(struct i2c_client *new_client,
375 struct i2c_board_info *info)
377 struct i2c_adapter *adapter = new_client->adapter;
381 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
384 mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
385 if (mfg_id != NATSEMI_MAN_ID)
388 chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
390 case LM95231_CHIP_ID:
393 case LM95241_CHIP_ID:
400 /* Fill the i2c board info */
401 strlcpy(info->type, name, I2C_NAME_SIZE);
405 static void lm95241_init_client(struct i2c_client *client,
406 struct lm95241_data *data)
408 data->interval = 1000;
409 data->config = CFG_CR1000;
410 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
412 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
413 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
414 R1FE_MASK | R2FE_MASK);
415 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
417 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
421 static const u32 lm95241_chip_config[] = {
422 HWMON_C_UPDATE_INTERVAL,
426 static const struct hwmon_channel_info lm95241_chip = {
428 .config = lm95241_chip_config,
431 static const u32 lm95241_temp_config[] = {
433 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
435 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
440 static const struct hwmon_channel_info lm95241_temp = {
442 .config = lm95241_temp_config,
445 static const struct hwmon_channel_info *lm95241_info[] = {
451 static const struct hwmon_ops lm95241_hwmon_ops = {
452 .is_visible = lm95241_is_visible,
453 .read = lm95241_read,
454 .write = lm95241_write,
457 static const struct hwmon_chip_info lm95241_chip_info = {
458 .ops = &lm95241_hwmon_ops,
459 .info = lm95241_info,
462 static int lm95241_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
465 struct device *dev = &client->dev;
466 struct lm95241_data *data;
467 struct device *hwmon_dev;
469 data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
473 data->client = client;
474 mutex_init(&data->update_lock);
476 /* Initialize the LM95241 chip */
477 lm95241_init_client(client, data);
479 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
483 return PTR_ERR_OR_ZERO(hwmon_dev);
486 /* Driver data (common to all clients) */
487 static const struct i2c_device_id lm95241_id[] = {
492 MODULE_DEVICE_TABLE(i2c, lm95241_id);
494 static struct i2c_driver lm95241_driver = {
495 .class = I2C_CLASS_HWMON,
499 .probe = lm95241_probe,
500 .id_table = lm95241_id,
501 .detect = lm95241_detect,
502 .address_list = normal_i2c,
505 module_i2c_driver(lm95241_driver);
507 MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
508 MODULE_DESCRIPTION("LM95231/LM95241 sensor driver");
509 MODULE_LICENSE("GPL");