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