192a9b825SPhil Edworthy // SPDX-License-Identifier: GPL-2.0
292a9b825SPhil Edworthy /*
392a9b825SPhil Edworthy * Renesas RZ/V2M Pin Control and GPIO driver core
492a9b825SPhil Edworthy *
592a9b825SPhil Edworthy * Based on:
692a9b825SPhil Edworthy * Renesas RZ/G2L Pin Control and GPIO driver core
792a9b825SPhil Edworthy *
892a9b825SPhil Edworthy * Copyright (C) 2022 Renesas Electronics Corporation.
992a9b825SPhil Edworthy */
1092a9b825SPhil Edworthy
1192a9b825SPhil Edworthy #include <linux/bitfield.h>
1292a9b825SPhil Edworthy #include <linux/bitops.h>
1392a9b825SPhil Edworthy #include <linux/clk.h>
1492a9b825SPhil Edworthy #include <linux/gpio/driver.h>
1592a9b825SPhil Edworthy #include <linux/io.h>
1692a9b825SPhil Edworthy #include <linux/module.h>
17*f982b9d5SBiju Das #include <linux/mutex.h>
18060f03e9SRob Herring #include <linux/of.h>
19060f03e9SRob Herring #include <linux/platform_device.h>
202fb98ab4SAndy Shevchenko #include <linux/spinlock.h>
212fb98ab4SAndy Shevchenko
222fb98ab4SAndy Shevchenko #include <linux/pinctrl/consumer.h>
2392a9b825SPhil Edworthy #include <linux/pinctrl/pinconf-generic.h>
2492a9b825SPhil Edworthy #include <linux/pinctrl/pinconf.h>
2592a9b825SPhil Edworthy #include <linux/pinctrl/pinctrl.h>
2692a9b825SPhil Edworthy #include <linux/pinctrl/pinmux.h>
2792a9b825SPhil Edworthy
2892a9b825SPhil Edworthy #include <dt-bindings/pinctrl/rzv2m-pinctrl.h>
2992a9b825SPhil Edworthy
3092a9b825SPhil Edworthy #include "../core.h"
3192a9b825SPhil Edworthy #include "../pinconf.h"
3292a9b825SPhil Edworthy #include "../pinmux.h"
3392a9b825SPhil Edworthy
3492a9b825SPhil Edworthy #define DRV_NAME "pinctrl-rzv2m"
3592a9b825SPhil Edworthy
3692a9b825SPhil Edworthy /*
3792a9b825SPhil Edworthy * Use 16 lower bits [15:0] for pin identifier
3892a9b825SPhil Edworthy * Use 16 higher bits [31:16] for pin mux function
3992a9b825SPhil Edworthy */
4092a9b825SPhil Edworthy #define MUX_PIN_ID_MASK GENMASK(15, 0)
4192a9b825SPhil Edworthy #define MUX_FUNC_MASK GENMASK(31, 16)
4292a9b825SPhil Edworthy #define MUX_FUNC(pinconf) FIELD_GET(MUX_FUNC_MASK, (pinconf))
4392a9b825SPhil Edworthy
4492a9b825SPhil Edworthy /* PIN capabilities */
4592a9b825SPhil Edworthy #define PIN_CFG_GRP_1_8V_2 1
4692a9b825SPhil Edworthy #define PIN_CFG_GRP_1_8V_3 2
4792a9b825SPhil Edworthy #define PIN_CFG_GRP_SWIO_1 3
4892a9b825SPhil Edworthy #define PIN_CFG_GRP_SWIO_2 4
4992a9b825SPhil Edworthy #define PIN_CFG_GRP_3_3V 5
5092a9b825SPhil Edworthy #define PIN_CFG_GRP_MASK GENMASK(2, 0)
5192a9b825SPhil Edworthy #define PIN_CFG_BIAS BIT(3)
5292a9b825SPhil Edworthy #define PIN_CFG_DRV BIT(4)
5392a9b825SPhil Edworthy #define PIN_CFG_SLEW BIT(5)
5492a9b825SPhil Edworthy
5592a9b825SPhil Edworthy #define RZV2M_MPXED_PIN_FUNCS (PIN_CFG_BIAS | \
5692a9b825SPhil Edworthy PIN_CFG_DRV | \
5792a9b825SPhil Edworthy PIN_CFG_SLEW)
5892a9b825SPhil Edworthy
5992a9b825SPhil Edworthy /*
6092a9b825SPhil Edworthy * n indicates number of pins in the port, a is the register index
6192a9b825SPhil Edworthy * and f is pin configuration capabilities supported.
6292a9b825SPhil Edworthy */
6392a9b825SPhil Edworthy #define RZV2M_GPIO_PORT_PACK(n, a, f) (((n) << 24) | ((a) << 16) | (f))
6492a9b825SPhil Edworthy #define RZV2M_GPIO_PORT_GET_PINCNT(x) FIELD_GET(GENMASK(31, 24), (x))
6592a9b825SPhil Edworthy #define RZV2M_GPIO_PORT_GET_INDEX(x) FIELD_GET(GENMASK(23, 16), (x))
6692a9b825SPhil Edworthy #define RZV2M_GPIO_PORT_GET_CFGS(x) FIELD_GET(GENMASK(15, 0), (x))
6792a9b825SPhil Edworthy
6892a9b825SPhil Edworthy #define RZV2M_DEDICATED_PORT_IDX 22
6992a9b825SPhil Edworthy
7092a9b825SPhil Edworthy /*
7192a9b825SPhil Edworthy * BIT(31) indicates dedicated pin, b is the register bits (b * 16)
7292a9b825SPhil Edworthy * and f is the pin configuration capabilities supported.
7392a9b825SPhil Edworthy */
7492a9b825SPhil Edworthy #define RZV2M_SINGLE_PIN BIT(31)
7592a9b825SPhil Edworthy #define RZV2M_SINGLE_PIN_PACK(b, f) (RZV2M_SINGLE_PIN | \
7692a9b825SPhil Edworthy ((RZV2M_DEDICATED_PORT_IDX) << 24) | \
7792a9b825SPhil Edworthy ((b) << 16) | (f))
7892a9b825SPhil Edworthy #define RZV2M_SINGLE_PIN_GET_PORT(x) FIELD_GET(GENMASK(30, 24), (x))
7992a9b825SPhil Edworthy #define RZV2M_SINGLE_PIN_GET_BIT(x) FIELD_GET(GENMASK(23, 16), (x))
8092a9b825SPhil Edworthy #define RZV2M_SINGLE_PIN_GET_CFGS(x) FIELD_GET(GENMASK(15, 0), (x))
8192a9b825SPhil Edworthy
8292a9b825SPhil Edworthy #define RZV2M_PIN_ID_TO_PORT(id) ((id) / RZV2M_PINS_PER_PORT)
8392a9b825SPhil Edworthy #define RZV2M_PIN_ID_TO_PIN(id) ((id) % RZV2M_PINS_PER_PORT)
8492a9b825SPhil Edworthy
8592a9b825SPhil Edworthy #define DO(n) (0x00 + (n) * 0x40)
8692a9b825SPhil Edworthy #define OE(n) (0x04 + (n) * 0x40)
8792a9b825SPhil Edworthy #define IE(n) (0x08 + (n) * 0x40)
8892a9b825SPhil Edworthy #define PFSEL(n) (0x10 + (n) * 0x40)
8992a9b825SPhil Edworthy #define DI(n) (0x20 + (n) * 0x40)
9092a9b825SPhil Edworthy #define PUPD(n) (0x24 + (n) * 0x40)
9192a9b825SPhil Edworthy #define DRV(n) ((n) < RZV2M_DEDICATED_PORT_IDX ? (0x28 + (n) * 0x40) \
9292a9b825SPhil Edworthy : 0x590)
9392a9b825SPhil Edworthy #define SR(n) ((n) < RZV2M_DEDICATED_PORT_IDX ? (0x2c + (n) * 0x40) \
9492a9b825SPhil Edworthy : 0x594)
9592a9b825SPhil Edworthy #define DI_MSK(n) (0x30 + (n) * 0x40)
9692a9b825SPhil Edworthy #define EN_MSK(n) (0x34 + (n) * 0x40)
9792a9b825SPhil Edworthy
9892a9b825SPhil Edworthy #define PFC_MASK 0x07
9992a9b825SPhil Edworthy #define PUPD_MASK 0x03
10092a9b825SPhil Edworthy #define DRV_MASK 0x03
10192a9b825SPhil Edworthy
10292a9b825SPhil Edworthy struct rzv2m_dedicated_configs {
10392a9b825SPhil Edworthy const char *name;
10492a9b825SPhil Edworthy u32 config;
10592a9b825SPhil Edworthy };
10692a9b825SPhil Edworthy
10792a9b825SPhil Edworthy struct rzv2m_pinctrl_data {
10892a9b825SPhil Edworthy const char * const *port_pins;
10992a9b825SPhil Edworthy const u32 *port_pin_configs;
11092a9b825SPhil Edworthy const struct rzv2m_dedicated_configs *dedicated_pins;
11192a9b825SPhil Edworthy unsigned int n_port_pins;
11292a9b825SPhil Edworthy unsigned int n_dedicated_pins;
11392a9b825SPhil Edworthy };
11492a9b825SPhil Edworthy
11592a9b825SPhil Edworthy struct rzv2m_pinctrl {
11692a9b825SPhil Edworthy struct pinctrl_dev *pctl;
11792a9b825SPhil Edworthy struct pinctrl_desc desc;
11892a9b825SPhil Edworthy struct pinctrl_pin_desc *pins;
11992a9b825SPhil Edworthy
12092a9b825SPhil Edworthy const struct rzv2m_pinctrl_data *data;
12192a9b825SPhil Edworthy void __iomem *base;
12292a9b825SPhil Edworthy struct device *dev;
12392a9b825SPhil Edworthy
12492a9b825SPhil Edworthy struct gpio_chip gpio_chip;
12592a9b825SPhil Edworthy struct pinctrl_gpio_range gpio_range;
12692a9b825SPhil Edworthy
127*f982b9d5SBiju Das spinlock_t lock; /* lock read/write registers */
128*f982b9d5SBiju Das struct mutex mutex; /* serialize adding groups and functions */
12992a9b825SPhil Edworthy };
13092a9b825SPhil Edworthy
13192a9b825SPhil Edworthy static const unsigned int drv_1_8V_group2_uA[] = { 1800, 3800, 7800, 11000 };
13292a9b825SPhil Edworthy static const unsigned int drv_1_8V_group3_uA[] = { 1600, 3200, 6400, 9600 };
13392a9b825SPhil Edworthy static const unsigned int drv_SWIO_group2_3_3V_uA[] = { 9000, 11000, 13000, 18000 };
13492a9b825SPhil Edworthy static const unsigned int drv_3_3V_group_uA[] = { 2000, 4000, 8000, 12000 };
13592a9b825SPhil Edworthy
13692a9b825SPhil Edworthy /* Helper for registers that have a write enable bit in the upper word */
rzv2m_writel_we(void __iomem * addr,u8 shift,u8 value)13792a9b825SPhil Edworthy static void rzv2m_writel_we(void __iomem *addr, u8 shift, u8 value)
13892a9b825SPhil Edworthy {
13992a9b825SPhil Edworthy writel((BIT(16) | value) << shift, addr);
14092a9b825SPhil Edworthy }
14192a9b825SPhil Edworthy
rzv2m_pinctrl_set_pfc_mode(struct rzv2m_pinctrl * pctrl,u8 port,u8 pin,u8 func)14292a9b825SPhil Edworthy static void rzv2m_pinctrl_set_pfc_mode(struct rzv2m_pinctrl *pctrl,
14392a9b825SPhil Edworthy u8 port, u8 pin, u8 func)
14492a9b825SPhil Edworthy {
14592a9b825SPhil Edworthy void __iomem *addr;
14692a9b825SPhil Edworthy
14792a9b825SPhil Edworthy /* Mask input/output */
14892a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 1);
14992a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 1);
15092a9b825SPhil Edworthy
15192a9b825SPhil Edworthy /* Select the function and set the write enable bits */
15292a9b825SPhil Edworthy addr = pctrl->base + PFSEL(port) + (pin / 4) * 4;
15392a9b825SPhil Edworthy writel(((PFC_MASK << 16) | func) << ((pin % 4) * 4), addr);
15492a9b825SPhil Edworthy
15592a9b825SPhil Edworthy /* Unmask input/output */
15692a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + EN_MSK(port), pin, 0);
15792a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + DI_MSK(port), pin, 0);
15892a9b825SPhil Edworthy };
15992a9b825SPhil Edworthy
rzv2m_pinctrl_set_mux(struct pinctrl_dev * pctldev,unsigned int func_selector,unsigned int group_selector)16092a9b825SPhil Edworthy static int rzv2m_pinctrl_set_mux(struct pinctrl_dev *pctldev,
16192a9b825SPhil Edworthy unsigned int func_selector,
16292a9b825SPhil Edworthy unsigned int group_selector)
16392a9b825SPhil Edworthy {
16492a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
16592a9b825SPhil Edworthy struct function_desc *func;
16692a9b825SPhil Edworthy unsigned int i, *psel_val;
16792a9b825SPhil Edworthy struct group_desc *group;
16892a9b825SPhil Edworthy int *pins;
16992a9b825SPhil Edworthy
17092a9b825SPhil Edworthy func = pinmux_generic_get_function(pctldev, func_selector);
17192a9b825SPhil Edworthy if (!func)
17292a9b825SPhil Edworthy return -EINVAL;
17392a9b825SPhil Edworthy group = pinctrl_generic_get_group(pctldev, group_selector);
17492a9b825SPhil Edworthy if (!group)
17592a9b825SPhil Edworthy return -EINVAL;
17692a9b825SPhil Edworthy
17792a9b825SPhil Edworthy psel_val = func->data;
17892a9b825SPhil Edworthy pins = group->pins;
17992a9b825SPhil Edworthy
18092a9b825SPhil Edworthy for (i = 0; i < group->num_pins; i++) {
18192a9b825SPhil Edworthy dev_dbg(pctrl->dev, "port:%u pin: %u PSEL:%u\n",
18292a9b825SPhil Edworthy RZV2M_PIN_ID_TO_PORT(pins[i]), RZV2M_PIN_ID_TO_PIN(pins[i]),
18392a9b825SPhil Edworthy psel_val[i]);
18492a9b825SPhil Edworthy rzv2m_pinctrl_set_pfc_mode(pctrl, RZV2M_PIN_ID_TO_PORT(pins[i]),
18592a9b825SPhil Edworthy RZV2M_PIN_ID_TO_PIN(pins[i]), psel_val[i]);
18692a9b825SPhil Edworthy }
18792a9b825SPhil Edworthy
18892a9b825SPhil Edworthy return 0;
18992a9b825SPhil Edworthy };
19092a9b825SPhil Edworthy
rzv2m_map_add_config(struct pinctrl_map * map,const char * group_or_pin,enum pinctrl_map_type type,unsigned long * configs,unsigned int num_configs)19192a9b825SPhil Edworthy static int rzv2m_map_add_config(struct pinctrl_map *map,
19292a9b825SPhil Edworthy const char *group_or_pin,
19392a9b825SPhil Edworthy enum pinctrl_map_type type,
19492a9b825SPhil Edworthy unsigned long *configs,
19592a9b825SPhil Edworthy unsigned int num_configs)
19692a9b825SPhil Edworthy {
19792a9b825SPhil Edworthy unsigned long *cfgs;
19892a9b825SPhil Edworthy
19992a9b825SPhil Edworthy cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
20092a9b825SPhil Edworthy GFP_KERNEL);
20192a9b825SPhil Edworthy if (!cfgs)
20292a9b825SPhil Edworthy return -ENOMEM;
20392a9b825SPhil Edworthy
20492a9b825SPhil Edworthy map->type = type;
20592a9b825SPhil Edworthy map->data.configs.group_or_pin = group_or_pin;
20692a9b825SPhil Edworthy map->data.configs.configs = cfgs;
20792a9b825SPhil Edworthy map->data.configs.num_configs = num_configs;
20892a9b825SPhil Edworthy
20992a9b825SPhil Edworthy return 0;
21092a9b825SPhil Edworthy }
21192a9b825SPhil Edworthy
rzv2m_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct device_node * parent,struct pinctrl_map ** map,unsigned int * num_maps,unsigned int * index)21292a9b825SPhil Edworthy static int rzv2m_dt_subnode_to_map(struct pinctrl_dev *pctldev,
21392a9b825SPhil Edworthy struct device_node *np,
214f46a0b47SGeert Uytterhoeven struct device_node *parent,
21592a9b825SPhil Edworthy struct pinctrl_map **map,
21692a9b825SPhil Edworthy unsigned int *num_maps,
21792a9b825SPhil Edworthy unsigned int *index)
21892a9b825SPhil Edworthy {
21992a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
22092a9b825SPhil Edworthy struct pinctrl_map *maps = *map;
22192a9b825SPhil Edworthy unsigned int nmaps = *num_maps;
22292a9b825SPhil Edworthy unsigned long *configs = NULL;
22392a9b825SPhil Edworthy unsigned int *pins, *psel_val;
22492a9b825SPhil Edworthy unsigned int num_pinmux = 0;
22592a9b825SPhil Edworthy unsigned int idx = *index;
22692a9b825SPhil Edworthy unsigned int num_pins, i;
22792a9b825SPhil Edworthy unsigned int num_configs;
22892a9b825SPhil Edworthy struct property *pinmux;
22992a9b825SPhil Edworthy struct property *prop;
23092a9b825SPhil Edworthy int ret, gsel, fsel;
23192a9b825SPhil Edworthy const char **pin_fn;
232f46a0b47SGeert Uytterhoeven const char *name;
23392a9b825SPhil Edworthy const char *pin;
23492a9b825SPhil Edworthy
23592a9b825SPhil Edworthy pinmux = of_find_property(np, "pinmux", NULL);
23692a9b825SPhil Edworthy if (pinmux)
23792a9b825SPhil Edworthy num_pinmux = pinmux->length / sizeof(u32);
23892a9b825SPhil Edworthy
23992a9b825SPhil Edworthy ret = of_property_count_strings(np, "pins");
24092a9b825SPhil Edworthy if (ret == -EINVAL) {
24192a9b825SPhil Edworthy num_pins = 0;
24292a9b825SPhil Edworthy } else if (ret < 0) {
24392a9b825SPhil Edworthy dev_err(pctrl->dev, "Invalid pins list in DT\n");
24492a9b825SPhil Edworthy return ret;
24592a9b825SPhil Edworthy } else {
24692a9b825SPhil Edworthy num_pins = ret;
24792a9b825SPhil Edworthy }
24892a9b825SPhil Edworthy
24992a9b825SPhil Edworthy if (!num_pinmux && !num_pins)
25092a9b825SPhil Edworthy return 0;
25192a9b825SPhil Edworthy
25292a9b825SPhil Edworthy if (num_pinmux && num_pins) {
25392a9b825SPhil Edworthy dev_err(pctrl->dev,
25492a9b825SPhil Edworthy "DT node must contain either a pinmux or pins and not both\n");
25592a9b825SPhil Edworthy return -EINVAL;
25692a9b825SPhil Edworthy }
25792a9b825SPhil Edworthy
25892a9b825SPhil Edworthy ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
25992a9b825SPhil Edworthy if (ret < 0)
26092a9b825SPhil Edworthy return ret;
26192a9b825SPhil Edworthy
26292a9b825SPhil Edworthy if (num_pins && !num_configs) {
26392a9b825SPhil Edworthy dev_err(pctrl->dev, "DT node must contain a config\n");
26492a9b825SPhil Edworthy ret = -ENODEV;
26592a9b825SPhil Edworthy goto done;
26692a9b825SPhil Edworthy }
26792a9b825SPhil Edworthy
26892a9b825SPhil Edworthy if (num_pinmux)
26992a9b825SPhil Edworthy nmaps += 1;
27092a9b825SPhil Edworthy
27192a9b825SPhil Edworthy if (num_pins)
27292a9b825SPhil Edworthy nmaps += num_pins;
27392a9b825SPhil Edworthy
27492a9b825SPhil Edworthy maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
27592a9b825SPhil Edworthy if (!maps) {
27692a9b825SPhil Edworthy ret = -ENOMEM;
27792a9b825SPhil Edworthy goto done;
27892a9b825SPhil Edworthy }
27992a9b825SPhil Edworthy
28092a9b825SPhil Edworthy *map = maps;
28192a9b825SPhil Edworthy *num_maps = nmaps;
28292a9b825SPhil Edworthy if (num_pins) {
28392a9b825SPhil Edworthy of_property_for_each_string(np, "pins", prop, pin) {
28492a9b825SPhil Edworthy ret = rzv2m_map_add_config(&maps[idx], pin,
28592a9b825SPhil Edworthy PIN_MAP_TYPE_CONFIGS_PIN,
28692a9b825SPhil Edworthy configs, num_configs);
28792a9b825SPhil Edworthy if (ret < 0)
28892a9b825SPhil Edworthy goto done;
28992a9b825SPhil Edworthy
29092a9b825SPhil Edworthy idx++;
29192a9b825SPhil Edworthy }
29292a9b825SPhil Edworthy ret = 0;
29392a9b825SPhil Edworthy goto done;
29492a9b825SPhil Edworthy }
29592a9b825SPhil Edworthy
29692a9b825SPhil Edworthy pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
29792a9b825SPhil Edworthy psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
29892a9b825SPhil Edworthy GFP_KERNEL);
29992a9b825SPhil Edworthy pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
30092a9b825SPhil Edworthy if (!pins || !psel_val || !pin_fn) {
30192a9b825SPhil Edworthy ret = -ENOMEM;
30292a9b825SPhil Edworthy goto done;
30392a9b825SPhil Edworthy }
30492a9b825SPhil Edworthy
30592a9b825SPhil Edworthy /* Collect pin locations and mux settings from DT properties */
30692a9b825SPhil Edworthy for (i = 0; i < num_pinmux; ++i) {
30792a9b825SPhil Edworthy u32 value;
30892a9b825SPhil Edworthy
30992a9b825SPhil Edworthy ret = of_property_read_u32_index(np, "pinmux", i, &value);
31092a9b825SPhil Edworthy if (ret)
31192a9b825SPhil Edworthy goto done;
31292a9b825SPhil Edworthy pins[i] = value & MUX_PIN_ID_MASK;
31392a9b825SPhil Edworthy psel_val[i] = MUX_FUNC(value);
31492a9b825SPhil Edworthy }
31592a9b825SPhil Edworthy
316f46a0b47SGeert Uytterhoeven if (parent) {
317f46a0b47SGeert Uytterhoeven name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
318f46a0b47SGeert Uytterhoeven parent, np);
319f46a0b47SGeert Uytterhoeven if (!name) {
320f46a0b47SGeert Uytterhoeven ret = -ENOMEM;
321f46a0b47SGeert Uytterhoeven goto done;
322f46a0b47SGeert Uytterhoeven }
323f46a0b47SGeert Uytterhoeven } else {
324f46a0b47SGeert Uytterhoeven name = np->name;
325f46a0b47SGeert Uytterhoeven }
326f46a0b47SGeert Uytterhoeven
327*f982b9d5SBiju Das mutex_lock(&pctrl->mutex);
328*f982b9d5SBiju Das
32992a9b825SPhil Edworthy /* Register a single pin group listing all the pins we read from DT */
330f46a0b47SGeert Uytterhoeven gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
33192a9b825SPhil Edworthy if (gsel < 0) {
33292a9b825SPhil Edworthy ret = gsel;
333*f982b9d5SBiju Das goto unlock;
33492a9b825SPhil Edworthy }
33592a9b825SPhil Edworthy
33692a9b825SPhil Edworthy /*
33792a9b825SPhil Edworthy * Register a single group function where the 'data' is an array PSEL
33892a9b825SPhil Edworthy * register values read from DT.
33992a9b825SPhil Edworthy */
340f46a0b47SGeert Uytterhoeven pin_fn[0] = name;
341f46a0b47SGeert Uytterhoeven fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
34292a9b825SPhil Edworthy if (fsel < 0) {
34392a9b825SPhil Edworthy ret = fsel;
34492a9b825SPhil Edworthy goto remove_group;
34592a9b825SPhil Edworthy }
34692a9b825SPhil Edworthy
347*f982b9d5SBiju Das mutex_unlock(&pctrl->mutex);
348*f982b9d5SBiju Das
34992a9b825SPhil Edworthy maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
350f46a0b47SGeert Uytterhoeven maps[idx].data.mux.group = name;
351f46a0b47SGeert Uytterhoeven maps[idx].data.mux.function = name;
35292a9b825SPhil Edworthy idx++;
35392a9b825SPhil Edworthy
35492a9b825SPhil Edworthy dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
35592a9b825SPhil Edworthy ret = 0;
35692a9b825SPhil Edworthy goto done;
35792a9b825SPhil Edworthy
35892a9b825SPhil Edworthy remove_group:
35992a9b825SPhil Edworthy pinctrl_generic_remove_group(pctldev, gsel);
360*f982b9d5SBiju Das unlock:
361*f982b9d5SBiju Das mutex_unlock(&pctrl->mutex);
36292a9b825SPhil Edworthy done:
36392a9b825SPhil Edworthy *index = idx;
36492a9b825SPhil Edworthy kfree(configs);
36592a9b825SPhil Edworthy return ret;
36692a9b825SPhil Edworthy }
36792a9b825SPhil Edworthy
rzv2m_dt_free_map(struct pinctrl_dev * pctldev,struct pinctrl_map * map,unsigned int num_maps)36892a9b825SPhil Edworthy static void rzv2m_dt_free_map(struct pinctrl_dev *pctldev,
36992a9b825SPhil Edworthy struct pinctrl_map *map,
37092a9b825SPhil Edworthy unsigned int num_maps)
37192a9b825SPhil Edworthy {
37292a9b825SPhil Edworthy unsigned int i;
37392a9b825SPhil Edworthy
37492a9b825SPhil Edworthy if (!map)
37592a9b825SPhil Edworthy return;
37692a9b825SPhil Edworthy
37792a9b825SPhil Edworthy for (i = 0; i < num_maps; ++i) {
37892a9b825SPhil Edworthy if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
37992a9b825SPhil Edworthy map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
38092a9b825SPhil Edworthy kfree(map[i].data.configs.configs);
38192a9b825SPhil Edworthy }
38292a9b825SPhil Edworthy kfree(map);
38392a9b825SPhil Edworthy }
38492a9b825SPhil Edworthy
rzv2m_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned int * num_maps)38592a9b825SPhil Edworthy static int rzv2m_dt_node_to_map(struct pinctrl_dev *pctldev,
38692a9b825SPhil Edworthy struct device_node *np,
38792a9b825SPhil Edworthy struct pinctrl_map **map,
38892a9b825SPhil Edworthy unsigned int *num_maps)
38992a9b825SPhil Edworthy {
39092a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
39192a9b825SPhil Edworthy struct device_node *child;
39292a9b825SPhil Edworthy unsigned int index;
39392a9b825SPhil Edworthy int ret;
39492a9b825SPhil Edworthy
39592a9b825SPhil Edworthy *map = NULL;
39692a9b825SPhil Edworthy *num_maps = 0;
39792a9b825SPhil Edworthy index = 0;
39892a9b825SPhil Edworthy
39992a9b825SPhil Edworthy for_each_child_of_node(np, child) {
400f46a0b47SGeert Uytterhoeven ret = rzv2m_dt_subnode_to_map(pctldev, child, np, map,
40192a9b825SPhil Edworthy num_maps, &index);
40292a9b825SPhil Edworthy if (ret < 0) {
40392a9b825SPhil Edworthy of_node_put(child);
40492a9b825SPhil Edworthy goto done;
40592a9b825SPhil Edworthy }
40692a9b825SPhil Edworthy }
40792a9b825SPhil Edworthy
40892a9b825SPhil Edworthy if (*num_maps == 0) {
409f46a0b47SGeert Uytterhoeven ret = rzv2m_dt_subnode_to_map(pctldev, np, NULL, map,
41092a9b825SPhil Edworthy num_maps, &index);
41192a9b825SPhil Edworthy if (ret < 0)
41292a9b825SPhil Edworthy goto done;
41392a9b825SPhil Edworthy }
41492a9b825SPhil Edworthy
41592a9b825SPhil Edworthy if (*num_maps)
41692a9b825SPhil Edworthy return 0;
41792a9b825SPhil Edworthy
41892a9b825SPhil Edworthy dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
41992a9b825SPhil Edworthy ret = -EINVAL;
42092a9b825SPhil Edworthy
42192a9b825SPhil Edworthy done:
42292a9b825SPhil Edworthy rzv2m_dt_free_map(pctldev, *map, *num_maps);
42392a9b825SPhil Edworthy
42492a9b825SPhil Edworthy return ret;
42592a9b825SPhil Edworthy }
42692a9b825SPhil Edworthy
rzv2m_validate_gpio_pin(struct rzv2m_pinctrl * pctrl,u32 cfg,u32 port,u8 bit)42792a9b825SPhil Edworthy static int rzv2m_validate_gpio_pin(struct rzv2m_pinctrl *pctrl,
42892a9b825SPhil Edworthy u32 cfg, u32 port, u8 bit)
42992a9b825SPhil Edworthy {
43092a9b825SPhil Edworthy u8 pincount = RZV2M_GPIO_PORT_GET_PINCNT(cfg);
43192a9b825SPhil Edworthy u32 port_index = RZV2M_GPIO_PORT_GET_INDEX(cfg);
43292a9b825SPhil Edworthy u32 data;
43392a9b825SPhil Edworthy
43492a9b825SPhil Edworthy if (bit >= pincount || port >= pctrl->data->n_port_pins)
43592a9b825SPhil Edworthy return -EINVAL;
43692a9b825SPhil Edworthy
43792a9b825SPhil Edworthy data = pctrl->data->port_pin_configs[port];
43892a9b825SPhil Edworthy if (port_index != RZV2M_GPIO_PORT_GET_INDEX(data))
43992a9b825SPhil Edworthy return -EINVAL;
44092a9b825SPhil Edworthy
44192a9b825SPhil Edworthy return 0;
44292a9b825SPhil Edworthy }
44392a9b825SPhil Edworthy
rzv2m_rmw_pin_config(struct rzv2m_pinctrl * pctrl,u32 offset,u8 shift,u32 mask,u32 val)44492a9b825SPhil Edworthy static void rzv2m_rmw_pin_config(struct rzv2m_pinctrl *pctrl, u32 offset,
44592a9b825SPhil Edworthy u8 shift, u32 mask, u32 val)
44692a9b825SPhil Edworthy {
44792a9b825SPhil Edworthy void __iomem *addr = pctrl->base + offset;
44892a9b825SPhil Edworthy unsigned long flags;
44992a9b825SPhil Edworthy u32 reg;
45092a9b825SPhil Edworthy
45192a9b825SPhil Edworthy spin_lock_irqsave(&pctrl->lock, flags);
45292a9b825SPhil Edworthy reg = readl(addr) & ~(mask << shift);
45392a9b825SPhil Edworthy writel(reg | (val << shift), addr);
45492a9b825SPhil Edworthy spin_unlock_irqrestore(&pctrl->lock, flags);
45592a9b825SPhil Edworthy }
45692a9b825SPhil Edworthy
rzv2m_pinctrl_pinconf_get(struct pinctrl_dev * pctldev,unsigned int _pin,unsigned long * config)45792a9b825SPhil Edworthy static int rzv2m_pinctrl_pinconf_get(struct pinctrl_dev *pctldev,
45892a9b825SPhil Edworthy unsigned int _pin,
45992a9b825SPhil Edworthy unsigned long *config)
46092a9b825SPhil Edworthy {
46192a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
46292a9b825SPhil Edworthy enum pin_config_param param = pinconf_to_config_param(*config);
46392a9b825SPhil Edworthy const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
46492a9b825SPhil Edworthy unsigned int *pin_data = pin->drv_data;
46592a9b825SPhil Edworthy unsigned int arg = 0;
46692a9b825SPhil Edworthy u32 port;
46792a9b825SPhil Edworthy u32 cfg;
46892a9b825SPhil Edworthy u8 bit;
46992a9b825SPhil Edworthy u32 val;
47092a9b825SPhil Edworthy
47192a9b825SPhil Edworthy if (!pin_data)
47292a9b825SPhil Edworthy return -EINVAL;
47392a9b825SPhil Edworthy
47492a9b825SPhil Edworthy if (*pin_data & RZV2M_SINGLE_PIN) {
47592a9b825SPhil Edworthy port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
47692a9b825SPhil Edworthy cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
47792a9b825SPhil Edworthy bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
47892a9b825SPhil Edworthy } else {
47992a9b825SPhil Edworthy cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
48092a9b825SPhil Edworthy port = RZV2M_PIN_ID_TO_PORT(_pin);
48192a9b825SPhil Edworthy bit = RZV2M_PIN_ID_TO_PIN(_pin);
48292a9b825SPhil Edworthy
48392a9b825SPhil Edworthy if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
48492a9b825SPhil Edworthy return -EINVAL;
48592a9b825SPhil Edworthy }
48692a9b825SPhil Edworthy
48792a9b825SPhil Edworthy switch (param) {
48892a9b825SPhil Edworthy case PIN_CONFIG_BIAS_DISABLE:
48992a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_UP:
49092a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_DOWN: {
49192a9b825SPhil Edworthy enum pin_config_param bias;
49292a9b825SPhil Edworthy
49392a9b825SPhil Edworthy if (!(cfg & PIN_CFG_BIAS))
49492a9b825SPhil Edworthy return -EINVAL;
49592a9b825SPhil Edworthy
49692a9b825SPhil Edworthy /* PUPD uses 2-bits per pin */
49792a9b825SPhil Edworthy bit *= 2;
49892a9b825SPhil Edworthy
49992a9b825SPhil Edworthy switch ((readl(pctrl->base + PUPD(port)) >> bit) & PUPD_MASK) {
50092a9b825SPhil Edworthy case 0:
50192a9b825SPhil Edworthy bias = PIN_CONFIG_BIAS_PULL_DOWN;
50292a9b825SPhil Edworthy break;
50392a9b825SPhil Edworthy case 2:
50492a9b825SPhil Edworthy bias = PIN_CONFIG_BIAS_PULL_UP;
50592a9b825SPhil Edworthy break;
50692a9b825SPhil Edworthy default:
50792a9b825SPhil Edworthy bias = PIN_CONFIG_BIAS_DISABLE;
50892a9b825SPhil Edworthy }
50992a9b825SPhil Edworthy
51092a9b825SPhil Edworthy if (bias != param)
51192a9b825SPhil Edworthy return -EINVAL;
51292a9b825SPhil Edworthy break;
51392a9b825SPhil Edworthy }
51492a9b825SPhil Edworthy
51592a9b825SPhil Edworthy case PIN_CONFIG_DRIVE_STRENGTH_UA:
51692a9b825SPhil Edworthy if (!(cfg & PIN_CFG_DRV))
51792a9b825SPhil Edworthy return -EINVAL;
51892a9b825SPhil Edworthy
51992a9b825SPhil Edworthy /* DRV uses 2-bits per pin */
52092a9b825SPhil Edworthy bit *= 2;
52192a9b825SPhil Edworthy
52292a9b825SPhil Edworthy val = (readl(pctrl->base + DRV(port)) >> bit) & DRV_MASK;
52392a9b825SPhil Edworthy
52492a9b825SPhil Edworthy switch (cfg & PIN_CFG_GRP_MASK) {
52592a9b825SPhil Edworthy case PIN_CFG_GRP_1_8V_2:
52692a9b825SPhil Edworthy arg = drv_1_8V_group2_uA[val];
52792a9b825SPhil Edworthy break;
52892a9b825SPhil Edworthy case PIN_CFG_GRP_1_8V_3:
52992a9b825SPhil Edworthy arg = drv_1_8V_group3_uA[val];
53092a9b825SPhil Edworthy break;
53192a9b825SPhil Edworthy case PIN_CFG_GRP_SWIO_2:
53292a9b825SPhil Edworthy arg = drv_SWIO_group2_3_3V_uA[val];
53392a9b825SPhil Edworthy break;
53492a9b825SPhil Edworthy case PIN_CFG_GRP_SWIO_1:
53592a9b825SPhil Edworthy case PIN_CFG_GRP_3_3V:
53692a9b825SPhil Edworthy arg = drv_3_3V_group_uA[val];
53792a9b825SPhil Edworthy break;
53892a9b825SPhil Edworthy default:
53992a9b825SPhil Edworthy return -EINVAL;
54092a9b825SPhil Edworthy }
54192a9b825SPhil Edworthy
54292a9b825SPhil Edworthy break;
54392a9b825SPhil Edworthy
54492a9b825SPhil Edworthy case PIN_CONFIG_SLEW_RATE:
54592a9b825SPhil Edworthy if (!(cfg & PIN_CFG_SLEW))
54692a9b825SPhil Edworthy return -EINVAL;
54792a9b825SPhil Edworthy
54892a9b825SPhil Edworthy arg = readl(pctrl->base + SR(port)) & BIT(bit);
54992a9b825SPhil Edworthy break;
55092a9b825SPhil Edworthy
55192a9b825SPhil Edworthy default:
55292a9b825SPhil Edworthy return -ENOTSUPP;
55392a9b825SPhil Edworthy }
55492a9b825SPhil Edworthy
55592a9b825SPhil Edworthy *config = pinconf_to_config_packed(param, arg);
55692a9b825SPhil Edworthy
55792a9b825SPhil Edworthy return 0;
55892a9b825SPhil Edworthy };
55992a9b825SPhil Edworthy
rzv2m_pinctrl_pinconf_set(struct pinctrl_dev * pctldev,unsigned int _pin,unsigned long * _configs,unsigned int num_configs)56092a9b825SPhil Edworthy static int rzv2m_pinctrl_pinconf_set(struct pinctrl_dev *pctldev,
56192a9b825SPhil Edworthy unsigned int _pin,
56292a9b825SPhil Edworthy unsigned long *_configs,
56392a9b825SPhil Edworthy unsigned int num_configs)
56492a9b825SPhil Edworthy {
56592a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
56692a9b825SPhil Edworthy const struct pinctrl_pin_desc *pin = &pctrl->desc.pins[_pin];
56792a9b825SPhil Edworthy unsigned int *pin_data = pin->drv_data;
56892a9b825SPhil Edworthy enum pin_config_param param;
56992a9b825SPhil Edworthy u32 port;
57092a9b825SPhil Edworthy unsigned int i;
57192a9b825SPhil Edworthy u32 cfg;
57292a9b825SPhil Edworthy u8 bit;
57392a9b825SPhil Edworthy u32 val;
57492a9b825SPhil Edworthy
57592a9b825SPhil Edworthy if (!pin_data)
57692a9b825SPhil Edworthy return -EINVAL;
57792a9b825SPhil Edworthy
57892a9b825SPhil Edworthy if (*pin_data & RZV2M_SINGLE_PIN) {
57992a9b825SPhil Edworthy port = RZV2M_SINGLE_PIN_GET_PORT(*pin_data);
58092a9b825SPhil Edworthy cfg = RZV2M_SINGLE_PIN_GET_CFGS(*pin_data);
58192a9b825SPhil Edworthy bit = RZV2M_SINGLE_PIN_GET_BIT(*pin_data);
58292a9b825SPhil Edworthy } else {
58392a9b825SPhil Edworthy cfg = RZV2M_GPIO_PORT_GET_CFGS(*pin_data);
58492a9b825SPhil Edworthy port = RZV2M_PIN_ID_TO_PORT(_pin);
58592a9b825SPhil Edworthy bit = RZV2M_PIN_ID_TO_PIN(_pin);
58692a9b825SPhil Edworthy
58792a9b825SPhil Edworthy if (rzv2m_validate_gpio_pin(pctrl, *pin_data, RZV2M_PIN_ID_TO_PORT(_pin), bit))
58892a9b825SPhil Edworthy return -EINVAL;
58992a9b825SPhil Edworthy }
59092a9b825SPhil Edworthy
59192a9b825SPhil Edworthy for (i = 0; i < num_configs; i++) {
59292a9b825SPhil Edworthy param = pinconf_to_config_param(_configs[i]);
59392a9b825SPhil Edworthy switch (param) {
59492a9b825SPhil Edworthy case PIN_CONFIG_BIAS_DISABLE:
59592a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_UP:
59692a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_DOWN:
59792a9b825SPhil Edworthy if (!(cfg & PIN_CFG_BIAS))
59892a9b825SPhil Edworthy return -EINVAL;
59992a9b825SPhil Edworthy
60092a9b825SPhil Edworthy /* PUPD uses 2-bits per pin */
60192a9b825SPhil Edworthy bit *= 2;
60292a9b825SPhil Edworthy
60392a9b825SPhil Edworthy switch (param) {
60492a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_DOWN:
60592a9b825SPhil Edworthy val = 0;
60692a9b825SPhil Edworthy break;
60792a9b825SPhil Edworthy case PIN_CONFIG_BIAS_PULL_UP:
60892a9b825SPhil Edworthy val = 2;
60992a9b825SPhil Edworthy break;
61092a9b825SPhil Edworthy default:
61192a9b825SPhil Edworthy val = 1;
61292a9b825SPhil Edworthy }
61392a9b825SPhil Edworthy
61492a9b825SPhil Edworthy rzv2m_rmw_pin_config(pctrl, PUPD(port), bit, PUPD_MASK, val);
61592a9b825SPhil Edworthy break;
61692a9b825SPhil Edworthy
61792a9b825SPhil Edworthy case PIN_CONFIG_DRIVE_STRENGTH_UA: {
61892a9b825SPhil Edworthy unsigned int arg = pinconf_to_config_argument(_configs[i]);
61992a9b825SPhil Edworthy const unsigned int *drv_strengths;
62092a9b825SPhil Edworthy unsigned int index;
62192a9b825SPhil Edworthy
62292a9b825SPhil Edworthy if (!(cfg & PIN_CFG_DRV))
62392a9b825SPhil Edworthy return -EINVAL;
62492a9b825SPhil Edworthy
62592a9b825SPhil Edworthy switch (cfg & PIN_CFG_GRP_MASK) {
62692a9b825SPhil Edworthy case PIN_CFG_GRP_1_8V_2:
62792a9b825SPhil Edworthy drv_strengths = drv_1_8V_group2_uA;
62892a9b825SPhil Edworthy break;
62992a9b825SPhil Edworthy case PIN_CFG_GRP_1_8V_3:
63092a9b825SPhil Edworthy drv_strengths = drv_1_8V_group3_uA;
63192a9b825SPhil Edworthy break;
63292a9b825SPhil Edworthy case PIN_CFG_GRP_SWIO_2:
63392a9b825SPhil Edworthy drv_strengths = drv_SWIO_group2_3_3V_uA;
63492a9b825SPhil Edworthy break;
63592a9b825SPhil Edworthy case PIN_CFG_GRP_SWIO_1:
63692a9b825SPhil Edworthy case PIN_CFG_GRP_3_3V:
63792a9b825SPhil Edworthy drv_strengths = drv_3_3V_group_uA;
63892a9b825SPhil Edworthy break;
63992a9b825SPhil Edworthy default:
64092a9b825SPhil Edworthy return -EINVAL;
64192a9b825SPhil Edworthy }
64292a9b825SPhil Edworthy
64392a9b825SPhil Edworthy for (index = 0; index < 4; index++) {
64492a9b825SPhil Edworthy if (arg == drv_strengths[index])
64592a9b825SPhil Edworthy break;
64692a9b825SPhil Edworthy }
64792a9b825SPhil Edworthy if (index >= 4)
64892a9b825SPhil Edworthy return -EINVAL;
64992a9b825SPhil Edworthy
65092a9b825SPhil Edworthy /* DRV uses 2-bits per pin */
65192a9b825SPhil Edworthy bit *= 2;
65292a9b825SPhil Edworthy
65392a9b825SPhil Edworthy rzv2m_rmw_pin_config(pctrl, DRV(port), bit, DRV_MASK, index);
65492a9b825SPhil Edworthy break;
65592a9b825SPhil Edworthy }
65692a9b825SPhil Edworthy
65792a9b825SPhil Edworthy case PIN_CONFIG_SLEW_RATE: {
65892a9b825SPhil Edworthy unsigned int arg = pinconf_to_config_argument(_configs[i]);
65992a9b825SPhil Edworthy
66092a9b825SPhil Edworthy if (!(cfg & PIN_CFG_SLEW))
66192a9b825SPhil Edworthy return -EINVAL;
66292a9b825SPhil Edworthy
66392a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + SR(port), bit, !arg);
66492a9b825SPhil Edworthy break;
66592a9b825SPhil Edworthy }
66692a9b825SPhil Edworthy
66792a9b825SPhil Edworthy default:
66892a9b825SPhil Edworthy return -EOPNOTSUPP;
66992a9b825SPhil Edworthy }
67092a9b825SPhil Edworthy }
67192a9b825SPhil Edworthy
67292a9b825SPhil Edworthy return 0;
67392a9b825SPhil Edworthy }
67492a9b825SPhil Edworthy
rzv2m_pinctrl_pinconf_group_set(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * configs,unsigned int num_configs)67592a9b825SPhil Edworthy static int rzv2m_pinctrl_pinconf_group_set(struct pinctrl_dev *pctldev,
67692a9b825SPhil Edworthy unsigned int group,
67792a9b825SPhil Edworthy unsigned long *configs,
67892a9b825SPhil Edworthy unsigned int num_configs)
67992a9b825SPhil Edworthy {
68092a9b825SPhil Edworthy const unsigned int *pins;
68192a9b825SPhil Edworthy unsigned int i, npins;
68292a9b825SPhil Edworthy int ret;
68392a9b825SPhil Edworthy
68492a9b825SPhil Edworthy ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
68592a9b825SPhil Edworthy if (ret)
68692a9b825SPhil Edworthy return ret;
68792a9b825SPhil Edworthy
68892a9b825SPhil Edworthy for (i = 0; i < npins; i++) {
68992a9b825SPhil Edworthy ret = rzv2m_pinctrl_pinconf_set(pctldev, pins[i], configs,
69092a9b825SPhil Edworthy num_configs);
69192a9b825SPhil Edworthy if (ret)
69292a9b825SPhil Edworthy return ret;
69392a9b825SPhil Edworthy }
69492a9b825SPhil Edworthy
69592a9b825SPhil Edworthy return 0;
69692a9b825SPhil Edworthy };
69792a9b825SPhil Edworthy
rzv2m_pinctrl_pinconf_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)69892a9b825SPhil Edworthy static int rzv2m_pinctrl_pinconf_group_get(struct pinctrl_dev *pctldev,
69992a9b825SPhil Edworthy unsigned int group,
70092a9b825SPhil Edworthy unsigned long *config)
70192a9b825SPhil Edworthy {
70292a9b825SPhil Edworthy const unsigned int *pins;
70392a9b825SPhil Edworthy unsigned int i, npins, prev_config = 0;
70492a9b825SPhil Edworthy int ret;
70592a9b825SPhil Edworthy
70692a9b825SPhil Edworthy ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
70792a9b825SPhil Edworthy if (ret)
70892a9b825SPhil Edworthy return ret;
70992a9b825SPhil Edworthy
71092a9b825SPhil Edworthy for (i = 0; i < npins; i++) {
71192a9b825SPhil Edworthy ret = rzv2m_pinctrl_pinconf_get(pctldev, pins[i], config);
71292a9b825SPhil Edworthy if (ret)
71392a9b825SPhil Edworthy return ret;
71492a9b825SPhil Edworthy
71592a9b825SPhil Edworthy /* Check config matches previous pins */
71692a9b825SPhil Edworthy if (i && prev_config != *config)
71792a9b825SPhil Edworthy return -EOPNOTSUPP;
71892a9b825SPhil Edworthy
71992a9b825SPhil Edworthy prev_config = *config;
72092a9b825SPhil Edworthy }
72192a9b825SPhil Edworthy
72292a9b825SPhil Edworthy return 0;
72392a9b825SPhil Edworthy };
72492a9b825SPhil Edworthy
72592a9b825SPhil Edworthy static const struct pinctrl_ops rzv2m_pinctrl_pctlops = {
72692a9b825SPhil Edworthy .get_groups_count = pinctrl_generic_get_group_count,
72792a9b825SPhil Edworthy .get_group_name = pinctrl_generic_get_group_name,
72892a9b825SPhil Edworthy .get_group_pins = pinctrl_generic_get_group_pins,
72992a9b825SPhil Edworthy .dt_node_to_map = rzv2m_dt_node_to_map,
73092a9b825SPhil Edworthy .dt_free_map = rzv2m_dt_free_map,
73192a9b825SPhil Edworthy };
73292a9b825SPhil Edworthy
73392a9b825SPhil Edworthy static const struct pinmux_ops rzv2m_pinctrl_pmxops = {
73492a9b825SPhil Edworthy .get_functions_count = pinmux_generic_get_function_count,
73592a9b825SPhil Edworthy .get_function_name = pinmux_generic_get_function_name,
73692a9b825SPhil Edworthy .get_function_groups = pinmux_generic_get_function_groups,
73792a9b825SPhil Edworthy .set_mux = rzv2m_pinctrl_set_mux,
73892a9b825SPhil Edworthy .strict = true,
73992a9b825SPhil Edworthy };
74092a9b825SPhil Edworthy
74192a9b825SPhil Edworthy static const struct pinconf_ops rzv2m_pinctrl_confops = {
74292a9b825SPhil Edworthy .is_generic = true,
74392a9b825SPhil Edworthy .pin_config_get = rzv2m_pinctrl_pinconf_get,
74492a9b825SPhil Edworthy .pin_config_set = rzv2m_pinctrl_pinconf_set,
74592a9b825SPhil Edworthy .pin_config_group_set = rzv2m_pinctrl_pinconf_group_set,
74692a9b825SPhil Edworthy .pin_config_group_get = rzv2m_pinctrl_pinconf_group_get,
74792a9b825SPhil Edworthy .pin_config_config_dbg_show = pinconf_generic_dump_config,
74892a9b825SPhil Edworthy };
74992a9b825SPhil Edworthy
rzv2m_gpio_request(struct gpio_chip * chip,unsigned int offset)75092a9b825SPhil Edworthy static int rzv2m_gpio_request(struct gpio_chip *chip, unsigned int offset)
75192a9b825SPhil Edworthy {
75292a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
75392a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
75492a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
75592a9b825SPhil Edworthy int ret;
75692a9b825SPhil Edworthy
75792a9b825SPhil Edworthy ret = pinctrl_gpio_request(chip->base + offset);
75892a9b825SPhil Edworthy if (ret)
75992a9b825SPhil Edworthy return ret;
76092a9b825SPhil Edworthy
76192a9b825SPhil Edworthy rzv2m_pinctrl_set_pfc_mode(pctrl, port, bit, 0);
76292a9b825SPhil Edworthy
76392a9b825SPhil Edworthy return 0;
76492a9b825SPhil Edworthy }
76592a9b825SPhil Edworthy
rzv2m_gpio_set_direction(struct rzv2m_pinctrl * pctrl,u32 port,u8 bit,bool output)76692a9b825SPhil Edworthy static void rzv2m_gpio_set_direction(struct rzv2m_pinctrl *pctrl, u32 port,
76792a9b825SPhil Edworthy u8 bit, bool output)
76892a9b825SPhil Edworthy {
76992a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + OE(port), bit, output);
77092a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + IE(port), bit, !output);
77192a9b825SPhil Edworthy }
77292a9b825SPhil Edworthy
rzv2m_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)77392a9b825SPhil Edworthy static int rzv2m_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
77492a9b825SPhil Edworthy {
77592a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
77692a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
77792a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
77892a9b825SPhil Edworthy
77992a9b825SPhil Edworthy if (!(readl(pctrl->base + IE(port)) & BIT(bit)))
78092a9b825SPhil Edworthy return GPIO_LINE_DIRECTION_OUT;
78192a9b825SPhil Edworthy
78292a9b825SPhil Edworthy return GPIO_LINE_DIRECTION_IN;
78392a9b825SPhil Edworthy }
78492a9b825SPhil Edworthy
rzv2m_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)78592a9b825SPhil Edworthy static int rzv2m_gpio_direction_input(struct gpio_chip *chip,
78692a9b825SPhil Edworthy unsigned int offset)
78792a9b825SPhil Edworthy {
78892a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
78992a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
79092a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
79192a9b825SPhil Edworthy
79292a9b825SPhil Edworthy rzv2m_gpio_set_direction(pctrl, port, bit, false);
79392a9b825SPhil Edworthy
79492a9b825SPhil Edworthy return 0;
79592a9b825SPhil Edworthy }
79692a9b825SPhil Edworthy
rzv2m_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)79792a9b825SPhil Edworthy static void rzv2m_gpio_set(struct gpio_chip *chip, unsigned int offset,
79892a9b825SPhil Edworthy int value)
79992a9b825SPhil Edworthy {
80092a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
80192a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
80292a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
80392a9b825SPhil Edworthy
80492a9b825SPhil Edworthy rzv2m_writel_we(pctrl->base + DO(port), bit, !!value);
80592a9b825SPhil Edworthy }
80692a9b825SPhil Edworthy
rzv2m_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)80792a9b825SPhil Edworthy static int rzv2m_gpio_direction_output(struct gpio_chip *chip,
80892a9b825SPhil Edworthy unsigned int offset, int value)
80992a9b825SPhil Edworthy {
81092a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
81192a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
81292a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
81392a9b825SPhil Edworthy
81492a9b825SPhil Edworthy rzv2m_gpio_set(chip, offset, value);
81592a9b825SPhil Edworthy rzv2m_gpio_set_direction(pctrl, port, bit, true);
81692a9b825SPhil Edworthy
81792a9b825SPhil Edworthy return 0;
81892a9b825SPhil Edworthy }
81992a9b825SPhil Edworthy
rzv2m_gpio_get(struct gpio_chip * chip,unsigned int offset)82092a9b825SPhil Edworthy static int rzv2m_gpio_get(struct gpio_chip *chip, unsigned int offset)
82192a9b825SPhil Edworthy {
82292a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl = gpiochip_get_data(chip);
82392a9b825SPhil Edworthy u32 port = RZV2M_PIN_ID_TO_PORT(offset);
82492a9b825SPhil Edworthy u8 bit = RZV2M_PIN_ID_TO_PIN(offset);
82592a9b825SPhil Edworthy int direction = rzv2m_gpio_get_direction(chip, offset);
82692a9b825SPhil Edworthy
82792a9b825SPhil Edworthy if (direction == GPIO_LINE_DIRECTION_IN)
82892a9b825SPhil Edworthy return !!(readl(pctrl->base + DI(port)) & BIT(bit));
82992a9b825SPhil Edworthy else
83092a9b825SPhil Edworthy return !!(readl(pctrl->base + DO(port)) & BIT(bit));
83192a9b825SPhil Edworthy }
83292a9b825SPhil Edworthy
rzv2m_gpio_free(struct gpio_chip * chip,unsigned int offset)83392a9b825SPhil Edworthy static void rzv2m_gpio_free(struct gpio_chip *chip, unsigned int offset)
83492a9b825SPhil Edworthy {
83592a9b825SPhil Edworthy pinctrl_gpio_free(chip->base + offset);
83692a9b825SPhil Edworthy
83792a9b825SPhil Edworthy /*
83892a9b825SPhil Edworthy * Set the GPIO as an input to ensure that the next GPIO request won't
83992a9b825SPhil Edworthy * drive the GPIO pin as an output.
84092a9b825SPhil Edworthy */
84192a9b825SPhil Edworthy rzv2m_gpio_direction_input(chip, offset);
84292a9b825SPhil Edworthy }
84392a9b825SPhil Edworthy
84492a9b825SPhil Edworthy static const char * const rzv2m_gpio_names[] = {
84592a9b825SPhil Edworthy "P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7",
84692a9b825SPhil Edworthy "P0_8", "P0_9", "P0_10", "P0_11", "P0_12", "P0_13", "P0_14", "P0_15",
84792a9b825SPhil Edworthy "P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7",
84892a9b825SPhil Edworthy "P1_8", "P1_9", "P1_10", "P1_11", "P1_12", "P1_13", "P1_14", "P1_15",
84992a9b825SPhil Edworthy "P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7",
85092a9b825SPhil Edworthy "P2_8", "P2_9", "P2_10", "P2_11", "P2_12", "P2_13", "P2_14", "P2_15",
85192a9b825SPhil Edworthy "P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7",
85292a9b825SPhil Edworthy "P3_8", "P3_9", "P3_10", "P3_11", "P3_12", "P3_13", "P3_14", "P3_15",
85392a9b825SPhil Edworthy "P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7",
85492a9b825SPhil Edworthy "P4_8", "P4_9", "P4_10", "P4_11", "P4_12", "P4_13", "P4_14", "P4_15",
85592a9b825SPhil Edworthy "P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7",
85692a9b825SPhil Edworthy "P5_8", "P5_9", "P5_10", "P5_11", "P5_12", "P5_13", "P5_14", "P5_15",
85792a9b825SPhil Edworthy "P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7",
85892a9b825SPhil Edworthy "P6_8", "P6_9", "P6_10", "P6_11", "P6_12", "P6_13", "P6_14", "P6_15",
85992a9b825SPhil Edworthy "P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7",
86092a9b825SPhil Edworthy "P7_8", "P7_9", "P7_10", "P7_11", "P7_12", "P7_13", "P7_14", "P7_15",
86192a9b825SPhil Edworthy "P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7",
86292a9b825SPhil Edworthy "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
86392a9b825SPhil Edworthy "P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7",
86492a9b825SPhil Edworthy "P9_8", "P9_9", "P9_10", "P9_11", "P9_12", "P9_13", "P9_14", "P9_15",
86592a9b825SPhil Edworthy "P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
86692a9b825SPhil Edworthy "P10_8", "P10_9", "P10_10", "P10_11", "P10_12", "P10_13", "P10_14", "P10_15",
86792a9b825SPhil Edworthy "P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
86892a9b825SPhil Edworthy "P11_8", "P11_9", "P11_10", "P11_11", "P11_12", "P11_13", "P11_14", "P11_15",
86992a9b825SPhil Edworthy "P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
87092a9b825SPhil Edworthy "P12_8", "P12_9", "P12_10", "P12_11", "P12_12", "P12_13", "P12_14", "P12_15",
87192a9b825SPhil Edworthy "P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
87292a9b825SPhil Edworthy "P13_8", "P13_9", "P13_10", "P13_11", "P13_12", "P13_13", "P13_14", "P13_15",
87392a9b825SPhil Edworthy "P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
87492a9b825SPhil Edworthy "P14_8", "P14_9", "P14_10", "P14_11", "P14_12", "P14_13", "P14_14", "P14_15",
87592a9b825SPhil Edworthy "P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
87692a9b825SPhil Edworthy "P15_8", "P15_9", "P15_10", "P15_11", "P15_12", "P15_13", "P15_14", "P15_15",
87792a9b825SPhil Edworthy "P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
87892a9b825SPhil Edworthy "P16_8", "P16_9", "P16_10", "P16_11", "P16_12", "P16_13", "P16_14", "P16_15",
87992a9b825SPhil Edworthy "P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
88092a9b825SPhil Edworthy "P17_8", "P17_9", "P17_10", "P17_11", "P17_12", "P17_13", "P17_14", "P17_15",
88192a9b825SPhil Edworthy "P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
88292a9b825SPhil Edworthy "P18_8", "P18_9", "P18_10", "P18_11", "P18_12", "P18_13", "P18_14", "P18_15",
88392a9b825SPhil Edworthy "P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
88492a9b825SPhil Edworthy "P19_8", "P19_9", "P19_10", "P19_11", "P19_12", "P19_13", "P19_14", "P19_15",
88592a9b825SPhil Edworthy "P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
88692a9b825SPhil Edworthy "P20_8", "P20_9", "P20_10", "P20_11", "P20_12", "P20_13", "P20_14", "P20_15",
88792a9b825SPhil Edworthy "P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
88892a9b825SPhil Edworthy "P21_8", "P21_9", "P21_10", "P21_11", "P21_12", "P21_13", "P21_14", "P21_15",
88992a9b825SPhil Edworthy };
89092a9b825SPhil Edworthy
89192a9b825SPhil Edworthy static const u32 rzv2m_gpio_configs[] = {
89292a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(14, 0, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
89392a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(16, 1, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
89492a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(8, 2, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
89592a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(16, 3, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
89692a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(8, 4, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
89792a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(4, 5, PIN_CFG_GRP_1_8V_3 | RZV2M_MPXED_PIN_FUNCS),
89892a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(12, 6, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
89992a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(6, 7, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
90092a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(8, 8, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
90192a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(8, 9, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
90292a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(9, 10, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
90392a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(9, 11, PIN_CFG_GRP_SWIO_1 | RZV2M_MPXED_PIN_FUNCS),
90492a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(4, 12, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
90592a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(12, 13, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
90692a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(8, 14, PIN_CFG_GRP_3_3V | RZV2M_MPXED_PIN_FUNCS),
90792a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(16, 15, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
90892a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(14, 16, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
90992a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(1, 17, PIN_CFG_GRP_SWIO_2 | RZV2M_MPXED_PIN_FUNCS),
91092a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(0, 18, 0),
91192a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(0, 19, 0),
91292a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(3, 20, PIN_CFG_GRP_1_8V_2 | PIN_CFG_DRV),
91392a9b825SPhil Edworthy RZV2M_GPIO_PORT_PACK(1, 21, PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW),
91492a9b825SPhil Edworthy };
91592a9b825SPhil Edworthy
91692a9b825SPhil Edworthy static const struct rzv2m_dedicated_configs rzv2m_dedicated_pins[] = {
91792a9b825SPhil Edworthy { "NAWPN", RZV2M_SINGLE_PIN_PACK(0,
91892a9b825SPhil Edworthy (PIN_CFG_GRP_SWIO_2 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
91992a9b825SPhil Edworthy { "IM0CLK", RZV2M_SINGLE_PIN_PACK(1,
92092a9b825SPhil Edworthy (PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
92192a9b825SPhil Edworthy { "IM1CLK", RZV2M_SINGLE_PIN_PACK(2,
92292a9b825SPhil Edworthy (PIN_CFG_GRP_SWIO_1 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
92392a9b825SPhil Edworthy { "DETDO", RZV2M_SINGLE_PIN_PACK(5,
92492a9b825SPhil Edworthy (PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
92592a9b825SPhil Edworthy { "DETMS", RZV2M_SINGLE_PIN_PACK(6,
92692a9b825SPhil Edworthy (PIN_CFG_GRP_1_8V_3 | PIN_CFG_DRV | PIN_CFG_SLEW)) },
92792a9b825SPhil Edworthy { "PCRSTOUTB", RZV2M_SINGLE_PIN_PACK(12,
92892a9b825SPhil Edworthy (PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
92992a9b825SPhil Edworthy { "USPWEN", RZV2M_SINGLE_PIN_PACK(14,
93092a9b825SPhil Edworthy (PIN_CFG_GRP_3_3V | PIN_CFG_DRV | PIN_CFG_SLEW)) },
93192a9b825SPhil Edworthy };
93292a9b825SPhil Edworthy
rzv2m_gpio_register(struct rzv2m_pinctrl * pctrl)93392a9b825SPhil Edworthy static int rzv2m_gpio_register(struct rzv2m_pinctrl *pctrl)
93492a9b825SPhil Edworthy {
93592a9b825SPhil Edworthy struct device_node *np = pctrl->dev->of_node;
93692a9b825SPhil Edworthy struct gpio_chip *chip = &pctrl->gpio_chip;
93792a9b825SPhil Edworthy const char *name = dev_name(pctrl->dev);
93892a9b825SPhil Edworthy struct of_phandle_args of_args;
93992a9b825SPhil Edworthy int ret;
94092a9b825SPhil Edworthy
94192a9b825SPhil Edworthy ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &of_args);
94292a9b825SPhil Edworthy if (ret) {
94392a9b825SPhil Edworthy dev_err(pctrl->dev, "Unable to parse gpio-ranges\n");
94492a9b825SPhil Edworthy return ret;
94592a9b825SPhil Edworthy }
94692a9b825SPhil Edworthy
94792a9b825SPhil Edworthy if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
94892a9b825SPhil Edworthy of_args.args[2] != pctrl->data->n_port_pins) {
94992a9b825SPhil Edworthy dev_err(pctrl->dev, "gpio-ranges does not match selected SOC\n");
95092a9b825SPhil Edworthy return -EINVAL;
95192a9b825SPhil Edworthy }
95292a9b825SPhil Edworthy
95392a9b825SPhil Edworthy chip->names = pctrl->data->port_pins;
95492a9b825SPhil Edworthy chip->request = rzv2m_gpio_request;
95592a9b825SPhil Edworthy chip->free = rzv2m_gpio_free;
95692a9b825SPhil Edworthy chip->get_direction = rzv2m_gpio_get_direction;
95792a9b825SPhil Edworthy chip->direction_input = rzv2m_gpio_direction_input;
95892a9b825SPhil Edworthy chip->direction_output = rzv2m_gpio_direction_output;
95992a9b825SPhil Edworthy chip->get = rzv2m_gpio_get;
96092a9b825SPhil Edworthy chip->set = rzv2m_gpio_set;
96192a9b825SPhil Edworthy chip->label = name;
96292a9b825SPhil Edworthy chip->parent = pctrl->dev;
96392a9b825SPhil Edworthy chip->owner = THIS_MODULE;
96492a9b825SPhil Edworthy chip->base = -1;
96592a9b825SPhil Edworthy chip->ngpio = of_args.args[2];
96692a9b825SPhil Edworthy
96792a9b825SPhil Edworthy pctrl->gpio_range.id = 0;
96892a9b825SPhil Edworthy pctrl->gpio_range.pin_base = 0;
96992a9b825SPhil Edworthy pctrl->gpio_range.base = 0;
97092a9b825SPhil Edworthy pctrl->gpio_range.npins = chip->ngpio;
97192a9b825SPhil Edworthy pctrl->gpio_range.name = chip->label;
97292a9b825SPhil Edworthy pctrl->gpio_range.gc = chip;
97392a9b825SPhil Edworthy ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
97492a9b825SPhil Edworthy if (ret) {
97592a9b825SPhil Edworthy dev_err(pctrl->dev, "failed to add GPIO controller\n");
97692a9b825SPhil Edworthy return ret;
97792a9b825SPhil Edworthy }
97892a9b825SPhil Edworthy
97992a9b825SPhil Edworthy dev_dbg(pctrl->dev, "Registered gpio controller\n");
98092a9b825SPhil Edworthy
98192a9b825SPhil Edworthy return 0;
98292a9b825SPhil Edworthy }
98392a9b825SPhil Edworthy
rzv2m_pinctrl_register(struct rzv2m_pinctrl * pctrl)98492a9b825SPhil Edworthy static int rzv2m_pinctrl_register(struct rzv2m_pinctrl *pctrl)
98592a9b825SPhil Edworthy {
98692a9b825SPhil Edworthy struct pinctrl_pin_desc *pins;
98792a9b825SPhil Edworthy unsigned int i, j;
98892a9b825SPhil Edworthy u32 *pin_data;
98992a9b825SPhil Edworthy int ret;
99092a9b825SPhil Edworthy
99192a9b825SPhil Edworthy pctrl->desc.name = DRV_NAME;
99292a9b825SPhil Edworthy pctrl->desc.npins = pctrl->data->n_port_pins + pctrl->data->n_dedicated_pins;
99392a9b825SPhil Edworthy pctrl->desc.pctlops = &rzv2m_pinctrl_pctlops;
99492a9b825SPhil Edworthy pctrl->desc.pmxops = &rzv2m_pinctrl_pmxops;
99592a9b825SPhil Edworthy pctrl->desc.confops = &rzv2m_pinctrl_confops;
99692a9b825SPhil Edworthy pctrl->desc.owner = THIS_MODULE;
99792a9b825SPhil Edworthy
99892a9b825SPhil Edworthy pins = devm_kcalloc(pctrl->dev, pctrl->desc.npins, sizeof(*pins), GFP_KERNEL);
99992a9b825SPhil Edworthy if (!pins)
100092a9b825SPhil Edworthy return -ENOMEM;
100192a9b825SPhil Edworthy
100292a9b825SPhil Edworthy pin_data = devm_kcalloc(pctrl->dev, pctrl->desc.npins,
100392a9b825SPhil Edworthy sizeof(*pin_data), GFP_KERNEL);
100492a9b825SPhil Edworthy if (!pin_data)
100592a9b825SPhil Edworthy return -ENOMEM;
100692a9b825SPhil Edworthy
100792a9b825SPhil Edworthy pctrl->pins = pins;
100892a9b825SPhil Edworthy pctrl->desc.pins = pins;
100992a9b825SPhil Edworthy
101092a9b825SPhil Edworthy for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
101192a9b825SPhil Edworthy pins[i].number = i;
101292a9b825SPhil Edworthy pins[i].name = pctrl->data->port_pins[i];
101392a9b825SPhil Edworthy if (i && !(i % RZV2M_PINS_PER_PORT))
101492a9b825SPhil Edworthy j++;
101592a9b825SPhil Edworthy pin_data[i] = pctrl->data->port_pin_configs[j];
101692a9b825SPhil Edworthy pins[i].drv_data = &pin_data[i];
101792a9b825SPhil Edworthy }
101892a9b825SPhil Edworthy
101992a9b825SPhil Edworthy for (i = 0; i < pctrl->data->n_dedicated_pins; i++) {
102092a9b825SPhil Edworthy unsigned int index = pctrl->data->n_port_pins + i;
102192a9b825SPhil Edworthy
102292a9b825SPhil Edworthy pins[index].number = index;
102392a9b825SPhil Edworthy pins[index].name = pctrl->data->dedicated_pins[i].name;
102492a9b825SPhil Edworthy pin_data[index] = pctrl->data->dedicated_pins[i].config;
102592a9b825SPhil Edworthy pins[index].drv_data = &pin_data[index];
102692a9b825SPhil Edworthy }
102792a9b825SPhil Edworthy
102892a9b825SPhil Edworthy ret = devm_pinctrl_register_and_init(pctrl->dev, &pctrl->desc, pctrl,
102992a9b825SPhil Edworthy &pctrl->pctl);
103092a9b825SPhil Edworthy if (ret) {
103192a9b825SPhil Edworthy dev_err(pctrl->dev, "pinctrl registration failed\n");
103292a9b825SPhil Edworthy return ret;
103392a9b825SPhil Edworthy }
103492a9b825SPhil Edworthy
103592a9b825SPhil Edworthy ret = pinctrl_enable(pctrl->pctl);
103692a9b825SPhil Edworthy if (ret) {
103792a9b825SPhil Edworthy dev_err(pctrl->dev, "pinctrl enable failed\n");
103892a9b825SPhil Edworthy return ret;
103992a9b825SPhil Edworthy }
104092a9b825SPhil Edworthy
104192a9b825SPhil Edworthy ret = rzv2m_gpio_register(pctrl);
104292a9b825SPhil Edworthy if (ret) {
104392a9b825SPhil Edworthy dev_err(pctrl->dev, "failed to add GPIO chip: %i\n", ret);
104492a9b825SPhil Edworthy return ret;
104592a9b825SPhil Edworthy }
104692a9b825SPhil Edworthy
104792a9b825SPhil Edworthy return 0;
104892a9b825SPhil Edworthy }
104992a9b825SPhil Edworthy
rzv2m_pinctrl_probe(struct platform_device * pdev)105092a9b825SPhil Edworthy static int rzv2m_pinctrl_probe(struct platform_device *pdev)
105192a9b825SPhil Edworthy {
105292a9b825SPhil Edworthy struct rzv2m_pinctrl *pctrl;
1053f4b2ce40SGeert Uytterhoeven struct clk *clk;
105492a9b825SPhil Edworthy int ret;
105592a9b825SPhil Edworthy
105692a9b825SPhil Edworthy pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
105792a9b825SPhil Edworthy if (!pctrl)
105892a9b825SPhil Edworthy return -ENOMEM;
105992a9b825SPhil Edworthy
106092a9b825SPhil Edworthy pctrl->dev = &pdev->dev;
106192a9b825SPhil Edworthy
106292a9b825SPhil Edworthy pctrl->data = of_device_get_match_data(&pdev->dev);
106392a9b825SPhil Edworthy if (!pctrl->data)
106492a9b825SPhil Edworthy return -EINVAL;
106592a9b825SPhil Edworthy
106692a9b825SPhil Edworthy pctrl->base = devm_platform_ioremap_resource(pdev, 0);
106792a9b825SPhil Edworthy if (IS_ERR(pctrl->base))
106892a9b825SPhil Edworthy return PTR_ERR(pctrl->base);
106992a9b825SPhil Edworthy
1070f4b2ce40SGeert Uytterhoeven clk = devm_clk_get_enabled(pctrl->dev, NULL);
1071f4b2ce40SGeert Uytterhoeven if (IS_ERR(clk))
1072f4b2ce40SGeert Uytterhoeven return dev_err_probe(pctrl->dev, PTR_ERR(clk),
1073f4b2ce40SGeert Uytterhoeven "failed to enable GPIO clk\n");
107492a9b825SPhil Edworthy
107592a9b825SPhil Edworthy spin_lock_init(&pctrl->lock);
1076*f982b9d5SBiju Das mutex_init(&pctrl->mutex);
107792a9b825SPhil Edworthy
107892a9b825SPhil Edworthy platform_set_drvdata(pdev, pctrl);
107992a9b825SPhil Edworthy
108092a9b825SPhil Edworthy ret = rzv2m_pinctrl_register(pctrl);
108192a9b825SPhil Edworthy if (ret)
108292a9b825SPhil Edworthy return ret;
108392a9b825SPhil Edworthy
108492a9b825SPhil Edworthy dev_info(pctrl->dev, "%s support registered\n", DRV_NAME);
108592a9b825SPhil Edworthy return 0;
108692a9b825SPhil Edworthy }
108792a9b825SPhil Edworthy
108892a9b825SPhil Edworthy static struct rzv2m_pinctrl_data r9a09g011_data = {
108992a9b825SPhil Edworthy .port_pins = rzv2m_gpio_names,
109092a9b825SPhil Edworthy .port_pin_configs = rzv2m_gpio_configs,
109192a9b825SPhil Edworthy .dedicated_pins = rzv2m_dedicated_pins,
109292a9b825SPhil Edworthy .n_port_pins = ARRAY_SIZE(rzv2m_gpio_configs) * RZV2M_PINS_PER_PORT,
109392a9b825SPhil Edworthy .n_dedicated_pins = ARRAY_SIZE(rzv2m_dedicated_pins),
109492a9b825SPhil Edworthy };
109592a9b825SPhil Edworthy
109692a9b825SPhil Edworthy static const struct of_device_id rzv2m_pinctrl_of_table[] = {
109792a9b825SPhil Edworthy {
109892a9b825SPhil Edworthy .compatible = "renesas,r9a09g011-pinctrl",
109992a9b825SPhil Edworthy .data = &r9a09g011_data,
110092a9b825SPhil Edworthy },
110192a9b825SPhil Edworthy { /* sentinel */ }
110292a9b825SPhil Edworthy };
110392a9b825SPhil Edworthy
110492a9b825SPhil Edworthy static struct platform_driver rzv2m_pinctrl_driver = {
110592a9b825SPhil Edworthy .driver = {
110692a9b825SPhil Edworthy .name = DRV_NAME,
110792a9b825SPhil Edworthy .of_match_table = of_match_ptr(rzv2m_pinctrl_of_table),
110892a9b825SPhil Edworthy },
110992a9b825SPhil Edworthy .probe = rzv2m_pinctrl_probe,
111092a9b825SPhil Edworthy };
111192a9b825SPhil Edworthy
rzv2m_pinctrl_init(void)111292a9b825SPhil Edworthy static int __init rzv2m_pinctrl_init(void)
111392a9b825SPhil Edworthy {
111492a9b825SPhil Edworthy return platform_driver_register(&rzv2m_pinctrl_driver);
111592a9b825SPhil Edworthy }
111692a9b825SPhil Edworthy core_initcall(rzv2m_pinctrl_init);
111792a9b825SPhil Edworthy
111892a9b825SPhil Edworthy MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
111992a9b825SPhil Edworthy MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/V2M");
1120