]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/bluetooth/hci_bcm.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[karo-tx-linux.git] / drivers / bluetooth / hci_bcm.c
index 322302b04710f1764fd339247f0ee32aed924743..f30654149c63b4099eb399b61aa5c5305907af10 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/clk.h>
 #include <linux/gpio/consumer.h>
 #include <linux/tty.h>
+#include <linux/interrupt.h>
 
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
@@ -51,6 +52,8 @@ struct bcm_device {
        bool                    clk_enabled;
 
        u32                     init_speed;
+       int                     irq;
+       u8                      irq_polarity;
 
 #ifdef CONFIG_PM_SLEEP
        struct hci_uart         *hu;
@@ -66,7 +69,7 @@ struct bcm_data {
 };
 
 /* List of BCM BT UART devices */
-static DEFINE_SPINLOCK(bcm_device_list_lock);
+static DEFINE_MUTEX(bcm_device_lock);
 static LIST_HEAD(bcm_device_list);
 
 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
@@ -80,7 +83,7 @@ static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
 
                clock.type = BCM_UART_CLOCK_48MHZ;
 
-               BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
+               bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
 
                /* This Broadcom specific command changes the UART's controller
                 * clock for baud rate > 3000000.
@@ -88,15 +91,15 @@ static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
                skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
                if (IS_ERR(skb)) {
                        int err = PTR_ERR(skb);
-                       BT_ERR("%s: BCM: failed to write clock command (%d)",
-                              hdev->name, err);
+                       bt_dev_err(hdev, "BCM: failed to write clock (%d)",
+                                  err);
                        return err;
                }
 
                kfree_skb(skb);
        }
 
-       BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
+       bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
 
        param.zero = cpu_to_le16(0);
        param.baud_rate = cpu_to_le32(speed);
@@ -108,8 +111,8 @@ static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
                             HCI_INIT_TIMEOUT);
        if (IS_ERR(skb)) {
                int err = PTR_ERR(skb);
-               BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
-                      hdev->name, err);
+               bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
+                          err);
                return err;
        }
 
@@ -118,7 +121,7 @@ static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
        return 0;
 }
 
-/* bcm_device_exists should be protected by bcm_device_list_lock */
+/* bcm_device_exists should be protected by bcm_device_lock */
 static bool bcm_device_exists(struct bcm_device *device)
 {
        struct list_head *p;
@@ -138,8 +141,8 @@ static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
        if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
                clk_enable(dev->clk);
 
-       gpiod_set_value_cansleep(dev->shutdown, powered);
-       gpiod_set_value_cansleep(dev->device_wakeup, powered);
+       gpiod_set_value(dev->shutdown, powered);
+       gpiod_set_value(dev->device_wakeup, powered);
 
        if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
                clk_disable(dev->clk);
@@ -149,12 +152,92 @@ static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
        return 0;
 }
 
+#ifdef CONFIG_PM_SLEEP
+static irqreturn_t bcm_host_wake(int irq, void *data)
+{
+       struct bcm_device *bdev = data;
+
+       bt_dev_dbg(bdev, "Host wake IRQ");
+
+       return IRQ_HANDLED;
+}
+
+static int bcm_request_irq(struct bcm_data *bcm)
+{
+       struct bcm_device *bdev = bcm->dev;
+       int err = 0;
+
+       /* If this is not a platform device, do not enable PM functionalities */
+       mutex_lock(&bcm_device_lock);
+       if (!bcm_device_exists(bdev)) {
+               err = -ENODEV;
+               goto unlock;
+       }
+
+       if (bdev->irq > 0) {
+               err = devm_request_irq(&bdev->pdev->dev, bdev->irq,
+                                      bcm_host_wake, IRQF_TRIGGER_RISING,
+                                      "host_wake", bdev);
+               if (err)
+                       goto unlock;
+
+               device_init_wakeup(&bdev->pdev->dev, true);
+       }
+
+unlock:
+       mutex_unlock(&bcm_device_lock);
+
+       return err;
+}
+
+static const struct bcm_set_sleep_mode default_sleep_params = {
+       .sleep_mode = 1,        /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
+       .idle_host = 2,         /* idle threshold HOST, in 300ms */
+       .idle_dev = 2,          /* idle threshold device, in 300ms */
+       .bt_wake_active = 1,    /* BT_WAKE active mode: 1 = high, 0 = low */
+       .host_wake_active = 0,  /* HOST_WAKE active mode: 1 = high, 0 = low */
+       .allow_host_sleep = 1,  /* Allow host sleep in SCO flag */
+       .combine_modes = 0,     /* Combine sleep and LPM flag */
+       .tristate_control = 0,  /* Allow tri-state control of UART tx flag */
+       /* Irrelevant USB flags */
+       .usb_auto_sleep = 0,
+       .usb_resume_timeout = 0,
+       .pulsed_host_wake = 0,
+       .break_to_host = 0
+};
+
+static int bcm_setup_sleep(struct hci_uart *hu)
+{
+       struct bcm_data *bcm = hu->priv;
+       struct sk_buff *skb;
+       struct bcm_set_sleep_mode sleep_params = default_sleep_params;
+
+       sleep_params.host_wake_active = !bcm->dev->irq_polarity;
+
+       skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
+                            &sleep_params, HCI_INIT_TIMEOUT);
+       if (IS_ERR(skb)) {
+               int err = PTR_ERR(skb);
+               bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
+               return err;
+       }
+       kfree_skb(skb);
+
+       bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
+
+       return 0;
+}
+#else
+static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
+static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
+#endif
+
 static int bcm_open(struct hci_uart *hu)
 {
        struct bcm_data *bcm;
        struct list_head *p;
 
-       BT_DBG("hu %p", hu);
+       bt_dev_dbg(hu->hdev, "hu %p", hu);
 
        bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
        if (!bcm)
@@ -164,7 +247,7 @@ static int bcm_open(struct hci_uart *hu)
 
        hu->priv = bcm;
 
-       spin_lock(&bcm_device_list_lock);
+       mutex_lock(&bcm_device_lock);
        list_for_each(p, &bcm_device_list) {
                struct bcm_device *dev = list_entry(p, struct bcm_device, list);
 
@@ -178,14 +261,12 @@ static int bcm_open(struct hci_uart *hu)
 #ifdef CONFIG_PM_SLEEP
                        dev->hu = hu;
 #endif
+                       bcm_gpio_set_power(bcm->dev, true);
                        break;
                }
        }
 
