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 35208c80f1SGeert Uytterhoeven struct gpio_rcar_info { 36208c80f1SGeert Uytterhoeven bool has_outdtsel; 37208c80f1SGeert Uytterhoeven bool has_both_edge_trigger; 38ecba1eaaSGeert Uytterhoeven bool has_always_in; 3993ac0b0cSGeert Uytterhoeven bool has_inen; 40208c80f1SGeert Uytterhoeven }; 41208c80f1SGeert Uytterhoeven 42119f5e44SMagnus Damm struct gpio_rcar_priv { 43119f5e44SMagnus Damm void __iomem *base; 44119f5e44SMagnus Damm spinlock_t lock; 45a53f7953SVladimir Zapolskiy struct device *dev; 46119f5e44SMagnus Damm struct gpio_chip gpio_chip; 47119f5e44SMagnus Damm struct irq_chip irq_chip; 488b092be9SGeert Uytterhoeven unsigned int irq_parent; 499ac79ba9SGeert Uytterhoeven atomic_t wakeup_path; 50208c80f1SGeert Uytterhoeven struct gpio_rcar_info info; 5151750fb1SHien Dang struct gpio_rcar_bank_info bank_info; 52119f5e44SMagnus Damm }; 53119f5e44SMagnus Damm 543dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */ 553dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04 /* General Input/Output Switching Register */ 563dc1e685SGeert Uytterhoeven #define OUTDT 0x08 /* General Output Register */ 573dc1e685SGeert Uytterhoeven #define INDT 0x0c /* General Input Register */ 583dc1e685SGeert Uytterhoeven #define INTDT 0x10 /* Interrupt Display Register */ 593dc1e685SGeert Uytterhoeven #define INTCLR 0x14 /* Interrupt Clear Register */ 603dc1e685SGeert Uytterhoeven #define INTMSK 0x18 /* Interrupt Mask Register */ 613dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c /* Interrupt Mask Clear Register */ 623dc1e685SGeert Uytterhoeven #define POSNEG 0x20 /* Positive/Negative Logic Select Register */ 633dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24 /* Edge/level Select Register */ 643dc1e685SGeert Uytterhoeven #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */ 653ae4f3aaSVladimir Zapolskiy #define OUTDTSEL 0x40 /* Output Data Select Register */ 663dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */ 6793ac0b0cSGeert Uytterhoeven #define INEN 0x50 /* General Input Enable Register */ 68119f5e44SMagnus Damm 69159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK 32 70159f8a02SLaurent Pinchart 71119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs) 72119f5e44SMagnus Damm { 73119f5e44SMagnus Damm return ioread32(p->base + offs); 74119f5e44SMagnus Damm } 75119f5e44SMagnus Damm 76119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs, 77119f5e44SMagnus Damm u32 value) 78119f5e44SMagnus Damm { 79119f5e44SMagnus Damm iowrite32(value, p->base + offs); 80119f5e44SMagnus Damm } 81119f5e44SMagnus Damm 82119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs, 83119f5e44SMagnus Damm int bit, bool value) 84119f5e44SMagnus Damm { 85119f5e44SMagnus Damm u32 tmp = gpio_rcar_read(p, offs); 86119f5e44SMagnus Damm 87119f5e44SMagnus Damm if (value) 88119f5e44SMagnus Damm tmp |= BIT(bit); 89119f5e44SMagnus Damm else 90119f5e44SMagnus Damm tmp &= ~BIT(bit); 91119f5e44SMagnus Damm 92119f5e44SMagnus Damm gpio_rcar_write(p, offs, tmp); 93119f5e44SMagnus Damm } 94119f5e44SMagnus Damm 95119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d) 96119f5e44SMagnus Damm { 97c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 98c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 99119f5e44SMagnus Damm 100119f5e44SMagnus Damm gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d))); 101119f5e44SMagnus Damm } 102119f5e44SMagnus Damm 103119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d) 104119f5e44SMagnus Damm { 105c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 106c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 107119f5e44SMagnus Damm 108119f5e44SMagnus Damm gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d))); 109119f5e44SMagnus Damm } 110119f5e44SMagnus Damm 111119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p, 112119f5e44SMagnus Damm unsigned int hwirq, 113119f5e44SMagnus Damm bool active_high_rising_edge, 1147e1092b5SSimon Horman bool level_trigger, 1157e1092b5SSimon Horman bool both) 116119f5e44SMagnus Damm { 117119f5e44SMagnus Damm unsigned long flags; 118119f5e44SMagnus Damm 119119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 120119f5e44SMagnus Damm * "Setting Edge-Sensitive Interrupt Input Mode" and 121119f5e44SMagnus Damm * "Setting Level-Sensitive Interrupt Input Mode" 122119f5e44SMagnus Damm */ 123119f5e44SMagnus Damm 124119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 125119f5e44SMagnus Damm 126b36368f6SAshish Chavan /* Configure positive or negative logic in POSNEG */ 127119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge); 128119f5e44SMagnus Damm 129119f5e44SMagnus Damm /* Configure edge or level trigger in EDGLEVEL */ 130119f5e44SMagnus Damm gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger); 131119f5e44SMagnus Damm 1327e1092b5SSimon Horman /* Select one edge or both edges in BOTHEDGE */ 133208c80f1SGeert Uytterhoeven if (p->info.has_both_edge_trigger) 1347e1092b5SSimon Horman gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both); 1357e1092b5SSimon Horman 136119f5e44SMagnus Damm /* Select "Interrupt Input Mode" in IOINTSEL */ 137119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true); 138119f5e44SMagnus Damm 139119f5e44SMagnus Damm /* Write INTCLR in case of edge trigger */ 140119f5e44SMagnus Damm if (!level_trigger) 141119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(hwirq)); 142119f5e44SMagnus Damm 143119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 144119f5e44SMagnus Damm } 145119f5e44SMagnus Damm 146119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type) 147119f5e44SMagnus Damm { 148c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 149c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 150119f5e44SMagnus Damm unsigned int hwirq = irqd_to_hwirq(d); 151119f5e44SMagnus Damm 152a53f7953SVladimir Zapolskiy dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type); 153119f5e44SMagnus Damm 154119f5e44SMagnus Damm switch (type & IRQ_TYPE_SENSE_MASK) { 155119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_HIGH: 1567e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true, 1577e1092b5SSimon Horman false); 158119f5e44SMagnus Damm break; 159119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_LOW: 1607e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true, 1617e1092b5SSimon Horman false); 162119f5e44SMagnus Damm break; 163119f5e44SMagnus Damm case IRQ_TYPE_EDGE_RISING: 1647e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1657e1092b5SSimon Horman false); 166119f5e44SMagnus Damm break; 167119f5e44SMagnus Damm case IRQ_TYPE_EDGE_FALLING: 1687e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false, 1697e1092b5SSimon Horman false); 1707e1092b5SSimon Horman break; 1717e1092b5SSimon Horman case IRQ_TYPE_EDGE_BOTH: 172208c80f1SGeert Uytterhoeven if (!p->info.has_both_edge_trigger) 1737e1092b5SSimon Horman return -EINVAL; 1747e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1757e1092b5SSimon Horman true); 176119f5e44SMagnus Damm break; 177119f5e44SMagnus Damm default: 178119f5e44SMagnus Damm return -EINVAL; 179119f5e44SMagnus Damm } 180119f5e44SMagnus Damm return 0; 181119f5e44SMagnus Damm } 182119f5e44SMagnus Damm 183ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on) 184ab82fa7dSGeert Uytterhoeven { 185ab82fa7dSGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 186c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(gc); 187501ef0f9SGeert Uytterhoeven int error; 188ab82fa7dSGeert Uytterhoeven 189501ef0f9SGeert Uytterhoeven if (p->irq_parent) { 190501ef0f9SGeert Uytterhoeven error = irq_set_irq_wake(p->irq_parent, on); 191501ef0f9SGeert Uytterhoeven if (error) { 192a53f7953SVladimir Zapolskiy dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n", 193501ef0f9SGeert Uytterhoeven p->irq_parent); 194501ef0f9SGeert Uytterhoeven p->irq_parent = 0; 195501ef0f9SGeert Uytterhoeven } 196501ef0f9SGeert Uytterhoeven } 197ab82fa7dSGeert Uytterhoeven 198ab82fa7dSGeert Uytterhoeven if (on) 1999ac79ba9SGeert Uytterhoeven atomic_inc(&p->wakeup_path); 200ab82fa7dSGeert Uytterhoeven else 2019ac79ba9SGeert Uytterhoeven atomic_dec(&p->wakeup_path); 202ab82fa7dSGeert Uytterhoeven 203ab82fa7dSGeert Uytterhoeven return 0; 204ab82fa7dSGeert Uytterhoeven } 205ab82fa7dSGeert Uytterhoeven 206119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) 207119f5e44SMagnus Damm { 208119f5e44SMagnus Damm struct gpio_rcar_priv *p = dev_id; 209119f5e44SMagnus Damm u32 pending; 210119f5e44SMagnus Damm unsigned int offset, irqs_handled = 0; 211119f5e44SMagnus Damm 2128808b64dSValentine Barshak while ((pending = gpio_rcar_read(p, INTDT) & 2138808b64dSValentine Barshak gpio_rcar_read(p, INTMSK))) { 214119f5e44SMagnus Damm offset = __ffs(pending); 215119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(offset)); 216*dbd1c54fSMarc Zyngier generic_handle_domain_irq(p->gpio_chip.irq.domain, 217*dbd1c54fSMarc Zyngier offset); 218119f5e44SMagnus Damm irqs_handled++; 219119f5e44SMagnus Damm } 220119f5e44SMagnus Damm 221119f5e44SMagnus Damm return irqs_handled ? IRQ_HANDLED : IRQ_NONE; 222119f5e44SMagnus Damm } 223119f5e44SMagnus Damm 224119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip, 225119f5e44SMagnus Damm unsigned int gpio, 226119f5e44SMagnus Damm bool output) 227119f5e44SMagnus Damm { 228c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(chip); 229119f5e44SMagnus Damm unsigned long flags; 230119f5e44SMagnus Damm 231119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 232119f5e44SMagnus Damm * "Setting General Output Mode" and 233119f5e44SMagnus Damm * "Setting General Input Mode" 234119f5e44SMagnus Damm */ 235119f5e44SMagnus Damm 236119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 237119f5e44SMagnus Damm 238b36368f6SAshish Chavan /* Configure positive logic in POSNEG */ 239119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, gpio, false); 240119f5e44SMagnus Damm 241119f5e44SMagnus Damm /* Select "General Input/Output Mode" in IOINTSEL */ 242119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, gpio, false); 243119f5e44SMagnus Damm 244119f5e44SMagnus Damm /* Select Input Mode or Output Mode in INOUTSEL */ 245119f5e44SMagnus Damm gpio_rcar_modify_bit(p, INOUTSEL, gpio, output); 246119f5e44SMagnus Damm 2473ae4f3aaSVladimir Zapolskiy /* Select General Output Register to output data in OUTDTSEL */ 248208c80f1SGeert Uytterhoeven if (p->info.has_outdtsel && output) 2493ae4f3aaSVladimir Zapolskiy gpio_rcar_modify_bit(p, OUTDTSEL, gpio, false); 2503ae4f3aaSVladimir Zapolskiy 251119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 252119f5e44SMagnus Damm } 253119f5e44SMagnus Damm 254dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset) 255dc3465a9SLaurent Pinchart { 2562d65472bSGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 2572d65472bSGeert Uytterhoeven int error; 2582d65472bSGeert Uytterhoeven 259a53f7953SVladimir Zapolskiy error = pm_runtime_get_sync(p->dev); 2606f8cd246SDinghao Liu if (error < 0) { 2616f8cd246SDinghao Liu pm_runtime_put(p->dev); 2622d65472bSGeert Uytterhoeven return error; 2636f8cd246SDinghao Liu } 2642d65472bSGeert Uytterhoeven 265a9a1d2a7SLinus Walleij error = pinctrl_gpio_request(chip->base + offset); 2662d65472bSGeert Uytterhoeven if (error) 267a53f7953SVladimir Zapolskiy pm_runtime_put(p->dev); 2682d65472bSGeert Uytterhoeven 2692d65472bSGeert Uytterhoeven return error; 270dc3465a9SLaurent Pinchart } 271dc3465a9SLaurent Pinchart 272dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset) 273dc3465a9SLaurent Pinchart { 2742d65472bSGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 2752d65472bSGeert Uytterhoeven 276a9a1d2a7SLinus Walleij pinctrl_gpio_free(chip->base + offset); 277dc3465a9SLaurent Pinchart 278ce0e2c60SLinus Walleij /* 279ce0e2c60SLinus Walleij * Set the GPIO as an input to ensure that the next GPIO request won't 280dc3465a9SLaurent Pinchart * drive the GPIO pin as an output. 281dc3465a9SLaurent Pinchart */ 282dc3465a9SLaurent Pinchart gpio_rcar_config_general_input_output_mode(chip, offset, false); 2832d65472bSGeert Uytterhoeven 284a53f7953SVladimir Zapolskiy pm_runtime_put(p->dev); 285dc3465a9SLaurent Pinchart } 286dc3465a9SLaurent Pinchart 287ad817297SGeert Uytterhoeven static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset) 288ad817297SGeert Uytterhoeven { 289ad817297SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 290ad817297SGeert Uytterhoeven 291e42615ecSMatti Vaittinen if (gpio_rcar_read(p, INOUTSEL) & BIT(offset)) 292e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT; 293e42615ecSMatti Vaittinen 294e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN; 295ad817297SGeert Uytterhoeven } 296ad817297SGeert Uytterhoeven 297119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset) 298119f5e44SMagnus Damm { 299119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, false); 300119f5e44SMagnus Damm return 0; 301119f5e44SMagnus Damm } 302119f5e44SMagnus Damm 303119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset) 304119f5e44SMagnus Damm { 305714d3a29SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 306ae9550f6SMagnus Damm u32 bit = BIT(offset); 307ae9550f6SMagnus Damm 308ecba1eaaSGeert Uytterhoeven /* 309ecba1eaaSGeert Uytterhoeven * Before R-Car Gen3, INDT does not show correct pin state when 310ecba1eaaSGeert Uytterhoeven * configured as output, so use OUTDT in case of output pins 311ecba1eaaSGeert Uytterhoeven */ 312ecba1eaaSGeert Uytterhoeven if (!p->info.has_always_in && (gpio_rcar_read(p, INOUTSEL) & bit)) 313714d3a29SGeert Uytterhoeven return !!(gpio_rcar_read(p, OUTDT) & bit); 314ae9550f6SMagnus Damm else 315714d3a29SGeert Uytterhoeven return !!(gpio_rcar_read(p, INDT) & bit); 316119f5e44SMagnus Damm } 317119f5e44SMagnus Damm 318183245c4SGeert Uytterhoeven static int gpio_rcar_get_multiple(struct gpio_chip *chip, unsigned long *mask, 319183245c4SGeert Uytterhoeven unsigned long *bits) 320183245c4SGeert Uytterhoeven { 321183245c4SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 322183245c4SGeert Uytterhoeven u32 bankmask, outputs, m, val = 0; 323183245c4SGeert Uytterhoeven unsigned long flags; 324183245c4SGeert Uytterhoeven 325183245c4SGeert Uytterhoeven bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0); 326183245c4SGeert Uytterhoeven if (chip->valid_mask) 327183245c4SGeert Uytterhoeven bankmask &= chip->valid_mask[0]; 328183245c4SGeert Uytterhoeven 329183245c4SGeert Uytterhoeven if (!bankmask) 330183245c4SGeert Uytterhoeven return 0; 331183245c4SGeert Uytterhoeven 332ecba1eaaSGeert Uytterhoeven if (p->info.has_always_in) { 333ecba1eaaSGeert Uytterhoeven bits[0] = gpio_rcar_read(p, INDT) & bankmask; 334ecba1eaaSGeert Uytterhoeven return 0; 335ecba1eaaSGeert Uytterhoeven } 336ecba1eaaSGeert Uytterhoeven 337183245c4SGeert Uytterhoeven spin_lock_irqsave(&p->lock, flags); 338183245c4SGeert Uytterhoeven outputs = gpio_rcar_read(p, INOUTSEL); 339183245c4SGeert Uytterhoeven m = outputs & bankmask; 340183245c4SGeert Uytterhoeven if (m) 341183245c4SGeert Uytterhoeven val |= gpio_rcar_read(p, OUTDT) & m; 342183245c4SGeert Uytterhoeven 343183245c4SGeert Uytterhoeven m = ~outputs & bankmask; 344183245c4SGeert Uytterhoeven if (m) 345183245c4SGeert Uytterhoeven val |= gpio_rcar_read(p, INDT) & m; 346183245c4SGeert Uytterhoeven spin_unlock_irqrestore(&p->lock, flags); 347183245c4SGeert Uytterhoeven 348183245c4SGeert Uytterhoeven bits[0] = val; 349183245c4SGeert Uytterhoeven return 0; 350183245c4SGeert Uytterhoeven } 351183245c4SGeert Uytterhoeven 352119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value) 353119f5e44SMagnus Damm { 354c7b6f457SLinus Walleij struct gpio_rcar_priv *p = gpiochip_get_data(chip); 355119f5e44SMagnus Damm unsigned long flags; 356119f5e44SMagnus Damm 357119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 358119f5e44SMagnus Damm gpio_rcar_modify_bit(p, OUTDT, offset, value); 359119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 360119f5e44SMagnus Damm } 361119f5e44SMagnus Damm 362dbb763b8SGeert Uytterhoeven static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask, 363dbb763b8SGeert Uytterhoeven unsigned long *bits) 364dbb763b8SGeert Uytterhoeven { 365dbb763b8SGeert Uytterhoeven struct gpio_rcar_priv *p = gpiochip_get_data(chip); 366dbb763b8SGeert Uytterhoeven unsigned long flags; 367dbb763b8SGeert Uytterhoeven u32 val, bankmask; 368dbb763b8SGeert Uytterhoeven 369dbb763b8SGeert Uytterhoeven bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0); 370496069b8SBiju Das if (chip->valid_mask) 371496069b8SBiju Das bankmask &= chip->valid_mask[0]; 372496069b8SBiju Das 373dbb763b8SGeert Uytterhoeven if (!bankmask) 374dbb763b8SGeert Uytterhoeven return; 375dbb763b8SGeert Uytterhoeven 376dbb763b8SGeert Uytterhoeven spin_lock_irqsave(&p->lock, flags); 377dbb763b8SGeert Uytterhoeven val = gpio_rcar_read(p, OUTDT); 378dbb763b8SGeert Uytterhoeven val &= ~bankmask; 379dbb763b8SGeert Uytterhoeven val |= (bankmask & bits[0]); 380dbb763b8SGeert Uytterhoeven gpio_rcar_write(p, OUTDT, val); 381dbb763b8SGeert Uytterhoeven spin_unlock_irqrestore(&p->lock, flags); 382dbb763b8SGeert Uytterhoeven } 383dbb763b8SGeert Uytterhoeven 384119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset, 385119f5e44SMagnus Damm int value) 386119f5e44SMagnus Damm { 387119f5e44SMagnus Damm /* write GPIO value to output before selecting output mode of pin */ 388119f5e44SMagnus Damm gpio_rcar_set(chip, offset, value); 389119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, true); 390119f5e44SMagnus Damm return 0; 391119f5e44SMagnus Damm } 392119f5e44SMagnus Damm 3931fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = { 3943ae4f3aaSVladimir Zapolskiy .has_outdtsel = false, 3951fd2b49dSHisashi Nakamura .has_both_edge_trigger = false, 396ecba1eaaSGeert Uytterhoeven .has_always_in = false, 39793ac0b0cSGeert Uytterhoeven .has_inen = false, 3981fd2b49dSHisashi Nakamura }; 3991fd2b49dSHisashi Nakamura 4001fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = { 4013ae4f3aaSVladimir Zapolskiy .has_outdtsel = true, 4021fd2b49dSHisashi Nakamura .has_both_edge_trigger = true, 403ecba1eaaSGeert Uytterhoeven .has_always_in = false, 40493ac0b0cSGeert Uytterhoeven .has_inen = false, 405ecba1eaaSGeert Uytterhoeven }; 406ecba1eaaSGeert Uytterhoeven 407ecba1eaaSGeert Uytterhoeven static const struct gpio_rcar_info gpio_rcar_info_gen3 = { 408ecba1eaaSGeert Uytterhoeven .has_outdtsel = true, 409ecba1eaaSGeert Uytterhoeven .has_both_edge_trigger = true, 410ecba1eaaSGeert Uytterhoeven .has_always_in = true, 41193ac0b0cSGeert Uytterhoeven .has_inen = false, 41293ac0b0cSGeert Uytterhoeven }; 41393ac0b0cSGeert Uytterhoeven 41493ac0b0cSGeert Uytterhoeven static const struct gpio_rcar_info gpio_rcar_info_v3u = { 41593ac0b0cSGeert Uytterhoeven .has_outdtsel = true, 41693ac0b0cSGeert Uytterhoeven .has_both_edge_trigger = true, 41793ac0b0cSGeert Uytterhoeven .has_always_in = true, 41893ac0b0cSGeert Uytterhoeven .has_inen = true, 4191fd2b49dSHisashi Nakamura }; 4201fd2b49dSHisashi Nakamura 421850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = { 422850dfe17SLaurent Pinchart { 42393ac0b0cSGeert Uytterhoeven .compatible = "renesas,gpio-r8a779a0", 42493ac0b0cSGeert Uytterhoeven .data = &gpio_rcar_info_v3u, 42593ac0b0cSGeert Uytterhoeven }, { 426dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen1-gpio", 427dbd1dad2SSimon Horman .data = &gpio_rcar_info_gen1, 428dbd1dad2SSimon Horman }, { 429dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen2-gpio", 430dbd1dad2SSimon Horman .data = &gpio_rcar_info_gen2, 431dbd1dad2SSimon Horman }, { 432dbd1dad2SSimon Horman .compatible = "renesas,rcar-gen3-gpio", 433ecba1eaaSGeert Uytterhoeven .data = &gpio_rcar_info_gen3, 434dbd1dad2SSimon Horman }, { 435850dfe17SLaurent Pinchart .compatible = "renesas,gpio-rcar", 4361fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen1, 437850dfe17SLaurent Pinchart }, { 438850dfe17SLaurent Pinchart /* Terminator */ 439850dfe17SLaurent Pinchart }, 440850dfe17SLaurent Pinchart }; 441850dfe17SLaurent Pinchart 442850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); 443850dfe17SLaurent Pinchart 4448b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins) 445159f8a02SLaurent Pinchart { 446a53f7953SVladimir Zapolskiy struct device_node *np = p->dev->of_node; 447850dfe17SLaurent Pinchart const struct gpio_rcar_info *info; 4488b092be9SGeert Uytterhoeven struct of_phandle_args args; 4498b092be9SGeert Uytterhoeven int ret; 450850dfe17SLaurent Pinchart 451a53f7953SVladimir Zapolskiy info = of_device_get_match_data(p->dev); 452208c80f1SGeert Uytterhoeven p->info = *info; 453850dfe17SLaurent Pinchart 4548b092be9SGeert Uytterhoeven ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args); 4558b092be9SGeert Uytterhoeven *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK; 456159f8a02SLaurent Pinchart 4578b092be9SGeert Uytterhoeven if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) { 458a53f7953SVladimir Zapolskiy dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n", 459a53f7953SVladimir Zapolskiy *npins, RCAR_MAX_GPIO_PER_BANK); 4608b092be9SGeert Uytterhoeven *npins = RCAR_MAX_GPIO_PER_BANK; 461159f8a02SLaurent Pinchart } 462850dfe17SLaurent Pinchart 463850dfe17SLaurent Pinchart return 0; 464159f8a02SLaurent Pinchart } 465159f8a02SLaurent Pinchart 46693ac0b0cSGeert Uytterhoeven static void gpio_rcar_enable_inputs(struct gpio_rcar_priv *p) 46793ac0b0cSGeert Uytterhoeven { 46893ac0b0cSGeert Uytterhoeven u32 mask = GENMASK(p->gpio_chip.ngpio - 1, 0); 46993ac0b0cSGeert Uytterhoeven 47093ac0b0cSGeert Uytterhoeven /* Select "Input Enable" in INEN */ 47193ac0b0cSGeert Uytterhoeven if (p->gpio_chip.valid_mask) 47293ac0b0cSGeert Uytterhoeven mask &= p->gpio_chip.valid_mask[0]; 47393ac0b0cSGeert Uytterhoeven if (mask) 47493ac0b0cSGeert Uytterhoeven gpio_rcar_write(p, INEN, gpio_rcar_read(p, INEN) | mask); 47593ac0b0cSGeert Uytterhoeven } 47693ac0b0cSGeert Uytterhoeven 477119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev) 478119f5e44SMagnus Damm { 479119f5e44SMagnus Damm struct gpio_rcar_priv *p; 480ecbf7c2eSEnrico Weigelt, metux IT consult struct resource *irq; 481119f5e44SMagnus Damm struct gpio_chip *gpio_chip; 482119f5e44SMagnus Damm struct irq_chip *irq_chip; 483b470cef1SLinus Walleij struct gpio_irq_chip *girq; 484b22978fcSGeert Uytterhoeven struct device *dev = &pdev->dev; 485b22978fcSGeert Uytterhoeven const char *name = dev_name(dev); 4868b092be9SGeert Uytterhoeven unsigned int npins; 487119f5e44SMagnus Damm int ret; 488119f5e44SMagnus Damm 489b22978fcSGeert Uytterhoeven p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); 4907d82bf34SGeert Uytterhoeven if (!p) 4917d82bf34SGeert Uytterhoeven return -ENOMEM; 492119f5e44SMagnus Damm 493a53f7953SVladimir Zapolskiy p->dev = dev; 494119f5e44SMagnus Damm spin_lock_init(&p->lock); 495119f5e44SMagnus Damm 4968b092be9SGeert Uytterhoeven /* Get device configuration from DT node */ 4978b092be9SGeert Uytterhoeven ret = gpio_rcar_parse_dt(p, &npins); 498850dfe17SLaurent Pinchart if (ret < 0) 499850dfe17SLaurent Pinchart return ret; 500159f8a02SLaurent Pinchart 501159f8a02SLaurent Pinchart platform_set_drvdata(pdev, p); 502159f8a02SLaurent Pinchart 503df0c6c80SGeert Uytterhoeven pm_runtime_enable(dev); 504df0c6c80SGeert Uytterhoeven 505119f5e44SMagnus Damm irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 5065a24d4b6SSergei Shtylyov if (!irq) { 5075a24d4b6SSergei Shtylyov dev_err(dev, "missing IRQ\n"); 508119f5e44SMagnus Damm ret = -EINVAL; 509119f5e44SMagnus Damm goto err0; 510119f5e44SMagnus Damm } 511119f5e44SMagnus Damm 512ecbf7c2eSEnrico Weigelt, metux IT consult p->base = devm_platform_ioremap_resource(pdev, 0); 5135a24d4b6SSergei Shtylyov if (IS_ERR(p->base)) { 5145a24d4b6SSergei Shtylyov ret = PTR_ERR(p->base); 515119f5e44SMagnus Damm goto err0; 516119f5e44SMagnus Damm } 517119f5e44SMagnus Damm 518119f5e44SMagnus Damm gpio_chip = &p->gpio_chip; 519dc3465a9SLaurent Pinchart gpio_chip->request = gpio_rcar_request; 520dc3465a9SLaurent Pinchart gpio_chip->free = gpio_rcar_free; 521ad817297SGeert Uytterhoeven gpio_chip->get_direction = gpio_rcar_get_direction; 522119f5e44SMagnus Damm gpio_chip->direction_input = gpio_rcar_direction_input; 523119f5e44SMagnus Damm gpio_chip->get = gpio_rcar_get; 524183245c4SGeert Uytterhoeven gpio_chip->get_multiple = gpio_rcar_get_multiple; 525119f5e44SMagnus Damm gpio_chip->direction_output = gpio_rcar_direction_output; 526119f5e44SMagnus Damm gpio_chip->set = gpio_rcar_set; 527dbb763b8SGeert Uytterhoeven gpio_chip->set_multiple = gpio_rcar_set_multiple; 528119f5e44SMagnus Damm gpio_chip->label = name; 52958383c78SLinus Walleij gpio_chip->parent = dev; 530119f5e44SMagnus Damm gpio_chip->owner = THIS_MODULE; 5318b092be9SGeert Uytterhoeven gpio_chip->base = -1; 5328b092be9SGeert Uytterhoeven gpio_chip->ngpio = npins; 533119f5e44SMagnus Damm 534119f5e44SMagnus Damm irq_chip = &p->irq_chip; 535f932a686SGeert Uytterhoeven irq_chip->name = "gpio-rcar"; 53647bd38a3SNiklas Söderlund irq_chip->parent_device = dev; 537119f5e44SMagnus Damm irq_chip->irq_mask = gpio_rcar_irq_disable; 538119f5e44SMagnus Damm irq_chip->irq_unmask = gpio_rcar_irq_enable; 539119f5e44SMagnus Damm irq_chip->irq_set_type = gpio_rcar_irq_set_type; 540ab82fa7dSGeert Uytterhoeven irq_chip->irq_set_wake = gpio_rcar_irq_set_wake; 541ab82fa7dSGeert Uytterhoeven irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND; 542119f5e44SMagnus Damm 543b470cef1SLinus Walleij girq = &gpio_chip->irq; 544b470cef1SLinus Walleij girq->chip = irq_chip; 545b470cef1SLinus Walleij /* This will let us handle the parent IRQ in the driver */ 546b470cef1SLinus Walleij girq->parent_handler = NULL; 547b470cef1SLinus Walleij girq->num_parents = 0; 548b470cef1SLinus Walleij girq->parents = NULL; 549b470cef1SLinus Walleij girq->default_type = IRQ_TYPE_NONE; 550b470cef1SLinus Walleij girq->handler = handle_level_irq; 551b470cef1SLinus Walleij 552c7b6f457SLinus Walleij ret = gpiochip_add_data(gpio_chip, p); 553c7f3c5d3SGeert Uytterhoeven if (ret) { 554c7f3c5d3SGeert Uytterhoeven dev_err(dev, "failed to add GPIO controller\n"); 5550c8aab8eSDan Carpenter goto err0; 556119f5e44SMagnus Damm } 557119f5e44SMagnus Damm 558ab82fa7dSGeert Uytterhoeven p->irq_parent = irq->start; 559b22978fcSGeert Uytterhoeven if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler, 560b22978fcSGeert Uytterhoeven IRQF_SHARED, name, p)) { 561b22978fcSGeert Uytterhoeven dev_err(dev, "failed to request IRQ\n"); 562119f5e44SMagnus Damm ret = -ENOENT; 563119f5e44SMagnus Damm goto err1; 564119f5e44SMagnus Damm } 565119f5e44SMagnus Damm 56693ac0b0cSGeert Uytterhoeven if (p->info.has_inen) { 56793ac0b0cSGeert Uytterhoeven pm_runtime_get_sync(p->dev); 56893ac0b0cSGeert Uytterhoeven gpio_rcar_enable_inputs(p); 56993ac0b0cSGeert Uytterhoeven pm_runtime_put(p->dev); 57093ac0b0cSGeert Uytterhoeven } 57193ac0b0cSGeert Uytterhoeven 5728b092be9SGeert Uytterhoeven dev_info(dev, "driving %d GPIOs\n", npins); 573dc3465a9SLaurent Pinchart 574119f5e44SMagnus Damm return 0; 575119f5e44SMagnus Damm 576119f5e44SMagnus Damm err1: 5774d84b9e4SGeert Uytterhoeven gpiochip_remove(gpio_chip); 578119f5e44SMagnus Damm err0: 579df0c6c80SGeert Uytterhoeven pm_runtime_disable(dev); 580119f5e44SMagnus Damm return ret; 581119f5e44SMagnus Damm } 582119f5e44SMagnus Damm 583119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev) 584119f5e44SMagnus Damm { 585119f5e44SMagnus Damm struct gpio_rcar_priv *p = platform_get_drvdata(pdev); 586119f5e44SMagnus Damm 5879f5132aeSabdoulaye berthe gpiochip_remove(&p->gpio_chip); 588119f5e44SMagnus Damm 589df0c6c80SGeert Uytterhoeven pm_runtime_disable(&pdev->dev); 590119f5e44SMagnus Damm return 0; 591119f5e44SMagnus Damm } 592119f5e44SMagnus Damm 59351750fb1SHien Dang #ifdef CONFIG_PM_SLEEP 59451750fb1SHien Dang static int gpio_rcar_suspend(struct device *dev) 59551750fb1SHien Dang { 59651750fb1SHien Dang struct gpio_rcar_priv *p = dev_get_drvdata(dev); 59751750fb1SHien Dang 59851750fb1SHien Dang p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL); 59951750fb1SHien Dang p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL); 60051750fb1SHien Dang p->bank_info.outdt = gpio_rcar_read(p, OUTDT); 60151750fb1SHien Dang p->bank_info.intmsk = gpio_rcar_read(p, INTMSK); 60251750fb1SHien Dang p->bank_info.posneg = gpio_rcar_read(p, POSNEG); 60351750fb1SHien Dang p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL); 604208c80f1SGeert Uytterhoeven if (p->info.has_both_edge_trigger) 60551750fb1SHien Dang p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE); 60651750fb1SHien Dang 6079ac79ba9SGeert Uytterhoeven if (atomic_read(&p->wakeup_path)) 6089ac79ba9SGeert Uytterhoeven device_set_wakeup_path(dev); 6099ac79ba9SGeert Uytterhoeven 61051750fb1SHien Dang return 0; 61151750fb1SHien Dang } 61251750fb1SHien Dang 61351750fb1SHien Dang static int gpio_rcar_resume(struct device *dev) 61451750fb1SHien Dang { 61551750fb1SHien Dang struct gpio_rcar_priv *p = dev_get_drvdata(dev); 61651750fb1SHien Dang unsigned int offset; 61751750fb1SHien Dang u32 mask; 61851750fb1SHien Dang 61951750fb1SHien Dang for (offset = 0; offset < p->gpio_chip.ngpio; offset++) { 620496069b8SBiju Das if (!gpiochip_line_is_valid(&p->gpio_chip, offset)) 621496069b8SBiju Das continue; 622496069b8SBiju Das 62351750fb1SHien Dang mask = BIT(offset); 62451750fb1SHien Dang /* I/O pin */ 62551750fb1SHien Dang if (!(p->bank_info.iointsel & mask)) { 62651750fb1SHien Dang if (p->bank_info.inoutsel & mask) 62751750fb1SHien Dang gpio_rcar_direction_output( 62851750fb1SHien Dang &p->gpio_chip, offset, 62951750fb1SHien Dang !!(p->bank_info.outdt & mask)); 63051750fb1SHien Dang else 63151750fb1SHien Dang gpio_rcar_direction_input(&p->gpio_chip, 63251750fb1SHien Dang offset); 63351750fb1SHien Dang } else { 63451750fb1SHien Dang /* Interrupt pin */ 63551750fb1SHien Dang gpio_rcar_config_interrupt_input_mode( 63651750fb1SHien Dang p, 63751750fb1SHien Dang offset, 63851750fb1SHien Dang !(p->bank_info.posneg & mask), 63951750fb1SHien Dang !(p->bank_info.edglevel & mask), 64051750fb1SHien Dang !!(p->bank_info.bothedge & mask)); 64151750fb1SHien Dang 64251750fb1SHien Dang if (p->bank_info.intmsk & mask) 64351750fb1SHien Dang gpio_rcar_write(p, MSKCLR, mask); 64451750fb1SHien Dang } 64551750fb1SHien Dang } 64651750fb1SHien Dang 64793ac0b0cSGeert Uytterhoeven if (p->info.has_inen) 64893ac0b0cSGeert Uytterhoeven gpio_rcar_enable_inputs(p); 64993ac0b0cSGeert Uytterhoeven 65051750fb1SHien Dang return 0; 65151750fb1SHien Dang } 65251750fb1SHien Dang #endif /* CONFIG_PM_SLEEP*/ 65351750fb1SHien Dang 65451750fb1SHien Dang static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume); 65551750fb1SHien Dang 656119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = { 657119f5e44SMagnus Damm .probe = gpio_rcar_probe, 658119f5e44SMagnus Damm .remove = gpio_rcar_remove, 659119f5e44SMagnus Damm .driver = { 660119f5e44SMagnus Damm .name = "gpio_rcar", 66151750fb1SHien Dang .pm = &gpio_rcar_pm_ops, 662159f8a02SLaurent Pinchart .of_match_table = of_match_ptr(gpio_rcar_of_table), 663119f5e44SMagnus Damm } 664119f5e44SMagnus Damm }; 665119f5e44SMagnus Damm 666119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver); 667119f5e44SMagnus Damm 668119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm"); 669119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); 670119f5e44SMagnus Damm MODULE_LICENSE("GPL v2"); 671