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 * http://www.ti.com/lit/ds/symlink/adc081c021.pdf
10 * http://www.ti.com/lit/ds/symlink/adc101c021.pdf
11 * http://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>
22 #include <linux/acpi.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
;
38 #define REG_CONV_RES 0x00
40 static int adc081c_read_raw(struct iio_dev
*iio
,
41 struct iio_chan_spec
const *channel
, int *value
,
42 int *shift
, long mask
)
44 struct adc081c
*adc
= iio_priv(iio
);
48 case IIO_CHAN_INFO_RAW
:
49 err
= i2c_smbus_read_word_swapped(adc
->i2c
, REG_CONV_RES
);
53 *value
= (err
& 0xFFF) >> (12 - adc
->bits
);
56 case IIO_CHAN_INFO_SCALE
:
57 err
= regulator_get_voltage(adc
->ref
);
64 return IIO_VAL_FRACTIONAL_LOG2
;
73 #define ADCxx1C_CHAN(_bits) { \
74 .type = IIO_VOLTAGE, \
75 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
76 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
79 .realbits = (_bits), \
81 .shift = 12 - (_bits), \
82 .endianness = IIO_CPU, \
86 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
87 static const struct iio_chan_spec _name ## _channels[] = { \
88 ADCxx1C_CHAN((_bits)), \
89 IIO_CHAN_SOFT_TIMESTAMP(1), \
92 #define ADC081C_NUM_CHANNELS 2
94 struct adcxx1c_model
{
95 const struct iio_chan_spec
* channels
;
99 #define ADCxx1C_MODEL(_name, _bits) \
101 .channels = _name ## _channels, \
105 DEFINE_ADCxx1C_CHANNELS(adc081c
, 8);
106 DEFINE_ADCxx1C_CHANNELS(adc101c
, 10);
107 DEFINE_ADCxx1C_CHANNELS(adc121c
, 12);
109 /* Model ids are indexes in _models array */
110 enum adcxx1c_model_id
{
116 static struct adcxx1c_model adcxx1c_models
[] = {
117 ADCxx1C_MODEL(adc081c
, 8),
118 ADCxx1C_MODEL(adc101c
, 10),
119 ADCxx1C_MODEL(adc121c
, 12),
122 static const struct iio_info adc081c_info
= {
123 .read_raw
= adc081c_read_raw
,
126 static irqreturn_t
adc081c_trigger_handler(int irq
, void *p
)
128 struct iio_poll_func
*pf
= p
;
129 struct iio_dev
*indio_dev
= pf
->indio_dev
;
130 struct adc081c
*data
= iio_priv(indio_dev
);
131 u16 buf
[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */
134 ret
= i2c_smbus_read_word_swapped(data
->i2c
, REG_CONV_RES
);
138 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
139 iio_get_time_ns(indio_dev
));
141 iio_trigger_notify_done(indio_dev
->trig
);
145 static int adc081c_probe(struct i2c_client
*client
,
146 const struct i2c_device_id
*id
)
150 struct adcxx1c_model
*model
;
153 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
156 if (ACPI_COMPANION(&client
->dev
)) {
157 const struct acpi_device_id
*ad_id
;
159 ad_id
= acpi_match_device(client
->dev
.driver
->acpi_match_table
,
163 model
= &adcxx1c_models
[ad_id
->driver_data
];
165 model
= &adcxx1c_models
[id
->driver_data
];
168 iio
= devm_iio_device_alloc(&client
->dev
, sizeof(*adc
));
174 adc
->bits
= model
->bits
;
176 adc
->ref
= devm_regulator_get(&client
->dev
, "vref");
177 if (IS_ERR(adc
->ref
))
178 return PTR_ERR(adc
->ref
);
180 err
= regulator_enable(adc
->ref
);
184 iio
->dev
.parent
= &client
->dev
;
185 iio
->dev
.of_node
= client
->dev
.of_node
;
186 iio
->name
= dev_name(&client
->dev
);
187 iio
->modes
= INDIO_DIRECT_MODE
;
188 iio
->info
= &adc081c_info
;
190 iio
->channels
= model
->channels
;
191 iio
->num_channels
= ADC081C_NUM_CHANNELS
;
193 err
= iio_triggered_buffer_setup(iio
, NULL
, adc081c_trigger_handler
, NULL
);
195 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
196 goto err_regulator_disable
;
199 err
= iio_device_register(iio
);
201 goto err_buffer_cleanup
;
203 i2c_set_clientdata(client
, iio
);
208 iio_triggered_buffer_cleanup(iio
);
209 err_regulator_disable
:
210 regulator_disable(adc
->ref
);
215 static int adc081c_remove(struct i2c_client
*client
)
217 struct iio_dev
*iio
= i2c_get_clientdata(client
);
218 struct adc081c
*adc
= iio_priv(iio
);
220 iio_device_unregister(iio
);
221 iio_triggered_buffer_cleanup(iio
);
222 regulator_disable(adc
->ref
);
227 static const struct i2c_device_id adc081c_id
[] = {
228 { "adc081c", ADC081C
},
229 { "adc101c", ADC101C
},
230 { "adc121c", ADC121C
},
233 MODULE_DEVICE_TABLE(i2c
, adc081c_id
);
236 static const struct of_device_id adc081c_of_match
[] = {
237 { .compatible
= "ti,adc081c" },
238 { .compatible
= "ti,adc101c" },
239 { .compatible
= "ti,adc121c" },
242 MODULE_DEVICE_TABLE(of
, adc081c_of_match
);
246 static const struct acpi_device_id adc081c_acpi_match
[] = {
247 { "ADC081C", ADC081C
},
248 { "ADC101C", ADC101C
},
249 { "ADC121C", ADC121C
},
252 MODULE_DEVICE_TABLE(acpi
, adc081c_acpi_match
);
255 static struct i2c_driver adc081c_driver
= {
258 .of_match_table
= of_match_ptr(adc081c_of_match
),
259 .acpi_match_table
= ACPI_PTR(adc081c_acpi_match
),
261 .probe
= adc081c_probe
,
262 .remove
= adc081c_remove
,
263 .id_table
= adc081c_id
,
265 module_i2c_driver(adc081c_driver
);
267 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
268 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
269 MODULE_LICENSE("GPL v2");