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