18b37eb74SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0 2119f5e44SMagnus Damm /* 3119f5e44SMagnus Damm * Renesas R-Car GPIO Support 4119f5e44SMagnus Damm * 51fd2b49dSHisashi Nakamura * Copyright (C) 2014 Renesas Electronics Corporation 6119f5e44SMagnus Damm * Copyright (C) 2013 Magnus Damm 7119f5e44SMagnus Damm */ 8119f5e44SMagnus Damm 9119f5e44SMagnus Damm #include <linux/err.h> 104b1d8007SLinus Walleij #include <linux/gpio/driver.h> 11119f5e44SMagnus Damm #include <linux/init.h> 12119f5e44SMagnus Damm #include <linux/interrupt.h> 13119f5e44SMagnus Damm #include <linux/io.h> 14119f5e44SMagnus Damm #include <linux/ioport.h> 15119f5e44SMagnus Damm #include <linux/irq.h> 16119f5e44SMagnus Damm #include <linux/module.h> 17bd0bf468SSachin Kamat #include <linux/of.h> 18f9f2a6feSGeert Uytterhoeven #include <linux/of_device.h> 19dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h> 20119f5e44SMagnus Damm #include <linux/platform_device.h> 21df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h> 22119f5e44SMagnus Damm #include <linux/spinlock.h> 23119f5e44SMagnus Damm #include <linux/slab.h> 24119f5e44SMagnus Damm 2551750fb1SHien Dang struct gpio_rcar_bank_info { 2651750fb1SHien Dang u32 iointsel; 2751750fb1SHien Dang u32 inoutsel; 2851750fb1SHien Dang u32 outdt; 2951750fb1SHien Dang u32 posneg; 3051750fb1SHien Dang u32 edglevel; 3151750fb1SHien Dang u32 bothedge; 3251750fb1SHien Dang u32 intmsk; 3351750fb1SHien Dang }; 3451750fb1SHien Dang 35119f5e44SMagnus Damm struct gpio_rcar_priv { 36119f5e44SMagnus Damm void __iomem *base; 37119f5e44SMagnus Damm spinlock_t lock; 38a53f7953SVladimir Zapolskiy struct device *dev; 39119f5e44SMagnus Damm struct gpio_chip gpio_chip; 40119f5e44SMagnus Damm struct irq_chip irq_chip; 418b092be9SGeert Uytterhoeven unsigned int irq_parent; 429ac79ba9SGeert Uytterhoeven atomic_t wakeup_path; 433ae4f3aaSVladimir Zapolskiy bool has_outdtsel; 448b092be9SGeert Uytterhoeven bool has_both_edge_trigger; 4551750fb1SHien Dang struct gpio_rcar_bank_info bank_info; 46119f5e44SMagnus Damm }; 47119f5e44SMagnus Damm 483dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */ 493dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04 /* General Input/Output Switching Register */ 503dc1e685SGeert Uytterhoeven #define OUTDT 0x08 /* General Output Register */ 513dc1e685SGeert Uytterhoeven #define INDT 0x0c /* General Input Register */ 523dc1e685SGeert Uytterhoeven #define INTDT 0x10 /* Interrupt Display Register */ 533dc1e685SGeert Uytterhoeven #define INTCLR 0x14 /* Interrupt Clear Register */ 543dc1e685SGeert Uytterhoeven #define INTMSK 0x18 /* Interrupt Mask Register */ 553dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c /* Interrupt Mask Clear Register */ 563dc1e685SGeert Uytterhoeven #define POSNEG 0x20 /* Positive/Negative Logic Select Register */ 573dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24 /* Edge/level Select Register */ 583dc1e685SGeert Uytterhoeven #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */ 593ae4f3aaSVladimir Zapolskiy #define OUTDTSEL 0x40 /* Output Data Select Register */ 603dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */ 61119f5e44SMagnus Damm 62159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK 32 63159f8a02SLaurent Pinchart 64119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs) 65119f5e44SMagnus Damm { 66119f5e44SMagnus Damm return ioread32(p->base + offs); 67119f5e44SMagnus Damm } 68119f5e44SMagnus Damm 69119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs, 70119f5e44SMagnus Damm u32 value) 71119f5e44SMagnus Damm { 72119f5e44SMagnus Damm iowrite32(value, p->base + offs); 73119f5e44SMagnus Damm } 74119f5e44SMagnus Damm 75119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs, 76119f5e44SMagnus Damm int bit, bool value) 77119f5e44SMagnus Damm { 78119f5e44SMagnus Damm u32 tmp = gpio_rcar_read(p, offs); 79119f5e44SMagnus Damm 80119f5e44SMagnus Damm if (value) 81119f5e44SMagnus Damm tmp |= BIT(bit); 82119f5e44SMagnus Damm else 83119f5e44SMagnus Damm tmp &= ~BIT(bit); 84119f5e44SMagnus Damm 85119f5e44SMagnus Damm gpio_rcar_write(p, offs, tmp); 86119f5e44SMagnus Damm } 87119f5e44SMagnus Damm 88119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d) 89119f5e44SMagnus Damm { 90c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 91c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 92119f5e44SMagnus Damm 93119f5e44SMagnus Damm gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d))); 94119f5e44SMagnus Damm } 95119f5e44SMagnus Damm 96119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d) 97119f5e44SMagnus Damm { 98c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 99c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 100119f5e44SMagnus Damm 101119f5e44SMagnus Damm gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d))); 102119f5e44SMagnus Damm } 103119f5e44SMagnus Damm 104119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p, 105119f5e44SMagnus Damm unsigned int hwirq, 106119f5e44SMagnus Damm bool active_high_rising_edge, 1077e1092b5SSimon Horman bool level_trigger, 1087e1092b5SSimon Horman bool both) 109119f5e44SMagnus Damm { 110119f5e44SMagnus Damm unsigned long flags; 111119f5e44SMagnus Damm 112119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 113119f5e44SMagnus Damm * "Setting Edge-Sensitive Interrupt Input Mode" and 114119f5e44SMagnus Damm * "Setting Level-Sensitive Interrupt Input Mode" 115119f5e44SMagnus Damm */ 116119f5e44SMagnus Damm 117119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 118119f5e44SMagnus Damm 119b36368f6SAshish Chavan /* Configure positive or negative logic in POSNEG */ 120119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge); 121119f5e44SMagnus Damm 122119f5e44SMagnus Damm /* Configure edge or level trigger in EDGLEVEL */ 123119f5e44SMagnus Damm gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger); 124119f5e44SMagnus Damm 1257e1092b5SSimon Horman /* Select one edge or both edges in BOTHEDGE */ 1268b092be9SGeert Uytterhoeven if (p->has_both_edge_trigger) 1277e1092b5SSimon Horman gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both); 1287e1092b5SSimon Horman 129119f5e44SMagnus Damm /* Select "Interrupt Input Mode" in IOINTSEL */ 130119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true); 131119f5e44SMagnus Damm 132119f5e44SMagnus Damm /* Write INTCLR in case of edge trigger */ 133119f5e44SMagnus Damm if (!level_trigger) 134119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(hwirq)); 135119f5e44SMagnus Damm 136119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 137119f5e44SMagnus Damm } 138119f5e44SMagnus Damm 139119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type) 140119f5e44SMagnus Damm { 141c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 142c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 143119f5e44SMagnus Damm unsigned int hwirq = irqd_to_hwirq(d); 144119f5e44SMagnus Damm 145a53f7953SVladimir Zapolskiy dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type); 146119f5e44SMagnus Damm 147119f5e44SMagnus Damm switch (type & IRQ_TYPE_SENSE_MASK) { 148119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_HIGH: 1497e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true, 1507e1092b5SSimon Horman false); 151119f5e44SMagnus Damm break; 152119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_LOW: 1537e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true, 1547e1092b5SSimon Horman false); 155119f5e44SMagnus Damm break; 156119f5e44SMagnus Damm case IRQ_TYPE_EDGE_RISING: 1577e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1587e1092b5SSimon Horman false); 159119f5e44SMagnus Damm break; 160119f5e44SMagnus Damm case IRQ_TYPE_EDGE_FALLING: 1617e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false, 1627e1092b5SSimon Horman false); 1637e1092b5SSimon Horman break; 1647e1092b5SSimon Horman case IRQ_TYPE_EDGE_BOTH: 1658b092be9SGeert Uytterhoeven if (!p->has_both_edge_trigger) 1667e1092b5SSimon Horman return -EINVAL; 1677e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1687e1092b5SSimon Horman true); 169119f5e44SMagnus Damm break; 170119f5e44SMagnus Damm default: 171119f5e44SMagnus Damm return -EINVAL; 172119f5e44SMagnus Damm } 173119f5e44SMagnus Damm return 0; 174119f5e44SMagnus Damm } 175119f5e44SMagnus Damm 176ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on) 177ab82fa7dSGeert Uytterhoeven { 178ab82fa7dSGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 179c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 180501ef0f9SGeert Uytterhoeven int error; 181ab82fa7dSGeert Uytterhoeven 182501ef0f9SGeert Uytterhoeven if (p->irq_parent) { 183501ef0f9SGeert Uytterhoeven error = irq_set_irq_wake(p->irq_parent, on); 184501ef0f9SGeert Uytterhoeven if (error) { 185a53f7953SVladimir Zapolskiy dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n", 186501ef0f9SGeert Uytterhoeven p->irq_parent); 187501ef0f9SGeert Uytterhoeven p->irq_parent = 0; 188501ef0f9SGeert Uytterhoeven } 189501ef0f9SGeert Uytterhoeven } 190ab82fa7dSGeert Uytterhoeven 191ab82fa7dSGeert Uytterhoeven if (on) 1929ac79ba9SGeert Uytterhoeven atomic_inc(&p->wakeup_path); 193ab82fa7dSGeert Uytterhoeven else 1949ac79ba9SGeert Uytterhoeven atomic_dec(&p->wakeup_path); 195ab82fa7dSGeert Uytterhoeven 196ab82fa7dSGeert Uytterhoeven return 0; 197ab82fa7dSGeert Uytterhoeven } 198ab82fa7dSGeert Uytterhoeven 199119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) 200119f5e44SMagnus Damm { 201119f5e44SMagnus Damm struct gpio_rcar_priv *p = dev_id; 202119f5e44SMagnus Damm u32 pending; 203119f5e44SMagnus Damm unsigned int offset, irqs_handled = 0; 204119f5e44SMagnus Damm 2058808b64dSValentine Barshak while ((pending = gpio_rcar_read(p, INTDT) & 2068808b64dSValentine Barshak gpio_rcar_read(p, INTMSK))) { 207119f5e44SMagnus Damm offset = __ffs(pending); 208119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(offset)); 209f0fbe7bcSThierry Reding generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain, 210c7f3c5d3SGeert Uytterhoeven offset)); 211119f5e44SMagnus Damm irqs_handled++; 212119f5e44SMagnus Damm } 213119f5e44SMagnus Damm 214119f5e44SMagnus Damm return irqs_handled ? IRQ_HANDLED : IRQ_NONE; 215119f5e44SMagnus Damm } 216119f5e44SMagnus Damm 217119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip, 218119f5e44SMagnus Damm unsigned int gpio, 219119f5e44SMagnus Damm bool output) 220119f5e44SMagnus Damm { 221c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(chip); 222119f5e44SMagnus Damm unsigned long flags; 223119f5e44SMagnus Damm 224119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 225119f5e44SMagnus Damm * "Setting General Output Mode" and 226119f5e44SMagnus Damm * "Setting General Input Mode" 227119f5e44SMagnus Damm */ 228119f5e44SMagnus Damm 229119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 230119f5e44SMagnus Damm 231b36368f6SAshish Chavan /* Configure positive logic in POSNEG */ 232119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, gpio, false); 233119f5e44SMagnus Damm 234119f5e44SMagnus Damm /* Select "General Input/Output Mode" in IOINTSEL */ 235119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, gpio, false); 236119f5e44SMagnus Damm 237119f5e44SMagnus Damm /* Select Input Mode or Output Mode in INOUTSEL */ 238119f5e44SMagnus Damm gpio_rcar_modify_bit(p, INOUTSEL, gpio, output); 239119f5e44SMagnus Damm 2403ae4f3aaSVladimir Zapolskiy /* Select General Output Register to output data in OUTDTSEL */ 2413ae4f3aaSVladimir Zapolskiy if (p->has_outdtsel && output) 2423ae4f3aaSVladimir Zapolskiy gpio_rcar_modify_bit(p, OUTDTSEL, gpio, false); 2433ae4f3aaSVladimir Zapolskiy 244119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 245119f5e44SMagnus Damm } 246119f5e44SMagnus Damm 247dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset) 248dc3465a9SLaurent Pinchart { 2492d65472bSGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 2502d65472bSGeert Uytterhoeven int error; 2512d65472bSGeert Uytterhoeven 252a53f7953SVladimir Zapolskiy error = pm_runtime_get_sync(p->dev); 2532d65472bSGeert Uytterhoeven if (error < 0) 2542d65472bSGeert Uytterhoeven return error; 2552d65472bSGeert Uytterhoeven 256a9a1d2a7SLinus Walleij error = pinctrl_gpio_request(chip->base + offset); 2572d65472bSGeert Uytterhoeven if (error) 258a53f7953SVladimir Zapolskiy pm_runtime_put(p->dev); 2592d65472bSGeert Uytterhoeven 2602d65472bSGeert Uytterhoeven return error; 261dc3465a9SLaurent Pinchart } 262dc3465a9SLaurent Pinchart 263dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset) 264dc3465a9SLaurent Pinchart { 2652d65472bSGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 2662d65472bSGeert Uytterhoeven 267a9a1d2a7SLinus Walleij pinctrl_gpio_free(chip->base + offset); 268dc3465a9SLaurent Pinchart 269ce0e2c60SLinus Walleij /* 270ce0e2c60SLinus Walleij * Set the GPIO as an input to ensure that the next GPIO request won't 271dc3465a9SLaurent Pinchart * drive the GPIO pin as an output. 272dc3465a9SLaurent Pinchart */ 273dc3465a9SLaurent Pinchart gpio_rcar_config_general_input_output_mode(chip, offset, false); 2742d65472bSGeert Uytterhoeven 275a53f7953SVladimir Zapolskiy pm_runtime_put(p->dev); 276dc3465a9SLaurent Pinchart } 277dc3465a9SLaurent Pinchart 278ad817297SGeert Uytterhoeven static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset) 279ad817297SGeert Uytterhoeven { 280ad817297SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 281ad817297SGeert Uytterhoeven 282e42615ecSMatti Vaittinen if (gpio_rcar_read(p, INOUTSEL) & BIT(offset)) 283e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT; 284e42615ecSMatti Vaittinen 285e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN; 286ad817297SGeert Uytterhoeven } 287ad817297SGeert Uytterhoeven 288119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset) 289119f5e44SMagnus Damm { 290119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, false); 291119f5e44SMagnus Damm return 0; 292119f5e44SMagnus Damm } 293119f5e44SMagnus Damm 294119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset) 295119f5e44SMagnus Damm { 296ae9550f6SMagnus Damm u32 bit = BIT(offset); 297ae9550f6SMagnus Damm 298ae9550f6SMagnus Damm /* testing on r8a7790 shows that INDT does not show correct pin state 299ae9550f6SMagnus Damm * when configured as output, so use OUTDT in case of output pins */ 300c7b6f457SLinus Walleij if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit) 301c7b6f457SLinus Walleij return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit); 302ae9550f6SMagnus Damm else 303c7b6f457SLinus Walleij return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit); 304119f5e44SMagnus Damm } 305119f5e44SMagnus Damm 306119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value) 307119f5e44SMagnus Damm { 308c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(chip); 309119f5e44SMagnus Damm unsigned long flags; 310119f5e44SMagnus Damm 311119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 312119f5e44SMagnus Damm gpio_rcar_modify_bit(p, OUTDT, offset, value); 313119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 314119f5e44SMagnus Damm } 315119f5e44SMagnus Damm 316dbb763b8SGeert Uytterhoeven static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask, 317dbb763b8SGeert Uytterhoeven unsigned long *bits) 318dbb763b8SGeert Uytterhoeven { 319dbb763b8SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 320dbb763b8SGeert Uytterhoeven unsigned long flags; 321dbb763b8SGeert Uytterhoeven u32 val, bankmask; 322dbb763b8SGeert Uytterhoeven 323dbb763b8SGeert Uytterhoeven bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0); 324496069b8SBiju Das if (chip->valid_mask) 325496069b8SBiju Das bankmask &= chip->valid_mask[0]; 326496069b8SBiju Das 327dbb763b8SGeert Uytterhoeven if (!bankmask) 328dbb763b8SGeert Uytterhoeven return; 329dbb763b8SGeert Uytterhoeven 330dbb763b8SGeert Uytterhoeven spin_lock_irqsave(&p->lock, flags); 331dbb763b8SGeert Uytterhoeven val = gpio_rcar_read(p, OUTDT); 332dbb763b8SGeert Uytterhoeven val &= ~bankmask; 333dbb763b8SGeert Uytterhoeven val |= (bankmask & bits[0]); 334dbb763b8SGeert Uytterhoeven gpio_rcar_write(p, OUTDT, val); 335dbb763b8SGeert Uytterhoeven spin_unlock_irqrestore(&p->lock, flags); 336dbb763b8SGeert Uytterhoeven } 337dbb763b8SGeert Uytterhoeven 338119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset, 339119f5e44SMagnus Damm int value) 340119f5e44SMagnus Damm { 341119f5e44SMagnus Damm /* write GPIO value to output before selecting output mode of pin */ 342119f5e44SMagnus Damm gpio_rcar_set(chip, offset, value); 343119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, true); 344119f5e44SMagnus Damm return 0; 345119f5e44SMagnus Damm } 346119f5e44SMagnus Damm 347850dfe17SLaurent Pinchart struct gpio_rcar_info { 3483ae4f3aaSVladimir Zapolskiy bool has_outdtsel; 349850dfe17SLaurent Pinchart bool has_both_edge_trigger; 350850dfe17SLaurent Pinchart }; 351850dfe17SLaurent Pinchart 3521fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = { 3533ae4f3aaSVladimir Zapolskiy .has_outdtsel = false, 3541fd2b49dSHisashi Nakamura .has_both_edge_trigger = false, 3551fd2b49dSHisashi Nakamura }; 3561fd2b49dSHisashi Nakamura 3571fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = { 3583ae4f3aaSVladimir Zapolskiy .has_outdtsel = true, 3591fd2b49dSHisashi Nakamura .has_both_edge_trigger = true, 3601fd2b49dSHisashi Nakamura }; 3611fd2b49dSHisashi Nakamura 362850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = { 363850dfe17SLaurent Pinchart { 36485bb4646SBiju Das .compatible = "renesas,gpio-r8a7743", 36585bb4646SBiju Das /* RZ/G1 GPIO is identical to R-Car Gen2. */ 36685bb4646SBiju Das .data = &gpio_rcar_info_gen2, 36785bb4646SBiju Das }, { 368850dfe17SLaurent Pinchart .compatible = "renesas,gpio-r8a7790", 3691fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 370850dfe17SLaurent Pinchart }, { 371850dfe17SLaurent Pinchart .compatible = "renesas,gpio-r8a7791", 3721fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 3731fd2b49dSHisashi Nakamura }, { 374e79c5830SSergei Shtylyov .compatible = "renesas,gpio-r8a7792", 375e79c5830SSergei Shtylyov .data = &gpio_rcar_info_gen2, 376e79c5830SSergei Shtylyov }, { 3771fd2b49dSHisashi Nakamura .compatible = "renesas,gpio-r8a7793", 3781fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 3791fd2b49dSHisashi Nakamura }, { 3801fd2b49dSHisashi Nakamura .compatible = "renesas,gpio-r8a7794", 3811fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 382850dfe17SLaurent Pinchart }, { 3838cd14702SUlrich Hecht .compatible = "renesas,gpio-r8a7795", 3848cd14702SUlrich Hecht /* Gen3 GPIO is identical to Gen2. */ 3858cd14702SUlrich Hecht .data = &gpio_rcar_info_gen2, 3868cd14702SUlrich Hecht }, { 3875d2f1d6eSSimon Horman .compatible = "renesas,gpio-r8a7796", 3885d2f1d6eSSimon Horman /* Gen3 GPIO is identical to Gen2. */ 3895d2f1d6eSSimon Horman .data = &gpio_rcar_info_gen2, 3905d2f1d6eSSimon Horman }, { 391dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen1-gpio", 392dbd1dad2SSimon Horman .data = &gpio_rcar_info_gen1, 393dbd1dad2SSimon Horman }, { 394dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen2-gpio", 395dbd1dad2SSimon Horman .data = &gpio_rcar_info_gen2, 396dbd1dad2SSimon Horman }, { 397dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen3-gpio", 398dbd1dad2SSimon Horman /* Gen3 GPIO is identical to Gen2. */ 399dbd1dad2SSimon Horman .data = &gpio_rcar_info_gen2, 400dbd1dad2SSimon Horman }, { 401850dfe17SLaurent Pinchart .compatible = "renesas,gpio-rcar", 4021fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen1, 403850dfe17SLaurent Pinchart }, { 404850dfe17SLaurent Pinchart /* Terminator */ 405850dfe17SLaurent Pinchart }, 406850dfe17SLaurent Pinchart }; 407850dfe17SLaurent Pinchart 408850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); 409850dfe17SLaurent Pinchart 4108b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins) 411159f8a02SLaurent Pinchart { 412a53f7953SVladimir Zapolskiy struct device_node *np = p->dev->of_node; 413850dfe17SLaurent Pinchart const struct gpio_rcar_info *info; 4148b092be9SGeert Uytterhoeven struct of_phandle_args args; 4158b092be9SGeert Uytterhoeven int ret; 416850dfe17SLaurent Pinchart 417a53f7953SVladimir Zapolskiy info = of_device_get_match_data(p->dev); 4183ae4f3aaSVladimir Zapolskiy p->has_outdtsel = info->has_outdtsel; 4193ae4f3aaSVladimir Zapolskiy p->has_both_edge_trigger = info->has_both_edge_trigger; 420850dfe17SLaurent Pinchart 4218b092be9SGeert Uytterhoeven ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args); 4228b092be9SGeert Uytterhoeven *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK; 423159f8a02SLaurent Pinchart 4248b092be9SGeert Uytterhoeven if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) { 425a53f7953SVladimir Zapolskiy dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n", 426a53f7953SVladimir Zapolskiy *npins, RCAR_MAX_GPIO_PER_BANK); 4278b092be9SGeert Uytterhoeven *npins = RCAR_MAX_GPIO_PER_BANK; 428159f8a02SLaurent Pinchart } 429850dfe17SLaurent Pinchart 430850dfe17SLaurent Pinchart return 0; 431159f8a02SLaurent Pinchart } 432159f8a02SLaurent Pinchart 433119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev) 434119f5e44SMagnus Damm { 435119f5e44SMagnus Damm struct gpio_rcar_priv *p; 436ecbf7c2eSEnrico Weigelt, metux IT consult struct resource *irq; 437119f5e44SMagnus Damm struct gpio_chip *gpio_chip; 438119f5e44SMagnus Damm struct irq_chip *irq_chip; 439b22978fcSGeert Uytterhoeven struct device *dev = &pdev->dev; 440b22978fcSGeert Uytterhoeven const char *name = dev_name(dev); 4418b092be9SGeert Uytterhoeven unsigned int npins; 442119f5e44SMagnus Damm int ret; 443119f5e44SMagnus Damm 444b22978fcSGeert Uytterhoeven p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); 4457d82bf34SGeert Uytterhoeven if (!p) 4467d82bf34SGeert Uytterhoeven return -ENOMEM; 447119f5e44SMagnus Damm 448a53f7953SVladimir Zapolskiy p->dev = dev; 449119f5e44SMagnus Damm spin_lock_init(&p->lock); 450119f5e44SMagnus Damm 4518b092be9SGeert Uytterhoeven /* Get device configuration from DT node */ 4528b092be9SGeert Uytterhoeven ret = gpio_rcar_parse_dt(p, &npins); 453850dfe17SLaurent Pinchart if (ret < 0) 454850dfe17SLaurent Pinchart return ret; 455159f8a02SLaurent Pinchart 456159f8a02SLaurent Pinchart platform_set_drvdata(pdev, p); 457159f8a02SLaurent Pinchart 458df0c6c80SGeert Uytterhoeven pm_runtime_enable(dev); 459df0c6c80SGeert Uytterhoeven 460119f5e44SMagnus Damm irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 4615a24d4b6SSergei Shtylyov if (!irq) { 4625a24d4b6SSergei Shtylyov dev_err(dev, "missing IRQ\n"); 463119f5e44SMagnus Damm ret = -EINVAL; 464119f5e44SMagnus Damm goto err0; 465119f5e44SMagnus Damm } 466119f5e44SMagnus Damm 467ecbf7c2eSEnrico Weigelt, metux IT consult p->base = devm_platform_ioremap_resource(pdev, 0); 4685a24d4b6SSergei Shtylyov if (IS_ERR(p->base)) { 4695a24d4b6SSergei Shtylyov ret = PTR_ERR(p->base); 470119f5e44SMagnus Damm goto err0; 471119f5e44SMagnus Damm } 472119f5e44SMagnus Damm 473119f5e44SMagnus Damm gpio_chip = &p->gpio_chip; 474dc3465a9SLaurent Pinchart gpio_chip->request = gpio_rcar_request; 475dc3465a9SLaurent Pinchart gpio_chip->free = gpio_rcar_free; 476ad817297SGeert Uytterhoeven gpio_chip->get_direction = gpio_rcar_get_direction; 477119f5e44SMagnus Damm gpio_chip->direction_input = gpio_rcar_direction_input; 478119f5e44SMagnus Damm gpio_chip->get = gpio_rcar_get; 479119f5e44SMagnus Damm gpio_chip->direction_output = gpio_rcar_direction_output; 480119f5e44SMagnus Damm gpio_chip->set = gpio_rcar_set; 481dbb763b8SGeert Uytterhoeven gpio_chip->set_multiple = gpio_rcar_set_multiple; 482119f5e44SMagnus Damm gpio_chip->label = name; 48358383c78SLinus Walleij gpio_chip->parent = dev; 484119f5e44SMagnus Damm gpio_chip->owner = THIS_MODULE; 4858b092be9SGeert Uytterhoeven gpio_chip->base = -1; 4868b092be9SGeert Uytterhoeven gpio_chip->ngpio = npins; 487119f5e44SMagnus Damm 488119f5e44SMagnus Damm irq_chip = &p->irq_chip; 489f932a686SGeert Uytterhoeven irq_chip->name = "gpio-rcar"; 49047bd38a3SNiklas Söderlund irq_chip->parent_device = dev; 491119f5e44SMagnus Damm irq_chip->irq_mask = gpio_rcar_irq_disable; 492119f5e44SMagnus Damm irq_chip->irq_unmask = gpio_rcar_irq_enable; 493119f5e44SMagnus Damm irq_chip->irq_set_type = gpio_rcar_irq_set_type; 494ab82fa7dSGeert Uytterhoeven irq_chip->irq_set_wake = gpio_rcar_irq_set_wake; 495ab82fa7dSGeert Uytterhoeven irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND; 496119f5e44SMagnus Damm 497c7b6f457SLinus Walleij ret = gpiochip_add_data(gpio_chip, p); 498c7f3c5d3SGeert Uytterhoeven if (ret) { 499c7f3c5d3SGeert Uytterhoeven dev_err(dev, "failed to add GPIO controller\n"); 5000c8aab8eSDan Carpenter goto err0; 501119f5e44SMagnus Damm } 502119f5e44SMagnus Damm 5038b092be9SGeert Uytterhoeven ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq, 5048b092be9SGeert Uytterhoeven IRQ_TYPE_NONE); 505c7f3c5d3SGeert Uytterhoeven if (ret) { 506c7f3c5d3SGeert Uytterhoeven dev_err(dev, "cannot add irqchip\n"); 507c7f3c5d3SGeert Uytterhoeven goto err1; 508c7f3c5d3SGeert Uytterhoeven } 509c7f3c5d3SGeert Uytterhoeven 510ab82fa7dSGeert Uytterhoeven p->irq_parent = irq->start; 511b22978fcSGeert Uytterhoeven if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler, 512b22978fcSGeert Uytterhoeven IRQF_SHARED, name, p)) { 513b22978fcSGeert Uytterhoeven dev_err(dev, "failed to request IRQ\n"); 514119f5e44SMagnus Damm ret = -ENOENT; 515119f5e44SMagnus Damm goto err1; 516119f5e44SMagnus Damm } 517119f5e44SMagnus Damm 5188b092be9SGeert Uytterhoeven dev_info(dev, "driving %d GPIOs\n", npins); 519dc3465a9SLaurent Pinchart 520119f5e44SMagnus Damm return 0; 521119f5e44SMagnus Damm 522119f5e44SMagnus Damm err1: 5234d84b9e4SGeert Uytterhoeven gpiochip_remove(gpio_chip); 524119f5e44SMagnus Damm err0: 525df0c6c80SGeert Uytterhoeven pm_runtime_disable(dev); 526119f5e44SMagnus Damm return ret; 527119f5e44SMagnus Damm } 528119f5e44SMagnus Damm 529119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev) 530119f5e44SMagnus Damm { 531119f5e44SMagnus Damm struct gpio_rcar_priv *p = platform_get_drvdata(pdev); 532119f5e44SMagnus Damm 5339f5132aeSabdoulaye berthe gpiochip_remove(&p->gpio_chip); 534119f5e44SMagnus Damm 535df0c6c80SGeert Uytterhoeven pm_runtime_disable(&pdev->dev); 536119f5e44SMagnus Damm return 0; 537119f5e44SMagnus Damm } 538119f5e44SMagnus Damm 53951750fb1SHien Dang #ifdef CONFIG_PM_SLEEP 54051750fb1SHien Dang static int gpio_rcar_suspend(struct device *dev) 54151750fb1SHien Dang { 54251750fb1SHien Dang struct gpio_rcar_priv *p = dev_get_drvdata(dev); 54351750fb1SHien Dang 54451750fb1SHien Dang p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL); 54551750fb1SHien Dang p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL); 54651750fb1SHien Dang p->bank_info.outdt = gpio_rcar_read(p, OUTDT); 54751750fb1SHien Dang p->bank_info.intmsk = gpio_rcar_read(p, INTMSK); 54851750fb1SHien Dang p->bank_info.posneg = gpio_rcar_read(p, POSNEG); 54951750fb1SHien Dang p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL); 55051750fb1SHien Dang if (p->has_both_edge_trigger) 55151750fb1SHien Dang p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE); 55251750fb1SHien Dang 5539ac79ba9SGeert Uytterhoeven if (atomic_read(&p->wakeup_path)) 5549ac79ba9SGeert Uytterhoeven device_set_wakeup_path(dev); 5559ac79ba9SGeert Uytterhoeven 55651750fb1SHien Dang return 0; 55751750fb1SHien Dang } 55851750fb1SHien Dang 55951750fb1SHien Dang static int gpio_rcar_resume(struct device *dev) 56051750fb1SHien Dang { 56151750fb1SHien Dang struct gpio_rcar_priv *p = dev_get_drvdata(dev); 56251750fb1SHien Dang unsigned int offset; 56351750fb1SHien Dang u32 mask; 56451750fb1SHien Dang 56551750fb1SHien Dang for (offset = 0; offset < p->gpio_chip.ngpio; offset++) { 566496069b8SBiju Das if (!gpiochip_line_is_valid(&p->gpio_chip, offset)) 567496069b8SBiju Das continue; 568496069b8SBiju Das 56951750fb1SHien Dang mask = BIT(offset); 57051750fb1SHien Dang /* I/O pin */ 57151750fb1SHien Dang if (!(p->bank_info.iointsel & mask)) { 57251750fb1SHien Dang if (p->bank_info.inoutsel & mask) 57351750fb1SHien Dang gpio_rcar_direction_output( 57451750fb1SHien Dang &p->gpio_chip, offset, 57551750fb1SHien Dang !!(p->bank_info.outdt & mask)); 57651750fb1SHien Dang else 57751750fb1SHien Dang gpio_rcar_direction_input(&p->gpio_chip, 57851750fb1SHien Dang offset); 57951750fb1SHien Dang } else { 58051750fb1SHien Dang /* Interrupt pin */ 58151750fb1SHien Dang gpio_rcar_config_interrupt_input_mode( 58251750fb1SHien Dang p, 58351750fb1SHien Dang offset, 58451750fb1SHien Dang !(p->bank_info.posneg & mask), 58551750fb1SHien Dang !(p->bank_info.edglevel & mask), 58651750fb1SHien Dang !!(p->bank_info.bothedge & mask)); 58751750fb1SHien Dang 58851750fb1SHien Dang if (p->bank_info.intmsk & mask) 58951750fb1SHien Dang gpio_rcar_write(p, MSKCLR, mask); 59051750fb1SHien Dang } 59151750fb1SHien Dang } 59251750fb1SHien Dang 59351750fb1SHien Dang return 0; 59451750fb1SHien Dang } 59551750fb1SHien Dang #endif /* CONFIG_PM_SLEEP*/ 59651750fb1SHien Dang 59751750fb1SHien Dang static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume); 59851750fb1SHien Dang 599119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = { 600119f5e44SMagnus Damm .probe = gpio_rcar_probe, 601119f5e44SMagnus Damm .remove = gpio_rcar_remove, 602119f5e44SMagnus Damm .driver = { 603119f5e44SMagnus Damm .name = "gpio_rcar", 60451750fb1SHien Dang .pm = &gpio_rcar_pm_ops, 605159f8a02SLaurent Pinchart .of_match_table = of_match_ptr(gpio_rcar_of_table), 606119f5e44SMagnus Damm } 607119f5e44SMagnus Damm }; 608119f5e44SMagnus Damm 609119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver); 610119f5e44SMagnus Damm 611119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm"); 612119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); 613119f5e44SMagnus Damm MODULE_LICENSE("GPL v2"); 614