2 * Allwinner A1X SoCs pinctrl driver.
4 * Copyright (C) 2012 Maxime Ripard
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
14 #include <linux/clk.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/irqdomain.h>
17 #include <linux/irqchip/chained_irq.h>
18 #include <linux/export.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinconf-generic.h>
27 #include <linux/pinctrl/pinmux.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
32 #include "../../gpio/gpiolib.h"
33 #include "pinctrl-sunxi.h"
35 static struct irq_chip sunxi_pinctrl_edge_irq_chip
;
36 static struct irq_chip sunxi_pinctrl_level_irq_chip
;
38 static struct sunxi_pinctrl_group
*
39 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl
*pctl
, const char *group
)
43 for (i
= 0; i
< pctl
->ngroups
; i
++) {
44 struct sunxi_pinctrl_group
*grp
= pctl
->groups
+ i
;
46 if (!strcmp(grp
->name
, group
))
53 static struct sunxi_pinctrl_function
*
54 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl
*pctl
,
57 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
60 for (i
= 0; i
< pctl
->nfunctions
; i
++) {
64 if (!strcmp(func
[i
].name
, name
))
71 static struct sunxi_desc_function
*
72 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl
*pctl
,
74 const char *func_name
)
78 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
79 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
81 if (!strcmp(pin
->pin
.name
, pin_name
)) {
82 struct sunxi_desc_function
*func
= pin
->functions
;
85 if (!strcmp(func
->name
, func_name
))
96 static struct sunxi_desc_function
*
97 sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl
*pctl
,
99 const char *func_name
)
103 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
104 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
106 if (pin
->pin
.number
== pin_num
) {
107 struct sunxi_desc_function
*func
= pin
->functions
;
110 if (!strcmp(func
->name
, func_name
))
121 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
123 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
125 return pctl
->ngroups
;
128 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
131 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
133 return pctl
->groups
[group
].name
;
136 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
138 const unsigned **pins
,
141 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
143 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
149 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
150 struct device_node
*node
,
151 struct pinctrl_map
**map
,
154 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
155 unsigned long *pinconfig
;
156 struct property
*prop
;
157 const char *function
;
159 int ret
, nmaps
, i
= 0;
165 ret
= of_property_read_string(node
, "allwinner,function", &function
);
168 "missing allwinner,function property in node %s\n",
173 nmaps
= of_property_count_strings(node
, "allwinner,pins") * 2;
176 "missing allwinner,pins property in node %s\n",
181 *map
= kmalloc(nmaps
* sizeof(struct pinctrl_map
), GFP_KERNEL
);
185 of_property_for_each_string(node
, "allwinner,pins", prop
, group
) {
186 struct sunxi_pinctrl_group
*grp
=
187 sunxi_pinctrl_find_group_by_name(pctl
, group
);
188 int j
= 0, configlen
= 0;
191 dev_err(pctl
->dev
, "unknown pin %s", group
);
195 if (!sunxi_pinctrl_desc_find_function_by_name(pctl
,
198 dev_err(pctl
->dev
, "unsupported function %s on pin %s",
203 (*map
)[i
].type
= PIN_MAP_TYPE_MUX_GROUP
;
204 (*map
)[i
].data
.mux
.group
= group
;
205 (*map
)[i
].data
.mux
.function
= function
;
209 (*map
)[i
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
210 (*map
)[i
].data
.configs
.group_or_pin
= group
;
212 if (of_find_property(node
, "allwinner,drive", NULL
))
214 if (of_find_property(node
, "allwinner,pull", NULL
))
217 pinconfig
= kzalloc(configlen
* sizeof(*pinconfig
), GFP_KERNEL
);
223 if (!of_property_read_u32(node
, "allwinner,drive", &val
)) {
224 u16 strength
= (val
+ 1) * 10;
226 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH
,
230 if (!of_property_read_u32(node
, "allwinner,pull", &val
)) {
231 enum pin_config_param pull
= PIN_CONFIG_END
;
233 pull
= PIN_CONFIG_BIAS_PULL_UP
;
235 pull
= PIN_CONFIG_BIAS_PULL_DOWN
;
236 pinconfig
[j
++] = pinconf_to_config_packed(pull
, 0);
239 (*map
)[i
].data
.configs
.configs
= pinconfig
;
240 (*map
)[i
].data
.configs
.num_configs
= configlen
;
250 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
251 struct pinctrl_map
*map
,
256 for (i
= 0; i
< num_maps
; i
++) {
257 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
258 kfree(map
[i
].data
.configs
.configs
);
264 static const struct pinctrl_ops sunxi_pctrl_ops
= {
265 .dt_node_to_map
= sunxi_pctrl_dt_node_to_map
,
266 .dt_free_map
= sunxi_pctrl_dt_free_map
,
267 .get_groups_count
= sunxi_pctrl_get_groups_count
,
268 .get_group_name
= sunxi_pctrl_get_group_name
,
269 .get_group_pins
= sunxi_pctrl_get_group_pins
,
272 static int sunxi_pconf_group_get(struct pinctrl_dev
*pctldev
,
274 unsigned long *config
)
276 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
278 *config
= pctl
->groups
[group
].config
;
283 static int sunxi_pconf_group_set(struct pinctrl_dev
*pctldev
,
285 unsigned long *configs
,
286 unsigned num_configs
)
288 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
289 struct sunxi_pinctrl_group
*g
= &pctl
->groups
[group
];
291 unsigned pin
= g
->pin
- pctl
->desc
->pin_base
;
297 spin_lock_irqsave(&pctl
->lock
, flags
);
299 for (i
= 0; i
< num_configs
; i
++) {
300 switch (pinconf_to_config_param(configs
[i
])) {
301 case PIN_CONFIG_DRIVE_STRENGTH
:
302 strength
= pinconf_to_config_argument(configs
[i
]);
304 spin_unlock_irqrestore(&pctl
->lock
, flags
);
308 * We convert from mA to what the register expects:
314 dlevel
= strength
/ 10 - 1;
315 val
= readl(pctl
->membase
+ sunxi_dlevel_reg(pin
));
316 mask
= DLEVEL_PINS_MASK
<< sunxi_dlevel_offset(pin
);
318 | dlevel
<< sunxi_dlevel_offset(pin
),
319 pctl
->membase
+ sunxi_dlevel_reg(pin
));
321 case PIN_CONFIG_BIAS_PULL_UP
:
322 val
= readl(pctl
->membase
+ sunxi_pull_reg(pin
));
323 mask
= PULL_PINS_MASK
<< sunxi_pull_offset(pin
);
324 writel((val
& ~mask
) | 1 << sunxi_pull_offset(pin
),
325 pctl
->membase
+ sunxi_pull_reg(pin
));
327 case PIN_CONFIG_BIAS_PULL_DOWN
:
328 val
= readl(pctl
->membase
+ sunxi_pull_reg(pin
));
329 mask
= PULL_PINS_MASK
<< sunxi_pull_offset(pin
);
330 writel((val
& ~mask
) | 2 << sunxi_pull_offset(pin
),
331 pctl
->membase
+ sunxi_pull_reg(pin
));
336 /* cache the config value */
337 g
->config
= configs
[i
];
338 } /* for each config */
340 spin_unlock_irqrestore(&pctl
->lock
, flags
);
345 static const struct pinconf_ops sunxi_pconf_ops
= {
346 .pin_config_group_get
= sunxi_pconf_group_get
,
347 .pin_config_group_set
= sunxi_pconf_group_set
,
350 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
352 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
354 return pctl
->nfunctions
;
357 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
360 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
362 return pctl
->functions
[function
].name
;
365 static int sunxi_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
367 const char * const **groups
,
368 unsigned * const num_groups
)
370 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
372 *groups
= pctl
->functions
[function
].groups
;
373 *num_groups
= pctl
->functions
[function
].ngroups
;
378 static void sunxi_pmx_set(struct pinctrl_dev
*pctldev
,
382 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
386 spin_lock_irqsave(&pctl
->lock
, flags
);
388 pin
-= pctl
->desc
->pin_base
;
389 val
= readl(pctl
->membase
+ sunxi_mux_reg(pin
));
390 mask
= MUX_PINS_MASK
<< sunxi_mux_offset(pin
);
391 writel((val
& ~mask
) | config
<< sunxi_mux_offset(pin
),
392 pctl
->membase
+ sunxi_mux_reg(pin
));
394 spin_unlock_irqrestore(&pctl
->lock
, flags
);
397 static int sunxi_pmx_set_mux(struct pinctrl_dev
*pctldev
,
401 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
402 struct sunxi_pinctrl_group
*g
= pctl
->groups
+ group
;
403 struct sunxi_pinctrl_function
*func
= pctl
->functions
+ function
;
404 struct sunxi_desc_function
*desc
=
405 sunxi_pinctrl_desc_find_function_by_name(pctl
,
412 sunxi_pmx_set(pctldev
, g
->pin
, desc
->muxval
);
418 sunxi_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
419 struct pinctrl_gpio_range
*range
,
423 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
424 struct sunxi_desc_function
*desc
;
432 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, offset
, func
);
436 sunxi_pmx_set(pctldev
, offset
, desc
->muxval
);
441 static const struct pinmux_ops sunxi_pmx_ops
= {
442 .get_functions_count
= sunxi_pmx_get_funcs_cnt
,
443 .get_function_name
= sunxi_pmx_get_func_name
,
444 .get_function_groups
= sunxi_pmx_get_func_groups
,
445 .set_mux
= sunxi_pmx_set_mux
,
446 .gpio_set_direction
= sunxi_pmx_gpio_set_direction
,
449 static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip
*chip
,
452 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
455 static int sunxi_pinctrl_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
457 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
458 u32 reg
= sunxi_data_reg(offset
);
459 u8 index
= sunxi_data_offset(offset
);
460 bool set_mux
= pctl
->desc
->irq_read_needs_mux
&&
461 gpiochip_line_is_irq(chip
, offset
);
462 u32 pin
= offset
+ chip
->base
;
466 sunxi_pmx_set(pctl
->pctl_dev
, pin
, SUN4I_FUNC_INPUT
);
468 val
= (readl(pctl
->membase
+ reg
) >> index
) & DATA_PINS_MASK
;
471 sunxi_pmx_set(pctl
->pctl_dev
, pin
, SUN4I_FUNC_IRQ
);
476 static void sunxi_pinctrl_gpio_set(struct gpio_chip
*chip
,
477 unsigned offset
, int value
)
479 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
480 u32 reg
= sunxi_data_reg(offset
);
481 u8 index
= sunxi_data_offset(offset
);
485 spin_lock_irqsave(&pctl
->lock
, flags
);
487 regval
= readl(pctl
->membase
+ reg
);
490 regval
|= BIT(index
);
492 regval
&= ~(BIT(index
));
494 writel(regval
, pctl
->membase
+ reg
);
496 spin_unlock_irqrestore(&pctl
->lock
, flags
);
499 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip
*chip
,
500 unsigned offset
, int value
)
502 sunxi_pinctrl_gpio_set(chip
, offset
, value
);
503 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
506 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip
*gc
,
507 const struct of_phandle_args
*gpiospec
,
512 base
= PINS_PER_BANK
* gpiospec
->args
[0];
513 pin
= base
+ gpiospec
->args
[1];
519 *flags
= gpiospec
->args
[2];
524 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
526 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
527 struct sunxi_desc_function
*desc
;
528 unsigned pinnum
= pctl
->desc
->pin_base
+ offset
;
531 if (offset
>= chip
->ngpio
)
534 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, pinnum
, "irq");
538 irqnum
= desc
->irqbank
* IRQ_PER_BANK
+ desc
->irqnum
;
540 dev_dbg(chip
->parent
, "%s: request IRQ for GPIO %d, return %d\n",
541 chip
->label
, offset
+ chip
->base
, irqnum
);
543 return irq_find_mapping(pctl
->domain
, irqnum
);
546 static int sunxi_pinctrl_irq_request_resources(struct irq_data
*d
)
548 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
549 struct sunxi_desc_function
*func
;
552 func
= sunxi_pinctrl_desc_find_function_by_pin(pctl
,
553 pctl
->irq_array
[d
->hwirq
], "irq");
557 ret
= gpiochip_lock_as_irq(pctl
->chip
,
558 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
560 dev_err(pctl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
565 /* Change muxing to INT mode */
566 sunxi_pmx_set(pctl
->pctl_dev
, pctl
->irq_array
[d
->hwirq
], func
->muxval
);
571 static void sunxi_pinctrl_irq_release_resources(struct irq_data
*d
)
573 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
575 gpiochip_unlock_as_irq(pctl
->chip
,
576 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
579 static int sunxi_pinctrl_irq_set_type(struct irq_data
*d
, unsigned int type
)
581 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
582 u32 reg
= sunxi_irq_cfg_reg(d
->hwirq
, pctl
->desc
->irq_bank_base
);
583 u8 index
= sunxi_irq_cfg_offset(d
->hwirq
);
589 case IRQ_TYPE_EDGE_RISING
:
590 mode
= IRQ_EDGE_RISING
;
592 case IRQ_TYPE_EDGE_FALLING
:
593 mode
= IRQ_EDGE_FALLING
;
595 case IRQ_TYPE_EDGE_BOTH
:
596 mode
= IRQ_EDGE_BOTH
;
598 case IRQ_TYPE_LEVEL_HIGH
:
599 mode
= IRQ_LEVEL_HIGH
;
601 case IRQ_TYPE_LEVEL_LOW
:
602 mode
= IRQ_LEVEL_LOW
;
608 spin_lock_irqsave(&pctl
->lock
, flags
);
610 if (type
& IRQ_TYPE_LEVEL_MASK
)
611 irq_set_chip_handler_name_locked(d
, &sunxi_pinctrl_level_irq_chip
,
612 handle_fasteoi_irq
, NULL
);
614 irq_set_chip_handler_name_locked(d
, &sunxi_pinctrl_edge_irq_chip
,
615 handle_edge_irq
, NULL
);
617 regval
= readl(pctl
->membase
+ reg
);
618 regval
&= ~(IRQ_CFG_IRQ_MASK
<< index
);
619 writel(regval
| (mode
<< index
), pctl
->membase
+ reg
);
621 spin_unlock_irqrestore(&pctl
->lock
, flags
);
626 static void sunxi_pinctrl_irq_ack(struct irq_data
*d
)
628 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
629 u32 status_reg
= sunxi_irq_status_reg(d
->hwirq
,
630 pctl
->desc
->irq_bank_base
);
631 u8 status_idx
= sunxi_irq_status_offset(d
->hwirq
);
634 writel(1 << status_idx
, pctl
->membase
+ status_reg
);
637 static void sunxi_pinctrl_irq_mask(struct irq_data
*d
)
639 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
640 u32 reg
= sunxi_irq_ctrl_reg(d
->hwirq
, pctl
->desc
->irq_bank_base
);
641 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
645 spin_lock_irqsave(&pctl
->lock
, flags
);
648 val
= readl(pctl
->membase
+ reg
);
649 writel(val
& ~(1 << idx
), pctl
->membase
+ reg
);
651 spin_unlock_irqrestore(&pctl
->lock
, flags
);
654 static void sunxi_pinctrl_irq_unmask(struct irq_data
*d
)
656 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
657 u32 reg
= sunxi_irq_ctrl_reg(d
->hwirq
, pctl
->desc
->irq_bank_base
);
658 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
662 spin_lock_irqsave(&pctl
->lock
, flags
);
665 val
= readl(pctl
->membase
+ reg
);
666 writel(val
| (1 << idx
), pctl
->membase
+ reg
);
668 spin_unlock_irqrestore(&pctl
->lock
, flags
);
671 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data
*d
)
673 sunxi_pinctrl_irq_ack(d
);
674 sunxi_pinctrl_irq_unmask(d
);
677 static struct irq_chip sunxi_pinctrl_edge_irq_chip
= {
678 .name
= "sunxi_pio_edge",
679 .irq_ack
= sunxi_pinctrl_irq_ack
,
680 .irq_mask
= sunxi_pinctrl_irq_mask
,
681 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
682 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
683 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
684 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
685 .flags
= IRQCHIP_SKIP_SET_WAKE
,
688 static struct irq_chip sunxi_pinctrl_level_irq_chip
= {
689 .name
= "sunxi_pio_level",
690 .irq_eoi
= sunxi_pinctrl_irq_ack
,
691 .irq_mask
= sunxi_pinctrl_irq_mask
,
692 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
693 /* Define irq_enable / disable to avoid spurious irqs for drivers
694 * using these to suppress irqs while they clear the irq source */
695 .irq_enable
= sunxi_pinctrl_irq_ack_unmask
,
696 .irq_disable
= sunxi_pinctrl_irq_mask
,
697 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
698 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
699 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
700 .flags
= IRQCHIP_SKIP_SET_WAKE
| IRQCHIP_EOI_THREADED
|
701 IRQCHIP_EOI_IF_HANDLED
,
704 static int sunxi_pinctrl_irq_of_xlate(struct irq_domain
*d
,
705 struct device_node
*node
,
707 unsigned int intsize
,
708 unsigned long *out_hwirq
,
709 unsigned int *out_type
)
711 struct sunxi_pinctrl
*pctl
= d
->host_data
;
712 struct sunxi_desc_function
*desc
;
718 base
= PINS_PER_BANK
* intspec
[0];
719 pin
= pctl
->desc
->pin_base
+ base
+ intspec
[1];
721 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, pin
, "irq");
725 *out_hwirq
= desc
->irqbank
* PINS_PER_BANK
+ desc
->irqnum
;
726 *out_type
= intspec
[2];
731 static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops
= {
732 .xlate
= sunxi_pinctrl_irq_of_xlate
,
735 static void sunxi_pinctrl_irq_handler(struct irq_desc
*desc
)
737 unsigned int irq
= irq_desc_get_irq(desc
);
738 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
739 struct sunxi_pinctrl
*pctl
= irq_desc_get_handler_data(desc
);
740 unsigned long bank
, reg
, val
;
742 for (bank
= 0; bank
< pctl
->desc
->irq_banks
; bank
++)
743 if (irq
== pctl
->irq
[bank
])
746 if (bank
== pctl
->desc
->irq_banks
)
749 reg
= sunxi_irq_status_reg_from_bank(bank
, pctl
->desc
->irq_bank_base
);
750 val
= readl(pctl
->membase
+ reg
);
755 chained_irq_enter(chip
, desc
);
756 for_each_set_bit(irqoffset
, &val
, IRQ_PER_BANK
) {
757 int pin_irq
= irq_find_mapping(pctl
->domain
,
758 bank
* IRQ_PER_BANK
+ irqoffset
);
759 generic_handle_irq(pin_irq
);
761 chained_irq_exit(chip
, desc
);
765 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl
*pctl
,
768 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
771 /* function already there */
772 if (strcmp(func
->name
, name
) == 0) {
787 static int sunxi_pinctrl_build_state(struct platform_device
*pdev
)
789 struct sunxi_pinctrl
*pctl
= platform_get_drvdata(pdev
);
792 pctl
->ngroups
= pctl
->desc
->npins
;
794 /* Allocate groups */
795 pctl
->groups
= devm_kzalloc(&pdev
->dev
,
796 pctl
->ngroups
* sizeof(*pctl
->groups
),
801 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
802 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
803 struct sunxi_pinctrl_group
*group
= pctl
->groups
+ i
;
805 group
->name
= pin
->pin
.name
;
806 group
->pin
= pin
->pin
.number
;
810 * We suppose that we won't have any more functions than pins,
811 * we'll reallocate that later anyway
813 pctl
->functions
= devm_kzalloc(&pdev
->dev
,
814 pctl
->desc
->npins
* sizeof(*pctl
->functions
),
816 if (!pctl
->functions
)
819 /* Count functions and their associated groups */
820 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
821 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
822 struct sunxi_desc_function
*func
= pin
->functions
;
825 /* Create interrupt mapping while we're at it */
826 if (!strcmp(func
->name
, "irq")) {
827 int irqnum
= func
->irqnum
+ func
->irqbank
* IRQ_PER_BANK
;
828 pctl
->irq_array
[irqnum
] = pin
->pin
.number
;
831 sunxi_pinctrl_add_function(pctl
, func
->name
);
836 pctl
->functions
= krealloc(pctl
->functions
,
837 pctl
->nfunctions
* sizeof(*pctl
->functions
),
840 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
841 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
842 struct sunxi_desc_function
*func
= pin
->functions
;
845 struct sunxi_pinctrl_function
*func_item
;
846 const char **func_grp
;
848 func_item
= sunxi_pinctrl_find_function_by_name(pctl
,
853 if (!func_item
->groups
) {
855 devm_kzalloc(&pdev
->dev
,
856 func_item
->ngroups
* sizeof(*func_item
->groups
),
858 if (!func_item
->groups
)
862 func_grp
= func_item
->groups
;
866 *func_grp
= pin
->pin
.name
;
874 int sunxi_pinctrl_init(struct platform_device
*pdev
,
875 const struct sunxi_pinctrl_desc
*desc
)
877 struct device_node
*node
= pdev
->dev
.of_node
;
878 struct pinctrl_desc
*pctrl_desc
;
879 struct pinctrl_pin_desc
*pins
;
880 struct sunxi_pinctrl
*pctl
;
881 struct resource
*res
;
882 int i
, ret
, last_pin
;
885 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
888 platform_set_drvdata(pdev
, pctl
);
890 spin_lock_init(&pctl
->lock
);
892 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
893 pctl
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
894 if (IS_ERR(pctl
->membase
))
895 return PTR_ERR(pctl
->membase
);
897 pctl
->dev
= &pdev
->dev
;
900 pctl
->irq_array
= devm_kcalloc(&pdev
->dev
,
901 IRQ_PER_BANK
* pctl
->desc
->irq_banks
,
902 sizeof(*pctl
->irq_array
),
904 if (!pctl
->irq_array
)
907 ret
= sunxi_pinctrl_build_state(pdev
);
909 dev_err(&pdev
->dev
, "dt probe failed: %d\n", ret
);
913 pins
= devm_kzalloc(&pdev
->dev
,
914 pctl
->desc
->npins
* sizeof(*pins
),
919 for (i
= 0; i
< pctl
->desc
->npins
; i
++)
920 pins
[i
] = pctl
->desc
->pins
[i
].pin
;
922 pctrl_desc
= devm_kzalloc(&pdev
->dev
,
928 pctrl_desc
->name
= dev_name(&pdev
->dev
);
929 pctrl_desc
->owner
= THIS_MODULE
;
930 pctrl_desc
->pins
= pins
;
931 pctrl_desc
->npins
= pctl
->desc
->npins
;
932 pctrl_desc
->confops
= &sunxi_pconf_ops
;
933 pctrl_desc
->pctlops
= &sunxi_pctrl_ops
;
934 pctrl_desc
->pmxops
= &sunxi_pmx_ops
;
936 pctl
->pctl_dev
= pinctrl_register(pctrl_desc
,
938 if (IS_ERR(pctl
->pctl_dev
)) {
939 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
940 return PTR_ERR(pctl
->pctl_dev
);
943 pctl
->chip
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
->chip
), GFP_KERNEL
);
949 last_pin
= pctl
->desc
->pins
[pctl
->desc
->npins
- 1].pin
.number
;
950 pctl
->chip
->owner
= THIS_MODULE
;
951 pctl
->chip
->request
= gpiochip_generic_request
,
952 pctl
->chip
->free
= gpiochip_generic_free
,
953 pctl
->chip
->direction_input
= sunxi_pinctrl_gpio_direction_input
,
954 pctl
->chip
->direction_output
= sunxi_pinctrl_gpio_direction_output
,
955 pctl
->chip
->get
= sunxi_pinctrl_gpio_get
,
956 pctl
->chip
->set
= sunxi_pinctrl_gpio_set
,
957 pctl
->chip
->of_xlate
= sunxi_pinctrl_gpio_of_xlate
,
958 pctl
->chip
->to_irq
= sunxi_pinctrl_gpio_to_irq
,
959 pctl
->chip
->of_gpio_n_cells
= 3,
960 pctl
->chip
->can_sleep
= false,
961 pctl
->chip
->ngpio
= round_up(last_pin
, PINS_PER_BANK
) -
962 pctl
->desc
->pin_base
;
963 pctl
->chip
->label
= dev_name(&pdev
->dev
);
964 pctl
->chip
->parent
= &pdev
->dev
;
965 pctl
->chip
->base
= pctl
->desc
->pin_base
;
967 ret
= gpiochip_add_data(pctl
->chip
, pctl
);
971 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
972 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
974 ret
= gpiochip_add_pin_range(pctl
->chip
, dev_name(&pdev
->dev
),
975 pin
->pin
.number
- pctl
->desc
->pin_base
,
981 clk
= devm_clk_get(&pdev
->dev
, NULL
);
987 ret
= clk_prepare_enable(clk
);
991 pctl
->irq
= devm_kcalloc(&pdev
->dev
,
992 pctl
->desc
->irq_banks
,
1000 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
1001 pctl
->irq
[i
] = platform_get_irq(pdev
, i
);
1002 if (pctl
->irq
[i
] < 0) {
1008 pctl
->domain
= irq_domain_add_linear(node
,
1009 pctl
->desc
->irq_banks
* IRQ_PER_BANK
,
1010 &sunxi_pinctrl_irq_domain_ops
,
1012 if (!pctl
->domain
) {
1013 dev_err(&pdev
->dev
, "Couldn't register IRQ domain\n");
1018 for (i
= 0; i
< (pctl
->desc
->irq_banks
* IRQ_PER_BANK
); i
++) {
1019 int irqno
= irq_create_mapping(pctl
->domain
, i
);
1021 irq_set_chip_and_handler(irqno
, &sunxi_pinctrl_edge_irq_chip
,
1023 irq_set_chip_data(irqno
, pctl
);
1026 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
1027 /* Mask and clear all IRQs before registering a handler */
1028 writel(0, pctl
->membase
+ sunxi_irq_ctrl_reg_from_bank(i
,
1029 pctl
->desc
->irq_bank_base
));
1031 pctl
->membase
+ sunxi_irq_status_reg_from_bank(i
,
1032 pctl
->desc
->irq_bank_base
));
1034 irq_set_chained_handler_and_data(pctl
->irq
[i
],
1035 sunxi_pinctrl_irq_handler
,
1039 dev_info(&pdev
->dev
, "initialized sunXi PIO driver\n");
1044 clk_disable_unprepare(clk
);
1046 gpiochip_remove(pctl
->chip
);
1048 pinctrl_unregister(pctl
->pctl_dev
);