2 * linux/arch/arm/mach-sa1100/gpio.c
4 * Generic SA-1100 GPIO handling
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/gpio.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/syscore_ops.h>
15 #include <soc/sa1100/pwer.h>
16 #include <mach/hardware.h>
17 #include <mach/irqs.h>
19 struct sa1100_gpio_chip
{
20 struct gpio_chip chip
;
21 void __iomem
*membase
;
29 #define sa1100_gpio_chip(x) container_of(x, struct sa1100_gpio_chip, chip)
42 static int sa1100_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
44 return readl_relaxed(sa1100_gpio_chip(chip
)->membase
+ R_GPLR
) &
48 static void sa1100_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
50 int reg
= value
? R_GPSR
: R_GPCR
;
52 writel_relaxed(BIT(offset
), sa1100_gpio_chip(chip
)->membase
+ reg
);
55 static int sa1100_get_direction(struct gpio_chip
*chip
, unsigned offset
)
57 void __iomem
*gpdr
= sa1100_gpio_chip(chip
)->membase
+ R_GPDR
;
59 return !(readl_relaxed(gpdr
) & BIT(offset
));
62 static int sa1100_direction_input(struct gpio_chip
*chip
, unsigned offset
)
64 void __iomem
*gpdr
= sa1100_gpio_chip(chip
)->membase
+ R_GPDR
;
67 local_irq_save(flags
);
68 writel_relaxed(readl_relaxed(gpdr
) & ~BIT(offset
), gpdr
);
69 local_irq_restore(flags
);
74 static int sa1100_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
76 void __iomem
*gpdr
= sa1100_gpio_chip(chip
)->membase
+ R_GPDR
;
79 local_irq_save(flags
);
80 sa1100_gpio_set(chip
, offset
, value
);
81 writel_relaxed(readl_relaxed(gpdr
) | BIT(offset
), gpdr
);
82 local_irq_restore(flags
);
87 static int sa1100_to_irq(struct gpio_chip
*chip
, unsigned offset
)
89 return sa1100_gpio_chip(chip
)->irqbase
+ offset
;
92 static struct sa1100_gpio_chip sa1100_gpio_chip
= {
95 .get_direction
= sa1100_get_direction
,
96 .direction_input
= sa1100_direction_input
,
97 .direction_output
= sa1100_direction_output
,
98 .set
= sa1100_gpio_set
,
99 .get
= sa1100_gpio_get
,
100 .to_irq
= sa1100_to_irq
,
102 .ngpio
= GPIO_MAX
+ 1,
104 .membase
= (void *)&GPLR
,
105 .irqbase
= IRQ_GPIO0
,
109 * SA1100 GPIO edge detection for IRQs:
110 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
111 * Use this instead of directly setting GRER/GFER.
113 static void sa1100_update_edge_regs(struct sa1100_gpio_chip
*sgc
)
115 void *base
= sgc
->membase
;
118 grer
= sgc
->irqrising
& sgc
->irqmask
;
119 gfer
= sgc
->irqfalling
& sgc
->irqmask
;
121 writel_relaxed(grer
, base
+ R_GRER
);
122 writel_relaxed(gfer
, base
+ R_GFER
);
125 static int sa1100_gpio_type(struct irq_data
*d
, unsigned int type
)
127 struct sa1100_gpio_chip
*sgc
= irq_data_get_irq_chip_data(d
);
128 unsigned int mask
= BIT(d
->hwirq
);
130 if (type
== IRQ_TYPE_PROBE
) {
131 if ((sgc
->irqrising
| sgc
->irqfalling
) & mask
)
133 type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
136 if (type
& IRQ_TYPE_EDGE_RISING
)
137 sgc
->irqrising
|= mask
;
139 sgc
->irqrising
&= ~mask
;
140 if (type
& IRQ_TYPE_EDGE_FALLING
)
141 sgc
->irqfalling
|= mask
;
143 sgc
->irqfalling
&= ~mask
;
145 sa1100_update_edge_regs(sgc
);
151 * GPIO IRQs must be acknowledged.
153 static void sa1100_gpio_ack(struct irq_data
*d
)
155 struct sa1100_gpio_chip
*sgc
= irq_data_get_irq_chip_data(d
);
157 writel_relaxed(BIT(d
->hwirq
), sgc
->membase
+ R_GEDR
);
160 static void sa1100_gpio_mask(struct irq_data
*d
)
162 struct sa1100_gpio_chip
*sgc
= irq_data_get_irq_chip_data(d
);
163 unsigned int mask
= BIT(d
->hwirq
);
165 sgc
->irqmask
&= ~mask
;
167 sa1100_update_edge_regs(sgc
);
170 static void sa1100_gpio_unmask(struct irq_data
*d
)
172 struct sa1100_gpio_chip
*sgc
= irq_data_get_irq_chip_data(d
);
173 unsigned int mask
= BIT(d
->hwirq
);
175 sgc
->irqmask
|= mask
;
177 sa1100_update_edge_regs(sgc
);
180 static int sa1100_gpio_wake(struct irq_data
*d
, unsigned int on
)
182 struct sa1100_gpio_chip
*sgc
= irq_data_get_irq_chip_data(d
);
183 int ret
= sa11x0_gpio_set_wake(d
->hwirq
, on
);
186 sgc
->irqwake
|= BIT(d
->hwirq
);
188 sgc
->irqwake
&= ~BIT(d
->hwirq
);
194 * This is for GPIO IRQs
196 static struct irq_chip sa1100_gpio_irq_chip
= {
198 .irq_ack
= sa1100_gpio_ack
,
199 .irq_mask
= sa1100_gpio_mask
,
200 .irq_unmask
= sa1100_gpio_unmask
,
201 .irq_set_type
= sa1100_gpio_type
,
202 .irq_set_wake
= sa1100_gpio_wake
,
205 static int sa1100_gpio_irqdomain_map(struct irq_domain
*d
,
206 unsigned int irq
, irq_hw_number_t hwirq
)
208 struct sa1100_gpio_chip
*sgc
= d
->host_data
;
210 irq_set_chip_data(irq
, sgc
);
211 irq_set_chip_and_handler(irq
, &sa1100_gpio_irq_chip
, handle_edge_irq
);
217 static const struct irq_domain_ops sa1100_gpio_irqdomain_ops
= {
218 .map
= sa1100_gpio_irqdomain_map
,
219 .xlate
= irq_domain_xlate_onetwocell
,
222 static struct irq_domain
*sa1100_gpio_irqdomain
;
225 * IRQ 0-11 (GPIO) handler. We enter here with the
226 * irq_controller_lock held, and IRQs disabled. Decode the IRQ
227 * and call the handler.
229 static void sa1100_gpio_handler(struct irq_desc
*desc
)
231 struct sa1100_gpio_chip
*sgc
= irq_desc_get_handler_data(desc
);
232 unsigned int irq
, mask
;
233 void __iomem
*gedr
= sgc
->membase
+ R_GEDR
;
235 mask
= readl_relaxed(gedr
);
238 * clear down all currently active IRQ sources.
239 * We will be processing them all.
241 writel_relaxed(mask
, gedr
);
246 generic_handle_irq(irq
);
251 mask
= readl_relaxed(gedr
);
255 static int sa1100_gpio_suspend(void)
257 struct sa1100_gpio_chip
*sgc
= &sa1100_gpio_chip
;
260 * Set the appropriate edges for wakeup.
262 writel_relaxed(sgc
->irqwake
& sgc
->irqrising
, sgc
->membase
+ R_GRER
);
263 writel_relaxed(sgc
->irqwake
& sgc
->irqfalling
, sgc
->membase
+ R_GFER
);
266 * Clear any pending GPIO interrupts.
268 writel_relaxed(readl_relaxed(sgc
->membase
+ R_GEDR
),
269 sgc
->membase
+ R_GEDR
);
274 static void sa1100_gpio_resume(void)
276 sa1100_update_edge_regs(&sa1100_gpio_chip
);
279 static struct syscore_ops sa1100_gpio_syscore_ops
= {
280 .suspend
= sa1100_gpio_suspend
,
281 .resume
= sa1100_gpio_resume
,
284 static int __init
sa1100_gpio_init_devicefs(void)
286 register_syscore_ops(&sa1100_gpio_syscore_ops
);
290 device_initcall(sa1100_gpio_init_devicefs
);
292 static const int sa1100_gpio_irqs
[] __initconst
= {
293 /* Install handlers for GPIO 0-10 edge detect interrupts */
305 /* Install handler for GPIO 11-27 edge detect interrupts */
309 void __init
sa1100_init_gpio(void)
311 struct sa1100_gpio_chip
*sgc
= &sa1100_gpio_chip
;
314 /* clear all GPIO edge detects */
315 writel_relaxed(0, sgc
->membase
+ R_GFER
);
316 writel_relaxed(0, sgc
->membase
+ R_GRER
);
317 writel_relaxed(-1, sgc
->membase
+ R_GEDR
);
319 gpiochip_add_data(&sa1100_gpio_chip
.chip
, NULL
);
321 sa1100_gpio_irqdomain
= irq_domain_add_simple(NULL
,
323 &sa1100_gpio_irqdomain_ops
, sgc
);
325 for (i
= 0; i
< ARRAY_SIZE(sa1100_gpio_irqs
); i
++)
326 irq_set_chained_handler_and_data(sa1100_gpio_irqs
[i
],
327 sa1100_gpio_handler
, sgc
);