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 unsigned int regs
[2];
170 case hwmon_temp_input
:
171 regs
[0] = channel
? LM95245_REG_R_REMOTE_TEMPL_S
:
172 LM95245_REG_R_LOCAL_TEMPL_S
;
173 regs
[1] = channel
? LM95245_REG_R_REMOTE_TEMPH_S
:
174 LM95245_REG_R_LOCAL_TEMPH_S
;
175 ret
= regmap_multi_reg_read(regmap
, regs
, regvals
, 2);
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
|| (regvals
[1] & 0x80) || regvals
[1] < 0x7f) {
185 *val
= temp_from_reg_signed(regvals
[1], regvals
[0]);
188 ret
= regmap_bulk_read(regmap
, LM95245_REG_R_REMOTE_TEMPH_U
, regvals
, 2);
191 *val
= temp_from_reg_unsigned(regvals
[0], regvals
[1]);
194 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
,
198 *val
= regval
* 1000;
200 case hwmon_temp_crit
:
201 regs
[0] = channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
202 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
203 ret
= regmap_read(regmap
, regs
[0], ®val
);
206 *val
= regval
* 1000;
208 case hwmon_temp_max_hyst
:
209 regs
[0] = LM95245_REG_RW_REMOTE_OS_LIMIT
;
210 regs
[1] = LM95245_REG_RW_COMMON_HYSTERESIS
;
211 ret
= regmap_multi_reg_read(regmap
, regs
, regvals
, 2);
214 *val
= (regvals
[0] - regvals
[1]) * 1000;
216 case hwmon_temp_crit_hyst
:
217 regs
[0] = channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
218 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
219 regs
[1] = LM95245_REG_RW_COMMON_HYSTERESIS
;
221 ret
= regmap_multi_reg_read(regmap
, regs
, regvals
, 2);
224 *val
= (regvals
[0] - regvals
[1]) * 1000;
226 case hwmon_temp_type
:
227 ret
= regmap_read(regmap
, LM95245_REG_RW_CONFIG2
, ®val
);
230 *val
= (regval
& CFG2_REMOTE_TT
) ? 1 : 2;
232 case hwmon_temp_offset
:
233 ret
= regmap_bulk_read(regmap
, LM95245_REG_RW_REMOTE_OFFH
, regvals
, 2);
236 *val
= temp_from_reg_signed(regvals
[0], regvals
[1]);
238 case hwmon_temp_max_alarm
:
239 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®val
);
242 *val
= !!(regval
& STATUS1_ROS
);
244 case hwmon_temp_crit_alarm
:
245 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®val
);
248 *val
= !!(regval
& (channel
? STATUS1_RTCRIT
: STATUS1_LOC
));
250 case hwmon_temp_fault
:
251 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®val
);
254 *val
= !!(regval
& STATUS1_DIODE_FAULT
);
261 static int lm95245_write_temp(struct device
*dev
, u32 attr
, int channel
,
264 struct lm95245_data
*data
= dev_get_drvdata(dev
);
265 struct regmap
*regmap
= data
->regmap
;
272 val
= clamp_val(val
/ 1000, 0, 255);
273 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
, val
);
275 case hwmon_temp_crit
:
276 reg
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
277 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
278 val
= clamp_val(val
/ 1000, 0, channel
? 255 : 127);
279 ret
= regmap_write(regmap
, reg
, val
);
281 case hwmon_temp_crit_hyst
:
282 mutex_lock(&data
->update_lock
);
283 ret
= regmap_read(regmap
, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
,
286 mutex_unlock(&data
->update_lock
);
289 /* Clamp to reasonable range to prevent overflow */
290 val
= clamp_val(val
, -1000000, 1000000);
291 val
= regval
- val
/ 1000;
292 val
= clamp_val(val
, 0, 31);
293 ret
= regmap_write(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
295 mutex_unlock(&data
->update_lock
);
297 case hwmon_temp_offset
:
298 val
= clamp_val(val
, -128000, 127875);
299 val
= val
* 256 / 1000;
300 regvals
[0] = val
>> 8;
301 regvals
[1] = val
& 0xe0;
303 ret
= regmap_bulk_write(regmap
, LM95245_REG_RW_REMOTE_OFFH
, regvals
, 2);
305 case hwmon_temp_type
:
306 if (val
!= 1 && val
!= 2)
308 ret
= regmap_update_bits(regmap
, LM95245_REG_RW_CONFIG2
,
310 val
== 1 ? CFG2_REMOTE_TT
: 0);
317 static int lm95245_read_chip(struct device
*dev
, u32 attr
, int channel
,
320 struct lm95245_data
*data
= dev_get_drvdata(dev
);
323 case hwmon_chip_update_interval
:
324 *val
= data
->interval
;
331 static int lm95245_write_chip(struct device
*dev
, u32 attr
, int channel
,
334 struct lm95245_data
*data
= dev_get_drvdata(dev
);
338 case hwmon_chip_update_interval
:
339 mutex_lock(&data
->update_lock
);
340 ret
= lm95245_set_conversion_rate(data
, val
);
341 mutex_unlock(&data
->update_lock
);
348 static int lm95245_read(struct device
*dev
, enum hwmon_sensor_types type
,
349 u32 attr
, int channel
, long *val
)
353 return lm95245_read_chip(dev
, attr
, channel
, val
);
355 return lm95245_read_temp(dev
, attr
, channel
, val
);
361 static int lm95245_write(struct device
*dev
, enum hwmon_sensor_types type
,
362 u32 attr
, int channel
, long val
)
366 return lm95245_write_chip(dev
, attr
, channel
, val
);
368 return lm95245_write_temp(dev
, attr
, channel
, val
);
374 static umode_t
lm95245_temp_is_visible(const void *data
, u32 attr
, int channel
)
377 case hwmon_temp_input
:
378 case hwmon_temp_max_alarm
:
379 case hwmon_temp_max_hyst
:
380 case hwmon_temp_crit_alarm
:
381 case hwmon_temp_fault
:
383 case hwmon_temp_type
:
385 case hwmon_temp_crit
:
386 case hwmon_temp_offset
:
388 case hwmon_temp_crit_hyst
:
389 return (channel
== 0) ? 0644 : 0444;
395 static umode_t
lm95245_is_visible(const void *data
,
396 enum hwmon_sensor_types type
,
397 u32 attr
, int channel
)
402 case hwmon_chip_update_interval
:
408 return lm95245_temp_is_visible(data
, attr
, channel
);
414 /* Return 0 if detection is successful, -ENODEV otherwise */
415 static int lm95245_detect(struct i2c_client
*new_client
,
416 struct i2c_board_info
*info
)
418 struct i2c_adapter
*adapter
= new_client
->adapter
;
419 int address
= new_client
->addr
;
423 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
426 id
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_MAN_ID
);
427 if (id
!= MANUFACTURER_ID
)
430 rev
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_CHIP_ID
);
432 case LM95235_REVISION
:
433 if (address
!= 0x18 && address
!= 0x29 && address
!= 0x4c)
437 case LM95245_REVISION
:
444 strscpy(info
->type
, name
, I2C_NAME_SIZE
);
448 static int lm95245_init_client(struct lm95245_data
*data
)
452 ret
= lm95245_read_conversion_rate(data
);
456 return regmap_update_bits(data
->regmap
, LM95245_REG_RW_CONFIG1
,
460 static bool lm95245_is_writeable_reg(struct device
*dev
, unsigned int reg
)
463 case LM95245_REG_RW_CONFIG1
:
464 case LM95245_REG_RW_CONVERS_RATE
:
465 case LM95245_REG_W_ONE_SHOT
:
466 case LM95245_REG_RW_CONFIG2
:
467 case LM95245_REG_RW_REMOTE_OFFH
:
468 case LM95245_REG_RW_REMOTE_OFFL
:
469 case LM95245_REG_RW_REMOTE_OS_LIMIT
:
470 case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
:
471 case LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
472 case LM95245_REG_RW_COMMON_HYSTERESIS
:
479 static bool lm95245_is_volatile_reg(struct device
*dev
, unsigned int reg
)
482 case LM95245_REG_R_STATUS1
:
483 case LM95245_REG_R_STATUS2
:
484 case LM95245_REG_R_LOCAL_TEMPH_S
:
485 case LM95245_REG_R_LOCAL_TEMPL_S
:
486 case LM95245_REG_R_REMOTE_TEMPH_S
:
487 case LM95245_REG_R_REMOTE_TEMPL_S
:
488 case LM95245_REG_R_REMOTE_TEMPH_U
:
489 case LM95245_REG_R_REMOTE_TEMPL_U
:
496 static const struct regmap_config lm95245_regmap_config
= {
499 .writeable_reg
= lm95245_is_writeable_reg
,
500 .volatile_reg
= lm95245_is_volatile_reg
,
501 .cache_type
= REGCACHE_MAPLE
,
502 .use_single_read
= true,
503 .use_single_write
= true,
506 static const struct hwmon_channel_info
* const lm95245_info
[] = {
507 HWMON_CHANNEL_INFO(chip
,
508 HWMON_C_UPDATE_INTERVAL
),
509 HWMON_CHANNEL_INFO(temp
,
510 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_HYST
|
512 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MAX_HYST
|
513 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_FAULT
|
514 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
515 HWMON_T_TYPE
| HWMON_T_OFFSET
),
519 static const struct hwmon_ops lm95245_hwmon_ops
= {
520 .is_visible
= lm95245_is_visible
,
521 .read
= lm95245_read
,
522 .write
= lm95245_write
,
525 static const struct hwmon_chip_info lm95245_chip_info
= {
526 .ops
= &lm95245_hwmon_ops
,
527 .info
= lm95245_info
,
530 static int lm95245_probe(struct i2c_client
*client
)
532 struct device
*dev
= &client
->dev
;
533 struct lm95245_data
*data
;
534 struct device
*hwmon_dev
;
537 data
= devm_kzalloc(dev
, sizeof(struct lm95245_data
), GFP_KERNEL
);
541 data
->regmap
= devm_regmap_init_i2c(client
, &lm95245_regmap_config
);
542 if (IS_ERR(data
->regmap
))
543 return PTR_ERR(data
->regmap
);
545 mutex_init(&data
->update_lock
);
547 /* Initialize the LM95245 chip */
548 ret
= lm95245_init_client(data
);
552 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
556 return PTR_ERR_OR_ZERO(hwmon_dev
);
559 /* Driver data (common to all clients) */
560 static const struct i2c_device_id lm95245_id
[] = {
565 MODULE_DEVICE_TABLE(i2c
, lm95245_id
);
567 static const struct of_device_id __maybe_unused lm95245_of_match
[] = {
568 { .compatible
= "national,lm95235" },
569 { .compatible
= "national,lm95245" },
572 MODULE_DEVICE_TABLE(of
, lm95245_of_match
);
574 static struct i2c_driver lm95245_driver
= {
575 .class = I2C_CLASS_HWMON
,
578 .of_match_table
= of_match_ptr(lm95245_of_match
),
580 .probe
= lm95245_probe
,
581 .id_table
= lm95245_id
,
582 .detect
= lm95245_detect
,
583 .address_list
= normal_i2c
,
586 module_i2c_driver(lm95245_driver
);
588 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
589 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
590 MODULE_LICENSE("GPL");