vf610_dac.c 7.0 KB

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