1 /*
2 * Copyright (c) 2022 HPMicro
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "device_resource_if.h"
19 #include "hdf_device_desc.h"
20 #include "osal_sem.h"
21 #include "osal_mem.h"
22 #include "hdf_log.h"
23 #include "i2c_if.h"
24 #include "i2c_core.h"
25 #include "hpm_i2c_drv.h"
26 #include <los_interrupt.h>
27
28 #define HDF_LOG_TAG HPMICRO_I2C_HDF
29
30 struct HPMI2cDevice {
31 uint32_t id;
32 uint32_t base;
33 uint32_t clkFreq;
34 };
35
hpmTransfer(struct I2cCntlr * cntlr,struct I2cMsg * msgs,int16_t count)36 static int32_t hpmTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count)
37 {
38 struct HPMI2cDevice *hpmI2cDev = (struct HPMI2cDevice *)cntlr->priv;
39 I2C_Type *base = (I2C_Type *)hpmI2cDev->base;
40
41 for (int i = 0; i < count; i++) {
42 if (msgs[i].flags & I2C_FLAG_ADDR_10BIT) {
43 i2c_enable_10bit_address_mode(base, 1);
44 } else {
45 i2c_enable_10bit_address_mode(base, 0);
46 }
47
48 /*
49 * TODO: Another flags: I2C_FLAG_NO_START, I2C_FLAG_STOP, I2C_FLAG_READ_NO_ACK
50 */
51 if (msgs[i].flags & I2C_FLAG_READ) {
52 if (i2c_master_read(base, msgs[i].addr, msgs[i].buf, msgs[i].len) != status_success) {
53 HDF_LOGI("i2c read: Failed!!!\n");
54 return HDF_FAILURE;
55 }
56 } else {
57 if (i2c_master_write(base, msgs[i].addr, msgs[i].buf, msgs[i].len) != status_success) {
58 HDF_LOGI("i2c write: Failed!!!\n");
59 return HDF_FAILURE;
60 }
61 }
62 }
63
64 return 0;
65 }
66
67 static struct I2cMethod hpmI2cMethod = {
68 .transfer = hpmTransfer,
69 };
70
71
I2cDriverBind(struct HdfDeviceObject * device)72 static int32_t I2cDriverBind(struct HdfDeviceObject *device)
73 {
74 int32_t ret = HDF_SUCCESS;
75
76 HDF_LOGI("Bind: successed\n");
77 return ret;
78 }
79
I2cDriverInit(struct HdfDeviceObject * device)80 static int32_t I2cDriverInit(struct HdfDeviceObject *device)
81 {
82 int32_t ret = HDF_SUCCESS;
83
84 struct I2cCntlr *cntlr = (struct I2cCntlr *)OsalMemCalloc(
85 sizeof(struct I2cCntlr) + sizeof(struct HPMI2cDevice));
86 if (cntlr == NULL) {
87 ret = HDF_FAILURE;
88 HDF_LOGE("Init: cntlr alloc Failed!!!\n");
89 return ret;
90 }
91
92 struct HPMI2cDevice *hpmI2cDev = (struct HPMI2cDevice *)((char *)cntlr + sizeof(struct I2cCntlr));
93 cntlr->priv = hpmI2cDev;
94 device->priv = cntlr;
95
96 struct DeviceResourceIface *dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
97 if (dri == NULL) {
98 ret = HDF_FAILURE;
99 HDF_LOGE("Init: get DeviceResourceIface Failed!!!\n");
100 goto ERROR1;
101 }
102
103 dri->GetUint32(device->property, "id", &hpmI2cDev->id, 0);
104 dri->GetUint32(device->property, "base", &hpmI2cDev->base, 0);
105 dri->GetUint32(device->property, "clk_freq", &hpmI2cDev->clkFreq, 0);
106 HDF_LOGI("Init: hpmI2cDev->id: %u\n", hpmI2cDev->id);
107 HDF_LOGI("Init: hpmI2cDev->base: 0x%X\n", hpmI2cDev->base);
108 HDF_LOGI("Init: hpmI2cDev->clkFreq: %u\n", hpmI2cDev->clkFreq);
109
110 cntlr->busId = hpmI2cDev->id;
111 cntlr->ops = &hpmI2cMethod;
112 ret = I2cCntlrAdd(cntlr);
113 if (ret) {
114 HDF_LOGE("Init: I2cCntlrAdd Failed!!!\n");
115 ret = HDF_FAILURE;
116 goto ERROR1;
117 }
118
119 I2C_Type *base = (I2C_Type *)hpmI2cDev->base;
120 i2c_config_t i2cConfig;
121 i2cConfig.i2c_mode = i2c_mode_normal;
122 i2cConfig.is_10bit_addressing = 0;
123 i2c_init_master(base, hpmI2cDev->clkFreq, &i2cConfig);
124
125 return ret;
126 ERROR1:
127 OsalMemFree(cntlr);
128 return ret;
129 }
130
I2cDriverRelease(struct HdfDeviceObject * device)131 static void I2cDriverRelease(struct HdfDeviceObject *device)
132 {
133 if (device == NULL) {
134 return;
135 }
136
137 struct I2cCntlr *cntlr = (struct I2cCntlr *)device->priv;
138 if (cntlr == NULL) {
139 HDF_LOGE("Release: cntlr null\n");
140 return;
141 }
142
143 I2cCntlrRemove(cntlr);
144 OsalMemFree(cntlr);
145
146 HDF_LOGI("Release");
147 return;
148 }
149
150 struct HdfDriverEntry g_i2cDriverEntry = {
151 .moduleVersion = 1,
152 .moduleName = "HPMICRO_I2C_MODULE_HDF",
153 .Bind = I2cDriverBind,
154 .Init = I2cDriverInit,
155 .Release = I2cDriverRelease,
156 };
157
158 HDF_INIT(g_i2cDriverEntry);