1 // SPDX-License-Identifier: GPL-2.0
3 * max31827.c - Support for Maxim Low-Power Switch
5 * Copyright (c) 2023 Daniel Matyas <daniel.matyas@analog.com>
8 #include <linux/bitfield.h>
9 #include <linux/bitops.h>
10 #include <linux/delay.h>
11 #include <linux/hwmon.h>
12 #include <linux/i2c.h>
13 #include <linux/mutex.h>
14 #include <linux/of_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
18 #define MAX31827_T_REG 0x0
19 #define MAX31827_CONFIGURATION_REG 0x2
20 #define MAX31827_TH_REG 0x4
21 #define MAX31827_TL_REG 0x6
22 #define MAX31827_TH_HYST_REG 0x8
23 #define MAX31827_TL_HYST_REG 0xA
25 #define MAX31827_CONFIGURATION_1SHOT_MASK BIT(0)
26 #define MAX31827_CONFIGURATION_CNV_RATE_MASK GENMASK(3, 1)
27 #define MAX31827_CONFIGURATION_PEC_EN_MASK BIT(4)
28 #define MAX31827_CONFIGURATION_TIMEOUT_MASK BIT(5)
29 #define MAX31827_CONFIGURATION_RESOLUTION_MASK GENMASK(7, 6)
30 #define MAX31827_CONFIGURATION_ALRM_POL_MASK BIT(8)
31 #define MAX31827_CONFIGURATION_COMP_INT_MASK BIT(9)
32 #define MAX31827_CONFIGURATION_FLT_Q_MASK GENMASK(11, 10)
33 #define MAX31827_CONFIGURATION_U_TEMP_STAT_MASK BIT(14)
34 #define MAX31827_CONFIGURATION_O_TEMP_STAT_MASK BIT(15)
36 #define MAX31827_ALRM_POL_LOW 0x0
37 #define MAX31827_ALRM_POL_HIGH 0x1
38 #define MAX31827_FLT_Q_1 0x0
39 #define MAX31827_FLT_Q_4 0x2
41 #define MAX31827_8_BIT_CNV_TIME 9
42 #define MAX31827_9_BIT_CNV_TIME 18
43 #define MAX31827_10_BIT_CNV_TIME 35
44 #define MAX31827_12_BIT_CNV_TIME 140
46 #define MAX31827_16_BIT_TO_M_DGR(x) (sign_extend32(x, 15) * 1000 / 16)
47 #define MAX31827_M_DGR_TO_16_BIT(x) (((x) << 4) / 1000)
48 #define MAX31827_DEVICE_ENABLE(x) ((x) ? 0xA : 0x0)
51 * The enum passed in the .data pointer of struct of_device_id must
52 * start with a value != 0 since that is a requirement for using
53 * device_get_match_data().
55 enum chips
{ max31827
= 1, max31828
, max31829
};
58 MAX31827_CNV_1_DIV_64_HZ
= 1,
59 MAX31827_CNV_1_DIV_32_HZ
,
60 MAX31827_CNV_1_DIV_16_HZ
,
61 MAX31827_CNV_1_DIV_4_HZ
,
67 static const u16 max31827_conversions
[] = {
68 [MAX31827_CNV_1_DIV_64_HZ
] = 64000,
69 [MAX31827_CNV_1_DIV_32_HZ
] = 32000,
70 [MAX31827_CNV_1_DIV_16_HZ
] = 16000,
71 [MAX31827_CNV_1_DIV_4_HZ
] = 4000,
72 [MAX31827_CNV_1_HZ
] = 1000,
73 [MAX31827_CNV_4_HZ
] = 250,
74 [MAX31827_CNV_8_HZ
] = 125,
77 enum max31827_resolution
{
78 MAX31827_RES_8_BIT
= 0,
84 static const u16 max31827_resolutions
[] = {
85 [MAX31827_RES_8_BIT
] = 1000,
86 [MAX31827_RES_9_BIT
] = 500,
87 [MAX31827_RES_10_BIT
] = 250,
88 [MAX31827_RES_12_BIT
] = 62,
91 static const u16 max31827_conv_times
[] = {
92 [MAX31827_RES_8_BIT
] = MAX31827_8_BIT_CNV_TIME
,
93 [MAX31827_RES_9_BIT
] = MAX31827_9_BIT_CNV_TIME
,
94 [MAX31827_RES_10_BIT
] = MAX31827_10_BIT_CNV_TIME
,
95 [MAX31827_RES_12_BIT
] = MAX31827_12_BIT_CNV_TIME
,
98 struct max31827_state
{
100 * Prevent simultaneous access to the i2c client.
103 struct regmap
*regmap
;
105 unsigned int resolution
;
106 unsigned int update_interval
;
109 static const struct regmap_config max31827_regmap
= {
115 static int shutdown_write(struct max31827_state
*st
, unsigned int reg
,
116 unsigned int mask
, unsigned int val
)
119 unsigned int cnv_rate
;
123 * Before the Temperature Threshold Alarm, Alarm Hysteresis Threshold
124 * and Resolution bits from Configuration register are changed over I2C,
125 * the part must be in shutdown mode.
127 * Mutex is used to ensure, that some other process doesn't change the
128 * configuration register.
130 mutex_lock(&st
->lock
);
134 ret
= regmap_write(st
->regmap
, reg
, val
);
136 ret
= regmap_update_bits(st
->regmap
, reg
, mask
, val
);
140 ret
= regmap_read(st
->regmap
, MAX31827_CONFIGURATION_REG
, &cfg
);
144 cnv_rate
= MAX31827_CONFIGURATION_CNV_RATE_MASK
& cfg
;
145 cfg
= cfg
& ~(MAX31827_CONFIGURATION_1SHOT_MASK
|
146 MAX31827_CONFIGURATION_CNV_RATE_MASK
);
147 ret
= regmap_write(st
->regmap
, MAX31827_CONFIGURATION_REG
, cfg
);
152 ret
= regmap_write(st
->regmap
, reg
, val
);
154 ret
= regmap_update_bits(st
->regmap
, reg
, mask
, val
);
159 ret
= regmap_update_bits(st
->regmap
, MAX31827_CONFIGURATION_REG
,
160 MAX31827_CONFIGURATION_CNV_RATE_MASK
,
164 mutex_unlock(&st
->lock
);
168 static int write_alarm_val(struct max31827_state
*st
, unsigned int reg
,
171 val
= MAX31827_M_DGR_TO_16_BIT(val
);
173 return shutdown_write(st
, reg
, 0, val
);
176 static umode_t
max31827_is_visible(const void *state
,
177 enum hwmon_sensor_types type
, u32 attr
,
180 if (type
== hwmon_temp
) {
182 case hwmon_temp_enable
:
185 case hwmon_temp_max_hyst
:
186 case hwmon_temp_min_hyst
:
188 case hwmon_temp_input
:
189 case hwmon_temp_min_alarm
:
190 case hwmon_temp_max_alarm
:
195 } else if (type
== hwmon_chip
) {
196 if (attr
== hwmon_chip_update_interval
)
203 static int max31827_read(struct device
*dev
, enum hwmon_sensor_types type
,
204 u32 attr
, int channel
, long *val
)
206 struct max31827_state
*st
= dev_get_drvdata(dev
);
213 case hwmon_temp_enable
:
214 ret
= regmap_read(st
->regmap
,
215 MAX31827_CONFIGURATION_REG
, &uval
);
219 uval
= FIELD_GET(MAX31827_CONFIGURATION_1SHOT_MASK
|
220 MAX31827_CONFIGURATION_CNV_RATE_MASK
,
225 case hwmon_temp_input
:
226 mutex_lock(&st
->lock
);
230 * This operation requires mutex protection,
231 * because the chip configuration should not
232 * be changed during the conversion process.
235 ret
= regmap_update_bits(st
->regmap
,
236 MAX31827_CONFIGURATION_REG
,
237 MAX31827_CONFIGURATION_1SHOT_MASK
,
240 mutex_unlock(&st
->lock
);
243 msleep(max31827_conv_times
[st
->resolution
]);
247 * For 12-bit resolution the conversion time is 140 ms,
248 * thus an additional 15 ms is needed to complete the
249 * conversion: 125 ms + 15 ms = 140 ms
251 if (max31827_resolutions
[st
->resolution
] == 12 &&
252 st
->update_interval
== 125)
253 usleep_range(15000, 20000);
255 ret
= regmap_read(st
->regmap
, MAX31827_T_REG
, &uval
);
257 mutex_unlock(&st
->lock
);
262 *val
= MAX31827_16_BIT_TO_M_DGR(uval
);
266 ret
= regmap_read(st
->regmap
, MAX31827_TH_REG
, &uval
);
270 *val
= MAX31827_16_BIT_TO_M_DGR(uval
);
272 case hwmon_temp_max_hyst
:
273 ret
= regmap_read(st
->regmap
, MAX31827_TH_HYST_REG
,
278 *val
= MAX31827_16_BIT_TO_M_DGR(uval
);
280 case hwmon_temp_max_alarm
:
281 ret
= regmap_read(st
->regmap
,
282 MAX31827_CONFIGURATION_REG
, &uval
);
286 *val
= FIELD_GET(MAX31827_CONFIGURATION_O_TEMP_STAT_MASK
,
290 ret
= regmap_read(st
->regmap
, MAX31827_TL_REG
, &uval
);
294 *val
= MAX31827_16_BIT_TO_M_DGR(uval
);
296 case hwmon_temp_min_hyst
:
297 ret
= regmap_read(st
->regmap
, MAX31827_TL_HYST_REG
,
302 *val
= MAX31827_16_BIT_TO_M_DGR(uval
);
304 case hwmon_temp_min_alarm
:
305 ret
= regmap_read(st
->regmap
,
306 MAX31827_CONFIGURATION_REG
, &uval
);
310 *val
= FIELD_GET(MAX31827_CONFIGURATION_U_TEMP_STAT_MASK
,
321 if (attr
== hwmon_chip_update_interval
) {
322 ret
= regmap_read(st
->regmap
,
323 MAX31827_CONFIGURATION_REG
, &uval
);
327 uval
= FIELD_GET(MAX31827_CONFIGURATION_CNV_RATE_MASK
,
329 *val
= max31827_conversions
[uval
];
341 static int max31827_write(struct device
*dev
, enum hwmon_sensor_types type
,
342 u32 attr
, int channel
, long val
)
344 struct max31827_state
*st
= dev_get_drvdata(dev
);
351 case hwmon_temp_enable
:
355 mutex_lock(&st
->lock
);
357 * The chip should not be enabled while a conversion is
358 * performed. Neither should the chip be enabled when
359 * the alarm values are changed.
364 ret
= regmap_update_bits(st
->regmap
,
365 MAX31827_CONFIGURATION_REG
,
366 MAX31827_CONFIGURATION_1SHOT_MASK
|
367 MAX31827_CONFIGURATION_CNV_RATE_MASK
,
368 MAX31827_DEVICE_ENABLE(val
));
370 mutex_unlock(&st
->lock
);
375 return write_alarm_val(st
, MAX31827_TH_REG
, val
);
377 case hwmon_temp_max_hyst
:
378 return write_alarm_val(st
, MAX31827_TH_HYST_REG
, val
);
381 return write_alarm_val(st
, MAX31827_TL_REG
, val
);
383 case hwmon_temp_min_hyst
:
384 return write_alarm_val(st
, MAX31827_TL_HYST_REG
, val
);
392 case hwmon_chip_update_interval
:
397 * Convert the desired conversion rate into register
398 * bits. res is already initialized with 1.
400 * This was inspired by lm73 driver.
402 while (res
< ARRAY_SIZE(max31827_conversions
) &&
403 val
< max31827_conversions
[res
])
406 if (res
== ARRAY_SIZE(max31827_conversions
))
407 res
= ARRAY_SIZE(max31827_conversions
) - 1;
409 res
= FIELD_PREP(MAX31827_CONFIGURATION_CNV_RATE_MASK
,
412 ret
= regmap_update_bits(st
->regmap
,
413 MAX31827_CONFIGURATION_REG
,
414 MAX31827_CONFIGURATION_CNV_RATE_MASK
,
419 st
->update_interval
= val
;
423 return regmap_update_bits(st
->regmap
, MAX31827_CONFIGURATION_REG
,
424 MAX31827_CONFIGURATION_PEC_EN_MASK
,
425 val
? MAX31827_CONFIGURATION_PEC_EN_MASK
: 0);
434 static ssize_t
temp1_resolution_show(struct device
*dev
,
435 struct device_attribute
*devattr
,
438 struct max31827_state
*st
= dev_get_drvdata(dev
);
442 ret
= regmap_read(st
->regmap
, MAX31827_CONFIGURATION_REG
, &val
);
446 val
= FIELD_GET(MAX31827_CONFIGURATION_RESOLUTION_MASK
, val
);
448 return scnprintf(buf
, PAGE_SIZE
, "%u\n", max31827_resolutions
[val
]);
451 static ssize_t
temp1_resolution_store(struct device
*dev
,
452 struct device_attribute
*devattr
,
453 const char *buf
, size_t count
)
455 struct max31827_state
*st
= dev_get_drvdata(dev
);
456 unsigned int idx
= 0;
460 ret
= kstrtouint(buf
, 10, &val
);
465 * Convert the desired resolution into register
466 * bits. idx is already initialized with 0.
468 * This was inspired by lm73 driver.
470 while (idx
< ARRAY_SIZE(max31827_resolutions
) &&
471 val
< max31827_resolutions
[idx
])
474 if (idx
== ARRAY_SIZE(max31827_resolutions
))
475 idx
= ARRAY_SIZE(max31827_resolutions
) - 1;
477 st
->resolution
= idx
;
479 ret
= shutdown_write(st
, MAX31827_CONFIGURATION_REG
,
480 MAX31827_CONFIGURATION_RESOLUTION_MASK
,
481 FIELD_PREP(MAX31827_CONFIGURATION_RESOLUTION_MASK
,
484 return ret
? ret
: count
;
487 static DEVICE_ATTR_RW(temp1_resolution
);
489 static struct attribute
*max31827_attrs
[] = {
490 &dev_attr_temp1_resolution
.attr
,
493 ATTRIBUTE_GROUPS(max31827
);
495 static const struct i2c_device_id max31827_i2c_ids
[] = {
496 { "max31827", max31827
},
497 { "max31828", max31828
},
498 { "max31829", max31829
},
501 MODULE_DEVICE_TABLE(i2c
, max31827_i2c_ids
);
503 static int max31827_init_client(struct max31827_state
*st
,
506 struct fwnode_handle
*fwnode
;
507 unsigned int res
= 0;
513 fwnode
= dev_fwnode(dev
);
516 res
|= MAX31827_DEVICE_ENABLE(1);
518 res
|= MAX31827_CONFIGURATION_RESOLUTION_MASK
;
520 prop
= fwnode_property_read_bool(fwnode
, "adi,comp-int");
521 res
|= FIELD_PREP(MAX31827_CONFIGURATION_COMP_INT_MASK
, prop
);
523 prop
= fwnode_property_read_bool(fwnode
, "adi,timeout-enable");
524 res
|= FIELD_PREP(MAX31827_CONFIGURATION_TIMEOUT_MASK
, !prop
);
526 type
= (enum chips
)(uintptr_t)device_get_match_data(dev
);
528 if (fwnode_property_present(fwnode
, "adi,alarm-pol")) {
529 ret
= fwnode_property_read_u32(fwnode
, "adi,alarm-pol", &data
);
533 res
|= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK
, !!data
);
541 res
|= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK
,
542 MAX31827_ALRM_POL_LOW
);
545 res
|= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK
,
546 MAX31827_ALRM_POL_HIGH
);
553 if (fwnode_property_present(fwnode
, "adi,fault-q")) {
554 ret
= fwnode_property_read_u32(fwnode
, "adi,fault-q", &data
);
559 * Convert the desired fault queue into register bits.
562 lsb_idx
= __ffs(data
);
564 if (hweight32(data
) != 1 || lsb_idx
> 4) {
565 dev_err(dev
, "Invalid data in adi,fault-q\n");
569 res
|= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK
, lsb_idx
);
576 res
|= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK
,
581 res
|= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK
,
589 return regmap_write(st
->regmap
, MAX31827_CONFIGURATION_REG
, res
);
592 static const struct hwmon_channel_info
*max31827_info
[] = {
593 HWMON_CHANNEL_INFO(temp
, HWMON_T_ENABLE
| HWMON_T_INPUT
| HWMON_T_MIN
|
594 HWMON_T_MIN_HYST
| HWMON_T_MIN_ALARM
|
595 HWMON_T_MAX
| HWMON_T_MAX_HYST
|
597 HWMON_CHANNEL_INFO(chip
, HWMON_C_UPDATE_INTERVAL
| HWMON_C_PEC
),
601 static const struct hwmon_ops max31827_hwmon_ops
= {
602 .is_visible
= max31827_is_visible
,
603 .read
= max31827_read
,
604 .write
= max31827_write
,
607 static const struct hwmon_chip_info max31827_chip_info
= {
608 .ops
= &max31827_hwmon_ops
,
609 .info
= max31827_info
,
612 static int max31827_probe(struct i2c_client
*client
)
614 struct device
*dev
= &client
->dev
;
615 struct device
*hwmon_dev
;
616 struct max31827_state
*st
;
619 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
622 st
= devm_kzalloc(dev
, sizeof(*st
), GFP_KERNEL
);
626 mutex_init(&st
->lock
);
628 st
->regmap
= devm_regmap_init_i2c(client
, &max31827_regmap
);
629 if (IS_ERR(st
->regmap
))
630 return dev_err_probe(dev
, PTR_ERR(st
->regmap
),
631 "Failed to allocate regmap.\n");
633 err
= devm_regulator_get_enable(dev
, "vref");
635 return dev_err_probe(dev
, err
, "failed to enable regulator\n");
637 err
= max31827_init_client(st
, dev
);
641 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
, st
,
645 return PTR_ERR_OR_ZERO(hwmon_dev
);
648 static const struct of_device_id max31827_of_match
[] = {
650 .compatible
= "adi,max31827",
651 .data
= (void *)max31827
654 .compatible
= "adi,max31828",
655 .data
= (void *)max31828
658 .compatible
= "adi,max31829",
659 .data
= (void *)max31829
663 MODULE_DEVICE_TABLE(of
, max31827_of_match
);
665 static struct i2c_driver max31827_driver
= {
668 .of_match_table
= max31827_of_match
,
670 .probe
= max31827_probe
,
671 .id_table
= max31827_i2c_ids
,
673 module_i2c_driver(max31827_driver
);
675 MODULE_AUTHOR("Daniel Matyas <daniel.matyas@analog.com>");
676 MODULE_DESCRIPTION("Maxim MAX31827 low-power temperature switch driver");
677 MODULE_LICENSE("GPL");