18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * emc1403.c - SMSC Thermal Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Intel Corp
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
178c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
188c2ecf20Sopenharmony_ci#include <linux/err.h>
198c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
208c2ecf20Sopenharmony_ci#include <linux/mutex.h>
218c2ecf20Sopenharmony_ci#include <linux/regmap.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define THERMAL_PID_REG		0xfd
248c2ecf20Sopenharmony_ci#define THERMAL_SMSC_ID_REG	0xfe
258c2ecf20Sopenharmony_ci#define THERMAL_REVISION_REG	0xff
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cienum emc1403_chip { emc1402, emc1403, emc1404 };
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct thermal_data {
308c2ecf20Sopenharmony_ci	struct regmap *regmap;
318c2ecf20Sopenharmony_ci	struct mutex mutex;
328c2ecf20Sopenharmony_ci	const struct attribute_group *groups[4];
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *attr,
368c2ecf20Sopenharmony_ci			 char *buf)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
398c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
408c2ecf20Sopenharmony_ci	unsigned int val;
418c2ecf20Sopenharmony_ci	int retval;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	retval = regmap_read(data->regmap, sda->index, &val);
448c2ecf20Sopenharmony_ci	if (retval < 0)
458c2ecf20Sopenharmony_ci		return retval;
468c2ecf20Sopenharmony_ci	return sprintf(buf, "%d000\n", val);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic ssize_t bit_show(struct device *dev, struct device_attribute *attr,
508c2ecf20Sopenharmony_ci			char *buf)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
538c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
548c2ecf20Sopenharmony_ci	unsigned int val;
558c2ecf20Sopenharmony_ci	int retval;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	retval = regmap_read(data->regmap, sda->nr, &val);
588c2ecf20Sopenharmony_ci	if (retval < 0)
598c2ecf20Sopenharmony_ci		return retval;
608c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(val & sda->index));
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic ssize_t temp_store(struct device *dev, struct device_attribute *attr,
648c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
678c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
688c2ecf20Sopenharmony_ci	unsigned long val;
698c2ecf20Sopenharmony_ci	int retval;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
728c2ecf20Sopenharmony_ci		return -EINVAL;
738c2ecf20Sopenharmony_ci	retval = regmap_write(data->regmap, sda->index,
748c2ecf20Sopenharmony_ci			      DIV_ROUND_CLOSEST(val, 1000));
758c2ecf20Sopenharmony_ci	if (retval < 0)
768c2ecf20Sopenharmony_ci		return retval;
778c2ecf20Sopenharmony_ci	return count;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic ssize_t bit_store(struct device *dev, struct device_attribute *attr,
818c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
848c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
858c2ecf20Sopenharmony_ci	unsigned long val;
868c2ecf20Sopenharmony_ci	int retval;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
898c2ecf20Sopenharmony_ci		return -EINVAL;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
928c2ecf20Sopenharmony_ci				    val ? sda->index : 0);
938c2ecf20Sopenharmony_ci	if (retval < 0)
948c2ecf20Sopenharmony_ci		return retval;
958c2ecf20Sopenharmony_ci	return count;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic ssize_t show_hyst_common(struct device *dev,
998c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf,
1008c2ecf20Sopenharmony_ci				bool is_min)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
1038c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
1048c2ecf20Sopenharmony_ci	struct regmap *regmap = data->regmap;
1058c2ecf20Sopenharmony_ci	unsigned int limit;
1068c2ecf20Sopenharmony_ci	unsigned int hyst;
1078c2ecf20Sopenharmony_ci	int retval;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	retval = regmap_read(regmap, sda->index, &limit);
1108c2ecf20Sopenharmony_ci	if (retval < 0)
1118c2ecf20Sopenharmony_ci		return retval;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	retval = regmap_read(regmap, 0x21, &hyst);
1148c2ecf20Sopenharmony_ci	if (retval < 0)
1158c2ecf20Sopenharmony_ci		return retval;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
1218c2ecf20Sopenharmony_ci			 char *buf)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return show_hyst_common(dev, attr, buf, false);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic ssize_t min_hyst_show(struct device *dev,
1278c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	return show_hyst_common(dev, attr, buf, true);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
1338c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
1368c2ecf20Sopenharmony_ci	struct thermal_data *data = dev_get_drvdata(dev);
1378c2ecf20Sopenharmony_ci	struct regmap *regmap = data->regmap;
1388c2ecf20Sopenharmony_ci	unsigned int limit;
1398c2ecf20Sopenharmony_ci	int retval;
1408c2ecf20Sopenharmony_ci	int hyst;
1418c2ecf20Sopenharmony_ci	unsigned long val;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
1448c2ecf20Sopenharmony_ci		return -EINVAL;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	mutex_lock(&data->mutex);
1478c2ecf20Sopenharmony_ci	retval = regmap_read(regmap, sda->index, &limit);
1488c2ecf20Sopenharmony_ci	if (retval < 0)
1498c2ecf20Sopenharmony_ci		goto fail;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	hyst = limit * 1000 - val;
1528c2ecf20Sopenharmony_ci	hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
1538c2ecf20Sopenharmony_ci	retval = regmap_write(regmap, 0x21, hyst);
1548c2ecf20Sopenharmony_ci	if (retval == 0)
1558c2ecf20Sopenharmony_ci		retval = count;
1568c2ecf20Sopenharmony_cifail:
1578c2ecf20Sopenharmony_ci	mutex_unlock(&data->mutex);
1588c2ecf20Sopenharmony_ci	return retval;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/*
1628c2ecf20Sopenharmony_ci *	Sensors. We pass the actual i2c register to the methods.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
1668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
1678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
1688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
1698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
1708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
1718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
1728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
1738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
1748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
1778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
1788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
1798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
1808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
1818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
1828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
1838c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
1848c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
1858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
1868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
1898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
1908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
1918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
1928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
1938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
1948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
1958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
1968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
1978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
1988c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
2018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
2028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
2038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
2048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
2058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
2068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
2078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
2088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
2098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
2108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic struct attribute *emc1402_attrs[] = {
2158c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
2168c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
2178c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
2188c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
2198c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
2208c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
2218c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
2248c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
2258c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit.dev_attr.attr,
2268c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
2278c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
2288c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
2298c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	&sensor_dev_attr_power_state.dev_attr.attr,
2328c2ecf20Sopenharmony_ci	NULL
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic const struct attribute_group emc1402_group = {
2368c2ecf20Sopenharmony_ci		.attrs = emc1402_attrs,
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic struct attribute *emc1403_attrs[] = {
2408c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
2418c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
2428c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
2458c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
2468c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
2478c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
2508c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
2518c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit.dev_attr.attr,
2528c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
2538c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
2548c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
2558c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
2568c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
2578c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
2588c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
2598c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2608c2ecf20Sopenharmony_ci	NULL
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic const struct attribute_group emc1403_group = {
2648c2ecf20Sopenharmony_ci	.attrs = emc1403_attrs,
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic struct attribute *emc1404_attrs[] = {
2688c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_min.dev_attr.attr,
2698c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max.dev_attr.attr,
2708c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_crit.dev_attr.attr,
2718c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_input.dev_attr.attr,
2728c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_fault.dev_attr.attr,
2738c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
2748c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
2758c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
2768c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
2778c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
2788c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
2798c2ecf20Sopenharmony_ci	NULL
2808c2ecf20Sopenharmony_ci};
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic const struct attribute_group emc1404_group = {
2838c2ecf20Sopenharmony_ci	.attrs = emc1404_attrs,
2848c2ecf20Sopenharmony_ci};
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci/*
2878c2ecf20Sopenharmony_ci * EMC14x2 uses a different register and different bits to report alarm and
2888c2ecf20Sopenharmony_ci * fault status. For simplicity, provide a separate attribute group for this
2898c2ecf20Sopenharmony_ci * chip series.
2908c2ecf20Sopenharmony_ci * Since we can not re-use the same attribute names, create a separate attribute
2918c2ecf20Sopenharmony_ci * array.
2928c2ecf20Sopenharmony_ci */
2938c2ecf20Sopenharmony_cistatic struct sensor_device_attribute_2 emc1402_alarms[] = {
2948c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
2958c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
2968c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
2998c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
3008c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
3018c2ecf20Sopenharmony_ci	SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
3028c2ecf20Sopenharmony_ci};
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic struct attribute *emc1402_alarm_attrs[] = {
3058c2ecf20Sopenharmony_ci	&emc1402_alarms[0].dev_attr.attr,
3068c2ecf20Sopenharmony_ci	&emc1402_alarms[1].dev_attr.attr,
3078c2ecf20Sopenharmony_ci	&emc1402_alarms[2].dev_attr.attr,
3088c2ecf20Sopenharmony_ci	&emc1402_alarms[3].dev_attr.attr,
3098c2ecf20Sopenharmony_ci	&emc1402_alarms[4].dev_attr.attr,
3108c2ecf20Sopenharmony_ci	&emc1402_alarms[5].dev_attr.attr,
3118c2ecf20Sopenharmony_ci	&emc1402_alarms[6].dev_attr.attr,
3128c2ecf20Sopenharmony_ci	NULL,
3138c2ecf20Sopenharmony_ci};
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic const struct attribute_group emc1402_alarm_group = {
3168c2ecf20Sopenharmony_ci	.attrs = emc1402_alarm_attrs,
3178c2ecf20Sopenharmony_ci};
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int emc1403_detect(struct i2c_client *client,
3208c2ecf20Sopenharmony_ci			struct i2c_board_info *info)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	int id;
3238c2ecf20Sopenharmony_ci	/* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
3268c2ecf20Sopenharmony_ci	if (id != 0x5d)
3278c2ecf20Sopenharmony_ci		return -ENODEV;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
3308c2ecf20Sopenharmony_ci	switch (id) {
3318c2ecf20Sopenharmony_ci	case 0x20:
3328c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
3338c2ecf20Sopenharmony_ci		break;
3348c2ecf20Sopenharmony_ci	case 0x21:
3358c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
3368c2ecf20Sopenharmony_ci		break;
3378c2ecf20Sopenharmony_ci	case 0x22:
3388c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	case 0x23:
3418c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
3428c2ecf20Sopenharmony_ci		break;
3438c2ecf20Sopenharmony_ci	case 0x25:
3448c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
3458c2ecf20Sopenharmony_ci		break;
3468c2ecf20Sopenharmony_ci	case 0x27:
3478c2ecf20Sopenharmony_ci		strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
3488c2ecf20Sopenharmony_ci		break;
3498c2ecf20Sopenharmony_ci	default:
3508c2ecf20Sopenharmony_ci		return -ENODEV;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
3548c2ecf20Sopenharmony_ci	if (id < 0x01 || id > 0x04)
3558c2ecf20Sopenharmony_ci		return -ENODEV;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	switch (reg) {
3638c2ecf20Sopenharmony_ci	case 0x00:	/* internal diode high byte */
3648c2ecf20Sopenharmony_ci	case 0x01:	/* external diode 1 high byte */
3658c2ecf20Sopenharmony_ci	case 0x02:	/* status */
3668c2ecf20Sopenharmony_ci	case 0x10:	/* external diode 1 low byte */
3678c2ecf20Sopenharmony_ci	case 0x1b:	/* external diode fault */
3688c2ecf20Sopenharmony_ci	case 0x23:	/* external diode 2 high byte */
3698c2ecf20Sopenharmony_ci	case 0x24:	/* external diode 2 low byte */
3708c2ecf20Sopenharmony_ci	case 0x29:	/* internal diode low byte */
3718c2ecf20Sopenharmony_ci	case 0x2a:	/* externl diode 3 high byte */
3728c2ecf20Sopenharmony_ci	case 0x2b:	/* external diode 3 low byte */
3738c2ecf20Sopenharmony_ci	case 0x35:	/* high limit status */
3748c2ecf20Sopenharmony_ci	case 0x36:	/* low limit status */
3758c2ecf20Sopenharmony_ci	case 0x37:	/* therm limit status */
3768c2ecf20Sopenharmony_ci		return true;
3778c2ecf20Sopenharmony_ci	default:
3788c2ecf20Sopenharmony_ci		return false;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic const struct regmap_config emc1403_regmap_config = {
3838c2ecf20Sopenharmony_ci	.reg_bits = 8,
3848c2ecf20Sopenharmony_ci	.val_bits = 8,
3858c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
3868c2ecf20Sopenharmony_ci	.volatile_reg = emc1403_regmap_is_volatile,
3878c2ecf20Sopenharmony_ci};
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic const struct i2c_device_id emc1403_idtable[];
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int emc1403_probe(struct i2c_client *client)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct thermal_data *data;
3948c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3958c2ecf20Sopenharmony_ci	const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
3988c2ecf20Sopenharmony_ci			    GFP_KERNEL);
3998c2ecf20Sopenharmony_ci	if (data == NULL)
4008c2ecf20Sopenharmony_ci		return -ENOMEM;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
4038c2ecf20Sopenharmony_ci	if (IS_ERR(data->regmap))
4048c2ecf20Sopenharmony_ci		return PTR_ERR(data->regmap);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	mutex_init(&data->mutex);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	switch (id->driver_data) {
4098c2ecf20Sopenharmony_ci	case emc1404:
4108c2ecf20Sopenharmony_ci		data->groups[2] = &emc1404_group;
4118c2ecf20Sopenharmony_ci		fallthrough;
4128c2ecf20Sopenharmony_ci	case emc1403:
4138c2ecf20Sopenharmony_ci		data->groups[1] = &emc1403_group;
4148c2ecf20Sopenharmony_ci		fallthrough;
4158c2ecf20Sopenharmony_ci	case emc1402:
4168c2ecf20Sopenharmony_ci		data->groups[0] = &emc1402_group;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if (id->driver_data == emc1402)
4208c2ecf20Sopenharmony_ci		data->groups[1] = &emc1402_alarm_group;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
4238c2ecf20Sopenharmony_ci							   client->name, data,
4248c2ecf20Sopenharmony_ci							   data->groups);
4258c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon_dev))
4268c2ecf20Sopenharmony_ci		return PTR_ERR(hwmon_dev);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	dev_info(&client->dev, "%s Thermal chip found\n", id->name);
4298c2ecf20Sopenharmony_ci	return 0;
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic const unsigned short emc1403_address_list[] = {
4338c2ecf20Sopenharmony_ci	0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/* Last digit of chip name indicates number of channels */
4378c2ecf20Sopenharmony_cistatic const struct i2c_device_id emc1403_idtable[] = {
4388c2ecf20Sopenharmony_ci	{ "emc1402", emc1402 },
4398c2ecf20Sopenharmony_ci	{ "emc1403", emc1403 },
4408c2ecf20Sopenharmony_ci	{ "emc1404", emc1404 },
4418c2ecf20Sopenharmony_ci	{ "emc1412", emc1402 },
4428c2ecf20Sopenharmony_ci	{ "emc1413", emc1403 },
4438c2ecf20Sopenharmony_ci	{ "emc1414", emc1404 },
4448c2ecf20Sopenharmony_ci	{ "emc1422", emc1402 },
4458c2ecf20Sopenharmony_ci	{ "emc1423", emc1403 },
4468c2ecf20Sopenharmony_ci	{ "emc1424", emc1404 },
4478c2ecf20Sopenharmony_ci	{ }
4488c2ecf20Sopenharmony_ci};
4498c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, emc1403_idtable);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic struct i2c_driver sensor_emc1403 = {
4528c2ecf20Sopenharmony_ci	.class = I2C_CLASS_HWMON,
4538c2ecf20Sopenharmony_ci	.driver = {
4548c2ecf20Sopenharmony_ci		.name = "emc1403",
4558c2ecf20Sopenharmony_ci	},
4568c2ecf20Sopenharmony_ci	.detect = emc1403_detect,
4578c2ecf20Sopenharmony_ci	.probe_new = emc1403_probe,
4588c2ecf20Sopenharmony_ci	.id_table = emc1403_idtable,
4598c2ecf20Sopenharmony_ci	.address_list = emc1403_address_list,
4608c2ecf20Sopenharmony_ci};
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cimodule_i2c_driver(sensor_emc1403);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
4658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("emc1403 Thermal Driver");
4668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
467