blob: b804597c048415f3875222b201442fc025b35658 [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_route_data rk3228_mux_route_data[] = {
16 {
17 /* pwm0-0 */
18 .bank_num = 0,
19 .pin = 26,
20 .func = 1,
21 .route_offset = 0x50,
22 .route_val = BIT(16),
23 }, {
24 /* pwm0-1 */
25 .bank_num = 3,
26 .pin = 21,
27 .func = 1,
28 .route_offset = 0x50,
29 .route_val = BIT(16) | BIT(0),
30 }, {
31 /* pwm1-0 */
32 .bank_num = 0,
33 .pin = 27,
34 .func = 1,
35 .route_offset = 0x50,
36 .route_val = BIT(16 + 1),
37 }, {
38 /* pwm1-1 */
39 .bank_num = 0,
40 .pin = 30,
41 .func = 2,
42 .route_offset = 0x50,
43 .route_val = BIT(16 + 1) | BIT(1),
44 }, {
45 /* pwm2-0 */
46 .bank_num = 0,
47 .pin = 28,
48 .func = 1,
49 .route_offset = 0x50,
50 .route_val = BIT(16 + 2),
51 }, {
52 /* pwm2-1 */
53 .bank_num = 1,
54 .pin = 12,
55 .func = 2,
56 .route_offset = 0x50,
57 .route_val = BIT(16 + 2) | BIT(2),
58 }, {
59 /* pwm3-0 */
60 .bank_num = 3,
61 .pin = 26,
62 .func = 1,
63 .route_offset = 0x50,
64 .route_val = BIT(16 + 3),
65 }, {
66 /* pwm3-1 */
67 .bank_num = 1,
68 .pin = 11,
69 .func = 2,
70 .route_offset = 0x50,
71 .route_val = BIT(16 + 3) | BIT(3),
72 }, {
73 /* sdio-0_d0 */
74 .bank_num = 1,
75 .pin = 1,
76 .func = 1,
77 .route_offset = 0x50,
78 .route_val = BIT(16 + 4),
79 }, {
80 /* sdio-1_d0 */
81 .bank_num = 3,
82 .pin = 2,
83 .func = 1,
84 .route_offset = 0x50,
85 .route_val = BIT(16 + 4) | BIT(4),
86 }, {
87 /* spi-0_rx */
88 .bank_num = 0,
89 .pin = 13,
90 .func = 2,
91 .route_offset = 0x50,
92 .route_val = BIT(16 + 5),
93 }, {
94 /* spi-1_rx */
95 .bank_num = 2,
96 .pin = 0,
97 .func = 2,
98 .route_offset = 0x50,
99 .route_val = BIT(16 + 5) | BIT(5),
100 }, {
101 /* emmc-0_cmd */
102 .bank_num = 1,
103 .pin = 22,
104 .func = 2,
105 .route_offset = 0x50,
106 .route_val = BIT(16 + 7),
107 }, {
108 /* emmc-1_cmd */
109 .bank_num = 2,
110 .pin = 4,
111 .func = 2,
112 .route_offset = 0x50,
113 .route_val = BIT(16 + 7) | BIT(7),
114 }, {
115 /* uart2-0_rx */
116 .bank_num = 1,
117 .pin = 19,
118 .func = 2,
119 .route_offset = 0x50,
120 .route_val = BIT(16 + 8),
121 }, {
122 /* uart2-1_rx */
123 .bank_num = 1,
124 .pin = 10,
125 .func = 2,
126 .route_offset = 0x50,
127 .route_val = BIT(16 + 8) | BIT(8),
128 }, {
129 /* uart1-0_rx */
130 .bank_num = 1,
131 .pin = 10,
132 .func = 1,
133 .route_offset = 0x50,
134 .route_val = BIT(16 + 11),
135 }, {
136 /* uart1-1_rx */
137 .bank_num = 3,
138 .pin = 13,
139 .func = 1,
140 .route_offset = 0x50,
141 .route_val = BIT(16 + 11) | BIT(11),
142 },
143};
144
David Wu3dd7d6c2019-04-16 21:50:55 +0800145static int rk3228_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
146{
147 struct rockchip_pinctrl_priv *priv = bank->priv;
148 int iomux_num = (pin / 8);
149 struct regmap *regmap;
150 int reg, ret, mask, mux_type;
151 u8 bit;
Jagan Teki9e0e6812022-12-14 23:20:56 +0530152 u32 data;
David Wu3dd7d6c2019-04-16 21:50:55 +0800153
154 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
155 ? priv->regmap_pmu : priv->regmap_base;
156
157 /* get basic quadrupel of mux registers and the correct reg inside */
158 mux_type = bank->iomux[iomux_num].type;
159 reg = bank->iomux[iomux_num].offset;
160 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
161
David Wu3dd7d6c2019-04-16 21:50:55 +0800162 data = (mask << (bit + 16));
163 data |= (mux & mask) << bit;
164 ret = regmap_write(regmap, reg, data);
165
166 return ret;
167}
168
David Wu5f596ae2019-01-02 21:00:55 +0800169#define RK3228_PULL_OFFSET 0x100
170
171static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
172 int pin_num, struct regmap **regmap,
173 int *reg, u8 *bit)
174{
175 struct rockchip_pinctrl_priv *priv = bank->priv;
176
177 *regmap = priv->regmap_base;
178 *reg = RK3228_PULL_OFFSET;
179 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
180 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
181
182 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
183 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
184}
185
David Wu2972c452019-04-16 21:57:05 +0800186static int rk3228_set_pull(struct rockchip_pin_bank *bank,
187 int pin_num, int pull)
188{
189 struct regmap *regmap;
190 int reg, ret;
191 u8 bit, type;
192 u32 data;
193
194 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
195 return -ENOTSUPP;
196
197 rk3228_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
198 type = bank->pull_type[pin_num / 8];
199 ret = rockchip_translate_pull_value(type, pull);
200 if (ret < 0) {
201 debug("unsupported pull setting %d\n", pull);
202 return ret;
203 }
204
205 /* enable the write to the equivalent lower bits */
206 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
207 data |= (ret << bit);
208 ret = regmap_write(regmap, reg, data);
209
210 return ret;
211}
212
David Wu5f596ae2019-01-02 21:00:55 +0800213#define RK3228_DRV_GRF_OFFSET 0x200
214
215static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
216 int pin_num, struct regmap **regmap,
217 int *reg, u8 *bit)
218{
219 struct rockchip_pinctrl_priv *priv = bank->priv;
220
221 *regmap = priv->regmap_base;
222 *reg = RK3228_DRV_GRF_OFFSET;
223 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
224 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
225
226 *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG);
227 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
228}
229
David Wu40a55482019-04-16 21:55:26 +0800230static int rk3228_set_drive(struct rockchip_pin_bank *bank,
231 int pin_num, int strength)
232{
233 struct regmap *regmap;
234 int reg, ret;
235 u32 data;
236 u8 bit;
237 int type = bank->drv[pin_num / 8].drv_type;
238
239 rk3228_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
240 ret = rockchip_translate_drive_value(type, strength);
241 if (ret < 0) {
242 debug("unsupported driver strength %d\n", strength);
243 return ret;
244 }
245
246 /* enable the write to the equivalent lower bits */
247 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
248 data |= (ret << bit);
249 ret = regmap_write(regmap, reg, data);
250 return ret;
251}
252
David Wu5f596ae2019-01-02 21:00:55 +0800253static struct rockchip_pin_bank rk3228_pin_banks[] = {
254 PIN_BANK(0, 32, "gpio0"),
255 PIN_BANK(1, 32, "gpio1"),
256 PIN_BANK(2, 32, "gpio2"),
257 PIN_BANK(3, 32, "gpio3"),
258};
259
260static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
David Wu71aede02019-04-16 21:50:54 +0800261 .pin_banks = rk3228_pin_banks,
262 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
David Wu71aede02019-04-16 21:50:54 +0800263 .grf_mux_offset = 0x0,
264 .iomux_routes = rk3228_mux_route_data,
265 .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data),
David Wu3dd7d6c2019-04-16 21:50:55 +0800266 .set_mux = rk3228_set_mux,
David Wu2972c452019-04-16 21:57:05 +0800267 .set_pull = rk3228_set_pull,
David Wu40a55482019-04-16 21:55:26 +0800268 .set_drive = rk3228_set_drive,
David Wu5f596ae2019-01-02 21:00:55 +0800269};
270
271static const struct udevice_id rk3228_pinctrl_ids[] = {
272 {
273 .compatible = "rockchip,rk3228-pinctrl",
274 .data = (ulong)&rk3228_pin_ctrl
275 },
276 { }
277};
278
279U_BOOT_DRIVER(pinctrl_rk3228) = {
280 .name = "rockchip_rk3228_pinctrl",
281 .id = UCLASS_PINCTRL,
282 .of_match = rk3228_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700283 .priv_auto = sizeof(struct rockchip_pinctrl_priv),
David Wu5f596ae2019-01-02 21:00:55 +0800284 .ops = &rockchip_pinctrl_ops,
Simon Glass92882652021-08-07 07:24:04 -0600285#if CONFIG_IS_ENABLED(OF_REAL)
David Wu5f596ae2019-01-02 21:00:55 +0800286 .bind = dm_scan_fdt_dev,
287#endif
288 .probe = rockchip_pinctrl_probe,
289};