1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mach-w90x900/gpio.c
5 * Generic nuc900 GPIO handling
7 * Wan ZongShun <mcuos.com@gmail.com>
10 #include <linux/clk.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
20 #include <linux/gpio/driver.h>
22 #include <mach/hardware.h>
24 #define GPIO_BASE (W90X900_VA_GPIO)
25 #define GPIO_DIR (0x04)
26 #define GPIO_OUT (0x08)
27 #define GPIO_IN (0x0C)
28 #define GROUPINERV (0x10)
29 #define GPIO_GPIO(Nb) (0x00000001 << (Nb))
31 #define NUC900_GPIO_CHIP(name, base_gpio, nr_gpio) \
35 .direction_input = nuc900_dir_input, \
36 .direction_output = nuc900_dir_output, \
37 .get = nuc900_gpio_get, \
38 .set = nuc900_gpio_set, \
44 struct nuc900_gpio_chip
{
45 struct gpio_chip chip
;
46 void __iomem
*regbase
; /* Base of group register*/
50 static int nuc900_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
52 struct nuc900_gpio_chip
*nuc900_gpio
= gpiochip_get_data(chip
);
53 void __iomem
*pio
= nuc900_gpio
->regbase
+ GPIO_IN
;
56 regval
= __raw_readl(pio
);
57 regval
&= GPIO_GPIO(offset
);
62 static void nuc900_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
64 struct nuc900_gpio_chip
*nuc900_gpio
= gpiochip_get_data(chip
);
65 void __iomem
*pio
= nuc900_gpio
->regbase
+ GPIO_OUT
;
69 spin_lock_irqsave(&nuc900_gpio
->gpio_lock
, flags
);
71 regval
= __raw_readl(pio
);
74 regval
|= GPIO_GPIO(offset
);
76 regval
&= ~GPIO_GPIO(offset
);
78 __raw_writel(regval
, pio
);
80 spin_unlock_irqrestore(&nuc900_gpio
->gpio_lock
, flags
);
83 static int nuc900_dir_input(struct gpio_chip
*chip
, unsigned offset
)
85 struct nuc900_gpio_chip
*nuc900_gpio
= gpiochip_get_data(chip
);
86 void __iomem
*pio
= nuc900_gpio
->regbase
+ GPIO_DIR
;
90 spin_lock_irqsave(&nuc900_gpio
->gpio_lock
, flags
);
92 regval
= __raw_readl(pio
);
93 regval
&= ~GPIO_GPIO(offset
);
94 __raw_writel(regval
, pio
);
96 spin_unlock_irqrestore(&nuc900_gpio
->gpio_lock
, flags
);
101 static int nuc900_dir_output(struct gpio_chip
*chip
, unsigned offset
, int val
)
103 struct nuc900_gpio_chip
*nuc900_gpio
= gpiochip_get_data(chip
);
104 void __iomem
*outreg
= nuc900_gpio
->regbase
+ GPIO_OUT
;
105 void __iomem
*pio
= nuc900_gpio
->regbase
+ GPIO_DIR
;
109 spin_lock_irqsave(&nuc900_gpio
->gpio_lock
, flags
);
111 regval
= __raw_readl(pio
);
112 regval
|= GPIO_GPIO(offset
);
113 __raw_writel(regval
, pio
);
115 regval
= __raw_readl(outreg
);
118 regval
|= GPIO_GPIO(offset
);
120 regval
&= ~GPIO_GPIO(offset
);
122 __raw_writel(regval
, outreg
);
124 spin_unlock_irqrestore(&nuc900_gpio
->gpio_lock
, flags
);
129 static struct nuc900_gpio_chip nuc900_gpio
[] = {
130 NUC900_GPIO_CHIP("GROUPC", 0, 16),
131 NUC900_GPIO_CHIP("GROUPD", 16, 10),
132 NUC900_GPIO_CHIP("GROUPE", 26, 14),
133 NUC900_GPIO_CHIP("GROUPF", 40, 10),
134 NUC900_GPIO_CHIP("GROUPG", 50, 17),
135 NUC900_GPIO_CHIP("GROUPH", 67, 8),
136 NUC900_GPIO_CHIP("GROUPI", 75, 17),
139 void __init
nuc900_init_gpio(int nr_group
)
142 struct nuc900_gpio_chip
*gpio_chip
;
144 for (i
= 0; i
< nr_group
; i
++) {
145 gpio_chip
= &nuc900_gpio
[i
];
146 spin_lock_init(&gpio_chip
->gpio_lock
);
147 gpio_chip
->regbase
= GPIO_BASE
+ i
* GROUPINERV
;
148 gpiochip_add_data(&gpio_chip
->chip
, gpio_chip
);