Audio System + Drivers (#562)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
|
||||
|
||||
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
|
||||
|
||||
tactility_add_module(aw88298-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel audio-codec-module esp_codec_dev esp_driver_i2s
|
||||
)
|
||||
@@ -0,0 +1,195 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# AW88298 speaker amplifier
|
||||
|
||||
A driver for the `AW88298` smart speaker power amplifier by Awinic, wired as an
|
||||
output-only `AUDIO_CODEC_TYPE` device. The I2C bus is the device's parent; the I2S
|
||||
controller carrying audio data is referenced via a devicetree phandle.
|
||||
|
||||
Wraps Espressif's `esp_codec_dev` AW88298 implementation. If a hardware reset
|
||||
(`pin-reset`) is wired, this driver pulses it itself before opening the codec,
|
||||
rather than handing it to `esp_codec_dev`'s own reset handling -- the vendor
|
||||
driver treats a reset pin index of `0` as "no pin wired" and silently skips the
|
||||
reset, which doesn't match the `audio_codec_adapter_new_gpio()` convention used
|
||||
elsewhere in this codebase (a single-pin array is always referenced at index 0).
|
||||
|
||||
See https://www.awinic.com/en/productDetail/AW88298
|
||||
|
||||
License: [Apache v2.0](LICENSE-Apache-2.0.md)
|
||||
@@ -0,0 +1,15 @@
|
||||
description: Awinic AW88298 smart speaker power amplifier (output-only)
|
||||
|
||||
include: [ "i2c-device.yaml" ]
|
||||
|
||||
compatible: "awinic,aw88298"
|
||||
|
||||
properties:
|
||||
i2s:
|
||||
type: phandle
|
||||
required: true
|
||||
description: "I2S controller device that carries audio data"
|
||||
pin-reset:
|
||||
type: phandles
|
||||
default: GPIO_PIN_SPEC_NONE
|
||||
description: "Hardware reset (RSTN) pin. The AW88298's I2C interface does not respond until this pin is released, so if wired, this driver asserts/releases reset itself before opening the codec, ensuring correct ordering relative to other devicetree devices. Omit if reset is tied high in hardware."
|
||||
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
- TactilityKernel
|
||||
bindings: bindings
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/aw88298.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(aw88298, struct Aw88298Config)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/drivers/audio_codec.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
|
||||
struct Device;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief AW88298 codec device configuration.
|
||||
*
|
||||
* The AW88298 is an output-only (speaker power amp) codec. The I2C bus is the
|
||||
* device's parent (per i2c-device.yaml), while the I2S controller carrying the
|
||||
* audio data is referenced via a devicetree phandle.
|
||||
*
|
||||
* The chip's I2C interface does not respond until its hardware RESET (RSTN)
|
||||
* line is released, so if that pin is wired (e.g. behind a GPIO expander on
|
||||
* M5Stack StackChan/CoreS3), this driver asserts and releases it itself before
|
||||
* opening the codec. This guarantees correct ordering: devicetree GPIO
|
||||
* controller devices (e.g. the expander driving reset_pin) start in
|
||||
* declaration order before this device, whereas board Configuration.cpp
|
||||
* initBoot() callbacks run after ALL devicetree devices have already started
|
||||
* and so cannot reliably gate a devicetree device's own bring-up.
|
||||
*/
|
||||
struct Aw88298Config {
|
||||
/** I2C address on the bus (typically 0x36) */
|
||||
uint8_t address;
|
||||
/** I2S controller device that carries audio data */
|
||||
struct Device* i2s_device;
|
||||
/** Optional hardware reset (RSTN) pin. GPIO_PIN_SPEC_NONE if tied high in hardware. */
|
||||
struct GpioPinSpec reset_pin;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,364 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/aw88298.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/audio_codec.h>
|
||||
#include <tactility/drivers/audio_codec_adapters.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/drivers/i2s_controller.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <aw88298_dac.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
#include <driver/i2s_types.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#define TAG "AW88298"
|
||||
|
||||
namespace {
|
||||
|
||||
// AW88298 native output rate per M5Unified's _speaker_enabled_cb_cores3(); higher rates
|
||||
// (e.g. 44100Hz) additionally need mclk_multiple = I2S_MCLK_MULTIPLE_384 per aw88298_dac.h.
|
||||
constexpr uint32_t NATIVE_SAMPLE_RATE = 16000;
|
||||
|
||||
struct Aw88298Data {
|
||||
const audio_codec_ctrl_if_t* ctrl_if = nullptr;
|
||||
const audio_codec_data_if_t* data_if = nullptr;
|
||||
const audio_codec_if_t* codec_if = nullptr;
|
||||
esp_codec_dev_handle_t codec_device = nullptr;
|
||||
bool is_open = false;
|
||||
esp_codec_dev_sample_info_t open_sample_info = {};
|
||||
};
|
||||
|
||||
#define GET_CONFIG(device) (static_cast<const Aw88298Config*>((device)->config))
|
||||
#define GET_DATA(device) (static_cast<Aw88298Data*>(device_get_driver_data(device)))
|
||||
|
||||
// region AudioCodecApi
|
||||
|
||||
error_t open(Device* device, const struct AudioCodecStreamConfig* config) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (config->direction == AUDIO_CODEC_DIR_INPUT || config->direction == AUDIO_CODEC_DIR_BOTH) {
|
||||
LOG_E(TAG, "AW88298 is output-only");
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
esp_codec_dev_sample_info_t sample_info = {
|
||||
.bits_per_sample = config->bits_per_sample,
|
||||
.channel = config->channels,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = config->sample_rate,
|
||||
.mclk_multiple = (config->sample_rate > 16000) ? I2S_MCLK_MULTIPLE_384 : 0,
|
||||
};
|
||||
|
||||
if (data->is_open) {
|
||||
bool same_config = data->open_sample_info.bits_per_sample == sample_info.bits_per_sample
|
||||
&& data->open_sample_info.channel == sample_info.channel
|
||||
&& data->open_sample_info.sample_rate == sample_info.sample_rate;
|
||||
return same_config ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (esp_codec_dev_open(data->codec_device, &sample_info) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open codec device");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
data->is_open = true;
|
||||
data->open_sample_info = sample_info;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t close(Device* device) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (data->is_open) {
|
||||
esp_codec_dev_close(data->codec_device);
|
||||
data->is_open = false;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t read(Device* device, void* buffer, size_t size, size_t* bytes_read, TickType_t timeout) {
|
||||
(void) device;
|
||||
(void) buffer;
|
||||
(void) size;
|
||||
(void) bytes_read;
|
||||
(void) timeout;
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
error_t write(Device* device, const void* buffer, size_t size, size_t* bytes_written, TickType_t timeout) {
|
||||
(void) timeout;
|
||||
auto* data = GET_DATA(device);
|
||||
if (!data->is_open) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// esp_codec_dev_write returns the number of bytes written (>= 0) on success, or a negative
|
||||
// ESP_CODEC_DEV_* error code on failure -- it does NOT return ESP_CODEC_DEV_OK (0) for
|
||||
// a successful nonzero-length write.
|
||||
int result = esp_codec_dev_write(data->codec_device, const_cast<void*>(buffer), (int) size);
|
||||
if (result < 0) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
*bytes_written = (size_t) result;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t set_volume(Device* device, AudioCodecDirection direction, float volume_percent) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (volume_percent < 0.0f || volume_percent > 100.0f) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
int volume = (int) std::lround(volume_percent);
|
||||
return (esp_codec_dev_set_out_vol(data->codec_device, volume) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_volume(Device* device, AudioCodecDirection direction, float* volume_percent) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int volume = 0;
|
||||
if (esp_codec_dev_get_out_vol(data->codec_device, &volume) != ESP_CODEC_DEV_OK) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
*volume_percent = (float) volume;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t set_mute(Device* device, AudioCodecDirection direction, bool muted) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return (esp_codec_dev_set_out_mute(data->codec_device, muted) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_mute(Device* device, AudioCodecDirection direction, bool* muted) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data->codec_device == nullptr || direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return (esp_codec_dev_get_out_mute(data->codec_device, muted) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t get_native_channels(Device* device, AudioCodecDirection direction, uint8_t* channels) {
|
||||
(void) device;
|
||||
if (direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
*channels = 2;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t get_native_sample_rate(Device* device, AudioCodecDirection direction, uint32_t* rate_hz) {
|
||||
(void) device;
|
||||
if (direction != AUDIO_CODEC_DIR_OUTPUT) {
|
||||
return ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
*rate_hz = NATIVE_SAMPLE_RATE;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t get_capabilities(Device* device, AudioCodecDirection* supported_directions) {
|
||||
(void) device;
|
||||
*supported_directions = AUDIO_CODEC_DIR_OUTPUT;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static const struct AudioCodecApi API = {
|
||||
.open = open,
|
||||
.close = close,
|
||||
.read = read,
|
||||
.write = write,
|
||||
.set_volume = set_volume,
|
||||
.get_volume = get_volume,
|
||||
.set_mute = set_mute,
|
||||
.get_mute = get_mute,
|
||||
.get_native_sample_rate = get_native_sample_rate,
|
||||
.get_native_channels = get_native_channels,
|
||||
.get_capabilities = get_capabilities,
|
||||
.get_input_gain_multiplier = nullptr,
|
||||
};
|
||||
|
||||
// endregion
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
error_t start_device(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
auto* i2c_controller = device_get_parent(device);
|
||||
if (i2c_controller == nullptr || device_get_type(i2c_controller) != &I2C_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "Parent is not an I2C controller");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* i2s_controller = config->i2s_device;
|
||||
if (i2s_controller == nullptr || device_get_type(i2s_controller) != &I2S_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "I2S controller device is not valid");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* data = new Aw88298Data();
|
||||
|
||||
data->ctrl_if = audio_codec_adapter_new_i2c_ctrl(i2c_controller, config->address);
|
||||
data->data_if = audio_codec_adapter_new_i2s_data(i2s_controller);
|
||||
if (data->ctrl_if == nullptr || data->data_if == nullptr) {
|
||||
LOG_E(TAG, "Failed to create adapters");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (data->ctrl_if->open(data->ctrl_if, nullptr, 0) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open control interface");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (data->data_if->open(data->data_if, nullptr, 0) != ESP_CODEC_DEV_OK) {
|
||||
LOG_E(TAG, "Failed to open data interface");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// If a reset pin is wired (e.g. behind a GPIO expander), the AW88298's I2C interface
|
||||
// won't respond until it's released. We pulse it ourselves here rather than handing it
|
||||
// to aw88298_codec_new() via gpio_if/reset_pin: the vendor driver's internal
|
||||
// aw88298_reset() treats reset_pin <= 0 as "no pin wired" and no-ops (aw88298.c:197),
|
||||
// but the audio_codec_adapter_new_gpio() convention (matched by dummy_i2s_amp's
|
||||
// pa_pin, see Drivers/dummy-i2s-amp-module) is to pass 0 as the array index of a
|
||||
// single-pin GPioPinSpec array -- so index 0 collides with the vendor's own "disabled"
|
||||
// sentinel and the reset never actually fires. This runs synchronously as part of this
|
||||
// device's own start_device(), so it's correctly ordered relative to devicetree bring-up
|
||||
// (unlike a board Configuration.cpp initBoot() callback, which only runs after ALL
|
||||
// devicetree devices, including this one, have already started).
|
||||
if (config->reset_pin.gpio_controller != nullptr) {
|
||||
auto* reset_descriptor = gpio_descriptor_acquire(config->reset_pin.gpio_controller, config->reset_pin.pin, GPIO_OWNER_GPIO);
|
||||
if (reset_descriptor == nullptr) {
|
||||
LOG_E(TAG, "Failed to acquire reset pin descriptor");
|
||||
goto cleanup;
|
||||
}
|
||||
gpio_descriptor_set_flags(reset_descriptor, GPIO_FLAG_DIRECTION_OUTPUT);
|
||||
gpio_descriptor_set_level(reset_descriptor, false);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
gpio_descriptor_set_level(reset_descriptor, true);
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
gpio_descriptor_release(reset_descriptor);
|
||||
}
|
||||
|
||||
{
|
||||
aw88298_codec_cfg_t codec_config = {};
|
||||
codec_config.ctrl_if = data->ctrl_if;
|
||||
codec_config.gpio_if = nullptr;
|
||||
codec_config.reset_pin = -1;
|
||||
codec_config.hw_gain = {};
|
||||
|
||||
data->codec_if = aw88298_codec_new(&codec_config);
|
||||
if (data->codec_if == nullptr) {
|
||||
LOG_E(TAG, "Failed to create AW88298 codec interface");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
esp_codec_dev_cfg_t dev_config = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_OUT,
|
||||
.codec_if = data->codec_if,
|
||||
.data_if = data->data_if,
|
||||
};
|
||||
|
||||
data->codec_device = esp_codec_dev_new(&dev_config);
|
||||
if (data->codec_device == nullptr) {
|
||||
LOG_E(TAG, "Failed to create codec device");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
device_set_driver_data(device, data);
|
||||
return ERROR_NONE;
|
||||
|
||||
cleanup:
|
||||
// Mirrors stop_device's teardown order -- delete_*_if() routines close their interface
|
||||
// first, so we don't need separate ->close() calls here.
|
||||
if (data->codec_device != nullptr) {
|
||||
esp_codec_dev_delete(data->codec_device);
|
||||
}
|
||||
if (data->codec_if != nullptr) {
|
||||
audio_codec_delete_codec_if(data->codec_if);
|
||||
}
|
||||
if (data->data_if != nullptr) {
|
||||
audio_codec_delete_data_if(data->data_if);
|
||||
}
|
||||
if (data->ctrl_if != nullptr) {
|
||||
audio_codec_delete_ctrl_if(data->ctrl_if);
|
||||
}
|
||||
delete data;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t stop_device(Device* device) {
|
||||
auto* data = GET_DATA(device);
|
||||
if (data == nullptr) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
if (data->is_open) {
|
||||
esp_codec_dev_close(data->codec_device);
|
||||
}
|
||||
|
||||
if (data->codec_device != nullptr) {
|
||||
esp_codec_dev_delete(data->codec_device);
|
||||
}
|
||||
if (data->codec_if != nullptr) {
|
||||
audio_codec_delete_codec_if(data->codec_if);
|
||||
}
|
||||
if (data->data_if != nullptr) {
|
||||
audio_codec_delete_data_if(data->data_if);
|
||||
}
|
||||
if (data->ctrl_if != nullptr) {
|
||||
audio_codec_delete_ctrl_if(data->ctrl_if);
|
||||
}
|
||||
|
||||
device_set_driver_data(device, nullptr);
|
||||
delete data;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
Driver aw88298_driver = {
|
||||
.name = "aw88298",
|
||||
.compatible = (const char*[]) { "awinic,aw88298", nullptr },
|
||||
.start_device = start_device,
|
||||
.stop_device = stop_device,
|
||||
.api = &API,
|
||||
.device_type = &AUDIO_CODEC_TYPE,
|
||||
.owner = nullptr,
|
||||
.internal = nullptr,
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver aw88298_driver;
|
||||
|
||||
static error_t start() {
|
||||
check(driver_construct_add(&aw88298_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
check(driver_remove_destruct(&aw88298_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
extern const ModuleSymbol aw88298_module_symbols[];
|
||||
|
||||
Module aw88298_module = {
|
||||
.name = "aw88298",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = aw88298_module_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/module.h>
|
||||
|
||||
const struct ModuleSymbol aw88298_module_symbols[] = {
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
Reference in New Issue
Block a user