1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADS1100 - Texas Instruments Analog-to-Digital Converter
5 * Copyright (c) 2023, Topic Embedded Products
7 * Datasheet: https://www.ti.com/lit/gpn/ads1100
8 * IIO driver for ADS1100 and ADS1000 ADC 16-bit I2C
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>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/types.h>
26 /* The ADS1100 has a single byte config register */
28 /* Conversion in progress bit */
29 #define ADS1100_CFG_ST_BSY BIT(7)
30 /* Single conversion bit */
31 #define ADS1100_CFG_SC BIT(4)
33 #define ADS1100_DR_MASK GENMASK(3, 2)
35 #define ADS1100_PGA_MASK GENMASK(1, 0)
37 #define ADS1100_CONTINUOUS 0
38 #define ADS1100_SINGLESHOT ADS1100_CFG_SC
40 #define ADS1100_SLEEP_DELAY_MS 2000
42 static const int ads1100_data_rate
[] = { 128, 32, 16, 8 };
43 static const int ads1100_data_rate_bits
[] = { 12, 14, 15, 16 };
46 struct i2c_client
*client
;
47 struct regulator
*reg_vdd
;
49 int scale_avail
[2 * 4]; /* 4 gain settings */
51 bool supports_data_rate
; /* Only the ADS1100 can select the rate */
54 static const struct iio_chan_spec ads1100_channel
= {
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
),
65 .endianness
= IIO_CPU
,
67 .datasheet_name
= "AIN",
70 static int ads1100_set_config_bits(struct ads1100_data
*data
, u8 mask
, u8 value
)
73 u8 config
= (data
->config
& ~mask
) | (value
& mask
);
75 if (data
->config
== config
)
76 return 0; /* Already done */
78 ret
= i2c_master_send(data
->client
, &config
, 1);
82 data
->config
= config
;
87 static int ads1100_data_bits(struct ads1100_data
*data
)
89 return ads1100_data_rate_bits
[FIELD_GET(ADS1100_DR_MASK
, data
->config
)];
92 static int ads1100_get_adc_result(struct ads1100_data
*data
, int chan
, int *val
)
101 ret
= pm_runtime_resume_and_get(&data
->client
->dev
);
105 ret
= i2c_master_recv(data
->client
, (char *)&buffer
, sizeof(buffer
));
107 pm_runtime_mark_last_busy(&data
->client
->dev
);
108 pm_runtime_put_autosuspend(&data
->client
->dev
);
111 dev_err(&data
->client
->dev
, "I2C read fail: %d\n", ret
);
115 /* Value is always 16-bit 2's complement */
116 value
= be16_to_cpu(buffer
);
118 /* Shift result to compensate for bit resolution vs. sample rate */
119 value
<<= 16 - ads1100_data_bits(data
);
121 *val
= sign_extend32(value
, 15);
126 static int ads1100_set_scale(struct ads1100_data
*data
, int val
, int val2
)
131 /* With Vdd between 2.7 and 5V, the scale is always below 1 */
138 microvolts
= regulator_get_voltage(data
->reg_vdd
);
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.
146 gain
= DIV_ROUND_CLOSEST(microvolts
, BIT(15)) * MILLI
/ val2
;
147 if (gain
< BIT(0) || gain
> BIT(3))
150 ads1100_set_config_bits(data
, ADS1100_PGA_MASK
, ffs(gain
) - 1);
155 static int ads1100_set_data_rate(struct ads1100_data
*data
, int chan
, int rate
)
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
));
170 static int ads1100_get_vdd_millivolts(struct ads1100_data
*data
)
172 return regulator_get_voltage(data
->reg_vdd
) / (MICRO
/ MILLI
);
175 static void ads1100_calc_scale_avail(struct ads1100_data
*data
)
177 int millivolts
= ads1100_get_vdd_millivolts(data
);
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
;
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
,
191 struct ads1100_data
*data
= iio_priv(indio_dev
);
193 if (chan
->type
!= IIO_VOLTAGE
)
197 case IIO_CHAN_INFO_SAMP_FREQ
:
199 *vals
= ads1100_data_rate
;
200 if (data
->supports_data_rate
)
201 *length
= ARRAY_SIZE(ads1100_data_rate
);
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
;
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
)
220 struct ads1100_data
*data
= iio_priv(indio_dev
);
222 mutex_lock(&data
->lock
);
224 case IIO_CHAN_INFO_RAW
:
225 ret
= iio_device_claim_direct_mode(indio_dev
);
229 ret
= ads1100_get_adc_result(data
, chan
->address
, val
);
232 iio_device_release_direct_mode(indio_dev
);
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
;
240 case IIO_CHAN_INFO_SAMP_FREQ
:
241 *val
= ads1100_data_rate
[FIELD_GET(ADS1100_DR_MASK
,
249 mutex_unlock(&data
->lock
);
254 static int ads1100_write_raw(struct iio_dev
*indio_dev
,
255 struct iio_chan_spec
const *chan
, int val
,
258 struct ads1100_data
*data
= iio_priv(indio_dev
);
261 mutex_lock(&data
->lock
);
263 case IIO_CHAN_INFO_SCALE
:
264 ret
= ads1100_set_scale(data
, val
, val2
);
266 case IIO_CHAN_INFO_SAMP_FREQ
:
267 ret
= ads1100_set_data_rate(data
, chan
->address
, val
);
273 mutex_unlock(&data
->lock
);
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
,
284 static int ads1100_setup(struct ads1100_data
*data
)
289 /* Setup continuous sampling mode at 8sps */
290 buffer
[0] = ADS1100_DR_MASK
| ADS1100_CONTINUOUS
;
291 ret
= i2c_master_send(data
->client
, buffer
, 1);
295 ret
= i2c_master_recv(data
->client
, buffer
, sizeof(buffer
));
299 /* Config register returned in third byte, strip away the busy status */
300 data
->config
= buffer
[2] & ~ADS1100_CFG_ST_BSY
;
302 /* Detect the sample rate capability by checking the DR bits */
303 data
->supports_data_rate
= FIELD_GET(ADS1100_DR_MASK
, buffer
[2]) != 0;
308 static void ads1100_reg_disable(void *reg
)
310 regulator_disable(reg
);
313 static void ads1100_disable_continuous(void *data
)
315 ads1100_set_config_bits(data
, ADS1100_CFG_SC
, ADS1100_SINGLESHOT
);
318 static int ads1100_probe(struct i2c_client
*client
)
320 struct iio_dev
*indio_dev
;
321 struct ads1100_data
*data
;
322 struct device
*dev
= &client
->dev
;
325 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
329 data
= iio_priv(indio_dev
);
330 dev_set_drvdata(dev
, data
);
331 data
->client
= client
;
332 mutex_init(&data
->lock
);
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
;
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");
345 ret
= regulator_enable(data
->reg_vdd
);
347 return dev_err_probe(dev
, ret
,
348 "Failed to enable vdd regulator\n");
350 ret
= devm_add_action_or_reset(dev
, ads1100_reg_disable
, data
->reg_vdd
);
354 ret
= ads1100_setup(data
);
356 return dev_err_probe(dev
, ret
,
357 "Failed to communicate with device\n");
359 ret
= devm_add_action_or_reset(dev
, ads1100_disable_continuous
, data
);
363 ads1100_calc_scale_avail(data
);
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
);
370 return dev_err_probe(dev
, ret
, "Failed to enable pm_runtime\n");
372 ret
= devm_iio_device_register(dev
, indio_dev
);
374 return dev_err_probe(dev
, ret
,
375 "Failed to register IIO device\n");
380 static int ads1100_runtime_suspend(struct device
*dev
)
382 struct ads1100_data
*data
= dev_get_drvdata(dev
);
384 ads1100_set_config_bits(data
, ADS1100_CFG_SC
, ADS1100_SINGLESHOT
);
385 regulator_disable(data
->reg_vdd
);
390 static int ads1100_runtime_resume(struct device
*dev
)
392 struct ads1100_data
*data
= dev_get_drvdata(dev
);
395 ret
= regulator_enable(data
->reg_vdd
);
397 dev_err(&data
->client
->dev
, "Failed to enable Vdd\n");
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.
406 return ads1100_set_config_bits(data
, ADS1100_CFG_SC
,
410 static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops
,
411 ads1100_runtime_suspend
,
412 ads1100_runtime_resume
,
415 static const struct i2c_device_id ads1100_id
[] = {
421 MODULE_DEVICE_TABLE(i2c
, ads1100_id
);
423 static const struct of_device_id ads1100_of_match
[] = {
424 {.compatible
= "ti,ads1100" },
425 {.compatible
= "ti,ads1000" },
429 MODULE_DEVICE_TABLE(of
, ads1100_of_match
);
431 static struct i2c_driver ads1100_driver
= {
434 .of_match_table
= ads1100_of_match
,
435 .pm
= pm_ptr(&ads1100_pm_ops
),
437 .probe
= ads1100_probe
,
438 .id_table
= ads1100_id
,
441 module_i2c_driver(ads1100_driver
);
443 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
444 MODULE_DESCRIPTION("Texas Instruments ADS1100 ADC driver");
445 MODULE_LICENSE("GPL");