1 // Copyright (C) 2022 Beken Corporation
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 #include "hdf_device_desc.h"
14 #include "hdf_log.h"
15 #include "device_resource_if.h"
16
17 #define HDF_LOG_TAG uart_driver
18
19 struct UartResource {
20 uint32_t idx;
21 uint32_t base;
22 uint32_t irqNum;
23 uint32_t baudrate;
24 uint32_t uartClk;
25 const char *sn;
26 };
27
28 struct UartService {
29 struct IDeviceIoService service;
30
31 struct UartResource resource;
32 int (*test)(void);
33 };
34
35 static struct UartService g_uartService;
36
GetUartDeviceResource(struct UartService * device,const struct DeviceResourceNode * resourceNode)37 static int GetUartDeviceResource(struct UartService *device, const struct DeviceResourceNode *resourceNode)
38 {
39 struct DeviceResourceIface *dri = NULL;
40 struct UartResource *resource;
41
42 if (device == NULL || resourceNode == NULL) {
43 HDF_LOGE("%s: device is %p, resourceNode is %p", __func__, device, resourceNode);
44 return HDF_ERR_INVALID_PARAM;
45 }
46
47 resource = &device->resource;
48
49 dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
50 if (dri == NULL) {
51 HDF_LOGE("DeviceResourceIface is invalid");
52 return HDF_ERR_INVALID_OBJECT;
53 }
54
55 if (dri->GetUint32(resourceNode, "idx", &resource->idx, 0) != HDF_SUCCESS) {
56 HDF_LOGE("Uart config read idx fail");
57 return HDF_FAILURE;
58 }
59
60 if (dri->GetUint32(resourceNode, "base", &resource->base, 0) != HDF_SUCCESS) {
61 HDF_LOGE("Uart config read base fail");
62 return HDF_FAILURE;
63 }
64
65 if (dri->GetUint32(resourceNode, "irqNum", &resource->irqNum, 0) != HDF_SUCCESS) {
66 HDF_LOGE("Uart config read irqNum fail");
67 return HDF_FAILURE;
68 }
69
70 if (dri->GetUint32(resourceNode, "baudrate", &resource->baudrate, 0) != HDF_SUCCESS) {
71 HDF_LOGE("Uart config read baudrate fail");
72 return HDF_FAILURE;
73 }
74
75 if (dri->GetUint32(resourceNode, "uartClk", &resource->uartClk, 0) != HDF_SUCCESS) {
76 HDF_LOGE("Uart config read uartClk fail");
77 return HDF_FAILURE;
78 }
79
80 if (dri->GetString(resourceNode, "sn", &resource->sn, "no-sn") != HDF_SUCCESS) {
81 HDF_LOGE("Uart config read sn fail");
82 return HDF_FAILURE;
83 }
84
85 HDF_LOGI("parse resource : idx=%d, base=%d, irqNum=%d, baudrate=%d, uartClk=%d, sn=%s",
86 resource->idx, resource->base, resource->irqNum, resource->baudrate, resource->uartClk, resource->sn);
87
88 return HDF_SUCCESS;
89 }
90
UartDriverBind(struct HdfDeviceObject * deviceObject)91 static int32_t UartDriverBind(struct HdfDeviceObject *deviceObject)
92 {
93 HDF_LOGI("%s::enter, deviceObject=%p", __func__, deviceObject);
94
95 if (deviceObject == NULL) {
96 return HDF_FAILURE;
97 }
98
99 deviceObject->service = &g_uartService.service;
100 return HDF_SUCCESS;
101 }
102
UartDriverInit(struct HdfDeviceObject * deviceObject)103 static int32_t UartDriverInit(struct HdfDeviceObject *deviceObject)
104 {
105 int ret;
106
107 HDF_LOGI("%s::enter, deviceObject=%p", __func__, deviceObject);
108
109 if (deviceObject == NULL) {
110 return HDF_FAILURE;
111 }
112
113 ret = GetUartDeviceResource(&g_uartService, deviceObject->property);
114 if (ret != HDF_SUCCESS) {
115 HDF_LOGE("Uart parse resource failed");
116 return HDF_FAILURE;
117 }
118
119 HDF_LOGD("%s:Init success", __func__);
120 return HDF_SUCCESS;
121 }
122
UartDriverRelease(struct HdfDeviceObject * deviceObject)123 static void UartDriverRelease(struct HdfDeviceObject *deviceObject)
124 {
125 HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject);
126 }
127
128 struct HdfDriverEntry g_UartDriverEntry = {
129 .moduleVersion = 1,
130 .moduleName = "HDF_PLATFORM_UART_DRIVER",
131 .Bind = UartDriverBind,
132 .Init = UartDriverInit,
133 .Release = UartDriverRelease,
134 };
135
136 HDF_INIT(g_UartDriverEntry);
137