2 * Pinctrl driver for Rockchip SoCs
4 * Copyright (c) 2013 MundoReader S.L.
5 * Author: Heiko Stuebner <heiko@sntech.de>
7 * With some ideas taken from pinctrl-samsung:
8 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
9 * http://www.samsung.com
10 * Copyright (c) 2012 Linaro Ltd
11 * http://www.linaro.org
14 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as published
18 * by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
29 #include <linux/bitops.h>
30 #include <linux/gpio.h>
31 #include <linux/of_address.h>
32 #include <linux/of_irq.h>
33 #include <linux/pinctrl/machine.h>
34 #include <linux/pinctrl/pinconf.h>
35 #include <linux/pinctrl/pinctrl.h>
36 #include <linux/pinctrl/pinmux.h>
37 #include <linux/pinctrl/pinconf-generic.h>
38 #include <linux/irqchip/chained_irq.h>
39 #include <linux/clk.h>
40 #include <linux/regmap.h>
41 #include <linux/mfd/syscon.h>
42 #include <dt-bindings/pinctrl/rockchip.h>
47 /* GPIO control registers */
48 #define GPIO_SWPORT_DR 0x00
49 #define GPIO_SWPORT_DDR 0x04
50 #define GPIO_INTEN 0x30
51 #define GPIO_INTMASK 0x34
52 #define GPIO_INTTYPE_LEVEL 0x38
53 #define GPIO_INT_POLARITY 0x3c
54 #define GPIO_INT_STATUS 0x40
55 #define GPIO_INT_RAWSTATUS 0x44
56 #define GPIO_DEBOUNCE 0x48
57 #define GPIO_PORTS_EOI 0x4c
58 #define GPIO_EXT_PORT 0x50
59 #define GPIO_LS_SYNC 0x60
61 enum rockchip_pinctrl_type {
70 * Encode variants of iomux registers into a type variable
72 #define IOMUX_GPIO_ONLY BIT(0)
73 #define IOMUX_WIDTH_4BIT BIT(1)
74 #define IOMUX_SOURCE_PMU BIT(2)
75 #define IOMUX_UNROUTED BIT(3)
78 * @type: iomux variant using IOMUX_* constants
79 * @offset: if initialized to -1 it will be autocalculated, by specifying
80 * an initial offset value the relevant source offset can be reset
81 * to a new value for autocalculating the following iomux registers.
83 struct rockchip_iomux {
89 * @reg_base: register base of the gpio bank
90 * @reg_pull: optional separate register for additional pull settings
91 * @clk: clock of the gpio bank
92 * @irq: interrupt of the gpio bank
93 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
94 * @pin_base: first pin number
95 * @nr_pins: number of pins in this bank
96 * @name: name of the bank
97 * @bank_num: number of the bank, to account for holes
98 * @iomux: array describing the 4 iomux sources of the bank
99 * @valid: are all necessary informations present
100 * @of_node: dt node of this bank
101 * @drvdata: common pinctrl basedata
102 * @domain: irqdomain of the gpio bank
103 * @gpio_chip: gpiolib chip
104 * @grange: gpio range
105 * @slock: spinlock for the gpio bank
107 struct rockchip_pin_bank {
108 void __iomem *reg_base;
109 struct regmap *regmap_pull;
117 struct rockchip_iomux iomux[4];
119 struct device_node *of_node;
120 struct rockchip_pinctrl *drvdata;
121 struct irq_domain *domain;
122 struct gpio_chip gpio_chip;
123 struct pinctrl_gpio_range grange;
125 u32 toggle_edge_mode;
128 #define PIN_BANK(id, pins, label) \
141 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
147 { .type = iom0, .offset = -1 }, \
148 { .type = iom1, .offset = -1 }, \
149 { .type = iom2, .offset = -1 }, \
150 { .type = iom3, .offset = -1 }, \
156 struct rockchip_pin_ctrl {
157 struct rockchip_pin_bank *pin_banks;
161 enum rockchip_pinctrl_type type;
164 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
165 int pin_num, struct regmap **regmap,
167 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
168 int pin_num, struct regmap **regmap,
172 struct rockchip_pin_config {
174 unsigned long *configs;
175 unsigned int nconfigs;
179 * struct rockchip_pin_group: represent group of pins of a pinmux function.
180 * @name: name of the pin group, used to lookup the group.
181 * @pins: the pins included in this group.
182 * @npins: number of pins included in this group.
183 * @func: the mux function number to be programmed when selected.
184 * @configs: the config values to be set for each pin
185 * @nconfigs: number of configs for each pin
187 struct rockchip_pin_group {
191 struct rockchip_pin_config *data;
195 * struct rockchip_pmx_func: represent a pin function.
196 * @name: name of the pin function, used to lookup the function.
197 * @groups: one or more names of pin groups that provide this function.
198 * @num_groups: number of groups included in @groups.
200 struct rockchip_pmx_func {
206 struct rockchip_pinctrl {
207 struct regmap *regmap_base;
209 struct regmap *regmap_pull;
210 struct regmap *regmap_pmu;
212 struct rockchip_pin_ctrl *ctrl;
213 struct pinctrl_desc pctl;
214 struct pinctrl_dev *pctl_dev;
215 struct rockchip_pin_group *groups;
216 unsigned int ngroups;
217 struct rockchip_pmx_func *functions;
218 unsigned int nfunctions;
221 static struct regmap_config rockchip_regmap_config = {
227 static const inline struct rockchip_pin_group *pinctrl_name_to_group(
228 const struct rockchip_pinctrl *info,
233 for (i = 0; i < info->ngroups; i++) {
234 if (!strcmp(info->groups[i].name, name))
235 return &info->groups[i];
242 * given a pin number that is local to a pin controller, find out the pin bank
243 * and the register base of the pin bank.
245 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
248 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
250 while (pin >= (b->pin_base + b->nr_pins))
256 static struct rockchip_pin_bank *bank_num_to_bank(
257 struct rockchip_pinctrl *info,
260 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
263 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
264 if (b->bank_num == num)
268 return ERR_PTR(-EINVAL);
272 * Pinctrl_ops handling
275 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
277 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
279 return info->ngroups;
282 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
285 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
287 return info->groups[selector].name;
290 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
291 unsigned selector, const unsigned **pins,
294 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
296 if (selector >= info->ngroups)
299 *pins = info->groups[selector].pins;
300 *npins = info->groups[selector].npins;
305 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
306 struct device_node *np,
307 struct pinctrl_map **map, unsigned *num_maps)
309 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
310 const struct rockchip_pin_group *grp;
311 struct pinctrl_map *new_map;
312 struct device_node *parent;
317 * first find the group of this node and check if we need to create
318 * config maps for pins
320 grp = pinctrl_name_to_group(info, np->name);
322 dev_err(info->dev, "unable to find group for node %s\n",
327 map_num += grp->npins;
328 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
337 parent = of_get_parent(np);
339 devm_kfree(pctldev->dev, new_map);
342 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
343 new_map[0].data.mux.function = parent->name;
344 new_map[0].data.mux.group = np->name;
347 /* create config map */
349 for (i = 0; i < grp->npins; i++) {
350 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
351 new_map[i].data.configs.group_or_pin =
352 pin_get_name(pctldev, grp->pins[i]);
353 new_map[i].data.configs.configs = grp->data[i].configs;
354 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
357 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
358 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
363 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
364 struct pinctrl_map *map, unsigned num_maps)
368 static const struct pinctrl_ops rockchip_pctrl_ops = {
369 .get_groups_count = rockchip_get_groups_count,
370 .get_group_name = rockchip_get_group_name,
371 .get_group_pins = rockchip_get_group_pins,
372 .dt_node_to_map = rockchip_dt_node_to_map,
373 .dt_free_map = rockchip_dt_free_map,
380 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
382 struct rockchip_pinctrl *info = bank->drvdata;
383 int iomux_num = (pin / 8);
384 struct regmap *regmap;
392 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
393 dev_err(info->dev, "pin %d is unrouted\n", pin);
397 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
400 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
401 ? info->regmap_pmu : info->regmap_base;
403 /* get basic quadrupel of mux registers and the correct reg inside */
404 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
405 reg = bank->iomux[iomux_num].offset;
406 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
414 ret = regmap_read(regmap, reg, &val);
418 return ((val >> bit) & mask);
422 * Set a new mux function for a pin.
424 * The register is divided into the upper and lower 16 bit. When changing
425 * a value, the previous register value is not read and changed. Instead
426 * it seems the changed bits are marked in the upper 16 bit, while the
427 * changed value gets set in the same offset in the lower 16 bit.
428 * All pin settings seem to be 2 bit wide in both the upper and lower
430 * @bank: pin bank to change
431 * @pin: pin to change
432 * @mux: new mux function to set
434 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
436 struct rockchip_pinctrl *info = bank->drvdata;
437 int iomux_num = (pin / 8);
438 struct regmap *regmap;
447 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
448 dev_err(info->dev, "pin %d is unrouted\n", pin);
452 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
453 if (mux != RK_FUNC_GPIO) {
455 "pin %d only supports a gpio mux\n", pin);
462 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
463 bank->bank_num, pin, mux);
465 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
466 ? info->regmap_pmu : info->regmap_base;
468 /* get basic quadrupel of mux registers and the correct reg inside */
469 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
470 reg = bank->iomux[iomux_num].offset;
471 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
479 spin_lock_irqsave(&bank->slock, flags);
481 data = (mask << (bit + 16));
482 rmask = data | (data >> 16);
483 data |= (mux & mask) << bit;
484 ret = regmap_update_bits(regmap, reg, rmask, data);
486 spin_unlock_irqrestore(&bank->slock, flags);
491 #define RK2928_PULL_OFFSET 0x118
492 #define RK2928_PULL_PINS_PER_REG 16
493 #define RK2928_PULL_BANK_STRIDE 8
495 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
496 int pin_num, struct regmap **regmap,
499 struct rockchip_pinctrl *info = bank->drvdata;
501 *regmap = info->regmap_base;
502 *reg = RK2928_PULL_OFFSET;
503 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
504 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
506 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
509 #define RK3188_PULL_OFFSET 0x164
510 #define RK3188_PULL_BITS_PER_PIN 2
511 #define RK3188_PULL_PINS_PER_REG 8
512 #define RK3188_PULL_BANK_STRIDE 16
513 #define RK3188_PULL_PMU_OFFSET 0x64
515 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
516 int pin_num, struct regmap **regmap,
519 struct rockchip_pinctrl *info = bank->drvdata;
521 /* The first 12 pins of the first bank are located elsewhere */
522 if (bank->bank_num == 0 && pin_num < 12) {
523 *regmap = info->regmap_pmu ? info->regmap_pmu
525 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
526 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
527 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
528 *bit *= RK3188_PULL_BITS_PER_PIN;
530 *regmap = info->regmap_pull ? info->regmap_pull
532 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
534 /* correct the offset, as it is the 2nd pull register */
536 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
537 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
540 * The bits in these registers have an inverse ordering
541 * with the lowest pin being in bits 15:14 and the highest
544 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
545 *bit *= RK3188_PULL_BITS_PER_PIN;
549 #define RK3288_PULL_OFFSET 0x140
550 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
551 int pin_num, struct regmap **regmap,
554 struct rockchip_pinctrl *info = bank->drvdata;
556 /* The first 24 pins of the first bank are located in PMU */
557 if (bank->bank_num == 0) {
558 *regmap = info->regmap_pmu;
559 *reg = RK3188_PULL_PMU_OFFSET;
561 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
562 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
563 *bit *= RK3188_PULL_BITS_PER_PIN;
565 *regmap = info->regmap_base;
566 *reg = RK3288_PULL_OFFSET;
568 /* correct the offset, as we're starting with the 2nd bank */
570 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
571 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
573 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
574 *bit *= RK3188_PULL_BITS_PER_PIN;
578 #define RK3288_DRV_PMU_OFFSET 0x70
579 #define RK3288_DRV_GRF_OFFSET 0x1c0
580 #define RK3288_DRV_BITS_PER_PIN 2
581 #define RK3288_DRV_PINS_PER_REG 8
582 #define RK3288_DRV_BANK_STRIDE 16
584 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
585 int pin_num, struct regmap **regmap,
588 struct rockchip_pinctrl *info = bank->drvdata;
590 /* The first 24 pins of the first bank are located in PMU */
591 if (bank->bank_num == 0) {
592 *regmap = info->regmap_pmu;
593 *reg = RK3288_DRV_PMU_OFFSET;
595 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
596 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
597 *bit *= RK3288_DRV_BITS_PER_PIN;
599 *regmap = info->regmap_base;
600 *reg = RK3288_DRV_GRF_OFFSET;
602 /* correct the offset, as we're starting with the 2nd bank */
604 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
605 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
607 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
608 *bit *= RK3288_DRV_BITS_PER_PIN;
612 #define RK3228_PULL_OFFSET 0x100
614 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
615 int pin_num, struct regmap **regmap,
618 struct rockchip_pinctrl *info = bank->drvdata;
620 *regmap = info->regmap_base;
621 *reg = RK3228_PULL_OFFSET;
622 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
623 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
625 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
626 *bit *= RK3188_PULL_BITS_PER_PIN;
629 #define RK3228_DRV_GRF_OFFSET 0x200
631 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
632 int pin_num, struct regmap **regmap,
635 struct rockchip_pinctrl *info = bank->drvdata;
637 *regmap = info->regmap_base;
638 *reg = RK3228_DRV_GRF_OFFSET;
639 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
640 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
642 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
643 *bit *= RK3288_DRV_BITS_PER_PIN;
646 #define RK3368_PULL_GRF_OFFSET 0x100
647 #define RK3368_PULL_PMU_OFFSET 0x10
649 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
650 int pin_num, struct regmap **regmap,
653 struct rockchip_pinctrl *info = bank->drvdata;
655 /* The first 32 pins of the first bank are located in PMU */
656 if (bank->bank_num == 0) {
657 *regmap = info->regmap_pmu;
658 *reg = RK3368_PULL_PMU_OFFSET;
660 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
661 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
662 *bit *= RK3188_PULL_BITS_PER_PIN;
664 *regmap = info->regmap_base;
665 *reg = RK3368_PULL_GRF_OFFSET;
667 /* correct the offset, as we're starting with the 2nd bank */
669 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
670 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
672 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
673 *bit *= RK3188_PULL_BITS_PER_PIN;
677 #define RK3368_DRV_PMU_OFFSET 0x20
678 #define RK3368_DRV_GRF_OFFSET 0x200
680 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
681 int pin_num, struct regmap **regmap,
684 struct rockchip_pinctrl *info = bank->drvdata;
686 /* The first 32 pins of the first bank are located in PMU */
687 if (bank->bank_num == 0) {
688 *regmap = info->regmap_pmu;
689 *reg = RK3368_DRV_PMU_OFFSET;
691 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
692 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
693 *bit *= RK3288_DRV_BITS_PER_PIN;
695 *regmap = info->regmap_base;
696 *reg = RK3368_DRV_GRF_OFFSET;
698 /* correct the offset, as we're starting with the 2nd bank */
700 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
701 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
703 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
704 *bit *= RK3288_DRV_BITS_PER_PIN;
708 static int rockchip_perpin_drv_list[] = { 2, 4, 8, 12 };
710 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
713 struct rockchip_pinctrl *info = bank->drvdata;
714 struct rockchip_pin_ctrl *ctrl = info->ctrl;
715 struct regmap *regmap;
720 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
722 ret = regmap_read(regmap, reg, &data);
727 data &= (1 << RK3288_DRV_BITS_PER_PIN) - 1;
729 return rockchip_perpin_drv_list[data];
732 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
733 int pin_num, int strength)
735 struct rockchip_pinctrl *info = bank->drvdata;
736 struct rockchip_pin_ctrl *ctrl = info->ctrl;
737 struct regmap *regmap;
743 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
746 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list); i++) {
747 if (rockchip_perpin_drv_list[i] == strength) {
754 dev_err(info->dev, "unsupported driver strength %d\n",
759 spin_lock_irqsave(&bank->slock, flags);
761 /* enable the write to the equivalent lower bits */
762 data = ((1 << RK3288_DRV_BITS_PER_PIN) - 1) << (bit + 16);
763 rmask = data | (data >> 16);
764 data |= (ret << bit);
766 ret = regmap_update_bits(regmap, reg, rmask, data);
767 spin_unlock_irqrestore(&bank->slock, flags);
772 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
774 struct rockchip_pinctrl *info = bank->drvdata;
775 struct rockchip_pin_ctrl *ctrl = info->ctrl;
776 struct regmap *regmap;
781 /* rk3066b does support any pulls */
782 if (ctrl->type == RK3066B)
783 return PIN_CONFIG_BIAS_DISABLE;
785 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
787 ret = regmap_read(regmap, reg, &data);
791 switch (ctrl->type) {
793 return !(data & BIT(bit))
794 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
795 : PIN_CONFIG_BIAS_DISABLE;
800 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
804 return PIN_CONFIG_BIAS_DISABLE;
806 return PIN_CONFIG_BIAS_PULL_UP;
808 return PIN_CONFIG_BIAS_PULL_DOWN;
810 return PIN_CONFIG_BIAS_BUS_HOLD;
813 dev_err(info->dev, "unknown pull setting\n");
816 dev_err(info->dev, "unsupported pinctrl type\n");
821 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
822 int pin_num, int pull)
824 struct rockchip_pinctrl *info = bank->drvdata;
825 struct rockchip_pin_ctrl *ctrl = info->ctrl;
826 struct regmap *regmap;
832 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
833 bank->bank_num, pin_num, pull);
835 /* rk3066b does support any pulls */
836 if (ctrl->type == RK3066B)
837 return pull ? -EINVAL : 0;
839 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
841 switch (ctrl->type) {
843 spin_lock_irqsave(&bank->slock, flags);
845 data = BIT(bit + 16);
846 if (pull == PIN_CONFIG_BIAS_DISABLE)
848 ret = regmap_write(regmap, reg, data);
850 spin_unlock_irqrestore(&bank->slock, flags);
855 spin_lock_irqsave(&bank->slock, flags);
857 /* enable the write to the equivalent lower bits */
858 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
859 rmask = data | (data >> 16);
862 case PIN_CONFIG_BIAS_DISABLE:
864 case PIN_CONFIG_BIAS_PULL_UP:
867 case PIN_CONFIG_BIAS_PULL_DOWN:
870 case PIN_CONFIG_BIAS_BUS_HOLD:
874 spin_unlock_irqrestore(&bank->slock, flags);
875 dev_err(info->dev, "unsupported pull setting %d\n",
880 ret = regmap_update_bits(regmap, reg, rmask, data);
882 spin_unlock_irqrestore(&bank->slock, flags);
885 dev_err(info->dev, "unsupported pinctrl type\n");
893 * Pinmux_ops handling
896 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
898 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
900 return info->nfunctions;
903 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
906 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
908 return info->functions[selector].name;
911 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
912 unsigned selector, const char * const **groups,
913 unsigned * const num_groups)
915 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
917 *groups = info->functions[selector].groups;
918 *num_groups = info->functions[selector].ngroups;
923 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
926 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
927 const unsigned int *pins = info->groups[group].pins;
928 const struct rockchip_pin_config *data = info->groups[group].data;
929 struct rockchip_pin_bank *bank;
932 dev_dbg(info->dev, "enable function %s group %s\n",
933 info->functions[selector].name, info->groups[group].name);
936 * for each pin in the pin group selected, program the correspoding pin
937 * pin function number in the config register.
939 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
940 bank = pin_to_bank(info, pins[cnt]);
941 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
948 /* revert the already done pin settings */
949 for (cnt--; cnt >= 0; cnt--)
950 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
959 * The calls to gpio_direction_output() and gpio_direction_input()
960 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
961 * function called from the gpiolib interface).
963 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
966 struct rockchip_pin_bank *bank;
971 bank = gpiochip_get_data(chip);
973 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
977 clk_enable(bank->clk);
978 spin_lock_irqsave(&bank->slock, flags);
980 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
981 /* set bit to 1 for output, 0 for input */
986 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
988 spin_unlock_irqrestore(&bank->slock, flags);
989 clk_disable(bank->clk);
994 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
995 struct pinctrl_gpio_range *range,
996 unsigned offset, bool input)
998 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
999 struct gpio_chip *chip;
1003 pin = offset - chip->base;
1004 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
1005 offset, range->name, pin, input ? "input" : "output");
1007 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
1011 static const struct pinmux_ops rockchip_pmx_ops = {
1012 .get_functions_count = rockchip_pmx_get_funcs_count,
1013 .get_function_name = rockchip_pmx_get_func_name,
1014 .get_function_groups = rockchip_pmx_get_groups,
1015 .set_mux = rockchip_pmx_set,
1016 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
1020 * Pinconf_ops handling
1023 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
1024 enum pin_config_param pull)
1026 switch (ctrl->type) {
1028 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
1029 pull == PIN_CONFIG_BIAS_DISABLE);
1031 return pull ? false : true;
1035 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
1041 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
1042 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
1044 /* set the pin config settings for a specified pin */
1045 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1046 unsigned long *configs, unsigned num_configs)
1048 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1049 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1050 enum pin_config_param param;
1055 for (i = 0; i < num_configs; i++) {
1056 param = pinconf_to_config_param(configs[i]);
1057 arg = pinconf_to_config_argument(configs[i]);
1060 case PIN_CONFIG_BIAS_DISABLE:
1061 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1066 case PIN_CONFIG_BIAS_PULL_UP:
1067 case PIN_CONFIG_BIAS_PULL_DOWN:
1068 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1069 case PIN_CONFIG_BIAS_BUS_HOLD:
1070 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1076 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1081 case PIN_CONFIG_OUTPUT:
1082 rockchip_gpio_set(&bank->gpio_chip,
1083 pin - bank->pin_base, arg);
1084 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
1085 pin - bank->pin_base, false);
1089 case PIN_CONFIG_DRIVE_STRENGTH:
1090 /* rk3288 is the first with per-pin drive-strength */
1091 if (!info->ctrl->drv_calc_reg)
1094 rc = rockchip_set_drive_perpin(bank,
1095 pin - bank->pin_base, arg);
1103 } /* for each config */
1108 /* get the pin config settings for a specified pin */
1109 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1110 unsigned long *config)
1112 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1113 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1114 enum pin_config_param param = pinconf_to_config_param(*config);
1119 case PIN_CONFIG_BIAS_DISABLE:
1120 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1125 case PIN_CONFIG_BIAS_PULL_UP:
1126 case PIN_CONFIG_BIAS_PULL_DOWN:
1127 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1128 case PIN_CONFIG_BIAS_BUS_HOLD:
1129 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1132 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1137 case PIN_CONFIG_OUTPUT:
1138 rc = rockchip_get_mux(bank, pin - bank->pin_base);
1139 if (rc != RK_FUNC_GPIO)
1142 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
1148 case PIN_CONFIG_DRIVE_STRENGTH:
1149 /* rk3288 is the first with per-pin drive-strength */
1150 if (!info->ctrl->drv_calc_reg)
1153 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
1164 *config = pinconf_to_config_packed(param, arg);
1169 static const struct pinconf_ops rockchip_pinconf_ops = {
1170 .pin_config_get = rockchip_pinconf_get,
1171 .pin_config_set = rockchip_pinconf_set,
1175 static const struct of_device_id rockchip_bank_match[] = {
1176 { .compatible = "rockchip,gpio-bank" },
1177 { .compatible = "rockchip,rk3188-gpio-bank0" },
1181 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
1182 struct device_node *np)
1184 struct device_node *child;
1186 for_each_child_of_node(np, child) {
1187 if (of_match_node(rockchip_bank_match, child))
1191 info->ngroups += of_get_child_count(child);
1195 static int rockchip_pinctrl_parse_groups(struct device_node *np,
1196 struct rockchip_pin_group *grp,
1197 struct rockchip_pinctrl *info,
1200 struct rockchip_pin_bank *bank;
1207 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1209 /* Initialise group */
1210 grp->name = np->name;
1213 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1214 * do sanity check and calculate pins number
1216 list = of_get_property(np, "rockchip,pins", &size);
1217 /* we do not check return since it's safe node passed down */
1218 size /= sizeof(*list);
1219 if (!size || size % 4) {
1220 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1224 grp->npins = size / 4;
1226 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1228 grp->data = devm_kzalloc(info->dev, grp->npins *
1229 sizeof(struct rockchip_pin_config),
1231 if (!grp->pins || !grp->data)
1234 for (i = 0, j = 0; i < size; i += 4, j++) {
1235 const __be32 *phandle;
1236 struct device_node *np_config;
1238 num = be32_to_cpu(*list++);
1239 bank = bank_num_to_bank(info, num);
1241 return PTR_ERR(bank);
1243 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
1244 grp->data[j].func = be32_to_cpu(*list++);
1250 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
1251 ret = pinconf_generic_parse_dt_config(np_config, NULL,
1252 &grp->data[j].configs, &grp->data[j].nconfigs);
1260 static int rockchip_pinctrl_parse_functions(struct device_node *np,
1261 struct rockchip_pinctrl *info,
1264 struct device_node *child;
1265 struct rockchip_pmx_func *func;
1266 struct rockchip_pin_group *grp;
1268 static u32 grp_index;
1271 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1273 func = &info->functions[index];
1275 /* Initialise function */
1276 func->name = np->name;
1277 func->ngroups = of_get_child_count(np);
1278 if (func->ngroups <= 0)
1281 func->groups = devm_kzalloc(info->dev,
1282 func->ngroups * sizeof(char *), GFP_KERNEL);
1286 for_each_child_of_node(np, child) {
1287 func->groups[i] = child->name;
1288 grp = &info->groups[grp_index++];
1289 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
1299 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
1300 struct rockchip_pinctrl *info)
1302 struct device *dev = &pdev->dev;
1303 struct device_node *np = dev->of_node;
1304 struct device_node *child;
1308 rockchip_pinctrl_child_count(info, np);
1310 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1311 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1313 info->functions = devm_kzalloc(dev, info->nfunctions *
1314 sizeof(struct rockchip_pmx_func),
1316 if (!info->functions) {
1317 dev_err(dev, "failed to allocate memory for function list\n");
1321 info->groups = devm_kzalloc(dev, info->ngroups *
1322 sizeof(struct rockchip_pin_group),
1324 if (!info->groups) {
1325 dev_err(dev, "failed allocate memory for ping group list\n");
1331 for_each_child_of_node(np, child) {
1332 if (of_match_node(rockchip_bank_match, child))
1335 ret = rockchip_pinctrl_parse_functions(child, info, i++);
1337 dev_err(&pdev->dev, "failed to parse function\n");
1346 static int rockchip_pinctrl_register(struct platform_device *pdev,
1347 struct rockchip_pinctrl *info)
1349 struct pinctrl_desc *ctrldesc = &info->pctl;
1350 struct pinctrl_pin_desc *pindesc, *pdesc;
1351 struct rockchip_pin_bank *pin_bank;
1355 ctrldesc->name = "rockchip-pinctrl";
1356 ctrldesc->owner = THIS_MODULE;
1357 ctrldesc->pctlops = &rockchip_pctrl_ops;
1358 ctrldesc->pmxops = &rockchip_pmx_ops;
1359 ctrldesc->confops = &rockchip_pinconf_ops;
1361 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
1362 info->ctrl->nr_pins, GFP_KERNEL);
1364 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
1367 ctrldesc->pins = pindesc;
1368 ctrldesc->npins = info->ctrl->nr_pins;
1371 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
1372 pin_bank = &info->ctrl->pin_banks[bank];
1373 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
1375 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
1376 pin_bank->name, pin);
1381 ret = rockchip_pinctrl_parse_dt(pdev, info);
1385 info->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, info);
1386 if (IS_ERR(info->pctl_dev)) {
1387 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1388 return PTR_ERR(info->pctl_dev);
1391 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
1392 pin_bank = &info->ctrl->pin_banks[bank];
1393 pin_bank->grange.name = pin_bank->name;
1394 pin_bank->grange.id = bank;
1395 pin_bank->grange.pin_base = pin_bank->pin_base;
1396 pin_bank->grange.base = pin_bank->gpio_chip.base;
1397 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
1398 pin_bank->grange.gc = &pin_bank->gpio_chip;
1399 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
1409 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
1411 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1412 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
1413 unsigned long flags;
1416 clk_enable(bank->clk);
1417 spin_lock_irqsave(&bank->slock, flags);
1420 data &= ~BIT(offset);
1422 data |= BIT(offset);
1425 spin_unlock_irqrestore(&bank->slock, flags);
1426 clk_disable(bank->clk);
1430 * Returns the level of the pin for input direction and setting of the DR
1431 * register for output gpios.
1433 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
1435 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1438 clk_enable(bank->clk);
1439 data = readl(bank->reg_base + GPIO_EXT_PORT);
1440 clk_disable(bank->clk);
1447 * gpiolib gpio_direction_input callback function. The setting of the pin
1448 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
1451 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
1453 return pinctrl_gpio_direction_input(gc->base + offset);
1457 * gpiolib gpio_direction_output callback function. The setting of the pin
1458 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
1461 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
1462 unsigned offset, int value)
1464 rockchip_gpio_set(gc, offset, value);
1465 return pinctrl_gpio_direction_output(gc->base + offset);
1469 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
1470 * and a virtual IRQ, if not already present.
1472 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
1474 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1480 virq = irq_create_mapping(bank->domain, offset);
1482 return (virq) ? : -ENXIO;
1485 static const struct gpio_chip rockchip_gpiolib_chip = {
1486 .request = gpiochip_generic_request,
1487 .free = gpiochip_generic_free,
1488 .set = rockchip_gpio_set,
1489 .get = rockchip_gpio_get,
1490 .direction_input = rockchip_gpio_direction_input,
1491 .direction_output = rockchip_gpio_direction_output,
1492 .to_irq = rockchip_gpio_to_irq,
1493 .owner = THIS_MODULE,
1497 * Interrupt handling
1500 static void rockchip_irq_demux(struct irq_desc *desc)
1502 struct irq_chip *chip = irq_desc_get_chip(desc);
1503 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
1506 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
1508 chained_irq_enter(chip, desc);
1510 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
1513 unsigned int irq, virq;
1517 virq = irq_linear_revmap(bank->domain, irq);
1520 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
1524 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
1527 * Triggering IRQ on both rising and falling edge
1528 * needs manual intervention.
1530 if (bank->toggle_edge_mode & BIT(irq)) {
1531 u32 data, data_old, polarity;
1532 unsigned long flags;
1534 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
1536 spin_lock_irqsave(&bank->slock, flags);
1538 polarity = readl_relaxed(bank->reg_base +
1540 if (data & BIT(irq))
1541 polarity &= ~BIT(irq);
1543 polarity |= BIT(irq);
1545 bank->reg_base + GPIO_INT_POLARITY);
1547 spin_unlock_irqrestore(&bank->slock, flags);
1550 data = readl_relaxed(bank->reg_base +
1552 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
1555 generic_handle_irq(virq);
1558 chained_irq_exit(chip, desc);
1561 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
1563 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1564 struct rockchip_pin_bank *bank = gc->private;
1565 u32 mask = BIT(d->hwirq);
1569 unsigned long flags;
1572 /* make sure the pin is configured as gpio input */
1573 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
1577 clk_enable(bank->clk);
1578 spin_lock_irqsave(&bank->slock, flags);
1580 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1582 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
1584 spin_unlock_irqrestore(&bank->slock, flags);
1586 if (type & IRQ_TYPE_EDGE_BOTH)
1587 irq_set_handler_locked(d, handle_edge_irq);
1589 irq_set_handler_locked(d, handle_level_irq);
1591 spin_lock_irqsave(&bank->slock, flags);
1594 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
1595 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
1598 case IRQ_TYPE_EDGE_BOTH:
1599 bank->toggle_edge_mode |= mask;
1603 * Determine gpio state. If 1 next interrupt should be falling
1606 data = readl(bank->reg_base + GPIO_EXT_PORT);
1612 case IRQ_TYPE_EDGE_RISING:
1613 bank->toggle_edge_mode &= ~mask;
1617 case IRQ_TYPE_EDGE_FALLING:
1618 bank->toggle_edge_mode &= ~mask;
1622 case IRQ_TYPE_LEVEL_HIGH:
1623 bank->toggle_edge_mode &= ~mask;
1627 case IRQ_TYPE_LEVEL_LOW:
1628 bank->toggle_edge_mode &= ~mask;
1634 spin_unlock_irqrestore(&bank->slock, flags);
1635 clk_disable(bank->clk);
1639 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
1640 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
1643 spin_unlock_irqrestore(&bank->slock, flags);
1644 clk_disable(bank->clk);
1649 static void rockchip_irq_suspend(struct irq_data *d)
1651 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1652 struct rockchip_pin_bank *bank = gc->private;
1654 clk_enable(bank->clk);
1655 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
1656 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
1657 clk_disable(bank->clk);
1660 static void rockchip_irq_resume(struct irq_data *d)
1662 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1663 struct rockchip_pin_bank *bank = gc->private;
1665 clk_enable(bank->clk);
1666 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
1667 clk_disable(bank->clk);
1670 static void rockchip_irq_gc_mask_clr_bit(struct irq_data *d)
1672 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1673 struct rockchip_pin_bank *bank = gc->private;
1675 clk_enable(bank->clk);
1676 irq_gc_mask_clr_bit(d);
1679 void rockchip_irq_gc_mask_set_bit(struct irq_data *d)
1681 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1682 struct rockchip_pin_bank *bank = gc->private;
1684 irq_gc_mask_set_bit(d);
1685 clk_disable(bank->clk);
1688 static int rockchip_interrupts_register(struct platform_device *pdev,
1689 struct rockchip_pinctrl *info)
1691 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1692 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1693 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
1694 struct irq_chip_generic *gc;
1698 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1700 dev_warn(&pdev->dev, "bank %s is not valid\n",
1705 ret = clk_enable(bank->clk);
1707 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
1712 bank->domain = irq_domain_add_linear(bank->of_node, 32,
1713 &irq_generic_chip_ops, NULL);
1714 if (!bank->domain) {
1715 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
1717 clk_disable(bank->clk);
1721 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
1722 "rockchip_gpio_irq", handle_level_irq,
1723 clr, 0, IRQ_GC_INIT_MASK_CACHE);
1725 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
1727 irq_domain_remove(bank->domain);
1728 clk_disable(bank->clk);
1733 * Linux assumes that all interrupts start out disabled/masked.
1734 * Our driver only uses the concept of masked and always keeps
1735 * things enabled, so for us that's all masked and all enabled.
1737 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
1738 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
1740 gc = irq_get_domain_generic_chip(bank->domain, 0);
1741 gc->reg_base = bank->reg_base;
1743 gc->chip_types[0].regs.mask = GPIO_INTMASK;
1744 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
1745 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
1746 gc->chip_types[0].chip.irq_mask = rockchip_irq_gc_mask_set_bit;
1747 gc->chip_types[0].chip.irq_unmask =
1748 rockchip_irq_gc_mask_clr_bit;
1749 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
1750 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
1751 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
1752 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
1753 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
1755 irq_set_chained_handler_and_data(bank->irq,
1756 rockchip_irq_demux, bank);
1758 /* map the gpio irqs here, when the clock is still running */
1759 for (j = 0 ; j < 32 ; j++)
1760 irq_create_mapping(bank->domain, j);
1762 clk_disable(bank->clk);
1768 static int rockchip_gpiolib_register(struct platform_device *pdev,
1769 struct rockchip_pinctrl *info)
1771 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1772 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1773 struct gpio_chip *gc;
1777 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1779 dev_warn(&pdev->dev, "bank %s is not valid\n",
1784 bank->gpio_chip = rockchip_gpiolib_chip;
1786 gc = &bank->gpio_chip;
1787 gc->base = bank->pin_base;
1788 gc->ngpio = bank->nr_pins;
1789 gc->parent = &pdev->dev;
1790 gc->of_node = bank->of_node;
1791 gc->label = bank->name;
1793 ret = gpiochip_add_data(gc, bank);
1795 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
1801 rockchip_interrupts_register(pdev, info);
1806 for (--i, --bank; i >= 0; --i, --bank) {
1809 gpiochip_remove(&bank->gpio_chip);
1814 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
1815 struct rockchip_pinctrl *info)
1817 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1818 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1821 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1824 gpiochip_remove(&bank->gpio_chip);
1830 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
1831 struct rockchip_pinctrl *info)
1833 struct resource res;
1836 if (of_address_to_resource(bank->of_node, 0, &res)) {
1837 dev_err(info->dev, "cannot find IO resource for bank\n");
1841 bank->reg_base = devm_ioremap_resource(info->dev, &res);
1842 if (IS_ERR(bank->reg_base))
1843 return PTR_ERR(bank->reg_base);
1846 * special case, where parts of the pull setting-registers are
1847 * part of the PMU register space
1849 if (of_device_is_compatible(bank->of_node,
1850 "rockchip,rk3188-gpio-bank0")) {
1851 struct device_node *node;
1853 node = of_parse_phandle(bank->of_node->parent,
1856 if (of_address_to_resource(bank->of_node, 1, &res)) {
1857 dev_err(info->dev, "cannot find IO resource for bank\n");
1861 base = devm_ioremap_resource(info->dev, &res);
1863 return PTR_ERR(base);
1864 rockchip_regmap_config.max_register =
1865 resource_size(&res) - 4;
1866 rockchip_regmap_config.name =
1867 "rockchip,rk3188-gpio-bank0-pull";
1868 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
1870 &rockchip_regmap_config);
1874 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
1876 bank->clk = of_clk_get(bank->of_node, 0);
1877 if (IS_ERR(bank->clk))
1878 return PTR_ERR(bank->clk);
1880 return clk_prepare(bank->clk);
1883 static const struct of_device_id rockchip_pinctrl_dt_match[];
1885 /* retrieve the soc specific data */
1886 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
1887 struct rockchip_pinctrl *d,
1888 struct platform_device *pdev)
1890 const struct of_device_id *match;
1891 struct device_node *node = pdev->dev.of_node;
1892 struct device_node *np;
1893 struct rockchip_pin_ctrl *ctrl;
1894 struct rockchip_pin_bank *bank;
1895 int grf_offs, pmu_offs, i, j;
1897 match = of_match_node(rockchip_pinctrl_dt_match, node);
1898 ctrl = (struct rockchip_pin_ctrl *)match->data;
1900 for_each_child_of_node(node, np) {
1901 if (!of_find_property(np, "gpio-controller", NULL))
1904 bank = ctrl->pin_banks;
1905 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1906 if (!strcmp(bank->name, np->name)) {
1909 if (!rockchip_get_bank_data(bank, d))
1917 grf_offs = ctrl->grf_mux_offset;
1918 pmu_offs = ctrl->pmu_mux_offset;
1919 bank = ctrl->pin_banks;
1920 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1923 spin_lock_init(&bank->slock);
1925 bank->pin_base = ctrl->nr_pins;
1926 ctrl->nr_pins += bank->nr_pins;
1928 /* calculate iomux offsets */
1929 for (j = 0; j < 4; j++) {
1930 struct rockchip_iomux *iom = &bank->iomux[j];
1933 if (bank_pins >= bank->nr_pins)
1936 /* preset offset value, set new start value */
1937 if (iom->offset >= 0) {
1938 if (iom->type & IOMUX_SOURCE_PMU)
1939 pmu_offs = iom->offset;
1941 grf_offs = iom->offset;
1942 } else { /* set current offset */
1943 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
1944 pmu_offs : grf_offs;
1947 dev_dbg(d->dev, "bank %d, iomux %d has offset 0x%x\n",
1951 * Increase offset according to iomux width.
1952 * 4bit iomux'es are spread over two registers.
1954 inc = (iom->type & IOMUX_WIDTH_4BIT) ? 8 : 4;
1955 if (iom->type & IOMUX_SOURCE_PMU)
1967 #define RK3288_GRF_GPIO6C_IOMUX 0x64
1968 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
1970 static u32 rk3288_grf_gpio6c_iomux;
1972 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
1974 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
1975 int ret = pinctrl_force_sleep(info->pctl_dev);
1981 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
1982 * the setting here, and restore it at resume.
1984 if (info->ctrl->type == RK3288) {
1985 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
1986 &rk3288_grf_gpio6c_iomux);
1988 pinctrl_force_default(info->pctl_dev);
1996 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
1998 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
1999 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2000 rk3288_grf_gpio6c_iomux |
2001 GPIO6C6_SEL_WRITE_ENABLE);
2006 return pinctrl_force_default(info->pctl_dev);
2009 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
2010 rockchip_pinctrl_resume);
2012 static int rockchip_pinctrl_probe(struct platform_device *pdev)
2014 struct rockchip_pinctrl *info;
2015 struct device *dev = &pdev->dev;
2016 struct rockchip_pin_ctrl *ctrl;
2017 struct device_node *np = pdev->dev.of_node, *node;
2018 struct resource *res;
2022 if (!dev->of_node) {
2023 dev_err(dev, "device tree node not found\n");
2027 info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL);
2033 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
2035 dev_err(dev, "driver data not available\n");
2040 node = of_parse_phandle(np, "rockchip,grf", 0);
2042 info->regmap_base = syscon_node_to_regmap(node);
2043 if (IS_ERR(info->regmap_base))
2044 return PTR_ERR(info->regmap_base);
2046 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2047 base = devm_ioremap_resource(&pdev->dev, res);
2049 return PTR_ERR(base);
2051 rockchip_regmap_config.max_register = resource_size(res) - 4;
2052 rockchip_regmap_config.name = "rockchip,pinctrl";
2053 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
2054 &rockchip_regmap_config);
2056 /* to check for the old dt-bindings */
2057 info->reg_size = resource_size(res);
2059 /* Honor the old binding, with pull registers as 2nd resource */
2060 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
2061 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2062 base = devm_ioremap_resource(&pdev->dev, res);
2064 return PTR_ERR(base);
2066 rockchip_regmap_config.max_register =
2067 resource_size(res) - 4;
2068 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
2069 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
2071 &rockchip_regmap_config);
2075 /* try to find the optional reference to the pmu syscon */
2076 node = of_parse_phandle(np, "rockchip,pmu", 0);
2078 info->regmap_pmu = syscon_node_to_regmap(node);
2079 if (IS_ERR(info->regmap_pmu))
2080 return PTR_ERR(info->regmap_pmu);
2083 ret = rockchip_gpiolib_register(pdev, info);
2087 ret = rockchip_pinctrl_register(pdev, info);
2089 rockchip_gpiolib_unregister(pdev, info);
2093 platform_set_drvdata(pdev, info);
2098 static struct rockchip_pin_bank rk2928_pin_banks[] = {
2099 PIN_BANK(0, 32, "gpio0"),
2100 PIN_BANK(1, 32, "gpio1"),
2101 PIN_BANK(2, 32, "gpio2"),
2102 PIN_BANK(3, 32, "gpio3"),
2105 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
2106 .pin_banks = rk2928_pin_banks,
2107 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
2108 .label = "RK2928-GPIO",
2110 .grf_mux_offset = 0xa8,
2111 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2114 static struct rockchip_pin_bank rk3036_pin_banks[] = {
2115 PIN_BANK(0, 32, "gpio0"),
2116 PIN_BANK(1, 32, "gpio1"),
2117 PIN_BANK(2, 32, "gpio2"),
2120 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
2121 .pin_banks = rk3036_pin_banks,
2122 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
2123 .label = "RK3036-GPIO",
2125 .grf_mux_offset = 0xa8,
2126 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2129 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
2130 PIN_BANK(0, 32, "gpio0"),
2131 PIN_BANK(1, 32, "gpio1"),
2132 PIN_BANK(2, 32, "gpio2"),
2133 PIN_BANK(3, 32, "gpio3"),
2134 PIN_BANK(4, 32, "gpio4"),
2135 PIN_BANK(6, 16, "gpio6"),
2138 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
2139 .pin_banks = rk3066a_pin_banks,
2140 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
2141 .label = "RK3066a-GPIO",
2143 .grf_mux_offset = 0xa8,
2144 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2147 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
2148 PIN_BANK(0, 32, "gpio0"),
2149 PIN_BANK(1, 32, "gpio1"),
2150 PIN_BANK(2, 32, "gpio2"),
2151 PIN_BANK(3, 32, "gpio3"),
2154 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
2155 .pin_banks = rk3066b_pin_banks,
2156 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
2157 .label = "RK3066b-GPIO",
2159 .grf_mux_offset = 0x60,
2162 static struct rockchip_pin_bank rk3188_pin_banks[] = {
2163 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
2164 PIN_BANK(1, 32, "gpio1"),
2165 PIN_BANK(2, 32, "gpio2"),
2166 PIN_BANK(3, 32, "gpio3"),
2169 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
2170 .pin_banks = rk3188_pin_banks,
2171 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
2172 .label = "RK3188-GPIO",
2174 .grf_mux_offset = 0x60,
2175 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
2178 static struct rockchip_pin_bank rk3228_pin_banks[] = {
2179 PIN_BANK(0, 32, "gpio0"),
2180 PIN_BANK(1, 32, "gpio1"),
2181 PIN_BANK(2, 32, "gpio2"),
2182 PIN_BANK(3, 32, "gpio3"),
2185 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
2186 .pin_banks = rk3228_pin_banks,
2187 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
2188 .label = "RK3228-GPIO",
2190 .grf_mux_offset = 0x0,
2191 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2192 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2195 static struct rockchip_pin_bank rk3288_pin_banks[] = {
2196 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
2201 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
2206 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
2207 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
2208 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
2213 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
2218 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
2219 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2224 PIN_BANK(8, 16, "gpio8"),
2227 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
2228 .pin_banks = rk3288_pin_banks,
2229 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
2230 .label = "RK3288-GPIO",
2232 .grf_mux_offset = 0x0,
2233 .pmu_mux_offset = 0x84,
2234 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
2235 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
2238 static struct rockchip_pin_bank rk3368_pin_banks[] = {
2239 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2244 PIN_BANK(1, 32, "gpio1"),
2245 PIN_BANK(2, 32, "gpio2"),
2246 PIN_BANK(3, 32, "gpio3"),
2249 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
2250 .pin_banks = rk3368_pin_banks,
2251 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
2252 .label = "RK3368-GPIO",
2254 .grf_mux_offset = 0x0,
2255 .pmu_mux_offset = 0x0,
2256 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
2257 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
2261 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
2262 { .compatible = "rockchip,rk2928-pinctrl",
2263 .data = (void *)&rk2928_pin_ctrl },
2264 { .compatible = "rockchip,rk3036-pinctrl",
2265 .data = (void *)&rk3036_pin_ctrl },
2266 { .compatible = "rockchip,rk3066a-pinctrl",
2267 .data = (void *)&rk3066a_pin_ctrl },
2268 { .compatible = "rockchip,rk3066b-pinctrl",
2269 .data = (void *)&rk3066b_pin_ctrl },
2270 { .compatible = "rockchip,rk3188-pinctrl",
2271 .data = (void *)&rk3188_pin_ctrl },
2272 { .compatible = "rockchip,rk3228-pinctrl",
2273 .data = (void *)&rk3228_pin_ctrl },
2274 { .compatible = "rockchip,rk3288-pinctrl",
2275 .data = (void *)&rk3288_pin_ctrl },
2276 { .compatible = "rockchip,rk3368-pinctrl",
2277 .data = (void *)&rk3368_pin_ctrl },
2280 MODULE_DEVICE_TABLE(of, rockchip_pinctrl_dt_match);
2282 static struct platform_driver rockchip_pinctrl_driver = {
2283 .probe = rockchip_pinctrl_probe,
2285 .name = "rockchip-pinctrl",
2286 .pm = &rockchip_pinctrl_dev_pm_ops,
2287 .of_match_table = rockchip_pinctrl_dt_match,
2291 static int __init rockchip_pinctrl_drv_register(void)
2293 return platform_driver_register(&rockchip_pinctrl_driver);
2295 postcore_initcall(rockchip_pinctrl_drv_register);
2297 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
2298 MODULE_DESCRIPTION("Rockchip pinctrl driver");
2299 MODULE_LICENSE("GPL v2");