# GPIO ## Overview In the Hardware Driver Foundation \(HDF\) framework, the general-purpose input/output \(GPIO\) module uses the service-free mode for API adaptation. The service-free mode applies to the devices that do not provide user-mode APIs or the OS system that does not distinguish the user mode and the kernel mode. In the service-free mode, **DevHandle** \(a void pointer\) directly points to the kernel-mode address of the device object. **Figure 1** Service-free mode ![](figures/service-free-mode.png "service-free-mode") ## Available APIs GpioMethod ``` struct GpioMethod { int32_t (*request)(struct GpioCntlr *cntlr, uint16_t local);// Reserved int32_t (*release)(struct GpioCntlr *cntlr, uint16_t local);// Reserved int32_t (*write)(struct GpioCntlr *cntlr, uint16_t local, uint16_t val); int32_t (*read)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *val); int32_t (*setDir)(struct GpioCntlr *cntlr, uint16_t local, uint16_t dir); int32_t (*getDir)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *dir); int32_t (*toIrq)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *irq);// Reserved int32_t (*setIrq)(struct GpioCntlr *cntlr, uint16_t local, uint16_t mode, GpioIrqFunc func, void *arg); int32_t (*unsetIrq)(struct GpioCntlr *cntlr, uint16_t local); int32_t (*enableIrq)(struct GpioCntlr *cntlr, uint16_t local); int32_t (*disableIrq)(struct GpioCntlr *cntlr, uint16_t local); } ``` **Table 1** Callbacks for the members in the GpioMethod structure

Callback

Input Parameter

Output Parameter

Return Value

Description

write

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

val: input level value, which is of the uint16_t type.

HDF_STATUS

Writes the level of a GPIO pin.

read

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

val: pointer to the output level value, which is of the uint16_t type.

HDF_STATUS

Reads the level of a GPIO pin.

setDir

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

dir: input pin direction, which is of the uint16_t type.

HDF_STATUS

Sets the input or output direction for a GPIO pin.

getDir

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

dir: pointer to the output pin direction, which is of the uint16_t type.

HDF_STATUS

Obtains the input or output direction of a GPIO pin.

setIrq

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

mode: triggering mode, which can be edge or level. The value is of the uint16_t type.

func: function pointer to the interrupt service handler.

arg: void pointer to the input parameters of the interrupt service handler.

HDF_STATUS

Sets an interrupt request (IRQ) for a GPIO pin.

unsetIrq

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

HDF_STATUS

Cancels the GPIO interrupt settings.

enableIrq

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

HDF_STATUS

Enables an interrupt for a GPIO pin.

disableIrq

cntlr: structure pointer to the GPIO controller at the core layer.

local: GPIO port number, which is of the uint16_t type.

HDF_STATUS

Disables an interrupt for a GPIO pin.

