I am trying to configure my Yocto Linux distro to setup two on-board I2C GPIO Expanders and document how to access them programmatically (i.e. from a C user application). The GPIO Expanders are both NXP PCA9557, which is supported by the gpio-pca953x.c kernel driver.
I understand how to compile this driver into the kernel (using CONFIG_GPIO_PCA953X
configuration option) and load it (using modprobe
). I also understand I can instantiate these two I2C devices in the device tree (this is an embedded device, so the GPIO expanders are static and unchanging):
gpio@20 {
compatible = "nxp,pca9557";
reg = <0x1a>;
gpio-controller;
#gpio-cells = <2>;
};
The information I'm lacking is how to use the gpio-pca953x driver with these instantiated devices? Do I need to associate these devices with that particular driver? What APIs do I use to access them in a user application written in C? Do these just act like normal GPIOs once I've configured them correctly?
I'm more than happy to read through any relevant documentation, lengthy or otherwise, on how to do this. I'm just a beginner in Linux device drivers and have no idea where to go from here, despite a great deal of googling.
There are several way to control GPIO from user space, but What are you trying to control? Linux is able to use GPIO as LED, key, interrupt and more.
sysfs is the "old" way of controlling GPIOs. It works and it is still being maintained. Essentially you write nodes in the /sys directory. Writing files is straight forward process in C. You can test writing to file from in Shell first, if you want.
There is a newer way to access the GPIO. You can open the /dev/chip and make some IOCTL calls. Include
#include <linux/gpio.h>
thenGPIO_GET_LINEHANDLE_IOCTL
, to see current statusGPIOHANDLE_GET_LINE_VALUES_IOCTL
then to readGPIOHANDLE_SET_LINE_VALUES_IOCTL
to writeYou could test it by using a Shell program that uses this same interface.
I have never done this, but here is an example tool that does this. Also on kernel newer then 5.9 you could used
#include "gpio-utils.h"
.