2 * Renesas R-Car SRC support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #define SRC_NAME "src"
16 #define OUF_SRCO ((1 << 12) | (1 << 13))
17 #define OUF_SRCI ((1 << 9) | (1 << 8))
19 /* SCU_SYSTEM_STATUS0/1 */
20 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
25 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
26 struct rsnd_kctrl_cfg_s sync; /* sync convert */
27 u32 convert_rate; /* sampling rate convert */
31 #define RSND_SRC_NAME_SIZE 16
33 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
34 #define rsnd_src_to_dma(src) ((src)->dma)
35 #define rsnd_src_nr(priv) ((priv)->src_nr)
36 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
38 #define rsnd_mod_to_src(_mod) \
39 container_of((_mod), struct rsnd_src, mod)
41 #define for_each_rsnd_src(pos, priv, i) \
43 ((i) < rsnd_src_nr(priv)) && \
44 ((pos) = (struct rsnd_src *)(priv)->src + i); \
49 * image of SRC (Sampling Rate Converter)
51 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
52 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
53 * 44.1kHz <-> +-----+ +-----+ +-------+
63 * [mem] -> [SRU] -> [SSI]
68 * [mem] -> [SRC] -> [SSIU] -> [SSI]
72 static void rsnd_src_activation(struct rsnd_mod *mod)
74 rsnd_mod_write(mod, SRC_SWRSR, 0);
75 rsnd_mod_write(mod, SRC_SWRSR, 1);
78 static void rsnd_src_halt(struct rsnd_mod *mod)
80 rsnd_mod_write(mod, SRC_SRCIR, 1);
81 rsnd_mod_write(mod, SRC_SWRSR, 0);
84 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
87 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
88 int is_play = rsnd_io_is_play(io);
90 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
92 is_play ? "rx" : "tx");
95 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
98 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
99 struct rsnd_src *src = rsnd_mod_to_src(mod);
105 if (!rsnd_src_sync_is_enabled(mod))
106 return src->convert_rate;
108 convert_rate = src->sync.val;
111 convert_rate = src->convert_rate;
114 convert_rate = runtime->rate;
119 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
120 struct rsnd_dai_stream *io,
123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
124 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
125 unsigned int rate = 0;
126 int is_play = rsnd_io_is_play(io);
131 * runtime_rate -> [SRC] -> convert_rate
134 * convert_rate -> [SRC] -> runtime_rate
137 if (is_play == is_in)
138 return runtime->rate;
141 * return convert rate if SRC is used,
142 * otherwise, return runtime->rate as usual
145 rate = rsnd_src_convert_rate(io, src_mod);
148 rate = runtime->rate;
153 static int rsnd_src_hw_params(struct rsnd_mod *mod,
154 struct rsnd_dai_stream *io,
155 struct snd_pcm_substream *substream,
156 struct snd_pcm_hw_params *fe_params)
158 struct rsnd_src *src = rsnd_mod_to_src(mod);
159 struct snd_soc_pcm_runtime *fe = substream->private_data;
162 * SRC assumes that it is used under DPCM if user want to use
163 * sampling rate convert. Then, SRC should be FE.
164 * And then, this function will be called *after* BE settings.
165 * this means, each BE already has fixuped hw_params.
167 * dpcm_fe_dai_hw_params()
168 * dpcm_be_dai_hw_params()
170 src->convert_rate = 0;
171 if (fe->dai_link->dynamic) {
172 int stream = substream->stream;
173 struct snd_soc_dpcm *dpcm;
174 struct snd_pcm_hw_params *be_params;
176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
177 be_params = &dpcm->hw_params;
179 if (params_rate(fe_params) != params_rate(be_params))
180 src->convert_rate = params_rate(be_params);
187 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
188 struct rsnd_mod *mod)
190 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
191 struct device *dev = rsnd_priv_to_dev(priv);
192 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
193 int is_play = rsnd_io_is_play(io);
196 u32 ifscr, fsrate, adinr;
199 u32 i_busif, o_busif, tmp;
205 fin = rsnd_src_get_in_rate(priv, io);
206 fout = rsnd_src_get_out_rate(priv, io);
208 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
212 ratio = 100 * fin / fout;
214 ratio = 100 * fout / fin;
217 dev_err(dev, "FSO/FSI ratio error\n");
221 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
226 adinr = rsnd_get_adinr_bit(mod, io) |
227 rsnd_runtime_channel_original(io);
230 * SRC_IFSCR / SRC_IFSVR
238 n = (u64)0x0400000 * fin;
244 * SRC_SRCCR / SRC_ROUTE_MODE0
251 if (rsnd_src_sync_is_enabled(mod)) {
253 route |= rsnd_io_is_play(io) ?
254 (0x1 << 24) : (0x1 << 25);
259 * SRC_BSDSR / SRC_BSISR
261 switch (rsnd_mod_id(mod)) {
266 bsdsr = 0x02400000; /* 6 - 1/6 */
267 bsisr = 0x00100060; /* 6 - 1/6 */
270 bsdsr = 0x01800000; /* 6 - 1/6 */
271 bsisr = 0x00100060 ;/* 6 - 1/6 */
276 tmp = rsnd_get_busif_shift(io, mod);
277 i_busif = ( is_play ? tmp : 0) | 1;
278 o_busif = (!is_play ? tmp : 0) | 1;
280 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
282 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
283 rsnd_mod_write(mod, SRC_ADINR, adinr);
284 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
285 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
286 rsnd_mod_write(mod, SRC_SRCCR, cr);
287 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
288 rsnd_mod_write(mod, SRC_BSISR, bsisr);
289 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
291 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
292 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
294 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
296 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
299 static int rsnd_src_irq(struct rsnd_mod *mod,
300 struct rsnd_dai_stream *io,
301 struct rsnd_priv *priv,
304 struct rsnd_src *src = rsnd_mod_to_src(mod);
305 u32 sys_int_val, int_val, sys_int_mask;
307 int id = rsnd_mod_id(mod);
310 sys_int_mask = OUF_SRC(id);
314 * IRQ is not supported on non-DT
318 if ((irq <= 0) || !enable) {
326 * ignore over flow error when rsnd_src_sync_is_enabled()
328 if (rsnd_src_sync_is_enabled(mod))
329 sys_int_val = sys_int_val & 0xffff;
331 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
332 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
333 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
338 static void rsnd_src_status_clear(struct rsnd_mod *mod)
340 u32 val = OUF_SRC(rsnd_mod_id(mod));
342 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
343 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
346 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
351 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
356 * ignore over flow error when rsnd_src_sync_is_enabled()
358 if (rsnd_src_sync_is_enabled(mod))
359 val0 = val0 & 0xffff;
361 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
362 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
368 static int rsnd_src_start(struct rsnd_mod *mod,
369 struct rsnd_dai_stream *io,
370 struct rsnd_priv *priv)
377 * Enable SRC output if you want to use sync convert together with DVC
379 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
382 rsnd_mod_write(mod, SRC_CTRL, val);
387 static int rsnd_src_stop(struct rsnd_mod *mod,
388 struct rsnd_dai_stream *io,
389 struct rsnd_priv *priv)
391 rsnd_mod_write(mod, SRC_CTRL, 0);
396 static int rsnd_src_init(struct rsnd_mod *mod,
397 struct rsnd_dai_stream *io,
398 struct rsnd_priv *priv)
400 struct rsnd_src *src = rsnd_mod_to_src(mod);
402 /* reset sync convert_rate */
405 rsnd_mod_power_on(mod);
407 rsnd_src_activation(mod);
409 rsnd_src_set_convert_rate(io, mod);
411 rsnd_src_status_clear(mod);
416 static int rsnd_src_quit(struct rsnd_mod *mod,
417 struct rsnd_dai_stream *io,
418 struct rsnd_priv *priv)
420 struct rsnd_src *src = rsnd_mod_to_src(mod);
424 rsnd_mod_power_off(mod);
426 /* reset sync convert_rate */
432 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
433 struct rsnd_dai_stream *io)
435 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
438 spin_lock(&priv->lock);
440 /* ignore all cases if not working */
441 if (!rsnd_io_is_working(io))
442 goto rsnd_src_interrupt_out;
444 if (rsnd_src_error_occurred(mod))
447 rsnd_src_status_clear(mod);
448 rsnd_src_interrupt_out:
450 spin_unlock(&priv->lock);
453 snd_pcm_stop_xrun(io->substream);
456 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
458 struct rsnd_mod *mod = data;
460 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
465 static int rsnd_src_probe_(struct rsnd_mod *mod,
466 struct rsnd_dai_stream *io,
467 struct rsnd_priv *priv)
469 struct rsnd_src *src = rsnd_mod_to_src(mod);
470 struct device *dev = rsnd_priv_to_dev(priv);
476 * IRQ is not supported on non-DT
480 ret = devm_request_irq(dev, irq,
488 ret = rsnd_dma_attach(io, mod, &src->dma);
493 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
494 struct rsnd_dai_stream *io,
495 struct snd_soc_pcm_runtime *rtd)
497 struct rsnd_src *src = rsnd_mod_to_src(mod);
501 * enable SRC sync convert if possible
505 * It can't use SRC Synchronous convert
506 * when Capture if it uses CMD
508 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
512 * enable sync convert
514 ret = rsnd_kctrl_new_s(mod, io, rtd,
515 rsnd_io_is_play(io) ?
516 "SRC Out Rate Switch" :
517 "SRC In Rate Switch",
518 rsnd_src_set_convert_rate,
523 ret = rsnd_kctrl_new_s(mod, io, rtd,
524 rsnd_io_is_play(io) ?
527 rsnd_src_set_convert_rate,
533 static struct rsnd_mod_ops rsnd_src_ops = {
535 .dma_req = rsnd_src_dma_req,
536 .probe = rsnd_src_probe_,
537 .init = rsnd_src_init,
538 .quit = rsnd_src_quit,
539 .start = rsnd_src_start,
540 .stop = rsnd_src_stop,
542 .hw_params = rsnd_src_hw_params,
543 .pcm_new = rsnd_src_pcm_new,
546 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
548 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
551 return rsnd_mod_get(rsnd_src_get(priv, id));
554 int rsnd_src_probe(struct rsnd_priv *priv)
556 struct device_node *node;
557 struct device_node *np;
558 struct device *dev = rsnd_priv_to_dev(priv);
559 struct rsnd_src *src;
561 char name[RSND_SRC_NAME_SIZE];
564 /* This driver doesn't support Gen1 at this point */
565 if (rsnd_is_gen1(priv))
568 node = rsnd_src_of_node(priv);
570 return 0; /* not used is not error */
572 nr = of_get_child_count(node);
575 goto rsnd_src_probe_done;
578 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
581 goto rsnd_src_probe_done;
588 for_each_child_of_node(node, np) {
589 if (!of_device_is_available(np))
592 src = rsnd_src_get(priv, i);
594 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
597 src->irq = irq_of_parse_and_map(np, 0);
600 goto rsnd_src_probe_done;
603 clk = devm_clk_get(dev, name);
606 goto rsnd_src_probe_done;
609 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
610 &rsnd_src_ops, clk, rsnd_mod_get_status,
613 goto rsnd_src_probe_done;
627 void rsnd_src_remove(struct rsnd_priv *priv)
629 struct rsnd_src *src;
632 for_each_rsnd_src(src, priv, i) {
633 rsnd_mod_quit(rsnd_mod_get(src));