drm/log: select CONFIG_FONT_SUPPORT
[drm/drm-misc.git] / drivers / iio / adc / ti-ads1100.c
blob1e46f07a9ca6a805ea65d51e636caa30bb59e3b1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
9 */
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)
32 /* Data rate */
33 #define ADS1100_DR_MASK GENMASK(3, 2)
34 /* Gain */
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 };
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 */
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,
67 .datasheet_name = "AIN",
70 static int ads1100_set_config_bits(struct ads1100_data *data, u8 mask, u8 value)
72 int ret;
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);
79 if (ret < 0)
80 return ret;
82 data->config = config;
84 return 0;
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)
94 int ret;
95 __be16 buffer;
96 s16 value;
98 if (chan != 0)
99 return -EINVAL;
101 ret = pm_runtime_resume_and_get(&data->client->dev);
102 if (ret < 0)
103 return ret;
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);
110 if (ret < 0) {
111 dev_err(&data->client->dev, "I2C read fail: %d\n", ret);
112 return 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);
123 return 0;
126 static int ads1100_set_scale(struct ads1100_data *data, int val, int val2)
128 int microvolts;
129 int gain;
131 /* With Vdd between 2.7 and 5V, the scale is always below 1 */
132 if (val)
133 return -EINVAL;
135 if (!val2)
136 return -EINVAL;
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))
148 return -EINVAL;
150 ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain) - 1);
152 return 0;
155 static int ads1100_set_data_rate(struct ads1100_data *data, int chan, int rate)
157 unsigned int i;
158 unsigned int size;
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));
167 return -EINVAL;
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);
178 unsigned int i;
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,
189 long mask)
191 struct ads1100_data *data = iio_priv(indio_dev);
193 if (chan->type != IIO_VOLTAGE)
194 return -EINVAL;
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;
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)
219 int ret;
220 struct ads1100_data *data = iio_priv(indio_dev);
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;
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;
249 mutex_unlock(&data->lock);
251 return ret;
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)
258 struct ads1100_data *data = iio_priv(indio_dev);
259 int ret;
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;
273 mutex_unlock(&data->lock);
275 return ret;
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)
286 int ret;
287 u8 buffer[3];
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;
295 ret = i2c_master_recv(data->client, buffer, sizeof(buffer));
296 if (ret < 0)
297 return ret;
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;
305 return 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;
323 int ret;
325 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
326 if (!indio_dev)
327 return -ENOMEM;
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);
346 if (ret < 0)
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);
351 if (ret)
352 return ret;
354 ret = ads1100_setup(data);
355 if (ret)
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);
360 if (ret)
361 return ret;
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);
369 if (ret)
370 return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
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");
377 return 0;
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);
387 return 0;
390 static int ads1100_runtime_resume(struct device *dev)
392 struct ads1100_data *data = dev_get_drvdata(dev);
393 int ret;
395 ret = regulator_enable(data->reg_vdd);
396 if (ret) {
397 dev_err(&data->client->dev, "Failed to enable Vdd\n");
398 return ret;
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,
407 ADS1100_CONTINUOUS);
410 static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops,
411 ads1100_runtime_suspend,
412 ads1100_runtime_resume,
413 NULL);
415 static const struct i2c_device_id ads1100_id[] = {
416 { "ads1100" },
417 { "ads1000" },
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 = {
432 .driver = {
433 .name = "ads1100",
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");