sun20i-gpadc-iio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GPADC driver for sunxi platforms (D1, T113-S3 and R329)
  4. * Copyright (c) 2023 Maksim Kiselev <bigunclemax@gmail.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/completion.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/reset.h>
  16. #include <linux/iio/iio.h>
  17. #define SUN20I_GPADC_DRIVER_NAME "sun20i-gpadc"
  18. /* Register map definition */
  19. #define SUN20I_GPADC_SR 0x00
  20. #define SUN20I_GPADC_CTRL 0x04
  21. #define SUN20I_GPADC_CS_EN 0x08
  22. #define SUN20I_GPADC_FIFO_INTC 0x0c
  23. #define SUN20I_GPADC_FIFO_INTS 0x10
  24. #define SUN20I_GPADC_FIFO_DATA 0X14
  25. #define SUN20I_GPADC_CB_DATA 0X18
  26. #define SUN20I_GPADC_DATAL_INTC 0x20
  27. #define SUN20I_GPADC_DATAH_INTC 0x24
  28. #define SUN20I_GPADC_DATA_INTC 0x28
  29. #define SUN20I_GPADC_DATAL_INTS 0x30
  30. #define SUN20I_GPADC_DATAH_INTS 0x34
  31. #define SUN20I_GPADC_DATA_INTS 0x38
  32. #define SUN20I_GPADC_CH_CMP_DATA(x) (0x40 + (x) * 4)
  33. #define SUN20I_GPADC_CH_DATA(x) (0x80 + (x) * 4)
  34. #define SUN20I_GPADC_CTRL_ADC_AUTOCALI_EN_MASK BIT(23)
  35. #define SUN20I_GPADC_CTRL_WORK_MODE_MASK GENMASK(19, 18)
  36. #define SUN20I_GPADC_CTRL_ADC_EN_MASK BIT(16)
  37. #define SUN20I_GPADC_CS_EN_ADC_CH(x) BIT(x)
  38. #define SUN20I_GPADC_DATA_INTC_CH_DATA_IRQ_EN(x) BIT(x)
  39. #define SUN20I_GPADC_WORK_MODE_SINGLE 0
  40. struct sun20i_gpadc_iio {
  41. void __iomem *regs;
  42. struct completion completion;
  43. int last_channel;
  44. /*
  45. * Lock to protect the device state during a potential concurrent
  46. * read access from userspace. Reading a raw value requires a sequence
  47. * of register writes, then a wait for a completion callback,
  48. * and finally a register read, during which userspace could issue
  49. * another read request. This lock protects a read access from
  50. * ocurring before another one has finished.
  51. */
  52. struct mutex lock;
  53. };
  54. static int sun20i_gpadc_adc_read(struct sun20i_gpadc_iio *info,
  55. struct iio_chan_spec const *chan, int *val)
  56. {
  57. u32 ctrl;
  58. int ret = IIO_VAL_INT;
  59. mutex_lock(&info->lock);
  60. reinit_completion(&info->completion);
  61. if (info->last_channel != chan->channel) {
  62. info->last_channel = chan->channel;
  63. /* enable the analog input channel */
  64. writel(SUN20I_GPADC_CS_EN_ADC_CH(chan->channel),
  65. info->regs + SUN20I_GPADC_CS_EN);
  66. /* enable the data irq for input channel */
  67. writel(SUN20I_GPADC_DATA_INTC_CH_DATA_IRQ_EN(chan->channel),
  68. info->regs + SUN20I_GPADC_DATA_INTC);
  69. }
  70. /* enable the ADC function */
  71. ctrl = readl(info->regs + SUN20I_GPADC_CTRL);
  72. ctrl |= FIELD_PREP(SUN20I_GPADC_CTRL_ADC_EN_MASK, 1);
  73. writel(ctrl, info->regs + SUN20I_GPADC_CTRL);
  74. /*
  75. * According to the datasheet maximum acquire time(TACQ) can be
  76. * (65535+1)/24Mhz and conversion time(CONV_TIME) is always constant
  77. * and equal to 14/24Mhz, so (TACQ+CONV_TIME) <= 2.73125ms.
  78. * A 10ms delay should be enough to make sure an interrupt occurs in
  79. * normal conditions. If it doesn't occur, then there is a timeout.
  80. */
  81. if (!wait_for_completion_timeout(&info->completion, msecs_to_jiffies(10))) {
  82. ret = -ETIMEDOUT;
  83. goto err_unlock;
  84. }
  85. /* read the ADC data */
  86. *val = readl(info->regs + SUN20I_GPADC_CH_DATA(chan->channel));
  87. err_unlock:
  88. mutex_unlock(&info->lock);
  89. return ret;
  90. }
  91. static int sun20i_gpadc_read_raw(struct iio_dev *indio_dev,
  92. struct iio_chan_spec const *chan, int *val,
  93. int *val2, long mask)
  94. {
  95. struct sun20i_gpadc_iio *info = iio_priv(indio_dev);
  96. switch (mask) {
  97. case IIO_CHAN_INFO_RAW:
  98. return sun20i_gpadc_adc_read(info, chan, val);
  99. case IIO_CHAN_INFO_SCALE:
  100. /* value in mv = 1800mV / 4096 raw */
  101. *val = 1800;
  102. *val2 = 12;
  103. return IIO_VAL_FRACTIONAL_LOG2;
  104. default:
  105. return -EINVAL;
  106. }
  107. }
  108. static irqreturn_t sun20i_gpadc_irq_handler(int irq, void *data)
  109. {
  110. struct sun20i_gpadc_iio *info = data;
  111. /* clear data interrupt status register */
  112. writel(GENMASK(31, 0), info->regs + SUN20I_GPADC_DATA_INTS);
  113. complete(&info->completion);
  114. return IRQ_HANDLED;
  115. }
  116. static const struct iio_info sun20i_gpadc_iio_info = {
  117. .read_raw = sun20i_gpadc_read_raw,
  118. };
  119. static void sun20i_gpadc_reset_assert(void *data)
  120. {
  121. struct reset_control *rst = data;
  122. reset_control_assert(rst);
  123. }
  124. static int sun20i_gpadc_alloc_channels(struct iio_dev *indio_dev,
  125. struct device *dev)
  126. {
  127. unsigned int channel;
  128. int num_channels, i, ret;
  129. struct iio_chan_spec *channels;
  130. struct fwnode_handle *node;
  131. num_channels = device_get_child_node_count(dev);
  132. if (num_channels == 0)
  133. return dev_err_probe(dev, -ENODEV, "no channel children\n");
  134. channels = devm_kcalloc(dev, num_channels, sizeof(*channels),
  135. GFP_KERNEL);
  136. if (!channels)
  137. return -ENOMEM;
  138. i = 0;
  139. device_for_each_child_node(dev, node) {
  140. ret = fwnode_property_read_u32(node, "reg", &channel);
  141. if (ret) {
  142. fwnode_handle_put(node);
  143. return dev_err_probe(dev, ret, "invalid channel number\n");
  144. }
  145. channels[i].type = IIO_VOLTAGE;
  146. channels[i].indexed = 1;
  147. channels[i].channel = channel;
  148. channels[i].info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  149. channels[i].info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  150. i++;
  151. }
  152. indio_dev->channels = channels;
  153. indio_dev->num_channels = num_channels;
  154. return 0;
  155. }
  156. static int sun20i_gpadc_probe(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. struct iio_dev *indio_dev;
  160. struct sun20i_gpadc_iio *info;
  161. struct reset_control *rst;
  162. struct clk *clk;
  163. int irq;
  164. int ret;
  165. indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
  166. if (!indio_dev)
  167. return -ENOMEM;
  168. info = iio_priv(indio_dev);
  169. info->last_channel = -1;
  170. mutex_init(&info->lock);
  171. init_completion(&info->completion);
  172. ret = sun20i_gpadc_alloc_channels(indio_dev, dev);
  173. if (ret)
  174. return ret;
  175. indio_dev->info = &sun20i_gpadc_iio_info;
  176. indio_dev->name = SUN20I_GPADC_DRIVER_NAME;
  177. info->regs = devm_platform_ioremap_resource(pdev, 0);
  178. if (IS_ERR(info->regs))
  179. return PTR_ERR(info->regs);
  180. clk = devm_clk_get_enabled(dev, NULL);
  181. if (IS_ERR(clk))
  182. return dev_err_probe(dev, PTR_ERR(clk), "failed to enable bus clock\n");
  183. rst = devm_reset_control_get_exclusive(dev, NULL);
  184. if (IS_ERR(rst))
  185. return dev_err_probe(dev, PTR_ERR(rst), "failed to get reset control\n");
  186. ret = reset_control_deassert(rst);
  187. if (ret)
  188. return dev_err_probe(dev, ret, "failed to deassert reset\n");
  189. ret = devm_add_action_or_reset(dev, sun20i_gpadc_reset_assert, rst);
  190. if (ret)
  191. return ret;
  192. irq = platform_get_irq(pdev, 0);
  193. if (irq < 0)
  194. return irq;
  195. ret = devm_request_irq(dev, irq, sun20i_gpadc_irq_handler, 0,
  196. dev_name(dev), info);
  197. if (ret)
  198. return dev_err_probe(dev, ret, "failed requesting irq %d\n", irq);
  199. writel(FIELD_PREP(SUN20I_GPADC_CTRL_ADC_AUTOCALI_EN_MASK, 1) |
  200. FIELD_PREP(SUN20I_GPADC_CTRL_WORK_MODE_MASK, SUN20I_GPADC_WORK_MODE_SINGLE),
  201. info->regs + SUN20I_GPADC_CTRL);
  202. ret = devm_iio_device_register(dev, indio_dev);
  203. if (ret)
  204. return dev_err_probe(dev, ret, "could not register the device\n");
  205. return 0;
  206. }
  207. static const struct of_device_id sun20i_gpadc_of_id[] = {
  208. { .compatible = "allwinner,sun20i-d1-gpadc" },
  209. { /* sentinel */ }
  210. };
  211. MODULE_DEVICE_TABLE(of, sun20i_gpadc_of_id);
  212. static struct platform_driver sun20i_gpadc_driver = {
  213. .driver = {
  214. .name = SUN20I_GPADC_DRIVER_NAME,
  215. .of_match_table = sun20i_gpadc_of_id,
  216. },
  217. .probe = sun20i_gpadc_probe,
  218. };
  219. module_platform_driver(sun20i_gpadc_driver);
  220. MODULE_DESCRIPTION("ADC driver for sunxi platforms");
  221. MODULE_AUTHOR("Maksim Kiselev <bigunclemax@gmail.com>");
  222. MODULE_LICENSE("GPL");