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 */
32 #define RSND_SRC_NAME_SIZE 16
34 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
35 #define rsnd_src_to_dma(src) ((src)->dma)
36 #define rsnd_src_nr(priv) ((priv)->src_nr)
37 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
39 #define rsnd_mod_to_src(_mod) \
40 container_of((_mod), struct rsnd_src, mod)
42 #define for_each_rsnd_src(pos, priv, i) \
44 ((i) < rsnd_src_nr(priv)) && \
45 ((pos) = (struct rsnd_src *)(priv)->src + i); \
50 * image of SRC (Sampling Rate Converter)
52 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
53 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
54 * 44.1kHz <-> +-----+ +-----+ +-------+
64 * [mem] -> [SRU] -> [SSI]
69 * [mem] -> [SRC] -> [SSIU] -> [SSI]
73 static void rsnd_src_soft_reset(struct rsnd_mod
*mod
)
75 rsnd_mod_write(mod
, SRC_SWRSR
, 0);
76 rsnd_mod_write(mod
, SRC_SWRSR
, 1);
79 static struct dma_chan
*rsnd_src_dma_req(struct rsnd_dai_stream
*io
,
82 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
83 int is_play
= rsnd_io_is_play(io
);
85 return rsnd_dma_request_channel(rsnd_src_of_node(priv
),
87 is_play
? "rx" : "tx");
90 static u32
rsnd_src_convert_rate(struct rsnd_dai_stream
*io
,
93 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
99 if (!rsnd_enable_sync_convert(src
))
100 return src
->convert_rate
;
102 convert_rate
= src
->sync
.val
;
105 convert_rate
= src
->convert_rate
;
108 convert_rate
= runtime
->rate
;
113 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv
*priv
,
114 struct rsnd_dai_stream
*io
,
115 struct snd_pcm_runtime
*runtime
)
117 struct rsnd_mod
*src_mod
= rsnd_io_to_mod_src(io
);
118 struct rsnd_src
*src
;
119 unsigned int rate
= 0;
122 src
= rsnd_mod_to_src(src_mod
);
125 * return convert rate if SRC is used,
126 * otherwise, return runtime->rate as usual
128 rate
= rsnd_src_convert_rate(io
, src
);
132 rate
= runtime
->rate
;
137 static int rsnd_src_hw_params(struct rsnd_mod
*mod
,
138 struct rsnd_dai_stream
*io
,
139 struct snd_pcm_substream
*substream
,
140 struct snd_pcm_hw_params
*fe_params
)
142 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
143 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
146 * SRC assumes that it is used under DPCM if user want to use
147 * sampling rate convert. Then, SRC should be FE.
148 * And then, this function will be called *after* BE settings.
149 * this means, each BE already has fixuped hw_params.
151 * dpcm_fe_dai_hw_params()
152 * dpcm_be_dai_hw_params()
154 if (fe
->dai_link
->dynamic
) {
155 int stream
= substream
->stream
;
156 struct snd_soc_dpcm
*dpcm
;
157 struct snd_pcm_hw_params
*be_params
;
159 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
160 be_params
= &dpcm
->hw_params
;
162 if (params_rate(fe_params
) != params_rate(be_params
))
163 src
->convert_rate
= params_rate(be_params
);
170 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream
*io
,
171 struct rsnd_mod
*mod
)
173 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
174 struct device
*dev
= rsnd_priv_to_dev(priv
);
175 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
176 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
177 u32 convert_rate
= rsnd_src_convert_rate(io
, src
);
178 u32 ifscr
, fsrate
, adinr
;
186 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
189 else if (convert_rate
> runtime
->rate
)
190 ratio
= 100 * convert_rate
/ runtime
->rate
;
192 ratio
= 100 * runtime
->rate
/ convert_rate
;
195 dev_err(dev
, "FSO/FSI ratio error\n");
202 adinr
= rsnd_get_adinr_bit(mod
, io
);
205 * SRC_IFSCR / SRC_IFSVR
211 fsrate
= 0x0400000 / convert_rate
* runtime
->rate
;
215 * SRC_SRCCR / SRC_ROUTE_MODE0
222 if (rsnd_enable_sync_convert(src
)) {
224 route
|= rsnd_io_is_play(io
) ?
225 (0x1 << 24) : (0x1 << 25);
230 * SRC_BSDSR / SRC_BSISR
232 switch (rsnd_mod_id(mod
)) {
237 bsdsr
= 0x02400000; /* 6 - 1/6 */
238 bsisr
= 0x00100060; /* 6 - 1/6 */
241 bsdsr
= 0x01800000; /* 6 - 1/6 */
242 bsisr
= 0x00100060 ;/* 6 - 1/6 */
246 rsnd_mod_write(mod
, SRC_SRCIR
, 1); /* initialize */
247 rsnd_mod_write(mod
, SRC_ADINR
, adinr
);
248 rsnd_mod_write(mod
, SRC_IFSCR
, ifscr
);
249 rsnd_mod_write(mod
, SRC_IFSVR
, fsrate
);
250 rsnd_mod_write(mod
, SRC_SRCCR
, cr
);
251 rsnd_mod_write(mod
, SRC_BSDSR
, bsdsr
);
252 rsnd_mod_write(mod
, SRC_BSISR
, bsisr
);
253 rsnd_mod_write(mod
, SRC_SRCIR
, 0); /* cancel initialize */
255 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, route
);
256 rsnd_mod_write(mod
, SRC_BUSIF_MODE
, 1);
257 rsnd_mod_write(mod
, SRC_BUSIF_DALIGN
, rsnd_get_dalign(mod
, io
));
260 rsnd_adg_set_convert_clk_gen2(mod
, io
,
264 rsnd_adg_set_convert_timing_gen2(mod
, io
);
267 #define rsnd_src_irq_enable(mod) rsnd_src_irq_ctrol(mod, 1)
268 #define rsnd_src_irq_disable(mod) rsnd_src_irq_ctrol(mod, 0)
269 static void rsnd_src_irq_ctrol(struct rsnd_mod
*mod
, int enable
)
271 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
272 u32 sys_int_val
, int_val
, sys_int_mask
;
274 int id
= rsnd_mod_id(mod
);
277 sys_int_mask
= OUF_SRC(id
);
281 * IRQ is not supported on non-DT
285 if ((irq
<= 0) || !enable
) {
293 * ignore over flow error when rsnd_enable_sync_convert()
295 if (rsnd_enable_sync_convert(src
))
296 sys_int_val
= sys_int_val
& 0xffff;
298 rsnd_mod_write(mod
, SRC_INT_ENABLE0
, int_val
);
299 rsnd_mod_bset(mod
, SCU_SYS_INT_EN0
, sys_int_mask
, sys_int_val
);
300 rsnd_mod_bset(mod
, SCU_SYS_INT_EN1
, sys_int_mask
, sys_int_val
);
303 static void rsnd_src_status_clear(struct rsnd_mod
*mod
)
305 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
307 rsnd_mod_bset(mod
, SCU_SYS_STATUS0
, val
, val
);
308 rsnd_mod_bset(mod
, SCU_SYS_STATUS1
, val
, val
);
311 static bool rsnd_src_record_error(struct rsnd_mod
*mod
)
313 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
317 val0
= val1
= OUF_SRC(rsnd_mod_id(mod
));
322 * ignore over flow error when rsnd_enable_sync_convert()
324 if (rsnd_enable_sync_convert(src
))
325 val0
= val0
& 0xffff;
327 if ((rsnd_mod_read(mod
, SCU_SYS_STATUS0
) & val0
) ||
328 (rsnd_mod_read(mod
, SCU_SYS_STATUS1
) & val1
)) {
329 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
338 static int rsnd_src_start(struct rsnd_mod
*mod
,
339 struct rsnd_dai_stream
*io
,
340 struct rsnd_priv
*priv
)
342 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
348 * Enable SRC output if you want to use sync convert together with DVC
350 val
= (rsnd_io_to_mod_dvc(io
) && !rsnd_enable_sync_convert(src
)) ?
353 rsnd_mod_write(mod
, SRC_CTRL
, val
);
358 static int rsnd_src_stop(struct rsnd_mod
*mod
,
359 struct rsnd_dai_stream
*io
,
360 struct rsnd_priv
*priv
)
363 * stop SRC output only
366 rsnd_mod_write(mod
, SRC_CTRL
, 0x01);
371 static int rsnd_src_init(struct rsnd_mod
*mod
,
372 struct rsnd_dai_stream
*io
,
373 struct rsnd_priv
*priv
)
375 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
377 rsnd_mod_power_on(mod
);
379 rsnd_src_soft_reset(mod
);
381 rsnd_src_set_convert_rate(io
, mod
);
383 rsnd_src_status_clear(mod
);
385 rsnd_src_irq_enable(mod
);
389 /* reset sync convert_rate */
395 static int rsnd_src_quit(struct rsnd_mod
*mod
,
396 struct rsnd_dai_stream
*io
,
397 struct rsnd_priv
*priv
)
399 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
400 struct device
*dev
= rsnd_priv_to_dev(priv
);
402 rsnd_src_irq_disable(mod
);
404 /* stop both out/in */
405 rsnd_mod_write(mod
, SRC_CTRL
, 0);
407 rsnd_mod_power_off(mod
);
410 dev_warn(dev
, "%s[%d] under/over flow err = %d\n",
411 rsnd_mod_name(mod
), rsnd_mod_id(mod
), src
->err
);
413 src
->convert_rate
= 0;
415 /* reset sync convert_rate */
421 static void __rsnd_src_interrupt(struct rsnd_mod
*mod
,
422 struct rsnd_dai_stream
*io
)
424 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
425 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
426 struct device
*dev
= rsnd_priv_to_dev(priv
);
428 spin_lock(&priv
->lock
);
430 /* ignore all cases if not working */
431 if (!rsnd_io_is_working(io
))
432 goto rsnd_src_interrupt_out
;
434 if (rsnd_src_record_error(mod
)) {
436 dev_dbg(dev
, "%s[%d] restart\n",
437 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
439 rsnd_src_stop(mod
, io
, priv
);
440 rsnd_src_start(mod
, io
, priv
);
443 if (src
->err
> 1024) {
444 rsnd_src_irq_disable(mod
);
446 dev_warn(dev
, "no more %s[%d] restart\n",
447 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
450 rsnd_src_status_clear(mod
);
451 rsnd_src_interrupt_out
:
453 spin_unlock(&priv
->lock
);
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
478 * rsnd_src_irq_enable()
480 ret
= devm_request_irq(dev
, irq
,
488 src
->dma
= rsnd_dma_attach(io
, mod
, 0);
489 if (IS_ERR(src
->dma
))
490 return PTR_ERR(src
->dma
);
495 static int rsnd_src_pcm_new(struct rsnd_mod
*mod
,
496 struct rsnd_dai_stream
*io
,
497 struct snd_soc_pcm_runtime
*rtd
)
499 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
500 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
504 * enable SRC sync convert if possible
508 * SRC sync convert needs clock master
510 if (!rsnd_rdai_is_clk_master(rdai
))
514 * enable sync convert
516 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
517 rsnd_io_is_play(io
) ?
518 "SRC Out Rate Switch" :
519 "SRC In Rate Switch",
520 rsnd_src_set_convert_rate
,
525 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
526 rsnd_io_is_play(io
) ?
529 rsnd_src_set_convert_rate
,
535 static struct rsnd_mod_ops rsnd_src_ops
= {
537 .dma_req
= rsnd_src_dma_req
,
538 .probe
= rsnd_src_probe_
,
539 .init
= rsnd_src_init
,
540 .quit
= rsnd_src_quit
,
541 .start
= rsnd_src_start
,
542 .stop
= rsnd_src_stop
,
543 .hw_params
= rsnd_src_hw_params
,
544 .pcm_new
= rsnd_src_pcm_new
,
547 struct rsnd_mod
*rsnd_src_mod_get(struct rsnd_priv
*priv
, int id
)
549 if (WARN_ON(id
< 0 || id
>= rsnd_src_nr(priv
)))
552 return rsnd_mod_get(rsnd_src_get(priv
, id
));
555 int rsnd_src_probe(struct rsnd_priv
*priv
)
557 struct device_node
*node
;
558 struct device_node
*np
;
559 struct device
*dev
= rsnd_priv_to_dev(priv
);
560 struct rsnd_src
*src
;
562 char name
[RSND_SRC_NAME_SIZE
];
565 /* This driver doesn't support Gen1 at this point */
566 if (rsnd_is_gen1(priv
))
569 node
= rsnd_src_of_node(priv
);
571 return 0; /* not used is not error */
573 nr
= of_get_child_count(node
);
576 goto rsnd_src_probe_done
;
579 src
= devm_kzalloc(dev
, sizeof(*src
) * nr
, GFP_KERNEL
);
582 goto rsnd_src_probe_done
;
589 for_each_child_of_node(node
, np
) {
590 src
= rsnd_src_get(priv
, i
);
592 snprintf(name
, RSND_SRC_NAME_SIZE
, "%s.%d",
595 src
->irq
= irq_of_parse_and_map(np
, 0);
598 goto rsnd_src_probe_done
;
601 clk
= devm_clk_get(dev
, name
);
604 goto rsnd_src_probe_done
;
607 ret
= rsnd_mod_init(priv
, rsnd_mod_get(src
),
608 &rsnd_src_ops
, clk
, RSND_MOD_SRC
, i
);
610 goto rsnd_src_probe_done
;
623 void rsnd_src_remove(struct rsnd_priv
*priv
)
625 struct rsnd_src
*src
;
628 for_each_rsnd_src(src
, priv
, i
) {
629 rsnd_mod_quit(rsnd_mod_get(src
));