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 {
74 * Encode variants of iomux registers into a type variable
76 #define IOMUX_GPIO_ONLY BIT(0)
77 #define IOMUX_WIDTH_4BIT BIT(1)
78 #define IOMUX_SOURCE_PMU BIT(2)
79 #define IOMUX_UNROUTED BIT(3)
80 #define IOMUX_WIDTH_3BIT BIT(4)
83 * struct rockchip_iomux
84 * @type: iomux variant using IOMUX_* constants
85 * @offset: if initialized to -1 it will be autocalculated, by specifying
86 * an initial offset value the relevant source offset can be reset
87 * to a new value for autocalculating the following iomux registers.
89 struct rockchip_iomux {
95 * enum type index corresponding to rockchip_perpin_drv_list arrays index.
97 enum rockchip_pin_drv_type {
98 DRV_TYPE_IO_DEFAULT = 0,
99 DRV_TYPE_IO_1V8_OR_3V0,
100 DRV_TYPE_IO_1V8_ONLY,
101 DRV_TYPE_IO_1V8_3V0_AUTO,
102 DRV_TYPE_IO_3V3_ONLY,
107 * enum type index corresponding to rockchip_pull_list arrays index.
109 enum rockchip_pin_pull_type {
110 PULL_TYPE_IO_DEFAULT = 0,
111 PULL_TYPE_IO_1V8_ONLY,
116 * struct rockchip_drv
117 * @drv_type: drive strength variant using rockchip_perpin_drv_type
118 * @offset: if initialized to -1 it will be autocalculated, by specifying
119 * an initial offset value the relevant source offset can be reset
120 * to a new value for autocalculating the following drive strength
121 * registers. if used chips own cal_drv func instead to calculate
122 * registers offset, the variant could be ignored.
124 struct rockchip_drv {
125 enum rockchip_pin_drv_type drv_type;
130 * struct rockchip_pin_bank
131 * @reg_base: register base of the gpio bank
132 * @regmap_pull: optional separate register for additional pull settings
133 * @clk: clock of the gpio bank
134 * @irq: interrupt of the gpio bank
135 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
136 * @pin_base: first pin number
137 * @nr_pins: number of pins in this bank
138 * @name: name of the bank
139 * @bank_num: number of the bank, to account for holes
140 * @iomux: array describing the 4 iomux sources of the bank
141 * @drv: array describing the 4 drive strength sources of the bank
142 * @pull_type: array describing the 4 pull type sources of the bank
143 * @valid: is all necessary information present
144 * @of_node: dt node of this bank
145 * @drvdata: common pinctrl basedata
146 * @domain: irqdomain of the gpio bank
147 * @gpio_chip: gpiolib chip
148 * @grange: gpio range
149 * @slock: spinlock for the gpio bank
150 * @toggle_edge_mode: bit mask to toggle (falling/rising) edge mode
151 * @recalced_mask: bit mask to indicate a need to recalulate the mask
152 * @route_mask: bits describing the routing pins of per bank
154 struct rockchip_pin_bank {
155 void __iomem *reg_base;
156 struct regmap *regmap_pull;
164 struct rockchip_iomux iomux[4];
165 struct rockchip_drv drv[4];
166 enum rockchip_pin_pull_type pull_type[4];
168 struct device_node *of_node;
169 struct rockchip_pinctrl *drvdata;
170 struct irq_domain *domain;
171 struct gpio_chip gpio_chip;
172 struct pinctrl_gpio_range grange;
173 raw_spinlock_t slock;
174 u32 toggle_edge_mode;
179 #define PIN_BANK(id, pins, label) \
192 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
198 { .type = iom0, .offset = -1 }, \
199 { .type = iom1, .offset = -1 }, \
200 { .type = iom2, .offset = -1 }, \
201 { .type = iom3, .offset = -1 }, \
205 #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \
217 { .drv_type = type0, .offset = -1 }, \
218 { .drv_type = type1, .offset = -1 }, \
219 { .drv_type = type2, .offset = -1 }, \
220 { .drv_type = type3, .offset = -1 }, \
224 #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \
225 drv2, drv3, pull0, pull1, \
238 { .drv_type = drv0, .offset = -1 }, \
239 { .drv_type = drv1, .offset = -1 }, \
240 { .drv_type = drv2, .offset = -1 }, \
241 { .drv_type = drv3, .offset = -1 }, \
243 .pull_type[0] = pull0, \
244 .pull_type[1] = pull1, \
245 .pull_type[2] = pull2, \
246 .pull_type[3] = pull3, \
249 #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \
250 iom2, iom3, drv0, drv1, drv2, \
251 drv3, offset0, offset1, \
258 { .type = iom0, .offset = -1 }, \
259 { .type = iom1, .offset = -1 }, \
260 { .type = iom2, .offset = -1 }, \
261 { .type = iom3, .offset = -1 }, \
264 { .drv_type = drv0, .offset = offset0 }, \
265 { .drv_type = drv1, .offset = offset1 }, \
266 { .drv_type = drv2, .offset = offset2 }, \
267 { .drv_type = drv3, .offset = offset3 }, \
271 #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \
272 label, iom0, iom1, iom2, \
273 iom3, drv0, drv1, drv2, \
274 drv3, offset0, offset1, \
275 offset2, offset3, pull0, \
276 pull1, pull2, pull3) \
282 { .type = iom0, .offset = -1 }, \
283 { .type = iom1, .offset = -1 }, \
284 { .type = iom2, .offset = -1 }, \
285 { .type = iom3, .offset = -1 }, \
288 { .drv_type = drv0, .offset = offset0 }, \
289 { .drv_type = drv1, .offset = offset1 }, \
290 { .drv_type = drv2, .offset = offset2 }, \
291 { .drv_type = drv3, .offset = offset3 }, \
293 .pull_type[0] = pull0, \
294 .pull_type[1] = pull1, \
295 .pull_type[2] = pull2, \
296 .pull_type[3] = pull3, \
300 * struct rockchip_mux_recalced_data: represent a pin iomux data.
303 * @bit: index at register.
304 * @reg: register offset.
307 struct rockchip_mux_recalced_data {
316 * struct rockchip_mux_recalced_data: represent a pin iomux data.
317 * @bank_num: bank number.
318 * @pin: index at register or used to calc index.
319 * @func: the min pin.
320 * @route_location: the mux route location (same, pmu, grf).
321 * @route_offset: the max pin.
322 * @route_val: the register offset.
324 struct rockchip_mux_route_data {
332 struct rockchip_pin_ctrl {
333 struct rockchip_pin_bank *pin_banks;
337 enum rockchip_pinctrl_type type;
342 struct rockchip_mux_recalced_data *iomux_recalced;
344 struct rockchip_mux_route_data *iomux_routes;
347 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
348 int pin_num, struct regmap **regmap,
350 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
351 int pin_num, struct regmap **regmap,
353 int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank,
354 int pin_num, struct regmap **regmap,
358 struct rockchip_pin_config {
360 unsigned long *configs;
361 unsigned int nconfigs;
365 * struct rockchip_pin_group: represent group of pins of a pinmux function.
366 * @name: name of the pin group, used to lookup the group.
367 * @pins: the pins included in this group.
368 * @npins: number of pins included in this group.
369 * @data: local pin configuration
371 struct rockchip_pin_group {
375 struct rockchip_pin_config *data;
379 * struct rockchip_pmx_func: represent a pin function.
380 * @name: name of the pin function, used to lookup the function.
381 * @groups: one or more names of pin groups that provide this function.
382 * @ngroups: number of groups included in @groups.
384 struct rockchip_pmx_func {
390 struct rockchip_pinctrl {
391 struct regmap *regmap_base;
393 struct regmap *regmap_pull;
394 struct regmap *regmap_pmu;
396 struct rockchip_pin_ctrl *ctrl;
397 struct pinctrl_desc pctl;
398 struct pinctrl_dev *pctl_dev;
399 struct rockchip_pin_group *groups;
400 unsigned int ngroups;
401 struct rockchip_pmx_func *functions;
402 unsigned int nfunctions;
405 static struct regmap_config rockchip_regmap_config = {
411 static inline const struct rockchip_pin_group *pinctrl_name_to_group(
412 const struct rockchip_pinctrl *info,
417 for (i = 0; i < info->ngroups; i++) {
418 if (!strcmp(info->groups[i].name, name))
419 return &info->groups[i];
426 * given a pin number that is local to a pin controller, find out the pin bank
427 * and the register base of the pin bank.
429 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
432 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
434 while (pin >= (b->pin_base + b->nr_pins))
440 static struct rockchip_pin_bank *bank_num_to_bank(
441 struct rockchip_pinctrl *info,
444 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
447 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
448 if (b->bank_num == num)
452 return ERR_PTR(-EINVAL);
456 * Pinctrl_ops handling
459 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
461 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
463 return info->ngroups;
466 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
469 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
471 return info->groups[selector].name;
474 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
475 unsigned selector, const unsigned **pins,
478 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
480 if (selector >= info->ngroups)
483 *pins = info->groups[selector].pins;
484 *npins = info->groups[selector].npins;
489 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
490 struct device_node *np,
491 struct pinctrl_map **map, unsigned *num_maps)
493 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
494 const struct rockchip_pin_group *grp;
495 struct pinctrl_map *new_map;
496 struct device_node *parent;
501 * first find the group of this node and check if we need to create
502 * config maps for pins
504 grp = pinctrl_name_to_group(info, np->name);
506 dev_err(info->dev, "unable to find group for node %s\n",
511 map_num += grp->npins;
513 new_map = kcalloc(map_num, sizeof(*new_map), GFP_KERNEL);
521 parent = of_get_parent(np);
526 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
527 new_map[0].data.mux.function = parent->name;
528 new_map[0].data.mux.group = np->name;
531 /* create config map */
533 for (i = 0; i < grp->npins; i++) {
534 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
535 new_map[i].data.configs.group_or_pin =
536 pin_get_name(pctldev, grp->pins[i]);
537 new_map[i].data.configs.configs = grp->data[i].configs;
538 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
541 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
542 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
547 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
548 struct pinctrl_map *map, unsigned num_maps)
553 static const struct pinctrl_ops rockchip_pctrl_ops = {
554 .get_groups_count = rockchip_get_groups_count,
555 .get_group_name = rockchip_get_group_name,
556 .get_group_pins = rockchip_get_group_pins,
557 .dt_node_to_map = rockchip_dt_node_to_map,
558 .dt_free_map = rockchip_dt_free_map,
565 static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = {
629 static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = {
663 static struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
685 static void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
686 int *reg, u8 *bit, int *mask)
688 struct rockchip_pinctrl *info = bank->drvdata;
689 struct rockchip_pin_ctrl *ctrl = info->ctrl;
690 struct rockchip_mux_recalced_data *data;
693 for (i = 0; i < ctrl->niomux_recalced; i++) {
694 data = &ctrl->iomux_recalced[i];
695 if (data->num == bank->bank_num &&
700 if (i >= ctrl->niomux_recalced)
708 static struct rockchip_mux_route_data px30_mux_route_data[] = {
714 .route_offset = 0x184,
715 .route_val = BIT(16 + 7),
721 .route_offset = 0x184,
722 .route_val = BIT(16 + 7) | BIT(7),
728 .route_offset = 0x184,
729 .route_val = BIT(16 + 8),
735 .route_offset = 0x184,
736 .route_val = BIT(16 + 8) | BIT(8),
742 .route_offset = 0x184,
743 .route_val = BIT(16 + 10),
749 .route_offset = 0x184,
750 .route_val = BIT(16 + 10) | BIT(10),
756 .route_offset = 0x184,
757 .route_val = BIT(16 + 9),
763 .route_offset = 0x184,
764 .route_val = BIT(16 + 9) | BIT(9),
768 static struct rockchip_mux_route_data rk3128_mux_route_data[] = {
774 .route_offset = 0x144,
775 .route_val = BIT(16 + 3) | BIT(16 + 4),
781 .route_offset = 0x144,
782 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3),
788 .route_offset = 0x144,
789 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4),
795 .route_offset = 0x144,
796 .route_val = BIT(16 + 5),
802 .route_offset = 0x144,
803 .route_val = BIT(16 + 5) | BIT(5),
809 .route_offset = 0x144,
810 .route_val = BIT(16 + 6),
816 .route_offset = 0x144,
817 .route_val = BIT(16 + 6) | BIT(6),
821 static struct rockchip_mux_route_data rk3228_mux_route_data[] = {
827 .route_offset = 0x50,
828 .route_val = BIT(16),
834 .route_offset = 0x50,
835 .route_val = BIT(16) | BIT(0),
841 .route_offset = 0x50,
842 .route_val = BIT(16 + 1),
848 .route_offset = 0x50,
849 .route_val = BIT(16 + 1) | BIT(1),
855 .route_offset = 0x50,
856 .route_val = BIT(16 + 2),
862 .route_offset = 0x50,
863 .route_val = BIT(16 + 2) | BIT(2),
869 .route_offset = 0x50,
870 .route_val = BIT(16 + 3),
876 .route_offset = 0x50,
877 .route_val = BIT(16 + 3) | BIT(3),
883 .route_offset = 0x50,
884 .route_val = BIT(16 + 4),
890 .route_offset = 0x50,
891 .route_val = BIT(16 + 4) | BIT(4),
897 .route_offset = 0x50,
898 .route_val = BIT(16 + 5),
904 .route_offset = 0x50,
905 .route_val = BIT(16 + 5) | BIT(5),
911 .route_offset = 0x50,
912 .route_val = BIT(16 + 7),
918 .route_offset = 0x50,
919 .route_val = BIT(16 + 7) | BIT(7),
925 .route_offset = 0x50,
926 .route_val = BIT(16 + 8),
932 .route_offset = 0x50,
933 .route_val = BIT(16 + 8) | BIT(8),
939 .route_offset = 0x50,
940 .route_val = BIT(16 + 11),
946 .route_offset = 0x50,
947 .route_val = BIT(16 + 11) | BIT(11),
951 static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
953 /* edphdmi_cecinoutt1 */
957 .route_offset = 0x264,
958 .route_val = BIT(16 + 12) | BIT(12),
960 /* edphdmi_cecinout */
964 .route_offset = 0x264,
965 .route_val = BIT(16 + 12),
969 static struct rockchip_mux_route_data rk3328_mux_route_data[] = {
975 .route_offset = 0x50,
976 .route_val = BIT(16) | BIT(16 + 1),
982 .route_offset = 0x50,
983 .route_val = BIT(16) | BIT(16 + 1) | BIT(0),
989 .route_offset = 0x50,
990 .route_val = BIT(16 + 2) | BIT(2),
992 /* gmac-m1-optimized_rxd3 */
996 .route_offset = 0x50,
997 .route_val = BIT(16 + 10) | BIT(10),
1003 .route_offset = 0x50,
1004 .route_val = BIT(16 + 3),
1010 .route_offset = 0x50,
1011 .route_val = BIT(16 + 3) | BIT(3),
1017 .route_offset = 0x50,
1018 .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5),
1024 .route_offset = 0x50,
1025 .route_val = BIT(16 + 6),
1031 .route_offset = 0x50,
1032 .route_val = BIT(16 + 6) | BIT(6),
1038 .route_offset = 0x50,
1039 .route_val = BIT(16 + 7) | BIT(7),
1045 .route_offset = 0x50,
1046 .route_val = BIT(16 + 8) | BIT(8),
1052 .route_offset = 0x50,
1053 .route_val = BIT(16 + 9) | BIT(9),
1057 static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
1063 .route_offset = 0xe21c,
1064 .route_val = BIT(16 + 10) | BIT(16 + 11),
1070 .route_offset = 0xe21c,
1071 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
1077 .route_offset = 0xe21c,
1078 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
1084 .route_offset = 0xe21c,
1085 .route_val = BIT(16 + 14),
1091 .route_offset = 0xe21c,
1092 .route_val = BIT(16 + 14) | BIT(14),
1096 static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
1097 int mux, u32 *reg, u32 *value)
1099 struct rockchip_pinctrl *info = bank->drvdata;
1100 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1101 struct rockchip_mux_route_data *data;
1104 for (i = 0; i < ctrl->niomux_routes; i++) {
1105 data = &ctrl->iomux_routes[i];
1106 if ((data->bank_num == bank->bank_num) &&
1107 (data->pin == pin) && (data->func == mux))
1111 if (i >= ctrl->niomux_routes)
1114 *reg = data->route_offset;
1115 *value = data->route_val;
1120 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
1122 struct rockchip_pinctrl *info = bank->drvdata;
1123 int iomux_num = (pin / 8);
1124 struct regmap *regmap;
1126 int reg, ret, mask, mux_type;
1132 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1133 dev_err(info->dev, "pin %d is unrouted\n", pin);
1137 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1138 return RK_FUNC_GPIO;
1140 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1141 ? info->regmap_pmu : info->regmap_base;
1143 /* get basic quadrupel of mux registers and the correct reg inside */
1144 mux_type = bank->iomux[iomux_num].type;
1145 reg = bank->iomux[iomux_num].offset;
1146 if (mux_type & IOMUX_WIDTH_4BIT) {
1149 bit = (pin % 4) * 4;
1151 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1154 bit = (pin % 8 % 5) * 3;
1157 bit = (pin % 8) * 2;
1161 if (bank->recalced_mask & BIT(pin))
1162 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1164 ret = regmap_read(regmap, reg, &val);
1168 return ((val >> bit) & mask);
1171 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
1174 struct rockchip_pinctrl *info = bank->drvdata;
1175 int iomux_num = (pin / 8);
1180 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1181 dev_err(info->dev, "pin %d is unrouted\n", pin);
1185 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
1186 if (mux != RK_FUNC_GPIO) {
1188 "pin %d only supports a gpio mux\n", pin);
1197 * Set a new mux function for a pin.
1199 * The register is divided into the upper and lower 16 bit. When changing
1200 * a value, the previous register value is not read and changed. Instead
1201 * it seems the changed bits are marked in the upper 16 bit, while the
1202 * changed value gets set in the same offset in the lower 16 bit.
1203 * All pin settings seem to be 2 bit wide in both the upper and lower
1205 * @bank: pin bank to change
1206 * @pin: pin to change
1207 * @mux: new mux function to set
1209 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
1211 struct rockchip_pinctrl *info = bank->drvdata;
1212 int iomux_num = (pin / 8);
1213 struct regmap *regmap;
1214 int reg, ret, mask, mux_type;
1216 u32 data, rmask, route_reg, route_val;
1218 ret = rockchip_verify_mux(bank, pin, mux);
1222 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1225 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
1226 bank->bank_num, pin, mux);
1228 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1229 ? info->regmap_pmu : info->regmap_base;
1231 /* get basic quadrupel of mux registers and the correct reg inside */
1232 mux_type = bank->iomux[iomux_num].type;
1233 reg = bank->iomux[iomux_num].offset;
1234 if (mux_type & IOMUX_WIDTH_4BIT) {
1237 bit = (pin % 4) * 4;
1239 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1242 bit = (pin % 8 % 5) * 3;
1245 bit = (pin % 8) * 2;
1249 if (bank->recalced_mask & BIT(pin))
1250 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1252 if (bank->route_mask & BIT(pin)) {
1253 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
1255 ret = regmap_write(regmap, route_reg, route_val);
1261 data = (mask << (bit + 16));
1262 rmask = data | (data >> 16);
1263 data |= (mux & mask) << bit;
1264 ret = regmap_update_bits(regmap, reg, rmask, data);
1269 #define PX30_PULL_PMU_OFFSET 0x10
1270 #define PX30_PULL_GRF_OFFSET 0x60
1271 #define PX30_PULL_BITS_PER_PIN 2
1272 #define PX30_PULL_PINS_PER_REG 8
1273 #define PX30_PULL_BANK_STRIDE 16
1275 static void px30_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1276 int pin_num, struct regmap **regmap,
1279 struct rockchip_pinctrl *info = bank->drvdata;
1281 /* The first 32 pins of the first bank are located in PMU */
1282 if (bank->bank_num == 0) {
1283 *regmap = info->regmap_pmu;
1284 *reg = PX30_PULL_PMU_OFFSET;
1286 *regmap = info->regmap_base;
1287 *reg = PX30_PULL_GRF_OFFSET;
1289 /* correct the offset, as we're starting with the 2nd bank */
1291 *reg += bank->bank_num * PX30_PULL_BANK_STRIDE;
1294 *reg += ((pin_num / PX30_PULL_PINS_PER_REG) * 4);
1295 *bit = (pin_num % PX30_PULL_PINS_PER_REG);
1296 *bit *= PX30_PULL_BITS_PER_PIN;
1299 #define PX30_DRV_PMU_OFFSET 0x20
1300 #define PX30_DRV_GRF_OFFSET 0xf0
1301 #define PX30_DRV_BITS_PER_PIN 2
1302 #define PX30_DRV_PINS_PER_REG 8
1303 #define PX30_DRV_BANK_STRIDE 16
1305 static void px30_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1306 int pin_num, struct regmap **regmap,
1309 struct rockchip_pinctrl *info = bank->drvdata;
1311 /* The first 32 pins of the first bank are located in PMU */
1312 if (bank->bank_num == 0) {
1313 *regmap = info->regmap_pmu;
1314 *reg = PX30_DRV_PMU_OFFSET;
1316 *regmap = info->regmap_base;
1317 *reg = PX30_DRV_GRF_OFFSET;
1319 /* correct the offset, as we're starting with the 2nd bank */
1321 *reg += bank->bank_num * PX30_DRV_BANK_STRIDE;
1324 *reg += ((pin_num / PX30_DRV_PINS_PER_REG) * 4);
1325 *bit = (pin_num % PX30_DRV_PINS_PER_REG);
1326 *bit *= PX30_DRV_BITS_PER_PIN;
1329 #define PX30_SCHMITT_PMU_OFFSET 0x38
1330 #define PX30_SCHMITT_GRF_OFFSET 0xc0
1331 #define PX30_SCHMITT_PINS_PER_PMU_REG 16
1332 #define PX30_SCHMITT_BANK_STRIDE 16
1333 #define PX30_SCHMITT_PINS_PER_GRF_REG 8
1335 static int px30_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1337 struct regmap **regmap,
1340 struct rockchip_pinctrl *info = bank->drvdata;
1343 if (bank->bank_num == 0) {
1344 *regmap = info->regmap_pmu;
1345 *reg = PX30_SCHMITT_PMU_OFFSET;
1346 pins_per_reg = PX30_SCHMITT_PINS_PER_PMU_REG;
1348 *regmap = info->regmap_base;
1349 *reg = PX30_SCHMITT_GRF_OFFSET;
1350 pins_per_reg = PX30_SCHMITT_PINS_PER_GRF_REG;
1351 *reg += (bank->bank_num - 1) * PX30_SCHMITT_BANK_STRIDE;
1354 *reg += ((pin_num / pins_per_reg) * 4);
1355 *bit = pin_num % pins_per_reg;
1360 #define RV1108_PULL_PMU_OFFSET 0x10
1361 #define RV1108_PULL_OFFSET 0x110
1362 #define RV1108_PULL_PINS_PER_REG 8
1363 #define RV1108_PULL_BITS_PER_PIN 2
1364 #define RV1108_PULL_BANK_STRIDE 16
1366 static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1367 int pin_num, struct regmap **regmap,
1370 struct rockchip_pinctrl *info = bank->drvdata;
1372 /* The first 24 pins of the first bank are located in PMU */
1373 if (bank->bank_num == 0) {
1374 *regmap = info->regmap_pmu;
1375 *reg = RV1108_PULL_PMU_OFFSET;
1377 *reg = RV1108_PULL_OFFSET;
1378 *regmap = info->regmap_base;
1379 /* correct the offset, as we're starting with the 2nd bank */
1381 *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
1384 *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
1385 *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
1386 *bit *= RV1108_PULL_BITS_PER_PIN;
1389 #define RV1108_DRV_PMU_OFFSET 0x20
1390 #define RV1108_DRV_GRF_OFFSET 0x210
1391 #define RV1108_DRV_BITS_PER_PIN 2
1392 #define RV1108_DRV_PINS_PER_REG 8
1393 #define RV1108_DRV_BANK_STRIDE 16
1395 static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1396 int pin_num, struct regmap **regmap,
1399 struct rockchip_pinctrl *info = bank->drvdata;
1401 /* The first 24 pins of the first bank are located in PMU */
1402 if (bank->bank_num == 0) {
1403 *regmap = info->regmap_pmu;
1404 *reg = RV1108_DRV_PMU_OFFSET;
1406 *regmap = info->regmap_base;
1407 *reg = RV1108_DRV_GRF_OFFSET;
1409 /* correct the offset, as we're starting with the 2nd bank */
1411 *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
1414 *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
1415 *bit = pin_num % RV1108_DRV_PINS_PER_REG;
1416 *bit *= RV1108_DRV_BITS_PER_PIN;
1419 #define RV1108_SCHMITT_PMU_OFFSET 0x30
1420 #define RV1108_SCHMITT_GRF_OFFSET 0x388
1421 #define RV1108_SCHMITT_BANK_STRIDE 8
1422 #define RV1108_SCHMITT_PINS_PER_GRF_REG 16
1423 #define RV1108_SCHMITT_PINS_PER_PMU_REG 8
1425 static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1427 struct regmap **regmap,
1430 struct rockchip_pinctrl *info = bank->drvdata;
1433 if (bank->bank_num == 0) {
1434 *regmap = info->regmap_pmu;
1435 *reg = RV1108_SCHMITT_PMU_OFFSET;
1436 pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG;
1438 *regmap = info->regmap_base;
1439 *reg = RV1108_SCHMITT_GRF_OFFSET;
1440 pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG;
1441 *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE;
1443 *reg += ((pin_num / pins_per_reg) * 4);
1444 *bit = pin_num % pins_per_reg;
1449 #define RK2928_PULL_OFFSET 0x118
1450 #define RK2928_PULL_PINS_PER_REG 16
1451 #define RK2928_PULL_BANK_STRIDE 8
1453 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1454 int pin_num, struct regmap **regmap,
1457 struct rockchip_pinctrl *info = bank->drvdata;
1459 *regmap = info->regmap_base;
1460 *reg = RK2928_PULL_OFFSET;
1461 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1462 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
1464 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1467 #define RK3128_PULL_OFFSET 0x118
1469 static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1470 int pin_num, struct regmap **regmap,
1473 struct rockchip_pinctrl *info = bank->drvdata;
1475 *regmap = info->regmap_base;
1476 *reg = RK3128_PULL_OFFSET;
1477 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1478 *reg += ((pin_num / RK2928_PULL_PINS_PER_REG) * 4);
1480 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1483 #define RK3188_PULL_OFFSET 0x164
1484 #define RK3188_PULL_BITS_PER_PIN 2
1485 #define RK3188_PULL_PINS_PER_REG 8
1486 #define RK3188_PULL_BANK_STRIDE 16
1487 #define RK3188_PULL_PMU_OFFSET 0x64
1489 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1490 int pin_num, struct regmap **regmap,
1493 struct rockchip_pinctrl *info = bank->drvdata;
1495 /* The first 12 pins of the first bank are located elsewhere */
1496 if (bank->bank_num == 0 && pin_num < 12) {
1497 *regmap = info->regmap_pmu ? info->regmap_pmu
1498 : bank->regmap_pull;
1499 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
1500 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1501 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1502 *bit *= RK3188_PULL_BITS_PER_PIN;
1504 *regmap = info->regmap_pull ? info->regmap_pull
1505 : info->regmap_base;
1506 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
1508 /* correct the offset, as it is the 2nd pull register */
1510 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1511 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1514 * The bits in these registers have an inverse ordering
1515 * with the lowest pin being in bits 15:14 and the highest
1518 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
1519 *bit *= RK3188_PULL_BITS_PER_PIN;
1523 #define RK3288_PULL_OFFSET 0x140
1524 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1525 int pin_num, struct regmap **regmap,
1528 struct rockchip_pinctrl *info = bank->drvdata;
1530 /* The first 24 pins of the first bank are located in PMU */
1531 if (bank->bank_num == 0) {
1532 *regmap = info->regmap_pmu;
1533 *reg = RK3188_PULL_PMU_OFFSET;
1535 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1536 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1537 *bit *= RK3188_PULL_BITS_PER_PIN;
1539 *regmap = info->regmap_base;
1540 *reg = RK3288_PULL_OFFSET;
1542 /* correct the offset, as we're starting with the 2nd bank */
1544 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1545 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1547 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1548 *bit *= RK3188_PULL_BITS_PER_PIN;
1552 #define RK3288_DRV_PMU_OFFSET 0x70
1553 #define RK3288_DRV_GRF_OFFSET 0x1c0
1554 #define RK3288_DRV_BITS_PER_PIN 2
1555 #define RK3288_DRV_PINS_PER_REG 8
1556 #define RK3288_DRV_BANK_STRIDE 16
1558 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1559 int pin_num, struct regmap **regmap,
1562 struct rockchip_pinctrl *info = bank->drvdata;
1564 /* The first 24 pins of the first bank are located in PMU */
1565 if (bank->bank_num == 0) {
1566 *regmap = info->regmap_pmu;
1567 *reg = RK3288_DRV_PMU_OFFSET;
1569 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1570 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1571 *bit *= RK3288_DRV_BITS_PER_PIN;
1573 *regmap = info->regmap_base;
1574 *reg = RK3288_DRV_GRF_OFFSET;
1576 /* correct the offset, as we're starting with the 2nd bank */
1578 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1579 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1581 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1582 *bit *= RK3288_DRV_BITS_PER_PIN;
1586 #define RK3228_PULL_OFFSET 0x100
1588 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1589 int pin_num, struct regmap **regmap,
1592 struct rockchip_pinctrl *info = bank->drvdata;
1594 *regmap = info->regmap_base;
1595 *reg = RK3228_PULL_OFFSET;
1596 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1597 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1599 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1600 *bit *= RK3188_PULL_BITS_PER_PIN;
1603 #define RK3228_DRV_GRF_OFFSET 0x200
1605 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1606 int pin_num, struct regmap **regmap,
1609 struct rockchip_pinctrl *info = bank->drvdata;
1611 *regmap = info->regmap_base;
1612 *reg = RK3228_DRV_GRF_OFFSET;
1613 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1614 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1616 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1617 *bit *= RK3288_DRV_BITS_PER_PIN;
1620 #define RK3368_PULL_GRF_OFFSET 0x100
1621 #define RK3368_PULL_PMU_OFFSET 0x10
1623 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1624 int pin_num, struct regmap **regmap,
1627 struct rockchip_pinctrl *info = bank->drvdata;
1629 /* The first 32 pins of the first bank are located in PMU */
1630 if (bank->bank_num == 0) {
1631 *regmap = info->regmap_pmu;
1632 *reg = RK3368_PULL_PMU_OFFSET;
1634 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1635 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1636 *bit *= RK3188_PULL_BITS_PER_PIN;
1638 *regmap = info->regmap_base;
1639 *reg = RK3368_PULL_GRF_OFFSET;
1641 /* correct the offset, as we're starting with the 2nd bank */
1643 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1644 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1646 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1647 *bit *= RK3188_PULL_BITS_PER_PIN;
1651 #define RK3368_DRV_PMU_OFFSET 0x20
1652 #define RK3368_DRV_GRF_OFFSET 0x200
1654 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1655 int pin_num, struct regmap **regmap,
1658 struct rockchip_pinctrl *info = bank->drvdata;
1660 /* The first 32 pins of the first bank are located in PMU */
1661 if (bank->bank_num == 0) {
1662 *regmap = info->regmap_pmu;
1663 *reg = RK3368_DRV_PMU_OFFSET;
1665 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1666 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1667 *bit *= RK3288_DRV_BITS_PER_PIN;
1669 *regmap = info->regmap_base;
1670 *reg = RK3368_DRV_GRF_OFFSET;
1672 /* correct the offset, as we're starting with the 2nd bank */
1674 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1675 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1677 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1678 *bit *= RK3288_DRV_BITS_PER_PIN;
1682 #define RK3399_PULL_GRF_OFFSET 0xe040
1683 #define RK3399_PULL_PMU_OFFSET 0x40
1684 #define RK3399_DRV_3BITS_PER_PIN 3
1686 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1687 int pin_num, struct regmap **regmap,
1690 struct rockchip_pinctrl *info = bank->drvdata;
1692 /* The bank0:16 and bank1:32 pins are located in PMU */
1693 if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
1694 *regmap = info->regmap_pmu;
1695 *reg = RK3399_PULL_PMU_OFFSET;
1697 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1699 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1700 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1701 *bit *= RK3188_PULL_BITS_PER_PIN;
1703 *regmap = info->regmap_base;
1704 *reg = RK3399_PULL_GRF_OFFSET;
1706 /* correct the offset, as we're starting with the 3rd bank */
1708 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1709 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1711 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1712 *bit *= RK3188_PULL_BITS_PER_PIN;
1716 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1717 int pin_num, struct regmap **regmap,
1720 struct rockchip_pinctrl *info = bank->drvdata;
1721 int drv_num = (pin_num / 8);
1723 /* The bank0:16 and bank1:32 pins are located in PMU */
1724 if ((bank->bank_num == 0) || (bank->bank_num == 1))
1725 *regmap = info->regmap_pmu;
1727 *regmap = info->regmap_base;
1729 *reg = bank->drv[drv_num].offset;
1730 if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
1731 (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
1732 *bit = (pin_num % 8) * 3;
1734 *bit = (pin_num % 8) * 2;
1737 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
1738 { 2, 4, 8, 12, -1, -1, -1, -1 },
1739 { 3, 6, 9, 12, -1, -1, -1, -1 },
1740 { 5, 10, 15, 20, -1, -1, -1, -1 },
1741 { 4, 6, 8, 10, 12, 14, 16, 18 },
1742 { 4, 7, 10, 13, 16, 19, 22, 26 }
1745 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
1748 struct rockchip_pinctrl *info = bank->drvdata;
1749 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1750 struct regmap *regmap;
1752 u32 data, temp, rmask_bits;
1754 int drv_type = bank->drv[pin_num / 8].drv_type;
1756 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1759 case DRV_TYPE_IO_1V8_3V0_AUTO:
1760 case DRV_TYPE_IO_3V3_ONLY:
1761 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1764 /* regular case, nothing to do */
1768 * drive-strength offset is special, as it is
1769 * spread over 2 registers
1771 ret = regmap_read(regmap, reg, &data);
1775 ret = regmap_read(regmap, reg + 0x4, &temp);
1780 * the bit data[15] contains bit 0 of the value
1781 * while temp[1:0] contains bits 2 and 1
1788 return rockchip_perpin_drv_list[drv_type][data];
1790 /* setting fully enclosed in the second register */
1795 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1801 case DRV_TYPE_IO_DEFAULT:
1802 case DRV_TYPE_IO_1V8_OR_3V0:
1803 case DRV_TYPE_IO_1V8_ONLY:
1804 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1807 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1812 ret = regmap_read(regmap, reg, &data);
1817 data &= (1 << rmask_bits) - 1;
1819 return rockchip_perpin_drv_list[drv_type][data];
1822 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
1823 int pin_num, int strength)
1825 struct rockchip_pinctrl *info = bank->drvdata;
1826 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1827 struct regmap *regmap;
1829 u32 data, rmask, rmask_bits, temp;
1831 int drv_type = bank->drv[pin_num / 8].drv_type;
1833 dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
1834 bank->bank_num, pin_num, strength);
1836 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1839 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
1840 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
1843 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
1844 ret = rockchip_perpin_drv_list[drv_type][i];
1850 dev_err(info->dev, "unsupported driver strength %d\n",
1856 case DRV_TYPE_IO_1V8_3V0_AUTO:
1857 case DRV_TYPE_IO_3V3_ONLY:
1858 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1861 /* regular case, nothing to do */
1865 * drive-strength offset is special, as it is spread
1866 * over 2 registers, the bit data[15] contains bit 0
1867 * of the value while temp[1:0] contains bits 2 and 1
1869 data = (ret & 0x1) << 15;
1870 temp = (ret >> 0x1) & 0x3;
1872 rmask = BIT(15) | BIT(31);
1874 ret = regmap_update_bits(regmap, reg, rmask, data);
1878 rmask = 0x3 | (0x3 << 16);
1879 temp |= (0x3 << 16);
1881 ret = regmap_update_bits(regmap, reg, rmask, temp);
1885 /* setting fully enclosed in the second register */
1890 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1895 case DRV_TYPE_IO_DEFAULT:
1896 case DRV_TYPE_IO_1V8_OR_3V0:
1897 case DRV_TYPE_IO_1V8_ONLY:
1898 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1901 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1906 /* enable the write to the equivalent lower bits */
1907 data = ((1 << rmask_bits) - 1) << (bit + 16);
1908 rmask = data | (data >> 16);
1909 data |= (ret << bit);
1911 ret = regmap_update_bits(regmap, reg, rmask, data);
1916 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
1918 PIN_CONFIG_BIAS_DISABLE,
1919 PIN_CONFIG_BIAS_PULL_UP,
1920 PIN_CONFIG_BIAS_PULL_DOWN,
1921 PIN_CONFIG_BIAS_BUS_HOLD
1924 PIN_CONFIG_BIAS_DISABLE,
1925 PIN_CONFIG_BIAS_PULL_DOWN,
1926 PIN_CONFIG_BIAS_DISABLE,
1927 PIN_CONFIG_BIAS_PULL_UP
1931 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
1933 struct rockchip_pinctrl *info = bank->drvdata;
1934 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1935 struct regmap *regmap;
1936 int reg, ret, pull_type;
1940 /* rk3066b does support any pulls */
1941 if (ctrl->type == RK3066B)
1942 return PIN_CONFIG_BIAS_DISABLE;
1944 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1946 ret = regmap_read(regmap, reg, &data);
1950 switch (ctrl->type) {
1953 return !(data & BIT(bit))
1954 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
1955 : PIN_CONFIG_BIAS_DISABLE;
1962 pull_type = bank->pull_type[pin_num / 8];
1964 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
1966 return rockchip_pull_list[pull_type][data];
1968 dev_err(info->dev, "unsupported pinctrl type\n");
1973 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
1974 int pin_num, int pull)
1976 struct rockchip_pinctrl *info = bank->drvdata;
1977 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1978 struct regmap *regmap;
1979 int reg, ret, i, pull_type;
1983 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
1984 bank->bank_num, pin_num, pull);
1986 /* rk3066b does support any pulls */
1987 if (ctrl->type == RK3066B)
1988 return pull ? -EINVAL : 0;
1990 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1992 switch (ctrl->type) {
1995 data = BIT(bit + 16);
1996 if (pull == PIN_CONFIG_BIAS_DISABLE)
1998 ret = regmap_write(regmap, reg, data);
2006 pull_type = bank->pull_type[pin_num / 8];
2008 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
2010 if (rockchip_pull_list[pull_type][i] == pull) {
2017 dev_err(info->dev, "unsupported pull setting %d\n",
2022 /* enable the write to the equivalent lower bits */
2023 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
2024 rmask = data | (data >> 16);
2025 data |= (ret << bit);
2027 ret = regmap_update_bits(regmap, reg, rmask, data);
2030 dev_err(info->dev, "unsupported pinctrl type\n");
2037 #define RK3328_SCHMITT_BITS_PER_PIN 1
2038 #define RK3328_SCHMITT_PINS_PER_REG 16
2039 #define RK3328_SCHMITT_BANK_STRIDE 8
2040 #define RK3328_SCHMITT_GRF_OFFSET 0x380
2042 static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
2044 struct regmap **regmap,
2047 struct rockchip_pinctrl *info = bank->drvdata;
2049 *regmap = info->regmap_base;
2050 *reg = RK3328_SCHMITT_GRF_OFFSET;
2052 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
2053 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
2054 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
2059 static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
2061 struct rockchip_pinctrl *info = bank->drvdata;
2062 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2063 struct regmap *regmap;
2068 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2072 ret = regmap_read(regmap, reg, &data);
2080 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
2081 int pin_num, int enable)
2083 struct rockchip_pinctrl *info = bank->drvdata;
2084 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2085 struct regmap *regmap;
2090 dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
2091 bank->bank_num, pin_num, enable);
2093 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2097 /* enable the write to the equivalent lower bits */
2098 data = BIT(bit + 16) | (enable << bit);
2099 rmask = BIT(bit + 16) | BIT(bit);
2101 return regmap_update_bits(regmap, reg, rmask, data);
2105 * Pinmux_ops handling
2108 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
2110 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2112 return info->nfunctions;
2115 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
2118 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2120 return info->functions[selector].name;
2123 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
2124 unsigned selector, const char * const **groups,
2125 unsigned * const num_groups)
2127 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2129 *groups = info->functions[selector].groups;
2130 *num_groups = info->functions[selector].ngroups;
2135 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
2138 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2139 const unsigned int *pins = info->groups[group].pins;
2140 const struct rockchip_pin_config *data = info->groups[group].data;
2141 struct rockchip_pin_bank *bank;
2144 dev_dbg(info->dev, "enable function %s group %s\n",
2145 info->functions[selector].name, info->groups[group].name);
2148 * for each pin in the pin group selected, program the corresponding
2149 * pin function number in the config register.
2151 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
2152 bank = pin_to_bank(info, pins[cnt]);
2153 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
2160 /* revert the already done pin settings */
2161 for (cnt--; cnt >= 0; cnt--)
2162 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
2170 static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
2172 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
2176 ret = clk_enable(bank->clk);
2178 dev_err(bank->drvdata->dev,
2179 "failed to enable clock for bank %s\n", bank->name);
2182 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2183 clk_disable(bank->clk);
2185 return !(data & BIT(offset));
2189 * The calls to gpio_direction_output() and gpio_direction_input()
2190 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
2191 * function called from the gpiolib interface).
2193 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
2194 int pin, bool input)
2196 struct rockchip_pin_bank *bank;
2198 unsigned long flags;
2201 bank = gpiochip_get_data(chip);
2203 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
2207 clk_enable(bank->clk);
2208 raw_spin_lock_irqsave(&bank->slock, flags);
2210 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2211 /* set bit to 1 for output, 0 for input */
2216 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2218 raw_spin_unlock_irqrestore(&bank->slock, flags);
2219 clk_disable(bank->clk);
2224 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
2225 struct pinctrl_gpio_range *range,
2226 unsigned offset, bool input)
2228 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2229 struct gpio_chip *chip;
2233 pin = offset - chip->base;
2234 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
2235 offset, range->name, pin, input ? "input" : "output");
2237 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
2241 static const struct pinmux_ops rockchip_pmx_ops = {
2242 .get_functions_count = rockchip_pmx_get_funcs_count,
2243 .get_function_name = rockchip_pmx_get_func_name,
2244 .get_function_groups = rockchip_pmx_get_groups,
2245 .set_mux = rockchip_pmx_set,
2246 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
2250 * Pinconf_ops handling
2253 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
2254 enum pin_config_param pull)
2256 switch (ctrl->type) {
2259 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
2260 pull == PIN_CONFIG_BIAS_DISABLE);
2262 return pull ? false : true;
2269 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
2275 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
2276 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
2278 /* set the pin config settings for a specified pin */
2279 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
2280 unsigned long *configs, unsigned num_configs)
2282 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2283 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2284 enum pin_config_param param;
2289 for (i = 0; i < num_configs; i++) {
2290 param = pinconf_to_config_param(configs[i]);
2291 arg = pinconf_to_config_argument(configs[i]);
2294 case PIN_CONFIG_BIAS_DISABLE:
2295 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2300 case PIN_CONFIG_BIAS_PULL_UP:
2301 case PIN_CONFIG_BIAS_PULL_DOWN:
2302 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2303 case PIN_CONFIG_BIAS_BUS_HOLD:
2304 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2310 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2315 case PIN_CONFIG_OUTPUT:
2316 rockchip_gpio_set(&bank->gpio_chip,
2317 pin - bank->pin_base, arg);
2318 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
2319 pin - bank->pin_base, false);
2323 case PIN_CONFIG_DRIVE_STRENGTH:
2324 /* rk3288 is the first with per-pin drive-strength */
2325 if (!info->ctrl->drv_calc_reg)
2328 rc = rockchip_set_drive_perpin(bank,
2329 pin - bank->pin_base, arg);
2333 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2334 if (!info->ctrl->schmitt_calc_reg)
2337 rc = rockchip_set_schmitt(bank,
2338 pin - bank->pin_base, arg);
2346 } /* for each config */
2351 /* get the pin config settings for a specified pin */
2352 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
2353 unsigned long *config)
2355 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2356 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2357 enum pin_config_param param = pinconf_to_config_param(*config);
2362 case PIN_CONFIG_BIAS_DISABLE:
2363 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2368 case PIN_CONFIG_BIAS_PULL_UP:
2369 case PIN_CONFIG_BIAS_PULL_DOWN:
2370 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2371 case PIN_CONFIG_BIAS_BUS_HOLD:
2372 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2375 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2380 case PIN_CONFIG_OUTPUT:
2381 rc = rockchip_get_mux(bank, pin - bank->pin_base);
2382 if (rc != RK_FUNC_GPIO)
2385 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
2391 case PIN_CONFIG_DRIVE_STRENGTH:
2392 /* rk3288 is the first with per-pin drive-strength */
2393 if (!info->ctrl->drv_calc_reg)
2396 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
2402 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2403 if (!info->ctrl->schmitt_calc_reg)
2406 rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
2417 *config = pinconf_to_config_packed(param, arg);
2422 static const struct pinconf_ops rockchip_pinconf_ops = {
2423 .pin_config_get = rockchip_pinconf_get,
2424 .pin_config_set = rockchip_pinconf_set,
2428 static const struct of_device_id rockchip_bank_match[] = {
2429 { .compatible = "rockchip,gpio-bank" },
2430 { .compatible = "rockchip,rk3188-gpio-bank0" },
2434 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
2435 struct device_node *np)
2437 struct device_node *child;
2439 for_each_child_of_node(np, child) {
2440 if (of_match_node(rockchip_bank_match, child))
2444 info->ngroups += of_get_child_count(child);
2448 static int rockchip_pinctrl_parse_groups(struct device_node *np,
2449 struct rockchip_pin_group *grp,
2450 struct rockchip_pinctrl *info,
2453 struct rockchip_pin_bank *bank;
2460 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
2462 /* Initialise group */
2463 grp->name = np->name;
2466 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
2467 * do sanity check and calculate pins number
2469 list = of_get_property(np, "rockchip,pins", &size);
2470 /* we do not check return since it's safe node passed down */
2471 size /= sizeof(*list);
2472 if (!size || size % 4) {
2473 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
2477 grp->npins = size / 4;
2479 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
2481 grp->data = devm_kcalloc(info->dev,
2483 sizeof(struct rockchip_pin_config),
2485 if (!grp->pins || !grp->data)
2488 for (i = 0, j = 0; i < size; i += 4, j++) {
2489 const __be32 *phandle;
2490 struct device_node *np_config;
2492 num = be32_to_cpu(*list++);
2493 bank = bank_num_to_bank(info, num);
2495 return PTR_ERR(bank);
2497 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
2498 grp->data[j].func = be32_to_cpu(*list++);
2504 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
2505 ret = pinconf_generic_parse_dt_config(np_config, NULL,
2506 &grp->data[j].configs, &grp->data[j].nconfigs);
2507 of_node_put(np_config);
2515 static int rockchip_pinctrl_parse_functions(struct device_node *np,
2516 struct rockchip_pinctrl *info,
2519 struct device_node *child;
2520 struct rockchip_pmx_func *func;
2521 struct rockchip_pin_group *grp;
2523 static u32 grp_index;
2526 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
2528 func = &info->functions[index];
2530 /* Initialise function */
2531 func->name = np->name;
2532 func->ngroups = of_get_child_count(np);
2533 if (func->ngroups <= 0)
2536 func->groups = devm_kcalloc(info->dev,
2537 func->ngroups, sizeof(char *), GFP_KERNEL);
2541 for_each_child_of_node(np, child) {
2542 func->groups[i] = child->name;
2543 grp = &info->groups[grp_index++];
2544 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
2554 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
2555 struct rockchip_pinctrl *info)
2557 struct device *dev = &pdev->dev;
2558 struct device_node *np = dev->of_node;
2559 struct device_node *child;
2563 rockchip_pinctrl_child_count(info, np);
2565 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
2566 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
2568 info->functions = devm_kcalloc(dev,
2570 sizeof(struct rockchip_pmx_func),
2572 if (!info->functions)
2575 info->groups = devm_kcalloc(dev,
2577 sizeof(struct rockchip_pin_group),
2584 for_each_child_of_node(np, child) {
2585 if (of_match_node(rockchip_bank_match, child))
2588 ret = rockchip_pinctrl_parse_functions(child, info, i++);
2590 dev_err(&pdev->dev, "failed to parse function\n");
2599 static int rockchip_pinctrl_register(struct platform_device *pdev,
2600 struct rockchip_pinctrl *info)
2602 struct pinctrl_desc *ctrldesc = &info->pctl;
2603 struct pinctrl_pin_desc *pindesc, *pdesc;
2604 struct rockchip_pin_bank *pin_bank;
2608 ctrldesc->name = "rockchip-pinctrl";
2609 ctrldesc->owner = THIS_MODULE;
2610 ctrldesc->pctlops = &rockchip_pctrl_ops;
2611 ctrldesc->pmxops = &rockchip_pmx_ops;
2612 ctrldesc->confops = &rockchip_pinconf_ops;
2614 pindesc = devm_kcalloc(&pdev->dev,
2615 info->ctrl->nr_pins, sizeof(*pindesc),
2620 ctrldesc->pins = pindesc;
2621 ctrldesc->npins = info->ctrl->nr_pins;
2624 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
2625 pin_bank = &info->ctrl->pin_banks[bank];
2626 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
2628 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
2629 pin_bank->name, pin);
2634 ret = rockchip_pinctrl_parse_dt(pdev, info);
2638 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
2639 if (IS_ERR(info->pctl_dev)) {
2640 dev_err(&pdev->dev, "could not register pinctrl driver\n");
2641 return PTR_ERR(info->pctl_dev);
2644 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
2645 pin_bank = &info->ctrl->pin_banks[bank];
2646 pin_bank->grange.name = pin_bank->name;
2647 pin_bank->grange.id = bank;
2648 pin_bank->grange.pin_base = pin_bank->pin_base;
2649 pin_bank->grange.base = pin_bank->gpio_chip.base;
2650 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
2651 pin_bank->grange.gc = &pin_bank->gpio_chip;
2652 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
2662 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
2664 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2665 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
2666 unsigned long flags;
2669 clk_enable(bank->clk);
2670 raw_spin_lock_irqsave(&bank->slock, flags);
2673 data &= ~BIT(offset);
2675 data |= BIT(offset);
2678 raw_spin_unlock_irqrestore(&bank->slock, flags);
2679 clk_disable(bank->clk);
2683 * Returns the level of the pin for input direction and setting of the DR
2684 * register for output gpios.
2686 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
2688 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2691 clk_enable(bank->clk);
2692 data = readl(bank->reg_base + GPIO_EXT_PORT);
2693 clk_disable(bank->clk);
2700 * gpiolib gpio_direction_input callback function. The setting of the pin
2701 * mux function as 'gpio input' will be handled by the pinctrl subsystem
2704 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
2706 return pinctrl_gpio_direction_input(gc->base + offset);
2710 * gpiolib gpio_direction_output callback function. The setting of the pin
2711 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2714 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
2715 unsigned offset, int value)
2717 rockchip_gpio_set(gc, offset, value);
2718 return pinctrl_gpio_direction_output(gc->base + offset);
2721 static void rockchip_gpio_set_debounce(struct gpio_chip *gc,
2722 unsigned int offset, bool enable)
2724 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2725 void __iomem *reg = bank->reg_base + GPIO_DEBOUNCE;
2726 unsigned long flags;
2729 clk_enable(bank->clk);
2730 raw_spin_lock_irqsave(&bank->slock, flags);
2734 data |= BIT(offset);
2736 data &= ~BIT(offset);
2739 raw_spin_unlock_irqrestore(&bank->slock, flags);
2740 clk_disable(bank->clk);
2744 * gpiolib set_config callback function. The setting of the pin
2745 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2748 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
2749 unsigned long config)
2751 enum pin_config_param param = pinconf_to_config_param(config);
2754 case PIN_CONFIG_INPUT_DEBOUNCE:
2755 rockchip_gpio_set_debounce(gc, offset, true);
2757 * Rockchip's gpio could only support up to one period
2758 * of the debounce clock(pclk), which is far away from
2759 * satisftying the requirement, as pclk is usually near
2760 * 100MHz shared by all peripherals. So the fact is it
2761 * has crippled debounce capability could only be useful
2762 * to prevent any spurious glitches from waking up the system
2763 * if the gpio is conguired as wakeup interrupt source. Let's
2764 * still return -ENOTSUPP as before, to make sure the caller
2765 * of gpiod_set_debounce won't change its behaviour.
2773 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
2774 * and a virtual IRQ, if not already present.
2776 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
2778 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2784 clk_enable(bank->clk);
2785 virq = irq_create_mapping(bank->domain, offset);
2786 clk_disable(bank->clk);
2788 return (virq) ? : -ENXIO;
2791 static const struct gpio_chip rockchip_gpiolib_chip = {
2792 .request = gpiochip_generic_request,
2793 .free = gpiochip_generic_free,
2794 .set = rockchip_gpio_set,
2795 .get = rockchip_gpio_get,
2796 .get_direction = rockchip_gpio_get_direction,
2797 .direction_input = rockchip_gpio_direction_input,
2798 .direction_output = rockchip_gpio_direction_output,
2799 .set_config = rockchip_gpio_set_config,
2800 .to_irq = rockchip_gpio_to_irq,
2801 .owner = THIS_MODULE,
2805 * Interrupt handling
2808 static void rockchip_irq_demux(struct irq_desc *desc)
2810 struct irq_chip *chip = irq_desc_get_chip(desc);
2811 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
2814 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
2816 chained_irq_enter(chip, desc);
2818 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
2821 unsigned int irq, virq;
2825 virq = irq_linear_revmap(bank->domain, irq);
2828 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
2832 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
2835 * Triggering IRQ on both rising and falling edge
2836 * needs manual intervention.
2838 if (bank->toggle_edge_mode & BIT(irq)) {
2839 u32 data, data_old, polarity;
2840 unsigned long flags;
2842 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
2844 raw_spin_lock_irqsave(&bank->slock, flags);
2846 polarity = readl_relaxed(bank->reg_base +
2848 if (data & BIT(irq))
2849 polarity &= ~BIT(irq);
2851 polarity |= BIT(irq);
2853 bank->reg_base + GPIO_INT_POLARITY);
2855 raw_spin_unlock_irqrestore(&bank->slock, flags);
2858 data = readl_relaxed(bank->reg_base +
2860 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
2863 generic_handle_irq(virq);
2866 chained_irq_exit(chip, desc);
2869 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
2871 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2872 struct rockchip_pin_bank *bank = gc->private;
2873 u32 mask = BIT(d->hwirq);
2877 unsigned long flags;
2880 /* make sure the pin is configured as gpio input */
2881 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
2885 clk_enable(bank->clk);
2886 raw_spin_lock_irqsave(&bank->slock, flags);
2888 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2890 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2892 raw_spin_unlock_irqrestore(&bank->slock, flags);
2894 if (type & IRQ_TYPE_EDGE_BOTH)
2895 irq_set_handler_locked(d, handle_edge_irq);
2897 irq_set_handler_locked(d, handle_level_irq);
2899 raw_spin_lock_irqsave(&bank->slock, flags);
2902 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
2903 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
2906 case IRQ_TYPE_EDGE_BOTH:
2907 bank->toggle_edge_mode |= mask;
2911 * Determine gpio state. If 1 next interrupt should be falling
2914 data = readl(bank->reg_base + GPIO_EXT_PORT);
2920 case IRQ_TYPE_EDGE_RISING:
2921 bank->toggle_edge_mode &= ~mask;
2925 case IRQ_TYPE_EDGE_FALLING:
2926 bank->toggle_edge_mode &= ~mask;
2930 case IRQ_TYPE_LEVEL_HIGH:
2931 bank->toggle_edge_mode &= ~mask;
2935 case IRQ_TYPE_LEVEL_LOW:
2936 bank->toggle_edge_mode &= ~mask;
2942 raw_spin_unlock_irqrestore(&bank->slock, flags);
2943 clk_disable(bank->clk);
2947 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
2948 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
2951 raw_spin_unlock_irqrestore(&bank->slock, flags);
2952 clk_disable(bank->clk);
2957 static void rockchip_irq_suspend(struct irq_data *d)
2959 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2960 struct rockchip_pin_bank *bank = gc->private;
2962 clk_enable(bank->clk);
2963 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
2964 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
2965 clk_disable(bank->clk);
2968 static void rockchip_irq_resume(struct irq_data *d)
2970 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2971 struct rockchip_pin_bank *bank = gc->private;
2973 clk_enable(bank->clk);
2974 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
2975 clk_disable(bank->clk);
2978 static void rockchip_irq_enable(struct irq_data *d)
2980 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2981 struct rockchip_pin_bank *bank = gc->private;
2983 clk_enable(bank->clk);
2984 irq_gc_mask_clr_bit(d);
2987 static void rockchip_irq_disable(struct irq_data *d)
2989 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2990 struct rockchip_pin_bank *bank = gc->private;
2992 irq_gc_mask_set_bit(d);
2993 clk_disable(bank->clk);
2996 static int rockchip_interrupts_register(struct platform_device *pdev,
2997 struct rockchip_pinctrl *info)
2999 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3000 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3001 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
3002 struct irq_chip_generic *gc;
3006 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3008 dev_warn(&pdev->dev, "bank %s is not valid\n",
3013 ret = clk_enable(bank->clk);
3015 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
3020 bank->domain = irq_domain_add_linear(bank->of_node, 32,
3021 &irq_generic_chip_ops, NULL);
3022 if (!bank->domain) {
3023 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
3025 clk_disable(bank->clk);
3029 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
3030 "rockchip_gpio_irq", handle_level_irq,
3031 clr, 0, IRQ_GC_INIT_MASK_CACHE);
3033 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
3035 irq_domain_remove(bank->domain);
3036 clk_disable(bank->clk);
3041 * Linux assumes that all interrupts start out disabled/masked.
3042 * Our driver only uses the concept of masked and always keeps
3043 * things enabled, so for us that's all masked and all enabled.
3045 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
3046 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
3048 gc = irq_get_domain_generic_chip(bank->domain, 0);
3049 gc->reg_base = bank->reg_base;
3051 gc->chip_types[0].regs.mask = GPIO_INTMASK;
3052 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
3053 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
3054 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
3055 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
3056 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
3057 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
3058 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
3059 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
3060 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
3061 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
3062 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
3064 irq_set_chained_handler_and_data(bank->irq,
3065 rockchip_irq_demux, bank);
3067 /* map the gpio irqs here, when the clock is still running */
3068 for (j = 0 ; j < 32 ; j++)
3069 irq_create_mapping(bank->domain, j);
3071 clk_disable(bank->clk);
3077 static int rockchip_gpiolib_register(struct platform_device *pdev,
3078 struct rockchip_pinctrl *info)
3080 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3081 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3082 struct gpio_chip *gc;
3086 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3088 dev_warn(&pdev->dev, "bank %s is not valid\n",
3093 bank->gpio_chip = rockchip_gpiolib_chip;
3095 gc = &bank->gpio_chip;
3096 gc->base = bank->pin_base;
3097 gc->ngpio = bank->nr_pins;
3098 gc->parent = &pdev->dev;
3099 gc->of_node = bank->of_node;
3100 gc->label = bank->name;
3102 ret = gpiochip_add_data(gc, bank);
3104 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
3110 rockchip_interrupts_register(pdev, info);
3115 for (--i, --bank; i >= 0; --i, --bank) {
3118 gpiochip_remove(&bank->gpio_chip);
3123 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
3124 struct rockchip_pinctrl *info)
3126 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3127 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3130 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3133 gpiochip_remove(&bank->gpio_chip);
3139 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
3140 struct rockchip_pinctrl *info)
3142 struct resource res;
3145 if (of_address_to_resource(bank->of_node, 0, &res)) {
3146 dev_err(info->dev, "cannot find IO resource for bank\n");
3150 bank->reg_base = devm_ioremap_resource(info->dev, &res);
3151 if (IS_ERR(bank->reg_base))
3152 return PTR_ERR(bank->reg_base);
3155 * special case, where parts of the pull setting-registers are
3156 * part of the PMU register space
3158 if (of_device_is_compatible(bank->of_node,
3159 "rockchip,rk3188-gpio-bank0")) {
3160 struct device_node *node;
3162 node = of_parse_phandle(bank->of_node->parent,
3165 if (of_address_to_resource(bank->of_node, 1, &res)) {
3166 dev_err(info->dev, "cannot find IO resource for bank\n");
3170 base = devm_ioremap_resource(info->dev, &res);
3172 return PTR_ERR(base);
3173 rockchip_regmap_config.max_register =
3174 resource_size(&res) - 4;
3175 rockchip_regmap_config.name =
3176 "rockchip,rk3188-gpio-bank0-pull";
3177 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
3179 &rockchip_regmap_config);
3184 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
3186 bank->clk = of_clk_get(bank->of_node, 0);
3187 if (IS_ERR(bank->clk))
3188 return PTR_ERR(bank->clk);
3190 return clk_prepare(bank->clk);
3193 static const struct of_device_id rockchip_pinctrl_dt_match[];
3195 /* retrieve the soc specific data */
3196 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
3197 struct rockchip_pinctrl *d,
3198 struct platform_device *pdev)
3200 const struct of_device_id *match;
3201 struct device_node *node = pdev->dev.of_node;
3202 struct device_node *np;
3203 struct rockchip_pin_ctrl *ctrl;
3204 struct rockchip_pin_bank *bank;
3205 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
3207 match = of_match_node(rockchip_pinctrl_dt_match, node);
3208 ctrl = (struct rockchip_pin_ctrl *)match->data;
3210 for_each_child_of_node(node, np) {
3211 if (!of_find_property(np, "gpio-controller", NULL))
3214 bank = ctrl->pin_banks;
3215 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3216 if (!strcmp(bank->name, np->name)) {
3219 if (!rockchip_get_bank_data(bank, d))
3227 grf_offs = ctrl->grf_mux_offset;
3228 pmu_offs = ctrl->pmu_mux_offset;
3229 drv_pmu_offs = ctrl->pmu_drv_offset;
3230 drv_grf_offs = ctrl->grf_drv_offset;
3231 bank = ctrl->pin_banks;
3232 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3235 raw_spin_lock_init(&bank->slock);
3237 bank->pin_base = ctrl->nr_pins;
3238 ctrl->nr_pins += bank->nr_pins;
3240 /* calculate iomux and drv offsets */
3241 for (j = 0; j < 4; j++) {
3242 struct rockchip_iomux *iom = &bank->iomux[j];
3243 struct rockchip_drv *drv = &bank->drv[j];
3246 if (bank_pins >= bank->nr_pins)
3249 /* preset iomux offset value, set new start value */
3250 if (iom->offset >= 0) {
3251 if (iom->type & IOMUX_SOURCE_PMU)
3252 pmu_offs = iom->offset;
3254 grf_offs = iom->offset;
3255 } else { /* set current iomux offset */
3256 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3257 pmu_offs : grf_offs;
3260 /* preset drv offset value, set new start value */
3261 if (drv->offset >= 0) {
3262 if (iom->type & IOMUX_SOURCE_PMU)
3263 drv_pmu_offs = drv->offset;
3265 drv_grf_offs = drv->offset;
3266 } else { /* set current drv offset */
3267 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3268 drv_pmu_offs : drv_grf_offs;
3271 dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
3272 i, j, iom->offset, drv->offset);
3275 * Increase offset according to iomux width.
3276 * 4bit iomux'es are spread over two registers.
3278 inc = (iom->type & (IOMUX_WIDTH_4BIT |
3279 IOMUX_WIDTH_3BIT)) ? 8 : 4;
3280 if (iom->type & IOMUX_SOURCE_PMU)
3286 * Increase offset according to drv width.
3287 * 3bit drive-strenth'es are spread over two registers.
3289 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
3290 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
3295 if (iom->type & IOMUX_SOURCE_PMU)
3296 drv_pmu_offs += inc;
3298 drv_grf_offs += inc;
3303 /* calculate the per-bank recalced_mask */
3304 for (j = 0; j < ctrl->niomux_recalced; j++) {
3307 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
3308 pin = ctrl->iomux_recalced[j].pin;
3309 bank->recalced_mask |= BIT(pin);
3313 /* calculate the per-bank route_mask */
3314 for (j = 0; j < ctrl->niomux_routes; j++) {
3317 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
3318 pin = ctrl->iomux_routes[j].pin;
3319 bank->route_mask |= BIT(pin);
3327 #define RK3288_GRF_GPIO6C_IOMUX 0x64
3328 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
3330 static u32 rk3288_grf_gpio6c_iomux;
3332 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
3334 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3335 int ret = pinctrl_force_sleep(info->pctl_dev);
3341 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
3342 * the setting here, and restore it at resume.
3344 if (info->ctrl->type == RK3288) {
3345 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3346 &rk3288_grf_gpio6c_iomux);
3348 pinctrl_force_default(info->pctl_dev);
3356 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
3358 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3361 if (info->ctrl->type == RK3288) {
3362 ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3363 rk3288_grf_gpio6c_iomux |
3364 GPIO6C6_SEL_WRITE_ENABLE);
3369 return pinctrl_force_default(info->pctl_dev);
3372 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
3373 rockchip_pinctrl_resume);
3375 static int rockchip_pinctrl_probe(struct platform_device *pdev)
3377 struct rockchip_pinctrl *info;
3378 struct device *dev = &pdev->dev;
3379 struct rockchip_pin_ctrl *ctrl;
3380 struct device_node *np = pdev->dev.of_node, *node;
3381 struct resource *res;
3385 if (!dev->of_node) {
3386 dev_err(dev, "device tree node not found\n");
3390 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3396 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
3398 dev_err(dev, "driver data not available\n");
3403 node = of_parse_phandle(np, "rockchip,grf", 0);
3405 info->regmap_base = syscon_node_to_regmap(node);
3407 if (IS_ERR(info->regmap_base))
3408 return PTR_ERR(info->regmap_base);
3410 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3411 base = devm_ioremap_resource(&pdev->dev, res);
3413 return PTR_ERR(base);
3415 rockchip_regmap_config.max_register = resource_size(res) - 4;
3416 rockchip_regmap_config.name = "rockchip,pinctrl";
3417 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
3418 &rockchip_regmap_config);
3420 /* to check for the old dt-bindings */
3421 info->reg_size = resource_size(res);
3423 /* Honor the old binding, with pull registers as 2nd resource */
3424 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
3425 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3426 base = devm_ioremap_resource(&pdev->dev, res);
3428 return PTR_ERR(base);
3430 rockchip_regmap_config.max_register =
3431 resource_size(res) - 4;
3432 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
3433 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
3435 &rockchip_regmap_config);
3439 /* try to find the optional reference to the pmu syscon */
3440 node = of_parse_phandle(np, "rockchip,pmu", 0);
3442 info->regmap_pmu = syscon_node_to_regmap(node);
3444 if (IS_ERR(info->regmap_pmu))
3445 return PTR_ERR(info->regmap_pmu);
3448 ret = rockchip_gpiolib_register(pdev, info);
3452 ret = rockchip_pinctrl_register(pdev, info);
3454 rockchip_gpiolib_unregister(pdev, info);
3458 platform_set_drvdata(pdev, info);
3463 static struct rockchip_pin_bank px30_pin_banks[] = {
3464 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3469 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_WIDTH_4BIT,
3474 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_WIDTH_4BIT,
3479 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_WIDTH_4BIT,
3486 static struct rockchip_pin_ctrl px30_pin_ctrl = {
3487 .pin_banks = px30_pin_banks,
3488 .nr_banks = ARRAY_SIZE(px30_pin_banks),
3489 .label = "PX30-GPIO",
3491 .grf_mux_offset = 0x0,
3492 .pmu_mux_offset = 0x0,
3493 .iomux_routes = px30_mux_route_data,
3494 .niomux_routes = ARRAY_SIZE(px30_mux_route_data),
3495 .pull_calc_reg = px30_calc_pull_reg_and_bit,
3496 .drv_calc_reg = px30_calc_drv_reg_and_bit,
3497 .schmitt_calc_reg = px30_calc_schmitt_reg_and_bit,
3500 static struct rockchip_pin_bank rv1108_pin_banks[] = {
3501 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3505 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3506 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
3507 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
3510 static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
3511 .pin_banks = rv1108_pin_banks,
3512 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
3513 .label = "RV1108-GPIO",
3515 .grf_mux_offset = 0x10,
3516 .pmu_mux_offset = 0x0,
3517 .iomux_recalced = rv1108_mux_recalced_data,
3518 .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data),
3519 .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
3520 .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
3521 .schmitt_calc_reg = rv1108_calc_schmitt_reg_and_bit,
3524 static struct rockchip_pin_bank rk2928_pin_banks[] = {
3525 PIN_BANK(0, 32, "gpio0"),
3526 PIN_BANK(1, 32, "gpio1"),
3527 PIN_BANK(2, 32, "gpio2"),
3528 PIN_BANK(3, 32, "gpio3"),
3531 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
3532 .pin_banks = rk2928_pin_banks,
3533 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
3534 .label = "RK2928-GPIO",
3536 .grf_mux_offset = 0xa8,
3537 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3540 static struct rockchip_pin_bank rk3036_pin_banks[] = {
3541 PIN_BANK(0, 32, "gpio0"),
3542 PIN_BANK(1, 32, "gpio1"),
3543 PIN_BANK(2, 32, "gpio2"),
3546 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
3547 .pin_banks = rk3036_pin_banks,
3548 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
3549 .label = "RK3036-GPIO",
3551 .grf_mux_offset = 0xa8,
3552 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3555 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
3556 PIN_BANK(0, 32, "gpio0"),
3557 PIN_BANK(1, 32, "gpio1"),
3558 PIN_BANK(2, 32, "gpio2"),
3559 PIN_BANK(3, 32, "gpio3"),
3560 PIN_BANK(4, 32, "gpio4"),
3561 PIN_BANK(6, 16, "gpio6"),
3564 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
3565 .pin_banks = rk3066a_pin_banks,
3566 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
3567 .label = "RK3066a-GPIO",
3569 .grf_mux_offset = 0xa8,
3570 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3573 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
3574 PIN_BANK(0, 32, "gpio0"),
3575 PIN_BANK(1, 32, "gpio1"),
3576 PIN_BANK(2, 32, "gpio2"),
3577 PIN_BANK(3, 32, "gpio3"),
3580 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
3581 .pin_banks = rk3066b_pin_banks,
3582 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
3583 .label = "RK3066b-GPIO",
3585 .grf_mux_offset = 0x60,
3588 static struct rockchip_pin_bank rk3128_pin_banks[] = {
3589 PIN_BANK(0, 32, "gpio0"),
3590 PIN_BANK(1, 32, "gpio1"),
3591 PIN_BANK(2, 32, "gpio2"),
3592 PIN_BANK(3, 32, "gpio3"),
3595 static struct rockchip_pin_ctrl rk3128_pin_ctrl = {
3596 .pin_banks = rk3128_pin_banks,
3597 .nr_banks = ARRAY_SIZE(rk3128_pin_banks),
3598 .label = "RK3128-GPIO",
3600 .grf_mux_offset = 0xa8,
3601 .iomux_recalced = rk3128_mux_recalced_data,
3602 .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data),
3603 .iomux_routes = rk3128_mux_route_data,
3604 .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data),
3605 .pull_calc_reg = rk3128_calc_pull_reg_and_bit,
3608 static struct rockchip_pin_bank rk3188_pin_banks[] = {
3609 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
3610 PIN_BANK(1, 32, "gpio1"),
3611 PIN_BANK(2, 32, "gpio2"),
3612 PIN_BANK(3, 32, "gpio3"),
3615 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
3616 .pin_banks = rk3188_pin_banks,
3617 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
3618 .label = "RK3188-GPIO",
3620 .grf_mux_offset = 0x60,
3621 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
3624 static struct rockchip_pin_bank rk3228_pin_banks[] = {
3625 PIN_BANK(0, 32, "gpio0"),
3626 PIN_BANK(1, 32, "gpio1"),
3627 PIN_BANK(2, 32, "gpio2"),
3628 PIN_BANK(3, 32, "gpio3"),
3631 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
3632 .pin_banks = rk3228_pin_banks,
3633 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
3634 .label = "RK3228-GPIO",
3636 .grf_mux_offset = 0x0,
3637 .iomux_routes = rk3228_mux_route_data,
3638 .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data),
3639 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3640 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3643 static struct rockchip_pin_bank rk3288_pin_banks[] = {
3644 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
3649 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
3654 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
3655 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
3656 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
3661 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
3666 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
3667 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
3672 PIN_BANK(8, 16, "gpio8"),
3675 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
3676 .pin_banks = rk3288_pin_banks,
3677 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
3678 .label = "RK3288-GPIO",
3680 .grf_mux_offset = 0x0,
3681 .pmu_mux_offset = 0x84,
3682 .iomux_routes = rk3288_mux_route_data,
3683 .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
3684 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
3685 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
3688 static struct rockchip_pin_bank rk3328_pin_banks[] = {
3689 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
3690 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3691 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
3695 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
3702 static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
3703 .pin_banks = rk3328_pin_banks,
3704 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
3705 .label = "RK3328-GPIO",
3707 .grf_mux_offset = 0x0,
3708 .iomux_recalced = rk3328_mux_recalced_data,
3709 .niomux_recalced = ARRAY_SIZE(rk3328_mux_recalced_data),
3710 .iomux_routes = rk3328_mux_route_data,
3711 .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data),
3712 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3713 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3714 .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
3717 static struct rockchip_pin_bank rk3368_pin_banks[] = {
3718 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3723 PIN_BANK(1, 32, "gpio1"),
3724 PIN_BANK(2, 32, "gpio2"),
3725 PIN_BANK(3, 32, "gpio3"),
3728 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
3729 .pin_banks = rk3368_pin_banks,
3730 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
3731 .label = "RK3368-GPIO",
3733 .grf_mux_offset = 0x0,
3734 .pmu_mux_offset = 0x0,
3735 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
3736 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
3739 static struct rockchip_pin_bank rk3399_pin_banks[] = {
3740 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
3745 DRV_TYPE_IO_1V8_ONLY,
3746 DRV_TYPE_IO_1V8_ONLY,
3747 DRV_TYPE_IO_DEFAULT,
3748 DRV_TYPE_IO_DEFAULT,
3753 PULL_TYPE_IO_1V8_ONLY,
3754 PULL_TYPE_IO_1V8_ONLY,
3755 PULL_TYPE_IO_DEFAULT,
3756 PULL_TYPE_IO_DEFAULT
3758 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
3762 DRV_TYPE_IO_1V8_OR_3V0,
3763 DRV_TYPE_IO_1V8_OR_3V0,
3764 DRV_TYPE_IO_1V8_OR_3V0,
3765 DRV_TYPE_IO_1V8_OR_3V0,
3771 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
3772 DRV_TYPE_IO_1V8_OR_3V0,
3773 DRV_TYPE_IO_1V8_ONLY,
3774 DRV_TYPE_IO_1V8_ONLY,
3775 PULL_TYPE_IO_DEFAULT,
3776 PULL_TYPE_IO_DEFAULT,
3777 PULL_TYPE_IO_1V8_ONLY,
3778 PULL_TYPE_IO_1V8_ONLY
3780 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
3781 DRV_TYPE_IO_3V3_ONLY,
3782 DRV_TYPE_IO_3V3_ONLY,
3783 DRV_TYPE_IO_1V8_OR_3V0
3785 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
3786 DRV_TYPE_IO_1V8_3V0_AUTO,
3787 DRV_TYPE_IO_1V8_OR_3V0,
3788 DRV_TYPE_IO_1V8_OR_3V0
3792 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
3793 .pin_banks = rk3399_pin_banks,
3794 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
3795 .label = "RK3399-GPIO",
3797 .grf_mux_offset = 0xe000,
3798 .pmu_mux_offset = 0x0,
3799 .grf_drv_offset = 0xe100,
3800 .pmu_drv_offset = 0x80,
3801 .iomux_routes = rk3399_mux_route_data,
3802 .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
3803 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
3804 .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
3807 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
3808 { .compatible = "rockchip,px30-pinctrl",
3809 .data = &px30_pin_ctrl },
3810 { .compatible = "rockchip,rv1108-pinctrl",
3811 .data = &rv1108_pin_ctrl },
3812 { .compatible = "rockchip,rk2928-pinctrl",
3813 .data = &rk2928_pin_ctrl },
3814 { .compatible = "rockchip,rk3036-pinctrl",
3815 .data = &rk3036_pin_ctrl },
3816 { .compatible = "rockchip,rk3066a-pinctrl",
3817 .data = &rk3066a_pin_ctrl },
3818 { .compatible = "rockchip,rk3066b-pinctrl",
3819 .data = &rk3066b_pin_ctrl },
3820 { .compatible = "rockchip,rk3128-pinctrl",
3821 .data = (void *)&rk3128_pin_ctrl },
3822 { .compatible = "rockchip,rk3188-pinctrl",
3823 .data = &rk3188_pin_ctrl },
3824 { .compatible = "rockchip,rk3228-pinctrl",
3825 .data = &rk3228_pin_ctrl },
3826 { .compatible = "rockchip,rk3288-pinctrl",
3827 .data = &rk3288_pin_ctrl },
3828 { .compatible = "rockchip,rk3328-pinctrl",
3829 .data = &rk3328_pin_ctrl },
3830 { .compatible = "rockchip,rk3368-pinctrl",
3831 .data = &rk3368_pin_ctrl },
3832 { .compatible = "rockchip,rk3399-pinctrl",
3833 .data = &rk3399_pin_ctrl },
3837 static struct platform_driver rockchip_pinctrl_driver = {
3838 .probe = rockchip_pinctrl_probe,
3840 .name = "rockchip-pinctrl",
3841 .pm = &rockchip_pinctrl_dev_pm_ops,
3842 .of_match_table = rockchip_pinctrl_dt_match,
3846 static int __init rockchip_pinctrl_drv_register(void)
3848 return platform_driver_register(&rockchip_pinctrl_driver);
3850 postcore_initcall(rockchip_pinctrl_drv_register);