ad7303.c 7.0 KB

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