blob: 9426f70a800598c0f4010708f332e8e5410a6c13 [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>
Dan O'Donovan7feae872016-05-27 18:37:30 +010022#include <linux/acpi.h>
Thierry Redingbc0a4092012-11-23 15:13:00 +000023
24#include <linux/iio/iio.h>
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030025#include <linux/iio/buffer.h>
26#include <linux/iio/trigger_consumer.h>
27#include <linux/iio/triggered_buffer.h>
Thierry Redingbc0a4092012-11-23 15:13:00 +000028#include <linux/regulator/consumer.h>
29
30struct adc081c {
31 struct i2c_client *i2c;
32 struct regulator *ref;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030033
34 /* 8, 10 or 12 */
35 int bits;
Thierry Redingbc0a4092012-11-23 15:13:00 +000036};
37
38#define REG_CONV_RES 0x00
39
40static int adc081c_read_raw(struct iio_dev *iio,
41 struct iio_chan_spec const *channel, int *value,
42 int *shift, long mask)
43{
44 struct adc081c *adc = iio_priv(iio);
45 int err;
46
47 switch (mask) {
48 case IIO_CHAN_INFO_RAW:
49 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
50 if (err < 0)
51 return err;
52
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030053 *value = (err & 0xFFF) >> (12 - adc->bits);
Thierry Redingbc0a4092012-11-23 15:13:00 +000054 return IIO_VAL_INT;
55
56 case IIO_CHAN_INFO_SCALE:
57 err = regulator_get_voltage(adc->ref);
58 if (err < 0)
59 return err;
60
61 *value = err / 1000;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030062 *shift = adc->bits;
Thierry Redingbc0a4092012-11-23 15:13:00 +000063
64 return IIO_VAL_FRACTIONAL_LOG2;
65
66 default:
67 break;
68 }
69
70 return -EINVAL;
71}
72
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030073#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), \
77 .scan_type = { \
78 .sign = 'u', \
79 .realbits = (_bits), \
80 .storagebits = 16, \
81 .shift = 12 - (_bits), \
82 .endianness = IIO_CPU, \
83 }, \
84}
85
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), \
90 }; \
91
92#define ADC081C_NUM_CHANNELS 2
Thierry Redingbc0a4092012-11-23 15:13:00 +000093
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030094struct adcxx1c_model {
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030095 const struct iio_chan_spec* channels;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +030096 int bits;
97};
98
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +030099#define ADCxx1C_MODEL(_name, _bits) \
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300100 { \
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300101 .channels = _name ## _channels, \
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300102 .bits = (_bits), \
103 }
104
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300105DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
106DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
107DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
108
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300109/* Model ids are indexes in _models array */
110enum adcxx1c_model_id {
111 ADC081C = 0,
112 ADC101C = 1,
113 ADC121C = 2,
114};
115
116static struct adcxx1c_model adcxx1c_models[] = {
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300117 ADCxx1C_MODEL(adc081c, 8),
118 ADCxx1C_MODEL(adc101c, 10),
119 ADCxx1C_MODEL(adc121c, 12),
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300120};
121
Thierry Redingbc0a4092012-11-23 15:13:00 +0000122static const struct iio_info adc081c_info = {
123 .read_raw = adc081c_read_raw,
Thierry Redingbc0a4092012-11-23 15:13:00 +0000124};
125
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300126static irqreturn_t adc081c_trigger_handler(int irq, void *p)
127{
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 */
132 int ret;
133
134 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
135 if (ret < 0)
136 goto out;
137 buf[0] = ret;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100138 iio_push_to_buffers_with_timestamp(indio_dev, buf,
139 iio_get_time_ns(indio_dev));
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300140out:
141 iio_trigger_notify_done(indio_dev->trig);
142 return IRQ_HANDLED;
143}
144
Thierry Redingbc0a4092012-11-23 15:13:00 +0000145static int adc081c_probe(struct i2c_client *client,
146 const struct i2c_device_id *id)
147{
148 struct iio_dev *iio;
149 struct adc081c *adc;
Dan O'Donovan7feae872016-05-27 18:37:30 +0100150 struct adcxx1c_model *model;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000151 int err;
152
153 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
Matt Ranostayf8d9d3b2016-02-26 22:13:49 -0800154 return -EOPNOTSUPP;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000155
Dan O'Donovan7feae872016-05-27 18:37:30 +0100156 if (ACPI_COMPANION(&client->dev)) {
157 const struct acpi_device_id *ad_id;
158
159 ad_id = acpi_match_device(client->dev.driver->acpi_match_table,
160 &client->dev);
161 if (!ad_id)
162 return -ENODEV;
163 model = &adcxx1c_models[ad_id->driver_data];
164 } else {
165 model = &adcxx1c_models[id->driver_data];
166 }
167
Sachin Kamat99e94b62013-07-23 09:58:00 +0100168 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
Thierry Redingbc0a4092012-11-23 15:13:00 +0000169 if (!iio)
170 return -ENOMEM;
171
172 adc = iio_priv(iio);
173 adc->i2c = client;
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300174 adc->bits = model->bits;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000175
Sachin Kamat99e94b62013-07-23 09:58:00 +0100176 adc->ref = devm_regulator_get(&client->dev, "vref");
177 if (IS_ERR(adc->ref))
178 return PTR_ERR(adc->ref);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000179
180 err = regulator_enable(adc->ref);
181 if (err < 0)
Sachin Kamat99e94b62013-07-23 09:58:00 +0100182 return err;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000183
Thierry Redingbc0a4092012-11-23 15:13:00 +0000184 iio->name = dev_name(&client->dev);
185 iio->modes = INDIO_DIRECT_MODE;
186 iio->info = &adc081c_info;
187
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300188 iio->channels = model->channels;
189 iio->num_channels = ADC081C_NUM_CHANNELS;
190
191 err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
192 if (err < 0) {
193 dev_err(&client->dev, "iio triggered buffer setup failed\n");
194 goto err_regulator_disable;
195 }
Thierry Redingbc0a4092012-11-23 15:13:00 +0000196
197 err = iio_device_register(iio);
198 if (err < 0)
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300199 goto err_buffer_cleanup;
Thierry Redingbc0a4092012-11-23 15:13:00 +0000200
201 i2c_set_clientdata(client, iio);
202
203 return 0;
204
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300205err_buffer_cleanup:
206 iio_triggered_buffer_cleanup(iio);
207err_regulator_disable:
Thierry Redingbc0a4092012-11-23 15:13:00 +0000208 regulator_disable(adc->ref);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000209
210 return err;
211}
212
213static int adc081c_remove(struct i2c_client *client)
214{
215 struct iio_dev *iio = i2c_get_clientdata(client);
216 struct adc081c *adc = iio_priv(iio);
217
218 iio_device_unregister(iio);
Crestez Dan Leonard08e05d12016-04-11 17:24:27 +0300219 iio_triggered_buffer_cleanup(iio);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000220 regulator_disable(adc->ref);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000221
222 return 0;
223}
224
225static const struct i2c_device_id adc081c_id[] = {
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300226 { "adc081c", ADC081C },
227 { "adc101c", ADC101C },
228 { "adc121c", ADC121C },
Thierry Redingbc0a4092012-11-23 15:13:00 +0000229 { }
230};
231MODULE_DEVICE_TABLE(i2c, adc081c_id);
232
Thierry Redingbc0a4092012-11-23 15:13:00 +0000233static const struct of_device_id adc081c_of_match[] = {
234 { .compatible = "ti,adc081c" },
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300235 { .compatible = "ti,adc101c" },
236 { .compatible = "ti,adc121c" },
Thierry Redingbc0a4092012-11-23 15:13:00 +0000237 { }
238};
239MODULE_DEVICE_TABLE(of, adc081c_of_match);
Thierry Redingbc0a4092012-11-23 15:13:00 +0000240
Dan O'Donovan7feae872016-05-27 18:37:30 +0100241#ifdef CONFIG_ACPI
242static const struct acpi_device_id adc081c_acpi_match[] = {
243 { "ADC081C", ADC081C },
244 { "ADC101C", ADC101C },
245 { "ADC121C", ADC121C },
246 { }
247};
248MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
249#endif
250
Thierry Redingbc0a4092012-11-23 15:13:00 +0000251static struct i2c_driver adc081c_driver = {
252 .driver = {
253 .name = "adc081c",
Jonathan Cameronb0873742020-06-28 13:36:46 +0100254 .of_match_table = adc081c_of_match,
Dan O'Donovan7feae872016-05-27 18:37:30 +0100255 .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
Thierry Redingbc0a4092012-11-23 15:13:00 +0000256 },
257 .probe = adc081c_probe,
258 .remove = adc081c_remove,
259 .id_table = adc081c_id,
260};
261module_i2c_driver(adc081c_driver);
262
263MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
Crestez Dan Leonarda6b5ec82016-04-11 17:24:26 +0300264MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
Thierry Redingbc0a4092012-11-23 15:13:00 +0000265MODULE_LICENSE("GPL v2");