hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/device.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <marlin_platform.h>
#include <wcn_bus.h>
#include "sdio_int.h"
#include "slp_mgr.h"
#include "slp_sdio.h"
#include "wcn_glb.h"
#include "../sdio/sdiohal.h"
 
struct sdio_int_t sdio_int = {0};
 
atomic_t flag_pub_int_done;
bool sdio_power_notify = FALSE;
 
static inline int sdio_pub_int_clr0(unsigned char int_sts0)
{
   return sprdwcn_bus_aon_writeb(sdio_int.pub_int_clr0,
           int_sts0);
}
 
bool sdio_get_power_notify(void)
{
   return sdio_power_notify;
}
 
void sdio_record_power_notify(bool notify_cb_sts)
{
   sdio_power_notify = notify_cb_sts;
}
 
void sdio_wait_pub_int_done(void)
{
   struct slp_mgr_t *slp_mgr;
   int wait_cnt = 0;
 
   if (sdio_int.pub_int_num <= 0)
       return;
 
   slp_mgr = slp_get_info();
 
   if (sdio_power_notify) {
       /* enter suspend, means no tx data to cp2, so set sleep*/
       mutex_lock(&(slp_mgr->drv_slp_lock));
       if (atomic_read(&(slp_mgr->cp2_state)) == STAY_AWAKING) {
           SLP_MGR_INFO("allow sleep1\n");
           slp_allow_sleep();
           atomic_set(&(slp_mgr->cp2_state), STAY_SLPING);
       }
       mutex_unlock(&(slp_mgr->drv_slp_lock));
 
       /* wait pub_int handle finish*/
       while ((atomic_read(&flag_pub_int_done) == 0) &&
              (wait_cnt < 10)) {
           wait_cnt++;
           SLP_MGR_INFO("wait pub_int_done:%d", wait_cnt);
           usleep_range(1500, 3000);
       }
       SLP_MGR_INFO("flag_pub_int_done-%d",
           atomic_read(&flag_pub_int_done));
   } else
       SLP_MGR_INFO("sdio power_notify is NULL");
}
EXPORT_SYMBOL(sdio_wait_pub_int_done);
 
int pub_int_handle_thread(void *data)
{
   union PUB_INT_STS0_REG pub_int_sts0 = {0};
   int bit_num, ret;
 
   while (!kthread_should_stop()) {
       /* wait_for_completion may cause hung_task_timeout_secs
        * with message of task blocked for more than 120 seconds.
        */
       wait_for_completion_interruptible(
           &(sdio_int.pub_int_completion));
 
       ret = sprdwcn_bus_aon_readb(sdio_int.pub_int_sts0,
           &(pub_int_sts0.reg));
       /* sdio cmd52 fail, it should be chip power off */
       if (ret < 0)
           SLP_MGR_INFO("sdio cmd52 fail, ret-%d", ret);
       else {
           SLP_MGR_INFO("PUB_INT_STS0-0x%x\n", pub_int_sts0.reg);
           sdio_pub_int_clr0(pub_int_sts0.reg);
 
           bit_num = 0;
           do {
               if ((pub_int_sts0.reg & BIT(bit_num)) &&
                   sdio_int.pub_int_cb[bit_num]) {
                   sdio_int.pub_int_cb[bit_num]();
               }
               bit_num++;
           } while (bit_num < PUB_INT_MAX);
       }
 
       if (sdio_power_notify)
           atomic_set(&flag_pub_int_done, 1);
       else {
           __pm_relax((sdio_int.pub_int_ws));
       }
 
       /* enable interrupt, balance with disable in pub_int_isr */
       if (atomic_read(&(sdio_int.chip_power_on)))
           enable_irq(sdio_int.pub_int_num);
   }
 
   return 0;
}
 
static int irq_cnt;
static irqreturn_t pub_int_isr(int irq, void *para)
{
   disable_irq_nosync(irq);
   /*
    * for wifi powersave special handle, when wifi driver send
    * power save cmd to cp2, then pub int can't take wakelock,
    * or ap can't enter deep sleep.
    */
   if (sdio_power_notify)
       atomic_set(&flag_pub_int_done, 0);
   else {
       __pm_stay_awake((sdio_int.pub_int_ws));
   }
 
   irq_cnt++;
   SLP_MGR_INFO("irq_cnt%d!!", irq_cnt);
 
   complete(&(sdio_int.pub_int_completion));
 
   return IRQ_HANDLED;
}
 
static struct task_struct *pub_int_handle_task;
static int sdio_isr_handle_init(void)
{
   if (!pub_int_handle_task)
       pub_int_handle_task = kthread_create(pub_int_handle_thread,
           NULL, "pub_int_handle_thread");
   if (pub_int_handle_task != 0) {
       wake_up_process(pub_int_handle_task);
       return 0;
   }
 
   SLP_MGR_INFO("%s ok!\n", __func__);
 
   return -1;
}
 
static int sdio_pub_int_register(int irq)
{
   int ret = 0;
 
   SLP_MGR_INFO("public_int, gpio-%d\n", irq);
 
   if (irq <= 0)
       return ret;
 
   ret = gpio_direction_input(irq);
   if (ret < 0) {
       SLP_MGR_ERR("public_int, gpio-%d input set fail!!!", irq);
       return ret;
   }
 
   sdio_int.pub_int_num = gpio_to_irq(irq);
   SLP_MGR_INFO("public_int, intnum-%d\n", sdio_int.pub_int_num);
 
   ret = request_irq(sdio_int.pub_int_num,
           pub_int_isr,
           IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND,
           "pub_int_isr",
           NULL);
   if (ret != 0) {
       SLP_MGR_ERR("req irq-%d err!!!", sdio_int.pub_int_num);
       return ret;
   }
 
   /* enable interrupt when chip power on */
   disable_irq(sdio_int.pub_int_num);
 
   return ret;
}
 
