audio: Add audio amplifier HAL

This is a squash of the following commits.

Author: Ethan Chen <intervigil@gmail.com>
Date:   Fri, 03 Jul 2015 21:35:30 -0700
    audio: Notify amplifier HAL of device enable/disable
    Change-Id: Ice808c9b55a9e3bc8bafe5ca3ff555377d38dd8f

Author: Scott Mertz <scott@cyngn.com>
Date:   Fri, 11 Sep 2015 12:09:06 -0700
    amplifier: add set_parameters method
    - audio hal should pass the audio_device parameters to the amplifier
      to allow the amplifier to make decisions based on the additional
      parameters.  For example, we may want to change settings for DTS
      TrueMedia use case.
    Change-Id: Iccf6ef7ced2abd2e12e857eea8e580cda15eec04

Author: Scott Mertz <scott@cyngn.com>
Date: Wed, 21 Oct 2015 10:11:49 -0700
    audio_amplifier: fix compilation
    stdlib.h is needed explicitely with the new
    toolchain/toolchain options.
    Change-Id: I876041828310ec8f9fb1e0d636a38cce1977d9b9

Author: Ethan Chen <intervigil@gmail.com>
Date: Fri, 15 Jan 2016 16:12:39 -0800
    amplifier: Fix compilation warning
    * Forward declare str_parms
    Change-Id: Iafb0ad8e4b0696807cfe568d0f89ce5dad6bcade

Author: codeworkx <daniel.hillenbrand@codeworkx.de>
Date:   Sat Mar 24 21:30:34 2018 +0100
    audio_amplifier: add hooks for stream parameter manipulation
    This allows to modify audio parameters before the first
    device selection happens which is necessary when setting ANC parameter
    since it must happen before audio hal hits voice_start_call() and
    selects devices. If setting ANC after stream has been started it will
    trigger a re-selection of devices and cause a short downtime.
    Change-Id: Icfddab5ce27235c1567a0557e63b3a7421778187

Author: Pig <pig.priv@gmail.com>
Date:   Tue Sep 22 21:41:59 2020 +0800
    audio_amplifier: Add new hook for setting amp feedback
    Change-Id: Ied5e1ccecd1eec39522055b9fdcfb21f8c101670

Author: Alexander Koskovich <akoskovich@pm.me>
Date:   Sun Jul 31 12:12:39 2022 -0700
    libhardware: audio_ampifier: Fix compile
     * Compiled as apart of vendor-snapshot, so need to
       make sure it compiles.
    Change-Id: Iae8a50a440e5cdc3c89a2e752e86adccbb50a506

Author: Luofan Chen <clfbbn@gmail.com>
Date:   Wed Aug 17 15:38:41 2022 +0800
    audio_amplifier: Add hook for amplifier calibration
    Sometimes the amplifier will need to do calibration and fw download
    on the very first boot. For example, it will need to load calibration
    files from another partition, download calibration firmware to the
    amplifier and play silence sound for some seconds.
    While the current interface, amp_module_open, is able to handle most
    of the operations mentioned above, it is not able to allow the HAL
    to play silence sound, because it cannot get the needed audio_device,
    or adev, it is not able to enable the snd device, and write silence
    pcm data using pcm_write interface.
    This change adds an interface to allow the audio_amplifier HAL to
    calibrate the amplifier. It passes the required audio_device struct
    to audio_amplifier as a parameter, and is called after opening the
    audio_amplifier module.
    Change-Id: I9d497fb5d9716bbcbc6ef9035205ee18da994e72

