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