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_mark_last_busy(&data->client->dev);
109 	pm_runtime_put_autosuspend(&data->client->dev);
110 
111 	if (ret < 0) {
112 		dev_err(&data->client->dev, "I2C read fail: %d\n", ret);
113 		return ret;
114 	}
115 
116 	/* Value is always 16-bit 2's complement */
117 	value = be16_to_cpu(buffer);
118 
119 	/* Shift result to compensate for bit resolution vs. sample rate */
120 	value <<= 16 - ads1100_data_bits(data);
121 
122 	*val = sign_extend32(value, 15);
123 
124 	return 0;
125 }
126 
127 static int ads1100_set_scale(struct ads1100_data *data, int val, int val2)
128 {
129 	int microvolts;
130 	int gain;
131 
132 	/* With Vdd between 2.7 and 5V, the scale is always below 1 */
133 	if (val)
134 		return -EINVAL;
135 
136 	if (!val2)
137 		return -EINVAL;
138 
139 	microvolts = regulator_get_voltage(data->reg_vdd);
140 	/*
141 	 * val2 is in 'micro' units, n = val2 / 1000000
142 	 * result must be millivolts, d = microvolts / 1000
143 	 * the full-scale value is d/n, corresponds to 2^15,
144 	 * hence the gain = (d / n) >> 15, factoring out the 1000 and moving the
145 	 * bitshift so everything fits in 32-bits yields this formula.
146 	 */
147 	gain = DIV_ROUND_CLOSEST(microvolts, BIT(15)) * MILLI / val2;
148 	if (gain < BIT(0) || gain > BIT(3))
149 		return -EINVAL;
150 
151 	ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain) - 1);
152 
153 	return 0;
154 }
155 
156 static int ads1100_set_data_rate(struct ads1100_data *data, int chan, int rate)
157 {
158 	unsigned int i;
159 	unsigned int size;
160 
161 	size = data->supports_data_rate ? ARRAY_SIZE(ads1100_data_rate) : 1;
162 	for (i = 0; i < size; i++) {
163 		if (ads1100_data_rate[i] == rate)
164 			return ads1100_set_config_bits(data, ADS1100_DR_MASK,
165 						       FIELD_PREP(ADS1100_DR_MASK, i));
166 	}
167 
168 	return -EINVAL;
169 }
170 
171 static int ads1100_get_vdd_millivolts(struct ads1100_data *data)
172 {
173 	return regulator_get_voltage(data->reg_vdd) / (MICRO / MILLI);
174 }
175 
176 static void ads1100_calc_scale_avail(struct ads1100_data *data)
177 {
178 	int millivolts = ads1100_get_vdd_millivolts(data);
179 	unsigned int i;
180 
181 	for (i = 0; i < ARRAY_SIZE(data->scale_avail) / 2; i++) {
182 		data->scale_avail[i * 2 + 0] = millivolts;
183 		data->scale_avail[i * 2 + 1] = 15 + i;
184 	}
185 }
186 
187 static int ads1100_read_avail(struct iio_dev *indio_dev,
188 			      struct iio_chan_spec const *chan,
189 			      const int **vals, int *type, int *length,
190 			      long mask)
191 {
192 	struct ads1100_data *data = iio_priv(indio_dev);
193 
194 	if (chan->type != IIO_VOLTAGE)
195 		return -EINVAL;
196 
197 	switch (mask) {
198 	case IIO_CHAN_INFO_SAMP_FREQ:
199 		*type = IIO_VAL_INT;
200 		*vals = ads1100_data_rate;
201 		if (data->supports_data_rate)
202 			*length = ARRAY_SIZE(ads1100_data_rate);
203 		else
204 			*length = 1;
205 		return IIO_AVAIL_LIST;
206 	case IIO_CHAN_INFO_SCALE:
207 		*type = IIO_VAL_FRACTIONAL_LOG2;
208 		*vals = data->scale_avail;
209 		*length = ARRAY_SIZE(data->scale_avail);
210 		return IIO_AVAIL_LIST;
211 	default:
212 		return -EINVAL;
213 	}
214 }
215 
216 static int ads1100_read_raw(struct iio_dev *indio_dev,
217 			    struct iio_chan_spec const *chan, int *val,
218 			    int *val2, long mask)
219 {
220 	int ret;
221 	struct ads1100_data *data = iio_priv(indio_dev);
222 
223 	guard(mutex)(&data->lock);
224 	switch (mask) {
225 	case IIO_CHAN_INFO_RAW:
226 		if (!iio_device_claim_direct(indio_dev))
227 			return -EBUSY;
228 
229 		ret = ads1100_get_adc_result(data, chan->address, val);
230 		iio_device_release_direct(indio_dev);
231 		if (ret < 0)
232 			return ret;
233 
234 		return IIO_VAL_INT;
235 	case IIO_CHAN_INFO_SCALE:
236 		/* full-scale is the supply voltage in millivolts */
237 		*val = ads1100_get_vdd_millivolts(data);
238 		*val2 = 15 + FIELD_GET(ADS1100_PGA_MASK, data->config);
239 		return IIO_VAL_FRACTIONAL_LOG2;
240 	case IIO_CHAN_INFO_SAMP_FREQ:
241 		*val = ads1100_data_rate[FIELD_GET(ADS1100_DR_MASK,
242 						   data->config)];
243 		return IIO_VAL_INT;
244 	default:
245 		return -EINVAL;
246 	}
247 }
248 
249 static int ads1100_write_raw(struct iio_dev *indio_dev,
250 			     struct iio_chan_spec const *chan, int val,
251 			     int val2, long mask)
252 {
253 	struct ads1100_data *data = iio_priv(indio_dev);
254 
255 	guard(mutex)(&data->lock);
256 	switch (mask) {
257 	case IIO_CHAN_INFO_SCALE:
258 		return ads1100_set_scale(data, val, val2);
259 	case IIO_CHAN_INFO_SAMP_FREQ:
260 		return ads1100_set_data_rate(data, chan->address, val);
261 	default:
262 		return -EINVAL;
263 	}
264 }
265 
266 static const struct iio_info ads1100_info = {
267 	.read_avail = ads1100_read_avail,
268 	.read_raw = ads1100_read_raw,
269 	.write_raw = ads1100_write_raw,
270 };
271 
272 static int ads1100_setup(struct ads1100_data *data)
273 {
274 	int ret;
275 	u8 buffer[3];
276 
277 	/* Setup continuous sampling mode at 8sps */
278 	buffer[0] = ADS1100_DR_MASK | ADS1100_CONTINUOUS;
279 	ret = i2c_master_send(data->client, buffer, 1);
280 	if (ret < 0)
281 		return ret;
282 
283 	ret = i2c_master_recv(data->client, buffer, sizeof(buffer));
284 	if (ret < 0)
285 		return ret;
286 
287 	/* Config register returned in third byte, strip away the busy status */
288 	data->config = buffer[2] & ~ADS1100_CFG_ST_BSY;
289 
290 	/* Detect the sample rate capability by checking the DR bits */
291 	data->supports_data_rate = FIELD_GET(ADS1100_DR_MASK, buffer[2]) != 0;
292 
293 	return 0;
294 }
295 
296 static void ads1100_reg_disable(void *reg)
297 {
298 	regulator_disable(reg);
299 }
300 
301 static void ads1100_disable_continuous(void *data)
302 {
303 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
304 }
305 
306 static int ads1100_probe(struct i2c_client *client)
307 {
308 	struct iio_dev *indio_dev;
309 	struct ads1100_data *data;
310 	struct device *dev = &client->dev;
311 	int ret;
312 
313 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
314 	if (!indio_dev)
315 		return -ENOMEM;
316 
317 	data = iio_priv(indio_dev);
318 	dev_set_drvdata(dev, data);
319 	data->client = client;
320 	mutex_init(&data->lock);
321 
322 	indio_dev->name = "ads1100";
323 	indio_dev->modes = INDIO_DIRECT_MODE;
324 	indio_dev->channels = &ads1100_channel;
325 	indio_dev->num_channels = 1;
326 	indio_dev->info = &ads1100_info;
327 
328 	data->reg_vdd = devm_regulator_get(dev, "vdd");
329 	if (IS_ERR(data->reg_vdd))
330 		return dev_err_probe(dev, PTR_ERR(data->reg_vdd),
331 				     "Failed to get vdd regulator\n");
332 
333 	ret = regulator_enable(data->reg_vdd);
334 	if (ret < 0)
335 		return dev_err_probe(dev, ret,
336 				     "Failed to enable vdd regulator\n");
337 
338 	ret = devm_add_action_or_reset(dev, ads1100_reg_disable, data->reg_vdd);
339 	if (ret)
340 		return ret;
341 
342 	ret = ads1100_setup(data);
343 	if (ret)
344 		return dev_err_probe(dev, ret,
345 				     "Failed to communicate with device\n");
346 
347 	ret = devm_add_action_or_reset(dev, ads1100_disable_continuous, data);
348 	if (ret)
349 		return ret;
350 
351 	ads1100_calc_scale_avail(data);
352 
353 	pm_runtime_set_autosuspend_delay(dev, ADS1100_SLEEP_DELAY_MS);
354 	pm_runtime_use_autosuspend(dev);
355 	pm_runtime_set_active(dev);
356 	ret = devm_pm_runtime_enable(dev);
357 	if (ret)
358 		return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
359 
360 	ret = devm_iio_device_register(dev, indio_dev);
361 	if (ret)
362 		return dev_err_probe(dev, ret,
363 				     "Failed to register IIO device\n");
364 
365 	return 0;
366 }
367 
368 static int ads1100_runtime_suspend(struct device *dev)
369 {
370 	struct ads1100_data *data = dev_get_drvdata(dev);
371 
372 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
373 	regulator_disable(data->reg_vdd);
374 
375 	return 0;
376 }
377 
378 static int ads1100_runtime_resume(struct device *dev)
379 {
380 	struct ads1100_data *data = dev_get_drvdata(dev);
381 	int ret;
382 
383 	ret = regulator_enable(data->reg_vdd);
384 	if (ret) {
385 		dev_err(&data->client->dev, "Failed to enable Vdd\n");
386 		return ret;
387 	}
388 
389 	/*
390 	 * We'll always change the mode bit in the config register, so there is
391 	 * no need here to "force" a write to the config register. If the device
392 	 * has been power-cycled, we'll re-write its config register now.
393 	 */
394 	return ads1100_set_config_bits(data, ADS1100_CFG_SC,
395 				       ADS1100_CONTINUOUS);
396 }
397 
398 static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops,
399 				 ads1100_runtime_suspend,
400 				 ads1100_runtime_resume,
401 				 NULL);
402 
403 static const struct i2c_device_id ads1100_id[] = {
404 	{ "ads1100" },
405 	{ "ads1000" },
406 	{ }
407 };
408 
409 MODULE_DEVICE_TABLE(i2c, ads1100_id);
410 
411 static const struct of_device_id ads1100_of_match[] = {
412 	{.compatible = "ti,ads1100" },
413 	{.compatible = "ti,ads1000" },
414 	{ }
415 };
416 
417 MODULE_DEVICE_TABLE(of, ads1100_of_match);
418 
419 static struct i2c_driver ads1100_driver = {
420 	.driver = {
421 		   .name = "ads1100",
422 		   .of_match_table = ads1100_of_match,
423 		   .pm = pm_ptr(&ads1100_pm_ops),
424 	},
425 	.probe = ads1100_probe,
426 	.id_table = ads1100_id,
427 };
428 
429 module_i2c_driver(ads1100_driver);
430 
431 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
432 MODULE_DESCRIPTION("Texas Instruments ADS1100 ADC driver");
433 MODULE_LICENSE("GPL");
434