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 #include "board.h"
28
29 #define HDF_LOG_TAG HPMICRO_I2C_HDF
30
31 struct HPMI2cDevice {
32 uint32_t id;
33 uint32_t base;
34 uint32_t clkFreq;
35 };
36
hpmTransfer(struct I2cCntlr * cntlr,struct I2cMsg * msgs,int16_t count)37 static int32_t hpmTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count)
38 {
39 struct HPMI2cDevice *hpmI2cDev = (struct HPMI2cDevice *)cntlr->priv;
40 I2C_Type *base = (I2C_Type *)hpmI2cDev->base;
41 hpm_stat_t stat;
42 i2c_config_t config;
43 uint32_t freq;
44
45 config.i2c_mode = i2c_mode_normal;
46 config.is_10bit_addressing = false;
47 freq = clock_get_frequency(TEST_I2C_CLOCK_NAME);
48 stat = i2c_init_master(base, freq, &config);
49 if (stat != status_success) {
50 return stat;
51 }
52
53
54 for (int i = 0; i < count; i++) {
55 if (msgs[i].flags & I2C_FLAG_ADDR_10BIT) {
56 i2c_enable_10bit_address_mode(base, 1);
57 } else {
58 i2c_enable_10bit_address_mode(base, 0);
59 }
60
61 /*
62 * TODO: Another flags: I2C_FLAG_NO_START, I2C_FLAG_STOP, I2C_FLAG_READ_NO_ACK
63 */
64 if (msgs[i].flags & I2C_FLAG_READ) {
65 if (i2c_master_read(base, msgs[i].addr, msgs[i].buf, msgs[i].len) != status_success) {
66 HDF_LOGI("i2c read: Failed!!!\n");
67 return HDF_FAILURE;
68 }
69 } else {
70 if (i2c_master_write(base, msgs[i].addr, msgs[i].buf, msgs[i].len) != status_success) {
71 HDF_LOGI("i2c write: Failed!!!\n");
72 return HDF_FAILURE;
73 }
74 }
75 }
76
77 return 0;
78 }
79
hpmReceive(struct I2cCntlr * cntlr,struct I2cMsg * msgs,int16_t count)80 int32_t hpmReceive(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count)
81 {
82 hpm_stat_t stat;
83 struct HPMI2cDevice *hpmI2cDev = (struct HPMI2cDevice *)cntlr->priv;
84 I2C_Type *base = (I2C_Type *)hpmI2cDev->base;
85
86 for (int i = 0; i < count; i++) {
87 /*
88 * TODO: Another flags: I2C_FLAG_NO_START, I2C_FLAG_STOP, I2C_FLAG_READ_NO_ACK
89 */
90 if (msgs[i].flags & I2C_FLAG_READ) {
91 /* wait for address hit */
92 do {
93 stat = i2c_slave_read(base, msgs[i].buf, msgs[i].len);
94 } while (stat == status_fail);
95
96 if (stat != status_success) {
97 printf("Slave read failed\n");
98 while (1) {
99 }
100 }
101
102 /* wait for address hit */
103 do {
104 stat = i2c_slave_write(base, msgs[i].buf, msgs[i].len);
105 } while (stat == status_fail);
106
107 if (stat != status_success) {
108 printf("Slave write failed");
109 while (1) {
110 }
111 }
112 }
113 }
114
115 return 0;
116 }
117
118 static struct I2cMethod hpmI2cMethod = {
119 .transfer = hpmTransfer,
120 };
121
122
I2cDriverBind(struct HdfDeviceObject * device)123 static int32_t I2cDriverBind(struct HdfDeviceObject *device)
124 {
125 int32_t ret = HDF_SUCCESS;
126
127 HDF_LOGI("Bind: successed\n");
128 return ret;
129 }
130
I2cDriverInit(struct HdfDeviceObject * device)131 static int32_t I2cDriverInit(struct HdfDeviceObject *device)
132 {
133 int32_t ret = HDF_SUCCESS;
134
135 struct I2cCntlr *cntlr = (struct I2cCntlr *)OsalMemCalloc(
136 sizeof(struct I2cCntlr) + sizeof(struct HPMI2cDevice));
137 if (cntlr == NULL) {
138 ret = HDF_FAILURE;
139 HDF_LOGE("Init: cntlr alloc Failed!!!\n");
140 return ret;
141 }
142
143 struct HPMI2cDevice *hpmI2cDev = (struct HPMI2cDevice *)((char *)cntlr + sizeof(struct I2cCntlr));
144 cntlr->priv = hpmI2cDev;
145 device->priv = cntlr;
146
147 struct DeviceResourceIface *dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
148 if (dri == NULL) {
149 ret = HDF_FAILURE;
150 HDF_LOGE("Init: get DeviceResourceIface Failed!!!\n");
151 goto ERROR1;
152 }
153
154 dri->GetUint32(device->property, "id", &hpmI2cDev->id, 0);
155 dri->GetUint32(device->property, "base", &hpmI2cDev->base, 0);
156 dri->GetUint32(device->property, "clk_freq", &hpmI2cDev->clkFreq, 0);
157 HDF_LOGI("Init: hpmI2cDev->id: %u\n", hpmI2cDev->id);
158 HDF_LOGI("Init: hpmI2cDev->base: 0x%X\n", hpmI2cDev->base);
159 HDF_LOGI("Init: hpmI2cDev->clkFreq: %u\n", hpmI2cDev->clkFreq);
160
161 cntlr->busId = hpmI2cDev->id;
162 cntlr->ops = &hpmI2cMethod;
163 ret = I2cCntlrAdd(cntlr);
164 if (ret) {
165 HDF_LOGE("Init: I2cCntlrAdd Failed!!!\n");
166 ret = HDF_FAILURE;
167 goto ERROR1;
168 }
169
170 I2C_Type *base = (I2C_Type *)hpmI2cDev->base;
171 i2c_config_t i2cConfig;
172 i2cConfig.i2c_mode = i2c_mode_normal;
173 i2cConfig.is_10bit_addressing = 0;
174 i2c_init_master(base, hpmI2cDev->clkFreq, &i2cConfig);
175
176 return ret;
177 ERROR1:
178 OsalMemFree(cntlr);
179 return ret;
180 }
181
I2cDriverRelease(struct HdfDeviceObject * device)182 static void I2cDriverRelease(struct HdfDeviceObject *device)
183 {
184 if (device == NULL) {
185 return;
186 }
187
188 struct I2cCntlr *cntlr = (struct I2cCntlr *)device->priv;
189 if (cntlr == NULL) {
190 HDF_LOGE("Release: cntlr null\n");
191 return;
192 }
193
194 I2cCntlrRemove(cntlr);
195 OsalMemFree(cntlr);
196
197 HDF_LOGI("Release");
198 return;
199 }
200
201 struct HdfDriverEntry g_i2cDriverEntry = {
202 .moduleVersion = 1,
203 .moduleName = "HPMICRO_I2C_MODULE_HDF",
204 .Bind = I2cDriverBind,
205 .Init = I2cDriverInit,
206 .Release = I2cDriverRelease,
207 };
208
209 HDF_INIT(g_i2cDriverEntry);