blob: e4b5383d0f7284150c3280abc17e4827f8e0dc1b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Minkyu Kangab693e92010-02-12 18:17:52 +09002/*
3 * (C) Copyright 2009 Samsung Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
Minkyu Kangab693e92010-02-12 18:17:52 +09005 */
6
7#include <common.h>
Simon Glassb8809e62014-10-20 19:48:40 -06008#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <malloc.h>
Minkyu Kangab693e92010-02-12 18:17:52 +090012#include <asm/io.h>
Joe Hershberger365d6072011-11-11 15:55:36 -060013#include <asm/gpio.h>
Simon Glassb8809e62014-10-20 19:48:40 -060014#include <dm/device-internal.h>
15
16DECLARE_GLOBAL_DATA_PTR;
Minkyu Kangab693e92010-02-12 18:17:52 +090017
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053018#define S5P_GPIO_GET_PIN(x) (x % GPIO_PER_BANK)
Przemyslaw Marczak8475c862014-01-22 11:24:10 +010019
Simon Glass3a233db2014-10-20 19:48:38 -060020#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))
Minkyu Kangab693e92010-02-12 18:17:52 +090023
Simon Glass3a233db2014-10-20 19:48:38 -060024#define DAT_MASK(gpio) (0x1 << (gpio))
25#define DAT_SET(gpio) (0x1 << (gpio))
Minkyu Kangab693e92010-02-12 18:17:52 +090026
Simon Glass3a233db2014-10-20 19:48:38 -060027#define PULL_MASK(gpio) (0x3 << ((gpio) << 1))
28#define PULL_MODE(gpio, pull) ((pull) << ((gpio) << 1))
Minkyu Kangab693e92010-02-12 18:17:52 +090029
Simon Glass3a233db2014-10-20 19:48:38 -060030#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))
Minkyu Kangab693e92010-02-12 18:17:52 +090034
Simon Glassb8809e62014-10-20 19:48:40 -060035/* Platform data for each bank */
36struct exynos_gpio_platdata {
37 struct s5p_gpio_bank *bank;
38 const char *bank_name; /* Name of port, e.g. 'gpa0" */
39};
40
41/* Information about each bank at run-time */
42struct exynos_bank_info {
Simon Glassb8809e62014-10-20 19:48:40 -060043 struct s5p_gpio_bank *bank;
44};
45
46static struct s5p_gpio_bank *s5p_gpio_get_bank(unsigned int gpio)
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053047{
Simon Glassb8809e62014-10-20 19:48:40 -060048 const struct gpio_info *data;
49 unsigned int upto;
50 int i, count;
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053051
Simon Glassb8809e62014-10-20 19:48:40 -060052 data = get_gpio_data();
53 count = get_bank_num();
54 upto = 0;
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053055
Simon Glassb8809e62014-10-20 19:48:40 -060056 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);
63 return bank;
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053064 }
Simon Glassb8809e62014-10-20 19:48:40 -060065
66 upto = data->max_gpio;
67 data++;
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053068 }
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053069
Simon Glassb8809e62014-10-20 19:48:40 -060070 return NULL;
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053071}
72
73static void s5p_gpio_cfg_pin(struct s5p_gpio_bank *bank, int gpio, int cfg)
Minkyu Kangab693e92010-02-12 18:17:52 +090074{
75 unsigned int value;
76
77 value = readl(&bank->con);
78 value &= ~CON_MASK(gpio);
79 value |= CON_SFR(gpio, cfg);
80 writel(value, &bank->con);
81}
82
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053083static void s5p_gpio_set_value(struct s5p_gpio_bank *bank, int gpio, int en)
Minkyu Kangab693e92010-02-12 18:17:52 +090084{
85 unsigned int value;
86
87 value = readl(&bank->dat);
88 value &= ~DAT_MASK(gpio);
89 if (en)
90 value |= DAT_SET(gpio);
91 writel(value, &bank->dat);
92}
93
Simon Glassb8809e62014-10-20 19:48:40 -060094#ifdef CONFIG_SPL_BUILD
95/* Common GPIO API - SPL does not support driver model yet */
96int gpio_set_value(unsigned gpio, int value)
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +053097{
Simon Glassb8809e62014-10-20 19:48:40 -060098 s5p_gpio_set_value(s5p_gpio_get_bank(gpio),
99 s5p_gpio_get_pin(gpio), value);
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530100
Simon Glassb8809e62014-10-20 19:48:40 -0600101 return 0;
102}
103#else
104static int s5p_gpio_get_cfg_pin(struct s5p_gpio_bank *bank, int gpio)
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530105{
Simon Glassb8809e62014-10-20 19:48:40 -0600106 unsigned int value;
107
108 value = readl(&bank->con);
109 value &= CON_MASK(gpio);
110 return CON_SFR_UNSHIFT(value, gpio);
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530111}
112
113static unsigned int s5p_gpio_get_value(struct s5p_gpio_bank *bank, int gpio)
Minkyu Kangab693e92010-02-12 18:17:52 +0900114{
115 unsigned int value;
116
117 value = readl(&bank->dat);
118 return !!(value & DAT_MASK(gpio));
119}
Simon Glassb8809e62014-10-20 19:48:40 -0600120#endif /* CONFIG_SPL_BUILD */
Minkyu Kangab693e92010-02-12 18:17:52 +0900121
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530122static void s5p_gpio_set_pull(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kangab693e92010-02-12 18:17:52 +0900123{
124 unsigned int value;
125
126 value = readl(&bank->pull);
127 value &= ~PULL_MASK(gpio);
128
129 switch (mode) {
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530130 case S5P_GPIO_PULL_DOWN:
131 case S5P_GPIO_PULL_UP:
Minkyu Kangab693e92010-02-12 18:17:52 +0900132 value |= PULL_MODE(gpio, mode);
133 break;
134 default:
Minkyu Kangffb4b022010-05-28 12:34:29 +0900135 break;
Minkyu Kangab693e92010-02-12 18:17:52 +0900136 }
137
138 writel(value, &bank->pull);
139}
140
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530141static void s5p_gpio_set_drv(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kangab693e92010-02-12 18:17:52 +0900142{
143 unsigned int value;
144
145 value = readl(&bank->drv);
146 value &= ~DRV_MASK(gpio);
147
148 switch (mode) {
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530149 case S5P_GPIO_DRV_1X:
150 case S5P_GPIO_DRV_2X:
151 case S5P_GPIO_DRV_3X:
152 case S5P_GPIO_DRV_4X:
Minkyu Kangab693e92010-02-12 18:17:52 +0900153 value |= DRV_SET(gpio, mode);
154 break;
155 default:
156 return;
157 }
158
159 writel(value, &bank->drv);
160}
161
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530162static void s5p_gpio_set_rate(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kangab693e92010-02-12 18:17:52 +0900163{
164 unsigned int value;
165
166 value = readl(&bank->drv);
167 value &= ~RATE_MASK(gpio);
168
169 switch (mode) {
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530170 case S5P_GPIO_DRV_FAST:
171 case S5P_GPIO_DRV_SLOW:
Minkyu Kangab693e92010-02-12 18:17:52 +0900172 value |= RATE_SET(gpio);
173 break;
174 default:
175 return;
176 }
177
178 writel(value, &bank->drv);
179}
Łukasz Majewski9f15bc02011-08-22 22:34:58 +0000180
Joe Hershberger365d6072011-11-11 15:55:36 -0600181int s5p_gpio_get_pin(unsigned gpio)
Łukasz Majewski9f15bc02011-08-22 22:34:58 +0000182{
Przemyslaw Marczak8475c862014-01-22 11:24:10 +0100183 return S5P_GPIO_GET_PIN(gpio);
Łukasz Majewski9f15bc02011-08-22 22:34:58 +0000184}
185
Simon Glassb8809e62014-10-20 19:48:40 -0600186/* Driver model interface */
187#ifndef CONFIG_SPL_BUILD
Simon Glassb8809e62014-10-20 19:48:40 -0600188/* set GPIO pin 'gpio' as an input */
189static int exynos_gpio_direction_input(struct udevice *dev, unsigned offset)
190{
191 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600192
193 /* Configure GPIO direction as input. */
194 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_INPUT);
195
196 return 0;
197}
198
199/* set GPIO pin 'gpio' as an output, with polarity 'value' */
200static int exynos_gpio_direction_output(struct udevice *dev, unsigned offset,
201 int value)
202{
203 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600204
205 /* Configure GPIO output value. */
206 s5p_gpio_set_value(state->bank, offset, value);
207
208 /* Configure GPIO direction as output. */
209 s5p_gpio_cfg_pin(state->bank, offset, S5P_GPIO_OUTPUT);
210
211 return 0;
212}
213
214/* read GPIO IN value of pin 'gpio' */
215static int exynos_gpio_get_value(struct udevice *dev, unsigned offset)
216{
217 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600218
219 return s5p_gpio_get_value(state->bank, offset);
220}
221
222/* write GPIO OUT value to pin 'gpio' */
223static int exynos_gpio_set_value(struct udevice *dev, unsigned offset,
224 int value)
225{
226 struct exynos_bank_info *state = dev_get_priv(dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600227
228 s5p_gpio_set_value(state->bank, offset, value);
229
230 return 0;
231}
Simon Glassb8809e62014-10-20 19:48:40 -0600232#endif /* nCONFIG_SPL_BUILD */
233
234/*
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.
237 */
Akshay Saraswatf6ae1ca2014-05-13 10:30:14 +0530238void gpio_set_pull(int gpio, int mode)
239{
240 s5p_gpio_set_pull(s5p_gpio_get_bank(gpio),
241 s5p_gpio_get_pin(gpio), mode);
242}
243
244void gpio_set_drv(int gpio, int mode)
245{
246 s5p_gpio_set_drv(s5p_gpio_get_bank(gpio),
247 s5p_gpio_get_pin(gpio), mode);
248}
249
250void gpio_cfg_pin(int gpio, int cfg)
251{
252 s5p_gpio_cfg_pin(s5p_gpio_get_bank(gpio),
253 s5p_gpio_get_pin(gpio), cfg);
254}
255
256void gpio_set_rate(int gpio, int mode)
257{
258 s5p_gpio_set_rate(s5p_gpio_get_bank(gpio),
259 s5p_gpio_get_pin(gpio), mode);
260}
Simon Glassb8809e62014-10-20 19:48:40 -0600261
262#ifndef CONFIG_SPL_BUILD
263static int exynos_gpio_get_function(struct udevice *dev, unsigned offset)
264{
265 struct exynos_bank_info *state = dev_get_priv(dev);
266 int cfg;
267
Simon Glassb8809e62014-10-20 19:48:40 -0600268 cfg = s5p_gpio_get_cfg_pin(state->bank, offset);
269 if (cfg == S5P_GPIO_OUTPUT)
270 return GPIOF_OUTPUT;
271 else if (cfg == S5P_GPIO_INPUT)
272 return GPIOF_INPUT;
273 else
274 return GPIOF_FUNC;
275}
276
277static const struct dm_gpio_ops gpio_exynos_ops = {
Simon Glassb8809e62014-10-20 19:48:40 -0600278 .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,
Simon Glassb8809e62014-10-20 19:48:40 -0600283};
284
285static int gpio_exynos_probe(struct udevice *dev)
286{
Simon Glasse564f052015-03-05 12:25:20 -0700287 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600288 struct exynos_bank_info *priv = dev->priv;
289 struct exynos_gpio_platdata *plat = dev->platdata;
290
291 /* Only child devices have ports */
292 if (!plat)
293 return 0;
294
295 priv->bank = plat->bank;
296
297 uc_priv->gpio_count = GPIO_PER_BANK;
298 uc_priv->bank_name = plat->bank_name;
299
300 return 0;
301}
302
303/**
304 * We have a top-level GPIO device with no actual GPIOs. It has a child
305 * device for each Exynos GPIO bank.
306 */
307static int gpio_exynos_bind(struct udevice *parent)
308{
309 struct exynos_gpio_platdata *plat = parent->platdata;
310 struct s5p_gpio_bank *bank, *base;
311 const void *blob = gd->fdt_blob;
312 int node;
313
314 /* If this is a child device, there is nothing to do here */
315 if (plat)
316 return 0;
317
Simon Glassa821c4a2017-05-17 17:18:05 -0600318 base = (struct s5p_gpio_bank *)devfdt_get_addr(parent);
Simon Glasse160f7d2017-01-17 16:52:55 -0700319 for (node = fdt_first_subnode(blob, dev_of_offset(parent)), bank = base;
Simon Glassb8809e62014-10-20 19:48:40 -0600320 node > 0;
321 node = fdt_next_subnode(blob, node), bank++) {
322 struct exynos_gpio_platdata *plat;
323 struct udevice *dev;
324 fdt_addr_t reg;
325 int ret;
326
327 if (!fdtdec_get_bool(blob, node, "gpio-controller"))
328 continue;
329 plat = calloc(1, sizeof(*plat));
330 if (!plat)
331 return -ENOMEM;
Simon Glassb8809e62014-10-20 19:48:40 -0600332
Przemyslaw Marczak6f183e82015-09-30 13:14:51 +0200333 plat->bank_name = fdt_get_name(blob, node, NULL);
Simon Glassb8809e62014-10-20 19:48:40 -0600334 ret = device_bind(parent, parent->driver,
Przemyslaw Marczak6f183e82015-09-30 13:14:51 +0200335 plat->bank_name, plat, -1, &dev);
Simon Glassb8809e62014-10-20 19:48:40 -0600336 if (ret)
337 return ret;
Przemyslaw Marczak6f183e82015-09-30 13:14:51 +0200338
Simon Glasse160f7d2017-01-17 16:52:55 -0700339 dev_set_of_offset(dev, node);
Przemyslaw Marczak6f183e82015-09-30 13:14:51 +0200340
Simon Glassa821c4a2017-05-17 17:18:05 -0600341 reg = devfdt_get_addr(dev);
Przemyslaw Marczak6f183e82015-09-30 13:14:51 +0200342 if (reg != FDT_ADDR_T_NONE)
343 bank = (struct s5p_gpio_bank *)((ulong)base + reg);
344
345 plat->bank = bank;
346
347 debug("dev at %p: %s\n", bank, plat->bank_name);
Simon Glassb8809e62014-10-20 19:48:40 -0600348 }
349
350 return 0;
351}
352
353static 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" },
360 { }
361};
362
363U_BOOT_DRIVER(gpio_exynos) = {
364 .name = "gpio_exynos",
365 .id = UCLASS_GPIO,
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,
371};
372#endif