2 * Copyright (C) 2012 ROCKCHIP, Inc.
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.
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.
14 /* Rock-chips rfkill driver for wifi
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>
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>
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>
38 #include <linux/of_device.h>
39 #include <linux/of_gpio.h>
43 #define DBG(x...) printk(KERN_INFO "[WLAN_RFKILL]: "x)
48 #define LOG(x...) printk(KERN_INFO "[WLAN_RFKILL]: "x)
50 struct rfkill_wlan_data {
51 struct rksdmmc_gpio_wifi_moudle *pdata;
52 struct wake_lock wlan_irq_wl;
55 static struct rfkill_wlan_data *g_rfkill = NULL;
57 static const char wlan_name[] =
58 #if defined (CONFIG_BCM4330)
59 #if defined (CONFIG_BT_MODULE_NH660)
64 #elif defined (CONFIG_RK903)
65 #if defined(CONFIG_RKWIFI_26M)
70 #elif defined(CONFIG_BCM4329)
72 #elif defined(CONFIG_MV8787)
74 #elif defined(CONFIG_AP6210)
75 #if defined(CONFIG_RKWIFI_26M)
80 #elif defined(CONFIG_AP6330)
82 #elif defined(CONFIG_AP6476)
84 #elif defined(CONFIG_AP6493)
91 /***********************************************************
93 * Broadcom Wifi Static Memory
95 **********************************************************/
96 #define BCM_STATIC_MEMORY_SUPPORT 0
97 //===========================
98 #if BCM_STATIC_MEMORY_SUPPORT
99 #define PREALLOC_WLAN_SEC_NUM 4
100 #define PREALLOC_WLAN_BUF_NUM 160
101 #define PREALLOC_WLAN_SECTION_HEADER 24
102 #define WLAN_SKB_BUF_NUM 16
104 #define WLAN_SECTION_SIZE_0 (PREALLOC_WLAN_BUF_NUM * 128)
105 #define WLAN_SECTION_SIZE_1 (PREALLOC_WLAN_BUF_NUM * 128)
106 #define WLAN_SECTION_SIZE_2 (PREALLOC_WLAN_BUF_NUM * 512)
107 #define WLAN_SECTION_SIZE_3 (PREALLOC_WLAN_BUF_NUM * 1024)
108 #define WLAN_SECTION_SIZE_5 (PREALLOC_WLAN_BUF_NUM * 512)
110 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM];
112 struct wifi_mem_prealloc {
117 static struct wifi_mem_prealloc wifi_mem_array[5] = {
118 {NULL, (WLAN_SECTION_SIZE_0 + PREALLOC_WLAN_SECTION_HEADER)},
119 {NULL, (WLAN_SECTION_SIZE_1 + PREALLOC_WLAN_SECTION_HEADER)},
120 {NULL, (WLAN_SECTION_SIZE_2 + PREALLOC_WLAN_SECTION_HEADER)},
121 {NULL, (WLAN_SECTION_SIZE_3 + PREALLOC_WLAN_SECTION_HEADER)},
122 {NULL, (WLAN_SECTION_SIZE_5 + PREALLOC_WLAN_SECTION_HEADER)}
125 static int __init rockchip_init_wifi_mem(void)
130 for (i = 0 ; i < WLAN_SKB_BUF_NUM ; i++) {
131 wlan_static_skb[i] = dev_alloc_skb(
132 ((i < (WLAN_SKB_BUF_NUM / 2)) ? 4096 : 8192));
134 if (!wlan_static_skb[i])
138 for (i = 0 ; i < 5; i++) {
139 wifi_mem_array[i].mem_ptr =
140 kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
142 if (!wifi_mem_array[i].mem_ptr)
148 pr_err("Failed to mem_alloc for WLAN\n");
149 for (j = 0 ; j < i ; j++)
150 kfree(wifi_mem_array[j].mem_ptr);
151 i = WLAN_SKB_BUF_NUM;
153 pr_err("Failed to skb_alloc for WLAN\n");
154 for (j = 0 ; j < i ; j++)
155 dev_kfree_skb(wlan_static_skb[j]);
160 void *rockchip_mem_prealloc(int section, unsigned long size)
162 if (section == PREALLOC_WLAN_SEC_NUM)
163 return wlan_static_skb;
165 if ((section < 0) || (section > 5))
169 return wifi_mem_array[4].mem_ptr;
171 if (wifi_mem_array[section].size < size)
174 return wifi_mem_array[section].mem_ptr;
177 void *rockchip_mem_prealloc(int section, unsigned long size) { return NULL;}
179 EXPORT_SYMBOL(rockchip_mem_prealloc);
181 /**************************************************************************
183 * Wifi Power Control Func
187 *************************************************************************/
188 int rockchip_wifi_power(int on)
190 struct rfkill_wlan_data *mrfkill = g_rfkill;
191 struct rksdmmc_gpio *poweron, *reset;
192 struct regulator *ldo = NULL;
194 LOG("%s: %d\n", __func__, on);
196 if (mrfkill == NULL) {
197 LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
201 if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
204 int level = mrfkill->pdata->mregulator.enable;
206 ldostr = mrfkill->pdata->mregulator.pmu_regulator;
207 if (ldostr == NULL) {
208 LOG("%s: wifi power set to be controled by pmic, but which one?\n", __func__);
211 ldo = regulator_get(NULL, ldostr);
213 LOG("\n\n\n%s get ldo error,please mod this\n\n\n", __func__);
216 regulator_set_voltage(ldo, 3000000, 3000000);
217 LOG("%s: %s enabled\n", __func__, ldostr);
218 ret = regulator_enable(ldo);
220 LOG("%s: faild to enable %s\n", __func__, ldostr);
222 LOG("wifi turn on power.\n");
224 LOG("wifi shut off power.\n");
225 LOG("%s: %s disabled\n", __func__, ldostr);
226 ret = regulator_disable(ldo);
228 LOG("%s: faild to disable %s\n", __func__, ldostr);
235 poweron = &mrfkill->pdata->power_n;
236 reset = &mrfkill->pdata->reset_n;
239 if (gpio_is_valid(poweron->io)) {
240 gpio_set_value(poweron->io, poweron->enable);
244 if (gpio_is_valid(reset->io)) {
245 gpio_set_value(reset->io, reset->enable);
248 LOG("wifi turn on power. %d\n", poweron->io);
250 if (gpio_is_valid(poweron->io)) {
251 gpio_set_value(poweron->io, !(poweron->enable));
255 if (gpio_is_valid(reset->io)) {
256 gpio_set_value(reset->io, !(reset->enable));
259 LOG("wifi shut off power.\n");
265 EXPORT_SYMBOL(rockchip_wifi_power);
267 /**************************************************************************
269 * Wifi Sdio Detect Func
271 *************************************************************************/
272 #include <linux/mmc/host.h>
273 extern int mmc_host_rescan(struct mmc_host *host, int val);
274 int rockchip_wifi_set_carddetect(int val)
276 return mmc_host_rescan(NULL, val);//NULL => SDIO host
278 EXPORT_SYMBOL(rockchip_wifi_set_carddetect);
280 /**************************************************************************
282 * Wifi Get Interrupt irq Func
284 *************************************************************************/
285 int rockchip_wifi_get_oob_irq(void)
287 struct rfkill_wlan_data *mrfkill = g_rfkill;
288 struct rksdmmc_gpio *wifi_int_irq;
290 LOG("%s: Enter\n", __func__);
292 if (mrfkill == NULL) {
293 LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
297 wifi_int_irq = &mrfkill->pdata->wifi_int_b;
298 if (gpio_is_valid(wifi_int_irq->io)) {
299 return gpio_to_irq(wifi_int_irq->io);
300 //return wifi_int_irq->io;
302 LOG("%s: wifi OOB pin isn't defined.\n", __func__);
307 EXPORT_SYMBOL(rockchip_wifi_get_oob_irq);
309 /**************************************************************************
313 *************************************************************************/
314 int rockchip_wifi_reset(int on)
318 EXPORT_SYMBOL(rockchip_wifi_reset);
320 /**************************************************************************
322 * Wifi MAC custom Func
324 *************************************************************************/
325 #include <linux/etherdevice.h>
326 u8 wifi_custom_mac_addr[6] = {0,0,0,0,0,0};
327 extern char GetSNSectorInfo(char * pbuf);
328 int rockchip_wifi_mac_addr(unsigned char *buf)
331 char mac_buf[20] = {0};
332 LOG("%s: enter.\n", __func__);
335 if(is_zero_ether_addr(wifi_custom_mac_addr)) {
337 char *tempBuf = kmalloc(512, GFP_KERNEL);
339 GetSNSectorInfo(tempBuf);
340 for (i = 506; i <= 511; i++)
341 wifi_custom_mac_addr[i-506] = tempBuf[i];
348 sprintf(mac_buf,"%02x:%02x:%02x:%02x:%02x:%02x",wifi_custom_mac_addr[0],wifi_custom_mac_addr[1],
349 wifi_custom_mac_addr[2],wifi_custom_mac_addr[3],wifi_custom_mac_addr[4],wifi_custom_mac_addr[5]);
350 LOG("falsh wifi_custom_mac_addr=[%s]\n", mac_buf);
352 if (is_valid_ether_addr(wifi_custom_mac_addr)) {
353 if (2 == (wifi_custom_mac_addr[0] & 0x0F)) {
354 LOG("This mac address come into conflict with the address of direct, ignored...\n");
358 LOG("This mac address is not valid, ignored...\n");
362 #if defined(CONFIG_RKWIFI)
363 memcpy(buf, wifi_custom_mac_addr, 6);
365 memcpy(buf, mac_buf, strlen(mac_buf));//realtek's wifi use this branch
369 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
371 /**************************************************************************
373 * wifi get country code func
375 *************************************************************************/
376 struct cntry_locales_custom {
377 char iso_abbrev[4]; /* ISO 3166-1 country abbreviation */
378 char custom_locale[4]; /* Custom firmware locale */
379 int custom_locale_rev; /* Custom local revisin default -1 */
382 static struct cntry_locales_custom country_cloc;
384 void *rockchip_wifi_country_code(char *ccode)
386 struct cntry_locales_custom *mcloc;
388 LOG("%s: set country code [%s]\n", __func__, ccode);
389 mcloc = &country_cloc;
390 memcpy(mcloc->custom_locale, ccode, 4);
391 mcloc->custom_locale_rev = 0;
395 EXPORT_SYMBOL(rockchip_wifi_country_code);
396 /**************************************************************************/
398 static int rockchip_wifi_voltage_select(void)
400 struct rfkill_wlan_data *mrfkill = g_rfkill;
403 if (mrfkill == NULL) {
404 LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
407 voltage = mrfkill->pdata->sdio_vol;
408 if (voltage > 2700 && voltage < 3500) {
409 writel_relaxed(0x00100000, RK_GRF_VIRT+0x380); //3.3
410 LOG("%s: wifi & sdio reference voltage: 3.3V\n", __func__);
411 } else if (voltage > 1500 && voltage < 1950) {
412 writel_relaxed(0x00100010, RK_GRF_VIRT+0x380); //1.8
413 LOG("%s: wifi & sdio reference voltage: 1.8V\n", __func__);
415 LOG("%s: unsupport wifi & sdio reference voltage!\n", __func__);
422 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char* prefix, const char* name)
424 if (gpio_is_valid(gpio->io)) {
426 sprintf(gpio->name, "%s_%s", prefix, name);
427 ret = gpio_request(gpio->io, gpio->name);
429 LOG("Failed to get %s gpio.\n", gpio->name);
438 static int wlan_platdata_parse_dt(struct device *dev,
439 struct rksdmmc_gpio_wifi_moudle *data)
441 struct device_node *node = dev->of_node;
445 enum of_gpio_flags flags;
450 memset(data, 0, sizeof(*data));
452 ret = of_property_read_u32(node, "sdio_vref", &value);
454 LOG("%s: Can't get sdio vref.", __func__);
457 data->sdio_vol = value;
459 if (of_find_property(node, "power_ctrl_by_pmu", NULL)) {
460 data->mregulator.power_ctrl_by_pmu = true;
461 ret = of_property_read_string(node, "pmu_regulator", &strings);
463 LOG("%s: Can not read property: pmu_regulator.\n", __func__);
464 data->mregulator.power_ctrl_by_pmu = false;
466 LOG("%s: wifi power controled by pmu(%s).\n", __func__, strings);
467 sprintf(data->mregulator.pmu_regulator, "%s", strings);
469 ret = of_property_read_u32(node, "pmu_enable_level", &value);
471 LOG("%s: Can not read property: pmu_enable_level.\n", __func__);
472 data->mregulator.power_ctrl_by_pmu = false;
474 LOG("%s: wifi power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
475 data->mregulator.enable = value;
478 data->mregulator.power_ctrl_by_pmu = false;
479 LOG("%s: wifi power controled by gpio.\n", __func__);
480 gpio = of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0, &flags);
481 if (gpio_is_valid(gpio)){
482 data->power_n.io = gpio;
483 data->power_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
484 LOG("%s: get property: WIFI,poweren_gpio = %d, flags = %d.\n", __func__, gpio, flags);
485 } else data->power_n.io = -1;
486 gpio = of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0, &flags);
487 if (gpio_is_valid(gpio)){
488 data->reset_n.io = gpio;
489 data->reset_n.enable = (flags == GPIO_ACTIVE_HIGH)? 1:0;
490 LOG("%s: get property: WIFI,reset_gpio = %d, flags = %d.\n", __func__, gpio, flags);
491 } else data->reset_n.io = -1;
492 gpio = of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0, &flags);
493 if (gpio_is_valid(gpio)){
494 data->wifi_int_b.io = gpio;
495 data->wifi_int_b.enable = flags;
496 LOG("%s: get property: WIFI,host_wake_irq = %d, flags = %d.\n", __func__, gpio, flags);
497 } else data->wifi_int_b.io = -1;
504 #if defined(CONFIG_HAS_EARLYSUSPEND)
505 #include <linux/earlysuspend.h>
507 static void wlan_early_suspend(struct early_suspend *h)
509 LOG("%s :enter\n", __func__);
514 static void wlan_late_resume(struct early_suspend *h)
516 LOG("%s :enter\n", __func__);
521 struct early_suspend wlan_early_suspend {
522 .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
523 .suspend = wlan_early_suspend;
524 .resume = wlan_late_resume;
528 static int rfkill_wlan_probe(struct platform_device *pdev)
530 struct rfkill_wlan_data *rfkill;
531 struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
534 LOG("Enter %s\n", __func__);
538 pdata = kzalloc(sizeof(struct rksdmmc_gpio_wifi_moudle), GFP_KERNEL);
542 ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
545 LOG("%s: No platform data specified\n", __func__);
552 rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
554 goto rfkill_alloc_fail;
556 rfkill->pdata = pdata;
559 LOG("%s: init gpio\n", __func__);
561 if (!pdata->mregulator.power_ctrl_by_pmu) {
562 ret = rfkill_rk_setup_gpio(&pdata->power_n, wlan_name, "wlan_poweren");
563 if (ret) goto fail_alloc;
565 ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name, "wlan_reset");
566 if (ret) goto fail_alloc;
569 wake_lock_init(&(rfkill->wlan_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_wlan_wake");
571 // Turn off wifi power as default
572 if (gpio_is_valid(pdata->power_n.io))
574 gpio_direction_output(pdata->power_n.io, !pdata->power_n.enable);
577 rockchip_wifi_voltage_select();
579 #if BCM_STATIC_MEMORY_SUPPORT
580 rockchip_init_wifi_mem();
583 #if defined(CONFIG_HAS_EARLYSUSPEND)
584 register_early_suspend(wlan_early_suspend);
587 LOG("Exit %s\n", __func__);
601 static int rfkill_wlan_remove(struct platform_device *pdev)
603 struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
605 LOG("Enter %s\n", __func__);
607 wake_lock_destroy(&rfkill->wlan_irq_wl);
609 if (gpio_is_valid(rfkill->pdata->power_n.io))
610 gpio_free(rfkill->pdata->power_n.io);
612 if (gpio_is_valid(rfkill->pdata->reset_n.io))
613 gpio_free(rfkill->pdata->reset_n.io);
615 // if (gpio_is_valid(rfkill->pdata->vddio.io))
616 // gpio_free(rfkill->pdata->vddio.io);
618 // if (gpio_is_valid(rfkill->pdata->bgf_int_b.io))
619 // gpio_free(rfkill->pdata->bgf_int_b.io);
621 // if (gpio_is_valid(rfkill->pdata->gps_sync.io))
622 // gpio_free(rfkill->pdata->gps_sync.io);
624 // if (gpio_is_valid(rfkill->pdata->ANTSEL2.io))
625 // gpio_free(rfkill->pdata->ANTSEL2.io);
627 // if (gpio_is_valid(rfkill->pdata->ANTSEL3.io))
628 // gpio_free(rfkill->pdata->ANTSEL3.io);
630 // if (gpio_is_valid(rfkill->pdata->GPS_LAN.io))
631 // gpio_free(rfkill->pdata->GPS_LAN.io);
639 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
641 LOG("Enter %s\n", __func__);
645 static int rfkill_wlan_resume(struct platform_device *pdev)
647 LOG("Enter %s\n", __func__);
652 static struct of_device_id wlan_platdata_of_match[] = {
653 { .compatible = "wlan-platdata" },
656 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
659 static struct platform_driver rfkill_wlan_driver = {
660 .probe = rfkill_wlan_probe,
661 .remove = rfkill_wlan_remove,
662 .suspend = rfkill_wlan_suspend,
663 .resume = rfkill_wlan_resume,
665 .name = "wlan-platdata",
666 .owner = THIS_MODULE,
667 .of_match_table = of_match_ptr(wlan_platdata_of_match),
671 static int __init rfkill_wlan_init(void)
673 LOG("Enter %s\n", __func__);
674 return platform_driver_register(&rfkill_wlan_driver);
677 static void __exit rfkill_wlan_exit(void)
679 LOG("Enter %s\n", __func__);
680 platform_driver_unregister(&rfkill_wlan_driver);
683 module_init(rfkill_wlan_init);
684 module_exit(rfkill_wlan_exit);
686 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
687 MODULE_AUTHOR("gwl@rock-chips.com");
688 MODULE_LICENSE("GPL");