Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 2 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 3 | #ifndef __SRC_INCLUDE_GPIO_H__ |
| 4 | #define __SRC_INCLUDE_GPIO_H__ |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 5 | |
Elyes Haouas | 4faa72f | 2022-12-09 07:11:02 +0100 | [diff] [blame] | 6 | #include <soc/gpio.h> /* IWYU pragma: export */ |
Felix Held | acf96df | 2022-12-08 20:42:02 +0100 | [diff] [blame] | 7 | |
| 8 | #ifndef __ASSEMBLER__ /* __ASSEMBLER__ also covers __ACPI__ case */ |
| 9 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 10 | #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. */ |
| 16 | int gpio_get(gpio_t gpio); |
| 17 | void gpio_set(gpio_t gpio, int value); |
| 18 | void gpio_input_pulldown(gpio_t gpio); |
| 19 | void gpio_input_pullup(gpio_t gpio); |
| 20 | void gpio_input(gpio_t gpio); |
| 21 | void gpio_output(gpio_t gpio, int value); |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 22 | uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
Duncan Laurie | 3a39f44 | 2016-05-09 16:09:25 -0700 | [diff] [blame] | 25 | * This function may be implemented by SoC/board code to provide |
| 26 | * a mapping from a GPIO pin to controller by returning the ACPI |
| 27 | * path for the controller that owns this GPIO. |
| 28 | * |
| 29 | * If not implemented the default handler will return NULL. |
| 30 | */ |
| 31 | const char *gpio_acpi_path(gpio_t gpio); |
| 32 | |
| 33 | /* |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 34 | * This function may be implemented by SoC/board code to provide |
| 35 | * a mapping from the internal representation of a GPIO to the 16bit |
| 36 | * value used in an ACPI GPIO pin table entry. |
| 37 | * |
| 38 | * If not implemented by the SOC the default handler will return 0 |
| 39 | * because the underlying type of gpio_t is unknown. |
| 40 | */ |
| 41 | uint16_t gpio_acpi_pin(gpio_t gpio); |
| 42 | |
| 43 | /* |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 44 | * Read the value presented by the set of GPIOs, when each pin is interpreted |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 45 | * as a base-2 digit (LOW = 0, HIGH = 1). |
| 46 | * |
| 47 | * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1]. |
| 48 | * num_gpio: number of pins to read. |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 49 | * |
| 50 | * There are also pulldown and pullup variants which default each gpio to |
| 51 | * be configured with an internal pulldown and pullup, respectively. |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 52 | */ |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 53 | uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio); |
| 54 | uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio); |
| 55 | uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio); |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Read the value presented by the set of GPIOs, when each pin is interpreted |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 59 | * as a base-3 digit (LOW = 0, HIGH = 1, Z/floating = 2). |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 60 | * Example: X1 = Z, X2 = 1 -> gpio_base3_value({GPIO(X1), GPIO(X2)}) = 5 |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 61 | * BASE3() from <base3.h> can generate numbers to compare the result to. |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 62 | * |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 63 | * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1]. |
Daisuke Nojiri | 573e211 | 2014-08-08 15:38:52 -0700 | [diff] [blame] | 64 | * num_gpio: number of pins to read. |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 65 | */ |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 66 | static inline uint32_t gpio_base3_value(const gpio_t gpio[], int num_gpio) |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 67 | { |
| 68 | return _gpio_base3_value(gpio, num_gpio, 0); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Read the value presented by the set of GPIOs, when each pin is interpreted |
| 73 | * as a base-3 digit (LOW = 0, HIGH = 1, Z/floating = 2) in a non-standard |
| 74 | * ternary number system where the first 2^n natural numbers are represented |
| 75 | * as they would be in a binary system (without any Z digits), and the following |
| 76 | * 3^n-2^n numbers use the remaining ternary representations in the normal |
| 77 | * ternary system order (skipping the values that were already used up). |
| 78 | * This is useful for boards which initially used a binary board ID and later |
| 79 | * decided to switch to tri-state after some revisions have already been built. |
| 80 | * Example: For num_gpio = 2 we get the following representation: |
| 81 | * |
| 82 | * Number X1 X0 |
| 83 | * 0 0 0 |
| 84 | * 1 0 1 |
| 85 | * 2 1 0 |
| 86 | * 3 1 1 // Start counting ternaries back at 0 after this |
| 87 | * 4 0 2 // Skipping 00 and 01 which are already used up |
| 88 | * 5 1 2 // Skipping 10 and 11 which are already used up |
| 89 | * 6 2 0 |
| 90 | * 7 2 1 |
| 91 | * 8 2 2 |
| 92 | * |
| 93 | * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1]. |
| 94 | * num_gpio: number of pins to read. |
| 95 | */ |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 96 | static inline uint32_t gpio_binary_first_base3_value(const gpio_t gpio[], |
| 97 | int num_gpio) |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 98 | { |
| 99 | return _gpio_base3_value(gpio, num_gpio, 1); |
| 100 | } |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 101 | |
Felix Held | acf96df | 2022-12-08 20:42:02 +0100 | [diff] [blame] | 102 | #endif /* !__ASSEMBLER__ */ |
| 103 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 104 | #endif /* __SRC_INCLUDE_GPIO_H__ */ |