2 * lm95231.c - Support for LM95231 temperature sensor, based on LM95241 driver.
4 * Copyright (C) 2012 Evgeny Kravtsunov <kravtsunov_e@mcst.ru>
6 * The LM95245 is a sensor chip made by National Semiconductors.
7 * Complete datasheet can be obtained from National's website at:
8 * http://www.national.com/pf/LM/LM95231.html
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/sysfs.h>
36 static const unsigned short normal_i2c
[] = {
37 0x19, 0x2b, I2C_CLIENT_END
};
39 /* LM95231 registers */
40 #define LM95231_REG_R_MAN_ID 0xFE
41 #define LM95231_REG_R_CHIP_ID 0xFF
42 #define LM95231_REG_R_STATUS 0x02
43 #define LM95231_REG_RW_CONFIG 0x03
44 #define LM95231_REG_RW_REM_FILTER 0x06
45 #define LM95231_REG_RW_TRUTHERM 0x07
46 #define LM95231_REG_W_ONE_SHOT 0x0F
47 #define LM95231_REG_R_LOCAL_TEMPH 0x10
48 #define LM95231_REG_R_REMOTE1_TEMPH 0x11
49 #define LM95231_REG_R_REMOTE2_TEMPH 0x12
50 #define LM95231_REG_R_LOCAL_TEMPL 0x20
51 #define LM95231_REG_R_REMOTE1_TEMPL 0x21
52 #define LM95231_REG_R_REMOTE2_TEMPL 0x22
53 #define LM95231_REG_RW_REMOTE_MODEL 0x30
55 /* LM95231 specific bitfields */
57 #define CFG_CR0076 0x00
58 #define CFG_CR0182 0x10
59 #define CFG_CR1000 0x20
60 #define CFG_CR2700 0x30
63 #define R1MS_MASK (0x01 << (R1MS_SHIFT))
64 #define R2MS_MASK (0x01 << (R2MS_SHIFT))
67 #define R1DF_MASK (0x01 << (R1DF_SHIFT))
68 #define R2DF_MASK (0x01 << (R2DF_SHIFT))
69 #define R1FE_MASK 0x01
70 #define R2FE_MASK 0x04
76 #define MANUFACTURER_ID 0x01
77 #define DEFAULT_REVISION 0xA1
79 /* Conversions and various macros */
80 #define TEMP_FROM_REG(val_h, val_l) \
81 (((val_h) & 0x80 ? (char)((~(val_h - 1)) * (-1)) : \
82 (val_h)) * 1000 + (val_l) * 1000 / 256)
84 /* Functions declaration */
85 static void lm95231_init_client(struct i2c_client
*client
);
86 static struct lm95231_data
*lm95231_update_device(struct device
*dev
);
88 /* Client data (each client gets its own) */
90 struct device
*hwmon_dev
;
91 struct mutex update_lock
;
92 unsigned long last_updated
, rate
; /* in jiffies */
93 char valid
; /* zero until following fields are valid */
94 /* registers values */
95 s32 local_h
, local_l
; /* local */
96 s32 remote1_h
, remote1_l
; /* remote1 */
97 s32 remote2_h
, remote2_l
; /* remote2 */
98 s32 local_h_st
, local_l_st
; /* status of i2c transaction
100 s32 remote1_h_st
; /* status of i2c transaction remote1_h */
101 s32 remote1_l_st
; /* status of i2c transaction remote1_l */
102 s32 remote2_h_st
; /* status of i2c transaction remote2_h */
103 s32 remote2_l_st
; /* status of i2c transaction remote2_l */
104 u8 config
, model
, trutherm
;
108 #define show_temp(value) \
109 static ssize_t show_##value(struct device *dev, \
110 struct device_attribute *attr, char *buf) \
112 struct lm95231_data *data = lm95231_update_device(dev); \
114 if (data->value##_h_st < 0 || data->value##_l_st < 0) \
115 snprintf(buf, PAGE_SIZE - 1, "Error\n"); \
117 snprintf(buf, PAGE_SIZE - 1, "%d\n", \
118 TEMP_FROM_REG(data->value##_h, data->value##_l)); \
119 return strlen(buf); \
125 static ssize_t
show_rate(struct device
*dev
, struct device_attribute
*attr
,
128 struct lm95231_data
*data
= lm95231_update_device(dev
);
130 snprintf(buf
, PAGE_SIZE
- 1, "%lu\n", 1000 * data
->rate
/ HZ
);
134 static ssize_t
set_rate(struct device
*dev
, struct device_attribute
*attr
,
135 const char *buf
, size_t count
)
137 struct i2c_client
*client
= to_i2c_client(dev
);
138 struct lm95231_data
*data
= i2c_get_clientdata(client
);
140 strict_strtol(buf
, 10, &data
->rate
);
141 data
->rate
= data
->rate
* HZ
/ 1000;
146 #define show_type(flag) \
147 static ssize_t show_type##flag(struct device *dev, \
148 struct device_attribute *attr, char *buf) \
150 struct i2c_client *client = to_i2c_client(dev); \
151 struct lm95231_data *data = i2c_get_clientdata(client); \
153 snprintf(buf, PAGE_SIZE - 1, \
154 data->model & R##flag##MS_MASK ? "1\n" : "2\n"); \
155 return strlen(buf); \
160 #define show_min(flag) \
161 static ssize_t show_min##flag(struct device *dev, \
162 struct device_attribute *attr, char *buf) \
164 struct i2c_client *client = to_i2c_client(dev); \
165 struct lm95231_data *data = i2c_get_clientdata(client); \
167 snprintf(buf, PAGE_SIZE - 1, \
168 data->config & R##flag##DF_MASK ? \
169 "-127000\n" : "0\n"); \
170 return strlen(buf); \
175 #define show_max(flag) \
176 static ssize_t show_max##flag(struct device *dev, \
177 struct device_attribute *attr, char *buf) \
179 struct i2c_client *client = to_i2c_client(dev); \
180 struct lm95231_data *data = i2c_get_clientdata(client); \
182 snprintf(buf, PAGE_SIZE - 1, \
183 data->config & R##flag##DF_MASK ? \
184 "127000\n" : "255000\n"); \
185 return strlen(buf); \
190 #define set_type(flag) \
191 static ssize_t set_type##flag(struct device *dev, \
192 struct device_attribute *attr, \
193 const char *buf, size_t count) \
195 struct i2c_client *client = to_i2c_client(dev); \
196 struct lm95231_data *data = i2c_get_clientdata(client); \
199 strict_strtol(buf, 10, &val); \
201 if ((val == 1) || (val == 2)) { \
203 mutex_lock(&data->update_lock); \
205 data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
207 data->model |= R##flag##MS_MASK; \
208 data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
211 data->model &= ~R##flag##MS_MASK; \
212 data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
217 i2c_smbus_write_byte_data(client, \
218 LM95231_REG_RW_REMOTE_MODEL, \
220 i2c_smbus_write_byte_data(client, \
221 LM95231_REG_RW_TRUTHERM, \
224 mutex_unlock(&data->update_lock); \
232 #define set_min(flag) \
233 static ssize_t set_min##flag(struct device *dev, \
234 struct device_attribute *devattr, const char *buf, \
237 struct i2c_client *client = to_i2c_client(dev); \
238 struct lm95231_data *data = i2c_get_clientdata(client); \
241 strict_strtol(buf, 10, &val); \
243 mutex_lock(&data->update_lock); \
246 data->config |= R##flag##DF_MASK; \
248 data->config &= ~R##flag##DF_MASK; \
252 i2c_smbus_write_byte_data(client, LM95231_REG_RW_CONFIG, \
255 mutex_unlock(&data->update_lock); \
262 #define set_max(flag) \
263 static ssize_t set_max##flag(struct device *dev, \
264 struct device_attribute *devattr, const char *buf, \
267 struct i2c_client *client = to_i2c_client(dev); \
268 struct lm95231_data *data = i2c_get_clientdata(client); \
271 strict_strtol(buf, 10, &val); \
273 mutex_lock(&data->update_lock); \
276 data->config |= R##flag##DF_MASK; \
278 data->config &= ~R##flag##DF_MASK; \
282 i2c_smbus_write_byte_data(client, LM95231_REG_RW_CONFIG, \
285 mutex_unlock(&data->update_lock); \
292 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_local
, NULL
);
293 static DEVICE_ATTR(temp2_input
, S_IRUGO
, show_remote1
, NULL
);
294 static DEVICE_ATTR(temp3_input
, S_IRUGO
, show_remote2
, NULL
);
295 static DEVICE_ATTR(temp2_type
, S_IWUSR
| S_IRUGO
, show_type1
, set_type1
);
296 static DEVICE_ATTR(temp3_type
, S_IWUSR
| S_IRUGO
, show_type2
, set_type2
);
297 static DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_min1
, set_min1
);
298 static DEVICE_ATTR(temp3_min
, S_IWUSR
| S_IRUGO
, show_min2
, set_min2
);
299 static DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_max1
, set_max1
);
300 static DEVICE_ATTR(temp3_max
, S_IWUSR
| S_IRUGO
, show_max2
, set_max2
);
301 static DEVICE_ATTR(rate
, S_IWUSR
| S_IRUGO
, show_rate
, set_rate
);
303 static struct attribute
*lm95231_attributes
[] = {
304 &dev_attr_temp1_input
.attr
,
305 &dev_attr_temp2_input
.attr
,
306 &dev_attr_temp3_input
.attr
,
307 &dev_attr_temp2_type
.attr
,
308 &dev_attr_temp3_type
.attr
,
309 &dev_attr_temp2_min
.attr
,
310 &dev_attr_temp3_min
.attr
,
311 &dev_attr_temp2_max
.attr
,
312 &dev_attr_temp3_max
.attr
,
317 static const struct attribute_group lm95231_group
= {
318 .attrs
= lm95231_attributes
,
321 /* Return 0 if detection is successful, -ENODEV otherwise */
322 static int lm95231_detect(struct i2c_client
*new_client
,
323 struct i2c_board_info
*info
)
325 struct i2c_adapter
*adapter
= new_client
->adapter
;
326 int address
= new_client
->addr
;
329 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
333 if ((i2c_smbus_read_byte_data(new_client
, LM95231_REG_R_MAN_ID
)
335 && (i2c_smbus_read_byte_data(new_client
, LM95231_REG_R_CHIP_ID
)
336 >= DEFAULT_REVISION
)) {
339 dev_dbg(&adapter
->dev
, "LM95231 detection failed at 0x%02x\n",
344 /* Fill the i2c board info */
345 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
349 static int lm95231_probe(struct i2c_client
*new_client
,
350 const struct i2c_device_id
*id
)
352 struct lm95231_data
*data
;
355 data
= kzalloc(sizeof(struct lm95231_data
), GFP_KERNEL
);
361 i2c_set_clientdata(new_client
, data
);
362 mutex_init(&data
->update_lock
);
364 /* Initialize the LM95231 chip */
365 lm95231_init_client(new_client
);
367 /* Register sysfs hooks */
368 err
= sysfs_create_group(&new_client
->dev
.kobj
, &lm95231_group
);
372 data
->hwmon_dev
= hwmon_device_register(&new_client
->dev
);
373 if (IS_ERR(data
->hwmon_dev
)) {
374 err
= PTR_ERR(data
->hwmon_dev
);
375 goto exit_remove_files
;
381 sysfs_remove_group(&new_client
->dev
.kobj
, &lm95231_group
);
388 static void lm95231_init_client(struct i2c_client
*client
)
390 struct lm95231_data
*data
= i2c_get_clientdata(client
);
392 data
->rate
= HZ
; /* 1 sec default */
394 data
->config
= CFG_CR0076
;
396 data
->trutherm
= (TT_OFF
<< TT1_SHIFT
) | (TT_OFF
<< TT2_SHIFT
);
398 i2c_smbus_write_byte_data(client
, LM95231_REG_RW_CONFIG
,
400 i2c_smbus_write_byte_data(client
, LM95231_REG_RW_REM_FILTER
,
401 R1FE_MASK
| R2FE_MASK
);
402 i2c_smbus_write_byte_data(client
, LM95231_REG_RW_TRUTHERM
,
404 i2c_smbus_write_byte_data(client
, LM95231_REG_RW_REMOTE_MODEL
,
408 static int lm95231_remove(struct i2c_client
*client
)
410 struct lm95231_data
*data
= i2c_get_clientdata(client
);
412 hwmon_device_unregister(data
->hwmon_dev
);
413 sysfs_remove_group(&client
->dev
.kobj
, &lm95231_group
);
415 i2c_set_clientdata(client
, NULL
);
420 static struct lm95231_data
*lm95231_update_device(struct device
*dev
)
422 struct i2c_client
*client
= to_i2c_client(dev
);
423 struct lm95231_data
*data
= i2c_get_clientdata(client
);
425 mutex_lock(&data
->update_lock
);
427 if (time_after(jiffies
, data
->last_updated
+ data
->rate
) ||
429 dev_dbg(&client
->dev
, "Updating lm95231 data.\n");
430 data
->local_h_st
= i2c_smbus_read_byte_data(client
,
431 LM95231_REG_R_LOCAL_TEMPH
);
432 if (data
->local_h_st
< 0)
435 data
->local_h
= data
->local_h_st
;
437 data
->local_l_st
= i2c_smbus_read_byte_data(client
,
438 LM95231_REG_R_LOCAL_TEMPL
);
439 if (data
->local_l_st
< 0)
442 data
->local_l
= data
->local_l_st
;
444 data
->remote1_h_st
= i2c_smbus_read_byte_data(client
,
445 LM95231_REG_R_REMOTE1_TEMPH
);
446 if (data
->remote1_h_st
< 0)
449 data
->remote1_h
= data
->remote1_h_st
;
451 data
->remote1_l_st
= i2c_smbus_read_byte_data(client
,
452 LM95231_REG_R_REMOTE1_TEMPL
);
453 if (data
->remote1_l_st
< 0)
456 data
->remote1_l
= (u8
) data
->remote1_l_st
;
458 data
->remote2_h_st
= i2c_smbus_read_byte_data(client
,
459 LM95231_REG_R_REMOTE2_TEMPH
);
460 if (data
->remote2_h_st
< 0)
463 data
->remote2_h
= data
->remote2_h_st
;
465 data
->remote2_l_st
= i2c_smbus_read_byte_data(client
,
466 LM95231_REG_R_REMOTE2_TEMPL
);
467 if (data
->remote2_l_st
< 0)
470 data
->remote2_l
= data
->remote2_l_st
;
472 data
->last_updated
= jiffies
;
476 mutex_unlock(&data
->update_lock
);
481 /* Driver data (common to all clients) */
482 static const struct i2c_device_id lm95231_id
[] = {
486 MODULE_DEVICE_TABLE(i2c
, lm95231_id
);
488 static struct i2c_driver lm95231_driver
= {
489 .class = I2C_CLASS_HWMON
,
493 .probe
= lm95231_probe
,
494 .remove
= lm95231_remove
,
495 .id_table
= lm95231_id
,
496 .detect
= lm95231_detect
,
497 .address_list
= normal_i2c
,
500 static int __init
sensors_lm95231_init(void)
502 return i2c_add_driver(&lm95231_driver
);
505 static void __exit
sensors_lm95231_exit(void)
507 i2c_del_driver(&lm95231_driver
);
510 MODULE_AUTHOR("Evgeny Kravtsunov <kravtsunov_e@mcst.ru>");
511 MODULE_DESCRIPTION("LM95231 sensor driver");
512 MODULE_LICENSE("GPL");
514 module_init(sensors_lm95231_init
);
515 module_exit(sensors_lm95231_exit
);