## How to Develop The GPIO controller manages all pins by group. The related parameters are described in the attribute file. Instantiating the driver entry and API functions is the core for the vendor driver to access the HDF. The GPIO module adaptation involves the following steps: 1. Instantiate the driver entry. - Instantiate the **HdfDriverEntry** structure. - Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF framework. 2. Configure attribute files. - Add the **deviceNode** information to the **device\_info.hcs** file. - \(Optional\) Add the **gpio\_config.hcs** file. 3. Instantiate the GPIO controller object. - Initialize **GpioCntlr**. - Instantiate **GpioMethod** in the **GpioCntlr** object. >![](../public_sys-resources/icon-note.gif) **NOTE:** >For details, see [Available APIs](#section752964871810). 4. Debug the driver. - \(Optional\) For new drivers, verify the basic functions, such as the GPIO control status and response to interrupts. ## Development Example The following uses **gpio\_hi35xx.c** as an example to present the contents that need to be provided by the vendor to implement device functions. 1. Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type \(defined in **hdf\_device\_desc.h**\), and the value of **moduleName** must be the same as that in **device\_info.hcs**. In the HDF framework, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke. Generally, HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, HDF calls **Release** to release driver resources and exits. - GPIO driver entry reference ``` struct HdfDriverEntry g_gpioDriverEntry = { .moduleVersion = 1, .Bind = Pl061GpioBind, // Bind does not need to be implemented for GPIO. In this example, the implementation is empty. Vendors can add related operations as required. .Init = Pl061GpioInit, // See the Init function. .Release = Pl061GpioRelease, // See the Release function. .moduleName = "hisi_pl061_driver",// (Mandatory) The value must be the same as that of moduleName in the .hcs file. }; // Call HDF_INIT to register the driver entry with the HDF framework. HDF_INIT(g_gpioDriverEntry); ``` 2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the device attributes in the **gpio\_config.hcs** file. The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **GpioCntlr** members at the core layer. In this example, there is only one GPIO controller. If there are multiple GPIO controllers, you need to add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **gpio\_config** file. - **device\_info.hcs** configuration reference ``` root { device_info { platform :: host { hostName = "platform_host"; priority = 50; device_gpio :: device { device0 :: deviceNode { policy = 0; // The value 0 indicates that no service needs to be published. priority = 10; // Driver startup priority permission = 0644; // Permission to create device nodes for the driver moduleName = "hisi_pl061_driver"; // (Mandatory) Driver name, which must be the same as the moduleName in the driver entry. deviceMatchAttr = "hisilicon_hi35xx_pl061";// (Mandatory) Used to configure the private data of the controller. The value must be the same as the controller information in gpio_config.hcs. // The controller information must be consistent. Other controller information is also contained in the file. } } } } } ``` - **gpio\_config.hcs** configuration reference ``` root { platform { gpio_config { controller_0x120d0000 { match_attr = "hisilicon_hi35xx_pl061"; // (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs. groupNum = 12; // (Mandatory) GPIO group index bitNum = 8; // (Mandatory) Number of GPIO pins in each group regBase = 0x120d0000;// (Mandatory) Physical base address regStep = 0x1000; // (Mandatory) Register offset step irqStart = 48; // (Mandatory) Start an IRQ. irqShare = 0; // (Mandatory) Share the IRQ. } } } } ``` 3. Initialize the **GpioCntlr** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **GpioMethod** \(used to call underlying functions of the driver\) in **GpioCntlr**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\). - Custom structure reference To the driver, the custom structure carries parameters and data. The values in the **gpio\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. Some important values, such as the index and the number of pins, are also passed to the **GpioCntlr** object at the core layer. ``` struct Pl061GpioCntlr { struct GpioCntlr cntlr;// (Mandatory) Control object of the core layer. For details about the member definitions, see the following description. volatile unsigned char *regBase; // (Mandatory) Base address of the register uint32_t phyBase; // (Mandatory) Physical base address uint32_t regStep; // (Mandatory) Register offset step uint32_t irqStart; // (Mandatory) Start an IRQ. uint16_t groupNum; // (Mandatory) GPIO port number uint16_t bitNum; // (Mandatory) GPIO port number uint8_t irqShare; // (Mandatory) Share the IRQ. struct Pl061GpioGroup *groups; // (Optional) Set based on the vendor's requirements. }; struct Pl061GpioGroup {// Register address, interrupt number, interrupt function, and lock volatile unsigned char *regBase; unsigned int index; unsigned int irq; OsalIRQHandle irqFunc; OsalSpinlock lock; }; // GpioCntlr is the controller structure at the core layer. Its members are assigned with values by using the Init function. struct GpioCntlr { struct IDeviceIoService service; struct HdfDeviceObject *device; struct GpioMethod *ops; struct DListHead list; OsalSpinlock spin; uint16_t start; uint16_t count; struct GpioInfo *ginfos; void *priv; }; ``` - Instantiate the callback function structure **GpioMethod** in **GpioCntlr**. Other members are initialized by using the **Init** function. ``` // The members of the GpioMethod structure are all callbacks. Vendors need to implement the corresponding functions according to [Table 1](#table151341544111). static struct GpioMethod g_method = { .request = NULL, .release = NULL, .write = Pl061GpioWrite, // Write pin settings. .read = Pl061GpioRead, // Read pin settings. .setDir = Pl061GpioSetDir, // Set the pin direction. .getDir = Pl061GpioGetDir, // Obtain the pin direction. .toIrq = NULL, .setIrq = Pl061GpioSetIrq, // Set an interrupt for the pin. If this capability is not available, skip it. .unsetIrq = Pl061GpioUnsetIrq, // Cancel the interrupt settings for the pin. If this capability is not available, skip it. .enableIrq = Pl061GpioEnableIrq, // Enable an interrupt for the pin. If this capability is not available, skip it. .disableIrq = Pl061GpioDisableIrq,// Disable the interrupt for the pin. If this capability is not available, skip it. }; ``` - Init function Input parameters: **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. Return values: HDF\_STATUS \(The following table lists some status. For details about other status, see **HDF\_STATUS** in the **//drivers/framework/include/utils/hdf\_base.h** file.\) **Table 2** Init function description

