dpot-dac.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IIO DAC emulation driver using a digital potentiometer
  4. *
  5. * Copyright (C) 2016 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. /*
  10. * It is assumed that the dpot is used as a voltage divider between the
  11. * current dpot wiper setting and the maximum resistance of the dpot. The
  12. * divided voltage is provided by a vref regulator.
  13. *
  14. * .------.
  15. * .-----------. | |
  16. * | vref |--' .---.
  17. * | regulator |--. | |
  18. * '-----------' | | d |
  19. * | | p |
  20. * | | o | wiper
  21. * | | t |<---------+
  22. * | | |
  23. * | '---' dac output voltage
  24. * | |
  25. * '------+------------+
  26. */
  27. #include <linux/err.h>
  28. #include <linux/iio/consumer.h>
  29. #include <linux/iio/iio.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regulator/consumer.h>
  34. struct dpot_dac {
  35. struct regulator *vref;
  36. struct iio_channel *dpot;
  37. u32 max_ohms;
  38. };
  39. static const struct iio_chan_spec dpot_dac_iio_channel = {
  40. .type = IIO_VOLTAGE,
  41. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
  42. | BIT(IIO_CHAN_INFO_SCALE),
  43. .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
  44. .output = 1,
  45. .indexed = 1,
  46. };
  47. static int dpot_dac_read_raw(struct iio_dev *indio_dev,
  48. struct iio_chan_spec const *chan,
  49. int *val, int *val2, long mask)
  50. {
  51. struct dpot_dac *dac = iio_priv(indio_dev);
  52. int ret;
  53. unsigned long long tmp;
  54. switch (mask) {
  55. case IIO_CHAN_INFO_RAW:
  56. return iio_read_channel_raw(dac->dpot, val);
  57. case IIO_CHAN_INFO_SCALE:
  58. ret = iio_read_channel_scale(dac->dpot, val, val2);
  59. switch (ret) {
  60. case IIO_VAL_FRACTIONAL_LOG2:
  61. tmp = *val * 1000000000LL;
  62. do_div(tmp, dac->max_ohms);
  63. tmp *= regulator_get_voltage(dac->vref) / 1000;
  64. do_div(tmp, 1000000000LL);
  65. *val = tmp;
  66. return ret;
  67. case IIO_VAL_INT:
  68. /*
  69. * Convert integer scale to fractional scale by
  70. * setting the denominator (val2) to one...
  71. */
  72. *val2 = 1;
  73. ret = IIO_VAL_FRACTIONAL;
  74. /* ...and fall through. Say it again for GCC. */
  75. fallthrough;
  76. case IIO_VAL_FRACTIONAL:
  77. *val *= regulator_get_voltage(dac->vref) / 1000;
  78. *val2 *= dac->max_ohms;
  79. break;
  80. }
  81. return ret;
  82. }
  83. return -EINVAL;
  84. }
  85. static int dpot_dac_read_avail(struct iio_dev *indio_dev,
  86. struct iio_chan_spec const *chan,
  87. const int **vals, int *type, int *length,
  88. long mask)
  89. {
  90. struct dpot_dac *dac = iio_priv(indio_dev);
  91. switch (mask) {
  92. case IIO_CHAN_INFO_RAW:
  93. *type = IIO_VAL_INT;
  94. return iio_read_avail_channel_raw(dac->dpot, vals, length);
  95. }
  96. return -EINVAL;
  97. }
  98. static int dpot_dac_write_raw(struct iio_dev *indio_dev,
  99. struct iio_chan_spec const *chan,
  100. int val, int val2, long mask)
  101. {
  102. struct dpot_dac *dac = iio_priv(indio_dev);
  103. switch (mask) {
  104. case IIO_CHAN_INFO_RAW:
  105. return iio_write_channel_raw(dac->dpot, val);
  106. }
  107. return -EINVAL;
  108. }
  109. static const struct iio_info dpot_dac_info = {
  110. .read_raw = dpot_dac_read_raw,
  111. .read_avail = dpot_dac_read_avail,
  112. .write_raw = dpot_dac_write_raw,
  113. };
  114. static int dpot_dac_channel_max_ohms(struct iio_dev *indio_dev)
  115. {
  116. struct device *dev = &indio_dev->dev;
  117. struct dpot_dac *dac = iio_priv(indio_dev);
  118. unsigned long long tmp;
  119. int ret;
  120. int val;
  121. int val2;
  122. int max;
  123. ret = iio_read_max_channel_raw(dac->dpot, &max);
  124. if (ret < 0) {
  125. dev_err(dev, "dpot does not indicate its raw maximum value\n");
  126. return ret;
  127. }
  128. switch (iio_read_channel_scale(dac->dpot, &val, &val2)) {
  129. case IIO_VAL_INT:
  130. return max * val;
  131. case IIO_VAL_FRACTIONAL:
  132. tmp = (unsigned long long)max * val;
  133. do_div(tmp, val2);
  134. return tmp;
  135. case IIO_VAL_FRACTIONAL_LOG2:
  136. tmp = val * 1000000000LL * max >> val2;
  137. do_div(tmp, 1000000000LL);
  138. return tmp;
  139. default:
  140. dev_err(dev, "dpot has a scale that is too weird\n");
  141. }
  142. return -EINVAL;
  143. }
  144. static int dpot_dac_probe(struct platform_device *pdev)
  145. {
  146. struct device *dev = &pdev->dev;
  147. struct iio_dev *indio_dev;
  148. struct dpot_dac *dac;
  149. enum iio_chan_type type;
  150. int ret;
  151. indio_dev = devm_iio_device_alloc(dev, sizeof(*dac));
  152. if (!indio_dev)
  153. return -ENOMEM;
  154. platform_set_drvdata(pdev, indio_dev);
  155. dac = iio_priv(indio_dev);
  156. indio_dev->name = dev_name(dev);
  157. indio_dev->info = &dpot_dac_info;
  158. indio_dev->modes = INDIO_DIRECT_MODE;
  159. indio_dev->channels = &dpot_dac_iio_channel;
  160. indio_dev->num_channels = 1;
  161. dac->vref = devm_regulator_get(dev, "vref");
  162. if (IS_ERR(dac->vref))
  163. return dev_err_probe(&pdev->dev, PTR_ERR(dac->vref),
  164. "failed to get vref regulator\n");
  165. dac->dpot = devm_iio_channel_get(dev, "dpot");
  166. if (IS_ERR(dac->dpot))
  167. return dev_err_probe(&pdev->dev, PTR_ERR(dac->dpot),
  168. "failed to get dpot input channel\n");
  169. ret = iio_get_channel_type(dac->dpot, &type);
  170. if (ret < 0)
  171. return ret;
  172. if (type != IIO_RESISTANCE) {
  173. dev_err(dev, "dpot is of the wrong type\n");
  174. return -EINVAL;
  175. }
  176. ret = dpot_dac_channel_max_ohms(indio_dev);
  177. if (ret < 0)
  178. return ret;
  179. dac->max_ohms = ret;
  180. ret = regulator_enable(dac->vref);
  181. if (ret) {
  182. dev_err(dev, "failed to enable the vref regulator\n");
  183. return ret;
  184. }
  185. ret = iio_device_register(indio_dev);
  186. if (ret) {
  187. dev_err(dev, "failed to register iio device\n");
  188. goto disable_reg;
  189. }
  190. return 0;
  191. disable_reg:
  192. regulator_disable(dac->vref);
  193. return ret;
  194. }
  195. static int dpot_dac_remove(struct platform_device *pdev)
  196. {
  197. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  198. struct dpot_dac *dac = iio_priv(indio_dev);
  199. iio_device_unregister(indio_dev);
  200. regulator_disable(dac->vref);
  201. return 0;
  202. }
  203. static const struct of_device_id dpot_dac_match[] = {
  204. { .compatible = "dpot-dac" },
  205. { /* sentinel */ }
  206. };
  207. MODULE_DEVICE_TABLE(of, dpot_dac_match);
  208. static struct platform_driver dpot_dac_driver = {
  209. .probe = dpot_dac_probe,
  210. .remove = dpot_dac_remove,
  211. .driver = {
  212. .name = "iio-dpot-dac",
  213. .of_match_table = dpot_dac_match,
  214. },
  215. };
  216. module_platform_driver(dpot_dac_driver);
  217. MODULE_DESCRIPTION("DAC emulation driver using a digital potentiometer");
  218. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  219. MODULE_LICENSE("GPL v2");