blob: 9fe854be26bf64cebbc71f61ad34e1181669cf58 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Vadim Bendebury9c9c3362014-07-23 09:40:02 -07002
Julius Wernereaa9c452014-09-24 15:40:49 -07003#ifndef __SRC_INCLUDE_GPIO_H__
4#define __SRC_INCLUDE_GPIO_H__
Vadim Bendebury9c9c3362014-07-23 09:40:02 -07005
Elyes Haouas4faa72f2022-12-09 07:11:02 +01006#include <soc/gpio.h> /* IWYU pragma: export */
Felix Heldacf96df2022-12-08 20:42:02 +01007
8#ifndef __ASSEMBLER__ /* __ASSEMBLER__ also covers __ACPI__ case */
9
Julius Wernereaa9c452014-09-24 15:40:49 -070010#include <types.h>
11
12/* <soc/gpio.h> must typedef a gpio_t that fits in 32 bits. */
13_Static_assert(sizeof(gpio_t) <= sizeof(u32), "gpio_t doesn't fit in lb_gpio");
14
15/* The following functions must be implemented by SoC/board code. */
16int gpio_get(gpio_t gpio);
Cliff Huang546e0932023-01-19 21:38:56 -080017int gpio_tx_get(gpio_t gpio);
Julius Wernereaa9c452014-09-24 15:40:49 -070018void gpio_set(gpio_t gpio, int value);
19void gpio_input_pulldown(gpio_t gpio);
20void gpio_input_pullup(gpio_t gpio);
21void gpio_input(gpio_t gpio);
22void gpio_output(gpio_t gpio, int value);
Julius Werner0a0340e2018-08-02 11:45:07 -070023uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070024
25/*
Duncan Laurie3a39f442016-05-09 16:09:25 -070026 * This function may be implemented by SoC/board code to provide
27 * a mapping from a GPIO pin to controller by returning the ACPI
28 * path for the controller that owns this GPIO.
29 *
30 * If not implemented the default handler will return NULL.
31 */
32const char *gpio_acpi_path(gpio_t gpio);
33
34/*
Duncan Laurie5b6c28c2016-06-29 10:47:22 -070035 * This function may be implemented by SoC/board code to provide
36 * a mapping from the internal representation of a GPIO to the 16bit
37 * value used in an ACPI GPIO pin table entry.
38 *
39 * If not implemented by the SOC the default handler will return 0
40 * because the underlying type of gpio_t is unknown.
41 */
42uint16_t gpio_acpi_pin(gpio_t gpio);
43
44/*
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070045 * Read the value presented by the set of GPIOs, when each pin is interpreted
David Hendricks21db62e2014-11-06 15:05:35 -080046 * as a base-2 digit (LOW = 0, HIGH = 1).
47 *
48 * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1].
49 * num_gpio: number of pins to read.
Aaron Durbinf41ac222016-07-06 22:37:10 -050050 *
51 * There are also pulldown and pullup variants which default each gpio to
52 * be configured with an internal pulldown and pullup, respectively.
David Hendricks21db62e2014-11-06 15:05:35 -080053 */
Julius Werner0a0340e2018-08-02 11:45:07 -070054uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio);
55uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio);
56uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio);
David Hendricks21db62e2014-11-06 15:05:35 -080057
58/*
59 * Read the value presented by the set of GPIOs, when each pin is interpreted
Julius Werner886d29b2014-09-24 15:40:49 -070060 * as a base-3 digit (LOW = 0, HIGH = 1, Z/floating = 2).
David Hendricks3fc63682014-11-06 15:09:27 -080061 * Example: X1 = Z, X2 = 1 -> gpio_base3_value({GPIO(X1), GPIO(X2)}) = 5
Julius Werner886d29b2014-09-24 15:40:49 -070062 * BASE3() from <base3.h> can generate numbers to compare the result to.
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070063 *
Julius Werner886d29b2014-09-24 15:40:49 -070064 * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1].
Daisuke Nojiri573e2112014-08-08 15:38:52 -070065 * num_gpio: number of pins to read.
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070066 */
Julius Werner0a0340e2018-08-02 11:45:07 -070067static inline uint32_t gpio_base3_value(const gpio_t gpio[], int num_gpio)
Julius Werner2b239482016-03-16 17:11:55 -070068{
69 return _gpio_base3_value(gpio, num_gpio, 0);
70}
71
72/*
73 * Read the value presented by the set of GPIOs, when each pin is interpreted
74 * as a base-3 digit (LOW = 0, HIGH = 1, Z/floating = 2) in a non-standard
75 * ternary number system where the first 2^n natural numbers are represented
76 * as they would be in a binary system (without any Z digits), and the following
77 * 3^n-2^n numbers use the remaining ternary representations in the normal
78 * ternary system order (skipping the values that were already used up).
79 * This is useful for boards which initially used a binary board ID and later
80 * decided to switch to tri-state after some revisions have already been built.
81 * Example: For num_gpio = 2 we get the following representation:
82 *
83 * Number X1 X0
84 * 0 0 0
85 * 1 0 1
86 * 2 1 0
87 * 3 1 1 // Start counting ternaries back at 0 after this
88 * 4 0 2 // Skipping 00 and 01 which are already used up
89 * 5 1 2 // Skipping 10 and 11 which are already used up
90 * 6 2 0
91 * 7 2 1
92 * 8 2 2
93 *
94 * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1].
95 * num_gpio: number of pins to read.
96 */
Julius Werner0a0340e2018-08-02 11:45:07 -070097static inline uint32_t gpio_binary_first_base3_value(const gpio_t gpio[],
98 int num_gpio)
Julius Werner2b239482016-03-16 17:11:55 -070099{
100 return _gpio_base3_value(gpio, num_gpio, 1);
101}
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700102
Felix Heldacf96df2022-12-08 20:42:02 +0100103#endif /* !__ASSEMBLER__ */
104
Julius Wernereaa9c452014-09-24 15:40:49 -0700105#endif /* __SRC_INCLUDE_GPIO_H__ */