1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AD8366 SPI Dual-Digital Variable Gain Amplifier (VGA) 4 * 5 * Copyright 2012 Analog Devices Inc. 6 */ 7 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> 17 18 #include <linux/iio/iio.h> 19 #include <linux/iio/sysfs.h> 20 21 struct ad8366_state { 22 struct spi_device *spi; 23 struct regulator *reg; 24 unsigned char ch[2]; 25 /* 26 * DMA (thus cache coherency maintenance) requires the 27 * transfer buffers to live in their own cache lines. 28 */ 29 unsigned char data[2] ____cacheline_aligned; 30 }; 31 32 static int ad8366_write(struct iio_dev *indio_dev, 33 unsigned char ch_a, unsigned char ch_b) 34 { 35 struct ad8366_state *st = iio_priv(indio_dev); 36 int ret; 37 38 ch_a = bitrev8(ch_a & 0x3F); 39 ch_b = bitrev8(ch_b & 0x3F); 40 41 st->data[0] = ch_b >> 4; 42 st->data[1] = (ch_b << 4) | (ch_a >> 2); 43 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); 47 48 return ret; 49 } 50 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) 56 { 57 struct ad8366_state *st = iio_priv(indio_dev); 58 int ret; 59 unsigned code; 60 61 mutex_lock(&indio_dev->mlock); 62 switch (m) { 63 case IIO_CHAN_INFO_HARDWAREGAIN: 64 code = st->ch[chan->channel]; 65 66 /* Values in dB */ 67 code = code * 253 + 4500; 68 *val = code / 1000; 69 *val2 = (code % 1000) * 1000; 70 71 ret = IIO_VAL_INT_PLUS_MICRO_DB; 72 break; 73 default: 74 ret = -EINVAL; 75 } 76 mutex_unlock(&indio_dev->mlock); 77 78 return ret; 79 }; 80 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) 86 { 87 struct ad8366_state *st = iio_priv(indio_dev); 88 unsigned code; 89 int ret; 90 91 if (val < 0 || val2 < 0) 92 return -EINVAL; 93 94 /* Values in dB */ 95 code = (((u8)val * 1000) + ((u32)val2 / 1000)); 96 97 if (code > 20500 || code < 4500) 98 return -EINVAL; 99 100 code = (code - 4500) / 253; 101 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; 110 } 111 mutex_unlock(&indio_dev->mlock); 112 113 return ret; 114 } 115 116 static const struct iio_info ad8366_info = { 117 .read_raw = &ad8366_read_raw, 118 .write_raw = &ad8366_write_raw, 119 }; 120 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),\ 127 } 128 129 static const struct iio_chan_spec ad8366_channels[] = { 130 AD8366_CHAN(0), 131 AD8366_CHAN(1), 132 }; 133 134 static int ad8366_probe(struct spi_device *spi) 135 { 136 struct iio_dev *indio_dev; 137 struct ad8366_state *st; 138 int ret; 139 140 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 141 if (indio_dev == NULL) 142 return -ENOMEM; 143 144 st = iio_priv(indio_dev); 145 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; 151 } 152 153 spi_set_drvdata(spi, indio_dev); 154 st->spi = spi; 155 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); 162 163 ret = ad8366_write(indio_dev, 0 , 0); 164 if (ret < 0) 165 goto error_disable_reg; 166 167 ret = iio_device_register(indio_dev); 168 if (ret) 169 goto error_disable_reg; 170 171 return 0; 172 173 error_disable_reg: 174 if (!IS_ERR(st->reg)) 175 regulator_disable(st->reg); 176 177 return ret; 178 } 179 180 static int ad8366_remove(struct spi_device *spi) 181 { 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; 185 186 iio_device_unregister(indio_dev); 187 188 if (!IS_ERR(reg)) 189 regulator_disable(reg); 190 191 return 0; 192 } 193 194 static const struct spi_device_id ad8366_id[] = { 195 {"ad8366", 0}, 196 {} 197 }; 198 MODULE_DEVICE_TABLE(spi, ad8366_id); 199 200 static struct spi_driver ad8366_driver = { 201 .driver = { 202 .name = KBUILD_MODNAME, 203 }, 204 .probe = ad8366_probe, 205 .remove = ad8366_remove, 206 .id_table = ad8366_id, 207 }; 208 209 module_spi_driver(ad8366_driver); 210 211 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 212 MODULE_DESCRIPTION("Analog Devices AD8366 VGA"); 213 MODULE_LICENSE("GPL v2"); 214