162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include <linux/module.h>
462306a36Sopenharmony_ci#include <linux/platform_device.h>
562306a36Sopenharmony_ci#include <linux/of.h>
662306a36Sopenharmony_ci#include "pinctrl-mtmips.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#define MT7620_GPIO_MODE_UART0_SHIFT	2
962306a36Sopenharmony_ci#define MT7620_GPIO_MODE_UART0_MASK	0x7
1062306a36Sopenharmony_ci#define MT7620_GPIO_MODE_UART0(x)	((x) << MT7620_GPIO_MODE_UART0_SHIFT)
1162306a36Sopenharmony_ci#define MT7620_GPIO_MODE_UARTF		0x0
1262306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCM_UARTF	0x1
1362306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCM_I2S	0x2
1462306a36Sopenharmony_ci#define MT7620_GPIO_MODE_I2S_UARTF	0x3
1562306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCM_GPIO	0x4
1662306a36Sopenharmony_ci#define MT7620_GPIO_MODE_GPIO_UARTF	0x5
1762306a36Sopenharmony_ci#define MT7620_GPIO_MODE_GPIO_I2S	0x6
1862306a36Sopenharmony_ci#define MT7620_GPIO_MODE_GPIO		0x7
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define MT7620_GPIO_MODE_NAND		0
2162306a36Sopenharmony_ci#define MT7620_GPIO_MODE_SD		1
2262306a36Sopenharmony_ci#define MT7620_GPIO_MODE_ND_SD_GPIO	2
2362306a36Sopenharmony_ci#define MT7620_GPIO_MODE_ND_SD_MASK	0x3
2462306a36Sopenharmony_ci#define MT7620_GPIO_MODE_ND_SD_SHIFT	18
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCIE_RST	0
2762306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCIE_REF	1
2862306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCIE_GPIO	2
2962306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCIE_MASK	0x3
3062306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PCIE_SHIFT	16
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WDT_RST	0
3362306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WDT_REF	1
3462306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WDT_GPIO	2
3562306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WDT_MASK	0x3
3662306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WDT_SHIFT	21
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define MT7620_GPIO_MODE_MDIO		0
3962306a36Sopenharmony_ci#define MT7620_GPIO_MODE_MDIO_REFCLK	1
4062306a36Sopenharmony_ci#define MT7620_GPIO_MODE_MDIO_GPIO	2
4162306a36Sopenharmony_ci#define MT7620_GPIO_MODE_MDIO_MASK	0x3
4262306a36Sopenharmony_ci#define MT7620_GPIO_MODE_MDIO_SHIFT	7
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define MT7620_GPIO_MODE_I2C		0
4562306a36Sopenharmony_ci#define MT7620_GPIO_MODE_UART1		5
4662306a36Sopenharmony_ci#define MT7620_GPIO_MODE_RGMII1		9
4762306a36Sopenharmony_ci#define MT7620_GPIO_MODE_RGMII2		10
4862306a36Sopenharmony_ci#define MT7620_GPIO_MODE_SPI		11
4962306a36Sopenharmony_ci#define MT7620_GPIO_MODE_SPI_REF_CLK	12
5062306a36Sopenharmony_ci#define MT7620_GPIO_MODE_WLED		13
5162306a36Sopenharmony_ci#define MT7620_GPIO_MODE_JTAG		15
5262306a36Sopenharmony_ci#define MT7620_GPIO_MODE_EPHY		15
5362306a36Sopenharmony_ci#define MT7620_GPIO_MODE_PA		20
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic struct mtmips_pmx_func i2c_grp[] =  { FUNC("i2c", 0, 1, 2) };
5662306a36Sopenharmony_cistatic struct mtmips_pmx_func spi_grp[] = { FUNC("spi", 0, 3, 4) };
5762306a36Sopenharmony_cistatic struct mtmips_pmx_func uartlite_grp[] = { FUNC("uartlite", 0, 15, 2) };
5862306a36Sopenharmony_cistatic struct mtmips_pmx_func mdio_grp[] = {
5962306a36Sopenharmony_ci	FUNC("mdio", MT7620_GPIO_MODE_MDIO, 22, 2),
6062306a36Sopenharmony_ci	FUNC("refclk", MT7620_GPIO_MODE_MDIO_REFCLK, 22, 2),
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_cistatic struct mtmips_pmx_func rgmii1_grp[] = { FUNC("rgmii1", 0, 24, 12) };
6362306a36Sopenharmony_cistatic struct mtmips_pmx_func refclk_grp[] = { FUNC("spi refclk", 0, 37, 3) };
6462306a36Sopenharmony_cistatic struct mtmips_pmx_func ephy_grp[] = { FUNC("ephy", 0, 40, 5) };
6562306a36Sopenharmony_cistatic struct mtmips_pmx_func rgmii2_grp[] = { FUNC("rgmii2", 0, 60, 12) };
6662306a36Sopenharmony_cistatic struct mtmips_pmx_func wled_grp[] = { FUNC("wled", 0, 72, 1) };
6762306a36Sopenharmony_cistatic struct mtmips_pmx_func pa_grp[] = { FUNC("pa", 0, 18, 4) };
6862306a36Sopenharmony_cistatic struct mtmips_pmx_func uartf_grp[] = {
6962306a36Sopenharmony_ci	FUNC("uartf", MT7620_GPIO_MODE_UARTF, 7, 8),
7062306a36Sopenharmony_ci	FUNC("pcm uartf", MT7620_GPIO_MODE_PCM_UARTF, 7, 8),
7162306a36Sopenharmony_ci	FUNC("pcm i2s", MT7620_GPIO_MODE_PCM_I2S, 7, 8),
7262306a36Sopenharmony_ci	FUNC("i2s uartf", MT7620_GPIO_MODE_I2S_UARTF, 7, 8),
7362306a36Sopenharmony_ci	FUNC("pcm gpio", MT7620_GPIO_MODE_PCM_GPIO, 11, 4),
7462306a36Sopenharmony_ci	FUNC("gpio uartf", MT7620_GPIO_MODE_GPIO_UARTF, 7, 4),
7562306a36Sopenharmony_ci	FUNC("gpio i2s", MT7620_GPIO_MODE_GPIO_I2S, 7, 4),
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_cistatic struct mtmips_pmx_func wdt_grp[] = {
7862306a36Sopenharmony_ci	FUNC("wdt rst", 0, 17, 1),
7962306a36Sopenharmony_ci	FUNC("wdt refclk", 0, 17, 1),
8062306a36Sopenharmony_ci	};
8162306a36Sopenharmony_cistatic struct mtmips_pmx_func pcie_rst_grp[] = {
8262306a36Sopenharmony_ci	FUNC("pcie rst", MT7620_GPIO_MODE_PCIE_RST, 36, 1),
8362306a36Sopenharmony_ci	FUNC("pcie refclk", MT7620_GPIO_MODE_PCIE_REF, 36, 1)
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_cistatic struct mtmips_pmx_func nd_sd_grp[] = {
8662306a36Sopenharmony_ci	FUNC("nand", MT7620_GPIO_MODE_NAND, 45, 15),
8762306a36Sopenharmony_ci	FUNC("sd", MT7620_GPIO_MODE_SD, 47, 13)
8862306a36Sopenharmony_ci};
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic struct mtmips_pmx_group mt7620a_pinmux_data[] = {
9162306a36Sopenharmony_ci	GRP("i2c", i2c_grp, 1, MT7620_GPIO_MODE_I2C),
9262306a36Sopenharmony_ci	GRP("uartf", uartf_grp, MT7620_GPIO_MODE_UART0_MASK,
9362306a36Sopenharmony_ci		MT7620_GPIO_MODE_UART0_SHIFT),
9462306a36Sopenharmony_ci	GRP("spi", spi_grp, 1, MT7620_GPIO_MODE_SPI),
9562306a36Sopenharmony_ci	GRP("uartlite", uartlite_grp, 1, MT7620_GPIO_MODE_UART1),
9662306a36Sopenharmony_ci	GRP_G("wdt", wdt_grp, MT7620_GPIO_MODE_WDT_MASK,
9762306a36Sopenharmony_ci		MT7620_GPIO_MODE_WDT_GPIO, MT7620_GPIO_MODE_WDT_SHIFT),
9862306a36Sopenharmony_ci	GRP_G("mdio", mdio_grp, MT7620_GPIO_MODE_MDIO_MASK,
9962306a36Sopenharmony_ci		MT7620_GPIO_MODE_MDIO_GPIO, MT7620_GPIO_MODE_MDIO_SHIFT),
10062306a36Sopenharmony_ci	GRP("rgmii1", rgmii1_grp, 1, MT7620_GPIO_MODE_RGMII1),
10162306a36Sopenharmony_ci	GRP("spi refclk", refclk_grp, 1, MT7620_GPIO_MODE_SPI_REF_CLK),
10262306a36Sopenharmony_ci	GRP_G("pcie", pcie_rst_grp, MT7620_GPIO_MODE_PCIE_MASK,
10362306a36Sopenharmony_ci		MT7620_GPIO_MODE_PCIE_GPIO, MT7620_GPIO_MODE_PCIE_SHIFT),
10462306a36Sopenharmony_ci	GRP_G("nd_sd", nd_sd_grp, MT7620_GPIO_MODE_ND_SD_MASK,
10562306a36Sopenharmony_ci		MT7620_GPIO_MODE_ND_SD_GPIO, MT7620_GPIO_MODE_ND_SD_SHIFT),
10662306a36Sopenharmony_ci	GRP("rgmii2", rgmii2_grp, 1, MT7620_GPIO_MODE_RGMII2),
10762306a36Sopenharmony_ci	GRP("wled", wled_grp, 1, MT7620_GPIO_MODE_WLED),
10862306a36Sopenharmony_ci	GRP("ephy", ephy_grp, 1, MT7620_GPIO_MODE_EPHY),
10962306a36Sopenharmony_ci	GRP("pa", pa_grp, 1, MT7620_GPIO_MODE_PA),
11062306a36Sopenharmony_ci	{ 0 }
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic int mt7620_pinctrl_probe(struct platform_device *pdev)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return mtmips_pinctrl_init(pdev, mt7620a_pinmux_data);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic const struct of_device_id mt7620_pinctrl_match[] = {
11962306a36Sopenharmony_ci	{ .compatible = "ralink,mt7620-pinctrl" },
12062306a36Sopenharmony_ci	{ .compatible = "ralink,rt2880-pinmux" },
12162306a36Sopenharmony_ci	{}
12262306a36Sopenharmony_ci};
12362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt7620_pinctrl_match);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic struct platform_driver mt7620_pinctrl_driver = {
12662306a36Sopenharmony_ci	.probe = mt7620_pinctrl_probe,
12762306a36Sopenharmony_ci	.driver = {
12862306a36Sopenharmony_ci		.name = "mt7620-pinctrl",
12962306a36Sopenharmony_ci		.of_match_table = mt7620_pinctrl_match,
13062306a36Sopenharmony_ci	},
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic int __init mt7620_pinctrl_init(void)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	return platform_driver_register(&mt7620_pinctrl_driver);
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_cicore_initcall_sync(mt7620_pinctrl_init);
138