Commit | Line | Data |
---|---|---|
07539c1d | 1 | /* |
ba9c949f | 2 | * Renesas R-Car SRC support |
07539c1d KM |
3 | * |
4 | * Copyright (C) 2013 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
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. | |
10 | */ | |
11 | #include "rsnd.h" | |
12 | ||
8aefda50 KM |
13 | #define SRC_NAME "src" |
14 | ||
ba9c949f KM |
15 | struct rsnd_src { |
16 | struct rsnd_src_platform_info *info; /* rcar_snd.h */ | |
07539c1d | 17 | struct rsnd_mod mod; |
ef749400 | 18 | struct clk *clk; |
07539c1d KM |
19 | }; |
20 | ||
ba9c949f | 21 | #define RSND_SRC_NAME_SIZE 16 |
374a5281 | 22 | |
ba9c949f KM |
23 | #define rsnd_src_convert_rate(p) ((p)->info->convert_rate) |
24 | #define rsnd_mod_to_src(_mod) \ | |
25 | container_of((_mod), struct rsnd_src, mod) | |
ba9c949f KM |
26 | #define rsnd_src_dma_available(src) \ |
27 | rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod)) | |
39cf3c40 | 28 | |
ba9c949f | 29 | #define for_each_rsnd_src(pos, priv, i) \ |
39cf3c40 | 30 | for ((i) = 0; \ |
ba9c949f KM |
31 | ((i) < rsnd_src_nr(priv)) && \ |
32 | ((pos) = (struct rsnd_src *)(priv)->src + i); \ | |
39cf3c40 KM |
33 | i++) |
34 | ||
35 | ||
ef749400 KM |
36 | /* |
37 | * image of SRC (Sampling Rate Converter) | |
38 | * | |
39 | * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ | |
40 | * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | | |
41 | * 44.1kHz <-> +-----+ +-----+ +-------+ | |
42 | * ... | |
43 | * | |
44 | */ | |
374a5281 | 45 | |
c926b746 | 46 | /* |
ba9c949f | 47 | * src.c is caring... |
c926b746 KM |
48 | * |
49 | * Gen1 | |
50 | * | |
51 | * [mem] -> [SRU] -> [SSI] | |
52 | * |--------| | |
53 | * | |
54 | * Gen2 | |
55 | * | |
ba9c949f | 56 | * [mem] -> [SRC] -> [SSIU] -> [SSI] |
c926b746 KM |
57 | * |-----------------| |
58 | */ | |
59 | ||
41c6221c KM |
60 | /* |
61 | * How to use SRC bypass mode for debugging | |
62 | * | |
63 | * SRC has bypass mode, and it is useful for debugging. | |
64 | * In Gen2 case, | |
65 | * SRCm_MODE controls whether SRC is used or not | |
66 | * SSI_MODE0 controls whether SSIU which receives SRC data | |
67 | * is used or not. | |
68 | * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC, | |
69 | * but SRC bypass mode needs SSI_MODE0 only. | |
70 | * | |
71 | * This driver request | |
ba9c949f | 72 | * struct rsnd_src_platform_info { |
41c6221c | 73 | * u32 convert_rate; |
29e69fd2 | 74 | * int dma_id; |
41c6221c KM |
75 | * } |
76 | * | |
ba9c949f | 77 | * rsnd_src_convert_rate() indicates |
41c6221c KM |
78 | * above convert_rate, and it controls |
79 | * whether SRC is used or not. | |
80 | * | |
81 | * ex) doesn't use SRC | |
29e69fd2 KM |
82 | * static struct rsnd_dai_platform_info rsnd_dai = { |
83 | * .playback = { .ssi = &rsnd_ssi[0], }, | |
41c6221c KM |
84 | * }; |
85 | * | |
86 | * ex) uses SRC | |
29e69fd2 KM |
87 | * static struct rsnd_src_platform_info rsnd_src[] = { |
88 | * RSND_SCU(48000, 0), | |
89 | * ... | |
90 | * }; | |
91 | * static struct rsnd_dai_platform_info rsnd_dai = { | |
92 | * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] }, | |
41c6221c KM |
93 | * }; |
94 | * | |
95 | * ex) uses SRC bypass mode | |
29e69fd2 KM |
96 | * static struct rsnd_src_platform_info rsnd_src[] = { |
97 | * RSND_SCU(0, 0), | |
98 | * ... | |
99 | * }; | |
100 | * static struct rsnd_dai_platform_info rsnd_dai = { | |
101 | * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] }, | |
41c6221c KM |
102 | * }; |
103 | * | |
104 | */ | |
105 | ||
1b7b08ef KM |
106 | /* |
107 | * Gen1/Gen2 common functions | |
108 | */ | |
d9288d0b KM |
109 | int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod, |
110 | struct rsnd_dai *rdai, | |
111 | int use_busif) | |
7b5ce975 | 112 | { |
1cc71959 KM |
113 | struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod); |
114 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
374e5426 | 115 | int ssi_id = rsnd_mod_id(ssi_mod); |
7b5ce975 KM |
116 | |
117 | /* | |
118 | * SSI_MODE0 | |
119 | */ | |
221bf523 | 120 | rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id), |
d9288d0b | 121 | !use_busif << ssi_id); |
7b5ce975 KM |
122 | |
123 | /* | |
124 | * SSI_MODE1 | |
125 | */ | |
374e5426 | 126 | if (rsnd_ssi_is_pin_sharing(ssi_mod)) { |
7b5ce975 | 127 | int shift = -1; |
374e5426 | 128 | switch (ssi_id) { |
7b5ce975 KM |
129 | case 1: |
130 | shift = 0; | |
131 | break; | |
132 | case 2: | |
133 | shift = 2; | |
134 | break; | |
135 | case 4: | |
136 | shift = 16; | |
137 | break; | |
138 | } | |
139 | ||
140 | if (shift >= 0) | |
221bf523 | 141 | rsnd_mod_bset(ssi_mod, SSI_MODE1, |
7b5ce975 KM |
142 | 0x3 << shift, |
143 | rsnd_dai_is_clk_master(rdai) ? | |
144 | 0x2 << shift : 0x1 << shift); | |
145 | } | |
146 | ||
d9288d0b KM |
147 | /* |
148 | * DMA settings for SSIU | |
149 | */ | |
150 | if (use_busif) { | |
1cc71959 KM |
151 | u32 val = 0x76543210; |
152 | u32 mask = ~0; | |
153 | ||
d9288d0b KM |
154 | rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR, |
155 | rsnd_get_adinr(ssi_mod)); | |
156 | rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1); | |
157 | rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1); | |
1cc71959 KM |
158 | |
159 | mask <<= runtime->channels * 4; | |
160 | val = val & mask; | |
161 | ||
162 | switch (runtime->sample_bits) { | |
163 | case 16: | |
164 | val |= 0x67452301 & ~mask; | |
165 | break; | |
166 | case 32: | |
167 | val |= 0x76543210 & ~mask; | |
168 | break; | |
169 | } | |
170 | rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val); | |
171 | ||
d9288d0b KM |
172 | } |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
177 | int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod, | |
178 | struct rsnd_dai *rdai, | |
179 | int use_busif) | |
180 | { | |
181 | /* | |
182 | * DMA settings for SSIU | |
183 | */ | |
184 | if (use_busif) | |
185 | rsnd_mod_write(ssi_mod, SSI_CTRL, 0); | |
186 | ||
7b5ce975 KM |
187 | return 0; |
188 | } | |
189 | ||
ba9c949f | 190 | int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod, |
b42fccf6 | 191 | struct rsnd_dai *rdai) |
b8cc41e9 KM |
192 | { |
193 | struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); | |
194 | ||
195 | /* enable PIO interrupt if Gen2 */ | |
196 | if (rsnd_is_gen2(priv)) | |
197 | rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000); | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
ba9c949f | 202 | unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv, |
374e5426 | 203 | struct rsnd_dai_stream *io, |
1b7b08ef KM |
204 | struct snd_pcm_runtime *runtime) |
205 | { | |
b1eac430 | 206 | struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); |
ba9c949f | 207 | struct rsnd_src *src; |
b1eac430 | 208 | unsigned int rate = 0; |
1b7b08ef | 209 | |
b1eac430 KM |
210 | if (src_mod) { |
211 | src = rsnd_mod_to_src(src_mod); | |
212 | ||
213 | /* | |
214 | * return convert rate if SRC is used, | |
215 | * otherwise, return runtime->rate as usual | |
216 | */ | |
217 | rate = rsnd_src_convert_rate(src); | |
218 | } | |
1b7b08ef | 219 | |
1b7b08ef KM |
220 | if (!rate) |
221 | rate = runtime->rate; | |
222 | ||
223 | return rate; | |
224 | } | |
225 | ||
ba9c949f | 226 | static int rsnd_src_set_convert_rate(struct rsnd_mod *mod, |
b42fccf6 | 227 | struct rsnd_dai *rdai) |
1b7b08ef | 228 | { |
b42fccf6 | 229 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
1b7b08ef | 230 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f KM |
231 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
232 | u32 convert_rate = rsnd_src_convert_rate(src); | |
1b7b08ef KM |
233 | u32 fsrate = 0; |
234 | ||
235 | if (convert_rate) | |
236 | fsrate = 0x0400000 / convert_rate * runtime->rate; | |
237 | ||
238 | /* set/clear soft reset */ | |
239 | rsnd_mod_write(mod, SRC_SWRSR, 0); | |
240 | rsnd_mod_write(mod, SRC_SWRSR, 1); | |
241 | ||
242 | /* | |
243 | * Initialize the operation of the SRC internal circuits | |
ba9c949f | 244 | * see rsnd_src_start() |
1b7b08ef KM |
245 | */ |
246 | rsnd_mod_write(mod, SRC_SRCIR, 1); | |
247 | ||
248 | /* Set channel number and output bit length */ | |
d7bdbc5d | 249 | rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod)); |
1b7b08ef KM |
250 | |
251 | /* Enable the initial value of IFS */ | |
252 | if (fsrate) { | |
253 | rsnd_mod_write(mod, SRC_IFSCR, 1); | |
254 | ||
255 | /* Set initial value of IFS */ | |
256 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); | |
257 | } | |
258 | ||
259 | /* use DMA transfer */ | |
260 | rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
ba9c949f | 265 | static int rsnd_src_init(struct rsnd_mod *mod, |
b42fccf6 | 266 | struct rsnd_dai *rdai) |
1b7b08ef | 267 | { |
ba9c949f | 268 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 269 | |
79861bbb | 270 | clk_prepare_enable(src->clk); |
1b7b08ef | 271 | |
1b7b08ef KM |
272 | return 0; |
273 | } | |
274 | ||
ba9c949f | 275 | static int rsnd_src_quit(struct rsnd_mod *mod, |
b42fccf6 | 276 | struct rsnd_dai *rdai) |
1b7b08ef | 277 | { |
ba9c949f | 278 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 279 | |
79861bbb | 280 | clk_disable_unprepare(src->clk); |
1b7b08ef KM |
281 | |
282 | return 0; | |
283 | } | |
284 | ||
ba9c949f | 285 | static int rsnd_src_start(struct rsnd_mod *mod, |
b42fccf6 | 286 | struct rsnd_dai *rdai) |
1b7b08ef | 287 | { |
ba9c949f | 288 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef KM |
289 | |
290 | /* | |
291 | * Cancel the initialization and operate the SRC function | |
ba9c949f | 292 | * see rsnd_src_set_convert_rate() |
1b7b08ef KM |
293 | */ |
294 | rsnd_mod_write(mod, SRC_SRCIR, 0); | |
295 | ||
ba9c949f | 296 | if (rsnd_src_convert_rate(src)) |
1b7b08ef KM |
297 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | ||
ba9c949f | 303 | static int rsnd_src_stop(struct rsnd_mod *mod, |
b42fccf6 | 304 | struct rsnd_dai *rdai) |
1b7b08ef | 305 | { |
ba9c949f | 306 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 307 | |
ba9c949f | 308 | if (rsnd_src_convert_rate(src)) |
1b7b08ef KM |
309 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
1b7b08ef KM |
314 | /* |
315 | * Gen1 functions | |
316 | */ | |
317 | static int rsnd_src_set_route_gen1(struct rsnd_mod *mod, | |
b42fccf6 | 318 | struct rsnd_dai *rdai) |
374a5281 | 319 | { |
b42fccf6 | 320 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
ba9c949f | 321 | struct src_route_config { |
374a5281 KM |
322 | u32 mask; |
323 | int shift; | |
324 | } routes[] = { | |
325 | { 0xF, 0, }, /* 0 */ | |
326 | { 0xF, 4, }, /* 1 */ | |
327 | { 0xF, 8, }, /* 2 */ | |
328 | { 0x7, 12, }, /* 3 */ | |
329 | { 0x7, 16, }, /* 4 */ | |
330 | { 0x7, 20, }, /* 5 */ | |
331 | { 0x7, 24, }, /* 6 */ | |
332 | { 0x3, 28, }, /* 7 */ | |
333 | { 0x3, 30, }, /* 8 */ | |
334 | }; | |
374a5281 KM |
335 | u32 mask; |
336 | u32 val; | |
374a5281 KM |
337 | int id; |
338 | ||
374a5281 | 339 | id = rsnd_mod_id(mod); |
b5f3d7af | 340 | if (id < 0 || id >= ARRAY_SIZE(routes)) |
374a5281 KM |
341 | return -EIO; |
342 | ||
343 | /* | |
344 | * SRC_ROUTE_SELECT | |
345 | */ | |
346 | val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; | |
347 | val = val << routes[id].shift; | |
348 | mask = routes[id].mask << routes[id].shift; | |
349 | ||
350 | rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); | |
351 | ||
28dc4b63 KM |
352 | return 0; |
353 | } | |
354 | ||
ba9c949f | 355 | static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod, |
b42fccf6 | 356 | struct rsnd_dai *rdai) |
28dc4b63 | 357 | { |
b42fccf6 | 358 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
28dc4b63 | 359 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
ba9c949f | 360 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
28dc4b63 | 361 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f | 362 | u32 convert_rate = rsnd_src_convert_rate(src); |
28dc4b63 KM |
363 | u32 mask; |
364 | u32 val; | |
365 | int shift; | |
366 | int id = rsnd_mod_id(mod); | |
367 | int ret; | |
368 | ||
374a5281 KM |
369 | /* |
370 | * SRC_TIMING_SELECT | |
371 | */ | |
372 | shift = (id % 4) * 8; | |
373 | mask = 0x1F << shift; | |
ef749400 KM |
374 | |
375 | /* | |
376 | * ADG is used as source clock if SRC was used, | |
377 | * then, SSI WS is used as destination clock. | |
378 | * SSI WS is used as source clock if SRC is not used | |
379 | * (when playback, source/destination become reverse when capture) | |
380 | */ | |
28dc4b63 KM |
381 | ret = 0; |
382 | if (convert_rate) { | |
383 | /* use ADG */ | |
ef749400 | 384 | val = 0; |
28dc4b63 KM |
385 | ret = rsnd_adg_set_convert_clk_gen1(priv, mod, |
386 | runtime->rate, | |
387 | convert_rate); | |
388 | } else if (8 == id) { | |
389 | /* use SSI WS, but SRU8 is special */ | |
374a5281 | 390 | val = id << shift; |
28dc4b63 KM |
391 | } else { |
392 | /* use SSI WS */ | |
374a5281 | 393 | val = (id + 1) << shift; |
28dc4b63 KM |
394 | } |
395 | ||
396 | if (ret < 0) | |
397 | return ret; | |
374a5281 KM |
398 | |
399 | switch (id / 4) { | |
400 | case 0: | |
401 | rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); | |
402 | break; | |
403 | case 1: | |
404 | rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); | |
405 | break; | |
406 | case 2: | |
407 | rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); | |
408 | break; | |
409 | } | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
ba9c949f | 414 | static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod, |
b42fccf6 | 415 | struct rsnd_dai *rdai) |
374a5281 | 416 | { |
1b7b08ef | 417 | int ret; |
ef749400 | 418 | |
b42fccf6 | 419 | ret = rsnd_src_set_convert_rate(mod, rdai); |
1b7b08ef KM |
420 | if (ret < 0) |
421 | return ret; | |
ef749400 | 422 | |
1b7b08ef KM |
423 | /* Select SRC mode (fixed value) */ |
424 | rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); | |
ef749400 | 425 | |
1b7b08ef KM |
426 | /* Set the restriction value of the FS ratio (98%) */ |
427 | rsnd_mod_write(mod, SRC_MNFSR, | |
428 | rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98); | |
ef749400 | 429 | |
1b7b08ef | 430 | /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ |
ef749400 | 431 | |
374a5281 KM |
432 | return 0; |
433 | } | |
434 | ||
8aefda50 KM |
435 | static int rsnd_src_probe_gen1(struct rsnd_mod *mod, |
436 | struct rsnd_dai *rdai) | |
437 | { | |
438 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
439 | struct device *dev = rsnd_priv_to_dev(priv); | |
440 | ||
441 | dev_dbg(dev, "%s (Gen1) is probed\n", rsnd_mod_name(mod)); | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
ba9c949f | 446 | static int rsnd_src_init_gen1(struct rsnd_mod *mod, |
b42fccf6 | 447 | struct rsnd_dai *rdai) |
07539c1d | 448 | { |
374a5281 KM |
449 | int ret; |
450 | ||
b42fccf6 | 451 | ret = rsnd_src_init(mod, rdai); |
7b5ce975 KM |
452 | if (ret < 0) |
453 | return ret; | |
454 | ||
b42fccf6 | 455 | ret = rsnd_src_set_route_gen1(mod, rdai); |
374a5281 KM |
456 | if (ret < 0) |
457 | return ret; | |
458 | ||
b42fccf6 | 459 | ret = rsnd_src_set_convert_rate_gen1(mod, rdai); |
374a5281 KM |
460 | if (ret < 0) |
461 | return ret; | |
07539c1d | 462 | |
b42fccf6 | 463 | ret = rsnd_src_set_convert_timing_gen1(mod, rdai); |
28dc4b63 KM |
464 | if (ret < 0) |
465 | return ret; | |
466 | ||
a204d90c KM |
467 | return 0; |
468 | } | |
469 | ||
ba9c949f | 470 | static int rsnd_src_start_gen1(struct rsnd_mod *mod, |
b42fccf6 | 471 | struct rsnd_dai *rdai) |
a204d90c | 472 | { |
1b7b08ef | 473 | int id = rsnd_mod_id(mod); |
a204d90c | 474 | |
1b7b08ef | 475 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); |
374a5281 | 476 | |
b42fccf6 | 477 | return rsnd_src_start(mod, rdai); |
07539c1d KM |
478 | } |
479 | ||
ba9c949f | 480 | static int rsnd_src_stop_gen1(struct rsnd_mod *mod, |
b42fccf6 | 481 | struct rsnd_dai *rdai) |
a204d90c | 482 | { |
e7ce74ea | 483 | int id = rsnd_mod_id(mod); |
a204d90c | 484 | |
1b7b08ef | 485 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); |
e7ce74ea | 486 | |
b42fccf6 | 487 | return rsnd_src_stop(mod, rdai); |
a204d90c KM |
488 | } |
489 | ||
ba9c949f | 490 | static struct rsnd_mod_ops rsnd_src_gen1_ops = { |
8aefda50 KM |
491 | .name = SRC_NAME, |
492 | .probe = rsnd_src_probe_gen1, | |
ba9c949f KM |
493 | .init = rsnd_src_init_gen1, |
494 | .quit = rsnd_src_quit, | |
495 | .start = rsnd_src_start_gen1, | |
496 | .stop = rsnd_src_stop_gen1, | |
1b7b08ef | 497 | }; |
e7ce74ea | 498 | |
1b7b08ef KM |
499 | /* |
500 | * Gen2 functions | |
501 | */ | |
ba9c949f | 502 | static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod, |
b42fccf6 | 503 | struct rsnd_dai *rdai) |
629509c5 | 504 | { |
054cd7f4 KM |
505 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
506 | struct device *dev = rsnd_priv_to_dev(priv); | |
507 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
508 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
509 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
510 | uint ratio; | |
629509c5 KM |
511 | int ret; |
512 | ||
054cd7f4 KM |
513 | /* 6 - 1/6 are very enough ratio for SRC_BSDSR */ |
514 | if (!rsnd_src_convert_rate(src)) | |
515 | ratio = 0; | |
516 | else if (rsnd_src_convert_rate(src) > runtime->rate) | |
517 | ratio = 100 * rsnd_src_convert_rate(src) / runtime->rate; | |
518 | else | |
519 | ratio = 100 * runtime->rate / rsnd_src_convert_rate(src); | |
520 | ||
521 | if (ratio > 600) { | |
522 | dev_err(dev, "FSO/FSI ratio error\n"); | |
523 | return -EINVAL; | |
524 | } | |
525 | ||
b42fccf6 | 526 | ret = rsnd_src_set_convert_rate(mod, rdai); |
629509c5 KM |
527 | if (ret < 0) |
528 | return ret; | |
529 | ||
629509c5 KM |
530 | rsnd_mod_write(mod, SRC_SRCCR, 0x00011110); |
531 | ||
054cd7f4 KM |
532 | switch (rsnd_mod_id(mod)) { |
533 | case 5: | |
534 | case 6: | |
535 | case 7: | |
536 | case 8: | |
537 | rsnd_mod_write(mod, SRC_BSDSR, 0x02400000); | |
538 | break; | |
539 | default: | |
540 | rsnd_mod_write(mod, SRC_BSDSR, 0x01800000); | |
541 | break; | |
542 | } | |
543 | ||
629509c5 KM |
544 | rsnd_mod_write(mod, SRC_BSISR, 0x00100060); |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
ba9c949f | 549 | static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod, |
b42fccf6 | 550 | struct rsnd_dai *rdai) |
629509c5 | 551 | { |
b42fccf6 | 552 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
629509c5 | 553 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f KM |
554 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
555 | u32 convert_rate = rsnd_src_convert_rate(src); | |
629509c5 KM |
556 | int ret; |
557 | ||
558 | if (convert_rate) | |
559 | ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io, | |
560 | runtime->rate, | |
561 | convert_rate); | |
562 | else | |
563 | ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io); | |
564 | ||
565 | return ret; | |
566 | } | |
567 | ||
ba9c949f | 568 | static int rsnd_src_probe_gen2(struct rsnd_mod *mod, |
b42fccf6 | 569 | struct rsnd_dai *rdai) |
76c6fb5c KM |
570 | { |
571 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
ba9c949f | 572 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
76c6fb5c KM |
573 | struct device *dev = rsnd_priv_to_dev(priv); |
574 | int ret; | |
76c6fb5c KM |
575 | |
576 | ret = rsnd_dma_init(priv, | |
577 | rsnd_mod_to_dma(mod), | |
29e69fd2 | 578 | rsnd_info_is_playback(priv, src), |
ba9c949f | 579 | src->info->dma_id); |
76c6fb5c | 580 | if (ret < 0) |
ba9c949f | 581 | dev_err(dev, "SRC DMA failed\n"); |
76c6fb5c | 582 | |
8aefda50 KM |
583 | dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod)); |
584 | ||
76c6fb5c KM |
585 | return ret; |
586 | } | |
587 | ||
ba9c949f | 588 | static int rsnd_src_remove_gen2(struct rsnd_mod *mod, |
b42fccf6 | 589 | struct rsnd_dai *rdai) |
76c6fb5c KM |
590 | { |
591 | rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod)); | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
ba9c949f | 596 | static int rsnd_src_init_gen2(struct rsnd_mod *mod, |
b42fccf6 | 597 | struct rsnd_dai *rdai) |
629509c5 KM |
598 | { |
599 | int ret; | |
600 | ||
b42fccf6 | 601 | ret = rsnd_src_init(mod, rdai); |
629509c5 KM |
602 | if (ret < 0) |
603 | return ret; | |
604 | ||
b42fccf6 | 605 | ret = rsnd_src_set_convert_rate_gen2(mod, rdai); |
629509c5 KM |
606 | if (ret < 0) |
607 | return ret; | |
608 | ||
b42fccf6 | 609 | ret = rsnd_src_set_convert_timing_gen2(mod, rdai); |
629509c5 KM |
610 | if (ret < 0) |
611 | return ret; | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
ba9c949f | 616 | static int rsnd_src_start_gen2(struct rsnd_mod *mod, |
b42fccf6 | 617 | struct rsnd_dai *rdai) |
629509c5 | 618 | { |
bff58ea4 | 619 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
ba9c949f | 620 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
bff58ea4 | 621 | u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11; |
629509c5 | 622 | |
ba9c949f | 623 | rsnd_dma_start(rsnd_mod_to_dma(&src->mod)); |
629509c5 | 624 | |
bff58ea4 | 625 | rsnd_mod_write(mod, SRC_CTRL, val); |
629509c5 | 626 | |
b42fccf6 | 627 | return rsnd_src_start(mod, rdai); |
629509c5 KM |
628 | } |
629 | ||
ba9c949f | 630 | static int rsnd_src_stop_gen2(struct rsnd_mod *mod, |
b42fccf6 | 631 | struct rsnd_dai *rdai) |
629509c5 | 632 | { |
ba9c949f | 633 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
629509c5 | 634 | |
629509c5 KM |
635 | rsnd_mod_write(mod, SRC_CTRL, 0); |
636 | ||
ba9c949f | 637 | rsnd_dma_stop(rsnd_mod_to_dma(&src->mod)); |
629509c5 | 638 | |
b42fccf6 | 639 | return rsnd_src_stop(mod, rdai); |
629509c5 KM |
640 | } |
641 | ||
ba9c949f | 642 | static struct rsnd_mod_ops rsnd_src_gen2_ops = { |
8aefda50 | 643 | .name = SRC_NAME, |
ba9c949f KM |
644 | .probe = rsnd_src_probe_gen2, |
645 | .remove = rsnd_src_remove_gen2, | |
646 | .init = rsnd_src_init_gen2, | |
647 | .quit = rsnd_src_quit, | |
648 | .start = rsnd_src_start_gen2, | |
649 | .stop = rsnd_src_stop_gen2, | |
629509c5 KM |
650 | }; |
651 | ||
ba9c949f | 652 | struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id) |
07539c1d | 653 | { |
ba9c949f | 654 | if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv))) |
8b14719b | 655 | id = 0; |
07539c1d | 656 | |
ba9c949f | 657 | return &((struct rsnd_src *)(priv->src) + id)->mod; |
07539c1d KM |
658 | } |
659 | ||
90e8e50f KM |
660 | static void rsnd_of_parse_src(struct platform_device *pdev, |
661 | const struct rsnd_of_data *of_data, | |
662 | struct rsnd_priv *priv) | |
663 | { | |
664 | struct device_node *src_node; | |
665 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); | |
666 | struct rsnd_src_platform_info *src_info; | |
667 | struct device *dev = &pdev->dev; | |
668 | int nr; | |
669 | ||
670 | if (!of_data) | |
671 | return; | |
672 | ||
673 | src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); | |
674 | if (!src_node) | |
675 | return; | |
676 | ||
677 | nr = of_get_child_count(src_node); | |
678 | if (!nr) | |
f451e48d | 679 | goto rsnd_of_parse_src_end; |
90e8e50f KM |
680 | |
681 | src_info = devm_kzalloc(dev, | |
682 | sizeof(struct rsnd_src_platform_info) * nr, | |
683 | GFP_KERNEL); | |
684 | if (!src_info) { | |
685 | dev_err(dev, "src info allocation error\n"); | |
f451e48d | 686 | goto rsnd_of_parse_src_end; |
90e8e50f KM |
687 | } |
688 | ||
689 | info->src_info = src_info; | |
690 | info->src_info_nr = nr; | |
f451e48d KM |
691 | |
692 | rsnd_of_parse_src_end: | |
693 | of_node_put(src_node); | |
90e8e50f KM |
694 | } |
695 | ||
ba9c949f | 696 | int rsnd_src_probe(struct platform_device *pdev, |
90e8e50f | 697 | const struct rsnd_of_data *of_data, |
07539c1d KM |
698 | struct rsnd_priv *priv) |
699 | { | |
5da39cf3 | 700 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
07539c1d | 701 | struct device *dev = rsnd_priv_to_dev(priv); |
ba9c949f | 702 | struct rsnd_src *src; |
013f38fe | 703 | struct rsnd_mod_ops *ops; |
ef749400 | 704 | struct clk *clk; |
ba9c949f | 705 | char name[RSND_SRC_NAME_SIZE]; |
07539c1d KM |
706 | int i, nr; |
707 | ||
033e7ed8 KM |
708 | ops = NULL; |
709 | if (rsnd_is_gen1(priv)) | |
710 | ops = &rsnd_src_gen1_ops; | |
711 | if (rsnd_is_gen2(priv)) | |
712 | ops = &rsnd_src_gen2_ops; | |
713 | if (!ops) { | |
714 | dev_err(dev, "unknown Generation\n"); | |
715 | return -EIO; | |
716 | } | |
717 | ||
90e8e50f KM |
718 | rsnd_of_parse_src(pdev, of_data, priv); |
719 | ||
07539c1d | 720 | /* |
ba9c949f | 721 | * init SRC |
07539c1d | 722 | */ |
ba9c949f | 723 | nr = info->src_info_nr; |
389933d9 KM |
724 | if (!nr) |
725 | return 0; | |
726 | ||
ba9c949f KM |
727 | src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL); |
728 | if (!src) { | |
729 | dev_err(dev, "SRC allocate failed\n"); | |
07539c1d KM |
730 | return -ENOMEM; |
731 | } | |
732 | ||
ba9c949f KM |
733 | priv->src_nr = nr; |
734 | priv->src = src; | |
07539c1d | 735 | |
ba9c949f | 736 | for_each_rsnd_src(src, priv, i) { |
8aefda50 KM |
737 | snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d", |
738 | SRC_NAME, i); | |
ef749400 KM |
739 | |
740 | clk = devm_clk_get(dev, name); | |
741 | if (IS_ERR(clk)) | |
742 | return PTR_ERR(clk); | |
743 | ||
ba9c949f KM |
744 | src->info = &info->src_info[i]; |
745 | src->clk = clk; | |
07539c1d | 746 | |
ba9c949f | 747 | rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i); |
013f38fe | 748 | |
ba9c949f | 749 | dev_dbg(dev, "SRC%d probed\n", i); |
374a5281 | 750 | } |
07539c1d KM |
751 | |
752 | return 0; | |
753 | } |