Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / amplifiers / ad8366.c
blobcfcf18a0bce8517a20c14b95cc11a84ace53fff7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD8366 and similar Gain Amplifiers
4 * This driver supports the following gain amplifiers:
5 * AD8366 Dual-Digital Variable Gain Amplifier (VGA)
6 * ADA4961 BiCMOS RF Digital Gain Amplifier (DGA)
7 * ADL5240 Digitally controlled variable gain amplifier (VGA)
8 * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator
10 * Copyright 2012-2019 Analog Devices Inc.
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/spi/spi.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/bitrev.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
27 enum ad8366_type {
28 ID_AD8366,
29 ID_ADA4961,
30 ID_ADL5240,
31 ID_HMC1119,
34 struct ad8366_info {
35 int gain_min;
36 int gain_max;
39 struct ad8366_state {
40 struct spi_device *spi;
41 struct regulator *reg;
42 struct mutex lock; /* protect sensor state */
43 struct gpio_desc *reset_gpio;
44 unsigned char ch[2];
45 enum ad8366_type type;
46 struct ad8366_info *info;
48 * DMA (thus cache coherency maintenance) requires the
49 * transfer buffers to live in their own cache lines.
51 unsigned char data[2] ____cacheline_aligned;
54 static struct ad8366_info ad8366_infos[] = {
55 [ID_AD8366] = {
56 .gain_min = 4500,
57 .gain_max = 20500,
59 [ID_ADA4961] = {
60 .gain_min = -6000,
61 .gain_max = 15000,
63 [ID_ADL5240] = {
64 .gain_min = -11500,
65 .gain_max = 20000,
67 [ID_HMC1119] = {
68 .gain_min = -31750,
69 .gain_max = 0,
73 static int ad8366_write(struct iio_dev *indio_dev,
74 unsigned char ch_a, unsigned char ch_b)
76 struct ad8366_state *st = iio_priv(indio_dev);
77 int ret;
79 switch (st->type) {
80 case ID_AD8366:
81 ch_a = bitrev8(ch_a & 0x3F);
82 ch_b = bitrev8(ch_b & 0x3F);
84 st->data[0] = ch_b >> 4;
85 st->data[1] = (ch_b << 4) | (ch_a >> 2);
86 break;
87 case ID_ADA4961:
88 st->data[0] = ch_a & 0x1F;
89 break;
90 case ID_ADL5240:
91 st->data[0] = (ch_a & 0x3F);
92 break;
93 case ID_HMC1119:
94 st->data[0] = ch_a;
95 break;
98 ret = spi_write(st->spi, st->data, indio_dev->num_channels);
99 if (ret < 0)
100 dev_err(&indio_dev->dev, "write failed (%d)", ret);
102 return ret;
105 static int ad8366_read_raw(struct iio_dev *indio_dev,
106 struct iio_chan_spec const *chan,
107 int *val,
108 int *val2,
109 long m)
111 struct ad8366_state *st = iio_priv(indio_dev);
112 int ret;
113 int code, gain = 0;
115 mutex_lock(&st->lock);
116 switch (m) {
117 case IIO_CHAN_INFO_HARDWAREGAIN:
118 code = st->ch[chan->channel];
120 switch (st->type) {
121 case ID_AD8366:
122 gain = code * 253 + 4500;
123 break;
124 case ID_ADA4961:
125 gain = 15000 - code * 1000;
126 break;
127 case ID_ADL5240:
128 gain = 20000 - 31500 + code * 500;
129 break;
130 case ID_HMC1119:
131 gain = -1 * code * 250;
132 break;
135 /* Values in dB */
136 *val = gain / 1000;
137 *val2 = (gain % 1000) * 1000;
139 ret = IIO_VAL_INT_PLUS_MICRO_DB;
140 break;
141 default:
142 ret = -EINVAL;
144 mutex_unlock(&st->lock);
146 return ret;
149 static int ad8366_write_raw(struct iio_dev *indio_dev,
150 struct iio_chan_spec const *chan,
151 int val,
152 int val2,
153 long mask)
155 struct ad8366_state *st = iio_priv(indio_dev);
156 struct ad8366_info *inf = st->info;
157 int code = 0, gain;
158 int ret;
160 /* Values in dB */
161 if (val < 0)
162 gain = (val * 1000) - (val2 / 1000);
163 else
164 gain = (val * 1000) + (val2 / 1000);
166 if (gain > inf->gain_max || gain < inf->gain_min)
167 return -EINVAL;
169 switch (st->type) {
170 case ID_AD8366:
171 code = (gain - 4500) / 253;
172 break;
173 case ID_ADA4961:
174 code = (15000 - gain) / 1000;
175 break;
176 case ID_ADL5240:
177 code = ((gain - 500 - 20000) / 500) & 0x3F;
178 break;
179 case ID_HMC1119:
180 code = (abs(gain) / 250) & 0x7F;
181 break;
184 mutex_lock(&st->lock);
185 switch (mask) {
186 case IIO_CHAN_INFO_HARDWAREGAIN:
187 st->ch[chan->channel] = code;
188 ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
189 break;
190 default:
191 ret = -EINVAL;
193 mutex_unlock(&st->lock);
195 return ret;
198 static int ad8366_write_raw_get_fmt(struct iio_dev *indio_dev,
199 struct iio_chan_spec const *chan,
200 long mask)
202 switch (mask) {
203 case IIO_CHAN_INFO_HARDWAREGAIN:
204 return IIO_VAL_INT_PLUS_MICRO_DB;
205 default:
206 return -EINVAL;
210 static const struct iio_info ad8366_info = {
211 .read_raw = &ad8366_read_raw,
212 .write_raw = &ad8366_write_raw,
213 .write_raw_get_fmt = &ad8366_write_raw_get_fmt,
216 #define AD8366_CHAN(_channel) { \
217 .type = IIO_VOLTAGE, \
218 .output = 1, \
219 .indexed = 1, \
220 .channel = _channel, \
221 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
224 static const struct iio_chan_spec ad8366_channels[] = {
225 AD8366_CHAN(0),
226 AD8366_CHAN(1),
229 static const struct iio_chan_spec ada4961_channels[] = {
230 AD8366_CHAN(0),
233 static int ad8366_probe(struct spi_device *spi)
235 struct iio_dev *indio_dev;
236 struct ad8366_state *st;
237 int ret;
239 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
240 if (indio_dev == NULL)
241 return -ENOMEM;
243 st = iio_priv(indio_dev);
245 st->reg = devm_regulator_get(&spi->dev, "vcc");
246 if (!IS_ERR(st->reg)) {
247 ret = regulator_enable(st->reg);
248 if (ret)
249 return ret;
252 spi_set_drvdata(spi, indio_dev);
253 mutex_init(&st->lock);
254 st->spi = spi;
255 st->type = spi_get_device_id(spi)->driver_data;
257 switch (st->type) {
258 case ID_AD8366:
259 indio_dev->channels = ad8366_channels;
260 indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
261 break;
262 case ID_ADA4961:
263 case ID_ADL5240:
264 case ID_HMC1119:
265 st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_HIGH);
266 if (IS_ERR(st->reset_gpio)) {
267 ret = PTR_ERR(st->reset_gpio);
268 goto error_disable_reg;
270 indio_dev->channels = ada4961_channels;
271 indio_dev->num_channels = ARRAY_SIZE(ada4961_channels);
272 break;
273 default:
274 dev_err(&spi->dev, "Invalid device ID\n");
275 ret = -EINVAL;
276 goto error_disable_reg;
279 st->info = &ad8366_infos[st->type];
280 indio_dev->name = spi_get_device_id(spi)->name;
281 indio_dev->info = &ad8366_info;
282 indio_dev->modes = INDIO_DIRECT_MODE;
284 ret = ad8366_write(indio_dev, 0 , 0);
285 if (ret < 0)
286 goto error_disable_reg;
288 ret = iio_device_register(indio_dev);
289 if (ret)
290 goto error_disable_reg;
292 return 0;
294 error_disable_reg:
295 if (!IS_ERR(st->reg))
296 regulator_disable(st->reg);
298 return ret;
301 static int ad8366_remove(struct spi_device *spi)
303 struct iio_dev *indio_dev = spi_get_drvdata(spi);
304 struct ad8366_state *st = iio_priv(indio_dev);
305 struct regulator *reg = st->reg;
307 iio_device_unregister(indio_dev);
309 if (!IS_ERR(reg))
310 regulator_disable(reg);
312 return 0;
315 static const struct spi_device_id ad8366_id[] = {
316 {"ad8366", ID_AD8366},
317 {"ada4961", ID_ADA4961},
318 {"adl5240", ID_ADL5240},
319 {"hmc1119", ID_HMC1119},
322 MODULE_DEVICE_TABLE(spi, ad8366_id);
324 static struct spi_driver ad8366_driver = {
325 .driver = {
326 .name = KBUILD_MODNAME,
328 .probe = ad8366_probe,
329 .remove = ad8366_remove,
330 .id_table = ad8366_id,
333 module_spi_driver(ad8366_driver);
335 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
336 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
337 MODULE_LICENSE("GPL v2");