dpot-dac.c 6.3 KB

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