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_activation(struct rsnd_mod
*mod
)
75 rsnd_mod_write(mod
, SRC_SWRSR
, 0);
76 rsnd_mod_write(mod
, SRC_SWRSR
, 1);
79 static void rsnd_src_halt(struct rsnd_mod
*mod
)
81 rsnd_mod_write(mod
, SRC_SRCIR
, 1);
82 rsnd_mod_write(mod
, SRC_SWRSR
, 0);
85 static struct dma_chan
*rsnd_src_dma_req(struct rsnd_dai_stream
*io
,
88 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
89 int is_play
= rsnd_io_is_play(io
);
91 return rsnd_dma_request_channel(rsnd_src_of_node(priv
),
93 is_play
? "rx" : "tx");
96 static u32
rsnd_src_convert_rate(struct rsnd_dai_stream
*io
,
99 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
105 if (!rsnd_enable_sync_convert(src
))
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_ssi_rate(struct rsnd_priv
*priv
,
120 struct rsnd_dai_stream
*io
,
121 struct snd_pcm_runtime
*runtime
)
123 struct rsnd_mod
*src_mod
= rsnd_io_to_mod_src(io
);
124 struct rsnd_src
*src
;
125 unsigned int rate
= 0;
128 src
= rsnd_mod_to_src(src_mod
);
131 * return convert rate if SRC is used,
132 * otherwise, return runtime->rate as usual
134 rate
= rsnd_src_convert_rate(io
, src
);
138 rate
= runtime
->rate
;
143 static int rsnd_src_hw_params(struct rsnd_mod
*mod
,
144 struct rsnd_dai_stream
*io
,
145 struct snd_pcm_substream
*substream
,
146 struct snd_pcm_hw_params
*fe_params
)
148 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
149 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
152 * SRC assumes that it is used under DPCM if user want to use
153 * sampling rate convert. Then, SRC should be FE.
154 * And then, this function will be called *after* BE settings.
155 * this means, each BE already has fixuped hw_params.
157 * dpcm_fe_dai_hw_params()
158 * dpcm_be_dai_hw_params()
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 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
183 u32 convert_rate
= rsnd_src_convert_rate(io
, src
);
184 u32 ifscr
, fsrate
, adinr
;
192 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
195 else if (convert_rate
> runtime
->rate
)
196 ratio
= 100 * convert_rate
/ runtime
->rate
;
198 ratio
= 100 * runtime
->rate
/ convert_rate
;
201 dev_err(dev
, "FSO/FSI ratio error\n");
208 adinr
= rsnd_get_adinr_bit(mod
, io
) |
209 rsnd_get_adinr_chan(mod
, io
);
212 * SRC_IFSCR / SRC_IFSVR
218 fsrate
= 0x0400000 / convert_rate
* runtime
->rate
;
222 * SRC_SRCCR / SRC_ROUTE_MODE0
229 if (rsnd_enable_sync_convert(src
)) {
231 route
|= rsnd_io_is_play(io
) ?
232 (0x1 << 24) : (0x1 << 25);
237 * SRC_BSDSR / SRC_BSISR
239 switch (rsnd_mod_id(mod
)) {
244 bsdsr
= 0x02400000; /* 6 - 1/6 */
245 bsisr
= 0x00100060; /* 6 - 1/6 */
248 bsdsr
= 0x01800000; /* 6 - 1/6 */
249 bsisr
= 0x00100060 ;/* 6 - 1/6 */
253 rsnd_mod_write(mod
, SRC_SRCIR
, 1); /* initialize */
254 rsnd_mod_write(mod
, SRC_ADINR
, adinr
);
255 rsnd_mod_write(mod
, SRC_IFSCR
, ifscr
);
256 rsnd_mod_write(mod
, SRC_IFSVR
, fsrate
);
257 rsnd_mod_write(mod
, SRC_SRCCR
, cr
);
258 rsnd_mod_write(mod
, SRC_BSDSR
, bsdsr
);
259 rsnd_mod_write(mod
, SRC_BSISR
, bsisr
);
260 rsnd_mod_write(mod
, SRC_SRCIR
, 0); /* cancel initialize */
262 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, route
);
263 rsnd_mod_write(mod
, SRC_I_BUSIF_MODE
, 1);
264 rsnd_mod_write(mod
, SRC_O_BUSIF_MODE
, 1);
265 rsnd_mod_write(mod
, SRC_BUSIF_DALIGN
, rsnd_get_dalign(mod
, io
));
268 rsnd_adg_set_convert_clk_gen2(mod
, io
,
272 rsnd_adg_set_convert_timing_gen2(mod
, io
);
275 #define rsnd_src_irq_enable(mod) rsnd_src_irq_ctrol(mod, 1)
276 #define rsnd_src_irq_disable(mod) rsnd_src_irq_ctrol(mod, 0)
277 static void rsnd_src_irq_ctrol(struct rsnd_mod
*mod
, int enable
)
279 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
280 u32 sys_int_val
, int_val
, sys_int_mask
;
282 int id
= rsnd_mod_id(mod
);
285 sys_int_mask
= OUF_SRC(id
);
289 * IRQ is not supported on non-DT
293 if ((irq
<= 0) || !enable
) {
301 * ignore over flow error when rsnd_enable_sync_convert()
303 if (rsnd_enable_sync_convert(src
))
304 sys_int_val
= sys_int_val
& 0xffff;
306 rsnd_mod_write(mod
, SRC_INT_ENABLE0
, int_val
);
307 rsnd_mod_bset(mod
, SCU_SYS_INT_EN0
, sys_int_mask
, sys_int_val
);
308 rsnd_mod_bset(mod
, SCU_SYS_INT_EN1
, sys_int_mask
, sys_int_val
);
311 static void rsnd_src_status_clear(struct rsnd_mod
*mod
)
313 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
315 rsnd_mod_bset(mod
, SCU_SYS_STATUS0
, val
, val
);
316 rsnd_mod_bset(mod
, SCU_SYS_STATUS1
, val
, val
);
319 static bool rsnd_src_record_error(struct rsnd_mod
*mod
)
321 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
325 val0
= val1
= OUF_SRC(rsnd_mod_id(mod
));
330 * ignore over flow error when rsnd_enable_sync_convert()
332 if (rsnd_enable_sync_convert(src
))
333 val0
= val0
& 0xffff;
335 if ((rsnd_mod_read(mod
, SCU_SYS_STATUS0
) & val0
) ||
336 (rsnd_mod_read(mod
, SCU_SYS_STATUS1
) & val1
)) {
337 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
346 static int rsnd_src_start(struct rsnd_mod
*mod
,
347 struct rsnd_dai_stream
*io
,
348 struct rsnd_priv
*priv
)
350 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
356 * Enable SRC output if you want to use sync convert together with DVC
358 val
= (rsnd_io_to_mod_dvc(io
) && !rsnd_enable_sync_convert(src
)) ?
361 rsnd_mod_write(mod
, SRC_CTRL
, val
);
366 static int rsnd_src_stop(struct rsnd_mod
*mod
,
367 struct rsnd_dai_stream
*io
,
368 struct rsnd_priv
*priv
)
371 * stop SRC output only
374 rsnd_mod_write(mod
, SRC_CTRL
, 0x01);
379 static int rsnd_src_init(struct rsnd_mod
*mod
,
380 struct rsnd_dai_stream
*io
,
381 struct rsnd_priv
*priv
)
383 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
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 rsnd_src_irq_enable(mod
);
397 /* reset sync convert_rate */
403 static int rsnd_src_quit(struct rsnd_mod
*mod
,
404 struct rsnd_dai_stream
*io
,
405 struct rsnd_priv
*priv
)
407 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
408 struct device
*dev
= rsnd_priv_to_dev(priv
);
410 rsnd_src_irq_disable(mod
);
412 /* stop both out/in */
413 rsnd_mod_write(mod
, SRC_CTRL
, 0);
417 rsnd_mod_power_off(mod
);
420 dev_warn(dev
, "%s[%d] under/over flow err = %d\n",
421 rsnd_mod_name(mod
), rsnd_mod_id(mod
), src
->err
);
423 src
->convert_rate
= 0;
425 /* reset sync convert_rate */
431 static void __rsnd_src_interrupt(struct rsnd_mod
*mod
,
432 struct rsnd_dai_stream
*io
)
434 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
435 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
436 struct device
*dev
= rsnd_priv_to_dev(priv
);
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_record_error(mod
)) {
446 dev_dbg(dev
, "%s[%d] restart\n",
447 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
449 rsnd_src_stop(mod
, io
, priv
);
450 rsnd_src_start(mod
, io
, priv
);
453 if (src
->err
> 1024) {
454 rsnd_src_irq_disable(mod
);
456 dev_warn(dev
, "no more %s[%d] restart\n",
457 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
460 rsnd_src_status_clear(mod
);
461 rsnd_src_interrupt_out
:
463 spin_unlock(&priv
->lock
);
466 static irqreturn_t
rsnd_src_interrupt(int irq
, void *data
)
468 struct rsnd_mod
*mod
= data
;
470 rsnd_mod_interrupt(mod
, __rsnd_src_interrupt
);
475 static int rsnd_src_probe_(struct rsnd_mod
*mod
,
476 struct rsnd_dai_stream
*io
,
477 struct rsnd_priv
*priv
)
479 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
480 struct device
*dev
= rsnd_priv_to_dev(priv
);
486 * IRQ is not supported on non-DT
488 * rsnd_src_irq_enable()
490 ret
= devm_request_irq(dev
, irq
,
498 src
->dma
= rsnd_dma_attach(io
, mod
, 0);
499 if (IS_ERR(src
->dma
))
500 return PTR_ERR(src
->dma
);
505 static int rsnd_src_pcm_new(struct rsnd_mod
*mod
,
506 struct rsnd_dai_stream
*io
,
507 struct snd_soc_pcm_runtime
*rtd
)
509 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
510 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
514 * enable SRC sync convert if possible
518 * SRC sync convert needs clock master
520 if (!rsnd_rdai_is_clk_master(rdai
))
524 * enable sync convert
526 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
527 rsnd_io_is_play(io
) ?
528 "SRC Out Rate Switch" :
529 "SRC In Rate Switch",
530 rsnd_src_set_convert_rate
,
535 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
536 rsnd_io_is_play(io
) ?
539 rsnd_src_set_convert_rate
,
545 static struct rsnd_mod_ops rsnd_src_ops
= {
547 .dma_req
= rsnd_src_dma_req
,
548 .probe
= rsnd_src_probe_
,
549 .init
= rsnd_src_init
,
550 .quit
= rsnd_src_quit
,
551 .start
= rsnd_src_start
,
552 .stop
= rsnd_src_stop
,
553 .hw_params
= rsnd_src_hw_params
,
554 .pcm_new
= rsnd_src_pcm_new
,
557 struct rsnd_mod
*rsnd_src_mod_get(struct rsnd_priv
*priv
, int id
)
559 if (WARN_ON(id
< 0 || id
>= rsnd_src_nr(priv
)))
562 return rsnd_mod_get(rsnd_src_get(priv
, id
));
565 int rsnd_src_probe(struct rsnd_priv
*priv
)
567 struct device_node
*node
;
568 struct device_node
*np
;
569 struct device
*dev
= rsnd_priv_to_dev(priv
);
570 struct rsnd_src
*src
;
572 char name
[RSND_SRC_NAME_SIZE
];
575 /* This driver doesn't support Gen1 at this point */
576 if (rsnd_is_gen1(priv
))
579 node
= rsnd_src_of_node(priv
);
581 return 0; /* not used is not error */
583 nr
= of_get_child_count(node
);
586 goto rsnd_src_probe_done
;
589 src
= devm_kzalloc(dev
, sizeof(*src
) * nr
, GFP_KERNEL
);
592 goto rsnd_src_probe_done
;
599 for_each_child_of_node(node
, np
) {
600 src
= rsnd_src_get(priv
, i
);
602 snprintf(name
, RSND_SRC_NAME_SIZE
, "%s.%d",
605 src
->irq
= irq_of_parse_and_map(np
, 0);
608 goto rsnd_src_probe_done
;
611 clk
= devm_clk_get(dev
, name
);
614 goto rsnd_src_probe_done
;
617 ret
= rsnd_mod_init(priv
, rsnd_mod_get(src
),
618 &rsnd_src_ops
, clk
, RSND_MOD_SRC
, i
);
620 goto rsnd_src_probe_done
;
633 void rsnd_src_remove(struct rsnd_priv
*priv
)
635 struct rsnd_src
*src
;
638 for_each_rsnd_src(src
, priv
, i
) {
639 rsnd_mod_quit(rsnd_mod_get(src
));