1 // SPDX-License-Identifier: GPL-2.0-only
3 * emc1403.c - SMSC Thermal Driver
5 * Copyright (C) 2008 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/sysfs.h>
20 #include <linux/mutex.h>
21 #include <linux/regmap.h>
22 #include <linux/util_macros.h>
24 #define THERMAL_PID_REG 0xfd
25 #define THERMAL_SMSC_ID_REG 0xfe
26 #define THERMAL_REVISION_REG 0xff
28 enum emc1403_chip
{ emc1402
, emc1403
, emc1404
, emc1428
};
31 enum emc1403_chip chip
;
32 struct regmap
*regmap
;
36 static ssize_t
power_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
38 struct thermal_data
*data
= dev_get_drvdata(dev
);
42 retval
= regmap_read(data
->regmap
, 0x03, &val
);
45 return sprintf(buf
, "%d\n", !!(val
& BIT(6)));
48 static ssize_t
power_state_store(struct device
*dev
, struct device_attribute
*attr
,
49 const char *buf
, size_t count
)
51 struct thermal_data
*data
= dev_get_drvdata(dev
);
55 if (kstrtoul(buf
, 10, &val
))
58 retval
= regmap_update_bits(data
->regmap
, 0x03, BIT(6),
65 static DEVICE_ATTR_RW(power_state
);
67 static struct attribute
*emc1403_attrs
[] = {
68 &dev_attr_power_state
.attr
,
71 ATTRIBUTE_GROUPS(emc1403
);
73 static int emc1403_detect(struct i2c_client
*client
,
74 struct i2c_board_info
*info
)
77 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
79 id
= i2c_smbus_read_byte_data(client
, THERMAL_SMSC_ID_REG
);
83 id
= i2c_smbus_read_byte_data(client
, THERMAL_PID_REG
);
86 strscpy(info
->type
, "emc1402", I2C_NAME_SIZE
);
89 strscpy(info
->type
, "emc1403", I2C_NAME_SIZE
);
92 strscpy(info
->type
, "emc1422", I2C_NAME_SIZE
);
95 strscpy(info
->type
, "emc1423", I2C_NAME_SIZE
);
98 strscpy(info
->type
, "emc1404", I2C_NAME_SIZE
);
101 strscpy(info
->type
, "emc1424", I2C_NAME_SIZE
);
104 strscpy(info
->type
, "emc1428", I2C_NAME_SIZE
);
107 strscpy(info
->type
, "emc1438", I2C_NAME_SIZE
);
110 strscpy(info
->type
, "emc1442", I2C_NAME_SIZE
);
116 id
= i2c_smbus_read_byte_data(client
, THERMAL_REVISION_REG
);
117 if (id
< 0x01 || id
> 0x04)
123 static bool emc1403_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
126 case 0x00: /* internal diode high byte */
127 case 0x01: /* external diode 1 high byte */
128 case 0x02: /* status */
129 case 0x10: /* external diode 1 low byte */
130 case 0x1b: /* external diode fault */
131 case 0x23: /* external diode 2 high byte */
132 case 0x24: /* external diode 2 low byte */
133 case 0x29: /* internal diode low byte */
134 case 0x2a: /* externl diode 3 high byte */
135 case 0x2b: /* external diode 3 low byte */
136 case 0x35: /* high limit status */
137 case 0x36: /* low limit status */
138 case 0x37: /* therm limit status */
139 case 0x41: /* external diode 4 high byte */
140 case 0x42: /* external diode 4 low byte */
141 case 0x43: /* external diode 5 high byte */
142 case 0x44: /* external diode 5 low byte */
143 case 0x45: /* external diode 6 high byte */
144 case 0x46: /* external diode 6 low byte */
145 case 0x47: /* external diode 7 high byte */
146 case 0x48: /* external diode 7 low byte */
153 static const struct regmap_config emc1403_regmap_config
= {
156 .cache_type
= REGCACHE_MAPLE
,
157 .volatile_reg
= emc1403_regmap_is_volatile
,
160 enum emc1403_reg_map
{temp_min
, temp_max
, temp_crit
, temp_input
};
162 static u8 ema1403_temp_map
[] = {
163 [hwmon_temp_min
] = temp_min
,
164 [hwmon_temp_max
] = temp_max
,
165 [hwmon_temp_crit
] = temp_crit
,
166 [hwmon_temp_input
] = temp_input
,
169 static u8 emc1403_temp_regs
[][4] = {
220 static s8 emc1403_temp_regs_low
[][4] = {
271 static int __emc1403_get_temp(struct thermal_data
*data
, int channel
,
272 enum emc1403_reg_map map
, long *val
)
274 unsigned int regvalh
;
275 unsigned int regvall
= 0;
279 ret
= regmap_read(data
->regmap
, emc1403_temp_regs
[channel
][map
], ®valh
);
283 reg
= emc1403_temp_regs_low
[channel
][map
];
285 ret
= regmap_read(data
->regmap
, reg
, ®vall
);
290 if (data
->chip
== emc1428
)
291 *val
= sign_extend32((regvalh
<< 3) | (regvall
>> 5), 10) * 125;
293 *val
= ((regvalh
<< 3) | (regvall
>> 5)) * 125;
298 static int emc1403_get_temp(struct thermal_data
*data
, int channel
,
299 enum emc1403_reg_map map
, long *val
)
303 mutex_lock(&data
->mutex
);
304 ret
= __emc1403_get_temp(data
, channel
, map
, val
);
305 mutex_unlock(&data
->mutex
);
310 static int emc1403_get_hyst(struct thermal_data
*data
, int channel
,
311 enum emc1403_reg_map map
, long *val
)
316 mutex_lock(&data
->mutex
);
317 ret
= __emc1403_get_temp(data
, channel
, map
, &limit
);
320 ret
= regmap_read(data
->regmap
, 0x21, &hyst
);
324 *val
= limit
+ hyst
* 1000;
326 *val
= limit
- hyst
* 1000;
328 mutex_unlock(&data
->mutex
);
332 static int emc1403_temp_read(struct thermal_data
*data
, u32 attr
, int channel
, long *val
)
340 case hwmon_temp_crit
:
341 case hwmon_temp_input
:
342 ret
= emc1403_get_temp(data
, channel
, ema1403_temp_map
[attr
], val
);
344 case hwmon_temp_min_hyst
:
345 ret
= emc1403_get_hyst(data
, channel
, temp_min
, val
);
347 case hwmon_temp_max_hyst
:
348 ret
= emc1403_get_hyst(data
, channel
, temp_max
, val
);
350 case hwmon_temp_crit_hyst
:
351 ret
= emc1403_get_hyst(data
, channel
, temp_crit
, val
);
353 case hwmon_temp_min_alarm
:
354 if (data
->chip
== emc1402
) {
355 ret
= regmap_read(data
->regmap
, 0x02, ®val
);
358 *val
= !!(regval
& BIT(5 - 2 * channel
));
360 ret
= regmap_read(data
->regmap
, 0x36, ®val
);
363 *val
= !!(regval
& BIT(channel
));
366 case hwmon_temp_max_alarm
:
367 if (data
->chip
== emc1402
) {
368 ret
= regmap_read(data
->regmap
, 0x02, ®val
);
371 *val
= !!(regval
& BIT(6 - 2 * channel
));
373 ret
= regmap_read(data
->regmap
, 0x35, ®val
);
376 *val
= !!(regval
& BIT(channel
));
379 case hwmon_temp_crit_alarm
:
380 if (data
->chip
== emc1402
) {
381 ret
= regmap_read(data
->regmap
, 0x02, ®val
);
384 *val
= !!(regval
& BIT(channel
));
386 ret
= regmap_read(data
->regmap
, 0x37, ®val
);
389 *val
= !!(regval
& BIT(channel
));
392 case hwmon_temp_fault
:
393 ret
= regmap_read(data
->regmap
, 0x1b, ®val
);
396 *val
= !!(regval
& BIT(channel
));
404 static int emc1403_get_convrate(struct thermal_data
*data
, long *val
)
406 unsigned int convrate
;
409 ret
= regmap_read(data
->regmap
, 0x04, &convrate
);
415 *val
= 16000 >> convrate
;
419 static int emc1403_chip_read(struct thermal_data
*data
, u32 attr
, long *val
)
422 case hwmon_chip_update_interval
:
423 return emc1403_get_convrate(data
, val
);
429 static int emc1403_read(struct device
*dev
, enum hwmon_sensor_types type
,
430 u32 attr
, int channel
, long *val
)
432 struct thermal_data
*data
= dev_get_drvdata(dev
);
436 return emc1403_temp_read(data
, attr
, channel
, val
);
438 return emc1403_chip_read(data
, attr
, val
);
444 static int emc1403_set_hyst(struct thermal_data
*data
, long val
)
449 if (data
->chip
== emc1428
)
450 val
= clamp_val(val
, -128000, 127000);
452 val
= clamp_val(val
, 0, 255000);
454 mutex_lock(&data
->mutex
);
455 ret
= __emc1403_get_temp(data
, 0, temp_crit
, &limit
);
460 if (data
->chip
== emc1428
)
461 hyst
= clamp_val(DIV_ROUND_CLOSEST(hyst
, 1000), 0, 127);
463 hyst
= clamp_val(DIV_ROUND_CLOSEST(hyst
, 1000), 0, 255);
464 ret
= regmap_write(data
->regmap
, 0x21, hyst
);
466 mutex_unlock(&data
->mutex
);
470 static int emc1403_set_temp(struct thermal_data
*data
, int channel
,
471 enum emc1403_reg_map map
, long val
)
478 regh
= emc1403_temp_regs
[channel
][map
];
479 regl
= emc1403_temp_regs_low
[channel
][map
];
481 mutex_lock(&data
->mutex
);
483 if (data
->chip
== emc1428
)
484 val
= clamp_val(val
, -128000, 127875);
486 val
= clamp_val(val
, 0, 255875);
487 regval
= DIV_ROUND_CLOSEST(val
, 125);
488 ret
= regmap_write(data
->regmap
, regh
, (regval
>> 3) & 0xff);
491 ret
= regmap_write(data
->regmap
, regl
, (regval
& 0x07) << 5);
493 if (data
->chip
== emc1428
)
494 val
= clamp_val(val
, -128000, 127000);
496 val
= clamp_val(val
, 0, 255000);
497 regval
= DIV_ROUND_CLOSEST(val
, 1000);
498 ret
= regmap_write(data
->regmap
, regh
, regval
);
501 mutex_unlock(&data
->mutex
);
505 static int emc1403_temp_write(struct thermal_data
*data
, u32 attr
, int channel
, long val
)
510 case hwmon_temp_crit
:
511 return emc1403_set_temp(data
, channel
, ema1403_temp_map
[attr
], val
);
512 case hwmon_temp_crit_hyst
:
513 return emc1403_set_hyst(data
, val
);
519 /* Lookup table for temperature conversion times in msec */
520 static const u16 ina3221_conv_time
[] = {
521 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62, 31, 16
524 static int emc1403_set_convrate(struct thermal_data
*data
, unsigned int interval
)
528 convrate
= find_closest_descending(interval
, ina3221_conv_time
,
529 ARRAY_SIZE(ina3221_conv_time
));
530 return regmap_write(data
->regmap
, 0x04, convrate
);
533 static int emc1403_chip_write(struct thermal_data
*data
, u32 attr
, long val
)
536 case hwmon_chip_update_interval
:
537 return emc1403_set_convrate(data
, clamp_val(val
, 0, 100000));
543 static int emc1403_write(struct device
*dev
, enum hwmon_sensor_types type
,
544 u32 attr
, int channel
, long val
)
546 struct thermal_data
*data
= dev_get_drvdata(dev
);
550 return emc1403_temp_write(data
, attr
, channel
, val
);
552 return emc1403_chip_write(data
, attr
, val
);
558 static umode_t
emc1403_temp_is_visible(const void *_data
, u32 attr
, int channel
)
560 const struct thermal_data
*data
= _data
;
562 if (data
->chip
== emc1402
&& channel
> 1)
564 if (data
->chip
== emc1403
&& channel
> 2)
566 if (data
->chip
!= emc1428
&& channel
> 3)
570 case hwmon_temp_input
:
571 case hwmon_temp_min_alarm
:
572 case hwmon_temp_max_alarm
:
573 case hwmon_temp_crit_alarm
:
574 case hwmon_temp_fault
:
575 case hwmon_temp_min_hyst
:
576 case hwmon_temp_max_hyst
:
580 case hwmon_temp_crit
:
582 case hwmon_temp_crit_hyst
:
591 static umode_t
emc1403_chip_is_visible(const void *_data
, u32 attr
)
594 case hwmon_chip_update_interval
:
601 static umode_t
emc1403_is_visible(const void *data
, enum hwmon_sensor_types type
,
602 u32 attr
, int channel
)
606 return emc1403_temp_is_visible(data
, attr
, channel
);
608 return emc1403_chip_is_visible(data
, attr
);
614 static const struct hwmon_channel_info
* const emc1403_info
[] = {
615 HWMON_CHANNEL_INFO(chip
, HWMON_C_UPDATE_INTERVAL
),
616 HWMON_CHANNEL_INFO(temp
,
617 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
618 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
619 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
620 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
,
621 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
622 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
623 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
624 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
625 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
626 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
627 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
628 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
629 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
630 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
631 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
632 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
633 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
634 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
635 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
636 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
637 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
638 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
639 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
640 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
641 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
642 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
643 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
644 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
,
645 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
646 HWMON_T_CRIT
| HWMON_T_MIN_HYST
| HWMON_T_MAX_HYST
|
647 HWMON_T_CRIT_HYST
| HWMON_T_MIN_ALARM
|
648 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
| HWMON_T_FAULT
653 static const struct hwmon_ops emc1403_hwmon_ops
= {
654 .is_visible
= emc1403_is_visible
,
655 .read
= emc1403_read
,
656 .write
= emc1403_write
,
659 static const struct hwmon_chip_info emc1403_chip_info
= {
660 .ops
= &emc1403_hwmon_ops
,
661 .info
= emc1403_info
,
664 /* Last digit of chip name indicates number of channels */
665 static const struct i2c_device_id emc1403_idtable
[] = {
666 { "emc1402", emc1402
},
667 { "emc1403", emc1403
},
668 { "emc1404", emc1404
},
669 { "emc1412", emc1402
},
670 { "emc1413", emc1403
},
671 { "emc1414", emc1404
},
672 { "emc1422", emc1402
},
673 { "emc1423", emc1403
},
674 { "emc1424", emc1404
},
675 { "emc1428", emc1428
},
676 { "emc1438", emc1428
},
677 { "emc1442", emc1402
},
680 MODULE_DEVICE_TABLE(i2c
, emc1403_idtable
);
682 static int emc1403_probe(struct i2c_client
*client
)
684 struct thermal_data
*data
;
685 struct device
*hwmon_dev
;
686 const struct i2c_device_id
*id
= i2c_match_id(emc1403_idtable
, client
);
688 data
= devm_kzalloc(&client
->dev
, sizeof(struct thermal_data
),
693 data
->chip
= id
->driver_data
;
694 data
->regmap
= devm_regmap_init_i2c(client
, &emc1403_regmap_config
);
695 if (IS_ERR(data
->regmap
))
696 return PTR_ERR(data
->regmap
);
698 mutex_init(&data
->mutex
);
700 hwmon_dev
= devm_hwmon_device_register_with_info(&client
->dev
,
704 return PTR_ERR_OR_ZERO(hwmon_dev
);
707 static const unsigned short emc1403_address_list
[] = {
708 0x18, 0x1c, 0x29, 0x3c, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
711 static struct i2c_driver sensor_emc1403
= {
712 .class = I2C_CLASS_HWMON
,
716 .detect
= emc1403_detect
,
717 .probe
= emc1403_probe
,
718 .id_table
= emc1403_idtable
,
719 .address_list
= emc1403_address_list
,
722 module_i2c_driver(sensor_emc1403
);
724 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
725 MODULE_DESCRIPTION("emc1403 Thermal Driver");
726 MODULE_LICENSE("GPL v2");