blob: b37b517fe57b69ef2a8edc0abfa8df43c68b65b4 [file] [log] [blame]
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com>
4 */
5
6#include <asm/gpio.h>
7#include <common.h>
8#include <dm.h>
9#include <dm/pinctrl.h>
10#include <linux/io.h>
11#include "pinctrl-meson-gx.h"
12
13static void meson_gx_pinmux_disable_other_groups(struct meson_pinctrl *priv,
14 unsigned int pin,
15 int sel_group)
16{
17 struct meson_pmx_group *group;
18 struct meson_gx_pmx_data *pmx_data;
19 void __iomem *addr;
20 int i, j;
21
22 for (i = 0; i < priv->data->num_groups; i++) {
23 group = &priv->data->groups[i];
24 pmx_data = (struct meson_gx_pmx_data *)group->data;
25 if (pmx_data->is_gpio || i == sel_group)
26 continue;
27
28 for (j = 0; j < group->num_pins; j++) {
29 if (group->pins[j] == pin) {
30 /* We have found a group using the pin */
31 debug("pinmux: disabling %s\n", group->name);
32 addr = priv->reg_mux + pmx_data->reg * 4;
33 writel(readl(addr) & ~BIT(pmx_data->bit), addr);
34 }
35 }
36 }
37}
38
39static int meson_gx_pinmux_group_set(struct udevice *dev,
40 unsigned int group_selector,
41 unsigned int func_selector)
42{
43 struct meson_pinctrl *priv = dev_get_priv(dev);
44 const struct meson_pmx_group *group;
45 const struct meson_pmx_func *func;
46 struct meson_gx_pmx_data *pmx_data;
47 void __iomem *addr;
48 int i;
49
50 group = &priv->data->groups[group_selector];
51 pmx_data = (struct meson_gx_pmx_data *)group->data;
52 func = &priv->data->funcs[func_selector];
53
54 debug("pinmux: set group %s func %s\n", group->name, func->name);
55
56 /*
57 * Disable groups using the same pins.
58 * The selected group is not disabled to avoid glitches.
59 */
60 for (i = 0; i < group->num_pins; i++) {
61 meson_gx_pinmux_disable_other_groups(priv,
62 group->pins[i],
63 group_selector);
64 }
65
66 /* Function 0 (GPIO) doesn't need any additional setting */
67 if (func_selector) {
68 addr = priv->reg_mux + pmx_data->reg * 4;
69 writel(readl(addr) | BIT(pmx_data->bit), addr);
70 }
71
72 return 0;
73}
74
Neil Armstrong23922892019-06-04 11:04:53 +020075static int meson_gx_pinmux_get(struct udevice *dev,
76 unsigned int selector,
77 char *buf, int size)
78{
79 struct meson_pinctrl *priv = dev_get_priv(dev);
80 struct meson_pmx_group *group;
81 struct meson_gx_pmx_data *pmx_data;
82 void __iomem *addr;
83 int i, j, pos = 0;
84 unsigned int pin;
85 u32 reg;
86
87 pin = selector + priv->data->pin_base;
88
89 for (i = 0; i < priv->data->num_groups; i++) {
90 group = &priv->data->groups[i];
91 pmx_data = (struct meson_gx_pmx_data *)group->data;
92 if (pmx_data->is_gpio)
93 continue;
94
95 for (j = 0; j < group->num_pins; j++) {
96 if (group->pins[j] == pin) {
97 /* We have found a group using the pin */
98 addr = priv->reg_mux + pmx_data->reg * 4;
99 reg = readl(addr) & BIT(pmx_data->bit);
100 if (reg) {
101 pos += snprintf(buf + pos, size - pos,
102 "%s ", group->name) - 1;
103 return 0;
104 }
105 }
106 }
107 }
108
109 /* Fallback, must be used as GPIO */
110 snprintf(buf, size, "%s or Unknown",
111 priv->data->groups[selector].name);
112
113 return 0;
114}
115
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100116const struct pinconf_param meson_gx_pinconf_params[] = {
117 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
118 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
119 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
120};
121
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200122const struct pinctrl_ops meson_gx_pinctrl_ops = {
123 .get_groups_count = meson_pinctrl_get_groups_count,
124 .get_group_name = meson_pinctrl_get_group_name,
125 .get_functions_count = meson_pinmux_get_functions_count,
126 .get_function_name = meson_pinmux_get_function_name,
127 .pinmux_group_set = meson_gx_pinmux_group_set,
128 .set_state = pinctrl_generic_set_state,
Jerome Brunetc4c726c2019-01-04 15:44:34 +0100129 .pinconf_params = meson_gx_pinconf_params,
130 .pinconf_num_params = ARRAY_SIZE(meson_gx_pinconf_params),
131 .pinconf_set = meson_pinconf_set,
132 .pinconf_group_set = meson_pinconf_group_set,
Neil Armstrong23922892019-06-04 11:04:53 +0200133 .get_pin_name = meson_pinctrl_get_pin_name,
134 .get_pins_count = meson_pinctrl_get_pins_count,
135 .get_pin_muxing = meson_gx_pinmux_get,
Jerome Brunet7c9dcfe2018-10-05 09:35:26 +0200136};
137
138static const struct dm_gpio_ops meson_gx_gpio_ops = {
139 .set_value = meson_gpio_set,
140 .get_value = meson_gpio_get,
141 .get_function = meson_gpio_get_direction,
142 .direction_input = meson_gpio_direction_input,
143 .direction_output = meson_gpio_direction_output,
144};
145
146const struct driver meson_gx_gpio_driver = {
147 .name = "meson-gx-gpio",
148 .id = UCLASS_GPIO,
149 .probe = meson_gpio_probe,
150 .ops = &meson_gx_gpio_ops,
151};