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