Skip to content

Commit

Permalink
Bluetooth: drivers: Convert B91 HCI driver to new API
Browse files Browse the repository at this point in the history
Convert the B91 HCI driver to use the new HCI driver API.

Signed-off-by: Johan Hedberg <johan.hedberg@gmail.com>
  • Loading branch information
jhedberg committed May 25, 2024
1 parent d3b94db commit 5f1a22e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
5 changes: 5 additions & 0 deletions boards/telink/tlsr9518adk80d/tlsr9518adk80d.dts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
zephyr,entropy = &trng0;
zephyr,code-partition = &slot0_partition;
zephyr,ieee802154 = &ieee802154;
zephyr,bt-hci = &bt_hci_b91;
};
};

Expand Down Expand Up @@ -180,3 +181,7 @@
vref-internal-mv = <1200>;
sample-freq = <DT_FREQ_K(96)>;
};

&bt_hci_b91 {
status = "okay";
};
63 changes: 42 additions & 21 deletions drivers/bluetooth/hci/hci_b91.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@

#include <zephyr/bluetooth/hci.h>

#include <zephyr/devicetree.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include <zephyr/drivers/bluetooth.h>

#include <b91_bt.h>

#define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_hci_driver_b91);

#define DT_DRV_COMPAT telink_bt_hci_b91

#define HCI_BT_B91_TIMEOUT K_MSEC(2000)

static K_SEM_DEFINE(hci_send_sem, 1, 1);

struct b91_data {
bt_hci_recv_t recv;
};

struct b91_config {
struct bt_hci_info info;
};

static bool is_hci_event_discardable(const uint8_t *evt_data)
{
uint8_t evt_type = evt_data[0];
Expand Down Expand Up @@ -135,6 +146,8 @@ static struct net_buf *bt_b91_acl_recv(uint8_t *data, size_t len)

static void hci_b91_host_rcv_pkt(uint8_t *data, uint16_t len)
{
const struct device *dev = DEVICE_DT_GET(DT_DRV_INST(0));
struct b91_data = dev->data;
uint8_t pkt_indicator;
struct net_buf *buf;

Expand All @@ -159,7 +172,7 @@ static void hci_b91_host_rcv_pkt(uint8_t *data, uint16_t len)

if (buf) {
LOG_DBG("Calling bt_recv(%p)", buf);
bt_recv(buf);
data->recv(dev, buf);
}
}

Expand All @@ -173,7 +186,7 @@ static b91_bt_host_callback_t vhci_host_cb = {
.host_read_packet = hci_b91_host_rcv_pkt
};

static int bt_b91_send(struct net_buf *buf)
static int hci_b91_send(const struct device *dev, struct net_buf *buf)
{
int err = 0;
uint8_t type;
Expand Down Expand Up @@ -210,8 +223,16 @@ static int bt_b91_send(struct net_buf *buf)
return err;
}

static int hci_b91_open(void)
static void hci_b91_get_info(const struct device *dev, struct bt_hci_info *info)
{
const struct b91_config *config = dev->config;

*info = config->info;
}

static int hci_b91_open(const struct device *dev, bt_hci_recv_t recv)
{
struct b91_data *data = dev->data;
int status;

status = b91_bt_controller_init();
Expand All @@ -220,29 +241,29 @@ static int hci_b91_open(void)
return status;
}

data->recv = recv;

b91_bt_host_callback_register(&vhci_host_cb);

LOG_DBG("B91 BT started");

return 0;
}

static const struct bt_hci_driver drv = {
.name = "BT B91",
.open = hci_b91_open,
.send = bt_b91_send,
.bus = BT_HCI_DRIVER_BUS_IPM,
#if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE)
.quirks = BT_QUIRK_NO_AUTO_DLE,
#endif
static const struct bt_hci_driver_api b91_driver_api = {
.get_info = hci_b91_get_info,
.open = hci_b91_open,
.send = hci_b91_send,
};

static int bt_b91_init(void)
{

bt_hci_driver_register(&drv);

return 0;
}

SYS_INIT(bt_b91_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
#define B91_DEVICE_INIT(inst) \
static const struct b91_config b91_config_##inst = { \
.info = BT_DT_HCI_INFO_INST_GET(inst), \
}; \
static struct b91_data b91_data_##inst = { \
}; \
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &b91_data_##inst, &b91_config_##inst, \
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &b91_driver_api)

/* Only a single instance is supported */
B91_DEVICE_INIT(0)
13 changes: 13 additions & 0 deletions dts/bindings/bluetooth/telink,bt-hci-b91.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: Bluetooth HCI provided by the Telink B91 controller

compatible: "telink,bt-hci-b91"

include: bt-hci.yaml

properties:
bt-hci-name:
default: "BT B91"
bt-hci-bus:
default: "BT_HCI_BUS_IPM"
bt-hci-quirks:
default: ["BT_HCI_QUIRK_NO_AUTO_DLE"]
5 changes: 5 additions & 0 deletions dts/riscv/telink/telink_b91.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
status = "disabled";
};

bt_hci_b91: bt_hci_b91 {
compatible = "telink,bt-hci-b91";
status = "disabled";
};

trng0: trng@80101800 {
compatible = "telink,b91-trng";
reg = <0x80101800 0x20>;
Expand Down

0 comments on commit 5f1a22e

Please sign in to comment.