blob: f23b188f2f3f372250eebd629fccda886e0dcd7c [file] [log] [blame]
Jerome Brunet85878392018-10-05 09:36:37 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Jerome Brunet <jbrunet@baylibre.com>
4 * Copyright (C) 2017 Xingyu Chen <xingyu.chen@amlogic.com>
5 */
6
7#include <asm/gpio.h>
8#include <common.h>
9#include <dm.h>
10#include <dm/pinctrl.h>
11#include <linux/io.h>
12#include "pinctrl-meson-axg.h"
13
14static int meson_axg_pmx_get_bank(struct udevice *dev, unsigned int pin,
15 struct meson_pmx_bank **bank)
16{
17 int i;
18 struct meson_pinctrl *priv = dev_get_priv(dev);
19 struct meson_axg_pmx_data *pmx = priv->data->pmx_data;
20
21 for (i = 0; i < pmx->num_pmx_banks; i++)
22 if (pin >= pmx->pmx_banks[i].first &&
23 pin <= pmx->pmx_banks[i].last) {
24 *bank = &pmx->pmx_banks[i];
25 return 0;
26 }
27
28 return -EINVAL;
29}
30
31static int meson_axg_pmx_calc_reg_and_offset(struct meson_pmx_bank *bank,
32 unsigned int pin,
33 unsigned int *reg,
34 unsigned int *offset)
35{
36 int shift;
37
38 shift = pin - bank->first;
39
40 *reg = bank->reg + (bank->offset + (shift << 2)) / 32;
41 *offset = (bank->offset + (shift << 2)) % 32;
42
43 return 0;
44}
45
46static int meson_axg_pmx_update_function(struct udevice *dev,
47 unsigned int pin, unsigned int func)
48{
49 struct meson_pinctrl *priv = dev_get_priv(dev);
50 struct meson_pmx_bank *bank;
51 unsigned int offset;
52 unsigned int reg;
53 unsigned int tmp;
54 int ret;
55
56 ret = meson_axg_pmx_get_bank(dev, pin, &bank);
57 if (ret)
58 return ret;
59
60 meson_axg_pmx_calc_reg_and_offset(bank, pin, &reg, &offset);
61
62 tmp = readl(priv->reg_mux + (reg << 2));
63 tmp &= ~(0xf << offset);
64 tmp |= (func & 0xf) << offset;
65 writel(tmp, priv->reg_mux + (reg << 2));
66
67 return ret;
68}
69
70static int meson_axg_pinmux_group_set(struct udevice *dev,
71 unsigned int group_selector,
72 unsigned int func_selector)
73{
74 struct meson_pinctrl *priv = dev_get_priv(dev);
75 const struct meson_pmx_group *group;
76 const struct meson_pmx_func *func;
77 struct meson_pmx_axg_data *pmx_data;
78 int i, ret;
79
80 group = &priv->data->groups[group_selector];
81 pmx_data = (struct meson_pmx_axg_data *)group->data;
82 func = &priv->data->funcs[func_selector];
83
84 debug("pinmux: set group %s func %s\n", group->name, func->name);
85
86 for (i = 0; i < group->num_pins; i++) {
87 ret = meson_axg_pmx_update_function(dev, group->pins[i],
88 pmx_data->func);
89 if (ret)
90 return ret;
91 }
92
93 return 0;
94}
95
Jerome Brunetc4c726c2019-01-04 15:44:34 +010096const struct pinconf_param meson_axg_pinconf_params[] = {
97 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
98 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
99 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
100};
101
Jerome Brunet85878392018-10-05 09:36:37 +0200102const struct pinctrl_ops meson_axg_pinctrl_ops = {
103 .get_groups_count = meson_pinctrl_get_groups_count,
104 .get_group_name = meson_pinctrl_get_group_name,
105 .get_functions_count = meson_pinmux_get_functions_count,
106 .get_function_name = meson_pinmux_get_function_name,
107 .pinmux_group_set = meson_axg_pinmux_group_set,
108 .set_state = pinctrl_generic_set_state,
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100109 .pinconf_params = meson_axg_pinconf_params,
110 .pinconf_num_params = ARRAY_SIZE(meson_axg_pinconf_params),
111 .pinconf_set = meson_pinconf_set,
112 .pinconf_group_set = meson_pinconf_group_set,
Jerome Brunet85878392018-10-05 09:36:37 +0200113};
114
115static int meson_axg_gpio_request(struct udevice *dev,
116 unsigned int offset, const char *label)
117{
118 return meson_axg_pmx_update_function(dev->parent, offset, 0);
119}
120
121static const struct dm_gpio_ops meson_axg_gpio_ops = {
122 .request = meson_axg_gpio_request,
123 .set_value = meson_gpio_set,
124 .get_value = meson_gpio_get,
125 .get_function = meson_gpio_get_direction,
126 .direction_input = meson_gpio_direction_input,
127 .direction_output = meson_gpio_direction_output,
128};
129
130const struct driver meson_axg_gpio_driver = {
131 .name = "meson-axg-gpio",
132 .id = UCLASS_GPIO,
133 .probe = meson_gpio_probe,
134 .ops = &meson_axg_gpio_ops,
135};