1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
5 * Copyright (C) 2012 Avionic Design GmbH
6 * Copyright (C) 2016 Intel
9 * https://www.ti.com/lit/ds/symlink/adc081c021.pdf
10 * https://www.ti.com/lit/ds/symlink/adc101c021.pdf
11 * https://www.ti.com/lit/ds/symlink/adc121c021.pdf
13 * The devices have a very similar interface and differ mostly in the number of
14 * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
15 * bits of value registers are reserved.
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/property.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/regulator/consumer.h>
31 struct i2c_client *i2c;
32 struct regulator *ref;
37 /* Ensure natural alignment of buffer elements */
44 #define REG_CONV_RES 0x00
46 static int adc081c_read_raw(struct iio_dev *iio,
47 struct iio_chan_spec const *channel, int *value,
48 int *shift, long mask)
50 struct adc081c *adc = iio_priv(iio);
54 case IIO_CHAN_INFO_RAW:
55 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
59 *value = (err & 0xFFF) >> (12 - adc->bits);
62 case IIO_CHAN_INFO_SCALE:
63 err = regulator_get_voltage(adc->ref);
70 return IIO_VAL_FRACTIONAL_LOG2;
79 #define ADCxx1C_CHAN(_bits) { \
80 .type = IIO_VOLTAGE, \
81 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
82 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
85 .realbits = (_bits), \
87 .shift = 12 - (_bits), \
88 .endianness = IIO_CPU, \
92 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
93 static const struct iio_chan_spec _name ## _channels[] = { \
94 ADCxx1C_CHAN((_bits)), \
95 IIO_CHAN_SOFT_TIMESTAMP(1), \
98 #define ADC081C_NUM_CHANNELS 2
100 struct adcxx1c_model {
101 const struct iio_chan_spec* channels;
105 #define ADCxx1C_MODEL(_name, _bits) \
107 .channels = _name ## _channels, \
111 DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
112 DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
113 DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
115 /* Model ids are indexes in _models array */
116 enum adcxx1c_model_id {
122 static struct adcxx1c_model adcxx1c_models[] = {
123 ADCxx1C_MODEL(adc081c, 8),
124 ADCxx1C_MODEL(adc101c, 10),
125 ADCxx1C_MODEL(adc121c, 12),
128 static const struct iio_info adc081c_info = {
129 .read_raw = adc081c_read_raw,
132 static irqreturn_t adc081c_trigger_handler(int irq, void *p)
134 struct iio_poll_func *pf = p;
135 struct iio_dev *indio_dev = pf->indio_dev;
136 struct adc081c *data = iio_priv(indio_dev);
139 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
142 data->scan.channel = ret;
143 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
144 iio_get_time_ns(indio_dev));
146 iio_trigger_notify_done(indio_dev->trig);
150 static void adc081c_reg_disable(void *reg)
152 regulator_disable(reg);
155 static int adc081c_probe(struct i2c_client *client)
159 const struct adcxx1c_model *model;
162 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
165 model = i2c_get_match_data(client);
167 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
173 adc->bits = model->bits;
175 adc->ref = devm_regulator_get(&client->dev, "vref");
176 if (IS_ERR(adc->ref))
177 return PTR_ERR(adc->ref);
179 err = regulator_enable(adc->ref);
183 err = devm_add_action_or_reset(&client->dev, adc081c_reg_disable,
188 iio->name = dev_name(&client->dev);
189 iio->modes = INDIO_DIRECT_MODE;
190 iio->info = &adc081c_info;
192 iio->channels = model->channels;
193 iio->num_channels = ADC081C_NUM_CHANNELS;
195 err = devm_iio_triggered_buffer_setup(&client->dev, iio, NULL,
196 adc081c_trigger_handler, NULL);
198 dev_err(&client->dev, "iio triggered buffer setup failed\n");
202 return devm_iio_device_register(&client->dev, iio);
205 static const struct i2c_device_id adc081c_id[] = {
206 { "adc081c", (kernel_ulong_t)&adcxx1c_models[ADC081C] },
207 { "adc101c", (kernel_ulong_t)&adcxx1c_models[ADC101C] },
208 { "adc121c", (kernel_ulong_t)&adcxx1c_models[ADC121C] },
211 MODULE_DEVICE_TABLE(i2c, adc081c_id);
213 static const struct acpi_device_id adc081c_acpi_match[] = {
214 /* Used on some AAEON boards */
215 { "ADC081C", (kernel_ulong_t)&adcxx1c_models[ADC081C] },
218 MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
220 static const struct of_device_id adc081c_of_match[] = {
221 { .compatible = "ti,adc081c", .data = &adcxx1c_models[ADC081C] },
222 { .compatible = "ti,adc101c", .data = &adcxx1c_models[ADC101C] },
223 { .compatible = "ti,adc121c", .data = &adcxx1c_models[ADC121C] },
226 MODULE_DEVICE_TABLE(of, adc081c_of_match);
228 static struct i2c_driver adc081c_driver = {
231 .of_match_table = adc081c_of_match,
232 .acpi_match_table = adc081c_acpi_match,
234 .probe = adc081c_probe,
235 .id_table = adc081c_id,
237 module_i2c_driver(adc081c_driver);
239 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
240 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
241 MODULE_LICENSE("GPL v2");