blob: ea6a214985d001484996cb826abc1865872703fd [file] [log] [blame]
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08002 * Renesas R-Car SRC support
Kuninori Morimoto07539c12013-07-21 21:36:35 -07003 *
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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080013struct rsnd_src {
14 struct rsnd_src_platform_info *info; /* rcar_snd.h */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070015 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 Morimotoba9c9492014-03-03 20:51:21 -080019#define RSND_SRC_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 Morimotoba9c9492014-03-03 20:51:21 -080030#define rsnd_src_mode_flags(p) ((p)->info->flags)
31#define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
32#define rsnd_mod_to_src(_mod) \
33 container_of((_mod), struct rsnd_src, mod)
34#define rsnd_src_hpbif_is_enable(src) \
35 (rsnd_src_mode_flags(src) & RSND_SCU_USE_HPBIF)
36#define rsnd_src_dma_available(src) \
37 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080038
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080039#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080040 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080041 ((i) < rsnd_src_nr(priv)) && \
42 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080043 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 Morimotoba9c9492014-03-03 20:51:21 -080057 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080058 *
59 * Gen1
60 *
61 * [mem] -> [SRU] -> [SSI]
62 * |--------|
63 *
64 * Gen2
65 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080066 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080067 * |-----------------|
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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080082 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080083 * u32 flags;
84 * u32 convert_rate;
85 * }
86 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080087 * rsnd_src_hpbif_is_enable() will be true
88 * if flags had RSND_SRC_USE_HPBIF,
Kuninori Morimoto41c62212014-01-23 18:39:56 -080089 * and it controls whether SSIU is used or not.
90 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080091 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080092 * above convert_rate, and it controls
93 * whether SRC is used or not.
94 *
95 * ex) doesn't use SRC
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080096 * struct rsnd_src_platform_info info = {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080097 * .flags = 0,
98 * .convert_rate = 0,
99 * };
100 *
101 * ex) uses SRC
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800102 * struct rsnd_src_platform_info info = {
103 * .flags = RSND_SRC_USE_HPBIF,
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800104 * .convert_rate = 48000,
105 * };
106 *
107 * ex) uses SRC bypass mode
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800108 * struct rsnd_src_platform_info info = {
109 * .flags = RSND_SRC_USE_HPBIF,
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800110 * .convert_rate = 0,
111 * };
112 *
113 */
114
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800115/*
116 * Gen1/Gen2 common functions
117 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800118int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod,
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800119 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);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800124 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 Morimotoba9c9492014-03-03 20:51:21 -0800126 int has_src = 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) {
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800132 has_src = !!src_mod;
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800133 } else {
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800134 struct rsnd_src *src = rsnd_mod_to_src(src_mod);
135 has_src = rsnd_src_hpbif_is_enable(src);
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800136 }
137
138 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800139 has_src ? 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 Morimotoba9c9492014-03-03 20:51:21 -0800168int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800169 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 Morimotoba9c9492014-03-03 20:51:21 -0800181unsigned int rsnd_src_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{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800185 struct rsnd_src *src;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800186 unsigned int rate;
187
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800188 src = rsnd_mod_to_src(rsnd_io_to_mod_src(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 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800194 rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800195 if (!rate)
196 rate = runtime->rate;
197
198 return rate;
199}
200
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800201static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
204{
205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800206 struct rsnd_src *src = rsnd_mod_to_src(mod);
207 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800208 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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800220 * see rsnd_src_start()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800221 */
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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800251static int rsnd_src_init(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800252 struct rsnd_dai *rdai,
253 struct rsnd_dai_stream *io)
254{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800255 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800256
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800257 clk_enable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800258
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800259 return 0;
260}
261
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800262static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800263 struct rsnd_dai *rdai,
264 struct rsnd_dai_stream *io)
265{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800266 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800267
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800268 clk_disable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800269
270 return 0;
271}
272
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800273static int rsnd_src_start(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800274 struct rsnd_dai *rdai,
275 struct rsnd_dai_stream *io)
276{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800277 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800278
279 /*
280 * Cancel the initialization and operate the SRC function
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800281 * see rsnd_src_set_convert_rate()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800282 */
283 rsnd_mod_write(mod, SRC_SRCIR, 0);
284
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800285 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800286 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
287
288 return 0;
289}
290
291
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800292static int rsnd_src_stop(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800293 struct rsnd_dai *rdai,
294 struct rsnd_dai_stream *io)
295{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800296 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800297
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800298 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800299 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
300
301 return 0;
302}
303
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800304static struct rsnd_mod_ops rsnd_src_non_ops = {
305 .name = "src (non)",
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800306};
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{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800315 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700316 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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800349static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800350 struct rsnd_dai *rdai,
351 struct rsnd_dai_stream *io)
352{
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800354 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800355 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800356 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800357 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 Morimotoba9c9492014-03-03 20:51:21 -0800408static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800409 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 Morimotoba9c9492014-03-03 20:51:21 -0800414 ret = rsnd_src_set_convert_rate(mod, rdai, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800415 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 Morimotoba9c9492014-03-03 20:51:21 -0800430static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800431 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 Morimotoba9c9492014-03-03 20:51:21 -0800436 ret = rsnd_src_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 Morimotoba9c9492014-03-03 20:51:21 -0800444 ret = rsnd_src_set_convert_rate_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700445 if (ret < 0)
446 return ret;
447
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800448 ret = rsnd_src_set_convert_timing_gen1(mod, rdai, io);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800449 if (ret < 0)
450 return ret;
451
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800452 return 0;
453}
454
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800455static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800456 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 Morimotoba9c9492014-03-03 20:51:21 -0800463 return rsnd_src_start(mod, rdai, io);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800464}
465
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800466static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800467 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 Morimotoba9c9492014-03-03 20:51:21 -0800474 return rsnd_src_stop(mod, rdai, io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800475}
476
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800477static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800478 .name = "sru (gen1)",
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800479 .init = rsnd_src_init_gen1,
480 .quit = rsnd_src_quit,
481 .start = rsnd_src_start_gen1,
482 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700483};
484
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800485/*
486 * Gen2 functions
487 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800488static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800489 struct rsnd_dai *rdai,
490 struct rsnd_dai_stream *io)
491{
492 int ret;
493
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800494 ret = rsnd_src_set_convert_rate(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800495 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
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800509static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800510 struct rsnd_dai *rdai,
511 struct rsnd_dai_stream *io)
512{
513 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800514 struct rsnd_src *src = rsnd_mod_to_src(mod);
515 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800516 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 Morimotoba9c9492014-03-03 20:51:21 -0800528static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800529 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);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800534 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800535 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)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800541 is_play = rsnd_info_is_playback(priv, src);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800542 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,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800548 src->info->dma_id);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800549 if (ret < 0)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800550 dev_err(dev, "SRC DMA failed\n");
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800551
552 return ret;
553}
554
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800555static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800556 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 Morimotoba9c9492014-03-03 20:51:21 -0800564static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800565 struct rsnd_dai *rdai,
566 struct rsnd_dai_stream *io)
567{
568 int ret;
569
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800570 ret = rsnd_src_init(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800571 if (ret < 0)
572 return ret;
573
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800574 ret = rsnd_src_set_convert_rate_gen2(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800575 if (ret < 0)
576 return ret;
577
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800578 ret = rsnd_src_set_convert_timing_gen2(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800579 if (ret < 0)
580 return ret;
581
582 return 0;
583}
584
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800585static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800586 struct rsnd_dai *rdai,
587 struct rsnd_dai_stream *io)
588{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800589 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800590
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800591 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800592
593 rsnd_mod_write(mod, SSI_CTRL, 0x1);
594 rsnd_mod_write(mod, SRC_CTRL, 0x11);
595
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800596 return rsnd_src_start(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800597}
598
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800599static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800600 struct rsnd_dai *rdai,
601 struct rsnd_dai_stream *io)
602{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800603 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800604
605 rsnd_mod_write(mod, SSI_CTRL, 0);
606 rsnd_mod_write(mod, SRC_CTRL, 0);
607
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800608 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800609
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800610 return rsnd_src_stop(mod, rdai, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800611}
612
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800613static struct rsnd_mod_ops rsnd_src_gen2_ops = {
614 .name = "src (gen2)",
615 .probe = rsnd_src_probe_gen2,
616 .remove = rsnd_src_remove_gen2,
617 .init = rsnd_src_init_gen2,
618 .quit = rsnd_src_quit,
619 .start = rsnd_src_start_gen2,
620 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800621};
622
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800623struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700624{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800625 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100626 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700627
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800628 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700629}
630
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800631int rsnd_src_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);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800636 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800637 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800638 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800639 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700640 int i, nr;
641
642 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800643 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700644 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800645 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800646 if (!nr)
647 return 0;
648
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800649 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
650 if (!src) {
651 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700652 return -ENOMEM;
653 }
654
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800655 priv->src_nr = nr;
656 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700657
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800658 for_each_rsnd_src(src, priv, i) {
659 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800660
661 clk = devm_clk_get(dev, name);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800662 if (IS_ERR(clk)) {
663 snprintf(name, RSND_SRC_NAME_SIZE, "scu.%d", i);
664 clk = devm_clk_get(dev, name);
665 }
666
Kuninori Morimotoef749402013-12-19 19:28:51 -0800667 if (IS_ERR(clk))
668 return PTR_ERR(clk);
669
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800670 src->info = &info->src_info[i];
671 src->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700672
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800673 ops = &rsnd_src_non_ops;
674 if (rsnd_src_hpbif_is_enable(src)) {
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800675 if (rsnd_is_gen1(priv))
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800676 ops = &rsnd_src_gen1_ops;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800677 if (rsnd_is_gen2(priv))
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800678 ops = &rsnd_src_gen2_ops;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800679 }
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800680
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800681 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800682
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800683 dev_dbg(dev, "SRC%d probed\n", i);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700684 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700685
686 return 0;
687}