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
33 #if CONFIG_IS_ENABLED(DM_GPIO)
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 int gpio_is_valid(int gpio)
46 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
49 static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
52 void *reg = bank->base;
66 * Get the direction of the GPIO by reading the GPIO_OE register
67 * corresponding to the specified bank.
69 static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
71 void *reg = bank->base;
79 return OMAP_GPIO_DIR_IN;
81 return OMAP_GPIO_DIR_OUT;
84 static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
87 void *reg = bank->base;
91 reg += OMAP_GPIO_SETDATAOUT;
93 reg += OMAP_GPIO_CLEARDATAOUT;
99 static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
101 void *reg = bank->base;
104 input = _get_gpio_direction(bank, gpio);
106 case OMAP_GPIO_DIR_IN:
107 reg += OMAP_GPIO_DATAIN;
109 case OMAP_GPIO_DIR_OUT:
110 reg += OMAP_GPIO_DATAOUT;
116 return (__raw_readl(reg) & (1 << gpio)) != 0;
119 #if !CONFIG_IS_ENABLED(DM_GPIO)
120 static inline int get_gpio_index(int gpio)
125 static inline const struct gpio_bank *get_gpio_bank(int gpio)
127 return &omap_gpio_bank[gpio >> 5];
130 static int check_gpio(int gpio)
132 if (!gpio_is_valid(gpio)) {
133 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
140 * Set value of the specified gpio
142 int gpio_set_value(unsigned gpio, int value)
144 const struct gpio_bank *bank;
146 if (check_gpio(gpio) < 0)
148 bank = get_gpio_bank(gpio);
149 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
155 * Get value of the specified gpio
157 int gpio_get_value(unsigned gpio)
159 const struct gpio_bank *bank;
161 if (check_gpio(gpio) < 0)
163 bank = get_gpio_bank(gpio);
165 return _get_gpio_value(bank, get_gpio_index(gpio));
169 * Set gpio direction as input
171 int gpio_direction_input(unsigned gpio)
173 const struct gpio_bank *bank;
175 if (check_gpio(gpio) < 0)
178 bank = get_gpio_bank(gpio);
179 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
185 * Set gpio direction as output
187 int gpio_direction_output(unsigned gpio, int value)
189 const struct gpio_bank *bank;
191 if (check_gpio(gpio) < 0)
194 bank = get_gpio_bank(gpio);
195 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
196 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
202 * Request a gpio before using it.
204 * NOTE: Argument 'label' is unused.
206 int gpio_request(unsigned gpio, const char *label)
208 if (check_gpio(gpio) < 0)
215 * Reset and free the gpio after using it.
217 int gpio_free(unsigned gpio)
222 #else /* new driver model interface CONFIG_DM_GPIO */
224 /* set GPIO pin 'gpio' as an input */
225 static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
227 struct gpio_bank *bank = dev_get_priv(dev);
229 /* Configure GPIO direction as input. */
230 _set_gpio_direction(bank, offset, 1);
235 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
236 static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
239 struct gpio_bank *bank = dev_get_priv(dev);
241 _set_gpio_dataout(bank, offset, value);
242 _set_gpio_direction(bank, offset, 0);
247 /* read GPIO IN value of pin 'gpio' */
248 static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
250 struct gpio_bank *bank = dev_get_priv(dev);
252 return _get_gpio_value(bank, offset);
255 /* write GPIO OUT value to pin 'gpio' */
256 static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
259 struct gpio_bank *bank = dev_get_priv(dev);
261 _set_gpio_dataout(bank, offset, value);
266 static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
268 struct gpio_bank *bank = dev_get_priv(dev);
270 /* GPIOF_FUNC is not implemented yet */
271 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
277 static const struct dm_gpio_ops gpio_omap_ops = {
278 .direction_input = omap_gpio_direction_input,
279 .direction_output = omap_gpio_direction_output,
280 .get_value = omap_gpio_get_value,
281 .set_value = omap_gpio_set_value,
282 .get_function = omap_gpio_get_function,
285 static int omap_gpio_probe(struct udevice *dev)
287 struct gpio_bank *bank = dev_get_priv(dev);
288 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
289 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
292 sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
296 uc_priv->bank_name = str;
297 uc_priv->gpio_count = GPIO_PER_BANK;
298 bank->base = (void *)plat->base;
302 #if !CONFIG_IS_ENABLED(OF_CONTROL)
303 static int omap_gpio_bind(struct udevice *dev)
305 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
306 fdt_addr_t base_addr;
311 base_addr = dev_read_addr(dev);
312 if (base_addr == FDT_ADDR_T_NONE)
317 * When every board is converted to driver model and DT is
318 * supported, this can be done by auto-alloc feature, but
319 * not using calloc to alloc memory for platdata.
321 * For example am33xx_gpio uses platform data rather than device tree.
323 * NOTE: DO NOT COPY this code if you are using device tree.
325 plat = calloc(1, sizeof(*plat));
329 plat->base = base_addr;
330 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
331 dev->platdata = plat;
337 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
338 static const struct udevice_id omap_gpio_ids[] = {
339 { .compatible = "ti,omap3-gpio" },
340 { .compatible = "ti,omap4-gpio" },
341 { .compatible = "ti,am4372-gpio" },
345 static int omap_gpio_ofdata_to_platdata(struct udevice *dev)
347 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
350 addr = dev_read_addr(dev);
351 if (addr == FDT_ADDR_T_NONE)
359 U_BOOT_DRIVER(gpio_omap) = {
362 #if CONFIG_IS_ENABLED(OF_CONTROL)
363 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
364 .of_match = omap_gpio_ids,
365 .ofdata_to_platdata = of_match_ptr(omap_gpio_ofdata_to_platdata),
366 .platdata_auto_alloc_size = sizeof(struct omap_gpio_platdata),
369 .bind = omap_gpio_bind,
371 .ops = &gpio_omap_ops,
372 .probe = omap_gpio_probe,
373 .priv_auto_alloc_size = sizeof(struct gpio_bank),
374 #if !CONFIG_IS_ENABLED(OF_CONTROL)
375 .flags = DM_FLAG_PRE_RELOC,
379 #endif /* !DM_GPIO */