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