xref: /openbmc/linux/drivers/iio/amplifiers/ad8366.c (revision b830f94f)
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  *
9  * Copyright 2012-2019 Analog Devices Inc.
10  */
11 
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/bitrev.h>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 
26 enum ad8366_type {
27 	ID_AD8366,
28 	ID_ADA4961,
29 	ID_ADL5240,
30 };
31 
32 struct ad8366_info {
33 	int gain_min;
34 	int gain_max;
35 };
36 
37 struct ad8366_state {
38 	struct spi_device	*spi;
39 	struct regulator	*reg;
40 	struct mutex            lock; /* protect sensor state */
41 	struct gpio_desc	*reset_gpio;
42 	unsigned char		ch[2];
43 	enum ad8366_type	type;
44 	struct ad8366_info	*info;
45 	/*
46 	 * DMA (thus cache coherency maintenance) requires the
47 	 * transfer buffers to live in their own cache lines.
48 	 */
49 	unsigned char		data[2] ____cacheline_aligned;
50 };
51 
52 static struct ad8366_info ad8366_infos[] = {
53 	[ID_AD8366] = {
54 		.gain_min = 4500,
55 		.gain_max = 20500,
56 	},
57 	[ID_ADA4961] = {
58 		.gain_min = -6000,
59 		.gain_max = 15000,
60 	},
61 	[ID_ADL5240] = {
62 		.gain_min = -11500,
63 		.gain_max = 20000,
64 	},
65 };
66 
67 static int ad8366_write(struct iio_dev *indio_dev,
68 			unsigned char ch_a, unsigned char ch_b)
69 {
70 	struct ad8366_state *st = iio_priv(indio_dev);
71 	int ret;
72 
73 	switch (st->type) {
74 	case ID_AD8366:
75 		ch_a = bitrev8(ch_a & 0x3F);
76 		ch_b = bitrev8(ch_b & 0x3F);
77 
78 		st->data[0] = ch_b >> 4;
79 		st->data[1] = (ch_b << 4) | (ch_a >> 2);
80 		break;
81 	case ID_ADA4961:
82 		st->data[0] = ch_a & 0x1F;
83 		break;
84 	case ID_ADL5240:
85 		st->data[0] = (ch_a & 0x3F);
86 		break;
87 	}
88 
89 	ret = spi_write(st->spi, st->data, indio_dev->num_channels);
90 	if (ret < 0)
91 		dev_err(&indio_dev->dev, "write failed (%d)", ret);
92 
93 	return ret;
94 }
95 
96 static int ad8366_read_raw(struct iio_dev *indio_dev,
97 			   struct iio_chan_spec const *chan,
98 			   int *val,
99 			   int *val2,
100 			   long m)
101 {
102 	struct ad8366_state *st = iio_priv(indio_dev);
103 	int ret;
104 	int code, gain = 0;
105 
106 	mutex_lock(&st->lock);
107 	switch (m) {
108 	case IIO_CHAN_INFO_HARDWAREGAIN:
109 		code = st->ch[chan->channel];
110 
111 		switch (st->type) {
112 		case ID_AD8366:
113 			gain = code * 253 + 4500;
114 			break;
115 		case ID_ADA4961:
116 			gain = 15000 - code * 1000;
117 			break;
118 		case ID_ADL5240:
119 			gain = 20000 - 31500 + code * 500;
120 			break;
121 		}
122 
123 		/* Values in dB */
124 		*val = gain / 1000;
125 		*val2 = (gain % 1000) * 1000;
126 
127 		ret = IIO_VAL_INT_PLUS_MICRO_DB;
128 		break;
129 	default:
130 		ret = -EINVAL;
131 	}
132 	mutex_unlock(&st->lock);
133 
134 	return ret;
135 };
136 
137 static int ad8366_write_raw(struct iio_dev *indio_dev,
138 			    struct iio_chan_spec const *chan,
139 			    int val,
140 			    int val2,
141 			    long mask)
142 {
143 	struct ad8366_state *st = iio_priv(indio_dev);
144 	struct ad8366_info *inf = st->info;
145 	int code = 0, gain;
146 	int ret;
147 
148 	/* Values in dB */
149 	if (val < 0)
150 		gain = (val * 1000) - (val2 / 1000);
151 	else
152 		gain = (val * 1000) + (val2 / 1000);
153 
154 	if (gain > inf->gain_max || gain < inf->gain_min)
155 		return -EINVAL;
156 
157 	switch (st->type) {
158 	case ID_AD8366:
159 		code = (gain - 4500) / 253;
160 		break;
161 	case ID_ADA4961:
162 		code = (15000 - gain) / 1000;
163 		break;
164 	case ID_ADL5240:
165 		code = ((gain - 500 - 20000) / 500) & 0x3F;
166 		break;
167 	}
168 
169 	mutex_lock(&st->lock);
170 	switch (mask) {
171 	case IIO_CHAN_INFO_HARDWAREGAIN:
172 		st->ch[chan->channel] = code;
173 		ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
174 		break;
175 	default:
176 		ret = -EINVAL;
177 	}
178 	mutex_unlock(&st->lock);
179 
180 	return ret;
181 }
182 
183 static const struct iio_info ad8366_info = {
184 	.read_raw = &ad8366_read_raw,
185 	.write_raw = &ad8366_write_raw,
186 };
187 
188 #define AD8366_CHAN(_channel) {				\
189 	.type = IIO_VOLTAGE,				\
190 	.output = 1,					\
191 	.indexed = 1,					\
192 	.channel = _channel,				\
193 	.info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
194 }
195 
196 static const struct iio_chan_spec ad8366_channels[] = {
197 	AD8366_CHAN(0),
198 	AD8366_CHAN(1),
199 };
200 
201 static const struct iio_chan_spec ada4961_channels[] = {
202 	AD8366_CHAN(0),
203 };
204 
205 static int ad8366_probe(struct spi_device *spi)
206 {
207 	struct iio_dev *indio_dev;
208 	struct ad8366_state *st;
209 	int ret;
210 
211 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
212 	if (indio_dev == NULL)
213 		return -ENOMEM;
214 
215 	st = iio_priv(indio_dev);
216 
217 	st->reg = devm_regulator_get(&spi->dev, "vcc");
218 	if (!IS_ERR(st->reg)) {
219 		ret = regulator_enable(st->reg);
220 		if (ret)
221 			return ret;
222 	}
223 
224 	spi_set_drvdata(spi, indio_dev);
225 	mutex_init(&st->lock);
226 	st->spi = spi;
227 	st->type = spi_get_device_id(spi)->driver_data;
228 
229 	switch (st->type) {
230 	case ID_AD8366:
231 		indio_dev->channels = ad8366_channels;
232 		indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
233 		break;
234 	case ID_ADA4961:
235 	case ID_ADL5240:
236 		st->reset_gpio = devm_gpiod_get(&spi->dev, "reset",
237 			GPIOD_OUT_HIGH);
238 		indio_dev->channels = ada4961_channels;
239 		indio_dev->num_channels = ARRAY_SIZE(ada4961_channels);
240 		break;
241 	default:
242 		dev_err(&spi->dev, "Invalid device ID\n");
243 		ret = -EINVAL;
244 		goto error_disable_reg;
245 	}
246 
247 	st->info = &ad8366_infos[st->type];
248 	indio_dev->dev.parent = &spi->dev;
249 	indio_dev->name = spi_get_device_id(spi)->name;
250 	indio_dev->info = &ad8366_info;
251 	indio_dev->modes = INDIO_DIRECT_MODE;
252 
253 	ret = ad8366_write(indio_dev, 0 , 0);
254 	if (ret < 0)
255 		goto error_disable_reg;
256 
257 	ret = iio_device_register(indio_dev);
258 	if (ret)
259 		goto error_disable_reg;
260 
261 	return 0;
262 
263 error_disable_reg:
264 	if (!IS_ERR(st->reg))
265 		regulator_disable(st->reg);
266 
267 	return ret;
268 }
269 
270 static int ad8366_remove(struct spi_device *spi)
271 {
272 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
273 	struct ad8366_state *st = iio_priv(indio_dev);
274 	struct regulator *reg = st->reg;
275 
276 	iio_device_unregister(indio_dev);
277 
278 	if (!IS_ERR(reg))
279 		regulator_disable(reg);
280 
281 	return 0;
282 }
283 
284 static const struct spi_device_id ad8366_id[] = {
285 	{"ad8366",  ID_AD8366},
286 	{"ada4961", ID_ADA4961},
287 	{"adl5240", ID_ADL5240},
288 	{}
289 };
290 MODULE_DEVICE_TABLE(spi, ad8366_id);
291 
292 static struct spi_driver ad8366_driver = {
293 	.driver = {
294 		.name	= KBUILD_MODNAME,
295 	},
296 	.probe		= ad8366_probe,
297 	.remove		= ad8366_remove,
298 	.id_table	= ad8366_id,
299 };
300 
301 module_spi_driver(ad8366_driver);
302 
303 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
304 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
305 MODULE_LICENSE("GPL v2");
306