hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/bluetooth/hci_bcm.c
....@@ -1,24 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 *
34 * Bluetooth HCI UART driver for Broadcom devices
45 *
56 * 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
- *
227 */
238
249 #include <linux/kernel.h>
....@@ -28,9 +13,11 @@
2813 #include <linux/module.h>
2914 #include <linux/acpi.h>
3015 #include <linux/of.h>
16
+#include <linux/of_irq.h>
3117 #include <linux/property.h>
3218 #include <linux/platform_data/x86/apple.h>
3319 #include <linux/platform_device.h>
20
+#include <linux/regulator/consumer.h>
3421 #include <linux/clk.h>
3522 #include <linux/gpio/consumer.h>
3623 #include <linux/tty.h>
....@@ -59,6 +46,17 @@
5946
6047 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
6148
49
+#define BCM_NUM_SUPPLIES 2
50
+
51
+/**
52
+ * struct bcm_device_data - device specific data
53
+ * @no_early_set_baudrate: Disallow set baudrate before driver setup()
54
+ */
55
+struct bcm_device_data {
56
+ bool no_early_set_baudrate;
57
+ bool drive_rts_on_open;
58
+};
59
+
6260 /**
6361 * struct bcm_device - device driver resources
6462 * @serdev_hu: HCI UART controller struct
....@@ -77,8 +75,10 @@
7775 * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power")
7876 * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up")
7977 * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down")
80
- * @clk: clock used by Bluetooth device
81
- * @clk_enabled: whether @clk is prepared and enabled
78
+ * @txco_clk: external reference frequency clock used by Bluetooth device
79
+ * @lpo_clk: external LPO clock used by Bluetooth device
80
+ * @supplies: VBAT and VDDIO supplies used by Bluetooth device
81
+ * @res_enabled: whether clocks and supplies are prepared and enabled
8282 * @init_speed: default baudrate of Bluetooth device;
8383 * the host UART is initially set to this baudrate so that
8484 * it can configure the Bluetooth device for @oper_speed
....@@ -89,6 +89,7 @@
8989 * @hu: pointer to HCI UART controller struct,
9090 * used to disable flow control during runtime suspend and system sleep
9191 * @is_suspended: whether flow control is currently disabled
92
+ * @no_early_set_baudrate: don't set_baudrate before setup()
9293 */
9394 struct bcm_device {
9495 /* Must be the first member, hci_serdev.c expects this. */
....@@ -108,8 +109,10 @@
108109 int gpio_int_idx;
109110 #endif
110111
111
- struct clk *clk;
112
- bool clk_enabled;
112
+ struct clk *txco_clk;
113
+ struct clk *lpo_clk;
114
+ struct regulator_bulk_data supplies[BCM_NUM_SUPPLIES];
115
+ bool res_enabled;
113116
114117 u32 init_speed;
115118 u32 oper_speed;
....@@ -121,6 +124,9 @@
121124 struct hci_uart *hu;
122125 bool is_suspended;
123126 #endif
127
+ bool no_early_set_baudrate;
128
+ bool drive_rts_on_open;
129
+ u8 pcm_int_params[5];
124130 };
125131
126132 /* generic bcm uart resources */
....@@ -221,32 +227,71 @@
221227 {
222228 int err;
223229
224
- if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) {
225
- err = clk_prepare_enable(dev->clk);
230
+ if (powered && !dev->res_enabled) {
231
+ /* Intel Macs use bcm_apple_get_resources() and don't
232
+ * have regulator supplies configured.
233
+ */
234
+ if (dev->supplies[0].supply) {
235
+ err = regulator_bulk_enable(BCM_NUM_SUPPLIES,
236
+ dev->supplies);
237
+ if (err)
238
+ return err;
239
+ }
240
+
241
+ /* LPO clock needs to be 32.768 kHz */
242
+ err = clk_set_rate(dev->lpo_clk, 32768);
243
+ if (err) {
244
+ dev_err(dev->dev, "Could not set LPO clock rate\n");
245
+ goto err_regulator_disable;
246
+ }
247
+
248
+ err = clk_prepare_enable(dev->lpo_clk);
226249 if (err)
227
- return err;
250
+ goto err_regulator_disable;
251
+
252
+ err = clk_prepare_enable(dev->txco_clk);
253
+ if (err)
254
+ goto err_lpo_clk_disable;
228255 }
229256
230257 err = dev->set_shutdown(dev, powered);
231258 if (err)
232
- goto err_clk_disable;
259
+ goto err_txco_clk_disable;
233260
234261 err = dev->set_device_wakeup(dev, powered);
235262 if (err)
236263 goto err_revert_shutdown;
237264
238
- if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
239
- clk_disable_unprepare(dev->clk);
265
+ if (!powered && dev->res_enabled) {
266
+ clk_disable_unprepare(dev->txco_clk);
267
+ clk_disable_unprepare(dev->lpo_clk);
240268
241
- dev->clk_enabled = powered;
269
+ /* Intel Macs use bcm_apple_get_resources() and don't
270
+ * have regulator supplies configured.
271
+ */
272
+ if (dev->supplies[0].supply)
273
+ regulator_bulk_disable(BCM_NUM_SUPPLIES,
274
+ dev->supplies);
275
+ }
276
+
277
+ /* wait for device to power on and come out of reset */
278
+ usleep_range(100000, 120000);
279
+
280
+ dev->res_enabled = powered;
242281
243282 return 0;
244283
245284 err_revert_shutdown:
246285 dev->set_shutdown(dev, !powered);
247
-err_clk_disable:
248
- if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
249
- clk_disable_unprepare(dev->clk);
286
+err_txco_clk_disable:
287
+ if (powered && !dev->res_enabled)
288
+ clk_disable_unprepare(dev->txco_clk);
289
+err_lpo_clk_disable:
290
+ if (powered && !dev->res_enabled)
291
+ clk_disable_unprepare(dev->lpo_clk);
292
+err_regulator_disable:
293
+ if (powered && !dev->res_enabled)
294
+ regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies);
250295 return err;
251296 }
252297
....@@ -417,9 +462,22 @@
417462
418463 out:
419464 if (bcm->dev) {
465
+ if (bcm->dev->drive_rts_on_open)
466
+ hci_uart_set_flow_control(hu, true);
467
+
420468 hu->init_speed = bcm->dev->init_speed;
421
- hu->oper_speed = bcm->dev->oper_speed;
469
+
470
+ /* If oper_speed is set, ldisc/serdev will set the baudrate
471
+ * before calling setup()
472
+ */
473
+ if (!bcm->dev->no_early_set_baudrate)
474
+ hu->oper_speed = bcm->dev->oper_speed;
475
+
422476 err = bcm_gpio_set_power(bcm->dev, true);
477
+
478
+ if (bcm->dev->drive_rts_on_open)
479
+ hci_uart_set_flow_control(hu, false);
480
+
423481 if (err)
424482 goto err_unset_hu;
425483 }
....@@ -495,8 +553,7 @@
495553 static int bcm_setup(struct hci_uart *hu)
496554 {
497555 struct bcm_data *bcm = hu->priv;
498
- char fw_name[64];
499
- const struct firmware *fw;
556
+ bool fw_load_done = false;
500557 unsigned int speed;
501558 int err;
502559
....@@ -505,21 +562,12 @@
505562 hu->hdev->set_diag = bcm_set_diag;
506563 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
507564
508
- err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name), false);
565
+ err = btbcm_initialize(hu->hdev, &fw_load_done);
509566 if (err)
510567 return err;
511568
512
- err = request_firmware(&fw, fw_name, &hu->hdev->dev);
513
- if (err < 0) {
514
- bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
569
+ if (!fw_load_done)
515570 return 0;
516
- }
517
-
518
- err = btbcm_patchram(hu->hdev, fw);
519
- if (err) {
520
- bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
521
- goto finalize;
522
- }
523571
524572 /* Init speed if any */
525573 if (hu->init_speed)
....@@ -535,6 +583,8 @@
535583 /* Operational speed if any */
536584 if (hu->oper_speed)
537585 speed = hu->oper_speed;
586
+ else if (bcm->dev && bcm->dev->oper_speed)
587
+ speed = bcm->dev->oper_speed;
538588 else if (hu->proto->oper_speed)
539589 speed = hu->proto->oper_speed;
540590 else
....@@ -546,12 +596,25 @@
546596 host_set_baudrate(hu, speed);
547597 }
548598
549
-finalize:
550
- release_firmware(fw);
599
+ /* PCM parameters if provided */
600
+ if (bcm->dev && bcm->dev->pcm_int_params[0] != 0xff) {
601
+ struct bcm_set_pcm_int_params params;
551602
552
- err = btbcm_finalize(hu->hdev);
603
+ btbcm_read_pcm_int_params(hu->hdev, &params);
604
+
605
+ memcpy(&params, bcm->dev->pcm_int_params, 5);
606
+ btbcm_write_pcm_int_params(hu->hdev, &params);
607
+ }
608
+
609
+ err = btbcm_finalize(hu->hdev, &fw_load_done);
553610 if (err)
554611 return err;
612
+
613
+ /* Some devices ship with the controller default address.
614
+ * Allow the bootloader to set a valid address through the
615
+ * device tree.
616
+ */
617
+ set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hu->hdev->quirks);
555618
556619 if (!bcm_request_irq(bcm))
557620 err = bcm_setup_sleep(hu);
....@@ -591,6 +654,7 @@
591654 { H4_RECV_ACL, .recv = hci_recv_frame },
592655 { H4_RECV_SCO, .recv = hci_recv_frame },
593656 { H4_RECV_EVENT, .recv = hci_recv_frame },
657
+ { H4_RECV_ISO, .recv = hci_recv_frame },
594658 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
595659 { BCM_RECV_NULL, .recv = hci_recv_diag },
596660 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
....@@ -796,6 +860,21 @@
796860 }
797861 #endif
798862
863
+/* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
864
+static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
865
+ {
866
+ .ident = "Meegopad T08",
867
+ .matches = {
868
+ DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
869
+ "To be filled by OEM."),
870
+ DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
871
+ DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
872
+ },
873
+ },
874
+ { }
875
+};
876
+
877
+#ifdef CONFIG_ACPI
799878 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
800879 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
801880 static const struct acpi_gpio_params third_gpio = { 2, 0, false };
....@@ -814,21 +893,6 @@
814893 { },
815894 };
816895
817
-/* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
818
-static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
819
- {
820
- .ident = "Meegopad T08",
821
- .matches = {
822
- DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
823
- "To be filled by OEM."),
824
- DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
825
- DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
826
- },
827
- },
828
- { }
829
-};
830
-
831
-#ifdef CONFIG_ACPI
832896 static int bcm_resource(struct acpi_resource *ares, void *data)
833897 {
834898 struct bcm_device *dev = data;
....@@ -924,20 +988,57 @@
924988 return 0;
925989 }
926990
991
+/* Try a bunch of names for TXCO */
992
+static struct clk *bcm_get_txco(struct device *dev)
993
+{
994
+ struct clk *clk;
995
+
996
+ /* New explicit name */
997
+ clk = devm_clk_get(dev, "txco");
998
+ if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
999
+ return clk;
1000
+
1001
+ /* Deprecated name */
1002
+ clk = devm_clk_get(dev, "extclk");
1003
+ if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1004
+ return clk;
1005
+
1006
+ /* Original code used no name at all */
1007
+ return devm_clk_get(dev, NULL);
1008
+}
1009
+
9271010 static int bcm_get_resources(struct bcm_device *dev)
9281011 {
9291012 const struct dmi_system_id *dmi_id;
1013
+ int err;
9301014
9311015 dev->name = dev_name(dev->dev);
9321016
9331017 if (x86_apple_machine && !bcm_apple_get_resources(dev))
9341018 return 0;
9351019
936
- dev->clk = devm_clk_get(dev->dev, NULL);
1020
+ dev->txco_clk = bcm_get_txco(dev->dev);
9371021
9381022 /* Handle deferred probing */
939
- if (dev->clk == ERR_PTR(-EPROBE_DEFER))
940
- return PTR_ERR(dev->clk);
1023
+ if (dev->txco_clk == ERR_PTR(-EPROBE_DEFER))
1024
+ return PTR_ERR(dev->txco_clk);
1025
+
1026
+ /* Ignore all other errors as before */
1027
+ if (IS_ERR(dev->txco_clk))
1028
+ dev->txco_clk = NULL;
1029
+
1030
+ dev->lpo_clk = devm_clk_get(dev->dev, "lpo");
1031
+ if (dev->lpo_clk == ERR_PTR(-EPROBE_DEFER))
1032
+ return PTR_ERR(dev->lpo_clk);
1033
+
1034
+ if (IS_ERR(dev->lpo_clk))
1035
+ dev->lpo_clk = NULL;
1036
+
1037
+ /* Check if we accidentally fetched the lpo clock twice */
1038
+ if (dev->lpo_clk && clk_is_match(dev->lpo_clk, dev->txco_clk)) {
1039
+ devm_clk_put(dev->dev, dev->txco_clk);
1040
+ dev->txco_clk = NULL;
1041
+ }
9411042
9421043 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
9431044 GPIOD_OUT_LOW);
....@@ -951,6 +1052,13 @@
9511052
9521053 dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
9531054 dev->set_shutdown = bcm_gpio_set_shutdown;
1055
+
1056
+ dev->supplies[0].supply = "vbat";
1057
+ dev->supplies[1].supply = "vddio";
1058
+ err = devm_regulator_bulk_get(dev->dev, BCM_NUM_SUPPLIES,
1059
+ dev->supplies);
1060
+ if (err)
1061
+ return err;
9541062
9551063 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
9561064 if (dev->irq <= 0) {
....@@ -1039,6 +1147,11 @@
10391147 static int bcm_of_probe(struct bcm_device *bdev)
10401148 {
10411149 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1150
+ device_property_read_u8_array(bdev->dev, "brcm,bt-pcm-int-params",
1151
+ bdev->pcm_int_params, 5);
1152
+ bdev->irq = of_irq_get_byname(bdev->dev->of_node, "host-wakeup");
1153
+ bdev->irq_active_low = irq_get_trigger_type(bdev->irq)
1154
+ & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW);
10421155 return 0;
10431156 }
10441157
....@@ -1058,6 +1171,9 @@
10581171 return ret;
10591172
10601173 dev->irq = ret;
1174
+
1175
+ /* Initialize routing field to an unused value */
1176
+ dev->pcm_int_params[0] = 0xff;
10611177
10621178 if (has_acpi_companion(&pdev->dev)) {
10631179 ret = bcm_acpi_probe(dev);
....@@ -1305,6 +1421,7 @@
13051421 static int bcm_serdev_probe(struct serdev_device *serdev)
13061422 {
13071423 struct bcm_device *bcmdev;
1424
+ const struct bcm_device_data *data;
13081425 int err;
13091426
13101427 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
....@@ -1317,6 +1434,9 @@
13171434 #endif
13181435 bcmdev->serdev_hu.serdev = serdev;
13191436 serdev_device_set_drvdata(serdev, bcmdev);
1437
+
1438
+ /* Initialize routing field to an unused value */
1439
+ bcmdev->pcm_int_params[0] = 0xff;
13201440
13211441 if (has_acpi_companion(&serdev->dev))
13221442 err = bcm_acpi_probe(bcmdev);
....@@ -1339,6 +1459,12 @@
13391459 if (err)
13401460 dev_err(&serdev->dev, "Failed to power down\n");
13411461
1462
+ data = device_get_match_data(bcmdev->dev);
1463
+ if (data) {
1464
+ bcmdev->no_early_set_baudrate = data->no_early_set_baudrate;
1465
+ bcmdev->drive_rts_on_open = data->drive_rts_on_open;
1466
+ }
1467
+
13421468 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
13431469 }
13441470
....@@ -1350,8 +1476,24 @@
13501476 }
13511477
13521478 #ifdef CONFIG_OF
1479
+static struct bcm_device_data bcm4354_device_data = {
1480
+ .no_early_set_baudrate = true,
1481
+};
1482
+
1483
+static struct bcm_device_data bcm43438_device_data = {
1484
+ .drive_rts_on_open = true,
1485
+};
1486
+
13531487 static const struct of_device_id bcm_bluetooth_of_match[] = {
1354
- { .compatible = "brcm,bcm43438-bt" },
1488
+ { .compatible = "brcm,bcm20702a1" },
1489
+ { .compatible = "brcm,bcm4329-bt" },
1490
+ { .compatible = "brcm,bcm4345c5" },
1491
+ { .compatible = "brcm,bcm4330-bt" },
1492
+ { .compatible = "brcm,bcm43438-bt", .data = &bcm43438_device_data },
1493
+ { .compatible = "brcm,bcm4349-bt", .data = &bcm43438_device_data },
1494
+ { .compatible = "brcm,bcm43540-bt", .data = &bcm4354_device_data },
1495
+ { .compatible = "brcm,bcm4335a0" },
1496
+ { .compatible = "infineon,cyw55572-bt" },
13551497 { },
13561498 };
13571499 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);