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/init.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 {
72 * Encode variants of iomux registers into a type variable
74 #define IOMUX_GPIO_ONLY BIT(0)
75 #define IOMUX_WIDTH_4BIT BIT(1)
76 #define IOMUX_SOURCE_PMU BIT(2)
77 #define IOMUX_UNROUTED BIT(3)
78 #define IOMUX_WIDTH_3BIT BIT(4)
79 #define IOMUX_RECALCED BIT(5)
82 * @type: iomux variant using IOMUX_* constants
83 * @offset: if initialized to -1 it will be autocalculated, by specifying
84 * an initial offset value the relevant source offset can be reset
85 * to a new value for autocalculating the following iomux registers.
87 struct rockchip_iomux {
93 * enum type index corresponding to rockchip_perpin_drv_list arrays index.
95 enum rockchip_pin_drv_type {
96 DRV_TYPE_IO_DEFAULT = 0,
97 DRV_TYPE_IO_1V8_OR_3V0,
99 DRV_TYPE_IO_1V8_3V0_AUTO,
100 DRV_TYPE_IO_3V3_ONLY,
105 * enum type index corresponding to rockchip_pull_list arrays index.
107 enum rockchip_pin_pull_type {
108 PULL_TYPE_IO_DEFAULT = 0,
109 PULL_TYPE_IO_1V8_ONLY,
114 * @drv_type: drive strength variant using rockchip_perpin_drv_type
115 * @offset: if initialized to -1 it will be autocalculated, by specifying
116 * an initial offset value the relevant source offset can be reset
117 * to a new value for autocalculating the following drive strength
118 * registers. if used chips own cal_drv func instead to calculate
119 * registers offset, the variant could be ignored.
121 struct rockchip_drv {
122 enum rockchip_pin_drv_type drv_type;
127 * @reg_base: register base of the gpio bank
128 * @reg_pull: optional separate register for additional pull settings
129 * @clk: clock of the gpio bank
130 * @irq: interrupt of the gpio bank
131 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
132 * @pin_base: first pin number
133 * @nr_pins: number of pins in this bank
134 * @name: name of the bank
135 * @bank_num: number of the bank, to account for holes
136 * @iomux: array describing the 4 iomux sources of the bank
137 * @drv: array describing the 4 drive strength sources of the bank
138 * @pull_type: array describing the 4 pull type sources of the bank
139 * @valid: are all necessary informations present
140 * @of_node: dt node of this bank
141 * @drvdata: common pinctrl basedata
142 * @domain: irqdomain of the gpio bank
143 * @gpio_chip: gpiolib chip
144 * @grange: gpio range
145 * @slock: spinlock for the gpio bank
147 struct rockchip_pin_bank {
148 void __iomem *reg_base;
149 struct regmap *regmap_pull;
157 struct rockchip_iomux iomux[4];
158 struct rockchip_drv drv[4];
159 enum rockchip_pin_pull_type pull_type[4];
161 struct device_node *of_node;
162 struct rockchip_pinctrl *drvdata;
163 struct irq_domain *domain;
164 struct gpio_chip gpio_chip;
165 struct pinctrl_gpio_range grange;
167 u32 toggle_edge_mode;
170 #define PIN_BANK(id, pins, label) \
183 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
189 { .type = iom0, .offset = -1 }, \
190 { .type = iom1, .offset = -1 }, \
191 { .type = iom2, .offset = -1 }, \
192 { .type = iom3, .offset = -1 }, \
196 #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \
208 { .drv_type = type0, .offset = -1 }, \
209 { .drv_type = type1, .offset = -1 }, \
210 { .drv_type = type2, .offset = -1 }, \
211 { .drv_type = type3, .offset = -1 }, \
215 #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \
216 drv2, drv3, pull0, pull1, \
229 { .drv_type = drv0, .offset = -1 }, \
230 { .drv_type = drv1, .offset = -1 }, \
231 { .drv_type = drv2, .offset = -1 }, \
232 { .drv_type = drv3, .offset = -1 }, \
234 .pull_type[0] = pull0, \
235 .pull_type[1] = pull1, \
236 .pull_type[2] = pull2, \
237 .pull_type[3] = pull3, \
240 #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \
241 iom2, iom3, drv0, drv1, drv2, \
242 drv3, offset0, offset1, \
249 { .type = iom0, .offset = -1 }, \
250 { .type = iom1, .offset = -1 }, \
251 { .type = iom2, .offset = -1 }, \
252 { .type = iom3, .offset = -1 }, \
255 { .drv_type = drv0, .offset = offset0 }, \
256 { .drv_type = drv1, .offset = offset1 }, \
257 { .drv_type = drv2, .offset = offset2 }, \
258 { .drv_type = drv3, .offset = offset3 }, \
262 #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \
263 label, iom0, iom1, iom2, \
264 iom3, drv0, drv1, drv2, \
265 drv3, offset0, offset1, \
266 offset2, offset3, pull0, \
267 pull1, pull2, pull3) \
273 { .type = iom0, .offset = -1 }, \
274 { .type = iom1, .offset = -1 }, \
275 { .type = iom2, .offset = -1 }, \
276 { .type = iom3, .offset = -1 }, \
279 { .drv_type = drv0, .offset = offset0 }, \
280 { .drv_type = drv1, .offset = offset1 }, \
281 { .drv_type = drv2, .offset = offset2 }, \
282 { .drv_type = drv3, .offset = offset3 }, \
284 .pull_type[0] = pull0, \
285 .pull_type[1] = pull1, \
286 .pull_type[2] = pull2, \
287 .pull_type[3] = pull3, \
292 struct rockchip_pin_ctrl {
293 struct rockchip_pin_bank *pin_banks;
297 enum rockchip_pinctrl_type type;
303 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
304 int pin_num, struct regmap **regmap,
306 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
307 int pin_num, struct regmap **regmap,
309 void (*iomux_recalc)(u8 bank_num, int pin, int *reg,
311 int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank,
312 int pin_num, struct regmap **regmap,
316 struct rockchip_pin_config {
318 unsigned long *configs;
319 unsigned int nconfigs;
323 * struct rockchip_pin_group: represent group of pins of a pinmux function.
324 * @name: name of the pin group, used to lookup the group.
325 * @pins: the pins included in this group.
326 * @npins: number of pins included in this group.
327 * @func: the mux function number to be programmed when selected.
328 * @configs: the config values to be set for each pin
329 * @nconfigs: number of configs for each pin
331 struct rockchip_pin_group {
335 struct rockchip_pin_config *data;
339 * struct rockchip_pmx_func: represent a pin function.
340 * @name: name of the pin function, used to lookup the function.
341 * @groups: one or more names of pin groups that provide this function.
342 * @num_groups: number of groups included in @groups.
344 struct rockchip_pmx_func {
350 struct rockchip_pinctrl {
351 struct regmap *regmap_base;
353 struct regmap *regmap_pull;
354 struct regmap *regmap_pmu;
356 struct rockchip_pin_ctrl *ctrl;
357 struct pinctrl_desc pctl;
358 struct pinctrl_dev *pctl_dev;
359 struct rockchip_pin_group *groups;
360 unsigned int ngroups;
361 struct rockchip_pmx_func *functions;
362 unsigned int nfunctions;
366 * struct rockchip_mux_recalced_data: represent a pin iomux data.
369 * @bit: index at register.
370 * @reg: register offset.
373 struct rockchip_mux_recalced_data {
381 static struct regmap_config rockchip_regmap_config = {
387 static inline const struct rockchip_pin_group *pinctrl_name_to_group(
388 const struct rockchip_pinctrl *info,
393 for (i = 0; i < info->ngroups; i++) {
394 if (!strcmp(info->groups[i].name, name))
395 return &info->groups[i];
402 * given a pin number that is local to a pin controller, find out the pin bank
403 * and the register base of the pin bank.
405 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
408 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
410 while (pin >= (b->pin_base + b->nr_pins))
416 static struct rockchip_pin_bank *bank_num_to_bank(
417 struct rockchip_pinctrl *info,
420 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
423 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
424 if (b->bank_num == num)
428 return ERR_PTR(-EINVAL);
432 * Pinctrl_ops handling
435 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
437 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
439 return info->ngroups;
442 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
445 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
447 return info->groups[selector].name;
450 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
451 unsigned selector, const unsigned **pins,
454 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
456 if (selector >= info->ngroups)
459 *pins = info->groups[selector].pins;
460 *npins = info->groups[selector].npins;
465 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
466 struct device_node *np,
467 struct pinctrl_map **map, unsigned *num_maps)
469 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
470 const struct rockchip_pin_group *grp;
471 struct pinctrl_map *new_map;
472 struct device_node *parent;
477 * first find the group of this node and check if we need to create
478 * config maps for pins
480 grp = pinctrl_name_to_group(info, np->name);
482 dev_err(info->dev, "unable to find group for node %s\n",
487 map_num += grp->npins;
488 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
497 parent = of_get_parent(np);
499 devm_kfree(pctldev->dev, new_map);
502 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
503 new_map[0].data.mux.function = parent->name;
504 new_map[0].data.mux.group = np->name;
507 /* create config map */
509 for (i = 0; i < grp->npins; i++) {
510 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
511 new_map[i].data.configs.group_or_pin =
512 pin_get_name(pctldev, grp->pins[i]);
513 new_map[i].data.configs.configs = grp->data[i].configs;
514 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
517 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
518 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
523 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
524 struct pinctrl_map *map, unsigned num_maps)
528 static const struct pinctrl_ops rockchip_pctrl_ops = {
529 .get_groups_count = rockchip_get_groups_count,
530 .get_group_name = rockchip_get_group_name,
531 .get_group_pins = rockchip_get_group_pins,
532 .dt_node_to_map = rockchip_dt_node_to_map,
533 .dt_free_map = rockchip_dt_free_map,
540 static const struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
562 static void rk3328_recalc_mux(u8 bank_num, int pin, int *reg,
565 const struct rockchip_mux_recalced_data *data = NULL;
568 for (i = 0; i < ARRAY_SIZE(rk3328_mux_recalced_data); i++)
569 if (rk3328_mux_recalced_data[i].num == bank_num &&
570 rk3328_mux_recalced_data[i].pin == pin) {
571 data = &rk3328_mux_recalced_data[i];
583 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
585 struct rockchip_pinctrl *info = bank->drvdata;
586 struct rockchip_pin_ctrl *ctrl = info->ctrl;
587 int iomux_num = (pin / 8);
588 struct regmap *regmap;
590 int reg, ret, mask, mux_type;
596 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
597 dev_err(info->dev, "pin %d is unrouted\n", pin);
601 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
604 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
605 ? info->regmap_pmu : info->regmap_base;
607 /* get basic quadrupel of mux registers and the correct reg inside */
608 mux_type = bank->iomux[iomux_num].type;
609 reg = bank->iomux[iomux_num].offset;
610 if (mux_type & IOMUX_WIDTH_4BIT) {
615 } else if (mux_type & IOMUX_WIDTH_3BIT) {
618 bit = (pin % 8 % 5) * 3;
625 if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED))
626 ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask);
628 ret = regmap_read(regmap, reg, &val);
632 return ((val >> bit) & mask);
636 * Set a new mux function for a pin.
638 * The register is divided into the upper and lower 16 bit. When changing
639 * a value, the previous register value is not read and changed. Instead
640 * it seems the changed bits are marked in the upper 16 bit, while the
641 * changed value gets set in the same offset in the lower 16 bit.
642 * All pin settings seem to be 2 bit wide in both the upper and lower
644 * @bank: pin bank to change
645 * @pin: pin to change
646 * @mux: new mux function to set
648 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
650 struct rockchip_pinctrl *info = bank->drvdata;
651 struct rockchip_pin_ctrl *ctrl = info->ctrl;
652 int iomux_num = (pin / 8);
653 struct regmap *regmap;
654 int reg, ret, mask, mux_type;
662 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
663 dev_err(info->dev, "pin %d is unrouted\n", pin);
667 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
668 if (mux != RK_FUNC_GPIO) {
670 "pin %d only supports a gpio mux\n", pin);
677 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
678 bank->bank_num, pin, mux);
680 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
681 ? info->regmap_pmu : info->regmap_base;
683 /* get basic quadrupel of mux registers and the correct reg inside */
684 mux_type = bank->iomux[iomux_num].type;
685 reg = bank->iomux[iomux_num].offset;
686 if (mux_type & IOMUX_WIDTH_4BIT) {
691 } else if (mux_type & IOMUX_WIDTH_3BIT) {
694 bit = (pin % 8 % 5) * 3;
701 if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED))
702 ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask);
704 spin_lock_irqsave(&bank->slock, flags);
706 data = (mask << (bit + 16));
707 rmask = data | (data >> 16);
708 data |= (mux & mask) << bit;
709 ret = regmap_update_bits(regmap, reg, rmask, data);
711 spin_unlock_irqrestore(&bank->slock, flags);
716 #define RV1108_PULL_PMU_OFFSET 0x10
717 #define RV1108_PULL_OFFSET 0x110
718 #define RV1108_PULL_PINS_PER_REG 8
719 #define RV1108_PULL_BITS_PER_PIN 2
720 #define RV1108_PULL_BANK_STRIDE 16
722 static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
723 int pin_num, struct regmap **regmap,
726 struct rockchip_pinctrl *info = bank->drvdata;
728 /* The first 24 pins of the first bank are located in PMU */
729 if (bank->bank_num == 0) {
730 *regmap = info->regmap_pmu;
731 *reg = RV1108_PULL_PMU_OFFSET;
733 *reg = RV1108_PULL_OFFSET;
734 *regmap = info->regmap_base;
735 /* correct the offset, as we're starting with the 2nd bank */
737 *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
740 *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
741 *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
742 *bit *= RV1108_PULL_BITS_PER_PIN;
745 #define RV1108_DRV_PMU_OFFSET 0x20
746 #define RV1108_DRV_GRF_OFFSET 0x210
747 #define RV1108_DRV_BITS_PER_PIN 2
748 #define RV1108_DRV_PINS_PER_REG 8
749 #define RV1108_DRV_BANK_STRIDE 16
751 static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
752 int pin_num, struct regmap **regmap,
755 struct rockchip_pinctrl *info = bank->drvdata;
757 /* The first 24 pins of the first bank are located in PMU */
758 if (bank->bank_num == 0) {
759 *regmap = info->regmap_pmu;
760 *reg = RV1108_DRV_PMU_OFFSET;
762 *regmap = info->regmap_base;
763 *reg = RV1108_DRV_GRF_OFFSET;
765 /* correct the offset, as we're starting with the 2nd bank */
767 *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
770 *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
771 *bit = pin_num % RV1108_DRV_PINS_PER_REG;
772 *bit *= RV1108_DRV_BITS_PER_PIN;
775 #define RK2928_PULL_OFFSET 0x118
776 #define RK2928_PULL_PINS_PER_REG 16
777 #define RK2928_PULL_BANK_STRIDE 8
779 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
780 int pin_num, struct regmap **regmap,
783 struct rockchip_pinctrl *info = bank->drvdata;
785 *regmap = info->regmap_base;
786 *reg = RK2928_PULL_OFFSET;
787 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
788 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
790 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
793 #define RK3188_PULL_OFFSET 0x164
794 #define RK3188_PULL_BITS_PER_PIN 2
795 #define RK3188_PULL_PINS_PER_REG 8
796 #define RK3188_PULL_BANK_STRIDE 16
797 #define RK3188_PULL_PMU_OFFSET 0x64
799 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
800 int pin_num, struct regmap **regmap,
803 struct rockchip_pinctrl *info = bank->drvdata;
805 /* The first 12 pins of the first bank are located elsewhere */
806 if (bank->bank_num == 0 && pin_num < 12) {
807 *regmap = info->regmap_pmu ? info->regmap_pmu
809 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
810 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
811 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
812 *bit *= RK3188_PULL_BITS_PER_PIN;
814 *regmap = info->regmap_pull ? info->regmap_pull
816 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
818 /* correct the offset, as it is the 2nd pull register */
820 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
821 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
824 * The bits in these registers have an inverse ordering
825 * with the lowest pin being in bits 15:14 and the highest
828 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
829 *bit *= RK3188_PULL_BITS_PER_PIN;
833 #define RK3288_PULL_OFFSET 0x140
834 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
835 int pin_num, struct regmap **regmap,
838 struct rockchip_pinctrl *info = bank->drvdata;
840 /* The first 24 pins of the first bank are located in PMU */
841 if (bank->bank_num == 0) {
842 *regmap = info->regmap_pmu;
843 *reg = RK3188_PULL_PMU_OFFSET;
845 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
846 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
847 *bit *= RK3188_PULL_BITS_PER_PIN;
849 *regmap = info->regmap_base;
850 *reg = RK3288_PULL_OFFSET;
852 /* correct the offset, as we're starting with the 2nd bank */
854 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
855 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
857 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
858 *bit *= RK3188_PULL_BITS_PER_PIN;
862 #define RK3288_DRV_PMU_OFFSET 0x70
863 #define RK3288_DRV_GRF_OFFSET 0x1c0
864 #define RK3288_DRV_BITS_PER_PIN 2
865 #define RK3288_DRV_PINS_PER_REG 8
866 #define RK3288_DRV_BANK_STRIDE 16
868 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
869 int pin_num, struct regmap **regmap,
872 struct rockchip_pinctrl *info = bank->drvdata;
874 /* The first 24 pins of the first bank are located in PMU */
875 if (bank->bank_num == 0) {
876 *regmap = info->regmap_pmu;
877 *reg = RK3288_DRV_PMU_OFFSET;
879 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
880 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
881 *bit *= RK3288_DRV_BITS_PER_PIN;
883 *regmap = info->regmap_base;
884 *reg = RK3288_DRV_GRF_OFFSET;
886 /* correct the offset, as we're starting with the 2nd bank */
888 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
889 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
891 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
892 *bit *= RK3288_DRV_BITS_PER_PIN;
896 #define RK3228_PULL_OFFSET 0x100
898 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
899 int pin_num, struct regmap **regmap,
902 struct rockchip_pinctrl *info = bank->drvdata;
904 *regmap = info->regmap_base;
905 *reg = RK3228_PULL_OFFSET;
906 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
907 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
909 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
910 *bit *= RK3188_PULL_BITS_PER_PIN;
913 #define RK3228_DRV_GRF_OFFSET 0x200
915 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
916 int pin_num, struct regmap **regmap,
919 struct rockchip_pinctrl *info = bank->drvdata;
921 *regmap = info->regmap_base;
922 *reg = RK3228_DRV_GRF_OFFSET;
923 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
924 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
926 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
927 *bit *= RK3288_DRV_BITS_PER_PIN;
930 #define RK3368_PULL_GRF_OFFSET 0x100
931 #define RK3368_PULL_PMU_OFFSET 0x10
933 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
934 int pin_num, struct regmap **regmap,
937 struct rockchip_pinctrl *info = bank->drvdata;
939 /* The first 32 pins of the first bank are located in PMU */
940 if (bank->bank_num == 0) {
941 *regmap = info->regmap_pmu;
942 *reg = RK3368_PULL_PMU_OFFSET;
944 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
945 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
946 *bit *= RK3188_PULL_BITS_PER_PIN;
948 *regmap = info->regmap_base;
949 *reg = RK3368_PULL_GRF_OFFSET;
951 /* correct the offset, as we're starting with the 2nd bank */
953 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
954 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
956 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
957 *bit *= RK3188_PULL_BITS_PER_PIN;
961 #define RK3368_DRV_PMU_OFFSET 0x20
962 #define RK3368_DRV_GRF_OFFSET 0x200
964 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
965 int pin_num, struct regmap **regmap,
968 struct rockchip_pinctrl *info = bank->drvdata;
970 /* The first 32 pins of the first bank are located in PMU */
971 if (bank->bank_num == 0) {
972 *regmap = info->regmap_pmu;
973 *reg = RK3368_DRV_PMU_OFFSET;
975 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
976 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
977 *bit *= RK3288_DRV_BITS_PER_PIN;
979 *regmap = info->regmap_base;
980 *reg = RK3368_DRV_GRF_OFFSET;
982 /* correct the offset, as we're starting with the 2nd bank */
984 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
985 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
987 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
988 *bit *= RK3288_DRV_BITS_PER_PIN;
992 #define RK3399_PULL_GRF_OFFSET 0xe040
993 #define RK3399_PULL_PMU_OFFSET 0x40
994 #define RK3399_DRV_3BITS_PER_PIN 3
996 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
997 int pin_num, struct regmap **regmap,
1000 struct rockchip_pinctrl *info = bank->drvdata;
1002 /* The bank0:16 and bank1:32 pins are located in PMU */
1003 if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
1004 *regmap = info->regmap_pmu;
1005 *reg = RK3399_PULL_PMU_OFFSET;
1007 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1009 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1010 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1011 *bit *= RK3188_PULL_BITS_PER_PIN;
1013 *regmap = info->regmap_base;
1014 *reg = RK3399_PULL_GRF_OFFSET;
1016 /* correct the offset, as we're starting with the 3rd bank */
1018 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1019 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1021 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1022 *bit *= RK3188_PULL_BITS_PER_PIN;
1026 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1027 int pin_num, struct regmap **regmap,
1030 struct rockchip_pinctrl *info = bank->drvdata;
1031 int drv_num = (pin_num / 8);
1033 /* The bank0:16 and bank1:32 pins are located in PMU */
1034 if ((bank->bank_num == 0) || (bank->bank_num == 1))
1035 *regmap = info->regmap_pmu;
1037 *regmap = info->regmap_base;
1039 *reg = bank->drv[drv_num].offset;
1040 if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
1041 (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
1042 *bit = (pin_num % 8) * 3;
1044 *bit = (pin_num % 8) * 2;
1047 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
1048 { 2, 4, 8, 12, -1, -1, -1, -1 },
1049 { 3, 6, 9, 12, -1, -1, -1, -1 },
1050 { 5, 10, 15, 20, -1, -1, -1, -1 },
1051 { 4, 6, 8, 10, 12, 14, 16, 18 },
1052 { 4, 7, 10, 13, 16, 19, 22, 26 }
1055 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
1058 struct rockchip_pinctrl *info = bank->drvdata;
1059 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1060 struct regmap *regmap;
1062 u32 data, temp, rmask_bits;
1064 int drv_type = bank->drv[pin_num / 8].drv_type;
1066 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1069 case DRV_TYPE_IO_1V8_3V0_AUTO:
1070 case DRV_TYPE_IO_3V3_ONLY:
1071 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1074 /* regular case, nothing to do */
1078 * drive-strength offset is special, as it is
1079 * spread over 2 registers
1081 ret = regmap_read(regmap, reg, &data);
1085 ret = regmap_read(regmap, reg + 0x4, &temp);
1090 * the bit data[15] contains bit 0 of the value
1091 * while temp[1:0] contains bits 2 and 1
1098 return rockchip_perpin_drv_list[drv_type][data];
1100 /* setting fully enclosed in the second register */
1105 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1111 case DRV_TYPE_IO_DEFAULT:
1112 case DRV_TYPE_IO_1V8_OR_3V0:
1113 case DRV_TYPE_IO_1V8_ONLY:
1114 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1117 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1122 ret = regmap_read(regmap, reg, &data);
1127 data &= (1 << rmask_bits) - 1;
1129 return rockchip_perpin_drv_list[drv_type][data];
1132 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
1133 int pin_num, int strength)
1135 struct rockchip_pinctrl *info = bank->drvdata;
1136 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1137 struct regmap *regmap;
1138 unsigned long flags;
1140 u32 data, rmask, rmask_bits, temp;
1142 int drv_type = bank->drv[pin_num / 8].drv_type;
1144 dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
1145 bank->bank_num, pin_num, strength);
1147 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1150 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
1151 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
1154 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
1155 ret = rockchip_perpin_drv_list[drv_type][i];
1161 dev_err(info->dev, "unsupported driver strength %d\n",
1166 spin_lock_irqsave(&bank->slock, flags);
1169 case DRV_TYPE_IO_1V8_3V0_AUTO:
1170 case DRV_TYPE_IO_3V3_ONLY:
1171 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1174 /* regular case, nothing to do */
1178 * drive-strength offset is special, as it is spread
1179 * over 2 registers, the bit data[15] contains bit 0
1180 * of the value while temp[1:0] contains bits 2 and 1
1182 data = (ret & 0x1) << 15;
1183 temp = (ret >> 0x1) & 0x3;
1185 rmask = BIT(15) | BIT(31);
1187 ret = regmap_update_bits(regmap, reg, rmask, data);
1189 spin_unlock_irqrestore(&bank->slock, flags);
1193 rmask = 0x3 | (0x3 << 16);
1194 temp |= (0x3 << 16);
1196 ret = regmap_update_bits(regmap, reg, rmask, temp);
1198 spin_unlock_irqrestore(&bank->slock, flags);
1201 /* setting fully enclosed in the second register */
1206 spin_unlock_irqrestore(&bank->slock, flags);
1207 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1212 case DRV_TYPE_IO_DEFAULT:
1213 case DRV_TYPE_IO_1V8_OR_3V0:
1214 case DRV_TYPE_IO_1V8_ONLY:
1215 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1218 spin_unlock_irqrestore(&bank->slock, flags);
1219 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1224 /* enable the write to the equivalent lower bits */
1225 data = ((1 << rmask_bits) - 1) << (bit + 16);
1226 rmask = data | (data >> 16);
1227 data |= (ret << bit);
1229 ret = regmap_update_bits(regmap, reg, rmask, data);
1230 spin_unlock_irqrestore(&bank->slock, flags);
1235 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
1237 PIN_CONFIG_BIAS_DISABLE,
1238 PIN_CONFIG_BIAS_PULL_UP,
1239 PIN_CONFIG_BIAS_PULL_DOWN,
1240 PIN_CONFIG_BIAS_BUS_HOLD
1243 PIN_CONFIG_BIAS_DISABLE,
1244 PIN_CONFIG_BIAS_PULL_DOWN,
1245 PIN_CONFIG_BIAS_DISABLE,
1246 PIN_CONFIG_BIAS_PULL_UP
1250 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
1252 struct rockchip_pinctrl *info = bank->drvdata;
1253 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1254 struct regmap *regmap;
1255 int reg, ret, pull_type;
1259 /* rk3066b does support any pulls */
1260 if (ctrl->type == RK3066B)
1261 return PIN_CONFIG_BIAS_DISABLE;
1263 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1265 ret = regmap_read(regmap, reg, &data);
1269 switch (ctrl->type) {
1271 return !(data & BIT(bit))
1272 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
1273 : PIN_CONFIG_BIAS_DISABLE;
1279 pull_type = bank->pull_type[pin_num / 8];
1281 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
1283 return rockchip_pull_list[pull_type][data];
1285 dev_err(info->dev, "unsupported pinctrl type\n");
1290 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
1291 int pin_num, int pull)
1293 struct rockchip_pinctrl *info = bank->drvdata;
1294 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1295 struct regmap *regmap;
1296 int reg, ret, i, pull_type;
1297 unsigned long flags;
1301 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
1302 bank->bank_num, pin_num, pull);
1304 /* rk3066b does support any pulls */
1305 if (ctrl->type == RK3066B)
1306 return pull ? -EINVAL : 0;
1308 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1310 switch (ctrl->type) {
1312 spin_lock_irqsave(&bank->slock, flags);
1314 data = BIT(bit + 16);
1315 if (pull == PIN_CONFIG_BIAS_DISABLE)
1317 ret = regmap_write(regmap, reg, data);
1319 spin_unlock_irqrestore(&bank->slock, flags);
1326 pull_type = bank->pull_type[pin_num / 8];
1328 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
1330 if (rockchip_pull_list[pull_type][i] == pull) {
1337 dev_err(info->dev, "unsupported pull setting %d\n",
1342 spin_lock_irqsave(&bank->slock, flags);
1344 /* enable the write to the equivalent lower bits */
1345 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
1346 rmask = data | (data >> 16);
1347 data |= (ret << bit);
1349 ret = regmap_update_bits(regmap, reg, rmask, data);
1351 spin_unlock_irqrestore(&bank->slock, flags);
1354 dev_err(info->dev, "unsupported pinctrl type\n");
1361 #define RK3328_SCHMITT_BITS_PER_PIN 1
1362 #define RK3328_SCHMITT_PINS_PER_REG 16
1363 #define RK3328_SCHMITT_BANK_STRIDE 8
1364 #define RK3328_SCHMITT_GRF_OFFSET 0x380
1366 static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1368 struct regmap **regmap,
1371 struct rockchip_pinctrl *info = bank->drvdata;
1373 *regmap = info->regmap_base;
1374 *reg = RK3328_SCHMITT_GRF_OFFSET;
1376 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
1377 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
1378 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
1383 static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
1385 struct rockchip_pinctrl *info = bank->drvdata;
1386 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1387 struct regmap *regmap;
1392 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
1396 ret = regmap_read(regmap, reg, &data);
1404 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
1405 int pin_num, int enable)
1407 struct rockchip_pinctrl *info = bank->drvdata;
1408 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1409 struct regmap *regmap;
1411 unsigned long flags;
1415 dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
1416 bank->bank_num, pin_num, enable);
1418 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
1422 spin_lock_irqsave(&bank->slock, flags);
1424 /* enable the write to the equivalent lower bits */
1425 data = BIT(bit + 16) | (enable << bit);
1426 rmask = BIT(bit + 16) | BIT(bit);
1428 ret = regmap_update_bits(regmap, reg, rmask, data);
1429 spin_unlock_irqrestore(&bank->slock, flags);
1435 * Pinmux_ops handling
1438 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
1440 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1442 return info->nfunctions;
1445 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
1448 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1450 return info->functions[selector].name;
1453 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
1454 unsigned selector, const char * const **groups,
1455 unsigned * const num_groups)
1457 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1459 *groups = info->functions[selector].groups;
1460 *num_groups = info->functions[selector].ngroups;
1465 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
1468 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1469 const unsigned int *pins = info->groups[group].pins;
1470 const struct rockchip_pin_config *data = info->groups[group].data;
1471 struct rockchip_pin_bank *bank;
1474 dev_dbg(info->dev, "enable function %s group %s\n",
1475 info->functions[selector].name, info->groups[group].name);
1478 * for each pin in the pin group selected, program the correspoding pin
1479 * pin function number in the config register.
1481 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
1482 bank = pin_to_bank(info, pins[cnt]);
1483 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
1490 /* revert the already done pin settings */
1491 for (cnt--; cnt >= 0; cnt--)
1492 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
1500 static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1502 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
1505 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1507 return !(data & BIT(offset));
1511 * The calls to gpio_direction_output() and gpio_direction_input()
1512 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
1513 * function called from the gpiolib interface).
1515 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
1516 int pin, bool input)
1518 struct rockchip_pin_bank *bank;
1520 unsigned long flags;
1523 bank = gpiochip_get_data(chip);
1525 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
1529 clk_enable(bank->clk);
1530 spin_lock_irqsave(&bank->slock, flags);
1532 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1533 /* set bit to 1 for output, 0 for input */
1538 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
1540 spin_unlock_irqrestore(&bank->slock, flags);
1541 clk_disable(bank->clk);
1546 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
1547 struct pinctrl_gpio_range *range,
1548 unsigned offset, bool input)
1550 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1551 struct gpio_chip *chip;
1555 pin = offset - chip->base;
1556 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
1557 offset, range->name, pin, input ? "input" : "output");
1559 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
1563 static const struct pinmux_ops rockchip_pmx_ops = {
1564 .get_functions_count = rockchip_pmx_get_funcs_count,
1565 .get_function_name = rockchip_pmx_get_func_name,
1566 .get_function_groups = rockchip_pmx_get_groups,
1567 .set_mux = rockchip_pmx_set,
1568 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
1572 * Pinconf_ops handling
1575 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
1576 enum pin_config_param pull)
1578 switch (ctrl->type) {
1580 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
1581 pull == PIN_CONFIG_BIAS_DISABLE);
1583 return pull ? false : true;
1589 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
1595 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
1596 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
1598 /* set the pin config settings for a specified pin */
1599 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1600 unsigned long *configs, unsigned num_configs)
1602 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1603 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1604 enum pin_config_param param;
1609 for (i = 0; i < num_configs; i++) {
1610 param = pinconf_to_config_param(configs[i]);
1611 arg = pinconf_to_config_argument(configs[i]);
1614 case PIN_CONFIG_BIAS_DISABLE:
1615 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1620 case PIN_CONFIG_BIAS_PULL_UP:
1621 case PIN_CONFIG_BIAS_PULL_DOWN:
1622 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1623 case PIN_CONFIG_BIAS_BUS_HOLD:
1624 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1630 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1635 case PIN_CONFIG_OUTPUT:
1636 rockchip_gpio_set(&bank->gpio_chip,
1637 pin - bank->pin_base, arg);
1638 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
1639 pin - bank->pin_base, false);
1643 case PIN_CONFIG_DRIVE_STRENGTH:
1644 /* rk3288 is the first with per-pin drive-strength */
1645 if (!info->ctrl->drv_calc_reg)
1648 rc = rockchip_set_drive_perpin(bank,
1649 pin - bank->pin_base, arg);
1653 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1654 if (!info->ctrl->schmitt_calc_reg)
1657 rc = rockchip_set_schmitt(bank,
1658 pin - bank->pin_base, arg);
1666 } /* for each config */
1671 /* get the pin config settings for a specified pin */
1672 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1673 unsigned long *config)
1675 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1676 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1677 enum pin_config_param param = pinconf_to_config_param(*config);
1682 case PIN_CONFIG_BIAS_DISABLE:
1683 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1688 case PIN_CONFIG_BIAS_PULL_UP:
1689 case PIN_CONFIG_BIAS_PULL_DOWN:
1690 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1691 case PIN_CONFIG_BIAS_BUS_HOLD:
1692 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1695 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1700 case PIN_CONFIG_OUTPUT:
1701 rc = rockchip_get_mux(bank, pin - bank->pin_base);
1702 if (rc != RK_FUNC_GPIO)
1705 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
1711 case PIN_CONFIG_DRIVE_STRENGTH:
1712 /* rk3288 is the first with per-pin drive-strength */
1713 if (!info->ctrl->drv_calc_reg)
1716 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
1722 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1723 if (!info->ctrl->schmitt_calc_reg)
1726 rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
1737 *config = pinconf_to_config_packed(param, arg);
1742 static const struct pinconf_ops rockchip_pinconf_ops = {
1743 .pin_config_get = rockchip_pinconf_get,
1744 .pin_config_set = rockchip_pinconf_set,
1748 static const struct of_device_id rockchip_bank_match[] = {
1749 { .compatible = "rockchip,gpio-bank" },
1750 { .compatible = "rockchip,rk3188-gpio-bank0" },
1754 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
1755 struct device_node *np)
1757 struct device_node *child;
1759 for_each_child_of_node(np, child) {
1760 if (of_match_node(rockchip_bank_match, child))
1764 info->ngroups += of_get_child_count(child);
1768 static int rockchip_pinctrl_parse_groups(struct device_node *np,
1769 struct rockchip_pin_group *grp,
1770 struct rockchip_pinctrl *info,
1773 struct rockchip_pin_bank *bank;
1780 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1782 /* Initialise group */
1783 grp->name = np->name;
1786 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1787 * do sanity check and calculate pins number
1789 list = of_get_property(np, "rockchip,pins", &size);
1790 /* we do not check return since it's safe node passed down */
1791 size /= sizeof(*list);
1792 if (!size || size % 4) {
1793 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1797 grp->npins = size / 4;
1799 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1801 grp->data = devm_kzalloc(info->dev, grp->npins *
1802 sizeof(struct rockchip_pin_config),
1804 if (!grp->pins || !grp->data)
1807 for (i = 0, j = 0; i < size; i += 4, j++) {
1808 const __be32 *phandle;
1809 struct device_node *np_config;
1811 num = be32_to_cpu(*list++);
1812 bank = bank_num_to_bank(info, num);
1814 return PTR_ERR(bank);
1816 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
1817 grp->data[j].func = be32_to_cpu(*list++);
1823 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
1824 ret = pinconf_generic_parse_dt_config(np_config, NULL,
1825 &grp->data[j].configs, &grp->data[j].nconfigs);
1833 static int rockchip_pinctrl_parse_functions(struct device_node *np,
1834 struct rockchip_pinctrl *info,
1837 struct device_node *child;
1838 struct rockchip_pmx_func *func;
1839 struct rockchip_pin_group *grp;
1841 static u32 grp_index;
1844 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1846 func = &info->functions[index];
1848 /* Initialise function */
1849 func->name = np->name;
1850 func->ngroups = of_get_child_count(np);
1851 if (func->ngroups <= 0)
1854 func->groups = devm_kzalloc(info->dev,
1855 func->ngroups * sizeof(char *), GFP_KERNEL);
1859 for_each_child_of_node(np, child) {
1860 func->groups[i] = child->name;
1861 grp = &info->groups[grp_index++];
1862 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
1872 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
1873 struct rockchip_pinctrl *info)
1875 struct device *dev = &pdev->dev;
1876 struct device_node *np = dev->of_node;
1877 struct device_node *child;
1881 rockchip_pinctrl_child_count(info, np);
1883 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1884 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1886 info->functions = devm_kzalloc(dev, info->nfunctions *
1887 sizeof(struct rockchip_pmx_func),
1889 if (!info->functions) {
1890 dev_err(dev, "failed to allocate memory for function list\n");
1894 info->groups = devm_kzalloc(dev, info->ngroups *
1895 sizeof(struct rockchip_pin_group),
1897 if (!info->groups) {
1898 dev_err(dev, "failed allocate memory for ping group list\n");
1904 for_each_child_of_node(np, child) {
1905 if (of_match_node(rockchip_bank_match, child))
1908 ret = rockchip_pinctrl_parse_functions(child, info, i++);
1910 dev_err(&pdev->dev, "failed to parse function\n");
1919 static int rockchip_pinctrl_register(struct platform_device *pdev,
1920 struct rockchip_pinctrl *info)
1922 struct pinctrl_desc *ctrldesc = &info->pctl;
1923 struct pinctrl_pin_desc *pindesc, *pdesc;
1924 struct rockchip_pin_bank *pin_bank;
1928 ctrldesc->name = "rockchip-pinctrl";
1929 ctrldesc->owner = THIS_MODULE;
1930 ctrldesc->pctlops = &rockchip_pctrl_ops;
1931 ctrldesc->pmxops = &rockchip_pmx_ops;
1932 ctrldesc->confops = &rockchip_pinconf_ops;
1934 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
1935 info->ctrl->nr_pins, GFP_KERNEL);
1937 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
1940 ctrldesc->pins = pindesc;
1941 ctrldesc->npins = info->ctrl->nr_pins;
1944 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
1945 pin_bank = &info->ctrl->pin_banks[bank];
1946 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
1948 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
1949 pin_bank->name, pin);
1954 ret = rockchip_pinctrl_parse_dt(pdev, info);
1958 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
1959 if (IS_ERR(info->pctl_dev)) {
1960 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1961 return PTR_ERR(info->pctl_dev);
1964 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
1965 pin_bank = &info->ctrl->pin_banks[bank];
1966 pin_bank->grange.name = pin_bank->name;
1967 pin_bank->grange.id = bank;
1968 pin_bank->grange.pin_base = pin_bank->pin_base;
1969 pin_bank->grange.base = pin_bank->gpio_chip.base;
1970 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
1971 pin_bank->grange.gc = &pin_bank->gpio_chip;
1972 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
1982 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
1984 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1985 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
1986 unsigned long flags;
1989 clk_enable(bank->clk);
1990 spin_lock_irqsave(&bank->slock, flags);
1993 data &= ~BIT(offset);
1995 data |= BIT(offset);
1998 spin_unlock_irqrestore(&bank->slock, flags);
1999 clk_disable(bank->clk);
2003 * Returns the level of the pin for input direction and setting of the DR
2004 * register for output gpios.
2006 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
2008 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2011 clk_enable(bank->clk);
2012 data = readl(bank->reg_base + GPIO_EXT_PORT);
2013 clk_disable(bank->clk);
2020 * gpiolib gpio_direction_input callback function. The setting of the pin
2021 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
2024 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
2026 return pinctrl_gpio_direction_input(gc->base + offset);
2030 * gpiolib gpio_direction_output callback function. The setting of the pin
2031 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
2034 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
2035 unsigned offset, int value)
2037 rockchip_gpio_set(gc, offset, value);
2038 return pinctrl_gpio_direction_output(gc->base + offset);
2042 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
2043 * and a virtual IRQ, if not already present.
2045 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
2047 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2053 virq = irq_create_mapping(bank->domain, offset);
2055 return (virq) ? : -ENXIO;
2058 static const struct gpio_chip rockchip_gpiolib_chip = {
2059 .request = gpiochip_generic_request,
2060 .free = gpiochip_generic_free,
2061 .set = rockchip_gpio_set,
2062 .get = rockchip_gpio_get,
2063 .get_direction = rockchip_gpio_get_direction,
2064 .direction_input = rockchip_gpio_direction_input,
2065 .direction_output = rockchip_gpio_direction_output,
2066 .to_irq = rockchip_gpio_to_irq,
2067 .owner = THIS_MODULE,
2071 * Interrupt handling
2074 static void rockchip_irq_demux(struct irq_desc *desc)
2076 struct irq_chip *chip = irq_desc_get_chip(desc);
2077 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
2080 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
2082 chained_irq_enter(chip, desc);
2084 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
2087 unsigned int irq, virq;
2091 virq = irq_linear_revmap(bank->domain, irq);
2094 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
2098 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
2101 * Triggering IRQ on both rising and falling edge
2102 * needs manual intervention.
2104 if (bank->toggle_edge_mode & BIT(irq)) {
2105 u32 data, data_old, polarity;
2106 unsigned long flags;
2108 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
2110 spin_lock_irqsave(&bank->slock, flags);
2112 polarity = readl_relaxed(bank->reg_base +
2114 if (data & BIT(irq))
2115 polarity &= ~BIT(irq);
2117 polarity |= BIT(irq);
2119 bank->reg_base + GPIO_INT_POLARITY);
2121 spin_unlock_irqrestore(&bank->slock, flags);
2124 data = readl_relaxed(bank->reg_base +
2126 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
2129 generic_handle_irq(virq);
2132 chained_irq_exit(chip, desc);
2135 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
2137 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2138 struct rockchip_pin_bank *bank = gc->private;
2139 u32 mask = BIT(d->hwirq);
2143 unsigned long flags;
2146 /* make sure the pin is configured as gpio input */
2147 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
2151 clk_enable(bank->clk);
2152 spin_lock_irqsave(&bank->slock, flags);
2154 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2156 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2158 spin_unlock_irqrestore(&bank->slock, flags);
2160 if (type & IRQ_TYPE_EDGE_BOTH)
2161 irq_set_handler_locked(d, handle_edge_irq);
2163 irq_set_handler_locked(d, handle_level_irq);
2165 spin_lock_irqsave(&bank->slock, flags);
2168 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
2169 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
2172 case IRQ_TYPE_EDGE_BOTH:
2173 bank->toggle_edge_mode |= mask;
2177 * Determine gpio state. If 1 next interrupt should be falling
2180 data = readl(bank->reg_base + GPIO_EXT_PORT);
2186 case IRQ_TYPE_EDGE_RISING:
2187 bank->toggle_edge_mode &= ~mask;
2191 case IRQ_TYPE_EDGE_FALLING:
2192 bank->toggle_edge_mode &= ~mask;
2196 case IRQ_TYPE_LEVEL_HIGH:
2197 bank->toggle_edge_mode &= ~mask;
2201 case IRQ_TYPE_LEVEL_LOW:
2202 bank->toggle_edge_mode &= ~mask;
2208 spin_unlock_irqrestore(&bank->slock, flags);
2209 clk_disable(bank->clk);
2213 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
2214 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
2217 spin_unlock_irqrestore(&bank->slock, flags);
2218 clk_disable(bank->clk);
2223 static void rockchip_irq_suspend(struct irq_data *d)
2225 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2226 struct rockchip_pin_bank *bank = gc->private;
2228 clk_enable(bank->clk);
2229 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
2230 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
2231 clk_disable(bank->clk);
2234 static void rockchip_irq_resume(struct irq_data *d)
2236 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2237 struct rockchip_pin_bank *bank = gc->private;
2239 clk_enable(bank->clk);
2240 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
2241 clk_disable(bank->clk);
2244 static void rockchip_irq_enable(struct irq_data *d)
2246 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2247 struct rockchip_pin_bank *bank = gc->private;
2249 clk_enable(bank->clk);
2250 irq_gc_mask_clr_bit(d);
2253 static void rockchip_irq_disable(struct irq_data *d)
2255 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2256 struct rockchip_pin_bank *bank = gc->private;
2258 irq_gc_mask_set_bit(d);
2259 clk_disable(bank->clk);
2262 static int rockchip_interrupts_register(struct platform_device *pdev,
2263 struct rockchip_pinctrl *info)
2265 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2266 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2267 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
2268 struct irq_chip_generic *gc;
2272 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2274 dev_warn(&pdev->dev, "bank %s is not valid\n",
2279 ret = clk_enable(bank->clk);
2281 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
2286 bank->domain = irq_domain_add_linear(bank->of_node, 32,
2287 &irq_generic_chip_ops, NULL);
2288 if (!bank->domain) {
2289 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
2291 clk_disable(bank->clk);
2295 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
2296 "rockchip_gpio_irq", handle_level_irq,
2297 clr, 0, IRQ_GC_INIT_MASK_CACHE);
2299 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
2301 irq_domain_remove(bank->domain);
2302 clk_disable(bank->clk);
2307 * Linux assumes that all interrupts start out disabled/masked.
2308 * Our driver only uses the concept of masked and always keeps
2309 * things enabled, so for us that's all masked and all enabled.
2311 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
2312 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
2314 gc = irq_get_domain_generic_chip(bank->domain, 0);
2315 gc->reg_base = bank->reg_base;
2317 gc->chip_types[0].regs.mask = GPIO_INTMASK;
2318 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
2319 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
2320 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
2321 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
2322 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
2323 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
2324 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
2325 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
2326 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
2327 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
2328 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
2330 irq_set_chained_handler_and_data(bank->irq,
2331 rockchip_irq_demux, bank);
2333 /* map the gpio irqs here, when the clock is still running */
2334 for (j = 0 ; j < 32 ; j++)
2335 irq_create_mapping(bank->domain, j);
2337 clk_disable(bank->clk);
2343 static int rockchip_gpiolib_register(struct platform_device *pdev,
2344 struct rockchip_pinctrl *info)
2346 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2347 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2348 struct gpio_chip *gc;
2352 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2354 dev_warn(&pdev->dev, "bank %s is not valid\n",
2359 bank->gpio_chip = rockchip_gpiolib_chip;
2361 gc = &bank->gpio_chip;
2362 gc->base = bank->pin_base;
2363 gc->ngpio = bank->nr_pins;
2364 gc->parent = &pdev->dev;
2365 gc->of_node = bank->of_node;
2366 gc->label = bank->name;
2368 ret = gpiochip_add_data(gc, bank);
2370 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
2376 rockchip_interrupts_register(pdev, info);
2381 for (--i, --bank; i >= 0; --i, --bank) {
2384 gpiochip_remove(&bank->gpio_chip);
2389 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
2390 struct rockchip_pinctrl *info)
2392 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2393 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2396 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2399 gpiochip_remove(&bank->gpio_chip);
2405 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
2406 struct rockchip_pinctrl *info)
2408 struct resource res;
2411 if (of_address_to_resource(bank->of_node, 0, &res)) {
2412 dev_err(info->dev, "cannot find IO resource for bank\n");
2416 bank->reg_base = devm_ioremap_resource(info->dev, &res);
2417 if (IS_ERR(bank->reg_base))
2418 return PTR_ERR(bank->reg_base);
2421 * special case, where parts of the pull setting-registers are
2422 * part of the PMU register space
2424 if (of_device_is_compatible(bank->of_node,
2425 "rockchip,rk3188-gpio-bank0")) {
2426 struct device_node *node;
2428 node = of_parse_phandle(bank->of_node->parent,
2431 if (of_address_to_resource(bank->of_node, 1, &res)) {
2432 dev_err(info->dev, "cannot find IO resource for bank\n");
2436 base = devm_ioremap_resource(info->dev, &res);
2438 return PTR_ERR(base);
2439 rockchip_regmap_config.max_register =
2440 resource_size(&res) - 4;
2441 rockchip_regmap_config.name =
2442 "rockchip,rk3188-gpio-bank0-pull";
2443 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
2445 &rockchip_regmap_config);
2449 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
2451 bank->clk = of_clk_get(bank->of_node, 0);
2452 if (IS_ERR(bank->clk))
2453 return PTR_ERR(bank->clk);
2455 return clk_prepare(bank->clk);
2458 static const struct of_device_id rockchip_pinctrl_dt_match[];
2460 /* retrieve the soc specific data */
2461 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
2462 struct rockchip_pinctrl *d,
2463 struct platform_device *pdev)
2465 const struct of_device_id *match;
2466 struct device_node *node = pdev->dev.of_node;
2467 struct device_node *np;
2468 struct rockchip_pin_ctrl *ctrl;
2469 struct rockchip_pin_bank *bank;
2470 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
2472 match = of_match_node(rockchip_pinctrl_dt_match, node);
2473 ctrl = (struct rockchip_pin_ctrl *)match->data;
2475 for_each_child_of_node(node, np) {
2476 if (!of_find_property(np, "gpio-controller", NULL))
2479 bank = ctrl->pin_banks;
2480 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2481 if (!strcmp(bank->name, np->name)) {
2484 if (!rockchip_get_bank_data(bank, d))
2492 grf_offs = ctrl->grf_mux_offset;
2493 pmu_offs = ctrl->pmu_mux_offset;
2494 drv_pmu_offs = ctrl->pmu_drv_offset;
2495 drv_grf_offs = ctrl->grf_drv_offset;
2496 bank = ctrl->pin_banks;
2497 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2500 spin_lock_init(&bank->slock);
2502 bank->pin_base = ctrl->nr_pins;
2503 ctrl->nr_pins += bank->nr_pins;
2505 /* calculate iomux and drv offsets */
2506 for (j = 0; j < 4; j++) {
2507 struct rockchip_iomux *iom = &bank->iomux[j];
2508 struct rockchip_drv *drv = &bank->drv[j];
2511 if (bank_pins >= bank->nr_pins)
2514 /* preset iomux offset value, set new start value */
2515 if (iom->offset >= 0) {
2516 if (iom->type & IOMUX_SOURCE_PMU)
2517 pmu_offs = iom->offset;
2519 grf_offs = iom->offset;
2520 } else { /* set current iomux offset */
2521 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
2522 pmu_offs : grf_offs;
2525 /* preset drv offset value, set new start value */
2526 if (drv->offset >= 0) {
2527 if (iom->type & IOMUX_SOURCE_PMU)
2528 drv_pmu_offs = drv->offset;
2530 drv_grf_offs = drv->offset;
2531 } else { /* set current drv offset */
2532 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
2533 drv_pmu_offs : drv_grf_offs;
2536 dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
2537 i, j, iom->offset, drv->offset);
2540 * Increase offset according to iomux width.
2541 * 4bit iomux'es are spread over two registers.
2543 inc = (iom->type & (IOMUX_WIDTH_4BIT |
2544 IOMUX_WIDTH_3BIT)) ? 8 : 4;
2545 if (iom->type & IOMUX_SOURCE_PMU)
2551 * Increase offset according to drv width.
2552 * 3bit drive-strenth'es are spread over two registers.
2554 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
2555 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
2560 if (iom->type & IOMUX_SOURCE_PMU)
2561 drv_pmu_offs += inc;
2563 drv_grf_offs += inc;
2572 #define RK3288_GRF_GPIO6C_IOMUX 0x64
2573 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
2575 static u32 rk3288_grf_gpio6c_iomux;
2577 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
2579 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
2580 int ret = pinctrl_force_sleep(info->pctl_dev);
2586 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
2587 * the setting here, and restore it at resume.
2589 if (info->ctrl->type == RK3288) {
2590 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2591 &rk3288_grf_gpio6c_iomux);
2593 pinctrl_force_default(info->pctl_dev);
2601 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
2603 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
2604 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2605 rk3288_grf_gpio6c_iomux |
2606 GPIO6C6_SEL_WRITE_ENABLE);
2611 return pinctrl_force_default(info->pctl_dev);
2614 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
2615 rockchip_pinctrl_resume);
2617 static int rockchip_pinctrl_probe(struct platform_device *pdev)
2619 struct rockchip_pinctrl *info;
2620 struct device *dev = &pdev->dev;
2621 struct rockchip_pin_ctrl *ctrl;
2622 struct device_node *np = pdev->dev.of_node, *node;
2623 struct resource *res;
2627 if (!dev->of_node) {
2628 dev_err(dev, "device tree node not found\n");
2632 info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL);
2638 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
2640 dev_err(dev, "driver data not available\n");
2645 node = of_parse_phandle(np, "rockchip,grf", 0);
2647 info->regmap_base = syscon_node_to_regmap(node);
2648 if (IS_ERR(info->regmap_base))
2649 return PTR_ERR(info->regmap_base);
2651 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2652 base = devm_ioremap_resource(&pdev->dev, res);
2654 return PTR_ERR(base);
2656 rockchip_regmap_config.max_register = resource_size(res) - 4;
2657 rockchip_regmap_config.name = "rockchip,pinctrl";
2658 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
2659 &rockchip_regmap_config);
2661 /* to check for the old dt-bindings */
2662 info->reg_size = resource_size(res);
2664 /* Honor the old binding, with pull registers as 2nd resource */
2665 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
2666 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2667 base = devm_ioremap_resource(&pdev->dev, res);
2669 return PTR_ERR(base);
2671 rockchip_regmap_config.max_register =
2672 resource_size(res) - 4;
2673 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
2674 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
2676 &rockchip_regmap_config);
2680 /* try to find the optional reference to the pmu syscon */
2681 node = of_parse_phandle(np, "rockchip,pmu", 0);
2683 info->regmap_pmu = syscon_node_to_regmap(node);
2684 if (IS_ERR(info->regmap_pmu))
2685 return PTR_ERR(info->regmap_pmu);
2688 ret = rockchip_gpiolib_register(pdev, info);
2692 ret = rockchip_pinctrl_register(pdev, info);
2694 rockchip_gpiolib_unregister(pdev, info);
2698 platform_set_drvdata(pdev, info);
2703 static struct rockchip_pin_bank rv1108_pin_banks[] = {
2704 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2708 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
2709 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
2710 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
2713 static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
2714 .pin_banks = rv1108_pin_banks,
2715 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
2716 .label = "RV1108-GPIO",
2718 .grf_mux_offset = 0x10,
2719 .pmu_mux_offset = 0x0,
2720 .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
2721 .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
2724 static struct rockchip_pin_bank rk2928_pin_banks[] = {
2725 PIN_BANK(0, 32, "gpio0"),
2726 PIN_BANK(1, 32, "gpio1"),
2727 PIN_BANK(2, 32, "gpio2"),
2728 PIN_BANK(3, 32, "gpio3"),
2731 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
2732 .pin_banks = rk2928_pin_banks,
2733 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
2734 .label = "RK2928-GPIO",
2736 .grf_mux_offset = 0xa8,
2737 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2740 static struct rockchip_pin_bank rk3036_pin_banks[] = {
2741 PIN_BANK(0, 32, "gpio0"),
2742 PIN_BANK(1, 32, "gpio1"),
2743 PIN_BANK(2, 32, "gpio2"),
2746 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
2747 .pin_banks = rk3036_pin_banks,
2748 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
2749 .label = "RK3036-GPIO",
2751 .grf_mux_offset = 0xa8,
2752 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2755 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
2756 PIN_BANK(0, 32, "gpio0"),
2757 PIN_BANK(1, 32, "gpio1"),
2758 PIN_BANK(2, 32, "gpio2"),
2759 PIN_BANK(3, 32, "gpio3"),
2760 PIN_BANK(4, 32, "gpio4"),
2761 PIN_BANK(6, 16, "gpio6"),
2764 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
2765 .pin_banks = rk3066a_pin_banks,
2766 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
2767 .label = "RK3066a-GPIO",
2769 .grf_mux_offset = 0xa8,
2770 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2773 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
2774 PIN_BANK(0, 32, "gpio0"),
2775 PIN_BANK(1, 32, "gpio1"),
2776 PIN_BANK(2, 32, "gpio2"),
2777 PIN_BANK(3, 32, "gpio3"),
2780 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
2781 .pin_banks = rk3066b_pin_banks,
2782 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
2783 .label = "RK3066b-GPIO",
2785 .grf_mux_offset = 0x60,
2788 static struct rockchip_pin_bank rk3188_pin_banks[] = {
2789 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
2790 PIN_BANK(1, 32, "gpio1"),
2791 PIN_BANK(2, 32, "gpio2"),
2792 PIN_BANK(3, 32, "gpio3"),
2795 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
2796 .pin_banks = rk3188_pin_banks,
2797 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
2798 .label = "RK3188-GPIO",
2800 .grf_mux_offset = 0x60,
2801 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
2804 static struct rockchip_pin_bank rk3228_pin_banks[] = {
2805 PIN_BANK(0, 32, "gpio0"),
2806 PIN_BANK(1, 32, "gpio1"),
2807 PIN_BANK(2, 32, "gpio2"),
2808 PIN_BANK(3, 32, "gpio3"),
2811 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
2812 .pin_banks = rk3228_pin_banks,
2813 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
2814 .label = "RK3228-GPIO",
2816 .grf_mux_offset = 0x0,
2817 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2818 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2821 static struct rockchip_pin_bank rk3288_pin_banks[] = {
2822 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
2827 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
2832 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
2833 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
2834 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
2839 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
2844 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
2845 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2850 PIN_BANK(8, 16, "gpio8"),
2853 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
2854 .pin_banks = rk3288_pin_banks,
2855 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
2856 .label = "RK3288-GPIO",
2858 .grf_mux_offset = 0x0,
2859 .pmu_mux_offset = 0x84,
2860 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
2861 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
2864 static struct rockchip_pin_bank rk3328_pin_banks[] = {
2865 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
2866 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
2867 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
2868 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2869 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2871 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
2873 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2878 static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
2879 .pin_banks = rk3328_pin_banks,
2880 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
2881 .label = "RK3328-GPIO",
2883 .grf_mux_offset = 0x0,
2884 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2885 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2886 .iomux_recalc = rk3328_recalc_mux,
2887 .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
2890 static struct rockchip_pin_bank rk3368_pin_banks[] = {
2891 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2896 PIN_BANK(1, 32, "gpio1"),
2897 PIN_BANK(2, 32, "gpio2"),
2898 PIN_BANK(3, 32, "gpio3"),
2901 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
2902 .pin_banks = rk3368_pin_banks,
2903 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
2904 .label = "RK3368-GPIO",
2906 .grf_mux_offset = 0x0,
2907 .pmu_mux_offset = 0x0,
2908 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
2909 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
2912 static struct rockchip_pin_bank rk3399_pin_banks[] = {
2913 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
2918 DRV_TYPE_IO_1V8_ONLY,
2919 DRV_TYPE_IO_1V8_ONLY,
2920 DRV_TYPE_IO_DEFAULT,
2921 DRV_TYPE_IO_DEFAULT,
2926 PULL_TYPE_IO_1V8_ONLY,
2927 PULL_TYPE_IO_1V8_ONLY,
2928 PULL_TYPE_IO_DEFAULT,
2929 PULL_TYPE_IO_DEFAULT
2931 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
2935 DRV_TYPE_IO_1V8_OR_3V0,
2936 DRV_TYPE_IO_1V8_OR_3V0,
2937 DRV_TYPE_IO_1V8_OR_3V0,
2938 DRV_TYPE_IO_1V8_OR_3V0,
2944 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
2945 DRV_TYPE_IO_1V8_OR_3V0,
2946 DRV_TYPE_IO_1V8_ONLY,
2947 DRV_TYPE_IO_1V8_ONLY,
2948 PULL_TYPE_IO_DEFAULT,
2949 PULL_TYPE_IO_DEFAULT,
2950 PULL_TYPE_IO_1V8_ONLY,
2951 PULL_TYPE_IO_1V8_ONLY
2953 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
2954 DRV_TYPE_IO_3V3_ONLY,
2955 DRV_TYPE_IO_3V3_ONLY,
2956 DRV_TYPE_IO_1V8_OR_3V0
2958 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
2959 DRV_TYPE_IO_1V8_3V0_AUTO,
2960 DRV_TYPE_IO_1V8_OR_3V0,
2961 DRV_TYPE_IO_1V8_OR_3V0
2965 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
2966 .pin_banks = rk3399_pin_banks,
2967 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
2968 .label = "RK3399-GPIO",
2970 .grf_mux_offset = 0xe000,
2971 .pmu_mux_offset = 0x0,
2972 .grf_drv_offset = 0xe100,
2973 .pmu_drv_offset = 0x80,
2974 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
2975 .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
2978 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
2979 { .compatible = "rockchip,rv1108-pinctrl",
2980 .data = (void *)&rv1108_pin_ctrl },
2981 { .compatible = "rockchip,rk2928-pinctrl",
2982 .data = (void *)&rk2928_pin_ctrl },
2983 { .compatible = "rockchip,rk3036-pinctrl",
2984 .data = (void *)&rk3036_pin_ctrl },
2985 { .compatible = "rockchip,rk3066a-pinctrl",
2986 .data = (void *)&rk3066a_pin_ctrl },
2987 { .compatible = "rockchip,rk3066b-pinctrl",
2988 .data = (void *)&rk3066b_pin_ctrl },
2989 { .compatible = "rockchip,rk3188-pinctrl",
2990 .data = (void *)&rk3188_pin_ctrl },
2991 { .compatible = "rockchip,rk3228-pinctrl",
2992 .data = (void *)&rk3228_pin_ctrl },
2993 { .compatible = "rockchip,rk3288-pinctrl",
2994 .data = (void *)&rk3288_pin_ctrl },
2995 { .compatible = "rockchip,rk3328-pinctrl",
2996 .data = (void *)&rk3328_pin_ctrl },
2997 { .compatible = "rockchip,rk3368-pinctrl",
2998 .data = (void *)&rk3368_pin_ctrl },
2999 { .compatible = "rockchip,rk3399-pinctrl",
3000 .data = (void *)&rk3399_pin_ctrl },
3004 static struct platform_driver rockchip_pinctrl_driver = {
3005 .probe = rockchip_pinctrl_probe,
3007 .name = "rockchip-pinctrl",
3008 .pm = &rockchip_pinctrl_dev_pm_ops,
3009 .of_match_table = rockchip_pinctrl_dt_match,
3013 static int __init rockchip_pinctrl_drv_register(void)
3015 return platform_driver_register(&rockchip_pinctrl_driver);
3017 postcore_initcall(rockchip_pinctrl_drv_register);