1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2012 Guenter Roeck <linux@roeck-us.net>
6 * Copyright (c) 2011 David George <david.george@ska.ac.za>
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 #include <linux/err.h>
12 #include <linux/hwmon.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 enum chips
{ max6581
, max6602
, max6622
, max6636
, max6689
, max6693
, max6694
,
22 max6697
, max6698
, max6699
};
24 /* Report local sensor as temp1 */
26 static const u8 MAX6697_REG_TEMP
[] = {
27 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 };
28 static const u8 MAX6697_REG_TEMP_EXT
[] = {
29 0x57, 0x09, 0x52, 0x53, 0x54, 0x55, 0x56, 0 };
30 static const u8 MAX6697_REG_MAX
[] = {
31 0x17, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18 };
32 static const u8 MAX6697_REG_CRIT
[] = {
33 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
35 #define MAX6697_REG_MIN 0x30
37 * Map device tree / internal register bit map to chip bit map.
38 * Applies to alert register and over-temperature register.
41 #define MAX6697_EXTERNAL_MASK_DT GENMASK(7, 1)
42 #define MAX6697_LOCAL_MASK_DT BIT(0)
43 #define MAX6697_EXTERNAL_MASK_CHIP GENMASK(6, 0)
44 #define MAX6697_LOCAL_MASK_CHIP BIT(7)
46 /* alert - local channel is in bit 6 */
47 #define MAX6697_ALERT_MAP_BITS(reg) ((((reg) & 0x7e) >> 1) | \
48 (((reg) & 0x01) << 6) | ((reg) & 0x80))
50 /* over-temperature - local channel is in bit 7 */
51 #define MAX6697_OVERT_MAP_BITS(reg) \
52 (FIELD_PREP(MAX6697_EXTERNAL_MASK_CHIP, FIELD_GET(MAX6697_EXTERNAL_MASK_DT, reg)) | \
53 FIELD_PREP(MAX6697_LOCAL_MASK_CHIP, FIELD_GET(MAX6697_LOCAL_MASK_DT, reg)))
55 #define MAX6697_REG_STAT_ALARM 0x44
56 #define MAX6697_REG_STAT_CRIT 0x45
57 #define MAX6697_REG_STAT_FAULT 0x46
58 #define MAX6697_REG_STAT_MIN_ALARM 0x47
60 #define MAX6697_REG_CONFIG 0x41
61 #define MAX6581_CONF_EXTENDED BIT(1)
62 #define MAX6693_CONF_BETA BIT(2)
63 #define MAX6697_CONF_RESISTANCE BIT(3)
64 #define MAX6697_CONF_TIMEOUT BIT(5)
65 #define MAX6697_REG_ALERT_MASK 0x42
66 #define MAX6697_REG_OVERT_MASK 0x43
68 #define MAX6581_REG_RESISTANCE 0x4a
69 #define MAX6581_REG_IDEALITY 0x4b
70 #define MAX6581_REG_IDEALITY_SELECT 0x4c
71 #define MAX6581_REG_OFFSET 0x4d
72 #define MAX6581_REG_OFFSET_SELECT 0x4e
73 #define MAX6581_OFFSET_MIN -31750
74 #define MAX6581_OFFSET_MAX 31750
76 #define MAX6697_CONV_TIME 156 /* ms per channel, worst case */
78 struct max6697_chip_data
{
87 struct regmap
*regmap
;
90 const struct max6697_chip_data
*chip
;
92 int temp_offset
; /* in degrees C */
94 struct mutex update_lock
;
96 #define MAX6697_TEMP_INPUT 0
97 #define MAX6697_TEMP_EXT 1
98 #define MAX6697_TEMP_MAX 2
99 #define MAX6697_TEMP_CRIT 3
103 static const struct max6697_chip_data max6697_chip_data
[] = {
109 .valid_conf
= MAX6581_CONF_EXTENDED
| MAX6697_CONF_TIMEOUT
,
116 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
123 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
130 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
137 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
144 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6693_CONF_BETA
|
145 MAX6697_CONF_TIMEOUT
,
152 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6693_CONF_BETA
|
153 MAX6697_CONF_TIMEOUT
,
160 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
167 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
174 .valid_conf
= MAX6697_CONF_RESISTANCE
| MAX6697_CONF_TIMEOUT
,
178 static int max6697_alarm_channel_map(int channel
)
190 static int max6697_read(struct device
*dev
, enum hwmon_sensor_types type
,
191 u32 attr
, int channel
, long *val
)
193 unsigned int offset_regs
[2] = { MAX6581_REG_OFFSET_SELECT
, MAX6581_REG_OFFSET
};
194 unsigned int temp_regs
[2] = { MAX6697_REG_TEMP
[channel
],
195 MAX6697_REG_TEMP_EXT
[channel
] };
196 struct max6697_data
*data
= dev_get_drvdata(dev
);
197 struct regmap
*regmap
= data
->regmap
;
203 case hwmon_temp_input
:
204 ret
= regmap_multi_reg_read(regmap
, temp_regs
, regdata
,
205 data
->chip
->have_ext
& BIT(channel
) ? 2 : 1);
208 *val
= (((regdata
[0] - data
->temp_offset
) << 3) | (regdata
[1] >> 5)) * 125;
211 ret
= regmap_read(regmap
, MAX6697_REG_MAX
[channel
], ®val
);
214 *val
= ((int)regval
- data
->temp_offset
) * 1000;
216 case hwmon_temp_crit
:
217 ret
= regmap_read(regmap
, MAX6697_REG_CRIT
[channel
], ®val
);
220 *val
= ((int)regval
- data
->temp_offset
) * 1000;
223 ret
= regmap_read(regmap
, MAX6697_REG_MIN
, ®val
);
226 *val
= ((int)regval
- data
->temp_offset
) * 1000;
228 case hwmon_temp_offset
:
229 ret
= regmap_multi_reg_read(regmap
, offset_regs
, regdata
, 2);
233 if (!(regdata
[0] & BIT(channel
- 1)))
236 *val
= sign_extend32(regdata
[1], 7) * 250;
238 case hwmon_temp_fault
:
239 ret
= regmap_read(regmap
, MAX6697_REG_STAT_FAULT
, ®val
);
242 if (data
->type
== max6581
)
243 *val
= !!(regval
& BIT(channel
- 1));
245 *val
= !!(regval
& BIT(channel
));
247 case hwmon_temp_crit_alarm
:
248 ret
= regmap_read(regmap
, MAX6697_REG_STAT_CRIT
, ®val
);
252 * In the MAX6581 datasheet revision 0 to 3, the local channel
253 * overtemperature status is reported in bit 6 of register 0x45,
254 * and the overtemperature status for remote channel 7 is
255 * reported in bit 7. In Revision 4 and later, the local channel
256 * overtemperature status is reported in bit 7, and the remote
257 * channel 7 overtemperature status is reported in bit 6. A real
258 * chip was found to match the functionality documented in
259 * Revision 4 and later.
261 *val
= !!(regval
& BIT(channel
? channel
- 1 : 7));
263 case hwmon_temp_max_alarm
:
264 ret
= regmap_read(regmap
, MAX6697_REG_STAT_ALARM
, ®val
);
267 *val
= !!(regval
& BIT(max6697_alarm_channel_map(channel
)));
269 case hwmon_temp_min_alarm
:
270 ret
= regmap_read(regmap
, MAX6697_REG_STAT_MIN_ALARM
, ®val
);
273 *val
= !!(regval
& BIT(max6697_alarm_channel_map(channel
)));
281 static int max6697_write(struct device
*dev
, enum hwmon_sensor_types type
,
282 u32 attr
, int channel
, long val
)
284 struct max6697_data
*data
= dev_get_drvdata(dev
);
285 struct regmap
*regmap
= data
->regmap
;
290 val
= clamp_val(val
, -1000000, 1000000); /* prevent underflow */
291 val
= DIV_ROUND_CLOSEST(val
, 1000) + data
->temp_offset
;
292 val
= clamp_val(val
, 0, data
->type
== max6581
? 255 : 127);
293 return regmap_write(regmap
, MAX6697_REG_MAX
[channel
], val
);
294 case hwmon_temp_crit
:
295 val
= clamp_val(val
, -1000000, 1000000); /* prevent underflow */
296 val
= DIV_ROUND_CLOSEST(val
, 1000) + data
->temp_offset
;
297 val
= clamp_val(val
, 0, data
->type
== max6581
? 255 : 127);
298 return regmap_write(regmap
, MAX6697_REG_CRIT
[channel
], val
);
300 val
= clamp_val(val
, -1000000, 1000000); /* prevent underflow */
301 val
= DIV_ROUND_CLOSEST(val
, 1000) + data
->temp_offset
;
302 val
= clamp_val(val
, 0, 255);
303 return regmap_write(regmap
, MAX6697_REG_MIN
, val
);
304 case hwmon_temp_offset
:
305 mutex_lock(&data
->update_lock
);
306 val
= clamp_val(val
, MAX6581_OFFSET_MIN
, MAX6581_OFFSET_MAX
);
307 val
= DIV_ROUND_CLOSEST(val
, 250);
308 if (!val
) { /* disable this (and only this) channel */
309 ret
= regmap_clear_bits(regmap
, MAX6581_REG_OFFSET_SELECT
,
312 /* enable channel and update offset */
313 ret
= regmap_set_bits(regmap
, MAX6581_REG_OFFSET_SELECT
,
317 ret
= regmap_write(regmap
, MAX6581_REG_OFFSET
, val
);
320 mutex_unlock(&data
->update_lock
);
327 static umode_t
max6697_is_visible(const void *_data
, enum hwmon_sensor_types type
,
328 u32 attr
, int channel
)
330 const struct max6697_data
*data
= _data
;
331 const struct max6697_chip_data
*chip
= data
->chip
;
333 if (channel
>= chip
->channels
)
339 case hwmon_temp_input
:
340 case hwmon_temp_max_alarm
:
343 if (data
->type
== max6581
)
344 return channel
? 0444 : 0644;
346 case hwmon_temp_min_alarm
:
347 if (data
->type
== max6581
)
350 case hwmon_temp_crit
:
351 if (chip
->have_crit
& BIT(channel
))
354 case hwmon_temp_crit_alarm
:
355 if (chip
->have_crit
& BIT(channel
))
358 case hwmon_temp_fault
:
359 if (chip
->have_fault
& BIT(channel
))
362 case hwmon_temp_offset
:
363 if (data
->type
== max6581
&& channel
)
372 /* Return 0 if detection is successful, -ENODEV otherwise */
373 static const struct hwmon_channel_info
* const max6697_info
[] = {
374 HWMON_CHANNEL_INFO(temp
,
375 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
376 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
377 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
,
378 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
379 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
380 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
381 HWMON_T_FAULT
| HWMON_T_OFFSET
,
382 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
383 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
384 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
385 HWMON_T_FAULT
| HWMON_T_OFFSET
,
386 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
387 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
388 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
389 HWMON_T_FAULT
| HWMON_T_OFFSET
,
390 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
391 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
392 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
393 HWMON_T_FAULT
| HWMON_T_OFFSET
,
394 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
395 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
396 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
397 HWMON_T_FAULT
| HWMON_T_OFFSET
,
398 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
399 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
400 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
401 HWMON_T_FAULT
| HWMON_T_OFFSET
,
402 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_CRIT
|
403 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
404 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
405 HWMON_T_FAULT
| HWMON_T_OFFSET
),
409 static const struct hwmon_ops max6697_hwmon_ops
= {
410 .is_visible
= max6697_is_visible
,
411 .read
= max6697_read
,
412 .write
= max6697_write
,
415 static const struct hwmon_chip_info max6697_chip_info
= {
416 .ops
= &max6697_hwmon_ops
,
417 .info
= max6697_info
,
420 static int max6697_config_of(struct device_node
*node
, struct max6697_data
*data
)
422 const struct max6697_chip_data
*chip
= data
->chip
;
423 struct regmap
*regmap
= data
->regmap
;
428 if (of_property_read_bool(node
, "smbus-timeout-disable") &&
429 (chip
->valid_conf
& MAX6697_CONF_TIMEOUT
)) {
430 confreg
|= MAX6697_CONF_TIMEOUT
;
432 if (of_property_read_bool(node
, "extended-range-enable") &&
433 (chip
->valid_conf
& MAX6581_CONF_EXTENDED
)) {
434 confreg
|= MAX6581_CONF_EXTENDED
;
435 data
->temp_offset
= 64;
437 if (of_property_read_bool(node
, "beta-compensation-enable") &&
438 (chip
->valid_conf
& MAX6693_CONF_BETA
)) {
439 confreg
|= MAX6693_CONF_BETA
;
442 if (of_property_read_u32(node
, "alert-mask", vals
))
444 ret
= regmap_write(regmap
, MAX6697_REG_ALERT_MASK
,
445 MAX6697_ALERT_MAP_BITS(vals
[0]));
449 if (of_property_read_u32(node
, "over-temperature-mask", vals
))
451 ret
= regmap_write(regmap
, MAX6697_REG_OVERT_MASK
,
452 MAX6697_OVERT_MAP_BITS(vals
[0]));
456 if (data
->type
!= max6581
) {
457 if (of_property_read_bool(node
, "resistance-cancellation") &&
458 chip
->valid_conf
& MAX6697_CONF_RESISTANCE
) {
459 confreg
|= MAX6697_CONF_RESISTANCE
;
462 if (of_property_read_u32(node
, "resistance-cancellation", &vals
[0])) {
463 if (of_property_read_bool(node
, "resistance-cancellation"))
470 ret
= regmap_write(regmap
, MAX6581_REG_RESISTANCE
, vals
[0] >> 1);
474 if (of_property_read_u32_array(node
, "transistor-ideality", vals
, 2)) {
479 ret
= regmap_write(regmap
, MAX6581_REG_IDEALITY
, vals
[1]);
482 ret
= regmap_write(regmap
, MAX6581_REG_IDEALITY_SELECT
,
483 (vals
[0] & 0xfe) >> 1);
487 return regmap_write(regmap
, MAX6697_REG_CONFIG
, confreg
);
490 static int max6697_init_chip(struct device_node
*np
, struct max6697_data
*data
)
496 * Don't touch configuration if there is no devicetree configuration.
497 * If that is the case, use the current chip configuration.
500 struct regmap
*regmap
= data
->regmap
;
502 ret
= regmap_read(regmap
, MAX6697_REG_CONFIG
, ®
);
505 if (data
->type
== max6581
) {
506 if (reg
& MAX6581_CONF_EXTENDED
)
507 data
->temp_offset
= 64;
508 ret
= regmap_read(regmap
, MAX6581_REG_RESISTANCE
, ®
);
511 ret
= max6697_config_of(np
, data
);
517 static bool max6697_volatile_reg(struct device
*dev
, unsigned int reg
)
520 case 0x00 ... 0x09: /* temperature high bytes */
521 case 0x44 ... 0x47: /* status */
522 case 0x51 ... 0x58: /* temperature low bytes */
529 static bool max6697_writeable_reg(struct device
*dev
, unsigned int reg
)
531 return reg
!= 0x0a && reg
!= 0x0f && !max6697_volatile_reg(dev
, reg
);
534 static const struct regmap_config max6697_regmap_config
= {
537 .max_register
= 0x58,
538 .writeable_reg
= max6697_writeable_reg
,
539 .volatile_reg
= max6697_volatile_reg
,
540 .cache_type
= REGCACHE_MAPLE
,
543 static int max6697_probe(struct i2c_client
*client
)
545 struct device
*dev
= &client
->dev
;
546 struct max6697_data
*data
;
547 struct device
*hwmon_dev
;
548 struct regmap
*regmap
;
551 regmap
= regmap_init_i2c(client
, &max6697_regmap_config
);
553 return PTR_ERR(regmap
);
555 data
= devm_kzalloc(dev
, sizeof(struct max6697_data
), GFP_KERNEL
);
559 data
->regmap
= regmap
;
560 data
->type
= (uintptr_t)i2c_get_match_data(client
);
561 data
->chip
= &max6697_chip_data
[data
->type
];
562 mutex_init(&data
->update_lock
);
564 err
= max6697_init_chip(client
->dev
.of_node
, data
);
568 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
569 &max6697_chip_info
, NULL
);
570 return PTR_ERR_OR_ZERO(hwmon_dev
);
573 static const struct i2c_device_id max6697_id
[] = {
574 { "max6581", max6581
},
575 { "max6602", max6602
},
576 { "max6622", max6622
},
577 { "max6636", max6636
},
578 { "max6689", max6689
},
579 { "max6693", max6693
},
580 { "max6694", max6694
},
581 { "max6697", max6697
},
582 { "max6698", max6698
},
583 { "max6699", max6699
},
586 MODULE_DEVICE_TABLE(i2c
, max6697_id
);
588 static const struct of_device_id __maybe_unused max6697_of_match
[] = {
590 .compatible
= "maxim,max6581",
591 .data
= (void *)max6581
594 .compatible
= "maxim,max6602",
595 .data
= (void *)max6602
598 .compatible
= "maxim,max6622",
599 .data
= (void *)max6622
602 .compatible
= "maxim,max6636",
603 .data
= (void *)max6636
606 .compatible
= "maxim,max6689",
607 .data
= (void *)max6689
610 .compatible
= "maxim,max6693",
611 .data
= (void *)max6693
614 .compatible
= "maxim,max6694",
615 .data
= (void *)max6694
618 .compatible
= "maxim,max6697",
619 .data
= (void *)max6697
622 .compatible
= "maxim,max6698",
623 .data
= (void *)max6698
626 .compatible
= "maxim,max6699",
627 .data
= (void *)max6699
631 MODULE_DEVICE_TABLE(of
, max6697_of_match
);
633 static struct i2c_driver max6697_driver
= {
636 .of_match_table
= of_match_ptr(max6697_of_match
),
638 .probe
= max6697_probe
,
639 .id_table
= max6697_id
,
642 module_i2c_driver(max6697_driver
);
644 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
645 MODULE_DESCRIPTION("MAX6697 temperature sensor driver");
646 MODULE_LICENSE("GPL");