blob: 16fc608db36a5385983de204133b4291deaa1d02 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thierry Redingbc0a4092012-11-23 15:13:00 +00002/*
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +03003 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4 *
Thierry Redingbc0a4092012-11-23 15:13:00 +00005 * Copyright (C) 2012 Avionic Design GmbH
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +03006 * Copyright (C) 2016 Intel
Thierry Redingbc0a4092012-11-23 15:13:00 +00007 *
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +03008 * Datasheets:
Alexander A. Klimov3593cd52020-07-04 21:27:43 +02009 * 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
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030012 *
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.
Thierry Redingbc0a4092012-11-23 15:13:00 +000016 */
17
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/module.h>
Jonathan Cameronb0873742020-06-28 13:36:46 +010021#include <linux/mod_devicetable.h>
Thierry Redingbc0a4092012-11-23 15:13:00 +000022
23#include <linux/iio/iio.h>
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030024#include <linux/iio/buffer.h>
25#include <linux/iio/trigger_consumer.h>
26#include <linux/iio/triggered_buffer.h>
Thierry Redingbc0a4092012-11-23 15:13:00 +000027#include <linux/regulator/consumer.h>
28
29struct adc081c {
30 struct i2c_client *i2c;
31 struct regulator *ref;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030032
33 /* 8, 10 or 12 */
34 int bits;
Jonathan Cameron54f82df2020-07-22 16:50:56 +010035
36 /* Ensure natural alignment of buffer elements */
37 struct {
38 u16 channel;
39 s64 ts __aligned(8);
40 } scan;
Thierry Redingbc0a4092012-11-23 15:13:00 +000041};
42
43#define REG_CONV_RES 0x00
44
45static int adc081c_read_raw(struct iio_dev *iio,
46 struct iio_chan_spec const *channel, int *value,
47 int *shift, long mask)
48{
49 struct adc081c *adc = iio_priv(iio);
50 int err;
51
52 switch (mask) {
53 case IIO_CHAN_INFO_RAW:
54 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
55 if (err < 0)
56 return err;
57
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030058 *value = (err & 0xFFF) >> (12 - adc->bits);
Thierry Redingbc0a4092012-11-23 15:13:00 +000059 return IIO_VAL_INT;
60
61 case IIO_CHAN_INFO_SCALE:
62 err = regulator_get_voltage(adc->ref);
63 if (err < 0)
64 return err;
65
66 *value = err / 1000;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030067 *shift = adc->bits;
Thierry Redingbc0a4092012-11-23 15:13:00 +000068
69 return IIO_VAL_FRACTIONAL_LOG2;
70
71 default:
72 break;
73 }
74
75 return -EINVAL;
76}
77
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030078#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), \
82 .scan_type = { \
83 .sign = 'u', \
84 .realbits = (_bits), \
85 .storagebits = 16, \
86 .shift = 12 - (_bits), \
87 .endianness = IIO_CPU, \
88 }, \
89}
90
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), \
95 }; \
96
97#define ADC081C_NUM_CHANNELS 2
Thierry Redingbc0a4092012-11-23 15:13:00 +000098
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030099struct adcxx1c_model {
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300100 const struct iio_chan_spec* channels;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300101 int bits;
102};
103
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300104#define ADCxx1C_MODEL(_name, _bits) \
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300105 { \
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300106 .channels = _name ## _channels, \
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300107 .bits = (_bits), \
108 }
109
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300110DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
111DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
112DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
113
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300114/* Model ids are indexes in _models array */
115enum adcxx1c_model_id {
116 ADC081C = 0,
117 ADC101C = 1,
118 ADC121C = 2,
119};
120
121static struct adcxx1c_model adcxx1c_models[] = {
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300122 ADCxx1C_MODEL(adc081c, 8),
123 ADCxx1C_MODEL(adc101c, 10),
124 ADCxx1C_MODEL(adc121c, 12),
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300125};
126
Thierry Redingbc0a4092012-11-23 15:13:00 +0000127static const struct iio_info adc081c_info = {
128 .read_raw = adc081c_read_raw,
Thierry Redingbc0a4092012-11-23 15:13:00 +0000129};
130
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300131static irqreturn_t adc081c_trigger_handler(int irq, void *p)
132{
133 struct iio_poll_func *pf = p;
134 struct iio_dev *indio_dev = pf->indio_dev;
135 struct adc081c *data = iio_priv(indio_dev);
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300136 int ret;
137
138 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
139 if (ret < 0)
140 goto out;
Jonathan Cameron54f82df2020-07-22 16:50:56 +0100141 data->scan.channel = ret;
142 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100143 iio_get_time_ns(indio_dev));
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300144out:
145 iio_trigger_notify_done(indio_dev->trig);
146 return IRQ_HANDLED;
147}
148
Jonathan Cameron6c100eb2021-05-16 18:25:17 +0100149static void adc081c_reg_disable(void *reg)
150{
151 regulator_disable(reg);
152}
153
Thierry Redingbc0a4092012-11-23 15:13:00 +0000154static int adc081c_probe(struct i2c_client *client,
155 const struct i2c_device_id *id)
156{
157 struct iio_dev *iio;
158 struct adc081c *adc;
Dan O'Donovan7feae872016-05-27 18:37:30 +0100159 struct adcxx1c_model *model;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000160 int err;
161
162 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
Matt Ranostayf8d9d3b2016-02-26 22:13:49 -0800163 return -EOPNOTSUPP;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000164
Jonathan Cameronc458b7c2020-07-21 18:14:41 +0100165 model = &adcxx1c_models[id->driver_data];
Dan O'Donovan7feae872016-05-27 18:37:30 +0100166
Sachin Kamat99e94b62013-07-23 09:58:00 +0100167 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
Thierry Redingbc0a4092012-11-23 15:13:00 +0000168 if (!iio)
169 return -ENOMEM;
170
171 adc = iio_priv(iio);
172 adc->i2c = client;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300173 adc->bits = model->bits;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000174
Sachin Kamat99e94b62013-07-23 09:58:00 +0100175 adc->ref = devm_regulator_get(&client->dev, "vref");
176 if (IS_ERR(adc->ref))
177 return PTR_ERR(adc->ref);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000178
179 err = regulator_enable(adc->ref);
180 if (err < 0)
Sachin Kamat99e94b62013-07-23 09:58:00 +0100181 return err;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000182
Jonathan Cameron6c100eb2021-05-16 18:25:17 +0100183 err = devm_add_action_or_reset(&client->dev, adc081c_reg_disable,
184 adc->ref);
185 if (err)
186 return err;
187
Thierry Redingbc0a4092012-11-23 15:13:00 +0000188 iio->name = dev_name(&client->dev);
189 iio->modes = INDIO_DIRECT_MODE;
190 iio->info = &adc081c_info;
191
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300192 iio->channels = model->channels;
193 iio->num_channels = ADC081C_NUM_CHANNELS;
194
Jonathan Cameron6c100eb2021-05-16 18:25:17 +0100195 err = devm_iio_triggered_buffer_setup(&client->dev, iio, NULL,
196 adc081c_trigger_handler, NULL);
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300197 if (err < 0) {
198 dev_err(&client->dev, "iio triggered buffer setup failed\n");
Jonathan Cameron6c100eb2021-05-16 18:25:17 +0100199 return err;
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300200 }
Thierry Redingbc0a4092012-11-23 15:13:00 +0000201
Jonathan Cameron6c100eb2021-05-16 18:25:17 +0100202 return devm_iio_device_register(&client->dev, iio);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000203}
204
205static const struct i2c_device_id adc081c_id[] = {
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300206 { "adc081c", ADC081C },
207 { "adc101c", ADC101C },
208 { "adc121c", ADC121C },
Thierry Redingbc0a4092012-11-23 15:13:00 +0000209 { }
210};
211MODULE_DEVICE_TABLE(i2c, adc081c_id);
212
Thierry Redingbc0a4092012-11-23 15:13:00 +0000213static const struct of_device_id adc081c_of_match[] = {
214 { .compatible = "ti,adc081c" },
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300215 { .compatible = "ti,adc101c" },
216 { .compatible = "ti,adc121c" },
Thierry Redingbc0a4092012-11-23 15:13:00 +0000217 { }
218};
219MODULE_DEVICE_TABLE(of, adc081c_of_match);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000220
221static struct i2c_driver adc081c_driver = {
222 .driver = {
223 .name = "adc081c",
Jonathan Cameronb0873742020-06-28 13:36:46 +0100224 .of_match_table = adc081c_of_match,
Thierry Redingbc0a4092012-11-23 15:13:00 +0000225 },
226 .probe = adc081c_probe,
Thierry Redingbc0a4092012-11-23 15:13:00 +0000227 .id_table = adc081c_id,
228};
229module_i2c_driver(adc081c_driver);
230
231MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300232MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
Thierry Redingbc0a4092012-11-23 15:13:00 +0000233MODULE_LICENSE("GPL v2");