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);
788 dev_warn(info->dev, "bt_wakeup_test: fail: "
789 "CTS low timed out: %d\n", err);
793 gpio_set_value(info->bt_wakeup_gpio, 1);
794 err = hci_h4p_wait_for_cts(info, 1, 100);
796 dev_warn(info->dev, "bt_wakeup_test: fail: "
797 "CTS high timed out: %d\n", err);
801 gpio_set_value(info->bt_wakeup_gpio, 0);
802 err = hci_h4p_wait_for_cts(info, 0, 100);
804 dev_warn(info->dev, "bt_wakeup_test: fail: "
805 "CTS re-low timed out: %d\n", err);
813 /* Re-enable wakeup interrupts */
814 enable_irq(gpio_to_irq(info->host_wakeup_gpio));
819 static int hci_h4p_hci_open(struct hci_dev *hdev)
821 struct hci_h4p_info *info;
822 int err, retries = 0;
823 struct sk_buff_head fw_queue;
826 info = hci_get_drvdata(hdev);
828 if (test_bit(HCI_RUNNING, &hdev->flags))
831 /* TI1271 has HW bug and boot up might fail. Retry up to three times */
834 info->rx_enabled = 1;
835 info->rx_state = WAIT_FOR_PKT_TYPE;
837 info->garbage_bytes = 0;
839 info->pm_enabled = 0;
840 init_completion(&info->fw_completion);
841 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
842 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
843 skb_queue_head_init(&fw_queue);
845 err = hci_h4p_reset(info);
849 hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
852 err = hci_h4p_send_negotiation(info);
854 err = hci_h4p_read_fw(info, &fw_queue);
856 dev_err(info->dev, "Cannot read firmware\n");
860 err = hci_h4p_send_fw(info, &fw_queue);
862 dev_err(info->dev, "Sending firmware failed.\n");
866 info->pm_enabled = 1;
868 err = hci_h4p_bt_wakeup_test(info);
870 dev_err(info->dev, "BT wakeup test failed.\n");
874 spin_lock_irqsave(&info->lock, flags);
875 info->rx_enabled = gpio_get_value(info->host_wakeup_gpio);
876 hci_h4p_set_clk(info, &info->rx_clocks_en, info->rx_enabled);
877 spin_unlock_irqrestore(&info->lock, flags);
879 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
881 kfree_skb(info->alive_cmd_skb);
882 info->alive_cmd_skb = NULL;
883 set_bit(HCI_RUNNING, &hdev->flags);
885 BT_DBG("hci up and running");
889 hci_h4p_hci_flush(hdev);
890 hci_h4p_reset_uart(info);
891 del_timer_sync(&info->lazy_release);
892 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
893 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
894 gpio_set_value(info->reset_gpio, 0);
895 gpio_set_value(info->bt_wakeup_gpio, 0);
896 skb_queue_purge(&fw_queue);
897 kfree_skb(info->alive_cmd_skb);
898 info->alive_cmd_skb = NULL;
899 kfree_skb(info->rx_skb);
903 dev_err(info->dev, "FW loading try %d fail. Retry.\n", retries);
910 static int hci_h4p_hci_close(struct hci_dev *hdev)
912 struct hci_h4p_info *info = hci_get_drvdata(hdev);
914 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
917 hci_h4p_hci_flush(hdev);
918 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
919 hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
920 hci_h4p_reset_uart(info);
921 del_timer_sync(&info->lazy_release);
922 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
923 hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
924 gpio_set_value(info->reset_gpio, 0);
925 gpio_set_value(info->bt_wakeup_gpio, 0);
926 kfree_skb(info->rx_skb);
931 static int hci_h4p_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
933 struct hci_h4p_info *info;
936 BT_DBG("dev %p, skb %p", hdev, skb);
938 info = hci_get_drvdata(hdev);
940 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
941 dev_warn(info->dev, "Frame for non-running device\n");
945 switch (bt_cb(skb)->pkt_type) {
946 case HCI_COMMAND_PKT:
949 case HCI_ACLDATA_PKT:
952 case HCI_SCODATA_PKT:
957 /* Push frame type to skb */
958 *skb_push(skb, 1) = (bt_cb(skb)->pkt_type);
959 /* We should allways send word aligned data to h4+ devices */
961 err = skb_pad(skb, 1);
963 *skb_put(skb, 1) = 0x00;
968 skb_queue_tail(&info->txq, skb);
969 hci_h4p_enable_tx(info);
974 static ssize_t hci_h4p_store_bdaddr(struct device *dev,
975 struct device_attribute *attr,
976 const char *buf, size_t count)
978 struct hci_h4p_info *info = dev_get_drvdata(dev);
979 unsigned int bdaddr[6];
982 ret = sscanf(buf, "%2x:%2x:%2x:%2x:%2x:%2x\n",
983 &bdaddr[0], &bdaddr[1], &bdaddr[2],
984 &bdaddr[3], &bdaddr[4], &bdaddr[5]);
989 for (i = 0; i < 6; i++) {
990 if (bdaddr[i] > 0xff)
992 info->bd_addr[i] = bdaddr[i] & 0xff;
998 static ssize_t hci_h4p_show_bdaddr(struct device *dev,
999 struct device_attribute *attr, char *buf)
1001 struct hci_h4p_info *info = dev_get_drvdata(dev);
1003 return sprintf(buf, "%pMR\n", info->bd_addr);
1006 static DEVICE_ATTR(bdaddr, S_IRUGO | S_IWUSR, hci_h4p_show_bdaddr,
1007 hci_h4p_store_bdaddr);
1009 static int hci_h4p_sysfs_create_files(struct device *dev)
1011 return device_create_file(dev, &dev_attr_bdaddr);
1014 static void hci_h4p_sysfs_remove_files(struct device *dev)
1016 device_remove_file(dev, &dev_attr_bdaddr);
1019 static int hci_h4p_register_hdev(struct hci_h4p_info *info)
1021 struct hci_dev *hdev;
1023 /* Initialize and register HCI device */
1025 hdev = hci_alloc_dev();
1027 dev_err(info->dev, "Can't allocate memory for device\n");
1032 hdev->bus = HCI_UART;
1033 hci_set_drvdata(hdev, info);
1035 hdev->open = hci_h4p_hci_open;
1036 hdev->close = hci_h4p_hci_close;
1037 hdev->flush = hci_h4p_hci_flush;
1038 hdev->send = hci_h4p_hci_send_frame;
1039 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1041 SET_HCIDEV_DEV(hdev, info->dev);
1043 if (hci_h4p_sysfs_create_files(info->dev) < 0) {
1044 dev_err(info->dev, "failed to create sysfs files\n");
1048 if (hci_register_dev(hdev) >= 0)
1051 dev_err(info->dev, "hci_register failed %s.\n", hdev->name);
1052 hci_h4p_sysfs_remove_files(info->dev);
1054 hci_free_dev(info->hdev);
1058 static int hci_h4p_probe(struct platform_device *pdev)
1060 struct hci_h4p_platform_data *bt_plat_data;
1061 struct hci_h4p_info *info;
1064 dev_info(&pdev->dev, "Registering HCI H4P device\n");
1065 info = devm_kzalloc(&pdev->dev, sizeof(struct hci_h4p_info), GFP_KERNEL);
1069 info->dev = &pdev->dev;
1070 info->tx_enabled = 1;
1071 info->rx_enabled = 1;
1072 spin_lock_init(&info->lock);
1073 spin_lock_init(&info->clocks_lock);
1074 skb_queue_head_init(&info->txq);
1076 if (pdev->dev.platform_data == NULL) {
1077 dev_err(&pdev->dev, "Could not get Bluetooth config data\n");
1081 bt_plat_data = pdev->dev.platform_data;
1082 info->chip_type = bt_plat_data->chip_type;
1083 info->bt_wakeup_gpio = bt_plat_data->bt_wakeup_gpio;
1084 info->host_wakeup_gpio = bt_plat_data->host_wakeup_gpio;
1085 info->reset_gpio = bt_plat_data->reset_gpio;
1086 info->reset_gpio_shared = bt_plat_data->reset_gpio_shared;
1087 info->bt_sysclk = bt_plat_data->bt_sysclk;
1089 BT_DBG("RESET gpio: %d", info->reset_gpio);
1090 BT_DBG("BTWU gpio: %d", info->bt_wakeup_gpio);
1091 BT_DBG("HOSTWU gpio: %d", info->host_wakeup_gpio);
1092 BT_DBG("sysclk: %d", info->bt_sysclk);
1094 init_completion(&info->test_completion);
1095 complete_all(&info->test_completion);
1097 if (!info->reset_gpio_shared) {
1098 err = devm_gpio_request_one(&pdev->dev, info->reset_gpio,
1099 GPIOF_OUT_INIT_LOW, "bt_reset");
1101 dev_err(&pdev->dev, "Cannot get GPIO line %d\n",
1107 err = devm_gpio_request_one(&pdev->dev, info->bt_wakeup_gpio,
1108 GPIOF_OUT_INIT_LOW, "bt_wakeup");
1111 dev_err(info->dev, "Cannot get GPIO line 0x%d",
1112 info->bt_wakeup_gpio);
1116 err = devm_gpio_request_one(&pdev->dev, info->host_wakeup_gpio,
1117 GPIOF_DIR_IN, "host_wakeup");
1119 dev_err(info->dev, "Cannot get GPIO line %d",
1120 info->host_wakeup_gpio);
1124 info->irq = bt_plat_data->uart_irq;
1125 info->uart_base = devm_ioremap(&pdev->dev, bt_plat_data->uart_base, SZ_2K);
1126 info->uart_iclk = devm_clk_get(&pdev->dev, bt_plat_data->uart_iclk);
1127 info->uart_fclk = devm_clk_get(&pdev->dev, bt_plat_data->uart_fclk);
1129 err = devm_request_irq(&pdev->dev, info->irq, hci_h4p_interrupt, IRQF_DISABLED,
1132 dev_err(info->dev, "hci_h4p: unable to get IRQ %d\n", info->irq);
1136 err = devm_request_irq(&pdev->dev, gpio_to_irq(info->host_wakeup_gpio),
1137 hci_h4p_wakeup_interrupt, IRQF_TRIGGER_FALLING |
1138 IRQF_TRIGGER_RISING | IRQF_DISABLED,
1139 "hci_h4p_wkup", info);
1141 dev_err(info->dev, "hci_h4p: unable to get wakeup IRQ %d\n",
1142 gpio_to_irq(info->host_wakeup_gpio));
1146 err = irq_set_irq_wake(gpio_to_irq(info->host_wakeup_gpio), 1);
1148 dev_err(info->dev, "hci_h4p: unable to set wakeup for IRQ %d\n",
1149 gpio_to_irq(info->host_wakeup_gpio));
1153 init_timer_deferrable(&info->lazy_release);
1154 info->lazy_release.function = hci_h4p_lazy_clock_release;
1155 info->lazy_release.data = (unsigned long)info;
1156 hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
1157 err = hci_h4p_reset_uart(info);
1160 gpio_set_value(info->reset_gpio, 0);
1161 hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
1163 platform_set_drvdata(pdev, info);
1165 if (hci_h4p_register_hdev(info) < 0) {
1166 dev_err(info->dev, "failed to register hci_h4p hci device\n");
1173 static int hci_h4p_remove(struct platform_device *pdev)
1175 struct hci_h4p_info *info;
1177 info = platform_get_drvdata(pdev);
1179 hci_h4p_sysfs_remove_files(info->dev);
1180 hci_h4p_hci_close(info->hdev);
1181 hci_unregister_dev(info->hdev);
1182 hci_free_dev(info->hdev);
1187 static struct platform_driver hci_h4p_driver = {
1188 .probe = hci_h4p_probe,
1189 .remove = hci_h4p_remove,
1195 module_platform_driver(hci_h4p_driver);
1197 MODULE_ALIAS("platform:hci_h4p");
1198 MODULE_DESCRIPTION("Bluetooth h4 driver with nokia extensions");
1199 MODULE_LICENSE("GPL");
1200 MODULE_AUTHOR("Ville Tervo");
1201 MODULE_FIRMWARE(FW_NAME_TI1271_PRELE);
1202 MODULE_FIRMWARE(FW_NAME_TI1271_LE);
1203 MODULE_FIRMWARE(FW_NAME_TI1271);
1204 MODULE_FIRMWARE(FW_NAME_BCM2048);
1205 MODULE_FIRMWARE(FW_NAME_CSR);