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"
15 /* SCU_SYSTEM_STATUS0/1 */
16 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
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 */
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) \
38 ((i) < rsnd_src_nr(priv)) && \
39 ((pos) = (struct rsnd_src *)(priv)->src + i); \
44 * image of SRC (Sampling Rate Converter)
46 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
47 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
48 * 44.1kHz <-> +-----+ +-----+ +-------+
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
,
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
),
73 is_play
? "rx" : "tx");
76 static u32
rsnd_src_convert_rate(struct rsnd_dai_stream
*io
,
79 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
80 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
86 if (!rsnd_src_sync_is_enabled(mod
))
87 return src
->convert_rate
;
89 convert_rate
= src
->sync
.val
;
92 convert_rate
= src
->convert_rate
;
95 convert_rate
= runtime
->rate
;
100 unsigned int rsnd_src_get_rate(struct rsnd_priv
*priv
,
101 struct rsnd_dai_stream
*io
,
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
);
111 * runtime_rate -> [SRC] -> convert_rate
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
125 rate
= rsnd_src_convert_rate(io
, src_mod
);
128 rate
= runtime
->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.
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
);
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
);
176 u32 ifscr
, fsrate
, adinr
;
179 u32 i_busif
, o_busif
, tmp
;
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 */
192 ratio
= 100 * fin
/ fout
;
194 ratio
= 100 * fout
/ fin
;
197 dev_err(dev
, "FSO/FSI ratio error\n");
201 use_src
= (fin
!= fout
) | rsnd_src_sync_is_enabled(mod
);
206 adinr
= rsnd_get_adinr_bit(mod
, io
) |
207 rsnd_runtime_channel_original(io
);
210 * SRC_IFSCR / SRC_IFSVR
218 n
= (u64
)0x0400000 * fin
;
224 * SRC_SRCCR / SRC_ROUTE_MODE0
231 if (rsnd_src_sync_is_enabled(mod
)) {
233 route
|= rsnd_io_is_play(io
) ?
234 (0x1 << 24) : (0x1 << 25);
239 * SRC_BSDSR / SRC_BSISR
241 switch (rsnd_mod_id(mod
)) {
246 bsdsr
= 0x02400000; /* 6 - 1/6 */
247 bsisr
= 0x00100060; /* 6 - 1/6 */
250 bsdsr
= 0x01800000; /* 6 - 1/6 */
251 bsisr
= 0x00100060 ;/* 6 - 1/6 */
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
,
284 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
285 u32 sys_int_val
, int_val
, sys_int_mask
;
287 int id
= rsnd_mod_id(mod
);
290 sys_int_mask
= OUF_SRC(id
);
294 * IRQ is not supported on non-DT
298 if ((irq
<= 0) || !enable
) {
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
);
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
)
331 val0
= val1
= OUF_SRC(rsnd_mod_id(mod
));
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
))
348 static int rsnd_src_start(struct rsnd_mod
*mod
,
349 struct rsnd_dai_stream
*io
,
350 struct rsnd_priv
*priv
)
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
)) ?
362 rsnd_mod_write(mod
, SRC_CTRL
, val
);
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);
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 */
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
);
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
);
404 rsnd_mod_power_off(mod
);
406 /* reset sync convert_rate */
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
);
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
))
427 rsnd_src_status_clear(mod
);
428 rsnd_src_interrupt_out
:
430 spin_unlock(&priv
->lock
);
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
);
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
);
456 * IRQ is not supported on non-DT
460 ret
= devm_request_irq(dev
, irq
,
468 ret
= rsnd_dma_attach(io
, mod
, &src
->dma
);
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
);
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
))
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
,
504 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
505 rsnd_io_is_play(io
) ?
508 rsnd_kctrl_accept_runtime
,
509 rsnd_src_set_convert_rate
,
515 static struct rsnd_mod_ops rsnd_src_ops
= {
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
,
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
)))
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
;
543 char name
[RSND_SRC_NAME_SIZE
];
546 /* This driver doesn't support Gen1 at this point */
547 if (rsnd_is_gen1(priv
))
550 node
= rsnd_src_of_node(priv
);
552 return 0; /* not used is not error */
554 nr
= of_get_child_count(node
);
557 goto rsnd_src_probe_done
;
560 src
= devm_kzalloc(dev
, sizeof(*src
) * nr
, GFP_KERNEL
);
563 goto rsnd_src_probe_done
;
570 for_each_child_of_node(node
, np
) {
571 if (!of_device_is_available(np
))
574 src
= rsnd_src_get(priv
, i
);
576 snprintf(name
, RSND_SRC_NAME_SIZE
, "%s.%d",
579 src
->irq
= irq_of_parse_and_map(np
, 0);
583 goto rsnd_src_probe_done
;
586 clk
= devm_clk_get(dev
, name
);
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
,
598 goto rsnd_src_probe_done
;
613 void rsnd_src_remove(struct rsnd_priv
*priv
)
615 struct rsnd_src
*src
;
618 for_each_rsnd_src(src
, priv
, i
) {
619 rsnd_mod_quit(rsnd_mod_get(src
));