1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO driver for NXP LPC18xx/43xx.
10 #include <linux/clk.h>
11 #include <linux/gpio/driver.h>
13 #include <linux/irqdomain.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
21 /* LPC18xx GPIO register offsets */
22 #define LPC18XX_REG_DIR(n) (0x2000 + n * sizeof(u32))
24 #define LPC18XX_MAX_PORTS 8
25 #define LPC18XX_PINS_PER_PORT 32
27 /* LPC18xx GPIO pin interrupt controller register offsets */
28 #define LPC18XX_GPIO_PIN_IC_ISEL 0x00
29 #define LPC18XX_GPIO_PIN_IC_IENR 0x04
30 #define LPC18XX_GPIO_PIN_IC_SIENR 0x08
31 #define LPC18XX_GPIO_PIN_IC_CIENR 0x0c
32 #define LPC18XX_GPIO_PIN_IC_IENF 0x10
33 #define LPC18XX_GPIO_PIN_IC_SIENF 0x14
34 #define LPC18XX_GPIO_PIN_IC_CIENF 0x18
35 #define LPC18XX_GPIO_PIN_IC_RISE 0x1c
36 #define LPC18XX_GPIO_PIN_IC_FALL 0x20
37 #define LPC18XX_GPIO_PIN_IC_IST 0x24
39 #define NR_LPC18XX_GPIO_PIN_IC_IRQS 8
41 struct lpc18xx_gpio_pin_ic {
43 struct irq_domain *domain;
44 struct raw_spinlock lock;
47 struct lpc18xx_gpio_chip {
48 struct gpio_chip gpio;
50 struct lpc18xx_gpio_pin_ic *pin_ic;
54 static inline void lpc18xx_gpio_pin_ic_isel(struct lpc18xx_gpio_pin_ic *ic,
57 u32 val = readl_relaxed(ic->base + LPC18XX_GPIO_PIN_IC_ISEL);
64 writel_relaxed(val, ic->base + LPC18XX_GPIO_PIN_IC_ISEL);
67 static inline void lpc18xx_gpio_pin_ic_set(struct lpc18xx_gpio_pin_ic *ic,
70 writel_relaxed(BIT(pin), ic->base + reg);
73 static void lpc18xx_gpio_pin_ic_mask(struct irq_data *d)
75 struct lpc18xx_gpio_pin_ic *ic = d->chip_data;
76 u32 type = irqd_get_trigger_type(d);
78 raw_spin_lock(&ic->lock);
80 if (type & IRQ_TYPE_LEVEL_MASK || type & IRQ_TYPE_EDGE_RISING)
81 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
82 LPC18XX_GPIO_PIN_IC_CIENR);
84 if (type & IRQ_TYPE_EDGE_FALLING)
85 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
86 LPC18XX_GPIO_PIN_IC_CIENF);
88 raw_spin_unlock(&ic->lock);
90 irq_chip_mask_parent(d);
93 static void lpc18xx_gpio_pin_ic_unmask(struct irq_data *d)
95 struct lpc18xx_gpio_pin_ic *ic = d->chip_data;
96 u32 type = irqd_get_trigger_type(d);
98 raw_spin_lock(&ic->lock);
100 if (type & IRQ_TYPE_LEVEL_MASK || type & IRQ_TYPE_EDGE_RISING)
101 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
102 LPC18XX_GPIO_PIN_IC_SIENR);
104 if (type & IRQ_TYPE_EDGE_FALLING)
105 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
106 LPC18XX_GPIO_PIN_IC_SIENF);
108 raw_spin_unlock(&ic->lock);
110 irq_chip_unmask_parent(d);
113 static void lpc18xx_gpio_pin_ic_eoi(struct irq_data *d)
115 struct lpc18xx_gpio_pin_ic *ic = d->chip_data;
116 u32 type = irqd_get_trigger_type(d);
118 raw_spin_lock(&ic->lock);
120 if (type & IRQ_TYPE_EDGE_BOTH)
121 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
122 LPC18XX_GPIO_PIN_IC_IST);
124 raw_spin_unlock(&ic->lock);
126 irq_chip_eoi_parent(d);
129 static int lpc18xx_gpio_pin_ic_set_type(struct irq_data *d, unsigned int type)
131 struct lpc18xx_gpio_pin_ic *ic = d->chip_data;
133 raw_spin_lock(&ic->lock);
135 if (type & IRQ_TYPE_LEVEL_HIGH) {
136 lpc18xx_gpio_pin_ic_isel(ic, d->hwirq, true);
137 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
138 LPC18XX_GPIO_PIN_IC_SIENF);
139 } else if (type & IRQ_TYPE_LEVEL_LOW) {
140 lpc18xx_gpio_pin_ic_isel(ic, d->hwirq, true);
141 lpc18xx_gpio_pin_ic_set(ic, d->hwirq,
142 LPC18XX_GPIO_PIN_IC_CIENF);
144 lpc18xx_gpio_pin_ic_isel(ic, d->hwirq, false);
147 raw_spin_unlock(&ic->lock);
152 static struct irq_chip lpc18xx_gpio_pin_ic = {
153 .name = "LPC18xx GPIO pin",
154 .irq_mask = lpc18xx_gpio_pin_ic_mask,
155 .irq_unmask = lpc18xx_gpio_pin_ic_unmask,
156 .irq_eoi = lpc18xx_gpio_pin_ic_eoi,
157 .irq_set_type = lpc18xx_gpio_pin_ic_set_type,
158 .flags = IRQCHIP_SET_TYPE_MASKED,
161 static int lpc18xx_gpio_pin_ic_domain_alloc(struct irq_domain *domain,
163 unsigned int nr_irqs, void *data)
165 struct irq_fwspec parent_fwspec, *fwspec = data;
166 struct lpc18xx_gpio_pin_ic *ic = domain->host_data;
167 irq_hw_number_t hwirq;
173 hwirq = fwspec->param[0];
174 if (hwirq >= NR_LPC18XX_GPIO_PIN_IC_IRQS)
178 * All LPC18xx/LPC43xx GPIO pin hardware interrupts are translated
179 * into edge interrupts 32...39 on parent Cortex-M3/M4 NVIC
181 parent_fwspec.fwnode = domain->parent->fwnode;
182 parent_fwspec.param_count = 1;
183 parent_fwspec.param[0] = hwirq + 32;
185 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
187 pr_err("failed to allocate parent irq %u: %d\n",
188 parent_fwspec.param[0], ret);
192 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
193 &lpc18xx_gpio_pin_ic, ic);
196 static const struct irq_domain_ops lpc18xx_gpio_pin_ic_domain_ops = {
197 .alloc = lpc18xx_gpio_pin_ic_domain_alloc,
198 .xlate = irq_domain_xlate_twocell,
199 .free = irq_domain_free_irqs_common,
202 static int lpc18xx_gpio_pin_ic_probe(struct lpc18xx_gpio_chip *gc)
204 struct device *dev = gc->gpio.parent;
205 struct irq_domain *parent_domain;
206 struct device_node *parent_node;
207 struct lpc18xx_gpio_pin_ic *ic;
211 parent_node = of_irq_find_parent(dev->of_node);
215 parent_domain = irq_find_host(parent_node);
216 of_node_put(parent_node);
220 ic = devm_kzalloc(dev, sizeof(*ic), GFP_KERNEL);
224 index = of_property_match_string(dev->of_node, "reg-names",
231 ret = of_address_to_resource(dev->of_node, index, &res);
235 ic->base = devm_ioremap_resource(dev, &res);
236 if (IS_ERR(ic->base)) {
237 ret = PTR_ERR(ic->base);
241 raw_spin_lock_init(&ic->lock);
243 ic->domain = irq_domain_add_hierarchy(parent_domain, 0,
244 NR_LPC18XX_GPIO_PIN_IC_IRQS,
246 &lpc18xx_gpio_pin_ic_domain_ops,
249 pr_err("unable to add irq domain\n");
259 devm_iounmap(dev, ic->base);
266 static void lpc18xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
268 struct lpc18xx_gpio_chip *gc = gpiochip_get_data(chip);
269 writeb(value ? 1 : 0, gc->base + offset);
272 static int lpc18xx_gpio_get(struct gpio_chip *chip, unsigned offset)
274 struct lpc18xx_gpio_chip *gc = gpiochip_get_data(chip);
275 return !!readb(gc->base + offset);
278 static int lpc18xx_gpio_direction(struct gpio_chip *chip, unsigned offset,
281 struct lpc18xx_gpio_chip *gc = gpiochip_get_data(chip);
285 port = offset / LPC18XX_PINS_PER_PORT;
286 pin = offset % LPC18XX_PINS_PER_PORT;
288 spin_lock_irqsave(&gc->lock, flags);
289 dir = readl(gc->base + LPC18XX_REG_DIR(port));
294 writel(dir, gc->base + LPC18XX_REG_DIR(port));
295 spin_unlock_irqrestore(&gc->lock, flags);
300 static int lpc18xx_gpio_direction_input(struct gpio_chip *chip,
303 return lpc18xx_gpio_direction(chip, offset, false);
306 static int lpc18xx_gpio_direction_output(struct gpio_chip *chip,
307 unsigned offset, int value)
309 lpc18xx_gpio_set(chip, offset, value);
310 return lpc18xx_gpio_direction(chip, offset, true);
313 static const struct gpio_chip lpc18xx_chip = {
314 .label = "lpc18xx/43xx-gpio",
315 .request = gpiochip_generic_request,
316 .free = gpiochip_generic_free,
317 .direction_input = lpc18xx_gpio_direction_input,
318 .direction_output = lpc18xx_gpio_direction_output,
319 .set = lpc18xx_gpio_set,
320 .get = lpc18xx_gpio_get,
321 .ngpio = LPC18XX_MAX_PORTS * LPC18XX_PINS_PER_PORT,
322 .owner = THIS_MODULE,
325 static int lpc18xx_gpio_probe(struct platform_device *pdev)
327 struct device *dev = &pdev->dev;
328 struct lpc18xx_gpio_chip *gc;
332 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
336 gc->gpio = lpc18xx_chip;
337 platform_set_drvdata(pdev, gc);
339 index = of_property_match_string(dev->of_node, "reg-names", "gpio");
341 /* To support backward compatibility take the first resource */
342 gc->base = devm_platform_ioremap_resource(pdev, 0);
346 ret = of_address_to_resource(dev->of_node, index, &res);
350 gc->base = devm_ioremap_resource(dev, &res);
352 if (IS_ERR(gc->base))
353 return PTR_ERR(gc->base);
355 clk = devm_clk_get_enabled(dev, NULL);
357 dev_err(dev, "input clock not found\n");
361 spin_lock_init(&gc->lock);
363 gc->gpio.parent = dev;
365 ret = devm_gpiochip_add_data(dev, &gc->gpio, gc);
367 return dev_err_probe(dev, ret, "failed to add gpio chip\n");
369 /* On error GPIO pin interrupt controller just won't be registered */
370 lpc18xx_gpio_pin_ic_probe(gc);
375 static void lpc18xx_gpio_remove(struct platform_device *pdev)
377 struct lpc18xx_gpio_chip *gc = platform_get_drvdata(pdev);
380 irq_domain_remove(gc->pin_ic->domain);
383 static const struct of_device_id lpc18xx_gpio_match[] = {
384 { .compatible = "nxp,lpc1850-gpio" },
387 MODULE_DEVICE_TABLE(of, lpc18xx_gpio_match);
389 static struct platform_driver lpc18xx_gpio_driver = {
390 .probe = lpc18xx_gpio_probe,
391 .remove = lpc18xx_gpio_remove,
393 .name = "lpc18xx-gpio",
394 .of_match_table = lpc18xx_gpio_match,
397 module_platform_driver(lpc18xx_gpio_driver);
401 MODULE_DESCRIPTION("GPIO driver for LPC18xx/43xx");
402 MODULE_LICENSE("GPL v2");