Haavard Skinnemoen | baf449f | 2008-09-01 14:00:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Atmel Corporation |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | #ifndef __AVR32_PORTMUX_GPIO_H__ |
| 23 | #define __AVR32_PORTMUX_GPIO_H__ |
| 24 | |
| 25 | #include <asm/io.h> |
| 26 | |
Gunnar Rangoy | f5f652f | 2009-01-23 12:56:29 +0100 | [diff] [blame^] | 27 | /* Register layout for this specific device */ |
| 28 | #include <asm/arch/gpio-impl.h> |
Haavard Skinnemoen | baf449f | 2008-09-01 14:00:07 +0200 | [diff] [blame] | 29 | |
| 30 | /* Register access macros */ |
| 31 | #define gpio_readl(port, reg) \ |
| 32 | __raw_readl(&((struct gpio_regs *)port)->reg) |
| 33 | #define gpio_writel(gpio, reg, value) \ |
| 34 | __raw_writel(value, &((struct gpio_regs *)port)->reg) |
| 35 | |
| 36 | /* Portmux API starts here. See doc/README.AVR32-port-muxing */ |
| 37 | |
| 38 | enum portmux_function { |
| 39 | PORTMUX_FUNC_A, |
| 40 | PORTMUX_FUNC_B, |
| 41 | PORTMUX_FUNC_C, |
| 42 | PORTMUX_FUNC_D, |
| 43 | }; |
| 44 | |
| 45 | #define PORTMUX_DIR_INPUT (0 << 0) |
| 46 | #define PORTMUX_DIR_OUTPUT (1 << 0) |
| 47 | #define PORTMUX_INIT_LOW (0 << 1) |
| 48 | #define PORTMUX_INIT_HIGH (1 << 1) |
| 49 | #define PORTMUX_PULL_UP (1 << 2) |
| 50 | #define PORTMUX_PULL_DOWN (2 << 2) |
| 51 | #define PORTMUX_BUSKEEPER (3 << 2) |
| 52 | #define PORTMUX_DRIVE_MIN (0 << 4) |
| 53 | #define PORTMUX_DRIVE_LOW (1 << 4) |
| 54 | #define PORTMUX_DRIVE_HIGH (2 << 4) |
| 55 | #define PORTMUX_DRIVE_MAX (3 << 4) |
| 56 | #define PORTMUX_OPEN_DRAIN (1 << 6) |
| 57 | |
| 58 | void portmux_select_peripheral(void *port, unsigned long pin_mask, |
| 59 | enum portmux_function func, unsigned long flags); |
| 60 | void portmux_select_gpio(void *port, unsigned long pin_mask, |
| 61 | unsigned long flags); |
| 62 | |
| 63 | /* Internal helper functions */ |
| 64 | |
| 65 | static inline void *gpio_pin_to_port(unsigned int pin) |
| 66 | { |
| 67 | return (void *)GPIO_BASE + (pin >> 5) * 0x200; |
| 68 | } |
| 69 | |
| 70 | static inline void __gpio_set_output_value(void *port, unsigned int pin, |
| 71 | int value) |
| 72 | { |
| 73 | if (value) |
| 74 | gpio_writel(port, OVRS, 1 << pin); |
| 75 | else |
| 76 | gpio_writel(port, OVRC, 1 << pin); |
| 77 | } |
| 78 | |
| 79 | static inline int __gpio_get_input_value(void *port, unsigned int pin) |
| 80 | { |
| 81 | return (gpio_readl(port, PVR) >> pin) & 1; |
| 82 | } |
| 83 | |
| 84 | void gpio_set_output_value(unsigned int pin, int value); |
| 85 | int gpio_get_input_value(unsigned int pin); |
| 86 | |
| 87 | /* GPIO API starts here */ |
| 88 | |
| 89 | /* |
| 90 | * GCC doesn't realize that the constant case is extremely trivial, |
| 91 | * so we need to help it make the right decision by using |
| 92 | * always_inline. |
| 93 | */ |
| 94 | __attribute__((always_inline)) |
| 95 | static inline void gpio_set_value(unsigned int pin, int value) |
| 96 | { |
| 97 | if (__builtin_constant_p(pin)) |
| 98 | __gpio_set_output_value(gpio_pin_to_port(pin), |
| 99 | pin & 0x1f, value); |
| 100 | else |
| 101 | gpio_set_output_value(pin, value); |
| 102 | } |
| 103 | |
| 104 | __attribute__((always_inline)) |
| 105 | static inline int gpio_get_value(unsigned int pin) |
| 106 | { |
| 107 | if (__builtin_constant_p(pin)) |
| 108 | return __gpio_get_input_value(gpio_pin_to_port(pin), |
| 109 | pin & 0x1f); |
| 110 | else |
| 111 | return gpio_get_input_value(pin); |
| 112 | } |
| 113 | |
| 114 | #endif /* __AVR32_PORTMUX_GPIO_H__ */ |