blob: 40250acf608dd19cf3e237d2579b18327469066c [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 Morimoto221bf522014-03-03 20:50:24 -0800118int rsnd_scu_ssi_mode_init(struct rsnd_mod *ssi_mod,
119 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800121{
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800122 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
123 struct rsnd_mod *scu_mod = rsnd_io_to_mod_scu(io);
124 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800125 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800126 int has_scu = 0;
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800127
128 /*
129 * SSI_MODE0
130 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800131 if (info->dai_info) {
132 has_scu = !!scu_mod;
133 } else {
134 struct rsnd_scu *scu = rsnd_mod_to_scu(scu_mod);
135 has_scu = rsnd_scu_hpbif_is_enable(scu);
136 }
137
138 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
139 has_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)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800159 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800160 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);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800256
257 clk_enable(scu->clk);
258
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800259 return 0;
260}
261
262static int rsnd_scu_quit(struct rsnd_mod *mod,
263 struct rsnd_dai *rdai,
264 struct rsnd_dai_stream *io)
265{
266 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
267
268 clk_disable(scu->clk);
269
270 return 0;
271}
272
273static int rsnd_scu_start(struct rsnd_mod *mod,
274 struct rsnd_dai *rdai,
275 struct rsnd_dai_stream *io)
276{
277 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
278
279 /*
280 * Cancel the initialization and operate the SRC function
281 * see rsnd_scu_set_convert_rate()
282 */
283 rsnd_mod_write(mod, SRC_SRCIR, 0);
284
285 if (rsnd_scu_convert_rate(scu))
286 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
287
288 return 0;
289}
290
291
292static int rsnd_scu_stop(struct rsnd_mod *mod,
293 struct rsnd_dai *rdai,
294 struct rsnd_dai_stream *io)
295{
296 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
297
298 if (rsnd_scu_convert_rate(scu))
299 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
300
301 return 0;
302}
303
304static struct rsnd_mod_ops rsnd_scu_non_ops = {
305 .name = "scu (non)",
306};
307
308/*
309 * Gen1 functions
310 */
311static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
312 struct rsnd_dai *rdai,
313 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700314{
315 struct scu_route_config {
316 u32 mask;
317 int shift;
318 } routes[] = {
319 { 0xF, 0, }, /* 0 */
320 { 0xF, 4, }, /* 1 */
321 { 0xF, 8, }, /* 2 */
322 { 0x7, 12, }, /* 3 */
323 { 0x7, 16, }, /* 4 */
324 { 0x7, 20, }, /* 5 */
325 { 0x7, 24, }, /* 6 */
326 { 0x3, 28, }, /* 7 */
327 { 0x3, 30, }, /* 8 */
328 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700329 u32 mask;
330 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700331 int id;
332
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700333 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300334 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700335 return -EIO;
336
337 /*
338 * SRC_ROUTE_SELECT
339 */
340 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
341 val = val << routes[id].shift;
342 mask = routes[id].mask << routes[id].shift;
343
344 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
345
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800346 return 0;
347}
348
349static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
350 struct rsnd_dai *rdai,
351 struct rsnd_dai_stream *io)
352{
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
354 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
355 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
356 u32 convert_rate = rsnd_scu_convert_rate(scu);
357 u32 mask;
358 u32 val;
359 int shift;
360 int id = rsnd_mod_id(mod);
361 int ret;
362
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700363 /*
364 * SRC_TIMING_SELECT
365 */
366 shift = (id % 4) * 8;
367 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800368
369 /*
370 * ADG is used as source clock if SRC was used,
371 * then, SSI WS is used as destination clock.
372 * SSI WS is used as source clock if SRC is not used
373 * (when playback, source/destination become reverse when capture)
374 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800375 ret = 0;
376 if (convert_rate) {
377 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800378 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800379 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
380 runtime->rate,
381 convert_rate);
382 } else if (8 == id) {
383 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700384 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800385 } else {
386 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700387 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800388 }
389
390 if (ret < 0)
391 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700392
393 switch (id / 4) {
394 case 0:
395 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
396 break;
397 case 1:
398 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
399 break;
400 case 2:
401 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
402 break;
403 }
404
405 return 0;
406}
407
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800408static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
409 struct rsnd_dai *rdai,
410 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700411{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800412 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700413
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800414 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
415 if (ret < 0)
416 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700417
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800418 /* Select SRC mode (fixed value) */
419 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800420
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800421 /* Set the restriction value of the FS ratio (98%) */
422 rsnd_mod_write(mod, SRC_MNFSR,
423 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700424
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800425 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800426
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700427 return 0;
428}
429
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800430static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
431 struct rsnd_dai *rdai,
432 struct rsnd_dai_stream *io)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700433{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700434 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700435
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800436 ret = rsnd_scu_init(mod, rdai, io);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800437 if (ret < 0)
438 return ret;
439
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800440 ret = rsnd_src_set_route_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700441 if (ret < 0)
442 return ret;
443
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800444 ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700445 if (ret < 0)
446 return ret;
447
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800448 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
449 if (ret < 0)
450 return ret;
451
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800452 return 0;
453}
454
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800455static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
456 struct rsnd_dai *rdai,
457 struct rsnd_dai_stream *io)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800458{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800459 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800460
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800461 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800462
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800463 return rsnd_scu_start(mod, rdai, io);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800464}
465
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800466static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
467 struct rsnd_dai *rdai,
468 struct rsnd_dai_stream *io)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800469{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800470 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800471
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800472 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800473
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800474 return rsnd_scu_stop(mod, rdai, io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800475}
476
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800477static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
478 .name = "sru (gen1)",
479 .init = rsnd_scu_init_gen1,
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800480 .quit = rsnd_scu_quit,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800481 .start = rsnd_scu_start_gen1,
482 .stop = rsnd_scu_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700483};
484
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800485/*
486 * Gen2 functions
487 */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800488static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod,
489 struct rsnd_dai *rdai,
490 struct rsnd_dai_stream *io)
491{
492 int ret;
493
494 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
495 if (ret < 0)
496 return ret;
497
498 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
499 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
500
501 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
502
503 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
504 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
505
506 return 0;
507}
508
509static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod,
510 struct rsnd_dai *rdai,
511 struct rsnd_dai_stream *io)
512{
513 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
514 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
515 u32 convert_rate = rsnd_scu_convert_rate(scu);
516 int ret;
517
518 if (convert_rate)
519 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
520 runtime->rate,
521 convert_rate);
522 else
523 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
524
525 return ret;
526}
527
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800528static int rsnd_scu_probe_gen2(struct rsnd_mod *mod,
529 struct rsnd_dai *rdai,
530 struct rsnd_dai_stream *io)
531{
532 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
533 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
534 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
535 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, rsnd_mod_id(mod));
536 struct device *dev = rsnd_priv_to_dev(priv);
537 int ret;
538 int is_play;
539
540 if (info->dai_info)
541 is_play = rsnd_info_is_playback(priv, scu);
542 else
543 is_play = rsnd_ssi_is_play(ssi);
544
545 ret = rsnd_dma_init(priv,
546 rsnd_mod_to_dma(mod),
547 is_play,
548 scu->info->dma_id);
549 if (ret < 0)
550 dev_err(dev, "SCU DMA failed\n");
551
552 return ret;
553}
554
555static int rsnd_scu_remove_gen2(struct rsnd_mod *mod,
556 struct rsnd_dai *rdai,
557 struct rsnd_dai_stream *io)
558{
559 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
560
561 return 0;
562}
563
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800564static int rsnd_scu_init_gen2(struct rsnd_mod *mod,
565 struct rsnd_dai *rdai,
566 struct rsnd_dai_stream *io)
567{
568 int ret;
569
570 ret = rsnd_scu_init(mod, rdai, io);
571 if (ret < 0)
572 return ret;
573
574 ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io);
575 if (ret < 0)
576 return ret;
577
578 ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io);
579 if (ret < 0)
580 return ret;
581
582 return 0;
583}
584
585static int rsnd_scu_start_gen2(struct rsnd_mod *mod,
586 struct rsnd_dai *rdai,
587 struct rsnd_dai_stream *io)
588{
589 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
590
591 rsnd_dma_start(rsnd_mod_to_dma(&scu->mod));
592
593 rsnd_mod_write(mod, SSI_CTRL, 0x1);
594 rsnd_mod_write(mod, SRC_CTRL, 0x11);
595
596 return rsnd_scu_start(mod, rdai, io);
597}
598
599static int rsnd_scu_stop_gen2(struct rsnd_mod *mod,
600 struct rsnd_dai *rdai,
601 struct rsnd_dai_stream *io)
602{
603 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
604
605 rsnd_mod_write(mod, SSI_CTRL, 0);
606 rsnd_mod_write(mod, SRC_CTRL, 0);
607
608 rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod));
609
610 return rsnd_scu_stop(mod, rdai, io);
611}
612
613static struct rsnd_mod_ops rsnd_scu_gen2_ops = {
614 .name = "scu (gen2)",
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800615 .probe = rsnd_scu_probe_gen2,
616 .remove = rsnd_scu_remove_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800617 .init = rsnd_scu_init_gen2,
618 .quit = rsnd_scu_quit,
619 .start = rsnd_scu_start_gen2,
620 .stop = rsnd_scu_stop_gen2,
621};
622
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700623struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
624{
Takashi Iwai8b147192013-11-05 18:40:05 +0100625 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
626 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700627
628 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
629}
630
631int rsnd_scu_probe(struct platform_device *pdev,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700632 struct rsnd_priv *priv)
633{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800634 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700635 struct device *dev = rsnd_priv_to_dev(priv);
636 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800637 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800638 struct clk *clk;
639 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700640 int i, nr;
641
642 /*
643 * init SCU
644 */
645 nr = info->scu_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800646 if (!nr)
647 return 0;
648
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700649 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
650 if (!scu) {
651 dev_err(dev, "SCU allocate failed\n");
652 return -ENOMEM;
653 }
654
655 priv->scu_nr = nr;
656 priv->scu = scu;
657
658 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800659 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
660
661 clk = devm_clk_get(dev, name);
662 if (IS_ERR(clk))
663 return PTR_ERR(clk);
664
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700665 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800666 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700667
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800668 ops = &rsnd_scu_non_ops;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800669 if (rsnd_scu_hpbif_is_enable(scu)) {
670 if (rsnd_is_gen1(priv))
671 ops = &rsnd_scu_gen1_ops;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800672 if (rsnd_is_gen2(priv))
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800673 ops = &rsnd_scu_gen2_ops;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800674 }
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800675
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800676 rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800677
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700678 dev_dbg(dev, "SCU%d probed\n", i);
679 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700680
681 return 0;
682}
683
684void rsnd_scu_remove(struct platform_device *pdev,
685 struct rsnd_priv *priv)
686{
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700687}