1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
5 * The LM95245 is a sensor chip made by TI / National Semiconductor.
6 * It reports up to two temperatures (its own plus an external one).
8 * This driver is based on lm95241.c
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/hwmon.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
20 static const unsigned short normal_i2c
[] = {
21 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END
};
23 /* LM95245 registers */
24 /* general registers */
25 #define LM95245_REG_RW_CONFIG1 0x03
26 #define LM95245_REG_RW_CONVERS_RATE 0x04
27 #define LM95245_REG_W_ONE_SHOT 0x0F
29 /* diode configuration */
30 #define LM95245_REG_RW_CONFIG2 0xBF
31 #define LM95245_REG_RW_REMOTE_OFFH 0x11
32 #define LM95245_REG_RW_REMOTE_OFFL 0x12
34 /* status registers */
35 #define LM95245_REG_R_STATUS1 0x02
36 #define LM95245_REG_R_STATUS2 0x33
39 #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
40 #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
41 #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
42 #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
44 /* temperature signed */
45 #define LM95245_REG_R_LOCAL_TEMPH_S 0x00
46 #define LM95245_REG_R_LOCAL_TEMPL_S 0x30
47 #define LM95245_REG_R_REMOTE_TEMPH_S 0x01
48 #define LM95245_REG_R_REMOTE_TEMPL_S 0x10
49 /* temperature unsigned */
50 #define LM95245_REG_R_REMOTE_TEMPH_U 0x31
51 #define LM95245_REG_R_REMOTE_TEMPL_U 0x32
54 #define LM95245_REG_R_MAN_ID 0xFE
55 #define LM95245_REG_R_CHIP_ID 0xFF
57 /* LM95245 specific bitfields */
59 #define CFG_REMOTE_TCRIT_MASK 0x10
60 #define CFG_REMOTE_OS_MASK 0x08
61 #define CFG_LOCAL_TCRIT_MASK 0x04
62 #define CFG_LOCAL_OS_MASK 0x02
64 #define CFG2_OS_A0 0x40
65 #define CFG2_DIODE_FAULT_OS 0x20
66 #define CFG2_DIODE_FAULT_TCRIT 0x10
67 #define CFG2_REMOTE_TT 0x08
68 #define CFG2_REMOTE_FILTER_DIS 0x00
69 #define CFG2_REMOTE_FILTER_EN 0x06
71 /* conversation rate in ms */
72 #define RATE_CR0063 0x00
73 #define RATE_CR0364 0x01
74 #define RATE_CR1000 0x02
75 #define RATE_CR2500 0x03
77 #define STATUS1_ROS 0x10
78 #define STATUS1_DIODE_FAULT 0x04
79 #define STATUS1_RTCRIT 0x02
80 #define STATUS1_LOC 0x01
82 #define MANUFACTURER_ID 0x01
83 #define LM95235_REVISION 0xB1
84 #define LM95245_REVISION 0xB3
86 /* Client data (each client gets its own) */
88 struct regmap
*regmap
;
89 struct mutex update_lock
;
90 int interval
; /* in msecs */
94 static int temp_from_reg_unsigned(u8 val_h
, u8 val_l
)
96 return val_h
* 1000 + val_l
* 1000 / 256;
99 static int temp_from_reg_signed(u8 val_h
, u8 val_l
)
102 return (val_h
- 0x100) * 1000;
103 return temp_from_reg_unsigned(val_h
, val_l
);
106 static int lm95245_read_conversion_rate(struct lm95245_data
*data
)
111 ret
= regmap_read(data
->regmap
, LM95245_REG_RW_CONVERS_RATE
, &rate
);
120 data
->interval
= 364;
123 data
->interval
= 1000;
127 data
->interval
= 2500;
133 static int lm95245_set_conversion_rate(struct lm95245_data
*data
, long interval
)
137 if (interval
<= 63) {
140 } else if (interval
<= 364) {
143 } else if (interval
<= 1000) {
151 ret
= regmap_write(data
->regmap
, LM95245_REG_RW_CONVERS_RATE
, rate
);
155 data
->interval
= interval
;
159 static int lm95245_read_temp(struct device
*dev
, u32 attr
, int channel
,
162 struct lm95245_data
*data
= dev_get_drvdata(dev
);
163 struct regmap
*regmap
= data
->regmap
;
164 int ret
, regl
, regh
, regvall
, regvalh
;
167 case hwmon_temp_input
:
168 regl
= channel
? LM95245_REG_R_REMOTE_TEMPL_S
:
169 LM95245_REG_R_LOCAL_TEMPL_S
;
170 regh
= channel
? LM95245_REG_R_REMOTE_TEMPH_S
:
171 LM95245_REG_R_LOCAL_TEMPH_S
;
172 ret
= regmap_read(regmap
, regl
, ®vall
);
175 ret
= regmap_read(regmap
, regh
, ®valh
);
179 * Local temp is always signed.
180 * Remote temp has both signed and unsigned data.
181 * Use signed calculation for remote if signed bit is set
182 * or if reported temperature is below signed limit.
184 if (!channel
|| (regvalh
& 0x80) || regvalh
< 0x7f) {
185 *val
= temp_from_reg_signed(regvalh
, regvall
);
188 ret
= regmap_read(regmap
, LM95245_REG_R_REMOTE_TEMPL_U
,
192 ret
= regmap_read(regmap
, LM95245_REG_R_REMOTE_TEMPH_U
,
196 *val
= temp_from_reg_unsigned(regvalh
, regvall
);
199 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
,
203 *val
= regvalh
* 1000;
205 case hwmon_temp_crit
:
206 regh
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
207 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
208 ret
= regmap_read(regmap
, regh
, ®valh
);
211 *val
= regvalh
* 1000;
213 case hwmon_temp_max_hyst
:
214 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
,
218 ret
= regmap_read(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
222 *val
= (regvalh
- regvall
) * 1000;
224 case hwmon_temp_crit_hyst
:
225 regh
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
226 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
227 ret
= regmap_read(regmap
, regh
, ®valh
);
230 ret
= regmap_read(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
234 *val
= (regvalh
- regvall
) * 1000;
236 case hwmon_temp_type
:
237 ret
= regmap_read(regmap
, LM95245_REG_RW_CONFIG2
, ®valh
);
240 *val
= (regvalh
& CFG2_REMOTE_TT
) ? 1 : 2;
242 case hwmon_temp_offset
:
243 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OFFL
,
247 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OFFH
,
251 *val
= temp_from_reg_signed(regvalh
, regvall
);
253 case hwmon_temp_max_alarm
:
254 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
257 *val
= !!(regvalh
& STATUS1_ROS
);
259 case hwmon_temp_crit_alarm
:
260 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
263 *val
= !!(regvalh
& (channel
? STATUS1_RTCRIT
: STATUS1_LOC
));
265 case hwmon_temp_fault
:
266 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
269 *val
= !!(regvalh
& STATUS1_DIODE_FAULT
);
276 static int lm95245_write_temp(struct device
*dev
, u32 attr
, int channel
,
279 struct lm95245_data
*data
= dev_get_drvdata(dev
);
280 struct regmap
*regmap
= data
->regmap
;
286 val
= clamp_val(val
/ 1000, 0, 255);
287 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
, val
);
289 case hwmon_temp_crit
:
290 reg
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
291 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
292 val
= clamp_val(val
/ 1000, 0, channel
? 255 : 127);
293 ret
= regmap_write(regmap
, reg
, val
);
295 case hwmon_temp_crit_hyst
:
296 mutex_lock(&data
->update_lock
);
297 ret
= regmap_read(regmap
, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
,
300 mutex_unlock(&data
->update_lock
);
303 /* Clamp to reasonable range to prevent overflow */
304 val
= clamp_val(val
, -1000000, 1000000);
305 val
= regval
- val
/ 1000;
306 val
= clamp_val(val
, 0, 31);
307 ret
= regmap_write(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
309 mutex_unlock(&data
->update_lock
);
311 case hwmon_temp_offset
:
312 val
= clamp_val(val
, -128000, 127875);
313 val
= val
* 256 / 1000;
314 mutex_lock(&data
->update_lock
);
315 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OFFL
,
318 mutex_unlock(&data
->update_lock
);
321 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OFFH
,
323 mutex_unlock(&data
->update_lock
);
325 case hwmon_temp_type
:
326 if (val
!= 1 && val
!= 2)
328 ret
= regmap_update_bits(regmap
, LM95245_REG_RW_CONFIG2
,
330 val
== 1 ? CFG2_REMOTE_TT
: 0);
337 static int lm95245_read_chip(struct device
*dev
, u32 attr
, int channel
,
340 struct lm95245_data
*data
= dev_get_drvdata(dev
);
343 case hwmon_chip_update_interval
:
344 *val
= data
->interval
;
351 static int lm95245_write_chip(struct device
*dev
, u32 attr
, int channel
,
354 struct lm95245_data
*data
= dev_get_drvdata(dev
);
358 case hwmon_chip_update_interval
:
359 mutex_lock(&data
->update_lock
);
360 ret
= lm95245_set_conversion_rate(data
, val
);
361 mutex_unlock(&data
->update_lock
);
368 static int lm95245_read(struct device
*dev
, enum hwmon_sensor_types type
,
369 u32 attr
, int channel
, long *val
)
373 return lm95245_read_chip(dev
, attr
, channel
, val
);
375 return lm95245_read_temp(dev
, attr
, channel
, val
);
381 static int lm95245_write(struct device
*dev
, enum hwmon_sensor_types type
,
382 u32 attr
, int channel
, long val
)
386 return lm95245_write_chip(dev
, attr
, channel
, val
);
388 return lm95245_write_temp(dev
, attr
, channel
, val
);
394 static umode_t
lm95245_temp_is_visible(const void *data
, u32 attr
, int channel
)
397 case hwmon_temp_input
:
398 case hwmon_temp_max_alarm
:
399 case hwmon_temp_max_hyst
:
400 case hwmon_temp_crit_alarm
:
401 case hwmon_temp_fault
:
403 case hwmon_temp_type
:
405 case hwmon_temp_crit
:
406 case hwmon_temp_offset
:
408 case hwmon_temp_crit_hyst
:
409 return (channel
== 0) ? 0644 : 0444;
415 static umode_t
lm95245_is_visible(const void *data
,
416 enum hwmon_sensor_types type
,
417 u32 attr
, int channel
)
422 case hwmon_chip_update_interval
:
428 return lm95245_temp_is_visible(data
, attr
, channel
);
434 /* Return 0 if detection is successful, -ENODEV otherwise */
435 static int lm95245_detect(struct i2c_client
*new_client
,
436 struct i2c_board_info
*info
)
438 struct i2c_adapter
*adapter
= new_client
->adapter
;
439 int address
= new_client
->addr
;
443 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
446 id
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_MAN_ID
);
447 if (id
!= MANUFACTURER_ID
)
450 rev
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_CHIP_ID
);
452 case LM95235_REVISION
:
453 if (address
!= 0x18 && address
!= 0x29 && address
!= 0x4c)
457 case LM95245_REVISION
:
464 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
468 static int lm95245_init_client(struct lm95245_data
*data
)
472 ret
= lm95245_read_conversion_rate(data
);
476 return regmap_update_bits(data
->regmap
, LM95245_REG_RW_CONFIG1
,
480 static bool lm95245_is_writeable_reg(struct device
*dev
, unsigned int reg
)
483 case LM95245_REG_RW_CONFIG1
:
484 case LM95245_REG_RW_CONVERS_RATE
:
485 case LM95245_REG_W_ONE_SHOT
:
486 case LM95245_REG_RW_CONFIG2
:
487 case LM95245_REG_RW_REMOTE_OFFH
:
488 case LM95245_REG_RW_REMOTE_OFFL
:
489 case LM95245_REG_RW_REMOTE_OS_LIMIT
:
490 case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
:
491 case LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
492 case LM95245_REG_RW_COMMON_HYSTERESIS
:
499 static bool lm95245_is_volatile_reg(struct device
*dev
, unsigned int reg
)
502 case LM95245_REG_R_STATUS1
:
503 case LM95245_REG_R_STATUS2
:
504 case LM95245_REG_R_LOCAL_TEMPH_S
:
505 case LM95245_REG_R_LOCAL_TEMPL_S
:
506 case LM95245_REG_R_REMOTE_TEMPH_S
:
507 case LM95245_REG_R_REMOTE_TEMPL_S
:
508 case LM95245_REG_R_REMOTE_TEMPH_U
:
509 case LM95245_REG_R_REMOTE_TEMPL_U
:
516 static const struct regmap_config lm95245_regmap_config
= {
519 .writeable_reg
= lm95245_is_writeable_reg
,
520 .volatile_reg
= lm95245_is_volatile_reg
,
521 .cache_type
= REGCACHE_RBTREE
,
522 .use_single_read
= true,
523 .use_single_write
= true,
526 static const struct hwmon_channel_info
*lm95245_info
[] = {
527 HWMON_CHANNEL_INFO(chip
,
528 HWMON_C_UPDATE_INTERVAL
),
529 HWMON_CHANNEL_INFO(temp
,
530 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_HYST
|
532 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MAX_HYST
|
533 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_FAULT
|
534 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
535 HWMON_T_TYPE
| HWMON_T_OFFSET
),
539 static const struct hwmon_ops lm95245_hwmon_ops
= {
540 .is_visible
= lm95245_is_visible
,
541 .read
= lm95245_read
,
542 .write
= lm95245_write
,
545 static const struct hwmon_chip_info lm95245_chip_info
= {
546 .ops
= &lm95245_hwmon_ops
,
547 .info
= lm95245_info
,
550 static int lm95245_probe(struct i2c_client
*client
)
552 struct device
*dev
= &client
->dev
;
553 struct lm95245_data
*data
;
554 struct device
*hwmon_dev
;
557 data
= devm_kzalloc(dev
, sizeof(struct lm95245_data
), GFP_KERNEL
);
561 data
->regmap
= devm_regmap_init_i2c(client
, &lm95245_regmap_config
);
562 if (IS_ERR(data
->regmap
))
563 return PTR_ERR(data
->regmap
);
565 mutex_init(&data
->update_lock
);
567 /* Initialize the LM95245 chip */
568 ret
= lm95245_init_client(data
);
572 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
576 return PTR_ERR_OR_ZERO(hwmon_dev
);
579 /* Driver data (common to all clients) */
580 static const struct i2c_device_id lm95245_id
[] = {
585 MODULE_DEVICE_TABLE(i2c
, lm95245_id
);
587 static const struct of_device_id __maybe_unused lm95245_of_match
[] = {
588 { .compatible
= "national,lm95235" },
589 { .compatible
= "national,lm95245" },
592 MODULE_DEVICE_TABLE(of
, lm95245_of_match
);
594 static struct i2c_driver lm95245_driver
= {
595 .class = I2C_CLASS_HWMON
,
598 .of_match_table
= of_match_ptr(lm95245_of_match
),
600 .probe_new
= lm95245_probe
,
601 .id_table
= lm95245_id
,
602 .detect
= lm95245_detect
,
603 .address_list
= normal_i2c
,
606 module_i2c_driver(lm95245_driver
);
608 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
609 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
610 MODULE_LICENSE("GPL");