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 * ADRF5702: 0.125 dB LSB, 8-Bit, Silicon Digital Attenuator, 50 MHz to 20 GHz 9 * ADRF5703: 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator, 9 kHz to 20 GHz 10 * ADRF5720: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator, 9 kHz to 40 GHz 11 * ADRF5730: 0.5 dB LSB, 6-Bit, Silicon Digital Attenuator, 100 MHz to 40 GHz 12 * ADRF5731: 2 dB LSB, 4-Bit, Silicon Digital Attenuator, 100 MHz to 40 GHz 13 * HMC271A: 1dB LSB 5-Bit Digital Attenuator SMT, 0.7 - 3.7 GHz 14 * HMC792A 0.25 dB LSB GaAs MMIC 6-Bit Digital Attenuator 15 * HMC1018A: 1.0 dB LSB GaAs MMIC 5-BIT DIGITAL ATTENUATOR, 0.1 - 30 GHz 16 * HMC1019A: 0.5 dB LSB GaAs MMIC 5-BIT DIGITAL ATTENUATOR, 0.1 - 30 GHz 17 * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator 18 * 19 * Copyright 2012-2026 Analog Devices Inc. 20 */ 21 22 #include <linux/bitrev.h> 23 #include <linux/bits.h> 24 #include <linux/dev_printk.h> 25 #include <linux/err.h> 26 #include <linux/gpio/consumer.h> 27 #include <linux/math.h> 28 #include <linux/minmax.h> 29 #include <linux/module.h> 30 #include <linux/mutex.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/reset.h> 33 #include <linux/spi/spi.h> 34 #include <linux/types.h> 35 #include <linux/unaligned.h> 36 37 #include <linux/iio/iio.h> 38 39 struct ad8366_info { 40 const char *name; 41 int gain_min; 42 int gain_max; 43 int gain_step; 44 size_t num_channels; 45 size_t (*pack_code)(const unsigned char *code, size_t num_channels, 46 unsigned char *data); 47 }; 48 49 struct ad8366_state { 50 struct spi_device *spi; 51 struct mutex lock; /* protect sensor state */ 52 unsigned char ch[2]; 53 const struct ad8366_info *info; 54 /* 55 * DMA (thus cache coherency maintenance) may require the 56 * transfer buffers to live in their own cache lines. 57 */ 58 unsigned char data[2] __aligned(IIO_DMA_MINALIGN); 59 }; 60 61 static size_t ad8366_pack_code(const unsigned char *code, size_t num_channels, 62 unsigned char *data) 63 { 64 u8 ch_a = bitrev8(code[0]) >> 2; 65 u8 ch_b = bitrev8(code[1]) >> 2; 66 67 put_unaligned_be16((ch_b << 6) | ch_a, &data[0]); 68 return sizeof(__be16); 69 } 70 71 static size_t adrf5731_pack_code(const unsigned char *code, size_t num_channels, 72 unsigned char *data) 73 { 74 data[0] = code[0] << 2; 75 return 1; 76 } 77 78 static size_t hmc271_pack_code(const unsigned char *code, size_t num_channels, 79 unsigned char *data) 80 { 81 data[0] = bitrev8(code[0]) >> 3; 82 return 1; 83 } 84 85 static const struct ad8366_info ad8366_chip_info = { 86 .name = "ad8366", 87 .gain_min = 4500, 88 .gain_max = 20500, 89 .gain_step = 253, 90 .num_channels = 2, 91 .pack_code = ad8366_pack_code, 92 }; 93 94 static const struct ad8366_info ada4961_chip_info = { 95 .name = "ada4961", 96 .gain_min = -6000, 97 .gain_max = 15000, 98 .gain_step = -1000, 99 .num_channels = 1, 100 }; 101 102 static const struct ad8366_info adl5240_chip_info = { 103 .name = "adl5240", 104 .gain_min = -11500, 105 .gain_max = 20000, 106 .gain_step = 500, 107 .num_channels = 1, 108 }; 109 110 static const struct ad8366_info adrf5702_chip_info = { 111 .name = "adrf5702", 112 .gain_min = -31875, 113 .gain_max = 0, 114 .gain_step = -125, 115 .num_channels = 1, 116 }; 117 118 static const struct ad8366_info adrf5703_chip_info = { 119 .name = "adrf5703", 120 .gain_min = -31750, 121 .gain_max = 0, 122 .gain_step = -250, 123 .num_channels = 1, 124 }; 125 126 static const struct ad8366_info adrf5720_chip_info = { 127 .name = "adrf5720", 128 .gain_min = -31500, 129 .gain_max = 0, 130 .gain_step = -500, 131 .num_channels = 1, 132 }; 133 134 static const struct ad8366_info adrf5730_chip_info = { 135 .name = "adrf5730", 136 .gain_min = -31500, 137 .gain_max = 0, 138 .gain_step = -500, 139 .num_channels = 1, 140 }; 141 142 static const struct ad8366_info adrf5731_chip_info = { 143 .name = "adrf5731", 144 .gain_min = -30000, 145 .gain_max = 0, 146 .gain_step = -2000, 147 .num_channels = 1, 148 .pack_code = adrf5731_pack_code, 149 }; 150 151 static const struct ad8366_info hmc271_chip_info = { 152 .name = "hmc271a", 153 .gain_min = -31000, 154 .gain_max = 0, 155 .gain_step = 1000, 156 .num_channels = 1, 157 .pack_code = hmc271_pack_code, 158 }; 159 160 static const struct ad8366_info hmc792_chip_info = { 161 .name = "hmc792a", 162 .gain_min = -15750, 163 .gain_max = 0, 164 .gain_step = 250, 165 .num_channels = 1, 166 }; 167 168 static const struct ad8366_info hmc1018_chip_info = { 169 .name = "hmc1018a", 170 .gain_min = -31000, 171 .gain_max = 0, 172 .gain_step = 1000, 173 .num_channels = 1, 174 }; 175 176 static const struct ad8366_info hmc1019_chip_info = { 177 .name = "hmc1019a", 178 .gain_min = -15500, 179 .gain_max = 0, 180 .gain_step = 500, 181 .num_channels = 1, 182 }; 183 184 static const struct ad8366_info hmc1119_chip_info = { 185 .name = "hmc1119", 186 .gain_min = -31750, 187 .gain_max = 0, 188 .gain_step = -250, 189 .num_channels = 1, 190 }; 191 192 static int ad8366_write_code(struct ad8366_state *st) 193 { 194 const struct ad8366_info *inf = st->info; 195 size_t len = 1; 196 197 if (inf->pack_code) 198 len = inf->pack_code(st->ch, inf->num_channels, st->data); 199 else 200 st->data[0] = st->ch[0]; 201 202 return spi_write(st->spi, st->data, len); 203 } 204 205 static int ad8366_read_raw(struct iio_dev *indio_dev, 206 struct iio_chan_spec const *chan, 207 int *val, 208 int *val2, 209 long m) 210 { 211 struct ad8366_state *st = iio_priv(indio_dev); 212 const struct ad8366_info *inf = st->info; 213 int ret; 214 int code, gain = 0; 215 216 mutex_lock(&st->lock); 217 switch (m) { 218 case IIO_CHAN_INFO_HARDWAREGAIN: 219 code = st->ch[chan->channel]; 220 gain = inf->gain_step > 0 ? inf->gain_min : inf->gain_max; 221 gain += inf->gain_step * code; 222 /* Values in dB */ 223 *val = gain / 1000; 224 *val2 = (gain % 1000) * 1000; 225 226 ret = IIO_VAL_INT_PLUS_MICRO_DB; 227 break; 228 default: 229 ret = -EINVAL; 230 } 231 mutex_unlock(&st->lock); 232 233 return ret; 234 }; 235 236 static int ad8366_write_raw(struct iio_dev *indio_dev, 237 struct iio_chan_spec const *chan, 238 int val, 239 int val2, 240 long mask) 241 { 242 struct ad8366_state *st = iio_priv(indio_dev); 243 const struct ad8366_info *inf = st->info; 244 int code = 0, gain; 245 int ret; 246 247 /* Values in dB */ 248 if (val < 0) 249 gain = (val * 1000) - (val2 / 1000); 250 else 251 gain = (val * 1000) + (val2 / 1000); 252 253 if (gain > inf->gain_max || gain < inf->gain_min) 254 return -EINVAL; 255 256 gain -= inf->gain_step > 0 ? inf->gain_min : inf->gain_max; 257 code = DIV_ROUND_CLOSEST(gain, inf->gain_step); 258 259 mutex_lock(&st->lock); 260 switch (mask) { 261 case IIO_CHAN_INFO_HARDWAREGAIN: 262 st->ch[chan->channel] = code; 263 ret = ad8366_write_code(st); 264 break; 265 default: 266 ret = -EINVAL; 267 } 268 mutex_unlock(&st->lock); 269 270 return ret; 271 } 272 273 static int ad8366_write_raw_get_fmt(struct iio_dev *indio_dev, 274 struct iio_chan_spec const *chan, 275 long mask) 276 { 277 switch (mask) { 278 case IIO_CHAN_INFO_HARDWAREGAIN: 279 return IIO_VAL_INT_PLUS_MICRO_DB; 280 default: 281 return -EINVAL; 282 } 283 } 284 285 static const struct iio_info ad8366_info = { 286 .read_raw = &ad8366_read_raw, 287 .write_raw = &ad8366_write_raw, 288 .write_raw_get_fmt = &ad8366_write_raw_get_fmt, 289 }; 290 291 #define AD8366_CHAN(_channel) { \ 292 .type = IIO_VOLTAGE, \ 293 .output = 1, \ 294 .indexed = 1, \ 295 .channel = _channel, \ 296 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\ 297 } 298 299 static const struct iio_chan_spec ad8366_channels[] = { 300 AD8366_CHAN(0), 301 AD8366_CHAN(1), 302 }; 303 304 static int ad8366_probe(struct spi_device *spi) 305 { 306 struct device *dev = &spi->dev; 307 struct gpio_desc *enable_gpio; 308 struct reset_control *rstc; 309 struct iio_dev *indio_dev; 310 struct ad8366_state *st; 311 int ret; 312 313 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 314 if (indio_dev == NULL) 315 return -ENOMEM; 316 317 st = iio_priv(indio_dev); 318 319 ret = devm_mutex_init(dev, &st->lock); 320 if (ret) 321 return ret; 322 323 ret = devm_regulator_get_enable(dev, "vcc"); 324 if (ret) 325 return dev_err_probe(dev, ret, "Failed to get regulator\n"); 326 327 st->spi = spi; 328 st->info = spi_get_device_match_data(spi); 329 330 enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); 331 if (IS_ERR(enable_gpio)) 332 return dev_err_probe(dev, PTR_ERR(enable_gpio), 333 "Failed to get enable GPIO\n"); 334 335 rstc = devm_reset_control_get_optional_exclusive_deasserted(dev, NULL); 336 if (IS_ERR(rstc)) 337 return dev_err_probe(dev, PTR_ERR(rstc), 338 "Failed to get reset controller\n"); 339 340 indio_dev->name = st->info->name; 341 indio_dev->info = &ad8366_info; 342 indio_dev->modes = INDIO_DIRECT_MODE; 343 indio_dev->channels = ad8366_channels; 344 indio_dev->num_channels = st->info->num_channels; 345 346 ret = ad8366_write_code(st); 347 if (ret < 0) 348 return dev_err_probe(dev, ret, "failed to write initial gain\n"); 349 350 return devm_iio_device_register(dev, indio_dev); 351 } 352 353 static const struct spi_device_id ad8366_id[] = { 354 { .name = "ad8366", .driver_data = (kernel_ulong_t)&ad8366_chip_info }, 355 { .name = "ada4961", .driver_data = (kernel_ulong_t)&ada4961_chip_info }, 356 { .name = "adl5240", .driver_data = (kernel_ulong_t)&adl5240_chip_info }, 357 { .name = "adrf5702", .driver_data = (kernel_ulong_t)&adrf5702_chip_info }, 358 { .name = "adrf5703", .driver_data = (kernel_ulong_t)&adrf5703_chip_info }, 359 { .name = "adrf5720", .driver_data = (kernel_ulong_t)&adrf5720_chip_info }, 360 { .name = "adrf5730", .driver_data = (kernel_ulong_t)&adrf5730_chip_info }, 361 { .name = "adrf5731", .driver_data = (kernel_ulong_t)&adrf5731_chip_info }, 362 { .name = "hmc271a", .driver_data = (kernel_ulong_t)&hmc271_chip_info }, 363 { .name = "hmc792a", .driver_data = (kernel_ulong_t)&hmc792_chip_info }, 364 { .name = "hmc1018a", .driver_data = (kernel_ulong_t)&hmc1018_chip_info }, 365 { .name = "hmc1019a", .driver_data = (kernel_ulong_t)&hmc1019_chip_info }, 366 { .name = "hmc1119", .driver_data = (kernel_ulong_t)&hmc1119_chip_info }, 367 { } 368 }; 369 MODULE_DEVICE_TABLE(spi, ad8366_id); 370 371 static const struct of_device_id ad8366_of_match[] = { 372 { .compatible = "adi,ad8366", .data = &ad8366_chip_info }, 373 { .compatible = "adi,ada4961", .data = &ada4961_chip_info }, 374 { .compatible = "adi,adl5240", .data = &adl5240_chip_info }, 375 { .compatible = "adi,adrf5702", .data = &adrf5702_chip_info }, 376 { .compatible = "adi,adrf5703", .data = &adrf5703_chip_info }, 377 { .compatible = "adi,adrf5720", .data = &adrf5720_chip_info }, 378 { .compatible = "adi,adrf5730", .data = &adrf5730_chip_info }, 379 { .compatible = "adi,adrf5731", .data = &adrf5731_chip_info }, 380 { .compatible = "adi,hmc271a", .data = &hmc271_chip_info }, 381 { .compatible = "adi,hmc792a", .data = &hmc792_chip_info }, 382 { .compatible = "adi,hmc1018a", .data = &hmc1018_chip_info }, 383 { .compatible = "adi,hmc1019a", .data = &hmc1019_chip_info }, 384 { .compatible = "adi,hmc1119", .data = &hmc1119_chip_info }, 385 { } 386 }; 387 MODULE_DEVICE_TABLE(of, ad8366_of_match); 388 389 static struct spi_driver ad8366_driver = { 390 .driver = { 391 .name = KBUILD_MODNAME, 392 .of_match_table = ad8366_of_match, 393 }, 394 .probe = ad8366_probe, 395 .id_table = ad8366_id, 396 }; 397 398 module_spi_driver(ad8366_driver); 399 400 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 401 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers"); 402 MODULE_LICENSE("GPL v2"); 403