18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics SA 2015 48c2ecf20Sopenharmony_ci * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com> 58c2ecf20Sopenharmony_ci * for STMicroelectronics. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <sound/soc.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include "uniperif.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define UNIPERIF_READER_I2S_IN 0 /* reader id connected to I2S/TDM TX bus */ 138c2ecf20Sopenharmony_ci/* 148c2ecf20Sopenharmony_ci * Note: snd_pcm_hardware is linked to DMA controller but is declared here to 158c2ecf20Sopenharmony_ci * integrate unireader capability in term of rate and supported channels 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware uni_reader_pcm_hw = { 188c2ecf20Sopenharmony_ci .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | 198c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP | 208c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP_VALID, 218c2ecf20Sopenharmony_ci .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE, 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci .rates = SNDRV_PCM_RATE_CONTINUOUS, 248c2ecf20Sopenharmony_ci .rate_min = 8000, 258c2ecf20Sopenharmony_ci .rate_max = 96000, 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci .channels_min = 2, 288c2ecf20Sopenharmony_ci .channels_max = 8, 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci .periods_min = 2, 318c2ecf20Sopenharmony_ci .periods_max = 48, 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci .period_bytes_min = 128, 348c2ecf20Sopenharmony_ci .period_bytes_max = 64 * PAGE_SIZE, 358c2ecf20Sopenharmony_ci .buffer_bytes_max = 256 * PAGE_SIZE 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* 398c2ecf20Sopenharmony_ci * uni_reader_irq_handler 408c2ecf20Sopenharmony_ci * In case of error audio stream is stopped; stop action is protected via PCM 418c2ecf20Sopenharmony_ci * stream lock to avoid race condition with trigger callback. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_cistatic irqreturn_t uni_reader_irq_handler(int irq, void *dev_id) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci irqreturn_t ret = IRQ_NONE; 468c2ecf20Sopenharmony_ci struct uniperif *reader = dev_id; 478c2ecf20Sopenharmony_ci unsigned int status; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci spin_lock(&reader->irq_lock); 508c2ecf20Sopenharmony_ci if (!reader->substream) 518c2ecf20Sopenharmony_ci goto irq_spin_unlock; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci snd_pcm_stream_lock(reader->substream); 548c2ecf20Sopenharmony_ci if (reader->state == UNIPERIF_STATE_STOPPED) { 558c2ecf20Sopenharmony_ci /* Unexpected IRQ: do nothing */ 568c2ecf20Sopenharmony_ci dev_warn(reader->dev, "unexpected IRQ\n"); 578c2ecf20Sopenharmony_ci goto stream_unlock; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* Get interrupt status & clear them immediately */ 618c2ecf20Sopenharmony_ci status = GET_UNIPERIF_ITS(reader); 628c2ecf20Sopenharmony_ci SET_UNIPERIF_ITS_BCLR(reader, status); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci /* Check for fifo overflow error */ 658c2ecf20Sopenharmony_ci if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(reader))) { 668c2ecf20Sopenharmony_ci dev_err(reader->dev, "FIFO error detected\n"); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci snd_pcm_stop(reader->substream, SNDRV_PCM_STATE_XRUN); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci ret = IRQ_HANDLED; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistream_unlock: 748c2ecf20Sopenharmony_ci snd_pcm_stream_unlock(reader->substream); 758c2ecf20Sopenharmony_ciirq_spin_unlock: 768c2ecf20Sopenharmony_ci spin_unlock(&reader->irq_lock); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return ret; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic int uni_reader_prepare_pcm(struct snd_pcm_runtime *runtime, 828c2ecf20Sopenharmony_ci struct uniperif *reader) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci int slot_width; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci /* Force slot width to 32 in I2S mode */ 878c2ecf20Sopenharmony_ci if ((reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) 888c2ecf20Sopenharmony_ci == SND_SOC_DAIFMT_I2S) { 898c2ecf20Sopenharmony_ci slot_width = 32; 908c2ecf20Sopenharmony_ci } else { 918c2ecf20Sopenharmony_ci switch (runtime->format) { 928c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S16_LE: 938c2ecf20Sopenharmony_ci slot_width = 16; 948c2ecf20Sopenharmony_ci break; 958c2ecf20Sopenharmony_ci default: 968c2ecf20Sopenharmony_ci slot_width = 32; 978c2ecf20Sopenharmony_ci break; 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* Number of bits per subframe (i.e one channel sample) on input. */ 1028c2ecf20Sopenharmony_ci switch (slot_width) { 1038c2ecf20Sopenharmony_ci case 32: 1048c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_NBIT_32(reader); 1058c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader); 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci case 16: 1088c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_NBIT_16(reader); 1098c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(reader); 1108c2ecf20Sopenharmony_ci break; 1118c2ecf20Sopenharmony_ci default: 1128c2ecf20Sopenharmony_ci dev_err(reader->dev, "subframe format not supported\n"); 1138c2ecf20Sopenharmony_ci return -EINVAL; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* Configure data memory format */ 1178c2ecf20Sopenharmony_ci switch (runtime->format) { 1188c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S16_LE: 1198c2ecf20Sopenharmony_ci /* One data word contains two samples */ 1208c2ecf20Sopenharmony_ci SET_UNIPERIF_CONFIG_MEM_FMT_16_16(reader); 1218c2ecf20Sopenharmony_ci break; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S32_LE: 1248c2ecf20Sopenharmony_ci /* 1258c2ecf20Sopenharmony_ci * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits 1268c2ecf20Sopenharmony_ci * on the MSB then zeros (if less than 32 bytes)"... 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_ci SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader); 1298c2ecf20Sopenharmony_ci break; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci default: 1328c2ecf20Sopenharmony_ci dev_err(reader->dev, "format not supported\n"); 1338c2ecf20Sopenharmony_ci return -EINVAL; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* Number of channels must be even */ 1378c2ecf20Sopenharmony_ci if ((runtime->channels % 2) || (runtime->channels < 2) || 1388c2ecf20Sopenharmony_ci (runtime->channels > 10)) { 1398c2ecf20Sopenharmony_ci dev_err(reader->dev, "%s: invalid nb of channels\n", __func__); 1408c2ecf20Sopenharmony_ci return -EINVAL; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_NUM_CH(reader, runtime->channels / 2); 1448c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic int uni_reader_prepare_tdm(struct snd_pcm_runtime *runtime, 1508c2ecf20Sopenharmony_ci struct uniperif *reader) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci int frame_size; /* user tdm frame size in bytes */ 1538c2ecf20Sopenharmony_ci /* default unip TDM_WORD_POS_X_Y */ 1548c2ecf20Sopenharmony_ci unsigned int word_pos[4] = { 1558c2ecf20Sopenharmony_ci 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci frame_size = sti_uniperiph_get_user_frame_size(runtime); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* fix 16/0 format */ 1608c2ecf20Sopenharmony_ci SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader); 1618c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* number of words inserted on the TDM line */ 1648c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_NUM_CH(reader, frame_size / 4 / 2); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader); 1678c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader); 1688c2ecf20Sopenharmony_ci SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(reader); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* 1718c2ecf20Sopenharmony_ci * set the timeslots allocation for words in FIFO 1728c2ecf20Sopenharmony_ci * 1738c2ecf20Sopenharmony_ci * HW bug: (LSB word < MSB word) => this config is not possible 1748c2ecf20Sopenharmony_ci * So if we want (LSB word < MSB) word, then it shall be 1758c2ecf20Sopenharmony_ci * handled by user 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_ci sti_uniperiph_get_tdm_word_pos(reader, word_pos); 1788c2ecf20Sopenharmony_ci SET_UNIPERIF_TDM_WORD_POS(reader, 1_2, word_pos[WORD_1_2]); 1798c2ecf20Sopenharmony_ci SET_UNIPERIF_TDM_WORD_POS(reader, 3_4, word_pos[WORD_3_4]); 1808c2ecf20Sopenharmony_ci SET_UNIPERIF_TDM_WORD_POS(reader, 5_6, word_pos[WORD_5_6]); 1818c2ecf20Sopenharmony_ci SET_UNIPERIF_TDM_WORD_POS(reader, 7_8, word_pos[WORD_7_8]); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return 0; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic int uni_reader_prepare(struct snd_pcm_substream *substream, 1878c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 1908c2ecf20Sopenharmony_ci struct uniperif *reader = priv->dai_data.uni; 1918c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 1928c2ecf20Sopenharmony_ci int transfer_size, trigger_limit, ret; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* The reader should be stopped */ 1958c2ecf20Sopenharmony_ci if (reader->state != UNIPERIF_STATE_STOPPED) { 1968c2ecf20Sopenharmony_ci dev_err(reader->dev, "%s: invalid reader state %d\n", __func__, 1978c2ecf20Sopenharmony_ci reader->state); 1988c2ecf20Sopenharmony_ci return -EINVAL; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci /* Calculate transfer size (in fifo cells and bytes) for frame count */ 2028c2ecf20Sopenharmony_ci if (reader->type == SND_ST_UNIPERIF_TYPE_TDM) { 2038c2ecf20Sopenharmony_ci /* transfer size = unip frame size (in 32 bits FIFO cell) */ 2048c2ecf20Sopenharmony_ci transfer_size = 2058c2ecf20Sopenharmony_ci sti_uniperiph_get_user_frame_size(runtime) / 4; 2068c2ecf20Sopenharmony_ci } else { 2078c2ecf20Sopenharmony_ci transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES; 2088c2ecf20Sopenharmony_ci } 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci /* Calculate number of empty cells available before asserting DREQ */ 2118c2ecf20Sopenharmony_ci if (reader->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 2128c2ecf20Sopenharmony_ci trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size; 2138c2ecf20Sopenharmony_ci else 2148c2ecf20Sopenharmony_ci /* 2158c2ecf20Sopenharmony_ci * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0 2168c2ecf20Sopenharmony_ci * FDMA_TRIGGER_LIMIT also controls when the state switches 2178c2ecf20Sopenharmony_ci * from OFF or STANDBY to AUDIO DATA. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_ci trigger_limit = transfer_size; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* Trigger limit must be an even number */ 2228c2ecf20Sopenharmony_ci if ((!trigger_limit % 2) || 2238c2ecf20Sopenharmony_ci (trigger_limit != 1 && transfer_size % 2) || 2248c2ecf20Sopenharmony_ci (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(reader))) { 2258c2ecf20Sopenharmony_ci dev_err(reader->dev, "invalid trigger limit %d\n", 2268c2ecf20Sopenharmony_ci trigger_limit); 2278c2ecf20Sopenharmony_ci return -EINVAL; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(reader, trigger_limit); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (UNIPERIF_TYPE_IS_TDM(reader)) 2338c2ecf20Sopenharmony_ci ret = uni_reader_prepare_tdm(runtime, reader); 2348c2ecf20Sopenharmony_ci else 2358c2ecf20Sopenharmony_ci ret = uni_reader_prepare_pcm(runtime, reader); 2368c2ecf20Sopenharmony_ci if (ret) 2378c2ecf20Sopenharmony_ci return ret; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci switch (reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) { 2408c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_I2S: 2418c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader); 2428c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(reader); 2438c2ecf20Sopenharmony_ci break; 2448c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_LEFT_J: 2458c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader); 2468c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader); 2478c2ecf20Sopenharmony_ci break; 2488c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_RIGHT_J: 2498c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(reader); 2508c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader); 2518c2ecf20Sopenharmony_ci break; 2528c2ecf20Sopenharmony_ci default: 2538c2ecf20Sopenharmony_ci dev_err(reader->dev, "format not supported\n"); 2548c2ecf20Sopenharmony_ci return -EINVAL; 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* Data clocking (changing) on the rising/falling edge */ 2588c2ecf20Sopenharmony_ci switch (reader->daifmt & SND_SOC_DAIFMT_INV_MASK) { 2598c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_NB_NF: 2608c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader); 2618c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader); 2628c2ecf20Sopenharmony_ci break; 2638c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_NB_IF: 2648c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader); 2658c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader); 2668c2ecf20Sopenharmony_ci break; 2678c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_IB_NF: 2688c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader); 2698c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader); 2708c2ecf20Sopenharmony_ci break; 2718c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_IB_IF: 2728c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader); 2738c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader); 2748c2ecf20Sopenharmony_ci break; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci /* Clear any pending interrupts */ 2788c2ecf20Sopenharmony_ci SET_UNIPERIF_ITS_BCLR(reader, GET_UNIPERIF_ITS(reader)); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(reader, 0); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* Set the interrupt mask */ 2838c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_DMA_ERROR(reader); 2848c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader); 2858c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_MEM_BLK_READ(reader); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci /* Enable underflow recovery interrupts */ 2888c2ecf20Sopenharmony_ci if (reader->underflow_enabled) { 2898c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(reader); 2908c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(reader); 2918c2ecf20Sopenharmony_ci } 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci /* Reset uniperipheral reader */ 2948c2ecf20Sopenharmony_ci return sti_uniperiph_reset(reader); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic int uni_reader_start(struct uniperif *reader) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci /* The reader should be stopped */ 3008c2ecf20Sopenharmony_ci if (reader->state != UNIPERIF_STATE_STOPPED) { 3018c2ecf20Sopenharmony_ci dev_err(reader->dev, "%s: invalid reader state\n", __func__); 3028c2ecf20Sopenharmony_ci return -EINVAL; 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci /* Enable reader interrupts (and clear possible stalled ones) */ 3068c2ecf20Sopenharmony_ci SET_UNIPERIF_ITS_BCLR_FIFO_ERROR(reader); 3078c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader); 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci /* Launch the reader */ 3108c2ecf20Sopenharmony_ci SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(reader); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci /* Update state to started */ 3138c2ecf20Sopenharmony_ci reader->state = UNIPERIF_STATE_STARTED; 3148c2ecf20Sopenharmony_ci return 0; 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_cistatic int uni_reader_stop(struct uniperif *reader) 3188c2ecf20Sopenharmony_ci{ 3198c2ecf20Sopenharmony_ci /* The reader should not be in stopped state */ 3208c2ecf20Sopenharmony_ci if (reader->state == UNIPERIF_STATE_STOPPED) { 3218c2ecf20Sopenharmony_ci dev_err(reader->dev, "%s: invalid reader state\n", __func__); 3228c2ecf20Sopenharmony_ci return -EINVAL; 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci /* Turn the reader off */ 3268c2ecf20Sopenharmony_ci SET_UNIPERIF_CTRL_OPERATION_OFF(reader); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci /* Disable interrupts */ 3298c2ecf20Sopenharmony_ci SET_UNIPERIF_ITM_BCLR(reader, GET_UNIPERIF_ITM(reader)); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci /* Update state to stopped and return */ 3328c2ecf20Sopenharmony_ci reader->state = UNIPERIF_STATE_STOPPED; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return 0; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_cistatic int uni_reader_trigger(struct snd_pcm_substream *substream, 3388c2ecf20Sopenharmony_ci int cmd, struct snd_soc_dai *dai) 3398c2ecf20Sopenharmony_ci{ 3408c2ecf20Sopenharmony_ci struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 3418c2ecf20Sopenharmony_ci struct uniperif *reader = priv->dai_data.uni; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci switch (cmd) { 3448c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 3458c2ecf20Sopenharmony_ci return uni_reader_start(reader); 3468c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 3478c2ecf20Sopenharmony_ci return uni_reader_stop(reader); 3488c2ecf20Sopenharmony_ci default: 3498c2ecf20Sopenharmony_ci return -EINVAL; 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic int uni_reader_startup(struct snd_pcm_substream *substream, 3548c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 3578c2ecf20Sopenharmony_ci struct uniperif *reader = priv->dai_data.uni; 3588c2ecf20Sopenharmony_ci unsigned long flags; 3598c2ecf20Sopenharmony_ci int ret; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci spin_lock_irqsave(&reader->irq_lock, flags); 3628c2ecf20Sopenharmony_ci reader->substream = substream; 3638c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&reader->irq_lock, flags); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci if (!UNIPERIF_TYPE_IS_TDM(reader)) 3668c2ecf20Sopenharmony_ci return 0; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci /* refine hw constraint in tdm mode */ 3698c2ecf20Sopenharmony_ci ret = snd_pcm_hw_rule_add(substream->runtime, 0, 3708c2ecf20Sopenharmony_ci SNDRV_PCM_HW_PARAM_CHANNELS, 3718c2ecf20Sopenharmony_ci sti_uniperiph_fix_tdm_chan, 3728c2ecf20Sopenharmony_ci reader, SNDRV_PCM_HW_PARAM_CHANNELS, 3738c2ecf20Sopenharmony_ci -1); 3748c2ecf20Sopenharmony_ci if (ret < 0) 3758c2ecf20Sopenharmony_ci return ret; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci return snd_pcm_hw_rule_add(substream->runtime, 0, 3788c2ecf20Sopenharmony_ci SNDRV_PCM_HW_PARAM_FORMAT, 3798c2ecf20Sopenharmony_ci sti_uniperiph_fix_tdm_format, 3808c2ecf20Sopenharmony_ci reader, SNDRV_PCM_HW_PARAM_FORMAT, 3818c2ecf20Sopenharmony_ci -1); 3828c2ecf20Sopenharmony_ci} 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_cistatic void uni_reader_shutdown(struct snd_pcm_substream *substream, 3858c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 3888c2ecf20Sopenharmony_ci struct uniperif *reader = priv->dai_data.uni; 3898c2ecf20Sopenharmony_ci unsigned long flags; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci spin_lock_irqsave(&reader->irq_lock, flags); 3928c2ecf20Sopenharmony_ci if (reader->state != UNIPERIF_STATE_STOPPED) { 3938c2ecf20Sopenharmony_ci /* Stop the reader */ 3948c2ecf20Sopenharmony_ci uni_reader_stop(reader); 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci reader->substream = NULL; 3978c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&reader->irq_lock, flags); 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops uni_reader_dai_ops = { 4018c2ecf20Sopenharmony_ci .startup = uni_reader_startup, 4028c2ecf20Sopenharmony_ci .shutdown = uni_reader_shutdown, 4038c2ecf20Sopenharmony_ci .prepare = uni_reader_prepare, 4048c2ecf20Sopenharmony_ci .trigger = uni_reader_trigger, 4058c2ecf20Sopenharmony_ci .hw_params = sti_uniperiph_dai_hw_params, 4068c2ecf20Sopenharmony_ci .set_fmt = sti_uniperiph_dai_set_fmt, 4078c2ecf20Sopenharmony_ci .set_tdm_slot = sti_uniperiph_set_tdm_slot 4088c2ecf20Sopenharmony_ci}; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ciint uni_reader_init(struct platform_device *pdev, 4118c2ecf20Sopenharmony_ci struct uniperif *reader) 4128c2ecf20Sopenharmony_ci{ 4138c2ecf20Sopenharmony_ci int ret = 0; 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci reader->dev = &pdev->dev; 4168c2ecf20Sopenharmony_ci reader->state = UNIPERIF_STATE_STOPPED; 4178c2ecf20Sopenharmony_ci reader->dai_ops = &uni_reader_dai_ops; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci if (UNIPERIF_TYPE_IS_TDM(reader)) 4208c2ecf20Sopenharmony_ci reader->hw = &uni_tdm_hw; 4218c2ecf20Sopenharmony_ci else 4228c2ecf20Sopenharmony_ci reader->hw = &uni_reader_pcm_hw; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci ret = devm_request_irq(&pdev->dev, reader->irq, 4258c2ecf20Sopenharmony_ci uni_reader_irq_handler, IRQF_SHARED, 4268c2ecf20Sopenharmony_ci dev_name(&pdev->dev), reader); 4278c2ecf20Sopenharmony_ci if (ret < 0) { 4288c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to request IRQ\n"); 4298c2ecf20Sopenharmony_ci return -EBUSY; 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci spin_lock_init(&reader->irq_lock); 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci return 0; 4358c2ecf20Sopenharmony_ci} 4368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(uni_reader_init); 437