ad8366.c 4.4 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. };
  100. #define AD8366_CHAN(_channel) { \
  101. .type = IIO_VOLTAGE, \
  102. .output = 1, \
  103. .indexed = 1, \
  104. .channel = _channel, \
  105. .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
  106. }
  107. static const struct iio_chan_spec ad8366_channels[] = {
  108. AD8366_CHAN(0),
  109. AD8366_CHAN(1),
  110. };
  111. static int ad8366_probe(struct spi_device *spi)
  112. {
  113. struct iio_dev *indio_dev;
  114. struct ad8366_state *st;
  115. int ret;
  116. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  117. if (indio_dev == NULL)
  118. return -ENOMEM;
  119. st = iio_priv(indio_dev);
  120. st->reg = devm_regulator_get(&spi->dev, "vcc");
  121. if (!IS_ERR(st->reg)) {
  122. ret = regulator_enable(st->reg);
  123. if (ret)
  124. return ret;
  125. }
  126. spi_set_drvdata(spi, indio_dev);
  127. st->spi = spi;
  128. indio_dev->dev.parent = &spi->dev;
  129. indio_dev->name = spi_get_device_id(spi)->name;
  130. indio_dev->info = &ad8366_info;
  131. indio_dev->modes = INDIO_DIRECT_MODE;
  132. indio_dev->channels = ad8366_channels;
  133. indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
  134. ret = ad8366_write(indio_dev, 0 , 0);
  135. if (ret < 0)
  136. goto error_disable_reg;
  137. ret = iio_device_register(indio_dev);
  138. if (ret)
  139. goto error_disable_reg;
  140. return 0;
  141. error_disable_reg:
  142. if (!IS_ERR(st->reg))
  143. regulator_disable(st->reg);
  144. return ret;
  145. }
  146. static int ad8366_remove(struct spi_device *spi)
  147. {
  148. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  149. struct ad8366_state *st = iio_priv(indio_dev);
  150. struct regulator *reg = st->reg;
  151. iio_device_unregister(indio_dev);
  152. if (!IS_ERR(reg))
  153. regulator_disable(reg);
  154. return 0;
  155. }
  156. static const struct spi_device_id ad8366_id[] = {
  157. {"ad8366", 0},
  158. {}
  159. };
  160. MODULE_DEVICE_TABLE(spi, ad8366_id);
  161. static struct spi_driver ad8366_driver = {
  162. .driver = {
  163. .name = KBUILD_MODNAME,
  164. },
  165. .probe = ad8366_probe,
  166. .remove = ad8366_remove,
  167. .id_table = ad8366_id,
  168. };
  169. module_spi_driver(ad8366_driver);
  170. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  171. MODULE_DESCRIPTION("Analog Devices AD8366 VGA");
  172. MODULE_LICENSE("GPL v2");