xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_linux_platdev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Linux platform device for DHD WLAN adapter
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  *
27*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * $Id: dhd_linux_platdev.c 697969 2017-05-05 16:36:11Z $
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun #include <typedefs.h>
32*4882a593Smuzhiyun #include <linux/kernel.h>
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun #include <linux/init.h>
35*4882a593Smuzhiyun #include <linux/platform_device.h>
36*4882a593Smuzhiyun #include <bcmutils.h>
37*4882a593Smuzhiyun #include <linux_osl.h>
38*4882a593Smuzhiyun #include <dhd_dbg.h>
39*4882a593Smuzhiyun #include <dngl_stats.h>
40*4882a593Smuzhiyun #include <dhd.h>
41*4882a593Smuzhiyun #include <dhd_bus.h>
42*4882a593Smuzhiyun #include <dhd_linux.h>
43*4882a593Smuzhiyun #if defined(OEM_ANDROID)
44*4882a593Smuzhiyun #include <wl_android.h>
45*4882a593Smuzhiyun #endif // endif
46*4882a593Smuzhiyun #if defined(CONFIG_WIFI_CONTROL_FUNC)
47*4882a593Smuzhiyun #include <linux/wlan_plat.h>
48*4882a593Smuzhiyun #endif // endif
49*4882a593Smuzhiyun #ifdef CONFIG_DTS
50*4882a593Smuzhiyun #include<linux/regulator/consumer.h>
51*4882a593Smuzhiyun #include<linux/of_gpio.h>
52*4882a593Smuzhiyun #endif /* CONFIG_DTS */
53*4882a593Smuzhiyun #define WIFI_PLAT_NAME		"bcmdhd_wlan"
54*4882a593Smuzhiyun #define WIFI_PLAT_NAME2		"bcm4329_wlan"
55*4882a593Smuzhiyun #define WIFI_PLAT_EXT		"bcmdhd_wifi_platform"
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #ifdef USE_CUSTOM_MSM_PCIE
58*4882a593Smuzhiyun #include <linux/msm_pcie.h>
59*4882a593Smuzhiyun #endif /* USE_CUSTOM_MSM_PCIE */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #ifdef DHD_WIFI_SHUTDOWN
62*4882a593Smuzhiyun extern void wifi_plat_dev_drv_shutdown(struct platform_device *pdev);
63*4882a593Smuzhiyun #endif // endif
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef CONFIG_DTS
66*4882a593Smuzhiyun struct regulator *wifi_regulator = NULL;
67*4882a593Smuzhiyun #endif /* CONFIG_DTS */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun bool cfg_multichip = FALSE;
70*4882a593Smuzhiyun bcmdhd_wifi_platdata_t *dhd_wifi_platdata = NULL;
71*4882a593Smuzhiyun static int wifi_plat_dev_probe_ret = 0;
72*4882a593Smuzhiyun static bool is_power_on = FALSE;
73*4882a593Smuzhiyun #if !defined(CONFIG_DTS)
74*4882a593Smuzhiyun #if defined(DHD_OF_SUPPORT)
75*4882a593Smuzhiyun static bool dts_enabled = TRUE;
76*4882a593Smuzhiyun extern struct resource dhd_wlan_resources;
77*4882a593Smuzhiyun extern struct wifi_platform_data dhd_wlan_control;
78*4882a593Smuzhiyun #else
79*4882a593Smuzhiyun static bool dts_enabled = FALSE;
80*4882a593Smuzhiyun #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
81*4882a593Smuzhiyun #pragma GCC diagnostic push
82*4882a593Smuzhiyun #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
83*4882a593Smuzhiyun #endif // endif
84*4882a593Smuzhiyun struct resource dhd_wlan_resources = {0};
85*4882a593Smuzhiyun struct wifi_platform_data dhd_wlan_control = {0};
86*4882a593Smuzhiyun #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
87*4882a593Smuzhiyun #pragma GCC diagnostic pop
88*4882a593Smuzhiyun #endif // endif
89*4882a593Smuzhiyun #endif /* CONFIG_OF && !defined(CONFIG_ARCH_MSM) */
90*4882a593Smuzhiyun #endif /* !defind(CONFIG_DTS) */
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #ifdef DHD_CUSTOM_PLAT_DATA
93*4882a593Smuzhiyun extern int dhd_wlan_init_plat_data(void);
94*4882a593Smuzhiyun extern int dhd_wlan_deinit_plat_data(void);
95*4882a593Smuzhiyun #endif /* DHD_CUSTOM_PLAT_DATA */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun static int dhd_wifi_platform_load(void);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun extern void* wl_cfg80211_get_dhdp(struct net_device *dev);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #if defined(BOARD_HIKEY) || defined(USE_CUSTOM_MSM_PCIE)
102*4882a593Smuzhiyun extern int dhd_wlan_init(void);
103*4882a593Smuzhiyun extern int dhd_wlan_deinit(void);
104*4882a593Smuzhiyun extern void post_power_operation(int on);
105*4882a593Smuzhiyun #endif /* defined(BOARD_HIKEY) || defined(USE_CUSTOM_MSM_PCIE) */
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #ifdef ENABLE_4335BT_WAR
108*4882a593Smuzhiyun extern int bcm_bt_lock(int cookie);
109*4882a593Smuzhiyun extern void bcm_bt_unlock(int cookie);
110*4882a593Smuzhiyun static int lock_cookie_wifi = 'W' | 'i'<<8 | 'F'<<16 | 'i'<<24;	/* cookie is "WiFi" */
111*4882a593Smuzhiyun #endif /* ENABLE_4335BT_WAR */
112*4882a593Smuzhiyun 
dhd_wifi_platform_get_adapter(uint32 bus_type,uint32 bus_num,uint32 slot_num)113*4882a593Smuzhiyun wifi_adapter_info_t* dhd_wifi_platform_get_adapter(uint32 bus_type, uint32 bus_num, uint32 slot_num)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	int i;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (dhd_wifi_platdata == NULL)
118*4882a593Smuzhiyun 		return NULL;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
121*4882a593Smuzhiyun 		wifi_adapter_info_t *adapter = &dhd_wifi_platdata->adapters[i];
122*4882a593Smuzhiyun 		if ((adapter->bus_type == -1 || adapter->bus_type == bus_type) &&
123*4882a593Smuzhiyun 			(adapter->bus_num == -1 || adapter->bus_num == bus_num) &&
124*4882a593Smuzhiyun 			(adapter->slot_num == -1 || adapter->slot_num == slot_num)) {
125*4882a593Smuzhiyun 			DHD_TRACE(("found adapter info '%s'\n", adapter->name));
126*4882a593Smuzhiyun 			return adapter;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 	return NULL;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
wifi_platform_prealloc(wifi_adapter_info_t * adapter,int section,unsigned long size)132*4882a593Smuzhiyun void* wifi_platform_prealloc(wifi_adapter_info_t *adapter, int section, unsigned long size)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	void *alloc_ptr = NULL;
135*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (!adapter || !adapter->wifi_plat_data)
138*4882a593Smuzhiyun 		return NULL;
139*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
140*4882a593Smuzhiyun 	if (plat_data->mem_prealloc) {
141*4882a593Smuzhiyun 		alloc_ptr = plat_data->mem_prealloc(section, size);
142*4882a593Smuzhiyun 		if (alloc_ptr) {
143*4882a593Smuzhiyun 			DHD_INFO(("success alloc section %d\n", section));
144*4882a593Smuzhiyun 			if (size != 0L)
145*4882a593Smuzhiyun 				bzero(alloc_ptr, size);
146*4882a593Smuzhiyun 			return alloc_ptr;
147*4882a593Smuzhiyun 		}
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	DHD_ERROR(("%s: failed to alloc static mem section %d\n", __FUNCTION__, section));
151*4882a593Smuzhiyun 	return NULL;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
wifi_platform_get_prealloc_func_ptr(wifi_adapter_info_t * adapter)154*4882a593Smuzhiyun void* wifi_platform_get_prealloc_func_ptr(wifi_adapter_info_t *adapter)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (!adapter || !adapter->wifi_plat_data)
159*4882a593Smuzhiyun 		return NULL;
160*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
161*4882a593Smuzhiyun 	return plat_data->mem_prealloc;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
wifi_platform_get_irq_number(wifi_adapter_info_t * adapter,unsigned long * irq_flags_ptr)164*4882a593Smuzhiyun int wifi_platform_get_irq_number(wifi_adapter_info_t *adapter, unsigned long *irq_flags_ptr)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	if (adapter == NULL)
167*4882a593Smuzhiyun 		return -1;
168*4882a593Smuzhiyun 	if (irq_flags_ptr)
169*4882a593Smuzhiyun 		*irq_flags_ptr = adapter->intr_flags;
170*4882a593Smuzhiyun 	return adapter->irq_num;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
wifi_platform_set_power(wifi_adapter_info_t * adapter,bool on,unsigned long msec)173*4882a593Smuzhiyun int wifi_platform_set_power(wifi_adapter_info_t *adapter, bool on, unsigned long msec)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	int err = 0;
176*4882a593Smuzhiyun #ifdef CONFIG_DTS
177*4882a593Smuzhiyun 	if (on) {
178*4882a593Smuzhiyun 		err = regulator_enable(wifi_regulator);
179*4882a593Smuzhiyun 		is_power_on = TRUE;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	else {
182*4882a593Smuzhiyun 		err = regulator_disable(wifi_regulator);
183*4882a593Smuzhiyun 		is_power_on = FALSE;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 	if (err < 0)
186*4882a593Smuzhiyun 		DHD_ERROR(("%s: regulator enable/disable failed", __FUNCTION__));
187*4882a593Smuzhiyun #else
188*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (!adapter || !adapter->wifi_plat_data)
191*4882a593Smuzhiyun 		return -EINVAL;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	DHD_ERROR(("%s = %d, delay: %lu msec\n", __FUNCTION__, on, msec));
196*4882a593Smuzhiyun 	if (plat_data->set_power) {
197*4882a593Smuzhiyun #ifdef ENABLE_4335BT_WAR
198*4882a593Smuzhiyun 		if (on) {
199*4882a593Smuzhiyun 			printk("WiFi: trying to acquire BT lock\n");
200*4882a593Smuzhiyun 			if (bcm_bt_lock(lock_cookie_wifi) != 0)
201*4882a593Smuzhiyun 				printk("** WiFi: timeout in acquiring bt lock**\n");
202*4882a593Smuzhiyun 			printk("%s: btlock acquired\n", __FUNCTION__);
203*4882a593Smuzhiyun 		}
204*4882a593Smuzhiyun 		else {
205*4882a593Smuzhiyun 			/* For a exceptional case, release btlock */
206*4882a593Smuzhiyun 			bcm_bt_unlock(lock_cookie_wifi);
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun #endif /* ENABLE_4335BT_WAR */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 		err = plat_data->set_power(on);
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (msec && !err)
214*4882a593Smuzhiyun 		OSL_SLEEP(msec);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (on && !err) {
217*4882a593Smuzhiyun 		is_power_on = TRUE;
218*4882a593Smuzhiyun #ifdef USE_CUSTOM_POST_PWR_OPER
219*4882a593Smuzhiyun 		post_power_operation(on);
220*4882a593Smuzhiyun #endif /* USE_CUSTOM_MSM_PCIE */
221*4882a593Smuzhiyun 	} else {
222*4882a593Smuzhiyun 		is_power_on = FALSE;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun #endif /* CONFIG_DTS */
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	return err;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun #if defined(PLATFORM_IMX)
231*4882a593Smuzhiyun extern void wifi_card_detect(bool);
wifi_platform_bus_enumerate(wifi_adapter_info_t * adapter,bool device_present)232*4882a593Smuzhiyun int wifi_platform_bus_enumerate(wifi_adapter_info_t *adapter, bool device_present)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	int err = 0;
235*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	if (!adapter) {
238*4882a593Smuzhiyun 		pr_err("!!!! %s: failed!  adapter variable is NULL!!!!!\n", __FUNCTION__);
239*4882a593Smuzhiyun 		return -EINVAL;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	DHD_ERROR(("%s device present %d\n", __FUNCTION__, device_present));
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (!adapter->wifi_plat_data) {
245*4882a593Smuzhiyun 		wifi_card_detect(device_present); /* hook for card_detect */
246*4882a593Smuzhiyun 	} else {
247*4882a593Smuzhiyun 		plat_data = adapter->wifi_plat_data;
248*4882a593Smuzhiyun 		if (plat_data->set_carddetect)
249*4882a593Smuzhiyun 			err = plat_data->set_carddetect(device_present);
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	return 0; /* force success status returned */
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun #else
wifi_platform_bus_enumerate(wifi_adapter_info_t * adapter,bool device_present)255*4882a593Smuzhiyun int wifi_platform_bus_enumerate(wifi_adapter_info_t *adapter, bool device_present)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	int err = 0;
258*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun #ifdef CONFIG_DTS
261*4882a593Smuzhiyun 	if (wifi_regulator)
262*4882a593Smuzhiyun 		return err;
263*4882a593Smuzhiyun #endif /* CONFIG_DTS */
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	if (!adapter || !adapter->wifi_plat_data)
266*4882a593Smuzhiyun 		return -EINVAL;
267*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	DHD_ERROR(("%s device present %d\n", __FUNCTION__, device_present));
270*4882a593Smuzhiyun 	if (plat_data->set_carddetect) {
271*4882a593Smuzhiyun 		err = plat_data->set_carddetect(device_present);
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 	return err;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun #endif /* PLATFORM_IMX */
277*4882a593Smuzhiyun 
wifi_platform_get_mac_addr(wifi_adapter_info_t * adapter,unsigned char * buf)278*4882a593Smuzhiyun int wifi_platform_get_mac_addr(wifi_adapter_info_t *adapter, unsigned char *buf)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	DHD_ERROR(("%s\n", __FUNCTION__));
283*4882a593Smuzhiyun 	if (!buf || !adapter || !adapter->wifi_plat_data)
284*4882a593Smuzhiyun 		return -EINVAL;
285*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
286*4882a593Smuzhiyun 	if (plat_data->get_mac_addr) {
287*4882a593Smuzhiyun 		return plat_data->get_mac_addr(buf);
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 	return -EOPNOTSUPP;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun #ifdef	CUSTOM_COUNTRY_CODE
wifi_platform_get_country_code(wifi_adapter_info_t * adapter,char * ccode,u32 flags)292*4882a593Smuzhiyun void *wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode, u32 flags)
293*4882a593Smuzhiyun #else
294*4882a593Smuzhiyun void *wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode)
295*4882a593Smuzhiyun #endif /* CUSTOM_COUNTRY_CODE */
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	/* get_country_code was added after 2.6.39 */
298*4882a593Smuzhiyun #if	(LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
299*4882a593Smuzhiyun 	struct wifi_platform_data *plat_data;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	if (!ccode || !adapter || !adapter->wifi_plat_data)
302*4882a593Smuzhiyun 		return NULL;
303*4882a593Smuzhiyun 	plat_data = adapter->wifi_plat_data;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	DHD_TRACE(("%s\n", __FUNCTION__));
306*4882a593Smuzhiyun 	if (plat_data->get_country_code) {
307*4882a593Smuzhiyun #if     (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 58))
308*4882a593Smuzhiyun 		return plat_data->get_country_code(ccode, WLAN_PLAT_NODFS_FLAG);
309*4882a593Smuzhiyun #else
310*4882a593Smuzhiyun #ifdef	CUSTOM_COUNTRY_CODE
311*4882a593Smuzhiyun 		return plat_data->get_country_code(ccode, flags);
312*4882a593Smuzhiyun #else
313*4882a593Smuzhiyun 		return plat_data->get_country_code(ccode);
314*4882a593Smuzhiyun #endif /* CUSTOM_COUNTRY_CODE */
315*4882a593Smuzhiyun #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 58)) */
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) */
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	return NULL;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
wifi_plat_dev_drv_probe(struct platform_device * pdev)322*4882a593Smuzhiyun static int wifi_plat_dev_drv_probe(struct platform_device *pdev)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct resource *resource;
325*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
326*4882a593Smuzhiyun #ifdef CONFIG_DTS
327*4882a593Smuzhiyun 	int irq, gpio;
328*4882a593Smuzhiyun #endif /* CONFIG_DTS */
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
331*4882a593Smuzhiyun 	 * is kept for backward compatibility and supports only 1 adapter
332*4882a593Smuzhiyun 	 */
333*4882a593Smuzhiyun 	ASSERT(dhd_wifi_platdata != NULL);
334*4882a593Smuzhiyun 	ASSERT(dhd_wifi_platdata->num_adapters == 1);
335*4882a593Smuzhiyun 	adapter = &dhd_wifi_platdata->adapters[0];
336*4882a593Smuzhiyun 	adapter->wifi_plat_data = (struct wifi_platform_data *)(pdev->dev.platform_data);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcmdhd_wlan_irq");
339*4882a593Smuzhiyun 	if (resource == NULL)
340*4882a593Smuzhiyun 		resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcm4329_wlan_irq");
341*4882a593Smuzhiyun 	if (resource) {
342*4882a593Smuzhiyun 		adapter->irq_num = resource->start;
343*4882a593Smuzhiyun 		adapter->intr_flags = resource->flags & IRQF_TRIGGER_MASK;
344*4882a593Smuzhiyun #ifdef DHD_ISR_NO_SUSPEND
345*4882a593Smuzhiyun 		adapter->intr_flags |= IRQF_NO_SUSPEND;
346*4882a593Smuzhiyun #endif // endif
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun #ifdef CONFIG_DTS
350*4882a593Smuzhiyun 	wifi_regulator = regulator_get(&pdev->dev, "wlreg_on");
351*4882a593Smuzhiyun 	if (wifi_regulator == NULL) {
352*4882a593Smuzhiyun 		DHD_ERROR(("%s regulator is null\n", __FUNCTION__));
353*4882a593Smuzhiyun 		return -1;
354*4882a593Smuzhiyun 	}
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun #if defined(OOB_INTR_ONLY) || defined(BCMPCIE_OOB_HOST_WAKE)
357*4882a593Smuzhiyun 	/* This is to get the irq for the OOB */
358*4882a593Smuzhiyun 	gpio = of_get_gpio(pdev->dev.of_node, 0);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	if (gpio < 0) {
361*4882a593Smuzhiyun 		DHD_ERROR(("%s OOB GPIO  information is incorrect Or Not available\n",
362*4882a593Smuzhiyun 			__FUNCTION__));
363*4882a593Smuzhiyun 		goto end;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 	irq = gpio_to_irq(gpio);
366*4882a593Smuzhiyun 	if (irq < 0) {
367*4882a593Smuzhiyun 		DHD_ERROR(("%s irq information is incorrect\n", __FUNCTION__));
368*4882a593Smuzhiyun 		return -1;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 	adapter->irq_num = irq;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/* need to change the flags according to our requirement */
373*4882a593Smuzhiyun 	adapter->intr_flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
374*4882a593Smuzhiyun 		IORESOURCE_IRQ_SHAREABLE;
375*4882a593Smuzhiyun end:
376*4882a593Smuzhiyun #endif /* defined(OOB_INTR_ONLY) || defined(BCMPCIE_OOB_HOST_WAKE) */
377*4882a593Smuzhiyun #endif /* CONFIG_DTS */
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
380*4882a593Smuzhiyun 	return wifi_plat_dev_probe_ret;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
wifi_plat_dev_drv_remove(struct platform_device * pdev)383*4882a593Smuzhiyun static int wifi_plat_dev_drv_remove(struct platform_device *pdev)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
388*4882a593Smuzhiyun 	 * is kept for backward compatibility and supports only 1 adapter
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	ASSERT(dhd_wifi_platdata != NULL);
391*4882a593Smuzhiyun 	ASSERT(dhd_wifi_platdata->num_adapters == 1);
392*4882a593Smuzhiyun 	adapter = &dhd_wifi_platdata->adapters[0];
393*4882a593Smuzhiyun 	if (is_power_on) {
394*4882a593Smuzhiyun #ifdef BCMPCIE
395*4882a593Smuzhiyun 		wifi_platform_bus_enumerate(adapter, FALSE);
396*4882a593Smuzhiyun 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
397*4882a593Smuzhiyun #else
398*4882a593Smuzhiyun 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
399*4882a593Smuzhiyun 		wifi_platform_bus_enumerate(adapter, FALSE);
400*4882a593Smuzhiyun #endif /* BCMPCIE */
401*4882a593Smuzhiyun 	}
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun #ifdef CONFIG_DTS
404*4882a593Smuzhiyun 	regulator_put(wifi_regulator);
405*4882a593Smuzhiyun #endif /* CONFIG_DTS */
406*4882a593Smuzhiyun 	return 0;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
wifi_plat_dev_drv_suspend(struct platform_device * pdev,pm_message_t state)409*4882a593Smuzhiyun static int wifi_plat_dev_drv_suspend(struct platform_device *pdev, pm_message_t state)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	DHD_TRACE(("##> %s\n", __FUNCTION__));
412*4882a593Smuzhiyun #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
413*4882a593Smuzhiyun 	defined(BCMSDIO)
414*4882a593Smuzhiyun 	bcmsdh_oob_intr_set(0);
415*4882a593Smuzhiyun #endif /* (OOB_INTR_ONLY) */
416*4882a593Smuzhiyun 	return 0;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
wifi_plat_dev_drv_resume(struct platform_device * pdev)419*4882a593Smuzhiyun static int wifi_plat_dev_drv_resume(struct platform_device *pdev)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	DHD_TRACE(("##> %s\n", __FUNCTION__));
422*4882a593Smuzhiyun #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
423*4882a593Smuzhiyun 	defined(BCMSDIO)
424*4882a593Smuzhiyun 	if (dhd_os_check_if_up(wl_cfg80211_get_dhdp()))
425*4882a593Smuzhiyun 		bcmsdh_oob_intr_set(1);
426*4882a593Smuzhiyun #endif /* (OOB_INTR_ONLY) */
427*4882a593Smuzhiyun 	return 0;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun #ifdef CONFIG_DTS
431*4882a593Smuzhiyun static const struct of_device_id wifi_device_dt_match[] = {
432*4882a593Smuzhiyun 	{ .compatible = "android,bcmdhd_wlan", },
433*4882a593Smuzhiyun 	{},
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun #endif /* CONFIG_DTS */
436*4882a593Smuzhiyun static struct platform_driver wifi_platform_dev_driver = {
437*4882a593Smuzhiyun 	.probe          = wifi_plat_dev_drv_probe,
438*4882a593Smuzhiyun 	.remove         = wifi_plat_dev_drv_remove,
439*4882a593Smuzhiyun 	.suspend        = wifi_plat_dev_drv_suspend,
440*4882a593Smuzhiyun 	.resume         = wifi_plat_dev_drv_resume,
441*4882a593Smuzhiyun #ifdef DHD_WIFI_SHUTDOWN
442*4882a593Smuzhiyun 	.shutdown       = wifi_plat_dev_drv_shutdown,
443*4882a593Smuzhiyun #endif // endif
444*4882a593Smuzhiyun 	.driver         = {
445*4882a593Smuzhiyun 	.name   = WIFI_PLAT_NAME,
446*4882a593Smuzhiyun #ifdef CONFIG_DTS
447*4882a593Smuzhiyun 	.of_match_table = wifi_device_dt_match,
448*4882a593Smuzhiyun #endif /* CONFIG_DTS */
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun };
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun static struct platform_driver wifi_platform_dev_driver_legacy = {
453*4882a593Smuzhiyun 	.probe          = wifi_plat_dev_drv_probe,
454*4882a593Smuzhiyun 	.remove         = wifi_plat_dev_drv_remove,
455*4882a593Smuzhiyun 	.suspend        = wifi_plat_dev_drv_suspend,
456*4882a593Smuzhiyun 	.resume         = wifi_plat_dev_drv_resume,
457*4882a593Smuzhiyun #ifdef DHD_WIFI_SHUTDOWN
458*4882a593Smuzhiyun 	.shutdown       = wifi_plat_dev_drv_shutdown,
459*4882a593Smuzhiyun #endif // endif
460*4882a593Smuzhiyun 	.driver         = {
461*4882a593Smuzhiyun 	.name	= WIFI_PLAT_NAME2,
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun };
464*4882a593Smuzhiyun #if     (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0))
wifi_platdev_match(struct device * dev,const void * data)465*4882a593Smuzhiyun static int wifi_platdev_match(struct device *dev, const void *data)
466*4882a593Smuzhiyun #else
467*4882a593Smuzhiyun static int wifi_platdev_match(struct device *dev, void *data)
468*4882a593Smuzhiyun #endif /* LINUX_VERSION_CODE */
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	const char *name = (const char*)data;
471*4882a593Smuzhiyun #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
472*4882a593Smuzhiyun #pragma GCC diagnostic push
473*4882a593Smuzhiyun #pragma GCC diagnostic ignored "-Wcast-qual"
474*4882a593Smuzhiyun #endif // endif
475*4882a593Smuzhiyun 	const struct platform_device *pdev = to_platform_device(dev);
476*4882a593Smuzhiyun #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
477*4882a593Smuzhiyun #pragma GCC diagnostic pop
478*4882a593Smuzhiyun #endif // endif
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	if (strcmp(pdev->name, name) == 0) {
481*4882a593Smuzhiyun 		DHD_ERROR(("found wifi platform device %s\n", name));
482*4882a593Smuzhiyun 		return TRUE;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	return FALSE;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun 
wifi_ctrlfunc_register_drv(void)488*4882a593Smuzhiyun static int wifi_ctrlfunc_register_drv(void)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun 	int err = 0;
491*4882a593Smuzhiyun 	struct device *dev1, *dev2;
492*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
495*4882a593Smuzhiyun 	dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun #if defined(BOARD_HIKEY_MODULAR) || defined(USE_CUSTOM_MSM_PCIE)
498*4882a593Smuzhiyun 	dhd_wlan_init();
499*4882a593Smuzhiyun #endif /* defined(BOARD_HIKEY_MODULAR) || defined(USE_CUSTOM_MSM_PCIE) */
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun #if !defined(CONFIG_DTS)
502*4882a593Smuzhiyun 	if (!dts_enabled) {
503*4882a593Smuzhiyun 		if (dev1 == NULL && dev2 == NULL) {
504*4882a593Smuzhiyun 			DHD_ERROR(("no wifi platform data, skip\n"));
505*4882a593Smuzhiyun 			return -ENXIO;
506*4882a593Smuzhiyun 		}
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun #endif /* !defined(CONFIG_DTS) */
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	/* multi-chip support not enabled, build one adapter information for
511*4882a593Smuzhiyun 	 * DHD (either SDIO, USB or PCIe)
512*4882a593Smuzhiyun 	 */
513*4882a593Smuzhiyun 	adapter = kzalloc(sizeof(wifi_adapter_info_t), GFP_KERNEL);
514*4882a593Smuzhiyun 	if (adapter == NULL) {
515*4882a593Smuzhiyun 		DHD_ERROR(("%s:adapter alloc failed", __FUNCTION__));
516*4882a593Smuzhiyun 		return ENOMEM;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 	adapter->name = "DHD generic adapter";
519*4882a593Smuzhiyun 	adapter->bus_type = -1;
520*4882a593Smuzhiyun 	adapter->bus_num = -1;
521*4882a593Smuzhiyun 	adapter->slot_num = -1;
522*4882a593Smuzhiyun 	adapter->irq_num = -1;
523*4882a593Smuzhiyun 	is_power_on = FALSE;
524*4882a593Smuzhiyun 	wifi_plat_dev_probe_ret = 0;
525*4882a593Smuzhiyun 	dhd_wifi_platdata = kzalloc(sizeof(bcmdhd_wifi_platdata_t), GFP_KERNEL);
526*4882a593Smuzhiyun 	dhd_wifi_platdata->num_adapters = 1;
527*4882a593Smuzhiyun 	dhd_wifi_platdata->adapters = adapter;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	if (dev1) {
530*4882a593Smuzhiyun 		err = platform_driver_register(&wifi_platform_dev_driver);
531*4882a593Smuzhiyun 		if (err) {
532*4882a593Smuzhiyun 			DHD_ERROR(("%s: failed to register wifi ctrl func driver\n",
533*4882a593Smuzhiyun 				__FUNCTION__));
534*4882a593Smuzhiyun 			return err;
535*4882a593Smuzhiyun 		}
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 	if (dev2) {
538*4882a593Smuzhiyun 		err = platform_driver_register(&wifi_platform_dev_driver_legacy);
539*4882a593Smuzhiyun 		if (err) {
540*4882a593Smuzhiyun 			DHD_ERROR(("%s: failed to register wifi ctrl func legacy driver\n",
541*4882a593Smuzhiyun 				__FUNCTION__));
542*4882a593Smuzhiyun 			return err;
543*4882a593Smuzhiyun 		}
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun #if !defined(CONFIG_DTS)
546*4882a593Smuzhiyun 	if (dts_enabled) {
547*4882a593Smuzhiyun 		struct resource *resource;
548*4882a593Smuzhiyun #ifdef DHD_CUSTOM_PLAT_DATA
549*4882a593Smuzhiyun         wifi_plat_dev_probe_ret = dhd_wlan_init_plat_data();
550*4882a593Smuzhiyun 		if (wifi_plat_dev_probe_ret) {
551*4882a593Smuzhiyun 			return wifi_plat_dev_probe_ret;
552*4882a593Smuzhiyun         }
553*4882a593Smuzhiyun #endif /* DHD_CUSTOM_PLAT_DATA */
554*4882a593Smuzhiyun 		adapter->wifi_plat_data = (void *)&dhd_wlan_control;
555*4882a593Smuzhiyun 		resource = &dhd_wlan_resources;
556*4882a593Smuzhiyun 		adapter->irq_num = resource->start;
557*4882a593Smuzhiyun 		adapter->intr_flags = resource->flags & IRQF_TRIGGER_MASK;
558*4882a593Smuzhiyun #ifdef DHD_ISR_NO_SUSPEND
559*4882a593Smuzhiyun 		adapter->intr_flags |= IRQF_NO_SUSPEND;
560*4882a593Smuzhiyun #endif // endif
561*4882a593Smuzhiyun 		wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun #endif /* !defined(CONFIG_DTS) */
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun #ifdef CONFIG_DTS
566*4882a593Smuzhiyun 	wifi_plat_dev_probe_ret = platform_driver_register(&wifi_platform_dev_driver);
567*4882a593Smuzhiyun #endif /* CONFIG_DTS */
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* return probe function's return value if registeration succeeded */
570*4882a593Smuzhiyun 	return wifi_plat_dev_probe_ret;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
wifi_ctrlfunc_unregister_drv(void)573*4882a593Smuzhiyun void wifi_ctrlfunc_unregister_drv(void)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun #ifdef CONFIG_DTS
577*4882a593Smuzhiyun 	DHD_ERROR(("unregister wifi platform drivers\n"));
578*4882a593Smuzhiyun 	platform_driver_unregister(&wifi_platform_dev_driver);
579*4882a593Smuzhiyun #else
580*4882a593Smuzhiyun 	struct device *dev1, *dev2;
581*4882a593Smuzhiyun 	dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
582*4882a593Smuzhiyun 	dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
583*4882a593Smuzhiyun 	if (!dts_enabled)
584*4882a593Smuzhiyun 		if (dev1 == NULL && dev2 == NULL)
585*4882a593Smuzhiyun 			return;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	DHD_ERROR(("unregister wifi platform drivers\n"));
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	if (dev1)
590*4882a593Smuzhiyun 		platform_driver_unregister(&wifi_platform_dev_driver);
591*4882a593Smuzhiyun 	if (dev2)
592*4882a593Smuzhiyun 		platform_driver_unregister(&wifi_platform_dev_driver_legacy);
593*4882a593Smuzhiyun 	if (dts_enabled) {
594*4882a593Smuzhiyun 		wifi_adapter_info_t *adapter;
595*4882a593Smuzhiyun 		adapter = &dhd_wifi_platdata->adapters[0];
596*4882a593Smuzhiyun 		if (is_power_on) {
597*4882a593Smuzhiyun 			wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
598*4882a593Smuzhiyun 			wifi_platform_bus_enumerate(adapter, FALSE);
599*4882a593Smuzhiyun 		}
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun #ifdef BOARD_HIKEY_MODULAR
602*4882a593Smuzhiyun 	dhd_wlan_deinit();
603*4882a593Smuzhiyun #endif /* BOARD_HIKEY_MODULAR */
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun #ifdef DHD_CUSTOM_PLAT_DATA
606*4882a593Smuzhiyun     dhd_wlan_deinit_plat_data();
607*4882a593Smuzhiyun #endif /* DHD_CUSTOM_PLAT_DATA */
608*4882a593Smuzhiyun #endif /* !defined(CONFIG_DTS) */
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	kfree(dhd_wifi_platdata->adapters);
611*4882a593Smuzhiyun 	dhd_wifi_platdata->adapters = NULL;
612*4882a593Smuzhiyun 	dhd_wifi_platdata->num_adapters = 0;
613*4882a593Smuzhiyun 	kfree(dhd_wifi_platdata);
614*4882a593Smuzhiyun 	dhd_wifi_platdata = NULL;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
bcmdhd_wifi_plat_dev_drv_probe(struct platform_device * pdev)617*4882a593Smuzhiyun static int bcmdhd_wifi_plat_dev_drv_probe(struct platform_device *pdev)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	dhd_wifi_platdata = (bcmdhd_wifi_platdata_t *)(pdev->dev.platform_data);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	return dhd_wifi_platform_load();
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
bcmdhd_wifi_plat_dev_drv_remove(struct platform_device * pdev)624*4882a593Smuzhiyun static int bcmdhd_wifi_plat_dev_drv_remove(struct platform_device *pdev)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	int i;
627*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
628*4882a593Smuzhiyun 	ASSERT(dhd_wifi_platdata != NULL);
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	/* power down all adapters */
631*4882a593Smuzhiyun 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
632*4882a593Smuzhiyun 		adapter = &dhd_wifi_platdata->adapters[i];
633*4882a593Smuzhiyun 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
634*4882a593Smuzhiyun 		wifi_platform_bus_enumerate(adapter, FALSE);
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 	return 0;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun static struct platform_driver dhd_wifi_platform_dev_driver = {
640*4882a593Smuzhiyun 	.probe          = bcmdhd_wifi_plat_dev_drv_probe,
641*4882a593Smuzhiyun 	.remove         = bcmdhd_wifi_plat_dev_drv_remove,
642*4882a593Smuzhiyun 	.driver         = {
643*4882a593Smuzhiyun 	.name   = WIFI_PLAT_EXT,
644*4882a593Smuzhiyun 	}
645*4882a593Smuzhiyun };
646*4882a593Smuzhiyun 
dhd_wifi_platform_register_drv(void)647*4882a593Smuzhiyun int dhd_wifi_platform_register_drv(void)
648*4882a593Smuzhiyun {
649*4882a593Smuzhiyun 	int err = 0;
650*4882a593Smuzhiyun 	struct device *dev;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	/* register Broadcom wifi platform data driver if multi-chip is enabled,
653*4882a593Smuzhiyun 	 * otherwise use Android style wifi platform data (aka wifi control function)
654*4882a593Smuzhiyun 	 * if it exists
655*4882a593Smuzhiyun 	 *
656*4882a593Smuzhiyun 	 * to support multi-chip DHD, Broadcom wifi platform data device must
657*4882a593Smuzhiyun 	 * be added in kernel early boot (e.g. board config file).
658*4882a593Smuzhiyun 	 */
659*4882a593Smuzhiyun 	if (cfg_multichip) {
660*4882a593Smuzhiyun 		dev = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_EXT, wifi_platdev_match);
661*4882a593Smuzhiyun 		if (dev == NULL) {
662*4882a593Smuzhiyun 			DHD_ERROR(("bcmdhd wifi platform data device not found!!\n"));
663*4882a593Smuzhiyun 			return -ENXIO;
664*4882a593Smuzhiyun 		}
665*4882a593Smuzhiyun 		err = platform_driver_register(&dhd_wifi_platform_dev_driver);
666*4882a593Smuzhiyun 	} else {
667*4882a593Smuzhiyun 		err = wifi_ctrlfunc_register_drv();
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 		/* no wifi ctrl func either, load bus directly and ignore this error */
670*4882a593Smuzhiyun 		if (err) {
671*4882a593Smuzhiyun 			if (err == -ENXIO) {
672*4882a593Smuzhiyun 				/* wifi ctrl function does not exist */
673*4882a593Smuzhiyun 				err = dhd_wifi_platform_load();
674*4882a593Smuzhiyun 			} else {
675*4882a593Smuzhiyun 				/* unregister driver due to initialization failure */
676*4882a593Smuzhiyun 				wifi_ctrlfunc_unregister_drv();
677*4882a593Smuzhiyun 			}
678*4882a593Smuzhiyun 		}
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	return err;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun #ifdef BCMPCIE
dhd_wifi_platform_load_pcie(void)685*4882a593Smuzhiyun static int dhd_wifi_platform_load_pcie(void)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	int err = 0;
688*4882a593Smuzhiyun 	int i;
689*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	BCM_REFERENCE(i);
692*4882a593Smuzhiyun 	BCM_REFERENCE(adapter);
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	if (dhd_wifi_platdata == NULL) {
695*4882a593Smuzhiyun 		err = dhd_bus_register();
696*4882a593Smuzhiyun 	} else {
697*4882a593Smuzhiyun 		if (dhd_download_fw_on_driverload) {
698*4882a593Smuzhiyun 			/* power up all adapters */
699*4882a593Smuzhiyun 			for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
700*4882a593Smuzhiyun 				int retry = POWERUP_MAX_RETRY;
701*4882a593Smuzhiyun 				adapter = &dhd_wifi_platdata->adapters[i];
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 				DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
704*4882a593Smuzhiyun 				DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
705*4882a593Smuzhiyun 					adapter->irq_num, adapter->intr_flags, adapter->fw_path,
706*4882a593Smuzhiyun 					adapter->nv_path));
707*4882a593Smuzhiyun 				DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
708*4882a593Smuzhiyun 					adapter->bus_type, adapter->bus_num, adapter->slot_num));
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 				do {
711*4882a593Smuzhiyun 					err = wifi_platform_set_power(adapter,
712*4882a593Smuzhiyun 						TRUE, WIFI_TURNON_DELAY);
713*4882a593Smuzhiyun 					if (err) {
714*4882a593Smuzhiyun 						DHD_ERROR(("failed to power up %s,"
715*4882a593Smuzhiyun 							" %d retry left\n",
716*4882a593Smuzhiyun 							adapter->name, retry));
717*4882a593Smuzhiyun 						/* WL_REG_ON state unknown, Power off forcely */
718*4882a593Smuzhiyun 						wifi_platform_set_power(adapter,
719*4882a593Smuzhiyun 							FALSE, WIFI_TURNOFF_DELAY);
720*4882a593Smuzhiyun 						continue;
721*4882a593Smuzhiyun 					} else {
722*4882a593Smuzhiyun 						err = wifi_platform_bus_enumerate(adapter, TRUE);
723*4882a593Smuzhiyun 						if (err) {
724*4882a593Smuzhiyun 							DHD_ERROR(("failed to enumerate bus %s, "
725*4882a593Smuzhiyun 								"%d retry left\n",
726*4882a593Smuzhiyun 								adapter->name, retry));
727*4882a593Smuzhiyun 							wifi_platform_set_power(adapter, FALSE,
728*4882a593Smuzhiyun 								WIFI_TURNOFF_DELAY);
729*4882a593Smuzhiyun 						} else {
730*4882a593Smuzhiyun 							break;
731*4882a593Smuzhiyun 						}
732*4882a593Smuzhiyun 					}
733*4882a593Smuzhiyun 				} while (retry--);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 				if (retry < 0) {
736*4882a593Smuzhiyun 					DHD_ERROR(("failed to power up %s, max retry reached**\n",
737*4882a593Smuzhiyun 						adapter->name));
738*4882a593Smuzhiyun 					return -ENODEV;
739*4882a593Smuzhiyun 				}
740*4882a593Smuzhiyun 			}
741*4882a593Smuzhiyun 		}
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 		err = dhd_bus_register();
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		if (err) {
746*4882a593Smuzhiyun 			DHD_ERROR(("%s: pcie_register_driver failed\n", __FUNCTION__));
747*4882a593Smuzhiyun 			if (dhd_download_fw_on_driverload) {
748*4882a593Smuzhiyun 				/* power down all adapters */
749*4882a593Smuzhiyun 				for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
750*4882a593Smuzhiyun 					adapter = &dhd_wifi_platdata->adapters[i];
751*4882a593Smuzhiyun 					wifi_platform_bus_enumerate(adapter, FALSE);
752*4882a593Smuzhiyun 					wifi_platform_set_power(adapter,
753*4882a593Smuzhiyun 						FALSE, WIFI_TURNOFF_DELAY);
754*4882a593Smuzhiyun 				}
755*4882a593Smuzhiyun 			}
756*4882a593Smuzhiyun 		}
757*4882a593Smuzhiyun 	}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	return err;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun #else
dhd_wifi_platform_load_pcie(void)762*4882a593Smuzhiyun static int dhd_wifi_platform_load_pcie(void)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun 	return 0;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun #endif /* BCMPCIE  */
767*4882a593Smuzhiyun 
dhd_wifi_platform_unregister_drv(void)768*4882a593Smuzhiyun void dhd_wifi_platform_unregister_drv(void)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	if (cfg_multichip)
771*4882a593Smuzhiyun 		platform_driver_unregister(&dhd_wifi_platform_dev_driver);
772*4882a593Smuzhiyun 	else
773*4882a593Smuzhiyun 		wifi_ctrlfunc_unregister_drv();
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun extern int dhd_watchdog_prio;
777*4882a593Smuzhiyun extern int dhd_dpc_prio;
778*4882a593Smuzhiyun extern uint dhd_deferred_tx;
779*4882a593Smuzhiyun #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
780*4882a593Smuzhiyun extern struct semaphore dhd_registration_sem;
781*4882a593Smuzhiyun #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun #ifdef BCMSDIO
dhd_wifi_platform_load_sdio(void)784*4882a593Smuzhiyun static int dhd_wifi_platform_load_sdio(void)
785*4882a593Smuzhiyun {
786*4882a593Smuzhiyun 	int i;
787*4882a593Smuzhiyun 	int err = 0;
788*4882a593Smuzhiyun 	wifi_adapter_info_t *adapter;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	BCM_REFERENCE(i);
791*4882a593Smuzhiyun 	BCM_REFERENCE(adapter);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/* Sanity check on the module parameters
794*4882a593Smuzhiyun 	 * - Both watchdog and DPC as tasklets are ok
795*4882a593Smuzhiyun 	 * - If both watchdog and DPC are threads, TX must be deferred
796*4882a593Smuzhiyun 	 */
797*4882a593Smuzhiyun 	if (!(dhd_watchdog_prio < 0 && dhd_dpc_prio < 0) &&
798*4882a593Smuzhiyun 		!(dhd_watchdog_prio >= 0 && dhd_dpc_prio >= 0 && dhd_deferred_tx))
799*4882a593Smuzhiyun 		return -EINVAL;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
802*4882a593Smuzhiyun 	sema_init(&dhd_registration_sem, 0);
803*4882a593Smuzhiyun #endif // endif
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	if (dhd_wifi_platdata == NULL) {
806*4882a593Smuzhiyun 		DHD_ERROR(("DHD wifi platform data is required for Android build\n"));
807*4882a593Smuzhiyun 		DHD_ERROR(("DHD registering bus directly\n"));
808*4882a593Smuzhiyun 		/* x86 bring-up PC needs no power-up operations */
809*4882a593Smuzhiyun 		err = dhd_bus_register();
810*4882a593Smuzhiyun 		return err;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
814*4882a593Smuzhiyun 	/* power up all adapters */
815*4882a593Smuzhiyun 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
816*4882a593Smuzhiyun 		bool chip_up = FALSE;
817*4882a593Smuzhiyun 		int retry = POWERUP_MAX_RETRY;
818*4882a593Smuzhiyun 		struct semaphore dhd_chipup_sem;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 		adapter = &dhd_wifi_platdata->adapters[i];
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
823*4882a593Smuzhiyun 		DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
824*4882a593Smuzhiyun 			adapter->irq_num, adapter->intr_flags, adapter->fw_path, adapter->nv_path));
825*4882a593Smuzhiyun 		DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
826*4882a593Smuzhiyun 			adapter->bus_type, adapter->bus_num, adapter->slot_num));
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 		do {
829*4882a593Smuzhiyun 			sema_init(&dhd_chipup_sem, 0);
830*4882a593Smuzhiyun 			err = dhd_bus_reg_sdio_notify(&dhd_chipup_sem);
831*4882a593Smuzhiyun 			if (err) {
832*4882a593Smuzhiyun 				DHD_ERROR(("%s dhd_bus_reg_sdio_notify fail(%d)\n\n",
833*4882a593Smuzhiyun 					__FUNCTION__, err));
834*4882a593Smuzhiyun 				return err;
835*4882a593Smuzhiyun 			}
836*4882a593Smuzhiyun 			err = wifi_platform_set_power(adapter, TRUE, WIFI_TURNON_DELAY);
837*4882a593Smuzhiyun 			if (err) {
838*4882a593Smuzhiyun 				DHD_ERROR(("%s: wifi pwr on error ! \n", __FUNCTION__));
839*4882a593Smuzhiyun 				dhd_bus_unreg_sdio_notify();
840*4882a593Smuzhiyun 				/* WL_REG_ON state unknown, Power off forcely */
841*4882a593Smuzhiyun 				wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
842*4882a593Smuzhiyun 				continue;
843*4882a593Smuzhiyun 			} else {
844*4882a593Smuzhiyun 				wifi_platform_bus_enumerate(adapter, TRUE);
845*4882a593Smuzhiyun 			}
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 			if (down_timeout(&dhd_chipup_sem, msecs_to_jiffies(POWERUP_WAIT_MS)) == 0) {
848*4882a593Smuzhiyun 				dhd_bus_unreg_sdio_notify();
849*4882a593Smuzhiyun 				chip_up = TRUE;
850*4882a593Smuzhiyun 				break;
851*4882a593Smuzhiyun 			}
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 			DHD_ERROR(("failed to power up %s, %d retry left\n", adapter->name, retry));
854*4882a593Smuzhiyun 			dhd_bus_unreg_sdio_notify();
855*4882a593Smuzhiyun 			wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
856*4882a593Smuzhiyun 			wifi_platform_bus_enumerate(adapter, FALSE);
857*4882a593Smuzhiyun 		} while (retry--);
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 		if (!chip_up) {
860*4882a593Smuzhiyun 			DHD_ERROR(("failed to power up %s, max retry reached**\n", adapter->name));
861*4882a593Smuzhiyun 			return -ENODEV;
862*4882a593Smuzhiyun 		}
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	err = dhd_bus_register();
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (err) {
869*4882a593Smuzhiyun 		DHD_ERROR(("%s: sdio_register_driver failed\n", __FUNCTION__));
870*4882a593Smuzhiyun 		goto fail;
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	/*
874*4882a593Smuzhiyun 	 * Wait till MMC sdio_register_driver callback called and made driver attach.
875*4882a593Smuzhiyun 	 * It's needed to make sync up exit from dhd insmod  and
876*4882a593Smuzhiyun 	 * Kernel MMC sdio device callback registration
877*4882a593Smuzhiyun 	 */
878*4882a593Smuzhiyun 	err = down_timeout(&dhd_registration_sem, msecs_to_jiffies(DHD_REGISTRATION_TIMEOUT));
879*4882a593Smuzhiyun 	if (err) {
880*4882a593Smuzhiyun 		DHD_ERROR(("%s: sdio_register_driver timeout or error \n", __FUNCTION__));
881*4882a593Smuzhiyun 		dhd_bus_unregister();
882*4882a593Smuzhiyun 		goto fail;
883*4882a593Smuzhiyun 	}
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	return err;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun fail:
888*4882a593Smuzhiyun 	/* power down all adapters */
889*4882a593Smuzhiyun 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
890*4882a593Smuzhiyun 		adapter = &dhd_wifi_platdata->adapters[i];
891*4882a593Smuzhiyun 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
892*4882a593Smuzhiyun 		wifi_platform_bus_enumerate(adapter, FALSE);
893*4882a593Smuzhiyun 	}
894*4882a593Smuzhiyun #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	return err;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun #else /* BCMSDIO */
dhd_wifi_platform_load_sdio(void)899*4882a593Smuzhiyun static int dhd_wifi_platform_load_sdio(void)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	return 0;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun #endif /* BCMSDIO */
904*4882a593Smuzhiyun 
dhd_wifi_platform_load_usb(void)905*4882a593Smuzhiyun static int dhd_wifi_platform_load_usb(void)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	return 0;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
dhd_wifi_platform_load()910*4882a593Smuzhiyun static int dhd_wifi_platform_load()
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	int err = 0;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun #if defined(OEM_ANDROID)
915*4882a593Smuzhiyun 		wl_android_init();
916*4882a593Smuzhiyun #endif /* OEM_ANDROID */
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	if ((err = dhd_wifi_platform_load_usb()))
919*4882a593Smuzhiyun 		goto end;
920*4882a593Smuzhiyun 	else if ((err = dhd_wifi_platform_load_sdio()))
921*4882a593Smuzhiyun 		goto end;
922*4882a593Smuzhiyun 	else
923*4882a593Smuzhiyun 		err = dhd_wifi_platform_load_pcie();
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun end:
926*4882a593Smuzhiyun #if defined(OEM_ANDROID)
927*4882a593Smuzhiyun 	if (err)
928*4882a593Smuzhiyun 		wl_android_exit();
929*4882a593Smuzhiyun 	else
930*4882a593Smuzhiyun 		wl_android_post_init();
931*4882a593Smuzhiyun #endif /* OEM_ANDROID */
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	return err;
934*4882a593Smuzhiyun }
935