ASoC: rsnd: rsnd_ssi_probe() goes forwarder than rsnd_scu_probe()
[deliverable/linux.git] / sound / soc / sh / rcar / scu.c
CommitLineData
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
13struct 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
1b7b08ef
KM
113/*
114 * Gen1/Gen2 common functions
115 */
7b5ce975
KM
116static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
117 struct rsnd_dai *rdai,
118 struct rsnd_dai_stream *io)
119{
120 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
96c7c0d6 121 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
7b5ce975
KM
122 int id = rsnd_mod_id(mod);
123
124 /*
125 * SSI_MODE0
126 */
127 rsnd_mod_bset(mod, SSI_MODE0, (1 << id),
96c7c0d6 128 rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << id));
7b5ce975
KM
129
130 /*
131 * SSI_MODE1
132 */
133 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
134 int shift = -1;
135 switch (id) {
136 case 1:
137 shift = 0;
138 break;
139 case 2:
140 shift = 2;
141 break;
142 case 4:
143 shift = 16;
144 break;
145 }
146
147 if (shift >= 0)
148 rsnd_mod_bset(mod, SSI_MODE1,
149 0x3 << shift,
150 rsnd_dai_is_clk_master(rdai) ?
151 0x2 << shift : 0x1 << shift);
152 }
153
154 return 0;
155}
156
1b7b08ef
KM
157unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
158 struct rsnd_mod *ssi_mod,
159 struct snd_pcm_runtime *runtime)
160{
161 struct rsnd_scu *scu;
162 unsigned int rate;
163
164 /* this function is assuming SSI id = SCU id here */
165 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
166
167 /*
168 * return convert rate if SRC is used,
169 * otherwise, return runtime->rate as usual
170 */
171 rate = rsnd_scu_convert_rate(scu);
172 if (!rate)
173 rate = runtime->rate;
174
175 return rate;
176}
177
178static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
179 struct rsnd_dai *rdai,
180 struct rsnd_dai_stream *io)
181{
182 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
183 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
184 u32 convert_rate = rsnd_scu_convert_rate(scu);
185 u32 adinr = runtime->channels;
186 u32 fsrate = 0;
187
188 if (convert_rate)
189 fsrate = 0x0400000 / convert_rate * runtime->rate;
190
191 /* set/clear soft reset */
192 rsnd_mod_write(mod, SRC_SWRSR, 0);
193 rsnd_mod_write(mod, SRC_SWRSR, 1);
194
195 /*
196 * Initialize the operation of the SRC internal circuits
197 * see rsnd_scu_start()
198 */
199 rsnd_mod_write(mod, SRC_SRCIR, 1);
200
201 /* Set channel number and output bit length */
202 switch (runtime->sample_bits) {
203 case 16:
204 adinr |= OTBL_16;
205 break;
206 case 32:
207 adinr |= OTBL_24;
208 break;
209 default:
210 return -EIO;
211 }
212 rsnd_mod_write(mod, SRC_ADINR, adinr);
213
214 /* Enable the initial value of IFS */
215 if (fsrate) {
216 rsnd_mod_write(mod, SRC_IFSCR, 1);
217
218 /* Set initial value of IFS */
219 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
220 }
221
222 /* use DMA transfer */
223 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
224
225 return 0;
226}
227
228static int rsnd_scu_init(struct rsnd_mod *mod,
229 struct rsnd_dai *rdai,
230 struct rsnd_dai_stream *io)
231{
232 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
233 int ret;
234
235 clk_enable(scu->clk);
236
237 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
238 if (ret < 0)
239 return ret;
240
241 return 0;
242}
243
244static int rsnd_scu_quit(struct rsnd_mod *mod,
245 struct rsnd_dai *rdai,
246 struct rsnd_dai_stream *io)
247{
248 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
249
250 clk_disable(scu->clk);
251
252 return 0;
253}
254
255static int rsnd_scu_start(struct rsnd_mod *mod,
256 struct rsnd_dai *rdai,
257 struct rsnd_dai_stream *io)
258{
259 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
260
261 /*
262 * Cancel the initialization and operate the SRC function
263 * see rsnd_scu_set_convert_rate()
264 */
265 rsnd_mod_write(mod, SRC_SRCIR, 0);
266
267 if (rsnd_scu_convert_rate(scu))
268 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
269
270 return 0;
271}
272
273
274static int rsnd_scu_stop(struct rsnd_mod *mod,
275 struct rsnd_dai *rdai,
276 struct rsnd_dai_stream *io)
277{
278 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
279
280 if (rsnd_scu_convert_rate(scu))
281 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
282
283 return 0;
284}
285
286static struct rsnd_mod_ops rsnd_scu_non_ops = {
287 .name = "scu (non)",
288};
289
290/*
291 * Gen1 functions
292 */
293static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
294 struct rsnd_dai *rdai,
295 struct rsnd_dai_stream *io)
374a5281
KM
296{
297 struct scu_route_config {
298 u32 mask;
299 int shift;
300 } routes[] = {
301 { 0xF, 0, }, /* 0 */
302 { 0xF, 4, }, /* 1 */
303 { 0xF, 8, }, /* 2 */
304 { 0x7, 12, }, /* 3 */
305 { 0x7, 16, }, /* 4 */
306 { 0x7, 20, }, /* 5 */
307 { 0x7, 24, }, /* 6 */
308 { 0x3, 28, }, /* 7 */
309 { 0x3, 30, }, /* 8 */
310 };
374a5281
KM
311 u32 mask;
312 u32 val;
374a5281
KM
313 int id;
314
374a5281 315 id = rsnd_mod_id(mod);
b5f3d7af 316 if (id < 0 || id >= ARRAY_SIZE(routes))
374a5281
KM
317 return -EIO;
318
319 /*
320 * SRC_ROUTE_SELECT
321 */
322 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
323 val = val << routes[id].shift;
324 mask = routes[id].mask << routes[id].shift;
325
326 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
327
28dc4b63
KM
328 return 0;
329}
330
331static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
332 struct rsnd_dai *rdai,
333 struct rsnd_dai_stream *io)
334{
335 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
336 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
337 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
338 u32 convert_rate = rsnd_scu_convert_rate(scu);
339 u32 mask;
340 u32 val;
341 int shift;
342 int id = rsnd_mod_id(mod);
343 int ret;
344
374a5281
KM
345 /*
346 * SRC_TIMING_SELECT
347 */
348 shift = (id % 4) * 8;
349 mask = 0x1F << shift;
ef749400
KM
350
351 /*
352 * ADG is used as source clock if SRC was used,
353 * then, SSI WS is used as destination clock.
354 * SSI WS is used as source clock if SRC is not used
355 * (when playback, source/destination become reverse when capture)
356 */
28dc4b63
KM
357 ret = 0;
358 if (convert_rate) {
359 /* use ADG */
ef749400 360 val = 0;
28dc4b63
KM
361 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
362 runtime->rate,
363 convert_rate);
364 } else if (8 == id) {
365 /* use SSI WS, but SRU8 is special */
374a5281 366 val = id << shift;
28dc4b63
KM
367 } else {
368 /* use SSI WS */
374a5281 369 val = (id + 1) << shift;
28dc4b63
KM
370 }
371
372 if (ret < 0)
373 return ret;
374a5281
KM
374
375 switch (id / 4) {
376 case 0:
377 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
378 break;
379 case 1:
380 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
381 break;
382 case 2:
383 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
384 break;
385 }
386
387 return 0;
388}
389
1b7b08ef
KM
390static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
391 struct rsnd_dai *rdai,
392 struct rsnd_dai_stream *io)
374a5281 393{
1b7b08ef 394 int ret;
ef749400 395
1b7b08ef
KM
396 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
397 if (ret < 0)
398 return ret;
ef749400 399
1b7b08ef
KM
400 /* Select SRC mode (fixed value) */
401 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
ef749400 402
1b7b08ef
KM
403 /* Set the restriction value of the FS ratio (98%) */
404 rsnd_mod_write(mod, SRC_MNFSR,
405 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
ef749400 406
1b7b08ef 407 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
ef749400 408
374a5281
KM
409 return 0;
410}
411
1b7b08ef
KM
412static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
413 struct rsnd_dai *rdai,
414 struct rsnd_dai_stream *io)
07539c1d 415{
374a5281
KM
416 int ret;
417
1b7b08ef 418 ret = rsnd_scu_init(mod, rdai, io);
7b5ce975
KM
419 if (ret < 0)
420 return ret;
421
1b7b08ef 422 ret = rsnd_src_set_route_gen1(mod, rdai, io);
374a5281
KM
423 if (ret < 0)
424 return ret;
425
1b7b08ef 426 ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
374a5281
KM
427 if (ret < 0)
428 return ret;
07539c1d 429
28dc4b63
KM
430 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
431 if (ret < 0)
432 return ret;
433
a204d90c
KM
434 return 0;
435}
436
1b7b08ef
KM
437static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
438 struct rsnd_dai *rdai,
439 struct rsnd_dai_stream *io)
a204d90c 440{
1b7b08ef 441 int id = rsnd_mod_id(mod);
a204d90c 442
1b7b08ef 443 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
374a5281 444
1b7b08ef 445 return rsnd_scu_start(mod, rdai, io);
07539c1d
KM
446}
447
1b7b08ef
KM
448static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
449 struct rsnd_dai *rdai,
450 struct rsnd_dai_stream *io)
a204d90c 451{
e7ce74ea 452 int id = rsnd_mod_id(mod);
a204d90c 453
1b7b08ef 454 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
e7ce74ea 455
1b7b08ef 456 return rsnd_scu_stop(mod, rdai, io);
a204d90c
KM
457}
458
1b7b08ef
KM
459static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
460 .name = "sru (gen1)",
461 .init = rsnd_scu_init_gen1,
462 .quit = rsnd_scu_quit,
463 .start = rsnd_scu_start_gen1,
464 .stop = rsnd_scu_stop_gen1,
465};
e7ce74ea 466
1b7b08ef
KM
467static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = {
468 .name = "non-sru (gen1)",
469 .init = rsnd_scu_ssi_mode_init,
470};
ef749400 471
1b7b08ef
KM
472/*
473 * Gen2 functions
474 */
475static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod,
476 struct rsnd_dai *rdai,
477 struct rsnd_dai_stream *io)
478{
479 /* enable PIO interrupt */
480 rsnd_mod_write(mod, INT_ENABLE, 0x0f000000);
ef749400 481
ef749400
KM
482 return 0;
483}
484
1b7b08ef
KM
485static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = {
486 .name = "non-scu (gen2)",
7b5ce975 487 .init = rsnd_scu_ssi_mode_init,
1b7b08ef 488 .start = rsnd_scu_start_non_gen2,
013f38fe
KM
489};
490
07539c1d
KM
491struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
492{
8b14719b
TI
493 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
494 id = 0;
07539c1d
KM
495
496 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
497}
498
499int rsnd_scu_probe(struct platform_device *pdev,
500 struct rcar_snd_info *info,
501 struct rsnd_priv *priv)
502{
503 struct device *dev = rsnd_priv_to_dev(priv);
504 struct rsnd_scu *scu;
013f38fe 505 struct rsnd_mod_ops *ops;
ef749400
KM
506 struct clk *clk;
507 char name[RSND_SCU_NAME_SIZE];
07539c1d
KM
508 int i, nr;
509
510 /*
511 * init SCU
512 */
513 nr = info->scu_info_nr;
514 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
515 if (!scu) {
516 dev_err(dev, "SCU allocate failed\n");
517 return -ENOMEM;
518 }
519
520 priv->scu_nr = nr;
521 priv->scu = scu;
522
523 for_each_rsnd_scu(scu, priv, i) {
ef749400
KM
524 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
525
526 clk = devm_clk_get(dev, name);
527 if (IS_ERR(clk))
528 return PTR_ERR(clk);
529
07539c1d 530 scu->info = &info->scu_info[i];
ef749400 531 scu->clk = clk;
07539c1d 532
013f38fe 533 ops = &rsnd_scu_non_ops;
1b7b08ef
KM
534 if (rsnd_scu_hpbif_is_enable(scu)) {
535 if (rsnd_is_gen1(priv))
536 ops = &rsnd_scu_gen1_ops;
537 } else {
538 if (rsnd_is_gen1(priv))
539 ops = &rsnd_scu_non_gen1_ops;
540 if (rsnd_is_gen2(priv))
541 ops = &rsnd_scu_non_gen2_ops;
542 }
013f38fe
KM
543
544 rsnd_mod_init(priv, &scu->mod, ops, i);
545
374a5281
KM
546 dev_dbg(dev, "SCU%d probed\n", i);
547 }
07539c1d
KM
548 dev_dbg(dev, "scu probed\n");
549
550 return 0;
551}
552
553void rsnd_scu_remove(struct platform_device *pdev,
554 struct rsnd_priv *priv)
555{
556}
This page took 0.258835 seconds and 5 git commands to generate.