rockchip: rk3328: add pinctrl driver

Add rk3328 pinctrl driver and grf/iomux structure definition.

Signed-off-by: William Zhang <william.zhang@rock-chips.com>
Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig
index 397fbe1..2eab9bd 100644
--- a/drivers/pinctrl/Kconfig
+++ b/drivers/pinctrl/Kconfig
@@ -157,6 +157,15 @@
 	  This option is to enable the AT91 pinctrl driver for AT91 PIO4
 	  controller which is available on SAMA5D2 SoC.
 
+config ROCKCHIP_RK3328_PINCTRL
+	bool "Rockchip pin control driver"
+	depends on DM
+	help
+	  Support pin multiplexing control on Rockchip rk3328 SoCs. The driver
+	  is controlled by a device tree node which contains both the GPIO
+	  definitions and pin control functions for each available multiplex
+	  function.
+
 config ROCKCHIP_RK3399_PINCTRL
 	bool "Rockchip pin control driver"
 	depends on DM
diff --git a/drivers/pinctrl/rockchip/Makefile b/drivers/pinctrl/rockchip/Makefile
index cc99aee..b0b698a 100644
--- a/drivers/pinctrl/rockchip/Makefile
+++ b/drivers/pinctrl/rockchip/Makefile
@@ -8,4 +8,5 @@
 obj-$(CONFIG_ROCKCHIP_RK3036_PINCTRL) += pinctrl_rk3036.o
 obj-$(CONFIG_ROCKCHIP_RK3188_PINCTRL) += pinctrl_rk3188.o
 obj-$(CONFIG_ROCKCHIP_RK3288_PINCTRL) += pinctrl_rk3288.o
+obj-$(CONFIG_ROCKCHIP_RK3328_PINCTRL) += pinctrl_rk3328.o
 obj-$(CONFIG_ROCKCHIP_RK3399_PINCTRL) += pinctrl_rk3399.o
