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
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
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 #if !CONFIG_IS_ENABLED(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);
293 sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
297 uc_priv->bank_name = str;
298 uc_priv->gpio_count = GPIO_PER_BANK;
299 bank->base = (void *)plat->base;
303 #if !CONFIG_IS_ENABLED(OF_CONTROL)
304 static int omap_gpio_bind(struct udevice *dev)
306 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
307 fdt_addr_t base_addr;
312 base_addr = devfdt_get_addr(dev);
313 if (base_addr == FDT_ADDR_T_NONE)
318 * When every board is converted to driver model and DT is
319 * supported, this can be done by auto-alloc feature, but
320 * not using calloc to alloc memory for platdata.
322 * For example am33xx_gpio uses platform data rather than device tree.
324 * NOTE: DO NOT COPY this code if you are using device tree.
326 plat = calloc(1, sizeof(*plat));
330 plat->base = base_addr;
331 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
332 dev->platdata = plat;
338 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
339 static const struct udevice_id omap_gpio_ids[] = {
340 { .compatible = "ti,omap3-gpio" },
341 { .compatible = "ti,omap4-gpio" },
342 { .compatible = "ti,am4372-gpio" },
346 static int omap_gpio_ofdata_to_platdata(struct udevice *dev)
348 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
351 addr = devfdt_get_addr(dev);
352 if (addr == FDT_ADDR_T_NONE)
360 U_BOOT_DRIVER(gpio_omap) = {
363 #if CONFIG_IS_ENABLED(OF_CONTROL)
364 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
365 .of_match = omap_gpio_ids,
366 .ofdata_to_platdata = of_match_ptr(omap_gpio_ofdata_to_platdata),
367 .platdata_auto_alloc_size = sizeof(struct omap_gpio_platdata),
370 .bind = omap_gpio_bind,
372 .ops = &gpio_omap_ops,
373 .probe = omap_gpio_probe,
374 .priv_auto_alloc_size = sizeof(struct gpio_bank),
375 #if !CONFIG_IS_ENABLED(OF_CONTROL)
376 .flags = DM_FLAG_PRE_RELOC,
380 #endif /* !DM_GPIO */