18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AMD ALSA SoC PCM Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2016 Advanced Micro Devices, Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "chip_offset_byte.h"
98c2ecf20Sopenharmony_ci#include <sound/pcm.h>
108c2ecf20Sopenharmony_ci#define I2S_SP_INSTANCE                 0x01
118c2ecf20Sopenharmony_ci#define I2S_BT_INSTANCE                 0x02
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define TDM_ENABLE 1
148c2ecf20Sopenharmony_ci#define TDM_DISABLE 0
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define ACP3x_DEVS		4
178c2ecf20Sopenharmony_ci#define ACP3x_PHY_BASE_ADDRESS 0x1240000
188c2ecf20Sopenharmony_ci#define	ACP3x_I2S_MODE	0
198c2ecf20Sopenharmony_ci#define	ACP3x_REG_START	0x1240000
208c2ecf20Sopenharmony_ci#define	ACP3x_REG_END	0x1250200
218c2ecf20Sopenharmony_ci#define ACP3x_I2STDM_REG_START	0x1242400
228c2ecf20Sopenharmony_ci#define ACP3x_I2STDM_REG_END	0x1242410
238c2ecf20Sopenharmony_ci#define ACP3x_BT_TDM_REG_START	0x1242800
248c2ecf20Sopenharmony_ci#define ACP3x_BT_TDM_REG_END	0x1242810
258c2ecf20Sopenharmony_ci#define I2S_MODE	0x04
268c2ecf20Sopenharmony_ci#define	I2S_RX_THRESHOLD	27
278c2ecf20Sopenharmony_ci#define	I2S_TX_THRESHOLD	28
288c2ecf20Sopenharmony_ci#define	BT_TX_THRESHOLD 26
298c2ecf20Sopenharmony_ci#define	BT_RX_THRESHOLD 25
308c2ecf20Sopenharmony_ci#define ACP_ERR_INTR_MASK	29
318c2ecf20Sopenharmony_ci#define ACP3x_POWER_ON 0x00
328c2ecf20Sopenharmony_ci#define ACP3x_POWER_ON_IN_PROGRESS 0x01
338c2ecf20Sopenharmony_ci#define ACP3x_POWER_OFF 0x02
348c2ecf20Sopenharmony_ci#define ACP3x_POWER_OFF_IN_PROGRESS 0x03
358c2ecf20Sopenharmony_ci#define ACP3x_SOFT_RESET__SoftResetAudDone_MASK	0x00010001
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define ACP_SRAM_PTE_OFFSET	0x02050000
388c2ecf20Sopenharmony_ci#define ACP_SRAM_SP_PB_PTE_OFFSET	0x0
398c2ecf20Sopenharmony_ci#define ACP_SRAM_SP_CP_PTE_OFFSET	0x100
408c2ecf20Sopenharmony_ci#define ACP_SRAM_BT_PB_PTE_OFFSET	0x200
418c2ecf20Sopenharmony_ci#define ACP_SRAM_BT_CP_PTE_OFFSET	0x300
428c2ecf20Sopenharmony_ci#define PAGE_SIZE_4K_ENABLE 0x2
438c2ecf20Sopenharmony_ci#define I2S_SP_TX_MEM_WINDOW_START	0x4000000
448c2ecf20Sopenharmony_ci#define I2S_SP_RX_MEM_WINDOW_START	0x4020000
458c2ecf20Sopenharmony_ci#define I2S_BT_TX_MEM_WINDOW_START	0x4040000
468c2ecf20Sopenharmony_ci#define I2S_BT_RX_MEM_WINDOW_START	0x4060000
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define SP_PB_FIFO_ADDR_OFFSET		0x500
498c2ecf20Sopenharmony_ci#define SP_CAPT_FIFO_ADDR_OFFSET	0x700
508c2ecf20Sopenharmony_ci#define BT_PB_FIFO_ADDR_OFFSET		0x900
518c2ecf20Sopenharmony_ci#define BT_CAPT_FIFO_ADDR_OFFSET	0xB00
528c2ecf20Sopenharmony_ci#define PLAYBACK_MIN_NUM_PERIODS    2
538c2ecf20Sopenharmony_ci#define PLAYBACK_MAX_NUM_PERIODS    8
548c2ecf20Sopenharmony_ci#define PLAYBACK_MAX_PERIOD_SIZE    8192
558c2ecf20Sopenharmony_ci#define PLAYBACK_MIN_PERIOD_SIZE    1024
568c2ecf20Sopenharmony_ci#define CAPTURE_MIN_NUM_PERIODS     2
578c2ecf20Sopenharmony_ci#define CAPTURE_MAX_NUM_PERIODS     8
588c2ecf20Sopenharmony_ci#define CAPTURE_MAX_PERIOD_SIZE     8192
598c2ecf20Sopenharmony_ci#define CAPTURE_MIN_PERIOD_SIZE     1024
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
628c2ecf20Sopenharmony_ci#define MIN_BUFFER MAX_BUFFER
638c2ecf20Sopenharmony_ci#define FIFO_SIZE 0x100
648c2ecf20Sopenharmony_ci#define DMA_SIZE 0x40
658c2ecf20Sopenharmony_ci#define FRM_LEN 0x100
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define SLOT_WIDTH_8 0x08
688c2ecf20Sopenharmony_ci#define SLOT_WIDTH_16 0x10
698c2ecf20Sopenharmony_ci#define SLOT_WIDTH_24 0x18
708c2ecf20Sopenharmony_ci#define SLOT_WIDTH_32 0x20
718c2ecf20Sopenharmony_ci#define ACP_PGFSM_CNTL_POWER_ON_MASK	0x01
728c2ecf20Sopenharmony_ci#define ACP_PGFSM_CNTL_POWER_OFF_MASK	0x00
738c2ecf20Sopenharmony_ci#define ACP_PGFSM_STATUS_MASK		0x03
748c2ecf20Sopenharmony_ci#define ACP_POWERED_ON			0x00
758c2ecf20Sopenharmony_ci#define ACP_POWER_ON_IN_PROGRESS	0x01
768c2ecf20Sopenharmony_ci#define ACP_POWERED_OFF			0x02
778c2ecf20Sopenharmony_ci#define ACP_POWER_OFF_IN_PROGRESS	0x03
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define ACP3x_ITER_IRER_SAMP_LEN_MASK	0x38
808c2ecf20Sopenharmony_ci#define ACP_EXT_INTR_STAT_CLEAR_MASK 0xFFFFFFFF
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistruct acp3x_platform_info {
838c2ecf20Sopenharmony_ci	u16 play_i2s_instance;
848c2ecf20Sopenharmony_ci	u16 cap_i2s_instance;
858c2ecf20Sopenharmony_ci	u16 capture_channel;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct i2s_dev_data {
898c2ecf20Sopenharmony_ci	bool tdm_mode;
908c2ecf20Sopenharmony_ci	unsigned int i2s_irq;
918c2ecf20Sopenharmony_ci	u16 i2s_instance;
928c2ecf20Sopenharmony_ci	u32 tdm_fmt;
938c2ecf20Sopenharmony_ci	u32 substream_type;
948c2ecf20Sopenharmony_ci	void __iomem *acp3x_base;
958c2ecf20Sopenharmony_ci	struct snd_pcm_substream *play_stream;
968c2ecf20Sopenharmony_ci	struct snd_pcm_substream *capture_stream;
978c2ecf20Sopenharmony_ci	struct snd_pcm_substream *i2ssp_play_stream;
988c2ecf20Sopenharmony_ci	struct snd_pcm_substream *i2ssp_capture_stream;
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistruct i2s_stream_instance {
1028c2ecf20Sopenharmony_ci	u16 num_pages;
1038c2ecf20Sopenharmony_ci	u16 i2s_instance;
1048c2ecf20Sopenharmony_ci	u16 capture_channel;
1058c2ecf20Sopenharmony_ci	u16 direction;
1068c2ecf20Sopenharmony_ci	u16 channels;
1078c2ecf20Sopenharmony_ci	u32 xfer_resolution;
1088c2ecf20Sopenharmony_ci	u32 val;
1098c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
1108c2ecf20Sopenharmony_ci	u64 bytescount;
1118c2ecf20Sopenharmony_ci	void __iomem *acp3x_base;
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic inline u32 rv_readl(void __iomem *base_addr)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return readl(base_addr - ACP3x_PHY_BASE_ADDRESS);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline void rv_writel(u32 val, void __iomem *base_addr)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	writel(val, base_addr - ACP3x_PHY_BASE_ADDRESS);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic inline u64 acp_get_byte_count(struct i2s_stream_instance *rtd,
1258c2ecf20Sopenharmony_ci							int direction)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u64 byte_count;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1308c2ecf20Sopenharmony_ci		switch (rtd->i2s_instance) {
1318c2ecf20Sopenharmony_ci		case I2S_BT_INSTANCE:
1328c2ecf20Sopenharmony_ci			byte_count = rv_readl(rtd->acp3x_base +
1338c2ecf20Sopenharmony_ci					mmACP_BT_TX_LINEARPOSITIONCNTR_HIGH);
1348c2ecf20Sopenharmony_ci			byte_count |= rv_readl(rtd->acp3x_base +
1358c2ecf20Sopenharmony_ci					mmACP_BT_TX_LINEARPOSITIONCNTR_LOW);
1368c2ecf20Sopenharmony_ci			break;
1378c2ecf20Sopenharmony_ci		case I2S_SP_INSTANCE:
1388c2ecf20Sopenharmony_ci		default:
1398c2ecf20Sopenharmony_ci			byte_count = rv_readl(rtd->acp3x_base +
1408c2ecf20Sopenharmony_ci					mmACP_I2S_TX_LINEARPOSITIONCNTR_HIGH);
1418c2ecf20Sopenharmony_ci			byte_count |= rv_readl(rtd->acp3x_base +
1428c2ecf20Sopenharmony_ci					mmACP_I2S_TX_LINEARPOSITIONCNTR_LOW);
1438c2ecf20Sopenharmony_ci		}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	} else {
1468c2ecf20Sopenharmony_ci		switch (rtd->i2s_instance) {
1478c2ecf20Sopenharmony_ci		case I2S_BT_INSTANCE:
1488c2ecf20Sopenharmony_ci			byte_count = rv_readl(rtd->acp3x_base +
1498c2ecf20Sopenharmony_ci					mmACP_BT_RX_LINEARPOSITIONCNTR_HIGH);
1508c2ecf20Sopenharmony_ci			byte_count |= rv_readl(rtd->acp3x_base +
1518c2ecf20Sopenharmony_ci					mmACP_BT_RX_LINEARPOSITIONCNTR_LOW);
1528c2ecf20Sopenharmony_ci			break;
1538c2ecf20Sopenharmony_ci		case I2S_SP_INSTANCE:
1548c2ecf20Sopenharmony_ci		default:
1558c2ecf20Sopenharmony_ci			byte_count = rv_readl(rtd->acp3x_base +
1568c2ecf20Sopenharmony_ci					mmACP_I2S_RX_LINEARPOSITIONCNTR_HIGH);
1578c2ecf20Sopenharmony_ci			byte_count |= rv_readl(rtd->acp3x_base +
1588c2ecf20Sopenharmony_ci					mmACP_I2S_RX_LINEARPOSITIONCNTR_LOW);
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci	return byte_count;
1628c2ecf20Sopenharmony_ci}
163