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>
14 #include <linux/of_device.h>
16 #include <linux/slab.h>
18 /* Addresses scanned */
19 static const unsigned short normal_i2c
[] = { 0x1c, 0x1d, 0x1e, 0x1f, 0x4c,
20 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
};
22 /* The MAX31730 registers */
23 #define MAX31730_REG_TEMP 0x00
24 #define MAX31730_REG_CONF 0x13
25 #define MAX31730_STOP BIT(7)
26 #define MAX31730_EXTRANGE BIT(1)
27 #define MAX31730_REG_TEMP_OFFSET 0x16
28 #define MAX31730_TEMP_OFFSET_BASELINE 0x77
29 #define MAX31730_REG_OFFSET_ENABLE 0x17
30 #define MAX31730_REG_TEMP_MAX 0x20
31 #define MAX31730_REG_TEMP_MIN 0x30
32 #define MAX31730_REG_STATUS_HIGH 0x32
33 #define MAX31730_REG_STATUS_LOW 0x33
34 #define MAX31730_REG_CHANNEL_ENABLE 0x35
35 #define MAX31730_REG_TEMP_FAULT 0x36
37 #define MAX31730_REG_MFG_ID 0x50
38 #define MAX31730_MFG_ID 0x4d
39 #define MAX31730_REG_MFG_REV 0x51
40 #define MAX31730_MFG_REV 0x01
42 #define MAX31730_TEMP_MIN (-128000)
43 #define MAX31730_TEMP_MAX 127937
45 /* Each client has this additional data */
46 struct max31730_data
{
47 struct i2c_client
*client
;
54 /*-----------------------------------------------------------------------*/
56 static inline long max31730_reg_to_mc(s16 temp
)
58 return DIV_ROUND_CLOSEST((temp
>> 4) * 1000, 16);
61 static int max31730_write_config(struct max31730_data
*data
, u8 set_mask
,
66 clr_mask
|= MAX31730_EXTRANGE
;
67 value
= data
->current_conf
& ~clr_mask
;
70 if (data
->current_conf
!= value
) {
73 err
= i2c_smbus_write_byte_data(data
->client
, MAX31730_REG_CONF
,
77 data
->current_conf
= value
;
82 static int max31730_set_enable(struct i2c_client
*client
, int reg
,
83 u8
*confdata
, int channel
, bool enable
)
85 u8 regval
= *confdata
;
89 regval
|= BIT(channel
);
91 regval
&= ~BIT(channel
);
93 if (regval
!= *confdata
) {
94 err
= i2c_smbus_write_byte_data(client
, reg
, regval
);
102 static int max31730_set_offset_enable(struct max31730_data
*data
, int channel
,
105 return max31730_set_enable(data
->client
, MAX31730_REG_OFFSET_ENABLE
,
106 &data
->offset_enable
, channel
, enable
);
109 static int max31730_set_channel_enable(struct max31730_data
*data
, int channel
,
112 return max31730_set_enable(data
->client
, MAX31730_REG_CHANNEL_ENABLE
,
113 &data
->channel_enable
, channel
, enable
);
116 static int max31730_read(struct device
*dev
, enum hwmon_sensor_types type
,
117 u32 attr
, int channel
, long *val
)
119 struct max31730_data
*data
= dev_get_drvdata(dev
);
120 int regval
, reg
, offset
;
122 if (type
!= hwmon_temp
)
126 case hwmon_temp_input
:
127 if (!(data
->channel_enable
& BIT(channel
)))
129 reg
= MAX31730_REG_TEMP
+ (channel
* 2);
132 reg
= MAX31730_REG_TEMP_MAX
+ (channel
* 2);
135 reg
= MAX31730_REG_TEMP_MIN
;
137 case hwmon_temp_enable
:
138 *val
= !!(data
->channel_enable
& BIT(channel
));
140 case hwmon_temp_offset
:
143 if (!(data
->offset_enable
& BIT(channel
))) {
147 offset
= i2c_smbus_read_byte_data(data
->client
,
148 MAX31730_REG_TEMP_OFFSET
);
151 *val
= (offset
- MAX31730_TEMP_OFFSET_BASELINE
) * 125;
153 case hwmon_temp_fault
:
154 regval
= i2c_smbus_read_byte_data(data
->client
,
155 MAX31730_REG_TEMP_FAULT
);
158 *val
= !!(regval
& BIT(channel
));
160 case hwmon_temp_min_alarm
:
161 regval
= i2c_smbus_read_byte_data(data
->client
,
162 MAX31730_REG_STATUS_LOW
);
165 *val
= !!(regval
& BIT(channel
));
167 case hwmon_temp_max_alarm
:
168 regval
= i2c_smbus_read_byte_data(data
->client
,
169 MAX31730_REG_STATUS_HIGH
);
172 *val
= !!(regval
& BIT(channel
));
177 regval
= i2c_smbus_read_word_swapped(data
->client
, reg
);
181 *val
= max31730_reg_to_mc(regval
);
186 static int max31730_write(struct device
*dev
, enum hwmon_sensor_types type
,
187 u32 attr
, int channel
, long val
)
189 struct max31730_data
*data
= dev_get_drvdata(dev
);
192 if (type
!= hwmon_temp
)
197 reg
= MAX31730_REG_TEMP_MAX
+ channel
* 2;
200 reg
= MAX31730_REG_TEMP_MIN
;
202 case hwmon_temp_enable
:
203 if (val
!= 0 && val
!= 1)
205 return max31730_set_channel_enable(data
, channel
, val
);
206 case hwmon_temp_offset
:
207 val
= clamp_val(val
, -14875, 17000) + 14875;
208 val
= DIV_ROUND_CLOSEST(val
, 125);
209 err
= max31730_set_offset_enable(data
, channel
,
210 val
!= MAX31730_TEMP_OFFSET_BASELINE
);
213 return i2c_smbus_write_byte_data(data
->client
,
214 MAX31730_REG_TEMP_OFFSET
, val
);
219 val
= clamp_val(val
, MAX31730_TEMP_MIN
, MAX31730_TEMP_MAX
);
220 val
= DIV_ROUND_CLOSEST(val
<< 4, 1000) << 4;
222 return i2c_smbus_write_word_swapped(data
->client
, reg
, (u16
)val
);
225 static umode_t
max31730_is_visible(const void *data
,
226 enum hwmon_sensor_types type
,
227 u32 attr
, int channel
)
232 case hwmon_temp_input
:
233 case hwmon_temp_min_alarm
:
234 case hwmon_temp_max_alarm
:
235 case hwmon_temp_fault
:
238 return channel
? 0444 : 0644;
239 case hwmon_temp_offset
:
240 case hwmon_temp_enable
:
251 static const struct hwmon_channel_info
*max31730_info
[] = {
252 HWMON_CHANNEL_INFO(chip
,
253 HWMON_C_REGISTER_TZ
),
254 HWMON_CHANNEL_INFO(temp
,
255 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
257 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
,
258 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
259 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
260 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
262 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
263 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
264 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
266 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
267 HWMON_T_OFFSET
| HWMON_T_ENABLE
|
268 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
274 static const struct hwmon_ops max31730_hwmon_ops
= {
275 .is_visible
= max31730_is_visible
,
276 .read
= max31730_read
,
277 .write
= max31730_write
,
280 static const struct hwmon_chip_info max31730_chip_info
= {
281 .ops
= &max31730_hwmon_ops
,
282 .info
= max31730_info
,
285 static void max31730_remove(void *data
)
287 struct max31730_data
*max31730
= data
;
288 struct i2c_client
*client
= max31730
->client
;
290 i2c_smbus_write_byte_data(client
, MAX31730_REG_CONF
,
291 max31730
->orig_conf
);
295 max31730_probe(struct i2c_client
*client
)
297 struct device
*dev
= &client
->dev
;
298 struct device
*hwmon_dev
;
299 struct max31730_data
*data
;
302 if (!i2c_check_functionality(client
->adapter
,
303 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
306 data
= devm_kzalloc(dev
, sizeof(struct max31730_data
), GFP_KERNEL
);
310 data
->client
= client
;
312 /* Cache original configuration and enable status */
313 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_CHANNEL_ENABLE
);
316 data
->channel_enable
= status
;
318 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_OFFSET_ENABLE
);
321 data
->offset_enable
= status
;
323 status
= i2c_smbus_read_byte_data(client
, MAX31730_REG_CONF
);
326 data
->orig_conf
= status
;
327 data
->current_conf
= status
;
329 err
= max31730_write_config(data
,
330 data
->channel_enable
? 0 : MAX31730_STOP
,
331 data
->channel_enable
? MAX31730_STOP
: 0);
335 dev_set_drvdata(dev
, data
);
337 err
= devm_add_action_or_reset(dev
, max31730_remove
, data
);
341 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
345 return PTR_ERR_OR_ZERO(hwmon_dev
);
348 static const struct i2c_device_id max31730_ids
[] = {
352 MODULE_DEVICE_TABLE(i2c
, max31730_ids
);
354 static const struct of_device_id __maybe_unused max31730_of_match
[] = {
356 .compatible
= "maxim,max31730",
360 MODULE_DEVICE_TABLE(of
, max31730_of_match
);
362 static bool max31730_check_reg_temp(struct i2c_client
*client
,
367 regval
= i2c_smbus_read_byte_data(client
, reg
+ 1);
368 return regval
< 0 || (regval
& 0x0f);
371 /* Return 0 if detection is successful, -ENODEV otherwise */
372 static int max31730_detect(struct i2c_client
*client
,
373 struct i2c_board_info
*info
)
375 struct i2c_adapter
*adapter
= client
->adapter
;
379 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
380 I2C_FUNC_SMBUS_WORD_DATA
))
383 regval
= i2c_smbus_read_byte_data(client
, MAX31730_REG_MFG_ID
);
384 if (regval
!= MAX31730_MFG_ID
)
386 regval
= i2c_smbus_read_byte_data(client
, MAX31730_REG_MFG_REV
);
387 if (regval
!= MAX31730_MFG_REV
)
390 /* lower 4 bit of temperature and limit registers must be 0 */
391 if (max31730_check_reg_temp(client
, MAX31730_REG_TEMP_MIN
))
394 for (i
= 0; i
< 4; i
++) {
395 if (max31730_check_reg_temp(client
, MAX31730_REG_TEMP
+ i
* 2))
397 if (max31730_check_reg_temp(client
,
398 MAX31730_REG_TEMP_MAX
+ i
* 2))
402 strlcpy(info
->type
, "max31730", I2C_NAME_SIZE
);
407 static int __maybe_unused
max31730_suspend(struct device
*dev
)
409 struct max31730_data
*data
= dev_get_drvdata(dev
);
411 return max31730_write_config(data
, MAX31730_STOP
, 0);
414 static int __maybe_unused
max31730_resume(struct device
*dev
)
416 struct max31730_data
*data
= dev_get_drvdata(dev
);
418 return max31730_write_config(data
, 0, MAX31730_STOP
);
421 static SIMPLE_DEV_PM_OPS(max31730_pm_ops
, max31730_suspend
, max31730_resume
);
423 static struct i2c_driver max31730_driver
= {
424 .class = I2C_CLASS_HWMON
,
427 .of_match_table
= of_match_ptr(max31730_of_match
),
428 .pm
= &max31730_pm_ops
,
430 .probe_new
= max31730_probe
,
431 .id_table
= max31730_ids
,
432 .detect
= max31730_detect
,
433 .address_list
= normal_i2c
,
436 module_i2c_driver(max31730_driver
);
438 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
439 MODULE_DESCRIPTION("MAX31730 driver");
440 MODULE_LICENSE("GPL");