1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
9 #include <dm/pinctrl.h>
11 #include <linux/bitops.h>
13 #include "pinctrl-rockchip.h"
15 static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
17 /* edphdmi_cecinoutt1 */
21 .route_offset = 0x264,
22 .route_val = BIT(16 + 12) | BIT(12),
24 /* edphdmi_cecinout */
28 .route_offset = 0x264,
29 .route_val = BIT(16 + 12),
33 static int rk3288_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
35 struct rockchip_pinctrl_priv *priv = bank->priv;
36 int iomux_num = (pin / 8);
37 struct regmap *regmap;
38 int reg, ret, mask, mux_type;
40 u32 data, route_reg, route_val;
42 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
43 ? priv->regmap_pmu : priv->regmap_base;
45 /* get basic quadrupel of mux registers and the correct reg inside */
46 mux_type = bank->iomux[iomux_num].type;
47 reg = bank->iomux[iomux_num].offset;
48 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
50 if (bank->route_mask & BIT(pin)) {
51 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
53 ret = regmap_write(regmap, route_reg, route_val);
59 /* bank0 is special, there are no higher 16 bit writing bits. */
60 if (bank->bank_num == 0) {
61 regmap_read(regmap, reg, &data);
62 data &= ~(mask << bit);
64 /* enable the write to the equivalent lower bits */
65 data = (mask << (bit + 16));
68 data |= (mux & mask) << bit;
69 ret = regmap_write(regmap, reg, data);
74 #define RK3288_PULL_OFFSET 0x140
75 #define RK3288_PULL_PMU_OFFSET 0x64
77 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
78 int pin_num, struct regmap **regmap,
81 struct rockchip_pinctrl_priv *priv = bank->priv;
83 /* The first 24 pins of the first bank are located in PMU */
84 if (bank->bank_num == 0) {
85 *regmap = priv->regmap_pmu;
86 *reg = RK3288_PULL_PMU_OFFSET;
88 *regmap = priv->regmap_base;
89 *reg = RK3288_PULL_OFFSET;
91 /* correct the offset, as we're starting with the 2nd bank */
93 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
96 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
98 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
99 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
102 static int rk3288_set_pull(struct rockchip_pin_bank *bank,
103 int pin_num, int pull)
105 struct regmap *regmap;
110 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
113 rk3288_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit);
114 type = bank->pull_type[pin_num / 8];
115 ret = rockchip_translate_pull_value(type, pull);
117 debug("unsupported pull setting %d\n", pull);
121 /* bank0 is special, there are no higher 16 bit writing bits */
122 if (bank->bank_num == 0) {
123 regmap_read(regmap, reg, &data);
124 data &= ~(((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << bit);
126 /* enable the write to the equivalent lower bits */
127 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
130 data |= (ret << bit);
131 ret = regmap_write(regmap, reg, data);
136 #define RK3288_DRV_PMU_OFFSET 0x70
137 #define RK3288_DRV_GRF_OFFSET 0x1c0
139 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
140 int pin_num, struct regmap **regmap,
143 struct rockchip_pinctrl_priv *priv = bank->priv;
145 /* The first 24 pins of the first bank are located in PMU */
146 if (bank->bank_num == 0) {
147 *regmap = priv->regmap_pmu;
148 *reg = RK3288_DRV_PMU_OFFSET;
150 *regmap = priv->regmap_base;
151 *reg = RK3288_DRV_GRF_OFFSET;
153 /* correct the offset, as we're starting with the 2nd bank */
155 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
158 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
159 *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG);
160 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
163 static int rk3288_set_drive(struct rockchip_pin_bank *bank,
164 int pin_num, int strength)
166 struct regmap *regmap;
170 int type = bank->drv[pin_num / 8].drv_type;
172 rk3288_calc_drv_reg_and_bit(bank, pin_num, ®map, ®, &bit);
173 ret = rockchip_translate_drive_value(type, strength);
175 debug("unsupported driver strength %d\n", strength);
179 /* bank0 is special, there are no higher 16 bit writing bits. */
180 if (bank->bank_num == 0) {
181 regmap_read(regmap, reg, &data);
182 data &= ~(((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << bit);
184 /* enable the write to the equivalent lower bits */
185 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
188 data |= (ret << bit);
189 ret = regmap_write(regmap, reg, data);
193 static struct rockchip_pin_bank rk3288_pin_banks[] = {
194 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
199 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
204 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
205 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
206 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
211 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
216 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
217 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
222 PIN_BANK(8, 16, "gpio8"),
225 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
226 .pin_banks = rk3288_pin_banks,
227 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
228 .grf_mux_offset = 0x0,
229 .pmu_mux_offset = 0x84,
230 .iomux_routes = rk3288_mux_route_data,
231 .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
232 .set_mux = rk3288_set_mux,
233 .set_pull = rk3288_set_pull,
234 .set_drive = rk3288_set_drive,
237 static const struct udevice_id rk3288_pinctrl_ids[] = {
239 .compatible = "rockchip,rk3288-pinctrl",
240 .data = (ulong)&rk3288_pin_ctrl
245 U_BOOT_DRIVER(pinctrl_rk3288) = {
246 .name = "rockchip_rk3288_pinctrl",
247 .id = UCLASS_PINCTRL,
248 .of_match = rk3288_pinctrl_ids,
249 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
250 .ops = &rockchip_pinctrl_ops,
251 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
252 .bind = dm_scan_fdt_dev,
254 .probe = rockchip_pinctrl_probe,