blob: 9192aa39496c3c908882e4acf5c933ea8ebc7a29 [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>
8#include <dm/pinctrl.h>
9#include <regmap.h>
David Wu5f596ae2019-01-02 21:00:55 +080010
11#include "pinctrl-rockchip.h"
12
13static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
14 {
15 /* edphdmi_cecinoutt1 */
16 .bank_num = 7,
17 .pin = 16,
18 .func = 2,
19 .route_offset = 0x264,
20 .route_val = BIT(16 + 12) | BIT(12),
21 }, {
22 /* edphdmi_cecinout */
23 .bank_num = 7,
24 .pin = 23,
25 .func = 4,
26 .route_offset = 0x264,
27 .route_val = BIT(16 + 12),
28 },
29};
30
David Wu3dd7d6c2019-04-16 21:50:55 +080031static int rk3288_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
32{
33 struct rockchip_pinctrl_priv *priv = bank->priv;
34 int iomux_num = (pin / 8);
35 struct regmap *regmap;
36 int reg, ret, mask, mux_type;
37 u8 bit;
38 u32 data, route_reg, route_val;
39
40 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
41 ? priv->regmap_pmu : priv->regmap_base;
42
43 /* get basic quadrupel of mux registers and the correct reg inside */
44 mux_type = bank->iomux[iomux_num].type;
45 reg = bank->iomux[iomux_num].offset;
46 reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask);
47
48 if (bank->route_mask & BIT(pin)) {
49 if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
50 &route_val)) {
51 ret = regmap_write(regmap, route_reg, route_val);
52 if (ret)
53 return ret;
54 }
55 }
56
David Wu84248ec2019-04-16 21:50:56 +080057 /* bank0 is special, there are no higher 16 bit writing bits. */
58 if (bank->bank_num == 0) {
59 regmap_read(regmap, reg, &data);
60 data &= ~(mask << bit);
61 } else {
62 /* enable the write to the equivalent lower bits */
63 data = (mask << (bit + 16));
64 }
65
David Wu3dd7d6c2019-04-16 21:50:55 +080066 data |= (mux & mask) << bit;
67 ret = regmap_write(regmap, reg, data);
68
69 return ret;
70}
71
David Wu5f596ae2019-01-02 21:00:55 +080072#define RK3288_PULL_OFFSET 0x140
73#define RK3288_PULL_PMU_OFFSET 0x64
74
75static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
76 int pin_num, struct regmap **regmap,
77 int *reg, u8 *bit)
78{
79 struct rockchip_pinctrl_priv *priv = bank->priv;
80
81 /* The first 24 pins of the first bank are located in PMU */
82 if (bank->bank_num == 0) {
83 *regmap = priv->regmap_pmu;
84 *reg = RK3288_PULL_PMU_OFFSET;
David Wu5f596ae2019-01-02 21:00:55 +080085 } else {
86 *regmap = priv->regmap_base;
87 *reg = RK3288_PULL_OFFSET;
88
89 /* correct the offset, as we're starting with the 2nd bank */
90 *reg -= 0x10;
91 *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE;
David Wu2972c452019-04-16 21:57:05 +080092 }
93
94 *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4);
95
96 *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG);
97 *bit *= ROCKCHIP_PULL_BITS_PER_PIN;
98}
99
100static int rk3288_set_pull(struct rockchip_pin_bank *bank,
101 int pin_num, int pull)
102{
103 struct regmap *regmap;
104 int reg, ret;
105 u8 bit, type;
106 u32 data;
107
108 if (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT)
109 return -ENOTSUPP;
David Wu5f596ae2019-01-02 21:00:55 +0800110
David Wu2972c452019-04-16 21:57:05 +0800111 rk3288_calc_pull_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
112 type = bank->pull_type[pin_num / 8];
113 ret = rockchip_translate_pull_value(type, pull);
114 if (ret < 0) {
115 debug("unsupported pull setting %d\n", pull);
116 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800117 }
David Wu2972c452019-04-16 21:57:05 +0800118
119 /* enable the write to the equivalent lower bits */
120 data = ((1 << ROCKCHIP_PULL_BITS_PER_PIN) - 1) << (bit + 16);
121 data |= (ret << bit);
122 ret = regmap_write(regmap, reg, data);
123
124 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800125}
126
127#define RK3288_DRV_PMU_OFFSET 0x70
128#define RK3288_DRV_GRF_OFFSET 0x1c0
129
130static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
131 int pin_num, struct regmap **regmap,
132 int *reg, u8 *bit)
133{
134 struct rockchip_pinctrl_priv *priv = bank->priv;
135
136 /* The first 24 pins of the first bank are located in PMU */
137 if (bank->bank_num == 0) {
138 *regmap = priv->regmap_pmu;
139 *reg = RK3288_DRV_PMU_OFFSET;
David Wu5f596ae2019-01-02 21:00:55 +0800140 } else {
141 *regmap = priv->regmap_base;
142 *reg = RK3288_DRV_GRF_OFFSET;
143
144 /* correct the offset, as we're starting with the 2nd bank */
145 *reg -= 0x10;
146 *reg += bank->bank_num * ROCKCHIP_DRV_BANK_STRIDE;
David Wu40a55482019-04-16 21:55:26 +0800147 }
148
149 *reg += ((pin_num / ROCKCHIP_DRV_PINS_PER_REG) * 4);
150 *bit = (pin_num % ROCKCHIP_DRV_PINS_PER_REG);
151 *bit *= ROCKCHIP_DRV_BITS_PER_PIN;
152}
153
154static int rk3288_set_drive(struct rockchip_pin_bank *bank,
155 int pin_num, int strength)
156{
157 struct regmap *regmap;
158 int reg, ret;
159 u32 data;
160 u8 bit;
161 int type = bank->drv[pin_num / 8].drv_type;
David Wu5f596ae2019-01-02 21:00:55 +0800162
David Wu40a55482019-04-16 21:55:26 +0800163 rk3288_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
164 ret = rockchip_translate_drive_value(type, strength);
165 if (ret < 0) {
166 debug("unsupported driver strength %d\n", strength);
167 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800168 }
David Wu40a55482019-04-16 21:55:26 +0800169
David Wu15300472019-04-16 21:56:34 +0800170 /* bank0 is special, there are no higher 16 bit writing bits. */
171 if (bank->bank_num == 0) {
172 regmap_read(regmap, reg, &data);
173 data &= ~(((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << bit);
174 } else {
175 /* enable the write to the equivalent lower bits */
176 data = ((1 << ROCKCHIP_DRV_BITS_PER_PIN) - 1) << (bit + 16);
177 }
178
David Wu40a55482019-04-16 21:55:26 +0800179 data |= (ret << bit);
180 ret = regmap_write(regmap, reg, data);
181 return ret;
David Wu5f596ae2019-01-02 21:00:55 +0800182}
183
184static struct rockchip_pin_bank rk3288_pin_banks[] = {
Kever Yang56573c42019-05-07 09:36:32 +0800185 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
186 IOMUX_SOURCE_PMU,
187 IOMUX_SOURCE_PMU,
188 IOMUX_UNROUTED
David Wu5f596ae2019-01-02 21:00:55 +0800189 ),
190 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
191 IOMUX_UNROUTED,
192 IOMUX_UNROUTED,
193 0
194 ),
195 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
196 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
197 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
198 IOMUX_WIDTH_4BIT,
199 0,
200 0
201 ),
202 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
203 0,
204 0,
205 IOMUX_UNROUTED
206 ),
207 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
208 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
209 0,
210 IOMUX_WIDTH_4BIT,
211 IOMUX_UNROUTED
212 ),
213 PIN_BANK(8, 16, "gpio8"),
214};
215
216static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
David Wu71aede02019-04-16 21:50:54 +0800217 .pin_banks = rk3288_pin_banks,
218 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
219 .label = "RK3288-GPIO",
220 .type = RK3288,
221 .grf_mux_offset = 0x0,
222 .pmu_mux_offset = 0x84,
223 .iomux_routes = rk3288_mux_route_data,
224 .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
David Wu3dd7d6c2019-04-16 21:50:55 +0800225 .set_mux = rk3288_set_mux,
David Wu2972c452019-04-16 21:57:05 +0800226 .set_pull = rk3288_set_pull,
David Wu40a55482019-04-16 21:55:26 +0800227 .set_drive = rk3288_set_drive,
David Wu5f596ae2019-01-02 21:00:55 +0800228};
229
230static const struct udevice_id rk3288_pinctrl_ids[] = {
231 {
232 .compatible = "rockchip,rk3288-pinctrl",
233 .data = (ulong)&rk3288_pin_ctrl
234 },
235 { }
236};
237
238U_BOOT_DRIVER(pinctrl_rk3288) = {
239 .name = "rockchip_rk3288_pinctrl",
240 .id = UCLASS_PINCTRL,
241 .of_match = rk3288_pinctrl_ids,
242 .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv),
243 .ops = &rockchip_pinctrl_ops,
244#if !CONFIG_IS_ENABLED(OF_PLATDATA)
245 .bind = dm_scan_fdt_dev,
246#endif
247 .probe = rockchip_pinctrl_probe,
248};