Commit | Line | Data |
---|---|---|
07539c1d KM |
1 | /* |
2 | * Renesas R-Car SCU support | |
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 | ||
13 | struct rsnd_scu { | |
14 | struct rsnd_scu_platform_info *info; /* rcar_snd.h */ | |
15 | struct rsnd_mod mod; | |
ef749400 | 16 | struct clk *clk; |
07539c1d KM |
17 | }; |
18 | ||
374a5281 | 19 | #define rsnd_scu_mode_flags(p) ((p)->info->flags) |
ef749400 KM |
20 | #define rsnd_scu_convert_rate(p) ((p)->info->convert_rate) |
21 | ||
22 | #define RSND_SCU_NAME_SIZE 16 | |
374a5281 KM |
23 | |
24 | /* | |
25 | * ADINR | |
26 | */ | |
27 | #define OTBL_24 (0 << 16) | |
28 | #define OTBL_22 (2 << 16) | |
29 | #define OTBL_20 (4 << 16) | |
30 | #define OTBL_18 (6 << 16) | |
31 | #define OTBL_16 (8 << 16) | |
32 | ||
ef749400 KM |
33 | /* |
34 | * image of SRC (Sampling Rate Converter) | |
35 | * | |
36 | * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ | |
37 | * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | | |
38 | * 44.1kHz <-> +-----+ +-----+ +-------+ | |
39 | * ... | |
40 | * | |
41 | */ | |
374a5281 | 42 | |
c926b746 KM |
43 | /* |
44 | * scu.c is caring... | |
45 | * | |
46 | * Gen1 | |
47 | * | |
48 | * [mem] -> [SRU] -> [SSI] | |
49 | * |--------| | |
50 | * | |
51 | * Gen2 | |
52 | * | |
53 | * [mem] -> [SCU] -> [SSIU] -> [SSI] | |
54 | * |-----------------| | |
55 | */ | |
56 | ||
41c6221c KM |
57 | /* |
58 | * How to use SRC bypass mode for debugging | |
59 | * | |
60 | * SRC has bypass mode, and it is useful for debugging. | |
61 | * In Gen2 case, | |
62 | * SRCm_MODE controls whether SRC is used or not | |
63 | * SSI_MODE0 controls whether SSIU which receives SRC data | |
64 | * is used or not. | |
65 | * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC, | |
66 | * but SRC bypass mode needs SSI_MODE0 only. | |
67 | * | |
68 | * This driver request | |
69 | * struct rsnd_scu_platform_info { | |
70 | * u32 flags; | |
71 | * u32 convert_rate; | |
72 | * } | |
73 | * | |
74 | * rsnd_scu_hpbif_is_enable() will be true | |
75 | * if flags had RSND_SCU_USE_HPBIF, | |
76 | * and it controls whether SSIU is used or not. | |
77 | * | |
78 | * rsnd_scu_convert_rate() indicates | |
79 | * above convert_rate, and it controls | |
80 | * whether SRC is used or not. | |
81 | * | |
82 | * ex) doesn't use SRC | |
83 | * struct rsnd_scu_platform_info info = { | |
84 | * .flags = 0, | |
85 | * .convert_rate = 0, | |
86 | * }; | |
87 | * | |
88 | * ex) uses SRC | |
89 | * struct rsnd_scu_platform_info info = { | |
90 | * .flags = RSND_SCU_USE_HPBIF, | |
91 | * .convert_rate = 48000, | |
92 | * }; | |
93 | * | |
94 | * ex) uses SRC bypass mode | |
95 | * struct rsnd_scu_platform_info info = { | |
96 | * .flags = RSND_SCU_USE_HPBIF, | |
97 | * .convert_rate = 0, | |
98 | * }; | |
99 | * | |
100 | */ | |
101 | ||
07539c1d KM |
102 | #define rsnd_mod_to_scu(_mod) \ |
103 | container_of((_mod), struct rsnd_scu, mod) | |
104 | ||
105 | #define for_each_rsnd_scu(pos, priv, i) \ | |
106 | for ((i) = 0; \ | |
107 | ((i) < rsnd_scu_nr(priv)) && \ | |
108 | ((pos) = (struct rsnd_scu *)(priv)->scu + i); \ | |
109 | i++) | |
110 | ||
7b5ce975 KM |
111 | static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod, |
112 | struct rsnd_dai *rdai, | |
113 | struct rsnd_dai_stream *io) | |
114 | { | |
115 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
116 | int id = rsnd_mod_id(mod); | |
117 | ||
118 | /* | |
119 | * SSI_MODE0 | |
120 | */ | |
121 | rsnd_mod_bset(mod, SSI_MODE0, (1 << id), | |
122 | rsnd_scu_hpbif_is_enable(mod) ? 0 : (1 << id)); | |
123 | ||
124 | /* | |
125 | * SSI_MODE1 | |
126 | */ | |
127 | if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) { | |
128 | int shift = -1; | |
129 | switch (id) { | |
130 | case 1: | |
131 | shift = 0; | |
132 | break; | |
133 | case 2: | |
134 | shift = 2; | |
135 | break; | |
136 | case 4: | |
137 | shift = 16; | |
138 | break; | |
139 | } | |
140 | ||
141 | if (shift >= 0) | |
142 | rsnd_mod_bset(mod, SSI_MODE1, | |
143 | 0x3 << shift, | |
144 | rsnd_dai_is_clk_master(rdai) ? | |
145 | 0x2 << shift : 0x1 << shift); | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
2582718c | 151 | /* Gen1 only */ |
47718dc7 | 152 | static int rsnd_src_set_route_if_gen1( |
374a5281 KM |
153 | struct rsnd_mod *mod, |
154 | struct rsnd_dai *rdai, | |
155 | struct rsnd_dai_stream *io) | |
156 | { | |
157 | struct scu_route_config { | |
158 | u32 mask; | |
159 | int shift; | |
160 | } routes[] = { | |
161 | { 0xF, 0, }, /* 0 */ | |
162 | { 0xF, 4, }, /* 1 */ | |
163 | { 0xF, 8, }, /* 2 */ | |
164 | { 0x7, 12, }, /* 3 */ | |
165 | { 0x7, 16, }, /* 4 */ | |
166 | { 0x7, 20, }, /* 5 */ | |
167 | { 0x7, 24, }, /* 6 */ | |
168 | { 0x3, 28, }, /* 7 */ | |
169 | { 0x3, 30, }, /* 8 */ | |
170 | }; | |
47718dc7 | 171 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
ef749400 | 172 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
374a5281 KM |
173 | u32 mask; |
174 | u32 val; | |
175 | int shift; | |
176 | int id; | |
177 | ||
178 | /* | |
179 | * Gen1 only | |
180 | */ | |
181 | if (!rsnd_is_gen1(priv)) | |
182 | return 0; | |
183 | ||
184 | id = rsnd_mod_id(mod); | |
b5f3d7af | 185 | if (id < 0 || id >= ARRAY_SIZE(routes)) |
374a5281 KM |
186 | return -EIO; |
187 | ||
188 | /* | |
189 | * SRC_ROUTE_SELECT | |
190 | */ | |
191 | val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; | |
192 | val = val << routes[id].shift; | |
193 | mask = routes[id].mask << routes[id].shift; | |
194 | ||
195 | rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); | |
196 | ||
197 | /* | |
198 | * SRC_TIMING_SELECT | |
199 | */ | |
200 | shift = (id % 4) * 8; | |
201 | mask = 0x1F << shift; | |
ef749400 KM |
202 | |
203 | /* | |
204 | * ADG is used as source clock if SRC was used, | |
205 | * then, SSI WS is used as destination clock. | |
206 | * SSI WS is used as source clock if SRC is not used | |
207 | * (when playback, source/destination become reverse when capture) | |
208 | */ | |
209 | if (rsnd_scu_convert_rate(scu)) /* use ADG */ | |
210 | val = 0; | |
211 | else if (8 == id) /* use SSI WS, but SRU8 is special */ | |
374a5281 | 212 | val = id << shift; |
ef749400 | 213 | else /* use SSI WS */ |
374a5281 KM |
214 | val = (id + 1) << shift; |
215 | ||
216 | switch (id / 4) { | |
217 | case 0: | |
218 | rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); | |
219 | break; | |
220 | case 1: | |
221 | rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); | |
222 | break; | |
223 | case 2: | |
224 | rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); | |
225 | break; | |
226 | } | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
ef749400 KM |
231 | unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv, |
232 | struct rsnd_mod *ssi_mod, | |
233 | struct snd_pcm_runtime *runtime) | |
374a5281 | 234 | { |
ef749400 KM |
235 | struct rsnd_scu *scu; |
236 | unsigned int rate; | |
374a5281 | 237 | |
ef749400 KM |
238 | /* this function is assuming SSI id = SCU id here */ |
239 | scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod))); | |
374a5281 | 240 | |
ef749400 KM |
241 | /* |
242 | * return convert rate if SRC is used, | |
243 | * otherwise, return runtime->rate as usual | |
244 | */ | |
245 | rate = rsnd_scu_convert_rate(scu); | |
246 | if (!rate) | |
247 | rate = runtime->rate; | |
248 | ||
249 | return rate; | |
374a5281 KM |
250 | } |
251 | ||
f80e1c96 KM |
252 | static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod, |
253 | struct rsnd_dai *rdai, | |
254 | struct rsnd_dai_stream *io) | |
374a5281 | 255 | { |
47718dc7 | 256 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
374a5281 | 257 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
ef749400 KM |
258 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
259 | u32 convert_rate = rsnd_scu_convert_rate(scu); | |
374a5281 KM |
260 | u32 adinr = runtime->channels; |
261 | ||
ef749400 KM |
262 | /* set/clear soft reset */ |
263 | rsnd_mod_write(mod, SRC_SWRSR, 0); | |
264 | rsnd_mod_write(mod, SRC_SWRSR, 1); | |
265 | ||
266 | /* Initialize the operation of the SRC internal circuits */ | |
267 | rsnd_mod_write(mod, SRC_SRCIR, 1); | |
268 | ||
269 | /* Set channel number and output bit length */ | |
374a5281 KM |
270 | switch (runtime->sample_bits) { |
271 | case 16: | |
272 | adinr |= OTBL_16; | |
273 | break; | |
274 | case 32: | |
275 | adinr |= OTBL_24; | |
276 | break; | |
277 | default: | |
278 | return -EIO; | |
279 | } | |
690ef81e | 280 | rsnd_mod_write(mod, SRC_ADINR, adinr); |
374a5281 | 281 | |
ef749400 KM |
282 | if (convert_rate) { |
283 | u32 fsrate = 0x0400000 / convert_rate * runtime->rate; | |
284 | int ret; | |
285 | ||
286 | /* Enable the initial value of IFS */ | |
287 | rsnd_mod_write(mod, SRC_IFSCR, 1); | |
288 | ||
289 | /* Set initial value of IFS */ | |
290 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); | |
291 | ||
292 | /* Select SRC mode (fixed value) */ | |
293 | rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); | |
294 | ||
295 | /* Set the restriction value of the FS ratio (98%) */ | |
296 | rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98); | |
297 | ||
298 | if (rsnd_is_gen1(priv)) { | |
299 | /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ | |
300 | } | |
374a5281 | 301 | |
ef749400 KM |
302 | /* set convert clock */ |
303 | ret = rsnd_adg_set_convert_clk(priv, mod, | |
304 | runtime->rate, | |
305 | convert_rate); | |
306 | if (ret < 0) | |
307 | return ret; | |
308 | } | |
309 | ||
310 | /* Cancel the initialization and operate the SRC function */ | |
311 | rsnd_mod_write(mod, SRC_SRCIR, 0); | |
312 | ||
313 | /* use DMA transfer */ | |
0290d2a4 | 314 | rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); |
ef749400 | 315 | |
374a5281 KM |
316 | return 0; |
317 | } | |
318 | ||
cdcfcac9 KM |
319 | bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod) |
320 | { | |
321 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); | |
322 | u32 flags = rsnd_scu_mode_flags(scu); | |
323 | ||
324 | return !!(flags & RSND_SCU_USE_HPBIF); | |
325 | } | |
326 | ||
a204d90c | 327 | static int rsnd_scu_init(struct rsnd_mod *mod, |
07539c1d KM |
328 | struct rsnd_dai *rdai, |
329 | struct rsnd_dai_stream *io) | |
330 | { | |
ef749400 | 331 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
374a5281 KM |
332 | int ret; |
333 | ||
ef749400 KM |
334 | clk_enable(scu->clk); |
335 | ||
7b5ce975 KM |
336 | ret = rsnd_scu_ssi_mode_init(mod, rdai, io); |
337 | if (ret < 0) | |
338 | return ret; | |
339 | ||
47718dc7 | 340 | ret = rsnd_src_set_route_if_gen1(mod, rdai, io); |
374a5281 KM |
341 | if (ret < 0) |
342 | return ret; | |
343 | ||
f80e1c96 | 344 | ret = rsnd_scu_set_convert_rate(mod, rdai, io); |
374a5281 KM |
345 | if (ret < 0) |
346 | return ret; | |
07539c1d | 347 | |
a204d90c KM |
348 | return 0; |
349 | } | |
350 | ||
351 | static int rsnd_scu_quit(struct rsnd_mod *mod, | |
352 | struct rsnd_dai *rdai, | |
353 | struct rsnd_dai_stream *io) | |
354 | { | |
355 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); | |
356 | ||
357 | clk_disable(scu->clk); | |
374a5281 | 358 | |
07539c1d KM |
359 | return 0; |
360 | } | |
361 | ||
a204d90c KM |
362 | static int rsnd_scu_start(struct rsnd_mod *mod, |
363 | struct rsnd_dai *rdai, | |
364 | struct rsnd_dai_stream *io) | |
365 | { | |
366 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
367 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); | |
e7ce74ea | 368 | int id = rsnd_mod_id(mod); |
a204d90c | 369 | |
e7ce74ea KM |
370 | if (rsnd_is_gen1(priv)) |
371 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); | |
372 | ||
373 | if (rsnd_scu_convert_rate(scu)) | |
374 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); | |
375 | ||
376 | return 0; | |
a204d90c KM |
377 | } |
378 | ||
ef749400 KM |
379 | static int rsnd_scu_stop(struct rsnd_mod *mod, |
380 | struct rsnd_dai *rdai, | |
381 | struct rsnd_dai_stream *io) | |
382 | { | |
383 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
384 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); | |
e7ce74ea KM |
385 | int id = rsnd_mod_id(mod); |
386 | ||
387 | if (rsnd_is_gen1(priv)) | |
388 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); | |
ef749400 | 389 | |
e7ce74ea KM |
390 | if (rsnd_scu_convert_rate(scu)) |
391 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); | |
ef749400 | 392 | |
ef749400 KM |
393 | return 0; |
394 | } | |
395 | ||
07539c1d KM |
396 | static struct rsnd_mod_ops rsnd_scu_ops = { |
397 | .name = "scu", | |
a204d90c KM |
398 | .init = rsnd_scu_init, |
399 | .quit = rsnd_scu_quit, | |
07539c1d | 400 | .start = rsnd_scu_start, |
ef749400 | 401 | .stop = rsnd_scu_stop, |
07539c1d KM |
402 | }; |
403 | ||
013f38fe KM |
404 | static struct rsnd_mod_ops rsnd_scu_non_ops = { |
405 | .name = "scu (non)", | |
7b5ce975 | 406 | .init = rsnd_scu_ssi_mode_init, |
013f38fe KM |
407 | }; |
408 | ||
07539c1d KM |
409 | struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id) |
410 | { | |
8b14719b TI |
411 | if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv))) |
412 | id = 0; | |
07539c1d KM |
413 | |
414 | return &((struct rsnd_scu *)(priv->scu) + id)->mod; | |
415 | } | |
416 | ||
417 | int rsnd_scu_probe(struct platform_device *pdev, | |
418 | struct rcar_snd_info *info, | |
419 | struct rsnd_priv *priv) | |
420 | { | |
421 | struct device *dev = rsnd_priv_to_dev(priv); | |
422 | struct rsnd_scu *scu; | |
013f38fe | 423 | struct rsnd_mod_ops *ops; |
ef749400 KM |
424 | struct clk *clk; |
425 | char name[RSND_SCU_NAME_SIZE]; | |
07539c1d KM |
426 | int i, nr; |
427 | ||
428 | /* | |
429 | * init SCU | |
430 | */ | |
431 | nr = info->scu_info_nr; | |
432 | scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL); | |
433 | if (!scu) { | |
434 | dev_err(dev, "SCU allocate failed\n"); | |
435 | return -ENOMEM; | |
436 | } | |
437 | ||
438 | priv->scu_nr = nr; | |
439 | priv->scu = scu; | |
440 | ||
441 | for_each_rsnd_scu(scu, priv, i) { | |
ef749400 KM |
442 | snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i); |
443 | ||
444 | clk = devm_clk_get(dev, name); | |
445 | if (IS_ERR(clk)) | |
446 | return PTR_ERR(clk); | |
447 | ||
07539c1d | 448 | scu->info = &info->scu_info[i]; |
ef749400 | 449 | scu->clk = clk; |
07539c1d | 450 | |
013f38fe KM |
451 | ops = &rsnd_scu_non_ops; |
452 | if (rsnd_scu_hpbif_is_enable(&scu->mod)) | |
453 | ops = &rsnd_scu_ops; | |
454 | ||
455 | rsnd_mod_init(priv, &scu->mod, ops, i); | |
456 | ||
374a5281 KM |
457 | dev_dbg(dev, "SCU%d probed\n", i); |
458 | } | |
07539c1d KM |
459 | dev_dbg(dev, "scu probed\n"); |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | void rsnd_scu_remove(struct platform_device *pdev, | |
465 | struct rsnd_priv *priv) | |
466 | { | |
467 | } |