2 * Pinctrl driver for Rockchip SoCs
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/clock.h>
15 #include <asm/arch/grf_rk3288.h>
16 #include <asm/arch/hardware.h>
17 #include <asm/arch/periph.h>
18 #include <asm/arch/pmu_rk3288.h>
19 #include <dm/pinctrl.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 struct rk3288_pinctrl_priv {
25 struct rk3288_grf *grf;
26 struct rk3288_pmu *pmu;
31 * Encode variants of iomux registers into a type variable
33 #define IOMUX_GPIO_ONLY BIT(0)
34 #define IOMUX_WIDTH_4BIT BIT(1)
35 #define IOMUX_SOURCE_PMU BIT(2)
36 #define IOMUX_UNROUTED BIT(3)
39 * @type: iomux variant using IOMUX_* constants
40 * @offset: if initialized to -1 it will be autocalculated, by specifying
41 * an initial offset value the relevant source offset can be reset
42 * to a new value for autocalculating the following iomux registers.
44 struct rockchip_iomux {
50 * @reg: register offset of the gpio bank
51 * @nr_pins: number of pins in this bank
52 * @bank_num: number of the bank, to account for holes
53 * @name: name of the bank
54 * @iomux: array describing the 4 iomux sources of the bank
56 struct rockchip_pin_bank {
61 struct rockchip_iomux iomux[4];
64 #define PIN_BANK(id, pins, label) \
77 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
83 { .type = iom0, .offset = -1 }, \
84 { .type = iom1, .offset = -1 }, \
85 { .type = iom2, .offset = -1 }, \
86 { .type = iom3, .offset = -1 }, \
90 #ifndef CONFIG_SPL_BUILD
91 static struct rockchip_pin_bank rk3288_pin_banks[] = {
92 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
97 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
102 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
103 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
104 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
109 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
114 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
115 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
120 PIN_BANK(8, 16, "gpio8"),
124 static void pinctrl_rk3288_pwm_config(struct rk3288_grf *grf, int pwm_id)
128 rk_clrsetreg(&grf->gpio7a_iomux, GPIO7A0_MASK << GPIO7A0_SHIFT,
129 GPIO7A0_PWM_0 << GPIO7A0_SHIFT);
132 rk_clrsetreg(&grf->gpio7a_iomux, GPIO7A1_MASK << GPIO7A1_SHIFT,
133 GPIO7A1_PWM_1 << GPIO7A1_SHIFT);
136 rk_clrsetreg(&grf->gpio7a_iomux, GPIO7C6_MASK << GPIO7C6_SHIFT,
137 GPIO7C6_PWM_2 << GPIO7C6_SHIFT);
140 rk_clrsetreg(&grf->gpio7a_iomux, GPIO7C7_MASK << GPIO7C6_SHIFT,
141 GPIO7C7_PWM_3 << GPIO7C7_SHIFT);
144 debug("pwm id = %d iomux error!\n", pwm_id);
149 static void pinctrl_rk3288_i2c_config(struct rk3288_grf *grf,
150 struct rk3288_pmu *pmu, int i2c_id)
154 clrsetbits_le32(&pmu->gpio0_iomux[PMU_GPIO0_B],
155 GPIO0_B7_MASK << GPIO0_B7_SHIFT,
156 GPIO0_B7_I2C0PMU_SDA << GPIO0_B7_SHIFT);
157 clrsetbits_le32(&pmu->gpio0_iomux[PMU_GPIO0_C],
158 GPIO0_C0_MASK << GPIO0_C0_SHIFT,
159 GPIO0_C0_I2C0PMU_SCL << GPIO0_C0_SHIFT);
162 rk_clrsetreg(&grf->gpio8a_iomux,
163 GPIO8A4_MASK << GPIO8A4_SHIFT |
164 GPIO8A5_MASK << GPIO8A5_SHIFT,
165 GPIO8A4_I2C2SENSOR_SDA << GPIO8A4_SHIFT |
166 GPIO8A5_I2C2SENSOR_SCL << GPIO8A5_SHIFT);
169 rk_clrsetreg(&grf->gpio6b_iomux,
170 GPIO6B1_MASK << GPIO6B1_SHIFT |
171 GPIO6B2_MASK << GPIO6B2_SHIFT,
172 GPIO6B1_I2C1AUDIO_SDA << GPIO6B1_SHIFT |
173 GPIO6B2_I2C1AUDIO_SCL << GPIO6B2_SHIFT);
176 rk_clrsetreg(&grf->gpio2c_iomux,
177 GPIO2C1_MASK << GPIO2C1_SHIFT |
178 GPIO2C0_MASK << GPIO2C0_SHIFT,
179 GPIO2C1_I2C3CAM_SDA << GPIO2C1_SHIFT |
180 GPIO2C0_I2C3CAM_SCL << GPIO2C0_SHIFT);
183 rk_clrsetreg(&grf->gpio7cl_iomux,
184 GPIO7C1_MASK << GPIO7C1_SHIFT |
185 GPIO7C2_MASK << GPIO7C2_SHIFT,
186 GPIO7C1_I2C4TP_SDA << GPIO7C1_SHIFT |
187 GPIO7C2_I2C4TP_SCL << GPIO7C2_SHIFT);
190 rk_clrsetreg(&grf->gpio7cl_iomux,
191 GPIO7C3_MASK << GPIO7C3_SHIFT,
192 GPIO7C3_I2C5HDMI_SDA << GPIO7C3_SHIFT);
193 rk_clrsetreg(&grf->gpio7ch_iomux,
194 GPIO7C4_MASK << GPIO7C4_SHIFT,
195 GPIO7C4_I2C5HDMI_SCL << GPIO7C4_SHIFT);
198 debug("i2c id = %d iomux error!\n", i2c_id);
203 static void pinctrl_rk3288_lcdc_config(struct rk3288_grf *grf, int lcd_id)
206 case PERIPH_ID_LCDC0:
207 rk_clrsetreg(&grf->gpio1d_iomux,
208 GPIO1D3_MASK << GPIO1D0_SHIFT |
209 GPIO1D2_MASK << GPIO1D2_SHIFT |
210 GPIO1D1_MASK << GPIO1D1_SHIFT |
211 GPIO1D0_MASK << GPIO1D0_SHIFT,
212 GPIO1D3_LCDC0_DCLK << GPIO1D3_SHIFT |
213 GPIO1D2_LCDC0_DEN << GPIO1D2_SHIFT |
214 GPIO1D1_LCDC0_VSYNC << GPIO1D1_SHIFT |
215 GPIO1D0_LCDC0_HSYNC << GPIO1D0_SHIFT);
218 debug("lcdc id = %d iomux error!\n", lcd_id);
223 static int pinctrl_rk3288_spi_config(struct rk3288_grf *grf,
224 enum periph_id spi_id, int cs)
230 rk_clrsetreg(&grf->gpio5b_iomux,
231 GPIO5B5_MASK << GPIO5B5_SHIFT,
232 GPIO5B5_SPI0_CSN0 << GPIO5B5_SHIFT);
235 rk_clrsetreg(&grf->gpio5c_iomux,
236 GPIO5C0_MASK << GPIO5C0_SHIFT,
237 GPIO5C0_SPI0_CSN1 << GPIO5C0_SHIFT);
242 rk_clrsetreg(&grf->gpio5b_iomux,
243 GPIO5B7_MASK << GPIO5B7_SHIFT |
244 GPIO5B6_MASK << GPIO5B6_SHIFT |
245 GPIO5B4_MASK << GPIO5B4_SHIFT,
246 GPIO5B7_SPI0_RXD << GPIO5B7_SHIFT |
247 GPIO5B6_SPI0_TXD << GPIO5B6_SHIFT |
248 GPIO5B4_SPI0_CLK << GPIO5B4_SHIFT);
253 rk_clrsetreg(&grf->gpio7b_iomux,
254 GPIO7B6_MASK << GPIO7B6_SHIFT |
255 GPIO7B7_MASK << GPIO7B7_SHIFT |
256 GPIO7B5_MASK << GPIO7B5_SHIFT |
257 GPIO7B4_MASK << GPIO7B4_SHIFT,
258 GPIO7B6_SPI1_RXD << GPIO7B6_SHIFT |
259 GPIO7B7_SPI1_TXD << GPIO7B7_SHIFT |
260 GPIO7B5_SPI1_CSN0 << GPIO7B5_SHIFT |
261 GPIO7B4_SPI1_CLK << GPIO7B4_SHIFT);
266 rk_clrsetreg(&grf->gpio8a_iomux,
267 GPIO8A7_MASK << GPIO8A7_SHIFT,
268 GPIO8A7_SPI2_CSN0 << GPIO8A7_SHIFT);
271 rk_clrsetreg(&grf->gpio8a_iomux,
272 GPIO8A3_MASK << GPIO8A3_SHIFT,
273 GPIO8A3_SPI2_CSN1 << GPIO8A3_SHIFT);
278 rk_clrsetreg(&grf->gpio8b_iomux,
279 GPIO8B1_MASK << GPIO8B1_SHIFT |
280 GPIO8B0_MASK << GPIO8B0_SHIFT,
281 GPIO8B1_SPI2_TXD << GPIO8B1_SHIFT |
282 GPIO8B0_SPI2_RXD << GPIO8B0_SHIFT);
283 rk_clrsetreg(&grf->gpio8a_iomux,
284 GPIO8A6_MASK << GPIO8A6_SHIFT,
285 GPIO8A6_SPI2_CLK << GPIO8A6_SHIFT);
293 debug("rkspi: periph%d cs=%d not supported", spi_id, cs);
297 static void pinctrl_rk3288_uart_config(struct rk3288_grf *grf, int uart_id)
300 case PERIPH_ID_UART_BT:
301 rk_clrsetreg(&grf->gpio4c_iomux,
302 GPIO4C3_MASK << GPIO4C3_SHIFT |
303 GPIO4C2_MASK << GPIO4C2_SHIFT |
304 GPIO4C1_MASK << GPIO4C1_SHIFT |
305 GPIO4C0_MASK << GPIO4C0_SHIFT,
306 GPIO4C3_UART0BT_RTSN << GPIO4C3_SHIFT |
307 GPIO4C2_UART0BT_CTSN << GPIO4C2_SHIFT |
308 GPIO4C1_UART0BT_SOUT << GPIO4C1_SHIFT |
309 GPIO4C0_UART0BT_SIN << GPIO4C0_SHIFT);
311 case PERIPH_ID_UART_BB:
312 rk_clrsetreg(&grf->gpio5b_iomux,
313 GPIO5B3_MASK << GPIO5B3_SHIFT |
314 GPIO5B2_MASK << GPIO5B2_SHIFT |
315 GPIO5B1_MASK << GPIO5B1_SHIFT |
316 GPIO5B0_MASK << GPIO5B0_SHIFT,
317 GPIO5B3_UART1BB_RTSN << GPIO5B3_SHIFT |
318 GPIO5B2_UART1BB_CTSN << GPIO5B2_SHIFT |
319 GPIO5B1_UART1BB_SOUT << GPIO5B1_SHIFT |
320 GPIO5B0_UART1BB_SIN << GPIO5B0_SHIFT);
322 case PERIPH_ID_UART_DBG:
323 rk_clrsetreg(&grf->gpio7ch_iomux,
324 GPIO7C7_MASK << GPIO7C7_SHIFT |
325 GPIO7C6_MASK << GPIO7C6_SHIFT,
326 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
327 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
329 case PERIPH_ID_UART_GPS:
330 rk_clrsetreg(&grf->gpio7b_iomux,
331 GPIO7B2_MASK << GPIO7B2_SHIFT |
332 GPIO7B1_MASK << GPIO7B1_SHIFT |
333 GPIO7B0_MASK << GPIO7B0_SHIFT,
334 GPIO7B2_UART3GPS_RTSN << GPIO7B2_SHIFT |
335 GPIO7B1_UART3GPS_CTSN << GPIO7B1_SHIFT |
336 GPIO7B0_UART3GPS_SOUT << GPIO7B0_SHIFT);
337 rk_clrsetreg(&grf->gpio7a_iomux,
338 GPIO7A7_MASK << GPIO7A7_SHIFT,
339 GPIO7A7_UART3GPS_SIN << GPIO7A7_SHIFT);
341 case PERIPH_ID_UART_EXP:
342 rk_clrsetreg(&grf->gpio5b_iomux,
343 GPIO5B5_MASK << GPIO5B5_SHIFT |
344 GPIO5B4_MASK << GPIO5B4_SHIFT |
345 GPIO5B6_MASK << GPIO5B6_SHIFT |
346 GPIO5B7_MASK << GPIO5B7_SHIFT,
347 GPIO5B5_UART4EXP_RTSN << GPIO5B5_SHIFT |
348 GPIO5B4_UART4EXP_CTSN << GPIO5B4_SHIFT |
349 GPIO5B6_UART4EXP_SOUT << GPIO5B6_SHIFT |
350 GPIO5B7_UART4EXP_SIN << GPIO5B7_SHIFT);
353 debug("uart id = %d iomux error!\n", uart_id);
358 static void pinctrl_rk3288_sdmmc_config(struct rk3288_grf *grf, int mmc_id)
362 rk_clrsetreg(&grf->gpio3a_iomux, 0xffff,
363 GPIO3A7_EMMC_DATA7 << GPIO3A7_SHIFT |
364 GPIO3A6_EMMC_DATA6 << GPIO3A6_SHIFT |
365 GPIO3A5_EMMC_DATA5 << GPIO3A5_SHIFT |
366 GPIO3A4_EMMC_DATA4 << GPIO3A4_SHIFT |
367 GPIO3A3_EMMC_DATA3 << GPIO3A3_SHIFT |
368 GPIO3A2_EMMC_DATA2 << GPIO3A2_SHIFT |
369 GPIO3A1_EMMC_DATA1 << GPIO3A1_SHIFT |
370 GPIO3A0_EMMC_DATA0 << GPIO3A0_SHIFT);
371 rk_clrsetreg(&grf->gpio3b_iomux, GPIO3B1_MASK << GPIO3B1_SHIFT,
372 GPIO3B1_EMMC_PWREN << GPIO3B1_SHIFT);
373 rk_clrsetreg(&grf->gpio3c_iomux,
374 GPIO3C0_MASK << GPIO3C0_SHIFT,
375 GPIO3C0_EMMC_CMD << GPIO3C0_SHIFT);
377 case PERIPH_ID_SDCARD:
378 rk_clrsetreg(&grf->gpio6c_iomux, 0xffff,
379 GPIO6C6_SDMMC0_DECTN << GPIO6C6_SHIFT |
380 GPIO6C5_SDMMC0_CMD << GPIO6C5_SHIFT |
381 GPIO6C4_SDMMC0_CLKOUT << GPIO6C4_SHIFT |
382 GPIO6C3_SDMMC0_DATA3 << GPIO6C3_SHIFT |
383 GPIO6C2_SDMMC0_DATA2 << GPIO6C2_SHIFT |
384 GPIO6C1_SDMMC0_DATA1 << GPIO6C1_SHIFT |
385 GPIO6C0_SDMMC0_DATA0 << GPIO6C0_SHIFT);
387 /* use sdmmc0 io, disable JTAG function */
388 rk_clrsetreg(&grf->soc_con0, 1 << GRF_FORCE_JTAG_SHIFT, 0);
391 debug("mmc id = %d iomux error!\n", mmc_id);
396 static void pinctrl_rk3288_hdmi_config(struct rk3288_grf *grf, int hdmi_id)
400 rk_clrsetreg(&grf->gpio7cl_iomux, GPIO7C3_MASK << GPIO7C3_SHIFT,
401 GPIO7C3_EDPHDMII2C_SDA << GPIO7C3_SHIFT);
402 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C4_MASK << GPIO7C4_SHIFT,
403 GPIO7C4_EDPHDMII2C_SCL << GPIO7C4_SHIFT);
406 debug("hdmi id = %d iomux error!\n", hdmi_id);
411 static int rk3288_pinctrl_request(struct udevice *dev, int func, int flags)
413 struct rk3288_pinctrl_priv *priv = dev_get_priv(dev);
415 debug("%s: func=%x, flags=%x\n", __func__, func, flags);
422 pinctrl_rk3288_pwm_config(priv->grf, func);
430 pinctrl_rk3288_i2c_config(priv->grf, priv->pmu, func);
435 pinctrl_rk3288_spi_config(priv->grf, func, flags);
437 case PERIPH_ID_UART0:
438 case PERIPH_ID_UART1:
439 case PERIPH_ID_UART2:
440 case PERIPH_ID_UART3:
441 case PERIPH_ID_UART4:
442 pinctrl_rk3288_uart_config(priv->grf, func);
444 case PERIPH_ID_LCDC0:
445 case PERIPH_ID_LCDC1:
446 pinctrl_rk3288_lcdc_config(priv->grf, func);
448 case PERIPH_ID_SDMMC0:
449 case PERIPH_ID_SDMMC1:
450 pinctrl_rk3288_sdmmc_config(priv->grf, func);
453 pinctrl_rk3288_hdmi_config(priv->grf, func);
462 static int rk3288_pinctrl_get_periph_id(struct udevice *dev,
463 struct udevice *periph)
468 ret = fdtdec_get_int_array(gd->fdt_blob, periph->of_offset,
469 "interrupts", cell, ARRAY_SIZE(cell));
475 return PERIPH_ID_SPI0;
477 return PERIPH_ID_SPI1;
479 return PERIPH_ID_SPI2;
481 return PERIPH_ID_I2C0;
482 case 62: /* Note strange order */
483 return PERIPH_ID_I2C1;
485 return PERIPH_ID_I2C2;
487 return PERIPH_ID_I2C3;
489 return PERIPH_ID_I2C4;
491 return PERIPH_ID_I2C5;
497 static int rk3288_pinctrl_set_state_simple(struct udevice *dev,
498 struct udevice *periph)
502 func = rk3288_pinctrl_get_periph_id(dev, periph);
505 return rk3288_pinctrl_request(dev, func, 0);
508 #ifndef CONFIG_SPL_BUILD
509 static int rk3288_pinctrl_set_pins(struct udevice *dev, int banknum, int index,
510 int muxval, int flags)
512 struct rk3288_pinctrl_priv *priv = dev_get_priv(dev);
513 struct rockchip_pin_bank *bank = &rk3288_pin_banks[banknum];
514 uint shift, muxnum, ind = index;
517 debug("%s: %x %x %x %x\n", __func__, banknum, index, muxval, flags);
518 for (muxnum = 0; muxnum < 4; muxnum++) {
519 struct rockchip_iomux *mux = &bank->iomux[muxnum];
527 if (mux->type & IOMUX_SOURCE_PMU)
528 addr = priv->pmu->gpio0_iomux;
530 addr = (u32 *)priv->grf - 4;
533 if (mux->type & IOMUX_WIDTH_4BIT) {
545 debug("%s: addr=%p, mask=%x, shift=%x\n", __func__, addr,
547 rk_clrsetreg(addr, mask << shift, muxval << shift);
553 if (flags & (1 << PIN_CONFIG_BIAS_PULL_UP))
555 else if (flags & (1 << PIN_CONFIG_BIAS_PULL_DOWN))
557 shift = (index & 7) * 2;
560 addr = &priv->pmu->gpio0pull[ind];
562 addr = &priv->grf->gpio1_p[banknum - 1][ind];
563 debug("%s: addr=%p, val=%x, shift=%x\n", __func__, addr, val,
565 rk_clrsetreg(addr, 3 << shift, val << shift);
571 static int rk3288_pinctrl_set_state(struct udevice *dev, struct udevice *config)
573 const void *blob = gd->fdt_blob;
574 int pcfg_node, ret, flags, count, i;
577 debug("%s: %s %s\n", __func__, dev->name, config->name);
578 ret = fdtdec_get_int_array_count(blob, config->of_offset,
579 "rockchip,pins", cell,
582 debug("%s: bad array %d\n", __func__, ret);
586 for (i = 0, ptr = cell; i < count; i += 4, ptr += 4) {
587 pcfg_node = fdt_node_offset_by_phandle(blob, ptr[3]);
590 flags = pinctrl_decode_pin_config(blob, pcfg_node);
594 ret = rk3288_pinctrl_set_pins(dev, ptr[0], ptr[1], ptr[2],
604 static struct pinctrl_ops rk3288_pinctrl_ops = {
605 #ifndef CONFIG_SPL_BUILD
606 .set_state = rk3288_pinctrl_set_state,
608 .set_state_simple = rk3288_pinctrl_set_state_simple,
609 .request = rk3288_pinctrl_request,
610 .get_periph_id = rk3288_pinctrl_get_periph_id,
613 static int rk3288_pinctrl_bind(struct udevice *dev)
615 /* scan child GPIO banks */
616 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
619 #ifndef CONFIG_SPL_BUILD
620 static int rk3288_pinctrl_parse_tables(struct rk3288_pinctrl_priv *priv,
621 struct rockchip_pin_bank *banks,
624 struct rockchip_pin_bank *bank;
625 uint reg, muxnum, banknum;
628 for (banknum = 0; banknum < count; banknum++) {
629 bank = &banks[banknum];
631 debug("%s: bank %d, reg %x\n", __func__, banknum, reg * 4);
632 for (muxnum = 0; muxnum < 4; muxnum++) {
633 struct rockchip_iomux *mux = &bank->iomux[muxnum];
635 if (!(mux->type & IOMUX_UNROUTED))
637 if (mux->type & IOMUX_WIDTH_4BIT)
648 static int rk3288_pinctrl_probe(struct udevice *dev)
650 struct rk3288_pinctrl_priv *priv = dev_get_priv(dev);
653 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
654 priv->pmu = syscon_get_first_range(ROCKCHIP_SYSCON_PMU);
655 debug("%s: grf=%p, pmu=%p\n", __func__, priv->grf, priv->pmu);
656 #ifndef CONFIG_SPL_BUILD
657 ret = rk3288_pinctrl_parse_tables(priv, rk3288_pin_banks,
658 ARRAY_SIZE(rk3288_pin_banks));
664 static const struct udevice_id rk3288_pinctrl_ids[] = {
665 { .compatible = "rockchip,rk3288-pinctrl" },
669 U_BOOT_DRIVER(pinctrl_rk3288) = {
670 .name = "pinctrl_rk3288",
671 .id = UCLASS_PINCTRL,
672 .of_match = rk3288_pinctrl_ids,
673 .priv_auto_alloc_size = sizeof(struct rk3288_pinctrl_priv),
674 .ops = &rk3288_pinctrl_ops,
675 .bind = rk3288_pinctrl_bind,
676 .probe = rk3288_pinctrl_probe,