18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * nct7802 - Driver for Nuvoton NCT7802Y
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014  Guenter Roeck <linux@roeck-us.net>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
148c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
158c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DRVNAME "nct7802"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
268c2ecf20Sopenharmony_ci	{ 0x46, 0x00, 0x40, 0x42, 0x44 },
278c2ecf20Sopenharmony_ci	{ 0x45, 0x00, 0x3f, 0x41, 0x43 },
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
338c2ecf20Sopenharmony_ci	{ 0, 0, 4, 0, 4 },
348c2ecf20Sopenharmony_ci	{ 2, 0, 6, 2, 6 },
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define REG_BANK		0x00
388c2ecf20Sopenharmony_ci#define REG_TEMP_LSB		0x05
398c2ecf20Sopenharmony_ci#define REG_TEMP_PECI_LSB	0x08
408c2ecf20Sopenharmony_ci#define REG_VOLTAGE_LOW		0x0f
418c2ecf20Sopenharmony_ci#define REG_FANCOUNT_LOW	0x13
428c2ecf20Sopenharmony_ci#define REG_START		0x21
438c2ecf20Sopenharmony_ci#define REG_MODE		0x22 /* 7.2.32 Mode Selection Register */
448c2ecf20Sopenharmony_ci#define REG_PECI_ENABLE		0x23
458c2ecf20Sopenharmony_ci#define REG_FAN_ENABLE		0x24
468c2ecf20Sopenharmony_ci#define REG_VMON_ENABLE		0x25
478c2ecf20Sopenharmony_ci#define REG_PWM(x)		(0x60 + (x))
488c2ecf20Sopenharmony_ci#define REG_SMARTFAN_EN(x)      (0x64 + (x) / 2)
498c2ecf20Sopenharmony_ci#define SMARTFAN_EN_SHIFT(x)    ((x) % 2 * 4)
508c2ecf20Sopenharmony_ci#define REG_VENDOR_ID		0xfd
518c2ecf20Sopenharmony_ci#define REG_CHIP_ID		0xfe
528c2ecf20Sopenharmony_ci#define REG_VERSION_ID		0xff
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * Data structures and manipulation thereof
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct nct7802_data {
598c2ecf20Sopenharmony_ci	struct regmap *regmap;
608c2ecf20Sopenharmony_ci	struct mutex access_lock; /* for multi-byte read and write operations */
618c2ecf20Sopenharmony_ci	u8 in_status;
628c2ecf20Sopenharmony_ci	struct mutex in_alarm_lock;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic ssize_t temp_type_show(struct device *dev,
668c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
698c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
708c2ecf20Sopenharmony_ci	unsigned int mode;
718c2ecf20Sopenharmony_ci	int ret;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, REG_MODE, &mode);
748c2ecf20Sopenharmony_ci	if (ret < 0)
758c2ecf20Sopenharmony_ci		return ret;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic ssize_t temp_type_store(struct device *dev,
818c2ecf20Sopenharmony_ci			       struct device_attribute *attr, const char *buf,
828c2ecf20Sopenharmony_ci			       size_t count)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
858c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
868c2ecf20Sopenharmony_ci	unsigned int type;
878c2ecf20Sopenharmony_ci	int err;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	err = kstrtouint(buf, 0, &type);
908c2ecf20Sopenharmony_ci	if (err < 0)
918c2ecf20Sopenharmony_ci		return err;
928c2ecf20Sopenharmony_ci	if (sattr->index == 2 && type != 4) /* RD3 */
938c2ecf20Sopenharmony_ci		return -EINVAL;
948c2ecf20Sopenharmony_ci	if (type < 3 || type > 4)
958c2ecf20Sopenharmony_ci		return -EINVAL;
968c2ecf20Sopenharmony_ci	err = regmap_update_bits(data->regmap, REG_MODE,
978c2ecf20Sopenharmony_ci			3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
988c2ecf20Sopenharmony_ci	return err ? : count;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic ssize_t pwm_mode_show(struct device *dev,
1028c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1058c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
1068c2ecf20Sopenharmony_ci	unsigned int regval;
1078c2ecf20Sopenharmony_ci	int ret;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (sattr->index > 1)
1108c2ecf20Sopenharmony_ci		return sprintf(buf, "1\n");
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, 0x5E, &regval);
1138c2ecf20Sopenharmony_ci	if (ret < 0)
1148c2ecf20Sopenharmony_ci		return ret;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
1208c2ecf20Sopenharmony_ci			char *buf)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1238c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
1248c2ecf20Sopenharmony_ci	unsigned int val;
1258c2ecf20Sopenharmony_ci	int ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	if (!attr->index)
1288c2ecf20Sopenharmony_ci		return sprintf(buf, "255\n");
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, attr->index, &val);
1318c2ecf20Sopenharmony_ci	if (ret < 0)
1328c2ecf20Sopenharmony_ci		return ret;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", val);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
1388c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1418c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
1428c2ecf20Sopenharmony_ci	int err;
1438c2ecf20Sopenharmony_ci	u8 val;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	err = kstrtou8(buf, 0, &val);
1468c2ecf20Sopenharmony_ci	if (err < 0)
1478c2ecf20Sopenharmony_ci		return err;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	err = regmap_write(data->regmap, attr->index, val);
1508c2ecf20Sopenharmony_ci	return err ? : count;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic ssize_t pwm_enable_show(struct device *dev,
1548c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
1578c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1588c2ecf20Sopenharmony_ci	unsigned int reg, enabled;
1598c2ecf20Sopenharmony_ci	int ret;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg);
1628c2ecf20Sopenharmony_ci	if (ret < 0)
1638c2ecf20Sopenharmony_ci		return ret;
1648c2ecf20Sopenharmony_ci	enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
1658c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", enabled + 1);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic ssize_t pwm_enable_store(struct device *dev,
1698c2ecf20Sopenharmony_ci				struct device_attribute *attr,
1708c2ecf20Sopenharmony_ci				const char *buf, size_t count)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
1738c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1748c2ecf20Sopenharmony_ci	u8 val;
1758c2ecf20Sopenharmony_ci	int ret;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ret = kstrtou8(buf, 0, &val);
1788c2ecf20Sopenharmony_ci	if (ret < 0)
1798c2ecf20Sopenharmony_ci		return ret;
1808c2ecf20Sopenharmony_ci	if (val < 1 || val > 2)
1818c2ecf20Sopenharmony_ci		return -EINVAL;
1828c2ecf20Sopenharmony_ci	ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
1838c2ecf20Sopenharmony_ci				 1 << SMARTFAN_EN_SHIFT(sattr->index),
1848c2ecf20Sopenharmony_ci				 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
1858c2ecf20Sopenharmony_ci	return ret ? : count;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic int nct7802_read_temp(struct nct7802_data *data,
1898c2ecf20Sopenharmony_ci			     u8 reg_temp, u8 reg_temp_low, int *temp)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	unsigned int t1, t2 = 0;
1928c2ecf20Sopenharmony_ci	int err;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	*temp = 0;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
1978c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, reg_temp, &t1);
1988c2ecf20Sopenharmony_ci	if (err < 0)
1998c2ecf20Sopenharmony_ci		goto abort;
2008c2ecf20Sopenharmony_ci	t1 <<= 8;
2018c2ecf20Sopenharmony_ci	if (reg_temp_low) {	/* 11 bit data */
2028c2ecf20Sopenharmony_ci		err = regmap_read(data->regmap, reg_temp_low, &t2);
2038c2ecf20Sopenharmony_ci		if (err < 0)
2048c2ecf20Sopenharmony_ci			goto abort;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	t1 |= t2 & 0xe0;
2078c2ecf20Sopenharmony_ci	*temp = (s16)t1 / 32 * 125;
2088c2ecf20Sopenharmony_ciabort:
2098c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
2108c2ecf20Sopenharmony_ci	return err;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	unsigned int f1, f2;
2168c2ecf20Sopenharmony_ci	int ret;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
2198c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, reg_fan, &f1);
2208c2ecf20Sopenharmony_ci	if (ret < 0)
2218c2ecf20Sopenharmony_ci		goto abort;
2228c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
2238c2ecf20Sopenharmony_ci	if (ret < 0)
2248c2ecf20Sopenharmony_ci		goto abort;
2258c2ecf20Sopenharmony_ci	ret = (f1 << 5) | (f2 >> 3);
2268c2ecf20Sopenharmony_ci	/* convert fan count to rpm */
2278c2ecf20Sopenharmony_ci	if (ret == 0x1fff)	/* maximum value, assume fan is stopped */
2288c2ecf20Sopenharmony_ci		ret = 0;
2298c2ecf20Sopenharmony_ci	else if (ret)
2308c2ecf20Sopenharmony_ci		ret = DIV_ROUND_CLOSEST(1350000U, ret);
2318c2ecf20Sopenharmony_ciabort:
2328c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
2338c2ecf20Sopenharmony_ci	return ret;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
2378c2ecf20Sopenharmony_ci				u8 reg_fan_high)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	unsigned int f1, f2;
2408c2ecf20Sopenharmony_ci	int ret;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
2438c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, reg_fan_low, &f1);
2448c2ecf20Sopenharmony_ci	if (ret < 0)
2458c2ecf20Sopenharmony_ci		goto abort;
2468c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, reg_fan_high, &f2);
2478c2ecf20Sopenharmony_ci	if (ret < 0)
2488c2ecf20Sopenharmony_ci		goto abort;
2498c2ecf20Sopenharmony_ci	ret = f1 | ((f2 & 0xf8) << 5);
2508c2ecf20Sopenharmony_ci	/* convert fan count to rpm */
2518c2ecf20Sopenharmony_ci	if (ret == 0x1fff)	/* maximum value, assume no limit */
2528c2ecf20Sopenharmony_ci		ret = 0;
2538c2ecf20Sopenharmony_ci	else if (ret)
2548c2ecf20Sopenharmony_ci		ret = DIV_ROUND_CLOSEST(1350000U, ret);
2558c2ecf20Sopenharmony_ci	else
2568c2ecf20Sopenharmony_ci		ret = 1350000U;
2578c2ecf20Sopenharmony_ciabort:
2588c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
2598c2ecf20Sopenharmony_ci	return ret;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
2638c2ecf20Sopenharmony_ci				 u8 reg_fan_high, unsigned long limit)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	int err;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (limit)
2688c2ecf20Sopenharmony_ci		limit = DIV_ROUND_CLOSEST(1350000U, limit);
2698c2ecf20Sopenharmony_ci	else
2708c2ecf20Sopenharmony_ci		limit = 0x1fff;
2718c2ecf20Sopenharmony_ci	limit = clamp_val(limit, 0, 0x1fff);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
2748c2ecf20Sopenharmony_ci	err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
2758c2ecf20Sopenharmony_ci	if (err < 0)
2768c2ecf20Sopenharmony_ci		goto abort;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
2798c2ecf20Sopenharmony_ciabort:
2808c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
2818c2ecf20Sopenharmony_ci	return err;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	unsigned int v1, v2;
2898c2ecf20Sopenharmony_ci	int ret;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
2928c2ecf20Sopenharmony_ci	if (index == 0) {	/* voltage */
2938c2ecf20Sopenharmony_ci		ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
2948c2ecf20Sopenharmony_ci		if (ret < 0)
2958c2ecf20Sopenharmony_ci			goto abort;
2968c2ecf20Sopenharmony_ci		ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
2978c2ecf20Sopenharmony_ci		if (ret < 0)
2988c2ecf20Sopenharmony_ci			goto abort;
2998c2ecf20Sopenharmony_ci		ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
3008c2ecf20Sopenharmony_ci	}  else {	/* limit */
3018c2ecf20Sopenharmony_ci		int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci		ret = regmap_read(data->regmap,
3048c2ecf20Sopenharmony_ci				  REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
3058c2ecf20Sopenharmony_ci		if (ret < 0)
3068c2ecf20Sopenharmony_ci			goto abort;
3078c2ecf20Sopenharmony_ci		ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
3088c2ecf20Sopenharmony_ci				  &v2);
3098c2ecf20Sopenharmony_ci		if (ret < 0)
3108c2ecf20Sopenharmony_ci			goto abort;
3118c2ecf20Sopenharmony_ci		ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ciabort:
3148c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
3158c2ecf20Sopenharmony_ci	return ret;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
3198c2ecf20Sopenharmony_ci				 unsigned long voltage)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
3228c2ecf20Sopenharmony_ci	int err;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
3258c2ecf20Sopenharmony_ci	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	mutex_lock(&data->access_lock);
3288c2ecf20Sopenharmony_ci	err = regmap_write(data->regmap,
3298c2ecf20Sopenharmony_ci			   REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
3308c2ecf20Sopenharmony_ci			   voltage & 0xff);
3318c2ecf20Sopenharmony_ci	if (err < 0)
3328c2ecf20Sopenharmony_ci		goto abort;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
3358c2ecf20Sopenharmony_ci				 0x0300 >> shift, (voltage & 0x0300) >> shift);
3368c2ecf20Sopenharmony_ciabort:
3378c2ecf20Sopenharmony_ci	mutex_unlock(&data->access_lock);
3388c2ecf20Sopenharmony_ci	return err;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic ssize_t in_show(struct device *dev, struct device_attribute *attr,
3428c2ecf20Sopenharmony_ci		       char *buf)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3458c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
3468c2ecf20Sopenharmony_ci	int voltage;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
3498c2ecf20Sopenharmony_ci	if (voltage < 0)
3508c2ecf20Sopenharmony_ci		return voltage;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", voltage);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic ssize_t in_store(struct device *dev, struct device_attribute *attr,
3568c2ecf20Sopenharmony_ci			const char *buf, size_t count)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3598c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
3608c2ecf20Sopenharmony_ci	int index = sattr->index;
3618c2ecf20Sopenharmony_ci	int nr = sattr->nr;
3628c2ecf20Sopenharmony_ci	unsigned long val;
3638c2ecf20Sopenharmony_ci	int err;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3668c2ecf20Sopenharmony_ci	if (err < 0)
3678c2ecf20Sopenharmony_ci		return err;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	err = nct7802_write_voltage(data, nr, index, val);
3708c2ecf20Sopenharmony_ci	return err ? : count;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr,
3748c2ecf20Sopenharmony_ci			     char *buf)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3778c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
3788c2ecf20Sopenharmony_ci	int volt, min, max, ret;
3798c2ecf20Sopenharmony_ci	unsigned int val;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	mutex_lock(&data->in_alarm_lock);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/*
3848c2ecf20Sopenharmony_ci	 * The SMI Voltage status register is the only register giving a status
3858c2ecf20Sopenharmony_ci	 * for voltages. A bit is set for each input crossing a threshold, in
3868c2ecf20Sopenharmony_ci	 * both direction, but the "inside" or "outside" limits info is not
3878c2ecf20Sopenharmony_ci	 * available. Also this register is cleared on read.
3888c2ecf20Sopenharmony_ci	 * Note: this is not explicitly spelled out in the datasheet, but
3898c2ecf20Sopenharmony_ci	 * from experiment.
3908c2ecf20Sopenharmony_ci	 * To deal with this we use a status cache with one validity bit and
3918c2ecf20Sopenharmony_ci	 * one status bit for each input. Validity is cleared at startup and
3928c2ecf20Sopenharmony_ci	 * each time the register reports a change, and the status is processed
3938c2ecf20Sopenharmony_ci	 * by software based on current input value and limits.
3948c2ecf20Sopenharmony_ci	 */
3958c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */
3968c2ecf20Sopenharmony_ci	if (ret < 0)
3978c2ecf20Sopenharmony_ci		goto abort;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* invalidate cached status for all inputs crossing a threshold */
4008c2ecf20Sopenharmony_ci	data->in_status &= ~((val & 0x0f) << 4);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* if cached status for requested input is invalid, update it */
4038c2ecf20Sopenharmony_ci	if (!(data->in_status & (0x10 << sattr->index))) {
4048c2ecf20Sopenharmony_ci		ret = nct7802_read_voltage(data, sattr->nr, 0);
4058c2ecf20Sopenharmony_ci		if (ret < 0)
4068c2ecf20Sopenharmony_ci			goto abort;
4078c2ecf20Sopenharmony_ci		volt = ret;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		ret = nct7802_read_voltage(data, sattr->nr, 1);
4108c2ecf20Sopenharmony_ci		if (ret < 0)
4118c2ecf20Sopenharmony_ci			goto abort;
4128c2ecf20Sopenharmony_ci		min = ret;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci		ret = nct7802_read_voltage(data, sattr->nr, 2);
4158c2ecf20Sopenharmony_ci		if (ret < 0)
4168c2ecf20Sopenharmony_ci			goto abort;
4178c2ecf20Sopenharmony_ci		max = ret;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		if (volt < min || volt > max)
4208c2ecf20Sopenharmony_ci			data->in_status |= (1 << sattr->index);
4218c2ecf20Sopenharmony_ci		else
4228c2ecf20Sopenharmony_ci			data->in_status &= ~(1 << sattr->index);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci		data->in_status |= 0x10 << sattr->index;
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index)));
4288c2ecf20Sopenharmony_ciabort:
4298c2ecf20Sopenharmony_ci	mutex_unlock(&data->in_alarm_lock);
4308c2ecf20Sopenharmony_ci	return ret;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *attr,
4348c2ecf20Sopenharmony_ci			 char *buf)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
4378c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4388c2ecf20Sopenharmony_ci	int err, temp;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
4418c2ecf20Sopenharmony_ci	if (err < 0)
4428c2ecf20Sopenharmony_ci		return err;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", temp);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic ssize_t temp_store(struct device *dev, struct device_attribute *attr,
4488c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4518c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
4528c2ecf20Sopenharmony_ci	int nr = sattr->nr;
4538c2ecf20Sopenharmony_ci	long val;
4548c2ecf20Sopenharmony_ci	int err;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4578c2ecf20Sopenharmony_ci	if (err < 0)
4588c2ecf20Sopenharmony_ci		return err;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	err = regmap_write(data->regmap, nr, val & 0xff);
4638c2ecf20Sopenharmony_ci	return err ? : count;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic ssize_t fan_show(struct device *dev, struct device_attribute *attr,
4678c2ecf20Sopenharmony_ci			char *buf)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
4708c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
4718c2ecf20Sopenharmony_ci	int speed;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	speed = nct7802_read_fan(data, sattr->index);
4748c2ecf20Sopenharmony_ci	if (speed < 0)
4758c2ecf20Sopenharmony_ci		return speed;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", speed);
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
4818c2ecf20Sopenharmony_ci			    char *buf)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4848c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
4858c2ecf20Sopenharmony_ci	int speed;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
4888c2ecf20Sopenharmony_ci	if (speed < 0)
4898c2ecf20Sopenharmony_ci		return speed;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", speed);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev,
4958c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
4968c2ecf20Sopenharmony_ci			     size_t count)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4998c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
5008c2ecf20Sopenharmony_ci	unsigned long val;
5018c2ecf20Sopenharmony_ci	int err;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5048c2ecf20Sopenharmony_ci	if (err < 0)
5058c2ecf20Sopenharmony_ci		return err;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
5088c2ecf20Sopenharmony_ci	return err ? : count;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
5128c2ecf20Sopenharmony_ci			  char *buf)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
5158c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5168c2ecf20Sopenharmony_ci	int bit = sattr->index;
5178c2ecf20Sopenharmony_ci	unsigned int val;
5188c2ecf20Sopenharmony_ci	int ret;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	ret = regmap_read(data->regmap, sattr->nr, &val);
5218c2ecf20Sopenharmony_ci	if (ret < 0)
5228c2ecf20Sopenharmony_ci		return ret;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", !!(val & (1 << bit)));
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic ssize_t
5288c2ecf20Sopenharmony_cibeep_show(struct device *dev, struct device_attribute *attr, char *buf)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5318c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
5328c2ecf20Sopenharmony_ci	unsigned int regval;
5338c2ecf20Sopenharmony_ci	int err;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, sattr->nr, &regval);
5368c2ecf20Sopenharmony_ci	if (err)
5378c2ecf20Sopenharmony_ci		return err;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic ssize_t
5438c2ecf20Sopenharmony_cibeep_store(struct device *dev, struct device_attribute *attr, const char *buf,
5448c2ecf20Sopenharmony_ci	   size_t count)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5478c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
5488c2ecf20Sopenharmony_ci	unsigned long val;
5498c2ecf20Sopenharmony_ci	int err;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5528c2ecf20Sopenharmony_ci	if (err < 0)
5538c2ecf20Sopenharmony_ci		return err;
5548c2ecf20Sopenharmony_ci	if (val > 1)
5558c2ecf20Sopenharmony_ci		return -EINVAL;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
5588c2ecf20Sopenharmony_ci				 val ? 1 << sattr->index : 0);
5598c2ecf20Sopenharmony_ci	return err ? : count;
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
5638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
5648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
5658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
5668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
5698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
5708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
5718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
5728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
5758c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
5768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
5778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
5788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
5818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
5828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
5838c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
5868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
5878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
5888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
5938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
5948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
5958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
5968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
5998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
6008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
6018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
6028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
6058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
6068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
6078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
6088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
6118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
6128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
6158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
6168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
6178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
6188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
6198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic struct attribute *nct7802_temp_attrs[] = {
6228c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_type.dev_attr.attr,
6238c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6248c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
6258c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
6268c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
6278c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
6288c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
6298c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
6308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_fault.dev_attr.attr,
6318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_beep.dev_attr.attr,
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_type.dev_attr.attr,		/* 10 */
6348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
6358c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
6368c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
6378c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit.dev_attr.attr,
6388c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
6398c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
6408c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
6418c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
6428c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_beep.dev_attr.attr,
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_type.dev_attr.attr,		/* 20 */
6458c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
6468c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
6478c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
6488c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit.dev_attr.attr,
6498c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
6508c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
6518c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
6528c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
6538c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_beep.dev_attr.attr,
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_input.dev_attr.attr,		/* 30 */
6568c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_min.dev_attr.attr,
6578c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max.dev_attr.attr,
6588c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_crit.dev_attr.attr,
6598c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
6608c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
6618c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
6628c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_beep.dev_attr.attr,
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_input.dev_attr.attr,		/* 38 */
6658c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_min.dev_attr.attr,
6668c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_max.dev_attr.attr,
6678c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_crit.dev_attr.attr,
6688c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
6698c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
6708c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
6718c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp5_beep.dev_attr.attr,
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp6_input.dev_attr.attr,		/* 46 */
6748c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp6_beep.dev_attr.attr,
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	NULL
6778c2ecf20Sopenharmony_ci};
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic umode_t nct7802_temp_is_visible(struct kobject *kobj,
6808c2ecf20Sopenharmony_ci				       struct attribute *attr, int index)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
6838c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
6848c2ecf20Sopenharmony_ci	unsigned int reg;
6858c2ecf20Sopenharmony_ci	int err;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, REG_MODE, &reg);
6888c2ecf20Sopenharmony_ci	if (err < 0)
6898c2ecf20Sopenharmony_ci		return 0;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	if (index < 10 &&
6928c2ecf20Sopenharmony_ci	    (reg & 03) != 0x01 && (reg & 0x03) != 0x02)		/* RD1 */
6938c2ecf20Sopenharmony_ci		return 0;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (index >= 10 && index < 20 &&
6968c2ecf20Sopenharmony_ci	    (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08)	/* RD2 */
6978c2ecf20Sopenharmony_ci		return 0;
6988c2ecf20Sopenharmony_ci	if (index >= 20 && index < 30 && (reg & 0x30) != 0x20)	/* RD3 */
6998c2ecf20Sopenharmony_ci		return 0;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	if (index >= 30 && index < 38)				/* local */
7028c2ecf20Sopenharmony_ci		return attr->mode;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
7058c2ecf20Sopenharmony_ci	if (err < 0)
7068c2ecf20Sopenharmony_ci		return 0;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (index >= 38 && index < 46 && !(reg & 0x01))		/* PECI 0 */
7098c2ecf20Sopenharmony_ci		return 0;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	if (index >= 46 && !(reg & 0x02))			/* PECI 1 */
7128c2ecf20Sopenharmony_ci		return 0;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return attr->mode;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic const struct attribute_group nct7802_temp_group = {
7188c2ecf20Sopenharmony_ci	.attrs = nct7802_temp_attrs,
7198c2ecf20Sopenharmony_ci	.is_visible = nct7802_temp_is_visible,
7208c2ecf20Sopenharmony_ci};
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
7238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
7248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
7258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3);
7268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
7318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
7328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
7338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0);
7348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
7378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
7388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
7398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1);
7408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
7438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
7448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
7458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2);
7468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_cistatic struct attribute *nct7802_in_attrs[] = {
7498c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
7508c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
7518c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
7528c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
7538c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_beep.dev_attr.attr,
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,	/* 5 */
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,	/* 6 */
7588c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
7598c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
7608c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
7618c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_beep.dev_attr.attr,
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,	/* 11 */
7648c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
7658c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
7668c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
7678c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_beep.dev_attr.attr,
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,	/* 16 */
7708c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
7718c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
7728c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
7738c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_beep.dev_attr.attr,
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	NULL,
7768c2ecf20Sopenharmony_ci};
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_cistatic umode_t nct7802_in_is_visible(struct kobject *kobj,
7798c2ecf20Sopenharmony_ci				     struct attribute *attr, int index)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
7828c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
7838c2ecf20Sopenharmony_ci	unsigned int reg;
7848c2ecf20Sopenharmony_ci	int err;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	if (index < 6)						/* VCC, VCORE */
7878c2ecf20Sopenharmony_ci		return attr->mode;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, REG_MODE, &reg);
7908c2ecf20Sopenharmony_ci	if (err < 0)
7918c2ecf20Sopenharmony_ci		return 0;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	if (index >= 6 && index < 11 && (reg & 0x03) != 0x03)	/* VSEN1 */
7948c2ecf20Sopenharmony_ci		return 0;
7958c2ecf20Sopenharmony_ci	if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c)	/* VSEN2 */
7968c2ecf20Sopenharmony_ci		return 0;
7978c2ecf20Sopenharmony_ci	if (index >= 16 && (reg & 0x30) != 0x30)		/* VSEN3 */
7988c2ecf20Sopenharmony_ci		return 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	return attr->mode;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic const struct attribute_group nct7802_in_group = {
8048c2ecf20Sopenharmony_ci	.attrs = nct7802_in_attrs,
8058c2ecf20Sopenharmony_ci	.is_visible = nct7802_in_is_visible,
8068c2ecf20Sopenharmony_ci};
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
8098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
8108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
8118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
8128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
8138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
8148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
8158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
8168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
8178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
8188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
8198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci/* 7.2.89 Fan Control Output Type */
8228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
8238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
8248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci/* 7.2.91... Fan Control Output Value */
8278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
8288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
8298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci/* 7.2.95... Temperature to Fan mapping Relationships Register */
8328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
8338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
8348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic struct attribute *nct7802_fan_attrs[] = {
8378c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
8388c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
8398c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
8408c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_beep.dev_attr.attr,
8418c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
8428c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
8438c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
8448c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_beep.dev_attr.attr,
8458c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_input.dev_attr.attr,
8468c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_min.dev_attr.attr,
8478c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
8488c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_beep.dev_attr.attr,
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	NULL
8518c2ecf20Sopenharmony_ci};
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic umode_t nct7802_fan_is_visible(struct kobject *kobj,
8548c2ecf20Sopenharmony_ci				      struct attribute *attr, int index)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
8578c2ecf20Sopenharmony_ci	struct nct7802_data *data = dev_get_drvdata(dev);
8588c2ecf20Sopenharmony_ci	int fan = index / 4;	/* 4 attributes per fan */
8598c2ecf20Sopenharmony_ci	unsigned int reg;
8608c2ecf20Sopenharmony_ci	int err;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
8638c2ecf20Sopenharmony_ci	if (err < 0 || !(reg & (1 << fan)))
8648c2ecf20Sopenharmony_ci		return 0;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	return attr->mode;
8678c2ecf20Sopenharmony_ci}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_cistatic const struct attribute_group nct7802_fan_group = {
8708c2ecf20Sopenharmony_ci	.attrs = nct7802_fan_attrs,
8718c2ecf20Sopenharmony_ci	.is_visible = nct7802_fan_is_visible,
8728c2ecf20Sopenharmony_ci};
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_cistatic struct attribute *nct7802_pwm_attrs[] = {
8758c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
8768c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
8778c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1.dev_attr.attr,
8788c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
8798c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
8808c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2.dev_attr.attr,
8818c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
8828c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_mode.dev_attr.attr,
8838c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3.dev_attr.attr,
8848c2ecf20Sopenharmony_ci	NULL
8858c2ecf20Sopenharmony_ci};
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic const struct attribute_group nct7802_pwm_group = {
8888c2ecf20Sopenharmony_ci	.attrs = nct7802_pwm_attrs,
8898c2ecf20Sopenharmony_ci};
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci/* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
8928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
8938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
8948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
8958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
8968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci/* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
8998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
9008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
9018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
9028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
9038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci/* 7.2.124 Table 2 X-axis Transition Point 1 Register */
9068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
9078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
9088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
9098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
9108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci/* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
9138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
9148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
9158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
9168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
9178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci/* 7.2.133 Table 3 X-axis Transition Point 1 Register */
9208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
9218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
9228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
9238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
9248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci/* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
9278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
9288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
9298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
9308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
9318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_cistatic struct attribute *nct7802_auto_point_attrs[] = {
9348c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
9358c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
9368c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
9378c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
9388c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
9418c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
9428c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
9438c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
9448c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
9478c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
9488c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
9498c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
9508c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
9538c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
9548c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
9558c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
9568c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
9598c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
9608c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
9618c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
9628c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
9658c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
9668c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
9678c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
9688c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	NULL
9718c2ecf20Sopenharmony_ci};
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic const struct attribute_group nct7802_auto_point_group = {
9748c2ecf20Sopenharmony_ci	.attrs = nct7802_auto_point_attrs,
9758c2ecf20Sopenharmony_ci};
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_cistatic const struct attribute_group *nct7802_groups[] = {
9788c2ecf20Sopenharmony_ci	&nct7802_temp_group,
9798c2ecf20Sopenharmony_ci	&nct7802_in_group,
9808c2ecf20Sopenharmony_ci	&nct7802_fan_group,
9818c2ecf20Sopenharmony_ci	&nct7802_pwm_group,
9828c2ecf20Sopenharmony_ci	&nct7802_auto_point_group,
9838c2ecf20Sopenharmony_ci	NULL
9848c2ecf20Sopenharmony_ci};
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_cistatic int nct7802_detect(struct i2c_client *client,
9878c2ecf20Sopenharmony_ci			  struct i2c_board_info *info)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	int reg;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	/*
9928c2ecf20Sopenharmony_ci	 * Chip identification registers are only available in bank 0,
9938c2ecf20Sopenharmony_ci	 * so only attempt chip detection if bank 0 is selected
9948c2ecf20Sopenharmony_ci	 */
9958c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_BANK);
9968c2ecf20Sopenharmony_ci	if (reg != 0x00)
9978c2ecf20Sopenharmony_ci		return -ENODEV;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
10008c2ecf20Sopenharmony_ci	if (reg != 0x50)
10018c2ecf20Sopenharmony_ci		return -ENODEV;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
10048c2ecf20Sopenharmony_ci	if (reg != 0xc3)
10058c2ecf20Sopenharmony_ci		return -ENODEV;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
10088c2ecf20Sopenharmony_ci	if (reg < 0 || (reg & 0xf0) != 0x20)
10098c2ecf20Sopenharmony_ci		return -ENODEV;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	/* Also validate lower bits of voltage and temperature registers */
10128c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
10138c2ecf20Sopenharmony_ci	if (reg < 0 || (reg & 0x1f))
10148c2ecf20Sopenharmony_ci		return -ENODEV;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
10178c2ecf20Sopenharmony_ci	if (reg < 0 || (reg & 0x3f))
10188c2ecf20Sopenharmony_ci		return -ENODEV;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
10218c2ecf20Sopenharmony_ci	if (reg < 0 || (reg & 0x3f))
10228c2ecf20Sopenharmony_ci		return -ENODEV;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
10258c2ecf20Sopenharmony_ci	return 0;
10268c2ecf20Sopenharmony_ci}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_cistatic bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
10298c2ecf20Sopenharmony_ci{
10308c2ecf20Sopenharmony_ci	return (reg != REG_BANK && reg <= 0x20) ||
10318c2ecf20Sopenharmony_ci		(reg >= REG_PWM(0) && reg <= REG_PWM(2));
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_cistatic const struct regmap_config nct7802_regmap_config = {
10358c2ecf20Sopenharmony_ci	.reg_bits = 8,
10368c2ecf20Sopenharmony_ci	.val_bits = 8,
10378c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
10388c2ecf20Sopenharmony_ci	.volatile_reg = nct7802_regmap_is_volatile,
10398c2ecf20Sopenharmony_ci};
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cistatic int nct7802_init_chip(struct nct7802_data *data)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	int err;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	/* Enable ADC */
10468c2ecf20Sopenharmony_ci	err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
10478c2ecf20Sopenharmony_ci	if (err)
10488c2ecf20Sopenharmony_ci		return err;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	/* Enable local temperature sensor */
10518c2ecf20Sopenharmony_ci	err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
10528c2ecf20Sopenharmony_ci	if (err)
10538c2ecf20Sopenharmony_ci		return err;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	/* Enable Vcore and VCC voltage monitoring */
10568c2ecf20Sopenharmony_ci	return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_cistatic int nct7802_probe(struct i2c_client *client)
10608c2ecf20Sopenharmony_ci{
10618c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
10628c2ecf20Sopenharmony_ci	struct nct7802_data *data;
10638c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
10648c2ecf20Sopenharmony_ci	int ret;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
10678c2ecf20Sopenharmony_ci	if (data == NULL)
10688c2ecf20Sopenharmony_ci		return -ENOMEM;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
10718c2ecf20Sopenharmony_ci	if (IS_ERR(data->regmap))
10728c2ecf20Sopenharmony_ci		return PTR_ERR(data->regmap);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	mutex_init(&data->access_lock);
10758c2ecf20Sopenharmony_ci	mutex_init(&data->in_alarm_lock);
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	ret = nct7802_init_chip(data);
10788c2ecf20Sopenharmony_ci	if (ret < 0)
10798c2ecf20Sopenharmony_ci		return ret;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
10828c2ecf20Sopenharmony_ci							   data,
10838c2ecf20Sopenharmony_ci							   nct7802_groups);
10848c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_cistatic const unsigned short nct7802_address_list[] = {
10888c2ecf20Sopenharmony_ci	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
10898c2ecf20Sopenharmony_ci};
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_cistatic const struct i2c_device_id nct7802_idtable[] = {
10928c2ecf20Sopenharmony_ci	{ "nct7802", 0 },
10938c2ecf20Sopenharmony_ci	{ }
10948c2ecf20Sopenharmony_ci};
10958c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, nct7802_idtable);
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_cistatic struct i2c_driver nct7802_driver = {
10988c2ecf20Sopenharmony_ci	.class = I2C_CLASS_HWMON,
10998c2ecf20Sopenharmony_ci	.driver = {
11008c2ecf20Sopenharmony_ci		.name = DRVNAME,
11018c2ecf20Sopenharmony_ci	},
11028c2ecf20Sopenharmony_ci	.detect = nct7802_detect,
11038c2ecf20Sopenharmony_ci	.probe_new = nct7802_probe,
11048c2ecf20Sopenharmony_ci	.id_table = nct7802_idtable,
11058c2ecf20Sopenharmony_ci	.address_list = nct7802_address_list,
11068c2ecf20Sopenharmony_ci};
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_cimodule_i2c_driver(nct7802_driver);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
11118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
11128c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1113