18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Copyright (c) 2020 BayLibre, SAS. 48c2ecf20Sopenharmony_ci// Author: Jerome Brunet <jbrunet@baylibre.com> 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/clk.h> 78c2ecf20Sopenharmony_ci#include <sound/pcm_params.h> 88c2ecf20Sopenharmony_ci#include <sound/soc.h> 98c2ecf20Sopenharmony_ci#include <sound/soc-dai.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include "aiu.h" 128c2ecf20Sopenharmony_ci#include "aiu-fifo.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_EN BIT(0) 158c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_AUTO_DISABLE BIT(1) 168c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_IRQ_MODE GENMASK(3, 2) 178c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_IRQ_OUT_THD BIT(2) 188c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_IRQ_FRAME_READ BIT(3) 198c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_SYNC_HEAD_EN BIT(4) 208c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_BYTE_SEEK BIT(5) 218c2ecf20Sopenharmony_ci#define AIU_IEC958_DCU_FF_CTRL_CONTINUE BIT(6) 228c2ecf20Sopenharmony_ci#define AIU_MEM_IEC958_CONTROL_ENDIAN GENMASK(5, 3) 238c2ecf20Sopenharmony_ci#define AIU_MEM_IEC958_CONTROL_RD_DDR BIT(6) 248c2ecf20Sopenharmony_ci#define AIU_MEM_IEC958_CONTROL_MODE_16BIT BIT(7) 258c2ecf20Sopenharmony_ci#define AIU_MEM_IEC958_CONTROL_MODE_LINEAR BIT(8) 268c2ecf20Sopenharmony_ci#define AIU_MEM_IEC958_BUF_CNTL_INIT BIT(0) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define AIU_FIFO_SPDIF_BLOCK 8 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic struct snd_pcm_hardware fifo_spdif_pcm = { 318c2ecf20Sopenharmony_ci .info = (SNDRV_PCM_INFO_INTERLEAVED | 328c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP | 338c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP_VALID | 348c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_PAUSE), 358c2ecf20Sopenharmony_ci .formats = AIU_FORMATS, 368c2ecf20Sopenharmony_ci .rate_min = 5512, 378c2ecf20Sopenharmony_ci .rate_max = 192000, 388c2ecf20Sopenharmony_ci .channels_min = 2, 398c2ecf20Sopenharmony_ci .channels_max = 2, 408c2ecf20Sopenharmony_ci .period_bytes_min = AIU_FIFO_SPDIF_BLOCK, 418c2ecf20Sopenharmony_ci .period_bytes_max = AIU_FIFO_SPDIF_BLOCK * USHRT_MAX, 428c2ecf20Sopenharmony_ci .periods_min = 2, 438c2ecf20Sopenharmony_ci .periods_max = UINT_MAX, 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci /* No real justification for this */ 468c2ecf20Sopenharmony_ci .buffer_bytes_max = 1 * 1024 * 1024, 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic void fifo_spdif_dcu_enable(struct snd_soc_component *component, 508c2ecf20Sopenharmony_ci bool enable) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, AIU_IEC958_DCU_FF_CTRL, 538c2ecf20Sopenharmony_ci AIU_IEC958_DCU_FF_CTRL_EN, 548c2ecf20Sopenharmony_ci enable ? AIU_IEC958_DCU_FF_CTRL_EN : 0); 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic int fifo_spdif_trigger(struct snd_pcm_substream *substream, int cmd, 588c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci struct snd_soc_component *component = dai->component; 618c2ecf20Sopenharmony_ci int ret; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci ret = aiu_fifo_trigger(substream, cmd, dai); 648c2ecf20Sopenharmony_ci if (ret) 658c2ecf20Sopenharmony_ci return ret; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci switch (cmd) { 688c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 698c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_RESUME: 708c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 718c2ecf20Sopenharmony_ci fifo_spdif_dcu_enable(component, true); 728c2ecf20Sopenharmony_ci break; 738c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_SUSPEND: 748c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 758c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 768c2ecf20Sopenharmony_ci fifo_spdif_dcu_enable(component, false); 778c2ecf20Sopenharmony_ci break; 788c2ecf20Sopenharmony_ci default: 798c2ecf20Sopenharmony_ci return -EINVAL; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic int fifo_spdif_prepare(struct snd_pcm_substream *substream, 868c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct snd_soc_component *component = dai->component; 898c2ecf20Sopenharmony_ci int ret; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci ret = aiu_fifo_prepare(substream, dai); 928c2ecf20Sopenharmony_ci if (ret) 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, 968c2ecf20Sopenharmony_ci AIU_MEM_IEC958_BUF_CNTL, 978c2ecf20Sopenharmony_ci AIU_MEM_IEC958_BUF_CNTL_INIT, 988c2ecf20Sopenharmony_ci AIU_MEM_IEC958_BUF_CNTL_INIT); 998c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, 1008c2ecf20Sopenharmony_ci AIU_MEM_IEC958_BUF_CNTL, 1018c2ecf20Sopenharmony_ci AIU_MEM_IEC958_BUF_CNTL_INIT, 0); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic int fifo_spdif_hw_params(struct snd_pcm_substream *substream, 1078c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *params, 1088c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci struct snd_soc_component *component = dai->component; 1118c2ecf20Sopenharmony_ci unsigned int val; 1128c2ecf20Sopenharmony_ci int ret; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci ret = aiu_fifo_hw_params(substream, params, dai); 1158c2ecf20Sopenharmony_ci if (ret) 1168c2ecf20Sopenharmony_ci return ret; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci val = AIU_MEM_IEC958_CONTROL_RD_DDR | 1198c2ecf20Sopenharmony_ci AIU_MEM_IEC958_CONTROL_MODE_LINEAR; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci switch (params_physical_width(params)) { 1228c2ecf20Sopenharmony_ci case 16: 1238c2ecf20Sopenharmony_ci val |= AIU_MEM_IEC958_CONTROL_MODE_16BIT; 1248c2ecf20Sopenharmony_ci break; 1258c2ecf20Sopenharmony_ci case 32: 1268c2ecf20Sopenharmony_ci break; 1278c2ecf20Sopenharmony_ci default: 1288c2ecf20Sopenharmony_ci dev_err(dai->dev, "Unsupported physical width %u\n", 1298c2ecf20Sopenharmony_ci params_physical_width(params)); 1308c2ecf20Sopenharmony_ci return -EINVAL; 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, AIU_MEM_IEC958_CONTROL, 1348c2ecf20Sopenharmony_ci AIU_MEM_IEC958_CONTROL_ENDIAN | 1358c2ecf20Sopenharmony_ci AIU_MEM_IEC958_CONTROL_RD_DDR | 1368c2ecf20Sopenharmony_ci AIU_MEM_IEC958_CONTROL_MODE_LINEAR | 1378c2ecf20Sopenharmony_ci AIU_MEM_IEC958_CONTROL_MODE_16BIT, 1388c2ecf20Sopenharmony_ci val); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci /* Number bytes read by the FIFO between each IRQ */ 1418c2ecf20Sopenharmony_ci snd_soc_component_write(component, AIU_IEC958_BPF, 1428c2ecf20Sopenharmony_ci params_period_bytes(params)); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci * AUTO_DISABLE and SYNC_HEAD are enabled by default but 1468c2ecf20Sopenharmony_ci * this should be disabled in PCM (uncompressed) mode 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci snd_soc_component_update_bits(component, AIU_IEC958_DCU_FF_CTRL, 1498c2ecf20Sopenharmony_ci AIU_IEC958_DCU_FF_CTRL_AUTO_DISABLE | 1508c2ecf20Sopenharmony_ci AIU_IEC958_DCU_FF_CTRL_IRQ_MODE | 1518c2ecf20Sopenharmony_ci AIU_IEC958_DCU_FF_CTRL_SYNC_HEAD_EN, 1528c2ecf20Sopenharmony_ci AIU_IEC958_DCU_FF_CTRL_IRQ_FRAME_READ); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return 0; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciconst struct snd_soc_dai_ops aiu_fifo_spdif_dai_ops = { 1588c2ecf20Sopenharmony_ci .trigger = fifo_spdif_trigger, 1598c2ecf20Sopenharmony_ci .prepare = fifo_spdif_prepare, 1608c2ecf20Sopenharmony_ci .hw_params = fifo_spdif_hw_params, 1618c2ecf20Sopenharmony_ci .hw_free = aiu_fifo_hw_free, 1628c2ecf20Sopenharmony_ci .startup = aiu_fifo_startup, 1638c2ecf20Sopenharmony_ci .shutdown = aiu_fifo_shutdown, 1648c2ecf20Sopenharmony_ci}; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ciint aiu_fifo_spdif_dai_probe(struct snd_soc_dai *dai) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci struct snd_soc_component *component = dai->component; 1698c2ecf20Sopenharmony_ci struct aiu *aiu = snd_soc_component_get_drvdata(component); 1708c2ecf20Sopenharmony_ci struct aiu_fifo *fifo; 1718c2ecf20Sopenharmony_ci int ret; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci ret = aiu_fifo_dai_probe(dai); 1748c2ecf20Sopenharmony_ci if (ret) 1758c2ecf20Sopenharmony_ci return ret; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci fifo = dai->playback_dma_data; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci fifo->pcm = &fifo_spdif_pcm; 1808c2ecf20Sopenharmony_ci fifo->mem_offset = AIU_MEM_IEC958_START; 1818c2ecf20Sopenharmony_ci fifo->fifo_block = 1; 1828c2ecf20Sopenharmony_ci fifo->pclk = aiu->spdif.clks[PCLK].clk; 1838c2ecf20Sopenharmony_ci fifo->irq = aiu->spdif.irq; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci return 0; 1868c2ecf20Sopenharmony_ci} 187