Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* ADC MFD core driver for sunxi platforms
0003  *
0004  * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons.com>
0005  */
0006 
0007 #include <linux/interrupt.h>
0008 #include <linux/kernel.h>
0009 #include <linux/mfd/core.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/of_irq.h>
0013 #include <linux/regmap.h>
0014 
0015 #include <linux/mfd/sun4i-gpadc.h>
0016 
0017 #define ARCH_SUN4I_A10 0
0018 #define ARCH_SUN5I_A13 1
0019 #define ARCH_SUN6I_A31 2
0020 
0021 static const struct resource adc_resources[] = {
0022     DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_FIFO_DATA, "FIFO_DATA_PENDING"),
0023     DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_TEMP_DATA, "TEMP_DATA_PENDING"),
0024 };
0025 
0026 static const struct regmap_irq sun4i_gpadc_regmap_irq[] = {
0027     REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_FIFO_DATA, 0,
0028                SUN4I_GPADC_INT_FIFOC_TP_DATA_IRQ_EN),
0029     REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_TEMP_DATA, 0,
0030                SUN4I_GPADC_INT_FIFOC_TEMP_IRQ_EN),
0031 };
0032 
0033 static const struct regmap_irq_chip sun4i_gpadc_regmap_irq_chip = {
0034     .name = "sun4i_gpadc_irq_chip",
0035     .status_base = SUN4I_GPADC_INT_FIFOS,
0036     .ack_base = SUN4I_GPADC_INT_FIFOS,
0037     .mask_base = SUN4I_GPADC_INT_FIFOC,
0038     .init_ack_masked = true,
0039     .mask_invert = true,
0040     .irqs = sun4i_gpadc_regmap_irq,
0041     .num_irqs = ARRAY_SIZE(sun4i_gpadc_regmap_irq),
0042     .num_regs = 1,
0043 };
0044 
0045 static struct mfd_cell sun4i_gpadc_cells[] = {
0046     {
0047         .name   = "sun4i-a10-gpadc-iio",
0048         .resources = adc_resources,
0049         .num_resources = ARRAY_SIZE(adc_resources),
0050     },
0051     { .name = "iio_hwmon" }
0052 };
0053 
0054 static struct mfd_cell sun5i_gpadc_cells[] = {
0055     {
0056         .name   = "sun5i-a13-gpadc-iio",
0057         .resources = adc_resources,
0058         .num_resources = ARRAY_SIZE(adc_resources),
0059     },
0060     { .name = "iio_hwmon" },
0061 };
0062 
0063 static struct mfd_cell sun6i_gpadc_cells[] = {
0064     {
0065         .name   = "sun6i-a31-gpadc-iio",
0066         .resources = adc_resources,
0067         .num_resources = ARRAY_SIZE(adc_resources),
0068     },
0069     { .name = "iio_hwmon" },
0070 };
0071 
0072 static const struct regmap_config sun4i_gpadc_regmap_config = {
0073     .reg_bits = 32,
0074     .val_bits = 32,
0075     .reg_stride = 4,
0076     .fast_io = true,
0077 };
0078 
0079 static const struct of_device_id sun4i_gpadc_of_match[] = {
0080     {
0081         .compatible = "allwinner,sun4i-a10-ts",
0082         .data = (void *)ARCH_SUN4I_A10,
0083     }, {
0084         .compatible = "allwinner,sun5i-a13-ts",
0085         .data = (void *)ARCH_SUN5I_A13,
0086     }, {
0087         .compatible = "allwinner,sun6i-a31-ts",
0088         .data = (void *)ARCH_SUN6I_A31,
0089     }, { /* sentinel */ }
0090 };
0091 
0092 MODULE_DEVICE_TABLE(of, sun4i_gpadc_of_match);
0093 
0094 static int sun4i_gpadc_probe(struct platform_device *pdev)
0095 {
0096     struct sun4i_gpadc_dev *dev;
0097     struct resource *mem;
0098     const struct of_device_id *of_id;
0099     const struct mfd_cell *cells;
0100     unsigned int irq, size;
0101     int ret;
0102 
0103     of_id = of_match_node(sun4i_gpadc_of_match, pdev->dev.of_node);
0104     if (!of_id)
0105         return -EINVAL;
0106 
0107     switch ((long)of_id->data) {
0108     case ARCH_SUN4I_A10:
0109         cells = sun4i_gpadc_cells;
0110         size = ARRAY_SIZE(sun4i_gpadc_cells);
0111         break;
0112     case ARCH_SUN5I_A13:
0113         cells = sun5i_gpadc_cells;
0114         size = ARRAY_SIZE(sun5i_gpadc_cells);
0115         break;
0116     case ARCH_SUN6I_A31:
0117         cells = sun6i_gpadc_cells;
0118         size = ARRAY_SIZE(sun6i_gpadc_cells);
0119         break;
0120     default:
0121         return -EINVAL;
0122     }
0123 
0124     dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
0125     if (!dev)
0126         return -ENOMEM;
0127 
0128     mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0129     dev->base = devm_ioremap_resource(&pdev->dev, mem);
0130     if (IS_ERR(dev->base))
0131         return PTR_ERR(dev->base);
0132 
0133     dev->dev = &pdev->dev;
0134     dev_set_drvdata(dev->dev, dev);
0135 
0136     dev->regmap = devm_regmap_init_mmio(dev->dev, dev->base,
0137                         &sun4i_gpadc_regmap_config);
0138     if (IS_ERR(dev->regmap)) {
0139         ret = PTR_ERR(dev->regmap);
0140         dev_err(&pdev->dev, "failed to init regmap: %d\n", ret);
0141         return ret;
0142     }
0143 
0144     /* Disable all interrupts */
0145     regmap_write(dev->regmap, SUN4I_GPADC_INT_FIFOC, 0);
0146 
0147     irq = platform_get_irq(pdev, 0);
0148     ret = devm_regmap_add_irq_chip(&pdev->dev, dev->regmap, irq,
0149                        IRQF_ONESHOT, 0,
0150                        &sun4i_gpadc_regmap_irq_chip,
0151                        &dev->regmap_irqc);
0152     if (ret) {
0153         dev_err(&pdev->dev, "failed to add irq chip: %d\n", ret);
0154         return ret;
0155     }
0156 
0157     ret = devm_mfd_add_devices(dev->dev, 0, cells, size, NULL, 0, NULL);
0158     if (ret) {
0159         dev_err(&pdev->dev, "failed to add MFD devices: %d\n", ret);
0160         return ret;
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 static struct platform_driver sun4i_gpadc_driver = {
0167     .driver = {
0168         .name = "sun4i-gpadc",
0169         .of_match_table = sun4i_gpadc_of_match,
0170     },
0171     .probe = sun4i_gpadc_probe,
0172 };
0173 
0174 module_platform_driver(sun4i_gpadc_driver);
0175 
0176 MODULE_DESCRIPTION("Allwinner sunxi platforms' GPADC MFD core driver");
0177 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
0178 MODULE_LICENSE("GPL v2");