2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
5 * SPDX-License-Identifier: GPL-2.0
7 * This work is derived from the linux 2.6.27 kernel source
8 * To fetch, use the kernel repository
9 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
10 * Use the v2.6.27 tag.
12 * Below is the original's header including its copyright
14 * linux/arch/arm/plat-omap/gpio.c
16 * Support functions for OMAP GPIO
18 * Copyright (C) 2003-2005 Nokia Corporation
19 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
26 #include <asm/errno.h>
28 #include <dt-bindings/gpio/gpio.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #define OMAP_GPIO_DIR_OUT 0
33 #define OMAP_GPIO_DIR_IN 1
37 #define GPIO_PER_BANK 32
40 /* TODO(sjg@chromium.org): Can we use a struct here? */
41 void *base; /* address of registers in physical memory */
46 static inline int get_gpio_index(int gpio)
51 int gpio_is_valid(int gpio)
53 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
56 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
59 void *reg = bank->base;
73 * Get the direction of the GPIO by reading the GPIO_OE register
74 * corresponding to the specified bank.
76 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
78 void *reg = bank->base;
86 return OMAP_GPIO_DIR_IN;
88 return OMAP_GPIO_DIR_OUT;
91 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
94 void *reg = bank->base;
98 reg += OMAP_GPIO_SETDATAOUT;
100 reg += OMAP_GPIO_CLEARDATAOUT;
103 __raw_writel(l, reg);
106 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
108 void *reg = bank->base;
111 input = _get_gpio_direction(bank, gpio);
113 case OMAP_GPIO_DIR_IN:
114 reg += OMAP_GPIO_DATAIN;
116 case OMAP_GPIO_DIR_OUT:
117 reg += OMAP_GPIO_DATAOUT;
123 return (__raw_readl(reg) & (1 << gpio)) != 0;
126 #ifndef CONFIG_DM_GPIO
128 static inline const struct gpio_bank *get_gpio_bank(int gpio)
130 return &omap_gpio_bank[gpio >> 5];
133 static int check_gpio(int gpio)
135 if (!gpio_is_valid(gpio)) {
136 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
143 * Set value of the specified gpio
145 int gpio_set_value(unsigned gpio, int value)
147 const struct gpio_bank *bank;
149 if (check_gpio(gpio) < 0)
151 bank = get_gpio_bank(gpio);
152 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
158 * Get value of the specified gpio
160 int gpio_get_value(unsigned gpio)
162 const struct gpio_bank *bank;
164 if (check_gpio(gpio) < 0)
166 bank = get_gpio_bank(gpio);
168 return _get_gpio_value(bank, get_gpio_index(gpio));
172 * Set gpio direction as input
174 int gpio_direction_input(unsigned gpio)
176 const struct gpio_bank *bank;
178 if (check_gpio(gpio) < 0)
181 bank = get_gpio_bank(gpio);
182 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
188 * Set gpio direction as output
190 int gpio_direction_output(unsigned gpio, int value)
192 const struct gpio_bank *bank;
194 if (check_gpio(gpio) < 0)
197 bank = get_gpio_bank(gpio);
198 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
199 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
205 * Request a gpio before using it.
207 * NOTE: Argument 'label' is unused.
209 int gpio_request(unsigned gpio, const char *label)
211 if (check_gpio(gpio) < 0)
218 * Reset and free the gpio after using it.
220 int gpio_free(unsigned gpio)
225 #else /* new driver model interface CONFIG_DM_GPIO */
227 /* set GPIO pin 'gpio' as an input */
228 static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
230 struct gpio_bank *bank = dev_get_priv(dev);
232 /* Configure GPIO direction as input. */
233 _set_gpio_direction(bank, offset, 1);
238 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
239 static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
242 struct gpio_bank *bank = dev_get_priv(dev);
244 _set_gpio_dataout(bank, offset, value);
245 _set_gpio_direction(bank, offset, 0);
250 /* read GPIO IN value of pin 'gpio' */
251 static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
253 struct gpio_bank *bank = dev_get_priv(dev);
255 return _get_gpio_value(bank, offset);
258 /* write GPIO OUT value to pin 'gpio' */
259 static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
262 struct gpio_bank *bank = dev_get_priv(dev);
264 _set_gpio_dataout(bank, offset, value);
269 static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
271 struct gpio_bank *bank = dev_get_priv(dev);
273 /* GPIOF_FUNC is not implemented yet */
274 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
280 static int omap_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
281 struct fdtdec_phandle_args *args)
283 desc->offset = args->args[0];
284 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
289 static const struct dm_gpio_ops gpio_omap_ops = {
290 .direction_input = omap_gpio_direction_input,
291 .direction_output = omap_gpio_direction_output,
292 .get_value = omap_gpio_get_value,
293 .set_value = omap_gpio_set_value,
294 .get_function = omap_gpio_get_function,
295 .xlate = omap_gpio_xlate,
298 static int omap_gpio_probe(struct udevice *dev)
300 struct gpio_bank *bank = dev_get_priv(dev);
301 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
302 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
304 uc_priv->bank_name = plat->port_name;
305 uc_priv->gpio_count = GPIO_PER_BANK;
306 bank->base = (void *)plat->base;
311 static int omap_gpio_bind(struct udevice *dev)
313 struct omap_gpio_platdata *plat = dev->platdata;
314 fdt_addr_t base_addr;
319 base_addr = dev_get_addr(dev);
320 if (base_addr == FDT_ADDR_T_NONE)
325 * When every board is converted to driver model and DT is
326 * supported, this can be done by auto-alloc feature, but
327 * not using calloc to alloc memory for platdata.
329 plat = calloc(1, sizeof(*plat));
333 plat->base = base_addr;
334 plat->port_name = fdt_get_name(gd->fdt_blob, dev->of_offset, NULL);
335 dev->platdata = plat;
340 static const struct udevice_id omap_gpio_ids[] = {
341 { .compatible = "ti,omap3-gpio" },
342 { .compatible = "ti,omap4-gpio" },
343 { .compatible = "ti,am4372-gpio" },
347 U_BOOT_DRIVER(gpio_omap) = {
350 .ops = &gpio_omap_ops,
351 .of_match = omap_gpio_ids,
352 .bind = omap_gpio_bind,
353 .probe = omap_gpio_probe,
354 .priv_auto_alloc_size = sizeof(struct gpio_bank),
357 #endif /* CONFIG_DM_GPIO */