blob: f1a5ea9b3de2a49bc6a107e0032b8e38d8a93b4b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Gabor Juhos6eae43c2011-01-04 21:28:15 +01002/*
3 * Atheros AR71XX/AR724X/AR913X GPIO API support
4 *
Alban Bedel28be55d2016-01-28 20:44:33 +01005 * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
Gabor Juhos5b5b5442012-03-14 10:45:23 +01006 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
7 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
Gabor Juhos6eae43c2011-01-04 21:28:15 +01008 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
Gabor Juhos6eae43c2011-01-04 21:28:15 +01009 */
10
Alban Bedel49a5bd82015-09-01 11:38:02 +020011#include <linux/gpio/driver.h>
Alban Bedel2ddf3a72015-05-31 02:18:24 +020012#include <linux/platform_data/gpio-ath79.h>
13#include <linux/of_device.h>
Alban Bedel2b8f89e2016-01-28 20:44:32 +010014#include <linux/interrupt.h>
Paul Gortmaker2034b9d2016-09-12 18:16:28 -040015#include <linux/module.h>
Alban Bedel2b8f89e2016-01-28 20:44:32 +010016#include <linux/irq.h>
Gabor Juhos6eae43c2011-01-04 21:28:15 +010017
Alban Bedel409d8782016-01-28 20:44:30 +010018#define AR71XX_GPIO_REG_OE 0x00
19#define AR71XX_GPIO_REG_IN 0x04
20#define AR71XX_GPIO_REG_SET 0x0c
21#define AR71XX_GPIO_REG_CLEAR 0x10
Gabor Juhos6eae43c2011-01-04 21:28:15 +010022
Alban Bedel2b8f89e2016-01-28 20:44:32 +010023#define AR71XX_GPIO_REG_INT_ENABLE 0x14
24#define AR71XX_GPIO_REG_INT_TYPE 0x18
25#define AR71XX_GPIO_REG_INT_POLARITY 0x1c
26#define AR71XX_GPIO_REG_INT_PENDING 0x20
27#define AR71XX_GPIO_REG_INT_MASK 0x24
28
Alban Bedel49a5bd82015-09-01 11:38:02 +020029struct ath79_gpio_ctrl {
Alban Bedelab327702016-01-28 20:44:29 +010030 struct gpio_chip gc;
Alban Bedel49a5bd82015-09-01 11:38:02 +020031 void __iomem *base;
Julia Cartwrighta080ce52017-03-09 10:21:53 -060032 raw_spinlock_t lock;
Alban Bedel2b8f89e2016-01-28 20:44:32 +010033 unsigned long both_edges;
Alban Bedel49a5bd82015-09-01 11:38:02 +020034};
Gabor Juhos6eae43c2011-01-04 21:28:15 +010035
Alban Bedel2b8f89e2016-01-28 20:44:32 +010036static struct ath79_gpio_ctrl *irq_data_to_ath79_gpio(struct irq_data *data)
37{
38 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
39
40 return container_of(gc, struct ath79_gpio_ctrl, gc);
41}
42
43static u32 ath79_gpio_read(struct ath79_gpio_ctrl *ctrl, unsigned reg)
44{
45 return readl(ctrl->base + reg);
46}
47
48static void ath79_gpio_write(struct ath79_gpio_ctrl *ctrl,
49 unsigned reg, u32 val)
50{
zhong jiang23211b02018-07-24 19:57:43 +080051 writel(val, ctrl->base + reg);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010052}
53
54static bool ath79_gpio_update_bits(
55 struct ath79_gpio_ctrl *ctrl, unsigned reg, u32 mask, u32 bits)
56{
57 u32 old_val, new_val;
58
59 old_val = ath79_gpio_read(ctrl, reg);
60 new_val = (old_val & ~mask) | (bits & mask);
61
62 if (new_val != old_val)
63 ath79_gpio_write(ctrl, reg, new_val);
64
65 return new_val != old_val;
66}
67
68static void ath79_gpio_irq_unmask(struct irq_data *data)
69{
70 struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
71 u32 mask = BIT(irqd_to_hwirq(data));
72 unsigned long flags;
73
Julia Cartwrighta080ce52017-03-09 10:21:53 -060074 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010075 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
Julia Cartwrighta080ce52017-03-09 10:21:53 -060076 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010077}
78
79static void ath79_gpio_irq_mask(struct irq_data *data)
80{
81 struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
82 u32 mask = BIT(irqd_to_hwirq(data));
83 unsigned long flags;
84
Julia Cartwrighta080ce52017-03-09 10:21:53 -060085 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010086 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
Julia Cartwrighta080ce52017-03-09 10:21:53 -060087 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010088}
89
90static void ath79_gpio_irq_enable(struct irq_data *data)
91{
92 struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
93 u32 mask = BIT(irqd_to_hwirq(data));
94 unsigned long flags;
95
Julia Cartwrighta080ce52017-03-09 10:21:53 -060096 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +010097 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
98 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
Julia Cartwrighta080ce52017-03-09 10:21:53 -060099 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100100}
101
102static void ath79_gpio_irq_disable(struct irq_data *data)
103{
104 struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
105 u32 mask = BIT(irqd_to_hwirq(data));
106 unsigned long flags;
107
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600108 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100109 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
110 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600111 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100112}
113
114static int ath79_gpio_irq_set_type(struct irq_data *data,
115 unsigned int flow_type)
116{
117 struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
118 u32 mask = BIT(irqd_to_hwirq(data));
119 u32 type = 0, polarity = 0;
120 unsigned long flags;
121 bool disabled;
122
123 switch (flow_type) {
124 case IRQ_TYPE_EDGE_RISING:
125 polarity |= mask;
126 case IRQ_TYPE_EDGE_FALLING:
127 case IRQ_TYPE_EDGE_BOTH:
128 break;
129
130 case IRQ_TYPE_LEVEL_HIGH:
131 polarity |= mask;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500132 /* fall through */
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100133 case IRQ_TYPE_LEVEL_LOW:
134 type |= mask;
135 break;
136
137 default:
138 return -EINVAL;
139 }
140
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600141 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100142
143 if (flow_type == IRQ_TYPE_EDGE_BOTH) {
144 ctrl->both_edges |= mask;
145 polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
146 } else {
147 ctrl->both_edges &= ~mask;
148 }
149
150 /* As the IRQ configuration can't be loaded atomically we
151 * have to disable the interrupt while the configuration state
152 * is invalid.
153 */
154 disabled = ath79_gpio_update_bits(
155 ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
156
157 ath79_gpio_update_bits(
158 ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type);
159 ath79_gpio_update_bits(
160 ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity);
161
162 if (disabled)
163 ath79_gpio_update_bits(
164 ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
165
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600166 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100167
168 return 0;
169}
170
171static struct irq_chip ath79_gpio_irqchip = {
172 .name = "gpio-ath79",
173 .irq_enable = ath79_gpio_irq_enable,
174 .irq_disable = ath79_gpio_irq_disable,
175 .irq_mask = ath79_gpio_irq_mask,
176 .irq_unmask = ath79_gpio_irq_unmask,
177 .irq_set_type = ath79_gpio_irq_set_type,
178};
179
180static void ath79_gpio_irq_handler(struct irq_desc *desc)
181{
182 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
183 struct irq_chip *irqchip = irq_desc_get_chip(desc);
184 struct ath79_gpio_ctrl *ctrl =
185 container_of(gc, struct ath79_gpio_ctrl, gc);
186 unsigned long flags, pending;
187 u32 both_edges, state;
188 int irq;
189
190 chained_irq_enter(irqchip, desc);
191
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600192 raw_spin_lock_irqsave(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100193
194 pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING);
195
196 /* Update the polarity of the both edges irqs */
197 both_edges = ctrl->both_edges & pending;
198 if (both_edges) {
199 state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
200 ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY,
201 both_edges, ~state);
202 }
203
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600204 raw_spin_unlock_irqrestore(&ctrl->lock, flags);
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100205
206 if (pending) {
207 for_each_set_bit(irq, &pending, gc->ngpio)
208 generic_handle_irq(
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100209 irq_linear_revmap(gc->irq.domain, irq));
Alban Bedel2b8f89e2016-01-28 20:44:32 +0100210 }
211
212 chained_irq_exit(irqchip, desc);
213}
214
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200215static const struct of_device_id ath79_gpio_of_match[] = {
216 { .compatible = "qca,ar7100-gpio" },
217 { .compatible = "qca,ar9340-gpio" },
218 {},
219};
Javier Martinez Canillas6d8d2712016-10-18 17:44:01 -0300220MODULE_DEVICE_TABLE(of, ath79_gpio_of_match);
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200221
222static int ath79_gpio_probe(struct platform_device *pdev)
Gabor Juhos6eae43c2011-01-04 21:28:15 +0100223{
Nizam Haiderab128af2015-11-23 20:53:18 +0530224 struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Linus Walleijaee5cec2019-06-25 13:57:33 +0200225 struct device *dev = &pdev->dev;
226 struct device_node *np = dev->of_node;
Alban Bedel49a5bd82015-09-01 11:38:02 +0200227 struct ath79_gpio_ctrl *ctrl;
Linus Walleijaee5cec2019-06-25 13:57:33 +0200228 struct gpio_irq_chip *girq;
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200229 struct resource *res;
Alban Bedel49a5bd82015-09-01 11:38:02 +0200230 u32 ath79_gpio_count;
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200231 bool oe_inverted;
Gabor Juhos6eae43c2011-01-04 21:28:15 +0100232 int err;
233
Linus Walleijaee5cec2019-06-25 13:57:33 +0200234 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
Alban Bedel49a5bd82015-09-01 11:38:02 +0200235 if (!ctrl)
236 return -ENOMEM;
Alban Bedel2f890cf2016-01-28 20:44:31 +0100237 platform_set_drvdata(pdev, ctrl);
Alban Bedel49a5bd82015-09-01 11:38:02 +0200238
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200239 if (np) {
240 err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
241 if (err) {
Linus Walleijaee5cec2019-06-25 13:57:33 +0200242 dev_err(dev, "ngpios property is not valid\n");
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200243 return err;
244 }
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200245 oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
246 } else if (pdata) {
247 ath79_gpio_count = pdata->ngpios;
248 oe_inverted = pdata->oe_inverted;
249 } else {
Linus Walleijaee5cec2019-06-25 13:57:33 +0200250 dev_err(dev, "No DT node or platform data found\n");
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200251 return -EINVAL;
252 }
Gabor Juhos6eae43c2011-01-04 21:28:15 +0100253
Axel Linf0d3c722016-02-20 09:48:07 +0800254 if (ath79_gpio_count >= 32) {
Linus Walleijaee5cec2019-06-25 13:57:33 +0200255 dev_err(dev, "ngpios must be less than 32\n");
Axel Linf0d3c722016-02-20 09:48:07 +0800256 return -EINVAL;
257 }
258
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200259 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Wei Yongjunf79b55d2018-03-25 00:40:48 +0100260 if (!res)
261 return -EINVAL;
Linus Walleijaee5cec2019-06-25 13:57:33 +0200262 ctrl->base = devm_ioremap_nocache(dev, res->start, resource_size(res));
Alban Bedel49a5bd82015-09-01 11:38:02 +0200263 if (!ctrl->base)
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200264 return -ENOMEM;
265
Julia Cartwrighta080ce52017-03-09 10:21:53 -0600266 raw_spin_lock_init(&ctrl->lock);
Linus Walleijaee5cec2019-06-25 13:57:33 +0200267 err = bgpio_init(&ctrl->gc, dev, 4,
Alban Bedelab327702016-01-28 20:44:29 +0100268 ctrl->base + AR71XX_GPIO_REG_IN,
269 ctrl->base + AR71XX_GPIO_REG_SET,
270 ctrl->base + AR71XX_GPIO_REG_CLEAR,
271 oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE,
272 oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL,
273 0);
274 if (err) {
Linus Walleijaee5cec2019-06-25 13:57:33 +0200275 dev_err(dev, "bgpio_init failed\n");
Alban Bedelab327702016-01-28 20:44:29 +0100276 return err;
Gabor Juhos5b5b5442012-03-14 10:45:23 +0100277 }
Alban Bedelab327702016-01-28 20:44:29 +0100278 /* Use base 0 to stay compatible with legacy platforms */
279 ctrl->gc.base = 0;
Gabor Juhos6eae43c2011-01-04 21:28:15 +0100280
Linus Walleijaee5cec2019-06-25 13:57:33 +0200281 /* Optional interrupt setup */
282 if (!np || of_property_read_bool(np, "interrupt-controller")) {
283 girq = &ctrl->gc.irq;
284 girq->chip = &ath79_gpio_irqchip;
285 girq->parent_handler = ath79_gpio_irq_handler;
286 girq->num_parents = 1;
287 girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
288 GFP_KERNEL);
289 if (!girq->parents)
290 return -ENOMEM;
291 girq->parents[0] = platform_get_irq(pdev, 0);
292 girq->default_type = IRQ_TYPE_NONE;
293 girq->handler = handle_simple_irq;
294 }
295
296 err = devm_gpiochip_add_data(dev, &ctrl->gc, ctrl);
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200297 if (err) {
Linus Walleijaee5cec2019-06-25 13:57:33 +0200298 dev_err(dev,
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200299 "cannot add AR71xx GPIO chip, error=%d", err);
300 return err;
301 }
Alban Bedel2f890cf2016-01-28 20:44:31 +0100302 return 0;
303}
304
Alban Bedel2ddf3a72015-05-31 02:18:24 +0200305static struct platform_driver ath79_gpio_driver = {
306 .driver = {
307 .name = "ath79-gpio",
308 .of_match_table = ath79_gpio_of_match,
309 },
310 .probe = ath79_gpio_probe,
311};
312
313module_platform_driver(ath79_gpio_driver);
Jesse Chan539340f2017-11-20 12:54:26 -0800314
315MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
316MODULE_LICENSE("GPL v2");