Marcel Holtmann | e9a2dd2 | 2015-04-04 16:13:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Bluetooth HCI UART driver for Broadcom devices |
| 4 | * |
| 5 | * Copyright (C) 2015 Intel Corporation |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/skbuff.h> |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 27 | #include <linux/firmware.h> |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/acpi.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/gpio/consumer.h> |
| 33 | #include <linux/tty.h> |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
Frederic Danis | 5cebdfe | 2015-09-23 18:18:08 +0200 | [diff] [blame] | 35 | #include <linux/dmi.h> |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> |
Marcel Holtmann | e9a2dd2 | 2015-04-04 16:13:03 -0700 | [diff] [blame] | 37 | |
| 38 | #include <net/bluetooth/bluetooth.h> |
| 39 | #include <net/bluetooth/hci_core.h> |
| 40 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 41 | #include "btbcm.h" |
Marcel Holtmann | e9a2dd2 | 2015-04-04 16:13:03 -0700 | [diff] [blame] | 42 | #include "hci_uart.h" |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 43 | |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 44 | #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */ |
| 45 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 46 | struct bcm_device { |
| 47 | struct list_head list; |
| 48 | |
| 49 | struct platform_device *pdev; |
| 50 | |
| 51 | const char *name; |
| 52 | struct gpio_desc *device_wakeup; |
| 53 | struct gpio_desc *shutdown; |
| 54 | |
| 55 | struct clk *clk; |
| 56 | bool clk_enabled; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 57 | |
| 58 | u32 init_speed; |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 59 | int irq; |
| 60 | u8 irq_polarity; |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 61 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 62 | #ifdef CONFIG_PM |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 63 | struct hci_uart *hu; |
| 64 | bool is_suspended; /* suspend/resume flag */ |
| 65 | #endif |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 68 | struct bcm_data { |
| 69 | struct sk_buff *rx_skb; |
| 70 | struct sk_buff_head txq; |
| 71 | |
| 72 | struct bcm_device *dev; |
| 73 | }; |
| 74 | |
| 75 | /* List of BCM BT UART devices */ |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 76 | static DEFINE_MUTEX(bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 77 | static LIST_HEAD(bcm_device_list); |
| 78 | |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 79 | static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 80 | { |
| 81 | struct hci_dev *hdev = hu->hdev; |
| 82 | struct sk_buff *skb; |
| 83 | struct bcm_update_uart_baud_rate param; |
| 84 | |
| 85 | if (speed > 3000000) { |
| 86 | struct bcm_write_uart_clock_setting clock; |
| 87 | |
| 88 | clock.type = BCM_UART_CLOCK_48MHZ; |
| 89 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 90 | bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 91 | |
| 92 | /* This Broadcom specific command changes the UART's controller |
| 93 | * clock for baud rate > 3000000. |
| 94 | */ |
| 95 | skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT); |
| 96 | if (IS_ERR(skb)) { |
| 97 | int err = PTR_ERR(skb); |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 98 | bt_dev_err(hdev, "BCM: failed to write clock (%d)", |
| 99 | err); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
| 103 | kfree_skb(skb); |
| 104 | } |
| 105 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 106 | bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 107 | |
| 108 | param.zero = cpu_to_le16(0); |
| 109 | param.baud_rate = cpu_to_le32(speed); |
| 110 | |
| 111 | /* This Broadcom specific command changes the UART's controller baud |
| 112 | * rate. |
| 113 | */ |
| 114 | skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m, |
| 115 | HCI_INIT_TIMEOUT); |
| 116 | if (IS_ERR(skb)) { |
| 117 | int err = PTR_ERR(skb); |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 118 | bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)", |
| 119 | err); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 120 | return err; |
| 121 | } |
| 122 | |
| 123 | kfree_skb(skb); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 128 | /* bcm_device_exists should be protected by bcm_device_lock */ |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 129 | static bool bcm_device_exists(struct bcm_device *device) |
| 130 | { |
| 131 | struct list_head *p; |
| 132 | |
| 133 | list_for_each(p, &bcm_device_list) { |
| 134 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); |
| 135 | |
| 136 | if (device == dev) |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | static int bcm_gpio_set_power(struct bcm_device *dev, bool powered) |
| 144 | { |
| 145 | if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) |
| 146 | clk_enable(dev->clk); |
| 147 | |
Loic Poulain | 2af0a70 | 2015-08-17 16:00:21 +0200 | [diff] [blame] | 148 | gpiod_set_value(dev->shutdown, powered); |
| 149 | gpiod_set_value(dev->device_wakeup, powered); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 150 | |
| 151 | if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled) |
| 152 | clk_disable(dev->clk); |
| 153 | |
| 154 | dev->clk_enabled = powered; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 159 | #ifdef CONFIG_PM |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 160 | static irqreturn_t bcm_host_wake(int irq, void *data) |
| 161 | { |
| 162 | struct bcm_device *bdev = data; |
| 163 | |
| 164 | bt_dev_dbg(bdev, "Host wake IRQ"); |
| 165 | |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 166 | pm_runtime_get(&bdev->pdev->dev); |
| 167 | pm_runtime_mark_last_busy(&bdev->pdev->dev); |
| 168 | pm_runtime_put_autosuspend(&bdev->pdev->dev); |
| 169 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 170 | return IRQ_HANDLED; |
| 171 | } |
| 172 | |
| 173 | static int bcm_request_irq(struct bcm_data *bcm) |
| 174 | { |
| 175 | struct bcm_device *bdev = bcm->dev; |
| 176 | int err = 0; |
| 177 | |
| 178 | /* If this is not a platform device, do not enable PM functionalities */ |
| 179 | mutex_lock(&bcm_device_lock); |
| 180 | if (!bcm_device_exists(bdev)) { |
| 181 | err = -ENODEV; |
| 182 | goto unlock; |
| 183 | } |
| 184 | |
| 185 | if (bdev->irq > 0) { |
| 186 | err = devm_request_irq(&bdev->pdev->dev, bdev->irq, |
| 187 | bcm_host_wake, IRQF_TRIGGER_RISING, |
| 188 | "host_wake", bdev); |
| 189 | if (err) |
| 190 | goto unlock; |
| 191 | |
| 192 | device_init_wakeup(&bdev->pdev->dev, true); |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 193 | |
| 194 | pm_runtime_set_autosuspend_delay(&bdev->pdev->dev, |
| 195 | BCM_AUTOSUSPEND_DELAY); |
| 196 | pm_runtime_use_autosuspend(&bdev->pdev->dev); |
| 197 | pm_runtime_set_active(&bdev->pdev->dev); |
| 198 | pm_runtime_enable(&bdev->pdev->dev); |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | unlock: |
| 202 | mutex_unlock(&bcm_device_lock); |
| 203 | |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | static const struct bcm_set_sleep_mode default_sleep_params = { |
| 208 | .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */ |
| 209 | .idle_host = 2, /* idle threshold HOST, in 300ms */ |
| 210 | .idle_dev = 2, /* idle threshold device, in 300ms */ |
| 211 | .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */ |
| 212 | .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */ |
| 213 | .allow_host_sleep = 1, /* Allow host sleep in SCO flag */ |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 214 | .combine_modes = 1, /* Combine sleep and LPM flag */ |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 215 | .tristate_control = 0, /* Allow tri-state control of UART tx flag */ |
| 216 | /* Irrelevant USB flags */ |
| 217 | .usb_auto_sleep = 0, |
| 218 | .usb_resume_timeout = 0, |
| 219 | .pulsed_host_wake = 0, |
| 220 | .break_to_host = 0 |
| 221 | }; |
| 222 | |
| 223 | static int bcm_setup_sleep(struct hci_uart *hu) |
| 224 | { |
| 225 | struct bcm_data *bcm = hu->priv; |
| 226 | struct sk_buff *skb; |
| 227 | struct bcm_set_sleep_mode sleep_params = default_sleep_params; |
| 228 | |
| 229 | sleep_params.host_wake_active = !bcm->dev->irq_polarity; |
| 230 | |
| 231 | skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params), |
| 232 | &sleep_params, HCI_INIT_TIMEOUT); |
| 233 | if (IS_ERR(skb)) { |
| 234 | int err = PTR_ERR(skb); |
| 235 | bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err); |
| 236 | return err; |
| 237 | } |
| 238 | kfree_skb(skb); |
| 239 | |
| 240 | bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded"); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | #else |
| 245 | static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; } |
| 246 | static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; } |
| 247 | #endif |
| 248 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 249 | static int bcm_open(struct hci_uart *hu) |
| 250 | { |
| 251 | struct bcm_data *bcm; |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 252 | struct list_head *p; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 253 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 254 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 255 | |
| 256 | bcm = kzalloc(sizeof(*bcm), GFP_KERNEL); |
| 257 | if (!bcm) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | skb_queue_head_init(&bcm->txq); |
| 261 | |
| 262 | hu->priv = bcm; |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 263 | |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 264 | mutex_lock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 265 | list_for_each(p, &bcm_device_list) { |
| 266 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); |
| 267 | |
| 268 | /* Retrieve saved bcm_device based on parent of the |
| 269 | * platform device (saved during device probe) and |
| 270 | * parent of tty device used by hci_uart |
| 271 | */ |
| 272 | if (hu->tty->dev->parent == dev->pdev->dev.parent) { |
| 273 | bcm->dev = dev; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 274 | hu->init_speed = dev->init_speed; |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 275 | #ifdef CONFIG_PM |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 276 | dev->hu = hu; |
| 277 | #endif |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 278 | bcm_gpio_set_power(bcm->dev, true); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 283 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 284 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int bcm_close(struct hci_uart *hu) |
| 289 | { |
| 290 | struct bcm_data *bcm = hu->priv; |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 291 | struct bcm_device *bdev = bcm->dev; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 292 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 293 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 294 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 295 | /* Protect bcm->dev against removal of the device or driver */ |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 296 | mutex_lock(&bcm_device_lock); |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 297 | if (bcm_device_exists(bdev)) { |
| 298 | bcm_gpio_set_power(bdev, false); |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 299 | #ifdef CONFIG_PM |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 300 | pm_runtime_disable(&bdev->pdev->dev); |
| 301 | pm_runtime_set_suspended(&bdev->pdev->dev); |
| 302 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 303 | if (device_can_wakeup(&bdev->pdev->dev)) { |
| 304 | devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev); |
| 305 | device_init_wakeup(&bdev->pdev->dev, false); |
| 306 | } |
| 307 | |
| 308 | bdev->hu = NULL; |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 309 | #endif |
| 310 | } |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 311 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 312 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 313 | skb_queue_purge(&bcm->txq); |
| 314 | kfree_skb(bcm->rx_skb); |
| 315 | kfree(bcm); |
| 316 | |
| 317 | hu->priv = NULL; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int bcm_flush(struct hci_uart *hu) |
| 322 | { |
| 323 | struct bcm_data *bcm = hu->priv; |
| 324 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 325 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 326 | |
| 327 | skb_queue_purge(&bcm->txq); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int bcm_setup(struct hci_uart *hu) |
| 333 | { |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 334 | struct bcm_data *bcm = hu->priv; |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 335 | char fw_name[64]; |
| 336 | const struct firmware *fw; |
Frederic Danis | 960ef1d | 2015-06-18 12:43:27 +0200 | [diff] [blame] | 337 | unsigned int speed; |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 338 | int err; |
| 339 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 340 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 341 | |
| 342 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; |
| 343 | |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 344 | err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name)); |
| 345 | if (err) |
| 346 | return err; |
| 347 | |
| 348 | err = request_firmware(&fw, fw_name, &hu->hdev->dev); |
| 349 | if (err < 0) { |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 350 | bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name); |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | err = btbcm_patchram(hu->hdev, fw); |
| 355 | if (err) { |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 356 | bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err); |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 357 | goto finalize; |
| 358 | } |
| 359 | |
Frederic Danis | 960ef1d | 2015-06-18 12:43:27 +0200 | [diff] [blame] | 360 | /* Init speed if any */ |
| 361 | if (hu->init_speed) |
| 362 | speed = hu->init_speed; |
| 363 | else if (hu->proto->init_speed) |
| 364 | speed = hu->proto->init_speed; |
| 365 | else |
| 366 | speed = 0; |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 367 | |
Frederic Danis | 960ef1d | 2015-06-18 12:43:27 +0200 | [diff] [blame] | 368 | if (speed) |
| 369 | hci_uart_set_baudrate(hu, speed); |
| 370 | |
| 371 | /* Operational speed if any */ |
| 372 | if (hu->oper_speed) |
| 373 | speed = hu->oper_speed; |
| 374 | else if (hu->proto->oper_speed) |
| 375 | speed = hu->proto->oper_speed; |
| 376 | else |
| 377 | speed = 0; |
| 378 | |
| 379 | if (speed) { |
| 380 | err = bcm_set_baudrate(hu, speed); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 381 | if (!err) |
Frederic Danis | 960ef1d | 2015-06-18 12:43:27 +0200 | [diff] [blame] | 382 | hci_uart_set_baudrate(hu, speed); |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 385 | finalize: |
| 386 | release_firmware(fw); |
| 387 | |
| 388 | err = btbcm_finalize(hu->hdev); |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 389 | if (err) |
| 390 | return err; |
| 391 | |
| 392 | err = bcm_request_irq(bcm); |
| 393 | if (!err) |
| 394 | err = bcm_setup_sleep(hu); |
Frederic Danis | 6be09b4 | 2015-05-28 11:25:05 +0200 | [diff] [blame] | 395 | |
| 396 | return err; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Marcel Holtmann | 79b8df9 | 2015-04-05 23:44:59 -0700 | [diff] [blame] | 399 | static const struct h4_recv_pkt bcm_recv_pkts[] = { |
| 400 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
| 401 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
| 402 | { H4_RECV_EVENT, .recv = hci_recv_frame }, |
| 403 | }; |
| 404 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 405 | static int bcm_recv(struct hci_uart *hu, const void *data, int count) |
| 406 | { |
| 407 | struct bcm_data *bcm = hu->priv; |
| 408 | |
| 409 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) |
| 410 | return -EUNATCH; |
| 411 | |
Marcel Holtmann | 79b8df9 | 2015-04-05 23:44:59 -0700 | [diff] [blame] | 412 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
| 413 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 414 | if (IS_ERR(bcm->rx_skb)) { |
| 415 | int err = PTR_ERR(bcm->rx_skb); |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 416 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
Chan-yeol Park | 3713416 | 2015-06-17 21:10:39 +0900 | [diff] [blame] | 417 | bcm->rx_skb = NULL; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 418 | return err; |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 419 | } else if (!bcm->rx_skb) { |
| 420 | /* Delay auto-suspend when receiving completed packet */ |
| 421 | mutex_lock(&bcm_device_lock); |
| 422 | if (bcm->dev && bcm_device_exists(bcm->dev)) { |
| 423 | pm_runtime_get(&bcm->dev->pdev->dev); |
| 424 | pm_runtime_mark_last_busy(&bcm->dev->pdev->dev); |
| 425 | pm_runtime_put_autosuspend(&bcm->dev->pdev->dev); |
| 426 | } |
| 427 | mutex_unlock(&bcm_device_lock); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return count; |
| 431 | } |
| 432 | |
| 433 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 434 | { |
| 435 | struct bcm_data *bcm = hu->priv; |
| 436 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 437 | bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb); |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 438 | |
| 439 | /* Prepend skb with frame type */ |
| 440 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
| 441 | skb_queue_tail(&bcm->txq, skb); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) |
| 447 | { |
| 448 | struct bcm_data *bcm = hu->priv; |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 449 | struct sk_buff *skb = NULL; |
| 450 | struct bcm_device *bdev = NULL; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 451 | |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 452 | mutex_lock(&bcm_device_lock); |
| 453 | |
| 454 | if (bcm_device_exists(bcm->dev)) { |
| 455 | bdev = bcm->dev; |
| 456 | pm_runtime_get_sync(&bdev->pdev->dev); |
| 457 | /* Shall be resumed here */ |
| 458 | } |
| 459 | |
| 460 | skb = skb_dequeue(&bcm->txq); |
| 461 | |
| 462 | if (bdev) { |
| 463 | pm_runtime_mark_last_busy(&bdev->pdev->dev); |
| 464 | pm_runtime_put_autosuspend(&bdev->pdev->dev); |
| 465 | } |
| 466 | |
| 467 | mutex_unlock(&bcm_device_lock); |
| 468 | |
| 469 | return skb; |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 472 | #ifdef CONFIG_PM |
| 473 | static int bcm_suspend_device(struct device *dev) |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 474 | { |
| 475 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 476 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 477 | bt_dev_dbg(bdev, ""); |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 478 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 479 | if (!bdev->is_suspended && bdev->hu) { |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 480 | hci_uart_set_flow_control(bdev->hu, true); |
| 481 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 482 | /* Once this returns, driver suspends BT via GPIO */ |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 483 | bdev->is_suspended = true; |
| 484 | } |
| 485 | |
| 486 | /* Suspend the device */ |
| 487 | if (bdev->device_wakeup) { |
| 488 | gpiod_set_value(bdev->device_wakeup, false); |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 489 | bt_dev_dbg(bdev, "suspend, delaying 15 ms"); |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 490 | mdelay(15); |
| 491 | } |
| 492 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int bcm_resume_device(struct device *dev) |
| 497 | { |
| 498 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 499 | |
| 500 | bt_dev_dbg(bdev, ""); |
| 501 | |
| 502 | if (bdev->device_wakeup) { |
| 503 | gpiod_set_value(bdev->device_wakeup, true); |
| 504 | bt_dev_dbg(bdev, "resume, delaying 15 ms"); |
| 505 | mdelay(15); |
| 506 | } |
| 507 | |
| 508 | /* When this executes, the device has woken up already */ |
| 509 | if (bdev->is_suspended && bdev->hu) { |
| 510 | bdev->is_suspended = false; |
| 511 | |
| 512 | hci_uart_set_flow_control(bdev->hu, false); |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | #ifdef CONFIG_PM_SLEEP |
| 520 | /* Platform suspend callback */ |
| 521 | static int bcm_suspend(struct device *dev) |
| 522 | { |
| 523 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 524 | int error; |
| 525 | |
| 526 | bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended); |
| 527 | |
| 528 | /* bcm_suspend can be called at any time as long as platform device is |
| 529 | * bound, so it should use bcm_device_lock to protect access to hci_uart |
| 530 | * and device_wake-up GPIO. |
| 531 | */ |
| 532 | mutex_lock(&bcm_device_lock); |
| 533 | |
| 534 | if (!bdev->hu) |
| 535 | goto unlock; |
| 536 | |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 537 | if (pm_runtime_active(dev)) |
| 538 | bcm_suspend_device(dev); |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 539 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 540 | if (device_may_wakeup(&bdev->pdev->dev)) { |
| 541 | error = enable_irq_wake(bdev->irq); |
| 542 | if (!error) |
| 543 | bt_dev_dbg(bdev, "BCM irq: enabled"); |
| 544 | } |
| 545 | |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 546 | unlock: |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 547 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 548 | |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* Platform resume callback */ |
| 553 | static int bcm_resume(struct device *dev) |
| 554 | { |
| 555 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 556 | |
Frederic Danis | 65ad07c | 2015-09-01 12:13:36 +0200 | [diff] [blame] | 557 | bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended); |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 558 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 559 | /* bcm_resume can be called at any time as long as platform device is |
| 560 | * bound, so it should use bcm_device_lock to protect access to hci_uart |
| 561 | * and device_wake-up GPIO. |
| 562 | */ |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 563 | mutex_lock(&bcm_device_lock); |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 564 | |
| 565 | if (!bdev->hu) |
| 566 | goto unlock; |
| 567 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 568 | if (device_may_wakeup(&bdev->pdev->dev)) { |
| 569 | disable_irq_wake(bdev->irq); |
| 570 | bt_dev_dbg(bdev, "BCM irq: disabled"); |
| 571 | } |
| 572 | |
Frederic Danis | b7a622a | 2015-09-23 18:18:09 +0200 | [diff] [blame] | 573 | bcm_resume_device(dev); |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 574 | |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 575 | unlock: |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 576 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 917522a | 2015-08-28 15:44:00 +0200 | [diff] [blame] | 577 | |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 578 | pm_runtime_disable(dev); |
| 579 | pm_runtime_set_active(dev); |
| 580 | pm_runtime_enable(dev); |
| 581 | |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | #endif |
| 585 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 586 | static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false }; |
| 587 | static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false }; |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 588 | static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false }; |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 589 | |
| 590 | static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = { |
| 591 | { "device-wakeup-gpios", &device_wakeup_gpios, 1 }, |
| 592 | { "shutdown-gpios", &shutdown_gpios, 1 }, |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 593 | { "host-wakeup-gpios", &host_wakeup_gpios, 1 }, |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 594 | { }, |
| 595 | }; |
| 596 | |
Frederic Danis | 50d78bc | 2015-08-12 12:46:01 +0200 | [diff] [blame] | 597 | #ifdef CONFIG_ACPI |
Frederic Danis | 5cebdfe | 2015-09-23 18:18:08 +0200 | [diff] [blame] | 598 | static u8 acpi_active_low = ACPI_ACTIVE_LOW; |
| 599 | |
| 600 | /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */ |
| 601 | static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = { |
| 602 | { |
| 603 | .ident = "Asus T100TA", |
| 604 | .matches = { |
| 605 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, |
| 606 | "ASUSTeK COMPUTER INC."), |
| 607 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"), |
| 608 | }, |
| 609 | .driver_data = &acpi_active_low, |
| 610 | }, |
| 611 | { } |
| 612 | }; |
| 613 | |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 614 | static int bcm_resource(struct acpi_resource *ares, void *data) |
| 615 | { |
| 616 | struct bcm_device *dev = data; |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 617 | struct acpi_resource_extended_irq *irq; |
| 618 | struct acpi_resource_gpio *gpio; |
| 619 | struct acpi_resource_uart_serialbus *sb; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 620 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 621 | switch (ares->type) { |
| 622 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 623 | irq = &ares->data.extended_irq; |
| 624 | dev->irq_polarity = irq->polarity; |
| 625 | break; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 626 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 627 | case ACPI_RESOURCE_TYPE_GPIO: |
| 628 | gpio = &ares->data.gpio; |
| 629 | if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) |
| 630 | dev->irq_polarity = gpio->polarity; |
| 631 | break; |
| 632 | |
| 633 | case ACPI_RESOURCE_TYPE_SERIAL_BUS: |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 634 | sb = &ares->data.uart_serial_bus; |
| 635 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) |
| 636 | dev->init_speed = sb->default_baud_rate; |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 637 | break; |
| 638 | |
| 639 | default: |
| 640 | break; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Always tell the ACPI core to skip this resource */ |
| 644 | return 1; |
| 645 | } |
| 646 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 647 | static int bcm_acpi_probe(struct bcm_device *dev) |
| 648 | { |
| 649 | struct platform_device *pdev = dev->pdev; |
| 650 | const struct acpi_device_id *id; |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 651 | struct acpi_device *adev; |
| 652 | LIST_HEAD(resources); |
Frederic Danis | 5cebdfe | 2015-09-23 18:18:08 +0200 | [diff] [blame] | 653 | const struct dmi_system_id *dmi_id; |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 654 | int ret; |
| 655 | |
| 656 | id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); |
| 657 | if (!id) |
| 658 | return -ENODEV; |
| 659 | |
| 660 | /* Retrieve GPIO data */ |
| 661 | dev->name = dev_name(&pdev->dev); |
| 662 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev), |
| 663 | acpi_bcm_default_gpios); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | |
| 667 | dev->clk = devm_clk_get(&pdev->dev, NULL); |
| 668 | |
Uwe Kleine-König | 62aaefa | 2015-08-12 09:20:56 +0200 | [diff] [blame] | 669 | dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev, |
| 670 | "device-wakeup", |
| 671 | GPIOD_OUT_LOW); |
| 672 | if (IS_ERR(dev->device_wakeup)) |
| 673 | return PTR_ERR(dev->device_wakeup); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 674 | |
Uwe Kleine-König | 62aaefa | 2015-08-12 09:20:56 +0200 | [diff] [blame] | 675 | dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown", |
| 676 | GPIOD_OUT_LOW); |
| 677 | if (IS_ERR(dev->shutdown)) |
| 678 | return PTR_ERR(dev->shutdown); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 679 | |
Frederic Danis | 6cc4396 | 2015-09-04 15:35:44 +0200 | [diff] [blame] | 680 | /* IRQ can be declared in ACPI table as Interrupt or GpioInt */ |
| 681 | dev->irq = platform_get_irq(pdev, 0); |
| 682 | if (dev->irq <= 0) { |
| 683 | struct gpio_desc *gpio; |
| 684 | |
| 685 | gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup", |
| 686 | GPIOD_IN); |
| 687 | if (IS_ERR(gpio)) |
| 688 | return PTR_ERR(gpio); |
| 689 | |
| 690 | dev->irq = gpiod_to_irq(gpio); |
| 691 | } |
| 692 | |
| 693 | dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq); |
| 694 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 695 | /* Make sure at-least one of the GPIO is defined and that |
| 696 | * a name is specified for this instance |
| 697 | */ |
| 698 | if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) { |
| 699 | dev_err(&pdev->dev, "invalid platform data\n"); |
| 700 | return -EINVAL; |
| 701 | } |
| 702 | |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 703 | /* Retrieve UART ACPI info */ |
| 704 | adev = ACPI_COMPANION(&dev->pdev->dev); |
| 705 | if (!adev) |
| 706 | return 0; |
| 707 | |
| 708 | acpi_dev_get_resources(adev, &resources, bcm_resource, dev); |
Jarkko Nikula | 09dbf1b | 2015-09-30 16:26:41 +0300 | [diff] [blame^] | 709 | acpi_dev_free_resource_list(&resources); |
Frederic Danis | ae05690 | 2015-08-11 16:35:37 +0200 | [diff] [blame] | 710 | |
Frederic Danis | 5cebdfe | 2015-09-23 18:18:08 +0200 | [diff] [blame] | 711 | dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table); |
| 712 | if (dmi_id) { |
| 713 | bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low", |
| 714 | dmi_id->ident); |
| 715 | dev->irq_polarity = *(u8 *)dmi_id->driver_data; |
| 716 | } |
| 717 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 718 | return 0; |
| 719 | } |
Frederic Danis | 50d78bc | 2015-08-12 12:46:01 +0200 | [diff] [blame] | 720 | #else |
| 721 | static int bcm_acpi_probe(struct bcm_device *dev) |
| 722 | { |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | #endif /* CONFIG_ACPI */ |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 726 | |
| 727 | static int bcm_probe(struct platform_device *pdev) |
| 728 | { |
| 729 | struct bcm_device *dev; |
| 730 | struct acpi_device_id *pdata = pdev->dev.platform_data; |
| 731 | int ret; |
| 732 | |
| 733 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
| 734 | if (!dev) |
| 735 | return -ENOMEM; |
| 736 | |
| 737 | dev->pdev = pdev; |
| 738 | |
| 739 | if (ACPI_HANDLE(&pdev->dev)) { |
| 740 | ret = bcm_acpi_probe(dev); |
| 741 | if (ret) |
| 742 | return ret; |
| 743 | } else if (pdata) { |
| 744 | dev->name = pdata->id; |
| 745 | } else { |
| 746 | return -ENODEV; |
| 747 | } |
| 748 | |
| 749 | platform_set_drvdata(pdev, dev); |
| 750 | |
| 751 | dev_info(&pdev->dev, "%s device registered.\n", dev->name); |
| 752 | |
| 753 | /* Place this instance on the device list */ |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 754 | mutex_lock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 755 | list_add_tail(&dev->list, &bcm_device_list); |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 756 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 757 | |
| 758 | bcm_gpio_set_power(dev, false); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static int bcm_remove(struct platform_device *pdev) |
| 764 | { |
| 765 | struct bcm_device *dev = platform_get_drvdata(pdev); |
| 766 | |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 767 | mutex_lock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 768 | list_del(&dev->list); |
Frederic Danis | bb3ea16 | 2015-09-01 12:13:35 +0200 | [diff] [blame] | 769 | mutex_unlock(&bcm_device_lock); |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 770 | |
| 771 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev)); |
| 772 | |
| 773 | dev_info(&pdev->dev, "%s device unregistered.\n", dev->name); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 778 | static const struct hci_uart_proto bcm_proto = { |
| 779 | .id = HCI_UART_BCM, |
| 780 | .name = "BCM", |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 781 | .init_speed = 115200, |
| 782 | .oper_speed = 4000000, |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 783 | .open = bcm_open, |
| 784 | .close = bcm_close, |
| 785 | .flush = bcm_flush, |
| 786 | .setup = bcm_setup, |
Frederic Danis | 61b2fc2 | 2015-06-09 16:15:37 +0200 | [diff] [blame] | 787 | .set_baudrate = bcm_set_baudrate, |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 788 | .recv = bcm_recv, |
| 789 | .enqueue = bcm_enqueue, |
| 790 | .dequeue = bcm_dequeue, |
| 791 | }; |
| 792 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 793 | #ifdef CONFIG_ACPI |
| 794 | static const struct acpi_device_id bcm_acpi_match[] = { |
| 795 | { "BCM2E39", 0 }, |
| 796 | { "BCM2E67", 0 }, |
| 797 | { }, |
| 798 | }; |
| 799 | MODULE_DEVICE_TABLE(acpi, bcm_acpi_match); |
| 800 | #endif |
| 801 | |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 802 | /* Platform suspend and resume callbacks */ |
Frederic Danis | e88ab30 | 2015-09-23 18:18:11 +0200 | [diff] [blame] | 803 | static const struct dev_pm_ops bcm_pm_ops = { |
| 804 | SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume) |
| 805 | SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL) |
| 806 | }; |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 807 | |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 808 | static struct platform_driver bcm_driver = { |
| 809 | .probe = bcm_probe, |
| 810 | .remove = bcm_remove, |
| 811 | .driver = { |
| 812 | .name = "hci_bcm", |
| 813 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), |
Frederic Danis | 118612f | 2015-08-11 16:35:38 +0200 | [diff] [blame] | 814 | .pm = &bcm_pm_ops, |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 815 | }, |
| 816 | }; |
| 817 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 818 | int __init bcm_init(void) |
| 819 | { |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 820 | platform_driver_register(&bcm_driver); |
| 821 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 822 | return hci_uart_register_proto(&bcm_proto); |
| 823 | } |
| 824 | |
| 825 | int __exit bcm_deinit(void) |
| 826 | { |
Frederic Danis | 0395ffc | 2015-08-11 16:35:35 +0200 | [diff] [blame] | 827 | platform_driver_unregister(&bcm_driver); |
| 828 | |
Marcel Holtmann | bdd8818 | 2015-04-05 22:52:18 -0700 | [diff] [blame] | 829 | return hci_uart_unregister_proto(&bcm_proto); |
| 830 | } |