xref: /linux/drivers/pinctrl/meson/pinctrl-meson.c (revision f088ab6d4f4ce49d422c220074b7e605f54e2299)
13c910ecbSThomas 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 
59*f088ab6dSHyeonki Hong static const unsigned int meson_bit_strides[] = {
60*f088ab6dSHyeonki Hong 	1, 1, 1, 1, 1, 2, 1
61*f088ab6dSHyeonki Hong };
62*f088ab6dSHyeonki Hong 
636ac73095SBeniamino Galvani /**
646ac73095SBeniamino Galvani  * meson_get_bank() - find the bank containing a given pin
656ac73095SBeniamino Galvani  *
66db80f0e1SBeniamino Galvani  * @pc:		the pinctrl instance
676ac73095SBeniamino Galvani  * @pin:	the pin number
686ac73095SBeniamino Galvani  * @bank:	the found bank
696ac73095SBeniamino Galvani  *
706ac73095SBeniamino Galvani  * Return:	0 on success, a negative value on error
716ac73095SBeniamino Galvani  */
72db80f0e1SBeniamino Galvani static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
736ac73095SBeniamino Galvani 			  struct meson_bank **bank)
746ac73095SBeniamino Galvani {
756ac73095SBeniamino Galvani 	int i;
766ac73095SBeniamino Galvani 
77db80f0e1SBeniamino Galvani 	for (i = 0; i < pc->data->num_banks; i++) {
78db80f0e1SBeniamino Galvani 		if (pin >= pc->data->banks[i].first &&
79db80f0e1SBeniamino Galvani 		    pin <= pc->data->banks[i].last) {
80db80f0e1SBeniamino Galvani 			*bank = &pc->data->banks[i];
816ac73095SBeniamino Galvani 			return 0;
826ac73095SBeniamino Galvani 		}
836ac73095SBeniamino Galvani 	}
846ac73095SBeniamino Galvani 
856ac73095SBeniamino Galvani 	return -EINVAL;
866ac73095SBeniamino Galvani }
876ac73095SBeniamino Galvani 
886ac73095SBeniamino Galvani /**
896ac73095SBeniamino Galvani  * meson_calc_reg_and_bit() - calculate register and bit for a pin
906ac73095SBeniamino Galvani  *
916ac73095SBeniamino Galvani  * @bank:	the bank containing the pin
926ac73095SBeniamino Galvani  * @pin:	the pin number
936ac73095SBeniamino Galvani  * @reg_type:	the type of register needed (pull-enable, pull, etc...)
946ac73095SBeniamino Galvani  * @reg:	the computed register offset
956ac73095SBeniamino Galvani  * @bit:	the computed bit
966ac73095SBeniamino Galvani  */
976ac73095SBeniamino Galvani static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin,
986ac73095SBeniamino Galvani 				   enum meson_reg_type reg_type,
996ac73095SBeniamino Galvani 				   unsigned int *reg, unsigned int *bit)
1006ac73095SBeniamino Galvani {
1016ac73095SBeniamino Galvani 	struct meson_reg_desc *desc = &bank->regs[reg_type];
1026ac73095SBeniamino Galvani 
103*f088ab6dSHyeonki Hong 	*bit = (desc->bit + pin - bank->first) * meson_bit_strides[reg_type];
104*f088ab6dSHyeonki Hong 	*reg = (desc->reg + (*bit / 32)) * 4;
105*f088ab6dSHyeonki Hong 	*bit &= 0x1f;
1066ac73095SBeniamino Galvani }
1076ac73095SBeniamino Galvani 
1086ac73095SBeniamino Galvani static int meson_get_groups_count(struct pinctrl_dev *pcdev)
1096ac73095SBeniamino Galvani {
1106ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1116ac73095SBeniamino Galvani 
1126ac73095SBeniamino Galvani 	return pc->data->num_groups;
1136ac73095SBeniamino Galvani }
1146ac73095SBeniamino Galvani 
1156ac73095SBeniamino Galvani static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
1166ac73095SBeniamino Galvani 					unsigned selector)
1176ac73095SBeniamino Galvani {
1186ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1196ac73095SBeniamino Galvani 
1206ac73095SBeniamino Galvani 	return pc->data->groups[selector].name;
1216ac73095SBeniamino Galvani }
1226ac73095SBeniamino Galvani 
1236ac73095SBeniamino Galvani static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
1246ac73095SBeniamino Galvani 				const unsigned **pins, unsigned *num_pins)
1256ac73095SBeniamino Galvani {
1266ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1276ac73095SBeniamino Galvani 
1286ac73095SBeniamino Galvani 	*pins = pc->data->groups[selector].pins;
1296ac73095SBeniamino Galvani 	*num_pins = pc->data->groups[selector].num_pins;
1306ac73095SBeniamino Galvani 
1316ac73095SBeniamino Galvani 	return 0;
1326ac73095SBeniamino Galvani }
1336ac73095SBeniamino Galvani 
1346ac73095SBeniamino Galvani static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
1356ac73095SBeniamino Galvani 			       unsigned offset)
1366ac73095SBeniamino Galvani {
1376ac73095SBeniamino Galvani 	seq_printf(s, " %s", dev_name(pcdev->dev));
1386ac73095SBeniamino Galvani }
1396ac73095SBeniamino Galvani 
1406ac73095SBeniamino Galvani static const struct pinctrl_ops meson_pctrl_ops = {
1416ac73095SBeniamino Galvani 	.get_groups_count	= meson_get_groups_count,
1426ac73095SBeniamino Galvani 	.get_group_name		= meson_get_group_name,
1436ac73095SBeniamino Galvani 	.get_group_pins		= meson_get_group_pins,
1446ac73095SBeniamino Galvani 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_all,
145d32f7fd3SIrina Tirdea 	.dt_free_map		= pinctrl_utils_free_map,
1466ac73095SBeniamino Galvani 	.pin_dbg_show		= meson_pin_dbg_show,
1476ac73095SBeniamino Galvani };
1486ac73095SBeniamino Galvani 
149ce385aa2SJerome Brunet int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
1506ac73095SBeniamino Galvani {
1516ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1526ac73095SBeniamino Galvani 
1536ac73095SBeniamino Galvani 	return pc->data->num_funcs;
1546ac73095SBeniamino Galvani }
1556ac73095SBeniamino Galvani 
156ce385aa2SJerome Brunet const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
1576ac73095SBeniamino Galvani 				    unsigned selector)
1586ac73095SBeniamino Galvani {
1596ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1606ac73095SBeniamino Galvani 
1616ac73095SBeniamino Galvani 	return pc->data->funcs[selector].name;
1626ac73095SBeniamino Galvani }
1636ac73095SBeniamino Galvani 
164ce385aa2SJerome Brunet int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
1656ac73095SBeniamino Galvani 			 const char * const **groups,
1666ac73095SBeniamino Galvani 			 unsigned * const num_groups)
1676ac73095SBeniamino Galvani {
1686ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1696ac73095SBeniamino Galvani 
1706ac73095SBeniamino Galvani 	*groups = pc->data->funcs[selector].groups;
1716ac73095SBeniamino Galvani 	*num_groups = pc->data->funcs[selector].num_groups;
1726ac73095SBeniamino Galvani 
1736ac73095SBeniamino Galvani 	return 0;
1746ac73095SBeniamino Galvani }
1756ac73095SBeniamino Galvani 
176b22a7f85SJerome Brunet static int meson_pinconf_set_gpio_bit(struct meson_pinctrl *pc,
177b22a7f85SJerome Brunet 				      unsigned int pin,
178b22a7f85SJerome Brunet 				      unsigned int reg_type,
179b22a7f85SJerome Brunet 				      bool arg)
1806ac73095SBeniamino Galvani {
1816ac73095SBeniamino Galvani 	struct meson_bank *bank;
1826ac73095SBeniamino Galvani 	unsigned int reg, bit;
183b22a7f85SJerome Brunet 	int ret;
1846ac73095SBeniamino Galvani 
185db80f0e1SBeniamino Galvani 	ret = meson_get_bank(pc, pin, &bank);
1866ac73095SBeniamino Galvani 	if (ret)
1876ac73095SBeniamino Galvani 		return ret;
1886ac73095SBeniamino Galvani 
189b22a7f85SJerome Brunet 	meson_calc_reg_and_bit(bank, pin, reg_type, &reg, &bit);
190b22a7f85SJerome Brunet 	return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
191b22a7f85SJerome Brunet 				  arg ? BIT(bit) : 0);
192b22a7f85SJerome Brunet }
193b22a7f85SJerome Brunet 
194b22a7f85SJerome Brunet static int meson_pinconf_get_gpio_bit(struct meson_pinctrl *pc,
195b22a7f85SJerome Brunet 				      unsigned int pin,
196b22a7f85SJerome Brunet 				      unsigned int reg_type)
197b22a7f85SJerome Brunet {
198b22a7f85SJerome Brunet 	struct meson_bank *bank;
199b22a7f85SJerome Brunet 	unsigned int reg, bit, val;
200b22a7f85SJerome Brunet 	int ret;
201b22a7f85SJerome Brunet 
202b22a7f85SJerome Brunet 	ret = meson_get_bank(pc, pin, &bank);
203b22a7f85SJerome Brunet 	if (ret)
204b22a7f85SJerome Brunet 		return ret;
205b22a7f85SJerome Brunet 
206b22a7f85SJerome Brunet 	meson_calc_reg_and_bit(bank, pin, reg_type, &reg, &bit);
207b22a7f85SJerome Brunet 	ret = regmap_read(pc->reg_gpio, reg, &val);
208b22a7f85SJerome Brunet 	if (ret)
209b22a7f85SJerome Brunet 		return ret;
210b22a7f85SJerome Brunet 
211b22a7f85SJerome Brunet 	return BIT(bit) & val ? 1 : 0;
212b22a7f85SJerome Brunet }
213b22a7f85SJerome Brunet 
214b22a7f85SJerome Brunet static int meson_pinconf_set_output(struct meson_pinctrl *pc,
215b22a7f85SJerome Brunet 				    unsigned int pin,
216b22a7f85SJerome Brunet 				    bool out)
217b22a7f85SJerome Brunet {
218b22a7f85SJerome Brunet 	return meson_pinconf_set_gpio_bit(pc, pin, REG_DIR, !out);
219b22a7f85SJerome Brunet }
220b22a7f85SJerome Brunet 
221b22a7f85SJerome Brunet static int meson_pinconf_get_output(struct meson_pinctrl *pc,
222b22a7f85SJerome Brunet 				    unsigned int pin)
223b22a7f85SJerome Brunet {
224b22a7f85SJerome Brunet 	int ret = meson_pinconf_get_gpio_bit(pc, pin, REG_DIR);
225b22a7f85SJerome Brunet 
226b22a7f85SJerome Brunet 	if (ret < 0)
227b22a7f85SJerome Brunet 		return ret;
228b22a7f85SJerome Brunet 
229b22a7f85SJerome Brunet 	return !ret;
230b22a7f85SJerome Brunet }
231b22a7f85SJerome Brunet 
232b22a7f85SJerome Brunet static int meson_pinconf_set_drive(struct meson_pinctrl *pc,
233b22a7f85SJerome Brunet 				   unsigned int pin,
234b22a7f85SJerome Brunet 				   bool high)
235b22a7f85SJerome Brunet {
236b22a7f85SJerome Brunet 	return meson_pinconf_set_gpio_bit(pc, pin, REG_OUT, high);
237b22a7f85SJerome Brunet }
238b22a7f85SJerome Brunet 
239b22a7f85SJerome Brunet static int meson_pinconf_get_drive(struct meson_pinctrl *pc,
240b22a7f85SJerome Brunet 				   unsigned int pin)
241b22a7f85SJerome Brunet {
242b22a7f85SJerome Brunet 	return meson_pinconf_get_gpio_bit(pc, pin, REG_OUT);
243b22a7f85SJerome Brunet }
244b22a7f85SJerome Brunet 
245b22a7f85SJerome Brunet static int meson_pinconf_set_output_drive(struct meson_pinctrl *pc,
246b22a7f85SJerome Brunet 					  unsigned int pin,
247b22a7f85SJerome Brunet 					  bool high)
248b22a7f85SJerome Brunet {
249b22a7f85SJerome Brunet 	int ret;
250b22a7f85SJerome Brunet 
251b22a7f85SJerome Brunet 	ret = meson_pinconf_set_output(pc, pin, true);
252b22a7f85SJerome Brunet 	if (ret)
253b22a7f85SJerome Brunet 		return ret;
254b22a7f85SJerome Brunet 
255b22a7f85SJerome Brunet 	return meson_pinconf_set_drive(pc, pin, high);
256b22a7f85SJerome Brunet }
257b22a7f85SJerome Brunet 
2589959d9a7SGuillaume La Roque static int meson_pinconf_disable_bias(struct meson_pinctrl *pc,
2599959d9a7SGuillaume La Roque 				      unsigned int pin)
2606ac73095SBeniamino Galvani {
2616ac73095SBeniamino Galvani 	struct meson_bank *bank;
2629959d9a7SGuillaume La Roque 	unsigned int reg, bit = 0;
2639959d9a7SGuillaume La Roque 	int ret;
2646ac73095SBeniamino Galvani 
2656ac73095SBeniamino Galvani 	ret = meson_get_bank(pc, pin, &bank);
2666ac73095SBeniamino Galvani 	if (ret)
2676ac73095SBeniamino Galvani 		return ret;
2686ac73095SBeniamino Galvani 
2699959d9a7SGuillaume La Roque 	meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
2709959d9a7SGuillaume La Roque 	ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), 0);
2719959d9a7SGuillaume La Roque 	if (ret)
2729959d9a7SGuillaume La Roque 		return ret;
2739959d9a7SGuillaume La Roque 
2749959d9a7SGuillaume La Roque 	return 0;
2759959d9a7SGuillaume La Roque }
2769959d9a7SGuillaume La Roque 
2779959d9a7SGuillaume La Roque static int meson_pinconf_enable_bias(struct meson_pinctrl *pc, unsigned int pin,
2789959d9a7SGuillaume La Roque 				     bool pull_up)
2799959d9a7SGuillaume La Roque {
2809959d9a7SGuillaume La Roque 	struct meson_bank *bank;
2819959d9a7SGuillaume La Roque 	unsigned int reg, bit, val = 0;
2829959d9a7SGuillaume La Roque 	int ret;
2839959d9a7SGuillaume La Roque 
2849959d9a7SGuillaume La Roque 	ret = meson_get_bank(pc, pin, &bank);
2859959d9a7SGuillaume La Roque 	if (ret)
2869959d9a7SGuillaume La Roque 		return ret;
2879959d9a7SGuillaume La Roque 
2889959d9a7SGuillaume La Roque 	meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
2899959d9a7SGuillaume La Roque 	if (pull_up)
2909959d9a7SGuillaume La Roque 		val = BIT(bit);
2919959d9a7SGuillaume La Roque 
2929959d9a7SGuillaume La Roque 	ret = regmap_update_bits(pc->reg_pull, reg, BIT(bit), val);
2939959d9a7SGuillaume La Roque 	if (ret)
2949959d9a7SGuillaume La Roque 		return ret;
2959959d9a7SGuillaume La Roque 
2969959d9a7SGuillaume La Roque 	meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
2979959d9a7SGuillaume La Roque 	ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit),	BIT(bit));
2989959d9a7SGuillaume La Roque 	if (ret)
2999959d9a7SGuillaume La Roque 		return ret;
3009959d9a7SGuillaume La Roque 
3019959d9a7SGuillaume La Roque 	return 0;
3029959d9a7SGuillaume La Roque }
3039959d9a7SGuillaume La Roque 
3046ea3e3bbSGuillaume La Roque static int meson_pinconf_set_drive_strength(struct meson_pinctrl *pc,
3056ea3e3bbSGuillaume La Roque 					    unsigned int pin,
3066ea3e3bbSGuillaume La Roque 					    u16 drive_strength_ua)
3076ea3e3bbSGuillaume La Roque {
3086ea3e3bbSGuillaume La Roque 	struct meson_bank *bank;
3096ea3e3bbSGuillaume La Roque 	unsigned int reg, bit, ds_val;
3106ea3e3bbSGuillaume La Roque 	int ret;
3116ea3e3bbSGuillaume La Roque 
3126ea3e3bbSGuillaume La Roque 	if (!pc->reg_ds) {
3136ea3e3bbSGuillaume La Roque 		dev_err(pc->dev, "drive-strength not supported\n");
3146ea3e3bbSGuillaume La Roque 		return -ENOTSUPP;
3156ea3e3bbSGuillaume La Roque 	}
3166ea3e3bbSGuillaume La Roque 
3176ea3e3bbSGuillaume La Roque 	ret = meson_get_bank(pc, pin, &bank);
3186ea3e3bbSGuillaume La Roque 	if (ret)
3196ea3e3bbSGuillaume La Roque 		return ret;
3206ea3e3bbSGuillaume La Roque 
3216ea3e3bbSGuillaume La Roque 	meson_calc_reg_and_bit(bank, pin, REG_DS, &reg, &bit);
3226ea3e3bbSGuillaume La Roque 
3236ea3e3bbSGuillaume La Roque 	if (drive_strength_ua <= 500) {
3246ea3e3bbSGuillaume La Roque 		ds_val = MESON_PINCONF_DRV_500UA;
3256ea3e3bbSGuillaume La Roque 	} else if (drive_strength_ua <= 2500) {
3266ea3e3bbSGuillaume La Roque 		ds_val = MESON_PINCONF_DRV_2500UA;
3276ea3e3bbSGuillaume La Roque 	} else if (drive_strength_ua <= 3000) {
3286ea3e3bbSGuillaume La Roque 		ds_val = MESON_PINCONF_DRV_3000UA;
3296ea3e3bbSGuillaume La Roque 	} else if (drive_strength_ua <= 4000) {
3306ea3e3bbSGuillaume La Roque 		ds_val = MESON_PINCONF_DRV_4000UA;
3316ea3e3bbSGuillaume La Roque 	} else {
3326ea3e3bbSGuillaume La Roque 		dev_warn_once(pc->dev,
3336ea3e3bbSGuillaume La Roque 			      "pin %u: invalid drive-strength : %d , default to 4mA\n",
3346ea3e3bbSGuillaume La Roque 			      pin, drive_strength_ua);
3356ea3e3bbSGuillaume La Roque 		ds_val = MESON_PINCONF_DRV_4000UA;
3366ea3e3bbSGuillaume La Roque 	}
3376ea3e3bbSGuillaume La Roque 
3386ea3e3bbSGuillaume La Roque 	ret = regmap_update_bits(pc->reg_ds, reg, 0x3 << bit, ds_val << bit);
3396ea3e3bbSGuillaume La Roque 	if (ret)
3406ea3e3bbSGuillaume La Roque 		return ret;
3416ea3e3bbSGuillaume La Roque 
3426ea3e3bbSGuillaume La Roque 	return 0;
3436ea3e3bbSGuillaume La Roque }
3446ea3e3bbSGuillaume La Roque 
3459959d9a7SGuillaume La Roque static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
3469959d9a7SGuillaume La Roque 			     unsigned long *configs, unsigned num_configs)
3479959d9a7SGuillaume La Roque {
3489959d9a7SGuillaume La Roque 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
3499959d9a7SGuillaume La Roque 	enum pin_config_param param;
350b22a7f85SJerome Brunet 	unsigned int arg = 0;
3519959d9a7SGuillaume La Roque 	int i, ret;
3529959d9a7SGuillaume La Roque 
3536ac73095SBeniamino Galvani 	for (i = 0; i < num_configs; i++) {
3546ac73095SBeniamino Galvani 		param = pinconf_to_config_param(configs[i]);
3556ac73095SBeniamino Galvani 
3566ac73095SBeniamino Galvani 		switch (param) {
357b22a7f85SJerome Brunet 		case PIN_CONFIG_DRIVE_STRENGTH_UA:
358b22a7f85SJerome Brunet 		case PIN_CONFIG_OUTPUT_ENABLE:
359b22a7f85SJerome Brunet 		case PIN_CONFIG_OUTPUT:
360b22a7f85SJerome Brunet 			arg = pinconf_to_config_argument(configs[i]);
361b22a7f85SJerome Brunet 			break;
3626ac73095SBeniamino Galvani 
363b22a7f85SJerome Brunet 		default:
364b22a7f85SJerome Brunet 			break;
365b22a7f85SJerome Brunet 		}
366b22a7f85SJerome Brunet 
367b22a7f85SJerome Brunet 		switch (param) {
3686ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_DISABLE:
3699959d9a7SGuillaume La Roque 			ret = meson_pinconf_disable_bias(pc, pin);
3706ac73095SBeniamino Galvani 			break;
3716ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_PULL_UP:
3729959d9a7SGuillaume La Roque 			ret = meson_pinconf_enable_bias(pc, pin, true);
3736ac73095SBeniamino Galvani 			break;
3746ac73095SBeniamino Galvani 		case PIN_CONFIG_BIAS_PULL_DOWN:
3759959d9a7SGuillaume La Roque 			ret = meson_pinconf_enable_bias(pc, pin, false);
3766ac73095SBeniamino Galvani 			break;
3776ea3e3bbSGuillaume La Roque 		case PIN_CONFIG_DRIVE_STRENGTH_UA:
378b22a7f85SJerome Brunet 			ret = meson_pinconf_set_drive_strength(pc, pin, arg);
379b22a7f85SJerome Brunet 			break;
380b22a7f85SJerome Brunet 		case PIN_CONFIG_OUTPUT_ENABLE:
381b22a7f85SJerome Brunet 			ret = meson_pinconf_set_output(pc, pin, arg);
382b22a7f85SJerome Brunet 			break;
383b22a7f85SJerome Brunet 		case PIN_CONFIG_OUTPUT:
384b22a7f85SJerome Brunet 			ret = meson_pinconf_set_output_drive(pc, pin, arg);
3856ac73095SBeniamino Galvani 			break;
3866ac73095SBeniamino Galvani 		default:
387b22a7f85SJerome Brunet 			ret = -ENOTSUPP;
3886ac73095SBeniamino Galvani 		}
389b22a7f85SJerome Brunet 
390b22a7f85SJerome Brunet 		if (ret)
391b22a7f85SJerome Brunet 			return ret;
3926ac73095SBeniamino Galvani 	}
3936ac73095SBeniamino Galvani 
3946ac73095SBeniamino Galvani 	return 0;
3956ac73095SBeniamino Galvani }
3966ac73095SBeniamino Galvani 
3976ac73095SBeniamino Galvani static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
3986ac73095SBeniamino Galvani {
3996ac73095SBeniamino Galvani 	struct meson_bank *bank;
4006ac73095SBeniamino Galvani 	unsigned int reg, bit, val;
4016ac73095SBeniamino Galvani 	int ret, conf;
4026ac73095SBeniamino Galvani 
403db80f0e1SBeniamino Galvani 	ret = meson_get_bank(pc, pin, &bank);
4046ac73095SBeniamino Galvani 	if (ret)
4056ac73095SBeniamino Galvani 		return ret;
4066ac73095SBeniamino Galvani 
4076ac73095SBeniamino Galvani 	meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
4086ac73095SBeniamino Galvani 
409db80f0e1SBeniamino Galvani 	ret = regmap_read(pc->reg_pullen, reg, &val);
4106ac73095SBeniamino Galvani 	if (ret)
4116ac73095SBeniamino Galvani 		return ret;
4126ac73095SBeniamino Galvani 
4136ac73095SBeniamino Galvani 	if (!(val & BIT(bit))) {
4146ac73095SBeniamino Galvani 		conf = PIN_CONFIG_BIAS_DISABLE;
4156ac73095SBeniamino Galvani 	} else {
4166ac73095SBeniamino Galvani 		meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
4176ac73095SBeniamino Galvani 
418db80f0e1SBeniamino Galvani 		ret = regmap_read(pc->reg_pull, reg, &val);
4196ac73095SBeniamino Galvani 		if (ret)
4206ac73095SBeniamino Galvani 			return ret;
4216ac73095SBeniamino Galvani 
4226ac73095SBeniamino Galvani 		if (val & BIT(bit))
4236ac73095SBeniamino Galvani 			conf = PIN_CONFIG_BIAS_PULL_UP;
4246ac73095SBeniamino Galvani 		else
4256ac73095SBeniamino Galvani 			conf = PIN_CONFIG_BIAS_PULL_DOWN;
4266ac73095SBeniamino Galvani 	}
4276ac73095SBeniamino Galvani 
4286ac73095SBeniamino Galvani 	return conf;
4296ac73095SBeniamino Galvani }
4306ac73095SBeniamino Galvani 
4316ea3e3bbSGuillaume La Roque static int meson_pinconf_get_drive_strength(struct meson_pinctrl *pc,
4326ea3e3bbSGuillaume La Roque 					    unsigned int pin,
4336ea3e3bbSGuillaume La Roque 					    u16 *drive_strength_ua)
4346ea3e3bbSGuillaume La Roque {
4356ea3e3bbSGuillaume La Roque 	struct meson_bank *bank;
4366ea3e3bbSGuillaume La Roque 	unsigned int reg, bit;
4376ea3e3bbSGuillaume La Roque 	unsigned int val;
4386ea3e3bbSGuillaume La Roque 	int ret;
4396ea3e3bbSGuillaume La Roque 
4406ea3e3bbSGuillaume La Roque 	if (!pc->reg_ds)
4416ea3e3bbSGuillaume La Roque 		return -ENOTSUPP;
4426ea3e3bbSGuillaume La Roque 
4436ea3e3bbSGuillaume La Roque 	ret = meson_get_bank(pc, pin, &bank);
4446ea3e3bbSGuillaume La Roque 	if (ret)
4456ea3e3bbSGuillaume La Roque 		return ret;
4466ea3e3bbSGuillaume La Roque 
4476ea3e3bbSGuillaume La Roque 	meson_calc_reg_and_bit(bank, pin, REG_DS, &reg, &bit);
4486ea3e3bbSGuillaume La Roque 
4496ea3e3bbSGuillaume La Roque 	ret = regmap_read(pc->reg_ds, reg, &val);
4506ea3e3bbSGuillaume La Roque 	if (ret)
4516ea3e3bbSGuillaume La Roque 		return ret;
4526ea3e3bbSGuillaume La Roque 
4536ea3e3bbSGuillaume La Roque 	switch ((val >> bit) & 0x3) {
4546ea3e3bbSGuillaume La Roque 	case MESON_PINCONF_DRV_500UA:
4556ea3e3bbSGuillaume La Roque 		*drive_strength_ua = 500;
4566ea3e3bbSGuillaume La Roque 		break;
4576ea3e3bbSGuillaume La Roque 	case MESON_PINCONF_DRV_2500UA:
4586ea3e3bbSGuillaume La Roque 		*drive_strength_ua = 2500;
4596ea3e3bbSGuillaume La Roque 		break;
4606ea3e3bbSGuillaume La Roque 	case MESON_PINCONF_DRV_3000UA:
4616ea3e3bbSGuillaume La Roque 		*drive_strength_ua = 3000;
4626ea3e3bbSGuillaume La Roque 		break;
4636ea3e3bbSGuillaume La Roque 	case MESON_PINCONF_DRV_4000UA:
4646ea3e3bbSGuillaume La Roque 		*drive_strength_ua = 4000;
4656ea3e3bbSGuillaume La Roque 		break;
4666ea3e3bbSGuillaume La Roque 	default:
4676ea3e3bbSGuillaume La Roque 		return -EINVAL;
4686ea3e3bbSGuillaume La Roque 	}
4696ea3e3bbSGuillaume La Roque 
4706ea3e3bbSGuillaume La Roque 	return 0;
4716ea3e3bbSGuillaume La Roque }
4726ea3e3bbSGuillaume La Roque 
4736ac73095SBeniamino Galvani static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
4746ac73095SBeniamino Galvani 			     unsigned long *config)
4756ac73095SBeniamino Galvani {
4766ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
4776ac73095SBeniamino Galvani 	enum pin_config_param param = pinconf_to_config_param(*config);
4786ac73095SBeniamino Galvani 	u16 arg;
4796ea3e3bbSGuillaume La Roque 	int ret;
4806ac73095SBeniamino Galvani 
4816ac73095SBeniamino Galvani 	switch (param) {
4826ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_DISABLE:
4836ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_PULL_DOWN:
4846ac73095SBeniamino Galvani 	case PIN_CONFIG_BIAS_PULL_UP:
4856ac73095SBeniamino Galvani 		if (meson_pinconf_get_pull(pc, pin) == param)
4866ac73095SBeniamino Galvani 			arg = 1;
4876ac73095SBeniamino Galvani 		else
4886ac73095SBeniamino Galvani 			return -EINVAL;
4896ac73095SBeniamino Galvani 		break;
4906ea3e3bbSGuillaume La Roque 	case PIN_CONFIG_DRIVE_STRENGTH_UA:
4916ea3e3bbSGuillaume La Roque 		ret = meson_pinconf_get_drive_strength(pc, pin, &arg);
4926ea3e3bbSGuillaume La Roque 		if (ret)
4936ea3e3bbSGuillaume La Roque 			return ret;
4946ea3e3bbSGuillaume La Roque 		break;
495b22a7f85SJerome Brunet 	case PIN_CONFIG_OUTPUT_ENABLE:
496b22a7f85SJerome Brunet 		ret = meson_pinconf_get_output(pc, pin);
497b22a7f85SJerome Brunet 		if (ret <= 0)
498b22a7f85SJerome Brunet 			return -EINVAL;
499b22a7f85SJerome Brunet 		arg = 1;
500b22a7f85SJerome Brunet 		break;
501b22a7f85SJerome Brunet 	case PIN_CONFIG_OUTPUT:
502b22a7f85SJerome Brunet 		ret = meson_pinconf_get_output(pc, pin);
503b22a7f85SJerome Brunet 		if (ret <= 0)
504b22a7f85SJerome Brunet 			return -EINVAL;
505b22a7f85SJerome Brunet 
506b22a7f85SJerome Brunet 		ret = meson_pinconf_get_drive(pc, pin);
507b22a7f85SJerome Brunet 		if (ret < 0)
508b22a7f85SJerome Brunet 			return -EINVAL;
509b22a7f85SJerome Brunet 
510b22a7f85SJerome Brunet 		arg = ret;
511b22a7f85SJerome Brunet 		break;
512b22a7f85SJerome Brunet 
5136ac73095SBeniamino Galvani 	default:
5146ac73095SBeniamino Galvani 		return -ENOTSUPP;
5156ac73095SBeniamino Galvani 	}
5166ac73095SBeniamino Galvani 
5176ac73095SBeniamino Galvani 	*config = pinconf_to_config_packed(param, arg);
5186ac73095SBeniamino Galvani 	dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
5196ac73095SBeniamino Galvani 
5206ac73095SBeniamino Galvani 	return 0;
5216ac73095SBeniamino Galvani }
5226ac73095SBeniamino Galvani 
5236ac73095SBeniamino Galvani static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
5246ac73095SBeniamino Galvani 				   unsigned int num_group,
5256ac73095SBeniamino Galvani 				   unsigned long *configs, unsigned num_configs)
5266ac73095SBeniamino Galvani {
5276ac73095SBeniamino Galvani 	struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
5286ac73095SBeniamino Galvani 	struct meson_pmx_group *group = &pc->data->groups[num_group];
5296ac73095SBeniamino Galvani 	int i;
5306ac73095SBeniamino Galvani 
5316ac73095SBeniamino Galvani 	dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
5326ac73095SBeniamino Galvani 
5336ac73095SBeniamino Galvani 	for (i = 0; i < group->num_pins; i++) {
5346ac73095SBeniamino Galvani 		meson_pinconf_set(pcdev, group->pins[i], configs,
5356ac73095SBeniamino Galvani 				  num_configs);
5366ac73095SBeniamino Galvani 	}
5376ac73095SBeniamino Galvani 
5386ac73095SBeniamino Galvani 	return 0;
5396ac73095SBeniamino Galvani }
5406ac73095SBeniamino Galvani 
5416ac73095SBeniamino Galvani static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
5426ac73095SBeniamino Galvani 				   unsigned int group, unsigned long *config)
5436ac73095SBeniamino Galvani {
5441ffbf50bSJerome Brunet 	return -ENOTSUPP;
5456ac73095SBeniamino Galvani }
5466ac73095SBeniamino Galvani 
5476ac73095SBeniamino Galvani static const struct pinconf_ops meson_pinconf_ops = {
5486ac73095SBeniamino Galvani 	.pin_config_get		= meson_pinconf_get,
5496ac73095SBeniamino Galvani 	.pin_config_set		= meson_pinconf_set,
5506ac73095SBeniamino Galvani 	.pin_config_group_get	= meson_pinconf_group_get,
5516ac73095SBeniamino Galvani 	.pin_config_group_set	= meson_pinconf_group_set,
5526ac73095SBeniamino Galvani 	.is_generic		= true,
5536ac73095SBeniamino Galvani };
5546ac73095SBeniamino Galvani 
555ef1d0bceSMartin Blumenstingl static int meson_gpio_get_direction(struct gpio_chip *chip, unsigned gpio)
556ef1d0bceSMartin Blumenstingl {
557ef1d0bceSMartin Blumenstingl 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
558ef1d0bceSMartin Blumenstingl 	int ret;
559ef1d0bceSMartin Blumenstingl 
560ef1d0bceSMartin Blumenstingl 	ret = meson_pinconf_get_output(pc, gpio);
561ef1d0bceSMartin Blumenstingl 	if (ret < 0)
562ef1d0bceSMartin Blumenstingl 		return ret;
563ef1d0bceSMartin Blumenstingl 
564ef1d0bceSMartin Blumenstingl 	return ret ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
565ef1d0bceSMartin Blumenstingl }
566ef1d0bceSMartin Blumenstingl 
5676ac73095SBeniamino Galvani static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
5686ac73095SBeniamino Galvani {
569b22a7f85SJerome Brunet 	return meson_pinconf_set_output(gpiochip_get_data(chip), gpio, false);
5706ac73095SBeniamino Galvani }
5716ac73095SBeniamino Galvani 
5726ac73095SBeniamino Galvani static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
5736ac73095SBeniamino Galvani 				       int value)
5746ac73095SBeniamino Galvani {
575b22a7f85SJerome Brunet 	return meson_pinconf_set_output_drive(gpiochip_get_data(chip),
576b22a7f85SJerome Brunet 					      gpio, value);
5776ac73095SBeniamino Galvani }
5786ac73095SBeniamino Galvani 
5796ac73095SBeniamino Galvani static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
5806ac73095SBeniamino Galvani {
581b22a7f85SJerome Brunet 	meson_pinconf_set_drive(gpiochip_get_data(chip), gpio, value);
5826ac73095SBeniamino Galvani }
5836ac73095SBeniamino Galvani 
5846ac73095SBeniamino Galvani static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
5856ac73095SBeniamino Galvani {
586db80f0e1SBeniamino Galvani 	struct meson_pinctrl *pc = gpiochip_get_data(chip);
58770e5ecb1SJerome Brunet 	unsigned int reg, bit, val;
5886ac73095SBeniamino Galvani 	struct meson_bank *bank;
5896ac73095SBeniamino Galvani 	int ret;
5906ac73095SBeniamino Galvani 
59170e5ecb1SJerome Brunet 	ret = meson_get_bank(pc, gpio, &bank);
5926ac73095SBeniamino Galvani 	if (ret)
5936ac73095SBeniamino Galvani 		return ret;
5946ac73095SBeniamino Galvani 
59570e5ecb1SJerome Brunet 	meson_calc_reg_and_bit(bank, gpio, REG_IN, &reg, &bit);
596db80f0e1SBeniamino Galvani 	regmap_read(pc->reg_gpio, reg, &val);
5976ac73095SBeniamino Galvani 
5986ac73095SBeniamino Galvani 	return !!(val & BIT(bit));
5996ac73095SBeniamino Galvani }
6006ac73095SBeniamino Galvani 
6016ac73095SBeniamino Galvani static int meson_gpiolib_register(struct meson_pinctrl *pc)
6026ac73095SBeniamino Galvani {
6039dab1868SCarlo Caione 	int ret;
6046ac73095SBeniamino Galvani 
605db80f0e1SBeniamino Galvani 	pc->chip.label = pc->data->name;
606db80f0e1SBeniamino Galvani 	pc->chip.parent = pc->dev;
607634e40b0SJerome Brunet 	pc->chip.request = gpiochip_generic_request;
608634e40b0SJerome Brunet 	pc->chip.free = gpiochip_generic_free;
609f8f0aa00SMartin Blumenstingl 	pc->chip.set_config = gpiochip_generic_config;
610ef1d0bceSMartin Blumenstingl 	pc->chip.get_direction = meson_gpio_get_direction;
611db80f0e1SBeniamino Galvani 	pc->chip.direction_input = meson_gpio_direction_input;
612db80f0e1SBeniamino Galvani 	pc->chip.direction_output = meson_gpio_direction_output;
613db80f0e1SBeniamino Galvani 	pc->chip.get = meson_gpio_get;
614db80f0e1SBeniamino Galvani 	pc->chip.set = meson_gpio_set;
615634e40b0SJerome Brunet 	pc->chip.base = -1;
616db80f0e1SBeniamino Galvani 	pc->chip.ngpio = pc->data->num_pins;
617db80f0e1SBeniamino Galvani 	pc->chip.can_sleep = false;
618db80f0e1SBeniamino Galvani 	pc->chip.of_node = pc->of_node;
619db80f0e1SBeniamino Galvani 	pc->chip.of_gpio_n_cells = 2;
6206ac73095SBeniamino Galvani 
621db80f0e1SBeniamino Galvani 	ret = gpiochip_add_data(&pc->chip, pc);
6226ac73095SBeniamino Galvani 	if (ret) {
6236ac73095SBeniamino Galvani 		dev_err(pc->dev, "can't add gpio chip %s\n",
624db80f0e1SBeniamino Galvani 			pc->data->name);
625c7fc5fbaSNeil Armstrong 		return ret;
6266ac73095SBeniamino Galvani 	}
6276ac73095SBeniamino Galvani 
6286ac73095SBeniamino Galvani 	return 0;
6296ac73095SBeniamino Galvani }
6306ac73095SBeniamino Galvani 
6316ac73095SBeniamino Galvani static struct regmap_config meson_regmap_config = {
6326ac73095SBeniamino Galvani 	.reg_bits = 32,
6336ac73095SBeniamino Galvani 	.val_bits = 32,
6346ac73095SBeniamino Galvani 	.reg_stride = 4,
6356ac73095SBeniamino Galvani };
6366ac73095SBeniamino Galvani 
6376ac73095SBeniamino Galvani static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
6386ac73095SBeniamino Galvani 					 struct device_node *node, char *name)
6396ac73095SBeniamino Galvani {
6406ac73095SBeniamino Galvani 	struct resource res;
6416ac73095SBeniamino Galvani 	void __iomem *base;
6426ac73095SBeniamino Galvani 	int i;
6436ac73095SBeniamino Galvani 
6446ac73095SBeniamino Galvani 	i = of_property_match_string(node, "reg-names", name);
6456ac73095SBeniamino Galvani 	if (of_address_to_resource(node, i, &res))
646fd422964SQianggui Song 		return NULL;
6476ac73095SBeniamino Galvani 
6486ac73095SBeniamino Galvani 	base = devm_ioremap_resource(pc->dev, &res);
6496ac73095SBeniamino Galvani 	if (IS_ERR(base))
6506ac73095SBeniamino Galvani 		return ERR_CAST(base);
6516ac73095SBeniamino Galvani 
6526ac73095SBeniamino Galvani 	meson_regmap_config.max_register = resource_size(&res) - 4;
6536ac73095SBeniamino Galvani 	meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
65494f4e54cSRob Herring 						  "%pOFn-%s", node,
6556ac73095SBeniamino Galvani 						  name);
6566ac73095SBeniamino Galvani 	if (!meson_regmap_config.name)
6576ac73095SBeniamino Galvani 		return ERR_PTR(-ENOMEM);
6586ac73095SBeniamino Galvani 
6596ac73095SBeniamino Galvani 	return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
6606ac73095SBeniamino Galvani }
6616ac73095SBeniamino Galvani 
6626ac73095SBeniamino Galvani static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc,
6636ac73095SBeniamino Galvani 				  struct device_node *node)
6646ac73095SBeniamino Galvani {
665db80f0e1SBeniamino Galvani 	struct device_node *np, *gpio_np = NULL;
6666ac73095SBeniamino Galvani 
6676ac73095SBeniamino Galvani 	for_each_child_of_node(node, np) {
6686ac73095SBeniamino Galvani 		if (!of_find_property(np, "gpio-controller", NULL))
6696ac73095SBeniamino Galvani 			continue;
670db80f0e1SBeniamino Galvani 		if (gpio_np) {
671db80f0e1SBeniamino Galvani 			dev_err(pc->dev, "multiple gpio nodes\n");
6722ff110bbSNishka Dasgupta 			of_node_put(np);
673db80f0e1SBeniamino Galvani 			return -EINVAL;
674db80f0e1SBeniamino Galvani 		}
675db80f0e1SBeniamino Galvani 		gpio_np = np;
6766ac73095SBeniamino Galvani 	}
6776ac73095SBeniamino Galvani 
678db80f0e1SBeniamino Galvani 	if (!gpio_np) {
679db80f0e1SBeniamino Galvani 		dev_err(pc->dev, "no gpio node found\n");
6806ac73095SBeniamino Galvani 		return -EINVAL;
6816ac73095SBeniamino Galvani 	}
6826ac73095SBeniamino Galvani 
683db80f0e1SBeniamino Galvani 	pc->of_node = gpio_np;
6846ac73095SBeniamino Galvani 
685db80f0e1SBeniamino Galvani 	pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
686fd422964SQianggui Song 	if (IS_ERR_OR_NULL(pc->reg_mux)) {
6876ac73095SBeniamino Galvani 		dev_err(pc->dev, "mux registers not found\n");
688fd422964SQianggui Song 		return pc->reg_mux ? PTR_ERR(pc->reg_mux) : -ENOENT;
6896ac73095SBeniamino Galvani 	}
6906ac73095SBeniamino Galvani 
691db80f0e1SBeniamino Galvani 	pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
692fd422964SQianggui Song 	if (IS_ERR_OR_NULL(pc->reg_gpio)) {
6936ac73095SBeniamino Galvani 		dev_err(pc->dev, "gpio registers not found\n");
694fd422964SQianggui Song 		return pc->reg_gpio ? PTR_ERR(pc->reg_gpio) : -ENOENT;
6956ac73095SBeniamino Galvani 	}
6966ac73095SBeniamino Galvani 
697e66dd48eSXingyu Chen 	pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
698e66dd48eSXingyu Chen 	if (IS_ERR(pc->reg_pull))
699fd422964SQianggui Song 		pc->reg_pull = NULL;
700e66dd48eSXingyu Chen 
701e66dd48eSXingyu Chen 	pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
702e66dd48eSXingyu Chen 	if (IS_ERR(pc->reg_pullen))
703fd422964SQianggui Song 		pc->reg_pullen = NULL;
704e66dd48eSXingyu Chen 
70564856974SJerome Brunet 	pc->reg_ds = meson_map_resource(pc, gpio_np, "ds");
70664856974SJerome Brunet 	if (IS_ERR(pc->reg_ds)) {
70764856974SJerome Brunet 		dev_dbg(pc->dev, "ds registers not found - skipping\n");
70864856974SJerome Brunet 		pc->reg_ds = NULL;
70964856974SJerome Brunet 	}
71064856974SJerome Brunet 
711fd422964SQianggui Song 	if (pc->data->parse_dt)
712fd422964SQianggui Song 		return pc->data->parse_dt(pc);
713fd422964SQianggui Song 
714fd422964SQianggui Song 	return 0;
715fd422964SQianggui Song }
716fd422964SQianggui Song 
717fd422964SQianggui Song int meson8_aobus_parse_dt_extra(struct meson_pinctrl *pc)
718fd422964SQianggui Song {
719fd422964SQianggui Song 	if (!pc->reg_pull)
720fd422964SQianggui Song 		return -EINVAL;
721fd422964SQianggui Song 
722fd422964SQianggui Song 	pc->reg_pullen = pc->reg_pull;
723fd422964SQianggui Song 
7246ac73095SBeniamino Galvani 	return 0;
7256ac73095SBeniamino Galvani }
7266ac73095SBeniamino Galvani 
727dabad1ffSQianggui Song int meson_a1_parse_dt_extra(struct meson_pinctrl *pc)
728dabad1ffSQianggui Song {
729dabad1ffSQianggui Song 	pc->reg_pull = pc->reg_gpio;
730dabad1ffSQianggui Song 	pc->reg_pullen = pc->reg_gpio;
731dabad1ffSQianggui Song 	pc->reg_ds = pc->reg_gpio;
732dabad1ffSQianggui Song 
733dabad1ffSQianggui Song 	return 0;
734dabad1ffSQianggui Song }
735dabad1ffSQianggui Song 
736277d14ebSJerome Brunet int meson_pinctrl_probe(struct platform_device *pdev)
7376ac73095SBeniamino Galvani {
7386ac73095SBeniamino Galvani 	struct device *dev = &pdev->dev;
7396ac73095SBeniamino Galvani 	struct meson_pinctrl *pc;
7406ac73095SBeniamino Galvani 	int ret;
7416ac73095SBeniamino Galvani 
7426ac73095SBeniamino Galvani 	pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
7436ac73095SBeniamino Galvani 	if (!pc)
7446ac73095SBeniamino Galvani 		return -ENOMEM;
7456ac73095SBeniamino Galvani 
7466ac73095SBeniamino Galvani 	pc->dev = dev;
747277d14ebSJerome Brunet 	pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev);
7486ac73095SBeniamino Galvani 
749277d14ebSJerome Brunet 	ret = meson_pinctrl_parse_dt(pc, dev->of_node);
7506ac73095SBeniamino Galvani 	if (ret)
7516ac73095SBeniamino Galvani 		return ret;
7526ac73095SBeniamino Galvani 
7536ac73095SBeniamino Galvani 	pc->desc.name		= "pinctrl-meson";
7546ac73095SBeniamino Galvani 	pc->desc.owner		= THIS_MODULE;
7556ac73095SBeniamino Galvani 	pc->desc.pctlops	= &meson_pctrl_ops;
756ce385aa2SJerome Brunet 	pc->desc.pmxops		= pc->data->pmx_ops;
7576ac73095SBeniamino Galvani 	pc->desc.confops	= &meson_pinconf_ops;
7586ac73095SBeniamino Galvani 	pc->desc.pins		= pc->data->pins;
7596ac73095SBeniamino Galvani 	pc->desc.npins		= pc->data->num_pins;
7606ac73095SBeniamino Galvani 
761e649f7ecSLaxman Dewangan 	pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
762323de9efSMasahiro Yamada 	if (IS_ERR(pc->pcdev)) {
7636ac73095SBeniamino Galvani 		dev_err(pc->dev, "can't register pinctrl device");
764323de9efSMasahiro Yamada 		return PTR_ERR(pc->pcdev);
7656ac73095SBeniamino Galvani 	}
7666ac73095SBeniamino Galvani 
7675b236d0fSWei Yongjun 	return meson_gpiolib_register(pc);
7686ac73095SBeniamino Galvani }
769