1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD8366 SPI Dual-Digital Variable Gain Amplifier (VGA)
5 * Copyright 2012 Analog Devices Inc.
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>
22 struct spi_device
*spi
;
23 struct regulator
*reg
;
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
);
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
));
46 dev_err(&indio_dev
->dev
, "write failed (%d)", ret
);
51 static int ad8366_read_raw(struct iio_dev
*indio_dev
,
52 struct iio_chan_spec
const *chan
,
57 struct ad8366_state
*st
= iio_priv(indio_dev
);
61 mutex_lock(&indio_dev
->mlock
);
63 case IIO_CHAN_INFO_HARDWAREGAIN
:
64 code
= st
->ch
[chan
->channel
];
67 code
= code
* 253 + 4500;
69 *val2
= (code
% 1000) * 1000;
71 ret
= IIO_VAL_INT_PLUS_MICRO_DB
;
76 mutex_unlock(&indio_dev
->mlock
);
81 static int ad8366_write_raw(struct iio_dev
*indio_dev
,
82 struct iio_chan_spec
const *chan
,
87 struct ad8366_state
*st
= iio_priv(indio_dev
);
91 if (val
< 0 || val2
< 0)
95 code
= (((u8
)val
* 1000) + ((u32
)val2
/ 1000));
97 if (code
> 20500 || code
< 4500)
100 code
= (code
- 4500) / 253;
102 mutex_lock(&indio_dev
->mlock
);
104 case IIO_CHAN_INFO_HARDWAREGAIN
:
105 st
->ch
[chan
->channel
] = code
;
106 ret
= ad8366_write(indio_dev
, st
->ch
[0], st
->ch
[1]);
111 mutex_unlock(&indio_dev
->mlock
);
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, \
125 .channel = _channel, \
126 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
129 static const struct iio_chan_spec ad8366_channels
[] = {
134 static int ad8366_probe(struct spi_device
*spi
)
136 struct iio_dev
*indio_dev
;
137 struct ad8366_state
*st
;
140 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
141 if (indio_dev
== NULL
)
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
);
153 spi_set_drvdata(spi
, indio_dev
);
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);
165 goto error_disable_reg
;
167 ret
= iio_device_register(indio_dev
);
169 goto error_disable_reg
;
174 if (!IS_ERR(st
->reg
))
175 regulator_disable(st
->reg
);
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
);
189 regulator_disable(reg
);
194 static const struct spi_device_id ad8366_id
[] = {
198 MODULE_DEVICE_TABLE(spi
, ad8366_id
);
200 static struct spi_driver ad8366_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");