18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author:      Vladimir Barinov, <vbarinov@embeddedalley.com>
68c2ecf20Sopenharmony_ci * Copyright:   (C) 2007 MontaVista Software, Inc., <source@mvista.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * DT support	(c) 2016 Petr Kulhavy, Barix AG <petr@barix.com>
98c2ecf20Sopenharmony_ci *		based on davinci-mcasp.c DT support
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * TODO:
128c2ecf20Sopenharmony_ci * on DA850 implement HW FIFOs instead of DMA into DXR and DRR registers
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/device.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <linux/clk.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_data/davinci_asp.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <sound/core.h>
258c2ecf20Sopenharmony_ci#include <sound/pcm.h>
268c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
278c2ecf20Sopenharmony_ci#include <sound/initval.h>
288c2ecf20Sopenharmony_ci#include <sound/soc.h>
298c2ecf20Sopenharmony_ci#include <sound/dmaengine_pcm.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "edma-pcm.h"
328c2ecf20Sopenharmony_ci#include "davinci-i2s.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define DRV_NAME "davinci-i2s"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * NOTE:  terminology here is confusing.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *  - This driver supports the "Audio Serial Port" (ASP),
408c2ecf20Sopenharmony_ci *    found on dm6446, dm355, and other DaVinci chips.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci *  - But it labels it a "Multi-channel Buffered Serial Port"
438c2ecf20Sopenharmony_ci *    (McBSP) as on older chips like the dm642 ... which was
448c2ecf20Sopenharmony_ci *    backward-compatible, possibly explaining that confusion.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci *  - OMAP chips have a controller called McBSP, which is
478c2ecf20Sopenharmony_ci *    incompatible with the DaVinci flavor of McBSP.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci *  - Newer DaVinci chips have a controller called McASP,
508c2ecf20Sopenharmony_ci *    incompatible with ASP and with either McBSP.
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * In short:  this uses ASP to implement I2S, not McBSP.
538c2ecf20Sopenharmony_ci * And it won't be the only DaVinci implemention of I2S.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_DRR_REG	0x00
568c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_DXR_REG	0x04
578c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_REG	0x08
588c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_REG	0x0c
598c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_REG	0x10
608c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SRGR_REG	0x14
618c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_REG	0x24
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_RRST		(1 << 0)
648c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_RINTM(v)	((v) << 4)
658c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_XRST		(1 << 16)
668c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_XINTM(v)	((v) << 20)
678c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_GRST		(1 << 22)
688c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_FRST		(1 << 23)
698c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SPCR_FREE		(1 << 25)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RWDLEN1(v)	((v) << 5)
728c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RFRLEN1(v)	((v) << 8)
738c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RDATDLY(v)	((v) << 16)
748c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RFIG		(1 << 18)
758c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RWDLEN2(v)	((v) << 21)
768c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RFRLEN2(v)	((v) << 24)
778c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_RCR_RPHASE	BIT(31)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XWDLEN1(v)	((v) << 5)
808c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XFRLEN1(v)	((v) << 8)
818c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XDATDLY(v)	((v) << 16)
828c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XFIG		(1 << 18)
838c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XWDLEN2(v)	((v) << 21)
848c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XFRLEN2(v)	((v) << 24)
858c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_XCR_XPHASE	BIT(31)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SRGR_FWID(v)	((v) << 8)
888c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SRGR_FPER(v)	((v) << 16)
898c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SRGR_FSGM		(1 << 28)
908c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_SRGR_CLKSM	BIT(29)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_CLKRP		(1 << 0)
938c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_CLKXP		(1 << 1)
948c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_FSRP		(1 << 2)
958c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_FSXP		(1 << 3)
968c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_SCLKME	(1 << 7)
978c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_CLKRM		(1 << 8)
988c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_CLKXM		(1 << 9)
998c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_FSRM		(1 << 10)
1008c2ecf20Sopenharmony_ci#define DAVINCI_MCBSP_PCR_FSXM		(1 << 11)
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cienum {
1038c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_8 = 0,
1048c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_12,
1058c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_16,
1068c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_20,
1078c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_24,
1088c2ecf20Sopenharmony_ci	DAVINCI_MCBSP_WORD_32,
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = {
1128c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S8]		= 1,
1138c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S16_LE]	= 2,
1148c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S32_LE]	= 4,
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = {
1188c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S8]		= DAVINCI_MCBSP_WORD_8,
1198c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S16_LE]	= DAVINCI_MCBSP_WORD_16,
1208c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S32_LE]	= DAVINCI_MCBSP_WORD_32,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = {
1248c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S8]		= SNDRV_PCM_FORMAT_S16_LE,
1258c2ecf20Sopenharmony_ci	[SNDRV_PCM_FORMAT_S16_LE]	= SNDRV_PCM_FORMAT_S32_LE,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistruct davinci_mcbsp_dev {
1298c2ecf20Sopenharmony_ci	struct device *dev;
1308c2ecf20Sopenharmony_ci	struct snd_dmaengine_dai_dma_data dma_data[2];
1318c2ecf20Sopenharmony_ci	int dma_request[2];
1328c2ecf20Sopenharmony_ci	void __iomem			*base;
1338c2ecf20Sopenharmony_ci#define MOD_DSP_A	0
1348c2ecf20Sopenharmony_ci#define MOD_DSP_B	1
1358c2ecf20Sopenharmony_ci	int				mode;
1368c2ecf20Sopenharmony_ci	u32				pcr;
1378c2ecf20Sopenharmony_ci	struct clk			*clk;
1388c2ecf20Sopenharmony_ci	/*
1398c2ecf20Sopenharmony_ci	 * Combining both channels into 1 element will at least double the
1408c2ecf20Sopenharmony_ci	 * amount of time between servicing the dma channel, increase
1418c2ecf20Sopenharmony_ci	 * effiency, and reduce the chance of overrun/underrun. But,
1428c2ecf20Sopenharmony_ci	 * it will result in the left & right channels being swapped.
1438c2ecf20Sopenharmony_ci	 *
1448c2ecf20Sopenharmony_ci	 * If relabeling the left and right channels is not possible,
1458c2ecf20Sopenharmony_ci	 * you may want to let the codec know to swap them back.
1468c2ecf20Sopenharmony_ci	 *
1478c2ecf20Sopenharmony_ci	 * It may allow x10 the amount of time to service dma requests,
1488c2ecf20Sopenharmony_ci	 * if the codec is master and is using an unnecessarily fast bit clock
1498c2ecf20Sopenharmony_ci	 * (ie. tlvaic23b), independent of the sample rate. So, having an
1508c2ecf20Sopenharmony_ci	 * entire frame at once means it can be serviced at the sample rate
1518c2ecf20Sopenharmony_ci	 * instead of the bit clock rate.
1528c2ecf20Sopenharmony_ci	 *
1538c2ecf20Sopenharmony_ci	 * In the now unlikely case that an underrun still
1548c2ecf20Sopenharmony_ci	 * occurs, both the left and right samples will be repeated
1558c2ecf20Sopenharmony_ci	 * so that no pops are heard, and the left and right channels
1568c2ecf20Sopenharmony_ci	 * won't end up being swapped because of the underrun.
1578c2ecf20Sopenharmony_ci	 */
1588c2ecf20Sopenharmony_ci	unsigned enable_channel_combine:1;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	unsigned int fmt;
1618c2ecf20Sopenharmony_ci	int clk_div;
1628c2ecf20Sopenharmony_ci	int clk_input_pin;
1638c2ecf20Sopenharmony_ci	bool i2s_accurate_sck;
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
1678c2ecf20Sopenharmony_ci					   int reg, u32 val)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	__raw_writel(val, dev->base + reg);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return __raw_readl(dev->base + reg);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
1808c2ecf20Sopenharmony_ci	/* The clock needs to toggle to complete reset.
1818c2ecf20Sopenharmony_ci	 * So, fake it by toggling the clk polarity.
1828c2ecf20Sopenharmony_ci	 */
1838c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
1848c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
1888c2ecf20Sopenharmony_ci		struct snd_pcm_substream *substream)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
1918c2ecf20Sopenharmony_ci	u32 spcr;
1928c2ecf20Sopenharmony_ci	u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* Enable transmitter or receiver */
1958c2ecf20Sopenharmony_ci	spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
1968c2ecf20Sopenharmony_ci	spcr |= mask;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
1998c2ecf20Sopenharmony_ci		/* Start frame sync */
2008c2ecf20Sopenharmony_ci		spcr |= DAVINCI_MCBSP_SPCR_FRST;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	u32 spcr;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* Reset transmitter/receiver and sample rate/frame sync generators */
2108c2ecf20Sopenharmony_ci	spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
2118c2ecf20Sopenharmony_ci	spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
2128c2ecf20Sopenharmony_ci	spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
2138c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
2148c2ecf20Sopenharmony_ci	toggle_clock(dev, playback);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci#define DEFAULT_BITPERSAMPLE	16
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
2208c2ecf20Sopenharmony_ci				   unsigned int fmt)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
2238c2ecf20Sopenharmony_ci	unsigned int pcr;
2248c2ecf20Sopenharmony_ci	unsigned int srgr;
2258c2ecf20Sopenharmony_ci	bool inv_fs = false;
2268c2ecf20Sopenharmony_ci	/* Attention srgr is updated by hw_params! */
2278c2ecf20Sopenharmony_ci	srgr = DAVINCI_MCBSP_SRGR_FSGM |
2288c2ecf20Sopenharmony_ci		DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
2298c2ecf20Sopenharmony_ci		DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	dev->fmt = fmt;
2328c2ecf20Sopenharmony_ci	/* set master/slave audio interface */
2338c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2348c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
2358c2ecf20Sopenharmony_ci		/* cpu is master */
2368c2ecf20Sopenharmony_ci		pcr = DAVINCI_MCBSP_PCR_FSXM |
2378c2ecf20Sopenharmony_ci			DAVINCI_MCBSP_PCR_FSRM |
2388c2ecf20Sopenharmony_ci			DAVINCI_MCBSP_PCR_CLKXM |
2398c2ecf20Sopenharmony_ci			DAVINCI_MCBSP_PCR_CLKRM;
2408c2ecf20Sopenharmony_ci		break;
2418c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFS:
2428c2ecf20Sopenharmony_ci		pcr = DAVINCI_MCBSP_PCR_FSRM | DAVINCI_MCBSP_PCR_FSXM;
2438c2ecf20Sopenharmony_ci		/*
2448c2ecf20Sopenharmony_ci		 * Selection of the clock input pin that is the
2458c2ecf20Sopenharmony_ci		 * input for the Sample Rate Generator.
2468c2ecf20Sopenharmony_ci		 * McBSP FSR and FSX are driven by the Sample Rate
2478c2ecf20Sopenharmony_ci		 * Generator.
2488c2ecf20Sopenharmony_ci		 */
2498c2ecf20Sopenharmony_ci		switch (dev->clk_input_pin) {
2508c2ecf20Sopenharmony_ci		case MCBSP_CLKS:
2518c2ecf20Sopenharmony_ci			pcr |= DAVINCI_MCBSP_PCR_CLKXM |
2528c2ecf20Sopenharmony_ci				DAVINCI_MCBSP_PCR_CLKRM;
2538c2ecf20Sopenharmony_ci			break;
2548c2ecf20Sopenharmony_ci		case MCBSP_CLKR:
2558c2ecf20Sopenharmony_ci			pcr |= DAVINCI_MCBSP_PCR_SCLKME;
2568c2ecf20Sopenharmony_ci			break;
2578c2ecf20Sopenharmony_ci		default:
2588c2ecf20Sopenharmony_ci			dev_err(dev->dev, "bad clk_input_pin\n");
2598c2ecf20Sopenharmony_ci			return -EINVAL;
2608c2ecf20Sopenharmony_ci		}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci		break;
2638c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
2648c2ecf20Sopenharmony_ci		/* codec is master */
2658c2ecf20Sopenharmony_ci		pcr = 0;
2668c2ecf20Sopenharmony_ci		break;
2678c2ecf20Sopenharmony_ci	default:
2688c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s:bad master\n", __func__);
2698c2ecf20Sopenharmony_ci		return -EINVAL;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* interface format */
2738c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2748c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
2758c2ecf20Sopenharmony_ci		/* Davinci doesn't support TRUE I2S, but some codecs will have
2768c2ecf20Sopenharmony_ci		 * the left and right channels contiguous. This allows
2778c2ecf20Sopenharmony_ci		 * dsp_a mode to be used with an inverted normal frame clk.
2788c2ecf20Sopenharmony_ci		 * If your codec is master and does not have contiguous
2798c2ecf20Sopenharmony_ci		 * channels, then you will have sound on only one channel.
2808c2ecf20Sopenharmony_ci		 * Try using a different mode, or codec as slave.
2818c2ecf20Sopenharmony_ci		 *
2828c2ecf20Sopenharmony_ci		 * The TLV320AIC33 is an example of a codec where this works.
2838c2ecf20Sopenharmony_ci		 * It has a variable bit clock frequency allowing it to have
2848c2ecf20Sopenharmony_ci		 * valid data on every bit clock.
2858c2ecf20Sopenharmony_ci		 *
2868c2ecf20Sopenharmony_ci		 * The TLV320AIC23 is an example of a codec where this does not
2878c2ecf20Sopenharmony_ci		 * work. It has a fixed bit clock frequency with progressively
2888c2ecf20Sopenharmony_ci		 * more empty bit clock slots between channels as the sample
2898c2ecf20Sopenharmony_ci		 * rate is lowered.
2908c2ecf20Sopenharmony_ci		 */
2918c2ecf20Sopenharmony_ci		inv_fs = true;
2928c2ecf20Sopenharmony_ci		fallthrough;
2938c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
2948c2ecf20Sopenharmony_ci		dev->mode = MOD_DSP_A;
2958c2ecf20Sopenharmony_ci		break;
2968c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_B:
2978c2ecf20Sopenharmony_ci		dev->mode = MOD_DSP_B;
2988c2ecf20Sopenharmony_ci		break;
2998c2ecf20Sopenharmony_ci	default:
3008c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s:bad format\n", __func__);
3018c2ecf20Sopenharmony_ci		return -EINVAL;
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
3058c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
3068c2ecf20Sopenharmony_ci		/* CLKRP Receive clock polarity,
3078c2ecf20Sopenharmony_ci		 *	1 - sampled on rising edge of CLKR
3088c2ecf20Sopenharmony_ci		 *	valid on rising edge
3098c2ecf20Sopenharmony_ci		 * CLKXP Transmit clock polarity,
3108c2ecf20Sopenharmony_ci		 *	1 - clocked on falling edge of CLKX
3118c2ecf20Sopenharmony_ci		 *	valid on rising edge
3128c2ecf20Sopenharmony_ci		 * FSRP  Receive frame sync pol, 0 - active high
3138c2ecf20Sopenharmony_ci		 * FSXP  Transmit frame sync pol, 0 - active high
3148c2ecf20Sopenharmony_ci		 */
3158c2ecf20Sopenharmony_ci		pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
3168c2ecf20Sopenharmony_ci		break;
3178c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF:
3188c2ecf20Sopenharmony_ci		/* CLKRP Receive clock polarity,
3198c2ecf20Sopenharmony_ci		 *	0 - sampled on falling edge of CLKR
3208c2ecf20Sopenharmony_ci		 *	valid on falling edge
3218c2ecf20Sopenharmony_ci		 * CLKXP Transmit clock polarity,
3228c2ecf20Sopenharmony_ci		 *	0 - clocked on rising edge of CLKX
3238c2ecf20Sopenharmony_ci		 *	valid on falling edge
3248c2ecf20Sopenharmony_ci		 * FSRP  Receive frame sync pol, 1 - active low
3258c2ecf20Sopenharmony_ci		 * FSXP  Transmit frame sync pol, 1 - active low
3268c2ecf20Sopenharmony_ci		 */
3278c2ecf20Sopenharmony_ci		pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
3288c2ecf20Sopenharmony_ci		break;
3298c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
3308c2ecf20Sopenharmony_ci		/* CLKRP Receive clock polarity,
3318c2ecf20Sopenharmony_ci		 *	1 - sampled on rising edge of CLKR
3328c2ecf20Sopenharmony_ci		 *	valid on rising edge
3338c2ecf20Sopenharmony_ci		 * CLKXP Transmit clock polarity,
3348c2ecf20Sopenharmony_ci		 *	1 - clocked on falling edge of CLKX
3358c2ecf20Sopenharmony_ci		 *	valid on rising edge
3368c2ecf20Sopenharmony_ci		 * FSRP  Receive frame sync pol, 1 - active low
3378c2ecf20Sopenharmony_ci		 * FSXP  Transmit frame sync pol, 1 - active low
3388c2ecf20Sopenharmony_ci		 */
3398c2ecf20Sopenharmony_ci		pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
3408c2ecf20Sopenharmony_ci			DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
3418c2ecf20Sopenharmony_ci		break;
3428c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
3438c2ecf20Sopenharmony_ci		/* CLKRP Receive clock polarity,
3448c2ecf20Sopenharmony_ci		 *	0 - sampled on falling edge of CLKR
3458c2ecf20Sopenharmony_ci		 *	valid on falling edge
3468c2ecf20Sopenharmony_ci		 * CLKXP Transmit clock polarity,
3478c2ecf20Sopenharmony_ci		 *	0 - clocked on rising edge of CLKX
3488c2ecf20Sopenharmony_ci		 *	valid on falling edge
3498c2ecf20Sopenharmony_ci		 * FSRP  Receive frame sync pol, 0 - active high
3508c2ecf20Sopenharmony_ci		 * FSXP  Transmit frame sync pol, 0 - active high
3518c2ecf20Sopenharmony_ci		 */
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	default:
3548c2ecf20Sopenharmony_ci		return -EINVAL;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci	if (inv_fs == true)
3578c2ecf20Sopenharmony_ci		pcr ^= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
3588c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
3598c2ecf20Sopenharmony_ci	dev->pcr = pcr;
3608c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
3658c2ecf20Sopenharmony_ci				int div_id, int div)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (div_id != DAVINCI_MCBSP_CLKGDV)
3708c2ecf20Sopenharmony_ci		return -ENODEV;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	dev->clk_div = div;
3738c2ecf20Sopenharmony_ci	return 0;
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
3778c2ecf20Sopenharmony_ci				 struct snd_pcm_hw_params *params,
3788c2ecf20Sopenharmony_ci				 struct snd_soc_dai *dai)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
3818c2ecf20Sopenharmony_ci	struct snd_interval *i = NULL;
3828c2ecf20Sopenharmony_ci	int mcbsp_word_length, master;
3838c2ecf20Sopenharmony_ci	unsigned int rcr, xcr, srgr, clk_div, freq, framesize;
3848c2ecf20Sopenharmony_ci	u32 spcr;
3858c2ecf20Sopenharmony_ci	snd_pcm_format_t fmt;
3868c2ecf20Sopenharmony_ci	unsigned element_cnt = 1;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* general line settings */
3898c2ecf20Sopenharmony_ci	spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
3908c2ecf20Sopenharmony_ci	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3918c2ecf20Sopenharmony_ci		spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
3928c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
3938c2ecf20Sopenharmony_ci	} else {
3948c2ecf20Sopenharmony_ci		spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
3958c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
3998c2ecf20Sopenharmony_ci	fmt = params_format(params);
4008c2ecf20Sopenharmony_ci	mcbsp_word_length = asp_word_length[fmt];
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	switch (master) {
4038c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
4048c2ecf20Sopenharmony_ci		freq = clk_get_rate(dev->clk);
4058c2ecf20Sopenharmony_ci		srgr = DAVINCI_MCBSP_SRGR_FSGM |
4068c2ecf20Sopenharmony_ci		       DAVINCI_MCBSP_SRGR_CLKSM;
4078c2ecf20Sopenharmony_ci		srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length *
4088c2ecf20Sopenharmony_ci						8 - 1);
4098c2ecf20Sopenharmony_ci		if (dev->i2s_accurate_sck) {
4108c2ecf20Sopenharmony_ci			clk_div = 256;
4118c2ecf20Sopenharmony_ci			do {
4128c2ecf20Sopenharmony_ci				framesize = (freq / (--clk_div)) /
4138c2ecf20Sopenharmony_ci				params->rate_num *
4148c2ecf20Sopenharmony_ci					params->rate_den;
4158c2ecf20Sopenharmony_ci			} while (((framesize < 33) || (framesize > 4095)) &&
4168c2ecf20Sopenharmony_ci				 (clk_div));
4178c2ecf20Sopenharmony_ci			clk_div--;
4188c2ecf20Sopenharmony_ci			srgr |= DAVINCI_MCBSP_SRGR_FPER(framesize - 1);
4198c2ecf20Sopenharmony_ci		} else {
4208c2ecf20Sopenharmony_ci			/* symmetric waveforms */
4218c2ecf20Sopenharmony_ci			clk_div = freq / (mcbsp_word_length * 16) /
4228c2ecf20Sopenharmony_ci				  params->rate_num * params->rate_den;
4238c2ecf20Sopenharmony_ci			srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length *
4248c2ecf20Sopenharmony_ci							16 - 1);
4258c2ecf20Sopenharmony_ci		}
4268c2ecf20Sopenharmony_ci		clk_div &= 0xFF;
4278c2ecf20Sopenharmony_ci		srgr |= clk_div;
4288c2ecf20Sopenharmony_ci		break;
4298c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFS:
4308c2ecf20Sopenharmony_ci		srgr = DAVINCI_MCBSP_SRGR_FSGM;
4318c2ecf20Sopenharmony_ci		clk_div = dev->clk_div - 1;
4328c2ecf20Sopenharmony_ci		srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * 8 - 1);
4338c2ecf20Sopenharmony_ci		srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * 16 - 1);
4348c2ecf20Sopenharmony_ci		clk_div &= 0xFF;
4358c2ecf20Sopenharmony_ci		srgr |= clk_div;
4368c2ecf20Sopenharmony_ci		break;
4378c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
4388c2ecf20Sopenharmony_ci		/* Clock and frame sync given from external sources */
4398c2ecf20Sopenharmony_ci		i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
4408c2ecf20Sopenharmony_ci		srgr = DAVINCI_MCBSP_SRGR_FSGM;
4418c2ecf20Sopenharmony_ci		srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
4428c2ecf20Sopenharmony_ci		pr_debug("%s - %d  FWID set: re-read srgr = %X\n",
4438c2ecf20Sopenharmony_ci			__func__, __LINE__, snd_interval_value(i) - 1);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
4468c2ecf20Sopenharmony_ci		srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
4478c2ecf20Sopenharmony_ci		break;
4488c2ecf20Sopenharmony_ci	default:
4498c2ecf20Sopenharmony_ci		return -EINVAL;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci	davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	rcr = DAVINCI_MCBSP_RCR_RFIG;
4548c2ecf20Sopenharmony_ci	xcr = DAVINCI_MCBSP_XCR_XFIG;
4558c2ecf20Sopenharmony_ci	if (dev->mode == MOD_DSP_B) {
4568c2ecf20Sopenharmony_ci		rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
4578c2ecf20Sopenharmony_ci		xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
4588c2ecf20Sopenharmony_ci	} else {
4598c2ecf20Sopenharmony_ci		rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
4608c2ecf20Sopenharmony_ci		xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci	/* Determine xfer data type */
4638c2ecf20Sopenharmony_ci	fmt = params_format(params);
4648c2ecf20Sopenharmony_ci	if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) {
4658c2ecf20Sopenharmony_ci		printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
4668c2ecf20Sopenharmony_ci		return -EINVAL;
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (params_channels(params) == 2) {
4708c2ecf20Sopenharmony_ci		element_cnt = 2;
4718c2ecf20Sopenharmony_ci		if (double_fmt[fmt] && dev->enable_channel_combine) {
4728c2ecf20Sopenharmony_ci			element_cnt = 1;
4738c2ecf20Sopenharmony_ci			fmt = double_fmt[fmt];
4748c2ecf20Sopenharmony_ci		}
4758c2ecf20Sopenharmony_ci		switch (master) {
4768c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_CBS_CFS:
4778c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_CBS_CFM:
4788c2ecf20Sopenharmony_ci			rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(0);
4798c2ecf20Sopenharmony_ci			xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(0);
4808c2ecf20Sopenharmony_ci			rcr |= DAVINCI_MCBSP_RCR_RPHASE;
4818c2ecf20Sopenharmony_ci			xcr |= DAVINCI_MCBSP_XCR_XPHASE;
4828c2ecf20Sopenharmony_ci			break;
4838c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_CBM_CFM:
4848c2ecf20Sopenharmony_ci		case SND_SOC_DAIFMT_CBM_CFS:
4858c2ecf20Sopenharmony_ci			rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt - 1);
4868c2ecf20Sopenharmony_ci			xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt - 1);
4878c2ecf20Sopenharmony_ci			break;
4888c2ecf20Sopenharmony_ci		default:
4898c2ecf20Sopenharmony_ci			return -EINVAL;
4908c2ecf20Sopenharmony_ci		}
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci	mcbsp_word_length = asp_word_length[fmt];
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	switch (master) {
4958c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
4968c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFM:
4978c2ecf20Sopenharmony_ci		rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(0);
4988c2ecf20Sopenharmony_ci		xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(0);
4998c2ecf20Sopenharmony_ci		break;
5008c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
5018c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFS:
5028c2ecf20Sopenharmony_ci		rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1);
5038c2ecf20Sopenharmony_ci		xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1);
5048c2ecf20Sopenharmony_ci		break;
5058c2ecf20Sopenharmony_ci	default:
5068c2ecf20Sopenharmony_ci		return -EINVAL;
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
5108c2ecf20Sopenharmony_ci		DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
5118c2ecf20Sopenharmony_ci	xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
5128c2ecf20Sopenharmony_ci		DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5158c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
5168c2ecf20Sopenharmony_ci	else
5178c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	pr_debug("%s - %d  srgr=%X\n", __func__, __LINE__, srgr);
5208c2ecf20Sopenharmony_ci	pr_debug("%s - %d  xcr=%X\n", __func__, __LINE__, xcr);
5218c2ecf20Sopenharmony_ci	pr_debug("%s - %d  rcr=%X\n", __func__, __LINE__, rcr);
5228c2ecf20Sopenharmony_ci	return 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int davinci_i2s_prepare(struct snd_pcm_substream *substream,
5268c2ecf20Sopenharmony_ci		struct snd_soc_dai *dai)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
5298c2ecf20Sopenharmony_ci	int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
5308c2ecf20Sopenharmony_ci	u32 spcr;
5318c2ecf20Sopenharmony_ci	u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	davinci_mcbsp_stop(dev, playback);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
5368c2ecf20Sopenharmony_ci	if (spcr & mask) {
5378c2ecf20Sopenharmony_ci		/* start off disabled */
5388c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
5398c2ecf20Sopenharmony_ci					spcr & ~mask);
5408c2ecf20Sopenharmony_ci		toggle_clock(dev, playback);
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci	if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
5438c2ecf20Sopenharmony_ci			DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
5448c2ecf20Sopenharmony_ci		/* Start the sample generator */
5458c2ecf20Sopenharmony_ci		spcr |= DAVINCI_MCBSP_SPCR_GRST;
5468c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (playback) {
5508c2ecf20Sopenharmony_ci		/* Enable the transmitter */
5518c2ecf20Sopenharmony_ci		spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
5528c2ecf20Sopenharmony_ci		spcr |= DAVINCI_MCBSP_SPCR_XRST;
5538c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci		/* wait for any unexpected frame sync error to occur */
5568c2ecf20Sopenharmony_ci		udelay(100);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci		/* Disable the transmitter to clear any outstanding XSYNCERR */
5598c2ecf20Sopenharmony_ci		spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
5608c2ecf20Sopenharmony_ci		spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
5618c2ecf20Sopenharmony_ci		davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
5628c2ecf20Sopenharmony_ci		toggle_clock(dev, playback);
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
5698c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
5728c2ecf20Sopenharmony_ci	int ret = 0;
5738c2ecf20Sopenharmony_ci	int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	switch (cmd) {
5768c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
5778c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
5788c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
5798c2ecf20Sopenharmony_ci		davinci_mcbsp_start(dev, substream);
5808c2ecf20Sopenharmony_ci		break;
5818c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
5828c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
5838c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
5848c2ecf20Sopenharmony_ci		davinci_mcbsp_stop(dev, playback);
5858c2ecf20Sopenharmony_ci		break;
5868c2ecf20Sopenharmony_ci	default:
5878c2ecf20Sopenharmony_ci		ret = -EINVAL;
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci	return ret;
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
5938c2ecf20Sopenharmony_ci		struct snd_soc_dai *dai)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
5968c2ecf20Sopenharmony_ci	int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
5978c2ecf20Sopenharmony_ci	davinci_mcbsp_stop(dev, playback);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci#define DAVINCI_I2S_RATES	SNDRV_PCM_RATE_8000_96000
6018c2ecf20Sopenharmony_ci#define DAVINCI_I2S_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
6028c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_S32_LE)
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops davinci_i2s_dai_ops = {
6058c2ecf20Sopenharmony_ci	.shutdown	= davinci_i2s_shutdown,
6068c2ecf20Sopenharmony_ci	.prepare	= davinci_i2s_prepare,
6078c2ecf20Sopenharmony_ci	.trigger	= davinci_i2s_trigger,
6088c2ecf20Sopenharmony_ci	.hw_params	= davinci_i2s_hw_params,
6098c2ecf20Sopenharmony_ci	.set_fmt	= davinci_i2s_set_dai_fmt,
6108c2ecf20Sopenharmony_ci	.set_clkdiv	= davinci_i2s_dai_set_clkdiv,
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci};
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic int davinci_i2s_dai_probe(struct snd_soc_dai *dai)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
6198c2ecf20Sopenharmony_ci	dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	return 0;
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver davinci_i2s_dai = {
6258c2ecf20Sopenharmony_ci	.probe = davinci_i2s_dai_probe,
6268c2ecf20Sopenharmony_ci	.playback = {
6278c2ecf20Sopenharmony_ci		.channels_min = 2,
6288c2ecf20Sopenharmony_ci		.channels_max = 2,
6298c2ecf20Sopenharmony_ci		.rates = DAVINCI_I2S_RATES,
6308c2ecf20Sopenharmony_ci		.formats = DAVINCI_I2S_FORMATS,
6318c2ecf20Sopenharmony_ci	},
6328c2ecf20Sopenharmony_ci	.capture = {
6338c2ecf20Sopenharmony_ci		.channels_min = 2,
6348c2ecf20Sopenharmony_ci		.channels_max = 2,
6358c2ecf20Sopenharmony_ci		.rates = DAVINCI_I2S_RATES,
6368c2ecf20Sopenharmony_ci		.formats = DAVINCI_I2S_FORMATS,
6378c2ecf20Sopenharmony_ci	},
6388c2ecf20Sopenharmony_ci	.ops = &davinci_i2s_dai_ops,
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci};
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver davinci_i2s_component = {
6438c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
6448c2ecf20Sopenharmony_ci};
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic int davinci_i2s_probe(struct platform_device *pdev)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	struct snd_dmaengine_dai_dma_data *dma_data;
6498c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev;
6508c2ecf20Sopenharmony_ci	struct resource *mem, *res;
6518c2ecf20Sopenharmony_ci	void __iomem *io_base;
6528c2ecf20Sopenharmony_ci	int *dma;
6538c2ecf20Sopenharmony_ci	int ret;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
6568c2ecf20Sopenharmony_ci	if (!mem) {
6578c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev,
6588c2ecf20Sopenharmony_ci			 "\"mpu\" mem resource not found, using index 0\n");
6598c2ecf20Sopenharmony_ci		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6608c2ecf20Sopenharmony_ci		if (!mem) {
6618c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "no mem resource?\n");
6628c2ecf20Sopenharmony_ci			return -ENODEV;
6638c2ecf20Sopenharmony_ci		}
6648c2ecf20Sopenharmony_ci	}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	io_base = devm_ioremap_resource(&pdev->dev, mem);
6678c2ecf20Sopenharmony_ci	if (IS_ERR(io_base))
6688c2ecf20Sopenharmony_ci		return PTR_ERR(io_base);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcbsp_dev),
6718c2ecf20Sopenharmony_ci			   GFP_KERNEL);
6728c2ecf20Sopenharmony_ci	if (!dev)
6738c2ecf20Sopenharmony_ci		return -ENOMEM;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	dev->base = io_base;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/* setup DMA, first TX, then RX */
6788c2ecf20Sopenharmony_ci	dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
6798c2ecf20Sopenharmony_ci	dma_data->addr = (dma_addr_t)(mem->start + DAVINCI_MCBSP_DXR_REG);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
6828c2ecf20Sopenharmony_ci	if (res) {
6838c2ecf20Sopenharmony_ci		dma = &dev->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
6848c2ecf20Sopenharmony_ci		*dma = res->start;
6858c2ecf20Sopenharmony_ci		dma_data->filter_data = dma;
6868c2ecf20Sopenharmony_ci	} else if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
6878c2ecf20Sopenharmony_ci		dma_data->filter_data = "tx";
6888c2ecf20Sopenharmony_ci	} else {
6898c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Missing DMA tx resource\n");
6908c2ecf20Sopenharmony_ci		return -ENODEV;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
6948c2ecf20Sopenharmony_ci	dma_data->addr = (dma_addr_t)(mem->start + DAVINCI_MCBSP_DRR_REG);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
6978c2ecf20Sopenharmony_ci	if (res) {
6988c2ecf20Sopenharmony_ci		dma = &dev->dma_request[SNDRV_PCM_STREAM_CAPTURE];
6998c2ecf20Sopenharmony_ci		*dma = res->start;
7008c2ecf20Sopenharmony_ci		dma_data->filter_data = dma;
7018c2ecf20Sopenharmony_ci	} else if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
7028c2ecf20Sopenharmony_ci		dma_data->filter_data = "rx";
7038c2ecf20Sopenharmony_ci	} else {
7048c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Missing DMA rx resource\n");
7058c2ecf20Sopenharmony_ci		return -ENODEV;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	dev->clk = clk_get(&pdev->dev, NULL);
7098c2ecf20Sopenharmony_ci	if (IS_ERR(dev->clk))
7108c2ecf20Sopenharmony_ci		return -ENODEV;
7118c2ecf20Sopenharmony_ci	ret = clk_enable(dev->clk);
7128c2ecf20Sopenharmony_ci	if (ret)
7138c2ecf20Sopenharmony_ci		goto err_put_clk;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	dev->dev = &pdev->dev;
7168c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, dev);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	ret = snd_soc_register_component(&pdev->dev, &davinci_i2s_component,
7198c2ecf20Sopenharmony_ci					 &davinci_i2s_dai, 1);
7208c2ecf20Sopenharmony_ci	if (ret != 0)
7218c2ecf20Sopenharmony_ci		goto err_release_clk;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	ret = edma_pcm_platform_register(&pdev->dev);
7248c2ecf20Sopenharmony_ci	if (ret) {
7258c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
7268c2ecf20Sopenharmony_ci		goto err_unregister_component;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	return 0;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cierr_unregister_component:
7328c2ecf20Sopenharmony_ci	snd_soc_unregister_component(&pdev->dev);
7338c2ecf20Sopenharmony_cierr_release_clk:
7348c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
7358c2ecf20Sopenharmony_cierr_put_clk:
7368c2ecf20Sopenharmony_ci	clk_put(dev->clk);
7378c2ecf20Sopenharmony_ci	return ret;
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cistatic int davinci_i2s_remove(struct platform_device *pdev)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct davinci_mcbsp_dev *dev = dev_get_drvdata(&pdev->dev);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	snd_soc_unregister_component(&pdev->dev);
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
7478c2ecf20Sopenharmony_ci	clk_put(dev->clk);
7488c2ecf20Sopenharmony_ci	dev->clk = NULL;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	return 0;
7518c2ecf20Sopenharmony_ci}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic const struct of_device_id davinci_i2s_match[] = {
7548c2ecf20Sopenharmony_ci	{ .compatible = "ti,da850-mcbsp" },
7558c2ecf20Sopenharmony_ci	{},
7568c2ecf20Sopenharmony_ci};
7578c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, davinci_i2s_match);
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_cistatic struct platform_driver davinci_mcbsp_driver = {
7608c2ecf20Sopenharmony_ci	.probe		= davinci_i2s_probe,
7618c2ecf20Sopenharmony_ci	.remove		= davinci_i2s_remove,
7628c2ecf20Sopenharmony_ci	.driver		= {
7638c2ecf20Sopenharmony_ci		.name	= "davinci-mcbsp",
7648c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(davinci_i2s_match),
7658c2ecf20Sopenharmony_ci	},
7668c2ecf20Sopenharmony_ci};
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cimodule_platform_driver(davinci_mcbsp_driver);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vladimir Barinov");
7718c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
7728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
773