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;
166 raw_spinlock_t slock;
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);
635 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
638 struct rockchip_pinctrl *info = bank->drvdata;
639 int iomux_num = (pin / 8);
644 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
645 dev_err(info->dev, "pin %d is unrouted\n", pin);
649 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
650 if (mux != RK_FUNC_GPIO) {
652 "pin %d only supports a gpio mux\n", pin);
661 * Set a new mux function for a pin.
663 * The register is divided into the upper and lower 16 bit. When changing
664 * a value, the previous register value is not read and changed. Instead
665 * it seems the changed bits are marked in the upper 16 bit, while the
666 * changed value gets set in the same offset in the lower 16 bit.
667 * All pin settings seem to be 2 bit wide in both the upper and lower
669 * @bank: pin bank to change
670 * @pin: pin to change
671 * @mux: new mux function to set
673 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
675 struct rockchip_pinctrl *info = bank->drvdata;
676 struct rockchip_pin_ctrl *ctrl = info->ctrl;
677 int iomux_num = (pin / 8);
678 struct regmap *regmap;
679 int reg, ret, mask, mux_type;
683 ret = rockchip_verify_mux(bank, pin, mux);
687 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
690 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
691 bank->bank_num, pin, mux);
693 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
694 ? info->regmap_pmu : info->regmap_base;
696 /* get basic quadrupel of mux registers and the correct reg inside */
697 mux_type = bank->iomux[iomux_num].type;
698 reg = bank->iomux[iomux_num].offset;
699 if (mux_type & IOMUX_WIDTH_4BIT) {
704 } else if (mux_type & IOMUX_WIDTH_3BIT) {
707 bit = (pin % 8 % 5) * 3;
714 if (ctrl->iomux_recalc && (mux_type & IOMUX_RECALCED))
715 ctrl->iomux_recalc(bank->bank_num, pin, ®, &bit, &mask);
717 data = (mask << (bit + 16));
718 rmask = data | (data >> 16);
719 data |= (mux & mask) << bit;
720 ret = regmap_update_bits(regmap, reg, rmask, data);
725 #define RV1108_PULL_PMU_OFFSET 0x10
726 #define RV1108_PULL_OFFSET 0x110
727 #define RV1108_PULL_PINS_PER_REG 8
728 #define RV1108_PULL_BITS_PER_PIN 2
729 #define RV1108_PULL_BANK_STRIDE 16
731 static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
732 int pin_num, struct regmap **regmap,
735 struct rockchip_pinctrl *info = bank->drvdata;
737 /* The first 24 pins of the first bank are located in PMU */
738 if (bank->bank_num == 0) {
739 *regmap = info->regmap_pmu;
740 *reg = RV1108_PULL_PMU_OFFSET;
742 *reg = RV1108_PULL_OFFSET;
743 *regmap = info->regmap_base;
744 /* correct the offset, as we're starting with the 2nd bank */
746 *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
749 *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
750 *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
751 *bit *= RV1108_PULL_BITS_PER_PIN;
754 #define RV1108_DRV_PMU_OFFSET 0x20
755 #define RV1108_DRV_GRF_OFFSET 0x210
756 #define RV1108_DRV_BITS_PER_PIN 2
757 #define RV1108_DRV_PINS_PER_REG 8
758 #define RV1108_DRV_BANK_STRIDE 16
760 static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
761 int pin_num, struct regmap **regmap,
764 struct rockchip_pinctrl *info = bank->drvdata;
766 /* The first 24 pins of the first bank are located in PMU */
767 if (bank->bank_num == 0) {
768 *regmap = info->regmap_pmu;
769 *reg = RV1108_DRV_PMU_OFFSET;
771 *regmap = info->regmap_base;
772 *reg = RV1108_DRV_GRF_OFFSET;
774 /* correct the offset, as we're starting with the 2nd bank */
776 *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
779 *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
780 *bit = pin_num % RV1108_DRV_PINS_PER_REG;
781 *bit *= RV1108_DRV_BITS_PER_PIN;
784 #define RK2928_PULL_OFFSET 0x118
785 #define RK2928_PULL_PINS_PER_REG 16
786 #define RK2928_PULL_BANK_STRIDE 8
788 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
789 int pin_num, struct regmap **regmap,
792 struct rockchip_pinctrl *info = bank->drvdata;
794 *regmap = info->regmap_base;
795 *reg = RK2928_PULL_OFFSET;
796 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
797 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
799 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
802 #define RK3188_PULL_OFFSET 0x164
803 #define RK3188_PULL_BITS_PER_PIN 2
804 #define RK3188_PULL_PINS_PER_REG 8
805 #define RK3188_PULL_BANK_STRIDE 16
806 #define RK3188_PULL_PMU_OFFSET 0x64
808 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
809 int pin_num, struct regmap **regmap,
812 struct rockchip_pinctrl *info = bank->drvdata;
814 /* The first 12 pins of the first bank are located elsewhere */
815 if (bank->bank_num == 0 && pin_num < 12) {
816 *regmap = info->regmap_pmu ? info->regmap_pmu
818 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
819 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
820 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
821 *bit *= RK3188_PULL_BITS_PER_PIN;
823 *regmap = info->regmap_pull ? info->regmap_pull
825 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
827 /* correct the offset, as it is the 2nd pull register */
829 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
830 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
833 * The bits in these registers have an inverse ordering
834 * with the lowest pin being in bits 15:14 and the highest
837 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
838 *bit *= RK3188_PULL_BITS_PER_PIN;
842 #define RK3288_PULL_OFFSET 0x140
843 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
844 int pin_num, struct regmap **regmap,
847 struct rockchip_pinctrl *info = bank->drvdata;
849 /* The first 24 pins of the first bank are located in PMU */
850 if (bank->bank_num == 0) {
851 *regmap = info->regmap_pmu;
852 *reg = RK3188_PULL_PMU_OFFSET;
854 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
855 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
856 *bit *= RK3188_PULL_BITS_PER_PIN;
858 *regmap = info->regmap_base;
859 *reg = RK3288_PULL_OFFSET;
861 /* correct the offset, as we're starting with the 2nd bank */
863 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
864 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
866 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
867 *bit *= RK3188_PULL_BITS_PER_PIN;
871 #define RK3288_DRV_PMU_OFFSET 0x70
872 #define RK3288_DRV_GRF_OFFSET 0x1c0
873 #define RK3288_DRV_BITS_PER_PIN 2
874 #define RK3288_DRV_PINS_PER_REG 8
875 #define RK3288_DRV_BANK_STRIDE 16
877 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
878 int pin_num, struct regmap **regmap,
881 struct rockchip_pinctrl *info = bank->drvdata;
883 /* The first 24 pins of the first bank are located in PMU */
884 if (bank->bank_num == 0) {
885 *regmap = info->regmap_pmu;
886 *reg = RK3288_DRV_PMU_OFFSET;
888 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
889 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
890 *bit *= RK3288_DRV_BITS_PER_PIN;
892 *regmap = info->regmap_base;
893 *reg = RK3288_DRV_GRF_OFFSET;
895 /* correct the offset, as we're starting with the 2nd bank */
897 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
898 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
900 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
901 *bit *= RK3288_DRV_BITS_PER_PIN;
905 #define RK3228_PULL_OFFSET 0x100
907 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
908 int pin_num, struct regmap **regmap,
911 struct rockchip_pinctrl *info = bank->drvdata;
913 *regmap = info->regmap_base;
914 *reg = RK3228_PULL_OFFSET;
915 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
916 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
918 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
919 *bit *= RK3188_PULL_BITS_PER_PIN;
922 #define RK3228_DRV_GRF_OFFSET 0x200
924 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
925 int pin_num, struct regmap **regmap,
928 struct rockchip_pinctrl *info = bank->drvdata;
930 *regmap = info->regmap_base;
931 *reg = RK3228_DRV_GRF_OFFSET;
932 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
933 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
935 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
936 *bit *= RK3288_DRV_BITS_PER_PIN;
939 #define RK3368_PULL_GRF_OFFSET 0x100
940 #define RK3368_PULL_PMU_OFFSET 0x10
942 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
943 int pin_num, struct regmap **regmap,
946 struct rockchip_pinctrl *info = bank->drvdata;
948 /* The first 32 pins of the first bank are located in PMU */
949 if (bank->bank_num == 0) {
950 *regmap = info->regmap_pmu;
951 *reg = RK3368_PULL_PMU_OFFSET;
953 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
954 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
955 *bit *= RK3188_PULL_BITS_PER_PIN;
957 *regmap = info->regmap_base;
958 *reg = RK3368_PULL_GRF_OFFSET;
960 /* correct the offset, as we're starting with the 2nd bank */
962 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
963 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
965 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
966 *bit *= RK3188_PULL_BITS_PER_PIN;
970 #define RK3368_DRV_PMU_OFFSET 0x20
971 #define RK3368_DRV_GRF_OFFSET 0x200
973 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
974 int pin_num, struct regmap **regmap,
977 struct rockchip_pinctrl *info = bank->drvdata;
979 /* The first 32 pins of the first bank are located in PMU */
980 if (bank->bank_num == 0) {
981 *regmap = info->regmap_pmu;
982 *reg = RK3368_DRV_PMU_OFFSET;
984 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
985 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
986 *bit *= RK3288_DRV_BITS_PER_PIN;
988 *regmap = info->regmap_base;
989 *reg = RK3368_DRV_GRF_OFFSET;
991 /* correct the offset, as we're starting with the 2nd bank */
993 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
994 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
996 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
997 *bit *= RK3288_DRV_BITS_PER_PIN;
1001 #define RK3399_PULL_GRF_OFFSET 0xe040
1002 #define RK3399_PULL_PMU_OFFSET 0x40
1003 #define RK3399_DRV_3BITS_PER_PIN 3
1005 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1006 int pin_num, struct regmap **regmap,
1009 struct rockchip_pinctrl *info = bank->drvdata;
1011 /* The bank0:16 and bank1:32 pins are located in PMU */
1012 if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
1013 *regmap = info->regmap_pmu;
1014 *reg = RK3399_PULL_PMU_OFFSET;
1016 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1018 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1019 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1020 *bit *= RK3188_PULL_BITS_PER_PIN;
1022 *regmap = info->regmap_base;
1023 *reg = RK3399_PULL_GRF_OFFSET;
1025 /* correct the offset, as we're starting with the 3rd bank */
1027 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1028 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1030 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1031 *bit *= RK3188_PULL_BITS_PER_PIN;
1035 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1036 int pin_num, struct regmap **regmap,
1039 struct rockchip_pinctrl *info = bank->drvdata;
1040 int drv_num = (pin_num / 8);
1042 /* The bank0:16 and bank1:32 pins are located in PMU */
1043 if ((bank->bank_num == 0) || (bank->bank_num == 1))
1044 *regmap = info->regmap_pmu;
1046 *regmap = info->regmap_base;
1048 *reg = bank->drv[drv_num].offset;
1049 if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
1050 (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
1051 *bit = (pin_num % 8) * 3;
1053 *bit = (pin_num % 8) * 2;
1056 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
1057 { 2, 4, 8, 12, -1, -1, -1, -1 },
1058 { 3, 6, 9, 12, -1, -1, -1, -1 },
1059 { 5, 10, 15, 20, -1, -1, -1, -1 },
1060 { 4, 6, 8, 10, 12, 14, 16, 18 },
1061 { 4, 7, 10, 13, 16, 19, 22, 26 }
1064 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
1067 struct rockchip_pinctrl *info = bank->drvdata;
1068 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1069 struct regmap *regmap;
1071 u32 data, temp, rmask_bits;
1073 int drv_type = bank->drv[pin_num / 8].drv_type;
1075 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1078 case DRV_TYPE_IO_1V8_3V0_AUTO:
1079 case DRV_TYPE_IO_3V3_ONLY:
1080 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1083 /* regular case, nothing to do */
1087 * drive-strength offset is special, as it is
1088 * spread over 2 registers
1090 ret = regmap_read(regmap, reg, &data);
1094 ret = regmap_read(regmap, reg + 0x4, &temp);
1099 * the bit data[15] contains bit 0 of the value
1100 * while temp[1:0] contains bits 2 and 1
1107 return rockchip_perpin_drv_list[drv_type][data];
1109 /* setting fully enclosed in the second register */
1114 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1120 case DRV_TYPE_IO_DEFAULT:
1121 case DRV_TYPE_IO_1V8_OR_3V0:
1122 case DRV_TYPE_IO_1V8_ONLY:
1123 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1126 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1131 ret = regmap_read(regmap, reg, &data);
1136 data &= (1 << rmask_bits) - 1;
1138 return rockchip_perpin_drv_list[drv_type][data];
1141 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
1142 int pin_num, int strength)
1144 struct rockchip_pinctrl *info = bank->drvdata;
1145 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1146 struct regmap *regmap;
1148 u32 data, rmask, rmask_bits, temp;
1150 int drv_type = bank->drv[pin_num / 8].drv_type;
1152 dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
1153 bank->bank_num, pin_num, strength);
1155 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1158 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
1159 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
1162 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
1163 ret = rockchip_perpin_drv_list[drv_type][i];
1169 dev_err(info->dev, "unsupported driver strength %d\n",
1175 case DRV_TYPE_IO_1V8_3V0_AUTO:
1176 case DRV_TYPE_IO_3V3_ONLY:
1177 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1180 /* regular case, nothing to do */
1184 * drive-strength offset is special, as it is spread
1185 * over 2 registers, the bit data[15] contains bit 0
1186 * of the value while temp[1:0] contains bits 2 and 1
1188 data = (ret & 0x1) << 15;
1189 temp = (ret >> 0x1) & 0x3;
1191 rmask = BIT(15) | BIT(31);
1193 ret = regmap_update_bits(regmap, reg, rmask, data);
1197 rmask = 0x3 | (0x3 << 16);
1198 temp |= (0x3 << 16);
1200 ret = regmap_update_bits(regmap, reg, rmask, temp);
1204 /* setting fully enclosed in the second register */
1209 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1214 case DRV_TYPE_IO_DEFAULT:
1215 case DRV_TYPE_IO_1V8_OR_3V0:
1216 case DRV_TYPE_IO_1V8_ONLY:
1217 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1220 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1225 /* enable the write to the equivalent lower bits */
1226 data = ((1 << rmask_bits) - 1) << (bit + 16);
1227 rmask = data | (data >> 16);
1228 data |= (ret << bit);
1230 ret = regmap_update_bits(regmap, reg, rmask, data);
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;
1300 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
1301 bank->bank_num, pin_num, pull);
1303 /* rk3066b does support any pulls */
1304 if (ctrl->type == RK3066B)
1305 return pull ? -EINVAL : 0;
1307 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1309 switch (ctrl->type) {
1311 data = BIT(bit + 16);
1312 if (pull == PIN_CONFIG_BIAS_DISABLE)
1314 ret = regmap_write(regmap, reg, data);
1321 pull_type = bank->pull_type[pin_num / 8];
1323 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
1325 if (rockchip_pull_list[pull_type][i] == pull) {
1332 dev_err(info->dev, "unsupported pull setting %d\n",
1337 /* enable the write to the equivalent lower bits */
1338 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
1339 rmask = data | (data >> 16);
1340 data |= (ret << bit);
1342 ret = regmap_update_bits(regmap, reg, rmask, data);
1345 dev_err(info->dev, "unsupported pinctrl type\n");
1352 #define RK3328_SCHMITT_BITS_PER_PIN 1
1353 #define RK3328_SCHMITT_PINS_PER_REG 16
1354 #define RK3328_SCHMITT_BANK_STRIDE 8
1355 #define RK3328_SCHMITT_GRF_OFFSET 0x380
1357 static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1359 struct regmap **regmap,
1362 struct rockchip_pinctrl *info = bank->drvdata;
1364 *regmap = info->regmap_base;
1365 *reg = RK3328_SCHMITT_GRF_OFFSET;
1367 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
1368 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
1369 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
1374 static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
1376 struct rockchip_pinctrl *info = bank->drvdata;
1377 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1378 struct regmap *regmap;
1383 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
1387 ret = regmap_read(regmap, reg, &data);
1395 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
1396 int pin_num, int enable)
1398 struct rockchip_pinctrl *info = bank->drvdata;
1399 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1400 struct regmap *regmap;
1405 dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
1406 bank->bank_num, pin_num, enable);
1408 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
1412 /* enable the write to the equivalent lower bits */
1413 data = BIT(bit + 16) | (enable << bit);
1414 rmask = BIT(bit + 16) | BIT(bit);
1416 return regmap_update_bits(regmap, reg, rmask, data);
1420 * Pinmux_ops handling
1423 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
1425 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1427 return info->nfunctions;
1430 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
1433 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1435 return info->functions[selector].name;
1438 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
1439 unsigned selector, const char * const **groups,
1440 unsigned * const num_groups)
1442 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1444 *groups = info->functions[selector].groups;
1445 *num_groups = info->functions[selector].ngroups;
1450 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
1453 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1454 const unsigned int *pins = info->groups[group].pins;
1455 const struct rockchip_pin_config *data = info->groups[group].data;
1456 struct rockchip_pin_bank *bank;
1459 dev_dbg(info->dev, "enable function %s group %s\n",
1460 info->functions[selector].name, info->groups[group].name);
1463 * for each pin in the pin group selected, program the correspoding pin
1464 * pin function number in the config register.
1466 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
1467 bank = pin_to_bank(info, pins[cnt]);
1468 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
1475 /* revert the already done pin settings */
1476 for (cnt--; cnt >= 0; cnt--)
1477 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
1485 static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1487 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
1490 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1492 return !(data & BIT(offset));
1496 * The calls to gpio_direction_output() and gpio_direction_input()
1497 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
1498 * function called from the gpiolib interface).
1500 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
1501 int pin, bool input)
1503 struct rockchip_pin_bank *bank;
1505 unsigned long flags;
1508 bank = gpiochip_get_data(chip);
1510 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
1514 clk_enable(bank->clk);
1515 raw_spin_lock_irqsave(&bank->slock, flags);
1517 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1518 /* set bit to 1 for output, 0 for input */
1523 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
1525 raw_spin_unlock_irqrestore(&bank->slock, flags);
1526 clk_disable(bank->clk);
1531 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
1532 struct pinctrl_gpio_range *range,
1533 unsigned offset, bool input)
1535 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1536 struct gpio_chip *chip;
1540 pin = offset - chip->base;
1541 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
1542 offset, range->name, pin, input ? "input" : "output");
1544 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
1548 static const struct pinmux_ops rockchip_pmx_ops = {
1549 .get_functions_count = rockchip_pmx_get_funcs_count,
1550 .get_function_name = rockchip_pmx_get_func_name,
1551 .get_function_groups = rockchip_pmx_get_groups,
1552 .set_mux = rockchip_pmx_set,
1553 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
1557 * Pinconf_ops handling
1560 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
1561 enum pin_config_param pull)
1563 switch (ctrl->type) {
1565 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
1566 pull == PIN_CONFIG_BIAS_DISABLE);
1568 return pull ? false : true;
1574 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
1580 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
1581 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
1583 /* set the pin config settings for a specified pin */
1584 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1585 unsigned long *configs, unsigned num_configs)
1587 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1588 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1589 enum pin_config_param param;
1594 for (i = 0; i < num_configs; i++) {
1595 param = pinconf_to_config_param(configs[i]);
1596 arg = pinconf_to_config_argument(configs[i]);
1599 case PIN_CONFIG_BIAS_DISABLE:
1600 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1605 case PIN_CONFIG_BIAS_PULL_UP:
1606 case PIN_CONFIG_BIAS_PULL_DOWN:
1607 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1608 case PIN_CONFIG_BIAS_BUS_HOLD:
1609 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1615 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1620 case PIN_CONFIG_OUTPUT:
1621 rockchip_gpio_set(&bank->gpio_chip,
1622 pin - bank->pin_base, arg);
1623 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
1624 pin - bank->pin_base, false);
1628 case PIN_CONFIG_DRIVE_STRENGTH:
1629 /* rk3288 is the first with per-pin drive-strength */
1630 if (!info->ctrl->drv_calc_reg)
1633 rc = rockchip_set_drive_perpin(bank,
1634 pin - bank->pin_base, arg);
1638 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1639 if (!info->ctrl->schmitt_calc_reg)
1642 rc = rockchip_set_schmitt(bank,
1643 pin - bank->pin_base, arg);
1651 } /* for each config */
1656 /* get the pin config settings for a specified pin */
1657 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1658 unsigned long *config)
1660 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1661 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1662 enum pin_config_param param = pinconf_to_config_param(*config);
1667 case PIN_CONFIG_BIAS_DISABLE:
1668 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1673 case PIN_CONFIG_BIAS_PULL_UP:
1674 case PIN_CONFIG_BIAS_PULL_DOWN:
1675 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1676 case PIN_CONFIG_BIAS_BUS_HOLD:
1677 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1680 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1685 case PIN_CONFIG_OUTPUT:
1686 rc = rockchip_get_mux(bank, pin - bank->pin_base);
1687 if (rc != RK_FUNC_GPIO)
1690 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
1696 case PIN_CONFIG_DRIVE_STRENGTH:
1697 /* rk3288 is the first with per-pin drive-strength */
1698 if (!info->ctrl->drv_calc_reg)
1701 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
1707 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1708 if (!info->ctrl->schmitt_calc_reg)
1711 rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
1722 *config = pinconf_to_config_packed(param, arg);
1727 static const struct pinconf_ops rockchip_pinconf_ops = {
1728 .pin_config_get = rockchip_pinconf_get,
1729 .pin_config_set = rockchip_pinconf_set,
1733 static const struct of_device_id rockchip_bank_match[] = {
1734 { .compatible = "rockchip,gpio-bank" },
1735 { .compatible = "rockchip,rk3188-gpio-bank0" },
1739 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
1740 struct device_node *np)
1742 struct device_node *child;
1744 for_each_child_of_node(np, child) {
1745 if (of_match_node(rockchip_bank_match, child))
1749 info->ngroups += of_get_child_count(child);
1753 static int rockchip_pinctrl_parse_groups(struct device_node *np,
1754 struct rockchip_pin_group *grp,
1755 struct rockchip_pinctrl *info,
1758 struct rockchip_pin_bank *bank;
1765 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1767 /* Initialise group */
1768 grp->name = np->name;
1771 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1772 * do sanity check and calculate pins number
1774 list = of_get_property(np, "rockchip,pins", &size);
1775 /* we do not check return since it's safe node passed down */
1776 size /= sizeof(*list);
1777 if (!size || size % 4) {
1778 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1782 grp->npins = size / 4;
1784 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1786 grp->data = devm_kzalloc(info->dev, grp->npins *
1787 sizeof(struct rockchip_pin_config),
1789 if (!grp->pins || !grp->data)
1792 for (i = 0, j = 0; i < size; i += 4, j++) {
1793 const __be32 *phandle;
1794 struct device_node *np_config;
1796 num = be32_to_cpu(*list++);
1797 bank = bank_num_to_bank(info, num);
1799 return PTR_ERR(bank);
1801 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
1802 grp->data[j].func = be32_to_cpu(*list++);
1808 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
1809 ret = pinconf_generic_parse_dt_config(np_config, NULL,
1810 &grp->data[j].configs, &grp->data[j].nconfigs);
1818 static int rockchip_pinctrl_parse_functions(struct device_node *np,
1819 struct rockchip_pinctrl *info,
1822 struct device_node *child;
1823 struct rockchip_pmx_func *func;
1824 struct rockchip_pin_group *grp;
1826 static u32 grp_index;
1829 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1831 func = &info->functions[index];
1833 /* Initialise function */
1834 func->name = np->name;
1835 func->ngroups = of_get_child_count(np);
1836 if (func->ngroups <= 0)
1839 func->groups = devm_kzalloc(info->dev,
1840 func->ngroups * sizeof(char *), GFP_KERNEL);
1844 for_each_child_of_node(np, child) {
1845 func->groups[i] = child->name;
1846 grp = &info->groups[grp_index++];
1847 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
1857 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
1858 struct rockchip_pinctrl *info)
1860 struct device *dev = &pdev->dev;
1861 struct device_node *np = dev->of_node;
1862 struct device_node *child;
1866 rockchip_pinctrl_child_count(info, np);
1868 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1869 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1871 info->functions = devm_kzalloc(dev, info->nfunctions *
1872 sizeof(struct rockchip_pmx_func),
1874 if (!info->functions) {
1875 dev_err(dev, "failed to allocate memory for function list\n");
1879 info->groups = devm_kzalloc(dev, info->ngroups *
1880 sizeof(struct rockchip_pin_group),
1882 if (!info->groups) {
1883 dev_err(dev, "failed allocate memory for ping group list\n");
1889 for_each_child_of_node(np, child) {
1890 if (of_match_node(rockchip_bank_match, child))
1893 ret = rockchip_pinctrl_parse_functions(child, info, i++);
1895 dev_err(&pdev->dev, "failed to parse function\n");
1904 static int rockchip_pinctrl_register(struct platform_device *pdev,
1905 struct rockchip_pinctrl *info)
1907 struct pinctrl_desc *ctrldesc = &info->pctl;
1908 struct pinctrl_pin_desc *pindesc, *pdesc;
1909 struct rockchip_pin_bank *pin_bank;
1913 ctrldesc->name = "rockchip-pinctrl";
1914 ctrldesc->owner = THIS_MODULE;
1915 ctrldesc->pctlops = &rockchip_pctrl_ops;
1916 ctrldesc->pmxops = &rockchip_pmx_ops;
1917 ctrldesc->confops = &rockchip_pinconf_ops;
1919 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
1920 info->ctrl->nr_pins, GFP_KERNEL);
1922 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
1925 ctrldesc->pins = pindesc;
1926 ctrldesc->npins = info->ctrl->nr_pins;
1929 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
1930 pin_bank = &info->ctrl->pin_banks[bank];
1931 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
1933 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
1934 pin_bank->name, pin);
1939 ret = rockchip_pinctrl_parse_dt(pdev, info);
1943 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
1944 if (IS_ERR(info->pctl_dev)) {
1945 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1946 return PTR_ERR(info->pctl_dev);
1949 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
1950 pin_bank = &info->ctrl->pin_banks[bank];
1951 pin_bank->grange.name = pin_bank->name;
1952 pin_bank->grange.id = bank;
1953 pin_bank->grange.pin_base = pin_bank->pin_base;
1954 pin_bank->grange.base = pin_bank->gpio_chip.base;
1955 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
1956 pin_bank->grange.gc = &pin_bank->gpio_chip;
1957 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
1967 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
1969 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1970 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
1971 unsigned long flags;
1974 clk_enable(bank->clk);
1975 raw_spin_lock_irqsave(&bank->slock, flags);
1978 data &= ~BIT(offset);
1980 data |= BIT(offset);
1983 raw_spin_unlock_irqrestore(&bank->slock, flags);
1984 clk_disable(bank->clk);
1988 * Returns the level of the pin for input direction and setting of the DR
1989 * register for output gpios.
1991 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
1993 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
1996 clk_enable(bank->clk);
1997 data = readl(bank->reg_base + GPIO_EXT_PORT);
1998 clk_disable(bank->clk);
2005 * gpiolib gpio_direction_input callback function. The setting of the pin
2006 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
2009 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
2011 return pinctrl_gpio_direction_input(gc->base + offset);
2015 * gpiolib gpio_direction_output callback function. The setting of the pin
2016 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
2019 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
2020 unsigned offset, int value)
2022 rockchip_gpio_set(gc, offset, value);
2023 return pinctrl_gpio_direction_output(gc->base + offset);
2027 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
2028 * and a virtual IRQ, if not already present.
2030 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
2032 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2038 virq = irq_create_mapping(bank->domain, offset);
2040 return (virq) ? : -ENXIO;
2043 static const struct gpio_chip rockchip_gpiolib_chip = {
2044 .request = gpiochip_generic_request,
2045 .free = gpiochip_generic_free,
2046 .set = rockchip_gpio_set,
2047 .get = rockchip_gpio_get,
2048 .get_direction = rockchip_gpio_get_direction,
2049 .direction_input = rockchip_gpio_direction_input,
2050 .direction_output = rockchip_gpio_direction_output,
2051 .to_irq = rockchip_gpio_to_irq,
2052 .owner = THIS_MODULE,
2056 * Interrupt handling
2059 static void rockchip_irq_demux(struct irq_desc *desc)
2061 struct irq_chip *chip = irq_desc_get_chip(desc);
2062 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
2065 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
2067 chained_irq_enter(chip, desc);
2069 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
2072 unsigned int irq, virq;
2076 virq = irq_linear_revmap(bank->domain, irq);
2079 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
2083 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
2086 * Triggering IRQ on both rising and falling edge
2087 * needs manual intervention.
2089 if (bank->toggle_edge_mode & BIT(irq)) {
2090 u32 data, data_old, polarity;
2091 unsigned long flags;
2093 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
2095 raw_spin_lock_irqsave(&bank->slock, flags);
2097 polarity = readl_relaxed(bank->reg_base +
2099 if (data & BIT(irq))
2100 polarity &= ~BIT(irq);
2102 polarity |= BIT(irq);
2104 bank->reg_base + GPIO_INT_POLARITY);
2106 raw_spin_unlock_irqrestore(&bank->slock, flags);
2109 data = readl_relaxed(bank->reg_base +
2111 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
2114 generic_handle_irq(virq);
2117 chained_irq_exit(chip, desc);
2120 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
2122 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2123 struct rockchip_pin_bank *bank = gc->private;
2124 u32 mask = BIT(d->hwirq);
2128 unsigned long flags;
2131 /* make sure the pin is configured as gpio input */
2132 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
2136 clk_enable(bank->clk);
2137 raw_spin_lock_irqsave(&bank->slock, flags);
2139 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2141 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2143 raw_spin_unlock_irqrestore(&bank->slock, flags);
2145 if (type & IRQ_TYPE_EDGE_BOTH)
2146 irq_set_handler_locked(d, handle_edge_irq);
2148 irq_set_handler_locked(d, handle_level_irq);
2150 raw_spin_lock_irqsave(&bank->slock, flags);
2153 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
2154 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
2157 case IRQ_TYPE_EDGE_BOTH:
2158 bank->toggle_edge_mode |= mask;
2162 * Determine gpio state. If 1 next interrupt should be falling
2165 data = readl(bank->reg_base + GPIO_EXT_PORT);
2171 case IRQ_TYPE_EDGE_RISING:
2172 bank->toggle_edge_mode &= ~mask;
2176 case IRQ_TYPE_EDGE_FALLING:
2177 bank->toggle_edge_mode &= ~mask;
2181 case IRQ_TYPE_LEVEL_HIGH:
2182 bank->toggle_edge_mode &= ~mask;
2186 case IRQ_TYPE_LEVEL_LOW:
2187 bank->toggle_edge_mode &= ~mask;
2193 raw_spin_unlock_irqrestore(&bank->slock, flags);
2194 clk_disable(bank->clk);
2198 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
2199 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
2202 raw_spin_unlock_irqrestore(&bank->slock, flags);
2203 clk_disable(bank->clk);
2208 static void rockchip_irq_suspend(struct irq_data *d)
2210 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2211 struct rockchip_pin_bank *bank = gc->private;
2213 clk_enable(bank->clk);
2214 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
2215 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
2216 clk_disable(bank->clk);
2219 static void rockchip_irq_resume(struct irq_data *d)
2221 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2222 struct rockchip_pin_bank *bank = gc->private;
2224 clk_enable(bank->clk);
2225 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
2226 clk_disable(bank->clk);
2229 static void rockchip_irq_enable(struct irq_data *d)
2231 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2232 struct rockchip_pin_bank *bank = gc->private;
2234 clk_enable(bank->clk);
2235 irq_gc_mask_clr_bit(d);
2238 static void rockchip_irq_disable(struct irq_data *d)
2240 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2241 struct rockchip_pin_bank *bank = gc->private;
2243 irq_gc_mask_set_bit(d);
2244 clk_disable(bank->clk);
2247 static int rockchip_interrupts_register(struct platform_device *pdev,
2248 struct rockchip_pinctrl *info)
2250 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2251 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2252 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
2253 struct irq_chip_generic *gc;
2257 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2259 dev_warn(&pdev->dev, "bank %s is not valid\n",
2264 ret = clk_enable(bank->clk);
2266 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
2271 bank->domain = irq_domain_add_linear(bank->of_node, 32,
2272 &irq_generic_chip_ops, NULL);
2273 if (!bank->domain) {
2274 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
2276 clk_disable(bank->clk);
2280 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
2281 "rockchip_gpio_irq", handle_level_irq,
2282 clr, 0, IRQ_GC_INIT_MASK_CACHE);
2284 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
2286 irq_domain_remove(bank->domain);
2287 clk_disable(bank->clk);
2292 * Linux assumes that all interrupts start out disabled/masked.
2293 * Our driver only uses the concept of masked and always keeps
2294 * things enabled, so for us that's all masked and all enabled.
2296 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
2297 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
2299 gc = irq_get_domain_generic_chip(bank->domain, 0);
2300 gc->reg_base = bank->reg_base;
2302 gc->chip_types[0].regs.mask = GPIO_INTMASK;
2303 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
2304 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
2305 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
2306 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
2307 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
2308 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
2309 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
2310 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
2311 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
2312 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
2313 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
2315 irq_set_chained_handler_and_data(bank->irq,
2316 rockchip_irq_demux, bank);
2318 /* map the gpio irqs here, when the clock is still running */
2319 for (j = 0 ; j < 32 ; j++)
2320 irq_create_mapping(bank->domain, j);
2322 clk_disable(bank->clk);
2328 static int rockchip_gpiolib_register(struct platform_device *pdev,
2329 struct rockchip_pinctrl *info)
2331 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2332 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2333 struct gpio_chip *gc;
2337 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2339 dev_warn(&pdev->dev, "bank %s is not valid\n",
2344 bank->gpio_chip = rockchip_gpiolib_chip;
2346 gc = &bank->gpio_chip;
2347 gc->base = bank->pin_base;
2348 gc->ngpio = bank->nr_pins;
2349 gc->parent = &pdev->dev;
2350 gc->of_node = bank->of_node;
2351 gc->label = bank->name;
2353 ret = gpiochip_add_data(gc, bank);
2355 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
2361 rockchip_interrupts_register(pdev, info);
2366 for (--i, --bank; i >= 0; --i, --bank) {
2369 gpiochip_remove(&bank->gpio_chip);
2374 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
2375 struct rockchip_pinctrl *info)
2377 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2378 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2381 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2384 gpiochip_remove(&bank->gpio_chip);
2390 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
2391 struct rockchip_pinctrl *info)
2393 struct resource res;
2396 if (of_address_to_resource(bank->of_node, 0, &res)) {
2397 dev_err(info->dev, "cannot find IO resource for bank\n");
2401 bank->reg_base = devm_ioremap_resource(info->dev, &res);
2402 if (IS_ERR(bank->reg_base))
2403 return PTR_ERR(bank->reg_base);
2406 * special case, where parts of the pull setting-registers are
2407 * part of the PMU register space
2409 if (of_device_is_compatible(bank->of_node,
2410 "rockchip,rk3188-gpio-bank0")) {
2411 struct device_node *node;
2413 node = of_parse_phandle(bank->of_node->parent,
2416 if (of_address_to_resource(bank->of_node, 1, &res)) {
2417 dev_err(info->dev, "cannot find IO resource for bank\n");
2421 base = devm_ioremap_resource(info->dev, &res);
2423 return PTR_ERR(base);
2424 rockchip_regmap_config.max_register =
2425 resource_size(&res) - 4;
2426 rockchip_regmap_config.name =
2427 "rockchip,rk3188-gpio-bank0-pull";
2428 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
2430 &rockchip_regmap_config);
2434 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
2436 bank->clk = of_clk_get(bank->of_node, 0);
2437 if (IS_ERR(bank->clk))
2438 return PTR_ERR(bank->clk);
2440 return clk_prepare(bank->clk);
2443 static const struct of_device_id rockchip_pinctrl_dt_match[];
2445 /* retrieve the soc specific data */
2446 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
2447 struct rockchip_pinctrl *d,
2448 struct platform_device *pdev)
2450 const struct of_device_id *match;
2451 struct device_node *node = pdev->dev.of_node;
2452 struct device_node *np;
2453 struct rockchip_pin_ctrl *ctrl;
2454 struct rockchip_pin_bank *bank;
2455 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
2457 match = of_match_node(rockchip_pinctrl_dt_match, node);
2458 ctrl = (struct rockchip_pin_ctrl *)match->data;
2460 for_each_child_of_node(node, np) {
2461 if (!of_find_property(np, "gpio-controller", NULL))
2464 bank = ctrl->pin_banks;
2465 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2466 if (!strcmp(bank->name, np->name)) {
2469 if (!rockchip_get_bank_data(bank, d))
2477 grf_offs = ctrl->grf_mux_offset;
2478 pmu_offs = ctrl->pmu_mux_offset;
2479 drv_pmu_offs = ctrl->pmu_drv_offset;
2480 drv_grf_offs = ctrl->grf_drv_offset;
2481 bank = ctrl->pin_banks;
2482 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
2485 raw_spin_lock_init(&bank->slock);
2487 bank->pin_base = ctrl->nr_pins;
2488 ctrl->nr_pins += bank->nr_pins;
2490 /* calculate iomux and drv offsets */
2491 for (j = 0; j < 4; j++) {
2492 struct rockchip_iomux *iom = &bank->iomux[j];
2493 struct rockchip_drv *drv = &bank->drv[j];
2496 if (bank_pins >= bank->nr_pins)
2499 /* preset iomux offset value, set new start value */
2500 if (iom->offset >= 0) {
2501 if (iom->type & IOMUX_SOURCE_PMU)
2502 pmu_offs = iom->offset;
2504 grf_offs = iom->offset;
2505 } else { /* set current iomux offset */
2506 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
2507 pmu_offs : grf_offs;
2510 /* preset drv offset value, set new start value */
2511 if (drv->offset >= 0) {
2512 if (iom->type & IOMUX_SOURCE_PMU)
2513 drv_pmu_offs = drv->offset;
2515 drv_grf_offs = drv->offset;
2516 } else { /* set current drv offset */
2517 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
2518 drv_pmu_offs : drv_grf_offs;
2521 dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
2522 i, j, iom->offset, drv->offset);
2525 * Increase offset according to iomux width.
2526 * 4bit iomux'es are spread over two registers.
2528 inc = (iom->type & (IOMUX_WIDTH_4BIT |
2529 IOMUX_WIDTH_3BIT)) ? 8 : 4;
2530 if (iom->type & IOMUX_SOURCE_PMU)
2536 * Increase offset according to drv width.
2537 * 3bit drive-strenth'es are spread over two registers.
2539 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
2540 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
2545 if (iom->type & IOMUX_SOURCE_PMU)
2546 drv_pmu_offs += inc;
2548 drv_grf_offs += inc;
2557 #define RK3288_GRF_GPIO6C_IOMUX 0x64
2558 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
2560 static u32 rk3288_grf_gpio6c_iomux;
2562 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
2564 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
2565 int ret = pinctrl_force_sleep(info->pctl_dev);
2571 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
2572 * the setting here, and restore it at resume.
2574 if (info->ctrl->type == RK3288) {
2575 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2576 &rk3288_grf_gpio6c_iomux);
2578 pinctrl_force_default(info->pctl_dev);
2586 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
2588 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
2589 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2590 rk3288_grf_gpio6c_iomux |
2591 GPIO6C6_SEL_WRITE_ENABLE);
2596 return pinctrl_force_default(info->pctl_dev);
2599 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
2600 rockchip_pinctrl_resume);
2602 static int rockchip_pinctrl_probe(struct platform_device *pdev)
2604 struct rockchip_pinctrl *info;
2605 struct device *dev = &pdev->dev;
2606 struct rockchip_pin_ctrl *ctrl;
2607 struct device_node *np = pdev->dev.of_node, *node;
2608 struct resource *res;
2612 if (!dev->of_node) {
2613 dev_err(dev, "device tree node not found\n");
2617 info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL);
2623 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
2625 dev_err(dev, "driver data not available\n");
2630 node = of_parse_phandle(np, "rockchip,grf", 0);
2632 info->regmap_base = syscon_node_to_regmap(node);
2633 if (IS_ERR(info->regmap_base))
2634 return PTR_ERR(info->regmap_base);
2636 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2637 base = devm_ioremap_resource(&pdev->dev, res);
2639 return PTR_ERR(base);
2641 rockchip_regmap_config.max_register = resource_size(res) - 4;
2642 rockchip_regmap_config.name = "rockchip,pinctrl";
2643 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
2644 &rockchip_regmap_config);
2646 /* to check for the old dt-bindings */
2647 info->reg_size = resource_size(res);
2649 /* Honor the old binding, with pull registers as 2nd resource */
2650 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
2651 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2652 base = devm_ioremap_resource(&pdev->dev, res);
2654 return PTR_ERR(base);
2656 rockchip_regmap_config.max_register =
2657 resource_size(res) - 4;
2658 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
2659 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
2661 &rockchip_regmap_config);
2665 /* try to find the optional reference to the pmu syscon */
2666 node = of_parse_phandle(np, "rockchip,pmu", 0);
2668 info->regmap_pmu = syscon_node_to_regmap(node);
2669 if (IS_ERR(info->regmap_pmu))
2670 return PTR_ERR(info->regmap_pmu);
2673 ret = rockchip_gpiolib_register(pdev, info);
2677 ret = rockchip_pinctrl_register(pdev, info);
2679 rockchip_gpiolib_unregister(pdev, info);
2683 platform_set_drvdata(pdev, info);
2688 static struct rockchip_pin_bank rv1108_pin_banks[] = {
2689 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2693 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
2694 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
2695 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
2698 static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
2699 .pin_banks = rv1108_pin_banks,
2700 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
2701 .label = "RV1108-GPIO",
2703 .grf_mux_offset = 0x10,
2704 .pmu_mux_offset = 0x0,
2705 .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
2706 .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
2709 static struct rockchip_pin_bank rk2928_pin_banks[] = {
2710 PIN_BANK(0, 32, "gpio0"),
2711 PIN_BANK(1, 32, "gpio1"),
2712 PIN_BANK(2, 32, "gpio2"),
2713 PIN_BANK(3, 32, "gpio3"),
2716 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
2717 .pin_banks = rk2928_pin_banks,
2718 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
2719 .label = "RK2928-GPIO",
2721 .grf_mux_offset = 0xa8,
2722 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2725 static struct rockchip_pin_bank rk3036_pin_banks[] = {
2726 PIN_BANK(0, 32, "gpio0"),
2727 PIN_BANK(1, 32, "gpio1"),
2728 PIN_BANK(2, 32, "gpio2"),
2731 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
2732 .pin_banks = rk3036_pin_banks,
2733 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
2734 .label = "RK3036-GPIO",
2736 .grf_mux_offset = 0xa8,
2737 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2740 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
2741 PIN_BANK(0, 32, "gpio0"),
2742 PIN_BANK(1, 32, "gpio1"),
2743 PIN_BANK(2, 32, "gpio2"),
2744 PIN_BANK(3, 32, "gpio3"),
2745 PIN_BANK(4, 32, "gpio4"),
2746 PIN_BANK(6, 16, "gpio6"),
2749 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
2750 .pin_banks = rk3066a_pin_banks,
2751 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
2752 .label = "RK3066a-GPIO",
2754 .grf_mux_offset = 0xa8,
2755 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2758 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
2759 PIN_BANK(0, 32, "gpio0"),
2760 PIN_BANK(1, 32, "gpio1"),
2761 PIN_BANK(2, 32, "gpio2"),
2762 PIN_BANK(3, 32, "gpio3"),
2765 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
2766 .pin_banks = rk3066b_pin_banks,
2767 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
2768 .label = "RK3066b-GPIO",
2770 .grf_mux_offset = 0x60,
2773 static struct rockchip_pin_bank rk3188_pin_banks[] = {
2774 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
2775 PIN_BANK(1, 32, "gpio1"),
2776 PIN_BANK(2, 32, "gpio2"),
2777 PIN_BANK(3, 32, "gpio3"),
2780 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
2781 .pin_banks = rk3188_pin_banks,
2782 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
2783 .label = "RK3188-GPIO",
2785 .grf_mux_offset = 0x60,
2786 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
2789 static struct rockchip_pin_bank rk3228_pin_banks[] = {
2790 PIN_BANK(0, 32, "gpio0"),
2791 PIN_BANK(1, 32, "gpio1"),
2792 PIN_BANK(2, 32, "gpio2"),
2793 PIN_BANK(3, 32, "gpio3"),
2796 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
2797 .pin_banks = rk3228_pin_banks,
2798 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
2799 .label = "RK3228-GPIO",
2801 .grf_mux_offset = 0x0,
2802 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2803 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2806 static struct rockchip_pin_bank rk3288_pin_banks[] = {
2807 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
2812 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
2817 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
2818 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
2819 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
2824 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
2829 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
2830 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2835 PIN_BANK(8, 16, "gpio8"),
2838 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
2839 .pin_banks = rk3288_pin_banks,
2840 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
2841 .label = "RK3288-GPIO",
2843 .grf_mux_offset = 0x0,
2844 .pmu_mux_offset = 0x84,
2845 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
2846 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
2849 static struct rockchip_pin_bank rk3328_pin_banks[] = {
2850 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
2851 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
2852 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
2853 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2854 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2856 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
2858 IOMUX_WIDTH_3BIT | IOMUX_RECALCED,
2863 static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
2864 .pin_banks = rk3328_pin_banks,
2865 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
2866 .label = "RK3328-GPIO",
2868 .grf_mux_offset = 0x0,
2869 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2870 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2871 .iomux_recalc = rk3328_recalc_mux,
2872 .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
2875 static struct rockchip_pin_bank rk3368_pin_banks[] = {
2876 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2881 PIN_BANK(1, 32, "gpio1"),
2882 PIN_BANK(2, 32, "gpio2"),
2883 PIN_BANK(3, 32, "gpio3"),
2886 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
2887 .pin_banks = rk3368_pin_banks,
2888 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
2889 .label = "RK3368-GPIO",
2891 .grf_mux_offset = 0x0,
2892 .pmu_mux_offset = 0x0,
2893 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
2894 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
2897 static struct rockchip_pin_bank rk3399_pin_banks[] = {
2898 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
2903 DRV_TYPE_IO_1V8_ONLY,
2904 DRV_TYPE_IO_1V8_ONLY,
2905 DRV_TYPE_IO_DEFAULT,
2906 DRV_TYPE_IO_DEFAULT,
2911 PULL_TYPE_IO_1V8_ONLY,
2912 PULL_TYPE_IO_1V8_ONLY,
2913 PULL_TYPE_IO_DEFAULT,
2914 PULL_TYPE_IO_DEFAULT
2916 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
2920 DRV_TYPE_IO_1V8_OR_3V0,
2921 DRV_TYPE_IO_1V8_OR_3V0,
2922 DRV_TYPE_IO_1V8_OR_3V0,
2923 DRV_TYPE_IO_1V8_OR_3V0,
2929 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
2930 DRV_TYPE_IO_1V8_OR_3V0,
2931 DRV_TYPE_IO_1V8_ONLY,
2932 DRV_TYPE_IO_1V8_ONLY,
2933 PULL_TYPE_IO_DEFAULT,
2934 PULL_TYPE_IO_DEFAULT,
2935 PULL_TYPE_IO_1V8_ONLY,
2936 PULL_TYPE_IO_1V8_ONLY
2938 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
2939 DRV_TYPE_IO_3V3_ONLY,
2940 DRV_TYPE_IO_3V3_ONLY,
2941 DRV_TYPE_IO_1V8_OR_3V0
2943 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
2944 DRV_TYPE_IO_1V8_3V0_AUTO,
2945 DRV_TYPE_IO_1V8_OR_3V0,
2946 DRV_TYPE_IO_1V8_OR_3V0
2950 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
2951 .pin_banks = rk3399_pin_banks,
2952 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
2953 .label = "RK3399-GPIO",
2955 .grf_mux_offset = 0xe000,
2956 .pmu_mux_offset = 0x0,
2957 .grf_drv_offset = 0xe100,
2958 .pmu_drv_offset = 0x80,
2959 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
2960 .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
2963 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
2964 { .compatible = "rockchip,rv1108-pinctrl",
2965 .data = (void *)&rv1108_pin_ctrl },
2966 { .compatible = "rockchip,rk2928-pinctrl",
2967 .data = (void *)&rk2928_pin_ctrl },
2968 { .compatible = "rockchip,rk3036-pinctrl",
2969 .data = (void *)&rk3036_pin_ctrl },
2970 { .compatible = "rockchip,rk3066a-pinctrl",
2971 .data = (void *)&rk3066a_pin_ctrl },
2972 { .compatible = "rockchip,rk3066b-pinctrl",
2973 .data = (void *)&rk3066b_pin_ctrl },
2974 { .compatible = "rockchip,rk3188-pinctrl",
2975 .data = (void *)&rk3188_pin_ctrl },
2976 { .compatible = "rockchip,rk3228-pinctrl",
2977 .data = (void *)&rk3228_pin_ctrl },
2978 { .compatible = "rockchip,rk3288-pinctrl",
2979 .data = (void *)&rk3288_pin_ctrl },
2980 { .compatible = "rockchip,rk3328-pinctrl",
2981 .data = (void *)&rk3328_pin_ctrl },
2982 { .compatible = "rockchip,rk3368-pinctrl",
2983 .data = (void *)&rk3368_pin_ctrl },
2984 { .compatible = "rockchip,rk3399-pinctrl",
2985 .data = (void *)&rk3399_pin_ctrl },
2989 static struct platform_driver rockchip_pinctrl_driver = {
2990 .probe = rockchip_pinctrl_probe,
2992 .name = "rockchip-pinctrl",
2993 .pm = &rockchip_pinctrl_dev_pm_ops,
2994 .of_match_table = rockchip_pinctrl_dt_match,
2998 static int __init rockchip_pinctrl_drv_register(void)
3000 return platform_driver_register(&rockchip_pinctrl_driver);
3002 postcore_initcall(rockchip_pinctrl_drv_register);