lpc32xx_adc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * lpc32xx_adc.c - Support for ADC in LPC32XX
  3. *
  4. * 3-channel, 10-bit ADC
  5. *
  6. * Copyright (C) 2011, 2012 Roland Stigge <stigge@antcom.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/device.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <linux/clk.h>
  30. #include <linux/err.h>
  31. #include <linux/completion.h>
  32. #include <linux/of.h>
  33. #include <linux/iio/iio.h>
  34. #include <linux/iio/sysfs.h>
  35. /*
  36. * LPC32XX registers definitions
  37. */
  38. #define LPC32XXAD_SELECT(x) ((x) + 0x04)
  39. #define LPC32XXAD_CTRL(x) ((x) + 0x08)
  40. #define LPC32XXAD_VALUE(x) ((x) + 0x48)
  41. /* Bit definitions for LPC32XXAD_SELECT: */
  42. /* constant, always write this value! */
  43. #define LPC32XXAD_REFm 0x00000200
  44. /* constant, always write this value! */
  45. #define LPC32XXAD_REFp 0x00000080
  46. /* multiple of this is the channel number: 0, 1, 2 */
  47. #define LPC32XXAD_IN 0x00000010
  48. /* constant, always write this value! */
  49. #define LPC32XXAD_INTERNAL 0x00000004
  50. /* Bit definitions for LPC32XXAD_CTRL: */
  51. #define LPC32XXAD_STROBE 0x00000002
  52. #define LPC32XXAD_PDN_CTRL 0x00000004
  53. /* Bit definitions for LPC32XXAD_VALUE: */
  54. #define LPC32XXAD_VALUE_MASK 0x000003FF
  55. #define LPC32XXAD_NAME "lpc32xx-adc"
  56. struct lpc32xx_adc_state {
  57. void __iomem *adc_base;
  58. struct clk *clk;
  59. struct completion completion;
  60. u32 value;
  61. };
  62. static int lpc32xx_read_raw(struct iio_dev *indio_dev,
  63. struct iio_chan_spec const *chan,
  64. int *val,
  65. int *val2,
  66. long mask)
  67. {
  68. struct lpc32xx_adc_state *st = iio_priv(indio_dev);
  69. int ret;
  70. if (mask == IIO_CHAN_INFO_RAW) {
  71. mutex_lock(&indio_dev->mlock);
  72. ret = clk_prepare_enable(st->clk);
  73. if (ret) {
  74. mutex_unlock(&indio_dev->mlock);
  75. return ret;
  76. }
  77. /* Measurement setup */
  78. __raw_writel(LPC32XXAD_INTERNAL | (chan->address) |
  79. LPC32XXAD_REFp | LPC32XXAD_REFm,
  80. LPC32XXAD_SELECT(st->adc_base));
  81. /* Trigger conversion */
  82. __raw_writel(LPC32XXAD_PDN_CTRL | LPC32XXAD_STROBE,
  83. LPC32XXAD_CTRL(st->adc_base));
  84. wait_for_completion(&st->completion); /* set by ISR */
  85. clk_disable_unprepare(st->clk);
  86. *val = st->value;
  87. mutex_unlock(&indio_dev->mlock);
  88. return IIO_VAL_INT;
  89. }
  90. return -EINVAL;
  91. }
  92. static const struct iio_info lpc32xx_adc_iio_info = {
  93. .read_raw = &lpc32xx_read_raw,
  94. };
  95. #define LPC32XX_ADC_CHANNEL(_index) { \
  96. .type = IIO_VOLTAGE, \
  97. .indexed = 1, \
  98. .channel = _index, \
  99. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  100. .address = LPC32XXAD_IN * _index, \
  101. .scan_index = _index, \
  102. }
  103. static const struct iio_chan_spec lpc32xx_adc_iio_channels[] = {
  104. LPC32XX_ADC_CHANNEL(0),
  105. LPC32XX_ADC_CHANNEL(1),
  106. LPC32XX_ADC_CHANNEL(2),
  107. };
  108. static irqreturn_t lpc32xx_adc_isr(int irq, void *dev_id)
  109. {
  110. struct lpc32xx_adc_state *st = dev_id;
  111. /* Read value and clear irq */
  112. st->value = __raw_readl(LPC32XXAD_VALUE(st->adc_base)) &
  113. LPC32XXAD_VALUE_MASK;
  114. complete(&st->completion);
  115. return IRQ_HANDLED;
  116. }
  117. static int lpc32xx_adc_probe(struct platform_device *pdev)
  118. {
  119. struct lpc32xx_adc_state *st = NULL;
  120. struct resource *res;
  121. int retval = -ENODEV;
  122. struct iio_dev *iodev = NULL;
  123. int irq;
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (!res) {
  126. dev_err(&pdev->dev, "failed to get platform I/O memory\n");
  127. return -ENXIO;
  128. }
  129. iodev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  130. if (!iodev)
  131. return -ENOMEM;
  132. st = iio_priv(iodev);
  133. st->adc_base = devm_ioremap(&pdev->dev, res->start,
  134. resource_size(res));
  135. if (!st->adc_base) {
  136. dev_err(&pdev->dev, "failed mapping memory\n");
  137. return -EBUSY;
  138. }
  139. st->clk = devm_clk_get(&pdev->dev, NULL);
  140. if (IS_ERR(st->clk)) {
  141. dev_err(&pdev->dev, "failed getting clock\n");
  142. return PTR_ERR(st->clk);
  143. }
  144. irq = platform_get_irq(pdev, 0);
  145. if (irq <= 0) {
  146. dev_err(&pdev->dev, "failed getting interrupt resource\n");
  147. return -ENXIO;
  148. }
  149. retval = devm_request_irq(&pdev->dev, irq, lpc32xx_adc_isr, 0,
  150. LPC32XXAD_NAME, st);
  151. if (retval < 0) {
  152. dev_err(&pdev->dev, "failed requesting interrupt\n");
  153. return retval;
  154. }
  155. platform_set_drvdata(pdev, iodev);
  156. init_completion(&st->completion);
  157. iodev->name = LPC32XXAD_NAME;
  158. iodev->dev.parent = &pdev->dev;
  159. iodev->info = &lpc32xx_adc_iio_info;
  160. iodev->modes = INDIO_DIRECT_MODE;
  161. iodev->channels = lpc32xx_adc_iio_channels;
  162. iodev->num_channels = ARRAY_SIZE(lpc32xx_adc_iio_channels);
  163. retval = devm_iio_device_register(&pdev->dev, iodev);
  164. if (retval)
  165. return retval;
  166. dev_info(&pdev->dev, "LPC32XX ADC driver loaded, IRQ %d\n", irq);
  167. return 0;
  168. }
  169. #ifdef CONFIG_OF
  170. static const struct of_device_id lpc32xx_adc_match[] = {
  171. { .compatible = "nxp,lpc3220-adc" },
  172. {},
  173. };
  174. MODULE_DEVICE_TABLE(of, lpc32xx_adc_match);
  175. #endif
  176. static struct platform_driver lpc32xx_adc_driver = {
  177. .probe = lpc32xx_adc_probe,
  178. .driver = {
  179. .name = LPC32XXAD_NAME,
  180. .of_match_table = of_match_ptr(lpc32xx_adc_match),
  181. },
  182. };
  183. module_platform_driver(lpc32xx_adc_driver);
  184. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  185. MODULE_DESCRIPTION("LPC32XX ADC driver");
  186. MODULE_LICENSE("GPL");