]> git.ipfire.org Git - thirdparty/u-boot.git/blob - arch/avr32/include/asm/arch-common/portmux-gpio.h
Add GPL-2.0+ SPDX-License-Identifier to source files
[thirdparty/u-boot.git] / arch / avr32 / include / asm / arch-common / portmux-gpio.h
1 /*
2 * Copyright (C) 2008 Atmel Corporation
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6 #ifndef __AVR32_PORTMUX_GPIO_H__
7 #define __AVR32_PORTMUX_GPIO_H__
8
9 #include <asm/io.h>
10
11 /* Register layout for this specific device */
12 #include <asm/arch/gpio-impl.h>
13
14 /* Register access macros */
15 #define gpio_readl(port, reg) \
16 __raw_readl(&((struct gpio_regs *)port)->reg)
17 #define gpio_writel(gpio, reg, value) \
18 __raw_writel(value, &((struct gpio_regs *)port)->reg)
19
20 /* Portmux API starts here. See doc/README.AVR32-port-muxing */
21
22 enum portmux_function {
23 PORTMUX_FUNC_A,
24 PORTMUX_FUNC_B,
25 PORTMUX_FUNC_C,
26 PORTMUX_FUNC_D,
27 };
28
29 #define PORTMUX_DIR_INPUT (0 << 0)
30 #define PORTMUX_DIR_OUTPUT (1 << 0)
31 #define PORTMUX_INIT_LOW (0 << 1)
32 #define PORTMUX_INIT_HIGH (1 << 1)
33 #define PORTMUX_PULL_UP (1 << 2)
34 #define PORTMUX_PULL_DOWN (2 << 2)
35 #define PORTMUX_BUSKEEPER (3 << 2)
36 #define PORTMUX_DRIVE_MIN (0 << 4)
37 #define PORTMUX_DRIVE_LOW (1 << 4)
38 #define PORTMUX_DRIVE_HIGH (2 << 4)
39 #define PORTMUX_DRIVE_MAX (3 << 4)
40 #define PORTMUX_OPEN_DRAIN (1 << 6)
41
42 void portmux_select_peripheral(void *port, unsigned long pin_mask,
43 enum portmux_function func, unsigned long flags);
44 void portmux_select_gpio(void *port, unsigned long pin_mask,
45 unsigned long flags);
46
47 /* Internal helper functions */
48
49 static inline void *gpio_pin_to_port(unsigned int pin)
50 {
51 return (void *)GPIO_BASE + (pin >> 5) * 0x200;
52 }
53
54 static inline void __gpio_set_output_value(void *port, unsigned int pin,
55 int value)
56 {
57 if (value)
58 gpio_writel(port, OVRS, 1 << pin);
59 else
60 gpio_writel(port, OVRC, 1 << pin);
61 }
62
63 static inline int __gpio_get_input_value(void *port, unsigned int pin)
64 {
65 return (gpio_readl(port, PVR) >> pin) & 1;
66 }
67
68 void gpio_set_output_value(unsigned int pin, int value);
69 int gpio_get_input_value(unsigned int pin);
70
71 /* GPIO API starts here */
72
73 /*
74 * GCC doesn't realize that the constant case is extremely trivial,
75 * so we need to help it make the right decision by using
76 * always_inline.
77 */
78 __attribute__((always_inline))
79 static inline void gpio_set_value(unsigned int pin, int value)
80 {
81 if (__builtin_constant_p(pin))
82 __gpio_set_output_value(gpio_pin_to_port(pin),
83 pin & 0x1f, value);
84 else
85 gpio_set_output_value(pin, value);
86 }
87
88 __attribute__((always_inline))
89 static inline int gpio_get_value(unsigned int pin)
90 {
91 if (__builtin_constant_p(pin))
92 return __gpio_get_input_value(gpio_pin_to_port(pin),
93 pin & 0x1f);
94 else
95 return gpio_get_input_value(pin);
96 }
97
98 #endif /* __AVR32_PORTMUX_GPIO_H__ */