2 * Allwinner A1X SoCs pinctrl driver.
4 * Copyright (C) 2012 Maxime Ripard
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 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
840 return sunxi_pmx_gpio_set_direction(pctl->pctl_dev, NULL, offset, true);
843 static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
845 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
846 u32 reg = sunxi_data_reg(offset);
847 u8 index = sunxi_data_offset(offset);
848 bool set_mux = pctl->desc->irq_read_needs_mux &&
849 gpiochip_line_is_irq(chip, offset);
850 u32 pin = offset + chip->base;
854 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
856 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
859 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
864 static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
865 unsigned offset, int value)
867 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
868 u32 reg = sunxi_data_reg(offset);
869 u8 index = sunxi_data_offset(offset);
873 raw_spin_lock_irqsave(&pctl->lock, flags);
875 regval = readl(pctl->membase + reg);
878 regval |= BIT(index);
880 regval &= ~(BIT(index));
882 writel(regval, pctl->membase + reg);
884 raw_spin_unlock_irqrestore(&pctl->lock, flags);
887 static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
888 unsigned offset, int value)
890 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
892 sunxi_pinctrl_gpio_set(chip, offset, value);
893 return sunxi_pmx_gpio_set_direction(pctl->pctl_dev, NULL, offset, false);
896 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
897 const struct of_phandle_args *gpiospec,
902 base = PINS_PER_BANK * gpiospec->args[0];
903 pin = base + gpiospec->args[1];
909 *flags = gpiospec->args[2];
914 static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
916 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
917 struct sunxi_desc_function *desc;
918 unsigned pinnum = pctl->desc->pin_base + offset;
921 if (offset >= chip->ngpio)
924 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
928 irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
930 dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
931 chip->label, offset + chip->base, irqnum);
933 return irq_find_mapping(pctl->domain, irqnum);
936 static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
938 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
939 struct sunxi_desc_function *func;
942 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
943 pctl->irq_array[d->hwirq], "irq");
947 ret = gpiochip_lock_as_irq(pctl->chip,
948 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
950 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
955 /* Change muxing to INT mode */
956 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
961 static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
963 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
965 gpiochip_unlock_as_irq(pctl->chip,
966 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
969 static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
971 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
972 u32 reg = sunxi_irq_cfg_reg(pctl->desc, d->hwirq);
973 u8 index = sunxi_irq_cfg_offset(d->hwirq);
979 case IRQ_TYPE_EDGE_RISING:
980 mode = IRQ_EDGE_RISING;
982 case IRQ_TYPE_EDGE_FALLING:
983 mode = IRQ_EDGE_FALLING;
985 case IRQ_TYPE_EDGE_BOTH:
986 mode = IRQ_EDGE_BOTH;
988 case IRQ_TYPE_LEVEL_HIGH:
989 mode = IRQ_LEVEL_HIGH;
991 case IRQ_TYPE_LEVEL_LOW:
992 mode = IRQ_LEVEL_LOW;
998 raw_spin_lock_irqsave(&pctl->lock, flags);
1000 if (type & IRQ_TYPE_LEVEL_MASK)
1001 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
1002 handle_fasteoi_irq, NULL);
1004 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
1005 handle_edge_irq, NULL);
1007 regval = readl(pctl->membase + reg);
1008 regval &= ~(IRQ_CFG_IRQ_MASK << index);
1009 writel(regval | (mode << index), pctl->membase + reg);
1011 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1016 static void sunxi_pinctrl_irq_ack(struct irq_data *d)
1018 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1019 u32 status_reg = sunxi_irq_status_reg(pctl->desc, d->hwirq);
1020 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
1023 writel(1 << status_idx, pctl->membase + status_reg);
1026 static void sunxi_pinctrl_irq_mask(struct irq_data *d)
1028 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1029 u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1030 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1031 unsigned long flags;
1034 raw_spin_lock_irqsave(&pctl->lock, flags);
1037 val = readl(pctl->membase + reg);
1038 writel(val & ~(1 << idx), pctl->membase + reg);
1040 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1043 static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
1045 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1046 u32 reg = sunxi_irq_ctrl_reg(pctl->desc, d->hwirq);
1047 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
1048 unsigned long flags;
1051 raw_spin_lock_irqsave(&pctl->lock, flags);
1053 /* Unmask the IRQ */
1054 val = readl(pctl->membase + reg);
1055 writel(val | (1 << idx), pctl->membase + reg);
1057 raw_spin_unlock_irqrestore(&pctl->lock, flags);
1060 static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
1062 sunxi_pinctrl_irq_ack(d);
1063 sunxi_pinctrl_irq_unmask(d);
1066 static int sunxi_pinctrl_irq_set_wake(struct irq_data *d, unsigned int on)
1068 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
1069 u8 bank = d->hwirq / IRQ_PER_BANK;
1071 return irq_set_irq_wake(pctl->irq[bank], on);
1074 static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
1075 .name = "sunxi_pio_edge",
1076 .irq_ack = sunxi_pinctrl_irq_ack,
1077 .irq_mask = sunxi_pinctrl_irq_mask,
1078 .irq_unmask = sunxi_pinctrl_irq_unmask,
1079 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
1080 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
1081 .irq_set_type = sunxi_pinctrl_irq_set_type,
1082 .irq_set_wake = sunxi_pinctrl_irq_set_wake,
1083 .flags = IRQCHIP_MASK_ON_SUSPEND,
1086 static struct irq_chip sunxi_pinctrl_level_irq_chip = {
1087 .name = "sunxi_pio_level",
1088 .irq_eoi = sunxi_pinctrl_irq_ack,
1089 .irq_mask = sunxi_pinctrl_irq_mask,
1090 .irq_unmask = sunxi_pinctrl_irq_unmask,
1091 /* Define irq_enable / disable to avoid spurious irqs for drivers
1092 * using these to suppress irqs while they clear the irq source */
1093 .irq_enable = sunxi_pinctrl_irq_ack_unmask,
1094 .irq_disable = sunxi_pinctrl_irq_mask,
1095 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
1096 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
1097 .irq_set_type = sunxi_pinctrl_irq_set_type,
1098 .irq_set_wake = sunxi_pinctrl_irq_set_wake,
1099 .flags = IRQCHIP_EOI_THREADED |
1100 IRQCHIP_MASK_ON_SUSPEND |
1101 IRQCHIP_EOI_IF_HANDLED,
1104 static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
1105 struct device_node *node,
1107 unsigned int intsize,
1108 unsigned long *out_hwirq,
1109 unsigned int *out_type)
1111 struct sunxi_pinctrl *pctl = d->host_data;
1112 struct sunxi_desc_function *desc;
1118 base = PINS_PER_BANK * intspec[0];
1119 pin = pctl->desc->pin_base + base + intspec[1];
1121 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
1125 *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
1126 *out_type = intspec[2];
1131 static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
1132 .xlate = sunxi_pinctrl_irq_of_xlate,
1135 static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
1137 unsigned int irq = irq_desc_get_irq(desc);
1138 struct irq_chip *chip = irq_desc_get_chip(desc);
1139 struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
1140 unsigned long bank, reg, val;
1142 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
1143 if (irq == pctl->irq[bank])
1146 WARN_ON(bank == pctl->desc->irq_banks);
1148 chained_irq_enter(chip, desc);
1150 reg = sunxi_irq_status_reg_from_bank(pctl->desc, bank);
1151 val = readl(pctl->membase + reg);
1156 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK)
1157 generic_handle_domain_irq(pctl->domain,
1158 bank * IRQ_PER_BANK + irqoffset);
1161 chained_irq_exit(chip, desc);
1164 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
1167 struct sunxi_pinctrl_function *func = pctl->functions;
1169 while (func->name) {
1170 /* function already there */
1171 if (strcmp(func->name, name) == 0) {
1186 static int sunxi_pinctrl_build_state(struct platform_device *pdev)
1188 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
1195 * We assume that the number of groups is the number of pins
1196 * given in the data array.
1198 * This will not always be true, since some pins might not be
1199 * available in the current variant, but fortunately for us,
1200 * this means that the number of pins is the maximum group
1201 * number we will ever see.
1203 pctl->groups = devm_kcalloc(&pdev->dev,
1204 pctl->desc->npins, sizeof(*pctl->groups),
1209 for (i = 0; i < pctl->desc->npins; i++) {
1210 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1211 struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
1213 if (pin->variant && !(pctl->variant & pin->variant))
1216 group->name = pin->pin.name;
1217 group->pin = pin->pin.number;
1219 /* And now we count the actual number of pins / groups */
1224 * Find an upper bound for the maximum number of functions: in
1225 * the worst case we have gpio_in, gpio_out, irq and up to four
1226 * special functions per pin, plus one entry for the sentinel.
1227 * We'll reallocate that later anyway.
1229 pctl->functions = kcalloc(4 * pctl->ngroups + 4,
1230 sizeof(*pctl->functions),
1232 if (!pctl->functions)
1235 /* Count functions and their associated groups */
1236 for (i = 0; i < pctl->desc->npins; i++) {
1237 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1238 struct sunxi_desc_function *func;
1240 if (pin->variant && !(pctl->variant & pin->variant))
1243 for (func = pin->functions; func->name; func++) {
1244 if (func->variant && !(pctl->variant & func->variant))
1247 /* Create interrupt mapping while we're at it */
1248 if (!strcmp(func->name, "irq")) {
1249 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
1250 pctl->irq_array[irqnum] = pin->pin.number;
1253 sunxi_pinctrl_add_function(pctl, func->name);
1257 /* And now allocated and fill the array for real */
1258 ptr = krealloc(pctl->functions,
1259 pctl->nfunctions * sizeof(*pctl->functions),
1262 kfree(pctl->functions);
1263 pctl->functions = NULL;
1266 pctl->functions = ptr;
1268 for (i = 0; i < pctl->desc->npins; i++) {
1269 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1270 struct sunxi_desc_function *func;
1272 if (pin->variant && !(pctl->variant & pin->variant))
1275 for (func = pin->functions; func->name; func++) {
1276 struct sunxi_pinctrl_function *func_item;
1277 const char **func_grp;
1279 if (func->variant && !(pctl->variant & func->variant))
1282 func_item = sunxi_pinctrl_find_function_by_name(pctl,
1285 kfree(pctl->functions);
1289 if (!func_item->groups) {
1291 devm_kcalloc(&pdev->dev,
1293 sizeof(*func_item->groups),
1295 if (!func_item->groups) {
1296 kfree(pctl->functions);
1301 func_grp = func_item->groups;
1305 *func_grp = pin->pin.name;
1312 static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
1314 unsigned long clock = clk_get_rate(clk);
1315 unsigned int best_diff, best_div;
1318 best_diff = abs(freq - clock);
1321 for (i = 1; i < 8; i++) {
1322 int cur_diff = abs(freq - (clock >> i));
1324 if (cur_diff < best_diff) {
1325 best_diff = cur_diff;
1334 static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
1335 struct device_node *node)
1337 unsigned int hosc_diff, losc_diff;
1338 unsigned int hosc_div, losc_div;
1339 struct clk *hosc, *losc;
1343 /* Deal with old DTs that didn't have the oscillators */
1344 if (of_clk_get_parent_count(node) != 3)
1347 /* If we don't have any setup, bail out */
1348 if (!of_find_property(node, "input-debounce", NULL))
1351 losc = devm_clk_get(pctl->dev, "losc");
1353 return PTR_ERR(losc);
1355 hosc = devm_clk_get(pctl->dev, "hosc");
1357 return PTR_ERR(hosc);
1359 for (i = 0; i < pctl->desc->irq_banks; i++) {
1360 unsigned long debounce_freq;
1363 ret = of_property_read_u32_index(node, "input-debounce",
1371 debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
1372 losc_div = sunxi_pinctrl_get_debounce_div(losc,
1376 hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
1380 if (hosc_diff < losc_diff) {
1388 writel(src | div << 4,
1390 sunxi_irq_debounce_reg_from_bank(pctl->desc, i));
1396 int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
1397 const struct sunxi_pinctrl_desc *desc,
1398 unsigned long variant)
1400 struct device_node *node = pdev->dev.of_node;
1401 struct pinctrl_desc *pctrl_desc;
1402 struct pinctrl_pin_desc *pins;
1403 struct sunxi_pinctrl *pctl;
1404 struct pinmux_ops *pmxops;
1405 int i, ret, last_pin, pin_idx;
1408 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1411 platform_set_drvdata(pdev, pctl);
1413 raw_spin_lock_init(&pctl->lock);
1415 pctl->membase = devm_platform_ioremap_resource(pdev, 0);
1416 if (IS_ERR(pctl->membase))
1417 return PTR_ERR(pctl->membase);
1419 pctl->dev = &pdev->dev;
1421 pctl->variant = variant;
1423 pctl->irq_array = devm_kcalloc(&pdev->dev,
1424 IRQ_PER_BANK * pctl->desc->irq_banks,
1425 sizeof(*pctl->irq_array),
1427 if (!pctl->irq_array)
1430 ret = sunxi_pinctrl_build_state(pdev);
1432 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
1436 pins = devm_kcalloc(&pdev->dev,
1437 pctl->desc->npins, sizeof(*pins),
1442 for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
1443 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1445 if (pin->variant && !(pctl->variant & pin->variant))
1448 pins[pin_idx++] = pin->pin;
1451 pctrl_desc = devm_kzalloc(&pdev->dev,
1452 sizeof(*pctrl_desc),
1457 pctrl_desc->name = dev_name(&pdev->dev);
1458 pctrl_desc->owner = THIS_MODULE;
1459 pctrl_desc->pins = pins;
1460 pctrl_desc->npins = pctl->ngroups;
1461 pctrl_desc->confops = &sunxi_pconf_ops;
1462 pctrl_desc->pctlops = &sunxi_pctrl_ops;
1464 pmxops = devm_kmemdup(&pdev->dev, &sunxi_pmx_ops, sizeof(sunxi_pmx_ops),
1469 if (desc->disable_strict_mode)
1470 pmxops->strict = false;
1472 pctrl_desc->pmxops = pmxops;
1474 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
1475 if (IS_ERR(pctl->pctl_dev)) {
1476 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1477 return PTR_ERR(pctl->pctl_dev);
1480 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1484 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
1485 pctl->chip->owner = THIS_MODULE;
1486 pctl->chip->request = gpiochip_generic_request;
1487 pctl->chip->free = gpiochip_generic_free;
1488 pctl->chip->set_config = gpiochip_generic_config;
1489 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
1490 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
1491 pctl->chip->get = sunxi_pinctrl_gpio_get;
1492 pctl->chip->set = sunxi_pinctrl_gpio_set;
1493 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
1494 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq;
1495 pctl->chip->of_gpio_n_cells = 3;
1496 pctl->chip->can_sleep = false;
1497 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
1498 pctl->desc->pin_base;
1499 pctl->chip->label = dev_name(&pdev->dev);
1500 pctl->chip->parent = &pdev->dev;
1501 pctl->chip->base = pctl->desc->pin_base;
1503 ret = gpiochip_add_data(pctl->chip, pctl);
1507 for (i = 0; i < pctl->desc->npins; i++) {
1508 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1510 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1511 pin->pin.number - pctl->desc->pin_base,
1512 pin->pin.number, 1);
1514 goto gpiochip_error;
1517 ret = of_clk_get_parent_count(node);
1518 clk = devm_clk_get(&pdev->dev, ret == 1 ? NULL : "apb");
1521 goto gpiochip_error;
1524 ret = clk_prepare_enable(clk);
1526 goto gpiochip_error;
1528 pctl->irq = devm_kcalloc(&pdev->dev,
1529 pctl->desc->irq_banks,
1537 for (i = 0; i < pctl->desc->irq_banks; i++) {
1538 pctl->irq[i] = platform_get_irq(pdev, i);
1539 if (pctl->irq[i] < 0) {
1545 pctl->domain = irq_domain_add_linear(node,
1546 pctl->desc->irq_banks * IRQ_PER_BANK,
1547 &sunxi_pinctrl_irq_domain_ops,
1549 if (!pctl->domain) {
1550 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1555 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
1556 int irqno = irq_create_mapping(pctl->domain, i);
1558 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
1560 irq_set_chip_data(irqno, pctl);
1563 for (i = 0; i < pctl->desc->irq_banks; i++) {
1564 /* Mask and clear all IRQs before registering a handler */
1565 writel(0, pctl->membase +
1566 sunxi_irq_ctrl_reg_from_bank(pctl->desc, i));
1569 sunxi_irq_status_reg_from_bank(pctl->desc, i));
1571 irq_set_chained_handler_and_data(pctl->irq[i],
1572 sunxi_pinctrl_irq_handler,
1576 sunxi_pinctrl_setup_debounce(pctl, node);
1578 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
1583 clk_disable_unprepare(clk);
1585 gpiochip_remove(pctl->chip);