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.
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/hwmon.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
29 static const unsigned short normal_i2c
[] = {
30 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END
};
32 /* LM95245 registers */
33 /* general registers */
34 #define LM95245_REG_RW_CONFIG1 0x03
35 #define LM95245_REG_RW_CONVERS_RATE 0x04
36 #define LM95245_REG_W_ONE_SHOT 0x0F
38 /* diode configuration */
39 #define LM95245_REG_RW_CONFIG2 0xBF
40 #define LM95245_REG_RW_REMOTE_OFFH 0x11
41 #define LM95245_REG_RW_REMOTE_OFFL 0x12
43 /* status registers */
44 #define LM95245_REG_R_STATUS1 0x02
45 #define LM95245_REG_R_STATUS2 0x33
48 #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
49 #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
50 #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
51 #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
53 /* temperature signed */
54 #define LM95245_REG_R_LOCAL_TEMPH_S 0x00
55 #define LM95245_REG_R_LOCAL_TEMPL_S 0x30
56 #define LM95245_REG_R_REMOTE_TEMPH_S 0x01
57 #define LM95245_REG_R_REMOTE_TEMPL_S 0x10
58 /* temperature unsigned */
59 #define LM95245_REG_R_REMOTE_TEMPH_U 0x31
60 #define LM95245_REG_R_REMOTE_TEMPL_U 0x32
63 #define LM95245_REG_R_MAN_ID 0xFE
64 #define LM95245_REG_R_CHIP_ID 0xFF
66 /* LM95245 specific bitfields */
68 #define CFG_REMOTE_TCRIT_MASK 0x10
69 #define CFG_REMOTE_OS_MASK 0x08
70 #define CFG_LOCAL_TCRIT_MASK 0x04
71 #define CFG_LOCAL_OS_MASK 0x02
73 #define CFG2_OS_A0 0x40
74 #define CFG2_DIODE_FAULT_OS 0x20
75 #define CFG2_DIODE_FAULT_TCRIT 0x10
76 #define CFG2_REMOTE_TT 0x08
77 #define CFG2_REMOTE_FILTER_DIS 0x00
78 #define CFG2_REMOTE_FILTER_EN 0x06
80 /* conversation rate in ms */
81 #define RATE_CR0063 0x00
82 #define RATE_CR0364 0x01
83 #define RATE_CR1000 0x02
84 #define RATE_CR2500 0x03
86 #define STATUS1_ROS 0x10
87 #define STATUS1_DIODE_FAULT 0x04
88 #define STATUS1_RTCRIT 0x02
89 #define STATUS1_LOC 0x01
91 #define MANUFACTURER_ID 0x01
92 #define LM95235_REVISION 0xB1
93 #define LM95245_REVISION 0xB3
95 static const u8 lm95245_reg_address
[] = {
96 LM95245_REG_R_LOCAL_TEMPH_S
,
97 LM95245_REG_R_LOCAL_TEMPL_S
,
98 LM95245_REG_R_REMOTE_TEMPH_S
,
99 LM95245_REG_R_REMOTE_TEMPL_S
,
100 LM95245_REG_R_REMOTE_TEMPH_U
,
101 LM95245_REG_R_REMOTE_TEMPL_U
,
102 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
,
103 LM95245_REG_RW_REMOTE_TCRIT_LIMIT
,
104 LM95245_REG_RW_COMMON_HYSTERESIS
,
105 LM95245_REG_R_STATUS1
,
108 /* Client data (each client gets its own) */
109 struct lm95245_data
{
110 struct regmap
*regmap
;
111 struct mutex update_lock
;
112 int interval
; /* in msecs */
116 static int temp_from_reg_unsigned(u8 val_h
, u8 val_l
)
118 return val_h
* 1000 + val_l
* 1000 / 256;
121 static int temp_from_reg_signed(u8 val_h
, u8 val_l
)
124 return (val_h
- 0x100) * 1000;
125 return temp_from_reg_unsigned(val_h
, val_l
);
128 static int lm95245_read_conversion_rate(struct lm95245_data
*data
)
133 ret
= regmap_read(data
->regmap
, LM95245_REG_RW_CONVERS_RATE
, &rate
);
142 data
->interval
= 364;
145 data
->interval
= 1000;
149 data
->interval
= 2500;
155 static int lm95245_set_conversion_rate(struct lm95245_data
*data
, long interval
)
159 if (interval
<= 63) {
162 } else if (interval
<= 364) {
165 } else if (interval
<= 1000) {
173 ret
= regmap_write(data
->regmap
, LM95245_REG_RW_CONVERS_RATE
, rate
);
177 data
->interval
= interval
;
181 static int lm95245_read_temp(struct device
*dev
, u32 attr
, int channel
,
184 struct lm95245_data
*data
= dev_get_drvdata(dev
);
185 struct regmap
*regmap
= data
->regmap
;
186 int ret
, regl
, regh
, regvall
, regvalh
;
189 case hwmon_temp_input
:
190 regl
= channel
? LM95245_REG_R_REMOTE_TEMPL_S
:
191 LM95245_REG_R_LOCAL_TEMPL_S
;
192 regh
= channel
? LM95245_REG_R_REMOTE_TEMPH_S
:
193 LM95245_REG_R_LOCAL_TEMPH_S
;
194 ret
= regmap_read(regmap
, regl
, ®vall
);
197 ret
= regmap_read(regmap
, regh
, ®valh
);
201 * Local temp is always signed.
202 * Remote temp has both signed and unsigned data.
203 * Use signed calculation for remote if signed bit is set
204 * or if reported temperature is below signed limit.
206 if (!channel
|| (regvalh
& 0x80) || regvalh
< 0x7f) {
207 *val
= temp_from_reg_signed(regvalh
, regvall
);
210 ret
= regmap_read(regmap
, LM95245_REG_R_REMOTE_TEMPL_U
,
214 ret
= regmap_read(regmap
, LM95245_REG_R_REMOTE_TEMPH_U
,
218 *val
= temp_from_reg_unsigned(regvalh
, regvall
);
221 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
,
225 *val
= regvalh
* 1000;
227 case hwmon_temp_crit
:
228 regh
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
229 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
230 ret
= regmap_read(regmap
, regh
, ®valh
);
233 *val
= regvalh
* 1000;
235 case hwmon_temp_max_hyst
:
236 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
,
240 ret
= regmap_read(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
244 *val
= (regvalh
- regvall
) * 1000;
246 case hwmon_temp_crit_hyst
:
247 regh
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
248 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
249 ret
= regmap_read(regmap
, regh
, ®valh
);
252 ret
= regmap_read(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
256 *val
= (regvalh
- regvall
) * 1000;
258 case hwmon_temp_type
:
259 ret
= regmap_read(regmap
, LM95245_REG_RW_CONFIG2
, ®valh
);
262 *val
= (regvalh
& CFG2_REMOTE_TT
) ? 1 : 2;
264 case hwmon_temp_offset
:
265 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OFFL
,
269 ret
= regmap_read(regmap
, LM95245_REG_RW_REMOTE_OFFH
,
273 *val
= temp_from_reg_signed(regvalh
, regvall
);
275 case hwmon_temp_max_alarm
:
276 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
279 *val
= !!(regvalh
& STATUS1_ROS
);
281 case hwmon_temp_crit_alarm
:
282 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
285 *val
= !!(regvalh
& (channel
? STATUS1_RTCRIT
: STATUS1_LOC
));
287 case hwmon_temp_fault
:
288 ret
= regmap_read(regmap
, LM95245_REG_R_STATUS1
, ®valh
);
291 *val
= !!(regvalh
& STATUS1_DIODE_FAULT
);
298 static int lm95245_write_temp(struct device
*dev
, u32 attr
, int channel
,
301 struct lm95245_data
*data
= dev_get_drvdata(dev
);
302 struct regmap
*regmap
= data
->regmap
;
308 val
= clamp_val(val
/ 1000, 0, 255);
309 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OS_LIMIT
, val
);
311 case hwmon_temp_crit
:
312 reg
= channel
? LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
313 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
;
314 val
= clamp_val(val
/ 1000, 0, channel
? 255 : 127);
315 ret
= regmap_write(regmap
, reg
, val
);
317 case hwmon_temp_crit_hyst
:
318 mutex_lock(&data
->update_lock
);
319 ret
= regmap_read(regmap
, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
,
322 mutex_unlock(&data
->update_lock
);
325 /* Clamp to reasonable range to prevent overflow */
326 val
= clamp_val(val
, -1000000, 1000000);
327 val
= regval
- val
/ 1000;
328 val
= clamp_val(val
, 0, 31);
329 ret
= regmap_write(regmap
, LM95245_REG_RW_COMMON_HYSTERESIS
,
331 mutex_unlock(&data
->update_lock
);
333 case hwmon_temp_offset
:
334 val
= clamp_val(val
, -128000, 127875);
335 val
= val
* 256 / 1000;
336 mutex_lock(&data
->update_lock
);
337 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OFFL
,
340 mutex_unlock(&data
->update_lock
);
343 ret
= regmap_write(regmap
, LM95245_REG_RW_REMOTE_OFFH
,
345 mutex_unlock(&data
->update_lock
);
347 case hwmon_temp_type
:
348 if (val
!= 1 && val
!= 2)
350 ret
= regmap_update_bits(regmap
, LM95245_REG_RW_CONFIG2
,
352 val
== 1 ? CFG2_REMOTE_TT
: 0);
359 static int lm95245_read_chip(struct device
*dev
, u32 attr
, int channel
,
362 struct lm95245_data
*data
= dev_get_drvdata(dev
);
365 case hwmon_chip_update_interval
:
366 *val
= data
->interval
;
373 static int lm95245_write_chip(struct device
*dev
, u32 attr
, int channel
,
376 struct lm95245_data
*data
= dev_get_drvdata(dev
);
380 case hwmon_chip_update_interval
:
381 mutex_lock(&data
->update_lock
);
382 ret
= lm95245_set_conversion_rate(data
, val
);
383 mutex_unlock(&data
->update_lock
);
390 static int lm95245_read(struct device
*dev
, enum hwmon_sensor_types type
,
391 u32 attr
, int channel
, long *val
)
395 return lm95245_read_chip(dev
, attr
, channel
, val
);
397 return lm95245_read_temp(dev
, attr
, channel
, val
);
403 static int lm95245_write(struct device
*dev
, enum hwmon_sensor_types type
,
404 u32 attr
, int channel
, long val
)
408 return lm95245_write_chip(dev
, attr
, channel
, val
);
410 return lm95245_write_temp(dev
, attr
, channel
, val
);
416 static umode_t
lm95245_temp_is_visible(const void *data
, u32 attr
, int channel
)
419 case hwmon_temp_input
:
420 case hwmon_temp_max_alarm
:
421 case hwmon_temp_max_hyst
:
422 case hwmon_temp_crit_alarm
:
423 case hwmon_temp_fault
:
425 case hwmon_temp_type
:
427 case hwmon_temp_crit
:
428 case hwmon_temp_offset
:
429 return S_IRUGO
| S_IWUSR
;
430 case hwmon_temp_crit_hyst
:
431 return (channel
== 0) ? S_IRUGO
| S_IWUSR
: S_IRUGO
;
437 static umode_t
lm95245_is_visible(const void *data
,
438 enum hwmon_sensor_types type
,
439 u32 attr
, int channel
)
444 case hwmon_chip_update_interval
:
445 return S_IRUGO
| S_IWUSR
;
450 return lm95245_temp_is_visible(data
, attr
, channel
);
456 /* Return 0 if detection is successful, -ENODEV otherwise */
457 static int lm95245_detect(struct i2c_client
*new_client
,
458 struct i2c_board_info
*info
)
460 struct i2c_adapter
*adapter
= new_client
->adapter
;
461 int address
= new_client
->addr
;
465 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
468 id
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_MAN_ID
);
469 if (id
!= MANUFACTURER_ID
)
472 rev
= i2c_smbus_read_byte_data(new_client
, LM95245_REG_R_CHIP_ID
);
474 case LM95235_REVISION
:
475 if (address
!= 0x18 && address
!= 0x29 && address
!= 0x4c)
479 case LM95245_REVISION
:
486 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
490 static int lm95245_init_client(struct lm95245_data
*data
)
494 ret
= lm95245_read_conversion_rate(data
);
498 return regmap_update_bits(data
->regmap
, LM95245_REG_RW_CONFIG1
,
502 static bool lm95245_is_writeable_reg(struct device
*dev
, unsigned int reg
)
505 case LM95245_REG_RW_CONFIG1
:
506 case LM95245_REG_RW_CONVERS_RATE
:
507 case LM95245_REG_W_ONE_SHOT
:
508 case LM95245_REG_RW_CONFIG2
:
509 case LM95245_REG_RW_REMOTE_OFFH
:
510 case LM95245_REG_RW_REMOTE_OFFL
:
511 case LM95245_REG_RW_REMOTE_OS_LIMIT
:
512 case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT
:
513 case LM95245_REG_RW_REMOTE_TCRIT_LIMIT
:
514 case LM95245_REG_RW_COMMON_HYSTERESIS
:
521 static bool lm95245_is_volatile_reg(struct device
*dev
, unsigned int reg
)
524 case LM95245_REG_R_STATUS1
:
525 case LM95245_REG_R_STATUS2
:
526 case LM95245_REG_R_LOCAL_TEMPH_S
:
527 case LM95245_REG_R_LOCAL_TEMPL_S
:
528 case LM95245_REG_R_REMOTE_TEMPH_S
:
529 case LM95245_REG_R_REMOTE_TEMPL_S
:
530 case LM95245_REG_R_REMOTE_TEMPH_U
:
531 case LM95245_REG_R_REMOTE_TEMPL_U
:
538 static const struct regmap_config lm95245_regmap_config
= {
541 .writeable_reg
= lm95245_is_writeable_reg
,
542 .volatile_reg
= lm95245_is_volatile_reg
,
543 .cache_type
= REGCACHE_RBTREE
,
544 .use_single_read
= true,
545 .use_single_write
= true,
548 static const u32 lm95245_chip_config
[] = {
549 HWMON_C_UPDATE_INTERVAL
,
553 static const struct hwmon_channel_info lm95245_chip
= {
555 .config
= lm95245_chip_config
,
558 static const u32 lm95245_temp_config
[] = {
559 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_CRIT_ALARM
,
560 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MAX_HYST
| HWMON_T_CRIT
|
561 HWMON_T_CRIT_HYST
| HWMON_T_FAULT
| HWMON_T_MAX_ALARM
|
562 HWMON_T_CRIT_ALARM
| HWMON_T_TYPE
| HWMON_T_OFFSET
,
566 static const struct hwmon_channel_info lm95245_temp
= {
568 .config
= lm95245_temp_config
,
571 static const struct hwmon_channel_info
*lm95245_info
[] = {
577 static const struct hwmon_ops lm95245_hwmon_ops
= {
578 .is_visible
= lm95245_is_visible
,
579 .read
= lm95245_read
,
580 .write
= lm95245_write
,
583 static const struct hwmon_chip_info lm95245_chip_info
= {
584 .ops
= &lm95245_hwmon_ops
,
585 .info
= lm95245_info
,
588 static int lm95245_probe(struct i2c_client
*client
,
589 const struct i2c_device_id
*id
)
591 struct device
*dev
= &client
->dev
;
592 struct lm95245_data
*data
;
593 struct device
*hwmon_dev
;
596 data
= devm_kzalloc(dev
, sizeof(struct lm95245_data
), GFP_KERNEL
);
600 data
->regmap
= devm_regmap_init_i2c(client
, &lm95245_regmap_config
);
601 if (IS_ERR(data
->regmap
))
602 return PTR_ERR(data
->regmap
);
604 mutex_init(&data
->update_lock
);
606 /* Initialize the LM95245 chip */
607 ret
= lm95245_init_client(data
);
611 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
615 return PTR_ERR_OR_ZERO(hwmon_dev
);
618 /* Driver data (common to all clients) */
619 static const struct i2c_device_id lm95245_id
[] = {
624 MODULE_DEVICE_TABLE(i2c
, lm95245_id
);
626 static const struct of_device_id lm95245_of_match
[] = {
627 { .compatible
= "national,lm95235" },
628 { .compatible
= "national,lm95245" },
631 MODULE_DEVICE_TABLE(of
, lm95245_of_match
);
633 static struct i2c_driver lm95245_driver
= {
634 .class = I2C_CLASS_HWMON
,
637 .of_match_table
= of_match_ptr(lm95245_of_match
),
639 .probe
= lm95245_probe
,
640 .id_table
= lm95245_id
,
641 .detect
= lm95245_detect
,
642 .address_list
= normal_i2c
,
645 module_i2c_driver(lm95245_driver
);
647 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
648 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
649 MODULE_LICENSE("GPL");