1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
5 * The LM95241 is a sensor chip made by National Semiconductors.
6 * It reports up to three temperatures (its own plus up to two external ones).
7 * Complete datasheet can be obtained from National's website at:
8 * http://www.national.com/ds.cgi/LM/LM95241.pdf
11 #include <linux/bitops.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/jiffies.h>
16 #include <linux/hwmon.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
21 #define DEVNAME "lm95241"
23 static const unsigned short normal_i2c
[] = {
24 0x19, 0x2a, 0x2b, I2C_CLIENT_END
};
26 /* LM95241 registers */
27 #define LM95241_REG_R_MAN_ID 0xFE
28 #define LM95241_REG_R_CHIP_ID 0xFF
29 #define LM95241_REG_R_STATUS 0x02
30 #define LM95241_REG_RW_CONFIG 0x03
31 #define LM95241_REG_RW_REM_FILTER 0x06
32 #define LM95241_REG_RW_TRUTHERM 0x07
33 #define LM95241_REG_W_ONE_SHOT 0x0F
34 #define LM95241_REG_R_LOCAL_TEMPH 0x10
35 #define LM95241_REG_R_REMOTE1_TEMPH 0x11
36 #define LM95241_REG_R_REMOTE2_TEMPH 0x12
37 #define LM95241_REG_R_LOCAL_TEMPL 0x20
38 #define LM95241_REG_R_REMOTE1_TEMPL 0x21
39 #define LM95241_REG_R_REMOTE2_TEMPL 0x22
40 #define LM95241_REG_RW_REMOTE_MODEL 0x30
42 /* LM95241 specific bitfields */
43 #define CFG_STOP BIT(6)
44 #define CFG_CR0076 0x00
45 #define CFG_CR0182 BIT(4)
46 #define CFG_CR1000 BIT(5)
47 #define CFG_CR2700 (BIT(4) | BIT(5))
48 #define CFG_CRMASK (BIT(4) | BIT(5))
49 #define R1MS_MASK BIT(0)
50 #define R2MS_MASK BIT(2)
51 #define R1DF_MASK BIT(1)
52 #define R2DF_MASK BIT(2)
53 #define R1FE_MASK BIT(0)
54 #define R2FE_MASK BIT(2)
62 #define NATSEMI_MAN_ID 0x01
63 #define LM95231_CHIP_ID 0xA1
64 #define LM95241_CHIP_ID 0xA4
66 static const u8 lm95241_reg_address
[] = {
67 LM95241_REG_R_LOCAL_TEMPH
,
68 LM95241_REG_R_LOCAL_TEMPL
,
69 LM95241_REG_R_REMOTE1_TEMPH
,
70 LM95241_REG_R_REMOTE1_TEMPL
,
71 LM95241_REG_R_REMOTE2_TEMPH
,
72 LM95241_REG_R_REMOTE2_TEMPL
75 /* Client data (each client gets its own) */
77 struct i2c_client
*client
;
78 struct mutex update_lock
;
79 unsigned long last_updated
; /* in jiffies */
80 unsigned long interval
; /* in milli-seconds */
81 char valid
; /* zero until following fields are valid */
82 /* registers values */
83 u8 temp
[ARRAY_SIZE(lm95241_reg_address
)];
84 u8 status
, config
, model
, trutherm
;
88 static int temp_from_reg_signed(u8 val_h
, u8 val_l
)
90 s16 val_hl
= (val_h
<< 8) | val_l
;
91 return val_hl
* 1000 / 256;
94 static int temp_from_reg_unsigned(u8 val_h
, u8 val_l
)
96 u16 val_hl
= (val_h
<< 8) | val_l
;
97 return val_hl
* 1000 / 256;
100 static struct lm95241_data
*lm95241_update_device(struct device
*dev
)
102 struct lm95241_data
*data
= dev_get_drvdata(dev
);
103 struct i2c_client
*client
= data
->client
;
105 mutex_lock(&data
->update_lock
);
107 if (time_after(jiffies
, data
->last_updated
108 + msecs_to_jiffies(data
->interval
)) ||
112 dev_dbg(dev
, "Updating lm95241 data.\n");
113 for (i
= 0; i
< ARRAY_SIZE(lm95241_reg_address
); i
++)
115 = i2c_smbus_read_byte_data(client
,
116 lm95241_reg_address
[i
]);
118 data
->status
= i2c_smbus_read_byte_data(client
,
119 LM95241_REG_R_STATUS
);
120 data
->last_updated
= jiffies
;
124 mutex_unlock(&data
->update_lock
);
129 static int lm95241_read_chip(struct device
*dev
, u32 attr
, int channel
,
132 struct lm95241_data
*data
= dev_get_drvdata(dev
);
135 case hwmon_chip_update_interval
:
136 *val
= data
->interval
;
143 static int lm95241_read_temp(struct device
*dev
, u32 attr
, int channel
,
146 struct lm95241_data
*data
= lm95241_update_device(dev
);
149 case hwmon_temp_input
:
150 if (!channel
|| (data
->config
& BIT(channel
- 1)))
151 *val
= temp_from_reg_signed(data
->temp
[channel
* 2],
152 data
->temp
[channel
* 2 + 1]);
154 *val
= temp_from_reg_unsigned(data
->temp
[channel
* 2],
155 data
->temp
[channel
* 2 + 1]);
159 *val
= (data
->config
& R1DF_MASK
) ? -128000 : 0;
161 *val
= (data
->config
& R2DF_MASK
) ? -128000 : 0;
165 *val
= (data
->config
& R1DF_MASK
) ? 127875 : 255875;
167 *val
= (data
->config
& R2DF_MASK
) ? 127875 : 255875;
169 case hwmon_temp_type
:
171 *val
= (data
->model
& R1MS_MASK
) ? 1 : 2;
173 *val
= (data
->model
& R2MS_MASK
) ? 1 : 2;
175 case hwmon_temp_fault
:
177 *val
= !!(data
->status
& R1DM
);
179 *val
= !!(data
->status
& R2DM
);
186 static int lm95241_read(struct device
*dev
, enum hwmon_sensor_types type
,
187 u32 attr
, int channel
, long *val
)
191 return lm95241_read_chip(dev
, attr
, channel
, val
);
193 return lm95241_read_temp(dev
, attr
, channel
, val
);
199 static int lm95241_write_chip(struct device
*dev
, u32 attr
, int channel
,
202 struct lm95241_data
*data
= dev_get_drvdata(dev
);
207 mutex_lock(&data
->update_lock
);
210 case hwmon_chip_update_interval
:
211 config
= data
->config
& ~CFG_CRMASK
;
214 config
|= CFG_CR0076
;
215 } else if (val
< 590) {
217 config
|= CFG_CR0182
;
218 } else if (val
< 1850) {
220 config
|= CFG_CR1000
;
223 config
|= CFG_CR2700
;
225 data
->interval
= convrate
;
226 data
->config
= config
;
227 ret
= i2c_smbus_write_byte_data(data
->client
,
228 LM95241_REG_RW_CONFIG
, config
);
234 mutex_unlock(&data
->update_lock
);
238 static int lm95241_write_temp(struct device
*dev
, u32 attr
, int channel
,
241 struct lm95241_data
*data
= dev_get_drvdata(dev
);
242 struct i2c_client
*client
= data
->client
;
245 mutex_lock(&data
->update_lock
);
251 data
->config
|= R1DF_MASK
;
253 data
->config
&= ~R1DF_MASK
;
256 data
->config
|= R2DF_MASK
;
258 data
->config
&= ~R2DF_MASK
;
261 ret
= i2c_smbus_write_byte_data(client
, LM95241_REG_RW_CONFIG
,
267 data
->config
|= R1DF_MASK
;
269 data
->config
&= ~R1DF_MASK
;
272 data
->config
|= R2DF_MASK
;
274 data
->config
&= ~R2DF_MASK
;
277 ret
= i2c_smbus_write_byte_data(client
, LM95241_REG_RW_CONFIG
,
280 case hwmon_temp_type
:
281 if (val
!= 1 && val
!= 2) {
286 data
->trutherm
&= ~(TT_MASK
<< TT1_SHIFT
);
288 data
->model
|= R1MS_MASK
;
289 data
->trutherm
|= (TT_ON
<< TT1_SHIFT
);
291 data
->model
&= ~R1MS_MASK
;
292 data
->trutherm
|= (TT_OFF
<< TT1_SHIFT
);
295 data
->trutherm
&= ~(TT_MASK
<< TT2_SHIFT
);
297 data
->model
|= R2MS_MASK
;
298 data
->trutherm
|= (TT_ON
<< TT2_SHIFT
);
300 data
->model
&= ~R2MS_MASK
;
301 data
->trutherm
|= (TT_OFF
<< TT2_SHIFT
);
304 ret
= i2c_smbus_write_byte_data(client
,
305 LM95241_REG_RW_REMOTE_MODEL
,
309 ret
= i2c_smbus_write_byte_data(client
, LM95241_REG_RW_TRUTHERM
,
317 mutex_unlock(&data
->update_lock
);
322 static int lm95241_write(struct device
*dev
, enum hwmon_sensor_types type
,
323 u32 attr
, int channel
, long val
)
327 return lm95241_write_chip(dev
, attr
, channel
, val
);
329 return lm95241_write_temp(dev
, attr
, channel
, val
);
335 static umode_t
lm95241_is_visible(const void *data
,
336 enum hwmon_sensor_types type
,
337 u32 attr
, int channel
)
342 case hwmon_chip_update_interval
:
348 case hwmon_temp_input
:
350 case hwmon_temp_fault
:
354 case hwmon_temp_type
:
364 /* Return 0 if detection is successful, -ENODEV otherwise */
365 static int lm95241_detect(struct i2c_client
*new_client
,
366 struct i2c_board_info
*info
)
368 struct i2c_adapter
*adapter
= new_client
->adapter
;
372 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
375 mfg_id
= i2c_smbus_read_byte_data(new_client
, LM95241_REG_R_MAN_ID
);
376 if (mfg_id
!= NATSEMI_MAN_ID
)
379 chip_id
= i2c_smbus_read_byte_data(new_client
, LM95241_REG_R_CHIP_ID
);
381 case LM95231_CHIP_ID
:
384 case LM95241_CHIP_ID
:
391 /* Fill the i2c board info */
392 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
396 static void lm95241_init_client(struct i2c_client
*client
,
397 struct lm95241_data
*data
)
399 data
->interval
= 1000;
400 data
->config
= CFG_CR1000
;
401 data
->trutherm
= (TT_OFF
<< TT1_SHIFT
) | (TT_OFF
<< TT2_SHIFT
);
403 i2c_smbus_write_byte_data(client
, LM95241_REG_RW_CONFIG
, data
->config
);
404 i2c_smbus_write_byte_data(client
, LM95241_REG_RW_REM_FILTER
,
405 R1FE_MASK
| R2FE_MASK
);
406 i2c_smbus_write_byte_data(client
, LM95241_REG_RW_TRUTHERM
,
408 i2c_smbus_write_byte_data(client
, LM95241_REG_RW_REMOTE_MODEL
,
412 static const struct hwmon_channel_info
*lm95241_info
[] = {
413 HWMON_CHANNEL_INFO(chip
,
414 HWMON_C_UPDATE_INTERVAL
),
415 HWMON_CHANNEL_INFO(temp
,
417 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MIN
|
418 HWMON_T_TYPE
| HWMON_T_FAULT
,
419 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MIN
|
420 HWMON_T_TYPE
| HWMON_T_FAULT
),
424 static const struct hwmon_ops lm95241_hwmon_ops
= {
425 .is_visible
= lm95241_is_visible
,
426 .read
= lm95241_read
,
427 .write
= lm95241_write
,
430 static const struct hwmon_chip_info lm95241_chip_info
= {
431 .ops
= &lm95241_hwmon_ops
,
432 .info
= lm95241_info
,
435 static int lm95241_probe(struct i2c_client
*client
,
436 const struct i2c_device_id
*id
)
438 struct device
*dev
= &client
->dev
;
439 struct lm95241_data
*data
;
440 struct device
*hwmon_dev
;
442 data
= devm_kzalloc(dev
, sizeof(struct lm95241_data
), GFP_KERNEL
);
446 data
->client
= client
;
447 mutex_init(&data
->update_lock
);
449 /* Initialize the LM95241 chip */
450 lm95241_init_client(client
, data
);
452 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
456 return PTR_ERR_OR_ZERO(hwmon_dev
);
459 /* Driver data (common to all clients) */
460 static const struct i2c_device_id lm95241_id
[] = {
465 MODULE_DEVICE_TABLE(i2c
, lm95241_id
);
467 static struct i2c_driver lm95241_driver
= {
468 .class = I2C_CLASS_HWMON
,
472 .probe
= lm95241_probe
,
473 .id_table
= lm95241_id
,
474 .detect
= lm95241_detect
,
475 .address_list
= normal_i2c
,
478 module_i2c_driver(lm95241_driver
);
480 MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
481 MODULE_DESCRIPTION("LM95231/LM95241 sensor driver");
482 MODULE_LICENSE("GPL");