1119f5e44SMagnus Damm /* 2119f5e44SMagnus Damm * Renesas R-Car GPIO Support 3119f5e44SMagnus Damm * 41fd2b49dSHisashi Nakamura * Copyright (C) 2014 Renesas Electronics Corporation 5119f5e44SMagnus Damm * Copyright (C) 2013 Magnus Damm 6119f5e44SMagnus Damm * 7119f5e44SMagnus Damm * This program is free software; you can redistribute it and/or modify 8119f5e44SMagnus Damm * it under the terms of the GNU General Public License as published by 9119f5e44SMagnus Damm * the Free Software Foundation; either version 2 of the License 10119f5e44SMagnus Damm * 11119f5e44SMagnus Damm * This program is distributed in the hope that it will be useful, 12119f5e44SMagnus Damm * but WITHOUT ANY WARRANTY; without even the implied warranty of 13119f5e44SMagnus Damm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14119f5e44SMagnus Damm * GNU General Public License for more details. 15119f5e44SMagnus Damm */ 16119f5e44SMagnus Damm 17119f5e44SMagnus Damm #include <linux/err.h> 18119f5e44SMagnus Damm #include <linux/gpio.h> 19119f5e44SMagnus Damm #include <linux/init.h> 20119f5e44SMagnus Damm #include <linux/interrupt.h> 21119f5e44SMagnus Damm #include <linux/io.h> 22119f5e44SMagnus Damm #include <linux/ioport.h> 23119f5e44SMagnus Damm #include <linux/irq.h> 24119f5e44SMagnus Damm #include <linux/module.h> 25bd0bf468SSachin Kamat #include <linux/of.h> 26dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h> 27119f5e44SMagnus Damm #include <linux/platform_data/gpio-rcar.h> 28119f5e44SMagnus Damm #include <linux/platform_device.h> 29df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h> 30119f5e44SMagnus Damm #include <linux/spinlock.h> 31119f5e44SMagnus Damm #include <linux/slab.h> 32119f5e44SMagnus Damm 33119f5e44SMagnus Damm struct gpio_rcar_priv { 34119f5e44SMagnus Damm void __iomem *base; 35119f5e44SMagnus Damm spinlock_t lock; 36119f5e44SMagnus Damm struct gpio_rcar_config config; 37119f5e44SMagnus Damm struct platform_device *pdev; 38119f5e44SMagnus Damm struct gpio_chip gpio_chip; 39119f5e44SMagnus Damm struct irq_chip irq_chip; 40119f5e44SMagnus Damm }; 41119f5e44SMagnus Damm 423dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */ 433dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04 /* General Input/Output Switching Register */ 443dc1e685SGeert Uytterhoeven #define OUTDT 0x08 /* General Output Register */ 453dc1e685SGeert Uytterhoeven #define INDT 0x0c /* General Input Register */ 463dc1e685SGeert Uytterhoeven #define INTDT 0x10 /* Interrupt Display Register */ 473dc1e685SGeert Uytterhoeven #define INTCLR 0x14 /* Interrupt Clear Register */ 483dc1e685SGeert Uytterhoeven #define INTMSK 0x18 /* Interrupt Mask Register */ 493dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c /* Interrupt Mask Clear Register */ 503dc1e685SGeert Uytterhoeven #define POSNEG 0x20 /* Positive/Negative Logic Select Register */ 513dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24 /* Edge/level Select Register */ 523dc1e685SGeert Uytterhoeven #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */ 533dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */ 54119f5e44SMagnus Damm 55159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK 32 56159f8a02SLaurent Pinchart 57119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs) 58119f5e44SMagnus Damm { 59119f5e44SMagnus Damm return ioread32(p->base + offs); 60119f5e44SMagnus Damm } 61119f5e44SMagnus Damm 62119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs, 63119f5e44SMagnus Damm u32 value) 64119f5e44SMagnus Damm { 65119f5e44SMagnus Damm iowrite32(value, p->base + offs); 66119f5e44SMagnus Damm } 67119f5e44SMagnus Damm 68119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs, 69119f5e44SMagnus Damm int bit, bool value) 70119f5e44SMagnus Damm { 71119f5e44SMagnus Damm u32 tmp = gpio_rcar_read(p, offs); 72119f5e44SMagnus Damm 73119f5e44SMagnus Damm if (value) 74119f5e44SMagnus Damm tmp |= BIT(bit); 75119f5e44SMagnus Damm else 76119f5e44SMagnus Damm tmp &= ~BIT(bit); 77119f5e44SMagnus Damm 78119f5e44SMagnus Damm gpio_rcar_write(p, offs, tmp); 79119f5e44SMagnus Damm } 80119f5e44SMagnus Damm 81119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d) 82119f5e44SMagnus Damm { 83c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 84c7f3c5d3SGeert Uytterhoeven struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv, 85c7f3c5d3SGeert Uytterhoeven gpio_chip); 86119f5e44SMagnus Damm 87119f5e44SMagnus Damm gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d))); 88119f5e44SMagnus Damm } 89119f5e44SMagnus Damm 90119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d) 91119f5e44SMagnus Damm { 92c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 93c7f3c5d3SGeert Uytterhoeven struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv, 94c7f3c5d3SGeert Uytterhoeven gpio_chip); 95119f5e44SMagnus Damm 96119f5e44SMagnus Damm gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d))); 97119f5e44SMagnus Damm } 98119f5e44SMagnus Damm 99119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p, 100119f5e44SMagnus Damm unsigned int hwirq, 101119f5e44SMagnus Damm bool active_high_rising_edge, 1027e1092b5SSimon Horman bool level_trigger, 1037e1092b5SSimon Horman bool both) 104119f5e44SMagnus Damm { 105119f5e44SMagnus Damm unsigned long flags; 106119f5e44SMagnus Damm 107119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 108119f5e44SMagnus Damm * "Setting Edge-Sensitive Interrupt Input Mode" and 109119f5e44SMagnus Damm * "Setting Level-Sensitive Interrupt Input Mode" 110119f5e44SMagnus Damm */ 111119f5e44SMagnus Damm 112119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 113119f5e44SMagnus Damm 114119f5e44SMagnus Damm /* Configure postive or negative logic in POSNEG */ 115119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge); 116119f5e44SMagnus Damm 117119f5e44SMagnus Damm /* Configure edge or level trigger in EDGLEVEL */ 118119f5e44SMagnus Damm gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger); 119119f5e44SMagnus Damm 1207e1092b5SSimon Horman /* Select one edge or both edges in BOTHEDGE */ 1217e1092b5SSimon Horman if (p->config.has_both_edge_trigger) 1227e1092b5SSimon Horman gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both); 1237e1092b5SSimon Horman 124119f5e44SMagnus Damm /* Select "Interrupt Input Mode" in IOINTSEL */ 125119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true); 126119f5e44SMagnus Damm 127119f5e44SMagnus Damm /* Write INTCLR in case of edge trigger */ 128119f5e44SMagnus Damm if (!level_trigger) 129119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(hwirq)); 130119f5e44SMagnus Damm 131119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 132119f5e44SMagnus Damm } 133119f5e44SMagnus Damm 134119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type) 135119f5e44SMagnus Damm { 136c7f3c5d3SGeert Uytterhoeven struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 137c7f3c5d3SGeert Uytterhoeven struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv, 138c7f3c5d3SGeert Uytterhoeven gpio_chip); 139119f5e44SMagnus Damm unsigned int hwirq = irqd_to_hwirq(d); 140119f5e44SMagnus Damm 141119f5e44SMagnus Damm dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type); 142119f5e44SMagnus Damm 143119f5e44SMagnus Damm switch (type & IRQ_TYPE_SENSE_MASK) { 144119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_HIGH: 1457e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true, 1467e1092b5SSimon Horman false); 147119f5e44SMagnus Damm break; 148119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_LOW: 1497e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true, 1507e1092b5SSimon Horman false); 151119f5e44SMagnus Damm break; 152119f5e44SMagnus Damm case IRQ_TYPE_EDGE_RISING: 1537e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1547e1092b5SSimon Horman false); 155119f5e44SMagnus Damm break; 156119f5e44SMagnus Damm case IRQ_TYPE_EDGE_FALLING: 1577e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false, 1587e1092b5SSimon Horman false); 1597e1092b5SSimon Horman break; 1607e1092b5SSimon Horman case IRQ_TYPE_EDGE_BOTH: 1617e1092b5SSimon Horman if (!p->config.has_both_edge_trigger) 1627e1092b5SSimon Horman return -EINVAL; 1637e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1647e1092b5SSimon Horman true); 165119f5e44SMagnus Damm break; 166119f5e44SMagnus Damm default: 167119f5e44SMagnus Damm return -EINVAL; 168119f5e44SMagnus Damm } 169119f5e44SMagnus Damm return 0; 170119f5e44SMagnus Damm } 171119f5e44SMagnus Damm 172119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) 173119f5e44SMagnus Damm { 174119f5e44SMagnus Damm struct gpio_rcar_priv *p = dev_id; 175119f5e44SMagnus Damm u32 pending; 176119f5e44SMagnus Damm unsigned int offset, irqs_handled = 0; 177119f5e44SMagnus Damm 1788808b64dSValentine Barshak while ((pending = gpio_rcar_read(p, INTDT) & 1798808b64dSValentine Barshak gpio_rcar_read(p, INTMSK))) { 180119f5e44SMagnus Damm offset = __ffs(pending); 181119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(offset)); 182c7f3c5d3SGeert Uytterhoeven generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain, 183c7f3c5d3SGeert Uytterhoeven offset)); 184119f5e44SMagnus Damm irqs_handled++; 185119f5e44SMagnus Damm } 186119f5e44SMagnus Damm 187119f5e44SMagnus Damm return irqs_handled ? IRQ_HANDLED : IRQ_NONE; 188119f5e44SMagnus Damm } 189119f5e44SMagnus Damm 190119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip) 191119f5e44SMagnus Damm { 192119f5e44SMagnus Damm return container_of(chip, struct gpio_rcar_priv, gpio_chip); 193119f5e44SMagnus Damm } 194119f5e44SMagnus Damm 195119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip, 196119f5e44SMagnus Damm unsigned int gpio, 197119f5e44SMagnus Damm bool output) 198119f5e44SMagnus Damm { 199119f5e44SMagnus Damm struct gpio_rcar_priv *p = gpio_to_priv(chip); 200119f5e44SMagnus Damm unsigned long flags; 201119f5e44SMagnus Damm 202119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 203119f5e44SMagnus Damm * "Setting General Output Mode" and 204119f5e44SMagnus Damm * "Setting General Input Mode" 205119f5e44SMagnus Damm */ 206119f5e44SMagnus Damm 207119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 208119f5e44SMagnus Damm 209119f5e44SMagnus Damm /* Configure postive logic in POSNEG */ 210119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, gpio, false); 211119f5e44SMagnus Damm 212119f5e44SMagnus Damm /* Select "General Input/Output Mode" in IOINTSEL */ 213119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, gpio, false); 214119f5e44SMagnus Damm 215119f5e44SMagnus Damm /* Select Input Mode or Output Mode in INOUTSEL */ 216119f5e44SMagnus Damm gpio_rcar_modify_bit(p, INOUTSEL, gpio, output); 217119f5e44SMagnus Damm 218119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 219119f5e44SMagnus Damm } 220119f5e44SMagnus Damm 221dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset) 222dc3465a9SLaurent Pinchart { 223dc3465a9SLaurent Pinchart return pinctrl_request_gpio(chip->base + offset); 224dc3465a9SLaurent Pinchart } 225dc3465a9SLaurent Pinchart 226dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset) 227dc3465a9SLaurent Pinchart { 228dc3465a9SLaurent Pinchart pinctrl_free_gpio(chip->base + offset); 229dc3465a9SLaurent Pinchart 230dc3465a9SLaurent Pinchart /* Set the GPIO as an input to ensure that the next GPIO request won't 231dc3465a9SLaurent Pinchart * drive the GPIO pin as an output. 232dc3465a9SLaurent Pinchart */ 233dc3465a9SLaurent Pinchart gpio_rcar_config_general_input_output_mode(chip, offset, false); 234dc3465a9SLaurent Pinchart } 235dc3465a9SLaurent Pinchart 236119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset) 237119f5e44SMagnus Damm { 238119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, false); 239119f5e44SMagnus Damm return 0; 240119f5e44SMagnus Damm } 241119f5e44SMagnus Damm 242119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset) 243119f5e44SMagnus Damm { 244ae9550f6SMagnus Damm u32 bit = BIT(offset); 245ae9550f6SMagnus Damm 246ae9550f6SMagnus Damm /* testing on r8a7790 shows that INDT does not show correct pin state 247ae9550f6SMagnus Damm * when configured as output, so use OUTDT in case of output pins */ 248ae9550f6SMagnus Damm if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit) 2497cb5409bSJürg Billeter return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit); 250ae9550f6SMagnus Damm else 2517cb5409bSJürg Billeter return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit); 252119f5e44SMagnus Damm } 253119f5e44SMagnus Damm 254119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value) 255119f5e44SMagnus Damm { 256119f5e44SMagnus Damm struct gpio_rcar_priv *p = gpio_to_priv(chip); 257119f5e44SMagnus Damm unsigned long flags; 258119f5e44SMagnus Damm 259119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 260119f5e44SMagnus Damm gpio_rcar_modify_bit(p, OUTDT, offset, value); 261119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 262119f5e44SMagnus Damm } 263119f5e44SMagnus Damm 264119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset, 265119f5e44SMagnus Damm int value) 266119f5e44SMagnus Damm { 267119f5e44SMagnus Damm /* write GPIO value to output before selecting output mode of pin */ 268119f5e44SMagnus Damm gpio_rcar_set(chip, offset, value); 269119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, true); 270119f5e44SMagnus Damm return 0; 271119f5e44SMagnus Damm } 272119f5e44SMagnus Damm 273850dfe17SLaurent Pinchart struct gpio_rcar_info { 274850dfe17SLaurent Pinchart bool has_both_edge_trigger; 275850dfe17SLaurent Pinchart }; 276850dfe17SLaurent Pinchart 2771fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = { 2781fd2b49dSHisashi Nakamura .has_both_edge_trigger = false, 2791fd2b49dSHisashi Nakamura }; 2801fd2b49dSHisashi Nakamura 2811fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = { 2821fd2b49dSHisashi Nakamura .has_both_edge_trigger = true, 2831fd2b49dSHisashi Nakamura }; 2841fd2b49dSHisashi Nakamura 285850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = { 286850dfe17SLaurent Pinchart { 287850dfe17SLaurent Pinchart .compatible = "renesas,gpio-r8a7790", 2881fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 289850dfe17SLaurent Pinchart }, { 290850dfe17SLaurent Pinchart .compatible = "renesas,gpio-r8a7791", 2911fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 2921fd2b49dSHisashi Nakamura }, { 2931fd2b49dSHisashi Nakamura .compatible = "renesas,gpio-r8a7793", 2941fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 2951fd2b49dSHisashi Nakamura }, { 2961fd2b49dSHisashi Nakamura .compatible = "renesas,gpio-r8a7794", 2971fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen2, 298850dfe17SLaurent Pinchart }, { 299850dfe17SLaurent Pinchart .compatible = "renesas,gpio-rcar", 3001fd2b49dSHisashi Nakamura .data = &gpio_rcar_info_gen1, 301850dfe17SLaurent Pinchart }, { 302850dfe17SLaurent Pinchart /* Terminator */ 303850dfe17SLaurent Pinchart }, 304850dfe17SLaurent Pinchart }; 305850dfe17SLaurent Pinchart 306850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); 307850dfe17SLaurent Pinchart 308850dfe17SLaurent Pinchart static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p) 309159f8a02SLaurent Pinchart { 310e56aee18SJingoo Han struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev); 311159f8a02SLaurent Pinchart struct device_node *np = p->pdev->dev.of_node; 312159f8a02SLaurent Pinchart struct of_phandle_args args; 313159f8a02SLaurent Pinchart int ret; 314159f8a02SLaurent Pinchart 315e305062eSLaurent Pinchart if (pdata) { 316159f8a02SLaurent Pinchart p->config = *pdata; 317e305062eSLaurent Pinchart } else if (IS_ENABLED(CONFIG_OF) && np) { 318850dfe17SLaurent Pinchart const struct of_device_id *match; 319850dfe17SLaurent Pinchart const struct gpio_rcar_info *info; 320850dfe17SLaurent Pinchart 321850dfe17SLaurent Pinchart match = of_match_node(gpio_rcar_of_table, np); 322850dfe17SLaurent Pinchart if (!match) 323850dfe17SLaurent Pinchart return -EINVAL; 324850dfe17SLaurent Pinchart 325850dfe17SLaurent Pinchart info = match->data; 326850dfe17SLaurent Pinchart 32701eb2d18SLaurent Pinchart ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, 32801eb2d18SLaurent Pinchart &args); 32901eb2d18SLaurent Pinchart p->config.number_of_pins = ret == 0 ? args.args[2] 330159f8a02SLaurent Pinchart : RCAR_MAX_GPIO_PER_BANK; 331159f8a02SLaurent Pinchart p->config.gpio_base = -1; 332850dfe17SLaurent Pinchart p->config.has_both_edge_trigger = info->has_both_edge_trigger; 333159f8a02SLaurent Pinchart } 334159f8a02SLaurent Pinchart 335159f8a02SLaurent Pinchart if (p->config.number_of_pins == 0 || 336159f8a02SLaurent Pinchart p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) { 337159f8a02SLaurent Pinchart dev_warn(&p->pdev->dev, 338159f8a02SLaurent Pinchart "Invalid number of gpio lines %u, using %u\n", 339159f8a02SLaurent Pinchart p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK); 340159f8a02SLaurent Pinchart p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK; 341159f8a02SLaurent Pinchart } 342850dfe17SLaurent Pinchart 343850dfe17SLaurent Pinchart return 0; 344159f8a02SLaurent Pinchart } 345159f8a02SLaurent Pinchart 346119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev) 347119f5e44SMagnus Damm { 348119f5e44SMagnus Damm struct gpio_rcar_priv *p; 349119f5e44SMagnus Damm struct resource *io, *irq; 350119f5e44SMagnus Damm struct gpio_chip *gpio_chip; 351119f5e44SMagnus Damm struct irq_chip *irq_chip; 352b22978fcSGeert Uytterhoeven struct device *dev = &pdev->dev; 353b22978fcSGeert Uytterhoeven const char *name = dev_name(dev); 354119f5e44SMagnus Damm int ret; 355119f5e44SMagnus Damm 356b22978fcSGeert Uytterhoeven p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); 3577d82bf34SGeert Uytterhoeven if (!p) 3587d82bf34SGeert Uytterhoeven return -ENOMEM; 359119f5e44SMagnus Damm 360119f5e44SMagnus Damm p->pdev = pdev; 361119f5e44SMagnus Damm spin_lock_init(&p->lock); 362119f5e44SMagnus Damm 363159f8a02SLaurent Pinchart /* Get device configuration from DT node or platform data. */ 364850dfe17SLaurent Pinchart ret = gpio_rcar_parse_pdata(p); 365850dfe17SLaurent Pinchart if (ret < 0) 366850dfe17SLaurent Pinchart return ret; 367159f8a02SLaurent Pinchart 368159f8a02SLaurent Pinchart platform_set_drvdata(pdev, p); 369159f8a02SLaurent Pinchart 370df0c6c80SGeert Uytterhoeven pm_runtime_enable(dev); 371df0c6c80SGeert Uytterhoeven pm_runtime_get_sync(dev); 372df0c6c80SGeert Uytterhoeven 373119f5e44SMagnus Damm io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 374119f5e44SMagnus Damm irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 375119f5e44SMagnus Damm 376119f5e44SMagnus Damm if (!io || !irq) { 377b22978fcSGeert Uytterhoeven dev_err(dev, "missing IRQ or IOMEM\n"); 378119f5e44SMagnus Damm ret = -EINVAL; 379119f5e44SMagnus Damm goto err0; 380119f5e44SMagnus Damm } 381119f5e44SMagnus Damm 382b22978fcSGeert Uytterhoeven p->base = devm_ioremap_nocache(dev, io->start, resource_size(io)); 383119f5e44SMagnus Damm if (!p->base) { 384b22978fcSGeert Uytterhoeven dev_err(dev, "failed to remap I/O memory\n"); 385119f5e44SMagnus Damm ret = -ENXIO; 386119f5e44SMagnus Damm goto err0; 387119f5e44SMagnus Damm } 388119f5e44SMagnus Damm 389119f5e44SMagnus Damm gpio_chip = &p->gpio_chip; 390dc3465a9SLaurent Pinchart gpio_chip->request = gpio_rcar_request; 391dc3465a9SLaurent Pinchart gpio_chip->free = gpio_rcar_free; 392119f5e44SMagnus Damm gpio_chip->direction_input = gpio_rcar_direction_input; 393119f5e44SMagnus Damm gpio_chip->get = gpio_rcar_get; 394119f5e44SMagnus Damm gpio_chip->direction_output = gpio_rcar_direction_output; 395119f5e44SMagnus Damm gpio_chip->set = gpio_rcar_set; 396119f5e44SMagnus Damm gpio_chip->label = name; 397b22978fcSGeert Uytterhoeven gpio_chip->dev = dev; 398119f5e44SMagnus Damm gpio_chip->owner = THIS_MODULE; 399119f5e44SMagnus Damm gpio_chip->base = p->config.gpio_base; 400119f5e44SMagnus Damm gpio_chip->ngpio = p->config.number_of_pins; 401119f5e44SMagnus Damm 402119f5e44SMagnus Damm irq_chip = &p->irq_chip; 403119f5e44SMagnus Damm irq_chip->name = name; 404119f5e44SMagnus Damm irq_chip->irq_mask = gpio_rcar_irq_disable; 405119f5e44SMagnus Damm irq_chip->irq_unmask = gpio_rcar_irq_enable; 406119f5e44SMagnus Damm irq_chip->irq_set_type = gpio_rcar_irq_set_type; 40740396112SMagnus Damm irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED 40840396112SMagnus Damm | IRQCHIP_MASK_ON_SUSPEND; 409119f5e44SMagnus Damm 410c7f3c5d3SGeert Uytterhoeven ret = gpiochip_add(gpio_chip); 411c7f3c5d3SGeert Uytterhoeven if (ret) { 412c7f3c5d3SGeert Uytterhoeven dev_err(dev, "failed to add GPIO controller\n"); 4130c8aab8eSDan Carpenter goto err0; 414119f5e44SMagnus Damm } 415119f5e44SMagnus Damm 4164d84b9e4SGeert Uytterhoeven ret = gpiochip_irqchip_add(gpio_chip, irq_chip, p->config.irq_base, 417c7f3c5d3SGeert Uytterhoeven handle_level_irq, IRQ_TYPE_NONE); 418c7f3c5d3SGeert Uytterhoeven if (ret) { 419c7f3c5d3SGeert Uytterhoeven dev_err(dev, "cannot add irqchip\n"); 420c7f3c5d3SGeert Uytterhoeven goto err1; 421c7f3c5d3SGeert Uytterhoeven } 422c7f3c5d3SGeert Uytterhoeven 423b22978fcSGeert Uytterhoeven if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler, 424b22978fcSGeert Uytterhoeven IRQF_SHARED, name, p)) { 425b22978fcSGeert Uytterhoeven dev_err(dev, "failed to request IRQ\n"); 426119f5e44SMagnus Damm ret = -ENOENT; 427119f5e44SMagnus Damm goto err1; 428119f5e44SMagnus Damm } 429119f5e44SMagnus Damm 430b22978fcSGeert Uytterhoeven dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins); 431119f5e44SMagnus Damm 432119f5e44SMagnus Damm /* warn in case of mismatch if irq base is specified */ 433119f5e44SMagnus Damm if (p->config.irq_base) { 4344d84b9e4SGeert Uytterhoeven ret = irq_find_mapping(gpio_chip->irqdomain, 0); 435119f5e44SMagnus Damm if (p->config.irq_base != ret) 436b22978fcSGeert Uytterhoeven dev_warn(dev, "irq base mismatch (%u/%u)\n", 437119f5e44SMagnus Damm p->config.irq_base, ret); 438119f5e44SMagnus Damm } 439119f5e44SMagnus Damm 440159f8a02SLaurent Pinchart if (p->config.pctl_name) { 441dc3465a9SLaurent Pinchart ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0, 442dc3465a9SLaurent Pinchart gpio_chip->base, gpio_chip->ngpio); 443dc3465a9SLaurent Pinchart if (ret < 0) 444b22978fcSGeert Uytterhoeven dev_warn(dev, "failed to add pin range\n"); 445159f8a02SLaurent Pinchart } 446dc3465a9SLaurent Pinchart 447119f5e44SMagnus Damm return 0; 448119f5e44SMagnus Damm 449119f5e44SMagnus Damm err1: 4504d84b9e4SGeert Uytterhoeven gpiochip_remove(gpio_chip); 451119f5e44SMagnus Damm err0: 452df0c6c80SGeert Uytterhoeven pm_runtime_put(dev); 453df0c6c80SGeert Uytterhoeven pm_runtime_disable(dev); 454119f5e44SMagnus Damm return ret; 455119f5e44SMagnus Damm } 456119f5e44SMagnus Damm 457119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev) 458119f5e44SMagnus Damm { 459119f5e44SMagnus Damm struct gpio_rcar_priv *p = platform_get_drvdata(pdev); 460119f5e44SMagnus Damm 4619f5132aeSabdoulaye berthe gpiochip_remove(&p->gpio_chip); 462119f5e44SMagnus Damm 463df0c6c80SGeert Uytterhoeven pm_runtime_put(&pdev->dev); 464df0c6c80SGeert Uytterhoeven pm_runtime_disable(&pdev->dev); 465119f5e44SMagnus Damm return 0; 466119f5e44SMagnus Damm } 467119f5e44SMagnus Damm 468119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = { 469119f5e44SMagnus Damm .probe = gpio_rcar_probe, 470119f5e44SMagnus Damm .remove = gpio_rcar_remove, 471119f5e44SMagnus Damm .driver = { 472119f5e44SMagnus Damm .name = "gpio_rcar", 473159f8a02SLaurent Pinchart .of_match_table = of_match_ptr(gpio_rcar_of_table), 474119f5e44SMagnus Damm } 475119f5e44SMagnus Damm }; 476119f5e44SMagnus Damm 477119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver); 478119f5e44SMagnus Damm 479119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm"); 480119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); 481119f5e44SMagnus Damm MODULE_LICENSE("GPL v2"); 482