meta-digi/meta-digi-dey/recipes-connectivity/bluez/bluez5-init/ccmp1/bluetooth-init

94 lines
1.9 KiB
Bash

#!/bin/sh
#===============================================================================
#
# Copyright (C) 2025 by Digi International Inc.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
#
# !Description: Initialize Bluetooth interface
#
#===============================================================================
SCRIPTNAME="$(basename ${0})"
RETRIES=3
HCI_IFACE="hci0"
MODULE_NAME="hci_uart"
if grep -qs '\<digi,ccmp13\>' /proc/device-tree/compatible; then
# CCMP13 - BT_REG_EN - GPIO PD13
GPIO_CHIP="GPIOD"
GPIO_NUMBER="13"
else
# CCMP15 - BT_REG_EN - GPIO PZ6
GPIO_CHIP="GPIOZ"
GPIO_NUMBER="6"
fi
log() {
if type "systemd-cat" >/dev/null 2>/dev/null; then
systemd-cat -p "${1}" -t "${SCRIPTNAME}" printf "%s" "${2}"
else
logger -p "${1}" -t "${SCRIPTNAME}" "${2}"
fi
}
bt_power() {
gpioset "${GPIO_CHIP}" "${GPIO_NUMBER}"="${1}"
}
is_kernel_module_loaded() {
lsmod | grep -qs -w "^${MODULE_NAME}"
}
bt_init() {
# If module is already loaded, skip
is_kernel_module_loaded && log info "kernel module already present, skipping" && return 1
for i in $(seq ${RETRIES}); do
# Perform a power cycle
bt_power 0
sleep 0.5
bt_power 1
# Load manually the kernel module
modprobe "${MODULE_NAME}"
sleep 1
is_kernel_module_loaded && log info "Bluetooth activated" && return 0
log err "Cannot initialize Bluetooth, retrying..."
done
log err "Failed to initialize Bluetooth interface"
return 1
}
bt_stop() {
hciconfig ${HCI_IFACE} down
# Unload HCI driver instance
rmmod hci_uart
rmmod btbcm
# Disable Bluetooth chip
bt_power 0 && log info "Bluetooth disabled" && return 0
}
case "$1" in
start)
bt_init
;;
stop)
bt_stop
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac