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_clk.h>
21 #include <linux/of_address.h>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #include <dt-bindings/pinctrl/sun4i-a10.h>
36 #include "pinctrl-sunxi.h"
38 static struct irq_chip sunxi_pinctrl_edge_irq_chip
;
39 static struct irq_chip sunxi_pinctrl_level_irq_chip
;
41 static struct sunxi_pinctrl_group
*
42 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl
*pctl
, const char *group
)
46 for (i
= 0; i
< pctl
->ngroups
; i
++) {
47 struct sunxi_pinctrl_group
*grp
= pctl
->groups
+ i
;
49 if (!strcmp(grp
->name
, group
))
56 static struct sunxi_pinctrl_function
*
57 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl
*pctl
,
60 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
63 for (i
= 0; i
< pctl
->nfunctions
; i
++) {
67 if (!strcmp(func
[i
].name
, name
))
74 static struct sunxi_desc_function
*
75 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl
*pctl
,
77 const char *func_name
)
81 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
82 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
84 if (!strcmp(pin
->pin
.name
, pin_name
)) {
85 struct sunxi_desc_function
*func
= pin
->functions
;
88 if (!strcmp(func
->name
, func_name
) &&
90 func
->variant
& pctl
->variant
))
101 static struct sunxi_desc_function
*
102 sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl
*pctl
,
104 const char *func_name
)
108 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
109 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
111 if (pin
->pin
.number
== pin_num
) {
112 struct sunxi_desc_function
*func
= pin
->functions
;
115 if (!strcmp(func
->name
, func_name
))
126 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
128 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
130 return pctl
->ngroups
;
133 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
136 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
138 return pctl
->groups
[group
].name
;
141 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
143 const unsigned **pins
,
146 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
148 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
154 static bool sunxi_pctrl_has_bias_prop(struct device_node
*node
)
156 return of_find_property(node
, "bias-pull-up", NULL
) ||
157 of_find_property(node
, "bias-pull-down", NULL
) ||
158 of_find_property(node
, "bias-disable", NULL
) ||
159 of_find_property(node
, "allwinner,pull", NULL
);
162 static bool sunxi_pctrl_has_drive_prop(struct device_node
*node
)
164 return of_find_property(node
, "drive-strength", NULL
) ||
165 of_find_property(node
, "allwinner,drive", NULL
);
168 static int sunxi_pctrl_parse_bias_prop(struct device_node
*node
)
172 /* Try the new style binding */
173 if (of_find_property(node
, "bias-pull-up", NULL
))
174 return PIN_CONFIG_BIAS_PULL_UP
;
176 if (of_find_property(node
, "bias-pull-down", NULL
))
177 return PIN_CONFIG_BIAS_PULL_DOWN
;
179 if (of_find_property(node
, "bias-disable", NULL
))
180 return PIN_CONFIG_BIAS_DISABLE
;
182 /* And fall back to the old binding */
183 if (of_property_read_u32(node
, "allwinner,pull", &val
))
187 case SUN4I_PINCTRL_NO_PULL
:
188 return PIN_CONFIG_BIAS_DISABLE
;
189 case SUN4I_PINCTRL_PULL_UP
:
190 return PIN_CONFIG_BIAS_PULL_UP
;
191 case SUN4I_PINCTRL_PULL_DOWN
:
192 return PIN_CONFIG_BIAS_PULL_DOWN
;
198 static int sunxi_pctrl_parse_drive_prop(struct device_node
*node
)
202 /* Try the new style binding */
203 if (!of_property_read_u32(node
, "drive-strength", &val
)) {
204 /* We can't go below 10mA ... */
208 /* ... and only up to 40 mA ... */
212 /* by steps of 10 mA */
213 return rounddown(val
, 10);
216 /* And then fall back to the old binding */
217 if (of_property_read_u32(node
, "allwinner,drive", &val
))
220 return (val
+ 1) * 10;
223 static const char *sunxi_pctrl_parse_function_prop(struct device_node
*node
)
225 const char *function
;
228 /* Try the generic binding */
229 ret
= of_property_read_string(node
, "function", &function
);
233 /* And fall back to our legacy one */
234 ret
= of_property_read_string(node
, "allwinner,function", &function
);
241 static const char *sunxi_pctrl_find_pins_prop(struct device_node
*node
,
246 /* Try the generic binding */
247 count
= of_property_count_strings(node
, "pins");
253 /* And fall back to our legacy one */
254 count
= of_property_count_strings(node
, "allwinner,pins");
257 return "allwinner,pins";
263 static unsigned long *sunxi_pctrl_build_pin_config(struct device_node
*node
,
266 unsigned long *pinconfig
;
267 unsigned int configlen
= 0, idx
= 0;
270 if (sunxi_pctrl_has_drive_prop(node
))
272 if (sunxi_pctrl_has_bias_prop(node
))
276 * If we don't have any configuration, bail out
281 pinconfig
= kcalloc(configlen
, sizeof(*pinconfig
), GFP_KERNEL
);
283 return ERR_PTR(-ENOMEM
);
285 if (sunxi_pctrl_has_drive_prop(node
)) {
286 int drive
= sunxi_pctrl_parse_drive_prop(node
);
292 pinconfig
[idx
++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH
,
296 if (sunxi_pctrl_has_bias_prop(node
)) {
297 int pull
= sunxi_pctrl_parse_bias_prop(node
);
304 if (pull
!= PIN_CONFIG_BIAS_DISABLE
)
305 arg
= 1; /* hardware uses weak pull resistors */
307 pinconfig
[idx
++] = pinconf_to_config_packed(pull
, arg
);
319 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
320 struct device_node
*node
,
321 struct pinctrl_map
**map
,
324 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
325 unsigned long *pinconfig
;
326 struct property
*prop
;
327 const char *function
, *pin_prop
;
329 int ret
, npins
, nmaps
, configlen
= 0, i
= 0;
334 function
= sunxi_pctrl_parse_function_prop(node
);
336 dev_err(pctl
->dev
, "missing function property in node %pOFn\n",
341 pin_prop
= sunxi_pctrl_find_pins_prop(node
, &npins
);
343 dev_err(pctl
->dev
, "missing pins property in node %pOFn\n",
349 * We have two maps for each pin: one for the function, one
350 * for the configuration (bias, strength, etc).
352 * We might be slightly overshooting, since we might not have
356 *map
= kmalloc_array(nmaps
, sizeof(struct pinctrl_map
), GFP_KERNEL
);
360 pinconfig
= sunxi_pctrl_build_pin_config(node
, &configlen
);
361 if (IS_ERR(pinconfig
)) {
362 ret
= PTR_ERR(pinconfig
);
366 of_property_for_each_string(node
, pin_prop
, prop
, group
) {
367 struct sunxi_pinctrl_group
*grp
=
368 sunxi_pinctrl_find_group_by_name(pctl
, group
);
371 dev_err(pctl
->dev
, "unknown pin %s", group
);
375 if (!sunxi_pinctrl_desc_find_function_by_name(pctl
,
378 dev_err(pctl
->dev
, "unsupported function %s on pin %s",
383 (*map
)[i
].type
= PIN_MAP_TYPE_MUX_GROUP
;
384 (*map
)[i
].data
.mux
.group
= group
;
385 (*map
)[i
].data
.mux
.function
= function
;
390 (*map
)[i
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
391 (*map
)[i
].data
.configs
.group_or_pin
= group
;
392 (*map
)[i
].data
.configs
.configs
= pinconfig
;
393 (*map
)[i
].data
.configs
.num_configs
= configlen
;
401 * We know have the number of maps we need, we can resize our
404 *map
= krealloc(*map
, i
* sizeof(struct pinctrl_map
), GFP_KERNEL
);
416 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
417 struct pinctrl_map
*map
,
422 /* pin config is never in the first map */
423 for (i
= 1; i
< num_maps
; i
++) {
424 if (map
[i
].type
!= PIN_MAP_TYPE_CONFIGS_GROUP
)
428 * All the maps share the same pin config,
429 * free only the first one we find.
431 kfree(map
[i
].data
.configs
.configs
);
438 static const struct pinctrl_ops sunxi_pctrl_ops
= {
439 .dt_node_to_map
= sunxi_pctrl_dt_node_to_map
,
440 .dt_free_map
= sunxi_pctrl_dt_free_map
,
441 .get_groups_count
= sunxi_pctrl_get_groups_count
,
442 .get_group_name
= sunxi_pctrl_get_group_name
,
443 .get_group_pins
= sunxi_pctrl_get_group_pins
,
446 static int sunxi_pconf_reg(unsigned pin
, enum pin_config_param param
,
447 u32
*offset
, u32
*shift
, u32
*mask
)
450 case PIN_CONFIG_DRIVE_STRENGTH
:
451 *offset
= sunxi_dlevel_reg(pin
);
452 *shift
= sunxi_dlevel_offset(pin
);
453 *mask
= DLEVEL_PINS_MASK
;
456 case PIN_CONFIG_BIAS_PULL_UP
:
457 case PIN_CONFIG_BIAS_PULL_DOWN
:
458 case PIN_CONFIG_BIAS_DISABLE
:
459 *offset
= sunxi_pull_reg(pin
);
460 *shift
= sunxi_pull_offset(pin
);
461 *mask
= PULL_PINS_MASK
;
471 static int sunxi_pconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
472 unsigned long *config
)
474 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
475 enum pin_config_param param
= pinconf_to_config_param(*config
);
476 u32 offset
, shift
, mask
, val
;
480 pin
-= pctl
->desc
->pin_base
;
482 ret
= sunxi_pconf_reg(pin
, param
, &offset
, &shift
, &mask
);
486 val
= (readl(pctl
->membase
+ offset
) >> shift
) & mask
;
488 switch (pinconf_to_config_param(*config
)) {
489 case PIN_CONFIG_DRIVE_STRENGTH
:
490 arg
= (val
+ 1) * 10;
493 case PIN_CONFIG_BIAS_PULL_UP
:
494 if (val
!= SUN4I_PINCTRL_PULL_UP
)
496 arg
= 1; /* hardware is weak pull-up */
499 case PIN_CONFIG_BIAS_PULL_DOWN
:
500 if (val
!= SUN4I_PINCTRL_PULL_DOWN
)
502 arg
= 1; /* hardware is weak pull-down */
505 case PIN_CONFIG_BIAS_DISABLE
:
506 if (val
!= SUN4I_PINCTRL_NO_PULL
)
512 /* sunxi_pconf_reg should catch anything unsupported */
517 *config
= pinconf_to_config_packed(param
, arg
);
522 static int sunxi_pconf_group_get(struct pinctrl_dev
*pctldev
,
524 unsigned long *config
)
526 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
527 struct sunxi_pinctrl_group
*g
= &pctl
->groups
[group
];
529 /* We only support 1 pin per group. Chain it to the pin callback */
530 return sunxi_pconf_get(pctldev
, g
->pin
, config
);
533 static int sunxi_pconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
534 unsigned long *configs
, unsigned num_configs
)
536 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
539 for (i
= 0; i
< num_configs
; i
++) {
540 enum pin_config_param param
;
542 u32 offset
, shift
, mask
, reg
;
546 param
= pinconf_to_config_param(configs
[i
]);
547 arg
= pinconf_to_config_argument(configs
[i
]);
549 ret
= sunxi_pconf_reg(pin
, param
, &offset
, &shift
, &mask
);
554 case PIN_CONFIG_DRIVE_STRENGTH
:
555 if (arg
< 10 || arg
> 40)
558 * We convert from mA to what the register expects:
566 case PIN_CONFIG_BIAS_DISABLE
:
569 case PIN_CONFIG_BIAS_PULL_UP
:
574 case PIN_CONFIG_BIAS_PULL_DOWN
:
580 /* sunxi_pconf_reg should catch anything unsupported */
585 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
586 reg
= readl(pctl
->membase
+ offset
);
587 reg
&= ~(mask
<< shift
);
588 writel(reg
| val
<< shift
, pctl
->membase
+ offset
);
589 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
590 } /* for each config */
595 static int sunxi_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
596 unsigned long *configs
, unsigned num_configs
)
598 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
599 struct sunxi_pinctrl_group
*g
= &pctl
->groups
[group
];
601 /* We only support 1 pin per group. Chain it to the pin callback */
602 return sunxi_pconf_set(pctldev
, g
->pin
, configs
, num_configs
);
605 static const struct pinconf_ops sunxi_pconf_ops
= {
607 .pin_config_get
= sunxi_pconf_get
,
608 .pin_config_set
= sunxi_pconf_set
,
609 .pin_config_group_get
= sunxi_pconf_group_get
,
610 .pin_config_group_set
= sunxi_pconf_group_set
,
613 static int sunxi_pinctrl_set_io_bias_cfg(struct sunxi_pinctrl
*pctl
,
615 struct regulator
*supply
)
617 unsigned short bank
= pin
/ PINS_PER_BANK
;
622 if (!pctl
->desc
->io_bias_cfg_variant
)
625 uV
= regulator_get_voltage(supply
);
629 /* Might be dummy regulator with no voltage set */
633 switch (pctl
->desc
->io_bias_cfg_variant
) {
634 case BIAS_VOLTAGE_GRP_CONFIG
:
636 * Configured value must be equal or greater to actual
640 val
= 0x0; /* 1.8V */
641 else if (uV
<= 2500000)
642 val
= 0x6; /* 2.5V */
643 else if (uV
<= 2800000)
644 val
= 0x9; /* 2.8V */
645 else if (uV
<= 3000000)
646 val
= 0xA; /* 3.0V */
648 val
= 0xD; /* 3.3V */
650 pin
-= pctl
->desc
->pin_base
;
652 reg
= readl(pctl
->membase
+ sunxi_grp_config_reg(pin
));
653 reg
&= ~IO_BIAS_MASK
;
654 writel(reg
| val
, pctl
->membase
+ sunxi_grp_config_reg(pin
));
656 case BIAS_VOLTAGE_PIO_POW_MODE_SEL
:
657 val
= uV
<= 1800000 ? 1 : 0;
659 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
660 reg
= readl(pctl
->membase
+ PIO_POW_MOD_SEL_REG
);
662 writel(reg
| val
<< bank
, pctl
->membase
+ PIO_POW_MOD_SEL_REG
);
663 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
670 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
672 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
674 return pctl
->nfunctions
;
677 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
680 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
682 return pctl
->functions
[function
].name
;
685 static int sunxi_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
687 const char * const **groups
,
688 unsigned * const num_groups
)
690 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
692 *groups
= pctl
->functions
[function
].groups
;
693 *num_groups
= pctl
->functions
[function
].ngroups
;
698 static void sunxi_pmx_set(struct pinctrl_dev
*pctldev
,
702 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
706 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
708 pin
-= pctl
->desc
->pin_base
;
709 val
= readl(pctl
->membase
+ sunxi_mux_reg(pin
));
710 mask
= MUX_PINS_MASK
<< sunxi_mux_offset(pin
);
711 writel((val
& ~mask
) | config
<< sunxi_mux_offset(pin
),
712 pctl
->membase
+ sunxi_mux_reg(pin
));
714 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
717 static int sunxi_pmx_set_mux(struct pinctrl_dev
*pctldev
,
721 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
722 struct sunxi_pinctrl_group
*g
= pctl
->groups
+ group
;
723 struct sunxi_pinctrl_function
*func
= pctl
->functions
+ function
;
724 struct sunxi_desc_function
*desc
=
725 sunxi_pinctrl_desc_find_function_by_name(pctl
,
732 sunxi_pmx_set(pctldev
, g
->pin
, desc
->muxval
);
738 sunxi_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
739 struct pinctrl_gpio_range
*range
,
743 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
744 struct sunxi_desc_function
*desc
;
752 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, offset
, func
);
756 sunxi_pmx_set(pctldev
, offset
, desc
->muxval
);
761 static int sunxi_pmx_request(struct pinctrl_dev
*pctldev
, unsigned offset
)
763 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
764 unsigned short bank
= offset
/ PINS_PER_BANK
;
765 unsigned short bank_offset
= bank
- pctl
->desc
->pin_base
/
767 struct sunxi_pinctrl_regulator
*s_reg
= &pctl
->regulators
[bank_offset
];
768 struct regulator
*reg
= s_reg
->regulator
;
773 refcount_inc(&s_reg
->refcount
);
777 snprintf(supply
, sizeof(supply
), "vcc-p%c", 'a' + bank
);
778 reg
= regulator_get(pctl
->dev
, supply
);
780 dev_err(pctl
->dev
, "Couldn't get bank P%c regulator\n",
785 ret
= regulator_enable(reg
);
788 "Couldn't enable bank P%c regulator\n", 'A' + bank
);
792 sunxi_pinctrl_set_io_bias_cfg(pctl
, offset
, reg
);
794 s_reg
->regulator
= reg
;
795 refcount_set(&s_reg
->refcount
, 1);
800 regulator_put(s_reg
->regulator
);
805 static int sunxi_pmx_free(struct pinctrl_dev
*pctldev
, unsigned offset
)
807 struct sunxi_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
808 unsigned short bank
= offset
/ PINS_PER_BANK
;
809 unsigned short bank_offset
= bank
- pctl
->desc
->pin_base
/
811 struct sunxi_pinctrl_regulator
*s_reg
= &pctl
->regulators
[bank_offset
];
813 if (!refcount_dec_and_test(&s_reg
->refcount
))
816 regulator_disable(s_reg
->regulator
);
817 regulator_put(s_reg
->regulator
);
818 s_reg
->regulator
= NULL
;
823 static const struct pinmux_ops sunxi_pmx_ops
= {
824 .get_functions_count
= sunxi_pmx_get_funcs_cnt
,
825 .get_function_name
= sunxi_pmx_get_func_name
,
826 .get_function_groups
= sunxi_pmx_get_func_groups
,
827 .set_mux
= sunxi_pmx_set_mux
,
828 .gpio_set_direction
= sunxi_pmx_gpio_set_direction
,
829 .request
= sunxi_pmx_request
,
830 .free
= sunxi_pmx_free
,
834 static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip
*chip
,
837 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
840 static int sunxi_pinctrl_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
842 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
843 u32 reg
= sunxi_data_reg(offset
);
844 u8 index
= sunxi_data_offset(offset
);
845 bool set_mux
= pctl
->desc
->irq_read_needs_mux
&&
846 gpiochip_line_is_irq(chip
, offset
);
847 u32 pin
= offset
+ chip
->base
;
851 sunxi_pmx_set(pctl
->pctl_dev
, pin
, SUN4I_FUNC_INPUT
);
853 val
= (readl(pctl
->membase
+ reg
) >> index
) & DATA_PINS_MASK
;
856 sunxi_pmx_set(pctl
->pctl_dev
, pin
, SUN4I_FUNC_IRQ
);
861 static void sunxi_pinctrl_gpio_set(struct gpio_chip
*chip
,
862 unsigned offset
, int value
)
864 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
865 u32 reg
= sunxi_data_reg(offset
);
866 u8 index
= sunxi_data_offset(offset
);
870 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
872 regval
= readl(pctl
->membase
+ reg
);
875 regval
|= BIT(index
);
877 regval
&= ~(BIT(index
));
879 writel(regval
, pctl
->membase
+ reg
);
881 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
884 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip
*chip
,
885 unsigned offset
, int value
)
887 sunxi_pinctrl_gpio_set(chip
, offset
, value
);
888 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
891 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip
*gc
,
892 const struct of_phandle_args
*gpiospec
,
897 base
= PINS_PER_BANK
* gpiospec
->args
[0];
898 pin
= base
+ gpiospec
->args
[1];
904 *flags
= gpiospec
->args
[2];
909 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
911 struct sunxi_pinctrl
*pctl
= gpiochip_get_data(chip
);
912 struct sunxi_desc_function
*desc
;
913 unsigned pinnum
= pctl
->desc
->pin_base
+ offset
;
916 if (offset
>= chip
->ngpio
)
919 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, pinnum
, "irq");
923 irqnum
= desc
->irqbank
* IRQ_PER_BANK
+ desc
->irqnum
;
925 dev_dbg(chip
->parent
, "%s: request IRQ for GPIO %d, return %d\n",
926 chip
->label
, offset
+ chip
->base
, irqnum
);
928 return irq_find_mapping(pctl
->domain
, irqnum
);
931 static int sunxi_pinctrl_irq_request_resources(struct irq_data
*d
)
933 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
934 struct sunxi_desc_function
*func
;
937 func
= sunxi_pinctrl_desc_find_function_by_pin(pctl
,
938 pctl
->irq_array
[d
->hwirq
], "irq");
942 ret
= gpiochip_lock_as_irq(pctl
->chip
,
943 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
945 dev_err(pctl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
950 /* Change muxing to INT mode */
951 sunxi_pmx_set(pctl
->pctl_dev
, pctl
->irq_array
[d
->hwirq
], func
->muxval
);
956 static void sunxi_pinctrl_irq_release_resources(struct irq_data
*d
)
958 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
960 gpiochip_unlock_as_irq(pctl
->chip
,
961 pctl
->irq_array
[d
->hwirq
] - pctl
->desc
->pin_base
);
964 static int sunxi_pinctrl_irq_set_type(struct irq_data
*d
, unsigned int type
)
966 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
967 u32 reg
= sunxi_irq_cfg_reg(pctl
->desc
, d
->hwirq
);
968 u8 index
= sunxi_irq_cfg_offset(d
->hwirq
);
974 case IRQ_TYPE_EDGE_RISING
:
975 mode
= IRQ_EDGE_RISING
;
977 case IRQ_TYPE_EDGE_FALLING
:
978 mode
= IRQ_EDGE_FALLING
;
980 case IRQ_TYPE_EDGE_BOTH
:
981 mode
= IRQ_EDGE_BOTH
;
983 case IRQ_TYPE_LEVEL_HIGH
:
984 mode
= IRQ_LEVEL_HIGH
;
986 case IRQ_TYPE_LEVEL_LOW
:
987 mode
= IRQ_LEVEL_LOW
;
993 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
995 if (type
& IRQ_TYPE_LEVEL_MASK
)
996 irq_set_chip_handler_name_locked(d
, &sunxi_pinctrl_level_irq_chip
,
997 handle_fasteoi_irq
, NULL
);
999 irq_set_chip_handler_name_locked(d
, &sunxi_pinctrl_edge_irq_chip
,
1000 handle_edge_irq
, NULL
);
1002 regval
= readl(pctl
->membase
+ reg
);
1003 regval
&= ~(IRQ_CFG_IRQ_MASK
<< index
);
1004 writel(regval
| (mode
<< index
), pctl
->membase
+ reg
);
1006 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
1011 static void sunxi_pinctrl_irq_ack(struct irq_data
*d
)
1013 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
1014 u32 status_reg
= sunxi_irq_status_reg(pctl
->desc
, d
->hwirq
);
1015 u8 status_idx
= sunxi_irq_status_offset(d
->hwirq
);
1018 writel(1 << status_idx
, pctl
->membase
+ status_reg
);
1021 static void sunxi_pinctrl_irq_mask(struct irq_data
*d
)
1023 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
1024 u32 reg
= sunxi_irq_ctrl_reg(pctl
->desc
, d
->hwirq
);
1025 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
1026 unsigned long flags
;
1029 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
1032 val
= readl(pctl
->membase
+ reg
);
1033 writel(val
& ~(1 << idx
), pctl
->membase
+ reg
);
1035 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
1038 static void sunxi_pinctrl_irq_unmask(struct irq_data
*d
)
1040 struct sunxi_pinctrl
*pctl
= irq_data_get_irq_chip_data(d
);
1041 u32 reg
= sunxi_irq_ctrl_reg(pctl
->desc
, d
->hwirq
);
1042 u8 idx
= sunxi_irq_ctrl_offset(d
->hwirq
);
1043 unsigned long flags
;
1046 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
1048 /* Unmask the IRQ */
1049 val
= readl(pctl
->membase
+ reg
);
1050 writel(val
| (1 << idx
), pctl
->membase
+ reg
);
1052 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
1055 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data
*d
)
1057 sunxi_pinctrl_irq_ack(d
);
1058 sunxi_pinctrl_irq_unmask(d
);
1061 static struct irq_chip sunxi_pinctrl_edge_irq_chip
= {
1062 .name
= "sunxi_pio_edge",
1063 .irq_ack
= sunxi_pinctrl_irq_ack
,
1064 .irq_mask
= sunxi_pinctrl_irq_mask
,
1065 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
1066 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
1067 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
1068 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
1069 .flags
= IRQCHIP_SKIP_SET_WAKE
,
1072 static struct irq_chip sunxi_pinctrl_level_irq_chip
= {
1073 .name
= "sunxi_pio_level",
1074 .irq_eoi
= sunxi_pinctrl_irq_ack
,
1075 .irq_mask
= sunxi_pinctrl_irq_mask
,
1076 .irq_unmask
= sunxi_pinctrl_irq_unmask
,
1077 /* Define irq_enable / disable to avoid spurious irqs for drivers
1078 * using these to suppress irqs while they clear the irq source */
1079 .irq_enable
= sunxi_pinctrl_irq_ack_unmask
,
1080 .irq_disable
= sunxi_pinctrl_irq_mask
,
1081 .irq_request_resources
= sunxi_pinctrl_irq_request_resources
,
1082 .irq_release_resources
= sunxi_pinctrl_irq_release_resources
,
1083 .irq_set_type
= sunxi_pinctrl_irq_set_type
,
1084 .flags
= IRQCHIP_SKIP_SET_WAKE
| IRQCHIP_EOI_THREADED
|
1085 IRQCHIP_EOI_IF_HANDLED
,
1088 static int sunxi_pinctrl_irq_of_xlate(struct irq_domain
*d
,
1089 struct device_node
*node
,
1091 unsigned int intsize
,
1092 unsigned long *out_hwirq
,
1093 unsigned int *out_type
)
1095 struct sunxi_pinctrl
*pctl
= d
->host_data
;
1096 struct sunxi_desc_function
*desc
;
1102 base
= PINS_PER_BANK
* intspec
[0];
1103 pin
= pctl
->desc
->pin_base
+ base
+ intspec
[1];
1105 desc
= sunxi_pinctrl_desc_find_function_by_pin(pctl
, pin
, "irq");
1109 *out_hwirq
= desc
->irqbank
* PINS_PER_BANK
+ desc
->irqnum
;
1110 *out_type
= intspec
[2];
1115 static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops
= {
1116 .xlate
= sunxi_pinctrl_irq_of_xlate
,
1119 static void sunxi_pinctrl_irq_handler(struct irq_desc
*desc
)
1121 unsigned int irq
= irq_desc_get_irq(desc
);
1122 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
1123 struct sunxi_pinctrl
*pctl
= irq_desc_get_handler_data(desc
);
1124 unsigned long bank
, reg
, val
;
1126 for (bank
= 0; bank
< pctl
->desc
->irq_banks
; bank
++)
1127 if (irq
== pctl
->irq
[bank
])
1130 if (bank
== pctl
->desc
->irq_banks
)
1133 reg
= sunxi_irq_status_reg_from_bank(pctl
->desc
, bank
);
1134 val
= readl(pctl
->membase
+ reg
);
1139 chained_irq_enter(chip
, desc
);
1140 for_each_set_bit(irqoffset
, &val
, IRQ_PER_BANK
) {
1141 int pin_irq
= irq_find_mapping(pctl
->domain
,
1142 bank
* IRQ_PER_BANK
+ irqoffset
);
1143 generic_handle_irq(pin_irq
);
1145 chained_irq_exit(chip
, desc
);
1149 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl
*pctl
,
1152 struct sunxi_pinctrl_function
*func
= pctl
->functions
;
1154 while (func
->name
) {
1155 /* function already there */
1156 if (strcmp(func
->name
, name
) == 0) {
1171 static int sunxi_pinctrl_build_state(struct platform_device
*pdev
)
1173 struct sunxi_pinctrl
*pctl
= platform_get_drvdata(pdev
);
1180 * We assume that the number of groups is the number of pins
1181 * given in the data array.
1183 * This will not always be true, since some pins might not be
1184 * available in the current variant, but fortunately for us,
1185 * this means that the number of pins is the maximum group
1186 * number we will ever see.
1188 pctl
->groups
= devm_kcalloc(&pdev
->dev
,
1189 pctl
->desc
->npins
, sizeof(*pctl
->groups
),
1194 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
1195 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
1196 struct sunxi_pinctrl_group
*group
= pctl
->groups
+ pctl
->ngroups
;
1198 if (pin
->variant
&& !(pctl
->variant
& pin
->variant
))
1201 group
->name
= pin
->pin
.name
;
1202 group
->pin
= pin
->pin
.number
;
1204 /* And now we count the actual number of pins / groups */
1209 * We suppose that we won't have any more functions than pins,
1210 * we'll reallocate that later anyway
1212 pctl
->functions
= kcalloc(pctl
->ngroups
,
1213 sizeof(*pctl
->functions
),
1215 if (!pctl
->functions
)
1218 /* Count functions and their associated groups */
1219 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
1220 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
1221 struct sunxi_desc_function
*func
;
1223 if (pin
->variant
&& !(pctl
->variant
& pin
->variant
))
1226 for (func
= pin
->functions
; func
->name
; func
++) {
1227 if (func
->variant
&& !(pctl
->variant
& func
->variant
))
1230 /* Create interrupt mapping while we're at it */
1231 if (!strcmp(func
->name
, "irq")) {
1232 int irqnum
= func
->irqnum
+ func
->irqbank
* IRQ_PER_BANK
;
1233 pctl
->irq_array
[irqnum
] = pin
->pin
.number
;
1236 sunxi_pinctrl_add_function(pctl
, func
->name
);
1240 /* And now allocated and fill the array for real */
1241 ptr
= krealloc(pctl
->functions
,
1242 pctl
->nfunctions
* sizeof(*pctl
->functions
),
1245 kfree(pctl
->functions
);
1246 pctl
->functions
= NULL
;
1249 pctl
->functions
= ptr
;
1251 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
1252 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
1253 struct sunxi_desc_function
*func
;
1255 if (pin
->variant
&& !(pctl
->variant
& pin
->variant
))
1258 for (func
= pin
->functions
; func
->name
; func
++) {
1259 struct sunxi_pinctrl_function
*func_item
;
1260 const char **func_grp
;
1262 if (func
->variant
&& !(pctl
->variant
& func
->variant
))
1265 func_item
= sunxi_pinctrl_find_function_by_name(pctl
,
1268 kfree(pctl
->functions
);
1272 if (!func_item
->groups
) {
1274 devm_kcalloc(&pdev
->dev
,
1276 sizeof(*func_item
->groups
),
1278 if (!func_item
->groups
) {
1279 kfree(pctl
->functions
);
1284 func_grp
= func_item
->groups
;
1288 *func_grp
= pin
->pin
.name
;
1295 static int sunxi_pinctrl_get_debounce_div(struct clk
*clk
, int freq
, int *diff
)
1297 unsigned long clock
= clk_get_rate(clk
);
1298 unsigned int best_diff
, best_div
;
1301 best_diff
= abs(freq
- clock
);
1304 for (i
= 1; i
< 8; i
++) {
1305 int cur_diff
= abs(freq
- (clock
>> i
));
1307 if (cur_diff
< best_diff
) {
1308 best_diff
= cur_diff
;
1317 static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl
*pctl
,
1318 struct device_node
*node
)
1320 unsigned int hosc_diff
, losc_diff
;
1321 unsigned int hosc_div
, losc_div
;
1322 struct clk
*hosc
, *losc
;
1326 /* Deal with old DTs that didn't have the oscillators */
1327 if (of_clk_get_parent_count(node
) != 3)
1330 /* If we don't have any setup, bail out */
1331 if (!of_find_property(node
, "input-debounce", NULL
))
1334 losc
= devm_clk_get(pctl
->dev
, "losc");
1336 return PTR_ERR(losc
);
1338 hosc
= devm_clk_get(pctl
->dev
, "hosc");
1340 return PTR_ERR(hosc
);
1342 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
1343 unsigned long debounce_freq
;
1346 ret
= of_property_read_u32_index(node
, "input-debounce",
1354 debounce_freq
= DIV_ROUND_CLOSEST(USEC_PER_SEC
, debounce
);
1355 losc_div
= sunxi_pinctrl_get_debounce_div(losc
,
1359 hosc_div
= sunxi_pinctrl_get_debounce_div(hosc
,
1363 if (hosc_diff
< losc_diff
) {
1371 writel(src
| div
<< 4,
1373 sunxi_irq_debounce_reg_from_bank(pctl
->desc
, i
));
1379 int sunxi_pinctrl_init_with_variant(struct platform_device
*pdev
,
1380 const struct sunxi_pinctrl_desc
*desc
,
1381 unsigned long variant
)
1383 struct device_node
*node
= pdev
->dev
.of_node
;
1384 struct pinctrl_desc
*pctrl_desc
;
1385 struct pinctrl_pin_desc
*pins
;
1386 struct sunxi_pinctrl
*pctl
;
1387 struct pinmux_ops
*pmxops
;
1388 int i
, ret
, last_pin
, pin_idx
;
1391 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
1394 platform_set_drvdata(pdev
, pctl
);
1396 raw_spin_lock_init(&pctl
->lock
);
1398 pctl
->membase
= devm_platform_ioremap_resource(pdev
, 0);
1399 if (IS_ERR(pctl
->membase
))
1400 return PTR_ERR(pctl
->membase
);
1402 pctl
->dev
= &pdev
->dev
;
1404 pctl
->variant
= variant
;
1406 pctl
->irq_array
= devm_kcalloc(&pdev
->dev
,
1407 IRQ_PER_BANK
* pctl
->desc
->irq_banks
,
1408 sizeof(*pctl
->irq_array
),
1410 if (!pctl
->irq_array
)
1413 ret
= sunxi_pinctrl_build_state(pdev
);
1415 dev_err(&pdev
->dev
, "dt probe failed: %d\n", ret
);
1419 pins
= devm_kcalloc(&pdev
->dev
,
1420 pctl
->desc
->npins
, sizeof(*pins
),
1425 for (i
= 0, pin_idx
= 0; i
< pctl
->desc
->npins
; i
++) {
1426 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
1428 if (pin
->variant
&& !(pctl
->variant
& pin
->variant
))
1431 pins
[pin_idx
++] = pin
->pin
;
1434 pctrl_desc
= devm_kzalloc(&pdev
->dev
,
1435 sizeof(*pctrl_desc
),
1440 pctrl_desc
->name
= dev_name(&pdev
->dev
);
1441 pctrl_desc
->owner
= THIS_MODULE
;
1442 pctrl_desc
->pins
= pins
;
1443 pctrl_desc
->npins
= pctl
->ngroups
;
1444 pctrl_desc
->confops
= &sunxi_pconf_ops
;
1445 pctrl_desc
->pctlops
= &sunxi_pctrl_ops
;
1447 pmxops
= devm_kmemdup(&pdev
->dev
, &sunxi_pmx_ops
, sizeof(sunxi_pmx_ops
),
1452 if (desc
->disable_strict_mode
)
1453 pmxops
->strict
= false;
1455 pctrl_desc
->pmxops
= pmxops
;
1457 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, pctrl_desc
, pctl
);
1458 if (IS_ERR(pctl
->pctl_dev
)) {
1459 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
1460 return PTR_ERR(pctl
->pctl_dev
);
1463 pctl
->chip
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
->chip
), GFP_KERNEL
);
1467 last_pin
= pctl
->desc
->pins
[pctl
->desc
->npins
- 1].pin
.number
;
1468 pctl
->chip
->owner
= THIS_MODULE
;
1469 pctl
->chip
->request
= gpiochip_generic_request
;
1470 pctl
->chip
->free
= gpiochip_generic_free
;
1471 pctl
->chip
->set_config
= gpiochip_generic_config
;
1472 pctl
->chip
->direction_input
= sunxi_pinctrl_gpio_direction_input
;
1473 pctl
->chip
->direction_output
= sunxi_pinctrl_gpio_direction_output
;
1474 pctl
->chip
->get
= sunxi_pinctrl_gpio_get
;
1475 pctl
->chip
->set
= sunxi_pinctrl_gpio_set
;
1476 pctl
->chip
->of_xlate
= sunxi_pinctrl_gpio_of_xlate
;
1477 pctl
->chip
->to_irq
= sunxi_pinctrl_gpio_to_irq
;
1478 pctl
->chip
->of_gpio_n_cells
= 3;
1479 pctl
->chip
->can_sleep
= false;
1480 pctl
->chip
->ngpio
= round_up(last_pin
, PINS_PER_BANK
) -
1481 pctl
->desc
->pin_base
;
1482 pctl
->chip
->label
= dev_name(&pdev
->dev
);
1483 pctl
->chip
->parent
= &pdev
->dev
;
1484 pctl
->chip
->base
= pctl
->desc
->pin_base
;
1486 ret
= gpiochip_add_data(pctl
->chip
, pctl
);
1490 for (i
= 0; i
< pctl
->desc
->npins
; i
++) {
1491 const struct sunxi_desc_pin
*pin
= pctl
->desc
->pins
+ i
;
1493 ret
= gpiochip_add_pin_range(pctl
->chip
, dev_name(&pdev
->dev
),
1494 pin
->pin
.number
- pctl
->desc
->pin_base
,
1495 pin
->pin
.number
, 1);
1497 goto gpiochip_error
;
1500 ret
= of_clk_get_parent_count(node
);
1501 clk
= devm_clk_get(&pdev
->dev
, ret
== 1 ? NULL
: "apb");
1504 goto gpiochip_error
;
1507 ret
= clk_prepare_enable(clk
);
1509 goto gpiochip_error
;
1511 pctl
->irq
= devm_kcalloc(&pdev
->dev
,
1512 pctl
->desc
->irq_banks
,
1520 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
1521 pctl
->irq
[i
] = platform_get_irq(pdev
, i
);
1522 if (pctl
->irq
[i
] < 0) {
1528 pctl
->domain
= irq_domain_add_linear(node
,
1529 pctl
->desc
->irq_banks
* IRQ_PER_BANK
,
1530 &sunxi_pinctrl_irq_domain_ops
,
1532 if (!pctl
->domain
) {
1533 dev_err(&pdev
->dev
, "Couldn't register IRQ domain\n");
1538 for (i
= 0; i
< (pctl
->desc
->irq_banks
* IRQ_PER_BANK
); i
++) {
1539 int irqno
= irq_create_mapping(pctl
->domain
, i
);
1541 irq_set_chip_and_handler(irqno
, &sunxi_pinctrl_edge_irq_chip
,
1543 irq_set_chip_data(irqno
, pctl
);
1546 for (i
= 0; i
< pctl
->desc
->irq_banks
; i
++) {
1547 /* Mask and clear all IRQs before registering a handler */
1548 writel(0, pctl
->membase
+
1549 sunxi_irq_ctrl_reg_from_bank(pctl
->desc
, i
));
1552 sunxi_irq_status_reg_from_bank(pctl
->desc
, i
));
1554 irq_set_chained_handler_and_data(pctl
->irq
[i
],
1555 sunxi_pinctrl_irq_handler
,
1559 sunxi_pinctrl_setup_debounce(pctl
, node
);
1561 dev_info(&pdev
->dev
, "initialized sunXi PIO driver\n");
1566 clk_disable_unprepare(clk
);
1568 gpiochip_remove(pctl
->chip
);