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