ad7303.c 6.9 KB

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