2 * AD8366 SPI Dual-Digital Variable Gain Amplifier (VGA)
4 * Copyright 2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
17 #include <linux/bitrev.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
23 struct spi_device
*spi
;
24 struct regulator
*reg
;
27 * DMA (thus cache coherency maintenance) requires the
28 * transfer buffers to live in their own cache lines.
30 unsigned char data
[2] ____cacheline_aligned
;
33 static int ad8366_write(struct iio_dev
*indio_dev
,
34 unsigned char ch_a
, char unsigned ch_b
)
36 struct ad8366_state
*st
= iio_priv(indio_dev
);
39 ch_a
= bitrev8(ch_a
& 0x3F);
40 ch_b
= bitrev8(ch_b
& 0x3F);
42 st
->data
[0] = ch_b
>> 4;
43 st
->data
[1] = (ch_b
<< 4) | (ch_a
>> 2);
45 ret
= spi_write(st
->spi
, st
->data
, ARRAY_SIZE(st
->data
));
47 dev_err(&indio_dev
->dev
, "write failed (%d)", ret
);
52 static int ad8366_read_raw(struct iio_dev
*indio_dev
,
53 struct iio_chan_spec
const *chan
,
58 struct ad8366_state
*st
= iio_priv(indio_dev
);
62 mutex_lock(&indio_dev
->mlock
);
64 case IIO_CHAN_INFO_HARDWAREGAIN
:
65 code
= st
->ch
[chan
->channel
];
68 code
= code
* 253 + 4500;
70 *val2
= (code
% 1000) * 1000;
72 ret
= IIO_VAL_INT_PLUS_MICRO_DB
;
77 mutex_unlock(&indio_dev
->mlock
);
82 static int ad8366_write_raw(struct iio_dev
*indio_dev
,
83 struct iio_chan_spec
const *chan
,
88 struct ad8366_state
*st
= iio_priv(indio_dev
);
92 if (val
< 0 || val2
< 0)
96 code
= (((u8
)val
* 1000) + ((u32
)val2
/ 1000));
98 if (code
> 20500 || code
< 4500)
101 code
= (code
- 4500) / 253;
103 mutex_lock(&indio_dev
->mlock
);
105 case IIO_CHAN_INFO_HARDWAREGAIN
:
106 st
->ch
[chan
->channel
] = code
;
107 ret
= ad8366_write(indio_dev
, st
->ch
[0], st
->ch
[1]);
112 mutex_unlock(&indio_dev
->mlock
);
117 static const struct iio_info ad8366_info
= {
118 .read_raw
= &ad8366_read_raw
,
119 .write_raw
= &ad8366_write_raw
,
120 .driver_module
= THIS_MODULE
,
123 #define AD8366_CHAN(_channel) { \
124 .type = IIO_VOLTAGE, \
127 .channel = _channel, \
128 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
131 static const struct iio_chan_spec ad8366_channels
[] = {
136 static int ad8366_probe(struct spi_device
*spi
)
138 struct iio_dev
*indio_dev
;
139 struct ad8366_state
*st
;
142 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
143 if (indio_dev
== NULL
)
146 st
= iio_priv(indio_dev
);
148 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
149 if (!IS_ERR(st
->reg
)) {
150 ret
= regulator_enable(st
->reg
);
155 spi_set_drvdata(spi
, indio_dev
);
158 indio_dev
->dev
.parent
= &spi
->dev
;
159 indio_dev
->name
= spi_get_device_id(spi
)->name
;
160 indio_dev
->info
= &ad8366_info
;
161 indio_dev
->modes
= INDIO_DIRECT_MODE
;
162 indio_dev
->channels
= ad8366_channels
;
163 indio_dev
->num_channels
= ARRAY_SIZE(ad8366_channels
);
165 ret
= iio_device_register(indio_dev
);
167 goto error_disable_reg
;
169 ad8366_write(indio_dev
, 0 , 0);
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
[] = {
199 static struct spi_driver ad8366_driver
= {
201 .name
= KBUILD_MODNAME
,
202 .owner
= THIS_MODULE
,
204 .probe
= ad8366_probe
,
205 .remove
= ad8366_remove
,
206 .id_table
= ad8366_id
,
209 module_spi_driver(ad8366_driver
);
211 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
212 MODULE_DESCRIPTION("Analog Devices AD8366 VGA");
213 MODULE_LICENSE("GPL v2");