Accessing GPIO from a Driver
The GPIO (General Purpose Input/Output) subsystem in Linux provides a unified interface for device drivers to interact with hardware pins that can be configured as either inputs or outputs. These pins are often used to control LEDs, read button states, or communicate with simple peripherals. The Linux kernel abstracts the hardware details, allowing you to request, configure, and manipulate GPIO pins using standard kernel APIs. This abstraction makes your driver code more portable and easier to maintain, regardless of the underlying hardware specifics.
gpio_request_example.c
12345678910111213141516171819202122232425262728293031323334353637383940414243#include <linux/module.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/kernel.h> #define GPIO_NUM 24 // Example GPIO pin number static int __init gpio_example_init(void) { int ret; // Request the GPIO pin ret = gpio_request(GPIO_NUM, "example_gpio"); if (ret) { pr_err("Failed to request GPIO %d\n", GPIO_NUM); return ret; } // Set GPIO direction to output, initial value low ret = gpio_direction_output(GPIO_NUM, 0); if (ret) { pr_err("Failed to set GPIO direction\n"); gpio_free(GPIO_NUM); return ret; } pr_info("GPIO %d requested and set as output\n", GPIO_NUM); return 0; } static void __exit gpio_example_exit(void) { gpio_set_value(GPIO_NUM, 0); // Set low on exit gpio_free(GPIO_NUM); pr_info("GPIO %d released\n", GPIO_NUM); } module_init(gpio_example_init); module_exit(gpio_example_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("GPIO Request and Configure Example");
Once you have requested and configured a GPIO pin as an output, you can control its value directly from your driver. Toggling a GPIO pin means changing its state from high to low or vice versa. This is typically done using the gpio_set_value() function, which sets the output level of the pin. Referencing the configuration code above, after you have successfully set the direction, you are ready to toggle the pin as needed by your device logic.
gpio_toggle_example.c
12345678910111213141516171819202122232425262728293031323334353637383940414243// gpio_toggle_example.c #include <linux/module.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/kernel.h> #define GPIO_NUM 24 static int __init gpio_toggle_init(void) { int ret; ret = gpio_request(GPIO_NUM, "toggle_gpio"); if (ret) return ret; ret = gpio_direction_output(GPIO_NUM, 0); if (ret) { gpio_free(GPIO_NUM); return ret; } // Set GPIO high gpio_set_value(GPIO_NUM, 1); pr_info("GPIO %d set HIGH\n", GPIO_NUM); // Set GPIO low gpio_set_value(GPIO_NUM, 0); pr_info("GPIO %d set LOW\n", GPIO_NUM); return 0; } static void __exit gpio_toggle_exit(void) { gpio_set_value(GPIO_NUM, 0); gpio_free(GPIO_NUM); } module_init(gpio_toggle_init); module_exit(gpio_toggle_exit); MODULE_LICENSE("GPL");
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how to request and configure a GPIO pin in a Linux driver?
What are some common use cases for GPIO pins in embedded systems?
How does the Linux kernel abstract hardware details for GPIOs?
Awesome!
Completion rate improved to 3.85
Accessing GPIO from a Driver
Swipe to show menu
The GPIO (General Purpose Input/Output) subsystem in Linux provides a unified interface for device drivers to interact with hardware pins that can be configured as either inputs or outputs. These pins are often used to control LEDs, read button states, or communicate with simple peripherals. The Linux kernel abstracts the hardware details, allowing you to request, configure, and manipulate GPIO pins using standard kernel APIs. This abstraction makes your driver code more portable and easier to maintain, regardless of the underlying hardware specifics.
gpio_request_example.c
12345678910111213141516171819202122232425262728293031323334353637383940414243#include <linux/module.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/kernel.h> #define GPIO_NUM 24 // Example GPIO pin number static int __init gpio_example_init(void) { int ret; // Request the GPIO pin ret = gpio_request(GPIO_NUM, "example_gpio"); if (ret) { pr_err("Failed to request GPIO %d\n", GPIO_NUM); return ret; } // Set GPIO direction to output, initial value low ret = gpio_direction_output(GPIO_NUM, 0); if (ret) { pr_err("Failed to set GPIO direction\n"); gpio_free(GPIO_NUM); return ret; } pr_info("GPIO %d requested and set as output\n", GPIO_NUM); return 0; } static void __exit gpio_example_exit(void) { gpio_set_value(GPIO_NUM, 0); // Set low on exit gpio_free(GPIO_NUM); pr_info("GPIO %d released\n", GPIO_NUM); } module_init(gpio_example_init); module_exit(gpio_example_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("GPIO Request and Configure Example");
Once you have requested and configured a GPIO pin as an output, you can control its value directly from your driver. Toggling a GPIO pin means changing its state from high to low or vice versa. This is typically done using the gpio_set_value() function, which sets the output level of the pin. Referencing the configuration code above, after you have successfully set the direction, you are ready to toggle the pin as needed by your device logic.
gpio_toggle_example.c
12345678910111213141516171819202122232425262728293031323334353637383940414243// gpio_toggle_example.c #include <linux/module.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/kernel.h> #define GPIO_NUM 24 static int __init gpio_toggle_init(void) { int ret; ret = gpio_request(GPIO_NUM, "toggle_gpio"); if (ret) return ret; ret = gpio_direction_output(GPIO_NUM, 0); if (ret) { gpio_free(GPIO_NUM); return ret; } // Set GPIO high gpio_set_value(GPIO_NUM, 1); pr_info("GPIO %d set HIGH\n", GPIO_NUM); // Set GPIO low gpio_set_value(GPIO_NUM, 0); pr_info("GPIO %d set LOW\n", GPIO_NUM); return 0; } static void __exit gpio_toggle_exit(void) { gpio_set_value(GPIO_NUM, 0); gpio_free(GPIO_NUM); } module_init(gpio_toggle_init); module_exit(gpio_toggle_exit); MODULE_LICENSE("GPL");
Thanks for your feedback!