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/interrupt.h>
17 #include <linux/irqdomain.h>
18 #include <linux/irqchip/chained_irq.h>
19 #include <linux/export.h>
21 #include <linux/of_clk.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_irq.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
34 #include <dt-bindings/pinctrl/sun4i-a10.h>
37 #include "pinctrl-sunxi.h"
39 static struct irq_chip sunxi_pinctrl_edge_irq_chip;
40 static struct irq_chip sunxi_pinctrl_level_irq_chip;
42 static struct sunxi_pinctrl_group *
43 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
47 for (i = 0; i < pctl->ngroups; i++) {
48 struct sunxi_pinctrl_group *grp = pctl->groups + i;
50 if (!strcmp(grp->name, group))
57 static struct sunxi_pinctrl_function *
58 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
61 struct sunxi_pinctrl_function *func = pctl->functions;
64 for (i = 0; i < pctl->nfunctions; i++) {
68 if (!strcmp(func[i].name, name))
75 static struct sunxi_desc_function *
76 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
78 const char *func_name)
82 for (i = 0; i < pctl->desc->npins; i++) {
83 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
85 if (!strcmp(pin->pin.name, pin_name)) {
86 struct sunxi_desc_function *func = pin->functions;
89 if (!strcmp(func->name, func_name) &&
91 func->variant & pctl->variant))
102 static struct sunxi_desc_function *
103 sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
105 const char *func_name)
109 for (i = 0; i < pctl->desc->npins; i++) {
110 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
112 if (pin->pin.number == pin_num) {
113 struct sunxi_desc_function *func = pin->functions;
116 if (!strcmp(func->name, func_name))
127 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
129 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
131 return pctl->ngroups;
134 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
137 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
139 return pctl->groups[group].name;
142 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
144 const unsigned **pins,
147 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
149 *pins = (unsigned *)&pctl->groups[group].pin;
155 static bool sunxi_pctrl_has_bias_prop(struct device_node *node)
157 return of_find_property(node, "bias-pull-up", NULL) ||
158 of_find_property(node, "bias-pull-down", NULL) ||
159 of_find_property(node, "bias-disable", NULL) ||
160 of_find_property(node, "allwinner,pull", NULL);
163 static bool sunxi_pctrl_has_drive_prop(struct device_node *node)
165 return of_find_property(node, "drive-strength", NULL) ||
166 of_find_property(node, "allwinner,drive", NULL);
169 static int sunxi_pctrl_parse_bias_prop(struct device_node *node)
173 /* Try the new style binding */
174 if (of_find_property(node, "bias-pull-up", NULL))
175 return PIN_CONFIG_BIAS_PULL_UP;
177 if (of_find_property(node, "bias-pull-down", NULL))
178 return PIN_CONFIG_BIAS_PULL_DOWN;
180 if (of_find_property(node, "bias-disable", NULL))
181 return PIN_CONFIG_BIAS_DISABLE;
183 /* And fall back to the old binding */
184 if (of_property_read_u32(node, "allwinner,pull", &val))
188 case SUN4I_PINCTRL_NO_PULL:
189 return PIN_CONFIG_BIAS_DISABLE;
190 case SUN4I_PINCTRL_PULL_UP:
191 return PIN_CONFIG_BIAS_PULL_UP;
192 case SUN4I_PINCTRL_PULL_DOWN:
193 return PIN_CONFIG_BIAS_PULL_DOWN;
199 static int sunxi_pctrl_parse_drive_prop(struct device_node *node)
203 /* Try the new style binding */
204 if (!of_property_read_u32(node, "drive-strength", &val)) {
205 /* We can't go below 10mA ... */
209 /* ... and only up to 40 mA ... */
213 /* by steps of 10 mA */
214 return rounddown(val, 10);
217 /* And then fall back to the old binding */
218 if (of_property_read_u32(node, "allwinner,drive", &val))
221 return (val + 1) * 10;
224 static const char *sunxi_pctrl_parse_function_prop(struct device_node *node)
226 const char *function;
229 /* Try the generic binding */
230 ret = of_property_read_string(node, "function", &function);
234 /* And fall back to our legacy one */
235 ret = of_property_read_string(node, "allwinner,function", &function);
242 static const char *sunxi_pctrl_find_pins_prop(struct device_node *node,
247 /* Try the generic binding */
248 count = of_property_count_strings(node, "pins");
254 /* And fall back to our legacy one */
255 count = of_property_count_strings(node, "allwinner,pins");
258 return "allwinner,pins";
264 static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node,
267 unsigned long *pinconfig;
268 unsigned int configlen = 0, idx = 0;
271 if (sunxi_pctrl_has_drive_prop(node))
273 if (sunxi_pctrl_has_bias_prop(node))
277 * If we don't have any configuration, bail out
282 pinconfig = kcalloc(configlen, sizeof(*pinconfig), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 if (sunxi_pctrl_has_drive_prop(node)) {
287 int drive = sunxi_pctrl_parse_drive_prop(node);
293 pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
297 if (sunxi_pctrl_has_bias_prop(node)) {
298 int pull = sunxi_pctrl_parse_bias_prop(node);
305 if (pull != PIN_CONFIG_BIAS_DISABLE)
306 arg = 1; /* hardware uses weak pull resistors */
308 pinconfig[idx++] = pinconf_to_config_packed(pull, arg);
320 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
321 struct device_node *node,
322 struct pinctrl_map **map,
325 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
326 unsigned long *pinconfig;
327 struct property *prop;
328 const char *function, *pin_prop;
330 int ret, npins, nmaps, configlen = 0, i = 0;
335 function = sunxi_pctrl_parse_function_prop(node);
337 dev_err(pctl->dev, "missing function property in node %pOFn\n",
342 pin_prop = sunxi_pctrl_find_pins_prop(node, &npins);
344 dev_err(pctl->dev, "missing pins property in node %pOFn\n",
350 * We have two maps for each pin: one for the function, one
351 * for the configuration (bias, strength, etc).
353 * We might be slightly overshooting, since we might not have
357 *map = kmalloc_array(nmaps, sizeof(struct pinctrl_map), GFP_KERNEL);
361 pinconfig = sunxi_pctrl_build_pin_config(node, &configlen);
362 if (IS_ERR(pinconfig)) {
363 ret = PTR_ERR(pinconfig);
367 of_property_for_each_string(node, pin_prop, prop, group) {
368 struct sunxi_pinctrl_group *grp =
369 sunxi_pinctrl_find_group_by_name(pctl, group);
372 dev_err(pctl->dev, "unknown pin %s", group);
376 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
379 dev_err(pctl->dev, "unsupported function %s on pin %s",
384 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
385 (*map)[i].data.mux.group = group;
386 (*map)[i].data.mux.function = function;
391 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
392 (*map)[i].data.configs.group_or_pin = group;
393 (*map)[i].data.configs.configs = pinconfig;
394 (*map)[i].data.configs.num_configs = configlen;
402 * We know have the number of maps we need, we can resize our
405 *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
417 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
418 struct pinctrl_map *map,
423 /* pin config is never in the first map */
424 for (i = 1; i < num_maps; i++) {
425 if (map[i].type != PIN_MAP_TYPE_CONFIGS_GROUP)
429 * All the maps share the same pin config,
430 * free only the first one we find.
432 kfree(map[i].data.configs.configs);
439 static const struct pinctrl_ops sunxi_pctrl_ops = {
440 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
441 .dt_free_map = sunxi_pctrl_dt_free_map,
442 .get_groups_count = sunxi_pctrl_get_groups_count,
443 .get_group_name = sunxi_pctrl_get_group_name,
444 .get_group_pins = sunxi_pctrl_get_group_pins,
447 static int sunxi_pconf_reg(unsigned pin, enum pin_config_param param,
448 u32 *offset, u32 *shift, u32 *mask)
451 case PIN_CONFIG_DRIVE_STRENGTH:
452 *offset = sunxi_dlevel_reg(pin);
453 *shift = sunxi_dlevel_offset(pin);
454 *mask = DLEVEL_PINS_MASK;
457 case PIN_CONFIG_BIAS_PULL_UP:
458 case PIN_CONFIG_BIAS_PULL_DOWN:
459 case PIN_CONFIG_BIAS_DISABLE:
460 *offset = sunxi_pull_reg(pin);
461 *shift = sunxi_pull_offset(pin);
462 *mask = PULL_PINS_MASK;
472 static int sunxi_pconf_get(struct pinctrl_dev *pctldev, unsigned pin,
473 unsigned long *config)
475 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
476 enum pin_config_param param = pinconf_to_config_param(*config);
477 u32 offset, shift, mask, val;
481 pin -= pctl->desc->pin_base;
483 ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
487 val = (readl(pctl->membase + offset) >> shift) & mask;
489 switch (pinconf_to_config_param(*config)) {
490 case PIN_CONFIG_DRIVE_STRENGTH:
491 arg = (val + 1) * 10;
494 case PIN_CONFIG_BIAS_PULL_UP:
495 if (val != SUN4I_PINCTRL_PULL_UP)
497 arg = 1; /* hardware is weak pull-up */
500 case PIN_CONFIG_BIAS_PULL_DOWN:
501 if (val != SUN4I_PINCTRL_PULL_DOWN)
503 arg = 1; /* hardware is weak pull-down */
506 case PIN_CONFIG_BIAS_DISABLE:
507 if (val != SUN4I_PINCTRL_NO_PULL)
513 /* sunxi_pconf_reg should catch anything unsupported */
518 *config = pinconf_to_config_packed(param, arg);
523 static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
525 unsigned long *config)
527 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
528 struct sunxi_pinctrl_group *g = &pctl->groups[group];
530 /* We only support 1 pin per group. Chain it to the pin callback */
531 return sunxi_pconf_get(pctldev, g->pin, config);
534 static int sunxi_pconf_set(struct pinctrl_dev *pctldev, unsigned pin,
535 unsigned long *configs, unsigned num_configs)
537 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
540 for (i = 0; i < num_configs; i++) {
541 enum pin_config_param param;
543 u32 offset, shift, mask, reg;
547 param = pinconf_to_config_param(configs[i]);
548 arg = pinconf_to_config_argument(configs[i]);
550 ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
555 case PIN_CONFIG_DRIVE_STRENGTH:
556 if (arg < 10 || arg > 40)
559 * We convert from mA to what the register expects:
567 case PIN_CONFIG_BIAS_DISABLE:
570 case PIN_CONFIG_BIAS_PULL_UP:
575 case PIN_CONFIG_BIAS_PULL_DOWN:
581 /* sunxi_pconf_reg should catch anything unsupported */
586 raw_spin_lock_irqsave(&pctl->lock, flags);
587 reg = readl(pctl->membase + offset);
588 reg &= ~(mask << shift);
589 writel(reg | val << shift, pctl->membase + offset);
590 raw_spin_unlock_irqrestore(&pctl->lock, flags);
591 } /* for each config */
596 static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
597 unsigned long *configs, unsigned num_configs)
599 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
600 struct sunxi_pinctrl_group *g = &pctl->groups[group];
602 /* We only support 1 pin per group. Chain it to the pin callback */
603 return sunxi_pconf_set(pctldev, g->pin, configs, num_configs);
606 static const struct pinconf_ops sunxi_pconf_ops = {
608 .pin_config_get = sunxi_pconf_get,
609 .pin_config_set = sunxi_pconf_set,
610 .pin_config_group_get = sunxi_pconf_group_get,
611 .pin_config_group_set = sunxi_pconf_group_set,
614 static int sunxi_pinctrl_set_io_bias_cfg(struct sunxi_pinctrl *pctl,
616 struct regulator *supply)
618 unsigned short bank = pin / PINS_PER_BANK;
623 if (!pctl->desc->io_bias_cfg_variant)
626 uV = regulator_get_voltage(supply);
630 /* Might be dummy regulator with no voltage set */
634 switch (pctl->desc->io_bias_cfg_variant) {
635 case BIAS_VOLTAGE_GRP_CONFIG:
637 * Configured value must be equal or greater to actual
641 val = 0x0; /* 1.8V */
642 else if (uV <= 2500000)
643 val = 0x6; /* 2.5V */
644 else if (uV <= 2800000)
645 val = 0x9; /* 2.8V */
646 else if (uV <= 3000000)
647 val = 0xA; /* 3.0V */
649 val = 0xD; /* 3.3V */
651 pin -= pctl->desc->pin_base;
653 reg = readl(pctl->membase + sunxi_grp_config_reg(pin));
654 reg &= ~IO_BIAS_MASK;
655 writel(reg | val, pctl->membase + sunxi_grp_config_reg(pin));
657 case BIAS_VOLTAGE_PIO_POW_MODE_SEL:
658 val = uV <= 1800000 ? 1 : 0;
660 raw_spin_lock_irqsave(&pctl->lock, flags);
661 reg = readl(pctl->membase + PIO_POW_MOD_SEL_REG);
663 writel(reg | val << bank, pctl->membase + PIO_POW_MOD_SEL_REG);
664 raw_spin_unlock_irqrestore(&pctl->lock, flags);
671 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
673 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
675 return pctl->nfunctions;
678 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
681 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
683 return pctl->functions[function].name;
686 static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
688 const char * const **groups,
689 unsigned * const num_groups)
691 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
693 *groups = pctl->functions[function].groups;
694 *num_groups = pctl->functions[function].ngroups;
699 static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
703 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
707 raw_spin_lock_irqsave(&pctl->lock, flags);
709 pin -= pctl->desc->pin_base;
710 val = readl(pctl->membase + sunxi_mux_reg(pin));
711 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
712 writel((val & ~mask) | config << sunxi_mux_offset(pin),
713 pctl->membase + sunxi_mux_reg(pin));
715 raw_spin_unlock_irqrestore(&pctl->lock, flags);
718 static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
722 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
723 struct sunxi_pinctrl_group *g = pctl->groups + group;
724 struct sunxi_pinctrl_function *func = pctl->functions + function;
725 struct sunxi_desc_function *desc =
726 sunxi_pinctrl_desc_find_function_by_name(pctl,
733 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
739 sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
740 struct pinctrl_gpio_range *range,
744 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
745 struct sunxi_desc_function *desc;
753 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
757 sunxi_pmx_set(pctldev, offset, desc->muxval);
762 static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
764 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
765 unsigned short bank = offset / PINS_PER_BANK;
766 unsigned short bank_offset = bank - pctl->desc->pin_base /
768 struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
769 struct regulator *reg = s_reg->regulator;
774 refcount_inc(&s_reg->refcount);
778 snprintf(supply, sizeof(supply), "vcc-p%c", 'a' + bank);
779 reg = regulator_get(pctl->dev, supply);
781 dev_err(pctl->dev, "Couldn't get bank P%c regulator\n",
786 ret = regulator_enable(reg);
789 "Couldn't enable bank P%c regulator\n", 'A' + bank);
793 sunxi_pinctrl_set_io_bias_cfg(pctl, offset, reg);
795 s_reg->regulator = reg;
796 refcount_set(&s_reg->refcount, 1);
801 regulator_put(s_reg->regulator);
806 static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
808 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
809 unsigned short bank = offset / PINS_PER_BANK;
810 unsigned short bank_offset = bank - pctl->desc->pin_base /
812 struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
814 if (!refcount_dec_and_test(&s_reg->refcount))
817 regulator_disable(s_reg->regulator);
818 regulator_put(s_reg->regulator);
819 s_reg->regulator = NULL;
824 static const struct pinmux_ops sunxi_pmx_ops = {
825 .get_functions_count = sunxi_pmx_get_funcs_cnt,
826 .get_function_name = sunxi_pmx_get_func_name,
827 .get_function_groups = sunxi_pmx_get_func_groups,
828 .set_mux = sunxi_pmx_set_mux,
829 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
830 .request = sunxi_pmx_request,
831 .free = sunxi_pmx_free,
835 static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
838 return pinctrl_gpio_direction_input(chip->base + offset);
841 static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
843 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
844 u32 reg = sunxi_data_reg(offset);
845 u8 index = sunxi_data_offset(offset);
846 bool set_mux = pctl->desc->irq_read_needs_mux &&
847 gpiochip_line_is_irq(chip, offset);
848 u32 pin = offset + chip->base;
852 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
854 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
857 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
862 static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
863 unsigned offset, int value)
865 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
866 u32 reg = sunxi_data_reg(offset);
867 u8 index = sunxi_data_offset(offset);
871 raw_spin_lock_irqsave(&pctl->lock, flags);
873 regval = readl(pctl->membase + reg);
876 regval |= BIT(index);
878 regval &= ~(BIT(index));
880 writel(regval, pctl->membase + reg);
882 raw_spin_unlock_irqrestore(&pctl->lock, flags);
885 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
886 unsigned offset, int value)
888 sunxi_pinctrl_gpio_set(chip, offset, value);
889 return pinctrl_gpio_direction_output(chip->base + offset);
892 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
893 const struct of_phandle_args *gpiospec,
898 base = PINS_PER_BANK * gpiospec->args[0];
899 pin = base + gpiospec->args[1];
905 *flags = gpiospec->args[2];
910 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
912 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
913 struct sunxi_desc_function *desc;
914 unsigned pinnum = pctl->desc->pin_base + offset;
917 if (offset >= chip->ngpio)
920 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
924 irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
926 dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
927 chip->label, offset + chip->base, irqnum);
929 return irq_find_mapping(pctl->domain, irqnum);
932 static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
934 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
935 struct sunxi_desc_function *func;
938 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
939 pctl->irq_array[d->hwirq], "irq");
943 ret = gpiochip_lock_as_irq(pctl->chip,
944 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
946 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
951 /* Change muxing to INT mode */
952 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
957 static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
959 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
961 gpiochip_unlock_as_irq(pctl->chip,
962 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
965 static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
967 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
968 u32 reg = sunxi_irq_cfg_reg(pctl->desc, d->hwirq);
969 u8 index = sunxi_irq_cfg_offset(d->hwirq);
975 case IRQ_TYPE_EDGE_RISING:
976 mode = IRQ_EDGE_RISING;
978 case IRQ_TYPE_EDGE_FALLING:
979 mode = IRQ_EDGE_FALLING;
981 case IRQ_TYPE_EDGE_BOTH:
982 mode = IRQ_EDGE_BOTH;
984 case IRQ_TYPE_LEVEL_HIGH:
985 mode = IRQ_LEVEL_HIGH;
987 case IRQ_TYPE_LEVEL_LOW:
988 mode = IRQ_LEVEL_LOW;
994 raw_spin_lock_irqsave(&pctl->lock, flags);
996 if (type & IRQ_TYPE_LEVEL_MASK)
997 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
998 handle_fasteoi_irq, NULL);
1000 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
1001 handle_edge_irq, NULL);
1003 regval = readl(pctl->membase + reg);
1004 regval &= ~(IRQ_CFG_IRQ_MASK << index);
1005 writel(regval | (mode << index), pctl->membase + reg);
1007 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1012 static void sunxi_pinctrl_irq_ack(struct irq_data *d)
1014 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1015 u32 status_reg = sunxi_irq_status_reg(pctl->desc, d->hwirq);
1016 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
1019 writel(1 << status_idx, pctl->membase + status_reg);
1022 static void sunxi_pinctrl_irq_mask(struct irq_data *d)
1024 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1025 u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1026 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1027 unsigned long flags;
1030 raw_spin_lock_irqsave(&pctl->lock, flags);
1033 val = readl(pctl->membase + reg);
1034 writel(val & ~(1 << idx), pctl->membase + reg);
1036 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1039 static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
1041 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1042 u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1043 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1044 unsigned long flags;
1047 raw_spin_lock_irqsave(&pctl->lock, flags);
1049 /* Unmask the IRQ */
1050 val = readl(pctl->membase + reg);
1051 writel(val | (1 << idx), pctl->membase + reg);
1053 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1056 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
1058 sunxi_pinctrl_irq_ack(d);
1059 sunxi_pinctrl_irq_unmask(d);
1062 static int sunxi_pinctrl_irq_set_wake(struct irq_data *d, unsigned int on)
1064 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1065 u8 bank = d->hwirq / IRQ_PER_BANK;
1067 return irq_set_irq_wake(pctl->irq[bank], on);
1070 static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
1071 .name = "sunxi_pio_edge",
1072 .irq_ack = sunxi_pinctrl_irq_ack,
1073 .irq_mask = sunxi_pinctrl_irq_mask,
1074 .irq_unmask = sunxi_pinctrl_irq_unmask,
1075 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
1076 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
1077 .irq_set_type = sunxi_pinctrl_irq_set_type,
1078 .irq_set_wake = sunxi_pinctrl_irq_set_wake,
1079 .flags = IRQCHIP_MASK_ON_SUSPEND,
1082 static struct irq_chip sunxi_pinctrl_level_irq_chip = {
1083 .name = "sunxi_pio_level",
1084 .irq_eoi = sunxi_pinctrl_irq_ack,
1085 .irq_mask = sunxi_pinctrl_irq_mask,
1086 .irq_unmask = sunxi_pinctrl_irq_unmask,
1087 /* Define irq_enable / disable to avoid spurious irqs for drivers
1088 * using these to suppress irqs while they clear the irq source */
1089 .irq_enable = sunxi_pinctrl_irq_ack_unmask,
1090 .irq_disable = sunxi_pinctrl_irq_mask,
1091 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
1092 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
1093 .irq_set_type = sunxi_pinctrl_irq_set_type,
1094 .irq_set_wake = sunxi_pinctrl_irq_set_wake,
1095 .flags = IRQCHIP_EOI_THREADED |
1096 IRQCHIP_MASK_ON_SUSPEND |
1097 IRQCHIP_EOI_IF_HANDLED,
1100 static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
1101 struct device_node *node,
1103 unsigned int intsize,
1104 unsigned long *out_hwirq,
1105 unsigned int *out_type)
1107 struct sunxi_pinctrl *pctl = d->host_data;
1108 struct sunxi_desc_function *desc;
1114 base = PINS_PER_BANK * intspec[0];
1115 pin = pctl->desc->pin_base + base + intspec[1];
1117 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
1121 *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
1122 *out_type = intspec[2];
1127 static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
1128 .xlate = sunxi_pinctrl_irq_of_xlate,
1131 static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
1133 unsigned int irq = irq_desc_get_irq(desc);
1134 struct irq_chip *chip = irq_desc_get_chip(desc);
1135 struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
1136 unsigned long bank, reg, val;
1138 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
1139 if (irq == pctl->irq[bank])
1142 if (bank == pctl->desc->irq_banks)
1145 reg = sunxi_irq_status_reg_from_bank(pctl->desc, bank);
1146 val = readl(pctl->membase + reg);
1151 chained_irq_enter(chip, desc);
1152 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
1153 int pin_irq = irq_find_mapping(pctl->domain,
1154 bank * IRQ_PER_BANK + irqoffset);
1155 generic_handle_irq(pin_irq);
1157 chained_irq_exit(chip, desc);
1161 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
1164 struct sunxi_pinctrl_function *func = pctl->functions;
1166 while (func->name) {
1167 /* function already there */
1168 if (strcmp(func->name, name) == 0) {
1183 static int sunxi_pinctrl_build_state(struct platform_device *pdev)
1185 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
1192 * We assume that the number of groups is the number of pins
1193 * given in the data array.
1195 * This will not always be true, since some pins might not be
1196 * available in the current variant, but fortunately for us,
1197 * this means that the number of pins is the maximum group
1198 * number we will ever see.
1200 pctl->groups = devm_kcalloc(&pdev->dev,
1201 pctl->desc->npins, sizeof(*pctl->groups),
1206 for (i = 0; i < pctl->desc->npins; i++) {
1207 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1208 struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
1210 if (pin->variant && !(pctl->variant & pin->variant))
1213 group->name = pin->pin.name;
1214 group->pin = pin->pin.number;
1216 /* And now we count the actual number of pins / groups */
1221 * We suppose that we won't have any more functions than pins,
1222 * we'll reallocate that later anyway
1224 pctl->functions = kcalloc(pctl->ngroups,
1225 sizeof(*pctl->functions),
1227 if (!pctl->functions)
1230 /* Count functions and their associated groups */
1231 for (i = 0; i < pctl->desc->npins; i++) {
1232 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1233 struct sunxi_desc_function *func;
1235 if (pin->variant && !(pctl->variant & pin->variant))
1238 for (func = pin->functions; func->name; func++) {
1239 if (func->variant && !(pctl->variant & func->variant))
1242 /* Create interrupt mapping while we're at it */
1243 if (!strcmp(func->name, "irq")) {
1244 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
1245 pctl->irq_array[irqnum] = pin->pin.number;
1248 sunxi_pinctrl_add_function(pctl, func->name);
1252 /* And now allocated and fill the array for real */
1253 ptr = krealloc(pctl->functions,
1254 pctl->nfunctions * sizeof(*pctl->functions),
1257 kfree(pctl->functions);
1258 pctl->functions = NULL;
1261 pctl->functions = ptr;
1263 for (i = 0; i < pctl->desc->npins; i++) {
1264 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1265 struct sunxi_desc_function *func;
1267 if (pin->variant && !(pctl->variant & pin->variant))
1270 for (func = pin->functions; func->name; func++) {
1271 struct sunxi_pinctrl_function *func_item;
1272 const char **func_grp;
1274 if (func->variant && !(pctl->variant & func->variant))
1277 func_item = sunxi_pinctrl_find_function_by_name(pctl,
1280 kfree(pctl->functions);
1284 if (!func_item->groups) {
1286 devm_kcalloc(&pdev->dev,
1288 sizeof(*func_item->groups),
1290 if (!func_item->groups) {
1291 kfree(pctl->functions);
1296 func_grp = func_item->groups;
1300 *func_grp = pin->pin.name;
1307 static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
1309 unsigned long clock = clk_get_rate(clk);
1310 unsigned int best_diff, best_div;
1313 best_diff = abs(freq - clock);
1316 for (i = 1; i < 8; i++) {
1317 int cur_diff = abs(freq - (clock >> i));
1319 if (cur_diff < best_diff) {
1320 best_diff = cur_diff;
1329 static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
1330 struct device_node *node)
1332 unsigned int hosc_diff, losc_diff;
1333 unsigned int hosc_div, losc_div;
1334 struct clk *hosc, *losc;
1338 /* Deal with old DTs that didn't have the oscillators */
1339 if (of_clk_get_parent_count(node) != 3)
1342 /* If we don't have any setup, bail out */
1343 if (!of_find_property(node, "input-debounce", NULL))
1346 losc = devm_clk_get(pctl->dev, "losc");
1348 return PTR_ERR(losc);
1350 hosc = devm_clk_get(pctl->dev, "hosc");
1352 return PTR_ERR(hosc);
1354 for (i = 0; i < pctl->desc->irq_banks; i++) {
1355 unsigned long debounce_freq;
1358 ret = of_property_read_u32_index(node, "input-debounce",
1366 debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
1367 losc_div = sunxi_pinctrl_get_debounce_div(losc,
1371 hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
1375 if (hosc_diff < losc_diff) {
1383 writel(src | div << 4,
1385 sunxi_irq_debounce_reg_from_bank(pctl->desc, i));
1391 int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
1392 const struct sunxi_pinctrl_desc *desc,
1393 unsigned long variant)
1395 struct device_node *node = pdev->dev.of_node;
1396 struct pinctrl_desc *pctrl_desc;
1397 struct pinctrl_pin_desc *pins;
1398 struct sunxi_pinctrl *pctl;
1399 struct pinmux_ops *pmxops;
1400 int i, ret, last_pin, pin_idx;
1403 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1406 platform_set_drvdata(pdev, pctl);
1408 raw_spin_lock_init(&pctl->lock);
1410 pctl->membase = devm_platform_ioremap_resource(pdev, 0);
1411 if (IS_ERR(pctl->membase))
1412 return PTR_ERR(pctl->membase);
1414 pctl->dev = &pdev->dev;
1416 pctl->variant = variant;
1418 pctl->irq_array = devm_kcalloc(&pdev->dev,
1419 IRQ_PER_BANK * pctl->desc->irq_banks,
1420 sizeof(*pctl->irq_array),
1422 if (!pctl->irq_array)
1425 ret = sunxi_pinctrl_build_state(pdev);
1427 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
1431 pins = devm_kcalloc(&pdev->dev,
1432 pctl->desc->npins, sizeof(*pins),
1437 for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
1438 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1440 if (pin->variant && !(pctl->variant & pin->variant))
1443 pins[pin_idx++] = pin->pin;
1446 pctrl_desc = devm_kzalloc(&pdev->dev,
1447 sizeof(*pctrl_desc),
1452 pctrl_desc->name = dev_name(&pdev->dev);
1453 pctrl_desc->owner = THIS_MODULE;
1454 pctrl_desc->pins = pins;
1455 pctrl_desc->npins = pctl->ngroups;
1456 pctrl_desc->confops = &sunxi_pconf_ops;
1457 pctrl_desc->pctlops = &sunxi_pctrl_ops;
1459 pmxops = devm_kmemdup(&pdev->dev, &sunxi_pmx_ops, sizeof(sunxi_pmx_ops),
1464 if (desc->disable_strict_mode)
1465 pmxops->strict = false;
1467 pctrl_desc->pmxops = pmxops;
1469 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
1470 if (IS_ERR(pctl->pctl_dev)) {
1471 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1472 return PTR_ERR(pctl->pctl_dev);
1475 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1479 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
1480 pctl->chip->owner = THIS_MODULE;
1481 pctl->chip->request = gpiochip_generic_request;
1482 pctl->chip->free = gpiochip_generic_free;
1483 pctl->chip->set_config = gpiochip_generic_config;
1484 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
1485 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
1486 pctl->chip->get = sunxi_pinctrl_gpio_get;
1487 pctl->chip->set = sunxi_pinctrl_gpio_set;
1488 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
1489 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq;
1490 pctl->chip->of_gpio_n_cells = 3;
1491 pctl->chip->can_sleep = false;
1492 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
1493 pctl->desc->pin_base;
1494 pctl->chip->label = dev_name(&pdev->dev);
1495 pctl->chip->parent = &pdev->dev;
1496 pctl->chip->base = pctl->desc->pin_base;
1498 ret = gpiochip_add_data(pctl->chip, pctl);
1502 for (i = 0; i < pctl->desc->npins; i++) {
1503 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1505 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1506 pin->pin.number - pctl->desc->pin_base,
1507 pin->pin.number, 1);
1509 goto gpiochip_error;
1512 ret = of_clk_get_parent_count(node);
1513 clk = devm_clk_get(&pdev->dev, ret == 1 ? NULL : "apb");
1516 goto gpiochip_error;
1519 ret = clk_prepare_enable(clk);
1521 goto gpiochip_error;
1523 pctl->irq = devm_kcalloc(&pdev->dev,
1524 pctl->desc->irq_banks,
1532 for (i = 0; i < pctl->desc->irq_banks; i++) {
1533 pctl->irq[i] = platform_get_irq(pdev, i);
1534 if (pctl->irq[i] < 0) {
1540 pctl->domain = irq_domain_add_linear(node,
1541 pctl->desc->irq_banks * IRQ_PER_BANK,
1542 &sunxi_pinctrl_irq_domain_ops,
1544 if (!pctl->domain) {
1545 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1550 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
1551 int irqno = irq_create_mapping(pctl->domain, i);
1553 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
1555 irq_set_chip_data(irqno, pctl);
1558 for (i = 0; i < pctl->desc->irq_banks; i++) {
1559 /* Mask and clear all IRQs before registering a handler */
1560 writel(0, pctl->membase +
1561 sunxi_irq_ctrl_reg_from_bank(pctl->desc, i));
1564 sunxi_irq_status_reg_from_bank(pctl->desc, i));
1566 irq_set_chained_handler_and_data(pctl->irq[i],
1567 sunxi_pinctrl_irq_handler,
1571 sunxi_pinctrl_setup_debounce(pctl, node);
1573 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
1578 clk_disable_unprepare(clk);
1580 gpiochip_remove(pctl->chip);