• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
220 /**************************************************************************
221  *
222  * Wifi Power Control Func
223  * 0 -> power off
224  * 1 -> power on
225  *
226  *************************************************************************/
rockchip_wifi_power(int on)227 int rockchip_wifi_power(int on)
228 {
229 	struct rfkill_wlan_data *mrfkill = g_rfkill;
230 	struct rksdmmc_gpio *poweron, *reset;
231 	struct regulator *ldo = NULL;
232 	int bt_power = 0;
233 	bool toggle = false;
234 
235 	LOG("%s: %d\n", __func__, on);
236 
237 	if (!mrfkill) {
238 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
239 		    __func__);
240 		return -1;
241 	}
242 
243 	if (mrfkill->pdata->wifi_power_remain && power_set_time) {
244 		LOG("%s: wifi power is setted to be remain on.", __func__);
245 		return 0;
246 	}
247 	power_set_time++;
248 
249 	if (!rfkill_get_bt_power_state(&bt_power, &toggle)) {
250 		LOG("%s: toggle = %s\n", __func__, toggle ? "true" : "false");
251 	}
252 
253 	if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
254 		int ret = -1;
255 		char *ldostr;
256 		int level = mrfkill->pdata->mregulator.enable;
257 
258 		ldostr = mrfkill->pdata->mregulator.pmu_regulator;
259 		if (!ldostr)
260 			return -1;
261 		ldo = regulator_get(NULL, ldostr);
262 		if (!ldo || IS_ERR(ldo)) {
263 			LOG("\n\n\n%s get ldo error,please mod this\n\n\n",
264 			    __func__);
265 			return -1;
266 		}
267 		if (on == level) {
268 			regulator_set_voltage(ldo, 3000000, 3000000);
269 			LOG("%s: %s enabled\n", __func__, ldostr);
270 			ret = regulator_enable(ldo);
271 			if (ret)
272 				LOG("ldo enable failed\n");
273 			wifi_power_state = 1;
274 			LOG("wifi turn on power.\n");
275 		} else {
276 			LOG("%s: %s disabled\n", __func__, ldostr);
277 			while (regulator_is_enabled(ldo) > 0) {
278 				ret = regulator_disable(ldo);
279 				if (ret)
280 					LOG("ldo disable failed\n");
281 			}
282 			wifi_power_state = 0;
283 			LOG("wifi shut off power.\n");
284 		}
285 		regulator_put(ldo);
286 		msleep(100);
287 	} else {
288 		poweron = &mrfkill->pdata->power_n;
289 		reset = &mrfkill->pdata->reset_n;
290 
291 		if (on) {
292 			if (toggle) {
293 				rfkill_set_wifi_bt_power(1);
294 				msleep(100);
295 			}
296 
297 			if (gpio_is_valid(poweron->io)) {
298 				gpio_direction_output(poweron->io, poweron->enable);
299 				msleep(100);
300 			}
301 
302 			if (gpio_is_valid(reset->io)) {
303 				gpio_direction_output(reset->io, reset->enable);
304 				msleep(100);
305 			}
306 
307 			wifi_power_state = 1;
308 			LOG("wifi turn on power [GPIO%d-%d]\n", poweron->io, poweron->enable);
309 		} else {
310 			if (gpio_is_valid(poweron->io)) {
311 				printk("wifi power off\n");
312 				gpio_direction_output(poweron->io, !(poweron->enable));
313 				msleep(100);
314 			}
315 
316 			if (gpio_is_valid(reset->io)) {
317 				gpio_direction_output(reset->io, !(reset->enable));
318 			}
319 
320 			wifi_power_state = 0;
321 			if (toggle) {
322 				if (!bt_power) {
323 					LOG("%s: wifi will set vbat to low\n", __func__);
324 					rfkill_set_wifi_bt_power(0);
325 				} else {
326 					LOG("%s: wifi shouldn't control the vbat\n", __func__);
327 				}
328 			}
329 			LOG("wifi shut off power [GPIO%d-%d]\n", poweron->io, !poweron->enable);
330 		}
331 	}
332 
333 	return 0;
334 }
335 EXPORT_SYMBOL(rockchip_wifi_power);
336 
337 /**************************************************************************
338  *
339  * Wifi Sdio Detect Func
340  *
341  *************************************************************************/
rockchip_wifi_set_carddetect(int val)342 int rockchip_wifi_set_carddetect(int val)
343 {
344 	return 0;
345 }
346 EXPORT_SYMBOL(rockchip_wifi_set_carddetect);
347 
348 /**************************************************************************
349  *
350  * Wifi Get Interrupt irq Func
351  *
352  *************************************************************************/
rockchip_wifi_get_oob_irq(void)353 int rockchip_wifi_get_oob_irq(void)
354 {
355 	struct rfkill_wlan_data *mrfkill = g_rfkill;
356 	struct rksdmmc_gpio *wifi_int_irq;
357 
358 	LOG("%s: Enter\n", __func__);
359 
360 	if (!mrfkill) {
361 		LOG("%s: rfkill-wlan driver has not Successful initialized\n",
362 		    __func__);
363 		return -1;
364 	}
365 
366 	wifi_int_irq = &mrfkill->pdata->wifi_int_b;
367 	if (gpio_is_valid(wifi_int_irq->io)) {
368 		return gpio_to_irq(wifi_int_irq->io);
369 		//return wifi_int_irq->io;
370 	} else {
371 		LOG("%s: wifi OOB pin isn't defined.\n", __func__);
372 	}
373 
374 	return -1;
375 }
376 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq);
377 
rockchip_wifi_get_oob_irq_flag(void)378 int rockchip_wifi_get_oob_irq_flag(void)
379 {
380 	struct rfkill_wlan_data *mrfkill = g_rfkill;
381 	struct rksdmmc_gpio *wifi_int_irq;
382 	int gpio_flags = -1;
383 
384 	if (mrfkill) {
385 		wifi_int_irq = &mrfkill->pdata->wifi_int_b;
386 		if (gpio_is_valid(wifi_int_irq->io))
387 			gpio_flags = wifi_int_irq->enable;
388 	}
389 
390 	return gpio_flags;
391 }
392 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq_flag);
393 
394 /**************************************************************************
395  *
396  * Wifi Reset Func
397  *
398  *************************************************************************/
rockchip_wifi_reset(int on)399 int rockchip_wifi_reset(int on)
400 {
401 	return 0;
402 }
403 EXPORT_SYMBOL(rockchip_wifi_reset);
404 
405 /**************************************************************************
406  *
407  * Wifi MAC custom Func
408  *
409  *************************************************************************/
410 #include <linux/etherdevice.h>
411 #include <linux/errno.h>
412 u8 wifi_custom_mac_addr[6] = { 0, 0, 0, 0, 0, 0 };
413 
414 //#define RANDOM_ADDRESS_SAVE
get_wifi_addr_vendor(unsigned char * addr)415 static int get_wifi_addr_vendor(unsigned char *addr)
416 {
417 	int ret;
418 	int count = 5;
419 
420 	while (count-- > 0) {
421 		if (is_rk_vendor_ready())
422 			break;
423 		/* sleep 500ms wait rk vendor driver ready */
424 		msleep(500);
425 	}
426 	ret = rk_vendor_read(WIFI_MAC_ID, addr, 6);
427 	if (ret != 6 || is_zero_ether_addr(addr)) {
428 		LOG("%s: rk_vendor_read wifi mac address failed (%d)\n",
429 		    __func__, ret);
430 #ifdef CONFIG_WIFI_GENERATE_RANDOM_MAC_ADDR
431 		random_ether_addr(addr);
432 		LOG("%s: generate random wifi mac address: "
433 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
434 		    __func__, addr[0], addr[1], addr[2], addr[3], addr[4],
435 		    addr[5]);
436 		ret = rk_vendor_write(WIFI_MAC_ID, addr, 6);
437 		if (ret != 0) {
438 			LOG("%s: rk_vendor_write failed %d\n"
439 			    __func__, ret);
440 			memset(addr, 0, 6);
441 			return -1;
442 		}
443 #else
444 		return -1;
445 #endif
446 	} else {
447 		LOG("%s: rk_vendor_read wifi mac address: "
448 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
449 		    __func__, addr[0], addr[1], addr[2], addr[3], addr[4],
450 		    addr[5]);
451 	}
452 	return 0;
453 }
454 
rockchip_wifi_mac_addr(unsigned char * buf)455 int rockchip_wifi_mac_addr(unsigned char *buf)
456 {
457 	char mac_buf[20] = { 0 };
458 
459 	LOG("%s: enter.\n", __func__);
460 
461 	// from vendor storage
462 	if (is_zero_ether_addr(wifi_custom_mac_addr)) {
463 		if (get_wifi_addr_vendor(wifi_custom_mac_addr) != 0)
464 			return -1;
465 	}
466 
467 	sprintf(mac_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
468 		wifi_custom_mac_addr[0], wifi_custom_mac_addr[1],
469 		wifi_custom_mac_addr[2], wifi_custom_mac_addr[3],
470 		wifi_custom_mac_addr[4], wifi_custom_mac_addr[5]);
471 	LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
472 
473 	if (is_valid_ether_addr(wifi_custom_mac_addr)) {
474 		if (!strncmp(wifi_chip_type_string, "rtl", 3))
475 			wifi_custom_mac_addr[0] &= ~0x2; // for p2p
476 	} else {
477 		LOG("This mac address is not valid, ignored...\n");
478 		return -1;
479 	}
480 
481 	memcpy(buf, wifi_custom_mac_addr, 6);
482 
483 	return 0;
484 }
485 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
486 
487 /**************************************************************************
488  *
489  * wifi get country code func
490  *
491  *************************************************************************/
492 struct cntry_locales_custom {
493 	char iso_abbrev[4]; /* ISO 3166-1 country abbreviation */
494 	char custom_locale[4]; /* Custom firmware locale */
495 	int custom_locale_rev; /* Custom local revisin default -1 */
496 };
497 
498 static struct cntry_locales_custom country_cloc;
499 
rockchip_wifi_country_code(char * ccode)500 void *rockchip_wifi_country_code(char *ccode)
501 {
502 	struct cntry_locales_custom *mcloc;
503 
504 	LOG("%s: set country code [%s]\n", __func__, ccode);
505 	mcloc = &country_cloc;
506 	memcpy(mcloc->custom_locale, ccode, 4);
507 	mcloc->custom_locale_rev = 0;
508 
509 	return mcloc;
510 }
511 EXPORT_SYMBOL(rockchip_wifi_country_code);
512 /**************************************************************************/
513 
rfkill_rk_setup_gpio(struct rksdmmc_gpio * gpio,const char * prefix,const char * name)514 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char *prefix,
515 				const char *name)
516 {
517 	if (gpio_is_valid(gpio->io)) {
518 		int ret = 0;
519 
520 		sprintf(gpio->name, "%s_%s", prefix, name);
521 		ret = gpio_request(gpio->io, gpio->name);
522 		if (ret) {
523 			LOG("Failed to get %s gpio.\n", gpio->name);
524 			return -1;
525 		}
526 	}
527 
528 	return 0;
529 }
530 
531 #ifdef CONFIG_OF
wlan_platdata_parse_dt(struct device * dev,struct rksdmmc_gpio_wifi_moudle * data)532 static int wlan_platdata_parse_dt(struct device *dev,
533 				  struct rksdmmc_gpio_wifi_moudle *data)
534 {
535 	struct device_node *node = dev->of_node;
536 	const char *strings;
537 	u32 value;
538 	int gpio, ret;
539 	enum of_gpio_flags flags;
540 	u32 ext_clk_value = 0;
541 
542 	if (!node)
543 		return -ENODEV;
544 
545 	memset(data, 0, sizeof(*data));
546 
547 #ifdef CONFIG_MFD_SYSCON
548 	data->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
549 	if (IS_ERR(data->grf)) {
550 		LOG("can't find rockchip,grf property\n");
551 		//return -1;
552 	}
553 #endif
554 
555 	ret = of_property_read_string(node, "wifi_chip_type", &strings);
556 	if (ret) {
557 		LOG("%s: Can not read wifi_chip_type, set default to rkwifi.\n",
558 		    __func__);
559 		strcpy(wifi_chip_type_string, "rkwifi");
560 	} else {
561 		if (strings && strlen(strings) < 64)
562 			strcpy(wifi_chip_type_string, strings);
563 	}
564 	LOG("%s: wifi_chip_type = %s\n", __func__, wifi_chip_type_string);
565 
566 	if (of_find_property(node, "keep_wifi_power_on", NULL)) {
567 		data->wifi_power_remain = true;
568 		LOG("%s: wifi power remain\n", __func__);
569 	} else {
570 		data->wifi_power_remain = false;
571 		LOG("%s: enable wifi power control.\n", __func__);
572 	}
573 
574 	if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
575 		data->mregulator.power_ctrl_by_pmu = true;
576 		ret = of_property_read_string(node, "power_pmu_regulator",
577 					      &strings);
578 		if (ret) {
579 			LOG("%s: Can not read property: power_pmu_regulator.\n",
580 			    __func__);
581 			data->mregulator.power_ctrl_by_pmu = false;
582 		} else {
583 			LOG("%s: wifi power controlled by pmu(%s).\n", __func__,
584 			    strings);
585 			sprintf(data->mregulator.pmu_regulator, "%s", strings);
586 		}
587 		ret = of_property_read_u32(node, "power_pmu_enable_level",
588 					   &value);
589 		if (ret) {
590 			LOG("%s: Can not read: power_pmu_enable_level.\n",
591 			    __func__);
592 			data->mregulator.power_ctrl_by_pmu = false;
593 		} else {
594 			LOG("%s: wifi power controlled by pmu(level = %s).\n",
595 			    __func__, (value == 1) ? "HIGH" : "LOW");
596 			data->mregulator.enable = value;
597 		}
598 	} else {
599 		data->mregulator.power_ctrl_by_pmu = false;
600 		LOG("%s: wifi power controled by gpio.\n", __func__);
601 		gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0,
602 					       &flags);
603 		if (gpio_is_valid(gpio)) {
604 			data->power_n.io = gpio;
605 			data->power_n.enable =
606 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
607 			LOG("%s: WIFI,poweren_gpio = %d flags = %d.\n",
608 			    __func__, gpio, flags);
609 		} else {
610 			data->power_n.io = -1;
611 		}
612 		gpio = of_get_named_gpio_flags(node, "WIFI,vbat_gpio", 0,
613 					       &flags);
614 		if (gpio_is_valid(gpio)) {
615 			data->vbat_n.io = gpio;
616 			data->vbat_n.enable =
617 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
618 			LOG("%s: WIFI,vbat_gpio = %d, flags = %d.\n",
619 			    __func__, gpio, flags);
620 		} else {
621 			data->vbat_n.io = -1;
622 		}
623 		gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0,
624 					       &flags);
625 		if (gpio_is_valid(gpio)) {
626 			data->reset_n.io = gpio;
627 			data->reset_n.enable =
628 				(flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
629 			LOG("%s: WIFI,reset_gpio = %d, flags = %d.\n",
630 			    __func__, gpio, flags);
631 		} else {
632 			data->reset_n.io = -1;
633 		}
634 		gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0,
635 					       &flags);
636 		if (gpio_is_valid(gpio)) {
637 			data->wifi_int_b.io = gpio;
638 			data->wifi_int_b.enable = !flags;
639 			LOG("%s: WIFI,host_wake_irq = %d, flags = %d.\n",
640 			    __func__, gpio, flags);
641 		} else {
642 			data->wifi_int_b.io = -1;
643 		}
644 	}
645 
646 	data->ext_clk = devm_clk_get(dev, "clk_wifi");
647 	if (IS_ERR(data->ext_clk)) {
648 		LOG("%s: The ref_wifi_clk not found !\n", __func__);
649 	} else {
650 		of_property_read_u32(node, "ref-clock-frequency",
651 				     &ext_clk_value);
652 		if (ext_clk_value > 0) {
653 			ret = clk_set_rate(data->ext_clk, ext_clk_value);
654 			if (ret)
655 				LOG("%s: set ref clk error!\n", __func__);
656 		}
657 
658 		ret = clk_prepare_enable(data->ext_clk);
659 		if (ret)
660 			LOG("%s: enable ref clk error!\n", __func__);
661 
662 		/* WIFI clock (REF_CLKOUT) output enable.
663 		 * 1'b0: drive disable
664 		 * 1'b1: output enable
665 		 */
666 		if (of_machine_is_compatible("rockchip,rk3308"))
667 			regmap_write(data->grf, 0x0314, 0x00020002);
668 	}
669 
670 	return 0;
671 }
672 #endif //CONFIG_OF
673 
674 #if defined(CONFIG_HAS_EARLYSUSPEND)
675 #include <linux/earlysuspend.h>
676 
wlan_early_suspend(struct early_suspend * h)677 static void wlan_early_suspend(struct early_suspend *h)
678 {
679 	LOG("%s :enter\n", __func__);
680 
681 	return;
682 }
683 
wlan_late_resume(struct early_suspend * h)684 static void wlan_late_resume(struct early_suspend *h)
685 {
686 	LOG("%s :enter\n", __func__);
687 
688 	return;
689 }
690 
691 struct early_suspend wlan_early_suspend {
692 	.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
693 	.suspend = wlan_early_suspend;
694 	.resume = wlan_late_resume;
695 }
696 #endif
697 
698 static void
rfkill_wlan_early_suspend(void)699 rfkill_wlan_early_suspend(void)
700 {
701 	//LOG("%s :enter\n", __func__);
702 
703 	return;
704 }
705 
rfkill_wlan_later_resume(void)706 static void rfkill_wlan_later_resume(void)
707 {
708 	//LOG("%s :enter\n", __func__);
709 
710 	return;
711 }
712 
rfkill_wlan_fb_event_notify(struct notifier_block * self,unsigned long action,void * data)713 static int rfkill_wlan_fb_event_notify(struct notifier_block *self,
714 				       unsigned long action, void *data)
715 {
716 	struct fb_event *event = data;
717 	int blank_mode = *((int *)event->data);
718 
719 	switch (blank_mode) {
720 	case FB_BLANK_UNBLANK:
721 		rfkill_wlan_later_resume();
722 		break;
723 	case FB_BLANK_NORMAL:
724 		rfkill_wlan_early_suspend();
725 		break;
726 	default:
727 		rfkill_wlan_early_suspend();
728 		break;
729 	}
730 
731 	return 0;
732 }
733 
734 static struct notifier_block rfkill_wlan_fb_notifier = {
735 	.notifier_call = rfkill_wlan_fb_event_notify,
736 };
737 
wifi_power_show(struct class * cls,struct class_attribute * attr,char * _buf)738 static ssize_t wifi_power_show(struct class *cls, struct class_attribute *attr, char *_buf)
739 {
740 	return sprintf(_buf, "%d\n", wifi_power_state);
741 }
742 
wifi_power_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)743 static ssize_t wifi_power_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
744 {
745 	long poweren = 0;
746 
747 	if (kstrtol(_buf, 10, &poweren) < 0)
748 		return -EINVAL;
749 
750 	LOG("%s: poweren = %ld\n", __func__, poweren);
751 
752 	if (poweren > 0)
753 		rockchip_wifi_power(1);
754 	else
755 		rockchip_wifi_power(0);
756 
757 	return _count;
758 }
759 
760 static CLASS_ATTR_RW(wifi_power);
761 
wifi_bt_vbat_show(struct class * cls,struct class_attribute * attr,char * _buf)762 static ssize_t wifi_bt_vbat_show(struct class *cls, struct class_attribute *attr, char *_buf)
763 {
764 	return sprintf(_buf, "%d\n", wifi_bt_vbat_state);
765 }
766 
wifi_bt_vbat_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)767 static ssize_t wifi_bt_vbat_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
768 {
769 	long vbat = 0;
770 
771 	if (kstrtol(_buf, 10, &vbat) < 0)
772 		return -EINVAL;
773 
774 	LOG("%s: vbat = %ld\n", __func__, vbat);
775 
776 	if (vbat > 0)
777 		rfkill_set_wifi_bt_power(1);
778 	else
779 		rfkill_set_wifi_bt_power(0);
780 
781 	return _count;
782 }
783 
784 static CLASS_ATTR_RW(wifi_bt_vbat);
785 
wifi_set_carddetect_store(struct class * cls,struct class_attribute * attr,const char * _buf,size_t _count)786 static ssize_t wifi_set_carddetect_store(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
787 {
788 	long val = 0;
789 
790 	if (kstrtol(_buf, 10, &val) < 0)
791 		return -EINVAL;
792 
793 	LOG("%s: val = %ld\n", __func__, val);
794 
795 	if (val > 0)
796 		rockchip_wifi_set_carddetect(1);
797 	else
798 		rockchip_wifi_set_carddetect(0);
799 
800 	return _count;
801 }
802 
803 static CLASS_ATTR_WO(wifi_set_carddetect);
804 
805 static struct attribute *rkwifi_power_attrs[] = {
806 	&class_attr_wifi_power.attr,
807 	&class_attr_wifi_bt_vbat.attr,
808 	&class_attr_wifi_set_carddetect.attr,
809 	NULL,
810 };
811 ATTRIBUTE_GROUPS(rkwifi_power);
812 
813 /** Device model classes */
814 static struct class rkwifi_power = {
815 	.name        = "rkwifi",
816 	.class_groups = rkwifi_power_groups,
817 };
818 
rfkill_wlan_probe(struct platform_device * pdev)819 static int rfkill_wlan_probe(struct platform_device *pdev)
820 {
821 	struct rfkill_wlan_data *rfkill;
822 	struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
823 	int ret = -1;
824 
825 	LOG("Enter %s\n", __func__);
826 
827 	class_register(&rkwifi_power);
828 
829 	if (!pdata) {
830 #ifdef CONFIG_OF
831 		pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
832 		if (!pdata)
833 			return -ENOMEM;
834 
835 		ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
836 		if (ret < 0) {
837 #endif
838 			LOG("%s: No platform data specified\n", __func__);
839 			return ret;
840 #ifdef CONFIG_OF
841 		}
842 #endif
843 	}
844 
845 	rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
846 	if (!rfkill)
847 		goto rfkill_alloc_fail;
848 
849 	rfkill->pdata = pdata;
850 	g_rfkill = rfkill;
851 
852 	LOG("%s: init gpio\n", __func__);
853 
854 	if (!pdata->mregulator.power_ctrl_by_pmu) {
855 		ret = rfkill_rk_setup_gpio(&pdata->vbat_n, wlan_name,
856 					   "wlan_vbat");
857 		if (ret)
858 			goto fail_alloc;
859 
860 		ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name,
861 					   "wlan_reset");
862 		if (ret)
863 			goto fail_alloc;
864 	}
865 
866 	wake_lock_init(&rfkill->wlan_irq_wl, WAKE_LOCK_SUSPEND,
867 		       "rfkill_wlan_wake");
868 
869 	rfkill_set_wifi_bt_power(1);
870 
871 #ifdef CONFIG_SDIO_KEEPALIVE
872 	if (gpio_is_valid(pdata->power_n.io) &&
873 		gpio_direction_output(pdata->power_n.io, pdata->power_n.enable);
874 #endif
875 
876 
877 	if (pdata->wifi_power_remain)
878 		rockchip_wifi_power(1);
879 
880 #if BCM_STATIC_MEMORY_SUPPORT
881 	rockchip_init_wifi_mem();
882 #endif
883 
884 #if defined(CONFIG_HAS_EARLYSUSPEND)
885 	register_early_suspend(wlan_early_suspend);
886 #endif
887 
888 	fb_register_client(&rfkill_wlan_fb_notifier);
889 
890 	LOG("Exit %s\n", __func__);
891 
892 	return 0;
893 
894 fail_alloc:
895 	kfree(rfkill);
896 rfkill_alloc_fail:
897 	kfree(pdata);
898 
899 	g_rfkill = NULL;
900 
901 	return ret;
902 }
903 
904 static int rfkill_wlan_remove(struct platform_device *pdev)
905 {
906 	struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
907 
908 	LOG("Enter %s\n", __func__);
909 
910 	wake_lock_destroy(&rfkill->wlan_irq_wl);
911 
912 	fb_unregister_client(&rfkill_wlan_fb_notifier);
913 
914 	if (gpio_is_valid(rfkill->pdata->power_n.io))
915 		gpio_free(rfkill->pdata->power_n.io);
916 
917 	if (gpio_is_valid(rfkill->pdata->reset_n.io))
918 		gpio_free(rfkill->pdata->reset_n.io);
919 
920 	kfree(rfkill);
921 	g_rfkill = NULL;
922 
923 	return 0;
924 }
925 
926 static void rfkill_wlan_shutdown(struct platform_device *pdev)
927 {
928 	LOG("Enter %s\n", __func__);
929 
930 	rockchip_wifi_power(0);
931 	rfkill_set_wifi_bt_power(0);
932 }
933 
934 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
935 {
936 	LOG("Enter %s\n", __func__);
937 	return 0;
938 }
939 
940 static int rfkill_wlan_resume(struct platform_device *pdev)
941 {
942 	LOG("Enter %s\n", __func__);
943 	return 0;
944 }
945 
946 #ifdef CONFIG_OF
947 static struct of_device_id wlan_platdata_of_match[] = {
948 	{ .compatible = "wlan-platdata" },
949 	{}
950 };
951 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
952 #endif //CONFIG_OF
953 
954 static struct platform_driver rfkill_wlan_driver = {
955 	.probe = rfkill_wlan_probe,
956 	.remove = rfkill_wlan_remove,
957 	.shutdown = rfkill_wlan_shutdown,
958     .suspend = rfkill_wlan_suspend,
959     .resume = rfkill_wlan_resume,
960 	.driver = {
961 		.name = "wlan-platdata",
962 		.owner = THIS_MODULE,
963         .of_match_table = of_match_ptr(wlan_platdata_of_match),
964 	},
965 };
966 
967 int __init rfkill_wlan_init(void)
968 {
969 	LOG("Enter %s\n", __func__);
970 	return platform_driver_register(&rfkill_wlan_driver);
971 }
972 
973 void __exit rfkill_wlan_exit(void)
974 {
975 	LOG("Enter %s\n", __func__);
976 	platform_driver_unregister(&rfkill_wlan_driver);
977 }
978 
979 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
980 MODULE_AUTHOR("gwl@rock-chips.com");
981 MODULE_LICENSE("GPL");
982