Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4 *
5 * Copyright (C) 2012 Avionic Design GmbH
6 * Copyright (C) 2016 Intel
7 *
8 * Datasheets:
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
12 *
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.
16 */
17
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/acpi.h>
23
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>
29
30struct adc081c {
31 struct i2c_client *i2c;
32 struct regulator *ref;
33
34 /* 8, 10 or 12 */
35 int bits;
36};
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
53 *value = (err & 0xFFF) >> (12 - adc->bits);
54 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;
62 *shift = adc->bits;
63
64 return IIO_VAL_FRACTIONAL_LOG2;
65
66 default:
67 break;
68 }
69
70 return -EINVAL;
71}
72
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), \
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
93
94struct adcxx1c_model {
95 const struct iio_chan_spec* channels;
96 int bits;
97};
98
99#define ADCxx1C_MODEL(_name, _bits) \
100 { \
101 .channels = _name ## _channels, \
102 .bits = (_bits), \
103 }
104
105DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
106DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
107DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
108
109/* 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[] = {
117 ADCxx1C_MODEL(adc081c, 8),
118 ADCxx1C_MODEL(adc101c, 10),
119 ADCxx1C_MODEL(adc121c, 12),
120};
121
122static const struct iio_info adc081c_info = {
123 .read_raw = adc081c_read_raw,
124};
125
126static 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;
138 iio_push_to_buffers_with_timestamp(indio_dev, buf,
139 iio_get_time_ns(indio_dev));
140out:
141 iio_trigger_notify_done(indio_dev->trig);
142 return IRQ_HANDLED;
143}
144
145static int adc081c_probe(struct i2c_client *client,
146 const struct i2c_device_id *id)
147{
148 struct iio_dev *iio;
149 struct adc081c *adc;
150 struct adcxx1c_model *model;
151 int err;
152
153 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
154 return -EOPNOTSUPP;
155
156 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
168 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
169 if (!iio)
170 return -ENOMEM;
171
172 adc = iio_priv(iio);
173 adc->i2c = client;
174 adc->bits = model->bits;
175
176 adc->ref = devm_regulator_get(&client->dev, "vref");
177 if (IS_ERR(adc->ref))
178 return PTR_ERR(adc->ref);
179
180 err = regulator_enable(adc->ref);
181 if (err < 0)
182 return err;
183
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;
189
190 iio->channels = model->channels;
191 iio->num_channels = ADC081C_NUM_CHANNELS;
192
193 err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
194 if (err < 0) {
195 dev_err(&client->dev, "iio triggered buffer setup failed\n");
196 goto err_regulator_disable;
197 }
198
199 err = iio_device_register(iio);
200 if (err < 0)
201 goto err_buffer_cleanup;
202
203 i2c_set_clientdata(client, iio);
204
205 return 0;
206
207err_buffer_cleanup:
208 iio_triggered_buffer_cleanup(iio);
209err_regulator_disable:
210 regulator_disable(adc->ref);
211
212 return err;
213}
214
215static int adc081c_remove(struct i2c_client *client)
216{
217 struct iio_dev *iio = i2c_get_clientdata(client);
218 struct adc081c *adc = iio_priv(iio);
219
220 iio_device_unregister(iio);
221 iio_triggered_buffer_cleanup(iio);
222 regulator_disable(adc->ref);
223
224 return 0;
225}
226
227static const struct i2c_device_id adc081c_id[] = {
228 { "adc081c", ADC081C },
229 { "adc101c", ADC101C },
230 { "adc121c", ADC121C },
231 { }
232};
233MODULE_DEVICE_TABLE(i2c, adc081c_id);
234
235#ifdef CONFIG_OF
236static const struct of_device_id adc081c_of_match[] = {
237 { .compatible = "ti,adc081c" },
238 { .compatible = "ti,adc101c" },
239 { .compatible = "ti,adc121c" },
240 { }
241};
242MODULE_DEVICE_TABLE(of, adc081c_of_match);
243#endif
244
245#ifdef CONFIG_ACPI
246static const struct acpi_device_id adc081c_acpi_match[] = {
247 { "ADC081C", ADC081C },
248 { "ADC101C", ADC101C },
249 { "ADC121C", ADC121C },
250 { }
251};
252MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
253#endif
254
255static struct i2c_driver adc081c_driver = {
256 .driver = {
257 .name = "adc081c",
258 .of_match_table = of_match_ptr(adc081c_of_match),
259 .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
260 },
261 .probe = adc081c_probe,
262 .remove = adc081c_remove,
263 .id_table = adc081c_id,
264};
265module_i2c_driver(adc081c_driver);
266
267MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
268MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
269MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4 *
5 * Copyright (C) 2012 Avionic Design GmbH
6 * Copyright (C) 2016 Intel
7 *
8 * Datasheets:
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
12 *
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.
16 */
17
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/module.h>
21#include <linux/mod_devicetable.h>
22#include <linux/acpi.h>
23
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>
29
30struct adc081c {
31 struct i2c_client *i2c;
32 struct regulator *ref;
33
34 /* 8, 10 or 12 */
35 int bits;
36
37 /* Ensure natural alignment of buffer elements */
38 struct {
39 u16 channel;
40 s64 ts __aligned(8);
41 } scan;
42};
43
44#define REG_CONV_RES 0x00
45
46static int adc081c_read_raw(struct iio_dev *iio,
47 struct iio_chan_spec const *channel, int *value,
48 int *shift, long mask)
49{
50 struct adc081c *adc = iio_priv(iio);
51 int err;
52
53 switch (mask) {
54 case IIO_CHAN_INFO_RAW:
55 err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
56 if (err < 0)
57 return err;
58
59 *value = (err & 0xFFF) >> (12 - adc->bits);
60 return IIO_VAL_INT;
61
62 case IIO_CHAN_INFO_SCALE:
63 err = regulator_get_voltage(adc->ref);
64 if (err < 0)
65 return err;
66
67 *value = err / 1000;
68 *shift = adc->bits;
69
70 return IIO_VAL_FRACTIONAL_LOG2;
71
72 default:
73 break;
74 }
75
76 return -EINVAL;
77}
78
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), \
83 .scan_type = { \
84 .sign = 'u', \
85 .realbits = (_bits), \
86 .storagebits = 16, \
87 .shift = 12 - (_bits), \
88 .endianness = IIO_CPU, \
89 }, \
90}
91
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), \
96 }; \
97
98#define ADC081C_NUM_CHANNELS 2
99
100struct adcxx1c_model {
101 const struct iio_chan_spec* channels;
102 int bits;
103};
104
105#define ADCxx1C_MODEL(_name, _bits) \
106 { \
107 .channels = _name ## _channels, \
108 .bits = (_bits), \
109 }
110
111DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
112DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
113DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
114
115/* Model ids are indexes in _models array */
116enum adcxx1c_model_id {
117 ADC081C = 0,
118 ADC101C = 1,
119 ADC121C = 2,
120};
121
122static struct adcxx1c_model adcxx1c_models[] = {
123 ADCxx1C_MODEL(adc081c, 8),
124 ADCxx1C_MODEL(adc101c, 10),
125 ADCxx1C_MODEL(adc121c, 12),
126};
127
128static const struct iio_info adc081c_info = {
129 .read_raw = adc081c_read_raw,
130};
131
132static irqreturn_t adc081c_trigger_handler(int irq, void *p)
133{
134 struct iio_poll_func *pf = p;
135 struct iio_dev *indio_dev = pf->indio_dev;
136 struct adc081c *data = iio_priv(indio_dev);
137 int ret;
138
139 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
140 if (ret < 0)
141 goto out;
142 data->scan.channel = ret;
143 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
144 iio_get_time_ns(indio_dev));
145out:
146 iio_trigger_notify_done(indio_dev->trig);
147 return IRQ_HANDLED;
148}
149
150static int adc081c_probe(struct i2c_client *client,
151 const struct i2c_device_id *id)
152{
153 struct iio_dev *iio;
154 struct adc081c *adc;
155 struct adcxx1c_model *model;
156 int err;
157
158 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
159 return -EOPNOTSUPP;
160
161 if (ACPI_COMPANION(&client->dev)) {
162 const struct acpi_device_id *ad_id;
163
164 ad_id = acpi_match_device(client->dev.driver->acpi_match_table,
165 &client->dev);
166 if (!ad_id)
167 return -ENODEV;
168 model = &adcxx1c_models[ad_id->driver_data];
169 } else {
170 model = &adcxx1c_models[id->driver_data];
171 }
172
173 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
174 if (!iio)
175 return -ENOMEM;
176
177 adc = iio_priv(iio);
178 adc->i2c = client;
179 adc->bits = model->bits;
180
181 adc->ref = devm_regulator_get(&client->dev, "vref");
182 if (IS_ERR(adc->ref))
183 return PTR_ERR(adc->ref);
184
185 err = regulator_enable(adc->ref);
186 if (err < 0)
187 return err;
188
189 iio->name = dev_name(&client->dev);
190 iio->modes = INDIO_DIRECT_MODE;
191 iio->info = &adc081c_info;
192
193 iio->channels = model->channels;
194 iio->num_channels = ADC081C_NUM_CHANNELS;
195
196 err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
197 if (err < 0) {
198 dev_err(&client->dev, "iio triggered buffer setup failed\n");
199 goto err_regulator_disable;
200 }
201
202 err = iio_device_register(iio);
203 if (err < 0)
204 goto err_buffer_cleanup;
205
206 i2c_set_clientdata(client, iio);
207
208 return 0;
209
210err_buffer_cleanup:
211 iio_triggered_buffer_cleanup(iio);
212err_regulator_disable:
213 regulator_disable(adc->ref);
214
215 return err;
216}
217
218static int adc081c_remove(struct i2c_client *client)
219{
220 struct iio_dev *iio = i2c_get_clientdata(client);
221 struct adc081c *adc = iio_priv(iio);
222
223 iio_device_unregister(iio);
224 iio_triggered_buffer_cleanup(iio);
225 regulator_disable(adc->ref);
226
227 return 0;
228}
229
230static const struct i2c_device_id adc081c_id[] = {
231 { "adc081c", ADC081C },
232 { "adc101c", ADC101C },
233 { "adc121c", ADC121C },
234 { }
235};
236MODULE_DEVICE_TABLE(i2c, adc081c_id);
237
238static const struct of_device_id adc081c_of_match[] = {
239 { .compatible = "ti,adc081c" },
240 { .compatible = "ti,adc101c" },
241 { .compatible = "ti,adc121c" },
242 { }
243};
244MODULE_DEVICE_TABLE(of, adc081c_of_match);
245
246#ifdef CONFIG_ACPI
247static const struct acpi_device_id adc081c_acpi_match[] = {
248 { "ADC081C", ADC081C },
249 { "ADC101C", ADC101C },
250 { "ADC121C", ADC121C },
251 { }
252};
253MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
254#endif
255
256static struct i2c_driver adc081c_driver = {
257 .driver = {
258 .name = "adc081c",
259 .of_match_table = adc081c_of_match,
260 .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
261 },
262 .probe = adc081c_probe,
263 .remove = adc081c_remove,
264 .id_table = adc081c_id,
265};
266module_i2c_driver(adc081c_driver);
267
268MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
269MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
270MODULE_LICENSE("GPL v2");