of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / pinctrl / bcm / pinctrl-cygnus-gpio.c
blob12a48f498b75847926ab7bc96ecd78fdd248971a
1 /*
2 * Copyright (C) 2014-2015 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * This file contains the Broadcom Cygnus GPIO driver that supports 3
14 * GPIO controllers on Cygnus including the ASIU GPIO controller, the
15 * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
16 * PINCONF such as bias pull up/down, and drive strength are also supported
17 * in this driver.
19 * Pins from the ASIU GPIO can be individually muxed to GPIO function,
20 * through the interaction with the Cygnus IOMUX controller
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/gpio.h>
28 #include <linux/ioport.h>
29 #include <linux/of_device.h>
30 #include <linux/of_irq.h>
31 #include <linux/pinctrl/pinctrl.h>
32 #include <linux/pinctrl/pinconf.h>
33 #include <linux/pinctrl/pinconf-generic.h>
35 #include "../pinctrl-utils.h"
37 #define CYGNUS_GPIO_DATA_IN_OFFSET 0x00
38 #define CYGNUS_GPIO_DATA_OUT_OFFSET 0x04
39 #define CYGNUS_GPIO_OUT_EN_OFFSET 0x08
40 #define CYGNUS_GPIO_INT_TYPE_OFFSET 0x0c
41 #define CYGNUS_GPIO_INT_DE_OFFSET 0x10
42 #define CYGNUS_GPIO_INT_EDGE_OFFSET 0x14
43 #define CYGNUS_GPIO_INT_MSK_OFFSET 0x18
44 #define CYGNUS_GPIO_INT_STAT_OFFSET 0x1c
45 #define CYGNUS_GPIO_INT_MSTAT_OFFSET 0x20
46 #define CYGNUS_GPIO_INT_CLR_OFFSET 0x24
47 #define CYGNUS_GPIO_PAD_RES_OFFSET 0x34
48 #define CYGNUS_GPIO_RES_EN_OFFSET 0x38
50 /* drive strength control for ASIU GPIO */
51 #define CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
53 /* drive strength control for CCM/CRMU (AON) GPIO */
54 #define CYGNUS_GPIO_DRV0_CTRL_OFFSET 0x00
56 #define GPIO_BANK_SIZE 0x200
57 #define NGPIOS_PER_BANK 32
58 #define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
60 #define CYGNUS_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
61 #define CYGNUS_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
63 #define GPIO_DRV_STRENGTH_BIT_SHIFT 20
64 #define GPIO_DRV_STRENGTH_BITS 3
65 #define GPIO_DRV_STRENGTH_BIT_MASK ((1 << GPIO_DRV_STRENGTH_BITS) - 1)
68 * Cygnus GPIO core
70 * @dev: pointer to device
71 * @base: I/O register base for Cygnus GPIO controller
72 * @io_ctrl: I/O register base for certain type of Cygnus GPIO controller that
73 * has the PINCONF support implemented outside of the GPIO block
74 * @lock: lock to protect access to I/O registers
75 * @gc: GPIO chip
76 * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
77 * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
78 * that can be individually muxed to GPIO
79 * @pctl: pointer to pinctrl_dev
80 * @pctldesc: pinctrl descriptor
82 struct cygnus_gpio {
83 struct device *dev;
85 void __iomem *base;
86 void __iomem *io_ctrl;
88 spinlock_t lock;
90 struct gpio_chip gc;
91 unsigned num_banks;
93 bool pinmux_is_supported;
95 struct pinctrl_dev *pctl;
96 struct pinctrl_desc pctldesc;
99 static inline struct cygnus_gpio *to_cygnus_gpio(struct gpio_chip *gc)
101 return container_of(gc, struct cygnus_gpio, gc);
105 * Mapping from PINCONF pins to GPIO pins is 1-to-1
107 static inline unsigned cygnus_pin_to_gpio(unsigned pin)
109 return pin;
113 * cygnus_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
114 * Cygnus GPIO register
116 * @cygnus_gpio: Cygnus GPIO device
117 * @reg: register offset
118 * @gpio: GPIO pin
119 * @set: set or clear
121 static inline void cygnus_set_bit(struct cygnus_gpio *chip, unsigned int reg,
122 unsigned gpio, bool set)
124 unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
125 unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
126 u32 val;
128 val = readl(chip->base + offset);
129 if (set)
130 val |= BIT(shift);
131 else
132 val &= ~BIT(shift);
133 writel(val, chip->base + offset);
136 static inline bool cygnus_get_bit(struct cygnus_gpio *chip, unsigned int reg,
137 unsigned gpio)
139 unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
140 unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
142 return !!(readl(chip->base + offset) & BIT(shift));
145 static void cygnus_gpio_irq_handler(struct irq_desc *desc)
147 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
148 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
149 struct irq_chip *irq_chip = irq_desc_get_chip(desc);
150 int i, bit;
152 chained_irq_enter(irq_chip, desc);
154 /* go through the entire GPIO banks and handle all interrupts */
155 for (i = 0; i < chip->num_banks; i++) {
156 unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
157 CYGNUS_GPIO_INT_MSTAT_OFFSET);
159 for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
160 unsigned pin = NGPIOS_PER_BANK * i + bit;
161 int child_irq = irq_find_mapping(gc->irqdomain, pin);
164 * Clear the interrupt before invoking the
165 * handler, so we do not leave any window
167 writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
168 CYGNUS_GPIO_INT_CLR_OFFSET);
170 generic_handle_irq(child_irq);
174 chained_irq_exit(irq_chip, desc);
178 static void cygnus_gpio_irq_ack(struct irq_data *d)
180 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
181 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
182 unsigned gpio = d->hwirq;
183 unsigned int offset = CYGNUS_GPIO_REG(gpio,
184 CYGNUS_GPIO_INT_CLR_OFFSET);
185 unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
186 u32 val = BIT(shift);
188 writel(val, chip->base + offset);
192 * cygnus_gpio_irq_set_mask - mask/unmask a GPIO interrupt
194 * @d: IRQ chip data
195 * @unmask: mask/unmask GPIO interrupt
197 static void cygnus_gpio_irq_set_mask(struct irq_data *d, bool unmask)
199 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
200 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
201 unsigned gpio = d->hwirq;
203 cygnus_set_bit(chip, CYGNUS_GPIO_INT_MSK_OFFSET, gpio, unmask);
206 static void cygnus_gpio_irq_mask(struct irq_data *d)
208 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
209 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
210 unsigned long flags;
212 spin_lock_irqsave(&chip->lock, flags);
213 cygnus_gpio_irq_set_mask(d, false);
214 spin_unlock_irqrestore(&chip->lock, flags);
217 static void cygnus_gpio_irq_unmask(struct irq_data *d)
219 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
220 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
221 unsigned long flags;
223 spin_lock_irqsave(&chip->lock, flags);
224 cygnus_gpio_irq_set_mask(d, true);
225 spin_unlock_irqrestore(&chip->lock, flags);
228 static int cygnus_gpio_irq_set_type(struct irq_data *d, unsigned int type)
230 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
231 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
232 unsigned gpio = d->hwirq;
233 bool level_triggered = false;
234 bool dual_edge = false;
235 bool rising_or_high = false;
236 unsigned long flags;
238 switch (type & IRQ_TYPE_SENSE_MASK) {
239 case IRQ_TYPE_EDGE_RISING:
240 rising_or_high = true;
241 break;
243 case IRQ_TYPE_EDGE_FALLING:
244 break;
246 case IRQ_TYPE_EDGE_BOTH:
247 dual_edge = true;
248 break;
250 case IRQ_TYPE_LEVEL_HIGH:
251 level_triggered = true;
252 rising_or_high = true;
253 break;
255 case IRQ_TYPE_LEVEL_LOW:
256 level_triggered = true;
257 break;
259 default:
260 dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
261 type);
262 return -EINVAL;
265 spin_lock_irqsave(&chip->lock, flags);
266 cygnus_set_bit(chip, CYGNUS_GPIO_INT_TYPE_OFFSET, gpio,
267 level_triggered);
268 cygnus_set_bit(chip, CYGNUS_GPIO_INT_DE_OFFSET, gpio, dual_edge);
269 cygnus_set_bit(chip, CYGNUS_GPIO_INT_EDGE_OFFSET, gpio,
270 rising_or_high);
271 spin_unlock_irqrestore(&chip->lock, flags);
273 dev_dbg(chip->dev,
274 "gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
275 gpio, level_triggered, dual_edge, rising_or_high);
277 return 0;
280 static struct irq_chip cygnus_gpio_irq_chip = {
281 .name = "bcm-cygnus-gpio",
282 .irq_ack = cygnus_gpio_irq_ack,
283 .irq_mask = cygnus_gpio_irq_mask,
284 .irq_unmask = cygnus_gpio_irq_unmask,
285 .irq_set_type = cygnus_gpio_irq_set_type,
289 * Request the Cygnus IOMUX pinmux controller to mux individual pins to GPIO
291 static int cygnus_gpio_request(struct gpio_chip *gc, unsigned offset)
293 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
294 unsigned gpio = gc->base + offset;
296 /* not all Cygnus GPIO pins can be muxed individually */
297 if (!chip->pinmux_is_supported)
298 return 0;
300 return pinctrl_request_gpio(gpio);
303 static void cygnus_gpio_free(struct gpio_chip *gc, unsigned offset)
305 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
306 unsigned gpio = gc->base + offset;
308 if (!chip->pinmux_is_supported)
309 return;
311 pinctrl_free_gpio(gpio);
314 static int cygnus_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
316 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
317 unsigned long flags;
319 spin_lock_irqsave(&chip->lock, flags);
320 cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, false);
321 spin_unlock_irqrestore(&chip->lock, flags);
323 dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
325 return 0;
328 static int cygnus_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
329 int val)
331 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
332 unsigned long flags;
334 spin_lock_irqsave(&chip->lock, flags);
335 cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, true);
336 cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
337 spin_unlock_irqrestore(&chip->lock, flags);
339 dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
341 return 0;
344 static void cygnus_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
346 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
347 unsigned long flags;
349 spin_lock_irqsave(&chip->lock, flags);
350 cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
351 spin_unlock_irqrestore(&chip->lock, flags);
353 dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
356 static int cygnus_gpio_get(struct gpio_chip *gc, unsigned gpio)
358 struct cygnus_gpio *chip = to_cygnus_gpio(gc);
359 unsigned int offset = CYGNUS_GPIO_REG(gpio,
360 CYGNUS_GPIO_DATA_IN_OFFSET);
361 unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
363 return !!(readl(chip->base + offset) & BIT(shift));
366 static int cygnus_get_groups_count(struct pinctrl_dev *pctldev)
368 return 1;
372 * Only one group: "gpio_grp", since this local pinctrl device only performs
373 * GPIO specific PINCONF configurations
375 static const char *cygnus_get_group_name(struct pinctrl_dev *pctldev,
376 unsigned selector)
378 return "gpio_grp";
381 static const struct pinctrl_ops cygnus_pctrl_ops = {
382 .get_groups_count = cygnus_get_groups_count,
383 .get_group_name = cygnus_get_group_name,
384 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
385 .dt_free_map = pinctrl_utils_dt_free_map,
388 static int cygnus_gpio_set_pull(struct cygnus_gpio *chip, unsigned gpio,
389 bool disable, bool pull_up)
391 unsigned long flags;
393 spin_lock_irqsave(&chip->lock, flags);
395 if (disable) {
396 cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, false);
397 } else {
398 cygnus_set_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio,
399 pull_up);
400 cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, true);
403 spin_unlock_irqrestore(&chip->lock, flags);
405 dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);
407 return 0;
410 static void cygnus_gpio_get_pull(struct cygnus_gpio *chip, unsigned gpio,
411 bool *disable, bool *pull_up)
413 unsigned long flags;
415 spin_lock_irqsave(&chip->lock, flags);
416 *disable = !cygnus_get_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio);
417 *pull_up = cygnus_get_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio);
418 spin_unlock_irqrestore(&chip->lock, flags);
421 static int cygnus_gpio_set_strength(struct cygnus_gpio *chip, unsigned gpio,
422 unsigned strength)
424 void __iomem *base;
425 unsigned int i, offset, shift;
426 u32 val;
427 unsigned long flags;
429 /* make sure drive strength is supported */
430 if (strength < 2 || strength > 16 || (strength % 2))
431 return -ENOTSUPP;
433 if (chip->io_ctrl) {
434 base = chip->io_ctrl;
435 offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
436 } else {
437 base = chip->base;
438 offset = CYGNUS_GPIO_REG(gpio,
439 CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
442 shift = CYGNUS_GPIO_SHIFT(gpio);
444 dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
445 strength);
447 spin_lock_irqsave(&chip->lock, flags);
448 strength = (strength / 2) - 1;
449 for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
450 val = readl(base + offset);
451 val &= ~BIT(shift);
452 val |= ((strength >> i) & 0x1) << shift;
453 writel(val, base + offset);
454 offset += 4;
456 spin_unlock_irqrestore(&chip->lock, flags);
458 return 0;
461 static int cygnus_gpio_get_strength(struct cygnus_gpio *chip, unsigned gpio,
462 u16 *strength)
464 void __iomem *base;
465 unsigned int i, offset, shift;
466 u32 val;
467 unsigned long flags;
469 if (chip->io_ctrl) {
470 base = chip->io_ctrl;
471 offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
472 } else {
473 base = chip->base;
474 offset = CYGNUS_GPIO_REG(gpio,
475 CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
478 shift = CYGNUS_GPIO_SHIFT(gpio);
480 spin_lock_irqsave(&chip->lock, flags);
481 *strength = 0;
482 for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
483 val = readl(base + offset) & BIT(shift);
484 val >>= shift;
485 *strength += (val << i);
486 offset += 4;
489 /* convert to mA */
490 *strength = (*strength + 1) * 2;
491 spin_unlock_irqrestore(&chip->lock, flags);
493 return 0;
496 static int cygnus_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
497 unsigned long *config)
499 struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
500 enum pin_config_param param = pinconf_to_config_param(*config);
501 unsigned gpio = cygnus_pin_to_gpio(pin);
502 u16 arg;
503 bool disable, pull_up;
504 int ret;
506 switch (param) {
507 case PIN_CONFIG_BIAS_DISABLE:
508 cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
509 if (disable)
510 return 0;
511 else
512 return -EINVAL;
514 case PIN_CONFIG_BIAS_PULL_UP:
515 cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
516 if (!disable && pull_up)
517 return 0;
518 else
519 return -EINVAL;
521 case PIN_CONFIG_BIAS_PULL_DOWN:
522 cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
523 if (!disable && !pull_up)
524 return 0;
525 else
526 return -EINVAL;
528 case PIN_CONFIG_DRIVE_STRENGTH:
529 ret = cygnus_gpio_get_strength(chip, gpio, &arg);
530 if (ret)
531 return ret;
532 else
533 *config = pinconf_to_config_packed(param, arg);
535 return 0;
537 default:
538 return -ENOTSUPP;
541 return -ENOTSUPP;
544 static int cygnus_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
545 unsigned long *configs, unsigned num_configs)
547 struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
548 enum pin_config_param param;
549 u16 arg;
550 unsigned i, gpio = cygnus_pin_to_gpio(pin);
551 int ret = -ENOTSUPP;
553 for (i = 0; i < num_configs; i++) {
554 param = pinconf_to_config_param(configs[i]);
555 arg = pinconf_to_config_argument(configs[i]);
557 switch (param) {
558 case PIN_CONFIG_BIAS_DISABLE:
559 ret = cygnus_gpio_set_pull(chip, gpio, true, false);
560 if (ret < 0)
561 goto out;
562 break;
564 case PIN_CONFIG_BIAS_PULL_UP:
565 ret = cygnus_gpio_set_pull(chip, gpio, false, true);
566 if (ret < 0)
567 goto out;
568 break;
570 case PIN_CONFIG_BIAS_PULL_DOWN:
571 ret = cygnus_gpio_set_pull(chip, gpio, false, false);
572 if (ret < 0)
573 goto out;
574 break;
576 case PIN_CONFIG_DRIVE_STRENGTH:
577 ret = cygnus_gpio_set_strength(chip, gpio, arg);
578 if (ret < 0)
579 goto out;
580 break;
582 default:
583 dev_err(chip->dev, "invalid configuration\n");
584 return -ENOTSUPP;
586 } /* for each config */
588 out:
589 return ret;
592 static const struct pinconf_ops cygnus_pconf_ops = {
593 .is_generic = true,
594 .pin_config_get = cygnus_pin_config_get,
595 .pin_config_set = cygnus_pin_config_set,
599 * Cygnus GPIO controller supports some PINCONF related configurations such as
600 * pull up, pull down, and drive strength, when the pin is configured to GPIO
602 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
603 * local GPIO pins
605 static int cygnus_gpio_register_pinconf(struct cygnus_gpio *chip)
607 struct pinctrl_desc *pctldesc = &chip->pctldesc;
608 struct pinctrl_pin_desc *pins;
609 struct gpio_chip *gc = &chip->gc;
610 int i;
612 pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
613 if (!pins)
614 return -ENOMEM;
616 for (i = 0; i < gc->ngpio; i++) {
617 pins[i].number = i;
618 pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
619 "gpio-%d", i);
620 if (!pins[i].name)
621 return -ENOMEM;
624 pctldesc->name = dev_name(chip->dev);
625 pctldesc->pctlops = &cygnus_pctrl_ops;
626 pctldesc->pins = pins;
627 pctldesc->npins = gc->ngpio;
628 pctldesc->confops = &cygnus_pconf_ops;
630 chip->pctl = pinctrl_register(pctldesc, chip->dev, chip);
631 if (IS_ERR(chip->pctl)) {
632 dev_err(chip->dev, "unable to register pinctrl device\n");
633 return PTR_ERR(chip->pctl);
636 return 0;
639 static void cygnus_gpio_unregister_pinconf(struct cygnus_gpio *chip)
641 if (chip->pctl)
642 pinctrl_unregister(chip->pctl);
645 struct cygnus_gpio_data {
646 unsigned num_gpios;
649 static const struct cygnus_gpio_data cygnus_cmm_gpio_data = {
650 .num_gpios = 24,
653 static const struct cygnus_gpio_data cygnus_asiu_gpio_data = {
654 .num_gpios = 146,
657 static const struct cygnus_gpio_data cygnus_crmu_gpio_data = {
658 .num_gpios = 6,
661 static const struct of_device_id cygnus_gpio_of_match[] = {
663 .compatible = "brcm,cygnus-ccm-gpio",
664 .data = &cygnus_cmm_gpio_data,
667 .compatible = "brcm,cygnus-asiu-gpio",
668 .data = &cygnus_asiu_gpio_data,
671 .compatible = "brcm,cygnus-crmu-gpio",
672 .data = &cygnus_crmu_gpio_data,
676 static int cygnus_gpio_probe(struct platform_device *pdev)
678 struct device *dev = &pdev->dev;
679 struct resource *res;
680 struct cygnus_gpio *chip;
681 struct gpio_chip *gc;
682 u32 ngpios;
683 int irq, ret;
684 const struct of_device_id *match;
685 const struct cygnus_gpio_data *gpio_data;
687 match = of_match_device(cygnus_gpio_of_match, dev);
688 if (!match)
689 return -ENODEV;
690 gpio_data = match->data;
691 ngpios = gpio_data->num_gpios;
693 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
694 if (!chip)
695 return -ENOMEM;
697 chip->dev = dev;
698 platform_set_drvdata(pdev, chip);
700 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
701 chip->base = devm_ioremap_resource(dev, res);
702 if (IS_ERR(chip->base)) {
703 dev_err(dev, "unable to map I/O memory\n");
704 return PTR_ERR(chip->base);
707 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
708 if (res) {
709 chip->io_ctrl = devm_ioremap_resource(dev, res);
710 if (IS_ERR(chip->io_ctrl)) {
711 dev_err(dev, "unable to map I/O memory\n");
712 return PTR_ERR(chip->io_ctrl);
716 spin_lock_init(&chip->lock);
718 gc = &chip->gc;
719 gc->base = -1;
720 gc->ngpio = ngpios;
721 chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
722 gc->label = dev_name(dev);
723 gc->dev = dev;
724 gc->of_node = dev->of_node;
725 gc->request = cygnus_gpio_request;
726 gc->free = cygnus_gpio_free;
727 gc->direction_input = cygnus_gpio_direction_input;
728 gc->direction_output = cygnus_gpio_direction_output;
729 gc->set = cygnus_gpio_set;
730 gc->get = cygnus_gpio_get;
732 chip->pinmux_is_supported = of_property_read_bool(dev->of_node,
733 "gpio-ranges");
735 ret = gpiochip_add(gc);
736 if (ret < 0) {
737 dev_err(dev, "unable to add GPIO chip\n");
738 return ret;
741 ret = cygnus_gpio_register_pinconf(chip);
742 if (ret) {
743 dev_err(dev, "unable to register pinconf\n");
744 goto err_rm_gpiochip;
747 /* optional GPIO interrupt support */
748 irq = platform_get_irq(pdev, 0);
749 if (irq) {
750 ret = gpiochip_irqchip_add(gc, &cygnus_gpio_irq_chip, 0,
751 handle_simple_irq, IRQ_TYPE_NONE);
752 if (ret) {
753 dev_err(dev, "no GPIO irqchip\n");
754 goto err_unregister_pinconf;
757 gpiochip_set_chained_irqchip(gc, &cygnus_gpio_irq_chip, irq,
758 cygnus_gpio_irq_handler);
761 return 0;
763 err_unregister_pinconf:
764 cygnus_gpio_unregister_pinconf(chip);
766 err_rm_gpiochip:
767 gpiochip_remove(gc);
769 return ret;
772 static struct platform_driver cygnus_gpio_driver = {
773 .driver = {
774 .name = "cygnus-gpio",
775 .of_match_table = cygnus_gpio_of_match,
777 .probe = cygnus_gpio_probe,
780 static int __init cygnus_gpio_init(void)
782 return platform_driver_probe(&cygnus_gpio_driver, cygnus_gpio_probe);
784 arch_initcall_sync(cygnus_gpio_init);