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