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 #ifdef CONFIG_OF 283159f8a02SLaurent Pinchart struct device_node *np = p->pdev->dev.of_node; 284159f8a02SLaurent Pinchart struct of_phandle_args args; 285159f8a02SLaurent Pinchart int ret; 286159f8a02SLaurent Pinchart #endif 287159f8a02SLaurent Pinchart 288159f8a02SLaurent Pinchart if (pdata) 289159f8a02SLaurent Pinchart p->config = *pdata; 290159f8a02SLaurent Pinchart #ifdef CONFIG_OF 291159f8a02SLaurent Pinchart else if (np) { 292159f8a02SLaurent Pinchart ret = of_parse_phandle_with_args(np, "gpio-ranges", 293159f8a02SLaurent Pinchart "#gpio-range-cells", 0, &args); 294159f8a02SLaurent Pinchart p->config.number_of_pins = ret == 0 && args.args_count == 3 295159f8a02SLaurent Pinchart ? args.args[2] 296159f8a02SLaurent Pinchart : RCAR_MAX_GPIO_PER_BANK; 297159f8a02SLaurent Pinchart p->config.gpio_base = -1; 298159f8a02SLaurent Pinchart } 299159f8a02SLaurent Pinchart #endif 300159f8a02SLaurent Pinchart 301159f8a02SLaurent Pinchart if (p->config.number_of_pins == 0 || 302159f8a02SLaurent Pinchart p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) { 303159f8a02SLaurent Pinchart dev_warn(&p->pdev->dev, 304159f8a02SLaurent Pinchart "Invalid number of gpio lines %u, using %u\n", 305159f8a02SLaurent Pinchart p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK); 306159f8a02SLaurent Pinchart p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK; 307159f8a02SLaurent Pinchart } 308159f8a02SLaurent Pinchart } 309159f8a02SLaurent Pinchart 310119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev) 311119f5e44SMagnus Damm { 312119f5e44SMagnus Damm struct gpio_rcar_priv *p; 313119f5e44SMagnus Damm struct resource *io, *irq; 314119f5e44SMagnus Damm struct gpio_chip *gpio_chip; 315119f5e44SMagnus Damm struct irq_chip *irq_chip; 316119f5e44SMagnus Damm const char *name = dev_name(&pdev->dev); 317119f5e44SMagnus Damm int ret; 318119f5e44SMagnus Damm 319119f5e44SMagnus Damm p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); 320119f5e44SMagnus Damm if (!p) { 321119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to allocate driver data\n"); 322119f5e44SMagnus Damm ret = -ENOMEM; 323119f5e44SMagnus Damm goto err0; 324119f5e44SMagnus Damm } 325119f5e44SMagnus Damm 326119f5e44SMagnus Damm p->pdev = pdev; 327119f5e44SMagnus Damm spin_lock_init(&p->lock); 328119f5e44SMagnus Damm 329159f8a02SLaurent Pinchart /* Get device configuration from DT node or platform data. */ 330159f8a02SLaurent Pinchart gpio_rcar_parse_pdata(p); 331159f8a02SLaurent Pinchart 332159f8a02SLaurent Pinchart platform_set_drvdata(pdev, p); 333159f8a02SLaurent Pinchart 334119f5e44SMagnus Damm io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 335119f5e44SMagnus Damm irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 336119f5e44SMagnus Damm 337119f5e44SMagnus Damm if (!io || !irq) { 338119f5e44SMagnus Damm dev_err(&pdev->dev, "missing IRQ or IOMEM\n"); 339119f5e44SMagnus Damm ret = -EINVAL; 340119f5e44SMagnus Damm goto err0; 341119f5e44SMagnus Damm } 342119f5e44SMagnus Damm 343119f5e44SMagnus Damm p->base = devm_ioremap_nocache(&pdev->dev, io->start, 344119f5e44SMagnus Damm resource_size(io)); 345119f5e44SMagnus Damm if (!p->base) { 346119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to remap I/O memory\n"); 347119f5e44SMagnus Damm ret = -ENXIO; 348119f5e44SMagnus Damm goto err0; 349119f5e44SMagnus Damm } 350119f5e44SMagnus Damm 351119f5e44SMagnus Damm gpio_chip = &p->gpio_chip; 352dc3465a9SLaurent Pinchart gpio_chip->request = gpio_rcar_request; 353dc3465a9SLaurent Pinchart gpio_chip->free = gpio_rcar_free; 354119f5e44SMagnus Damm gpio_chip->direction_input = gpio_rcar_direction_input; 355119f5e44SMagnus Damm gpio_chip->get = gpio_rcar_get; 356119f5e44SMagnus Damm gpio_chip->direction_output = gpio_rcar_direction_output; 357119f5e44SMagnus Damm gpio_chip->set = gpio_rcar_set; 358119f5e44SMagnus Damm gpio_chip->to_irq = gpio_rcar_to_irq; 359119f5e44SMagnus Damm gpio_chip->label = name; 360159f8a02SLaurent Pinchart gpio_chip->dev = &pdev->dev; 361119f5e44SMagnus Damm gpio_chip->owner = THIS_MODULE; 362119f5e44SMagnus Damm gpio_chip->base = p->config.gpio_base; 363119f5e44SMagnus Damm gpio_chip->ngpio = p->config.number_of_pins; 364119f5e44SMagnus Damm 365119f5e44SMagnus Damm irq_chip = &p->irq_chip; 366119f5e44SMagnus Damm irq_chip->name = name; 367119f5e44SMagnus Damm irq_chip->irq_mask = gpio_rcar_irq_disable; 368119f5e44SMagnus Damm irq_chip->irq_unmask = gpio_rcar_irq_enable; 369119f5e44SMagnus Damm irq_chip->irq_enable = gpio_rcar_irq_enable; 370119f5e44SMagnus Damm irq_chip->irq_disable = gpio_rcar_irq_disable; 371119f5e44SMagnus Damm irq_chip->irq_set_type = gpio_rcar_irq_set_type; 372119f5e44SMagnus Damm irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED; 373119f5e44SMagnus Damm 374119f5e44SMagnus Damm p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, 375119f5e44SMagnus Damm p->config.number_of_pins, 376119f5e44SMagnus Damm p->config.irq_base, 377119f5e44SMagnus Damm &gpio_rcar_irq_domain_ops, p); 378119f5e44SMagnus Damm if (!p->irq_domain) { 379119f5e44SMagnus Damm ret = -ENXIO; 380119f5e44SMagnus Damm dev_err(&pdev->dev, "cannot initialize irq domain\n"); 381119f5e44SMagnus Damm goto err1; 382119f5e44SMagnus Damm } 383119f5e44SMagnus Damm 384119f5e44SMagnus Damm if (devm_request_irq(&pdev->dev, irq->start, 385c234962bSKuninori Morimoto gpio_rcar_irq_handler, IRQF_SHARED, name, p)) { 386119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to request IRQ\n"); 387119f5e44SMagnus Damm ret = -ENOENT; 388119f5e44SMagnus Damm goto err1; 389119f5e44SMagnus Damm } 390119f5e44SMagnus Damm 391119f5e44SMagnus Damm ret = gpiochip_add(gpio_chip); 392119f5e44SMagnus Damm if (ret) { 393119f5e44SMagnus Damm dev_err(&pdev->dev, "failed to add GPIO controller\n"); 394119f5e44SMagnus Damm goto err1; 395119f5e44SMagnus Damm } 396119f5e44SMagnus Damm 397119f5e44SMagnus Damm dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins); 398119f5e44SMagnus Damm 399119f5e44SMagnus Damm /* warn in case of mismatch if irq base is specified */ 400119f5e44SMagnus Damm if (p->config.irq_base) { 401119f5e44SMagnus Damm ret = irq_find_mapping(p->irq_domain, 0); 402119f5e44SMagnus Damm if (p->config.irq_base != ret) 403119f5e44SMagnus Damm dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n", 404119f5e44SMagnus Damm p->config.irq_base, ret); 405119f5e44SMagnus Damm } 406119f5e44SMagnus Damm 407159f8a02SLaurent Pinchart if (p->config.pctl_name) { 408dc3465a9SLaurent Pinchart ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0, 409dc3465a9SLaurent Pinchart gpio_chip->base, gpio_chip->ngpio); 410dc3465a9SLaurent Pinchart if (ret < 0) 411dc3465a9SLaurent Pinchart dev_warn(&pdev->dev, "failed to add pin range\n"); 412159f8a02SLaurent Pinchart } 413dc3465a9SLaurent Pinchart 414119f5e44SMagnus Damm return 0; 415119f5e44SMagnus Damm 416119f5e44SMagnus Damm err1: 417119f5e44SMagnus Damm irq_domain_remove(p->irq_domain); 418119f5e44SMagnus Damm err0: 419119f5e44SMagnus Damm return ret; 420119f5e44SMagnus Damm } 421119f5e44SMagnus Damm 422119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev) 423119f5e44SMagnus Damm { 424119f5e44SMagnus Damm struct gpio_rcar_priv *p = platform_get_drvdata(pdev); 425119f5e44SMagnus Damm int ret; 426119f5e44SMagnus Damm 427119f5e44SMagnus Damm ret = gpiochip_remove(&p->gpio_chip); 428119f5e44SMagnus Damm if (ret) 429119f5e44SMagnus Damm return ret; 430119f5e44SMagnus Damm 431119f5e44SMagnus Damm irq_domain_remove(p->irq_domain); 432119f5e44SMagnus Damm return 0; 433119f5e44SMagnus Damm } 434119f5e44SMagnus Damm 435159f8a02SLaurent Pinchart #ifdef CONFIG_OF 436159f8a02SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = { 437159f8a02SLaurent Pinchart { 438159f8a02SLaurent Pinchart .compatible = "renesas,gpio-rcar", 439159f8a02SLaurent Pinchart }, 440159f8a02SLaurent Pinchart }; 441159f8a02SLaurent Pinchart 442159f8a02SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table); 443159f8a02SLaurent Pinchart #endif 444159f8a02SLaurent Pinchart 445119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = { 446119f5e44SMagnus Damm .probe = gpio_rcar_probe, 447119f5e44SMagnus Damm .remove = gpio_rcar_remove, 448119f5e44SMagnus Damm .driver = { 449119f5e44SMagnus Damm .name = "gpio_rcar", 450159f8a02SLaurent Pinchart .of_match_table = of_match_ptr(gpio_rcar_of_table), 451119f5e44SMagnus Damm } 452119f5e44SMagnus Damm }; 453119f5e44SMagnus Damm 454119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver); 455119f5e44SMagnus Damm 456119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm"); 457119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver"); 458119f5e44SMagnus Damm MODULE_LICENSE("GPL v2"); 459