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