xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 0102eed57c47371023c03b3b0c564f33d5e94570)
1ba9c949fSKuninori Morimoto /*
2ba9c949fSKuninori Morimoto  * Renesas R-Car SRC support
3ba9c949fSKuninori Morimoto  *
4ba9c949fSKuninori Morimoto  * Copyright (C) 2013 Renesas Solutions Corp.
5ba9c949fSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6ba9c949fSKuninori Morimoto  *
7ba9c949fSKuninori Morimoto  * This program is free software; you can redistribute it and/or modify
8ba9c949fSKuninori Morimoto  * it under the terms of the GNU General Public License version 2 as
9ba9c949fSKuninori Morimoto  * published by the Free Software Foundation.
10ba9c949fSKuninori Morimoto  */
11ba9c949fSKuninori Morimoto #include "rsnd.h"
12ba9c949fSKuninori Morimoto 
138aefda50SKuninori Morimoto #define SRC_NAME "src"
148aefda50SKuninori Morimoto 
15cfcefe01SKuninori Morimoto /* SRCx_STATUS */
16cfcefe01SKuninori Morimoto #define OUF_SRCO	((1 << 12) | (1 << 13))
17cfcefe01SKuninori Morimoto #define OUF_SRCI	((1 <<  9) | (1 <<  8))
18cfcefe01SKuninori Morimoto 
19cfcefe01SKuninori Morimoto /* SCU_SYSTEM_STATUS0/1 */
20cfcefe01SKuninori Morimoto #define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))
21cfcefe01SKuninori Morimoto 
22ba9c949fSKuninori Morimoto struct rsnd_src {
23ba9c949fSKuninori Morimoto 	struct rsnd_mod mod;
24940e9479SKuninori Morimoto 	struct rsnd_mod *dma;
2543cb6954SKuninori Morimoto 	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
2643cb6954SKuninori Morimoto 	struct rsnd_kctrl_cfg_s sync; /* sync convert */
273b7843ffSKuninori Morimoto 	u32 convert_rate; /* sampling rate convert */
28adf6a681SKuninori Morimoto 	int irq;
29ba9c949fSKuninori Morimoto };
30ba9c949fSKuninori Morimoto 
31ba9c949fSKuninori Morimoto #define RSND_SRC_NAME_SIZE 16
32ba9c949fSKuninori Morimoto 
33adf6a681SKuninori Morimoto #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
34232c00b6SKuninori Morimoto #define rsnd_src_to_dma(src) ((src)->dma)
35da599fd3SKuninori Morimoto #define rsnd_src_nr(priv) ((priv)->src_nr)
36ab2049f9SKuninori Morimoto #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
3782e76ed3SKuninori Morimoto 
38ba9c949fSKuninori Morimoto #define rsnd_mod_to_src(_mod)				\
39ba9c949fSKuninori Morimoto 	container_of((_mod), struct rsnd_src, mod)
40ba9c949fSKuninori Morimoto 
41ba9c949fSKuninori Morimoto #define for_each_rsnd_src(pos, priv, i)				\
42ba9c949fSKuninori Morimoto 	for ((i) = 0;						\
43ba9c949fSKuninori Morimoto 	     ((i) < rsnd_src_nr(priv)) &&			\
44ba9c949fSKuninori Morimoto 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
45ba9c949fSKuninori Morimoto 	     i++)
46ba9c949fSKuninori Morimoto 
47ba9c949fSKuninori Morimoto 
48ba9c949fSKuninori Morimoto /*
49ba9c949fSKuninori Morimoto  *		image of SRC (Sampling Rate Converter)
50ba9c949fSKuninori Morimoto  *
51ba9c949fSKuninori Morimoto  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
52ba9c949fSKuninori Morimoto  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
53ba9c949fSKuninori Morimoto  * 44.1kHz <-> +-----+		+-----+		+-------+
54ba9c949fSKuninori Morimoto  * ...
55ba9c949fSKuninori Morimoto  *
56ba9c949fSKuninori Morimoto  */
57ba9c949fSKuninori Morimoto 
58ba9c949fSKuninori Morimoto /*
59ba9c949fSKuninori Morimoto  * src.c is caring...
60ba9c949fSKuninori Morimoto  *
61ba9c949fSKuninori Morimoto  * Gen1
62ba9c949fSKuninori Morimoto  *
63ba9c949fSKuninori Morimoto  * [mem] -> [SRU] -> [SSI]
64ba9c949fSKuninori Morimoto  *        |--------|
65ba9c949fSKuninori Morimoto  *
66ba9c949fSKuninori Morimoto  * Gen2
67ba9c949fSKuninori Morimoto  *
68ba9c949fSKuninori Morimoto  * [mem] -> [SRC] -> [SSIU] -> [SSI]
69ba9c949fSKuninori Morimoto  *        |-----------------|
70ba9c949fSKuninori Morimoto  */
71ba9c949fSKuninori Morimoto 
7298efeeaeSKuninori Morimoto static void rsnd_src_activation(struct rsnd_mod *mod)
73379febfdSKuninori Morimoto {
74379febfdSKuninori Morimoto 	rsnd_mod_write(mod, SRC_SWRSR, 0);
75379febfdSKuninori Morimoto 	rsnd_mod_write(mod, SRC_SWRSR, 1);
76379febfdSKuninori Morimoto }
77379febfdSKuninori Morimoto 
78475a361aSKuninori Morimoto static void rsnd_src_halt(struct rsnd_mod *mod)
79475a361aSKuninori Morimoto {
80475a361aSKuninori Morimoto 	rsnd_mod_write(mod, SRC_SRCIR, 1);
81475a361aSKuninori Morimoto 	rsnd_mod_write(mod, SRC_SWRSR, 0);
82475a361aSKuninori Morimoto }
83475a361aSKuninori Morimoto 
849b99e9a7SKuninori Morimoto static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
859b99e9a7SKuninori Morimoto 					 struct rsnd_mod *mod)
8672adc61fSKuninori Morimoto {
8772adc61fSKuninori Morimoto 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
8872adc61fSKuninori Morimoto 	int is_play = rsnd_io_is_play(io);
8972adc61fSKuninori Morimoto 
9072adc61fSKuninori Morimoto 	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
9172adc61fSKuninori Morimoto 					mod,
9272adc61fSKuninori Morimoto 					is_play ? "rx" : "tx");
9372adc61fSKuninori Morimoto }
9472adc61fSKuninori Morimoto 
9588c61cffSKuninori Morimoto static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
961a9be9eeSKuninori Morimoto 				 struct rsnd_mod *mod)
9743cb6954SKuninori Morimoto {
9843cb6954SKuninori Morimoto 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
991a9be9eeSKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
10043cb6954SKuninori Morimoto 	u32 convert_rate;
10143cb6954SKuninori Morimoto 
10243cb6954SKuninori Morimoto 	if (!runtime)
10343cb6954SKuninori Morimoto 		return 0;
10443cb6954SKuninori Morimoto 
105ab2049f9SKuninori Morimoto 	if (!rsnd_src_sync_is_enabled(mod))
10643cb6954SKuninori Morimoto 		return src->convert_rate;
10743cb6954SKuninori Morimoto 
10843cb6954SKuninori Morimoto 	convert_rate = src->sync.val;
10943cb6954SKuninori Morimoto 
11043cb6954SKuninori Morimoto 	if (!convert_rate)
11143cb6954SKuninori Morimoto 		convert_rate = src->convert_rate;
11243cb6954SKuninori Morimoto 
11343cb6954SKuninori Morimoto 	if (!convert_rate)
11443cb6954SKuninori Morimoto 		convert_rate = runtime->rate;
11543cb6954SKuninori Morimoto 
11643cb6954SKuninori Morimoto 	return convert_rate;
11743cb6954SKuninori Morimoto }
11843cb6954SKuninori Morimoto 
119cbf1494fSKuninori Morimoto unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
120ba9c949fSKuninori Morimoto 			       struct rsnd_dai_stream *io,
121cbf1494fSKuninori Morimoto 			       int is_in)
122ba9c949fSKuninori Morimoto {
123b1eac430SKuninori Morimoto 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
124cbf1494fSKuninori Morimoto 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
125b1eac430SKuninori Morimoto 	unsigned int rate = 0;
126cbf1494fSKuninori Morimoto 	int is_play = rsnd_io_is_play(io);
127cbf1494fSKuninori Morimoto 
128cbf1494fSKuninori Morimoto 	/*
129cbf1494fSKuninori Morimoto 	 *
130cbf1494fSKuninori Morimoto 	 * Playback
131cbf1494fSKuninori Morimoto 	 * runtime_rate -> [SRC] -> convert_rate
132cbf1494fSKuninori Morimoto 	 *
133cbf1494fSKuninori Morimoto 	 * Capture
134cbf1494fSKuninori Morimoto 	 * convert_rate -> [SRC] -> runtime_rate
135cbf1494fSKuninori Morimoto 	 */
136cbf1494fSKuninori Morimoto 
137cbf1494fSKuninori Morimoto 	if (is_play == is_in)
138cbf1494fSKuninori Morimoto 		return runtime->rate;
139ba9c949fSKuninori Morimoto 
140ba9c949fSKuninori Morimoto 	/*
141ba9c949fSKuninori Morimoto 	 * return convert rate if SRC is used,
142ba9c949fSKuninori Morimoto 	 * otherwise, return runtime->rate as usual
143ba9c949fSKuninori Morimoto 	 */
1441a9be9eeSKuninori Morimoto 	if (src_mod)
1451a9be9eeSKuninori Morimoto 		rate = rsnd_src_convert_rate(io, src_mod);
146b1eac430SKuninori Morimoto 
147ba9c949fSKuninori Morimoto 	if (!rate)
148ba9c949fSKuninori Morimoto 		rate = runtime->rate;
149ba9c949fSKuninori Morimoto 
150ba9c949fSKuninori Morimoto 	return rate;
151ba9c949fSKuninori Morimoto }
152ba9c949fSKuninori Morimoto 
1533b7843ffSKuninori Morimoto static int rsnd_src_hw_params(struct rsnd_mod *mod,
1542c0fac19SKuninori Morimoto 			      struct rsnd_dai_stream *io,
1553b7843ffSKuninori Morimoto 			      struct snd_pcm_substream *substream,
1563b7843ffSKuninori Morimoto 			      struct snd_pcm_hw_params *fe_params)
1573b7843ffSKuninori Morimoto {
1583b7843ffSKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
1593b7843ffSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1603b7843ffSKuninori Morimoto 
1613b7843ffSKuninori Morimoto 	/*
1623b7843ffSKuninori Morimoto 	 * SRC assumes that it is used under DPCM if user want to use
1633b7843ffSKuninori Morimoto 	 * sampling rate convert. Then, SRC should be FE.
1643b7843ffSKuninori Morimoto 	 * And then, this function will be called *after* BE settings.
1653b7843ffSKuninori Morimoto 	 * this means, each BE already has fixuped hw_params.
1663b7843ffSKuninori Morimoto 	 * see
1673b7843ffSKuninori Morimoto 	 *	dpcm_fe_dai_hw_params()
1683b7843ffSKuninori Morimoto 	 *	dpcm_be_dai_hw_params()
1693b7843ffSKuninori Morimoto 	 */
1703b7843ffSKuninori Morimoto 	if (fe->dai_link->dynamic) {
1713b7843ffSKuninori Morimoto 		int stream = substream->stream;
1723b7843ffSKuninori Morimoto 		struct snd_soc_dpcm *dpcm;
1733b7843ffSKuninori Morimoto 		struct snd_pcm_hw_params *be_params;
1743b7843ffSKuninori Morimoto 
1753b7843ffSKuninori Morimoto 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1763b7843ffSKuninori Morimoto 			be_params = &dpcm->hw_params;
1773b7843ffSKuninori Morimoto 
1783b7843ffSKuninori Morimoto 			if (params_rate(fe_params) != params_rate(be_params))
1793b7843ffSKuninori Morimoto 				src->convert_rate = params_rate(be_params);
1803b7843ffSKuninori Morimoto 		}
1813b7843ffSKuninori Morimoto 	}
1823b7843ffSKuninori Morimoto 
1833b7843ffSKuninori Morimoto 	return 0;
1843b7843ffSKuninori Morimoto }
1853b7843ffSKuninori Morimoto 
18675916f65SKuninori Morimoto static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
18775916f65SKuninori Morimoto 				      struct rsnd_mod *mod)
188ba9c949fSKuninori Morimoto {
18975916f65SKuninori Morimoto 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
190cfcefe01SKuninori Morimoto 	struct device *dev = rsnd_priv_to_dev(priv);
19175916f65SKuninori Morimoto 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
192*0102eed5SKuninori Morimoto 	u32 fin, fout;
19375916f65SKuninori Morimoto 	u32 ifscr, fsrate, adinr;
19475916f65SKuninori Morimoto 	u32 cr, route;
19575916f65SKuninori Morimoto 	u32 bsdsr, bsisr;
19675916f65SKuninori Morimoto 	uint ratio;
197ba9c949fSKuninori Morimoto 
19875916f65SKuninori Morimoto 	if (!runtime)
19975916f65SKuninori Morimoto 		return;
200ba9c949fSKuninori Morimoto 
201*0102eed5SKuninori Morimoto 	fin  = rsnd_src_get_in_rate(priv, io);
202*0102eed5SKuninori Morimoto 	fout = rsnd_src_get_out_rate(priv, io);
203*0102eed5SKuninori Morimoto 
20475916f65SKuninori Morimoto 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
205*0102eed5SKuninori Morimoto 	if (fin == fout)
20675916f65SKuninori Morimoto 		ratio = 0;
207*0102eed5SKuninori Morimoto 	else if (fin > fout)
208*0102eed5SKuninori Morimoto 		ratio = 100 * fin / fout;
20975916f65SKuninori Morimoto 	else
210*0102eed5SKuninori Morimoto 		ratio = 100 * fout / fin;
211cfcefe01SKuninori Morimoto 
21275916f65SKuninori Morimoto 	if (ratio > 600) {
21375916f65SKuninori Morimoto 		dev_err(dev, "FSO/FSI ratio error\n");
21475916f65SKuninori Morimoto 		return;
215ba9c949fSKuninori Morimoto 	}
216ba9c949fSKuninori Morimoto 
217ba9c949fSKuninori Morimoto 	/*
21875916f65SKuninori Morimoto 	 *	SRC_ADINR
219ba9c949fSKuninori Morimoto 	 */
220c45f7263SKuninori Morimoto 	adinr = rsnd_get_adinr_bit(mod, io) |
221eed76bb8SKuninori Morimoto 		rsnd_runtime_channel_original(io);
22275916f65SKuninori Morimoto 
22375916f65SKuninori Morimoto 	/*
22475916f65SKuninori Morimoto 	 *	SRC_IFSCR / SRC_IFSVR
22575916f65SKuninori Morimoto 	 */
22675916f65SKuninori Morimoto 	ifscr = 0;
22775916f65SKuninori Morimoto 	fsrate = 0;
228*0102eed5SKuninori Morimoto 	if (fin != fout) {
22975916f65SKuninori Morimoto 		ifscr = 1;
230*0102eed5SKuninori Morimoto 		fsrate = 0x0400000 / fout * fin;
23175916f65SKuninori Morimoto 	}
23275916f65SKuninori Morimoto 
23375916f65SKuninori Morimoto 	/*
23475916f65SKuninori Morimoto 	 *	SRC_SRCCR / SRC_ROUTE_MODE0
23575916f65SKuninori Morimoto 	 */
23675916f65SKuninori Morimoto 	cr	= 0x00011110;
23775916f65SKuninori Morimoto 	route	= 0x0;
238*0102eed5SKuninori Morimoto 	if (fin != fout) {
23975916f65SKuninori Morimoto 		route	= 0x1;
24075916f65SKuninori Morimoto 
241ab2049f9SKuninori Morimoto 		if (rsnd_src_sync_is_enabled(mod)) {
24275916f65SKuninori Morimoto 			cr |= 0x1;
24375916f65SKuninori Morimoto 			route |= rsnd_io_is_play(io) ?
24475916f65SKuninori Morimoto 				(0x1 << 24) : (0x1 << 25);
24575916f65SKuninori Morimoto 		}
24675916f65SKuninori Morimoto 	}
24775916f65SKuninori Morimoto 
24875916f65SKuninori Morimoto 	/*
24975916f65SKuninori Morimoto 	 * SRC_BSDSR / SRC_BSISR
25075916f65SKuninori Morimoto 	 */
25175916f65SKuninori Morimoto 	switch (rsnd_mod_id(mod)) {
25275916f65SKuninori Morimoto 	case 5:
25375916f65SKuninori Morimoto 	case 6:
25475916f65SKuninori Morimoto 	case 7:
25575916f65SKuninori Morimoto 	case 8:
25675916f65SKuninori Morimoto 		bsdsr = 0x02400000; /* 6 - 1/6 */
25775916f65SKuninori Morimoto 		bsisr = 0x00100060; /* 6 - 1/6 */
25875916f65SKuninori Morimoto 		break;
25975916f65SKuninori Morimoto 	default:
26075916f65SKuninori Morimoto 		bsdsr = 0x01800000; /* 6 - 1/6 */
26175916f65SKuninori Morimoto 		bsisr = 0x00100060 ;/* 6 - 1/6 */
26275916f65SKuninori Morimoto 		break;
26375916f65SKuninori Morimoto 	}
26475916f65SKuninori Morimoto 
2650fbab951SKuninori Morimoto 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
2660fbab951SKuninori Morimoto 
26775916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
26875916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_ADINR, adinr);
26975916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
27075916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
27175916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_SRCCR, cr);
27275916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
27375916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_BSISR, bsisr);
27475916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
27575916f65SKuninori Morimoto 
276bf4e8d7cSKuninori Morimoto 	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
277bf4e8d7cSKuninori Morimoto 	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
27875916f65SKuninori Morimoto 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
27975916f65SKuninori Morimoto 
280*0102eed5SKuninori Morimoto 	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
28175916f65SKuninori Morimoto }
28275916f65SKuninori Morimoto 
283b5b442abSKuninori Morimoto static int rsnd_src_irq(struct rsnd_mod *mod,
284b5b442abSKuninori Morimoto 			struct rsnd_dai_stream *io,
285b5b442abSKuninori Morimoto 			struct rsnd_priv *priv,
286b5b442abSKuninori Morimoto 			int enable)
287cfcefe01SKuninori Morimoto {
288cfcefe01SKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
289cfcefe01SKuninori Morimoto 	u32 sys_int_val, int_val, sys_int_mask;
290adf6a681SKuninori Morimoto 	int irq = src->irq;
291cfcefe01SKuninori Morimoto 	int id = rsnd_mod_id(mod);
292cfcefe01SKuninori Morimoto 
293cfcefe01SKuninori Morimoto 	sys_int_val =
294cfcefe01SKuninori Morimoto 	sys_int_mask = OUF_SRC(id);
295cfcefe01SKuninori Morimoto 	int_val = 0x3300;
296cfcefe01SKuninori Morimoto 
297cfcefe01SKuninori Morimoto 	/*
298cfcefe01SKuninori Morimoto 	 * IRQ is not supported on non-DT
299cfcefe01SKuninori Morimoto 	 * see
30075916f65SKuninori Morimoto 	 *	rsnd_src_probe_()
301cfcefe01SKuninori Morimoto 	 */
302cfcefe01SKuninori Morimoto 	if ((irq <= 0) || !enable) {
303cfcefe01SKuninori Morimoto 		sys_int_val = 0;
304cfcefe01SKuninori Morimoto 		int_val = 0;
305cfcefe01SKuninori Morimoto 	}
306cfcefe01SKuninori Morimoto 
3071a1bf58aSKuninori Morimoto 	/*
3081a1bf58aSKuninori Morimoto 	 * WORKAROUND
3091a1bf58aSKuninori Morimoto 	 *
310ab2049f9SKuninori Morimoto 	 * ignore over flow error when rsnd_src_sync_is_enabled()
3111a1bf58aSKuninori Morimoto 	 */
312ab2049f9SKuninori Morimoto 	if (rsnd_src_sync_is_enabled(mod))
3131a1bf58aSKuninori Morimoto 		sys_int_val = sys_int_val & 0xffff;
3141a1bf58aSKuninori Morimoto 
315cfcefe01SKuninori Morimoto 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
316cfcefe01SKuninori Morimoto 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
317cfcefe01SKuninori Morimoto 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
318b5b442abSKuninori Morimoto 
319b5b442abSKuninori Morimoto 	return 0;
320cfcefe01SKuninori Morimoto }
321cfcefe01SKuninori Morimoto 
3228cc225f7SKuninori Morimoto static void rsnd_src_status_clear(struct rsnd_mod *mod)
323cfcefe01SKuninori Morimoto {
324cfcefe01SKuninori Morimoto 	u32 val = OUF_SRC(rsnd_mod_id(mod));
325cfcefe01SKuninori Morimoto 
326cfcefe01SKuninori Morimoto 	rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
327cfcefe01SKuninori Morimoto 	rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
328cfcefe01SKuninori Morimoto }
329cfcefe01SKuninori Morimoto 
3306a25c8daSKuninori Morimoto static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
331cfcefe01SKuninori Morimoto {
3321a1bf58aSKuninori Morimoto 	u32 val0, val1;
333cfcefe01SKuninori Morimoto 	bool ret = false;
334cfcefe01SKuninori Morimoto 
3351a1bf58aSKuninori Morimoto 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
3361a1bf58aSKuninori Morimoto 
3371a1bf58aSKuninori Morimoto 	/*
3381a1bf58aSKuninori Morimoto 	 * WORKAROUND
3391a1bf58aSKuninori Morimoto 	 *
340ab2049f9SKuninori Morimoto 	 * ignore over flow error when rsnd_src_sync_is_enabled()
3411a1bf58aSKuninori Morimoto 	 */
342ab2049f9SKuninori Morimoto 	if (rsnd_src_sync_is_enabled(mod))
3431a1bf58aSKuninori Morimoto 		val0 = val0 & 0xffff;
3441a1bf58aSKuninori Morimoto 
3451a1bf58aSKuninori Morimoto 	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
3466a25c8daSKuninori Morimoto 	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
347cfcefe01SKuninori Morimoto 		ret = true;
348cfcefe01SKuninori Morimoto 
349cfcefe01SKuninori Morimoto 	return ret;
350cfcefe01SKuninori Morimoto }
351cfcefe01SKuninori Morimoto 
35275916f65SKuninori Morimoto static int rsnd_src_start(struct rsnd_mod *mod,
353497debaaSKuninori Morimoto 			  struct rsnd_dai_stream *io,
354497debaaSKuninori Morimoto 			  struct rsnd_priv *priv)
355cfcefe01SKuninori Morimoto {
3561a1bf58aSKuninori Morimoto 	u32 val;
3571a1bf58aSKuninori Morimoto 
3581a1bf58aSKuninori Morimoto 	/*
3591a1bf58aSKuninori Morimoto 	 * WORKAROUND
3601a1bf58aSKuninori Morimoto 	 *
3611a1bf58aSKuninori Morimoto 	 * Enable SRC output if you want to use sync convert together with DVC
3621a1bf58aSKuninori Morimoto 	 */
363ab2049f9SKuninori Morimoto 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
3641a1bf58aSKuninori Morimoto 		0x01 : 0x11;
365cfcefe01SKuninori Morimoto 
366cfcefe01SKuninori Morimoto 	rsnd_mod_write(mod, SRC_CTRL, val);
367cfcefe01SKuninori Morimoto 
36875916f65SKuninori Morimoto 	return 0;
36975916f65SKuninori Morimoto }
370cfcefe01SKuninori Morimoto 
37175916f65SKuninori Morimoto static int rsnd_src_stop(struct rsnd_mod *mod,
37275916f65SKuninori Morimoto 			 struct rsnd_dai_stream *io,
37375916f65SKuninori Morimoto 			 struct rsnd_priv *priv)
37475916f65SKuninori Morimoto {
37531739a68SKuninori Morimoto 	rsnd_mod_write(mod, SRC_CTRL, 0);
376cfcefe01SKuninori Morimoto 
377cfcefe01SKuninori Morimoto 	return 0;
378cfcefe01SKuninori Morimoto }
379cfcefe01SKuninori Morimoto 
38075916f65SKuninori Morimoto static int rsnd_src_init(struct rsnd_mod *mod,
381497debaaSKuninori Morimoto 			 struct rsnd_dai_stream *io,
382497debaaSKuninori Morimoto 			 struct rsnd_priv *priv)
383cfcefe01SKuninori Morimoto {
38475916f65SKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
385cfcefe01SKuninori Morimoto 
38675916f65SKuninori Morimoto 	rsnd_mod_power_on(mod);
387cfcefe01SKuninori Morimoto 
38898efeeaeSKuninori Morimoto 	rsnd_src_activation(mod);
389cfcefe01SKuninori Morimoto 
39075916f65SKuninori Morimoto 	rsnd_src_set_convert_rate(io, mod);
39175916f65SKuninori Morimoto 
3928cc225f7SKuninori Morimoto 	rsnd_src_status_clear(mod);
39375916f65SKuninori Morimoto 
39475916f65SKuninori Morimoto 	/* reset sync convert_rate */
39575916f65SKuninori Morimoto 	src->sync.val = 0;
39675916f65SKuninori Morimoto 
39775916f65SKuninori Morimoto 	return 0;
398cfcefe01SKuninori Morimoto }
399cfcefe01SKuninori Morimoto 
40075916f65SKuninori Morimoto static int rsnd_src_quit(struct rsnd_mod *mod,
401b761bf27SKuninori Morimoto 			 struct rsnd_dai_stream *io,
402b761bf27SKuninori Morimoto 			 struct rsnd_priv *priv)
403b761bf27SKuninori Morimoto {
40475916f65SKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
40575916f65SKuninori Morimoto 
406475a361aSKuninori Morimoto 	rsnd_src_halt(mod);
407475a361aSKuninori Morimoto 
40875916f65SKuninori Morimoto 	rsnd_mod_power_off(mod);
40975916f65SKuninori Morimoto 
41075916f65SKuninori Morimoto 	src->convert_rate = 0;
41175916f65SKuninori Morimoto 
41275916f65SKuninori Morimoto 	/* reset sync convert_rate */
41375916f65SKuninori Morimoto 	src->sync.val = 0;
41475916f65SKuninori Morimoto 
41575916f65SKuninori Morimoto 	return 0;
416b761bf27SKuninori Morimoto }
417b761bf27SKuninori Morimoto 
41875916f65SKuninori Morimoto static void __rsnd_src_interrupt(struct rsnd_mod *mod,
41988c61cffSKuninori Morimoto 				 struct rsnd_dai_stream *io)
420cfcefe01SKuninori Morimoto {
42102299d98SKuninori Morimoto 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
4226a25c8daSKuninori Morimoto 	bool stop = false;
423cfcefe01SKuninori Morimoto 
42402299d98SKuninori Morimoto 	spin_lock(&priv->lock);
42502299d98SKuninori Morimoto 
42602299d98SKuninori Morimoto 	/* ignore all cases if not working */
427d5bbe7deSKuninori Morimoto 	if (!rsnd_io_is_working(io))
42875916f65SKuninori Morimoto 		goto rsnd_src_interrupt_out;
429cfcefe01SKuninori Morimoto 
4306a25c8daSKuninori Morimoto 	if (rsnd_src_error_occurred(mod))
4316a25c8daSKuninori Morimoto 		stop = true;
43288c61cffSKuninori Morimoto 
4338cc225f7SKuninori Morimoto 	rsnd_src_status_clear(mod);
43475916f65SKuninori Morimoto rsnd_src_interrupt_out:
4358cc225f7SKuninori Morimoto 
43602299d98SKuninori Morimoto 	spin_unlock(&priv->lock);
4376a25c8daSKuninori Morimoto 
4386a25c8daSKuninori Morimoto 	if (stop)
4396a25c8daSKuninori Morimoto 		snd_pcm_stop_xrun(io->substream);
44088c61cffSKuninori Morimoto }
44188c61cffSKuninori Morimoto 
44275916f65SKuninori Morimoto static irqreturn_t rsnd_src_interrupt(int irq, void *data)
44388c61cffSKuninori Morimoto {
44488c61cffSKuninori Morimoto 	struct rsnd_mod *mod = data;
44588c61cffSKuninori Morimoto 
44675916f65SKuninori Morimoto 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
447cfcefe01SKuninori Morimoto 
448cfcefe01SKuninori Morimoto 	return IRQ_HANDLED;
449cfcefe01SKuninori Morimoto }
450cfcefe01SKuninori Morimoto 
45175916f65SKuninori Morimoto static int rsnd_src_probe_(struct rsnd_mod *mod,
4522c0fac19SKuninori Morimoto 			   struct rsnd_dai_stream *io,
453690602fcSKuninori Morimoto 			   struct rsnd_priv *priv)
454ba9c949fSKuninori Morimoto {
455ba9c949fSKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
456ba9c949fSKuninori Morimoto 	struct device *dev = rsnd_priv_to_dev(priv);
457adf6a681SKuninori Morimoto 	int irq = src->irq;
458ba9c949fSKuninori Morimoto 	int ret;
459ba9c949fSKuninori Morimoto 
460cfcefe01SKuninori Morimoto 	if (irq > 0) {
461cfcefe01SKuninori Morimoto 		/*
462cfcefe01SKuninori Morimoto 		 * IRQ is not supported on non-DT
463cfcefe01SKuninori Morimoto 		 * see
464b5b442abSKuninori Morimoto 		 *	rsnd_src_irq()
465cfcefe01SKuninori Morimoto 		 */
466cfcefe01SKuninori Morimoto 		ret = devm_request_irq(dev, irq,
46775916f65SKuninori Morimoto 				       rsnd_src_interrupt,
468cfcefe01SKuninori Morimoto 				       IRQF_SHARED,
469cfcefe01SKuninori Morimoto 				       dev_name(dev), mod);
470cfcefe01SKuninori Morimoto 		if (ret)
471b543b52aSKuninori Morimoto 			return ret;
472cfcefe01SKuninori Morimoto 	}
473cfcefe01SKuninori Morimoto 
474355cb84fSKuninori Morimoto 	ret = rsnd_dma_attach(io, mod, &src->dma, 0);
475cfcefe01SKuninori Morimoto 
476cfcefe01SKuninori Morimoto 	return ret;
477ba9c949fSKuninori Morimoto }
478ba9c949fSKuninori Morimoto 
47975916f65SKuninori Morimoto static int rsnd_src_pcm_new(struct rsnd_mod *mod,
4802c0fac19SKuninori Morimoto 			    struct rsnd_dai_stream *io,
48143cb6954SKuninori Morimoto 			    struct snd_soc_pcm_runtime *rtd)
48243cb6954SKuninori Morimoto {
48343cb6954SKuninori Morimoto 	struct rsnd_src *src = rsnd_mod_to_src(mod);
48443cb6954SKuninori Morimoto 	int ret;
48543cb6954SKuninori Morimoto 
48643cb6954SKuninori Morimoto 	/*
48743cb6954SKuninori Morimoto 	 * enable SRC sync convert if possible
48843cb6954SKuninori Morimoto 	 */
48943cb6954SKuninori Morimoto 
49043cb6954SKuninori Morimoto 	/*
49161a219feSKuninori Morimoto 	 * It can't use SRC Synchronous convert
49261a219feSKuninori Morimoto 	 * when Capture if it uses CMD
4937115cb91SKuninori Morimoto 	 */
49461a219feSKuninori Morimoto 	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
4957115cb91SKuninori Morimoto 		return 0;
4967115cb91SKuninori Morimoto 
4977115cb91SKuninori Morimoto 	/*
49843cb6954SKuninori Morimoto 	 * enable sync convert
49943cb6954SKuninori Morimoto 	 */
500b65a7cccSKuninori Morimoto 	ret = rsnd_kctrl_new_s(mod, io, rtd,
50143cb6954SKuninori Morimoto 			       rsnd_io_is_play(io) ?
50243cb6954SKuninori Morimoto 			       "SRC Out Rate Switch" :
50343cb6954SKuninori Morimoto 			       "SRC In Rate Switch",
50475916f65SKuninori Morimoto 			       rsnd_src_set_convert_rate,
50543cb6954SKuninori Morimoto 			       &src->sen, 1);
50643cb6954SKuninori Morimoto 	if (ret < 0)
50743cb6954SKuninori Morimoto 		return ret;
50843cb6954SKuninori Morimoto 
509b65a7cccSKuninori Morimoto 	ret = rsnd_kctrl_new_s(mod, io, rtd,
51043cb6954SKuninori Morimoto 			       rsnd_io_is_play(io) ?
51143cb6954SKuninori Morimoto 			       "SRC Out Rate" :
51243cb6954SKuninori Morimoto 			       "SRC In Rate",
51375916f65SKuninori Morimoto 			       rsnd_src_set_convert_rate,
51443cb6954SKuninori Morimoto 			       &src->sync, 192000);
51543cb6954SKuninori Morimoto 
51643cb6954SKuninori Morimoto 	return ret;
51743cb6954SKuninori Morimoto }
51843cb6954SKuninori Morimoto 
51975916f65SKuninori Morimoto static struct rsnd_mod_ops rsnd_src_ops = {
5208aefda50SKuninori Morimoto 	.name	= SRC_NAME,
52172adc61fSKuninori Morimoto 	.dma_req = rsnd_src_dma_req,
52275916f65SKuninori Morimoto 	.probe	= rsnd_src_probe_,
52375916f65SKuninori Morimoto 	.init	= rsnd_src_init,
52475916f65SKuninori Morimoto 	.quit	= rsnd_src_quit,
52575916f65SKuninori Morimoto 	.start	= rsnd_src_start,
52675916f65SKuninori Morimoto 	.stop	= rsnd_src_stop,
527b5b442abSKuninori Morimoto 	.irq	= rsnd_src_irq,
5283b7843ffSKuninori Morimoto 	.hw_params = rsnd_src_hw_params,
52975916f65SKuninori Morimoto 	.pcm_new = rsnd_src_pcm_new,
530ba9c949fSKuninori Morimoto };
531ba9c949fSKuninori Morimoto 
532ba9c949fSKuninori Morimoto struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
533ba9c949fSKuninori Morimoto {
534ba9c949fSKuninori Morimoto 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
535ba9c949fSKuninori Morimoto 		id = 0;
536ba9c949fSKuninori Morimoto 
537adf6a681SKuninori Morimoto 	return rsnd_mod_get(rsnd_src_get(priv, id));
53890e8e50fSKuninori Morimoto }
53990e8e50fSKuninori Morimoto 
5402ea6b074SKuninori Morimoto int rsnd_src_probe(struct rsnd_priv *priv)
541ba9c949fSKuninori Morimoto {
542adf6a681SKuninori Morimoto 	struct device_node *node;
543adf6a681SKuninori Morimoto 	struct device_node *np;
544ba9c949fSKuninori Morimoto 	struct device *dev = rsnd_priv_to_dev(priv);
545ba9c949fSKuninori Morimoto 	struct rsnd_src *src;
546ba9c949fSKuninori Morimoto 	struct clk *clk;
547ba9c949fSKuninori Morimoto 	char name[RSND_SRC_NAME_SIZE];
5482f78dd7fSKuninori Morimoto 	int i, nr, ret;
549ba9c949fSKuninori Morimoto 
550e8e7b7bdSKuninori Morimoto 	/* This driver doesn't support Gen1 at this point */
551e8e7b7bdSKuninori Morimoto 	if (rsnd_is_gen1(priv))
552e8e7b7bdSKuninori Morimoto 		return 0;
553033e7ed8SKuninori Morimoto 
554adf6a681SKuninori Morimoto 	node = rsnd_src_of_node(priv);
555adf6a681SKuninori Morimoto 	if (!node)
556adf6a681SKuninori Morimoto 		return 0; /* not used is not error */
55790e8e50fSKuninori Morimoto 
558adf6a681SKuninori Morimoto 	nr = of_get_child_count(node);
559adf6a681SKuninori Morimoto 	if (!nr) {
560adf6a681SKuninori Morimoto 		ret = -EINVAL;
561adf6a681SKuninori Morimoto 		goto rsnd_src_probe_done;
562adf6a681SKuninori Morimoto 	}
563ba9c949fSKuninori Morimoto 
564ba9c949fSKuninori Morimoto 	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
565adf6a681SKuninori Morimoto 	if (!src) {
566adf6a681SKuninori Morimoto 		ret = -ENOMEM;
567adf6a681SKuninori Morimoto 		goto rsnd_src_probe_done;
568adf6a681SKuninori Morimoto 	}
569ba9c949fSKuninori Morimoto 
570ba9c949fSKuninori Morimoto 	priv->src_nr	= nr;
571ba9c949fSKuninori Morimoto 	priv->src	= src;
572ba9c949fSKuninori Morimoto 
573adf6a681SKuninori Morimoto 	i = 0;
574adf6a681SKuninori Morimoto 	for_each_child_of_node(node, np) {
575adf6a681SKuninori Morimoto 		src = rsnd_src_get(priv, i);
576adf6a681SKuninori Morimoto 
5778aefda50SKuninori Morimoto 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
5788aefda50SKuninori Morimoto 			 SRC_NAME, i);
579ba9c949fSKuninori Morimoto 
580adf6a681SKuninori Morimoto 		src->irq = irq_of_parse_and_map(np, 0);
581adf6a681SKuninori Morimoto 		if (!src->irq) {
582adf6a681SKuninori Morimoto 			ret = -EINVAL;
583adf6a681SKuninori Morimoto 			goto rsnd_src_probe_done;
584adf6a681SKuninori Morimoto 		}
585ba9c949fSKuninori Morimoto 
586adf6a681SKuninori Morimoto 		clk = devm_clk_get(dev, name);
587adf6a681SKuninori Morimoto 		if (IS_ERR(clk)) {
588adf6a681SKuninori Morimoto 			ret = PTR_ERR(clk);
589adf6a681SKuninori Morimoto 			goto rsnd_src_probe_done;
590adf6a681SKuninori Morimoto 		}
591ba9c949fSKuninori Morimoto 
592e8e7b7bdSKuninori Morimoto 		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
5935ba17b42SKuninori Morimoto 				    &rsnd_src_ops, clk, rsnd_mod_get_status,
5945ba17b42SKuninori Morimoto 				    RSND_MOD_SRC, i);
5952f78dd7fSKuninori Morimoto 		if (ret)
596adf6a681SKuninori Morimoto 			goto rsnd_src_probe_done;
597adf6a681SKuninori Morimoto 
598adf6a681SKuninori Morimoto 		i++;
599ba9c949fSKuninori Morimoto 	}
600ba9c949fSKuninori Morimoto 
601adf6a681SKuninori Morimoto 	ret = 0;
602adf6a681SKuninori Morimoto 
603adf6a681SKuninori Morimoto rsnd_src_probe_done:
604adf6a681SKuninori Morimoto 	of_node_put(node);
605adf6a681SKuninori Morimoto 
606adf6a681SKuninori Morimoto 	return ret;
607ba9c949fSKuninori Morimoto }
6082f78dd7fSKuninori Morimoto 
6092ea6b074SKuninori Morimoto void rsnd_src_remove(struct rsnd_priv *priv)
6102f78dd7fSKuninori Morimoto {
6112f78dd7fSKuninori Morimoto 	struct rsnd_src *src;
6122f78dd7fSKuninori Morimoto 	int i;
6132f78dd7fSKuninori Morimoto 
6142f78dd7fSKuninori Morimoto 	for_each_rsnd_src(src, priv, i) {
615b76e218aSKuninori Morimoto 		rsnd_mod_quit(rsnd_mod_get(src));
6162f78dd7fSKuninori Morimoto 	}
6172f78dd7fSKuninori Morimoto }
618