ad7303.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AD7303 Digital to analog converters driver
  4. *
  5. * Copyright 2013 Analog Devices Inc.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/kernel.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #define AD7303_CFG_EXTERNAL_VREF BIT(15)
  18. #define AD7303_CFG_POWER_DOWN(ch) BIT(11 + (ch))
  19. #define AD7303_CFG_ADDR_OFFSET 10
  20. #define AD7303_CMD_UPDATE_DAC (0x3 << 8)
  21. /**
  22. * struct ad7303_state - driver instance specific data
  23. * @spi: the device for this driver instance
  24. * @config: cached config register value
  25. * @dac_cache: current DAC raw value (chip does not support readback)
  26. * @vdd_reg: reference to VDD regulator
  27. * @vref_reg: reference to VREF regulator
  28. * @lock: protect writes and cache updates
  29. * @data: spi transfer buffer
  30. */
  31. struct ad7303_state {
  32. struct spi_device *spi;
  33. uint16_t config;
  34. uint8_t dac_cache[2];
  35. struct regulator *vdd_reg;
  36. struct regulator *vref_reg;
  37. struct mutex lock;
  38. /*
  39. * DMA (thus cache coherency maintenance) may require the
  40. * transfer buffers to live in their own cache lines.
  41. */
  42. __be16 data __aligned(IIO_DMA_MINALIGN);
  43. };
  44. static int ad7303_write(struct ad7303_state *st, unsigned int chan,
  45. uint8_t val)
  46. {
  47. st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC |
  48. (chan << AD7303_CFG_ADDR_OFFSET) |
  49. st->config | val);
  50. return spi_write(st->spi, &st->data, sizeof(st->data));
  51. }
  52. static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev,
  53. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  54. {
  55. struct ad7303_state *st = iio_priv(indio_dev);
  56. return sysfs_emit(buf, "%d\n", (bool)(st->config &
  57. AD7303_CFG_POWER_DOWN(chan->channel)));
  58. }
  59. static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
  60. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  61. size_t len)
  62. {
  63. struct ad7303_state *st = iio_priv(indio_dev);
  64. bool pwr_down;
  65. int ret;
  66. ret = kstrtobool(buf, &pwr_down);
  67. if (ret)
  68. return ret;
  69. mutex_lock(&st->lock);
  70. if (pwr_down)
  71. st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
  72. else
  73. st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel);
  74. /* There is no noop cmd which allows us to only update the powerdown
  75. * mode, so just write one of the DAC channels again */
  76. ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
  77. mutex_unlock(&st->lock);
  78. return len;
  79. }
  80. static int ad7303_get_vref(struct ad7303_state *st,
  81. struct iio_chan_spec const *chan)
  82. {
  83. int ret;
  84. if (st->config & AD7303_CFG_EXTERNAL_VREF)
  85. return regulator_get_voltage(st->vref_reg);
  86. ret = regulator_get_voltage(st->vdd_reg);
  87. if (ret < 0)
  88. return ret;
  89. return ret / 2;
  90. }
  91. static int ad7303_read_raw(struct iio_dev *indio_dev,
  92. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  93. {
  94. struct ad7303_state *st = iio_priv(indio_dev);
  95. int vref_uv;
  96. switch (info) {
  97. case IIO_CHAN_INFO_RAW:
  98. mutex_lock(&st->lock);
  99. *val = st->dac_cache[chan->channel];
  100. mutex_unlock(&st->lock);
  101. return IIO_VAL_INT;
  102. case IIO_CHAN_INFO_SCALE:
  103. vref_uv = ad7303_get_vref(st, chan);
  104. if (vref_uv < 0)
  105. return vref_uv;
  106. *val = 2 * vref_uv / 1000;
  107. *val2 = chan->scan_type.realbits;
  108. return IIO_VAL_FRACTIONAL_LOG2;
  109. default:
  110. break;
  111. }
  112. return -EINVAL;
  113. }
  114. static int ad7303_write_raw(struct iio_dev *indio_dev,
  115. struct iio_chan_spec const *chan, int val, int val2, long mask)
  116. {
  117. struct ad7303_state *st = iio_priv(indio_dev);
  118. int ret;
  119. switch (mask) {
  120. case IIO_CHAN_INFO_RAW:
  121. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  122. return -EINVAL;
  123. mutex_lock(&st->lock);
  124. ret = ad7303_write(st, chan->address, val);
  125. if (ret == 0)
  126. st->dac_cache[chan->channel] = val;
  127. mutex_unlock(&st->lock);
  128. break;
  129. default:
  130. ret = -EINVAL;
  131. }
  132. return ret;
  133. }
  134. static const struct iio_info ad7303_info = {
  135. .read_raw = ad7303_read_raw,
  136. .write_raw = ad7303_write_raw,
  137. };
  138. static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
  139. {
  140. .name = "powerdown",
  141. .read = ad7303_read_dac_powerdown,
  142. .write = ad7303_write_dac_powerdown,
  143. .shared = IIO_SEPARATE,
  144. },
  145. { },
  146. };
  147. #define AD7303_CHANNEL(chan) { \
  148. .type = IIO_VOLTAGE, \
  149. .indexed = 1, \
  150. .output = 1, \
  151. .channel = (chan), \
  152. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  153. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  154. .address = (chan), \
  155. .scan_type = { \
  156. .sign = 'u', \
  157. .realbits = 8, \
  158. .storagebits = 8, \
  159. .shift = 0, \
  160. }, \
  161. .ext_info = ad7303_ext_info, \
  162. }
  163. static const struct iio_chan_spec ad7303_channels[] = {
  164. AD7303_CHANNEL(0),
  165. AD7303_CHANNEL(1),
  166. };
  167. static void ad7303_reg_disable(void *reg)
  168. {
  169. regulator_disable(reg);
  170. }
  171. static int ad7303_probe(struct spi_device *spi)
  172. {
  173. const struct spi_device_id *id = spi_get_device_id(spi);
  174. struct iio_dev *indio_dev;
  175. struct ad7303_state *st;
  176. int ret;
  177. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  178. if (indio_dev == NULL)
  179. return -ENOMEM;
  180. st = iio_priv(indio_dev);
  181. st->spi = spi;
  182. mutex_init(&st->lock);
  183. st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
  184. if (IS_ERR(st->vdd_reg))
  185. return PTR_ERR(st->vdd_reg);
  186. ret = regulator_enable(st->vdd_reg);
  187. if (ret)
  188. return ret;
  189. ret = devm_add_action_or_reset(&spi->dev, ad7303_reg_disable, st->vdd_reg);
  190. if (ret)
  191. return ret;
  192. st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF");
  193. if (IS_ERR(st->vref_reg)) {
  194. ret = PTR_ERR(st->vref_reg);
  195. if (ret != -ENODEV)
  196. return ret;
  197. st->vref_reg = NULL;
  198. }
  199. if (st->vref_reg) {
  200. ret = regulator_enable(st->vref_reg);
  201. if (ret)
  202. return ret;
  203. ret = devm_add_action_or_reset(&spi->dev, ad7303_reg_disable,
  204. st->vref_reg);
  205. if (ret)
  206. return ret;
  207. st->config |= AD7303_CFG_EXTERNAL_VREF;
  208. }
  209. indio_dev->name = id->name;
  210. indio_dev->info = &ad7303_info;
  211. indio_dev->modes = INDIO_DIRECT_MODE;
  212. indio_dev->channels = ad7303_channels;
  213. indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
  214. return devm_iio_device_register(&spi->dev, indio_dev);
  215. }
  216. static const struct of_device_id ad7303_spi_of_match[] = {
  217. { .compatible = "adi,ad7303", },
  218. { /* sentinel */ },
  219. };
  220. MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
  221. static const struct spi_device_id ad7303_spi_ids[] = {
  222. { "ad7303", 0 },
  223. {}
  224. };
  225. MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
  226. static struct spi_driver ad7303_driver = {
  227. .driver = {
  228. .name = "ad7303",
  229. .of_match_table = ad7303_spi_of_match,
  230. },
  231. .probe = ad7303_probe,
  232. .id_table = ad7303_spi_ids,
  233. };
  234. module_spi_driver(ad7303_driver);
  235. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  236. MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
  237. MODULE_LICENSE("GPL v2");