GPIO code
This commit is contained in:
		
							parent
							
								
									cd15ea0d55
								
							
						
					
					
						commit
						edc26eca45
					
				
					 1 changed files with 52 additions and 5 deletions
				
			
		| 
						 | 
					@ -1,6 +1,11 @@
 | 
				
			||||||
#include <linux/module.h>
 | 
					#include <linux/gpio/consumer.h>
 | 
				
			||||||
#include <linux/kernel.h>
 | 
					 | 
				
			||||||
#include <linux/init.h>
 | 
					#include <linux/init.h>
 | 
				
			||||||
 | 
					#include <linux/kernel.h>
 | 
				
			||||||
 | 
					#include <linux/module.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Last compiled with Linux 6.14.5-300.fc42.x86_64
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Meta Info */
 | 
					/* Meta Info */
 | 
				
			||||||
MODULE_DESCRIPTION("Demo kernel module.");
 | 
					MODULE_DESCRIPTION("Demo kernel module.");
 | 
				
			||||||
| 
						 | 
					@ -8,19 +13,61 @@ MODULE_VERSION("0.0.1");
 | 
				
			||||||
MODULE_AUTHOR("Imbus");
 | 
					MODULE_AUTHOR("Imbus");
 | 
				
			||||||
MODULE_LICENSE("GPL");
 | 
					MODULE_LICENSE("GPL");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Hard coded for RPI3
 | 
				
			||||||
 | 
					 * Usually retrieved via device tree
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define IO_LED 21
 | 
				
			||||||
 | 
					#define IO_BUTTON 20
 | 
				
			||||||
 | 
					#define IO_OFFSET 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct gpio_desc *led, *button;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief Called on load
 | 
					 * @brief Called on load
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static int __init demo_mod_init(void) {
 | 
					static int __init demo_mod_init(void) {
 | 
				
			||||||
    printk(KERN_INFO "Demo module loaded successfully...");
 | 
					  printk(KERN_INFO "Demo module loaded successfully...");
 | 
				
			||||||
    return 0;
 | 
					
 | 
				
			||||||
 | 
					  int status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  led = gpio_to_desc(IO_LED + IO_OFFSET);
 | 
				
			||||||
 | 
					  if (!led) {
 | 
				
			||||||
 | 
					    printk("gpioctrl - Error getting pin 21\n");
 | 
				
			||||||
 | 
					    return -ENODEV;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  button = gpio_to_desc(IO_BUTTON + IO_OFFSET);
 | 
				
			||||||
 | 
					  if (!button) {
 | 
				
			||||||
 | 
					    printk("gpioctrl - Error getting pin 20\n");
 | 
				
			||||||
 | 
					    return -ENODEV;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  status = gpiod_direction_output(led, 0);
 | 
				
			||||||
 | 
					  if (status) {
 | 
				
			||||||
 | 
					    printk("gpioctrl - Error setting pin 20 to output\n");
 | 
				
			||||||
 | 
					    return status;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  status = gpiod_direction_input(button);
 | 
				
			||||||
 | 
					  if (status) {
 | 
				
			||||||
 | 
					    printk("gpioctrl - Error setting pin 21 to input\n");
 | 
				
			||||||
 | 
					    return status;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  gpiod_set_value(led, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  printk("gpioctrl - Button is %spressed\n",
 | 
				
			||||||
 | 
					         gpiod_get_value(button) ? " " : "not ");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief Called on unload
 | 
					 * @brief Called on unload
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void __exit demo_mod_exit(void) {
 | 
					static void __exit demo_mod_exit(void) {
 | 
				
			||||||
    printk(KERN_INFO "Demo module unloaded successfully...");
 | 
					  printk(KERN_INFO "Demo module unloaded successfully...");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module_init(demo_mod_init);
 | 
					module_init(demo_mod_init);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue