From 5dc933505e61e86037c915649c30cb607ec74002 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 1 Jun 2017 20:24:49 +0900 Subject: [PATCH] audio/alc5658: Add alc5658 codec driver Add alc5658 codec driver Change-Id: Ia65453c8e961f0b7c3d404418ba81419f2ceabe7 Signed-off-by: Ivan --- os/drivers/audio/Kconfig | 61 ++ os/drivers/audio/Make.defs | 4 + os/drivers/audio/alc5658.c | 1736 ++++++++++++++++++++++++++++++++++++ os/drivers/audio/alc5658.h | 315 +++++++ os/include/tinyara/audio/alc5658.h | 284 ++++++ 5 files changed, 2400 insertions(+) create mode 100644 os/drivers/audio/alc5658.c create mode 100644 os/drivers/audio/alc5658.h create mode 100644 os/include/tinyara/audio/alc5658.h diff --git a/os/drivers/audio/Kconfig b/os/drivers/audio/Kconfig index 248d935..214d8a8 100755 --- a/os/drivers/audio/Kconfig +++ b/os/drivers/audio/Kconfig @@ -64,3 +64,64 @@ config AUDIO_NULL_WORKER_STACKSIZE endif # AUDIO_NULL +config AUDIO_ALC5658 + bool "ALC5658 audio chip" + default n + depends on AUDIO + ---help--- + Select to enable support for the ALC5658 Audio codec by Wolfson + Microelectonics. This chip is a lower level audio chip.. basically + an exotic D-to-A. It includes no built-in support for audio CODECS + The ALC5658 provides: + + - Low power consumption + - High SNR + - Stereo digital microphone input + - Digital Dynamic Range Controller (compressor / limiter) + - Digital sidetone mixing + - Ground-referenced headphone driver + - Ground-referenced line outputs + + NOTE: This driver also depends on both I2C and I2S support although + that dependency is not explicit here. + +if AUDIO_ALC5658 + +config ALC5658_INITVOLUME + int "ALC5658 initial volume setting" + default 250 + +config ALC5658_INFLIGHT + int "ALC5658 maximum in-flight audio buffers" + default 2 + +config ALC5658_MSG_PRIO + int "ALC5658 message priority" + default 1 + +config ALC5658_BUFFER_SIZE + int "ALC5658 preferred buffer size" + default 8192 + +config ALC5658_NUM_BUFFERS + int "ALC5658 preferred number of buffers" + default 4 + +config ALC5658_WORKER_STACKSIZE + int "ALC5658 worker thread stack size" + default 768 + +config ALC5658_REGDUMP + bool "ALC5658 register dump" + default n + ---help--- + Enable logic to dump the contents of all ALC5658 registers. + +config ALC5658_CLKDEBUG + bool "ALC5658 clock analysis" + default n + ---help--- + Enable logic to analyze ALC5658 clock configuation. + +endif # AUDIO_ALC5658 + diff --git a/os/drivers/audio/Make.defs b/os/drivers/audio/Make.defs index 331d6d3..81cbc5a 100644 --- a/os/drivers/audio/Make.defs +++ b/os/drivers/audio/Make.defs @@ -64,6 +64,10 @@ ifeq ($(CONFIG_AUDIO_I2SCHAR),y) CSRCS += i2schar.c endif +ifeq ($(CONFIG_AUDIO_ALC5658),y) +CSRCS += alc5658.c +endif + # Include Audio driver support DEPPATH += --dep-path audio diff --git a/os/drivers/audio/alc5658.c b/os/drivers/audio/alc5658.c new file mode 100644 index 0000000..6944371 --- /dev/null +++ b/os/drivers/audio/alc5658.c @@ -0,0 +1,1736 @@ +/**************************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * 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. + * + ****************************************************************************/ +/**************************************************************************** + * drivers/audio/alc5658.c + * + * Audio device driver for Wolfson Microelectronics ALC5658 Audio codec. + * + * Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * References: + * - "ALC5658 Ultra Low Power CODEC for Portable Audio Applications, Pre- + * Production", September 2012, Rev 3.3, Wolfson Microelectronics + * + * - The framework for this driver is based on Ken Pettit's VS1053 driver. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "alc5658.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ALC5658_DEFAULT_SAMPRATE 48000 +#define ALC5658_DEFAULT_NCHANNELS 2 +#define ALC5658_DEFAULT_BPSAMP 16 + +#define enter_critical_section() 0 +#define leave_critical_section(x) + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#if !defined(CONFIG_ALC5658_REGDUMP) && !defined(CONFIG_ALC5658_CLKDEBUG) +static +#endif +uint16_t alc5658_readreg(FAR struct alc5658_dev_s *priv, uint16_t regaddr); +static void alc5658_writereg(FAR struct alc5658_dev_s *priv, uint16_t regaddr, uint16_t regval); +static void alc5658_takesem(sem_t *sem); +static uint16_t alc5658_modifyreg(FAR struct alc5658_dev_s *priv, uint16_t regaddr, uint16_t set, uint16_t clear); + +#define alc5658_givesem(s) sem_post(s) + +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME +static inline uint16_t alc5658_scalevolume(uint16_t volume, b16_t scale); +static void alc5658_setvolume(FAR struct alc5658_dev_s *priv, uint16_t volume, bool mute); +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_TONE +static void alc5658_setbass(FAR struct alc5658_dev_s *priv, uint8_t bass); +static void alc5658_settreble(FAR struct alc5658_dev_s *priv, uint8_t treble); +#endif + +static void alc5658_setdatawidth(FAR struct alc5658_dev_s *priv); +static void alc5658_setbitrate(FAR struct alc5658_dev_s *priv); + +/* Audio lower half methods (and close friends) */ + +static int alc5658_getcaps(FAR struct audio_lowerhalf_s *dev, int type, FAR struct audio_caps_s *caps); +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_configure(FAR struct audio_lowerhalf_s *dev, FAR void *session, FAR const struct audio_caps_s *caps); +#else +static int alc5658_configure(FAR struct audio_lowerhalf_s *dev, FAR const struct audio_caps_s *caps); +#endif +static int alc5658_shutdown(FAR struct audio_lowerhalf_s *dev); +static void alc5658_senddone(FAR struct i2s_dev_s *i2s, FAR struct ap_buffer_s *apb, FAR void *arg, int result); +static void alc5658_returnbuffers(FAR struct alc5658_dev_s *priv); +static int alc5658_sendbuffer(FAR struct alc5658_dev_s *priv); + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_start(FAR struct audio_lowerhalf_s *dev, FAR void *session); +#else +static int alc5658_start(FAR struct audio_lowerhalf_s *dev); +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_STOP +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session); +#else +static int alc5658_stop(FAR struct audio_lowerhalf_s *dev); +#endif +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session); +static int alc5658_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session); +#else +static int alc5658_pause(FAR struct audio_lowerhalf_s *dev); +static int alc5658_resume(FAR struct audio_lowerhalf_s *dev); +#endif +#endif +static int alc5658_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, FAR struct ap_buffer_s *apb); +static int alc5658_cancelbuffer(FAR struct audio_lowerhalf_s *dev, FAR struct ap_buffer_s *apb); +static int alc5658_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd, unsigned long arg); +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_reserve(FAR struct audio_lowerhalf_s *dev, FAR void **session); +#else +static int alc5658_reserve(FAR struct audio_lowerhalf_s *dev); +#endif +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_release(FAR struct audio_lowerhalf_s *dev, FAR void *session); +#else +static int alc5658_release(FAR struct audio_lowerhalf_s *dev); +#endif + +/* Interrupt handling an worker thread */ + +#ifdef ALC5658_USE_FFLOCK_INT +static void alc5658_interrupt_work(FAR void *arg); +static int alc5658_interrupt(FAR const struct alc5658_lower_s *lower, FAR void *arg); +#endif + +static void *alc5658_workerthread(pthread_addr_t pvarg); + +/* Initialization */ + +static void alc5658_audio_output(FAR struct alc5658_dev_s *priv); +static void alc5658_audio_input(FAR struct alc5658_dev_s *priv); +#ifdef ALC5658_USE_FFLOCK_INT +static void alc5658_configure_ints(FAR struct alc5658_dev_s *priv); +#else +#define alc5658_configure_ints(p) +#endif +static void alc5658_hw_reset(FAR struct alc5658_dev_s *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct audio_ops_s g_audioops = { + alc5658_getcaps, /* getcaps */ + alc5658_configure, /* configure */ + alc5658_shutdown, /* shutdown */ + alc5658_start, /* start */ +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + alc5658_stop, /* stop */ +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME + alc5658_pause, /* pause */ + alc5658_resume, /* resume */ +#endif + NULL, /* allocbuffer */ + NULL, /* freebuffer */ + alc5658_enqueuebuffer, /* enqueue_buffer */ + alc5658_cancelbuffer, /* cancel_buffer */ + alc5658_ioctl, /* ioctl */ + NULL, /* read */ + NULL, /* write */ + alc5658_reserve, /* reserve */ + alc5658_release /* release */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void delay(unsigned int mS) +{ + volatile systime_t start = clock_systimer(); + + mS = mS / MSEC_PER_TICK + 1; + + while (1) + if ((start + mS) < clock_systimer()) { + return; + } +} + +/**************************************************************************** + * Name: alc5658_readreg + * + * Description + * Read the specified 16-bit register from the ALC5658 device. + * + ****************************************************************************/ + +#if !defined(CONFIG_ALC5658_REGDUMP) && !defined(CONFIG_ALC5658_CLKDEBUG) +static +#endif +uint16_t alc5658_readreg(FAR struct alc5658_dev_s *priv, uint16_t regaddr) +{ + + int32_t ret; + uint8_t reg[2]; + uint16_t regval; + FAR struct i2c_dev_s *dev = priv->i2c; + FAR struct i2c_config_s *alc5658_i2c_config = &(priv->lower->i2c_config); + + reg[0] = ((char *)®addr)[1]; + reg[1] = ((char *)®addr)[0]; + + ret = i2c_write(dev, alc5658_i2c_config, reg, 2); + if (ret < 0) { + auddbg("Error, cannot read reg %x\n", regaddr); + return 0; + } + + ret = i2c_read(dev, alc5658_i2c_config, reg, 2); + if (ret < 0) { + auddbg("Error, cannot read reg %x\n", regaddr); + return 0; + } + + regval = ((uint16_t) reg[0] << 8) | (uint16_t) reg[1]; +// audvdbg("I2CRead: %04x -> %04x\n", regaddr, regval); + + return regval; +} + +/************************************************************************************ + * Name: alc5658_writereg + * + * Description: + * Write the specified 16-bit register to the ALC5658 device. + * + ************************************************************************************/ + +static void alc5658_writereg(FAR struct alc5658_dev_s *priv, uint16_t regaddr, uint16_t regval) +{ + int32_t ret; + uint8_t reg[4]; + FAR struct i2c_dev_s *dev = priv->i2c; + FAR struct i2c_config_s *alc5658_i2c_config = &(priv->lower->i2c_config); + + reg[0] = ((char *)®addr)[1]; + reg[1] = ((char *)®addr)[0]; + + reg[2] = ((char *)®val)[1]; + reg[3] = ((char *)®val)[0]; + + ret = i2c_write(dev, alc5658_i2c_config, (uint8_t *) reg, 4); + if (ret < 0) { + auddbg("Error, cannot write reg %x\n", regaddr); + return; + } +// audvdbg("I2CWrite: %04x <- %04x\n", regaddr, regval); + return; +} + +static uint16_t alc5658_modifyreg(FAR struct alc5658_dev_s *priv, uint16_t regaddr, uint16_t set, uint16_t clear) +{ + uint16_t data; +// audvdbg("I2Cmodify ------\n"); + data = alc5658_readreg(priv, regaddr); + data &= ~clear; + data |= set; + + alc5658_writereg(priv, regaddr, data); + return alc5658_readreg(priv, regaddr); +} + +static void alc5658_exec_i2c_script(FAR struct alc5658_dev_s *priv, t_codec_init_script_entry *script, uint32_t size) +{ + uint32_t i; + uint16_t ret; + + for (i = 0; i < size; i++) { + ret = alc5658_modifyreg(priv, script[i].addr, script[i].val, 0xFFFF); +// audvdbg("ALC %04x := %04x\n", script[i].addr, ret); + delay(script[i].delay); + } +} + +/************************************************************************************ + * Name: alc5658_takesem + * + * Description: + * Take a semaphore count, handling the nasty EINTR return if we are interrupted + * by a signal. + * + ************************************************************************************/ + +static void alc5658_takesem(sem_t *sem) +{ + int ret; + + do { + ret = sem_wait(sem); + DEBUGASSERT(ret == 0 || errno == EINTR); + } while (ret < 0); +} + +/************************************************************************************ + * Name: alc5658_scalevolume + * + * Description: + * Set the right and left volume values in the ALC5658 device based on the current + * volume and balance settings. + * + ************************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME +static inline uint16_t alc5658_scalevolume(uint16_t volume, b16_t scale) +{ + return b16toi((b16_t) volume * scale); +} +#endif + +/************************************************************************************ + * Name: alc5658_setvolume + * + * Description: + * Set the right and left volume values in the ALC5658 device based on the current + * volume and balance settings. + * + ************************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME +static void alc5658_setvolume(FAR struct alc5658_dev_s *priv, uint16_t volume, bool mute) +{ + + audvdbg("volume=%u mute=%u\n", volume, mute); + + /* ADD VOLUME CODE HERE */ + + /* Remember the volume level and mute settings */ + + priv->volume = volume; + priv->mute = mute; +} +#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */ + +/************************************************************************************ + * Name: alc5658_setbass + * + * Description: + * Set the bass level. + * + * The level and range are in whole percentage levels (0-100). + * + ************************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_TONE +static void alc5658_setbass(FAR struct alc5658_dev_s *priv, uint8_t bass) +{ + audvdbg("bass=%u\n", bass); +} +#endif /* CONFIG_AUDIO_EXCLUDE_TONE */ + +/************************************************************************************ + * Name: alc5658_settreble + * + * Description: + * Set the treble level . + * + * The level and range are in whole percentage levels (0-100). + * + ************************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_TONE +static void alc5658_settreble(FAR struct alc5658_dev_s *priv, uint8_t treble) +{ + audvdbg("treble=%u\n", treble); +} +#endif /* CONFIG_AUDIO_EXCLUDE_TONE */ + +/**************************************************************************** + * Name: alc5658_setdatawidth + * + * Description: + * Set the 8- 16- 24- bit data modes + * + ****************************************************************************/ + +static void alc5658_setdatawidth(FAR struct alc5658_dev_s *priv) +{ +} + +/**************************************************************************** + * Name: alc5658_setbitrate + * + * Description: + * + ****************************************************************************/ + +static void alc5658_setbitrate(FAR struct alc5658_dev_s *priv) +{ + +} + +/**************************************************************************** + * Name: alc5658_getcaps + * + * Description: + * Get the audio device capabilities + * + ****************************************************************************/ + +static int alc5658_getcaps(FAR struct audio_lowerhalf_s *dev, int type, FAR struct audio_caps_s *caps) +{ + /* Validate the structure */ + + DEBUGASSERT(caps && caps->ac_len >= sizeof(struct audio_caps_s)); + audvdbg("type=%d ac_type=%d\n", type, caps->ac_type); + + /* Fill in the caller's structure based on requested info */ + + caps->ac_format.hw = 0; + caps->ac_controls.w = 0; + + switch (caps->ac_type) { + /* Caller is querying for the types of units we support */ + + case AUDIO_TYPE_QUERY: + + /* Provide our overall capabilities. The interfacing software + * must then call us back for specific info for each capability. + */ + + caps->ac_channels = 2; /* Stereo output */ + + switch (caps->ac_subtype) { + case AUDIO_TYPE_QUERY: + /* We don't decode any formats! Only something above us in + * the audio stream can perform decoding on our behalf. + */ + + /* The types of audio units we implement */ + + caps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_FEATURE | AUDIO_TYPE_PROCESSING; + + break; + + case AUDIO_FMT_MIDI: + /* We only support Format 0 */ + + caps->ac_controls.b[0] = AUDIO_SUBFMT_END; + break; + + default: + caps->ac_controls.b[0] = AUDIO_SUBFMT_END; + break; + } + + break; + + /* Provide capabilities of our OUTPUT unit */ + + case AUDIO_TYPE_OUTPUT: + + caps->ac_channels = 2; + + switch (caps->ac_subtype) { + case AUDIO_TYPE_QUERY: + + /* Report the Sample rates we support */ + + caps->ac_controls.b[0] = AUDIO_SAMP_RATE_8K | AUDIO_SAMP_RATE_11K | AUDIO_SAMP_RATE_16K | AUDIO_SAMP_RATE_22K | AUDIO_SAMP_RATE_32K | AUDIO_SAMP_RATE_44K | AUDIO_SAMP_RATE_48K; + break; + + case AUDIO_FMT_MP3: + case AUDIO_FMT_WMA: + case AUDIO_FMT_PCM: + break; + + default: + break; + } + + break; + + /* Provide capabilities of our FEATURE units */ + + case AUDIO_TYPE_FEATURE: + + /* If the sub-type is UNDEF, then report the Feature Units we support */ + + if (caps->ac_subtype == AUDIO_FU_UNDEF) { + /* Fill in the ac_controls section with the Feature Units we have */ + + caps->ac_controls.b[0] = AUDIO_FU_VOLUME | AUDIO_FU_BASS | AUDIO_FU_TREBLE; + caps->ac_controls.b[1] = AUDIO_FU_BALANCE >> 8; + } else { + /* TODO: Do we need to provide specific info for the Feature Units, + * such as volume setting ranges, etc.? + */ + } + + break; + + /* Provide capabilities of our PROCESSING unit */ + + case AUDIO_TYPE_PROCESSING: + + switch (caps->ac_subtype) { + case AUDIO_PU_UNDEF: + + /* Provide the type of Processing Units we support */ + + caps->ac_controls.b[0] = AUDIO_PU_STEREO_EXTENDER; + break; + + case AUDIO_PU_STEREO_EXTENDER: + + /* Provide capabilities of our Stereo Extender */ + + caps->ac_controls.b[0] = AUDIO_STEXT_ENABLE | AUDIO_STEXT_WIDTH; + break; + + default: + + /* Other types of processing uint we don't support */ + + break; + } + + break; + + /* All others we don't support */ + + default: + + /* Zero out the fields to indicate no support */ + + caps->ac_subtype = 0; + caps->ac_channels = 0; + + break; + } + + /* Return the length of the audio_caps_s struct for validation of + * proper Audio device type. + */ + + return caps->ac_len; +} + +/**************************************************************************** + * Name: alc5658_configure + * + * Description: + * Configure the audio device for the specified mode of operation. + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_configure(FAR struct audio_lowerhalf_s *dev, FAR void *session, FAR const struct audio_caps_s *caps) +#else +static int alc5658_configure(FAR struct audio_lowerhalf_s *dev, FAR const struct audio_caps_s *caps) +#endif +{ +#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) || !defined(CONFIG_AUDIO_EXCLUDE_TONE) + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; +#endif + int ret = OK; + + DEBUGASSERT(priv && caps); + audvdbg("ac_type: %d\n", caps->ac_type); + + /* Process the configure operation */ + + switch (caps->ac_type) { + case AUDIO_TYPE_FEATURE: + audvdbg(" AUDIO_TYPE_FEATURE\n"); + + /* Process based on Feature Unit */ + + switch (caps->ac_format.hw) { +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME + case AUDIO_FU_VOLUME: { + /* Set the volume */ + + uint16_t volume = caps->ac_controls.hw[0]; + audvdbg(" Volume: %d\n", volume); + + if (volume >= 0 && volume <= 1000) { + /* Scale the volume setting to the range {0.. 63} */ + + alc5658_setvolume(priv, (63 * volume / 1000), priv->mute); + } else { + ret = -EDOM; + } + } + break; +#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */ + +#ifndef CONFIG_AUDIO_EXCLUDE_TONE + case AUDIO_FU_BASS: { + /* Set the bass. The percentage level (0-100) is in the + * ac_controls.b[0] parameter. + */ + + uint8_t bass = caps->ac_controls.b[0]; + audvdbg(" Bass: %d\n", bass); + + if (bass <= 100) { + alc5658_setbass(priv, bass); + } else { + ret = -EDOM; + } + } + break; + + case AUDIO_FU_TREBLE: { + /* Set the treble. The percentage level (0-100) is in the + * ac_controls.b[0] parameter. + */ + + uint8_t treble = caps->ac_controls.b[0]; + audvdbg(" Treble: %d\n", treble); + + if (treble <= 100) { + alc5658_settreble(priv, treble); + } else { + ret = -EDOM; + } + } + break; +#endif /* CONFIG_AUDIO_EXCLUDE_TONE */ + + default: + auddbg(" ERROR: Unrecognized feature unit\n"); + ret = -ENOTTY; + break; + } + break; + + case AUDIO_TYPE_OUTPUT: { + audvdbg(" AUDIO_TYPE_OUTPUT:\n"); + audvdbg(" Number of channels: %u\n", caps->ac_channels); + audvdbg(" Sample rate: %u\n", caps->ac_controls.hw[0]); + audvdbg(" Sample width: %u\n", caps->ac_controls.b[2]); + + /* Verify that all of the requested values are supported */ + + ret = -ERANGE; + if (caps->ac_channels != 1 && caps->ac_channels != 2) { + auddbg("ERROR: Unsupported number of channels: %d\n", caps->ac_channels); + break; + } + + if (caps->ac_controls.b[2] != 8 && caps->ac_controls.b[2] != 16) { + auddbg("ERROR: Unsupported bits per sample: %d\n", caps->ac_controls.b[2]); + break; + } + + /* Save the current stream configuration */ + + priv->samprate = caps->ac_controls.hw[0]; + priv->nchannels = caps->ac_channels; + priv->bpsamp = caps->ac_controls.b[2]; + + /* Reconfigure the FLL to support the resulting number or channels, + * bits per sample, and bitrate. + */ + + alc5658_setdatawidth(priv); + alc5658_setbitrate(priv); + + alc5658_clock_analysis(&priv->dev, "AUDIO_TYPE_OUTPUT"); + ret = OK; + } + break; + + case AUDIO_TYPE_PROCESSING: + break; + } + + return ret; +} + +/**************************************************************************** + * Name: alc5658_shutdown + * + * Description: + * Shutdown the ALC5658 chip and put it in the lowest power state possible. + * + ****************************************************************************/ + +static int alc5658_shutdown(FAR struct audio_lowerhalf_s *dev) +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + + DEBUGASSERT(priv); + + /* First disable interrupts */ + + ALC5658_DISABLE(priv->lower); + + /* Now issue a software reset. This puts all ALC5658 registers back in + * their default state. + */ + + alc5658_hw_reset(priv); + return OK; +} + +/**************************************************************************** + * Name: alc5658_senddone + * + * Description: + * This is the I2S callback function that is invoked when the transfer + * completes. + * + ****************************************************************************/ + +static void alc5658_senddone(FAR struct i2s_dev_s *i2s, FAR struct ap_buffer_s *apb, FAR void *arg, int result) +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)arg; + struct audio_msg_s msg; + irqstate_t flags; + int ret; + + DEBUGASSERT(i2s && priv && priv->running && apb); + audvdbg("apb=%p inflight=%d result=%d\n", apb, priv->inflight, result); + + /* We do not place any restriction on the context in which this function + * is called. It may be called from an interrupt handler. Therefore, the + * doneq and in-flight values might be accessed from the interrupt level. + * Not the best design. But we will use interrupt controls to protect + * against that possibility. + */ + + flags = enter_critical_section(); + + /* Add the completed buffer to the end of our doneq. We do not yet + * decrement the reference count. + */ + + dq_addlast((FAR dq_entry_t *) apb, &priv->doneq); + + /* And decrement the number of buffers in-flight */ + + DEBUGASSERT(priv->inflight > 0); + priv->inflight--; + + /* Save the result of the transfer */ + /* REVISIT: This can be overwritten */ + + priv->result = result; + leave_critical_section(flags); + + /* Now send a message to the worker thread, informing it that there are + * buffers in the done queue that need to be cleaned up. + */ + + msg.msgId = AUDIO_MSG_COMPLETE; + ret = mq_send(priv->mq, (FAR const char *)&msg, sizeof(msg), CONFIG_ALC5658_MSG_PRIO); + if (ret < 0) { + auddbg("ERROR: mq_send failed: %d\n", errno); + } +} + +/**************************************************************************** + * Name: alc5658_returnbuffers + * + * Description: + * This function is called after the complete of one or more data + * transfers. This function will empty the done queue and release our + * reference to each buffer. + * + ****************************************************************************/ + +static void alc5658_returnbuffers(FAR struct alc5658_dev_s *priv) +{ + FAR struct ap_buffer_s *apb; + irqstate_t flags; + + /* The doneq and in-flight values might be accessed from the interrupt + * level in some implementations. Not the best design. But we will + * use interrupt controls to protect against that possibility. + */ + + flags = enter_critical_section(); + while (dq_peek(&priv->doneq) != NULL) { + /* Take the next buffer from the queue of completed transfers */ + + apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->doneq); + leave_critical_section(flags); + + audvdbg("Returning: apb=%p curbyte=%d nbytes=%d flags=%04x\n", apb, apb->curbyte, apb->nbytes, apb->flags); + + /* Are we returning the final buffer in the stream? */ + + if ((apb->flags & AUDIO_APB_FINAL) != 0) { + /* Both the pending and the done queues should be empty and there + * should be no buffers in-flight. + */ + + DEBUGASSERT(dq_empty(&priv->doneq) && dq_empty(&priv->pendq) && priv->inflight == 0); + + /* Set the terminating flag. This will, eventually, cause the + * worker thread to exit (if it is not already terminating). + */ + + audvdbg("Terminating\n"); + priv->terminating = true; + } + + /* Release our reference to the audio buffer */ + + apb_free(apb); + + /* Send the buffer back up to the previous level. */ + +#ifdef CONFIG_AUDIO_MULTI_SESSION + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL); +#else + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK); +#endif + flags = enter_critical_section(); + } + + leave_critical_section(flags); +} + +/**************************************************************************** + * Name: alc5658_sendbuffer + * + * Description: + * Start the transfer an audio buffer to the ALC5658 via I2S. This + * will not wait for the transfer to complete but will return immediately. + * the alc5658 called will be invoked when the transfer + * completes, stimulating the worker thread to call this function again. + * + ****************************************************************************/ + +static int alc5658_sendbuffer(FAR struct alc5658_dev_s *priv) +{ + FAR struct ap_buffer_s *apb; + irqstate_t flags; + uint32_t timeout; + int shift; + int ret = OK; + + /* Loop while there are audio buffers to be sent and we have few than + * CONFIG_ALC5658_INFLIGHT then "in-flight" + * + * The 'inflight' value might be modified from the interrupt level in some + * implementations. We will use interrupt controls to protect against + * that possibility. + * + * The 'pendq', on the other hand, is protected via a semaphore. Let's + * hold the semaphore while we are busy here and disable the interrupts + * only while accessing 'inflight'. + */ + + alc5658_takesem(&priv->pendsem); + while (priv->inflight < CONFIG_ALC5658_INFLIGHT && dq_peek(&priv->pendq) != NULL && !priv->paused) { + /* Take next buffer from the queue of pending transfers */ + + apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq); + audvdbg("Sending apb=%p, size=%d inflight=%d\n", apb, apb->nbytes, priv->inflight); + + /* Increment the number of buffers in-flight before sending in order + * to avoid a possible race condition. + */ + + flags = enter_critical_section(); + priv->inflight++; + leave_critical_section(flags); + + /* Send the entire audio buffer via I2S. What is a reasonable timeout + * to use? This would depend on the bit rate and size of the buffer. + * + * Samples in the buffer (samples): + * = buffer_size * 8 / bpsamp samples + * Sample rate (samples/second): + * = samplerate * nchannels + * Expected transfer time (seconds): + * = (buffer_size * 8) / bpsamp / samplerate / nchannels + * + * We will set the timeout about twice that. + * + * NOTES: + * - The multiplier of 8 becomes 16000 for 2x and units of + * milliseconds. + * - 16000 is a approximately 16384 (1 << 14), bpsamp is either + * (1 << 3) or (1 << 4), and nchannels is either (1 << 0) or + * (1 << 1). So this can be simplifies to (milliseconds): + * + * = (buffer_size << shift) / samplerate + */ + + shift = (priv->bpsamp == 8) ? 14 - 3 : 14 - 4; + shift -= (priv->nchannels > 1) ? 1 : 0; + + timeout = MSEC2TICK(((uint32_t)(apb->nbytes - apb->curbyte) << shift) / (uint32_t) priv->samprate); + + ret = I2S_SEND(priv->i2s, apb, alc5658_senddone, priv, timeout); + if (ret < 0) { + auddbg("ERROR: I2S_SEND failed: %d\n", ret); + break; + } + } + + alc5658_givesem(&priv->pendsem); + return ret; +} + +/**************************************************************************** + * Name: alc5658_start + * + * Description: + * Start the configured operation (audio streaming, volume enabled, etc.). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_start(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int alc5658_start(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + struct sched_param sparam; + struct mq_attr attr; + pthread_attr_t tattr; + FAR void *value; + int ret; + + audvdbg("Entry\n"); + + /* Exit reduced power modes of operation */ + /* REVISIT */ + + /* Create a message queue for the worker thread */ + + snprintf(priv->mqname, sizeof(priv->mqname), "/tmp/%X", priv); + + attr.mq_maxmsg = 16; + attr.mq_msgsize = sizeof(struct audio_msg_s); + attr.mq_curmsgs = 0; + attr.mq_flags = 0; + + priv->mq = mq_open(priv->mqname, O_RDWR | O_CREAT, 0644, &attr); + if (priv->mq == NULL) { + /* Error creating message queue! */ + + auddbg("ERROR: Couldn't allocate message queue\n"); + return -ENOMEM; + } + + /* Join any old worker thread we had created to prevent a memory leak */ + + if (priv->threadid != 0) { + audvdbg("Joining old thread\n"); + pthread_join(priv->threadid, &value); + } + + /* Start our thread for sending data to the device */ + + pthread_attr_init(&tattr); + sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 3; + (void)pthread_attr_setschedparam(&tattr, &sparam); + (void)pthread_attr_setstacksize(&tattr, CONFIG_ALC5658_WORKER_STACKSIZE); + + audvdbg("Starting worker thread\n"); + ret = pthread_create(&priv->threadid, &tattr, alc5658_workerthread, (pthread_addr_t) priv); + if (ret != OK) { + auddbg("ERROR: pthread_create failed: %d\n", ret); + } else { + pthread_setname_np(priv->threadid, "alc5658"); + audvdbg("Created worker thread\n"); + } + + return ret; +} + +/**************************************************************************** + * Name: alc5658_stop + * + * Description: Stop the configured operation (audio streaming, volume + * disabled, etc.). + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int alc5658_stop(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + struct audio_msg_s term_msg; + FAR void *value; + + /* Send a message to stop all audio streaming */ + + term_msg.msgId = AUDIO_MSG_STOP; + term_msg.u.data = 0; + mq_send(priv->mq, (FAR const char *)&term_msg, sizeof(term_msg), CONFIG_ALC5658_MSG_PRIO); + + /* Join the worker thread */ + + pthread_join(priv->threadid, &value); + priv->threadid = 0; + + /* Enter into a reduced power usage mode */ + /* REVISIT: */ + + return OK; +} +#endif + +/**************************************************************************** + * Name: alc5658_pause + * + * Description: Pauses the playback. + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int alc5658_pause(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + + if (priv->running && !priv->paused) { + /* Disable interrupts to prevent us from suppling any more data */ + + priv->paused = true; + alc5658_setvolume(priv, priv->volume, true); + ALC5658_DISABLE(priv->lower); + } + + return OK; +} +#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */ + +/**************************************************************************** + * Name: alc5658_resume + * + * Description: Resumes the playback. + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int alc5658_resume(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + + if (priv->running && priv->paused) { + priv->paused = false; + alc5658_setvolume(priv, priv->volume, false); + + /* Enable interrupts to allow sampling data */ + + alc5658_sendbuffer(priv); +#ifdef ALC5658_USE_FFLOCK_INT + ALC5658_ENABLE(priv->lower); +#endif + } + + return OK; +} +#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */ + +/**************************************************************************** + * Name: alc5658_enqueuebuffer + * + * Description: Enqueue an Audio Pipeline Buffer for playback/ processing. + * + ****************************************************************************/ + +static int alc5658_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, FAR struct ap_buffer_s *apb) +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + struct audio_msg_s term_msg; + int ret; + + audvdbg("Enqueueing: apb=%p curbyte=%d nbytes=%d flags=%04x\n", apb, apb->curbyte, apb->nbytes, apb->flags); + + /* Take a reference on the new audio buffer */ + + apb_reference(apb); + + /* Add the new buffer to the tail of pending audio buffers */ + + alc5658_takesem(&priv->pendsem); + apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; + dq_addlast(&apb->dq_entry, &priv->pendq); + alc5658_givesem(&priv->pendsem); + + /* Send a message to the worker thread indicating that a new buffer has been + * enqueued. If mq is NULL, then the playing has not yet started. In that + * case we are just "priming the pump" and we don't need to send any message. + */ + + ret = OK; + if (priv->mq != NULL) { + term_msg.msgId = AUDIO_MSG_ENQUEUE; + term_msg.u.data = 0; + + ret = mq_send(priv->mq, (FAR const char *)&term_msg, sizeof(term_msg), CONFIG_ALC5658_MSG_PRIO); + if (ret < 0) { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + auddbg("ERROR: mq_send failed: %d\n", errcode); + UNUSED(errcode); + } + } + + return ret; +} + +/**************************************************************************** + * Name: alc5658_cancelbuffer + * + * Description: Called when an enqueued buffer is being cancelled. + * + ****************************************************************************/ + +static int alc5658_cancelbuffer(FAR struct audio_lowerhalf_s *dev, FAR struct ap_buffer_s *apb) +{ + audvdbg("apb=%p\n", apb); + return OK; +} + +/**************************************************************************** + * Name: alc5658_ioctl + * + * Description: Perform a device ioctl + * + ****************************************************************************/ + +static int alc5658_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd, unsigned long arg) +{ +#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS + FAR struct ap_buffer_info_s *bufinfo; +#endif + + /* Deal with ioctls passed from the upper-half driver */ + + switch (cmd) { + /* Check for AUDIOIOC_HWRESET ioctl. This ioctl is passed straight + * through from the upper-half audio driver. + */ + + case AUDIOIOC_HWRESET: { + /* REVISIT: Should we completely re-initialize the chip? We + * can't just issue a software reset; that would puts all ALC5658 + * registers back in their default state. + */ + + audvdbg("AUDIOIOC_HWRESET:\n"); + } + break; + + /* Report our preferred buffer size and quantity */ + +#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS + case AUDIOIOC_GETBUFFERINFO: { + audvdbg("AUDIOIOC_GETBUFFERINFO:\n"); + bufinfo = (FAR struct ap_buffer_info_s *)arg; + bufinfo->buffer_size = CONFIG_ALC5658_BUFFER_SIZE; + bufinfo->nbuffers = CONFIG_ALC5658_NUM_BUFFERS; + } + break; +#endif + + default: + audvdbg("Ignored\n"); + break; + } + + return OK; +} + +/**************************************************************************** + * Name: alc5658_reserve + * + * Description: Reserves a session (the only one we have). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_reserve(FAR struct audio_lowerhalf_s *dev, FAR void **session) +#else +static int alc5658_reserve(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + int ret = OK; + + /* Borrow the APBQ semaphore for thread sync */ + + alc5658_takesem(&priv->pendsem); + if (priv->reserved) { + ret = -EBUSY; + } else { + /* Initialize the session context */ + +#ifdef CONFIG_AUDIO_MULTI_SESSION + *session = NULL; +#endif + priv->inflight = 0; + priv->running = false; + priv->paused = false; +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + priv->terminating = false; +#endif + priv->reserved = true; + } + + alc5658_givesem(&priv->pendsem); + + return ret; +} + +/**************************************************************************** + * Name: alc5658_release + * + * Description: Releases the session (the only one we have). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int alc5658_release(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int alc5658_release(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)dev; + void *value; + + /* Join any old worker thread we had created to prevent a memory leak */ + + if (priv->threadid != 0) { + pthread_join(priv->threadid, &value); + priv->threadid = 0; + } + + /* Borrow the APBQ semaphore for thread sync */ + + alc5658_takesem(&priv->pendsem); + + /* Really we should free any queued buffers here */ + + priv->reserved = false; + alc5658_givesem(&priv->pendsem); + + return OK; +} + +/**************************************************************************** + * Name: alc5658_interrupt_work + * + * Description: + * ALC5658 interrupt actions cannot be performed in the interrupt handler + * because I2C access is not possible in that context. Instead, all I2C + * operations are deferred to the work queue. + * + * Assumptions: + * ALC5658 interrupts were disabled in the interrupt handler. + * + ****************************************************************************/ + +#ifdef ALC5658_USE_FFLOCK_INT +static void alc5658_interrupt_work(FAR void *arg) +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)arg; + uint16_t regval; + + DEBUGASSERT(priv && priv->lower); + + /* Sample the interrupt status */ + + regval = alc5658_readreg(priv, ALC5658_INT_STATUS); + audvdbg("INT_STATUS: %04x\n", regval); + + /* Check for the FLL lock interrupt. We are sloppy here since at + * present, only the FLL lock interrupt is used. + */ + + DEBUGASSERT((regval & ALC5658_FLL_LOCK_INT) != 0 && !priv->locked); + UNUSED(regval); + + priv->locked = true; + + /* Clear all pending interrupts by write 1's to the interrupt status + * register. + * + * REVISIT: Since I2C is slow and not atomic with respect to ALC5658 event, + * could this not cause the lost of interrupts? + */ + + alc5658_writereg(priv, ALC5658_INT_STATUS, ALC5658_ALL_INTS); + + /* Disable further FLL lock interrupts. We are sloppy here since at + * present, only the FLL lock interrupt is used. + */ + + alc5658_writereg(priv, ALC5658_INT_MASK, ALC5658_ALL_INTS); + +#ifdef ALC5658_USE_FFLOCK_INT + /* Re-enable ALC5658 interrupts */ + + ALC5658_ENABLE(priv->lower); +#endif +} +#endif + +/**************************************************************************** + * Name: alc5658_interrupt + * + * Description: + * This is the ISR that services the GPIO1/IRQ pin from the ALC5658. It + * signals ALC5658 events such FLL lock. + * + ****************************************************************************/ + +#ifdef ALC5658_USE_FFLOCK_INT +static int alc5658_interrupt(FAR const struct alc5658_lower_s *lower, FAR void *arg) +{ + FAR struct alc5658_dev_s *priv = (FAR struct alc5658_dev_s *)arg; + int ret; + + DEBUGASSERT(lower && priv); + + /* Disable further interrupts and perform all interrupt related activities + * on the work thread. There is nothing that we can do from the interrupt + * handler because we cannot perform I2C operations here. + */ + + ALC5658_DISABLE(priv->lower); + + DEBUGASSERT(work_available(&priv->work)); + ret = work_queue(LPWORK, &priv->work, alc5658_interrupt_work, priv, 0); + if (ret < 0) { + auddbg("ERROR: Failed to schedule work\n"); + } + + return OK; +} +#endif + +/**************************************************************************** + * Name: alc5658_workerthread + * + * This is the thread that feeds data to the chip and keeps the audio + * stream going. + * + ****************************************************************************/ + +static void *alc5658_workerthread(pthread_addr_t pvarg) +{ + FAR struct alc5658_dev_s *priv = (struct alc5658_dev_s *)pvarg; + struct audio_msg_s msg; + FAR struct ap_buffer_s *apb; + int msglen; + int prio; + + audvdbg("Entry\n"); + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + priv->terminating = false; +#endif + + /* Mark ourself as running and make sure that ALC5658 interrupts are + * enabled. + */ + + priv->running = true; +#ifdef ALC5658_USE_FFLOCK_INT + ALC5658_ENABLE(priv->lower); +#endif + alc5658_setvolume(priv, priv->volume, false); + + /* Loop as long as we are supposed to be running and as long as we have + * buffers in-flight. + */ + + while (priv->running || priv->inflight > 0) { + /* Check if we have been asked to terminate. We have to check if we + * still have buffers in-flight. If we do, then we can't stop until + * birds come back to roost. + */ + + if (priv->terminating && priv->inflight <= 0) { + /* We are IDLE. Break out of the loop and exit. */ + + break; + } else { + /* Check if we can send more audio buffers to the ALC5658 */ + + alc5658_sendbuffer(priv); + } + + /* Wait for messages from our message queue */ + + msglen = mq_receive(priv->mq, (FAR char *)&msg, sizeof(msg), &prio); + + /* Handle the case when we return with no message */ + + if (msglen < sizeof(struct audio_msg_s)) { + auddbg("ERROR: Message too small: %d\n", msglen); + continue; + } + + /* Process the message */ + + switch (msg.msgId) { + /* The ISR has requested more data. We will catch this case at + * the top of the loop. + */ + + case AUDIO_MSG_DATA_REQUEST: + audvdbg("AUDIO_MSG_DATA_REQUEST\n"); + break; + + /* Stop the playback */ + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + case AUDIO_MSG_STOP: + /* Indicate that we are terminating */ + + audvdbg("AUDIO_MSG_STOP: Terminating\n"); + priv->terminating = true; + break; +#endif + + /* We have a new buffer to send. We will catch this case at + * the top of the loop. + */ + + case AUDIO_MSG_ENQUEUE: + audvdbg("AUDIO_MSG_ENQUEUE\n"); + break; + + /* We will wake up from the I2S callback with this message */ + + case AUDIO_MSG_COMPLETE: + audvdbg("AUDIO_MSG_COMPLETE\n"); + alc5658_returnbuffers(priv); + break; + + default: + auddbg("ERROR: Ignoring message ID %d\n", msg.msgId); + break; + } + } + + /* Reset the ALC5658 hardware */ + + alc5658_hw_reset(priv); + + /* Return any pending buffers in our pending queue */ + + alc5658_takesem(&priv->pendsem); + while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { + /* Release our reference to the buffer */ + + apb_free(apb); + + /* Send the buffer back up to the previous level. */ + +#ifdef CONFIG_AUDIO_MULTI_SESSION + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL); +#else + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK); +#endif + } + + alc5658_givesem(&priv->pendsem); + + /* Return any pending buffers in our done queue */ + + alc5658_returnbuffers(priv); + + /* Close the message queue */ + + mq_close(priv->mq); + mq_unlink(priv->mqname); + priv->mq = NULL; + + /* Send an AUDIO_MSG_COMPLETE message to the client */ + +#ifdef CONFIG_AUDIO_MULTI_SESSION + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK, NULL); +#else + priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK); +#endif + + audvdbg("Exit\n"); + return NULL; +} + +/**************************************************************************** + * Name: alc5658_audio_output + * + * Description: + * Initialize and configure the ALC5658 device as an audio output device. + * + * Input Parameters: + * priv - A reference to the driver state structure + * + * Returned Value: + * None. No failures are detected. + * + ****************************************************************************/ + +static void alc5658_audio_output(FAR struct alc5658_dev_s *priv) +{ + alc5658_exec_i2c_script(priv, codec_init_script, sizeof(codec_init_script) / sizeof(t_codec_init_script_entry)); +} + +/**************************************************************************** + * Name: alc5658_audio_input + * + * Description: + * Initialize and configure the ALC5658 device as an audio input device + * + * Input Parameters: + * priv - A reference to the driver state structure + * + * Returned Value: + * None. No failures are detected. + * + ****************************************************************************/ + +static void alc5658_audio_input(FAR struct alc5658_dev_s *priv) +{ +} + +/**************************************************************************** + * Name: alc5658_configure_ints + * + * Description: + * Configure the GPIO/IRQ interrupt + * + * Input Parameters: + * priv - A reference to the driver state structure + * + * Returned Value: + * None + * + ****************************************************************************/ + +#ifdef ALC5658_USE_FFLOCK_INT +static void alc5658_configure_ints(FAR struct alc5658_dev_s *priv) +{ +} +#endif + +/**************************************************************************** + * Name: alc5658_hw_reset + * + * Description: + * Reset and re-initialize the ALC5658 + * + * Input Parameters: + * priv - A reference to the driver state structure + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void alc5658_hw_reset(FAR struct alc5658_dev_s *priv) +{ + + /* Put audio output back to its initial configuration */ + + priv->samprate = ALC5658_DEFAULT_SAMPRATE; + priv->nchannels = ALC5658_DEFAULT_NCHANNELS; + priv->bpsamp = ALC5658_DEFAULT_BPSAMP; +#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) && !defined(CONFIG_AUDIO_EXCLUDE_BALANCE) + priv->balance = b16HALF; /* Center balance */ +#endif + + /* Software reset. This puts all ALC5658 registers back in their + * default state. + */ + + alc5658_exec_i2c_script(priv, codec_reset_script, sizeof(codec_reset_script) / sizeof(t_codec_init_script_entry)); + + alc5658_audio_output(priv); + + /* Configure interrupts */ + + /* Configure the FLL and the LRCLK */ + + alc5658_setbitrate(priv); + + /* Dump some information and return the device instance */ + + alc5658_dump_registers(&priv->dev, "After configuration"); + alc5658_clock_analysis(&priv->dev, "After configuration"); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: alc5658_initialize + * + * Description: + * Initialize the ALC5658 device. + * + * Input Parameters: + * i2c - An I2C driver instance + * i2s - An I2S driver instance + * lower - Persistent board configuration data + * + * Returned Value: + * A new lower half audio interface for the ALC5658 device is returned on + * success; NULL is returned on failure. + * + ****************************************************************************/ + +FAR struct audio_lowerhalf_s *alc5658_initialize(FAR struct i2c_dev_s *i2c, FAR struct i2s_dev_s *i2s, FAR struct alc5658_lower_s *lower) +{ + + FAR struct alc5658_dev_s *priv; + uint16_t regval; + /* Sanity check */ + DEBUGASSERT(i2c && i2s && lower); + + /* Allocate a ALC5658 device structure */ + priv = (FAR struct alc5658_dev_s *)kmm_zalloc(sizeof(struct alc5658_dev_s)); + if (priv) { + /* Initialize the ALC5658 device structure. Since we used kmm_zalloc, + * only the non-zero elements of the structure need to be initialized. + */ + + priv->dev.ops = &g_audioops; + priv->lower = lower; + priv->i2c = i2c; + priv->i2s = i2s; + + sem_init(&priv->pendsem, 0, 1); + dq_init(&priv->pendq); + dq_init(&priv->doneq); + + /* Software reset. This puts all ALC5658 registers back in their + * default state. + */ + + alc5658_writereg(priv, ACL5658_RESET, 0); + alc5658_dump_registers(&priv->dev, "After reset"); + + /* Verify that ALC5658 is present and available on this I2C */ + + regval = alc5658_readreg(priv, ACL5658_RESET); + if (regval != 0) { + auddbg("ERROR: ALC5658 not found: ID=%04x\n", regval); + goto errout_with_dev; + } + + /* Reset and reconfigure the ALC5658 hardwaqre */ + + alc5658_hw_reset(priv); + return &priv->dev; + } + + return NULL; + +errout_with_dev: + sem_destroy(&priv->pendsem); + kmm_free(priv); + return NULL; +} diff --git a/os/drivers/audio/alc5658.h b/os/drivers/audio/alc5658.h new file mode 100644 index 0000000..5bad99e --- /dev/null +++ b/os/drivers/audio/alc5658.h @@ -0,0 +1,315 @@ +/**************************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * 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. + * + ****************************************************************************/ +/**************************************************************************** + * drivers/audio/alc5658.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __DRIVERS_AUDIO_ALC5658_H +#define __DRIVERS_AUDIO_ALC5658_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +#include +#include + +#ifdef CONFIG_AUDIO + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Registers Addresses ******************************************************/ + +typedef enum { + ACL5658_RESET = 0x0000, + ACL5658_SPO_VOL = 0x0001, + ACL5658_HP_VOL = 0x0002, + ACL5658_LOUT_CTRL1 = 0x0003, + ACL5658_LOUT_CTRL2 = 0x0004, + ACL5658_HPOUT_VLML = 0x0005, + ACL5658_HPOUT_VLMR = 0x0006, + ACL5658_SPDIF_CTRL1 = 0x0008, + ACL5658_SPDIF_CTRL2 = 0x0009, + ACL5658_SPDIF_CTRL3 = 0x0036, + ACL5658_IN1_CTRL = 0x000C, + ACL5658_INL_VLM = 0x000F, + + ACL5658_SIDETONE = 0x0018, + + /* DIGITAL Volume*/ + ACL5658_DAC_L1R1_VLM = 0x0019, + ACL5658_DAC_L2R2_VLM = 0x001A, + ACL5658_DAC_L2R2_MUTE = 0x001B, + + /* DIGITAL Mixers*/ + ACL5658_ADC_2_DAC_MXR = 0x0029, + ACL5658_DAC_STR_MXR = 0x002A, + ACL5658_DAC_MN_MXR = 0x002B, + ACL5658_DAC_LB_SDTONE = 0x002C, + ACL5658_COPY_MODE = 0x002F, + + /* Analog DAC Source */ + ACL5658_DAC_SRC = 0x002D, + + /* ANALOG Mixers */ + ACL5658_SPKMIXL = 0x0046, + ACL5658_SPKMIXR = 0x0047, + ACL5658_SPOMIX = 0x0048, + ACL5658_OUTMIXL1 = 0x004D, + ACL5658_OUTMIXL2 = 0x004E, + ACL5658_OUTMIXR1 = 0x004F, + ACL5658_OUTMIXR2 = 0x0050, + ACL5658_LOUTMIX = 0x0052, + + /* Power management */ + ACL5658_PWR_MNG1 = 0x0061, + ACL5658_PWR_MNG2 = 0x0062, + ACL5658_PWR_MNG3 = 0x0063, + ACL5658_PWR_MNG4 = 0x0064, + ACL5658_PWR_MNG5 = 0x0065, + ACL5658_PWR_MNG6 = 0x0066, + ACL5658_PWR_MNG7 = 0x0067, + + /* DIGITAL ports comtrol */ + ACL5658_IF_DTCT = 0x006B, + ACL5658_I2S1_CTRL = 0x0070, + ACL5658_I2S2_CTRL = 0x0071, + ACL5658_ADDA_CLK = 0x0073, + ACL5658_ADDA_HPF = 0x0074, + + /* TDM */ + ACL5658_TDM_CTRL1 = 0x0077, + ACL5658_TDM_CTRL2 = 0x0078, + ACL5658_TDM_CTRL3 = 0x0079, + ACL5658_TDM_CTRL4 = 0x007A, + + /* Global Clock*/ + ACL5658_GLBL_CLK = 0x0080, + ACL5658_GLBL_PLL1 = 0x0081, + ACL5658_GLBL_PLL2 = 0x0082, + ACL5658_GLBL_ASRC1 = 0x0083, + ACL5658_GLBL_ASRC2 = 0x0084, + ACL5658_GLBL_ASRC3 = 0x0085, + ACL5658_GLBL_ASRC4 = 0x008A, + + /* Amplifiers */ + ACL5658_HP_AMP = 0x008E, + ACL5658_SPK_AMP = 0x00A0, + +} ALC5658_REG; + +typedef struct { + uint16_t addr; + uint16_t val; + unsigned int delay; +} t_codec_init_script_entry; + +t_codec_init_script_entry codec_reset_script[] = { + {0x0000, 0x0000, 0}, +}; + +t_codec_init_script_entry codec_init_script[] = { + {0x0000, 0x0000, 0}, + {0x006E, 0xFFFF, 0}, + {0x006F, 0xFFFF, 0}, + {0x0080, 0x8000, 0}, + {0x0094, 0x0280, 0}, + {0x0111, 0xA502, 0}, + {0x0125, 0x0430, 0}, + {0x013A, 0x3020, 0}, + {0x0073, 0x1770, 0}, + +//{0x0070, 0x8020, 0}, + {0x0070, 0x0000, 0}, + {0x007B, 0x0003, 0}, + + {0x00FA, 0x0001, 0}, + {0x0091, 0x0C16, 0}, + {0x0063, 0xA23E, 60}, + {0x0063, 0xF23E, 50}, + {0x0062, 0x0400, 50}, + {0x0061, 0x8080, 10}, + {0x0029, 0x8080, 0}, + {0x002A, 0xAAAA, 0}, + {0x002D, 0x0000, 0}, + {0x008E, 0x0009, 50}, + {0x0061, 0x8C80, 50}, + {0x0091, 0x0E16, 50}, + {0x0040, 0x0505, 0}, + {0x0065, 0x0180, 0}, + {0x013C, 0x3C05, 0}, + {0x01DF, 0x02C1, 0}, + {0x01DF, 0x2CC1, 0}, + {0x01DE, 0x5100, 0}, + {0x01E4, 0x0014, 0}, + {0x01DE, 0xD100, 30}, + {0x01DF, 0x2CC1, 0}, + {0x01DE, 0x4900, 0}, + {0x01E4, 0x0016, 0}, + {0x01DE, 0xC900, 250}, + {0x01DF, 0x2CC1, 0}, + {0x0002, 0x0000, 0}, + {0x01DE, 0x4500, 0}, + {0x01E4, 0x001F, 0}, + {0x01DE, 0xC500, 800}, + {0x0040, 0x0808, 0}, + {0x0065, 0x0000, 0}, + {0x013C, 0x2005, 0}, + {0x01E4, 0x0000, 0}, + {0x01DF, 0x20C0, 0}, + {0x0073, 0x0770, 0}, + {0x0080, 0x0000, 0}, + {0x0160, 0x8EC0, 0}, + {0x008E, 0x0019, 0}, + {0x0015, 0xC0F0, 0}, + {0x0015, 0x87F9, 0}, + {0x0094, 0x0180, 0}, + {0x00FB, 0x3000, 0}, +}; + +/* Commonly defined and redefined macros */ + +#ifndef MIN +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef MAX +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct alc5658_dev_s { + /* We are an audio lower half driver (We are also the upper "half" of + * the ALC5658 driver with respect to the board lower half driver). + * + * Terminology: Our "lower" half audio instances will be called dev for the + * publicly visible version and "priv" for the version that only this driver + * knows. From the point of view of this driver, it is the board lower + * "half" that is referred to as "lower". + */ + + struct audio_lowerhalf_s dev; /* ALC5658 audio lower half (this device) */ + + /* Our specific driver data goes here */ + + FAR struct alc5658_lower_s *lower; /* Pointer to the board lower functions */ + FAR struct i2c_dev_s *i2c; /* I2C driver to use */ + FAR struct i2s_dev_s *i2s; /* I2S driver to use */ + struct dq_queue_s pendq; /* Queue of pending buffers to be sent */ + struct dq_queue_s doneq; /* Queue of sent buffers to be returned */ + mqd_t mq; /* Message queue for receiving messages */ + char mqname[16]; /* Our message queue name */ + pthread_t threadid; /* ID of our thread */ + uint32_t bitrate; /* Actual programmed bit rate */ + sem_t pendsem; /* Protect pendq */ +#ifdef ALC5658_USE_FFLOCK_INT + struct work_s work; /* Interrupt work */ +#endif + uint16_t samprate; /* Configured samprate (samples/sec) */ +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME +#ifndef CONFIG_AUDIO_EXCLUDE_BALANCE + uint16_t balance; /* Current balance level (b16) */ +#endif /* CONFIG_AUDIO_EXCLUDE_BALANCE */ + uint8_t volume; /* Current volume level {0..63} */ +#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */ + uint8_t nchannels; /* Number of channels (1 or 2) */ + uint8_t bpsamp; /* Bits per sample (8 or 16) */ + volatile uint8_t inflight; /* Number of audio buffers in-flight */ +#ifdef ALC5658_USE_FFLOCK_INT + volatile bool locked; /* FLL is locked */ +#endif + bool running; /* True: Worker thread is running */ + bool paused; /* True: Playing is paused */ + bool mute; /* True: Output is muted */ +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + bool terminating; /* True: Stop requested */ +#endif + bool reserved; /* True: Device is reserved */ + volatile int result; /* The result of the last transfer */ +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef CONFIG_ALC5658_CLKDEBUG +extern const uint8_t g_sysclk_scaleb1[ALC5658_BCLK_MAXDIV + 1]; +extern const uint8_t g_fllratio[ALC5658_NFLLRATIO]; +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: alc5658_readreg + * + * Description + * Read the specified 16-bit register from the ALC5658 device. + * + ****************************************************************************/ + +#if defined(CONFIG_ALC5658_REGDUMP) || defined(CONFIG_ALC5658_CLKDEBUG) +struct alc5658_dev_s; +uint16_t alc5658_readreg(FAR struct alc5658_dev_s *priv, uint8_t regaddr); +#endif + +#endif /* CONFIG_AUDIO */ +#endif /* __DRIVERS_AUDIO_ALC5658_H */ diff --git a/os/include/tinyara/audio/alc5658.h b/os/include/tinyara/audio/alc5658.h new file mode 100644 index 0000000..d3795b2 --- /dev/null +++ b/os/include/tinyara/audio/alc5658.h @@ -0,0 +1,284 @@ +/**************************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * 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. + * + ****************************************************************************/ +/**************************************************************************** + * include/tinyara/audio/alc5658.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Reference: + * "ALC5658 Ultra Low Power CODEC for Portable Audio Applications, Pre- + * Production", September 2012, Rev 3.3, Wolfson Microelectronics + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_TINYARA_AUDIO_ALC5658_H +#define __INCLUDE_TINYARA_AUDIO_ALC5658_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +#ifdef CONFIG_AUDIO_ALC5658 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************ + * + * CONFIG_AUDIO_ALC5658 - Enables ALC5658 support + * CONFIG_ALC5658_INITVOLUME - The initial volume level in the range {0..1000} + * CONFIG_ALC5658_INFLIGHT - Maximum number of buffers that the ALC5658 driver + * will send to the I2S driver before any have completed. + * CONFIG_ALC5658_MSG_PRIO - Priority of messages sent to the ALC5658 worker + * thread. + * CONFIG_ALC5658_BUFFER_SIZE - Preferred buffer size + * CONFIG_ALC5658_NUM_BUFFERS - Preferred number of buffers + * CONFIG_ALC5658_WORKER_STACKSIZE - Stack size to use when creating the the + * ALC5658 worker thread. + * CONFIG_ALC5658_REGDUMP - Enable logic to dump all ALC5658 registers to + * the SYSLOG device. + */ + +/* Pre-requisites */ + +#ifndef CONFIG_AUDIO +#error CONFIG_AUDIO is required for audio subsystem support +#endif + +#ifndef CONFIG_I2S +#error CONFIG_I2S is required by the ALC5658 driver +#endif + +#ifndef CONFIG_I2C +#error CONFIG_I2C is required by the ALC5658 driver +#endif + +#ifndef CONFIG_SCHED_WORKQUEUE +#error CONFIG_SCHED_WORKQUEUE is required by the ALC5658 driver +#endif + +/* Default configuration values */ + +#ifndef CONFIG_ALC5658_INITVOLUME +#define CONFIG_ALC5658_INITVOLUME 250 +#endif + +#ifndef CONFIG_ALC5658_INFLIGHT +#define CONFIG_ALC5658_INFLIGHT 2 +#endif + +#if CONFIG_ALC5658_INFLIGHT > 255 +#error CONFIG_ALC5658_INFLIGHT must fit in a uint8_t +#endif + +#ifndef CONFIG_ALC5658_MSG_PRIO +#define CONFIG_ALC5658_MSG_PRIO 1 +#endif + +#ifndef CONFIG_ALC5658_BUFFER_SIZE +#define CONFIG_ALC5658_BUFFER_SIZE 8192 +#endif + +#ifndef CONFIG_ALC5658_NUM_BUFFERS +#define CONFIG_ALC5658_NUM_BUFFERS 4 +#endif + +#ifndef CONFIG_ALC5658_WORKER_STACKSIZE +#define CONFIG_ALC5658_WORKER_STACKSIZE 768 +#endif + +/* Helper macros ************************************************************/ + +#define ALC5658_ATTACH(s,isr,arg) ((s)->attach(s,isr,arg)) +#define ALC5658_DETACH(s) ((s)->attach(s,NULL,NULL)) +#define ALC5658_ENABLE(s) ((s)->enable(s,true)) +#define ALC5658_DISABLE(s) ((s)->enable(s,false)) +#define ALC5658_RESTORE(s,e) ((s)->enable(s,e)) + +/**************************************************************************** + * Public Types + ****************************************************************************/ +/* This is the type of the ALC5658 interrupt handler. The lower level code + * will intercept the interrupt and provide the upper level with the private + * data that was provided when the interrupt was attached. + */ + +struct alc5658_lower_s; /* Forward reference. Defined below */ + +typedef CODE int (*alc5658_handler_t)(FAR const struct alc5658_lower_s *lower, FAR void *arg); + +/* A reference to a structure of this type must be passed to the ALC5658 + * driver. This structure provides information about the configuration + * of the ALC5658 and provides some board-specific hooks. + * + * Memory for this structure is provided by the caller. It is not copied + * by the driver and is presumed to persist while the driver is active. + */ + +struct alc5658_lower_s { + /* I2C characterization */ + struct i2c_config_s i2c_config; /* I2C config should be bere */ + + /* Clocking is provided via MCLK. The ALC5658 driver will need to know + * the frequency of MCLK in order to generate the correct bitrates. + */ + uint32_t mclk; /* Master clock frequency. In slave mode never used */ + + /* IRQ/GPIO access callbacks. These operations all hidden behind + * callbacks to isolate the ALC5658 driver from differences in GPIO + * interrupt handling by varying boards and MCUs. If possible, + * interrupts should be configured on both rising and falling edges + * so that contact and loss-of-contact events can be detected. + * + * attach - Attach or detach the ALC5658 interrupt handler to the GPIO + * interrupt + * enable - Enable or disable the GPIO interrupt. Returns the + * previous interrupt state. + */ + + CODE int (*attach)(FAR const struct alc5658_lower_s *lower, alc5658_handler_t isr, FAR void *arg); + CODE bool(*enable)(FAR const struct alc5658_lower_s *lower, bool enable); +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" { +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: alc5658_initialize + * + * Description: + * Initialize the ALC5658 device. + * + * Input Parameters: + * i2c - An I2C driver instance + * i2s - An I2S driver instance + * lower - Persistent board configuration data + * + * Returned Value: + * A new lower half audio interface for the ALC5658 device is returned on + * success; NULL is returned on failure. + * + ****************************************************************************/ + +struct i2c_dev_s; /* Forward reference. Defined in include/tinyara ... */ +struct i2s_dev_s; /* Forward reference. Defined in include/tinyara ... */ +struct audio_lowerhalf_s; /* Forward reference. Defined in tinyara/audio/audio.h */ + +FAR struct audio_lowerhalf_s *alc5658_initialize(struct i2c_dev_s *i2c, FAR struct i2s_dev_s *i2s, FAR struct alc5658_lower_s *lower); + +/**************************************************************************** + * Name: alc5658_dump_registers + * + * Description: + * Dump the contents of all ALC5658 registers to the syslog device + * + * Input Parameters: + * dev - The device instance returned by alc5658_initialize + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_ALC5658_REGDUMP +void alc5658_dump_registers(FAR struct audio_lowerhalf_s *dev, FAR const char *msg); +#else +/* This eliminates the need for any conditional compilation in the + * including file. + */ + +#define alc5658_dump_registers(d,m) +#endif + +/**************************************************************************** + * Name: alc5658_clock_analysis + * + * Description: + * Analyze the settings in the clock chain and dump to syslog. + * + * Input Parameters: + * dev - The device instance returned by alc5658_initialize + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_ALC5658_CLKDEBUG +void alc5658_clock_analysis(FAR struct audio_lowerhalf_s *dev, FAR const char *msg); +#else +/* This eliminates the need for any conditional compilation in the + * including file. + */ + +#define alc5658_clock_analysis(d,m) +#endif + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_AUDIO_ALC5658 */ +#endif /* __INCLUDE_TINYARA_AUDIO_ALC5658_H */ -- 2.7.4