sun4i-gpadc.c 4.6 KB

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