162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include <asm/mach-ralink/ralink_regs.h>
462306a36Sopenharmony_ci#include <asm/mach-ralink/rt305x.h>
562306a36Sopenharmony_ci#include <linux/module.h>
662306a36Sopenharmony_ci#include <linux/platform_device.h>
762306a36Sopenharmony_ci#include <linux/of.h>
862306a36Sopenharmony_ci#include "pinctrl-mtmips.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define RT305X_GPIO_MODE_UART0_SHIFT	2
1162306a36Sopenharmony_ci#define RT305X_GPIO_MODE_UART0_MASK	0x7
1262306a36Sopenharmony_ci#define RT305X_GPIO_MODE_UART0(x)	((x) << RT305X_GPIO_MODE_UART0_SHIFT)
1362306a36Sopenharmony_ci#define RT305X_GPIO_MODE_UARTF		0
1462306a36Sopenharmony_ci#define RT305X_GPIO_MODE_PCM_UARTF	1
1562306a36Sopenharmony_ci#define RT305X_GPIO_MODE_PCM_I2S	2
1662306a36Sopenharmony_ci#define RT305X_GPIO_MODE_I2S_UARTF	3
1762306a36Sopenharmony_ci#define RT305X_GPIO_MODE_PCM_GPIO	4
1862306a36Sopenharmony_ci#define RT305X_GPIO_MODE_GPIO_UARTF	5
1962306a36Sopenharmony_ci#define RT305X_GPIO_MODE_GPIO_I2S	6
2062306a36Sopenharmony_ci#define RT305X_GPIO_MODE_GPIO		7
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define RT305X_GPIO_MODE_I2C		0
2362306a36Sopenharmony_ci#define RT305X_GPIO_MODE_SPI		1
2462306a36Sopenharmony_ci#define RT305X_GPIO_MODE_UART1		5
2562306a36Sopenharmony_ci#define RT305X_GPIO_MODE_JTAG		6
2662306a36Sopenharmony_ci#define RT305X_GPIO_MODE_MDIO		7
2762306a36Sopenharmony_ci#define RT305X_GPIO_MODE_SDRAM		8
2862306a36Sopenharmony_ci#define RT305X_GPIO_MODE_RGMII		9
2962306a36Sopenharmony_ci#define RT5350_GPIO_MODE_PHY_LED	14
3062306a36Sopenharmony_ci#define RT5350_GPIO_MODE_SPI_CS1	21
3162306a36Sopenharmony_ci#define RT3352_GPIO_MODE_LNA		18
3262306a36Sopenharmony_ci#define RT3352_GPIO_MODE_PA		20
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic struct mtmips_pmx_func i2c_grp[] =  { FUNC("i2c", 0, 1, 2) };
3562306a36Sopenharmony_cistatic struct mtmips_pmx_func spi_grp[] = { FUNC("spi", 0, 3, 4) };
3662306a36Sopenharmony_cistatic struct mtmips_pmx_func uartf_grp[] = {
3762306a36Sopenharmony_ci	FUNC("uartf", RT305X_GPIO_MODE_UARTF, 7, 8),
3862306a36Sopenharmony_ci	FUNC("pcm uartf", RT305X_GPIO_MODE_PCM_UARTF, 7, 8),
3962306a36Sopenharmony_ci	FUNC("pcm i2s", RT305X_GPIO_MODE_PCM_I2S, 7, 8),
4062306a36Sopenharmony_ci	FUNC("i2s uartf", RT305X_GPIO_MODE_I2S_UARTF, 7, 8),
4162306a36Sopenharmony_ci	FUNC("pcm gpio", RT305X_GPIO_MODE_PCM_GPIO, 11, 4),
4262306a36Sopenharmony_ci	FUNC("gpio uartf", RT305X_GPIO_MODE_GPIO_UARTF, 7, 4),
4362306a36Sopenharmony_ci	FUNC("gpio i2s", RT305X_GPIO_MODE_GPIO_I2S, 7, 4),
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_cistatic struct mtmips_pmx_func uartlite_grp[] = { FUNC("uartlite", 0, 15, 2) };
4662306a36Sopenharmony_cistatic struct mtmips_pmx_func jtag_grp[] = { FUNC("jtag", 0, 17, 5) };
4762306a36Sopenharmony_cistatic struct mtmips_pmx_func mdio_grp[] = { FUNC("mdio", 0, 22, 2) };
4862306a36Sopenharmony_cistatic struct mtmips_pmx_func rt5350_led_grp[] = { FUNC("led", 0, 22, 5) };
4962306a36Sopenharmony_cistatic struct mtmips_pmx_func rt5350_cs1_grp[] = {
5062306a36Sopenharmony_ci	FUNC("spi_cs1", 0, 27, 1),
5162306a36Sopenharmony_ci	FUNC("wdg_cs1", 1, 27, 1),
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_cistatic struct mtmips_pmx_func sdram_grp[] = { FUNC("sdram", 0, 24, 16) };
5462306a36Sopenharmony_cistatic struct mtmips_pmx_func rt3352_rgmii_grp[] = {
5562306a36Sopenharmony_ci	FUNC("rgmii", 0, 24, 12)
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_cistatic struct mtmips_pmx_func rgmii_grp[] = { FUNC("rgmii", 0, 40, 12) };
5862306a36Sopenharmony_cistatic struct mtmips_pmx_func rt3352_lna_grp[] = { FUNC("lna", 0, 36, 2) };
5962306a36Sopenharmony_cistatic struct mtmips_pmx_func rt3352_pa_grp[] = { FUNC("pa", 0, 38, 2) };
6062306a36Sopenharmony_cistatic struct mtmips_pmx_func rt3352_led_grp[] = { FUNC("led", 0, 40, 5) };
6162306a36Sopenharmony_cistatic struct mtmips_pmx_func rt3352_cs1_grp[] = {
6262306a36Sopenharmony_ci	FUNC("spi_cs1", 0, 45, 1),
6362306a36Sopenharmony_ci	FUNC("wdg_cs1", 1, 45, 1),
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic struct mtmips_pmx_group rt3050_pinmux_data[] = {
6762306a36Sopenharmony_ci	GRP("i2c", i2c_grp, 1, RT305X_GPIO_MODE_I2C),
6862306a36Sopenharmony_ci	GRP("spi", spi_grp, 1, RT305X_GPIO_MODE_SPI),
6962306a36Sopenharmony_ci	GRP("uartf", uartf_grp, RT305X_GPIO_MODE_UART0_MASK,
7062306a36Sopenharmony_ci		RT305X_GPIO_MODE_UART0_SHIFT),
7162306a36Sopenharmony_ci	GRP("uartlite", uartlite_grp, 1, RT305X_GPIO_MODE_UART1),
7262306a36Sopenharmony_ci	GRP("jtag", jtag_grp, 1, RT305X_GPIO_MODE_JTAG),
7362306a36Sopenharmony_ci	GRP("mdio", mdio_grp, 1, RT305X_GPIO_MODE_MDIO),
7462306a36Sopenharmony_ci	GRP("rgmii", rgmii_grp, 1, RT305X_GPIO_MODE_RGMII),
7562306a36Sopenharmony_ci	GRP("sdram", sdram_grp, 1, RT305X_GPIO_MODE_SDRAM),
7662306a36Sopenharmony_ci	{ 0 }
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic struct mtmips_pmx_group rt3352_pinmux_data[] = {
8062306a36Sopenharmony_ci	GRP("i2c", i2c_grp, 1, RT305X_GPIO_MODE_I2C),
8162306a36Sopenharmony_ci	GRP("spi", spi_grp, 1, RT305X_GPIO_MODE_SPI),
8262306a36Sopenharmony_ci	GRP("uartf", uartf_grp, RT305X_GPIO_MODE_UART0_MASK,
8362306a36Sopenharmony_ci		RT305X_GPIO_MODE_UART0_SHIFT),
8462306a36Sopenharmony_ci	GRP("uartlite", uartlite_grp, 1, RT305X_GPIO_MODE_UART1),
8562306a36Sopenharmony_ci	GRP("jtag", jtag_grp, 1, RT305X_GPIO_MODE_JTAG),
8662306a36Sopenharmony_ci	GRP("mdio", mdio_grp, 1, RT305X_GPIO_MODE_MDIO),
8762306a36Sopenharmony_ci	GRP("rgmii", rt3352_rgmii_grp, 1, RT305X_GPIO_MODE_RGMII),
8862306a36Sopenharmony_ci	GRP("lna", rt3352_lna_grp, 1, RT3352_GPIO_MODE_LNA),
8962306a36Sopenharmony_ci	GRP("pa", rt3352_pa_grp, 1, RT3352_GPIO_MODE_PA),
9062306a36Sopenharmony_ci	GRP("led", rt3352_led_grp, 1, RT5350_GPIO_MODE_PHY_LED),
9162306a36Sopenharmony_ci	GRP("spi_cs1", rt3352_cs1_grp, 2, RT5350_GPIO_MODE_SPI_CS1),
9262306a36Sopenharmony_ci	{ 0 }
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic struct mtmips_pmx_group rt5350_pinmux_data[] = {
9662306a36Sopenharmony_ci	GRP("i2c", i2c_grp, 1, RT305X_GPIO_MODE_I2C),
9762306a36Sopenharmony_ci	GRP("spi", spi_grp, 1, RT305X_GPIO_MODE_SPI),
9862306a36Sopenharmony_ci	GRP("uartf", uartf_grp, RT305X_GPIO_MODE_UART0_MASK,
9962306a36Sopenharmony_ci		RT305X_GPIO_MODE_UART0_SHIFT),
10062306a36Sopenharmony_ci	GRP("uartlite", uartlite_grp, 1, RT305X_GPIO_MODE_UART1),
10162306a36Sopenharmony_ci	GRP("jtag", jtag_grp, 1, RT305X_GPIO_MODE_JTAG),
10262306a36Sopenharmony_ci	GRP("led", rt5350_led_grp, 1, RT5350_GPIO_MODE_PHY_LED),
10362306a36Sopenharmony_ci	GRP("spi_cs1", rt5350_cs1_grp, 2, RT5350_GPIO_MODE_SPI_CS1),
10462306a36Sopenharmony_ci	{ 0 }
10562306a36Sopenharmony_ci};
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic int rt305x_pinctrl_probe(struct platform_device *pdev)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	if (soc_is_rt5350())
11062306a36Sopenharmony_ci		return mtmips_pinctrl_init(pdev, rt5350_pinmux_data);
11162306a36Sopenharmony_ci	else if (soc_is_rt305x() || soc_is_rt3350())
11262306a36Sopenharmony_ci		return mtmips_pinctrl_init(pdev, rt3050_pinmux_data);
11362306a36Sopenharmony_ci	else if (soc_is_rt3352())
11462306a36Sopenharmony_ci		return mtmips_pinctrl_init(pdev, rt3352_pinmux_data);
11562306a36Sopenharmony_ci	else
11662306a36Sopenharmony_ci		return -EINVAL;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic const struct of_device_id rt305x_pinctrl_match[] = {
12062306a36Sopenharmony_ci	{ .compatible = "ralink,rt305x-pinctrl" },
12162306a36Sopenharmony_ci	{ .compatible = "ralink,rt3352-pinctrl" },
12262306a36Sopenharmony_ci	{ .compatible = "ralink,rt5350-pinctrl" },
12362306a36Sopenharmony_ci	{ .compatible = "ralink,rt2880-pinmux" },
12462306a36Sopenharmony_ci	{}
12562306a36Sopenharmony_ci};
12662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, rt305x_pinctrl_match);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic struct platform_driver rt305x_pinctrl_driver = {
12962306a36Sopenharmony_ci	.probe = rt305x_pinctrl_probe,
13062306a36Sopenharmony_ci	.driver = {
13162306a36Sopenharmony_ci		.name = "rt305x-pinctrl",
13262306a36Sopenharmony_ci		.of_match_table = rt305x_pinctrl_match,
13362306a36Sopenharmony_ci	},
13462306a36Sopenharmony_ci};
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistatic int __init rt305x_pinctrl_init(void)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	return platform_driver_register(&rt305x_pinctrl_driver);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_cicore_initcall_sync(rt305x_pinctrl_init);
141