1119f5e44SMagnus Damm /* 2119f5e44SMagnus Damm * Renesas R-Car GPIO Support 3119f5e44SMagnus Damm * 4119f5e44SMagnus Damm * Copyright (C) 2013 Magnus Damm 5119f5e44SMagnus Damm * 6119f5e44SMagnus Damm * This program is free software; you can redistribute it and/or modify 7119f5e44SMagnus Damm * it under the terms of the GNU General Public License as published by 8119f5e44SMagnus Damm * the Free Software Foundation; either version 2 of the License 9119f5e44SMagnus Damm * 10119f5e44SMagnus Damm * This program is distributed in the hope that it will be useful, 11119f5e44SMagnus Damm * but WITHOUT ANY WARRANTY; without even the implied warranty of 12119f5e44SMagnus Damm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13119f5e44SMagnus Damm * GNU General Public License for more details. 14119f5e44SMagnus Damm */ 15119f5e44SMagnus Damm 16119f5e44SMagnus Damm #include <linux/err.h> 17119f5e44SMagnus Damm #include <linux/gpio.h> 18119f5e44SMagnus Damm #include <linux/init.h> 19119f5e44SMagnus Damm #include <linux/interrupt.h> 20119f5e44SMagnus Damm #include <linux/io.h> 21119f5e44SMagnus Damm #include <linux/ioport.h> 22119f5e44SMagnus Damm #include <linux/irq.h> 23119f5e44SMagnus Damm #include <linux/irqdomain.h> 24119f5e44SMagnus Damm #include <linux/module.h> 25dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h> 26119f5e44SMagnus Damm #include <linux/platform_data/gpio-rcar.h> 27119f5e44SMagnus Damm #include <linux/platform_device.h> 28119f5e44SMagnus Damm #include <linux/spinlock.h> 29119f5e44SMagnus Damm #include <linux/slab.h> 30119f5e44SMagnus Damm 31119f5e44SMagnus Damm struct gpio_rcar_priv { 32119f5e44SMagnus Damm void __iomem *base; 33119f5e44SMagnus Damm spinlock_t lock; 34119f5e44SMagnus Damm struct gpio_rcar_config config; 35119f5e44SMagnus Damm struct platform_device *pdev; 36119f5e44SMagnus Damm struct gpio_chip gpio_chip; 37119f5e44SMagnus Damm struct irq_chip irq_chip; 38119f5e44SMagnus Damm struct irq_domain *irq_domain; 39119f5e44SMagnus Damm }; 40119f5e44SMagnus Damm 41119f5e44SMagnus Damm #define IOINTSEL 0x00 42119f5e44SMagnus Damm #define INOUTSEL 0x04 43119f5e44SMagnus Damm #define OUTDT 0x08 44119f5e44SMagnus Damm #define INDT 0x0c 45119f5e44SMagnus Damm #define INTDT 0x10 46119f5e44SMagnus Damm #define INTCLR 0x14 47119f5e44SMagnus Damm #define INTMSK 0x18 48119f5e44SMagnus Damm #define MSKCLR 0x1c 49119f5e44SMagnus Damm #define POSNEG 0x20 50119f5e44SMagnus Damm #define EDGLEVEL 0x24 51119f5e44SMagnus Damm #define FILONOFF 0x28 527e1092b5SSimon Horman #define BOTHEDGE 0x4c 53119f5e44SMagnus Damm 54159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK 32 55159f8a02SLaurent Pinchart 56119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs) 57119f5e44SMagnus Damm { 58119f5e44SMagnus Damm return ioread32(p->base + offs); 59119f5e44SMagnus Damm } 60119f5e44SMagnus Damm 61119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs, 62119f5e44SMagnus Damm u32 value) 63119f5e44SMagnus Damm { 64119f5e44SMagnus Damm iowrite32(value, p->base + offs); 65119f5e44SMagnus Damm } 66119f5e44SMagnus Damm 67119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs, 68119f5e44SMagnus Damm int bit, bool value) 69119f5e44SMagnus Damm { 70119f5e44SMagnus Damm u32 tmp = gpio_rcar_read(p, offs); 71119f5e44SMagnus Damm 72119f5e44SMagnus Damm if (value) 73119f5e44SMagnus Damm tmp |= BIT(bit); 74119f5e44SMagnus Damm else 75119f5e44SMagnus Damm tmp &= ~BIT(bit); 76119f5e44SMagnus Damm 77119f5e44SMagnus Damm gpio_rcar_write(p, offs, tmp); 78119f5e44SMagnus Damm } 79119f5e44SMagnus Damm 80119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d) 81119f5e44SMagnus Damm { 82119f5e44SMagnus Damm struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d); 83119f5e44SMagnus Damm 84119f5e44SMagnus Damm gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d))); 85119f5e44SMagnus Damm } 86119f5e44SMagnus Damm 87119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d) 88119f5e44SMagnus Damm { 89119f5e44SMagnus Damm struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d); 90119f5e44SMagnus Damm 91119f5e44SMagnus Damm gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d))); 92119f5e44SMagnus Damm } 93119f5e44SMagnus Damm 94119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p, 95119f5e44SMagnus Damm unsigned int hwirq, 96119f5e44SMagnus Damm bool active_high_rising_edge, 977e1092b5SSimon Horman bool level_trigger, 987e1092b5SSimon Horman bool both) 99119f5e44SMagnus Damm { 100119f5e44SMagnus Damm unsigned long flags; 101119f5e44SMagnus Damm 102119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 103119f5e44SMagnus Damm * "Setting Edge-Sensitive Interrupt Input Mode" and 104119f5e44SMagnus Damm * "Setting Level-Sensitive Interrupt Input Mode" 105119f5e44SMagnus Damm */ 106119f5e44SMagnus Damm 107119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 108119f5e44SMagnus Damm 109119f5e44SMagnus Damm /* Configure postive or negative logic in POSNEG */ 110119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge); 111119f5e44SMagnus Damm 112119f5e44SMagnus Damm /* Configure edge or level trigger in EDGLEVEL */ 113119f5e44SMagnus Damm gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger); 114119f5e44SMagnus Damm 1157e1092b5SSimon Horman /* Select one edge or both edges in BOTHEDGE */ 1167e1092b5SSimon Horman if (p->config.has_both_edge_trigger) 1177e1092b5SSimon Horman gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both); 1187e1092b5SSimon Horman 119119f5e44SMagnus Damm /* Select "Interrupt Input Mode" in IOINTSEL */ 120119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true); 121119f5e44SMagnus Damm 122119f5e44SMagnus Damm /* Write INTCLR in case of edge trigger */ 123119f5e44SMagnus Damm if (!level_trigger) 124119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(hwirq)); 125119f5e44SMagnus Damm 126119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 127119f5e44SMagnus Damm } 128119f5e44SMagnus Damm 129119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type) 130119f5e44SMagnus Damm { 131119f5e44SMagnus Damm struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d); 132119f5e44SMagnus Damm unsigned int hwirq = irqd_to_hwirq(d); 133119f5e44SMagnus Damm 134119f5e44SMagnus Damm dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type); 135119f5e44SMagnus Damm 136119f5e44SMagnus Damm switch (type & IRQ_TYPE_SENSE_MASK) { 137119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_HIGH: 1387e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true, 1397e1092b5SSimon Horman false); 140119f5e44SMagnus Damm break; 141119f5e44SMagnus Damm case IRQ_TYPE_LEVEL_LOW: 1427e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true, 1437e1092b5SSimon Horman false); 144119f5e44SMagnus Damm break; 145119f5e44SMagnus Damm case IRQ_TYPE_EDGE_RISING: 1467e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1477e1092b5SSimon Horman false); 148119f5e44SMagnus Damm break; 149119f5e44SMagnus Damm case IRQ_TYPE_EDGE_FALLING: 1507e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false, 1517e1092b5SSimon Horman false); 1527e1092b5SSimon Horman break; 1537e1092b5SSimon Horman case IRQ_TYPE_EDGE_BOTH: 1547e1092b5SSimon Horman if (!p->config.has_both_edge_trigger) 1557e1092b5SSimon Horman return -EINVAL; 1567e1092b5SSimon Horman gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false, 1577e1092b5SSimon Horman true); 158119f5e44SMagnus Damm break; 159119f5e44SMagnus Damm default: 160119f5e44SMagnus Damm return -EINVAL; 161119f5e44SMagnus Damm } 162119f5e44SMagnus Damm return 0; 163119f5e44SMagnus Damm } 164119f5e44SMagnus Damm 165119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) 166119f5e44SMagnus Damm { 167119f5e44SMagnus Damm struct gpio_rcar_priv *p = dev_id; 168119f5e44SMagnus Damm u32 pending; 169119f5e44SMagnus Damm unsigned int offset, irqs_handled = 0; 170119f5e44SMagnus Damm 171119f5e44SMagnus Damm while ((pending = gpio_rcar_read(p, INTDT))) { 172119f5e44SMagnus Damm offset = __ffs(pending); 173119f5e44SMagnus Damm gpio_rcar_write(p, INTCLR, BIT(offset)); 174119f5e44SMagnus Damm generic_handle_irq(irq_find_mapping(p->irq_domain, offset)); 175119f5e44SMagnus Damm irqs_handled++; 176119f5e44SMagnus Damm } 177119f5e44SMagnus Damm 178119f5e44SMagnus Damm return irqs_handled ? IRQ_HANDLED : IRQ_NONE; 179119f5e44SMagnus Damm } 180119f5e44SMagnus Damm 181119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip) 182119f5e44SMagnus Damm { 183119f5e44SMagnus Damm return container_of(chip, struct gpio_rcar_priv, gpio_chip); 184119f5e44SMagnus Damm } 185119f5e44SMagnus Damm 186119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip, 187119f5e44SMagnus Damm unsigned int gpio, 188119f5e44SMagnus Damm bool output) 189119f5e44SMagnus Damm { 190119f5e44SMagnus Damm struct gpio_rcar_priv *p = gpio_to_priv(chip); 191119f5e44SMagnus Damm unsigned long flags; 192119f5e44SMagnus Damm 193119f5e44SMagnus Damm /* follow steps in the GPIO documentation for 194119f5e44SMagnus Damm * "Setting General Output Mode" and 195119f5e44SMagnus Damm * "Setting General Input Mode" 196119f5e44SMagnus Damm */ 197119f5e44SMagnus Damm 198119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 199119f5e44SMagnus Damm 200119f5e44SMagnus Damm /* Configure postive logic in POSNEG */ 201119f5e44SMagnus Damm gpio_rcar_modify_bit(p, POSNEG, gpio, false); 202119f5e44SMagnus Damm 203119f5e44SMagnus Damm /* Select "General Input/Output Mode" in IOINTSEL */ 204119f5e44SMagnus Damm gpio_rcar_modify_bit(p, IOINTSEL, gpio, false); 205119f5e44SMagnus Damm 206119f5e44SMagnus Damm /* Select Input Mode or Output Mode in INOUTSEL */ 207119f5e44SMagnus Damm gpio_rcar_modify_bit(p, INOUTSEL, gpio, output); 208119f5e44SMagnus Damm 209119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 210119f5e44SMagnus Damm } 211119f5e44SMagnus Damm 212dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset) 213dc3465a9SLaurent Pinchart { 214dc3465a9SLaurent Pinchart return pinctrl_request_gpio(chip->base + offset); 215dc3465a9SLaurent Pinchart } 216dc3465a9SLaurent Pinchart 217dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset) 218dc3465a9SLaurent Pinchart { 219dc3465a9SLaurent Pinchart pinctrl_free_gpio(chip->base + offset); 220dc3465a9SLaurent Pinchart 221dc3465a9SLaurent Pinchart /* Set the GPIO as an input to ensure that the next GPIO request won't 222dc3465a9SLaurent Pinchart * drive the GPIO pin as an output. 223dc3465a9SLaurent Pinchart */ 224dc3465a9SLaurent Pinchart gpio_rcar_config_general_input_output_mode(chip, offset, false); 225dc3465a9SLaurent Pinchart } 226dc3465a9SLaurent Pinchart 227119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset) 228119f5e44SMagnus Damm { 229119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, false); 230119f5e44SMagnus Damm return 0; 231119f5e44SMagnus Damm } 232119f5e44SMagnus Damm 233119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset) 234119f5e44SMagnus Damm { 235119f5e44SMagnus Damm return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & BIT(offset)); 236119f5e44SMagnus Damm } 237119f5e44SMagnus Damm 238119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value) 239119f5e44SMagnus Damm { 240119f5e44SMagnus Damm struct gpio_rcar_priv *p = gpio_to_priv(chip); 241119f5e44SMagnus Damm unsigned long flags; 242119f5e44SMagnus Damm 243119f5e44SMagnus Damm spin_lock_irqsave(&p->lock, flags); 244119f5e44SMagnus Damm gpio_rcar_modify_bit(p, OUTDT, offset, value); 245119f5e44SMagnus Damm spin_unlock_irqrestore(&p->lock, flags); 246119f5e44SMagnus Damm } 247119f5e44SMagnus Damm 248119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset, 249119f5e44SMagnus Damm int value) 250119f5e44SMagnus Damm { 251119f5e44SMagnus Damm /* write GPIO value to output before selecting output mode of pin */ 252119f5e44SMagnus Damm gpio_rcar_set(chip, offset, value); 253119f5e44SMagnus Damm gpio_rcar_config_general_input_output_mode(chip, offset, true); 254119f5e44SMagnus Damm return 0; 255119f5e44SMagnus Damm } 256119f5e44SMagnus Damm 257119f5e44SMagnus Damm static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset) 258119f5e44SMagnus Damm { 259119f5e44SMagnus Damm return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset); 260119f5e44SMagnus Damm } 261119f5e44SMagnus Damm 262119f5e44SMagnus Damm static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int virq, 263119f5e44SMagnus Damm irq_hw_number_t hw) 264119f5e44SMagnus Damm { 265119f5e44SMagnus Damm struct gpio_rcar_priv *p = h->host_data; 266119f5e44SMagnus Damm 267119f5e44SMagnus Damm dev_dbg(&p->pdev->dev, "map hw irq = %d, virq = %d\n", (int)hw, virq); 268119f5e44SMagnus Damm 269119f5e44SMagnus Damm irq_set_chip_data(virq, h->host_data); 270119f5e44SMagnus Damm irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq); 271119f5e44SMagnus Damm set_irq_flags(virq, IRQF_VALID); /* kill me now */ 272119f5e44SMagnus Damm return 0; 273119f5e44SMagnus Damm } 274119f5e44SMagnus Damm 275119f5e44SMagnus Damm static struct irq_domain_ops gpio_rcar_irq_domain_ops = { 276119f5e44SMagnus Damm .map = gpio_rcar_irq_domain_map, 277119f5e44SMagnus Damm }; 278119f5e44SMagnus Damm 279159f8a02SLaurent Pinchart static void gpio_rcar_parse_pdata(struct gpio_rcar_priv *p) 280159f8a02SLaurent Pinchart { 281159f8a02SLaurent Pinchart struct gpio_rcar_config *pdata = p->pdev->dev.platform_data; 282159f8a02SLaurent Pinchart struct device_node *np = p->pdev->dev.of_node; 283159f8a02SLaurent Pinchart struct of_phandle_args args; 284159f8a02SLaurent Pinchart int ret; 285159f8a02SLaurent Pinchart 286e305062eSLaurent Pinchart if (pdata) { 287159f8a02SLaurent Pinchart p->config = *pdata; 288e305062eSLaurent Pinchart } else if (IS_ENABLED(CONFIG_OF) && np) { 289159f8a02SLaurent Pinchart ret = of_parse_phandle_with_args(np, "gpio-ranges", 290159f8a02SLaurent Pinchart "#gpio-range-cells", 0, &args); 291159f8a02SLaurent Pinchart p->config.number_of_pins = ret == 0 && args.args_count == 3 292159f8a02SLaurent Pinchart ? args.args[2] 293159f8a02SLaurent Pinchart : RCAR_MAX_GPIO_PER_BANK; 294159f8a02SLaurent Pinchart p->config.gpio_base = -1; 295159f8a02SLaurent Pinchart } 296159f8a02SLaurent Pinchart 297159f8a02SLaurent Pinchart if (p->config.number_of_pins == 0 || 298159f8a02SLaurent Pinchart p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) { 299159f8a02SLaurent Pinchart dev_warn(&p->pdev->dev, 300159f8a02SLaurent Pinchart "Invalid number of gpio lines %u, using %u\n", 301159f8a02SLaurent Pinchart p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK); 302159f8a02SLaurent Pinchart p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK; 303159f8a02SLaurent Pinchart } 304159f8a02SLaurent Pinchart } 305159f8a02SLaurent Pinchart 306119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev) 307119f5e44SMagnus Damm { 308119f5e44SMagnus Damm struct gpio_rcar_priv *p; 309119f5e44SMagnus Damm struct resource *io, *irq; 310119f5e44SMagnus Damm struct gpio_chip *gpio_chip; 311119f5e44SMagnus Damm struct irq_chip *irq_chip; 312119f5e44SMagnus Damm const char *name = dev_name(&pdev->dev); 313119f5e44SMagnus Damm int ret; 314119f5e44SMagnus Damm 315119f5e44SMagnus Damm p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); 316119f5e44SMagnus Damm if (!p) { 317119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to allocate driver data\n"); 318119f5e44SMagnus Damm ret = -ENOMEM; 319119f5e44SMagnus Damm goto err0; 320119f5e44SMagnus Damm } 321119f5e44SMagnus Damm 322119f5e44SMagnus Damm p->pdev = pdev; 323119f5e44SMagnus Damm spin_lock_init(&p->lock); 324119f5e44SMagnus Damm 325159f8a02SLaurent Pinchart /* Get device configuration from DT node or platform data. */ 326159f8a02SLaurent Pinchart gpio_rcar_parse_pdata(p); 327159f8a02SLaurent Pinchart 328159f8a02SLaurent Pinchart platform_set_drvdata(pdev, p); 329159f8a02SLaurent Pinchart 330119f5e44SMagnus Damm io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 331119f5e44SMagnus Damm irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 332119f5e44SMagnus Damm 333119f5e44SMagnus Damm if (!io || !irq) { 334119f5e44SMagnus Damm dev_err(&pdev->dev, "missing IRQ or IOMEM\n"); 335119f5e44SMagnus Damm ret = -EINVAL; 336119f5e44SMagnus Damm goto err0; 337119f5e44SMagnus Damm } 338119f5e44SMagnus Damm 339119f5e44SMagnus Damm p->base = devm_ioremap_nocache(&pdev->dev, io->start, 340119f5e44SMagnus Damm resource_size(io)); 341119f5e44SMagnus Damm if (!p->base) { 342119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to remap I/O memory\n"); 343119f5e44SMagnus Damm ret = -ENXIO; 344119f5e44SMagnus Damm goto err0; 345119f5e44SMagnus Damm } 346119f5e44SMagnus Damm 347119f5e44SMagnus Damm gpio_chip = &p->gpio_chip; 348dc3465a9SLaurent Pinchart gpio_chip->request = gpio_rcar_request; 349dc3465a9SLaurent Pinchart gpio_chip->free = gpio_rcar_free; 350119f5e44SMagnus Damm gpio_chip->direction_input = gpio_rcar_direction_input; 351119f5e44SMagnus Damm gpio_chip->get = gpio_rcar_get; 352119f5e44SMagnus Damm gpio_chip->direction_output = gpio_rcar_direction_output; 353119f5e44SMagnus Damm gpio_chip->set = gpio_rcar_set; 354119f5e44SMagnus Damm gpio_chip->to_irq = gpio_rcar_to_irq; 355119f5e44SMagnus Damm gpio_chip->label = name; 356159f8a02SLaurent Pinchart gpio_chip->dev = &pdev->dev; 357119f5e44SMagnus Damm gpio_chip->owner = THIS_MODULE; 358119f5e44SMagnus Damm gpio_chip->base = p->config.gpio_base; 359119f5e44SMagnus Damm gpio_chip->ngpio = p->config.number_of_pins; 360119f5e44SMagnus Damm 361119f5e44SMagnus Damm irq_chip = &p->irq_chip; 362119f5e44SMagnus Damm irq_chip->name = name; 363119f5e44SMagnus Damm irq_chip->irq_mask = gpio_rcar_irq_disable; 364119f5e44SMagnus Damm irq_chip->irq_unmask = gpio_rcar_irq_enable; 365119f5e44SMagnus Damm irq_chip->irq_enable = gpio_rcar_irq_enable; 366119f5e44SMagnus Damm irq_chip->irq_disable = gpio_rcar_irq_disable; 367119f5e44SMagnus Damm irq_chip->irq_set_type = gpio_rcar_irq_set_type; 368119f5e44SMagnus Damm irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED; 369119f5e44SMagnus Damm 370119f5e44SMagnus Damm p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, 371119f5e44SMagnus Damm p->config.number_of_pins, 372119f5e44SMagnus Damm p->config.irq_base, 373119f5e44SMagnus Damm &gpio_rcar_irq_domain_ops, p); 374119f5e44SMagnus Damm if (!p->irq_domain) { 375119f5e44SMagnus Damm ret = -ENXIO; 376119f5e44SMagnus Damm dev_err(&pdev->dev, "cannot initialize irq domain\n"); 377119f5e44SMagnus Damm goto err1; 378119f5e44SMagnus Damm } 379119f5e44SMagnus Damm 380119f5e44SMagnus Damm if (devm_request_irq(&pdev->dev, irq->start, 381c234962bSKuninori Morimoto gpio_rcar_irq_handler, IRQF_SHARED, name, p)) { 382119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to request IRQ\n"); 383119f5e44SMagnus Damm ret = -ENOENT; 384119f5e44SMagnus Damm goto err1; 385119f5e44SMagnus Damm } 386119f5e44SMagnus Damm 387119f5e44SMagnus Damm ret = gpiochip_add(gpio_chip); 388119f5e44SMagnus Damm if (ret) { 389119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to add GPIO controller\n"); 390119f5e44SMagnus Damm goto err1; 391119f5e44SMagnus Damm } 392119f5e44SMagnus Damm 393119f5e44SMagnus Damm dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins); 394119f5e44SMagnus Damm 395119f5e44SMagnus Damm /* warn in case of mismatch if irq base is specified */ 396119f5e44SMagnus Damm if (p->config.irq_base) { 397119f5e44SMagnus Damm ret = irq_find_mapping(p->irq_domain, 0); 398119f5e44SMagnus Damm if (p->config.irq_base != ret) 399119f5e44SMagnus Damm dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n", 400119f5e44SMagnus Damm p->config.irq_base, ret); 401119f5e44SMagnus Damm } 402119f5e44SMagnus Damm 403159f8a02SLaurent Pinchart if (p->config.pctl_name) { 404dc3465a9SLaurent Pinchart ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0, 405dc3465a9SLaurent Pinchart gpio_chip->base, gpio_chip->ngpio); 406dc3465a9SLaurent Pinchart if (ret < 0) 407dc3465a9SLaurent Pinchart dev_warn(&pdev->dev, "failed to add pin range\n"); 408159f8a02SLaurent Pinchart } 409dc3465a9SLaurent Pinchart 410119f5e44SMagnus Damm return 0; 411119f5e44SMagnus Damm 412119f5e44SMagnus Damm err1: 413119f5e44SMagnus Damm irq_domain_remove(p->irq_domain); 414119f5e44SMagnus Damm err0: 415119f5e44SMagnus Damm return ret; 416119f5e44SMagnus Damm } 417119f5e44SMagnus Damm 418119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev) 419119f5e44SMagnus Damm { 420119f5e44SMagnus Damm struct gpio_rcar_priv *p = platform_get_drvdata(pdev); 421119f5e44SMagnus Damm int ret; 422119f5e44SMagnus Damm 423119f5e44SMagnus Damm ret = gpiochip_remove(&p->gpio_chip); 424119f5e44SMagnus Damm if (ret) 425119f5e44SMagnus Damm return ret; 426119f5e44SMagnus Damm 427119f5e44SMagnus Damm irq_domain_remove(p->irq_domain); 428119f5e44SMagnus Damm return 0; 429119f5e44SMagnus Damm } 430119f5e44SMagnus Damm 431159f8a02SLaurent Pinchart #ifdef CONFIG_OF 432159f8a02SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = { 433159f8a02SLaurent Pinchart { 434159f8a02SLaurent Pinchart .compatible = "renesas,gpio-rcar", 435159f8a02SLaurent Pinchart }, 436159f8a02SLaurent Pinchart }; 437159f8a02SLaurent Pinchart 438159f8a02SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); 439159f8a02SLaurent Pinchart #endif 440159f8a02SLaurent Pinchart 441119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = { 442119f5e44SMagnus Damm .probe = gpio_rcar_probe, 443119f5e44SMagnus Damm .remove = gpio_rcar_remove, 444119f5e44SMagnus Damm .driver = { 445119f5e44SMagnus Damm .name = "gpio_rcar", 446159f8a02SLaurent Pinchart .of_match_table = of_match_ptr(gpio_rcar_of_table), 447119f5e44SMagnus Damm } 448119f5e44SMagnus Damm }; 449119f5e44SMagnus Damm 450119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver); 451119f5e44SMagnus Damm 452119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm"); 453119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); 454119f5e44SMagnus Damm MODULE_LICENSE("GPL v2"); 455