1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for MAX31730 3-Channel Remote Temperature Sensor
5 * Copyright (c) 2019 Guenter Roeck <linux@roeck-us.net>
8 #include <linux/bits.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/hwmon.h>
13 #include <linux/module.h>
15 #include <linux/slab.h>
17 /* Addresses scanned */
18 static const unsigned short normal_i2c
[] = { 0x1c, 0x1d, 0x1e, 0x1f, 0x4c,
19 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
};
21 /* The MAX31730 registers */
22 #define MAX31730_REG_TEMP 0x00
23 #define MAX31730_REG_CONF 0x13
24 #define MAX31730_STOP BIT(7)
25 #define MAX31730_EXTRANGE BIT(1)
26 #define MAX31730_REG_TEMP_OFFSET 0x16
27 #define MAX31730_TEMP_OFFSET_BASELINE 0x77
28 #define MAX31730_REG_OFFSET_ENABLE 0x17
29 #define MAX31730_REG_TEMP_MAX 0x20
30 #define MAX31730_REG_TEMP_MIN 0x30
31 #define MAX31730_REG_STATUS_HIGH 0x32
32 #define MAX31730_REG_STATUS_LOW 0x33
33 #define MAX31730_REG_CHANNEL_ENABLE 0x35
34 #define MAX31730_REG_TEMP_FAULT 0x36
36 #define MAX31730_REG_MFG_ID 0x50
37 #define MAX31730_MFG_ID 0x4d
38 #define MAX31730_REG_MFG_REV 0x51
39 #define MAX31730_MFG_REV 0x01
41 #define MAX31730_TEMP_MIN (-128000)
42 #define MAX31730_TEMP_MAX 127937
44 /* Each client has this additional data */
45 struct max31730_data
{
46 struct i2c_client
*client
;
53 /*-----------------------------------------------------------------------*/
55 static inline long max31730_reg_to_mc(s16 temp
)
57 return DIV_ROUND_CLOSEST((temp
>> 4) * 1000, 16);
60 static int max31730_write_config(struct max31730_data
*data
, u8 set_mask
,
65 clr_mask
|= MAX31730_EXTRANGE
;
66 value
= data
->current_conf
& ~clr_mask
;
69 if (data
->current_conf
!= value
) {
72 err
= i2c_smbus_write_byte_data(data
->client
, MAX31730_REG_CONF
,
76 data
->current_conf
= value
;
81 static int max31730_set_enable(struct i2c_client
*client
, int reg
,
82 u8
*confdata
, int channel
, bool enable
)
84 u8 regval
= *confdata
;
88 regval
|= BIT(channel
);
90 regval
&= ~BIT(channel
);
92 if (regval
!= *confdata
) {
93 err
= i2c_smbus_write_byte_data(client
, reg
, regval
);
101 static int max31730_set_offset_enable(struct max31730_data
*data
, int channel
,
104 return max31730_set_enable(data
->client
, MAX31730_REG_OFFSET_ENABLE
,
105 &data
->offset_enable
, channel
, enable
);
108 static int max31730_set_channel_enable(struct max31730_data
*data
, int channel
,
111 return max31730_set_enable(data
->client
, MAX31730_REG_CHANNEL_ENABLE
,
112 &data
->channel_enable
, channel
, enable
);
115 static int max31730_read(struct device
*dev
, enum hwmon_sensor_types type
,
116 u32 attr
, int channel
, long *val
)
118 struct max31730_data
*data
= dev_get_drvdata(dev
);
119 int regval
, reg
, offset
;
121 if (type
!= hwmon_temp
)
125 case hwmon_temp_input
:
126 if (!(data
->channel_enable
& BIT(channel
)))
128 reg
= MAX31730_REG_TEMP
+ (channel
* 2);
131 reg
= MAX31730_REG_TEMP_MAX
+ (channel
* 2);
134 reg
= MAX31730_REG_TEMP_MIN
;
136 case hwmon_temp_enable
:
137 *val
= !!(data
->channel_enable
& BIT(channel
));
139 case hwmon_temp_offset
:
142 if (!(data
->offset_enable
& BIT(channel
))) {
146 offset
= i2c_smbus_read_byte_data(data
->client
,
147 MAX31730_REG_TEMP_OFFSET
);
150 *val
= (offset
- MAX31730_TEMP_OFFSET_BASELINE
) * 125;
152 case hwmon_temp_fault
:
153 regval
= i2c_smbus_read_byte_data(data
->client
,
154 MAX31730_REG_TEMP_FAULT
);
157 *val
= !!(regval
& BIT(channel
));
159 case hwmon_temp_min_alarm
:
160 regval
= i2c_smbus_read_byte_data(data
->client
,
161 MAX31730_REG_STATUS_LOW
);
164 *val
= !!(regval
& BIT(channel
));
166 case hwmon_temp_max_alarm
:
167 regval
= i2c_smbus_read_byte_data(data
->client
,
168 MAX31730_REG_STATUS_HIGH
);
171 *val
= !!(regval
& BIT(channel
));
176 regval
= i2c_smbus_read_word_swapped(data
->client
, reg
);
180 *val
= max31730_reg_to_mc(regval
);
185 static int max31730_write(struct device
*dev
, enum hwmon_sensor_types type
,
186 u32 attr
, int channel
, long val
)
188 struct max31730_data
*data
= dev_get_drvdata(dev
);
191 if (type
!= hwmon_temp
)
196 reg
= MAX31730_REG_TEMP_MAX
+ channel
* 2;
199 reg
= MAX31730_REG_TEMP_MIN
;
201 case hwmon_temp_enable
:
202 if (val
!= 0 && val
!= 1)
204 return max31730_set_channel_enable(data
, channel
, val
);
205 case hwmon_temp_offset
:
206 val
= clamp_val(val
, -14875, 17000) + 14875;
207 val
= DIV_ROUND_CLOSEST(val
, 125);
208 err
= max31730_set_offset_enable(data
, channel
,
209 val
!= MAX31730_TEMP_OFFSET_BASELINE
);
212 return i2c_smbus_write_byte_data(data
->client
,
213 MAX31730_REG_TEMP_OFFSET
, val
);
218 val
= clamp_val(val
, MAX31730_TEMP_MIN
, MAX31730_TEMP_MAX
);
219 val
= DIV_ROUND_CLOSEST(val
<< 4, 1000) << 4;
221 return i2c_smbus_write_word_swapped(data
->client
, reg
, (u16
)val
);
224 static umode_t
max31730_is_visible(const void *data
,
225 enum hwmon_sensor_types type
,
226 u32 attr
, int channel
)
231 case hwmon_temp_input
:
232 case hwmon_temp_min_alarm
:
233 case hwmon_temp_max_alarm
:
234 case hwmon_temp_fault
:
237 return channel
? 0444 : 0644;
238 case hwmon_temp_offset
:
239 case hwmon_temp_enable
:
250 static const struct hwmon_channel_info
* const max31730_info
[] = {
251 HWMON_CHANNEL_INFO(chip
,
252 HWMON_C_REGISTER_TZ
),
253 HWMON_CHANNEL_INFO(temp
,
254 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
256 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
,
257 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
258 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
259 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
261 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
262 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
263 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
265 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
266 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
267 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
273 static const struct hwmon_ops max31730_hwmon_ops
= {
274 .is_visible
= max31730_is_visible
,
275 .read
= max31730_read
,
276 .write
= max31730_write
,
279 static const struct hwmon_chip_info max31730_chip_info
= {
280 .ops
= &max31730_hwmon_ops
,
281 .info
= max31730_info
,
284 static void max31730_remove(void *data
)
286 struct max31730_data
*max31730
= data
;
287 struct i2c_client
*client
= max31730
->client
;
289 i2c_smbus_write_byte_data(client
, MAX31730_REG_CONF
,
290 max31730
->orig_conf
);
294 max31730_probe(struct i2c_client
*client
)
296 struct device
*dev
= &client
->dev
;
297 struct device
*hwmon_dev
;
298 struct max31730_data
*data
;
301 if (!i2c_check_functionality(client
->adapter
,
302 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
305 data
= devm_kzalloc(dev
, sizeof(struct max31730_data
), GFP_KERNEL
);
309 data
->client
= client
;
311 /* Cache original configuration and enable status */
312 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_CHANNEL_ENABLE
);
315 data
->channel_enable
= status
;
317 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_OFFSET_ENABLE
);
320 data
->offset_enable
= status
;
322 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_CONF
);
325 data
->orig_conf
= status
;
326 data
->current_conf
= status
;
328 err
= max31730_write_config(data
,
329 data
->channel_enable
? 0 : MAX31730_STOP
,
330 data
->channel_enable
? MAX31730_STOP
: 0);
334 dev_set_drvdata(dev
, data
);
336 err
= devm_add_action_or_reset(dev
, max31730_remove
, data
);
340 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
344 return PTR_ERR_OR_ZERO(hwmon_dev
);
347 static const struct i2c_device_id max31730_ids
[] = {
351 MODULE_DEVICE_TABLE(i2c
, max31730_ids
);
353 static const struct of_device_id __maybe_unused max31730_of_match
[] = {
355 .compatible
= "maxim,max31730",
359 MODULE_DEVICE_TABLE(of
, max31730_of_match
);
361 static bool max31730_check_reg_temp(struct i2c_client
*client
,
366 regval
= i2c_smbus_read_byte_data(client
, reg
+ 1);
367 return regval
< 0 || (regval
& 0x0f);
370 /* Return 0 if detection is successful, -ENODEV otherwise */
371 static int max31730_detect(struct i2c_client
*client
,
372 struct i2c_board_info
*info
)
374 struct i2c_adapter
*adapter
= client
->adapter
;
378 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
379 I2C_FUNC_SMBUS_WORD_DATA
))
382 regval
= i2c_smbus_read_byte_data(client
, MAX31730_REG_MFG_ID
);
383 if (regval
!= MAX31730_MFG_ID
)
385 regval
= i2c_smbus_read_byte_data(client
, MAX31730_REG_MFG_REV
);
386 if (regval
!= MAX31730_MFG_REV
)
389 /* lower 4 bit of temperature and limit registers must be 0 */
390 if (max31730_check_reg_temp(client
, MAX31730_REG_TEMP_MIN
))
393 for (i
= 0; i
< 4; i
++) {
394 if (max31730_check_reg_temp(client
, MAX31730_REG_TEMP
+ i
* 2))
396 if (max31730_check_reg_temp(client
,
397 MAX31730_REG_TEMP_MAX
+ i
* 2))
401 strscpy(info
->type
, "max31730", I2C_NAME_SIZE
);
406 static int max31730_suspend(struct device
*dev
)
408 struct max31730_data
*data
= dev_get_drvdata(dev
);
410 return max31730_write_config(data
, MAX31730_STOP
, 0);
413 static int max31730_resume(struct device
*dev
)
415 struct max31730_data
*data
= dev_get_drvdata(dev
);
417 return max31730_write_config(data
, 0, MAX31730_STOP
);
420 static DEFINE_SIMPLE_DEV_PM_OPS(max31730_pm_ops
, max31730_suspend
, max31730_resume
);
422 static struct i2c_driver max31730_driver
= {
423 .class = I2C_CLASS_HWMON
,
426 .of_match_table
= of_match_ptr(max31730_of_match
),
427 .pm
= pm_sleep_ptr(&max31730_pm_ops
),
429 .probe
= max31730_probe
,
430 .id_table
= max31730_ids
,
431 .detect
= max31730_detect
,
432 .address_list
= normal_i2c
,
435 module_i2c_driver(max31730_driver
);
437 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
438 MODULE_DESCRIPTION("MAX31730 driver");
439 MODULE_LICENSE("GPL");