lpc18xx_adc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. .driver_module = THIS_MODULE,
  102. };
  103. static int lpc18xx_adc_probe(struct platform_device *pdev)
  104. {
  105. struct iio_dev *indio_dev;
  106. struct lpc18xx_adc *adc;
  107. struct resource *res;
  108. unsigned int clkdiv;
  109. unsigned long rate;
  110. int ret;
  111. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  112. if (!indio_dev)
  113. return -ENOMEM;
  114. platform_set_drvdata(pdev, indio_dev);
  115. adc = iio_priv(indio_dev);
  116. adc->dev = &pdev->dev;
  117. mutex_init(&adc->lock);
  118. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  119. adc->base = devm_ioremap_resource(&pdev->dev, res);
  120. if (IS_ERR(adc->base))
  121. return PTR_ERR(adc->base);
  122. adc->clk = devm_clk_get(&pdev->dev, NULL);
  123. if (IS_ERR(adc->clk)) {
  124. dev_err(&pdev->dev, "error getting clock\n");
  125. return PTR_ERR(adc->clk);
  126. }
  127. rate = clk_get_rate(adc->clk);
  128. clkdiv = DIV_ROUND_UP(rate, LPC18XX_ADC_CLK_TARGET);
  129. adc->vref = devm_regulator_get(&pdev->dev, "vref");
  130. if (IS_ERR(adc->vref)) {
  131. dev_err(&pdev->dev, "error getting regulator\n");
  132. return PTR_ERR(adc->vref);
  133. }
  134. indio_dev->name = dev_name(&pdev->dev);
  135. indio_dev->dev.parent = &pdev->dev;
  136. indio_dev->info = &lpc18xx_adc_info;
  137. indio_dev->modes = INDIO_DIRECT_MODE;
  138. indio_dev->channels = lpc18xx_adc_iio_channels;
  139. indio_dev->num_channels = ARRAY_SIZE(lpc18xx_adc_iio_channels);
  140. ret = regulator_enable(adc->vref);
  141. if (ret) {
  142. dev_err(&pdev->dev, "unable to enable regulator\n");
  143. return ret;
  144. }
  145. ret = clk_prepare_enable(adc->clk);
  146. if (ret) {
  147. dev_err(&pdev->dev, "unable to enable clock\n");
  148. goto dis_reg;
  149. }
  150. adc->cr_reg = (clkdiv << LPC18XX_ADC_CR_CLKDIV_SHIFT) |
  151. LPC18XX_ADC_CR_PDN;
  152. writel(adc->cr_reg, adc->base + LPC18XX_ADC_CR);
  153. ret = iio_device_register(indio_dev);
  154. if (ret) {
  155. dev_err(&pdev->dev, "unable to register device\n");
  156. goto dis_clk;
  157. }
  158. return 0;
  159. dis_clk:
  160. writel(0, adc->base + LPC18XX_ADC_CR);
  161. clk_disable_unprepare(adc->clk);
  162. dis_reg:
  163. regulator_disable(adc->vref);
  164. return ret;
  165. }
  166. static int lpc18xx_adc_remove(struct platform_device *pdev)
  167. {
  168. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  169. struct lpc18xx_adc *adc = iio_priv(indio_dev);
  170. iio_device_unregister(indio_dev);
  171. writel(0, adc->base + LPC18XX_ADC_CR);
  172. clk_disable_unprepare(adc->clk);
  173. regulator_disable(adc->vref);
  174. return 0;
  175. }
  176. static const struct of_device_id lpc18xx_adc_match[] = {
  177. { .compatible = "nxp,lpc1850-adc" },
  178. { /* sentinel */ }
  179. };
  180. MODULE_DEVICE_TABLE(of, lpc18xx_adc_match);
  181. static struct platform_driver lpc18xx_adc_driver = {
  182. .probe = lpc18xx_adc_probe,
  183. .remove = lpc18xx_adc_remove,
  184. .driver = {
  185. .name = "lpc18xx-adc",
  186. .of_match_table = lpc18xx_adc_match,
  187. },
  188. };
  189. module_platform_driver(lpc18xx_adc_driver);
  190. MODULE_DESCRIPTION("LPC18xx ADC driver");
  191. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  192. MODULE_LICENSE("GPL v2");