1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Coldfire generic GPIO support
4  *
5  * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
6 */
7 
8 #ifndef coldfire_gpio_h
9 #define coldfire_gpio_h
10 
11 #include <linux/io.h>
12 #include <asm/coldfire.h>
13 #include <asm/mcfsim.h>
14 #include <asm/mcfgpio.h>
15 /*
16  * The Generic GPIO functions
17  *
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.
20  */
21 
gpio_get_value(unsigned gpio)22 static inline int gpio_get_value(unsigned gpio)
23 {
24 	if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
25 		return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
26 	else
27 		return __gpio_get_value(gpio);
28 }
29 
gpio_set_value(unsigned gpio,int value)30 static inline void gpio_set_value(unsigned gpio, int value)
31 {
32 	if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
33 		if (gpio < MCFGPIO_SCR_START) {
34 			unsigned long flags;
35 			MCFGPIO_PORTTYPE data;
36 
37 			local_irq_save(flags);
38 			data = mcfgpio_read(__mcfgpio_podr(gpio));
39 			if (value)
40 				data |= mcfgpio_bit(gpio);
41 			else
42 				data &= ~mcfgpio_bit(gpio);
43 			mcfgpio_write(data, __mcfgpio_podr(gpio));
44 			local_irq_restore(flags);
45 		} else {
46 			if (value)
47 				mcfgpio_write(mcfgpio_bit(gpio),
48 						MCFGPIO_SETR_PORT(gpio));
49 			else
50 				mcfgpio_write(~mcfgpio_bit(gpio),
51 						MCFGPIO_CLRR_PORT(gpio));
52 		}
53 	} else
54 		__gpio_set_value(gpio, value);
55 }
56 
gpio_to_irq(unsigned gpio)57 static inline int gpio_to_irq(unsigned gpio)
58 {
59 #if defined(MCFGPIO_IRQ_MIN)
60 	if ((gpio >= MCFGPIO_IRQ_MIN) && (gpio < MCFGPIO_IRQ_MAX))
61 #else
62 	if (gpio < MCFGPIO_IRQ_MAX)
63 #endif
64 		return gpio + MCFGPIO_IRQ_VECBASE;
65 	else
66 		return __gpio_to_irq(gpio);
67 }
68 
irq_to_gpio(unsigned irq)69 static inline int irq_to_gpio(unsigned irq)
70 {
71 	return (irq >= MCFGPIO_IRQ_VECBASE &&
72 		irq < (MCFGPIO_IRQ_VECBASE + MCFGPIO_IRQ_MAX)) ?
73 		irq - MCFGPIO_IRQ_VECBASE : -ENXIO;
74 }
75 
gpio_cansleep(unsigned gpio)76 static inline int gpio_cansleep(unsigned gpio)
77 {
78 	return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
79 }
80 
81 #ifndef CONFIG_GPIOLIB
gpio_request_one(unsigned gpio,unsigned long flags,const char * label)82 static inline int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
83 {
84 	int err;
85 
86 	err = gpio_request(gpio, label);
87 	if (err)
88 		return err;
89 
90 	if (flags & GPIOF_DIR_IN)
91 		err = gpio_direction_input(gpio);
92 	else
93 		err = gpio_direction_output(gpio,
94 			(flags & GPIOF_INIT_HIGH) ? 1 : 0);
95 
96 	if (err)
97 		gpio_free(gpio);
98 
99 	return err;
100 }
101 #endif /* !CONFIG_GPIOLIB */
102 #endif
103