hal: Add open source HAL for Elliptic Ultrasound
Author: Balázs Triszka <balika011@gmail.com>
Date: Thu May 11 03:19:29 2017 +0200
msm8998: ultrasound: Initial open source hal for Elliptic Ultrasound
* Needed for proximity sensor on Xiaomi Mi MIX
[Arasthel]: Change PCM id for Mi MIX 2 implementation
Change-Id: Iaef2266bc1b853d7a9d1e2a906014c6c91019d5f
Signed-off-by: Balázs Triszka <balika011@gmail.com>
Author: Michael Bestas <mkbestas@lineageos.org>
Date: Sat Feb 10 00:28:00 2018 +0200
msm8998: ultrasound: Remove unused code
* Params ultrasound_set_manual_calibration and ultrasound_set_sensitivity
do not exist.
Change-Id: I0004949db19b6ab7d49f20e422984e06a970cfe9
Author: Demon Singur <demonsingur@gmail.com>
Date: Sat Apr 21 09:08:03 2018 +0000
msm8998: hal: Update ultrasound route hacks
Change-Id: If002503dfba0f005f73a4455d68bbcce9d2f617e
Change-Id: I119316f264afbda9faf24950edfbca3891aa9769
Author: Vol Zhdanov <wight554@gmail.com>
Date: Fri Feb 1 02:16:26 2019 +0000
hal: fix channels swap for elliptic ultrasound devices
Change-Id: Id45b075c4ad098c95fcb617da10c56d38565b9c8
Change-Id: Icb18f5b41483d33188be103e13b6c915b6e681b8
diff --git a/hal/Android.mk b/hal/Android.mk
index 5fb3322..d5aa190 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -336,6 +336,11 @@
LOCAL_SHARED_LIBRARIES += vendor.qti.hardware.audiohalext@1.0
endif
+ifeq ($(strip $(AUDIO_FEATURE_ELLIPTIC_ULTRASOUND_SUPPORT)),true)
+ LOCAL_CFLAGS += -DELLIPTIC_ULTRASOUND_ENABLED
+ LOCAL_SRC_FILES += audio_extn/ultrasound.c
+endif
+
LOCAL_CFLAGS += -D_GNU_SOURCE
LOCAL_CFLAGS += -Wall -Werror
diff --git a/hal/audio_extn/ultrasound.c b/hal/audio_extn/ultrasound.c
new file mode 100644
index 0000000..1199e22
--- /dev/null
+++ b/hal/audio_extn/ultrasound.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (c) 2017-2018 The LineageOS Project
+ * Copyright (c) 2017 Balázs Triszka <balika011@protonmail.ch>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "ultrasound"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <cutils/log.h>
+#include "audio_hw.h"
+#include "platform_api.h"
+#include <platform.h>
+#include "ultrasound.h"
+
+#define ULTRASOUND_CALIBRATION_FILE "/persist/audio/us_cal"
+#define ULTRASOUND_CALIBRATION_MIXER "Ultrasound Calibration Data"
+
+enum {
+ ULTRASOUND_STATUS_DEFAULT,
+ ULTRASOUND_STATUS_STARTED,
+ ULTRASOUND_STATUS_STOPPED,
+};
+
+struct pcm_config pcm_config_us = {
+ .channels = 1,
+ .rate = 96000,
+ .period_size = 1024,
+ .period_count = 2,
+ .format = PCM_FORMAT_S16_LE,
+};
+
+struct ultrasound_device {
+ struct pcm *rx_pcm, *tx_pcm;
+ int state;
+ struct audio_device *adev;
+};
+
+static struct ultrasound_device *us = NULL;
+
+void us_cal_load(void)
+{
+ FILE *f;
+ char buff[5] = {0}, us_cal[64];
+ struct mixer_ctl * ctl;
+ int rc;
+
+ f = fopen(ULTRASOUND_CALIBRATION_FILE, "r");
+ if (!f) {
+ ALOGE("%s: Cannot open calibration file: %s",
+ __func__, ULTRASOUND_CALIBRATION_FILE);
+ return;
+ }
+
+ for (size_t i = 0; i < sizeof(us_cal); i++) {
+ fread(buff, 1, sizeof(buff), f);
+ us_cal[i] = strtol(buff, 0, 16);
+ }
+ fclose(f);
+
+ ctl = mixer_get_ctl_by_name(us->adev->mixer, ULTRASOUND_CALIBRATION_MIXER);
+ if (!ctl) {
+ ALOGE("%s: Could not get ctl for mixer cmd - %s",
+ __func__, ULTRASOUND_CALIBRATION_MIXER);
+ return;
+ }
+
+ rc = mixer_ctl_set_array(ctl, us_cal, sizeof(us_cal));
+ if (rc < 0)
+ ALOGE("%s: Could not set ctl, error:%d ", __func__, rc);
+}
+
+int us_init(struct audio_device *adev)
+{
+ ALOGD("%s: enter", __func__);
+
+ if (us) {
+ ALOGI("%s: ultrasound has been initialized!", __func__);
+ return 0;
+ }
+
+ us = calloc(1, sizeof(struct ultrasound_device));
+ if (!us) {
+ ALOGE("%s: Out of memory!", __func__);
+ return -ENOMEM;
+ }
+
+ us->adev = adev;
+
+ us_cal_load();
+
+ ALOGD("%s: exit, status(0)", __func__);
+
+ return 0;
+}
+
+void us_deinit(void)
+{
+ ALOGD("%s: enter", __func__);
+
+ if (us) {
+ free(us);
+ us = NULL;
+ }
+
+ ALOGD("%s: exit", __func__);
+}
+
+int stop_us(void)
+{
+ struct audio_usecase *rx_usecase, *tx_usecase;
+ int rc = 0;
+
+ ALOGD("%s: enter usecase: ultrasound", __func__);
+
+ us->state = ULTRASOUND_STATUS_STOPPED;
+ if (us->rx_pcm) {
+ pcm_close(us->rx_pcm);
+ us->rx_pcm = NULL;
+ }
+
+ if (us->tx_pcm) {
+ pcm_close(us->tx_pcm);
+ us->tx_pcm = NULL;
+ }
+
+ rx_usecase = get_usecase_from_list(us->adev, USECASE_AUDIO_ULTRASOUND_RX);
+ if (!rx_usecase) {
+ ALOGE("%s: Could not find the usecase (%d) in the list",
+ __func__, USECASE_AUDIO_ULTRASOUND_RX);
+ rc = -EINVAL;
+ } else {
+ disable_audio_route(us->adev, rx_usecase);
+ disable_snd_device(us->adev, rx_usecase->out_snd_device);
+ list_remove(&rx_usecase->list);
+ free(rx_usecase);
+ }
+
+ tx_usecase = get_usecase_from_list(us->adev, USECASE_AUDIO_ULTRASOUND_TX);
+ if (!rx_usecase) {
+ ALOGE("%s: Could not find the usecase (%d) in the list",
+ __func__, USECASE_AUDIO_ULTRASOUND_TX);
+ rc = -EINVAL;
+ } else {
+ disable_audio_route(us->adev, tx_usecase);
+ disable_snd_device(us->adev, tx_usecase->in_snd_device);
+ list_remove(&tx_usecase->list);
+ free(tx_usecase);
+ }
+
+ ALOGD("%s: exit: status(%d)", __func__, rc);
+
+ return rc;
+}
+
+int us_start(void)
+{
+ int rx_device_id, tx_device_id;
+ struct audio_usecase *rx_usecase, *tx_usecase;
+
+ ALOGD("%s: enter", __func__);
+
+ if (!us || us->state == ULTRASOUND_STATUS_STARTED)
+ return -EPERM;
+
+ ALOGD("%s: enter usecase: ultrasound", __func__);
+ rx_device_id = platform_get_pcm_device_id(USECASE_AUDIO_ULTRASOUND_RX, PCM_PLAYBACK);
+ tx_device_id = platform_get_pcm_device_id(USECASE_AUDIO_ULTRASOUND_TX, PCM_CAPTURE);
+ if (rx_device_id < 0 || tx_device_id < 0) {
+ ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(ultrasound)",
+ __func__, rx_device_id, tx_device_id);
+ stop_us();
+ ALOGE("%s: exit: status(%d)", __func__, -EIO);
+ return -EIO;
+ }
+
+ rx_usecase = calloc(1, sizeof(struct audio_usecase));
+ if (!rx_usecase) {
+ ALOGE("%s: Out of memory!", __func__);
+ return -ENOMEM;
+ }
+
+ rx_usecase->type = PCM_PLAYBACK;
+ rx_usecase->out_snd_device = SND_DEVICE_OUT_ULTRASOUND_HANDSET;
+ rx_usecase->id = USECASE_AUDIO_ULTRASOUND_RX;
+ list_add_tail(&us->adev->usecase_list, &rx_usecase->list);
+
+ enable_snd_device(us->adev, SND_DEVICE_OUT_ULTRASOUND_HANDSET);
+ enable_audio_route(us->adev, rx_usecase);
+ ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
+ __func__, us->adev->snd_card, rx_device_id);
+ us->rx_pcm = pcm_open(us->adev->snd_card, rx_device_id, PCM_OUT, &pcm_config_us);
+ if (us->rx_pcm && !pcm_is_ready(us->rx_pcm)) {
+ ALOGE("%s: %s", __func__, pcm_get_error(us->rx_pcm));
+ stop_us();
+ ALOGE("%s: exit: status(%d)", __func__, -EIO);
+ return -EIO;
+ }
+
+ tx_usecase = calloc(1, sizeof(struct audio_usecase));
+ if (!tx_usecase) {
+ ALOGE("%s: Out of memory!", __func__);
+ return -ENOMEM;
+ }
+
+ tx_usecase->type = PCM_CAPTURE;
+ tx_usecase->in_snd_device = SND_DEVICE_IN_ULTRASOUND_MIC;
+ tx_usecase->id = USECASE_AUDIO_ULTRASOUND_TX;
+ list_add_tail(&us->adev->usecase_list, &tx_usecase->list);
+
+ enable_snd_device(us->adev, SND_DEVICE_IN_ULTRASOUND_MIC);
+ enable_audio_route(us->adev, tx_usecase);
+ ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
+ __func__, us->adev->snd_card, tx_device_id);
+ us->tx_pcm = pcm_open(us->adev->snd_card, tx_device_id, PCM_IN, &pcm_config_us);
+ if (us->tx_pcm && !pcm_is_ready(us->tx_pcm)) {
+ ALOGD("%s: %s", __func__, pcm_get_error(us->tx_pcm));
+ stop_us();
+ ALOGE("%s: exit: status(%d)", __func__, -EIO);
+ return -EIO;
+ }
+
+ pcm_start(us->rx_pcm);
+ pcm_start(us->tx_pcm);
+ us->state = ULTRASOUND_STATUS_STARTED;
+
+ ALOGD("%s: exit, status(0)", __func__);
+
+ return 0;
+}
+
+int us_stop(void)
+{
+ ALOGD("%s: enter", __func__);
+
+ if (!us || us->state != ULTRASOUND_STATUS_STARTED)
+ return -EPERM;
+
+ stop_us();
+
+ return 0;
+}
diff --git a/hal/audio_extn/ultrasound.h b/hal/audio_extn/ultrasound.h
new file mode 100644
index 0000000..0bb24ca
--- /dev/null
+++ b/hal/audio_extn/ultrasound.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2017-2018 The LineageOS Project
+ * Copyright (c) 2017 Balázs Triszka <balika011@protonmail.ch>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ULTRASOUND_H
+#define ULTRASOUND_H
+
+#ifndef ELLIPTIC_ULTRASOUND_ENABLED
+#define us_init(adev) (0)
+#define us_deinit() (0)
+#define us_start() (0)
+#define us_stop() (0)
+#else
+int us_init(struct audio_device *adev);
+void us_deinit(void);
+int us_start(void);
+int us_stop(void);
+#endif
+
+#endif
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index d6194cb..27e2fa3 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -75,6 +75,7 @@
#include "audio_extn.h"
#include "voice_extn.h"
#include "ip_hdlr_intf.h"
+#include "ultrasound.h"
#include "sound/compress_params.h"
#include "sound/asound.h"
@@ -407,6 +408,10 @@
[USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
[USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
[USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
+
+ /* For Elliptic Ultrasound proximity sensor */
+ [USECASE_AUDIO_ULTRASOUND_RX] = "ultrasound-rx",
+ [USECASE_AUDIO_ULTRASOUND_TX] = "ultrasound-tx",
};
static const audio_usecase_t offload_usecases[] = {
@@ -1139,6 +1144,9 @@
audio_extn_utils_send_app_type_cfg(adev, usecase);
if (audio_extn_is_maxx_audio_enabled())
audio_extn_ma_set_device(usecase);
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ if (usecase->id != USECASE_AUDIO_ULTRASOUND_TX)
+#endif
audio_extn_utils_send_audio_calibration(adev, usecase);
if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
out = usecase->stream.out;
@@ -1308,6 +1316,10 @@
ST_EVENT_SND_DEVICE_BUSY);
audio_extn_listen_update_device_status(snd_device,
LISTEN_EVENT_SND_DEVICE_BUSY);
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ if (snd_device != SND_DEVICE_OUT_ULTRASOUND_HANDSET &&
+ snd_device != SND_DEVICE_IN_ULTRASOUND_MIC)
+#endif
if (platform_get_snd_device_acdb_id(snd_device) < 0) {
audio_extn_sound_trigger_update_device_status(snd_device,
ST_EVENT_SND_DEVICE_FREE);
@@ -1613,6 +1625,12 @@
platform_get_snd_device_name(snd_device),
platform_get_snd_device_name(usecase->out_snd_device),
platform_check_backends_match(snd_device, usecase->out_snd_device));
+
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ if (usecase->id == USECASE_AUDIO_ULTRASOUND_RX)
+ continue;
+#endif
+
if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
(usecase->type != PCM_PASSTHROUGH)) {
uc_derive_snd_device = derive_playback_snd_device(adev->platform,
@@ -1758,6 +1776,12 @@
/*
* TODO: Enhance below condition to handle BT sco/USB multi recording
*/
+
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ if (usecase->id == USECASE_AUDIO_ULTRASOUND_TX)
+ continue;
+#endif
+
if (usecase->type != PCM_PLAYBACK &&
usecase != uc_info &&
(usecase->in_snd_device != snd_device || force_routing) &&
@@ -8348,6 +8372,15 @@
}
}
+ ret = str_parms_get_int(parms, "ultrasound-sensor", &val);
+ if (ret >= 0) {
+ if (val == 1) {
+ us_start();
+ } else {
+ us_stop();
+ }
+ }
+
audio_extn_set_parameters(adev, parms);
done:
str_parms_destroy(parms);
@@ -9371,6 +9404,9 @@
free(device);
adev = NULL;
}
+
+ us_deinit();
+
pthread_mutex_unlock(&adev_init_lock);
enable_gcov();
return 0;
@@ -9750,6 +9786,9 @@
adev->vr_audio_mode_enabled = false;
audio_extn_ds2_enable(adev);
+
+ us_init(adev);
+
*device = &adev->device.common;
if (k_enable_extended_precision)
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 9b7bf5b..dc62f83 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -230,6 +230,11 @@
/*Audio FM Tuner usecase*/
USECASE_AUDIO_FM_TUNER_EXT,
+
+ /* Elliptic Ultrasound */
+ USECASE_AUDIO_ULTRASOUND_RX,
+ USECASE_AUDIO_ULTRASOUND_TX,
+
AUDIO_USECASE_MAX
};
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 7d63d17..4a1d49c 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -482,6 +482,9 @@
[USECASE_AUDIO_PLAYBACK_PHONE] = {PHONE_PCM_DEVICE,
PHONE_PCM_DEVICE},
[USECASE_AUDIO_FM_TUNER_EXT] = {-1, -1},
+ [USECASE_AUDIO_ULTRASOUND_RX] = {ULTRASOUND_PCM_DEVICE, -1},
+ [USECASE_AUDIO_ULTRASOUND_TX] = {-1, ULTRASOUND_PCM_DEVICE},
+
};
/* Array to store sound devices */
@@ -584,6 +587,7 @@
[SND_DEVICE_OUT_BUS_SYS] = "bus-speaker",
[SND_DEVICE_OUT_BUS_NAV] = "bus-speaker",
[SND_DEVICE_OUT_BUS_PHN] = "bus-speaker",
+ [SND_DEVICE_OUT_ULTRASOUND_HANDSET] = "ultrasound-handset",
/* Capture sound devices */
[SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
@@ -722,6 +726,7 @@
[SND_DEVICE_IN_HANDSET_QMIC_AND_EC_REF_LOOPBACK] = "handset-qmic-and-ec-ref-loopback",
[SND_DEVICE_IN_HANDSET_6MIC_AND_EC_REF_LOOPBACK] = "handset-6mic-and-ec-ref-loopback",
[SND_DEVICE_IN_HANDSET_8MIC_AND_EC_REF_LOOPBACK] = "handset-8mic-and-ec-ref-loopback",
+ [SND_DEVICE_IN_ULTRASOUND_MIC] = "ultrasound-mic",
};
// Platform specific backend bit width table
@@ -1281,6 +1286,8 @@
{TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION)},
{TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE)},
{TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_PHONE)},
+ {TO_NAME_INDEX(USECASE_AUDIO_ULTRASOUND_RX)},
+ {TO_NAME_INDEX(USECASE_AUDIO_ULTRASOUND_TX)},
};
static const struct name_to_index usecase_type_index[USECASE_TYPE_MAX] = {
@@ -1748,7 +1755,11 @@
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase != NULL && usecase->stream.out &&
- usecase->type == PCM_PLAYBACK) {
+ usecase->type == PCM_PLAYBACK
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ && usecase->id != USECASE_AUDIO_ULTRASOUND_RX
+#endif
+ ) {
int new_snd_device[2] = {0};
int i, num_devices = 1;
@@ -2230,6 +2241,7 @@
hw_interface_table[SND_DEVICE_OUT_BUS_SYS] = strdup("TERT_TDM_RX_0");
hw_interface_table[SND_DEVICE_OUT_BUS_NAV] = strdup("TERT_TDM_RX_1");
hw_interface_table[SND_DEVICE_OUT_BUS_PHN] = strdup("TERT_TDM_RX_2");
+ hw_interface_table[SND_DEVICE_OUT_ULTRASOUND_HANDSET] = strdup("SLIMBUS_0_RX");
hw_interface_table[SND_DEVICE_IN_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_SB] = strdup("SLIMBUS_0_TX");
hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_EXTERNAL] = strdup("SLIMBUS_0_TX");
@@ -2357,6 +2369,8 @@
hw_interface_table[SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT] = strdup("SLIMBUS_0_TX");
hw_interface_table[SND_DEVICE_IN_VOICE_HEARING_AID] = strdup("SLIMBUS_0_TX");
hw_interface_table[SND_DEVICE_IN_BUS] = strdup("TERT_TDM_TX_0");
+ hw_interface_table[SND_DEVICE_IN_ULTRASOUND_MIC] = strdup("SLIMBUS_0_TX");
+
my_data->max_mic_count = PLATFORM_DEFAULT_MIC_COUNT;
/*remove ALAC & APE from DSP decoder list based on software decoder availability*/
@@ -10598,6 +10612,9 @@
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
+#ifdef ELLIPTIC_ULTRASOUND_ENABLED
+ if (usecase->id != USECASE_AUDIO_ULTRASOUND_RX)
+#endif
if (usecase->stream.out && usecase->type == PCM_PLAYBACK &&
usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
/*
diff --git a/hal/msm8974/platform.h b/hal/msm8974/platform.h
index f638b44..e3dfe84 100644
--- a/hal/msm8974/platform.h
+++ b/hal/msm8974/platform.h
@@ -181,6 +181,7 @@
SND_DEVICE_OUT_BUS_SYS,
SND_DEVICE_OUT_BUS_NAV,
SND_DEVICE_OUT_BUS_PHN,
+ SND_DEVICE_OUT_ULTRASOUND_HANDSET,
SND_DEVICE_OUT_END,
/*
@@ -323,6 +324,7 @@
SND_DEVICE_IN_HANDSET_QMIC_AND_EC_REF_LOOPBACK,
SND_DEVICE_IN_HANDSET_6MIC_AND_EC_REF_LOOPBACK,
SND_DEVICE_IN_HANDSET_8MIC_AND_EC_REF_LOOPBACK,
+ SND_DEVICE_IN_ULTRASOUND_MIC,
SND_DEVICE_IN_END,
SND_DEVICE_MAX = SND_DEVICE_IN_END,
@@ -623,6 +625,8 @@
#define AFE_PROXY_PLAYBACK_PCM_DEVICE 7
#define AFE_PROXY_RECORD_PCM_DEVICE 8
+#define ULTRASOUND_PCM_DEVICE 10
+
#ifdef PLATFORM_MSM8x26
#ifdef EXTERNAL_BT_SUPPORTED
#define HFP_SCO_RX 10 // AUXPCM Hostless