xref: /linux/drivers/hwmon/adc128d818.c (revision 673afe466166cda94053d2d2400e669fe19f8050)
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 	struct regulator *regulator;
62b4c9c1a7SGuenter Roeck 	int vref;		/* Reference voltage in mV */
63b4c9c1a7SGuenter Roeck 	struct mutex update_lock;
64a45923c2SAlexander Koch 	u8 mode;		/* Operation mode */
65b4c9c1a7SGuenter Roeck 	bool valid;		/* true if following fields are valid */
66b4c9c1a7SGuenter Roeck 	unsigned long last_updated;	/* In jiffies */
67b4c9c1a7SGuenter Roeck 
684e1796c8SAlexander Koch 	u16 in[3][8];		/* Register value, normalized to 12 bit
69b4c9c1a7SGuenter Roeck 				 * 0: input voltage
70b4c9c1a7SGuenter Roeck 				 * 1: min limit
71b4c9c1a7SGuenter Roeck 				 * 2: max limit
72b4c9c1a7SGuenter Roeck 				 */
73b4c9c1a7SGuenter Roeck 	s16 temp[3];		/* Register value, normalized to 9 bit
74b4c9c1a7SGuenter Roeck 				 * 0: sensor 1: limit 2: hyst
75b4c9c1a7SGuenter Roeck 				 */
76b4c9c1a7SGuenter Roeck 	u8 alarms;		/* alarm register value */
77b4c9c1a7SGuenter Roeck };
78b4c9c1a7SGuenter Roeck 
79b4c9c1a7SGuenter Roeck static struct adc128_data *adc128_update_device(struct device *dev)
80b4c9c1a7SGuenter Roeck {
81b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
82b4c9c1a7SGuenter Roeck 	struct i2c_client *client = data->client;
83b4c9c1a7SGuenter Roeck 	struct adc128_data *ret = data;
84b4c9c1a7SGuenter Roeck 	int i, rv;
85b4c9c1a7SGuenter Roeck 
86b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
87b4c9c1a7SGuenter Roeck 
88b4c9c1a7SGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
894e1796c8SAlexander Koch 		for (i = 0; i < num_inputs[data->mode]; i++) {
90b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_word_swapped(client,
91b4c9c1a7SGuenter Roeck 							 ADC128_REG_IN(i));
92b4c9c1a7SGuenter Roeck 			if (rv < 0)
93b4c9c1a7SGuenter Roeck 				goto abort;
94b4c9c1a7SGuenter Roeck 			data->in[0][i] = rv >> 4;
95b4c9c1a7SGuenter Roeck 
96b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_byte_data(client,
97b4c9c1a7SGuenter Roeck 						      ADC128_REG_IN_MIN(i));
98b4c9c1a7SGuenter Roeck 			if (rv < 0)
99b4c9c1a7SGuenter Roeck 				goto abort;
100b4c9c1a7SGuenter Roeck 			data->in[1][i] = rv << 4;
101b4c9c1a7SGuenter Roeck 
102b4c9c1a7SGuenter Roeck 			rv = i2c_smbus_read_byte_data(client,
103b4c9c1a7SGuenter Roeck 						      ADC128_REG_IN_MAX(i));
104b4c9c1a7SGuenter Roeck 			if (rv < 0)
105b4c9c1a7SGuenter Roeck 				goto abort;
106b4c9c1a7SGuenter Roeck 			data->in[2][i] = rv << 4;
107b4c9c1a7SGuenter Roeck 		}
108b4c9c1a7SGuenter Roeck 
1094e1796c8SAlexander Koch 		if (data->mode != 1) {
1104e1796c8SAlexander Koch 			rv = i2c_smbus_read_word_swapped(client,
1114e1796c8SAlexander Koch 							 ADC128_REG_TEMP);
112b4c9c1a7SGuenter Roeck 			if (rv < 0)
113b4c9c1a7SGuenter Roeck 				goto abort;
114b4c9c1a7SGuenter Roeck 			data->temp[0] = rv >> 7;
115b4c9c1a7SGuenter Roeck 
1164e1796c8SAlexander Koch 			rv = i2c_smbus_read_byte_data(client,
1174e1796c8SAlexander Koch 						      ADC128_REG_TEMP_MAX);
118b4c9c1a7SGuenter Roeck 			if (rv < 0)
119b4c9c1a7SGuenter Roeck 				goto abort;
120b4c9c1a7SGuenter Roeck 			data->temp[1] = rv << 1;
121b4c9c1a7SGuenter Roeck 
1224e1796c8SAlexander Koch 			rv = i2c_smbus_read_byte_data(client,
1234e1796c8SAlexander Koch 						      ADC128_REG_TEMP_HYST);
124b4c9c1a7SGuenter Roeck 			if (rv < 0)
125b4c9c1a7SGuenter Roeck 				goto abort;
126b4c9c1a7SGuenter Roeck 			data->temp[2] = rv << 1;
1274e1796c8SAlexander Koch 		}
128b4c9c1a7SGuenter Roeck 
129b4c9c1a7SGuenter Roeck 		rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
130b4c9c1a7SGuenter Roeck 		if (rv < 0)
131b4c9c1a7SGuenter Roeck 			goto abort;
132b4c9c1a7SGuenter Roeck 		data->alarms |= rv;
133b4c9c1a7SGuenter Roeck 
134b4c9c1a7SGuenter Roeck 		data->last_updated = jiffies;
135b4c9c1a7SGuenter Roeck 		data->valid = true;
136b4c9c1a7SGuenter Roeck 	}
137b4c9c1a7SGuenter Roeck 	goto done;
138b4c9c1a7SGuenter Roeck 
139b4c9c1a7SGuenter Roeck abort:
140b4c9c1a7SGuenter Roeck 	ret = ERR_PTR(rv);
141b4c9c1a7SGuenter Roeck 	data->valid = false;
142b4c9c1a7SGuenter Roeck done:
143b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
144b4c9c1a7SGuenter Roeck 	return ret;
145b4c9c1a7SGuenter Roeck }
146b4c9c1a7SGuenter Roeck 
1470594462fSGuenter Roeck static ssize_t adc128_in_show(struct device *dev,
1480594462fSGuenter Roeck 			      struct device_attribute *attr, char *buf)
149b4c9c1a7SGuenter Roeck {
150b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
151b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr_2(attr)->index;
152b4c9c1a7SGuenter Roeck 	int nr = to_sensor_dev_attr_2(attr)->nr;
153b4c9c1a7SGuenter Roeck 	int val;
154b4c9c1a7SGuenter Roeck 
155b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
156b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
157b4c9c1a7SGuenter Roeck 
158b4c9c1a7SGuenter Roeck 	val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
159b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%d\n", val);
160b4c9c1a7SGuenter Roeck }
161b4c9c1a7SGuenter Roeck 
1620594462fSGuenter Roeck static ssize_t adc128_in_store(struct device *dev,
1630594462fSGuenter Roeck 			       struct device_attribute *attr, const char *buf,
1640594462fSGuenter Roeck 			       size_t count)
165b4c9c1a7SGuenter Roeck {
166b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
167b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr_2(attr)->index;
168b4c9c1a7SGuenter Roeck 	int nr = to_sensor_dev_attr_2(attr)->nr;
169b4c9c1a7SGuenter Roeck 	u8 reg, regval;
170b4c9c1a7SGuenter Roeck 	long val;
171b4c9c1a7SGuenter Roeck 	int err;
172b4c9c1a7SGuenter Roeck 
173b4c9c1a7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
174b4c9c1a7SGuenter Roeck 	if (err < 0)
175b4c9c1a7SGuenter Roeck 		return err;
176b4c9c1a7SGuenter Roeck 
177b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
178b4c9c1a7SGuenter Roeck 	/* 10 mV LSB on limit registers */
179b4c9c1a7SGuenter Roeck 	regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
180b4c9c1a7SGuenter Roeck 	data->in[index][nr] = regval << 4;
181b4c9c1a7SGuenter Roeck 	reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
182b4c9c1a7SGuenter Roeck 	i2c_smbus_write_byte_data(data->client, reg, regval);
183b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
184b4c9c1a7SGuenter Roeck 
185b4c9c1a7SGuenter Roeck 	return count;
186b4c9c1a7SGuenter Roeck }
187b4c9c1a7SGuenter Roeck 
1880594462fSGuenter Roeck static ssize_t adc128_temp_show(struct device *dev,
189b4c9c1a7SGuenter Roeck 				struct device_attribute *attr, char *buf)
190b4c9c1a7SGuenter Roeck {
191b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
192b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr(attr)->index;
193b4c9c1a7SGuenter Roeck 	int temp;
194b4c9c1a7SGuenter Roeck 
195b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
196b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
197b4c9c1a7SGuenter Roeck 
1982c3b1189SRasmus Villemoes 	temp = sign_extend32(data->temp[index], 8);
199b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
200b4c9c1a7SGuenter Roeck }
201b4c9c1a7SGuenter Roeck 
2020594462fSGuenter Roeck static ssize_t adc128_temp_store(struct device *dev,
203b4c9c1a7SGuenter Roeck 				 struct device_attribute *attr,
204b4c9c1a7SGuenter Roeck 				 const char *buf, size_t count)
205b4c9c1a7SGuenter Roeck {
206b4c9c1a7SGuenter Roeck 	struct adc128_data *data = dev_get_drvdata(dev);
207b4c9c1a7SGuenter Roeck 	int index = to_sensor_dev_attr(attr)->index;
208b4c9c1a7SGuenter Roeck 	long val;
209b4c9c1a7SGuenter Roeck 	int err;
210b4c9c1a7SGuenter Roeck 	s8 regval;
211b4c9c1a7SGuenter Roeck 
212b4c9c1a7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
213b4c9c1a7SGuenter Roeck 	if (err < 0)
214b4c9c1a7SGuenter Roeck 		return err;
215b4c9c1a7SGuenter Roeck 
216b4c9c1a7SGuenter Roeck 	mutex_lock(&data->update_lock);
217b4c9c1a7SGuenter Roeck 	regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
218b4c9c1a7SGuenter Roeck 	data->temp[index] = regval << 1;
219b4c9c1a7SGuenter Roeck 	i2c_smbus_write_byte_data(data->client,
220b4c9c1a7SGuenter Roeck 				  index == 1 ? ADC128_REG_TEMP_MAX
221b4c9c1a7SGuenter Roeck 					     : ADC128_REG_TEMP_HYST,
222b4c9c1a7SGuenter Roeck 				  regval);
223b4c9c1a7SGuenter Roeck 	mutex_unlock(&data->update_lock);
224b4c9c1a7SGuenter Roeck 
225b4c9c1a7SGuenter Roeck 	return count;
226b4c9c1a7SGuenter Roeck }
227b4c9c1a7SGuenter Roeck 
2280594462fSGuenter Roeck static ssize_t adc128_alarm_show(struct device *dev,
229b4c9c1a7SGuenter Roeck 				 struct device_attribute *attr, char *buf)
230b4c9c1a7SGuenter Roeck {
231b4c9c1a7SGuenter Roeck 	struct adc128_data *data = adc128_update_device(dev);
232b4c9c1a7SGuenter Roeck 	int mask = 1 << to_sensor_dev_attr(attr)->index;
233b4c9c1a7SGuenter Roeck 	u8 alarms;
234b4c9c1a7SGuenter Roeck 
235b4c9c1a7SGuenter Roeck 	if (IS_ERR(data))
236b4c9c1a7SGuenter Roeck 		return PTR_ERR(data);
237b4c9c1a7SGuenter Roeck 
238b4c9c1a7SGuenter Roeck 	/*
239b4c9c1a7SGuenter Roeck 	 * Clear an alarm after reporting it to user space. If it is still
240b4c9c1a7SGuenter Roeck 	 * active, the next update sequence will set the alarm bit again.
241b4c9c1a7SGuenter Roeck 	 */
242b4c9c1a7SGuenter Roeck 	alarms = data->alarms;
243b4c9c1a7SGuenter Roeck 	data->alarms &= ~mask;
244b4c9c1a7SGuenter Roeck 
245b4c9c1a7SGuenter Roeck 	return sprintf(buf, "%u\n", !!(alarms & mask));
246b4c9c1a7SGuenter Roeck }
247b4c9c1a7SGuenter Roeck 
2484e1796c8SAlexander Koch static umode_t adc128_is_visible(struct kobject *kobj,
2494e1796c8SAlexander Koch 				 struct attribute *attr, int index)
2504e1796c8SAlexander Koch {
2514e1796c8SAlexander Koch 	struct device *dev = container_of(kobj, struct device, kobj);
2524e1796c8SAlexander Koch 	struct adc128_data *data = dev_get_drvdata(dev);
2534e1796c8SAlexander Koch 
2544e1796c8SAlexander Koch 	if (index < ADC128_ATTR_NUM_VOLT) {
2554e1796c8SAlexander Koch 		/* Voltage, visible according to num_inputs[] */
2564e1796c8SAlexander Koch 		if (index >= num_inputs[data->mode] * 4)
2574e1796c8SAlexander Koch 			return 0;
2584e1796c8SAlexander Koch 	} else {
2594e1796c8SAlexander Koch 		/* Temperature, visible if not in mode 1 */
2604e1796c8SAlexander Koch 		if (data->mode == 1)
2614e1796c8SAlexander Koch 			return 0;
2624e1796c8SAlexander Koch 	}
2634e1796c8SAlexander Koch 
2644e1796c8SAlexander Koch 	return attr->mode;
2654e1796c8SAlexander Koch }
2664e1796c8SAlexander Koch 
2670594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0);
2680594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1);
2690594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2);
270b4c9c1a7SGuenter Roeck 
2710594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0);
2720594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1);
2730594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2);
274b4c9c1a7SGuenter Roeck 
2750594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0);
2760594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1);
2770594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2);
278b4c9c1a7SGuenter Roeck 
2790594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0);
2800594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1);
2810594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2);
282b4c9c1a7SGuenter Roeck 
2830594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0);
2840594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1);
2850594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2);
286b4c9c1a7SGuenter Roeck 
2870594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0);
2880594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1);
2890594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2);
290b4c9c1a7SGuenter Roeck 
2910594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0);
2920594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1);
2930594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2);
294b4c9c1a7SGuenter Roeck 
2950594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0);
2960594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1);
2970594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2);
2984e1796c8SAlexander Koch 
2990594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0);
3000594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1);
3010594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2);
302b4c9c1a7SGuenter Roeck 
3030594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0);
3040594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1);
3050594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2);
3060594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3);
3070594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4);
3080594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5);
3090594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6);
3100594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7);
3110594462fSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7);
312b4c9c1a7SGuenter Roeck 
313b4c9c1a7SGuenter Roeck static struct attribute *adc128_attrs[] = {
3144e1796c8SAlexander Koch 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
315b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in0_input.dev_attr.attr,
3164e1796c8SAlexander Koch 	&sensor_dev_attr_in0_max.dev_attr.attr,
3174e1796c8SAlexander Koch 	&sensor_dev_attr_in0_min.dev_attr.attr,
3184e1796c8SAlexander Koch 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
319b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in1_input.dev_attr.attr,
3204e1796c8SAlexander Koch 	&sensor_dev_attr_in1_max.dev_attr.attr,
3214e1796c8SAlexander Koch 	&sensor_dev_attr_in1_min.dev_attr.attr,
3224e1796c8SAlexander Koch 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
323b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in2_input.dev_attr.attr,
3244e1796c8SAlexander Koch 	&sensor_dev_attr_in2_max.dev_attr.attr,
3254e1796c8SAlexander Koch 	&sensor_dev_attr_in2_min.dev_attr.attr,
3264e1796c8SAlexander Koch 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
327b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in3_input.dev_attr.attr,
3284e1796c8SAlexander Koch 	&sensor_dev_attr_in3_max.dev_attr.attr,
3294e1796c8SAlexander Koch 	&sensor_dev_attr_in3_min.dev_attr.attr,
3304e1796c8SAlexander Koch 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
331b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in4_input.dev_attr.attr,
3324e1796c8SAlexander Koch 	&sensor_dev_attr_in4_max.dev_attr.attr,
3334e1796c8SAlexander Koch 	&sensor_dev_attr_in4_min.dev_attr.attr,
3344e1796c8SAlexander Koch 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
335b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in5_input.dev_attr.attr,
3364e1796c8SAlexander Koch 	&sensor_dev_attr_in5_max.dev_attr.attr,
3374e1796c8SAlexander Koch 	&sensor_dev_attr_in5_min.dev_attr.attr,
3384e1796c8SAlexander Koch 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
339b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_in6_input.dev_attr.attr,
3404e1796c8SAlexander Koch 	&sensor_dev_attr_in6_max.dev_attr.attr,
3414e1796c8SAlexander Koch 	&sensor_dev_attr_in6_min.dev_attr.attr,
3424e1796c8SAlexander Koch 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
3434e1796c8SAlexander Koch 	&sensor_dev_attr_in7_input.dev_attr.attr,
3444e1796c8SAlexander Koch 	&sensor_dev_attr_in7_max.dev_attr.attr,
3454e1796c8SAlexander Koch 	&sensor_dev_attr_in7_min.dev_attr.attr,
346b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_input.dev_attr.attr,
347b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_max.dev_attr.attr,
348b4c9c1a7SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
3494e1796c8SAlexander Koch 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
350b4c9c1a7SGuenter Roeck 	NULL
351b4c9c1a7SGuenter Roeck };
3524e1796c8SAlexander Koch 
35333d62d11SArvind Yadav static const struct attribute_group adc128_group = {
3544e1796c8SAlexander Koch 	.attrs = adc128_attrs,
3554e1796c8SAlexander Koch 	.is_visible = adc128_is_visible,
3564e1796c8SAlexander Koch };
3574e1796c8SAlexander Koch __ATTRIBUTE_GROUPS(adc128);
358b4c9c1a7SGuenter Roeck 
359b4c9c1a7SGuenter Roeck static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
360b4c9c1a7SGuenter Roeck {
361b4c9c1a7SGuenter Roeck 	int man_id, dev_id;
362b4c9c1a7SGuenter Roeck 
363b4c9c1a7SGuenter Roeck 	if (!i2c_check_functionality(client->adapter,
364b4c9c1a7SGuenter Roeck 				     I2C_FUNC_SMBUS_BYTE_DATA |
365b4c9c1a7SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
366b4c9c1a7SGuenter Roeck 		return -ENODEV;
367b4c9c1a7SGuenter Roeck 
368b4c9c1a7SGuenter Roeck 	man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
369b4c9c1a7SGuenter Roeck 	dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
370b4c9c1a7SGuenter Roeck 	if (man_id != 0x01 || dev_id != 0x09)
371b4c9c1a7SGuenter Roeck 		return -ENODEV;
372b4c9c1a7SGuenter Roeck 
373b4c9c1a7SGuenter Roeck 	/* Check unused bits for confirmation */
374b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
375b4c9c1a7SGuenter Roeck 		return -ENODEV;
376b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
377b4c9c1a7SGuenter Roeck 		return -ENODEV;
378b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
379b4c9c1a7SGuenter Roeck 		return -ENODEV;
380b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
381b4c9c1a7SGuenter Roeck 		return -ENODEV;
382b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
383b4c9c1a7SGuenter Roeck 		return -ENODEV;
384b4c9c1a7SGuenter Roeck 	if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
385b4c9c1a7SGuenter Roeck 		return -ENODEV;
386b4c9c1a7SGuenter Roeck 
387b4c9c1a7SGuenter Roeck 	strlcpy(info->type, "adc128d818", I2C_NAME_SIZE);
388b4c9c1a7SGuenter Roeck 
389b4c9c1a7SGuenter Roeck 	return 0;
390b4c9c1a7SGuenter Roeck }
391b4c9c1a7SGuenter Roeck 
392b4c9c1a7SGuenter Roeck static int adc128_init_client(struct adc128_data *data)
393b4c9c1a7SGuenter Roeck {
394b4c9c1a7SGuenter Roeck 	struct i2c_client *client = data->client;
395b4c9c1a7SGuenter Roeck 	int err;
396e2f75e6bSRoy van Doormaal 	u8 regval = 0x0;
397b4c9c1a7SGuenter Roeck 
398b4c9c1a7SGuenter Roeck 	/*
399b4c9c1a7SGuenter Roeck 	 * Reset chip to defaults.
400b4c9c1a7SGuenter Roeck 	 * This makes most other initializations unnecessary.
401b4c9c1a7SGuenter Roeck 	 */
402b4c9c1a7SGuenter Roeck 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
403b4c9c1a7SGuenter Roeck 	if (err)
404b4c9c1a7SGuenter Roeck 		return err;
405b4c9c1a7SGuenter Roeck 
406a45923c2SAlexander Koch 	/* Set operation mode, if non-default */
407e2f75e6bSRoy van Doormaal 	if (data->mode != 0)
408e2f75e6bSRoy van Doormaal 		regval |= data->mode << 1;
409e2f75e6bSRoy van Doormaal 
410e2f75e6bSRoy van Doormaal 	/* If external vref is selected, configure the chip to use it */
411e2f75e6bSRoy van Doormaal 	if (data->regulator)
412e2f75e6bSRoy van Doormaal 		regval |= 0x01;
413e2f75e6bSRoy van Doormaal 
414e2f75e6bSRoy van Doormaal 	/* Write advanced configuration register */
415e2f75e6bSRoy van Doormaal 	if (regval != 0x0) {
416e2f75e6bSRoy van Doormaal 		err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV,
417e2f75e6bSRoy van Doormaal 						regval);
418a45923c2SAlexander Koch 		if (err)
419a45923c2SAlexander Koch 			return err;
420a45923c2SAlexander Koch 	}
421a45923c2SAlexander Koch 
422b4c9c1a7SGuenter Roeck 	/* Start monitoring */
423b4c9c1a7SGuenter Roeck 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
424b4c9c1a7SGuenter Roeck 	if (err)
425b4c9c1a7SGuenter Roeck 		return err;
426b4c9c1a7SGuenter Roeck 
427b4c9c1a7SGuenter Roeck 	return 0;
428b4c9c1a7SGuenter Roeck }
429b4c9c1a7SGuenter Roeck 
430*673afe46SStephen Kitt static int adc128_probe(struct i2c_client *client)
431b4c9c1a7SGuenter Roeck {
432b4c9c1a7SGuenter Roeck 	struct device *dev = &client->dev;
433b4c9c1a7SGuenter Roeck 	struct regulator *regulator;
434b4c9c1a7SGuenter Roeck 	struct device *hwmon_dev;
435b4c9c1a7SGuenter Roeck 	struct adc128_data *data;
436b4c9c1a7SGuenter Roeck 	int err, vref;
437b4c9c1a7SGuenter Roeck 
438b4c9c1a7SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
439b4c9c1a7SGuenter Roeck 	if (!data)
440b4c9c1a7SGuenter Roeck 		return -ENOMEM;
441b4c9c1a7SGuenter Roeck 
442b4c9c1a7SGuenter Roeck 	/* vref is optional. If specified, is used as chip reference voltage */
443b4c9c1a7SGuenter Roeck 	regulator = devm_regulator_get_optional(dev, "vref");
444b4c9c1a7SGuenter Roeck 	if (!IS_ERR(regulator)) {
445b4c9c1a7SGuenter Roeck 		data->regulator = regulator;
446b4c9c1a7SGuenter Roeck 		err = regulator_enable(regulator);
447b4c9c1a7SGuenter Roeck 		if (err < 0)
448b4c9c1a7SGuenter Roeck 			return err;
449b4c9c1a7SGuenter Roeck 		vref = regulator_get_voltage(regulator);
450b4c9c1a7SGuenter Roeck 		if (vref < 0) {
451b4c9c1a7SGuenter Roeck 			err = vref;
452b4c9c1a7SGuenter Roeck 			goto error;
453b4c9c1a7SGuenter Roeck 		}
454b4c9c1a7SGuenter Roeck 		data->vref = DIV_ROUND_CLOSEST(vref, 1000);
455b4c9c1a7SGuenter Roeck 	} else {
456b4c9c1a7SGuenter Roeck 		data->vref = 2560;	/* 2.56V, in mV */
457b4c9c1a7SGuenter Roeck 	}
458b4c9c1a7SGuenter Roeck 
459c72eeabdSAlexander Koch 	/* Operation mode is optional. If unspecified, keep current mode */
460a45923c2SAlexander Koch 	if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) {
4614e1796c8SAlexander Koch 		if (data->mode > 3) {
4624e1796c8SAlexander Koch 			dev_err(dev, "invalid operation mode %d\n",
463a45923c2SAlexander Koch 				data->mode);
464a45923c2SAlexander Koch 			err = -EINVAL;
465a45923c2SAlexander Koch 			goto error;
466a45923c2SAlexander Koch 		}
467a45923c2SAlexander Koch 	} else {
468c72eeabdSAlexander Koch 		err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV);
469c72eeabdSAlexander Koch 		if (err < 0)
470c72eeabdSAlexander Koch 			goto error;
471c72eeabdSAlexander Koch 		data->mode = (err >> 1) & ADC128_REG_MASK;
472a45923c2SAlexander Koch 	}
473a45923c2SAlexander Koch 
474b4c9c1a7SGuenter Roeck 	data->client = client;
475b4c9c1a7SGuenter Roeck 	i2c_set_clientdata(client, data);
476b4c9c1a7SGuenter Roeck 	mutex_init(&data->update_lock);
477b4c9c1a7SGuenter Roeck 
478b4c9c1a7SGuenter Roeck 	/* Initialize the chip */
479b4c9c1a7SGuenter Roeck 	err = adc128_init_client(data);
480b4c9c1a7SGuenter Roeck 	if (err < 0)
481b4c9c1a7SGuenter Roeck 		goto error;
482b4c9c1a7SGuenter Roeck 
483b4c9c1a7SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
484b4c9c1a7SGuenter Roeck 							   data, adc128_groups);
485b4c9c1a7SGuenter Roeck 	if (IS_ERR(hwmon_dev)) {
486b4c9c1a7SGuenter Roeck 		err = PTR_ERR(hwmon_dev);
487b4c9c1a7SGuenter Roeck 		goto error;
488b4c9c1a7SGuenter Roeck 	}
489b4c9c1a7SGuenter Roeck 
490b4c9c1a7SGuenter Roeck 	return 0;
491b4c9c1a7SGuenter Roeck 
492b4c9c1a7SGuenter Roeck error:
493b4c9c1a7SGuenter Roeck 	if (data->regulator)
494b4c9c1a7SGuenter Roeck 		regulator_disable(data->regulator);
495b4c9c1a7SGuenter Roeck 	return err;
496b4c9c1a7SGuenter Roeck }
497b4c9c1a7SGuenter Roeck 
498b4c9c1a7SGuenter Roeck static int adc128_remove(struct i2c_client *client)
499b4c9c1a7SGuenter Roeck {
500b4c9c1a7SGuenter Roeck 	struct adc128_data *data = i2c_get_clientdata(client);
501b4c9c1a7SGuenter Roeck 
502b4c9c1a7SGuenter Roeck 	if (data->regulator)
503b4c9c1a7SGuenter Roeck 		regulator_disable(data->regulator);
504b4c9c1a7SGuenter Roeck 
505b4c9c1a7SGuenter Roeck 	return 0;
506b4c9c1a7SGuenter Roeck }
507b4c9c1a7SGuenter Roeck 
508b4c9c1a7SGuenter Roeck static const struct i2c_device_id adc128_id[] = {
509b4c9c1a7SGuenter Roeck 	{ "adc128d818", 0 },
510b4c9c1a7SGuenter Roeck 	{ }
511b4c9c1a7SGuenter Roeck };
512b4c9c1a7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, adc128_id);
513b4c9c1a7SGuenter Roeck 
514462d7e7eSGuenter Roeck static const struct of_device_id __maybe_unused adc128_of_match[] = {
515d593e665SJavier Martinez Canillas 	{ .compatible = "ti,adc128d818" },
516d593e665SJavier Martinez Canillas 	{ },
517d593e665SJavier Martinez Canillas };
518d593e665SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adc128_of_match);
519d593e665SJavier Martinez Canillas 
520b4c9c1a7SGuenter Roeck static struct i2c_driver adc128_driver = {
521b4c9c1a7SGuenter Roeck 	.class		= I2C_CLASS_HWMON,
522b4c9c1a7SGuenter Roeck 	.driver = {
523b4c9c1a7SGuenter Roeck 		.name	= "adc128d818",
524d593e665SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adc128_of_match),
525b4c9c1a7SGuenter Roeck 	},
526*673afe46SStephen Kitt 	.probe_new	= adc128_probe,
527b4c9c1a7SGuenter Roeck 	.remove		= adc128_remove,
528b4c9c1a7SGuenter Roeck 	.id_table	= adc128_id,
529b4c9c1a7SGuenter Roeck 	.detect		= adc128_detect,
530b4c9c1a7SGuenter Roeck 	.address_list	= normal_i2c,
531b4c9c1a7SGuenter Roeck };
532b4c9c1a7SGuenter Roeck 
533b4c9c1a7SGuenter Roeck module_i2c_driver(adc128_driver);
534b4c9c1a7SGuenter Roeck 
535b4c9c1a7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
536b4c9c1a7SGuenter Roeck MODULE_DESCRIPTION("Driver for ADC128D818");
537b4c9c1a7SGuenter Roeck MODULE_LICENSE("GPL");
538