x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / sound / soc / sh / rcar / src.c
blob510b68a483b4056ec9e211d651b3638e1dac9c84
1 /*
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.
11 #include "rsnd.h"
13 #define SRC_NAME "src"
15 /* SCU_SYSTEM_STATUS0/1 */
16 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
18 struct rsnd_src {
19 struct rsnd_mod mod;
20 struct rsnd_mod *dma;
21 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
22 struct rsnd_kctrl_cfg_s sync; /* sync convert */
23 u32 convert_rate; /* sampling rate convert */
24 int irq;
27 #define RSND_SRC_NAME_SIZE 16
29 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
30 #define rsnd_src_nr(priv) ((priv)->src_nr)
31 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
33 #define rsnd_mod_to_src(_mod) \
34 container_of((_mod), struct rsnd_src, mod)
36 #define for_each_rsnd_src(pos, priv, i) \
37 for ((i) = 0; \
38 ((i) < rsnd_src_nr(priv)) && \
39 ((pos) = (struct rsnd_src *)(priv)->src + i); \
40 i++)
44 * image of SRC (Sampling Rate Converter)
46 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
47 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
48 * 44.1kHz <-> +-----+ +-----+ +-------+
49 * ...
53 static void rsnd_src_activation(struct rsnd_mod *mod)
55 rsnd_mod_write(mod, SRC_SWRSR, 0);
56 rsnd_mod_write(mod, SRC_SWRSR, 1);
59 static void rsnd_src_halt(struct rsnd_mod *mod)
61 rsnd_mod_write(mod, SRC_SRCIR, 1);
62 rsnd_mod_write(mod, SRC_SWRSR, 0);
65 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
66 struct rsnd_mod *mod)
68 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
69 int is_play = rsnd_io_is_play(io);
71 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
72 mod,
73 is_play ? "rx" : "tx");
76 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
77 struct rsnd_mod *mod)
79 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
80 struct rsnd_src *src = rsnd_mod_to_src(mod);
81 u32 convert_rate;
83 if (!runtime)
84 return 0;
86 if (!rsnd_src_sync_is_enabled(mod))
87 return src->convert_rate;
89 convert_rate = src->sync.val;
91 if (!convert_rate)
92 convert_rate = src->convert_rate;
94 if (!convert_rate)
95 convert_rate = runtime->rate;
97 return convert_rate;
100 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
101 struct rsnd_dai_stream *io,
102 int is_in)
104 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
105 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
106 unsigned int rate = 0;
107 int is_play = rsnd_io_is_play(io);
110 * Playback
111 * runtime_rate -> [SRC] -> convert_rate
113 * Capture
114 * convert_rate -> [SRC] -> runtime_rate
117 if (is_play == is_in)
118 return runtime->rate;
121 * return convert rate if SRC is used,
122 * otherwise, return runtime->rate as usual
124 if (src_mod)
125 rate = rsnd_src_convert_rate(io, src_mod);
127 if (!rate)
128 rate = runtime->rate;
130 return rate;
133 static int rsnd_src_hw_params(struct rsnd_mod *mod,
134 struct rsnd_dai_stream *io,
135 struct snd_pcm_substream *substream,
136 struct snd_pcm_hw_params *fe_params)
138 struct rsnd_src *src = rsnd_mod_to_src(mod);
139 struct snd_soc_pcm_runtime *fe = substream->private_data;
142 * SRC assumes that it is used under DPCM if user want to use
143 * sampling rate convert. Then, SRC should be FE.
144 * And then, this function will be called *after* BE settings.
145 * this means, each BE already has fixuped hw_params.
146 * see
147 * dpcm_fe_dai_hw_params()
148 * dpcm_be_dai_hw_params()
150 src->convert_rate = 0;
151 if (fe->dai_link->dynamic) {
152 int stream = substream->stream;
153 struct snd_soc_dpcm *dpcm;
154 struct snd_pcm_hw_params *be_params;
156 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
157 be_params = &dpcm->hw_params;
159 if (params_rate(fe_params) != params_rate(be_params))
160 src->convert_rate = params_rate(be_params);
164 return 0;
167 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
168 struct rsnd_mod *mod)
170 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
171 struct device *dev = rsnd_priv_to_dev(priv);
172 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
173 int is_play = rsnd_io_is_play(io);
174 int use_src = 0;
175 u32 fin, fout;
176 u32 ifscr, fsrate, adinr;
177 u32 cr, route;
178 u32 bsdsr, bsisr;
179 u32 i_busif, o_busif, tmp;
180 uint ratio;
182 if (!runtime)
183 return;
185 fin = rsnd_src_get_in_rate(priv, io);
186 fout = rsnd_src_get_out_rate(priv, io);
188 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
189 if (fin == fout)
190 ratio = 0;
191 else if (fin > fout)
192 ratio = 100 * fin / fout;
193 else
194 ratio = 100 * fout / fin;
196 if (ratio > 600) {
197 dev_err(dev, "FSO/FSI ratio error\n");
198 return;
201 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
204 * SRC_ADINR
206 adinr = rsnd_get_adinr_bit(mod, io) |
207 rsnd_runtime_channel_original(io);
210 * SRC_IFSCR / SRC_IFSVR
212 ifscr = 0;
213 fsrate = 0;
214 if (use_src) {
215 u64 n;
217 ifscr = 1;
218 n = (u64)0x0400000 * fin;
219 do_div(n, fout);
220 fsrate = n;
224 * SRC_SRCCR / SRC_ROUTE_MODE0
226 cr = 0x00011110;
227 route = 0x0;
228 if (use_src) {
229 route = 0x1;
231 if (rsnd_src_sync_is_enabled(mod)) {
232 cr |= 0x1;
233 route |= rsnd_io_is_play(io) ?
234 (0x1 << 24) : (0x1 << 25);
239 * SRC_BSDSR / SRC_BSISR
241 switch (rsnd_mod_id(mod)) {
242 case 5:
243 case 6:
244 case 7:
245 case 8:
246 bsdsr = 0x02400000; /* 6 - 1/6 */
247 bsisr = 0x00100060; /* 6 - 1/6 */
248 break;
249 default:
250 bsdsr = 0x01800000; /* 6 - 1/6 */
251 bsisr = 0x00100060 ;/* 6 - 1/6 */
252 break;
255 /* BUSIF_MODE */
256 tmp = rsnd_get_busif_shift(io, mod);
257 i_busif = ( is_play ? tmp : 0) | 1;
258 o_busif = (!is_play ? tmp : 0) | 1;
260 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
262 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
263 rsnd_mod_write(mod, SRC_ADINR, adinr);
264 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
265 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
266 rsnd_mod_write(mod, SRC_SRCCR, cr);
267 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
268 rsnd_mod_write(mod, SRC_BSISR, bsisr);
269 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
271 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
272 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
274 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
276 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
279 static int rsnd_src_irq(struct rsnd_mod *mod,
280 struct rsnd_dai_stream *io,
281 struct rsnd_priv *priv,
282 int enable)
284 struct rsnd_src *src = rsnd_mod_to_src(mod);
285 u32 sys_int_val, int_val, sys_int_mask;
286 int irq = src->irq;
287 int id = rsnd_mod_id(mod);
289 sys_int_val =
290 sys_int_mask = OUF_SRC(id);
291 int_val = 0x3300;
294 * IRQ is not supported on non-DT
295 * see
296 * rsnd_src_probe_()
298 if ((irq <= 0) || !enable) {
299 sys_int_val = 0;
300 int_val = 0;
304 * WORKAROUND
306 * ignore over flow error when rsnd_src_sync_is_enabled()
308 if (rsnd_src_sync_is_enabled(mod))
309 sys_int_val = sys_int_val & 0xffff;
311 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
312 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
313 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
315 return 0;
318 static void rsnd_src_status_clear(struct rsnd_mod *mod)
320 u32 val = OUF_SRC(rsnd_mod_id(mod));
322 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
323 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
326 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
328 u32 val0, val1;
329 bool ret = false;
331 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
334 * WORKAROUND
336 * ignore over flow error when rsnd_src_sync_is_enabled()
338 if (rsnd_src_sync_is_enabled(mod))
339 val0 = val0 & 0xffff;
341 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
342 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
343 ret = true;
345 return ret;
348 static int rsnd_src_start(struct rsnd_mod *mod,
349 struct rsnd_dai_stream *io,
350 struct rsnd_priv *priv)
352 u32 val;
355 * WORKAROUND
357 * Enable SRC output if you want to use sync convert together with DVC
359 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
360 0x01 : 0x11;
362 rsnd_mod_write(mod, SRC_CTRL, val);
364 return 0;
367 static int rsnd_src_stop(struct rsnd_mod *mod,
368 struct rsnd_dai_stream *io,
369 struct rsnd_priv *priv)
371 rsnd_mod_write(mod, SRC_CTRL, 0);
373 return 0;
376 static int rsnd_src_init(struct rsnd_mod *mod,
377 struct rsnd_dai_stream *io,
378 struct rsnd_priv *priv)
380 struct rsnd_src *src = rsnd_mod_to_src(mod);
382 /* reset sync convert_rate */
383 src->sync.val = 0;
385 rsnd_mod_power_on(mod);
387 rsnd_src_activation(mod);
389 rsnd_src_set_convert_rate(io, mod);
391 rsnd_src_status_clear(mod);
393 return 0;
396 static int rsnd_src_quit(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 rsnd_src_halt(mod);
404 rsnd_mod_power_off(mod);
406 /* reset sync convert_rate */
407 src->sync.val = 0;
409 return 0;
412 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
413 struct rsnd_dai_stream *io)
415 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
416 bool stop = false;
418 spin_lock(&priv->lock);
420 /* ignore all cases if not working */
421 if (!rsnd_io_is_working(io))
422 goto rsnd_src_interrupt_out;
424 if (rsnd_src_error_occurred(mod))
425 stop = true;
427 rsnd_src_status_clear(mod);
428 rsnd_src_interrupt_out:
430 spin_unlock(&priv->lock);
432 if (stop)
433 snd_pcm_stop_xrun(io->substream);
436 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
438 struct rsnd_mod *mod = data;
440 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
442 return IRQ_HANDLED;
445 static int rsnd_src_probe_(struct rsnd_mod *mod,
446 struct rsnd_dai_stream *io,
447 struct rsnd_priv *priv)
449 struct rsnd_src *src = rsnd_mod_to_src(mod);
450 struct device *dev = rsnd_priv_to_dev(priv);
451 int irq = src->irq;
452 int ret;
454 if (irq > 0) {
456 * IRQ is not supported on non-DT
457 * see
458 * rsnd_src_irq()
460 ret = devm_request_irq(dev, irq,
461 rsnd_src_interrupt,
462 IRQF_SHARED,
463 dev_name(dev), mod);
464 if (ret)
465 return ret;
468 ret = rsnd_dma_attach(io, mod, &src->dma);
470 return ret;
473 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
474 struct rsnd_dai_stream *io,
475 struct snd_soc_pcm_runtime *rtd)
477 struct rsnd_src *src = rsnd_mod_to_src(mod);
478 int ret;
481 * enable SRC sync convert if possible
485 * It can't use SRC Synchronous convert
486 * when Capture if it uses CMD
488 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
489 return 0;
492 * enable sync convert
494 ret = rsnd_kctrl_new_s(mod, io, rtd,
495 rsnd_io_is_play(io) ?
496 "SRC Out Rate Switch" :
497 "SRC In Rate Switch",
498 rsnd_kctrl_accept_anytime,
499 rsnd_src_set_convert_rate,
500 &src->sen, 1);
501 if (ret < 0)
502 return ret;
504 ret = rsnd_kctrl_new_s(mod, io, rtd,
505 rsnd_io_is_play(io) ?
506 "SRC Out Rate" :
507 "SRC In Rate",
508 rsnd_kctrl_accept_runtime,
509 rsnd_src_set_convert_rate,
510 &src->sync, 192000);
512 return ret;
515 static struct rsnd_mod_ops rsnd_src_ops = {
516 .name = SRC_NAME,
517 .dma_req = rsnd_src_dma_req,
518 .probe = rsnd_src_probe_,
519 .init = rsnd_src_init,
520 .quit = rsnd_src_quit,
521 .start = rsnd_src_start,
522 .stop = rsnd_src_stop,
523 .irq = rsnd_src_irq,
524 .hw_params = rsnd_src_hw_params,
525 .pcm_new = rsnd_src_pcm_new,
528 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
530 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
531 id = 0;
533 return rsnd_mod_get(rsnd_src_get(priv, id));
536 int rsnd_src_probe(struct rsnd_priv *priv)
538 struct device_node *node;
539 struct device_node *np;
540 struct device *dev = rsnd_priv_to_dev(priv);
541 struct rsnd_src *src;
542 struct clk *clk;
543 char name[RSND_SRC_NAME_SIZE];
544 int i, nr, ret;
546 /* This driver doesn't support Gen1 at this point */
547 if (rsnd_is_gen1(priv))
548 return 0;
550 node = rsnd_src_of_node(priv);
551 if (!node)
552 return 0; /* not used is not error */
554 nr = of_get_child_count(node);
555 if (!nr) {
556 ret = -EINVAL;
557 goto rsnd_src_probe_done;
560 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
561 if (!src) {
562 ret = -ENOMEM;
563 goto rsnd_src_probe_done;
566 priv->src_nr = nr;
567 priv->src = src;
569 i = 0;
570 for_each_child_of_node(node, np) {
571 if (!of_device_is_available(np))
572 goto skip;
574 src = rsnd_src_get(priv, i);
576 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
577 SRC_NAME, i);
579 src->irq = irq_of_parse_and_map(np, 0);
580 if (!src->irq) {
581 ret = -EINVAL;
582 of_node_put(np);
583 goto rsnd_src_probe_done;
586 clk = devm_clk_get(dev, name);
587 if (IS_ERR(clk)) {
588 ret = PTR_ERR(clk);
589 of_node_put(np);
590 goto rsnd_src_probe_done;
593 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
594 &rsnd_src_ops, clk, rsnd_mod_get_status,
595 RSND_MOD_SRC, i);
596 if (ret) {
597 of_node_put(np);
598 goto rsnd_src_probe_done;
601 skip:
602 i++;
605 ret = 0;
607 rsnd_src_probe_done:
608 of_node_put(node);
610 return ret;
613 void rsnd_src_remove(struct rsnd_priv *priv)
615 struct rsnd_src *src;
616 int i;
618 for_each_rsnd_src(src, priv, i) {
619 rsnd_mod_quit(rsnd_mod_get(src));