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 * you can enable below define if you don't need
14 * SSI interrupt status debug message when debugging
15 * see rsnd_dbg_irq_status()
17 * #define RSND_DEBUG_NO_IRQ_STATUS 1
22 #define SRC_NAME "src"
24 /* SCU_SYSTEM_STATUS0/1 */
25 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
30 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
31 struct rsnd_kctrl_cfg_s sync; /* sync convert */
32 u32 convert_rate; /* sampling rate convert */
36 #define RSND_SRC_NAME_SIZE 16
38 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
39 #define rsnd_src_nr(priv) ((priv)->src_nr)
40 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
42 #define rsnd_mod_to_src(_mod) \
43 container_of((_mod), struct rsnd_src, mod)
45 #define for_each_rsnd_src(pos, priv, i) \
47 ((i) < rsnd_src_nr(priv)) && \
48 ((pos) = (struct rsnd_src *)(priv)->src + i); \
53 * image of SRC (Sampling Rate Converter)
55 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
56 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
57 * 44.1kHz <-> +-----+ +-----+ +-------+
62 static void rsnd_src_activation(struct rsnd_mod *mod)
64 rsnd_mod_write(mod, SRC_SWRSR, 0);
65 rsnd_mod_write(mod, SRC_SWRSR, 1);
68 static void rsnd_src_halt(struct rsnd_mod *mod)
70 rsnd_mod_write(mod, SRC_SRCIR, 1);
71 rsnd_mod_write(mod, SRC_SWRSR, 0);
74 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
77 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
78 int is_play = rsnd_io_is_play(io);
80 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
82 is_play ? "rx" : "tx");
85 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
88 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
89 struct rsnd_src *src = rsnd_mod_to_src(mod);
95 if (!rsnd_src_sync_is_enabled(mod))
96 return src->convert_rate;
98 convert_rate = src->sync.val;
101 convert_rate = src->convert_rate;
104 convert_rate = runtime->rate;
109 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
110 struct rsnd_dai_stream *io,
113 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
114 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
115 unsigned int rate = 0;
116 int is_play = rsnd_io_is_play(io);
120 * runtime_rate -> [SRC] -> convert_rate
123 * convert_rate -> [SRC] -> runtime_rate
126 if (is_play == is_in)
127 return runtime->rate;
130 * return convert rate if SRC is used,
131 * otherwise, return runtime->rate as usual
134 rate = rsnd_src_convert_rate(io, src_mod);
137 rate = runtime->rate;
142 static int rsnd_src_hw_params(struct rsnd_mod *mod,
143 struct rsnd_dai_stream *io,
144 struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *fe_params)
147 struct rsnd_src *src = rsnd_mod_to_src(mod);
148 struct snd_soc_pcm_runtime *fe = substream->private_data;
151 * SRC assumes that it is used under DPCM if user want to use
152 * sampling rate convert. Then, SRC should be FE.
153 * And then, this function will be called *after* BE settings.
154 * this means, each BE already has fixuped hw_params.
156 * dpcm_fe_dai_hw_params()
157 * dpcm_be_dai_hw_params()
159 src->convert_rate = 0;
160 if (fe->dai_link->dynamic) {
161 int stream = substream->stream;
162 struct snd_soc_dpcm *dpcm;
163 struct snd_pcm_hw_params *be_params;
165 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
166 be_params = &dpcm->hw_params;
168 if (params_rate(fe_params) != params_rate(be_params))
169 src->convert_rate = params_rate(be_params);
176 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
177 struct rsnd_mod *mod)
179 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
180 struct device *dev = rsnd_priv_to_dev(priv);
181 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
182 int is_play = rsnd_io_is_play(io);
185 u32 ifscr, fsrate, adinr;
188 u32 i_busif, o_busif, tmp;
194 fin = rsnd_src_get_in_rate(priv, io);
195 fout = rsnd_src_get_out_rate(priv, io);
197 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
201 ratio = 100 * fin / fout;
203 ratio = 100 * fout / fin;
206 dev_err(dev, "FSO/FSI ratio error\n");
210 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
215 adinr = rsnd_get_adinr_bit(mod, io) |
216 rsnd_runtime_channel_original(io);
219 * SRC_IFSCR / SRC_IFSVR
227 n = (u64)0x0400000 * fin;
233 * SRC_SRCCR / SRC_ROUTE_MODE0
240 if (rsnd_src_sync_is_enabled(mod)) {
242 route |= rsnd_io_is_play(io) ?
243 (0x1 << 24) : (0x1 << 25);
248 * SRC_BSDSR / SRC_BSISR
250 switch (rsnd_mod_id(mod)) {
255 bsdsr = 0x02400000; /* 6 - 1/6 */
256 bsisr = 0x00100060; /* 6 - 1/6 */
259 bsdsr = 0x01800000; /* 6 - 1/6 */
260 bsisr = 0x00100060 ;/* 6 - 1/6 */
265 tmp = rsnd_get_busif_shift(io, mod);
266 i_busif = ( is_play ? tmp : 0) | 1;
267 o_busif = (!is_play ? tmp : 0) | 1;
269 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
271 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
272 rsnd_mod_write(mod, SRC_ADINR, adinr);
273 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
274 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
275 rsnd_mod_write(mod, SRC_SRCCR, cr);
276 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
277 rsnd_mod_write(mod, SRC_BSISR, bsisr);
278 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
280 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
281 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
283 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
285 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
288 static int rsnd_src_irq(struct rsnd_mod *mod,
289 struct rsnd_dai_stream *io,
290 struct rsnd_priv *priv,
293 struct rsnd_src *src = rsnd_mod_to_src(mod);
294 u32 sys_int_val, int_val, sys_int_mask;
296 int id = rsnd_mod_id(mod);
299 sys_int_mask = OUF_SRC(id);
303 * IRQ is not supported on non-DT
307 if ((irq <= 0) || !enable) {
315 * ignore over flow error when rsnd_src_sync_is_enabled()
317 if (rsnd_src_sync_is_enabled(mod))
318 sys_int_val = sys_int_val & 0xffff;
320 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
321 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
322 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
327 static void rsnd_src_status_clear(struct rsnd_mod *mod)
329 u32 val = OUF_SRC(rsnd_mod_id(mod));
331 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
332 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
335 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
337 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
338 struct device *dev = rsnd_priv_to_dev(priv);
340 u32 status0, status1;
343 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
348 * ignore over flow error when rsnd_src_sync_is_enabled()
350 if (rsnd_src_sync_is_enabled(mod))
351 val0 = val0 & 0xffff;
353 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
354 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
355 if ((status0 & val0) || (status1 & val1)) {
356 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x, 0x%08x\n",
357 rsnd_mod_name(mod), rsnd_mod_id(mod),
366 static int rsnd_src_start(struct rsnd_mod *mod,
367 struct rsnd_dai_stream *io,
368 struct rsnd_priv *priv)
375 * Enable SRC output if you want to use sync convert together with DVC
377 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
380 rsnd_mod_write(mod, SRC_CTRL, val);
385 static int rsnd_src_stop(struct rsnd_mod *mod,
386 struct rsnd_dai_stream *io,
387 struct rsnd_priv *priv)
389 rsnd_mod_write(mod, SRC_CTRL, 0);
394 static int rsnd_src_init(struct rsnd_mod *mod,
395 struct rsnd_dai_stream *io,
396 struct rsnd_priv *priv)
398 struct rsnd_src *src = rsnd_mod_to_src(mod);
400 /* reset sync convert_rate */
403 rsnd_mod_power_on(mod);
405 rsnd_src_activation(mod);
407 rsnd_src_set_convert_rate(io, mod);
409 rsnd_src_status_clear(mod);
414 static int rsnd_src_quit(struct rsnd_mod *mod,
415 struct rsnd_dai_stream *io,
416 struct rsnd_priv *priv)
418 struct rsnd_src *src = rsnd_mod_to_src(mod);
422 rsnd_mod_power_off(mod);
424 /* reset sync convert_rate */
430 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
431 struct rsnd_dai_stream *io)
433 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
436 spin_lock(&priv->lock);
438 /* ignore all cases if not working */
439 if (!rsnd_io_is_working(io))
440 goto rsnd_src_interrupt_out;
442 if (rsnd_src_error_occurred(mod))
445 rsnd_src_status_clear(mod);
446 rsnd_src_interrupt_out:
448 spin_unlock(&priv->lock);
451 snd_pcm_stop_xrun(io->substream);
454 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
456 struct rsnd_mod *mod = data;
458 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
463 static int rsnd_src_probe_(struct rsnd_mod *mod,
464 struct rsnd_dai_stream *io,
465 struct rsnd_priv *priv)
467 struct rsnd_src *src = rsnd_mod_to_src(mod);
468 struct device *dev = rsnd_priv_to_dev(priv);
474 * IRQ is not supported on non-DT
478 ret = devm_request_irq(dev, irq,
486 ret = rsnd_dma_attach(io, mod, &src->dma);
491 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
492 struct rsnd_dai_stream *io,
493 struct snd_soc_pcm_runtime *rtd)
495 struct rsnd_src *src = rsnd_mod_to_src(mod);
499 * enable SRC sync convert if possible
503 * It can't use SRC Synchronous convert
504 * when Capture if it uses CMD
506 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
510 * enable sync convert
512 ret = rsnd_kctrl_new_s(mod, io, rtd,
513 rsnd_io_is_play(io) ?
514 "SRC Out Rate Switch" :
515 "SRC In Rate Switch",
516 rsnd_kctrl_accept_anytime,
517 rsnd_src_set_convert_rate,
522 ret = rsnd_kctrl_new_s(mod, io, rtd,
523 rsnd_io_is_play(io) ?
526 rsnd_kctrl_accept_runtime,
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_kcalloc(dev, nr, sizeof(*src), 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);
601 goto rsnd_src_probe_done;
604 clk = devm_clk_get(dev, name);
608 goto rsnd_src_probe_done;
611 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
612 &rsnd_src_ops, clk, rsnd_mod_get_status,
616 goto rsnd_src_probe_done;
631 void rsnd_src_remove(struct rsnd_priv *priv)
633 struct rsnd_src *src;
636 for_each_rsnd_src(src, priv, i) {
637 rsnd_mod_quit(rsnd_mod_get(src));