1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
8 #include <dm/pinctrl.h>
11 #include <linux/bitops.h>
13 #include "pinctrl-rockchip.h"
15 static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = {
49 static struct rockchip_mux_route_data rk3128_mux_route_data[] = {
55 .route_offset = 0x144,
56 .route_val = BIT(16 + 3) | BIT(16 + 4),
62 .route_offset = 0x144,
63 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3),
69 .route_offset = 0x144,
70 .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4),
76 .route_offset = 0x144,
77 .route_val = BIT(16 + 5),
83 .route_offset = 0x144,
84 .route_val = BIT(16 + 5) | BIT(5),
90 .route_offset = 0x144,
91 .route_val = BIT(16 + 6),
97 .route_offset = 0x144,
98 .route_val = BIT(16 + 6) | BIT(6),
102 static int rk3128_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
104 struct rockchip_pinctrl_priv *priv = bank->priv;
105 int iomux_num = (pin / 8);
106 struct regmap *regmap;
107 int reg, ret, mask, mux_type;
109 u32 data, route_reg, route_val;
111 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
112 ? priv->regmap_pmu : priv->regmap_base;
114 /* get basic quadrupel of mux registers and the correct reg inside */
115 mux_type = bank->iomux[iomux_num].type;
116 reg = bank->iomux[iomux_num].offset;
117 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
119 if (bank->recalced_mask & BIT(pin))
120 rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask);
122 if (bank->route_mask & BIT(pin)) {
123 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
125 ret = regmap_write(regmap, route_reg, route_val);
131 data = (mask << (bit + 16));
132 data |= (mux & mask) << bit;
133 ret = regmap_write(regmap, reg, data);
138 #define RK3128_PULL_OFFSET 0x118
139 #define RK3128_PULL_PINS_PER_REG 16
140 #define RK3128_PULL_BANK_STRIDE 8
142 static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
143 int pin_num, struct regmap **regmap,
146 struct rockchip_pinctrl_priv *priv = bank->priv;
148 *regmap = priv->regmap_base;
149 *reg = RK3128_PULL_OFFSET;
150 *reg += bank->bank_num * RK3128_PULL_BANK_STRIDE;
151 *reg += ((pin_num / RK3128_PULL_PINS_PER_REG) * 4);
153 *bit = pin_num % RK3128_PULL_PINS_PER_REG;
156 static int rk3128_set_pull(struct rockchip_pin_bank *bank,
157 int pin_num, int pull)
159 struct regmap *regmap;
164 if (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT &&
165 pull != PIN_CONFIG_BIAS_DISABLE)
168 rk3128_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit);
169 data = BIT(bit + 16);
170 if (pull == PIN_CONFIG_BIAS_DISABLE)
172 ret = regmap_write(regmap, reg, data);
177 static struct rockchip_pin_bank rk3128_pin_banks[] = {
178 PIN_BANK(0, 32, "gpio0"),
179 PIN_BANK(1, 32, "gpio1"),
180 PIN_BANK(2, 32, "gpio2"),
181 PIN_BANK(3, 32, "gpio3"),
184 static struct rockchip_pin_ctrl rk3128_pin_ctrl = {
185 .pin_banks = rk3128_pin_banks,
186 .nr_banks = ARRAY_SIZE(rk3128_pin_banks),
187 .grf_mux_offset = 0xa8,
188 .iomux_recalced = rk3128_mux_recalced_data,
189 .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data),
190 .iomux_routes = rk3128_mux_route_data,
191 .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data),
192 .set_mux = rk3128_set_mux,
193 .set_pull = rk3128_set_pull,
196 static const struct udevice_id rk3128_pinctrl_ids[] = {
197 { .compatible = "rockchip,rk3128-pinctrl",
198 .data = (ulong)&rk3128_pin_ctrl },
202 U_BOOT_DRIVER(pinctrl_rk3128) = {
203 .name = "pinctrl_rk3128",
204 .id = UCLASS_PINCTRL,
205 .of_match = rk3128_pinctrl_ids,
206 .priv_auto = sizeof(struct rockchip_pinctrl_priv),
207 .ops = &rockchip_pinctrl_ops,
208 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
209 .bind = dm_scan_fdt_dev,
211 .probe = rockchip_pinctrl_probe,