Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Bluetooth Software UART Qualcomm protocol |
| 4 | * |
| 5 | * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management |
| 6 | * protocol extension to H4. |
| 7 | * |
| 8 | * Copyright (C) 2007 Texas Instruments, Inc. |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 9 | * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 10 | * |
| 11 | * Acknowledgements: |
| 12 | * This file is based on hci_ll.c, which was... |
| 13 | * Written by Ohad Ben-Cohen <ohad@bencohen.org> |
| 14 | * which was in turn based on hci_h4.c, which was written |
| 15 | * by Maxim Krasnyansky and Marcel Holtmann. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 19 | #include <linux/clk.h> |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 20 | #include <linux/completion.h> |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 22 | #include <linux/delay.h> |
| 23 | #include <linux/device.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 24 | #include <linux/gpio/consumer.h> |
| 25 | #include <linux/mod_devicetable.h> |
| 26 | #include <linux/module.h> |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 27 | #include <linux/of_device.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/regulator/consumer.h> |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 30 | #include <linux/serdev.h> |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 31 | #include <asm/unaligned.h> |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 32 | |
| 33 | #include <net/bluetooth/bluetooth.h> |
| 34 | #include <net/bluetooth/hci_core.h> |
| 35 | |
| 36 | #include "hci_uart.h" |
| 37 | #include "btqca.h" |
| 38 | |
| 39 | /* HCI_IBS protocol messages */ |
| 40 | #define HCI_IBS_SLEEP_IND 0xFE |
| 41 | #define HCI_IBS_WAKE_IND 0xFD |
| 42 | #define HCI_IBS_WAKE_ACK 0xFC |
Marcel Holtmann | f81b001 | 2015-08-30 23:05:32 +0200 | [diff] [blame] | 43 | #define HCI_MAX_IBS_SIZE 10 |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 44 | |
Marcel Holtmann | f81b001 | 2015-08-30 23:05:32 +0200 | [diff] [blame] | 45 | #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 46 | #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40 |
| 47 | #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000 |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 48 | #define CMD_TRANS_TIMEOUT_MS 100 |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 49 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 50 | /* susclk rate */ |
| 51 | #define SUSCLK_RATE_32KHZ 32768 |
| 52 | |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 53 | /* Controller debug log header */ |
| 54 | #define QCA_DEBUG_HANDLE 0x2EDC |
| 55 | |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 56 | enum qca_flags { |
| 57 | QCA_IBS_ENABLED, |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 58 | QCA_DROP_VENDOR_EVENT, |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 59 | QCA_SUSPENDING, |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 62 | /* HCI_IBS transmit side sleep protocol states */ |
| 63 | enum tx_ibs_states { |
| 64 | HCI_IBS_TX_ASLEEP, |
| 65 | HCI_IBS_TX_WAKING, |
| 66 | HCI_IBS_TX_AWAKE, |
| 67 | }; |
| 68 | |
| 69 | /* HCI_IBS receive side sleep protocol states */ |
| 70 | enum rx_states { |
| 71 | HCI_IBS_RX_ASLEEP, |
| 72 | HCI_IBS_RX_AWAKE, |
| 73 | }; |
| 74 | |
| 75 | /* HCI_IBS transmit and receive side clock state vote */ |
| 76 | enum hci_ibs_clock_state_vote { |
| 77 | HCI_IBS_VOTE_STATS_UPDATE, |
| 78 | HCI_IBS_TX_VOTE_CLOCK_ON, |
| 79 | HCI_IBS_TX_VOTE_CLOCK_OFF, |
| 80 | HCI_IBS_RX_VOTE_CLOCK_ON, |
| 81 | HCI_IBS_RX_VOTE_CLOCK_OFF, |
| 82 | }; |
| 83 | |
| 84 | struct qca_data { |
| 85 | struct hci_uart *hu; |
| 86 | struct sk_buff *rx_skb; |
| 87 | struct sk_buff_head txq; |
| 88 | struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */ |
| 89 | spinlock_t hci_ibs_lock; /* HCI_IBS state lock */ |
| 90 | u8 tx_ibs_state; /* HCI_IBS transmit side power state*/ |
| 91 | u8 rx_ibs_state; /* HCI_IBS receive side power state */ |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 92 | bool tx_vote; /* Clock must be on for TX */ |
| 93 | bool rx_vote; /* Clock must be on for RX */ |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 94 | struct timer_list tx_idle_timer; |
| 95 | u32 tx_idle_delay; |
| 96 | struct timer_list wake_retrans_timer; |
| 97 | u32 wake_retrans; |
| 98 | struct workqueue_struct *workqueue; |
| 99 | struct work_struct ws_awake_rx; |
| 100 | struct work_struct ws_awake_device; |
| 101 | struct work_struct ws_rx_vote_off; |
| 102 | struct work_struct ws_tx_vote_off; |
| 103 | unsigned long flags; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 104 | struct completion drop_ev_comp; |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 105 | wait_queue_head_t suspend_wait_q; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 106 | |
| 107 | /* For debugging purpose */ |
| 108 | u64 ibs_sent_wacks; |
| 109 | u64 ibs_sent_slps; |
| 110 | u64 ibs_sent_wakes; |
| 111 | u64 ibs_recv_wacks; |
| 112 | u64 ibs_recv_slps; |
| 113 | u64 ibs_recv_wakes; |
| 114 | u64 vote_last_jif; |
| 115 | u32 vote_on_ms; |
| 116 | u32 vote_off_ms; |
| 117 | u64 tx_votes_on; |
| 118 | u64 rx_votes_on; |
| 119 | u64 tx_votes_off; |
| 120 | u64 rx_votes_off; |
| 121 | u64 votes_on; |
| 122 | u64 votes_off; |
| 123 | }; |
| 124 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 125 | enum qca_speed_type { |
| 126 | QCA_INIT_SPEED = 1, |
| 127 | QCA_OPER_SPEED |
| 128 | }; |
| 129 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 130 | /* |
| 131 | * Voltage regulator information required for configuring the |
| 132 | * QCA Bluetooth chipset |
| 133 | */ |
| 134 | struct qca_vreg { |
| 135 | const char *name; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 136 | unsigned int load_uA; |
| 137 | }; |
| 138 | |
| 139 | struct qca_vreg_data { |
| 140 | enum qca_btsoc_type soc_type; |
| 141 | struct qca_vreg *vregs; |
| 142 | size_t num_vregs; |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * Platform data for the QCA Bluetooth power driver. |
| 147 | */ |
| 148 | struct qca_power { |
| 149 | struct device *dev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 150 | struct regulator_bulk_data *vreg_bulk; |
Bjorn Andersson | 163d42f | 2019-10-17 22:24:03 -0700 | [diff] [blame] | 151 | int num_vregs; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 152 | bool vregs_on; |
| 153 | }; |
| 154 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 155 | struct qca_serdev { |
| 156 | struct hci_uart serdev_hu; |
| 157 | struct gpio_desc *bt_en; |
| 158 | struct clk *susclk; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 159 | enum qca_btsoc_type btsoc_type; |
| 160 | struct qca_power *bt_power; |
| 161 | u32 init_speed; |
| 162 | u32 oper_speed; |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 163 | const char *firmware_name; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 166 | static int qca_regulator_enable(struct qca_serdev *qcadev); |
| 167 | static void qca_regulator_disable(struct qca_serdev *qcadev); |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 168 | static void qca_power_shutdown(struct hci_uart *hu); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 169 | static int qca_power_off(struct hci_dev *hdev); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 170 | |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 171 | static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu) |
| 172 | { |
| 173 | enum qca_btsoc_type soc_type; |
| 174 | |
| 175 | if (hu->serdev) { |
| 176 | struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); |
| 177 | |
| 178 | soc_type = qsd->btsoc_type; |
| 179 | } else { |
| 180 | soc_type = QCA_ROME; |
| 181 | } |
| 182 | |
| 183 | return soc_type; |
| 184 | } |
| 185 | |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 186 | static const char *qca_get_firmware_name(struct hci_uart *hu) |
| 187 | { |
| 188 | if (hu->serdev) { |
| 189 | struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); |
| 190 | |
| 191 | return qsd->firmware_name; |
| 192 | } else { |
| 193 | return NULL; |
| 194 | } |
| 195 | } |
| 196 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 197 | static void __serial_clock_on(struct tty_struct *tty) |
| 198 | { |
| 199 | /* TODO: Some chipset requires to enable UART clock on client |
| 200 | * side to save power consumption or manual work is required. |
| 201 | * Please put your code to control UART clock here if needed |
| 202 | */ |
| 203 | } |
| 204 | |
| 205 | static void __serial_clock_off(struct tty_struct *tty) |
| 206 | { |
| 207 | /* TODO: Some chipset requires to disable UART clock on client |
| 208 | * side to save power consumption or manual work is required. |
| 209 | * Please put your code to control UART clock off here if needed |
| 210 | */ |
| 211 | } |
| 212 | |
| 213 | /* serial_clock_vote needs to be called with the ibs lock held */ |
| 214 | static void serial_clock_vote(unsigned long vote, struct hci_uart *hu) |
| 215 | { |
| 216 | struct qca_data *qca = hu->priv; |
| 217 | unsigned int diff; |
| 218 | |
| 219 | bool old_vote = (qca->tx_vote | qca->rx_vote); |
| 220 | bool new_vote; |
| 221 | |
| 222 | switch (vote) { |
| 223 | case HCI_IBS_VOTE_STATS_UPDATE: |
| 224 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); |
| 225 | |
| 226 | if (old_vote) |
| 227 | qca->vote_off_ms += diff; |
| 228 | else |
| 229 | qca->vote_on_ms += diff; |
| 230 | return; |
| 231 | |
| 232 | case HCI_IBS_TX_VOTE_CLOCK_ON: |
| 233 | qca->tx_vote = true; |
| 234 | qca->tx_votes_on++; |
| 235 | new_vote = true; |
| 236 | break; |
| 237 | |
| 238 | case HCI_IBS_RX_VOTE_CLOCK_ON: |
| 239 | qca->rx_vote = true; |
| 240 | qca->rx_votes_on++; |
| 241 | new_vote = true; |
| 242 | break; |
| 243 | |
| 244 | case HCI_IBS_TX_VOTE_CLOCK_OFF: |
| 245 | qca->tx_vote = false; |
| 246 | qca->tx_votes_off++; |
| 247 | new_vote = qca->rx_vote | qca->tx_vote; |
| 248 | break; |
| 249 | |
| 250 | case HCI_IBS_RX_VOTE_CLOCK_OFF: |
| 251 | qca->rx_vote = false; |
| 252 | qca->rx_votes_off++; |
| 253 | new_vote = qca->rx_vote | qca->tx_vote; |
| 254 | break; |
| 255 | |
| 256 | default: |
| 257 | BT_ERR("Voting irregularity"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (new_vote != old_vote) { |
| 262 | if (new_vote) |
| 263 | __serial_clock_on(hu->tty); |
| 264 | else |
| 265 | __serial_clock_off(hu->tty); |
| 266 | |
Prasanna Karthik | ce26d81 | 2015-09-15 12:19:45 +0000 | [diff] [blame] | 267 | BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", |
| 268 | vote ? "true" : "false"); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 269 | |
| 270 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); |
| 271 | |
| 272 | if (new_vote) { |
| 273 | qca->votes_on++; |
| 274 | qca->vote_off_ms += diff; |
| 275 | } else { |
| 276 | qca->votes_off++; |
| 277 | qca->vote_on_ms += diff; |
| 278 | } |
| 279 | qca->vote_last_jif = jiffies; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* Builds and sends an HCI_IBS command packet. |
| 284 | * These are very simple packets with only 1 cmd byte. |
| 285 | */ |
| 286 | static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu) |
| 287 | { |
| 288 | int err = 0; |
| 289 | struct sk_buff *skb = NULL; |
| 290 | struct qca_data *qca = hu->priv; |
| 291 | |
| 292 | BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd); |
| 293 | |
| 294 | skb = bt_skb_alloc(1, GFP_ATOMIC); |
| 295 | if (!skb) { |
| 296 | BT_ERR("Failed to allocate memory for HCI_IBS packet"); |
| 297 | return -ENOMEM; |
| 298 | } |
| 299 | |
| 300 | /* Assign HCI_IBS type */ |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 301 | skb_put_u8(skb, cmd); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 302 | |
| 303 | skb_queue_tail(&qca->txq, skb); |
| 304 | |
| 305 | return err; |
| 306 | } |
| 307 | |
| 308 | static void qca_wq_awake_device(struct work_struct *work) |
| 309 | { |
| 310 | struct qca_data *qca = container_of(work, struct qca_data, |
| 311 | ws_awake_device); |
| 312 | struct hci_uart *hu = qca->hu; |
| 313 | unsigned long retrans_delay; |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 314 | unsigned long flags; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 315 | |
| 316 | BT_DBG("hu %p wq awake device", hu); |
| 317 | |
| 318 | /* Vote for serial clock */ |
| 319 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu); |
| 320 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 321 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 322 | |
| 323 | /* Send wake indication to device */ |
| 324 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) |
| 325 | BT_ERR("Failed to send WAKE to device"); |
| 326 | |
| 327 | qca->ibs_sent_wakes++; |
| 328 | |
| 329 | /* Start retransmit timer */ |
| 330 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); |
| 331 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); |
| 332 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 333 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 334 | |
| 335 | /* Actually send the packets */ |
| 336 | hci_uart_tx_wakeup(hu); |
| 337 | } |
| 338 | |
| 339 | static void qca_wq_awake_rx(struct work_struct *work) |
| 340 | { |
| 341 | struct qca_data *qca = container_of(work, struct qca_data, |
| 342 | ws_awake_rx); |
| 343 | struct hci_uart *hu = qca->hu; |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 344 | unsigned long flags; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 345 | |
| 346 | BT_DBG("hu %p wq awake rx", hu); |
| 347 | |
| 348 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); |
| 349 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 350 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 351 | qca->rx_ibs_state = HCI_IBS_RX_AWAKE; |
| 352 | |
| 353 | /* Always acknowledge device wake up, |
| 354 | * sending IBS message doesn't count as TX ON. |
| 355 | */ |
| 356 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) |
| 357 | BT_ERR("Failed to acknowledge device wake up"); |
| 358 | |
| 359 | qca->ibs_sent_wacks++; |
| 360 | |
Harish Bandi | 31fb1bb | 2019-09-04 10:04:16 +0530 | [diff] [blame] | 361 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 362 | |
| 363 | /* Actually send the packets */ |
| 364 | hci_uart_tx_wakeup(hu); |
| 365 | } |
| 366 | |
| 367 | static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work) |
| 368 | { |
| 369 | struct qca_data *qca = container_of(work, struct qca_data, |
| 370 | ws_rx_vote_off); |
| 371 | struct hci_uart *hu = qca->hu; |
| 372 | |
| 373 | BT_DBG("hu %p rx clock vote off", hu); |
| 374 | |
| 375 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu); |
| 376 | } |
| 377 | |
| 378 | static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work) |
| 379 | { |
| 380 | struct qca_data *qca = container_of(work, struct qca_data, |
| 381 | ws_tx_vote_off); |
| 382 | struct hci_uart *hu = qca->hu; |
| 383 | |
| 384 | BT_DBG("hu %p tx clock vote off", hu); |
| 385 | |
| 386 | /* Run HCI tx handling unlocked */ |
| 387 | hci_uart_tx_wakeup(hu); |
| 388 | |
| 389 | /* Now that message queued to tty driver, vote for tty clocks off. |
| 390 | * It is up to the tty driver to pend the clocks off until tx done. |
| 391 | */ |
| 392 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu); |
| 393 | } |
| 394 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 395 | static void hci_ibs_tx_idle_timeout(struct timer_list *t) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 396 | { |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 397 | struct qca_data *qca = from_timer(qca, t, tx_idle_timer); |
| 398 | struct hci_uart *hu = qca->hu; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 399 | unsigned long flags; |
| 400 | |
| 401 | BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state); |
| 402 | |
| 403 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 404 | flags, SINGLE_DEPTH_NESTING); |
| 405 | |
| 406 | switch (qca->tx_ibs_state) { |
| 407 | case HCI_IBS_TX_AWAKE: |
| 408 | /* TX_IDLE, go to SLEEP */ |
| 409 | if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) { |
| 410 | BT_ERR("Failed to send SLEEP to device"); |
| 411 | break; |
| 412 | } |
| 413 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 414 | qca->ibs_sent_slps++; |
| 415 | queue_work(qca->workqueue, &qca->ws_tx_vote_off); |
| 416 | break; |
| 417 | |
| 418 | case HCI_IBS_TX_ASLEEP: |
| 419 | case HCI_IBS_TX_WAKING: |
| 420 | /* Fall through */ |
| 421 | |
| 422 | default: |
Colin Ian King | e059a46 | 2017-02-17 19:58:10 +0000 | [diff] [blame] | 423 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | |
| 427 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 428 | } |
| 429 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 430 | static void hci_ibs_wake_retrans_timeout(struct timer_list *t) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 431 | { |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 432 | struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); |
| 433 | struct hci_uart *hu = qca->hu; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 434 | unsigned long flags, retrans_delay; |
Prasanna Karthik | a913718 | 2015-09-28 08:03:24 +0000 | [diff] [blame] | 435 | bool retransmit = false; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 436 | |
| 437 | BT_DBG("hu %p wake retransmit timeout in %d state", |
| 438 | hu, qca->tx_ibs_state); |
| 439 | |
| 440 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 441 | flags, SINGLE_DEPTH_NESTING); |
| 442 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 443 | /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */ |
| 444 | if (test_bit(QCA_SUSPENDING, &qca->flags)) { |
| 445 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 446 | return; |
| 447 | } |
| 448 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 449 | switch (qca->tx_ibs_state) { |
| 450 | case HCI_IBS_TX_WAKING: |
| 451 | /* No WAKE_ACK, retransmit WAKE */ |
Prasanna Karthik | a913718 | 2015-09-28 08:03:24 +0000 | [diff] [blame] | 452 | retransmit = true; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 453 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) { |
| 454 | BT_ERR("Failed to acknowledge device wake up"); |
| 455 | break; |
| 456 | } |
| 457 | qca->ibs_sent_wakes++; |
| 458 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); |
| 459 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); |
| 460 | break; |
| 461 | |
| 462 | case HCI_IBS_TX_ASLEEP: |
| 463 | case HCI_IBS_TX_AWAKE: |
| 464 | /* Fall through */ |
| 465 | |
| 466 | default: |
Colin Ian King | e059a46 | 2017-02-17 19:58:10 +0000 | [diff] [blame] | 467 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | |
| 471 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 472 | |
| 473 | if (retransmit) |
| 474 | hci_uart_tx_wakeup(hu); |
| 475 | } |
| 476 | |
| 477 | /* Initialize protocol */ |
| 478 | static int qca_open(struct hci_uart *hu) |
| 479 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 480 | struct qca_serdev *qcadev; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 481 | struct qca_data *qca; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 482 | int ret; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 483 | |
| 484 | BT_DBG("hu %p qca_open", hu); |
| 485 | |
Vladis Dronov | b36a155 | 2019-07-30 11:33:45 +0200 | [diff] [blame] | 486 | if (!hci_uart_has_flow_control(hu)) |
| 487 | return -EOPNOTSUPP; |
| 488 | |
Jia-Ju Bai | 25a13e38 | 2018-07-23 11:56:51 +0800 | [diff] [blame] | 489 | qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 490 | if (!qca) |
| 491 | return -ENOMEM; |
| 492 | |
| 493 | skb_queue_head_init(&qca->txq); |
| 494 | skb_queue_head_init(&qca->tx_wait_q); |
| 495 | spin_lock_init(&qca->hci_ibs_lock); |
Bhaktipriya Shridhar | fac9a60 | 2016-08-30 22:42:53 +0530 | [diff] [blame] | 496 | qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 497 | if (!qca->workqueue) { |
| 498 | BT_ERR("QCA Workqueue not initialized properly"); |
| 499 | kfree(qca); |
| 500 | return -ENOMEM; |
| 501 | } |
| 502 | |
| 503 | INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx); |
| 504 | INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device); |
| 505 | INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off); |
| 506 | INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off); |
| 507 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 508 | init_waitqueue_head(&qca->suspend_wait_q); |
| 509 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 510 | qca->hu = hu; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 511 | init_completion(&qca->drop_ev_comp); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 512 | |
| 513 | /* Assume we start with both sides asleep -- extra wakes OK */ |
| 514 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 515 | qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; |
| 516 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 517 | qca->vote_last_jif = jiffies; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 518 | |
| 519 | hu->priv = qca; |
| 520 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 521 | if (hu->serdev) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 522 | |
| 523 | qcadev = serdev_device_get_drvdata(hu->serdev); |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 524 | if (!qca_is_wcn399x(qcadev->btsoc_type)) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 525 | gpiod_set_value_cansleep(qcadev->bt_en, 1); |
Balakrishna Godavarthi | 7f09d5a | 2019-04-01 15:19:08 +0530 | [diff] [blame] | 526 | /* Controller needs time to bootup. */ |
| 527 | msleep(150); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 528 | } else { |
| 529 | hu->init_speed = qcadev->init_speed; |
| 530 | hu->oper_speed = qcadev->oper_speed; |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 531 | ret = qca_regulator_enable(qcadev); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 532 | if (ret) { |
| 533 | destroy_workqueue(qca->workqueue); |
| 534 | kfree_skb(qca->rx_skb); |
| 535 | hu->priv = NULL; |
| 536 | kfree(qca); |
| 537 | return ret; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 542 | timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 543 | qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; |
| 544 | |
Kees Cook | 0435605 | 2017-10-04 17:54:29 -0700 | [diff] [blame] | 545 | timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 546 | qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 547 | |
| 548 | BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u", |
| 549 | qca->tx_idle_delay, qca->wake_retrans); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static void qca_debugfs_init(struct hci_dev *hdev) |
| 555 | { |
| 556 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 557 | struct qca_data *qca = hu->priv; |
| 558 | struct dentry *ibs_dir; |
| 559 | umode_t mode; |
| 560 | |
| 561 | if (!hdev->debugfs) |
| 562 | return; |
| 563 | |
| 564 | ibs_dir = debugfs_create_dir("ibs", hdev->debugfs); |
| 565 | |
| 566 | /* read only */ |
| 567 | mode = S_IRUGO; |
| 568 | debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state); |
| 569 | debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state); |
| 570 | debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir, |
| 571 | &qca->ibs_sent_slps); |
| 572 | debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir, |
| 573 | &qca->ibs_sent_wakes); |
| 574 | debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir, |
| 575 | &qca->ibs_sent_wacks); |
| 576 | debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir, |
| 577 | &qca->ibs_recv_slps); |
| 578 | debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir, |
| 579 | &qca->ibs_recv_wakes); |
| 580 | debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir, |
| 581 | &qca->ibs_recv_wacks); |
Ben YoungTae Kim | 10be6c0 | 2015-08-13 22:09:42 -0700 | [diff] [blame] | 582 | debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 583 | debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on); |
| 584 | debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off); |
Ben YoungTae Kim | 10be6c0 | 2015-08-13 22:09:42 -0700 | [diff] [blame] | 585 | debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 586 | debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on); |
| 587 | debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off); |
| 588 | debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on); |
| 589 | debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off); |
| 590 | debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms); |
| 591 | debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms); |
| 592 | |
| 593 | /* read/write */ |
| 594 | mode = S_IRUGO | S_IWUSR; |
| 595 | debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans); |
| 596 | debugfs_create_u32("tx_idle_delay", mode, ibs_dir, |
| 597 | &qca->tx_idle_delay); |
| 598 | } |
| 599 | |
| 600 | /* Flush protocol data */ |
| 601 | static int qca_flush(struct hci_uart *hu) |
| 602 | { |
| 603 | struct qca_data *qca = hu->priv; |
| 604 | |
| 605 | BT_DBG("hu %p qca flush", hu); |
| 606 | |
| 607 | skb_queue_purge(&qca->tx_wait_q); |
| 608 | skb_queue_purge(&qca->txq); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* Close protocol */ |
| 614 | static int qca_close(struct hci_uart *hu) |
| 615 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 616 | struct qca_serdev *qcadev; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 617 | struct qca_data *qca = hu->priv; |
| 618 | |
| 619 | BT_DBG("hu %p qca close", hu); |
| 620 | |
| 621 | serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu); |
| 622 | |
| 623 | skb_queue_purge(&qca->tx_wait_q); |
| 624 | skb_queue_purge(&qca->txq); |
| 625 | del_timer(&qca->tx_idle_timer); |
| 626 | del_timer(&qca->wake_retrans_timer); |
| 627 | destroy_workqueue(qca->workqueue); |
| 628 | qca->hu = NULL; |
| 629 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 630 | if (hu->serdev) { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 631 | qcadev = serdev_device_get_drvdata(hu->serdev); |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 632 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 633 | qca_power_shutdown(hu); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 634 | else |
| 635 | gpiod_set_value_cansleep(qcadev->bt_en, 0); |
| 636 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 639 | kfree_skb(qca->rx_skb); |
| 640 | |
| 641 | hu->priv = NULL; |
| 642 | |
| 643 | kfree(qca); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* Called upon a wake-up-indication from the device. |
| 649 | */ |
| 650 | static void device_want_to_wakeup(struct hci_uart *hu) |
| 651 | { |
| 652 | unsigned long flags; |
| 653 | struct qca_data *qca = hu->priv; |
| 654 | |
| 655 | BT_DBG("hu %p want to wake up", hu); |
| 656 | |
| 657 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 658 | |
| 659 | qca->ibs_recv_wakes++; |
| 660 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 661 | /* Don't wake the rx up when suspending. */ |
| 662 | if (test_bit(QCA_SUSPENDING, &qca->flags)) { |
| 663 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 664 | return; |
| 665 | } |
| 666 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 667 | switch (qca->rx_ibs_state) { |
| 668 | case HCI_IBS_RX_ASLEEP: |
| 669 | /* Make sure clock is on - we may have turned clock off since |
| 670 | * receiving the wake up indicator awake rx clock. |
| 671 | */ |
| 672 | queue_work(qca->workqueue, &qca->ws_awake_rx); |
| 673 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 674 | return; |
| 675 | |
| 676 | case HCI_IBS_RX_AWAKE: |
| 677 | /* Always acknowledge device wake up, |
| 678 | * sending IBS message doesn't count as TX ON. |
| 679 | */ |
| 680 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) { |
| 681 | BT_ERR("Failed to acknowledge device wake up"); |
| 682 | break; |
| 683 | } |
| 684 | qca->ibs_sent_wacks++; |
| 685 | break; |
| 686 | |
| 687 | default: |
| 688 | /* Any other state is illegal */ |
| 689 | BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d", |
| 690 | qca->rx_ibs_state); |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 695 | |
| 696 | /* Actually send the packets */ |
| 697 | hci_uart_tx_wakeup(hu); |
| 698 | } |
| 699 | |
| 700 | /* Called upon a sleep-indication from the device. |
| 701 | */ |
| 702 | static void device_want_to_sleep(struct hci_uart *hu) |
| 703 | { |
| 704 | unsigned long flags; |
| 705 | struct qca_data *qca = hu->priv; |
| 706 | |
Rocky Liao | 6600c08 | 2019-08-14 15:42:39 +0800 | [diff] [blame] | 707 | BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 708 | |
| 709 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 710 | |
| 711 | qca->ibs_recv_slps++; |
| 712 | |
| 713 | switch (qca->rx_ibs_state) { |
| 714 | case HCI_IBS_RX_AWAKE: |
| 715 | /* Update state */ |
| 716 | qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; |
| 717 | /* Vote off rx clock under workqueue */ |
| 718 | queue_work(qca->workqueue, &qca->ws_rx_vote_off); |
| 719 | break; |
| 720 | |
| 721 | case HCI_IBS_RX_ASLEEP: |
Rocky Liao | 6600c08 | 2019-08-14 15:42:39 +0800 | [diff] [blame] | 722 | break; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 723 | |
| 724 | default: |
| 725 | /* Any other state is illegal */ |
| 726 | BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d", |
| 727 | qca->rx_ibs_state); |
| 728 | break; |
| 729 | } |
| 730 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 731 | wake_up_interruptible(&qca->suspend_wait_q); |
| 732 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 733 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 734 | } |
| 735 | |
| 736 | /* Called upon wake-up-acknowledgement from the device |
| 737 | */ |
| 738 | static void device_woke_up(struct hci_uart *hu) |
| 739 | { |
| 740 | unsigned long flags, idle_delay; |
| 741 | struct qca_data *qca = hu->priv; |
| 742 | struct sk_buff *skb = NULL; |
| 743 | |
| 744 | BT_DBG("hu %p woke up", hu); |
| 745 | |
| 746 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 747 | |
| 748 | qca->ibs_recv_wacks++; |
| 749 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 750 | /* Don't react to the wake-up-acknowledgment when suspending. */ |
| 751 | if (test_bit(QCA_SUSPENDING, &qca->flags)) { |
| 752 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 753 | return; |
| 754 | } |
| 755 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 756 | switch (qca->tx_ibs_state) { |
| 757 | case HCI_IBS_TX_AWAKE: |
| 758 | /* Expect one if we send 2 WAKEs */ |
| 759 | BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d", |
| 760 | qca->tx_ibs_state); |
| 761 | break; |
| 762 | |
| 763 | case HCI_IBS_TX_WAKING: |
| 764 | /* Send pending packets */ |
| 765 | while ((skb = skb_dequeue(&qca->tx_wait_q))) |
| 766 | skb_queue_tail(&qca->txq, skb); |
| 767 | |
| 768 | /* Switch timers and change state to HCI_IBS_TX_AWAKE */ |
| 769 | del_timer(&qca->wake_retrans_timer); |
| 770 | idle_delay = msecs_to_jiffies(qca->tx_idle_delay); |
| 771 | mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); |
| 772 | qca->tx_ibs_state = HCI_IBS_TX_AWAKE; |
| 773 | break; |
| 774 | |
| 775 | case HCI_IBS_TX_ASLEEP: |
| 776 | /* Fall through */ |
| 777 | |
| 778 | default: |
| 779 | BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d", |
| 780 | qca->tx_ibs_state); |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 785 | |
| 786 | /* Actually send the packets */ |
| 787 | hci_uart_tx_wakeup(hu); |
| 788 | } |
| 789 | |
| 790 | /* Enqueue frame for transmittion (padding, crc, etc) may be called from |
| 791 | * two simultaneous tasklets. |
| 792 | */ |
| 793 | static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 794 | { |
| 795 | unsigned long flags = 0, idle_delay; |
| 796 | struct qca_data *qca = hu->priv; |
| 797 | |
| 798 | BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb, |
| 799 | qca->tx_ibs_state); |
| 800 | |
| 801 | /* Prepend skb with frame type */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 802 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 803 | |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 804 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
| 805 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 806 | /* Don't go to sleep in middle of patch download or |
| 807 | * Out-Of-Band(GPIOs control) sleep is selected. |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 808 | * Don't wake the device up when suspending. |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 809 | */ |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 810 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags) || |
| 811 | test_bit(QCA_SUSPENDING, &qca->flags)) { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 812 | skb_queue_tail(&qca->txq, skb); |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 813 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 814 | return 0; |
| 815 | } |
| 816 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 817 | /* Act according to current state */ |
| 818 | switch (qca->tx_ibs_state) { |
| 819 | case HCI_IBS_TX_AWAKE: |
| 820 | BT_DBG("Device awake, sending normally"); |
| 821 | skb_queue_tail(&qca->txq, skb); |
| 822 | idle_delay = msecs_to_jiffies(qca->tx_idle_delay); |
| 823 | mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); |
| 824 | break; |
| 825 | |
| 826 | case HCI_IBS_TX_ASLEEP: |
| 827 | BT_DBG("Device asleep, waking up and queueing packet"); |
| 828 | /* Save packet for later */ |
| 829 | skb_queue_tail(&qca->tx_wait_q, skb); |
| 830 | |
| 831 | qca->tx_ibs_state = HCI_IBS_TX_WAKING; |
| 832 | /* Schedule a work queue to wake up device */ |
| 833 | queue_work(qca->workqueue, &qca->ws_awake_device); |
| 834 | break; |
| 835 | |
| 836 | case HCI_IBS_TX_WAKING: |
| 837 | BT_DBG("Device waking up, queueing packet"); |
| 838 | /* Transient state; just keep packet for later */ |
| 839 | skb_queue_tail(&qca->tx_wait_q, skb); |
| 840 | break; |
| 841 | |
| 842 | default: |
| 843 | BT_ERR("Illegal tx state: %d (losing packet)", |
| 844 | qca->tx_ibs_state); |
| 845 | kfree_skb(skb); |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb) |
| 855 | { |
| 856 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 857 | |
| 858 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND); |
| 859 | |
| 860 | device_want_to_sleep(hu); |
| 861 | |
| 862 | kfree_skb(skb); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb) |
| 867 | { |
| 868 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 869 | |
| 870 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND); |
| 871 | |
| 872 | device_want_to_wakeup(hu); |
| 873 | |
| 874 | kfree_skb(skb); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb) |
| 879 | { |
| 880 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 881 | |
| 882 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK); |
| 883 | |
| 884 | device_woke_up(hu); |
| 885 | |
| 886 | kfree_skb(skb); |
| 887 | return 0; |
| 888 | } |
| 889 | |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 890 | static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb) |
| 891 | { |
| 892 | /* We receive debug logs from chip as an ACL packets. |
| 893 | * Instead of sending the data to ACL to decode the |
| 894 | * received data, we are pushing them to the above layers |
| 895 | * as a diagnostic packet. |
| 896 | */ |
| 897 | if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE) |
| 898 | return hci_recv_diag(hdev, skb); |
| 899 | |
| 900 | return hci_recv_frame(hdev, skb); |
| 901 | } |
| 902 | |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 903 | static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb) |
| 904 | { |
| 905 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 906 | struct qca_data *qca = hu->priv; |
| 907 | |
| 908 | if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) { |
| 909 | struct hci_event_hdr *hdr = (void *)skb->data; |
| 910 | |
| 911 | /* For the WCN3990 the vendor command for a baudrate change |
| 912 | * isn't sent as synchronous HCI command, because the |
| 913 | * controller sends the corresponding vendor event with the |
| 914 | * new baudrate. The event is received and properly decoded |
| 915 | * after changing the baudrate of the host port. It needs to |
| 916 | * be dropped, otherwise it can be misinterpreted as |
| 917 | * response to a later firmware download command (also a |
| 918 | * vendor command). |
| 919 | */ |
| 920 | |
| 921 | if (hdr->evt == HCI_EV_VENDOR) |
| 922 | complete(&qca->drop_ev_comp); |
| 923 | |
Wei Yongjun | 4974c83 | 2019-07-09 01:35:30 +0000 | [diff] [blame] | 924 | kfree_skb(skb); |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | return hci_recv_frame(hdev, skb); |
| 930 | } |
| 931 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 932 | #define QCA_IBS_SLEEP_IND_EVENT \ |
| 933 | .type = HCI_IBS_SLEEP_IND, \ |
| 934 | .hlen = 0, \ |
| 935 | .loff = 0, \ |
| 936 | .lsize = 0, \ |
| 937 | .maxlen = HCI_MAX_IBS_SIZE |
| 938 | |
| 939 | #define QCA_IBS_WAKE_IND_EVENT \ |
| 940 | .type = HCI_IBS_WAKE_IND, \ |
| 941 | .hlen = 0, \ |
| 942 | .loff = 0, \ |
| 943 | .lsize = 0, \ |
| 944 | .maxlen = HCI_MAX_IBS_SIZE |
| 945 | |
| 946 | #define QCA_IBS_WAKE_ACK_EVENT \ |
| 947 | .type = HCI_IBS_WAKE_ACK, \ |
| 948 | .hlen = 0, \ |
| 949 | .loff = 0, \ |
| 950 | .lsize = 0, \ |
| 951 | .maxlen = HCI_MAX_IBS_SIZE |
| 952 | |
| 953 | static const struct h4_recv_pkt qca_recv_pkts[] = { |
Balakrishna Godavarthi | c614ca3 | 2018-10-16 19:51:35 +0530 | [diff] [blame] | 954 | { H4_RECV_ACL, .recv = qca_recv_acl_data }, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 955 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 956 | { H4_RECV_EVENT, .recv = qca_recv_event }, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 957 | { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind }, |
| 958 | { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack }, |
| 959 | { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind }, |
| 960 | }; |
| 961 | |
| 962 | static int qca_recv(struct hci_uart *hu, const void *data, int count) |
| 963 | { |
| 964 | struct qca_data *qca = hu->priv; |
| 965 | |
| 966 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) |
| 967 | return -EUNATCH; |
| 968 | |
| 969 | qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count, |
| 970 | qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts)); |
| 971 | if (IS_ERR(qca->rx_skb)) { |
| 972 | int err = PTR_ERR(qca->rx_skb); |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 973 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 974 | qca->rx_skb = NULL; |
| 975 | return err; |
| 976 | } |
| 977 | |
| 978 | return count; |
| 979 | } |
| 980 | |
| 981 | static struct sk_buff *qca_dequeue(struct hci_uart *hu) |
| 982 | { |
| 983 | struct qca_data *qca = hu->priv; |
| 984 | |
| 985 | return skb_dequeue(&qca->txq); |
| 986 | } |
| 987 | |
| 988 | static uint8_t qca_get_baudrate_value(int speed) |
| 989 | { |
Prasanna Karthik | ce26d81 | 2015-09-15 12:19:45 +0000 | [diff] [blame] | 990 | switch (speed) { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 991 | case 9600: |
| 992 | return QCA_BAUDRATE_9600; |
| 993 | case 19200: |
| 994 | return QCA_BAUDRATE_19200; |
| 995 | case 38400: |
| 996 | return QCA_BAUDRATE_38400; |
| 997 | case 57600: |
| 998 | return QCA_BAUDRATE_57600; |
| 999 | case 115200: |
| 1000 | return QCA_BAUDRATE_115200; |
| 1001 | case 230400: |
| 1002 | return QCA_BAUDRATE_230400; |
| 1003 | case 460800: |
| 1004 | return QCA_BAUDRATE_460800; |
| 1005 | case 500000: |
| 1006 | return QCA_BAUDRATE_500000; |
| 1007 | case 921600: |
| 1008 | return QCA_BAUDRATE_921600; |
| 1009 | case 1000000: |
| 1010 | return QCA_BAUDRATE_1000000; |
| 1011 | case 2000000: |
| 1012 | return QCA_BAUDRATE_2000000; |
| 1013 | case 3000000: |
| 1014 | return QCA_BAUDRATE_3000000; |
Balakrishna Godavarthi | be93a49 | 2018-08-03 17:46:30 +0530 | [diff] [blame] | 1015 | case 3200000: |
| 1016 | return QCA_BAUDRATE_3200000; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1017 | case 3500000: |
| 1018 | return QCA_BAUDRATE_3500000; |
| 1019 | default: |
| 1020 | return QCA_BAUDRATE_115200; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) |
| 1025 | { |
| 1026 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1027 | struct qca_data *qca = hu->priv; |
| 1028 | struct sk_buff *skb; |
| 1029 | u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 }; |
| 1030 | |
Balakrishna Godavarthi | be93a49 | 2018-08-03 17:46:30 +0530 | [diff] [blame] | 1031 | if (baudrate > QCA_BAUDRATE_3200000) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1032 | return -EINVAL; |
| 1033 | |
| 1034 | cmd[4] = baudrate; |
| 1035 | |
Jia-Ju Bai | 25a13e38 | 2018-07-23 11:56:51 +0800 | [diff] [blame] | 1036 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1037 | if (!skb) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 1038 | bt_dev_err(hdev, "Failed to allocate baudrate packet"); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1039 | return -ENOMEM; |
| 1040 | } |
| 1041 | |
| 1042 | /* Assign commands to change baudrate and packet type. */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1043 | skb_put_data(skb, cmd, sizeof(cmd)); |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 1044 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1045 | |
| 1046 | skb_queue_tail(&qca->txq, skb); |
| 1047 | hci_uart_tx_wakeup(hu); |
| 1048 | |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1049 | /* Wait for the baudrate change request to be sent */ |
| 1050 | |
| 1051 | while (!skb_queue_empty(&qca->txq)) |
| 1052 | usleep_range(100, 200); |
| 1053 | |
Matthias Kaehlcke | ecf2b76 | 2019-04-23 11:16:52 -0700 | [diff] [blame] | 1054 | if (hu->serdev) |
| 1055 | serdev_device_wait_until_sent(hu->serdev, |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1056 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); |
| 1057 | |
| 1058 | /* Give the controller time to process the request */ |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1059 | if (qca_is_wcn399x(qca_soc_type(hu))) |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1060 | msleep(10); |
| 1061 | else |
| 1062 | msleep(300); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1067 | static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 1068 | { |
| 1069 | if (hu->serdev) |
| 1070 | serdev_device_set_baudrate(hu->serdev, speed); |
| 1071 | else |
| 1072 | hci_uart_set_baudrate(hu, speed); |
| 1073 | } |
| 1074 | |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1075 | static int qca_send_power_pulse(struct hci_uart *hu, bool on) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1076 | { |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1077 | int ret; |
Matthias Kaehlcke | 94d6671 | 2019-02-27 15:52:23 -0800 | [diff] [blame] | 1078 | int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1079 | u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1080 | |
| 1081 | /* These power pulses are single byte command which are sent |
| 1082 | * at required baudrate to wcn3990. On wcn3990, we have an external |
| 1083 | * circuit at Tx pin which decodes the pulse sent at specific baudrate. |
| 1084 | * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT |
| 1085 | * and also we use the same power inputs to turn on and off for |
| 1086 | * Wi-Fi/BT. Powering up the power sources will not enable BT, until |
| 1087 | * we send a power on pulse at 115200 bps. This algorithm will help to |
| 1088 | * save power. Disabling hardware flow control is mandatory while |
| 1089 | * sending power pulses to SoC. |
| 1090 | */ |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1091 | bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1092 | |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1093 | serdev_device_write_flush(hu->serdev); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1094 | hci_uart_set_flow_control(hu, true); |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1095 | ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); |
| 1096 | if (ret < 0) { |
| 1097 | bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd); |
| 1098 | return ret; |
| 1099 | } |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1100 | |
Balakrishna Godavarthi | f955827 | 2019-02-04 20:36:41 +0530 | [diff] [blame] | 1101 | serdev_device_wait_until_sent(hu->serdev, timeout); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1102 | hci_uart_set_flow_control(hu, false); |
| 1103 | |
Matthias Kaehlcke | 0ebcddd | 2019-02-26 11:46:47 -0800 | [diff] [blame] | 1104 | /* Give to controller time to boot/shutdown */ |
Matthias Kaehlcke | ad571d7 | 2019-02-26 11:46:46 -0800 | [diff] [blame] | 1105 | if (on) |
| 1106 | msleep(100); |
Matthias Kaehlcke | 0ebcddd | 2019-02-26 11:46:47 -0800 | [diff] [blame] | 1107 | else |
| 1108 | msleep(10); |
Matthias Kaehlcke | ad571d7 | 2019-02-26 11:46:46 -0800 | [diff] [blame] | 1109 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
| 1112 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1113 | static unsigned int qca_get_speed(struct hci_uart *hu, |
| 1114 | enum qca_speed_type speed_type) |
| 1115 | { |
| 1116 | unsigned int speed = 0; |
| 1117 | |
| 1118 | if (speed_type == QCA_INIT_SPEED) { |
| 1119 | if (hu->init_speed) |
| 1120 | speed = hu->init_speed; |
| 1121 | else if (hu->proto->init_speed) |
| 1122 | speed = hu->proto->init_speed; |
| 1123 | } else { |
| 1124 | if (hu->oper_speed) |
| 1125 | speed = hu->oper_speed; |
| 1126 | else if (hu->proto->oper_speed) |
| 1127 | speed = hu->proto->oper_speed; |
| 1128 | } |
| 1129 | |
| 1130 | return speed; |
| 1131 | } |
| 1132 | |
| 1133 | static int qca_check_speeds(struct hci_uart *hu) |
| 1134 | { |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1135 | if (qca_is_wcn399x(qca_soc_type(hu))) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1136 | if (!qca_get_speed(hu, QCA_INIT_SPEED) && |
| 1137 | !qca_get_speed(hu, QCA_OPER_SPEED)) |
| 1138 | return -EINVAL; |
| 1139 | } else { |
| 1140 | if (!qca_get_speed(hu, QCA_INIT_SPEED) || |
| 1141 | !qca_get_speed(hu, QCA_OPER_SPEED)) |
| 1142 | return -EINVAL; |
| 1143 | } |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1144 | |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) |
| 1149 | { |
| 1150 | unsigned int speed, qca_baudrate; |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1151 | struct qca_data *qca = hu->priv; |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1152 | int ret = 0; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1153 | |
| 1154 | if (speed_type == QCA_INIT_SPEED) { |
| 1155 | speed = qca_get_speed(hu, QCA_INIT_SPEED); |
| 1156 | if (speed) |
| 1157 | host_set_baudrate(hu, speed); |
| 1158 | } else { |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 1159 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
| 1160 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1161 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
| 1162 | if (!speed) |
| 1163 | return 0; |
| 1164 | |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1165 | /* Disable flow control for wcn3990 to deassert RTS while |
| 1166 | * changing the baudrate of chip and host. |
| 1167 | */ |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1168 | if (qca_is_wcn399x(soc_type)) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1169 | hci_uart_set_flow_control(hu, true); |
| 1170 | |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1171 | if (soc_type == QCA_WCN3990) { |
| 1172 | reinit_completion(&qca->drop_ev_comp); |
| 1173 | set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); |
| 1174 | } |
| 1175 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1176 | qca_baudrate = qca_get_baudrate_value(speed); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1177 | bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1178 | ret = qca_set_baudrate(hu->hdev, qca_baudrate); |
| 1179 | if (ret) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1180 | goto error; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1181 | |
| 1182 | host_set_baudrate(hu, speed); |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1183 | |
| 1184 | error: |
Jeffrey Hugo | bba79fe | 2019-10-21 07:18:27 -0700 | [diff] [blame] | 1185 | if (qca_is_wcn399x(soc_type)) |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1186 | hci_uart_set_flow_control(hu, false); |
Matthias Kaehlcke | 2faa3f1 | 2019-05-21 12:53:07 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (soc_type == QCA_WCN3990) { |
| 1189 | /* Wait for the controller to send the vendor event |
| 1190 | * for the baudrate change command. |
| 1191 | */ |
| 1192 | if (!wait_for_completion_timeout(&qca->drop_ev_comp, |
| 1193 | msecs_to_jiffies(100))) { |
| 1194 | bt_dev_err(hu->hdev, |
| 1195 | "Failed to change controller baudrate\n"); |
| 1196 | ret = -ETIMEDOUT; |
| 1197 | } |
| 1198 | |
| 1199 | clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); |
| 1200 | } |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1201 | } |
| 1202 | |
Balakrishna Godavarthi | 78e8fa2 | 2019-02-04 20:36:42 +0530 | [diff] [blame] | 1203 | return ret; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1204 | } |
| 1205 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1206 | static int qca_wcn3990_init(struct hci_uart *hu) |
| 1207 | { |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1208 | struct qca_serdev *qcadev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1209 | int ret; |
| 1210 | |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1211 | /* Check for vregs status, may be hci down has turned |
| 1212 | * off the voltage regulator. |
| 1213 | */ |
| 1214 | qcadev = serdev_device_get_drvdata(hu->serdev); |
| 1215 | if (!qcadev->bt_power->vregs_on) { |
| 1216 | serdev_device_close(hu->serdev); |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1217 | ret = qca_regulator_enable(qcadev); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1218 | if (ret) |
| 1219 | return ret; |
| 1220 | |
| 1221 | ret = serdev_device_open(hu->serdev); |
| 1222 | if (ret) { |
| 1223 | bt_dev_err(hu->hdev, "failed to open port"); |
| 1224 | return ret; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1228 | /* Forcefully enable wcn3990 to enter in to boot mode. */ |
| 1229 | host_set_baudrate(hu, 2400); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1230 | ret = qca_send_power_pulse(hu, false); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1231 | if (ret) |
| 1232 | return ret; |
| 1233 | |
| 1234 | qca_set_speed(hu, QCA_INIT_SPEED); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1235 | ret = qca_send_power_pulse(hu, true); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1236 | if (ret) |
| 1237 | return ret; |
| 1238 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1239 | /* Now the device is in ready state to communicate with host. |
| 1240 | * To sync host with device we need to reopen port. |
| 1241 | * Without this, we will have RTS and CTS synchronization |
| 1242 | * issues. |
| 1243 | */ |
| 1244 | serdev_device_close(hu->serdev); |
| 1245 | ret = serdev_device_open(hu->serdev); |
| 1246 | if (ret) { |
| 1247 | bt_dev_err(hu->hdev, "failed to open port"); |
| 1248 | return ret; |
| 1249 | } |
| 1250 | |
| 1251 | hci_uart_set_flow_control(hu, false); |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1256 | static int qca_setup(struct hci_uart *hu) |
| 1257 | { |
| 1258 | struct hci_dev *hdev = hu->hdev; |
| 1259 | struct qca_data *qca = hu->priv; |
| 1260 | unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200; |
Matthias Kaehlcke | 4fdd5a4 | 2019-03-11 11:38:31 -0700 | [diff] [blame] | 1261 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1262 | const char *firmware_name = qca_get_firmware_name(hu); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1263 | int ret; |
Balakrishna Godavarthi | aadebac | 2018-08-03 17:46:28 +0530 | [diff] [blame] | 1264 | int soc_ver = 0; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1265 | |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1266 | ret = qca_check_speeds(hu); |
| 1267 | if (ret) |
| 1268 | return ret; |
| 1269 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1270 | /* Patch downloading has to be done without IBS mode */ |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1271 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1272 | |
Rocky Liao | e14c167 | 2019-08-21 14:23:39 +0800 | [diff] [blame] | 1273 | /* Enable controller to do both LE scan and BR/EDR inquiry |
| 1274 | * simultaneously. |
| 1275 | */ |
| 1276 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
| 1277 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1278 | if (qca_is_wcn399x(soc_type)) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1279 | bt_dev_info(hdev, "setting up wcn3990"); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1280 | |
| 1281 | /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute |
| 1282 | * setup for every hci up. |
| 1283 | */ |
| 1284 | set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); |
Matthias Kaehlcke | 5971752d | 2019-02-19 12:05:59 -0800 | [diff] [blame] | 1285 | set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1286 | hu->hdev->shutdown = qca_power_off; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1287 | ret = qca_wcn3990_init(hu); |
| 1288 | if (ret) |
| 1289 | return ret; |
| 1290 | |
| 1291 | ret = qca_read_soc_version(hdev, &soc_ver); |
| 1292 | if (ret) |
| 1293 | return ret; |
| 1294 | } else { |
| 1295 | bt_dev_info(hdev, "ROME setup"); |
| 1296 | qca_set_speed(hu, QCA_INIT_SPEED); |
| 1297 | } |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1298 | |
| 1299 | /* Setup user speed if needed */ |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1300 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1301 | if (speed) { |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1302 | ret = qca_set_speed(hu, QCA_OPER_SPEED); |
| 1303 | if (ret) |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1304 | return ret; |
Balakrishna Godavarthi | 83d9c5e | 2018-08-03 17:46:29 +0530 | [diff] [blame] | 1305 | |
| 1306 | qca_baudrate = qca_get_baudrate_value(speed); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1309 | if (!qca_is_wcn399x(soc_type)) { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1310 | /* Get QCA version information */ |
| 1311 | ret = qca_read_soc_version(hdev, &soc_ver); |
| 1312 | if (ret) |
| 1313 | return ret; |
| 1314 | } |
Balakrishna Godavarthi | aadebac | 2018-08-03 17:46:28 +0530 | [diff] [blame] | 1315 | |
| 1316 | bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1317 | /* Setup patch / NVM configurations */ |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1318 | ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver, |
| 1319 | firmware_name); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1320 | if (!ret) { |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1321 | set_bit(QCA_IBS_ENABLED, &qca->flags); |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1322 | qca_debugfs_init(hdev); |
Loic Poulain | ba8f359 | 2017-11-06 12:16:56 +0100 | [diff] [blame] | 1323 | } else if (ret == -ENOENT) { |
| 1324 | /* No patch/nvm-config found, run with original fw/config */ |
| 1325 | ret = 0; |
Amit Pundir | 7dc5fe0 | 2018-04-16 12:10:24 +0530 | [diff] [blame] | 1326 | } else if (ret == -EAGAIN) { |
| 1327 | /* |
| 1328 | * Userspace firmware loader will return -EAGAIN in case no |
| 1329 | * patch/nvm-config is found, so run with original fw/config. |
| 1330 | */ |
| 1331 | ret = 0; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* Setup bdaddr */ |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1335 | if (qca_is_wcn399x(soc_type)) |
Balakrishna Godavarthi | 5c0a1001 | 2019-01-16 18:01:15 +0530 | [diff] [blame] | 1336 | hu->hdev->set_bdaddr = qca_set_bdaddr; |
| 1337 | else |
| 1338 | hu->hdev->set_bdaddr = qca_set_bdaddr_rome; |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1339 | |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
Nishka Dasgupta | 2edc9c5 | 2019-08-15 11:21:49 +0530 | [diff] [blame] | 1343 | static const struct hci_uart_proto qca_proto = { |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1344 | .id = HCI_UART_QCA, |
| 1345 | .name = "QCA", |
Marcel Holtmann | aee61f7 | 2015-10-20 21:30:45 +0200 | [diff] [blame] | 1346 | .manufacturer = 29, |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1347 | .init_speed = 115200, |
| 1348 | .oper_speed = 3000000, |
| 1349 | .open = qca_open, |
| 1350 | .close = qca_close, |
| 1351 | .flush = qca_flush, |
| 1352 | .setup = qca_setup, |
| 1353 | .recv = qca_recv, |
| 1354 | .enqueue = qca_enqueue, |
| 1355 | .dequeue = qca_dequeue, |
| 1356 | }; |
| 1357 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1358 | static const struct qca_vreg_data qca_soc_data_wcn3990 = { |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1359 | .soc_type = QCA_WCN3990, |
| 1360 | .vregs = (struct qca_vreg []) { |
Bjorn Andersson | f2edd66 | 2019-10-17 22:24:02 -0700 | [diff] [blame] | 1361 | { "vddio", 15000 }, |
| 1362 | { "vddxo", 80000 }, |
| 1363 | { "vddrf", 300000 }, |
| 1364 | { "vddch0", 450000 }, |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1365 | }, |
| 1366 | .num_vregs = 4, |
| 1367 | }; |
| 1368 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1369 | static const struct qca_vreg_data qca_soc_data_wcn3998 = { |
| 1370 | .soc_type = QCA_WCN3998, |
| 1371 | .vregs = (struct qca_vreg []) { |
Bjorn Andersson | f2edd66 | 2019-10-17 22:24:02 -0700 | [diff] [blame] | 1372 | { "vddio", 10000 }, |
| 1373 | { "vddxo", 80000 }, |
| 1374 | { "vddrf", 300000 }, |
| 1375 | { "vddch0", 450000 }, |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1376 | }, |
| 1377 | .num_vregs = 4, |
| 1378 | }; |
| 1379 | |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 1380 | static void qca_power_shutdown(struct hci_uart *hu) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1381 | { |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1382 | struct qca_serdev *qcadev; |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1383 | struct qca_data *qca = hu->priv; |
| 1384 | unsigned long flags; |
| 1385 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1386 | qcadev = serdev_device_get_drvdata(hu->serdev); |
| 1387 | |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1388 | /* From this point we go into power off state. But serial port is |
| 1389 | * still open, stop queueing the IBS data and flush all the buffered |
| 1390 | * data in skb's. |
| 1391 | */ |
| 1392 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
Matthias Kaehlcke | 62a9199 | 2019-04-29 16:21:30 -0700 | [diff] [blame] | 1393 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
Balakrishna Godavarthi | 035a960 | 2019-02-04 20:36:43 +0530 | [diff] [blame] | 1394 | qca_flush(hu); |
| 1395 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 1396 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1397 | host_set_baudrate(hu, 2400); |
Matthias Kaehlcke | 9836b80 | 2019-02-26 11:46:45 -0800 | [diff] [blame] | 1398 | qca_send_power_pulse(hu, false); |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1399 | qca_regulator_disable(qcadev); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1400 | } |
| 1401 | |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1402 | static int qca_power_off(struct hci_dev *hdev) |
| 1403 | { |
| 1404 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1405 | |
Harish Bandi | a278088 | 2019-07-12 10:39:40 +0530 | [diff] [blame] | 1406 | /* Perform pre shutdown command */ |
| 1407 | qca_send_pre_shutdown_cmd(hdev); |
| 1408 | |
Harish Bandi | 010376a | 2019-08-30 17:58:36 +0530 | [diff] [blame] | 1409 | usleep_range(8000, 10000); |
| 1410 | |
Balakrishna Godavarthi | 3e4be65 | 2018-09-24 20:14:45 +0530 | [diff] [blame] | 1411 | qca_power_shutdown(hu); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1415 | static int qca_regulator_enable(struct qca_serdev *qcadev) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1416 | { |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1417 | struct qca_power *power = qcadev->bt_power; |
| 1418 | int ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1419 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1420 | /* Already enabled */ |
| 1421 | if (power->vregs_on) |
| 1422 | return 0; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1423 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1424 | BT_DBG("enabling %d regulators)", power->num_vregs); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1425 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1426 | ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk); |
| 1427 | if (ret) |
| 1428 | return ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1429 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1430 | power->vregs_on = true; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1431 | |
Bjorn Andersson | 163d42f | 2019-10-17 22:24:03 -0700 | [diff] [blame] | 1432 | return 0; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1433 | } |
| 1434 | |
Bjorn Andersson | a9314e7 | 2019-10-17 22:24:04 -0700 | [diff] [blame] | 1435 | static void qca_regulator_disable(struct qca_serdev *qcadev) |
| 1436 | { |
| 1437 | struct qca_power *power; |
| 1438 | |
| 1439 | if (!qcadev) |
| 1440 | return; |
| 1441 | |
| 1442 | power = qcadev->bt_power; |
| 1443 | |
| 1444 | /* Already disabled? */ |
| 1445 | if (!power->vregs_on) |
| 1446 | return; |
| 1447 | |
| 1448 | regulator_bulk_disable(power->num_vregs, power->vreg_bulk); |
| 1449 | power->vregs_on = false; |
| 1450 | } |
| 1451 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1452 | static int qca_init_regulators(struct qca_power *qca, |
| 1453 | const struct qca_vreg *vregs, size_t num_vregs) |
| 1454 | { |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1455 | struct regulator_bulk_data *bulk; |
| 1456 | int ret; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1457 | int i; |
| 1458 | |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1459 | bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL); |
| 1460 | if (!bulk) |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1461 | return -ENOMEM; |
| 1462 | |
| 1463 | for (i = 0; i < num_vregs; i++) |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1464 | bulk[i].supply = vregs[i].name; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1465 | |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1466 | ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk); |
| 1467 | if (ret < 0) |
| 1468 | return ret; |
| 1469 | |
| 1470 | for (i = 0; i < num_vregs; i++) { |
| 1471 | ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA); |
| 1472 | if (ret) |
| 1473 | return ret; |
| 1474 | } |
| 1475 | |
| 1476 | qca->vreg_bulk = bulk; |
Bjorn Andersson | 163d42f | 2019-10-17 22:24:03 -0700 | [diff] [blame] | 1477 | qca->num_vregs = num_vregs; |
Bjorn Andersson | c29ff10 | 2019-10-17 22:24:01 -0700 | [diff] [blame] | 1478 | |
| 1479 | return 0; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1480 | } |
| 1481 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1482 | static int qca_serdev_probe(struct serdev_device *serdev) |
| 1483 | { |
| 1484 | struct qca_serdev *qcadev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1485 | const struct qca_vreg_data *data; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1486 | int err; |
| 1487 | |
| 1488 | qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); |
| 1489 | if (!qcadev) |
| 1490 | return -ENOMEM; |
| 1491 | |
| 1492 | qcadev->serdev_hu.serdev = serdev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1493 | data = of_device_get_match_data(&serdev->dev); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1494 | serdev_device_set_drvdata(serdev, qcadev); |
Rocky Liao | 99c905c | 2019-06-06 17:40:30 +0800 | [diff] [blame] | 1495 | device_property_read_string(&serdev->dev, "firmware-name", |
| 1496 | &qcadev->firmware_name); |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1497 | if (data && qca_is_wcn399x(data->soc_type)) { |
| 1498 | qcadev->btsoc_type = data->soc_type; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1499 | qcadev->bt_power = devm_kzalloc(&serdev->dev, |
| 1500 | sizeof(struct qca_power), |
| 1501 | GFP_KERNEL); |
| 1502 | if (!qcadev->bt_power) |
| 1503 | return -ENOMEM; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1504 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1505 | qcadev->bt_power->dev = &serdev->dev; |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1506 | err = qca_init_regulators(qcadev->bt_power, data->vregs, |
| 1507 | data->num_vregs); |
| 1508 | if (err) { |
| 1509 | BT_ERR("Failed to init regulators:%d", err); |
| 1510 | goto out; |
| 1511 | } |
| 1512 | |
| 1513 | qcadev->bt_power->vregs_on = false; |
| 1514 | |
| 1515 | device_property_read_u32(&serdev->dev, "max-speed", |
| 1516 | &qcadev->oper_speed); |
| 1517 | if (!qcadev->oper_speed) |
| 1518 | BT_DBG("UART will pick default operating speed"); |
| 1519 | |
| 1520 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
| 1521 | if (err) { |
| 1522 | BT_ERR("wcn3990 serdev registration failed"); |
| 1523 | goto out; |
| 1524 | } |
| 1525 | } else { |
| 1526 | qcadev->btsoc_type = QCA_ROME; |
| 1527 | qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable", |
| 1528 | GPIOD_OUT_LOW); |
| 1529 | if (IS_ERR(qcadev->bt_en)) { |
| 1530 | dev_err(&serdev->dev, "failed to acquire enable gpio\n"); |
| 1531 | return PTR_ERR(qcadev->bt_en); |
| 1532 | } |
| 1533 | |
| 1534 | qcadev->susclk = devm_clk_get(&serdev->dev, NULL); |
| 1535 | if (IS_ERR(qcadev->susclk)) { |
| 1536 | dev_err(&serdev->dev, "failed to acquire clk\n"); |
| 1537 | return PTR_ERR(qcadev->susclk); |
| 1538 | } |
| 1539 | |
| 1540 | err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); |
| 1541 | if (err) |
| 1542 | return err; |
| 1543 | |
| 1544 | err = clk_prepare_enable(qcadev->susclk); |
| 1545 | if (err) |
| 1546 | return err; |
| 1547 | |
| 1548 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
| 1549 | if (err) |
| 1550 | clk_disable_unprepare(qcadev->susclk); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1553 | out: return err; |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1554 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static void qca_serdev_remove(struct serdev_device *serdev) |
| 1558 | { |
| 1559 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); |
| 1560 | |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1561 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
Balakrishna Godavarthi | c2d7827 | 2018-08-22 17:50:05 +0530 | [diff] [blame] | 1562 | qca_power_shutdown(&qcadev->serdev_hu); |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1563 | else |
| 1564 | clk_disable_unprepare(qcadev->susclk); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1565 | |
Balakrishna Godavarthi | fa9ad87 | 2018-08-03 17:46:32 +0530 | [diff] [blame] | 1566 | hci_uart_unregister_device(&qcadev->serdev_hu); |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 1569 | static int __maybe_unused qca_suspend(struct device *dev) |
| 1570 | { |
| 1571 | struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); |
| 1572 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1573 | struct qca_data *qca = hu->priv; |
| 1574 | unsigned long flags; |
| 1575 | int ret = 0; |
| 1576 | u8 cmd; |
| 1577 | |
| 1578 | set_bit(QCA_SUSPENDING, &qca->flags); |
| 1579 | |
| 1580 | /* Device is downloading patch or doesn't support in-band sleep. */ |
| 1581 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) |
| 1582 | return 0; |
| 1583 | |
| 1584 | cancel_work_sync(&qca->ws_awake_device); |
| 1585 | cancel_work_sync(&qca->ws_awake_rx); |
| 1586 | |
| 1587 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, |
| 1588 | flags, SINGLE_DEPTH_NESTING); |
| 1589 | |
| 1590 | switch (qca->tx_ibs_state) { |
| 1591 | case HCI_IBS_TX_WAKING: |
| 1592 | del_timer(&qca->wake_retrans_timer); |
| 1593 | /* Fall through */ |
| 1594 | case HCI_IBS_TX_AWAKE: |
| 1595 | del_timer(&qca->tx_idle_timer); |
| 1596 | |
| 1597 | serdev_device_write_flush(hu->serdev); |
| 1598 | cmd = HCI_IBS_SLEEP_IND; |
| 1599 | ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); |
| 1600 | |
| 1601 | if (ret < 0) { |
| 1602 | BT_ERR("Failed to send SLEEP to device"); |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; |
| 1607 | qca->ibs_sent_slps++; |
| 1608 | |
| 1609 | qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off); |
| 1610 | break; |
| 1611 | |
| 1612 | case HCI_IBS_TX_ASLEEP: |
| 1613 | break; |
| 1614 | |
| 1615 | default: |
| 1616 | BT_ERR("Spurious tx state %d", qca->tx_ibs_state); |
| 1617 | ret = -EINVAL; |
| 1618 | break; |
| 1619 | } |
| 1620 | |
| 1621 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
| 1622 | |
| 1623 | if (ret < 0) |
| 1624 | goto error; |
| 1625 | |
| 1626 | serdev_device_wait_until_sent(hu->serdev, |
| 1627 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); |
| 1628 | |
| 1629 | /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going |
| 1630 | * to sleep, so that the packet does not wake the system later. |
| 1631 | */ |
| 1632 | |
| 1633 | ret = wait_event_interruptible_timeout(qca->suspend_wait_q, |
| 1634 | qca->rx_ibs_state == HCI_IBS_RX_ASLEEP, |
| 1635 | msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS)); |
| 1636 | |
| 1637 | if (ret > 0) |
| 1638 | return 0; |
| 1639 | |
| 1640 | if (ret == 0) |
| 1641 | ret = -ETIMEDOUT; |
| 1642 | |
| 1643 | error: |
| 1644 | clear_bit(QCA_SUSPENDING, &qca->flags); |
| 1645 | |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static int __maybe_unused qca_resume(struct device *dev) |
| 1650 | { |
| 1651 | struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); |
| 1652 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1653 | struct qca_data *qca = hu->priv; |
| 1654 | |
| 1655 | clear_bit(QCA_SUSPENDING, &qca->flags); |
| 1656 | |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
| 1660 | static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume); |
| 1661 | |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1662 | static const struct of_device_id qca_bluetooth_of_match[] = { |
| 1663 | { .compatible = "qcom,qca6174-bt" }, |
Harish Bandi | 523760b | 2019-04-26 19:26:01 +0530 | [diff] [blame] | 1664 | { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990}, |
| 1665 | { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998}, |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1666 | { /* sentinel */ } |
| 1667 | }; |
| 1668 | MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); |
| 1669 | |
| 1670 | static struct serdev_device_driver qca_serdev_driver = { |
| 1671 | .probe = qca_serdev_probe, |
| 1672 | .remove = qca_serdev_remove, |
| 1673 | .driver = { |
| 1674 | .name = "hci_uart_qca", |
| 1675 | .of_match_table = qca_bluetooth_of_match, |
Claire Chang | 41d5b25 | 2019-10-31 18:46:14 +0800 | [diff] [blame] | 1676 | .pm = &qca_pm_ops, |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1677 | }, |
| 1678 | }; |
| 1679 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1680 | int __init qca_init(void) |
| 1681 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1682 | serdev_device_driver_register(&qca_serdev_driver); |
| 1683 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1684 | return hci_uart_register_proto(&qca_proto); |
| 1685 | } |
| 1686 | |
| 1687 | int __exit qca_deinit(void) |
| 1688 | { |
Thierry Escande | 05ba533 | 2018-03-29 21:15:24 +0200 | [diff] [blame] | 1689 | serdev_device_driver_unregister(&qca_serdev_driver); |
| 1690 | |
Ben Young Tae Kim | 0ff252c | 2015-08-10 14:24:17 -0700 | [diff] [blame] | 1691 | return hci_uart_unregister_proto(&qca_proto); |
| 1692 | } |