If you don’t think this question is constructed properly, please see its original version. I have been asked to reduce this question to its minimal form.
If I’m writing a Linux device driver, how do I get programmatic access to actual GPIO pins? For example:
// Turn a green LED on by sending the GPIO pins 0x11223344
int cmd = encode(Commands.TURN_GREEN_ON);
send_to_gpio_pins(cmd);
Again, if this is unclear, it’s because I’m trying to oblige community rules for keeping it simple, stupid. In that case read my first version of this question.
A Linux device driver should have access to a set of functions provided by the arch’s gpio module. Since I am familiar with Atmel ARM code and not sure about the Broadcom SoC used by the RPI, here’s some real gpio code.
I/O devices on ARM SoCs are typically memory mapped (i.e. there is no separate I/O address space). These physical addresses can be mapped into kernel virtual address space. (The addresses of a peripheral’s registers are often mapped by the individual device driver for its exclusive use.)
Once mapped into virtual memory, registers that contain or control the state of a GPIO pin can simply be accessed by an ordinary read or write memory operation (while conforming to C language requirements). Consult the Broadcom SoC hardware document for the exact assignment/functionality of each bit in these device registers.
Prior to using a GPIO as either an input pin or an output pin, the GPIO pin must be configured. Often the pin will have multiple uses (it’s “multiplexed”), so one of those specific functionalities has to be selected during early board initilization. These assignments are performed by writing to device configuration registers (which are mapped to memory locations).
An Atmel function to write the value of a GPIO pin (from arch/arm/mach-at91/gpio.c) :
An Atmel function to read the value of a GPIO pin:
Hopefully you can find similar code in the kernel that you use (e.g. grep the
System.mapsymbol file for “gpio”).