4c63219376d59bd39825554c62886c4bc4ca01a9
[firefly-linux-kernel-4.4.55.git] / net / rfkill / rfkill-wlan.c
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 <linux/rockchip/iomap.h>
35 #include <dt-bindings/gpio/gpio.h>
36 #include <linux/skbuff.h>
37 #include <linux/fb.h>
38 #include <linux/rockchip/grf.h>
39 #include <linux/rockchip/common.h>
40 #include <linux/regmap.h>
41 #include <linux/mfd/syscon.h>
42 #include <linux/mmc/host.h>
43 #ifdef CONFIG_OF
44 #include <linux/of.h>
45 #include <linux/of_device.h>
46 #include <linux/of_gpio.h>
47 #endif
48
49 #if 0
50 #define DBG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
51 #else
52 #define DBG(x...)
53 #endif
54
55 #define LOG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
56
57 extern struct mmc_host *primary_sdio_host;
58 extern void mmc_pwrseq_power_off(struct mmc_host *host);
59
60 struct rfkill_wlan_data {
61         struct rksdmmc_gpio_wifi_moudle *pdata;
62     struct wake_lock            wlan_irq_wl;
63 };
64
65 static struct rfkill_wlan_data *g_rfkill = NULL;
66 static int power_set_time = 0;
67
68 static const char wlan_name[] = 
69 #if defined (CONFIG_BCM4330)
70     #if defined (CONFIG_BT_MODULE_NH660)
71         "nh660"
72     #else
73         "bcm4330"
74     #endif
75 #elif defined (CONFIG_RK903)
76     #if defined(CONFIG_RKWIFI_26M)
77         "rk903_26M"
78     #else
79         "rk903"
80     #endif
81 #elif defined(CONFIG_BCM4329)
82         "bcm4329"
83 #elif defined(CONFIG_MV8787)
84         "mv8787"
85 #elif defined(CONFIG_AP6210)
86     #if defined(CONFIG_RKWIFI_26M)
87         "ap6210"
88     #else
89         "ap6210_24M"
90     #endif
91 #elif defined(CONFIG_AP6330)
92                 "ap6330"
93 #elif defined(CONFIG_AP6476)
94                 "ap6476"
95 #elif defined(CONFIG_AP6493)
96                 "ap6493"
97 #else
98         "wlan_default"
99 #endif
100 ;
101
102 static char wifi_chip_type_string[64];
103 int get_wifi_chip_type(void)
104 {
105     int type;
106     if (strcmp(wifi_chip_type_string, "ap6210") == 0) {
107         type = WIFI_AP6210;
108     } else if (strcmp(wifi_chip_type_string, "ap6212") == 0) {
109         type = WIFI_AP6212;
110     } else if (strcmp(wifi_chip_type_string, "rk901") == 0) {
111         type = WIFI_RK901;    
112     } else if (strcmp(wifi_chip_type_string, "rk903") == 0) {
113         type = WIFI_RK903;  
114     } else if (strcmp(wifi_chip_type_string, "ap6181") == 0) {
115         type = WIFI_AP6181;
116     } else if (strcmp(wifi_chip_type_string, "ap6234") == 0) {
117         type = WIFI_AP6234;                            
118     } else if (strcmp(wifi_chip_type_string, "ap6330") == 0) {
119         type = WIFI_AP6330;
120     } else if (strcmp(wifi_chip_type_string, "ap6335") == 0) {
121         type = WIFI_AP6335;
122     } else if (strcmp(wifi_chip_type_string, "ap6354") == 0) {
123         type = WIFI_AP6354;
124     } else if (strcmp(wifi_chip_type_string, "ap6441") == 0) {
125         type = WIFI_AP6441;
126     } else if (strcmp(wifi_chip_type_string, "ap6476") == 0) {
127         type = WIFI_AP6476;    
128     } else if (strcmp(wifi_chip_type_string, "ap6493") == 0) {
129         type = WIFI_AP6493;                    
130     } else if (strcmp(wifi_chip_type_string, "rtl8188eu") == 0) {
131         type = WIFI_RTL8188EU;
132     } else if (strcmp(wifi_chip_type_string, "rtl8192du") == 0) {
133         type = WIFI_RTL8192DU;
134     } else if (strcmp(wifi_chip_type_string, "rtl8723as") == 0) {
135         type = WIFI_RTL8723AS;        
136     } else if (strcmp(wifi_chip_type_string, "rtl8723bs_vq0") == 0) {
137         type = WIFI_RTL8723BS_VQ0;        
138     } else if (strcmp(wifi_chip_type_string, "rtl8723bs") == 0) {
139         type = WIFI_RTL8723BS;
140     } else if (strcmp(wifi_chip_type_string, "rtl8723au") == 0) {
141         type = WIFI_RTL8723AU;        
142     } else if (strcmp(wifi_chip_type_string, "rtl8723bu") == 0) {
143         type = WIFI_RTL8723BU;
144     } else if (strcmp(wifi_chip_type_string, "rtl8189es") == 0) {
145         type = WIFI_RTL8189ES;
146     } else if (strcmp(wifi_chip_type_string, "rtl8812au") == 0) {
147         type = WIFI_RTL8812AU;                        
148     } else if (strcmp(wifi_chip_type_string, "esp8089") == 0) {
149         type = WIFI_ESP8089;
150     } else {
151         type = WIFI_AP6210;
152     }
153     return type;
154 }
155 EXPORT_SYMBOL(get_wifi_chip_type);
156
157 /***********************************************************
158  * 
159  * Broadcom Wifi Static Memory
160  * 
161  **********************************************************/
162 #ifdef CONFIG_RKWIFI
163 #define BCM_STATIC_MEMORY_SUPPORT 0
164 #else
165 #define BCM_STATIC_MEMORY_SUPPORT 0
166 #endif
167 //===========================
168 #if BCM_STATIC_MEMORY_SUPPORT
169 #define PREALLOC_WLAN_SEC_NUM           4
170 #define PREALLOC_WLAN_BUF_NUM           160
171 #define PREALLOC_WLAN_SECTION_HEADER    0
172 #define WLAN_SKB_BUF_NUM        16
173
174 #define WLAN_SECTION_SIZE_0     (12 * 1024)
175 #define WLAN_SECTION_SIZE_1     (12 * 1024)
176 #define WLAN_SECTION_SIZE_2     (32 * 1024)
177 #define WLAN_SECTION_SIZE_3     (136* 1024)
178 #define WLAN_SECTION_SIZE_4     (4  * 1024)
179 #define WLAN_SECTION_SIZE_5     (64 * 1024)
180 #define WLAN_SECTION_SIZE_6     (4  * 1024)
181 #define WLAN_SECTION_SIZE_7     (4  * 1024)
182
183 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM+1];
184
185 struct wifi_mem_prealloc {
186     void *mem_ptr;
187     unsigned long size;
188 };
189
190 static struct wifi_mem_prealloc wifi_mem_array[8] = {
191     {NULL, (WLAN_SECTION_SIZE_0)},
192     {NULL, (WLAN_SECTION_SIZE_1)},
193     {NULL, (WLAN_SECTION_SIZE_2)},
194     {NULL, (WLAN_SECTION_SIZE_3)},
195     {NULL, (WLAN_SECTION_SIZE_4)},
196     {NULL, (WLAN_SECTION_SIZE_5)},
197     {NULL, (WLAN_SECTION_SIZE_6)},
198     {NULL, (WLAN_SECTION_SIZE_7)}
199 };
200
201 static int rockchip_init_wifi_mem(void)
202 {
203     int i;
204     int j;
205
206     for (i = 0 ; i < WLAN_SKB_BUF_NUM ; i++) {
207         wlan_static_skb[i] = dev_alloc_skb(
208                ((i < (WLAN_SKB_BUF_NUM / 2)) ? (PAGE_SIZE*1) : (PAGE_SIZE*2)));
209
210         if (!wlan_static_skb[i])
211             goto err_skb_alloc;
212     }
213
214     wlan_static_skb[i] = dev_alloc_skb((PAGE_SIZE*4));
215     if (!wlan_static_skb[i])
216         goto err_skb_alloc;
217
218     for (i = 0 ; i <= 7; i++) {
219         wifi_mem_array[i].mem_ptr =
220                kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
221
222         if (!wifi_mem_array[i].mem_ptr)
223             goto err_mem_alloc;
224     }
225     return 0;
226
227 err_mem_alloc:
228     pr_err("Failed to mem_alloc for WLAN\n");
229     for (j = 0 ; j < i ; j++)
230         kfree(wifi_mem_array[j].mem_ptr);
231     i = WLAN_SKB_BUF_NUM;
232 err_skb_alloc:
233     pr_err("Failed to skb_alloc for WLAN\n");
234     for (j = 0 ; j < i ; j++)
235         dev_kfree_skb(wlan_static_skb[j]);
236     dev_kfree_skb(wlan_static_skb[j]);
237
238     return -ENOMEM;
239 }
240
241 void *rockchip_mem_prealloc(int section, unsigned long size)
242 {
243     //printk("rockchip_mem_prealloc: section = %d, size = %d\n", section, size);
244     if (section == PREALLOC_WLAN_SEC_NUM)
245         return wlan_static_skb;
246
247     if ((section < 0) || (section > 7))
248         return NULL;
249
250     if (wifi_mem_array[section].size < size)
251         return NULL;
252
253     return wifi_mem_array[section].mem_ptr;
254 }
255 #else
256 void *rockchip_mem_prealloc(int section, unsigned long size) { return NULL;}
257 #endif
258 EXPORT_SYMBOL(rockchip_mem_prealloc);
259
260 /**************************************************************************
261  *
262  * get wifi power state Func
263  *
264  *************************************************************************/
265 static int wifi_power_state = 0;
266 int rfkill_get_wifi_power_state(int *power, int *vref_ctrl_enable)
267 {
268     struct rfkill_wlan_data *mrfkill = g_rfkill;
269
270     if (mrfkill == NULL) {
271         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
272         return -1;
273     }
274
275     *power = wifi_power_state;
276
277     return 0;
278 }
279
280 /**************************************************************************
281  *
282  * Wifi Power Control Func
283  * 0 -> power off
284  * 1 -> power on
285  *
286  *************************************************************************/
287 int rockchip_wifi_power(int on)
288 {
289         struct rfkill_wlan_data *mrfkill = g_rfkill;
290     struct rksdmmc_gpio *poweron, *reset;
291     struct regulator *ldo = NULL;
292     int power = 0;
293     bool toggle = false;
294
295     LOG("%s: %d\n", __func__, on);
296
297         if (!on && primary_sdio_host)
298                 mmc_pwrseq_power_off(primary_sdio_host);
299
300     if (mrfkill == NULL) {
301         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
302         return -1;
303     }
304
305     if (mrfkill->pdata->wifi_power_remain && power_set_time) {
306         LOG("%s: wifi power is setted to be remain on. skip anything to the power control", __func__);
307         return 0;
308     } else power_set_time++;
309
310     if (!rfkill_get_bt_power_state(&power, &toggle)) {
311         if (toggle == true && power == 1) {
312             LOG("%s: wifi shouldn't control the power, it was enabled by BT!\n", __func__);
313             return 0;
314         }
315     }
316
317     if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
318         int ret = -1;
319         char *ldostr;
320         int level = mrfkill->pdata->mregulator.enable;
321
322         ldostr = mrfkill->pdata->mregulator.pmu_regulator;
323         if (ldostr == NULL) {
324             LOG("%s: wifi power set to be controled by pmic, but which one?\n", __func__);
325             return -1;
326         }
327         ldo = regulator_get(NULL, ldostr);
328         if (ldo == NULL || IS_ERR(ldo)) {
329             LOG("\n\n\n%s get ldo error,please mod this\n\n\n", __func__);
330             return -1;
331         } else {
332                         if (on == level) {
333                                 regulator_set_voltage(ldo, 3000000, 3000000);
334                             LOG("%s: %s enabled\n", __func__, ldostr);
335                                 ret = regulator_enable(ldo);
336                 wifi_power_state = 1;
337                             LOG("wifi turn on power.\n");
338             } else {
339                                 LOG("%s: %s disabled\n", __func__, ldostr);
340                 while (regulator_is_enabled(ldo) > 0) {
341                                     ret = regulator_disable(ldo);
342                 }
343                 wifi_power_state = 0;
344                             LOG("wifi shut off power.\n");
345                         }
346                         regulator_put(ldo);
347                         msleep(100);
348                 }
349     } else {
350                 poweron = &mrfkill->pdata->power_n;
351                 reset = &mrfkill->pdata->reset_n;
352
353                 if (on){
354                         if (gpio_is_valid(poweron->io)) {
355                                 gpio_set_value(poweron->io, poweron->enable);
356                                 msleep(100);
357                         }
358
359                         if (gpio_is_valid(reset->io)) {
360                                 gpio_set_value(reset->io, reset->enable);
361                                 msleep(100);
362                         }
363
364             wifi_power_state = 1;
365                         LOG("wifi turn on power. %d\n", poweron->io);
366                 }else{
367                         if (gpio_is_valid(poweron->io)) {
368                                 gpio_set_value(poweron->io, !(poweron->enable));
369                                 msleep(100);
370                         }
371
372                         if (gpio_is_valid(reset->io)) {
373                                 gpio_set_value(reset->io, !(reset->enable));
374                         }
375
376             wifi_power_state = 0;
377                         LOG("wifi shut off power.\n");
378                 }
379     }
380
381     return 0;
382 }
383 EXPORT_SYMBOL(rockchip_wifi_power);
384
385 /**************************************************************************
386  *
387  * Wifi Sdio Detect Func
388  *
389  *************************************************************************/
390 extern int mmc_host_rescan(struct mmc_host *host, int val, int irq_type);
391 int rockchip_wifi_set_carddetect(int val)
392 {
393         return mmc_host_rescan(NULL, val, 1);
394 }
395 EXPORT_SYMBOL(rockchip_wifi_set_carddetect);
396
397 /**************************************************************************
398  *
399  * Wifi Get Interrupt irq Func
400  *
401  *************************************************************************/
402 int rockchip_wifi_get_oob_irq(void)
403 {
404     struct rfkill_wlan_data *mrfkill = g_rfkill;
405     struct rksdmmc_gpio *wifi_int_irq;
406
407     LOG("%s: Enter\n", __func__);
408
409     if (mrfkill == NULL) {
410         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
411         return -1;
412     }
413     
414     wifi_int_irq = &mrfkill->pdata->wifi_int_b;
415     if (gpio_is_valid(wifi_int_irq->io)) {
416         return gpio_to_irq(wifi_int_irq->io);
417         //return wifi_int_irq->io;
418     } else {
419         LOG("%s: wifi OOB pin isn't defined.\n", __func__);
420     }
421     
422     return -1;
423 }
424 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq);
425
426 /**************************************************************************
427  *
428  * Wifi Reset Func
429  *
430  *************************************************************************/
431 int rockchip_wifi_reset(int on)
432 {
433     return 0;
434 }
435 EXPORT_SYMBOL(rockchip_wifi_reset);
436
437 /**************************************************************************
438  *
439  * Wifi MAC custom Func
440  *
441  *************************************************************************/
442 #include <linux/etherdevice.h>
443 #include <linux/errno.h>
444 u8 wifi_custom_mac_addr[6] = {0,0,0,0,0,0};
445
446 static void rockchip_wifi_get_sn(char *buf)
447 {
448         memset(buf, 0, 512);
449         //GetSNSectorInfo(char * pbuf);
450
451         return ;
452 }
453
454 //#define ENABLE_WIFI_RAND_MAC
455 #ifdef ENABLE_WIFI_RAND_MAC
456 #define WIFI_RAND_MAC_FILE "/data/misc/wifi_rand_mac"
457
458 static int rockchip_wifi_rand_mac_addr(unsigned char *buf)
459 {
460     struct file *fp; 
461     loff_t pos;
462     mm_segment_t fs;
463     char mac_buf[20] = {0};
464
465     LOG("%s\n", __func__);
466     fp = filp_open(WIFI_RAND_MAC_FILE, O_RDONLY, 0);
467     if (fp == NULL || IS_ERR(fp)) {
468         fp = filp_open(WIFI_RAND_MAC_FILE, O_RDWR | O_CREAT, 0644);
469         if (fp == NULL || IS_ERR(fp)) {
470             LOG("%s: create %s failed.\n", __func__, WIFI_RAND_MAC_FILE);
471             return -1;
472         }
473         fs = get_fs();
474         set_fs(KERNEL_DS);
475         random_ether_addr(wifi_custom_mac_addr);
476         pos = 0;
477         vfs_write(fp, wifi_custom_mac_addr, 6, &pos);
478         filp_close(fp, NULL);  
479     } else {
480         fs = get_fs();
481         set_fs(KERNEL_DS);
482         pos = 0;
483         vfs_read(fp, wifi_custom_mac_addr, 6, &pos);
484         filp_close(fp, NULL); 
485     }
486     sprintf(mac_buf,"%02x:%02x:%02x:%02x:%02x:%02x",wifi_custom_mac_addr[0],wifi_custom_mac_addr[1],
487     wifi_custom_mac_addr[2],wifi_custom_mac_addr[3],wifi_custom_mac_addr[4],wifi_custom_mac_addr[5]);
488     LOG("random wifi_custom_mac_addr=[%s]\n", mac_buf);    
489     return 0;
490 }
491 #endif
492
493 int rockchip_wifi_mac_addr(unsigned char *buf)
494 {
495     char mac_buf[20] = {0};
496     LOG("%s: enter.\n", __func__);
497
498     // from vflash
499     if(is_zero_ether_addr(wifi_custom_mac_addr)) {
500         int i;
501         char *tempBuf = kmalloc(512, GFP_KERNEL);
502         if(tempBuf) {
503             rockchip_wifi_get_sn(tempBuf);
504             for (i = 445; i <= 450; i++)
505                 wifi_custom_mac_addr[i-445] = tempBuf[i];
506             kfree(tempBuf);
507         } else {
508             return -1;
509         }
510     }
511
512     sprintf(mac_buf,"%02x:%02x:%02x:%02x:%02x:%02x",wifi_custom_mac_addr[0],wifi_custom_mac_addr[1],
513     wifi_custom_mac_addr[2],wifi_custom_mac_addr[3],wifi_custom_mac_addr[4],wifi_custom_mac_addr[5]);
514     LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
515
516 #ifdef ENABLE_WIFI_RAND_MAC
517     rockchip_wifi_rand_mac_addr(buf);
518 #endif
519
520     if (is_valid_ether_addr(wifi_custom_mac_addr)) {
521         if (!strncmp(wifi_chip_type_string, "rtl", 3))
522             wifi_custom_mac_addr[0] &= ~0x2; // for p2p
523     } else {
524         LOG("This mac address is not valid, ignored...\n");
525         return -1;
526     }
527
528     memcpy(buf, wifi_custom_mac_addr, 6);
529
530     return 0;
531 }
532 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
533
534 /**************************************************************************
535  *
536  * wifi get country code func
537  *
538  *************************************************************************/
539 struct cntry_locales_custom {
540     char iso_abbrev[4];  /* ISO 3166-1 country abbreviation */
541     char custom_locale[4];   /* Custom firmware locale */
542     int custom_locale_rev;        /* Custom local revisin default -1 */
543 };
544
545 static struct cntry_locales_custom country_cloc;
546
547 void *rockchip_wifi_country_code(char *ccode)
548 {
549     struct cntry_locales_custom *mcloc;
550
551     LOG("%s: set country code [%s]\n", __func__, ccode);
552     mcloc = &country_cloc;
553     memcpy(mcloc->custom_locale, ccode, 4);
554     mcloc->custom_locale_rev = 0;
555
556     return mcloc;
557 }
558 EXPORT_SYMBOL(rockchip_wifi_country_code);
559 /**************************************************************************/
560
561 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char* prefix, const char* name)
562 {
563     if (gpio_is_valid(gpio->io)) {
564         int ret=0;
565         sprintf(gpio->name, "%s_%s", prefix, name);
566         ret = gpio_request(gpio->io, gpio->name);
567         if (ret) {
568             LOG("Failed to get %s gpio.\n", gpio->name);
569             return -1;
570         }
571     }
572
573     return 0;
574 }
575
576 #ifdef CONFIG_OF
577 static int wlan_platdata_parse_dt(struct device *dev,
578                   struct rksdmmc_gpio_wifi_moudle *data)
579 {
580     struct device_node *node = dev->of_node;
581     const char *strings;
582     u32 value;
583     int gpio,ret;
584     enum of_gpio_flags flags;
585
586     if (!node)
587         return -ENODEV;
588
589     memset(data, 0, sizeof(*data));
590
591 #ifdef CONFIG_MFD_SYSCON
592     data->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
593     if (IS_ERR(data->grf)) {
594             LOG("can't find rockchip,grf property\n");
595             //return -1;
596     }
597 #endif
598
599     ret = of_property_read_string(node, "wifi_chip_type", &strings);
600     if (ret) {
601         LOG("%s: Can not read wifi_chip_type, set default to rkwifi.\n", __func__);
602         strcpy(wifi_chip_type_string, "rkwifi");
603     } else {
604         strcpy(wifi_chip_type_string, strings);
605     }
606     LOG("%s: wifi_chip_type = %s\n", __func__, wifi_chip_type_string);
607
608     if (of_find_property(node, "keep_wifi_power_on", NULL)) {
609         data->wifi_power_remain = true;
610         LOG("%s: wifi power will enabled while kernel starting and keep on.\n", __func__);
611     } else {
612         data->wifi_power_remain = false;
613         LOG("%s: enable wifi power control.\n", __func__);
614     }
615
616     if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
617         data->mregulator.power_ctrl_by_pmu = true;
618         ret = of_property_read_string(node, "power_pmu_regulator", &strings);
619         if (ret) {
620             LOG("%s: Can not read property: power_pmu_regulator.\n", __func__);
621             data->mregulator.power_ctrl_by_pmu = false;
622         } else {
623             LOG("%s: wifi power controled by pmu(%s).\n", __func__, strings);
624             sprintf(data->mregulator.pmu_regulator, "%s", strings);
625         }
626         ret = of_property_read_u32(node, "power_pmu_enable_level", &value);
627         if (ret) {
628             LOG("%s: Can not read property: power_pmu_enable_level.\n", __func__);
629             data->mregulator.power_ctrl_by_pmu = false;
630         } else {
631             LOG("%s: wifi power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
632             data->mregulator.enable = value;
633         }
634         } else {
635                 data->mregulator.power_ctrl_by_pmu = false;
636                 LOG("%s: wifi power controled by gpio.\n", __func__);
637         gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0, &flags);
638         if (gpio_is_valid(gpio)){
639                         data->power_n.io = gpio;
640                         data->power_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
641                         LOG("%s: get property: WIFI,poweren_gpio = %d, flags = %d.\n", __func__, gpio, flags);
642         } else data->power_n.io = -1;
643         gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0, &flags);
644         if (gpio_is_valid(gpio)){
645                         data->reset_n.io = gpio;
646                         data->reset_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
647                         LOG("%s: get property: WIFI,reset_gpio = %d, flags = %d.\n", __func__, gpio, flags);
648         } else data->reset_n.io = -1;
649         gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0, &flags);
650         if (gpio_is_valid(gpio)){
651                         data->wifi_int_b.io = gpio;
652                         data->wifi_int_b.enable = flags;
653                         LOG("%s: get property: WIFI,host_wake_irq = %d, flags = %d.\n", __func__, gpio, flags);
654         } else data->wifi_int_b.io = -1;
655         }
656
657     return 0;
658 }
659 #endif //CONFIG_OF
660
661 #if defined(CONFIG_HAS_EARLYSUSPEND)
662 #include <linux/earlysuspend.h>
663
664 static void wlan_early_suspend(struct early_suspend *h)
665 {
666     LOG("%s :enter\n", __func__);
667
668     return;
669 }
670
671 static void wlan_late_resume(struct early_suspend *h)
672 {
673     LOG("%s :enter\n", __func__);
674
675     return;
676 }
677
678 struct early_suspend wlan_early_suspend {
679     .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
680     .suspend = wlan_early_suspend;
681     .resume = wlan_late_resume; 
682 }
683 #endif
684
685 static void rfkill_wlan_early_suspend(void)
686 {
687     //LOG("%s :enter\n", __func__);
688
689     return;
690 }
691
692 static void rfkill_wlan_later_resume(void)
693 {
694     //LOG("%s :enter\n", __func__);
695
696     return;
697 }
698
699 static int rfkill_wlan_fb_event_notify(struct notifier_block *self,
700                        unsigned long action, void *data)
701 {
702
703     struct fb_event *event = data;
704     int blank_mode = *((int *)event->data);
705
706     switch (blank_mode) {
707     case FB_BLANK_UNBLANK:
708         rfkill_wlan_later_resume();
709         break;
710     case FB_BLANK_NORMAL:
711         rfkill_wlan_early_suspend();
712         break;
713     default:
714         rfkill_wlan_early_suspend();
715         break;
716     }
717
718     return 0;
719 }
720
721 static struct notifier_block rfkill_wlan_fb_notifier = {
722     .notifier_call = rfkill_wlan_fb_event_notify,
723 };
724
725
726 static int rfkill_wlan_probe(struct platform_device *pdev)
727 {
728         struct rfkill_wlan_data *rfkill;
729         struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
730         int ret = -1;
731
732     LOG("Enter %s\n", __func__);
733
734         if (!pdata) {
735 #ifdef CONFIG_OF
736         pdata = kzalloc(sizeof(struct rksdmmc_gpio_wifi_moudle), GFP_KERNEL);
737         if (!pdata)
738             return -ENOMEM;
739
740         ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
741         if (ret < 0) {
742 #endif
743                     LOG("%s: No platform data specified\n", __func__);
744             return ret;
745 #ifdef CONFIG_OF
746         }
747 #endif
748         }
749
750         rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
751         if (!rfkill)
752         goto rfkill_alloc_fail;
753
754         rfkill->pdata = pdata;
755     g_rfkill = rfkill;
756
757     LOG("%s: init gpio\n", __func__);
758
759     if (!pdata->mregulator.power_ctrl_by_pmu) {
760         ret = rfkill_rk_setup_gpio(&pdata->power_n, wlan_name, "wlan_poweren");
761         if (ret) goto fail_alloc;
762
763         ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name, "wlan_reset");
764         if (ret) goto fail_alloc;
765     }
766
767     wake_lock_init(&(rfkill->wlan_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_wlan_wake");
768
769     // Turn off wifi power as default
770     if (gpio_is_valid(pdata->power_n.io))
771     {
772         gpio_direction_output(pdata->power_n.io, !pdata->power_n.enable);
773     }
774
775     if (pdata->wifi_power_remain)
776     {
777         rockchip_wifi_power(1);
778     }
779
780 #if BCM_STATIC_MEMORY_SUPPORT
781     rockchip_init_wifi_mem();
782 #endif
783
784 #if defined(CONFIG_HAS_EARLYSUSPEND)
785     register_early_suspend(wlan_early_suspend);
786 #endif
787     
788     fb_register_client(&rfkill_wlan_fb_notifier);
789
790     LOG("Exit %s\n", __func__);
791
792         return 0;
793
794 fail_alloc:
795         kfree(rfkill);
796 rfkill_alloc_fail:
797     kfree(pdata);
798
799     g_rfkill = NULL;
800
801         return ret;
802 }
803
804 static int rfkill_wlan_remove(struct platform_device *pdev)
805 {
806         struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
807
808     LOG("Enter %s\n", __func__);
809
810     wake_lock_destroy(&rfkill->wlan_irq_wl);
811
812     fb_unregister_client(&rfkill_wlan_fb_notifier);
813     
814     if (gpio_is_valid(rfkill->pdata->power_n.io))
815         gpio_free(rfkill->pdata->power_n.io);
816     
817     if (gpio_is_valid(rfkill->pdata->reset_n.io))
818         gpio_free(rfkill->pdata->reset_n.io);
819     
820 //    if (gpio_is_valid(rfkill->pdata->vddio.io))
821 //        gpio_free(rfkill->pdata->vddio.io);
822 //
823 //    if (gpio_is_valid(rfkill->pdata->bgf_int_b.io))
824 //        gpio_free(rfkill->pdata->bgf_int_b.io);
825 //    
826 //    if (gpio_is_valid(rfkill->pdata->gps_sync.io))
827 //        gpio_free(rfkill->pdata->gps_sync.io);
828 //    
829 //    if (gpio_is_valid(rfkill->pdata->ANTSEL2.io))
830 //        gpio_free(rfkill->pdata->ANTSEL2.io);
831 //
832 //    if (gpio_is_valid(rfkill->pdata->ANTSEL3.io))
833 //        gpio_free(rfkill->pdata->ANTSEL3.io);
834 //    
835 //    if (gpio_is_valid(rfkill->pdata->GPS_LAN.io))
836 //        gpio_free(rfkill->pdata->GPS_LAN.io);
837
838     kfree(rfkill);
839     g_rfkill = NULL;
840
841         return 0;
842 }
843
844 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
845 {
846     LOG("Enter %s\n", __func__);
847     return 0;
848 }
849
850 static int rfkill_wlan_resume(struct platform_device *pdev)
851 {
852     LOG("Enter %s\n", __func__);
853     return 0;
854 }
855
856 #ifdef CONFIG_OF
857 static struct of_device_id wlan_platdata_of_match[] = {
858     { .compatible = "wlan-platdata" },
859     { }
860 };
861 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
862 #endif //CONFIG_OF
863
864 static struct platform_driver rfkill_wlan_driver = {
865         .probe = rfkill_wlan_probe,
866         .remove = rfkill_wlan_remove,
867     .suspend = rfkill_wlan_suspend,
868     .resume = rfkill_wlan_resume,
869         .driver = {
870                 .name = "wlan-platdata",
871                 .owner = THIS_MODULE,
872         .of_match_table = of_match_ptr(wlan_platdata_of_match),
873         },
874 };
875
876 static int __init rfkill_wlan_init(void)
877 {
878     LOG("Enter %s\n", __func__);
879         return platform_driver_register(&rfkill_wlan_driver);
880 }
881
882 static void __exit rfkill_wlan_exit(void)
883 {
884     LOG("Enter %s\n", __func__);
885         platform_driver_unregister(&rfkill_wlan_driver);
886 }
887
888 module_init(rfkill_wlan_init);
889 module_exit(rfkill_wlan_exit);
890
891 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
892 MODULE_AUTHOR("gwl@rock-chips.com");
893 MODULE_LICENSE("GPL");