1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2009 Wind River Systems, Inc.
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
23 #include <asm/global_data.h>
26 #include <dm/device-internal.h>
27 #include <linux/errno.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #define OMAP_GPIO_DIR_OUT 0
33 #define OMAP_GPIO_DIR_IN 1
35 #if CONFIG_IS_ENABLED(DM_GPIO)
37 #define GPIO_PER_BANK 32
41 void *base; /* address of registers in physical memory */
46 int gpio_is_valid(int gpio)
48 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
51 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
54 void *reg = bank->base;
68 * Get the direction of the GPIO by reading the GPIO_OE register
69 * corresponding to the specified bank.
71 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
73 void *reg = bank->base;
81 return OMAP_GPIO_DIR_IN;
83 return OMAP_GPIO_DIR_OUT;
86 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
89 void *reg = bank->base;
93 reg += OMAP_GPIO_SETDATAOUT;
95 reg += OMAP_GPIO_CLEARDATAOUT;
101 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
103 void *reg = bank->base;
106 input = _get_gpio_direction(bank, gpio);
108 case OMAP_GPIO_DIR_IN:
109 reg += OMAP_GPIO_DATAIN;
111 case OMAP_GPIO_DIR_OUT:
112 reg += OMAP_GPIO_DATAOUT;
118 return (__raw_readl(reg) & (1 << gpio)) != 0;
121 #if !CONFIG_IS_ENABLED(DM_GPIO)
122 static inline int get_gpio_index(int gpio)
127 static inline const struct gpio_bank *get_gpio_bank(int gpio)
129 return &omap_gpio_bank[gpio >> 5];
132 static int check_gpio(int gpio)
134 if (!gpio_is_valid(gpio)) {
135 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
142 * Set value of the specified gpio
144 int gpio_set_value(unsigned gpio, int value)
146 const struct gpio_bank *bank;
148 if (check_gpio(gpio) < 0)
150 bank = get_gpio_bank(gpio);
151 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
157 * Get value of the specified gpio
159 int gpio_get_value(unsigned gpio)
161 const struct gpio_bank *bank;
163 if (check_gpio(gpio) < 0)
165 bank = get_gpio_bank(gpio);
167 return _get_gpio_value(bank, get_gpio_index(gpio));
171 * Set gpio direction as input
173 int gpio_direction_input(unsigned gpio)
175 const struct gpio_bank *bank;
177 if (check_gpio(gpio) < 0)
180 bank = get_gpio_bank(gpio);
181 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
187 * Set gpio direction as output
189 int gpio_direction_output(unsigned gpio, int value)
191 const struct gpio_bank *bank;
193 if (check_gpio(gpio) < 0)
196 bank = get_gpio_bank(gpio);
197 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
198 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
204 * Request a gpio before using it.
206 * NOTE: Argument 'label' is unused.
208 int gpio_request(unsigned gpio, const char *label)
210 if (check_gpio(gpio) < 0)
217 * Reset and free the gpio after using it.
219 int gpio_free(unsigned gpio)
224 #else /* new driver model interface CONFIG_DM_GPIO */
226 /* set GPIO pin 'gpio' as an input */
227 static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
229 struct gpio_bank *bank = dev_get_priv(dev);
231 /* Configure GPIO direction as input. */
232 _set_gpio_direction(bank, offset, 1);
237 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
238 static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
241 struct gpio_bank *bank = dev_get_priv(dev);
243 _set_gpio_dataout(bank, offset, value);
244 _set_gpio_direction(bank, offset, 0);
249 /* read GPIO IN value of pin 'gpio' */
250 static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
252 struct gpio_bank *bank = dev_get_priv(dev);
254 return _get_gpio_value(bank, offset);
257 /* write GPIO OUT value to pin 'gpio' */
258 static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
261 struct gpio_bank *bank = dev_get_priv(dev);
263 _set_gpio_dataout(bank, offset, value);
268 static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
270 struct gpio_bank *bank = dev_get_priv(dev);
272 /* GPIOF_FUNC is not implemented yet */
273 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
279 static const struct dm_gpio_ops gpio_omap_ops = {
280 .direction_input = omap_gpio_direction_input,
281 .direction_output = omap_gpio_direction_output,
282 .get_value = omap_gpio_get_value,
283 .set_value = omap_gpio_set_value,
284 .get_function = omap_gpio_get_function,
287 static int omap_gpio_probe(struct udevice *dev)
289 struct gpio_bank *bank = dev_get_priv(dev);
290 struct omap_gpio_plat *plat = dev_get_plat(dev);
291 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
294 sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
298 uc_priv->bank_name = str;
299 uc_priv->gpio_count = GPIO_PER_BANK;
300 bank->base = (void *)plat->base;
304 #if !CONFIG_IS_ENABLED(OF_CONTROL)
305 static int omap_gpio_bind(struct udevice *dev)
307 struct omap_gpio_plat *plat = dev_get_plat(dev);
308 fdt_addr_t base_addr;
313 base_addr = dev_read_addr(dev);
314 if (base_addr == FDT_ADDR_T_NONE)
319 * When every board is converted to driver model and DT is
320 * supported, this can be done by auto-alloc feature, but
321 * not using calloc to alloc memory for plat.
323 * For example am33xx_gpio uses platform data rather than device tree.
325 * NOTE: DO NOT COPY this code if you are using device tree.
327 plat = calloc(1, sizeof(*plat));
331 plat->base = base_addr;
332 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
333 dev_set_plat(dev, plat);
339 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
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 static int omap_gpio_of_to_plat(struct udevice *dev)
349 struct omap_gpio_plat *plat = dev_get_plat(dev);
352 addr = dev_read_addr(dev);
353 if (addr == FDT_ADDR_T_NONE)
361 U_BOOT_DRIVER(gpio_omap) = {
364 #if CONFIG_IS_ENABLED(OF_CONTROL)
365 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
366 .of_match = omap_gpio_ids,
367 .of_to_plat = of_match_ptr(omap_gpio_of_to_plat),
368 .plat_auto = sizeof(struct omap_gpio_plat),
371 .bind = omap_gpio_bind,
373 .ops = &gpio_omap_ops,
374 .probe = omap_gpio_probe,
375 .priv_auto = sizeof(struct gpio_bank),
376 #if !CONFIG_IS_ENABLED(OF_CONTROL)
377 .flags = DM_FLAG_PRE_RELOC,
381 #endif /* !DM_GPIO */