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