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