1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mach-sa1100/gpio.c
5 * Generic SA-1100 GPIO handling
7 #include <linux/gpio/driver.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
11 #include <linux/syscore_ops.h>
12 #include <soc/sa1100/pwer.h>
13 #include <mach/hardware.h>
14 #include <mach/irqs.h>
15 #include <mach/generic.h>
17 struct sa1100_gpio_chip {
18 struct gpio_chip chip;
19 void __iomem *membase;
27 #define sa1100_gpio_chip(x) container_of(x, struct sa1100_gpio_chip, chip)
40 static int sa1100_gpio_get(struct gpio_chip *chip, unsigned offset)
42 return readl_relaxed(sa1100_gpio_chip(chip)->membase + R_GPLR) &
46 static void sa1100_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
48 int reg = value ? R_GPSR : R_GPCR;
50 writel_relaxed(BIT(offset), sa1100_gpio_chip(chip)->membase + reg);
53 static int sa1100_get_direction(struct gpio_chip *chip, unsigned offset)
55 void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
57 if (readl_relaxed(gpdr) & BIT(offset))
58 return GPIO_LINE_DIRECTION_OUT;
60 return GPIO_LINE_DIRECTION_IN;
63 static int sa1100_direction_input(struct gpio_chip *chip, unsigned offset)
65 void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
68 local_irq_save(flags);
69 writel_relaxed(readl_relaxed(gpdr) & ~BIT(offset), gpdr);
70 local_irq_restore(flags);
75 static int sa1100_direction_output(struct gpio_chip *chip, unsigned offset, int value)
77 void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
80 local_irq_save(flags);
81 sa1100_gpio_set(chip, offset, value);
82 writel_relaxed(readl_relaxed(gpdr) | BIT(offset), gpdr);
83 local_irq_restore(flags);
88 static int sa1100_to_irq(struct gpio_chip *chip, unsigned offset)
90 return sa1100_gpio_chip(chip)->irqbase + offset;
93 static struct sa1100_gpio_chip sa1100_gpio_chip = {
96 .get_direction = sa1100_get_direction,
97 .direction_input = sa1100_direction_input,
98 .direction_output = sa1100_direction_output,
99 .set = sa1100_gpio_set,
100 .get = sa1100_gpio_get,
101 .to_irq = sa1100_to_irq,
103 .ngpio = GPIO_MAX + 1,
105 .membase = (void *)&GPLR,
106 .irqbase = IRQ_GPIO0,
110 * SA1100 GPIO edge detection for IRQs:
111 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
112 * Use this instead of directly setting GRER/GFER.
114 static void sa1100_update_edge_regs(struct sa1100_gpio_chip *sgc)
116 void *base = sgc->membase;
119 grer = sgc->irqrising & sgc->irqmask;
120 gfer = sgc->irqfalling & sgc->irqmask;
122 writel_relaxed(grer, base + R_GRER);
123 writel_relaxed(gfer, base + R_GFER);
126 static int sa1100_gpio_type(struct irq_data *d, unsigned int type)
128 struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
129 unsigned int mask = BIT(d->hwirq);
131 if (type == IRQ_TYPE_PROBE) {
132 if ((sgc->irqrising | sgc->irqfalling) & mask)
134 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
137 if (type & IRQ_TYPE_EDGE_RISING)
138 sgc->irqrising |= mask;
140 sgc->irqrising &= ~mask;
141 if (type & IRQ_TYPE_EDGE_FALLING)
142 sgc->irqfalling |= mask;
144 sgc->irqfalling &= ~mask;
146 sa1100_update_edge_regs(sgc);
152 * GPIO IRQs must be acknowledged.
154 static void sa1100_gpio_ack(struct irq_data *d)
156 struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
158 writel_relaxed(BIT(d->hwirq), sgc->membase + R_GEDR);
161 static void sa1100_gpio_mask(struct irq_data *d)
163 struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
164 unsigned int mask = BIT(d->hwirq);
166 sgc->irqmask &= ~mask;
168 sa1100_update_edge_regs(sgc);
171 static void sa1100_gpio_unmask(struct irq_data *d)
173 struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
174 unsigned int mask = BIT(d->hwirq);
176 sgc->irqmask |= mask;
178 sa1100_update_edge_regs(sgc);
181 static int sa1100_gpio_wake(struct irq_data *d, unsigned int on)
183 struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
184 int ret = sa11x0_gpio_set_wake(d->hwirq, on);
187 sgc->irqwake |= BIT(d->hwirq);
189 sgc->irqwake &= ~BIT(d->hwirq);
195 * This is for GPIO IRQs
197 static struct irq_chip sa1100_gpio_irq_chip = {
199 .irq_ack = sa1100_gpio_ack,
200 .irq_mask = sa1100_gpio_mask,
201 .irq_unmask = sa1100_gpio_unmask,
202 .irq_set_type = sa1100_gpio_type,
203 .irq_set_wake = sa1100_gpio_wake,
206 static int sa1100_gpio_irqdomain_map(struct irq_domain *d,
207 unsigned int irq, irq_hw_number_t hwirq)
209 struct sa1100_gpio_chip *sgc = d->host_data;
211 irq_set_chip_data(irq, sgc);
212 irq_set_chip_and_handler(irq, &sa1100_gpio_irq_chip, handle_edge_irq);
218 static const struct irq_domain_ops sa1100_gpio_irqdomain_ops = {
219 .map = sa1100_gpio_irqdomain_map,
220 .xlate = irq_domain_xlate_onetwocell,
223 static struct irq_domain *sa1100_gpio_irqdomain;
226 * IRQ 0-11 (GPIO) handler. We enter here with the
227 * irq_controller_lock held, and IRQs disabled. Decode the IRQ
228 * and call the handler.
230 static void sa1100_gpio_handler(struct irq_desc *desc)
232 struct sa1100_gpio_chip *sgc = irq_desc_get_handler_data(desc);
233 unsigned int irq, mask;
234 void __iomem *gedr = sgc->membase + R_GEDR;
236 mask = readl_relaxed(gedr);
239 * clear down all currently active IRQ sources.
240 * We will be processing them all.
242 writel_relaxed(mask, gedr);
247 generic_handle_irq(irq);
252 mask = readl_relaxed(gedr);
256 static int sa1100_gpio_suspend(void)
258 struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
261 * Set the appropriate edges for wakeup.
263 writel_relaxed(sgc->irqwake & sgc->irqrising, sgc->membase + R_GRER);
264 writel_relaxed(sgc->irqwake & sgc->irqfalling, sgc->membase + R_GFER);
267 * Clear any pending GPIO interrupts.
269 writel_relaxed(readl_relaxed(sgc->membase + R_GEDR),
270 sgc->membase + R_GEDR);
275 static void sa1100_gpio_resume(void)
277 sa1100_update_edge_regs(&sa1100_gpio_chip);
280 static struct syscore_ops sa1100_gpio_syscore_ops = {
281 .suspend = sa1100_gpio_suspend,
282 .resume = sa1100_gpio_resume,
285 static int __init sa1100_gpio_init_devicefs(void)
287 register_syscore_ops(&sa1100_gpio_syscore_ops);
291 device_initcall(sa1100_gpio_init_devicefs);
293 static const int sa1100_gpio_irqs[] __initconst = {
294 /* Install handlers for GPIO 0-10 edge detect interrupts */
306 /* Install handler for GPIO 11-27 edge detect interrupts */
310 void __init sa1100_init_gpio(void)
312 struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
315 /* clear all GPIO edge detects */
316 writel_relaxed(0, sgc->membase + R_GFER);
317 writel_relaxed(0, sgc->membase + R_GRER);
318 writel_relaxed(-1, sgc->membase + R_GEDR);
320 gpiochip_add_data(&sa1100_gpio_chip.chip, NULL);
322 sa1100_gpio_irqdomain = irq_domain_add_simple(NULL,
324 &sa1100_gpio_irqdomain_ops, sgc);
326 for (i = 0; i < ARRAY_SIZE(sa1100_gpio_irqs); i++)
327 irq_set_chained_handler_and_data(sa1100_gpio_irqs[i],
328 sa1100_gpio_handler, sgc);