xref: /openbmc/linux/drivers/iio/adc/ti-ads1100.c (revision c4c3c32d)
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/delay.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/mutex.h>
18 #include <linux/property.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/units.h>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/types.h>
25 
26 /* The ADS1100 has a single byte config register */
27 
28 /* Conversion in progress bit */
29 #define ADS1100_CFG_ST_BSY	BIT(7)
30 /* Single conversion bit */
31 #define ADS1100_CFG_SC		BIT(4)
32 /* Data rate */
33 #define ADS1100_DR_MASK		GENMASK(3, 2)
34 /* Gain */
35 #define ADS1100_PGA_MASK	GENMASK(1, 0)
36 
37 #define ADS1100_CONTINUOUS	0
38 #define	ADS1100_SINGLESHOT	ADS1100_CFG_SC
39 
40 #define ADS1100_SLEEP_DELAY_MS	2000
41 
42 static const int ads1100_data_rate[] = { 128, 32, 16, 8 };
43 static const int ads1100_data_rate_bits[] = { 12, 14, 15, 16 };
44 
45 struct ads1100_data {
46 	struct i2c_client *client;
47 	struct regulator *reg_vdd;
48 	struct mutex lock;
49 	int scale_avail[2 * 4]; /* 4 gain settings */
50 	u8 config;
51 	bool supports_data_rate; /* Only the ADS1100 can select the rate */
52 };
53 
54 static const struct iio_chan_spec ads1100_channel = {
55 	.type = IIO_VOLTAGE,
56 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
57 	.info_mask_shared_by_all =
58 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
59 	.info_mask_shared_by_all_available =
60 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
61 	.scan_type = {
62 		      .sign = 's',
63 		      .realbits = 16,
64 		      .storagebits = 16,
65 		      .endianness = IIO_CPU,
66 		       },
67 	.datasheet_name = "AIN",
68 };
69 
70 static int ads1100_set_config_bits(struct ads1100_data *data, u8 mask, u8 value)
71 {
72 	int ret;
73 	u8 config = (data->config & ~mask) | (value & mask);
74 
75 	if (data->config == config)
76 		return 0;	/* Already done */
77 
78 	ret = i2c_master_send(data->client, &config, 1);
79 	if (ret < 0)
80 		return ret;
81 
82 	data->config = config;
83 
84 	return 0;
85 };
86 
87 static int ads1100_data_bits(struct ads1100_data *data)
88 {
89 	return ads1100_data_rate_bits[FIELD_GET(ADS1100_DR_MASK, data->config)];
90 }
91 
92 static int ads1100_get_adc_result(struct ads1100_data *data, int chan, int *val)
93 {
94 	int ret;
95 	__be16 buffer;
96 	s16 value;
97 
98 	if (chan != 0)
99 		return -EINVAL;
100 
101 	ret = pm_runtime_resume_and_get(&data->client->dev);
102 	if (ret < 0)
103 		return ret;
104 
105 	ret = i2c_master_recv(data->client, (char *)&buffer, sizeof(buffer));
106 
107 	pm_runtime_mark_last_busy(&data->client->dev);
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 	mutex_lock(&data->lock);
223 	switch (mask) {
224 	case IIO_CHAN_INFO_RAW:
225 		ret = iio_device_claim_direct_mode(indio_dev);
226 		if (ret)
227 			break;
228 
229 		ret = ads1100_get_adc_result(data, chan->address, val);
230 		if (ret >= 0)
231 			ret = IIO_VAL_INT;
232 		iio_device_release_direct_mode(indio_dev);
233 		break;
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 		ret = IIO_VAL_FRACTIONAL_LOG2;
239 		break;
240 	case IIO_CHAN_INFO_SAMP_FREQ:
241 		*val = ads1100_data_rate[FIELD_GET(ADS1100_DR_MASK,
242 						   data->config)];
243 		ret = IIO_VAL_INT;
244 		break;
245 	default:
246 		ret = -EINVAL;
247 		break;
248 	}
249 	mutex_unlock(&data->lock);
250 
251 	return ret;
252 }
253 
254 static int ads1100_write_raw(struct iio_dev *indio_dev,
255 			     struct iio_chan_spec const *chan, int val,
256 			     int val2, long mask)
257 {
258 	struct ads1100_data *data = iio_priv(indio_dev);
259 	int ret;
260 
261 	mutex_lock(&data->lock);
262 	switch (mask) {
263 	case IIO_CHAN_INFO_SCALE:
264 		ret = ads1100_set_scale(data, val, val2);
265 		break;
266 	case IIO_CHAN_INFO_SAMP_FREQ:
267 		ret = ads1100_set_data_rate(data, chan->address, val);
268 		break;
269 	default:
270 		ret = -EINVAL;
271 		break;
272 	}
273 	mutex_unlock(&data->lock);
274 
275 	return ret;
276 }
277 
278 static const struct iio_info ads1100_info = {
279 	.read_avail = ads1100_read_avail,
280 	.read_raw = ads1100_read_raw,
281 	.write_raw = ads1100_write_raw,
282 };
283 
284 static int ads1100_setup(struct ads1100_data *data)
285 {
286 	int ret;
287 	u8 buffer[3];
288 
289 	/* Setup continuous sampling mode at 8sps */
290 	buffer[0] = ADS1100_DR_MASK | ADS1100_CONTINUOUS;
291 	ret = i2c_master_send(data->client, buffer, 1);
292 	if (ret < 0)
293 		return ret;
294 
295 	ret = i2c_master_recv(data->client, buffer, sizeof(buffer));
296 	if (ret < 0)
297 		return ret;
298 
299 	/* Config register returned in third byte, strip away the busy status */
300 	data->config = buffer[2] & ~ADS1100_CFG_ST_BSY;
301 
302 	/* Detect the sample rate capability by checking the DR bits */
303 	data->supports_data_rate = FIELD_GET(ADS1100_DR_MASK, buffer[2]) != 0;
304 
305 	return 0;
306 }
307 
308 static void ads1100_reg_disable(void *reg)
309 {
310 	regulator_disable(reg);
311 }
312 
313 static void ads1100_disable_continuous(void *data)
314 {
315 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
316 }
317 
318 static int ads1100_probe(struct i2c_client *client)
319 {
320 	struct iio_dev *indio_dev;
321 	struct ads1100_data *data;
322 	struct device *dev = &client->dev;
323 	int ret;
324 
325 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
326 	if (!indio_dev)
327 		return -ENOMEM;
328 
329 	data = iio_priv(indio_dev);
330 	dev_set_drvdata(dev, data);
331 	data->client = client;
332 	mutex_init(&data->lock);
333 
334 	indio_dev->name = "ads1100";
335 	indio_dev->modes = INDIO_DIRECT_MODE;
336 	indio_dev->channels = &ads1100_channel;
337 	indio_dev->num_channels = 1;
338 	indio_dev->info = &ads1100_info;
339 
340 	data->reg_vdd = devm_regulator_get(dev, "vdd");
341 	if (IS_ERR(data->reg_vdd))
342 		return dev_err_probe(dev, PTR_ERR(data->reg_vdd),
343 				     "Failed to get vdd regulator\n");
344 
345 	ret = regulator_enable(data->reg_vdd);
346 	if (ret < 0)
347 		return dev_err_probe(dev, ret,
348 				     "Failed to enable vdd regulator\n");
349 
350 	ret = devm_add_action_or_reset(dev, ads1100_reg_disable, data->reg_vdd);
351 	if (ret)
352 		return ret;
353 
354 	ret = ads1100_setup(data);
355 	if (ret)
356 		return dev_err_probe(dev, ret,
357 				     "Failed to communicate with device\n");
358 
359 	ret = devm_add_action_or_reset(dev, ads1100_disable_continuous, data);
360 	if (ret)
361 		return ret;
362 
363 	ads1100_calc_scale_avail(data);
364 
365 	pm_runtime_set_autosuspend_delay(dev, ADS1100_SLEEP_DELAY_MS);
366 	pm_runtime_use_autosuspend(dev);
367 	pm_runtime_set_active(dev);
368 	ret = devm_pm_runtime_enable(dev);
369 	if (ret)
370 		return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
371 
372 	ret = devm_iio_device_register(dev, indio_dev);
373 	if (ret)
374 		return dev_err_probe(dev, ret,
375 				     "Failed to register IIO device\n");
376 
377 	return 0;
378 }
379 
380 static int ads1100_runtime_suspend(struct device *dev)
381 {
382 	struct ads1100_data *data = dev_get_drvdata(dev);
383 
384 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
385 	regulator_disable(data->reg_vdd);
386 
387 	return 0;
388 }
389 
390 static int ads1100_runtime_resume(struct device *dev)
391 {
392 	struct ads1100_data *data = dev_get_drvdata(dev);
393 	int ret;
394 
395 	ret = regulator_enable(data->reg_vdd);
396 	if (ret) {
397 		dev_err(&data->client->dev, "Failed to enable Vdd\n");
398 		return ret;
399 	}
400 
401 	/*
402 	 * We'll always change the mode bit in the config register, so there is
403 	 * no need here to "force" a write to the config register. If the device
404 	 * has been power-cycled, we'll re-write its config register now.
405 	 */
406 	return ads1100_set_config_bits(data, ADS1100_CFG_SC,
407 				       ADS1100_CONTINUOUS);
408 }
409 
410 static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops,
411 				 ads1100_runtime_suspend,
412 				 ads1100_runtime_resume,
413 				 NULL);
414 
415 static const struct i2c_device_id ads1100_id[] = {
416 	{ "ads1100" },
417 	{ "ads1000" },
418 	{ }
419 };
420 
421 MODULE_DEVICE_TABLE(i2c, ads1100_id);
422 
423 static const struct of_device_id ads1100_of_match[] = {
424 	{.compatible = "ti,ads1100" },
425 	{.compatible = "ti,ads1000" },
426 	{ }
427 };
428 
429 MODULE_DEVICE_TABLE(of, ads1100_of_match);
430 
431 static struct i2c_driver ads1100_driver = {
432 	.driver = {
433 		   .name = "ads1100",
434 		   .of_match_table = ads1100_of_match,
435 		   .pm = pm_ptr(&ads1100_pm_ops),
436 	},
437 	.probe = ads1100_probe,
438 	.id_table = ads1100_id,
439 };
440 
441 module_i2c_driver(ads1100_driver);
442 
443 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
444 MODULE_DESCRIPTION("Texas Instruments ADS1100 ADC driver");
445 MODULE_LICENSE("GPL");
446