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