GPIO Driver and Driver Binding Computer Science Engineering
GPIO Driver and Driver Binding Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu. edu (480) 727 -7507 CSE 530 EOSI Fall 2016
Linux GPIO Driver q A GPIO (General Purpose Input/Output) pin can be configured, set up its direction, and value if it is an output pin q A So. C chip may have several GPIO components v Multiple “gpio chips” q A global number in the integrated GPIO namespace, i. e. , 0, 1, 2, …, n q sysfs interface to user space GPIO framework (gpiolib. c) IO expanders GPIO[…. ] CSE 530 EOSI Fall 2016 Quark GIP controller GPIO[9: 0] Quark legacy GPIO chip drivers GPIO_SUS[5: 0] 1
Software Components for Galileo Gen 2 GPIO q /linux/driver/gpiolib. c v the core program for gpio subsystem (framework) q /linux/driver/mfd/intel_qrk_gip_core. c v the dontroller for gip device on PCI bus q /linux/driver/mfd/intel_qrk_gip_gpio. c v gpio chip controller for gip_gpio q /linux/driver/gpio-pca 953 x. c v gpio chip controller for I 2 c expander pca 9535 q /linux/driver/i 2 c/busses/i 2 c-designware-core. c v i 2 c bus controller for designware i 2 c controller q /linux/driver/mfd/intel-qrk-gip_i 2 c. c v a warpper to connect to i 2 c_designware_core. c q /linux/driver/gpio-sch. c v gpio chip controller for gpio port in Poulsbo SCH (system control hub) q /linux/driver/platform/x 86/quark/intel_qrk_board_data. c q /linux/driver/platform/x 86/quark/intel_qrk_plat_galileo_gen 2. c CSE 530 EOSI Fall 2016 2
GPIO Chip Driver(1) q A driver for each type of GPIO controller to provide v methods to establish GPIO direction and to access GPIO values v method to return the IRQ number associated to a given GPIO v flag saying whether calls to its methods may sleep v optional base number q In intel_qrk_gip_gpio. c /* The base GPIO number under GPIOLIB framework */ #define INTEL_QRK_GIP_GPIO_BASE 8 /* The default number of South-Cluster GPIO on Quark. */ #define INTEL_QRK_GIP_NGPIO 8 q In /include/asm-generic/gpio. h, “gpio_chip” is defined, including v base: identifies the first GPIO number handled by this chip. v ngpio: the number of GPIOs handled by this controller; the last GPIO handled is (base + ngpio - 1). CSE 530 EOSI Fall 2016 3
GPIO Chip Driver(2) q gpio_chip includes function pointers to operate a gpio chip int (*get)(struct gpio_chip *chip, unsigned offset); void (*set)(struct gpio_chip *chip, unsigned offset, int value); int (*direction_input)(struct gpio_chip *chip, unsigned offset); ……. v So, we can invoke operations at each pin of a gpio chip q When a gpio chip driver is installed v register itself to the bus it attached (e. g. PCI or i 2 c) v The callback “probe” function is invoked to initialize struct gpio_chip, then to add gpio_chip v /driver/mfd/intel_qrk_gip_gpio. c for gip_gpio v /driver/gpio_pca 953 x. c for pca 9535 chips CSE 530 EOSI Fall 2016 4
Binding of GPIOs to Pins q Example: gpio 26 is bit 10 of the 1 st PCAL 9535 chip q Each GPIO chip is represented by “struct gpio_chip” v standard methods such as get, set, etc. v “int base; ” and “u 16 ngpio; ” q When a gpio chip is added, use “base” and “ngpio” to determine “pin range” of the chip q In intel_qrk_plt_galileo_gen 2. c, define gpio_base q When pca 953 x_setup_gpio() is invoked, ngpio is set, q When pca 953 x_gpio_get_value() is called, v v an input argument: gpio=26 use container_of to find the chip (there are 3 PCAL 9535) invoke pca 953 x_read_reg(chip, offset, ®_val) to read input reg check the bit and return 1 or 0 CSE 530 EOSI Fall 2016 5
IO Expenders in Galileo Gen 1 & 2 q Gen 1 – CY 8 C 9540 A q Gen 2 – 3 PCAL 9535 q Both connected to quark processor via I 2 C bus q How does the Linux know board configuration v Read BIOS DMI data to identify the board v Then, register the board (platform_device_register() in linux/drivers/platform/x 86/quark/intel_qrk_board_data. c) v Once the board is registered, find the platform_driver for "Galileo. Gen 2“, i. e. , intel_qrk_plat_galileo_gen 2. c v In the platform (board) driver, 3 PCA 9535 chips are defined and then probed. strlcpy(probed_i 2 c_pcal 9555 a_exp 0. type, "pcal 9555 a", I 2 C_NAME_SIZE); client = i 2 c_new_probed_device(i 2 c_adap, &probed_i 2 c_pcal 9555 a_exp 0, pcal 9555 a_exp 0_i 2 c_addr, i 2 c_probe); CSE 530 EOSI Fall 2016 6
Binding of GPIO Adapter and Driver (1) q Case 1: pca 9535 v instantiate the devices --- add 3 pca 9535 devices as i 2 c clients in intel_qrk_plat_galileo_gen 2. c (call to i 2 c_new_device()) v initiate pca 953 x driver – static int __init pca 953 x_init(void) { return i 2 c_add_driver(&pca 953 x_driver); • driver_register } • bus_add_driver v then invoke i 2 c_register_driver and eventually • driver_attach pca 953 x_probe () if found matching devices • __driver_attach for each v In the probe function device on bus Ø pca 953 x_setup_gpio • driver_probe_device if matches Ø device_pca 953 x_init • really_probe Ø gpiochip_add • dev->bus->probe (i. e. i 2 c_device_probe) • pca 953 x_probe CSE 530 EOSI Fall 2016 7
Binding of GPIO Adapter and Driver (2) q Case 2: gip_gpio v instantiate the devices --- pci device initialization v initiate intel_qrk_gip_core driver – static intel_qrk_gip_init(void) { return pci_register_driver(&intel_qrk_gip_driver); } v then invoke pci_register_driver and eventually intel_qrk_gip_probe () and then pci_enable_device (for gip) and intel_qrk_gpio_probe v In the probe function Ø set up a gpio_chip struct Ø gpiochip_add CSE 530 EOSI Fall 2016 8
GPIO Driver Operation in gpio core q GPIO chip driver request to add “gpio_chip” to the platform gc->base = pdata->gpio_base; gc->ngpio = NGPIO; ret = gpiochip_add(&dev->gpio_chip); q gpiolib. c exports methods to work on GPIO pins v from GPIO # to find chip and to invoke the corresponding methods provided by the chip gpio_request_one(LED 1, GPIOF_OUT_INIT_LOW, "led 1"); gpio_desc 1 = gpio_to_desc(LED 1); gpio_set_value(desc 1, data); v sysfs gpio interfaces, such as gpiod_export, gpio_unexport, gpiod_set_value, gpio_direction_input CSE 530 EOSI Fall 2016 9
GPIO Interrupts (1) q Interrupts from gip_gpio v One interrupt source from gip controller q GIP ISR is requested in intel_qrk_gip_probe() request_irq(pdev->irq, intel_qrk_gip_handler, IRQF_SHARED, "intel_qrk_gip", gip_drvdata); q The handler calls i 2 c_dw_isr() and intel_qrk_gpio_isr() q In intel_qrk_gpio_isr(), v Check any pending interrupts (triggered) pin gpio irq desc->handle_irq(desc); v irq_desc to keep track of interrupt request source and manage IRQ flow. Ø A list of irq actions and an action may be called when the irq occurs v gpio_chip: include irq_chip as the driver for interrupt controller (to manage hardware) v To receive a gpio interrupt, an isr (action) must be registered for the irq with proper triggering flag. CSE 530 EOSI Fall 2016 10
GPIO Interrupts (2) q In gpio sysfs interface, setting an edge type enables interrupt gpio_edge_store() gpio_setup_irq() request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, "gpiolib", value_sd); request_threaded_irq(irq, NULL, handler, flags, name, dev_id); (or request_irq(…)) an action is allocated __setup_irq(irq, desc, action); v The handler static irqreturn_t gpio_sysfs_irq(int irq, void *priv) to wake up threads in polling list (of sysfs dir) q A similar approach 1. irq_no = gpio_to_irq(GPIO_X); // get the irq number from Linux gpio number 2. request_irq(irq_no, isr_handler, TRIGGER_FLAG, “name", NULL); 3. Use irq_set_irq_type() to reset flags CSE 530 EOSI Fall 2016 11
Linux Kernel Thread q A way to implement background tasks inside the kernel static struct task_struct *tsk; static int thread_function(void *data) { int time_count = 0; do { printk(KERN_INFO "thread_function: %d times", ++time_count); msleep(1000); }while(!kthread_should_stop() && time_count<=30); return time_count; } static int hello_init(void) { tsk = kthread_run(thread_function, NULL, "mythread%d", 1); if (IS_ERR(tsk)) { …. } } v kthread_run() kthread_create() + wake_up_process() create a kthread_create_info wake_up kthreadd_task calls create_kthread() kernel_thread() do_fork () CSE 530 EOSI Fall 2016 12
Platform Drivers q platform_driver_register(&my_pdevice_of_driver ); q The driver’s probe function v invoked when a platform device is registered and it's device name matches the name specified on the device driver. v Initializes and registers the device(s) static const struct platform_device_id my_pdevice_id_table[] = { { "basic-mmio-gpio", }, { "basic-mmio-gpio-be", }, {}, }; MODULE_DEVICE_TABLE(platform, my_pdevice_id_table); static struct platform_driver my_pdevice_driver = {. name = "my_pdevice”, . owner = THIS_MODULE, . of_match_table = of_match_ptr(my_pdevice_of_match), }, , id_table = my_pdevice_id_table, . probe = my_pdevice_probe, . remove = my_pdevice_remove, }; module_platform_driver(my_pdevice_of_driver); CSE 530 EOSI Fall 2016 13
Platform Devices q platform_device v name, used in driver binding, v a list of resources q Once a platform device is declared, v init device structure v after attached to platform_bus (a virtual bus) v device_add(&pdev->dev) struct platform_device { const char u 32 struct device u 32 struct resource }; *name; id; dev; num_resources; *resource; bus_probe_device(dev) device_attach(dev) bus_for_each_drv(dev->bus, NULL, dev, __device_attach) driver_match_device(drv, dev) drv->bus->match(dev, drv) given “platform_bus_type”, platform_match(dev, drv) driver_probe_device(drv, dev) int platform_device_register(struct platform_device *pdev) { device_initialize(&pdev->dev); return platform_device_add(pdev); } CSE 530 EOSI Fall 2016 14
Platform_match if (pdev->driver_override) return !strcmp(pdev->driver_override, drv->name); if (of_driver_match_device(dev, drv)) /* an OF style match first */ return 1; if (acpi_driver_match_device(dev, drv)) /* ACPI style match */ return 1; if (pdrv->id_table) /* match against the id table */ return platform_match_id(pdrv->id_table, pdev) != NULL; /* fall-back to driver name match */ return (strcmp(pdev->name, drv->name) == 0); static const struct i 2 c_device_id pca 953 x_id[] = { { "pca 9535", 16 | PCA 953 X_TYPE | PCA_INT, }, … { "pca 9555", 16 | PCA 953 X_TYPE | PCA_INT, }, { "pcal 9555 a", 16 | PCAL 953 X_TYPE | PCA_INT, }, …} CSE 530 EOSI Fall 2016 15
I 2 C Devices and Drivers of Galileo Board CSE 530 EOSI Fall 2016 set 4 -- 16
Platform Devices and Drivers of Galileo Board CSE 530 EOSI Fall 2016 set 4 -- 17
- Slides: 18