Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Renesas R-Car GPIO Support |
| 3 | * |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 4 | * Copyright (C) 2014 Renesas Electronics Corporation |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 5 | * Copyright (C) 2013 Magnus Damm |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/ioport.h> |
| 23 | #include <linux/irq.h> |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 24 | #include <linux/module.h> |
Sachin Kamat | bd0bf46 | 2013-10-16 15:35:02 +0530 | [diff] [blame] | 25 | #include <linux/of.h> |
Geert Uytterhoeven | f9f2a6f | 2017-10-04 14:16:16 +0200 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 27 | #include <linux/pinctrl/consumer.h> |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
Geert Uytterhoeven | df0c6c8 | 2014-04-14 20:33:13 +0200 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/slab.h> |
| 32 | |
| 33 | struct gpio_rcar_priv { |
| 34 | void __iomem *base; |
| 35 | spinlock_t lock; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 36 | struct platform_device *pdev; |
| 37 | struct gpio_chip gpio_chip; |
| 38 | struct irq_chip irq_chip; |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 39 | unsigned int irq_parent; |
Geert Uytterhoeven | 9ac79ba | 2018-02-12 14:55:13 +0100 | [diff] [blame^] | 40 | atomic_t wakeup_path; |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 41 | bool has_both_edge_trigger; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
Geert Uytterhoeven | 3dc1e68 | 2015-03-18 19:41:08 +0100 | [diff] [blame] | 44 | #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */ |
| 45 | #define INOUTSEL 0x04 /* General Input/Output Switching Register */ |
| 46 | #define OUTDT 0x08 /* General Output Register */ |
| 47 | #define INDT 0x0c /* General Input Register */ |
| 48 | #define INTDT 0x10 /* Interrupt Display Register */ |
| 49 | #define INTCLR 0x14 /* Interrupt Clear Register */ |
| 50 | #define INTMSK 0x18 /* Interrupt Mask Register */ |
| 51 | #define MSKCLR 0x1c /* Interrupt Mask Clear Register */ |
| 52 | #define POSNEG 0x20 /* Positive/Negative Logic Select Register */ |
| 53 | #define EDGLEVEL 0x24 /* Edge/level Select Register */ |
| 54 | #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */ |
| 55 | #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */ |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 56 | |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 57 | #define RCAR_MAX_GPIO_PER_BANK 32 |
| 58 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 59 | static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs) |
| 60 | { |
| 61 | return ioread32(p->base + offs); |
| 62 | } |
| 63 | |
| 64 | static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs, |
| 65 | u32 value) |
| 66 | { |
| 67 | iowrite32(value, p->base + offs); |
| 68 | } |
| 69 | |
| 70 | static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs, |
| 71 | int bit, bool value) |
| 72 | { |
| 73 | u32 tmp = gpio_rcar_read(p, offs); |
| 74 | |
| 75 | if (value) |
| 76 | tmp |= BIT(bit); |
| 77 | else |
| 78 | tmp &= ~BIT(bit); |
| 79 | |
| 80 | gpio_rcar_write(p, offs, tmp); |
| 81 | } |
| 82 | |
| 83 | static void gpio_rcar_irq_disable(struct irq_data *d) |
| 84 | { |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 85 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 86 | struct gpio_rcar_priv *p = gpiochip_get_data(gc); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 87 | |
| 88 | gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d))); |
| 89 | } |
| 90 | |
| 91 | static void gpio_rcar_irq_enable(struct irq_data *d) |
| 92 | { |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 93 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 94 | struct gpio_rcar_priv *p = gpiochip_get_data(gc); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 95 | |
| 96 | gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d))); |
| 97 | } |
| 98 | |
| 99 | static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p, |
| 100 | unsigned int hwirq, |
| 101 | bool active_high_rising_edge, |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 102 | bool level_trigger, |
| 103 | bool both) |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 104 | { |
| 105 | unsigned long flags; |
| 106 | |
| 107 | /* follow steps in the GPIO documentation for |
| 108 | * "Setting Edge-Sensitive Interrupt Input Mode" and |
| 109 | * "Setting Level-Sensitive Interrupt Input Mode" |
| 110 | */ |
| 111 | |
| 112 | spin_lock_irqsave(&p->lock, flags); |
| 113 | |
| 114 | /* Configure postive or negative logic in POSNEG */ |
| 115 | gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge); |
| 116 | |
| 117 | /* Configure edge or level trigger in EDGLEVEL */ |
| 118 | gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger); |
| 119 | |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 120 | /* Select one edge or both edges in BOTHEDGE */ |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 121 | if (p->has_both_edge_trigger) |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 122 | gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both); |
| 123 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 124 | /* Select "Interrupt Input Mode" in IOINTSEL */ |
| 125 | gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true); |
| 126 | |
| 127 | /* Write INTCLR in case of edge trigger */ |
| 128 | if (!level_trigger) |
| 129 | gpio_rcar_write(p, INTCLR, BIT(hwirq)); |
| 130 | |
| 131 | spin_unlock_irqrestore(&p->lock, flags); |
| 132 | } |
| 133 | |
| 134 | static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type) |
| 135 | { |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 136 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 137 | struct gpio_rcar_priv *p = gpiochip_get_data(gc); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 138 | unsigned int hwirq = irqd_to_hwirq(d); |
| 139 | |
| 140 | dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type); |
| 141 | |
| 142 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 143 | case IRQ_TYPE_LEVEL_HIGH: |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 144 | gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true, |
| 145 | false); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 146 | break; |
| 147 | case IRQ_TYPE_LEVEL_LOW: |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 148 | gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true, |
| 149 | false); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 150 | break; |
| 151 | case IRQ_TYPE_EDGE_RISING: |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 152 | gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, |
| 153 | false); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 154 | break; |
| 155 | case IRQ_TYPE_EDGE_FALLING: |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 156 | gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false, |
| 157 | false); |
| 158 | break; |
| 159 | case IRQ_TYPE_EDGE_BOTH: |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 160 | if (!p->has_both_edge_trigger) |
Simon Horman | 7e1092b | 2013-05-24 18:47:24 +0900 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, |
| 163 | true); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 164 | break; |
| 165 | default: |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 171 | static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on) |
| 172 | { |
| 173 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 174 | struct gpio_rcar_priv *p = gpiochip_get_data(gc); |
Geert Uytterhoeven | 501ef0f | 2015-05-21 13:21:37 +0200 | [diff] [blame] | 175 | int error; |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 176 | |
Geert Uytterhoeven | 501ef0f | 2015-05-21 13:21:37 +0200 | [diff] [blame] | 177 | if (p->irq_parent) { |
| 178 | error = irq_set_irq_wake(p->irq_parent, on); |
| 179 | if (error) { |
| 180 | dev_dbg(&p->pdev->dev, |
| 181 | "irq %u doesn't support irq_set_wake\n", |
| 182 | p->irq_parent); |
| 183 | p->irq_parent = 0; |
| 184 | } |
| 185 | } |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 186 | |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 187 | if (on) |
Geert Uytterhoeven | 9ac79ba | 2018-02-12 14:55:13 +0100 | [diff] [blame^] | 188 | atomic_inc(&p->wakeup_path); |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 189 | else |
Geert Uytterhoeven | 9ac79ba | 2018-02-12 14:55:13 +0100 | [diff] [blame^] | 190 | atomic_dec(&p->wakeup_path); |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 195 | static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) |
| 196 | { |
| 197 | struct gpio_rcar_priv *p = dev_id; |
| 198 | u32 pending; |
| 199 | unsigned int offset, irqs_handled = 0; |
| 200 | |
Valentine Barshak | 8808b64 | 2013-11-29 22:04:09 +0400 | [diff] [blame] | 201 | while ((pending = gpio_rcar_read(p, INTDT) & |
| 202 | gpio_rcar_read(p, INTMSK))) { |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 203 | offset = __ffs(pending); |
| 204 | gpio_rcar_write(p, INTCLR, BIT(offset)); |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 205 | generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain, |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 206 | offset)); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 207 | irqs_handled++; |
| 208 | } |
| 209 | |
| 210 | return irqs_handled ? IRQ_HANDLED : IRQ_NONE; |
| 211 | } |
| 212 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 213 | static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip, |
| 214 | unsigned int gpio, |
| 215 | bool output) |
| 216 | { |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 217 | struct gpio_rcar_priv *p = gpiochip_get_data(chip); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 218 | unsigned long flags; |
| 219 | |
| 220 | /* follow steps in the GPIO documentation for |
| 221 | * "Setting General Output Mode" and |
| 222 | * "Setting General Input Mode" |
| 223 | */ |
| 224 | |
| 225 | spin_lock_irqsave(&p->lock, flags); |
| 226 | |
| 227 | /* Configure postive logic in POSNEG */ |
| 228 | gpio_rcar_modify_bit(p, POSNEG, gpio, false); |
| 229 | |
| 230 | /* Select "General Input/Output Mode" in IOINTSEL */ |
| 231 | gpio_rcar_modify_bit(p, IOINTSEL, gpio, false); |
| 232 | |
| 233 | /* Select Input Mode or Output Mode in INOUTSEL */ |
| 234 | gpio_rcar_modify_bit(p, INOUTSEL, gpio, output); |
| 235 | |
| 236 | spin_unlock_irqrestore(&p->lock, flags); |
| 237 | } |
| 238 | |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 239 | static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset) |
| 240 | { |
Geert Uytterhoeven | 2d65472 | 2016-12-08 18:32:28 +0100 | [diff] [blame] | 241 | struct gpio_rcar_priv *p = gpiochip_get_data(chip); |
| 242 | int error; |
| 243 | |
| 244 | error = pm_runtime_get_sync(&p->pdev->dev); |
| 245 | if (error < 0) |
| 246 | return error; |
| 247 | |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 248 | error = pinctrl_gpio_request(chip->base + offset); |
Geert Uytterhoeven | 2d65472 | 2016-12-08 18:32:28 +0100 | [diff] [blame] | 249 | if (error) |
| 250 | pm_runtime_put(&p->pdev->dev); |
| 251 | |
| 252 | return error; |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset) |
| 256 | { |
Geert Uytterhoeven | 2d65472 | 2016-12-08 18:32:28 +0100 | [diff] [blame] | 257 | struct gpio_rcar_priv *p = gpiochip_get_data(chip); |
| 258 | |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 259 | pinctrl_gpio_free(chip->base + offset); |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 260 | |
Linus Walleij | ce0e2c6 | 2016-04-12 10:05:22 +0200 | [diff] [blame] | 261 | /* |
| 262 | * Set the GPIO as an input to ensure that the next GPIO request won't |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 263 | * drive the GPIO pin as an output. |
| 264 | */ |
| 265 | gpio_rcar_config_general_input_output_mode(chip, offset, false); |
Geert Uytterhoeven | 2d65472 | 2016-12-08 18:32:28 +0100 | [diff] [blame] | 266 | |
| 267 | pm_runtime_put(&p->pdev->dev); |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 270 | static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset) |
| 271 | { |
| 272 | gpio_rcar_config_general_input_output_mode(chip, offset, false); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset) |
| 277 | { |
Magnus Damm | ae9550f | 2013-06-17 08:41:52 +0900 | [diff] [blame] | 278 | u32 bit = BIT(offset); |
| 279 | |
| 280 | /* testing on r8a7790 shows that INDT does not show correct pin state |
| 281 | * when configured as output, so use OUTDT in case of output pins */ |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 282 | if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit) |
| 283 | return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit); |
Magnus Damm | ae9550f | 2013-06-17 08:41:52 +0900 | [diff] [blame] | 284 | else |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 285 | return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value) |
| 289 | { |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 290 | struct gpio_rcar_priv *p = gpiochip_get_data(chip); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 291 | unsigned long flags; |
| 292 | |
| 293 | spin_lock_irqsave(&p->lock, flags); |
| 294 | gpio_rcar_modify_bit(p, OUTDT, offset, value); |
| 295 | spin_unlock_irqrestore(&p->lock, flags); |
| 296 | } |
| 297 | |
Geert Uytterhoeven | dbb763b8 | 2016-03-14 16:21:44 +0100 | [diff] [blame] | 298 | static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask, |
| 299 | unsigned long *bits) |
| 300 | { |
| 301 | struct gpio_rcar_priv *p = gpiochip_get_data(chip); |
| 302 | unsigned long flags; |
| 303 | u32 val, bankmask; |
| 304 | |
| 305 | bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0); |
| 306 | if (!bankmask) |
| 307 | return; |
| 308 | |
| 309 | spin_lock_irqsave(&p->lock, flags); |
| 310 | val = gpio_rcar_read(p, OUTDT); |
| 311 | val &= ~bankmask; |
| 312 | val |= (bankmask & bits[0]); |
| 313 | gpio_rcar_write(p, OUTDT, val); |
| 314 | spin_unlock_irqrestore(&p->lock, flags); |
| 315 | } |
| 316 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 317 | static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset, |
| 318 | int value) |
| 319 | { |
| 320 | /* write GPIO value to output before selecting output mode of pin */ |
| 321 | gpio_rcar_set(chip, offset, value); |
| 322 | gpio_rcar_config_general_input_output_mode(chip, offset, true); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 326 | struct gpio_rcar_info { |
| 327 | bool has_both_edge_trigger; |
| 328 | }; |
| 329 | |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 330 | static const struct gpio_rcar_info gpio_rcar_info_gen1 = { |
| 331 | .has_both_edge_trigger = false, |
| 332 | }; |
| 333 | |
| 334 | static const struct gpio_rcar_info gpio_rcar_info_gen2 = { |
| 335 | .has_both_edge_trigger = true, |
| 336 | }; |
| 337 | |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 338 | static const struct of_device_id gpio_rcar_of_table[] = { |
| 339 | { |
Biju Das | 85bb464 | 2017-06-21 15:27:09 +0100 | [diff] [blame] | 340 | .compatible = "renesas,gpio-r8a7743", |
| 341 | /* RZ/G1 GPIO is identical to R-Car Gen2. */ |
| 342 | .data = &gpio_rcar_info_gen2, |
| 343 | }, { |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 344 | .compatible = "renesas,gpio-r8a7790", |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 345 | .data = &gpio_rcar_info_gen2, |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 346 | }, { |
| 347 | .compatible = "renesas,gpio-r8a7791", |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 348 | .data = &gpio_rcar_info_gen2, |
| 349 | }, { |
Sergei Shtylyov | e79c583 | 2016-07-07 17:11:45 +0300 | [diff] [blame] | 350 | .compatible = "renesas,gpio-r8a7792", |
| 351 | .data = &gpio_rcar_info_gen2, |
| 352 | }, { |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 353 | .compatible = "renesas,gpio-r8a7793", |
| 354 | .data = &gpio_rcar_info_gen2, |
| 355 | }, { |
| 356 | .compatible = "renesas,gpio-r8a7794", |
| 357 | .data = &gpio_rcar_info_gen2, |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 358 | }, { |
Ulrich Hecht | 8cd1470 | 2015-07-21 11:08:50 +0200 | [diff] [blame] | 359 | .compatible = "renesas,gpio-r8a7795", |
| 360 | /* Gen3 GPIO is identical to Gen2. */ |
| 361 | .data = &gpio_rcar_info_gen2, |
| 362 | }, { |
Simon Horman | 5d2f1d6 | 2016-09-06 12:35:39 +0200 | [diff] [blame] | 363 | .compatible = "renesas,gpio-r8a7796", |
| 364 | /* Gen3 GPIO is identical to Gen2. */ |
| 365 | .data = &gpio_rcar_info_gen2, |
| 366 | }, { |
Simon Horman | dbd1dad | 2017-07-11 14:38:30 +0200 | [diff] [blame] | 367 | .compatible = "renesas,rcar-gen1-gpio", |
| 368 | .data = &gpio_rcar_info_gen1, |
| 369 | }, { |
| 370 | .compatible = "renesas,rcar-gen2-gpio", |
| 371 | .data = &gpio_rcar_info_gen2, |
| 372 | }, { |
| 373 | .compatible = "renesas,rcar-gen3-gpio", |
| 374 | /* Gen3 GPIO is identical to Gen2. */ |
| 375 | .data = &gpio_rcar_info_gen2, |
| 376 | }, { |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 377 | .compatible = "renesas,gpio-rcar", |
Hisashi Nakamura | 1fd2b49 | 2014-11-07 20:54:08 +0900 | [diff] [blame] | 378 | .data = &gpio_rcar_info_gen1, |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 379 | }, { |
| 380 | /* Terminator */ |
| 381 | }, |
| 382 | }; |
| 383 | |
| 384 | MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); |
| 385 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 386 | static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins) |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 387 | { |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 388 | struct device_node *np = p->pdev->dev.of_node; |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 389 | const struct gpio_rcar_info *info; |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 390 | struct of_phandle_args args; |
| 391 | int ret; |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 392 | |
Geert Uytterhoeven | f9f2a6f | 2017-10-04 14:16:16 +0200 | [diff] [blame] | 393 | info = of_device_get_match_data(&p->pdev->dev); |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 394 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 395 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args); |
| 396 | *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK; |
| 397 | p->has_both_edge_trigger = info->has_both_edge_trigger; |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 398 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 399 | if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) { |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 400 | dev_warn(&p->pdev->dev, |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 401 | "Invalid number of gpio lines %u, using %u\n", *npins, |
| 402 | RCAR_MAX_GPIO_PER_BANK); |
| 403 | *npins = RCAR_MAX_GPIO_PER_BANK; |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 404 | } |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 405 | |
| 406 | return 0; |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 409 | static int gpio_rcar_probe(struct platform_device *pdev) |
| 410 | { |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 411 | struct gpio_rcar_priv *p; |
| 412 | struct resource *io, *irq; |
| 413 | struct gpio_chip *gpio_chip; |
| 414 | struct irq_chip *irq_chip; |
Geert Uytterhoeven | b22978f | 2014-03-27 21:47:36 +0100 | [diff] [blame] | 415 | struct device *dev = &pdev->dev; |
| 416 | const char *name = dev_name(dev); |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 417 | unsigned int npins; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 418 | int ret; |
| 419 | |
Geert Uytterhoeven | b22978f | 2014-03-27 21:47:36 +0100 | [diff] [blame] | 420 | p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); |
Geert Uytterhoeven | 7d82bf3 | 2015-01-12 11:07:58 +0100 | [diff] [blame] | 421 | if (!p) |
| 422 | return -ENOMEM; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 423 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 424 | p->pdev = pdev; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 425 | spin_lock_init(&p->lock); |
| 426 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 427 | /* Get device configuration from DT node */ |
| 428 | ret = gpio_rcar_parse_dt(p, &npins); |
Laurent Pinchart | 850dfe1 | 2013-11-29 14:48:00 +0100 | [diff] [blame] | 429 | if (ret < 0) |
| 430 | return ret; |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 431 | |
| 432 | platform_set_drvdata(pdev, p); |
| 433 | |
Geert Uytterhoeven | df0c6c8 | 2014-04-14 20:33:13 +0200 | [diff] [blame] | 434 | pm_runtime_enable(dev); |
Geert Uytterhoeven | df0c6c8 | 2014-04-14 20:33:13 +0200 | [diff] [blame] | 435 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 436 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Sergei Shtylyov | 5a24d4b | 2017-10-13 00:08:14 +0300 | [diff] [blame] | 437 | if (!irq) { |
| 438 | dev_err(dev, "missing IRQ\n"); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 439 | ret = -EINVAL; |
| 440 | goto err0; |
| 441 | } |
| 442 | |
Sergei Shtylyov | 5a24d4b | 2017-10-13 00:08:14 +0300 | [diff] [blame] | 443 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 444 | p->base = devm_ioremap_resource(dev, io); |
| 445 | if (IS_ERR(p->base)) { |
| 446 | ret = PTR_ERR(p->base); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 447 | goto err0; |
| 448 | } |
| 449 | |
| 450 | gpio_chip = &p->gpio_chip; |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 451 | gpio_chip->request = gpio_rcar_request; |
| 452 | gpio_chip->free = gpio_rcar_free; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 453 | gpio_chip->direction_input = gpio_rcar_direction_input; |
| 454 | gpio_chip->get = gpio_rcar_get; |
| 455 | gpio_chip->direction_output = gpio_rcar_direction_output; |
| 456 | gpio_chip->set = gpio_rcar_set; |
Geert Uytterhoeven | dbb763b8 | 2016-03-14 16:21:44 +0100 | [diff] [blame] | 457 | gpio_chip->set_multiple = gpio_rcar_set_multiple; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 458 | gpio_chip->label = name; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 459 | gpio_chip->parent = dev; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 460 | gpio_chip->owner = THIS_MODULE; |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 461 | gpio_chip->base = -1; |
| 462 | gpio_chip->ngpio = npins; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 463 | |
| 464 | irq_chip = &p->irq_chip; |
| 465 | irq_chip->name = name; |
Niklas Söderlund | 47bd38a | 2016-12-08 18:32:27 +0100 | [diff] [blame] | 466 | irq_chip->parent_device = dev; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 467 | irq_chip->irq_mask = gpio_rcar_irq_disable; |
| 468 | irq_chip->irq_unmask = gpio_rcar_irq_enable; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 469 | irq_chip->irq_set_type = gpio_rcar_irq_set_type; |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 470 | irq_chip->irq_set_wake = gpio_rcar_irq_set_wake; |
| 471 | irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 472 | |
Linus Walleij | c7b6f45 | 2015-12-07 14:12:45 +0100 | [diff] [blame] | 473 | ret = gpiochip_add_data(gpio_chip, p); |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 474 | if (ret) { |
| 475 | dev_err(dev, "failed to add GPIO controller\n"); |
Dan Carpenter | 0c8aab8 | 2013-11-07 10:56:51 +0300 | [diff] [blame] | 476 | goto err0; |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 477 | } |
| 478 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 479 | ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq, |
| 480 | IRQ_TYPE_NONE); |
Geert Uytterhoeven | c7f3c5d | 2015-01-12 11:07:59 +0100 | [diff] [blame] | 481 | if (ret) { |
| 482 | dev_err(dev, "cannot add irqchip\n"); |
| 483 | goto err1; |
| 484 | } |
| 485 | |
Geert Uytterhoeven | ab82fa7 | 2015-03-18 19:41:09 +0100 | [diff] [blame] | 486 | p->irq_parent = irq->start; |
Geert Uytterhoeven | b22978f | 2014-03-27 21:47:36 +0100 | [diff] [blame] | 487 | if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler, |
| 488 | IRQF_SHARED, name, p)) { |
| 489 | dev_err(dev, "failed to request IRQ\n"); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 490 | ret = -ENOENT; |
| 491 | goto err1; |
| 492 | } |
| 493 | |
Geert Uytterhoeven | 8b092be | 2015-12-04 16:33:52 +0100 | [diff] [blame] | 494 | dev_info(dev, "driving %d GPIOs\n", npins); |
Laurent Pinchart | dc3465a | 2013-03-10 03:27:00 +0100 | [diff] [blame] | 495 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 496 | return 0; |
| 497 | |
| 498 | err1: |
Geert Uytterhoeven | 4d84b9e | 2015-03-18 19:41:07 +0100 | [diff] [blame] | 499 | gpiochip_remove(gpio_chip); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 500 | err0: |
Geert Uytterhoeven | df0c6c8 | 2014-04-14 20:33:13 +0200 | [diff] [blame] | 501 | pm_runtime_disable(dev); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | static int gpio_rcar_remove(struct platform_device *pdev) |
| 506 | { |
| 507 | struct gpio_rcar_priv *p = platform_get_drvdata(pdev); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 508 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 509 | gpiochip_remove(&p->gpio_chip); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 510 | |
Geert Uytterhoeven | df0c6c8 | 2014-04-14 20:33:13 +0200 | [diff] [blame] | 511 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Geert Uytterhoeven | 9ac79ba | 2018-02-12 14:55:13 +0100 | [diff] [blame^] | 515 | static int __maybe_unused gpio_rcar_suspend(struct device *dev) |
| 516 | { |
| 517 | struct gpio_rcar_priv *p = dev_get_drvdata(dev); |
| 518 | |
| 519 | if (atomic_read(&p->wakeup_path)) |
| 520 | device_set_wakeup_path(dev); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, NULL); |
| 526 | |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 527 | static struct platform_driver gpio_rcar_device_driver = { |
| 528 | .probe = gpio_rcar_probe, |
| 529 | .remove = gpio_rcar_remove, |
| 530 | .driver = { |
| 531 | .name = "gpio_rcar", |
Geert Uytterhoeven | 9ac79ba | 2018-02-12 14:55:13 +0100 | [diff] [blame^] | 532 | .pm = &gpio_rcar_pm_ops, |
Laurent Pinchart | 159f8a0 | 2013-05-21 13:40:06 +0200 | [diff] [blame] | 533 | .of_match_table = of_match_ptr(gpio_rcar_of_table), |
Magnus Damm | 119f5e4 | 2013-03-13 20:32:13 +0900 | [diff] [blame] | 534 | } |
| 535 | }; |
| 536 | |
| 537 | module_platform_driver(gpio_rcar_device_driver); |
| 538 | |
| 539 | MODULE_AUTHOR("Magnus Damm"); |
| 540 | MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); |
| 541 | MODULE_LICENSE("GPL v2"); |