Status (Value)

Description

HDF_ERR_INVALID_OBJECT

Invalid controller object

HDF_ERR_MALLOC_FAIL

Failed to allocate memory

HDF_ERR_INVALID_PARAM

Invalid parameter

HDF_ERR_IO

I/O error

HDF_SUCCESS

Initialization successful

HDF_FAILURE

Initialization failed

Function description: Initializes the custom structure object and **GpioCntlr**, calls the **GpioCntlrAdd** function at the core layer, and connects to the VFS \(optional\). ``` static int32_t Pl061GpioInit(struct HdfDeviceObject *device) { ... struct Pl061GpioCntlr *pl061 = &g_pl061;// Use static global variables to complete initialization. //static struct Pl061GpioCntlr g_pl061 = { // .groups = NULL, // .groupNum = PL061_GROUP_MAX, // .bitNum = PL061_BIT_MAX, //}; ret = Pl061GpioReadDrs(pl061, device->property);// Use the attribute values read from the gpio_config.HCS file to initialize the members of the custom structure object. ... pl061->regBase = OsalIoRemap(pl061->phyBase, pl061->groupNum * pl061->regStep);// Address mapping ... ret = Pl061GpioInitCntlrMem(pl061); // Apply for memory. ... pl061->cntlr.count = pl061->groupNum x pl061->bitNum;// (Mandatory) Calculate the number of pins. pl061->cntlr.priv = (void *)device->property; // (Mandatory) Store device attributes. pl061->cntlr.ops = &g_method; // (Mandatory) Connect to the GpioMethod instance. pl061->cntlr.device = device; // (Mandatory) Enable conversion between HdfDeviceObject and GpioCntlr. ret = GpioCntlrAdd(&pl061->cntlr); // (Mandatory) Call this function to set the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned. ... Pl061GpioDebugCntlr(pl061); #ifdef PL061_GPIO_USER_SUPPORT // (Optional) Access the user-level virtual file system if supported. if (GpioAddVfs(pl061->bitNum) != HDF_SUCCESS) { HDF_LOGE("%s: add vfs fail!", __func__); } #endif ... } ``` - Release function Input parameters: **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. Return values: – Function description: Releases the memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. When the HDF framework fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the **Init** function has the corresponding value assignment operations. ``` static void Pl061GpioRelease(struct HdfDeviceObject *device) { struct GpioCntlr *cntlr = NULL; struct Pl061GpioCntlr *pl061 = NULL; ... cntlr = GpioCntlrFromDevice(device);// (Mandatory) Obtain the control object of the core layer through forced conversion. //return (device == NULL) ? NULL : (struct GpioCntlr *)device->service; ... #ifdef PL061_GPIO_USER_SUPPORT GpioRemoveVfs();// Perform operations reverse to GpioAddVfs in Init. #endif GpioCntlrRemove(cntlr); // (Mandatory) Remove the device information and services from the core layer. pl061 = ToPl061GpioCntlr(cntlr); //return (struct Pl061GpioCntlr *)cntlr; Pl061GpioRleaseCntlrMem(pl061); // (Mandatory) Release the lock and memory. OsalIoUnmap((void *)pl061->regBase);// (Mandatory) Remove the address mapping. pl061->regBase = NULL; } ```