Change-Id: I0473143057d542c8fa0a3ec4a67ce277900badbe
diff --git a/include/hardware/audio_amplifier.h b/include/hardware/audio_amplifier.h
new file mode 120000
index 0000000..18a66ec
--- /dev/null
+++ b/include/hardware/audio_amplifier.h
@@ -0,0 +1 @@
+../../include_all/hardware/audio_amplifier.h
\ No newline at end of file
diff --git a/include_all/hardware/audio_amplifier.h b/include_all/hardware/audio_amplifier.h
new file mode 100644
index 0000000..4b6f634
--- /dev/null
+++ b/include_all/hardware/audio_amplifier.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2015, The CyanogenMod Project
+ *
+ * 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 CM_AUDIO_AMPLIFIER_INTERFACE_H
+#define CM_AUDIO_AMPLIFIER_INTERFACE_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/audio.h>
+#include <hardware/hardware.h>
+
+#include <system/audio.h>
+
+__BEGIN_DECLS
+
+#define AMPLIFIER_HARDWARE_MODULE_ID "audio_amplifier"
+
+#define AMPLIFIER_HARDWARE_INTERFACE "audio_amplifier_hw_if"
+
+#define AMPLIFIER_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
+
+#define AMPLIFIER_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
+#define AMPLIFIER_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
+#define AMPLIFIER_DEVICE_API_VERSION_2_1 HARDWARE_DEVICE_API_VERSION(2, 1)
+#define AMPLIFIER_DEVICE_API_VERSION_CURRENT AMPLIFIER_DEVICE_API_VERSION_2_1
+
+struct str_parms;
+
+typedef struct amplifier_device {
+    /**
+     * Common methods of the amplifier device. This *must* be the first member
+     * of amplifier_device as users of this structure will cast a hw_device_t
+     * to amplifier_device pointer in contexts where it's known
+     * the hw_device_t references a amplifier_device.
+     */
+    struct hw_device_t common;
+
+    /**
+     * Notify amplifier device of current input devices
+     *
+     * This function should handle only input devices.
+     */
+    int (*set_input_devices)(struct amplifier_device *device, uint32_t devices);
+
+    /**
+     * Notify amplifier device of current output devices
+     *
+     * This function should handle only output devices.
+     */
+    int (*set_output_devices)(struct amplifier_device *device, uint32_t devices);
+
+    /**
+     * Notify amplifier device of output device enable/disable
+     *
+     * This function should handle only output devices.
+     */
+    int (*enable_output_devices)(struct amplifier_device *device,
+            uint32_t devices, bool enable);
+
+    /**
+     * Notify amplifier device of input device enable/disable
+     *
+     * This function should handle only input devices.
+     */
+    int (*enable_input_devices)(struct amplifier_device *device,
+            uint32_t devices, bool enable);
+
+    /**
+     * Notify amplifier device about current audio mode
+     */
+    int (*set_mode)(struct amplifier_device *device, audio_mode_t mode);
+
+    /**
+     * Notify amplifier device that an output stream has started
+     */
+    int (*output_stream_start)(struct amplifier_device *device,
+            struct audio_stream_out *stream, bool offload);
+
+    /**
+     * Notify amplifier device that an input stream has started
+     */
+    int (*input_stream_start)(struct amplifier_device *device,
+            struct audio_stream_in *stream);
+
+    /**
+     * Notify amplifier device that an output stream has stopped
+     */
+    int (*output_stream_standby)(struct amplifier_device *device,
+            struct audio_stream_out *stream);
+
+    /**
+     * Notify amplifier device that an input stream has stopped
+     */
+    int (*input_stream_standby)(struct amplifier_device *device,
+            struct audio_stream_in *stream);
+
+    /**
+     * set/get audio device parameters.
+     */
+    int (*set_parameters)(struct amplifier_device *device,
+        struct str_parms *parms);
+
+    /**
+     * set/get output stream parameters.
+     */
+    int (*out_set_parameters)(struct amplifier_device *device,
+        struct str_parms *parms);
+
+    /**
+     * set/get input stream parameters.
+     */
+    int (*in_set_parameters)(struct amplifier_device *device,
+        struct str_parms *parms);
+
+    /**
+     * set feedback
+     */
+    int (*set_feedback)(struct amplifier_device *device,
+        void *adev, uint32_t devices, bool enable);
+
+    /**
+     * Amplifier calibration
+     */
+    int (*calibrate)(void *adev);
+} amplifier_device_t;
+
+typedef struct amplifier_module {
+    /**
+     * Common methods of the amplifier module. This *must* be the first member
+     * of amplifier_module as users of this structure will cast a hw_module_t
+     * to amplifier_module pointer in contexts where it's known
+     * the hw_module_t references a amplifier_module.
+     */
+    struct hw_module_t common;
+} amplifier_module_t;
+
+/** convenience API for opening and closing a supported device */
+
+static inline int amplifier_device_open(const struct hw_module_t *module,
+        struct amplifier_device **device)
+{
+    return module->methods->open(module, AMPLIFIER_HARDWARE_INTERFACE,
+            (struct hw_device_t **) device);
+}
+
+static inline int amplifier_device_close(struct amplifier_device *device)
+{
+    return device->common.close(&device->common);
+}
+
+__END_DECLS
+
+#endif  // CM_AUDIO_AMPLIFIER_INTERFACE_H
diff --git a/modules/audio/Android.bp b/modules/audio/Android.bp
index 1b34336..12de12b 100644
--- a/modules/audio/Android.bp
+++ b/modules/audio/Android.bp
@@ -70,3 +70,20 @@
     ],
     cflags: ["-Wall", "-Werror", "-Wno-unused-parameter"],
 }
