xref: /openbmc/linux/drivers/iio/adc/ti-adc081c.c (revision 0d3b051a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4  *
5  * Copyright (C) 2012 Avionic Design GmbH
6  * Copyright (C) 2016 Intel
7  *
8  * Datasheets:
9  *	https://www.ti.com/lit/ds/symlink/adc081c021.pdf
10  *	https://www.ti.com/lit/ds/symlink/adc101c021.pdf
11  *	https://www.ti.com/lit/ds/symlink/adc121c021.pdf
12  *
13  * The devices have a very similar interface and differ mostly in the number of
14  * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
15  * bits of value registers are reserved.
16  */
17 
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/regulator/consumer.h>
28 
29 struct adc081c {
30 	struct i2c_client *i2c;
31 	struct regulator *ref;
32 
33 	/* 8, 10 or 12 */
34 	int bits;
35 
36 	/* Ensure natural alignment of buffer elements */
37 	struct {
38 		u16 channel;
39 		s64 ts __aligned(8);
40 	} scan;
41 };
42 
43 #define REG_CONV_RES 0x00
44 
45 static int adc081c_read_raw(struct iio_dev *iio,
46 			    struct iio_chan_spec const *channel, int *value,
47 			    int *shift, long mask)
48 {
49 	struct adc081c *adc = iio_priv(iio);
50 	int err;
51 
52 	switch (mask) {
53 	case IIO_CHAN_INFO_RAW:
54 		err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
55 		if (err < 0)
56 			return err;
57 
58 		*value = (err & 0xFFF) >> (12 - adc->bits);
59 		return IIO_VAL_INT;
60 
61 	case IIO_CHAN_INFO_SCALE:
62 		err = regulator_get_voltage(adc->ref);
63 		if (err < 0)
64 			return err;
65 
66 		*value = err / 1000;
67 		*shift = adc->bits;
68 
69 		return IIO_VAL_FRACTIONAL_LOG2;
70 
71 	default:
72 		break;
73 	}
74 
75 	return -EINVAL;
76 }
77 
78 #define ADCxx1C_CHAN(_bits) {					\
79 	.type = IIO_VOLTAGE,					\
80 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
81 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
82 	.scan_type = {						\
83 		.sign = 'u',					\
84 		.realbits = (_bits),				\
85 		.storagebits = 16,				\
86 		.shift = 12 - (_bits),				\
87 		.endianness = IIO_CPU,				\
88 	},							\
89 }
90 
91 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits)				\
92 	static const struct iio_chan_spec _name ## _channels[] = {	\
93 		ADCxx1C_CHAN((_bits)),					\
94 		IIO_CHAN_SOFT_TIMESTAMP(1),				\
95 	};								\
96 
97 #define ADC081C_NUM_CHANNELS 2
98 
99 struct adcxx1c_model {
100 	const struct iio_chan_spec* channels;
101 	int bits;
102 };
103 
104 #define ADCxx1C_MODEL(_name, _bits)					\
105 	{								\
106 		.channels = _name ## _channels,				\
107 		.bits = (_bits),					\
108 	}
109 
110 DEFINE_ADCxx1C_CHANNELS(adc081c,  8);
111 DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
112 DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
113 
114 /* Model ids are indexes in _models array */
115 enum adcxx1c_model_id {
116 	ADC081C = 0,
117 	ADC101C = 1,
118 	ADC121C = 2,
119 };
120 
121 static struct adcxx1c_model adcxx1c_models[] = {
122 	ADCxx1C_MODEL(adc081c,  8),
123 	ADCxx1C_MODEL(adc101c, 10),
124 	ADCxx1C_MODEL(adc121c, 12),
125 };
126 
127 static const struct iio_info adc081c_info = {
128 	.read_raw = adc081c_read_raw,
129 };
130 
131 static irqreturn_t adc081c_trigger_handler(int irq, void *p)
132 {
133 	struct iio_poll_func *pf = p;
134 	struct iio_dev *indio_dev = pf->indio_dev;
135 	struct adc081c *data = iio_priv(indio_dev);
136 	int ret;
137 
138 	ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
139 	if (ret < 0)
140 		goto out;
141 	data->scan.channel = ret;
142 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
143 					   iio_get_time_ns(indio_dev));
144 out:
145 	iio_trigger_notify_done(indio_dev->trig);
146 	return IRQ_HANDLED;
147 }
148 
149 static int adc081c_probe(struct i2c_client *client,
150 			 const struct i2c_device_id *id)
151 {
152 	struct iio_dev *iio;
153 	struct adc081c *adc;
154 	struct adcxx1c_model *model;
155 	int err;
156 
157 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
158 		return -EOPNOTSUPP;
159 
160 	model = &adcxx1c_models[id->driver_data];
161 
162 	iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
163 	if (!iio)
164 		return -ENOMEM;
165 
166 	adc = iio_priv(iio);
167 	adc->i2c = client;
168 	adc->bits = model->bits;
169 
170 	adc->ref = devm_regulator_get(&client->dev, "vref");
171 	if (IS_ERR(adc->ref))
172 		return PTR_ERR(adc->ref);
173 
174 	err = regulator_enable(adc->ref);
175 	if (err < 0)
176 		return err;
177 
178 	iio->name = dev_name(&client->dev);
179 	iio->modes = INDIO_DIRECT_MODE;
180 	iio->info = &adc081c_info;
181 
182 	iio->channels = model->channels;
183 	iio->num_channels = ADC081C_NUM_CHANNELS;
184 
185 	err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
186 	if (err < 0) {
187 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
188 		goto err_regulator_disable;
189 	}
190 
191 	err = iio_device_register(iio);
192 	if (err < 0)
193 		goto err_buffer_cleanup;
194 
195 	i2c_set_clientdata(client, iio);
196 
197 	return 0;
198 
199 err_buffer_cleanup:
200 	iio_triggered_buffer_cleanup(iio);
201 err_regulator_disable:
202 	regulator_disable(adc->ref);
203 
204 	return err;
205 }
206 
207 static int adc081c_remove(struct i2c_client *client)
208 {
209 	struct iio_dev *iio = i2c_get_clientdata(client);
210 	struct adc081c *adc = iio_priv(iio);
211 
212 	iio_device_unregister(iio);
213 	iio_triggered_buffer_cleanup(iio);
214 	regulator_disable(adc->ref);
215 
216 	return 0;
217 }
218 
219 static const struct i2c_device_id adc081c_id[] = {
220 	{ "adc081c", ADC081C },
221 	{ "adc101c", ADC101C },
222 	{ "adc121c", ADC121C },
223 	{ }
224 };
225 MODULE_DEVICE_TABLE(i2c, adc081c_id);
226 
227 static const struct of_device_id adc081c_of_match[] = {
228 	{ .compatible = "ti,adc081c" },
229 	{ .compatible = "ti,adc101c" },
230 	{ .compatible = "ti,adc121c" },
231 	{ }
232 };
233 MODULE_DEVICE_TABLE(of, adc081c_of_match);
234 
235 static struct i2c_driver adc081c_driver = {
236 	.driver = {
237 		.name = "adc081c",
238 		.of_match_table = adc081c_of_match,
239 	},
240 	.probe = adc081c_probe,
241 	.remove = adc081c_remove,
242 	.id_table = adc081c_id,
243 };
244 module_i2c_driver(adc081c_driver);
245 
246 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
247 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
248 MODULE_LICENSE("GPL v2");
249