1 /* 2 * AD8366 SPI Dual-Digital Variable Gain Amplifier (VGA) 3 * 4 * Copyright 2012 Analog Devices Inc. 5 * 6 * Licensed under the GPL-2. 7 */ 8 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> 18 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 22 struct ad8366_state { 23 struct spi_device *spi; 24 struct regulator *reg; 25 unsigned char ch[2]; 26 /* 27 * DMA (thus cache coherency maintenance) requires the 28 * transfer buffers to live in their own cache lines. 29 */ 30 unsigned char data[2] ____cacheline_aligned; 31 }; 32 33 static int ad8366_write(struct iio_dev *indio_dev, 34 unsigned char ch_a, unsigned char ch_b) 35 { 36 struct ad8366_state *st = iio_priv(indio_dev); 37 int ret; 38 39 ch_a = bitrev8(ch_a & 0x3F); 40 ch_b = bitrev8(ch_b & 0x3F); 41 42 st->data[0] = ch_b >> 4; 43 st->data[1] = (ch_b << 4) | (ch_a >> 2); 44 45 ret = spi_write(st->spi, st->data, ARRAY_SIZE(st->data)); 46 if (ret < 0) 47 dev_err(&indio_dev->dev, "write failed (%d)", ret); 48 49 return ret; 50 } 51 52 static int ad8366_read_raw(struct iio_dev *indio_dev, 53 struct iio_chan_spec const *chan, 54 int *val, 55 int *val2, 56 long m) 57 { 58 struct ad8366_state *st = iio_priv(indio_dev); 59 int ret; 60 unsigned code; 61 62 mutex_lock(&indio_dev->mlock); 63 switch (m) { 64 case IIO_CHAN_INFO_HARDWAREGAIN: 65 code = st->ch[chan->channel]; 66 67 /* Values in dB */ 68 code = code * 253 + 4500; 69 *val = code / 1000; 70 *val2 = (code % 1000) * 1000; 71 72 ret = IIO_VAL_INT_PLUS_MICRO_DB; 73 break; 74 default: 75 ret = -EINVAL; 76 } 77 mutex_unlock(&indio_dev->mlock); 78 79 return ret; 80 }; 81 82 static int ad8366_write_raw(struct iio_dev *indio_dev, 83 struct iio_chan_spec const *chan, 84 int val, 85 int val2, 86 long mask) 87 { 88 struct ad8366_state *st = iio_priv(indio_dev); 89 unsigned code; 90 int ret; 91 92 if (val < 0 || val2 < 0) 93 return -EINVAL; 94 95 /* Values in dB */ 96 code = (((u8)val * 1000) + ((u32)val2 / 1000)); 97 98 if (code > 20500 || code < 4500) 99 return -EINVAL; 100 101 code = (code - 4500) / 253; 102 103 mutex_lock(&indio_dev->mlock); 104 switch (mask) { 105 case IIO_CHAN_INFO_HARDWAREGAIN: 106 st->ch[chan->channel] = code; 107 ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]); 108 break; 109 default: 110 ret = -EINVAL; 111 } 112 mutex_unlock(&indio_dev->mlock); 113 114 return ret; 115 } 116 117 static const struct iio_info ad8366_info = { 118 .read_raw = &ad8366_read_raw, 119 .write_raw = &ad8366_write_raw, 120 }; 121 122 #define AD8366_CHAN(_channel) { \ 123 .type = IIO_VOLTAGE, \ 124 .output = 1, \ 125 .indexed = 1, \ 126 .channel = _channel, \ 127 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\ 128 } 129 130 static const struct iio_chan_spec ad8366_channels[] = { 131 AD8366_CHAN(0), 132 AD8366_CHAN(1), 133 }; 134 135 static int ad8366_probe(struct spi_device *spi) 136 { 137 struct iio_dev *indio_dev; 138 struct ad8366_state *st; 139 int ret; 140 141 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 142 if (indio_dev == NULL) 143 return -ENOMEM; 144 145 st = iio_priv(indio_dev); 146 147 st->reg = devm_regulator_get(&spi->dev, "vcc"); 148 if (!IS_ERR(st->reg)) { 149 ret = regulator_enable(st->reg); 150 if (ret) 151 return ret; 152 } 153 154 spi_set_drvdata(spi, indio_dev); 155 st->spi = spi; 156 157 indio_dev->dev.parent = &spi->dev; 158 indio_dev->name = spi_get_device_id(spi)->name; 159 indio_dev->info = &ad8366_info; 160 indio_dev->modes = INDIO_DIRECT_MODE; 161 indio_dev->channels = ad8366_channels; 162 indio_dev->num_channels = ARRAY_SIZE(ad8366_channels); 163 164 ret = iio_device_register(indio_dev); 165 if (ret) 166 goto error_disable_reg; 167 168 ad8366_write(indio_dev, 0, 0); 169 170 return 0; 171 172 error_disable_reg: 173 if (!IS_ERR(st->reg)) 174 regulator_disable(st->reg); 175 176 return ret; 177 } 178 179 static int ad8366_remove(struct spi_device *spi) 180 { 181 struct iio_dev *indio_dev = spi_get_drvdata(spi); 182 struct ad8366_state *st = iio_priv(indio_dev); 183 struct regulator *reg = st->reg; 184 185 iio_device_unregister(indio_dev); 186 187 if (!IS_ERR(reg)) 188 regulator_disable(reg); 189 190 return 0; 191 } 192 193 static const struct spi_device_id ad8366_id[] = { 194 {"ad8366", 0}, 195 {} 196 }; 197 MODULE_DEVICE_TABLE(spi, ad8366_id); 198 199 static struct spi_driver ad8366_driver = { 200 .driver = { 201 .name = KBUILD_MODNAME, 202 }, 203 .probe = ad8366_probe, 204 .remove = ad8366_remove, 205 .id_table = ad8366_id, 206 }; 207 208 module_spi_driver(ad8366_driver); 209 210 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 211 MODULE_DESCRIPTION("Analog Devices AD8366 VGA"); 212 MODULE_LICENSE("GPL v2"); 213