2 * Marvell 37xx SoC pinctrl driver
4 * Copyright (C) 2017 Marvell
6 * Gregory CLEMENT <gregory.clement@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2 or later. This program is licensed "as is"
10 * without any warranty of any kind, whether express or implied.
13 #include <linux/gpio/driver.h>
14 #include <linux/mfd/syscon.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/pinctrl/pinconf-generic.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
27 #include "../pinctrl-utils.h"
30 #define INPUT_VAL 0x10
31 #define OUTPUT_VAL 0x18
32 #define OUTPUT_CTL 0x20
33 #define SELECTION 0x30
37 #define IRQ_STATUS 0x10
41 #define GPIO_PER_REG 32
44 * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
45 * The pins of a pinmux groups are composed of one or two groups of contiguous
47 * @name: Name of the pin group, used to lookup the group.
48 * @start_pins: Index of the first pin of the main range of pins belonging to
50 * @npins: Number of pins included in the first range
51 * @reg_mask: Bit mask matching the group in the selection register
52 * @extra_pins: Index of the first pin of the optional second range of pins
53 * belonging to the group
54 * @npins: Number of pins included in the second optional range
55 * @funcs: A list of pinmux functions that can be selected for this group.
56 * @pins: List of the pins included in the group
58 struct armada_37xx_pin_group
{
60 unsigned int start_pin
;
64 unsigned int extra_pin
;
65 unsigned int extra_npins
;
66 const char *funcs
[NB_FUNCS
];
70 struct armada_37xx_pin_data
{
73 struct armada_37xx_pin_group
*groups
;
77 struct armada_37xx_pmx_func
{
83 struct armada_37xx_pinctrl
{
84 struct regmap
*regmap
;
86 const struct armada_37xx_pin_data
*data
;
88 struct gpio_chip gpio_chip
;
89 struct irq_chip irq_chip
;
91 struct pinctrl_desc pctl
;
92 struct pinctrl_dev
*pctl_dev
;
93 struct armada_37xx_pin_group
*groups
;
95 struct armada_37xx_pmx_func
*funcs
;
99 #define PIN_GRP(_name, _start, _nr, _mask, _func1, _func2) \
102 .start_pin = _start, \
106 .funcs = {_func1, _func2} \
109 #define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1) \
112 .start_pin = _start, \
116 .funcs = {_func1, "gpio"} \
119 #define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1) \
122 .start_pin = _start, \
125 .val = {_val1, _val2}, \
126 .funcs = {_func1, "gpio"} \
129 #define PIN_GRP_GPIO_3(_name, _start, _nr, _mask, _v1, _v2, _v3, _f1, _f2) \
132 .start_pin = _start, \
135 .val = {_v1, _v2, _v3}, \
136 .funcs = {_f1, _f2, "gpio"} \
139 #define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
143 .start_pin = _start, \
147 .extra_pin = _start2, \
148 .extra_npins = _nr2, \
149 .funcs = {_f1, _f2} \
152 static struct armada_37xx_pin_group armada_37xx_nb_groups
[] = {
153 PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
154 PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
155 PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
156 PIN_GRP_GPIO("pwm0", 11, 1, BIT(3), "pwm"),
157 PIN_GRP_GPIO("pwm1", 12, 1, BIT(4), "pwm"),
158 PIN_GRP_GPIO("pwm2", 13, 1, BIT(5), "pwm"),
159 PIN_GRP_GPIO("pwm3", 14, 1, BIT(6), "pwm"),
160 PIN_GRP_GPIO("pmic1", 17, 1, BIT(7), "pmic"),
161 PIN_GRP_GPIO("pmic0", 16, 1, BIT(8), "pmic"),
162 PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
163 PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
164 PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
165 PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
166 PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
167 PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
168 PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
169 PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
170 PIN_GRP_EXTRA("uart2", 9, 2, BIT(1) | BIT(13) | BIT(14) | BIT(19),
171 BIT(1) | BIT(13) | BIT(14), BIT(1) | BIT(19),
172 18, 2, "gpio", "uart"),
173 PIN_GRP_GPIO("led0_od", 11, 1, BIT(20), "led"),
174 PIN_GRP_GPIO("led1_od", 12, 1, BIT(21), "led"),
175 PIN_GRP_GPIO("led2_od", 13, 1, BIT(22), "led"),
176 PIN_GRP_GPIO("led3_od", 14, 1, BIT(23), "led"),
180 static struct armada_37xx_pin_group armada_37xx_sb_groups
[] = {
181 PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
182 PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
183 PIN_GRP_GPIO("sdio_sb", 24, 6, BIT(2), "sdio"),
184 PIN_GRP_GPIO("rgmii", 6, 12, BIT(3), "mii"),
185 PIN_GRP_GPIO("pcie1", 3, 2, BIT(4), "pcie"),
186 PIN_GRP_GPIO("ptp", 20, 3, BIT(5), "ptp"),
187 PIN_GRP("ptp_clk", 21, 1, BIT(6), "ptp", "mii"),
188 PIN_GRP("ptp_trig", 22, 1, BIT(7), "ptp", "mii"),
189 PIN_GRP_GPIO_3("mii_col", 23, 1, BIT(8) | BIT(14), 0, BIT(8), BIT(14),
193 static const struct armada_37xx_pin_data armada_37xx_pin_nb
= {
196 .groups
= armada_37xx_nb_groups
,
197 .ngroups
= ARRAY_SIZE(armada_37xx_nb_groups
),
200 static const struct armada_37xx_pin_data armada_37xx_pin_sb
= {
203 .groups
= armada_37xx_sb_groups
,
204 .ngroups
= ARRAY_SIZE(armada_37xx_sb_groups
),
207 static inline void armada_37xx_update_reg(unsigned int *reg
,
210 /* We never have more than 2 registers */
211 if (offset
>= GPIO_PER_REG
) {
212 offset
-= GPIO_PER_REG
;
217 static struct armada_37xx_pin_group
*armada_37xx_find_next_grp_by_pin(
218 struct armada_37xx_pinctrl
*info
, int pin
, int *grp
)
220 while (*grp
< info
->ngroups
) {
221 struct armada_37xx_pin_group
*group
= &info
->groups
[*grp
];
225 for (j
= 0; j
< (group
->npins
+ group
->extra_npins
); j
++)
226 if (group
->pins
[j
] == pin
)
232 static int armada_37xx_pin_config_group_get(struct pinctrl_dev
*pctldev
,
233 unsigned int selector
, unsigned long *config
)
238 static int armada_37xx_pin_config_group_set(struct pinctrl_dev
*pctldev
,
239 unsigned int selector
, unsigned long *configs
,
240 unsigned int num_configs
)
245 static const struct pinconf_ops armada_37xx_pinconf_ops
= {
247 .pin_config_group_get
= armada_37xx_pin_config_group_get
,
248 .pin_config_group_set
= armada_37xx_pin_config_group_set
,
251 static int armada_37xx_get_groups_count(struct pinctrl_dev
*pctldev
)
253 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
255 return info
->ngroups
;
258 static const char *armada_37xx_get_group_name(struct pinctrl_dev
*pctldev
,
261 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
263 return info
->groups
[group
].name
;
266 static int armada_37xx_get_group_pins(struct pinctrl_dev
*pctldev
,
267 unsigned int selector
,
268 const unsigned int **pins
,
271 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
273 if (selector
>= info
->ngroups
)
276 *pins
= info
->groups
[selector
].pins
;
277 *npins
= info
->groups
[selector
].npins
+
278 info
->groups
[selector
].extra_npins
;
283 static const struct pinctrl_ops armada_37xx_pctrl_ops
= {
284 .get_groups_count
= armada_37xx_get_groups_count
,
285 .get_group_name
= armada_37xx_get_group_name
,
286 .get_group_pins
= armada_37xx_get_group_pins
,
287 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
288 .dt_free_map
= pinctrl_utils_free_map
,
292 * Pinmux_ops handling
295 static int armada_37xx_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
297 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
302 static const char *armada_37xx_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
303 unsigned int selector
)
305 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
307 return info
->funcs
[selector
].name
;
310 static int armada_37xx_pmx_get_groups(struct pinctrl_dev
*pctldev
,
311 unsigned int selector
,
312 const char * const **groups
,
313 unsigned int * const num_groups
)
315 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
317 *groups
= info
->funcs
[selector
].groups
;
318 *num_groups
= info
->funcs
[selector
].ngroups
;
323 static int armada_37xx_pmx_set_by_name(struct pinctrl_dev
*pctldev
,
325 struct armada_37xx_pin_group
*grp
)
327 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
328 unsigned int reg
= SELECTION
;
329 unsigned int mask
= grp
->reg_mask
;
332 dev_dbg(info
->dev
, "enable function %s group %s\n",
335 func
= match_string(grp
->funcs
, NB_FUNCS
, name
);
339 val
= grp
->val
[func
];
341 regmap_update_bits(info
->regmap
, reg
, mask
, val
);
346 static int armada_37xx_pmx_set(struct pinctrl_dev
*pctldev
,
347 unsigned int selector
,
351 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
352 struct armada_37xx_pin_group
*grp
= &info
->groups
[group
];
353 const char *name
= info
->funcs
[selector
].name
;
355 return armada_37xx_pmx_set_by_name(pctldev
, name
, grp
);
358 static inline void armada_37xx_irq_update_reg(unsigned int *reg
,
361 int offset
= irqd_to_hwirq(d
);
363 armada_37xx_update_reg(reg
, offset
);
366 static int armada_37xx_gpio_direction_input(struct gpio_chip
*chip
,
369 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
370 unsigned int reg
= OUTPUT_EN
;
373 armada_37xx_update_reg(®
, offset
);
376 return regmap_update_bits(info
->regmap
, reg
, mask
, 0);
379 static int armada_37xx_gpio_get_direction(struct gpio_chip
*chip
,
382 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
383 unsigned int reg
= OUTPUT_EN
;
384 unsigned int val
, mask
;
386 armada_37xx_update_reg(®
, offset
);
388 regmap_read(info
->regmap
, reg
, &val
);
390 return !(val
& mask
);
393 static int armada_37xx_gpio_direction_output(struct gpio_chip
*chip
,
394 unsigned int offset
, int value
)
396 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
397 unsigned int reg
= OUTPUT_EN
;
398 unsigned int mask
, val
, ret
;
400 armada_37xx_update_reg(®
, offset
);
403 ret
= regmap_update_bits(info
->regmap
, reg
, mask
, mask
);
409 val
= value
? mask
: 0;
410 regmap_update_bits(info
->regmap
, reg
, mask
, val
);
415 static int armada_37xx_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
417 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
418 unsigned int reg
= INPUT_VAL
;
419 unsigned int val
, mask
;
421 armada_37xx_update_reg(®
, offset
);
424 regmap_read(info
->regmap
, reg
, &val
);
426 return (val
& mask
) != 0;
429 static void armada_37xx_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
432 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
433 unsigned int reg
= OUTPUT_VAL
;
434 unsigned int mask
, val
;
436 armada_37xx_update_reg(®
, offset
);
438 val
= value
? mask
: 0;
440 regmap_update_bits(info
->regmap
, reg
, mask
, val
);
443 static int armada_37xx_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
444 struct pinctrl_gpio_range
*range
,
445 unsigned int offset
, bool input
)
447 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
448 struct gpio_chip
*chip
= range
->gc
;
450 dev_dbg(info
->dev
, "gpio_direction for pin %u as %s-%d to %s\n",
451 offset
, range
->name
, offset
, input
? "input" : "output");
454 armada_37xx_gpio_direction_input(chip
, offset
);
456 armada_37xx_gpio_direction_output(chip
, offset
, 0);
461 static int armada_37xx_gpio_request_enable(struct pinctrl_dev
*pctldev
,
462 struct pinctrl_gpio_range
*range
,
465 struct armada_37xx_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
466 struct armada_37xx_pin_group
*group
;
469 dev_dbg(info
->dev
, "requesting gpio %d\n", offset
);
471 while ((group
= armada_37xx_find_next_grp_by_pin(info
, offset
, &grp
)))
472 armada_37xx_pmx_set_by_name(pctldev
, "gpio", group
);
477 static const struct pinmux_ops armada_37xx_pmx_ops
= {
478 .get_functions_count
= armada_37xx_pmx_get_funcs_count
,
479 .get_function_name
= armada_37xx_pmx_get_func_name
,
480 .get_function_groups
= armada_37xx_pmx_get_groups
,
481 .set_mux
= armada_37xx_pmx_set
,
482 .gpio_request_enable
= armada_37xx_gpio_request_enable
,
483 .gpio_set_direction
= armada_37xx_pmx_gpio_set_direction
,
486 static const struct gpio_chip armada_37xx_gpiolib_chip
= {
487 .request
= gpiochip_generic_request
,
488 .free
= gpiochip_generic_free
,
489 .set
= armada_37xx_gpio_set
,
490 .get
= armada_37xx_gpio_get
,
491 .get_direction
= armada_37xx_gpio_get_direction
,
492 .direction_input
= armada_37xx_gpio_direction_input
,
493 .direction_output
= armada_37xx_gpio_direction_output
,
494 .owner
= THIS_MODULE
,
497 static void armada_37xx_irq_ack(struct irq_data
*d
)
499 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
500 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
501 u32 reg
= IRQ_STATUS
;
504 armada_37xx_irq_update_reg(®
, d
);
505 spin_lock_irqsave(&info
->irq_lock
, flags
);
506 writel(d
->mask
, info
->base
+ reg
);
507 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
510 static void armada_37xx_irq_mask(struct irq_data
*d
)
512 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
513 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
514 u32 val
, reg
= IRQ_EN
;
517 armada_37xx_irq_update_reg(®
, d
);
518 spin_lock_irqsave(&info
->irq_lock
, flags
);
519 val
= readl(info
->base
+ reg
);
520 writel(val
& ~d
->mask
, info
->base
+ reg
);
521 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
524 static void armada_37xx_irq_unmask(struct irq_data
*d
)
526 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
527 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
528 u32 val
, reg
= IRQ_EN
;
531 armada_37xx_irq_update_reg(®
, d
);
532 spin_lock_irqsave(&info
->irq_lock
, flags
);
533 val
= readl(info
->base
+ reg
);
534 writel(val
| d
->mask
, info
->base
+ reg
);
535 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
538 static int armada_37xx_irq_set_wake(struct irq_data
*d
, unsigned int on
)
540 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
541 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
542 u32 val
, reg
= IRQ_WKUP
;
545 armada_37xx_irq_update_reg(®
, d
);
546 spin_lock_irqsave(&info
->irq_lock
, flags
);
547 val
= readl(info
->base
+ reg
);
549 val
|= (BIT(d
->hwirq
% GPIO_PER_REG
));
551 val
&= ~(BIT(d
->hwirq
% GPIO_PER_REG
));
552 writel(val
, info
->base
+ reg
);
553 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
558 static int armada_37xx_irq_set_type(struct irq_data
*d
, unsigned int type
)
560 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
561 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(chip
);
562 u32 val
, reg
= IRQ_POL
;
565 spin_lock_irqsave(&info
->irq_lock
, flags
);
566 armada_37xx_irq_update_reg(®
, d
);
567 val
= readl(info
->base
+ reg
);
569 case IRQ_TYPE_EDGE_RISING
:
570 val
&= ~(BIT(d
->hwirq
% GPIO_PER_REG
));
572 case IRQ_TYPE_EDGE_FALLING
:
573 val
|= (BIT(d
->hwirq
% GPIO_PER_REG
));
575 case IRQ_TYPE_EDGE_BOTH
: {
576 u32 in_val
, in_reg
= INPUT_VAL
;
578 armada_37xx_irq_update_reg(&in_reg
, d
);
579 regmap_read(info
->regmap
, in_reg
, &in_val
);
581 /* Set initial polarity based on current input level. */
582 if (in_val
& d
->mask
)
583 val
|= d
->mask
; /* falling */
585 val
&= ~d
->mask
; /* rising */
589 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
592 writel(val
, info
->base
+ reg
);
593 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
598 static int armada_37xx_edge_both_irq_swap_pol(struct armada_37xx_pinctrl
*info
,
601 u32 reg_idx
= pin_idx
/ GPIO_PER_REG
;
602 u32 bit_num
= pin_idx
% GPIO_PER_REG
;
606 regmap_read(info
->regmap
, INPUT_VAL
+ 4*reg_idx
, &l
);
608 spin_lock_irqsave(&info
->irq_lock
, flags
);
609 p
= readl(info
->base
+ IRQ_POL
+ 4 * reg_idx
);
610 if ((p
^ l
) & (1 << bit_num
)) {
612 * For the gpios which are used for both-edge irqs, when their
613 * interrupts happen, their input levels are changed,
614 * yet their interrupt polarities are kept in old values, we
615 * should synchronize their interrupt polarities; for example,
616 * at first a gpio's input level is low and its interrupt
617 * polarity control is "Detect rising edge", then the gpio has
618 * a interrupt , its level turns to high, we should change its
619 * polarity control to "Detect falling edge" correspondingly.
622 writel(p
, info
->base
+ IRQ_POL
+ 4 * reg_idx
);
629 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
633 static void armada_37xx_irq_handler(struct irq_desc
*desc
)
635 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
636 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
637 struct armada_37xx_pinctrl
*info
= gpiochip_get_data(gc
);
638 struct irq_domain
*d
= gc
->irq
.domain
;
641 chained_irq_enter(chip
, desc
);
642 for (i
= 0; i
<= d
->revmap_size
/ GPIO_PER_REG
; i
++) {
646 spin_lock_irqsave(&info
->irq_lock
, flags
);
647 status
= readl_relaxed(info
->base
+ IRQ_STATUS
+ 4 * i
);
648 /* Manage only the interrupt that was enabled */
649 status
&= readl_relaxed(info
->base
+ IRQ_EN
+ 4 * i
);
650 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
652 u32 hwirq
= ffs(status
) - 1;
653 u32 virq
= irq_find_mapping(d
, hwirq
+
655 u32 t
= irq_get_trigger_type(virq
);
657 if ((t
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
) {
658 /* Swap polarity (race with GPIO line) */
659 if (armada_37xx_edge_both_irq_swap_pol(info
,
660 hwirq
+ i
* GPIO_PER_REG
)) {
662 * For spurious irq, which gpio level
663 * is not as expected after incoming
664 * edge, just ack the gpio irq.
673 generic_handle_irq(virq
);
676 /* Update status in case a new IRQ appears */
677 spin_lock_irqsave(&info
->irq_lock
, flags
);
678 status
= readl_relaxed(info
->base
+
680 /* Manage only the interrupt that was enabled */
681 status
&= readl_relaxed(info
->base
+ IRQ_EN
+ 4 * i
);
682 spin_unlock_irqrestore(&info
->irq_lock
, flags
);
685 chained_irq_exit(chip
, desc
);
688 static unsigned int armada_37xx_irq_startup(struct irq_data
*d
)
691 * The mask field is a "precomputed bitmask for accessing the
692 * chip registers" which was introduced for the generic
693 * irqchip framework. As we don't use this framework, we can
694 * reuse this field for our own usage.
696 d
->mask
= BIT(d
->hwirq
% GPIO_PER_REG
);
698 armada_37xx_irq_unmask(d
);
703 static int armada_37xx_irqchip_register(struct platform_device
*pdev
,
704 struct armada_37xx_pinctrl
*info
)
706 struct device_node
*np
= info
->dev
->of_node
;
707 struct gpio_chip
*gc
= &info
->gpio_chip
;
708 struct irq_chip
*irqchip
= &info
->irq_chip
;
710 int ret
= -ENODEV
, i
, nr_irq_parent
;
712 /* Check if we have at least one gpio-controller child node */
713 for_each_child_of_node(info
->dev
->of_node
, np
) {
714 if (of_property_read_bool(np
, "gpio-controller")) {
722 nr_irq_parent
= of_irq_count(np
);
723 spin_lock_init(&info
->irq_lock
);
725 if (!nr_irq_parent
) {
726 dev_err(&pdev
->dev
, "Invalid or no IRQ\n");
730 if (of_address_to_resource(info
->dev
->of_node
, 1, &res
)) {
731 dev_err(info
->dev
, "cannot find IO resource\n");
735 info
->base
= devm_ioremap_resource(info
->dev
, &res
);
736 if (IS_ERR(info
->base
))
737 return PTR_ERR(info
->base
);
739 irqchip
->irq_ack
= armada_37xx_irq_ack
;
740 irqchip
->irq_mask
= armada_37xx_irq_mask
;
741 irqchip
->irq_unmask
= armada_37xx_irq_unmask
;
742 irqchip
->irq_set_wake
= armada_37xx_irq_set_wake
;
743 irqchip
->irq_set_type
= armada_37xx_irq_set_type
;
744 irqchip
->irq_startup
= armada_37xx_irq_startup
;
745 irqchip
->name
= info
->data
->name
;
746 ret
= gpiochip_irqchip_add(gc
, irqchip
, 0,
747 handle_edge_irq
, IRQ_TYPE_NONE
);
749 dev_info(&pdev
->dev
, "could not add irqchip\n");
754 * Many interrupts are connected to the parent interrupt
755 * controller. But we do not take advantage of this and use
756 * the chained irq with all of them.
758 for (i
= 0; i
< nr_irq_parent
; i
++) {
759 int irq
= irq_of_parse_and_map(np
, i
);
764 gpiochip_set_chained_irqchip(gc
, irqchip
, irq
,
765 armada_37xx_irq_handler
);
771 static int armada_37xx_gpiochip_register(struct platform_device
*pdev
,
772 struct armada_37xx_pinctrl
*info
)
774 struct device_node
*np
;
775 struct gpio_chip
*gc
;
778 for_each_child_of_node(info
->dev
->of_node
, np
) {
779 if (of_find_property(np
, "gpio-controller", NULL
)) {
787 info
->gpio_chip
= armada_37xx_gpiolib_chip
;
789 gc
= &info
->gpio_chip
;
790 gc
->ngpio
= info
->data
->nr_pins
;
791 gc
->parent
= &pdev
->dev
;
794 gc
->label
= info
->data
->name
;
796 ret
= devm_gpiochip_add_data(&pdev
->dev
, gc
, info
);
799 ret
= armada_37xx_irqchip_register(pdev
, info
);
807 * armada_37xx_add_function() - Add a new function to the list
808 * @funcs: array of function to add the new one
809 * @funcsize: size of the remaining space for the function
810 * @name: name of the function to add
812 * If it is a new function then create it by adding its name else
813 * increment the number of group associated to this function.
815 static int armada_37xx_add_function(struct armada_37xx_pmx_func
*funcs
,
816 int *funcsize
, const char *name
)
823 while (funcs
->ngroups
) {
824 /* function already there */
825 if (strcmp(funcs
->name
, name
) == 0) {
834 /* append new unique function */
843 * armada_37xx_fill_group() - complete the group array
844 * @info: info driver instance
846 * Based on the data available from the armada_37xx_pin_group array
847 * completes the last member of the struct for each function: the list
848 * of the groups associated to this function.
851 static int armada_37xx_fill_group(struct armada_37xx_pinctrl
*info
)
853 int n
, num
= 0, funcsize
= info
->data
->nr_pins
;
855 for (n
= 0; n
< info
->ngroups
; n
++) {
856 struct armada_37xx_pin_group
*grp
= &info
->groups
[n
];
859 grp
->pins
= devm_kcalloc(info
->dev
,
860 grp
->npins
+ grp
->extra_npins
,
866 for (i
= 0; i
< grp
->npins
; i
++)
867 grp
->pins
[i
] = grp
->start_pin
+ i
;
869 for (j
= 0; j
< grp
->extra_npins
; j
++)
870 grp
->pins
[i
+j
] = grp
->extra_pin
+ j
;
872 for (f
= 0; (f
< NB_FUNCS
) && grp
->funcs
[f
]; f
++) {
874 /* check for unique functions and count groups */
875 ret
= armada_37xx_add_function(info
->funcs
, &funcsize
,
877 if (ret
== -EOVERFLOW
)
879 "More functions than pins(%d)\n",
880 info
->data
->nr_pins
);
893 * armada_37xx_fill_funcs() - complete the funcs array
894 * @info: info driver instance
896 * Based on the data available from the armada_37xx_pin_group array
897 * completes the last two member of the struct for each group:
898 * - the list of the pins included in the group
899 * - the list of pinmux functions that can be selected for this group
902 static int armada_37xx_fill_func(struct armada_37xx_pinctrl
*info
)
904 struct armada_37xx_pmx_func
*funcs
= info
->funcs
;
907 for (n
= 0; n
< info
->nfuncs
; n
++) {
908 const char *name
= funcs
[n
].name
;
912 funcs
[n
].groups
= devm_kcalloc(info
->dev
,
914 sizeof(*(funcs
[n
].groups
)),
916 if (!funcs
[n
].groups
)
919 groups
= funcs
[n
].groups
;
921 for (g
= 0; g
< info
->ngroups
; g
++) {
922 struct armada_37xx_pin_group
*gp
= &info
->groups
[g
];
925 f
= match_string(gp
->funcs
, NB_FUNCS
, name
);
936 static int armada_37xx_pinctrl_register(struct platform_device
*pdev
,
937 struct armada_37xx_pinctrl
*info
)
939 const struct armada_37xx_pin_data
*pin_data
= info
->data
;
940 struct pinctrl_desc
*ctrldesc
= &info
->pctl
;
941 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
944 info
->groups
= pin_data
->groups
;
945 info
->ngroups
= pin_data
->ngroups
;
947 ctrldesc
->name
= "armada_37xx-pinctrl";
948 ctrldesc
->owner
= THIS_MODULE
;
949 ctrldesc
->pctlops
= &armada_37xx_pctrl_ops
;
950 ctrldesc
->pmxops
= &armada_37xx_pmx_ops
;
951 ctrldesc
->confops
= &armada_37xx_pinconf_ops
;
953 pindesc
= devm_kcalloc(&pdev
->dev
,
954 pin_data
->nr_pins
, sizeof(*pindesc
),
959 ctrldesc
->pins
= pindesc
;
960 ctrldesc
->npins
= pin_data
->nr_pins
;
963 for (pin
= 0; pin
< pin_data
->nr_pins
; pin
++) {
965 pdesc
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
966 pin_data
->name
, pin
);
971 * we allocate functions for number of pins and hope there are
972 * fewer unique functions than pins available
974 info
->funcs
= devm_kcalloc(&pdev
->dev
,
976 sizeof(struct armada_37xx_pmx_func
),
982 ret
= armada_37xx_fill_group(info
);
986 ret
= armada_37xx_fill_func(info
);
990 info
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, ctrldesc
, info
);
991 if (IS_ERR(info
->pctl_dev
)) {
992 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
993 return PTR_ERR(info
->pctl_dev
);
999 static const struct of_device_id armada_37xx_pinctrl_of_match
[] = {
1001 .compatible
= "marvell,armada3710-sb-pinctrl",
1002 .data
= &armada_37xx_pin_sb
,
1005 .compatible
= "marvell,armada3710-nb-pinctrl",
1006 .data
= &armada_37xx_pin_nb
,
1011 static int __init
armada_37xx_pinctrl_probe(struct platform_device
*pdev
)
1013 struct armada_37xx_pinctrl
*info
;
1014 struct device
*dev
= &pdev
->dev
;
1015 struct device_node
*np
= dev
->of_node
;
1016 struct regmap
*regmap
;
1019 info
= devm_kzalloc(dev
, sizeof(struct armada_37xx_pinctrl
),
1026 regmap
= syscon_node_to_regmap(np
);
1027 if (IS_ERR(regmap
)) {
1028 dev_err(&pdev
->dev
, "cannot get regmap\n");
1029 return PTR_ERR(regmap
);
1031 info
->regmap
= regmap
;
1033 info
->data
= of_device_get_match_data(dev
);
1035 ret
= armada_37xx_pinctrl_register(pdev
, info
);
1039 ret
= armada_37xx_gpiochip_register(pdev
, info
);
1043 platform_set_drvdata(pdev
, info
);
1048 static struct platform_driver armada_37xx_pinctrl_driver
= {
1050 .name
= "armada-37xx-pinctrl",
1051 .of_match_table
= armada_37xx_pinctrl_of_match
,
1055 builtin_platform_driver_probe(armada_37xx_pinctrl_driver
,
1056 armada_37xx_pinctrl_probe
);