2 * This file is part of Nokia H4P bluetooth driver
4 * Copyright (C) 2005-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * Thanks to all the Nokia people that helped with this driver,
21 * including Ville Tervo and Roger Quadros.
23 * Power saving functionality was removed from this driver to make
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/errno.h>
31 #include <linux/delay.h>
32 #include <linux/spinlock.h>
33 #include <linux/serial_reg.h>
34 #include <linux/skbuff.h>
35 #include <linux/device.h>
36 #include <linux/platform_device.h>
37 #include <linux/clk.h>
38 #include <linux/interrupt.h>
39 #include <linux/gpio.h>
40 #include <linux/timer.h>
41 #include <linux/kthread.h>
43 #include <linux/completion.h>
44 #include <linux/sizes.h>
46 #include <net/bluetooth/bluetooth.h>
47 #include <net/bluetooth/hci_core.h>
48 #include <net/bluetooth/hci.h>
50 #include <linux/platform_data/bt-nokia-h4p.h>
54 /* This should be used in function that cannot release clocks */
55 static void hci_h4p_set_clk(struct hci_h4p_info
*info
, int *clock
, int enable
)
59 spin_lock_irqsave(&info
->clocks_lock
, flags
);
60 if (enable
&& !*clock
) {
61 BT_DBG("Enabling %p", clock
);
62 clk_prepare_enable(info
->uart_fclk
);
63 clk_prepare_enable(info
->uart_iclk
);
64 if (atomic_read(&info
->clk_users
) == 0)
65 hci_h4p_restore_regs(info
);
66 atomic_inc(&info
->clk_users
);
69 if (!enable
&& *clock
) {
70 BT_DBG("Disabling %p", clock
);
71 if (atomic_dec_and_test(&info
->clk_users
))
72 hci_h4p_store_regs(info
);
73 clk_disable_unprepare(info
->uart_fclk
);
74 clk_disable_unprepare(info
->uart_iclk
);
78 spin_unlock_irqrestore(&info
->clocks_lock
, flags
);
81 static void hci_h4p_lazy_clock_release(unsigned long data
)
83 struct hci_h4p_info
*info
= (struct hci_h4p_info
*)data
;
86 spin_lock_irqsave(&info
->lock
, flags
);
87 if (!info
->tx_enabled
)
88 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 0);
89 spin_unlock_irqrestore(&info
->lock
, flags
);
92 /* Power management functions */
93 void hci_h4p_smart_idle(struct hci_h4p_info
*info
, bool enable
)
97 v
= hci_h4p_inb(info
, UART_OMAP_SYSC
);
98 v
&= ~(UART_OMAP_SYSC_IDLEMASK
);
101 v
|= UART_OMAP_SYSC_SMART_IDLE
;
103 v
|= UART_OMAP_SYSC_NO_IDLE
;
105 hci_h4p_outb(info
, UART_OMAP_SYSC
, v
);
108 static inline void h4p_schedule_pm(struct hci_h4p_info
*info
)
112 static void hci_h4p_disable_tx(struct hci_h4p_info
*info
)
114 if (!info
->pm_enabled
)
117 /* Re-enable smart-idle */
118 hci_h4p_smart_idle(info
, 1);
120 gpio_set_value(info
->bt_wakeup_gpio
, 0);
121 mod_timer(&info
->lazy_release
, jiffies
+ msecs_to_jiffies(100));
122 info
->tx_enabled
= 0;
125 void hci_h4p_enable_tx(struct hci_h4p_info
*info
)
129 if (!info
->pm_enabled
)
132 h4p_schedule_pm(info
);
134 spin_lock_irqsave(&info
->lock
, flags
);
135 del_timer(&info
->lazy_release
);
136 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 1);
137 info
->tx_enabled
= 1;
138 gpio_set_value(info
->bt_wakeup_gpio
, 1);
139 hci_h4p_outb(info
, UART_IER
, hci_h4p_inb(info
, UART_IER
) |
142 * Disable smart-idle as UART TX interrupts
143 * are not wake-up capable
145 hci_h4p_smart_idle(info
, 0);
147 spin_unlock_irqrestore(&info
->lock
, flags
);
150 static void hci_h4p_disable_rx(struct hci_h4p_info
*info
)
152 if (!info
->pm_enabled
)
155 info
->rx_enabled
= 0;
157 if (hci_h4p_inb(info
, UART_LSR
) & UART_LSR_DR
)
160 if (!(hci_h4p_inb(info
, UART_LSR
) & UART_LSR_TEMT
))
163 __hci_h4p_set_auto_ctsrts(info
, 0, UART_EFR_RTS
);
165 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 0);
168 static void hci_h4p_enable_rx(struct hci_h4p_info
*info
)
170 if (!info
->pm_enabled
)
173 h4p_schedule_pm(info
);
175 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 1);
176 info
->rx_enabled
= 1;
178 if (!(hci_h4p_inb(info
, UART_LSR
) & UART_LSR_TEMT
))
181 __hci_h4p_set_auto_ctsrts(info
, 1, UART_EFR_RTS
);
185 /* Negotiation functions */
186 int hci_h4p_send_alive_packet(struct hci_h4p_info
*info
)
188 struct hci_h4p_alive_hdr
*hdr
;
189 struct hci_h4p_alive_pkt
*pkt
;
194 BT_DBG("Sending alive packet");
196 len
= H4_TYPE_SIZE
+ sizeof(*hdr
) + sizeof(*pkt
);
197 skb
= bt_skb_alloc(len
, GFP_KERNEL
);
201 memset(skb
->data
, 0x00, len
);
202 *skb_put(skb
, 1) = H4_ALIVE_PKT
;
203 hdr
= (struct hci_h4p_alive_hdr
*)skb_put(skb
, sizeof(*hdr
));
204 hdr
->dlen
= sizeof(*pkt
);
205 pkt
= (struct hci_h4p_alive_pkt
*)skb_put(skb
, sizeof(*pkt
));
206 pkt
->mid
= H4P_ALIVE_REQ
;
208 skb_queue_tail(&info
->txq
, skb
);
209 spin_lock_irqsave(&info
->lock
, flags
);
210 hci_h4p_outb(info
, UART_IER
, hci_h4p_inb(info
, UART_IER
) |
212 spin_unlock_irqrestore(&info
->lock
, flags
);
214 BT_DBG("Alive packet sent");
219 static void hci_h4p_alive_packet(struct hci_h4p_info
*info
,
222 struct hci_h4p_alive_hdr
*hdr
;
223 struct hci_h4p_alive_pkt
*pkt
;
225 BT_DBG("Received alive packet");
226 hdr
= (struct hci_h4p_alive_hdr
*)skb
->data
;
227 if (hdr
->dlen
!= sizeof(*pkt
)) {
228 dev_err(info
->dev
, "Corrupted alive message\n");
229 info
->init_error
= -EIO
;
233 pkt
= (struct hci_h4p_alive_pkt
*)skb_pull(skb
, sizeof(*hdr
));
234 if (pkt
->mid
!= H4P_ALIVE_RESP
) {
235 dev_err(info
->dev
, "Could not negotiate hci_h4p settings\n");
236 info
->init_error
= -EINVAL
;
240 complete(&info
->init_completion
);
244 static int hci_h4p_send_negotiation(struct hci_h4p_info
*info
)
246 struct hci_h4p_neg_cmd
*neg_cmd
;
247 struct hci_h4p_neg_hdr
*neg_hdr
;
253 BT_DBG("Sending negotiation..");
255 switch (info
->bt_sysclk
) {
266 len
= sizeof(*neg_cmd
) + sizeof(*neg_hdr
) + H4_TYPE_SIZE
;
267 skb
= bt_skb_alloc(len
, GFP_KERNEL
);
271 memset(skb
->data
, 0x00, len
);
272 *skb_put(skb
, 1) = H4_NEG_PKT
;
273 neg_hdr
= (struct hci_h4p_neg_hdr
*)skb_put(skb
, sizeof(*neg_hdr
));
274 neg_cmd
= (struct hci_h4p_neg_cmd
*)skb_put(skb
, sizeof(*neg_cmd
));
276 neg_hdr
->dlen
= sizeof(*neg_cmd
);
277 neg_cmd
->ack
= H4P_NEG_REQ
;
278 neg_cmd
->baud
= cpu_to_le16(BT_BAUDRATE_DIVIDER
/MAX_BAUD_RATE
);
279 neg_cmd
->proto
= H4P_PROTO_BYTE
;
280 neg_cmd
->sys_clk
= cpu_to_le16(sysclk
);
282 hci_h4p_change_speed(info
, INIT_SPEED
);
284 hci_h4p_set_rts(info
, 1);
285 info
->init_error
= 0;
286 init_completion(&info
->init_completion
);
287 skb_queue_tail(&info
->txq
, skb
);
288 spin_lock_irqsave(&info
->lock
, flags
);
289 hci_h4p_outb(info
, UART_IER
, hci_h4p_inb(info
, UART_IER
) |
291 spin_unlock_irqrestore(&info
->lock
, flags
);
293 if (!wait_for_completion_interruptible_timeout(&info
->init_completion
,
294 msecs_to_jiffies(1000)))
297 if (info
->init_error
< 0)
298 return info
->init_error
;
300 /* Change to operational settings */
301 hci_h4p_set_auto_ctsrts(info
, 0, UART_EFR_RTS
);
302 hci_h4p_set_rts(info
, 0);
303 hci_h4p_change_speed(info
, MAX_BAUD_RATE
);
305 err
= hci_h4p_wait_for_cts(info
, 1, 100);
309 hci_h4p_set_auto_ctsrts(info
, 1, UART_EFR_RTS
);
310 init_completion(&info
->init_completion
);
311 err
= hci_h4p_send_alive_packet(info
);
316 if (!wait_for_completion_interruptible_timeout(&info
->init_completion
,
317 msecs_to_jiffies(1000)))
320 if (info
->init_error
< 0)
321 return info
->init_error
;
323 BT_DBG("Negotiation successful");
327 static void hci_h4p_negotiation_packet(struct hci_h4p_info
*info
,
330 struct hci_h4p_neg_hdr
*hdr
;
331 struct hci_h4p_neg_evt
*evt
;
333 hdr
= (struct hci_h4p_neg_hdr
*)skb
->data
;
334 if (hdr
->dlen
!= sizeof(*evt
)) {
335 info
->init_error
= -EIO
;
339 evt
= (struct hci_h4p_neg_evt
*)skb_pull(skb
, sizeof(*hdr
));
341 if (evt
->ack
!= H4P_NEG_ACK
) {
342 dev_err(info
->dev
, "Could not negotiate hci_h4p settings\n");
343 info
->init_error
= -EINVAL
;
346 info
->man_id
= evt
->man_id
;
347 info
->ver_id
= evt
->ver_id
;
351 complete(&info
->init_completion
);
355 /* H4 packet handling functions */
356 static int hci_h4p_get_hdr_len(struct hci_h4p_info
*info
, u8 pkt_type
)
362 retval
= HCI_EVENT_HDR_SIZE
;
365 retval
= HCI_ACL_HDR_SIZE
;
368 retval
= HCI_SCO_HDR_SIZE
;
371 retval
= H4P_NEG_HDR_SIZE
;
374 retval
= H4P_ALIVE_HDR_SIZE
;
377 retval
= H4_RADIO_HDR_SIZE
;
380 dev_err(info
->dev
, "Unknown H4 packet type 0x%.2x\n", pkt_type
);
388 static unsigned int hci_h4p_get_data_len(struct hci_h4p_info
*info
,
392 struct hci_acl_hdr
*acl_hdr
;
393 struct hci_sco_hdr
*sco_hdr
;
394 struct hci_event_hdr
*evt_hdr
;
395 struct hci_h4p_neg_hdr
*neg_hdr
;
396 struct hci_h4p_alive_hdr
*alive_hdr
;
397 struct hci_h4p_radio_hdr
*radio_hdr
;
399 switch (bt_cb(skb
)->pkt_type
) {
401 evt_hdr
= (struct hci_event_hdr
*)skb
->data
;
402 retval
= evt_hdr
->plen
;
405 acl_hdr
= (struct hci_acl_hdr
*)skb
->data
;
406 retval
= le16_to_cpu(acl_hdr
->dlen
);
409 sco_hdr
= (struct hci_sco_hdr
*)skb
->data
;
410 retval
= sco_hdr
->dlen
;
413 radio_hdr
= (struct hci_h4p_radio_hdr
*)skb
->data
;
414 retval
= radio_hdr
->dlen
;
417 neg_hdr
= (struct hci_h4p_neg_hdr
*)skb
->data
;
418 retval
= neg_hdr
->dlen
;
421 alive_hdr
= (struct hci_h4p_alive_hdr
*)skb
->data
;
422 retval
= alive_hdr
->dlen
;
429 static inline void hci_h4p_recv_frame(struct hci_h4p_info
*info
,
432 if (unlikely(!test_bit(HCI_RUNNING
, &info
->hdev
->flags
))) {
433 switch (bt_cb(skb
)->pkt_type
) {
435 hci_h4p_negotiation_packet(info
, skb
);
436 info
->rx_state
= WAIT_FOR_PKT_TYPE
;
439 hci_h4p_alive_packet(info
, skb
);
440 info
->rx_state
= WAIT_FOR_PKT_TYPE
;
444 if (!test_bit(HCI_UP
, &info
->hdev
->flags
)) {
446 hci_h4p_parse_fw_event(info
, skb
);
451 hci_recv_frame(info
->hdev
, skb
);
452 BT_DBG("Frame sent to upper layer");
455 static inline void hci_h4p_handle_byte(struct hci_h4p_info
*info
, u8 byte
)
457 switch (info
->rx_state
) {
458 case WAIT_FOR_PKT_TYPE
:
459 bt_cb(info
->rx_skb
)->pkt_type
= byte
;
460 info
->rx_count
= hci_h4p_get_hdr_len(info
, byte
);
461 if (info
->rx_count
< 0) {
462 info
->hdev
->stat
.err_rx
++;
463 kfree_skb(info
->rx_skb
);
466 info
->rx_state
= WAIT_FOR_HEADER
;
469 case WAIT_FOR_HEADER
:
471 *skb_put(info
->rx_skb
, 1) = byte
;
472 if (info
->rx_count
!= 0)
474 info
->rx_count
= hci_h4p_get_data_len(info
, info
->rx_skb
);
475 if (info
->rx_count
> skb_tailroom(info
->rx_skb
)) {
476 dev_err(info
->dev
, "frame too long\n");
477 info
->garbage_bytes
= info
->rx_count
478 - skb_tailroom(info
->rx_skb
);
479 kfree_skb(info
->rx_skb
);
483 info
->rx_state
= WAIT_FOR_DATA
;
487 *skb_put(info
->rx_skb
, 1) = byte
;
494 if (info
->rx_count
== 0) {
495 /* H4+ devices should always send word aligned packets */
496 if (!(info
->rx_skb
->len
% 2))
497 info
->garbage_bytes
++;
498 hci_h4p_recv_frame(info
, info
->rx_skb
);
503 static void hci_h4p_rx_tasklet(unsigned long data
)
506 struct hci_h4p_info
*info
= (struct hci_h4p_info
*)data
;
508 BT_DBG("tasklet woke up");
509 BT_DBG("rx_tasklet woke up");
511 while (hci_h4p_inb(info
, UART_LSR
) & UART_LSR_DR
) {
512 byte
= hci_h4p_inb(info
, UART_RX
);
513 if (info
->garbage_bytes
) {
514 info
->garbage_bytes
--;
517 if (info
->rx_skb
== NULL
) {
518 info
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
,
519 GFP_ATOMIC
| GFP_DMA
);
522 "No memory for new packet\n");
525 info
->rx_state
= WAIT_FOR_PKT_TYPE
;
526 info
->rx_skb
->dev
= (void *)info
->hdev
;
528 info
->hdev
->stat
.byte_rx
++;
529 hci_h4p_handle_byte(info
, byte
);
532 if (!info
->rx_enabled
) {
533 if (hci_h4p_inb(info
, UART_LSR
) & UART_LSR_TEMT
&&
535 __hci_h4p_set_auto_ctsrts(info
, 0 , UART_EFR_RTS
);
538 /* Flush posted write to avoid spurious interrupts */
539 hci_h4p_inb(info
, UART_OMAP_SCR
);
540 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 0);
547 static void hci_h4p_tx_tasklet(unsigned long data
)
549 unsigned int sent
= 0;
551 struct hci_h4p_info
*info
= (struct hci_h4p_info
*)data
;
553 BT_DBG("tasklet woke up");
554 BT_DBG("tx_tasklet woke up");
556 if (info
->autorts
!= info
->rx_enabled
) {
557 if (hci_h4p_inb(info
, UART_LSR
) & UART_LSR_TEMT
) {
558 if (info
->autorts
&& !info
->rx_enabled
) {
559 __hci_h4p_set_auto_ctsrts(info
, 0,
563 if (!info
->autorts
&& info
->rx_enabled
) {
564 __hci_h4p_set_auto_ctsrts(info
, 1,
569 hci_h4p_outb(info
, UART_OMAP_SCR
,
570 hci_h4p_inb(info
, UART_OMAP_SCR
) |
571 UART_OMAP_SCR_EMPTY_THR
);
576 skb
= skb_dequeue(&info
->txq
);
578 /* No data in buffer */
580 if (hci_h4p_inb(info
, UART_LSR
) & UART_LSR_TEMT
) {
581 hci_h4p_outb(info
, UART_IER
,
582 hci_h4p_inb(info
, UART_IER
) &
584 hci_h4p_inb(info
, UART_OMAP_SCR
);
585 hci_h4p_disable_tx(info
);
588 hci_h4p_outb(info
, UART_OMAP_SCR
,
589 hci_h4p_inb(info
, UART_OMAP_SCR
) |
590 UART_OMAP_SCR_EMPTY_THR
);
594 /* Copy data to tx fifo */
595 while (!(hci_h4p_inb(info
, UART_OMAP_SSR
) & UART_OMAP_SSR_TXFULL
) &&
597 hci_h4p_outb(info
, UART_TX
, skb
->data
[sent
]);
601 info
->hdev
->stat
.byte_tx
+= sent
;
602 if (skb
->len
== sent
) {
606 skb_queue_head(&info
->txq
, skb
);
609 hci_h4p_outb(info
, UART_OMAP_SCR
, hci_h4p_inb(info
, UART_OMAP_SCR
) &
610 ~UART_OMAP_SCR_EMPTY_THR
);
611 hci_h4p_outb(info
, UART_IER
, hci_h4p_inb(info
, UART_IER
) |
615 /* Flush posted write to avoid spurious interrupts */
616 hci_h4p_inb(info
, UART_OMAP_SCR
);
620 static irqreturn_t
hci_h4p_interrupt(int irq
, void *data
)
622 struct hci_h4p_info
*info
= (struct hci_h4p_info
*)data
;
628 iir
= hci_h4p_inb(info
, UART_IIR
);
629 if (iir
& UART_IIR_NO_INT
)
632 BT_DBG("In interrupt handler iir 0x%.2x", iir
);
636 if (iir
== UART_IIR_MSI
) {
637 msr
= hci_h4p_inb(info
, UART_MSR
);
640 if (iir
== UART_IIR_RLSI
) {
641 hci_h4p_inb(info
, UART_RX
);
642 hci_h4p_inb(info
, UART_LSR
);
646 if (iir
== UART_IIR_RDI
) {
647 hci_h4p_rx_tasklet((unsigned long)data
);
651 if (iir
== UART_IIR_THRI
) {
652 hci_h4p_tx_tasklet((unsigned long)data
);
659 static irqreturn_t
hci_h4p_wakeup_interrupt(int irq
, void *dev_inst
)
661 struct hci_h4p_info
*info
= dev_inst
;
663 struct hci_dev
*hdev
;
668 should_wakeup
= gpio_get_value(info
->host_wakeup_gpio
);
671 if (!test_bit(HCI_RUNNING
, &hdev
->flags
)) {
672 if (should_wakeup
== 1)
673 complete_all(&info
->test_completion
);
678 BT_DBG("gpio interrupt %d", should_wakeup
);
680 /* Check if wee have missed some interrupts */
681 if (info
->rx_enabled
== should_wakeup
)
685 hci_h4p_enable_rx(info
);
687 hci_h4p_disable_rx(info
);
692 static inline void hci_h4p_set_pm_limits(struct hci_h4p_info
*info
, bool set
)
694 struct hci_h4p_platform_data
*bt_plat_data
= info
->dev
->platform_data
;
695 const char *sset
= set
? "set" : "clear";
697 if (unlikely(!bt_plat_data
|| !bt_plat_data
->set_pm_limits
))
700 if (set
!= !!test_bit(H4P_ACTIVE_MODE
, &info
->pm_flags
)) {
701 bt_plat_data
->set_pm_limits(info
->dev
, set
);
703 set_bit(H4P_ACTIVE_MODE
, &info
->pm_flags
);
705 clear_bit(H4P_ACTIVE_MODE
, &info
->pm_flags
);
706 BT_DBG("Change pm constraints to: %s", sset
);
710 BT_DBG("pm constraints remains: %s", sset
);
713 static int hci_h4p_reset(struct hci_h4p_info
*info
)
717 err
= hci_h4p_reset_uart(info
);
719 dev_err(info
->dev
, "Uart reset failed\n");
722 hci_h4p_init_uart(info
);
723 hci_h4p_set_rts(info
, 0);
725 gpio_set_value(info
->reset_gpio
, 0);
726 gpio_set_value(info
->bt_wakeup_gpio
, 1);
729 if (gpio_get_value(info
->host_wakeup_gpio
) == 1) {
730 dev_err(info
->dev
, "host_wakeup_gpio not low\n");
734 init_completion(&info
->test_completion
);
735 gpio_set_value(info
->reset_gpio
, 1);
737 if (!wait_for_completion_interruptible_timeout(&info
->test_completion
,
738 msecs_to_jiffies(100))) {
739 dev_err(info
->dev
, "wakeup test timed out\n");
740 complete_all(&info
->test_completion
);
744 err
= hci_h4p_wait_for_cts(info
, 1, 100);
746 dev_err(info
->dev
, "No cts from bt chip\n");
750 hci_h4p_set_rts(info
, 1);
755 /* hci callback functions */
756 static int hci_h4p_hci_flush(struct hci_dev
*hdev
)
758 struct hci_h4p_info
*info
= hci_get_drvdata(hdev
);
759 skb_queue_purge(&info
->txq
);
764 static int hci_h4p_bt_wakeup_test(struct hci_h4p_info
*info
)
768 * Host de-asserts the BT_WAKE_UP line.
769 * Host polls the UART_CTS line, waiting for it to be de-asserted.
770 * Host asserts the BT_WAKE_UP line.
771 * Host polls the UART_CTS line, waiting for it to be asserted.
772 * Host de-asserts the BT_WAKE_UP line (allow the Bluetooth device to
774 * Host polls the UART_CTS line, waiting for it to be de-asserted.
782 /* Disable wakeup interrupts */
783 disable_irq(gpio_to_irq(info
->host_wakeup_gpio
));
785 gpio_set_value(info
->bt_wakeup_gpio
, 0);
786 err
= hci_h4p_wait_for_cts(info
, 0, 100);
789 "bt_wakeup_test: fail: CTS low timed out: %d\n",
794 gpio_set_value(info
->bt_wakeup_gpio
, 1);
795 err
= hci_h4p_wait_for_cts(info
, 1, 100);
798 "bt_wakeup_test: fail: CTS high timed out: %d\n",
803 gpio_set_value(info
->bt_wakeup_gpio
, 0);
804 err
= hci_h4p_wait_for_cts(info
, 0, 100);
807 "bt_wakeup_test: fail: CTS re-low timed out: %d\n",
816 /* Re-enable wakeup interrupts */
817 enable_irq(gpio_to_irq(info
->host_wakeup_gpio
));
822 static int hci_h4p_hci_open(struct hci_dev
*hdev
)
824 struct hci_h4p_info
*info
;
825 int err
, retries
= 0;
826 struct sk_buff_head fw_queue
;
829 info
= hci_get_drvdata(hdev
);
831 if (test_bit(HCI_RUNNING
, &hdev
->flags
))
834 /* TI1271 has HW bug and boot up might fail. Retry up to three times */
837 info
->rx_enabled
= 1;
838 info
->rx_state
= WAIT_FOR_PKT_TYPE
;
840 info
->garbage_bytes
= 0;
842 info
->pm_enabled
= 0;
843 init_completion(&info
->fw_completion
);
844 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 1);
845 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 1);
846 skb_queue_head_init(&fw_queue
);
848 err
= hci_h4p_reset(info
);
852 hci_h4p_set_auto_ctsrts(info
, 1, UART_EFR_CTS
| UART_EFR_RTS
);
855 err
= hci_h4p_send_negotiation(info
);
857 err
= hci_h4p_read_fw(info
, &fw_queue
);
859 dev_err(info
->dev
, "Cannot read firmware\n");
863 err
= hci_h4p_send_fw(info
, &fw_queue
);
865 dev_err(info
->dev
, "Sending firmware failed.\n");
869 info
->pm_enabled
= 1;
871 err
= hci_h4p_bt_wakeup_test(info
);
873 dev_err(info
->dev
, "BT wakeup test failed.\n");
877 spin_lock_irqsave(&info
->lock
, flags
);
878 info
->rx_enabled
= gpio_get_value(info
->host_wakeup_gpio
);
879 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, info
->rx_enabled
);
880 spin_unlock_irqrestore(&info
->lock
, flags
);
882 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 0);
884 kfree_skb(info
->alive_cmd_skb
);
885 info
->alive_cmd_skb
= NULL
;
886 set_bit(HCI_RUNNING
, &hdev
->flags
);
888 BT_DBG("hci up and running");
892 hci_h4p_hci_flush(hdev
);
893 hci_h4p_reset_uart(info
);
894 del_timer_sync(&info
->lazy_release
);
895 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 0);
896 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 0);
897 gpio_set_value(info
->reset_gpio
, 0);
898 gpio_set_value(info
->bt_wakeup_gpio
, 0);
899 skb_queue_purge(&fw_queue
);
900 kfree_skb(info
->alive_cmd_skb
);
901 info
->alive_cmd_skb
= NULL
;
902 kfree_skb(info
->rx_skb
);
906 dev_err(info
->dev
, "FW loading try %d fail. Retry.\n", retries
);
913 static int hci_h4p_hci_close(struct hci_dev
*hdev
)
915 struct hci_h4p_info
*info
= hci_get_drvdata(hdev
);
917 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
920 hci_h4p_hci_flush(hdev
);
921 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 1);
922 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 1);
923 hci_h4p_reset_uart(info
);
924 del_timer_sync(&info
->lazy_release
);
925 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 0);
926 hci_h4p_set_clk(info
, &info
->rx_clocks_en
, 0);
927 gpio_set_value(info
->reset_gpio
, 0);
928 gpio_set_value(info
->bt_wakeup_gpio
, 0);
929 kfree_skb(info
->rx_skb
);
934 static int hci_h4p_hci_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
936 struct hci_h4p_info
*info
;
939 BT_DBG("dev %p, skb %p", hdev
, skb
);
941 info
= hci_get_drvdata(hdev
);
943 if (!test_bit(HCI_RUNNING
, &hdev
->flags
)) {
944 dev_warn(info
->dev
, "Frame for non-running device\n");
948 switch (bt_cb(skb
)->pkt_type
) {
949 case HCI_COMMAND_PKT
:
952 case HCI_ACLDATA_PKT
:
955 case HCI_SCODATA_PKT
:
960 /* Push frame type to skb */
961 *skb_push(skb
, 1) = (bt_cb(skb
)->pkt_type
);
962 /* We should allways send word aligned data to h4+ devices */
964 err
= skb_pad(skb
, 1);
966 *skb_put(skb
, 1) = 0x00;
971 skb_queue_tail(&info
->txq
, skb
);
972 hci_h4p_enable_tx(info
);
977 static ssize_t
hci_h4p_store_bdaddr(struct device
*dev
,
978 struct device_attribute
*attr
,
979 const char *buf
, size_t count
)
981 struct hci_h4p_info
*info
= dev_get_drvdata(dev
);
982 unsigned int bdaddr
[6];
985 ret
= sscanf(buf
, "%2x:%2x:%2x:%2x:%2x:%2x\n",
986 &bdaddr
[0], &bdaddr
[1], &bdaddr
[2],
987 &bdaddr
[3], &bdaddr
[4], &bdaddr
[5]);
992 for (i
= 0; i
< 6; i
++) {
993 if (bdaddr
[i
] > 0xff)
995 info
->bd_addr
[i
] = bdaddr
[i
] & 0xff;
1001 static ssize_t
hci_h4p_show_bdaddr(struct device
*dev
,
1002 struct device_attribute
*attr
, char *buf
)
1004 struct hci_h4p_info
*info
= dev_get_drvdata(dev
);
1006 return sprintf(buf
, "%pMR\n", info
->bd_addr
);
1009 static DEVICE_ATTR(bdaddr
, S_IRUGO
| S_IWUSR
, hci_h4p_show_bdaddr
,
1010 hci_h4p_store_bdaddr
);
1012 static int hci_h4p_sysfs_create_files(struct device
*dev
)
1014 return device_create_file(dev
, &dev_attr_bdaddr
);
1017 static void hci_h4p_sysfs_remove_files(struct device
*dev
)
1019 device_remove_file(dev
, &dev_attr_bdaddr
);
1022 static int hci_h4p_register_hdev(struct hci_h4p_info
*info
)
1024 struct hci_dev
*hdev
;
1026 /* Initialize and register HCI device */
1028 hdev
= hci_alloc_dev();
1030 dev_err(info
->dev
, "Can't allocate memory for device\n");
1035 hdev
->bus
= HCI_UART
;
1036 hci_set_drvdata(hdev
, info
);
1038 hdev
->open
= hci_h4p_hci_open
;
1039 hdev
->close
= hci_h4p_hci_close
;
1040 hdev
->flush
= hci_h4p_hci_flush
;
1041 hdev
->send
= hci_h4p_hci_send_frame
;
1042 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
1044 SET_HCIDEV_DEV(hdev
, info
->dev
);
1046 if (hci_h4p_sysfs_create_files(info
->dev
) < 0) {
1047 dev_err(info
->dev
, "failed to create sysfs files\n");
1051 if (hci_register_dev(hdev
) >= 0)
1054 dev_err(info
->dev
, "hci_register failed %s.\n", hdev
->name
);
1055 hci_h4p_sysfs_remove_files(info
->dev
);
1057 hci_free_dev(info
->hdev
);
1061 static int hci_h4p_probe(struct platform_device
*pdev
)
1063 struct hci_h4p_platform_data
*bt_plat_data
;
1064 struct hci_h4p_info
*info
;
1067 dev_info(&pdev
->dev
, "Registering HCI H4P device\n");
1068 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct hci_h4p_info
),
1073 info
->dev
= &pdev
->dev
;
1074 info
->tx_enabled
= 1;
1075 info
->rx_enabled
= 1;
1076 spin_lock_init(&info
->lock
);
1077 spin_lock_init(&info
->clocks_lock
);
1078 skb_queue_head_init(&info
->txq
);
1080 if (pdev
->dev
.platform_data
== NULL
) {
1081 dev_err(&pdev
->dev
, "Could not get Bluetooth config data\n");
1085 bt_plat_data
= pdev
->dev
.platform_data
;
1086 info
->chip_type
= bt_plat_data
->chip_type
;
1087 info
->bt_wakeup_gpio
= bt_plat_data
->bt_wakeup_gpio
;
1088 info
->host_wakeup_gpio
= bt_plat_data
->host_wakeup_gpio
;
1089 info
->reset_gpio
= bt_plat_data
->reset_gpio
;
1090 info
->reset_gpio_shared
= bt_plat_data
->reset_gpio_shared
;
1091 info
->bt_sysclk
= bt_plat_data
->bt_sysclk
;
1093 BT_DBG("RESET gpio: %d", info
->reset_gpio
);
1094 BT_DBG("BTWU gpio: %d", info
->bt_wakeup_gpio
);
1095 BT_DBG("HOSTWU gpio: %d", info
->host_wakeup_gpio
);
1096 BT_DBG("sysclk: %d", info
->bt_sysclk
);
1098 init_completion(&info
->test_completion
);
1099 complete_all(&info
->test_completion
);
1101 if (!info
->reset_gpio_shared
) {
1102 err
= devm_gpio_request_one(&pdev
->dev
, info
->reset_gpio
,
1103 GPIOF_OUT_INIT_LOW
, "bt_reset");
1105 dev_err(&pdev
->dev
, "Cannot get GPIO line %d\n",
1111 err
= devm_gpio_request_one(&pdev
->dev
, info
->bt_wakeup_gpio
,
1112 GPIOF_OUT_INIT_LOW
, "bt_wakeup");
1115 dev_err(info
->dev
, "Cannot get GPIO line 0x%d",
1116 info
->bt_wakeup_gpio
);
1120 err
= devm_gpio_request_one(&pdev
->dev
, info
->host_wakeup_gpio
,
1121 GPIOF_DIR_IN
, "host_wakeup");
1123 dev_err(info
->dev
, "Cannot get GPIO line %d",
1124 info
->host_wakeup_gpio
);
1128 info
->irq
= bt_plat_data
->uart_irq
;
1129 info
->uart_base
= devm_ioremap(&pdev
->dev
, bt_plat_data
->uart_base
,
1131 info
->uart_iclk
= devm_clk_get(&pdev
->dev
, bt_plat_data
->uart_iclk
);
1132 info
->uart_fclk
= devm_clk_get(&pdev
->dev
, bt_plat_data
->uart_fclk
);
1134 err
= devm_request_irq(&pdev
->dev
, info
->irq
, hci_h4p_interrupt
,
1135 IRQF_DISABLED
, "hci_h4p", info
);
1137 dev_err(info
->dev
, "hci_h4p: unable to get IRQ %d\n",
1142 err
= devm_request_irq(&pdev
->dev
, gpio_to_irq(info
->host_wakeup_gpio
),
1143 hci_h4p_wakeup_interrupt
, IRQF_TRIGGER_FALLING
|
1144 IRQF_TRIGGER_RISING
| IRQF_DISABLED
,
1145 "hci_h4p_wkup", info
);
1147 dev_err(info
->dev
, "hci_h4p: unable to get wakeup IRQ %d\n",
1148 gpio_to_irq(info
->host_wakeup_gpio
));
1152 err
= irq_set_irq_wake(gpio_to_irq(info
->host_wakeup_gpio
), 1);
1154 dev_err(info
->dev
, "hci_h4p: unable to set wakeup for IRQ %d\n",
1155 gpio_to_irq(info
->host_wakeup_gpio
));
1159 init_timer_deferrable(&info
->lazy_release
);
1160 info
->lazy_release
.function
= hci_h4p_lazy_clock_release
;
1161 info
->lazy_release
.data
= (unsigned long)info
;
1162 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 1);
1163 err
= hci_h4p_reset_uart(info
);
1166 gpio_set_value(info
->reset_gpio
, 0);
1167 hci_h4p_set_clk(info
, &info
->tx_clocks_en
, 0);
1169 platform_set_drvdata(pdev
, info
);
1171 if (hci_h4p_register_hdev(info
) < 0) {
1172 dev_err(info
->dev
, "failed to register hci_h4p hci device\n");
1179 static int hci_h4p_remove(struct platform_device
*pdev
)
1181 struct hci_h4p_info
*info
;
1183 info
= platform_get_drvdata(pdev
);
1185 hci_h4p_sysfs_remove_files(info
->dev
);
1186 hci_h4p_hci_close(info
->hdev
);
1187 hci_unregister_dev(info
->hdev
);
1188 hci_free_dev(info
->hdev
);
1193 static struct platform_driver hci_h4p_driver
= {
1194 .probe
= hci_h4p_probe
,
1195 .remove
= hci_h4p_remove
,
1201 module_platform_driver(hci_h4p_driver
);
1203 MODULE_ALIAS("platform:hci_h4p");
1204 MODULE_DESCRIPTION("Bluetooth h4 driver with nokia extensions");
1205 MODULE_LICENSE("GPL");
1206 MODULE_AUTHOR("Ville Tervo");