1# Platform Driver Porting<a name="EN-US_TOPIC_0000001154372314"></a> 2 3Create a platform driver in the source code directory **//device/vendor\_name/soc\_name/drivers**. If there is no repository for the vendor of your SoC, contact the [device SIG](https://gitee.com/openharmony/community/blob/master/sig/sig_devboard/sig_devboard.md) to create one. 4 5The recommended directory structure is as follows: 6 7``` 8device 9├── vendor_name 10│ ├── drivers 11│ │ │ ├── common 12│ │ │ ├── Kconfig # Entry of the vendor driver kernel menu 13│ │ │ └── lite.mk # Build entry 14│ ├── soc_name 15│ │ ├── drivers 16│ │ │ ├── dmac 17│ │ │ ├── gpio 18│ │ │ ├── i2c 19│ │ │ ├── LICENSE 20│ │ │ ├── mipi_dsi 21│ │ │ ├── mmc 22│ │ │ ├── pwm 23│ │ │ ├── README.md # docs Add documentation information as needed. 24│ │ │ ├── README_zh.md 25│ │ │ ├── rtc 26│ │ │ ├── spi 27│ │ │ ├── uart 28│ │ │ └── watchdog 29│ ├── board_name 30``` 31 32The HDF creates driver models for all platform drivers. The main task of porting platform drivers is to inject instances into the models. You can find the definitions of these models in the source code directory **//drivers/framework/support/platform/include**. 33 34The following uses the GPIO as an example to describe how to port the platform driver: 35 361. Create a GPIO driver. 37 38 Create the **soc\_name\_gpio.c** file in the source code directory **//device/vendor\_name/soc\_name/drivers/gpio**. The sample code is as follows: 39 40 ``` 41 #include "gpio_core.h" 42 43 // Define the GPIO structure if necessary. 44 struct SocNameGpioCntlr { 45 struct GpioCntlr cntlr; // Structure required by the HDF GPIO driver framework. 46 int myData; // The following information is required by the current driver. 47 }; 48 49 // The Bind method is mainly used to release services in the HDF driver. As this method is not needed here, simply return a success message. 50 static int32_t GpioBind(struct HdfDeviceObject *device) 51 { 52 (void)device; 53 return HDF_SUCCESS; 54 } 55 56 // Entry for initializing the driver when the Init method is used. You need to register the model instance in the Init method. 57 static int32_t GpioInit(struct HdfDeviceObject *device) 58 { 59 SocNameGpioCntlr *impl = CreateGpio(); // Implement the CreateGpio method. 60 ret = GpioCntlrAdd(&impl->cntlr); // Register a GPIO model instance. 61 if (ret != HDF_SUCCESS) { 62 HDF_LOGE("%s: err add controller:%d", __func__, ret); 63 return ret; 64 } 65 return HDF_SUCCESS; 66 } 67 68 // The Release method is called when the driver is uninstalled to reclaim resources. 69 static void GpioRelease(struct HdfDeviceObject *device) 70 { 71 // The GpioCntlrFromDevice method obtains the model instance registered in the init method from the abstract device object. 72 struct GpioCntlr *cntlr = GpioCntlrFromDevice(device); 73 // Release resources. 74 } 75 76 struct HdfDriverEntry g_gpioDriverEntry = { 77 .moduleVersion = 1, 78 .Bind = GpioBind, 79 .Init = GpioInit, 80 .Release = GpioRelease, 81 .moduleName = "SOC_NAME_gpio_driver", // Name to be used in the configuration file to load the driver. 82 }; 83 HDF_INIT(g_gpioDriverEntry); // Register a GPIO driver entry. 84 ``` 85 862. Create a build entry for the vendor driver. 87 88 As described above, **device/vendor\_name/drivers/lite.mk** is the entry for building vendor drivers. We need to start building from this entry. 89 90 ``` 91 # File: device/vendor_name/drivers/lite.mk 92 93 SOC_VENDOR_NAME := $(subst $/",,$(LOSCFG_DEVICE_COMPANY)) 94 SOC_NAME := $(subst $/",,$(LOSCFG_PLATFORM)) 95 BOARD_NAME := $(subst $/",,$(LOSCFG_PRODUCT_NAME)) 96 97 # Specify the SoC for building. 98 LIB_SUBDIRS += $(LITEOSTOPDIR)/../../device/$(SOC_VENDOR_NAME)/$(SOC_NAME)/drivers/ 99 ``` 100 1013. Create a build entry for the SoC driver. 102 103 ``` 104 # File: device/vendor_name/soc_name/drivers/lite.mk 105 106 SOC_DRIVER_ROOT := $(LITEOSTOPDIR)/../../device/$(SOC_VENDOR_NAME)/$(SOC_NAME)/drivers/ 107 108 # Check whether the kernel compilation switch of the GPIO is enabled. 109 ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_GPIO), y) 110 # After the construction is complete, an object named hdf_gpio needs to be linked. 111 LITEOS_BASELIB += -lhdf_gpio 112 # Add the build directory gpio. 113 LIB_SUBDIRS += $(SOC_DRIVER_ROOT)/gpio 114 endif 115 116 # Add other drivers here. 117 ``` 118 1194. Create a build entry for the GPIO driver. 120 121 ``` 122 include $(LITEOSTOPDIR)/config.mk 123 include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk 124 125 # Specify the name of the output object. Ensure that the name is the same as LITEOS_BASELIB in the SoC driver build entry. 126 MODULE_NAME := hdf_gpio 127 128 # Add the INCLUDE of the HDF framework. 129 LOCAL_CFLAGS += $(HDF_INCLUDE) 130 131 # Specify the file to be compiled. 132 LOCAL_SRCS += soc_name_gpio.c 133 134 # Build parameters 135 LOCAL_CFLAGS += -fstack-protector-strong -Wextra -Wall -Werror -fsigned-char -fno-strict-aliasing -fno-common 136 137 include $(HDF_DRIVER) 138 ``` 139 1405. Configure the product loading driver. 141 142 All device information of the product is defined in the source code file **//vendor/vendor\_name/product\_name/config/device\_info/device\_info.hcs**. 143 144 Add the platform driver to the host of the platform. 145 146 >![](../public_sys-resources/icon-note.gif) **NOTE:** 147 >The value of **moduleName** must be the same as that defined in the driver. 148 149 ``` 150 root { 151 ... 152 platform :: host { 153 device_gpio :: device { 154 device0 :: deviceNode { 155 policy = 0; 156 priority = 10; 157 permission = 0644; 158 moduleName = "SOC_NAME_gpio_driver"; 159 } 160 } 161 } 162 } 163 ``` 164 165 166