blob: 47c2e923a1bb5292bdbe934a91fdb78404c37327 [file] [log] [blame]
David Wu5f596ae2019-01-02 21:00:55 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4 */
5
David Wu5f596ae2019-01-02 21:00:55 +08006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
David Wu5f596ae2019-01-02 21:00:55 +08008#include <dm/pinctrl.h>
9#include <regmap.h>
10#include <syscon.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
David Wu5f596ae2019-01-02 21:00:55 +080012
13#include "pinctrl-rockchip.h"
14
15static struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
16 {
17 .num = 2,
18 .pin = 12,
19 .reg = 0x24,
20 .bit = 8,
21 .mask = 0x3
22 }, {
23 .num = 2,
24 .pin = 15,
25 .reg = 0x28,
26 .bit = 0,
27 .mask = 0x7
28 }, {
29 .num = 2,
30 .pin = 23,
31 .reg = 0x30,
32 .bit = 14,
33 .mask = 0x3
34 },
35};
36
37static struct rockchip_mux_route_data rk3328_mux_route_data[] = {
38 {
39 /* uart2dbg_rxm0 */
40 .bank_num = 1,
41 .pin = 1,
42 .func = 2,
43 .route_offset = 0x50,
44 .route_val = BIT(16) | BIT(16 + 1),
45 }, {
46 /* uart2dbg_rxm1 */
47 .bank_num = 2,
48 .pin = 1,
49 .func = 1,
50 .route_offset = 0x50,
51 .route_val = BIT(16) | BIT(16 + 1) | BIT(0),
52 }, {
53 /* gmac-m1_rxd0 */
54 .bank_num = 1,
55 .pin = 11,
56 .func = 2,
57 .route_offset = 0x50,
58 .route_val = BIT(16 + 2) | BIT(2),
59 }, {
60 /* gmac-m1-optimized_rxd3 */
61 .bank_num = 1,
62 .pin = 14,
63 .func = 2,
64 .route_offset = 0x50,
65 .route_val = BIT(16 + 10) | BIT(10),
66 }, {
67 /* pdm_sdi0m0 */
68 .bank_num = 2,
69 .pin = 19,
70 .func = 2,
71 .route_offset = 0x50,
72 .route_val = BIT(16 + 3),
73 }, {
74 /* pdm_sdi0m1 */
75 .bank_num = 1,
76 .pin = 23,
77 .func = 3,
78 .route_offset = 0x50,
79 .route_val = BIT(16 + 3) | BIT(3),
80 }, {
81 /* spi_rxdm2 */
82 .bank_num = 3,
83 .pin = 2,
84 .func = 4,
85 .route_offset = 0x50,
86 .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5),
87 }, {
88 /* i2s2_sdim0 */
89 .bank_num = 1,
90 .pin = 24,
91 .func = 1,
92 .route_offset = 0x50,
93 .route_val = BIT(16 + 6),
94 }, {
95 /* i2s2_sdim1 */
96 .bank_num = 3,
97 .pin = 2,
98 .func = 6,
99 .route_offset = 0x50,
100 .route_val = BIT(16 + 6) | BIT(6),
101 }, {
102 /* card_iom1 */
103 .bank_num = 2,
104 .pin = 22,
105 .func = 3,
106 .route_offset = 0x50,
107 .route_val = BIT(16 + 7) | BIT(7),
108 }, {
109 /* tsp_d5m1 */
110 .bank_num = 2,
111 .pin = 16,
112 .func = 3,
113 .route_offset = 0x50,
114 .route_val = BIT(16 + 8) | BIT(8),
115 }, {
116 /* cif_data5m1 */
117 .bank_num = 2,
118 .pin = 16,
119 .func = 4,
120 .route_offset = 0x50,
121 .route_val = BIT(16 + 9) | BIT(9),
122 },
123};
124
David Wu3dd7d6c2019-04-16 21:50:55 +0800125static int rk3328_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
126{
127 struct rockchip_pinctrl_priv *priv = bank->priv;
128 int iomux_num = (pin / 8);
129 struct regmap *regmap;
130 int reg, ret, mask, mux_type;
131 u8 bit;
Jagan Teki9e0e6812022-12-14 23:20:56 +0530132 u32 data;
David Wu3dd7d6c2019-04-16 21:50:55 +0800133
134 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
135 ? priv->regmap_pmu : priv->regmap_base;
136
137 /* get basic quadrupel of mux registers and the correct reg inside */
138 mux_type = bank->iomux[iomux_num].type;
139 reg = bank->iomux[iomux_num].offset;
140 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
141
142 if (bank->recalced_mask & BIT(pin))
143 rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
144
David Wu3dd7d6c2019-04-16 21:50:55 +0800145 data = (mask << (bit + 16));
146 data |= (mux & mask) << bit;
147 ret = regmap_write(regmap, reg, data);
148
149 return ret;
150}
151
David Wu5f596ae2019-01-02 21:00:55 +0800152#define RK3328_PULL_OFFSET 0x100
153
154static void rk3328_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
155 int pin_num, struct regmap **regmap,
156 int *reg, u8 *bit)
157{
158 struct rockchip_pinctrl_priv *priv = bank->priv;
159
160 *regmap = priv->regmap_base;
161 *reg = RK3328_PULL_OFFSET;
162 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
163 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
164
165 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
166 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
167}
168
David Wu2972c452019-04-16 21:57:05 +0800169static int rk3328_set_pull(struct rockchip_pin_bank *bank,
170 int pin_num, int pull)
171{
172 struct regmap *regmap;
173 int reg, ret;
174 u8 bit, type;
175 u32 data;
176
177 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
178 return -ENOTSUPP;
179
180 rk3328_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
181 type = bank->pull_type[pin_num / 8];
182 ret = rockchip_translate_pull_value(type, pull);
183 if (ret < 0) {
184 debug("unsupported pull setting %d\n", pull);
185 return ret;
186 }
187
188 /* enable the write to the equivalent lower bits */
189 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
190 data |= (ret << bit);
191 ret = regmap_write(regmap, reg, data);
192
193 return ret;
194}
195
David Wu5f596ae2019-01-02 21:00:55 +0800196#define RK3328_DRV_GRF_OFFSET 0x200
197
198static void rk3328_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
199 int pin_num, struct regmap **regmap,
200 int *reg, u8 *bit)
201{
202 struct rockchip_pinctrl_priv *priv = bank->priv;
203
204 *regmap = priv->regmap_base;
205 *reg = RK3328_DRV_GRF_OFFSET;
206 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
207 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
208
209 *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG);
210 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
211}
212
David Wu40a55482019-04-16 21:55:26 +0800213static int rk3328_set_drive(struct rockchip_pin_bank *bank,
214 int pin_num, int strength)
215{
216 struct regmap *regmap;
217 int reg, ret;
218 u32 data;
219 u8 bit;
220 int type = bank->drv[pin_num / 8].drv_type;
221
222 rk3328_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
223 ret = rockchip_translate_drive_value(type, strength);
224 if (ret < 0) {
225 debug("unsupported driver strength %d\n", strength);
226 return ret;
227 }
228
229 /* enable the write to the equivalent lower bits */
230 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
231 data |= (ret << bit);
232 ret = regmap_write(regmap, reg, data);
233
234 return ret;
235}
236
David Wu5f596ae2019-01-02 21:00:55 +0800237#define RK3328_SCHMITT_BITS_PER_PIN 1
238#define RK3328_SCHMITT_PINS_PER_REG 16
239#define RK3328_SCHMITT_BANK_STRIDE 8
240#define RK3328_SCHMITT_GRF_OFFSET 0x380
241
242static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
243 int pin_num,
244 struct regmap **regmap,
245 int *reg, u8 *bit)
246{
247 struct rockchip_pinctrl_priv *priv = bank->priv;
248
249 *regmap = priv->regmap_base;
250 *reg = RK3328_SCHMITT_GRF_OFFSET;
251
252 *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
253 *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
254 *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
255
256 return 0;
257}
258
David Wu7ae4ec92019-04-16 21:58:13 +0800259static int rk3328_set_schmitt(struct rockchip_pin_bank *bank,
260 int pin_num, int enable)
261{
262 struct regmap *regmap;
263 int reg;
264 u8 bit;
265 u32 data;
266
267 rk3328_calc_schmitt_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
268 /* enable the write to the equivalent lower bits */
269 data = BIT(bit + 16) | (enable << bit);
270
271 return regmap_write(regmap, reg, data);
272}
273
David Wu5f596ae2019-01-02 21:00:55 +0800274static struct rockchip_pin_bank rk3328_pin_banks[] = {
275 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
276 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
277 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
278 IOMUX_WIDTH_3BIT,
279 IOMUX_WIDTH_3BIT,
280 0),
281 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
282 IOMUX_WIDTH_3BIT,
283 IOMUX_WIDTH_3BIT,
284 0,
285 0),
286};
287
288static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
David Wu71aede02019-04-16 21:50:54 +0800289 .pin_banks = rk3328_pin_banks,
290 .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
David Wu71aede02019-04-16 21:50:54 +0800291 .grf_mux_offset = 0x0,
292 .iomux_recalced = rk3328_mux_recalced_data,
293 .niomux_recalced = ARRAY_SIZE(rk3328_mux_recalced_data),
294 .iomux_routes = rk3328_mux_route_data,
295 .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data),
David Wu3dd7d6c2019-04-16 21:50:55 +0800296 .set_mux = rk3328_set_mux,
David Wu2972c452019-04-16 21:57:05 +0800297 .set_pull = rk3328_set_pull,
David Wu40a55482019-04-16 21:55:26 +0800298 .set_drive = rk3328_set_drive,
David Wu7ae4ec92019-04-16 21:58:13 +0800299 .set_schmitt = rk3328_set_schmitt,
David Wu5f596ae2019-01-02 21:00:55 +0800300};
301
302static const struct udevice_id rk3328_pinctrl_ids[] = {
303 {
304 .compatible = "rockchip,rk3328-pinctrl",
305 .data = (ulong)&rk3328_pin_ctrl
306 },
307 { }
308};
309
Walter Lozano2901ac62020-06-25 01:10:04 -0300310U_BOOT_DRIVER(rockchip_rk3328_pinctrl) = {
David Wu5f596ae2019-01-02 21:00:55 +0800311 .name = "rockchip_rk3328_pinctrl",
312 .id = UCLASS_PINCTRL,
313 .of_match = rk3328_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700314 .priv_auto = sizeof(struct rockchip_pinctrl_priv),
David Wu5f596ae2019-01-02 21:00:55 +0800315 .ops = &rockchip_pinctrl_ops,
Simon Glass92882652021-08-07 07:24:04 -0600316#if CONFIG_IS_ENABLED(OF_REAL)
David Wu5f596ae2019-01-02 21:00:55 +0800317 .bind = dm_scan_fdt_dev,
318#endif
319 .probe = rockchip_pinctrl_probe,
320};