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>
466ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf-generic.h>
476ac73095SBeniamino Galvani #include <linux/pinctrl/pinconf.h>
486ac73095SBeniamino Galvani #include <linux/pinctrl/pinctrl.h>
496ac73095SBeniamino Galvani #include <linux/pinctrl/pinmux.h>
506ac73095SBeniamino Galvani #include <linux/platform_device.h>
51edc5601dSAndy Shevchenko #include <linux/property.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
59f088ab6dSHyeonki Hong static const unsigned int meson_bit_strides[] = {
60f088ab6dSHyeonki Hong 1, 1, 1, 1, 1, 2, 1
61f088ab6dSHyeonki Hong };
62f088ab6dSHyeonki 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 */
meson_get_bank(struct meson_pinctrl * pc,unsigned int pin,const struct meson_bank ** bank)72db80f0e1SBeniamino Galvani static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
73*8a74a53eSChristophe JAILLET const 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 */
meson_calc_reg_and_bit(const struct meson_bank * bank,unsigned int pin,enum meson_reg_type reg_type,unsigned int * reg,unsigned int * bit)97*8a74a53eSChristophe JAILLET static void meson_calc_reg_and_bit(const struct meson_bank *bank,
98*8a74a53eSChristophe JAILLET unsigned int pin,
996ac73095SBeniamino Galvani enum meson_reg_type reg_type,
1006ac73095SBeniamino Galvani unsigned int *reg, unsigned int *bit)
1016ac73095SBeniamino Galvani {
102*8a74a53eSChristophe JAILLET const struct meson_reg_desc *desc = &bank->regs[reg_type];
1036ac73095SBeniamino Galvani
104f088ab6dSHyeonki Hong *bit = (desc->bit + pin - bank->first) * meson_bit_strides[reg_type];
105f088ab6dSHyeonki Hong *reg = (desc->reg + (*bit / 32)) * 4;
106f088ab6dSHyeonki Hong *bit &= 0x1f;
1076ac73095SBeniamino Galvani }
1086ac73095SBeniamino Galvani
meson_get_groups_count(struct pinctrl_dev * pcdev)1096ac73095SBeniamino Galvani static int meson_get_groups_count(struct pinctrl_dev *pcdev)
1106ac73095SBeniamino Galvani {
1116ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1126ac73095SBeniamino Galvani
1136ac73095SBeniamino Galvani return pc->data->num_groups;
1146ac73095SBeniamino Galvani }
1156ac73095SBeniamino Galvani
meson_get_group_name(struct pinctrl_dev * pcdev,unsigned selector)1166ac73095SBeniamino Galvani static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
1176ac73095SBeniamino Galvani unsigned selector)
1186ac73095SBeniamino Galvani {
1196ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1206ac73095SBeniamino Galvani
1216ac73095SBeniamino Galvani return pc->data->groups[selector].name;
1226ac73095SBeniamino Galvani }
1236ac73095SBeniamino Galvani
meson_get_group_pins(struct pinctrl_dev * pcdev,unsigned selector,const unsigned ** pins,unsigned * num_pins)1246ac73095SBeniamino Galvani static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
1256ac73095SBeniamino Galvani const unsigned **pins, unsigned *num_pins)
1266ac73095SBeniamino Galvani {
1276ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1286ac73095SBeniamino Galvani
1296ac73095SBeniamino Galvani *pins = pc->data->groups[selector].pins;
1306ac73095SBeniamino Galvani *num_pins = pc->data->groups[selector].num_pins;
1316ac73095SBeniamino Galvani
1326ac73095SBeniamino Galvani return 0;
1336ac73095SBeniamino Galvani }
1346ac73095SBeniamino Galvani
meson_pin_dbg_show(struct pinctrl_dev * pcdev,struct seq_file * s,unsigned offset)1356ac73095SBeniamino Galvani static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
1366ac73095SBeniamino Galvani unsigned offset)
1376ac73095SBeniamino Galvani {
1386ac73095SBeniamino Galvani seq_printf(s, " %s", dev_name(pcdev->dev));
1396ac73095SBeniamino Galvani }
1406ac73095SBeniamino Galvani
1416ac73095SBeniamino Galvani static const struct pinctrl_ops meson_pctrl_ops = {
1426ac73095SBeniamino Galvani .get_groups_count = meson_get_groups_count,
1436ac73095SBeniamino Galvani .get_group_name = meson_get_group_name,
1446ac73095SBeniamino Galvani .get_group_pins = meson_get_group_pins,
1456ac73095SBeniamino Galvani .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
146d32f7fd3SIrina Tirdea .dt_free_map = pinctrl_utils_free_map,
1476ac73095SBeniamino Galvani .pin_dbg_show = meson_pin_dbg_show,
1486ac73095SBeniamino Galvani };
1496ac73095SBeniamino Galvani
meson_pmx_get_funcs_count(struct pinctrl_dev * pcdev)150ce385aa2SJerome Brunet int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
1516ac73095SBeniamino Galvani {
1526ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1536ac73095SBeniamino Galvani
1546ac73095SBeniamino Galvani return pc->data->num_funcs;
1556ac73095SBeniamino Galvani }
1569c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson_pmx_get_funcs_count);
1576ac73095SBeniamino Galvani
meson_pmx_get_func_name(struct pinctrl_dev * pcdev,unsigned selector)158ce385aa2SJerome Brunet const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
1596ac73095SBeniamino Galvani unsigned selector)
1606ac73095SBeniamino Galvani {
1616ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1626ac73095SBeniamino Galvani
1636ac73095SBeniamino Galvani return pc->data->funcs[selector].name;
1646ac73095SBeniamino Galvani }
1659c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson_pmx_get_func_name);
1666ac73095SBeniamino Galvani
meson_pmx_get_groups(struct pinctrl_dev * pcdev,unsigned selector,const char * const ** groups,unsigned * const num_groups)167ce385aa2SJerome Brunet int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
1686ac73095SBeniamino Galvani const char * const **groups,
1696ac73095SBeniamino Galvani unsigned * const num_groups)
1706ac73095SBeniamino Galvani {
1716ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
1726ac73095SBeniamino Galvani
1736ac73095SBeniamino Galvani *groups = pc->data->funcs[selector].groups;
1746ac73095SBeniamino Galvani *num_groups = pc->data->funcs[selector].num_groups;
1756ac73095SBeniamino Galvani
1766ac73095SBeniamino Galvani return 0;
1776ac73095SBeniamino Galvani }
1789c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson_pmx_get_groups);
1796ac73095SBeniamino Galvani
meson_pinconf_set_gpio_bit(struct meson_pinctrl * pc,unsigned int pin,unsigned int reg_type,bool arg)180b22a7f85SJerome Brunet static int meson_pinconf_set_gpio_bit(struct meson_pinctrl *pc,
181b22a7f85SJerome Brunet unsigned int pin,
182b22a7f85SJerome Brunet unsigned int reg_type,
183b22a7f85SJerome Brunet bool arg)
1846ac73095SBeniamino Galvani {
185*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
1866ac73095SBeniamino Galvani unsigned int reg, bit;
187b22a7f85SJerome Brunet int ret;
1886ac73095SBeniamino Galvani
189db80f0e1SBeniamino Galvani ret = meson_get_bank(pc, pin, &bank);
1906ac73095SBeniamino Galvani if (ret)
1916ac73095SBeniamino Galvani return ret;
1926ac73095SBeniamino Galvani
193b22a7f85SJerome Brunet meson_calc_reg_and_bit(bank, pin, reg_type, ®, &bit);
194b22a7f85SJerome Brunet return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
195b22a7f85SJerome Brunet arg ? BIT(bit) : 0);
196b22a7f85SJerome Brunet }
197b22a7f85SJerome Brunet
meson_pinconf_get_gpio_bit(struct meson_pinctrl * pc,unsigned int pin,unsigned int reg_type)198b22a7f85SJerome Brunet static int meson_pinconf_get_gpio_bit(struct meson_pinctrl *pc,
199b22a7f85SJerome Brunet unsigned int pin,
200b22a7f85SJerome Brunet unsigned int reg_type)
201b22a7f85SJerome Brunet {
202*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
203b22a7f85SJerome Brunet unsigned int reg, bit, val;
204b22a7f85SJerome Brunet int ret;
205b22a7f85SJerome Brunet
206b22a7f85SJerome Brunet ret = meson_get_bank(pc, pin, &bank);
207b22a7f85SJerome Brunet if (ret)
208b22a7f85SJerome Brunet return ret;
209b22a7f85SJerome Brunet
210b22a7f85SJerome Brunet meson_calc_reg_and_bit(bank, pin, reg_type, ®, &bit);
211b22a7f85SJerome Brunet ret = regmap_read(pc->reg_gpio, reg, &val);
212b22a7f85SJerome Brunet if (ret)
213b22a7f85SJerome Brunet return ret;
214b22a7f85SJerome Brunet
215b22a7f85SJerome Brunet return BIT(bit) & val ? 1 : 0;
216b22a7f85SJerome Brunet }
217b22a7f85SJerome Brunet
meson_pinconf_set_output(struct meson_pinctrl * pc,unsigned int pin,bool out)218b22a7f85SJerome Brunet static int meson_pinconf_set_output(struct meson_pinctrl *pc,
219b22a7f85SJerome Brunet unsigned int pin,
220b22a7f85SJerome Brunet bool out)
221b22a7f85SJerome Brunet {
2222b2dce80SAndy Shevchenko return meson_pinconf_set_gpio_bit(pc, pin, MESON_REG_DIR, !out);
223b22a7f85SJerome Brunet }
224b22a7f85SJerome Brunet
meson_pinconf_get_output(struct meson_pinctrl * pc,unsigned int pin)225b22a7f85SJerome Brunet static int meson_pinconf_get_output(struct meson_pinctrl *pc,
226b22a7f85SJerome Brunet unsigned int pin)
227b22a7f85SJerome Brunet {
2282b2dce80SAndy Shevchenko int ret = meson_pinconf_get_gpio_bit(pc, pin, MESON_REG_DIR);
229b22a7f85SJerome Brunet
230b22a7f85SJerome Brunet if (ret < 0)
231b22a7f85SJerome Brunet return ret;
232b22a7f85SJerome Brunet
233b22a7f85SJerome Brunet return !ret;
234b22a7f85SJerome Brunet }
235b22a7f85SJerome Brunet
meson_pinconf_set_drive(struct meson_pinctrl * pc,unsigned int pin,bool high)236b22a7f85SJerome Brunet static int meson_pinconf_set_drive(struct meson_pinctrl *pc,
237b22a7f85SJerome Brunet unsigned int pin,
238b22a7f85SJerome Brunet bool high)
239b22a7f85SJerome Brunet {
2402b2dce80SAndy Shevchenko return meson_pinconf_set_gpio_bit(pc, pin, MESON_REG_OUT, high);
241b22a7f85SJerome Brunet }
242b22a7f85SJerome Brunet
meson_pinconf_get_drive(struct meson_pinctrl * pc,unsigned int pin)243b22a7f85SJerome Brunet static int meson_pinconf_get_drive(struct meson_pinctrl *pc,
244b22a7f85SJerome Brunet unsigned int pin)
245b22a7f85SJerome Brunet {
2462b2dce80SAndy Shevchenko return meson_pinconf_get_gpio_bit(pc, pin, MESON_REG_OUT);
247b22a7f85SJerome Brunet }
248b22a7f85SJerome Brunet
meson_pinconf_set_output_drive(struct meson_pinctrl * pc,unsigned int pin,bool high)249b22a7f85SJerome Brunet static int meson_pinconf_set_output_drive(struct meson_pinctrl *pc,
250b22a7f85SJerome Brunet unsigned int pin,
251b22a7f85SJerome Brunet bool high)
252b22a7f85SJerome Brunet {
253b22a7f85SJerome Brunet int ret;
254b22a7f85SJerome Brunet
255b22a7f85SJerome Brunet ret = meson_pinconf_set_output(pc, pin, true);
256b22a7f85SJerome Brunet if (ret)
257b22a7f85SJerome Brunet return ret;
258b22a7f85SJerome Brunet
259b22a7f85SJerome Brunet return meson_pinconf_set_drive(pc, pin, high);
260b22a7f85SJerome Brunet }
261b22a7f85SJerome Brunet
meson_pinconf_disable_bias(struct meson_pinctrl * pc,unsigned int pin)2629959d9a7SGuillaume La Roque static int meson_pinconf_disable_bias(struct meson_pinctrl *pc,
2639959d9a7SGuillaume La Roque unsigned int pin)
2646ac73095SBeniamino Galvani {
265*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
2669959d9a7SGuillaume La Roque unsigned int reg, bit = 0;
2679959d9a7SGuillaume La Roque int ret;
2686ac73095SBeniamino Galvani
2696ac73095SBeniamino Galvani ret = meson_get_bank(pc, pin, &bank);
2706ac73095SBeniamino Galvani if (ret)
2716ac73095SBeniamino Galvani return ret;
2726ac73095SBeniamino Galvani
2732b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, ®, &bit);
2749959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), 0);
2759959d9a7SGuillaume La Roque if (ret)
2769959d9a7SGuillaume La Roque return ret;
2779959d9a7SGuillaume La Roque
2789959d9a7SGuillaume La Roque return 0;
2799959d9a7SGuillaume La Roque }
2809959d9a7SGuillaume La Roque
meson_pinconf_enable_bias(struct meson_pinctrl * pc,unsigned int pin,bool pull_up)2819959d9a7SGuillaume La Roque static int meson_pinconf_enable_bias(struct meson_pinctrl *pc, unsigned int pin,
2829959d9a7SGuillaume La Roque bool pull_up)
2839959d9a7SGuillaume La Roque {
284*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
2859959d9a7SGuillaume La Roque unsigned int reg, bit, val = 0;
2869959d9a7SGuillaume La Roque int ret;
2879959d9a7SGuillaume La Roque
2889959d9a7SGuillaume La Roque ret = meson_get_bank(pc, pin, &bank);
2899959d9a7SGuillaume La Roque if (ret)
2909959d9a7SGuillaume La Roque return ret;
2919959d9a7SGuillaume La Roque
2922b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_PULL, ®, &bit);
2939959d9a7SGuillaume La Roque if (pull_up)
2949959d9a7SGuillaume La Roque val = BIT(bit);
2959959d9a7SGuillaume La Roque
2969959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pull, reg, BIT(bit), val);
2979959d9a7SGuillaume La Roque if (ret)
2989959d9a7SGuillaume La Roque return ret;
2999959d9a7SGuillaume La Roque
3002b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, ®, &bit);
3019959d9a7SGuillaume La Roque ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), BIT(bit));
3029959d9a7SGuillaume La Roque if (ret)
3039959d9a7SGuillaume La Roque return ret;
3049959d9a7SGuillaume La Roque
3059959d9a7SGuillaume La Roque return 0;
3069959d9a7SGuillaume La Roque }
3079959d9a7SGuillaume La Roque
meson_pinconf_set_drive_strength(struct meson_pinctrl * pc,unsigned int pin,u16 drive_strength_ua)3086ea3e3bbSGuillaume La Roque static int meson_pinconf_set_drive_strength(struct meson_pinctrl *pc,
3096ea3e3bbSGuillaume La Roque unsigned int pin,
3106ea3e3bbSGuillaume La Roque u16 drive_strength_ua)
3116ea3e3bbSGuillaume La Roque {
312*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
3136ea3e3bbSGuillaume La Roque unsigned int reg, bit, ds_val;
3146ea3e3bbSGuillaume La Roque int ret;
3156ea3e3bbSGuillaume La Roque
3166ea3e3bbSGuillaume La Roque if (!pc->reg_ds) {
3176ea3e3bbSGuillaume La Roque dev_err(pc->dev, "drive-strength not supported\n");
3186ea3e3bbSGuillaume La Roque return -ENOTSUPP;
3196ea3e3bbSGuillaume La Roque }
3206ea3e3bbSGuillaume La Roque
3216ea3e3bbSGuillaume La Roque ret = meson_get_bank(pc, pin, &bank);
3226ea3e3bbSGuillaume La Roque if (ret)
3236ea3e3bbSGuillaume La Roque return ret;
3246ea3e3bbSGuillaume La Roque
3252b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_DS, ®, &bit);
3266ea3e3bbSGuillaume La Roque
3276ea3e3bbSGuillaume La Roque if (drive_strength_ua <= 500) {
3286ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_500UA;
3296ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 2500) {
3306ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_2500UA;
3316ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 3000) {
3326ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_3000UA;
3336ea3e3bbSGuillaume La Roque } else if (drive_strength_ua <= 4000) {
3346ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_4000UA;
3356ea3e3bbSGuillaume La Roque } else {
3366ea3e3bbSGuillaume La Roque dev_warn_once(pc->dev,
3376ea3e3bbSGuillaume La Roque "pin %u: invalid drive-strength : %d , default to 4mA\n",
3386ea3e3bbSGuillaume La Roque pin, drive_strength_ua);
3396ea3e3bbSGuillaume La Roque ds_val = MESON_PINCONF_DRV_4000UA;
3406ea3e3bbSGuillaume La Roque }
3416ea3e3bbSGuillaume La Roque
3426ea3e3bbSGuillaume La Roque ret = regmap_update_bits(pc->reg_ds, reg, 0x3 << bit, ds_val << bit);
3436ea3e3bbSGuillaume La Roque if (ret)
3446ea3e3bbSGuillaume La Roque return ret;
3456ea3e3bbSGuillaume La Roque
3466ea3e3bbSGuillaume La Roque return 0;
3476ea3e3bbSGuillaume La Roque }
3486ea3e3bbSGuillaume La Roque
meson_pinconf_set(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * configs,unsigned num_configs)3499959d9a7SGuillaume La Roque static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
3509959d9a7SGuillaume La Roque unsigned long *configs, unsigned num_configs)
3519959d9a7SGuillaume La Roque {
3529959d9a7SGuillaume La Roque struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
3539959d9a7SGuillaume La Roque enum pin_config_param param;
354b22a7f85SJerome Brunet unsigned int arg = 0;
3559959d9a7SGuillaume La Roque int i, ret;
3569959d9a7SGuillaume La Roque
3576ac73095SBeniamino Galvani for (i = 0; i < num_configs; i++) {
3586ac73095SBeniamino Galvani param = pinconf_to_config_param(configs[i]);
3596ac73095SBeniamino Galvani
3606ac73095SBeniamino Galvani switch (param) {
361b22a7f85SJerome Brunet case PIN_CONFIG_DRIVE_STRENGTH_UA:
362b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT_ENABLE:
363b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT:
364b22a7f85SJerome Brunet arg = pinconf_to_config_argument(configs[i]);
365b22a7f85SJerome Brunet break;
3666ac73095SBeniamino Galvani
367b22a7f85SJerome Brunet default:
368b22a7f85SJerome Brunet break;
369b22a7f85SJerome Brunet }
370b22a7f85SJerome Brunet
371b22a7f85SJerome Brunet switch (param) {
3726ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_DISABLE:
3739959d9a7SGuillaume La Roque ret = meson_pinconf_disable_bias(pc, pin);
3746ac73095SBeniamino Galvani break;
3756ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_UP:
3769959d9a7SGuillaume La Roque ret = meson_pinconf_enable_bias(pc, pin, true);
3776ac73095SBeniamino Galvani break;
3786ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_DOWN:
3799959d9a7SGuillaume La Roque ret = meson_pinconf_enable_bias(pc, pin, false);
3806ac73095SBeniamino Galvani break;
3816ea3e3bbSGuillaume La Roque case PIN_CONFIG_DRIVE_STRENGTH_UA:
382b22a7f85SJerome Brunet ret = meson_pinconf_set_drive_strength(pc, pin, arg);
383b22a7f85SJerome Brunet break;
384b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT_ENABLE:
385b22a7f85SJerome Brunet ret = meson_pinconf_set_output(pc, pin, arg);
386b22a7f85SJerome Brunet break;
387b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT:
388b22a7f85SJerome Brunet ret = meson_pinconf_set_output_drive(pc, pin, arg);
3896ac73095SBeniamino Galvani break;
3906ac73095SBeniamino Galvani default:
391b22a7f85SJerome Brunet ret = -ENOTSUPP;
3926ac73095SBeniamino Galvani }
393b22a7f85SJerome Brunet
394b22a7f85SJerome Brunet if (ret)
395b22a7f85SJerome Brunet return ret;
3966ac73095SBeniamino Galvani }
3976ac73095SBeniamino Galvani
3986ac73095SBeniamino Galvani return 0;
3996ac73095SBeniamino Galvani }
4006ac73095SBeniamino Galvani
meson_pinconf_get_pull(struct meson_pinctrl * pc,unsigned int pin)4016ac73095SBeniamino Galvani static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
4026ac73095SBeniamino Galvani {
403*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
4046ac73095SBeniamino Galvani unsigned int reg, bit, val;
4056ac73095SBeniamino Galvani int ret, conf;
4066ac73095SBeniamino Galvani
407db80f0e1SBeniamino Galvani ret = meson_get_bank(pc, pin, &bank);
4086ac73095SBeniamino Galvani if (ret)
4096ac73095SBeniamino Galvani return ret;
4106ac73095SBeniamino Galvani
4112b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, ®, &bit);
4126ac73095SBeniamino Galvani
413db80f0e1SBeniamino Galvani ret = regmap_read(pc->reg_pullen, reg, &val);
4146ac73095SBeniamino Galvani if (ret)
4156ac73095SBeniamino Galvani return ret;
4166ac73095SBeniamino Galvani
4176ac73095SBeniamino Galvani if (!(val & BIT(bit))) {
4186ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_DISABLE;
4196ac73095SBeniamino Galvani } else {
4202b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_PULL, ®, &bit);
4216ac73095SBeniamino Galvani
422db80f0e1SBeniamino Galvani ret = regmap_read(pc->reg_pull, reg, &val);
4236ac73095SBeniamino Galvani if (ret)
4246ac73095SBeniamino Galvani return ret;
4256ac73095SBeniamino Galvani
4266ac73095SBeniamino Galvani if (val & BIT(bit))
4276ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_PULL_UP;
4286ac73095SBeniamino Galvani else
4296ac73095SBeniamino Galvani conf = PIN_CONFIG_BIAS_PULL_DOWN;
4306ac73095SBeniamino Galvani }
4316ac73095SBeniamino Galvani
4326ac73095SBeniamino Galvani return conf;
4336ac73095SBeniamino Galvani }
4346ac73095SBeniamino Galvani
meson_pinconf_get_drive_strength(struct meson_pinctrl * pc,unsigned int pin,u16 * drive_strength_ua)4356ea3e3bbSGuillaume La Roque static int meson_pinconf_get_drive_strength(struct meson_pinctrl *pc,
4366ea3e3bbSGuillaume La Roque unsigned int pin,
4376ea3e3bbSGuillaume La Roque u16 *drive_strength_ua)
4386ea3e3bbSGuillaume La Roque {
439*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
4406ea3e3bbSGuillaume La Roque unsigned int reg, bit;
4416ea3e3bbSGuillaume La Roque unsigned int val;
4426ea3e3bbSGuillaume La Roque int ret;
4436ea3e3bbSGuillaume La Roque
4446ea3e3bbSGuillaume La Roque if (!pc->reg_ds)
4456ea3e3bbSGuillaume La Roque return -ENOTSUPP;
4466ea3e3bbSGuillaume La Roque
4476ea3e3bbSGuillaume La Roque ret = meson_get_bank(pc, pin, &bank);
4486ea3e3bbSGuillaume La Roque if (ret)
4496ea3e3bbSGuillaume La Roque return ret;
4506ea3e3bbSGuillaume La Roque
4512b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, pin, MESON_REG_DS, ®, &bit);
4526ea3e3bbSGuillaume La Roque
4536ea3e3bbSGuillaume La Roque ret = regmap_read(pc->reg_ds, reg, &val);
4546ea3e3bbSGuillaume La Roque if (ret)
4556ea3e3bbSGuillaume La Roque return ret;
4566ea3e3bbSGuillaume La Roque
4576ea3e3bbSGuillaume La Roque switch ((val >> bit) & 0x3) {
4586ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_500UA:
4596ea3e3bbSGuillaume La Roque *drive_strength_ua = 500;
4606ea3e3bbSGuillaume La Roque break;
4616ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_2500UA:
4626ea3e3bbSGuillaume La Roque *drive_strength_ua = 2500;
4636ea3e3bbSGuillaume La Roque break;
4646ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_3000UA:
4656ea3e3bbSGuillaume La Roque *drive_strength_ua = 3000;
4666ea3e3bbSGuillaume La Roque break;
4676ea3e3bbSGuillaume La Roque case MESON_PINCONF_DRV_4000UA:
4686ea3e3bbSGuillaume La Roque *drive_strength_ua = 4000;
4696ea3e3bbSGuillaume La Roque break;
4706ea3e3bbSGuillaume La Roque default:
4716ea3e3bbSGuillaume La Roque return -EINVAL;
4726ea3e3bbSGuillaume La Roque }
4736ea3e3bbSGuillaume La Roque
4746ea3e3bbSGuillaume La Roque return 0;
4756ea3e3bbSGuillaume La Roque }
4766ea3e3bbSGuillaume La Roque
meson_pinconf_get(struct pinctrl_dev * pcdev,unsigned int pin,unsigned long * config)4776ac73095SBeniamino Galvani static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
4786ac73095SBeniamino Galvani unsigned long *config)
4796ac73095SBeniamino Galvani {
4806ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
4816ac73095SBeniamino Galvani enum pin_config_param param = pinconf_to_config_param(*config);
4826ac73095SBeniamino Galvani u16 arg;
4836ea3e3bbSGuillaume La Roque int ret;
4846ac73095SBeniamino Galvani
4856ac73095SBeniamino Galvani switch (param) {
4866ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_DISABLE:
4876ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_DOWN:
4886ac73095SBeniamino Galvani case PIN_CONFIG_BIAS_PULL_UP:
4896ac73095SBeniamino Galvani if (meson_pinconf_get_pull(pc, pin) == param)
4906ac73095SBeniamino Galvani arg = 1;
4916ac73095SBeniamino Galvani else
4926ac73095SBeniamino Galvani return -EINVAL;
4936ac73095SBeniamino Galvani break;
4946ea3e3bbSGuillaume La Roque case PIN_CONFIG_DRIVE_STRENGTH_UA:
4956ea3e3bbSGuillaume La Roque ret = meson_pinconf_get_drive_strength(pc, pin, &arg);
4966ea3e3bbSGuillaume La Roque if (ret)
4976ea3e3bbSGuillaume La Roque return ret;
4986ea3e3bbSGuillaume La Roque break;
499b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT_ENABLE:
500b22a7f85SJerome Brunet ret = meson_pinconf_get_output(pc, pin);
501b22a7f85SJerome Brunet if (ret <= 0)
502b22a7f85SJerome Brunet return -EINVAL;
503b22a7f85SJerome Brunet arg = 1;
504b22a7f85SJerome Brunet break;
505b22a7f85SJerome Brunet case PIN_CONFIG_OUTPUT:
506b22a7f85SJerome Brunet ret = meson_pinconf_get_output(pc, pin);
507b22a7f85SJerome Brunet if (ret <= 0)
508b22a7f85SJerome Brunet return -EINVAL;
509b22a7f85SJerome Brunet
510b22a7f85SJerome Brunet ret = meson_pinconf_get_drive(pc, pin);
511b22a7f85SJerome Brunet if (ret < 0)
512b22a7f85SJerome Brunet return -EINVAL;
513b22a7f85SJerome Brunet
514b22a7f85SJerome Brunet arg = ret;
515b22a7f85SJerome Brunet break;
516b22a7f85SJerome Brunet
5176ac73095SBeniamino Galvani default:
5186ac73095SBeniamino Galvani return -ENOTSUPP;
5196ac73095SBeniamino Galvani }
5206ac73095SBeniamino Galvani
5216ac73095SBeniamino Galvani *config = pinconf_to_config_packed(param, arg);
5226ac73095SBeniamino Galvani dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
5236ac73095SBeniamino Galvani
5246ac73095SBeniamino Galvani return 0;
5256ac73095SBeniamino Galvani }
5266ac73095SBeniamino Galvani
meson_pinconf_group_set(struct pinctrl_dev * pcdev,unsigned int num_group,unsigned long * configs,unsigned num_configs)5276ac73095SBeniamino Galvani static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
5286ac73095SBeniamino Galvani unsigned int num_group,
5296ac73095SBeniamino Galvani unsigned long *configs, unsigned num_configs)
5306ac73095SBeniamino Galvani {
5316ac73095SBeniamino Galvani struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
532*8a74a53eSChristophe JAILLET const struct meson_pmx_group *group = &pc->data->groups[num_group];
5336ac73095SBeniamino Galvani int i;
5346ac73095SBeniamino Galvani
5356ac73095SBeniamino Galvani dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
5366ac73095SBeniamino Galvani
5376ac73095SBeniamino Galvani for (i = 0; i < group->num_pins; i++) {
5386ac73095SBeniamino Galvani meson_pinconf_set(pcdev, group->pins[i], configs,
5396ac73095SBeniamino Galvani num_configs);
5406ac73095SBeniamino Galvani }
5416ac73095SBeniamino Galvani
5426ac73095SBeniamino Galvani return 0;
5436ac73095SBeniamino Galvani }
5446ac73095SBeniamino Galvani
meson_pinconf_group_get(struct pinctrl_dev * pcdev,unsigned int group,unsigned long * config)5456ac73095SBeniamino Galvani static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
5466ac73095SBeniamino Galvani unsigned int group, unsigned long *config)
5476ac73095SBeniamino Galvani {
5481ffbf50bSJerome Brunet return -ENOTSUPP;
5496ac73095SBeniamino Galvani }
5506ac73095SBeniamino Galvani
5516ac73095SBeniamino Galvani static const struct pinconf_ops meson_pinconf_ops = {
5526ac73095SBeniamino Galvani .pin_config_get = meson_pinconf_get,
5536ac73095SBeniamino Galvani .pin_config_set = meson_pinconf_set,
5546ac73095SBeniamino Galvani .pin_config_group_get = meson_pinconf_group_get,
5556ac73095SBeniamino Galvani .pin_config_group_set = meson_pinconf_group_set,
5566ac73095SBeniamino Galvani .is_generic = true,
5576ac73095SBeniamino Galvani };
5586ac73095SBeniamino Galvani
meson_gpio_get_direction(struct gpio_chip * chip,unsigned gpio)559ef1d0bceSMartin Blumenstingl static int meson_gpio_get_direction(struct gpio_chip *chip, unsigned gpio)
560ef1d0bceSMartin Blumenstingl {
561ef1d0bceSMartin Blumenstingl struct meson_pinctrl *pc = gpiochip_get_data(chip);
562ef1d0bceSMartin Blumenstingl int ret;
563ef1d0bceSMartin Blumenstingl
564ef1d0bceSMartin Blumenstingl ret = meson_pinconf_get_output(pc, gpio);
565ef1d0bceSMartin Blumenstingl if (ret < 0)
566ef1d0bceSMartin Blumenstingl return ret;
567ef1d0bceSMartin Blumenstingl
568ef1d0bceSMartin Blumenstingl return ret ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
569ef1d0bceSMartin Blumenstingl }
570ef1d0bceSMartin Blumenstingl
meson_gpio_direction_input(struct gpio_chip * chip,unsigned gpio)5716ac73095SBeniamino Galvani static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
5726ac73095SBeniamino Galvani {
573b22a7f85SJerome Brunet return meson_pinconf_set_output(gpiochip_get_data(chip), gpio, false);
5746ac73095SBeniamino Galvani }
5756ac73095SBeniamino Galvani
meson_gpio_direction_output(struct gpio_chip * chip,unsigned gpio,int value)5766ac73095SBeniamino Galvani static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
5776ac73095SBeniamino Galvani int value)
5786ac73095SBeniamino Galvani {
579b22a7f85SJerome Brunet return meson_pinconf_set_output_drive(gpiochip_get_data(chip),
580b22a7f85SJerome Brunet gpio, value);
5816ac73095SBeniamino Galvani }
5826ac73095SBeniamino Galvani
meson_gpio_set(struct gpio_chip * chip,unsigned gpio,int value)5836ac73095SBeniamino Galvani static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
5846ac73095SBeniamino Galvani {
585b22a7f85SJerome Brunet meson_pinconf_set_drive(gpiochip_get_data(chip), gpio, value);
5866ac73095SBeniamino Galvani }
5876ac73095SBeniamino Galvani
meson_gpio_get(struct gpio_chip * chip,unsigned gpio)5886ac73095SBeniamino Galvani static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
5896ac73095SBeniamino Galvani {
590db80f0e1SBeniamino Galvani struct meson_pinctrl *pc = gpiochip_get_data(chip);
591*8a74a53eSChristophe JAILLET const struct meson_bank *bank;
59270e5ecb1SJerome Brunet unsigned int reg, bit, val;
5936ac73095SBeniamino Galvani int ret;
5946ac73095SBeniamino Galvani
59570e5ecb1SJerome Brunet ret = meson_get_bank(pc, gpio, &bank);
5966ac73095SBeniamino Galvani if (ret)
5976ac73095SBeniamino Galvani return ret;
5986ac73095SBeniamino Galvani
5992b2dce80SAndy Shevchenko meson_calc_reg_and_bit(bank, gpio, MESON_REG_IN, ®, &bit);
600db80f0e1SBeniamino Galvani regmap_read(pc->reg_gpio, reg, &val);
6016ac73095SBeniamino Galvani
6026ac73095SBeniamino Galvani return !!(val & BIT(bit));
6036ac73095SBeniamino Galvani }
6046ac73095SBeniamino Galvani
meson_gpiolib_register(struct meson_pinctrl * pc)6056ac73095SBeniamino Galvani static int meson_gpiolib_register(struct meson_pinctrl *pc)
6066ac73095SBeniamino Galvani {
6079dab1868SCarlo Caione int ret;
6086ac73095SBeniamino Galvani
609db80f0e1SBeniamino Galvani pc->chip.label = pc->data->name;
610db80f0e1SBeniamino Galvani pc->chip.parent = pc->dev;
611827eb27eSAndy Shevchenko pc->chip.fwnode = pc->fwnode;
612634e40b0SJerome Brunet pc->chip.request = gpiochip_generic_request;
613634e40b0SJerome Brunet pc->chip.free = gpiochip_generic_free;
614f8f0aa00SMartin Blumenstingl pc->chip.set_config = gpiochip_generic_config;
615ef1d0bceSMartin Blumenstingl pc->chip.get_direction = meson_gpio_get_direction;
616db80f0e1SBeniamino Galvani pc->chip.direction_input = meson_gpio_direction_input;
617db80f0e1SBeniamino Galvani pc->chip.direction_output = meson_gpio_direction_output;
618db80f0e1SBeniamino Galvani pc->chip.get = meson_gpio_get;
619db80f0e1SBeniamino Galvani pc->chip.set = meson_gpio_set;
620634e40b0SJerome Brunet pc->chip.base = -1;
621db80f0e1SBeniamino Galvani pc->chip.ngpio = pc->data->num_pins;
622db80f0e1SBeniamino Galvani pc->chip.can_sleep = false;
6236ac73095SBeniamino Galvani
624db80f0e1SBeniamino Galvani ret = gpiochip_add_data(&pc->chip, pc);
6256ac73095SBeniamino Galvani if (ret) {
6266ac73095SBeniamino Galvani dev_err(pc->dev, "can't add gpio chip %s\n",
627db80f0e1SBeniamino Galvani pc->data->name);
628c7fc5fbaSNeil Armstrong return ret;
6296ac73095SBeniamino Galvani }
6306ac73095SBeniamino Galvani
6316ac73095SBeniamino Galvani return 0;
6326ac73095SBeniamino Galvani }
6336ac73095SBeniamino Galvani
6346ac73095SBeniamino Galvani static struct regmap_config meson_regmap_config = {
6356ac73095SBeniamino Galvani .reg_bits = 32,
6366ac73095SBeniamino Galvani .val_bits = 32,
6376ac73095SBeniamino Galvani .reg_stride = 4,
6386ac73095SBeniamino Galvani };
6396ac73095SBeniamino Galvani
meson_map_resource(struct meson_pinctrl * pc,struct device_node * node,char * name)6406ac73095SBeniamino Galvani static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
6416ac73095SBeniamino Galvani struct device_node *node, char *name)
6426ac73095SBeniamino Galvani {
6436ac73095SBeniamino Galvani struct resource res;
6446ac73095SBeniamino Galvani void __iomem *base;
6456ac73095SBeniamino Galvani int i;
6466ac73095SBeniamino Galvani
6476ac73095SBeniamino Galvani i = of_property_match_string(node, "reg-names", name);
6486ac73095SBeniamino Galvani if (of_address_to_resource(node, i, &res))
649fd422964SQianggui Song return NULL;
6506ac73095SBeniamino Galvani
6516ac73095SBeniamino Galvani base = devm_ioremap_resource(pc->dev, &res);
6526ac73095SBeniamino Galvani if (IS_ERR(base))
6536ac73095SBeniamino Galvani return ERR_CAST(base);
6546ac73095SBeniamino Galvani
6556ac73095SBeniamino Galvani meson_regmap_config.max_register = resource_size(&res) - 4;
6566ac73095SBeniamino Galvani meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
65794f4e54cSRob Herring "%pOFn-%s", node,
6586ac73095SBeniamino Galvani name);
6596ac73095SBeniamino Galvani if (!meson_regmap_config.name)
6606ac73095SBeniamino Galvani return ERR_PTR(-ENOMEM);
6616ac73095SBeniamino Galvani
6626ac73095SBeniamino Galvani return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
6636ac73095SBeniamino Galvani }
6646ac73095SBeniamino Galvani
meson_pinctrl_parse_dt(struct meson_pinctrl * pc)665edc5601dSAndy Shevchenko static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc)
6666ac73095SBeniamino Galvani {
667edc5601dSAndy Shevchenko struct device_node *gpio_np;
668edc5601dSAndy Shevchenko unsigned int chips;
6696ac73095SBeniamino Galvani
670edc5601dSAndy Shevchenko chips = gpiochip_node_count(pc->dev);
671edc5601dSAndy Shevchenko if (!chips) {
672db80f0e1SBeniamino Galvani dev_err(pc->dev, "no gpio node found\n");
6736ac73095SBeniamino Galvani return -EINVAL;
6746ac73095SBeniamino Galvani }
675edc5601dSAndy Shevchenko if (chips > 1) {
676edc5601dSAndy Shevchenko dev_err(pc->dev, "multiple gpio nodes\n");
677edc5601dSAndy Shevchenko return -EINVAL;
678edc5601dSAndy Shevchenko }
6796ac73095SBeniamino Galvani
680827eb27eSAndy Shevchenko pc->fwnode = gpiochip_node_get_first(pc->dev);
681827eb27eSAndy Shevchenko gpio_np = to_of_node(pc->fwnode);
6826ac73095SBeniamino Galvani
683db80f0e1SBeniamino Galvani pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
684fd422964SQianggui Song if (IS_ERR_OR_NULL(pc->reg_mux)) {
6856ac73095SBeniamino Galvani dev_err(pc->dev, "mux registers not found\n");
686fd422964SQianggui Song return pc->reg_mux ? PTR_ERR(pc->reg_mux) : -ENOENT;
6876ac73095SBeniamino Galvani }
6886ac73095SBeniamino Galvani
689db80f0e1SBeniamino Galvani pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
690fd422964SQianggui Song if (IS_ERR_OR_NULL(pc->reg_gpio)) {
6916ac73095SBeniamino Galvani dev_err(pc->dev, "gpio registers not found\n");
692fd422964SQianggui Song return pc->reg_gpio ? PTR_ERR(pc->reg_gpio) : -ENOENT;
6936ac73095SBeniamino Galvani }
6946ac73095SBeniamino Galvani
695e66dd48eSXingyu Chen pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
696e66dd48eSXingyu Chen if (IS_ERR(pc->reg_pull))
697fd422964SQianggui Song pc->reg_pull = NULL;
698e66dd48eSXingyu Chen
699e66dd48eSXingyu Chen pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
700e66dd48eSXingyu Chen if (IS_ERR(pc->reg_pullen))
701fd422964SQianggui Song pc->reg_pullen = NULL;
702e66dd48eSXingyu Chen
70364856974SJerome Brunet pc->reg_ds = meson_map_resource(pc, gpio_np, "ds");
70464856974SJerome Brunet if (IS_ERR(pc->reg_ds)) {
70564856974SJerome Brunet dev_dbg(pc->dev, "ds registers not found - skipping\n");
70664856974SJerome Brunet pc->reg_ds = NULL;
70764856974SJerome Brunet }
70864856974SJerome Brunet
709fd422964SQianggui Song if (pc->data->parse_dt)
710fd422964SQianggui Song return pc->data->parse_dt(pc);
711fd422964SQianggui Song
712fd422964SQianggui Song return 0;
713fd422964SQianggui Song }
714fd422964SQianggui Song
meson8_aobus_parse_dt_extra(struct meson_pinctrl * pc)715fd422964SQianggui Song int meson8_aobus_parse_dt_extra(struct meson_pinctrl *pc)
716fd422964SQianggui Song {
717fd422964SQianggui Song if (!pc->reg_pull)
718fd422964SQianggui Song return -EINVAL;
719fd422964SQianggui Song
720fd422964SQianggui Song pc->reg_pullen = pc->reg_pull;
721fd422964SQianggui Song
7226ac73095SBeniamino Galvani return 0;
7236ac73095SBeniamino Galvani }
7249c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson8_aobus_parse_dt_extra);
7256ac73095SBeniamino Galvani
meson_a1_parse_dt_extra(struct meson_pinctrl * pc)726dabad1ffSQianggui Song int meson_a1_parse_dt_extra(struct meson_pinctrl *pc)
727dabad1ffSQianggui Song {
728dabad1ffSQianggui Song pc->reg_pull = pc->reg_gpio;
729dabad1ffSQianggui Song pc->reg_pullen = pc->reg_gpio;
730dabad1ffSQianggui Song pc->reg_ds = pc->reg_gpio;
731dabad1ffSQianggui Song
732dabad1ffSQianggui Song return 0;
733dabad1ffSQianggui Song }
7349c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson_a1_parse_dt_extra);
735dabad1ffSQianggui Song
meson_pinctrl_probe(struct platform_device * pdev)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
749edc5601dSAndy Shevchenko ret = meson_pinctrl_parse_dt(pc);
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 }
7699c65441eSKevin Hilman EXPORT_SYMBOL_GPL(meson_pinctrl_probe);
7709c65441eSKevin Hilman
7715224b7a9SJeff Johnson MODULE_DESCRIPTION("Amlogic Meson SoCs core pinctrl driver");
7729c65441eSKevin Hilman MODULE_LICENSE("GPL v2");
773