1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2009 Wind River Systems, Inc.
4 * Tom Rix <Tom.Rix@windriver.com>
6 * This work is derived from the linux 2.6.27 kernel source
7 * To fetch, use the kernel repository
8 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
11 * Below is the original's header including its copyright
13 * linux/arch/arm/plat-omap/gpio.c
15 * Support functions for OMAP GPIO
17 * Copyright (C) 2003-2005 Nokia Corporation
18 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
25 #include <linux/errno.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #define OMAP_GPIO_DIR_OUT 0
31 #define OMAP_GPIO_DIR_IN 1
35 #define GPIO_PER_BANK 32
38 /* TODO(sjg@chromium.org): Can we use a struct here? */
39 void *base; /* address of registers in physical memory */
44 static inline int get_gpio_index(int gpio)
49 int gpio_is_valid(int gpio)
51 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
54 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
57 void *reg = bank->base;
71 * Get the direction of the GPIO by reading the GPIO_OE register
72 * corresponding to the specified bank.
74 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
76 void *reg = bank->base;
84 return OMAP_GPIO_DIR_IN;
86 return OMAP_GPIO_DIR_OUT;
89 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
92 void *reg = bank->base;
96 reg += OMAP_GPIO_SETDATAOUT;
98 reg += OMAP_GPIO_CLEARDATAOUT;
101 __raw_writel(l, reg);
104 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
106 void *reg = bank->base;
109 input = _get_gpio_direction(bank, gpio);
111 case OMAP_GPIO_DIR_IN:
112 reg += OMAP_GPIO_DATAIN;
114 case OMAP_GPIO_DIR_OUT:
115 reg += OMAP_GPIO_DATAOUT;
121 return (__raw_readl(reg) & (1 << gpio)) != 0;
124 #ifndef CONFIG_DM_GPIO
126 static inline const struct gpio_bank *get_gpio_bank(int gpio)
128 return &omap_gpio_bank[gpio >> 5];
131 static int check_gpio(int gpio)
133 if (!gpio_is_valid(gpio)) {
134 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
141 * Set value of the specified gpio
143 int gpio_set_value(unsigned gpio, int value)
145 const struct gpio_bank *bank;
147 if (check_gpio(gpio) < 0)
149 bank = get_gpio_bank(gpio);
150 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
156 * Get value of the specified gpio
158 int gpio_get_value(unsigned gpio)
160 const struct gpio_bank *bank;
162 if (check_gpio(gpio) < 0)
164 bank = get_gpio_bank(gpio);
166 return _get_gpio_value(bank, get_gpio_index(gpio));
170 * Set gpio direction as input
172 int gpio_direction_input(unsigned gpio)
174 const struct gpio_bank *bank;
176 if (check_gpio(gpio) < 0)
179 bank = get_gpio_bank(gpio);
180 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
186 * Set gpio direction as output
188 int gpio_direction_output(unsigned gpio, int value)
190 const struct gpio_bank *bank;
192 if (check_gpio(gpio) < 0)
195 bank = get_gpio_bank(gpio);
196 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
197 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
203 * Request a gpio before using it.
205 * NOTE: Argument 'label' is unused.
207 int gpio_request(unsigned gpio, const char *label)
209 if (check_gpio(gpio) < 0)
216 * Reset and free the gpio after using it.
218 int gpio_free(unsigned gpio)
223 #else /* new driver model interface CONFIG_DM_GPIO */
225 /* set GPIO pin 'gpio' as an input */
226 static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
228 struct gpio_bank *bank = dev_get_priv(dev);
230 /* Configure GPIO direction as input. */
231 _set_gpio_direction(bank, offset, 1);
236 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
237 static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
240 struct gpio_bank *bank = dev_get_priv(dev);
242 _set_gpio_dataout(bank, offset, value);
243 _set_gpio_direction(bank, offset, 0);
248 /* read GPIO IN value of pin 'gpio' */
249 static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
251 struct gpio_bank *bank = dev_get_priv(dev);
253 return _get_gpio_value(bank, offset);
256 /* write GPIO OUT value to pin 'gpio' */
257 static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
260 struct gpio_bank *bank = dev_get_priv(dev);
262 _set_gpio_dataout(bank, offset, value);
267 static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
269 struct gpio_bank *bank = dev_get_priv(dev);
271 /* GPIOF_FUNC is not implemented yet */
272 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
278 static const struct dm_gpio_ops gpio_omap_ops = {
279 .direction_input = omap_gpio_direction_input,
280 .direction_output = omap_gpio_direction_output,
281 .get_value = omap_gpio_get_value,
282 .set_value = omap_gpio_set_value,
283 .get_function = omap_gpio_get_function,
286 static int omap_gpio_probe(struct udevice *dev)
288 struct gpio_bank *bank = dev_get_priv(dev);
289 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
290 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
292 uc_priv->bank_name = plat->port_name;
293 uc_priv->gpio_count = GPIO_PER_BANK;
294 bank->base = (void *)plat->base;
299 static int omap_gpio_bind(struct udevice *dev)
301 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
302 fdt_addr_t base_addr;
307 base_addr = devfdt_get_addr(dev);
308 if (base_addr == FDT_ADDR_T_NONE)
313 * When every board is converted to driver model and DT is
314 * supported, this can be done by auto-alloc feature, but
315 * not using calloc to alloc memory for platdata.
317 * For example am33xx_gpio uses platform data rather than device tree.
319 * NOTE: DO NOT COPY this code if you are using device tree.
321 plat = calloc(1, sizeof(*plat));
325 plat->base = base_addr;
326 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
327 dev->platdata = plat;
332 static const struct udevice_id omap_gpio_ids[] = {
333 { .compatible = "ti,omap3-gpio" },
334 { .compatible = "ti,omap4-gpio" },
335 { .compatible = "ti,am4372-gpio" },
339 U_BOOT_DRIVER(gpio_omap) = {
342 .ops = &gpio_omap_ops,
343 .of_match = omap_gpio_ids,
344 .bind = omap_gpio_bind,
345 .probe = omap_gpio_probe,
346 .priv_auto_alloc_size = sizeof(struct gpio_bank),
347 .flags = DM_FLAG_PRE_RELOC,
350 #endif /* CONFIG_DM_GPIO */