xref: /linux/drivers/pinctrl/meson/pinctrl-meson.c (revision 3c910ecbdda4227abd145967774f92b1a3341493)
1*3c910ecbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26ac73095SBeniamino Galvani /*
36ac73095SBeniamino Galvani  * Pin controller and GPIO driver for Amlogic Meson SoCs
46ac73095SBeniamino Galvani  *
56ac73095SBeniamino Galvani  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
66ac73095SBeniamino Galvani  */
76ac73095SBeniamino Galvani 
86ac73095SBeniamino Galvani /*
96ac73095SBeniamino Galvani  * The available pins are organized in banks (A,B,C,D,E,X,Y,Z,AO,
10faa246deSCarlo Caione  * BOOT,CARD for meson6, X,Y,DV,H,Z,AO,BOOT,CARD for meson8 and
11faa246deSCarlo Caione  * X,Y,DV,H,AO,BOOT,CARD,DIF for meson8b) and each bank has a
12faa246deSCarlo Caione  * variable number of pins.
136ac73095SBeniamino Galvani  *
146ac73095SBeniamino Galvani  * The AO bank is special because it belongs to the Always-On power
156ac73095SBeniamino Galvani  * domain which can't be powered off; the bank also uses a set of
166ac73095SBeniamino Galvani  * registers different from the other banks.
176ac73095SBeniamino Galvani  *
18db80f0e1SBeniamino Galvani  * For each pin controller there are 4 different register ranges that
19db80f0e1SBeniamino Galvani  * control the following properties of the pins:
206ac73095SBeniamino Galvani  *  1) pin muxing
216ac73095SBeniamino Galvani  *  2) pull enable/disable
226ac73095SBeniamino Galvani  *  3) pull up/down
236ac73095SBeniamino Galvani  *  4) GPIO direction, output value, input value
246ac73095SBeniamino Galvani  *
256ac73095SBeniamino Galvani  * In some cases the register ranges for pull enable and pull
266ac73095SBeniamino Galvani  * direction are the same and thus there are only 3 register ranges.
276ac73095SBeniamino Galvani  *
28e66dd48eSXingyu Chen  * Since Meson G12A SoC, the ao register ranges for gpio, pull enable
29e66dd48eSXingyu Chen  * and pull direction are the same, so there are only 2 register ranges.
30e66dd48eSXingyu Chen  *
316ac73095SBeniamino Galvani  * For the pull and GPIO configuration every bank uses a contiguous
326ac73095SBeniamino Galvani  * set of bits in the register sets described above; the same register
336ac73095SBeniamino Galvani  * can be shared by more banks with different offsets.
346ac73095SBeniamino Galvani  *
356ac73095SBeniamino Galvani  * In addition to this there are some registers shared between all
366ac73095SBeniamino Galvani  * banks that control the IRQ functionality. This feature is not
376ac73095SBeniamino Galvani  * supported at the moment by the driver.
386ac73095SBeniamino Galvani  */
396ac73095SBeniamino Galvani 
406ac73095SBeniamino Galvani #include <linux/device.h>
411c5fb66aSLinus Walleij #include <linux/gpio/driver.h>
426ac73095SBeniamino Galvani #include <linux/init.h>
436ac73095SBeniamino Galvani #include <linux/io.h>
446ac73095SBeniamino Galvani #include <linux/of.h>
456ac73095SBeniamino Galvani #include <linux/of_address.h>
46277d14ebSJerome Brunet #include <linux/of_device.h>
476ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf-generic.h>
486ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf.h>
496ac73095SBeniamino Galvani #include <linux/pinctrl/pinctrl.h>
506ac73095SBeniamino Galvani #include <linux/pinctrl/pinmux.h>
516ac73095SBeniamino Galvani #include <linux/platform_device.h>
526ac73095SBeniamino Galvani #include <linux/regmap.h>
536ac73095SBeniamino Galvani #include <linux/seq_file.h>
546ac73095SBeniamino Galvani 
556ac73095SBeniamino Galvani #include "../core.h"
566ac73095SBeniamino Galvani #include "../pinctrl-utils.h"
576ac73095SBeniamino Galvani #include "pinctrl-meson.h"
586ac73095SBeniamino Galvani 
596ac73095SBeniamino Galvani /**
606ac73095SBeniamino Galvani  * meson_get_bank() - find the bank containing a given pin
616ac73095SBeniamino Galvani  *
62db80f0e1SBeniamino Galvani  * @pc:		the pinctrl instance
636ac73095SBeniamino Galvani  * @pin:	the pin number
646ac73095SBeniamino Galvani  * @bank:	the found bank
656ac73095SBeniamino Galvani  *
666ac73095SBeniamino Galvani  * Return:	0 on success, a negative value on error
676ac73095SBeniamino Galvani  */
68db80f0e1SBeniamino Galvani static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
696ac73095SBeniamino Galvani 			  struct meson_bank **bank)
706ac73095SBeniamino Galvani {
716ac73095SBeniamino Galvani 	int i;
726ac73095SBeniamino Galvani 
73db80f0e1SBeniamino Galvani 	for (i = 0; i < pc->data->num_banks; i++) {
74db80f0e1SBeniamino Galvani 		if (pin >= pc->data->banks[i].first &&
75db80f0e1SBeniamino Galvani 		    pin <= pc->data->banks[i].last) {
76db80f0e1SBeniamino Galvani 			*bank = &pc->data->banks[i];
776ac73095SBeniamino Galvani 			return 0;
786ac73095SBeniamino Galvani 		}
796ac73095SBeniamino Galvani 	}
806ac73095SBeniamino Galvani 
816ac73095SBeniamino Galvani 	return -EINVAL;
826ac73095SBeniamino Galvani }
836ac73095SBeniamino Galvani 
846ac73095SBeniamino Galvani /**
856ac73095SBeniamino Galvani  * meson_calc_reg_and_bit() - calculate register and bit for a pin
866ac73095SBeniamino Galvani  *
876ac73095SBeniamino Galvani  * @bank:	the bank containing the pin
886ac73095SBeniamino Galvani  * @pin:	the pin number
896ac73095SBeniamino Galvani  * @reg_type:	the type of register needed (pull-enable, pull, etc...)
906ac73095SBeniamino Galvani  * @reg:	the computed register offset
916ac73095SBeniamino Galvani  * @bit:	the computed bit
926ac73095SBeniamino Galvani  */
936ac73095SBeniamino Galvani static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin,
946ac73095SBeniamino Galvani 				   enum meson_reg_type reg_type,
956ac73095SBeniamino Galvani 				   unsigned int *reg, unsigned int *bit)
966ac73095SBeniamino Galvani {
976ac73095SBeniamino Galvani 	struct meson_reg_desc *desc = &bank->regs[reg_type];
986ac73095SBeniamino Galvani 
996ac73095SBeniamino Galvani 	*reg = desc->reg * 4;
1006ac73095SBeniamino Galvani 	*bit = desc->bit + pin - bank->first;
1016ac73095SBeniamino Galvani }
1026ac73095SBeniamino Galvani 
1036ac73095SBeniamino Galvani static int meson_get_groups_count(struct pinctrl_dev *pcdev)
1046ac73095SBeniamino Galvani {
1056ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1066ac73095SBeniamino Galvani 
1076ac73095SBeniamino Galvani 	return pc->data->num_groups;
1086ac73095SBeniamino Galvani }
1096ac73095SBeniamino Galvani 
1106ac73095SBeniamino Galvani static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
1116ac73095SBeniamino Galvani 					unsigned selector)
1126ac73095SBeniamino Galvani {
1136ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1146ac73095SBeniamino Galvani 
1156ac73095SBeniamino Galvani 	return pc->data->groups[selector].name;
1166ac73095SBeniamino Galvani }
1176ac73095SBeniamino Galvani 
1186ac73095SBeniamino Galvani static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
1196ac73095SBeniamino Galvani 				const unsigned **pins, unsigned *num_pins)
1206ac73095SBeniamino Galvani {
1216ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1226ac73095SBeniamino Galvani 
1236ac73095SBeniamino Galvani 	*pins = pc->data->groups[selector].pins;
1246ac73095SBeniamino Galvani 	*num_pins = pc->data->groups[selector].num_pins;
1256ac73095SBeniamino Galvani 
1266ac73095SBeniamino Galvani 	return 0;
1276ac73095SBeniamino Galvani }
1286ac73095SBeniamino Galvani 
1296ac73095SBeniamino Galvani static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
1306ac73095SBeniamino Galvani 			       unsigned offset)
1316ac73095SBeniamino Galvani {
1326ac73095SBeniamino Galvani 	seq_printf(s, " %s", dev_name(pcdev->dev));
1336ac73095SBeniamino Galvani }
1346ac73095SBeniamino Galvani 
1356ac73095SBeniamino Galvani static const struct pinctrl_ops meson_pctrl_ops = {
1366ac73095SBeniamino Galvani 	.get_groups_count	= meson_get_groups_count,
1376ac73095SBeniamino Galvani 	.get_group_name		= meson_get_group_name,
1386ac73095SBeniamino Galvani 	.get_group_pins		= meson_get_group_pins,
1396ac73095SBeniamino Galvani 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_all,
140d32f7fd3SIrina Tirdea 	.dt_free_map		= pinctrl_utils_free_map,
1416ac73095SBeniamino Galvani 	.pin_dbg_show		= meson_pin_dbg_show,
1426ac73095SBeniamino Galvani };
1436ac73095SBeniamino Galvani 
144ce385aa2SJerome Brunet int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
1456ac73095SBeniamino Galvani {
1466ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1476ac73095SBeniamino Galvani 
1486ac73095SBeniamino Galvani 	return pc->data->num_funcs;
1496ac73095SBeniamino Galvani }
1506ac73095SBeniamino Galvani 
151ce385aa2SJerome Brunet const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
1526ac73095SBeniamino Galvani 				    unsigned selector)
1536ac73095SBeniamino Galvani {
1546ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1556ac73095SBeniamino Galvani 
1566ac73095SBeniamino Galvani 	return pc->data->funcs[selector].name;
1576ac73095SBeniamino Galvani }
1586ac73095SBeniamino Galvani 
159ce385aa2SJerome Brunet int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
1606ac73095SBeniamino Galvani 			 const char * const **groups,
1616ac73095SBeniamino Galvani 			 unsigned * const num_groups)
1626ac73095SBeniamino Galvani {
1636ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1646ac73095SBeniamino Galvani 
1656ac73095SBeniamino Galvani 	*groups = pc->data->funcs[selector].groups;
1666ac73095SBeniamino Galvani 	*num_groups = pc->data->funcs[selector].num_groups;
1676ac73095SBeniamino Galvani 
1686ac73095SBeniamino Galvani 	return 0;
1696ac73095SBeniamino Galvani }
1706ac73095SBeniamino Galvani 
1716ac73095SBeniamino Galvani static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
1726ac73095SBeniamino Galvani 			     unsigned long *configs, unsigned num_configs)
1736ac73095SBeniamino Galvani {
1746ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1756ac73095SBeniamino Galvani 	struct meson_bank *bank;
1766ac73095SBeniamino Galvani 	enum pin_config_param param;
1776ac73095SBeniamino Galvani 	unsigned int reg, bit;
1786ac73095SBeniamino Galvani 	int i, ret;
1796ac73095SBeniamino Galvani 
180db80f0e1SBeniamino Galvani 	ret = meson_get_bank(pc, pin, &bank);
1816ac73095SBeniamino Galvani 	if (ret)
1826ac73095SBeniamino Galvani 		return ret;
1836ac73095SBeniamino Galvani 
1846ac73095SBeniamino Galvani 	for (i = 0; i < num_configs; i++) {
1856ac73095SBeniamino Galvani 		param = pinconf_to_config_param(configs[i]);
1866ac73095SBeniamino Galvani 
1876ac73095SBeniamino Galvani 		switch (param) {
1886ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_DISABLE:
1896ac73095SBeniamino Galvani 			dev_dbg(pc->dev, "pin %u: disable bias\n", pin);
1906ac73095SBeniamino Galvani 
191614b1868SJerome Brunet 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg,
192614b1868SJerome Brunet 					       &bit);
193e39f9dd8SJerome Brunet 			ret = regmap_update_bits(pc->reg_pullen, reg,
1946ac73095SBeniamino Galvani 						 BIT(bit), 0);
1956ac73095SBeniamino Galvani 			if (ret)
1966ac73095SBeniamino Galvani 				return ret;
1976ac73095SBeniamino Galvani 			break;
1986ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_PULL_UP:
1996ac73095SBeniamino Galvani 			dev_dbg(pc->dev, "pin %u: enable pull-up\n", pin);
2006ac73095SBeniamino Galvani 
2016ac73095SBeniamino Galvani 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
2026ac73095SBeniamino Galvani 					       &reg, &bit);
203db80f0e1SBeniamino Galvani 			ret = regmap_update_bits(pc->reg_pullen, reg,
2046ac73095SBeniamino Galvani 						 BIT(bit), BIT(bit));
2056ac73095SBeniamino Galvani 			if (ret)
2066ac73095SBeniamino Galvani 				return ret;
2076ac73095SBeniamino Galvani 
2086ac73095SBeniamino Galvani 			meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
209db80f0e1SBeniamino Galvani 			ret = regmap_update_bits(pc->reg_pull, reg,
2106ac73095SBeniamino Galvani 						 BIT(bit), BIT(bit));
2116ac73095SBeniamino Galvani 			if (ret)
2126ac73095SBeniamino Galvani 				return ret;
2136ac73095SBeniamino Galvani 			break;
2146ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_PULL_DOWN:
2156ac73095SBeniamino Galvani 			dev_dbg(pc->dev, "pin %u: enable pull-down\n", pin);
2166ac73095SBeniamino Galvani 
2176ac73095SBeniamino Galvani 			meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
2186ac73095SBeniamino Galvani 					       &reg, &bit);
219db80f0e1SBeniamino Galvani 			ret = regmap_update_bits(pc->reg_pullen, reg,
2206ac73095SBeniamino Galvani 						 BIT(bit), BIT(bit));
2216ac73095SBeniamino Galvani 			if (ret)
2226ac73095SBeniamino Galvani 				return ret;
2236ac73095SBeniamino Galvani 
2246ac73095SBeniamino Galvani 			meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
225db80f0e1SBeniamino Galvani 			ret = regmap_update_bits(pc->reg_pull, reg,
2266ac73095SBeniamino Galvani 						 BIT(bit), 0);
2276ac73095SBeniamino Galvani 			if (ret)
2286ac73095SBeniamino Galvani 				return ret;
2296ac73095SBeniamino Galvani 			break;
2306ac73095SBeniamino Galvani 		default:
2316ac73095SBeniamino Galvani 			return -ENOTSUPP;
2326ac73095SBeniamino Galvani 		}
2336ac73095SBeniamino Galvani 	}
2346ac73095SBeniamino Galvani 
2356ac73095SBeniamino Galvani 	return 0;
2366ac73095SBeniamino Galvani }
2376ac73095SBeniamino Galvani 
2386ac73095SBeniamino Galvani static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
2396ac73095SBeniamino Galvani {
2406ac73095SBeniamino Galvani 	struct meson_bank *bank;
2416ac73095SBeniamino Galvani 	unsigned int reg, bit, val;
2426ac73095SBeniamino Galvani 	int ret, conf;
2436ac73095SBeniamino Galvani 
244db80f0e1SBeniamino Galvani 	ret = meson_get_bank(pc, pin, &bank);
2456ac73095SBeniamino Galvani 	if (ret)
2466ac73095SBeniamino Galvani 		return ret;
2476ac73095SBeniamino Galvani 
2486ac73095SBeniamino Galvani 	meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
2496ac73095SBeniamino Galvani 
250db80f0e1SBeniamino Galvani 	ret = regmap_read(pc->reg_pullen, reg, &val);
2516ac73095SBeniamino Galvani 	if (ret)
2526ac73095SBeniamino Galvani 		return ret;
2536ac73095SBeniamino Galvani 
2546ac73095SBeniamino Galvani 	if (!(val & BIT(bit))) {
2556ac73095SBeniamino Galvani 		conf = PIN_CONFIG_BIAS_DISABLE;
2566ac73095SBeniamino Galvani 	} else {
2576ac73095SBeniamino Galvani 		meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
2586ac73095SBeniamino Galvani 
259db80f0e1SBeniamino Galvani 		ret = regmap_read(pc->reg_pull, reg, &val);
2606ac73095SBeniamino Galvani 		if (ret)
2616ac73095SBeniamino Galvani 			return ret;
2626ac73095SBeniamino Galvani 
2636ac73095SBeniamino Galvani 		if (val & BIT(bit))
2646ac73095SBeniamino Galvani 			conf = PIN_CONFIG_BIAS_PULL_UP;
2656ac73095SBeniamino Galvani 		else
2666ac73095SBeniamino Galvani 			conf = PIN_CONFIG_BIAS_PULL_DOWN;
2676ac73095SBeniamino Galvani 	}
2686ac73095SBeniamino Galvani 
2696ac73095SBeniamino Galvani 	return conf;
2706ac73095SBeniamino Galvani }
2716ac73095SBeniamino Galvani 
2726ac73095SBeniamino Galvani static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
2736ac73095SBeniamino Galvani 			     unsigned long *config)
2746ac73095SBeniamino Galvani {
2756ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
2766ac73095SBeniamino Galvani 	enum pin_config_param param = pinconf_to_config_param(*config);
2776ac73095SBeniamino Galvani 	u16 arg;
2786ac73095SBeniamino Galvani 
2796ac73095SBeniamino Galvani 	switch (param) {
2806ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_DISABLE:
2816ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_PULL_DOWN:
2826ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_PULL_UP:
2836ac73095SBeniamino Galvani 		if (meson_pinconf_get_pull(pc, pin) == param)
2846ac73095SBeniamino Galvani 			arg = 1;
2856ac73095SBeniamino Galvani 		else
2866ac73095SBeniamino Galvani 			return -EINVAL;
2876ac73095SBeniamino Galvani 		break;
2886ac73095SBeniamino Galvani 	default:
2896ac73095SBeniamino Galvani 		return -ENOTSUPP;
2906ac73095SBeniamino Galvani 	}
2916ac73095SBeniamino Galvani 
2926ac73095SBeniamino Galvani 	*config = pinconf_to_config_packed(param, arg);
2936ac73095SBeniamino Galvani 	dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
2946ac73095SBeniamino Galvani 
2956ac73095SBeniamino Galvani 	return 0;
2966ac73095SBeniamino Galvani }
2976ac73095SBeniamino Galvani 
2986ac73095SBeniamino Galvani static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
2996ac73095SBeniamino Galvani 				   unsigned int num_group,
3006ac73095SBeniamino Galvani 				   unsigned long *configs, unsigned num_configs)
3016ac73095SBeniamino Galvani {
3026ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
3036ac73095SBeniamino Galvani 	struct meson_pmx_group *group = &pc->data->groups[num_group];
3046ac73095SBeniamino Galvani 	int i;
3056ac73095SBeniamino Galvani 
3066ac73095SBeniamino Galvani 	dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
3076ac73095SBeniamino Galvani 
3086ac73095SBeniamino Galvani 	for (i = 0; i < group->num_pins; i++) {
3096ac73095SBeniamino Galvani 		meson_pinconf_set(pcdev, group->pins[i], configs,
3106ac73095SBeniamino Galvani 				  num_configs);
3116ac73095SBeniamino Galvani 	}
3126ac73095SBeniamino Galvani 
3136ac73095SBeniamino Galvani 	return 0;
3146ac73095SBeniamino Galvani }
3156ac73095SBeniamino Galvani 
3166ac73095SBeniamino Galvani static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
3176ac73095SBeniamino Galvani 				   unsigned int group, unsigned long *config)
3186ac73095SBeniamino Galvani {
3191ffbf50bSJerome Brunet 	return -ENOTSUPP;
3206ac73095SBeniamino Galvani }
3216ac73095SBeniamino Galvani 
3226ac73095SBeniamino Galvani static const struct pinconf_ops meson_pinconf_ops = {
3236ac73095SBeniamino Galvani 	.pin_config_get		= meson_pinconf_get,
3246ac73095SBeniamino Galvani 	.pin_config_set		= meson_pinconf_set,
3256ac73095SBeniamino Galvani 	.pin_config_group_get	= meson_pinconf_group_get,
3266ac73095SBeniamino Galvani 	.pin_config_group_set	= meson_pinconf_group_set,
3276ac73095SBeniamino Galvani 	.is_generic		= true,
3286ac73095SBeniamino Galvani };
3296ac73095SBeniamino Galvani 
3306ac73095SBeniamino Galvani static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
3316ac73095SBeniamino Galvani {
332db80f0e1SBeniamino Galvani 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
33370e5ecb1SJerome Brunet 	unsigned int reg, bit;
3346ac73095SBeniamino Galvani 	struct meson_bank *bank;
3356ac73095SBeniamino Galvani 	int ret;
3366ac73095SBeniamino Galvani 
33770e5ecb1SJerome Brunet 	ret = meson_get_bank(pc, gpio, &bank);
3386ac73095SBeniamino Galvani 	if (ret)
3396ac73095SBeniamino Galvani 		return ret;
3406ac73095SBeniamino Galvani 
34170e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
3426ac73095SBeniamino Galvani 
343db80f0e1SBeniamino Galvani 	return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), BIT(bit));
3446ac73095SBeniamino Galvani }
3456ac73095SBeniamino Galvani 
3466ac73095SBeniamino Galvani static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
3476ac73095SBeniamino Galvani 				       int value)
3486ac73095SBeniamino Galvani {
349db80f0e1SBeniamino Galvani 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
35070e5ecb1SJerome Brunet 	unsigned int reg, bit;
3516ac73095SBeniamino Galvani 	struct meson_bank *bank;
3526ac73095SBeniamino Galvani 	int ret;
3536ac73095SBeniamino Galvani 
35470e5ecb1SJerome Brunet 	ret = meson_get_bank(pc, gpio, &bank);
3556ac73095SBeniamino Galvani 	if (ret)
3566ac73095SBeniamino Galvani 		return ret;
3576ac73095SBeniamino Galvani 
35870e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
359db80f0e1SBeniamino Galvani 	ret = regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 0);
3606ac73095SBeniamino Galvani 	if (ret)
3616ac73095SBeniamino Galvani 		return ret;
3626ac73095SBeniamino Galvani 
36370e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
364db80f0e1SBeniamino Galvani 	return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
3656ac73095SBeniamino Galvani 				  value ? BIT(bit) : 0);
3666ac73095SBeniamino Galvani }
3676ac73095SBeniamino Galvani 
3686ac73095SBeniamino Galvani static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
3696ac73095SBeniamino Galvani {
370db80f0e1SBeniamino Galvani 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
37170e5ecb1SJerome Brunet 	unsigned int reg, bit;
3726ac73095SBeniamino Galvani 	struct meson_bank *bank;
3736ac73095SBeniamino Galvani 	int ret;
3746ac73095SBeniamino Galvani 
37570e5ecb1SJerome Brunet 	ret = meson_get_bank(pc, gpio, &bank);
3766ac73095SBeniamino Galvani 	if (ret)
3776ac73095SBeniamino Galvani 		return;
3786ac73095SBeniamino Galvani 
37970e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
380db80f0e1SBeniamino Galvani 	regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
3816ac73095SBeniamino Galvani 			   value ? BIT(bit) : 0);
3826ac73095SBeniamino Galvani }
3836ac73095SBeniamino Galvani 
3846ac73095SBeniamino Galvani static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
3856ac73095SBeniamino Galvani {
386db80f0e1SBeniamino Galvani 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
38770e5ecb1SJerome Brunet 	unsigned int reg, bit, val;
3886ac73095SBeniamino Galvani 	struct meson_bank *bank;
3896ac73095SBeniamino Galvani 	int ret;
3906ac73095SBeniamino Galvani 
39170e5ecb1SJerome Brunet 	ret = meson_get_bank(pc, gpio, &bank);
3926ac73095SBeniamino Galvani 	if (ret)
3936ac73095SBeniamino Galvani 		return ret;
3946ac73095SBeniamino Galvani 
39570e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_IN, &reg, &bit);
396db80f0e1SBeniamino Galvani 	regmap_read(pc->reg_gpio, reg, &val);
3976ac73095SBeniamino Galvani 
3986ac73095SBeniamino Galvani 	return !!(val & BIT(bit));
3996ac73095SBeniamino Galvani }
4006ac73095SBeniamino Galvani 
4016ac73095SBeniamino Galvani static int meson_gpiolib_register(struct meson_pinctrl *pc)
4026ac73095SBeniamino Galvani {
4039dab1868SCarlo Caione 	int ret;
4046ac73095SBeniamino Galvani 
405db80f0e1SBeniamino Galvani 	pc->chip.label = pc->data->name;
406db80f0e1SBeniamino Galvani 	pc->chip.parent = pc->dev;
407634e40b0SJerome Brunet 	pc->chip.request = gpiochip_generic_request;
408634e40b0SJerome Brunet 	pc->chip.free = gpiochip_generic_free;
409db80f0e1SBeniamino Galvani 	pc->chip.direction_input = meson_gpio_direction_input;
410db80f0e1SBeniamino Galvani 	pc->chip.direction_output = meson_gpio_direction_output;
411db80f0e1SBeniamino Galvani 	pc->chip.get = meson_gpio_get;
412db80f0e1SBeniamino Galvani 	pc->chip.set = meson_gpio_set;
413634e40b0SJerome Brunet 	pc->chip.base = -1;
414db80f0e1SBeniamino Galvani 	pc->chip.ngpio = pc->data->num_pins;
415db80f0e1SBeniamino Galvani 	pc->chip.can_sleep = false;
416db80f0e1SBeniamino Galvani 	pc->chip.of_node = pc->of_node;
417db80f0e1SBeniamino Galvani 	pc->chip.of_gpio_n_cells = 2;
4186ac73095SBeniamino Galvani 
419db80f0e1SBeniamino Galvani 	ret = gpiochip_add_data(&pc->chip, pc);
4206ac73095SBeniamino Galvani 	if (ret) {
4216ac73095SBeniamino Galvani 		dev_err(pc->dev, "can't add gpio chip %s\n",
422db80f0e1SBeniamino Galvani 			pc->data->name);
423c7fc5fbaSNeil Armstrong 		return ret;
4246ac73095SBeniamino Galvani 	}
4256ac73095SBeniamino Galvani 
4266ac73095SBeniamino Galvani 	return 0;
4276ac73095SBeniamino Galvani }
4286ac73095SBeniamino Galvani 
4296ac73095SBeniamino Galvani static struct regmap_config meson_regmap_config = {
4306ac73095SBeniamino Galvani 	.reg_bits = 32,
4316ac73095SBeniamino Galvani 	.val_bits = 32,
4326ac73095SBeniamino Galvani 	.reg_stride = 4,
4336ac73095SBeniamino Galvani };
4346ac73095SBeniamino Galvani 
4356ac73095SBeniamino Galvani static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
4366ac73095SBeniamino Galvani 					 struct device_node *node, char *name)
4376ac73095SBeniamino Galvani {
4386ac73095SBeniamino Galvani 	struct resource res;
4396ac73095SBeniamino Galvani 	void __iomem *base;
4406ac73095SBeniamino Galvani 	int i;
4416ac73095SBeniamino Galvani 
4426ac73095SBeniamino Galvani 	i = of_property_match_string(node, "reg-names", name);
4436ac73095SBeniamino Galvani 	if (of_address_to_resource(node, i, &res))
4446ac73095SBeniamino Galvani 		return ERR_PTR(-ENOENT);
4456ac73095SBeniamino Galvani 
4466ac73095SBeniamino Galvani 	base = devm_ioremap_resource(pc->dev, &res);
4476ac73095SBeniamino Galvani 	if (IS_ERR(base))
4486ac73095SBeniamino Galvani 		return ERR_CAST(base);
4496ac73095SBeniamino Galvani 
4506ac73095SBeniamino Galvani 	meson_regmap_config.max_register = resource_size(&res) - 4;
4516ac73095SBeniamino Galvani 	meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
45294f4e54cSRob Herring 						  "%pOFn-%s", node,
4536ac73095SBeniamino Galvani 						  name);
4546ac73095SBeniamino Galvani 	if (!meson_regmap_config.name)
4556ac73095SBeniamino Galvani 		return ERR_PTR(-ENOMEM);
4566ac73095SBeniamino Galvani 
4576ac73095SBeniamino Galvani 	return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
4586ac73095SBeniamino Galvani }
4596ac73095SBeniamino Galvani 
4606ac73095SBeniamino Galvani static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc,
4616ac73095SBeniamino Galvani 				  struct device_node *node)
4626ac73095SBeniamino Galvani {
463db80f0e1SBeniamino Galvani 	struct device_node *np, *gpio_np = NULL;
4646ac73095SBeniamino Galvani 
4656ac73095SBeniamino Galvani 	for_each_child_of_node(node, np) {
4666ac73095SBeniamino Galvani 		if (!of_find_property(np, "gpio-controller", NULL))
4676ac73095SBeniamino Galvani 			continue;
468db80f0e1SBeniamino Galvani 		if (gpio_np) {
469db80f0e1SBeniamino Galvani 			dev_err(pc->dev, "multiple gpio nodes\n");
470db80f0e1SBeniamino Galvani 			return -EINVAL;
471db80f0e1SBeniamino Galvani 		}
472db80f0e1SBeniamino Galvani 		gpio_np = np;
4736ac73095SBeniamino Galvani 	}
4746ac73095SBeniamino Galvani 
475db80f0e1SBeniamino Galvani 	if (!gpio_np) {
476db80f0e1SBeniamino Galvani 		dev_err(pc->dev, "no gpio node found\n");
4776ac73095SBeniamino Galvani 		return -EINVAL;
4786ac73095SBeniamino Galvani 	}
4796ac73095SBeniamino Galvani 
480db80f0e1SBeniamino Galvani 	pc->of_node = gpio_np;
4816ac73095SBeniamino Galvani 
482db80f0e1SBeniamino Galvani 	pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
483db80f0e1SBeniamino Galvani 	if (IS_ERR(pc->reg_mux)) {
4846ac73095SBeniamino Galvani 		dev_err(pc->dev, "mux registers not found\n");
485db80f0e1SBeniamino Galvani 		return PTR_ERR(pc->reg_mux);
4866ac73095SBeniamino Galvani 	}
4876ac73095SBeniamino Galvani 
488db80f0e1SBeniamino Galvani 	pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
489db80f0e1SBeniamino Galvani 	if (IS_ERR(pc->reg_gpio)) {
4906ac73095SBeniamino Galvani 		dev_err(pc->dev, "gpio registers not found\n");
491db80f0e1SBeniamino Galvani 		return PTR_ERR(pc->reg_gpio);
4926ac73095SBeniamino Galvani 	}
4936ac73095SBeniamino Galvani 
494e66dd48eSXingyu Chen 	pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
495e66dd48eSXingyu Chen 	/* Use gpio region if pull one is not present */
496e66dd48eSXingyu Chen 	if (IS_ERR(pc->reg_pull))
497e66dd48eSXingyu Chen 		pc->reg_pull = pc->reg_gpio;
498e66dd48eSXingyu Chen 
499e66dd48eSXingyu Chen 	pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
500e66dd48eSXingyu Chen 	/* Use pull region if pull-enable one is not present */
501e66dd48eSXingyu Chen 	if (IS_ERR(pc->reg_pullen))
502e66dd48eSXingyu Chen 		pc->reg_pullen = pc->reg_pull;
503e66dd48eSXingyu Chen 
50464856974SJerome Brunet 	pc->reg_ds = meson_map_resource(pc, gpio_np, "ds");
50564856974SJerome Brunet 	if (IS_ERR(pc->reg_ds)) {
50664856974SJerome Brunet 		dev_dbg(pc->dev, "ds registers not found - skipping\n");
50764856974SJerome Brunet 		pc->reg_ds = NULL;
50864856974SJerome Brunet 	}
50964856974SJerome Brunet 
5106ac73095SBeniamino Galvani 	return 0;
5116ac73095SBeniamino Galvani }
5126ac73095SBeniamino Galvani 
513277d14ebSJerome Brunet int meson_pinctrl_probe(struct platform_device *pdev)
5146ac73095SBeniamino Galvani {
5156ac73095SBeniamino Galvani 	struct device *dev = &pdev->dev;
5166ac73095SBeniamino Galvani 	struct meson_pinctrl *pc;
5176ac73095SBeniamino Galvani 	int ret;
5186ac73095SBeniamino Galvani 
5196ac73095SBeniamino Galvani 	pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
5206ac73095SBeniamino Galvani 	if (!pc)
5216ac73095SBeniamino Galvani 		return -ENOMEM;
5226ac73095SBeniamino Galvani 
5236ac73095SBeniamino Galvani 	pc->dev = dev;
524277d14ebSJerome Brunet 	pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev);
5256ac73095SBeniamino Galvani 
526277d14ebSJerome Brunet 	ret = meson_pinctrl_parse_dt(pc, dev->of_node);
5276ac73095SBeniamino Galvani 	if (ret)
5286ac73095SBeniamino Galvani 		return ret;
5296ac73095SBeniamino Galvani 
5306ac73095SBeniamino Galvani 	pc->desc.name		= "pinctrl-meson";
5316ac73095SBeniamino Galvani 	pc->desc.owner		= THIS_MODULE;
5326ac73095SBeniamino Galvani 	pc->desc.pctlops	= &meson_pctrl_ops;
533ce385aa2SJerome Brunet 	pc->desc.pmxops		= pc->data->pmx_ops;
5346ac73095SBeniamino Galvani 	pc->desc.confops	= &meson_pinconf_ops;
5356ac73095SBeniamino Galvani 	pc->desc.pins		= pc->data->pins;
5366ac73095SBeniamino Galvani 	pc->desc.npins		= pc->data->num_pins;
5376ac73095SBeniamino Galvani 
538e649f7ecSLaxman Dewangan 	pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
539323de9efSMasahiro Yamada 	if (IS_ERR(pc->pcdev)) {
5406ac73095SBeniamino Galvani 		dev_err(pc->dev, "can't register pinctrl device");
541323de9efSMasahiro Yamada 		return PTR_ERR(pc->pcdev);
5426ac73095SBeniamino Galvani 	}
5436ac73095SBeniamino Galvani 
5445b236d0fSWei Yongjun 	return meson_gpiolib_register(pc);
5456ac73095SBeniamino Galvani }
546