1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Coldfire generic GPIO support
5 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
8 #ifndef coldfire_gpio_h
9 #define coldfire_gpio_h
12 #include <asm/coldfire.h>
13 #include <asm/mcfsim.h>
14 #include <asm/mcfgpio.h>
16 * The Generic GPIO functions
18 * If the gpio is a compile time constant and is one of the Coldfire gpios,
19 * use the inline version, otherwise dispatch thru gpiolib.
22 static inline int gpio_get_value(unsigned gpio)
24 if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
25 return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
27 return __gpio_get_value(gpio);
30 static inline void gpio_set_value(unsigned gpio, int value)
32 if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
33 if (gpio < MCFGPIO_SCR_START) {
35 MCFGPIO_PORTTYPE data;
37 local_irq_save(flags);
38 data = mcfgpio_read(__mcfgpio_podr(gpio));
40 data |= mcfgpio_bit(gpio);
42 data &= ~mcfgpio_bit(gpio);
43 mcfgpio_write(data, __mcfgpio_podr(gpio));
44 local_irq_restore(flags);
47 mcfgpio_write(mcfgpio_bit(gpio),
48 MCFGPIO_SETR_PORT(gpio));
50 mcfgpio_write(~mcfgpio_bit(gpio),
51 MCFGPIO_CLRR_PORT(gpio));
54 __gpio_set_value(gpio, value);
57 static inline int gpio_to_irq(unsigned gpio)
59 #if defined(MCFGPIO_IRQ_MIN)
60 if ((gpio >= MCFGPIO_IRQ_MIN) && (gpio < MCFGPIO_IRQ_MAX))
62 if (gpio < MCFGPIO_IRQ_MAX)
64 return gpio + MCFGPIO_IRQ_VECBASE;
66 return __gpio_to_irq(gpio);
69 static inline int gpio_cansleep(unsigned gpio)
71 return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
74 #ifndef CONFIG_GPIOLIB
75 static inline int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
79 err = gpio_request(gpio, label);
83 if (flags & GPIOF_DIR_IN)
84 err = gpio_direction_input(gpio);
86 err = gpio_direction_output(gpio,
87 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
94 #endif /* !CONFIG_GPIOLIB */