ad8366.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator
  9. *
  10. * Copyright 2012-2019 Analog Devices Inc.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include <linux/bitrev.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/sysfs.h>
  24. enum ad8366_type {
  25. ID_AD8366,
  26. ID_ADA4961,
  27. ID_ADL5240,
  28. ID_HMC1119,
  29. };
  30. struct ad8366_info {
  31. int gain_min;
  32. int gain_max;
  33. };
  34. struct ad8366_state {
  35. struct spi_device *spi;
  36. struct regulator *reg;
  37. struct mutex lock; /* protect sensor state */
  38. struct gpio_desc *reset_gpio;
  39. unsigned char ch[2];
  40. enum ad8366_type type;
  41. struct ad8366_info *info;
  42. /*
  43. * DMA (thus cache coherency maintenance) may require the
  44. * transfer buffers to live in their own cache lines.
  45. */
  46. unsigned char data[2] __aligned(IIO_DMA_MINALIGN);
  47. };
  48. static struct ad8366_info ad8366_infos[] = {
  49. [ID_AD8366] = {
  50. .gain_min = 4500,
  51. .gain_max = 20500,
  52. },
  53. [ID_ADA4961] = {
  54. .gain_min = -6000,
  55. .gain_max = 15000,
  56. },
  57. [ID_ADL5240] = {
  58. .gain_min = -11500,
  59. .gain_max = 20000,
  60. },
  61. [ID_HMC1119] = {
  62. .gain_min = -31750,
  63. .gain_max = 0,
  64. },
  65. };
  66. static int ad8366_write(struct iio_dev *indio_dev,
  67. unsigned char ch_a, unsigned char ch_b)
  68. {
  69. struct ad8366_state *st = iio_priv(indio_dev);
  70. int ret;
  71. switch (st->type) {
  72. case ID_AD8366:
  73. ch_a = bitrev8(ch_a & 0x3F);
  74. ch_b = bitrev8(ch_b & 0x3F);
  75. st->data[0] = ch_b >> 4;
  76. st->data[1] = (ch_b << 4) | (ch_a >> 2);
  77. break;
  78. case ID_ADA4961:
  79. st->data[0] = ch_a & 0x1F;
  80. break;
  81. case ID_ADL5240:
  82. st->data[0] = (ch_a & 0x3F);
  83. break;
  84. case ID_HMC1119:
  85. st->data[0] = ch_a;
  86. break;
  87. }
  88. ret = spi_write(st->spi, st->data, indio_dev->num_channels);
  89. if (ret < 0)
  90. dev_err(&indio_dev->dev, "write failed (%d)", ret);
  91. return ret;
  92. }
  93. static int ad8366_read_raw(struct iio_dev *indio_dev,
  94. struct iio_chan_spec const *chan,
  95. int *val,
  96. int *val2,
  97. long m)
  98. {
  99. struct ad8366_state *st = iio_priv(indio_dev);
  100. int ret;
  101. int code, gain = 0;
  102. mutex_lock(&st->lock);
  103. switch (m) {
  104. case IIO_CHAN_INFO_HARDWAREGAIN:
  105. code = st->ch[chan->channel];
  106. switch (st->type) {
  107. case ID_AD8366:
  108. gain = code * 253 + 4500;
  109. break;
  110. case ID_ADA4961:
  111. gain = 15000 - code * 1000;
  112. break;
  113. case ID_ADL5240:
  114. gain = 20000 - 31500 + code * 500;
  115. break;
  116. case ID_HMC1119:
  117. gain = -1 * code * 250;
  118. break;
  119. }
  120. /* Values in dB */
  121. *val = gain / 1000;
  122. *val2 = (gain % 1000) * 1000;
  123. ret = IIO_VAL_INT_PLUS_MICRO_DB;
  124. break;
  125. default:
  126. ret = -EINVAL;
  127. }
  128. mutex_unlock(&st->lock);
  129. return ret;
  130. };
  131. static int ad8366_write_raw(struct iio_dev *indio_dev,
  132. struct iio_chan_spec const *chan,
  133. int val,
  134. int val2,
  135. long mask)
  136. {
  137. struct ad8366_state *st = iio_priv(indio_dev);
  138. struct ad8366_info *inf = st->info;
  139. int code = 0, gain;
  140. int ret;
  141. /* Values in dB */
  142. if (val < 0)
  143. gain = (val * 1000) - (val2 / 1000);
  144. else
  145. gain = (val * 1000) + (val2 / 1000);
  146. if (gain > inf->gain_max || gain < inf->gain_min)
  147. return -EINVAL;
  148. switch (st->type) {
  149. case ID_AD8366:
  150. code = (gain - 4500) / 253;
  151. break;
  152. case ID_ADA4961:
  153. code = (15000 - gain) / 1000;
  154. break;
  155. case ID_ADL5240:
  156. code = ((gain - 500 - 20000) / 500) & 0x3F;
  157. break;
  158. case ID_HMC1119:
  159. code = (abs(gain) / 250) & 0x7F;
  160. break;
  161. }
  162. mutex_lock(&st->lock);
  163. switch (mask) {
  164. case IIO_CHAN_INFO_HARDWAREGAIN:
  165. st->ch[chan->channel] = code;
  166. ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
  167. break;
  168. default:
  169. ret = -EINVAL;
  170. }
  171. mutex_unlock(&st->lock);
  172. return ret;
  173. }
  174. static int ad8366_write_raw_get_fmt(struct iio_dev *indio_dev,
  175. struct iio_chan_spec const *chan,
  176. long mask)
  177. {
  178. switch (mask) {
  179. case IIO_CHAN_INFO_HARDWAREGAIN:
  180. return IIO_VAL_INT_PLUS_MICRO_DB;
  181. default:
  182. return -EINVAL;
  183. }
  184. }
  185. static const struct iio_info ad8366_info = {
  186. .read_raw = &ad8366_read_raw,
  187. .write_raw = &ad8366_write_raw,
  188. .write_raw_get_fmt = &ad8366_write_raw_get_fmt,
  189. };
  190. #define AD8366_CHAN(_channel) { \
  191. .type = IIO_VOLTAGE, \
  192. .output = 1, \
  193. .indexed = 1, \
  194. .channel = _channel, \
  195. .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
  196. }
  197. static const struct iio_chan_spec ad8366_channels[] = {
  198. AD8366_CHAN(0),
  199. AD8366_CHAN(1),
  200. };
  201. static const struct iio_chan_spec ada4961_channels[] = {
  202. AD8366_CHAN(0),
  203. };
  204. static int ad8366_probe(struct spi_device *spi)
  205. {
  206. struct iio_dev *indio_dev;
  207. struct ad8366_state *st;
  208. int ret;
  209. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  210. if (indio_dev == NULL)
  211. return -ENOMEM;
  212. st = iio_priv(indio_dev);
  213. st->reg = devm_regulator_get(&spi->dev, "vcc");
  214. if (!IS_ERR(st->reg)) {
  215. ret = regulator_enable(st->reg);
  216. if (ret)
  217. return ret;
  218. }
  219. spi_set_drvdata(spi, indio_dev);
  220. mutex_init(&st->lock);
  221. st->spi = spi;
  222. st->type = spi_get_device_id(spi)->driver_data;
  223. switch (st->type) {
  224. case ID_AD8366:
  225. indio_dev->channels = ad8366_channels;
  226. indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
  227. break;
  228. case ID_ADA4961:
  229. case ID_ADL5240:
  230. case ID_HMC1119:
  231. st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_HIGH);
  232. if (IS_ERR(st->reset_gpio)) {
  233. ret = PTR_ERR(st->reset_gpio);
  234. goto error_disable_reg;
  235. }
  236. indio_dev->channels = ada4961_channels;
  237. indio_dev->num_channels = ARRAY_SIZE(ada4961_channels);
  238. break;
  239. default:
  240. dev_err(&spi->dev, "Invalid device ID\n");
  241. ret = -EINVAL;
  242. goto error_disable_reg;
  243. }
  244. st->info = &ad8366_infos[st->type];
  245. indio_dev->name = spi_get_device_id(spi)->name;
  246. indio_dev->info = &ad8366_info;
  247. indio_dev->modes = INDIO_DIRECT_MODE;
  248. ret = ad8366_write(indio_dev, 0 , 0);
  249. if (ret < 0)
  250. goto error_disable_reg;
  251. ret = iio_device_register(indio_dev);
  252. if (ret)
  253. goto error_disable_reg;
  254. return 0;
  255. error_disable_reg:
  256. if (!IS_ERR(st->reg))
  257. regulator_disable(st->reg);
  258. return ret;
  259. }
  260. static void ad8366_remove(struct spi_device *spi)
  261. {
  262. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  263. struct ad8366_state *st = iio_priv(indio_dev);
  264. struct regulator *reg = st->reg;
  265. iio_device_unregister(indio_dev);
  266. if (!IS_ERR(reg))
  267. regulator_disable(reg);
  268. }
  269. static const struct spi_device_id ad8366_id[] = {
  270. {"ad8366", ID_AD8366},
  271. {"ada4961", ID_ADA4961},
  272. {"adl5240", ID_ADL5240},
  273. {"hmc1119", ID_HMC1119},
  274. {}
  275. };
  276. MODULE_DEVICE_TABLE(spi, ad8366_id);
  277. static struct spi_driver ad8366_driver = {
  278. .driver = {
  279. .name = KBUILD_MODNAME,
  280. },
  281. .probe = ad8366_probe,
  282. .remove = ad8366_remove,
  283. .id_table = ad8366_id,
  284. };
  285. module_spi_driver(ad8366_driver);
  286. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  287. MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
  288. MODULE_LICENSE("GPL v2");