blob: c82413d08f34d3041e5d0a937436be84866caf5a [file] [log] [blame]
Jerome Brunet004447d2018-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
96const struct pinctrl_ops meson_axg_pinctrl_ops = {
97 .get_groups_count = meson_pinctrl_get_groups_count,
98 .get_group_name = meson_pinctrl_get_group_name,
99 .get_functions_count = meson_pinmux_get_functions_count,
100 .get_function_name = meson_pinmux_get_function_name,
101 .pinmux_group_set = meson_axg_pinmux_group_set,
102 .set_state = pinctrl_generic_set_state,
103};
104
105static int meson_axg_gpio_request(struct udevice *dev,
106 unsigned int offset, const char *label)
107{
108 return meson_axg_pmx_update_function(dev->parent, offset, 0);
109}
110
111static const struct dm_gpio_ops meson_axg_gpio_ops = {
112 .request = meson_axg_gpio_request,
113 .set_value = meson_gpio_set,
114 .get_value = meson_gpio_get,
115 .get_function = meson_gpio_get_direction,
116 .direction_input = meson_gpio_direction_input,
117 .direction_output = meson_gpio_direction_output,
118};
119
120const struct driver meson_axg_gpio_driver = {
121 .name = "meson-axg-gpio",
122 .id = UCLASS_GPIO,
123 .probe = meson_gpio_probe,
124 .ops = &meson_axg_gpio_ops,
125};