diff --git a/arch/arm/src/lc823450/Kconfig b/arch/arm/src/lc823450/Kconfig index 4040a21a449..259d63fea81 100644 --- a/arch/arm/src/lc823450/Kconfig +++ b/arch/arm/src/lc823450/Kconfig @@ -185,6 +185,11 @@ config LC823450_I2C1 default n depends on I2C +config LC823450_I2S0 + bool "I2S0" + default n + depends on I2S + config LC823450_SPI_DMA bool "DMA for SPI" default n diff --git a/arch/arm/src/lc823450/Make.defs b/arch/arm/src/lc823450/Make.defs index e15c11db2b7..ada22c44f08 100644 --- a/arch/arm/src/lc823450/Make.defs +++ b/arch/arm/src/lc823450/Make.defs @@ -181,3 +181,7 @@ ifeq ($(CONFIG_LC823450_MTD),y) CHIP_CSRCS += lc823450_mtd.c CHIP_CSRCS += lc823450_mmcl.c endif + +ifeq ($(CONFIG_LC823450_I2S0),y) +CHIP_CSRCS += lc823450_i2s.c +endif diff --git a/arch/arm/src/lc823450/lc823450_i2s.c b/arch/arm/src/lc823450/lc823450_i2s.c new file mode 100644 index 00000000000..b467d6324e7 --- /dev/null +++ b/arch/arm/src/lc823450/lc823450_i2s.c @@ -0,0 +1,473 @@ +/**************************************************************************** + * arch/arm/src/lc823450/lc823450_i2s.c + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * 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 "up_arch.h" +#include "lc823450_dma.h" +#include "lc823450_i2s.h" +#include "lc823450_syscontrol.h" +#include "lc823450_clockconfig.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define LC823450_AUDIO_REGBASE 0x40060000 + +#define ABUF_REGBASE (LC823450_AUDIO_REGBASE + 0x0000) +#define BEEP_REGBASE (LC823450_AUDIO_REGBASE + 0x1200) +#define PCKGEN_REGBASE (LC823450_AUDIO_REGBASE + 0x1600) +#define AUDCTL_REGBASE (LC823450_AUDIO_REGBASE + 0x4000) + +#define ABUFCLR (ABUF_REGBASE + 0x0000) + +#define ABUFACCEN (ABUF_REGBASE + 0x0004) +#define ABUFACCEN_CDCFEN (1 << 5) + +#define ABUFIRQEN0 (ABUF_REGBASE + 0x0008) +#define ABUFIRQEN0_BFULIRQEN (1 << 5) + +#define ABUFSTS1 (ABUF_REGBASE + 0x0034) + +#define BUF_F_BASE (ABUF_REGBASE + 0x00c0 + (0x4 * 5)) +#define BUF_F_SIZE (ABUF_REGBASE + 0x0100 + (0x4 * 5)) +#define BUF_F_ULVL (ABUF_REGBASE + 0x0140 + (0x4 * 5)) +#define BUF_F_DTCAP (ABUF_REGBASE + 0x01c0 + (0x4 * 5)) +#define BUF_F_ACCESS (ABUF_REGBASE + 0x0300 + (0x4 * 5)) + +#define CLOCKEN (AUDCTL_REGBASE + 0x0000) +#define CLOCKEN_FCE_PCKGEN (1 << 28) +#define CLOCKEN_FCE_PCMPS0 (1 << 17) +#define CLOCKEN_FCE_BEEP (1 << 16) +#define CLOCKEN_FCE_VOLPS0 (1 << 13) + +#define AUDSEL (AUDCTL_REGBASE + 0x001c) +#define AUDSEL_PCM0_MODE (1 << 17) +#define AUDSEL_PCM0_MODEM (1 << 16) + +#define PSCTL (AUDCTL_REGBASE + 0x0110) + +#define PCMOUTEN (AUDCTL_REGBASE + 0x0500) +#define PCMOUTEN_DOUT0EN (1 << 3) +#define PCMOUTEN_LRCK0EN (1 << 2) +#define PCMOUTEN_MCLK0EN (1 << 1) +#define PCMOUTEN_BCK0EN (1 << 0) + +#define PCMCTL (AUDCTL_REGBASE + 0x0504) + +#define BEEP_CTL (BEEP_REGBASE + 0x0000) +#define BEEP_BYPASS (BEEP_REGBASE + 0x0004) +#define BEEP_COEFF (BEEP_REGBASE + 0x0008) +#define BEEP_TIME (BEEP_REGBASE + 0x000c) + +/* Audio PLL */ + +#define AUDIOPLL_REGBASE (LC823450_OSCSYS_REGBASE + 0x2000) +#define AUDPLLCNT (AUDIOPLL_REGBASE + 0x00) +#define AUDPLLMDIV (AUDIOPLL_REGBASE + 0x04) +#define AUDPLLNDIV (AUDIOPLL_REGBASE + 0x08) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The state of the one I2S peripheral */ + +struct lc823450_i2s_s +{ + struct i2s_dev_s dev; /* Externally visible I2S interface */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static uint32_t lc823450_i2s_txsamplerate(struct i2s_dev_s *dev, uint32_t rate); +static uint32_t lc823450_i2s_txdatawidth(struct i2s_dev_s *dev, int bits); +static int lc823450_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, + i2s_callback_t callback, void *arg, + uint32_t timeout); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* I2S device operations */ + +static const struct i2s_ops_s g_i2sops = +{ + /* Transmitter methods */ + + .i2s_txsamplerate = lc823450_i2s_txsamplerate, + .i2s_txdatawidth = lc823450_i2s_txdatawidth, + .i2s_send = lc823450_i2s_send, +}; + +static DMA_HANDLE _htxdma; +static sem_t _sem_txdma; +static sem_t _sem_buf_under; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +extern unsigned int XT1OSC_CLK; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: _setup_audio_pll + ****************************************************************************/ + +static void _setup_audio_pll(uint32_t freq) +{ + ASSERT(24000000 == XT1OSC_CLK); + + uint32_t m; + uint32_t n; + + switch (freq) + { + case 44100: + m = 625; + n = 3528; + break; + + case 48000: + m = 125; + n = 768; + break; + + default: + ASSERT(false); + } + + /* Set divider */ + + putreg32(n, AUDPLLNDIV); + putreg32(m, AUDPLLMDIV); + + /* Audio PLL standby=off, Audio PLL unreset */ + + putreg32(0x0503, AUDPLLCNT); + + /* TODO: Wait */ + + usleep(50 * 1000); + + /* Switch to the PLL */ + + modifyreg32(AUDCLKCNT, + 0x0, + 0x03 /* AUDCLKSEL=Audio PLL */ + ); + + /* TODO: Clock divider settings */ + + modifyreg32(AUDCLKCNT, + 0x0, + 0x0200 /* AUDDIV=2 */ + ); + +} + +/**************************************************************************** + * Name: _i2s_txdma_callback + ****************************************************************************/ + +static void _i2s_txdma_callback(DMA_HANDLE hdma, void *arg, int result) +{ + sem_t *waitsem = (sem_t *)arg; + nxsem_post(waitsem); +} + +/**************************************************************************** + * Name: _i2s_semtake + ****************************************************************************/ + +static void _i2s_semtake(FAR sem_t *sem) +{ + int ret; + + do + { + /* Take the semaphore (perhaps waiting) */ + + ret = nxsem_wait(sem); + + /* The only case that an error should occur here is if the wait was + * awakened by a signal. + */ + + DEBUGASSERT(ret == OK || ret == -EINTR); + } + while (ret == -EINTR); +} + + +/**************************************************************************** + * Name: lc823450_i2s_txsamplerate + ****************************************************************************/ + +static uint32_t lc823450_i2s_txsamplerate(struct i2s_dev_s *dev, uint32_t rate) +{ + /* TODO */ + return 0; +} + +/**************************************************************************** + * Name: lc823450_i2s_txdatawidth + ****************************************************************************/ + +static uint32_t lc823450_i2s_txdatawidth(struct i2s_dev_s *dev, int bits) +{ + /* TODO */ + return 0; +} + +/**************************************************************************** + * Name: _i2s_isr + ****************************************************************************/ + +static int _i2s_isr(int irq, FAR void *context, FAR void *arg) +{ + /* disable interrupt */ + + up_disable_irq(LC823450_IRQ_AUDIOBUF0); + + /* post semaphore for the waiter */ + + nxsem_post(&_sem_buf_under); + return 0; +} + +/**************************************************************************** + * Name: lc823450_i2s_send + ****************************************************************************/ + +static int lc823450_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, + i2s_callback_t callback, void *arg, + uint32_t timeout) +{ + /* Enable IRQ for Audio Buffer */ + + up_enable_irq(LC823450_IRQ_AUDIOBUF0); + + /* Wait for Audio Buffer */ + + _i2s_semtake(&_sem_buf_under); + + volatile uint32_t *ptr = (uint32_t *)&apb->samp[apb->curbyte]; + uint32_t n = apb->nbytes; + + /* Setup and start DMA for I2S */ + + lc823450_dmasetup(_htxdma, + LC823450_DMA_SRCINC | + LC823450_DMA_SRCWIDTH_WORD | + LC823450_DMA_DSTWIDTH_WORD, + (uint32_t)ptr, (uint32_t)BUF_F_ACCESS, n / 4); + + lc823450_dmastart(_htxdma, + _i2s_txdma_callback, + &_sem_txdma); + + _i2s_semtake(&_sem_txdma); + + /* Invoke the callback handler */ + + callback(dev, apb, arg, 0); + return OK; +} + +/**************************************************************************** + * Name: lc823450_i2s_beeptest + ****************************************************************************/ +#ifdef BEEP_TEST +static void lc823450_i2s_beeptest(void) +{ + /* Set BEEP params */ + + putreg32(0x0, BEEP_BYPASS); + putreg32(0x123ca6, BEEP_COEFF); /* 1kHz@fs=44.1k */ + putreg32(0xffff, BEEP_TIME); + + /* Start */ + + putreg32(0x3, BEEP_CTL); +} +#endif + +/**************************************************************************** + * Name: lc823450_i2s_configure + ****************************************************************************/ + +static int lc823450_i2s_configure(void) +{ + _setup_audio_pll(44100); + + /* Unreset Audio Buffer */ + + putreg32(MRSTCNTEXT3_AUDIOBUF_RSTB, + MRSTCNTEXT3); + + /* Enable clock to Audio Buffer */ + + putreg32(MCLKCNTEXT3_AUDIOBUF_CLKEN, + MCLKCNTEXT3); + + /* F Buffer = 32KB */ + + putreg32(4096 * 8, BUF_F_SIZE); + + /* Buffer Under Level = 1KB */ + + putreg32(1024, BUF_F_ULVL); + + /* Enable Buffer F Under Level IRQ */ + + putreg32(ABUFIRQEN0_BFULIRQEN, ABUFIRQEN0); + + /* Clear Audio Buffer */ + + putreg32(0xffff, ABUFCLR); + + /* Access Enable */ + + putreg32(ABUFACCEN_CDCFEN, ABUFACCEN); + + /* PCM0: BCK0/LRCK0=master, MCLK0=master */ + + putreg32(AUDSEL_PCM0_MODE | + AUDSEL_PCM0_MODEM, + AUDSEL); + + /* LRCK0/BCK0: 1/1fs, BCK0:64fs, BCK1:64fs */ + + putreg32(0x00001010, + PCMCTL); + + /* Enable DOUT0/LRCK0/MCL0/BCK0 */ + + putreg32(PCMOUTEN_DOUT0EN | + PCMOUTEN_LRCK0EN | + PCMOUTEN_MCLK0EN | + PCMOUTEN_BCK0EN, + PCMOUTEN); + + /* Stereo, PCMDLY=1, LRCK active low, + * MSB first and left justified, 32bit + */ + + putreg32(0x64, PSCTL); + + /* Enable PCMPS0 */ + + putreg32(CLOCKEN_FCE_PCKGEN | + CLOCKEN_FCE_BEEP | + CLOCKEN_FCE_PCMPS0 | + CLOCKEN_FCE_VOLPS0, + CLOCKEN); + + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lc823450_i2sdev_initialize + ****************************************************************************/ + +FAR struct i2s_dev_s *lc823450_i2sdev_initialize(void) +{ + FAR struct lc823450_i2s_s *priv = NULL; + + /* The support STM32 parts have only a single I2S port */ + + i2sinfo("port: %d\n", port); + + /* Allocate a new state structure for this chip select. NOTE that there + * is no protection if the same chip select is used in two different + * chip select structures. + */ + + priv = (struct lc823450_i2s_s *)zalloc(sizeof(struct lc823450_i2s_s)); + if (!priv) + { + i2serr("ERROR: Failed to allocate a chip select structure\n"); + return NULL; + } + + /* Initialize the common parts for the I2S device structure */ + + priv->dev.ops = &g_i2sops; + + (void)lc823450_i2s_configure(); + +#ifdef BEEP_TEST + lc823450_i2s_beeptest(); +#endif + + _htxdma = lc823450_dmachannel(DMA_CHANNEL_VIRTUAL); + nxsem_init(&_sem_txdma, 0, 0); + nxsem_init(&_sem_buf_under, 0, 0); + + irq_attach(LC823450_IRQ_AUDIOBUF0, _i2s_isr, NULL); + + /* Success exit */ + + return &priv->dev; +} diff --git a/arch/arm/src/lc823450/lc823450_i2s.h b/arch/arm/src/lc823450/lc823450_i2s.h new file mode 100644 index 00000000000..6a12f4ec9c0 --- /dev/null +++ b/arch/arm/src/lc823450/lc823450_i2s.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * arch/arm/src/lc823450/lc823450_i2s.h + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * 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 __ARCH_ARM_SRC_LC823450_LC823450_I2S_H +#define __ARCH_ARM_SRC_LC823450_LC823450_I2S_H + + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include "chip.h" + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +FAR struct i2s_dev_s *lc823450_i2sdev_initialize(void); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_ARM_SRC_LC823450_LC823450_I2S_H */ diff --git a/arch/arm/src/lc823450/lc823450_syscontrol.h b/arch/arm/src/lc823450/lc823450_syscontrol.h index 509f2c3b9f2..d31edf4eda6 100644 --- a/arch/arm/src/lc823450/lc823450_syscontrol.h +++ b/arch/arm/src/lc823450/lc823450_syscontrol.h @@ -108,6 +108,9 @@ #define MCLKCNTEXT1_PTM1C_CLKEN (1 << 29) #define MCLKCNTEXT1_PTM2C_CLKEN (1 << 30) +#define MCLKCNTEXT3 (LC823450_SYSCONTROL_REGBASE + 0x0108) +#define MCLKCNTEXT3_AUDIOBUF_CLKEN (1 << 0) + #define MCLKCNTEXT4 (LC823450_SYSCONTROL_REGBASE + 0x010c) #define MCLKCNTEXT4_SDRAMC_CLKEN0 (1 << 0) #define MCLKCNTEXT4_SDRAMC_CLKEN1 (1 << 1) @@ -156,6 +159,9 @@ #define MRSTCNTEXT1_SDIF2_RSTB (1 << 10) #define MRSTCNTEXT1_MSIF_RSTB (1 << 11) +#define MRSTCNTEXT3 (LC823450_SYSCONTROL_REGBASE + 0x011c) +#define MRSTCNTEXT3_AUDIOBUF_RSTB (1 << 0) + #define MRSTCNTEXT4 (LC823450_SYSCONTROL_REGBASE + 0x0120) #define MRSTCNTEXT4_SDRAMC_RSTB (1 << 0) diff --git a/configs/lc823450-xgevk/README.txt b/configs/lc823450-xgevk/README.txt index b91b25a3be5..4030086ecdf 100644 --- a/configs/lc823450-xgevk/README.txt +++ b/configs/lc823450-xgevk/README.txt @@ -21,7 +21,7 @@ MakeIPL2 Tool for eMMC boot is available at This port is intended to test LC823450 features including SMP. Supported peripherals: -UART, TIMER, RTC, GPIO, DMA, I2C, SPI, LCD, eMMC, USB, WDT, ADC. +UART, TIMER, RTC, GPIO, DMA, I2C, SPI, LCD, eMMC, USB, WDT, ADC, Audio. Settings ^^^^^^^^ @@ -43,9 +43,6 @@ output into the console because UART operates in FIFO mode. 1. "nsh> smp" works but the result will be corrupted. 2. "nsh> ostest" works but might cause a deadlock or assertion. - - - Other Status ^^^^^^^^^^^^ @@ -187,8 +184,27 @@ then dd the files to the kernel partition (/dev/mtdblock0p4) and the IPL2 partit nsh> dd if=/mnt/sd0/LC8234xx_17S_start_data.boot_bin of=/dev/mtdblock0p1 nsh> reboot +10. Audio playback (WAV/44.1k/16bit/2ch only) + +Firstly, please check the jumper pin settings as follows. + + JP1, JP2 => short + JP3, JP4 => open + +To play WAV file on uSD card, + + nsh> mount -t vfat /dev/mtdblock1 /mnt/sd1 + nsh> nxplayer + nxplayer> play /mnt/sd1/sample.wav + nxplayer> volume 50 + +Currently nxplayer does not work in SMP mode. + + up_assert: Assertion failed at file:chip/lc823450_cpupause.c line: 279 task: wm8776 + + TODO ^^^^ The following features will be supported. -Audio, etc. +LED, Accelerometer, etc. diff --git a/configs/lc823450-xgevk/audio/defconfig b/configs/lc823450-xgevk/audio/defconfig new file mode 100644 index 00000000000..c337fa3d08b --- /dev/null +++ b/configs/lc823450-xgevk/audio/defconfig @@ -0,0 +1,143 @@ +CONFIG_AQM_1248A=y +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="lc823450-xgevk" +CONFIG_ARCH_BOARD_LC823450_XGEVK=y +CONFIG_ARCH_CHIP_LC823450=y +CONFIG_ARCH_FLOAT_H=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_STDARG_H=y +CONFIG_AUDIO_BUFFER_NUMBYTES=1024 +CONFIG_AUDIO_EXCLUDE_BALANCE=y +CONFIG_AUDIO_EXCLUDE_FFORWARD=y +CONFIG_AUDIO_EXCLUDE_TONE=y +# CONFIG_AUDIO_FORMAT_MP3 is not set +CONFIG_AUDIO_WM8776=y +CONFIG_AUDIO=y +CONFIG_BOARDCTL_RESET=y +CONFIG_BOARD_LOOPSPERMSEC=12061 +CONFIG_BUILTIN=y +CONFIG_C99_BOOL8=y +CONFIG_CODECS_HASH_MD5=y +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_WARN=y +CONFIG_DEV_ZERO=y +CONFIG_DISABLE_POSIX_TIMERS=y +CONFIG_DRIVERS_AUDIO=y +CONFIG_EXAMPLES_HELLO=y +CONFIG_EXAMPLES_NSH=y +CONFIG_EXAMPLES_NXHELLO_BPP=1 +CONFIG_EXAMPLES_NXHELLO=y +CONFIG_EXAMPLES_OSTEST=y +CONFIG_EXAMPLES_PIPE=y +CONFIG_EXPERIMENTAL=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FATTIME=y +CONFIG_FS_FAT=y +CONFIG_FS_PROCFS=y +CONFIG_I2C_RESET=y +CONFIG_I2CTOOL_MAXBUS=1 +CONFIG_I2C=y +CONFIG_I2S=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LC823450_I2C0=y +CONFIG_LC823450_I2C1=y +CONFIG_LC823450_I2S0=y +CONFIG_LC823450_MTD=y +CONFIG_LC823450_SDIF_SDC=y +CONFIG_LC823450_SPI_DMA=y +CONFIG_LC823450_UART0=y +CONFIG_LCD_ST7565=y +CONFIG_LCD=y +CONFIG_LIB_KBDCODEC=y +CONFIG_LIBM=y +CONFIG_MAX_TASKS=64 +CONFIG_MAX_WDOGPARMS=2 +CONFIG_MEMSET_OPTSPEED=y +CONFIG_MQ_MAXMSGSIZE=64 +CONFIG_MTD=y +CONFIG_NAME_MAX=765 +CONFIG_NETUTILS_CODECS=y +CONFIG_NFILE_DESCRIPTORS=45 +CONFIG_NFILE_STREAMS=8 +CONFIG_NSH_ARCHINIT=y +# CONFIG_NSH_ARGCAT is not set +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_CMDOPT_DD_STATS=y +CONFIG_NSH_DISABLE_BASENAME=y +CONFIG_NSH_DISABLE_DIRNAME=y +CONFIG_NSH_DISABLE_EXEC=y +CONFIG_NSH_DISABLE_GET=y +CONFIG_NSH_DISABLE_LOSETUP=y +CONFIG_NSH_DISABLE_MB=y +CONFIG_NSH_DISABLE_MH=y +CONFIG_NSH_DISABLE_MKFIFO=y +CONFIG_NSH_DISABLE_MKRD=y +CONFIG_NSH_DISABLE_PUT=y +CONFIG_NSH_DISABLE_SH=y +CONFIG_NSH_DISABLE_XD=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=128 +CONFIG_NSH_MAXARGUMENTS=10 +CONFIG_NSH_READLINE=y +CONFIG_NX_BLOCKING=y +# CONFIG_NX_DISABLE_1BPP is not set +CONFIG_NXFONT_MONO5X8=y +CONFIG_NXPLAYER_DEFAULT_MEDIADIR="/mnt/sd1" +CONFIG_NX=y +CONFIG_PIPES=y +CONFIG_POSIX_SPAWN_PROXY_STACKSIZE=2048 +CONFIG_PREALLOC_MQ_MSGS=4 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PREALLOC_WDOGS=16 +CONFIG_PTHREAD_MUTEX_TYPES=y +CONFIG_PTHREAD_STACK_DEFAULT=3072 +CONFIG_RAM_SIZE=1044480 +CONFIG_RAM_START=0x02001000 +CONFIG_RAW_BINARY=y +CONFIG_READLINE_CMD_HISTORY=y +CONFIG_RTC_DATETIME=y +CONFIG_RTC=y +CONFIG_SCHED_ATEXIT=y +CONFIG_SCHED_CHILD_STATUS=y +CONFIG_SCHED_HAVE_PARENT=y +CONFIG_SCHED_HPWORKPRIORITY=192 +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_ONEXIT_MAX=32 +CONFIG_SCHED_ONEXIT=y +CONFIG_SCHED_STARTHOOK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_SERIAL_TERMIOS=y +# CONFIG_SPI_EXCHANGE is not set +CONFIG_SPI=y +CONFIG_START_DAY=3 +CONFIG_START_MONTH=10 +CONFIG_START_YEAR=2013 +CONFIG_SYSTEM_I2CTOOL=y +CONFIG_SYSTEM_NXPLAYER=y +CONFIG_SYSTEM_USBMSC_CMD_STACKSIZE=2048 +CONFIG_SYSTEM_USBMSC_DEVPATH1="/dev/mtdblock0p10" +CONFIG_SYSTEM_USBMSC_DEVPATH2="/dev/mtdblock1" +CONFIG_SYSTEM_USBMSC_NLUNS=2 +CONFIG_SYSTEM_USBMSC=y +CONFIG_TASK_NAME_SIZE=24 +CONFIG_UART0_RXBUFSIZE=512 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_UART0_TXBUFSIZE=2048 +CONFIG_USBDEV_BUSPOWERED=y +CONFIG_USBDEV_DUALSPEED=y +CONFIG_USBDEV_MAXPOWER=500 +CONFIG_USBDEV=y +CONFIG_USBMSC_EPBULKIN=2 +CONFIG_USBMSC_EPBULKOUT=1 +CONFIG_USBMSC_REMOVABLE=y +CONFIG_USBMSC=y +CONFIG_USER_ENTRYPOINT="nsh_main" +CONFIG_USERMAIN_STACKSIZE=3072 +CONFIG_WM8776_SWAP_HPOUT=y diff --git a/configs/lc823450-xgevk/src/Makefile b/configs/lc823450-xgevk/src/Makefile index 975cf9e9bef..38a4ab779ad 100644 --- a/configs/lc823450-xgevk/src/Makefile +++ b/configs/lc823450-xgevk/src/Makefile @@ -72,4 +72,8 @@ ifeq ($(CONFIG_NETDEVICES),y) CSRCS += lc823450_netinit.c endif +ifeq ($(CONFIG_AUDIO_WM8776),y) +CSRCS += lc823450_wm8776.c +endif + include $(TOPDIR)/configs/Board.mk diff --git a/configs/lc823450-xgevk/src/lc823450-xgevk.h b/configs/lc823450-xgevk/src/lc823450-xgevk.h index a73818d7c90..ab80b2bcd7e 100644 --- a/configs/lc823450-xgevk/src/lc823450-xgevk.h +++ b/configs/lc823450-xgevk/src/lc823450-xgevk.h @@ -94,5 +94,9 @@ int lc823450_bringup(void); int lc823450_bma250initialize(FAR const char *devpath); #endif +#ifdef CONFIG_AUDIO_WM8776 +int lc823450_wm8776initialize(int minor); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __CONFIGS_LC823450_XGEVK_SRC_LC823450_XGEVK_H */ diff --git a/configs/lc823450-xgevk/src/lc823450-xgevk_mux.h b/configs/lc823450-xgevk/src/lc823450-xgevk_mux.h index d55b81da09e..97295194ef6 100644 --- a/configs/lc823450-xgevk/src/lc823450-xgevk_mux.h +++ b/configs/lc823450-xgevk/src/lc823450-xgevk_mux.h @@ -149,11 +149,11 @@ 1 << 10 | /* 0: GPIO15, 1:DOUT1 */ \ 0 << 12 | /* 0: GPIO16, 1:NLBEXA0 */ \ 0 << 14 | /* 0: GPIO17, 1:NRD */ \ - 0 << 16 | /* 0: GPIO18, 1:MCLK0, 2:MCLK1 */ \ - 0 << 18 | /* 0: GPIO19, 1:BCK0, 2:DMCKO1 */ \ - 0 << 20 | /* 0: GPIO1A, 1:LRCK0, 2:DMDIN1 */ \ - 2 << 22 | /* 0: GPIO1B, 1:DIN0, 2:DMDIN0 */ \ - 0 << 24 | /* 0: GPIO1C, 1:DOUT0, 2:DMCKO0 */ \ + 1 << 16 | /* 0: GPIO18, 1:MCLK0, 2:MCLK1 */ \ + 1 << 18 | /* 0: GPIO19, 1:BCK0, 2:DMCKO1 */ \ + 1 << 20 | /* 0: GPIO1A, 1:LRCK0, 2:DMDIN1 */ \ + 1 << 22 | /* 0: GPIO1B, 1:DIN0, 2:DMDIN0 */ \ + 1 << 24 | /* 0: GPIO1C, 1:DOUT0, 2:DMCKO0 */ \ 1 << 26 | /* 0: GPIO1D, 1:SCK0 */ \ 0 << 28 | /* 0: GPIO1E, 1:SDI0 */ \ 1 << 30 /* 0: GPIO1F, 1:SDO0 */ @@ -185,7 +185,7 @@ 0 << 10 | /* GPIO15 0:1mA, 1:---, 2:2mA, 3:4mA */ \ 0 << 12 | /* GPIO16 0:2mA, 1:---, 2:4mA, 3:8mA */ \ 0 << 14 | /* GPIO17 0:2mA, 1:---, 2:4mA, 3:8mA */ \ - 0 << 16 | /* GPIO18 0:1mA, 1:---, 2:2mA, 3:4mA */ \ + 3 << 16 | /* GPIO18 0:1mA, 1:---, 2:2mA, 3:4mA */ \ 0 << 18 | /* GPIO19 0:1mA, 1:---, 2:2mA, 3:4mA */ \ 0 << 20 | /* GPIO1A 0:1mA, 1:---, 2:2mA, 3:4mA */ \ 0 << 22 | /* GPIO1B 0:1mA, 1:---, 2:2mA, 3:4mA */ \ diff --git a/configs/lc823450-xgevk/src/lc823450_bringup.c b/configs/lc823450-xgevk/src/lc823450_bringup.c index 98447fb1285..f4bc4065757 100644 --- a/configs/lc823450-xgevk/src/lc823450_bringup.c +++ b/configs/lc823450-xgevk/src/lc823450_bringup.c @@ -85,6 +85,10 @@ int lc823450_bringup(void) lc823450_bma250initialize("/dev/accel"); #endif +#ifdef CONFIG_AUDIO_WM8776 + lc823450_wm8776initialize(0); +#endif + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities. diff --git a/configs/lc823450-xgevk/src/lc823450_wm8776.c b/configs/lc823450-xgevk/src/lc823450_wm8776.c new file mode 100644 index 00000000000..50116a6ecc5 --- /dev/null +++ b/configs/lc823450-xgevk/src/lc823450_wm8776.c @@ -0,0 +1,130 @@ +/**************************************************************************** + * configs/lc823450-xgevk/src/lc823450_wm8776.c + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * 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 "up_arch.h" +#include "lc823450_i2c.h" +#include "lc823450_i2s.h" +#include "lc823450-xgevk.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define WM8776_I2C_PORTNO 0 /* On I2C0 */ +#define WM8776_I2C_ADDR 0x1a + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct wm8776_lower_s g_wm8776info = +{ + .address = WM8776_I2C_ADDR, + .frequency = 400000, +}; + + +/**************************************************************************** + * Name: lc823450_wm8776initialize + ****************************************************************************/ + +int lc823450_wm8776initialize(int minor) +{ + FAR struct audio_lowerhalf_s *wm8776; + FAR struct audio_lowerhalf_s *pcm; + FAR struct i2c_master_s *i2c; + FAR struct i2s_dev_s *i2s; + char devname[12]; + int ret; + + ainfo("Initializing WM8776 \n"); + + /* Initialize I2C */ + + i2c = lc823450_i2cbus_initialize(WM8776_I2C_PORTNO); + + if (!i2c) + { + return -ENODEV; + } + + i2s = lc823450_i2sdev_initialize(); + + wm8776 = wm8776_initialize(i2c, i2s, &g_wm8776info); + + if (!wm8776) + { + auderr("ERROR: Failed to initialize the WM8904\n"); + return -ENODEV; + } + + pcm = pcm_decode_initialize(wm8776); + + if (!pcm) + { + auderr("ERROR: Failed create the PCM decoder\n"); + return -ENODEV; + } + + snprintf(devname, 12, "pcm%d", minor); + + ret = audio_register(devname, pcm); + + if (ret < 0) + { + auderr("ERROR: Failed to register /dev/%s device: %d\n", devname, ret); + } + + return 0; +} + diff --git a/drivers/audio/Kconfig b/drivers/audio/Kconfig index aad77e7ea5f..a8d9fa82c11 100644 --- a/drivers/audio/Kconfig +++ b/drivers/audio/Kconfig @@ -155,6 +155,34 @@ config CS43L22_CLKDEBUG endif # AUDIO_CS43L22 +config AUDIO_WM8776 + bool "WM8776 audio chip" + default n + depends on AUDIO + ---help--- + Select to enable support for the WM8776 Audio codec by Wolfson + Microelectonics. + +if AUDIO_WM8776 + +config WM8776_INFLIGHT + int "WM8776 maximum in-flight audio buffers" + default 2 + +config WM8776_MSG_PRIO + int "WM8776 message priority" + default 1 + +config WM8776_WORKER_STACKSIZE + int "WM8776 worker thread stack size" + default 768 + +config WM8776_SWAP_HPOUT + bool "Swap WM8776 HP out signals" + default n + +endif # AUDIO_WM8776 + config AUDIO_WM8904 bool "WM8904 audio chip" default n diff --git a/drivers/audio/Make.defs b/drivers/audio/Make.defs index acba0d1a8ef..5dfca4c3dec 100644 --- a/drivers/audio/Make.defs +++ b/drivers/audio/Make.defs @@ -65,6 +65,10 @@ endif endif endif +ifeq ($(CONFIG_AUDIO_WM8776),y) +CSRCS += wm8776.c +endif + ifeq ($(CONFIG_AUDIO_NULL),y) CSRCS += audio_null.c endif diff --git a/drivers/audio/wm8776.c b/drivers/audio/wm8776.c new file mode 100644 index 00000000000..3157a6626da --- /dev/null +++ b/drivers/audio/wm8776.c @@ -0,0 +1,1350 @@ +/**************************************************************************** + * drivers/audio/wm8776.c + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * Based on drivers/audio/wm8904.c + * + * 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 "wm8776.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +static void wm8776_writereg(FAR struct wm8776_dev_s *priv, + uint8_t regaddr, uint16_t regval); + +static void wm8776_takesem(sem_t *sem); +#define wm8776_givesem(s) nxsem_post(s) + +static int wm8776_getcaps(FAR struct audio_lowerhalf_s *dev, int type, + FAR struct audio_caps_s *caps); +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_configure(FAR struct audio_lowerhalf_s *dev, + FAR void *session, FAR const struct audio_caps_s *caps); +#else +static int wm8776_configure(FAR struct audio_lowerhalf_s *dev, + FAR const struct audio_caps_s *caps); +#endif +static int wm8776_shutdown(FAR struct audio_lowerhalf_s *dev); +static void wm8776_senddone(FAR struct i2s_dev_s *i2s, + FAR struct ap_buffer_s *apb, FAR void *arg, int result); +static void wm8776_returnbuffers(FAR struct wm8776_dev_s *priv); +static int wm8776_sendbuffer(FAR struct wm8776_dev_s *priv); + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_start(FAR struct audio_lowerhalf_s *dev, + FAR void *session); +#else +static int wm8776_start(FAR struct audio_lowerhalf_s *dev); +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_STOP +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_stop(FAR struct audio_lowerhalf_s *dev, + FAR void *session); +#else +static int wm8776_stop(FAR struct audio_lowerhalf_s *dev); +#endif +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_pause(FAR struct audio_lowerhalf_s *dev, + FAR void *session); +static int wm8776_resume(FAR struct audio_lowerhalf_s *dev, + FAR void *session); +#else +static int wm8776_pause(FAR struct audio_lowerhalf_s *dev); +static int wm8776_resume(FAR struct audio_lowerhalf_s *dev); +#endif +#endif +static int wm8776_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, + FAR struct ap_buffer_s *apb); +static int wm8776_cancelbuffer(FAR struct audio_lowerhalf_s *dev, + FAR struct ap_buffer_s *apb); +static int wm8776_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd, + unsigned long arg); +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev, + FAR void **session); +#else +static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev); +#endif +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_release(FAR struct audio_lowerhalf_s *dev, + FAR void *session); +#else +static int wm8776_release(FAR struct audio_lowerhalf_s *dev); +#endif + +static void *wm8776_workerthread(pthread_addr_t pvarg); + +/* Initialization */ + +static void wm8776_audio_output(FAR struct wm8776_dev_s *priv); +#if 0 /* Not used */ +static void wm8776_audio_input(FAR struct wm8776_dev_s *priv); +#endif +static void wm8776_hw_reset(FAR struct wm8776_dev_s *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct audio_ops_s g_audioops = +{ + wm8776_getcaps, /* getcaps */ + wm8776_configure, /* configure */ + wm8776_shutdown, /* shutdown */ + wm8776_start, /* start */ +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + wm8776_stop, /* stop */ +#endif +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME + wm8776_pause, /* pause */ + wm8776_resume, /* resume */ +#endif + NULL, /* allocbuffer */ + NULL, /* freebuffer */ + wm8776_enqueuebuffer, /* enqueue_buffer */ + wm8776_cancelbuffer, /* cancel_buffer */ + wm8776_ioctl, /* ioctl */ + NULL, /* read */ + NULL, /* write */ + wm8776_reserve, /* reserve */ + wm8776_release /* release */ +}; + +/************************************************************************************ + * Name: wm8776_writereg + * + * Description: + * Write the specified 16-bit register to the WM8776 device. + * + ************************************************************************************/ + +static void wm8776_writereg(FAR struct wm8776_dev_s *priv, + uint8_t regaddr, + uint16_t regval) +{ + struct i2c_config_s config; + + /* Setup up the I2C configuration */ + + config.frequency = priv->lower->frequency; + config.address = priv->lower->address; + config.addrlen = 7; + + uint8_t data[2]; + int ret; + + /* Set up the data to write */ + + data[0] = (regaddr << 1) + ((regval >> 8) & 0x1); + data[1] = (regval & 0xff); + + ret = i2c_write(priv->i2c, &config, data, sizeof(data)); + + if (ret < 0) + { + auderr("ERROR: I2C_TRANSFER failed: %d\n", ret); + } +} + +/************************************************************************************ + * Name: wm8776_takesem + * + * Description: + * Take a semaphore count, handling the nasty EINTR return if we are interrupted + * by a signal. + * + ************************************************************************************/ + +static void wm8776_takesem(sem_t *sem) +{ + int ret; + + do + { + /* Take the semaphore (perhaps waiting) */ + + ret = nxsem_wait(sem); + + /* The only case that an error should occur here is if the wait was + * awakened by a signal. + */ + + DEBUGASSERT(ret == OK || ret == -EINTR); + } + while (ret == -EINTR); +} + +/************************************************************************************ + * Name: wm8776_setvolume + * + * Description: + * Set the right and left volume values in the WM8776 device based on the current + * volume and balance settings. + * + ************************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME +static void wm8776_setvolume(FAR struct wm8776_dev_s *priv, uint16_t volume, + bool mute) +{ + uint16_t regval; + uint16_t tmp_vol; + + /* TODO: balance */ + + if (mute) + { + tmp_vol = 0; + } + else + { + tmp_vol = volume; + } + + /* limit the max vol */ + + if (tmp_vol > 0x69) + { + tmp_vol = 0x69; /* -10db */ + } + + regval = WM8776_UPDATE | WM8776_HPOUT_VOL(tmp_vol); + + wm8776_writereg(priv, WM8776_MASTER_ATT, regval); + + audinfo("volume=%d mute=%d tmp_vol=%d (regval=0x%x) \n", + volume, mute, tmp_vol, regval); + + /* Remember the volume level and mute settings */ + + priv->volume = volume; + priv->mute = mute; +} +#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */ + +/**************************************************************************** + * Name: wm8776_getcaps + * + * Description: + * Get the audio device capabilities + * + ****************************************************************************/ + +static int wm8776_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)); + audinfo("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; + + 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_44K; + break; + + case AUDIO_FMT_MP3: + case AUDIO_FMT_WMA: + case AUDIO_FMT_PCM: + default: + 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: wm8776_configure + * + * Description: + * Configure the audio device for the specified mode of operation. + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_configure(FAR struct audio_lowerhalf_s *dev, + FAR void *session, + FAR const struct audio_caps_s *caps) +#else +static int wm8776_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 wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; +#endif + int ret = OK; + + DEBUGASSERT(priv && caps); + audinfo("ac_type: %d\n", caps->ac_type); + + /* Process the configure operation */ + + switch (caps->ac_type) + { + case AUDIO_TYPE_FEATURE: + audinfo(" 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]; + audinfo(" Volume: %d\n", volume); + + if (volume >= 0 && volume <= 1000) + { + /* Scale the volume setting to the range {0x2f .. 0x79} */ + + wm8776_setvolume(priv, (0x4a * volume / 1000) + 0x2f, priv->mute); + } + else + { + ret = -EDOM; + } + } + break; +#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */ + + default: + auderr(" ERROR: Unrecognized feature unit\n"); + ret = -ENOTTY; + break; + } + break; + + case AUDIO_TYPE_OUTPUT: + { + audinfo(" AUDIO_TYPE_OUTPUT:\n"); + audinfo(" Number of channels: %u\n", caps->ac_channels); + audinfo(" Sample rate: %u\n", caps->ac_controls.hw[0]); + audinfo(" 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) + { + auderr("ERROR: Unsupported number of channels: %d\n", + caps->ac_channels); + break; + } + + if (caps->ac_controls.b[2] != 8 && caps->ac_controls.b[2] != 16) + { + auderr("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]; + + /* TODO : channels, bits per sample, bitrate */ + + ret = OK; + } + break; + + case AUDIO_TYPE_PROCESSING: + break; + } + + return ret; +} + +/**************************************************************************** + * Name: wm8776_shutdown + * + * Description: + * Shutdown the WM8776 chip and put it in the lowest power state possible. + * + ****************************************************************************/ + +static int wm8776_shutdown(FAR struct audio_lowerhalf_s *dev) +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; + + DEBUGASSERT(priv); + + /* Now issue a software reset. This puts all WM8776 registers back in + * their default state. + */ + + wm8776_hw_reset(priv); + return OK; +} + +/**************************************************************************** + * Name: wm8776_senddone + * + * Description: + * This is the I2S callback function that is invoked when the transfer + * completes. + * + ****************************************************************************/ + +static void wm8776_senddone(FAR struct i2s_dev_s *i2s, + FAR struct ap_buffer_s *apb, FAR void *arg, + int result) +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)arg; + struct audio_msg_s msg; + irqstate_t flags; + int ret; + + DEBUGASSERT(i2s && priv && priv->running && apb); + audinfo("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_WM8776_MSG_PRIO); + if (ret < 0) + { + auderr("ERROR: mq_send failed: %d\n", errno); + } +} + +/**************************************************************************** + * Name: wm8776_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 wm8776_returnbuffers(FAR struct wm8776_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); + + audinfo("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). + */ + + audinfo("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: wm8776_sendbuffer + * + * Description: + * Start the transfer an audio buffer to the WM8776 via I2S. This + * will not wait for the transfer to complete but will return immediately. + * the wmd8776_senddone called will be invoked when the transfer + * completes, stimulating the worker thread to call this function again. + * + ****************************************************************************/ + +static int wm8776_sendbuffer(FAR struct wm8776_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_WM8776_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'. + */ + + wm8776_takesem(&priv->pendsem); + while (priv->inflight < CONFIG_WM8776_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); + audinfo("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); + + 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, wm8776_senddone, priv, timeout); + if (ret < 0) + { + auderr("ERROR: I2S_SEND failed: %d\n", ret); + break; + } + } + + wm8776_givesem(&priv->pendsem); + return ret; +} + +/**************************************************************************** + * Name: wm8776_start + * + * Description: + * Start the configured operation (audio streaming, volume enabled, etc.). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_start(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int wm8776_start(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; + struct sched_param sparam; + struct mq_attr attr; + pthread_attr_t tattr; + FAR void *value; + int ret; + + audinfo("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! */ + + auderr("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) + { + audinfo("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_WM8776_WORKER_STACKSIZE); + + audinfo("Starting worker thread\n"); + ret = pthread_create(&priv->threadid, &tattr, wm8776_workerthread, + (pthread_addr_t)priv); + if (ret != OK) + { + auderr("ERROR: pthread_create failed: %d\n", ret); + } + else + { + pthread_setname_np(priv->threadid, "wm8776"); + audinfo("Created worker thread\n"); + } + + return ret; +} + +/**************************************************************************** + * Name: wm8776_stop + * + * Description: Stop the configured operation (audio streaming, volume + * disabled, etc.). + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int wm8776_stop(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_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_WM8776_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: wm8776_pause + * + * Description: Pauses the playback. + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int wm8776_pause(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; + + if (priv->running && !priv->paused) + { + priv->paused = true; + wm8776_setvolume(priv, priv->volume, true); + } + + return OK; +} +#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */ + +/**************************************************************************** + * Name: wm8776_resume + * + * Description: Resumes the playback. + * + ****************************************************************************/ + +#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session) +#else +static int wm8776_resume(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; + + if (priv->running && priv->paused) + { + priv->paused = false; + wm8776_setvolume(priv, priv->volume, false); + wm8776_sendbuffer(priv); + } + + return OK; +} +#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */ + +/**************************************************************************** + * Name: wm8776_enqueuebuffer + * + * Description: Enqueue an Audio Pipeline Buffer for playback/ processing. + * + ****************************************************************************/ + +static int wm8776_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, + FAR struct ap_buffer_s *apb) +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *)dev; + struct audio_msg_s term_msg; + int ret; + + audinfo("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 */ + + wm8776_takesem(&priv->pendsem); + apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; + dq_addlast(&apb->dq_entry, &priv->pendq); + wm8776_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_WM8776_MSG_PRIO); + if (ret < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + auderr("ERROR: mq_send failed: %d\n", errcode); + UNUSED(errcode); + } + } + + return ret; +} + +/**************************************************************************** + * Name: wm8776_cancelbuffer + * + * Description: Called when an enqueued buffer is being cancelled. + * + ****************************************************************************/ + +static int wm8776_cancelbuffer(FAR struct audio_lowerhalf_s *dev, + FAR struct ap_buffer_s *apb) +{ + audinfo("apb=%p\n", apb); + return OK; +} + +/**************************************************************************** + * Name: wm8776_ioctl + * + * Description: Perform a device ioctl + * + ****************************************************************************/ + +static int wm8776_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 WM8776 + * registers back in their default state. + */ + + audinfo("AUDIOIOC_HWRESET:\n"); + } + break; + + /* Report our preferred buffer size and quantity */ + +#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS + case AUDIOIOC_GETBUFFERINFO: + { + audinfo("AUDIOIOC_GETBUFFERINFO:\n"); + bufinfo = (FAR struct ap_buffer_info_s *) arg; + bufinfo->buffer_size = CONFIG_WM8776_BUFFER_SIZE; + bufinfo->nbuffers = CONFIG_WM8776_NUM_BUFFERS; + } + break; +#endif + + default: + audinfo("Ignored\n"); + break; + } + + return OK; +} + +/**************************************************************************** + * Name: wm8776_reserve + * + * Description: Reserves a session (the only one we have). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev, + FAR void **session) +#else +static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *) dev; + int ret = OK; + + /* Borrow the APBQ semaphore for thread sync */ + + wm8776_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; + } + + wm8776_givesem(&priv->pendsem); + + return ret; +} + +/**************************************************************************** + * Name: wm8776_release + * + * Description: Releases the session (the only one we have). + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_MULTI_SESSION +static int wm8776_release(FAR struct audio_lowerhalf_s *dev, + FAR void *session) +#else +static int wm8776_release(FAR struct audio_lowerhalf_s *dev) +#endif +{ + FAR struct wm8776_dev_s *priv = (FAR struct wm8776_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 */ + + wm8776_takesem(&priv->pendsem); + + /* Really we should free any queued buffers here */ + + priv->reserved = false; + wm8776_givesem(&priv->pendsem); + + return OK; +} + +/**************************************************************************** + * Name: wm8776_audio_output + * + * Description: + * Initialize and configure the WM8776 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 wm8776_audio_output(FAR struct wm8776_dev_s *priv) +{ + wm8776_writereg(priv, WM8776_MASTER_ATT, WM8776_UPDATE | 0x58); /* -33db */ + wm8776_writereg(priv, WM8776_DAC_IF, 0x32); /* 32bit, I2S, standard pol */ + +#ifdef CONFIG_WM8776_SWAP_HPOUT + wm8776_writereg(priv, WM8776_DAC_CC, 0x62); /* Swap HPOUT L/R */ +#endif + + wm8776_writereg(priv, WM8776_MASTER_MODE, 0x00); /* slave mode, 128fs */ + wm8776_writereg(priv, WM8776_PWR_DOWN, 0x12); /* AINPD, ADCPD */ +} + +/**************************************************************************** + * Name: wm8776_hw_reset + * + * Description: + * Reset and re-initialize the WM8776 + * + * Input Parameters: + * priv - A reference to the driver state structure + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wm8776_hw_reset(FAR struct wm8776_dev_s *priv) +{ + /* Put audio output back to its initial configuration */ + + priv->samprate = WM8776_DEFAULT_SAMPRATE; + priv->nchannels = WM8776_DEFAULT_NCHANNELS; + priv->bpsamp = WM8776_DEFAULT_BPSAMP; +#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) && !defined(CONFIG_AUDIO_EXCLUDE_BALANCE) + priv->balance = b16HALF; /* Center balance */ +#endif + + /* Software reset. This puts all WM8776 registers back in their + * default state. + */ + + wm8776_writereg(priv, WM8776_SOFT_RESET, 0x00); + + /* Configure the WM8776 hardware as an audio input device */ + + wm8776_audio_output(priv); + +} + +/**************************************************************************** + * Name: wm8776_workerthread + * + * This is the thread that feeds data to the chip and keeps the audio + * stream going. + * + ****************************************************************************/ + +static void *wm8776_workerthread(pthread_addr_t pvarg) +{ + FAR struct wm8776_dev_s *priv = (struct wm8776_dev_s *) pvarg; + struct audio_msg_s msg; + FAR struct ap_buffer_s *apb; + int msglen; + int prio; + + audinfo("Entry\n"); + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + priv->terminating = false; +#endif + + priv->running = true; + wm8776_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 WM8776 */ + + wm8776_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)) + { + auderr("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: + audinfo("AUDIO_MSG_DATA_REQUEST\n"); + break; + + /* Stop the playback */ + +#ifndef CONFIG_AUDIO_EXCLUDE_STOP + case AUDIO_MSG_STOP: + /* Indicate that we are terminating */ + + audinfo("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: + audinfo("AUDIO_MSG_ENQUEUE\n"); + break; + + /* We will wake up from the I2S callback with this message */ + + case AUDIO_MSG_COMPLETE: + audinfo("AUDIO_MSG_COMPLETE\n"); + wm8776_returnbuffers(priv); + break; + + default: + auderr("ERROR: Ignoring message ID %d\n", msg.msgId); + break; + } + } + + /* Reset the WM8776 hardware */ + + wm8776_hw_reset(priv); + + /* Return any pending buffers in our pending queue */ + + wm8776_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 + } + + wm8776_givesem(&priv->pendsem); + + /* Return any pending buffers in our done queue */ + + wm8776_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 + + audinfo("Exit\n"); + return NULL; +} + + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wm8776_initialize + * + * Description: + * Initialize the WM8776 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 WM8776 device is returned on + * success; NULL is returned on failure. + * + ****************************************************************************/ + +FAR struct audio_lowerhalf_s * + wm8776_initialize(FAR struct i2c_master_s *i2c, + FAR struct i2s_dev_s *i2s, + FAR const struct wm8776_lower_s *lower) +{ + FAR struct wm8776_dev_s *priv; + + /* Sanity check */ + + DEBUGASSERT(i2c && i2s && lower); + + /* Allocate a WM8776 device structure */ + + priv = (FAR struct wm8776_dev_s *)kmm_zalloc(sizeof(struct wm8776_dev_s)); + + if (priv) + { + priv->dev.ops = &g_audioops; + priv->lower = lower; + priv->i2c = i2c; + priv->i2s = i2s; + + nxsem_init(&priv->pendsem, 0, 1); + dq_init(&priv->pendq); + dq_init(&priv->doneq); + + /* Reset and reconfigure the WM8776 hardwaqre */ + + wm8776_hw_reset(priv); + return &priv->dev; + } + + return NULL; +} diff --git a/drivers/audio/wm8776.h b/drivers/audio/wm8776.h new file mode 100644 index 00000000000..c82e03e23ef --- /dev/null +++ b/drivers/audio/wm8776.h @@ -0,0 +1,128 @@ +/**************************************************************************** + * drivers/audio/wm8776.h + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * Based on drivers/audio/wm8904.h + * + * 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_WM8776_H +#define __DRIVERS_AUDIO_WM8776_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +#include +#include + +#ifdef CONFIG_AUDIO + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define WM8776_MASTER_ATT 0x02 +#define WM8776_DAC_CC 0x07 +#define WM8776_DAC_IF 0x0a +#define WM8776_MASTER_MODE 0x0c +#define WM8776_PWR_DOWN 0x0d +#define WM8776_SOFT_RESET 0x17 + +#define WM8776_DEFAULT_SAMPRATE 44100 /* Initial sample rate */ +#define WM8776_DEFAULT_NCHANNELS 2 /* Initial number of channels */ +#define WM8776_DEFAULT_BPSAMP 16 /* Initial bits per sample */ + +#define WM8776_HPLZCEN (0x1 << 7) +#define WM8776_UPDATE (0x1 << 8) + +#define WM8776_HPOUT_VOL_SHIFT (0) /* Bits 0-6: Headphone output volume */ +#define WM8776_HPOUT_VOL_MASK (0x7f << WM8776_HPOUT_VOL_SHIFT) +# define WM8776_HPOUT_VOL(n) ((uint16_t)(n) << WM8776_HPOUT_VOL_SHIFT) + + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct wm8776_dev_s +{ + /* We are an audio lower half driver (We are also the upper "half" of + * the WM8776 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; /* WM8776 audio lower half (this device) */ + + const FAR struct wm8776_lower_s *lower; /* Pointer to the board lower functions */ + FAR struct i2c_master_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 */ + 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 */ + 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 */ +}; + + +#endif /* CONFIG_AUDIO */ +#endif /* __DRIVERS_AUDIO_WM8776_H */ diff --git a/include/nuttx/audio/wm8776.h b/include/nuttx/audio/wm8776.h new file mode 100644 index 00000000000..1fdf87bf6eb --- /dev/null +++ b/include/nuttx/audio/wm8776.h @@ -0,0 +1,97 @@ +/**************************************************************************** + * include/nuttx/audio/wm8776.h + * + * Copyright (C) 2017 Sony Corporation. All rights reserved. + * Author: Masayuki Ishikawa + * + * 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_NUTTX_AUDIO_WM8776_H +#define __INCLUDE_NUTTX_AUDIO_WM8776_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +#ifdef CONFIG_AUDIO_WM8776 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct wm8776_lower_s; + +struct wm8776_lower_s +{ + /* I2C characterization */ + + uint32_t frequency; /* Initial I2C frequency */ + uint8_t address; /* 7-bit I2C address (only bits 0-6 used) */ +}; + + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +struct i2c_master_s; +struct i2s_dev_s; +struct audio_lowerhalf_s; + +FAR struct audio_lowerhalf_s * + wm8776_initialize(FAR struct i2c_master_s *i2c, + FAR struct i2s_dev_s *i2s, + FAR const struct wm8776_lower_s *lower); + + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_AUDIO_WM8776 */ +#endif /* __INCLUDE_NUTTX_AUDIO_WM8776_H */ diff --git a/libc/audio/lib_buffer.c b/libc/audio/lib_buffer.c index b1406da1c56..8fe2fd51f3e 100644 --- a/libc/audio/lib_buffer.c +++ b/libc/audio/lib_buffer.c @@ -70,6 +70,8 @@ static void apb_semtake(FAR struct ap_buffer_s *apb) { + int ret; + /* Take the semaphore (perhaps waiting) */ while (_SEM_WAIT(&apb->sem) < 0) @@ -79,6 +81,7 @@ static void apb_semtake(FAR struct ap_buffer_s *apb) */ DEBUGASSERT(_SEM_ERRNO(ret) == EINTR); + UNUSED(ret); } }