wifi: modify wifi plat data.
[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/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 <dt-bindings/gpio/gpio.h>
35 #ifdef CONFIG_OF
36 #include <linux/of.h>
37 #include <linux/of_device.h>
38 #include <linux/of_gpio.h>
39 #endif
40
41 #if 0
42 #define DBG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
43 #else
44 #define DBG(x...)
45 #endif
46
47 #define LOG(x...)   printk(KERN_INFO "[WLAN_RFKILL]: "x)
48
49 struct rfkill_wlan_data {
50         struct rksdmmc_gpio_wifi_moudle *pdata;
51     struct wake_lock            wlan_irq_wl;
52 };
53
54 static struct rfkill_wlan_data *g_rfkill = NULL;
55
56 static const char wlan_name[] = 
57 #if defined (CONFIG_BCM4330)
58     #if defined (CONFIG_BT_MODULE_NH660)
59         "nh660"
60     #else
61         "bcm4330"
62     #endif
63 #elif defined (CONFIG_RK903)
64     #if defined(CONFIG_RKWIFI_26M)
65         "rk903_26M"
66     #else
67         "rk903"
68     #endif
69 #elif defined(CONFIG_BCM4329)
70         "bcm4329"
71 #elif defined(CONFIG_MV8787)
72         "mv8787"
73 #elif defined(CONFIG_AP6210)
74     #if defined(CONFIG_RKWIFI_26M)
75         "ap6210"
76     #else
77         "ap6210_24M"
78     #endif
79 #elif defined(CONFIG_AP6330)
80                 "ap6330"
81 #elif defined(CONFIG_AP6476)
82                 "ap6476"
83 #elif defined(CONFIG_AP6493)
84                 "ap6493"
85 #else
86         "wlan_default"
87 #endif
88 ;
89
90 /***********************************************************
91  * 
92  * Broadcom Wifi Static Memory
93  * 
94  **********************************************************/
95 #define BCM_STATIC_MEMORY_SUPPORT 0
96 //===========================
97 #if BCM_STATIC_MEMORY_SUPPORT
98 #define PREALLOC_WLAN_SEC_NUM           4
99 #define PREALLOC_WLAN_BUF_NUM           160
100 #define PREALLOC_WLAN_SECTION_HEADER    24
101 #define WLAN_SKB_BUF_NUM        16
102
103 #define WLAN_SECTION_SIZE_0     (PREALLOC_WLAN_BUF_NUM * 128)
104 #define WLAN_SECTION_SIZE_1     (PREALLOC_WLAN_BUF_NUM * 128)
105 #define WLAN_SECTION_SIZE_2     (PREALLOC_WLAN_BUF_NUM * 512)
106 #define WLAN_SECTION_SIZE_3     (PREALLOC_WLAN_BUF_NUM * 1024)
107 #define WLAN_SECTION_SIZE_5     (PREALLOC_WLAN_BUF_NUM * 512)
108
109 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM];
110
111 struct wifi_mem_prealloc {
112     void *mem_ptr;
113     unsigned long size;
114 };
115
116 static struct wifi_mem_prealloc wifi_mem_array[5] = {
117     {NULL, (WLAN_SECTION_SIZE_0 + PREALLOC_WLAN_SECTION_HEADER)},
118     {NULL, (WLAN_SECTION_SIZE_1 + PREALLOC_WLAN_SECTION_HEADER)},
119     {NULL, (WLAN_SECTION_SIZE_2 + PREALLOC_WLAN_SECTION_HEADER)},
120     {NULL, (WLAN_SECTION_SIZE_3 + PREALLOC_WLAN_SECTION_HEADER)},
121     {NULL, (WLAN_SECTION_SIZE_5 + PREALLOC_WLAN_SECTION_HEADER)}
122 };
123
124 static int __init rockchip_init_wifi_mem(void)
125 {
126     int i;
127     int j;
128
129     for (i = 0 ; i < WLAN_SKB_BUF_NUM ; i++) {
130         wlan_static_skb[i] = dev_alloc_skb(
131                ((i < (WLAN_SKB_BUF_NUM / 2)) ? 4096 : 8192));
132
133         if (!wlan_static_skb[i])
134             goto err_skb_alloc;
135     }
136
137     for (i = 0 ; i < 5; i++) {
138         wifi_mem_array[i].mem_ptr =
139                kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
140
141         if (!wifi_mem_array[i].mem_ptr)
142             goto err_mem_alloc;
143         }
144         return 0;
145
146 err_mem_alloc:
147     pr_err("Failed to mem_alloc for WLAN\n");
148     for (j = 0 ; j < i ; j++)
149         kfree(wifi_mem_array[j].mem_ptr);
150     i = WLAN_SKB_BUF_NUM;
151 err_skb_alloc:
152     pr_err("Failed to skb_alloc for WLAN\n");
153     for (j = 0 ; j < i ; j++)
154         dev_kfree_skb(wlan_static_skb[j]);
155
156     return -ENOMEM;
157 }
158
159 void *rockchip_mem_prealloc(int section, unsigned long size)
160 {
161     if (section == PREALLOC_WLAN_SEC_NUM)
162         return wlan_static_skb;
163
164     if ((section < 0) || (section > 5))
165         return NULL;
166
167     if (section == 5)
168         return wifi_mem_array[4].mem_ptr;
169
170     if (wifi_mem_array[section].size < size)
171         return NULL;
172
173     return wifi_mem_array[section].mem_ptr;
174 }
175 #else
176 void *rockchip_mem_prealloc(int section, unsigned long size) { return NULL;}
177 #endif
178 EXPORT_SYMBOL(rockchip_mem_prealloc);
179
180 /**************************************************************************
181  *
182  * Wifi Power Control Func
183  * 0 -> power off
184  * 1 -> power on
185  *
186  *************************************************************************/
187 int rockchip_wifi_power(int on)
188 {
189         struct rfkill_wlan_data *mrfkill = g_rfkill;
190     struct rksdmmc_gpio *poweron, *reset;
191     struct regulator *ldo = NULL;
192
193     LOG("%s: %d\n", __func__, on);
194
195     if (mrfkill == NULL) {
196         LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
197         return -1;
198     }
199
200     if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
201         char *ldostr;
202         int ret = -1;
203         int level = mrfkill->pdata->mregulator.enable;
204
205         ldostr = mrfkill->pdata->mregulator.pmu_regulator;
206         if (ldostr == NULL) {
207             LOG("%s: wifi power set to be controled by pmic, but which one?\n", __func__);
208             return -1;
209         }
210         ldo = regulator_get(NULL, ldostr);
211         if (ldo == NULL) {
212             LOG("\n\n\n%s get ldo error,please mod this\n\n\n", __func__);
213         } else {
214                         if (on == level) {
215                                 regulator_set_voltage(ldo, 3000000, 3000000);
216                             LOG("%s: %s enabled\n", __func__, ldostr);
217                                 ret = regulator_enable(ldo);
218                                 if(ret != 0){
219                                     LOG("%s: faild to enable %s\n", __func__, ldostr);
220                                 }
221                             LOG("wifi turn on power.\n");
222             } else {
223                             LOG("wifi shut off power.\n");
224                                 LOG("%s: %s disabled\n", __func__, ldostr);
225                                 ret = regulator_disable(ldo);
226                                 if(ret != 0){
227                                         LOG("%s: faild to disable %s\n", __func__, ldostr);
228                                 }
229                         }
230                         regulator_put(ldo);
231                         mdelay(100);
232                 }
233     } else {
234                 poweron = &mrfkill->pdata->power_n;
235                 reset = &mrfkill->pdata->reset_n;
236
237                 if (on){
238                         if (gpio_is_valid(poweron->io)) {
239                                 gpio_set_value(poweron->io, poweron->enable);
240                         }
241                         mdelay(100);
242
243                         if (gpio_is_valid(reset->io)) {
244                                 gpio_set_value(reset->io, reset->enable);
245                         }
246                         mdelay(100);
247                         LOG("wifi turn on power. %d\n", poweron->io);
248                 }else{
249                         if (gpio_is_valid(poweron->io)) {
250                                 gpio_set_value(poweron->io, !(poweron->enable));
251                         }
252
253                         mdelay(100);
254                         if (gpio_is_valid(reset->io)) {
255                                 gpio_set_value(reset->io, !(reset->enable));
256                         }
257
258                         LOG("wifi shut off power.\n");
259                 }
260     }
261
262     return 0;
263 }
264 EXPORT_SYMBOL(rockchip_wifi_power);
265
266 /**************************************************************************
267  *
268  * Wifi Reset Func
269  *
270  *************************************************************************/
271 int rockchip_wifi_reset(int on)
272 {
273     return 0;
274 }
275 EXPORT_SYMBOL(rockchip_wifi_reset);
276
277 /**************************************************************************
278  *
279  * Wifi MAC custom Func
280  *
281  *************************************************************************/
282 #include <linux/etherdevice.h>
283 u8 wifi_custom_mac_addr[6] = {0,0,0,0,0,0};
284 extern char GetSNSectorInfo(char * pbuf);
285 int rockchip_wifi_mac_addr(unsigned char *buf)
286 {
287     char mac_buf[20] = {0};
288     LOG("%s: enter.\n", __func__);
289
290     // from vflash
291     if(is_zero_ether_addr(wifi_custom_mac_addr)) {
292         int i;
293         char *tempBuf = kmalloc(512, GFP_KERNEL);
294         if(tempBuf) {
295             GetSNSectorInfo(tempBuf);
296             for (i = 506; i <= 511; i++)
297                 wifi_custom_mac_addr[i-506] = tempBuf[i];
298             kfree(tempBuf);
299         } else {
300             return -1;
301         }
302     }
303
304     sprintf(mac_buf,"%02x:%02x:%02x:%02x:%02x:%02x",wifi_custom_mac_addr[0],wifi_custom_mac_addr[1],
305     wifi_custom_mac_addr[2],wifi_custom_mac_addr[3],wifi_custom_mac_addr[4],wifi_custom_mac_addr[5]);
306     LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
307
308     if (is_valid_ether_addr(wifi_custom_mac_addr)) {
309         if (2 == (wifi_custom_mac_addr[0] & 0x0F)) {
310             LOG("This mac address come into conflict with the address of direct, ignored...\n");
311             return -1;
312         }
313     } else {
314         LOG("This mac address is not valid, ignored...\n");
315         return -1;
316     }
317
318 #if defined(CONFIG_RKWIFI)
319     memcpy(buf, wifi_custom_mac_addr, 6);
320 #else
321     memcpy(buf, mac_buf, strlen(mac_buf));//realtek's wifi use this branch
322 #endif
323     return 0;
324 }
325 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
326
327 /**************************************************************************
328  *
329  * wifi get country code func
330  *
331  *************************************************************************/
332 struct cntry_locales_custom {
333     char iso_abbrev[4];  /* ISO 3166-1 country abbreviation */
334     char custom_locale[4];   /* Custom firmware locale */
335     int custom_locale_rev;        /* Custom local revisin default -1 */
336 };
337
338 static struct cntry_locales_custom country_cloc;
339
340 void *rockchip_wifi_country_code(char *ccode)
341 {
342     struct cntry_locales_custom *mcloc;
343
344     LOG("%s: set country code [%s]\n", __func__, ccode);
345     mcloc = &country_cloc;
346     memcpy(mcloc->custom_locale, ccode, 4);
347     mcloc->custom_locale_rev = 0;
348
349     return mcloc;
350 }
351 EXPORT_SYMBOL(rockchip_wifi_country_code);
352 /**************************************************************************/
353
354 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char* prefix, const char* name)
355 {
356     if (gpio_is_valid(gpio->io)) {
357         int ret=0;
358         sprintf(gpio->name, "%s_%s", prefix, name);
359         ret = gpio_request(gpio->io, gpio->name);
360         if (ret) {
361             LOG("Failed to get %s gpio.\n", gpio->name);
362             return -1;
363         }
364     }
365
366     return 0;
367 }
368
369 #ifdef CONFIG_OF
370 static int wlan_platdata_parse_dt(struct device *dev,
371                   struct rksdmmc_gpio_wifi_moudle *data)
372 {
373     struct device_node *node = dev->of_node;
374     const char *strings;
375     u32 value;
376     int gpio,ret;
377     enum of_gpio_flags flags;
378
379     if (!node)
380         return -ENODEV;
381
382     memset(data, 0, sizeof(*data));
383
384     ret = of_property_read_u32(node, "sdio_vref", &value);
385     if (ret < 0)
386         LOG("%s: Can't get sdio vref.", __func__);
387
388     data->sdio_vol = value;
389
390     if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
391         data->mregulator.power_ctrl_by_pmu = true;
392         ret = of_property_read_string(node, "pmu_regulator", &strings);
393         if (ret) {
394             LOG("%s: Can not read property: pmu_regulator.\n", __func__);
395             data->mregulator.power_ctrl_by_pmu = false;
396         } else {
397             LOG("%s: wifi power controled by pmu(%s).\n", __func__, strings);
398             sprintf(data->mregulator.pmu_regulator, "%s", strings);
399         }
400         ret = of_property_read_u32(node, "pmu_enable_level", &value);
401         if (ret) {
402             LOG("%s: Can not read property: pmu_enable_level.\n", __func__);
403             data->mregulator.power_ctrl_by_pmu = false;
404         } else {
405             LOG("%s: wifi power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
406             data->mregulator.enable = value;
407         }
408         } else {
409                 data->mregulator.power_ctrl_by_pmu = false;
410                 LOG("%s: wifi power controled by gpio.\n", __func__);
411         gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0, &flags);
412         if (gpio_is_valid(gpio)){
413                         data->power_n.io = gpio;
414                         data->power_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
415                         LOG("%s: get property: WIFI,poweren_gpio = %d, flags = %d.\n", __func__, gpio, flags);
416         }
417         gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0, &flags);
418         if (gpio_is_valid(gpio)){
419                         data->reset_n.io = gpio;
420                         data->reset_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
421                         LOG("%s: get property: WIFI,reset_gpio = %d, flags = %d.\n", __func__, gpio, flags);
422         }
423         gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0, &flags);
424         if (gpio_is_valid(gpio)){
425                         data->wifi_int_b.io = gpio;
426                         data->wifi_int_b.enable = flags;
427                         LOG("%s: get property: WIFI,host_wake_irq = %d, flags = %d.\n", __func__, gpio, flags);
428         }
429         }
430
431     return 0;
432 }
433 #endif //CONFIG_OF
434
435 #if defined(CONFIG_HAS_EARLYSUSPEND)
436 #include <linux/earlysuspend.h>
437
438 static void wlan_early_suspend(struct early_suspend *h)
439 {
440     LOG("%s :enter\n", __func__);
441
442     return;
443 }
444
445 static void wlan_late_resume(struct early_suspend *h)
446 {
447     LOG("%s :enter\n", __func__);
448
449     return;
450 }
451
452 struct early_suspend wlan_early_suspend {
453     .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
454     .suspend = wlan_early_suspend;
455     .resume = wlan_late_resume; 
456 }
457 #endif
458
459 static int rfkill_wlan_probe(struct platform_device *pdev)
460 {
461         struct rfkill_wlan_data *rfkill;
462         struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
463         int ret = -1;
464
465     LOG("Enter %s\n", __func__);
466
467         if (!pdata) {
468 #ifdef CONFIG_OF
469         pdata = kzalloc(sizeof(struct rksdmmc_gpio_wifi_moudle), GFP_KERNEL);
470         if (!pdata)
471             return -ENOMEM;
472
473         ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
474         if (ret < 0) {
475 #endif
476                     LOG("%s: No platform data specified\n", __func__);
477             return ret;
478 #ifdef CONFIG_OF
479         }
480 #endif
481         }
482
483         rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
484         if (!rfkill)
485         goto rfkill_alloc_fail;
486
487         rfkill->pdata = pdata;
488     g_rfkill = rfkill;
489
490     LOG("%s: init gpio\n", __func__);
491
492     if (!pdata->mregulator.power_ctrl_by_pmu) {
493         ret = rfkill_rk_setup_gpio(&pdata->power_n, wlan_name, "wlan_poweren");
494         if (ret) goto fail_alloc;
495
496         ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name, "wlan_reset");
497         if (ret) goto fail_alloc;
498     }
499
500     wake_lock_init(&(rfkill->wlan_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_wlan_wake");
501
502     // Turn off wifi power as default
503     if (gpio_is_valid(pdata->power_n.io))
504     {
505         gpio_direction_output(pdata->power_n.io, pdata->power_n.enable);
506     }
507
508 #if BCM_STATIC_MEMORY_SUPPORT
509     rockchip_init_wifi_mem();
510 #endif
511
512 #if defined(CONFIG_HAS_EARLYSUSPEND)
513     register_early_suspend(wlan_early_suspend);
514 #endif
515
516     LOG("Exit %s\n", __func__);
517
518         return 0;
519
520 fail_alloc:
521         kfree(rfkill);
522 rfkill_alloc_fail:
523     kfree(pdata);
524
525     g_rfkill = NULL;
526
527         return ret;
528 }
529
530 static int rfkill_wlan_remove(struct platform_device *pdev)
531 {
532         struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
533
534     LOG("Enter %s\n", __func__);
535
536     wake_lock_destroy(&rfkill->wlan_irq_wl);
537     
538     if (gpio_is_valid(rfkill->pdata->power_n.io))
539         gpio_free(rfkill->pdata->power_n.io);
540     
541     if (gpio_is_valid(rfkill->pdata->reset_n.io))
542         gpio_free(rfkill->pdata->reset_n.io);
543     
544 //    if (gpio_is_valid(rfkill->pdata->vddio.io))
545 //        gpio_free(rfkill->pdata->vddio.io);
546 //
547 //    if (gpio_is_valid(rfkill->pdata->bgf_int_b.io))
548 //        gpio_free(rfkill->pdata->bgf_int_b.io);
549 //    
550 //    if (gpio_is_valid(rfkill->pdata->gps_sync.io))
551 //        gpio_free(rfkill->pdata->gps_sync.io);
552 //    
553 //    if (gpio_is_valid(rfkill->pdata->ANTSEL2.io))
554 //        gpio_free(rfkill->pdata->ANTSEL2.io);
555 //
556 //    if (gpio_is_valid(rfkill->pdata->ANTSEL3.io))
557 //        gpio_free(rfkill->pdata->ANTSEL3.io);
558 //    
559 //    if (gpio_is_valid(rfkill->pdata->GPS_LAN.io))
560 //        gpio_free(rfkill->pdata->GPS_LAN.io);
561
562     kfree(rfkill);
563     g_rfkill = NULL;
564
565         return 0;
566 }
567
568 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
569 {
570     LOG("Enter %s\n", __func__);
571     return 0;
572 }
573
574 static int rfkill_wlan_resume(struct platform_device *pdev)
575 {
576     LOG("Enter %s\n", __func__);
577     return 0;
578 }
579
580 #ifdef CONFIG_OF
581 static struct of_device_id wlan_platdata_of_match[] = {
582     { .compatible = "wlan-platdata" },
583     { }
584 };
585 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
586 #endif //CONFIG_OF
587
588 static struct platform_driver rfkill_wlan_driver = {
589         .probe = rfkill_wlan_probe,
590         .remove = rfkill_wlan_remove,
591     .suspend = rfkill_wlan_suspend,
592     .resume = rfkill_wlan_resume,
593         .driver = {
594                 .name = "wlan-platdata",
595                 .owner = THIS_MODULE,
596         .of_match_table = of_match_ptr(wlan_platdata_of_match),
597         },
598 };
599
600 static int __init rfkill_wlan_init(void)
601 {
602     LOG("Enter %s\n", __func__);
603         return platform_driver_register(&rfkill_wlan_driver);
604 }
605
606 static void __exit rfkill_wlan_exit(void)
607 {
608     LOG("Enter %s\n", __func__);
609         platform_driver_unregister(&rfkill_wlan_driver);
610 }
611
612 module_init(rfkill_wlan_init);
613 module_exit(rfkill_wlan_exit);
614
615 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
616 MODULE_AUTHOR("gwl@rock-chips.com");
617 MODULE_LICENSE("GPL");