ASoC: rsnd: Merge macros in scu.c
[linux-2.6-block.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)
34
35#define for_each_rsnd_scu(pos, priv, i) \
36 for ((i) = 0; \
37 ((i) < rsnd_scu_nr(priv)) && \
38 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
39 i++)
40
41
ef749400
KM
42/*
43 * image of SRC (Sampling Rate Converter)
44 *
45 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
46 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
47 * 44.1kHz <-> +-----+ +-----+ +-------+
48 * ...
49 *
50 */
374a5281 51
c926b746
KM
52/*
53 * scu.c is caring...
54 *
55 * Gen1
56 *
57 * [mem] -> [SRU] -> [SSI]
58 * |--------|
59 *
60 * Gen2
61 *
62 * [mem] -> [SCU] -> [SSIU] -> [SSI]
63 * |-----------------|
64 */
65
41c6221c
KM
66/*
67 * How to use SRC bypass mode for debugging
68 *
69 * SRC has bypass mode, and it is useful for debugging.
70 * In Gen2 case,
71 * SRCm_MODE controls whether SRC is used or not
72 * SSI_MODE0 controls whether SSIU which receives SRC data
73 * is used or not.
74 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
75 * but SRC bypass mode needs SSI_MODE0 only.
76 *
77 * This driver request
78 * struct rsnd_scu_platform_info {
79 * u32 flags;
80 * u32 convert_rate;
81 * }
82 *
83 * rsnd_scu_hpbif_is_enable() will be true
84 * if flags had RSND_SCU_USE_HPBIF,
85 * and it controls whether SSIU is used or not.
86 *
87 * rsnd_scu_convert_rate() indicates
88 * above convert_rate, and it controls
89 * whether SRC is used or not.
90 *
91 * ex) doesn't use SRC
92 * struct rsnd_scu_platform_info info = {
93 * .flags = 0,
94 * .convert_rate = 0,
95 * };
96 *
97 * ex) uses SRC
98 * struct rsnd_scu_platform_info info = {
99 * .flags = RSND_SCU_USE_HPBIF,
100 * .convert_rate = 48000,
101 * };
102 *
103 * ex) uses SRC bypass mode
104 * struct rsnd_scu_platform_info info = {
105 * .flags = RSND_SCU_USE_HPBIF,
106 * .convert_rate = 0,
107 * };
108 *
109 */
110
7b5ce975
KM
111static 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 152static 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
231unsigned 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
252static 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
319bool 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 327static 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
351static 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
362static 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
379static 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
396static 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
404static 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
409struct 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
417int 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
464void rsnd_scu_remove(struct platform_device *pdev,
465 struct rsnd_priv *priv)
466{
467}