We use cookies to provide our visitors with an optimal site experience. View our privacy notice and cookie notice to learn more about how we use cookies and how to manage your settings. By proceeding on our website you consent to the use of cookies.
I use leds{} in the DT to the control the pin properties ‘active_low’ and ‘default_state’.
I now need to change SPARE pin from output to input after boot. My first thought is to use /sys/class/gpio/gpioxx/direction to set the pin to output.
I tried “echo 33 > /sys/class/gpio/export”
but receive
“ash: write error: Device or resource busy”
I understand the error is because the pin belongs to leds{}.
I need to remove the pin from leds{}.
Is there a way to change a gpio pin that is a member of leds{} from output to input by the command line?
After booting and login, #I list the /sys/class/gpio directory
ls /sys/class/gpio
export gpiochip0 gpiochip64 gpiochip32 gpiochip96 unexport #problem! the pin is not exported.
Q. Is there something that can be added to the DT to automatically export then pin and set the direction while the kernel is parsing the DT?
Up to this point I have wanted the pin to be an input instead of an output. In this example I want the pin direction to be an output. I expected that when the pin was exported that direction would be ‘out’ because the pinmux sets the pin PIN_OUTPUT_PULLUP.
#I export the pin and check the direction
echo 33 > /sys/class/gpio/export #re-list the dirctory
ls /sys/class/gpio
export gpio33 gpiochip0 gpiochip64 gpiochip32 gpiochip96 unexport #check the direction of the pin
cat /sys/class/gpio/gpio33/direction
in #problem! the pin direction is ‘in’
Q. Why is the direction ‘in’ when the pinmux set the pin as PIN_OUTPUT_PULLUP?
When using /sys/class/gpio, is there a gpio or other DT property to automatically export the pin and set the direction while the kernel is parsing the DT?
Hi,
When exporting a gpio, ‘echo 33 > /sys/class/gpio/export’, the default direction is input.
When changing direction to output ‘echo out /sys/class/gpio/gpio33/direction’, the default value is 0 (zero). On my custom board, a value of 0 causes my output to be ON. A value of 1 = OFF.
Is there a way to set the default value to ‘1’ when changing direction to ‘out’.
Another option would be to invert gpio pin so that a value of 0=OFF and 1=ON.
Is there a way to invert the state of the gpio pin in the device tree or by command line before exporting the gpio pin?
RCN,
I was looking thru the kernel code. The linux-5.19.3/drivers/gpio/gpiolib-sysfx.c file has a function
static ssize_t direction_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct gpiod_data *data = dev_get_drvdata(dev);
struct gpio_desc *desc = data->desc;
ssize_t status;
mutex_lock(&data->mutex);
if (sysfs_streq(buf, "high"))
status = gpiod_direction_output_raw(desc, 1);
else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
status = gpiod_direction_output_raw(desc, 0);
else if (sysfs_streq(buf, "in"))
status = gpiod_direction_input(desc);
else
status = -EINVAL;
mutex_unlock(&data->mutex);
return status ? : size;
}
I don’t know what the gpiolib-sysfs.c file has to do with export but the mention of ‘high’ made me think to try,
echo 23 > /sys/class/gpio/export #default direction is input
echo high > /sys/class/gpio/gpio23/direction
cat /sys/class/gpio/gpio23/direction
out
cat /sys/class/gpio/gpio23/value
1 #direction was set to output with a value of 1, which on my custom board is OFF