ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / board-rk30-phone-loquat-rfkill.c
1 /*
2  * Copyright (C) 2010 ROCKCHIP, Inc.
3  * Author: roger_chen <cz@rock-chips.com>
4  *
5  * This program is the bluetooth device bcm4330's driver,
6  *
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/platform_device.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/rfkill.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/wakelock.h>
20 #include <linux/fs.h>
21 #include <asm/uaccess.h>
22 #include <mach/gpio.h>
23 #include <asm/irq.h>
24 #include <mach/iomux.h>
25 #include <linux/wakelock.h>
26 #include <linux/timer.h>
27 #include <mach/board.h>
28
29 #if 0
30 #define DBG(x...)   printk(KERN_INFO "[BT_RFKILL]: "x)
31 #else
32 #define DBG(x...)
33 #endif
34
35 #define LOG(x...)   printk(KERN_INFO "[BT_RFKILL]: "x)
36
37 #ifdef CONFIG_BCM4329
38 #define WIFI_BT_POWER_TOGGLE    1
39 #else
40 #define WIFI_BT_POWER_TOGGLE    0
41 #endif
42
43 #define BT_WAKE_LOCK_TIMEOUT    10 //s
44
45 #define BT_AUTO_SLEEP_TIMEOUT   3
46
47 /*
48  * IO Configuration for RK29
49  */
50 #ifdef CONFIG_ARCH_RK29
51
52 #define BT_WAKE_HOST_SUPPORT    0
53
54 /* IO configuration */
55 // BT power pin
56 #define BT_GPIO_POWER           RK29_PIN5_PD6
57 #define IOMUX_BT_GPIO_POWER()     rk29_mux_api_set(GPIO5D6_SDMMC1PWREN_NAME, GPIO5H_GPIO5D6);
58
59 // BT reset pin
60 #define BT_GPIO_RESET           RK29_PIN6_PC4
61 #define IOMUX_BT_GPIO_RESET()
62
63 // BT wakeup pin
64 #define BT_GPIO_WAKE_UP         RK29_PIN6_PC5
65 #define IOMUX_BT_GPIO_WAKE_UP()
66
67 // BT wakeup host pin
68 #define BT_GPIO_WAKE_UP_HOST
69 #define IOMUX_BT_GPIO_WAKE_UP_HOST()
70
71 //bt cts paired to uart rts
72 #define UART_RTS                RK29_PIN2_PA7
73 #define IOMUX_UART_RTS_GPIO()   rk29_mux_api_set(GPIO2A7_UART2RTSN_NAME, GPIO2L_GPIO2A7)
74 #define IOMUX_UART_RTS()        rk29_mux_api_set(GPIO2A7_UART2RTSN_NAME, GPIO2L_UART2_RTS_N)
75
76 /*
77  * IO Configuration for RK30
78  */
79 #elif defined (CONFIG_ARCH_RK30)
80
81 #define BT_WAKE_HOST_SUPPORT    1
82
83 /* IO configuration */
84 // BT power pin
85 #define BT_GPIO_POWER           RK30_PIN4_PD5
86 #define IOMUX_BT_GPIO_POWER     rk30_mux_api_set(GPIO4D5_SMCDATA13_TRACEDATA13_NAME, GPIO4D_GPIO4D5)
87
88 // BT reset pin
89 #define BT_GPIO_RESET           RK30_PIN3_PD1
90 #define IOMUX_BT_GPIO_RESET     rk30_mux_api_set(GPIO3D1_SDMMC1BACKENDPWR_NAME, GPIO3D_GPIO3D1)
91
92
93 // BT wakeup pin
94 #define BT_GPIO_WAKE_UP         RK30_PIN3_PC6
95 #define IOMUX_BT_GPIO_WAKE_UP() rk29_mux_api_set(GPIO3C6_SDMMC1DETECTN_NAME, GPIO3C_GPIO3C6);
96
97 // BT wakeup host pin
98 #define BT_GPIO_WAKE_UP_HOST    RK30_PIN3_PD2
99 #define IOMUX_BT_GPIO_WAKE_UP_HOST() rk29_mux_api_set(GPIO3D2_SDMMC1INTN_NAME, GPIO3D_GPIO3D2)
100 #define BT_IRQ_WAKE_UP_HOST     gpio_to_irq(BT_GPIO_WAKE_UP_HOST)
101
102 //bt cts paired to uart rts
103 #define UART_RTS                RK30_PIN1_PA3
104 #define IOMUX_UART_RTS_GPIO()     rk29_mux_api_set(GPIO1A3_UART0RTSN_NAME, GPIO1A_GPIO1A3)
105 #define IOMUX_UART_RTS()          rk29_mux_api_set(GPIO1A3_UART0RTSN_NAME, GPIO1A_UART0_RTS_N)
106
107 #endif
108
109 struct bt_ctrl
110 {
111     struct rfkill *bt_rfk;
112 #if BT_WAKE_HOST_SUPPORT
113     struct timer_list tl;
114     bool b_HostWake;
115     struct wake_lock bt_wakelock;
116 #endif
117 };
118
119 static const char bt_name[] = 
120 #if defined(CONFIG_RKWIFI)
121     #if defined(CONFIG_RKWIFI_26M)
122         "rk903_26M"
123     #else
124         "rk903"
125     #endif
126 #elif defined(CONFIG_BCM4329)
127     "bcm4329"
128 #elif defined(CONFIG_MV8787)
129     "mv8787"
130 #else
131     "bt_default"
132 #endif
133 ;
134
135 #if WIFI_BT_POWER_TOGGLE
136 extern int rk29sdk_bt_power_state;
137 extern int rk29sdk_wifi_power_state;
138 #endif
139
140 struct bt_ctrl gBtCtrl;
141 struct timer_list bt_sleep_tl;
142
143
144 #if BT_WAKE_HOST_SUPPORT
145 void resetBtHostSleepTimer(void)
146 {
147     mod_timer(&(gBtCtrl.tl),jiffies + BT_WAKE_LOCK_TIMEOUT*HZ);//ÔÙÖØÐÂÉèÖó¬Ê±Öµ¡£    
148 }
149
150 void btWakeupHostLock(void)
151 {
152     if(gBtCtrl.b_HostWake == false){
153         DBG("** Lock **\n");
154         wake_lock(&(gBtCtrl.bt_wakelock));
155         gBtCtrl.b_HostWake = true;
156     }
157 }
158
159 void btWakeupHostUnlock(void)
160 {
161     if(gBtCtrl.b_HostWake == true){        
162         DBG("** UnLock **\n");
163         wake_unlock(&(gBtCtrl.bt_wakelock));  //ÈÃϵͳ˯Ãß    
164         gBtCtrl.b_HostWake = false;
165     }    
166 }
167
168 static void timer_hostSleep(unsigned long arg)
169 {     
170         DBG("b_HostWake=%d\n", gBtCtrl.b_HostWake);
171     btWakeupHostUnlock();
172 }
173
174 void bcm4325_sleep(unsigned long bSleep);
175
176 #ifdef CONFIG_PM
177 static irqreturn_t bcm4329_wake_host_irq(int irq, void *dev)
178 {
179     DBG("%s\n",__FUNCTION__);
180
181     btWakeupHostLock();
182     resetBtHostSleepTimer();
183         return IRQ_HANDLED;
184 }
185
186 static void rfkill_do_wakeup(struct work_struct *work)
187 {
188     // disable bt wakeup host
189     DBG("** free irq\n");
190     free_irq(BT_IRQ_WAKE_UP_HOST, NULL);
191
192     DBG("Enable UART_RTS\n");
193     gpio_set_value(UART_RTS, GPIO_LOW);
194     IOMUX_UART_RTS();
195 }
196
197 static DECLARE_DELAYED_WORK(wakeup_work, rfkill_do_wakeup);
198
199 static int bcm4329_rfkill_suspend(struct platform_device *pdev, pm_message_t state)
200 {
201     DBG("%s\n",__FUNCTION__);
202
203     cancel_delayed_work(&wakeup_work);
204
205 #ifdef CONFIG_BT_AUTOSLEEP
206     bcm4325_sleep(1);
207 #endif
208
209     DBG("Disable UART_RTS\n");
210         //To prevent uart to receive bt data when suspended
211         IOMUX_UART_RTS_GPIO();
212         gpio_request(UART_RTS, "uart_rts");
213         gpio_set_value(UART_RTS, GPIO_HIGH);
214
215     // enable bt wakeup host
216     DBG("Request irq for bt wakeup host\n");
217         if (0 == request_irq(BT_IRQ_WAKE_UP_HOST,
218                     bcm4329_wake_host_irq,
219                     IRQF_TRIGGER_FALLING,
220                     "bt_wake",
221                     NULL))
222         enable_irq_wake(BT_IRQ_WAKE_UP_HOST);
223     else
224                 LOG("Failed to request BT_WAKE_UP_HOST irq\n");
225
226 #ifdef CONFIG_RFKILL_RESET
227     extern void rfkill_set_block(struct rfkill *rfkill, bool blocked);
228     rfkill_set_block(gBtCtrl.bt_rfk, true);
229 #endif
230
231     return 0;
232 }
233
234 static int bcm4329_rfkill_resume(struct platform_device *pdev)
235 {  
236     DBG("%s\n",__FUNCTION__);
237
238     // ÏµÍ³Í˳ö¶þ¼¶Ë¯ÃߺóÐèÒªÀ­µÍRTS£¬´Ó¶ø²ÅÔÊÐíBT·¢Êý¾Ý¹ýÀ´
239     // µ«ÊÇÄ¿Ç°·¢ÏÖÔÚresumeº¯ÊýÖÐÖ±½ÓÀ­µÍRTS»áµ¼ÖÂBTÊý¾Ý¶ªÊ§
240     // ËùÒÔÑÓ³Ù1sºóÔÙÀ­µÍRTS
241     // ÏµÍ³Í˳ö¶þ¼¶Ë¯ÃßʱÊͷŵôBT_IRQ_WAKE_UP_HOST£¬ÔÚ˯ÃßʱºòÔÙ
242     // ´ÎÉêÇ룬Ŀǰ·¢ÏÖÖжϻص÷º¯Êý±Èresume¸üÍíÖ´ÐУ¬Èç¹ûresume
243     // Ê±Ö±½ÓfreeµôIRQ£¬»áµ¼ÖÂÖжϻص÷º¯Êý²»»á±»Ö´ÐУ¬
244     DBG("delay 1s\n");
245     schedule_delayed_work(&wakeup_work, HZ);
246
247     return 0;
248 }
249 #else
250 #define bcm4329_rfkill_suspend NULL
251 #define bcm4329_rfkill_resume  NULL
252 #endif
253
254 #endif
255
256 void bcm4325_sleep(unsigned long bSleep)
257 {
258     DBG("*** bt sleep: %d ***\n", bSleep);
259 #ifdef CONFIG_BT_AUTOSLEEP
260     del_timer(&bt_sleep_tl);// cmy: È·±£ÔÚ»½ÐÑBTʱ£¬²»»áÒò´¥·¢bt_sleep_tl¶øÂíÉÏ˯Ãß
261 #endif
262
263     IOMUX_BT_GPIO_WAKE_UP();
264     gpio_set_value(BT_GPIO_WAKE_UP, bSleep?GPIO_LOW:GPIO_HIGH);
265
266 #ifdef CONFIG_BT_AUTOSLEEP
267     if(!bSleep)
268         mod_timer(&bt_sleep_tl, jiffies + BT_AUTO_SLEEP_TIMEOUT*HZ);//ÔÙÖØÐÂÉèÖó¬Ê±Öµ¡£
269 #endif
270 }
271
272 static int bcm4329_set_block(void *data, bool blocked)
273 {
274         DBG("set blocked :%d\n", blocked);
275
276     IOMUX_BT_GPIO_POWER;
277     IOMUX_BT_GPIO_RESET;
278
279         if (false == blocked) { 
280                 gpio_set_value(BT_GPIO_POWER, GPIO_HIGH);  /* bt power on */
281                 mdelay(20);
282
283         gpio_set_value(BT_GPIO_RESET, GPIO_LOW);
284         mdelay(20);
285                 gpio_set_value(BT_GPIO_RESET, GPIO_HIGH);  /* bt reset deactive*/
286
287         mdelay(20);
288         bcm4325_sleep(0); // ensure bt is wakeup
289         
290         pr_info("bt turn on power\n");
291         } else {
292 #if WIFI_BT_POWER_TOGGLE
293                 if (!rk29sdk_wifi_power_state) {
294 #endif
295                         gpio_set_value(BT_GPIO_POWER, GPIO_LOW);  /* bt power off */
296                 mdelay(20);     
297                 pr_info("bt shut off power\n");
298 #if WIFI_BT_POWER_TOGGLE
299                 }else {
300                         pr_info("bt shouldn't shut off power, wifi is using it!\n");
301                 }
302 #endif
303
304         gpio_set_value(BT_GPIO_RESET, GPIO_LOW);  /* bt reset active*/
305         mdelay(20);
306         }
307
308 #if WIFI_BT_POWER_TOGGLE
309         rk29sdk_bt_power_state = !blocked;
310 #endif
311         return 0;
312 }
313
314 static const struct rfkill_ops bcm4329_rfk_ops = {
315         .set_block = bcm4329_set_block,
316 };
317
318 static int __devinit bcm4329_rfkill_probe(struct platform_device *pdev)
319 {
320         int rc = 0;
321         bool default_state = true;
322         
323         DBG("Enter %s\n",__FUNCTION__);
324         
325         /* default to bluetooth off */
326         bcm4329_set_block(NULL, default_state); /* blocked -> bt off */
327          
328         gBtCtrl.bt_rfk = rfkill_alloc(bt_name, 
329                 NULL, 
330                 RFKILL_TYPE_BLUETOOTH, 
331                 &bcm4329_rfk_ops, 
332                 NULL);
333
334         if (!gBtCtrl.bt_rfk)
335         {
336                 LOG("fail to rfkill_allocate\n");
337                 return -ENOMEM;
338         }
339         
340         rfkill_set_states(gBtCtrl.bt_rfk, default_state, false);
341
342         rc = rfkill_register(gBtCtrl.bt_rfk);
343         if (rc)
344         {
345                 LOG("failed to rfkill_register,rc=0x%x\n",rc);
346                 rfkill_destroy(gBtCtrl.bt_rfk);
347         }
348         
349         gpio_request(BT_GPIO_POWER, NULL);
350         gpio_request(BT_GPIO_RESET, NULL);
351         gpio_request(BT_GPIO_WAKE_UP, NULL);
352     
353 #ifdef CONFIG_BT_AUTOSLEEP
354     init_timer(&bt_sleep_tl);
355     bt_sleep_tl.expires = 0;
356     bt_sleep_tl.function = bcm4325_sleep;
357     bt_sleep_tl.data = 1;
358     add_timer(&bt_sleep_tl);
359 #endif
360
361 #if BT_WAKE_HOST_SUPPORT
362     init_timer(&(gBtCtrl.tl));
363     gBtCtrl.tl.expires = 0;
364     gBtCtrl.tl.function = timer_hostSleep;
365     add_timer(&(gBtCtrl.tl));
366     gBtCtrl.b_HostWake = false;
367     
368         wake_lock_init(&(gBtCtrl.bt_wakelock), WAKE_LOCK_SUSPEND, "bt_wake");
369         
370         rc = gpio_request(BT_GPIO_WAKE_UP_HOST, "bt_wake");
371         if (rc) {
372                 LOG("Failed to request BT_WAKE_UP_HOST\n");
373         }
374         
375         IOMUX_BT_GPIO_WAKE_UP_HOST();
376         gpio_pull_updown(BT_GPIO_WAKE_UP_HOST,GPIOPullUp);
377  #endif
378  
379     LOG("bcm4329 module has been initialized,rc=0x%x\n",rc);
380  
381         return rc;
382 }
383
384
385 static int __devexit bcm4329_rfkill_remove(struct platform_device *pdev)
386 {
387         if (gBtCtrl.bt_rfk)
388                 rfkill_unregister(gBtCtrl.bt_rfk);
389         gBtCtrl.bt_rfk = NULL;
390 #if BT_WAKE_HOST_SUPPORT
391     del_timer(&(gBtCtrl.tl));//ɾµô¶¨Ê±Æ÷
392     btWakeupHostUnlock();
393     wake_lock_destroy(&(gBtCtrl.bt_wakelock));
394 #endif
395 #ifdef CONFIG_BT_AUTOSLEEP
396     del_timer(&bt_sleep_tl);
397 #endif
398
399         platform_set_drvdata(pdev, NULL);
400
401         DBG("Enter %s\n",__FUNCTION__);
402         return 0;
403 }
404
405 static struct platform_driver bcm4329_rfkill_driver = {
406         .probe = bcm4329_rfkill_probe,
407         .remove = __devexit_p(bcm4329_rfkill_remove),
408         .driver = {
409                 .name = "rk29sdk_rfkill", 
410                 .owner = THIS_MODULE,
411         },      
412 #if BT_WAKE_HOST_SUPPORT
413     .suspend = bcm4329_rfkill_suspend,
414     .resume = bcm4329_rfkill_resume,
415 #endif
416 };
417
418 /*
419  * Module initialization
420  */
421 static int __init bcm4329_mod_init(void)
422 {
423         int ret;
424         DBG("Enter %s\n",__FUNCTION__);
425         ret = platform_driver_register(&bcm4329_rfkill_driver);
426         LOG("ret=0x%x\n", ret);
427         return ret;
428 }
429
430 static void __exit bcm4329_mod_exit(void)
431 {
432         platform_driver_unregister(&bcm4329_rfkill_driver);
433 }
434
435 module_init(bcm4329_mod_init);
436 module_exit(bcm4329_mod_exit);
437 MODULE_DESCRIPTION("bcm4329 Bluetooth driver");
438 MODULE_AUTHOR("roger_chen cz@rock-chips.com, cmy@rock-chips.com");
439 MODULE_LICENSE("GPL");
440