lpc32xx_adc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * lpc32xx_adc.c - Support for ADC in LPC32XX
  4. *
  5. * 3-channel, 10-bit ADC
  6. *
  7. * Copyright (C) 2011, 2012 Roland Stigge <stigge@antcom.de>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/completion.h>
  11. #include <linux/err.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/mutex.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. /*
  21. * LPC32XX registers definitions
  22. */
  23. #define LPC32XXAD_SELECT(x) ((x) + 0x04)
  24. #define LPC32XXAD_CTRL(x) ((x) + 0x08)
  25. #define LPC32XXAD_VALUE(x) ((x) + 0x48)
  26. /* Bit definitions for LPC32XXAD_SELECT: */
  27. /* constant, always write this value! */
  28. #define LPC32XXAD_REFm 0x00000200
  29. /* constant, always write this value! */
  30. #define LPC32XXAD_REFp 0x00000080
  31. /* multiple of this is the channel number: 0, 1, 2 */
  32. #define LPC32XXAD_IN 0x00000010
  33. /* constant, always write this value! */
  34. #define LPC32XXAD_INTERNAL 0x00000004
  35. /* Bit definitions for LPC32XXAD_CTRL: */
  36. #define LPC32XXAD_STROBE 0x00000002
  37. #define LPC32XXAD_PDN_CTRL 0x00000004
  38. /* Bit definitions for LPC32XXAD_VALUE: */
  39. #define LPC32XXAD_VALUE_MASK 0x000003FF
  40. #define LPC32XXAD_NAME "lpc32xx-adc"
  41. struct lpc32xx_adc_state {
  42. void __iomem *adc_base;
  43. struct clk *clk;
  44. struct completion completion;
  45. struct regulator *vref;
  46. /* lock to protect against multiple access to the device */
  47. struct mutex lock;
  48. u32 value;
  49. };
  50. static int lpc32xx_read_raw(struct iio_dev *indio_dev,
  51. struct iio_chan_spec const *chan,
  52. int *val,
  53. int *val2,
  54. long mask)
  55. {
  56. struct lpc32xx_adc_state *st = iio_priv(indio_dev);
  57. int ret;
  58. switch (mask) {
  59. case IIO_CHAN_INFO_RAW:
  60. mutex_lock(&st->lock);
  61. ret = clk_prepare_enable(st->clk);
  62. if (ret) {
  63. mutex_unlock(&st->lock);
  64. return ret;
  65. }
  66. /* Measurement setup */
  67. __raw_writel(LPC32XXAD_INTERNAL | (chan->address) |
  68. LPC32XXAD_REFp | LPC32XXAD_REFm,
  69. LPC32XXAD_SELECT(st->adc_base));
  70. /* Trigger conversion */
  71. __raw_writel(LPC32XXAD_PDN_CTRL | LPC32XXAD_STROBE,
  72. LPC32XXAD_CTRL(st->adc_base));
  73. wait_for_completion(&st->completion); /* set by ISR */
  74. clk_disable_unprepare(st->clk);
  75. *val = st->value;
  76. mutex_unlock(&st->lock);
  77. return IIO_VAL_INT;
  78. case IIO_CHAN_INFO_SCALE:
  79. *val = regulator_get_voltage(st->vref) / 1000;
  80. *val2 = 10;
  81. return IIO_VAL_FRACTIONAL_LOG2;
  82. default:
  83. return -EINVAL;
  84. }
  85. }
  86. static const struct iio_info lpc32xx_adc_iio_info = {
  87. .read_raw = &lpc32xx_read_raw,
  88. };
  89. #define LPC32XX_ADC_CHANNEL_BASE(_index) \
  90. .type = IIO_VOLTAGE, \
  91. .indexed = 1, \
  92. .channel = _index, \
  93. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  94. .address = LPC32XXAD_IN * _index, \
  95. .scan_index = _index,
  96. #define LPC32XX_ADC_CHANNEL(_index) { \
  97. LPC32XX_ADC_CHANNEL_BASE(_index) \
  98. }
  99. #define LPC32XX_ADC_SCALE_CHANNEL(_index) { \
  100. LPC32XX_ADC_CHANNEL_BASE(_index) \
  101. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  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 const struct iio_chan_spec lpc32xx_adc_iio_scale_channels[] = {
  109. LPC32XX_ADC_SCALE_CHANNEL(0),
  110. LPC32XX_ADC_SCALE_CHANNEL(1),
  111. LPC32XX_ADC_SCALE_CHANNEL(2),
  112. };
  113. static irqreturn_t lpc32xx_adc_isr(int irq, void *dev_id)
  114. {
  115. struct lpc32xx_adc_state *st = dev_id;
  116. /* Read value and clear irq */
  117. st->value = __raw_readl(LPC32XXAD_VALUE(st->adc_base)) &
  118. LPC32XXAD_VALUE_MASK;
  119. complete(&st->completion);
  120. return IRQ_HANDLED;
  121. }
  122. static int lpc32xx_adc_probe(struct platform_device *pdev)
  123. {
  124. struct lpc32xx_adc_state *st = NULL;
  125. struct resource *res;
  126. int retval = -ENODEV;
  127. struct iio_dev *iodev = NULL;
  128. int irq;
  129. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  130. if (!res) {
  131. dev_err(&pdev->dev, "failed to get platform I/O memory\n");
  132. return -ENXIO;
  133. }
  134. iodev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  135. if (!iodev)
  136. return -ENOMEM;
  137. st = iio_priv(iodev);
  138. st->adc_base = devm_ioremap(&pdev->dev, res->start,
  139. resource_size(res));
  140. if (!st->adc_base) {
  141. dev_err(&pdev->dev, "failed mapping memory\n");
  142. return -EBUSY;
  143. }
  144. st->clk = devm_clk_get(&pdev->dev, NULL);
  145. if (IS_ERR(st->clk)) {
  146. dev_err(&pdev->dev, "failed getting clock\n");
  147. return PTR_ERR(st->clk);
  148. }
  149. irq = platform_get_irq(pdev, 0);
  150. if (irq < 0)
  151. return irq;
  152. retval = devm_request_irq(&pdev->dev, irq, lpc32xx_adc_isr, 0,
  153. LPC32XXAD_NAME, st);
  154. if (retval < 0) {
  155. dev_err(&pdev->dev, "failed requesting interrupt\n");
  156. return retval;
  157. }
  158. st->vref = devm_regulator_get(&pdev->dev, "vref");
  159. if (IS_ERR(st->vref)) {
  160. iodev->channels = lpc32xx_adc_iio_channels;
  161. dev_info(&pdev->dev,
  162. "Missing vref regulator: No scaling available\n");
  163. } else {
  164. iodev->channels = lpc32xx_adc_iio_scale_channels;
  165. }
  166. platform_set_drvdata(pdev, iodev);
  167. init_completion(&st->completion);
  168. iodev->name = LPC32XXAD_NAME;
  169. iodev->info = &lpc32xx_adc_iio_info;
  170. iodev->modes = INDIO_DIRECT_MODE;
  171. iodev->num_channels = ARRAY_SIZE(lpc32xx_adc_iio_channels);
  172. mutex_init(&st->lock);
  173. retval = devm_iio_device_register(&pdev->dev, iodev);
  174. if (retval)
  175. return retval;
  176. dev_info(&pdev->dev, "LPC32XX ADC driver loaded, IRQ %d\n", irq);
  177. return 0;
  178. }
  179. static const struct of_device_id lpc32xx_adc_match[] = {
  180. { .compatible = "nxp,lpc3220-adc" },
  181. { }
  182. };
  183. MODULE_DEVICE_TABLE(of, lpc32xx_adc_match);
  184. static struct platform_driver lpc32xx_adc_driver = {
  185. .probe = lpc32xx_adc_probe,
  186. .driver = {
  187. .name = LPC32XXAD_NAME,
  188. .of_match_table = lpc32xx_adc_match,
  189. },
  190. };
  191. module_platform_driver(lpc32xx_adc_driver);
  192. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  193. MODULE_DESCRIPTION("LPC32XX ADC driver");
  194. MODULE_LICENSE("GPL");