1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2937d3a0aSQuentin Schulz /* ADC MFD core driver for sunxi platforms
3937d3a0aSQuentin Schulz *
4937d3a0aSQuentin Schulz * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons.com>
5937d3a0aSQuentin Schulz */
6937d3a0aSQuentin Schulz
7937d3a0aSQuentin Schulz #include <linux/interrupt.h>
8937d3a0aSQuentin Schulz #include <linux/kernel.h>
9937d3a0aSQuentin Schulz #include <linux/mfd/core.h>
10937d3a0aSQuentin Schulz #include <linux/module.h>
11*dc0c386eSRob Herring #include <linux/of.h>
12*dc0c386eSRob Herring #include <linux/platform_device.h>
13937d3a0aSQuentin Schulz #include <linux/regmap.h>
14937d3a0aSQuentin Schulz
15937d3a0aSQuentin Schulz #include <linux/mfd/sun4i-gpadc.h>
16937d3a0aSQuentin Schulz
17937d3a0aSQuentin Schulz #define ARCH_SUN4I_A10 0
18937d3a0aSQuentin Schulz #define ARCH_SUN5I_A13 1
19937d3a0aSQuentin Schulz #define ARCH_SUN6I_A31 2
20937d3a0aSQuentin Schulz
214bdcbbb9SRikard Falkeborn static const struct resource adc_resources[] = {
22937d3a0aSQuentin Schulz DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_FIFO_DATA, "FIFO_DATA_PENDING"),
23937d3a0aSQuentin Schulz DEFINE_RES_IRQ_NAMED(SUN4I_GPADC_IRQ_TEMP_DATA, "TEMP_DATA_PENDING"),
24937d3a0aSQuentin Schulz };
25937d3a0aSQuentin Schulz
26937d3a0aSQuentin Schulz static const struct regmap_irq sun4i_gpadc_regmap_irq[] = {
27937d3a0aSQuentin Schulz REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_FIFO_DATA, 0,
28937d3a0aSQuentin Schulz SUN4I_GPADC_INT_FIFOC_TP_DATA_IRQ_EN),
29937d3a0aSQuentin Schulz REGMAP_IRQ_REG(SUN4I_GPADC_IRQ_TEMP_DATA, 0,
30937d3a0aSQuentin Schulz SUN4I_GPADC_INT_FIFOC_TEMP_IRQ_EN),
31937d3a0aSQuentin Schulz };
32937d3a0aSQuentin Schulz
33937d3a0aSQuentin Schulz static const struct regmap_irq_chip sun4i_gpadc_regmap_irq_chip = {
34937d3a0aSQuentin Schulz .name = "sun4i_gpadc_irq_chip",
35937d3a0aSQuentin Schulz .status_base = SUN4I_GPADC_INT_FIFOS,
36937d3a0aSQuentin Schulz .ack_base = SUN4I_GPADC_INT_FIFOS,
378d9ad032SAidan MacDonald .unmask_base = SUN4I_GPADC_INT_FIFOC,
38937d3a0aSQuentin Schulz .init_ack_masked = true,
39937d3a0aSQuentin Schulz .irqs = sun4i_gpadc_regmap_irq,
40937d3a0aSQuentin Schulz .num_irqs = ARRAY_SIZE(sun4i_gpadc_regmap_irq),
41937d3a0aSQuentin Schulz .num_regs = 1,
42937d3a0aSQuentin Schulz };
43937d3a0aSQuentin Schulz
44937d3a0aSQuentin Schulz static struct mfd_cell sun4i_gpadc_cells[] = {
45937d3a0aSQuentin Schulz {
46937d3a0aSQuentin Schulz .name = "sun4i-a10-gpadc-iio",
47937d3a0aSQuentin Schulz .resources = adc_resources,
48937d3a0aSQuentin Schulz .num_resources = ARRAY_SIZE(adc_resources),
49937d3a0aSQuentin Schulz },
50937d3a0aSQuentin Schulz { .name = "iio_hwmon" }
51937d3a0aSQuentin Schulz };
52937d3a0aSQuentin Schulz
53937d3a0aSQuentin Schulz static struct mfd_cell sun5i_gpadc_cells[] = {
54937d3a0aSQuentin Schulz {
55937d3a0aSQuentin Schulz .name = "sun5i-a13-gpadc-iio",
56937d3a0aSQuentin Schulz .resources = adc_resources,
57937d3a0aSQuentin Schulz .num_resources = ARRAY_SIZE(adc_resources),
58937d3a0aSQuentin Schulz },
59937d3a0aSQuentin Schulz { .name = "iio_hwmon" },
60937d3a0aSQuentin Schulz };
61937d3a0aSQuentin Schulz
62937d3a0aSQuentin Schulz static struct mfd_cell sun6i_gpadc_cells[] = {
63937d3a0aSQuentin Schulz {
64937d3a0aSQuentin Schulz .name = "sun6i-a31-gpadc-iio",
65937d3a0aSQuentin Schulz .resources = adc_resources,
66937d3a0aSQuentin Schulz .num_resources = ARRAY_SIZE(adc_resources),
67937d3a0aSQuentin Schulz },
68937d3a0aSQuentin Schulz { .name = "iio_hwmon" },
69937d3a0aSQuentin Schulz };
70937d3a0aSQuentin Schulz
71937d3a0aSQuentin Schulz static const struct regmap_config sun4i_gpadc_regmap_config = {
72937d3a0aSQuentin Schulz .reg_bits = 32,
73937d3a0aSQuentin Schulz .val_bits = 32,
74937d3a0aSQuentin Schulz .reg_stride = 4,
75937d3a0aSQuentin Schulz .fast_io = true,
76937d3a0aSQuentin Schulz };
77937d3a0aSQuentin Schulz
78937d3a0aSQuentin Schulz static const struct of_device_id sun4i_gpadc_of_match[] = {
79937d3a0aSQuentin Schulz {
80937d3a0aSQuentin Schulz .compatible = "allwinner,sun4i-a10-ts",
81937d3a0aSQuentin Schulz .data = (void *)ARCH_SUN4I_A10,
82937d3a0aSQuentin Schulz }, {
83937d3a0aSQuentin Schulz .compatible = "allwinner,sun5i-a13-ts",
84937d3a0aSQuentin Schulz .data = (void *)ARCH_SUN5I_A13,
85937d3a0aSQuentin Schulz }, {
86937d3a0aSQuentin Schulz .compatible = "allwinner,sun6i-a31-ts",
87937d3a0aSQuentin Schulz .data = (void *)ARCH_SUN6I_A31,
88937d3a0aSQuentin Schulz }, { /* sentinel */ }
89937d3a0aSQuentin Schulz };
90937d3a0aSQuentin Schulz
91937d3a0aSQuentin Schulz MODULE_DEVICE_TABLE(of, sun4i_gpadc_of_match);
92937d3a0aSQuentin Schulz
sun4i_gpadc_probe(struct platform_device * pdev)93937d3a0aSQuentin Schulz static int sun4i_gpadc_probe(struct platform_device *pdev)
94937d3a0aSQuentin Schulz {
95937d3a0aSQuentin Schulz struct sun4i_gpadc_dev *dev;
96937d3a0aSQuentin Schulz const struct of_device_id *of_id;
97937d3a0aSQuentin Schulz const struct mfd_cell *cells;
98937d3a0aSQuentin Schulz unsigned int irq, size;
99937d3a0aSQuentin Schulz int ret;
100937d3a0aSQuentin Schulz
101937d3a0aSQuentin Schulz of_id = of_match_node(sun4i_gpadc_of_match, pdev->dev.of_node);
102937d3a0aSQuentin Schulz if (!of_id)
103937d3a0aSQuentin Schulz return -EINVAL;
104937d3a0aSQuentin Schulz
105e8ae79d6SLee Jones switch ((long)of_id->data) {
106937d3a0aSQuentin Schulz case ARCH_SUN4I_A10:
107937d3a0aSQuentin Schulz cells = sun4i_gpadc_cells;
108937d3a0aSQuentin Schulz size = ARRAY_SIZE(sun4i_gpadc_cells);
109937d3a0aSQuentin Schulz break;
110937d3a0aSQuentin Schulz case ARCH_SUN5I_A13:
111937d3a0aSQuentin Schulz cells = sun5i_gpadc_cells;
112937d3a0aSQuentin Schulz size = ARRAY_SIZE(sun5i_gpadc_cells);
113937d3a0aSQuentin Schulz break;
114937d3a0aSQuentin Schulz case ARCH_SUN6I_A31:
115937d3a0aSQuentin Schulz cells = sun6i_gpadc_cells;
116937d3a0aSQuentin Schulz size = ARRAY_SIZE(sun6i_gpadc_cells);
117937d3a0aSQuentin Schulz break;
118937d3a0aSQuentin Schulz default:
119937d3a0aSQuentin Schulz return -EINVAL;
120937d3a0aSQuentin Schulz }
121937d3a0aSQuentin Schulz
122937d3a0aSQuentin Schulz dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
123937d3a0aSQuentin Schulz if (!dev)
124937d3a0aSQuentin Schulz return -ENOMEM;
125937d3a0aSQuentin Schulz
1264723d2efSYe Xingchen dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
127937d3a0aSQuentin Schulz if (IS_ERR(dev->base))
128937d3a0aSQuentin Schulz return PTR_ERR(dev->base);
129937d3a0aSQuentin Schulz
130937d3a0aSQuentin Schulz dev->dev = &pdev->dev;
131937d3a0aSQuentin Schulz dev_set_drvdata(dev->dev, dev);
132937d3a0aSQuentin Schulz
133937d3a0aSQuentin Schulz dev->regmap = devm_regmap_init_mmio(dev->dev, dev->base,
134937d3a0aSQuentin Schulz &sun4i_gpadc_regmap_config);
135937d3a0aSQuentin Schulz if (IS_ERR(dev->regmap)) {
136937d3a0aSQuentin Schulz ret = PTR_ERR(dev->regmap);
137937d3a0aSQuentin Schulz dev_err(&pdev->dev, "failed to init regmap: %d\n", ret);
138937d3a0aSQuentin Schulz return ret;
139937d3a0aSQuentin Schulz }
140937d3a0aSQuentin Schulz
141937d3a0aSQuentin Schulz /* Disable all interrupts */
142937d3a0aSQuentin Schulz regmap_write(dev->regmap, SUN4I_GPADC_INT_FIFOC, 0);
143937d3a0aSQuentin Schulz
144937d3a0aSQuentin Schulz irq = platform_get_irq(pdev, 0);
145937d3a0aSQuentin Schulz ret = devm_regmap_add_irq_chip(&pdev->dev, dev->regmap, irq,
146937d3a0aSQuentin Schulz IRQF_ONESHOT, 0,
147937d3a0aSQuentin Schulz &sun4i_gpadc_regmap_irq_chip,
148937d3a0aSQuentin Schulz &dev->regmap_irqc);
149937d3a0aSQuentin Schulz if (ret) {
150937d3a0aSQuentin Schulz dev_err(&pdev->dev, "failed to add irq chip: %d\n", ret);
151937d3a0aSQuentin Schulz return ret;
152937d3a0aSQuentin Schulz }
153937d3a0aSQuentin Schulz
154937d3a0aSQuentin Schulz ret = devm_mfd_add_devices(dev->dev, 0, cells, size, NULL, 0, NULL);
155937d3a0aSQuentin Schulz if (ret) {
156937d3a0aSQuentin Schulz dev_err(&pdev->dev, "failed to add MFD devices: %d\n", ret);
157937d3a0aSQuentin Schulz return ret;
158937d3a0aSQuentin Schulz }
159937d3a0aSQuentin Schulz
160937d3a0aSQuentin Schulz return 0;
161937d3a0aSQuentin Schulz }
162937d3a0aSQuentin Schulz
163937d3a0aSQuentin Schulz static struct platform_driver sun4i_gpadc_driver = {
164937d3a0aSQuentin Schulz .driver = {
165937d3a0aSQuentin Schulz .name = "sun4i-gpadc",
166cc5b7ebeSKrzysztof Kozlowski .of_match_table = sun4i_gpadc_of_match,
167937d3a0aSQuentin Schulz },
168937d3a0aSQuentin Schulz .probe = sun4i_gpadc_probe,
169937d3a0aSQuentin Schulz };
170937d3a0aSQuentin Schulz
171937d3a0aSQuentin Schulz module_platform_driver(sun4i_gpadc_driver);
172937d3a0aSQuentin Schulz
173937d3a0aSQuentin Schulz MODULE_DESCRIPTION("Allwinner sunxi platforms' GPADC MFD core driver");
174937d3a0aSQuentin Schulz MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
175937d3a0aSQuentin Schulz MODULE_LICENSE("GPL v2");
176