]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/staging/iio/adc/ad7780.c
Merge tag 'iio-for-4.5a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio...
[mirror_ubuntu-zesty-kernel.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 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
91 switch (m) {
92 case IIO_CHAN_INFO_RAW:
93 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
94 case IIO_CHAN_INFO_SCALE:
95 *val = st->int_vref_mv * st->gain;
96 *val2 = chan->scan_type.realbits - 1;
97 return IIO_VAL_FRACTIONAL_LOG2;
98 case IIO_CHAN_INFO_OFFSET:
99 *val -= (1 << (chan->scan_type.realbits - 1));
100 return IIO_VAL_INT;
101 }
102
103 return -EINVAL;
104 }
105
106 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
107 unsigned int raw_sample)
108 {
109 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
110 const struct ad7780_chip_info *chip_info = st->chip_info;
111
112 if ((raw_sample & AD7780_ERR) ||
113 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
114 return -EIO;
115
116 if (raw_sample & AD7780_GAIN)
117 st->gain = 1;
118 else
119 st->gain = 128;
120
121 return 0;
122 }
123
124 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
125 .set_mode = ad7780_set_mode,
126 .postprocess_sample = ad7780_postprocess_sample,
127 .has_registers = false,
128 };
129
130 #define AD7780_CHANNEL(bits, wordsize) \
131 AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
132
133 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
134 [ID_AD7170] = {
135 .channel = AD7780_CHANNEL(12, 24),
136 .pattern = 0x5,
137 .pattern_mask = 0x7,
138 },
139 [ID_AD7171] = {
140 .channel = AD7780_CHANNEL(16, 24),
141 .pattern = 0x5,
142 .pattern_mask = 0x7,
143 },
144 [ID_AD7780] = {
145 .channel = AD7780_CHANNEL(24, 32),
146 .pattern = 0x1,
147 .pattern_mask = 0x3,
148 },
149 [ID_AD7781] = {
150 .channel = AD7780_CHANNEL(20, 32),
151 .pattern = 0x1,
152 .pattern_mask = 0x3,
153 },
154 };
155
156 static const struct iio_info ad7780_info = {
157 .read_raw = &ad7780_read_raw,
158 .driver_module = THIS_MODULE,
159 };
160
161 static int ad7780_probe(struct spi_device *spi)
162 {
163 struct ad7780_state *st;
164 struct iio_dev *indio_dev;
165 int ret, voltage_uv = 0;
166
167 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
168 if (!indio_dev)
169 return -ENOMEM;
170
171 st = iio_priv(indio_dev);
172 st->gain = 1;
173
174 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
175
176 st->reg = devm_regulator_get(&spi->dev, "vcc");
177 if (!IS_ERR(st->reg)) {
178 ret = regulator_enable(st->reg);
179 if (ret)
180 return ret;
181
182 voltage_uv = regulator_get_voltage(st->reg);
183 }
184
185 st->chip_info =
186 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
187
188 if (voltage_uv)
189 st->int_vref_mv = voltage_uv / 1000;
190 else
191 dev_warn(&spi->dev, "Reference voltage unspecified\n");
192
193 spi_set_drvdata(spi, indio_dev);
194
195 indio_dev->dev.parent = &spi->dev;
196 indio_dev->name = spi_get_device_id(spi)->name;
197 indio_dev->modes = INDIO_DIRECT_MODE;
198 indio_dev->channels = &st->chip_info->channel;
199 indio_dev->num_channels = 1;
200 indio_dev->info = &ad7780_info;
201
202 st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
203 "powerdown",
204 GPIOD_OUT_LOW);
205 if (IS_ERR(st->powerdown_gpio)) {
206 ret = PTR_ERR(st->powerdown_gpio);
207 dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
208 ret);
209 goto error_disable_reg;
210 }
211
212 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
213 if (ret)
214 goto error_disable_reg;
215
216 ret = iio_device_register(indio_dev);
217 if (ret)
218 goto error_cleanup_buffer_and_trigger;
219
220 return 0;
221
222 error_cleanup_buffer_and_trigger:
223 ad_sd_cleanup_buffer_and_trigger(indio_dev);
224 error_disable_reg:
225 if (!IS_ERR(st->reg))
226 regulator_disable(st->reg);
227
228 return ret;
229 }
230
231 static int ad7780_remove(struct spi_device *spi)
232 {
233 struct iio_dev *indio_dev = spi_get_drvdata(spi);
234 struct ad7780_state *st = iio_priv(indio_dev);
235
236 iio_device_unregister(indio_dev);
237 ad_sd_cleanup_buffer_and_trigger(indio_dev);
238
239 if (!IS_ERR(st->reg))
240 regulator_disable(st->reg);
241
242 return 0;
243 }
244
245 static const struct spi_device_id ad7780_id[] = {
246 {"ad7170", ID_AD7170},
247 {"ad7171", ID_AD7171},
248 {"ad7780", ID_AD7780},
249 {"ad7781", ID_AD7781},
250 {}
251 };
252 MODULE_DEVICE_TABLE(spi, ad7780_id);
253
254 static struct spi_driver ad7780_driver = {
255 .driver = {
256 .name = "ad7780",
257 },
258 .probe = ad7780_probe,
259 .remove = ad7780_remove,
260 .id_table = ad7780_id,
261 };
262 module_spi_driver(ad7780_driver);
263
264 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
265 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
266 MODULE_LICENSE("GPL v2");