blob: 8ce79e855cf060a5d718dae5a118655bf85d2b1e [file] [log] [blame]
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001/*
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;
Kuninori Morimotoef749402013-12-19 19:28:51 -080016 struct clk *clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070017};
18
Kuninori Morimotoef749402013-12-19 19:28:51 -080019#define RSND_SCU_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070020
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
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080030#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)
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -080034#define rsnd_scu_hpbif_is_enable(scu) \
35 (rsnd_scu_mode_flags(scu) & RSND_SCU_USE_HPBIF)
Kuninori Morimoto629509c2014-01-23 18:42:00 -080036#define rsnd_scu_dma_available(scu) \
37 rsnd_dma_available(rsnd_mod_to_dma(&(scu)->mod))
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080038
39#define for_each_rsnd_scu(pos, priv, i) \
40 for ((i) = 0; \
41 ((i) < rsnd_scu_nr(priv)) && \
42 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
43 i++)
44
45
Kuninori Morimotoef749402013-12-19 19:28:51 -080046/*
47 * image of SRC (Sampling Rate Converter)
48 *
49 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
50 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
51 * 44.1kHz <-> +-----+ +-----+ +-------+
52 * ...
53 *
54 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070055
Kuninori Morimoto41c62212014-01-23 18:39:56 -080056/*
Kuninori Morimotoc926b742014-01-23 18:40:41 -080057 * scu.c is caring...
58 *
59 * Gen1
60 *
61 * [mem] -> [SRU] -> [SSI]
62 * |--------|
63 *
64 * Gen2
65 *
66 * [mem] -> [SCU] -> [SSIU] -> [SSI]
67 * |-----------------|
68 */
69
70/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080071 * How to use SRC bypass mode for debugging
72 *
73 * SRC has bypass mode, and it is useful for debugging.
74 * In Gen2 case,
75 * SRCm_MODE controls whether SRC is used or not
76 * SSI_MODE0 controls whether SSIU which receives SRC data
77 * is used or not.
78 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
79 * but SRC bypass mode needs SSI_MODE0 only.
80 *
81 * This driver request
82 * struct rsnd_scu_platform_info {
83 * u32 flags;
84 * u32 convert_rate;
85 * }
86 *
87 * rsnd_scu_hpbif_is_enable() will be true
88 * if flags had RSND_SCU_USE_HPBIF,
89 * and it controls whether SSIU is used or not.
90 *
91 * rsnd_scu_convert_rate() indicates
92 * above convert_rate, and it controls
93 * whether SRC is used or not.
94 *
95 * ex) doesn't use SRC
96 * struct rsnd_scu_platform_info info = {
97 * .flags = 0,
98 * .convert_rate = 0,
99 * };
100 *
101 * ex) uses SRC
102 * struct rsnd_scu_platform_info info = {
103 * .flags = RSND_SCU_USE_HPBIF,
104 * .convert_rate = 48000,
105 * };
106 *
107 * ex) uses SRC bypass mode
108 * struct rsnd_scu_platform_info info = {
109 * .flags = RSND_SCU_USE_HPBIF,
110 * .convert_rate = 0,
111 * };
112 *
113 */
114
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800115/*
116 * Gen1/Gen2 common functions
117 */
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800118static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
119 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io)
121{
122 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -0800123 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800124 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
125 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimotoa2070fe2014-02-11 21:05:26 -0800126 u32 convert_rate = rsnd_scu_convert_rate(scu);
127
128 if (convert_rate && !rsnd_dai_is_clk_master(rdai)) {
129 struct device *dev = rsnd_priv_to_dev(priv);
130
131 dev_err(dev, "rsnd should be clk master when you rate convert\n");
132 return -EINVAL;
133 }
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800134
135 /*
136 * SSI_MODE0
137 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800138 rsnd_mod_bset(mod, SSI_MODE0, (1 << ssi_id),
139 rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << ssi_id));
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800140
141 /*
142 * SSI_MODE1
143 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800144 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800145 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800146 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800147 case 1:
148 shift = 0;
149 break;
150 case 2:
151 shift = 2;
152 break;
153 case 4:
154 shift = 16;
155 break;
156 }
157
158 if (shift >= 0)
159 rsnd_mod_bset(mod, SSI_MODE1,
160 0x3 << shift,
161 rsnd_dai_is_clk_master(rdai) ?
162 0x2 << shift : 0x1 << shift);
163 }
164
165 return 0;
166}
167
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800168int rsnd_scu_enable_ssi_irq(struct rsnd_mod *ssi_mod,
169 struct rsnd_dai *rdai,
170 struct rsnd_dai_stream *io)
171{
172 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
173
174 /* enable PIO interrupt if Gen2 */
175 if (rsnd_is_gen2(priv))
176 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
177
178 return 0;
179}
180
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800181unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800182 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800183 struct snd_pcm_runtime *runtime)
184{
185 struct rsnd_scu *scu;
186 unsigned int rate;
187
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800188 scu = rsnd_mod_to_scu(rsnd_io_to_mod_scu(io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800189
190 /*
191 * return convert rate if SRC is used,
192 * otherwise, return runtime->rate as usual
193 */
194 rate = rsnd_scu_convert_rate(scu);
195 if (!rate)
196 rate = runtime->rate;
197
198 return rate;
199}
200
201static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
204{
205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
206 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
207 u32 convert_rate = rsnd_scu_convert_rate(scu);
208 u32 adinr = runtime->channels;
209 u32 fsrate = 0;
210
211 if (convert_rate)
212 fsrate = 0x0400000 / convert_rate * runtime->rate;
213
214 /* set/clear soft reset */
215 rsnd_mod_write(mod, SRC_SWRSR, 0);
216 rsnd_mod_write(mod, SRC_SWRSR, 1);
217
218 /*
219 * Initialize the operation of the SRC internal circuits
220 * see rsnd_scu_start()
221 */
222 rsnd_mod_write(mod, SRC_SRCIR, 1);
223
224 /* Set channel number and output bit length */
225 switch (runtime->sample_bits) {
226 case 16:
227 adinr |= OTBL_16;
228 break;
229 case 32:
230 adinr |= OTBL_24;
231 break;
232 default:
233 return -EIO;
234 }
235 rsnd_mod_write(mod, SRC_ADINR, adinr);
236
237 /* Enable the initial value of IFS */
238 if (fsrate) {
239 rsnd_mod_write(mod, SRC_IFSCR, 1);
240
241 /* Set initial value of IFS */
242 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
243 }
244
245 /* use DMA transfer */
246 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
247
248 return 0;
249}
250
251static int rsnd_scu_init(struct rsnd_mod *mod,
252 struct rsnd_dai *rdai,
253 struct rsnd_dai_stream *io)
254{
255 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
256 int ret;
257
258 clk_enable(scu->clk);
259
260 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
261 if (ret < 0)
262 return ret;
263
264 return 0;
265}
266
267static int rsnd_scu_quit(struct rsnd_mod *mod,
268 struct rsnd_dai *rdai,
269 struct rsnd_dai_stream *io)
270{
271 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
272
273 clk_disable(scu->clk);
274
275 return 0;
276}
277
278static int rsnd_scu_start(struct rsnd_mod *mod,
279 struct rsnd_dai *rdai,
280 struct rsnd_dai_stream *io)
281{
282 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
283
284 /*
285 * Cancel the initialization and operate the SRC function
286 * see rsnd_scu_set_convert_rate()
287 */
288 rsnd_mod_write(mod, SRC_SRCIR, 0);
289
290 if (rsnd_scu_convert_rate(scu))
291 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
292
293 return 0;
294}
295
296
297static int rsnd_scu_stop(struct rsnd_mod *mod,
298 struct rsnd_dai *rdai,
299 struct rsnd_dai_stream *io)
300{
301 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
302
303 if (rsnd_scu_convert_rate(scu))
304 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
305
306 return 0;
307}
308
309static struct rsnd_mod_ops rsnd_scu_non_ops = {
310 .name = "scu (non)",
311};
312
313/*
314 * Gen1 functions
315 */
316static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
317 struct rsnd_dai *rdai,
318 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700319{
320 struct scu_route_config {
321 u32 mask;
322 int shift;
323 } routes[] = {
324 { 0xF, 0, }, /* 0 */
325 { 0xF, 4, }, /* 1 */
326 { 0xF, 8, }, /* 2 */
327 { 0x7, 12, }, /* 3 */
328 { 0x7, 16, }, /* 4 */
329 { 0x7, 20, }, /* 5 */
330 { 0x7, 24, }, /* 6 */
331 { 0x3, 28, }, /* 7 */
332 { 0x3, 30, }, /* 8 */
333 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700334 u32 mask;
335 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700336 int id;
337
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700338 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300339 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700340 return -EIO;
341
342 /*
343 * SRC_ROUTE_SELECT
344 */
345 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
346 val = val << routes[id].shift;
347 mask = routes[id].mask << routes[id].shift;
348
349 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
350
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800351 return 0;
352}
353
354static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
355 struct rsnd_dai *rdai,
356 struct rsnd_dai_stream *io)
357{
358 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
359 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
360 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
361 u32 convert_rate = rsnd_scu_convert_rate(scu);
362 u32 mask;
363 u32 val;
364 int shift;
365 int id = rsnd_mod_id(mod);
366 int ret;
367
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700368 /*
369 * SRC_TIMING_SELECT
370 */
371 shift = (id % 4) * 8;
372 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800373
374 /*
375 * ADG is used as source clock if SRC was used,
376 * then, SSI WS is used as destination clock.
377 * SSI WS is used as source clock if SRC is not used
378 * (when playback, source/destination become reverse when capture)
379 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800380 ret = 0;
381 if (convert_rate) {
382 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800383 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800384 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
385 runtime->rate,
386 convert_rate);
387 } else if (8 == id) {
388 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700389 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800390 } else {
391 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700392 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800393 }
394
395 if (ret < 0)
396 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700397
398 switch (id / 4) {
399 case 0:
400 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
401 break;
402 case 1:
403 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
404 break;
405 case 2:
406 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
407 break;
408 }
409
410 return 0;
411}
412
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800413static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
414 struct rsnd_dai *rdai,
415 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700416{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800417 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700418
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800419 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
420 if (ret < 0)
421 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700422
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800423 /* Select SRC mode (fixed value) */
424 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800425
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800426 /* Set the restriction value of the FS ratio (98%) */
427 rsnd_mod_write(mod, SRC_MNFSR,
428 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700429
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800430 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800431
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700432 return 0;
433}
434
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800435static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
436 struct rsnd_dai *rdai,
437 struct rsnd_dai_stream *io)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700438{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700439 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700440
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800441 ret = rsnd_scu_init(mod, rdai, io);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800442 if (ret < 0)
443 return ret;
444
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800445 ret = rsnd_src_set_route_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700446 if (ret < 0)
447 return ret;
448
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800449 ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700450 if (ret < 0)
451 return ret;
452
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800453 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
454 if (ret < 0)
455 return ret;
456
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800457 return 0;
458}
459
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800460static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
461 struct rsnd_dai *rdai,
462 struct rsnd_dai_stream *io)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800463{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800464 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800465
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800466 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800467
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800468 return rsnd_scu_start(mod, rdai, io);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800469}
470
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800471static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
472 struct rsnd_dai *rdai,
473 struct rsnd_dai_stream *io)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800474{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800475 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800476
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800477 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800478
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800479 return rsnd_scu_stop(mod, rdai, io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800480}
481
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800482static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
483 .name = "sru (gen1)",
484 .init = rsnd_scu_init_gen1,
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800485 .quit = rsnd_scu_quit,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800486 .start = rsnd_scu_start_gen1,
487 .stop = rsnd_scu_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700488};
489
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800490static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = {
491 .name = "non-sru (gen1)",
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800492 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800493};
494
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800495/*
496 * Gen2 functions
497 */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800498static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod,
499 struct rsnd_dai *rdai,
500 struct rsnd_dai_stream *io)
501{
502 int ret;
503
504 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
505 if (ret < 0)
506 return ret;
507
508 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
509 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
510
511 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
512
513 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
514 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
515
516 return 0;
517}
518
519static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod,
520 struct rsnd_dai *rdai,
521 struct rsnd_dai_stream *io)
522{
523 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
524 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
525 u32 convert_rate = rsnd_scu_convert_rate(scu);
526 int ret;
527
528 if (convert_rate)
529 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
530 runtime->rate,
531 convert_rate);
532 else
533 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
534
535 return ret;
536}
537
538static int rsnd_scu_init_gen2(struct rsnd_mod *mod,
539 struct rsnd_dai *rdai,
540 struct rsnd_dai_stream *io)
541{
542 int ret;
543
544 ret = rsnd_scu_init(mod, rdai, io);
545 if (ret < 0)
546 return ret;
547
548 ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io);
549 if (ret < 0)
550 return ret;
551
552 ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io);
553 if (ret < 0)
554 return ret;
555
556 return 0;
557}
558
559static int rsnd_scu_start_gen2(struct rsnd_mod *mod,
560 struct rsnd_dai *rdai,
561 struct rsnd_dai_stream *io)
562{
563 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
564
565 rsnd_dma_start(rsnd_mod_to_dma(&scu->mod));
566
567 rsnd_mod_write(mod, SSI_CTRL, 0x1);
568 rsnd_mod_write(mod, SRC_CTRL, 0x11);
569
570 return rsnd_scu_start(mod, rdai, io);
571}
572
573static int rsnd_scu_stop_gen2(struct rsnd_mod *mod,
574 struct rsnd_dai *rdai,
575 struct rsnd_dai_stream *io)
576{
577 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
578
579 rsnd_mod_write(mod, SSI_CTRL, 0);
580 rsnd_mod_write(mod, SRC_CTRL, 0);
581
582 rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod));
583
584 return rsnd_scu_stop(mod, rdai, io);
585}
586
587static struct rsnd_mod_ops rsnd_scu_gen2_ops = {
588 .name = "scu (gen2)",
589 .init = rsnd_scu_init_gen2,
590 .quit = rsnd_scu_quit,
591 .start = rsnd_scu_start_gen2,
592 .stop = rsnd_scu_stop_gen2,
593};
594
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800595static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = {
596 .name = "non-scu (gen2)",
597 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800598};
599
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700600struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
601{
Takashi Iwai8b147192013-11-05 18:40:05 +0100602 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
603 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700604
605 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
606}
607
608int rsnd_scu_probe(struct platform_device *pdev,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700609 struct rsnd_priv *priv)
610{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800611 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700612 struct device *dev = rsnd_priv_to_dev(priv);
613 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800614 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800615 struct clk *clk;
616 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700617 int i, nr;
618
619 /*
620 * init SCU
621 */
622 nr = info->scu_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800623 if (!nr)
624 return 0;
625
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700626 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
627 if (!scu) {
628 dev_err(dev, "SCU allocate failed\n");
629 return -ENOMEM;
630 }
631
632 priv->scu_nr = nr;
633 priv->scu = scu;
634
635 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800636 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
637
638 clk = devm_clk_get(dev, name);
639 if (IS_ERR(clk))
640 return PTR_ERR(clk);
641
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700642 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800643 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700644
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800645 ops = &rsnd_scu_non_ops;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800646 if (rsnd_scu_hpbif_is_enable(scu)) {
647 if (rsnd_is_gen1(priv))
648 ops = &rsnd_scu_gen1_ops;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800649 if (rsnd_is_gen2(priv)) {
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800650 int ret;
651 int is_play;
652
653 if (info->dai_info) {
654 is_play = rsnd_info_is_playback(priv, scu);
655 } else {
656 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i);
657 is_play = rsnd_ssi_is_play(ssi);
658 }
659 ret = rsnd_dma_init(priv,
660 rsnd_mod_to_dma(&scu->mod),
661 is_play,
662 scu->info->dma_id);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800663 if (ret < 0)
664 return ret;
665
666 ops = &rsnd_scu_gen2_ops;
667 }
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800668 } else {
669 if (rsnd_is_gen1(priv))
670 ops = &rsnd_scu_non_gen1_ops;
671 if (rsnd_is_gen2(priv))
672 ops = &rsnd_scu_non_gen2_ops;
673 }
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800674
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800675 rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800676
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700677 dev_dbg(dev, "SCU%d probed\n", i);
678 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700679
680 return 0;
681}
682
683void rsnd_scu_remove(struct platform_device *pdev,
684 struct rsnd_priv *priv)
685{
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800686 struct rsnd_scu *scu;
687 int i;
688
689 for_each_rsnd_scu(scu, priv, i) {
690 if (rsnd_scu_dma_available(scu))
691 rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod));
692 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700693}