-       if (bcm->dev)
-               bcm_gpio_set_power(bcm->dev, true);
-
-       spin_unlock(&bcm_device_list_lock);
+       mutex_unlock(&bcm_device_lock);
 
        return 0;
 }
@@ -193,18 +274,24 @@ static int bcm_open(struct hci_uart *hu)
 static int bcm_close(struct hci_uart *hu)
 {
        struct bcm_data *bcm = hu->priv;
+       struct bcm_device *bdev = bcm->dev;
 
-       BT_DBG("hu %p", hu);
+       bt_dev_dbg(hu->hdev, "hu %p", hu);
 
        /* Protect bcm->dev against removal of the device or driver */
-       spin_lock(&bcm_device_list_lock);
-       if (bcm_device_exists(bcm->dev)) {
-               bcm_gpio_set_power(bcm->dev, false);
+       mutex_lock(&bcm_device_lock);
+       if (bcm_device_exists(bdev)) {
+               bcm_gpio_set_power(bdev, false);
 #ifdef CONFIG_PM_SLEEP
-               bcm->dev->hu = NULL;
+               if (device_can_wakeup(&bdev->pdev->dev)) {
+                       devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
+                       device_init_wakeup(&bdev->pdev->dev, false);
+               }
+
+               bdev->hu = NULL;
 #endif
        }
-       spin_unlock(&bcm_device_list_lock);
+       mutex_unlock(&bcm_device_lock);
 
        skb_queue_purge(&bcm->txq);
        kfree_skb(bcm->rx_skb);
@@ -218,7 +305,7 @@ static int bcm_flush(struct hci_uart *hu)
 {
        struct bcm_data *bcm = hu->priv;
 
-       BT_DBG("hu %p", hu);
+       bt_dev_dbg(hu->hdev, "hu %p", hu);
 
        skb_queue_purge(&bcm->txq);
 
@@ -227,12 +314,13 @@ static int bcm_flush(struct hci_uart *hu)
 
 static int bcm_setup(struct hci_uart *hu)
 {
+       struct bcm_data *bcm = hu->priv;
        char fw_name[64];
        const struct firmware *fw;
        unsigned int speed;
        int err;
 
-       BT_DBG("hu %p", hu);
+       bt_dev_dbg(hu->hdev, "hu %p", hu);
 
        hu->hdev->set_bdaddr = btbcm_set_bdaddr;
 
@@ -242,13 +330,13 @@ static int bcm_setup(struct hci_uart *hu)
 
        err = request_firmware(&fw, fw_name, &hu->hdev->dev);
        if (err < 0) {
-               BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
+               bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
                return 0;
        }
 
        err = btbcm_patchram(hu->hdev, fw);
        if (err) {
-               BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
+               bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
                goto finalize;
        }
 
@@ -281,6 +369,12 @@ finalize:
        release_firmware(fw);
 
        err = btbcm_finalize(hu->hdev);
+       if (err)
+               return err;
+
+       err = bcm_request_irq(bcm);
+       if (!err)
+               err = bcm_setup_sleep(hu);
 
        return err;
 }
@@ -302,7 +396,7 @@ static int bcm_recv(struct hci_uart *hu, const void *data, int count)
                                  bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
        if (IS_ERR(bcm->rx_skb)) {
                int err = PTR_ERR(bcm->rx_skb);
-               BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
+               bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
                bcm->rx_skb = NULL;
                return err;
        }
@@ -314,7 +408,7 @@ static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
 {
        struct bcm_data *bcm = hu->priv;
 
-       BT_DBG("hu %p skb %p", hu, skb);
+       bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
 
        /* Prepend skb with frame type */
        memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
@@ -335,8 +429,14 @@ static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
 static int bcm_suspend(struct device *dev)
 {
        struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
+       int error;
 
-       BT_DBG("suspend (%p): is_suspended %d", bdev, bdev->is_suspended);
+       bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
+
+       mutex_lock(&bcm_device_lock);
+
+       if (!bdev->hu)
+               goto unlock;
 
        if (!bdev->is_suspended) {
                hci_uart_set_flow_control(bdev->hu, true);
@@ -348,10 +448,19 @@ static int bcm_suspend(struct device *dev)
        /* Suspend the device */
        if (bdev->device_wakeup) {
                gpiod_set_value(bdev->device_wakeup, false);
-               BT_DBG("suspend, delaying 15 ms");
+               bt_dev_dbg(bdev, "suspend, delaying 15 ms");
                mdelay(15);
        }
 
+       if (device_may_wakeup(&bdev->pdev->dev)) {
+               error = enable_irq_wake(bdev->irq);
+               if (!error)
+                       bt_dev_dbg(bdev, "BCM irq: enabled");
+       }
+
+unlock:
+       mutex_unlock(&bcm_device_lock);
+
        return 0;
 }
 
@@ -360,11 +469,21 @@ static int bcm_resume(struct device *dev)
 {
        struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
 
-       BT_DBG("resume (%p): is_suspended %d", bdev, bdev->is_suspended);
+       bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
+
+       mutex_lock(&bcm_device_lock);
+
+       if (!bdev->hu)
+               goto unlock;
+
+       if (device_may_wakeup(&bdev->pdev->dev)) {
+               disable_irq_wake(bdev->irq);
+               bt_dev_dbg(bdev, "BCM irq: disabled");
+       }
 
        if (bdev->device_wakeup) {
                gpiod_set_value(bdev->device_wakeup, true);
-               BT_DBG("resume, delaying 15 ms");
+               bt_dev_dbg(bdev, "resume, delaying 15 ms");
                mdelay(15);
        }
 
@@ -375,16 +494,21 @@ static int bcm_resume(struct device *dev)
                hci_uart_set_flow_control(bdev->hu, false);
        }
 
+unlock:
+       mutex_unlock(&bcm_device_lock);
+
        return 0;
 }
 #endif
 
 static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
 static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
+static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
 
 static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
        { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
        { "shutdown-gpios", &shutdown_gpios, 1 },
+       { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
        { },
 };
 
@@ -392,13 +516,30 @@ static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
 static int bcm_resource(struct acpi_resource *ares, void *data)
 {
        struct bcm_device *dev = data;
-
-       if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
-               struct acpi_resource_uart_serialbus *sb;
-
+       struct acpi_resource_extended_irq *irq;
+       struct acpi_resource_gpio *gpio;
+       struct acpi_resource_uart_serialbus *sb;
+
+       switch (ares->type) {
+       case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
+               irq = &ares->data.extended_irq;
+               dev->irq_polarity = irq->polarity;
+               break;
+
+       case ACPI_RESOURCE_TYPE_GPIO:
+               gpio = &ares->data.gpio;
+               if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
+                       dev->irq_polarity = gpio->polarity;
+               break;
+
+       case ACPI_RESOURCE_TYPE_SERIAL_BUS:
                sb = &ares->data.uart_serial_bus;
                if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
                        dev->init_speed = sb->default_baud_rate;
+               break;
+
+       default:
+               break;
        }
 
        /* Always tell the ACPI core to skip this resource */
@@ -437,6 +578,21 @@ static int bcm_acpi_probe(struct bcm_device *dev)
        if (IS_ERR(dev->shutdown))
                return PTR_ERR(dev->shutdown);
 
+       /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
+       dev->irq = platform_get_irq(pdev, 0);
+       if (dev->irq <= 0) {
+               struct gpio_desc *gpio;
+
+               gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
+                                              GPIOD_IN);
+               if (IS_ERR(gpio))
+                       return PTR_ERR(gpio);
+
+               dev->irq = gpiod_to_irq(gpio);
+       }
+
+       dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
+
        /* Make sure at-least one of the GPIO is defined and that
         * a name is specified for this instance
         */
@@ -488,9 +644,9 @@ static int bcm_probe(struct platform_device *pdev)
        dev_info(&pdev->dev, "%s device registered.\n", dev->name);
 
        /* Place this instance on the device list */
-       spin_lock(&bcm_device_list_lock);
+       mutex_lock(&bcm_device_lock);
        list_add_tail(&dev->list, &bcm_device_list);
-       spin_unlock(&bcm_device_list_lock);
+       mutex_unlock(&bcm_device_lock);
 
        bcm_gpio_set_power(dev, false);
 
@@ -501,9 +657,9 @@ static int bcm_remove(struct platform_device *pdev)
 {
        struct bcm_device *dev = platform_get_drvdata(pdev);
 
-       spin_lock(&bcm_device_list_lock);
+       mutex_lock(&bcm_device_lock);
        list_del(&dev->list);
-       spin_unlock(&bcm_device_list_lock);
+       mutex_unlock(&bcm_device_lock);
 
        acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));