Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / amplifiers / ad8366.c
blob3d6246f8642933bcf069ceb55827e8a27f0f3f7a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AD8366 SPI Dual-Digital Variable Gain Amplifier (VGA)
5 * Copyright 2012 Analog Devices Inc.
6 */
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/sysfs.h>
12 #include <linux/spi/spi.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/bitrev.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 struct ad8366_state {
22 struct spi_device *spi;
23 struct regulator *reg;
24 unsigned char ch[2];
26 * DMA (thus cache coherency maintenance) requires the
27 * transfer buffers to live in their own cache lines.
29 unsigned char data[2] ____cacheline_aligned;
32 static int ad8366_write(struct iio_dev *indio_dev,
33 unsigned char ch_a, unsigned char ch_b)
35 struct ad8366_state *st = iio_priv(indio_dev);
36 int ret;
38 ch_a = bitrev8(ch_a & 0x3F);
39 ch_b = bitrev8(ch_b & 0x3F);
41 st->data[0] = ch_b >> 4;
42 st->data[1] = (ch_b << 4) | (ch_a >> 2);
44 ret = spi_write(st->spi, st->data, ARRAY_SIZE(st->data));
45 if (ret < 0)
46 dev_err(&indio_dev->dev, "write failed (%d)", ret);
48 return ret;
51 static int ad8366_read_raw(struct iio_dev *indio_dev,
52 struct iio_chan_spec const *chan,
53 int *val,
54 int *val2,
55 long m)
57 struct ad8366_state *st = iio_priv(indio_dev);
58 int ret;
59 unsigned code;
61 mutex_lock(&indio_dev->mlock);
62 switch (m) {
63 case IIO_CHAN_INFO_HARDWAREGAIN:
64 code = st->ch[chan->channel];
66 /* Values in dB */
67 code = code * 253 + 4500;
68 *val = code / 1000;
69 *val2 = (code % 1000) * 1000;
71 ret = IIO_VAL_INT_PLUS_MICRO_DB;
72 break;
73 default:
74 ret = -EINVAL;
76 mutex_unlock(&indio_dev->mlock);
78 return ret;
81 static int ad8366_write_raw(struct iio_dev *indio_dev,
82 struct iio_chan_spec const *chan,
83 int val,
84 int val2,
85 long mask)
87 struct ad8366_state *st = iio_priv(indio_dev);
88 unsigned code;
89 int ret;
91 if (val < 0 || val2 < 0)
92 return -EINVAL;
94 /* Values in dB */
95 code = (((u8)val * 1000) + ((u32)val2 / 1000));
97 if (code > 20500 || code < 4500)
98 return -EINVAL;
100 code = (code - 4500) / 253;
102 mutex_lock(&indio_dev->mlock);
103 switch (mask) {
104 case IIO_CHAN_INFO_HARDWAREGAIN:
105 st->ch[chan->channel] = code;
106 ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
107 break;
108 default:
109 ret = -EINVAL;
111 mutex_unlock(&indio_dev->mlock);
113 return ret;
116 static const struct iio_info ad8366_info = {
117 .read_raw = &ad8366_read_raw,
118 .write_raw = &ad8366_write_raw,
121 #define AD8366_CHAN(_channel) { \
122 .type = IIO_VOLTAGE, \
123 .output = 1, \
124 .indexed = 1, \
125 .channel = _channel, \
126 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
129 static const struct iio_chan_spec ad8366_channels[] = {
130 AD8366_CHAN(0),
131 AD8366_CHAN(1),
134 static int ad8366_probe(struct spi_device *spi)
136 struct iio_dev *indio_dev;
137 struct ad8366_state *st;
138 int ret;
140 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
141 if (indio_dev == NULL)
142 return -ENOMEM;
144 st = iio_priv(indio_dev);
146 st->reg = devm_regulator_get(&spi->dev, "vcc");
147 if (!IS_ERR(st->reg)) {
148 ret = regulator_enable(st->reg);
149 if (ret)
150 return ret;
153 spi_set_drvdata(spi, indio_dev);
154 st->spi = spi;
156 indio_dev->dev.parent = &spi->dev;
157 indio_dev->name = spi_get_device_id(spi)->name;
158 indio_dev->info = &ad8366_info;
159 indio_dev->modes = INDIO_DIRECT_MODE;
160 indio_dev->channels = ad8366_channels;
161 indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
163 ret = ad8366_write(indio_dev, 0 , 0);
164 if (ret < 0)
165 goto error_disable_reg;
167 ret = iio_device_register(indio_dev);
168 if (ret)
169 goto error_disable_reg;
171 return 0;
173 error_disable_reg:
174 if (!IS_ERR(st->reg))
175 regulator_disable(st->reg);
177 return ret;
180 static int ad8366_remove(struct spi_device *spi)
182 struct iio_dev *indio_dev = spi_get_drvdata(spi);
183 struct ad8366_state *st = iio_priv(indio_dev);
184 struct regulator *reg = st->reg;
186 iio_device_unregister(indio_dev);
188 if (!IS_ERR(reg))
189 regulator_disable(reg);
191 return 0;
194 static const struct spi_device_id ad8366_id[] = {
195 {"ad8366", 0},
198 MODULE_DEVICE_TABLE(spi, ad8366_id);
200 static struct spi_driver ad8366_driver = {
201 .driver = {
202 .name = KBUILD_MODNAME,
204 .probe = ad8366_probe,
205 .remove = ad8366_remove,
206 .id_table = ad8366_id,
209 module_spi_driver(ad8366_driver);
211 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
212 MODULE_DESCRIPTION("Analog Devices AD8366 VGA");
213 MODULE_LICENSE("GPL v2");