lpc18xx_dac.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * IIO DAC driver for NXP LPC18xx DAC
  3. *
  4. * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * UNSUPPORTED hardware features:
  11. * - Interrupts
  12. * - DMA
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/driver.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regulator/consumer.h>
  26. /* LPC18XX DAC registers and bits */
  27. #define LPC18XX_DAC_CR 0x000
  28. #define LPC18XX_DAC_CR_VALUE_SHIFT 6
  29. #define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
  30. #define LPC18XX_DAC_CR_BIAS BIT(16)
  31. #define LPC18XX_DAC_CTRL 0x004
  32. #define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
  33. struct lpc18xx_dac {
  34. struct regulator *vref;
  35. void __iomem *base;
  36. struct mutex lock;
  37. struct clk *clk;
  38. };
  39. static const struct iio_chan_spec lpc18xx_dac_iio_channels[] = {
  40. {
  41. .type = IIO_VOLTAGE,
  42. .output = 1,
  43. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  44. BIT(IIO_CHAN_INFO_SCALE),
  45. },
  46. };
  47. static int lpc18xx_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 lpc18xx_dac *dac = iio_priv(indio_dev);
  52. u32 reg;
  53. switch (mask) {
  54. case IIO_CHAN_INFO_RAW:
  55. reg = readl(dac->base + LPC18XX_DAC_CR);
  56. *val = reg >> LPC18XX_DAC_CR_VALUE_SHIFT;
  57. *val &= LPC18XX_DAC_CR_VALUE_MASK;
  58. return IIO_VAL_INT;
  59. case IIO_CHAN_INFO_SCALE:
  60. *val = regulator_get_voltage(dac->vref) / 1000;
  61. *val2 = 10;
  62. return IIO_VAL_FRACTIONAL_LOG2;
  63. }
  64. return -EINVAL;
  65. }
  66. static int lpc18xx_dac_write_raw(struct iio_dev *indio_dev,
  67. struct iio_chan_spec const *chan,
  68. int val, int val2, long mask)
  69. {
  70. struct lpc18xx_dac *dac = iio_priv(indio_dev);
  71. u32 reg;
  72. switch (mask) {
  73. case IIO_CHAN_INFO_RAW:
  74. if (val < 0 || val > LPC18XX_DAC_CR_VALUE_MASK)
  75. return -EINVAL;
  76. reg = LPC18XX_DAC_CR_BIAS;
  77. reg |= val << LPC18XX_DAC_CR_VALUE_SHIFT;
  78. mutex_lock(&dac->lock);
  79. writel(reg, dac->base + LPC18XX_DAC_CR);
  80. writel(LPC18XX_DAC_CTRL_DMA_ENA, dac->base + LPC18XX_DAC_CTRL);
  81. mutex_unlock(&dac->lock);
  82. return 0;
  83. }
  84. return -EINVAL;
  85. }
  86. static const struct iio_info lpc18xx_dac_info = {
  87. .read_raw = lpc18xx_dac_read_raw,
  88. .write_raw = lpc18xx_dac_write_raw,
  89. .driver_module = THIS_MODULE,
  90. };
  91. static int lpc18xx_dac_probe(struct platform_device *pdev)
  92. {
  93. struct iio_dev *indio_dev;
  94. struct lpc18xx_dac *dac;
  95. struct resource *res;
  96. int ret;
  97. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
  98. if (!indio_dev)
  99. return -ENOMEM;
  100. platform_set_drvdata(pdev, indio_dev);
  101. dac = iio_priv(indio_dev);
  102. mutex_init(&dac->lock);
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. dac->base = devm_ioremap_resource(&pdev->dev, res);
  105. if (IS_ERR(dac->base))
  106. return PTR_ERR(dac->base);
  107. dac->clk = devm_clk_get(&pdev->dev, NULL);
  108. if (IS_ERR(dac->clk)) {
  109. dev_err(&pdev->dev, "error getting clock\n");
  110. return PTR_ERR(dac->clk);
  111. }
  112. dac->vref = devm_regulator_get(&pdev->dev, "vref");
  113. if (IS_ERR(dac->vref)) {
  114. dev_err(&pdev->dev, "error getting regulator\n");
  115. return PTR_ERR(dac->vref);
  116. }
  117. indio_dev->name = dev_name(&pdev->dev);
  118. indio_dev->dev.parent = &pdev->dev;
  119. indio_dev->info = &lpc18xx_dac_info;
  120. indio_dev->modes = INDIO_DIRECT_MODE;
  121. indio_dev->channels = lpc18xx_dac_iio_channels;
  122. indio_dev->num_channels = ARRAY_SIZE(lpc18xx_dac_iio_channels);
  123. ret = regulator_enable(dac->vref);
  124. if (ret) {
  125. dev_err(&pdev->dev, "unable to enable regulator\n");
  126. return ret;
  127. }
  128. ret = clk_prepare_enable(dac->clk);
  129. if (ret) {
  130. dev_err(&pdev->dev, "unable to enable clock\n");
  131. goto dis_reg;
  132. }
  133. writel(0, dac->base + LPC18XX_DAC_CTRL);
  134. writel(0, dac->base + LPC18XX_DAC_CR);
  135. ret = iio_device_register(indio_dev);
  136. if (ret) {
  137. dev_err(&pdev->dev, "unable to register device\n");
  138. goto dis_clk;
  139. }
  140. return 0;
  141. dis_clk:
  142. clk_disable_unprepare(dac->clk);
  143. dis_reg:
  144. regulator_disable(dac->vref);
  145. return ret;
  146. }
  147. static int lpc18xx_dac_remove(struct platform_device *pdev)
  148. {
  149. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  150. struct lpc18xx_dac *dac = iio_priv(indio_dev);
  151. iio_device_unregister(indio_dev);
  152. writel(0, dac->base + LPC18XX_DAC_CTRL);
  153. clk_disable_unprepare(dac->clk);
  154. regulator_disable(dac->vref);
  155. return 0;
  156. }
  157. static const struct of_device_id lpc18xx_dac_match[] = {
  158. { .compatible = "nxp,lpc1850-dac" },
  159. { /* sentinel */ }
  160. };
  161. MODULE_DEVICE_TABLE(of, lpc18xx_dac_match);
  162. static struct platform_driver lpc18xx_dac_driver = {
  163. .probe = lpc18xx_dac_probe,
  164. .remove = lpc18xx_dac_remove,
  165. .driver = {
  166. .name = "lpc18xx-dac",
  167. .of_match_table = lpc18xx_dac_match,
  168. },
  169. };
  170. module_platform_driver(lpc18xx_dac_driver);
  171. MODULE_DESCRIPTION("LPC18xx DAC driver");
  172. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  173. MODULE_LICENSE("GPL v2");