1 // SPDX-License-Identifier: GPL-2.0-only
3 * Pinctrl driver for Rockchip SoCs
5 * Copyright (c) 2013 MundoReader S.L.
6 * Author: Heiko Stuebner <heiko@sntech.de>
8 * With some ideas taken from pinctrl-samsung:
9 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
10 * http://www.samsung.com
11 * Copyright (c) 2012 Linaro Ltd
12 * http://www.linaro.org
15 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
21 #include <linux/bitops.h>
22 #include <linux/gpio/driver.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/pinctrl/pinconf-generic.h>
30 #include <linux/irqchip/chained_irq.h>
31 #include <linux/clk.h>
32 #include <linux/regmap.h>
33 #include <linux/mfd/syscon.h>
34 #include <dt-bindings/pinctrl/rockchip.h>
39 /* GPIO control registers */
40 #define GPIO_SWPORT_DR 0x00
41 #define GPIO_SWPORT_DDR 0x04
42 #define GPIO_INTEN 0x30
43 #define GPIO_INTMASK 0x34
44 #define GPIO_INTTYPE_LEVEL 0x38
45 #define GPIO_INT_POLARITY 0x3c
46 #define GPIO_INT_STATUS 0x40
47 #define GPIO_INT_RAWSTATUS 0x44
48 #define GPIO_DEBOUNCE 0x48
49 #define GPIO_PORTS_EOI 0x4c
50 #define GPIO_EXT_PORT 0x50
51 #define GPIO_LS_SYNC 0x60
53 enum rockchip_pinctrl_type {
66 * Encode variants of iomux registers into a type variable
68 #define IOMUX_GPIO_ONLY BIT(0)
69 #define IOMUX_WIDTH_4BIT BIT(1)
70 #define IOMUX_SOURCE_PMU BIT(2)
71 #define IOMUX_UNROUTED BIT(3)
72 #define IOMUX_WIDTH_3BIT BIT(4)
75 * @type: iomux variant using IOMUX_* constants
76 * @offset: if initialized to -1 it will be autocalculated, by specifying
77 * an initial offset value the relevant source offset can be reset
78 * to a new value for autocalculating the following iomux registers.
80 struct rockchip_iomux {
86 * enum type index corresponding to rockchip_perpin_drv_list arrays index.
88 enum rockchip_pin_drv_type {
89 DRV_TYPE_IO_DEFAULT = 0,
90 DRV_TYPE_IO_1V8_OR_3V0,
92 DRV_TYPE_IO_1V8_3V0_AUTO,
98 * enum type index corresponding to rockchip_pull_list arrays index.
100 enum rockchip_pin_pull_type {
101 PULL_TYPE_IO_DEFAULT = 0,
102 PULL_TYPE_IO_1V8_ONLY,
107 * @drv_type: drive strength variant using rockchip_perpin_drv_type
108 * @offset: if initialized to -1 it will be autocalculated, by specifying
109 * an initial offset value the relevant source offset can be reset
110 * to a new value for autocalculating the following drive strength
111 * registers. if used chips own cal_drv func instead to calculate
112 * registers offset, the variant could be ignored.
114 struct rockchip_drv {
115 enum rockchip_pin_drv_type drv_type;
120 * @reg_base: register base of the gpio bank
121 * @reg_pull: optional separate register for additional pull settings
122 * @clk: clock of the gpio bank
123 * @irq: interrupt of the gpio bank
124 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
125 * @pin_base: first pin number
126 * @nr_pins: number of pins in this bank
127 * @name: name of the bank
128 * @bank_num: number of the bank, to account for holes
129 * @iomux: array describing the 4 iomux sources of the bank
130 * @drv: array describing the 4 drive strength sources of the bank
131 * @pull_type: array describing the 4 pull type sources of the bank
132 * @valid: is all necessary information present
133 * @of_node: dt node of this bank
134 * @drvdata: common pinctrl basedata
135 * @domain: irqdomain of the gpio bank
136 * @gpio_chip: gpiolib chip
137 * @grange: gpio range
138 * @slock: spinlock for the gpio bank
139 * @route_mask: bits describing the routing pins of per bank
141 struct rockchip_pin_bank {
142 void __iomem *reg_base;
143 struct regmap *regmap_pull;
151 struct rockchip_iomux iomux[4];
152 struct rockchip_drv drv[4];
153 enum rockchip_pin_pull_type pull_type[4];
155 struct device_node *of_node;
156 struct rockchip_pinctrl *drvdata;
157 struct irq_domain *domain;
158 struct gpio_chip gpio_chip;
159 struct pinctrl_gpio_range grange;
160 raw_spinlock_t slock;
161 u32 toggle_edge_mode;
166 #define PIN_BANK(id, pins, label) \
179 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
185 { .type = iom0, .offset = -1 }, \
186 { .type = iom1, .offset = -1 }, \
187 { .type = iom2, .offset = -1 }, \
188 { .type = iom3, .offset = -1 }, \
192 #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \
204 { .drv_type = type0, .offset = -1 }, \
205 { .drv_type = type1, .offset = -1 }, \
206 { .drv_type = type2, .offset = -1 }, \
207 { .drv_type = type3, .offset = -1 }, \
211 #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \
212 drv2, drv3, pull0, pull1, \
225 { .drv_type = drv0, .offset = -1 }, \
226 { .drv_type = drv1, .offset = -1 }, \
227 { .drv_type = drv2, .offset = -1 }, \
228 { .drv_type = drv3, .offset = -1 }, \
230 .pull_type[0] = pull0, \
231 .pull_type[1] = pull1, \
232 .pull_type[2] = pull2, \
233 .pull_type[3] = pull3, \
236 #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \
237 iom2, iom3, drv0, drv1, drv2, \
238 drv3, offset0, offset1, \
245 { .type = iom0, .offset = -1 }, \
246 { .type = iom1, .offset = -1 }, \
247 { .type = iom2, .offset = -1 }, \
248 { .type = iom3, .offset = -1 }, \
251 { .drv_type = drv0, .offset = offset0 }, \
252 { .drv_type = drv1, .offset = offset1 }, \
253 { .drv_type = drv2, .offset = offset2 }, \
254 { .drv_type = drv3, .offset = offset3 }, \
258 #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \
259 label, iom0, iom1, iom2, \
260 iom3, drv0, drv1, drv2, \
261 drv3, offset0, offset1, \
262 offset2, offset3, pull0, \
263 pull1, pull2, pull3) \
269 { .type = iom0, .offset = -1 }, \
270 { .type = iom1, .offset = -1 }, \
271 { .type = iom2, .offset = -1 }, \
272 { .type = iom3, .offset = -1 }, \
275 { .drv_type = drv0, .offset = offset0 }, \
276 { .drv_type = drv1, .offset = offset1 }, \
277 { .drv_type = drv2, .offset = offset2 }, \
278 { .drv_type = drv3, .offset = offset3 }, \
280 .pull_type[0] = pull0, \
281 .pull_type[1] = pull1, \
282 .pull_type[2] = pull2, \
283 .pull_type[3] = pull3, \
287 * struct rockchip_mux_recalced_data: represent a pin iomux data.
290 * @bit: index at register.
291 * @reg: register offset.
294 struct rockchip_mux_recalced_data {
302 enum rockchip_mux_route_location {
303 ROCKCHIP_ROUTE_SAME = 0,
309 * struct rockchip_mux_recalced_data: represent a pin iomux data.
310 * @bank_num: bank number.
311 * @pin: index at register or used to calc index.
312 * @func: the min pin.
313 * @route_offset: the max pin.
314 * @route_val: the register offset.
316 struct rockchip_mux_route_data {
320 enum rockchip_mux_route_location route_location;
327 struct rockchip_pin_ctrl {
328 struct rockchip_pin_bank *pin_banks;
332 enum rockchip_pinctrl_type type;
337 struct rockchip_mux_recalced_data *iomux_recalced;
339 struct rockchip_mux_route_data *iomux_routes;
342 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
343 int pin_num, struct regmap **regmap,
345 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
346 int pin_num, struct regmap **regmap,
348 int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank,
349 int pin_num, struct regmap **regmap,
353 struct rockchip_pin_config {
355 unsigned long *configs;
356 unsigned int nconfigs;
360 * struct rockchip_pin_group: represent group of pins of a pinmux function.
361 * @name: name of the pin group, used to lookup the group.
362 * @pins: the pins included in this group.
363 * @npins: number of pins included in this group.
364 * @func: the mux function number to be programmed when selected.
365 * @configs: the config values to be set for each pin
366 * @nconfigs: number of configs for each pin
368 struct rockchip_pin_group {
372 struct rockchip_pin_config *data;
376 * struct rockchip_pmx_func: represent a pin function.
377 * @name: name of the pin function, used to lookup the function.
378 * @groups: one or more names of pin groups that provide this function.
379 * @num_groups: number of groups included in @groups.
381 struct rockchip_pmx_func {
387 struct rockchip_pinctrl {
388 struct regmap *regmap_base;
390 struct regmap *regmap_pull;
391 struct regmap *regmap_pmu;
393 struct rockchip_pin_ctrl *ctrl;
394 struct pinctrl_desc pctl;
395 struct pinctrl_dev *pctl_dev;
396 struct rockchip_pin_group *groups;
397 unsigned int ngroups;
398 struct rockchip_pmx_func *functions;
399 unsigned int nfunctions;
402 static struct regmap_config rockchip_regmap_config = {
408 static inline const struct rockchip_pin_group *pinctrl_name_to_group(
409 const struct rockchip_pinctrl *info,
414 for (i = 0; i < info->ngroups; i++) {
415 if (!strcmp(info->groups[i].name, name))
416 return &info->groups[i];
423 * given a pin number that is local to a pin controller, find out the pin bank
424 * and the register base of the pin bank.
426 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
429 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
431 while (pin >= (b->pin_base + b->nr_pins))
437 static struct rockchip_pin_bank *bank_num_to_bank(
438 struct rockchip_pinctrl *info,
441 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
444 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
445 if (b->bank_num == num)
449 return ERR_PTR(-EINVAL);
453 * Pinctrl_ops handling
456 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
458 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
460 return info->ngroups;
463 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
466 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
468 return info->groups[selector].name;
471 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
472 unsigned selector, const unsigned **pins,
475 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
477 if (selector >= info->ngroups)
480 *pins = info->groups[selector].pins;
481 *npins = info->groups[selector].npins;
486 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
487 struct device_node *np,
488 struct pinctrl_map **map, unsigned *num_maps)
490 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
491 const struct rockchip_pin_group *grp;
492 struct pinctrl_map *new_map;
493 struct device_node *parent;
498 * first find the group of this node and check if we need to create
499 * config maps for pins
501 grp = pinctrl_name_to_group(info, np->name);
503 dev_err(info->dev, "unable to find group for node %pOFn\n",
508 map_num += grp->npins;
509 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
518 parent = of_get_parent(np);
520 devm_kfree(pctldev->dev, new_map);
523 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
524 new_map[0].data.mux.function = parent->name;
525 new_map[0].data.mux.group = np->name;
528 /* create config map */
530 for (i = 0; i < grp->npins; i++) {
531 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
532 new_map[i].data.configs.group_or_pin =
533 pin_get_name(pctldev, grp->pins[i]);
534 new_map[i].data.configs.configs = grp->data[i].configs;
535 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
538 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
539 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
544 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
545 struct pinctrl_map *map, unsigned num_maps)
549 static const struct pinctrl_ops rockchip_pctrl_ops = {
550 .get_groups_count = rockchip_get_groups_count,
551 .get_group_name = rockchip_get_group_name,
552 .get_group_pins = rockchip_get_group_pins,
553 .dt_node_to_map = rockchip_dt_node_to_map,
554 .dt_free_map = rockchip_dt_free_map,
561 static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = {
625 static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = {
659 static struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
681 static void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
682 int *reg, u8 *bit, int *mask)
684 struct rockchip_pinctrl *info = bank->drvdata;
685 struct rockchip_pin_ctrl *ctrl = info->ctrl;
686 struct rockchip_mux_recalced_data *data;
689 for (i = 0; i < ctrl->niomux_recalced; i++) {
690 data = &ctrl->iomux_recalced[i];
691 if (data->num == bank->bank_num &&
696 if (i >= ctrl->niomux_recalced)
704 static struct rockchip_mux_route_data px30_mux_route_data[] = {
710 .route_offset = 0x184,
711 .route_val = BIT(16 + 7),
717 .route_offset = 0x184,
718 .route_val = BIT(16 + 7) | BIT(7),
724 .route_offset = 0x184,
725 .route_val = BIT(16 + 8),
731 .route_offset = 0x184,
732 .route_val = BIT(16 + 8) | BIT(8),
738 .route_offset = 0x184,
739 .route_val = BIT(16 + 10),
745 .route_offset = 0x184,
746 .route_val = BIT(16 + 10) | BIT(10),
752 .route_offset = 0x184,
753 .route_val = BIT(16 + 9),
759 .route_offset = 0x184,
760 .route_val = BIT(16 + 9) | BIT(9),
764 static struct rockchip_mux_route_data rk3128_mux_route_data[] = {
770 .route_offset = 0x144,
771 .route_val = BIT(16 + 3) | BIT(16 + 4),
777 .route_offset = 0x144,
778 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3),
784 .route_offset = 0x144,
785 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4),
791 .route_offset = 0x144,
792 .route_val = BIT(16 + 5),
798 .route_offset = 0x144,
799 .route_val = BIT(16 + 5) | BIT(5),
805 .route_offset = 0x144,
806 .route_val = BIT(16 + 6),
812 .route_offset = 0x144,
813 .route_val = BIT(16 + 6) | BIT(6),
817 static struct rockchip_mux_route_data rk3188_mux_route_data[] = {
819 /* non-iomuxed emmc/flash pins on flash-dqs */
823 .route_location = ROCKCHIP_ROUTE_GRF,
824 .route_offset = 0xa0,
825 .route_val = BIT(16 + 11),
827 /* non-iomuxed emmc/flash pins on emmc-clk */
831 .route_location = ROCKCHIP_ROUTE_GRF,
832 .route_offset = 0xa0,
833 .route_val = BIT(16 + 11) | BIT(11),
837 static struct rockchip_mux_route_data rk3228_mux_route_data[] = {
843 .route_offset = 0x50,
844 .route_val = BIT(16),
850 .route_offset = 0x50,
851 .route_val = BIT(16) | BIT(0),
857 .route_offset = 0x50,
858 .route_val = BIT(16 + 1),
864 .route_offset = 0x50,
865 .route_val = BIT(16 + 1) | BIT(1),
871 .route_offset = 0x50,
872 .route_val = BIT(16 + 2),
878 .route_offset = 0x50,
879 .route_val = BIT(16 + 2) | BIT(2),
885 .route_offset = 0x50,
886 .route_val = BIT(16 + 3),
892 .route_offset = 0x50,
893 .route_val = BIT(16 + 3) | BIT(3),
899 .route_offset = 0x50,
900 .route_val = BIT(16 + 4),
906 .route_offset = 0x50,
907 .route_val = BIT(16 + 4) | BIT(4),
913 .route_offset = 0x50,
914 .route_val = BIT(16 + 5),
920 .route_offset = 0x50,
921 .route_val = BIT(16 + 5) | BIT(5),
927 .route_offset = 0x50,
928 .route_val = BIT(16 + 7),
934 .route_offset = 0x50,
935 .route_val = BIT(16 + 7) | BIT(7),
941 .route_offset = 0x50,
942 .route_val = BIT(16 + 8),
948 .route_offset = 0x50,
949 .route_val = BIT(16 + 8) | BIT(8),
955 .route_offset = 0x50,
956 .route_val = BIT(16 + 11),
962 .route_offset = 0x50,
963 .route_val = BIT(16 + 11) | BIT(11),
967 static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
969 /* edphdmi_cecinoutt1 */
973 .route_offset = 0x264,
974 .route_val = BIT(16 + 12) | BIT(12),
976 /* edphdmi_cecinout */
980 .route_offset = 0x264,
981 .route_val = BIT(16 + 12),
985 static struct rockchip_mux_route_data rk3328_mux_route_data[] = {
991 .route_offset = 0x50,
992 .route_val = BIT(16) | BIT(16 + 1),
998 .route_offset = 0x50,
999 .route_val = BIT(16) | BIT(16 + 1) | BIT(0),
1005 .route_offset = 0x50,
1006 .route_val = BIT(16 + 2) | BIT(2),
1008 /* gmac-m1-optimized_rxd3 */
1012 .route_offset = 0x50,
1013 .route_val = BIT(16 + 10) | BIT(10),
1019 .route_offset = 0x50,
1020 .route_val = BIT(16 + 3),
1026 .route_offset = 0x50,
1027 .route_val = BIT(16 + 3) | BIT(3),
1033 .route_offset = 0x50,
1034 .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5),
1040 .route_offset = 0x50,
1041 .route_val = BIT(16 + 6),
1047 .route_offset = 0x50,
1048 .route_val = BIT(16 + 6) | BIT(6),
1054 .route_offset = 0x50,
1055 .route_val = BIT(16 + 7) | BIT(7),
1061 .route_offset = 0x50,
1062 .route_val = BIT(16 + 8) | BIT(8),
1068 .route_offset = 0x50,
1069 .route_val = BIT(16 + 9) | BIT(9),
1073 static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
1079 .route_offset = 0xe21c,
1080 .route_val = BIT(16 + 10) | BIT(16 + 11),
1086 .route_offset = 0xe21c,
1087 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
1093 .route_offset = 0xe21c,
1094 .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
1100 .route_offset = 0xe21c,
1101 .route_val = BIT(16 + 14),
1107 .route_offset = 0xe21c,
1108 .route_val = BIT(16 + 14) | BIT(14),
1112 static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
1113 int mux, u32 *loc, u32 *reg, u32 *value)
1115 struct rockchip_pinctrl *info = bank->drvdata;
1116 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1117 struct rockchip_mux_route_data *data;
1120 for (i = 0; i < ctrl->niomux_routes; i++) {
1121 data = &ctrl->iomux_routes[i];
1122 if ((data->bank_num == bank->bank_num) &&
1123 (data->pin == pin) && (data->func == mux))
1127 if (i >= ctrl->niomux_routes)
1130 *loc = data->route_location;
1131 *reg = data->route_offset;
1132 *value = data->route_val;
1137 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
1139 struct rockchip_pinctrl *info = bank->drvdata;
1140 int iomux_num = (pin / 8);
1141 struct regmap *regmap;
1143 int reg, ret, mask, mux_type;
1149 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1150 dev_err(info->dev, "pin %d is unrouted\n", pin);
1154 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1155 return RK_FUNC_GPIO;
1157 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1158 ? info->regmap_pmu : info->regmap_base;
1160 /* get basic quadrupel of mux registers and the correct reg inside */
1161 mux_type = bank->iomux[iomux_num].type;
1162 reg = bank->iomux[iomux_num].offset;
1163 if (mux_type & IOMUX_WIDTH_4BIT) {
1166 bit = (pin % 4) * 4;
1168 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1171 bit = (pin % 8 % 5) * 3;
1174 bit = (pin % 8) * 2;
1178 if (bank->recalced_mask & BIT(pin))
1179 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1181 ret = regmap_read(regmap, reg, &val);
1185 return ((val >> bit) & mask);
1188 static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
1191 struct rockchip_pinctrl *info = bank->drvdata;
1192 int iomux_num = (pin / 8);
1197 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
1198 dev_err(info->dev, "pin %d is unrouted\n", pin);
1202 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
1203 if (mux != RK_FUNC_GPIO) {
1205 "pin %d only supports a gpio mux\n", pin);
1214 * Set a new mux function for a pin.
1216 * The register is divided into the upper and lower 16 bit. When changing
1217 * a value, the previous register value is not read and changed. Instead
1218 * it seems the changed bits are marked in the upper 16 bit, while the
1219 * changed value gets set in the same offset in the lower 16 bit.
1220 * All pin settings seem to be 2 bit wide in both the upper and lower
1222 * @bank: pin bank to change
1223 * @pin: pin to change
1224 * @mux: new mux function to set
1226 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
1228 struct rockchip_pinctrl *info = bank->drvdata;
1229 int iomux_num = (pin / 8);
1230 struct regmap *regmap;
1231 int reg, ret, mask, mux_type;
1233 u32 data, rmask, route_location, route_reg, route_val;
1235 ret = rockchip_verify_mux(bank, pin, mux);
1239 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
1242 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
1243 bank->bank_num, pin, mux);
1245 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
1246 ? info->regmap_pmu : info->regmap_base;
1248 /* get basic quadrupel of mux registers and the correct reg inside */
1249 mux_type = bank->iomux[iomux_num].type;
1250 reg = bank->iomux[iomux_num].offset;
1251 if (mux_type & IOMUX_WIDTH_4BIT) {
1254 bit = (pin % 4) * 4;
1256 } else if (mux_type & IOMUX_WIDTH_3BIT) {
1259 bit = (pin % 8 % 5) * 3;
1262 bit = (pin % 8) * 2;
1266 if (bank->recalced_mask & BIT(pin))
1267 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
1269 if (bank->route_mask & BIT(pin)) {
1270 if (rockchip_get_mux_route(bank, pin, mux, &route_location,
1271 &route_reg, &route_val)) {
1272 struct regmap *route_regmap = regmap;
1274 /* handle special locations */
1275 switch (route_location) {
1276 case ROCKCHIP_ROUTE_PMU:
1277 route_regmap = info->regmap_pmu;
1279 case ROCKCHIP_ROUTE_GRF:
1280 route_regmap = info->regmap_base;
1284 ret = regmap_write(route_regmap, route_reg, route_val);
1290 data = (mask << (bit + 16));
1291 rmask = data | (data >> 16);
1292 data |= (mux & mask) << bit;
1293 ret = regmap_update_bits(regmap, reg, rmask, data);
1298 #define PX30_PULL_PMU_OFFSET 0x10
1299 #define PX30_PULL_GRF_OFFSET 0x60
1300 #define PX30_PULL_BITS_PER_PIN 2
1301 #define PX30_PULL_PINS_PER_REG 8
1302 #define PX30_PULL_BANK_STRIDE 16
1304 static void px30_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1305 int pin_num, struct regmap **regmap,
1308 struct rockchip_pinctrl *info = bank->drvdata;
1310 /* The first 32 pins of the first bank are located in PMU */
1311 if (bank->bank_num == 0) {
1312 *regmap = info->regmap_pmu;
1313 *reg = PX30_PULL_PMU_OFFSET;
1315 *regmap = info->regmap_base;
1316 *reg = PX30_PULL_GRF_OFFSET;
1318 /* correct the offset, as we're starting with the 2nd bank */
1320 *reg += bank->bank_num * PX30_PULL_BANK_STRIDE;
1323 *reg += ((pin_num / PX30_PULL_PINS_PER_REG) * 4);
1324 *bit = (pin_num % PX30_PULL_PINS_PER_REG);
1325 *bit *= PX30_PULL_BITS_PER_PIN;
1328 #define PX30_DRV_PMU_OFFSET 0x20
1329 #define PX30_DRV_GRF_OFFSET 0xf0
1330 #define PX30_DRV_BITS_PER_PIN 2
1331 #define PX30_DRV_PINS_PER_REG 8
1332 #define PX30_DRV_BANK_STRIDE 16
1334 static void px30_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1335 int pin_num, struct regmap **regmap,
1338 struct rockchip_pinctrl *info = bank->drvdata;
1340 /* The first 32 pins of the first bank are located in PMU */
1341 if (bank->bank_num == 0) {
1342 *regmap = info->regmap_pmu;
1343 *reg = PX30_DRV_PMU_OFFSET;
1345 *regmap = info->regmap_base;
1346 *reg = PX30_DRV_GRF_OFFSET;
1348 /* correct the offset, as we're starting with the 2nd bank */
1350 *reg += bank->bank_num * PX30_DRV_BANK_STRIDE;
1353 *reg += ((pin_num / PX30_DRV_PINS_PER_REG) * 4);
1354 *bit = (pin_num % PX30_DRV_PINS_PER_REG);
1355 *bit *= PX30_DRV_BITS_PER_PIN;
1358 #define PX30_SCHMITT_PMU_OFFSET 0x38
1359 #define PX30_SCHMITT_GRF_OFFSET 0xc0
1360 #define PX30_SCHMITT_PINS_PER_PMU_REG 16
1361 #define PX30_SCHMITT_BANK_STRIDE 16
1362 #define PX30_SCHMITT_PINS_PER_GRF_REG 8
1364 static int px30_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1366 struct regmap **regmap,
1369 struct rockchip_pinctrl *info = bank->drvdata;
1372 if (bank->bank_num == 0) {
1373 *regmap = info->regmap_pmu;
1374 *reg = PX30_SCHMITT_PMU_OFFSET;
1375 pins_per_reg = PX30_SCHMITT_PINS_PER_PMU_REG;
1377 *regmap = info->regmap_base;
1378 *reg = PX30_SCHMITT_GRF_OFFSET;
1379 pins_per_reg = PX30_SCHMITT_PINS_PER_GRF_REG;
1380 *reg += (bank->bank_num - 1) * PX30_SCHMITT_BANK_STRIDE;
1383 *reg += ((pin_num / pins_per_reg) * 4);
1384 *bit = pin_num % pins_per_reg;
1389 #define RV1108_PULL_PMU_OFFSET 0x10
1390 #define RV1108_PULL_OFFSET 0x110
1391 #define RV1108_PULL_PINS_PER_REG 8
1392 #define RV1108_PULL_BITS_PER_PIN 2
1393 #define RV1108_PULL_BANK_STRIDE 16
1395 static void rv1108_calc_pull_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_PULL_PMU_OFFSET;
1406 *reg = RV1108_PULL_OFFSET;
1407 *regmap = info->regmap_base;
1408 /* correct the offset, as we're starting with the 2nd bank */
1410 *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
1413 *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
1414 *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
1415 *bit *= RV1108_PULL_BITS_PER_PIN;
1418 #define RV1108_DRV_PMU_OFFSET 0x20
1419 #define RV1108_DRV_GRF_OFFSET 0x210
1420 #define RV1108_DRV_BITS_PER_PIN 2
1421 #define RV1108_DRV_PINS_PER_REG 8
1422 #define RV1108_DRV_BANK_STRIDE 16
1424 static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1425 int pin_num, struct regmap **regmap,
1428 struct rockchip_pinctrl *info = bank->drvdata;
1430 /* The first 24 pins of the first bank are located in PMU */
1431 if (bank->bank_num == 0) {
1432 *regmap = info->regmap_pmu;
1433 *reg = RV1108_DRV_PMU_OFFSET;
1435 *regmap = info->regmap_base;
1436 *reg = RV1108_DRV_GRF_OFFSET;
1438 /* correct the offset, as we're starting with the 2nd bank */
1440 *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
1443 *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
1444 *bit = pin_num % RV1108_DRV_PINS_PER_REG;
1445 *bit *= RV1108_DRV_BITS_PER_PIN;
1448 #define RV1108_SCHMITT_PMU_OFFSET 0x30
1449 #define RV1108_SCHMITT_GRF_OFFSET 0x388
1450 #define RV1108_SCHMITT_BANK_STRIDE 8
1451 #define RV1108_SCHMITT_PINS_PER_GRF_REG 16
1452 #define RV1108_SCHMITT_PINS_PER_PMU_REG 8
1454 static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
1456 struct regmap **regmap,
1459 struct rockchip_pinctrl *info = bank->drvdata;
1462 if (bank->bank_num == 0) {
1463 *regmap = info->regmap_pmu;
1464 *reg = RV1108_SCHMITT_PMU_OFFSET;
1465 pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG;
1467 *regmap = info->regmap_base;
1468 *reg = RV1108_SCHMITT_GRF_OFFSET;
1469 pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG;
1470 *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE;
1472 *reg += ((pin_num / pins_per_reg) * 4);
1473 *bit = pin_num % pins_per_reg;
1478 #define RK2928_PULL_OFFSET 0x118
1479 #define RK2928_PULL_PINS_PER_REG 16
1480 #define RK2928_PULL_BANK_STRIDE 8
1482 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1483 int pin_num, struct regmap **regmap,
1486 struct rockchip_pinctrl *info = bank->drvdata;
1488 *regmap = info->regmap_base;
1489 *reg = RK2928_PULL_OFFSET;
1490 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1491 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
1493 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1496 #define RK3128_PULL_OFFSET 0x118
1498 static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1499 int pin_num, struct regmap **regmap,
1502 struct rockchip_pinctrl *info = bank->drvdata;
1504 *regmap = info->regmap_base;
1505 *reg = RK3128_PULL_OFFSET;
1506 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
1507 *reg += ((pin_num / RK2928_PULL_PINS_PER_REG) * 4);
1509 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
1512 #define RK3188_PULL_OFFSET 0x164
1513 #define RK3188_PULL_BITS_PER_PIN 2
1514 #define RK3188_PULL_PINS_PER_REG 8
1515 #define RK3188_PULL_BANK_STRIDE 16
1516 #define RK3188_PULL_PMU_OFFSET 0x64
1518 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1519 int pin_num, struct regmap **regmap,
1522 struct rockchip_pinctrl *info = bank->drvdata;
1524 /* The first 12 pins of the first bank are located elsewhere */
1525 if (bank->bank_num == 0 && pin_num < 12) {
1526 *regmap = info->regmap_pmu ? info->regmap_pmu
1527 : bank->regmap_pull;
1528 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
1529 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1530 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1531 *bit *= RK3188_PULL_BITS_PER_PIN;
1533 *regmap = info->regmap_pull ? info->regmap_pull
1534 : info->regmap_base;
1535 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
1537 /* correct the offset, as it is the 2nd pull register */
1539 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1540 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1543 * The bits in these registers have an inverse ordering
1544 * with the lowest pin being in bits 15:14 and the highest
1547 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
1548 *bit *= RK3188_PULL_BITS_PER_PIN;
1552 #define RK3288_PULL_OFFSET 0x140
1553 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1554 int pin_num, struct regmap **regmap,
1557 struct rockchip_pinctrl *info = bank->drvdata;
1559 /* The first 24 pins of the first bank are located in PMU */
1560 if (bank->bank_num == 0) {
1561 *regmap = info->regmap_pmu;
1562 *reg = RK3188_PULL_PMU_OFFSET;
1564 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1565 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1566 *bit *= RK3188_PULL_BITS_PER_PIN;
1568 *regmap = info->regmap_base;
1569 *reg = RK3288_PULL_OFFSET;
1571 /* correct the offset, as we're starting with the 2nd bank */
1573 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1574 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1576 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1577 *bit *= RK3188_PULL_BITS_PER_PIN;
1581 #define RK3288_DRV_PMU_OFFSET 0x70
1582 #define RK3288_DRV_GRF_OFFSET 0x1c0
1583 #define RK3288_DRV_BITS_PER_PIN 2
1584 #define RK3288_DRV_PINS_PER_REG 8
1585 #define RK3288_DRV_BANK_STRIDE 16
1587 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1588 int pin_num, struct regmap **regmap,
1591 struct rockchip_pinctrl *info = bank->drvdata;
1593 /* The first 24 pins of the first bank are located in PMU */
1594 if (bank->bank_num == 0) {
1595 *regmap = info->regmap_pmu;
1596 *reg = RK3288_DRV_PMU_OFFSET;
1598 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1599 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1600 *bit *= RK3288_DRV_BITS_PER_PIN;
1602 *regmap = info->regmap_base;
1603 *reg = RK3288_DRV_GRF_OFFSET;
1605 /* correct the offset, as we're starting with the 2nd bank */
1607 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1608 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1610 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1611 *bit *= RK3288_DRV_BITS_PER_PIN;
1615 #define RK3228_PULL_OFFSET 0x100
1617 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1618 int pin_num, struct regmap **regmap,
1621 struct rockchip_pinctrl *info = bank->drvdata;
1623 *regmap = info->regmap_base;
1624 *reg = RK3228_PULL_OFFSET;
1625 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1626 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1628 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1629 *bit *= RK3188_PULL_BITS_PER_PIN;
1632 #define RK3228_DRV_GRF_OFFSET 0x200
1634 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1635 int pin_num, struct regmap **regmap,
1638 struct rockchip_pinctrl *info = bank->drvdata;
1640 *regmap = info->regmap_base;
1641 *reg = RK3228_DRV_GRF_OFFSET;
1642 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1643 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1645 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1646 *bit *= RK3288_DRV_BITS_PER_PIN;
1649 #define RK3368_PULL_GRF_OFFSET 0x100
1650 #define RK3368_PULL_PMU_OFFSET 0x10
1652 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1653 int pin_num, struct regmap **regmap,
1656 struct rockchip_pinctrl *info = bank->drvdata;
1658 /* The first 32 pins of the first bank are located in PMU */
1659 if (bank->bank_num == 0) {
1660 *regmap = info->regmap_pmu;
1661 *reg = RK3368_PULL_PMU_OFFSET;
1663 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1664 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1665 *bit *= RK3188_PULL_BITS_PER_PIN;
1667 *regmap = info->regmap_base;
1668 *reg = RK3368_PULL_GRF_OFFSET;
1670 /* correct the offset, as we're starting with the 2nd bank */
1672 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1673 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1675 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1676 *bit *= RK3188_PULL_BITS_PER_PIN;
1680 #define RK3368_DRV_PMU_OFFSET 0x20
1681 #define RK3368_DRV_GRF_OFFSET 0x200
1683 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1684 int pin_num, struct regmap **regmap,
1687 struct rockchip_pinctrl *info = bank->drvdata;
1689 /* The first 32 pins of the first bank are located in PMU */
1690 if (bank->bank_num == 0) {
1691 *regmap = info->regmap_pmu;
1692 *reg = RK3368_DRV_PMU_OFFSET;
1694 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1695 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
1696 *bit *= RK3288_DRV_BITS_PER_PIN;
1698 *regmap = info->regmap_base;
1699 *reg = RK3368_DRV_GRF_OFFSET;
1701 /* correct the offset, as we're starting with the 2nd bank */
1703 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
1704 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
1706 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
1707 *bit *= RK3288_DRV_BITS_PER_PIN;
1711 #define RK3399_PULL_GRF_OFFSET 0xe040
1712 #define RK3399_PULL_PMU_OFFSET 0x40
1713 #define RK3399_DRV_3BITS_PER_PIN 3
1715 static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
1716 int pin_num, struct regmap **regmap,
1719 struct rockchip_pinctrl *info = bank->drvdata;
1721 /* The bank0:16 and bank1:32 pins are located in PMU */
1722 if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
1723 *regmap = info->regmap_pmu;
1724 *reg = RK3399_PULL_PMU_OFFSET;
1726 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1728 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1729 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
1730 *bit *= RK3188_PULL_BITS_PER_PIN;
1732 *regmap = info->regmap_base;
1733 *reg = RK3399_PULL_GRF_OFFSET;
1735 /* correct the offset, as we're starting with the 3rd bank */
1737 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
1738 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
1740 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
1741 *bit *= RK3188_PULL_BITS_PER_PIN;
1745 static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
1746 int pin_num, struct regmap **regmap,
1749 struct rockchip_pinctrl *info = bank->drvdata;
1750 int drv_num = (pin_num / 8);
1752 /* The bank0:16 and bank1:32 pins are located in PMU */
1753 if ((bank->bank_num == 0) || (bank->bank_num == 1))
1754 *regmap = info->regmap_pmu;
1756 *regmap = info->regmap_base;
1758 *reg = bank->drv[drv_num].offset;
1759 if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
1760 (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
1761 *bit = (pin_num % 8) * 3;
1763 *bit = (pin_num % 8) * 2;
1766 static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
1767 { 2, 4, 8, 12, -1, -1, -1, -1 },
1768 { 3, 6, 9, 12, -1, -1, -1, -1 },
1769 { 5, 10, 15, 20, -1, -1, -1, -1 },
1770 { 4, 6, 8, 10, 12, 14, 16, 18 },
1771 { 4, 7, 10, 13, 16, 19, 22, 26 }
1774 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
1777 struct rockchip_pinctrl *info = bank->drvdata;
1778 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1779 struct regmap *regmap;
1781 u32 data, temp, rmask_bits;
1783 int drv_type = bank->drv[pin_num / 8].drv_type;
1785 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1788 case DRV_TYPE_IO_1V8_3V0_AUTO:
1789 case DRV_TYPE_IO_3V3_ONLY:
1790 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1793 /* regular case, nothing to do */
1797 * drive-strength offset is special, as it is
1798 * spread over 2 registers
1800 ret = regmap_read(regmap, reg, &data);
1804 ret = regmap_read(regmap, reg + 0x4, &temp);
1809 * the bit data[15] contains bit 0 of the value
1810 * while temp[1:0] contains bits 2 and 1
1817 return rockchip_perpin_drv_list[drv_type][data];
1819 /* setting fully enclosed in the second register */
1824 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1830 case DRV_TYPE_IO_DEFAULT:
1831 case DRV_TYPE_IO_1V8_OR_3V0:
1832 case DRV_TYPE_IO_1V8_ONLY:
1833 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1836 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1841 ret = regmap_read(regmap, reg, &data);
1846 data &= (1 << rmask_bits) - 1;
1848 return rockchip_perpin_drv_list[drv_type][data];
1851 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
1852 int pin_num, int strength)
1854 struct rockchip_pinctrl *info = bank->drvdata;
1855 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1856 struct regmap *regmap;
1858 u32 data, rmask, rmask_bits, temp;
1860 int drv_type = bank->drv[pin_num / 8].drv_type;
1862 dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
1863 bank->bank_num, pin_num, strength);
1865 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
1868 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
1869 if (rockchip_perpin_drv_list[drv_type][i] == strength) {
1872 } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
1873 ret = rockchip_perpin_drv_list[drv_type][i];
1879 dev_err(info->dev, "unsupported driver strength %d\n",
1885 case DRV_TYPE_IO_1V8_3V0_AUTO:
1886 case DRV_TYPE_IO_3V3_ONLY:
1887 rmask_bits = RK3399_DRV_3BITS_PER_PIN;
1890 /* regular case, nothing to do */
1894 * drive-strength offset is special, as it is spread
1895 * over 2 registers, the bit data[15] contains bit 0
1896 * of the value while temp[1:0] contains bits 2 and 1
1898 data = (ret & 0x1) << 15;
1899 temp = (ret >> 0x1) & 0x3;
1901 rmask = BIT(15) | BIT(31);
1903 ret = regmap_update_bits(regmap, reg, rmask, data);
1907 rmask = 0x3 | (0x3 << 16);
1908 temp |= (0x3 << 16);
1910 ret = regmap_update_bits(regmap, reg, rmask, temp);
1914 /* setting fully enclosed in the second register */
1919 dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
1924 case DRV_TYPE_IO_DEFAULT:
1925 case DRV_TYPE_IO_1V8_OR_3V0:
1926 case DRV_TYPE_IO_1V8_ONLY:
1927 rmask_bits = RK3288_DRV_BITS_PER_PIN;
1930 dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
1935 /* enable the write to the equivalent lower bits */
1936 data = ((1 << rmask_bits) - 1) << (bit + 16);
1937 rmask = data | (data >> 16);
1938 data |= (ret << bit);
1940 ret = regmap_update_bits(regmap, reg, rmask, data);
1945 static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
1947 PIN_CONFIG_BIAS_DISABLE,
1948 PIN_CONFIG_BIAS_PULL_UP,
1949 PIN_CONFIG_BIAS_PULL_DOWN,
1950 PIN_CONFIG_BIAS_BUS_HOLD
1953 PIN_CONFIG_BIAS_DISABLE,
1954 PIN_CONFIG_BIAS_PULL_DOWN,
1955 PIN_CONFIG_BIAS_DISABLE,
1956 PIN_CONFIG_BIAS_PULL_UP
1960 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
1962 struct rockchip_pinctrl *info = bank->drvdata;
1963 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1964 struct regmap *regmap;
1965 int reg, ret, pull_type;
1969 /* rk3066b does support any pulls */
1970 if (ctrl->type == RK3066B)
1971 return PIN_CONFIG_BIAS_DISABLE;
1973 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
1975 ret = regmap_read(regmap, reg, &data);
1979 switch (ctrl->type) {
1982 return !(data & BIT(bit))
1983 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
1984 : PIN_CONFIG_BIAS_DISABLE;
1991 pull_type = bank->pull_type[pin_num / 8];
1993 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
1995 return rockchip_pull_list[pull_type][data];
1997 dev_err(info->dev, "unsupported pinctrl type\n");
2002 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
2003 int pin_num, int pull)
2005 struct rockchip_pinctrl *info = bank->drvdata;
2006 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2007 struct regmap *regmap;
2008 int reg, ret, i, pull_type;
2012 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
2013 bank->bank_num, pin_num, pull);
2015 /* rk3066b does support any pulls */
2016 if (ctrl->type == RK3066B)
2017 return pull ? -EINVAL : 0;
2019 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
2021 switch (ctrl->type) {
2024 data = BIT(bit + 16);
2025 if (pull == PIN_CONFIG_BIAS_DISABLE)
2027 ret = regmap_write(regmap, reg, data);
2035 pull_type = bank->pull_type[pin_num / 8];
2037 for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
2039 if (rockchip_pull_list[pull_type][i] == pull) {
2046 dev_err(info->dev, "unsupported pull setting %d\n",
2051 /* enable the write to the equivalent lower bits */
2052 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
2053 rmask = data | (data >> 16);
2054 data |= (ret << bit);
2056 ret = regmap_update_bits(regmap, reg, rmask, data);
2059 dev_err(info->dev, "unsupported pinctrl type\n");
2066 #define RK3328_SCHMITT_BITS_PER_PIN 1
2067 #define RK3328_SCHMITT_PINS_PER_REG 16
2068 #define RK3328_SCHMITT_BANK_STRIDE 8
2069 #define RK3328_SCHMITT_GRF_OFFSET 0x380
2071 static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
2073 struct regmap **regmap,
2076 struct rockchip_pinctrl *info = bank->drvdata;
2078 *regmap = info->regmap_base;
2079 *reg = RK3328_SCHMITT_GRF_OFFSET;
2081 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
2082 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
2083 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
2088 static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
2090 struct rockchip_pinctrl *info = bank->drvdata;
2091 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2092 struct regmap *regmap;
2097 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2101 ret = regmap_read(regmap, reg, &data);
2109 static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
2110 int pin_num, int enable)
2112 struct rockchip_pinctrl *info = bank->drvdata;
2113 struct rockchip_pin_ctrl *ctrl = info->ctrl;
2114 struct regmap *regmap;
2119 dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
2120 bank->bank_num, pin_num, enable);
2122 ret = ctrl->schmitt_calc_reg(bank, pin_num, ®map, ®, &bit);
2126 /* enable the write to the equivalent lower bits */
2127 data = BIT(bit + 16) | (enable << bit);
2128 rmask = BIT(bit + 16) | BIT(bit);
2130 return regmap_update_bits(regmap, reg, rmask, data);
2134 * Pinmux_ops handling
2137 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
2139 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2141 return info->nfunctions;
2144 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
2147 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2149 return info->functions[selector].name;
2152 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
2153 unsigned selector, const char * const **groups,
2154 unsigned * const num_groups)
2156 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2158 *groups = info->functions[selector].groups;
2159 *num_groups = info->functions[selector].ngroups;
2164 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
2167 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2168 const unsigned int *pins = info->groups[group].pins;
2169 const struct rockchip_pin_config *data = info->groups[group].data;
2170 struct rockchip_pin_bank *bank;
2173 dev_dbg(info->dev, "enable function %s group %s\n",
2174 info->functions[selector].name, info->groups[group].name);
2177 * for each pin in the pin group selected, program the corresponding
2178 * pin function number in the config register.
2180 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
2181 bank = pin_to_bank(info, pins[cnt]);
2182 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
2189 /* revert the already done pin settings */
2190 for (cnt--; cnt >= 0; cnt--)
2191 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
2199 static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
2201 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
2205 ret = clk_enable(bank->clk);
2207 dev_err(bank->drvdata->dev,
2208 "failed to enable clock for bank %s\n", bank->name);
2211 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2212 clk_disable(bank->clk);
2214 return !(data & BIT(offset));
2218 * The calls to gpio_direction_output() and gpio_direction_input()
2219 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
2220 * function called from the gpiolib interface).
2222 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
2223 int pin, bool input)
2225 struct rockchip_pin_bank *bank;
2227 unsigned long flags;
2230 bank = gpiochip_get_data(chip);
2232 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
2236 clk_enable(bank->clk);
2237 raw_spin_lock_irqsave(&bank->slock, flags);
2239 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2240 /* set bit to 1 for output, 0 for input */
2245 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2247 raw_spin_unlock_irqrestore(&bank->slock, flags);
2248 clk_disable(bank->clk);
2253 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
2254 struct pinctrl_gpio_range *range,
2255 unsigned offset, bool input)
2257 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2258 struct gpio_chip *chip;
2262 pin = offset - chip->base;
2263 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
2264 offset, range->name, pin, input ? "input" : "output");
2266 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
2270 static const struct pinmux_ops rockchip_pmx_ops = {
2271 .get_functions_count = rockchip_pmx_get_funcs_count,
2272 .get_function_name = rockchip_pmx_get_func_name,
2273 .get_function_groups = rockchip_pmx_get_groups,
2274 .set_mux = rockchip_pmx_set,
2275 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
2279 * Pinconf_ops handling
2282 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
2283 enum pin_config_param pull)
2285 switch (ctrl->type) {
2288 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
2289 pull == PIN_CONFIG_BIAS_DISABLE);
2291 return pull ? false : true;
2298 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
2304 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
2305 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
2307 /* set the pin config settings for a specified pin */
2308 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
2309 unsigned long *configs, unsigned num_configs)
2311 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2312 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2313 enum pin_config_param param;
2318 for (i = 0; i < num_configs; i++) {
2319 param = pinconf_to_config_param(configs[i]);
2320 arg = pinconf_to_config_argument(configs[i]);
2323 case PIN_CONFIG_BIAS_DISABLE:
2324 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2329 case PIN_CONFIG_BIAS_PULL_UP:
2330 case PIN_CONFIG_BIAS_PULL_DOWN:
2331 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2332 case PIN_CONFIG_BIAS_BUS_HOLD:
2333 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2339 rc = rockchip_set_pull(bank, pin - bank->pin_base,
2344 case PIN_CONFIG_OUTPUT:
2345 rockchip_gpio_set(&bank->gpio_chip,
2346 pin - bank->pin_base, arg);
2347 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
2348 pin - bank->pin_base, false);
2352 case PIN_CONFIG_DRIVE_STRENGTH:
2353 /* rk3288 is the first with per-pin drive-strength */
2354 if (!info->ctrl->drv_calc_reg)
2357 rc = rockchip_set_drive_perpin(bank,
2358 pin - bank->pin_base, arg);
2362 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2363 if (!info->ctrl->schmitt_calc_reg)
2366 rc = rockchip_set_schmitt(bank,
2367 pin - bank->pin_base, arg);
2375 } /* for each config */
2380 /* get the pin config settings for a specified pin */
2381 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
2382 unsigned long *config)
2384 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
2385 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
2386 enum pin_config_param param = pinconf_to_config_param(*config);
2391 case PIN_CONFIG_BIAS_DISABLE:
2392 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2397 case PIN_CONFIG_BIAS_PULL_UP:
2398 case PIN_CONFIG_BIAS_PULL_DOWN:
2399 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
2400 case PIN_CONFIG_BIAS_BUS_HOLD:
2401 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
2404 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
2409 case PIN_CONFIG_OUTPUT:
2410 rc = rockchip_get_mux(bank, pin - bank->pin_base);
2411 if (rc != RK_FUNC_GPIO)
2414 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
2420 case PIN_CONFIG_DRIVE_STRENGTH:
2421 /* rk3288 is the first with per-pin drive-strength */
2422 if (!info->ctrl->drv_calc_reg)
2425 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
2431 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
2432 if (!info->ctrl->schmitt_calc_reg)
2435 rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
2446 *config = pinconf_to_config_packed(param, arg);
2451 static const struct pinconf_ops rockchip_pinconf_ops = {
2452 .pin_config_get = rockchip_pinconf_get,
2453 .pin_config_set = rockchip_pinconf_set,
2457 static const struct of_device_id rockchip_bank_match[] = {
2458 { .compatible = "rockchip,gpio-bank" },
2459 { .compatible = "rockchip,rk3188-gpio-bank0" },
2463 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
2464 struct device_node *np)
2466 struct device_node *child;
2468 for_each_child_of_node(np, child) {
2469 if (of_match_node(rockchip_bank_match, child))
2473 info->ngroups += of_get_child_count(child);
2477 static int rockchip_pinctrl_parse_groups(struct device_node *np,
2478 struct rockchip_pin_group *grp,
2479 struct rockchip_pinctrl *info,
2482 struct rockchip_pin_bank *bank;
2489 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
2491 /* Initialise group */
2492 grp->name = np->name;
2495 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
2496 * do sanity check and calculate pins number
2498 list = of_get_property(np, "rockchip,pins", &size);
2499 /* we do not check return since it's safe node passed down */
2500 size /= sizeof(*list);
2501 if (!size || size % 4) {
2502 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
2506 grp->npins = size / 4;
2508 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
2510 grp->data = devm_kcalloc(info->dev,
2512 sizeof(struct rockchip_pin_config),
2514 if (!grp->pins || !grp->data)
2517 for (i = 0, j = 0; i < size; i += 4, j++) {
2518 const __be32 *phandle;
2519 struct device_node *np_config;
2521 num = be32_to_cpu(*list++);
2522 bank = bank_num_to_bank(info, num);
2524 return PTR_ERR(bank);
2526 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
2527 grp->data[j].func = be32_to_cpu(*list++);
2533 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
2534 ret = pinconf_generic_parse_dt_config(np_config, NULL,
2535 &grp->data[j].configs, &grp->data[j].nconfigs);
2543 static int rockchip_pinctrl_parse_functions(struct device_node *np,
2544 struct rockchip_pinctrl *info,
2547 struct device_node *child;
2548 struct rockchip_pmx_func *func;
2549 struct rockchip_pin_group *grp;
2551 static u32 grp_index;
2554 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
2556 func = &info->functions[index];
2558 /* Initialise function */
2559 func->name = np->name;
2560 func->ngroups = of_get_child_count(np);
2561 if (func->ngroups <= 0)
2564 func->groups = devm_kcalloc(info->dev,
2565 func->ngroups, sizeof(char *), GFP_KERNEL);
2569 for_each_child_of_node(np, child) {
2570 func->groups[i] = child->name;
2571 grp = &info->groups[grp_index++];
2572 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
2582 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
2583 struct rockchip_pinctrl *info)
2585 struct device *dev = &pdev->dev;
2586 struct device_node *np = dev->of_node;
2587 struct device_node *child;
2591 rockchip_pinctrl_child_count(info, np);
2593 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
2594 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
2596 info->functions = devm_kcalloc(dev,
2598 sizeof(struct rockchip_pmx_func),
2600 if (!info->functions)
2603 info->groups = devm_kcalloc(dev,
2605 sizeof(struct rockchip_pin_group),
2612 for_each_child_of_node(np, child) {
2613 if (of_match_node(rockchip_bank_match, child))
2616 ret = rockchip_pinctrl_parse_functions(child, info, i++);
2618 dev_err(&pdev->dev, "failed to parse function\n");
2627 static int rockchip_pinctrl_register(struct platform_device *pdev,
2628 struct rockchip_pinctrl *info)
2630 struct pinctrl_desc *ctrldesc = &info->pctl;
2631 struct pinctrl_pin_desc *pindesc, *pdesc;
2632 struct rockchip_pin_bank *pin_bank;
2636 ctrldesc->name = "rockchip-pinctrl";
2637 ctrldesc->owner = THIS_MODULE;
2638 ctrldesc->pctlops = &rockchip_pctrl_ops;
2639 ctrldesc->pmxops = &rockchip_pmx_ops;
2640 ctrldesc->confops = &rockchip_pinconf_ops;
2642 pindesc = devm_kcalloc(&pdev->dev,
2643 info->ctrl->nr_pins, sizeof(*pindesc),
2648 ctrldesc->pins = pindesc;
2649 ctrldesc->npins = info->ctrl->nr_pins;
2652 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
2653 pin_bank = &info->ctrl->pin_banks[bank];
2654 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
2656 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
2657 pin_bank->name, pin);
2662 ret = rockchip_pinctrl_parse_dt(pdev, info);
2666 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
2667 if (IS_ERR(info->pctl_dev)) {
2668 dev_err(&pdev->dev, "could not register pinctrl driver\n");
2669 return PTR_ERR(info->pctl_dev);
2672 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
2673 pin_bank = &info->ctrl->pin_banks[bank];
2674 pin_bank->grange.name = pin_bank->name;
2675 pin_bank->grange.id = bank;
2676 pin_bank->grange.pin_base = pin_bank->pin_base;
2677 pin_bank->grange.base = pin_bank->gpio_chip.base;
2678 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
2679 pin_bank->grange.gc = &pin_bank->gpio_chip;
2680 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
2690 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
2692 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2693 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
2694 unsigned long flags;
2697 clk_enable(bank->clk);
2698 raw_spin_lock_irqsave(&bank->slock, flags);
2701 data &= ~BIT(offset);
2703 data |= BIT(offset);
2706 raw_spin_unlock_irqrestore(&bank->slock, flags);
2707 clk_disable(bank->clk);
2711 * Returns the level of the pin for input direction and setting of the DR
2712 * register for output gpios.
2714 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
2716 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2719 clk_enable(bank->clk);
2720 data = readl(bank->reg_base + GPIO_EXT_PORT);
2721 clk_disable(bank->clk);
2728 * gpiolib gpio_direction_input callback function. The setting of the pin
2729 * mux function as 'gpio input' will be handled by the pinctrl subsystem
2732 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
2734 return pinctrl_gpio_direction_input(gc->base + offset);
2738 * gpiolib gpio_direction_output callback function. The setting of the pin
2739 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2742 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
2743 unsigned offset, int value)
2745 rockchip_gpio_set(gc, offset, value);
2746 return pinctrl_gpio_direction_output(gc->base + offset);
2749 static void rockchip_gpio_set_debounce(struct gpio_chip *gc,
2750 unsigned int offset, bool enable)
2752 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2753 void __iomem *reg = bank->reg_base + GPIO_DEBOUNCE;
2754 unsigned long flags;
2757 clk_enable(bank->clk);
2758 raw_spin_lock_irqsave(&bank->slock, flags);
2762 data |= BIT(offset);
2764 data &= ~BIT(offset);
2767 raw_spin_unlock_irqrestore(&bank->slock, flags);
2768 clk_disable(bank->clk);
2772 * gpiolib set_config callback function. The setting of the pin
2773 * mux function as 'gpio output' will be handled by the pinctrl subsystem
2776 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
2777 unsigned long config)
2779 enum pin_config_param param = pinconf_to_config_param(config);
2782 case PIN_CONFIG_INPUT_DEBOUNCE:
2783 rockchip_gpio_set_debounce(gc, offset, true);
2785 * Rockchip's gpio could only support up to one period
2786 * of the debounce clock(pclk), which is far away from
2787 * satisftying the requirement, as pclk is usually near
2788 * 100MHz shared by all peripherals. So the fact is it
2789 * has crippled debounce capability could only be useful
2790 * to prevent any spurious glitches from waking up the system
2791 * if the gpio is conguired as wakeup interrupt source. Let's
2792 * still return -ENOTSUPP as before, to make sure the caller
2793 * of gpiod_set_debounce won't change its behaviour.
2802 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
2803 * and a virtual IRQ, if not already present.
2805 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
2807 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
2813 virq = irq_create_mapping(bank->domain, offset);
2815 return (virq) ? : -ENXIO;
2818 static const struct gpio_chip rockchip_gpiolib_chip = {
2819 .request = gpiochip_generic_request,
2820 .free = gpiochip_generic_free,
2821 .set = rockchip_gpio_set,
2822 .get = rockchip_gpio_get,
2823 .get_direction = rockchip_gpio_get_direction,
2824 .direction_input = rockchip_gpio_direction_input,
2825 .direction_output = rockchip_gpio_direction_output,
2826 .set_config = rockchip_gpio_set_config,
2827 .to_irq = rockchip_gpio_to_irq,
2828 .owner = THIS_MODULE,
2832 * Interrupt handling
2835 static void rockchip_irq_demux(struct irq_desc *desc)
2837 struct irq_chip *chip = irq_desc_get_chip(desc);
2838 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
2841 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
2843 chained_irq_enter(chip, desc);
2845 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
2848 unsigned int irq, virq;
2852 virq = irq_linear_revmap(bank->domain, irq);
2855 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
2859 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
2862 * Triggering IRQ on both rising and falling edge
2863 * needs manual intervention.
2865 if (bank->toggle_edge_mode & BIT(irq)) {
2866 u32 data, data_old, polarity;
2867 unsigned long flags;
2869 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
2871 raw_spin_lock_irqsave(&bank->slock, flags);
2873 polarity = readl_relaxed(bank->reg_base +
2875 if (data & BIT(irq))
2876 polarity &= ~BIT(irq);
2878 polarity |= BIT(irq);
2880 bank->reg_base + GPIO_INT_POLARITY);
2882 raw_spin_unlock_irqrestore(&bank->slock, flags);
2885 data = readl_relaxed(bank->reg_base +
2887 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
2890 generic_handle_irq(virq);
2893 chained_irq_exit(chip, desc);
2896 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
2898 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2899 struct rockchip_pin_bank *bank = gc->private;
2900 u32 mask = BIT(d->hwirq);
2904 unsigned long flags;
2907 /* make sure the pin is configured as gpio input */
2908 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
2912 clk_enable(bank->clk);
2913 raw_spin_lock_irqsave(&bank->slock, flags);
2915 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
2917 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
2919 raw_spin_unlock_irqrestore(&bank->slock, flags);
2921 if (type & IRQ_TYPE_EDGE_BOTH)
2922 irq_set_handler_locked(d, handle_edge_irq);
2924 irq_set_handler_locked(d, handle_level_irq);
2926 raw_spin_lock_irqsave(&bank->slock, flags);
2929 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
2930 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
2933 case IRQ_TYPE_EDGE_BOTH:
2934 bank->toggle_edge_mode |= mask;
2938 * Determine gpio state. If 1 next interrupt should be falling
2941 data = readl(bank->reg_base + GPIO_EXT_PORT);
2947 case IRQ_TYPE_EDGE_RISING:
2948 bank->toggle_edge_mode &= ~mask;
2952 case IRQ_TYPE_EDGE_FALLING:
2953 bank->toggle_edge_mode &= ~mask;
2957 case IRQ_TYPE_LEVEL_HIGH:
2958 bank->toggle_edge_mode &= ~mask;
2962 case IRQ_TYPE_LEVEL_LOW:
2963 bank->toggle_edge_mode &= ~mask;
2969 raw_spin_unlock_irqrestore(&bank->slock, flags);
2970 clk_disable(bank->clk);
2974 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
2975 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
2978 raw_spin_unlock_irqrestore(&bank->slock, flags);
2979 clk_disable(bank->clk);
2984 static void rockchip_irq_suspend(struct irq_data *d)
2986 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2987 struct rockchip_pin_bank *bank = gc->private;
2989 clk_enable(bank->clk);
2990 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
2991 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
2992 clk_disable(bank->clk);
2995 static void rockchip_irq_resume(struct irq_data *d)
2997 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
2998 struct rockchip_pin_bank *bank = gc->private;
3000 clk_enable(bank->clk);
3001 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
3002 clk_disable(bank->clk);
3005 static void rockchip_irq_enable(struct irq_data *d)
3007 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
3008 struct rockchip_pin_bank *bank = gc->private;
3010 clk_enable(bank->clk);
3011 irq_gc_mask_clr_bit(d);
3014 static void rockchip_irq_disable(struct irq_data *d)
3016 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
3017 struct rockchip_pin_bank *bank = gc->private;
3019 irq_gc_mask_set_bit(d);
3020 clk_disable(bank->clk);
3023 static int rockchip_interrupts_register(struct platform_device *pdev,
3024 struct rockchip_pinctrl *info)
3026 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3027 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3028 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
3029 struct irq_chip_generic *gc;
3033 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3035 dev_warn(&pdev->dev, "bank %s is not valid\n",
3040 ret = clk_enable(bank->clk);
3042 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
3047 bank->domain = irq_domain_add_linear(bank->of_node, 32,
3048 &irq_generic_chip_ops, NULL);
3049 if (!bank->domain) {
3050 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
3052 clk_disable(bank->clk);
3056 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
3057 "rockchip_gpio_irq", handle_level_irq,
3058 clr, 0, IRQ_GC_INIT_MASK_CACHE);
3060 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
3062 irq_domain_remove(bank->domain);
3063 clk_disable(bank->clk);
3068 * Linux assumes that all interrupts start out disabled/masked.
3069 * Our driver only uses the concept of masked and always keeps
3070 * things enabled, so for us that's all masked and all enabled.
3072 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
3073 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
3075 gc = irq_get_domain_generic_chip(bank->domain, 0);
3076 gc->reg_base = bank->reg_base;
3078 gc->chip_types[0].regs.mask = GPIO_INTMASK;
3079 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
3080 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
3081 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
3082 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
3083 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
3084 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
3085 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
3086 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
3087 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
3088 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
3089 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
3091 irq_set_chained_handler_and_data(bank->irq,
3092 rockchip_irq_demux, bank);
3094 /* map the gpio irqs here, when the clock is still running */
3095 for (j = 0 ; j < 32 ; j++)
3096 irq_create_mapping(bank->domain, j);
3098 clk_disable(bank->clk);
3104 static int rockchip_gpiolib_register(struct platform_device *pdev,
3105 struct rockchip_pinctrl *info)
3107 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3108 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3109 struct gpio_chip *gc;
3113 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3115 dev_warn(&pdev->dev, "bank %s is not valid\n",
3120 bank->gpio_chip = rockchip_gpiolib_chip;
3122 gc = &bank->gpio_chip;
3123 gc->base = bank->pin_base;
3124 gc->ngpio = bank->nr_pins;
3125 gc->parent = &pdev->dev;
3126 gc->of_node = bank->of_node;
3127 gc->label = bank->name;
3129 ret = gpiochip_add_data(gc, bank);
3131 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
3137 rockchip_interrupts_register(pdev, info);
3142 for (--i, --bank; i >= 0; --i, --bank) {
3145 gpiochip_remove(&bank->gpio_chip);
3150 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
3151 struct rockchip_pinctrl *info)
3153 struct rockchip_pin_ctrl *ctrl = info->ctrl;
3154 struct rockchip_pin_bank *bank = ctrl->pin_banks;
3157 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3160 gpiochip_remove(&bank->gpio_chip);
3166 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
3167 struct rockchip_pinctrl *info)
3169 struct resource res;
3172 if (of_address_to_resource(bank->of_node, 0, &res)) {
3173 dev_err(info->dev, "cannot find IO resource for bank\n");
3177 bank->reg_base = devm_ioremap_resource(info->dev, &res);
3178 if (IS_ERR(bank->reg_base))
3179 return PTR_ERR(bank->reg_base);
3182 * special case, where parts of the pull setting-registers are
3183 * part of the PMU register space
3185 if (of_device_is_compatible(bank->of_node,
3186 "rockchip,rk3188-gpio-bank0")) {
3187 struct device_node *node;
3189 node = of_parse_phandle(bank->of_node->parent,
3192 if (of_address_to_resource(bank->of_node, 1, &res)) {
3193 dev_err(info->dev, "cannot find IO resource for bank\n");
3197 base = devm_ioremap_resource(info->dev, &res);
3199 return PTR_ERR(base);
3200 rockchip_regmap_config.max_register =
3201 resource_size(&res) - 4;
3202 rockchip_regmap_config.name =
3203 "rockchip,rk3188-gpio-bank0-pull";
3204 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
3206 &rockchip_regmap_config);
3211 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
3213 bank->clk = of_clk_get(bank->of_node, 0);
3214 if (IS_ERR(bank->clk))
3215 return PTR_ERR(bank->clk);
3217 return clk_prepare(bank->clk);
3220 static const struct of_device_id rockchip_pinctrl_dt_match[];
3222 /* retrieve the soc specific data */
3223 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
3224 struct rockchip_pinctrl *d,
3225 struct platform_device *pdev)
3227 const struct of_device_id *match;
3228 struct device_node *node = pdev->dev.of_node;
3229 struct device_node *np;
3230 struct rockchip_pin_ctrl *ctrl;
3231 struct rockchip_pin_bank *bank;
3232 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
3234 match = of_match_node(rockchip_pinctrl_dt_match, node);
3235 ctrl = (struct rockchip_pin_ctrl *)match->data;
3237 for_each_child_of_node(node, np) {
3238 if (!of_find_property(np, "gpio-controller", NULL))
3241 bank = ctrl->pin_banks;
3242 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3243 if (!strcmp(bank->name, np->name)) {
3246 if (!rockchip_get_bank_data(bank, d))
3254 grf_offs = ctrl->grf_mux_offset;
3255 pmu_offs = ctrl->pmu_mux_offset;
3256 drv_pmu_offs = ctrl->pmu_drv_offset;
3257 drv_grf_offs = ctrl->grf_drv_offset;
3258 bank = ctrl->pin_banks;
3259 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
3262 raw_spin_lock_init(&bank->slock);
3264 bank->pin_base = ctrl->nr_pins;
3265 ctrl->nr_pins += bank->nr_pins;
3267 /* calculate iomux and drv offsets */
3268 for (j = 0; j < 4; j++) {
3269 struct rockchip_iomux *iom = &bank->iomux[j];
3270 struct rockchip_drv *drv = &bank->drv[j];
3273 if (bank_pins >= bank->nr_pins)
3276 /* preset iomux offset value, set new start value */
3277 if (iom->offset >= 0) {
3278 if (iom->type & IOMUX_SOURCE_PMU)
3279 pmu_offs = iom->offset;
3281 grf_offs = iom->offset;
3282 } else { /* set current iomux offset */
3283 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3284 pmu_offs : grf_offs;
3287 /* preset drv offset value, set new start value */
3288 if (drv->offset >= 0) {
3289 if (iom->type & IOMUX_SOURCE_PMU)
3290 drv_pmu_offs = drv->offset;
3292 drv_grf_offs = drv->offset;
3293 } else { /* set current drv offset */
3294 drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
3295 drv_pmu_offs : drv_grf_offs;
3298 dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
3299 i, j, iom->offset, drv->offset);
3302 * Increase offset according to iomux width.
3303 * 4bit iomux'es are spread over two registers.
3305 inc = (iom->type & (IOMUX_WIDTH_4BIT |
3306 IOMUX_WIDTH_3BIT)) ? 8 : 4;
3307 if (iom->type & IOMUX_SOURCE_PMU)
3313 * Increase offset according to drv width.
3314 * 3bit drive-strenth'es are spread over two registers.
3316 if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
3317 (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
3322 if (iom->type & IOMUX_SOURCE_PMU)
3323 drv_pmu_offs += inc;
3325 drv_grf_offs += inc;
3330 /* calculate the per-bank recalced_mask */
3331 for (j = 0; j < ctrl->niomux_recalced; j++) {
3334 if (ctrl->iomux_recalced[j].num == bank->bank_num) {
3335 pin = ctrl->iomux_recalced[j].pin;
3336 bank->recalced_mask |= BIT(pin);
3340 /* calculate the per-bank route_mask */
3341 for (j = 0; j < ctrl->niomux_routes; j++) {
3344 if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
3345 pin = ctrl->iomux_routes[j].pin;
3346 bank->route_mask |= BIT(pin);
3354 #define RK3288_GRF_GPIO6C_IOMUX 0x64
3355 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
3357 static u32 rk3288_grf_gpio6c_iomux;
3359 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
3361 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3362 int ret = pinctrl_force_sleep(info->pctl_dev);
3368 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
3369 * the setting here, and restore it at resume.
3371 if (info->ctrl->type == RK3288) {
3372 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3373 &rk3288_grf_gpio6c_iomux);
3375 pinctrl_force_default(info->pctl_dev);
3383 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
3385 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
3386 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
3387 rk3288_grf_gpio6c_iomux |
3388 GPIO6C6_SEL_WRITE_ENABLE);
3393 return pinctrl_force_default(info->pctl_dev);
3396 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
3397 rockchip_pinctrl_resume);
3399 static int rockchip_pinctrl_probe(struct platform_device *pdev)
3401 struct rockchip_pinctrl *info;
3402 struct device *dev = &pdev->dev;
3403 struct rockchip_pin_ctrl *ctrl;
3404 struct device_node *np = pdev->dev.of_node, *node;
3405 struct resource *res;
3409 if (!dev->of_node) {
3410 dev_err(dev, "device tree node not found\n");
3414 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3420 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
3422 dev_err(dev, "driver data not available\n");
3427 node = of_parse_phandle(np, "rockchip,grf", 0);
3429 info->regmap_base = syscon_node_to_regmap(node);
3430 if (IS_ERR(info->regmap_base))
3431 return PTR_ERR(info->regmap_base);
3433 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3434 base = devm_ioremap_resource(&pdev->dev, res);
3436 return PTR_ERR(base);
3438 rockchip_regmap_config.max_register = resource_size(res) - 4;
3439 rockchip_regmap_config.name = "rockchip,pinctrl";
3440 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
3441 &rockchip_regmap_config);
3443 /* to check for the old dt-bindings */
3444 info->reg_size = resource_size(res);
3446 /* Honor the old binding, with pull registers as 2nd resource */
3447 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
3448 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3449 base = devm_ioremap_resource(&pdev->dev, res);
3451 return PTR_ERR(base);
3453 rockchip_regmap_config.max_register =
3454 resource_size(res) - 4;
3455 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
3456 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
3458 &rockchip_regmap_config);
3462 /* try to find the optional reference to the pmu syscon */
3463 node = of_parse_phandle(np, "rockchip,pmu", 0);
3465 info->regmap_pmu = syscon_node_to_regmap(node);
3466 if (IS_ERR(info->regmap_pmu))
3467 return PTR_ERR(info->regmap_pmu);
3470 ret = rockchip_gpiolib_register(pdev, info);
3474 ret = rockchip_pinctrl_register(pdev, info);
3476 rockchip_gpiolib_unregister(pdev, info);
3480 platform_set_drvdata(pdev, info);
3485 static struct rockchip_pin_bank px30_pin_banks[] = {
3486 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3491 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_WIDTH_4BIT,
3496 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_WIDTH_4BIT,
3501 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_WIDTH_4BIT,
3508 static struct rockchip_pin_ctrl px30_pin_ctrl = {
3509 .pin_banks = px30_pin_banks,
3510 .nr_banks = ARRAY_SIZE(px30_pin_banks),
3511 .label = "PX30-GPIO",
3513 .grf_mux_offset = 0x0,
3514 .pmu_mux_offset = 0x0,
3515 .iomux_routes = px30_mux_route_data,
3516 .niomux_routes = ARRAY_SIZE(px30_mux_route_data),
3517 .pull_calc_reg = px30_calc_pull_reg_and_bit,
3518 .drv_calc_reg = px30_calc_drv_reg_and_bit,
3519 .schmitt_calc_reg = px30_calc_schmitt_reg_and_bit,
3522 static struct rockchip_pin_bank rv1108_pin_banks[] = {
3523 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3527 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3528 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
3529 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
3532 static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
3533 .pin_banks = rv1108_pin_banks,
3534 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
3535 .label = "RV1108-GPIO",
3537 .grf_mux_offset = 0x10,
3538 .pmu_mux_offset = 0x0,
3539 .iomux_recalced = rv1108_mux_recalced_data,
3540 .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data),
3541 .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
3542 .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
3543 .schmitt_calc_reg = rv1108_calc_schmitt_reg_and_bit,
3546 static struct rockchip_pin_bank rk2928_pin_banks[] = {
3547 PIN_BANK(0, 32, "gpio0"),
3548 PIN_BANK(1, 32, "gpio1"),
3549 PIN_BANK(2, 32, "gpio2"),
3550 PIN_BANK(3, 32, "gpio3"),
3553 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
3554 .pin_banks = rk2928_pin_banks,
3555 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
3556 .label = "RK2928-GPIO",
3558 .grf_mux_offset = 0xa8,
3559 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3562 static struct rockchip_pin_bank rk3036_pin_banks[] = {
3563 PIN_BANK(0, 32, "gpio0"),
3564 PIN_BANK(1, 32, "gpio1"),
3565 PIN_BANK(2, 32, "gpio2"),
3568 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
3569 .pin_banks = rk3036_pin_banks,
3570 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
3571 .label = "RK3036-GPIO",
3573 .grf_mux_offset = 0xa8,
3574 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3577 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
3578 PIN_BANK(0, 32, "gpio0"),
3579 PIN_BANK(1, 32, "gpio1"),
3580 PIN_BANK(2, 32, "gpio2"),
3581 PIN_BANK(3, 32, "gpio3"),
3582 PIN_BANK(4, 32, "gpio4"),
3583 PIN_BANK(6, 16, "gpio6"),
3586 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
3587 .pin_banks = rk3066a_pin_banks,
3588 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
3589 .label = "RK3066a-GPIO",
3591 .grf_mux_offset = 0xa8,
3592 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
3595 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
3596 PIN_BANK(0, 32, "gpio0"),
3597 PIN_BANK(1, 32, "gpio1"),
3598 PIN_BANK(2, 32, "gpio2"),
3599 PIN_BANK(3, 32, "gpio3"),
3602 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
3603 .pin_banks = rk3066b_pin_banks,
3604 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
3605 .label = "RK3066b-GPIO",
3607 .grf_mux_offset = 0x60,
3610 static struct rockchip_pin_bank rk3128_pin_banks[] = {
3611 PIN_BANK(0, 32, "gpio0"),
3612 PIN_BANK(1, 32, "gpio1"),
3613 PIN_BANK(2, 32, "gpio2"),
3614 PIN_BANK(3, 32, "gpio3"),
3617 static struct rockchip_pin_ctrl rk3128_pin_ctrl = {
3618 .pin_banks = rk3128_pin_banks,
3619 .nr_banks = ARRAY_SIZE(rk3128_pin_banks),
3620 .label = "RK3128-GPIO",
3622 .grf_mux_offset = 0xa8,
3623 .iomux_recalced = rk3128_mux_recalced_data,
3624 .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data),
3625 .iomux_routes = rk3128_mux_route_data,
3626 .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data),
3627 .pull_calc_reg = rk3128_calc_pull_reg_and_bit,
3630 static struct rockchip_pin_bank rk3188_pin_banks[] = {
3631 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
3632 PIN_BANK(1, 32, "gpio1"),
3633 PIN_BANK(2, 32, "gpio2"),
3634 PIN_BANK(3, 32, "gpio3"),
3637 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
3638 .pin_banks = rk3188_pin_banks,
3639 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
3640 .label = "RK3188-GPIO",
3642 .grf_mux_offset = 0x60,
3643 .iomux_routes = rk3188_mux_route_data,
3644 .niomux_routes = ARRAY_SIZE(rk3188_mux_route_data),
3645 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
3648 static struct rockchip_pin_bank rk3228_pin_banks[] = {
3649 PIN_BANK(0, 32, "gpio0"),
3650 PIN_BANK(1, 32, "gpio1"),
3651 PIN_BANK(2, 32, "gpio2"),
3652 PIN_BANK(3, 32, "gpio3"),
3655 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
3656 .pin_banks = rk3228_pin_banks,
3657 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
3658 .label = "RK3228-GPIO",
3660 .grf_mux_offset = 0x0,
3661 .iomux_routes = rk3228_mux_route_data,
3662 .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data),
3663 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3664 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3667 static struct rockchip_pin_bank rk3288_pin_banks[] = {
3668 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
3673 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
3678 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
3679 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
3680 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
3685 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
3690 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
3691 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
3696 PIN_BANK(8, 16, "gpio8"),
3699 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
3700 .pin_banks = rk3288_pin_banks,
3701 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
3702 .label = "RK3288-GPIO",
3704 .grf_mux_offset = 0x0,
3705 .pmu_mux_offset = 0x84,
3706 .iomux_routes = rk3288_mux_route_data,
3707 .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
3708 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
3709 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
3712 static struct rockchip_pin_bank rk3328_pin_banks[] = {
3713 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
3714 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
3715 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
3719 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
3726 static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
3727 .pin_banks = rk3328_pin_banks,
3728 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
3729 .label = "RK3328-GPIO",
3731 .grf_mux_offset = 0x0,
3732 .iomux_recalced = rk3328_mux_recalced_data,
3733 .niomux_recalced = ARRAY_SIZE(rk3328_mux_recalced_data),
3734 .iomux_routes = rk3328_mux_route_data,
3735 .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data),
3736 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
3737 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
3738 .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
3741 static struct rockchip_pin_bank rk3368_pin_banks[] = {
3742 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
3747 PIN_BANK(1, 32, "gpio1"),
3748 PIN_BANK(2, 32, "gpio2"),
3749 PIN_BANK(3, 32, "gpio3"),
3752 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
3753 .pin_banks = rk3368_pin_banks,
3754 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
3755 .label = "RK3368-GPIO",
3757 .grf_mux_offset = 0x0,
3758 .pmu_mux_offset = 0x0,
3759 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
3760 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
3763 static struct rockchip_pin_bank rk3399_pin_banks[] = {
3764 PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
3769 DRV_TYPE_IO_1V8_ONLY,
3770 DRV_TYPE_IO_1V8_ONLY,
3771 DRV_TYPE_IO_DEFAULT,
3772 DRV_TYPE_IO_DEFAULT,
3777 PULL_TYPE_IO_1V8_ONLY,
3778 PULL_TYPE_IO_1V8_ONLY,
3779 PULL_TYPE_IO_DEFAULT,
3780 PULL_TYPE_IO_DEFAULT
3782 PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
3786 DRV_TYPE_IO_1V8_OR_3V0,
3787 DRV_TYPE_IO_1V8_OR_3V0,
3788 DRV_TYPE_IO_1V8_OR_3V0,
3789 DRV_TYPE_IO_1V8_OR_3V0,
3795 PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
3796 DRV_TYPE_IO_1V8_OR_3V0,
3797 DRV_TYPE_IO_1V8_ONLY,
3798 DRV_TYPE_IO_1V8_ONLY,
3799 PULL_TYPE_IO_DEFAULT,
3800 PULL_TYPE_IO_DEFAULT,
3801 PULL_TYPE_IO_1V8_ONLY,
3802 PULL_TYPE_IO_1V8_ONLY
3804 PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
3805 DRV_TYPE_IO_3V3_ONLY,
3806 DRV_TYPE_IO_3V3_ONLY,
3807 DRV_TYPE_IO_1V8_OR_3V0
3809 PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
3810 DRV_TYPE_IO_1V8_3V0_AUTO,
3811 DRV_TYPE_IO_1V8_OR_3V0,
3812 DRV_TYPE_IO_1V8_OR_3V0
3816 static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
3817 .pin_banks = rk3399_pin_banks,
3818 .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
3819 .label = "RK3399-GPIO",
3821 .grf_mux_offset = 0xe000,
3822 .pmu_mux_offset = 0x0,
3823 .grf_drv_offset = 0xe100,
3824 .pmu_drv_offset = 0x80,
3825 .iomux_routes = rk3399_mux_route_data,
3826 .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
3827 .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
3828 .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
3831 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
3832 { .compatible = "rockchip,px30-pinctrl",
3833 .data = &px30_pin_ctrl },
3834 { .compatible = "rockchip,rv1108-pinctrl",
3835 .data = &rv1108_pin_ctrl },
3836 { .compatible = "rockchip,rk2928-pinctrl",
3837 .data = &rk2928_pin_ctrl },
3838 { .compatible = "rockchip,rk3036-pinctrl",
3839 .data = &rk3036_pin_ctrl },
3840 { .compatible = "rockchip,rk3066a-pinctrl",
3841 .data = &rk3066a_pin_ctrl },
3842 { .compatible = "rockchip,rk3066b-pinctrl",
3843 .data = &rk3066b_pin_ctrl },
3844 { .compatible = "rockchip,rk3128-pinctrl",
3845 .data = (void *)&rk3128_pin_ctrl },
3846 { .compatible = "rockchip,rk3188-pinctrl",
3847 .data = &rk3188_pin_ctrl },
3848 { .compatible = "rockchip,rk3228-pinctrl",
3849 .data = &rk3228_pin_ctrl },
3850 { .compatible = "rockchip,rk3288-pinctrl",
3851 .data = &rk3288_pin_ctrl },
3852 { .compatible = "rockchip,rk3328-pinctrl",
3853 .data = &rk3328_pin_ctrl },
3854 { .compatible = "rockchip,rk3368-pinctrl",
3855 .data = &rk3368_pin_ctrl },
3856 { .compatible = "rockchip,rk3399-pinctrl",
3857 .data = &rk3399_pin_ctrl },
3861 static struct platform_driver rockchip_pinctrl_driver = {
3862 .probe = rockchip_pinctrl_probe,
3864 .name = "rockchip-pinctrl",
3865 .pm = &rockchip_pinctrl_dev_pm_ops,
3866 .of_match_table = rockchip_pinctrl_dt_match,
3870 static int __init rockchip_pinctrl_drv_register(void)
3872 return platform_driver_register(&rockchip_pinctrl_driver);
3874 postcore_initcall(rockchip_pinctrl_drv_register);