lpc18xx_adc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * IIO ADC driver for NXP LPC18xx ADC
  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. * - Hardware triggers
  12. * - Burst mode
  13. * - Interrupts
  14. * - DMA
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/driver.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regulator/consumer.h>
  28. /* LPC18XX ADC registers and bits */
  29. #define LPC18XX_ADC_CR 0x000
  30. #define LPC18XX_ADC_CR_CLKDIV_SHIFT 8
  31. #define LPC18XX_ADC_CR_PDN BIT(21)
  32. #define LPC18XX_ADC_CR_START_NOW (0x1 << 24)
  33. #define LPC18XX_ADC_GDR 0x004
  34. /* Data register bits */
  35. #define LPC18XX_ADC_SAMPLE_SHIFT 6
  36. #define LPC18XX_ADC_SAMPLE_MASK 0x3ff
  37. #define LPC18XX_ADC_CONV_DONE BIT(31)
  38. /* Clock should be 4.5 MHz or less */
  39. #define LPC18XX_ADC_CLK_TARGET 4500000
  40. struct lpc18xx_adc {
  41. struct regulator *vref;
  42. void __iomem *base;
  43. struct device *dev;
  44. struct mutex lock;
  45. struct clk *clk;
  46. u32 cr_reg;
  47. };
  48. #define LPC18XX_ADC_CHAN(_idx) { \
  49. .type = IIO_VOLTAGE, \
  50. .indexed = 1, \
  51. .channel = _idx, \
  52. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  53. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  54. }
  55. static const struct iio_chan_spec lpc18xx_adc_iio_channels[] = {
  56. LPC18XX_ADC_CHAN(0),
  57. LPC18XX_ADC_CHAN(1),
  58. LPC18XX_ADC_CHAN(2),
  59. LPC18XX_ADC_CHAN(3),
  60. LPC18XX_ADC_CHAN(4),
  61. LPC18XX_ADC_CHAN(5),
  62. LPC18XX_ADC_CHAN(6),
  63. LPC18XX_ADC_CHAN(7),
  64. };
  65. static int lpc18xx_adc_read_chan(struct lpc18xx_adc *adc, unsigned int ch)
  66. {
  67. int ret;
  68. u32 reg;
  69. reg = adc->cr_reg | BIT(ch) | LPC18XX_ADC_CR_START_NOW;
  70. writel(reg, adc->base + LPC18XX_ADC_CR);
  71. ret = readl_poll_timeout(adc->base + LPC18XX_ADC_GDR, reg,
  72. reg & LPC18XX_ADC_CONV_DONE, 3, 9);
  73. if (ret) {
  74. dev_warn(adc->dev, "adc read timed out\n");
  75. return ret;
  76. }
  77. return (reg >> LPC18XX_ADC_SAMPLE_SHIFT) & LPC18XX_ADC_SAMPLE_MASK;
  78. }
  79. static int lpc18xx_adc_read_raw(struct iio_dev *indio_dev,
  80. struct iio_chan_spec const *chan,
  81. int *val, int *val2, long mask)
  82. {
  83. struct lpc18xx_adc *adc = iio_priv(indio_dev);
  84. switch (mask) {
  85. case IIO_CHAN_INFO_RAW:
  86. mutex_lock(&adc->lock);
  87. *val = lpc18xx_adc_read_chan(adc, chan->channel);
  88. mutex_unlock(&adc->lock);
  89. if (*val < 0)
  90. return *val;
  91. return IIO_VAL_INT;
  92. case IIO_CHAN_INFO_SCALE:
  93. *val = regulator_get_voltage(adc->vref) / 1000;
  94. *val2 = 10;
  95. return IIO_VAL_FRACTIONAL_LOG2;
  96. }
  97. return -EINVAL;
  98. }
  99. static const struct iio_info lpc18xx_adc_info = {
  100. .read_raw = lpc18xx_adc_read_raw,
  101. };
  102. static int lpc18xx_adc_probe(struct platform_device *pdev)
  103. {
  104. struct iio_dev *indio_dev;
  105. struct lpc18xx_adc *adc;
  106. struct resource *res;
  107. unsigned int clkdiv;
  108. unsigned long rate;
  109. int ret;
  110. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  111. if (!indio_dev)
  112. return -ENOMEM;
  113. platform_set_drvdata(pdev, indio_dev);
  114. adc = iio_priv(indio_dev);
  115. adc->dev = &pdev->dev;
  116. mutex_init(&adc->lock);
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. adc->base = devm_ioremap_resource(&pdev->dev, res);
  119. if (IS_ERR(adc->base))
  120. return PTR_ERR(adc->base);
  121. adc->clk = devm_clk_get(&pdev->dev, NULL);
  122. if (IS_ERR(adc->clk)) {
  123. dev_err(&pdev->dev, "error getting clock\n");
  124. return PTR_ERR(adc->clk);
  125. }
  126. rate = clk_get_rate(adc->clk);
  127. clkdiv = DIV_ROUND_UP(rate, LPC18XX_ADC_CLK_TARGET);
  128. adc->vref = devm_regulator_get(&pdev->dev, "vref");
  129. if (IS_ERR(adc->vref)) {
  130. dev_err(&pdev->dev, "error getting regulator\n");
  131. return PTR_ERR(adc->vref);
  132. }
  133. indio_dev->name = dev_name(&pdev->dev);
  134. indio_dev->dev.parent = &pdev->dev;
  135. indio_dev->info = &lpc18xx_adc_info;
  136. indio_dev->modes = INDIO_DIRECT_MODE;
  137. indio_dev->channels = lpc18xx_adc_iio_channels;
  138. indio_dev->num_channels = ARRAY_SIZE(lpc18xx_adc_iio_channels);
  139. ret = regulator_enable(adc->vref);
  140. if (ret) {
  141. dev_err(&pdev->dev, "unable to enable regulator\n");
  142. return ret;
  143. }
  144. ret = clk_prepare_enable(adc->clk);
  145. if (ret) {
  146. dev_err(&pdev->dev, "unable to enable clock\n");
  147. goto dis_reg;
  148. }
  149. adc->cr_reg = (clkdiv << LPC18XX_ADC_CR_CLKDIV_SHIFT) |
  150. LPC18XX_ADC_CR_PDN;
  151. writel(adc->cr_reg, adc->base + LPC18XX_ADC_CR);
  152. ret = iio_device_register(indio_dev);
  153. if (ret) {
  154. dev_err(&pdev->dev, "unable to register device\n");
  155. goto dis_clk;
  156. }
  157. return 0;
  158. dis_clk:
  159. writel(0, adc->base + LPC18XX_ADC_CR);
  160. clk_disable_unprepare(adc->clk);
  161. dis_reg:
  162. regulator_disable(adc->vref);
  163. return ret;
  164. }
  165. static int lpc18xx_adc_remove(struct platform_device *pdev)
  166. {
  167. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  168. struct lpc18xx_adc *adc = iio_priv(indio_dev);
  169. iio_device_unregister(indio_dev);
  170. writel(0, adc->base + LPC18XX_ADC_CR);
  171. clk_disable_unprepare(adc->clk);
  172. regulator_disable(adc->vref);
  173. return 0;
  174. }
  175. static const struct of_device_id lpc18xx_adc_match[] = {
  176. { .compatible = "nxp,lpc1850-adc" },
  177. { /* sentinel */ }
  178. };
  179. MODULE_DEVICE_TABLE(of, lpc18xx_adc_match);
  180. static struct platform_driver lpc18xx_adc_driver = {
  181. .probe = lpc18xx_adc_probe,
  182. .remove = lpc18xx_adc_remove,
  183. .driver = {
  184. .name = "lpc18xx-adc",
  185. .of_match_table = lpc18xx_adc_match,
  186. },
  187. };
  188. module_platform_driver(lpc18xx_adc_driver);
  189. MODULE_DESCRIPTION("LPC18xx ADC driver");
  190. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  191. MODULE_LICENSE("GPL v2");