xref: /openbmc/linux/drivers/hwmon/nct7802.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
23434f378SGuenter Roeck /*
33434f378SGuenter Roeck  * nct7802 - Driver for Nuvoton NCT7802Y
43434f378SGuenter Roeck  *
53434f378SGuenter Roeck  * Copyright (C) 2014  Guenter Roeck <linux@roeck-us.net>
63434f378SGuenter Roeck  */
73434f378SGuenter Roeck 
83434f378SGuenter Roeck #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
93434f378SGuenter Roeck 
103434f378SGuenter Roeck #include <linux/err.h>
113434f378SGuenter Roeck #include <linux/i2c.h>
123434f378SGuenter Roeck #include <linux/init.h>
133434f378SGuenter Roeck #include <linux/hwmon.h>
143434f378SGuenter Roeck #include <linux/hwmon-sysfs.h>
153434f378SGuenter Roeck #include <linux/jiffies.h>
163434f378SGuenter Roeck #include <linux/module.h>
173434f378SGuenter Roeck #include <linux/mutex.h>
183434f378SGuenter Roeck #include <linux/regmap.h>
193434f378SGuenter Roeck #include <linux/slab.h>
203434f378SGuenter Roeck 
213434f378SGuenter Roeck #define DRVNAME "nct7802"
223434f378SGuenter Roeck 
233434f378SGuenter Roeck static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
243434f378SGuenter Roeck 
253434f378SGuenter Roeck static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
267713e62cSGilles Buloz 	{ 0x46, 0x00, 0x40, 0x42, 0x44 },
277713e62cSGilles Buloz 	{ 0x45, 0x00, 0x3f, 0x41, 0x43 },
283434f378SGuenter Roeck };
293434f378SGuenter Roeck 
303434f378SGuenter Roeck static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
313434f378SGuenter Roeck 
323434f378SGuenter Roeck static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
333434f378SGuenter Roeck 	{ 0, 0, 4, 0, 4 },
343434f378SGuenter Roeck 	{ 2, 0, 6, 2, 6 },
353434f378SGuenter Roeck };
363434f378SGuenter Roeck 
373434f378SGuenter Roeck #define REG_BANK		0x00
383434f378SGuenter Roeck #define REG_TEMP_LSB		0x05
393434f378SGuenter Roeck #define REG_TEMP_PECI_LSB	0x08
403434f378SGuenter Roeck #define REG_VOLTAGE_LOW		0x0f
413434f378SGuenter Roeck #define REG_FANCOUNT_LOW	0x13
423434f378SGuenter Roeck #define REG_START		0x21
43fcdc5739SConstantine Shulyupin #define REG_MODE		0x22 /* 7.2.32 Mode Selection Register */
443434f378SGuenter Roeck #define REG_PECI_ENABLE		0x23
453434f378SGuenter Roeck #define REG_FAN_ENABLE		0x24
463434f378SGuenter Roeck #define REG_VMON_ENABLE		0x25
471c6e8f6bSConstantine Shulyupin #define REG_PWM(x)		(0x60 + (x))
485102f022SConstantine Shulyupin #define REG_SMARTFAN_EN(x)      (0x64 + (x) / 2)
495102f022SConstantine Shulyupin #define SMARTFAN_EN_SHIFT(x)    ((x) % 2 * 4)
503434f378SGuenter Roeck #define REG_VENDOR_ID		0xfd
513434f378SGuenter Roeck #define REG_CHIP_ID		0xfe
523434f378SGuenter Roeck #define REG_VERSION_ID		0xff
533434f378SGuenter Roeck 
543434f378SGuenter Roeck /*
550e346a86SOskar Senft  * Resistance temperature detector (RTD) modes according to 7.2.32 Mode
560e346a86SOskar Senft  * Selection Register
570e346a86SOskar Senft  */
580e346a86SOskar Senft #define RTD_MODE_CURRENT	0x1
590e346a86SOskar Senft #define RTD_MODE_THERMISTOR	0x2
600e346a86SOskar Senft #define RTD_MODE_VOLTAGE	0x3
610e346a86SOskar Senft 
620e346a86SOskar Senft #define MODE_RTD_MASK		0x3
630e346a86SOskar Senft #define MODE_LTD_EN		0x40
640e346a86SOskar Senft 
650e346a86SOskar Senft /*
660e346a86SOskar Senft  * Bit offset for sensors modes in REG_MODE.
670e346a86SOskar Senft  * Valid for index 0..2, indicating RTD1..3.
680e346a86SOskar Senft  */
690e346a86SOskar Senft #define MODE_BIT_OFFSET_RTD(index) ((index) * 2)
700e346a86SOskar Senft 
710e346a86SOskar Senft /*
723434f378SGuenter Roeck  * Data structures and manipulation thereof
733434f378SGuenter Roeck  */
743434f378SGuenter Roeck 
753434f378SGuenter Roeck struct nct7802_data {
763434f378SGuenter Roeck 	struct regmap *regmap;
773434f378SGuenter Roeck 	struct mutex access_lock; /* for multi-byte read and write operations */
78e51a7ddaSGilles Buloz 	u8 in_status;
79e51a7ddaSGilles Buloz 	struct mutex in_alarm_lock;
803434f378SGuenter Roeck };
813434f378SGuenter Roeck 
temp_type_show(struct device * dev,struct device_attribute * attr,char * buf)824aabaf30SGuenter Roeck static ssize_t temp_type_show(struct device *dev,
834aabaf30SGuenter Roeck 			      struct device_attribute *attr, char *buf)
84fcdc5739SConstantine Shulyupin {
85fcdc5739SConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
86fcdc5739SConstantine Shulyupin 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
87fcdc5739SConstantine Shulyupin 	unsigned int mode;
88fcdc5739SConstantine Shulyupin 	int ret;
89fcdc5739SConstantine Shulyupin 
90fcdc5739SConstantine Shulyupin 	ret = regmap_read(data->regmap, REG_MODE, &mode);
91fcdc5739SConstantine Shulyupin 	if (ret < 0)
92fcdc5739SConstantine Shulyupin 		return ret;
93fcdc5739SConstantine Shulyupin 
94fcdc5739SConstantine Shulyupin 	return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
95fcdc5739SConstantine Shulyupin }
96fcdc5739SConstantine Shulyupin 
temp_type_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)974aabaf30SGuenter Roeck static ssize_t temp_type_store(struct device *dev,
984aabaf30SGuenter Roeck 			       struct device_attribute *attr, const char *buf,
994aabaf30SGuenter Roeck 			       size_t count)
100fcdc5739SConstantine Shulyupin {
101fcdc5739SConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
102fcdc5739SConstantine Shulyupin 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
103fcdc5739SConstantine Shulyupin 	unsigned int type;
104fcdc5739SConstantine Shulyupin 	int err;
105fcdc5739SConstantine Shulyupin 
106fcdc5739SConstantine Shulyupin 	err = kstrtouint(buf, 0, &type);
107fcdc5739SConstantine Shulyupin 	if (err < 0)
108fcdc5739SConstantine Shulyupin 		return err;
109fcdc5739SConstantine Shulyupin 	if (sattr->index == 2 && type != 4) /* RD3 */
110fcdc5739SConstantine Shulyupin 		return -EINVAL;
111fcdc5739SConstantine Shulyupin 	if (type < 3 || type > 4)
112fcdc5739SConstantine Shulyupin 		return -EINVAL;
113fcdc5739SConstantine Shulyupin 	err = regmap_update_bits(data->regmap, REG_MODE,
114fcdc5739SConstantine Shulyupin 			3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
115fcdc5739SConstantine Shulyupin 	return err ? : count;
116fcdc5739SConstantine Shulyupin }
117fcdc5739SConstantine Shulyupin 
pwm_mode_show(struct device * dev,struct device_attribute * attr,char * buf)1184aabaf30SGuenter Roeck static ssize_t pwm_mode_show(struct device *dev,
1194aabaf30SGuenter Roeck 			     struct device_attribute *attr, char *buf)
120876420e0SConstantine Shulyupin {
121876420e0SConstantine Shulyupin 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
122876420e0SConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
123876420e0SConstantine Shulyupin 	unsigned int regval;
124876420e0SConstantine Shulyupin 	int ret;
125876420e0SConstantine Shulyupin 
126876420e0SConstantine Shulyupin 	if (sattr->index > 1)
127876420e0SConstantine Shulyupin 		return sprintf(buf, "1\n");
128876420e0SConstantine Shulyupin 
129876420e0SConstantine Shulyupin 	ret = regmap_read(data->regmap, 0x5E, &regval);
130876420e0SConstantine Shulyupin 	if (ret < 0)
131876420e0SConstantine Shulyupin 		return ret;
132876420e0SConstantine Shulyupin 
133876420e0SConstantine Shulyupin 	return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
134876420e0SConstantine Shulyupin }
135876420e0SConstantine Shulyupin 
pwm_show(struct device * dev,struct device_attribute * devattr,char * buf)1364aabaf30SGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
137ea33597cSConstantine Shulyupin 			char *buf)
138ea33597cSConstantine Shulyupin {
139ea33597cSConstantine Shulyupin 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
140ea33597cSConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
141ea33597cSConstantine Shulyupin 	unsigned int val;
142ea33597cSConstantine Shulyupin 	int ret;
143ea33597cSConstantine Shulyupin 
1441c6e8f6bSConstantine Shulyupin 	if (!attr->index)
1451c6e8f6bSConstantine Shulyupin 		return sprintf(buf, "255\n");
1461c6e8f6bSConstantine Shulyupin 
147ea33597cSConstantine Shulyupin 	ret = regmap_read(data->regmap, attr->index, &val);
148ea33597cSConstantine Shulyupin 	if (ret < 0)
149ea33597cSConstantine Shulyupin 		return ret;
150ea33597cSConstantine Shulyupin 
151ea33597cSConstantine Shulyupin 	return sprintf(buf, "%d\n", val);
152ea33597cSConstantine Shulyupin }
153ea33597cSConstantine Shulyupin 
pwm_store(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1544aabaf30SGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
155ea33597cSConstantine Shulyupin 			 const char *buf, size_t count)
156ea33597cSConstantine Shulyupin {
157ea33597cSConstantine Shulyupin 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
158ea33597cSConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
159ea33597cSConstantine Shulyupin 	int err;
160ea33597cSConstantine Shulyupin 	u8 val;
161ea33597cSConstantine Shulyupin 
162ea33597cSConstantine Shulyupin 	err = kstrtou8(buf, 0, &val);
163ea33597cSConstantine Shulyupin 	if (err < 0)
164ea33597cSConstantine Shulyupin 		return err;
165ea33597cSConstantine Shulyupin 
166ea33597cSConstantine Shulyupin 	err = regmap_write(data->regmap, attr->index, val);
167ea33597cSConstantine Shulyupin 	return err ? : count;
168ea33597cSConstantine Shulyupin }
169fcdc5739SConstantine Shulyupin 
pwm_enable_show(struct device * dev,struct device_attribute * attr,char * buf)1704aabaf30SGuenter Roeck static ssize_t pwm_enable_show(struct device *dev,
1715102f022SConstantine Shulyupin 			       struct device_attribute *attr, char *buf)
1725102f022SConstantine Shulyupin {
1735102f022SConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
1745102f022SConstantine Shulyupin 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1755102f022SConstantine Shulyupin 	unsigned int reg, enabled;
1765102f022SConstantine Shulyupin 	int ret;
1775102f022SConstantine Shulyupin 
1785102f022SConstantine Shulyupin 	ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg);
1795102f022SConstantine Shulyupin 	if (ret < 0)
1805102f022SConstantine Shulyupin 		return ret;
1815102f022SConstantine Shulyupin 	enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
1825102f022SConstantine Shulyupin 	return sprintf(buf, "%u\n", enabled + 1);
1835102f022SConstantine Shulyupin }
1845102f022SConstantine Shulyupin 
pwm_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1854aabaf30SGuenter Roeck static ssize_t pwm_enable_store(struct device *dev,
1865102f022SConstantine Shulyupin 				struct device_attribute *attr,
1875102f022SConstantine Shulyupin 				const char *buf, size_t count)
1885102f022SConstantine Shulyupin {
1895102f022SConstantine Shulyupin 	struct nct7802_data *data = dev_get_drvdata(dev);
1905102f022SConstantine Shulyupin 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
1915102f022SConstantine Shulyupin 	u8 val;
1925102f022SConstantine Shulyupin 	int ret;
1935102f022SConstantine Shulyupin 
1945102f022SConstantine Shulyupin 	ret = kstrtou8(buf, 0, &val);
1955102f022SConstantine Shulyupin 	if (ret < 0)
1965102f022SConstantine Shulyupin 		return ret;
1975102f022SConstantine Shulyupin 	if (val < 1 || val > 2)
1985102f022SConstantine Shulyupin 		return -EINVAL;
1995102f022SConstantine Shulyupin 	ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
2005102f022SConstantine Shulyupin 				 1 << SMARTFAN_EN_SHIFT(sattr->index),
2015102f022SConstantine Shulyupin 				 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
2025102f022SConstantine Shulyupin 	return ret ? : count;
2035102f022SConstantine Shulyupin }
2045102f022SConstantine Shulyupin 
nct7802_read_temp(struct nct7802_data * data,u8 reg_temp,u8 reg_temp_low,int * temp)2053434f378SGuenter Roeck static int nct7802_read_temp(struct nct7802_data *data,
2063434f378SGuenter Roeck 			     u8 reg_temp, u8 reg_temp_low, int *temp)
2073434f378SGuenter Roeck {
2083434f378SGuenter Roeck 	unsigned int t1, t2 = 0;
2093434f378SGuenter Roeck 	int err;
2103434f378SGuenter Roeck 
2113434f378SGuenter Roeck 	*temp = 0;
2123434f378SGuenter Roeck 
2133434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
2143434f378SGuenter Roeck 	err = regmap_read(data->regmap, reg_temp, &t1);
2153434f378SGuenter Roeck 	if (err < 0)
2163434f378SGuenter Roeck 		goto abort;
2173434f378SGuenter Roeck 	t1 <<= 8;
2183434f378SGuenter Roeck 	if (reg_temp_low) {	/* 11 bit data */
2193434f378SGuenter Roeck 		err = regmap_read(data->regmap, reg_temp_low, &t2);
2203434f378SGuenter Roeck 		if (err < 0)
2213434f378SGuenter Roeck 			goto abort;
2223434f378SGuenter Roeck 	}
2233434f378SGuenter Roeck 	t1 |= t2 & 0xe0;
2243434f378SGuenter Roeck 	*temp = (s16)t1 / 32 * 125;
2253434f378SGuenter Roeck abort:
2263434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
2273434f378SGuenter Roeck 	return err;
2283434f378SGuenter Roeck }
2293434f378SGuenter Roeck 
nct7802_read_fan(struct nct7802_data * data,u8 reg_fan)2303434f378SGuenter Roeck static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
2313434f378SGuenter Roeck {
2323434f378SGuenter Roeck 	unsigned int f1, f2;
2333434f378SGuenter Roeck 	int ret;
2343434f378SGuenter Roeck 
2353434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
2363434f378SGuenter Roeck 	ret = regmap_read(data->regmap, reg_fan, &f1);
2373434f378SGuenter Roeck 	if (ret < 0)
2383434f378SGuenter Roeck 		goto abort;
2393434f378SGuenter Roeck 	ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
2403434f378SGuenter Roeck 	if (ret < 0)
2413434f378SGuenter Roeck 		goto abort;
2423434f378SGuenter Roeck 	ret = (f1 << 5) | (f2 >> 3);
2433434f378SGuenter Roeck 	/* convert fan count to rpm */
2443434f378SGuenter Roeck 	if (ret == 0x1fff)	/* maximum value, assume fan is stopped */
2453434f378SGuenter Roeck 		ret = 0;
2463434f378SGuenter Roeck 	else if (ret)
2473434f378SGuenter Roeck 		ret = DIV_ROUND_CLOSEST(1350000U, ret);
2483434f378SGuenter Roeck abort:
2493434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
2503434f378SGuenter Roeck 	return ret;
2513434f378SGuenter Roeck }
2523434f378SGuenter Roeck 
nct7802_read_fan_min(struct nct7802_data * data,u8 reg_fan_low,u8 reg_fan_high)2533434f378SGuenter Roeck static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
2543434f378SGuenter Roeck 				u8 reg_fan_high)
2553434f378SGuenter Roeck {
2563434f378SGuenter Roeck 	unsigned int f1, f2;
2573434f378SGuenter Roeck 	int ret;
2583434f378SGuenter Roeck 
2593434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
2603434f378SGuenter Roeck 	ret = regmap_read(data->regmap, reg_fan_low, &f1);
2613434f378SGuenter Roeck 	if (ret < 0)
2623434f378SGuenter Roeck 		goto abort;
2633434f378SGuenter Roeck 	ret = regmap_read(data->regmap, reg_fan_high, &f2);
2643434f378SGuenter Roeck 	if (ret < 0)
2653434f378SGuenter Roeck 		goto abort;
2663434f378SGuenter Roeck 	ret = f1 | ((f2 & 0xf8) << 5);
2673434f378SGuenter Roeck 	/* convert fan count to rpm */
2683434f378SGuenter Roeck 	if (ret == 0x1fff)	/* maximum value, assume no limit */
2693434f378SGuenter Roeck 		ret = 0;
2703434f378SGuenter Roeck 	else if (ret)
2713434f378SGuenter Roeck 		ret = DIV_ROUND_CLOSEST(1350000U, ret);
272c0d04e91SGuenter Roeck 	else
273c0d04e91SGuenter Roeck 		ret = 1350000U;
2743434f378SGuenter Roeck abort:
2753434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
2763434f378SGuenter Roeck 	return ret;
2773434f378SGuenter Roeck }
2783434f378SGuenter Roeck 
nct7802_write_fan_min(struct nct7802_data * data,u8 reg_fan_low,u8 reg_fan_high,unsigned long limit)2793434f378SGuenter Roeck static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
280c0d04e91SGuenter Roeck 				 u8 reg_fan_high, unsigned long limit)
2813434f378SGuenter Roeck {
2823434f378SGuenter Roeck 	int err;
2833434f378SGuenter Roeck 
2843434f378SGuenter Roeck 	if (limit)
2853434f378SGuenter Roeck 		limit = DIV_ROUND_CLOSEST(1350000U, limit);
2863434f378SGuenter Roeck 	else
2873434f378SGuenter Roeck 		limit = 0x1fff;
2883434f378SGuenter Roeck 	limit = clamp_val(limit, 0, 0x1fff);
2893434f378SGuenter Roeck 
2903434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
2913434f378SGuenter Roeck 	err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
2923434f378SGuenter Roeck 	if (err < 0)
2933434f378SGuenter Roeck 		goto abort;
2943434f378SGuenter Roeck 
2953434f378SGuenter Roeck 	err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
2963434f378SGuenter Roeck abort:
2973434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
2983434f378SGuenter Roeck 	return err;
2993434f378SGuenter Roeck }
3003434f378SGuenter Roeck 
3013434f378SGuenter Roeck static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
3023434f378SGuenter Roeck 
nct7802_read_voltage(struct nct7802_data * data,int nr,int index)3033434f378SGuenter Roeck static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
3043434f378SGuenter Roeck {
3053434f378SGuenter Roeck 	unsigned int v1, v2;
3063434f378SGuenter Roeck 	int ret;
3073434f378SGuenter Roeck 
3083434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
3093434f378SGuenter Roeck 	if (index == 0) {	/* voltage */
3103434f378SGuenter Roeck 		ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
3113434f378SGuenter Roeck 		if (ret < 0)
3123434f378SGuenter Roeck 			goto abort;
3133434f378SGuenter Roeck 		ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
3143434f378SGuenter Roeck 		if (ret < 0)
3153434f378SGuenter Roeck 			goto abort;
3163434f378SGuenter Roeck 		ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
3173434f378SGuenter Roeck 	}  else {	/* limit */
3183434f378SGuenter Roeck 		int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
3193434f378SGuenter Roeck 
3203434f378SGuenter Roeck 		ret = regmap_read(data->regmap,
3213434f378SGuenter Roeck 				  REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
3223434f378SGuenter Roeck 		if (ret < 0)
3233434f378SGuenter Roeck 			goto abort;
3243434f378SGuenter Roeck 		ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
3253434f378SGuenter Roeck 				  &v2);
3263434f378SGuenter Roeck 		if (ret < 0)
3273434f378SGuenter Roeck 			goto abort;
3283434f378SGuenter Roeck 		ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
3293434f378SGuenter Roeck 	}
3303434f378SGuenter Roeck abort:
3313434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
3323434f378SGuenter Roeck 	return ret;
3333434f378SGuenter Roeck }
3343434f378SGuenter Roeck 
nct7802_write_voltage(struct nct7802_data * data,int nr,int index,unsigned long voltage)3353434f378SGuenter Roeck static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
3369200bc4cSGuenter Roeck 				 unsigned long voltage)
3373434f378SGuenter Roeck {
3383434f378SGuenter Roeck 	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
3393434f378SGuenter Roeck 	int err;
3403434f378SGuenter Roeck 
341c0d04e91SGuenter Roeck 	voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
3423434f378SGuenter Roeck 	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
3433434f378SGuenter Roeck 
3443434f378SGuenter Roeck 	mutex_lock(&data->access_lock);
3453434f378SGuenter Roeck 	err = regmap_write(data->regmap,
3463434f378SGuenter Roeck 			   REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
3473434f378SGuenter Roeck 			   voltage & 0xff);
3483434f378SGuenter Roeck 	if (err < 0)
3493434f378SGuenter Roeck 		goto abort;
3503434f378SGuenter Roeck 
3513434f378SGuenter Roeck 	err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
3523434f378SGuenter Roeck 				 0x0300 >> shift, (voltage & 0x0300) >> shift);
3533434f378SGuenter Roeck abort:
3543434f378SGuenter Roeck 	mutex_unlock(&data->access_lock);
3553434f378SGuenter Roeck 	return err;
3563434f378SGuenter Roeck }
3573434f378SGuenter Roeck 
in_show(struct device * dev,struct device_attribute * attr,char * buf)3584aabaf30SGuenter Roeck static ssize_t in_show(struct device *dev, struct device_attribute *attr,
3593434f378SGuenter Roeck 		       char *buf)
3603434f378SGuenter Roeck {
3613434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3623434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
3633434f378SGuenter Roeck 	int voltage;
3643434f378SGuenter Roeck 
3653434f378SGuenter Roeck 	voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
3663434f378SGuenter Roeck 	if (voltage < 0)
3673434f378SGuenter Roeck 		return voltage;
3683434f378SGuenter Roeck 
3693434f378SGuenter Roeck 	return sprintf(buf, "%d\n", voltage);
3703434f378SGuenter Roeck }
3713434f378SGuenter Roeck 
in_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3724aabaf30SGuenter Roeck static ssize_t in_store(struct device *dev, struct device_attribute *attr,
3733434f378SGuenter Roeck 			const char *buf, size_t count)
3743434f378SGuenter Roeck {
3753434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3763434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
3773434f378SGuenter Roeck 	int index = sattr->index;
3783434f378SGuenter Roeck 	int nr = sattr->nr;
3793434f378SGuenter Roeck 	unsigned long val;
3803434f378SGuenter Roeck 	int err;
3813434f378SGuenter Roeck 
3823434f378SGuenter Roeck 	err = kstrtoul(buf, 10, &val);
3833434f378SGuenter Roeck 	if (err < 0)
3843434f378SGuenter Roeck 		return err;
3853434f378SGuenter Roeck 
3863434f378SGuenter Roeck 	err = nct7802_write_voltage(data, nr, index, val);
3873434f378SGuenter Roeck 	return err ? : count;
3883434f378SGuenter Roeck }
3893434f378SGuenter Roeck 
in_alarm_show(struct device * dev,struct device_attribute * attr,char * buf)390e51a7ddaSGilles Buloz static ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr,
391e51a7ddaSGilles Buloz 			     char *buf)
392e51a7ddaSGilles Buloz {
393e51a7ddaSGilles Buloz 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
394e51a7ddaSGilles Buloz 	struct nct7802_data *data = dev_get_drvdata(dev);
395e51a7ddaSGilles Buloz 	int volt, min, max, ret;
396e51a7ddaSGilles Buloz 	unsigned int val;
397e51a7ddaSGilles Buloz 
398e51a7ddaSGilles Buloz 	mutex_lock(&data->in_alarm_lock);
399e51a7ddaSGilles Buloz 
400e51a7ddaSGilles Buloz 	/*
401e51a7ddaSGilles Buloz 	 * The SMI Voltage status register is the only register giving a status
402e51a7ddaSGilles Buloz 	 * for voltages. A bit is set for each input crossing a threshold, in
403e51a7ddaSGilles Buloz 	 * both direction, but the "inside" or "outside" limits info is not
404e51a7ddaSGilles Buloz 	 * available. Also this register is cleared on read.
405e51a7ddaSGilles Buloz 	 * Note: this is not explicitly spelled out in the datasheet, but
406e51a7ddaSGilles Buloz 	 * from experiment.
407e51a7ddaSGilles Buloz 	 * To deal with this we use a status cache with one validity bit and
408e51a7ddaSGilles Buloz 	 * one status bit for each input. Validity is cleared at startup and
409e51a7ddaSGilles Buloz 	 * each time the register reports a change, and the status is processed
410e51a7ddaSGilles Buloz 	 * by software based on current input value and limits.
411e51a7ddaSGilles Buloz 	 */
412e51a7ddaSGilles Buloz 	ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */
413e51a7ddaSGilles Buloz 	if (ret < 0)
414e51a7ddaSGilles Buloz 		goto abort;
415e51a7ddaSGilles Buloz 
416e51a7ddaSGilles Buloz 	/* invalidate cached status for all inputs crossing a threshold */
417e51a7ddaSGilles Buloz 	data->in_status &= ~((val & 0x0f) << 4);
418e51a7ddaSGilles Buloz 
419e51a7ddaSGilles Buloz 	/* if cached status for requested input is invalid, update it */
420e51a7ddaSGilles Buloz 	if (!(data->in_status & (0x10 << sattr->index))) {
421e51a7ddaSGilles Buloz 		ret = nct7802_read_voltage(data, sattr->nr, 0);
422e51a7ddaSGilles Buloz 		if (ret < 0)
423e51a7ddaSGilles Buloz 			goto abort;
424e51a7ddaSGilles Buloz 		volt = ret;
425e51a7ddaSGilles Buloz 
426e51a7ddaSGilles Buloz 		ret = nct7802_read_voltage(data, sattr->nr, 1);
427e51a7ddaSGilles Buloz 		if (ret < 0)
428e51a7ddaSGilles Buloz 			goto abort;
429e51a7ddaSGilles Buloz 		min = ret;
430e51a7ddaSGilles Buloz 
431e51a7ddaSGilles Buloz 		ret = nct7802_read_voltage(data, sattr->nr, 2);
432e51a7ddaSGilles Buloz 		if (ret < 0)
433e51a7ddaSGilles Buloz 			goto abort;
434e51a7ddaSGilles Buloz 		max = ret;
435e51a7ddaSGilles Buloz 
436e51a7ddaSGilles Buloz 		if (volt < min || volt > max)
437e51a7ddaSGilles Buloz 			data->in_status |= (1 << sattr->index);
438e51a7ddaSGilles Buloz 		else
439e51a7ddaSGilles Buloz 			data->in_status &= ~(1 << sattr->index);
440e51a7ddaSGilles Buloz 
441e51a7ddaSGilles Buloz 		data->in_status |= 0x10 << sattr->index;
442e51a7ddaSGilles Buloz 	}
443e51a7ddaSGilles Buloz 
444e51a7ddaSGilles Buloz 	ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index)));
445e51a7ddaSGilles Buloz abort:
446e51a7ddaSGilles Buloz 	mutex_unlock(&data->in_alarm_lock);
447e51a7ddaSGilles Buloz 	return ret;
448e51a7ddaSGilles Buloz }
449e51a7ddaSGilles Buloz 
temp_show(struct device * dev,struct device_attribute * attr,char * buf)4504aabaf30SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
4513434f378SGuenter Roeck 			 char *buf)
4523434f378SGuenter Roeck {
4533434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
4543434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4553434f378SGuenter Roeck 	int err, temp;
4563434f378SGuenter Roeck 
4573434f378SGuenter Roeck 	err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
4583434f378SGuenter Roeck 	if (err < 0)
4593434f378SGuenter Roeck 		return err;
4603434f378SGuenter Roeck 
4613434f378SGuenter Roeck 	return sprintf(buf, "%d\n", temp);
4623434f378SGuenter Roeck }
4633434f378SGuenter Roeck 
temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4644aabaf30SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
4653434f378SGuenter Roeck 			  const char *buf, size_t count)
4663434f378SGuenter Roeck {
4673434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4683434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
4693434f378SGuenter Roeck 	int nr = sattr->nr;
4703434f378SGuenter Roeck 	long val;
4713434f378SGuenter Roeck 	int err;
4723434f378SGuenter Roeck 
4733434f378SGuenter Roeck 	err = kstrtol(buf, 10, &val);
4743434f378SGuenter Roeck 	if (err < 0)
4753434f378SGuenter Roeck 		return err;
4763434f378SGuenter Roeck 
477c0d04e91SGuenter Roeck 	val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
4783434f378SGuenter Roeck 
4793434f378SGuenter Roeck 	err = regmap_write(data->regmap, nr, val & 0xff);
4803434f378SGuenter Roeck 	return err ? : count;
4813434f378SGuenter Roeck }
4823434f378SGuenter Roeck 
fan_show(struct device * dev,struct device_attribute * attr,char * buf)4834aabaf30SGuenter Roeck static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
4843434f378SGuenter Roeck 			char *buf)
4853434f378SGuenter Roeck {
4863434f378SGuenter Roeck 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
4873434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
4883434f378SGuenter Roeck 	int speed;
4893434f378SGuenter Roeck 
4903434f378SGuenter Roeck 	speed = nct7802_read_fan(data, sattr->index);
4913434f378SGuenter Roeck 	if (speed < 0)
4923434f378SGuenter Roeck 		return speed;
4933434f378SGuenter Roeck 
4943434f378SGuenter Roeck 	return sprintf(buf, "%d\n", speed);
4953434f378SGuenter Roeck }
4963434f378SGuenter Roeck 
fan_min_show(struct device * dev,struct device_attribute * attr,char * buf)4974aabaf30SGuenter Roeck static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
4983434f378SGuenter Roeck 			    char *buf)
4993434f378SGuenter Roeck {
5003434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5013434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
5023434f378SGuenter Roeck 	int speed;
5033434f378SGuenter Roeck 
5043434f378SGuenter Roeck 	speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
5053434f378SGuenter Roeck 	if (speed < 0)
5063434f378SGuenter Roeck 		return speed;
5073434f378SGuenter Roeck 
5083434f378SGuenter Roeck 	return sprintf(buf, "%d\n", speed);
5093434f378SGuenter Roeck }
5103434f378SGuenter Roeck 
fan_min_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5114aabaf30SGuenter Roeck static ssize_t fan_min_store(struct device *dev,
5124aabaf30SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
5134aabaf30SGuenter Roeck 			     size_t count)
5143434f378SGuenter Roeck {
5153434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5163434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
5173434f378SGuenter Roeck 	unsigned long val;
5183434f378SGuenter Roeck 	int err;
5193434f378SGuenter Roeck 
5203434f378SGuenter Roeck 	err = kstrtoul(buf, 10, &val);
5213434f378SGuenter Roeck 	if (err < 0)
5223434f378SGuenter Roeck 		return err;
5233434f378SGuenter Roeck 
5243434f378SGuenter Roeck 	err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
5253434f378SGuenter Roeck 	return err ? : count;
5263434f378SGuenter Roeck }
5273434f378SGuenter Roeck 
alarm_show(struct device * dev,struct device_attribute * attr,char * buf)5284aabaf30SGuenter Roeck static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
5293434f378SGuenter Roeck 			  char *buf)
5303434f378SGuenter Roeck {
5313434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
5323434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5333434f378SGuenter Roeck 	int bit = sattr->index;
5343434f378SGuenter Roeck 	unsigned int val;
5353434f378SGuenter Roeck 	int ret;
5363434f378SGuenter Roeck 
5373434f378SGuenter Roeck 	ret = regmap_read(data->regmap, sattr->nr, &val);
5383434f378SGuenter Roeck 	if (ret < 0)
5393434f378SGuenter Roeck 		return ret;
5403434f378SGuenter Roeck 
5413434f378SGuenter Roeck 	return sprintf(buf, "%u\n", !!(val & (1 << bit)));
5423434f378SGuenter Roeck }
5433434f378SGuenter Roeck 
5443434f378SGuenter Roeck static ssize_t
beep_show(struct device * dev,struct device_attribute * attr,char * buf)5454aabaf30SGuenter Roeck beep_show(struct device *dev, struct device_attribute *attr, char *buf)
5463434f378SGuenter Roeck {
5473434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5483434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
5493434f378SGuenter Roeck 	unsigned int regval;
5503434f378SGuenter Roeck 	int err;
5513434f378SGuenter Roeck 
5523434f378SGuenter Roeck 	err = regmap_read(data->regmap, sattr->nr, &regval);
5533434f378SGuenter Roeck 	if (err)
5543434f378SGuenter Roeck 		return err;
5553434f378SGuenter Roeck 
5563434f378SGuenter Roeck 	return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
5573434f378SGuenter Roeck }
5583434f378SGuenter Roeck 
5593434f378SGuenter Roeck static ssize_t
beep_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5604aabaf30SGuenter Roeck beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
5613434f378SGuenter Roeck 	   size_t count)
5623434f378SGuenter Roeck {
5633434f378SGuenter Roeck 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5643434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
5653434f378SGuenter Roeck 	unsigned long val;
5663434f378SGuenter Roeck 	int err;
5673434f378SGuenter Roeck 
5683434f378SGuenter Roeck 	err = kstrtoul(buf, 10, &val);
5693434f378SGuenter Roeck 	if (err < 0)
5703434f378SGuenter Roeck 		return err;
5713434f378SGuenter Roeck 	if (val > 1)
5723434f378SGuenter Roeck 		return -EINVAL;
5733434f378SGuenter Roeck 
5743434f378SGuenter Roeck 	err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
5753434f378SGuenter Roeck 				 val ? 1 << sattr->index : 0);
5763434f378SGuenter Roeck 	return err ? : count;
5773434f378SGuenter Roeck }
5783434f378SGuenter Roeck 
5794aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
5804aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
5814aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
5824aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
5834aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
5843434f378SGuenter Roeck 
5854aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
5864aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
5874aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
5884aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
5894aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
5903434f378SGuenter Roeck 
5914aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
5924aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
5934aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
5944aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
5954aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
5963434f378SGuenter Roeck 
5974aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
5984aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
5994aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
6004aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
6013434f378SGuenter Roeck 
6024aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
6034aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
6044aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
6054aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
6063434f378SGuenter Roeck 
6074aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
6083434f378SGuenter Roeck 
6094aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
6104aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
6114aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
6124aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
6134aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
6143434f378SGuenter Roeck 
6154aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
6164aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
6174aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
6184aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
6194aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
6203434f378SGuenter Roeck 
6214aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
6224aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
6234aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
6244aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
6254aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
6263434f378SGuenter Roeck 
6274aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
6284aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
6294aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
6303434f378SGuenter Roeck 
6314aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
6324aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
6334aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
6344aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
6354aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
6364aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
6373434f378SGuenter Roeck 
6383434f378SGuenter Roeck static struct attribute *nct7802_temp_attrs[] = {
639fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp1_type.dev_attr.attr,
6403434f378SGuenter Roeck 	&sensor_dev_attr_temp1_input.dev_attr.attr,
6413434f378SGuenter Roeck 	&sensor_dev_attr_temp1_min.dev_attr.attr,
6423434f378SGuenter Roeck 	&sensor_dev_attr_temp1_max.dev_attr.attr,
6433434f378SGuenter Roeck 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
6443434f378SGuenter Roeck 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
6453434f378SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
6463434f378SGuenter Roeck 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
6473434f378SGuenter Roeck 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
6483434f378SGuenter Roeck 	&sensor_dev_attr_temp1_beep.dev_attr.attr,
6493434f378SGuenter Roeck 
650fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp2_type.dev_attr.attr,		/* 10 */
651fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp2_input.dev_attr.attr,
6523434f378SGuenter Roeck 	&sensor_dev_attr_temp2_min.dev_attr.attr,
6533434f378SGuenter Roeck 	&sensor_dev_attr_temp2_max.dev_attr.attr,
6543434f378SGuenter Roeck 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
6553434f378SGuenter Roeck 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
6563434f378SGuenter Roeck 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
6573434f378SGuenter Roeck 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
6583434f378SGuenter Roeck 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
6593434f378SGuenter Roeck 	&sensor_dev_attr_temp2_beep.dev_attr.attr,
6603434f378SGuenter Roeck 
661fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp3_type.dev_attr.attr,		/* 20 */
662fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp3_input.dev_attr.attr,
6633434f378SGuenter Roeck 	&sensor_dev_attr_temp3_min.dev_attr.attr,
6643434f378SGuenter Roeck 	&sensor_dev_attr_temp3_max.dev_attr.attr,
6653434f378SGuenter Roeck 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
6663434f378SGuenter Roeck 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
6673434f378SGuenter Roeck 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
6683434f378SGuenter Roeck 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
6693434f378SGuenter Roeck 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
6703434f378SGuenter Roeck 	&sensor_dev_attr_temp3_beep.dev_attr.attr,
6713434f378SGuenter Roeck 
672fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp4_input.dev_attr.attr,		/* 30 */
6733434f378SGuenter Roeck 	&sensor_dev_attr_temp4_min.dev_attr.attr,
6743434f378SGuenter Roeck 	&sensor_dev_attr_temp4_max.dev_attr.attr,
6753434f378SGuenter Roeck 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
6763434f378SGuenter Roeck 	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
6773434f378SGuenter Roeck 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
6783434f378SGuenter Roeck 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
6793434f378SGuenter Roeck 	&sensor_dev_attr_temp4_beep.dev_attr.attr,
6803434f378SGuenter Roeck 
681fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp5_input.dev_attr.attr,		/* 38 */
6823434f378SGuenter Roeck 	&sensor_dev_attr_temp5_min.dev_attr.attr,
6833434f378SGuenter Roeck 	&sensor_dev_attr_temp5_max.dev_attr.attr,
6843434f378SGuenter Roeck 	&sensor_dev_attr_temp5_crit.dev_attr.attr,
6853434f378SGuenter Roeck 	&sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
6863434f378SGuenter Roeck 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
6873434f378SGuenter Roeck 	&sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
6883434f378SGuenter Roeck 	&sensor_dev_attr_temp5_beep.dev_attr.attr,
6893434f378SGuenter Roeck 
690fcdc5739SConstantine Shulyupin 	&sensor_dev_attr_temp6_input.dev_attr.attr,		/* 46 */
6913434f378SGuenter Roeck 	&sensor_dev_attr_temp6_beep.dev_attr.attr,
6923434f378SGuenter Roeck 
6933434f378SGuenter Roeck 	NULL
6943434f378SGuenter Roeck };
6953434f378SGuenter Roeck 
nct7802_temp_is_visible(struct kobject * kobj,struct attribute * attr,int index)6963434f378SGuenter Roeck static umode_t nct7802_temp_is_visible(struct kobject *kobj,
6973434f378SGuenter Roeck 				       struct attribute *attr, int index)
6983434f378SGuenter Roeck {
699d06f9e6cShailizheng 	struct device *dev = kobj_to_dev(kobj);
7003434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
7013434f378SGuenter Roeck 	unsigned int reg;
7023434f378SGuenter Roeck 	int err;
7033434f378SGuenter Roeck 
7043434f378SGuenter Roeck 	err = regmap_read(data->regmap, REG_MODE, &reg);
7053434f378SGuenter Roeck 	if (err < 0)
7063434f378SGuenter Roeck 		return 0;
7073434f378SGuenter Roeck 
708fcdc5739SConstantine Shulyupin 	if (index < 10 &&
7093434f378SGuenter Roeck 	    (reg & 03) != 0x01 && (reg & 0x03) != 0x02)		/* RD1 */
7103434f378SGuenter Roeck 		return 0;
711fcdc5739SConstantine Shulyupin 
712fcdc5739SConstantine Shulyupin 	if (index >= 10 && index < 20 &&
7133434f378SGuenter Roeck 	    (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08)	/* RD2 */
7143434f378SGuenter Roeck 		return 0;
715fcdc5739SConstantine Shulyupin 	if (index >= 20 && index < 30 && (reg & 0x30) != 0x20)	/* RD3 */
7163434f378SGuenter Roeck 		return 0;
717fcdc5739SConstantine Shulyupin 
718fcdc5739SConstantine Shulyupin 	if (index >= 30 && index < 38)				/* local */
7193434f378SGuenter Roeck 		return attr->mode;
7203434f378SGuenter Roeck 
7213434f378SGuenter Roeck 	err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
7223434f378SGuenter Roeck 	if (err < 0)
7233434f378SGuenter Roeck 		return 0;
7243434f378SGuenter Roeck 
725fcdc5739SConstantine Shulyupin 	if (index >= 38 && index < 46 && !(reg & 0x01))		/* PECI 0 */
7263434f378SGuenter Roeck 		return 0;
7273434f378SGuenter Roeck 
728*54685abeSGilles Buloz 	if (index >= 46 && !(reg & 0x02))			/* PECI 1 */
7293434f378SGuenter Roeck 		return 0;
7303434f378SGuenter Roeck 
7313434f378SGuenter Roeck 	return attr->mode;
7323434f378SGuenter Roeck }
7333434f378SGuenter Roeck 
734afc680f3SArvind Yadav static const struct attribute_group nct7802_temp_group = {
7353434f378SGuenter Roeck 	.attrs = nct7802_temp_attrs,
7363434f378SGuenter Roeck 	.is_visible = nct7802_temp_is_visible,
7373434f378SGuenter Roeck };
7383434f378SGuenter Roeck 
7394aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
7404aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
7414aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
742e51a7ddaSGilles Buloz static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3);
7434aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
7443434f378SGuenter Roeck 
7454aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
7463434f378SGuenter Roeck 
7474aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
7484aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
7494aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
750e51a7ddaSGilles Buloz static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0);
7514aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
7523434f378SGuenter Roeck 
7534aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
7544aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
7554aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
756e51a7ddaSGilles Buloz static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1);
7574aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
7583434f378SGuenter Roeck 
7594aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
7604aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
7614aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
762e51a7ddaSGilles Buloz static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2);
7634aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
7643434f378SGuenter Roeck 
7653434f378SGuenter Roeck static struct attribute *nct7802_in_attrs[] = {
7663434f378SGuenter Roeck 	&sensor_dev_attr_in0_input.dev_attr.attr,
7673434f378SGuenter Roeck 	&sensor_dev_attr_in0_min.dev_attr.attr,
7683434f378SGuenter Roeck 	&sensor_dev_attr_in0_max.dev_attr.attr,
7693434f378SGuenter Roeck 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
7703434f378SGuenter Roeck 	&sensor_dev_attr_in0_beep.dev_attr.attr,
7713434f378SGuenter Roeck 
7723434f378SGuenter Roeck 	&sensor_dev_attr_in1_input.dev_attr.attr,	/* 5 */
7733434f378SGuenter Roeck 
7743434f378SGuenter Roeck 	&sensor_dev_attr_in2_input.dev_attr.attr,	/* 6 */
7753434f378SGuenter Roeck 	&sensor_dev_attr_in2_min.dev_attr.attr,
7763434f378SGuenter Roeck 	&sensor_dev_attr_in2_max.dev_attr.attr,
7773434f378SGuenter Roeck 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
7783434f378SGuenter Roeck 	&sensor_dev_attr_in2_beep.dev_attr.attr,
7793434f378SGuenter Roeck 
7803434f378SGuenter Roeck 	&sensor_dev_attr_in3_input.dev_attr.attr,	/* 11 */
7813434f378SGuenter Roeck 	&sensor_dev_attr_in3_min.dev_attr.attr,
7823434f378SGuenter Roeck 	&sensor_dev_attr_in3_max.dev_attr.attr,
7833434f378SGuenter Roeck 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
7843434f378SGuenter Roeck 	&sensor_dev_attr_in3_beep.dev_attr.attr,
7853434f378SGuenter Roeck 
78638ada2f4SGuenter Roeck 	&sensor_dev_attr_in4_input.dev_attr.attr,	/* 16 */
7873434f378SGuenter Roeck 	&sensor_dev_attr_in4_min.dev_attr.attr,
7883434f378SGuenter Roeck 	&sensor_dev_attr_in4_max.dev_attr.attr,
7893434f378SGuenter Roeck 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
7903434f378SGuenter Roeck 	&sensor_dev_attr_in4_beep.dev_attr.attr,
7913434f378SGuenter Roeck 
7923434f378SGuenter Roeck 	NULL,
7933434f378SGuenter Roeck };
7943434f378SGuenter Roeck 
nct7802_in_is_visible(struct kobject * kobj,struct attribute * attr,int index)7953434f378SGuenter Roeck static umode_t nct7802_in_is_visible(struct kobject *kobj,
7963434f378SGuenter Roeck 				     struct attribute *attr, int index)
7973434f378SGuenter Roeck {
798d06f9e6cShailizheng 	struct device *dev = kobj_to_dev(kobj);
7993434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
8003434f378SGuenter Roeck 	unsigned int reg;
8013434f378SGuenter Roeck 	int err;
8023434f378SGuenter Roeck 
8033434f378SGuenter Roeck 	if (index < 6)						/* VCC, VCORE */
8043434f378SGuenter Roeck 		return attr->mode;
8053434f378SGuenter Roeck 
8063434f378SGuenter Roeck 	err = regmap_read(data->regmap, REG_MODE, &reg);
8073434f378SGuenter Roeck 	if (err < 0)
8083434f378SGuenter Roeck 		return 0;
8093434f378SGuenter Roeck 
8103434f378SGuenter Roeck 	if (index >= 6 && index < 11 && (reg & 0x03) != 0x03)	/* VSEN1 */
8113434f378SGuenter Roeck 		return 0;
81238ada2f4SGuenter Roeck 	if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c)	/* VSEN2 */
8133434f378SGuenter Roeck 		return 0;
81438ada2f4SGuenter Roeck 	if (index >= 16 && (reg & 0x30) != 0x30)		/* VSEN3 */
8153434f378SGuenter Roeck 		return 0;
8163434f378SGuenter Roeck 
8173434f378SGuenter Roeck 	return attr->mode;
8183434f378SGuenter Roeck }
8193434f378SGuenter Roeck 
820afc680f3SArvind Yadav static const struct attribute_group nct7802_in_group = {
8213434f378SGuenter Roeck 	.attrs = nct7802_in_attrs,
8223434f378SGuenter Roeck 	.is_visible = nct7802_in_is_visible,
8233434f378SGuenter Roeck };
8243434f378SGuenter Roeck 
8254aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
8264aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
8274aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
8284aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
8294aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
8304aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
8314aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
8324aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
8334aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
8344aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
8354aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
8364aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
8373434f378SGuenter Roeck 
838876420e0SConstantine Shulyupin /* 7.2.89 Fan Control Output Type */
8394aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
8404aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
8414aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
842876420e0SConstantine Shulyupin 
843ea33597cSConstantine Shulyupin /* 7.2.91... Fan Control Output Value */
8444aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
8454aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
8464aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
847ea33597cSConstantine Shulyupin 
8485102f022SConstantine Shulyupin /* 7.2.95... Temperature to Fan mapping Relationships Register */
8494aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
8504aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
8514aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
8525102f022SConstantine Shulyupin 
8533434f378SGuenter Roeck static struct attribute *nct7802_fan_attrs[] = {
8543434f378SGuenter Roeck 	&sensor_dev_attr_fan1_input.dev_attr.attr,
8553434f378SGuenter Roeck 	&sensor_dev_attr_fan1_min.dev_attr.attr,
8563434f378SGuenter Roeck 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
8573434f378SGuenter Roeck 	&sensor_dev_attr_fan1_beep.dev_attr.attr,
8583434f378SGuenter Roeck 	&sensor_dev_attr_fan2_input.dev_attr.attr,
8593434f378SGuenter Roeck 	&sensor_dev_attr_fan2_min.dev_attr.attr,
8603434f378SGuenter Roeck 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
8613434f378SGuenter Roeck 	&sensor_dev_attr_fan2_beep.dev_attr.attr,
8623434f378SGuenter Roeck 	&sensor_dev_attr_fan3_input.dev_attr.attr,
8633434f378SGuenter Roeck 	&sensor_dev_attr_fan3_min.dev_attr.attr,
8643434f378SGuenter Roeck 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
8653434f378SGuenter Roeck 	&sensor_dev_attr_fan3_beep.dev_attr.attr,
8663434f378SGuenter Roeck 
8673434f378SGuenter Roeck 	NULL
8683434f378SGuenter Roeck };
8693434f378SGuenter Roeck 
nct7802_fan_is_visible(struct kobject * kobj,struct attribute * attr,int index)8703434f378SGuenter Roeck static umode_t nct7802_fan_is_visible(struct kobject *kobj,
8713434f378SGuenter Roeck 				      struct attribute *attr, int index)
8723434f378SGuenter Roeck {
873d06f9e6cShailizheng 	struct device *dev = kobj_to_dev(kobj);
8743434f378SGuenter Roeck 	struct nct7802_data *data = dev_get_drvdata(dev);
8753434f378SGuenter Roeck 	int fan = index / 4;	/* 4 attributes per fan */
8763434f378SGuenter Roeck 	unsigned int reg;
8773434f378SGuenter Roeck 	int err;
8783434f378SGuenter Roeck 
8793434f378SGuenter Roeck 	err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
8803434f378SGuenter Roeck 	if (err < 0 || !(reg & (1 << fan)))
8813434f378SGuenter Roeck 		return 0;
8823434f378SGuenter Roeck 
8833434f378SGuenter Roeck 	return attr->mode;
8843434f378SGuenter Roeck }
8853434f378SGuenter Roeck 
886afc680f3SArvind Yadav static const struct attribute_group nct7802_fan_group = {
8873434f378SGuenter Roeck 	.attrs = nct7802_fan_attrs,
8883434f378SGuenter Roeck 	.is_visible = nct7802_fan_is_visible,
8893434f378SGuenter Roeck };
8903434f378SGuenter Roeck 
891ea33597cSConstantine Shulyupin static struct attribute *nct7802_pwm_attrs[] = {
8925102f022SConstantine Shulyupin 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
893876420e0SConstantine Shulyupin 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
894ea33597cSConstantine Shulyupin 	&sensor_dev_attr_pwm1.dev_attr.attr,
8955102f022SConstantine Shulyupin 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
896876420e0SConstantine Shulyupin 	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
897ea33597cSConstantine Shulyupin 	&sensor_dev_attr_pwm2.dev_attr.attr,
8985102f022SConstantine Shulyupin 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
899876420e0SConstantine Shulyupin 	&sensor_dev_attr_pwm3_mode.dev_attr.attr,
900ea33597cSConstantine Shulyupin 	&sensor_dev_attr_pwm3.dev_attr.attr,
901ea33597cSConstantine Shulyupin 	NULL
902ea33597cSConstantine Shulyupin };
903ea33597cSConstantine Shulyupin 
904afc680f3SArvind Yadav static const struct attribute_group nct7802_pwm_group = {
905ea33597cSConstantine Shulyupin 	.attrs = nct7802_pwm_attrs,
906ea33597cSConstantine Shulyupin };
907ea33597cSConstantine Shulyupin 
9081c6e8f6bSConstantine Shulyupin /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
9094aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
9104aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
9114aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
9124aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
9134aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
9141c6e8f6bSConstantine Shulyupin 
9151c6e8f6bSConstantine Shulyupin /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
9164aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
9174aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
9184aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
9194aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
9204aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
9211c6e8f6bSConstantine Shulyupin 
9221c6e8f6bSConstantine Shulyupin /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
9234aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
9244aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
9254aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
9264aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
9274aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
9281c6e8f6bSConstantine Shulyupin 
9291c6e8f6bSConstantine Shulyupin /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
9304aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
9314aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
9324aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
9334aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
9344aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
9351c6e8f6bSConstantine Shulyupin 
9361c6e8f6bSConstantine Shulyupin /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
9374aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
9384aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
9394aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
9404aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
9414aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
9421c6e8f6bSConstantine Shulyupin 
9431c6e8f6bSConstantine Shulyupin /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
9444aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
9454aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
9464aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
9474aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
9484aabaf30SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
9491c6e8f6bSConstantine Shulyupin 
9501c6e8f6bSConstantine Shulyupin static struct attribute *nct7802_auto_point_attrs[] = {
9511c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
9521c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
9531c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
9541c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
9551c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
9561c6e8f6bSConstantine Shulyupin 
9571c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
9581c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
9591c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
9601c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
9611c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
9621c6e8f6bSConstantine Shulyupin 
9631c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
9641c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
9651c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
9661c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
9671c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
9681c6e8f6bSConstantine Shulyupin 
9691c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
9701c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
9711c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
9721c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
9731c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
9741c6e8f6bSConstantine Shulyupin 
9751c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
9761c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
9771c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
9781c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
9791c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
9801c6e8f6bSConstantine Shulyupin 
9811c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
9821c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
9831c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
9841c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
9851c6e8f6bSConstantine Shulyupin 	&sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
9861c6e8f6bSConstantine Shulyupin 
9871c6e8f6bSConstantine Shulyupin 	NULL
9881c6e8f6bSConstantine Shulyupin };
9891c6e8f6bSConstantine Shulyupin 
990afc680f3SArvind Yadav static const struct attribute_group nct7802_auto_point_group = {
9911c6e8f6bSConstantine Shulyupin 	.attrs = nct7802_auto_point_attrs,
9921c6e8f6bSConstantine Shulyupin };
9931c6e8f6bSConstantine Shulyupin 
9943434f378SGuenter Roeck static const struct attribute_group *nct7802_groups[] = {
9953434f378SGuenter Roeck 	&nct7802_temp_group,
9963434f378SGuenter Roeck 	&nct7802_in_group,
9973434f378SGuenter Roeck 	&nct7802_fan_group,
998ea33597cSConstantine Shulyupin 	&nct7802_pwm_group,
9991c6e8f6bSConstantine Shulyupin 	&nct7802_auto_point_group,
10003434f378SGuenter Roeck 	NULL
10013434f378SGuenter Roeck };
10023434f378SGuenter Roeck 
nct7802_detect(struct i2c_client * client,struct i2c_board_info * info)10033434f378SGuenter Roeck static int nct7802_detect(struct i2c_client *client,
10043434f378SGuenter Roeck 			  struct i2c_board_info *info)
10053434f378SGuenter Roeck {
10063434f378SGuenter Roeck 	int reg;
10073434f378SGuenter Roeck 
10083434f378SGuenter Roeck 	/*
10093434f378SGuenter Roeck 	 * Chip identification registers are only available in bank 0,
10103434f378SGuenter Roeck 	 * so only attempt chip detection if bank 0 is selected
10113434f378SGuenter Roeck 	 */
10123434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_BANK);
10133434f378SGuenter Roeck 	if (reg != 0x00)
10143434f378SGuenter Roeck 		return -ENODEV;
10153434f378SGuenter Roeck 
10163434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
10173434f378SGuenter Roeck 	if (reg != 0x50)
10183434f378SGuenter Roeck 		return -ENODEV;
10193434f378SGuenter Roeck 
10203434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
10213434f378SGuenter Roeck 	if (reg != 0xc3)
10223434f378SGuenter Roeck 		return -ENODEV;
10233434f378SGuenter Roeck 
10243434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
10253434f378SGuenter Roeck 	if (reg < 0 || (reg & 0xf0) != 0x20)
10263434f378SGuenter Roeck 		return -ENODEV;
10273434f378SGuenter Roeck 
10283434f378SGuenter Roeck 	/* Also validate lower bits of voltage and temperature registers */
10293434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
10303434f378SGuenter Roeck 	if (reg < 0 || (reg & 0x1f))
10313434f378SGuenter Roeck 		return -ENODEV;
10323434f378SGuenter Roeck 
10333434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
10343434f378SGuenter Roeck 	if (reg < 0 || (reg & 0x3f))
10353434f378SGuenter Roeck 		return -ENODEV;
10363434f378SGuenter Roeck 
10373434f378SGuenter Roeck 	reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
10383434f378SGuenter Roeck 	if (reg < 0 || (reg & 0x3f))
10393434f378SGuenter Roeck 		return -ENODEV;
10403434f378SGuenter Roeck 
1041f2f394dbSWolfram Sang 	strscpy(info->type, "nct7802", I2C_NAME_SIZE);
10423434f378SGuenter Roeck 	return 0;
10433434f378SGuenter Roeck }
10443434f378SGuenter Roeck 
nct7802_regmap_is_volatile(struct device * dev,unsigned int reg)10453434f378SGuenter Roeck static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
10463434f378SGuenter Roeck {
10471c6e8f6bSConstantine Shulyupin 	return (reg != REG_BANK && reg <= 0x20) ||
10481c6e8f6bSConstantine Shulyupin 		(reg >= REG_PWM(0) && reg <= REG_PWM(2));
10493434f378SGuenter Roeck }
10503434f378SGuenter Roeck 
10513c535bcaSKrzysztof Kozlowski static const struct regmap_config nct7802_regmap_config = {
10523434f378SGuenter Roeck 	.reg_bits = 8,
10533434f378SGuenter Roeck 	.val_bits = 8,
10543434f378SGuenter Roeck 	.cache_type = REGCACHE_RBTREE,
10553434f378SGuenter Roeck 	.volatile_reg = nct7802_regmap_is_volatile,
10563434f378SGuenter Roeck };
10573434f378SGuenter Roeck 
nct7802_get_channel_config(struct device * dev,struct device_node * node,u8 * mode_mask,u8 * mode_val)10580e346a86SOskar Senft static int nct7802_get_channel_config(struct device *dev,
10590e346a86SOskar Senft 				      struct device_node *node, u8 *mode_mask,
10600e346a86SOskar Senft 				      u8 *mode_val)
10610e346a86SOskar Senft {
10620e346a86SOskar Senft 	u32 reg;
10630e346a86SOskar Senft 	const char *type_str, *md_str;
10640e346a86SOskar Senft 	u8 md;
10650e346a86SOskar Senft 
10660e346a86SOskar Senft 	if (!node->name || of_node_cmp(node->name, "channel"))
10670e346a86SOskar Senft 		return 0;
10680e346a86SOskar Senft 
10690e346a86SOskar Senft 	if (of_property_read_u32(node, "reg", &reg)) {
10700e346a86SOskar Senft 		dev_err(dev, "Could not read reg value for '%s'\n",
10710e346a86SOskar Senft 			node->full_name);
10720e346a86SOskar Senft 		return -EINVAL;
10730e346a86SOskar Senft 	}
10740e346a86SOskar Senft 
10750e346a86SOskar Senft 	if (reg > 3) {
10760e346a86SOskar Senft 		dev_err(dev, "Invalid reg (%u) in '%s'\n", reg,
10770e346a86SOskar Senft 			node->full_name);
10780e346a86SOskar Senft 		return -EINVAL;
10790e346a86SOskar Senft 	}
10800e346a86SOskar Senft 
10810e346a86SOskar Senft 	if (reg == 0) {
10820e346a86SOskar Senft 		if (!of_device_is_available(node))
10830e346a86SOskar Senft 			*mode_val &= ~MODE_LTD_EN;
10840e346a86SOskar Senft 		else
10850e346a86SOskar Senft 			*mode_val |= MODE_LTD_EN;
10860e346a86SOskar Senft 		*mode_mask |= MODE_LTD_EN;
10870e346a86SOskar Senft 		return 0;
10880e346a86SOskar Senft 	}
10890e346a86SOskar Senft 
10900e346a86SOskar Senft 	/* At this point we have reg >= 1 && reg <= 3 */
10910e346a86SOskar Senft 
10920e346a86SOskar Senft 	if (!of_device_is_available(node)) {
10930e346a86SOskar Senft 		*mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1));
10940e346a86SOskar Senft 		*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
10950e346a86SOskar Senft 		return 0;
10960e346a86SOskar Senft 	}
10970e346a86SOskar Senft 
10980e346a86SOskar Senft 	if (of_property_read_string(node, "sensor-type", &type_str)) {
10990e346a86SOskar Senft 		dev_err(dev, "No type for '%s'\n", node->full_name);
11000e346a86SOskar Senft 		return -EINVAL;
11010e346a86SOskar Senft 	}
11020e346a86SOskar Senft 
11030e346a86SOskar Senft 	if (!strcmp(type_str, "voltage")) {
11040e346a86SOskar Senft 		*mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK)
11050e346a86SOskar Senft 			     << MODE_BIT_OFFSET_RTD(reg - 1);
11060e346a86SOskar Senft 		*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
11070e346a86SOskar Senft 		return 0;
11080e346a86SOskar Senft 	}
11090e346a86SOskar Senft 
11100e346a86SOskar Senft 	if (strcmp(type_str, "temperature")) {
11110e346a86SOskar Senft 		dev_err(dev, "Invalid type '%s' for '%s'\n", type_str,
11120e346a86SOskar Senft 			node->full_name);
11130e346a86SOskar Senft 		return -EINVAL;
11140e346a86SOskar Senft 	}
11150e346a86SOskar Senft 
11160e346a86SOskar Senft 	if (reg == 3) {
11170e346a86SOskar Senft 		/* RTD3 only supports thermistor mode */
11180e346a86SOskar Senft 		md = RTD_MODE_THERMISTOR;
11190e346a86SOskar Senft 	} else {
11200e346a86SOskar Senft 		if (of_property_read_string(node, "temperature-mode",
11210e346a86SOskar Senft 					    &md_str)) {
11220e346a86SOskar Senft 			dev_err(dev, "No mode for '%s'\n", node->full_name);
11230e346a86SOskar Senft 			return -EINVAL;
11240e346a86SOskar Senft 		}
11250e346a86SOskar Senft 
11260e346a86SOskar Senft 		if (!strcmp(md_str, "thermal-diode"))
11270e346a86SOskar Senft 			md = RTD_MODE_CURRENT;
11280e346a86SOskar Senft 		else if (!strcmp(md_str, "thermistor"))
11290e346a86SOskar Senft 			md = RTD_MODE_THERMISTOR;
11300e346a86SOskar Senft 		else {
11310e346a86SOskar Senft 			dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str,
11320e346a86SOskar Senft 				node->full_name);
11330e346a86SOskar Senft 			return -EINVAL;
11340e346a86SOskar Senft 		}
11350e346a86SOskar Senft 	}
11360e346a86SOskar Senft 
11370e346a86SOskar Senft 	*mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1);
11380e346a86SOskar Senft 	*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
11390e346a86SOskar Senft 
11400e346a86SOskar Senft 	return 0;
11410e346a86SOskar Senft }
11420e346a86SOskar Senft 
nct7802_configure_channels(struct device * dev,struct nct7802_data * data)11430e346a86SOskar Senft static int nct7802_configure_channels(struct device *dev,
11440e346a86SOskar Senft 				      struct nct7802_data *data)
11450e346a86SOskar Senft {
11460e346a86SOskar Senft 	/* Enable local temperature sensor by default */
11470e346a86SOskar Senft 	u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN;
11480e346a86SOskar Senft 	struct device_node *node;
11490e346a86SOskar Senft 	int err;
11500e346a86SOskar Senft 
11510e346a86SOskar Senft 	if (dev->of_node) {
11520e346a86SOskar Senft 		for_each_child_of_node(dev->of_node, node) {
11530e346a86SOskar Senft 			err = nct7802_get_channel_config(dev, node, &mode_mask,
11540e346a86SOskar Senft 							 &mode_val);
115510f0d2abSWan Jiabing 			if (err) {
115610f0d2abSWan Jiabing 				of_node_put(node);
11570e346a86SOskar Senft 				return err;
11580e346a86SOskar Senft 			}
11590e346a86SOskar Senft 		}
116010f0d2abSWan Jiabing 	}
11610e346a86SOskar Senft 
11620e346a86SOskar Senft 	return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val);
11630e346a86SOskar Senft }
11640e346a86SOskar Senft 
nct7802_init_chip(struct device * dev,struct nct7802_data * data)11650e346a86SOskar Senft static int nct7802_init_chip(struct device *dev, struct nct7802_data *data)
11663434f378SGuenter Roeck {
11673434f378SGuenter Roeck 	int err;
11683434f378SGuenter Roeck 
11693434f378SGuenter Roeck 	/* Enable ADC */
11703434f378SGuenter Roeck 	err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
11713434f378SGuenter Roeck 	if (err)
11723434f378SGuenter Roeck 		return err;
11733434f378SGuenter Roeck 
11740e346a86SOskar Senft 	err = nct7802_configure_channels(dev, data);
11753434f378SGuenter Roeck 	if (err)
11763434f378SGuenter Roeck 		return err;
11773434f378SGuenter Roeck 
11783434f378SGuenter Roeck 	/* Enable Vcore and VCC voltage monitoring */
11793434f378SGuenter Roeck 	return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
11803434f378SGuenter Roeck }
11813434f378SGuenter Roeck 
nct7802_probe(struct i2c_client * client)118267487038SStephen Kitt static int nct7802_probe(struct i2c_client *client)
11833434f378SGuenter Roeck {
11843434f378SGuenter Roeck 	struct device *dev = &client->dev;
11853434f378SGuenter Roeck 	struct nct7802_data *data;
11863434f378SGuenter Roeck 	struct device *hwmon_dev;
11873434f378SGuenter Roeck 	int ret;
11883434f378SGuenter Roeck 
11893434f378SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
11903434f378SGuenter Roeck 	if (data == NULL)
11913434f378SGuenter Roeck 		return -ENOMEM;
11923434f378SGuenter Roeck 
11933434f378SGuenter Roeck 	data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
11943434f378SGuenter Roeck 	if (IS_ERR(data->regmap))
11953434f378SGuenter Roeck 		return PTR_ERR(data->regmap);
11963434f378SGuenter Roeck 
11973434f378SGuenter Roeck 	mutex_init(&data->access_lock);
1198e51a7ddaSGilles Buloz 	mutex_init(&data->in_alarm_lock);
11993434f378SGuenter Roeck 
12000e346a86SOskar Senft 	ret = nct7802_init_chip(dev, data);
12013434f378SGuenter Roeck 	if (ret < 0)
12023434f378SGuenter Roeck 		return ret;
12033434f378SGuenter Roeck 
12043434f378SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
12053434f378SGuenter Roeck 							   data,
12063434f378SGuenter Roeck 							   nct7802_groups);
12073434f378SGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
12083434f378SGuenter Roeck }
12093434f378SGuenter Roeck 
12103434f378SGuenter Roeck static const unsigned short nct7802_address_list[] = {
12113434f378SGuenter Roeck 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
12123434f378SGuenter Roeck };
12133434f378SGuenter Roeck 
12143434f378SGuenter Roeck static const struct i2c_device_id nct7802_idtable[] = {
12153434f378SGuenter Roeck 	{ "nct7802", 0 },
12163434f378SGuenter Roeck 	{ }
12173434f378SGuenter Roeck };
12183434f378SGuenter Roeck MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
12193434f378SGuenter Roeck 
12203434f378SGuenter Roeck static struct i2c_driver nct7802_driver = {
12213434f378SGuenter Roeck 	.class = I2C_CLASS_HWMON,
12223434f378SGuenter Roeck 	.driver = {
12233434f378SGuenter Roeck 		.name = DRVNAME,
12243434f378SGuenter Roeck 	},
12253434f378SGuenter Roeck 	.detect = nct7802_detect,
12261975d167SUwe Kleine-König 	.probe = nct7802_probe,
12273434f378SGuenter Roeck 	.id_table = nct7802_idtable,
12283434f378SGuenter Roeck 	.address_list = nct7802_address_list,
12293434f378SGuenter Roeck };
12303434f378SGuenter Roeck 
12313434f378SGuenter Roeck module_i2c_driver(nct7802_driver);
12323434f378SGuenter Roeck 
12333434f378SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
12343434f378SGuenter Roeck MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
12353434f378SGuenter Roeck MODULE_LICENSE("GPL v2");
1236