Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Bluetooth HCI UART driver for Broadcom devices
0005  *
0006  *  Copyright (C) 2015  Intel Corporation
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/errno.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/firmware.h>
0013 #include <linux/module.h>
0014 #include <linux/acpi.h>
0015 #include <linux/of.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/property.h>
0018 #include <linux/platform_data/x86/apple.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regulator/consumer.h>
0021 #include <linux/clk.h>
0022 #include <linux/gpio/consumer.h>
0023 #include <linux/gpio/machine.h>
0024 #include <linux/tty.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/dmi.h>
0027 #include <linux/pm_runtime.h>
0028 #include <linux/serdev.h>
0029 
0030 #include <net/bluetooth/bluetooth.h>
0031 #include <net/bluetooth/hci_core.h>
0032 
0033 #include "btbcm.h"
0034 #include "hci_uart.h"
0035 
0036 #define BCM_NULL_PKT 0x00
0037 #define BCM_NULL_SIZE 0
0038 
0039 #define BCM_LM_DIAG_PKT 0x07
0040 #define BCM_LM_DIAG_SIZE 63
0041 
0042 #define BCM_TYPE49_PKT 0x31
0043 #define BCM_TYPE49_SIZE 0
0044 
0045 #define BCM_TYPE52_PKT 0x34
0046 #define BCM_TYPE52_SIZE 0
0047 
0048 #define BCM_AUTOSUSPEND_DELAY   5000 /* default autosleep delay */
0049 
0050 #define BCM_NUM_SUPPLIES 2
0051 
0052 /**
0053  * struct bcm_device_data - device specific data
0054  * @no_early_set_baudrate: Disallow set baudrate before driver setup()
0055  * @drive_rts_on_open: drive RTS signal on ->open() when platform requires it
0056  * @max_autobaud_speed: max baudrate supported by device in autobaud mode
0057  */
0058 struct bcm_device_data {
0059     bool    no_early_set_baudrate;
0060     bool    drive_rts_on_open;
0061     u32 max_autobaud_speed;
0062 };
0063 
0064 /**
0065  * struct bcm_device - device driver resources
0066  * @serdev_hu: HCI UART controller struct
0067  * @list: bcm_device_list node
0068  * @dev: physical UART slave
0069  * @name: device name logged by bt_dev_*() functions
0070  * @device_wakeup: BT_WAKE pin,
0071  *  assert = Bluetooth device must wake up or remain awake,
0072  *  deassert = Bluetooth device may sleep when sleep criteria are met
0073  * @shutdown: BT_REG_ON pin,
0074  *  power up or power down Bluetooth device internal regulators
0075  * @reset: BT_RST_N pin,
0076  *  active low resets the Bluetooth logic core
0077  * @set_device_wakeup: callback to toggle BT_WAKE pin
0078  *  either by accessing @device_wakeup or by calling @btlp
0079  * @set_shutdown: callback to toggle BT_REG_ON pin
0080  *  either by accessing @shutdown or by calling @btpu/@btpd
0081  * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power")
0082  * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up")
0083  * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down")
0084  * @gpio_count: internal counter for GPIO resources associated with ACPI device
0085  * @gpio_int_idx: index in _CRS for GpioInt() resource
0086  * @txco_clk: external reference frequency clock used by Bluetooth device
0087  * @lpo_clk: external LPO clock used by Bluetooth device
0088  * @supplies: VBAT and VDDIO supplies used by Bluetooth device
0089  * @res_enabled: whether clocks and supplies are prepared and enabled
0090  * @init_speed: default baudrate of Bluetooth device;
0091  *  the host UART is initially set to this baudrate so that
0092  *  it can configure the Bluetooth device for @oper_speed
0093  * @oper_speed: preferred baudrate of Bluetooth device;
0094  *  set to 0 if @init_speed is already the preferred baudrate
0095  * @irq: interrupt triggered by HOST_WAKE_BT pin
0096  * @irq_active_low: whether @irq is active low
0097  * @irq_acquired: flag to show if IRQ handler has been assigned
0098  * @hu: pointer to HCI UART controller struct,
0099  *  used to disable flow control during runtime suspend and system sleep
0100  * @is_suspended: whether flow control is currently disabled
0101  * @no_early_set_baudrate: don't set_baudrate before setup()
0102  * @drive_rts_on_open: drive RTS signal on ->open() when platform requires it
0103  * @pcm_int_params: keep the initial PCM configuration
0104  * @use_autobaud_mode: start Bluetooth device in autobaud mode
0105  * @max_autobaud_speed: max baudrate supported by device in autobaud mode
0106  */
0107 struct bcm_device {
0108     /* Must be the first member, hci_serdev.c expects this. */
0109     struct hci_uart     serdev_hu;
0110     struct list_head    list;
0111 
0112     struct device       *dev;
0113 
0114     const char      *name;
0115     struct gpio_desc    *device_wakeup;
0116     struct gpio_desc    *shutdown;
0117     struct gpio_desc    *reset;
0118     int         (*set_device_wakeup)(struct bcm_device *, bool);
0119     int         (*set_shutdown)(struct bcm_device *, bool);
0120 #ifdef CONFIG_ACPI
0121     acpi_handle     btlp, btpu, btpd;
0122     int         gpio_count;
0123     int         gpio_int_idx;
0124 #endif
0125 
0126     struct clk      *txco_clk;
0127     struct clk      *lpo_clk;
0128     struct regulator_bulk_data supplies[BCM_NUM_SUPPLIES];
0129     bool            res_enabled;
0130 
0131     u32         init_speed;
0132     u32         oper_speed;
0133     int         irq;
0134     bool            irq_active_low;
0135     bool            irq_acquired;
0136 
0137 #ifdef CONFIG_PM
0138     struct hci_uart     *hu;
0139     bool            is_suspended;
0140 #endif
0141     bool            no_early_set_baudrate;
0142     bool            drive_rts_on_open;
0143     bool            use_autobaud_mode;
0144     u8          pcm_int_params[5];
0145     u32         max_autobaud_speed;
0146 };
0147 
0148 /* generic bcm uart resources */
0149 struct bcm_data {
0150     struct sk_buff      *rx_skb;
0151     struct sk_buff_head txq;
0152 
0153     struct bcm_device   *dev;
0154 };
0155 
0156 /* List of BCM BT UART devices */
0157 static DEFINE_MUTEX(bcm_device_lock);
0158 static LIST_HEAD(bcm_device_list);
0159 
0160 static int irq_polarity = -1;
0161 module_param(irq_polarity, int, 0444);
0162 MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
0163 
0164 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
0165 {
0166     if (hu->serdev)
0167         serdev_device_set_baudrate(hu->serdev, speed);
0168     else
0169         hci_uart_set_baudrate(hu, speed);
0170 }
0171 
0172 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
0173 {
0174     struct hci_dev *hdev = hu->hdev;
0175     struct sk_buff *skb;
0176     struct bcm_update_uart_baud_rate param;
0177 
0178     if (speed > 3000000) {
0179         struct bcm_write_uart_clock_setting clock;
0180 
0181         clock.type = BCM_UART_CLOCK_48MHZ;
0182 
0183         bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
0184 
0185         /* This Broadcom specific command changes the UART's controller
0186          * clock for baud rate > 3000000.
0187          */
0188         skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
0189         if (IS_ERR(skb)) {
0190             int err = PTR_ERR(skb);
0191             bt_dev_err(hdev, "BCM: failed to write clock (%d)",
0192                    err);
0193             return err;
0194         }
0195 
0196         kfree_skb(skb);
0197     }
0198 
0199     bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
0200 
0201     param.zero = cpu_to_le16(0);
0202     param.baud_rate = cpu_to_le32(speed);
0203 
0204     /* This Broadcom specific command changes the UART's controller baud
0205      * rate.
0206      */
0207     skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
0208                  HCI_INIT_TIMEOUT);
0209     if (IS_ERR(skb)) {
0210         int err = PTR_ERR(skb);
0211         bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
0212                err);
0213         return err;
0214     }
0215 
0216     kfree_skb(skb);
0217 
0218     return 0;
0219 }
0220 
0221 /* bcm_device_exists should be protected by bcm_device_lock */
0222 static bool bcm_device_exists(struct bcm_device *device)
0223 {
0224     struct list_head *p;
0225 
0226 #ifdef CONFIG_PM
0227     /* Devices using serdev always exist */
0228     if (device && device->hu && device->hu->serdev)
0229         return true;
0230 #endif
0231 
0232     list_for_each(p, &bcm_device_list) {
0233         struct bcm_device *dev = list_entry(p, struct bcm_device, list);
0234 
0235         if (device == dev)
0236             return true;
0237     }
0238 
0239     return false;
0240 }
0241 
0242 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
0243 {
0244     int err;
0245 
0246     if (powered && !dev->res_enabled) {
0247         /* Intel Macs use bcm_apple_get_resources() and don't
0248          * have regulator supplies configured.
0249          */
0250         if (dev->supplies[0].supply) {
0251             err = regulator_bulk_enable(BCM_NUM_SUPPLIES,
0252                             dev->supplies);
0253             if (err)
0254                 return err;
0255         }
0256 
0257         /* LPO clock needs to be 32.768 kHz */
0258         err = clk_set_rate(dev->lpo_clk, 32768);
0259         if (err) {
0260             dev_err(dev->dev, "Could not set LPO clock rate\n");
0261             goto err_regulator_disable;
0262         }
0263 
0264         err = clk_prepare_enable(dev->lpo_clk);
0265         if (err)
0266             goto err_regulator_disable;
0267 
0268         err = clk_prepare_enable(dev->txco_clk);
0269         if (err)
0270             goto err_lpo_clk_disable;
0271     }
0272 
0273     err = dev->set_shutdown(dev, powered);
0274     if (err)
0275         goto err_txco_clk_disable;
0276 
0277     err = dev->set_device_wakeup(dev, powered);
0278     if (err)
0279         goto err_revert_shutdown;
0280 
0281     if (!powered && dev->res_enabled) {
0282         clk_disable_unprepare(dev->txco_clk);
0283         clk_disable_unprepare(dev->lpo_clk);
0284 
0285         /* Intel Macs use bcm_apple_get_resources() and don't
0286          * have regulator supplies configured.
0287          */
0288         if (dev->supplies[0].supply)
0289             regulator_bulk_disable(BCM_NUM_SUPPLIES,
0290                            dev->supplies);
0291     }
0292 
0293     /* wait for device to power on and come out of reset */
0294     usleep_range(100000, 120000);
0295 
0296     dev->res_enabled = powered;
0297 
0298     return 0;
0299 
0300 err_revert_shutdown:
0301     dev->set_shutdown(dev, !powered);
0302 err_txco_clk_disable:
0303     if (powered && !dev->res_enabled)
0304         clk_disable_unprepare(dev->txco_clk);
0305 err_lpo_clk_disable:
0306     if (powered && !dev->res_enabled)
0307         clk_disable_unprepare(dev->lpo_clk);
0308 err_regulator_disable:
0309     if (powered && !dev->res_enabled)
0310         regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies);
0311     return err;
0312 }
0313 
0314 #ifdef CONFIG_PM
0315 static irqreturn_t bcm_host_wake(int irq, void *data)
0316 {
0317     struct bcm_device *bdev = data;
0318 
0319     bt_dev_dbg(bdev, "Host wake IRQ");
0320 
0321     pm_runtime_get(bdev->dev);
0322     pm_runtime_mark_last_busy(bdev->dev);
0323     pm_runtime_put_autosuspend(bdev->dev);
0324 
0325     return IRQ_HANDLED;
0326 }
0327 
0328 static int bcm_request_irq(struct bcm_data *bcm)
0329 {
0330     struct bcm_device *bdev = bcm->dev;
0331     int err;
0332 
0333     mutex_lock(&bcm_device_lock);
0334     if (!bcm_device_exists(bdev)) {
0335         err = -ENODEV;
0336         goto unlock;
0337     }
0338 
0339     if (bdev->irq <= 0) {
0340         err = -EOPNOTSUPP;
0341         goto unlock;
0342     }
0343 
0344     err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
0345                    bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
0346                               IRQF_TRIGGER_RISING,
0347                    "host_wake", bdev);
0348     if (err) {
0349         bdev->irq = err;
0350         goto unlock;
0351     }
0352 
0353     bdev->irq_acquired = true;
0354 
0355     device_init_wakeup(bdev->dev, true);
0356 
0357     pm_runtime_set_autosuspend_delay(bdev->dev,
0358                      BCM_AUTOSUSPEND_DELAY);
0359     pm_runtime_use_autosuspend(bdev->dev);
0360     pm_runtime_set_active(bdev->dev);
0361     pm_runtime_enable(bdev->dev);
0362 
0363 unlock:
0364     mutex_unlock(&bcm_device_lock);
0365 
0366     return err;
0367 }
0368 
0369 static const struct bcm_set_sleep_mode default_sleep_params = {
0370     .sleep_mode = 1,    /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
0371     .idle_host = 2,     /* idle threshold HOST, in 300ms */
0372     .idle_dev = 2,      /* idle threshold device, in 300ms */
0373     .bt_wake_active = 1,    /* BT_WAKE active mode: 1 = high, 0 = low */
0374     .host_wake_active = 0,  /* HOST_WAKE active mode: 1 = high, 0 = low */
0375     .allow_host_sleep = 1,  /* Allow host sleep in SCO flag */
0376     .combine_modes = 1, /* Combine sleep and LPM flag */
0377     .tristate_control = 0,  /* Allow tri-state control of UART tx flag */
0378     /* Irrelevant USB flags */
0379     .usb_auto_sleep = 0,
0380     .usb_resume_timeout = 0,
0381     .break_to_host = 0,
0382     .pulsed_host_wake = 1,
0383 };
0384 
0385 static int bcm_setup_sleep(struct hci_uart *hu)
0386 {
0387     struct bcm_data *bcm = hu->priv;
0388     struct sk_buff *skb;
0389     struct bcm_set_sleep_mode sleep_params = default_sleep_params;
0390 
0391     sleep_params.host_wake_active = !bcm->dev->irq_active_low;
0392 
0393     skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
0394                  &sleep_params, HCI_INIT_TIMEOUT);
0395     if (IS_ERR(skb)) {
0396         int err = PTR_ERR(skb);
0397         bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
0398         return err;
0399     }
0400     kfree_skb(skb);
0401 
0402     bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
0403 
0404     return 0;
0405 }
0406 #else
0407 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
0408 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
0409 #endif
0410 
0411 static int bcm_set_diag(struct hci_dev *hdev, bool enable)
0412 {
0413     struct hci_uart *hu = hci_get_drvdata(hdev);
0414     struct bcm_data *bcm = hu->priv;
0415     struct sk_buff *skb;
0416 
0417     if (!test_bit(HCI_RUNNING, &hdev->flags))
0418         return -ENETDOWN;
0419 
0420     skb = bt_skb_alloc(3, GFP_KERNEL);
0421     if (!skb)
0422         return -ENOMEM;
0423 
0424     skb_put_u8(skb, BCM_LM_DIAG_PKT);
0425     skb_put_u8(skb, 0xf0);
0426     skb_put_u8(skb, enable);
0427 
0428     skb_queue_tail(&bcm->txq, skb);
0429     hci_uart_tx_wakeup(hu);
0430 
0431     return 0;
0432 }
0433 
0434 static int bcm_open(struct hci_uart *hu)
0435 {
0436     struct bcm_data *bcm;
0437     struct list_head *p;
0438     int err;
0439 
0440     bt_dev_dbg(hu->hdev, "hu %p", hu);
0441 
0442     if (!hci_uart_has_flow_control(hu))
0443         return -EOPNOTSUPP;
0444 
0445     bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
0446     if (!bcm)
0447         return -ENOMEM;
0448 
0449     skb_queue_head_init(&bcm->txq);
0450 
0451     hu->priv = bcm;
0452 
0453     mutex_lock(&bcm_device_lock);
0454 
0455     if (hu->serdev) {
0456         bcm->dev = serdev_device_get_drvdata(hu->serdev);
0457         goto out;
0458     }
0459 
0460     if (!hu->tty->dev)
0461         goto out;
0462 
0463     list_for_each(p, &bcm_device_list) {
0464         struct bcm_device *dev = list_entry(p, struct bcm_device, list);
0465 
0466         /* Retrieve saved bcm_device based on parent of the
0467          * platform device (saved during device probe) and
0468          * parent of tty device used by hci_uart
0469          */
0470         if (hu->tty->dev->parent == dev->dev->parent) {
0471             bcm->dev = dev;
0472 #ifdef CONFIG_PM
0473             dev->hu = hu;
0474 #endif
0475             break;
0476         }
0477     }
0478 
0479 out:
0480     if (bcm->dev) {
0481         if (bcm->dev->use_autobaud_mode)
0482             hci_uart_set_flow_control(hu, false);   /* Assert BT_UART_CTS_N */
0483         else if (bcm->dev->drive_rts_on_open)
0484             hci_uart_set_flow_control(hu, true);
0485 
0486         if (bcm->dev->use_autobaud_mode && bcm->dev->max_autobaud_speed)
0487             hu->init_speed = min(bcm->dev->oper_speed, bcm->dev->max_autobaud_speed);
0488         else
0489             hu->init_speed = bcm->dev->init_speed;
0490 
0491         /* If oper_speed is set, ldisc/serdev will set the baudrate
0492          * before calling setup()
0493          */
0494         if (!bcm->dev->no_early_set_baudrate && !bcm->dev->use_autobaud_mode)
0495             hu->oper_speed = bcm->dev->oper_speed;
0496 
0497         err = bcm_gpio_set_power(bcm->dev, true);
0498 
0499         if (bcm->dev->drive_rts_on_open)
0500             hci_uart_set_flow_control(hu, false);
0501 
0502         if (err)
0503             goto err_unset_hu;
0504     }
0505 
0506     mutex_unlock(&bcm_device_lock);
0507     return 0;
0508 
0509 err_unset_hu:
0510 #ifdef CONFIG_PM
0511     if (!hu->serdev)
0512         bcm->dev->hu = NULL;
0513 #endif
0514     mutex_unlock(&bcm_device_lock);
0515     hu->priv = NULL;
0516     kfree(bcm);
0517     return err;
0518 }
0519 
0520 static int bcm_close(struct hci_uart *hu)
0521 {
0522     struct bcm_data *bcm = hu->priv;
0523     struct bcm_device *bdev = NULL;
0524     int err;
0525 
0526     bt_dev_dbg(hu->hdev, "hu %p", hu);
0527 
0528     /* Protect bcm->dev against removal of the device or driver */
0529     mutex_lock(&bcm_device_lock);
0530 
0531     if (hu->serdev) {
0532         bdev = serdev_device_get_drvdata(hu->serdev);
0533     } else if (bcm_device_exists(bcm->dev)) {
0534         bdev = bcm->dev;
0535 #ifdef CONFIG_PM
0536         bdev->hu = NULL;
0537 #endif
0538     }
0539 
0540     if (bdev) {
0541         if (IS_ENABLED(CONFIG_PM) && bdev->irq_acquired) {
0542             devm_free_irq(bdev->dev, bdev->irq, bdev);
0543             device_init_wakeup(bdev->dev, false);
0544             pm_runtime_disable(bdev->dev);
0545         }
0546 
0547         err = bcm_gpio_set_power(bdev, false);
0548         if (err)
0549             bt_dev_err(hu->hdev, "Failed to power down");
0550         else
0551             pm_runtime_set_suspended(bdev->dev);
0552     }
0553     mutex_unlock(&bcm_device_lock);
0554 
0555     skb_queue_purge(&bcm->txq);
0556     kfree_skb(bcm->rx_skb);
0557     kfree(bcm);
0558 
0559     hu->priv = NULL;
0560     return 0;
0561 }
0562 
0563 static int bcm_flush(struct hci_uart *hu)
0564 {
0565     struct bcm_data *bcm = hu->priv;
0566 
0567     bt_dev_dbg(hu->hdev, "hu %p", hu);
0568 
0569     skb_queue_purge(&bcm->txq);
0570 
0571     return 0;
0572 }
0573 
0574 static int bcm_setup(struct hci_uart *hu)
0575 {
0576     struct bcm_data *bcm = hu->priv;
0577     bool fw_load_done = false;
0578     bool use_autobaud_mode = (bcm->dev ? bcm->dev->use_autobaud_mode : 0);
0579     unsigned int speed;
0580     int err;
0581 
0582     bt_dev_dbg(hu->hdev, "hu %p", hu);
0583 
0584     hu->hdev->set_diag = bcm_set_diag;
0585     hu->hdev->set_bdaddr = btbcm_set_bdaddr;
0586 
0587     err = btbcm_initialize(hu->hdev, &fw_load_done, use_autobaud_mode);
0588     if (err)
0589         return err;
0590 
0591     if (!fw_load_done)
0592         return 0;
0593 
0594     /* Init speed if any */
0595     if (bcm->dev && bcm->dev->init_speed)
0596         speed = bcm->dev->init_speed;
0597     else if (hu->proto->init_speed)
0598         speed = hu->proto->init_speed;
0599     else
0600         speed = 0;
0601 
0602     if (speed)
0603         host_set_baudrate(hu, speed);
0604 
0605     /* Operational speed if any */
0606     if (hu->oper_speed)
0607         speed = hu->oper_speed;
0608     else if (bcm->dev && bcm->dev->oper_speed)
0609         speed = bcm->dev->oper_speed;
0610     else if (hu->proto->oper_speed)
0611         speed = hu->proto->oper_speed;
0612     else
0613         speed = 0;
0614 
0615     if (speed) {
0616         err = bcm_set_baudrate(hu, speed);
0617         if (!err)
0618             host_set_baudrate(hu, speed);
0619     }
0620 
0621     /* PCM parameters if provided */
0622     if (bcm->dev && bcm->dev->pcm_int_params[0] != 0xff) {
0623         struct bcm_set_pcm_int_params params;
0624 
0625         btbcm_read_pcm_int_params(hu->hdev, &params);
0626 
0627         memcpy(&params, bcm->dev->pcm_int_params, 5);
0628         btbcm_write_pcm_int_params(hu->hdev, &params);
0629     }
0630 
0631     err = btbcm_finalize(hu->hdev, &fw_load_done, use_autobaud_mode);
0632     if (err)
0633         return err;
0634 
0635     /* Some devices ship with the controller default address.
0636      * Allow the bootloader to set a valid address through the
0637      * device tree.
0638      */
0639     set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hu->hdev->quirks);
0640 
0641     if (!bcm_request_irq(bcm))
0642         err = bcm_setup_sleep(hu);
0643 
0644     return err;
0645 }
0646 
0647 #define BCM_RECV_LM_DIAG \
0648     .type = BCM_LM_DIAG_PKT, \
0649     .hlen = BCM_LM_DIAG_SIZE, \
0650     .loff = 0, \
0651     .lsize = 0, \
0652     .maxlen = BCM_LM_DIAG_SIZE
0653 
0654 #define BCM_RECV_NULL \
0655     .type = BCM_NULL_PKT, \
0656     .hlen = BCM_NULL_SIZE, \
0657     .loff = 0, \
0658     .lsize = 0, \
0659     .maxlen = BCM_NULL_SIZE
0660 
0661 #define BCM_RECV_TYPE49 \
0662     .type = BCM_TYPE49_PKT, \
0663     .hlen = BCM_TYPE49_SIZE, \
0664     .loff = 0, \
0665     .lsize = 0, \
0666     .maxlen = BCM_TYPE49_SIZE
0667 
0668 #define BCM_RECV_TYPE52 \
0669     .type = BCM_TYPE52_PKT, \
0670     .hlen = BCM_TYPE52_SIZE, \
0671     .loff = 0, \
0672     .lsize = 0, \
0673     .maxlen = BCM_TYPE52_SIZE
0674 
0675 static const struct h4_recv_pkt bcm_recv_pkts[] = {
0676     { H4_RECV_ACL,      .recv = hci_recv_frame },
0677     { H4_RECV_SCO,      .recv = hci_recv_frame },
0678     { H4_RECV_EVENT,    .recv = hci_recv_frame },
0679     { H4_RECV_ISO,      .recv = hci_recv_frame },
0680     { BCM_RECV_LM_DIAG, .recv = hci_recv_diag  },
0681     { BCM_RECV_NULL,    .recv = hci_recv_diag  },
0682     { BCM_RECV_TYPE49,  .recv = hci_recv_diag  },
0683     { BCM_RECV_TYPE52,  .recv = hci_recv_diag  },
0684 };
0685 
0686 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
0687 {
0688     struct bcm_data *bcm = hu->priv;
0689 
0690     if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
0691         return -EUNATCH;
0692 
0693     bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
0694                   bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
0695     if (IS_ERR(bcm->rx_skb)) {
0696         int err = PTR_ERR(bcm->rx_skb);
0697         bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0698         bcm->rx_skb = NULL;
0699         return err;
0700     } else if (!bcm->rx_skb) {
0701         /* Delay auto-suspend when receiving completed packet */
0702         mutex_lock(&bcm_device_lock);
0703         if (bcm->dev && bcm_device_exists(bcm->dev)) {
0704             pm_runtime_get(bcm->dev->dev);
0705             pm_runtime_mark_last_busy(bcm->dev->dev);
0706             pm_runtime_put_autosuspend(bcm->dev->dev);
0707         }
0708         mutex_unlock(&bcm_device_lock);
0709     }
0710 
0711     return count;
0712 }
0713 
0714 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0715 {
0716     struct bcm_data *bcm = hu->priv;
0717 
0718     bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
0719 
0720     /* Prepend skb with frame type */
0721     memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
0722     skb_queue_tail(&bcm->txq, skb);
0723 
0724     return 0;
0725 }
0726 
0727 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
0728 {
0729     struct bcm_data *bcm = hu->priv;
0730     struct sk_buff *skb = NULL;
0731     struct bcm_device *bdev = NULL;
0732 
0733     mutex_lock(&bcm_device_lock);
0734 
0735     if (bcm_device_exists(bcm->dev)) {
0736         bdev = bcm->dev;
0737         pm_runtime_get_sync(bdev->dev);
0738         /* Shall be resumed here */
0739     }
0740 
0741     skb = skb_dequeue(&bcm->txq);
0742 
0743     if (bdev) {
0744         pm_runtime_mark_last_busy(bdev->dev);
0745         pm_runtime_put_autosuspend(bdev->dev);
0746     }
0747 
0748     mutex_unlock(&bcm_device_lock);
0749 
0750     return skb;
0751 }
0752 
0753 #ifdef CONFIG_PM
0754 static int bcm_suspend_device(struct device *dev)
0755 {
0756     struct bcm_device *bdev = dev_get_drvdata(dev);
0757     int err;
0758 
0759     bt_dev_dbg(bdev, "");
0760 
0761     if (!bdev->is_suspended && bdev->hu) {
0762         hci_uart_set_flow_control(bdev->hu, true);
0763 
0764         /* Once this returns, driver suspends BT via GPIO */
0765         bdev->is_suspended = true;
0766     }
0767 
0768     /* Suspend the device */
0769     err = bdev->set_device_wakeup(bdev, false);
0770     if (err) {
0771         if (bdev->is_suspended && bdev->hu) {
0772             bdev->is_suspended = false;
0773             hci_uart_set_flow_control(bdev->hu, false);
0774         }
0775         return -EBUSY;
0776     }
0777 
0778     bt_dev_dbg(bdev, "suspend, delaying 15 ms");
0779     msleep(15);
0780 
0781     return 0;
0782 }
0783 
0784 static int bcm_resume_device(struct device *dev)
0785 {
0786     struct bcm_device *bdev = dev_get_drvdata(dev);
0787     int err;
0788 
0789     bt_dev_dbg(bdev, "");
0790 
0791     err = bdev->set_device_wakeup(bdev, true);
0792     if (err) {
0793         dev_err(dev, "Failed to power up\n");
0794         return err;
0795     }
0796 
0797     bt_dev_dbg(bdev, "resume, delaying 15 ms");
0798     msleep(15);
0799 
0800     /* When this executes, the device has woken up already */
0801     if (bdev->is_suspended && bdev->hu) {
0802         bdev->is_suspended = false;
0803 
0804         hci_uart_set_flow_control(bdev->hu, false);
0805     }
0806 
0807     return 0;
0808 }
0809 #endif
0810 
0811 #ifdef CONFIG_PM_SLEEP
0812 /* suspend callback */
0813 static int bcm_suspend(struct device *dev)
0814 {
0815     struct bcm_device *bdev = dev_get_drvdata(dev);
0816     int error;
0817 
0818     bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
0819 
0820     /*
0821      * When used with a device instantiated as platform_device, bcm_suspend
0822      * can be called at any time as long as the platform device is bound,
0823      * so it should use bcm_device_lock to protect access to hci_uart
0824      * and device_wake-up GPIO.
0825      */
0826     mutex_lock(&bcm_device_lock);
0827 
0828     if (!bdev->hu)
0829         goto unlock;
0830 
0831     if (pm_runtime_active(dev))
0832         bcm_suspend_device(dev);
0833 
0834     if (device_may_wakeup(dev) && bdev->irq > 0) {
0835         error = enable_irq_wake(bdev->irq);
0836         if (!error)
0837             bt_dev_dbg(bdev, "BCM irq: enabled");
0838     }
0839 
0840 unlock:
0841     mutex_unlock(&bcm_device_lock);
0842 
0843     return 0;
0844 }
0845 
0846 /* resume callback */
0847 static int bcm_resume(struct device *dev)
0848 {
0849     struct bcm_device *bdev = dev_get_drvdata(dev);
0850     int err = 0;
0851 
0852     bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
0853 
0854     /*
0855      * When used with a device instantiated as platform_device, bcm_resume
0856      * can be called at any time as long as platform device is bound,
0857      * so it should use bcm_device_lock to protect access to hci_uart
0858      * and device_wake-up GPIO.
0859      */
0860     mutex_lock(&bcm_device_lock);
0861 
0862     if (!bdev->hu)
0863         goto unlock;
0864 
0865     if (device_may_wakeup(dev) && bdev->irq > 0) {
0866         disable_irq_wake(bdev->irq);
0867         bt_dev_dbg(bdev, "BCM irq: disabled");
0868     }
0869 
0870     err = bcm_resume_device(dev);
0871 
0872 unlock:
0873     mutex_unlock(&bcm_device_lock);
0874 
0875     if (!err) {
0876         pm_runtime_disable(dev);
0877         pm_runtime_set_active(dev);
0878         pm_runtime_enable(dev);
0879     }
0880 
0881     return 0;
0882 }
0883 #endif
0884 
0885 /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
0886 static struct gpiod_lookup_table asus_tf103c_irq_gpios = {
0887     .dev_id = "serial0-0",
0888     .table = {
0889         GPIO_LOOKUP("INT33FC:02", 17, "host-wakeup-alt", GPIO_ACTIVE_HIGH),
0890         { }
0891     },
0892 };
0893 
0894 static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
0895     {
0896         .ident = "Asus TF103C",
0897         .matches = {
0898             DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
0899             DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
0900         },
0901         .driver_data = &asus_tf103c_irq_gpios,
0902     },
0903     {
0904         .ident = "Meegopad T08",
0905         .matches = {
0906             DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
0907                     "To be filled by OEM."),
0908             DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
0909             DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
0910         },
0911     },
0912     { }
0913 };
0914 
0915 #ifdef CONFIG_ACPI
0916 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
0917 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
0918 static const struct acpi_gpio_params third_gpio = { 2, 0, false };
0919 
0920 static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
0921     { "device-wakeup-gpios", &first_gpio, 1 },
0922     { "shutdown-gpios", &second_gpio, 1 },
0923     { "host-wakeup-gpios", &third_gpio, 1 },
0924     { },
0925 };
0926 
0927 static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
0928     { "host-wakeup-gpios", &first_gpio, 1 },
0929     { "device-wakeup-gpios", &second_gpio, 1 },
0930     { "shutdown-gpios", &third_gpio, 1 },
0931     { },
0932 };
0933 
0934 static int bcm_resource(struct acpi_resource *ares, void *data)
0935 {
0936     struct bcm_device *dev = data;
0937     struct acpi_resource_extended_irq *irq;
0938     struct acpi_resource_gpio *gpio;
0939     struct acpi_resource_uart_serialbus *sb;
0940 
0941     switch (ares->type) {
0942     case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
0943         irq = &ares->data.extended_irq;
0944         if (irq->polarity != ACPI_ACTIVE_LOW)
0945             dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
0946         dev->irq_active_low = true;
0947         break;
0948 
0949     case ACPI_RESOURCE_TYPE_GPIO:
0950         gpio = &ares->data.gpio;
0951         if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
0952             dev->gpio_int_idx = dev->gpio_count;
0953             dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
0954         }
0955         dev->gpio_count++;
0956         break;
0957 
0958     case ACPI_RESOURCE_TYPE_SERIAL_BUS:
0959         sb = &ares->data.uart_serial_bus;
0960         if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
0961             dev->init_speed = sb->default_baud_rate;
0962             dev->oper_speed = 4000000;
0963         }
0964         break;
0965 
0966     default:
0967         break;
0968     }
0969 
0970     return 0;
0971 }
0972 
0973 static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
0974 {
0975     if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
0976         return -EIO;
0977 
0978     return 0;
0979 }
0980 
0981 static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
0982 {
0983     if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
0984                           NULL, NULL, NULL)))
0985         return -EIO;
0986 
0987     return 0;
0988 }
0989 
0990 static int bcm_apple_get_resources(struct bcm_device *dev)
0991 {
0992     struct acpi_device *adev = ACPI_COMPANION(dev->dev);
0993     const union acpi_object *obj;
0994 
0995     if (!adev ||
0996         ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
0997         ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
0998         ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
0999         return -ENODEV;
1000 
1001     if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
1002         obj->buffer.length == 8)
1003         dev->init_speed = *(u64 *)obj->buffer.pointer;
1004 
1005     dev->set_device_wakeup = bcm_apple_set_device_wakeup;
1006     dev->set_shutdown = bcm_apple_set_shutdown;
1007 
1008     return 0;
1009 }
1010 #else
1011 static inline int bcm_apple_get_resources(struct bcm_device *dev)
1012 {
1013     return -EOPNOTSUPP;
1014 }
1015 #endif /* CONFIG_ACPI */
1016 
1017 static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
1018 {
1019     gpiod_set_value_cansleep(dev->device_wakeup, awake);
1020     return 0;
1021 }
1022 
1023 static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
1024 {
1025     gpiod_set_value_cansleep(dev->shutdown, powered);
1026     if (dev->reset)
1027         /*
1028          * The reset line is asserted on powerdown and deasserted
1029          * on poweron so the inverse of powered is used. Notice
1030          * that the GPIO line BT_RST_N needs to be specified as
1031          * active low in the device tree or similar system
1032          * description.
1033          */
1034         gpiod_set_value_cansleep(dev->reset, !powered);
1035     return 0;
1036 }
1037 
1038 /* Try a bunch of names for TXCO */
1039 static struct clk *bcm_get_txco(struct device *dev)
1040 {
1041     struct clk *clk;
1042 
1043     /* New explicit name */
1044     clk = devm_clk_get(dev, "txco");
1045     if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1046         return clk;
1047 
1048     /* Deprecated name */
1049     clk = devm_clk_get(dev, "extclk");
1050     if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1051         return clk;
1052 
1053     /* Original code used no name at all */
1054     return devm_clk_get(dev, NULL);
1055 }
1056 
1057 static int bcm_get_resources(struct bcm_device *dev)
1058 {
1059     const struct dmi_system_id *broken_irq_dmi_id;
1060     const char *irq_con_id = "host-wakeup";
1061     int err;
1062 
1063     dev->name = dev_name(dev->dev);
1064 
1065     if (x86_apple_machine && !bcm_apple_get_resources(dev))
1066         return 0;
1067 
1068     dev->txco_clk = bcm_get_txco(dev->dev);
1069 
1070     /* Handle deferred probing */
1071     if (dev->txco_clk == ERR_PTR(-EPROBE_DEFER))
1072         return PTR_ERR(dev->txco_clk);
1073 
1074     /* Ignore all other errors as before */
1075     if (IS_ERR(dev->txco_clk))
1076         dev->txco_clk = NULL;
1077 
1078     dev->lpo_clk = devm_clk_get(dev->dev, "lpo");
1079     if (dev->lpo_clk == ERR_PTR(-EPROBE_DEFER))
1080         return PTR_ERR(dev->lpo_clk);
1081 
1082     if (IS_ERR(dev->lpo_clk))
1083         dev->lpo_clk = NULL;
1084 
1085     /* Check if we accidentally fetched the lpo clock twice */
1086     if (dev->lpo_clk && clk_is_match(dev->lpo_clk, dev->txco_clk)) {
1087         devm_clk_put(dev->dev, dev->txco_clk);
1088         dev->txco_clk = NULL;
1089     }
1090 
1091     dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
1092                              GPIOD_OUT_LOW);
1093     if (IS_ERR(dev->device_wakeup))
1094         return PTR_ERR(dev->device_wakeup);
1095 
1096     dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
1097                         GPIOD_OUT_LOW);
1098     if (IS_ERR(dev->shutdown))
1099         return PTR_ERR(dev->shutdown);
1100 
1101     dev->reset = devm_gpiod_get_optional(dev->dev, "reset",
1102                          GPIOD_OUT_LOW);
1103     if (IS_ERR(dev->reset))
1104         return PTR_ERR(dev->reset);
1105 
1106     dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
1107     dev->set_shutdown = bcm_gpio_set_shutdown;
1108 
1109     dev->supplies[0].supply = "vbat";
1110     dev->supplies[1].supply = "vddio";
1111     err = devm_regulator_bulk_get(dev->dev, BCM_NUM_SUPPLIES,
1112                       dev->supplies);
1113     if (err)
1114         return err;
1115 
1116     broken_irq_dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
1117     if (broken_irq_dmi_id && broken_irq_dmi_id->driver_data) {
1118         gpiod_add_lookup_table(broken_irq_dmi_id->driver_data);
1119         irq_con_id = "host-wakeup-alt";
1120         dev->irq_active_low = false;
1121         dev->irq = 0;
1122     }
1123 
1124     /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
1125     if (dev->irq <= 0) {
1126         struct gpio_desc *gpio;
1127 
1128         gpio = devm_gpiod_get_optional(dev->dev, irq_con_id, GPIOD_IN);
1129         if (IS_ERR(gpio))
1130             return PTR_ERR(gpio);
1131 
1132         dev->irq = gpiod_to_irq(gpio);
1133     }
1134 
1135     if (broken_irq_dmi_id) {
1136         if (broken_irq_dmi_id->driver_data) {
1137             gpiod_remove_lookup_table(broken_irq_dmi_id->driver_data);
1138         } else {
1139             dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
1140                  broken_irq_dmi_id->ident);
1141             dev->irq = 0;
1142         }
1143     }
1144 
1145     dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
1146     return 0;
1147 }
1148 
1149 #ifdef CONFIG_ACPI
1150 static int bcm_acpi_probe(struct bcm_device *dev)
1151 {
1152     LIST_HEAD(resources);
1153     const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
1154     struct resource_entry *entry;
1155     int ret;
1156 
1157     /* Retrieve UART ACPI info */
1158     dev->gpio_int_idx = -1;
1159     ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
1160                      &resources, bcm_resource, dev);
1161     if (ret < 0)
1162         return ret;
1163 
1164     resource_list_for_each_entry(entry, &resources) {
1165         if (resource_type(entry->res) == IORESOURCE_IRQ) {
1166             dev->irq = entry->res->start;
1167             break;
1168         }
1169     }
1170     acpi_dev_free_resource_list(&resources);
1171 
1172     /* If the DSDT uses an Interrupt resource for the IRQ, then there are
1173      * only 2 GPIO resources, we use the irq-last mapping for this, since
1174      * we already have an irq the 3th / last mapping will not be used.
1175      */
1176     if (dev->irq)
1177         gpio_mapping = acpi_bcm_int_last_gpios;
1178     else if (dev->gpio_int_idx == 0)
1179         gpio_mapping = acpi_bcm_int_first_gpios;
1180     else if (dev->gpio_int_idx == 2)
1181         gpio_mapping = acpi_bcm_int_last_gpios;
1182     else
1183         dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
1184              dev->gpio_int_idx);
1185 
1186     /* Warn if our expectations are not met. */
1187     if (dev->gpio_count != (dev->irq ? 2 : 3))
1188         dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
1189              dev->gpio_count);
1190 
1191     ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
1192     if (ret)
1193         return ret;
1194 
1195     if (irq_polarity != -1) {
1196         dev->irq_active_low = irq_polarity;
1197         dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
1198              dev->irq_active_low ? "low" : "high");
1199     }
1200 
1201     return 0;
1202 }
1203 #else
1204 static int bcm_acpi_probe(struct bcm_device *dev)
1205 {
1206     return -EINVAL;
1207 }
1208 #endif /* CONFIG_ACPI */
1209 
1210 static int bcm_of_probe(struct bcm_device *bdev)
1211 {
1212     bdev->use_autobaud_mode = device_property_read_bool(bdev->dev,
1213                                 "brcm,requires-autobaud-mode");
1214     device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1215     device_property_read_u8_array(bdev->dev, "brcm,bt-pcm-int-params",
1216                       bdev->pcm_int_params, 5);
1217     bdev->irq = of_irq_get_byname(bdev->dev->of_node, "host-wakeup");
1218     bdev->irq_active_low = irq_get_trigger_type(bdev->irq)
1219                  & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW);
1220     return 0;
1221 }
1222 
1223 static int bcm_probe(struct platform_device *pdev)
1224 {
1225     struct bcm_device *dev;
1226     int ret;
1227 
1228     dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1229     if (!dev)
1230         return -ENOMEM;
1231 
1232     dev->dev = &pdev->dev;
1233 
1234     ret = platform_get_irq(pdev, 0);
1235     if (ret < 0)
1236         return ret;
1237 
1238     dev->irq = ret;
1239 
1240     /* Initialize routing field to an unused value */
1241     dev->pcm_int_params[0] = 0xff;
1242 
1243     if (has_acpi_companion(&pdev->dev)) {
1244         ret = bcm_acpi_probe(dev);
1245         if (ret)
1246             return ret;
1247     }
1248 
1249     ret = bcm_get_resources(dev);
1250     if (ret)
1251         return ret;
1252 
1253     platform_set_drvdata(pdev, dev);
1254 
1255     dev_info(&pdev->dev, "%s device registered.\n", dev->name);
1256 
1257     /* Place this instance on the device list */
1258     mutex_lock(&bcm_device_lock);
1259     list_add_tail(&dev->list, &bcm_device_list);
1260     mutex_unlock(&bcm_device_lock);
1261 
1262     ret = bcm_gpio_set_power(dev, false);
1263     if (ret)
1264         dev_err(&pdev->dev, "Failed to power down\n");
1265 
1266     return 0;
1267 }
1268 
1269 static int bcm_remove(struct platform_device *pdev)
1270 {
1271     struct bcm_device *dev = platform_get_drvdata(pdev);
1272 
1273     mutex_lock(&bcm_device_lock);
1274     list_del(&dev->list);
1275     mutex_unlock(&bcm_device_lock);
1276 
1277     dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
1278 
1279     return 0;
1280 }
1281 
1282 static const struct hci_uart_proto bcm_proto = {
1283     .id     = HCI_UART_BCM,
1284     .name       = "Broadcom",
1285     .manufacturer   = 15,
1286     .init_speed = 115200,
1287     .open       = bcm_open,
1288     .close      = bcm_close,
1289     .flush      = bcm_flush,
1290     .setup      = bcm_setup,
1291     .set_baudrate   = bcm_set_baudrate,
1292     .recv       = bcm_recv,
1293     .enqueue    = bcm_enqueue,
1294     .dequeue    = bcm_dequeue,
1295 };
1296 
1297 #ifdef CONFIG_ACPI
1298 static const struct acpi_device_id bcm_acpi_match[] = {
1299     { "BCM2E00" },
1300     { "BCM2E01" },
1301     { "BCM2E02" },
1302     { "BCM2E03" },
1303     { "BCM2E04" },
1304     { "BCM2E05" },
1305     { "BCM2E06" },
1306     { "BCM2E07" },
1307     { "BCM2E08" },
1308     { "BCM2E09" },
1309     { "BCM2E0A" },
1310     { "BCM2E0B" },
1311     { "BCM2E0C" },
1312     { "BCM2E0D" },
1313     { "BCM2E0E" },
1314     { "BCM2E0F" },
1315     { "BCM2E10" },
1316     { "BCM2E11" },
1317     { "BCM2E12" },
1318     { "BCM2E13" },
1319     { "BCM2E14" },
1320     { "BCM2E15" },
1321     { "BCM2E16" },
1322     { "BCM2E17" },
1323     { "BCM2E18" },
1324     { "BCM2E19" },
1325     { "BCM2E1A" },
1326     { "BCM2E1B" },
1327     { "BCM2E1C" },
1328     { "BCM2E1D" },
1329     { "BCM2E1F" },
1330     { "BCM2E20" },
1331     { "BCM2E21" },
1332     { "BCM2E22" },
1333     { "BCM2E23" },
1334     { "BCM2E24" },
1335     { "BCM2E25" },
1336     { "BCM2E26" },
1337     { "BCM2E27" },
1338     { "BCM2E28" },
1339     { "BCM2E29" },
1340     { "BCM2E2A" },
1341     { "BCM2E2B" },
1342     { "BCM2E2C" },
1343     { "BCM2E2D" },
1344     { "BCM2E2E" },
1345     { "BCM2E2F" },
1346     { "BCM2E30" },
1347     { "BCM2E31" },
1348     { "BCM2E32" },
1349     { "BCM2E33" },
1350     { "BCM2E34" },
1351     { "BCM2E35" },
1352     { "BCM2E36" },
1353     { "BCM2E37" },
1354     { "BCM2E38" },
1355     { "BCM2E39" },
1356     { "BCM2E3A" },
1357     { "BCM2E3B" },
1358     { "BCM2E3C" },
1359     { "BCM2E3D" },
1360     { "BCM2E3E" },
1361     { "BCM2E3F" },
1362     { "BCM2E40" },
1363     { "BCM2E41" },
1364     { "BCM2E42" },
1365     { "BCM2E43" },
1366     { "BCM2E44" },
1367     { "BCM2E45" },
1368     { "BCM2E46" },
1369     { "BCM2E47" },
1370     { "BCM2E48" },
1371     { "BCM2E49" },
1372     { "BCM2E4A" },
1373     { "BCM2E4B" },
1374     { "BCM2E4C" },
1375     { "BCM2E4D" },
1376     { "BCM2E4E" },
1377     { "BCM2E4F" },
1378     { "BCM2E50" },
1379     { "BCM2E51" },
1380     { "BCM2E52" },
1381     { "BCM2E53" },
1382     { "BCM2E54" },
1383     { "BCM2E55" },
1384     { "BCM2E56" },
1385     { "BCM2E57" },
1386     { "BCM2E58" },
1387     { "BCM2E59" },
1388     { "BCM2E5A" },
1389     { "BCM2E5B" },
1390     { "BCM2E5C" },
1391     { "BCM2E5D" },
1392     { "BCM2E5E" },
1393     { "BCM2E5F" },
1394     { "BCM2E60" },
1395     { "BCM2E61" },
1396     { "BCM2E62" },
1397     { "BCM2E63" },
1398     { "BCM2E64" },
1399     { "BCM2E65" },
1400     { "BCM2E66" },
1401     { "BCM2E67" },
1402     { "BCM2E68" },
1403     { "BCM2E69" },
1404     { "BCM2E6B" },
1405     { "BCM2E6D" },
1406     { "BCM2E6E" },
1407     { "BCM2E6F" },
1408     { "BCM2E70" },
1409     { "BCM2E71" },
1410     { "BCM2E72" },
1411     { "BCM2E73" },
1412     { "BCM2E74" },
1413     { "BCM2E75" },
1414     { "BCM2E76" },
1415     { "BCM2E77" },
1416     { "BCM2E78" },
1417     { "BCM2E79" },
1418     { "BCM2E7A" },
1419     { "BCM2E7B" },
1420     { "BCM2E7C" },
1421     { "BCM2E7D" },
1422     { "BCM2E7E" },
1423     { "BCM2E7F" },
1424     { "BCM2E80" },
1425     { "BCM2E81" },
1426     { "BCM2E82" },
1427     { "BCM2E83" },
1428     { "BCM2E84" },
1429     { "BCM2E85" },
1430     { "BCM2E86" },
1431     { "BCM2E87" },
1432     { "BCM2E88" },
1433     { "BCM2E89" },
1434     { "BCM2E8A" },
1435     { "BCM2E8B" },
1436     { "BCM2E8C" },
1437     { "BCM2E8D" },
1438     { "BCM2E8E" },
1439     { "BCM2E90" },
1440     { "BCM2E92" },
1441     { "BCM2E93" },
1442     { "BCM2E94" },
1443     { "BCM2E95" },
1444     { "BCM2E96" },
1445     { "BCM2E97" },
1446     { "BCM2E98" },
1447     { "BCM2E99" },
1448     { "BCM2E9A" },
1449     { "BCM2E9B" },
1450     { "BCM2E9C" },
1451     { "BCM2E9D" },
1452     { "BCM2EA0" },
1453     { "BCM2EA1" },
1454     { "BCM2EA2" },
1455     { "BCM2EA3" },
1456     { "BCM2EA4" },
1457     { "BCM2EA5" },
1458     { "BCM2EA6" },
1459     { "BCM2EA7" },
1460     { "BCM2EA8" },
1461     { "BCM2EA9" },
1462     { "BCM2EAA" },
1463     { "BCM2EAB" },
1464     { "BCM2EAC" },
1465     { },
1466 };
1467 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
1468 #endif
1469 
1470 /* suspend and resume callbacks */
1471 static const struct dev_pm_ops bcm_pm_ops = {
1472     SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
1473     SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
1474 };
1475 
1476 static struct platform_driver bcm_driver = {
1477     .probe = bcm_probe,
1478     .remove = bcm_remove,
1479     .driver = {
1480         .name = "hci_bcm",
1481         .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1482         .pm = &bcm_pm_ops,
1483     },
1484 };
1485 
1486 static int bcm_serdev_probe(struct serdev_device *serdev)
1487 {
1488     struct bcm_device *bcmdev;
1489     const struct bcm_device_data *data;
1490     int err;
1491 
1492     bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
1493     if (!bcmdev)
1494         return -ENOMEM;
1495 
1496     bcmdev->dev = &serdev->dev;
1497 #ifdef CONFIG_PM
1498     bcmdev->hu = &bcmdev->serdev_hu;
1499 #endif
1500     bcmdev->serdev_hu.serdev = serdev;
1501     serdev_device_set_drvdata(serdev, bcmdev);
1502 
1503     /* Initialize routing field to an unused value */
1504     bcmdev->pcm_int_params[0] = 0xff;
1505 
1506     if (has_acpi_companion(&serdev->dev))
1507         err = bcm_acpi_probe(bcmdev);
1508     else
1509         err = bcm_of_probe(bcmdev);
1510     if (err)
1511         return err;
1512 
1513     err = bcm_get_resources(bcmdev);
1514     if (err)
1515         return err;
1516 
1517     if (!bcmdev->shutdown) {
1518         dev_warn(&serdev->dev,
1519              "No reset resource, using default baud rate\n");
1520         bcmdev->oper_speed = bcmdev->init_speed;
1521     }
1522 
1523     err = bcm_gpio_set_power(bcmdev, false);
1524     if (err)
1525         dev_err(&serdev->dev, "Failed to power down\n");
1526 
1527     data = device_get_match_data(bcmdev->dev);
1528     if (data) {
1529         bcmdev->max_autobaud_speed = data->max_autobaud_speed;
1530         bcmdev->no_early_set_baudrate = data->no_early_set_baudrate;
1531         bcmdev->drive_rts_on_open = data->drive_rts_on_open;
1532     }
1533 
1534     return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
1535 }
1536 
1537 static void bcm_serdev_remove(struct serdev_device *serdev)
1538 {
1539     struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1540 
1541     hci_uart_unregister_device(&bcmdev->serdev_hu);
1542 }
1543 
1544 #ifdef CONFIG_OF
1545 static struct bcm_device_data bcm4354_device_data = {
1546     .no_early_set_baudrate = true,
1547 };
1548 
1549 static struct bcm_device_data bcm43438_device_data = {
1550     .drive_rts_on_open = true,
1551 };
1552 
1553 static struct bcm_device_data cyw55572_device_data = {
1554     .max_autobaud_speed = 921600,
1555 };
1556 
1557 static const struct of_device_id bcm_bluetooth_of_match[] = {
1558     { .compatible = "brcm,bcm20702a1" },
1559     { .compatible = "brcm,bcm4329-bt" },
1560     { .compatible = "brcm,bcm4330-bt" },
1561     { .compatible = "brcm,bcm4334-bt" },
1562     { .compatible = "brcm,bcm4345c5" },
1563     { .compatible = "brcm,bcm43430a0-bt" },
1564     { .compatible = "brcm,bcm43430a1-bt" },
1565     { .compatible = "brcm,bcm43438-bt", .data = &bcm43438_device_data },
1566     { .compatible = "brcm,bcm4349-bt", .data = &bcm43438_device_data },
1567     { .compatible = "brcm,bcm43540-bt", .data = &bcm4354_device_data },
1568     { .compatible = "brcm,bcm4335a0" },
1569     { .compatible = "infineon,cyw55572-bt", .data = &cyw55572_device_data },
1570     { },
1571 };
1572 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1573 #endif
1574 
1575 static struct serdev_device_driver bcm_serdev_driver = {
1576     .probe = bcm_serdev_probe,
1577     .remove = bcm_serdev_remove,
1578     .driver = {
1579         .name = "hci_uart_bcm",
1580         .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1581         .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1582         .pm = &bcm_pm_ops,
1583     },
1584 };
1585 
1586 int __init bcm_init(void)
1587 {
1588     /* For now, we need to keep both platform device
1589      * driver (ACPI generated) and serdev driver (DT).
1590      */
1591     platform_driver_register(&bcm_driver);
1592     serdev_device_driver_register(&bcm_serdev_driver);
1593 
1594     return hci_uart_register_proto(&bcm_proto);
1595 }
1596 
1597 int __exit bcm_deinit(void)
1598 {
1599     platform_driver_unregister(&bcm_driver);
1600     serdev_device_driver_unregister(&bcm_serdev_driver);
1601 
1602     return hci_uart_unregister_proto(&bcm_proto);
1603 }