1 /*
2 ** Bluetooth sleep and wakeup host interface for XRADIO Drivers
3 **
4 ** Copyright (c) 2013, XRadio
5 ** Author: XRadio
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License version 2 as
9 ** published by the Free Software Foundation.
10 */
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/notifier.h>
17 #include <linux/proc_fs.h>
18 #include <linux/spinlock.h>
19 #include <linux/timer.h>
20 #include <linux/uaccess.h>
21 #include <linux/version.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/irq.h>
25 #include <linux/param.h>
26 #include <linux/bitops.h>
27 #include <linux/termios.h>
28 #include <linux/gpio.h>
29 #include <linux/of_gpio.h>
30 #include <linux/of_platform.h>
31 #include <linux/pm_wakeirq.h>
32 #include <linux/serial_core.h>
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
35
36
37 #define BT_SLEEP_DEBUG
38
39 #ifdef BT_SLEEP_DEBUG
40 #define BT_SLEEP_DBG(fmt, arg...) printk(KERN_DEBUG "[XR_BT_LPM] %s: " fmt "\n", __func__, ## arg)
41 #else
42 #define BT_SLEEP_DBG(fmt, arg...)
43 #endif
44
45 #define BT_SLEEP_INF(fmt, arg...) printk(KERN_INFO "[XR_BT_LPM] %s: " fmt "\n", __func__, ## arg)
46 #define VERSION "1.0.10"
47 #define PROC_SLEEP_DIR "bluetooth/sleep"
48 #define PROC_POWER_DIR "bluetooth/power"
49 #define BT_BLUEDROID_SUPPORT 1
50
51 #define AW1732_BT 1
52 #define XRADIO_ETF_RFKILL 1
53
54 struct xr_btsleep_info {
55 unsigned int wakeup_enable;
56 unsigned int host_wake;
57 unsigned int ext_wake;
58 unsigned int host_wake_irq;
59 struct wakeup_source *ws;
60 struct uart_port *uport;
61 unsigned int host_wake_polarity:1;
62 unsigned int bt_wake_polarity:1;
63 struct platform_device *pdev;
64 };
65
66 /* work function */
67 static void bluesleep_sleep_work(struct work_struct *work);
68
69
70 /* work queue */
71 DECLARE_DELAYED_WORK(sleep_workqueue, bluesleep_sleep_work);
72
73
74 /* Macros for handling sleep work */
75 #define bluesleep_rx_busy() schedule_delayed_work(&sleep_workqueue, 0)
76 #define bluesleep_tx_busy() schedule_delayed_work(&sleep_workqueue, 0)
77 #define bluesleep_rx_idle() schedule_delayed_work(&sleep_workqueue, 0)
78 #define bluesleep_tx_idle() schedule_delayed_work(&sleep_workqueue, 0)
79 #define bluesleep_wake_up() schedule_delayed_work(&sleep_workqueue, 0)
80 #define bluesleep_standby() schedule_delayed_work(&sleep_workqueue, 0)
81
82
83
84 /* 10 second timeout */
85 #define TX_TIMER_INTERVAL 2 //10s
86
87 /* state variable names and bit positions */
88 #define BT_PROTO 0x01
89 #define BT_TXDATA 0x02
90 #define BT_ASLEEP 0x04
91
92
93 /* variable use indicate lpm modle */
94 volatile bool has_lpm_enabled;
95
96 /* struct use save platform_device from uart */
97 static struct platform_device *bluesleep_uart_dev;
98 static struct xr_btsleep_info *bsi;
99
100 /* Global state flags */
101 static unsigned long flags;
102
103 #ifdef AW1732_BT
104 /* module usage */
105 static atomic_t open_count = ATOMIC_INIT(1);
106 /* Tasklet to respond to change in hostwake line */
107 static struct tasklet_struct hostwake_task;
108 #endif
109
110 /* Transmission timer */
111 static struct timer_list tx_timer;
112
113 /* Lock for state transitions */
114 static spinlock_t rw_lock;
115 static spinlock_t irq_lock;
116
117
118 struct proc_dir_entry *bluetooth_dir, *sleep_dir;
119
120 #ifdef XRADIO_ETF_RFKILL
121 struct proc_dir_entry *power_dir;
122 volatile char power_state_save;
123 #endif
124
125 /*
126 * bt go to sleep will call this function tell uart stop data interactive
127 */
hsuart_power(int on)128 static void hsuart_power(int on)
129 {
130 if (bsi->uport == NULL) {
131 return;
132 }
133 if (on) {
134 bsi->uport->ops->set_mctrl(bsi->uport, TIOCM_RTS);
135 } else {
136 bsi->uport->ops->set_mctrl(bsi->uport, 0);
137 }
138 }
139
140 /**
141 * @return 1 if the Host can go to sleep, 0 otherwise.
142 */
bluesleep_can_sleep(void)143 static inline int bluesleep_can_sleep(void)
144 {
145 /* check if HOST_WAKE_BT_GPIO and BT_WAKE_HOST_GPIO are both deasserted */
146 int flag1, flag2;
147 flag1 = !test_bit(BT_TXDATA, &flags);
148 flag2 = (bsi->uport != NULL);
149 BT_SLEEP_DBG(" !BT_TXDATA is %d, bsi->uport is %d", flag1, flag2);
150 return (flag1 && flag2);
151 }
152
153 /*
154 * after bt wakeup should clean BT_ASLEEP flag and start time.
155 */
bluesleep_sleep_wakeup(void)156 int bluesleep_sleep_wakeup(void)
157 {
158 int poll_count = 0;
159 BT_SLEEP_DBG("test_bit(BT_ASLEEP, &flags) %d", test_bit(BT_ASLEEP, &flags));
160 if (gpio_get_value(bsi->ext_wake) != bsi->bt_wake_polarity ||
161 gpio_get_value(bsi->host_wake) != bsi->host_wake_polarity ||
162 test_bit(BT_ASLEEP, &flags) == 1) {
163 BT_SLEEP_DBG("waking up...");
164 /*Activating UART */
165 hsuart_power(1);
166 gpio_set_value(bsi->ext_wake, bsi->bt_wake_polarity);
167 //add poll bsi->hostwake
168 while (poll_count++ < 6) {
169 mdelay(25);
170 if (gpio_get_value(bsi->host_wake) == 1) {
171 BT_SLEEP_DBG("bt device active");
172 /*wake up, maybe no data or cmd need send to fw*/
173 /*so didn't change TXDATA in here*/
174 clear_bit(BT_ASLEEP, &flags);
175 return 0;
176 }
177 }
178 /*
179 *add once more wakeup operation when first operation failed,
180 *because if host wakeup device during device is wakeuped by yourself,
181 *device maybe couldn't receive wakeup GPIO rise-up signal
182 */
183 poll_count = 0;
184 while (poll_count++ < 6) {
185 mdelay(25);
186 if (gpio_get_value(bsi->host_wake) == 1) {
187 BT_SLEEP_DBG("bt device active");
188 /*wake up, maybe no data or cmd need send to fw*/
189 /*so didn't change TXDATA in here*/
190 clear_bit(BT_ASLEEP, &flags);
191 return 0;
192 }
193 }
194 return 1;
195 } else {
196 BT_SLEEP_DBG("bluesleep_sleep_wakeup del_timer");
197 del_timer(&tx_timer);
198 return 0;
199 }
200 return 1;
201 }
202
203 /**
204 * @brief@ main sleep work handling function which update the flags
205 * and activate and deactivate UART ,check FIFO.
206 */
bluesleep_sleep_work(struct work_struct * work)207 static void bluesleep_sleep_work(struct work_struct *work)
208 {
209 if (!has_lpm_enabled) {
210 BT_SLEEP_DBG("lpm disabled already");
211 return;
212 }
213
214 if (bluesleep_can_sleep()) {
215 /* already asleep, this is an error case */
216 if (test_bit(BT_ASLEEP, &flags)) {
217 BT_SLEEP_DBG("already asleep");
218 return;
219 }
220 if (bsi->uport->ops->tx_empty(bsi->uport)) {
221 BT_SLEEP_DBG("going to sleep...");
222 gpio_set_value(bsi->ext_wake, !bsi->bt_wake_polarity);
223 set_bit(BT_ASLEEP, &flags);
224 clear_bit(BT_TXDATA, &flags);
225 /*Deactivating UART */
226 hsuart_power(0);
227 __pm_relax(bsi->ws);
228 }
229 } else {
230 bluesleep_sleep_wakeup();
231 }
232 }
233
234 #ifdef AW1732_BT
bluesleep_hostwake_task(unsigned long data)235 static void bluesleep_hostwake_task(unsigned long data)
236 {
237 BT_SLEEP_DBG("hostwake line change");
238 if (!has_lpm_enabled) {
239 return;
240 }
241 spin_lock(&rw_lock);
242 //wakeup signal from fw
243 if (test_bit(BT_ASLEEP, &flags) && !test_bit(BT_TXDATA, &flags)) {
244 clear_bit(BT_ASLEEP, &flags);
245 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
246 bluesleep_sleep_wakeup();
247 }
248 spin_unlock(&rw_lock);
249 }
250 #endif
251
252
bluesleep_outgoing_data(void)253 static void bluesleep_outgoing_data(void)
254 {
255 unsigned long irq_flags;
256 BT_SLEEP_DBG();
257 spin_lock_irqsave(&rw_lock, irq_flags);
258 /* log data passing by */
259 set_bit(BT_TXDATA, &flags);
260 /* if the tx side is sleeping... */
261 if (gpio_get_value(bsi->ext_wake) != bsi->bt_wake_polarity ||
262 gpio_get_value(bsi->host_wake) != bsi->host_wake_polarity) {
263 BT_SLEEP_DBG("tx was sleeping");
264 if (bluesleep_sleep_wakeup() == 1) {
265 BT_SLEEP_DBG("can't wakeup bt device!");
266 }
267 }
268 spin_unlock_irqrestore(&rw_lock, irq_flags);
269 }
270
bluesleep_get_uart_port(void)271 static struct uart_port *bluesleep_get_uart_port(void)
272 {
273 struct uart_port *uport = NULL;
274 BT_INFO("%s enter.", __func__);
275 if (bluesleep_uart_dev) {
276 uport = platform_get_drvdata(bluesleep_uart_dev);
277 BT_INFO("%s get uart_port from blusleep_uart_dev: %s",
278 __func__, bluesleep_uart_dev->name);
279 }
280 return uport;
281 }
282
283 #ifdef AW1732_BT
bluesleep_tx_timer_expire(struct timer_list * t)284 static void bluesleep_tx_timer_expire(struct timer_list *t)
285 {
286 unsigned long irq_flags;
287 spin_lock_irqsave(&rw_lock, irq_flags);
288 BT_SLEEP_DBG("Tx timer expired");
289 /* were we silent during the last timeout */
290 if (!test_bit(BT_ASLEEP, &flags) && !test_bit(BT_TXDATA, &flags)) {
291 BT_SLEEP_DBG("Tx has been idle");
292 bluesleep_tx_idle();
293 } else {
294 BT_SLEEP_DBG("Tx data during last period");
295 }
296
297 del_timer(&tx_timer);
298 /* clear the incoming data flag */
299 spin_unlock_irqrestore(&rw_lock, irq_flags);
300 }
301
302
bluesleep_hostwake_isr(int irq,void * dev_id)303 static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
304 {
305 /* schedule a tasklet to handle the change in the host wake line */
306 tasklet_schedule(&hostwake_task);
307 __pm_stay_awake(bsi->ws);
308 return IRQ_HANDLED;
309 }
310
311
bluesleep_start(void)312 static int bluesleep_start(void)
313 {
314 int retval;
315 unsigned long irq_flags;
316
317 spin_lock_irqsave(&rw_lock, irq_flags);
318 if (test_bit(BT_PROTO, &flags)) {
319 spin_unlock_irqrestore(&rw_lock, irq_flags);
320 return 0;
321 }
322 spin_unlock_irqrestore(&rw_lock, irq_flags);
323
324 if (!atomic_dec_and_test(&open_count)) {
325 atomic_inc(&open_count);
326 return -EBUSY;
327 }
328 /*wake up bt device ,initial irq service*/
329 gpio_set_value(bsi->ext_wake, bsi->bt_wake_polarity);
330 retval = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
331 bsi->host_wake_polarity ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING,
332 "bluetooth hostwake", &bsi->pdev->dev);
333
334 if (retval < 0) {
335 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
336 goto fail;
337 }
338
339 set_bit(BT_PROTO, &flags);
340 __pm_stay_awake(bsi->ws);
341
342 return 0;
343
344
345 fail:
346 atomic_inc(&open_count);
347 return retval;
348 }
349
bluesleep_stop(void)350 static void bluesleep_stop(void)
351 {
352 unsigned long irq_flags;
353
354 spin_lock_irqsave(&rw_lock, irq_flags);
355
356 if (!test_bit(BT_PROTO, &flags)) {
357 spin_unlock_irqrestore(&rw_lock, irq_flags);
358 return;
359 }
360
361 clear_bit(BT_PROTO, &flags);
362 del_timer(&tx_timer);
363
364 if (test_bit(BT_ASLEEP, &flags)) {
365 clear_bit(BT_ASLEEP, &flags);
366 hsuart_power(1);
367 }
368
369 if (test_bit(BT_TXDATA, &flags)) {
370 clear_bit(BT_TXDATA, &flags);
371 }
372
373 atomic_inc(&open_count);
374 spin_unlock_irqrestore(&rw_lock, irq_flags);
375 free_irq(bsi->host_wake_irq, &bsi->pdev->dev);
376 __pm_wakeup_event(bsi->ws, jiffies_to_msecs(HZ * 5));
377 }
378 #endif
379
380
381 #ifdef XRADIO_ETF_RFKILL
382 extern void sunxi_bluetooth_set_power(bool on_off);
383
bluetooth_pwr_proc_show(struct seq_file * m,void * v)384 static int bluetooth_pwr_proc_show(struct seq_file *m, void *v)
385 {
386 seq_printf(m, "power state: %d\n", power_state_save);
387 return 0;
388 }
389
bluetooth_pwr_proc_open(struct inode * inode,struct file * file)390 static int bluetooth_pwr_proc_open(struct inode *inode, struct file *file)
391 {
392 return single_open(file, bluetooth_pwr_proc_show, NULL);
393 }
394
bluetooth_write_proc_pwr(struct file * file,const char __user * buffer,size_t count,loff_t * pos)395 static ssize_t bluetooth_write_proc_pwr(struct file *file, const char __user *buffer,
396 size_t count, loff_t *pos)
397 {
398 char b;
399
400 if (count < 1)
401 return -EINVAL;
402
403 if (copy_from_user(&b, buffer, 1))
404 return -EFAULT;
405
406 if (b == '0') {
407 BT_SLEEP_INF("bt power off");
408 //sunxi_bluetooth_set_power(false);
409 power_state_save = 0;
410 } else {
411 BT_SLEEP_INF("bt power on");
412 //sunxi_bluetooth_set_power(true);
413 power_state_save = 1;
414 }
415
416 return count;
417 }
418 #endif
419
bluedroid_lpm_proc_show(struct seq_file * m,void * v)420 static int bluedroid_lpm_proc_show(struct seq_file *m, void *v)
421 {
422 seq_printf(m, "lpm enable: %d\n", has_lpm_enabled);
423 return 0;
424 }
425
bluedroid_lpm_proc_open(struct inode * inode,struct file * file)426 static int bluedroid_lpm_proc_open(struct inode *inode, struct file *file)
427 {
428 return single_open(file, bluedroid_lpm_proc_show, NULL);
429 }
430
bluedroid_write_proc_lpm(struct file * file,const char __user * buffer,size_t count,loff_t * pos)431 static ssize_t bluedroid_write_proc_lpm(struct file *file, const char __user *buffer,
432 size_t count, loff_t *pos)
433
434 {
435 char b;
436
437 if (count < 1)
438 return -EINVAL;
439
440 if (copy_from_user(&b, buffer, 1))
441 return -EFAULT;
442
443 #ifdef AW1732_BT
444 if (b == '0') {
445 has_lpm_enabled = false;
446 BT_SLEEP_DBG("disable lpm mode");
447 bluesleep_stop();
448 bsi->uport = NULL;
449 } else {
450 BT_SLEEP_DBG("enable lpm mode");
451 if (!has_lpm_enabled) {
452 has_lpm_enabled = true;
453 if (bluesleep_uart_dev)
454 bsi->uport = bluesleep_get_uart_port();
455 bluesleep_start();
456 }
457 }
458 #else //work around for aw1722 wakeup bt before brom download
459 if (b == '0') { //disable lpm mode
460 BT_SLEEP_DBG("disable lpm mode");
461 has_lpm_enabled = false;
462 } else { //enable lpm mode
463 BT_SLEEP_DBG("enable lpm mode");
464 has_lpm_enabled = true;
465 if (bluesleep_uart_dev)
466 bsi->uport = bluesleep_get_uart_port();
467 }
468 #endif
469
470 return count;
471 }
472
bluedroid_btwrite_proc_show(struct seq_file * m,void * v)473 static int bluedroid_btwrite_proc_show(struct seq_file *m, void *v)
474 {
475 seq_printf(m, "it's not support\n");
476 return 0;
477 }
478
bluedroid_btwrite_proc_open(struct inode * inode,struct file * file)479 static int bluedroid_btwrite_proc_open(struct inode *inode, struct file *file)
480 {
481 return single_open(file, bluedroid_btwrite_proc_show, NULL);
482 }
483
bluedroid_write_proc_btwrite(struct file * file,const char __user * buffer,size_t count,loff_t * pos)484 static ssize_t bluedroid_write_proc_btwrite(struct file *file, const char __user *buffer,
485 size_t count, loff_t *pos)
486 {
487 char b;
488
489 if (count < 1)
490 return -EINVAL;
491
492 if (copy_from_user(&b, buffer, 1))
493 return -EFAULT;
494
495 BT_SLEEP_DBG("bluedroid_write_proc_btwrite %c", b);
496 if (b == '1') {
497 mdelay(10);
498 set_bit(BT_TXDATA, &flags);
499 bluesleep_outgoing_data();
500 //start the timer when host receive event but reply no data or cmd send to fw
501 del_timer(&tx_timer);
502 } else if (b == '0') {
503 clear_bit(BT_TXDATA, &flags);
504 bluesleep_tx_idle();
505 }
506 return count;
507 }
508
bluedroid_btwake_proc_show(struct seq_file * m,void * v)509 static int bluedroid_btwake_proc_show(struct seq_file *m, void *v)
510 {
511 seq_printf(m, "bt wake state:%d\n", gpio_get_value(bsi->ext_wake));
512 return 0;
513 }
514
bluedroid_btwake_proc_open(struct inode * inode,struct file * file)515 static int bluedroid_btwake_proc_open(struct inode *inode, struct file *file)
516 {
517 return single_open(file, bluedroid_btwake_proc_show, NULL);
518 }
519
bluedroid_write_proc_btwake(struct file * file,const char __user * buffer,size_t count,loff_t * pos)520 static ssize_t bluedroid_write_proc_btwake(struct file *file, const char __user *buffer,
521 size_t count, loff_t *pos)
522 {
523 char *buf; if (count < 1)
524 return -EINVAL;
525
526 buf = kmalloc(count, GFP_KERNEL);
527 if (!buf)
528 return -ENOMEM;
529
530 if (copy_from_user(buf, buffer, count)) {
531 kfree(buf);
532 return -EFAULT;
533 }
534 BT_SLEEP_DBG("bluedroid_write_proc_btwake %c", buf[0]);
535 if (buf[0] == '0') {
536 BT_SLEEP_DBG("bt sleeping");
537 gpio_set_value(bsi->ext_wake, !bsi->bt_wake_polarity);
538 } else if (buf[0] == '1') {
539 BT_SLEEP_DBG("wakeup bt device");
540 gpio_set_value(bsi->ext_wake, bsi->bt_wake_polarity);
541 } else {
542 kfree(buf);
543 return -EINVAL;
544 }
545
546 kfree(buf);
547 return count;
548 }
549
550 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
551
552 #ifdef XRADIO_ETF_RFKILL
553 static const struct proc_ops pwr_fops = {
554 .proc_open = bluetooth_pwr_proc_open,
555 .proc_read = seq_read,
556 .proc_lseek = seq_lseek,
557 .proc_release = single_release,
558 .proc_write = bluetooth_write_proc_pwr,
559 };
560 #endif
561
562 static const struct proc_ops lpm_fops = {
563 .proc_open = bluedroid_lpm_proc_open,
564 .proc_read = seq_read,
565 .proc_lseek = seq_lseek,
566 .proc_release = single_release,
567 .proc_write = bluedroid_write_proc_lpm,
568 };
569
570 static const struct proc_ops btwrite_fops = {
571 .proc_open = bluedroid_btwrite_proc_open,
572 .proc_read = seq_read,
573 .proc_lseek = seq_lseek,
574 .proc_release = single_release,
575 .proc_write = bluedroid_write_proc_btwrite,
576 };
577
578 static const struct proc_ops btwake_fops = {
579 .proc_open = bluedroid_btwake_proc_open,
580 .proc_read = seq_read,
581 .proc_lseek = seq_lseek,
582 .proc_release = single_release,
583 .proc_write = bluedroid_write_proc_btwake,
584 };
585
586 #else
587
588 #ifdef XRADIO_ETF_RFKILL
589 static const struct file_operations pwr_fops = {
590 .owner = THIS_MODULE,
591 .open = bluetooth_pwr_proc_open,
592 .read = seq_read,
593 .llseek = seq_lseek,
594 .release = single_release,
595 .write = bluetooth_write_proc_pwr,
596 };
597 #endif
598
599 static const struct file_operations lpm_fops = {
600 .owner = THIS_MODULE,
601 .open = bluedroid_lpm_proc_open,
602 .read = seq_read,
603 .llseek = seq_lseek,
604 .release = single_release,
605 .write = bluedroid_write_proc_lpm,
606 };
607 static const struct file_operations btwrite_fops = {
608 .owner = THIS_MODULE,
609 .open = bluedroid_btwrite_proc_open,
610 .read = seq_read,
611 .llseek = seq_lseek,
612 .release = single_release,
613 .write = bluedroid_write_proc_btwrite,
614 };
615 static const struct file_operations btwake_fops = {
616 .owner = THIS_MODULE,
617 .open = bluedroid_btwake_proc_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621 .write = bluedroid_write_proc_btwake,
622 };
623 #endif
624
625 static int assert_level = -1;
626 module_param(assert_level, int, S_IRUGO);
627 MODULE_PARM_DESC(assert_level, "BT_LPM hostwake/btwake assert level");
628
sw_uart_get_pdev(int id)629 static struct platform_device *sw_uart_get_pdev(int id)
630 {
631 struct device_node *np;
632 char match[20];
633 sprintf(match, "uart%d", id);
634 np = of_find_node_by_type(NULL, match);
635 return of_find_device_by_node(np);
636 }
637
bluesleep_probe(struct platform_device * pdev)638 static int bluesleep_probe(struct platform_device *pdev)
639 {
640 struct device_node *np = pdev->dev.of_node;
641 struct device *dev = &pdev->dev;
642 enum of_gpio_flags config;
643 int ret;
644 u32 val;
645
646 bsi = devm_kzalloc(&pdev->dev, sizeof(struct xr_btsleep_info), GFP_KERNEL);
647 if (!bsi)
648 return -ENOMEM;
649
650 //get bt_wake & bt_host_wake from sys_config.fex
651 bsi->ext_wake = of_get_named_gpio_flags(np, "bt_wake", 0, &config);
652 if (!gpio_is_valid(bsi->ext_wake)) {
653 BT_ERR("get gpio bt_wake failed");
654 return -EINVAL;
655 }
656
657 bsi->bt_wake_polarity = (config == OF_GPIO_ACTIVE_LOW) ? 0 : 1;
658 if (assert_level != -1) {
659 bsi->bt_wake_polarity = (assert_level & 0x01) > 0;
660 BT_SLEEP_DBG("override bt_wake polarity to %d", bsi->bt_wake_polarity);
661 }
662
663 BT_SLEEP_DBG("bt_wake polarity: %d", bsi->bt_wake_polarity);
664
665 ret = devm_gpio_request(dev, bsi->ext_wake, "bt_wake");
666 if (ret < 0) {
667 BT_ERR("can't request bt_wake gpio %d",
668 bsi->ext_wake);
669 goto exit;
670 }
671 ret = gpio_direction_output(bsi->ext_wake, bsi->bt_wake_polarity);
672 if (ret < 0) {
673 BT_ERR("can't request output direction bt_wake gpio %d",
674 bsi->ext_wake);
675 goto exit;
676 }
677
678 bsi->host_wake = of_get_named_gpio_flags(np, "bt_hostwake", 0, &config);
679 if (!gpio_is_valid(bsi->host_wake)) {
680 BT_ERR("get gpio bt_hostwake failed");
681 return -EINVAL;
682 }
683
684 bsi->host_wake_polarity = (config == OF_GPIO_ACTIVE_LOW) ? 0 : 1;
685 if (assert_level != -1) {
686 bsi->host_wake_polarity = (assert_level & 0x02) > 0;
687 BT_SLEEP_DBG("override host_wake polarity to %d", bsi->host_wake_polarity);
688 }
689
690 BT_SLEEP_DBG("host_wake polarity: %d", bsi->host_wake_polarity);
691
692 ret = devm_gpio_request(dev, bsi->host_wake, "bt_hostwake");
693 if (ret < 0) {
694 BT_ERR("can't request bt_hostwake gpio %d",
695 bsi->host_wake);
696 goto exit;
697 }
698 ret = gpio_direction_input(bsi->host_wake);
699 if (ret < 0) {
700 BT_ERR("can't request input direction bt_wake gpio %d",
701 bsi->host_wake);
702 goto exit;
703 }
704 if (!of_property_read_bool(np, "wakeup-source")) {
705 BT_SLEEP_DBG("wakeup source is disabled!\n");
706 } else {
707 ret = device_init_wakeup(dev, true);
708 if (ret < 0) {
709 BT_ERR("device init wakeup failed!\n");
710 return ret;
711 }
712 ret = dev_pm_set_wake_irq(dev, gpio_to_irq(bsi->host_wake));
713 if (ret < 0) {
714 BT_ERR("can't enable wakeup src for bt_hostwake %d\n",
715 bsi->host_wake);
716 return ret;
717 }
718 bsi->wakeup_enable = 1;
719 }
720 if (!of_property_read_u32(np, "uart_index", &val)) {
721 BT_SLEEP_DBG("uart_index(%u)", val);
722 bluesleep_uart_dev = sw_uart_get_pdev(val);
723 }
724
725 bsi->ws = wakeup_source_register(dev, "bluesleep");
726
727 //1.set bt_wake as output and the level is 0
728 gpio_set_value(bsi->ext_wake, 0);
729
730 //2.get bt_host_wake gpio irq
731 #ifdef AW1732_BT
732 bsi->host_wake_irq = gpio_to_irq(bsi->host_wake);
733 if (bsi->host_wake_irq < 0) {
734 BT_ERR("map gpio [%d] to virq failed, errno = %d\n", bsi->host_wake, bsi->host_wake_irq);
735 ret = -ENODEV;
736 goto free_bt_ext_wake;
737 }
738 #endif
739 bsi->pdev = pdev;
740
741 return 0;
742
743 #ifdef AW1732_BT
744 free_bt_ext_wake:
745 gpio_free(bsi->ext_wake);
746 gpio_free(bsi->host_wake);
747 #endif
748
749 exit:
750 return ret;
751 }
752
bluesleep_remove(struct platform_device * pdev)753 static int bluesleep_remove(struct platform_device *pdev)
754 {
755 BT_SLEEP_DBG();
756 /* assert bt wake */
757 gpio_set_value(bsi->ext_wake, bsi->bt_wake_polarity);
758 if (test_bit(BT_PROTO, &flags)) {
759 #ifdef AW1732_BT
760 free_irq(bsi->host_wake_irq, &bsi->pdev->dev);
761 #endif
762 del_timer(&tx_timer);
763 if (test_bit(BT_ASLEEP, &flags))
764 hsuart_power(1);
765 }
766 gpio_free(bsi->host_wake);
767 gpio_free(bsi->ext_wake);
768 wakeup_source_unregister(bsi->ws);
769 if (bsi->wakeup_enable) {
770 BT_SLEEP_DBG("Deinit wakeup source");
771 device_init_wakeup(&pdev->dev, false);
772 dev_pm_clear_wake_irq(&pdev->dev);
773 }
774
775 return 0;
776 }
777
bluesleep_resume(struct platform_device * pdev)778 static int bluesleep_resume(struct platform_device *pdev)
779 {
780 BT_SLEEP_DBG();
781 if (!has_lpm_enabled) {
782 return 0;
783 }
784 #if 0 //fix bug for always wakeup
785 if (test_bit(BT_ASLEEP, &flags)) {
786 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL*HZ));
787 }
788 bluesleep_sleep_wakeup();
789 #endif
790 return 0;
791 }
792
bluesleep_suspend(struct platform_device * pdev,pm_message_t state)793 static int bluesleep_suspend(struct platform_device *pdev, pm_message_t state)
794 {
795 BT_SLEEP_DBG();
796 if (!has_lpm_enabled) {
797 return 0;
798 }
799 /*TXing DATA,if suspend will cause a error*/
800 if (test_bit(BT_TXDATA, &flags)) {
801 return -EBUSY;
802 }
803 del_timer(&tx_timer);
804 bluesleep_standby();
805 return 0;
806 }
807
808
809 static const struct of_device_id sunxi_btlpm_ids[] = {
810 { .compatible = "allwinner,sunxi-btlpm" },
811 { /* Sentinel */ }
812 };
813
814 static struct platform_driver bluesleep_driver = {
815 .probe = bluesleep_probe,
816 .remove = bluesleep_remove,
817 .suspend = bluesleep_suspend,
818 .resume = bluesleep_resume,
819 .driver = {
820 .owner = THIS_MODULE,
821 .name = "sunxi-btlpm",
822 .of_match_table = sunxi_btlpm_ids,
823 },
824 };
825
btsleep_init(void)826 static int __init btsleep_init(void)
827 {
828 int retval;
829 struct proc_dir_entry *ent;
830
831 BT_INFO("XRadio Bluetooth LPM Mode Driver Ver %s", VERSION);
832
833 retval = platform_driver_probe(&bluesleep_driver, bluesleep_probe);
834 if (retval)
835 goto fail;
836
837 bluetooth_dir = proc_mkdir("bluetooth", NULL);
838 if (bluetooth_dir == NULL) {
839 BT_ERR("Unable to create /proc/bluetooth directory");
840 goto unreg_drv;
841 }
842 sleep_dir = proc_mkdir("sleep", bluetooth_dir);
843 if (sleep_dir == NULL) {
844 BT_ERR("Unable to create /proc/%s directory", PROC_SLEEP_DIR);
845 goto rm_bt_dir;
846 }
847
848 #ifdef XRADIO_ETF_RFKILL
849 power_dir = proc_mkdir("power", bluetooth_dir);
850 if (power_dir == NULL) {
851 BT_ERR("Unable to create /proc/%s directory", PROC_POWER_DIR);
852 goto rm_bt_dir;
853 }
854 /* read/write proc entries */
855 ent = proc_create("state", 0220, power_dir, &pwr_fops);
856 if (ent == NULL) {
857 BT_ERR("Unable to create /proc/%s/state entry", PROC_POWER_DIR);
858 retval = -ENOMEM;
859 goto rm_power_dir;
860 }
861 #endif
862
863 /* read/write proc entries */
864 ent = proc_create("lpm", 0220, sleep_dir, &lpm_fops);
865 if (ent == NULL) {
866 BT_ERR("Unable to create /proc/%s/lpm entry", PROC_SLEEP_DIR);
867 retval = -ENOMEM;
868 goto rm_sleep_dir;
869 }
870 ent = proc_create("btwrite", 0220, sleep_dir, &btwrite_fops);
871 if (ent == NULL) {
872 BT_ERR("Unable to create /proc/%s/btwrite entry", PROC_SLEEP_DIR);
873 retval = -ENOMEM;
874 goto rm_btwrite_dir;
875 }
876 ent = proc_create("btwake", 0220, sleep_dir, &btwake_fops);
877 if (ent == NULL) {
878 BT_ERR("Unable to create /proc/%s/btwrite entry", PROC_SLEEP_DIR);
879 retval = -ENOMEM;
880 goto rm_btwake_dir;
881 }
882
883 #ifdef AW1732_BT
884 flags = 0;
885 /* Initialize spinlock. */
886 spin_lock_init(&rw_lock);
887 spin_lock_init(&irq_lock);
888 /* Initialize timer */
889 timer_setup(&tx_timer, bluesleep_tx_timer_expire, 0);
890 /* initialize host wake tasklet */
891 tasklet_init(&hostwake_task, bluesleep_hostwake_task, 0);
892 #endif
893
894 return 0;
895
896 rm_btwake_dir:
897 remove_proc_entry("btwake", sleep_dir);
898 rm_btwrite_dir:
899 remove_proc_entry("btwrite", sleep_dir);
900 rm_sleep_dir:
901 remove_proc_entry("sleep", bluetooth_dir);
902 #ifdef XRADIO_ETF_RFKILL
903 rm_power_dir:
904 remove_proc_entry("state", power_dir);
905 remove_proc_entry("power", bluetooth_dir);
906 #endif
907 rm_bt_dir:
908 remove_proc_entry("bluetooth", 0);
909 unreg_drv:
910 platform_driver_unregister(&bluesleep_driver);
911 fail:
912 return retval;
913 }
914
btsleep_exit(void)915 static void __exit btsleep_exit(void)
916 {
917 platform_driver_unregister(&bluesleep_driver);
918
919 remove_proc_entry("btwake", sleep_dir);
920 remove_proc_entry("btwrite", sleep_dir);
921 remove_proc_entry("lpm", sleep_dir);
922 remove_proc_entry("sleep", bluetooth_dir);
923 #ifdef XRADIO_ETF_RFKILL
924 remove_proc_entry("state", power_dir);
925 remove_proc_entry("power", bluetooth_dir);
926 #endif
927 remove_proc_entry("bluetooth", 0);
928 }
929
930 module_init(btsleep_init);
931 module_exit(btsleep_exit);
932 MODULE_AUTHOR("XRadioTech");
933 MODULE_DESCRIPTION("Bluetooth Sleep Mode Driver ver. %s" VERSION);
934 MODULE_LICENSE("GPL");
935