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