2 * Pinctrl driver for Rockchip SoCs
4 * Copyright (c) 2013 MundoReader S.L.
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
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/driver.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 * @type: iomux variant using IOMUX_* constants
84 * @offset: if initialized to -1 it will be autocalculated, by specifying
85 * an initial offset value the relevant source offset can be reset
86 * to a new value for autocalculating the following iomux registers.
88 struct rockchip_iomux {
94 * enum type index corresponding to rockchip_perpin_drv_list arrays index.
96 enum rockchip_pin_drv_type {
97 DRV_TYPE_IO_DEFAULT = 0,
98 DRV_TYPE_IO_1V8_OR_3V0,
100 DRV_TYPE_IO_1V8_3V0_AUTO,
101 DRV_TYPE_IO_3V3_ONLY,
106 * enum type index corresponding to rockchip_pull_list arrays index.
108 enum rockchip_pin_pull_type {
109 PULL_TYPE_IO_DEFAULT = 0,
110 PULL_TYPE_IO_1V8_ONLY,
115 * @drv_type: drive strength variant using rockchip_perpin_drv_type
116 * @offset: if initialized to -1 it will be autocalculated, by specifying
117 * an initial offset value the relevant source offset can be reset
118 * to a new value for autocalculating the following drive strength
119 * registers. if used chips own cal_drv func instead to calculate
120 * registers offset, the variant could be ignored.
122 struct rockchip_drv {
123 enum rockchip_pin_drv_type drv_type;
128 * @reg_base: register base of the gpio bank
129 * @reg_pull: optional separate register for additional pull settings
130 * @clk: clock of the gpio bank
131 * @irq: interrupt of the gpio bank
132 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
133 * @pin_base: first pin number
134 * @nr_pins: number of pins in this bank
135 * @name: name of the bank
136 * @bank_num: number of the bank, to account for holes
137 * @iomux: array describing the 4 iomux sources of the bank
138 * @drv: array describing the 4 drive strength sources of the bank
139 * @pull_type: array describing the 4 pull type sources of the bank
140 * @valid: is all necessary information present
141 * @of_node: dt node of this bank
142 * @drvdata: common pinctrl basedata
143 * @domain: irqdomain of the gpio bank
144 * @gpio_chip: gpiolib chip
145 * @grange: gpio range
146 * @slock: spinlock for the gpio bank
147 * @route_mask: bits describing the routing pins of per bank
149 struct rockchip_pin_bank {
150 void __iomem *reg_base;
151 struct regmap *regmap_pull;
159 struct rockchip_iomux iomux[4];
160 struct rockchip_drv drv[4];
161 enum rockchip_pin_pull_type pull_type[4];
163 struct device_node *of_node;
164 struct rockchip_pinctrl *drvdata;
165 struct irq_domain *domain;
166 struct gpio_chip gpio_chip;
167 struct pinctrl_gpio_range grange;
168 raw_spinlock_t slock;
169 u32 toggle_edge_mode;
174 #define PIN_BANK(id, pins, label) \
187 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
193 { .type = iom0, .offset = -1 }, \
194 { .type = iom1, .offset = -1 }, \
195 { .type = iom2, .offset = -1 }, \
196 { .type = iom3, .offset = -1 }, \
200 #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \
212 { .drv_type = type0, .offset = -1 }, \
213 { .drv_type = type1, .offset = -1 }, \
214 { .drv_type = type2, .offset = -1 }, \
215 { .drv_type = type3, .offset = -1 }, \
219 #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \
220 drv2, drv3, pull0, pull1, \
233 { .drv_type = drv0, .offset = -1 }, \
234 { .drv_type = drv1, .offset = -1 }, \
235 { .drv_type = drv2, .offset = -1 }, \
236 { .drv_type = drv3, .offset = -1 }, \
238 .pull_type[0] = pull0, \
239 .pull_type[1] = pull1, \
240 .pull_type[2] = pull2, \
241 .pull_type[3] = pull3, \
244 #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \
245 iom2, iom3, drv0, drv1, drv2, \
246 drv3, offset0, offset1, \
253 { .type = iom0, .offset = -1 }, \
254 { .type = iom1, .offset = -1 }, \
255 { .type = iom2, .offset = -1 }, \
256 { .type = iom3, .offset = -1 }, \
259 { .drv_type = drv0, .offset = offset0 }, \
260 { .drv_type = drv1, .offset = offset1 }, \
261 { .drv_type = drv2, .offset = offset2 }, \
262 { .drv_type = drv3, .offset = offset3 }, \
266 #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \
267 label, iom0, iom1, iom2, \
268 iom3, drv0, drv1, drv2, \
269 drv3, offset0, offset1, \
270 offset2, offset3, pull0, \
271 pull1, pull2, pull3) \
277 { .type = iom0, .offset = -1 }, \
278 { .type = iom1, .offset = -1 }, \
279 { .type = iom2, .offset = -1 }, \
280 { .type = iom3, .offset = -1 }, \
283 { .drv_type = drv0, .offset = offset0 }, \
284 { .drv_type = drv1, .offset = offset1 }, \
285 { .drv_type = drv2, .offset = offset2 }, \
286 { .drv_type = drv3, .offset = offset3 }, \
288 .pull_type[0] = pull0, \
289 .pull_type[1] = pull1, \
290 .pull_type[2] = pull2, \
291 .pull_type[3] = pull3, \
295 * struct rockchip_mux_recalced_data: represent a pin iomux data.
298 * @bit: index at register.
299 * @reg: register offset.
302 struct rockchip_mux_recalced_data {
311 * struct rockchip_mux_recalced_data: represent a pin iomux data.
312 * @bank_num: bank number.
313 * @pin: index at register or used to calc index.
314 * @func: the min pin.
315 * @route_offset: the max pin.
316 * @route_val: the register offset.
318 struct rockchip_mux_route_data {
328 struct rockchip_pin_ctrl {
329 struct rockchip_pin_bank *pin_banks;
333 enum rockchip_pinctrl_type type;
338 struct rockchip_mux_recalced_data *iomux_recalced;
340 struct rockchip_mux_route_data *iomux_routes;
343 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
344 int pin_num, struct regmap **regmap,
346 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
347 int pin_num, struct regmap **regmap,
349 int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank,
350 int pin_num, struct regmap **regmap,
354 struct rockchip_pin_config {
356 unsigned long *configs;
357 unsigned int nconfigs;
361 * struct rockchip_pin_group: represent group of pins of a pinmux function.
362 * @name: name of the pin group, used to lookup the group.
363 * @pins: the pins included in this group.
364 * @npins: number of pins included in this group.
365 * @func: the mux function number to be programmed when selected.
366 * @configs: the config values to be set for each pin
367 * @nconfigs: number of configs for each pin
369 struct rockchip_pin_group {
373 struct rockchip_pin_config *data;
377 * struct rockchip_pmx_func: represent a pin function.
378 * @name: name of the pin function, used to lookup the function.
379 * @groups: one or more names of pin groups that provide this function.
380 * @num_groups: number of groups included in @groups.
382 struct rockchip_pmx_func {
388 struct rockchip_pinctrl {
389 struct regmap *regmap_base;
391 struct regmap *regmap_pull;
392 struct regmap *regmap_pmu;
394 struct rockchip_pin_ctrl *ctrl;
395 struct pinctrl_desc pctl;
396 struct pinctrl_dev *pctl_dev;
397 struct rockchip_pin_group *groups;
398 unsigned int ngroups;
399 struct rockchip_pmx_func *functions;
400 unsigned int nfunctions;
403 static struct regmap_config rockchip_regmap_config = {
409 static inline const struct rockchip_pin_group *pinctrl_name_to_group(
410 const struct rockchip_pinctrl *info,
415 for (i = 0; i < info->ngroups; i++) {
416 if (!strcmp(info->groups[i].name, name))
417 return &info->groups[i];
424 * given a pin number that is local to a pin controller, find out the pin bank
425 * and the register base of the pin bank.
427 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
430 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
432 while (pin >= (b->pin_base + b->nr_pins))
438 static struct rockchip_pin_bank *bank_num_to_bank(
439 struct rockchip_pinctrl *info,
442 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
445 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
446 if (b->bank_num == num)
450 return ERR_PTR(-EINVAL);
454 * Pinctrl_ops handling
457 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
459 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
461 return info->ngroups;
464 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
467 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
469 return info->groups[selector].name;
472 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
473 unsigned selector, const unsigned **pins,
476 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
478 if (selector >= info->ngroups)
481 *pins = info->groups[selector].pins;
482 *npins = info->groups[selector].npins;
487 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
488 struct device_node *np,
489 struct pinctrl_map **map, unsigned *num_maps)
491 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
492 const struct rockchip_pin_group *grp;
493 struct pinctrl_map *new_map;
494 struct device_node *parent;
499 * first find the group of this node and check if we need to create
500 * config maps for pins
502 grp = pinctrl_name_to_group(info, np->name);
504 dev_err(info->dev, "unable to find group for node %pOFn\n",
509 map_num += grp->npins;
510 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
519 parent = of_get_parent(np);
521 devm_kfree(pctldev->dev, new_map);
524 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
525 new_map[0].data.mux.function = parent->name;
526 new_map[0].data.mux.group = np->name;
529 /* create config map */
531 for (i = 0; i < grp->npins; i++) {
532 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
533 new_map[i].data.configs.group_or_pin =
534 pin_get_name(pctldev, grp->pins[i]);
535 new_map[i].data.configs.configs = grp->data[i].configs;
536 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
539 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
540 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
545 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
546 struct pinctrl_map *map, unsigned num_maps)
550 static const struct pinctrl_ops rockchip_pctrl_ops = {
551 .get_groups_count = rockchip_get_groups_count,
552 .get_group_name = rockchip_get_group_name,
553 .get_group_pins = rockchip_get_group_pins,
554 .dt_node_to_map = rockchip_dt_node_to_map,
555 .dt_free_map = rockchip_dt_free_map,
562 static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = {
626 static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = {
660 static struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
682 static void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
683 int *reg, u8 *bit, int *mask)
685 struct rockchip_pinctrl *info = bank->drvdata;
686 struct rockchip_pin_ctrl *ctrl = info->ctrl;
687 struct rockchip_mux_recalced_data *data;
690 for (i = 0; i < ctrl->niomux_recalced; i++) {
691 data = &ctrl->iomux_recalced[i];
692 if (data->num == bank->bank_num &&
697 if (i >= ctrl->niomux_recalced)
705 static struct rockchip_mux_route_data px30_mux_route_data[] = {
711 .route_offset = 0x184,
712 .route_val = BIT(16 + 7),
718 .route_offset = 0x184,
719 .route_val = BIT(16 + 7) | BIT(7),
725 .route_offset = 0x184,
726 .route_val = BIT(16 + 8),
732 .route_offset = 0x184,
733 .route_val = BIT(16 + 8) | BIT(8),
739 .route_offset = 0x184,
740 .route_val = BIT(16 + 10),
746 .route_offset = 0x184,
747 .route_val = BIT(16 + 10) | BIT(10),
753 .route_offset = 0x184,
754 .route_val = BIT(16 + 9),
760 .route_offset = 0x184,
761 .route_val = BIT(16 + 9) | BIT(9),
765 static struct rockchip_mux_route_data rk3128_mux_route_data[] = {
771 .route_offset = 0x144,
772 .route_val = BIT(16 + 3) | BIT(16 + 4),
778 .route_offset = 0x144,
779 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3),
785 .route_offset = 0x144,
786 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4),
792 .route_offset = 0x144,
793 .route_val = BIT(16 + 5),
799 .route_offset = 0x144,
800 .route_val = BIT(16 + 5) | BIT(5),
806 .route_offset = 0x144,
807 .route_val = BIT(16 + 6),
813 .route_offset = 0x144,
814 .route_val = BIT(16 + 6) | BIT(6),
818 static struct rockchip_mux_route_data rk3228_mux_route_data[] = {
824 .route_offset = 0x50,
825 .route_val = BIT(16),
831 .route_offset = 0x50,
832 .route_val = BIT(16) | BIT(0),
838 .route_offset = 0x50,
839 .route_val = BIT(16 + 1),
845 .route_offset = 0x50,
846 .route_val = BIT(16 + 1) | BIT(1),
852 .route_offset = 0x50,
853 .route_val = BIT(16 + 2),
859 .route_offset = 0x50,
860 .route_val = BIT(16 + 2) | BIT(2),
866 .route_offset = 0x50,
867 .route_val = BIT(16 + 3),
873 .route_offset = 0x50,
874 .route_val = BIT(16 + 3) | BIT(3),
880 .route_offset = 0x50,
881 .route_val = BIT(16 + 4),
887 .route_offset = 0x50,
888 .route_val = BIT(16 + 4) | BIT(4),
894 .route_offset = 0x50,
895 .route_val = BIT(16 + 5),
901 .route_offset = 0x50,
902 .route_val = BIT(16 + 5) | BIT(5),
908 .route_offset = 0x50,
909 .route_val = BIT(16 + 7),
915 .route_offset = 0x50,
916 .route_val = BIT(16 + 7) | BIT(7),
922 .route_offset = 0x50,
923 .route_val = BIT(16 + 8),
929 .route_offset = 0x50,
930 .route_val = BIT(16 + 8) | BIT(8),
936 .route_offset = 0x50,
937 .route_val = BIT(16 + 11),
943 .route_offset = 0x50,
944 .route_val = BIT(16 + 11) | BIT(11),
948 static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
950 /* edphdmi_cecinoutt1 */
954 .route_offset = 0x264,
955 .route_val = BIT(16 + 12) | BIT(12),
957 /* edphdmi_cecinout */
961 .route_offset = 0x264,
962 .route_val = BIT(16 + 12),
966 static struct rockchip_mux_route_data rk3328_mux_route_data[] = {
972 .route_offset = 0x50,
973 .route_val = BIT(16) | BIT(16 + 1),
979 .route_offset = 0x50,
980 .route_val = BIT(16) | BIT(16 + 1) | BIT(0),
986 .route_offset = 0x50,
987 .route_val = BIT(16 + 2) | BIT(2),
989 /* gmac-m1-optimized_rxd3 */
993 .route_offset = 0x50,
994 .route_val = BIT(16 + 10) | BIT(10),
1000 .route_offset = 0x50,
1001 .route_val = BIT(16 + 3),
1007 .route_offset = 0x50,
1008 .route_val = BIT(16 + 3) | BIT(3),
1014 .route_offset = 0x50,
1015 .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5),
1021 .route_offset = 0x50,
1022 .route_val = BIT(16 + 6),
1028 .route_offset = 0x50,
1029 .route_val = BIT(16 + 6) | BIT(6),
1035 .route_offset = 0x50,
1036 .route_val = BIT(16 + 7) | BIT(7),
1042 .route_offset = 0x50,
1043 .route_val = BIT(16 + 8) | BIT(8),
1049 .route_offset = 0x50,
1050 .route_val = BIT(16 + 9) | BIT(9),
1054 static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
1060 .route_offset = 0xe21c,
1061 .route_val = BIT(16 + 10) | BIT(16 + 11),
1067 .route_offset = 0xe21c,
1068 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
1074 .route_offset = 0xe21c,
1075 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
1081 .route_offset = 0xe21c,
1082 .route_val = BIT(16 + 14),
1088 .route_offset = 0xe21c,
1089 .route_val = BIT(16 + 14) | BIT(14),
1093 static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
1094 int mux, u32 *reg, u32 *value)
1096 struct rockchip_pinctrl *info = bank->drvdata;
1097 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1098 struct rockchip_mux_route_data *data;
1101 for (i = 0; i < ctrl->niomux_routes; i++) {
1102 data = &ctrl->iomux_routes[i];
1103 if ((data->bank_num == bank->bank_num) &&
1104 (data->pin == pin) && (data->func == mux))
1108 if (i >= ctrl->niomux_routes)
1111 *reg = data->route_offset;
1112 *value = data->route_val;
1117 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
1119 struct rockchip_pinctrl *info = bank->drvdata;
1120 int iomux_num = (pin / 8);
1121 struct regmap *regmap;
1123 int reg, ret, mask, mux_type;
1129 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1130 dev_err(info->dev, "pin %d is unrouted\n", pin);
1134 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1135 return RK_FUNC_GPIO;
1137 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1138 ? info->regmap_pmu : info->regmap_base;
1140 /* get basic quadrupel of mux registers and the correct reg inside */
1141 mux_type = bank->iomux[iomux_num].type;
1142 reg = bank->iomux[iomux_num].offset;
1143 if (mux_type & IOMUX_WIDTH_4BIT) {
1146 bit = (pin % 4) * 4;
1148 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1151 bit = (pin % 8 % 5) * 3;
1154 bit = (pin % 8) * 2;
1158 if (bank->recalced_mask & BIT(pin))
1159 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1161 ret = regmap_read(regmap, reg, &val);
1165 return ((val >> bit) & mask);
1168 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
1171 struct rockchip_pinctrl *info = bank->drvdata;
1172 int iomux_num = (pin / 8);
1177 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1178 dev_err(info->dev, "pin %d is unrouted\n", pin);
1182 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
1183 if (mux != RK_FUNC_GPIO) {
1185 "pin %d only supports a gpio mux\n", pin);
1194 * Set a new mux function for a pin.
1196 * The register is divided into the upper and lower 16 bit. When changing
1197 * a value, the previous register value is not read and changed. Instead
1198 * it seems the changed bits are marked in the upper 16 bit, while the
1199 * changed value gets set in the same offset in the lower 16 bit.
1200 * All pin settings seem to be 2 bit wide in both the upper and lower
1202 * @bank: pin bank to change
1203 * @pin: pin to change
1204 * @mux: new mux function to set
1206 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
1208 struct rockchip_pinctrl *info = bank->drvdata;
1209 int iomux_num = (pin / 8);
1210 struct regmap *regmap;
1211 int reg, ret, mask, mux_type;
1213 u32 data, rmask, route_reg, route_val;
1215 ret = rockchip_verify_mux(bank, pin, mux);
1219 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1222 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
1223 bank->bank_num, pin, mux);
1225 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1226 ? info->regmap_pmu : info->regmap_base;
1228 /* get basic quadrupel of mux registers and the correct reg inside */
1229 mux_type = bank->iomux[iomux_num].type;
1230 reg = bank->iomux[iomux_num].offset;
1231 if (mux_type & IOMUX_WIDTH_4BIT) {
1234 bit = (pin % 4) * 4;
1236 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1239 bit = (pin % 8 % 5) * 3;
1242 bit = (pin % 8) * 2;
1246 if (bank->recalced_mask & BIT(pin))
1247 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1249 if (bank->route_mask & BIT(pin)) {
1250 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
1252 ret = regmap_write(regmap, route_reg, route_val);
1258 data = (mask << (bit + 16));
1259 rmask = data | (data >> 16);
1260 data |= (mux & mask) << bit;
1261 ret = regmap_update_bits(regmap, reg, rmask, data);
1266 #define PX30_PULL_PMU_OFFSET 0x10
1267 #define PX30_PULL_GRF_OFFSET 0x60
1268 #define PX30_PULL_BITS_PER_PIN 2
1269 #define PX30_PULL_PINS_PER_REG 8
1270 #define PX30_PULL_BANK_STRIDE 16
1272 static void px30_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1273 int pin_num, struct regmap **regmap,
1276 struct rockchip_pinctrl *info = bank->drvdata;
1278 /* The first 32 pins of the first bank are located in PMU */
1279 if (bank->bank_num == 0) {
1280 *regmap = info->regmap_pmu;
1281 *reg = PX30_PULL_PMU_OFFSET;
1283 *regmap = info->regmap_base;
1284 *reg = PX30_PULL_GRF_OFFSET;
1286 /* correct the offset, as we're starting with the 2nd bank */
1288 *reg += bank->bank_num * PX30_PULL_BANK_STRIDE;
1291 *reg += ((pin_num / PX30_PULL_PINS_PER_REG) * 4);
1292 *bit = (pin_num % PX30_PULL_PINS_PER_REG);
1293 *bit *= PX30_PULL_BITS_PER_PIN;
1296 #define PX30_DRV_PMU_OFFSET 0x20
1297 #define PX30_DRV_GRF_OFFSET 0xf0
1298 #define PX30_DRV_BITS_PER_PIN 2
1299 #define PX30_DRV_PINS_PER_REG 8
1300 #define PX30_DRV_BANK_STRIDE 16
1302 static void px30_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1303 int pin_num, struct regmap **regmap,
1306 struct rockchip_pinctrl *info = bank->drvdata;
1308 /* The first 32 pins of the first bank are located in PMU */
1309 if (bank->bank_num == 0) {
1310 *regmap = info->regmap_pmu;
1311 *reg = PX30_DRV_PMU_OFFSET;
1313 *regmap = info->regmap_base;
1314 *reg = PX30_DRV_GRF_OFFSET;
1316 /* correct the offset, as we're starting with the 2nd bank */
1318 *reg += bank->bank_num * PX30_DRV_BANK_STRIDE;
1321 *reg += ((pin_num / PX30_DRV_PINS_PER_REG) * 4);
1322 *bit = (pin_num % PX30_DRV_PINS_PER_REG);
1323 *bit *= PX30_DRV_BITS_PER_PIN;
1326 #define PX30_SCHMITT_PMU_OFFSET 0x38
1327 #define PX30_SCHMITT_GRF_OFFSET 0xc0
1328 #define PX30_SCHMITT_PINS_PER_PMU_REG 16
1329 #define PX30_SCHMITT_BANK_STRIDE 16
1330 #define PX30_SCHMITT_PINS_PER_GRF_REG 8
1332 static int px30_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1334 struct regmap **regmap,
1337 struct rockchip_pinctrl *info = bank->drvdata;
1340 if (bank->bank_num == 0) {
1341 *regmap = info->regmap_pmu;
1342 *reg = PX30_SCHMITT_PMU_OFFSET;
1343 pins_per_reg = PX30_SCHMITT_PINS_PER_PMU_REG;
1345 *regmap = info->regmap_base;
1346 *reg = PX30_SCHMITT_GRF_OFFSET;
1347 pins_per_reg = PX30_SCHMITT_PINS_PER_GRF_REG;
1348 *reg += (bank->bank_num - 1) * PX30_SCHMITT_BANK_STRIDE;
1351 *reg += ((pin_num / pins_per_reg) * 4);
1352 *bit = pin_num % pins_per_reg;
1357 #define RV1108_PULL_PMU_OFFSET 0x10
1358 #define RV1108_PULL_OFFSET 0x110
1359 #define RV1108_PULL_PINS_PER_REG 8
1360 #define RV1108_PULL_BITS_PER_PIN 2
1361 #define RV1108_PULL_BANK_STRIDE 16
1363 static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1364 int pin_num, struct regmap **regmap,
1367 struct rockchip_pinctrl *info = bank->drvdata;
1369 /* The first 24 pins of the first bank are located in PMU */
1370 if (bank->bank_num == 0) {
1371 *regmap = info->regmap_pmu;
1372 *reg = RV1108_PULL_PMU_OFFSET;
1374 *reg = RV1108_PULL_OFFSET;
1375 *regmap = info->regmap_base;
1376 /* correct the offset, as we're starting with the 2nd bank */
1378 *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
1381 *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
1382 *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
1383 *bit *= RV1108_PULL_BITS_PER_PIN;
1386 #define RV1108_DRV_PMU_OFFSET 0x20
1387 #define RV1108_DRV_GRF_OFFSET 0x210
1388 #define RV1108_DRV_BITS_PER_PIN 2
1389 #define RV1108_DRV_PINS_PER_REG 8
1390 #define RV1108_DRV_BANK_STRIDE 16
1392 static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1393 int pin_num, struct regmap **regmap,
1396 struct rockchip_pinctrl *info = bank->drvdata;
1398 /* The first 24 pins of the first bank are located in PMU */
1399 if (bank->bank_num == 0) {
1400 *regmap = info->regmap_pmu;
1401 *reg = RV1108_DRV_PMU_OFFSET;
1403 *regmap = info->regmap_base;
1404 *reg = RV1108_DRV_GRF_OFFSET;
1406 /* correct the offset, as we're starting with the 2nd bank */
1408 *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
1411 *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
1412 *bit = pin_num % RV1108_DRV_PINS_PER_REG;
1413 *bit *= RV1108_DRV_BITS_PER_PIN;
1416 #define RV1108_SCHMITT_PMU_OFFSET 0x30
1417 #define RV1108_SCHMITT_GRF_OFFSET 0x388
1418 #define RV1108_SCHMITT_BANK_STRIDE 8
1419 #define RV1108_SCHMITT_PINS_PER_GRF_REG 16
1420 #define RV1108_SCHMITT_PINS_PER_PMU_REG 8
1422 static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1424 struct regmap **regmap,
1427 struct rockchip_pinctrl *info = bank->drvdata;
1430 if (bank->bank_num == 0) {
1431 *regmap = info->regmap_pmu;
1432 *reg = RV1108_SCHMITT_PMU_OFFSET;
1433 pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG;
1435 *regmap = info->regmap_base;
1436 *reg = RV1108_SCHMITT_GRF_OFFSET;
1437 pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG;
1438 *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE;
1440 *reg += ((pin_num / pins_per_reg) * 4);
1441 *bit = pin_num % pins_per_reg;
1446 #define RK2928_PULL_OFFSET 0x118
1447 #define RK2928_PULL_PINS_PER_REG 16
1448 #define RK2928_PULL_BANK_STRIDE 8
1450 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1451 int pin_num, struct regmap **regmap,
1454 struct rockchip_pinctrl *info = bank->drvdata;
1456 *regmap = info->regmap_base;
1457 *reg = RK2928_PULL_OFFSET;
1458 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1459 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
1461 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1464 #define RK3128_PULL_OFFSET 0x118
1466 static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1467 int pin_num, struct regmap **regmap,
1470 struct rockchip_pinctrl *info = bank->drvdata;
1472 *regmap = info->regmap_base;
1473 *reg = RK3128_PULL_OFFSET;
1474 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1475 *reg += ((pin_num / RK2928_PULL_PINS_PER_REG) * 4);
1477 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1480 #define RK3188_PULL_OFFSET 0x164
1481 #define RK3188_PULL_BITS_PER_PIN 2
1482 #define RK3188_PULL_PINS_PER_REG 8
1483 #define RK3188_PULL_BANK_STRIDE 16
1484 #define RK3188_PULL_PMU_OFFSET 0x64
1486 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1487 int pin_num, struct regmap **regmap,
1490 struct rockchip_pinctrl *info = bank->drvdata;
1492 /* The first 12 pins of the first bank are located elsewhere */
1493 if (bank->bank_num == 0 && pin_num < 12) {
1494 *regmap = info->regmap_pmu ? info->regmap_pmu
1495 : bank->regmap_pull;
1496 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
1497 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1498 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1499 *bit *= RK3188_PULL_BITS_PER_PIN;
1501 *regmap = info->regmap_pull ? info->regmap_pull
1502 : info->regmap_base;
1503 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
1505 /* correct the offset, as it is the 2nd pull register */
1507 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1508 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1511 * The bits in these registers have an inverse ordering
1512 * with the lowest pin being in bits 15:14 and the highest
1515 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
1516 *bit *= RK3188_PULL_BITS_PER_PIN;
1520 #define RK3288_PULL_OFFSET 0x140
1521 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1522 int pin_num, struct regmap **regmap,
1525 struct rockchip_pinctrl *info = bank->drvdata;
1527 /* The first 24 pins of the first bank are located in PMU */
1528 if (bank->bank_num == 0) {
1529 *regmap = info->regmap_pmu;
1530 *reg = RK3188_PULL_PMU_OFFSET;
1532 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1533 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1534 *bit *= RK3188_PULL_BITS_PER_PIN;
1536 *regmap = info->regmap_base;
1537 *reg = RK3288_PULL_OFFSET;
1539 /* correct the offset, as we're starting with the 2nd bank */
1541 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1542 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1544 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1545 *bit *= RK3188_PULL_BITS_PER_PIN;
1549 #define RK3288_DRV_PMU_OFFSET 0x70
1550 #define RK3288_DRV_GRF_OFFSET 0x1c0
1551 #define RK3288_DRV_BITS_PER_PIN 2
1552 #define RK3288_DRV_PINS_PER_REG 8
1553 #define RK3288_DRV_BANK_STRIDE 16
1555 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1556 int pin_num, struct regmap **regmap,
1559 struct rockchip_pinctrl *info = bank->drvdata;
1561 /* The first 24 pins of the first bank are located in PMU */
1562 if (bank->bank_num == 0) {
1563 *regmap = info->regmap_pmu;
1564 *reg = RK3288_DRV_PMU_OFFSET;
1566 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1567 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1568 *bit *= RK3288_DRV_BITS_PER_PIN;
1570 *regmap = info->regmap_base;
1571 *reg = RK3288_DRV_GRF_OFFSET;
1573 /* correct the offset, as we're starting with the 2nd bank */
1575 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1576 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1578 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1579 *bit *= RK3288_DRV_BITS_PER_PIN;
1583 #define RK3228_PULL_OFFSET 0x100
1585 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1586 int pin_num, struct regmap **regmap,
1589 struct rockchip_pinctrl *info = bank->drvdata;
1591 *regmap = info->regmap_base;
1592 *reg = RK3228_PULL_OFFSET;
1593 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1594 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1596 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1597 *bit *= RK3188_PULL_BITS_PER_PIN;
1600 #define RK3228_DRV_GRF_OFFSET 0x200
1602 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1603 int pin_num, struct regmap **regmap,
1606 struct rockchip_pinctrl *info = bank->drvdata;
1608 *regmap = info->regmap_base;
1609 *reg = RK3228_DRV_GRF_OFFSET;
1610 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1611 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1613 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1614 *bit *= RK3288_DRV_BITS_PER_PIN;
1617 #define RK3368_PULL_GRF_OFFSET 0x100
1618 #define RK3368_PULL_PMU_OFFSET 0x10
1620 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1621 int pin_num, struct regmap **regmap,
1624 struct rockchip_pinctrl *info = bank->drvdata;
1626 /* The first 32 pins of the first bank are located in PMU */
1627 if (bank->bank_num == 0) {
1628 *regmap = info->regmap_pmu;
1629 *reg = RK3368_PULL_PMU_OFFSET;
1631 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1632 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1633 *bit *= RK3188_PULL_BITS_PER_PIN;
1635 *regmap = info->regmap_base;
1636 *reg = RK3368_PULL_GRF_OFFSET;
1638 /* correct the offset, as we're starting with the 2nd bank */
1640 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1641 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1643 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1644 *bit *= RK3188_PULL_BITS_PER_PIN;
1648 #define RK3368_DRV_PMU_OFFSET 0x20
1649 #define RK3368_DRV_GRF_OFFSET 0x200
1651 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1652 int pin_num, struct regmap **regmap,
1655 struct rockchip_pinctrl *info = bank->drvdata;
1657 /* The first 32 pins of the first bank are located in PMU */
1658 if (bank->bank_num == 0) {
1659 *regmap = info->regmap_pmu;
1660 *reg = RK3368_DRV_PMU_OFFSET;
1662 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1663 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1664 *bit *= RK3288_DRV_BITS_PER_PIN;
1666 *regmap = info->regmap_base;
1667 *reg = RK3368_DRV_GRF_OFFSET;
1669 /* correct the offset, as we're starting with the 2nd bank */
1671 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1672 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1674 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1675 *bit *= RK3288_DRV_BITS_PER_PIN;
1679 #define RK3399_PULL_GRF_OFFSET 0xe040
1680 #define RK3399_PULL_PMU_OFFSET 0x40
1681 #define RK3399_DRV_3BITS_PER_PIN 3
1683 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1684 int pin_num, struct regmap **regmap,
1687 struct rockchip_pinctrl *info = bank->drvdata;
1689 /* The bank0:16 and bank1:32 pins are located in PMU */
1690 if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
1691 *regmap = info->regmap_pmu;
1692 *reg = RK3399_PULL_PMU_OFFSET;
1694 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1696 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1697 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1698 *bit *= RK3188_PULL_BITS_PER_PIN;
1700 *regmap = info->regmap_base;
1701 *reg = RK3399_PULL_GRF_OFFSET;
1703 /* correct the offset, as we're starting with the 3rd bank */
1705 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1706 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1708 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1709 *bit *= RK3188_PULL_BITS_PER_PIN;
1713 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1714 int pin_num, struct regmap **regmap,
1717 struct rockchip_pinctrl *info = bank->drvdata;
1718 int drv_num = (pin_num / 8);
1720 /* The bank0:16 and bank1:32 pins are located in PMU */
1721 if ((bank->bank_num == 0) || (bank->bank_num == 1))
1722 *regmap = info->regmap_pmu;
1724 *regmap = info->regmap_base;
1726 *reg = bank->drv[drv_num].offset;
1727 if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
1728 (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
1729 *bit = (pin_num % 8) * 3;
1731 *bit = (pin_num % 8) * 2;
1734 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
1735 { 2, 4, 8, 12, -1, -1, -1, -1 },
1736 { 3, 6, 9, 12, -1, -1, -1, -1 },
1737 { 5, 10, 15, 20, -1, -1, -1, -1 },
1738 { 4, 6, 8, 10, 12, 14, 16, 18 },
1739 { 4, 7, 10, 13, 16, 19, 22, 26 }
1742 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
1745 struct rockchip_pinctrl *info = bank->drvdata;
1746 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1747 struct regmap *regmap;
1749 u32 data, temp, rmask_bits;
1751 int drv_type = bank->drv[pin_num / 8].drv_type;
1753 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1756 case DRV_TYPE_IO_1V8_3V0_AUTO:
1757 case DRV_TYPE_IO_3V3_ONLY:
1758 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1761 /* regular case, nothing to do */
1765 * drive-strength offset is special, as it is
1766 * spread over 2 registers
1768 ret = regmap_read(regmap, reg, &data);
1772 ret = regmap_read(regmap, reg + 0x4, &temp);
1777 * the bit data[15] contains bit 0 of the value
1778 * while temp[1:0] contains bits 2 and 1
1785 return rockchip_perpin_drv_list[drv_type][data];
1787 /* setting fully enclosed in the second register */
1792 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1798 case DRV_TYPE_IO_DEFAULT:
1799 case DRV_TYPE_IO_1V8_OR_3V0:
1800 case DRV_TYPE_IO_1V8_ONLY:
1801 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1804 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1809 ret = regmap_read(regmap, reg, &data);
1814 data &= (1 << rmask_bits) - 1;
1816 return rockchip_perpin_drv_list[drv_type][data];
1819 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
1820 int pin_num, int strength)
1822 struct rockchip_pinctrl *info = bank->drvdata;
1823 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1824 struct regmap *regmap;
1826 u32 data, rmask, rmask_bits, temp;
1828 int drv_type = bank->drv[pin_num / 8].drv_type;
1830 dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
1831 bank->bank_num, pin_num, strength);
1833 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1836 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
1837 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
1840 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
1841 ret = rockchip_perpin_drv_list[drv_type][i];
1847 dev_err(info->dev, "unsupported driver strength %d\n",
1853 case DRV_TYPE_IO_1V8_3V0_AUTO:
1854 case DRV_TYPE_IO_3V3_ONLY:
1855 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1858 /* regular case, nothing to do */
1862 * drive-strength offset is special, as it is spread
1863 * over 2 registers, the bit data[15] contains bit 0
1864 * of the value while temp[1:0] contains bits 2 and 1
1866 data = (ret & 0x1) << 15;
1867 temp = (ret >> 0x1) & 0x3;
1869 rmask = BIT(15) | BIT(31);
1871 ret = regmap_update_bits(regmap, reg, rmask, data);
1875 rmask = 0x3 | (0x3 << 16);
1876 temp |= (0x3 << 16);
1878 ret = regmap_update_bits(regmap, reg, rmask, temp);
1882 /* setting fully enclosed in the second register */
1887 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1892 case DRV_TYPE_IO_DEFAULT:
1893 case DRV_TYPE_IO_1V8_OR_3V0:
1894 case DRV_TYPE_IO_1V8_ONLY:
1895 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1898 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1903 /* enable the write to the equivalent lower bits */
1904 data = ((1 << rmask_bits) - 1) << (bit + 16);
1905 rmask = data | (data >> 16);
1906 data |= (ret << bit);
1908 ret = regmap_update_bits(regmap, reg, rmask, data);
1913 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
1915 PIN_CONFIG_BIAS_DISABLE,
1916 PIN_CONFIG_BIAS_PULL_UP,
1917 PIN_CONFIG_BIAS_PULL_DOWN,
1918 PIN_CONFIG_BIAS_BUS_HOLD
1921 PIN_CONFIG_BIAS_DISABLE,
1922 PIN_CONFIG_BIAS_PULL_DOWN,
1923 PIN_CONFIG_BIAS_DISABLE,
1924 PIN_CONFIG_BIAS_PULL_UP
1928 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
1930 struct rockchip_pinctrl *info = bank->drvdata;
1931 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1932 struct regmap *regmap;
1933 int reg, ret, pull_type;
1937 /* rk3066b does support any pulls */
1938 if (ctrl->type == RK3066B)
1939 return PIN_CONFIG_BIAS_DISABLE;
1941 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1943 ret = regmap_read(regmap, reg, &data);
1947 switch (ctrl->type) {
1950 return !(data & BIT(bit))
1951 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
1952 : PIN_CONFIG_BIAS_DISABLE;
1959 pull_type = bank->pull_type[pin_num / 8];
1961 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
1963 return rockchip_pull_list[pull_type][data];
1965 dev_err(info->dev, "unsupported pinctrl type\n");
1970 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
1971 int pin_num, int pull)
1973 struct rockchip_pinctrl *info = bank->drvdata;
1974 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1975 struct regmap *regmap;
1976 int reg, ret, i, pull_type;
1980 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
1981 bank->bank_num, pin_num, pull);
1983 /* rk3066b does support any pulls */
1984 if (ctrl->type == RK3066B)
1985 return pull ? -EINVAL : 0;
1987 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1989 switch (ctrl->type) {
1992 data = BIT(bit + 16);
1993 if (pull == PIN_CONFIG_BIAS_DISABLE)
1995 ret = regmap_write(regmap, reg, data);
2003 pull_type = bank->pull_type[pin_num / 8];
2005 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
2007 if (rockchip_pull_list[pull_type][i] == pull) {
2014 dev_err(info->dev, "unsupported pull setting %d\n",
2019 /* enable the write to the equivalent lower bits */
2020 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
2021 rmask = data | (data >> 16);
2022 data |= (ret << bit);
2024 ret = regmap_update_bits(regmap, reg, rmask, data);
2027 dev_err(info->dev, "unsupported pinctrl type\n");
2034 #define RK3328_SCHMITT_BITS_PER_PIN 1
2035 #define RK3328_SCHMITT_PINS_PER_REG 16
2036 #define RK3328_SCHMITT_BANK_STRIDE 8
2037 #define RK3328_SCHMITT_GRF_OFFSET 0x380
2039 static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
2041 struct regmap **regmap,
2044 struct rockchip_pinctrl *info = bank->drvdata;
2046 *regmap = info->regmap_base;
2047 *reg = RK3328_SCHMITT_GRF_OFFSET;
2049 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
2050 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
2051 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
2056 static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
2058 struct rockchip_pinctrl *info = bank->drvdata;
2059 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2060 struct regmap *regmap;
2065 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2069 ret = regmap_read(regmap, reg, &data);
2077 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
2078 int pin_num, int enable)
2080 struct rockchip_pinctrl *info = bank->drvdata;
2081 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2082 struct regmap *regmap;
2087 dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
2088 bank->bank_num, pin_num, enable);
2090 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2094 /* enable the write to the equivalent lower bits */
2095 data = BIT(bit + 16) | (enable << bit);
2096 rmask = BIT(bit + 16) | BIT(bit);
2098 return regmap_update_bits(regmap, reg, rmask, data);
2102 * Pinmux_ops handling
2105 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
2107 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2109 return info->nfunctions;
2112 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
2115 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2117 return info->functions[selector].name;
2120 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
2121 unsigned selector, const char * const **groups,
2122 unsigned * const num_groups)
2124 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2126 *groups = info->functions[selector].groups;
2127 *num_groups = info->functions[selector].ngroups;
2132 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
2135 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2136 const unsigned int *pins = info->groups[group].pins;
2137 const struct rockchip_pin_config *data = info->groups[group].data;
2138 struct rockchip_pin_bank *bank;
2141 dev_dbg(info->dev, "enable function %s group %s\n",
2142 info->functions[selector].name, info->groups[group].name);
2145 * for each pin in the pin group selected, program the corresponding
2146 * pin function number in the config register.
2148 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
2149 bank = pin_to_bank(info, pins[cnt]);
2150 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
2157 /* revert the already done pin settings */
2158 for (cnt--; cnt >= 0; cnt--)
2159 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
2167 static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
2169 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
2173 ret = clk_enable(bank->clk);
2175 dev_err(bank->drvdata->dev,
2176 "failed to enable clock for bank %s\n", bank->name);
2179 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2180 clk_disable(bank->clk);
2182 return !(data & BIT(offset));
2186 * The calls to gpio_direction_output() and gpio_direction_input()
2187 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
2188 * function called from the gpiolib interface).
2190 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
2191 int pin, bool input)
2193 struct rockchip_pin_bank *bank;
2195 unsigned long flags;
2198 bank = gpiochip_get_data(chip);
2200 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
2204 clk_enable(bank->clk);
2205 raw_spin_lock_irqsave(&bank->slock, flags);
2207 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2208 /* set bit to 1 for output, 0 for input */
2213 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2215 raw_spin_unlock_irqrestore(&bank->slock, flags);
2216 clk_disable(bank->clk);
2221 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
2222 struct pinctrl_gpio_range *range,
2223 unsigned offset, bool input)
2225 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2226 struct gpio_chip *chip;
2230 pin = offset - chip->base;
2231 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
2232 offset, range->name, pin, input ? "input" : "output");
2234 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
2238 static const struct pinmux_ops rockchip_pmx_ops = {
2239 .get_functions_count = rockchip_pmx_get_funcs_count,
2240 .get_function_name = rockchip_pmx_get_func_name,
2241 .get_function_groups = rockchip_pmx_get_groups,
2242 .set_mux = rockchip_pmx_set,
2243 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
2247 * Pinconf_ops handling
2250 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
2251 enum pin_config_param pull)
2253 switch (ctrl->type) {
2256 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
2257 pull == PIN_CONFIG_BIAS_DISABLE);
2259 return pull ? false : true;
2266 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
2272 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
2273 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
2275 /* set the pin config settings for a specified pin */
2276 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
2277 unsigned long *configs, unsigned num_configs)
2279 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2280 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2281 enum pin_config_param param;
2286 for (i = 0; i < num_configs; i++) {
2287 param = pinconf_to_config_param(configs[i]);
2288 arg = pinconf_to_config_argument(configs[i]);
2291 case PIN_CONFIG_BIAS_DISABLE:
2292 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2297 case PIN_CONFIG_BIAS_PULL_UP:
2298 case PIN_CONFIG_BIAS_PULL_DOWN:
2299 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2300 case PIN_CONFIG_BIAS_BUS_HOLD:
2301 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2307 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2312 case PIN_CONFIG_OUTPUT:
2313 rockchip_gpio_set(&bank->gpio_chip,
2314 pin - bank->pin_base, arg);
2315 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
2316 pin - bank->pin_base, false);
2320 case PIN_CONFIG_DRIVE_STRENGTH:
2321 /* rk3288 is the first with per-pin drive-strength */
2322 if (!info->ctrl->drv_calc_reg)
2325 rc = rockchip_set_drive_perpin(bank,
2326 pin - bank->pin_base, arg);
2330 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2331 if (!info->ctrl->schmitt_calc_reg)
2334 rc = rockchip_set_schmitt(bank,
2335 pin - bank->pin_base, arg);
2343 } /* for each config */
2348 /* get the pin config settings for a specified pin */
2349 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
2350 unsigned long *config)
2352 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2353 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2354 enum pin_config_param param = pinconf_to_config_param(*config);
2359 case PIN_CONFIG_BIAS_DISABLE:
2360 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2365 case PIN_CONFIG_BIAS_PULL_UP:
2366 case PIN_CONFIG_BIAS_PULL_DOWN:
2367 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2368 case PIN_CONFIG_BIAS_BUS_HOLD:
2369 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2372 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2377 case PIN_CONFIG_OUTPUT:
2378 rc = rockchip_get_mux(bank, pin - bank->pin_base);
2379 if (rc != RK_FUNC_GPIO)
2382 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
2388 case PIN_CONFIG_DRIVE_STRENGTH:
2389 /* rk3288 is the first with per-pin drive-strength */
2390 if (!info->ctrl->drv_calc_reg)
2393 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
2399 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2400 if (!info->ctrl->schmitt_calc_reg)
2403 rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
2414 *config = pinconf_to_config_packed(param, arg);
2419 static const struct pinconf_ops rockchip_pinconf_ops = {
2420 .pin_config_get = rockchip_pinconf_get,
2421 .pin_config_set = rockchip_pinconf_set,
2425 static const struct of_device_id rockchip_bank_match[] = {
2426 { .compatible = "rockchip,gpio-bank" },
2427 { .compatible = "rockchip,rk3188-gpio-bank0" },
2431 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
2432 struct device_node *np)
2434 struct device_node *child;
2436 for_each_child_of_node(np, child) {
2437 if (of_match_node(rockchip_bank_match, child))
2441 info->ngroups += of_get_child_count(child);
2445 static int rockchip_pinctrl_parse_groups(struct device_node *np,
2446 struct rockchip_pin_group *grp,
2447 struct rockchip_pinctrl *info,
2450 struct rockchip_pin_bank *bank;
2457 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
2459 /* Initialise group */
2460 grp->name = np->name;
2463 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
2464 * do sanity check and calculate pins number
2466 list = of_get_property(np, "rockchip,pins", &size);
2467 /* we do not check return since it's safe node passed down */
2468 size /= sizeof(*list);
2469 if (!size || size % 4) {
2470 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
2474 grp->npins = size / 4;
2476 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
2478 grp->data = devm_kcalloc(info->dev,
2480 sizeof(struct rockchip_pin_config),
2482 if (!grp->pins || !grp->data)
2485 for (i = 0, j = 0; i < size; i += 4, j++) {
2486 const __be32 *phandle;
2487 struct device_node *np_config;
2489 num = be32_to_cpu(*list++);
2490 bank = bank_num_to_bank(info, num);
2492 return PTR_ERR(bank);
2494 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
2495 grp->data[j].func = be32_to_cpu(*list++);
2501 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
2502 ret = pinconf_generic_parse_dt_config(np_config, NULL,
2503 &grp->data[j].configs, &grp->data[j].nconfigs);
2511 static int rockchip_pinctrl_parse_functions(struct device_node *np,
2512 struct rockchip_pinctrl *info,
2515 struct device_node *child;
2516 struct rockchip_pmx_func *func;
2517 struct rockchip_pin_group *grp;
2519 static u32 grp_index;
2522 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
2524 func = &info->functions[index];
2526 /* Initialise function */
2527 func->name = np->name;
2528 func->ngroups = of_get_child_count(np);
2529 if (func->ngroups <= 0)
2532 func->groups = devm_kcalloc(info->dev,
2533 func->ngroups, sizeof(char *), GFP_KERNEL);
2537 for_each_child_of_node(np, child) {
2538 func->groups[i] = child->name;
2539 grp = &info->groups[grp_index++];
2540 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
2550 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
2551 struct rockchip_pinctrl *info)
2553 struct device *dev = &pdev->dev;
2554 struct device_node *np = dev->of_node;
2555 struct device_node *child;
2559 rockchip_pinctrl_child_count(info, np);
2561 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
2562 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
2564 info->functions = devm_kcalloc(dev,
2566 sizeof(struct rockchip_pmx_func),
2568 if (!info->functions)
2571 info->groups = devm_kcalloc(dev,
2573 sizeof(struct rockchip_pin_group),
2580 for_each_child_of_node(np, child) {
2581 if (of_match_node(rockchip_bank_match, child))
2584 ret = rockchip_pinctrl_parse_functions(child, info, i++);
2586 dev_err(&pdev->dev, "failed to parse function\n");
2595 static int rockchip_pinctrl_register(struct platform_device *pdev,
2596 struct rockchip_pinctrl *info)
2598 struct pinctrl_desc *ctrldesc = &info->pctl;
2599 struct pinctrl_pin_desc *pindesc, *pdesc;
2600 struct rockchip_pin_bank *pin_bank;
2604 ctrldesc->name = "rockchip-pinctrl";
2605 ctrldesc->owner = THIS_MODULE;
2606 ctrldesc->pctlops = &rockchip_pctrl_ops;
2607 ctrldesc->pmxops = &rockchip_pmx_ops;
2608 ctrldesc->confops = &rockchip_pinconf_ops;
2610 pindesc = devm_kcalloc(&pdev->dev,
2611 info->ctrl->nr_pins, sizeof(*pindesc),
2616 ctrldesc->pins = pindesc;
2617 ctrldesc->npins = info->ctrl->nr_pins;
2620 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
2621 pin_bank = &info->ctrl->pin_banks[bank];
2622 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
2624 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
2625 pin_bank->name, pin);
2630 ret = rockchip_pinctrl_parse_dt(pdev, info);
2634 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
2635 if (IS_ERR(info->pctl_dev)) {
2636 dev_err(&pdev->dev, "could not register pinctrl driver\n");
2637 return PTR_ERR(info->pctl_dev);
2640 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
2641 pin_bank = &info->ctrl->pin_banks[bank];
2642 pin_bank->grange.name = pin_bank->name;
2643 pin_bank->grange.id = bank;
2644 pin_bank->grange.pin_base = pin_bank->pin_base;
2645 pin_bank->grange.base = pin_bank->gpio_chip.base;
2646 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
2647 pin_bank->grange.gc = &pin_bank->gpio_chip;
2648 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
2658 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
2660 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2661 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
2662 unsigned long flags;
2665 clk_enable(bank->clk);
2666 raw_spin_lock_irqsave(&bank->slock, flags);
2669 data &= ~BIT(offset);
2671 data |= BIT(offset);
2674 raw_spin_unlock_irqrestore(&bank->slock, flags);
2675 clk_disable(bank->clk);
2679 * Returns the level of the pin for input direction and setting of the DR
2680 * register for output gpios.
2682 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
2684 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2687 clk_enable(bank->clk);
2688 data = readl(bank->reg_base + GPIO_EXT_PORT);
2689 clk_disable(bank->clk);
2696 * gpiolib gpio_direction_input callback function. The setting of the pin
2697 * mux function as 'gpio input' will be handled by the pinctrl subsystem
2700 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
2702 return pinctrl_gpio_direction_input(gc->base + offset);
2706 * gpiolib gpio_direction_output callback function. The setting of the pin
2707 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2710 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
2711 unsigned offset, int value)
2713 rockchip_gpio_set(gc, offset, value);
2714 return pinctrl_gpio_direction_output(gc->base + offset);
2717 static void rockchip_gpio_set_debounce(struct gpio_chip *gc,
2718 unsigned int offset, bool enable)
2720 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2721 void __iomem *reg = bank->reg_base + GPIO_DEBOUNCE;
2722 unsigned long flags;
2725 clk_enable(bank->clk);
2726 raw_spin_lock_irqsave(&bank->slock, flags);
2730 data |= BIT(offset);
2732 data &= ~BIT(offset);
2735 raw_spin_unlock_irqrestore(&bank->slock, flags);
2736 clk_disable(bank->clk);
2740 * gpiolib set_config callback function. The setting of the pin
2741 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2744 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
2745 unsigned long config)
2747 enum pin_config_param param = pinconf_to_config_param(config);
2750 case PIN_CONFIG_INPUT_DEBOUNCE:
2751 rockchip_gpio_set_debounce(gc, offset, true);
2753 * Rockchip's gpio could only support up to one period
2754 * of the debounce clock(pclk), which is far away from
2755 * satisftying the requirement, as pclk is usually near
2756 * 100MHz shared by all peripherals. So the fact is it
2757 * has crippled debounce capability could only be useful
2758 * to prevent any spurious glitches from waking up the system
2759 * if the gpio is conguired as wakeup interrupt source. Let's
2760 * still return -ENOTSUPP as before, to make sure the caller
2761 * of gpiod_set_debounce won't change its behaviour.
2769 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
2770 * and a virtual IRQ, if not already present.
2772 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
2774 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2780 virq = irq_create_mapping(bank->domain, offset);
2782 return (virq) ? : -ENXIO;
2785 static const struct gpio_chip rockchip_gpiolib_chip = {
2786 .request = gpiochip_generic_request,
2787 .free = gpiochip_generic_free,
2788 .set = rockchip_gpio_set,
2789 .get = rockchip_gpio_get,
2790 .get_direction = rockchip_gpio_get_direction,
2791 .direction_input = rockchip_gpio_direction_input,
2792 .direction_output = rockchip_gpio_direction_output,
2793 .set_config = rockchip_gpio_set_config,
2794 .to_irq = rockchip_gpio_to_irq,
2795 .owner = THIS_MODULE,
2799 * Interrupt handling
2802 static void rockchip_irq_demux(struct irq_desc *desc)
2804 struct irq_chip *chip = irq_desc_get_chip(desc);
2805 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
2808 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
2810 chained_irq_enter(chip, desc);
2812 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
2815 unsigned int irq, virq;
2819 virq = irq_linear_revmap(bank->domain, irq);
2822 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
2826 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
2829 * Triggering IRQ on both rising and falling edge
2830 * needs manual intervention.
2832 if (bank->toggle_edge_mode & BIT(irq)) {
2833 u32 data, data_old, polarity;
2834 unsigned long flags;
2836 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
2838 raw_spin_lock_irqsave(&bank->slock, flags);
2840 polarity = readl_relaxed(bank->reg_base +
2842 if (data & BIT(irq))
2843 polarity &= ~BIT(irq);
2845 polarity |= BIT(irq);
2847 bank->reg_base + GPIO_INT_POLARITY);
2849 raw_spin_unlock_irqrestore(&bank->slock, flags);
2852 data = readl_relaxed(bank->reg_base +
2854 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
2857 generic_handle_irq(virq);
2860 chained_irq_exit(chip, desc);
2863 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
2865 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2866 struct rockchip_pin_bank *bank = gc->private;
2867 u32 mask = BIT(d->hwirq);
2871 unsigned long flags;
2874 /* make sure the pin is configured as gpio input */
2875 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
2879 clk_enable(bank->clk);
2880 raw_spin_lock_irqsave(&bank->slock, flags);
2882 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2884 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2886 raw_spin_unlock_irqrestore(&bank->slock, flags);
2888 if (type & IRQ_TYPE_EDGE_BOTH)
2889 irq_set_handler_locked(d, handle_edge_irq);
2891 irq_set_handler_locked(d, handle_level_irq);
2893 raw_spin_lock_irqsave(&bank->slock, flags);
2896 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
2897 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
2900 case IRQ_TYPE_EDGE_BOTH:
2901 bank->toggle_edge_mode |= mask;
2905 * Determine gpio state. If 1 next interrupt should be falling
2908 data = readl(bank->reg_base + GPIO_EXT_PORT);
2914 case IRQ_TYPE_EDGE_RISING:
2915 bank->toggle_edge_mode &= ~mask;
2919 case IRQ_TYPE_EDGE_FALLING:
2920 bank->toggle_edge_mode &= ~mask;
2924 case IRQ_TYPE_LEVEL_HIGH:
2925 bank->toggle_edge_mode &= ~mask;
2929 case IRQ_TYPE_LEVEL_LOW:
2930 bank->toggle_edge_mode &= ~mask;
2936 raw_spin_unlock_irqrestore(&bank->slock, flags);
2937 clk_disable(bank->clk);
2941 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
2942 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
2945 raw_spin_unlock_irqrestore(&bank->slock, flags);
2946 clk_disable(bank->clk);
2951 static void rockchip_irq_suspend(struct irq_data *d)
2953 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2954 struct rockchip_pin_bank *bank = gc->private;
2956 clk_enable(bank->clk);
2957 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
2958 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
2959 clk_disable(bank->clk);
2962 static void rockchip_irq_resume(struct irq_data *d)
2964 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2965 struct rockchip_pin_bank *bank = gc->private;
2967 clk_enable(bank->clk);
2968 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
2969 clk_disable(bank->clk);
2972 static void rockchip_irq_enable(struct irq_data *d)
2974 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2975 struct rockchip_pin_bank *bank = gc->private;
2977 clk_enable(bank->clk);
2978 irq_gc_mask_clr_bit(d);
2981 static void rockchip_irq_disable(struct irq_data *d)
2983 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2984 struct rockchip_pin_bank *bank = gc->private;
2986 irq_gc_mask_set_bit(d);
2987 clk_disable(bank->clk);
2990 static int rockchip_interrupts_register(struct platform_device *pdev,
2991 struct rockchip_pinctrl *info)
2993 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2994 struct rockchip_pin_bank *bank = ctrl->pin_banks;
2995 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
2996 struct irq_chip_generic *gc;
3000 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3002 dev_warn(&pdev->dev, "bank %s is not valid\n",
3007 ret = clk_enable(bank->clk);
3009 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
3014 bank->domain = irq_domain_add_linear(bank->of_node, 32,
3015 &irq_generic_chip_ops, NULL);
3016 if (!bank->domain) {
3017 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
3019 clk_disable(bank->clk);
3023 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
3024 "rockchip_gpio_irq", handle_level_irq,
3025 clr, 0, IRQ_GC_INIT_MASK_CACHE);
3027 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
3029 irq_domain_remove(bank->domain);
3030 clk_disable(bank->clk);
3035 * Linux assumes that all interrupts start out disabled/masked.
3036 * Our driver only uses the concept of masked and always keeps
3037 * things enabled, so for us that's all masked and all enabled.
3039 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
3040 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
3042 gc = irq_get_domain_generic_chip(bank->domain, 0);
3043 gc->reg_base = bank->reg_base;
3045 gc->chip_types[0].regs.mask = GPIO_INTMASK;
3046 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
3047 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
3048 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
3049 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
3050 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
3051 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
3052 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
3053 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
3054 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
3055 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
3056 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
3058 irq_set_chained_handler_and_data(bank->irq,
3059 rockchip_irq_demux, bank);
3061 /* map the gpio irqs here, when the clock is still running */
3062 for (j = 0 ; j < 32 ; j++)
3063 irq_create_mapping(bank->domain, j);
3065 clk_disable(bank->clk);
3071 static int rockchip_gpiolib_register(struct platform_device *pdev,
3072 struct rockchip_pinctrl *info)
3074 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3075 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3076 struct gpio_chip *gc;
3080 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3082 dev_warn(&pdev->dev, "bank %s is not valid\n",
3087 bank->gpio_chip = rockchip_gpiolib_chip;
3089 gc = &bank->gpio_chip;
3090 gc->base = bank->pin_base;
3091 gc->ngpio = bank->nr_pins;
3092 gc->parent = &pdev->dev;
3093 gc->of_node = bank->of_node;
3094 gc->label = bank->name;
3096 ret = gpiochip_add_data(gc, bank);
3098 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
3104 rockchip_interrupts_register(pdev, info);
3109 for (--i, --bank; i >= 0; --i, --bank) {
3112 gpiochip_remove(&bank->gpio_chip);
3117 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
3118 struct rockchip_pinctrl *info)
3120 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3121 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3124 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3127 gpiochip_remove(&bank->gpio_chip);
3133 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
3134 struct rockchip_pinctrl *info)
3136 struct resource res;
3139 if (of_address_to_resource(bank->of_node, 0, &res)) {
3140 dev_err(info->dev, "cannot find IO resource for bank\n");
3144 bank->reg_base = devm_ioremap_resource(info->dev, &res);
3145 if (IS_ERR(bank->reg_base))
3146 return PTR_ERR(bank->reg_base);
3149 * special case, where parts of the pull setting-registers are
3150 * part of the PMU register space
3152 if (of_device_is_compatible(bank->of_node,
3153 "rockchip,rk3188-gpio-bank0")) {
3154 struct device_node *node;
3156 node = of_parse_phandle(bank->of_node->parent,
3159 if (of_address_to_resource(bank->of_node, 1, &res)) {
3160 dev_err(info->dev, "cannot find IO resource for bank\n");
3164 base = devm_ioremap_resource(info->dev, &res);
3166 return PTR_ERR(base);
3167 rockchip_regmap_config.max_register =
3168 resource_size(&res) - 4;
3169 rockchip_regmap_config.name =
3170 "rockchip,rk3188-gpio-bank0-pull";
3171 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
3173 &rockchip_regmap_config);
3177 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
3179 bank->clk = of_clk_get(bank->of_node, 0);
3180 if (IS_ERR(bank->clk))
3181 return PTR_ERR(bank->clk);
3183 return clk_prepare(bank->clk);
3186 static const struct of_device_id rockchip_pinctrl_dt_match[];
3188 /* retrieve the soc specific data */
3189 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
3190 struct rockchip_pinctrl *d,
3191 struct platform_device *pdev)
3193 const struct of_device_id *match;
3194 struct device_node *node = pdev->dev.of_node;
3195 struct device_node *np;
3196 struct rockchip_pin_ctrl *ctrl;
3197 struct rockchip_pin_bank *bank;
3198 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
3200 match = of_match_node(rockchip_pinctrl_dt_match, node);
3201 ctrl = (struct rockchip_pin_ctrl *)match->data;
3203 for_each_child_of_node(node, np) {
3204 if (!of_find_property(np, "gpio-controller", NULL))
3207 bank = ctrl->pin_banks;
3208 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3209 if (!strcmp(bank->name, np->name)) {
3212 if (!rockchip_get_bank_data(bank, d))
3220 grf_offs = ctrl->grf_mux_offset;
3221 pmu_offs = ctrl->pmu_mux_offset;
3222 drv_pmu_offs = ctrl->pmu_drv_offset;
3223 drv_grf_offs = ctrl->grf_drv_offset;
3224 bank = ctrl->pin_banks;
3225 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3228 raw_spin_lock_init(&bank->slock);
3230 bank->pin_base = ctrl->nr_pins;
3231 ctrl->nr_pins += bank->nr_pins;
3233 /* calculate iomux and drv offsets */
3234 for (j = 0; j < 4; j++) {
3235 struct rockchip_iomux *iom = &bank->iomux[j];
3236 struct rockchip_drv *drv = &bank->drv[j];
3239 if (bank_pins >= bank->nr_pins)
3242 /* preset iomux offset value, set new start value */
3243 if (iom->offset >= 0) {
3244 if (iom->type & IOMUX_SOURCE_PMU)
3245 pmu_offs = iom->offset;
3247 grf_offs = iom->offset;
3248 } else { /* set current iomux offset */
3249 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3250 pmu_offs : grf_offs;
3253 /* preset drv offset value, set new start value */
3254 if (drv->offset >= 0) {
3255 if (iom->type & IOMUX_SOURCE_PMU)
3256 drv_pmu_offs = drv->offset;
3258 drv_grf_offs = drv->offset;
3259 } else { /* set current drv offset */
3260 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3261 drv_pmu_offs : drv_grf_offs;
3264 dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
3265 i, j, iom->offset, drv->offset);
3268 * Increase offset according to iomux width.
3269 * 4bit iomux'es are spread over two registers.
3271 inc = (iom->type & (IOMUX_WIDTH_4BIT |
3272 IOMUX_WIDTH_3BIT)) ? 8 : 4;
3273 if (iom->type & IOMUX_SOURCE_PMU)
3279 * Increase offset according to drv width.
3280 * 3bit drive-strenth'es are spread over two registers.
3282 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
3283 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
3288 if (iom->type & IOMUX_SOURCE_PMU)
3289 drv_pmu_offs += inc;
3291 drv_grf_offs += inc;
3296 /* calculate the per-bank recalced_mask */
3297 for (j = 0; j < ctrl->niomux_recalced; j++) {
3300 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
3301 pin = ctrl->iomux_recalced[j].pin;
3302 bank->recalced_mask |= BIT(pin);
3306 /* calculate the per-bank route_mask */
3307 for (j = 0; j < ctrl->niomux_routes; j++) {
3310 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
3311 pin = ctrl->iomux_routes[j].pin;
3312 bank->route_mask |= BIT(pin);
3320 #define RK3288_GRF_GPIO6C_IOMUX 0x64
3321 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
3323 static u32 rk3288_grf_gpio6c_iomux;
3325 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
3327 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3328 int ret = pinctrl_force_sleep(info->pctl_dev);
3334 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
3335 * the setting here, and restore it at resume.
3337 if (info->ctrl->type == RK3288) {
3338 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3339 &rk3288_grf_gpio6c_iomux);
3341 pinctrl_force_default(info->pctl_dev);
3349 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
3351 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3352 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3353 rk3288_grf_gpio6c_iomux |
3354 GPIO6C6_SEL_WRITE_ENABLE);
3359 return pinctrl_force_default(info->pctl_dev);
3362 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
3363 rockchip_pinctrl_resume);
3365 static int rockchip_pinctrl_probe(struct platform_device *pdev)
3367 struct rockchip_pinctrl *info;
3368 struct device *dev = &pdev->dev;
3369 struct rockchip_pin_ctrl *ctrl;
3370 struct device_node *np = pdev->dev.of_node, *node;
3371 struct resource *res;
3375 if (!dev->of_node) {
3376 dev_err(dev, "device tree node not found\n");
3380 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3386 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
3388 dev_err(dev, "driver data not available\n");
3393 node = of_parse_phandle(np, "rockchip,grf", 0);
3395 info->regmap_base = syscon_node_to_regmap(node);
3396 if (IS_ERR(info->regmap_base))
3397 return PTR_ERR(info->regmap_base);
3399 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3400 base = devm_ioremap_resource(&pdev->dev, res);
3402 return PTR_ERR(base);
3404 rockchip_regmap_config.max_register = resource_size(res) - 4;
3405 rockchip_regmap_config.name = "rockchip,pinctrl";
3406 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
3407 &rockchip_regmap_config);
3409 /* to check for the old dt-bindings */
3410 info->reg_size = resource_size(res);
3412 /* Honor the old binding, with pull registers as 2nd resource */
3413 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
3414 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3415 base = devm_ioremap_resource(&pdev->dev, res);
3417 return PTR_ERR(base);
3419 rockchip_regmap_config.max_register =
3420 resource_size(res) - 4;
3421 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
3422 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
3424 &rockchip_regmap_config);
3428 /* try to find the optional reference to the pmu syscon */
3429 node = of_parse_phandle(np, "rockchip,pmu", 0);
3431 info->regmap_pmu = syscon_node_to_regmap(node);
3432 if (IS_ERR(info->regmap_pmu))
3433 return PTR_ERR(info->regmap_pmu);
3436 ret = rockchip_gpiolib_register(pdev, info);
3440 ret = rockchip_pinctrl_register(pdev, info);
3442 rockchip_gpiolib_unregister(pdev, info);
3446 platform_set_drvdata(pdev, info);
3451 static struct rockchip_pin_bank px30_pin_banks[] = {
3452 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3457 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_WIDTH_4BIT,
3462 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_WIDTH_4BIT,
3467 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_WIDTH_4BIT,
3474 static struct rockchip_pin_ctrl px30_pin_ctrl = {
3475 .pin_banks = px30_pin_banks,
3476 .nr_banks = ARRAY_SIZE(px30_pin_banks),
3477 .label = "PX30-GPIO",
3479 .grf_mux_offset = 0x0,
3480 .pmu_mux_offset = 0x0,
3481 .iomux_routes = px30_mux_route_data,
3482 .niomux_routes = ARRAY_SIZE(px30_mux_route_data),
3483 .pull_calc_reg = px30_calc_pull_reg_and_bit,
3484 .drv_calc_reg = px30_calc_drv_reg_and_bit,
3485 .schmitt_calc_reg = px30_calc_schmitt_reg_and_bit,
3488 static struct rockchip_pin_bank rv1108_pin_banks[] = {
3489 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3493 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3494 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
3495 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
3498 static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
3499 .pin_banks = rv1108_pin_banks,
3500 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
3501 .label = "RV1108-GPIO",
3503 .grf_mux_offset = 0x10,
3504 .pmu_mux_offset = 0x0,
3505 .iomux_recalced = rv1108_mux_recalced_data,
3506 .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data),
3507 .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
3508 .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
3509 .schmitt_calc_reg = rv1108_calc_schmitt_reg_and_bit,
3512 static struct rockchip_pin_bank rk2928_pin_banks[] = {
3513 PIN_BANK(0, 32, "gpio0"),
3514 PIN_BANK(1, 32, "gpio1"),
3515 PIN_BANK(2, 32, "gpio2"),
3516 PIN_BANK(3, 32, "gpio3"),
3519 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
3520 .pin_banks = rk2928_pin_banks,
3521 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
3522 .label = "RK2928-GPIO",
3524 .grf_mux_offset = 0xa8,
3525 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3528 static struct rockchip_pin_bank rk3036_pin_banks[] = {
3529 PIN_BANK(0, 32, "gpio0"),
3530 PIN_BANK(1, 32, "gpio1"),
3531 PIN_BANK(2, 32, "gpio2"),
3534 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
3535 .pin_banks = rk3036_pin_banks,
3536 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
3537 .label = "RK3036-GPIO",
3539 .grf_mux_offset = 0xa8,
3540 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3543 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
3544 PIN_BANK(0, 32, "gpio0"),
3545 PIN_BANK(1, 32, "gpio1"),
3546 PIN_BANK(2, 32, "gpio2"),
3547 PIN_BANK(3, 32, "gpio3"),
3548 PIN_BANK(4, 32, "gpio4"),
3549 PIN_BANK(6, 16, "gpio6"),
3552 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
3553 .pin_banks = rk3066a_pin_banks,
3554 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
3555 .label = "RK3066a-GPIO",
3557 .grf_mux_offset = 0xa8,
3558 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3561 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
3562 PIN_BANK(0, 32, "gpio0"),
3563 PIN_BANK(1, 32, "gpio1"),
3564 PIN_BANK(2, 32, "gpio2"),
3565 PIN_BANK(3, 32, "gpio3"),
3568 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
3569 .pin_banks = rk3066b_pin_banks,
3570 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
3571 .label = "RK3066b-GPIO",
3573 .grf_mux_offset = 0x60,
3576 static struct rockchip_pin_bank rk3128_pin_banks[] = {
3577 PIN_BANK(0, 32, "gpio0"),
3578 PIN_BANK(1, 32, "gpio1"),
3579 PIN_BANK(2, 32, "gpio2"),
3580 PIN_BANK(3, 32, "gpio3"),
3583 static struct rockchip_pin_ctrl rk3128_pin_ctrl = {
3584 .pin_banks = rk3128_pin_banks,
3585 .nr_banks = ARRAY_SIZE(rk3128_pin_banks),
3586 .label = "RK3128-GPIO",
3588 .grf_mux_offset = 0xa8,
3589 .iomux_recalced = rk3128_mux_recalced_data,
3590 .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data),
3591 .iomux_routes = rk3128_mux_route_data,
3592 .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data),
3593 .pull_calc_reg = rk3128_calc_pull_reg_and_bit,
3596 static struct rockchip_pin_bank rk3188_pin_banks[] = {
3597 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
3598 PIN_BANK(1, 32, "gpio1"),
3599 PIN_BANK(2, 32, "gpio2"),
3600 PIN_BANK(3, 32, "gpio3"),
3603 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
3604 .pin_banks = rk3188_pin_banks,
3605 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
3606 .label = "RK3188-GPIO",
3608 .grf_mux_offset = 0x60,
3609 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
3612 static struct rockchip_pin_bank rk3228_pin_banks[] = {
3613 PIN_BANK(0, 32, "gpio0"),
3614 PIN_BANK(1, 32, "gpio1"),
3615 PIN_BANK(2, 32, "gpio2"),
3616 PIN_BANK(3, 32, "gpio3"),
3619 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
3620 .pin_banks = rk3228_pin_banks,
3621 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
3622 .label = "RK3228-GPIO",
3624 .grf_mux_offset = 0x0,
3625 .iomux_routes = rk3228_mux_route_data,
3626 .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data),
3627 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3628 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3631 static struct rockchip_pin_bank rk3288_pin_banks[] = {
3632 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
3637 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
3642 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
3643 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
3644 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
3649 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
3654 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
3655 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
3660 PIN_BANK(8, 16, "gpio8"),
3663 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
3664 .pin_banks = rk3288_pin_banks,
3665 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
3666 .label = "RK3288-GPIO",
3668 .grf_mux_offset = 0x0,
3669 .pmu_mux_offset = 0x84,
3670 .iomux_routes = rk3288_mux_route_data,
3671 .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
3672 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
3673 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
3676 static struct rockchip_pin_bank rk3328_pin_banks[] = {
3677 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
3678 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3679 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
3683 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
3690 static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
3691 .pin_banks = rk3328_pin_banks,
3692 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
3693 .label = "RK3328-GPIO",
3695 .grf_mux_offset = 0x0,
3696 .iomux_recalced = rk3328_mux_recalced_data,
3697 .niomux_recalced = ARRAY_SIZE(rk3328_mux_recalced_data),
3698 .iomux_routes = rk3328_mux_route_data,
3699 .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data),
3700 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3701 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3702 .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
3705 static struct rockchip_pin_bank rk3368_pin_banks[] = {
3706 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3711 PIN_BANK(1, 32, "gpio1"),
3712 PIN_BANK(2, 32, "gpio2"),
3713 PIN_BANK(3, 32, "gpio3"),
3716 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
3717 .pin_banks = rk3368_pin_banks,
3718 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
3719 .label = "RK3368-GPIO",
3721 .grf_mux_offset = 0x0,
3722 .pmu_mux_offset = 0x0,
3723 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
3724 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
3727 static struct rockchip_pin_bank rk3399_pin_banks[] = {
3728 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
3733 DRV_TYPE_IO_1V8_ONLY,
3734 DRV_TYPE_IO_1V8_ONLY,
3735 DRV_TYPE_IO_DEFAULT,
3736 DRV_TYPE_IO_DEFAULT,
3741 PULL_TYPE_IO_1V8_ONLY,
3742 PULL_TYPE_IO_1V8_ONLY,
3743 PULL_TYPE_IO_DEFAULT,
3744 PULL_TYPE_IO_DEFAULT
3746 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
3750 DRV_TYPE_IO_1V8_OR_3V0,
3751 DRV_TYPE_IO_1V8_OR_3V0,
3752 DRV_TYPE_IO_1V8_OR_3V0,
3753 DRV_TYPE_IO_1V8_OR_3V0,
3759 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
3760 DRV_TYPE_IO_1V8_OR_3V0,
3761 DRV_TYPE_IO_1V8_ONLY,
3762 DRV_TYPE_IO_1V8_ONLY,
3763 PULL_TYPE_IO_DEFAULT,
3764 PULL_TYPE_IO_DEFAULT,
3765 PULL_TYPE_IO_1V8_ONLY,
3766 PULL_TYPE_IO_1V8_ONLY
3768 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
3769 DRV_TYPE_IO_3V3_ONLY,
3770 DRV_TYPE_IO_3V3_ONLY,
3771 DRV_TYPE_IO_1V8_OR_3V0
3773 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
3774 DRV_TYPE_IO_1V8_3V0_AUTO,
3775 DRV_TYPE_IO_1V8_OR_3V0,
3776 DRV_TYPE_IO_1V8_OR_3V0
3780 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
3781 .pin_banks = rk3399_pin_banks,
3782 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
3783 .label = "RK3399-GPIO",
3785 .grf_mux_offset = 0xe000,
3786 .pmu_mux_offset = 0x0,
3787 .grf_drv_offset = 0xe100,
3788 .pmu_drv_offset = 0x80,
3789 .iomux_routes = rk3399_mux_route_data,
3790 .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
3791 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
3792 .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
3795 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
3796 { .compatible = "rockchip,px30-pinctrl",
3797 .data = &px30_pin_ctrl },
3798 { .compatible = "rockchip,rv1108-pinctrl",
3799 .data = &rv1108_pin_ctrl },
3800 { .compatible = "rockchip,rk2928-pinctrl",
3801 .data = &rk2928_pin_ctrl },
3802 { .compatible = "rockchip,rk3036-pinctrl",
3803 .data = &rk3036_pin_ctrl },
3804 { .compatible = "rockchip,rk3066a-pinctrl",
3805 .data = &rk3066a_pin_ctrl },
3806 { .compatible = "rockchip,rk3066b-pinctrl",
3807 .data = &rk3066b_pin_ctrl },
3808 { .compatible = "rockchip,rk3128-pinctrl",
3809 .data = (void *)&rk3128_pin_ctrl },
3810 { .compatible = "rockchip,rk3188-pinctrl",
3811 .data = &rk3188_pin_ctrl },
3812 { .compatible = "rockchip,rk3228-pinctrl",
3813 .data = &rk3228_pin_ctrl },
3814 { .compatible = "rockchip,rk3288-pinctrl",
3815 .data = &rk3288_pin_ctrl },
3816 { .compatible = "rockchip,rk3328-pinctrl",
3817 .data = &rk3328_pin_ctrl },
3818 { .compatible = "rockchip,rk3368-pinctrl",
3819 .data = &rk3368_pin_ctrl },
3820 { .compatible = "rockchip,rk3399-pinctrl",
3821 .data = &rk3399_pin_ctrl },
3825 static struct platform_driver rockchip_pinctrl_driver = {
3826 .probe = rockchip_pinctrl_probe,
3828 .name = "rockchip-pinctrl",
3829 .pm = &rockchip_pinctrl_dev_pm_ops,
3830 .of_match_table = rockchip_pinctrl_dt_match,
3834 static int __init rockchip_pinctrl_drv_register(void)
3836 return platform_driver_register(&rockchip_pinctrl_driver);
3838 postcore_initcall(rockchip_pinctrl_drv_register);