ti-dac7612.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DAC7612 Dual, 12-Bit Serial input Digital-to-Analog Converter
  4. *
  5. * Copyright 2019 Qtechnology A/S
  6. * 2019 Ricardo Ribalda <[email protected]>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/iio/iio.h>
  15. #define DAC7612_RESOLUTION 12
  16. #define DAC7612_ADDRESS 4
  17. #define DAC7612_START 5
  18. struct dac7612 {
  19. struct spi_device *spi;
  20. struct gpio_desc *loaddacs;
  21. uint16_t cache[2];
  22. /*
  23. * Lock to protect the state of the device from potential concurrent
  24. * write accesses from userspace. The write operation requires an
  25. * SPI write, then toggling of a GPIO, so the lock aims to protect
  26. * the sanity of the entire sequence of operation.
  27. */
  28. struct mutex lock;
  29. /*
  30. * DMA (thus cache coherency maintenance) may require the
  31. * transfer buffers to live in their own cache lines.
  32. */
  33. uint8_t data[2] __aligned(IIO_DMA_MINALIGN);
  34. };
  35. static int dac7612_cmd_single(struct dac7612 *priv, int channel, u16 val)
  36. {
  37. int ret;
  38. priv->data[0] = BIT(DAC7612_START) | (channel << DAC7612_ADDRESS);
  39. priv->data[0] |= val >> 8;
  40. priv->data[1] = val & 0xff;
  41. priv->cache[channel] = val;
  42. ret = spi_write(priv->spi, priv->data, sizeof(priv->data));
  43. if (ret)
  44. return ret;
  45. gpiod_set_value(priv->loaddacs, 1);
  46. gpiod_set_value(priv->loaddacs, 0);
  47. return 0;
  48. }
  49. #define dac7612_CHANNEL(chan, name) { \
  50. .type = IIO_VOLTAGE, \
  51. .channel = (chan), \
  52. .indexed = 1, \
  53. .output = 1, \
  54. .datasheet_name = name, \
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  56. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  57. }
  58. static const struct iio_chan_spec dac7612_channels[] = {
  59. dac7612_CHANNEL(0, "OUTA"),
  60. dac7612_CHANNEL(1, "OUTB"),
  61. };
  62. static int dac7612_read_raw(struct iio_dev *iio_dev,
  63. const struct iio_chan_spec *chan,
  64. int *val, int *val2, long mask)
  65. {
  66. struct dac7612 *priv;
  67. switch (mask) {
  68. case IIO_CHAN_INFO_RAW:
  69. priv = iio_priv(iio_dev);
  70. *val = priv->cache[chan->channel];
  71. return IIO_VAL_INT;
  72. case IIO_CHAN_INFO_SCALE:
  73. *val = 1;
  74. return IIO_VAL_INT;
  75. default:
  76. return -EINVAL;
  77. }
  78. }
  79. static int dac7612_write_raw(struct iio_dev *iio_dev,
  80. const struct iio_chan_spec *chan,
  81. int val, int val2, long mask)
  82. {
  83. struct dac7612 *priv = iio_priv(iio_dev);
  84. int ret;
  85. if (mask != IIO_CHAN_INFO_RAW)
  86. return -EINVAL;
  87. if ((val >= BIT(DAC7612_RESOLUTION)) || val < 0 || val2)
  88. return -EINVAL;
  89. if (val == priv->cache[chan->channel])
  90. return 0;
  91. mutex_lock(&priv->lock);
  92. ret = dac7612_cmd_single(priv, chan->channel, val);
  93. mutex_unlock(&priv->lock);
  94. return ret;
  95. }
  96. static const struct iio_info dac7612_info = {
  97. .read_raw = dac7612_read_raw,
  98. .write_raw = dac7612_write_raw,
  99. };
  100. static int dac7612_probe(struct spi_device *spi)
  101. {
  102. struct iio_dev *iio_dev;
  103. struct dac7612 *priv;
  104. int i;
  105. int ret;
  106. iio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*priv));
  107. if (!iio_dev)
  108. return -ENOMEM;
  109. priv = iio_priv(iio_dev);
  110. /*
  111. * LOADDACS pin can be controlled by the driver or externally.
  112. * When controlled by the driver, the DAC value is updated after
  113. * every write.
  114. * When the driver does not control the PIN, the user or an external
  115. * event can change the value of all DACs by pulsing down the LOADDACs
  116. * pin.
  117. */
  118. priv->loaddacs = devm_gpiod_get_optional(&spi->dev, "ti,loaddacs",
  119. GPIOD_OUT_LOW);
  120. if (IS_ERR(priv->loaddacs))
  121. return PTR_ERR(priv->loaddacs);
  122. priv->spi = spi;
  123. spi_set_drvdata(spi, iio_dev);
  124. iio_dev->info = &dac7612_info;
  125. iio_dev->modes = INDIO_DIRECT_MODE;
  126. iio_dev->channels = dac7612_channels;
  127. iio_dev->num_channels = ARRAY_SIZE(priv->cache);
  128. iio_dev->name = spi_get_device_id(spi)->name;
  129. mutex_init(&priv->lock);
  130. for (i = 0; i < ARRAY_SIZE(priv->cache); i++) {
  131. ret = dac7612_cmd_single(priv, i, 0);
  132. if (ret)
  133. return ret;
  134. }
  135. return devm_iio_device_register(&spi->dev, iio_dev);
  136. }
  137. static const struct spi_device_id dac7612_id[] = {
  138. {"ti-dac7612"},
  139. {}
  140. };
  141. MODULE_DEVICE_TABLE(spi, dac7612_id);
  142. static const struct of_device_id dac7612_of_match[] = {
  143. { .compatible = "ti,dac7612" },
  144. { .compatible = "ti,dac7612u" },
  145. { .compatible = "ti,dac7612ub" },
  146. { },
  147. };
  148. MODULE_DEVICE_TABLE(of, dac7612_of_match);
  149. static struct spi_driver dac7612_driver = {
  150. .driver = {
  151. .name = "ti-dac7612",
  152. .of_match_table = dac7612_of_match,
  153. },
  154. .probe = dac7612_probe,
  155. .id_table = dac7612_id,
  156. };
  157. module_spi_driver(dac7612_driver);
  158. MODULE_AUTHOR("Ricardo Ribalda <[email protected]>");
  159. MODULE_DESCRIPTION("Texas Instruments DAC7612 DAC driver");
  160. MODULE_LICENSE("GPL v2");