2 * Coldfire generic GPIO support
4 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #ifndef coldfire_gpio_h
17 #define coldfire_gpio_h
20 #include <asm/coldfire.h>
21 #include <asm/mcfsim.h>
22 #include <asm/mcfgpio.h>
24 * The Generic GPIO functions
26 * If the gpio is a compile time constant and is one of the Coldfire gpios,
27 * use the inline version, otherwise dispatch thru gpiolib.
30 static inline int gpio_get_value(unsigned gpio
)
32 if (__builtin_constant_p(gpio
) && gpio
< MCFGPIO_PIN_MAX
)
33 return mcfgpio_read(__mcfgpio_ppdr(gpio
)) & mcfgpio_bit(gpio
);
35 return __gpio_get_value(gpio
);
38 static inline void gpio_set_value(unsigned gpio
, int value
)
40 if (__builtin_constant_p(gpio
) && gpio
< MCFGPIO_PIN_MAX
) {
41 if (gpio
< MCFGPIO_SCR_START
) {
43 MCFGPIO_PORTTYPE data
;
45 local_irq_save(flags
);
46 data
= mcfgpio_read(__mcfgpio_podr(gpio
));
48 data
|= mcfgpio_bit(gpio
);
50 data
&= ~mcfgpio_bit(gpio
);
51 mcfgpio_write(data
, __mcfgpio_podr(gpio
));
52 local_irq_restore(flags
);
55 mcfgpio_write(mcfgpio_bit(gpio
),
56 MCFGPIO_SETR_PORT(gpio
));
58 mcfgpio_write(~mcfgpio_bit(gpio
),
59 MCFGPIO_CLRR_PORT(gpio
));
62 __gpio_set_value(gpio
, value
);
65 static inline int gpio_to_irq(unsigned gpio
)
67 #if defined(MCFGPIO_IRQ_MIN)
68 if ((gpio
>= MCFGPIO_IRQ_MIN
) && (gpio
< MCFGPIO_IRQ_MAX
))
70 if (gpio
< MCFGPIO_IRQ_MAX
)
72 return gpio
+ MCFGPIO_IRQ_VECBASE
;
74 return __gpio_to_irq(gpio
);
77 static inline int irq_to_gpio(unsigned irq
)
79 return (irq
>= MCFGPIO_IRQ_VECBASE
&&
80 irq
< (MCFGPIO_IRQ_VECBASE
+ MCFGPIO_IRQ_MAX
)) ?
81 irq
- MCFGPIO_IRQ_VECBASE
: -ENXIO
;
84 static inline int gpio_cansleep(unsigned gpio
)
86 return gpio
< MCFGPIO_PIN_MAX
? 0 : __gpio_cansleep(gpio
);
89 #ifndef CONFIG_GPIOLIB
90 static inline int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
94 err
= gpio_request(gpio
, label
);
98 if (flags
& GPIOF_DIR_IN
)
99 err
= gpio_direction_input(gpio
);
101 err
= gpio_direction_output(gpio
,
102 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
109 #endif /* !CONFIG_GPIOLIB */