1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009 Samsung Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
14 #include <dm/device-internal.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define S5P_GPIO_GET_PIN(x) (x % GPIO_PER_BANK)
20 #define CON_MASK(val) (0xf << ((val) << 2))
21 #define CON_SFR(gpio, cfg) ((cfg) << ((gpio) << 2))
22 #define CON_SFR_UNSHIFT(val, gpio) ((val) >> ((gpio) << 2))
24 #define DAT_MASK(gpio) (0x1 << (gpio))
25 #define DAT_SET(gpio) (0x1 << (gpio))
27 #define PULL_MASK(gpio) (0x3 << ((gpio) << 1))
28 #define PULL_MODE(gpio, pull) ((pull) << ((gpio) << 1))
30 #define DRV_MASK(gpio) (0x3 << ((gpio) << 1))
31 #define DRV_SET(gpio, mode) ((mode) << ((gpio) << 1))
32 #define RATE_MASK(gpio) (0x1 << (gpio + 16))
33 #define RATE_SET(gpio) (0x1 << (gpio + 16))
35 /* Platform data for each bank */
36 struct exynos_gpio_platdata {
37 struct s5p_gpio_bank *bank;
38 const char *bank_name; /* Name of port, e.g. 'gpa0" */
41 /* Information about each bank at run-time */
42 struct exynos_bank_info {
43 struct s5p_gpio_bank *bank;
46 static struct s5p_gpio_bank *s5p_gpio_get_bank(unsigned int gpio)
48 const struct gpio_info *data;
52 data = get_gpio_data();
53 count = get_bank_num();
56 for (i = 0; i < count; i++) {
57 debug("i=%d, upto=%d\n", i, upto);
58 if (gpio < data->max_gpio) {
59 struct s5p_gpio_bank *bank;
60 bank = (struct s5p_gpio_bank *)data->reg_addr;
61 bank += (gpio - upto) / GPIO_PER_BANK;
62 debug("gpio=%d, bank=%p\n", gpio, bank);
66 upto = data->max_gpio;
73 static void s5p_gpio_cfg_pin(struct s5p_gpio_bank *bank, int gpio, int cfg)
77 value = readl(&bank->con);
78 value &= ~CON_MASK(gpio);
79 value |= CON_SFR(gpio, cfg);
80 writel(value, &bank->con);
83 static void s5p_gpio_set_value(struct s5p_gpio_bank *bank, int gpio, int en)
87 value = readl(&bank->dat);
88 value &= ~DAT_MASK(gpio);
90 value |= DAT_SET(gpio);
91 writel(value, &bank->dat);
94 #ifdef CONFIG_SPL_BUILD
95 /* Common GPIO API - SPL does not support driver model yet */
96 int gpio_set_value(unsigned gpio, int value)
98 s5p_gpio_set_value(s5p_gpio_get_bank(gpio),
99 s5p_gpio_get_pin(gpio), value);
104 static int s5p_gpio_get_cfg_pin(struct s5p_gpio_bank *bank, int gpio)
108 value = readl(&bank->con);
109 value &= CON_MASK(gpio);
110 return CON_SFR_UNSHIFT(value, gpio);
113 static unsigned int s5p_gpio_get_value(struct s5p_gpio_bank *bank, int gpio)
117 value = readl(&bank->dat);
118 return !!(value & DAT_MASK(gpio));
120 #endif /* CONFIG_SPL_BUILD */
122 static void s5p_gpio_set_pull(struct s5p_gpio_bank *bank, int gpio, int mode)
126 value = readl(&bank->pull);
127 value &= ~PULL_MASK(gpio);
130 case S5P_GPIO_PULL_DOWN:
131 case S5P_GPIO_PULL_UP:
132 value |= PULL_MODE(gpio, mode);
138 writel(value, &bank->pull);
141 static void s5p_gpio_set_drv(struct s5p_gpio_bank *bank, int gpio, int mode)
145 value = readl(&bank->drv);
146 value &= ~DRV_MASK(gpio);
149 case S5P_GPIO_DRV_1X:
150 case S5P_GPIO_DRV_2X:
151 case S5P_GPIO_DRV_3X:
152 case S5P_GPIO_DRV_4X:
153 value |= DRV_SET(gpio, mode);
159 writel(value, &bank->drv);
162 static void s5p_gpio_set_rate(struct s5p_gpio_bank *bank, int gpio, int mode)
166 value = readl(&bank->drv);
167 value &= ~RATE_MASK(gpio);
170 case S5P_GPIO_DRV_FAST:
171 case S5P_GPIO_DRV_SLOW:
172 value |= RATE_SET(gpio);
178 writel(value, &bank->drv);
181 int s5p_gpio_get_pin(unsigned gpio)
183 return S5P_GPIO_GET_PIN(gpio);
186 /* Driver model interface */
187 #ifndef CONFIG_SPL_BUILD
188 /* set GPIO pin 'gpio' as an input */
189 static int exynos_gpio_direction_input(struct udevice *dev, unsigned offset)
191 struct exynos_bank_info *state = dev_get_priv(dev);
193 /* Configure GPIO direction as input. */
194 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_INPUT);
199 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
200 static int exynos_gpio_direction_output(struct udevice *dev, unsigned offset,
203 struct exynos_bank_info *state = dev_get_priv(dev);
205 /* Configure GPIO output value. */
206 s5p_gpio_set_value(state->bank, offset, value);
208 /* Configure GPIO direction as output. */
209 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_OUTPUT);
214 /* read GPIO IN value of pin 'gpio' */
215 static int exynos_gpio_get_value(struct udevice *dev, unsigned offset)
217 struct exynos_bank_info *state = dev_get_priv(dev);
219 return s5p_gpio_get_value(state->bank, offset);
222 /* write GPIO OUT value to pin 'gpio' */
223 static int exynos_gpio_set_value(struct udevice *dev, unsigned offset,
226 struct exynos_bank_info *state = dev_get_priv(dev);
228 s5p_gpio_set_value(state->bank, offset, value);
232 #endif /* nCONFIG_SPL_BUILD */
235 * There is no common GPIO API for pull, drv, pin, rate (yet). These
236 * functions are kept here to preserve function ordering for review.
238 void gpio_set_pull(int gpio, int mode)
240 s5p_gpio_set_pull(s5p_gpio_get_bank(gpio),
241 s5p_gpio_get_pin(gpio), mode);
244 void gpio_set_drv(int gpio, int mode)
246 s5p_gpio_set_drv(s5p_gpio_get_bank(gpio),
247 s5p_gpio_get_pin(gpio), mode);
250 void gpio_cfg_pin(int gpio, int cfg)
252 s5p_gpio_cfg_pin(s5p_gpio_get_bank(gpio),
253 s5p_gpio_get_pin(gpio), cfg);
256 void gpio_set_rate(int gpio, int mode)
258 s5p_gpio_set_rate(s5p_gpio_get_bank(gpio),
259 s5p_gpio_get_pin(gpio), mode);
262 #ifndef CONFIG_SPL_BUILD
263 static int exynos_gpio_get_function(struct udevice *dev, unsigned offset)
265 struct exynos_bank_info *state = dev_get_priv(dev);
268 cfg = s5p_gpio_get_cfg_pin(state->bank, offset);
269 if (cfg == S5P_GPIO_OUTPUT)
271 else if (cfg == S5P_GPIO_INPUT)
277 static const struct dm_gpio_ops gpio_exynos_ops = {
278 .direction_input = exynos_gpio_direction_input,
279 .direction_output = exynos_gpio_direction_output,
280 .get_value = exynos_gpio_get_value,
281 .set_value = exynos_gpio_set_value,
282 .get_function = exynos_gpio_get_function,
285 static int gpio_exynos_probe(struct udevice *dev)
287 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
288 struct exynos_bank_info *priv = dev->priv;
289 struct exynos_gpio_platdata *plat = dev->platdata;
291 /* Only child devices have ports */
295 priv->bank = plat->bank;
297 uc_priv->gpio_count = GPIO_PER_BANK;
298 uc_priv->bank_name = plat->bank_name;
304 * We have a top-level GPIO device with no actual GPIOs. It has a child
305 * device for each Exynos GPIO bank.
307 static int gpio_exynos_bind(struct udevice *parent)
309 struct exynos_gpio_platdata *plat = parent->platdata;
310 struct s5p_gpio_bank *bank, *base;
311 const void *blob = gd->fdt_blob;
314 /* If this is a child device, there is nothing to do here */
318 base = (struct s5p_gpio_bank *)devfdt_get_addr(parent);
319 for (node = fdt_first_subnode(blob, dev_of_offset(parent)), bank = base;
321 node = fdt_next_subnode(blob, node), bank++) {
322 struct exynos_gpio_platdata *plat;
327 if (!fdtdec_get_bool(blob, node, "gpio-controller"))
329 plat = calloc(1, sizeof(*plat));
333 plat->bank_name = fdt_get_name(blob, node, NULL);
334 ret = device_bind(parent, parent->driver,
335 plat->bank_name, plat, -1, &dev);
339 dev_set_of_offset(dev, node);
341 reg = devfdt_get_addr(dev);
342 if (reg != FDT_ADDR_T_NONE)
343 bank = (struct s5p_gpio_bank *)((ulong)base + reg);
347 debug("dev at %p: %s\n", bank, plat->bank_name);
353 static const struct udevice_id exynos_gpio_ids[] = {
354 { .compatible = "samsung,s5pc100-pinctrl" },
355 { .compatible = "samsung,s5pc110-pinctrl" },
356 { .compatible = "samsung,exynos4210-pinctrl" },
357 { .compatible = "samsung,exynos4x12-pinctrl" },
358 { .compatible = "samsung,exynos5250-pinctrl" },
359 { .compatible = "samsung,exynos5420-pinctrl" },
363 U_BOOT_DRIVER(gpio_exynos) = {
364 .name = "gpio_exynos",
366 .of_match = exynos_gpio_ids,
367 .bind = gpio_exynos_bind,
368 .probe = gpio_exynos_probe,
369 .priv_auto_alloc_size = sizeof(struct exynos_bank_info),
370 .ops = &gpio_exynos_ops,