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