2 * arch/arm/plat-orion/gpio.c
4 * Marvell Orion SoC GPIO handling.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/bitops.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/leds.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_address.h>
27 #include <plat/orion-gpio.h>
30 * GPIO unit register offsets.
32 #define GPIO_OUT_OFF 0x0000
33 #define GPIO_IO_CONF_OFF 0x0004
34 #define GPIO_BLINK_EN_OFF 0x0008
35 #define GPIO_IN_POL_OFF 0x000c
36 #define GPIO_DATA_IN_OFF 0x0010
37 #define GPIO_EDGE_CAUSE_OFF 0x0014
38 #define GPIO_EDGE_MASK_OFF 0x0018
39 #define GPIO_LEVEL_MASK_OFF 0x001c
41 struct orion_gpio_chip {
42 struct gpio_chip chip;
45 unsigned long valid_input;
46 unsigned long valid_output;
48 int secondary_irq_base;
49 struct irq_domain *domain;
52 static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
54 return ochip->base + GPIO_OUT_OFF;
57 static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
59 return ochip->base + GPIO_IO_CONF_OFF;
62 static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
64 return ochip->base + GPIO_BLINK_EN_OFF;
67 static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
69 return ochip->base + GPIO_IN_POL_OFF;
72 static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
74 return ochip->base + GPIO_DATA_IN_OFF;
77 static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
79 return ochip->base + GPIO_EDGE_CAUSE_OFF;
82 static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
84 return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
87 static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
89 return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
93 static struct orion_gpio_chip orion_gpio_chips[2];
94 static int orion_gpio_chip_count;
97 __set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
101 u = readl(GPIO_IO_CONF(ochip));
106 writel(u, GPIO_IO_CONF(ochip));
109 static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
113 u = readl(GPIO_OUT(ochip));
118 writel(u, GPIO_OUT(ochip));
122 __set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
126 u = readl(GPIO_BLINK_EN(ochip));
131 writel(u, GPIO_BLINK_EN(ochip));
135 orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
137 if (pin >= ochip->chip.ngpio)
140 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
143 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
149 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
156 static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
158 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
160 if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
161 orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
167 static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
169 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
172 if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
175 spin_lock_irqsave(&ochip->lock, flags);
176 __set_direction(ochip, pin, 1);
177 spin_unlock_irqrestore(&ochip->lock, flags);
182 static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
184 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
187 if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
188 val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
190 val = readl(GPIO_OUT(ochip));
193 return (val >> pin) & 1;
197 orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
199 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
202 if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
205 spin_lock_irqsave(&ochip->lock, flags);
206 __set_blinking(ochip, pin, 0);
207 __set_level(ochip, pin, value);
208 __set_direction(ochip, pin, 0);
209 spin_unlock_irqrestore(&ochip->lock, flags);
214 static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
216 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
219 spin_lock_irqsave(&ochip->lock, flags);
220 __set_level(ochip, pin, value);
221 spin_unlock_irqrestore(&ochip->lock, flags);
224 static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
226 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
228 return irq_create_mapping(ochip->domain,
229 ochip->secondary_irq_base + pin);
233 * Orion-specific GPIO API extensions.
235 static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
239 for (i = 0; i < orion_gpio_chip_count; i++) {
240 struct orion_gpio_chip *ochip = orion_gpio_chips + i;
241 struct gpio_chip *chip = &ochip->chip;
243 if (pin >= chip->base && pin < chip->base + chip->ngpio)
250 void __init orion_gpio_set_unused(unsigned pin)
252 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
257 pin -= ochip->chip.base;
259 /* Configure as output, drive low. */
260 __set_level(ochip, pin, 0);
261 __set_direction(ochip, pin, 0);
264 void __init orion_gpio_set_valid(unsigned pin, int mode)
266 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
271 pin -= ochip->chip.base;
274 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
276 if (mode & GPIO_INPUT_OK)
277 __set_bit(pin, &ochip->valid_input);
279 __clear_bit(pin, &ochip->valid_input);
281 if (mode & GPIO_OUTPUT_OK)
282 __set_bit(pin, &ochip->valid_output);
284 __clear_bit(pin, &ochip->valid_output);
287 void orion_gpio_set_blink(unsigned pin, int blink)
289 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
295 spin_lock_irqsave(&ochip->lock, flags);
296 __set_level(ochip, pin & 31, 0);
297 __set_blinking(ochip, pin & 31, blink);
298 spin_unlock_irqrestore(&ochip->lock, flags);
300 EXPORT_SYMBOL(orion_gpio_set_blink);
302 #define ORION_BLINK_HALF_PERIOD 100 /* ms */
304 int orion_gpio_led_blink_set(struct gpio_desc *desc, int state,
305 unsigned long *delay_on, unsigned long *delay_off)
307 unsigned gpio = desc_to_gpio(desc);
309 if (delay_on && delay_off && !*delay_on && !*delay_off)
310 *delay_on = *delay_off = ORION_BLINK_HALF_PERIOD;
313 case GPIO_LED_NO_BLINK_LOW:
314 case GPIO_LED_NO_BLINK_HIGH:
315 orion_gpio_set_blink(gpio, 0);
316 gpiod_set_raw_value(desc, state);
319 orion_gpio_set_blink(gpio, 1);
323 EXPORT_SYMBOL_GPL(orion_gpio_led_blink_set);
326 /*****************************************************************************
329 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
330 * value of the line or the opposite value.
332 * Level IRQ handlers: DATA_IN is used directly as cause register.
333 * Interrupt are masked by LEVEL_MASK registers.
334 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
335 * Interrupt are masked by EDGE_MASK registers.
336 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
337 * the polarity to catch the next line transaction.
338 * This is a race condition that might not perfectly
339 * work on some use cases.
341 * Every eight GPIO lines are grouped (OR'ed) before going up to main
345 * data-in /--------| |-----| |----\
346 * -----| |----- ---- to main cause reg
347 * X \----------------| |----/
348 * polarity LEVEL mask
350 ****************************************************************************/
352 static int gpio_irq_set_type(struct irq_data *d, u32 type)
354 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
355 struct irq_chip_type *ct = irq_data_get_chip_type(d);
356 struct orion_gpio_chip *ochip = gc->private;
360 pin = d->hwirq - ochip->secondary_irq_base;
362 u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
367 type &= IRQ_TYPE_SENSE_MASK;
368 if (type == IRQ_TYPE_NONE)
371 /* Check if we need to change chip and handler */
372 if (!(ct->type & type))
373 if (irq_setup_alt_chip(d, type))
377 * Configure interrupt polarity.
379 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
380 u = readl(GPIO_IN_POL(ochip));
382 writel(u, GPIO_IN_POL(ochip));
383 } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
384 u = readl(GPIO_IN_POL(ochip));
386 writel(u, GPIO_IN_POL(ochip));
387 } else if (type == IRQ_TYPE_EDGE_BOTH) {
390 v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
393 * set initial polarity based on current input level
395 u = readl(GPIO_IN_POL(ochip));
397 u |= 1 << pin; /* falling */
399 u &= ~(1 << pin); /* rising */
400 writel(u, GPIO_IN_POL(ochip));
405 static void gpio_irq_handler(struct irq_desc *desc)
407 struct orion_gpio_chip *ochip = irq_desc_get_handler_data(desc);
414 cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
415 cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
417 for (i = 0; i < ochip->chip.ngpio; i++) {
420 irq = ochip->secondary_irq_base + i;
422 if (!(cause & (1 << i)))
425 type = irq_get_trigger_type(irq);
426 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
427 /* Swap polarity (race with GPIO line) */
430 polarity = readl(GPIO_IN_POL(ochip));
432 writel(polarity, GPIO_IN_POL(ochip));
434 generic_handle_irq(irq);
438 #ifdef CONFIG_DEBUG_FS
439 #include <linux/seq_file.h>
441 static void orion_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
444 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
445 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
449 out = readl_relaxed(GPIO_OUT(ochip));
450 io_conf = readl_relaxed(GPIO_IO_CONF(ochip));
451 blink = readl_relaxed(GPIO_BLINK_EN(ochip));
452 in_pol = readl_relaxed(GPIO_IN_POL(ochip));
453 data_in = readl_relaxed(GPIO_DATA_IN(ochip));
454 cause = readl_relaxed(GPIO_EDGE_CAUSE(ochip));
455 edg_msk = readl_relaxed(GPIO_EDGE_MASK(ochip));
456 lvl_msk = readl_relaxed(GPIO_LEVEL_MASK(ochip));
458 for_each_requested_gpio(chip, i, label) {
463 is_out = !(io_conf & msk);
465 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
468 seq_printf(s, " out %s %s\n",
469 out & msk ? "hi" : "lo",
470 blink & msk ? "(blink )" : "");
474 seq_printf(s, " in %s (act %s) - IRQ",
475 (data_in ^ in_pol) & msk ? "hi" : "lo",
476 in_pol & msk ? "lo" : "hi");
477 if (!((edg_msk | lvl_msk) & msk)) {
478 seq_puts(s, " disabled\n");
482 seq_puts(s, " edge ");
484 seq_puts(s, " level");
485 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
489 #define orion_gpio_dbg_show NULL
492 static void orion_gpio_unmask_irq(struct irq_data *d)
494 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
495 struct irq_chip_type *ct = irq_data_get_chip_type(d);
500 reg_val = irq_reg_readl(gc, ct->regs.mask);
502 irq_reg_writel(gc, reg_val, ct->regs.mask);
506 static void orion_gpio_mask_irq(struct irq_data *d)
508 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
509 struct irq_chip_type *ct = irq_data_get_chip_type(d);
514 reg_val = irq_reg_readl(gc, ct->regs.mask);
516 irq_reg_writel(gc, reg_val, ct->regs.mask);
520 void __init orion_gpio_init(int gpio_base, int ngpio,
521 void __iomem *base, int mask_offset,
522 int secondary_irq_base,
525 struct orion_gpio_chip *ochip;
526 struct irq_chip_generic *gc;
527 struct irq_chip_type *ct;
531 if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
534 snprintf(gc_label, sizeof(gc_label), "orion_gpio%d",
535 orion_gpio_chip_count);
537 ochip = orion_gpio_chips + orion_gpio_chip_count;
538 ochip->chip.label = kstrdup(gc_label, GFP_KERNEL);
539 ochip->chip.request = orion_gpio_request;
540 ochip->chip.direction_input = orion_gpio_direction_input;
541 ochip->chip.get = orion_gpio_get;
542 ochip->chip.direction_output = orion_gpio_direction_output;
543 ochip->chip.set = orion_gpio_set;
544 ochip->chip.to_irq = orion_gpio_to_irq;
545 ochip->chip.base = gpio_base;
546 ochip->chip.ngpio = ngpio;
547 ochip->chip.can_sleep = 0;
548 ochip->chip.dbg_show = orion_gpio_dbg_show;
550 spin_lock_init(&ochip->lock);
551 ochip->base = (void __iomem *)base;
552 ochip->valid_input = 0;
553 ochip->valid_output = 0;
554 ochip->mask_offset = mask_offset;
555 ochip->secondary_irq_base = secondary_irq_base;
557 gpiochip_add_data(&ochip->chip, ochip);
560 * Mask and clear GPIO interrupts.
562 writel(0, GPIO_EDGE_CAUSE(ochip));
563 writel(0, GPIO_EDGE_MASK(ochip));
564 writel(0, GPIO_LEVEL_MASK(ochip));
566 /* Setup the interrupt handlers. Each chip can have up to 4
567 * interrupt handlers, with each handler dealing with 8 GPIO
570 for (i = 0; i < 4; i++) {
572 irq_set_chained_handler_and_data(irqs[i],
578 gc = irq_alloc_generic_chip("orion_gpio_irq", 2,
580 ochip->base, handle_level_irq);
583 ct->regs.mask = ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
584 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
585 ct->chip.irq_mask = orion_gpio_mask_irq;
586 ct->chip.irq_unmask = orion_gpio_unmask_irq;
587 ct->chip.irq_set_type = gpio_irq_set_type;
588 ct->chip.name = ochip->chip.label;
591 ct->regs.mask = ochip->mask_offset + GPIO_EDGE_MASK_OFF;
592 ct->regs.ack = GPIO_EDGE_CAUSE_OFF;
593 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
594 ct->chip.irq_ack = irq_gc_ack_clr_bit;
595 ct->chip.irq_mask = orion_gpio_mask_irq;
596 ct->chip.irq_unmask = orion_gpio_unmask_irq;
597 ct->chip.irq_set_type = gpio_irq_set_type;
598 ct->handler = handle_edge_irq;
599 ct->chip.name = ochip->chip.label;
601 irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
602 IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
604 /* Setup irq domain on top of the generic chip. */
605 ochip->domain = irq_domain_add_legacy(NULL,
607 ochip->secondary_irq_base,
608 ochip->secondary_irq_base,
609 &irq_domain_simple_ops,
612 panic("%s: couldn't allocate irq domain (DT).\n",
615 orion_gpio_chip_count++;