1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 276c2145dSArnaud Pouliquen /* 376c2145dSArnaud Pouliquen * Copyright (C) STMicroelectronics SA 2015 476c2145dSArnaud Pouliquen * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com> 576c2145dSArnaud Pouliquen * for STMicroelectronics. 676c2145dSArnaud Pouliquen */ 776c2145dSArnaud Pouliquen 876c2145dSArnaud Pouliquen #include <linux/clk.h> 976c2145dSArnaud Pouliquen #include <linux/mfd/syscon.h> 1076c2145dSArnaud Pouliquen 1176c2145dSArnaud Pouliquen #include <sound/asoundef.h> 1276c2145dSArnaud Pouliquen #include <sound/soc.h> 1376c2145dSArnaud Pouliquen 1476c2145dSArnaud Pouliquen #include "uniperif.h" 1576c2145dSArnaud Pouliquen 1676c2145dSArnaud Pouliquen /* 1776c2145dSArnaud Pouliquen * Some hardware-related definitions 1876c2145dSArnaud Pouliquen */ 1976c2145dSArnaud Pouliquen 2076c2145dSArnaud Pouliquen /* sys config registers definitions */ 2176c2145dSArnaud Pouliquen #define SYS_CFG_AUDIO_GLUE 0xA4 2276c2145dSArnaud Pouliquen 2376c2145dSArnaud Pouliquen /* 2476c2145dSArnaud Pouliquen * Driver specific types. 2576c2145dSArnaud Pouliquen */ 2676c2145dSArnaud Pouliquen 27fa050796SArnaud Pouliquen #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999 28fa050796SArnaud Pouliquen #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000 293ee15cacSMoise Gergaud #define UNIPERIF_PLAYER_I2S_OUT 1 /* player id connected to I2S/TDM TX bus */ 30fa050796SArnaud Pouliquen 3176c2145dSArnaud Pouliquen /* 3276c2145dSArnaud Pouliquen * Note: snd_pcm_hardware is linked to DMA controller but is declared here to 3376c2145dSArnaud Pouliquen * integrate DAI_CPU capability in term of rate and supported channels 3476c2145dSArnaud Pouliquen */ 35f2da4542Skbuild test robot static const struct snd_pcm_hardware uni_player_pcm_hw = { 3676c2145dSArnaud Pouliquen .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | 3776c2145dSArnaud Pouliquen SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP | 3876c2145dSArnaud Pouliquen SNDRV_PCM_INFO_MMAP_VALID, 3976c2145dSArnaud Pouliquen .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE, 4076c2145dSArnaud Pouliquen 4176c2145dSArnaud Pouliquen .rates = SNDRV_PCM_RATE_CONTINUOUS, 4276c2145dSArnaud Pouliquen .rate_min = 8000, 4376c2145dSArnaud Pouliquen .rate_max = 192000, 4476c2145dSArnaud Pouliquen 4576c2145dSArnaud Pouliquen .channels_min = 2, 4676c2145dSArnaud Pouliquen .channels_max = 8, 4776c2145dSArnaud Pouliquen 4876c2145dSArnaud Pouliquen .periods_min = 2, 4976c2145dSArnaud Pouliquen .periods_max = 48, 5076c2145dSArnaud Pouliquen 5176c2145dSArnaud Pouliquen .period_bytes_min = 128, 5276c2145dSArnaud Pouliquen .period_bytes_max = 64 * PAGE_SIZE, 5376c2145dSArnaud Pouliquen .buffer_bytes_max = 256 * PAGE_SIZE 5476c2145dSArnaud Pouliquen }; 5576c2145dSArnaud Pouliquen 5676c2145dSArnaud Pouliquen /* 5776c2145dSArnaud Pouliquen * uni_player_irq_handler 5876c2145dSArnaud Pouliquen * In case of error audio stream is stopped; stop action is protected via PCM 5976c2145dSArnaud Pouliquen * stream lock to avoid race condition with trigger callback. 6076c2145dSArnaud Pouliquen */ 6176c2145dSArnaud Pouliquen static irqreturn_t uni_player_irq_handler(int irq, void *dev_id) 6276c2145dSArnaud Pouliquen { 6376c2145dSArnaud Pouliquen irqreturn_t ret = IRQ_NONE; 6476c2145dSArnaud Pouliquen struct uniperif *player = dev_id; 6576c2145dSArnaud Pouliquen unsigned int status; 6676c2145dSArnaud Pouliquen unsigned int tmp; 6776c2145dSArnaud Pouliquen 68d05d862eSArnaud Pouliquen spin_lock(&player->irq_lock); 69d05d862eSArnaud Pouliquen if (!player->substream) 70d05d862eSArnaud Pouliquen goto irq_spin_unlock; 71d05d862eSArnaud Pouliquen 72d05d862eSArnaud Pouliquen snd_pcm_stream_lock(player->substream); 73d05d862eSArnaud Pouliquen if (player->state == UNIPERIF_STATE_STOPPED) 74d05d862eSArnaud Pouliquen goto stream_unlock; 7576c2145dSArnaud Pouliquen 7676c2145dSArnaud Pouliquen /* Get interrupt status & clear them immediately */ 7776c2145dSArnaud Pouliquen status = GET_UNIPERIF_ITS(player); 7876c2145dSArnaud Pouliquen SET_UNIPERIF_ITS_BCLR(player, status); 7976c2145dSArnaud Pouliquen 8076c2145dSArnaud Pouliquen /* Check for fifo error (underrun) */ 8176c2145dSArnaud Pouliquen if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) { 82748abba8SArnaud Pouliquen dev_err(player->dev, "FIFO underflow error detected\n"); 8376c2145dSArnaud Pouliquen 8476c2145dSArnaud Pouliquen /* Interrupt is just for information when underflow recovery */ 855a4326d1SArnaud Pouliquen if (player->underflow_enabled) { 8676c2145dSArnaud Pouliquen /* Update state to underflow */ 8776c2145dSArnaud Pouliquen player->state = UNIPERIF_STATE_UNDERFLOW; 8876c2145dSArnaud Pouliquen 8976c2145dSArnaud Pouliquen } else { 9076c2145dSArnaud Pouliquen /* Disable interrupt so doesn't continually fire */ 9176c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player); 9276c2145dSArnaud Pouliquen 9376c2145dSArnaud Pouliquen /* Stop the player */ 94455c5653STakashi Iwai snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 9576c2145dSArnaud Pouliquen } 9676c2145dSArnaud Pouliquen 9776c2145dSArnaud Pouliquen ret = IRQ_HANDLED; 9876c2145dSArnaud Pouliquen } 9976c2145dSArnaud Pouliquen 10076c2145dSArnaud Pouliquen /* Check for dma error (overrun) */ 10176c2145dSArnaud Pouliquen if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) { 102748abba8SArnaud Pouliquen dev_err(player->dev, "DMA error detected\n"); 10376c2145dSArnaud Pouliquen 10476c2145dSArnaud Pouliquen /* Disable interrupt so doesn't continually fire */ 10576c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player); 10676c2145dSArnaud Pouliquen 10776c2145dSArnaud Pouliquen /* Stop the player */ 108455c5653STakashi Iwai snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 10976c2145dSArnaud Pouliquen 11076c2145dSArnaud Pouliquen ret = IRQ_HANDLED; 11176c2145dSArnaud Pouliquen } 11276c2145dSArnaud Pouliquen 11376c2145dSArnaud Pouliquen /* Check for underflow recovery done */ 11476c2145dSArnaud Pouliquen if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) { 1155a4326d1SArnaud Pouliquen if (!player->underflow_enabled) { 116748abba8SArnaud Pouliquen dev_err(player->dev, 117748abba8SArnaud Pouliquen "unexpected Underflow recovering\n"); 118d05d862eSArnaud Pouliquen ret = -EPERM; 119d05d862eSArnaud Pouliquen goto stream_unlock; 12076c2145dSArnaud Pouliquen } 12176c2145dSArnaud Pouliquen /* Read the underflow recovery duration */ 12276c2145dSArnaud Pouliquen tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player); 123748abba8SArnaud Pouliquen dev_dbg(player->dev, "Underflow recovered (%d LR clocks max)\n", 124748abba8SArnaud Pouliquen tmp); 12576c2145dSArnaud Pouliquen 12676c2145dSArnaud Pouliquen /* Clear the underflow recovery duration */ 12776c2145dSArnaud Pouliquen SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player); 12876c2145dSArnaud Pouliquen 12976c2145dSArnaud Pouliquen /* Update state to started */ 13076c2145dSArnaud Pouliquen player->state = UNIPERIF_STATE_STARTED; 13176c2145dSArnaud Pouliquen 13276c2145dSArnaud Pouliquen ret = IRQ_HANDLED; 13376c2145dSArnaud Pouliquen } 13476c2145dSArnaud Pouliquen 13576c2145dSArnaud Pouliquen /* Check if underflow recovery failed */ 13676c2145dSArnaud Pouliquen if (unlikely(status & 13776c2145dSArnaud Pouliquen UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) { 138748abba8SArnaud Pouliquen dev_err(player->dev, "Underflow recovery failed\n"); 13976c2145dSArnaud Pouliquen 14076c2145dSArnaud Pouliquen /* Stop the player */ 141455c5653STakashi Iwai snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 14276c2145dSArnaud Pouliquen 14376c2145dSArnaud Pouliquen ret = IRQ_HANDLED; 14476c2145dSArnaud Pouliquen } 14576c2145dSArnaud Pouliquen 146d05d862eSArnaud Pouliquen stream_unlock: 147d05d862eSArnaud Pouliquen snd_pcm_stream_unlock(player->substream); 148d05d862eSArnaud Pouliquen irq_spin_unlock: 149d05d862eSArnaud Pouliquen spin_unlock(&player->irq_lock); 150d05d862eSArnaud Pouliquen 15176c2145dSArnaud Pouliquen return ret; 15276c2145dSArnaud Pouliquen } 15376c2145dSArnaud Pouliquen 154f2da4542Skbuild test robot static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate) 155fa050796SArnaud Pouliquen { 156fa050796SArnaud Pouliquen int rate_adjusted, rate_achieved, delta, ret; 157fa050796SArnaud Pouliquen int adjustment = player->clk_adj; 158fa050796SArnaud Pouliquen 159fa050796SArnaud Pouliquen /* 160fa050796SArnaud Pouliquen * a 161fa050796SArnaud Pouliquen * F = f + --------- * f = f + d 162fa050796SArnaud Pouliquen * 1000000 163fa050796SArnaud Pouliquen * 164fa050796SArnaud Pouliquen * a 165fa050796SArnaud Pouliquen * d = --------- * f 166fa050796SArnaud Pouliquen * 1000000 167fa050796SArnaud Pouliquen * 168fa050796SArnaud Pouliquen * where: 169fa050796SArnaud Pouliquen * f - nominal rate 170fa050796SArnaud Pouliquen * a - adjustment in ppm (parts per milion) 171fa050796SArnaud Pouliquen * F - rate to be set in synthesizer 172fa050796SArnaud Pouliquen * d - delta (difference) between f and F 173fa050796SArnaud Pouliquen */ 174fa050796SArnaud Pouliquen if (adjustment < 0) { 175fa050796SArnaud Pouliquen /* div64_64 operates on unsigned values... */ 176fa050796SArnaud Pouliquen delta = -1; 177fa050796SArnaud Pouliquen adjustment = -adjustment; 178fa050796SArnaud Pouliquen } else { 179fa050796SArnaud Pouliquen delta = 1; 180fa050796SArnaud Pouliquen } 181fa050796SArnaud Pouliquen /* 500000 ppm is 0.5, which is used to round up values */ 182fa050796SArnaud Pouliquen delta *= (int)div64_u64((uint64_t)rate * 183fa050796SArnaud Pouliquen (uint64_t)adjustment + 500000, 1000000); 184fa050796SArnaud Pouliquen rate_adjusted = rate + delta; 185fa050796SArnaud Pouliquen 186fa050796SArnaud Pouliquen /* Adjusted rate should never be == 0 */ 187fa050796SArnaud Pouliquen if (!rate_adjusted) 188fa050796SArnaud Pouliquen return -EINVAL; 189fa050796SArnaud Pouliquen 190fa050796SArnaud Pouliquen ret = clk_set_rate(player->clk, rate_adjusted); 191fa050796SArnaud Pouliquen if (ret < 0) 192fa050796SArnaud Pouliquen return ret; 193fa050796SArnaud Pouliquen 194fa050796SArnaud Pouliquen rate_achieved = clk_get_rate(player->clk); 195fa050796SArnaud Pouliquen if (!rate_achieved) 196fa050796SArnaud Pouliquen /* If value is 0 means that clock or parent not valid */ 197fa050796SArnaud Pouliquen return -EINVAL; 198fa050796SArnaud Pouliquen 199fa050796SArnaud Pouliquen /* 200fa050796SArnaud Pouliquen * Using ALSA's adjustment control, we can modify the rate to be up 201fa050796SArnaud Pouliquen * to twice as much as requested, but no more 202fa050796SArnaud Pouliquen */ 203fa050796SArnaud Pouliquen delta = rate_achieved - rate; 204fa050796SArnaud Pouliquen if (delta < 0) { 205fa050796SArnaud Pouliquen /* div64_64 operates on unsigned values... */ 206fa050796SArnaud Pouliquen delta = -delta; 207fa050796SArnaud Pouliquen adjustment = -1; 208fa050796SArnaud Pouliquen } else { 209fa050796SArnaud Pouliquen adjustment = 1; 210fa050796SArnaud Pouliquen } 211fa050796SArnaud Pouliquen /* Frequency/2 is added to round up result */ 212fa050796SArnaud Pouliquen adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2, 213fa050796SArnaud Pouliquen rate); 214fa050796SArnaud Pouliquen player->clk_adj = adjustment; 215fa050796SArnaud Pouliquen return 0; 216fa050796SArnaud Pouliquen } 217fa050796SArnaud Pouliquen 21876c2145dSArnaud Pouliquen static void uni_player_set_channel_status(struct uniperif *player, 21976c2145dSArnaud Pouliquen struct snd_pcm_runtime *runtime) 22076c2145dSArnaud Pouliquen { 22176c2145dSArnaud Pouliquen int n; 22276c2145dSArnaud Pouliquen unsigned int status; 22376c2145dSArnaud Pouliquen 22476c2145dSArnaud Pouliquen /* 22576c2145dSArnaud Pouliquen * Some AVRs and TVs require the channel status to contain a correct 22676c2145dSArnaud Pouliquen * sampling frequency. If no sample rate is already specified, then 22776c2145dSArnaud Pouliquen * set one. 22876c2145dSArnaud Pouliquen */ 2290d3f3c9aSMoise Gergaud if (runtime) { 23076c2145dSArnaud Pouliquen switch (runtime->rate) { 23176c2145dSArnaud Pouliquen case 22050: 23276c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 23376c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_22050; 23476c2145dSArnaud Pouliquen break; 23576c2145dSArnaud Pouliquen case 44100: 23676c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 23776c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_44100; 23876c2145dSArnaud Pouliquen break; 23976c2145dSArnaud Pouliquen case 88200: 24076c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 24176c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_88200; 24276c2145dSArnaud Pouliquen break; 24376c2145dSArnaud Pouliquen case 176400: 24476c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 24576c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_176400; 24676c2145dSArnaud Pouliquen break; 24776c2145dSArnaud Pouliquen case 24000: 24876c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 24976c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_24000; 25076c2145dSArnaud Pouliquen break; 25176c2145dSArnaud Pouliquen case 48000: 25276c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 25376c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_48000; 25476c2145dSArnaud Pouliquen break; 25576c2145dSArnaud Pouliquen case 96000: 25676c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 25776c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_96000; 25876c2145dSArnaud Pouliquen break; 25976c2145dSArnaud Pouliquen case 192000: 26076c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 26176c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_192000; 26276c2145dSArnaud Pouliquen break; 26376c2145dSArnaud Pouliquen case 32000: 26476c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 26576c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_32000; 26676c2145dSArnaud Pouliquen break; 26776c2145dSArnaud Pouliquen default: 26876c2145dSArnaud Pouliquen /* Mark as sampling frequency not indicated */ 26976c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 27076c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_NOTID; 27176c2145dSArnaud Pouliquen break; 27276c2145dSArnaud Pouliquen } 27376c2145dSArnaud Pouliquen } 27476c2145dSArnaud Pouliquen 27576c2145dSArnaud Pouliquen /* Audio mode: 27676c2145dSArnaud Pouliquen * Use audio mode status to select PCM or encoded mode 27776c2145dSArnaud Pouliquen */ 27876c2145dSArnaud Pouliquen if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO) 27976c2145dSArnaud Pouliquen player->stream_settings.encoding_mode = 28076c2145dSArnaud Pouliquen UNIPERIF_IEC958_ENCODING_MODE_ENCODED; 28176c2145dSArnaud Pouliquen else 28276c2145dSArnaud Pouliquen player->stream_settings.encoding_mode = 28376c2145dSArnaud Pouliquen UNIPERIF_IEC958_ENCODING_MODE_PCM; 28476c2145dSArnaud Pouliquen 28576c2145dSArnaud Pouliquen if (player->stream_settings.encoding_mode == 28676c2145dSArnaud Pouliquen UNIPERIF_IEC958_ENCODING_MODE_PCM) 28776c2145dSArnaud Pouliquen /* Clear user validity bits */ 28876c2145dSArnaud Pouliquen SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 28976c2145dSArnaud Pouliquen else 29076c2145dSArnaud Pouliquen /* Set user validity bits */ 29176c2145dSArnaud Pouliquen SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1); 29276c2145dSArnaud Pouliquen 29376c2145dSArnaud Pouliquen /* Program the new channel status */ 29476c2145dSArnaud Pouliquen for (n = 0; n < 6; ++n) { 29576c2145dSArnaud Pouliquen status = 29676c2145dSArnaud Pouliquen player->stream_settings.iec958.status[0 + (n * 4)] & 0xf; 29776c2145dSArnaud Pouliquen status |= 29876c2145dSArnaud Pouliquen player->stream_settings.iec958.status[1 + (n * 4)] << 8; 29976c2145dSArnaud Pouliquen status |= 30076c2145dSArnaud Pouliquen player->stream_settings.iec958.status[2 + (n * 4)] << 16; 30176c2145dSArnaud Pouliquen status |= 30276c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3 + (n * 4)] << 24; 30376c2145dSArnaud Pouliquen SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status); 30476c2145dSArnaud Pouliquen } 30576c2145dSArnaud Pouliquen 30676c2145dSArnaud Pouliquen /* Update the channel status */ 30776c2145dSArnaud Pouliquen if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 30876c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player); 30976c2145dSArnaud Pouliquen else 31076c2145dSArnaud Pouliquen SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player); 31176c2145dSArnaud Pouliquen } 31276c2145dSArnaud Pouliquen 31376c2145dSArnaud Pouliquen static int uni_player_prepare_iec958(struct uniperif *player, 31476c2145dSArnaud Pouliquen struct snd_pcm_runtime *runtime) 31576c2145dSArnaud Pouliquen { 31676c2145dSArnaud Pouliquen int clk_div; 31776c2145dSArnaud Pouliquen 31876c2145dSArnaud Pouliquen clk_div = player->mclk / runtime->rate; 31976c2145dSArnaud Pouliquen 32076c2145dSArnaud Pouliquen /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */ 32176c2145dSArnaud Pouliquen if ((clk_div % 128) || (clk_div <= 0)) { 322748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid clk_div %d\n", 32376c2145dSArnaud Pouliquen __func__, clk_div); 32476c2145dSArnaud Pouliquen return -EINVAL; 32576c2145dSArnaud Pouliquen } 32676c2145dSArnaud Pouliquen 32776c2145dSArnaud Pouliquen switch (runtime->format) { 32876c2145dSArnaud Pouliquen case SNDRV_PCM_FORMAT_S16_LE: 32976c2145dSArnaud Pouliquen /* 16/16 memory format */ 33076c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 33176c2145dSArnaud Pouliquen /* 16-bits per sub-frame */ 33276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NBIT_32(player); 33376c2145dSArnaud Pouliquen /* Set 16-bit sample precision */ 33476c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 33576c2145dSArnaud Pouliquen break; 33676c2145dSArnaud Pouliquen case SNDRV_PCM_FORMAT_S32_LE: 33776c2145dSArnaud Pouliquen /* 16/0 memory format */ 33876c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 33976c2145dSArnaud Pouliquen /* 32-bits per sub-frame */ 34076c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NBIT_32(player); 34176c2145dSArnaud Pouliquen /* Set 24-bit sample precision */ 34276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player); 34376c2145dSArnaud Pouliquen break; 34476c2145dSArnaud Pouliquen default: 345748abba8SArnaud Pouliquen dev_err(player->dev, "format not supported\n"); 34676c2145dSArnaud Pouliquen return -EINVAL; 34776c2145dSArnaud Pouliquen } 34876c2145dSArnaud Pouliquen 34976c2145dSArnaud Pouliquen /* Set parity to be calculated by the hardware */ 35076c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player); 35176c2145dSArnaud Pouliquen 35276c2145dSArnaud Pouliquen /* Set channel status bits to be inserted by the hardware */ 35376c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player); 35476c2145dSArnaud Pouliquen 35576c2145dSArnaud Pouliquen /* Set user data bits to be inserted by the hardware */ 35676c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player); 35776c2145dSArnaud Pouliquen 35876c2145dSArnaud Pouliquen /* Set validity bits to be inserted by the hardware */ 35976c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player); 36076c2145dSArnaud Pouliquen 36176c2145dSArnaud Pouliquen /* Set full software control to disabled */ 36276c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player); 36376c2145dSArnaud Pouliquen 36476c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player); 36576c2145dSArnaud Pouliquen 366ce780a47SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 36776c2145dSArnaud Pouliquen /* Update the channel status */ 36876c2145dSArnaud Pouliquen uni_player_set_channel_status(player, runtime); 369ce780a47SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 37076c2145dSArnaud Pouliquen 37176c2145dSArnaud Pouliquen /* Clear the user validity user bits */ 37276c2145dSArnaud Pouliquen SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 37376c2145dSArnaud Pouliquen 37476c2145dSArnaud Pouliquen /* Disable one-bit audio mode */ 37576c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 37676c2145dSArnaud Pouliquen 37776c2145dSArnaud Pouliquen /* Enable consecutive frames repetition of Z preamble (not for HBRA) */ 37876c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player); 37976c2145dSArnaud Pouliquen 38076c2145dSArnaud Pouliquen /* Change to SUF0_SUBF1 and left/right channels swap! */ 38176c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player); 38276c2145dSArnaud Pouliquen 38376c2145dSArnaud Pouliquen /* Set data output as MSB first */ 38476c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 38576c2145dSArnaud Pouliquen 38676c2145dSArnaud Pouliquen if (player->stream_settings.encoding_mode == 38776c2145dSArnaud Pouliquen UNIPERIF_IEC958_ENCODING_MODE_ENCODED) 38876c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player); 38976c2145dSArnaud Pouliquen else 39076c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player); 39176c2145dSArnaud Pouliquen 39276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 39376c2145dSArnaud Pouliquen 39476c2145dSArnaud Pouliquen /* Set rounding to off */ 39576c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 39676c2145dSArnaud Pouliquen 39776c2145dSArnaud Pouliquen /* Set clock divisor */ 39876c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128); 39976c2145dSArnaud Pouliquen 40076c2145dSArnaud Pouliquen /* Set the spdif latency to not wait before starting player */ 40176c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 40276c2145dSArnaud Pouliquen 40376c2145dSArnaud Pouliquen /* 40476c2145dSArnaud Pouliquen * Ensure iec958 formatting is off. It will be enabled in function 40576c2145dSArnaud Pouliquen * uni_player_start() at the same time as the operation 40676c2145dSArnaud Pouliquen * mode is set to work around a silicon issue. 40776c2145dSArnaud Pouliquen */ 40876c2145dSArnaud Pouliquen if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 40976c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 41076c2145dSArnaud Pouliquen else 41176c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player); 41276c2145dSArnaud Pouliquen 41376c2145dSArnaud Pouliquen return 0; 41476c2145dSArnaud Pouliquen } 41576c2145dSArnaud Pouliquen 41676c2145dSArnaud Pouliquen static int uni_player_prepare_pcm(struct uniperif *player, 41776c2145dSArnaud Pouliquen struct snd_pcm_runtime *runtime) 41876c2145dSArnaud Pouliquen { 41976c2145dSArnaud Pouliquen int output_frame_size, slot_width, clk_div; 42076c2145dSArnaud Pouliquen 42176c2145dSArnaud Pouliquen /* Force slot width to 32 in I2S mode (HW constraint) */ 42276c2145dSArnaud Pouliquen if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == 4238d8b1e2eSMoise Gergaud SND_SOC_DAIFMT_I2S) 42476c2145dSArnaud Pouliquen slot_width = 32; 4258d8b1e2eSMoise Gergaud else 4268d8b1e2eSMoise Gergaud slot_width = snd_pcm_format_width(runtime->format); 4278d8b1e2eSMoise Gergaud 42876c2145dSArnaud Pouliquen output_frame_size = slot_width * runtime->channels; 42976c2145dSArnaud Pouliquen 43076c2145dSArnaud Pouliquen clk_div = player->mclk / runtime->rate; 43176c2145dSArnaud Pouliquen /* 43276c2145dSArnaud Pouliquen * For 32 bits subframe clk_div must be a multiple of 128, 43376c2145dSArnaud Pouliquen * for 16 bits must be a multiple of 64 43476c2145dSArnaud Pouliquen */ 43576c2145dSArnaud Pouliquen if ((slot_width == 32) && (clk_div % 128)) { 436748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid clk_div\n", __func__); 43776c2145dSArnaud Pouliquen return -EINVAL; 43876c2145dSArnaud Pouliquen } 43976c2145dSArnaud Pouliquen 44076c2145dSArnaud Pouliquen if ((slot_width == 16) && (clk_div % 64)) { 441748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid clk_div\n", __func__); 44276c2145dSArnaud Pouliquen return -EINVAL; 44376c2145dSArnaud Pouliquen } 44476c2145dSArnaud Pouliquen 44576c2145dSArnaud Pouliquen /* 44676c2145dSArnaud Pouliquen * Number of bits per subframe (which is one channel sample) 44776c2145dSArnaud Pouliquen * on output - Transfer 16 or 32 bits from FIFO 44876c2145dSArnaud Pouliquen */ 44976c2145dSArnaud Pouliquen switch (slot_width) { 45076c2145dSArnaud Pouliquen case 32: 45176c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NBIT_32(player); 45276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player); 45376c2145dSArnaud Pouliquen break; 45476c2145dSArnaud Pouliquen case 16: 45576c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NBIT_16(player); 45676c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 45776c2145dSArnaud Pouliquen break; 45876c2145dSArnaud Pouliquen default: 459748abba8SArnaud Pouliquen dev_err(player->dev, "subframe format not supported\n"); 46076c2145dSArnaud Pouliquen return -EINVAL; 46176c2145dSArnaud Pouliquen } 46276c2145dSArnaud Pouliquen 46376c2145dSArnaud Pouliquen /* Configure data memory format */ 46476c2145dSArnaud Pouliquen switch (runtime->format) { 46576c2145dSArnaud Pouliquen case SNDRV_PCM_FORMAT_S16_LE: 46676c2145dSArnaud Pouliquen /* One data word contains two samples */ 46776c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 46876c2145dSArnaud Pouliquen break; 46976c2145dSArnaud Pouliquen 47076c2145dSArnaud Pouliquen case SNDRV_PCM_FORMAT_S32_LE: 47176c2145dSArnaud Pouliquen /* 47276c2145dSArnaud Pouliquen * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits 47376c2145dSArnaud Pouliquen * on the left than zeros (if less than 32 bytes)"... ;-) 47476c2145dSArnaud Pouliquen */ 47576c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 47676c2145dSArnaud Pouliquen break; 47776c2145dSArnaud Pouliquen 47876c2145dSArnaud Pouliquen default: 479748abba8SArnaud Pouliquen dev_err(player->dev, "format not supported\n"); 48076c2145dSArnaud Pouliquen return -EINVAL; 48176c2145dSArnaud Pouliquen } 48276c2145dSArnaud Pouliquen 48376c2145dSArnaud Pouliquen /* Set rounding to off */ 48476c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 48576c2145dSArnaud Pouliquen 48676c2145dSArnaud Pouliquen /* Set clock divisor */ 48776c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size)); 48876c2145dSArnaud Pouliquen 48976c2145dSArnaud Pouliquen /* Number of channelsmust be even*/ 49076c2145dSArnaud Pouliquen if ((runtime->channels % 2) || (runtime->channels < 2) || 49176c2145dSArnaud Pouliquen (runtime->channels > 10)) { 492748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid nb of channels\n", __func__); 49376c2145dSArnaud Pouliquen return -EINVAL; 49476c2145dSArnaud Pouliquen } 49576c2145dSArnaud Pouliquen 49676c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 49776c2145dSArnaud Pouliquen 49876c2145dSArnaud Pouliquen /* Set 1-bit audio format to disabled */ 49976c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 50076c2145dSArnaud Pouliquen 50176c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 50276c2145dSArnaud Pouliquen 50376c2145dSArnaud Pouliquen /* No iec958 formatting as outputting to DAC */ 50476c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 50576c2145dSArnaud Pouliquen 50676c2145dSArnaud Pouliquen return 0; 50776c2145dSArnaud Pouliquen } 50876c2145dSArnaud Pouliquen 5098d8b1e2eSMoise Gergaud static int uni_player_prepare_tdm(struct uniperif *player, 5108d8b1e2eSMoise Gergaud struct snd_pcm_runtime *runtime) 5118d8b1e2eSMoise Gergaud { 5128d8b1e2eSMoise Gergaud int tdm_frame_size; /* unip tdm frame size in bytes */ 5138d8b1e2eSMoise Gergaud int user_frame_size; /* user tdm frame size in bytes */ 5148d8b1e2eSMoise Gergaud /* default unip TDM_WORD_POS_X_Y */ 5158d8b1e2eSMoise Gergaud unsigned int word_pos[4] = { 5168d8b1e2eSMoise Gergaud 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A}; 5178d8b1e2eSMoise Gergaud int freq, ret; 5188d8b1e2eSMoise Gergaud 5198d8b1e2eSMoise Gergaud tdm_frame_size = 5208d8b1e2eSMoise Gergaud sti_uniperiph_get_unip_tdm_frame_size(player); 5218d8b1e2eSMoise Gergaud user_frame_size = 5228d8b1e2eSMoise Gergaud sti_uniperiph_get_user_frame_size(runtime); 5238d8b1e2eSMoise Gergaud 5248d8b1e2eSMoise Gergaud /* fix 16/0 format */ 5258d8b1e2eSMoise Gergaud SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 5268d8b1e2eSMoise Gergaud SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player); 5278d8b1e2eSMoise Gergaud 5288d8b1e2eSMoise Gergaud /* number of words inserted on the TDM line */ 5298d8b1e2eSMoise Gergaud SET_UNIPERIF_I2S_FMT_NUM_CH(player, user_frame_size / 4 / 2); 5308d8b1e2eSMoise Gergaud 5318d8b1e2eSMoise Gergaud SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 5328d8b1e2eSMoise Gergaud SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player); 5338d8b1e2eSMoise Gergaud 5348d8b1e2eSMoise Gergaud /* Enable the tdm functionality */ 5358d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player); 5368d8b1e2eSMoise Gergaud 5378d8b1e2eSMoise Gergaud /* number of 8 bits timeslots avail in unip tdm frame */ 5388d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player, tdm_frame_size); 5398d8b1e2eSMoise Gergaud 5408d8b1e2eSMoise Gergaud /* set the timeslot allocation for words in FIFO */ 5418d8b1e2eSMoise Gergaud sti_uniperiph_get_tdm_word_pos(player, word_pos); 5428d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_WORD_POS(player, 1_2, word_pos[WORD_1_2]); 5438d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_WORD_POS(player, 3_4, word_pos[WORD_3_4]); 5448d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_WORD_POS(player, 5_6, word_pos[WORD_5_6]); 5458d8b1e2eSMoise Gergaud SET_UNIPERIF_TDM_WORD_POS(player, 7_8, word_pos[WORD_7_8]); 5468d8b1e2eSMoise Gergaud 5478d8b1e2eSMoise Gergaud /* set unip clk rate (not done vai set_sysclk ops) */ 5488d8b1e2eSMoise Gergaud freq = runtime->rate * tdm_frame_size * 8; 5498d8b1e2eSMoise Gergaud mutex_lock(&player->ctrl_lock); 5508d8b1e2eSMoise Gergaud ret = uni_player_clk_set_rate(player, freq); 5518d8b1e2eSMoise Gergaud if (!ret) 5528d8b1e2eSMoise Gergaud player->mclk = freq; 5538d8b1e2eSMoise Gergaud mutex_unlock(&player->ctrl_lock); 5548d8b1e2eSMoise Gergaud 5558d8b1e2eSMoise Gergaud return 0; 5568d8b1e2eSMoise Gergaud } 5578d8b1e2eSMoise Gergaud 558fa050796SArnaud Pouliquen /* 55936cc0935SArnaud Pouliquen * ALSA uniperipheral iec958 controls 56036cc0935SArnaud Pouliquen */ 56136cc0935SArnaud Pouliquen static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol, 56236cc0935SArnaud Pouliquen struct snd_ctl_elem_info *uinfo) 56336cc0935SArnaud Pouliquen { 56436cc0935SArnaud Pouliquen uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 56536cc0935SArnaud Pouliquen uinfo->count = 1; 56636cc0935SArnaud Pouliquen 56736cc0935SArnaud Pouliquen return 0; 56836cc0935SArnaud Pouliquen } 56936cc0935SArnaud Pouliquen 57036cc0935SArnaud Pouliquen static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol, 57136cc0935SArnaud Pouliquen struct snd_ctl_elem_value *ucontrol) 57236cc0935SArnaud Pouliquen { 57336cc0935SArnaud Pouliquen struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 57436cc0935SArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 57536cc0935SArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 57636cc0935SArnaud Pouliquen struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 57736cc0935SArnaud Pouliquen 57836cc0935SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 57936cc0935SArnaud Pouliquen ucontrol->value.iec958.status[0] = iec958->status[0]; 58036cc0935SArnaud Pouliquen ucontrol->value.iec958.status[1] = iec958->status[1]; 58136cc0935SArnaud Pouliquen ucontrol->value.iec958.status[2] = iec958->status[2]; 58236cc0935SArnaud Pouliquen ucontrol->value.iec958.status[3] = iec958->status[3]; 58336cc0935SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 58436cc0935SArnaud Pouliquen return 0; 58536cc0935SArnaud Pouliquen } 58636cc0935SArnaud Pouliquen 58736cc0935SArnaud Pouliquen static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol, 58836cc0935SArnaud Pouliquen struct snd_ctl_elem_value *ucontrol) 58936cc0935SArnaud Pouliquen { 59036cc0935SArnaud Pouliquen struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 59136cc0935SArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 59236cc0935SArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 59336cc0935SArnaud Pouliquen struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 594d05d862eSArnaud Pouliquen unsigned long flags; 59536cc0935SArnaud Pouliquen 59636cc0935SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 59736cc0935SArnaud Pouliquen iec958->status[0] = ucontrol->value.iec958.status[0]; 59836cc0935SArnaud Pouliquen iec958->status[1] = ucontrol->value.iec958.status[1]; 59936cc0935SArnaud Pouliquen iec958->status[2] = ucontrol->value.iec958.status[2]; 60036cc0935SArnaud Pouliquen iec958->status[3] = ucontrol->value.iec958.status[3]; 60136cc0935SArnaud Pouliquen 602d05d862eSArnaud Pouliquen spin_lock_irqsave(&player->irq_lock, flags); 6031e6d3044SArnaud Pouliquen if (player->substream && player->substream->runtime) 6041e6d3044SArnaud Pouliquen uni_player_set_channel_status(player, 6051e6d3044SArnaud Pouliquen player->substream->runtime); 6061e6d3044SArnaud Pouliquen else 60736cc0935SArnaud Pouliquen uni_player_set_channel_status(player, NULL); 60836cc0935SArnaud Pouliquen 609d05d862eSArnaud Pouliquen spin_unlock_irqrestore(&player->irq_lock, flags); 610ce780a47SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 611ce780a47SArnaud Pouliquen 61236cc0935SArnaud Pouliquen return 0; 61336cc0935SArnaud Pouliquen } 61436cc0935SArnaud Pouliquen 61536cc0935SArnaud Pouliquen static struct snd_kcontrol_new uni_player_iec958_ctl = { 61636cc0935SArnaud Pouliquen .iface = SNDRV_CTL_ELEM_IFACE_PCM, 61736cc0935SArnaud Pouliquen .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 61836cc0935SArnaud Pouliquen .info = uni_player_ctl_iec958_info, 61936cc0935SArnaud Pouliquen .get = uni_player_ctl_iec958_get, 62036cc0935SArnaud Pouliquen .put = uni_player_ctl_iec958_put, 62136cc0935SArnaud Pouliquen }; 62236cc0935SArnaud Pouliquen 62336cc0935SArnaud Pouliquen /* 624fa050796SArnaud Pouliquen * uniperif rate adjustement control 625fa050796SArnaud Pouliquen */ 626fa050796SArnaud Pouliquen static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol, 627fa050796SArnaud Pouliquen struct snd_ctl_elem_info *uinfo) 628fa050796SArnaud Pouliquen { 629fa050796SArnaud Pouliquen uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 630fa050796SArnaud Pouliquen uinfo->count = 1; 631fa050796SArnaud Pouliquen uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN; 632fa050796SArnaud Pouliquen uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX; 633fa050796SArnaud Pouliquen uinfo->value.integer.step = 1; 634fa050796SArnaud Pouliquen 635fa050796SArnaud Pouliquen return 0; 636fa050796SArnaud Pouliquen } 637fa050796SArnaud Pouliquen 638fa050796SArnaud Pouliquen static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol, 639fa050796SArnaud Pouliquen struct snd_ctl_elem_value *ucontrol) 640fa050796SArnaud Pouliquen { 641fa050796SArnaud Pouliquen struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 642fa050796SArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 643fa050796SArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 644fa050796SArnaud Pouliquen 64536cc0935SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 646fa050796SArnaud Pouliquen ucontrol->value.integer.value[0] = player->clk_adj; 64736cc0935SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 648fa050796SArnaud Pouliquen 649fa050796SArnaud Pouliquen return 0; 650fa050796SArnaud Pouliquen } 651fa050796SArnaud Pouliquen 652fa050796SArnaud Pouliquen static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol, 653fa050796SArnaud Pouliquen struct snd_ctl_elem_value *ucontrol) 654fa050796SArnaud Pouliquen { 655fa050796SArnaud Pouliquen struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 656fa050796SArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 657fa050796SArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 658fa050796SArnaud Pouliquen int ret = 0; 659fa050796SArnaud Pouliquen 660fa050796SArnaud Pouliquen if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) || 661fa050796SArnaud Pouliquen (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX)) 662fa050796SArnaud Pouliquen return -EINVAL; 663fa050796SArnaud Pouliquen 664fa050796SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 665fa050796SArnaud Pouliquen player->clk_adj = ucontrol->value.integer.value[0]; 666fa050796SArnaud Pouliquen 667fa050796SArnaud Pouliquen if (player->mclk) 668fa050796SArnaud Pouliquen ret = uni_player_clk_set_rate(player, player->mclk); 669fa050796SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 670fa050796SArnaud Pouliquen 671fa050796SArnaud Pouliquen return ret; 672fa050796SArnaud Pouliquen } 673fa050796SArnaud Pouliquen 674fa050796SArnaud Pouliquen static struct snd_kcontrol_new uni_player_clk_adj_ctl = { 675fa050796SArnaud Pouliquen .iface = SNDRV_CTL_ELEM_IFACE_PCM, 676fa050796SArnaud Pouliquen .name = "PCM Playback Oversampling Freq. Adjustment", 677fa050796SArnaud Pouliquen .info = snd_sti_clk_adjustment_info, 678fa050796SArnaud Pouliquen .get = snd_sti_clk_adjustment_get, 679fa050796SArnaud Pouliquen .put = snd_sti_clk_adjustment_put, 680fa050796SArnaud Pouliquen }; 681fa050796SArnaud Pouliquen 68236cc0935SArnaud Pouliquen static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = { 68336cc0935SArnaud Pouliquen &uni_player_clk_adj_ctl, 68436cc0935SArnaud Pouliquen }; 68536cc0935SArnaud Pouliquen 68636cc0935SArnaud Pouliquen static struct snd_kcontrol_new *snd_sti_iec_ctl[] = { 68736cc0935SArnaud Pouliquen &uni_player_iec958_ctl, 688fa050796SArnaud Pouliquen &uni_player_clk_adj_ctl, 689fa050796SArnaud Pouliquen }; 690fa050796SArnaud Pouliquen 691fa050796SArnaud Pouliquen static int uni_player_startup(struct snd_pcm_substream *substream, 692fa050796SArnaud Pouliquen struct snd_soc_dai *dai) 693fa050796SArnaud Pouliquen { 694fa050796SArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 695fa050796SArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 696d05d862eSArnaud Pouliquen unsigned long flags; 6978d8b1e2eSMoise Gergaud int ret; 6988d8b1e2eSMoise Gergaud 699d05d862eSArnaud Pouliquen spin_lock_irqsave(&player->irq_lock, flags); 70036a65e20SMoise Gergaud player->substream = substream; 701d05d862eSArnaud Pouliquen spin_unlock_irqrestore(&player->irq_lock, flags); 702fa050796SArnaud Pouliquen 703fa050796SArnaud Pouliquen player->clk_adj = 0; 704fa050796SArnaud Pouliquen 7058d8b1e2eSMoise Gergaud if (!UNIPERIF_TYPE_IS_TDM(player)) 706fa050796SArnaud Pouliquen return 0; 7078d8b1e2eSMoise Gergaud 7088d8b1e2eSMoise Gergaud /* refine hw constraint in tdm mode */ 7098d8b1e2eSMoise Gergaud ret = snd_pcm_hw_rule_add(substream->runtime, 0, 7108d8b1e2eSMoise Gergaud SNDRV_PCM_HW_PARAM_CHANNELS, 7118d8b1e2eSMoise Gergaud sti_uniperiph_fix_tdm_chan, 7128d8b1e2eSMoise Gergaud player, SNDRV_PCM_HW_PARAM_CHANNELS, 7138d8b1e2eSMoise Gergaud -1); 7148d8b1e2eSMoise Gergaud if (ret < 0) 7158d8b1e2eSMoise Gergaud return ret; 7168d8b1e2eSMoise Gergaud 7178d8b1e2eSMoise Gergaud return snd_pcm_hw_rule_add(substream->runtime, 0, 7188d8b1e2eSMoise Gergaud SNDRV_PCM_HW_PARAM_FORMAT, 7198d8b1e2eSMoise Gergaud sti_uniperiph_fix_tdm_format, 7208d8b1e2eSMoise Gergaud player, SNDRV_PCM_HW_PARAM_FORMAT, 7218d8b1e2eSMoise Gergaud -1); 722fa050796SArnaud Pouliquen } 723fa050796SArnaud Pouliquen 72476c2145dSArnaud Pouliquen static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id, 72576c2145dSArnaud Pouliquen unsigned int freq, int dir) 72676c2145dSArnaud Pouliquen { 72776c2145dSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 72876c2145dSArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 729ed6c75f2SArnaud Pouliquen int ret; 73076c2145dSArnaud Pouliquen 7318d8b1e2eSMoise Gergaud if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN)) 73276c2145dSArnaud Pouliquen return 0; 73376c2145dSArnaud Pouliquen 73476c2145dSArnaud Pouliquen if (clk_id != 0) 73576c2145dSArnaud Pouliquen return -EINVAL; 73676c2145dSArnaud Pouliquen 737fa050796SArnaud Pouliquen mutex_lock(&player->ctrl_lock); 738fa050796SArnaud Pouliquen ret = uni_player_clk_set_rate(player, freq); 739ed6c75f2SArnaud Pouliquen if (!ret) 74076c2145dSArnaud Pouliquen player->mclk = freq; 741fa050796SArnaud Pouliquen mutex_unlock(&player->ctrl_lock); 74276c2145dSArnaud Pouliquen 743ed6c75f2SArnaud Pouliquen return ret; 74476c2145dSArnaud Pouliquen } 74576c2145dSArnaud Pouliquen 74676c2145dSArnaud Pouliquen static int uni_player_prepare(struct snd_pcm_substream *substream, 74776c2145dSArnaud Pouliquen struct snd_soc_dai *dai) 74876c2145dSArnaud Pouliquen { 74976c2145dSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 75076c2145dSArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 75176c2145dSArnaud Pouliquen struct snd_pcm_runtime *runtime = substream->runtime; 75276c2145dSArnaud Pouliquen int transfer_size, trigger_limit; 75376c2145dSArnaud Pouliquen int ret; 75476c2145dSArnaud Pouliquen 75576c2145dSArnaud Pouliquen /* The player should be stopped */ 75676c2145dSArnaud Pouliquen if (player->state != UNIPERIF_STATE_STOPPED) { 757748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid player state %d\n", __func__, 75876c2145dSArnaud Pouliquen player->state); 75976c2145dSArnaud Pouliquen return -EINVAL; 76076c2145dSArnaud Pouliquen } 76176c2145dSArnaud Pouliquen 76276c2145dSArnaud Pouliquen /* Calculate transfer size (in fifo cells and bytes) for frame count */ 7635a4326d1SArnaud Pouliquen if (player->type == SND_ST_UNIPERIF_TYPE_TDM) { 7648d8b1e2eSMoise Gergaud /* transfer size = user frame size (in 32 bits FIFO cell) */ 7658d8b1e2eSMoise Gergaud transfer_size = 7668d8b1e2eSMoise Gergaud sti_uniperiph_get_user_frame_size(runtime) / 4; 7678d8b1e2eSMoise Gergaud } else { 76876c2145dSArnaud Pouliquen transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES; 7698d8b1e2eSMoise Gergaud } 77076c2145dSArnaud Pouliquen 77176c2145dSArnaud Pouliquen /* Calculate number of empty cells available before asserting DREQ */ 77276c2145dSArnaud Pouliquen if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) { 77376c2145dSArnaud Pouliquen trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size; 77476c2145dSArnaud Pouliquen } else { 77576c2145dSArnaud Pouliquen /* 77676c2145dSArnaud Pouliquen * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0 77776c2145dSArnaud Pouliquen * FDMA_TRIGGER_LIMIT also controls when the state switches 77876c2145dSArnaud Pouliquen * from OFF or STANDBY to AUDIO DATA. 77976c2145dSArnaud Pouliquen */ 78076c2145dSArnaud Pouliquen trigger_limit = transfer_size; 78176c2145dSArnaud Pouliquen } 78276c2145dSArnaud Pouliquen 78376c2145dSArnaud Pouliquen /* Trigger limit must be an even number */ 78476c2145dSArnaud Pouliquen if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) || 78576c2145dSArnaud Pouliquen (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) { 786748abba8SArnaud Pouliquen dev_err(player->dev, "invalid trigger limit %d\n", 787748abba8SArnaud Pouliquen trigger_limit); 78876c2145dSArnaud Pouliquen return -EINVAL; 78976c2145dSArnaud Pouliquen } 79076c2145dSArnaud Pouliquen 79176c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit); 79276c2145dSArnaud Pouliquen 79376c2145dSArnaud Pouliquen /* Uniperipheral setup depends on player type */ 7945a4326d1SArnaud Pouliquen switch (player->type) { 7955295a0dcSMoise Gergaud case SND_ST_UNIPERIF_TYPE_HDMI: 79676c2145dSArnaud Pouliquen ret = uni_player_prepare_iec958(player, runtime); 79776c2145dSArnaud Pouliquen break; 7985295a0dcSMoise Gergaud case SND_ST_UNIPERIF_TYPE_PCM: 79976c2145dSArnaud Pouliquen ret = uni_player_prepare_pcm(player, runtime); 80076c2145dSArnaud Pouliquen break; 8015295a0dcSMoise Gergaud case SND_ST_UNIPERIF_TYPE_SPDIF: 80276c2145dSArnaud Pouliquen ret = uni_player_prepare_iec958(player, runtime); 80376c2145dSArnaud Pouliquen break; 8048d8b1e2eSMoise Gergaud case SND_ST_UNIPERIF_TYPE_TDM: 8058d8b1e2eSMoise Gergaud ret = uni_player_prepare_tdm(player, runtime); 8068d8b1e2eSMoise Gergaud break; 80776c2145dSArnaud Pouliquen default: 808748abba8SArnaud Pouliquen dev_err(player->dev, "invalid player type\n"); 80976c2145dSArnaud Pouliquen return -EINVAL; 81076c2145dSArnaud Pouliquen } 81176c2145dSArnaud Pouliquen 81276c2145dSArnaud Pouliquen if (ret) 81376c2145dSArnaud Pouliquen return ret; 81476c2145dSArnaud Pouliquen 81576c2145dSArnaud Pouliquen switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) { 81676c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_NB_NF: 81776c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player); 81876c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player); 81976c2145dSArnaud Pouliquen break; 82076c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_NB_IF: 82176c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player); 82276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player); 82376c2145dSArnaud Pouliquen break; 82476c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_IB_NF: 82576c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player); 82676c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 82776c2145dSArnaud Pouliquen break; 82876c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_IB_IF: 82976c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player); 83076c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 831ed6c75f2SArnaud Pouliquen break; 83276c2145dSArnaud Pouliquen } 83376c2145dSArnaud Pouliquen 83476c2145dSArnaud Pouliquen switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) { 83576c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_I2S: 83676c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player); 83776c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player); 83876c2145dSArnaud Pouliquen break; 83976c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_LEFT_J: 84076c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player); 84176c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player); 84276c2145dSArnaud Pouliquen break; 84376c2145dSArnaud Pouliquen case SND_SOC_DAIFMT_RIGHT_J: 84476c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player); 84576c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player); 84676c2145dSArnaud Pouliquen break; 84776c2145dSArnaud Pouliquen default: 848748abba8SArnaud Pouliquen dev_err(player->dev, "format not supported\n"); 84976c2145dSArnaud Pouliquen return -EINVAL; 85076c2145dSArnaud Pouliquen } 85176c2145dSArnaud Pouliquen 85276c2145dSArnaud Pouliquen SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0); 85376c2145dSArnaud Pouliquen 85476c2145dSArnaud Pouliquen 8554c88f89fSArnaud Pouliquen return sti_uniperiph_reset(player); 85676c2145dSArnaud Pouliquen } 85776c2145dSArnaud Pouliquen 85876c2145dSArnaud Pouliquen static int uni_player_start(struct uniperif *player) 85976c2145dSArnaud Pouliquen { 86076c2145dSArnaud Pouliquen int ret; 86176c2145dSArnaud Pouliquen 86276c2145dSArnaud Pouliquen /* The player should be stopped */ 86376c2145dSArnaud Pouliquen if (player->state != UNIPERIF_STATE_STOPPED) { 864748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid player state\n", __func__); 86576c2145dSArnaud Pouliquen return -EINVAL; 86676c2145dSArnaud Pouliquen } 86776c2145dSArnaud Pouliquen 86876c2145dSArnaud Pouliquen ret = clk_prepare_enable(player->clk); 86976c2145dSArnaud Pouliquen if (ret) { 870748abba8SArnaud Pouliquen dev_err(player->dev, "%s: Failed to enable clock\n", __func__); 87176c2145dSArnaud Pouliquen return ret; 87276c2145dSArnaud Pouliquen } 87376c2145dSArnaud Pouliquen 87476c2145dSArnaud Pouliquen /* Clear any pending interrupts */ 87576c2145dSArnaud Pouliquen SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player)); 87676c2145dSArnaud Pouliquen 87776c2145dSArnaud Pouliquen /* Set the interrupt mask */ 87876c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BSET_DMA_ERROR(player); 87976c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player); 88076c2145dSArnaud Pouliquen 88176c2145dSArnaud Pouliquen /* Enable underflow recovery interrupts */ 8825a4326d1SArnaud Pouliquen if (player->underflow_enabled) { 88376c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player); 88476c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player); 88576c2145dSArnaud Pouliquen } 88676c2145dSArnaud Pouliquen 8874c88f89fSArnaud Pouliquen ret = sti_uniperiph_reset(player); 888b7c8c5d6SWei Yongjun if (ret < 0) { 889b7c8c5d6SWei Yongjun clk_disable_unprepare(player->clk); 89076c2145dSArnaud Pouliquen return ret; 891b7c8c5d6SWei Yongjun } 89276c2145dSArnaud Pouliquen 89376c2145dSArnaud Pouliquen /* 89476c2145dSArnaud Pouliquen * Does not use IEC61937 features of the uniperipheral hardware. 89576c2145dSArnaud Pouliquen * Instead it performs IEC61937 in software and inserts it directly 89676c2145dSArnaud Pouliquen * into the audio data stream. As such, when encoded mode is selected, 89776c2145dSArnaud Pouliquen * linear pcm mode is still used, but with the differences of the 89876c2145dSArnaud Pouliquen * channel status bits set for encoded mode and the validity bits set. 89976c2145dSArnaud Pouliquen */ 90076c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player); 90176c2145dSArnaud Pouliquen 90276c2145dSArnaud Pouliquen /* 90376c2145dSArnaud Pouliquen * If iec958 formatting is required for hdmi or spdif, then it must be 90476c2145dSArnaud Pouliquen * enabled after the operation mode is set. If set prior to this, it 90576c2145dSArnaud Pouliquen * will not take affect and hang the player. 90676c2145dSArnaud Pouliquen */ 90776c2145dSArnaud Pouliquen if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 9085295a0dcSMoise Gergaud if (UNIPERIF_TYPE_IS_IEC958(player)) 90976c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player); 91076c2145dSArnaud Pouliquen 91176c2145dSArnaud Pouliquen /* Force channel status update (no update if clk disable) */ 91276c2145dSArnaud Pouliquen if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 91376c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player); 91476c2145dSArnaud Pouliquen else 91576c2145dSArnaud Pouliquen SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player); 91676c2145dSArnaud Pouliquen 91776c2145dSArnaud Pouliquen /* Update state to started */ 91876c2145dSArnaud Pouliquen player->state = UNIPERIF_STATE_STARTED; 91976c2145dSArnaud Pouliquen 92076c2145dSArnaud Pouliquen return 0; 92176c2145dSArnaud Pouliquen } 92276c2145dSArnaud Pouliquen 92376c2145dSArnaud Pouliquen static int uni_player_stop(struct uniperif *player) 92476c2145dSArnaud Pouliquen { 92576c2145dSArnaud Pouliquen int ret; 92676c2145dSArnaud Pouliquen 92776c2145dSArnaud Pouliquen /* The player should not be in stopped state */ 92876c2145dSArnaud Pouliquen if (player->state == UNIPERIF_STATE_STOPPED) { 929748abba8SArnaud Pouliquen dev_err(player->dev, "%s: invalid player state\n", __func__); 93076c2145dSArnaud Pouliquen return -EINVAL; 93176c2145dSArnaud Pouliquen } 93276c2145dSArnaud Pouliquen 93376c2145dSArnaud Pouliquen /* Turn the player off */ 93476c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_OPERATION_OFF(player); 93576c2145dSArnaud Pouliquen 9364c88f89fSArnaud Pouliquen ret = sti_uniperiph_reset(player); 93776c2145dSArnaud Pouliquen if (ret < 0) 93876c2145dSArnaud Pouliquen return ret; 93976c2145dSArnaud Pouliquen 94076c2145dSArnaud Pouliquen /* Disable interrupts */ 94176c2145dSArnaud Pouliquen SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player)); 94276c2145dSArnaud Pouliquen 94376c2145dSArnaud Pouliquen /* Disable clock */ 94476c2145dSArnaud Pouliquen clk_disable_unprepare(player->clk); 94576c2145dSArnaud Pouliquen 94676c2145dSArnaud Pouliquen /* Update state to stopped and return */ 94776c2145dSArnaud Pouliquen player->state = UNIPERIF_STATE_STOPPED; 94876c2145dSArnaud Pouliquen 94976c2145dSArnaud Pouliquen return 0; 95076c2145dSArnaud Pouliquen } 95176c2145dSArnaud Pouliquen 95276c2145dSArnaud Pouliquen int uni_player_resume(struct uniperif *player) 95376c2145dSArnaud Pouliquen { 95476c2145dSArnaud Pouliquen int ret; 95576c2145dSArnaud Pouliquen 95676c2145dSArnaud Pouliquen /* Select the frequency synthesizer clock */ 95776c2145dSArnaud Pouliquen if (player->clk_sel) { 95876c2145dSArnaud Pouliquen ret = regmap_field_write(player->clk_sel, 1); 95976c2145dSArnaud Pouliquen if (ret) { 96076c2145dSArnaud Pouliquen dev_err(player->dev, 961748abba8SArnaud Pouliquen "%s: Failed to select freq synth clock\n", 96276c2145dSArnaud Pouliquen __func__); 96376c2145dSArnaud Pouliquen return ret; 96476c2145dSArnaud Pouliquen } 96576c2145dSArnaud Pouliquen } 96676c2145dSArnaud Pouliquen 96776c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player); 96876c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 96976c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 97076c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player); 97176c2145dSArnaud Pouliquen 97276c2145dSArnaud Pouliquen return 0; 97376c2145dSArnaud Pouliquen } 97476c2145dSArnaud Pouliquen EXPORT_SYMBOL_GPL(uni_player_resume); 97576c2145dSArnaud Pouliquen 97676c2145dSArnaud Pouliquen static int uni_player_trigger(struct snd_pcm_substream *substream, 97776c2145dSArnaud Pouliquen int cmd, struct snd_soc_dai *dai) 97876c2145dSArnaud Pouliquen { 97976c2145dSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 98076c2145dSArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 98176c2145dSArnaud Pouliquen 98276c2145dSArnaud Pouliquen switch (cmd) { 98376c2145dSArnaud Pouliquen case SNDRV_PCM_TRIGGER_START: 98476c2145dSArnaud Pouliquen return uni_player_start(player); 98576c2145dSArnaud Pouliquen case SNDRV_PCM_TRIGGER_STOP: 98676c2145dSArnaud Pouliquen return uni_player_stop(player); 98776c2145dSArnaud Pouliquen case SNDRV_PCM_TRIGGER_RESUME: 98876c2145dSArnaud Pouliquen return uni_player_resume(player); 98976c2145dSArnaud Pouliquen default: 99076c2145dSArnaud Pouliquen return -EINVAL; 99176c2145dSArnaud Pouliquen } 99276c2145dSArnaud Pouliquen } 99376c2145dSArnaud Pouliquen 99476c2145dSArnaud Pouliquen static void uni_player_shutdown(struct snd_pcm_substream *substream, 99576c2145dSArnaud Pouliquen struct snd_soc_dai *dai) 99676c2145dSArnaud Pouliquen { 99776c2145dSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 99876c2145dSArnaud Pouliquen struct uniperif *player = priv->dai_data.uni; 999d05d862eSArnaud Pouliquen unsigned long flags; 100076c2145dSArnaud Pouliquen 1001d05d862eSArnaud Pouliquen spin_lock_irqsave(&player->irq_lock, flags); 100276c2145dSArnaud Pouliquen if (player->state != UNIPERIF_STATE_STOPPED) 100376c2145dSArnaud Pouliquen /* Stop the player */ 100476c2145dSArnaud Pouliquen uni_player_stop(player); 100536a65e20SMoise Gergaud 100636a65e20SMoise Gergaud player->substream = NULL; 1007d05d862eSArnaud Pouliquen spin_unlock_irqrestore(&player->irq_lock, flags); 100876c2145dSArnaud Pouliquen } 100976c2145dSArnaud Pouliquen 10103ee15cacSMoise Gergaud static int uni_player_parse_dt_audio_glue(struct platform_device *pdev, 101176c2145dSArnaud Pouliquen struct uniperif *player) 101276c2145dSArnaud Pouliquen { 101376c2145dSArnaud Pouliquen struct device_node *node = pdev->dev.of_node; 101476c2145dSArnaud Pouliquen struct regmap *regmap; 10153ee15cacSMoise Gergaud struct reg_field regfield[2] = { 10163ee15cacSMoise Gergaud /* PCM_CLK_SEL */ 10173ee15cacSMoise Gergaud REG_FIELD(SYS_CFG_AUDIO_GLUE, 10185a4326d1SArnaud Pouliquen 8 + player->id, 10195a4326d1SArnaud Pouliquen 8 + player->id), 10203ee15cacSMoise Gergaud /* PCMP_VALID_SEL */ 10213ee15cacSMoise Gergaud REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1) 10223ee15cacSMoise Gergaud }; 102376c2145dSArnaud Pouliquen 102476c2145dSArnaud Pouliquen regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg"); 102576c2145dSArnaud Pouliquen 10267d267ddfSWei Yongjun if (IS_ERR(regmap)) { 102776c2145dSArnaud Pouliquen dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n"); 10287d267ddfSWei Yongjun return PTR_ERR(regmap); 102976c2145dSArnaud Pouliquen } 103076c2145dSArnaud Pouliquen 10313ee15cacSMoise Gergaud player->clk_sel = regmap_field_alloc(regmap, regfield[0]); 10323ee15cacSMoise Gergaud player->valid_sel = regmap_field_alloc(regmap, regfield[1]); 10333ee15cacSMoise Gergaud 103476c2145dSArnaud Pouliquen return 0; 103576c2145dSArnaud Pouliquen } 103676c2145dSArnaud Pouliquen 103785cf604eSLars-Peter Clausen static const struct snd_soc_dai_ops uni_player_dai_ops = { 1038fa050796SArnaud Pouliquen .startup = uni_player_startup, 103976c2145dSArnaud Pouliquen .shutdown = uni_player_shutdown, 104076c2145dSArnaud Pouliquen .prepare = uni_player_prepare, 1041*641de16bSJerome Audu .probe = sti_uniperiph_dai_probe, 104276c2145dSArnaud Pouliquen .trigger = uni_player_trigger, 104376c2145dSArnaud Pouliquen .hw_params = sti_uniperiph_dai_hw_params, 104476c2145dSArnaud Pouliquen .set_fmt = sti_uniperiph_dai_set_fmt, 10458d8b1e2eSMoise Gergaud .set_sysclk = uni_player_set_sysclk, 10468d8b1e2eSMoise Gergaud .set_tdm_slot = sti_uniperiph_set_tdm_slot 104776c2145dSArnaud Pouliquen }; 104876c2145dSArnaud Pouliquen 104976c2145dSArnaud Pouliquen int uni_player_init(struct platform_device *pdev, 105076c2145dSArnaud Pouliquen struct uniperif *player) 105176c2145dSArnaud Pouliquen { 105276c2145dSArnaud Pouliquen int ret = 0; 105376c2145dSArnaud Pouliquen 105476c2145dSArnaud Pouliquen player->dev = &pdev->dev; 105576c2145dSArnaud Pouliquen player->state = UNIPERIF_STATE_STOPPED; 105676c2145dSArnaud Pouliquen player->dai_ops = &uni_player_dai_ops; 105776c2145dSArnaud Pouliquen 10585a4326d1SArnaud Pouliquen /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */ 10595a4326d1SArnaud Pouliquen ret = uni_player_parse_dt_audio_glue(pdev, player); 106076c2145dSArnaud Pouliquen 106176c2145dSArnaud Pouliquen if (ret < 0) { 1062748abba8SArnaud Pouliquen dev_err(player->dev, "Failed to parse DeviceTree\n"); 106376c2145dSArnaud Pouliquen return ret; 106476c2145dSArnaud Pouliquen } 106576c2145dSArnaud Pouliquen 10665a4326d1SArnaud Pouliquen /* Underflow recovery is only supported on later ip revisions */ 10675a4326d1SArnaud Pouliquen if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 10685a4326d1SArnaud Pouliquen player->underflow_enabled = 1; 10695a4326d1SArnaud Pouliquen 10708d8b1e2eSMoise Gergaud if (UNIPERIF_TYPE_IS_TDM(player)) 10718d8b1e2eSMoise Gergaud player->hw = &uni_tdm_hw; 10728d8b1e2eSMoise Gergaud else 10738d8b1e2eSMoise Gergaud player->hw = &uni_player_pcm_hw; 10748d8b1e2eSMoise Gergaud 107576c2145dSArnaud Pouliquen /* Get uniperif resource */ 107676c2145dSArnaud Pouliquen player->clk = of_clk_get(pdev->dev.of_node, 0); 1077748abba8SArnaud Pouliquen if (IS_ERR(player->clk)) { 1078748abba8SArnaud Pouliquen dev_err(player->dev, "Failed to get clock\n"); 107965ed0a8dSDan Carpenter return PTR_ERR(player->clk); 1080748abba8SArnaud Pouliquen } 108176c2145dSArnaud Pouliquen 108276c2145dSArnaud Pouliquen /* Select the frequency synthesizer clock */ 108376c2145dSArnaud Pouliquen if (player->clk_sel) { 108476c2145dSArnaud Pouliquen ret = regmap_field_write(player->clk_sel, 1); 108576c2145dSArnaud Pouliquen if (ret) { 108676c2145dSArnaud Pouliquen dev_err(player->dev, 1087748abba8SArnaud Pouliquen "%s: Failed to select freq synth clock\n", 108876c2145dSArnaud Pouliquen __func__); 108976c2145dSArnaud Pouliquen return ret; 109076c2145dSArnaud Pouliquen } 109176c2145dSArnaud Pouliquen } 109276c2145dSArnaud Pouliquen 10933ee15cacSMoise Gergaud /* connect to I2S/TDM TX bus */ 10943ee15cacSMoise Gergaud if (player->valid_sel && 10955a4326d1SArnaud Pouliquen (player->id == UNIPERIF_PLAYER_I2S_OUT)) { 10965a4326d1SArnaud Pouliquen ret = regmap_field_write(player->valid_sel, player->id); 10973ee15cacSMoise Gergaud if (ret) { 10983ee15cacSMoise Gergaud dev_err(player->dev, 1099748abba8SArnaud Pouliquen "%s: unable to connect to tdm bus\n", __func__); 11003ee15cacSMoise Gergaud return ret; 11013ee15cacSMoise Gergaud } 11023ee15cacSMoise Gergaud } 11033ee15cacSMoise Gergaud 110476c2145dSArnaud Pouliquen ret = devm_request_irq(&pdev->dev, player->irq, 110576c2145dSArnaud Pouliquen uni_player_irq_handler, IRQF_SHARED, 110676c2145dSArnaud Pouliquen dev_name(&pdev->dev), player); 1107748abba8SArnaud Pouliquen if (ret < 0) { 1108748abba8SArnaud Pouliquen dev_err(player->dev, "unable to request IRQ %d\n", player->irq); 110976c2145dSArnaud Pouliquen return ret; 1110748abba8SArnaud Pouliquen } 111176c2145dSArnaud Pouliquen 1112fa050796SArnaud Pouliquen mutex_init(&player->ctrl_lock); 1113d05d862eSArnaud Pouliquen spin_lock_init(&player->irq_lock); 1114fa050796SArnaud Pouliquen 111576c2145dSArnaud Pouliquen /* Ensure that disabled by default */ 111676c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player); 111776c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 111876c2145dSArnaud Pouliquen SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 111976c2145dSArnaud Pouliquen SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player); 112076c2145dSArnaud Pouliquen 11215295a0dcSMoise Gergaud if (UNIPERIF_TYPE_IS_IEC958(player)) { 112276c2145dSArnaud Pouliquen /* Set default iec958 status bits */ 112376c2145dSArnaud Pouliquen 112476c2145dSArnaud Pouliquen /* Consumer, PCM, copyright, 2ch, mode 0 */ 112576c2145dSArnaud Pouliquen player->stream_settings.iec958.status[0] = 0x00; 112676c2145dSArnaud Pouliquen /* Broadcast reception category */ 112776c2145dSArnaud Pouliquen player->stream_settings.iec958.status[1] = 112876c2145dSArnaud Pouliquen IEC958_AES1_CON_GENERAL; 112976c2145dSArnaud Pouliquen /* Do not take into account source or channel number */ 113076c2145dSArnaud Pouliquen player->stream_settings.iec958.status[2] = 113176c2145dSArnaud Pouliquen IEC958_AES2_CON_SOURCE_UNSPEC; 113276c2145dSArnaud Pouliquen /* Sampling frequency not indicated */ 113376c2145dSArnaud Pouliquen player->stream_settings.iec958.status[3] = 113476c2145dSArnaud Pouliquen IEC958_AES3_CON_FS_NOTID; 113576c2145dSArnaud Pouliquen /* Max sample word 24-bit, sample word length not indicated */ 113676c2145dSArnaud Pouliquen player->stream_settings.iec958.status[4] = 113776c2145dSArnaud Pouliquen IEC958_AES4_CON_MAX_WORDLEN_24 | 113876c2145dSArnaud Pouliquen IEC958_AES4_CON_WORDLEN_24_20; 113976c2145dSArnaud Pouliquen 114036cc0935SArnaud Pouliquen player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl); 114136cc0935SArnaud Pouliquen player->snd_ctrls = snd_sti_iec_ctl[0]; 114236cc0935SArnaud Pouliquen } else { 114336cc0935SArnaud Pouliquen player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl); 114436cc0935SArnaud Pouliquen player->snd_ctrls = snd_sti_pcm_ctl[0]; 114536cc0935SArnaud Pouliquen } 1146fa050796SArnaud Pouliquen 114776c2145dSArnaud Pouliquen return 0; 114876c2145dSArnaud Pouliquen } 114976c2145dSArnaud Pouliquen EXPORT_SYMBOL_GPL(uni_player_init); 1150