blob: fb01a17fc17e3c48f2d5bb56b29e7f7cd71e2eda [file] [log] [blame]
Haavard Skinnemoenfd077f22008-09-01 14:00:07 +02001/*
2 * Copyright (C) 2008 Atmel Corporation
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoenfd077f22008-09-01 14:00:07 +02005 */
6#ifndef __AVR32_PORTMUX_GPIO_H__
7#define __AVR32_PORTMUX_GPIO_H__
8
9#include <asm/io.h>
10
Gunnar Rangoye14f8dd2009-01-23 12:56:29 +010011/* Register layout for this specific device */
12#include <asm/arch/gpio-impl.h>
Haavard Skinnemoenfd077f22008-09-01 14:00:07 +020013
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
22enum 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
42void portmux_select_peripheral(void *port, unsigned long pin_mask,
43 enum portmux_function func, unsigned long flags);
44void portmux_select_gpio(void *port, unsigned long pin_mask,
45 unsigned long flags);
46
47/* Internal helper functions */
48
49static inline void *gpio_pin_to_port(unsigned int pin)
50{
51 return (void *)GPIO_BASE + (pin >> 5) * 0x200;
52}
53
54static 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
63static inline int __gpio_get_input_value(void *port, unsigned int pin)
64{
65 return (gpio_readl(port, PVR) >> pin) & 1;
66}
67
68void gpio_set_output_value(unsigned int pin, int value);
69int 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))
79static 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))
89static 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__ */