blob: 1073d35486e3781858a42a2008568c222e092bcd [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 Morimoto1b7b08ef2014-01-23 18:41:36 -0800168unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800169 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800170 struct snd_pcm_runtime *runtime)
171{
172 struct rsnd_scu *scu;
173 unsigned int rate;
174
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800175 scu = rsnd_mod_to_scu(rsnd_io_to_mod_scu(io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800176
177 /*
178 * return convert rate if SRC is used,
179 * otherwise, return runtime->rate as usual
180 */
181 rate = rsnd_scu_convert_rate(scu);
182 if (!rate)
183 rate = runtime->rate;
184
185 return rate;
186}
187
188static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
189 struct rsnd_dai *rdai,
190 struct rsnd_dai_stream *io)
191{
192 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
193 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
194 u32 convert_rate = rsnd_scu_convert_rate(scu);
195 u32 adinr = runtime->channels;
196 u32 fsrate = 0;
197
198 if (convert_rate)
199 fsrate = 0x0400000 / convert_rate * runtime->rate;
200
201 /* set/clear soft reset */
202 rsnd_mod_write(mod, SRC_SWRSR, 0);
203 rsnd_mod_write(mod, SRC_SWRSR, 1);
204
205 /*
206 * Initialize the operation of the SRC internal circuits
207 * see rsnd_scu_start()
208 */
209 rsnd_mod_write(mod, SRC_SRCIR, 1);
210
211 /* Set channel number and output bit length */
212 switch (runtime->sample_bits) {
213 case 16:
214 adinr |= OTBL_16;
215 break;
216 case 32:
217 adinr |= OTBL_24;
218 break;
219 default:
220 return -EIO;
221 }
222 rsnd_mod_write(mod, SRC_ADINR, adinr);
223
224 /* Enable the initial value of IFS */
225 if (fsrate) {
226 rsnd_mod_write(mod, SRC_IFSCR, 1);
227
228 /* Set initial value of IFS */
229 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
230 }
231
232 /* use DMA transfer */
233 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
234
235 return 0;
236}
237
238static int rsnd_scu_init(struct rsnd_mod *mod,
239 struct rsnd_dai *rdai,
240 struct rsnd_dai_stream *io)
241{
242 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
243 int ret;
244
245 clk_enable(scu->clk);
246
247 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
248 if (ret < 0)
249 return ret;
250
251 return 0;
252}
253
254static int rsnd_scu_quit(struct rsnd_mod *mod,
255 struct rsnd_dai *rdai,
256 struct rsnd_dai_stream *io)
257{
258 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
259
260 clk_disable(scu->clk);
261
262 return 0;
263}
264
265static int rsnd_scu_start(struct rsnd_mod *mod,
266 struct rsnd_dai *rdai,
267 struct rsnd_dai_stream *io)
268{
269 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
270
271 /*
272 * Cancel the initialization and operate the SRC function
273 * see rsnd_scu_set_convert_rate()
274 */
275 rsnd_mod_write(mod, SRC_SRCIR, 0);
276
277 if (rsnd_scu_convert_rate(scu))
278 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
279
280 return 0;
281}
282
283
284static int rsnd_scu_stop(struct rsnd_mod *mod,
285 struct rsnd_dai *rdai,
286 struct rsnd_dai_stream *io)
287{
288 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
289
290 if (rsnd_scu_convert_rate(scu))
291 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
292
293 return 0;
294}
295
296static struct rsnd_mod_ops rsnd_scu_non_ops = {
297 .name = "scu (non)",
298};
299
300/*
301 * Gen1 functions
302 */
303static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
304 struct rsnd_dai *rdai,
305 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700306{
307 struct scu_route_config {
308 u32 mask;
309 int shift;
310 } routes[] = {
311 { 0xF, 0, }, /* 0 */
312 { 0xF, 4, }, /* 1 */
313 { 0xF, 8, }, /* 2 */
314 { 0x7, 12, }, /* 3 */
315 { 0x7, 16, }, /* 4 */
316 { 0x7, 20, }, /* 5 */
317 { 0x7, 24, }, /* 6 */
318 { 0x3, 28, }, /* 7 */
319 { 0x3, 30, }, /* 8 */
320 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700321 u32 mask;
322 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700323 int id;
324
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700325 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300326 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700327 return -EIO;
328
329 /*
330 * SRC_ROUTE_SELECT
331 */
332 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
333 val = val << routes[id].shift;
334 mask = routes[id].mask << routes[id].shift;
335
336 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
337
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800338 return 0;
339}
340
341static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
342 struct rsnd_dai *rdai,
343 struct rsnd_dai_stream *io)
344{
345 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
346 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
347 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
348 u32 convert_rate = rsnd_scu_convert_rate(scu);
349 u32 mask;
350 u32 val;
351 int shift;
352 int id = rsnd_mod_id(mod);
353 int ret;
354
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700355 /*
356 * SRC_TIMING_SELECT
357 */
358 shift = (id % 4) * 8;
359 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800360
361 /*
362 * ADG is used as source clock if SRC was used,
363 * then, SSI WS is used as destination clock.
364 * SSI WS is used as source clock if SRC is not used
365 * (when playback, source/destination become reverse when capture)
366 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800367 ret = 0;
368 if (convert_rate) {
369 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800370 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800371 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
372 runtime->rate,
373 convert_rate);
374 } else if (8 == id) {
375 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700376 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800377 } else {
378 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700379 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800380 }
381
382 if (ret < 0)
383 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700384
385 switch (id / 4) {
386 case 0:
387 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
388 break;
389 case 1:
390 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
391 break;
392 case 2:
393 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
394 break;
395 }
396
397 return 0;
398}
399
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800400static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
401 struct rsnd_dai *rdai,
402 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700403{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800404 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700405
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800406 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
407 if (ret < 0)
408 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700409
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800410 /* Select SRC mode (fixed value) */
411 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800412
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800413 /* Set the restriction value of the FS ratio (98%) */
414 rsnd_mod_write(mod, SRC_MNFSR,
415 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700416
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800417 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800418
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700419 return 0;
420}
421
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800422static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
423 struct rsnd_dai *rdai,
424 struct rsnd_dai_stream *io)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700425{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700426 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700427
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800428 ret = rsnd_scu_init(mod, rdai, io);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800429 if (ret < 0)
430 return ret;
431
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800432 ret = rsnd_src_set_route_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700433 if (ret < 0)
434 return ret;
435
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800436 ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700437 if (ret < 0)
438 return ret;
439
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800440 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
441 if (ret < 0)
442 return ret;
443
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800444 return 0;
445}
446
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800447static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
448 struct rsnd_dai *rdai,
449 struct rsnd_dai_stream *io)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800450{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800451 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800452
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800453 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800454
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800455 return rsnd_scu_start(mod, rdai, io);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800456}
457
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800458static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
459 struct rsnd_dai *rdai,
460 struct rsnd_dai_stream *io)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800461{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800462 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800463
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800464 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800465
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800466 return rsnd_scu_stop(mod, rdai, io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800467}
468
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800469static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
470 .name = "sru (gen1)",
471 .init = rsnd_scu_init_gen1,
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800472 .quit = rsnd_scu_quit,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800473 .start = rsnd_scu_start_gen1,
474 .stop = rsnd_scu_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700475};
476
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800477static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = {
478 .name = "non-sru (gen1)",
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800479 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800480};
481
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800482/*
483 * Gen2 functions
484 */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800485static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod,
486 struct rsnd_dai *rdai,
487 struct rsnd_dai_stream *io)
488{
489 int ret;
490
491 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
492 if (ret < 0)
493 return ret;
494
495 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
496 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
497
498 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
499
500 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
501 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
502
503 return 0;
504}
505
506static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod,
507 struct rsnd_dai *rdai,
508 struct rsnd_dai_stream *io)
509{
510 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
511 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
512 u32 convert_rate = rsnd_scu_convert_rate(scu);
513 int ret;
514
515 if (convert_rate)
516 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
517 runtime->rate,
518 convert_rate);
519 else
520 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
521
522 return ret;
523}
524
525static int rsnd_scu_init_gen2(struct rsnd_mod *mod,
526 struct rsnd_dai *rdai,
527 struct rsnd_dai_stream *io)
528{
529 int ret;
530
531 ret = rsnd_scu_init(mod, rdai, io);
532 if (ret < 0)
533 return ret;
534
535 ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io);
536 if (ret < 0)
537 return ret;
538
539 ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io);
540 if (ret < 0)
541 return ret;
542
543 return 0;
544}
545
546static int rsnd_scu_start_gen2(struct rsnd_mod *mod,
547 struct rsnd_dai *rdai,
548 struct rsnd_dai_stream *io)
549{
550 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
551
552 rsnd_dma_start(rsnd_mod_to_dma(&scu->mod));
553
554 rsnd_mod_write(mod, SSI_CTRL, 0x1);
555 rsnd_mod_write(mod, SRC_CTRL, 0x11);
556
557 return rsnd_scu_start(mod, rdai, io);
558}
559
560static int rsnd_scu_stop_gen2(struct rsnd_mod *mod,
561 struct rsnd_dai *rdai,
562 struct rsnd_dai_stream *io)
563{
564 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
565
566 rsnd_mod_write(mod, SSI_CTRL, 0);
567 rsnd_mod_write(mod, SRC_CTRL, 0);
568
569 rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod));
570
571 return rsnd_scu_stop(mod, rdai, io);
572}
573
574static struct rsnd_mod_ops rsnd_scu_gen2_ops = {
575 .name = "scu (gen2)",
576 .init = rsnd_scu_init_gen2,
577 .quit = rsnd_scu_quit,
578 .start = rsnd_scu_start_gen2,
579 .stop = rsnd_scu_stop_gen2,
580};
581
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800582static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod,
583 struct rsnd_dai *rdai,
584 struct rsnd_dai_stream *io)
585{
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800586 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
587
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800588 /* enable PIO interrupt */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800589 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800590
591 return 0;
592}
593
594static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = {
595 .name = "non-scu (gen2)",
596 .init = rsnd_scu_ssi_mode_init,
597 .start = rsnd_scu_start_non_gen2,
598};
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;
623 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
624 if (!scu) {
625 dev_err(dev, "SCU allocate failed\n");
626 return -ENOMEM;
627 }
628
629 priv->scu_nr = nr;
630 priv->scu = scu;
631
632 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800633 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
634
635 clk = devm_clk_get(dev, name);
636 if (IS_ERR(clk))
637 return PTR_ERR(clk);
638
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700639 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800640 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700641
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800642 ops = &rsnd_scu_non_ops;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800643 if (rsnd_scu_hpbif_is_enable(scu)) {
644 if (rsnd_is_gen1(priv))
645 ops = &rsnd_scu_gen1_ops;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800646 if (rsnd_is_gen2(priv)) {
647 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i);
648 int ret = rsnd_dma_init(priv,
649 rsnd_mod_to_dma(&scu->mod),
650 rsnd_ssi_is_play(ssi),
651 scu->info->dma_id);
652 if (ret < 0)
653 return ret;
654
655 ops = &rsnd_scu_gen2_ops;
656 }
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800657 } else {
658 if (rsnd_is_gen1(priv))
659 ops = &rsnd_scu_non_gen1_ops;
660 if (rsnd_is_gen2(priv))
661 ops = &rsnd_scu_non_gen2_ops;
662 }
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800663
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800664 rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800665
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700666 dev_dbg(dev, "SCU%d probed\n", i);
667 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700668
669 return 0;
670}
671
672void rsnd_scu_remove(struct platform_device *pdev,
673 struct rsnd_priv *priv)
674{
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800675 struct rsnd_scu *scu;
676 int i;
677
678 for_each_rsnd_scu(scu, priv, i) {
679 if (rsnd_scu_dma_available(scu))
680 rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod));
681 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700682}