1 /*
2  * Copyright (C) 2017-2020 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv/common.h
7  * @brief    Header File for Common Driver
8  * @version  V1.0
9  * @date     31. March 2020
10  * @model    common
11  ******************************************************************************/
12 
13 #ifndef _DRV_COMMON_H_
14 #define _DRV_COMMON_H_
15 
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdbool.h>
20 #include <drv/list.h>
21 #include <drv/dev_tag.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #ifdef CONFIG_DEBUG_MODE
28 #define CSI_ASSERT(expr)                            \
29         do {                                        \
30             if ((long)expr == (long)NULL) {   \
31                 printf("PROGRAM ASSERT\n");         \
32                 while(1);                           \
33             }                                       \
34         } while(0);
35 #else
36 #define CSI_ASSERT(expr)        ((void)0U)
37 #endif
38 
39 #ifdef CONFIG_PARAM_NOT_CHECK
40 #define CSI_PARAM_CHK(para, err)                    \
41     do {                                            \
42         if ((int32_t)para == (int32_t)NULL) {       \
43             return (err);                           \
44         }                                           \
45     } while (0)
46 
47 #define CSI_PARAM_CHK_NORETVAL(para)                \
48     do {                                            \
49         if ((int32_t)para == (int32_t)NULL) {       \
50             return;                                 \
51         }                                           \
52     } while (0)
53 #else
54 #define CSI_PARAM_CHK(para, err)
55 #define CSI_PARAM_CHK_NORETVAL(para)
56 #endif
57 
58 typedef enum {
59     CSI_OK          =  0,
60     CSI_ERROR       = -1,
61     CSI_BUSY        = -2,
62     CSI_TIMEOUT     = -3,
63     CSI_UNSUPPORTED = -4
64 } csi_error_t;
65 
66 typedef struct {
67    uint8_t    readable;
68    uint8_t    writeable;
69    uint8_t    error;
70 } csi_state_t;
71 
72 typedef struct csi_dev csi_dev_t;
73 
74 #ifdef CONFIG_PM
75 typedef enum {
76     PM_DEV_SUSPEND,
77     PM_DEV_RESUME,
78 } csi_pm_dev_action_t;
79 
80 typedef enum {
81     PM_MODE_RUN                  = 0,   ///< Running mode
82     PM_MODE_SLEEP_1,                    ///< Sleep LV1 mode
83     PM_MODE_SLEEP_2,                    ///< Sleep LV2 mode
84     PM_MODE_DEEP_SLEEP_1,               ///< Deep sleep LV1 mode
85     PM_MODE_DEEP_SLEEP_2,               ///< Deep sleep LV2 mode
86 } csi_pm_mode_t;
87 
88 typedef struct {
89     slist_t     next;
90     csi_error_t (*pm_action)(csi_dev_t *dev, csi_pm_dev_action_t action);
91     uint32_t    *reten_mem;
92     uint32_t    size;
93 } csi_pm_dev_t;
94 #include <drv/pm.h>
95 #endif
96 
97 struct csi_dev {
98     unsigned long reg_base;
99     uint8_t       irq_num;
100     uint8_t       idx;
101     uint16_t      dev_tag;
102     void          (*irq_handler)(void *);
103 #ifdef CONFIG_PM
104     csi_pm_dev_t pm_dev;
105 #endif
106 };
107 
108 #define HANDLE_REG_BASE(handle)     (handle->dev.reg_base)
109 #define HANDLE_IRQ_NUM(handle)      (handle->dev.irq_num)
110 #define HANDLE_DEV_IDX(handle)      (handle->dev.idx)
111 #define HANDLE_IRQ_HANDLER(handle)  (handle->dev.irq_handler)
112 
113 typedef struct {
114     unsigned long    reg_base;
115     uint8_t          irq_num;
116     uint8_t          idx;
117     uint16_t         dev_tag;
118 } csi_perip_info_t;
119 
120 csi_error_t target_get(csi_dev_tag_t dev_tag, uint32_t idx, csi_dev_t *dev);
121 csi_error_t target_get_optimal_dma_channel(void *dma_list, uint32_t ctrl_num, csi_dev_t *parent_dev, void *ch_info);
122 void mdelay(uint32_t ms);
123 void udelay(uint32_t us);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* _DRV_COMMON_H_ */
130 
131