ad8801.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * IIO DAC driver for Analog Devices AD8801 DAC
  3. *
  4. * Copyright (C) 2016 Gwenhael Goavec-Merou
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. */
  15. #include <linux/iio/iio.h>
  16. #include <linux/module.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/sysfs.h>
  20. #define AD8801_CFG_ADDR_OFFSET 8
  21. enum ad8801_device_ids {
  22. ID_AD8801,
  23. ID_AD8803,
  24. };
  25. struct ad8801_state {
  26. struct spi_device *spi;
  27. unsigned char dac_cache[8]; /* Value write on each channel */
  28. unsigned int vrefh_mv;
  29. unsigned int vrefl_mv;
  30. struct regulator *vrefh_reg;
  31. struct regulator *vrefl_reg;
  32. __be16 data ____cacheline_aligned;
  33. };
  34. static int ad8801_spi_write(struct ad8801_state *state,
  35. u8 channel, unsigned char value)
  36. {
  37. state->data = cpu_to_be16((channel << AD8801_CFG_ADDR_OFFSET) | value);
  38. return spi_write(state->spi, &state->data, sizeof(state->data));
  39. }
  40. static int ad8801_write_raw(struct iio_dev *indio_dev,
  41. struct iio_chan_spec const *chan, int val, int val2, long mask)
  42. {
  43. struct ad8801_state *state = iio_priv(indio_dev);
  44. int ret;
  45. switch (mask) {
  46. case IIO_CHAN_INFO_RAW:
  47. if (val >= 256 || val < 0)
  48. return -EINVAL;
  49. ret = ad8801_spi_write(state, chan->channel, val);
  50. if (ret == 0)
  51. state->dac_cache[chan->channel] = val;
  52. break;
  53. default:
  54. ret = -EINVAL;
  55. }
  56. return ret;
  57. }
  58. static int ad8801_read_raw(struct iio_dev *indio_dev,
  59. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  60. {
  61. struct ad8801_state *state = iio_priv(indio_dev);
  62. switch (info) {
  63. case IIO_CHAN_INFO_RAW:
  64. *val = state->dac_cache[chan->channel];
  65. return IIO_VAL_INT;
  66. case IIO_CHAN_INFO_SCALE:
  67. *val = state->vrefh_mv - state->vrefl_mv;
  68. *val2 = 8;
  69. return IIO_VAL_FRACTIONAL_LOG2;
  70. case IIO_CHAN_INFO_OFFSET:
  71. *val = state->vrefl_mv;
  72. return IIO_VAL_INT;
  73. default:
  74. return -EINVAL;
  75. }
  76. return -EINVAL;
  77. }
  78. static const struct iio_info ad8801_info = {
  79. .read_raw = ad8801_read_raw,
  80. .write_raw = ad8801_write_raw,
  81. .driver_module = THIS_MODULE,
  82. };
  83. #define AD8801_CHANNEL(chan) { \
  84. .type = IIO_VOLTAGE, \
  85. .indexed = 1, \
  86. .output = 1, \
  87. .channel = chan, \
  88. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  89. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  90. BIT(IIO_CHAN_INFO_OFFSET), \
  91. }
  92. static const struct iio_chan_spec ad8801_channels[] = {
  93. AD8801_CHANNEL(0),
  94. AD8801_CHANNEL(1),
  95. AD8801_CHANNEL(2),
  96. AD8801_CHANNEL(3),
  97. AD8801_CHANNEL(4),
  98. AD8801_CHANNEL(5),
  99. AD8801_CHANNEL(6),
  100. AD8801_CHANNEL(7),
  101. };
  102. static int ad8801_probe(struct spi_device *spi)
  103. {
  104. struct iio_dev *indio_dev;
  105. struct ad8801_state *state;
  106. const struct spi_device_id *id;
  107. int ret;
  108. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
  109. if (indio_dev == NULL)
  110. return -ENOMEM;
  111. state = iio_priv(indio_dev);
  112. state->spi = spi;
  113. id = spi_get_device_id(spi);
  114. state->vrefh_reg = devm_regulator_get(&spi->dev, "vrefh");
  115. if (IS_ERR(state->vrefh_reg)) {
  116. dev_err(&spi->dev, "Vrefh regulator not specified\n");
  117. return PTR_ERR(state->vrefh_reg);
  118. }
  119. ret = regulator_enable(state->vrefh_reg);
  120. if (ret) {
  121. dev_err(&spi->dev, "Failed to enable vrefh regulator: %d\n",
  122. ret);
  123. return ret;
  124. }
  125. ret = regulator_get_voltage(state->vrefh_reg);
  126. if (ret < 0) {
  127. dev_err(&spi->dev, "Failed to read vrefh regulator: %d\n",
  128. ret);
  129. goto error_disable_vrefh_reg;
  130. }
  131. state->vrefh_mv = ret / 1000;
  132. if (id->driver_data == ID_AD8803) {
  133. state->vrefl_reg = devm_regulator_get(&spi->dev, "vrefl");
  134. if (IS_ERR(state->vrefl_reg)) {
  135. dev_err(&spi->dev, "Vrefl regulator not specified\n");
  136. ret = PTR_ERR(state->vrefl_reg);
  137. goto error_disable_vrefh_reg;
  138. }
  139. ret = regulator_enable(state->vrefl_reg);
  140. if (ret) {
  141. dev_err(&spi->dev, "Failed to enable vrefl regulator: %d\n",
  142. ret);
  143. goto error_disable_vrefh_reg;
  144. }
  145. ret = regulator_get_voltage(state->vrefl_reg);
  146. if (ret < 0) {
  147. dev_err(&spi->dev, "Failed to read vrefl regulator: %d\n",
  148. ret);
  149. goto error_disable_vrefl_reg;
  150. }
  151. state->vrefl_mv = ret / 1000;
  152. } else {
  153. state->vrefl_mv = 0;
  154. state->vrefl_reg = NULL;
  155. }
  156. spi_set_drvdata(spi, indio_dev);
  157. indio_dev->dev.parent = &spi->dev;
  158. indio_dev->info = &ad8801_info;
  159. indio_dev->modes = INDIO_DIRECT_MODE;
  160. indio_dev->channels = ad8801_channels;
  161. indio_dev->num_channels = ARRAY_SIZE(ad8801_channels);
  162. indio_dev->name = id->name;
  163. ret = iio_device_register(indio_dev);
  164. if (ret) {
  165. dev_err(&spi->dev, "Failed to register iio device: %d\n",
  166. ret);
  167. goto error_disable_vrefl_reg;
  168. }
  169. return 0;
  170. error_disable_vrefl_reg:
  171. if (state->vrefl_reg)
  172. regulator_disable(state->vrefl_reg);
  173. error_disable_vrefh_reg:
  174. regulator_disable(state->vrefh_reg);
  175. return ret;
  176. }
  177. static int ad8801_remove(struct spi_device *spi)
  178. {
  179. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  180. struct ad8801_state *state = iio_priv(indio_dev);
  181. iio_device_unregister(indio_dev);
  182. if (state->vrefl_reg)
  183. regulator_disable(state->vrefl_reg);
  184. regulator_disable(state->vrefh_reg);
  185. return 0;
  186. }
  187. static const struct spi_device_id ad8801_ids[] = {
  188. {"ad8801", ID_AD8801},
  189. {"ad8803", ID_AD8803},
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(spi, ad8801_ids);
  193. static struct spi_driver ad8801_driver = {
  194. .driver = {
  195. .name = "ad8801",
  196. },
  197. .probe = ad8801_probe,
  198. .remove = ad8801_remove,
  199. .id_table = ad8801_ids,
  200. };
  201. module_spi_driver(ad8801_driver);
  202. MODULE_AUTHOR("Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>");
  203. MODULE_DESCRIPTION("Analog Devices AD8801/AD8803 DAC");
  204. MODULE_LICENSE("GPL v2");