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-rk.h>
27 #include <linux/rfkill-wlan.h>
28 #include <linux/wakelock.h>
29 #include <linux/interrupt.h>
31 #include <linux/suspend.h>
32 #include <linux/proc_fs.h>
33 #include <linux/uaccess.h>
34 #include <linux/gpio.h>
37 #include <linux/of_device.h>
38 #include <linux/of_gpio.h>
42 #define DBG(x...) printk(KERN_INFO "[WLAN_RFKILL]: "x)
47 #define LOG(x...) printk(KERN_INFO "[WLAN_RFKILL]: "x)
49 struct rfkill_wlan_data {
50 struct rksdmmc_gpio_wifi_moudle *pdata;
51 struct wake_lock wlan_irq_wl;
54 static struct rfkill_wlan_data *g_rfkill = NULL;
56 static const char wlan_name[] =
57 #if defined (CONFIG_BCM4330)
58 #if defined (CONFIG_BT_MODULE_NH660)
63 #elif defined (CONFIG_RK903)
64 #if defined(CONFIG_RKWIFI_26M)
69 #elif defined(CONFIG_BCM4329)
71 #elif defined(CONFIG_MV8787)
73 #elif defined(CONFIG_AP6210)
74 #if defined(CONFIG_RKWIFI_26M)
79 #elif defined(CONFIG_AP6330)
81 #elif defined(CONFIG_AP6476)
83 #elif defined(CONFIG_AP6493)
90 /***********************************************************
92 * Broadcom Wifi Static Memory
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
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)
109 static struct sk_buff *wlan_static_skb[WLAN_SKB_BUF_NUM];
111 struct wifi_mem_prealloc {
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)}
124 static int __init rockchip_init_wifi_mem(void)
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));
133 if (!wlan_static_skb[i])
137 for (i = 0 ; i < 5; i++) {
138 wifi_mem_array[i].mem_ptr =
139 kmalloc(wifi_mem_array[i].size, GFP_KERNEL);
141 if (!wifi_mem_array[i].mem_ptr)
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;
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]);
159 void *rockchip_mem_prealloc(int section, unsigned long size)
161 if (section == PREALLOC_WLAN_SEC_NUM)
162 return wlan_static_skb;
164 if ((section < 0) || (section > 5))
168 return wifi_mem_array[4].mem_ptr;
170 if (wifi_mem_array[section].size < size)
173 return wifi_mem_array[section].mem_ptr;
176 void *rockchip_mem_prealloc(int section, unsigned long size) { return NULL;}
178 EXPORT_SYMBOL(rockchip_mem_prealloc);
180 /**************************************************************************
182 * Wifi Power Control Func
186 *************************************************************************/
187 int rockchip_wifi_power(int on)
189 struct rfkill_wlan_data *mrfkill = g_rfkill;
190 struct rksdmmc_gpio *poweron, *reset;
191 struct regulator *ldo = NULL;
193 LOG("%s: %d\n", __func__, on);
195 if (mrfkill == NULL) {
196 LOG("%s: rfkill-wlan driver has not Successful initialized\n", __func__);
200 if (mrfkill->pdata->mregulator.power_ctrl_by_pmu) {
203 int level = mrfkill->pdata->mregulator.enable;
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__);
210 ldo = regulator_get(NULL, ldostr);
212 LOG("\n\n\n%s get ldo error,please mod this\n\n\n", __func__);
215 regulator_set_voltage(ldo, 3000000, 3000000);
216 LOG("%s: %s enabled\n", __func__, ldostr);
217 ret = regulator_enable(ldo);
219 LOG("%s: faild to enable %s\n", __func__, ldostr);
221 LOG("wifi turn on power.\n");
223 LOG("wifi shut off power.\n");
224 LOG("%s: %s disabled\n", __func__, ldostr);
225 ret = regulator_disable(ldo);
227 LOG("%s: faild to disable %s\n", __func__, ldostr);
234 poweron = &mrfkill->pdata->power_n;
235 reset = &mrfkill->pdata->reset_n;
238 if (gpio_is_valid(poweron->io)) {
239 gpio_set_value(poweron->io, !(poweron->enable));
243 if (gpio_is_valid(reset->io)) {
244 gpio_set_value(reset->io, !(reset->enable));
247 LOG("wifi turn on power. %d\n", poweron->io);
249 if (gpio_is_valid(poweron->io)) {
250 gpio_set_value(poweron->io, poweron->enable);
254 if (gpio_is_valid(reset->io)) {
255 gpio_set_value(reset->io, reset->enable);
258 LOG("wifi shut off power.\n");
264 EXPORT_SYMBOL(rockchip_wifi_power);
266 /**************************************************************************
270 *************************************************************************/
271 int rockchip_wifi_reset(int on)
275 EXPORT_SYMBOL(rockchip_wifi_reset);
277 /**************************************************************************
279 * Wifi MAC custom Func
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)
287 char mac_buf[20] = {0};
288 LOG("%s: enter.\n", __func__);
291 if(is_zero_ether_addr(wifi_custom_mac_addr)) {
293 char *tempBuf = kmalloc(512, GFP_KERNEL);
295 GetSNSectorInfo(tempBuf);
296 for (i = 506; i <= 511; i++)
297 wifi_custom_mac_addr[i-506] = tempBuf[i];
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);
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");
314 LOG("This mac address is not valid, ignored...\n");
318 #if defined(CONFIG_RKWIFI)
319 memcpy(buf, wifi_custom_mac_addr, 6);
321 memcpy(buf, mac_buf, strlen(mac_buf));//realtek's wifi use this branch
325 EXPORT_SYMBOL(rockchip_wifi_mac_addr);
327 /**************************************************************************
329 * wifi get country code func
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 */
338 static struct cntry_locales_custom country_cloc;
340 void *rockchip_wifi_country_code(char *ccode)
342 struct cntry_locales_custom *mcloc;
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;
351 EXPORT_SYMBOL(rockchip_wifi_country_code);
352 /**************************************************************************/
354 static int rfkill_rk_setup_gpio(struct rksdmmc_gpio *gpio, const char* prefix, const char* name)
356 if (gpio_is_valid(gpio->io)) {
358 sprintf(gpio->name, "%s_%s", prefix, name);
359 ret = gpio_request(gpio->io, gpio->name);
361 LOG("Failed to get %s gpio.\n", gpio->name);
370 static int wlan_platdata_parse_dt(struct device *dev,
371 struct rksdmmc_gpio_wifi_moudle *data)
373 struct device_node *node = dev->of_node;
374 struct device_node *childnode, *grandchildnode;
375 const char *pin_funcs, *strings;
378 enum of_gpio_flags flags;
383 memset(data, 0, sizeof(*data));
385 ret = of_property_read_u32(node, "sdio_vref", &value);
387 LOG("%s: Can't get sdio vref.", __func__);
389 data->sdio_vol = value;
391 childnode = of_get_child_by_name(node, "wlan_ctrl_pmic");
393 LOG("%s: Can not get child => wlan_ctrl_pmic.\n", __func__);
395 ret = of_property_read_string(childnode, "power_ctrl_by_pmu", &strings);
397 LOG("%s: Can not read property: power_ctrl_by_pmu.\n", __func__);
398 data->mregulator.power_ctrl_by_pmu = false;
400 if (0 == strcmp(strings, "true")) {
401 data->mregulator.power_ctrl_by_pmu = true;
402 ret = of_property_read_string(childnode, "pmu_regulator", &strings);
404 LOG("%s: Can not read property: pmu_regulator.\n", __func__);
405 data->mregulator.power_ctrl_by_pmu = false;
407 LOG("%s: wifi power controled by pmu(%s).\n", __func__, strings);
408 sprintf(data->mregulator.pmu_regulator, "%s", strings);
410 ret = of_property_read_u32(childnode, "pmu_enable_level", &value);
412 LOG("%s: Can not read property: pmu_enable_level.\n", __func__);
413 data->mregulator.power_ctrl_by_pmu = false;
415 LOG("%s: wifi power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
416 data->mregulator.enable = value;
419 data->mregulator.power_ctrl_by_pmu = false;
420 LOG("%s: wifi power controled by gpio.\n", __func__);
424 childnode = of_get_child_by_name(node, "wlan_ctrl_gpios");
426 LOG("%s: Can not get child => wlan_ctrl_gpios.\n", __func__);
430 for_each_child_of_node(childnode, grandchildnode) {
431 ret = of_property_read_string(grandchildnode, "pin-func", &pin_funcs);
433 LOG("%s: Can not read property: pin-func.\n", __func__);
436 gpio = of_get_named_gpio_flags(grandchildnode, "gpios", 0, &flags);
437 if (!gpio_is_valid(gpio)){
438 LOG("%s: Can not read property: %s->gpios.\n", __func__, pin_funcs);
442 if (0 == strcmp(pin_funcs, "wlan_poweren")) {
443 data->power_n.io = gpio;
444 data->power_n.enable = flags;
445 LOG("pin_funcs = %s, gpio = %d, flags = %d", pin_funcs, gpio, flags);
446 } else if (0 == strcmp(pin_funcs, "wlan_reset")) {
447 data->reset_n.io = gpio;
448 data->reset_n.enable = flags;
449 LOG("pin_funcs = %s, gpio = %d, flags = %d", pin_funcs, gpio, flags);
450 } else if (0 == strcmp(pin_funcs, "wlan_wake_host_irq")) {
451 data->wifi_int_b.io = gpio;
452 data->wifi_int_b.enable = flags;
453 LOG("pin_funcs = %s, gpio = %d, flags = %d", pin_funcs, gpio, flags);
462 #if defined(CONFIG_HAS_EARLYSUSPEND)
463 #include <linux/earlysuspend.h>
465 static void wlan_early_suspend(struct early_suspend *h)
467 LOG("%s :enter\n", __func__);
472 static void wlan_late_resume(struct early_suspend *h)
474 LOG("%s :enter\n", __func__);
479 struct early_suspend wlan_early_suspend {
480 .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
481 .suspend = wlan_early_suspend;
482 .resume = wlan_late_resume;
486 static int rfkill_wlan_probe(struct platform_device *pdev)
488 struct rfkill_wlan_data *rfkill;
489 struct rksdmmc_gpio_wifi_moudle *pdata = pdev->dev.platform_data;
492 LOG("Enter %s\n", __func__);
496 pdata = kzalloc(sizeof(struct rksdmmc_gpio_wifi_moudle), GFP_KERNEL);
500 ret = wlan_platdata_parse_dt(&pdev->dev, pdata);
503 LOG("%s: No platform data specified\n", __func__);
510 rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
512 goto rfkill_alloc_fail;
514 rfkill->pdata = pdata;
517 LOG("%s: init gpio\n", __func__);
519 ret = rfkill_rk_setup_gpio(&pdata->power_n, wlan_name, "wlan_poweren");
520 if (ret) goto fail_alloc;
522 ret = rfkill_rk_setup_gpio(&pdata->reset_n, wlan_name, "wlan_reset");
523 if (ret) goto fail_alloc;
525 ret = rfkill_rk_setup_gpio(&pdata->vddio, IOMUX_FGPIO, wlan_name, "wlan-vddio");
526 if (ret) goto fail_alloc;
528 ret = rfkill_rk_setup_gpio(&pdata->bgf_int_b, IOMUX_FGPIO, wlan_name, "bgf_int_b");
529 if (ret) goto fail_alloc;
531 ret = rfkill_rk_setup_gpio(&pdata->gps_sync, IOMUX_FGPIO, wlan_name, "gps_sync");
532 if (ret) goto fail_alloc;
534 ret = rfkill_rk_setup_gpio(&pdata->ANTSEL2, IOMUX_FGPIO, wlan_name, "ANTSEL2");
535 if (ret) goto fail_alloc;
537 ret = rfkill_rk_setup_gpio(&pdata->ANTSEL3, IOMUX_FGPIO, wlan_name, "ANTSEL3");
538 if (ret) goto fail_alloc;
540 ret = rfkill_rk_setup_gpio(&pdata->GPS_LAN, IOMUX_FGPIO, wlan_name, "GPS_LAN");
541 if (ret) goto fail_alloc;
543 wake_lock_init(&(rfkill->wlan_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_wlan_wake");
545 // Turn off wifi power as default
546 if (gpio_is_valid(pdata->power_n.io))
548 gpio_direction_output(pdata->power_n.io, pdata->power_n.enable);
551 #if BCM_STATIC_MEMORY_SUPPORT
552 rockchip_init_wifi_mem();
555 #if defined(CONFIG_HAS_EARLYSUSPEND)
556 register_early_suspend(wlan_early_suspend);
559 LOG("Exit %s\n", __func__);
573 static int rfkill_wlan_remove(struct platform_device *pdev)
575 struct rfkill_wlan_data *rfkill = platform_get_drvdata(pdev);
577 LOG("Enter %s\n", __func__);
579 wake_lock_destroy(&rfkill->wlan_irq_wl);
581 if (gpio_is_valid(rfkill->pdata->power_n.io))
582 gpio_free(rfkill->pdata->power_n.io);
584 if (gpio_is_valid(rfkill->pdata->reset_n.io))
585 gpio_free(rfkill->pdata->reset_n.io);
587 // if (gpio_is_valid(rfkill->pdata->vddio.io))
588 // gpio_free(rfkill->pdata->vddio.io);
590 // if (gpio_is_valid(rfkill->pdata->bgf_int_b.io))
591 // gpio_free(rfkill->pdata->bgf_int_b.io);
593 // if (gpio_is_valid(rfkill->pdata->gps_sync.io))
594 // gpio_free(rfkill->pdata->gps_sync.io);
596 // if (gpio_is_valid(rfkill->pdata->ANTSEL2.io))
597 // gpio_free(rfkill->pdata->ANTSEL2.io);
599 // if (gpio_is_valid(rfkill->pdata->ANTSEL3.io))
600 // gpio_free(rfkill->pdata->ANTSEL3.io);
602 // if (gpio_is_valid(rfkill->pdata->GPS_LAN.io))
603 // gpio_free(rfkill->pdata->GPS_LAN.io);
611 static int rfkill_wlan_suspend(struct platform_device *pdev, pm_message_t state)
613 LOG("Enter %s\n", __func__);
617 static int rfkill_wlan_resume(struct platform_device *pdev)
619 LOG("Enter %s\n", __func__);
624 static struct of_device_id wlan_platdata_of_match[] = {
625 { .compatible = "wlan-platdata" },
628 MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
631 static struct platform_driver rfkill_wlan_driver = {
632 .probe = rfkill_wlan_probe,
633 .remove = rfkill_wlan_remove,
634 .suspend = rfkill_wlan_suspend,
635 .resume = rfkill_wlan_resume,
637 .name = "wlan-platdata",
638 .owner = THIS_MODULE,
639 .of_match_table = of_match_ptr(wlan_platdata_of_match),
643 static int __init rfkill_wlan_init(void)
645 LOG("Enter %s\n", __func__);
646 return platform_driver_register(&rfkill_wlan_driver);
649 static void __exit rfkill_wlan_exit(void)
651 LOG("Enter %s\n", __func__);
652 platform_driver_unregister(&rfkill_wlan_driver);
655 module_init(rfkill_wlan_init);
656 module_exit(rfkill_wlan_exit);
658 MODULE_DESCRIPTION("rock-chips rfkill for wifi v0.1");
659 MODULE_AUTHOR("gwl@rock-chips.com");
660 MODULE_LICENSE("GPL");