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