1aaf9128aSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
255059114SPaul Mundt /*
355059114SPaul Mundt * arch/sh/boards/mach-x3proto/gpio.c
455059114SPaul Mundt *
555059114SPaul Mundt * Renesas SH-X3 Prototype Baseboard GPIO Support.
655059114SPaul Mundt *
7b98b3581SPaul Mundt * Copyright (C) 2010 - 2012 Paul Mundt
855059114SPaul Mundt */
955059114SPaul Mundt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1055059114SPaul Mundt
1155059114SPaul Mundt #include <linux/init.h>
1255059114SPaul Mundt #include <linux/interrupt.h>
13efed58f1SLinus Walleij #include <linux/gpio/driver.h>
1455059114SPaul Mundt #include <linux/irq.h>
1555059114SPaul Mundt #include <linux/kernel.h>
1655059114SPaul Mundt #include <linux/spinlock.h>
17b98b3581SPaul Mundt #include <linux/irqdomain.h>
1855059114SPaul Mundt #include <linux/io.h>
1955059114SPaul Mundt #include <mach/ilsel.h>
2055059114SPaul Mundt #include <mach/hardware.h>
2155059114SPaul Mundt
2255059114SPaul Mundt #define KEYCTLR 0xb81c0000
2355059114SPaul Mundt #define KEYOUTR 0xb81c0002
2455059114SPaul Mundt #define KEYDETR 0xb81c0004
2555059114SPaul Mundt
2655059114SPaul Mundt static DEFINE_SPINLOCK(x3proto_gpio_lock);
27b98b3581SPaul Mundt static struct irq_domain *x3proto_irq_domain;
2855059114SPaul Mundt
x3proto_gpio_direction_input(struct gpio_chip * chip,unsigned gpio)2955059114SPaul Mundt static int x3proto_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
3055059114SPaul Mundt {
3155059114SPaul Mundt unsigned long flags;
3255059114SPaul Mundt unsigned int data;
3355059114SPaul Mundt
3455059114SPaul Mundt spin_lock_irqsave(&x3proto_gpio_lock, flags);
3555059114SPaul Mundt data = __raw_readw(KEYCTLR);
3655059114SPaul Mundt data |= (1 << gpio);
3755059114SPaul Mundt __raw_writew(data, KEYCTLR);
3855059114SPaul Mundt spin_unlock_irqrestore(&x3proto_gpio_lock, flags);
3955059114SPaul Mundt
4055059114SPaul Mundt return 0;
4155059114SPaul Mundt }
4255059114SPaul Mundt
x3proto_gpio_get(struct gpio_chip * chip,unsigned gpio)4355059114SPaul Mundt static int x3proto_gpio_get(struct gpio_chip *chip, unsigned gpio)
4455059114SPaul Mundt {
4555059114SPaul Mundt return !!(__raw_readw(KEYDETR) & (1 << gpio));
4655059114SPaul Mundt }
4755059114SPaul Mundt
x3proto_gpio_to_irq(struct gpio_chip * chip,unsigned gpio)4855059114SPaul Mundt static int x3proto_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
4955059114SPaul Mundt {
50b98b3581SPaul Mundt int virq;
51b98b3581SPaul Mundt
52b98b3581SPaul Mundt if (gpio < chip->ngpio)
53b98b3581SPaul Mundt virq = irq_create_mapping(x3proto_irq_domain, gpio);
54b98b3581SPaul Mundt else
55b98b3581SPaul Mundt virq = -ENXIO;
56b98b3581SPaul Mundt
57b98b3581SPaul Mundt return virq;
5855059114SPaul Mundt }
5955059114SPaul Mundt
x3proto_gpio_irq_handler(struct irq_desc * desc)60bd0b9ac4SThomas Gleixner static void x3proto_gpio_irq_handler(struct irq_desc *desc)
6155059114SPaul Mundt {
628228a048SJiang Liu struct irq_data *data = irq_desc_get_irq_data(desc);
6379c98128SPaul Mundt struct irq_chip *chip = irq_data_get_irq_chip(data);
6455059114SPaul Mundt unsigned long mask;
6555059114SPaul Mundt int pin;
6655059114SPaul Mundt
6779c98128SPaul Mundt chip->irq_mask_ack(data);
6855059114SPaul Mundt
6955059114SPaul Mundt mask = __raw_readw(KEYDETR);
7055059114SPaul Mundt for_each_set_bit(pin, &mask, NR_BASEBOARD_GPIOS)
71*2e0e0ff4SMarc Zyngier generic_handle_domain_irq(x3proto_irq_domain, pin);
7255059114SPaul Mundt
7379c98128SPaul Mundt chip->irq_unmask(data);
7455059114SPaul Mundt }
7555059114SPaul Mundt
7655059114SPaul Mundt struct gpio_chip x3proto_gpio_chip = {
7755059114SPaul Mundt .label = "x3proto-gpio",
7855059114SPaul Mundt .direction_input = x3proto_gpio_direction_input,
7955059114SPaul Mundt .get = x3proto_gpio_get,
8055059114SPaul Mundt .to_irq = x3proto_gpio_to_irq,
8155059114SPaul Mundt .base = -1,
8255059114SPaul Mundt .ngpio = NR_BASEBOARD_GPIOS,
8355059114SPaul Mundt };
8455059114SPaul Mundt
x3proto_gpio_irq_map(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq)85b98b3581SPaul Mundt static int x3proto_gpio_irq_map(struct irq_domain *domain, unsigned int virq,
86b98b3581SPaul Mundt irq_hw_number_t hwirq)
87b98b3581SPaul Mundt {
88b98b3581SPaul Mundt irq_set_chip_and_handler_name(virq, &dummy_irq_chip, handle_simple_irq,
89b98b3581SPaul Mundt "gpio");
90b98b3581SPaul Mundt
91b98b3581SPaul Mundt return 0;
92b98b3581SPaul Mundt }
93b98b3581SPaul Mundt
94b98b3581SPaul Mundt static struct irq_domain_ops x3proto_gpio_irq_ops = {
95b98b3581SPaul Mundt .map = x3proto_gpio_irq_map,
96b98b3581SPaul Mundt .xlate = irq_domain_xlate_twocell,
97b98b3581SPaul Mundt };
98b98b3581SPaul Mundt
x3proto_gpio_setup(void)9955059114SPaul Mundt int __init x3proto_gpio_setup(void)
10055059114SPaul Mundt {
101b98b3581SPaul Mundt int ilsel, ret;
10255059114SPaul Mundt
10355059114SPaul Mundt ilsel = ilsel_enable(ILSEL_KEY);
10455059114SPaul Mundt if (unlikely(ilsel < 0))
10555059114SPaul Mundt return ilsel;
10655059114SPaul Mundt
107efed58f1SLinus Walleij ret = gpiochip_add_data(&x3proto_gpio_chip, NULL);
10855059114SPaul Mundt if (unlikely(ret))
10955059114SPaul Mundt goto err_gpio;
11055059114SPaul Mundt
111b98b3581SPaul Mundt x3proto_irq_domain = irq_domain_add_linear(NULL, NR_BASEBOARD_GPIOS,
112b98b3581SPaul Mundt &x3proto_gpio_irq_ops, NULL);
113b98b3581SPaul Mundt if (unlikely(!x3proto_irq_domain))
11455059114SPaul Mundt goto err_irq;
11555059114SPaul Mundt
11655059114SPaul Mundt pr_info("registering '%s' support, handling GPIOs %u -> %u, "
11755059114SPaul Mundt "bound to IRQ %u\n",
11855059114SPaul Mundt x3proto_gpio_chip.label, x3proto_gpio_chip.base,
11955059114SPaul Mundt x3proto_gpio_chip.base + x3proto_gpio_chip.ngpio,
12055059114SPaul Mundt ilsel);
12155059114SPaul Mundt
122fcb8918fSThomas Gleixner irq_set_chained_handler(ilsel, x3proto_gpio_irq_handler);
123fcb8918fSThomas Gleixner irq_set_irq_wake(ilsel, 1);
12455059114SPaul Mundt
12555059114SPaul Mundt return 0;
12655059114SPaul Mundt
12755059114SPaul Mundt err_irq:
12888d5e520Sabdoulaye berthe gpiochip_remove(&x3proto_gpio_chip);
12988d5e520Sabdoulaye berthe ret = 0;
13055059114SPaul Mundt err_gpio:
13155059114SPaul Mundt synchronize_irq(ilsel);
13255059114SPaul Mundt
13355059114SPaul Mundt ilsel_disable(ILSEL_KEY);
13455059114SPaul Mundt
13555059114SPaul Mundt return ret;
13655059114SPaul Mundt }
137