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>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/regulator/consumer.h>
30 struct i2c_client
*i2c
;
31 struct regulator
*ref
;
36 /* Ensure natural alignment of buffer elements */
43 #define REG_CONV_RES 0x00
45 static int adc081c_read_raw(struct iio_dev
*iio
,
46 struct iio_chan_spec
const *channel
, int *value
,
47 int *shift
, long mask
)
49 struct adc081c
*adc
= iio_priv(iio
);
53 case IIO_CHAN_INFO_RAW
:
54 err
= i2c_smbus_read_word_swapped(adc
->i2c
, REG_CONV_RES
);
58 *value
= (err
& 0xFFF) >> (12 - adc
->bits
);
61 case IIO_CHAN_INFO_SCALE
:
62 err
= regulator_get_voltage(adc
->ref
);
69 return IIO_VAL_FRACTIONAL_LOG2
;
78 #define ADCxx1C_CHAN(_bits) { \
79 .type = IIO_VOLTAGE, \
80 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
84 .realbits = (_bits), \
86 .shift = 12 - (_bits), \
87 .endianness = IIO_CPU, \
91 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
92 static const struct iio_chan_spec _name ## _channels[] = { \
93 ADCxx1C_CHAN((_bits)), \
94 IIO_CHAN_SOFT_TIMESTAMP(1), \
97 #define ADC081C_NUM_CHANNELS 2
99 struct adcxx1c_model
{
100 const struct iio_chan_spec
* channels
;
104 #define ADCxx1C_MODEL(_name, _bits) \
106 .channels = _name ## _channels, \
110 DEFINE_ADCxx1C_CHANNELS(adc081c
, 8);
111 DEFINE_ADCxx1C_CHANNELS(adc101c
, 10);
112 DEFINE_ADCxx1C_CHANNELS(adc121c
, 12);
114 /* Model ids are indexes in _models array */
115 enum adcxx1c_model_id
{
121 static struct adcxx1c_model adcxx1c_models
[] = {
122 ADCxx1C_MODEL(adc081c
, 8),
123 ADCxx1C_MODEL(adc101c
, 10),
124 ADCxx1C_MODEL(adc121c
, 12),
127 static const struct iio_info adc081c_info
= {
128 .read_raw
= adc081c_read_raw
,
131 static irqreturn_t
adc081c_trigger_handler(int irq
, void *p
)
133 struct iio_poll_func
*pf
= p
;
134 struct iio_dev
*indio_dev
= pf
->indio_dev
;
135 struct adc081c
*data
= iio_priv(indio_dev
);
138 ret
= i2c_smbus_read_word_swapped(data
->i2c
, REG_CONV_RES
);
141 data
->scan
.channel
= ret
;
142 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
143 iio_get_time_ns(indio_dev
));
145 iio_trigger_notify_done(indio_dev
->trig
);
149 static int adc081c_probe(struct i2c_client
*client
,
150 const struct i2c_device_id
*id
)
154 struct adcxx1c_model
*model
;
157 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
160 model
= &adcxx1c_models
[id
->driver_data
];
162 iio
= devm_iio_device_alloc(&client
->dev
, sizeof(*adc
));
168 adc
->bits
= model
->bits
;
170 adc
->ref
= devm_regulator_get(&client
->dev
, "vref");
171 if (IS_ERR(adc
->ref
))
172 return PTR_ERR(adc
->ref
);
174 err
= regulator_enable(adc
->ref
);
178 iio
->name
= dev_name(&client
->dev
);
179 iio
->modes
= INDIO_DIRECT_MODE
;
180 iio
->info
= &adc081c_info
;
182 iio
->channels
= model
->channels
;
183 iio
->num_channels
= ADC081C_NUM_CHANNELS
;
185 err
= iio_triggered_buffer_setup(iio
, NULL
, adc081c_trigger_handler
, NULL
);
187 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
188 goto err_regulator_disable
;
191 err
= iio_device_register(iio
);
193 goto err_buffer_cleanup
;
195 i2c_set_clientdata(client
, iio
);
200 iio_triggered_buffer_cleanup(iio
);
201 err_regulator_disable
:
202 regulator_disable(adc
->ref
);
207 static int adc081c_remove(struct i2c_client
*client
)
209 struct iio_dev
*iio
= i2c_get_clientdata(client
);
210 struct adc081c
*adc
= iio_priv(iio
);
212 iio_device_unregister(iio
);
213 iio_triggered_buffer_cleanup(iio
);
214 regulator_disable(adc
->ref
);
219 static const struct i2c_device_id adc081c_id
[] = {
220 { "adc081c", ADC081C
},
221 { "adc101c", ADC101C
},
222 { "adc121c", ADC121C
},
225 MODULE_DEVICE_TABLE(i2c
, adc081c_id
);
227 static const struct of_device_id adc081c_of_match
[] = {
228 { .compatible
= "ti,adc081c" },
229 { .compatible
= "ti,adc101c" },
230 { .compatible
= "ti,adc121c" },
233 MODULE_DEVICE_TABLE(of
, adc081c_of_match
);
235 static struct i2c_driver adc081c_driver
= {
238 .of_match_table
= adc081c_of_match
,
240 .probe
= adc081c_probe
,
241 .remove
= adc081c_remove
,
242 .id_table
= adc081c_id
,
244 module_i2c_driver(adc081c_driver
);
246 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
247 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
248 MODULE_LICENSE("GPL v2");