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 "hdf_log.h"
22 #include "osal_mem.h"
23 #include "spi_core.h"
24 #include "spi_if.h"
25
26 #include "lz_hardware.h"
27
28 #define PRINT_ERR(fmt, args...) do { \
29 printf("%s, %d, error: "fmt, __func__, __LINE__, ##args); \
30 } while (0)
31
32 #define PRINT_WARR(fmt, args...) do { \
33 if (1) printf("%s, %d, warr: "fmt, __func__, __LINE__, ##args); \
34 } while (0)
35
36 #define PRINT_LOG(fmt, args...) do { \
37 if (1) printf("%s, %d, log: "fmt, __func__, __LINE__, ##args); \
38 } while (0)
39
40 struct spi_params {
41 uint32_t bus;
42 uint32_t id;
43 uint32_t func_mode;
44 uint32_t cs_gpio;
45 uint32_t cs_func;
46 uint32_t cs_type;
47 uint32_t cs_drv;
48 uint32_t cs_dir;
49 uint32_t cs_val;
50 uint32_t clk_gpio;
51 uint32_t clk_func;
52 uint32_t clk_type;
53 uint32_t clk_drv;
54 uint32_t clk_dir;
55 uint32_t clk_val;
56 uint32_t mosi_gpio;
57 uint32_t mosi_func;
58 uint32_t mosi_type;
59 uint32_t mosi_drv;
60 uint32_t mosi_dir;
61 uint32_t mosi_val;
62 uint32_t miso_gpio;
63 uint32_t miso_func;
64 uint32_t miso_type;
65 uint32_t miso_drv;
66 uint32_t miso_dir;
67 uint32_t miso_val;
68 uint32_t bitsPerWord;
69 uint32_t firstBit;
70 uint32_t mode;
71 uint32_t csm;
72 uint32_t speed;
73 uint32_t isSlave;
74 };
75
spidrv_readdrs(struct DeviceResourceNode * node,struct spi_params * params)76 static int32_t spidrv_readdrs(struct DeviceResourceNode *node, struct spi_params *params)
77 {
78 int32_t ret;
79 struct DeviceResourceIface *iface = NULL;
80
81 if (node == NULL) {
82 PRINT_ERR("%s: node is null\n", __func__);
83 return HDF_ERR_INVALID_PARAM;
84 }
85 if (params == NULL) {
86 PRINT_ERR("%s: params is null\n", __func__);
87 return HDF_ERR_INVALID_PARAM;
88 }
89
90 iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
91 if (iface == NULL) {
92 PRINT_ERR("%s: iface is null\n", __func__);
93 return HDF_ERR_INVALID_PARAM;
94 }
95 if (iface->GetUint32 == NULL) {
96 PRINT_ERR("%s: GetUint32 is null\n", __func__);
97 return HDF_ERR_INVALID_PARAM;
98 }
99
100 memset_s(params, sizeof(struct spi_params), 0, sizeof(struct spi_params));
101
102 ret = iface->GetUint32(node, "bus", ¶ms->bus, 0);
103 if (ret != HDF_SUCCESS) {
104 PRINT_ERR("%s: GetUint32(bus) failed\n", __func__);
105 return HDF_FAILURE;
106 }
107
108 ret = iface->GetUint32(node, "id", ¶ms->id, 0);
109 if (ret != HDF_SUCCESS) {
110 PRINT_ERR("%s: GetUint32(id) failed\n", __func__);
111 return HDF_FAILURE;
112 }
113
114 ret = iface->GetUint32(node, "func_mode", ¶ms->func_mode, 0);
115 if (ret != HDF_SUCCESS) {
116 PRINT_ERR("%s: GetUint32(func_mode) failed\n", __func__);
117 return HDF_FAILURE;
118 }
119
120 ret = iface->GetUint32(node, "cs_gpio", ¶ms->cs_gpio, 0);
121 if (ret != HDF_SUCCESS) {
122 PRINT_ERR("%s: GetUint32(cs_gpio) failed\n", __func__);
123 return HDF_FAILURE;
124 }
125
126 ret = iface->GetUint32(node, "cs_func", ¶ms->cs_func, 0);
127 if (ret != HDF_SUCCESS) {
128 PRINT_ERR("%s: GetUint32(cs_func) failed\n", __func__);
129 return HDF_FAILURE;
130 }
131
132 ret = iface->GetUint32(node, "cs_type", ¶ms->cs_type, 0);
133 if (ret != HDF_SUCCESS) {
134 PRINT_ERR("%s: GetUint32(cs_type) failed\n", __func__);
135 return HDF_FAILURE;
136 }
137
138 ret = iface->GetUint32(node, "cs_drv", ¶ms->cs_drv, 0);
139 if (ret != HDF_SUCCESS) {
140 PRINT_ERR("%s: GetUint32(cs_drv) failed\n", __func__);
141 return HDF_FAILURE;
142 }
143
144 ret = iface->GetUint32(node, "cs_dir", ¶ms->cs_dir, 0);
145 if (ret != HDF_SUCCESS) {
146 PRINT_ERR("%s: GetUint32(cs_dir) failed\n", __func__);
147 return HDF_FAILURE;
148 }
149
150 ret = iface->GetUint32(node, "cs_val", ¶ms->cs_val, 0);
151 if (ret != HDF_SUCCESS) {
152 PRINT_ERR("%s: GetUint32(cs_val) failed\n", __func__);
153 return HDF_FAILURE;
154 }
155
156 ret = iface->GetUint32(node, "clk_gpio", ¶ms->clk_gpio, 0);
157 if (ret != HDF_SUCCESS) {
158 PRINT_ERR("%s: GetUint32(clk_gpio) failed\n", __func__);
159 return HDF_FAILURE;
160 }
161
162 ret = iface->GetUint32(node, "clk_func", ¶ms->clk_func, 0);
163 if (ret != HDF_SUCCESS) {
164 PRINT_ERR("%s: GetUint32(clk_func) failed\n", __func__);
165 return HDF_FAILURE;
166 }
167
168 ret = iface->GetUint32(node, "clk_type", ¶ms->clk_type, 0);
169 if (ret != HDF_SUCCESS) {
170 PRINT_ERR("%s: GetUint32(clk_type) failed\n", __func__);
171 return HDF_FAILURE;
172 }
173
174 ret = iface->GetUint32(node, "clk_drv", ¶ms->clk_drv, 0);
175 if (ret != HDF_SUCCESS) {
176 PRINT_ERR("%s: GetUint32(clk_drv) failed\n", __func__);
177 return HDF_FAILURE;
178 }
179
180 ret = iface->GetUint32(node, "clk_dir", ¶ms->clk_dir, 0);
181 if (ret != HDF_SUCCESS) {
182 PRINT_ERR("%s: GetUint32(clk_dir) failed\n", __func__);
183 return HDF_FAILURE;
184 }
185
186 ret = iface->GetUint32(node, "clk_val", ¶ms->clk_val, 0);
187 if (ret != HDF_SUCCESS) {
188 PRINT_ERR("%s: GetUint32(clk_val) failed\n", __func__);
189 return HDF_FAILURE;
190 }
191
192 ret = iface->GetUint32(node, "mosi_gpio", ¶ms->mosi_gpio, 0);
193 if (ret != HDF_SUCCESS) {
194 PRINT_ERR("%s: GetUint32(mosi_gpio) failed\n", __func__);
195 return HDF_FAILURE;
196 }
197
198 ret = iface->GetUint32(node, "mosi_func", ¶ms->mosi_func, 0);
199 if (ret != HDF_SUCCESS) {
200 PRINT_ERR("%s: GetUint32(mosi_func) failed\n", __func__);
201 return HDF_FAILURE;
202 }
203
204 ret = iface->GetUint32(node, "mosi_type", ¶ms->mosi_type, 0);
205 if (ret != HDF_SUCCESS) {
206 PRINT_ERR("%s: GetUint32(mosi_type) failed\n", __func__);
207 return HDF_FAILURE;
208 }
209
210 ret = iface->GetUint32(node, "mosi_drv", ¶ms->mosi_drv, 0);
211 if (ret != HDF_SUCCESS) {
212 PRINT_ERR("%s: GetUint32(mosi_drv) failed\n", __func__);
213 return HDF_FAILURE;
214 }
215
216 ret = iface->GetUint32(node, "mosi_dir", ¶ms->mosi_dir, 0);
217 if (ret != HDF_SUCCESS) {
218 PRINT_ERR("%s: GetUint32(mosi_dir) failed\n", __func__);
219 return HDF_FAILURE;
220 }
221
222 ret = iface->GetUint32(node, "mosi_val", ¶ms->mosi_val, 0);
223 if (ret != HDF_SUCCESS) {
224 PRINT_ERR("%s: GetUint32(mosi_val) failed\n", __func__);
225 return HDF_FAILURE;
226 }
227
228 ret = iface->GetUint32(node, "miso_gpio", ¶ms->miso_gpio, 0);
229 if (ret != HDF_SUCCESS) {
230 PRINT_ERR("%s: GetUint32(miso_gpio) failed\n", __func__);
231 return HDF_FAILURE;
232 }
233
234 ret = iface->GetUint32(node, "miso_func", ¶ms->miso_func, 0);
235 if (ret != HDF_SUCCESS) {
236 PRINT_ERR("%s: GetUint32(miso_func) failed\n", __func__);
237 return HDF_FAILURE;
238 }
239
240 ret = iface->GetUint32(node, "miso_type", ¶ms->miso_type, 0);
241 if (ret != HDF_SUCCESS) {
242 PRINT_ERR("%s: GetUint32(miso_type) failed\n", __func__);
243 return HDF_FAILURE;
244 }
245
246 ret = iface->GetUint32(node, "miso_drv", ¶ms->miso_drv, 0);
247 if (ret != HDF_SUCCESS) {
248 PRINT_ERR("%s: GetUint32(miso_drv) failed\n", __func__);
249 return HDF_FAILURE;
250 }
251
252 ret = iface->GetUint32(node, "miso_dir", ¶ms->miso_dir, 0);
253 if (ret != HDF_SUCCESS) {
254 PRINT_ERR("%s: GetUint32(miso_dir) failed\n", __func__);
255 return HDF_FAILURE;
256 }
257
258 ret = iface->GetUint32(node, "miso_val", ¶ms->miso_val, 0);
259 if (ret != HDF_SUCCESS) {
260 PRINT_ERR("%s: GetUint32(miso_val) failed\n", __func__);
261 return HDF_FAILURE;
262 }
263
264 ret = iface->GetUint32(node, "bitsPerWord", ¶ms->bitsPerWord, 0);
265 if (ret != HDF_SUCCESS) {
266 PRINT_ERR("%s: GetUint32(bitsPerWord) failed\n", __func__);
267 return HDF_FAILURE;
268 }
269
270 ret = iface->GetUint32(node, "firstBit", ¶ms->firstBit, 0);
271 if (ret != HDF_SUCCESS) {
272 PRINT_ERR("%s: GetUint32(firstBit) failed\n", __func__);
273 return HDF_FAILURE;
274 }
275
276 ret = iface->GetUint32(node, "mode", ¶ms->mode, 0);
277 if (ret != HDF_SUCCESS) {
278 PRINT_ERR("%s: GetUint32(mode) failed\n", __func__);
279 return HDF_FAILURE;
280 }
281
282 ret = iface->GetUint32(node, "csm", ¶ms->csm, 0);
283 if (ret != HDF_SUCCESS) {
284 PRINT_ERR("%s: GetUint32(csm) failed\n", __func__);
285 return HDF_FAILURE;
286 }
287
288 ret = iface->GetUint32(node, "speed", ¶ms->speed, 0);
289 if (ret != HDF_SUCCESS) {
290 PRINT_ERR("%s: GetUint32(speed) failed\n", __func__);
291 return HDF_FAILURE;
292 }
293
294 ret = iface->GetUint32(node, "isSlave", ¶ms->isSlave, 0);
295 if (ret != HDF_SUCCESS) {
296 PRINT_ERR("%s: GetUint32(isSlave) failed\n", __func__);
297 return HDF_FAILURE;
298 }
299
300 return HDF_SUCCESS;
301 }
302
spidrv_initdevice(const struct spi_params * params)303 static int32_t spidrv_initdevice(const struct spi_params *params)
304 {
305 unsigned int ret;
306 SpiBusIo bus;
307 LzSpiConfig config;
308 unsigned int bus_id;
309
310 if (params == NULL) {
311 PRINT_ERR("%s: params is null\n", __func__);
312 return HDF_ERR_INVALID_PARAM;
313 }
314
315 bus_id = (unsigned int)params->bus;
316
317 memset_s(&bus, sizeof(bus), 0, sizeof(bus));
318 bus.id = (FuncID)params->id;
319 bus.mode = (FuncMode)params->func_mode;
320 bus.cs.gpio = (GpioID)params->cs_gpio;
321 bus.cs.func = (MuxFunc)params->cs_func;
322 bus.cs.type = (PullType)params->cs_type;
323 bus.cs.drv = (DriveLevel)params->cs_drv;
324 bus.cs.dir = (LzGpioDir)params->cs_dir;
325 bus.cs.val = (LzGpioValue)params->cs_val;
326 bus.mosi.gpio = (GpioID)params->mosi_gpio;
327 bus.mosi.func = (MuxFunc)params->mosi_func;
328 bus.mosi.type = (PullType)params->mosi_type;
329 bus.mosi.drv = (DriveLevel)params->mosi_drv;
330 bus.mosi.dir = (LzGpioDir)params->mosi_dir;
331 bus.mosi.val = (LzGpioValue)params->mosi_val;
332 bus.miso.gpio = (GpioID)params->miso_gpio;
333 bus.miso.func = (MuxFunc)params->miso_func;
334 bus.miso.type = (PullType)params->miso_type;
335 bus.miso.drv = (DriveLevel)params->miso_drv;
336 bus.miso.dir = (LzGpioDir)params->miso_dir;
337 bus.miso.val = (LzGpioValue)params->miso_val;
338
339 memset_s(&config, sizeof(config), 0, sizeof(config));
340 config.bitsPerWord = (unsigned int)params->bitsPerWord;
341 config.firstBit = (unsigned int)params->firstBit;
342 config.mode = (unsigned int)params->mode;
343 config.csm = (unsigned int)params->csm;
344 config.speed = (unsigned int)params->speed;
345 config.isSlave = (unsigned int)params->isSlave;
346
347 ret = SpiIoInit(bus);
348 if (ret != LZ_HARDWARE_SUCCESS) {
349 PRINT_ERR("%s: SpiIoInit failed(%u)\n", __func__, ret);
350 }
351
352 ret = LzSpiInit(bus_id, config);
353 if (ret != LZ_HARDWARE_SUCCESS) {
354 PRINT_ERR("%s: LzSpiInit failed(%u)\n", __func__, ret);
355 }
356
357 return HDF_SUCCESS;
358 }
359
spidrv_deinitdevice(const struct spi_params * params)360 static void spidrv_deinitdevice(const struct spi_params *params)
361 {
362 unsigned int bus_id;
363
364 if (params == NULL) {
365 PRINT_ERR("%s: params is null\n", __func__);
366 return;
367 }
368
369 bus_id = (unsigned int)params->bus;
370 LzSpiDeinit(bus_id);
371 }
372
spidrv_open(struct SpiCntlr * cntlr)373 static int32_t spidrv_open(struct SpiCntlr *cntlr)
374 {
375 int32_t ret;
376 struct spi_params *params = NULL;
377
378 if (cntlr == NULL) {
379 PRINT_ERR("%s: cntlr is null\n", __func__);
380 return HDF_ERR_INVALID_PARAM;
381 }
382 if (cntlr->priv == NULL) {
383 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
384 return HDF_ERR_INVALID_PARAM;
385 }
386
387 params = (struct spi_params *)cntlr->priv;
388
389 ret = spidrv_initdevice(params);
390 if (ret != HDF_SUCCESS) {
391 PRINT_ERR("%s: spidrv_initdevice error\n", __func__);
392 return HDF_FAILURE;
393 }
394
395 return HDF_SUCCESS;
396 }
397
spidrv_close(struct SpiCntlr * cntlr)398 static int32_t spidrv_close(struct SpiCntlr *cntlr)
399 {
400 struct spi_params *params = NULL;
401
402 if (cntlr == NULL) {
403 PRINT_ERR("%s: cntlr is null\n", __func__);
404 return HDF_ERR_INVALID_PARAM;
405 }
406 if (cntlr->priv == NULL) {
407 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
408 return HDF_ERR_INVALID_PARAM;
409 }
410
411 params = (struct spi_params *)cntlr->priv;
412
413 /* 销毁硬件设备 */
414 spidrv_deinitdevice(params);
415
416 return HDF_SUCCESS;
417 }
418
spidrv_setcfg(struct SpiCntlr * cntlr,struct SpiCfg * cfg)419 static int32_t spidrv_setcfg(struct SpiCntlr *cntlr, struct SpiCfg *cfg)
420 {
421 struct spi_params *params = NULL;
422
423 if (cntlr == NULL) {
424 PRINT_ERR("%s: cntlr is null\n", __func__);
425 return HDF_ERR_INVALID_PARAM;
426 }
427 if (cntlr->priv == NULL) {
428 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
429 return HDF_ERR_INVALID_PARAM;
430 }
431 if (cfg == NULL) {
432 PRINT_ERR("%s: cfg is null\n", __func__);
433 return HDF_ERR_INVALID_PARAM;
434 }
435
436 params = (struct spi_params *)cntlr->priv;
437
438 params->speed = cfg->maxSpeedHz;
439 params->bitsPerWord = cfg->bitsPerWord;
440
441 /* LiteOS的标准定义转化为LzHardware的定义 */
442 params->mode = 0;
443 if (cfg->mode & SPI_CLK_PHASE) {
444 params->mode |= SPI_CPHA;
445 }
446 if (cfg->mode & SPI_CLK_POLARITY) {
447 params->mode |= SPI_CPOL;
448 }
449 if (cfg->mode & SPI_MODE_3WIRE) {
450 PRINT_ERR("%s: SPI_MODE_3WIRE is not support!\n", __func__);
451 }
452 if (cfg->mode & SPI_MODE_LOOP) {
453 PRINT_ERR("%s: SPI_MODE_3WIRE is not support!\n", __func__);
454 }
455 if ((cfg->mode & SPI_MODE_LSBFE) == 0) {
456 params->mode |= SPI_MSB;
457 }
458 if (cfg->mode & SPI_MODE_NOCS) {
459 PRINT_ERR("%s: SPI_MODE_NOCS is not support!\n", __func__);
460 }
461 if (cfg->mode & SPI_MODE_CS_HIGH) {
462 PRINT_ERR("%s: SPI_MODE_CS_HIGH is not support!\n", __func__);
463 }
464 if (cfg->mode & SPI_MODE_READY) {
465 PRINT_ERR("%s: SPI_MODE_READY is not support!\n", __func__);
466 }
467 if (params->isSlave == 0) {
468 params->mode |= SPI_SLAVE;
469 } else {
470 params->mode &= ~(SPI_SLAVE);
471 }
472
473 switch (cfg->transferMode) {
474 case SPI_INTERRUPT_TRANSFER:
475 break;
476
477 case SPI_POLLING_TRANSFER:
478 PRINT_ERR("%s: SPI_POLLING_TRANSFER is not support!\n", __func__);
479 break;
480
481 case SPI_DMA_TRANSFER:
482 PRINT_ERR("%s: SPI_DMA_TRANSFER is not support!\n", __func__);
483 break;
484
485 default:
486 PRINT_ERR("%s: %d is not support!\n", __func__, cfg->transferMode);
487 break;
488 }
489
490 spidrv_initdevice(params);
491
492 return HDF_SUCCESS;
493 }
494
spidrv_getcfg(struct SpiCntlr * cntlr,struct SpiCfg * cfg)495 static int32_t spidrv_getcfg(struct SpiCntlr *cntlr, struct SpiCfg *cfg)
496 {
497 struct spi_params *params = NULL;
498
499 if (cntlr == NULL) {
500 PRINT_ERR("%s: cntlr is null\n", __func__);
501 return HDF_ERR_INVALID_PARAM;
502 }
503 if (cntlr->priv == NULL) {
504 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
505 return HDF_ERR_INVALID_PARAM;
506 }
507 if (cfg == NULL) {
508 PRINT_ERR("%s: cfg is null\n", __func__);
509 return HDF_ERR_INVALID_PARAM;
510 }
511
512 params = (struct spi_params *)cntlr->priv;
513
514 cfg->maxSpeedHz = params->speed;
515 cfg->bitsPerWord = params->bitsPerWord;
516
517 /* LzHardware的定义转化为LiteOS的标准定义 */
518 cfg->mode = 0;
519 if (params->mode & SPI_CPHA) {
520 cfg->mode |= SPI_CLK_PHASE;
521 }
522 if (params->mode & SPI_CPOL) {
523 cfg->mode |= SPI_CLK_POLARITY;
524 }
525 if ((params->mode & SPI_MSB) == 0) {
526 cfg->mode |= SPI_MODE_LSBFE;
527 }
528
529 cfg->transferMode = SPI_INTERRUPT_TRANSFER;
530
531 return HDF_SUCCESS;
532 }
533
spidrv_transfer(struct SpiCntlr * cntlr,struct SpiMsg * msgs,uint32_t count)534 static int32_t spidrv_transfer(struct SpiCntlr *cntlr, struct SpiMsg *msgs, uint32_t count)
535 {
536 unsigned int ret;
537 struct spi_params *params = NULL;
538 unsigned int bus_id;
539 uint32_t i;
540 struct SpiMsg *msg;
541 LzSpiMsg lz_spi_msg;
542
543 if (cntlr == NULL) {
544 PRINT_ERR("%s: cntlr is null\n", __func__);
545 return HDF_ERR_INVALID_PARAM;
546 }
547 if (cntlr->priv == NULL) {
548 PRINT_ERR("%s: cntlr->priv is null\n", __func__);
549 return HDF_ERR_INVALID_PARAM;
550 }
551 if (msgs == NULL) {
552 PRINT_ERR("%s: msgs is null\n", __func__);
553 return HDF_ERR_INVALID_PARAM;
554 }
555
556 params = (struct spi_params *)cntlr->priv;
557 bus_id = (unsigned int)params->bus;
558
559 for (i = 0; i < count; i++) {
560 msg = &msgs[i];
561
562 if ((msg->wbuf != NULL) && (msg->rbuf != NULL)) {
563 ret = LzSpiWriteAndRead(bus_id, 0, msg->wbuf, msg->rbuf, msg->len);
564 if (ret != LZ_HARDWARE_SUCCESS) {
565 PRINT_ERR("%s: LzSpiWriteAndRead error\n", __func__);
566 return HDF_ERR_IO;
567 }
568 } else if ((msg->wbuf != NULL) && (msg->rbuf == NULL)) {
569 ret = LzSpiWrite(bus_id, 0, msg->wbuf, msg->len);
570 if (ret != LZ_HARDWARE_SUCCESS) {
571 PRINT_ERR("%s: LzSpiWrite error\n", __func__);
572 return HDF_ERR_IO;
573 }
574 } else if ((msg->wbuf == NULL) && (msg->rbuf != NULL)) {
575 ret = LzSpiRead(bus_id, 0, msg->rbuf, msg->len);
576 if (ret != LZ_HARDWARE_SUCCESS) {
577 PRINT_ERR("%s: LzSpiRead error\n", __func__);
578 return HDF_ERR_IO;
579 }
580 } else {
581 PRINT_ERR("%s: i = %d, msg->wbuf or msg->rbuf is null\n", __func__, i);
582 return HDF_ERR_INVALID_PARAM;
583 }
584 }
585
586 return HDF_SUCCESS;
587 }
588
589 static struct SpiCntlrMethod m_spi_method = {
590 .Open = spidrv_open,
591 .Close = spidrv_close,
592 .SetCfg = spidrv_setcfg,
593 .GetCfg = spidrv_getcfg,
594 .Transfer = spidrv_transfer,
595 };
596
spidrv_bind(struct HdfDeviceObject * device)597 static int32_t spidrv_bind(struct HdfDeviceObject *device)
598 {
599 return HDF_SUCCESS;
600 }
601
spidrv_init(struct HdfDeviceObject * device)602 static int32_t spidrv_init(struct HdfDeviceObject *device)
603 {
604 int32_t ret;
605 struct SpiCntlr *cntlr = NULL;
606 struct spi_params *params = NULL;
607
608 HDF_LOGI("%s: Enter", __func__);
609 if ((device == NULL) || (device->property == NULL)) {
610 PRINT_ERR("%s: device or property is null\n", __func__);
611 return HDF_ERR_INVALID_OBJECT;
612 }
613
614 cntlr = (struct SpiCntlr *)OsalMemAlloc(sizeof(struct SpiCntlr));
615 params = (struct spi_params *)OsalMemAlloc(sizeof(struct spi_params));
616
617 if (cntlr == NULL) {
618 PRINT_ERR("%s: cntlr is null\n", __func__);
619 if (cntlr != NULL) {
620 OsalMemFree(cntlr);
621 cntlr = NULL;
622 }
623 if (params != NULL) {
624 OsalMemFree(params);
625 params = NULL;
626 }
627 return HDF_ERR_MALLOC_FAIL;
628 }
629 if (params == NULL) {
630 PRINT_ERR("%s: params is null\n", __func__);
631 if (cntlr != NULL) {
632 OsalMemFree(cntlr);
633 cntlr = NULL;
634 }
635 if (params != NULL) {
636 OsalMemFree(params);
637 params = NULL;
638 }
639 return HDF_ERR_MALLOC_FAIL;
640 }
641
642 ret = spidrv_readdrs(device->property, params);
643 if (ret != HDF_SUCCESS) {
644 PRINT_ERR("%s: cntlr is null\n", __func__);
645 if (cntlr != NULL) {
646 OsalMemFree(cntlr);
647 cntlr = NULL;
648 }
649 if (params != NULL) {
650 OsalMemFree(params);
651 params = NULL;
652 }
653 return ret;
654 }
655
656 /* 数据对齐 */
657 cntlr->busNum = params->bus;
658 cntlr->numCs = 1;
659 cntlr->curCs = 0;
660 cntlr->priv = (void *)params;
661 /* 注册回调函数,并初始化SpiCntlr */
662 cntlr->method = &m_spi_method;
663 device->service = &cntlr->service;
664 cntlr->device = device;
665 cntlr->priv = (void *)params;
666
667 PRINT_LOG("spi service: %s init success!\n", HdfDeviceGetServiceName(device));
668 return HDF_SUCCESS;
669 }
670
spidrv_release(struct HdfDeviceObject * device)671 static void spidrv_release(struct HdfDeviceObject *device)
672 {
673 struct SpiCntlr *cntlr = NULL;
674 struct spi_params *params = NULL;
675
676 HDF_LOGI("%s: Enter", __func__);
677 if ((device == NULL) || (device->property == NULL)) {
678 PRINT_ERR("%s: device or property is null\n", __func__);
679 return;
680 }
681
682 cntlr = SpiCntlrFromDevice(device);
683 if (cntlr == NULL) {
684 PRINT_ERR("%s: cntlr is null\n", __func__);
685 return;
686 }
687
688 params = (struct spi_params *)cntlr->priv;
689
690 /* 销毁SPI参数 */
691 if (params != NULL) {
692 OsalMemFree(params);
693 params = NULL;
694 }
695
696 /* 销毁SpiCntlr */
697 cntlr->priv = NULL;
698 if (cntlr != NULL) {
699 OsalMemFree(cntlr);
700 cntlr = NULL;
701 }
702
703 PRINT_LOG("spi service: %s release!\n", HdfDeviceGetServiceName(device));
704 }
705
706 struct HdfDriverEntry g_spiDriverEntry = {
707 .moduleVersion = 1,
708 .moduleName = "HDF_PLATFORM_SPI",
709 .Init = spidrv_init,
710 .Release = spidrv_release,
711 .Bind = spidrv_bind,
712 };
713
714 HDF_INIT(g_spiDriverEntry);
715