18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Combined GPIO and pin controller support for Renesas RZ/A2 (R7S9210) SoC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Chris Brandt
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * This pin controller/gpio combined driver supports Renesas devices of RZ/A2
108c2ecf20Sopenharmony_ci * family.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/bitops.h>
148c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/of_device.h>
198c2ecf20Sopenharmony_ci#include <linux/pinctrl/pinmux.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "../core.h"
228c2ecf20Sopenharmony_ci#include "../pinmux.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define DRIVER_NAME		"pinctrl-rza2"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define RZA2_PINS_PER_PORT	8
278c2ecf20Sopenharmony_ci#define RZA2_PIN_ID_TO_PORT(id)	((id) / RZA2_PINS_PER_PORT)
288c2ecf20Sopenharmony_ci#define RZA2_PIN_ID_TO_PIN(id)	((id) % RZA2_PINS_PER_PORT)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Use 16 lower bits [15:0] for pin identifier
328c2ecf20Sopenharmony_ci * Use 16 higher bits [31:16] for pin mux function
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define MUX_PIN_ID_MASK		GENMASK(15, 0)
358c2ecf20Sopenharmony_ci#define MUX_FUNC_MASK		GENMASK(31, 16)
368c2ecf20Sopenharmony_ci#define MUX_FUNC_OFFS		16
378c2ecf20Sopenharmony_ci#define MUX_FUNC(pinconf)	((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const char port_names[] = "0123456789ABCDEFGHJKLM";
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct rza2_pinctrl_priv {
428c2ecf20Sopenharmony_ci	struct device *dev;
438c2ecf20Sopenharmony_ci	void __iomem *base;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	struct pinctrl_pin_desc *pins;
468c2ecf20Sopenharmony_ci	struct pinctrl_desc desc;
478c2ecf20Sopenharmony_ci	struct pinctrl_dev *pctl;
488c2ecf20Sopenharmony_ci	struct pinctrl_gpio_range gpio_range;
498c2ecf20Sopenharmony_ci	int npins;
508c2ecf20Sopenharmony_ci	struct mutex mutex; /* serialize adding groups and functions */
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define RZA2_PDR(port)		(0x0000 + (port) * 2)	/* Direction 16-bit */
548c2ecf20Sopenharmony_ci#define RZA2_PODR(port)		(0x0040 + (port))	/* Output Data 8-bit */
558c2ecf20Sopenharmony_ci#define RZA2_PIDR(port)		(0x0060 + (port))	/* Input Data 8-bit */
568c2ecf20Sopenharmony_ci#define RZA2_PMR(port)		(0x0080 + (port))	/* Mode 8-bit */
578c2ecf20Sopenharmony_ci#define RZA2_DSCR(port)		(0x0140 + (port) * 2)	/* Drive 16-bit */
588c2ecf20Sopenharmony_ci#define RZA2_PFS(port, pin)	(0x0200 + ((port) * 8) + (pin))	/* Fnct 8-bit */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define RZA2_PWPR		0x02ff	/* Write Protect 8-bit */
618c2ecf20Sopenharmony_ci#define RZA2_PFENET		0x0820	/* Ethernet Pins 8-bit */
628c2ecf20Sopenharmony_ci#define RZA2_PPOC		0x0900	/* Dedicated Pins 32-bit */
638c2ecf20Sopenharmony_ci#define RZA2_PHMOMO		0x0980	/* Peripheral Pins 32-bit */
648c2ecf20Sopenharmony_ci#define RZA2_PCKIO		0x09d0	/* CKIO Drive 8-bit */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define RZA2_PDR_INPUT		0x02
678c2ecf20Sopenharmony_ci#define RZA2_PDR_OUTPUT		0x03
688c2ecf20Sopenharmony_ci#define RZA2_PDR_MASK		0x03
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define PWPR_B0WI		BIT(7)	/* Bit Write Disable */
718c2ecf20Sopenharmony_ci#define PWPR_PFSWE		BIT(6)	/* PFS Register Write Enable */
728c2ecf20Sopenharmony_ci#define PFS_ISEL		BIT(6)	/* Interrupt Select */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void rza2_set_pin_function(void __iomem *pfc_base, u8 port, u8 pin,
758c2ecf20Sopenharmony_ci				  u8 func)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	u16 mask16;
788c2ecf20Sopenharmony_ci	u16 reg16;
798c2ecf20Sopenharmony_ci	u8 reg8;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Set pin to 'Non-use (Hi-z input protection)'  */
828c2ecf20Sopenharmony_ci	reg16 = readw(pfc_base + RZA2_PDR(port));
838c2ecf20Sopenharmony_ci	mask16 = RZA2_PDR_MASK << (pin * 2);
848c2ecf20Sopenharmony_ci	reg16 &= ~mask16;
858c2ecf20Sopenharmony_ci	writew(reg16, pfc_base + RZA2_PDR(port));
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* Temporarily switch to GPIO */
888c2ecf20Sopenharmony_ci	reg8 = readb(pfc_base + RZA2_PMR(port));
898c2ecf20Sopenharmony_ci	reg8 &= ~BIT(pin);
908c2ecf20Sopenharmony_ci	writeb(reg8, pfc_base + RZA2_PMR(port));
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* PFS Register Write Protect : OFF */
938c2ecf20Sopenharmony_ci	writeb(0x00, pfc_base + RZA2_PWPR);		/* B0WI=0, PFSWE=0 */
948c2ecf20Sopenharmony_ci	writeb(PWPR_PFSWE, pfc_base + RZA2_PWPR);	/* B0WI=0, PFSWE=1 */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* Set Pin function (interrupt disabled, ISEL=0) */
978c2ecf20Sopenharmony_ci	writeb(func, pfc_base + RZA2_PFS(port, pin));
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* PFS Register Write Protect : ON */
1008c2ecf20Sopenharmony_ci	writeb(0x00, pfc_base + RZA2_PWPR);	/* B0WI=0, PFSWE=0 */
1018c2ecf20Sopenharmony_ci	writeb(0x80, pfc_base + RZA2_PWPR);	/* B0WI=1, PFSWE=0 */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* Port Mode  : Peripheral module pin functions */
1048c2ecf20Sopenharmony_ci	reg8 = readb(pfc_base + RZA2_PMR(port));
1058c2ecf20Sopenharmony_ci	reg8 |= BIT(pin);
1068c2ecf20Sopenharmony_ci	writeb(reg8, pfc_base + RZA2_PMR(port));
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic void rza2_pin_to_gpio(void __iomem *pfc_base, unsigned int offset,
1108c2ecf20Sopenharmony_ci			     u8 dir)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	u8 port = RZA2_PIN_ID_TO_PORT(offset);
1138c2ecf20Sopenharmony_ci	u8 pin = RZA2_PIN_ID_TO_PIN(offset);
1148c2ecf20Sopenharmony_ci	u16 mask16;
1158c2ecf20Sopenharmony_ci	u16 reg16;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	reg16 = readw(pfc_base + RZA2_PDR(port));
1188c2ecf20Sopenharmony_ci	mask16 = RZA2_PDR_MASK << (pin * 2);
1198c2ecf20Sopenharmony_ci	reg16 &= ~mask16;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (dir)
1228c2ecf20Sopenharmony_ci		reg16 |= RZA2_PDR_INPUT << (pin * 2);	/* pin as input */
1238c2ecf20Sopenharmony_ci	else
1248c2ecf20Sopenharmony_ci		reg16 |= RZA2_PDR_OUTPUT << (pin * 2);	/* pin as output */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	writew(reg16, pfc_base + RZA2_PDR(port));
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int rza2_chip_get_direction(struct gpio_chip *chip, unsigned int offset)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
1328c2ecf20Sopenharmony_ci	u8 port = RZA2_PIN_ID_TO_PORT(offset);
1338c2ecf20Sopenharmony_ci	u8 pin = RZA2_PIN_ID_TO_PIN(offset);
1348c2ecf20Sopenharmony_ci	u16 reg16;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	reg16 = readw(priv->base + RZA2_PDR(port));
1378c2ecf20Sopenharmony_ci	reg16 = (reg16 >> (pin * 2)) & RZA2_PDR_MASK;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (reg16 == RZA2_PDR_OUTPUT)
1408c2ecf20Sopenharmony_ci		return GPIO_LINE_DIRECTION_OUT;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (reg16 == RZA2_PDR_INPUT)
1438c2ecf20Sopenharmony_ci		return GPIO_LINE_DIRECTION_IN;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * This GPIO controller has a default Hi-Z state that is not input or
1478c2ecf20Sopenharmony_ci	 * output, so force the pin to input now.
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	rza2_pin_to_gpio(priv->base, offset, 1);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return GPIO_LINE_DIRECTION_IN;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int rza2_chip_direction_input(struct gpio_chip *chip,
1558c2ecf20Sopenharmony_ci				     unsigned int offset)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	rza2_pin_to_gpio(priv->base, offset, 1);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return 0;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int rza2_chip_get(struct gpio_chip *chip, unsigned int offset)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
1678c2ecf20Sopenharmony_ci	u8 port = RZA2_PIN_ID_TO_PORT(offset);
1688c2ecf20Sopenharmony_ci	u8 pin = RZA2_PIN_ID_TO_PIN(offset);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return !!(readb(priv->base + RZA2_PIDR(port)) & BIT(pin));
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void rza2_chip_set(struct gpio_chip *chip, unsigned int offset,
1748c2ecf20Sopenharmony_ci			  int value)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
1778c2ecf20Sopenharmony_ci	u8 port = RZA2_PIN_ID_TO_PORT(offset);
1788c2ecf20Sopenharmony_ci	u8 pin = RZA2_PIN_ID_TO_PIN(offset);
1798c2ecf20Sopenharmony_ci	u8 new_value;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	new_value = readb(priv->base + RZA2_PODR(port));
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (value)
1848c2ecf20Sopenharmony_ci		new_value |= BIT(pin);
1858c2ecf20Sopenharmony_ci	else
1868c2ecf20Sopenharmony_ci		new_value &= ~BIT(pin);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	writeb(new_value, priv->base + RZA2_PODR(port));
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int rza2_chip_direction_output(struct gpio_chip *chip,
1928c2ecf20Sopenharmony_ci				      unsigned int offset, int val)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = gpiochip_get_data(chip);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	rza2_chip_set(chip, offset, val);
1978c2ecf20Sopenharmony_ci	rza2_pin_to_gpio(priv->base, offset, 0);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return 0;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const char * const rza2_gpio_names[] = {
2038c2ecf20Sopenharmony_ci	"P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
2048c2ecf20Sopenharmony_ci	"P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
2058c2ecf20Sopenharmony_ci	"P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
2068c2ecf20Sopenharmony_ci	"P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
2078c2ecf20Sopenharmony_ci	"P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
2088c2ecf20Sopenharmony_ci	"P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
2098c2ecf20Sopenharmony_ci	"P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
2108c2ecf20Sopenharmony_ci	"P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
2118c2ecf20Sopenharmony_ci	"P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
2128c2ecf20Sopenharmony_ci	"P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
2138c2ecf20Sopenharmony_ci	"PA_0", "PA_1", "PA_2", "PA_3", "PA_4", "PA_5", "PA_6", "PA_7",
2148c2ecf20Sopenharmony_ci	"PB_0", "PB_1", "PB_2", "PB_3", "PB_4", "PB_5", "PB_6", "PB_7",
2158c2ecf20Sopenharmony_ci	"PC_0", "PC_1", "PC_2", "PC_3", "PC_4", "PC_5", "PC_6", "PC_7",
2168c2ecf20Sopenharmony_ci	"PD_0", "PD_1", "PD_2", "PD_3", "PD_4", "PD_5", "PD_6", "PD_7",
2178c2ecf20Sopenharmony_ci	"PE_0", "PE_1", "PE_2", "PE_3", "PE_4", "PE_5", "PE_6", "PE_7",
2188c2ecf20Sopenharmony_ci	"PF_0", "PF_1", "PF_2", "PF_3", "PF_4", "PF_5", "PF_6", "PF_7",
2198c2ecf20Sopenharmony_ci	"PG_0", "PG_1", "PG_2", "PG_3", "PG_4", "PG_5", "PG_6", "PG_7",
2208c2ecf20Sopenharmony_ci	"PH_0", "PH_1", "PH_2", "PH_3", "PH_4", "PH_5", "PH_6", "PH_7",
2218c2ecf20Sopenharmony_ci	/* port I does not exist */
2228c2ecf20Sopenharmony_ci	"PJ_0", "PJ_1", "PJ_2", "PJ_3", "PJ_4", "PJ_5", "PJ_6", "PJ_7",
2238c2ecf20Sopenharmony_ci	"PK_0", "PK_1", "PK_2", "PK_3", "PK_4", "PK_5", "PK_6", "PK_7",
2248c2ecf20Sopenharmony_ci	"PL_0", "PL_1", "PL_2", "PL_3", "PL_4", "PL_5", "PL_6", "PL_7",
2258c2ecf20Sopenharmony_ci	"PM_0", "PM_1", "PM_2", "PM_3", "PM_4", "PM_5", "PM_6", "PM_7",
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic struct gpio_chip chip = {
2298c2ecf20Sopenharmony_ci	.names = rza2_gpio_names,
2308c2ecf20Sopenharmony_ci	.base = -1,
2318c2ecf20Sopenharmony_ci	.get_direction = rza2_chip_get_direction,
2328c2ecf20Sopenharmony_ci	.direction_input = rza2_chip_direction_input,
2338c2ecf20Sopenharmony_ci	.direction_output = rza2_chip_direction_output,
2348c2ecf20Sopenharmony_ci	.get = rza2_chip_get,
2358c2ecf20Sopenharmony_ci	.set = rza2_chip_set,
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int rza2_gpio_register(struct rza2_pinctrl_priv *priv)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct device_node *np = priv->dev->of_node;
2418c2ecf20Sopenharmony_ci	struct of_phandle_args of_args;
2428c2ecf20Sopenharmony_ci	int ret;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	chip.label = devm_kasprintf(priv->dev, GFP_KERNEL, "%pOFn", np);
2458c2ecf20Sopenharmony_ci	chip.of_node = np;
2468c2ecf20Sopenharmony_ci	chip.parent = priv->dev;
2478c2ecf20Sopenharmony_ci	chip.ngpio = priv->npins;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
2508c2ecf20Sopenharmony_ci					       &of_args);
2518c2ecf20Sopenharmony_ci	if (ret) {
2528c2ecf20Sopenharmony_ci		dev_err(priv->dev, "Unable to parse gpio-ranges\n");
2538c2ecf20Sopenharmony_ci		return ret;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if ((of_args.args[0] != 0) ||
2578c2ecf20Sopenharmony_ci	    (of_args.args[1] != 0) ||
2588c2ecf20Sopenharmony_ci	    (of_args.args[2] != priv->npins)) {
2598c2ecf20Sopenharmony_ci		dev_err(priv->dev, "gpio-ranges does not match selected SOC\n");
2608c2ecf20Sopenharmony_ci		return -EINVAL;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci	priv->gpio_range.id = 0;
2638c2ecf20Sopenharmony_ci	priv->gpio_range.pin_base = priv->gpio_range.base = 0;
2648c2ecf20Sopenharmony_ci	priv->gpio_range.npins = priv->npins;
2658c2ecf20Sopenharmony_ci	priv->gpio_range.name = chip.label;
2668c2ecf20Sopenharmony_ci	priv->gpio_range.gc = &chip;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Register our gpio chip with gpiolib */
2698c2ecf20Sopenharmony_ci	ret = devm_gpiochip_add_data(priv->dev, &chip, priv);
2708c2ecf20Sopenharmony_ci	if (ret)
2718c2ecf20Sopenharmony_ci		return ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Register pin range with pinctrl core */
2748c2ecf20Sopenharmony_ci	pinctrl_add_gpio_range(priv->pctl, &priv->gpio_range);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	dev_dbg(priv->dev, "Registered gpio controller\n");
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return 0;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int rza2_pinctrl_register(struct rza2_pinctrl_priv *priv)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct pinctrl_pin_desc *pins;
2848c2ecf20Sopenharmony_ci	unsigned int i;
2858c2ecf20Sopenharmony_ci	int ret;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	pins = devm_kcalloc(priv->dev, priv->npins, sizeof(*pins), GFP_KERNEL);
2888c2ecf20Sopenharmony_ci	if (!pins)
2898c2ecf20Sopenharmony_ci		return -ENOMEM;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	priv->pins = pins;
2928c2ecf20Sopenharmony_ci	priv->desc.pins = pins;
2938c2ecf20Sopenharmony_ci	priv->desc.npins = priv->npins;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	for (i = 0; i < priv->npins; i++) {
2968c2ecf20Sopenharmony_ci		pins[i].number = i;
2978c2ecf20Sopenharmony_ci		pins[i].name = rza2_gpio_names[i];
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ret = devm_pinctrl_register_and_init(priv->dev, &priv->desc, priv,
3018c2ecf20Sopenharmony_ci					     &priv->pctl);
3028c2ecf20Sopenharmony_ci	if (ret) {
3038c2ecf20Sopenharmony_ci		dev_err(priv->dev, "pinctrl registration failed\n");
3048c2ecf20Sopenharmony_ci		return ret;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	ret = pinctrl_enable(priv->pctl);
3088c2ecf20Sopenharmony_ci	if (ret) {
3098c2ecf20Sopenharmony_ci		dev_err(priv->dev, "pinctrl enable failed\n");
3108c2ecf20Sopenharmony_ci		return ret;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	ret = rza2_gpio_register(priv);
3148c2ecf20Sopenharmony_ci	if (ret) {
3158c2ecf20Sopenharmony_ci		dev_err(priv->dev, "GPIO registration failed\n");
3168c2ecf20Sopenharmony_ci		return ret;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return 0;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci/*
3238c2ecf20Sopenharmony_ci * For each DT node, create a single pin mapping. That pin mapping will only
3248c2ecf20Sopenharmony_ci * contain a single group of pins, and that group of pins will only have a
3258c2ecf20Sopenharmony_ci * single function that can be selected.
3268c2ecf20Sopenharmony_ci */
3278c2ecf20Sopenharmony_cistatic int rza2_dt_node_to_map(struct pinctrl_dev *pctldev,
3288c2ecf20Sopenharmony_ci			       struct device_node *np,
3298c2ecf20Sopenharmony_ci			       struct pinctrl_map **map,
3308c2ecf20Sopenharmony_ci			       unsigned int *num_maps)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
3338c2ecf20Sopenharmony_ci	unsigned int *pins, *psel_val;
3348c2ecf20Sopenharmony_ci	int i, ret, npins, gsel, fsel;
3358c2ecf20Sopenharmony_ci	struct property *of_pins;
3368c2ecf20Sopenharmony_ci	const char **pin_fn;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	/* Find out how many pins to map */
3398c2ecf20Sopenharmony_ci	of_pins = of_find_property(np, "pinmux", NULL);
3408c2ecf20Sopenharmony_ci	if (!of_pins) {
3418c2ecf20Sopenharmony_ci		dev_info(priv->dev, "Missing pinmux property\n");
3428c2ecf20Sopenharmony_ci		return -ENOENT;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci	npins = of_pins->length / sizeof(u32);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	pins = devm_kcalloc(priv->dev, npins, sizeof(*pins), GFP_KERNEL);
3478c2ecf20Sopenharmony_ci	psel_val = devm_kcalloc(priv->dev, npins, sizeof(*psel_val),
3488c2ecf20Sopenharmony_ci				GFP_KERNEL);
3498c2ecf20Sopenharmony_ci	pin_fn = devm_kzalloc(priv->dev, sizeof(*pin_fn), GFP_KERNEL);
3508c2ecf20Sopenharmony_ci	if (!pins || !psel_val || !pin_fn)
3518c2ecf20Sopenharmony_ci		return -ENOMEM;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	/* Collect pin locations and mux settings from DT properties */
3548c2ecf20Sopenharmony_ci	for (i = 0; i < npins; ++i) {
3558c2ecf20Sopenharmony_ci		u32 value;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		ret = of_property_read_u32_index(np, "pinmux", i, &value);
3588c2ecf20Sopenharmony_ci		if (ret)
3598c2ecf20Sopenharmony_ci			return ret;
3608c2ecf20Sopenharmony_ci		pins[i] = value & MUX_PIN_ID_MASK;
3618c2ecf20Sopenharmony_ci		psel_val[i] = MUX_FUNC(value);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	mutex_lock(&priv->mutex);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Register a single pin group listing all the pins we read from DT */
3678c2ecf20Sopenharmony_ci	gsel = pinctrl_generic_add_group(pctldev, np->name, pins, npins, NULL);
3688c2ecf20Sopenharmony_ci	if (gsel < 0) {
3698c2ecf20Sopenharmony_ci		ret = gsel;
3708c2ecf20Sopenharmony_ci		goto unlock;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/*
3748c2ecf20Sopenharmony_ci	 * Register a single group function where the 'data' is an array PSEL
3758c2ecf20Sopenharmony_ci	 * register values read from DT.
3768c2ecf20Sopenharmony_ci	 */
3778c2ecf20Sopenharmony_ci	pin_fn[0] = np->name;
3788c2ecf20Sopenharmony_ci	fsel = pinmux_generic_add_function(pctldev, np->name, pin_fn, 1,
3798c2ecf20Sopenharmony_ci					   psel_val);
3808c2ecf20Sopenharmony_ci	if (fsel < 0) {
3818c2ecf20Sopenharmony_ci		ret = fsel;
3828c2ecf20Sopenharmony_ci		goto remove_group;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	dev_dbg(priv->dev, "Parsed %pOF with %d pins\n", np, npins);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/* Create map where to retrieve function and mux settings from */
3888c2ecf20Sopenharmony_ci	*num_maps = 0;
3898c2ecf20Sopenharmony_ci	*map = kzalloc(sizeof(**map), GFP_KERNEL);
3908c2ecf20Sopenharmony_ci	if (!*map) {
3918c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3928c2ecf20Sopenharmony_ci		goto remove_function;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	(*map)->type = PIN_MAP_TYPE_MUX_GROUP;
3968c2ecf20Sopenharmony_ci	(*map)->data.mux.group = np->name;
3978c2ecf20Sopenharmony_ci	(*map)->data.mux.function = np->name;
3988c2ecf20Sopenharmony_ci	*num_maps = 1;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ciremove_function:
4058c2ecf20Sopenharmony_ci	pinmux_generic_remove_function(pctldev, fsel);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ciremove_group:
4088c2ecf20Sopenharmony_ci	pinctrl_generic_remove_group(pctldev, gsel);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ciunlock:
4118c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	dev_err(priv->dev, "Unable to parse DT node %s\n", np->name);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	return ret;
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic void rza2_dt_free_map(struct pinctrl_dev *pctldev,
4198c2ecf20Sopenharmony_ci			     struct pinctrl_map *map, unsigned int num_maps)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	kfree(map);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic const struct pinctrl_ops rza2_pinctrl_ops = {
4258c2ecf20Sopenharmony_ci	.get_groups_count	= pinctrl_generic_get_group_count,
4268c2ecf20Sopenharmony_ci	.get_group_name		= pinctrl_generic_get_group_name,
4278c2ecf20Sopenharmony_ci	.get_group_pins		= pinctrl_generic_get_group_pins,
4288c2ecf20Sopenharmony_ci	.dt_node_to_map		= rza2_dt_node_to_map,
4298c2ecf20Sopenharmony_ci	.dt_free_map		= rza2_dt_free_map,
4308c2ecf20Sopenharmony_ci};
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic int rza2_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
4338c2ecf20Sopenharmony_ci			unsigned int group)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
4368c2ecf20Sopenharmony_ci	struct function_desc *func;
4378c2ecf20Sopenharmony_ci	unsigned int i, *psel_val;
4388c2ecf20Sopenharmony_ci	struct group_desc *grp;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	grp = pinctrl_generic_get_group(pctldev, group);
4418c2ecf20Sopenharmony_ci	if (!grp)
4428c2ecf20Sopenharmony_ci		return -EINVAL;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	func = pinmux_generic_get_function(pctldev, selector);
4458c2ecf20Sopenharmony_ci	if (!func)
4468c2ecf20Sopenharmony_ci		return -EINVAL;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	psel_val = func->data;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	for (i = 0; i < grp->num_pins; ++i) {
4518c2ecf20Sopenharmony_ci		dev_dbg(priv->dev, "Setting P%c_%d to PSEL=%d\n",
4528c2ecf20Sopenharmony_ci			port_names[RZA2_PIN_ID_TO_PORT(grp->pins[i])],
4538c2ecf20Sopenharmony_ci			RZA2_PIN_ID_TO_PIN(grp->pins[i]),
4548c2ecf20Sopenharmony_ci			psel_val[i]);
4558c2ecf20Sopenharmony_ci		rza2_set_pin_function(
4568c2ecf20Sopenharmony_ci			priv->base,
4578c2ecf20Sopenharmony_ci			RZA2_PIN_ID_TO_PORT(grp->pins[i]),
4588c2ecf20Sopenharmony_ci			RZA2_PIN_ID_TO_PIN(grp->pins[i]),
4598c2ecf20Sopenharmony_ci			psel_val[i]);
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	return 0;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic const struct pinmux_ops rza2_pinmux_ops = {
4668c2ecf20Sopenharmony_ci	.get_functions_count	= pinmux_generic_get_function_count,
4678c2ecf20Sopenharmony_ci	.get_function_name	= pinmux_generic_get_function_name,
4688c2ecf20Sopenharmony_ci	.get_function_groups	= pinmux_generic_get_function_groups,
4698c2ecf20Sopenharmony_ci	.set_mux		= rza2_set_mux,
4708c2ecf20Sopenharmony_ci	.strict			= true,
4718c2ecf20Sopenharmony_ci};
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic int rza2_pinctrl_probe(struct platform_device *pdev)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct rza2_pinctrl_priv *priv;
4768c2ecf20Sopenharmony_ci	int ret;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
4798c2ecf20Sopenharmony_ci	if (!priv)
4808c2ecf20Sopenharmony_ci		return -ENOMEM;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	priv->dev = &pdev->dev;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	priv->base = devm_platform_ioremap_resource(pdev, 0);
4858c2ecf20Sopenharmony_ci	if (IS_ERR(priv->base))
4868c2ecf20Sopenharmony_ci		return PTR_ERR(priv->base);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	mutex_init(&priv->mutex);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	priv->npins = (int)(uintptr_t)of_device_get_match_data(&pdev->dev) *
4938c2ecf20Sopenharmony_ci		      RZA2_PINS_PER_PORT;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	priv->desc.name		= DRIVER_NAME;
4968c2ecf20Sopenharmony_ci	priv->desc.pctlops	= &rza2_pinctrl_ops;
4978c2ecf20Sopenharmony_ci	priv->desc.pmxops	= &rza2_pinmux_ops;
4988c2ecf20Sopenharmony_ci	priv->desc.owner	= THIS_MODULE;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	ret = rza2_pinctrl_register(priv);
5018c2ecf20Sopenharmony_ci	if (ret)
5028c2ecf20Sopenharmony_ci		return ret;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "Registered ports P0 - P%c\n",
5058c2ecf20Sopenharmony_ci		 port_names[priv->desc.npins / RZA2_PINS_PER_PORT - 1]);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	return 0;
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic const struct of_device_id rza2_pinctrl_of_match[] = {
5118c2ecf20Sopenharmony_ci	{ .compatible = "renesas,r7s9210-pinctrl", .data = (void *)22, },
5128c2ecf20Sopenharmony_ci	{ /* sentinel */ }
5138c2ecf20Sopenharmony_ci};
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic struct platform_driver rza2_pinctrl_driver = {
5168c2ecf20Sopenharmony_ci	.driver = {
5178c2ecf20Sopenharmony_ci		.name = DRIVER_NAME,
5188c2ecf20Sopenharmony_ci		.of_match_table = rza2_pinctrl_of_match,
5198c2ecf20Sopenharmony_ci	},
5208c2ecf20Sopenharmony_ci	.probe = rza2_pinctrl_probe,
5218c2ecf20Sopenharmony_ci};
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic int __init rza2_pinctrl_init(void)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	return platform_driver_register(&rza2_pinctrl_driver);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_cicore_initcall(rza2_pinctrl_init);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chris Brandt <chris.brandt@renesas.com>");
5308c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Pin and gpio controller driver for RZ/A2 SoC");
5318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
532