1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2017-2018 Cadence
7 * Jan Kotas <jank@cadence.com>
8 * Boris Brezillon <boris.brezillon@free-electrons.com>
11 #include <linux/gpio/driver.h>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
19 #define CDNS_GPIO_BYPASS_MODE 0x00
20 #define CDNS_GPIO_DIRECTION_MODE 0x04
21 #define CDNS_GPIO_OUTPUT_EN 0x08
22 #define CDNS_GPIO_OUTPUT_VALUE 0x0c
23 #define CDNS_GPIO_INPUT_VALUE 0x10
24 #define CDNS_GPIO_IRQ_MASK 0x14
25 #define CDNS_GPIO_IRQ_EN 0x18
26 #define CDNS_GPIO_IRQ_DIS 0x1c
27 #define CDNS_GPIO_IRQ_STATUS 0x20
28 #define CDNS_GPIO_IRQ_TYPE 0x24
29 #define CDNS_GPIO_IRQ_VALUE 0x28
30 #define CDNS_GPIO_IRQ_ANY_EDGE 0x2c
32 struct cdns_gpio_chip {
39 static int cdns_gpio_request(struct gpio_chip *chip, unsigned int offset)
41 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
44 raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
46 iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) & ~BIT(offset),
47 cgpio->regs + CDNS_GPIO_BYPASS_MODE);
49 raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
53 static void cdns_gpio_free(struct gpio_chip *chip, unsigned int offset)
55 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
58 raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
60 iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) |
61 (BIT(offset) & cgpio->bypass_orig),
62 cgpio->regs + CDNS_GPIO_BYPASS_MODE);
64 raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
67 static void cdns_gpio_irq_mask(struct irq_data *d)
69 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
70 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
72 iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_DIS);
73 gpiochip_disable_irq(chip, irqd_to_hwirq(d));
76 static void cdns_gpio_irq_unmask(struct irq_data *d)
78 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
79 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
81 gpiochip_enable_irq(chip, irqd_to_hwirq(d));
82 iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_EN);
85 static int cdns_gpio_irq_set_type(struct irq_data *d, unsigned int type)
87 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
88 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
92 u32 mask = BIT(d->hwirq);
95 raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
97 int_value = ioread32(cgpio->regs + CDNS_GPIO_IRQ_VALUE) & ~mask;
98 int_type = ioread32(cgpio->regs + CDNS_GPIO_IRQ_TYPE) & ~mask;
101 * The GPIO controller doesn't have an ACK register.
102 * All interrupt statuses are cleared on a status register read.
103 * Don't support edge interrupts for now.
106 if (type == IRQ_TYPE_LEVEL_HIGH) {
109 } else if (type == IRQ_TYPE_LEVEL_LOW) {
116 iowrite32(int_value, cgpio->regs + CDNS_GPIO_IRQ_VALUE);
117 iowrite32(int_type, cgpio->regs + CDNS_GPIO_IRQ_TYPE);
120 raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
124 static void cdns_gpio_irq_handler(struct irq_desc *desc)
126 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
127 struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
128 struct irq_chip *irqchip = irq_desc_get_chip(desc);
129 unsigned long status;
132 chained_irq_enter(irqchip, desc);
134 status = ioread32(cgpio->regs + CDNS_GPIO_IRQ_STATUS) &
135 ~ioread32(cgpio->regs + CDNS_GPIO_IRQ_MASK);
137 for_each_set_bit(hwirq, &status, chip->ngpio)
138 generic_handle_domain_irq(chip->irq.domain, hwirq);
140 chained_irq_exit(irqchip, desc);
143 static const struct irq_chip cdns_gpio_irqchip = {
145 .irq_mask = cdns_gpio_irq_mask,
146 .irq_unmask = cdns_gpio_irq_unmask,
147 .irq_set_type = cdns_gpio_irq_set_type,
148 .flags = IRQCHIP_IMMUTABLE,
149 GPIOCHIP_IRQ_RESOURCE_HELPERS,
152 static int cdns_gpio_probe(struct platform_device *pdev)
154 struct cdns_gpio_chip *cgpio;
159 cgpio = devm_kzalloc(&pdev->dev, sizeof(*cgpio), GFP_KERNEL);
163 cgpio->regs = devm_platform_ioremap_resource(pdev, 0);
164 if (IS_ERR(cgpio->regs))
165 return PTR_ERR(cgpio->regs);
167 of_property_read_u32(pdev->dev.of_node, "ngpios", &num_gpios);
169 if (num_gpios > 32) {
170 dev_err(&pdev->dev, "ngpios must be less or equal 32\n");
175 * Set all pins as inputs by default, otherwise:
176 * gpiochip_lock_as_irq:
177 * tried to flag a GPIO set as output for IRQ
178 * Generic GPIO driver stores the direction value internally,
179 * so it needs to be changed before bgpio_init() is called.
181 dir_prev = ioread32(cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
182 iowrite32(GENMASK(num_gpios - 1, 0),
183 cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
185 ret = bgpio_init(&cgpio->gc, &pdev->dev, 4,
186 cgpio->regs + CDNS_GPIO_INPUT_VALUE,
187 cgpio->regs + CDNS_GPIO_OUTPUT_VALUE,
190 cgpio->regs + CDNS_GPIO_DIRECTION_MODE,
191 BGPIOF_READ_OUTPUT_REG_SET);
193 dev_err(&pdev->dev, "Failed to register generic gpio, %d\n",
198 cgpio->gc.label = dev_name(&pdev->dev);
199 cgpio->gc.ngpio = num_gpios;
200 cgpio->gc.parent = &pdev->dev;
202 cgpio->gc.owner = THIS_MODULE;
203 cgpio->gc.request = cdns_gpio_request;
204 cgpio->gc.free = cdns_gpio_free;
206 cgpio->pclk = devm_clk_get(&pdev->dev, NULL);
207 if (IS_ERR(cgpio->pclk)) {
208 ret = PTR_ERR(cgpio->pclk);
210 "Failed to retrieve peripheral clock, %d\n", ret);
214 ret = clk_prepare_enable(cgpio->pclk);
217 "Failed to enable the peripheral clock, %d\n", ret);
222 * Optional irq_chip support
224 irq = platform_get_irq(pdev, 0);
226 struct gpio_irq_chip *girq;
228 girq = &cgpio->gc.irq;
229 gpio_irq_chip_set_chip(girq, &cdns_gpio_irqchip);
230 girq->parent_handler = cdns_gpio_irq_handler;
231 girq->num_parents = 1;
232 girq->parents = devm_kcalloc(&pdev->dev, 1,
233 sizeof(*girq->parents),
235 if (!girq->parents) {
237 goto err_disable_clk;
239 girq->parents[0] = irq;
240 girq->default_type = IRQ_TYPE_NONE;
241 girq->handler = handle_level_irq;
244 ret = devm_gpiochip_add_data(&pdev->dev, &cgpio->gc, cgpio);
246 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
247 goto err_disable_clk;
250 cgpio->bypass_orig = ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE);
253 * Enable gpio outputs, ignored for input direction
255 iowrite32(GENMASK(num_gpios - 1, 0),
256 cgpio->regs + CDNS_GPIO_OUTPUT_EN);
257 iowrite32(0, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
259 platform_set_drvdata(pdev, cgpio);
263 clk_disable_unprepare(cgpio->pclk);
266 iowrite32(dir_prev, cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
271 static int cdns_gpio_remove(struct platform_device *pdev)
273 struct cdns_gpio_chip *cgpio = platform_get_drvdata(pdev);
275 iowrite32(cgpio->bypass_orig, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
276 clk_disable_unprepare(cgpio->pclk);
281 static const struct of_device_id cdns_of_ids[] = {
282 { .compatible = "cdns,gpio-r1p02" },
285 MODULE_DEVICE_TABLE(of, cdns_of_ids);
287 static struct platform_driver cdns_gpio_driver = {
290 .of_match_table = cdns_of_ids,
292 .probe = cdns_gpio_probe,
293 .remove = cdns_gpio_remove,
295 module_platform_driver(cdns_gpio_driver);
297 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
298 MODULE_DESCRIPTION("Cadence GPIO driver");
299 MODULE_LICENSE("GPL v2");
300 MODULE_ALIAS("platform:cdns-gpio");