Kuninori Morimoto | aaf9128 | 2018-12-28 00:31:46 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 2 | /* |
| 3 | * arch/sh/boards/mach-x3proto/gpio.c |
| 4 | * |
| 5 | * Renesas SH-X3 Prototype Baseboard GPIO Support. |
| 6 | * |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 7 | * Copyright (C) 2010 - 2012 Paul Mundt |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 8 | */ |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/interrupt.h> |
Linus Walleij | efed58f | 2015-12-08 16:02:00 +0100 | [diff] [blame] | 13 | #include <linux/gpio/driver.h> |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 14 | #include <linux/irq.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/spinlock.h> |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 17 | #include <linux/irqdomain.h> |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 18 | #include <linux/io.h> |
| 19 | #include <mach/ilsel.h> |
| 20 | #include <mach/hardware.h> |
| 21 | |
| 22 | #define KEYCTLR 0xb81c0000 |
| 23 | #define KEYOUTR 0xb81c0002 |
| 24 | #define KEYDETR 0xb81c0004 |
| 25 | |
| 26 | static DEFINE_SPINLOCK(x3proto_gpio_lock); |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 27 | static struct irq_domain *x3proto_irq_domain; |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 28 | |
| 29 | static int x3proto_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 30 | { |
| 31 | unsigned long flags; |
| 32 | unsigned int data; |
| 33 | |
| 34 | spin_lock_irqsave(&x3proto_gpio_lock, flags); |
| 35 | data = __raw_readw(KEYCTLR); |
| 36 | data |= (1 << gpio); |
| 37 | __raw_writew(data, KEYCTLR); |
| 38 | spin_unlock_irqrestore(&x3proto_gpio_lock, flags); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int x3proto_gpio_get(struct gpio_chip *chip, unsigned gpio) |
| 44 | { |
| 45 | return !!(__raw_readw(KEYDETR) & (1 << gpio)); |
| 46 | } |
| 47 | |
| 48 | static int x3proto_gpio_to_irq(struct gpio_chip *chip, unsigned gpio) |
| 49 | { |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 50 | int virq; |
| 51 | |
| 52 | if (gpio < chip->ngpio) |
| 53 | virq = irq_create_mapping(x3proto_irq_domain, gpio); |
| 54 | else |
| 55 | virq = -ENXIO; |
| 56 | |
| 57 | return virq; |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 60 | static void x3proto_gpio_irq_handler(struct irq_desc *desc) |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 61 | { |
Jiang Liu | 8228a04 | 2015-07-13 20:51:25 +0000 | [diff] [blame] | 62 | struct irq_data *data = irq_desc_get_irq_data(desc); |
Paul Mundt | 79c9812 | 2010-10-27 15:11:01 +0900 | [diff] [blame] | 63 | struct irq_chip *chip = irq_data_get_irq_chip(data); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 64 | unsigned long mask; |
| 65 | int pin; |
| 66 | |
Paul Mundt | 79c9812 | 2010-10-27 15:11:01 +0900 | [diff] [blame] | 67 | chip->irq_mask_ack(data); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 68 | |
| 69 | mask = __raw_readw(KEYDETR); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 70 | for_each_set_bit(pin, &mask, NR_BASEBOARD_GPIOS) |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 71 | generic_handle_irq(irq_linear_revmap(x3proto_irq_domain, pin)); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 72 | |
Paul Mundt | 79c9812 | 2010-10-27 15:11:01 +0900 | [diff] [blame] | 73 | chip->irq_unmask(data); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | struct gpio_chip x3proto_gpio_chip = { |
| 77 | .label = "x3proto-gpio", |
| 78 | .direction_input = x3proto_gpio_direction_input, |
| 79 | .get = x3proto_gpio_get, |
| 80 | .to_irq = x3proto_gpio_to_irq, |
| 81 | .base = -1, |
| 82 | .ngpio = NR_BASEBOARD_GPIOS, |
| 83 | }; |
| 84 | |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 85 | static int x3proto_gpio_irq_map(struct irq_domain *domain, unsigned int virq, |
| 86 | irq_hw_number_t hwirq) |
| 87 | { |
| 88 | irq_set_chip_and_handler_name(virq, &dummy_irq_chip, handle_simple_irq, |
| 89 | "gpio"); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static struct irq_domain_ops x3proto_gpio_irq_ops = { |
| 95 | .map = x3proto_gpio_irq_map, |
| 96 | .xlate = irq_domain_xlate_twocell, |
| 97 | }; |
| 98 | |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 99 | int __init x3proto_gpio_setup(void) |
| 100 | { |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 101 | int ilsel, ret; |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 102 | |
| 103 | ilsel = ilsel_enable(ILSEL_KEY); |
| 104 | if (unlikely(ilsel < 0)) |
| 105 | return ilsel; |
| 106 | |
Linus Walleij | efed58f | 2015-12-08 16:02:00 +0100 | [diff] [blame] | 107 | ret = gpiochip_add_data(&x3proto_gpio_chip, NULL); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 108 | if (unlikely(ret)) |
| 109 | goto err_gpio; |
| 110 | |
Paul Mundt | b98b3581 | 2012-05-24 15:24:39 +0900 | [diff] [blame] | 111 | x3proto_irq_domain = irq_domain_add_linear(NULL, NR_BASEBOARD_GPIOS, |
| 112 | &x3proto_gpio_irq_ops, NULL); |
| 113 | if (unlikely(!x3proto_irq_domain)) |
| 114 | goto err_irq; |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 115 | |
| 116 | pr_info("registering '%s' support, handling GPIOs %u -> %u, " |
| 117 | "bound to IRQ %u\n", |
| 118 | x3proto_gpio_chip.label, x3proto_gpio_chip.base, |
| 119 | x3proto_gpio_chip.base + x3proto_gpio_chip.ngpio, |
| 120 | ilsel); |
| 121 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 122 | irq_set_chained_handler(ilsel, x3proto_gpio_irq_handler); |
| 123 | irq_set_irq_wake(ilsel, 1); |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | |
| 127 | err_irq: |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 128 | gpiochip_remove(&x3proto_gpio_chip); |
| 129 | ret = 0; |
Paul Mundt | 5505911 | 2010-10-04 02:59:29 +0900 | [diff] [blame] | 130 | err_gpio: |
| 131 | synchronize_irq(ilsel); |
| 132 | |
| 133 | ilsel_disable(ILSEL_KEY); |
| 134 | |
| 135 | return ret; |
| 136 | } |