blob: fed594a3051776914a5edf06d1a988379cf0455a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/arch/arm/plat-pxa/gpio.c
4 *
5 * Generic PXA GPIO handling
6 *
7 * Author: Nicolas Pitre
8 * Created: Jun 15, 2001
9 * Copyright: MontaVista Software Inc.
10 */
11#include <linux/module.h>
12#include <linux/clk.h>
13#include <linux/err.h>
14#include <linux/gpio/driver.h>
15#include <linux/gpio-pxa.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/irqdomain.h>
20#include <linux/irqchip/chained_irq.h>
21#include <linux/io.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/pinctrl/consumer.h>
25#include <linux/platform_device.h>
26#include <linux/syscore_ops.h>
27#include <linux/slab.h>
28#include <soc/asr/wakeup_defines.h>
29
30/*
31 * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
32 * one set of registers. The register offsets are organized below:
33 *
34 * GPLR GPDR GPSR GPCR GRER GFER GEDR
35 * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
36 * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
37 * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
38 *
39 * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
40 * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
41 * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
42 *
43 * BANK 6 - 0x0200 0x020C 0x0218 0x0224 0x0230 0x023C 0x0248
44 *
45 * NOTE:
46 * BANK 3 is only available on PXA27x and later processors.
47 * BANK 4 and 5 are only available on PXA935, PXA1928
48 * BANK 6 is only available on PXA1928
49 */
50
51#define GPLR_OFFSET 0x00
52#define GPDR_OFFSET 0x0C
53#define GPSR_OFFSET 0x18
54#define GPCR_OFFSET 0x24
55#define GRER_OFFSET 0x30
56#define GFER_OFFSET 0x3C
57#define GEDR_OFFSET 0x48
58#define GAFR_OFFSET 0x54
59#define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
60
61#define BANK_OFF(n) (((n) / 3) << 8) + (((n) % 3) << 2)
62
63int pxa_last_gpio;
64static int irq_base;
65
66struct pxa_gpio_bank {
67 void __iomem *regbase;
68 unsigned long irq_mask;
69 unsigned long irq_edge_rise;
70 unsigned long irq_edge_fall;
71
72#ifdef CONFIG_PM
73 unsigned long saved_gplr;
74 unsigned long saved_gpdr;
75 unsigned long saved_grer;
76 unsigned long saved_gfer;
77#endif
78 unsigned long original_grer;
79 unsigned long original_gfer;
80 unsigned long gpio_edge_disabled;
81};
82
83struct pxa_gpio_chip {
84 struct device *dev;
85 struct gpio_chip chip;
86 struct pxa_gpio_bank *banks;
87 struct irq_domain *irqdomain;
88
89 int irq0;
90 int irq1;
91 int (*set_wake)(unsigned int gpio, unsigned int on);
92};
93
94enum pxa_gpio_type {
95 PXA25X_GPIO = 0,
96 PXA26X_GPIO,
97 PXA27X_GPIO,
98 PXA3XX_GPIO,
99 PXA93X_GPIO,
100 MMP_GPIO = 0x10,
101 MMP2_GPIO,
102 PXA1928_GPIO,
103};
104
105struct pxa_gpio_id {
106 enum pxa_gpio_type type;
107 int gpio_nums;
108};
109
110static DEFINE_SPINLOCK(gpio_lock);
111static struct pxa_gpio_chip *pxa_gpio_chip;
112static enum pxa_gpio_type gpio_type;
113
114static struct pxa_gpio_id pxa25x_id = {
115 .type = PXA25X_GPIO,
116 .gpio_nums = 85,
117};
118
119static struct pxa_gpio_id pxa26x_id = {
120 .type = PXA26X_GPIO,
121 .gpio_nums = 90,
122};
123
124static struct pxa_gpio_id pxa27x_id = {
125 .type = PXA27X_GPIO,
126 .gpio_nums = 121,
127};
128
129static struct pxa_gpio_id pxa3xx_id = {
130 .type = PXA3XX_GPIO,
131 .gpio_nums = 128,
132};
133
134static struct pxa_gpio_id pxa93x_id = {
135 .type = PXA93X_GPIO,
136 .gpio_nums = 192,
137};
138
139static struct pxa_gpio_id mmp_id = {
140 .type = MMP_GPIO,
141 .gpio_nums = 128,
142};
143
144static struct pxa_gpio_id mmp2_id = {
145 .type = MMP2_GPIO,
146 .gpio_nums = 192,
147};
148
149static struct pxa_gpio_id pxa1928_id = {
150 .type = PXA1928_GPIO,
151 .gpio_nums = 224,
152};
153
154static u32 sys_gpio_int_wakeup_id[MAX_GPIO_INT_WAKEUP_EVENT];
155static u16 gpio_int_wakeup_idx;
156
157#define for_each_gpio_bank(i, b, pc) \
158 for (i = 0, b = pc->banks; i <= pxa_last_gpio; i += 32, b++)
159
160
161static void asr_save_gpio_int_wakeup_event(u32 event)
162{
163 if (gpio_int_wakeup_idx < MAX_GPIO_INT_WAKEUP_EVENT)
164 sys_gpio_int_wakeup_id[gpio_int_wakeup_idx++] = event;
165}
166int asr_get_gpio_int_wakeup_count(void)
167{
168 return gpio_int_wakeup_idx;
169}
170u32 asr_get_gpio_int_wakeup_event(int idx)
171{
172 if (idx < gpio_int_wakeup_idx) {
173 return sys_gpio_int_wakeup_id[idx];
174 } else {
175 pr_err("%s: error gpio wakeup idx %d\n", __func__, idx);
176 return 0;
177 }
178}
179
180static inline struct pxa_gpio_chip *chip_to_pxachip(struct gpio_chip *c)
181{
182 struct pxa_gpio_chip *pxa_chip = gpiochip_get_data(c);
183
184 return pxa_chip;
185}
186
187static inline void __iomem *gpio_bank_base(struct gpio_chip *c, int gpio)
188{
189 struct pxa_gpio_chip *p = gpiochip_get_data(c);
190 struct pxa_gpio_bank *bank = p->banks + (gpio / 32);
191
192 return bank->regbase;
193}
194
195static inline struct pxa_gpio_bank *gpio_to_pxabank(struct gpio_chip *c,
196 unsigned gpio)
197{
198 return chip_to_pxachip(c)->banks + gpio / 32;
199}
200
201static inline int gpio_is_pxa_type(int type)
202{
203 return (type & MMP_GPIO) == 0;
204}
205
206static inline int gpio_is_mmp_type(int type)
207{
208 return (type & MMP_GPIO) != 0;
209}
210
211/* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
212 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
213 */
214static inline int __gpio_is_inverted(int gpio)
215{
216 if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
217 return 1;
218 return 0;
219}
220
221/*
222 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
223 * function of a GPIO, and GPDRx cannot be altered once configured. It
224 * is attributed as "occupied" here (I know this terminology isn't
225 * accurate, you are welcome to propose a better one :-)
226 */
227static inline int __gpio_is_occupied(struct pxa_gpio_chip *pchip, unsigned gpio)
228{
229 void __iomem *base;
230 unsigned long gafr = 0, gpdr = 0;
231 int ret, af = 0, dir = 0;
232
233 base = gpio_bank_base(&pchip->chip, gpio);
234 gpdr = readl_relaxed(base + GPDR_OFFSET);
235
236 switch (gpio_type) {
237 case PXA25X_GPIO:
238 case PXA26X_GPIO:
239 case PXA27X_GPIO:
240 gafr = readl_relaxed(base + GAFR_OFFSET);
241 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
242 dir = gpdr & GPIO_bit(gpio);
243
244 if (__gpio_is_inverted(gpio))
245 ret = (af != 1) || (dir == 0);
246 else
247 ret = (af != 0) || (dir != 0);
248 break;
249 default:
250 ret = gpdr & GPIO_bit(gpio);
251 break;
252 }
253 return ret;
254}
255
256int extern_set_gpio_int_disabled(bool disable, int gpio_num)
257{
258 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
259 struct pxa_gpio_bank *c;
260 u32 gpio_bank, gpio_num_in_bank, regval;
261
262 if (!pchip)
263 return -ENODEV;
264
265 if (gpio_num < 0 || gpio_num > pxa_last_gpio) {
266 pr_err("%s wrong gpio: %d\n", __func__, gpio_num);
267 return -EINVAL;
268 }
269 gpio_bank = gpio_num / 32;
270 gpio_num_in_bank = gpio_num % 32;
271 c = &pchip->banks[gpio_bank];
272
273 if (disable && (c->gpio_edge_disabled & (0x1 << gpio_num_in_bank))) {
274 pr_err("%s gpio%d INT already disabled\n", __func__, gpio_num);
275 return -EPERM;
276 }
277 if ((!disable) && (!(c->gpio_edge_disabled & (0x1 << gpio_num_in_bank)))) {
278 pr_err("%s gpio%d INT already enabled\n", __func__, gpio_num);
279 return -EPERM;
280 }
281
282 if (disable) {
283 c->gpio_edge_disabled |= (0x1 << gpio_num_in_bank);
284
285 /* save original and set disabled */
286 regval = readl_relaxed(c->regbase + GRER_OFFSET);
287 c->original_grer &= ~(0x1 << gpio_num_in_bank);
288 c->original_grer |= ((0x1 << gpio_num_in_bank) & regval);
289 regval &= ~(0x1 << gpio_num_in_bank);
290 writel_relaxed(regval, c->regbase + GRER_OFFSET);
291
292 regval = readl_relaxed(c->regbase + GFER_OFFSET);
293 c->original_gfer &= ~(0x1 << gpio_num_in_bank);
294 c->original_gfer |= ((0x1 << gpio_num_in_bank) & regval);
295 regval &= ~(0x1 << gpio_num_in_bank);
296 writel_relaxed(regval, c->regbase + GFER_OFFSET);
297 } else {
298 c->gpio_edge_disabled &= ~(0x1 << gpio_num_in_bank);
299
300 /* set original and clear saved */
301 regval = readl_relaxed(c->regbase + GRER_OFFSET);
302 regval &= ~(0x1 << gpio_num_in_bank);
303 regval |= (c->original_grer & (0x1 << gpio_num_in_bank));
304 writel_relaxed(regval, c->regbase + GRER_OFFSET);
305 c->original_grer &= ~(0x1 << gpio_num_in_bank);
306
307 regval = readl_relaxed(c->regbase + GFER_OFFSET);
308 regval &= ~(0x1 << gpio_num_in_bank);
309 regval |= (c->original_gfer & (0x1 << gpio_num_in_bank));
310 writel_relaxed(regval, c->regbase + GFER_OFFSET);
311 c->original_gfer &= ~(0x1 << gpio_num_in_bank);
312 }
313 return 0;
314}
315
316int pxa_irq_to_gpio(int irq)
317{
318 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
319 int irq_gpio0;
320
321 irq_gpio0 = irq_find_mapping(pchip->irqdomain, 0);
322 if (irq_gpio0 > 0)
323 return irq - irq_gpio0;
324
325 return irq_gpio0;
326}
327
328static bool pxa_gpio_has_pinctrl(void)
329{
330 switch (gpio_type) {
331 case PXA3XX_GPIO:
332 case MMP2_GPIO:
333 case MMP_GPIO:
334 return false;
335
336 default:
337 return true;
338 }
339}
340
341static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
342{
343 struct pxa_gpio_chip *pchip = chip_to_pxachip(chip);
344
345 return irq_find_mapping(pchip->irqdomain, offset);
346}
347
348static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
349{
350 void __iomem *base = gpio_bank_base(chip, offset);
351 uint32_t value, mask = GPIO_bit(offset);
352 unsigned long flags;
353 int ret;
354
355 if (pxa_gpio_has_pinctrl()) {
356 ret = pinctrl_gpio_direction_input(chip->base + offset);
357 if (ret)
358 return ret;
359 }
360
361 spin_lock_irqsave(&gpio_lock, flags);
362
363 value = readl_relaxed(base + GPDR_OFFSET);
364 if (__gpio_is_inverted(chip->base + offset))
365 value |= mask;
366 else
367 value &= ~mask;
368 writel_relaxed(value, base + GPDR_OFFSET);
369
370 spin_unlock_irqrestore(&gpio_lock, flags);
371 return 0;
372}
373
374static int pxa_gpio_direction_output(struct gpio_chip *chip,
375 unsigned offset, int value)
376{
377 void __iomem *base = gpio_bank_base(chip, offset);
378 uint32_t tmp, mask = GPIO_bit(offset);
379 unsigned long flags;
380 int ret;
381
382 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
383
384 if (pxa_gpio_has_pinctrl()) {
385 ret = pinctrl_gpio_direction_output(chip->base + offset);
386 if (ret)
387 return ret;
388 }
389
390 spin_lock_irqsave(&gpio_lock, flags);
391
392 tmp = readl_relaxed(base + GPDR_OFFSET);
393 if (__gpio_is_inverted(chip->base + offset))
394 tmp &= ~mask;
395 else
396 tmp |= mask;
397 writel_relaxed(tmp, base + GPDR_OFFSET);
398
399 spin_unlock_irqrestore(&gpio_lock, flags);
400 return 0;
401}
402
403static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
404{
405 void __iomem *base = gpio_bank_base(chip, offset);
406 u32 gplr = readl_relaxed(base + GPLR_OFFSET);
407
408 return !!(gplr & GPIO_bit(offset));
409}
410
411static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
412{
413 void __iomem *base = gpio_bank_base(chip, offset);
414
415 writel_relaxed(GPIO_bit(offset),
416 base + (value ? GPSR_OFFSET : GPCR_OFFSET));
417}
418
419#ifdef CONFIG_OF_GPIO
420static int pxa_gpio_of_xlate(struct gpio_chip *gc,
421 const struct of_phandle_args *gpiospec,
422 u32 *flags)
423{
424 if (gpiospec->args[0] > pxa_last_gpio)
425 return -EINVAL;
426
427 if (flags)
428 *flags = gpiospec->args[1];
429
430 return gpiospec->args[0];
431}
432#endif
433
434static int pxa_init_gpio_chip(struct pxa_gpio_chip *pchip, int ngpio,
435 struct device_node *np, void __iomem *regbase)
436{
437 int i, gpio, nbanks = DIV_ROUND_UP(ngpio, 32);
438 struct pxa_gpio_bank *bank;
439
440 pchip->banks = devm_kcalloc(pchip->dev, nbanks, sizeof(*pchip->banks),
441 GFP_KERNEL);
442 if (!pchip->banks)
443 return -ENOMEM;
444
445 pchip->chip.label = "gpio-pxa";
446 pchip->chip.direction_input = pxa_gpio_direction_input;
447 pchip->chip.direction_output = pxa_gpio_direction_output;
448 pchip->chip.get = pxa_gpio_get;
449 pchip->chip.set = pxa_gpio_set;
450 pchip->chip.to_irq = pxa_gpio_to_irq;
451 pchip->chip.ngpio = ngpio;
452
453 if (pxa_gpio_has_pinctrl()) {
454 pchip->chip.request = gpiochip_generic_request;
455 pchip->chip.free = gpiochip_generic_free;
456 }
457
458#ifdef CONFIG_OF_GPIO
459 pchip->chip.of_node = np;
460 pchip->chip.of_xlate = pxa_gpio_of_xlate;
461 pchip->chip.of_gpio_n_cells = 2;
462#endif
463
464 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
465 bank = pchip->banks + i;
466 bank->regbase = regbase + BANK_OFF(i);
467 }
468
469 return gpiochip_add_data(&pchip->chip, pchip);
470}
471
472/* Update only those GRERx and GFERx edge detection register bits if those
473 * bits are set in c->irq_mask
474 */
475static inline void update_edge_detect(struct pxa_gpio_bank *c)
476{
477 uint32_t grer, gfer;
478
479 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
480 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
481 grer |= c->irq_edge_rise & c->irq_mask;
482 gfer |= c->irq_edge_fall & c->irq_mask;
483 writel_relaxed(grer, c->regbase + GRER_OFFSET);
484 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
485}
486
487static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
488{
489 struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
490 unsigned int gpio = irqd_to_hwirq(d);
491 struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
492 unsigned long gpdr, mask = GPIO_bit(gpio);
493
494 if (type == IRQ_TYPE_PROBE) {
495 /* Don't mess with enabled GPIOs using preconfigured edges or
496 * GPIOs set to alternate function or to output during probe
497 */
498 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
499 return 0;
500
501 if (__gpio_is_occupied(pchip, gpio))
502 return 0;
503
504 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
505 }
506
507 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
508
509 if (__gpio_is_inverted(gpio))
510 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
511 else
512 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
513
514 if (type & IRQ_TYPE_EDGE_RISING)
515 c->irq_edge_rise |= mask;
516 else
517 c->irq_edge_rise &= ~mask;
518
519 if (type & IRQ_TYPE_EDGE_FALLING)
520 c->irq_edge_fall |= mask;
521 else
522 c->irq_edge_fall &= ~mask;
523
524 update_edge_detect(c);
525
526 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
527 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
528 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
529 return 0;
530}
531
532static irqreturn_t pxa_gpio_demux_handler(int in_irq, void *d)
533{
534 int loop, gpio, n, handled = 0;
535 unsigned long gedr;
536 struct pxa_gpio_chip *pchip = d;
537 struct pxa_gpio_bank *c;
538
539 do {
540 loop = 0;
541 for_each_gpio_bank(gpio, c, pchip) {
542 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
543 gedr = gedr & c->irq_mask;
544 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
545
546 for_each_set_bit(n, &gedr, BITS_PER_LONG) {
547 loop = 1;
548
549 generic_handle_irq(
550 irq_find_mapping(pchip->irqdomain,
551 gpio + n));
552 }
553 }
554 handled += loop;
555 } while (loop);
556
557 return handled ? IRQ_HANDLED : IRQ_NONE;
558}
559
560static irqreturn_t pxa_gpio_direct_handler(int in_irq, void *d)
561{
562 struct pxa_gpio_chip *pchip = d;
563
564 if (in_irq == pchip->irq0) {
565 generic_handle_irq(irq_find_mapping(pchip->irqdomain, 0));
566 } else if (in_irq == pchip->irq1) {
567 generic_handle_irq(irq_find_mapping(pchip->irqdomain, 1));
568 } else {
569 pr_err("%s() unknown irq %d\n", __func__, in_irq);
570 return IRQ_NONE;
571 }
572 return IRQ_HANDLED;
573}
574
575static void pxa_ack_muxed_gpio(struct irq_data *d)
576{
577 struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
578 unsigned int gpio = irqd_to_hwirq(d);
579 void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
580
581 writel_relaxed(GPIO_bit(gpio), base + GEDR_OFFSET);
582}
583
584static void pxa_mask_muxed_gpio(struct irq_data *d)
585{
586 struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
587 unsigned int gpio = irqd_to_hwirq(d);
588 struct pxa_gpio_bank *b = gpio_to_pxabank(&pchip->chip, gpio);
589 void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
590 uint32_t grer, gfer;
591
592 b->irq_mask &= ~GPIO_bit(gpio);
593
594 grer = readl_relaxed(base + GRER_OFFSET) & ~GPIO_bit(gpio);
595 gfer = readl_relaxed(base + GFER_OFFSET) & ~GPIO_bit(gpio);
596 writel_relaxed(grer, base + GRER_OFFSET);
597 writel_relaxed(gfer, base + GFER_OFFSET);
598}
599
600static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
601{
602 struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
603 unsigned int gpio = irqd_to_hwirq(d);
604
605 if (pchip->set_wake)
606 return pchip->set_wake(gpio, on);
607 else
608 return 0;
609}
610
611static void pxa_unmask_muxed_gpio(struct irq_data *d)
612{
613 struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
614 unsigned int gpio = irqd_to_hwirq(d);
615 struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
616
617 c->irq_mask |= GPIO_bit(gpio);
618 update_edge_detect(c);
619}
620
621static struct irq_chip pxa_muxed_gpio_chip = {
622 .name = "GPIO",
623 .irq_ack = pxa_ack_muxed_gpio,
624 .irq_mask = pxa_mask_muxed_gpio,
625 .irq_unmask = pxa_unmask_muxed_gpio,
626 .irq_set_type = pxa_gpio_irq_type,
627 .irq_set_wake = pxa_gpio_set_wake,
628};
629
630static int pxa_gpio_nums(struct platform_device *pdev)
631{
632 const struct platform_device_id *id = platform_get_device_id(pdev);
633 struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
634 int count = 0;
635
636 switch (pxa_id->type) {
637 case PXA25X_GPIO:
638 case PXA26X_GPIO:
639 case PXA27X_GPIO:
640 case PXA3XX_GPIO:
641 case PXA93X_GPIO:
642 case MMP_GPIO:
643 case MMP2_GPIO:
644 case PXA1928_GPIO:
645 gpio_type = pxa_id->type;
646 count = pxa_id->gpio_nums - 1;
647 break;
648 default:
649 count = -EINVAL;
650 break;
651 }
652 return count;
653}
654
655static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
656 irq_hw_number_t hw)
657{
658 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
659 handle_edge_irq);
660 irq_set_chip_data(irq, d->host_data);
661 irq_set_noprobe(irq);
662 return 0;
663}
664
665static const struct irq_domain_ops pxa_irq_domain_ops = {
666 .map = pxa_irq_domain_map,
667 .xlate = irq_domain_xlate_twocell,
668};
669
670#ifdef CONFIG_OF
671static const struct of_device_id pxa_gpio_dt_ids[] = {
672 { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
673 { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
674 { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
675 { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
676 { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
677 { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
678 { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
679 { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
680 {}
681};
682
683static int pxa_gpio_probe_dt(struct platform_device *pdev,
684 struct pxa_gpio_chip *pchip)
685{
686 int nr_gpios;
687 const struct pxa_gpio_id *gpio_id;
688
689 gpio_id = of_device_get_match_data(&pdev->dev);
690 gpio_type = gpio_id->type;
691
692 nr_gpios = gpio_id->gpio_nums;
693 pxa_last_gpio = nr_gpios - 1;
694
695 irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, nr_gpios, 0);
696 if (irq_base < 0) {
697 dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
698 return irq_base;
699 }
700 return irq_base;
701}
702#else
703#define pxa_gpio_probe_dt(pdev, pchip) (-1)
704#endif
705
706static int pxa_gpio_probe(struct platform_device *pdev)
707{
708 struct pxa_gpio_chip *pchip;
709 struct pxa_gpio_bank *c;
710 struct clk *clk;
711 struct pxa_gpio_platform_data *info;
712 void __iomem *gpio_reg_base;
713 int gpio, ret;
714 int irq0 = 0, irq1 = 0, irq_mux;
715
716 pchip = devm_kzalloc(&pdev->dev, sizeof(*pchip), GFP_KERNEL);
717 if (!pchip)
718 return -ENOMEM;
719 pchip->dev = &pdev->dev;
720
721 info = dev_get_platdata(&pdev->dev);
722 if (info) {
723 irq_base = info->irq_base;
724 if (irq_base <= 0)
725 return -EINVAL;
726 pxa_last_gpio = pxa_gpio_nums(pdev);
727 pchip->set_wake = info->gpio_set_wake;
728 } else {
729 irq_base = pxa_gpio_probe_dt(pdev, pchip);
730 if (irq_base < 0)
731 return -EINVAL;
732 }
733
734 if (!pxa_last_gpio)
735 return -EINVAL;
736
737 pchip->irqdomain = irq_domain_add_legacy(pdev->dev.of_node,
738 pxa_last_gpio + 1, irq_base,
739 0, &pxa_irq_domain_ops, pchip);
740 if (!pchip->irqdomain)
741 return -ENOMEM;
742
743 irq0 = platform_get_irq_byname(pdev, "gpio0");
744 irq1 = platform_get_irq_byname(pdev, "gpio1");
745 irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
746 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
747 || (irq_mux <= 0))
748 return -EINVAL;
749
750 pchip->irq0 = irq0;
751 pchip->irq1 = irq1;
752
753 gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
754 if (IS_ERR(gpio_reg_base))
755 return PTR_ERR(gpio_reg_base);
756
757 clk = clk_get(&pdev->dev, NULL);
758 if (IS_ERR(clk)) {
759 dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
760 PTR_ERR(clk));
761 return PTR_ERR(clk);
762 }
763 ret = clk_prepare_enable(clk);
764 if (ret) {
765 clk_put(clk);
766 return ret;
767 }
768
769 /* Initialize GPIO chips */
770 ret = pxa_init_gpio_chip(pchip, pxa_last_gpio + 1, pdev->dev.of_node,
771 gpio_reg_base);
772 if (ret) {
773 clk_put(clk);
774 return ret;
775 }
776
777 /* clear all GPIO edge detects */
778 for_each_gpio_bank(gpio, c, pchip) {
779 writel_relaxed(0, c->regbase + GFER_OFFSET);
780 writel_relaxed(0, c->regbase + GRER_OFFSET);
781 writel_relaxed(~0, c->regbase + GEDR_OFFSET);
782 /* unmask GPIO edge detect for AP side */
783 if (gpio_is_mmp_type(gpio_type))
784 writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
785 }
786
787 if (irq0 > 0) {
788 ret = devm_request_irq(&pdev->dev,
789 irq0, pxa_gpio_direct_handler, 0,
790 "gpio-0", pchip);
791 if (ret)
792 dev_err(&pdev->dev, "request of gpio0 irq failed: %d\n",
793 ret);
794 }
795 if (irq1 > 0) {
796 ret = devm_request_irq(&pdev->dev,
797 irq1, pxa_gpio_direct_handler, 0,
798 "gpio-1", pchip);
799 if (ret)
800 dev_err(&pdev->dev, "request of gpio1 irq failed: %d\n",
801 ret);
802 }
803 ret = devm_request_irq(&pdev->dev,
804 irq_mux, pxa_gpio_demux_handler, 0,
805 "gpio-mux", pchip);
806 if (ret)
807 dev_err(&pdev->dev, "request of gpio-mux irq failed: %d\n",
808 ret);
809
810 pxa_gpio_chip = pchip;
811
812 return 0;
813}
814
815static const struct platform_device_id gpio_id_table[] = {
816 { "pxa25x-gpio", (unsigned long)&pxa25x_id },
817 { "pxa26x-gpio", (unsigned long)&pxa26x_id },
818 { "pxa27x-gpio", (unsigned long)&pxa27x_id },
819 { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
820 { "pxa93x-gpio", (unsigned long)&pxa93x_id },
821 { "mmp-gpio", (unsigned long)&mmp_id },
822 { "mmp2-gpio", (unsigned long)&mmp2_id },
823 { "pxa1928-gpio", (unsigned long)&pxa1928_id },
824 { },
825};
826
827static struct platform_driver pxa_gpio_driver = {
828 .probe = pxa_gpio_probe,
829 .driver = {
830 .name = "pxa-gpio",
831 .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
832 },
833 .id_table = gpio_id_table,
834};
835
836static int __init pxa_gpio_legacy_init(void)
837{
838 if (of_have_populated_dt())
839 return 0;
840
841 return platform_driver_register(&pxa_gpio_driver);
842}
843postcore_initcall(pxa_gpio_legacy_init);
844
845static int __init pxa_gpio_dt_init(void)
846{
847 if (of_have_populated_dt())
848 return platform_driver_register(&pxa_gpio_driver);
849
850 return 0;
851}
852subsys_initcall(pxa_gpio_dt_init);
853
854#ifdef CONFIG_PM
855static int pxa_gpio_suspend(void)
856{
857 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
858 struct pxa_gpio_bank *c;
859 int gpio;
860
861 if (!pchip)
862 return 0;
863
864 for_each_gpio_bank(gpio, c, pchip) {
865#if 0 /* disable save/restore to avoid mismatch with cp/dsp etc other cpus */
866 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
867 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
868 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
869 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
870#endif
871 /* Clear GPIO transition detect bits */
872 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
873 }
874
875 gpio_int_wakeup_idx = 0;
876 return 0;
877}
878
879static void pxa_gpio_resume(void)
880{
881#if 0 /* disable save/restore to avoid mismatch with cp/dsp etc other cpus */
882 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
883 struct pxa_gpio_bank *c;
884 int gpio;
885
886 if (!pchip)
887 return;
888
889 for_each_gpio_bank(gpio, c, pchip) {
890 /* restore level with set/clear */
891 writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
892 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
893
894 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
895 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
896 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
897 }
898#endif
899 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
900 struct pxa_gpio_bank *c;
901 int gpio, bit;
902 unsigned long regval;
903
904 if (!pchip)
905 return;
906
907 for_each_gpio_bank(gpio, c, pchip) {
908 pr_pm_debug("GPLR[%02d-%03d]: 0x%08X\n", gpio, gpio + 31, readl_relaxed(c->regbase + GPLR_OFFSET));
909 regval = readl_relaxed(c->regbase + GEDR_OFFSET);
910 pr_pm_debug("GEDR[%02d-%03d]: 0x%08X\n", gpio, gpio + 31, (u32)regval);
911 bit = find_first_bit(&regval, BITS_PER_LONG);
912 while (bit < BITS_PER_LONG) {
913 pr_pm_debug("Woken by GPIO%d INT\n", bit + gpio);
914 asr_save_gpio_int_wakeup_event(bit + gpio);
915 bit = find_next_bit(&regval, BITS_PER_LONG, bit + 1);
916 }
917 }
918}
919#else
920#define pxa_gpio_suspend NULL
921#define pxa_gpio_resume NULL
922#endif
923
924static struct syscore_ops pxa_gpio_syscore_ops = {
925 .suspend = pxa_gpio_suspend,
926 .resume = pxa_gpio_resume,
927};
928
929static int __init pxa_gpio_sysinit(void)
930{
931 register_syscore_ops(&pxa_gpio_syscore_ops);
932 return 0;
933}
934postcore_initcall(pxa_gpio_sysinit);