int sdio_ap_int_cp0(enum AP_INT_CP_BIT bit)
{
   union AP_INT_CP0_REG reg_int_cp0 = {0};
 
   switch (bit) {
 
   case ALLOW_CP_SLP:
       reg_int_cp0.bit.allow_cp_slp = 1;
       break;
 
   case WIFI_BIN_DOWNLOAD:
       reg_int_cp0.bit.wifi_bin_download = 1;
       break;
 
   case BT_BIN_DOWNLOAD:
       reg_int_cp0.bit.bt_bin_download = 1;
       break;
   case SAVE_CP_MEM:
       reg_int_cp0.bit.save_cp_mem = 1;
       break;
   case TEST_DEL_THREAD:
       reg_int_cp0.bit.test_delet_thread = 1;
       break;
 
   default:
       SLP_MGR_INFO("ap_int_cp bit error");
       break;
   }
 
   return sprdwcn_bus_aon_writeb(sdio_int.ap_int_cp0,
           reg_int_cp0.reg);
}
EXPORT_SYMBOL(sdio_ap_int_cp0);
 
int sdio_pub_int_RegCb(enum PUB_INT_BIT bit,
       PUB_INT_ISR isr_handler)
{
   if (sdio_int.pub_int_num <= 0)
       return 0;
 
   if (isr_handler == NULL) {
       SLP_MGR_ERR("pub_int_RegCb error !!");
       return -1;
   }
 
   sdio_int.pub_int_cb[bit] = isr_handler;
 
   SLP_MGR_INFO("0X%x pub_int_RegCb", bit);
 
   return 0;
}
EXPORT_SYMBOL(sdio_pub_int_RegCb);
 
int sdio_pub_int_btwf_en0(void)
{
   union PUB_INT_EN0_REG reg_int_en = {0};
 
   if (sdio_int.pub_int_num <= 0)
       return 0;
 
   sprdwcn_bus_aon_readb(sdio_int.pub_int_en0, &(reg_int_en.reg));
 
   reg_int_en.bit.req_slp = 1;
   reg_int_en.bit.mem_save_bin = 1;
   reg_int_en.bit.wifi_open = 1;
   reg_int_en.bit.bt_open = 1;
   reg_int_en.bit.wifi_close = 1;
   reg_int_en.bit.bt_close = 1;
   sprdwcn_bus_aon_writeb(sdio_int.pub_int_en0, reg_int_en.reg);
 
   SLP_MGR_INFO("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(sdio_pub_int_btwf_en0);
 
int sdio_pub_int_gnss_en0(void)
{
   union PUB_INT_EN0_REG reg_int_en = {0};
 
   if (sdio_int.pub_int_num <= 0)
       return 0;
 
   sprdwcn_bus_aon_readb(sdio_int.pub_int_en0, &(reg_int_en.reg));
 
   reg_int_en.bit.gnss_cali_done = 1;
 
   sprdwcn_bus_aon_writeb(sdio_int.pub_int_en0, reg_int_en.reg);
 
   SLP_MGR_INFO("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(sdio_pub_int_gnss_en0);
 
 
void sdio_pub_int_poweron(bool state)
{
   if (sdio_int.pub_int_num <= 0)
       return;
 
   atomic_set(&(sdio_int.chip_power_on), state);
 
   if (state)
       enable_irq(sdio_int.pub_int_num);
   else {
       disable_irq(sdio_int.pub_int_num);
       reinit_completion(&(sdio_int.pub_int_completion));
   }
}
EXPORT_SYMBOL(sdio_pub_int_poweron);
 
int sdio_pub_int_init(int irq)
{
   if (irq <= 0) {
       sdio_int.pub_int_num = 0;
       return 0;
   }
 
   sdio_int.cp_slp_ctl = REG_CP_SLP_CTL;
   sdio_int.ap_int_cp0 = REG_AP_INT_CP0;
   sdio_int.pub_int_en0 = REG_PUB_INT_EN0;
   sdio_int.pub_int_clr0 = REG_PUB_INT_CLR0;
   sdio_int.pub_int_sts0 = REG_PUB_INT_STS0;
 
   atomic_set(&flag_pub_int_done, 1);
 
   /*wakeup_source pointer*/
   sdio_int.pub_int_ws = wakeup_source_create("pub_int_ws");
   wakeup_source_add(sdio_int.pub_int_ws);
 
   init_completion(&(sdio_int.pub_int_completion));
 
   sdio_pub_int_register(irq);
 
   sdio_isr_handle_init();
 
   SLP_MGR_INFO("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(sdio_pub_int_init);
 
int sdio_pub_int_deinit(void)
{
   if (sdio_int.pub_int_num <= 0)
       return 0;
 
   atomic_set(&flag_pub_int_done, 1);
   if (pub_int_handle_task) {
       disable_irq(sdio_int.pub_int_num);
       complete(&(sdio_int.pub_int_completion));
       kthread_stop(pub_int_handle_task);
       pub_int_handle_task = NULL;
   }
 
   sdio_power_notify = FALSE;
   disable_irq(sdio_int.pub_int_num);
   free_irq(sdio_int.pub_int_num, NULL);
 
   /*wakeup_source pointer*/
   wakeup_source_remove(sdio_int.pub_int_ws);
   wakeup_source_destroy(sdio_int.pub_int_ws);
 
   SLP_MGR_INFO("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(sdio_pub_int_deinit);