vf610_dac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale Vybrid vf610 DAC driver
  4. *
  5. * Copyright 2016 Toradex AG
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/slab.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #define VF610_DACx_STATCTRL 0x20
  20. #define VF610_DAC_DACEN BIT(15)
  21. #define VF610_DAC_DACRFS BIT(14)
  22. #define VF610_DAC_LPEN BIT(11)
  23. #define VF610_DAC_DAT0(x) ((x) & 0xFFF)
  24. enum vf610_conversion_mode_sel {
  25. VF610_DAC_CONV_HIGH_POWER,
  26. VF610_DAC_CONV_LOW_POWER,
  27. };
  28. struct vf610_dac {
  29. struct clk *clk;
  30. struct device *dev;
  31. enum vf610_conversion_mode_sel conv_mode;
  32. void __iomem *regs;
  33. struct mutex lock;
  34. };
  35. static void vf610_dac_init(struct vf610_dac *info)
  36. {
  37. int val;
  38. info->conv_mode = VF610_DAC_CONV_LOW_POWER;
  39. val = VF610_DAC_DACEN | VF610_DAC_DACRFS |
  40. VF610_DAC_LPEN;
  41. writel(val, info->regs + VF610_DACx_STATCTRL);
  42. }
  43. static void vf610_dac_exit(struct vf610_dac *info)
  44. {
  45. int val;
  46. val = readl(info->regs + VF610_DACx_STATCTRL);
  47. val &= ~VF610_DAC_DACEN;
  48. writel(val, info->regs + VF610_DACx_STATCTRL);
  49. }
  50. static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
  51. const struct iio_chan_spec *chan,
  52. unsigned int mode)
  53. {
  54. struct vf610_dac *info = iio_priv(indio_dev);
  55. int val;
  56. mutex_lock(&info->lock);
  57. info->conv_mode = mode;
  58. val = readl(info->regs + VF610_DACx_STATCTRL);
  59. if (mode)
  60. val |= VF610_DAC_LPEN;
  61. else
  62. val &= ~VF610_DAC_LPEN;
  63. writel(val, info->regs + VF610_DACx_STATCTRL);
  64. mutex_unlock(&info->lock);
  65. return 0;
  66. }
  67. static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
  68. const struct iio_chan_spec *chan)
  69. {
  70. struct vf610_dac *info = iio_priv(indio_dev);
  71. return info->conv_mode;
  72. }
  73. static const char * const vf610_conv_modes[] = { "high-power", "low-power" };
  74. static const struct iio_enum vf610_conversion_mode = {
  75. .items = vf610_conv_modes,
  76. .num_items = ARRAY_SIZE(vf610_conv_modes),
  77. .get = vf610_get_conversion_mode,
  78. .set = vf610_set_conversion_mode,
  79. };
  80. static const struct iio_chan_spec_ext_info vf610_ext_info[] = {
  81. IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR,
  82. &vf610_conversion_mode),
  83. {},
  84. };
  85. #define VF610_DAC_CHAN(_chan_type) { \
  86. .type = (_chan_type), \
  87. .output = 1, \
  88. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  89. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  90. .ext_info = vf610_ext_info, \
  91. }
  92. static const struct iio_chan_spec vf610_dac_iio_channels[] = {
  93. VF610_DAC_CHAN(IIO_VOLTAGE),
  94. };
  95. static int vf610_read_raw(struct iio_dev *indio_dev,
  96. struct iio_chan_spec const *chan,
  97. int *val, int *val2,
  98. long mask)
  99. {
  100. struct vf610_dac *info = iio_priv(indio_dev);
  101. switch (mask) {
  102. case IIO_CHAN_INFO_RAW:
  103. *val = VF610_DAC_DAT0(readl(info->regs));
  104. return IIO_VAL_INT;
  105. case IIO_CHAN_INFO_SCALE:
  106. /*
  107. * DACRFS is always 1 for valid reference and typical
  108. * reference voltage as per Vybrid datasheet is 3.3V
  109. * from section 9.1.2.1 of Vybrid datasheet
  110. */
  111. *val = 3300 /* mV */;
  112. *val2 = 12;
  113. return IIO_VAL_FRACTIONAL_LOG2;
  114. default:
  115. return -EINVAL;
  116. }
  117. }
  118. static int vf610_write_raw(struct iio_dev *indio_dev,
  119. struct iio_chan_spec const *chan,
  120. int val, int val2,
  121. long mask)
  122. {
  123. struct vf610_dac *info = iio_priv(indio_dev);
  124. switch (mask) {
  125. case IIO_CHAN_INFO_RAW:
  126. mutex_lock(&info->lock);
  127. writel(VF610_DAC_DAT0(val), info->regs);
  128. mutex_unlock(&info->lock);
  129. return 0;
  130. default:
  131. return -EINVAL;
  132. }
  133. }
  134. static const struct iio_info vf610_dac_iio_info = {
  135. .read_raw = &vf610_read_raw,
  136. .write_raw = &vf610_write_raw,
  137. };
  138. static const struct of_device_id vf610_dac_match[] = {
  139. { .compatible = "fsl,vf610-dac", },
  140. { /* sentinel */ }
  141. };
  142. MODULE_DEVICE_TABLE(of, vf610_dac_match);
  143. static int vf610_dac_probe(struct platform_device *pdev)
  144. {
  145. struct iio_dev *indio_dev;
  146. struct vf610_dac *info;
  147. int ret;
  148. indio_dev = devm_iio_device_alloc(&pdev->dev,
  149. sizeof(struct vf610_dac));
  150. if (!indio_dev) {
  151. dev_err(&pdev->dev, "Failed allocating iio device\n");
  152. return -ENOMEM;
  153. }
  154. info = iio_priv(indio_dev);
  155. info->dev = &pdev->dev;
  156. info->regs = devm_platform_ioremap_resource(pdev, 0);
  157. if (IS_ERR(info->regs))
  158. return PTR_ERR(info->regs);
  159. info->clk = devm_clk_get(&pdev->dev, "dac");
  160. if (IS_ERR(info->clk)) {
  161. dev_err(&pdev->dev, "Failed getting clock, err = %ld\n",
  162. PTR_ERR(info->clk));
  163. return PTR_ERR(info->clk);
  164. }
  165. platform_set_drvdata(pdev, indio_dev);
  166. indio_dev->name = dev_name(&pdev->dev);
  167. indio_dev->info = &vf610_dac_iio_info;
  168. indio_dev->modes = INDIO_DIRECT_MODE;
  169. indio_dev->channels = vf610_dac_iio_channels;
  170. indio_dev->num_channels = ARRAY_SIZE(vf610_dac_iio_channels);
  171. mutex_init(&info->lock);
  172. ret = clk_prepare_enable(info->clk);
  173. if (ret) {
  174. dev_err(&pdev->dev,
  175. "Could not prepare or enable the clock\n");
  176. return ret;
  177. }
  178. vf610_dac_init(info);
  179. ret = iio_device_register(indio_dev);
  180. if (ret) {
  181. dev_err(&pdev->dev, "Couldn't register the device\n");
  182. goto error_iio_device_register;
  183. }
  184. return 0;
  185. error_iio_device_register:
  186. vf610_dac_exit(info);
  187. clk_disable_unprepare(info->clk);
  188. return ret;
  189. }
  190. static int vf610_dac_remove(struct platform_device *pdev)
  191. {
  192. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  193. struct vf610_dac *info = iio_priv(indio_dev);
  194. iio_device_unregister(indio_dev);
  195. vf610_dac_exit(info);
  196. clk_disable_unprepare(info->clk);
  197. return 0;
  198. }
  199. static int vf610_dac_suspend(struct device *dev)
  200. {
  201. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  202. struct vf610_dac *info = iio_priv(indio_dev);
  203. vf610_dac_exit(info);
  204. clk_disable_unprepare(info->clk);
  205. return 0;
  206. }
  207. static int vf610_dac_resume(struct device *dev)
  208. {
  209. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  210. struct vf610_dac *info = iio_priv(indio_dev);
  211. int ret;
  212. ret = clk_prepare_enable(info->clk);
  213. if (ret)
  214. return ret;
  215. vf610_dac_init(info);
  216. return 0;
  217. }
  218. static DEFINE_SIMPLE_DEV_PM_OPS(vf610_dac_pm_ops, vf610_dac_suspend,
  219. vf610_dac_resume);
  220. static struct platform_driver vf610_dac_driver = {
  221. .probe = vf610_dac_probe,
  222. .remove = vf610_dac_remove,
  223. .driver = {
  224. .name = "vf610-dac",
  225. .of_match_table = vf610_dac_match,
  226. .pm = pm_sleep_ptr(&vf610_dac_pm_ops),
  227. },
  228. };
  229. module_platform_driver(vf610_dac_driver);
  230. MODULE_AUTHOR("Sanchayan Maity <[email protected]>");
  231. MODULE_DESCRIPTION("Freescale VF610 DAC driver");
  232. MODULE_LICENSE("GPL v2");