1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file     drv/common.h
21  * @brief    Header File for Common Driver
22  * @version  V1.0
23  * @date     31. March 2020
24  * @model    common
25  ******************************************************************************/
26 
27 #ifndef _DRV_COMMON_H_
28 #define _DRV_COMMON_H_
29 
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #include <drv/list.h>
35 #include <drv/dev_tag.h>
36 #include <csi_config.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #ifdef CONFIG_DEBUG_MODE
43 #define CSI_ASSERT(expr)                            \
44         do {                                        \
45             if ((long)expr == (long)NULL) {   \
46                 printf("PROGRAM ASSERT\n");         \
47                 while(1);                           \
48             }                                       \
49         } while(0);
50 #else
51 #define CSI_ASSERT(expr)        ((void)0U)
52 #endif
53 
54 #ifndef CONFIG_PARAM_NOT_CHECK
55 #define CSI_PARAM_CHK(para, err)                        \
56     do                                                  \
57     {                                                   \
58         if ((unsigned long)para == (unsigned long)NULL) \
59         {                                               \
60             return (err);                               \
61         }                                               \
62     } while (0)
63 
64 #define CSI_PARAM_CHK_NORETVAL(para)                    \
65     do                                                  \
66     {                                                   \
67         if ((unsigned long)para == (unsigned long)NULL) \
68         {                                               \
69             return;                                     \
70         }                                               \
71     } while (0)
72 #else
73 #define CSI_PARAM_CHK(para, err)
74 #define CSI_PARAM_CHK_NORETVAL(para)
75 #endif
76 
77 typedef enum {
78     CSI_OK          =  0,
79     CSI_ERROR       = -1,
80     CSI_BUSY        = -2,
81     CSI_TIMEOUT     = -3,
82     CSI_UNSUPPORTED = -4
83 } csi_error_t;
84 
85 typedef struct {
86    uint8_t    readable;
87    uint8_t    writeable;
88    uint8_t    error;
89 } csi_state_t;
90 
91 typedef struct csi_dev csi_dev_t;
92 
93 #ifdef CONFIG_PM
94 typedef enum {
95     PM_DEV_SUSPEND,
96     PM_DEV_RESUME,
97 } csi_pm_dev_action_t;
98 
99 typedef enum {
100     PM_MODE_RUN                  = 0,   ///< Running mode
101     PM_MODE_SLEEP_1,                    ///< Sleep LV1 mode
102     PM_MODE_SLEEP_2,                    ///< Sleep LV2 mode
103     PM_MODE_DEEP_SLEEP_1,               ///< Deep sleep LV1 mode
104     PM_MODE_DEEP_SLEEP_2,               ///< Deep sleep LV2 mode
105 } csi_pm_mode_t;
106 
107 typedef struct {
108     slist_t     next;
109     csi_error_t (*pm_action)(csi_dev_t *dev, csi_pm_dev_action_t action);
110     uint32_t    *reten_mem;
111     uint32_t    size;
112 } csi_pm_dev_t;
113 #include <drv/pm.h>
114 #endif
115 
116 struct csi_dev {
117     unsigned long reg_base;
118     uint16_t      irq_num;
119     uint16_t      idx;
120     uint16_t      dev_tag;
121     void          (*irq_handler)(void *);
122     void          (*irq_handler2)(uint32_t irqn, void *arg);
123     void          *arg;
124 #ifdef CONFIG_PM
125     csi_pm_dev_t  pm_dev;
126 #endif
127 };
128 
129 #define HANDLE_REG_BASE(handle)     (handle->dev.reg_base)
130 #define HANDLE_IRQ_NUM(handle)      (handle->dev.irq_num)
131 #define HANDLE_DEV_IDX(handle)      (handle->dev.idx)
132 #define HANDLE_IRQ_HANDLER(handle)  (handle->dev.irq_handler)
133 
134 typedef struct {
135     unsigned long    reg_base;
136     uint16_t         irq_num;
137     uint16_t         idx;
138     uint16_t         dev_tag;
139 } csi_perip_info_t;
140 
141 csi_error_t target_get(csi_dev_tag_t dev_tag, uint32_t idx, csi_dev_t *dev);
142 csi_error_t target_get_optimal_dma_channel(void *dma_list, uint32_t ctrl_num, csi_dev_t *parent_dev, void *ch_info);
143 void mdelay(uint32_t ms);
144 void udelay(uint32_t us);
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* _DRV_COMMON_H_ */
151 
152