2 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
4 * The LM95245 is a sensor chip made by TI / National Semiconductor.
5 * It reports up to two temperatures (its own plus an external one).
7 * This driver is based on lm95241.c
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 #include <linux/sysfs.h>
35 static const unsigned short normal_i2c
[] = {
36 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END
};
38 /* LM95245 registers */
39 /* general registers */
40 #define LM95245_REG_RW_CONFIG1 0x03
41 #define LM95245_REG_RW_CONVERS_RATE 0x04
42 #define LM95245_REG_W_ONE_SHOT 0x0F
44 /* diode configuration */
45 #define LM95245_REG_RW_CONFIG2 0xBF
46 #define LM95245_REG_RW_REMOTE_OFFH 0x11
47 #define LM95245_REG_RW_REMOTE_OFFL 0x12
49 /* status registers */
50 #define LM95245_REG_R_STATUS1 0x02
51 #define LM95245_REG_R_STATUS2 0x33
54 #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
55 #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
56 #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
57 #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
59 /* temperature signed */
60 #define LM95245_REG_R_LOCAL_TEMPH_S 0x00
61 #define LM95245_REG_R_LOCAL_TEMPL_S 0x30
62 #define LM95245_REG_R_REMOTE_TEMPH_S 0x01
63 #define LM95245_REG_R_REMOTE_TEMPL_S 0x10
64 /* temperature unsigned */
65 #define LM95245_REG_R_REMOTE_TEMPH_U 0x31
66 #define LM95245_REG_R_REMOTE_TEMPL_U 0x32
69 #define LM95245_REG_R_MAN_ID 0xFE
70 #define LM95245_REG_R_CHIP_ID 0xFF
72 /* LM95245 specific bitfields */
74 #define CFG_REMOTE_TCRIT_MASK 0x10
75 #define CFG_REMOTE_OS_MASK 0x08
76 #define CFG_LOCAL_TCRIT_MASK 0x04
77 #define CFG_LOCAL_OS_MASK 0x02
79 #define CFG2_OS_A0 0x40
80 #define CFG2_DIODE_FAULT_OS 0x20
81 #define CFG2_DIODE_FAULT_TCRIT 0x10
82 #define CFG2_REMOTE_TT 0x08
83 #define CFG2_REMOTE_FILTER_DIS 0x00
84 #define CFG2_REMOTE_FILTER_EN 0x06
86 /* conversation rate in ms */
87 #define RATE_CR0063 0x00
88 #define RATE_CR0364 0x01
89 #define RATE_CR1000 0x02
90 #define RATE_CR2500 0x03
92 #define STATUS1_DIODE_FAULT 0x04
93 #define STATUS1_RTCRIT 0x02
94 #define STATUS1_LOC 0x01
96 #define MANUFACTURER_ID 0x01
97 #define LM95235_REVISION 0xB1
98 #define LM95245_REVISION 0xB3
100 static const u8 lm95245_reg_address
[] = {
101 LM95245_REG_R_LOCAL_TEMPH_S
,
102 LM95245_REG_R_LOCAL_TEMPL_S
,
103 LM95245_REG_R_REMOTE_TEMPH_S
,
104 LM95245_REG_R_REMOTE_TEMPL_S
,
105 LM95245_REG_R_REMOTE_TEMPH_U
,
106 LM95245_REG_R_REMOTE_TEMPL_U
,
107 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
,
108 LM95245_REG_RW_REMOTE_TCRIT_LIMIT
,
109 LM95245_REG_RW_COMMON_HYSTERESIS
,
110 LM95245_REG_R_STATUS1
,
113 /* Client data (each client gets its own) */
114 struct lm95245_data
{
115 struct i2c_client
*client
;
116 struct mutex update_lock
;
117 unsigned long last_updated
; /* in jiffies */
118 unsigned long interval
; /* in msecs */
119 bool valid
; /* zero until following fields are valid */
120 /* registers values */
121 u8 regs
[ARRAY_SIZE(lm95245_reg_address
)];
126 static int temp_from_reg_unsigned(u8 val_h
, u8 val_l
)
128 return val_h
* 1000 + val_l
* 1000 / 256;
131 static int temp_from_reg_signed(u8 val_h
, u8 val_l
)
134 return (val_h
- 0x100) * 1000;
135 return temp_from_reg_unsigned(val_h
, val_l
);
138 static struct lm95245_data
*lm95245_update_device(struct device
*dev
)
140 struct lm95245_data
*data
= dev_get_drvdata(dev
);
141 struct i2c_client
*client
= data
->client
;
143 mutex_lock(&data
->update_lock
);
145 if (time_after(jiffies
, data
->last_updated
146 + msecs_to_jiffies(data
->interval
)) || !data
->valid
) {
149 for (i
= 0; i
< ARRAY_SIZE(lm95245_reg_address
); i
++)
151 = i2c_smbus_read_byte_data(client
,
152 lm95245_reg_address
[i
]);
153 data
->last_updated
= jiffies
;
157 mutex_unlock(&data
->update_lock
);
162 static unsigned long lm95245_read_conversion_rate(struct i2c_client
*client
)
165 unsigned long interval
;
167 rate
= i2c_smbus_read_byte_data(client
, LM95245_REG_RW_CONVERS_RATE
);
188 static unsigned long lm95245_set_conversion_rate(struct i2c_client
*client
,
189 unsigned long interval
)
193 if (interval
<= 63) {
196 } else if (interval
<= 364) {
199 } else if (interval
<= 1000) {
207 i2c_smbus_write_byte_data(client
, LM95245_REG_RW_CONVERS_RATE
, rate
);
213 static ssize_t
show_input(struct device
*dev
, struct device_attribute
*attr
,
216 struct lm95245_data
*data
= lm95245_update_device(dev
);
218 int index
= to_sensor_dev_attr(attr
)->index
;
221 * Index 0 (Local temp) is always signed
222 * Index 2 (Remote temp) has both signed and unsigned data
223 * use signed calculation for remote if signed bit is set
225 if (index
== 0 || data
->regs
[index
] & 0x80)
226 temp
= temp_from_reg_signed(data
->regs
[index
],
227 data
->regs
[index
+ 1]);
229 temp
= temp_from_reg_unsigned(data
->regs
[index
+ 2],
230 data
->regs
[index
+ 3]);
232 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n", temp
);
235 static ssize_t
show_limit(struct device
*dev
, struct device_attribute
*attr
,
238 struct lm95245_data
*data
= lm95245_update_device(dev
);
239 int index
= to_sensor_dev_attr(attr
)->index
;
241 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n",
242 data
->regs
[index
] * 1000);
245 static ssize_t
set_limit(struct device
*dev
, struct device_attribute
*attr
,
246 const char *buf
, size_t count
)
248 struct lm95245_data
*data
= dev_get_drvdata(dev
);
249 int index
= to_sensor_dev_attr(attr
)->index
;
250 struct i2c_client
*client
= data
->client
;
253 if (kstrtoul(buf
, 10, &val
) < 0)
258 val
= clamp_val(val
, 0, (index
== 6 ? 127 : 255));
260 mutex_lock(&data
->update_lock
);
264 i2c_smbus_write_byte_data(client
, lm95245_reg_address
[index
], val
);
266 mutex_unlock(&data
->update_lock
);
271 static ssize_t
show_crit_hyst(struct device
*dev
, struct device_attribute
*attr
,
274 struct lm95245_data
*data
= lm95245_update_device(dev
);
275 int index
= to_sensor_dev_attr(attr
)->index
;
276 int hyst
= data
->regs
[index
] - data
->regs
[8];
278 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n", hyst
* 1000);
281 static ssize_t
set_crit_hyst(struct device
*dev
, struct device_attribute
*attr
,
282 const char *buf
, size_t count
)
284 struct lm95245_data
*data
= dev_get_drvdata(dev
);
285 int index
= to_sensor_dev_attr(attr
)->index
;
286 struct i2c_client
*client
= data
->client
;
290 if (kstrtoul(buf
, 10, &val
) < 0)
293 mutex_lock(&data
->update_lock
);
295 limit
= i2c_smbus_read_byte_data(client
, lm95245_reg_address
[index
]);
296 hyst
= limit
- val
/ 1000;
297 hyst
= clamp_val(hyst
, 0, 31);
298 data
->regs
[8] = hyst
;
300 /* shared crit hysteresis */
301 i2c_smbus_write_byte_data(client
, LM95245_REG_RW_COMMON_HYSTERESIS
,
304 mutex_unlock(&data
->update_lock
);
309 static ssize_t
show_type(struct device
*dev
, struct device_attribute
*attr
,
312 struct lm95245_data
*data
= dev_get_drvdata(dev
);
314 return snprintf(buf
, PAGE_SIZE
- 1,
315 data
->config2
& CFG2_REMOTE_TT
? "1\n" : "2\n");
318 static ssize_t
set_type(struct device
*dev
, struct device_attribute
*attr
,
319 const char *buf
, size_t count
)
321 struct lm95245_data
*data
= dev_get_drvdata(dev
);
322 struct i2c_client
*client
= data
->client
;
325 if (kstrtoul(buf
, 10, &val
) < 0)
327 if (val
!= 1 && val
!= 2)
330 mutex_lock(&data
->update_lock
);
333 data
->config2
|= CFG2_REMOTE_TT
;
335 data
->config2
&= ~CFG2_REMOTE_TT
;
339 i2c_smbus_write_byte_data(client
, LM95245_REG_RW_CONFIG2
,
342 mutex_unlock(&data
->update_lock
);
347 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
350 struct lm95245_data
*data
= lm95245_update_device(dev
);
351 int index
= to_sensor_dev_attr(attr
)->index
;
353 return snprintf(buf
, PAGE_SIZE
- 1, "%d\n",
354 !!(data
->regs
[9] & index
));
357 static ssize_t
show_interval(struct device
*dev
, struct device_attribute
*attr
,
360 struct lm95245_data
*data
= lm95245_update_device(dev
);
362 return snprintf(buf
, PAGE_SIZE
- 1, "%lu\n", data
->interval
);
365 static ssize_t
set_interval(struct device
*dev
, struct device_attribute
*attr
,
366 const char *buf
, size_t count
)
368 struct lm95245_data
*data
= dev_get_drvdata(dev
);
369 struct i2c_client
*client
= data
->client
;
372 if (kstrtoul(buf
, 10, &val
) < 0)
375 mutex_lock(&data
->update_lock
);
377 data
->interval
= lm95245_set_conversion_rate(client
, val
);
379 mutex_unlock(&data
->update_lock
);
384 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_input
, NULL
, 0);
385 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
, show_limit
,
387 static SENSOR_DEVICE_ATTR(temp1_crit_hyst
, S_IWUSR
| S_IRUGO
, show_crit_hyst
,
389 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
392 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_input
, NULL
, 2);
393 static SENSOR_DEVICE_ATTR(temp2_crit
, S_IWUSR
| S_IRUGO
, show_limit
,
395 static SENSOR_DEVICE_ATTR(temp2_crit_hyst
, S_IRUGO
, show_crit_hyst
, NULL
, 7);
396 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
398 static SENSOR_DEVICE_ATTR(temp2_type
, S_IWUSR
| S_IRUGO
, show_type
,
400 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
,
401 STATUS1_DIODE_FAULT
);
403 static DEVICE_ATTR(update_interval
, S_IWUSR
| S_IRUGO
, show_interval
,
406 static struct attribute
*lm95245_attrs
[] = {
407 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
408 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
409 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
410 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
411 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
412 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
413 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
414 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
415 &sensor_dev_attr_temp2_type
.dev_attr
.attr
,
416 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
417 &dev_attr_update_interval
.attr
,
420 ATTRIBUTE_GROUPS(lm95245
);
422 /* Return 0 if detection is successful, -ENODEV otherwise */
423 static int lm95245_detect(struct i2c_client
*new_client
,
424 struct i2c_board_info
*info
)
426 struct i2c_adapter
*adapter
= new_client
->adapter
;
427 int address
= new_client
->addr
;
431 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
434 id
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_MAN_ID
);
435 if (id
!= MANUFACTURER_ID
)
438 rev
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_CHIP_ID
);
440 case LM95235_REVISION
:
441 if (address
!= 0x18 && address
!= 0x29 && address
!= 0x4c)
445 case LM95245_REVISION
:
452 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
456 static void lm95245_init_client(struct i2c_client
*client
,
457 struct lm95245_data
*data
)
459 data
->interval
= lm95245_read_conversion_rate(client
);
461 data
->config1
= i2c_smbus_read_byte_data(client
,
462 LM95245_REG_RW_CONFIG1
);
463 data
->config2
= i2c_smbus_read_byte_data(client
,
464 LM95245_REG_RW_CONFIG2
);
466 if (data
->config1
& CFG_STOP
) {
467 /* Clear the standby bit */
468 data
->config1
&= ~CFG_STOP
;
469 i2c_smbus_write_byte_data(client
, LM95245_REG_RW_CONFIG1
,
474 static int lm95245_probe(struct i2c_client
*client
,
475 const struct i2c_device_id
*id
)
477 struct device
*dev
= &client
->dev
;
478 struct lm95245_data
*data
;
479 struct device
*hwmon_dev
;
481 data
= devm_kzalloc(dev
, sizeof(struct lm95245_data
), GFP_KERNEL
);
485 data
->client
= client
;
486 mutex_init(&data
->update_lock
);
488 /* Initialize the LM95245 chip */
489 lm95245_init_client(client
, data
);
491 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
494 return PTR_ERR_OR_ZERO(hwmon_dev
);
497 /* Driver data (common to all clients) */
498 static const struct i2c_device_id lm95245_id
[] = {
503 MODULE_DEVICE_TABLE(i2c
, lm95245_id
);
505 static struct i2c_driver lm95245_driver
= {
506 .class = I2C_CLASS_HWMON
,
510 .probe
= lm95245_probe
,
511 .id_table
= lm95245_id
,
512 .detect
= lm95245_detect
,
513 .address_list
= normal_i2c
,
516 module_i2c_driver(lm95245_driver
);
518 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
519 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
520 MODULE_LICENSE("GPL");