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/bitfield.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
98c2ecf20Sopenharmony_ci#include <sound/soc.h>
108c2ecf20Sopenharmony_ci#include <sound/soc-dai.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "aiu.h"
138c2ecf20Sopenharmony_ci#include "aiu-fifo.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define AIU_I2S_SOURCE_DESC_MODE_8CH	BIT(0)
168c2ecf20Sopenharmony_ci#define AIU_I2S_SOURCE_DESC_MODE_24BIT	BIT(5)
178c2ecf20Sopenharmony_ci#define AIU_I2S_SOURCE_DESC_MODE_32BIT	BIT(9)
188c2ecf20Sopenharmony_ci#define AIU_I2S_SOURCE_DESC_MODE_SPLIT	BIT(11)
198c2ecf20Sopenharmony_ci#define AIU_MEM_I2S_MASKS_IRQ_BLOCK	GENMASK(31, 16)
208c2ecf20Sopenharmony_ci#define AIU_MEM_I2S_CONTROL_MODE_16BIT	BIT(6)
218c2ecf20Sopenharmony_ci#define AIU_MEM_I2S_BUF_CNTL_INIT	BIT(0)
228c2ecf20Sopenharmony_ci#define AIU_RST_SOFT_I2S_FAST		BIT(0)
238c2ecf20Sopenharmony_ci#define AIU_I2S_MISC_HOLD_EN		BIT(2)
248c2ecf20Sopenharmony_ci#define AIU_I2S_MISC_FORCE_LEFT_RIGHT	BIT(4)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define AIU_FIFO_I2S_BLOCK		256
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic struct snd_pcm_hardware fifo_i2s_pcm = {
298c2ecf20Sopenharmony_ci	.info = (SNDRV_PCM_INFO_INTERLEAVED |
308c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_MMAP |
318c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_MMAP_VALID |
328c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_PAUSE),
338c2ecf20Sopenharmony_ci	.formats = AIU_FORMATS,
348c2ecf20Sopenharmony_ci	.rate_min = 5512,
358c2ecf20Sopenharmony_ci	.rate_max = 192000,
368c2ecf20Sopenharmony_ci	.channels_min = 2,
378c2ecf20Sopenharmony_ci	.channels_max = 8,
388c2ecf20Sopenharmony_ci	.period_bytes_min = AIU_FIFO_I2S_BLOCK,
398c2ecf20Sopenharmony_ci	.period_bytes_max = AIU_FIFO_I2S_BLOCK * USHRT_MAX,
408c2ecf20Sopenharmony_ci	.periods_min = 2,
418c2ecf20Sopenharmony_ci	.periods_max = UINT_MAX,
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* No real justification for this */
448c2ecf20Sopenharmony_ci	.buffer_bytes_max = 1 * 1024 * 1024,
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int aiu_fifo_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
488c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	switch (cmd) {
538c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
548c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
558c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
568c2ecf20Sopenharmony_ci		snd_soc_component_write(component, AIU_RST_SOFT,
578c2ecf20Sopenharmony_ci					AIU_RST_SOFT_I2S_FAST);
588c2ecf20Sopenharmony_ci		snd_soc_component_read(component, AIU_I2S_SYNC);
598c2ecf20Sopenharmony_ci		break;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return aiu_fifo_trigger(substream, cmd, dai);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int aiu_fifo_i2s_prepare(struct snd_pcm_substream *substream,
668c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	ret = aiu_fifo_prepare(substream, dai);
728c2ecf20Sopenharmony_ci	if (ret)
738c2ecf20Sopenharmony_ci		return ret;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component,
768c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_BUF_CNTL,
778c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_BUF_CNTL_INIT,
788c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_BUF_CNTL_INIT);
798c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component,
808c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_BUF_CNTL,
818c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_BUF_CNTL_INIT, 0);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int aiu_fifo_i2s_hw_params(struct snd_pcm_substream *substream,
878c2ecf20Sopenharmony_ci				  struct snd_pcm_hw_params *params,
888c2ecf20Sopenharmony_ci				  struct snd_soc_dai *dai)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
918c2ecf20Sopenharmony_ci	struct aiu_fifo *fifo = dai->playback_dma_data;
928c2ecf20Sopenharmony_ci	unsigned int val;
938c2ecf20Sopenharmony_ci	int ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AIU_I2S_MISC,
968c2ecf20Sopenharmony_ci				      AIU_I2S_MISC_HOLD_EN,
978c2ecf20Sopenharmony_ci				      AIU_I2S_MISC_HOLD_EN);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	ret = aiu_fifo_hw_params(substream, params, dai);
1008c2ecf20Sopenharmony_ci	if (ret)
1018c2ecf20Sopenharmony_ci		return ret;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	switch (params_physical_width(params)) {
1048c2ecf20Sopenharmony_ci	case 16:
1058c2ecf20Sopenharmony_ci		val = AIU_MEM_I2S_CONTROL_MODE_16BIT;
1068c2ecf20Sopenharmony_ci		break;
1078c2ecf20Sopenharmony_ci	case 32:
1088c2ecf20Sopenharmony_ci		val = 0;
1098c2ecf20Sopenharmony_ci		break;
1108c2ecf20Sopenharmony_ci	default:
1118c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported physical width %u\n",
1128c2ecf20Sopenharmony_ci			params_physical_width(params));
1138c2ecf20Sopenharmony_ci		return -EINVAL;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AIU_MEM_I2S_CONTROL,
1178c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_CONTROL_MODE_16BIT,
1188c2ecf20Sopenharmony_ci				      val);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Setup the irq periodicity */
1218c2ecf20Sopenharmony_ci	val = params_period_bytes(params) / fifo->fifo_block;
1228c2ecf20Sopenharmony_ci	val = FIELD_PREP(AIU_MEM_I2S_MASKS_IRQ_BLOCK, val);
1238c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AIU_MEM_I2S_MASKS,
1248c2ecf20Sopenharmony_ci				      AIU_MEM_I2S_MASKS_IRQ_BLOCK, val);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/*
1278c2ecf20Sopenharmony_ci	 * Most (all?) supported SoCs have this bit set by default. The vendor
1288c2ecf20Sopenharmony_ci	 * driver however sets it manually (depending on the version either
1298c2ecf20Sopenharmony_ci	 * while un-setting AIU_I2S_MISC_HOLD_EN or right before that). Follow
1308c2ecf20Sopenharmony_ci	 * the same approach for consistency with the vendor driver.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AIU_I2S_MISC,
1338c2ecf20Sopenharmony_ci				      AIU_I2S_MISC_FORCE_LEFT_RIGHT,
1348c2ecf20Sopenharmony_ci				      AIU_I2S_MISC_FORCE_LEFT_RIGHT);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, AIU_I2S_MISC,
1378c2ecf20Sopenharmony_ci				      AIU_I2S_MISC_HOLD_EN, 0);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciconst struct snd_soc_dai_ops aiu_fifo_i2s_dai_ops = {
1438c2ecf20Sopenharmony_ci	.trigger	= aiu_fifo_i2s_trigger,
1448c2ecf20Sopenharmony_ci	.prepare	= aiu_fifo_i2s_prepare,
1458c2ecf20Sopenharmony_ci	.hw_params	= aiu_fifo_i2s_hw_params,
1468c2ecf20Sopenharmony_ci	.hw_free	= aiu_fifo_hw_free,
1478c2ecf20Sopenharmony_ci	.startup	= aiu_fifo_startup,
1488c2ecf20Sopenharmony_ci	.shutdown	= aiu_fifo_shutdown,
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciint aiu_fifo_i2s_dai_probe(struct snd_soc_dai *dai)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
1548c2ecf20Sopenharmony_ci	struct aiu *aiu = snd_soc_component_get_drvdata(component);
1558c2ecf20Sopenharmony_ci	struct aiu_fifo *fifo;
1568c2ecf20Sopenharmony_ci	int ret;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	ret = aiu_fifo_dai_probe(dai);
1598c2ecf20Sopenharmony_ci	if (ret)
1608c2ecf20Sopenharmony_ci		return ret;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	fifo = dai->playback_dma_data;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	fifo->pcm = &fifo_i2s_pcm;
1658c2ecf20Sopenharmony_ci	fifo->mem_offset = AIU_MEM_I2S_START;
1668c2ecf20Sopenharmony_ci	fifo->fifo_block = AIU_FIFO_I2S_BLOCK;
1678c2ecf20Sopenharmony_ci	fifo->pclk = aiu->i2s.clks[PCLK].clk;
1688c2ecf20Sopenharmony_ci	fifo->irq = aiu->i2s.irq;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
172