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