1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Platform Dependent file for Qualcomm MSM/APQ
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 1999-2017, Broadcom Corporation
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Unless you and Broadcom execute a separate written software license
9*4882a593Smuzhiyun * agreement governing use of this software, this software is licensed to you
10*4882a593Smuzhiyun * under the terms of the GNU General Public License version 2 (the "GPL"),
11*4882a593Smuzhiyun * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12*4882a593Smuzhiyun * following added to such license:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * As a special exception, the copyright holders of this software give you
15*4882a593Smuzhiyun * permission to link this software with independent modules, and to copy and
16*4882a593Smuzhiyun * distribute the resulting executable under terms of your choice, provided that
17*4882a593Smuzhiyun * you also meet, for each linked independent module, the terms and conditions of
18*4882a593Smuzhiyun * the license of that module. An independent module is a module which is not
19*4882a593Smuzhiyun * derived from this software. The special exception does not apply to any
20*4882a593Smuzhiyun * modifications of the software.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Notwithstanding the above, under no circumstances may you combine this
23*4882a593Smuzhiyun * software in any way with any other Broadcom software provided under a license
24*4882a593Smuzhiyun * other than the GPL, without Broadcom's express prior written consent.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * <<Broadcom-WL-IPTag/Open:>>
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * $Id: dhd_custom_msm.c 695148 2017-04-19 04:15:17Z $
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include <linux/kernel.h>
33*4882a593Smuzhiyun #include <linux/init.h>
34*4882a593Smuzhiyun #include <linux/platform_device.h>
35*4882a593Smuzhiyun #include <linux/delay.h>
36*4882a593Smuzhiyun #include <linux/err.h>
37*4882a593Smuzhiyun #include <linux/gpio.h>
38*4882a593Smuzhiyun #include <linux/skbuff.h>
39*4882a593Smuzhiyun #include <linux/wlan_plat.h>
40*4882a593Smuzhiyun #include <linux/mmc/host.h>
41*4882a593Smuzhiyun #include <linux/msm_pcie.h>
42*4882a593Smuzhiyun #include <linux/fcntl.h>
43*4882a593Smuzhiyun #include <linux/fs.h>
44*4882a593Smuzhiyun #include <linux/of_gpio.h>
45*4882a593Smuzhiyun #if defined(CONFIG_ARCH_MSM8996) || defined(CONFIG_ARCH_MSM8998) || \
46*4882a593Smuzhiyun defined(CONFIG_ARCH_SDM845) || defined(CONFIG_ARCH_SM8150) || \
47*4882a593Smuzhiyun defined(USE_CUSTOM_MSM_PCIE)
48*4882a593Smuzhiyun #include <linux/msm_pcie.h>
49*4882a593Smuzhiyun #endif /* CONFIG_ARCH_MSM8996 || CONFIG_ARCH_MSM8998 ||
50*4882a593Smuzhiyun CONFIG_ARCH_SDM845 || CONFIG_ARCH_SM8150
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #ifdef CONFIG_BROADCOM_WIFI_RESERVED_MEM
54*4882a593Smuzhiyun extern int dhd_init_wlan_mem(void);
55*4882a593Smuzhiyun extern void *dhd_wlan_mem_prealloc(int section, unsigned long size);
56*4882a593Smuzhiyun #endif /* CONFIG_BROADCOM_WIFI_RESERVED_MEM */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #define WIFI_TURNON_DELAY 200
59*4882a593Smuzhiyun static int wlan_reg_on = -1;
60*4882a593Smuzhiyun #ifdef CUSTOM_DT_COMPAT_ENTRY
61*4882a593Smuzhiyun #define DHD_DT_COMPAT_ENTRY CUSTOM_DT_COMPAT_ENTRY
62*4882a593Smuzhiyun #else /* CUSTOM_DT_COMPAT_ENTRY */
63*4882a593Smuzhiyun #define DHD_DT_COMPAT_ENTRY "android,bcmdhd_wlan"
64*4882a593Smuzhiyun #endif /* CUSTOM_DT_COMPAT_ENTRY */
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #ifdef CUSTOMER_HW2
67*4882a593Smuzhiyun #define WIFI_WL_REG_ON_PROPNAME "wl_reg_on"
68*4882a593Smuzhiyun #else
69*4882a593Smuzhiyun #define WIFI_WL_REG_ON_PROPNAME "wlan-en-gpio"
70*4882a593Smuzhiyun #endif /* CUSTOMER_HW2 */
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #if defined(CONFIG_ARCH_MSM8996) || defined(CONFIG_ARCH_MSM8998) || \
73*4882a593Smuzhiyun defined(CONFIG_ARCH_SDM845) || defined(CONFIG_ARCH_SM8150)
74*4882a593Smuzhiyun #define MSM_PCIE_CH_NUM 0
75*4882a593Smuzhiyun #elif defined(USE_CUSTOM_MSM_PCIE)
76*4882a593Smuzhiyun #define MSM_PCIE_CH_NUM MSM_PCIE_CUSTOM_CH_NUM
77*4882a593Smuzhiyun #else
78*4882a593Smuzhiyun #define MSM_PCIE_CH_NUM 1
79*4882a593Smuzhiyun #endif /* CONFIG_ARCH_MSM8996 || CONFIG_ARCH_MSM8998
80*4882a593Smuzhiyun || CONFIG_ARCH_SDM845 || CONFIG_ARCH_SM8150
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun #ifdef CONFIG_BCMDHD_OOB_HOST_WAKE
84*4882a593Smuzhiyun static int wlan_host_wake_up = -1;
85*4882a593Smuzhiyun static int wlan_host_wake_irq = 0;
86*4882a593Smuzhiyun #ifdef CUSTOMER_HW2
87*4882a593Smuzhiyun #define WIFI_WLAN_HOST_WAKE_PROPNAME "wl_host_wake"
88*4882a593Smuzhiyun #else
89*4882a593Smuzhiyun #define WIFI_WLAN_HOST_WAKE_PROPNAME "wlan-host-wake-gpio"
90*4882a593Smuzhiyun #endif /* CUSTOMER_HW2 */
91*4882a593Smuzhiyun #endif /* CONFIG_BCMDHD_OOB_HOST_WAKE */
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun #ifndef USE_CUSTOM_MSM_PCIE
94*4882a593Smuzhiyun int __init
95*4882a593Smuzhiyun #else
96*4882a593Smuzhiyun int
97*4882a593Smuzhiyun #endif /* USE_CUSTOM_MSM_PCIE */
dhd_wifi_init_gpio(void)98*4882a593Smuzhiyun dhd_wifi_init_gpio(void)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun char *wlan_node = DHD_DT_COMPAT_ENTRY;
101*4882a593Smuzhiyun struct device_node *root_node = NULL;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun root_node = of_find_compatible_node(NULL, NULL, wlan_node);
104*4882a593Smuzhiyun if (!root_node) {
105*4882a593Smuzhiyun WARN(1, "failed to get device node of BRCM WLAN\n");
106*4882a593Smuzhiyun return -ENODEV;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* ========== WLAN_PWR_EN ============ */
110*4882a593Smuzhiyun wlan_reg_on = of_get_named_gpio(root_node, WIFI_WL_REG_ON_PROPNAME, 0);
111*4882a593Smuzhiyun printk(KERN_INFO "%s: gpio_wlan_power : %d\n", __FUNCTION__, wlan_reg_on);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (gpio_request_one(wlan_reg_on, GPIOF_OUT_INIT_LOW, "WL_REG_ON")) {
114*4882a593Smuzhiyun printk(KERN_ERR "%s: Faiiled to request gpio %d for WL_REG_ON\n",
115*4882a593Smuzhiyun __FUNCTION__, wlan_reg_on);
116*4882a593Smuzhiyun } else {
117*4882a593Smuzhiyun printk(KERN_ERR "%s: gpio_request WL_REG_ON done - WLAN_EN: GPIO %d\n",
118*4882a593Smuzhiyun __FUNCTION__, wlan_reg_on);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (gpio_direction_output(wlan_reg_on, 1)) {
122*4882a593Smuzhiyun printk(KERN_ERR "%s: WL_REG_ON failed to pull up\n", __FUNCTION__);
123*4882a593Smuzhiyun } else {
124*4882a593Smuzhiyun printk(KERN_ERR "%s: WL_REG_ON is pulled up\n", __FUNCTION__);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (gpio_get_value(wlan_reg_on)) {
128*4882a593Smuzhiyun printk(KERN_INFO "%s: Initial WL_REG_ON: [%d]\n",
129*4882a593Smuzhiyun __FUNCTION__, gpio_get_value(wlan_reg_on));
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* Wait for WIFI_TURNON_DELAY due to power stability */
133*4882a593Smuzhiyun msleep(WIFI_TURNON_DELAY);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun #ifdef CONFIG_BCMDHD_OOB_HOST_WAKE
136*4882a593Smuzhiyun /* ========== WLAN_HOST_WAKE ============ */
137*4882a593Smuzhiyun wlan_host_wake_up = of_get_named_gpio(root_node, WIFI_WLAN_HOST_WAKE_PROPNAME, 0);
138*4882a593Smuzhiyun printk(KERN_INFO "%s: gpio_wlan_host_wake : %d\n", __FUNCTION__, wlan_host_wake_up);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun #ifndef CUSTOMER_HW2
141*4882a593Smuzhiyun if (gpio_request_one(wlan_host_wake_up, GPIOF_IN, "WLAN_HOST_WAKE")) {
142*4882a593Smuzhiyun printk(KERN_ERR "%s: Faiiled to request gpio %d for WLAN_HOST_WAKE\n",
143*4882a593Smuzhiyun __FUNCTION__, wlan_host_wake_up);
144*4882a593Smuzhiyun return -ENODEV;
145*4882a593Smuzhiyun } else {
146*4882a593Smuzhiyun printk(KERN_ERR "%s: gpio_request WLAN_HOST_WAKE done"
147*4882a593Smuzhiyun " - WLAN_HOST_WAKE: GPIO %d\n",
148*4882a593Smuzhiyun __FUNCTION__, wlan_host_wake_up);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun #endif /* !CUSTOMER_HW2 */
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun gpio_direction_input(wlan_host_wake_up);
153*4882a593Smuzhiyun wlan_host_wake_irq = gpio_to_irq(wlan_host_wake_up);
154*4882a593Smuzhiyun #endif /* CONFIG_BCMDHD_OOB_HOST_WAKE */
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun #if defined(CONFIG_BCM4359) || defined(CONFIG_BCM4361) || defined(CONFIG_BCM4375) || \
157*4882a593Smuzhiyun defined(USE_CUSTOM_MSM_PCIE)
158*4882a593Smuzhiyun printk(KERN_INFO "%s: Call msm_pcie_enumerate\n", __FUNCTION__);
159*4882a593Smuzhiyun msm_pcie_enumerate(MSM_PCIE_CH_NUM);
160*4882a593Smuzhiyun #endif /* CONFIG_BCM4359 || CONFIG_BCM4361 || CONFIG_BCM4375 */
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun int
dhd_wlan_power(int onoff)166*4882a593Smuzhiyun dhd_wlan_power(int onoff)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun printk(KERN_INFO"------------------------------------------------");
169*4882a593Smuzhiyun printk(KERN_INFO"------------------------------------------------\n");
170*4882a593Smuzhiyun printk(KERN_INFO"%s Enter: power %s\n", __func__, onoff ? "on" : "off");
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (onoff) {
173*4882a593Smuzhiyun if (gpio_direction_output(wlan_reg_on, 1)) {
174*4882a593Smuzhiyun printk(KERN_ERR "%s: WL_REG_ON is failed to pull up\n", __FUNCTION__);
175*4882a593Smuzhiyun return -EIO;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun if (gpio_get_value(wlan_reg_on)) {
178*4882a593Smuzhiyun printk(KERN_INFO"WL_REG_ON on-step-2 : [%d]\n",
179*4882a593Smuzhiyun gpio_get_value(wlan_reg_on));
180*4882a593Smuzhiyun } else {
181*4882a593Smuzhiyun printk("[%s] gpio value is 0. We need reinit.\n", __func__);
182*4882a593Smuzhiyun if (gpio_direction_output(wlan_reg_on, 1)) {
183*4882a593Smuzhiyun printk(KERN_ERR "%s: WL_REG_ON is "
184*4882a593Smuzhiyun "failed to pull up\n", __func__);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun } else {
188*4882a593Smuzhiyun if (gpio_direction_output(wlan_reg_on, 0)) {
189*4882a593Smuzhiyun printk(KERN_ERR "%s: WL_REG_ON is failed to pull up\n", __FUNCTION__);
190*4882a593Smuzhiyun return -EIO;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun if (gpio_get_value(wlan_reg_on)) {
193*4882a593Smuzhiyun printk(KERN_INFO"WL_REG_ON on-step-2 : [%d]\n",
194*4882a593Smuzhiyun gpio_get_value(wlan_reg_on));
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun EXPORT_SYMBOL(dhd_wlan_power);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun void
post_power_operation(int on)202*4882a593Smuzhiyun post_power_operation(int on)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun #if defined(CONFIG_BCM4359) || defined(CONFIG_BCM4361) || defined(CONFIG_BCM4375) || \
205*4882a593Smuzhiyun defined(USE_CUSTOM_MSM_PCIE)
206*4882a593Smuzhiyun printk(KERN_INFO "%s: Call msm_pcie_enumerate\n", __FUNCTION__);
207*4882a593Smuzhiyun if (on) {
208*4882a593Smuzhiyun msm_pcie_enumerate(MSM_PCIE_CH_NUM);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun #endif /* CONFIG_BCM4359 || CONFIG_BCM4361 || CONFIG_BCM4375 */
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun static int
dhd_wlan_reset(int onoff)214*4882a593Smuzhiyun dhd_wlan_reset(int onoff)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun return 0;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun static int
dhd_wlan_set_carddetect(int val)220*4882a593Smuzhiyun dhd_wlan_set_carddetect(int val)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun #if defined(CONFIG_BCMDHD_OOB_HOST_WAKE) && defined(CONFIG_BCMDHD_GET_OOB_STATE)
226*4882a593Smuzhiyun int
dhd_get_wlan_oob_gpio(void)227*4882a593Smuzhiyun dhd_get_wlan_oob_gpio(void)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun return gpio_is_valid(wlan_host_wake_up) ?
230*4882a593Smuzhiyun gpio_get_value(wlan_host_wake_up) : -1;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun EXPORT_SYMBOL(dhd_get_wlan_oob_gpio);
233*4882a593Smuzhiyun #endif /* CONFIG_BCMDHD_OOB_HOST_WAKE && CONFIG_BCMDHD_GET_OOB_STATE */
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun struct resource dhd_wlan_resources = {
236*4882a593Smuzhiyun .name = "bcmdhd_wlan_irq",
237*4882a593Smuzhiyun .start = 0, /* Dummy */
238*4882a593Smuzhiyun .end = 0, /* Dummy */
239*4882a593Smuzhiyun .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_SHAREABLE |
240*4882a593Smuzhiyun #ifdef CONFIG_BCMDHD_PCIE
241*4882a593Smuzhiyun IORESOURCE_IRQ_HIGHEDGE,
242*4882a593Smuzhiyun #else
243*4882a593Smuzhiyun IORESOURCE_IRQ_HIGHLEVEL,
244*4882a593Smuzhiyun #endif /* CONFIG_BCMDHD_PCIE */
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun EXPORT_SYMBOL(dhd_wlan_resources);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun struct wifi_platform_data dhd_wlan_control = {
249*4882a593Smuzhiyun .set_power = dhd_wlan_power,
250*4882a593Smuzhiyun .set_reset = dhd_wlan_reset,
251*4882a593Smuzhiyun .set_carddetect = dhd_wlan_set_carddetect,
252*4882a593Smuzhiyun #ifdef CONFIG_BROADCOM_WIFI_RESERVED_MEM
253*4882a593Smuzhiyun .mem_prealloc = dhd_wlan_mem_prealloc,
254*4882a593Smuzhiyun #endif /* CONFIG_BROADCOM_WIFI_RESERVED_MEM */
255*4882a593Smuzhiyun };
256*4882a593Smuzhiyun EXPORT_SYMBOL(dhd_wlan_control);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun #ifndef USE_CUSTOM_MSM_PCIE
259*4882a593Smuzhiyun int __init
260*4882a593Smuzhiyun #else
261*4882a593Smuzhiyun int
262*4882a593Smuzhiyun #endif /* USE_CUSTOM_MSM_PCIE */
dhd_wlan_init(void)263*4882a593Smuzhiyun dhd_wlan_init(void)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun int ret;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun printk(KERN_INFO"%s: START.......\n", __FUNCTION__);
268*4882a593Smuzhiyun ret = dhd_wifi_init_gpio();
269*4882a593Smuzhiyun if (ret < 0) {
270*4882a593Smuzhiyun printk(KERN_ERR "%s: failed to initiate GPIO, ret=%d\n",
271*4882a593Smuzhiyun __FUNCTION__, ret);
272*4882a593Smuzhiyun goto fail;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun #ifdef CONFIG_BCMDHD_OOB_HOST_WAKE
276*4882a593Smuzhiyun dhd_wlan_resources.start = wlan_host_wake_irq;
277*4882a593Smuzhiyun dhd_wlan_resources.end = wlan_host_wake_irq;
278*4882a593Smuzhiyun #endif /* CONFIG_BCMDHD_OOB_HOST_WAKE */
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun #ifdef CONFIG_BROADCOM_WIFI_RESERVED_MEM
281*4882a593Smuzhiyun ret = dhd_init_wlan_mem();
282*4882a593Smuzhiyun if (ret < 0) {
283*4882a593Smuzhiyun printk(KERN_ERR "%s: failed to alloc reserved memory,"
284*4882a593Smuzhiyun " ret=%d\n", __FUNCTION__, ret);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun #endif /* CONFIG_BROADCOM_WIFI_RESERVED_MEM */
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun fail:
289*4882a593Smuzhiyun printk(KERN_INFO"%s: FINISH.......\n", __FUNCTION__);
290*4882a593Smuzhiyun return ret;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun #if defined(CONFIG_ARCH_MSM8996) || defined(CONFIG_ARCH_MSM8998) || \
293*4882a593Smuzhiyun defined(CONFIG_ARCH_SDM845) || defined(CONFIG_ARCH_SM8150)
294*4882a593Smuzhiyun #if defined(CONFIG_DEFERRED_INITCALLS)
295*4882a593Smuzhiyun deferred_module_init(dhd_wlan_init);
296*4882a593Smuzhiyun #else
297*4882a593Smuzhiyun late_initcall(dhd_wlan_init);
298*4882a593Smuzhiyun #endif /* CONFIG_DEFERRED_INITCALLS */
299*4882a593Smuzhiyun #else
300*4882a593Smuzhiyun #ifndef USE_CUSTOM_MSM_PCIE
301*4882a593Smuzhiyun device_initcall(dhd_wlan_init);
302*4882a593Smuzhiyun #endif /* !USE_CUSTOM_MSM_PCIE */
303*4882a593Smuzhiyun #endif /* CONFIG_ARCH_MSM8996 || CONFIG_ARCH_MSM8998
304*4882a593Smuzhiyun * CONFIG_ARCH_SDM845 || CONFIG_ARCH_SM8150
305*4882a593Smuzhiyun */
306