2 * Renesas R-Car SCU 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.
14 struct rsnd_scu_platform_info *info; /* rcar_snd.h */
19 #define rsnd_scu_mode_flags(p) ((p)->info->flags)
20 #define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
22 #define RSND_SCU_NAME_SIZE 16
27 #define OTBL_24 (0 << 16)
28 #define OTBL_22 (2 << 16)
29 #define OTBL_20 (4 << 16)
30 #define OTBL_18 (6 << 16)
31 #define OTBL_16 (8 << 16)
34 * image of SRC (Sampling Rate Converter)
36 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
37 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
38 * 44.1kHz <-> +-----+ +-----+ +-------+
43 #define rsnd_mod_to_scu(_mod) \
44 container_of((_mod), struct rsnd_scu, mod)
46 #define for_each_rsnd_scu(pos, priv, i) \
48 ((i) < rsnd_scu_nr(priv)) && \
49 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
53 static int rsnd_src_set_route_if_gen1(struct rsnd_priv *priv,
55 struct rsnd_dai *rdai,
56 struct rsnd_dai_stream *io)
58 struct scu_route_config {
72 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
81 if (!rsnd_is_gen1(priv))
84 id = rsnd_mod_id(mod);
85 if (id < 0 || id >= ARRAY_SIZE(routes))
91 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
92 val = val << routes[id].shift;
93 mask = routes[id].mask << routes[id].shift;
95 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
100 shift = (id % 4) * 8;
101 mask = 0x1F << shift;
104 * ADG is used as source clock if SRC was used,
105 * then, SSI WS is used as destination clock.
106 * SSI WS is used as source clock if SRC is not used
107 * (when playback, source/destination become reverse when capture)
109 if (rsnd_scu_convert_rate(scu)) /* use ADG */
111 else if (8 == id) /* use SSI WS, but SRU8 is special */
113 else /* use SSI WS */
114 val = (id + 1) << shift;
118 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
121 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
124 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
131 unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
132 struct rsnd_mod *ssi_mod,
133 struct snd_pcm_runtime *runtime)
135 struct rsnd_scu *scu;
138 /* this function is assuming SSI id = SCU id here */
139 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
142 * return convert rate if SRC is used,
143 * otherwise, return runtime->rate as usual
145 rate = rsnd_scu_convert_rate(scu);
147 rate = runtime->rate;
152 static int rsnd_scu_convert_rate_ctrl(struct rsnd_priv *priv,
153 struct rsnd_mod *mod,
154 struct rsnd_dai *rdai,
155 struct rsnd_dai_stream *io)
157 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
158 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
159 u32 convert_rate = rsnd_scu_convert_rate(scu);
160 u32 adinr = runtime->channels;
162 /* set/clear soft reset */
163 rsnd_mod_write(mod, SRC_SWRSR, 0);
164 rsnd_mod_write(mod, SRC_SWRSR, 1);
166 /* Initialize the operation of the SRC internal circuits */
167 rsnd_mod_write(mod, SRC_SRCIR, 1);
169 /* Set channel number and output bit length */
170 switch (runtime->sample_bits) {
180 rsnd_mod_write(mod, SRC_ADINR, adinr);
183 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
186 /* Enable the initial value of IFS */
187 rsnd_mod_write(mod, SRC_IFSCR, 1);
189 /* Set initial value of IFS */
190 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
192 /* Select SRC mode (fixed value) */
193 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
195 /* Set the restriction value of the FS ratio (98%) */
196 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
198 if (rsnd_is_gen1(priv)) {
199 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
202 /* set convert clock */
203 ret = rsnd_adg_set_convert_clk(priv, mod,
210 /* Cancel the initialization and operate the SRC function */
211 rsnd_mod_write(mod, SRC_SRCIR, 0);
213 /* use DMA transfer */
214 rsnd_mod_write(mod, BUSIF_MODE, 1);
219 static int rsnd_scu_transfer_start(struct rsnd_priv *priv,
220 struct rsnd_mod *mod,
221 struct rsnd_dai *rdai,
222 struct rsnd_dai_stream *io)
224 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
225 int id = rsnd_mod_id(mod);
228 if (rsnd_is_gen1(priv)) {
230 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, val, val);
233 if (rsnd_scu_convert_rate(scu))
234 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
239 static int rsnd_scu_transfer_stop(struct rsnd_priv *priv,
240 struct rsnd_mod *mod,
241 struct rsnd_dai *rdai,
242 struct rsnd_dai_stream *io)
244 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
245 int id = rsnd_mod_id(mod);
248 if (rsnd_is_gen1(priv)) {
250 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, mask, 0);
253 if (rsnd_scu_convert_rate(scu))
254 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
259 bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
261 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
262 u32 flags = rsnd_scu_mode_flags(scu);
264 return !!(flags & RSND_SCU_USE_HPBIF);
267 static int rsnd_scu_start(struct rsnd_mod *mod,
268 struct rsnd_dai *rdai,
269 struct rsnd_dai_stream *io)
271 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
272 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
273 struct device *dev = rsnd_priv_to_dev(priv);
277 * SCU will be used if it has RSND_SCU_USE_HPBIF flags
279 if (!rsnd_scu_hpbif_is_enable(mod)) {
280 /* it use PIO transter */
281 dev_dbg(dev, "%s%d is not used\n",
282 rsnd_mod_name(mod), rsnd_mod_id(mod));
287 clk_enable(scu->clk);
289 /* it use DMA transter */
291 ret = rsnd_src_set_route_if_gen1(priv, mod, rdai, io);
295 ret = rsnd_scu_convert_rate_ctrl(priv, mod, rdai, io);
299 ret = rsnd_scu_transfer_start(priv, mod, rdai, io);
303 dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
308 static int rsnd_scu_stop(struct rsnd_mod *mod,
309 struct rsnd_dai *rdai,
310 struct rsnd_dai_stream *io)
312 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
313 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
315 if (!rsnd_scu_hpbif_is_enable(mod))
318 rsnd_scu_transfer_stop(priv, mod, rdai, io);
320 clk_disable(scu->clk);
325 static struct rsnd_mod_ops rsnd_scu_ops = {
327 .start = rsnd_scu_start,
328 .stop = rsnd_scu_stop,
331 struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
333 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
336 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
339 int rsnd_scu_probe(struct platform_device *pdev,
340 struct rcar_snd_info *info,
341 struct rsnd_priv *priv)
343 struct device *dev = rsnd_priv_to_dev(priv);
344 struct rsnd_scu *scu;
346 char name[RSND_SCU_NAME_SIZE];
352 nr = info->scu_info_nr;
353 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
355 dev_err(dev, "SCU allocate failed\n");
362 for_each_rsnd_scu(scu, priv, i) {
363 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
365 clk = devm_clk_get(dev, name);
369 rsnd_mod_init(priv, &scu->mod,
371 scu->info = &info->scu_info[i];
374 dev_dbg(dev, "SCU%d probed\n", i);
376 dev_dbg(dev, "scu probed\n");
381 void rsnd_scu_remove(struct platform_device *pdev,
382 struct rsnd_priv *priv)