2 * Pinctrl Driver for ADI GPIO2 controller
4 * Copyright 2007-2013 Analog Devices Inc.
6 * Licensed under the GPLv2 or later
9 #include <linux/bitops.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/debugfs.h>
14 #include <linux/seq_file.h>
15 #include <linux/irq.h>
16 #include <linux/platform_data/pinctrl-adi2.h>
17 #include <linux/irqdomain.h>
18 #include <linux/irqchip/chained_irq.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pinctrl/machine.h>
23 #include <linux/syscore_ops.h>
24 #include <linux/gpio.h>
25 #include <asm/portmux.h>
26 #include "pinctrl-adi2.h"
30 According to the BF54x HRM, pint means "pin interrupt".
31 http://www.analog.com/static/imported-files/processor_manuals/ADSP-BF54x_hwr_rev1.2.pdf
33 ADSP-BF54x processor Blackfin processors have four SIC interrupt chan-
34 nels dedicated to pin interrupt purposes. These channels are managed by
35 four hardware blocks, called PINT0, PINT1, PINT2, and PINT3. Every PINTx
36 block can sense to up to 32 pins. While PINT0 and PINT1 can sense the
37 pins of port A and port B, PINT2 and PINT3 manage all the pins from port
38 C to port J as shown in Figure 9-2.
41 The ten GPIO ports are subdivided into 8-bit half ports, resulting in lower and
42 upper half 8-bit units. The PINTx_ASSIGN registers control the 8-bit multi-
43 plexers shown in Figure 9-3. Lower half units of eight pins can be
44 forwarded to either byte 0 or byte 2 of either associated PINTx block.
45 Upper half units can be forwarded to either byte 1 or byte 3 of the pin
46 interrupt blocks, without further restrictions.
48 All MMR registers in the pin interrupt module are 32 bits wide. To simply the
49 mapping logic, this driver only maps a 16-bit gpio port to the upper or lower
50 16 bits of a PINTx block. You can find the Figure 9-3 on page 583.
52 Each IRQ domain is binding to a GPIO bank device. 2 GPIO bank devices can map
53 to one PINT device. Two in "struct gpio_pint" are used to ease the PINT
56 The GPIO bank mapping to the lower 16 bits of the PINT device set its IRQ
57 domain pointer in domain[0]. The IRQ domain pointer of the other bank is set
58 to domain[1]. PINT interrupt handler adi_gpio_handle_pint_irq() finds out
59 the current domain pointer according to whether the interrupt request mask
60 is in lower 16 bits (domain[0]) or upper 16bits (domain[1]).
62 A PINT device is not part of a GPIO port device in Blackfin. Multiple GPIO
63 port devices can be mapped to the same PINT device.
67 static LIST_HEAD(adi_pint_list);
68 static LIST_HEAD(adi_gpio_port_list);
70 #define DRIVER_NAME "pinctrl-adi2"
72 #define PINT_HI_OFFSET 16
75 * struct gpio_port_saved - GPIO port registers that should be saved between
76 * power suspend and resume operations.
78 * @fer: PORTx_FER register
79 * @data: PORTx_DATA register
80 * @dir: PORTx_DIR register
81 * @inen: PORTx_INEN register
82 * @mux: PORTx_MUX register
84 struct gpio_port_saved {
93 * struct gpio_pint - Pin interrupt controller device. Multiple ADI GPIO
94 * banks can be mapped into one Pin interrupt controller.
96 * @node: All gpio_pint instances are added to a global list.
97 * @base: PINT device register base address
98 * @irq: IRQ of the PINT device, it is the parent IRQ of all
99 * GPIO IRQs mapping to this device.
100 * @domain: [0] irq domain of the gpio port, whose hardware interrupts are
101 * mapping to the low 16-bit of the pint registers.
102 * [1] irq domain of the gpio port, whose hardware interrupts are
103 * mapping to the high 16-bit of the pint registers.
104 * @regs: address pointer to the PINT device
105 * @map_count: No more than 2 GPIO banks can be mapped to this PINT device.
106 * @lock: This lock make sure the irq_chip operations to one PINT device
107 * for different GPIO interrrupts are atomic.
108 * @pint_map_port: Set up the mapping between one PINT device and
109 * multiple GPIO banks.
112 struct list_head node;
115 struct irq_domain *domain[2];
116 struct gpio_pint_regs *regs;
117 struct adi_pm_pint_save saved_data;
121 int (*pint_map_port)(struct gpio_pint *pint, bool assign,
122 u8 map, struct irq_domain *domain);
128 * @dev: a pointer back to containing device
129 * @pctl: the pinctrl device
130 * @soc: SoC data for this specific chip
134 struct pinctrl_dev *pctl;
135 const struct adi_pinctrl_soc_data *soc;
139 * struct gpio_port - GPIO bank device. Multiple ADI GPIO banks can be mapped
140 * into one pin interrupt controller.
142 * @node: All gpio_port instances are added to a list.
143 * @base: GPIO bank device register base address
144 * @irq_base: base IRQ of the GPIO bank device
145 * @width: PIN number of the GPIO bank device
146 * @regs: address pointer to the GPIO bank device
147 * @saved_data: registers that should be saved between PM operations.
148 * @dev: device structure of this GPIO bank
149 * @pint: GPIO PINT device that this GPIO bank mapped to
150 * @pint_map: GIOP bank mapping code in PINT device
151 * @pint_assign: The 32-bit PINT registers can be divided into 2 parts. A
152 * GPIO bank can be mapped into either low 16 bits[0] or high 16
153 * bits[1] of each PINT register.
154 * @lock: This lock make sure the irq_chip operations to one PINT device
155 * for different GPIO interrrupts are atomic.
156 * @chip: abstract a GPIO controller
157 * @domain: The irq domain owned by the GPIO port.
158 * @rsvmap: Reservation map array for each pin in the GPIO bank
161 struct list_head node;
163 unsigned int irq_base;
165 struct gpio_port_t *regs;
166 struct gpio_port_saved saved_data;
169 struct gpio_pint *pint;
174 struct gpio_chip chip;
175 struct irq_domain *domain;
178 static inline u8 pin_to_offset(struct pinctrl_gpio_range *range, unsigned pin)
180 return pin - range->pin_base;
183 static inline u32 hwirq_to_pintbit(struct gpio_port *port, int hwirq)
185 return port->pint_assign ? BIT(hwirq) << PINT_HI_OFFSET : BIT(hwirq);
188 static struct gpio_pint *find_gpio_pint(unsigned id)
190 struct gpio_pint *pint;
193 list_for_each_entry(pint, &adi_pint_list, node) {
202 static inline void port_setup(struct gpio_port *port, unsigned offset,
205 struct gpio_port_t *regs = port->regs;
208 writew(readw(®s->port_fer) & ~BIT(offset),
211 writew(readw(®s->port_fer) | BIT(offset), ®s->port_fer);
214 static inline void portmux_setup(struct gpio_port *port, unsigned offset,
215 unsigned short function)
217 struct gpio_port_t *regs = port->regs;
220 pmux = readl(®s->port_mux);
222 /* The function field of each pin has 2 consecutive bits in
225 pmux &= ~(0x3 << (2 * offset));
226 pmux |= (function & 0x3) << (2 * offset);
228 writel(pmux, ®s->port_mux);
231 static inline u16 get_portmux(struct gpio_port *port, unsigned offset)
233 struct gpio_port_t *regs = port->regs;
234 u32 pmux = readl(®s->port_mux);
236 /* The function field of each pin has 2 consecutive bits in
239 return pmux >> (2 * offset) & 0x3;
242 static void adi_gpio_ack_irq(struct irq_data *d)
245 struct gpio_port *port = irq_data_get_irq_chip_data(d);
246 struct gpio_pint_regs *regs = port->pint->regs;
247 unsigned pintbit = hwirq_to_pintbit(port, d->hwirq);
249 spin_lock_irqsave(&port->lock, flags);
250 spin_lock(&port->pint->lock);
252 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
253 if (readl(®s->invert_set) & pintbit)
254 writel(pintbit, ®s->invert_clear);
256 writel(pintbit, ®s->invert_set);
259 writel(pintbit, ®s->request);
261 spin_unlock(&port->pint->lock);
262 spin_unlock_irqrestore(&port->lock, flags);
265 static void adi_gpio_mask_ack_irq(struct irq_data *d)
268 struct gpio_port *port = irq_data_get_irq_chip_data(d);
269 struct gpio_pint_regs *regs = port->pint->regs;
270 unsigned pintbit = hwirq_to_pintbit(port, d->hwirq);
272 spin_lock_irqsave(&port->lock, flags);
273 spin_lock(&port->pint->lock);
275 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
276 if (readl(®s->invert_set) & pintbit)
277 writel(pintbit, ®s->invert_clear);
279 writel(pintbit, ®s->invert_set);
282 writel(pintbit, ®s->request);
283 writel(pintbit, ®s->mask_clear);
285 spin_unlock(&port->pint->lock);
286 spin_unlock_irqrestore(&port->lock, flags);
289 static void adi_gpio_mask_irq(struct irq_data *d)
292 struct gpio_port *port = irq_data_get_irq_chip_data(d);
293 struct gpio_pint_regs *regs = port->pint->regs;
295 spin_lock_irqsave(&port->lock, flags);
296 spin_lock(&port->pint->lock);
298 writel(hwirq_to_pintbit(port, d->hwirq), ®s->mask_clear);
300 spin_unlock(&port->pint->lock);
301 spin_unlock_irqrestore(&port->lock, flags);
304 static void adi_gpio_unmask_irq(struct irq_data *d)
307 struct gpio_port *port = irq_data_get_irq_chip_data(d);
308 struct gpio_pint_regs *regs = port->pint->regs;
310 spin_lock_irqsave(&port->lock, flags);
311 spin_lock(&port->pint->lock);
313 writel(hwirq_to_pintbit(port, d->hwirq), ®s->mask_set);
315 spin_unlock(&port->pint->lock);
316 spin_unlock_irqrestore(&port->lock, flags);
319 static unsigned int adi_gpio_irq_startup(struct irq_data *d)
322 struct gpio_port *port = irq_data_get_irq_chip_data(d);
323 struct gpio_pint_regs *regs;
326 pr_err("GPIO IRQ %d :Not exist\n", d->irq);
330 regs = port->pint->regs;
332 spin_lock_irqsave(&port->lock, flags);
333 spin_lock(&port->pint->lock);
335 port_setup(port, d->hwirq, true);
336 writew(BIT(d->hwirq), &port->regs->dir_clear);
337 writew(readw(&port->regs->inen) | BIT(d->hwirq), &port->regs->inen);
339 writel(hwirq_to_pintbit(port, d->hwirq), ®s->mask_set);
341 spin_unlock(&port->pint->lock);
342 spin_unlock_irqrestore(&port->lock, flags);
347 static void adi_gpio_irq_shutdown(struct irq_data *d)
350 struct gpio_port *port = irq_data_get_irq_chip_data(d);
351 struct gpio_pint_regs *regs = port->pint->regs;
353 spin_lock_irqsave(&port->lock, flags);
354 spin_lock(&port->pint->lock);
356 writel(hwirq_to_pintbit(port, d->hwirq), ®s->mask_clear);
358 spin_unlock(&port->pint->lock);
359 spin_unlock_irqrestore(&port->lock, flags);
362 static int adi_gpio_irq_type(struct irq_data *d, unsigned int type)
365 struct gpio_port *port = irq_data_get_irq_chip_data(d);
366 struct gpio_pint_regs *pint_regs;
368 unsigned int irq = d->irq;
373 pr_err("GPIO IRQ %d :Not exist\n", d->irq);
377 pint_regs = port->pint->regs;
379 pintmask = hwirq_to_pintbit(port, d->hwirq);
381 spin_lock_irqsave(&port->lock, flags);
382 spin_lock(&port->pint->lock);
384 /* In case of interrupt autodetect, set irq type to edge sensitive. */
385 if (type == IRQ_TYPE_PROBE)
386 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
388 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
389 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
390 snprintf(buf, 16, "gpio-irq%d", irq);
391 port_setup(port, d->hwirq, true);
395 /* The GPIO interrupt is triggered only when its input value
396 * transfer from 0 to 1. So, invert the input value if the
397 * irq type is low or falling
399 if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
400 writel(pintmask, &pint_regs->invert_set);
402 writel(pintmask, &pint_regs->invert_clear);
404 /* In edge sensitive case, if the input value of the requested irq
405 * is already 1, invert it.
407 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
408 if (gpio_get_value(port->chip.base + d->hwirq))
409 writel(pintmask, &pint_regs->invert_set);
411 writel(pintmask, &pint_regs->invert_clear);
414 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
415 writel(pintmask, &pint_regs->edge_set);
416 __irq_set_handler_locked(irq, handle_edge_irq);
418 writel(pintmask, &pint_regs->edge_clear);
419 __irq_set_handler_locked(irq, handle_level_irq);
423 spin_unlock(&port->pint->lock);
424 spin_unlock_irqrestore(&port->lock, flags);
430 static int adi_gpio_set_wake(struct irq_data *d, unsigned int state)
432 struct gpio_port *port = irq_data_get_irq_chip_data(d);
434 if (!port || !port->pint || port->pint->irq != d->irq)
438 adi_internal_set_wake(port->pint->irq, state);
444 static int adi_pint_suspend(void)
446 struct gpio_pint *pint;
448 list_for_each_entry(pint, &adi_pint_list, node) {
449 writel(0xffffffff, &pint->regs->mask_clear);
450 pint->saved_data.assign = readl(&pint->regs->assign);
451 pint->saved_data.edge_set = readl(&pint->regs->edge_set);
452 pint->saved_data.invert_set = readl(&pint->regs->invert_set);
458 static void adi_pint_resume(void)
460 struct gpio_pint *pint;
462 list_for_each_entry(pint, &adi_pint_list, node) {
463 writel(pint->saved_data.assign, &pint->regs->assign);
464 writel(pint->saved_data.edge_set, &pint->regs->edge_set);
465 writel(pint->saved_data.invert_set, &pint->regs->invert_set);
469 static int adi_gpio_suspend(void)
471 struct gpio_port *port;
473 list_for_each_entry(port, &adi_gpio_port_list, node) {
474 port->saved_data.fer = readw(&port->regs->port_fer);
475 port->saved_data.mux = readl(&port->regs->port_mux);
476 port->saved_data.data = readw(&port->regs->data);
477 port->saved_data.inen = readw(&port->regs->inen);
478 port->saved_data.dir = readw(&port->regs->dir_set);
481 return adi_pint_suspend();
484 static void adi_gpio_resume(void)
486 struct gpio_port *port;
490 list_for_each_entry(port, &adi_gpio_port_list, node) {
491 writel(port->saved_data.mux, &port->regs->port_mux);
492 writew(port->saved_data.fer, &port->regs->port_fer);
493 writew(port->saved_data.inen, &port->regs->inen);
494 writew(port->saved_data.data & port->saved_data.dir,
495 &port->regs->data_set);
496 writew(port->saved_data.dir, &port->regs->dir_set);
501 static struct syscore_ops gpio_pm_syscore_ops = {
502 .suspend = adi_gpio_suspend,
503 .resume = adi_gpio_resume,
505 #else /* CONFIG_PM */
506 #define adi_gpio_set_wake NULL
507 #endif /* CONFIG_PM */
509 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
510 static inline void preflow_handler(struct irq_desc *desc)
512 if (desc->preflow_handler)
513 desc->preflow_handler(&desc->irq_data);
516 static inline void preflow_handler(struct irq_desc *desc) { }
519 static void adi_gpio_handle_pint_irq(unsigned int inta_irq,
520 struct irq_desc *desc)
523 u32 level_mask, hwirq;
525 struct gpio_pint *pint = irq_desc_get_handler_data(desc);
526 struct irq_chip *chip = irq_desc_get_chip(desc);
527 struct gpio_pint_regs *regs = pint->regs;
528 struct irq_domain *domain;
530 preflow_handler(desc);
531 chained_irq_enter(chip, desc);
533 request = readl(®s->request);
534 level_mask = readl(®s->edge_set) & request;
537 domain = pint->domain[0];
539 /* domain pointer need to be changed only once at IRQ 16 when
540 * we go through IRQ requests from bit 0 to bit 31.
542 if (hwirq == PINT_HI_OFFSET)
543 domain = pint->domain[1];
546 if (level_mask & BIT(hwirq)) {
548 chained_irq_exit(chip, desc);
550 generic_handle_irq(irq_find_mapping(domain,
551 hwirq % PINT_HI_OFFSET));
559 chained_irq_exit(chip, desc);
562 static struct irq_chip adi_gpio_irqchip = {
564 .irq_ack = adi_gpio_ack_irq,
565 .irq_mask = adi_gpio_mask_irq,
566 .irq_mask_ack = adi_gpio_mask_ack_irq,
567 .irq_unmask = adi_gpio_unmask_irq,
568 .irq_disable = adi_gpio_mask_irq,
569 .irq_enable = adi_gpio_unmask_irq,
570 .irq_set_type = adi_gpio_irq_type,
571 .irq_startup = adi_gpio_irq_startup,
572 .irq_shutdown = adi_gpio_irq_shutdown,
573 .irq_set_wake = adi_gpio_set_wake,
576 static int adi_get_groups_count(struct pinctrl_dev *pctldev)
578 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
580 return pinctrl->soc->ngroups;
583 static const char *adi_get_group_name(struct pinctrl_dev *pctldev,
586 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
588 return pinctrl->soc->groups[selector].name;
591 static int adi_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
592 const unsigned **pins,
595 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
597 *pins = pinctrl->soc->groups[selector].pins;
598 *num_pins = pinctrl->soc->groups[selector].num;
602 static struct pinctrl_ops adi_pctrl_ops = {
603 .get_groups_count = adi_get_groups_count,
604 .get_group_name = adi_get_group_name,
605 .get_group_pins = adi_get_group_pins,
608 static int adi_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
611 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
612 struct gpio_port *port;
613 struct pinctrl_gpio_range *range;
615 unsigned short *mux, pin;
617 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
622 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
623 if (range == NULL) /* should not happen */
626 port = container_of(range->gc, struct gpio_port, chip);
628 spin_lock_irqsave(&port->lock, flags);
630 portmux_setup(port, pin_to_offset(range, pin),
632 port_setup(port, pin_to_offset(range, pin), false);
635 spin_unlock_irqrestore(&port->lock, flags);
641 static void adi_pinmux_disable(struct pinctrl_dev *pctldev, unsigned selector,
644 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
645 struct gpio_port *port;
646 struct pinctrl_gpio_range *range;
648 unsigned short *mux, pin;
650 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
655 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
656 if (range == NULL) /* should not happen */
659 port = container_of(range->gc, struct gpio_port, chip);
661 spin_lock_irqsave(&port->lock, flags);
663 port_setup(port, pin_to_offset(range, pin), true);
666 spin_unlock_irqrestore(&port->lock, flags);
670 static int adi_pinmux_get_funcs_count(struct pinctrl_dev *pctldev)
672 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
674 return pinctrl->soc->nfunctions;
677 static const char *adi_pinmux_get_func_name(struct pinctrl_dev *pctldev,
680 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
682 return pinctrl->soc->functions[selector].name;
685 static int adi_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
686 const char * const **groups,
687 unsigned * const num_groups)
689 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
691 *groups = pinctrl->soc->functions[selector].groups;
692 *num_groups = pinctrl->soc->functions[selector].num_groups;
696 static int adi_pinmux_request_gpio(struct pinctrl_dev *pctldev,
697 struct pinctrl_gpio_range *range, unsigned pin)
699 struct gpio_port *port;
703 port = container_of(range->gc, struct gpio_port, chip);
704 offset = pin_to_offset(range, pin);
706 spin_lock_irqsave(&port->lock, flags);
708 port_setup(port, offset, true);
710 spin_unlock_irqrestore(&port->lock, flags);
715 static struct pinmux_ops adi_pinmux_ops = {
716 .enable = adi_pinmux_enable,
717 .disable = adi_pinmux_disable,
718 .get_functions_count = adi_pinmux_get_funcs_count,
719 .get_function_name = adi_pinmux_get_func_name,
720 .get_function_groups = adi_pinmux_get_groups,
721 .gpio_request_enable = adi_pinmux_request_gpio,
725 static struct pinctrl_desc adi_pinmux_desc = {
727 .pctlops = &adi_pctrl_ops,
728 .pmxops = &adi_pinmux_ops,
729 .owner = THIS_MODULE,
732 static int adi_gpio_request(struct gpio_chip *chip, unsigned offset)
734 return pinctrl_request_gpio(chip->base + offset);
737 static void adi_gpio_free(struct gpio_chip *chip, unsigned offset)
739 pinctrl_free_gpio(chip->base + offset);
742 static int adi_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
744 struct gpio_port *port;
747 port = container_of(chip, struct gpio_port, chip);
749 spin_lock_irqsave(&port->lock, flags);
751 writew(BIT(offset), &port->regs->dir_clear);
752 writew(readw(&port->regs->inen) | BIT(offset), &port->regs->inen);
754 spin_unlock_irqrestore(&port->lock, flags);
759 static void adi_gpio_set_value(struct gpio_chip *chip, unsigned offset,
762 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
763 struct gpio_port_t *regs = port->regs;
766 spin_lock_irqsave(&port->lock, flags);
769 writew(BIT(offset), ®s->data_set);
771 writew(BIT(offset), ®s->data_clear);
773 spin_unlock_irqrestore(&port->lock, flags);
776 static int adi_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
779 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
780 struct gpio_port_t *regs = port->regs;
783 spin_lock_irqsave(&port->lock, flags);
785 writew(readw(®s->inen) & ~BIT(offset), ®s->inen);
787 writew(BIT(offset), ®s->data_set);
789 writew(BIT(offset), ®s->data_clear);
790 writew(BIT(offset), ®s->dir_set);
792 spin_unlock_irqrestore(&port->lock, flags);
797 static int adi_gpio_get_value(struct gpio_chip *chip, unsigned offset)
799 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
800 struct gpio_port_t *regs = port->regs;
804 spin_lock_irqsave(&port->lock, flags);
806 ret = !!(readw(®s->data) & BIT(offset));
808 spin_unlock_irqrestore(&port->lock, flags);
813 static int adi_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
815 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
817 if (port->irq_base >= 0)
818 return irq_find_mapping(port->domain, offset);
820 return irq_create_mapping(port->domain, offset);
823 static int adi_pint_map_port(struct gpio_pint *pint, bool assign, u8 map,
824 struct irq_domain *domain)
826 struct gpio_pint_regs *regs = pint->regs;
829 if (pint->map_count > 1)
834 /* The map_mask of each gpio port is a 16-bit duplicate
835 * of the 8-bit map. It can be set to either high 16 bits or low
836 * 16 bits of the pint assignment register.
838 map_mask = (map << 8) | map;
840 map_mask <<= PINT_HI_OFFSET;
841 writel((readl(®s->assign) & 0xFFFF) | map_mask,
844 writel((readl(®s->assign) & 0xFFFF0000) | map_mask,
847 pint->domain[assign] = domain;
852 static int adi_gpio_pint_probe(struct platform_device *pdev)
854 struct device *dev = &pdev->dev;
855 struct resource *res;
856 struct gpio_pint *pint;
858 pint = devm_kzalloc(dev, sizeof(struct gpio_pint), GFP_KERNEL);
860 dev_err(dev, "Memory alloc failed\n");
864 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
865 pint->base = devm_ioremap_resource(dev, res);
866 if (IS_ERR(pint->base))
867 return PTR_ERR(pint->base);
869 pint->regs = (struct gpio_pint_regs *)pint->base;
871 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
873 dev_err(dev, "Invalid IRQ resource\n");
877 spin_lock_init(&pint->lock);
879 pint->irq = res->start;
880 pint->pint_map_port = adi_pint_map_port;
881 platform_set_drvdata(pdev, pint);
883 irq_set_chained_handler(pint->irq, adi_gpio_handle_pint_irq);
884 irq_set_handler_data(pint->irq, pint);
886 list_add_tail(&pint->node, &adi_pint_list);
891 static int adi_gpio_pint_remove(struct platform_device *pdev)
893 struct gpio_pint *pint = platform_get_drvdata(pdev);
895 list_del(&pint->node);
896 irq_set_handler(pint->irq, handle_simple_irq);
901 static int adi_gpio_irq_map(struct irq_domain *d, unsigned int irq,
902 irq_hw_number_t hwirq)
904 struct gpio_port *port = d->host_data;
909 irq_set_chip_data(irq, port);
910 irq_set_chip_and_handler(irq, &adi_gpio_irqchip,
916 const struct irq_domain_ops adi_gpio_irq_domain_ops = {
917 .map = adi_gpio_irq_map,
918 .xlate = irq_domain_xlate_onecell,
921 static int adi_gpio_init_int(struct gpio_port *port)
923 struct device_node *node = port->dev->of_node;
924 struct gpio_pint *pint = port->pint;
927 port->domain = irq_domain_add_linear(node, port->width,
928 &adi_gpio_irq_domain_ops, port);
930 dev_err(port->dev, "Failed to create irqdomain\n");
934 /* According to BF54x and BF60x HRM, pin interrupt devices are not
935 * part of the GPIO port device. in GPIO interrupt mode, the GPIO
936 * pins of multiple port devices can be routed into one pin interrupt
937 * device. The mapping can be configured by setting pint assignment
938 * register with the mapping value of different GPIO port. This is
939 * done via function pint_map_port().
941 ret = pint->pint_map_port(port->pint, port->pint_assign,
942 port->pint_map, port->domain);
946 if (port->irq_base >= 0) {
947 ret = irq_create_strict_mappings(port->domain, port->irq_base,
950 dev_err(port->dev, "Couldn't associate to domain\n");
958 #define DEVNAME_SIZE 16
960 static int adi_gpio_probe(struct platform_device *pdev)
962 struct device *dev = &pdev->dev;
963 const struct adi_pinctrl_gpio_platform_data *pdata;
964 struct resource *res;
965 struct gpio_port *port;
966 char pinctrl_devname[DEVNAME_SIZE];
970 pdata = dev->platform_data;
974 port = devm_kzalloc(dev, sizeof(struct gpio_port), GFP_KERNEL);
976 dev_err(dev, "Memory alloc failed\n");
980 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
981 port->base = devm_ioremap_resource(dev, res);
982 if (IS_ERR(port->base))
983 return PTR_ERR(port->base);
985 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
989 port->irq_base = res->start;
991 port->width = pdata->port_width;
993 port->regs = (struct gpio_port_t *)port->base;
994 port->pint_assign = pdata->pint_assign;
995 port->pint_map = pdata->pint_map;
997 port->pint = find_gpio_pint(pdata->pint_id);
999 ret = adi_gpio_init_int(port);
1004 spin_lock_init(&port->lock);
1006 platform_set_drvdata(pdev, port);
1008 port->chip.label = "adi-gpio";
1009 port->chip.direction_input = adi_gpio_direction_input;
1010 port->chip.get = adi_gpio_get_value;
1011 port->chip.direction_output = adi_gpio_direction_output;
1012 port->chip.set = adi_gpio_set_value;
1013 port->chip.request = adi_gpio_request;
1014 port->chip.free = adi_gpio_free;
1015 port->chip.to_irq = adi_gpio_to_irq;
1016 if (pdata->port_gpio_base > 0)
1017 port->chip.base = pdata->port_gpio_base;
1019 port->chip.base = gpio;
1020 port->chip.ngpio = port->width;
1021 gpio = port->chip.base + port->width;
1023 ret = gpiochip_add(&port->chip);
1025 dev_err(&pdev->dev, "Fail to add GPIO chip.\n");
1026 goto out_remove_domain;
1029 /* Add gpio pin range */
1030 snprintf(pinctrl_devname, DEVNAME_SIZE, "pinctrl-adi2.%d",
1032 pinctrl_devname[DEVNAME_SIZE - 1] = 0;
1033 ret = gpiochip_add_pin_range(&port->chip, pinctrl_devname,
1034 0, pdata->port_pin_base, port->width);
1036 dev_err(&pdev->dev, "Fail to add pin range to %s.\n",
1038 goto out_remove_gpiochip;
1041 list_add_tail(&port->node, &adi_gpio_port_list);
1045 out_remove_gpiochip:
1046 ret1 = gpiochip_remove(&port->chip);
1049 irq_domain_remove(port->domain);
1054 static int adi_gpio_remove(struct platform_device *pdev)
1056 struct gpio_port *port = platform_get_drvdata(pdev);
1060 list_del(&port->node);
1061 gpiochip_remove_pin_ranges(&port->chip);
1062 ret = gpiochip_remove(&port->chip);
1064 for (offset = 0; offset < port->width; offset++)
1065 irq_dispose_mapping(irq_find_mapping(port->domain,
1067 irq_domain_remove(port->domain);
1073 static int adi_pinctrl_probe(struct platform_device *pdev)
1075 struct adi_pinctrl *pinctrl;
1077 pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
1081 pinctrl->dev = &pdev->dev;
1083 adi_pinctrl_soc_init(&pinctrl->soc);
1085 adi_pinmux_desc.pins = pinctrl->soc->pins;
1086 adi_pinmux_desc.npins = pinctrl->soc->npins;
1088 /* Now register the pin controller and all pins it handles */
1089 pinctrl->pctl = pinctrl_register(&adi_pinmux_desc, &pdev->dev, pinctrl);
1090 if (!pinctrl->pctl) {
1091 dev_err(&pdev->dev, "could not register pinctrl ADI2 driver\n");
1095 platform_set_drvdata(pdev, pinctrl);
1100 static int adi_pinctrl_remove(struct platform_device *pdev)
1102 struct adi_pinctrl *pinctrl = platform_get_drvdata(pdev);
1104 pinctrl_unregister(pinctrl->pctl);
1109 static struct platform_driver adi_pinctrl_driver = {
1110 .probe = adi_pinctrl_probe,
1111 .remove = adi_pinctrl_remove,
1113 .name = DRIVER_NAME,
1117 static struct platform_driver adi_gpio_pint_driver = {
1118 .probe = adi_gpio_pint_probe,
1119 .remove = adi_gpio_pint_remove,
1121 .name = "adi-gpio-pint",
1125 static struct platform_driver adi_gpio_driver = {
1126 .probe = adi_gpio_probe,
1127 .remove = adi_gpio_remove,
1133 static int __init adi_pinctrl_setup(void)
1137 ret = platform_driver_register(&adi_pinctrl_driver);
1141 ret = platform_driver_register(&adi_gpio_pint_driver);
1145 ret = platform_driver_register(&adi_gpio_driver);
1150 register_syscore_ops(&gpio_pm_syscore_ops);
1154 platform_driver_unregister(&adi_gpio_pint_driver);
1156 platform_driver_unregister(&adi_pinctrl_driver);
1160 arch_initcall(adi_pinctrl_setup);
1162 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
1163 MODULE_DESCRIPTION("ADI gpio2 pin control driver");
1164 MODULE_LICENSE("GPL");