blob: e80634c464a9852b22e459c30e436c954df0d953 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Walleij49cec4d2017-01-22 13:18:44 +01002/*
Linus Walleij9d3a15a2017-03-13 00:28:16 +01003 * Faraday Technolog FTGPIO010 gpiochip and interrupt routines
Linus Walleij49cec4d2017-01-22 13:18:44 +01004 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5 *
6 * Based on arch/arm/mach-gemini/gpio.c:
7 * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
8 *
9 * Based on plat-mxc/gpio.c:
10 * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
11 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
12 */
13#include <linux/gpio/driver.h>
14#include <linux/io.h>
15#include <linux/interrupt.h>
16#include <linux/platform_device.h>
17#include <linux/of_gpio.h>
18#include <linux/bitops.h>
19
20/* GPIO registers definition */
21#define GPIO_DATA_OUT 0x00
22#define GPIO_DATA_IN 0x04
23#define GPIO_DIR 0x08
Linus Walleij69a87f22018-02-12 22:40:23 +010024#define GPIO_BYPASS_IN 0x0C
Linus Walleij49cec4d2017-01-22 13:18:44 +010025#define GPIO_DATA_SET 0x10
26#define GPIO_DATA_CLR 0x14
27#define GPIO_PULL_EN 0x18
28#define GPIO_PULL_TYPE 0x1C
29#define GPIO_INT_EN 0x20
Linus Walleij69a87f22018-02-12 22:40:23 +010030#define GPIO_INT_STAT_RAW 0x24
31#define GPIO_INT_STAT_MASKED 0x28
Linus Walleij49cec4d2017-01-22 13:18:44 +010032#define GPIO_INT_MASK 0x2C
33#define GPIO_INT_CLR 0x30
34#define GPIO_INT_TYPE 0x34
35#define GPIO_INT_BOTH_EDGE 0x38
36#define GPIO_INT_LEVEL 0x3C
37#define GPIO_DEBOUNCE_EN 0x40
38#define GPIO_DEBOUNCE_PRESCALE 0x44
39
40/**
Linus Walleij9d3a15a2017-03-13 00:28:16 +010041 * struct ftgpio_gpio - Gemini GPIO state container
Linus Walleij49cec4d2017-01-22 13:18:44 +010042 * @dev: containing device for this instance
43 * @gc: gpiochip for this instance
44 */
Linus Walleij9d3a15a2017-03-13 00:28:16 +010045struct ftgpio_gpio {
Linus Walleij49cec4d2017-01-22 13:18:44 +010046 struct device *dev;
47 struct gpio_chip gc;
48 void __iomem *base;
49};
50
Linus Walleij9d3a15a2017-03-13 00:28:16 +010051static void ftgpio_gpio_ack_irq(struct irq_data *d)
Linus Walleij49cec4d2017-01-22 13:18:44 +010052{
53 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij9d3a15a2017-03-13 00:28:16 +010054 struct ftgpio_gpio *g = gpiochip_get_data(gc);
Linus Walleij49cec4d2017-01-22 13:18:44 +010055
56 writel(BIT(irqd_to_hwirq(d)), g->base + GPIO_INT_CLR);
57}
58
Linus Walleij9d3a15a2017-03-13 00:28:16 +010059static void ftgpio_gpio_mask_irq(struct irq_data *d)
Linus Walleij49cec4d2017-01-22 13:18:44 +010060{
61 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij9d3a15a2017-03-13 00:28:16 +010062 struct ftgpio_gpio *g = gpiochip_get_data(gc);
Linus Walleij49cec4d2017-01-22 13:18:44 +010063 u32 val;
64
65 val = readl(g->base + GPIO_INT_EN);
66 val &= ~BIT(irqd_to_hwirq(d));
67 writel(val, g->base + GPIO_INT_EN);
68}
69
Linus Walleij9d3a15a2017-03-13 00:28:16 +010070static void ftgpio_gpio_unmask_irq(struct irq_data *d)
Linus Walleij49cec4d2017-01-22 13:18:44 +010071{
72 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij9d3a15a2017-03-13 00:28:16 +010073 struct ftgpio_gpio *g = gpiochip_get_data(gc);
Linus Walleij49cec4d2017-01-22 13:18:44 +010074 u32 val;
75
76 val = readl(g->base + GPIO_INT_EN);
77 val |= BIT(irqd_to_hwirq(d));
78 writel(val, g->base + GPIO_INT_EN);
79}
80
Linus Walleij9d3a15a2017-03-13 00:28:16 +010081static int ftgpio_gpio_set_irq_type(struct irq_data *d, unsigned int type)
Linus Walleij49cec4d2017-01-22 13:18:44 +010082{
83 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij9d3a15a2017-03-13 00:28:16 +010084 struct ftgpio_gpio *g = gpiochip_get_data(gc);
Linus Walleij49cec4d2017-01-22 13:18:44 +010085 u32 mask = BIT(irqd_to_hwirq(d));
86 u32 reg_both, reg_level, reg_type;
87
88 reg_type = readl(g->base + GPIO_INT_TYPE);
89 reg_level = readl(g->base + GPIO_INT_LEVEL);
90 reg_both = readl(g->base + GPIO_INT_BOTH_EDGE);
91
92 switch (type) {
93 case IRQ_TYPE_EDGE_BOTH:
94 irq_set_handler_locked(d, handle_edge_irq);
95 reg_type &= ~mask;
96 reg_both |= mask;
97 break;
98 case IRQ_TYPE_EDGE_RISING:
99 irq_set_handler_locked(d, handle_edge_irq);
100 reg_type &= ~mask;
101 reg_both &= ~mask;
102 reg_level &= ~mask;
103 break;
104 case IRQ_TYPE_EDGE_FALLING:
105 irq_set_handler_locked(d, handle_edge_irq);
106 reg_type &= ~mask;
107 reg_both &= ~mask;
108 reg_level |= mask;
109 break;
110 case IRQ_TYPE_LEVEL_HIGH:
111 irq_set_handler_locked(d, handle_level_irq);
112 reg_type |= mask;
113 reg_level &= ~mask;
114 break;
115 case IRQ_TYPE_LEVEL_LOW:
116 irq_set_handler_locked(d, handle_level_irq);
117 reg_type |= mask;
118 reg_level |= mask;
119 break;
120 default:
121 irq_set_handler_locked(d, handle_bad_irq);
122 return -EINVAL;
123 }
124
125 writel(reg_type, g->base + GPIO_INT_TYPE);
126 writel(reg_level, g->base + GPIO_INT_LEVEL);
127 writel(reg_both, g->base + GPIO_INT_BOTH_EDGE);
128
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100129 ftgpio_gpio_ack_irq(d);
Linus Walleij49cec4d2017-01-22 13:18:44 +0100130
131 return 0;
132}
133
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100134static struct irq_chip ftgpio_gpio_irqchip = {
135 .name = "FTGPIO010",
136 .irq_ack = ftgpio_gpio_ack_irq,
137 .irq_mask = ftgpio_gpio_mask_irq,
138 .irq_unmask = ftgpio_gpio_unmask_irq,
139 .irq_set_type = ftgpio_gpio_set_irq_type,
Linus Walleij49cec4d2017-01-22 13:18:44 +0100140};
141
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100142static void ftgpio_gpio_irq_handler(struct irq_desc *desc)
Linus Walleij49cec4d2017-01-22 13:18:44 +0100143{
144 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100145 struct ftgpio_gpio *g = gpiochip_get_data(gc);
Linus Walleij49cec4d2017-01-22 13:18:44 +0100146 struct irq_chip *irqchip = irq_desc_get_chip(desc);
147 int offset;
148 unsigned long stat;
149
150 chained_irq_enter(irqchip, desc);
151
Linus Walleij69a87f22018-02-12 22:40:23 +0100152 stat = readl(g->base + GPIO_INT_STAT_RAW);
Linus Walleij49cec4d2017-01-22 13:18:44 +0100153 if (stat)
154 for_each_set_bit(offset, &stat, gc->ngpio)
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100155 generic_handle_irq(irq_find_mapping(gc->irq.domain,
Linus Walleij49cec4d2017-01-22 13:18:44 +0100156 offset));
157
158 chained_irq_exit(irqchip, desc);
159}
160
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100161static int ftgpio_gpio_probe(struct platform_device *pdev)
Linus Walleij49cec4d2017-01-22 13:18:44 +0100162{
163 struct device *dev = &pdev->dev;
164 struct resource *res;
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100165 struct ftgpio_gpio *g;
Linus Walleij49cec4d2017-01-22 13:18:44 +0100166 int irq;
167 int ret;
168
169 g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
170 if (!g)
171 return -ENOMEM;
172
173 g->dev = dev;
174
175 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176 g->base = devm_ioremap_resource(dev, res);
177 if (IS_ERR(g->base))
178 return PTR_ERR(g->base);
179
180 irq = platform_get_irq(pdev, 0);
Arvind Yadav4070a532017-12-02 22:31:01 +0530181 if (irq <= 0)
182 return irq ? irq : -EINVAL;
Linus Walleij49cec4d2017-01-22 13:18:44 +0100183
184 ret = bgpio_init(&g->gc, dev, 4,
185 g->base + GPIO_DATA_IN,
186 g->base + GPIO_DATA_SET,
187 g->base + GPIO_DATA_CLR,
188 g->base + GPIO_DIR,
189 NULL,
190 0);
191 if (ret) {
192 dev_err(dev, "unable to init generic GPIO\n");
193 return ret;
194 }
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100195 g->gc.label = "FTGPIO010";
Linus Walleij49cec4d2017-01-22 13:18:44 +0100196 g->gc.base = -1;
197 g->gc.parent = dev;
198 g->gc.owner = THIS_MODULE;
199 /* ngpio is set by bgpio_init() */
200
201 ret = devm_gpiochip_add_data(dev, &g->gc, g);
202 if (ret)
203 return ret;
204
205 /* Disable, unmask and clear all interrupts */
206 writel(0x0, g->base + GPIO_INT_EN);
207 writel(0x0, g->base + GPIO_INT_MASK);
208 writel(~0x0, g->base + GPIO_INT_CLR);
209
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100210 ret = gpiochip_irqchip_add(&g->gc, &ftgpio_gpio_irqchip,
Linus Walleij49cec4d2017-01-22 13:18:44 +0100211 0, handle_bad_irq,
212 IRQ_TYPE_NONE);
213 if (ret) {
214 dev_info(dev, "could not add irqchip\n");
215 return ret;
216 }
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100217 gpiochip_set_chained_irqchip(&g->gc, &ftgpio_gpio_irqchip,
218 irq, ftgpio_gpio_irq_handler);
Linus Walleij49cec4d2017-01-22 13:18:44 +0100219
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100220 dev_info(dev, "FTGPIO010 @%p registered\n", g->base);
Linus Walleij49cec4d2017-01-22 13:18:44 +0100221
222 return 0;
223}
224
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100225static const struct of_device_id ftgpio_gpio_of_match[] = {
Linus Walleij49cec4d2017-01-22 13:18:44 +0100226 {
227 .compatible = "cortina,gemini-gpio",
228 },
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100229 {
230 .compatible = "moxa,moxart-gpio",
231 },
232 {
233 .compatible = "faraday,ftgpio010",
234 },
Linus Walleij49cec4d2017-01-22 13:18:44 +0100235 {},
236};
237
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100238static struct platform_driver ftgpio_gpio_driver = {
Linus Walleij49cec4d2017-01-22 13:18:44 +0100239 .driver = {
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100240 .name = "ftgpio010-gpio",
241 .of_match_table = of_match_ptr(ftgpio_gpio_of_match),
Linus Walleij49cec4d2017-01-22 13:18:44 +0100242 },
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100243 .probe = ftgpio_gpio_probe,
Linus Walleij49cec4d2017-01-22 13:18:44 +0100244};
Linus Walleij9d3a15a2017-03-13 00:28:16 +0100245builtin_platform_driver(ftgpio_gpio_driver);