RX680R Linux GPIO Programming Guide
=
### 1. GPIO Header of [RX680R](https://www.bcmcom.com/bcm_product_RX680R.html)
|Function |Pin |Pin|Function |
| :---: | :---: | :---: |:---: |
|AP_GPIO1 |1 | 2 | AP_GPIO5|
|AP_GPIO2 |3 | 4 | AP_GPIO6 |
|AP_GPIO3 |5 | 6 | AP_GPIO7 |
|AP_GPIO4 |7 | 8 | AP_GPIO8 |
|SMB_CLK |9 | 10 | SMB_DATA |
|GND |11 | 12 | +3V_DUAL |
### 2. PCH GPIO and Linux Pinctrl mappings
Debian 12.5 kernel 6.1.76, [drivers/pinctrl/intel/pinctrl-alderlake.c](https://elixir.bootlin.com/linux/v6.1.76/source/drivers/pinctrl/intel/pinctrl-alderlake.c)
|Pin |Function |PCH|Pin# |GPIO#|
| :---: | :---: | :---: |:---: |:---: |
|1 |AP_GPIO1 | GPP_K9/CORE_VID_1 | 240 | 393 |
|3 |AP_GPIO2 | GPP_K2/GSXDIN | 233 | 386 |
|5 |AP_GPIO3 | GPP_K1/GSXSLOAD | 232 | 386 |
|7 |AP_GPIO4 | GPP_K4/GSXCLK | 235 | 388 |
|2 |AP_GPIO5 | GPP_K5/ADR_COMPLETE | 236 | 389 |
|4 |AP_GPIO6 | GPP_I20/GSPI1_CLK | 20 | 20 |
|6 |AP_GPIO7 | GPP_I21/GSPI1_MISO | 21 | 21 |
|8 |AP_GPIO8 | GPP_I19/GSPI1_CS0B | 19 | 19 |
#### Check driver status:
```bash=
lsmod | grep pin
pinctrl_alderlake 36864 0
```
To find the GPIO number for "CORE_VID_1" based on the provided information from your Intel Alderlake platform configuration, let's follow these steps:
### Locate the Pin in pinctrl_pin_desc:
#### Pin Description for "CORE_VID_1":
```
PINCTRL_PIN(240, "CORE_VID_1"),
```
This line tells us that "CORE_VID_1" is identified as pin number 240.
### Identify Pad Group and Community:
#### Locate the Pad Group for Pin 240:
From the intel_padgroup structures provided, we find that pin 240 falls within the following pad group in adls_community4_gpps:
```
ADL_GPP(2, 231, 245, 384), // GPP_K
```
This group starts at pin 231 and ends at pin 245 with a GPIO base of 384.
### Calculate the GPIO Number:
#### Formula to Determine GPIO Number:
Given the starting GPIO base and the range, you can calculate the GPIO number as follows:
* GPIO number = GPIO base + (Pin number - First pin number in group)
* Calculation:
* GPIO number for "CORE_VID_1" = 384 + (240 - 231)
* GPIO number for "CORE_VID_1" = 384 + 9
* GPIO number for "CORE_VID_1" = 393
### 3. Access GPIO using libgpiod
`libgpiod` is a C library and set of tools for interacting with the GPIO character device introduced in Linux kernels 4.8+. It replaces the older method of directly accessing GPIOs via sysfs and provides a more robust interface for GPIO manipulation.
#### Installing libgpiod
On Debian or Ubuntu:
```bash=
sudo apt update
sudo apt install gpiod libgpiod2 libgpiod-dev
```
#### Using libgiod Tools
Listing available GPIO chips:
```bash=
gpiodetect
gpiochip0 [INTC1056:00] (473 lines)
```
#### Check the Current State
```bash=
gpioget gpiochip0 393
1
```
#### Set the GPIO Direction and Value
```bash=
To set it high:
gpioset gpiochip0 393=1
To set it low:
gpioset gpiochip0 393=0
```
### Using libgpiod in a C Program
```clike=
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#define GPIO_CHIP_NAME "gpiochip0"
#define GPIO_LINE_NUMBER 393
int main(void) {
struct gpiod_chip *chip;
struct gpiod_line *line;
int i, ret;
chip = gpiod_chip_open_by_name(GPIO_CHIP_NAME);
if (!chip) {
perror("Open chip failed");
return -1;
}
line = gpiod_chip_get_line(chip, GPIO_LINE_NUMBER);
if (!line) {
perror("Get line failed");
gpiod_chip_close(chip);
return -1;
}
ret = gpiod_line_request_output(line, "example_program", 0);
if (ret < 0) {
perror("Request line as output failed");
gpiod_line_release(line);
gpiod_chip_close(chip);
return -1;
}
// Toggle the GPIO line 10 times
for (i = 0; i < 10; i++) {
gpiod_line_set_value(line, 1);
sleep(1);
gpiod_line_set_value(line, 0);
sleep(1);
}
gpiod_line_release(line);
gpiod_chip_close(chip);
return 0;
}
```