1 /*
2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
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 #include <stdio.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18
19 #include "hdf_device_desc.h"
20 #include "device_resource_if.h"
21 #include "osal_mem.h"
22 #include "i2c_core.h"
23 #include "i2c_if.h"
24
25 #include "lz_hardware.h"
26
27 #define PRINT_ERR(fmt, args...) do { \
28 printf("%s, %d, error: "fmt, __func__, __LINE__, ##args); \
29 } while (0)
30
31 #define PRINT_WARR(fmt, args...) do { \
32 if (1) printf("%s, %d, warr: "fmt, __func__, __LINE__, ##args); \
33 } while (0)
34
35 #define PRINT_LOG(fmt, args...) do { \
36 if (1) printf("%s, %d, log: "fmt, __func__, __LINE__, ##args); \
37 } while (0)
38
39 struct i2c_bus {
40 uint32_t bus;
41 uint32_t id;
42 uint32_t mode;
43 uint32_t freq;
44 uint32_t scl_gpio;
45 uint32_t scl_func;
46 uint32_t scl_type;
47 uint32_t scl_drv;
48 uint32_t scl_dir;
49 uint32_t scl_val;
50 uint32_t scl_mux;
51 uint32_t sda_gpio;
52 uint32_t sda_func;
53 uint32_t sda_type;
54 uint32_t sda_drv;
55 uint32_t sda_dir;
56 uint32_t sda_val;
57 uint32_t sda_mux;
58 };
59
i2cdrv_initdevice(const struct i2c_bus * bus)60 static int32_t i2cdrv_initdevice(const struct i2c_bus *bus)
61 {
62 unsigned int ret;
63 I2cBusIo i2cBus;
64 unsigned int i2cBusId;
65 unsigned int i2cFreq;
66 GpioID sclGpio, sdaGpio;
67 MuxFunc sclMux, sdaMux;
68
69 if (bus == NULL) {
70 PRINT_ERR("%s: bus is null\n", __func__);
71 return HDF_ERR_INVALID_PARAM;
72 }
73
74 i2cBusId = (unsigned int)(bus->bus);
75 i2cFreq = (unsigned int)(bus->freq);
76 /* 初始化i2c配置结构体 */
77 i2cBus.id = (FuncID)(bus->id);
78 i2cBus.mode = (FuncMode)(bus->mode);
79 i2cBus.scl.gpio = (GpioID)(bus->scl_gpio);
80 i2cBus.scl.func = (MuxFunc)(bus->scl_func);
81 i2cBus.scl.type = (PullType)(bus->scl_type);
82 i2cBus.scl.drv = (DriveLevel)(bus->scl_drv);
83 i2cBus.scl.dir = (LzGpioDir)(bus->scl_dir);
84 i2cBus.scl.val = (LzGpioValue)(bus->scl_val);
85 i2cBus.sda.gpio = (GpioID)(bus->sda_gpio);
86 i2cBus.sda.func = (MuxFunc)(bus->sda_func);
87 i2cBus.sda.type = (PullType)(bus->sda_type);
88 i2cBus.sda.drv = (DriveLevel)(bus->sda_drv);
89 i2cBus.sda.dir = (LzGpioDir)(bus->sda_dir);
90 i2cBus.sda.val = (LzGpioValue)(bus->sda_val);
91 /* 初始化引脚复用寄存器 */
92 sclGpio = (GpioID)(bus->scl_gpio);
93 sclMux = (MuxFunc)(bus->scl_mux);
94 sdaGpio = (GpioID)(bus->sda_gpio);
95 sdaMux = (MuxFunc)(bus->sda_mux);
96
97 ret = I2cIoInit(i2cBus);
98 if (ret != LZ_HARDWARE_SUCCESS) {
99 PRINT_ERR("%s: I2cIoInit failed(%u)\n", __func__, ret);
100 return HDF_FAILURE;
101 }
102
103 ret = LzI2cInit(i2cBusId, i2cFreq);
104 if (ret != LZ_HARDWARE_SUCCESS) {
105 PRINT_ERR("%s: LzI2cInit failed(%u)\n", __func__, ret);
106 return HDF_FAILURE;
107 }
108
109 PinctrlSet(sclGpio, sclMux, PULL_KEEP, DRIVE_KEEP);
110 PinctrlSet(sdaGpio, sdaMux, PULL_KEEP, DRIVE_KEEP);
111
112 return HDF_SUCCESS;
113 }
114
i2cdrv_deinitdevice(const struct i2c_bus * bus)115 static void i2cdrv_deinitdevice(const struct i2c_bus *bus)
116 {
117 unsigned int i2cBusId;
118
119 if (bus == NULL) {
120 PRINT_ERR("%s: bus is null\n", __func__);
121 return;
122 }
123
124 i2cBusId = (unsigned int)(bus->bus);
125 LzI2cDeinit(i2cBusId);
126 }
127
i2cdrv_readdrs(struct DeviceResourceNode * node,struct i2c_bus * bus)128 static int32_t i2cdrv_readdrs(struct DeviceResourceNode *node, struct i2c_bus *bus)
129 {
130 int32_t ret;
131 struct DeviceResourceIface *iface = NULL;
132
133 if (node == NULL) {
134 PRINT_ERR("%s: node is null\n", __func__);
135 return HDF_ERR_INVALID_PARAM;
136 }
137 if (bus == NULL) {
138 PRINT_ERR("%s: bus is null\n", __func__);
139 return HDF_ERR_INVALID_PARAM;
140 }
141
142 iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
143 if (iface == NULL) {
144 PRINT_ERR("%s: iface is null\n", __func__);
145 return HDF_ERR_INVALID_PARAM;
146 }
147 if (iface->GetUint32 == NULL) {
148 PRINT_ERR("%s: GetUint32 is null\n", __func__);
149 return HDF_ERR_INVALID_PARAM;
150 }
151
152 memset_s(bus, sizeof(struct i2c_bus), 0, sizeof(struct i2c_bus));
153
154 ret = iface->GetUint32(node, "bus", &bus->bus, 0);
155 if (ret != HDF_SUCCESS) {
156 PRINT_ERR("%s: GetUint32(bus) failed\n", __func__);
157 return HDF_FAILURE;
158 }
159
160 ret = iface->GetUint32(node, "id", &bus->id, 0);
161 if (ret != HDF_SUCCESS) {
162 PRINT_ERR("%s: GetUint32(id) failed\n", __func__);
163 return HDF_FAILURE;
164 }
165
166 ret = iface->GetUint32(node, "mode", &bus->mode, 0);
167 if (ret != HDF_SUCCESS) {
168 PRINT_ERR("%s: GetUint32(mode) failed\n", __func__);
169 return HDF_FAILURE;
170 }
171
172 ret = iface->GetUint32(node, "freq", &bus->freq, 0);
173 if (ret != HDF_SUCCESS) {
174 PRINT_ERR("%s: GetUint32(freq) failed\n", __func__);
175 return HDF_FAILURE;
176 }
177
178 ret = iface->GetUint32(node, "scl_gpio", &bus->scl_gpio, 0);
179 if (ret != HDF_SUCCESS) {
180 PRINT_ERR("%s: GetUint32(scl_gpio) failed\n", __func__);
181 return HDF_FAILURE;
182 }
183
184 ret = iface->GetUint32(node, "scl_func", &bus->scl_func, 0);
185 if (ret != HDF_SUCCESS) {
186 PRINT_ERR("%s: GetUint32(scl_func) failed\n", __func__);
187 return HDF_FAILURE;
188 }
189
190 ret = iface->GetUint32(node, "scl_type", &bus->scl_type, 0);
191 if (ret != HDF_SUCCESS) {
192 PRINT_ERR("%s: GetUint32(scl_type) failed\n", __func__);
193 return HDF_FAILURE;
194 }
195
196 ret = iface->GetUint32(node, "scl_drv", &bus->scl_drv, 0);
197 if (ret != HDF_SUCCESS) {
198 PRINT_ERR("%s: GetUint32(scl_drv) failed\n", __func__);
199 return HDF_FAILURE;
200 }
201
202 ret = iface->GetUint32(node, "scl_dir", &bus->scl_dir, 0);
203 if (ret != HDF_SUCCESS) {
204 PRINT_ERR("%s: GetUint32(scl_dir) failed\n", __func__);
205 return HDF_FAILURE;
206 }
207
208 ret = iface->GetUint32(node, "scl_val", &bus->scl_val, 0);
209 if (ret != HDF_SUCCESS) {
210 PRINT_ERR("%s: GetUint32(scl_val) failed\n", __func__);
211 return HDF_FAILURE;
212 }
213
214 ret = iface->GetUint32(node, "scl_mux", &bus->scl_mux, 0);
215 if (ret != HDF_SUCCESS) {
216 PRINT_ERR("%s: GetUint32(scl_mux) failed\n", __func__);
217 return HDF_FAILURE;
218 }
219
220 ret = iface->GetUint32(node, "sda_gpio", &bus->sda_gpio, 0);
221 if (ret != HDF_SUCCESS) {
222 PRINT_ERR("%s: GetUint32(sda_gpio) failed\n", __func__);
223 return HDF_FAILURE;
224 }
225
226 ret = iface->GetUint32(node, "sda_func", &bus->sda_func, 0);
227 if (ret != HDF_SUCCESS) {
228 PRINT_ERR("%s: GetUint32(sda_func) failed\n", __func__);
229 return HDF_FAILURE;
230 }
231
232 ret = iface->GetUint32(node, "sda_type", &bus->sda_type, 0);
233 if (ret != HDF_SUCCESS) {
234 PRINT_ERR("%s: GetUint32(sda_type) failed\n", __func__);
235 return HDF_FAILURE;
236 }
237
238 ret = iface->GetUint32(node, "sda_drv", &bus->sda_drv, 0);
239 if (ret != HDF_SUCCESS) {
240 PRINT_ERR("%s: GetUint32(sda_drv) failed\n", __func__);
241 return HDF_FAILURE;
242 }
243
244 ret = iface->GetUint32(node, "sda_dir", &bus->sda_dir, 0);
245 if (ret != HDF_SUCCESS) {
246 PRINT_ERR("%s: GetUint32(sda_dir) failed\n", __func__);
247 return HDF_FAILURE;
248 }
249
250 ret = iface->GetUint32(node, "sda_val", &bus->sda_val, 0);
251 if (ret != HDF_SUCCESS) {
252 PRINT_ERR("%s: GetUint32(sda_val) failed\n", __func__);
253 return HDF_FAILURE;
254 }
255
256 ret = iface->GetUint32(node, "sda_mux", &bus->sda_mux, 0);
257 if (ret != HDF_SUCCESS) {
258 PRINT_ERR("%s: GetUint32(sda_mux) failed\n", __func__);
259 return HDF_FAILURE;
260 }
261
262 return HDF_SUCCESS;
263 }
264
i2cdrv_transfer(struct I2cCntlr * cntlr,struct I2cMsg * msgs,int16_t count)265 static int32_t i2cdrv_transfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count)
266 {
267 #define I2C_MSGS_MAX 2 // LzI2cMsgs最大数组个数
268 int16_t i;
269 unsigned int ret;
270 unsigned int i2cBusId;
271 struct i2c_bus *bus;
272 struct I2cMsg *msg, *msg2;
273 unsigned short addr;
274 unsigned char *buf;
275 unsigned short buf_len;
276 LzI2cMsg lzI2cMsgs[I2C_MSGS_MAX];
277
278 if (cntlr == NULL) {
279 PRINT_ERR("%s: cntlr is null\n", __func__);
280 return HDF_ERR_INVALID_PARAM;
281 }
282 if (cntlr->priv == NULL) {
283 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
284 return HDF_ERR_INVALID_PARAM;
285 }
286 if (msgs == NULL) {
287 PRINT_ERR("%s: msgs is null\n", __func__);
288 return HDF_ERR_INVALID_PARAM;
289 }
290
291 bus = (struct i2c_bus *)cntlr->priv;
292 i2cBusId = (unsigned int)(bus->bus);
293
294 for (i = 0; i < count; i++) {
295 msg = &msgs[i];
296
297 if (msg->flags == I2C_FLAG_READ) {
298 addr = (unsigned short)(msg->addr);
299 *buf = (unsigned char *)(msg->buf);
300 buf_len = (unsigned short)(msg->len);
301
302 ret = LzI2cRead(i2cBusId, addr, buf, buf_len);
303 if (ret != LZ_HARDWARE_SUCCESS) {
304 PRINT_ERR("%s: LzI2cRead failed(%u)\n", __func__, ret);
305 return i;
306 }
307 } else if (msg->flags == I2C_FLAG_STOP) {
308 /* 本字符串是写,下一个字符串是读,一起操作 */
309 i++;
310 msg2 = &msgs[i];
311
312 lzI2cMsgs[0].addr = (unsigned short)(msg->addr);
313 lzI2cMsgs[0].flags = (unsigned short)(msg->flags);
314 lzI2cMsgs[0].len = (unsigned short)(msg->len);
315 lzI2cMsgs[0].buf = (unsigned char *)(msg->buf);
316
317 lzI2cMsgs[1].addr = (unsigned short)(msg2->addr);
318 lzI2cMsgs[1].flags = (unsigned short)(msg2->flags);
319 lzI2cMsgs[1].len = (unsigned short)(msg2->len);
320 lzI2cMsgs[1].buf = (unsigned char *)(msg2->buf);
321
322 ret = LzI2cTransfer(i2cBusId, &lzI2cMsgs[0], I2C_MSGS_MAX);
323 if (ret != LZ_HARDWARE_SUCCESS) {
324 PRINT_ERR("%s: LzI2cTransfer failed(%u)\n", __func__, ret);
325 return i;
326 }
327 } else {
328 addr = (unsigned short)(msg->addr);
329 *buf = (unsigned char *)(msg->buf);
330 buf_len = (unsigned short)(msg->len);
331
332 ret = LzI2cWrite(i2cBusId, addr, buf, buf_len);
333 if (ret != LZ_HARDWARE_SUCCESS) {
334 PRINT_ERR("%s: LzI2cWrite failed(%u)\n", __func__, ret);
335 return i;
336 }
337 }
338 }
339
340 return i;
341 }
342
343 static struct I2cMethod m_i2c_method = {
344 .transfer = i2cdrv_transfer,
345 };
346
i2cdrv_init(struct HdfDeviceObject * device)347 static int32_t i2cdrv_init(struct HdfDeviceObject *device)
348 {
349 int32_t ret;
350 struct I2cCntlr *cntlr = NULL;
351 struct i2c_bus *bus = NULL;
352
353 PLAT_LOGI("%s: Enter", __func__);
354 if ((device == NULL) || (device->property == NULL)) {
355 PRINT_ERR("%s: device or property is null\n", __func__);
356 return HDF_ERR_INVALID_OBJECT;
357 }
358
359 cntlr = (struct I2cCntlr *)OsalMemAlloc(sizeof(struct I2cCntlr));
360 bus = (struct i2c_bus *)OsalMemAlloc(sizeof(struct i2c_bus));
361
362 if (cntlr == NULL) {
363 PRINT_ERR("%s: cntlr is null\n", __func__);
364 if (bus != NULL) {
365 OsalMemFree(bus);
366 bus = NULL;
367 }
368 if (cntlr != NULL) {
369 OsalMemFree(cntlr);
370 cntlr = NULL;
371 }
372 return HDF_ERR_MALLOC_FAIL;
373 }
374 if (bus == NULL) {
375 PRINT_ERR("%s: bus is null\n", __func__);
376 if (bus != NULL) {
377 OsalMemFree(bus);
378 bus = NULL;
379 }
380 if (cntlr != NULL) {
381 OsalMemFree(cntlr);
382 cntlr = NULL;
383 }
384 return HDF_ERR_MALLOC_FAIL;
385 }
386
387 memset_s(cntlr, sizeof(struct I2cCntlr), 0, sizeof(struct I2cCntlr));
388 cntlr->ops = &m_i2c_method;
389 device->priv = (void *)cntlr;
390
391 ret = i2cdrv_readdrs(device->property, bus);
392 if (ret != HDF_SUCCESS) {
393 PRINT_ERR("%s: i2cdrv_readdrs error\n", __func__);
394 cntlr->priv = NULL;
395 cntlr->ops = NULL;
396 device->priv = NULL;
397 if (bus != NULL) {
398 OsalMemFree(bus);
399 bus = NULL;
400 }
401 if (cntlr != NULL) {
402 OsalMemFree(cntlr);
403 cntlr = NULL;
404 }
405 return ret;
406 }
407
408 ret = i2cdrv_initdevice(bus);
409 if (ret != HDF_SUCCESS) {
410 PRINT_ERR("%s: i2cdrv_readdrs error\n", __func__);
411 i2cdrv_deinitdevice(bus);
412 cntlr->priv = NULL;
413 cntlr->ops = NULL;
414 device->priv = NULL;
415 if (bus != NULL) {
416 OsalMemFree(bus);
417 bus = NULL;
418 }
419 if (cntlr != NULL) {
420 OsalMemFree(cntlr);
421 cntlr = NULL;
422 }
423 return ret;
424 }
425
426 cntlr->priv = (void *)bus;
427
428 ret = I2cCntlrAdd(cntlr);
429 if (ret != HDF_SUCCESS) {
430 PRINT_ERR("%s: i2c cntlr add failed\n", __func__);
431 I2cCntlrRemove(cntlr);
432 i2cdrv_deinitdevice(bus);
433 cntlr->priv = NULL;
434 cntlr->ops = NULL;
435 device->priv = NULL;
436 if (bus != NULL) {
437 OsalMemFree(bus);
438 bus = NULL;
439 }
440 if (cntlr != NULL) {
441 OsalMemFree(cntlr);
442 cntlr = NULL;
443 }
444 return ret;
445 }
446
447 PRINT_LOG("i2c service: %s init success!\n", HdfDeviceGetServiceName(device));
448 return HDF_SUCCESS;
449 }
450
i2cdrv_release(struct HdfDeviceObject * device)451 static void i2cdrv_release(struct HdfDeviceObject *device)
452 {
453 struct I2cCntlr *cntlr = NULL;
454 struct i2c_bus *bus = NULL;
455
456 if (device == NULL) {
457 PRINT_ERR("%s: device is null\n", __func__);
458 return;
459 }
460
461 cntlr = (struct I2cCntlr *)device->priv;
462 if (cntlr == NULL) {
463 PRINT_ERR("%s: cntlr is null\n", __func__);
464 return;
465 }
466
467 bus = (struct i2c_bus *)cntlr->priv;
468 if (bus == NULL) {
469 PRINT_ERR("%s: bus is null\n", __func__);
470 return;
471 }
472
473 I2cCntlrRemove(cntlr);
474 i2cdrv_deinitdevice(bus);
475 cntlr->priv = NULL;
476 cntlr->ops = NULL;
477 device->priv = NULL;
478 if (bus != NULL) {
479 OsalMemFree(bus);
480 bus = NULL;
481 }
482 if (cntlr != NULL) {
483 OsalMemFree(cntlr);
484 cntlr = NULL;
485 }
486
487 PRINT_LOG("i2c service: %s release\n", HdfDeviceGetServiceName(device));
488 }
489
i2cdrv_bind(struct HdfDeviceObject * device)490 static int32_t i2cdrv_bind(struct HdfDeviceObject *device)
491 {
492 return HDF_SUCCESS;
493 }
494
495 struct HdfDriverEntry g_i2cDriverEntry = {
496 .moduleVersion = 1,
497 .moduleName = "HDF_PLATFORM_I2C",
498 .Init = i2cdrv_init,
499 .Release = i2cdrv_release,
500 .Bind = i2cdrv_bind,
501 };
502
503 HDF_INIT(g_i2cDriverEntry);
504