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
14 #include "hdf_device_desc.h"
15 #include "hdf_log.h"
16 #include "device_resource_if.h"
17
18 #define HDF_LOG_TAG i2c_driver
19
20 struct I2cResource {
21 uint32_t irqNum;
22 uint32_t dataRate;
23 uint32_t devAddr;
24 };
25
26 struct I2cService {
27 struct IDeviceIoService service;
28
29 struct I2cResource resource;
30 int (*test)(void);
31 };
32
33 static struct I2cService g_i2cService;
34
GetI2cDeviceResource(struct I2cService * device,const struct DeviceResourceNode * resourceNode)35 static int GetI2cDeviceResource(struct I2cService *device, const struct DeviceResourceNode *resourceNode)
36 {
37 struct DeviceResourceIface *dri = NULL;
38 struct I2cResource *resource;
39
40 if (device == NULL || resourceNode == NULL) {
41 HDF_LOGE("%s: device is %p, resourceNode is %p", __func__, device, resourceNode);
42 return HDF_ERR_INVALID_PARAM;
43 }
44
45 resource = &device->resource;
46
47 dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
48 if (dri == NULL) {
49 HDF_LOGE("DeviceResourceIface is invalid");
50 return HDF_ERR_INVALID_OBJECT;
51 }
52
53 if (dri->GetUint32(resourceNode, "irqNum", &resource->irqNum, 0) != HDF_SUCCESS) {
54 HDF_LOGE("I2c config read base fail");
55 return HDF_FAILURE;
56 }
57
58 if (dri->GetUint32(resourceNode, "dataRate", &resource->dataRate, 0) != HDF_SUCCESS) {
59 HDF_LOGE("I2c config read irqNum fail");
60 return HDF_FAILURE;
61 }
62
63 if (dri->GetUint32(resourceNode, "devAddr", &resource->devAddr, 0) != HDF_SUCCESS) {
64 HDF_LOGE("I2c config read baudrate fail");
65 return HDF_FAILURE;
66 }
67
68 HDF_LOGI("parse resource : irqNum=%d, dataRate=%d, devAddr=%d",
69 resource->irqNum, resource->dataRate, resource->devAddr);
70
71 return HDF_SUCCESS;
72 }
73
I2cDriverBind(struct HdfDeviceObject * deviceObject)74 static int32_t I2cDriverBind(struct HdfDeviceObject *deviceObject)
75 {
76 HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject);
77
78 if (deviceObject == NULL) {
79 return HDF_FAILURE;
80 }
81
82 deviceObject->service = &g_i2cService.service;
83 return HDF_SUCCESS;
84 }
85
I2cDriverInit(struct HdfDeviceObject * deviceObject)86 static int32_t I2cDriverInit(struct HdfDeviceObject *deviceObject)
87 {
88 int ret;
89
90 HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject);
91
92 if (deviceObject == NULL) {
93 return HDF_FAILURE;
94 }
95
96 ret = GetI2cDeviceResource(&g_i2cService, deviceObject->property);
97 if (ret != HDF_SUCCESS) {
98 HDF_LOGE("I2c parse resource failed");
99 return HDF_FAILURE;
100 }
101
102 HDF_LOGD("%s:Init success", __func__);
103 return HDF_SUCCESS;
104 }
105
I2cDriverRelease(struct HdfDeviceObject * deviceObject)106 static void I2cDriverRelease(struct HdfDeviceObject *deviceObject)
107 {
108 HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject);
109 }
110
111 struct HdfDriverEntry g_I2cDriverEntry = {
112 .moduleVersion = 1,
113 .moduleName = "HDF_PLATFORM_I2C_DRIVER",
114 .Bind = I2cDriverBind,
115 .Init = I2cDriverInit,
116 .Release = I2cDriverRelease,
117 };
118
119 HDF_INIT(g_I2cDriverEntry);
120