ep93xx_adc.c 6.6 KB

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