GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / staging / iio / adc / ad7780.c
1 /*
2  * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/adc/ad_sigma_delta.h>
24
25 #define AD7780_RDY      BIT(7)
26 #define AD7780_FILTER   BIT(6)
27 #define AD7780_ERR      BIT(5)
28 #define AD7780_ID1      BIT(4)
29 #define AD7780_ID0      BIT(3)
30 #define AD7780_GAIN     BIT(2)
31 #define AD7780_PAT1     BIT(1)
32 #define AD7780_PAT0     BIT(0)
33
34 struct ad7780_chip_info {
35         struct iio_chan_spec    channel;
36         unsigned int            pattern_mask;
37         unsigned int            pattern;
38 };
39
40 struct ad7780_state {
41         const struct ad7780_chip_info   *chip_info;
42         struct regulator                *reg;
43         struct gpio_desc                *powerdown_gpio;
44         unsigned int    gain;
45         u16                             int_vref_mv;
46
47         struct ad_sigma_delta sd;
48 };
49
50 enum ad7780_supported_device_ids {
51         ID_AD7170,
52         ID_AD7171,
53         ID_AD7780,
54         ID_AD7781,
55 };
56
57 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
58 {
59         return container_of(sd, struct ad7780_state, sd);
60 }
61
62 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
63                            enum ad_sigma_delta_mode mode)
64 {
65         struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
66         unsigned int val;
67
68         switch (mode) {
69         case AD_SD_MODE_SINGLE:
70         case AD_SD_MODE_CONTINUOUS:
71                 val = 1;
72                 break;
73         default:
74                 val = 0;
75                 break;
76         }
77
78         gpiod_set_value(st->powerdown_gpio, val);
79
80         return 0;
81 }
82
83 static int ad7780_read_raw(struct iio_dev *indio_dev,
84                            struct iio_chan_spec const *chan,
85                            int *val,
86                            int *val2,
87                            long m)
88 {
89         struct ad7780_state *st = iio_priv(indio_dev);
90         int voltage_uv;
91
92         switch (m) {
93         case IIO_CHAN_INFO_RAW:
94                 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
95         case IIO_CHAN_INFO_SCALE:
96                 voltage_uv = regulator_get_voltage(st->reg);
97                 if (voltage_uv < 0)
98                         return voltage_uv;
99                 *val = (voltage_uv / 1000) * st->gain;
100                 *val2 = chan->scan_type.realbits - 1;
101                 return IIO_VAL_FRACTIONAL_LOG2;
102         case IIO_CHAN_INFO_OFFSET:
103                 *val -= (1 << (chan->scan_type.realbits - 1));
104                 return IIO_VAL_INT;
105         }
106
107         return -EINVAL;
108 }
109
110 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
111                                      unsigned int raw_sample)
112 {
113         struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
114         const struct ad7780_chip_info *chip_info = st->chip_info;
115
116         if ((raw_sample & AD7780_ERR) ||
117             ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
118                 return -EIO;
119
120         if (raw_sample & AD7780_GAIN)
121                 st->gain = 1;
122         else
123                 st->gain = 128;
124
125         return 0;
126 }
127
128 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
129         .set_mode = ad7780_set_mode,
130         .postprocess_sample = ad7780_postprocess_sample,
131         .has_registers = false,
132 };
133
134 #define AD7780_CHANNEL(bits, wordsize) \
135         AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
136
137 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
138         [ID_AD7170] = {
139                 .channel = AD7780_CHANNEL(12, 24),
140                 .pattern = 0x5,
141                 .pattern_mask = 0x7,
142         },
143         [ID_AD7171] = {
144                 .channel = AD7780_CHANNEL(16, 24),
145                 .pattern = 0x5,
146                 .pattern_mask = 0x7,
147         },
148         [ID_AD7780] = {
149                 .channel = AD7780_CHANNEL(24, 32),
150                 .pattern = 0x1,
151                 .pattern_mask = 0x3,
152         },
153         [ID_AD7781] = {
154                 .channel = AD7780_CHANNEL(20, 32),
155                 .pattern = 0x1,
156                 .pattern_mask = 0x3,
157         },
158 };
159
160 static const struct iio_info ad7780_info = {
161         .read_raw = ad7780_read_raw,
162 };
163
164 static int ad7780_probe(struct spi_device *spi)
165 {
166         struct ad7780_state *st;
167         struct iio_dev *indio_dev;
168         int ret, voltage_uv = 0;
169
170         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
171         if (!indio_dev)
172                 return -ENOMEM;
173
174         st = iio_priv(indio_dev);
175         st->gain = 1;
176
177         ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
178
179         st->reg = devm_regulator_get(&spi->dev, "avdd");
180         if (IS_ERR(st->reg))
181                 return PTR_ERR(st->reg);
182
183         ret = regulator_enable(st->reg);
184         if (ret) {
185                 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
186                 return ret;
187         }
188         voltage_uv = regulator_get_voltage(st->reg);
189
190         st->chip_info =
191                 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
192
193         if (voltage_uv)
194                 st->int_vref_mv = voltage_uv / 1000;
195         else
196                 dev_warn(&spi->dev, "Reference voltage unspecified\n");
197
198         spi_set_drvdata(spi, indio_dev);
199
200         indio_dev->dev.parent = &spi->dev;
201         indio_dev->name = spi_get_device_id(spi)->name;
202         indio_dev->modes = INDIO_DIRECT_MODE;
203         indio_dev->channels = &st->chip_info->channel;
204         indio_dev->num_channels = 1;
205         indio_dev->info = &ad7780_info;
206
207         st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
208                                                      "powerdown",
209                                                      GPIOD_OUT_LOW);
210         if (IS_ERR(st->powerdown_gpio)) {
211                 ret = PTR_ERR(st->powerdown_gpio);
212                 dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
213                         ret);
214                 goto error_disable_reg;
215         }
216
217         ret = ad_sd_setup_buffer_and_trigger(indio_dev);
218         if (ret)
219                 goto error_disable_reg;
220
221         ret = iio_device_register(indio_dev);
222         if (ret)
223                 goto error_cleanup_buffer_and_trigger;
224
225         return 0;
226
227 error_cleanup_buffer_and_trigger:
228         ad_sd_cleanup_buffer_and_trigger(indio_dev);
229 error_disable_reg:
230         regulator_disable(st->reg);
231
232         return ret;
233 }
234
235 static int ad7780_remove(struct spi_device *spi)
236 {
237         struct iio_dev *indio_dev = spi_get_drvdata(spi);
238         struct ad7780_state *st = iio_priv(indio_dev);
239
240         iio_device_unregister(indio_dev);
241         ad_sd_cleanup_buffer_and_trigger(indio_dev);
242
243         regulator_disable(st->reg);
244
245         return 0;
246 }
247
248 static const struct spi_device_id ad7780_id[] = {
249         {"ad7170", ID_AD7170},
250         {"ad7171", ID_AD7171},
251         {"ad7780", ID_AD7780},
252         {"ad7781", ID_AD7781},
253         {}
254 };
255 MODULE_DEVICE_TABLE(spi, ad7780_id);
256
257 static struct spi_driver ad7780_driver = {
258         .driver = {
259                 .name   = "ad7780",
260         },
261         .probe          = ad7780_probe,
262         .remove         = ad7780_remove,
263         .id_table       = ad7780_id,
264 };
265 module_spi_driver(ad7780_driver);
266
267 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
268 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
269 MODULE_LICENSE("GPL v2");