WiFi&BT add io reference voltage control to reducing power consumption, default is...
[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 <asm/gpio.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/delay.h>
26 #include <linux/rfkill-wlan.h>
27 #include <linux/rfkill-bt.h>
28 #include <linux/wakelock.h>
29 #include <linux/interrupt.h>
30 #include <asm/irq.h>
31 #include <linux/suspend.h>
32 #include <linux/proc_fs.h>
33 #include <linux/uaccess.h>
34 #include <linux/gpio.h>
35 #include <linux/rockchip/iomap.h>
36 #include <dt-bindings/gpio/gpio.h>
37 #ifdef CONFIG_OF
38 #include <linux/of.h>
39 #include <linux/of_device.h>
40 #include <linux/of_gpio.h>
41 #endif
42
43 #if 0
44 #define DBG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
45 #else
46 #define DBG(x...)
47 #endif
48
49 #define LOG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
50
51 struct rfkill_wlan_data {
52         struct rksdmmc_gpio_wifi_moudle *pdata;
53     struct wake_lock            wlan_irq_wl;
54 };
55
56 static struct rfkill_wlan_data *g_rfkill = NULL;
57
58 static const char wlan_name[] = 
59 #if defined (CONFIG_BCM4330)
60     #if defined (CONFIG_BT_MODULE_NH660)
61         "nh660"
62     #else
63         "bcm4330"
64     #endif
65 #elif defined (CONFIG_RK903)
66     #if defined(CONFIG_RKWIFI_26M)
67         "rk903_26M"
68     #else
69         "rk903"
70     #endif
71 #elif defined(CONFIG_BCM4329)
72         "bcm4329"
73 #elif defined(CONFIG_MV8787)
74         "mv8787"
75 #elif defined(CONFIG_AP6210)
76     #if defined(CONFIG_RKWIFI_26M)
77         "ap6210"
78     #else
79         "ap6210_24M"
80     #endif
81 #elif defined(CONFIG_AP6330)
82                 "ap6330"
83 #elif defined(CONFIG_AP6476)
84                 "ap6476"
85 #elif defined(CONFIG_AP6493)
86                 "ap6493"
87 #else
88         "wlan_default"
89 #endif
90 ;
91
92 /***********************************************************
93  * 
94  * Broadcom Wifi Static Memory
95  * 
96  **********************************************************/
97 #define BCM_STATIC_MEMORY_SUPPORT 0
98 //===========================
99 #if BCM_STATIC_MEMORY_SUPPORT
100 #define PREALLOC_WLAN_SEC_NUM           4
101 #define PREALLOC_WLAN_BUF_NUM           160
102 #define PREALLOC_WLAN_SECTION_HEADER    24
103 #define WLAN_SKB_BUF_NUM        16
104
105 #define WLAN_SECTION_SIZE_0     (PREALLOC_WLAN_BUF_NUM * 128)
106 #define WLAN_SECTION_SIZE_1     (PREALLOC_WLAN_BUF_NUM * 128)
107 #define WLAN_SECTION_SIZE_2     (PREALLOC_WLAN_BUF_NUM * 512)
108 #define WLAN_SECTION_SIZE_3     (PREALLOC_WLAN_BUF_NUM * 1024)
109 #define WLAN_SECTION_SIZE_5     (PREALLOC_WLAN_BUF_NUM * 512)
110
111 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM];
112
113 struct wifi_mem_prealloc {
114     void *mem_ptr;
115     unsigned long size;
116 };
117
118 static struct wifi_mem_prealloc wifi_mem_array[5] = {
119     {NULL, (WLAN_SECTION_SIZE_0 + PREALLOC_WLAN_SECTION_HEADER)},
120     {NULL, (WLAN_SECTION_SIZE_1 + PREALLOC_WLAN_SECTION_HEADER)},
121     {NULL, (WLAN_SECTION_SIZE_2 + PREALLOC_WLAN_SECTION_HEADER)},
122     {NULL, (WLAN_SECTION_SIZE_3 + PREALLOC_WLAN_SECTION_HEADER)},
123     {NULL, (WLAN_SECTION_SIZE_5 + PREALLOC_WLAN_SECTION_HEADER)}
124 };
125
126 static int __init rockchip_init_wifi_mem(void)
127 {
128     int i;
129     int j;
130
131     for (i = 0 ; i < WLAN_SKB_BUF_NUM ; i++) {
132         wlan_static_skb[i] = dev_alloc_skb(
133                ((i < (WLAN_SKB_BUF_NUM / 2)) ? 4096 : 8192));
134
135         if (!wlan_static_skb[i])
136             goto err_skb_alloc;
137     }
138
139     for (i = 0 ; i < 5; i++) {
140         wifi_mem_array[i].mem_ptr =
141                kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
142
143         if (!wifi_mem_array[i].mem_ptr)
144             goto err_mem_alloc;
145         }
146         return 0;
147
148 err_mem_alloc:
149     pr_err("Failed to mem_alloc for WLAN\n");
150     for (j = 0 ; j < i ; j++)
151         kfree(wifi_mem_array[j].mem_ptr);
152     i = WLAN_SKB_BUF_NUM;
153 err_skb_alloc:
154     pr_err("Failed to skb_alloc for WLAN\n");
155     for (j = 0 ; j < i ; j++)
156         dev_kfree_skb(wlan_static_skb[j]);
157
158     return -ENOMEM;
159 }
160
161 void *rockchip_mem_prealloc(int section, unsigned long size)
162 {
163     if (section == PREALLOC_WLAN_SEC_NUM)
164         return wlan_static_skb;
165
166     if ((section < 0) || (section > 5))
167         return NULL;
168
169     if (section == 5)
170         return wifi_mem_array[4].mem_ptr;
171
172     if (wifi_mem_array[section].size < size)
173         return NULL;
174
175     return wifi_mem_array[section].mem_ptr;
176 }
177 #else
178 void *rockchip_mem_prealloc(int section, unsigned long size) { return NULL;}
179 #endif
180 EXPORT_SYMBOL(rockchip_mem_prealloc);
181
182 /**************************************************************************
183  *
184  * get wifi power state Func
185  *
186  *************************************************************************/
187 static int wifi_power_state = 0;
188 int rfkill_get_wifi_power_state(int *power, int *vref_ctrl_enable)
189 {
190     struct rfkill_wlan_data *mrfkill = g_rfkill;
191
192     if (mrfkill == NULL) {
193         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
194         return -1;
195     }
196
197     if (mrfkill->pdata->vref_ctrl_enble)
198         *vref_ctrl_enable = 1;
199     *power = wifi_power_state;
200
201     return 0;
202 }
203
204 /**************************************************************************
205  *
206  * wifi reference voltage control Func
207  *
208  *************************************************************************/
209 int rockchip_wifi_ref_voltage(int on)
210 {
211     struct rfkill_wlan_data *mrfkill = g_rfkill;
212     struct rksdmmc_gpio *vddio;
213     struct regulator *ldo = NULL;
214     int power = 0;
215     bool toggle = false;
216
217     LOG("%s: %d\n", __func__, on);
218
219     if (mrfkill == NULL) {
220         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
221         return -1;
222     }
223
224     if (!mrfkill->pdata->vref_ctrl_enble) {
225         LOG("%s: wifi io reference voltage control is disabled.\n", __func__);
226         return 0;
227     }
228
229     if (!rfkill_get_bt_power_state(&power, &toggle)) {
230         if (power == 1) {
231             LOG("%s: wifi shouldn't control io reference voltage, BT is running!\n", __func__);
232             return 0;
233         }
234     }
235
236     if (mrfkill->pdata->ioregulator.power_ctrl_by_pmu) {
237         int ret = -1;
238         char *ldostr;
239         int level = mrfkill->pdata->ioregulator.enable;
240         int voltage = 1000 * mrfkill->pdata->sdio_vol;
241
242         ldostr = mrfkill->pdata->ioregulator.pmu_regulator;
243         if (ldostr == NULL) {
244             LOG("%s: wifi io reference voltage set to be controled by pmic, but which one?\n", __func__);
245             return -1;
246         }
247         ldo = regulator_get(NULL, ldostr);
248         if (ldo == NULL || IS_ERR(ldo)) {
249             LOG("\n\n\n%s get ldo error,please mod this\n\n\n", __func__);
250             return -1;
251         } else {
252             if (on == level) {
253                 regulator_set_voltage(ldo, voltage, voltage);
254                 LOG("%s: %s enabled, level = %d\n", __func__, ldostr, voltage);
255                 ret = regulator_enable(ldo);
256                 LOG("wifi turn on io reference voltage.\n");
257             } else {
258                 LOG("%s: %s disabled\n", __func__, ldostr);
259                 while (regulator_is_enabled(ldo) > 0) {
260                     ret = regulator_disable(ldo);
261                 }
262                 LOG("wifi shut off io reference voltage.\n");
263             }
264             regulator_put(ldo);
265             msleep(100);
266         }
267     } else {
268         vddio = &mrfkill->pdata->power_n;
269
270         if (on){
271             if (gpio_is_valid(vddio->io)) {
272                 gpio_set_value(vddio->io, vddio->enable);
273                 msleep(100);
274             }
275
276             LOG("wifi turn on io reference voltage.\n");
277         }else{
278             if (gpio_is_valid(vddio->io)) {
279                 gpio_set_value(vddio->io, !(vddio->enable));
280                 msleep(100);
281             }
282
283             LOG("wifi shut off io reference voltage.\n");
284         }
285     }
286
287         return 0;
288 }
289
290 /**************************************************************************
291  *
292  * Wifi Power Control Func
293  * 0 -> power off
294  * 1 -> power on
295  *
296  *************************************************************************/
297 int rockchip_wifi_power(int on)
298 {
299         struct rfkill_wlan_data *mrfkill = g_rfkill;
300     struct rksdmmc_gpio *poweron, *reset;
301     struct regulator *ldo = NULL;
302     int power = 0;
303     bool toggle = false;
304
305     LOG("%s: %d\n", __func__, on);
306
307     if (mrfkill == NULL) {
308         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
309         return -1;
310     }
311
312     if (!rfkill_get_bt_power_state(&power, &toggle)) {
313         if (toggle == true && power == 1) {
314             LOG("%s: wifi shouldn't control the power, it was enabled by BT!\n", __func__);
315             return 0;
316         }
317     }
318     
319     if (on)
320         rockchip_wifi_ref_voltage(1);
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     if (!on)
387         rockchip_wifi_ref_voltage(0);
388
389     return 0;
390 }
391 EXPORT_SYMBOL(rockchip_wifi_power);
392
393 /**************************************************************************
394  *
395  * Wifi Sdio Detect Func
396  *
397  *************************************************************************/
398 #include <linux/mmc/host.h>
399 extern int mmc_host_rescan(struct mmc_host *host, int val);
400 int rockchip_wifi_set_carddetect(int val)
401 {
402     return mmc_host_rescan(NULL, val);//NULL => SDIO host
403 }
404 EXPORT_SYMBOL(rockchip_wifi_set_carddetect);
405
406 /**************************************************************************
407  *
408  * Wifi Get Interrupt irq Func
409  *
410  *************************************************************************/
411 int rockchip_wifi_get_oob_irq(void)
412 {
413     struct rfkill_wlan_data *mrfkill = g_rfkill;
414     struct rksdmmc_gpio *wifi_int_irq;
415
416     LOG("%s: Enter\n", __func__);
417
418     if (mrfkill == NULL) {
419         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
420         return -1;
421     }
422     
423     wifi_int_irq = &mrfkill->pdata->wifi_int_b;
424     if (gpio_is_valid(wifi_int_irq->io)) {
425         return gpio_to_irq(wifi_int_irq->io);
426         //return wifi_int_irq->io;
427     } else {
428         LOG("%s: wifi OOB pin isn't defined.\n", __func__);
429     }
430     
431     return -1;
432 }
433 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq);
434
435 /**************************************************************************
436  *
437  * Wifi Reset Func
438  *
439  *************************************************************************/
440 int rockchip_wifi_reset(int on)
441 {
442     return 0;
443 }
444 EXPORT_SYMBOL(rockchip_wifi_reset);
445
446 /**************************************************************************
447  *
448  * Wifi MAC custom Func
449  *
450  *************************************************************************/
451 #include <linux/etherdevice.h>
452 u8 wifi_custom_mac_addr[6] = {0,0,0,0,0,0};
453 extern char GetSNSectorInfo(char * pbuf);
454 int rockchip_wifi_mac_addr(unsigned char *buf)
455 {
456     return -1;
457     char mac_buf[20] = {0};
458     LOG("%s: enter.\n", __func__);
459
460     // from vflash
461     if(is_zero_ether_addr(wifi_custom_mac_addr)) {
462         int i;
463         char *tempBuf = kmalloc(512, GFP_KERNEL);
464         if(tempBuf) {
465             GetSNSectorInfo(tempBuf);
466             for (i = 506; i <= 511; i++)
467                 wifi_custom_mac_addr[i-506] = tempBuf[i];
468             kfree(tempBuf);
469         } else {
470             return -1;
471         }
472     }
473
474     sprintf(mac_buf,"%02x:%02x:%02x:%02x:%02x:%02x",wifi_custom_mac_addr[0],wifi_custom_mac_addr[1],
475     wifi_custom_mac_addr[2],wifi_custom_mac_addr[3],wifi_custom_mac_addr[4],wifi_custom_mac_addr[5]);
476     LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
477
478     if (is_valid_ether_addr(wifi_custom_mac_addr)) {
479         if (2 == (wifi_custom_mac_addr[0] & 0x0F)) {
480             LOG("This mac address come into conflict with the address of direct, ignored...\n");
481             return -1;
482         }
483     } else {
484         LOG("This mac address is not valid, ignored...\n");
485         return -1;
486     }
487
488 #if defined(CONFIG_RKWIFI)
489     memcpy(buf, wifi_custom_mac_addr, 6);
490 #else
491     memcpy(buf, mac_buf, strlen(mac_buf));//realtek's wifi use this branch
492 #endif
493     return 0;
494 }
495 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
496
497 /**************************************************************************
498  *
499  * wifi get country code func
500  *
501  *************************************************************************/
502 struct cntry_locales_custom {
503     char iso_abbrev[4];  /* ISO 3166-1 country abbreviation */
504     char custom_locale[4];   /* Custom firmware locale */
505     int custom_locale_rev;        /* Custom local revisin default -1 */
506 };
507
508 static struct cntry_locales_custom country_cloc;
509
510 void *rockchip_wifi_country_code(char *ccode)
511 {
512     struct cntry_locales_custom *mcloc;
513
514     LOG("%s: set country code [%s]\n", __func__, ccode);
515     mcloc = &country_cloc;
516     memcpy(mcloc->custom_locale, ccode, 4);
517     mcloc->custom_locale_rev = 0;
518
519     return mcloc;
520 }
521 EXPORT_SYMBOL(rockchip_wifi_country_code);
522 /**************************************************************************/
523
524 static int rockchip_wifi_voltage_select(void)
525 {
526     struct rfkill_wlan_data *mrfkill = g_rfkill;
527     int voltage = 0;
528
529     if (mrfkill == NULL) {
530         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
531         return -1;
532     }
533     voltage = mrfkill->pdata->sdio_vol;
534     if (voltage > 2700 && voltage < 3500) {
535         writel_relaxed(0x00100000, RK_GRF_VIRT+0x380); //3.3
536         LOG("%s: wifi & sdio reference voltage: 3.3V\n", __func__);
537     } else if (voltage  > 1500 && voltage < 1950) {
538         writel_relaxed(0x00100010, RK_GRF_VIRT+0x380); //1.8
539         LOG("%s: wifi & sdio reference voltage: 1.8V\n", __func__);
540     } else {
541         LOG("%s: unsupport wifi & sdio reference voltage!\n", __func__);
542         return -1;
543     }
544
545     return 0;
546 }
547
548 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char* prefix, const char* name)
549 {
550     if (gpio_is_valid(gpio->io)) {
551         int ret=0;
552         sprintf(gpio->name, "%s_%s", prefix, name);
553         ret = gpio_request(gpio->io, gpio->name);
554         if (ret) {
555             LOG("Failed to get %s gpio.\n", gpio->name);
556             return -1;
557         }
558     }
559
560     return 0;
561 }
562
563 #ifdef CONFIG_OF
564 static int wlan_platdata_parse_dt(struct device *dev,
565                   struct rksdmmc_gpio_wifi_moudle *data)
566 {
567     struct device_node *node = dev->of_node;
568     const char *strings;
569     u32 value;
570     int gpio,ret;
571     enum of_gpio_flags flags;
572
573     if (!node)
574         return -ENODEV;
575
576     memset(data, 0, sizeof(*data));
577
578     ret = of_property_read_u32(node, "sdio_vref", &value);
579     if (ret < 0) {
580         LOG("%s: Can't get sdio vref.", __func__);
581         return -1;
582     }
583     data->sdio_vol = value;
584
585     if (of_find_property(node, "vref_ctrl_enable", NULL)) {
586         LOG("%s: enable wifi io reference voltage control.\n", __func__);
587         data->vref_ctrl_enble = true;
588         if (of_find_property(node, "vref_ctrl_gpio", NULL)) {
589             gpio = of_get_named_gpio_flags(node, "vref_ctrl_gpio", 0, &flags);
590             if (gpio_is_valid(gpio)){
591                 data->vddio.io = gpio;
592                 data->vddio.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
593                 data->ioregulator.power_ctrl_by_pmu = false;
594                 LOG("%s: get property: vref_ctrl_gpio = %d, flags = %d.\n", __func__, gpio, flags);
595             } else {
596                 data->vddio.io = -1;
597                 data->vref_ctrl_enble = false;
598                 LOG("%s: vref_ctrl_gpio defined invalid, disable wifi io reference voltage control.\n", __func__);
599             }
600         } else {
601             data->ioregulator.power_ctrl_by_pmu = true;
602             ret = of_property_read_string(node, "vref_pmu_regulator", &strings);
603             if (ret) {
604                 LOG("%s: Can not read property: vref_pmu_regulator.\n", __func__);
605                 data->vref_ctrl_enble = false;
606                 data->ioregulator.power_ctrl_by_pmu = false;
607             } else {
608                 LOG("%s: wifi io reference voltage controled by pmu(%s).\n", __func__, strings);
609                 sprintf(data->ioregulator.pmu_regulator, "%s", strings);
610             }
611             ret = of_property_read_u32(node, "vref_pmu_enable_level", &value);
612             if (ret) {
613                 LOG("%s: Can not read property: vref_pmu_enable_level.\n", __func__);
614                 data->vref_ctrl_enble = false;
615                 data->ioregulator.power_ctrl_by_pmu = false;
616             } else {
617                 LOG("%s: wifi io reference voltage controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
618                 data->ioregulator.enable = value;
619             }
620         }
621     } else {
622         data->vref_ctrl_enble = false;
623         LOG("%s: disable wifi io reference voltage control.\n", __func__);
624     }
625
626     if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
627         data->mregulator.power_ctrl_by_pmu = true;
628         ret = of_property_read_string(node, "power_pmu_regulator", &strings);
629         if (ret) {
630             LOG("%s: Can not read property: power_pmu_regulator.\n", __func__);
631             data->mregulator.power_ctrl_by_pmu = false;
632         } else {
633             LOG("%s: wifi power controled by pmu(%s).\n", __func__, strings);
634             sprintf(data->mregulator.pmu_regulator, "%s", strings);
635         }
636         ret = of_property_read_u32(node, "power_pmu_enable_level", &value);
637         if (ret) {
638             LOG("%s: Can not read property: power_pmu_enable_level.\n", __func__);
639             data->mregulator.power_ctrl_by_pmu = false;
640         } else {
641             LOG("%s: wifi power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
642             data->mregulator.enable = value;
643         }
644         } else {
645                 data->mregulator.power_ctrl_by_pmu = false;
646                 LOG("%s: wifi power controled by gpio.\n", __func__);
647         gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0, &flags);
648         if (gpio_is_valid(gpio)){
649                         data->power_n.io = gpio;
650                         data->power_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
651                         LOG("%s: get property: WIFI,poweren_gpio = %d, flags = %d.\n", __func__, gpio, flags);
652         } else data->power_n.io = -1;
653         gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0, &flags);
654         if (gpio_is_valid(gpio)){
655                         data->reset_n.io = gpio;
656                         data->reset_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
657                         LOG("%s: get property: WIFI,reset_gpio = %d, flags = %d.\n", __func__, gpio, flags);
658         } else data->reset_n.io = -1;
659         gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0, &flags);
660         if (gpio_is_valid(gpio)){
661                         data->wifi_int_b.io = gpio;
662                         data->wifi_int_b.enable = flags;
663                         LOG("%s: get property: WIFI,host_wake_irq = %d, flags = %d.\n", __func__, gpio, flags);
664         } else data->wifi_int_b.io = -1;
665         }
666
667     return 0;
668 }
669 #endif //CONFIG_OF
670
671 #if defined(CONFIG_HAS_EARLYSUSPEND)
672 #include <linux/earlysuspend.h>
673
674 static void wlan_early_suspend(struct early_suspend *h)
675 {
676     LOG("%s :enter\n", __func__);
677
678     return;
679 }
680
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 int rfkill_wlan_probe(struct platform_device *pdev)
696 {
697         struct rfkill_wlan_data *rfkill;
698         struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
699         int ret = -1;
700
701     LOG("Enter %s\n", __func__);
702
703         if (!pdata) {
704 #ifdef CONFIG_OF
705         pdata = kzalloc(sizeof(struct rksdmmc_gpio_wifi_moudle), GFP_KERNEL);
706         if (!pdata)
707             return -ENOMEM;
708
709         ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
710         if (ret < 0) {
711 #endif
712                     LOG("%s: No platform data specified\n", __func__);
713             return ret;
714 #ifdef CONFIG_OF
715         }
716 #endif
717         }
718
719         rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
720         if (!rfkill)
721         goto rfkill_alloc_fail;
722
723         rfkill->pdata = pdata;
724     g_rfkill = rfkill;
725
726     LOG("%s: init gpio\n", __func__);
727
728     if (!pdata->mregulator.power_ctrl_by_pmu) {
729         ret = rfkill_rk_setup_gpio(&pdata->power_n, wlan_name, "wlan_poweren");
730         if (ret) goto fail_alloc;
731
732         ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name, "wlan_reset");
733         if (ret) goto fail_alloc;
734     }
735
736     wake_lock_init(&(rfkill->wlan_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_wlan_wake");
737
738     // Turn off wifi power as default
739     if (gpio_is_valid(pdata->power_n.io))
740     {
741         gpio_direction_output(pdata->power_n.io, !pdata->power_n.enable);
742     }
743
744     rockchip_wifi_voltage_select();
745
746 #if BCM_STATIC_MEMORY_SUPPORT
747     rockchip_init_wifi_mem();
748 #endif
749
750 #if defined(CONFIG_HAS_EARLYSUSPEND)
751     register_early_suspend(wlan_early_suspend);
752 #endif
753
754     LOG("Exit %s\n", __func__);
755
756         return 0;
757
758 fail_alloc:
759         kfree(rfkill);
760 rfkill_alloc_fail:
761     kfree(pdata);
762
763     g_rfkill = NULL;
764
765         return ret;
766 }
767
768 static int rfkill_wlan_remove(struct platform_device *pdev)
769 {
770         struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
771
772     LOG("Enter %s\n", __func__);
773
774     wake_lock_destroy(&rfkill->wlan_irq_wl);
775     
776     if (gpio_is_valid(rfkill->pdata->power_n.io))
777         gpio_free(rfkill->pdata->power_n.io);
778     
779     if (gpio_is_valid(rfkill->pdata->reset_n.io))
780         gpio_free(rfkill->pdata->reset_n.io);
781     
782 //    if (gpio_is_valid(rfkill->pdata->vddio.io))
783 //        gpio_free(rfkill->pdata->vddio.io);
784 //
785 //    if (gpio_is_valid(rfkill->pdata->bgf_int_b.io))
786 //        gpio_free(rfkill->pdata->bgf_int_b.io);
787 //    
788 //    if (gpio_is_valid(rfkill->pdata->gps_sync.io))
789 //        gpio_free(rfkill->pdata->gps_sync.io);
790 //    
791 //    if (gpio_is_valid(rfkill->pdata->ANTSEL2.io))
792 //        gpio_free(rfkill->pdata->ANTSEL2.io);
793 //
794 //    if (gpio_is_valid(rfkill->pdata->ANTSEL3.io))
795 //        gpio_free(rfkill->pdata->ANTSEL3.io);
796 //    
797 //    if (gpio_is_valid(rfkill->pdata->GPS_LAN.io))
798 //        gpio_free(rfkill->pdata->GPS_LAN.io);
799
800     kfree(rfkill);
801     g_rfkill = NULL;
802
803         return 0;
804 }
805
806 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
807 {
808     LOG("Enter %s\n", __func__);
809     return 0;
810 }
811
812 static int rfkill_wlan_resume(struct platform_device *pdev)
813 {
814     LOG("Enter %s\n", __func__);
815     return 0;
816 }
817
818 #ifdef CONFIG_OF
819 static struct of_device_id wlan_platdata_of_match[] = {
820     { .compatible = "wlan-platdata" },
821     { }
822 };
823 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
824 #endif //CONFIG_OF
825
826 static struct platform_driver rfkill_wlan_driver = {
827         .probe = rfkill_wlan_probe,
828         .remove = rfkill_wlan_remove,
829     .suspend = rfkill_wlan_suspend,
830     .resume = rfkill_wlan_resume,
831         .driver = {
832                 .name = "wlan-platdata",
833                 .owner = THIS_MODULE,
834         .of_match_table = of_match_ptr(wlan_platdata_of_match),
835         },
836 };
837
838 static int __init rfkill_wlan_init(void)
839 {
840     LOG("Enter %s\n", __func__);
841         return platform_driver_register(&rfkill_wlan_driver);
842 }
843
844 static void __exit rfkill_wlan_exit(void)
845 {
846     LOG("Enter %s\n", __func__);
847         platform_driver_unregister(&rfkill_wlan_driver);
848 }
849
850 module_init(rfkill_wlan_init);
851 module_exit(rfkill_wlan_exit);
852
853 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
854 MODULE_AUTHOR("gwl@rock-chips.com");
855 MODULE_LICENSE("GPL");