hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
/*
 * Copyright (C) 2013 Spreadtrum Communications Inc.
 *
 * Filename : slp_mgr.c
 * Abstract : This file is a implementation for  sleep manager
 *
 * Authors    : sam.sun
 *
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/version.h>
//#include <linux/wakelock.h>
#include <wcn_bus.h>
#include "../sdio/sdiohal.h"
#include "slp_mgr.h"
#include "slp_sdio.h"
#include "wcn_glb.h"
 
struct slp_mgr_t slp_mgr;
 
struct slp_mgr_t *slp_get_info(void)
{
   return &slp_mgr;
}
 
void slp_mgr_drv_sleep(enum slp_subsys subsys, bool enable)
{
   mutex_lock(&(slp_mgr.drv_slp_lock));
   if (enable)
       slp_mgr.active_module &= ~(BIT(subsys));
   else
       slp_mgr.active_module |= (BIT(subsys));
   if (slp_mgr.active_module == 0) {
       /* If pubint pin is valid (not used as bt_wake_host pin),
        * packet mode will enter sleep when pub int irq coming.
        */
       if (marlin_get_bt_wl_wake_host_en()) {
           slp_allow_sleep();
           atomic_set(&(slp_mgr.cp2_state), STAY_SLPING);
       } else if (subsys > PACKER_DT_RX) {
           slp_allow_sleep();
           atomic_set(&(slp_mgr.cp2_state), STAY_SLPING);
       }
   }
   mutex_unlock(&(slp_mgr.drv_slp_lock));
}
 
int slp_mgr_wakeup(enum slp_subsys subsys)
{
   unsigned char slp_sts;
   int ret;
   int do_dump = 0;
   ktime_t time_end;
#if KERNEL_VERSION(3, 16, 75) > LINUX_VERSION_CODE
   ktime_t time_cmp;
#endif
   mutex_lock(&(slp_mgr.wakeup_lock));
   if (STAY_SLPING == (atomic_read(&(slp_mgr.cp2_state)))) {
       ap_wakeup_cp();
       time_end = ktime_add_ms(ktime_get(), 30);
#ifndef CONFIG_CHECK_DRIVER_BY_CHIPID
#ifdef CONFIG_UWE5623
       /*select btwf_slp_status*/
       sprdwcn_bus_aon_writeb(REG_CP_PMU_SEL_CTL, 6);
#endif
#else /*CONFIG_CHECK_DRIVER_BY_CHIPID*/
       if (wcn_get_chip_model() == WCN_CHIP_MARLIN3E)
           sprdwcn_bus_aon_writeb(REG_CP_PMU_SEL_CTL, 6);
#endif
       while (1) {
           ret = sprdwcn_bus_aon_readb(REG_BTWF_SLP_STS, &slp_sts);
           if (ret < 0) {
               SLP_MGR_ERR("read slp sts err:%d", ret);
               usleep_range(40, 80);
               goto try_timeout;
           }
 
           slp_sts &= SLEEP_STATUS_FLAG;
#ifndef CONFIG_CHECK_DRIVER_BY_CHIPID
           if ((slp_sts != BTWF_IN_DEEPSLEEP) &&
              (slp_sts != BTWF_IN_DEEPSLEEP_XLT_ON) &&
#if defined(CONFIG_UWE5622) || defined(CONFIG_UWE5623)
              (slp_sts != BTWF_PLL_PWR_WAIT) &&
              (slp_sts != BTWF_XLT_WAIT) &&
              (slp_sts != BTWF_XLTBUF_WAIT))
#else
              (slp_sts != BTWF_IN_DEEPSLEEP_XLT_ON))
#endif
           {
#if defined(CONFIG_UWE5623)
               unsigned int reg_val = 0;
 
               sprdwcn_bus_reg_read(CP_WAKE_STATUS,
                            &reg_val, 4);
               if ((reg_val & BIT(31)) == 0)
#endif
                   break;
           }
#else
           if ((slp_sts != BTWF_IN_DEEPSLEEP) &&
              (slp_sts != BTWF_IN_DEEPSLEEP_XLT_ON) &&
              ((((wcn_get_chip_model() == WCN_CHIP_MARLIN3E) ||
              (wcn_get_chip_model() == WCN_CHIP_MARLIN3L)) &&
              (slp_sts != BTWF_PLL_PWR_WAIT) &&
              (slp_sts != BTWF_XLT_WAIT) &&
              (slp_sts != BTWF_XLTBUF_WAIT)) ||
              (wcn_get_chip_model() == WCN_CHIP_MARLIN3))) {
               if (wcn_get_chip_model() ==
                   WCN_CHIP_MARLIN3E) {
                   unsigned int reg_val = 0;
 
                   sprdwcn_bus_reg_read(CP_WAKE_STATUS,
                                &reg_val, 4);
                   if ((reg_val & BIT(31)) == 0)
                       break;
               } else
                   break;
           }
#endif
try_timeout:
           //SLP_MGR_INFO("slp_sts-0x%x", slp_sts);
           if (do_dump) {
               atomic_set(&(slp_mgr.cp2_state), STAY_AWAKING);
               SLP_MGR_INFO("wakeup fail, slp_sts-0x%x",
                        slp_sts);
               sdiohal_dump_aon_reg();
               mutex_unlock(&(slp_mgr.wakeup_lock));
               return -1;
           }
#if KERNEL_VERSION(3, 16, 75) <= LINUX_VERSION_CODE
           /* kernelv3.16.75 add ktime_after function. */
           if (ktime_after(ktime_get(), time_end))
               do_dump = 1;
#else
           time_cmp = ktime_get();
           if (time_cmp.tv64 > time_end.tv64)
               do_dump = 1;
#endif
       }
 
       atomic_set(&(slp_mgr.cp2_state), STAY_AWAKING);
   }
   mutex_unlock(&(slp_mgr.wakeup_lock));
 
   return 0;
}
 
/* called after chip power on, and reset sleep status */
void slp_mgr_reset(void)
{
   atomic_set(&(slp_mgr.cp2_state), STAY_AWAKING);
   reinit_completion(&(slp_mgr.wakeup_ack_completion));
}
 
int slp_mgr_init(void)
{
   SLP_MGR_DBG("%s enter\n", __func__);
 
   atomic_set(&(slp_mgr.cp2_state), STAY_AWAKING);
   mutex_init(&(slp_mgr.drv_slp_lock));
   mutex_init(&(slp_mgr.wakeup_lock));
   init_completion(&(slp_mgr.wakeup_ack_completion));
   slp_pub_int_RegCb();
#ifdef SLP_MGR_TEST
   slp_test_init();
#endif
 
   SLP_MGR_DBG("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(slp_mgr_init);
 
int slp_mgr_deinit(void)
{
   SLP_MGR_DBG("%s enter\n", __func__);
   atomic_set(&(slp_mgr.cp2_state), STAY_SLPING);
   slp_mgr.active_module = 0;
   mutex_destroy(&(slp_mgr.drv_slp_lock));
   mutex_destroy(&(slp_mgr.wakeup_lock));
   SLP_MGR_DBG("%s ok!\n", __func__);
 
   return 0;
}
EXPORT_SYMBOL(slp_mgr_deinit);