ep93xx_adc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Driver for ADC module on the Cirrus Logic EP93xx series of SoCs
  3. *
  4. * Copyright (C) 2015 Alexander Sverdlin
  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. * The driver uses polling to get the conversion status. According to EP93xx
  11. * datasheets, reading ADCResult register starts the conversion, but user is also
  12. * responsible for ensuring that delay between adjacent conversion triggers is
  13. * long enough so that maximum allowed conversion rate is not exceeded. This
  14. * basically renders IRQ mode unusable.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/io.h>
  22. #include <linux/irqflags.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. /*
  27. * This code could benefit from real HR Timers, but jiffy granularity would
  28. * lower ADC conversion rate down to CONFIG_HZ, so we fallback to busy wait
  29. * in such case.
  30. *
  31. * HR Timers-based version loads CPU only up to 10% during back to back ADC
  32. * conversion, while busy wait-based version consumes whole CPU power.
  33. */
  34. #ifdef CONFIG_HIGH_RES_TIMERS
  35. #define ep93xx_adc_delay(usmin, usmax) usleep_range(usmin, usmax)
  36. #else
  37. #define ep93xx_adc_delay(usmin, usmax) udelay(usmin)
  38. #endif
  39. #define EP93XX_ADC_RESULT 0x08
  40. #define EP93XX_ADC_SDR BIT(31)
  41. #define EP93XX_ADC_SWITCH 0x18
  42. #define EP93XX_ADC_SW_LOCK 0x20
  43. struct ep93xx_adc_priv {
  44. struct clk *clk;
  45. void __iomem *base;
  46. int lastch;
  47. struct mutex lock;
  48. };
  49. #define EP93XX_ADC_CH(index, dname, swcfg) { \
  50. .type = IIO_VOLTAGE, \
  51. .indexed = 1, \
  52. .channel = index, \
  53. .address = swcfg, \
  54. .datasheet_name = dname, \
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  56. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) | \
  57. BIT(IIO_CHAN_INFO_OFFSET), \
  58. }
  59. /*
  60. * Numbering scheme for channels 0..4 is defined in EP9301 and EP9302 datasheets.
  61. * EP9307, EP9312 and EP9312 have 3 channels more (total 8), but the numbering is
  62. * not defined. So the last three are numbered randomly, let's say.
  63. */
  64. static const struct iio_chan_spec ep93xx_adc_channels[8] = {
  65. EP93XX_ADC_CH(0, "YM", 0x608),
  66. EP93XX_ADC_CH(1, "SXP", 0x680),
  67. EP93XX_ADC_CH(2, "SXM", 0x640),
  68. EP93XX_ADC_CH(3, "SYP", 0x620),
  69. EP93XX_ADC_CH(4, "SYM", 0x610),
  70. EP93XX_ADC_CH(5, "XP", 0x601),
  71. EP93XX_ADC_CH(6, "XM", 0x602),
  72. EP93XX_ADC_CH(7, "YP", 0x604),
  73. };
  74. static int ep93xx_read_raw(struct iio_dev *iiodev,
  75. struct iio_chan_spec const *channel, int *value,
  76. int *shift, long mask)
  77. {
  78. struct ep93xx_adc_priv *priv = iio_priv(iiodev);
  79. unsigned long timeout;
  80. int ret;
  81. switch (mask) {
  82. case IIO_CHAN_INFO_RAW:
  83. mutex_lock(&priv->lock);
  84. if (priv->lastch != channel->channel) {
  85. priv->lastch = channel->channel;
  86. /*
  87. * Switch register is software-locked, unlocking must be
  88. * immediately followed by write
  89. */
  90. local_irq_disable();
  91. writel_relaxed(0xAA, priv->base + EP93XX_ADC_SW_LOCK);
  92. writel_relaxed(channel->address,
  93. priv->base + EP93XX_ADC_SWITCH);
  94. local_irq_enable();
  95. /*
  96. * Settling delay depends on module clock and could be
  97. * 2ms or 500us
  98. */
  99. ep93xx_adc_delay(2000, 2000);
  100. }
  101. /* Start the conversion, eventually discarding old result */
  102. readl_relaxed(priv->base + EP93XX_ADC_RESULT);
  103. /* Ensure maximum conversion rate is not exceeded */
  104. ep93xx_adc_delay(DIV_ROUND_UP(1000000, 925),
  105. DIV_ROUND_UP(1000000, 925));
  106. /* At this point conversion must be completed, but anyway... */
  107. ret = IIO_VAL_INT;
  108. timeout = jiffies + msecs_to_jiffies(1) + 1;
  109. while (1) {
  110. u32 t;
  111. t = readl_relaxed(priv->base + EP93XX_ADC_RESULT);
  112. if (t & EP93XX_ADC_SDR) {
  113. *value = sign_extend32(t, 15);
  114. break;
  115. }
  116. if (time_after(jiffies, timeout)) {
  117. dev_err(&iiodev->dev, "Conversion timeout\n");
  118. ret = -ETIMEDOUT;
  119. break;
  120. }
  121. cpu_relax();
  122. }
  123. mutex_unlock(&priv->lock);
  124. return ret;
  125. case IIO_CHAN_INFO_OFFSET:
  126. /* According to datasheet, range is -25000..25000 */
  127. *value = 25000;
  128. return IIO_VAL_INT;
  129. case IIO_CHAN_INFO_SCALE:
  130. /* Typical supply voltage is 3.3v */
  131. *value = (1ULL << 32) * 3300 / 50000;
  132. *shift = 32;
  133. return IIO_VAL_FRACTIONAL_LOG2;
  134. }
  135. return -EINVAL;
  136. }
  137. static const struct iio_info ep93xx_adc_info = {
  138. .read_raw = ep93xx_read_raw,
  139. };
  140. static int ep93xx_adc_probe(struct platform_device *pdev)
  141. {
  142. int ret;
  143. struct iio_dev *iiodev;
  144. struct ep93xx_adc_priv *priv;
  145. struct clk *pclk;
  146. struct resource *res;
  147. iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
  148. if (!iiodev)
  149. return -ENOMEM;
  150. priv = iio_priv(iiodev);
  151. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. priv->base = devm_ioremap_resource(&pdev->dev, res);
  153. if (IS_ERR(priv->base)) {
  154. dev_err(&pdev->dev, "Cannot map memory resource\n");
  155. return PTR_ERR(priv->base);
  156. }
  157. iiodev->dev.parent = &pdev->dev;
  158. iiodev->name = dev_name(&pdev->dev);
  159. iiodev->modes = INDIO_DIRECT_MODE;
  160. iiodev->info = &ep93xx_adc_info;
  161. iiodev->num_channels = ARRAY_SIZE(ep93xx_adc_channels);
  162. iiodev->channels = ep93xx_adc_channels;
  163. priv->lastch = -1;
  164. mutex_init(&priv->lock);
  165. platform_set_drvdata(pdev, iiodev);
  166. priv->clk = devm_clk_get(&pdev->dev, NULL);
  167. if (IS_ERR(priv->clk)) {
  168. dev_err(&pdev->dev, "Cannot obtain clock\n");
  169. return PTR_ERR(priv->clk);
  170. }
  171. pclk = clk_get_parent(priv->clk);
  172. if (!pclk) {
  173. dev_warn(&pdev->dev, "Cannot obtain parent clock\n");
  174. } else {
  175. /*
  176. * This is actually a place for improvement:
  177. * EP93xx ADC supports two clock divisors -- 4 and 16,
  178. * resulting in conversion rates 3750 and 925 samples per second
  179. * with 500us or 2ms settling time respectively.
  180. * One might find this interesting enough to be configurable.
  181. */
  182. ret = clk_set_rate(priv->clk, clk_get_rate(pclk) / 16);
  183. if (ret)
  184. dev_warn(&pdev->dev, "Cannot set clock rate\n");
  185. /*
  186. * We can tolerate rate setting failure because the module should
  187. * work in any case.
  188. */
  189. }
  190. ret = clk_enable(priv->clk);
  191. if (ret) {
  192. dev_err(&pdev->dev, "Cannot enable clock\n");
  193. return ret;
  194. }
  195. ret = iio_device_register(iiodev);
  196. if (ret)
  197. clk_disable(priv->clk);
  198. return ret;
  199. }
  200. static int ep93xx_adc_remove(struct platform_device *pdev)
  201. {
  202. struct iio_dev *iiodev = platform_get_drvdata(pdev);
  203. struct ep93xx_adc_priv *priv = iio_priv(iiodev);
  204. iio_device_unregister(iiodev);
  205. clk_disable(priv->clk);
  206. return 0;
  207. }
  208. static struct platform_driver ep93xx_adc_driver = {
  209. .driver = {
  210. .name = "ep93xx-adc",
  211. },
  212. .probe = ep93xx_adc_probe,
  213. .remove = ep93xx_adc_remove,
  214. };
  215. module_platform_driver(ep93xx_adc_driver);
  216. MODULE_AUTHOR("Alexander Sverdlin <alexander.sverdlin@gmail.com>");
  217. MODULE_DESCRIPTION("Cirrus Logic EP93XX ADC driver");
  218. MODULE_LICENSE("GPL");
  219. MODULE_ALIAS("platform:ep93xx-adc");