diff --git a/drivers/pinctrl/rockchip/pinctrl_rk3328.c b/drivers/pinctrl/rockchip/pinctrl_rk3328.c
new file mode 100644
index 0000000..5ca6782
--- /dev/null
+++ b/drivers/pinctrl/rockchip/pinctrl_rk3328.c
@@ -0,0 +1,419 @@
+/*
+ * (C) Copyright 2016 Rockchip Electronics Co., Ltd
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <syscon.h>
+#include <asm/arch/clock.h>
+#include <asm/arch/hardware.h>
+#include <asm/arch/grf_rk3328.h>
+#include <asm/arch/periph.h>
+#include <asm/io.h>
+#include <dm/pinctrl.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+struct rk3328_pinctrl_priv {
+	struct rk3328_grf_regs *grf;
+};
+
+enum {
+	/* GRF_GPIO0A_IOMUX */
+	GRF_GPIO0A5_SEL_SHIFT	= 10,
+	GRF_GPIO0A5_SEL_MASK	= 3 << GRF_GPIO0A5_SEL_SHIFT,
+	GRF_I2C3_SCL		= 2,
+
+	GRF_GPIO0A6_SEL_SHIFT	= 12,
+	GRF_GPIO0A6_SEL_MASK	= 3 << GRF_GPIO0A6_SEL_SHIFT,
+	GRF_I2C3_SDA		= 2,
+
+	GRF_GPIO0A7_SEL_SHIFT	= 14,
+	GRF_GPIO0A7_SEL_MASK	= 3 << GRF_GPIO0A7_SEL_SHIFT,
+	GRF_EMMC_DATA0		= 2,
+
+	/* GRF_GPIO1A_IOMUX */
+	GRF_GPIO1A0_SEL_SHIFT	= 0,
+	GRF_GPIO1A0_SEL_MASK	= 0x3fff << GRF_GPIO1A0_SEL_SHIFT,
+	GRF_CARD_DATA_CLK_CMD_DETN	= 0x1555,
+
+	/* GRF_GPIO2A_IOMUX */
+	GRF_GPIO2A0_SEL_SHIFT	= 0,
+	GRF_GPIO2A0_SEL_MASK	= 3 << GRF_GPIO2A0_SEL_SHIFT,
+	GRF_UART2_TX_M1		= 1,
+
+	GRF_GPIO2A1_SEL_SHIFT	= 2,
+	GRF_GPIO2A1_SEL_MASK	= 3 << GRF_GPIO2A1_SEL_SHIFT,
+	GRF_UART2_RX_M1		= 1,
+
+	GRF_GPIO2A2_SEL_SHIFT	= 4,
+	GRF_GPIO2A2_SEL_MASK	= 3 << GRF_GPIO2A2_SEL_SHIFT,
+	GRF_PWM_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
+	GRF_GPIO2D0_SEL_SHIFT	= 0,
+	GRF_GPIO2D0_SEL_MASK	= 3 << GRF_GPIO2D0_SEL_SHIFT,
+	GRF_I2C0_SCL		= 1,
+
+	GRF_GPIO2D1_SEL_SHIFT	= 2,
+	GRF_GPIO2D1_SEL_MASK	= 3 << GRF_GPIO2D1_SEL_SHIFT,
+	GRF_I2C0_SDA		= 1,
+
+	GRF_GPIO2D4_SEL_SHIFT	= 8,
+	GRF_GPIO2D4_SEL_MASK	= 0xff << GRF_GPIO2D4_SEL_SHIFT,
+	GRF_EMMC_DATA123	= 0xaa,
+
+	/* GRF_GPIO3C_IOMUX */
+	GRF_GPIO3C0_SEL_SHIFT	= 0,
+	GRF_GPIO3C0_SEL_MASK	= 0x3fff << GRF_GPIO3C0_SEL_SHIFT,
+	GRF_EMMC_DATA567_PWR_CLK_RSTN_CMD	= 0x2aaa,
+
+	/* GRF_COM_IOMUX */
+	GRF_UART2_IOMUX_SEL_SHIFT	= 0,
+	GRF_UART2_IOMUX_SEL_MASK	= 3 << GRF_UART2_IOMUX_SEL_SHIFT,
+	GRF_UART2_IOMUX_SEL_M0		= 0,
+	GRF_UART2_IOMUX_SEL_M1,
+
+	GRF_SPI_IOMUX_SEL_SHIFT = 4,
+	GRF_SPI_IOMUX_SEL_MASK	= 3 << GRF_SPI_IOMUX_SEL_SHIFT,
+	GRF_SPI_IOMUX_SEL_M0	= 0,
+	GRF_SPI_IOMUX_SEL_M1,
+	GRF_SPI_IOMUX_SEL_M2,
+
+	GRF_CARD_IOMUX_SEL_SHIFT	= 7,
+	GRF_CARD_IOMUX_SEL_MASK		= 1 << GRF_CARD_IOMUX_SEL_SHIFT,
+	GRF_CARD_IOMUX_SEL_M0		= 0,
+	GRF_CARD_IOMUX_SEL_M1,
+};
+
+static void pinctrl_rk3328_pwm_config(struct rk3328_grf_regs *grf, int pwm_id)
+{
+	switch (pwm_id) {
+	case PERIPH_ID_PWM0:
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A4_SEL_MASK,
+			     GRF_PWM_0 << GRF_GPIO2A4_SEL_SHIFT);
+		break;
+	case PERIPH_ID_PWM1:
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A5_SEL_MASK,
+			     GRF_PWM_1 << GRF_GPIO2A5_SEL_SHIFT);
+		break;
+	case PERIPH_ID_PWM2:
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A6_SEL_MASK,
+			     GRF_PWM_2 << GRF_GPIO2A6_SEL_SHIFT);
+		break;
+	case PERIPH_ID_PWM3:
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A2_SEL_MASK,
+			     GRF_PWM_IR << GRF_GPIO2A2_SEL_SHIFT);
+		break;
+	default:
+		debug("pwm id = %d iomux error!\n", pwm_id);
+		break;
+	}
+}
+
+static void pinctrl_rk3328_i2c_config(struct rk3328_grf_regs *grf, int i2c_id)
+{
+	switch (i2c_id) {
+	case PERIPH_ID_I2C0:
+		rk_clrsetreg(&grf->gpio2d_iomux,
+			     GRF_GPIO2D0_SEL_MASK | GRF_GPIO2D1_SEL_MASK,
+			     GRF_I2C0_SCL << GRF_GPIO2D0_SEL_SHIFT
+			     | GRF_I2C0_SDA << GRF_GPIO2D1_SEL_SHIFT);
+		break;
+	case PERIPH_ID_I2C1:
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A4_SEL_MASK | GRF_GPIO2A5_SEL_MASK,
+			     GRF_I2C1_SCL << GRF_GPIO2A5_SEL_SHIFT
+			     | GRF_I2C1_SDA << GRF_GPIO2A4_SEL_SHIFT);
+		break;
+	case PERIPH_ID_I2C2:
+		rk_clrsetreg(&grf->gpio2bl_iomux,
+			     GRF_GPIO2BL5_SEL_MASK | GRF_GPIO2BL6_SEL_MASK,
+			     GRF_I2C2_SCL << GRF_GPIO2BL6_SEL_SHIFT
+			     | GRF_I2C2_SDA << GRF_GPIO2BL6_SEL_SHIFT);
+		break;
+	case PERIPH_ID_I2C3:
+		rk_clrsetreg(&grf->gpio0a_iomux,
+			     GRF_GPIO0A5_SEL_MASK | GRF_GPIO0A6_SEL_MASK,
+			     GRF_I2C3_SCL << GRF_GPIO0A5_SEL_SHIFT
+			     | GRF_I2C3_SDA << GRF_GPIO0A6_SEL_SHIFT);
+		break;
+	default:
+		debug("i2c id = %d iomux error!\n", i2c_id);
+		break;
+	}
+}
+
+static void pinctrl_rk3328_lcdc_config(struct rk3328_grf_regs *grf, int lcd_id)
+{
+	switch (lcd_id) {
+	case PERIPH_ID_LCDC0:
+		break;
+	default:
+		debug("lcdc id = %d iomux error!\n", lcd_id);
+		break;
+	}
+}
+
+static int pinctrl_rk3328_spi_config(struct rk3328_grf_regs *grf,
+				     enum periph_id spi_id, int cs)
+{
+	rk_clrsetreg(&grf->com_iomux,
+		     GRF_SPI_IOMUX_SEL_MASK,
+		     GRF_SPI_IOMUX_SEL_M0 << GRF_SPI_IOMUX_SEL_SHIFT);
+
+	switch (spi_id) {
+	case PERIPH_ID_SPI0:
+		switch (cs) {
+		case 0:
+			rk_clrsetreg(&grf->gpio2bl_iomux,
+				     GRF_GPIO2BL3_SEL_MASK,
+				     GRF_SPI_CSN0_M0 << GRF_GPIO2BL3_SEL_SHIFT);
+			break;
+		case 1:
+			rk_clrsetreg(&grf->gpio2bl_iomux,
+				     GRF_GPIO2BL4_SEL_MASK,
+				     GRF_SPI_CSN1_M0 << GRF_GPIO2BL4_SEL_SHIFT);
+			break;
+		default:
+			goto err;
+		}
+		rk_clrsetreg(&grf->gpio2bl_iomux,
+			     GRF_GPIO2BL0_SEL_MASK,
+			     GRF_SPI_CLK_TX_RX_M0 << GRF_GPIO2BL0_SEL_SHIFT);
+		break;
+	default:
+		goto err;
+	}
+
+	return 0;
+err:
+	debug("rkspi: periph%d cs=%d not supported", spi_id, cs);
+	return -ENOENT;
+}
+
+static void pinctrl_rk3328_uart_config(struct rk3328_grf_regs *grf, int uart_id)
+{
+	switch (uart_id) {
+	case PERIPH_ID_UART2:
+		break;
+		/* uart2 iomux select m1 */
+		rk_clrsetreg(&grf->com_iomux,
+			     GRF_UART2_IOMUX_SEL_MASK,
+			     GRF_UART2_IOMUX_SEL_M1
+			     << GRF_UART2_IOMUX_SEL_SHIFT);
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A0_SEL_MASK | GRF_GPIO2A1_SEL_MASK,
+			     GRF_UART2_TX_M1 << GRF_GPIO2A0_SEL_SHIFT |
+			     GRF_UART2_RX_M1 << GRF_GPIO2A1_SEL_SHIFT);
+		break;
+	case PERIPH_ID_UART0:
+	case PERIPH_ID_UART1:
+	case PERIPH_ID_UART3:
+	case PERIPH_ID_UART4:
+	default:
+		debug("uart id = %d iomux error!\n", uart_id);
+		break;
+	}
+}
+
+static void pinctrl_rk3328_sdmmc_config(struct rk3328_grf_regs *grf,
+					int mmc_id)
+{
+	switch (mmc_id) {
+	case PERIPH_ID_EMMC:
+		rk_clrsetreg(&grf->gpio0a_iomux,
+			     GRF_GPIO0A7_SEL_MASK,
+			     GRF_EMMC_DATA0 << GRF_GPIO0A7_SEL_SHIFT);
+		rk_clrsetreg(&grf->gpio2d_iomux,
+			     GRF_GPIO2D4_SEL_MASK,
+			     GRF_EMMC_DATA123 << GRF_GPIO2D4_SEL_SHIFT);
+		rk_clrsetreg(&grf->gpio3c_iomux,
+			     GRF_GPIO3C0_SEL_MASK,
+			     GRF_EMMC_DATA567_PWR_CLK_RSTN_CMD
+			     << GRF_GPIO3C0_SEL_SHIFT);
+		break;
+	case PERIPH_ID_SDCARD:
+		/* sdcard iomux select m0 */
+		rk_clrsetreg(&grf->com_iomux,
+			     GRF_CARD_IOMUX_SEL_MASK,
+			     GRF_CARD_IOMUX_SEL_M0 << GRF_CARD_IOMUX_SEL_SHIFT);
+		rk_clrsetreg(&grf->gpio2a_iomux,
+			     GRF_GPIO2A7_SEL_MASK,
+			     GRF_CARD_PWR_EN_M0 << GRF_GPIO2A7_SEL_SHIFT);
+		rk_clrsetreg(&grf->gpio1a_iomux,
+			     GRF_GPIO1A0_SEL_MASK,
+			     GRF_CARD_DATA_CLK_CMD_DETN
+			     << GRF_GPIO1A0_SEL_SHIFT);
+		break;
+	default:
+		debug("mmc id = %d iomux error!\n", mmc_id);
+		break;
+	}
+}
+
+static int rk3328_pinctrl_request(struct udevice *dev, int func, int flags)
+{
+	struct rk3328_pinctrl_priv *priv = dev_get_priv(dev);
+
+	debug("%s: func=%x, flags=%x\n", __func__, func, flags);
+	switch (func) {
+	case PERIPH_ID_PWM0:
+	case PERIPH_ID_PWM1:
+	case PERIPH_ID_PWM2:
+	case PERIPH_ID_PWM3:
+		pinctrl_rk3328_pwm_config(priv->grf, func);
+		break;
+	case PERIPH_ID_I2C0:
+	case PERIPH_ID_I2C1:
+	case PERIPH_ID_I2C2:
+	case PERIPH_ID_I2C3:
+		pinctrl_rk3328_i2c_config(priv->grf, func);
+		break;
+	case PERIPH_ID_SPI0:
+		pinctrl_rk3328_spi_config(priv->grf, func, flags);
+		break;
+	case PERIPH_ID_UART0:
+	case PERIPH_ID_UART1:
+	case PERIPH_ID_UART2:
+	case PERIPH_ID_UART3:
+	case PERIPH_ID_UART4:
+		pinctrl_rk3328_uart_config(priv->grf, func);
+		break;
+	case PERIPH_ID_LCDC0:
+	case PERIPH_ID_LCDC1:
+		pinctrl_rk3328_lcdc_config(priv->grf, func);
+		break;
+	case PERIPH_ID_SDMMC0:
+	case PERIPH_ID_SDMMC1:
+		pinctrl_rk3328_sdmmc_config(priv->grf, func);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int rk3328_pinctrl_get_periph_id(struct udevice *dev,
+					struct udevice *periph)
+{
+	u32 cell[3];
+	int ret;
+
+	ret = fdtdec_get_int_array(gd->fdt_blob, periph->of_offset,
+				   "interrupts", cell, ARRAY_SIZE(cell));
+	if (ret < 0)
+		return -EINVAL;
+
+	switch (cell[1]) {
+	case 49:
+		return PERIPH_ID_SPI0;
+	case 50:
+		return PERIPH_ID_PWM0;
+	case 36:
+		return PERIPH_ID_I2C0;
+	case 37: /* Note strange order */
+		return PERIPH_ID_I2C1;
+	case 38:
+		return PERIPH_ID_I2C2;
+	case 39:
+		return PERIPH_ID_I2C3;
+	case 12:
+		return PERIPH_ID_SDCARD;
+	case 14:
+		return PERIPH_ID_EMMC;
+	}
+
+	return -ENOENT;
+}
+
+static int rk3328_pinctrl_set_state_simple(struct udevice *dev,
+					   struct udevice *periph)
+{
+	int func;
+
+	func = rk3328_pinctrl_get_periph_id(dev, periph);
+	if (func < 0)
+		return func;
+
+	return rk3328_pinctrl_request(dev, func, 0);
+}
+
+static struct pinctrl_ops rk3328_pinctrl_ops = {
+	.set_state_simple	= rk3328_pinctrl_set_state_simple,
+	.request	= rk3328_pinctrl_request,
+	.get_periph_id	= rk3328_pinctrl_get_periph_id,
+};
+
+static int rk3328_pinctrl_probe(struct udevice *dev)
+{
+	struct rk3328_pinctrl_priv *priv = dev_get_priv(dev);
+	int ret = 0;
+
+	priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
+	debug("%s: grf=%p\n", __func__, priv->grf);
+
+	return ret;
+}
+
+static const struct udevice_id rk3328_pinctrl_ids[] = {
+	{ .compatible = "rockchip,rk3328-pinctrl" },
+	{ }
+};
+
+U_BOOT_DRIVER(pinctrl_rk3328) = {
+	.name		= "rockchip_rk3328_pinctrl",
+	.id		= UCLASS_PINCTRL,
+	.of_match	= rk3328_pinctrl_ids,
+	.priv_auto_alloc_size = sizeof(struct rk3328_pinctrl_priv),
+	.ops		= &rk3328_pinctrl_ops,
+	.bind		= dm_scan_fdt_dev,
+	.probe		= rk3328_pinctrl_probe,
+};