xref: /OK3568_Linux_fs/kernel/net/rfkill/rfkill-wlan.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2012 ROCKCHIP, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 /* Rock-chips rfkill driver for wifi
15 */
16 
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/module.h>
20 #include <linux/rfkill.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/delay.h>
25 #include <linux/rfkill-wlan.h>
26 #include <linux/rfkill-bt.h>
27 #include <linux/wakelock.h>
28 #include <linux/interrupt.h>
29 #include <asm/irq.h>
30 #include <linux/suspend.h>
31 #include <linux/proc_fs.h>
32 #include <linux/uaccess.h>
33 #include <linux/gpio.h>
34 #include <dt-bindings/gpio/gpio.h>
35 #include <linux/skbuff.h>
36 #include <linux/fb.h>
37 #include <linux/rockchip/grf.h>
38 #include <linux/regmap.h>
39 #include <linux/mfd/syscon.h>
40 #include <linux/mmc/host.h>
41 #ifdef CONFIG_OF
42 #include <linux/of.h>
43 #include <linux/of_device.h>
44 #include <linux/of_gpio.h>
45 #endif
46 #include <linux/soc/rockchip/rk_vendor_storage.h>
47 #include <linux/device.h>
48 
49 #include "../../drivers/mmc/core/pwrseq.h"
50 
51 #if 0
52 #define DBG(x...) pr_info("[WLAN_RFKILL]: " x)
53 #else
54 #define DBG(x...)
55 #endif
56 
57 #define LOG(x...) pr_info("[WLAN_RFKILL]: " x)
58 
59 struct rfkill_wlan_data {
60 	struct rksdmmc_gpio_wifi_moudle *pdata;
61 	struct wake_lock wlan_irq_wl;
62 };
63 
64 static struct rfkill_wlan_data *g_rfkill = NULL;
65 static int power_set_time = 0;
66 static int wifi_bt_vbat_state;
67 static int wifi_power_state;
68 
69 static const char wlan_name[] = "rkwifi";
70 
71 static char wifi_chip_type_string[64];
72 /***********************************************************
73  *
74  * Broadcom Wifi Static Memory
75  *
76  **********************************************************/
77 #ifdef CONFIG_RKWIFI
78 #define BCM_STATIC_MEMORY_SUPPORT 0
79 #else
80 #define BCM_STATIC_MEMORY_SUPPORT 0
81 #endif
82 //===========================
83 #if BCM_STATIC_MEMORY_SUPPORT
84 #define PREALLOC_WLAN_SEC_NUM 4
85 #define PREALLOC_WLAN_BUF_NUM 160
86 #define PREALLOC_WLAN_SECTION_HEADER 0
87 #define WLAN_SKB_BUF_NUM 16
88 
89 #define WLAN_SECTION_SIZE_0 (12 * 1024)
90 #define WLAN_SECTION_SIZE_1 (12 * 1024)
91 #define WLAN_SECTION_SIZE_2 (32 * 1024)
92 #define WLAN_SECTION_SIZE_3 (136 * 1024)
93 #define WLAN_SECTION_SIZE_4 (4 * 1024)
94 #define WLAN_SECTION_SIZE_5 (64 * 1024)
95 #define WLAN_SECTION_SIZE_6 (4 * 1024)
96 #define WLAN_SECTION_SIZE_7 (4 * 1024)
97 
98 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM + 1];
99 
100 struct wifi_mem_prealloc {
101 	void *mem_ptr;
102 	unsigned long size;
103 };
104 
105 static struct wifi_mem_prealloc wifi_mem_array[8] = {
106 	{ NULL, (WLAN_SECTION_SIZE_0) }, { NULL, (WLAN_SECTION_SIZE_1) },
107 	{ NULL, (WLAN_SECTION_SIZE_2) }, { NULL, (WLAN_SECTION_SIZE_3) },
108 	{ NULL, (WLAN_SECTION_SIZE_4) }, { NULL, (WLAN_SECTION_SIZE_5) },
109 	{ NULL, (WLAN_SECTION_SIZE_6) }, { NULL, (WLAN_SECTION_SIZE_7) }
110 };
111 
rockchip_init_wifi_mem(void)112 static int rockchip_init_wifi_mem(void)
113 {
114 	int i;
115 	int j;
116 
117 	for (i = 0; i < WLAN_SKB_BUF_NUM; i++) {
118 		wlan_static_skb[i] =
119 			dev_alloc_skb(((i < (WLAN_SKB_BUF_NUM / 2)) ?
120 				(PAGE_SIZE * 1) :
121 				(PAGE_SIZE * 2)));
122 
123 		if (!wlan_static_skb[i])
124 			goto err_skb_alloc;
125 	}
126 
127 	wlan_static_skb[i] = dev_alloc_skb((PAGE_SIZE * 4));
128 	if (!wlan_static_skb[i])
129 		goto err_skb_alloc;
130 
131 	for (i = 0; i <= 7; i++) {
132 		wifi_mem_array[i].mem_ptr =
133 			kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
134 
135 		if (!wifi_mem_array[i].mem_ptr)
136 			goto err_mem_alloc;
137 	}
138 	return 0;
139 
140 err_mem_alloc:
141 	pr_err("Failed to mem_alloc for WLAN\n");
142 	for (j = 0; j < i; j++)
143 		kfree(wifi_mem_array[j].mem_ptr);
144 	i = WLAN_SKB_BUF_NUM;
145 err_skb_alloc:
146 	pr_err("Failed to skb_alloc for WLAN\n");
147 	for (j = 0; j < i; j++)
148 		dev_kfree_skb(wlan_static_skb[j]);
149 	dev_kfree_skb(wlan_static_skb[j]);
150 
151 	return -ENOMEM;
152 }
153 
rockchip_mem_prealloc(int section,unsigned long size)154 void *rockchip_mem_prealloc(int section, unsigned long size)
155 {
156 	if (section == PREALLOC_WLAN_SEC_NUM)
157 		return wlan_static_skb;
158 
159 	if (section < 0 || section > 7)
160 		return NULL;
161 
162 	if (wifi_mem_array[section].size < size)
163 		return NULL;
164 
165 	return wifi_mem_array[section].mem_ptr;
166 }
167 #else
rockchip_mem_prealloc(int section,unsigned long size)168 void *rockchip_mem_prealloc(int section, unsigned long size)
169 {
170 	return NULL;
171 }
172 #endif
173 EXPORT_SYMBOL(rockchip_mem_prealloc);
174 
rfkill_set_wifi_bt_power(int on)175 int rfkill_set_wifi_bt_power(int on)
176 {
177 	struct rfkill_wlan_data *mrfkill = g_rfkill;
178 	struct rksdmmc_gpio *vbat;
179 
180 	LOG("%s: %d\n", __func__, on);
181 
182 	if (!mrfkill) {
183 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
184 		    __func__);
185 		return -1;
186 	}
187 
188 	vbat = &mrfkill->pdata->vbat_n;
189 	if (on) {
190 		if (gpio_is_valid(vbat->io))
191 			gpio_direction_output(vbat->io, vbat->enable);
192 	} else {
193 		if (gpio_is_valid(vbat->io))
194 			gpio_direction_output(vbat->io, !(vbat->enable));
195 	}
196 	wifi_bt_vbat_state = on;
197 	return 0;
198 }
199 
200 /**************************************************************************
201  *
202  * get wifi power state Func
203  *
204  *************************************************************************/
rfkill_get_wifi_power_state(int * power)205 int rfkill_get_wifi_power_state(int *power)
206 {
207 	struct rfkill_wlan_data *mrfkill = g_rfkill;
208 
209 	if (!mrfkill) {
210 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
211 		    __func__);
212 		return -1;
213 	}
214 
215 	*power = wifi_power_state;
216 
217 	return 0;
218 }
219 EXPORT_SYMBOL(rfkill_get_wifi_power_state);
220 
221 /**************************************************************************
222  *
223  * Wifi Power Control Func
224  * 0 -> power off
225  * 1 -> power on
226  *
227  *************************************************************************/
rockchip_wifi_power(int on)228 int rockchip_wifi_power(int on)
229 {
230 	struct rfkill_wlan_data *mrfkill = g_rfkill;
231 	struct rksdmmc_gpio *poweron, *reset;
232 	struct regulator *ldo = NULL;
233 	int bt_power = 0;
234 	bool toggle = false;
235 
236 	LOG("%s: %d\n", __func__, on);
237 
238 	if (!mrfkill) {
239 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
240 		    __func__);
241 		return -1;
242 	}
243 
244 	if (mrfkill->pdata->wifi_power_remain && power_set_time) {
245 		LOG("%s: wifi power is setted to be remain on.", __func__);
246 		return 0;
247 	}
248 	power_set_time++;
249 
250 	if (!rfkill_get_bt_power_state(&bt_power, &toggle)) {
251 		LOG("%s: toggle = %s\n", __func__, toggle ? "true" : "false");
252 	}
253 
254 	if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
255 		int ret = -1;
256 		char *ldostr;
257 		int level = mrfkill->pdata->mregulator.enable;
258 
259 		ldostr = mrfkill->pdata->mregulator.pmu_regulator;
260 		if (!ldostr)
261 			return -1;
262 		ldo = regulator_get(NULL, ldostr);
263 		if (!ldo || IS_ERR(ldo)) {
264 			LOG("\n\n\n%s get ldo error,please mod this\n\n\n",
265 			    __func__);
266 			return -1;
267 		}
268 		if (on == level) {
269 			regulator_set_voltage(ldo, 3000000, 3000000);
270 			LOG("%s: %s enabled\n", __func__, ldostr);
271 			ret = regulator_enable(ldo);
272 			if (ret)
273 				LOG("ldo enable failed\n");
274 			wifi_power_state = 1;
275 			LOG("wifi turn on power.\n");
276 		} else {
277 			LOG("%s: %s disabled\n", __func__, ldostr);
278 			while (regulator_is_enabled(ldo) > 0) {
279 				ret = regulator_disable(ldo);
280 				if (ret)
281 					LOG("ldo disable failed\n");
282 			}
283 			wifi_power_state = 0;
284 			LOG("wifi shut off power.\n");
285 		}
286 		regulator_put(ldo);
287 		msleep(100);
288 	} else {
289 		poweron = &mrfkill->pdata->power_n;
290 		reset = &mrfkill->pdata->reset_n;
291 
292 		if (on) {
293 			if (toggle) {
294 				rfkill_set_wifi_bt_power(1);
295 				msleep(100);
296 			}
297 
298 			if (gpio_is_valid(poweron->io)) {
299 				gpio_direction_output(poweron->io, poweron->enable);
300 				msleep(100);
301 			}
302 
303 			if (gpio_is_valid(reset->io)) {
304 				gpio_direction_output(reset->io, reset->enable);
305 				msleep(100);
306 			}
307 
308 			wifi_power_state = 1;
309 			LOG("wifi turn on power [GPIO%d-%d]\n", poweron->io, poweron->enable);
310 		} else {
311 			if (gpio_is_valid(poweron->io)) {
312 				printk("wifi power off\n");
313 				gpio_direction_output(poweron->io, !(poweron->enable));
314 				msleep(100);
315 			}
316 
317 			if (gpio_is_valid(reset->io)) {
318 				gpio_direction_output(reset->io, !(reset->enable));
319 			}
320 
321 			wifi_power_state = 0;
322 			if (toggle) {
323 				if (!bt_power) {
324 					LOG("%s: wifi will set vbat to low\n", __func__);
325 					rfkill_set_wifi_bt_power(0);
326 				} else {
327 					LOG("%s: wifi shouldn't control the vbat\n", __func__);
328 				}
329 			}
330 			LOG("wifi shut off power [GPIO%d-%d]\n", poweron->io, !poweron->enable);
331 		}
332 	}
333 
334 	return 0;
335 }
336 EXPORT_SYMBOL(rockchip_wifi_power);
337 
338 /**************************************************************************
339  *
340  * Wifi Sdio Detect Func
341  *
342  *************************************************************************/
rockchip_wifi_set_carddetect(int val)343 int rockchip_wifi_set_carddetect(int val)
344 {
345 	return 0;
346 }
347 EXPORT_SYMBOL(rockchip_wifi_set_carddetect);
348 
349 /**************************************************************************
350  *
351  * Wifi Get Interrupt irq Func
352  *
353  *************************************************************************/
rockchip_wifi_get_oob_irq(void)354 int rockchip_wifi_get_oob_irq(void)
355 {
356 	struct rfkill_wlan_data *mrfkill = g_rfkill;
357 	struct rksdmmc_gpio *wifi_int_irq;
358 
359 	LOG("%s: Enter\n", __func__);
360 
361 	if (!mrfkill) {
362 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
363 		    __func__);
364 		return -1;
365 	}
366 
367 	wifi_int_irq = &mrfkill->pdata->wifi_int_b;
368 	if (gpio_is_valid(wifi_int_irq->io)) {
369 		return gpio_to_irq(wifi_int_irq->io);
370 		//return wifi_int_irq->io;
371 	} else {
372 		LOG("%s: wifi OOB pin isn't defined.\n", __func__);
373 	}
374 
375 	return -1;
376 }
377 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq);
378 
rockchip_wifi_get_oob_irq_flag(void)379 int rockchip_wifi_get_oob_irq_flag(void)
380 {
381 	struct rfkill_wlan_data *mrfkill = g_rfkill;
382 	struct rksdmmc_gpio *wifi_int_irq;
383 	int gpio_flags = -1;
384 
385 	if (mrfkill) {
386 		wifi_int_irq = &mrfkill->pdata->wifi_int_b;
387 		if (gpio_is_valid(wifi_int_irq->io))
388 			gpio_flags = wifi_int_irq->enable;
389 	}
390 
391 	return gpio_flags;
392 }
393 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq_flag);
394 
395 /**************************************************************************
396  *
397  * Wifi Reset Func
398  *
399  *************************************************************************/
rockchip_wifi_reset(int on)400 int rockchip_wifi_reset(int on)
401 {
402 	return 0;
403 }
404 EXPORT_SYMBOL(rockchip_wifi_reset);
405 
406 /**************************************************************************
407  *
408  * Wifi MAC custom Func
409  *
410  *************************************************************************/
411 #include <linux/etherdevice.h>
412 #include <linux/errno.h>
413 u8 wifi_custom_mac_addr[6] = { 0, 0, 0, 0, 0, 0 };
414 
415 //#define RANDOM_ADDRESS_SAVE
get_wifi_addr_vendor(unsigned char * addr)416 static int get_wifi_addr_vendor(unsigned char *addr)
417 {
418 	int ret;
419 	int count = 5;
420 
421 	while (count-- > 0) {
422 		if (is_rk_vendor_ready())
423 			break;
424 		/* sleep 500ms wait rk vendor driver ready */
425 		msleep(500);
426 	}
427 	ret = rk_vendor_read(WIFI_MAC_ID, addr, 6);
428 	if (ret != 6 || is_zero_ether_addr(addr)) {
429 		LOG("%s: rk_vendor_read wifi mac address failed (%d)\n",
430 		    __func__, ret);
431 #ifdef CONFIG_WIFI_GENERATE_RANDOM_MAC_ADDR
432 		random_ether_addr(addr);
433 		LOG("%s: generate random wifi mac address: "
434 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
435 		    __func__, addr[0], addr[1], addr[2], addr[3], addr[4],
436 		    addr[5]);
437 		ret = rk_vendor_write(WIFI_MAC_ID, addr, 6);
438 		if (ret != 0) {
439 			LOG("%s: rk_vendor_write failed %d\n",
440 			    __func__, ret);
441 			memset(addr, 0, 6);
442 			return -1;
443 		}
444 #else
445 		return -1;
446 #endif
447 	} else {
448 		LOG("%s: rk_vendor_read wifi mac address: "
449 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
450 		    __func__, addr[0], addr[1], addr[2], addr[3], addr[4],
451 		    addr[5]);
452 	}
453 	return 0;
454 }
455 
rockchip_wifi_mac_addr(unsigned char * buf)456 int rockchip_wifi_mac_addr(unsigned char *buf)
457 {
458 	char mac_buf[20] = { 0 };
459 
460 	LOG("%s: enter.\n", __func__);
461 
462 	// from vendor storage
463 	if (is_zero_ether_addr(wifi_custom_mac_addr)) {
464 		if (get_wifi_addr_vendor(wifi_custom_mac_addr) != 0)
465 			return -1;
466 	}
467 
468 	sprintf(mac_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
469 		wifi_custom_mac_addr[0], wifi_custom_mac_addr[1],
470 		wifi_custom_mac_addr[2], wifi_custom_mac_addr[3],
471 		wifi_custom_mac_addr[4], wifi_custom_mac_addr[5]);
472 	LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
473 
474 	if (is_valid_ether_addr(wifi_custom_mac_addr)) {
475 		if (!strncmp(wifi_chip_type_string, "rtl", 3))
476 			wifi_custom_mac_addr[0] &= ~0x2; // for p2p
477 	} else {
478 		LOG("This mac address is not valid, ignored...\n");
479 		return -1;
480 	}
481 
482 	memcpy(buf, wifi_custom_mac_addr, 6);
483 
484 	return 0;
485 }
486 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
487 
488 /**************************************************************************
489  *
490  * wifi get country code func
491  *
492  *************************************************************************/
493 struct cntry_locales_custom {
494 	char iso_abbrev[4]; /* ISO 3166-1 country abbreviation */
495 	char custom_locale[4]; /* Custom firmware locale */
496 	int custom_locale_rev; /* Custom local revisin default -1 */
497 };
498 
499 static struct cntry_locales_custom country_cloc;
500 
rockchip_wifi_country_code(char * ccode)501 void *rockchip_wifi_country_code(char *ccode)
502 {
503 	struct cntry_locales_custom *mcloc;
504 
505 	LOG("%s: set country code [%s]\n", __func__, ccode);
506 	mcloc = &country_cloc;
507 	memcpy(mcloc->custom_locale, ccode, 4);
508 	mcloc->custom_locale_rev = 0;
509 
510 	return mcloc;
511 }
512 EXPORT_SYMBOL(rockchip_wifi_country_code);
513 /**************************************************************************/
514 
rfkill_rk_setup_gpio(struct rksdmmc_gpio * gpio,const char * prefix,const char * name)515 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char *prefix,
516 				const char *name)
517 {
518 	if (gpio_is_valid(gpio->io)) {
519 		int ret = 0;
520 
521 		sprintf(gpio->name, "%s_%s", prefix, name);
522 		ret = gpio_request(gpio->io, gpio->name);
523 		if (ret) {
524 			LOG("Failed to get %s gpio.\n", gpio->name);
525 			return -1;
526 		}
527 	}
528 
529 	return 0;
530 }
531 
532 #ifdef CONFIG_OF
wlan_platdata_parse_dt(struct device * dev,struct rksdmmc_gpio_wifi_moudle * data)533 static int wlan_platdata_parse_dt(struct device *dev,
534 				  struct rksdmmc_gpio_wifi_moudle *data)
535 {
536 	struct device_node *node = dev->of_node;
537 	const char *strings;
538 	u32 value;
539 	int gpio, ret;
540 	enum of_gpio_flags flags;
541 	u32 ext_clk_value = 0;
542 
543 	if (!node)
544 		return -ENODEV;
545 
546 	memset(data, 0, sizeof(*data));
547 
548 #ifdef CONFIG_MFD_SYSCON
549 	data->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
550 	if (IS_ERR(data->grf)) {
551 		LOG("can't find rockchip,grf property\n");
552 		//return -1;
553 	}
554 #endif
555 
556 	ret = of_property_read_string(node, "wifi_chip_type", &strings);
557 	if (ret) {
558 		LOG("%s: Can not read wifi_chip_type, set default to rkwifi.\n",
559 		    __func__);
560 		strcpy(wifi_chip_type_string, "rkwifi");
561 	} else {
562 		if (strings && strlen(strings) < 64)
563 			strcpy(wifi_chip_type_string, strings);
564 	}
565 	LOG("%s: wifi_chip_type = %s\n", __func__, wifi_chip_type_string);
566 
567 	if (of_find_property(node, "keep_wifi_power_on", NULL)) {
568 		data->wifi_power_remain = true;
569 		LOG("%s: wifi power remain\n", __func__);
570 	} else {
571 		data->wifi_power_remain = false;
572 		LOG("%s: enable wifi power control.\n", __func__);
573 	}
574 
575 	if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
576 		data->mregulator.power_ctrl_by_pmu = true;
577 		ret = of_property_read_string(node, "power_pmu_regulator",
578 					      &strings);
579 		if (ret) {
580 			LOG("%s: Can not read property: power_pmu_regulator.\n",
581 			    __func__);
582 			data->mregulator.power_ctrl_by_pmu = false;
583 		} else {
584 			LOG("%s: wifi power controlled by pmu(%s).\n", __func__,
585 			    strings);
586 			sprintf(data->mregulator.pmu_regulator, "%s", strings);
587 		}
588 		ret = of_property_read_u32(node, "power_pmu_enable_level",
589 					   &value);
590 		if (ret) {
591 			LOG("%s: Can not read: power_pmu_enable_level.\n",
592 			    __func__);
593 			data->mregulator.power_ctrl_by_pmu = false;
594 		} else {
595 			LOG("%s: wifi power controlled by pmu(level = %s).\n",
596 			    __func__, (value == 1) ? "HIGH" : "LOW");
597 			data->mregulator.enable = value;
598 		}
599 	} else {
600 		data->mregulator.power_ctrl_by_pmu = false;
601 		LOG("%s: wifi power controled by gpio.\n", __func__);
602 		gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0,
603 					       &flags);
604 		if (gpio_is_valid(gpio)) {
605 			data->power_n.io = gpio;
606 			data->power_n.enable =
607 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
608 			LOG("%s: WIFI,poweren_gpio = %d flags = %d.\n",
609 			    __func__, gpio, flags);
610 		} else {
611 			data->power_n.io = -1;
612 		}
613 		gpio = of_get_named_gpio_flags(node, "WIFI,vbat_gpio", 0,
614 					       &flags);
615 		if (gpio_is_valid(gpio)) {
616 			data->vbat_n.io = gpio;
617 			data->vbat_n.enable =
618 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
619 			LOG("%s: WIFI,vbat_gpio = %d, flags = %d.\n",
620 			    __func__, gpio, flags);
621 		} else {
622 			data->vbat_n.io = -1;
623 		}
624 		gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0,
625 					       &flags);
626 		if (gpio_is_valid(gpio)) {
627 			data->reset_n.io = gpio;
628 			data->reset_n.enable =
629 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
630 			LOG("%s: WIFI,reset_gpio = %d, flags = %d.\n",
631 			    __func__, gpio, flags);
632 		} else {
633 			data->reset_n.io = -1;
634 		}
635 		gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0,
636 					       &flags);
637 		if (gpio_is_valid(gpio)) {
638 			data->wifi_int_b.io = gpio;
639 			data->wifi_int_b.enable = !flags;
640 			LOG("%s: WIFI,host_wake_irq = %d, flags = %d.\n",
641 			    __func__, gpio, flags);
642 		} else {
643 			data->wifi_int_b.io = -1;
644 		}
645 	}
646 
647 	data->ext_clk = devm_clk_get(dev, "clk_wifi");
648 	if (IS_ERR(data->ext_clk)) {
649 		LOG("%s: The ref_wifi_clk not found !\n", __func__);
650 	} else {
651 		of_property_read_u32(node, "ref-clock-frequency",
652 				     &ext_clk_value);
653 		if (ext_clk_value > 0) {
654 			ret = clk_set_rate(data->ext_clk, ext_clk_value);
655 			if (ret)
656 				LOG("%s: set ref clk error!\n", __func__);
657 		}
658 
659 		ret = clk_prepare_enable(data->ext_clk);
660 		if (ret)
661 			LOG("%s: enable ref clk error!\n", __func__);
662 
663 		/* WIFI clock (REF_CLKOUT) output enable.
664 		 * 1'b0: drive disable
665 		 * 1'b1: output enable
666 		 */
667 		if (of_machine_is_compatible("rockchip,rk3308"))
668 			regmap_write(data->grf, 0x0314, 0x00020002);
669 	}
670 
671 	return 0;
672 }
673 #endif //CONFIG_OF
674 
675 #if defined(CONFIG_HAS_EARLYSUSPEND)
676 #include <linux/earlysuspend.h>
677 
wlan_early_suspend(struct early_suspend * h)678 static void wlan_early_suspend(struct early_suspend *h)
679 {
680 	LOG("%s :enter\n", __func__);
681 
682 	return;
683 }
684 
wlan_late_resume(struct early_suspend * h)685 static void wlan_late_resume(struct early_suspend *h)
686 {
687 	LOG("%s :enter\n", __func__);
688 
689 	return;
690 }
691 
692 struct early_suspend wlan_early_suspend {
693 	.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
694 	.suspend = wlan_early_suspend;
695 	.resume = wlan_late_resume;
696 }
697 #endif
698 
699 static void
rfkill_wlan_early_suspend(void)700 rfkill_wlan_early_suspend(void)
701 {
702 	//LOG("%s :enter\n", __func__);
703 
704 	return;
705 }
706 
rfkill_wlan_later_resume(void)707 static void rfkill_wlan_later_resume(void)
708 {
709 	//LOG("%s :enter\n", __func__);
710 
711 	return;
712 }
713 
rfkill_wlan_fb_event_notify(struct notifier_block * self,unsigned long action,void * data)714 static int rfkill_wlan_fb_event_notify(struct notifier_block *self,
715 				       unsigned long action, void *data)
716 {
717 	struct fb_event *event = data;
718 	int blank_mode = *((int *)event->data);
719 
720 	switch (blank_mode) {
721 	case FB_BLANK_UNBLANK:
722 		rfkill_wlan_later_resume();
723 		break;
724 	case FB_BLANK_NORMAL:
725 		rfkill_wlan_early_suspend();
726 		break;
727 	default:
728 		rfkill_wlan_early_suspend();
729 		break;
730 	}
731 
732 	return 0;
733 }
734 
735 static struct notifier_block rfkill_wlan_fb_notifier = {
736 	.notifier_call = rfkill_wlan_fb_event_notify,
737 };
738 
wifi_power_show(struct class * cls,struct class_attribute * attr,char * _buf)739 static ssize_t wifi_power_show(struct class *cls, struct class_attribute *attr, char *_buf)
740 {
741 	return sprintf(_buf, "%d\n", wifi_power_state);
742 }
743 
wifi_power_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)744 static ssize_t wifi_power_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
745 {
746 	long poweren = 0;
747 
748 	if (kstrtol(_buf, 10, &poweren) < 0)
749 		return -EINVAL;
750 
751 	LOG("%s: poweren = %ld\n", __func__, poweren);
752 
753 	if (poweren > 0)
754 		rockchip_wifi_power(1);
755 	else
756 		rockchip_wifi_power(0);
757 
758 	return _count;
759 }
760 
761 static CLASS_ATTR_RW(wifi_power);
762 
wifi_bt_vbat_show(struct class * cls,struct class_attribute * attr,char * _buf)763 static ssize_t wifi_bt_vbat_show(struct class *cls, struct class_attribute *attr, char *_buf)
764 {
765 	return sprintf(_buf, "%d\n", wifi_bt_vbat_state);
766 }
767 
wifi_bt_vbat_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)768 static ssize_t wifi_bt_vbat_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
769 {
770 	long vbat = 0;
771 
772 	if (kstrtol(_buf, 10, &vbat) < 0)
773 		return -EINVAL;
774 
775 	LOG("%s: vbat = %ld\n", __func__, vbat);
776 
777 	if (vbat > 0)
778 		rfkill_set_wifi_bt_power(1);
779 	else
780 		rfkill_set_wifi_bt_power(0);
781 
782 	return _count;
783 }
784 
785 static CLASS_ATTR_RW(wifi_bt_vbat);
786 
wifi_set_carddetect_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)787 static ssize_t wifi_set_carddetect_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
788 {
789 	long val = 0;
790 
791 	if (kstrtol(_buf, 10, &val) < 0)
792 		return -EINVAL;
793 
794 	LOG("%s: val = %ld\n", __func__, val);
795 
796 	if (val > 0)
797 		rockchip_wifi_set_carddetect(1);
798 	else
799 		rockchip_wifi_set_carddetect(0);
800 
801 	return _count;
802 }
803 
804 static CLASS_ATTR_WO(wifi_set_carddetect);
805 
806 static struct attribute *rkwifi_power_attrs[] = {
807 	&class_attr_wifi_power.attr,
808 	&class_attr_wifi_bt_vbat.attr,
809 	&class_attr_wifi_set_carddetect.attr,
810 	NULL,
811 };
812 ATTRIBUTE_GROUPS(rkwifi_power);
813 
814 /** Device model classes */
815 static struct class rkwifi_power = {
816 	.name        = "rkwifi",
817 	.class_groups = rkwifi_power_groups,
818 };
819 
rfkill_wlan_probe(struct platform_device * pdev)820 static int rfkill_wlan_probe(struct platform_device *pdev)
821 {
822 	struct rfkill_wlan_data *rfkill;
823 	struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
824 	int ret = -1;
825 
826 	LOG("Enter %s\n", __func__);
827 
828 	class_register(&rkwifi_power);
829 
830 	if (!pdata) {
831 #ifdef CONFIG_OF
832 		pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
833 		if (!pdata)
834 			return -ENOMEM;
835 
836 		ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
837 		if (ret < 0) {
838 #endif
839 			LOG("%s: No platform data specified\n", __func__);
840 			return ret;
841 #ifdef CONFIG_OF
842 		}
843 #endif
844 	}
845 
846 	rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
847 	if (!rfkill)
848 		goto rfkill_alloc_fail;
849 
850 	rfkill->pdata = pdata;
851 	g_rfkill = rfkill;
852 
853 	LOG("%s: init gpio\n", __func__);
854 
855 	if (!pdata->mregulator.power_ctrl_by_pmu) {
856 		ret = rfkill_rk_setup_gpio(&pdata->vbat_n, wlan_name,
857 					   "wlan_vbat");
858 		if (ret)
859 			goto fail_alloc;
860 
861 		ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name,
862 					   "wlan_reset");
863 		if (ret)
864 			goto fail_alloc;
865 	}
866 
867 	wake_lock_init(&rfkill->wlan_irq_wl, WAKE_LOCK_SUSPEND,
868 		       "rfkill_wlan_wake");
869 
870 	rfkill_set_wifi_bt_power(1);
871 
872 #ifdef CONFIG_SDIO_KEEPALIVE
873 	if (gpio_is_valid(pdata->power_n.io) &&
874 		gpio_direction_output(pdata->power_n.io, pdata->power_n.enable);
875 #endif
876 
877 
878 	if (pdata->wifi_power_remain)
879 		rockchip_wifi_power(1);
880 
881 #if BCM_STATIC_MEMORY_SUPPORT
882 	rockchip_init_wifi_mem();
883 #endif
884 
885 #if defined(CONFIG_HAS_EARLYSUSPEND)
886 	register_early_suspend(wlan_early_suspend);
887 #endif
888 
889 	fb_register_client(&rfkill_wlan_fb_notifier);
890 
891 	LOG("Exit %s\n", __func__);
892 
893 	return 0;
894 
895 fail_alloc:
896 	kfree(rfkill);
897 rfkill_alloc_fail:
898 	kfree(pdata);
899 
900 	g_rfkill = NULL;
901 
902 	return ret;
903 }
904 
905 static int rfkill_wlan_remove(struct platform_device *pdev)
906 {
907 	struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
908 
909 	LOG("Enter %s\n", __func__);
910 
911 	wake_lock_destroy(&rfkill->wlan_irq_wl);
912 
913 	fb_unregister_client(&rfkill_wlan_fb_notifier);
914 
915 	if (gpio_is_valid(rfkill->pdata->power_n.io))
916 		gpio_free(rfkill->pdata->power_n.io);
917 
918 	if (gpio_is_valid(rfkill->pdata->reset_n.io))
919 		gpio_free(rfkill->pdata->reset_n.io);
920 
921 	kfree(rfkill);
922 	g_rfkill = NULL;
923 
924 	return 0;
925 }
926 
927 static void rfkill_wlan_shutdown(struct platform_device *pdev)
928 {
929 	LOG("Enter %s\n", __func__);
930 
931 	rockchip_wifi_power(0);
932 	rfkill_set_wifi_bt_power(0);
933 }
934 
935 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
936 {
937 	LOG("Enter %s\n", __func__);
938 	return 0;
939 }
940 
941 static int rfkill_wlan_resume(struct platform_device *pdev)
942 {
943 	LOG("Enter %s\n", __func__);
944 	return 0;
945 }
946 
947 #ifdef CONFIG_OF
948 static struct of_device_id wlan_platdata_of_match[] = {
949 	{ .compatible = "wlan-platdata" },
950 	{}
951 };
952 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
953 #endif //CONFIG_OF
954 
955 static struct platform_driver rfkill_wlan_driver = {
956 	.probe = rfkill_wlan_probe,
957 	.remove = rfkill_wlan_remove,
958 	.shutdown = rfkill_wlan_shutdown,
959     .suspend = rfkill_wlan_suspend,
960     .resume = rfkill_wlan_resume,
961 	.driver = {
962 		.name = "wlan-platdata",
963 		.owner = THIS_MODULE,
964         .of_match_table = of_match_ptr(wlan_platdata_of_match),
965 	},
966 };
967 
968 int __init rfkill_wlan_init(void)
969 {
970 	LOG("Enter %s\n", __func__);
971 	return platform_driver_register(&rfkill_wlan_driver);
972 }
973 
974 void __exit rfkill_wlan_exit(void)
975 {
976 	LOG("Enter %s\n", __func__);
977 	platform_driver_unregister(&rfkill_wlan_driver);
978 }
979 
980 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
981 MODULE_AUTHOR("gwl@rock-chips.com");
982 MODULE_LICENSE("GPL");
983