xref: /linux/drivers/iio/adc/ti-ads1100.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADS1100 - Texas Instruments Analog-to-Digital Converter
4  *
5  * Copyright (c) 2023, Topic Embedded Products
6  *
7  * Datasheet: https://www.ti.com/lit/gpn/ads1100
8  * IIO driver for ADS1100 and ADS1000 ADC 16-bit I2C
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/bits.h>
13 #include <linux/cleanup.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <linux/property.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/units.h>
23 
24 #include <linux/iio/iio.h>
25 #include <linux/iio/types.h>
26 
27 /* The ADS1100 has a single byte config register */
28 
29 /* Conversion in progress bit */
30 #define ADS1100_CFG_ST_BSY	BIT(7)
31 /* Single conversion bit */
32 #define ADS1100_CFG_SC		BIT(4)
33 /* Data rate */
34 #define ADS1100_DR_MASK		GENMASK(3, 2)
35 /* Gain */
36 #define ADS1100_PGA_MASK	GENMASK(1, 0)
37 
38 #define ADS1100_CONTINUOUS	0
39 #define	ADS1100_SINGLESHOT	ADS1100_CFG_SC
40 
41 #define ADS1100_SLEEP_DELAY_MS	2000
42 
43 static const int ads1100_data_rate[] = { 128, 32, 16, 8 };
44 static const int ads1100_data_rate_bits[] = { 12, 14, 15, 16 };
45 
46 struct ads1100_data {
47 	struct i2c_client *client;
48 	struct regulator *reg_vdd;
49 	struct mutex lock;
50 	int scale_avail[2 * 4]; /* 4 gain settings */
51 	u8 config;
52 	bool supports_data_rate; /* Only the ADS1100 can select the rate */
53 };
54 
55 static const struct iio_chan_spec ads1100_channel = {
56 	.type = IIO_VOLTAGE,
57 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
58 	.info_mask_shared_by_all =
59 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
60 	.info_mask_shared_by_all_available =
61 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
62 	.scan_type = {
63 		      .sign = 's',
64 		      .realbits = 16,
65 		      .storagebits = 16,
66 		      .endianness = IIO_CPU,
67 		       },
68 	.datasheet_name = "AIN",
69 };
70 
71 static int ads1100_set_config_bits(struct ads1100_data *data, u8 mask, u8 value)
72 {
73 	int ret;
74 	u8 config = (data->config & ~mask) | (value & mask);
75 
76 	if (data->config == config)
77 		return 0;	/* Already done */
78 
79 	ret = i2c_master_send(data->client, &config, 1);
80 	if (ret < 0)
81 		return ret;
82 
83 	data->config = config;
84 
85 	return 0;
86 };
87 
88 static int ads1100_data_bits(struct ads1100_data *data)
89 {
90 	return ads1100_data_rate_bits[FIELD_GET(ADS1100_DR_MASK, data->config)];
91 }
92 
93 static int ads1100_get_adc_result(struct ads1100_data *data, int chan, int *val)
94 {
95 	int ret;
96 	__be16 buffer;
97 	s16 value;
98 
99 	if (chan != 0)
100 		return -EINVAL;
101 
102 	ret = pm_runtime_resume_and_get(&data->client->dev);
103 	if (ret < 0)
104 		return ret;
105 
106 	ret = i2c_master_recv(data->client, (char *)&buffer, sizeof(buffer));
107 
108 	pm_runtime_put_autosuspend(&data->client->dev);
109 
110 	if (ret < 0) {
111 		dev_err(&data->client->dev, "I2C read fail: %d\n", ret);
112 		return ret;
113 	}
114 
115 	/* Value is always 16-bit 2's complement */
116 	value = be16_to_cpu(buffer);
117 
118 	/* Shift result to compensate for bit resolution vs. sample rate */
119 	value <<= 16 - ads1100_data_bits(data);
120 
121 	*val = sign_extend32(value, 15);
122 
123 	return 0;
124 }
125 
126 static int ads1100_set_scale(struct ads1100_data *data, int val, int val2)
127 {
128 	int microvolts;
129 	int gain;
130 
131 	/* With Vdd between 2.7 and 5V, the scale is always below 1 */
132 	if (val)
133 		return -EINVAL;
134 
135 	if (!val2)
136 		return -EINVAL;
137 
138 	microvolts = regulator_get_voltage(data->reg_vdd);
139 	/*
140 	 * val2 is in 'micro' units, n = val2 / 1000000
141 	 * result must be millivolts, d = microvolts / 1000
142 	 * the full-scale value is d/n, corresponds to 2^15,
143 	 * hence the gain = (d / n) >> 15, factoring out the 1000 and moving the
144 	 * bitshift so everything fits in 32-bits yields this formula.
145 	 */
146 	gain = DIV_ROUND_CLOSEST(microvolts, BIT(15)) * MILLI / val2;
147 	if (gain < BIT(0) || gain > BIT(3))
148 		return -EINVAL;
149 
150 	ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain) - 1);
151 
152 	return 0;
153 }
154 
155 static int ads1100_set_data_rate(struct ads1100_data *data, int chan, int rate)
156 {
157 	unsigned int i;
158 	unsigned int size;
159 
160 	size = data->supports_data_rate ? ARRAY_SIZE(ads1100_data_rate) : 1;
161 	for (i = 0; i < size; i++) {
162 		if (ads1100_data_rate[i] == rate)
163 			return ads1100_set_config_bits(data, ADS1100_DR_MASK,
164 						       FIELD_PREP(ADS1100_DR_MASK, i));
165 	}
166 
167 	return -EINVAL;
168 }
169 
170 static int ads1100_get_vdd_millivolts(struct ads1100_data *data)
171 {
172 	return regulator_get_voltage(data->reg_vdd) / (MICRO / MILLI);
173 }
174 
175 static void ads1100_calc_scale_avail(struct ads1100_data *data)
176 {
177 	int millivolts = ads1100_get_vdd_millivolts(data);
178 	unsigned int i;
179 
180 	for (i = 0; i < ARRAY_SIZE(data->scale_avail) / 2; i++) {
181 		data->scale_avail[i * 2 + 0] = millivolts;
182 		data->scale_avail[i * 2 + 1] = 15 + i;
183 	}
184 }
185 
186 static int ads1100_read_avail(struct iio_dev *indio_dev,
187 			      struct iio_chan_spec const *chan,
188 			      const int **vals, int *type, int *length,
189 			      long mask)
190 {
191 	struct ads1100_data *data = iio_priv(indio_dev);
192 
193 	if (chan->type != IIO_VOLTAGE)
194 		return -EINVAL;
195 
196 	switch (mask) {
197 	case IIO_CHAN_INFO_SAMP_FREQ:
198 		*type = IIO_VAL_INT;
199 		*vals = ads1100_data_rate;
200 		if (data->supports_data_rate)
201 			*length = ARRAY_SIZE(ads1100_data_rate);
202 		else
203 			*length = 1;
204 		return IIO_AVAIL_LIST;
205 	case IIO_CHAN_INFO_SCALE:
206 		*type = IIO_VAL_FRACTIONAL_LOG2;
207 		*vals = data->scale_avail;
208 		*length = ARRAY_SIZE(data->scale_avail);
209 		return IIO_AVAIL_LIST;
210 	default:
211 		return -EINVAL;
212 	}
213 }
214 
215 static int ads1100_read_raw(struct iio_dev *indio_dev,
216 			    struct iio_chan_spec const *chan, int *val,
217 			    int *val2, long mask)
218 {
219 	int ret;
220 	struct ads1100_data *data = iio_priv(indio_dev);
221 
222 	guard(mutex)(&data->lock);
223 	switch (mask) {
224 	case IIO_CHAN_INFO_RAW:
225 		if (!iio_device_claim_direct(indio_dev))
226 			return -EBUSY;
227 
228 		ret = ads1100_get_adc_result(data, chan->address, val);
229 		iio_device_release_direct(indio_dev);
230 		if (ret < 0)
231 			return ret;
232 
233 		return IIO_VAL_INT;
234 	case IIO_CHAN_INFO_SCALE:
235 		/* full-scale is the supply voltage in millivolts */
236 		*val = ads1100_get_vdd_millivolts(data);
237 		*val2 = 15 + FIELD_GET(ADS1100_PGA_MASK, data->config);
238 		return IIO_VAL_FRACTIONAL_LOG2;
239 	case IIO_CHAN_INFO_SAMP_FREQ:
240 		*val = ads1100_data_rate[FIELD_GET(ADS1100_DR_MASK,
241 						   data->config)];
242 		return IIO_VAL_INT;
243 	default:
244 		return -EINVAL;
245 	}
246 }
247 
248 static int ads1100_write_raw(struct iio_dev *indio_dev,
249 			     struct iio_chan_spec const *chan, int val,
250 			     int val2, long mask)
251 {
252 	struct ads1100_data *data = iio_priv(indio_dev);
253 
254 	guard(mutex)(&data->lock);
255 	switch (mask) {
256 	case IIO_CHAN_INFO_SCALE:
257 		return ads1100_set_scale(data, val, val2);
258 	case IIO_CHAN_INFO_SAMP_FREQ:
259 		return ads1100_set_data_rate(data, chan->address, val);
260 	default:
261 		return -EINVAL;
262 	}
263 }
264 
265 static const struct iio_info ads1100_info = {
266 	.read_avail = ads1100_read_avail,
267 	.read_raw = ads1100_read_raw,
268 	.write_raw = ads1100_write_raw,
269 };
270 
271 static int ads1100_setup(struct ads1100_data *data)
272 {
273 	int ret;
274 	u8 buffer[3];
275 
276 	/* Setup continuous sampling mode at 8sps */
277 	buffer[0] = ADS1100_DR_MASK | ADS1100_CONTINUOUS;
278 	ret = i2c_master_send(data->client, buffer, 1);
279 	if (ret < 0)
280 		return ret;
281 
282 	ret = i2c_master_recv(data->client, buffer, sizeof(buffer));
283 	if (ret < 0)
284 		return ret;
285 
286 	/* Config register returned in third byte, strip away the busy status */
287 	data->config = buffer[2] & ~ADS1100_CFG_ST_BSY;
288 
289 	/* Detect the sample rate capability by checking the DR bits */
290 	data->supports_data_rate = FIELD_GET(ADS1100_DR_MASK, buffer[2]) != 0;
291 
292 	return 0;
293 }
294 
295 static void ads1100_reg_disable(void *reg)
296 {
297 	regulator_disable(reg);
298 }
299 
300 static void ads1100_disable_continuous(void *data)
301 {
302 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
303 }
304 
305 static int ads1100_probe(struct i2c_client *client)
306 {
307 	struct iio_dev *indio_dev;
308 	struct ads1100_data *data;
309 	struct device *dev = &client->dev;
310 	int ret;
311 
312 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
313 	if (!indio_dev)
314 		return -ENOMEM;
315 
316 	data = iio_priv(indio_dev);
317 	dev_set_drvdata(dev, data);
318 	data->client = client;
319 	mutex_init(&data->lock);
320 
321 	indio_dev->name = "ads1100";
322 	indio_dev->modes = INDIO_DIRECT_MODE;
323 	indio_dev->channels = &ads1100_channel;
324 	indio_dev->num_channels = 1;
325 	indio_dev->info = &ads1100_info;
326 
327 	data->reg_vdd = devm_regulator_get(dev, "vdd");
328 	if (IS_ERR(data->reg_vdd))
329 		return dev_err_probe(dev, PTR_ERR(data->reg_vdd),
330 				     "Failed to get vdd regulator\n");
331 
332 	ret = regulator_enable(data->reg_vdd);
333 	if (ret < 0)
334 		return dev_err_probe(dev, ret,
335 				     "Failed to enable vdd regulator\n");
336 
337 	ret = devm_add_action_or_reset(dev, ads1100_reg_disable, data->reg_vdd);
338 	if (ret)
339 		return ret;
340 
341 	ret = ads1100_setup(data);
342 	if (ret)
343 		return dev_err_probe(dev, ret,
344 				     "Failed to communicate with device\n");
345 
346 	ret = devm_add_action_or_reset(dev, ads1100_disable_continuous, data);
347 	if (ret)
348 		return ret;
349 
350 	ads1100_calc_scale_avail(data);
351 
352 	pm_runtime_set_autosuspend_delay(dev, ADS1100_SLEEP_DELAY_MS);
353 	pm_runtime_use_autosuspend(dev);
354 	pm_runtime_set_active(dev);
355 	ret = devm_pm_runtime_enable(dev);
356 	if (ret)
357 		return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
358 
359 	ret = devm_iio_device_register(dev, indio_dev);
360 	if (ret)
361 		return dev_err_probe(dev, ret,
362 				     "Failed to register IIO device\n");
363 
364 	return 0;
365 }
366 
367 static int ads1100_runtime_suspend(struct device *dev)
368 {
369 	struct ads1100_data *data = dev_get_drvdata(dev);
370 
371 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
372 	regulator_disable(data->reg_vdd);
373 
374 	return 0;
375 }
376 
377 static int ads1100_runtime_resume(struct device *dev)
378 {
379 	struct ads1100_data *data = dev_get_drvdata(dev);
380 	int ret;
381 
382 	ret = regulator_enable(data->reg_vdd);
383 	if (ret) {
384 		dev_err(&data->client->dev, "Failed to enable Vdd\n");
385 		return ret;
386 	}
387 
388 	/*
389 	 * We'll always change the mode bit in the config register, so there is
390 	 * no need here to "force" a write to the config register. If the device
391 	 * has been power-cycled, we'll re-write its config register now.
392 	 */
393 	return ads1100_set_config_bits(data, ADS1100_CFG_SC,
394 				       ADS1100_CONTINUOUS);
395 }
396 
397 static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops,
398 				 ads1100_runtime_suspend,
399 				 ads1100_runtime_resume,
400 				 NULL);
401 
402 static const struct i2c_device_id ads1100_id[] = {
403 	{ "ads1100" },
404 	{ "ads1000" },
405 	{ }
406 };
407 
408 MODULE_DEVICE_TABLE(i2c, ads1100_id);
409 
410 static const struct of_device_id ads1100_of_match[] = {
411 	{.compatible = "ti,ads1100" },
412 	{.compatible = "ti,ads1000" },
413 	{ }
414 };
415 
416 MODULE_DEVICE_TABLE(of, ads1100_of_match);
417 
418 static struct i2c_driver ads1100_driver = {
419 	.driver = {
420 		   .name = "ads1100",
421 		   .of_match_table = ads1100_of_match,
422 		   .pm = pm_ptr(&ads1100_pm_ops),
423 	},
424 	.probe = ads1100_probe,
425 	.id_table = ads1100_id,
426 };
427 
428 module_i2c_driver(ads1100_driver);
429 
430 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
431 MODULE_DESCRIPTION("Texas Instruments ADS1100 ADC driver");
432 MODULE_LICENSE("GPL");
433