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.h>
16 #include <linux/irqdomain.h>
17 #include <linux/irqchip/chained_irq.h>
18 #include <linux/module.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 "pinctrl-sunxi.h"
34 static struct irq_chip sunxi_pinctrl_edge_irq_chip
;
35 static struct irq_chip sunxi_pinctrl_level_irq_chip
;
37 static struct sunxi_pinctrl_group
*
38 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl
*pctl
, const char *group
)
42 for (i
= 0; i
< pctl
->ngroups
; i
++) {
43 struct sunxi_pinctrl_group
*grp
= pctl
->groups
+ i
;
45 if (!strcmp(grp
->name
, group
))
52 static struct sunxi_pinctrl_function
*
53 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl
*pctl
,
56 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
59 for (i
= 0; i
< pctl
->nfunctions
; i
++) {
63 if (!strcmp(func
[i
].name
, name
))
70 static struct sunxi_desc_function
*
71 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl
*pctl
,
73 const char *func_name
)
77 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
78 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
80 if (!strcmp(pin
->pin
.name
, pin_name
)) {
81 struct sunxi_desc_function
*func
= pin
->functions
;
84 if (!strcmp(func
->name
, func_name
))
95 static struct sunxi_desc_function
*
96 sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl
*pctl
,
98 const char *func_name
)
102 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
103 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
105 if (pin
->pin
.number
== pin_num
) {
106 struct sunxi_desc_function
*func
= pin
->functions
;
109 if (!strcmp(func
->name
, func_name
))
120 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
122 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
124 return pctl
->ngroups
;
127 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
130 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
132 return pctl
->groups
[group
].name
;
135 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
137 const unsigned **pins
,
140 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
142 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
148 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
149 struct device_node
*node
,
150 struct pinctrl_map
**map
,
153 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
154 unsigned long *pinconfig
;
155 struct property
*prop
;
156 const char *function
;
158 int ret
, nmaps
, i
= 0;
164 ret
= of_property_read_string(node
, "allwinner,function", &function
);
167 "missing allwinner,function property in node %s\n",
172 nmaps
= of_property_count_strings(node
, "allwinner,pins") * 2;
175 "missing allwinner,pins property in node %s\n",
180 *map
= kmalloc(nmaps
* sizeof(struct pinctrl_map
), GFP_KERNEL
);
184 of_property_for_each_string(node
, "allwinner,pins", prop
, group
) {
185 struct sunxi_pinctrl_group
*grp
=
186 sunxi_pinctrl_find_group_by_name(pctl
, group
);
187 int j
= 0, configlen
= 0;
190 dev_err(pctl
->dev
, "unknown pin %s", group
);
194 if (!sunxi_pinctrl_desc_find_function_by_name(pctl
,
197 dev_err(pctl
->dev
, "unsupported function %s on pin %s",
202 (*map
)[i
].type
= PIN_MAP_TYPE_MUX_GROUP
;
203 (*map
)[i
].data
.mux
.group
= group
;
204 (*map
)[i
].data
.mux
.function
= function
;
208 (*map
)[i
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
209 (*map
)[i
].data
.configs
.group_or_pin
= group
;
211 if (of_find_property(node
, "allwinner,drive", NULL
))
213 if (of_find_property(node
, "allwinner,pull", NULL
))
216 pinconfig
= kzalloc(configlen
* sizeof(*pinconfig
), GFP_KERNEL
);
218 if (!of_property_read_u32(node
, "allwinner,drive", &val
)) {
219 u16 strength
= (val
+ 1) * 10;
221 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH
,
225 if (!of_property_read_u32(node
, "allwinner,pull", &val
)) {
226 enum pin_config_param pull
= PIN_CONFIG_END
;
228 pull
= PIN_CONFIG_BIAS_PULL_UP
;
230 pull
= PIN_CONFIG_BIAS_PULL_DOWN
;
231 pinconfig
[j
++] = pinconf_to_config_packed(pull
, 0);
234 (*map
)[i
].data
.configs
.configs
= pinconfig
;
235 (*map
)[i
].data
.configs
.num_configs
= configlen
;
245 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
246 struct pinctrl_map
*map
,
251 for (i
= 0; i
< num_maps
; i
++) {
252 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
253 kfree(map
[i
].data
.configs
.configs
);
259 static const struct pinctrl_ops sunxi_pctrl_ops
= {
260 .dt_node_to_map
= sunxi_pctrl_dt_node_to_map
,
261 .dt_free_map
= sunxi_pctrl_dt_free_map
,
262 .get_groups_count
= sunxi_pctrl_get_groups_count
,
263 .get_group_name
= sunxi_pctrl_get_group_name
,
264 .get_group_pins
= sunxi_pctrl_get_group_pins
,
267 static int sunxi_pconf_group_get(struct pinctrl_dev
*pctldev
,
269 unsigned long *config
)
271 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
273 *config
= pctl
->groups
[group
].config
;
278 static int sunxi_pconf_group_set(struct pinctrl_dev
*pctldev
,
280 unsigned long *configs
,
281 unsigned num_configs
)
283 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
284 struct sunxi_pinctrl_group
*g
= &pctl
->groups
[group
];
286 unsigned pin
= g
->pin
- pctl
->desc
->pin_base
;
292 spin_lock_irqsave(&pctl
->lock
, flags
);
294 for (i
= 0; i
< num_configs
; i
++) {
295 switch (pinconf_to_config_param(configs
[i
])) {
296 case PIN_CONFIG_DRIVE_STRENGTH
:
297 strength
= pinconf_to_config_argument(configs
[i
]);
299 spin_unlock_irqrestore(&pctl
->lock
, flags
);
303 * We convert from mA to what the register expects:
309 dlevel
= strength
/ 10 - 1;
310 val
= readl(pctl
->membase
+ sunxi_dlevel_reg(pin
));
311 mask
= DLEVEL_PINS_MASK
<< sunxi_dlevel_offset(pin
);
313 | dlevel
<< sunxi_dlevel_offset(pin
),
314 pctl
->membase
+ sunxi_dlevel_reg(pin
));
316 case PIN_CONFIG_BIAS_PULL_UP
:
317 val
= readl(pctl
->membase
+ sunxi_pull_reg(pin
));
318 mask
= PULL_PINS_MASK
<< sunxi_pull_offset(pin
);
319 writel((val
& ~mask
) | 1 << sunxi_pull_offset(pin
),
320 pctl
->membase
+ sunxi_pull_reg(pin
));
322 case PIN_CONFIG_BIAS_PULL_DOWN
:
323 val
= readl(pctl
->membase
+ sunxi_pull_reg(pin
));
324 mask
= PULL_PINS_MASK
<< sunxi_pull_offset(pin
);
325 writel((val
& ~mask
) | 2 << sunxi_pull_offset(pin
),
326 pctl
->membase
+ sunxi_pull_reg(pin
));
331 /* cache the config value */
332 g
->config
= configs
[i
];
333 } /* for each config */
335 spin_unlock_irqrestore(&pctl
->lock
, flags
);
340 static const struct pinconf_ops sunxi_pconf_ops
= {
341 .pin_config_group_get
= sunxi_pconf_group_get
,
342 .pin_config_group_set
= sunxi_pconf_group_set
,
345 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
347 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
349 return pctl
->nfunctions
;
352 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
355 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
357 return pctl
->functions
[function
].name
;
360 static int sunxi_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
362 const char * const **groups
,
363 unsigned * const num_groups
)
365 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
367 *groups
= pctl
->functions
[function
].groups
;
368 *num_groups
= pctl
->functions
[function
].ngroups
;
373 static void sunxi_pmx_set(struct pinctrl_dev
*pctldev
,
377 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
381 spin_lock_irqsave(&pctl
->lock
, flags
);
383 pin
-= pctl
->desc
->pin_base
;
384 val
= readl(pctl
->membase
+ sunxi_mux_reg(pin
));
385 mask
= MUX_PINS_MASK
<< sunxi_mux_offset(pin
);
386 writel((val
& ~mask
) | config
<< sunxi_mux_offset(pin
),
387 pctl
->membase
+ sunxi_mux_reg(pin
));
389 spin_unlock_irqrestore(&pctl
->lock
, flags
);
392 static int sunxi_pmx_enable(struct pinctrl_dev
*pctldev
,
396 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
397 struct sunxi_pinctrl_group
*g
= pctl
->groups
+ group
;
398 struct sunxi_pinctrl_function
*func
= pctl
->functions
+ function
;
399 struct sunxi_desc_function
*desc
=
400 sunxi_pinctrl_desc_find_function_by_name(pctl
,
407 sunxi_pmx_set(pctldev
, g
->pin
, desc
->muxval
);
413 sunxi_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
414 struct pinctrl_gpio_range
*range
,
418 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
419 struct sunxi_desc_function
*desc
;
427 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, offset
, func
);
431 sunxi_pmx_set(pctldev
, offset
, desc
->muxval
);
436 static const struct pinmux_ops sunxi_pmx_ops
= {
437 .get_functions_count
= sunxi_pmx_get_funcs_cnt
,
438 .get_function_name
= sunxi_pmx_get_func_name
,
439 .get_function_groups
= sunxi_pmx_get_func_groups
,
440 .enable
= sunxi_pmx_enable
,
441 .gpio_set_direction
= sunxi_pmx_gpio_set_direction
,
444 static int sunxi_pinctrl_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
446 return pinctrl_request_gpio(chip
->base
+ offset
);
449 static void sunxi_pinctrl_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
451 pinctrl_free_gpio(chip
->base
+ offset
);
454 static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip
*chip
,
457 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
460 static int sunxi_pinctrl_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
462 struct sunxi_pinctrl
*pctl
= dev_get_drvdata(chip
->dev
);
464 u32 reg
= sunxi_data_reg(offset
);
465 u8 index
= sunxi_data_offset(offset
);
466 u32 val
= (readl(pctl
->membase
+ reg
) >> index
) & DATA_PINS_MASK
;
471 static void sunxi_pinctrl_gpio_set(struct gpio_chip
*chip
,
472 unsigned offset
, int value
)
474 struct sunxi_pinctrl
*pctl
= dev_get_drvdata(chip
->dev
);
475 u32 reg
= sunxi_data_reg(offset
);
476 u8 index
= sunxi_data_offset(offset
);
480 spin_lock_irqsave(&pctl
->lock
, flags
);
482 regval
= readl(pctl
->membase
+ reg
);
485 regval
|= BIT(index
);
487 regval
&= ~(BIT(index
));
489 writel(regval
, pctl
->membase
+ reg
);
491 spin_unlock_irqrestore(&pctl
->lock
, flags
);
494 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip
*chip
,
495 unsigned offset
, int value
)
497 sunxi_pinctrl_gpio_set(chip
, offset
, value
);
498 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
501 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip
*gc
,
502 const struct of_phandle_args
*gpiospec
,
507 base
= PINS_PER_BANK
* gpiospec
->args
[0];
508 pin
= base
+ gpiospec
->args
[1];
514 *flags
= gpiospec
->args
[2];
519 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
521 struct sunxi_pinctrl
*pctl
= dev_get_drvdata(chip
->dev
);
522 struct sunxi_desc_function
*desc
;
523 unsigned pinnum
= pctl
->desc
->pin_base
+ offset
;
526 if (offset
>= chip
->ngpio
)
529 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, pinnum
, "irq");
533 irqnum
= desc
->irqbank
* IRQ_PER_BANK
+ desc
->irqnum
;
535 dev_dbg(chip
->dev
, "%s: request IRQ for GPIO %d, return %d\n",
536 chip
->label
, offset
+ chip
->base
, irqnum
);
538 return irq_find_mapping(pctl
->domain
, irqnum
);
541 static int sunxi_pinctrl_irq_request_resources(struct irq_data
*d
)
543 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
544 struct sunxi_desc_function
*func
;
547 func
= sunxi_pinctrl_desc_find_function_by_pin(pctl
,
548 pctl
->irq_array
[d
->hwirq
], "irq");
552 ret
= gpio_lock_as_irq(pctl
->chip
,
553 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
555 dev_err(pctl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
560 /* Change muxing to INT mode */
561 sunxi_pmx_set(pctl
->pctl_dev
, pctl
->irq_array
[d
->hwirq
], func
->muxval
);
566 static void sunxi_pinctrl_irq_release_resources(struct irq_data
*d
)
568 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
570 gpio_unlock_as_irq(pctl
->chip
,
571 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
574 static int sunxi_pinctrl_irq_set_type(struct irq_data
*d
, unsigned int type
)
576 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
577 struct irq_desc
*desc
= container_of(d
, struct irq_desc
, irq_data
);
578 u32 reg
= sunxi_irq_cfg_reg(d
->hwirq
);
579 u8 index
= sunxi_irq_cfg_offset(d
->hwirq
);
585 case IRQ_TYPE_EDGE_RISING
:
586 mode
= IRQ_EDGE_RISING
;
588 case IRQ_TYPE_EDGE_FALLING
:
589 mode
= IRQ_EDGE_FALLING
;
591 case IRQ_TYPE_EDGE_BOTH
:
592 mode
= IRQ_EDGE_BOTH
;
594 case IRQ_TYPE_LEVEL_HIGH
:
595 mode
= IRQ_LEVEL_HIGH
;
597 case IRQ_TYPE_LEVEL_LOW
:
598 mode
= IRQ_LEVEL_LOW
;
604 if (type
& IRQ_TYPE_LEVEL_MASK
) {
605 d
->chip
= &sunxi_pinctrl_level_irq_chip
;
606 desc
->handle_irq
= handle_fasteoi_irq
;
608 d
->chip
= &sunxi_pinctrl_edge_irq_chip
;
609 desc
->handle_irq
= handle_edge_irq
;
612 spin_lock_irqsave(&pctl
->lock
, flags
);
614 regval
= readl(pctl
->membase
+ reg
);
615 regval
&= ~(IRQ_CFG_IRQ_MASK
<< index
);
616 writel(regval
| (mode
<< index
), pctl
->membase
+ reg
);
618 spin_unlock_irqrestore(&pctl
->lock
, flags
);
623 static void sunxi_pinctrl_irq_ack(struct irq_data
*d
)
625 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
626 u32 status_reg
= sunxi_irq_status_reg(d
->hwirq
);
627 u8 status_idx
= sunxi_irq_status_offset(d
->hwirq
);
630 writel(1 << status_idx
, pctl
->membase
+ status_reg
);
633 static void sunxi_pinctrl_irq_mask(struct irq_data
*d
)
635 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
636 u32 reg
= sunxi_irq_ctrl_reg(d
->hwirq
);
637 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
641 spin_lock_irqsave(&pctl
->lock
, flags
);
644 val
= readl(pctl
->membase
+ reg
);
645 writel(val
& ~(1 << idx
), pctl
->membase
+ reg
);
647 spin_unlock_irqrestore(&pctl
->lock
, flags
);
650 static void sunxi_pinctrl_irq_unmask(struct irq_data
*d
)
652 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
653 u32 reg
= sunxi_irq_ctrl_reg(d
->hwirq
);
654 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
658 spin_lock_irqsave(&pctl
->lock
, flags
);
661 val
= readl(pctl
->membase
+ reg
);
662 writel(val
| (1 << idx
), pctl
->membase
+ reg
);
664 spin_unlock_irqrestore(&pctl
->lock
, flags
);
667 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data
*d
)
669 sunxi_pinctrl_irq_ack(d
);
670 sunxi_pinctrl_irq_unmask(d
);
673 static struct irq_chip sunxi_pinctrl_edge_irq_chip
= {
674 .irq_ack
= sunxi_pinctrl_irq_ack
,
675 .irq_mask
= sunxi_pinctrl_irq_mask
,
676 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
677 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
678 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
679 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
680 .flags
= IRQCHIP_SKIP_SET_WAKE
,
683 static struct irq_chip sunxi_pinctrl_level_irq_chip
= {
684 .irq_eoi
= sunxi_pinctrl_irq_ack
,
685 .irq_mask
= sunxi_pinctrl_irq_mask
,
686 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
687 /* Define irq_enable / disable to avoid spurious irqs for drivers
688 * using these to suppress irqs while they clear the irq source */
689 .irq_enable
= sunxi_pinctrl_irq_ack_unmask
,
690 .irq_disable
= sunxi_pinctrl_irq_mask
,
691 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
692 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
693 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
694 .flags
= IRQCHIP_SKIP_SET_WAKE
| IRQCHIP_EOI_THREADED
|
695 IRQCHIP_EOI_IF_HANDLED
,
698 static void sunxi_pinctrl_irq_handler(unsigned irq
, struct irq_desc
*desc
)
700 struct irq_chip
*chip
= irq_get_chip(irq
);
701 struct sunxi_pinctrl
*pctl
= irq_get_handler_data(irq
);
702 unsigned long bank
, reg
, val
;
704 for (bank
= 0; bank
< pctl
->desc
->irq_banks
; bank
++)
705 if (irq
== pctl
->irq
[bank
])
708 if (bank
== pctl
->desc
->irq_banks
)
711 reg
= sunxi_irq_status_reg_from_bank(bank
);
712 val
= readl(pctl
->membase
+ reg
);
717 chained_irq_enter(chip
, desc
);
718 for_each_set_bit(irqoffset
, &val
, IRQ_PER_BANK
) {
719 int pin_irq
= irq_find_mapping(pctl
->domain
,
720 bank
* IRQ_PER_BANK
+ irqoffset
);
721 generic_handle_irq(pin_irq
);
723 chained_irq_exit(chip
, desc
);
727 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl
*pctl
,
730 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
733 /* function already there */
734 if (strcmp(func
->name
, name
) == 0) {
749 static int sunxi_pinctrl_build_state(struct platform_device
*pdev
)
751 struct sunxi_pinctrl
*pctl
= platform_get_drvdata(pdev
);
754 pctl
->ngroups
= pctl
->desc
->npins
;
756 /* Allocate groups */
757 pctl
->groups
= devm_kzalloc(&pdev
->dev
,
758 pctl
->ngroups
* sizeof(*pctl
->groups
),
763 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
764 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
765 struct sunxi_pinctrl_group
*group
= pctl
->groups
+ i
;
767 group
->name
= pin
->pin
.name
;
768 group
->pin
= pin
->pin
.number
;
772 * We suppose that we won't have any more functions than pins,
773 * we'll reallocate that later anyway
775 pctl
->functions
= devm_kzalloc(&pdev
->dev
,
776 pctl
->desc
->npins
* sizeof(*pctl
->functions
),
778 if (!pctl
->functions
)
781 /* Count functions and their associated groups */
782 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
783 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
784 struct sunxi_desc_function
*func
= pin
->functions
;
787 /* Create interrupt mapping while we're at it */
788 if (!strcmp(func
->name
, "irq")) {
789 int irqnum
= func
->irqnum
+ func
->irqbank
* IRQ_PER_BANK
;
790 pctl
->irq_array
[irqnum
] = pin
->pin
.number
;
793 sunxi_pinctrl_add_function(pctl
, func
->name
);
798 pctl
->functions
= krealloc(pctl
->functions
,
799 pctl
->nfunctions
* sizeof(*pctl
->functions
),
802 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
803 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
804 struct sunxi_desc_function
*func
= pin
->functions
;
807 struct sunxi_pinctrl_function
*func_item
;
808 const char **func_grp
;
810 func_item
= sunxi_pinctrl_find_function_by_name(pctl
,
815 if (!func_item
->groups
) {
817 devm_kzalloc(&pdev
->dev
,
818 func_item
->ngroups
* sizeof(*func_item
->groups
),
820 if (!func_item
->groups
)
824 func_grp
= func_item
->groups
;
828 *func_grp
= pin
->pin
.name
;
836 int sunxi_pinctrl_init(struct platform_device
*pdev
,
837 const struct sunxi_pinctrl_desc
*desc
)
839 struct device_node
*node
= pdev
->dev
.of_node
;
840 struct pinctrl_desc
*pctrl_desc
;
841 struct pinctrl_pin_desc
*pins
;
842 struct sunxi_pinctrl
*pctl
;
843 struct resource
*res
;
844 int i
, ret
, last_pin
;
847 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
850 platform_set_drvdata(pdev
, pctl
);
852 spin_lock_init(&pctl
->lock
);
854 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
855 pctl
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
856 if (IS_ERR(pctl
->membase
))
857 return PTR_ERR(pctl
->membase
);
859 pctl
->dev
= &pdev
->dev
;
862 pctl
->irq_array
= devm_kcalloc(&pdev
->dev
,
863 IRQ_PER_BANK
* pctl
->desc
->irq_banks
,
864 sizeof(*pctl
->irq_array
),
866 if (!pctl
->irq_array
)
869 ret
= sunxi_pinctrl_build_state(pdev
);
871 dev_err(&pdev
->dev
, "dt probe failed: %d\n", ret
);
875 pins
= devm_kzalloc(&pdev
->dev
,
876 pctl
->desc
->npins
* sizeof(*pins
),
881 for (i
= 0; i
< pctl
->desc
->npins
; i
++)
882 pins
[i
] = pctl
->desc
->pins
[i
].pin
;
884 pctrl_desc
= devm_kzalloc(&pdev
->dev
,
890 pctrl_desc
->name
= dev_name(&pdev
->dev
);
891 pctrl_desc
->owner
= THIS_MODULE
;
892 pctrl_desc
->pins
= pins
;
893 pctrl_desc
->npins
= pctl
->desc
->npins
;
894 pctrl_desc
->confops
= &sunxi_pconf_ops
;
895 pctrl_desc
->pctlops
= &sunxi_pctrl_ops
;
896 pctrl_desc
->pmxops
= &sunxi_pmx_ops
;
898 pctl
->pctl_dev
= pinctrl_register(pctrl_desc
,
900 if (!pctl
->pctl_dev
) {
901 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
905 pctl
->chip
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
->chip
), GFP_KERNEL
);
911 last_pin
= pctl
->desc
->pins
[pctl
->desc
->npins
- 1].pin
.number
;
912 pctl
->chip
->owner
= THIS_MODULE
;
913 pctl
->chip
->request
= sunxi_pinctrl_gpio_request
,
914 pctl
->chip
->free
= sunxi_pinctrl_gpio_free
,
915 pctl
->chip
->direction_input
= sunxi_pinctrl_gpio_direction_input
,
916 pctl
->chip
->direction_output
= sunxi_pinctrl_gpio_direction_output
,
917 pctl
->chip
->get
= sunxi_pinctrl_gpio_get
,
918 pctl
->chip
->set
= sunxi_pinctrl_gpio_set
,
919 pctl
->chip
->of_xlate
= sunxi_pinctrl_gpio_of_xlate
,
920 pctl
->chip
->to_irq
= sunxi_pinctrl_gpio_to_irq
,
921 pctl
->chip
->of_gpio_n_cells
= 3,
922 pctl
->chip
->can_sleep
= false,
923 pctl
->chip
->ngpio
= round_up(last_pin
, PINS_PER_BANK
) -
924 pctl
->desc
->pin_base
;
925 pctl
->chip
->label
= dev_name(&pdev
->dev
);
926 pctl
->chip
->dev
= &pdev
->dev
;
927 pctl
->chip
->base
= pctl
->desc
->pin_base
;
929 ret
= gpiochip_add(pctl
->chip
);
933 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
934 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
936 ret
= gpiochip_add_pin_range(pctl
->chip
, dev_name(&pdev
->dev
),
937 pin
->pin
.number
- pctl
->desc
->pin_base
,
943 clk
= devm_clk_get(&pdev
->dev
, NULL
);
949 ret
= clk_prepare_enable(clk
);
953 pctl
->irq
= devm_kcalloc(&pdev
->dev
,
954 pctl
->desc
->irq_banks
,
962 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
963 pctl
->irq
[i
] = platform_get_irq(pdev
, i
);
964 if (pctl
->irq
[i
] < 0) {
970 pctl
->domain
= irq_domain_add_linear(node
,
971 pctl
->desc
->irq_banks
* IRQ_PER_BANK
,
972 &irq_domain_simple_ops
,
975 dev_err(&pdev
->dev
, "Couldn't register IRQ domain\n");
980 for (i
= 0; i
< (pctl
->desc
->irq_banks
* IRQ_PER_BANK
); i
++) {
981 int irqno
= irq_create_mapping(pctl
->domain
, i
);
983 irq_set_chip_and_handler(irqno
, &sunxi_pinctrl_edge_irq_chip
,
985 irq_set_chip_data(irqno
, pctl
);
988 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
989 /* Mask and clear all IRQs before registering a handler */
990 writel(0, pctl
->membase
+ sunxi_irq_ctrl_reg_from_bank(i
));
992 pctl
->membase
+ sunxi_irq_status_reg_from_bank(i
));
994 irq_set_chained_handler(pctl
->irq
[i
],
995 sunxi_pinctrl_irq_handler
);
996 irq_set_handler_data(pctl
->irq
[i
], pctl
);
999 dev_info(&pdev
->dev
, "initialized sunXi PIO driver\n");
1004 clk_disable_unprepare(clk
);
1006 gpiochip_remove(pctl
->chip
);
1008 pinctrl_unregister(pctl
->pctl_dev
);