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