MT6620: add the new driver JB2 V1.0
[firefly-linux-kernel-4.4.55.git] / drivers / mtk_wcn_combo / drv_fm / inc / fm_utils.h
1 #ifndef __FM_UTILS_H__
2 #define __FM_UTILS_H__
3
4 #include "fm_typedef.h"
5
6
7 /**
8  * Base structure of fm object
9  */
10 #define FM_NAME_MAX 20
11 struct fm_object {
12     fm_s8       name[FM_NAME_MAX+1];                                            // name of fm object
13     fm_u8       type;                                                                   // type of fm object
14     fm_u8       flag;                                                                   // flag of fm object
15     fm_s32      ref;
16     void        *priv;
17     //struct fm_list    *list;                                                                  // list node of fm object
18 };
19
20
21 /*
22  * FM FIFO 
23  */
24 struct fm_fifo {
25     struct fm_object obj;
26     fm_s32 size;
27     fm_s32 in;
28     fm_s32 out;
29     fm_s32 len;
30     fm_s32 item_size;
31     fm_s32 (*input)(struct fm_fifo *thiz, void *item);
32     fm_s32 (*output)(struct fm_fifo *thiz, void *item);
33     fm_bool (*is_full)(struct fm_fifo *thiz);
34     fm_bool (*is_empty)(struct fm_fifo *thiz);
35     fm_s32 (*get_total_len)(struct fm_fifo *thiz);
36     fm_s32 (*get_valid_len)(struct fm_fifo *thiz);
37     fm_s32 (*reset)(struct fm_fifo *thiz);
38 };
39
40 extern struct fm_fifo* fm_fifo_init(struct fm_fifo* fifo, void *buf, const fm_s8 *name, fm_s32 item_size, fm_s32 item_num); 
41
42 extern struct fm_fifo* fm_fifo_create(const fm_s8 *name, fm_s32 item_size, fm_s32 item_num); 
43
44 extern fm_s32 fm_fifo_release(struct fm_fifo *fifo); 
45
46 #define FM_FIFO_INPUT(fifop, item)  \
47 ({                                    \
48     fm_s32 __ret = (fm_s32)0;              \
49     if(fifop && (fifop)->input){          \
50         __ret = (fifop)->input(fifop, item);    \
51     }                               \
52     __ret;                          \
53 })
54
55 #define FM_FIFO_OUTPUT(fifop, item)  \
56 ({                                    \
57     fm_s32 __ret = (fm_s32)0;              \
58     if(fifop && (fifop)->output){          \
59         __ret = (fifop)->output(fifop, item);    \
60     }                               \
61     __ret;                          \
62 })
63
64 #define FM_FIFO_IS_FULL(fifop)  \
65 ({                                    \
66     fm_bool __ret = fm_false;              \
67     if(fifop && (fifop)->is_full){          \
68         __ret = (fifop)->is_full(fifop);    \
69     }                               \
70     __ret;                          \
71 })
72
73 #define FM_FIFO_IS_EMPTY(fifop)  \
74 ({                                    \
75     fm_bool __ret = fm_false;              \
76     if(fifop && (fifop)->is_empty){          \
77         __ret = (fifop)->is_empty(fifop);    \
78     }                               \
79     __ret;                          \
80 })
81
82 #define FM_FIFO_RESET(fifop)  \
83 ({                                    \
84     fm_s32 __ret = (fm_s32)0;              \
85     if(fifop && (fifop)->reset){          \
86         __ret = (fifop)->reset(fifop);    \
87     }                               \
88     __ret;                          \
89 })
90
91 #define FM_FIFO_GET_TOTAL_LEN(fifop)  \
92 ({                                    \
93     fm_s32 __ret = (fm_s32)0;              \
94     if(fifop && (fifop)->get_total_len){          \
95         __ret = (fifop)->get_total_len(fifop);    \
96     }                               \
97     __ret;                          \
98 })
99
100 #define FM_FIFO_GET_VALID_LEN(fifop)  \
101 ({                                    \
102     fm_s32 __ret = (fm_s32)0;              \
103     if(fifop && (fifop)->get_valid_len){          \
104         __ret = (fifop)->get_valid_len(fifop);    \
105     }                               \
106     __ret;                          \
107 })
108
109        
110 /*
111  * FM asynchronous information mechanism
112  */
113 struct fm_flag_event {
114     fm_s32 ref;
115     fm_s8  name[FM_NAME_MAX+1];
116     void *priv;
117
118     volatile fm_u32 flag;
119
120     //flag methods
121     fm_u32(*send)(struct fm_flag_event* thiz, fm_u32 mask);
122     fm_s32(*wait)(struct fm_flag_event* thiz, fm_u32 mask);
123     long(*wait_timeout)(struct fm_flag_event* thiz, fm_u32 mask, long timeout);
124     fm_u32(*clr)(struct fm_flag_event* thiz, fm_u32 mask);
125     fm_u32(*get)(struct fm_flag_event* thiz);
126     fm_u32(*rst)(struct fm_flag_event* thiz);
127 };
128
129 extern struct fm_flag_event* fm_flag_event_create(const fm_s8 *name);
130
131 extern fm_s32 fm_flag_event_get(struct fm_flag_event *thiz);
132
133 extern fm_s32 fm_flag_event_put(struct fm_flag_event *thiz);
134
135 #define FM_EVENT_SEND(eventp, mask)  \
136 ({                                    \
137     fm_u32 __ret = (fm_u32)0;              \
138     if(eventp && (eventp)->send){          \
139         __ret = (eventp)->send(eventp, mask);    \
140     }                               \
141     __ret;                          \
142 })
143
144 #define FM_EVENT_WAIT(eventp, mask)  \
145 ({                                    \
146     fm_s32 __ret = (fm_s32)0;              \
147     if(eventp && (eventp)->wait){          \
148         __ret = (eventp)->wait(eventp, mask);    \
149     }                               \
150     __ret;                          \
151 })
152
153 #define FM_EVENT_WAIT_TIMEOUT(eventp, mask, timeout)  \
154 ({                                    \
155     long __ret = (long)0;              \
156     if(eventp && (eventp)->wait_timeout){          \
157         __ret = (eventp)->wait_timeout(eventp, mask, timeout);    \
158     }                               \
159     __ret;                          \
160 })
161
162 #define FM_EVENT_GET(eventp)  \
163 ({                                    \
164     fm_u32 __ret = (fm_u32)0;              \
165     if(eventp && (eventp)->get){          \
166         __ret = (eventp)->get(eventp);    \
167     }                               \
168     __ret;                          \
169 })
170
171 #define FM_EVENT_RESET(eventp)  \
172 ({                                    \
173     fm_u32 __ret = (fm_u32)0;              \
174     if(eventp && (eventp)->rst){          \
175         __ret = (eventp)->rst(eventp);    \
176     }                               \
177     __ret;                          \
178 })
179
180 #define FM_EVENT_CLR(eventp, mask)  \
181 ({                                    \
182     fm_u32 __ret = (fm_u32)0;              \
183     if(eventp && (eventp)->clr){          \
184         __ret = (eventp)->clr(eventp, mask);    \
185     }                               \
186     __ret;                          \
187 })
188
189 /*
190  * FM lock mechanism
191  */
192 struct fm_lock {
193     fm_s8   name[FM_NAME_MAX+1];
194     fm_s32  ref;
195     void    *priv;
196
197     //lock methods
198     fm_s32(*lock)(struct fm_lock* thiz);
199         fm_s32(*trylock)(struct fm_lock *thiz,fm_s32 retryCnt);
200     fm_s32(*unlock)(struct fm_lock* thiz);
201 };
202
203 extern struct fm_lock* fm_lock_create(const fm_s8 *name);
204
205 extern fm_s32 fm_lock_get(struct fm_lock *thiz);
206
207 extern fm_s32 fm_lock_put(struct fm_lock *thiz);
208
209 extern struct fm_lock* fm_spin_lock_create(const fm_s8 *name);
210
211 extern fm_s32 fm_spin_lock_get(struct fm_lock *thiz);
212
213 extern fm_s32 fm_spin_lock_put(struct fm_lock *thiz);
214
215 #define FM_LOCK(a)         \
216 ({                           \
217     fm_s32 __ret = (fm_s32)0; \
218     if(a && (a)->lock){          \
219         __ret = (a)->lock(a);    \
220     }                       \
221     __ret;                   \
222 })
223
224 #define FM_UNLOCK(a)         \
225 {                             \
226     if((a)->unlock){          \
227         (a)->unlock(a);    \
228     }                       \
229 }
230
231
232 /*
233  * FM timer mechanism
234  */
235 enum fm_timer_ctrl {
236     FM_TIMER_CTRL_GET_TIME = 0,
237     FM_TIMER_CTRL_SET_TIME = 1,
238     FM_TIMER_CTRL_MAX
239 };
240
241 #define FM_TIMER_FLAG_ACTIVATED (1<<0)
242
243 struct fm_timer {
244     fm_s32 ref;
245     fm_s8  name[FM_NAME_MAX+1];
246     void *priv;                                         // platform detail impliment
247
248     fm_s32 flag;                                        // timer active/inactive
249     void (*timeout_func)(unsigned long data);               //  timeout function
250     unsigned long data;                                                                 // timeout function's parameter
251     signed long timeout_ms;                                                         // timeout tick
252     //Tx parameters
253     volatile fm_u32    count;
254     volatile fm_u8     tx_pwr_ctrl_en;
255     volatile fm_u8     tx_rtc_ctrl_en;
256     volatile fm_u8     tx_desense_en;
257
258     //timer methods
259     fm_s32(*init)(struct fm_timer *thiz, void (*timeout)(unsigned long data), unsigned long data, signed long time, fm_s32 flag);
260     fm_s32(*start)(struct fm_timer *thiz);
261     fm_s32(*update)(struct fm_timer *thiz);
262     fm_s32(*stop)(struct fm_timer *thiz);
263     fm_s32(*control)(struct fm_timer *thiz, enum fm_timer_ctrl cmd, void* arg);
264 };
265
266 extern struct fm_timer* fm_timer_create(const fm_s8 *name);
267
268 extern fm_s32 fm_timer_get(struct fm_timer *thiz);
269
270 extern fm_s32 fm_timer_put(struct fm_timer *thiz);
271
272 /*
273  * FM work thread mechanism
274  */
275 struct fm_work {
276     fm_s32 ref;
277     fm_s8  name[FM_NAME_MAX+1];
278     void *priv;
279
280     void (*work_func)(unsigned long data);
281     unsigned long data;
282     //work methods
283     fm_s32(*init)(struct fm_work *thiz, void (*work_func)(unsigned long data), unsigned long data);
284 };
285
286 extern struct fm_work* fm_work_create(const fm_s8 *name);
287
288 extern fm_s32 fm_work_get(struct fm_work *thiz);
289
290 extern fm_s32 fm_work_put(struct fm_work *thiz);
291
292
293 struct fm_workthread {
294     fm_s32 ref;
295     fm_s8  name[FM_NAME_MAX+1];
296     void *priv;
297
298     //workthread methods
299     fm_s32(*add_work)(struct fm_workthread *thiz, struct fm_work *work);
300 };
301
302 extern struct fm_workthread* fm_workthread_create(const fm_s8* name);
303
304 extern fm_s32 fm_workthread_get(struct fm_workthread *thiz);
305
306 extern fm_s32 fm_workthread_put(struct fm_workthread *thiz);
307
308 #endif //__FM_UTILS_H__
309