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 
gpio_cansleep(unsigned gpio)69 static inline int gpio_cansleep(unsigned gpio)
70 {
71 	return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
72 }
73 
74 #ifndef CONFIG_GPIOLIB
gpio_request_one(unsigned gpio,unsigned long flags,const char * label)75 static inline int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
76 {
77 	int err;
78 
79 	err = gpio_request(gpio, label);
80 	if (err)
81 		return err;
82 
83 	if (flags & GPIOF_DIR_IN)
84 		err = gpio_direction_input(gpio);
85 	else
86 		err = gpio_direction_output(gpio,
87 			(flags & GPIOF_INIT_HIGH) ? 1 : 0);
88 
89 	if (err)
90 		gpio_free(gpio);
91 
92 	return err;
93 }
94 #endif /* !CONFIG_GPIOLIB */
95 #endif
96