+
+// The stub audio amplifier HAL module that can be used as a skeleton for
+// new implementations.
+cc_library_shared {
+    name: "audio_amplifier.default",
+    relative_install_path: "hw",
+    proprietary: true,
+    srcs: ["audio_amplifier.c"],
+    header_libs: [
+        "libhardware_headers",
+    ],
+    shared_libs: [
+        "libcutils",
+        "liblog",
+    ],
+    cflags: ["-Wall", "-Werror", "-Wno-unused-parameter"],
+}
diff --git a/modules/audio/audio_amplifier.c b/modules/audio/audio_amplifier.c
new file mode 100644
index 0000000..7837b4d
--- /dev/null
+++ b/modules/audio/audio_amplifier.c
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2015 The CyanogenMod Open Source Project
+ *
+ * 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 "amplifier_default"
+//#define LOG_NDEBUG 0
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include <cutils/log.h>
+#include <cutils/str_parms.h>
+
+#include <hardware/audio_amplifier.h>
+#include <hardware/hardware.h>
+
+static int amp_set_input_devices(amplifier_device_t *device, uint32_t devices)
+{
+    return 0;
+}
+
+static int amp_set_output_devices(amplifier_device_t *device, uint32_t devices)
+{
+    return 0;
+}
+
+static int amp_enable_output_devices(amplifier_device_t *device,
+        uint32_t devices, bool enable)
+{
+    return 0;
+}
+
+static int amp_enable_input_devices(amplifier_device_t *device,
+        uint32_t devices, bool enable)
+{
+    return 0;
+}
+
+static int amp_set_mode(amplifier_device_t *device, audio_mode_t mode)
+{
+    return 0;
+}
+
+static int amp_output_stream_start(amplifier_device_t *device,
+        struct audio_stream_out *stream, bool offload)
+{
+    return 0;
+}
+
+static int amp_input_stream_start(amplifier_device_t *device,
+        struct audio_stream_in *stream)
+{
+    return 0;
+}
+
+static int amp_output_stream_standby(amplifier_device_t *device,
+        struct audio_stream_out *stream)
+{
+    return 0;
+}
+
+static int amp_input_stream_standby(amplifier_device_t *device,
+        struct audio_stream_in *stream)
+{
+    return 0;
+}
+
+static int amp_set_parameters(struct amplifier_device *device,
+        struct str_parms *parms)
+{
+    return 0;
+}
+
+static int amp_out_set_parameters(struct amplifier_device *device,
+        struct str_parms *parms)
+{
+    return 0;
+}
+
+static int amp_in_set_parameters(struct amplifier_device *device,
+        struct str_parms *parms)
+{
+    return 0;
+}
+
+static int amp_set_feedback(struct amplifier_device *device,
+        void *adev, uint32_t devices, bool enable)
+{
+    return 0;
+}
+
+static int amp_dev_close(hw_device_t *device)
+{
+    if (device)
+        free(device);
+
+    return 0;
+}
+
+static int amp_module_open(const hw_module_t *module, const char *name,
+        hw_device_t **device)
+{
+    if (strcmp(name, AMPLIFIER_HARDWARE_INTERFACE)) {
+        ALOGE("%s:%d: %s does not match amplifier hardware interface name\n",
+                __func__, __LINE__, name);
+        return -ENODEV;
+    }
+
+    amplifier_device_t *amp_dev = calloc(1, sizeof(amplifier_device_t));
+    if (!amp_dev) {
+        ALOGE("%s:%d: Unable to allocate memory for amplifier device\n",
+                __func__, __LINE__);
+        return -ENOMEM;
+    }
+
+    amp_dev->common.tag = HARDWARE_DEVICE_TAG;
+    amp_dev->common.module = (hw_module_t *) module;
+    amp_dev->common.version = HARDWARE_DEVICE_API_VERSION(1, 0);
+    amp_dev->common.close = amp_dev_close;
+
+    amp_dev->set_input_devices = amp_set_input_devices;
+    amp_dev->set_output_devices = amp_set_output_devices;
+    amp_dev->enable_output_devices = amp_enable_output_devices;
+    amp_dev->enable_input_devices = amp_enable_input_devices;
+    amp_dev->set_mode = amp_set_mode;
+    amp_dev->output_stream_start = amp_output_stream_start;
+    amp_dev->input_stream_start = amp_input_stream_start;
+    amp_dev->output_stream_standby = amp_output_stream_standby;
+    amp_dev->input_stream_standby = amp_input_stream_standby;
+    amp_dev->set_parameters = amp_set_parameters;
+    amp_dev->out_set_parameters = amp_out_set_parameters;
+    amp_dev->in_set_parameters = amp_in_set_parameters;
+    amp_dev->set_feedback = amp_set_feedback;
+
+    *device = (hw_device_t *) amp_dev;
+
+    return 0;
+}
+
+static struct hw_module_methods_t hal_module_methods = {
+    .open = amp_module_open,
+};
+
+amplifier_module_t HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version = AMPLIFIER_MODULE_API_VERSION_0_1,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = AMPLIFIER_HARDWARE_MODULE_ID,
+        .name = "Default audio amplifier HAL",
+        .author = "The CyanogenMod Open Source Project",
+        .methods = &hal_module_methods,
+    },
+};