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