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