ad8366.c 4.3 KB

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