xref: /linux/drivers/hwmon/adc128d818.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b4c9c1a7SGuenter Roeck /*
3b4c9c1a7SGuenter Roeck  * Driver for TI ADC128D818 System Monitor with Temperature Sensor
4b4c9c1a7SGuenter Roeck  *
5b4c9c1a7SGuenter Roeck  * Copyright (c) 2014 Guenter Roeck
6b4c9c1a7SGuenter Roeck  *
7b4c9c1a7SGuenter Roeck  * Derived from lm80.c
8b4c9c1a7SGuenter Roeck  * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
9b4c9c1a7SGuenter Roeck  *			     and Philip Edelbrock <phil@netroedge.com>
10b4c9c1a7SGuenter Roeck  */
11b4c9c1a7SGuenter Roeck 
12b4c9c1a7SGuenter Roeck #include <linux/module.h>
13b4c9c1a7SGuenter Roeck #include <linux/slab.h>
14b4c9c1a7SGuenter Roeck #include <linux/jiffies.h>
15b4c9c1a7SGuenter Roeck #include <linux/i2c.h>
16b4c9c1a7SGuenter Roeck #include <linux/hwmon.h>
17b4c9c1a7SGuenter Roeck #include <linux/hwmon-sysfs.h>
18b4c9c1a7SGuenter Roeck #include <linux/err.h>
19b4c9c1a7SGuenter Roeck #include <linux/regulator/consumer.h>
20b4c9c1a7SGuenter Roeck #include <linux/mutex.h>
212c3b1189SRasmus Villemoes #include <linux/bitops.h>
22a45923c2SAlexander Koch #include <linux/of.h>
23b4c9c1a7SGuenter Roeck 
24b4c9c1a7SGuenter Roeck /* Addresses to scan
25b4c9c1a7SGuenter Roeck  * The chip also supports addresses 0x35..0x37. Don't scan those addresses
26b4c9c1a7SGuenter Roeck  * since they are also used by some EEPROMs, which may result in false
27b4c9c1a7SGuenter Roeck  * positives.
28b4c9c1a7SGuenter Roeck  */
29b4c9c1a7SGuenter Roeck static const unsigned short normal_i2c[] = {
30b4c9c1a7SGuenter Roeck 	0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
31b4c9c1a7SGuenter Roeck 
32b4c9c1a7SGuenter Roeck /* registers */
33b4c9c1a7SGuenter Roeck #define ADC128_REG_IN_MAX(nr)		(0x2a + (nr) * 2)
34b4c9c1a7SGuenter Roeck #define ADC128_REG_IN_MIN(nr)		(0x2b + (nr) * 2)
35b4c9c1a7SGuenter Roeck #define ADC128_REG_IN(nr)		(0x20 + (nr))
36b4c9c1a7SGuenter Roeck 
37b4c9c1a7SGuenter Roeck #define ADC128_REG_TEMP			0x27
38b4c9c1a7SGuenter Roeck #define ADC128_REG_TEMP_MAX		0x38
39b4c9c1a7SGuenter Roeck #define ADC128_REG_TEMP_HYST		0x39
40b4c9c1a7SGuenter Roeck 
41b4c9c1a7SGuenter Roeck #define ADC128_REG_CONFIG		0x00
42b4c9c1a7SGuenter Roeck #define ADC128_REG_ALARM		0x01
43b4c9c1a7SGuenter Roeck #define ADC128_REG_MASK			0x03
44b4c9c1a7SGuenter Roeck #define ADC128_REG_CONV_RATE		0x07
45b4c9c1a7SGuenter Roeck #define ADC128_REG_ONESHOT		0x09
46b4c9c1a7SGuenter Roeck #define ADC128_REG_SHUTDOWN		0x0a
47b4c9c1a7SGuenter Roeck #define ADC128_REG_CONFIG_ADV		0x0b
48b4c9c1a7SGuenter Roeck #define ADC128_REG_BUSY_STATUS		0x0c
49b4c9c1a7SGuenter Roeck 
50b4c9c1a7SGuenter Roeck #define ADC128_REG_MAN_ID		0x3e
51b4c9c1a7SGuenter Roeck #define ADC128_REG_DEV_ID		0x3f
52b4c9c1a7SGuenter Roeck 
534e1796c8SAlexander Koch /* No. of voltage entries in adc128_attrs */
544e1796c8SAlexander Koch #define ADC128_ATTR_NUM_VOLT		(8 * 4)
554e1796c8SAlexander Koch 
564e1796c8SAlexander Koch /* Voltage inputs visible per operation mode */
574e1796c8SAlexander Koch static const u8 num_inputs[] = { 7, 8, 4, 6 };
584e1796c8SAlexander Koch 
59b4c9c1a7SGuenter Roeck struct adc128_data {
60b4c9c1a7SGuenter Roeck 	struct i2c_client *client;
61b4c9c1a7SGuenter Roeck 	int vref;		/* Reference voltage in mV */
62b4c9c1a7SGuenter Roeck 	struct mutex update_lock;
63a45923c2SAlexander Koch 	u8 mode;		/* Operation mode */
64b4c9c1a7SGuenter Roeck 	bool valid;		/* true if following fields are valid */
65b4c9c1a7SGuenter Roeck 	unsigned long last_updated;	/* In jiffies */
66b4c9c1a7SGuenter Roeck 
674e1796c8SAlexander Koch 	u16 in[3][8];		/* Register value, normalized to 12 bit
68b4c9c1a7SGuenter Roeck 				 * 0: input voltage
69b4c9c1a7SGuenter Roeck 				 * 1: min limit
70b4c9c1a7SGuenter Roeck 				 * 2: max limit
71b4c9c1a7SGuenter Roeck 				 */
72b4c9c1a7SGuenter Roeck 	s16 temp[3];		/* Register value, normalized to 9 bit
73b4c9c1a7SGuenter Roeck 				 * 0: sensor 1: limit 2: hyst
74b4c9c1a7SGuenter Roeck 				 */
75b4c9c1a7SGuenter Roeck 	u8 alarms;		/* alarm register value */
76b4c9c1a7SGuenter Roeck };
77b4c9c1a7SGuenter Roeck 
adc128_update_device(struct device * dev)78b4c9c1a7SGuenter Roeck static struct adc128_data *adc128_update_device(struct device *dev)
79b4c9c1a7SGuenter Roeck {
80b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
81b4c9c1a7SGuenter Roeck 	struct i2c_client *client = data->client;
82b4c9c1a7SGuenter Roeck 	struct adc128_data *ret = data;
83b4c9c1a7SGuenter Roeck 	int i, rv;
84b4c9c1a7SGuenter Roeck 
85b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
86b4c9c1a7SGuenter Roeck 
87b4c9c1a7SGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
884e1796c8SAlexander Koch 		for (i = 0; i < num_inputs[data->mode]; i++) {
89b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_word_swapped(client,
90b4c9c1a7SGuenter Roeck 							 ADC128_REG_IN(i));
91b4c9c1a7SGuenter Roeck 			if (rv < 0)
92b4c9c1a7SGuenter Roeck 				goto abort;
93b4c9c1a7SGuenter Roeck 			data->in[0][i] = rv >> 4;
94b4c9c1a7SGuenter Roeck 
95b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_byte_data(client,
96b4c9c1a7SGuenter Roeck 						      ADC128_REG_IN_MIN(i));
97b4c9c1a7SGuenter Roeck 			if (rv < 0)
98b4c9c1a7SGuenter Roeck 				goto abort;
99b4c9c1a7SGuenter Roeck 			data->in[1][i] = rv << 4;
100b4c9c1a7SGuenter Roeck 
101b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_byte_data(client,
102b4c9c1a7SGuenter Roeck 						      ADC128_REG_IN_MAX(i));
103b4c9c1a7SGuenter Roeck 			if (rv < 0)
104b4c9c1a7SGuenter Roeck 				goto abort;
105b4c9c1a7SGuenter Roeck 			data->in[2][i] = rv << 4;
106b4c9c1a7SGuenter Roeck 		}
107b4c9c1a7SGuenter Roeck 
1084e1796c8SAlexander Koch 		if (data->mode != 1) {
1094e1796c8SAlexander Koch 			rv = i2c_smbus_read_word_swapped(client,
1104e1796c8SAlexander Koch 							 ADC128_REG_TEMP);
111b4c9c1a7SGuenter Roeck 			if (rv < 0)
112b4c9c1a7SGuenter Roeck 				goto abort;
113b4c9c1a7SGuenter Roeck 			data->temp[0] = rv >> 7;
114b4c9c1a7SGuenter Roeck 
1154e1796c8SAlexander Koch 			rv = i2c_smbus_read_byte_data(client,
1164e1796c8SAlexander Koch 						      ADC128_REG_TEMP_MAX);
117b4c9c1a7SGuenter Roeck 			if (rv < 0)
118b4c9c1a7SGuenter Roeck 				goto abort;
119b4c9c1a7SGuenter Roeck 			data->temp[1] = rv << 1;
120b4c9c1a7SGuenter Roeck 
1214e1796c8SAlexander Koch 			rv = i2c_smbus_read_byte_data(client,
1224e1796c8SAlexander Koch 						      ADC128_REG_TEMP_HYST);
123b4c9c1a7SGuenter Roeck 			if (rv < 0)
124b4c9c1a7SGuenter Roeck 				goto abort;
125b4c9c1a7SGuenter Roeck 			data->temp[2] = rv << 1;
1264e1796c8SAlexander Koch 		}
127b4c9c1a7SGuenter Roeck 
128b4c9c1a7SGuenter Roeck 		rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
129b4c9c1a7SGuenter Roeck 		if (rv < 0)
130b4c9c1a7SGuenter Roeck 			goto abort;
131b4c9c1a7SGuenter Roeck 		data->alarms |= rv;
132b4c9c1a7SGuenter Roeck 
133b4c9c1a7SGuenter Roeck 		data->last_updated = jiffies;
134b4c9c1a7SGuenter Roeck 		data->valid = true;
135b4c9c1a7SGuenter Roeck 	}
136b4c9c1a7SGuenter Roeck 	goto done;
137b4c9c1a7SGuenter Roeck 
138b4c9c1a7SGuenter Roeck abort:
139b4c9c1a7SGuenter Roeck 	ret = ERR_PTR(rv);
140b4c9c1a7SGuenter Roeck 	data->valid = false;
141b4c9c1a7SGuenter Roeck done:
142b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
143b4c9c1a7SGuenter Roeck 	return ret;
144b4c9c1a7SGuenter Roeck }
145b4c9c1a7SGuenter Roeck 
adc128_in_show(struct device * dev,struct device_attribute * attr,char * buf)1460594462fSGuenter Roeck static ssize_t adc128_in_show(struct device *dev,
1470594462fSGuenter Roeck 			      struct device_attribute *attr, char *buf)
148b4c9c1a7SGuenter Roeck {
149b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
150b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr_2(attr)->index;
151b4c9c1a7SGuenter Roeck 	int nr = to_sensor_dev_attr_2(attr)->nr;
152b4c9c1a7SGuenter Roeck 	int val;
153b4c9c1a7SGuenter Roeck 
154b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
155b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
156b4c9c1a7SGuenter Roeck 
157b4c9c1a7SGuenter Roeck 	val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
158b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%d\n", val);
159b4c9c1a7SGuenter Roeck }
160b4c9c1a7SGuenter Roeck 
adc128_in_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1610594462fSGuenter Roeck static ssize_t adc128_in_store(struct device *dev,
1620594462fSGuenter Roeck 			       struct device_attribute *attr, const char *buf,
1630594462fSGuenter Roeck 			       size_t count)
164b4c9c1a7SGuenter Roeck {
165b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
166b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr_2(attr)->index;
167b4c9c1a7SGuenter Roeck 	int nr = to_sensor_dev_attr_2(attr)->nr;
168b4c9c1a7SGuenter Roeck 	u8 reg, regval;
169b4c9c1a7SGuenter Roeck 	long val;
170b4c9c1a7SGuenter Roeck 	int err;
171b4c9c1a7SGuenter Roeck 
172b4c9c1a7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
173b4c9c1a7SGuenter Roeck 	if (err < 0)
174b4c9c1a7SGuenter Roeck 		return err;
175b4c9c1a7SGuenter Roeck 
176b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
177b4c9c1a7SGuenter Roeck 	/* 10 mV LSB on limit registers */
178*8cad724cSGuenter Roeck 	regval = DIV_ROUND_CLOSEST(clamp_val(val, 0, 2550), 10);
179b4c9c1a7SGuenter Roeck 	data->in[index][nr] = regval << 4;
180b4c9c1a7SGuenter Roeck 	reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
181b4c9c1a7SGuenter Roeck 	i2c_smbus_write_byte_data(data->client, reg, regval);
182b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
183b4c9c1a7SGuenter Roeck 
184b4c9c1a7SGuenter Roeck 	return count;
185b4c9c1a7SGuenter Roeck }
186b4c9c1a7SGuenter Roeck 
adc128_temp_show(struct device * dev,struct device_attribute * attr,char * buf)1870594462fSGuenter Roeck static ssize_t adc128_temp_show(struct device *dev,
188b4c9c1a7SGuenter Roeck 				struct device_attribute *attr, char *buf)
189b4c9c1a7SGuenter Roeck {
190b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
191b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr(attr)->index;
192b4c9c1a7SGuenter Roeck 	int temp;
193b4c9c1a7SGuenter Roeck 
194b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
195b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
196b4c9c1a7SGuenter Roeck 
1972c3b1189SRasmus Villemoes 	temp = sign_extend32(data->temp[index], 8);
198b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
199b4c9c1a7SGuenter Roeck }
200b4c9c1a7SGuenter Roeck 
adc128_temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2010594462fSGuenter Roeck static ssize_t adc128_temp_store(struct device *dev,
202b4c9c1a7SGuenter Roeck 				 struct device_attribute *attr,
203b4c9c1a7SGuenter Roeck 				 const char *buf, size_t count)
204b4c9c1a7SGuenter Roeck {
205b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
206b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr(attr)->index;
207b4c9c1a7SGuenter Roeck 	long val;
208b4c9c1a7SGuenter Roeck 	int err;
209b4c9c1a7SGuenter Roeck 	s8 regval;
210b4c9c1a7SGuenter Roeck 
211b4c9c1a7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
212b4c9c1a7SGuenter Roeck 	if (err < 0)
213b4c9c1a7SGuenter Roeck 		return err;
214b4c9c1a7SGuenter Roeck 
215b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
216*8cad724cSGuenter Roeck 	regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
217b4c9c1a7SGuenter Roeck 	data->temp[index] = regval << 1;
218b4c9c1a7SGuenter Roeck 	i2c_smbus_write_byte_data(data->client,
219b4c9c1a7SGuenter Roeck 				  index == 1 ? ADC128_REG_TEMP_MAX
220b4c9c1a7SGuenter Roeck 					     : ADC128_REG_TEMP_HYST,
221b4c9c1a7SGuenter Roeck 				  regval);
222b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
223b4c9c1a7SGuenter Roeck 
224b4c9c1a7SGuenter Roeck 	return count;
225b4c9c1a7SGuenter Roeck }
226b4c9c1a7SGuenter Roeck 
adc128_alarm_show(struct device * dev,struct device_attribute * attr,char * buf)2270594462fSGuenter Roeck static ssize_t adc128_alarm_show(struct device *dev,
228b4c9c1a7SGuenter Roeck 				 struct device_attribute *attr, char *buf)
229b4c9c1a7SGuenter Roeck {
230b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
231b4c9c1a7SGuenter Roeck 	int mask = 1 << to_sensor_dev_attr(attr)->index;
232b4c9c1a7SGuenter Roeck 	u8 alarms;
233b4c9c1a7SGuenter Roeck 
234b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
235b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
236b4c9c1a7SGuenter Roeck 
237b4c9c1a7SGuenter Roeck 	/*
238b4c9c1a7SGuenter Roeck 	 * Clear an alarm after reporting it to user space. If it is still
239b4c9c1a7SGuenter Roeck 	 * active, the next update sequence will set the alarm bit again.
240b4c9c1a7SGuenter Roeck 	 */
241b4c9c1a7SGuenter Roeck 	alarms = data->alarms;
242b4c9c1a7SGuenter Roeck 	data->alarms &= ~mask;
243b4c9c1a7SGuenter Roeck 
244b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%u\n", !!(alarms & mask));
245b4c9c1a7SGuenter Roeck }
246b4c9c1a7SGuenter Roeck 
adc128_is_visible(struct kobject * kobj,struct attribute * attr,int index)2474e1796c8SAlexander Koch static umode_t adc128_is_visible(struct kobject *kobj,
2484e1796c8SAlexander Koch 				 struct attribute *attr, int index)
2494e1796c8SAlexander Koch {
2509d2227bbSGuenter Roeck 	struct device *dev = kobj_to_dev(kobj);
2514e1796c8SAlexander Koch 	struct adc128_data *data = dev_get_drvdata(dev);
2524e1796c8SAlexander Koch 
2534e1796c8SAlexander Koch 	if (index < ADC128_ATTR_NUM_VOLT) {
2544e1796c8SAlexander Koch 		/* Voltage, visible according to num_inputs[] */
2554e1796c8SAlexander Koch 		if (index >= num_inputs[data->mode] * 4)
2564e1796c8SAlexander Koch 			return 0;
2574e1796c8SAlexander Koch 	} else {
2584e1796c8SAlexander Koch 		/* Temperature, visible if not in mode 1 */
2594e1796c8SAlexander Koch 		if (data->mode == 1)
2604e1796c8SAlexander Koch 			return 0;
2614e1796c8SAlexander Koch 	}
2624e1796c8SAlexander Koch 
2634e1796c8SAlexander Koch 	return attr->mode;
2644e1796c8SAlexander Koch }
2654e1796c8SAlexander Koch 
2660594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0);
2670594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1);
2680594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2);
269b4c9c1a7SGuenter Roeck 
2700594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0);
2710594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1);
2720594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2);
273b4c9c1a7SGuenter Roeck 
2740594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0);
2750594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1);
2760594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2);
277b4c9c1a7SGuenter Roeck 
2780594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0);
2790594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1);
2800594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2);
281b4c9c1a7SGuenter Roeck 
2820594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0);
2830594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1);
2840594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2);
285b4c9c1a7SGuenter Roeck 
2860594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0);
2870594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1);
2880594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2);
289b4c9c1a7SGuenter Roeck 
2900594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0);
2910594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1);
2920594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2);
293b4c9c1a7SGuenter Roeck 
2940594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0);
2950594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1);
2960594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2);
2974e1796c8SAlexander Koch 
2980594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0);
2990594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1);
3000594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2);
301b4c9c1a7SGuenter Roeck 
3020594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0);
3030594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1);
3040594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2);
3050594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3);
3060594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4);
3070594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5);
3080594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6);
3090594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7);
3100594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7);
311b4c9c1a7SGuenter Roeck 
312b4c9c1a7SGuenter Roeck static struct attribute *adc128_attrs[] = {
3134e1796c8SAlexander Koch 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
314b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in0_input.dev_attr.attr,
3154e1796c8SAlexander Koch 	&sensor_dev_attr_in0_max.dev_attr.attr,
3164e1796c8SAlexander Koch 	&sensor_dev_attr_in0_min.dev_attr.attr,
3174e1796c8SAlexander Koch 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
318b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in1_input.dev_attr.attr,
3194e1796c8SAlexander Koch 	&sensor_dev_attr_in1_max.dev_attr.attr,
3204e1796c8SAlexander Koch 	&sensor_dev_attr_in1_min.dev_attr.attr,
3214e1796c8SAlexander Koch 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
322b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in2_input.dev_attr.attr,
3234e1796c8SAlexander Koch 	&sensor_dev_attr_in2_max.dev_attr.attr,
3244e1796c8SAlexander Koch 	&sensor_dev_attr_in2_min.dev_attr.attr,
3254e1796c8SAlexander Koch 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
326b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in3_input.dev_attr.attr,
3274e1796c8SAlexander Koch 	&sensor_dev_attr_in3_max.dev_attr.attr,
3284e1796c8SAlexander Koch 	&sensor_dev_attr_in3_min.dev_attr.attr,
3294e1796c8SAlexander Koch 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
330b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in4_input.dev_attr.attr,
3314e1796c8SAlexander Koch 	&sensor_dev_attr_in4_max.dev_attr.attr,
3324e1796c8SAlexander Koch 	&sensor_dev_attr_in4_min.dev_attr.attr,
3334e1796c8SAlexander Koch 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
334b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in5_input.dev_attr.attr,
3354e1796c8SAlexander Koch 	&sensor_dev_attr_in5_max.dev_attr.attr,
3364e1796c8SAlexander Koch 	&sensor_dev_attr_in5_min.dev_attr.attr,
3374e1796c8SAlexander Koch 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
338b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in6_input.dev_attr.attr,
3394e1796c8SAlexander Koch 	&sensor_dev_attr_in6_max.dev_attr.attr,
3404e1796c8SAlexander Koch 	&sensor_dev_attr_in6_min.dev_attr.attr,
3414e1796c8SAlexander Koch 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
3424e1796c8SAlexander Koch 	&sensor_dev_attr_in7_input.dev_attr.attr,
3434e1796c8SAlexander Koch 	&sensor_dev_attr_in7_max.dev_attr.attr,
3444e1796c8SAlexander Koch 	&sensor_dev_attr_in7_min.dev_attr.attr,
345b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_input.dev_attr.attr,
346b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_max.dev_attr.attr,
347b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
3484e1796c8SAlexander Koch 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
349b4c9c1a7SGuenter Roeck 	NULL
350b4c9c1a7SGuenter Roeck };
3514e1796c8SAlexander Koch 
35233d62d11SArvind Yadav static const struct attribute_group adc128_group = {
3534e1796c8SAlexander Koch 	.attrs = adc128_attrs,
3544e1796c8SAlexander Koch 	.is_visible = adc128_is_visible,
3554e1796c8SAlexander Koch };
3564e1796c8SAlexander Koch __ATTRIBUTE_GROUPS(adc128);
357b4c9c1a7SGuenter Roeck 
adc128_detect(struct i2c_client * client,struct i2c_board_info * info)358b4c9c1a7SGuenter Roeck static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
359b4c9c1a7SGuenter Roeck {
360b4c9c1a7SGuenter Roeck 	int man_id, dev_id;
361b4c9c1a7SGuenter Roeck 
362b4c9c1a7SGuenter Roeck 	if (!i2c_check_functionality(client->adapter,
363b4c9c1a7SGuenter Roeck 				     I2C_FUNC_SMBUS_BYTE_DATA |
364b4c9c1a7SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
365b4c9c1a7SGuenter Roeck 		return -ENODEV;
366b4c9c1a7SGuenter Roeck 
367b4c9c1a7SGuenter Roeck 	man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
368b4c9c1a7SGuenter Roeck 	dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
369b4c9c1a7SGuenter Roeck 	if (man_id != 0x01 || dev_id != 0x09)
370b4c9c1a7SGuenter Roeck 		return -ENODEV;
371b4c9c1a7SGuenter Roeck 
372b4c9c1a7SGuenter Roeck 	/* Check unused bits for confirmation */
373b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
374b4c9c1a7SGuenter Roeck 		return -ENODEV;
375b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
376b4c9c1a7SGuenter Roeck 		return -ENODEV;
377b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
378b4c9c1a7SGuenter Roeck 		return -ENODEV;
379b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
380b4c9c1a7SGuenter Roeck 		return -ENODEV;
381b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
382b4c9c1a7SGuenter Roeck 		return -ENODEV;
383b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
384b4c9c1a7SGuenter Roeck 		return -ENODEV;
385b4c9c1a7SGuenter Roeck 
386f2f394dbSWolfram Sang 	strscpy(info->type, "adc128d818", I2C_NAME_SIZE);
387b4c9c1a7SGuenter Roeck 
388b4c9c1a7SGuenter Roeck 	return 0;
389b4c9c1a7SGuenter Roeck }
390b4c9c1a7SGuenter Roeck 
adc128_init_client(struct adc128_data * data,bool external_vref)391cffb8d74SDavid Lechner static int adc128_init_client(struct adc128_data *data, bool external_vref)
392b4c9c1a7SGuenter Roeck {
393b4c9c1a7SGuenter Roeck 	struct i2c_client *client = data->client;
394b4c9c1a7SGuenter Roeck 	int err;
395e2f75e6bSRoy van Doormaal 	u8 regval = 0x0;
396b4c9c1a7SGuenter Roeck 
397b4c9c1a7SGuenter Roeck 	/*
398b4c9c1a7SGuenter Roeck 	 * Reset chip to defaults.
399b4c9c1a7SGuenter Roeck 	 * This makes most other initializations unnecessary.
400b4c9c1a7SGuenter Roeck 	 */
401b4c9c1a7SGuenter Roeck 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
402b4c9c1a7SGuenter Roeck 	if (err)
403b4c9c1a7SGuenter Roeck 		return err;
404b4c9c1a7SGuenter Roeck 
405a45923c2SAlexander Koch 	/* Set operation mode, if non-default */
406e2f75e6bSRoy van Doormaal 	if (data->mode != 0)
407e2f75e6bSRoy van Doormaal 		regval |= data->mode << 1;
408e2f75e6bSRoy van Doormaal 
409e2f75e6bSRoy van Doormaal 	/* If external vref is selected, configure the chip to use it */
410cffb8d74SDavid Lechner 	if (external_vref)
411e2f75e6bSRoy van Doormaal 		regval |= 0x01;
412e2f75e6bSRoy van Doormaal 
413e2f75e6bSRoy van Doormaal 	/* Write advanced configuration register */
414e2f75e6bSRoy van Doormaal 	if (regval != 0x0) {
415e2f75e6bSRoy van Doormaal 		err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV,
416e2f75e6bSRoy van Doormaal 						regval);
417a45923c2SAlexander Koch 		if (err)
418a45923c2SAlexander Koch 			return err;
419a45923c2SAlexander Koch 	}
420a45923c2SAlexander Koch 
421b4c9c1a7SGuenter Roeck 	/* Start monitoring */
422b4c9c1a7SGuenter Roeck 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
423b4c9c1a7SGuenter Roeck 	if (err)
424b4c9c1a7SGuenter Roeck 		return err;
425b4c9c1a7SGuenter Roeck 
426b4c9c1a7SGuenter Roeck 	return 0;
427b4c9c1a7SGuenter Roeck }
428b4c9c1a7SGuenter Roeck 
adc128_probe(struct i2c_client * client)429673afe46SStephen Kitt static int adc128_probe(struct i2c_client *client)
430b4c9c1a7SGuenter Roeck {
431b4c9c1a7SGuenter Roeck 	struct device *dev = &client->dev;
432b4c9c1a7SGuenter Roeck 	struct device *hwmon_dev;
433b4c9c1a7SGuenter Roeck 	struct adc128_data *data;
434cffb8d74SDavid Lechner 	bool external_vref;
435b4c9c1a7SGuenter Roeck 	int err, vref;
436b4c9c1a7SGuenter Roeck 
437b4c9c1a7SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
438b4c9c1a7SGuenter Roeck 	if (!data)
439b4c9c1a7SGuenter Roeck 		return -ENOMEM;
440b4c9c1a7SGuenter Roeck 
441b4c9c1a7SGuenter Roeck 	/* vref is optional. If specified, is used as chip reference voltage */
442cffb8d74SDavid Lechner 	vref = devm_regulator_get_enable_read_voltage(dev, "vref");
443cffb8d74SDavid Lechner 	if (vref == -ENODEV) {
444cffb8d74SDavid Lechner 		external_vref = false;
445b4c9c1a7SGuenter Roeck 		data->vref = 2560;	/* 2.56V, in mV */
446cffb8d74SDavid Lechner 	} else if (vref < 0) {
447cffb8d74SDavid Lechner 		return vref;
448cffb8d74SDavid Lechner 	} else {
449cffb8d74SDavid Lechner 		external_vref = true;
450cffb8d74SDavid Lechner 		data->vref = DIV_ROUND_CLOSEST(vref, 1000);
451b4c9c1a7SGuenter Roeck 	}
452b4c9c1a7SGuenter Roeck 
453c72eeabdSAlexander Koch 	/* Operation mode is optional. If unspecified, keep current mode */
454a45923c2SAlexander Koch 	if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) {
4554e1796c8SAlexander Koch 		if (data->mode > 3) {
4564e1796c8SAlexander Koch 			dev_err(dev, "invalid operation mode %d\n",
457a45923c2SAlexander Koch 				data->mode);
458cffb8d74SDavid Lechner 			return -EINVAL;
459a45923c2SAlexander Koch 		}
460a45923c2SAlexander Koch 	} else {
461c72eeabdSAlexander Koch 		err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV);
462c72eeabdSAlexander Koch 		if (err < 0)
463cffb8d74SDavid Lechner 			return err;
464c72eeabdSAlexander Koch 		data->mode = (err >> 1) & ADC128_REG_MASK;
465a45923c2SAlexander Koch 	}
466a45923c2SAlexander Koch 
467b4c9c1a7SGuenter Roeck 	data->client = client;
468b4c9c1a7SGuenter Roeck 	i2c_set_clientdata(client, data);
469b4c9c1a7SGuenter Roeck 	mutex_init(&data->update_lock);
470b4c9c1a7SGuenter Roeck 
471b4c9c1a7SGuenter Roeck 	/* Initialize the chip */
472cffb8d74SDavid Lechner 	err = adc128_init_client(data, external_vref);
473b4c9c1a7SGuenter Roeck 	if (err < 0)
474cffb8d74SDavid Lechner 		return err;
475b4c9c1a7SGuenter Roeck 
476b4c9c1a7SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
477b4c9c1a7SGuenter Roeck 							   data, adc128_groups);
478b4c9c1a7SGuenter Roeck 
479346fe0ceSDavid Lechner 	return PTR_ERR_OR_ZERO(hwmon_dev);
480b4c9c1a7SGuenter Roeck }
481b4c9c1a7SGuenter Roeck 
482b4c9c1a7SGuenter Roeck static const struct i2c_device_id adc128_id[] = {
483d8a66f36SUwe Kleine-König 	{ "adc128d818" },
484b4c9c1a7SGuenter Roeck 	{ }
485b4c9c1a7SGuenter Roeck };
486b4c9c1a7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, adc128_id);
487b4c9c1a7SGuenter Roeck 
488462d7e7eSGuenter Roeck static const struct of_device_id __maybe_unused adc128_of_match[] = {
489d593e665SJavier Martinez Canillas 	{ .compatible = "ti,adc128d818" },
490d593e665SJavier Martinez Canillas 	{ },
491d593e665SJavier Martinez Canillas };
492d593e665SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adc128_of_match);
493d593e665SJavier Martinez Canillas 
494b4c9c1a7SGuenter Roeck static struct i2c_driver adc128_driver = {
495b4c9c1a7SGuenter Roeck 	.class		= I2C_CLASS_HWMON,
496b4c9c1a7SGuenter Roeck 	.driver = {
497b4c9c1a7SGuenter Roeck 		.name	= "adc128d818",
498d593e665SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adc128_of_match),
499b4c9c1a7SGuenter Roeck 	},
5001975d167SUwe Kleine-König 	.probe		= adc128_probe,
501b4c9c1a7SGuenter Roeck 	.id_table	= adc128_id,
502b4c9c1a7SGuenter Roeck 	.detect		= adc128_detect,
503b4c9c1a7SGuenter Roeck 	.address_list	= normal_i2c,
504b4c9c1a7SGuenter Roeck };
505b4c9c1a7SGuenter Roeck 
506b4c9c1a7SGuenter Roeck module_i2c_driver(adc128_driver);
507b4c9c1a7SGuenter Roeck 
508b4c9c1a7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
509b4c9c1a7SGuenter Roeck MODULE_DESCRIPTION("Driver for ADC128D818");
510b4c9c1a7SGuenter Roeck MODULE_LICENSE("GPL");
511