18587839fSJerome Brunet // SPDX-License-Identifier: GPL-2.0+
28587839fSJerome Brunet /*
38587839fSJerome Brunet * Copyright (C) 2018 Jerome Brunet <jbrunet@baylibre.com>
48587839fSJerome Brunet * Copyright (C) 2017 Xingyu Chen <xingyu.chen@amlogic.com>
58587839fSJerome Brunet */
68587839fSJerome Brunet
78587839fSJerome Brunet #include <asm/gpio.h>
88587839fSJerome Brunet #include <common.h>
98587839fSJerome Brunet #include <dm.h>
108587839fSJerome Brunet #include <dm/pinctrl.h>
118587839fSJerome Brunet #include <linux/io.h>
128587839fSJerome Brunet #include "pinctrl-meson-axg.h"
138587839fSJerome Brunet
meson_axg_pmx_get_bank(struct udevice * dev,unsigned int pin,struct meson_pmx_bank ** bank)148587839fSJerome Brunet static int meson_axg_pmx_get_bank(struct udevice *dev, unsigned int pin,
158587839fSJerome Brunet struct meson_pmx_bank **bank)
168587839fSJerome Brunet {
178587839fSJerome Brunet int i;
188587839fSJerome Brunet struct meson_pinctrl *priv = dev_get_priv(dev);
198587839fSJerome Brunet struct meson_axg_pmx_data *pmx = priv->data->pmx_data;
208587839fSJerome Brunet
218587839fSJerome Brunet for (i = 0; i < pmx->num_pmx_banks; i++)
228587839fSJerome Brunet if (pin >= pmx->pmx_banks[i].first &&
238587839fSJerome Brunet pin <= pmx->pmx_banks[i].last) {
248587839fSJerome Brunet *bank = &pmx->pmx_banks[i];
258587839fSJerome Brunet return 0;
268587839fSJerome Brunet }
278587839fSJerome Brunet
288587839fSJerome Brunet return -EINVAL;
298587839fSJerome Brunet }
308587839fSJerome Brunet
meson_axg_pmx_calc_reg_and_offset(struct meson_pmx_bank * bank,unsigned int pin,unsigned int * reg,unsigned int * offset)318587839fSJerome Brunet static int meson_axg_pmx_calc_reg_and_offset(struct meson_pmx_bank *bank,
328587839fSJerome Brunet unsigned int pin,
338587839fSJerome Brunet unsigned int *reg,
348587839fSJerome Brunet unsigned int *offset)
358587839fSJerome Brunet {
368587839fSJerome Brunet int shift;
378587839fSJerome Brunet
388587839fSJerome Brunet shift = pin - bank->first;
398587839fSJerome Brunet
408587839fSJerome Brunet *reg = bank->reg + (bank->offset + (shift << 2)) / 32;
418587839fSJerome Brunet *offset = (bank->offset + (shift << 2)) % 32;
428587839fSJerome Brunet
438587839fSJerome Brunet return 0;
448587839fSJerome Brunet }
458587839fSJerome Brunet
meson_axg_pmx_update_function(struct udevice * dev,unsigned int pin,unsigned int func)468587839fSJerome Brunet static int meson_axg_pmx_update_function(struct udevice *dev,
478587839fSJerome Brunet unsigned int pin, unsigned int func)
488587839fSJerome Brunet {
498587839fSJerome Brunet struct meson_pinctrl *priv = dev_get_priv(dev);
508587839fSJerome Brunet struct meson_pmx_bank *bank;
518587839fSJerome Brunet unsigned int offset;
528587839fSJerome Brunet unsigned int reg;
538587839fSJerome Brunet unsigned int tmp;
548587839fSJerome Brunet int ret;
558587839fSJerome Brunet
568587839fSJerome Brunet ret = meson_axg_pmx_get_bank(dev, pin, &bank);
578587839fSJerome Brunet if (ret)
588587839fSJerome Brunet return ret;
598587839fSJerome Brunet
608587839fSJerome Brunet meson_axg_pmx_calc_reg_and_offset(bank, pin, ®, &offset);
618587839fSJerome Brunet
628587839fSJerome Brunet tmp = readl(priv->reg_mux + (reg << 2));
638587839fSJerome Brunet tmp &= ~(0xf << offset);
648587839fSJerome Brunet tmp |= (func & 0xf) << offset;
658587839fSJerome Brunet writel(tmp, priv->reg_mux + (reg << 2));
668587839fSJerome Brunet
678587839fSJerome Brunet return ret;
688587839fSJerome Brunet }
698587839fSJerome Brunet
meson_axg_pinmux_group_set(struct udevice * dev,unsigned int group_selector,unsigned int func_selector)708587839fSJerome Brunet static int meson_axg_pinmux_group_set(struct udevice *dev,
718587839fSJerome Brunet unsigned int group_selector,
728587839fSJerome Brunet unsigned int func_selector)
738587839fSJerome Brunet {
748587839fSJerome Brunet struct meson_pinctrl *priv = dev_get_priv(dev);
758587839fSJerome Brunet const struct meson_pmx_group *group;
768587839fSJerome Brunet const struct meson_pmx_func *func;
778587839fSJerome Brunet struct meson_pmx_axg_data *pmx_data;
788587839fSJerome Brunet int i, ret;
798587839fSJerome Brunet
808587839fSJerome Brunet group = &priv->data->groups[group_selector];
818587839fSJerome Brunet pmx_data = (struct meson_pmx_axg_data *)group->data;
828587839fSJerome Brunet func = &priv->data->funcs[func_selector];
838587839fSJerome Brunet
848587839fSJerome Brunet debug("pinmux: set group %s func %s\n", group->name, func->name);
858587839fSJerome Brunet
868587839fSJerome Brunet for (i = 0; i < group->num_pins; i++) {
878587839fSJerome Brunet ret = meson_axg_pmx_update_function(dev, group->pins[i],
888587839fSJerome Brunet pmx_data->func);
898587839fSJerome Brunet if (ret)
908587839fSJerome Brunet return ret;
918587839fSJerome Brunet }
928587839fSJerome Brunet
938587839fSJerome Brunet return 0;
948587839fSJerome Brunet }
958587839fSJerome Brunet
96*c4c726c2SJerome Brunet const struct pinconf_param meson_axg_pinconf_params[] = {
97*c4c726c2SJerome Brunet { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
98*c4c726c2SJerome Brunet { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
99*c4c726c2SJerome Brunet { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
100*c4c726c2SJerome Brunet };
101*c4c726c2SJerome Brunet
1028587839fSJerome Brunet const struct pinctrl_ops meson_axg_pinctrl_ops = {
1038587839fSJerome Brunet .get_groups_count = meson_pinctrl_get_groups_count,
1048587839fSJerome Brunet .get_group_name = meson_pinctrl_get_group_name,
1058587839fSJerome Brunet .get_functions_count = meson_pinmux_get_functions_count,
1068587839fSJerome Brunet .get_function_name = meson_pinmux_get_function_name,
1078587839fSJerome Brunet .pinmux_group_set = meson_axg_pinmux_group_set,
1088587839fSJerome Brunet .set_state = pinctrl_generic_set_state,
109*c4c726c2SJerome Brunet .pinconf_params = meson_axg_pinconf_params,
110*c4c726c2SJerome Brunet .pinconf_num_params = ARRAY_SIZE(meson_axg_pinconf_params),
111*c4c726c2SJerome Brunet .pinconf_set = meson_pinconf_set,
112*c4c726c2SJerome Brunet .pinconf_group_set = meson_pinconf_group_set,
1138587839fSJerome Brunet };
1148587839fSJerome Brunet
meson_axg_gpio_request(struct udevice * dev,unsigned int offset,const char * label)1158587839fSJerome Brunet static int meson_axg_gpio_request(struct udevice *dev,
1168587839fSJerome Brunet unsigned int offset, const char *label)
1178587839fSJerome Brunet {
1188587839fSJerome Brunet return meson_axg_pmx_update_function(dev->parent, offset, 0);
1198587839fSJerome Brunet }
1208587839fSJerome Brunet
1218587839fSJerome Brunet static const struct dm_gpio_ops meson_axg_gpio_ops = {
1228587839fSJerome Brunet .request = meson_axg_gpio_request,
1238587839fSJerome Brunet .set_value = meson_gpio_set,
1248587839fSJerome Brunet .get_value = meson_gpio_get,
1258587839fSJerome Brunet .get_function = meson_gpio_get_direction,
1268587839fSJerome Brunet .direction_input = meson_gpio_direction_input,
1278587839fSJerome Brunet .direction_output = meson_gpio_direction_output,
1288587839fSJerome Brunet };
1298587839fSJerome Brunet
1308587839fSJerome Brunet const struct driver meson_axg_gpio_driver = {
1318587839fSJerome Brunet .name = "meson-axg-gpio",
1328587839fSJerome Brunet .id = UCLASS_GPIO,
1338587839fSJerome Brunet .probe = meson_gpio_probe,
1348587839fSJerome Brunet .ops = &meson_axg_gpio_ops,
1358587839fSJerome Brunet };
136