sun4i-gpadc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* ADC MFD core driver for sunxi platforms
  3. *
  4. * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons.com>
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/kernel.h>
  8. #include <linux/mfd/core.h>
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/sun4i-gpadc.h>
  14. #define ARCH_SUN4I_A10 0
  15. #define ARCH_SUN5I_A13 1
  16. #define ARCH_SUN6I_A31 2
  17. static struct resource adc_resources[] = {
  18. DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_FIFO_DATA, "FIFO_DATA_PENDING"),
  19. DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_TEMP_DATA, "TEMP_DATA_PENDING"),
  20. };
  21. static const struct regmap_irq sun4i_gpadc_regmap_irq[] = {
  22. REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_FIFO_DATA, 0,
  23. SUN4I_GPADC_INT_FIFOC_TP_DATA_IRQ_EN),
  24. REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_TEMP_DATA, 0,
  25. SUN4I_GPADC_INT_FIFOC_TEMP_IRQ_EN),
  26. };
  27. static const struct regmap_irq_chip sun4i_gpadc_regmap_irq_chip = {
  28. .name = "sun4i_gpadc_irq_chip",
  29. .status_base = SUN4I_GPADC_INT_FIFOS,
  30. .ack_base = SUN4I_GPADC_INT_FIFOS,
  31. .mask_base = SUN4I_GPADC_INT_FIFOC,
  32. .init_ack_masked = true,
  33. .mask_invert = true,
  34. .irqs = sun4i_gpadc_regmap_irq,
  35. .num_irqs = ARRAY_SIZE(sun4i_gpadc_regmap_irq),
  36. .num_regs = 1,
  37. };
  38. static struct mfd_cell sun4i_gpadc_cells[] = {
  39. {
  40. .name = "sun4i-a10-gpadc-iio",
  41. .resources = adc_resources,
  42. .num_resources = ARRAY_SIZE(adc_resources),
  43. },
  44. { .name = "iio_hwmon" }
  45. };
  46. static struct mfd_cell sun5i_gpadc_cells[] = {
  47. {
  48. .name = "sun5i-a13-gpadc-iio",
  49. .resources = adc_resources,
  50. .num_resources = ARRAY_SIZE(adc_resources),
  51. },
  52. { .name = "iio_hwmon" },
  53. };
  54. static struct mfd_cell sun6i_gpadc_cells[] = {
  55. {
  56. .name = "sun6i-a31-gpadc-iio",
  57. .resources = adc_resources,
  58. .num_resources = ARRAY_SIZE(adc_resources),
  59. },
  60. { .name = "iio_hwmon" },
  61. };
  62. static const struct regmap_config sun4i_gpadc_regmap_config = {
  63. .reg_bits = 32,
  64. .val_bits = 32,
  65. .reg_stride = 4,
  66. .fast_io = true,
  67. };
  68. static const struct of_device_id sun4i_gpadc_of_match[] = {
  69. {
  70. .compatible = "allwinner,sun4i-a10-ts",
  71. .data = (void *)ARCH_SUN4I_A10,
  72. }, {
  73. .compatible = "allwinner,sun5i-a13-ts",
  74. .data = (void *)ARCH_SUN5I_A13,
  75. }, {
  76. .compatible = "allwinner,sun6i-a31-ts",
  77. .data = (void *)ARCH_SUN6I_A31,
  78. }, { /* sentinel */ }
  79. };
  80. MODULE_DEVICE_TABLE(of, sun4i_gpadc_of_match);
  81. static int sun4i_gpadc_probe(struct platform_device *pdev)
  82. {
  83. struct sun4i_gpadc_dev *dev;
  84. struct resource *mem;
  85. const struct of_device_id *of_id;
  86. const struct mfd_cell *cells;
  87. unsigned int irq, size;
  88. int ret;
  89. of_id = of_match_node(sun4i_gpadc_of_match, pdev->dev.of_node);
  90. if (!of_id)
  91. return -EINVAL;
  92. switch ((long)of_id->data) {
  93. case ARCH_SUN4I_A10:
  94. cells = sun4i_gpadc_cells;
  95. size = ARRAY_SIZE(sun4i_gpadc_cells);
  96. break;
  97. case ARCH_SUN5I_A13:
  98. cells = sun5i_gpadc_cells;
  99. size = ARRAY_SIZE(sun5i_gpadc_cells);
  100. break;
  101. case ARCH_SUN6I_A31:
  102. cells = sun6i_gpadc_cells;
  103. size = ARRAY_SIZE(sun6i_gpadc_cells);
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  109. if (!dev)
  110. return -ENOMEM;
  111. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  113. if (IS_ERR(dev->base))
  114. return PTR_ERR(dev->base);
  115. dev->dev = &pdev->dev;
  116. dev_set_drvdata(dev->dev, dev);
  117. dev->regmap = devm_regmap_init_mmio(dev->dev, dev->base,
  118. &sun4i_gpadc_regmap_config);
  119. if (IS_ERR(dev->regmap)) {
  120. ret = PTR_ERR(dev->regmap);
  121. dev_err(&pdev->dev, "failed to init regmap: %d\n", ret);
  122. return ret;
  123. }
  124. /* Disable all interrupts */
  125. regmap_write(dev->regmap, SUN4I_GPADC_INT_FIFOC, 0);
  126. irq = platform_get_irq(pdev, 0);
  127. ret = devm_regmap_add_irq_chip(&pdev->dev, dev->regmap, irq,
  128. IRQF_ONESHOT, 0,
  129. &sun4i_gpadc_regmap_irq_chip,
  130. &dev->regmap_irqc);
  131. if (ret) {
  132. dev_err(&pdev->dev, "failed to add irq chip: %d\n", ret);
  133. return ret;
  134. }
  135. ret = devm_mfd_add_devices(dev->dev, 0, cells, size, NULL, 0, NULL);
  136. if (ret) {
  137. dev_err(&pdev->dev, "failed to add MFD devices: %d\n", ret);
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. static struct platform_driver sun4i_gpadc_driver = {
  143. .driver = {
  144. .name = "sun4i-gpadc",
  145. .of_match_table = of_match_ptr(sun4i_gpadc_of_match),
  146. },
  147. .probe = sun4i_gpadc_probe,
  148. };
  149. module_platform_driver(sun4i_gpadc_driver);
  150. MODULE_DESCRIPTION("Allwinner sunxi platforms' GPADC MFD core driver");
  151. MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
  152. MODULE_LICENSE("GPL v2");