dpot-dac.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. .driver_module = THIS_MODULE,
  116. };
  117. static int dpot_dac_channel_max_ohms(struct iio_dev *indio_dev)
  118. {
  119. struct device *dev = &indio_dev->dev;
  120. struct dpot_dac *dac = iio_priv(indio_dev);
  121. unsigned long long tmp;
  122. int ret;
  123. int val;
  124. int val2;
  125. int max;
  126. ret = iio_read_max_channel_raw(dac->dpot, &max);
  127. if (ret < 0) {
  128. dev_err(dev, "dpot does not indicate its raw maximum value\n");
  129. return ret;
  130. }
  131. switch (iio_read_channel_scale(dac->dpot, &val, &val2)) {
  132. case IIO_VAL_INT:
  133. return max * val;
  134. case IIO_VAL_FRACTIONAL:
  135. tmp = (unsigned long long)max * val;
  136. do_div(tmp, val2);
  137. return tmp;
  138. case IIO_VAL_FRACTIONAL_LOG2:
  139. tmp = val * 1000000000LL * max >> val2;
  140. do_div(tmp, 1000000000LL);
  141. return tmp;
  142. default:
  143. dev_err(dev, "dpot has a scale that is too weird\n");
  144. }
  145. return -EINVAL;
  146. }
  147. static int dpot_dac_probe(struct platform_device *pdev)
  148. {
  149. struct device *dev = &pdev->dev;
  150. struct iio_dev *indio_dev;
  151. struct dpot_dac *dac;
  152. enum iio_chan_type type;
  153. int ret;
  154. indio_dev = devm_iio_device_alloc(dev, sizeof(*dac));
  155. if (!indio_dev)
  156. return -ENOMEM;
  157. platform_set_drvdata(pdev, indio_dev);
  158. dac = iio_priv(indio_dev);
  159. indio_dev->name = dev_name(dev);
  160. indio_dev->dev.parent = dev;
  161. indio_dev->info = &dpot_dac_info;
  162. indio_dev->modes = INDIO_DIRECT_MODE;
  163. indio_dev->channels = &dpot_dac_iio_channel;
  164. indio_dev->num_channels = 1;
  165. dac->vref = devm_regulator_get(dev, "vref");
  166. if (IS_ERR(dac->vref)) {
  167. if (PTR_ERR(dac->vref) != -EPROBE_DEFER)
  168. dev_err(&pdev->dev, "failed to get vref regulator\n");
  169. return PTR_ERR(dac->vref);
  170. }
  171. dac->dpot = devm_iio_channel_get(dev, "dpot");
  172. if (IS_ERR(dac->dpot)) {
  173. if (PTR_ERR(dac->dpot) != -EPROBE_DEFER)
  174. dev_err(dev, "failed to get dpot input channel\n");
  175. return PTR_ERR(dac->dpot);
  176. }
  177. ret = iio_get_channel_type(dac->dpot, &type);
  178. if (ret < 0)
  179. return ret;
  180. if (type != IIO_RESISTANCE) {
  181. dev_err(dev, "dpot is of the wrong type\n");
  182. return -EINVAL;
  183. }
  184. ret = dpot_dac_channel_max_ohms(indio_dev);
  185. if (ret < 0)
  186. return ret;
  187. dac->max_ohms = ret;
  188. ret = regulator_enable(dac->vref);
  189. if (ret) {
  190. dev_err(dev, "failed to enable the vref regulator\n");
  191. return ret;
  192. }
  193. ret = iio_device_register(indio_dev);
  194. if (ret) {
  195. dev_err(dev, "failed to register iio device\n");
  196. goto disable_reg;
  197. }
  198. return 0;
  199. disable_reg:
  200. regulator_disable(dac->vref);
  201. return ret;
  202. }
  203. static int dpot_dac_remove(struct platform_device *pdev)
  204. {
  205. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  206. struct dpot_dac *dac = iio_priv(indio_dev);
  207. iio_device_unregister(indio_dev);
  208. regulator_disable(dac->vref);
  209. return 0;
  210. }
  211. static const struct of_device_id dpot_dac_match[] = {
  212. { .compatible = "dpot-dac" },
  213. { /* sentinel */ }
  214. };
  215. MODULE_DEVICE_TABLE(of, dpot_dac_match);
  216. static struct platform_driver dpot_dac_driver = {
  217. .probe = dpot_dac_probe,
  218. .remove = dpot_dac_remove,
  219. .driver = {
  220. .name = "iio-dpot-dac",
  221. .of_match_table = dpot_dac_match,
  222. },
  223. };
  224. module_platform_driver(dpot_dac_driver);
  225. MODULE_DESCRIPTION("DAC emulation driver using a digital potentiometer");
  226. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  227. MODULE_LICENSE("GPL v2");