Merge branch linux-linaro-lsk-v3.10-android
[firefly-linux-kernel-4.4.55.git] / net / rfkill / rfkill-rk.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 bluetooth
15  *
16  * BT Ô´ÂëÓÉÔ­À´µÄarch/arm/mach-rkxx/board-xxx-rfkill.cÒÆÖÁ´Ë´¦
17  * GPIOµÄÅäÖÃÔÚ arch/arm/mach-rkxx/board-rkxx-sdk.c µÄrfkill_rk_platdata½á¹¹ÌåÖÐ
18  *
19  * Çë¸ù¾Ýʵ¼ÊÇé¿öÅäÖúÃÀ¶ÑÀµÄ¸÷¸öGPIOÒý½Å£¬Òý½ÅÅäÖÃ˵Ã÷ÈçÏÂ:
20     .xxxx_gpio   = {
21       .io         = -1,      // GPIOÖµ£¬ -1 ±íʾÎ޴˹¦ÄÜ
22       .enable     = -1,      // Ê¹ÄÜ, GPIO_HIGH - ¸ßʹÄÜ£¬ GPIO_LOW - µÍʹÄÜ
23       .iomux      = {
24           .name    = NULL,   // IOMUXÃû³Æ£¬NULL ±íʾËüÊǵ¥¹¦ÄÜ£¬²»ÐèÒªmux
25           .fgpio   = -1,     // ÅäÖÃΪGPIOʱ£¬Ó¦ÉèÖõÄÖµ
26           .fmux   = -1,      // ÅäÖÃΪ¸´Óù¦ÄÜʱ£¬Ó¦ÉèÖõÄÖµ
27       },
28     },
29 */
30
31 #include <linux/kernel.h>
32 #include <linux/platform_device.h>
33 #include <linux/module.h>
34 #include <linux/rfkill.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <asm/gpio.h>
38 #include <mach/iomux.h>
39 #include <linux/delay.h>
40 #include <linux/rfkill-rk.h>
41 #include <linux/wakelock.h>
42 #include <linux/interrupt.h>
43 #include <asm/irq.h>
44 #include <linux/suspend.h>
45 #include <linux/proc_fs.h>
46 #include <linux/uaccess.h>
47
48 #if 0
49 #define DBG(x...)   printk(KERN_INFO "[BT_RFKILL]: "x)
50 #else
51 #define DBG(x...)
52 #endif
53
54 #define LOG(x...)   printk(KERN_INFO "[BT_RFKILL]: "x)
55
56 #define BT_WAKEUP_TIMEOUT           10000
57 #define BT_IRQ_WAKELOCK_TIMEOUT     10*1000
58
59 #define BT_BLOCKED     true
60 #define BT_UNBLOCK     false
61 #define BT_SLEEP       true
62 #define BT_WAKEUP      false
63
64 enum {
65     IOMUX_FNORMAL=0,
66     IOMUX_FGPIO,
67     IOMUX_FMUX,
68 };
69
70 #ifdef CONFIG_ARCH_RK29
71     #define rk_mux_api_set(name,mode)      rk29_mux_api_set(name,mode)
72 #elif defined (CONFIG_ARCH_RK30)
73     #define rk_mux_api_set(name,mode)      rk30_mux_api_set(name,mode)
74 #else
75     #define rk_mux_api_set(name,mode)      rk30_mux_api_set(name,mode)
76 #endif
77
78 // RK29+BCM4329, ÆäwifiÓëbtµÄpower¿ØÖƽÅÊǽÓÔÚÒ»ÆðµÄ
79 // ÔÚ¸øbtϵçʱ£¬ÐèÒªÏÈÅжÏwifi״̬
80 #if defined(CONFIG_BCM4329)
81 #define WIFI_BT_POWER_TOGGLE    1
82 extern int rk29sdk_bt_power_state;
83 extern int rk29sdk_wifi_power_state;
84 #else
85 #define WIFI_BT_POWER_TOGGLE    0
86 #endif
87
88 struct rfkill_rk_data {
89     // Ö¸ÏòboardÖж¨ÒåµÄplatform data
90         struct rfkill_rk_platform_data  *pdata;
91
92     // Ö¸ÏòrfkillÉ豸£¬ËüÓÉrfkill_alloc´´½¨
93         struct rfkill                           *rfkill_dev;
94
95     // ÔÚIRQÖжϺ¯ÊýÖÐʹÓã¬È·±£ÔÚBT»½ÐÑAPºó£¬²»»áÁ¢¼´
96     // µ÷ÓÃsuspend½øÈë˯Ãß
97     struct wake_lock            bt_irq_wl;
98     
99     // ÔÚ»½ÐÑBTºó£¬Í¨¹ý¸Ãdelay workÑÓʱһ¶Îʱ¼äºó½øÈë˯Ãß
100     // Èç¹ûdelay work»¹Î´Ö´ÐоÍÓÖÀ´Ò»´Î BT wake£¬Ôò»áÏÈcancel
101     // ÒÑÓеĠdelay work£¬²¢ÖØмÆʱ¡£
102     struct delayed_work         bt_sleep_delay_work;
103 };
104
105 static struct rfkill_rk_data *g_rfkill = NULL;
106
107 static const char bt_name[] = 
108 #if defined (CONFIG_BCM4330)
109     #if defined (CONFIG_BT_MODULE_NH660)
110         "nh660"
111     #else
112         "bcm4330"
113     #endif
114 #elif defined (CONFIG_RK903)
115     #if defined(CONFIG_RKWIFI_26M)
116         "rk903_26M"
117     #else
118         "rk903"
119     #endif
120 #elif defined(CONFIG_BCM4329)
121         "bcm4329"
122 #elif defined(CONFIG_MV8787)
123         "mv8787"
124 #elif defined(CONFIG_AP6210)
125     #if defined(CONFIG_RKWIFI_26M)
126         "ap6210"
127     #else
128         "ap6210_24M"
129     #endif
130 #elif defined(CONFIG_AP6330)
131                 "ap6330"
132 #elif defined(CONFIG_AP6476)
133                 "ap6476"
134 #elif defined(CONFIG_AP6493)
135                 "ap6493"
136 #elif defined(CONFIG_AP6441)
137         "ap6441"
138 #else
139         "bt_default"
140 #endif
141 ;
142
143 /*
144  *  rfkill_rk_wake_host_irq - BT_WAKE_HOST Öжϻص÷º¯Êý
145  *      ÉêÇëÒ»¸öwakelock£¬ÒÔÈ·±£´Ë´Î»½ÐѲ»»áÂíÉϽáÊø
146  */
147 static irqreturn_t rfkill_rk_wake_host_irq(int irq, void *dev)
148 {
149     struct rfkill_rk_data *rfkill = dev;
150     LOG("BT_WAKE_HOST IRQ fired\n");
151     
152     DBG("BT IRQ wakeup, request %dms wakelock\n", BT_IRQ_WAKELOCK_TIMEOUT);
153
154     wake_lock_timeout(&rfkill->bt_irq_wl, 
155                     msecs_to_jiffies(BT_IRQ_WAKELOCK_TIMEOUT));
156     
157         return IRQ_HANDLED;
158 }
159
160 /*
161  * rfkill_rk_setup_gpio - ÉèÖÃGPIO
162  *      gpio    - ÒªÉèÖõÄGPIO
163  *      mux     - iomux Ê²Ã´¹¦ÄÜ
164  *      prefix,name - ×é³É¸ÃIOµÄÃû³Æ
165  * ·µ»ØÖµ
166  *      ·µ»ØÖµÓë gpio_request Ïàͬ
167  */
168 static int rfkill_rk_setup_gpio(struct rfkill_rk_gpio* gpio, int mux, const char* prefix, const char* name)
169 {
170         if (gpio_is_valid(gpio->io)) {
171         int ret=0;
172         sprintf(gpio->name, "%s_%s", prefix, name);
173                 ret = gpio_request(gpio->io, gpio->name);
174                 if (ret) {
175                         LOG("Failed to get %s gpio.\n", gpio->name);
176                         return -1;
177                 }
178         if (gpio->iomux.name)
179         {
180             if (mux==IOMUX_FGPIO)
181                 rk_mux_api_set(gpio->iomux.name, gpio->iomux.fgpio);
182             else if (mux==IOMUX_FMUX)
183                 rk_mux_api_set(gpio->iomux.name, gpio->iomux.fmux);
184             else
185                 ;// do nothing
186         }
187         }
188
189     return 0;
190 }
191
192 // ÉèÖàBT_WAKE_HOST IRQ
193 static int rfkill_rk_setup_wake_irq(struct rfkill_rk_data* rfkill)
194 {
195     int ret=0;
196     struct rfkill_rk_irq* irq = &(rfkill->pdata->wake_host_irq);
197     
198 #ifndef CONFIG_BK3515A_COMBO
199     ret = rfkill_rk_setup_gpio(&irq->gpio, IOMUX_FGPIO, rfkill->pdata->name, "wake_host");
200     if (ret) goto fail1;
201 #endif
202
203     if (gpio_is_valid(irq->gpio.io))
204     {
205 #ifndef CONFIG_BK3515A_COMBO
206         ret = gpio_pull_updown(irq->gpio.io, (irq->gpio.enable==GPIO_LOW)?GPIOPullUp:GPIOPullDown);
207         if (ret) goto fail2;
208 #endif
209         LOG("Request irq for bt wakeup host\n");
210         irq->irq = gpio_to_irq(irq->gpio.io);
211         sprintf(irq->name, "%s_irq", irq->gpio.name);
212         ret = request_irq(irq->irq,
213                     rfkill_rk_wake_host_irq,
214                     (irq->gpio.enable==GPIO_LOW)?IRQF_TRIGGER_FALLING:IRQF_TRIGGER_RISING,
215                     irq->name,
216                     rfkill);
217         if (ret) goto fail2;
218         LOG("** disable irq\n");
219         disable_irq(irq->irq);
220         ret = enable_irq_wake(irq->irq);
221         if (ret) goto fail3;
222     }
223
224     return ret;
225
226 fail3:
227     free_irq(irq->gpio.io, rfkill);
228 fail2:
229     gpio_free(irq->gpio.io);
230 fail1:
231     return ret;
232 }
233
234 static inline void rfkill_rk_sleep_bt_internal(struct rfkill_rk_data *rfkill, bool sleep)
235 {
236     struct rfkill_rk_gpio *wake = &rfkill->pdata->wake_gpio;
237     
238     DBG("*** bt sleep: %d ***\n", sleep);
239 #ifndef CONFIG_BK3515A_COMBO
240     gpio_direction_output(wake->io, sleep?!wake->enable:wake->enable);
241 #else
242     if(!sleep)
243     {
244         DBG("HOST_UART0_TX pull down 10us\n");
245         if (rfkill_rk_setup_gpio(wake, IOMUX_FGPIO, rfkill->pdata->name, "wake") != 0) {
246             return;
247         }
248
249         gpio_direction_output(wake->io, wake->enable);
250         udelay(10);
251         gpio_direction_output(wake->io, !wake->enable);
252
253         rk_mux_api_set(wake->iomux.name, wake->iomux.fmux);
254         gpio_free(wake->io);
255     }
256 #endif
257 }
258
259 static void rfkill_rk_delay_sleep_bt(struct work_struct *work)
260 {
261     struct rfkill_rk_data *rfkill = NULL;
262     DBG("Enter %s\n",__FUNCTION__);
263
264     rfkill = container_of(work, struct rfkill_rk_data, bt_sleep_delay_work.work);
265
266     rfkill_rk_sleep_bt_internal(rfkill, BT_SLEEP);
267 }
268
269 /*
270  *  rfkill_rk_sleep_bt - Sleep or Wakeup BT
271  *      1 ÔÚ¸øBTÉϵçʱºò£¬µ÷Óøú¯Êý»½ÐÑBT
272  *      2 ÔÚsuspendÇý¶¯Ê±ºò£¬µ÷Óøú¯Êý˯ÃßBT
273  *      3 ÔÚHCIÇý¶¯ÖУ¬µ±ÓÐÃüÁî·¢Ë͸øBTʱ£¬µ÷Óøú¯Êý»½ÐÑBT
274  *
275  *  ¶ÔÀ¶ÑÀµÄÖ¸Áî·¢ËÍÊÇ·¢ÉúÔÚÀ¶ÑÀÉϵçÖ®ºó£¬Òò´Ë1Óë3²»»áͬʱµ÷Óøú¯Êý
276  *  À¶ÑÀÖ¸Áî·¢ËÍÓÉÓû§²ã·¢Æð£¬2Óë3²»»áͬʱµ÷Óøú¯Êý
277  */
278 void rfkill_rk_sleep_bt(bool sleep)
279 {
280     struct rfkill_rk_data *rfkill = g_rfkill;
281     struct rfkill_rk_gpio *wake;
282     bool ret;
283     DBG("Enter %s\n",__FUNCTION__);
284     
285     if (rfkill==NULL)
286     {
287         LOG("*** RFKILL is empty???\n");
288         return;
289     }
290
291     wake = &rfkill->pdata->wake_gpio;
292     if (!gpio_is_valid(wake->io))
293     {
294         DBG("*** Not support bt wakeup and sleep\n");
295         return;
296     }
297
298     // È¡Ïûdelay work£¬Èç¹ûwork´¦ÓÚpendding£¬ÔòÁ¢¼´È¡Ïû
299     // Èç¹ûwork´¦ÓÚrunning£¬ÔòµÈ´ýÖ±µ½¸Ãwork½áÊø
300     ret = cancel_delayed_work_sync(&rfkill->bt_sleep_delay_work);
301
302     rfkill_rk_sleep_bt_internal(rfkill, sleep);
303
304 #ifdef CONFIG_BT_AUTOSLEEP
305     if (sleep==BT_WAKEUP)
306     {
307         // ÖØÐÂÉèÖÃdelay work
308         schedule_delayed_work(&rfkill->bt_sleep_delay_work, 
309                             msecs_to_jiffies(BT_WAKEUP_TIMEOUT));
310     }
311 #endif
312 }
313 EXPORT_SYMBOL(rfkill_rk_sleep_bt);
314
315 static int rfkill_rk_set_power(void *data, bool blocked)
316 {
317         struct rfkill_rk_data *rfkill = data;
318     struct rfkill_rk_gpio *poweron = &rfkill->pdata->poweron_gpio;
319     struct rfkill_rk_gpio *reset = &rfkill->pdata->reset_gpio;
320     struct rfkill_rk_gpio* rts = &rfkill->pdata->rts_gpio;
321
322     DBG("Enter %s\n", __func__);
323
324     DBG("Set blocked:%d\n", blocked);
325
326         if (false == blocked) { 
327         rfkill_rk_sleep_bt(BT_WAKEUP); // ensure bt is wakeup
328
329                 if (gpio_is_valid(poweron->io))
330         {
331                         gpio_direction_output(poweron->io, poweron->enable);
332             msleep(20);
333         }
334                 if (gpio_is_valid(reset->io))
335         {
336                         gpio_direction_output(reset->io, reset->enable);
337             msleep(20);
338                         gpio_direction_output(reset->io, !reset->enable);
339             msleep(20);
340         }
341
342 #if defined(CONFIG_AP6210)
343         if (gpio_is_valid(rts->io))
344         {
345             if (rts->iomux.name)
346             {
347                 rk_mux_api_set(rts->iomux.name, rts->iomux.fgpio);
348             }
349             LOG("ENABLE UART_RTS\n");
350             gpio_direction_output(rts->io, rts->enable);
351
352             msleep(100);
353
354             LOG("DISABLE UART_RTS\n");
355             gpio_direction_output(rts->io, !rts->enable);
356             if (rts->iomux.name)
357             {
358                 rk_mux_api_set(rts->iomux.name, rts->iomux.fmux);
359             }
360         }
361 #endif
362
363         LOG("bt turn on power\n");
364         } else {
365 #if WIFI_BT_POWER_TOGGLE
366                 if (!rk29sdk_wifi_power_state) {
367 #endif
368             if (gpio_is_valid(poweron->io))
369             {      
370                 gpio_direction_output(poweron->io, !poweron->enable);
371                 msleep(20);
372             }
373
374                 LOG("bt shut off power\n");
375 #if WIFI_BT_POWER_TOGGLE
376                 }else {
377                         LOG("bt shouldn't shut off power, wifi is using it!\n");
378                 }
379 #endif
380                 if (gpio_is_valid(reset->io))
381         {      
382                         gpio_direction_output(reset->io, reset->enable);/* bt reset active*/
383             msleep(20);
384         }
385         }
386
387 #if WIFI_BT_POWER_TOGGLE
388         rk29sdk_bt_power_state = !blocked;
389 #endif
390         return 0;
391 }
392
393 static int rfkill_rk_pm_prepare(struct device *dev)
394 {
395     struct rfkill_rk_data *rfkill = g_rfkill;
396     struct rfkill_rk_gpio* rts;
397     struct rfkill_rk_irq*  wake_host_irq;
398     DBG("Enter %s\n",__FUNCTION__);
399
400     if (!rfkill)
401         return 0;
402
403     rts = &rfkill->pdata->rts_gpio;
404     wake_host_irq = &rfkill->pdata->wake_host_irq;
405
406     //To prevent uart to receive bt data when suspended
407     if (gpio_is_valid(rts->io))
408     {
409         DBG("Disable UART_RTS\n");
410         if (rts->iomux.name)
411         {
412             rk_mux_api_set(rts->iomux.name, rts->iomux.fgpio);
413         }
414         gpio_direction_output(rts->io, !rts->enable);
415     }
416
417 #ifdef CONFIG_BT_AUTOSLEEP
418     // BT½øÈë˯Ãß״̬£¬²»½ÓÊÕÖ÷¿ØÊý¾Ý
419     rfkill_rk_sleep_bt(BT_SLEEP);
420 #endif
421
422     /* ÖÁ´Ë£¬À¶ÑÀ²»ÔÙËÍÊý¾Ýµ½UART£¬Ò²²»ÔÙ½ÓÊÕÖ÷¿ØµÄUARTÊý¾Ý
423      * ½Ó×ŵ÷ÓÃenable_irqʹÄÜ bt_wake_host irq£¬µ±Ô¶¶ËÉ豸ÓÐÊý¾Ý
424      * µ½À´Ê±£¬½«Í¨¹ý¸ÃIRQ»½ÐÑÖ÷¿Ø
425      */
426
427     // enable bt wakeup host
428     if (gpio_is_valid(wake_host_irq->gpio.io))
429     {
430 #ifdef CONFIG_BK3515A_COMBO
431         int ret = 0;
432         ret = rfkill_rk_setup_gpio(&wake_host_irq->gpio, IOMUX_FGPIO, rfkill->pdata->name, "wake_host");
433         if (ret)
434             LOG("irq rfkill_rk_setup_gpio failed\n");
435
436         ret = gpio_pull_updown(wake_host_irq->gpio.io, (wake_host_irq->gpio.enable==GPIO_LOW)?GPIOPullUp:GPIOPullDown);
437         if (ret)
438             LOG("irq gpio_pull_updown failed\n");
439 #endif
440         DBG("enable irq for bt wakeup host\n");
441         enable_irq(wake_host_irq->irq);
442     }
443
444 #ifdef CONFIG_RFKILL_RESET
445     rfkill_set_states(rfkill->rfkill_dev, BT_BLOCKED, false);
446     rfkill_rk_set_power(rfkill, BT_BLOCKED);
447 #endif
448
449     return 0;
450 }
451
452 static void rfkill_rk_pm_complete(struct device *dev)
453 {
454     struct rfkill_rk_data *rfkill = g_rfkill;
455     struct rfkill_rk_irq*  wake_host_irq;
456     struct rfkill_rk_gpio* rts;
457     DBG("Enter %s\n",__FUNCTION__);
458
459     if (!rfkill)
460         return;
461
462     wake_host_irq = &rfkill->pdata->wake_host_irq;
463     rts = &rfkill->pdata->rts_gpio;
464
465     if (gpio_is_valid(wake_host_irq->gpio.io))
466     {
467         // ½ûÓõô BT_WAKE_HOST IRQ£¬È·±£ÔÚϵͳ»½ÐѺ󲻻áÒòBTµÄ²Ù×÷
468         // ¶ø¶à´Î´¥·¢¸ÃÖжÏ
469         LOG("** disable irq\n");
470         disable_irq(wake_host_irq->irq);
471 #ifdef CONFIG_BK3515A_COMBO
472         rk_mux_api_set(wake_host_irq->gpio.iomux.name, wake_host_irq->gpio.iomux.fmux);
473         gpio_free(wake_host_irq->gpio.io);
474 #endif
475     }
476
477     /* Ê¹ÓÃUART_RTS£¬´ËʱÀ¶ÑÀÈç¹ûÓÐÊý¾Ý¾Í»áË͵½UART
478      * Éϲã·ÖÎöËÍÀ´µÄÊý¾Ý²¢×ö³öÏàÓ¦µÄ¶¯×÷£¬±ÈÈç: ËÍÀ´µÄÊÇ
479      * Åä¶ÔÇëÇó£¬ÔòÉϲ㽫»áÁÁÆÁ²¢µ¯³öÅä¶Ô½çÃæ
480      */
481     if (gpio_is_valid(rts->io))
482     {
483         DBG("Enable UART_RTS\n");
484         gpio_direction_output(rts->io, rts->enable);
485         if (rts->iomux.name)
486         {
487             rk_mux_api_set(rts->iomux.name, rts->iomux.fmux);
488         }
489     }
490 }
491
492 static const struct rfkill_ops rfkill_rk_ops = {
493     .set_block = rfkill_rk_set_power,
494 };
495
496 #define PROC_DIR        "bluetooth/sleep"
497
498 static struct proc_dir_entry *bluetooth_dir, *sleep_dir;
499
500 static int bluesleep_read_proc_lpm(char *page, char **start, off_t offset,
501                                         int count, int *eof, void *data)
502 {
503     *eof = 1;
504     return sprintf(page, "unsupported to read\n");
505 }
506
507 static int bluesleep_write_proc_lpm(struct file *file, const char *buffer,
508                                         unsigned long count, void *data)
509 {
510     return count;
511 }
512
513 static int bluesleep_read_proc_btwrite(char *page, char **start, off_t offset,
514                                         int count, int *eof, void *data)
515 {
516     *eof = 1;
517     return sprintf(page, "unsupported to read\n");
518 }
519
520 static int bluesleep_write_proc_btwrite(struct file *file, const char *buffer,
521                                         unsigned long count, void *data)
522 {
523     char b;
524
525     if (count < 1)
526         return -EINVAL;
527
528     if (copy_from_user(&b, buffer, 1))
529         return -EFAULT;
530
531     DBG("btwrite %c\n", b);
532     /* HCI_DEV_WRITE */
533     if (b != '0') {
534         rfkill_rk_sleep_bt(BT_WAKEUP);
535     }
536
537     return count;
538 }
539
540 static int rfkill_rk_probe(struct platform_device *pdev)
541 {
542         struct rfkill_rk_data *rfkill;
543         struct rfkill_rk_platform_data *pdata = pdev->dev.platform_data;
544         int ret = 0;
545     struct proc_dir_entry *ent;
546
547     DBG("Enter %s\n", __func__);
548
549         if (!pdata) {
550                 LOG("%s: No platform data specified\n", __func__);
551                 return -EINVAL;
552         }
553
554     pdata->name = (char*)bt_name;
555
556         rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
557         if (!rfkill)
558                 return -ENOMEM;
559
560         rfkill->pdata = pdata;
561     g_rfkill = rfkill;
562
563     bluetooth_dir = proc_mkdir("bluetooth", NULL);
564     if (bluetooth_dir == NULL) {
565         LOG("Unable to create /proc/bluetooth directory");
566         return -ENOMEM;
567     }
568
569     sleep_dir = proc_mkdir("sleep", bluetooth_dir);
570     if (sleep_dir == NULL) {
571         LOG("Unable to create /proc/%s directory", PROC_DIR);
572         return -ENOMEM;
573     }
574
575         /* read/write proc entries */
576     ent = create_proc_entry("lpm", 0, sleep_dir);
577     if (ent == NULL) {
578         LOG("Unable to create /proc/%s/lpm entry", PROC_DIR);
579         ret = -ENOMEM;
580         goto fail_alloc;
581     }
582     ent->read_proc = bluesleep_read_proc_lpm;
583     ent->write_proc = bluesleep_write_proc_lpm;
584
585     /* read/write proc entries */
586     ent = create_proc_entry("btwrite", 0, sleep_dir);
587     if (ent == NULL) {
588         LOG("Unable to create /proc/%s/btwrite entry", PROC_DIR);
589         ret = -ENOMEM;
590         goto fail_alloc;
591     }
592     ent->read_proc = bluesleep_read_proc_btwrite;
593     ent->write_proc = bluesleep_write_proc_btwrite;
594
595     // ÉêÇëGPIOÒÔ¼°IRQ
596     DBG("init gpio\n");
597     // ¶ÔÓÚRK29 BCM4329£¬ËüµÄpoweron ioÓëwifi¹²Óã¬ÔÚboadÎļþÖÐÒѾ­request
598     // ´Ë´¦²»ÓÃÈ¥ÉêÇë
599 #if !WIFI_BT_POWER_TOGGLE
600     ret = rfkill_rk_setup_gpio(&pdata->poweron_gpio, IOMUX_FGPIO, pdata->name, "poweron");
601     if (ret) goto fail_alloc;
602 #endif
603
604     ret = rfkill_rk_setup_gpio(&pdata->reset_gpio, IOMUX_FGPIO, pdata->name, "reset");
605     if (ret) goto fail_poweron;
606
607 #ifndef CONFIG_BK3515A_COMBO
608     ret = rfkill_rk_setup_gpio(&pdata->wake_gpio, IOMUX_FGPIO, pdata->name, "wake");
609     if (ret) goto fail_reset;
610 #endif
611
612     ret = rfkill_rk_setup_wake_irq(rfkill);
613     if (ret) goto fail_wake;
614
615     ret = rfkill_rk_setup_gpio(&pdata->rts_gpio, IOMUX_FMUX, rfkill->pdata->name, "rts"); 
616     if (ret) goto fail_wake_host_irq;
617
618     // ´´½¨²¢×¢²áRFKILLÉ豸
619     DBG("setup rfkill\n");
620         rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
621                                 &rfkill_rk_ops, rfkill);
622         if (!rfkill->rfkill_dev)
623                 goto fail_rts;
624
625     // cmy: ÉèÖÃrfkill³õʼ״̬Ϊblocked£¬ÔÚ×¢²áʱ²»»áµ÷Óàset_blockedº¯Êý
626     rfkill_set_states(rfkill->rfkill_dev, BT_BLOCKED, false);
627         ret = rfkill_register(rfkill->rfkill_dev);
628         if (ret < 0)
629                 goto fail_rfkill;
630
631     wake_lock_init(&(rfkill->bt_irq_wl), WAKE_LOCK_SUSPEND, "rfkill_rk_irq_wl");
632     INIT_DELAYED_WORK(&rfkill->bt_sleep_delay_work, rfkill_rk_delay_sleep_bt);
633
634     // cmy: ÉèÖÃÀ¶ÑÀµçÔ´µÄ״̬Ϊ blocked
635     rfkill_rk_set_power(rfkill, BT_BLOCKED);
636
637         platform_set_drvdata(pdev, rfkill);
638
639     LOG("%s device registered.\n", pdata->name);
640
641         return 0;
642
643 fail_rfkill:
644         rfkill_destroy(rfkill->rfkill_dev);
645 fail_rts:
646     if (gpio_is_valid(pdata->rts_gpio.io))
647         gpio_free(pdata->rts_gpio.io);
648 fail_wake_host_irq:
649     if (gpio_is_valid(pdata->wake_host_irq.gpio.io)){
650         free_irq(pdata->wake_host_irq.irq, rfkill);
651 #ifndef CONFIG_BK3515A_COMBO
652         gpio_free(pdata->wake_host_irq.gpio.io);
653 #endif
654     }
655 fail_wake:
656 #ifndef CONFIG_BK3515A_COMBO
657     if (gpio_is_valid(pdata->wake_gpio.io))
658         gpio_free(pdata->wake_gpio.io);
659 #endif
660 fail_reset:
661         if (gpio_is_valid(pdata->reset_gpio.io))
662                 gpio_free(pdata->reset_gpio.io);
663 fail_poweron:
664 #if !WIFI_BT_POWER_TOGGLE
665     if (gpio_is_valid(pdata->poweron_gpio.io))
666         gpio_free(pdata->poweron_gpio.io);
667 #endif
668 fail_alloc:
669         kfree(rfkill);
670     g_rfkill = NULL;
671
672         remove_proc_entry("btwrite", sleep_dir);
673         remove_proc_entry("lpm", sleep_dir);
674
675         return ret;
676 }
677
678 static int rfkill_rk_remove(struct platform_device *pdev)
679 {
680         struct rfkill_rk_data *rfkill = platform_get_drvdata(pdev);
681
682     LOG("Enter %s\n", __func__);
683
684         rfkill_unregister(rfkill->rfkill_dev);
685         rfkill_destroy(rfkill->rfkill_dev);
686
687     wake_lock_destroy(&rfkill->bt_irq_wl);
688     
689     cancel_delayed_work_sync(&rfkill->bt_sleep_delay_work);
690
691     // free gpio
692     if (gpio_is_valid(rfkill->pdata->rts_gpio.io))
693         gpio_free(rfkill->pdata->rts_gpio.io);
694     
695     if (gpio_is_valid(rfkill->pdata->wake_host_irq.gpio.io)){
696         free_irq(rfkill->pdata->wake_host_irq.irq, rfkill);
697 #ifndef CONFIG_BK3515A_COMBO
698         gpio_free(rfkill->pdata->wake_host_irq.gpio.io);
699 #endif
700     }
701     
702 #ifndef CONFIG_BK3515A_COMBO
703     if (gpio_is_valid(rfkill->pdata->wake_gpio.io))
704         gpio_free(rfkill->pdata->wake_gpio.io);
705 #endif
706     
707     if (gpio_is_valid(rfkill->pdata->reset_gpio.io))
708         gpio_free(rfkill->pdata->reset_gpio.io);
709     
710     if (gpio_is_valid(rfkill->pdata->poweron_gpio.io))
711         gpio_free(rfkill->pdata->poweron_gpio.io);
712
713     kfree(rfkill);
714     g_rfkill = NULL;
715
716         return 0;
717 }
718
719 static const struct dev_pm_ops rfkill_rk_pm_ops = {
720         .prepare = rfkill_rk_pm_prepare,
721         .complete = rfkill_rk_pm_complete,
722 };
723
724 static struct platform_driver rfkill_rk_driver = {
725         .probe = rfkill_rk_probe,
726         .remove = __devexit_p(rfkill_rk_remove),
727         .driver = {
728                 .name = "rfkill_rk",
729                 .owner = THIS_MODULE,
730                 .pm = &rfkill_rk_pm_ops,
731         },
732 };
733
734 static int __init rfkill_rk_init(void)
735 {
736     LOG("Enter %s\n", __func__);
737         return platform_driver_register(&rfkill_rk_driver);
738 }
739
740 static void __exit rfkill_rk_exit(void)
741 {
742     LOG("Enter %s\n", __func__);
743         platform_driver_unregister(&rfkill_rk_driver);
744 }
745
746 module_init(rfkill_rk_init);
747 module_exit(rfkill_rk_exit);
748
749 MODULE_DESCRIPTION("rock-chips rfkill for Bluetooth v0.2");
750 MODULE_AUTHOR("cmy@rock-chips.com, cz@rock-chips.com");
751 MODULE_LICENSE("GPL");
752