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, | |
660cdce2 | 178 | struct rsnd_dai *rdai) |
d9288d0b KM |
179 | { |
180 | /* | |
181 | * DMA settings for SSIU | |
182 | */ | |
660cdce2 | 183 | rsnd_mod_write(ssi_mod, SSI_CTRL, 0); |
d9288d0b | 184 | |
7b5ce975 KM |
185 | return 0; |
186 | } | |
187 | ||
ba9c949f | 188 | int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod, |
b42fccf6 | 189 | struct rsnd_dai *rdai) |
b8cc41e9 KM |
190 | { |
191 | struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); | |
192 | ||
193 | /* enable PIO interrupt if Gen2 */ | |
194 | if (rsnd_is_gen2(priv)) | |
195 | rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000); | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
ba9c949f | 200 | unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv, |
374e5426 | 201 | struct rsnd_dai_stream *io, |
1b7b08ef KM |
202 | struct snd_pcm_runtime *runtime) |
203 | { | |
b1eac430 | 204 | struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); |
ba9c949f | 205 | struct rsnd_src *src; |
b1eac430 | 206 | unsigned int rate = 0; |
1b7b08ef | 207 | |
b1eac430 KM |
208 | if (src_mod) { |
209 | src = rsnd_mod_to_src(src_mod); | |
210 | ||
211 | /* | |
212 | * return convert rate if SRC is used, | |
213 | * otherwise, return runtime->rate as usual | |
214 | */ | |
215 | rate = rsnd_src_convert_rate(src); | |
216 | } | |
1b7b08ef | 217 | |
1b7b08ef KM |
218 | if (!rate) |
219 | rate = runtime->rate; | |
220 | ||
221 | return rate; | |
222 | } | |
223 | ||
ba9c949f | 224 | static int rsnd_src_set_convert_rate(struct rsnd_mod *mod, |
b42fccf6 | 225 | struct rsnd_dai *rdai) |
1b7b08ef | 226 | { |
b42fccf6 | 227 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
1b7b08ef | 228 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f KM |
229 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
230 | u32 convert_rate = rsnd_src_convert_rate(src); | |
1b7b08ef KM |
231 | u32 fsrate = 0; |
232 | ||
233 | if (convert_rate) | |
234 | fsrate = 0x0400000 / convert_rate * runtime->rate; | |
235 | ||
236 | /* set/clear soft reset */ | |
237 | rsnd_mod_write(mod, SRC_SWRSR, 0); | |
238 | rsnd_mod_write(mod, SRC_SWRSR, 1); | |
239 | ||
240 | /* | |
241 | * Initialize the operation of the SRC internal circuits | |
ba9c949f | 242 | * see rsnd_src_start() |
1b7b08ef KM |
243 | */ |
244 | rsnd_mod_write(mod, SRC_SRCIR, 1); | |
245 | ||
246 | /* Set channel number and output bit length */ | |
d7bdbc5d | 247 | rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod)); |
1b7b08ef KM |
248 | |
249 | /* Enable the initial value of IFS */ | |
250 | if (fsrate) { | |
251 | rsnd_mod_write(mod, SRC_IFSCR, 1); | |
252 | ||
253 | /* Set initial value of IFS */ | |
254 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); | |
255 | } | |
256 | ||
257 | /* use DMA transfer */ | |
258 | rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
ba9c949f | 263 | static int rsnd_src_init(struct rsnd_mod *mod, |
b42fccf6 | 264 | struct rsnd_dai *rdai) |
1b7b08ef | 265 | { |
ba9c949f | 266 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 267 | |
79861bbb | 268 | clk_prepare_enable(src->clk); |
1b7b08ef | 269 | |
1b7b08ef KM |
270 | return 0; |
271 | } | |
272 | ||
ba9c949f | 273 | static int rsnd_src_quit(struct rsnd_mod *mod, |
b42fccf6 | 274 | struct rsnd_dai *rdai) |
1b7b08ef | 275 | { |
ba9c949f | 276 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 277 | |
79861bbb | 278 | clk_disable_unprepare(src->clk); |
1b7b08ef KM |
279 | |
280 | return 0; | |
281 | } | |
282 | ||
ba9c949f | 283 | static int rsnd_src_start(struct rsnd_mod *mod, |
b42fccf6 | 284 | struct rsnd_dai *rdai) |
1b7b08ef | 285 | { |
ba9c949f | 286 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef KM |
287 | |
288 | /* | |
289 | * Cancel the initialization and operate the SRC function | |
ba9c949f | 290 | * see rsnd_src_set_convert_rate() |
1b7b08ef KM |
291 | */ |
292 | rsnd_mod_write(mod, SRC_SRCIR, 0); | |
293 | ||
ba9c949f | 294 | if (rsnd_src_convert_rate(src)) |
1b7b08ef KM |
295 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | ||
ba9c949f | 301 | static int rsnd_src_stop(struct rsnd_mod *mod, |
b42fccf6 | 302 | struct rsnd_dai *rdai) |
1b7b08ef | 303 | { |
ba9c949f | 304 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
1b7b08ef | 305 | |
ba9c949f | 306 | if (rsnd_src_convert_rate(src)) |
1b7b08ef KM |
307 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
1b7b08ef KM |
312 | /* |
313 | * Gen1 functions | |
314 | */ | |
315 | static int rsnd_src_set_route_gen1(struct rsnd_mod *mod, | |
b42fccf6 | 316 | struct rsnd_dai *rdai) |
374a5281 | 317 | { |
b42fccf6 | 318 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
ba9c949f | 319 | struct src_route_config { |
374a5281 KM |
320 | u32 mask; |
321 | int shift; | |
322 | } routes[] = { | |
323 | { 0xF, 0, }, /* 0 */ | |
324 | { 0xF, 4, }, /* 1 */ | |
325 | { 0xF, 8, }, /* 2 */ | |
326 | { 0x7, 12, }, /* 3 */ | |
327 | { 0x7, 16, }, /* 4 */ | |
328 | { 0x7, 20, }, /* 5 */ | |
329 | { 0x7, 24, }, /* 6 */ | |
330 | { 0x3, 28, }, /* 7 */ | |
331 | { 0x3, 30, }, /* 8 */ | |
332 | }; | |
374a5281 KM |
333 | u32 mask; |
334 | u32 val; | |
374a5281 KM |
335 | int id; |
336 | ||
374a5281 | 337 | id = rsnd_mod_id(mod); |
b5f3d7af | 338 | if (id < 0 || id >= ARRAY_SIZE(routes)) |
374a5281 KM |
339 | return -EIO; |
340 | ||
341 | /* | |
342 | * SRC_ROUTE_SELECT | |
343 | */ | |
344 | val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; | |
345 | val = val << routes[id].shift; | |
346 | mask = routes[id].mask << routes[id].shift; | |
347 | ||
348 | rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); | |
349 | ||
28dc4b63 KM |
350 | return 0; |
351 | } | |
352 | ||
ba9c949f | 353 | static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod, |
b42fccf6 | 354 | struct rsnd_dai *rdai) |
28dc4b63 | 355 | { |
b42fccf6 | 356 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
28dc4b63 | 357 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
ba9c949f | 358 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
28dc4b63 | 359 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f | 360 | u32 convert_rate = rsnd_src_convert_rate(src); |
28dc4b63 KM |
361 | u32 mask; |
362 | u32 val; | |
363 | int shift; | |
364 | int id = rsnd_mod_id(mod); | |
365 | int ret; | |
366 | ||
374a5281 KM |
367 | /* |
368 | * SRC_TIMING_SELECT | |
369 | */ | |
370 | shift = (id % 4) * 8; | |
371 | mask = 0x1F << shift; | |
ef749400 KM |
372 | |
373 | /* | |
374 | * ADG is used as source clock if SRC was used, | |
375 | * then, SSI WS is used as destination clock. | |
376 | * SSI WS is used as source clock if SRC is not used | |
377 | * (when playback, source/destination become reverse when capture) | |
378 | */ | |
28dc4b63 KM |
379 | ret = 0; |
380 | if (convert_rate) { | |
381 | /* use ADG */ | |
ef749400 | 382 | val = 0; |
28dc4b63 KM |
383 | ret = rsnd_adg_set_convert_clk_gen1(priv, mod, |
384 | runtime->rate, | |
385 | convert_rate); | |
386 | } else if (8 == id) { | |
387 | /* use SSI WS, but SRU8 is special */ | |
374a5281 | 388 | val = id << shift; |
28dc4b63 KM |
389 | } else { |
390 | /* use SSI WS */ | |
374a5281 | 391 | val = (id + 1) << shift; |
28dc4b63 KM |
392 | } |
393 | ||
394 | if (ret < 0) | |
395 | return ret; | |
374a5281 KM |
396 | |
397 | switch (id / 4) { | |
398 | case 0: | |
399 | rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); | |
400 | break; | |
401 | case 1: | |
402 | rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); | |
403 | break; | |
404 | case 2: | |
405 | rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); | |
406 | break; | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
ba9c949f | 412 | static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod, |
b42fccf6 | 413 | struct rsnd_dai *rdai) |
374a5281 | 414 | { |
1b7b08ef | 415 | int ret; |
ef749400 | 416 | |
b42fccf6 | 417 | ret = rsnd_src_set_convert_rate(mod, rdai); |
1b7b08ef KM |
418 | if (ret < 0) |
419 | return ret; | |
ef749400 | 420 | |
1b7b08ef KM |
421 | /* Select SRC mode (fixed value) */ |
422 | rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); | |
ef749400 | 423 | |
1b7b08ef KM |
424 | /* Set the restriction value of the FS ratio (98%) */ |
425 | rsnd_mod_write(mod, SRC_MNFSR, | |
426 | rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98); | |
ef749400 | 427 | |
1b7b08ef | 428 | /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ |
ef749400 | 429 | |
374a5281 KM |
430 | return 0; |
431 | } | |
432 | ||
8aefda50 KM |
433 | static int rsnd_src_probe_gen1(struct rsnd_mod *mod, |
434 | struct rsnd_dai *rdai) | |
435 | { | |
436 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
437 | struct device *dev = rsnd_priv_to_dev(priv); | |
438 | ||
30cc4faf KM |
439 | dev_dbg(dev, "%s[%d] (Gen1) is probed\n", |
440 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
8aefda50 KM |
441 | |
442 | return 0; | |
443 | } | |
444 | ||
ba9c949f | 445 | static int rsnd_src_init_gen1(struct rsnd_mod *mod, |
b42fccf6 | 446 | struct rsnd_dai *rdai) |
07539c1d | 447 | { |
374a5281 KM |
448 | int ret; |
449 | ||
b42fccf6 | 450 | ret = rsnd_src_init(mod, rdai); |
7b5ce975 KM |
451 | if (ret < 0) |
452 | return ret; | |
453 | ||
b42fccf6 | 454 | ret = rsnd_src_set_route_gen1(mod, rdai); |
374a5281 KM |
455 | if (ret < 0) |
456 | return ret; | |
457 | ||
b42fccf6 | 458 | ret = rsnd_src_set_convert_rate_gen1(mod, rdai); |
374a5281 KM |
459 | if (ret < 0) |
460 | return ret; | |
07539c1d | 461 | |
b42fccf6 | 462 | ret = rsnd_src_set_convert_timing_gen1(mod, rdai); |
28dc4b63 KM |
463 | if (ret < 0) |
464 | return ret; | |
465 | ||
a204d90c KM |
466 | return 0; |
467 | } | |
468 | ||
ba9c949f | 469 | static int rsnd_src_start_gen1(struct rsnd_mod *mod, |
b42fccf6 | 470 | struct rsnd_dai *rdai) |
a204d90c | 471 | { |
1b7b08ef | 472 | int id = rsnd_mod_id(mod); |
a204d90c | 473 | |
1b7b08ef | 474 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); |
374a5281 | 475 | |
b42fccf6 | 476 | return rsnd_src_start(mod, rdai); |
07539c1d KM |
477 | } |
478 | ||
ba9c949f | 479 | static int rsnd_src_stop_gen1(struct rsnd_mod *mod, |
b42fccf6 | 480 | struct rsnd_dai *rdai) |
a204d90c | 481 | { |
e7ce74ea | 482 | int id = rsnd_mod_id(mod); |
a204d90c | 483 | |
1b7b08ef | 484 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); |
e7ce74ea | 485 | |
b42fccf6 | 486 | return rsnd_src_stop(mod, rdai); |
a204d90c KM |
487 | } |
488 | ||
ba9c949f | 489 | static struct rsnd_mod_ops rsnd_src_gen1_ops = { |
8aefda50 KM |
490 | .name = SRC_NAME, |
491 | .probe = rsnd_src_probe_gen1, | |
ba9c949f KM |
492 | .init = rsnd_src_init_gen1, |
493 | .quit = rsnd_src_quit, | |
494 | .start = rsnd_src_start_gen1, | |
495 | .stop = rsnd_src_stop_gen1, | |
1b7b08ef | 496 | }; |
e7ce74ea | 497 | |
1b7b08ef KM |
498 | /* |
499 | * Gen2 functions | |
500 | */ | |
ba9c949f | 501 | static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod, |
b42fccf6 | 502 | struct rsnd_dai *rdai) |
629509c5 | 503 | { |
054cd7f4 KM |
504 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
505 | struct device *dev = rsnd_priv_to_dev(priv); | |
506 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
507 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
508 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
509 | uint ratio; | |
629509c5 KM |
510 | int ret; |
511 | ||
054cd7f4 KM |
512 | /* 6 - 1/6 are very enough ratio for SRC_BSDSR */ |
513 | if (!rsnd_src_convert_rate(src)) | |
514 | ratio = 0; | |
515 | else if (rsnd_src_convert_rate(src) > runtime->rate) | |
516 | ratio = 100 * rsnd_src_convert_rate(src) / runtime->rate; | |
517 | else | |
518 | ratio = 100 * runtime->rate / rsnd_src_convert_rate(src); | |
519 | ||
520 | if (ratio > 600) { | |
521 | dev_err(dev, "FSO/FSI ratio error\n"); | |
522 | return -EINVAL; | |
523 | } | |
524 | ||
b42fccf6 | 525 | ret = rsnd_src_set_convert_rate(mod, rdai); |
629509c5 KM |
526 | if (ret < 0) |
527 | return ret; | |
528 | ||
629509c5 KM |
529 | rsnd_mod_write(mod, SRC_SRCCR, 0x00011110); |
530 | ||
054cd7f4 KM |
531 | switch (rsnd_mod_id(mod)) { |
532 | case 5: | |
533 | case 6: | |
534 | case 7: | |
535 | case 8: | |
536 | rsnd_mod_write(mod, SRC_BSDSR, 0x02400000); | |
537 | break; | |
538 | default: | |
539 | rsnd_mod_write(mod, SRC_BSDSR, 0x01800000); | |
540 | break; | |
541 | } | |
542 | ||
629509c5 KM |
543 | rsnd_mod_write(mod, SRC_BSISR, 0x00100060); |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
ba9c949f | 548 | static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod, |
b42fccf6 | 549 | struct rsnd_dai *rdai) |
629509c5 | 550 | { |
b42fccf6 | 551 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
629509c5 | 552 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ba9c949f KM |
553 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
554 | u32 convert_rate = rsnd_src_convert_rate(src); | |
629509c5 KM |
555 | int ret; |
556 | ||
557 | if (convert_rate) | |
558 | ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io, | |
559 | runtime->rate, | |
560 | convert_rate); | |
561 | else | |
562 | ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io); | |
563 | ||
564 | return ret; | |
565 | } | |
566 | ||
ba9c949f | 567 | static int rsnd_src_probe_gen2(struct rsnd_mod *mod, |
b42fccf6 | 568 | struct rsnd_dai *rdai) |
76c6fb5c KM |
569 | { |
570 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
ba9c949f | 571 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
76c6fb5c KM |
572 | struct device *dev = rsnd_priv_to_dev(priv); |
573 | int ret; | |
76c6fb5c KM |
574 | |
575 | ret = rsnd_dma_init(priv, | |
576 | rsnd_mod_to_dma(mod), | |
29e69fd2 | 577 | rsnd_info_is_playback(priv, src), |
ba9c949f | 578 | src->info->dma_id); |
76c6fb5c | 579 | if (ret < 0) |
30cc4faf KM |
580 | dev_err(dev, "%s[%d] (Gen2) failed\n", |
581 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
582 | else | |
583 | dev_dbg(dev, "%s[%d] (Gen2) is probed\n", | |
584 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
8aefda50 | 585 | |
76c6fb5c KM |
586 | return ret; |
587 | } | |
588 | ||
ba9c949f | 589 | static int rsnd_src_remove_gen2(struct rsnd_mod *mod, |
b42fccf6 | 590 | struct rsnd_dai *rdai) |
76c6fb5c KM |
591 | { |
592 | rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod)); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
ba9c949f | 597 | static int rsnd_src_init_gen2(struct rsnd_mod *mod, |
b42fccf6 | 598 | struct rsnd_dai *rdai) |
629509c5 KM |
599 | { |
600 | int ret; | |
601 | ||
b42fccf6 | 602 | ret = rsnd_src_init(mod, rdai); |
629509c5 KM |
603 | if (ret < 0) |
604 | return ret; | |
605 | ||
b42fccf6 | 606 | ret = rsnd_src_set_convert_rate_gen2(mod, rdai); |
629509c5 KM |
607 | if (ret < 0) |
608 | return ret; | |
609 | ||
b42fccf6 | 610 | ret = rsnd_src_set_convert_timing_gen2(mod, rdai); |
629509c5 KM |
611 | if (ret < 0) |
612 | return ret; | |
613 | ||
614 | return 0; | |
615 | } | |
616 | ||
ba9c949f | 617 | static int rsnd_src_start_gen2(struct rsnd_mod *mod, |
b42fccf6 | 618 | struct rsnd_dai *rdai) |
629509c5 | 619 | { |
bff58ea4 | 620 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
ba9c949f | 621 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
bff58ea4 | 622 | u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11; |
629509c5 | 623 | |
ba9c949f | 624 | rsnd_dma_start(rsnd_mod_to_dma(&src->mod)); |
629509c5 | 625 | |
bff58ea4 | 626 | rsnd_mod_write(mod, SRC_CTRL, val); |
629509c5 | 627 | |
b42fccf6 | 628 | return rsnd_src_start(mod, rdai); |
629509c5 KM |
629 | } |
630 | ||
ba9c949f | 631 | static int rsnd_src_stop_gen2(struct rsnd_mod *mod, |
b42fccf6 | 632 | struct rsnd_dai *rdai) |
629509c5 | 633 | { |
ba9c949f | 634 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
629509c5 | 635 | |
629509c5 KM |
636 | rsnd_mod_write(mod, SRC_CTRL, 0); |
637 | ||
ba9c949f | 638 | rsnd_dma_stop(rsnd_mod_to_dma(&src->mod)); |
629509c5 | 639 | |
b42fccf6 | 640 | return rsnd_src_stop(mod, rdai); |
629509c5 KM |
641 | } |
642 | ||
ba9c949f | 643 | static struct rsnd_mod_ops rsnd_src_gen2_ops = { |
8aefda50 | 644 | .name = SRC_NAME, |
ba9c949f KM |
645 | .probe = rsnd_src_probe_gen2, |
646 | .remove = rsnd_src_remove_gen2, | |
647 | .init = rsnd_src_init_gen2, | |
648 | .quit = rsnd_src_quit, | |
649 | .start = rsnd_src_start_gen2, | |
650 | .stop = rsnd_src_stop_gen2, | |
629509c5 KM |
651 | }; |
652 | ||
ba9c949f | 653 | struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id) |
07539c1d | 654 | { |
ba9c949f | 655 | if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv))) |
8b14719b | 656 | id = 0; |
07539c1d | 657 | |
ba9c949f | 658 | return &((struct rsnd_src *)(priv->src) + id)->mod; |
07539c1d KM |
659 | } |
660 | ||
90e8e50f KM |
661 | static void rsnd_of_parse_src(struct platform_device *pdev, |
662 | const struct rsnd_of_data *of_data, | |
663 | struct rsnd_priv *priv) | |
664 | { | |
665 | struct device_node *src_node; | |
666 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); | |
667 | struct rsnd_src_platform_info *src_info; | |
668 | struct device *dev = &pdev->dev; | |
669 | int nr; | |
670 | ||
671 | if (!of_data) | |
672 | return; | |
673 | ||
674 | src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); | |
675 | if (!src_node) | |
676 | return; | |
677 | ||
678 | nr = of_get_child_count(src_node); | |
679 | if (!nr) | |
f451e48d | 680 | goto rsnd_of_parse_src_end; |
90e8e50f KM |
681 | |
682 | src_info = devm_kzalloc(dev, | |
683 | sizeof(struct rsnd_src_platform_info) * nr, | |
684 | GFP_KERNEL); | |
685 | if (!src_info) { | |
686 | dev_err(dev, "src info allocation error\n"); | |
f451e48d | 687 | goto rsnd_of_parse_src_end; |
90e8e50f KM |
688 | } |
689 | ||
690 | info->src_info = src_info; | |
691 | info->src_info_nr = nr; | |
f451e48d KM |
692 | |
693 | rsnd_of_parse_src_end: | |
694 | of_node_put(src_node); | |
90e8e50f KM |
695 | } |
696 | ||
ba9c949f | 697 | int rsnd_src_probe(struct platform_device *pdev, |
90e8e50f | 698 | const struct rsnd_of_data *of_data, |
07539c1d KM |
699 | struct rsnd_priv *priv) |
700 | { | |
5da39cf3 | 701 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
07539c1d | 702 | struct device *dev = rsnd_priv_to_dev(priv); |
ba9c949f | 703 | struct rsnd_src *src; |
013f38fe | 704 | struct rsnd_mod_ops *ops; |
ef749400 | 705 | struct clk *clk; |
ba9c949f | 706 | char name[RSND_SRC_NAME_SIZE]; |
07539c1d KM |
707 | int i, nr; |
708 | ||
033e7ed8 KM |
709 | ops = NULL; |
710 | if (rsnd_is_gen1(priv)) | |
711 | ops = &rsnd_src_gen1_ops; | |
712 | if (rsnd_is_gen2(priv)) | |
713 | ops = &rsnd_src_gen2_ops; | |
714 | if (!ops) { | |
715 | dev_err(dev, "unknown Generation\n"); | |
716 | return -EIO; | |
717 | } | |
718 | ||
90e8e50f KM |
719 | rsnd_of_parse_src(pdev, of_data, priv); |
720 | ||
07539c1d | 721 | /* |
ba9c949f | 722 | * init SRC |
07539c1d | 723 | */ |
ba9c949f | 724 | nr = info->src_info_nr; |
389933d9 KM |
725 | if (!nr) |
726 | return 0; | |
727 | ||
ba9c949f KM |
728 | src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL); |
729 | if (!src) { | |
730 | dev_err(dev, "SRC allocate failed\n"); | |
07539c1d KM |
731 | return -ENOMEM; |
732 | } | |
733 | ||
ba9c949f KM |
734 | priv->src_nr = nr; |
735 | priv->src = src; | |
07539c1d | 736 | |
ba9c949f | 737 | for_each_rsnd_src(src, priv, i) { |
8aefda50 KM |
738 | snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d", |
739 | SRC_NAME, i); | |
ef749400 KM |
740 | |
741 | clk = devm_clk_get(dev, name); | |
742 | if (IS_ERR(clk)) | |
743 | return PTR_ERR(clk); | |
744 | ||
ba9c949f KM |
745 | src->info = &info->src_info[i]; |
746 | src->clk = clk; | |
07539c1d | 747 | |
ba9c949f | 748 | rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i); |
013f38fe | 749 | |
ba9c949f | 750 | dev_dbg(dev, "SRC%d probed\n", i); |
374a5281 | 751 | } |
07539c1d KM |
752 | |
753 | return 0; | |
754 | } |