ASoC: rsnd: add rsnd_ssi_is_play()
[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
7b5ce975
KM
113static 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 155static 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);
374a5281
KM
175 u32 mask;
176 u32 val;
374a5281
KM
177 int id;
178
179 /*
180 * Gen1 only
181 */
182 if (!rsnd_is_gen1(priv))
183 return 0;
184
185 id = rsnd_mod_id(mod);
b5f3d7af 186 if (id < 0 || id >= ARRAY_SIZE(routes))
374a5281
KM
187 return -EIO;
188
189 /*
190 * SRC_ROUTE_SELECT
191 */
192 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
193 val = val << routes[id].shift;
194 mask = routes[id].mask << routes[id].shift;
195
196 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
197
28dc4b63
KM
198 return 0;
199}
200
201static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
204{
205 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
206 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
207 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
208 u32 convert_rate = rsnd_scu_convert_rate(scu);
209 u32 mask;
210 u32 val;
211 int shift;
212 int id = rsnd_mod_id(mod);
213 int ret;
214
374a5281
KM
215 /*
216 * SRC_TIMING_SELECT
217 */
218 shift = (id % 4) * 8;
219 mask = 0x1F << shift;
ef749400
KM
220
221 /*
222 * ADG is used as source clock if SRC was used,
223 * then, SSI WS is used as destination clock.
224 * SSI WS is used as source clock if SRC is not used
225 * (when playback, source/destination become reverse when capture)
226 */
28dc4b63
KM
227 ret = 0;
228 if (convert_rate) {
229 /* use ADG */
ef749400 230 val = 0;
28dc4b63
KM
231 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
232 runtime->rate,
233 convert_rate);
234 } else if (8 == id) {
235 /* use SSI WS, but SRU8 is special */
374a5281 236 val = id << shift;
28dc4b63
KM
237 } else {
238 /* use SSI WS */
374a5281 239 val = (id + 1) << shift;
28dc4b63
KM
240 }
241
242 if (ret < 0)
243 return ret;
374a5281
KM
244
245 switch (id / 4) {
246 case 0:
247 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
248 break;
249 case 1:
250 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
251 break;
252 case 2:
253 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
254 break;
255 }
256
257 return 0;
258}
259
ef749400
KM
260unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
261 struct rsnd_mod *ssi_mod,
262 struct snd_pcm_runtime *runtime)
374a5281 263{
ef749400
KM
264 struct rsnd_scu *scu;
265 unsigned int rate;
374a5281 266
ef749400
KM
267 /* this function is assuming SSI id = SCU id here */
268 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
374a5281 269
ef749400
KM
270 /*
271 * return convert rate if SRC is used,
272 * otherwise, return runtime->rate as usual
273 */
274 rate = rsnd_scu_convert_rate(scu);
275 if (!rate)
276 rate = runtime->rate;
277
278 return rate;
374a5281
KM
279}
280
f80e1c96
KM
281static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
282 struct rsnd_dai *rdai,
283 struct rsnd_dai_stream *io)
374a5281 284{
47718dc7 285 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
374a5281 286 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ef749400
KM
287 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
288 u32 convert_rate = rsnd_scu_convert_rate(scu);
374a5281
KM
289 u32 adinr = runtime->channels;
290
ef749400
KM
291 /* set/clear soft reset */
292 rsnd_mod_write(mod, SRC_SWRSR, 0);
293 rsnd_mod_write(mod, SRC_SWRSR, 1);
294
295 /* Initialize the operation of the SRC internal circuits */
296 rsnd_mod_write(mod, SRC_SRCIR, 1);
297
298 /* Set channel number and output bit length */
374a5281
KM
299 switch (runtime->sample_bits) {
300 case 16:
301 adinr |= OTBL_16;
302 break;
303 case 32:
304 adinr |= OTBL_24;
305 break;
306 default:
307 return -EIO;
308 }
690ef81e 309 rsnd_mod_write(mod, SRC_ADINR, adinr);
374a5281 310
ef749400
KM
311 if (convert_rate) {
312 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
ef749400
KM
313
314 /* Enable the initial value of IFS */
315 rsnd_mod_write(mod, SRC_IFSCR, 1);
316
317 /* Set initial value of IFS */
318 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
319
320 /* Select SRC mode (fixed value) */
321 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
322
323 /* Set the restriction value of the FS ratio (98%) */
324 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
325
326 if (rsnd_is_gen1(priv)) {
327 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
328 }
ef749400
KM
329 }
330
331 /* Cancel the initialization and operate the SRC function */
332 rsnd_mod_write(mod, SRC_SRCIR, 0);
333
334 /* use DMA transfer */
0290d2a4 335 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
ef749400 336
374a5281
KM
337 return 0;
338}
339
a204d90c 340static int rsnd_scu_init(struct rsnd_mod *mod,
07539c1d
KM
341 struct rsnd_dai *rdai,
342 struct rsnd_dai_stream *io)
343{
ef749400 344 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
374a5281
KM
345 int ret;
346
ef749400
KM
347 clk_enable(scu->clk);
348
7b5ce975
KM
349 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
350 if (ret < 0)
351 return ret;
352
47718dc7 353 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
374a5281
KM
354 if (ret < 0)
355 return ret;
356
f80e1c96 357 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
374a5281
KM
358 if (ret < 0)
359 return ret;
07539c1d 360
28dc4b63
KM
361 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
362 if (ret < 0)
363 return ret;
364
a204d90c
KM
365 return 0;
366}
367
368static int rsnd_scu_quit(struct rsnd_mod *mod,
369 struct rsnd_dai *rdai,
370 struct rsnd_dai_stream *io)
371{
372 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
373
374 clk_disable(scu->clk);
374a5281 375
07539c1d
KM
376 return 0;
377}
378
a204d90c
KM
379static int rsnd_scu_start(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 385 int id = rsnd_mod_id(mod);
a204d90c 386
e7ce74ea
KM
387 if (rsnd_is_gen1(priv))
388 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
389
390 if (rsnd_scu_convert_rate(scu))
391 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
392
393 return 0;
a204d90c
KM
394}
395
ef749400
KM
396static int rsnd_scu_stop(struct rsnd_mod *mod,
397 struct rsnd_dai *rdai,
398 struct rsnd_dai_stream *io)
399{
400 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
401 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
e7ce74ea
KM
402 int id = rsnd_mod_id(mod);
403
404 if (rsnd_is_gen1(priv))
405 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
ef749400 406
e7ce74ea
KM
407 if (rsnd_scu_convert_rate(scu))
408 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
ef749400 409
ef749400
KM
410 return 0;
411}
412
07539c1d
KM
413static struct rsnd_mod_ops rsnd_scu_ops = {
414 .name = "scu",
a204d90c
KM
415 .init = rsnd_scu_init,
416 .quit = rsnd_scu_quit,
07539c1d 417 .start = rsnd_scu_start,
ef749400 418 .stop = rsnd_scu_stop,
07539c1d
KM
419};
420
013f38fe
KM
421static struct rsnd_mod_ops rsnd_scu_non_ops = {
422 .name = "scu (non)",
7b5ce975 423 .init = rsnd_scu_ssi_mode_init,
013f38fe
KM
424};
425
07539c1d
KM
426struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
427{
8b14719b
TI
428 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
429 id = 0;
07539c1d
KM
430
431 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
432}
433
434int rsnd_scu_probe(struct platform_device *pdev,
435 struct rcar_snd_info *info,
436 struct rsnd_priv *priv)
437{
438 struct device *dev = rsnd_priv_to_dev(priv);
439 struct rsnd_scu *scu;
013f38fe 440 struct rsnd_mod_ops *ops;
ef749400
KM
441 struct clk *clk;
442 char name[RSND_SCU_NAME_SIZE];
07539c1d
KM
443 int i, nr;
444
445 /*
446 * init SCU
447 */
448 nr = info->scu_info_nr;
449 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
450 if (!scu) {
451 dev_err(dev, "SCU allocate failed\n");
452 return -ENOMEM;
453 }
454
455 priv->scu_nr = nr;
456 priv->scu = scu;
457
458 for_each_rsnd_scu(scu, priv, i) {
ef749400
KM
459 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
460
461 clk = devm_clk_get(dev, name);
462 if (IS_ERR(clk))
463 return PTR_ERR(clk);
464
07539c1d 465 scu->info = &info->scu_info[i];
ef749400 466 scu->clk = clk;
07539c1d 467
013f38fe 468 ops = &rsnd_scu_non_ops;
96c7c0d6 469 if (rsnd_scu_hpbif_is_enable(scu))
013f38fe
KM
470 ops = &rsnd_scu_ops;
471
472 rsnd_mod_init(priv, &scu->mod, ops, i);
473
374a5281
KM
474 dev_dbg(dev, "SCU%d probed\n", i);
475 }
07539c1d
KM
476 dev_dbg(dev, "scu probed\n");
477
478 return 0;
479}
480
481void rsnd_scu_remove(struct platform_device *pdev,
482 struct rsnd_priv *priv)
483{
484}
This page took 0.248814 seconds and 5 git commands to generate.