blob: 653e40b354b594989169766d054e346d3c30b007 [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
6#include <common.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
David Wu5f596ae2019-01-02 21:00:55 +08009#include <dm/pinctrl.h>
10#include <regmap.h>
11#include <syscon.h>
12
13#include "pinctrl-rockchip.h"
14
15static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = {
16 {
17 .num = 1,
18 .pin = 0,
19 .reg = 0x418,
20 .bit = 0,
21 .mask = 0x3
22 }, {
23 .num = 1,
24 .pin = 1,
25 .reg = 0x418,
26 .bit = 2,
27 .mask = 0x3
28 }, {
29 .num = 1,
30 .pin = 2,
31 .reg = 0x418,
32 .bit = 4,
33 .mask = 0x3
34 }, {
35 .num = 1,
36 .pin = 3,
37 .reg = 0x418,
38 .bit = 6,
39 .mask = 0x3
40 }, {
41 .num = 1,
42 .pin = 4,
43 .reg = 0x418,
44 .bit = 8,
45 .mask = 0x3
46 }, {
47 .num = 1,
48 .pin = 5,
49 .reg = 0x418,
50 .bit = 10,
51 .mask = 0x3
52 }, {
53 .num = 1,
54 .pin = 6,
55 .reg = 0x418,
56 .bit = 12,
57 .mask = 0x3
58 }, {
59 .num = 1,
60 .pin = 7,
61 .reg = 0x418,
62 .bit = 14,
63 .mask = 0x3
64 }, {
65 .num = 1,
66 .pin = 8,
67 .reg = 0x41c,
68 .bit = 0,
69 .mask = 0x3
70 }, {
71 .num = 1,
72 .pin = 9,
73 .reg = 0x41c,
74 .bit = 2,
75 .mask = 0x3
76 },
77};
78
David Wu3dd7d6c2019-04-16 21:50:55 +080079static int rv1108_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
80{
81 struct rockchip_pinctrl_priv *priv = bank->priv;
82 int iomux_num = (pin / 8);
83 struct regmap *regmap;
84 int reg, ret, mask, mux_type;
85 u8 bit;
86 u32 data;
87
88 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
89 ? priv->regmap_pmu : priv->regmap_base;
90
91 /* get basic quadrupel of mux registers and the correct reg inside */
92 mux_type = bank->iomux[iomux_num].type;
93 reg = bank->iomux[iomux_num].offset;
94 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
95
96 if (bank->recalced_mask & BIT(pin))
97 rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
98
99 data = (mask << (bit + 16));
100 data |= (mux & mask) << bit;
101 ret = regmap_write(regmap, reg, data);
102
103 return ret;
104}
105
David Wu5f596ae2019-01-02 21:00:55 +0800106#define RV1108_PULL_PMU_OFFSET 0x10
107#define RV1108_PULL_OFFSET 0x110
108
109static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
110 int pin_num, struct regmap **regmap,
111 int *reg, u8 *bit)
112{
113 struct rockchip_pinctrl_priv *priv = bank->priv;
114
115 /* The first 24 pins of the first bank are located in PMU */
116 if (bank->bank_num == 0) {
117 *regmap = priv->regmap_pmu;
118 *reg = RV1108_PULL_PMU_OFFSET;
119 } else {
120 *reg = RV1108_PULL_OFFSET;
121 *regmap = priv->regmap_base;
122 /* correct the offset, as we're starting with the 2nd bank */
123 *reg -= 0x10;
124 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
125 }
126
127 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
128 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
129 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
130}
131
David Wu2972c452019-04-16 21:57:05 +0800132static int rv1108_set_pull(struct rockchip_pin_bank *bank,
133 int pin_num, int pull)
134{
135 struct regmap *regmap;
136 int reg, ret;
137 u8 bit, type;
138 u32 data;
139
140 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
141 return -ENOTSUPP;
142
143 rv1108_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
144 type = bank->pull_type[pin_num / 8];
145 ret = rockchip_translate_pull_value(type, pull);
146 if (ret < 0) {
147 debug("unsupported pull setting %d\n", pull);
148 return ret;
149 }
150
151 /* enable the write to the equivalent lower bits */
152 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
153
154 data |= (ret << bit);
155 ret = regmap_write(regmap, reg, data);
156
157 return ret;
158}
159
David Wu5f596ae2019-01-02 21:00:55 +0800160#define RV1108_DRV_PMU_OFFSET 0x20
161#define RV1108_DRV_GRF_OFFSET 0x210
162
163static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
164 int pin_num, struct regmap **regmap,
165 int *reg, u8 *bit)
166{
167 struct rockchip_pinctrl_priv *priv = bank->priv;
168
169 /* The first 24 pins of the first bank are located in PMU */
170 if (bank->bank_num == 0) {
171 *regmap = priv->regmap_pmu;
172 *reg = RV1108_DRV_PMU_OFFSET;
173 } else {
174 *regmap = priv->regmap_base;
175 *reg = RV1108_DRV_GRF_OFFSET;
176
177 /* correct the offset, as we're starting with the 2nd bank */
178 *reg -= 0x10;
179 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
180 }
181
182 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
183 *bit = pin_num % ROCKCHIP_DRV_PINS_PER_REG;
184 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
185}
186
David Wu40a55482019-04-16 21:55:26 +0800187static int rv1108_set_drive(struct rockchip_pin_bank *bank,
188 int pin_num, int strength)
189{
190 struct regmap *regmap;
191 int reg, ret;
192 u32 data;
193 u8 bit;
194 int type = bank->drv[pin_num / 8].drv_type;
195
196 rv1108_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
197 ret = rockchip_translate_drive_value(type, strength);
198 if (ret < 0) {
199 debug("unsupported driver strength %d\n", strength);
200 return ret;
201 }
202
203 /* enable the write to the equivalent lower bits */
204 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
205
206 data |= (ret << bit);
207 ret = regmap_write(regmap, reg, data);
208 return ret;
209}
210
David Wu5f596ae2019-01-02 21:00:55 +0800211#define RV1108_SCHMITT_PMU_OFFSET 0x30
212#define RV1108_SCHMITT_GRF_OFFSET 0x388
213#define RV1108_SCHMITT_BANK_STRIDE 8
214#define RV1108_SCHMITT_PINS_PER_GRF_REG 16
215#define RV1108_SCHMITT_PINS_PER_PMU_REG 8
216
217static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
218 int pin_num,
219 struct regmap **regmap,
220 int *reg, u8 *bit)
221{
222 struct rockchip_pinctrl_priv *priv = bank->priv;
223 int pins_per_reg;
224
225 if (bank->bank_num == 0) {
226 *regmap = priv->regmap_pmu;
227 *reg = RV1108_SCHMITT_PMU_OFFSET;
228 pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG;
229 } else {
230 *regmap = priv->regmap_base;
231 *reg = RV1108_SCHMITT_GRF_OFFSET;
232 pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG;
233 *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE;
234 }
235 *reg += ((pin_num / pins_per_reg) * 4);
236 *bit = pin_num % pins_per_reg;
237
238 return 0;
239}
240
David Wu7ae4ec92019-04-16 21:58:13 +0800241static int rv1108_set_schmitt(struct rockchip_pin_bank *bank,
242 int pin_num, int enable)
243{
244 struct regmap *regmap;
245 int reg;
246 u8 bit;
247 u32 data;
248
249 rv1108_calc_schmitt_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
250 /* enable the write to the equivalent lower bits */
251 data = BIT(bit + 16) | (enable << bit);
252
253 return regmap_write(regmap, reg, data);
254}
255
David Wu5f596ae2019-01-02 21:00:55 +0800256static struct rockchip_pin_bank rv1108_pin_banks[] = {
257 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
258 IOMUX_SOURCE_PMU,
259 IOMUX_SOURCE_PMU,
260 IOMUX_SOURCE_PMU),
261 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
262 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
263 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
264};
265
266static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
267 .pin_banks = rv1108_pin_banks,
268 .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
David Wu5f596ae2019-01-02 21:00:55 +0800269 .grf_mux_offset = 0x10,
270 .pmu_mux_offset = 0x0,
271 .iomux_recalced = rv1108_mux_recalced_data,
272 .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data),
David Wu3dd7d6c2019-04-16 21:50:55 +0800273 .set_mux = rv1108_set_mux,
David Wu2972c452019-04-16 21:57:05 +0800274 .set_pull = rv1108_set_pull,
David Wu40a55482019-04-16 21:55:26 +0800275 .set_drive = rv1108_set_drive,
David Wu7ae4ec92019-04-16 21:58:13 +0800276 .set_schmitt = rv1108_set_schmitt,
David Wu5f596ae2019-01-02 21:00:55 +0800277};
278
279static const struct udevice_id rv1108_pinctrl_ids[] = {
280 {
281 .compatible = "rockchip,rv1108-pinctrl",
282 .data = (ulong)&rv1108_pin_ctrl
283 },
284 { }
285};
286
287U_BOOT_DRIVER(pinctrl_rv1108) = {
288 .name = "pinctrl_rv1108",
289 .id = UCLASS_PINCTRL,
290 .of_match = rv1108_pinctrl_ids,
291 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
292 .ops = &rockchip_pinctrl_ops,
293#if !CONFIG_IS_ENABLED(OF_PLATDATA)
294 .bind = dm_scan_fdt_dev,
295#endif
296 .probe = rockchip_pinctrl_probe,
297};