blob: 200eda019bc7de6eaac43c86ccc1de19a8a2f6f7 [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 Morimoto8aefda52014-05-22 23:25:43 -070013#define SRC_NAME "src"
14
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080015struct rsnd_src {
16 struct rsnd_src_platform_info *info; /* rcar_snd.h */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070017 struct rsnd_mod mod;
Kuninori Morimotoef749402013-12-19 19:28:51 -080018 struct clk *clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070019};
20
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080021#define RSND_SRC_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070022
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080023#define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
24#define rsnd_mod_to_src(_mod) \
25 container_of((_mod), struct rsnd_src, mod)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080026#define rsnd_src_dma_available(src) \
27 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080028
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080029#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080030 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080031 ((i) < rsnd_src_nr(priv)) && \
32 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080033 i++)
34
35
Kuninori Morimotoef749402013-12-19 19:28:51 -080036/*
37 * image of SRC (Sampling Rate Converter)
38 *
39 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
40 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
41 * 44.1kHz <-> +-----+ +-----+ +-------+
42 * ...
43 *
44 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070045
Kuninori Morimoto41c62212014-01-23 18:39:56 -080046/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080047 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080048 *
49 * Gen1
50 *
51 * [mem] -> [SRU] -> [SSI]
52 * |--------|
53 *
54 * Gen2
55 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080056 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080057 * |-----------------|
58 */
59
60/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080061 * How to use SRC bypass mode for debugging
62 *
63 * SRC has bypass mode, and it is useful for debugging.
64 * In Gen2 case,
65 * SRCm_MODE controls whether SRC is used or not
66 * SSI_MODE0 controls whether SSIU which receives SRC data
67 * is used or not.
68 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
69 * but SRC bypass mode needs SSI_MODE0 only.
70 *
71 * This driver request
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080072 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080073 * u32 convert_rate;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070074 * int dma_id;
Kuninori Morimoto41c62212014-01-23 18:39:56 -080075 * }
76 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080077 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080078 * above convert_rate, and it controls
79 * whether SRC is used or not.
80 *
81 * ex) doesn't use SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070082 * static struct rsnd_dai_platform_info rsnd_dai = {
83 * .playback = { .ssi = &rsnd_ssi[0], },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080084 * };
85 *
86 * ex) uses SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070087 * static struct rsnd_src_platform_info rsnd_src[] = {
88 * RSND_SCU(48000, 0),
89 * ...
90 * };
91 * static struct rsnd_dai_platform_info rsnd_dai = {
92 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080093 * };
94 *
95 * ex) uses SRC bypass mode
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070096 * static struct rsnd_src_platform_info rsnd_src[] = {
97 * RSND_SCU(0, 0),
98 * ...
99 * };
100 * static struct rsnd_dai_platform_info rsnd_dai = {
101 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800102 * };
103 *
104 */
105
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800106/*
107 * Gen1/Gen2 common functions
108 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800109int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700110 struct rsnd_dai *rdai)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800111{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700112 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800113 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800114 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800115
116 /*
117 * SSI_MODE0
118 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800119 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700120 src_mod ? 0 : (1 << ssi_id));
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800121
122 /*
123 * SSI_MODE1
124 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800125 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800126 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800127 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800128 case 1:
129 shift = 0;
130 break;
131 case 2:
132 shift = 2;
133 break;
134 case 4:
135 shift = 16;
136 break;
137 }
138
139 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800140 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800141 0x3 << shift,
142 rsnd_dai_is_clk_master(rdai) ?
143 0x2 << shift : 0x1 << shift);
144 }
145
146 return 0;
147}
148
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800149int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700150 struct rsnd_dai *rdai)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800151{
152 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
153
154 /* enable PIO interrupt if Gen2 */
155 if (rsnd_is_gen2(priv))
156 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
157
158 return 0;
159}
160
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800161unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800162 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800163 struct snd_pcm_runtime *runtime)
164{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700165 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800166 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700167 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800168
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700169 if (src_mod) {
170 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800171
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700172 /*
173 * return convert rate if SRC is used,
174 * otherwise, return runtime->rate as usual
175 */
176 rate = rsnd_src_convert_rate(src);
177 }
178
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800179 if (!rate)
180 rate = runtime->rate;
181
182 return rate;
183}
184
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800185static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700186 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800187{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700188 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800189 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800190 struct rsnd_src *src = rsnd_mod_to_src(mod);
191 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800192 u32 fsrate = 0;
193
194 if (convert_rate)
195 fsrate = 0x0400000 / convert_rate * runtime->rate;
196
197 /* set/clear soft reset */
198 rsnd_mod_write(mod, SRC_SWRSR, 0);
199 rsnd_mod_write(mod, SRC_SWRSR, 1);
200
201 /*
202 * Initialize the operation of the SRC internal circuits
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800203 * see rsnd_src_start()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800204 */
205 rsnd_mod_write(mod, SRC_SRCIR, 1);
206
207 /* Set channel number and output bit length */
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700208 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800209
210 /* Enable the initial value of IFS */
211 if (fsrate) {
212 rsnd_mod_write(mod, SRC_IFSCR, 1);
213
214 /* Set initial value of IFS */
215 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
216 }
217
218 /* use DMA transfer */
219 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
220
221 return 0;
222}
223
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800224static int rsnd_src_init(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700225 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800226{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800227 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800228
Ben Dooks79861bb2014-04-10 18:16:33 -0700229 clk_prepare_enable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800230
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800231 return 0;
232}
233
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800234static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700235 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800236{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800237 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800238
Ben Dooks79861bb2014-04-10 18:16:33 -0700239 clk_disable_unprepare(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800240
241 return 0;
242}
243
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800244static int rsnd_src_start(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700245 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800246{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800247 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800248
249 /*
250 * Cancel the initialization and operate the SRC function
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800251 * see rsnd_src_set_convert_rate()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800252 */
253 rsnd_mod_write(mod, SRC_SRCIR, 0);
254
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800255 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800256 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
257
258 return 0;
259}
260
261
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800262static int rsnd_src_stop(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700263 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800264{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800265 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800266
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800267 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800268 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
269
270 return 0;
271}
272
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800273/*
274 * Gen1 functions
275 */
276static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700277 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700278{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700279 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800280 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700281 u32 mask;
282 int shift;
283 } routes[] = {
284 { 0xF, 0, }, /* 0 */
285 { 0xF, 4, }, /* 1 */
286 { 0xF, 8, }, /* 2 */
287 { 0x7, 12, }, /* 3 */
288 { 0x7, 16, }, /* 4 */
289 { 0x7, 20, }, /* 5 */
290 { 0x7, 24, }, /* 6 */
291 { 0x3, 28, }, /* 7 */
292 { 0x3, 30, }, /* 8 */
293 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700294 u32 mask;
295 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700296 int id;
297
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700298 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300299 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700300 return -EIO;
301
302 /*
303 * SRC_ROUTE_SELECT
304 */
305 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
306 val = val << routes[id].shift;
307 mask = routes[id].mask << routes[id].shift;
308
309 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
310
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800311 return 0;
312}
313
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800314static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700315 struct rsnd_dai *rdai)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800316{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700317 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800318 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800319 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800320 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800321 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800322 u32 mask;
323 u32 val;
324 int shift;
325 int id = rsnd_mod_id(mod);
326 int ret;
327
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700328 /*
329 * SRC_TIMING_SELECT
330 */
331 shift = (id % 4) * 8;
332 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800333
334 /*
335 * ADG is used as source clock if SRC was used,
336 * then, SSI WS is used as destination clock.
337 * SSI WS is used as source clock if SRC is not used
338 * (when playback, source/destination become reverse when capture)
339 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800340 ret = 0;
341 if (convert_rate) {
342 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800343 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800344 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
345 runtime->rate,
346 convert_rate);
347 } else if (8 == id) {
348 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700349 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800350 } else {
351 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700352 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800353 }
354
355 if (ret < 0)
356 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700357
358 switch (id / 4) {
359 case 0:
360 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
361 break;
362 case 1:
363 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
364 break;
365 case 2:
366 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
367 break;
368 }
369
370 return 0;
371}
372
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800373static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700374 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700375{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800376 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700377
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700378 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800379 if (ret < 0)
380 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700381
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800382 /* Select SRC mode (fixed value) */
383 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800384
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800385 /* Set the restriction value of the FS ratio (98%) */
386 rsnd_mod_write(mod, SRC_MNFSR,
387 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700388
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800389 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800390
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700391 return 0;
392}
393
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700394static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
395 struct rsnd_dai *rdai)
396{
397 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
398 struct device *dev = rsnd_priv_to_dev(priv);
399
400 dev_dbg(dev, "%s (Gen1) is probed\n", rsnd_mod_name(mod));
401
402 return 0;
403}
404
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800405static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700406 struct rsnd_dai *rdai)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700407{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700408 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700409
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700410 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800411 if (ret < 0)
412 return ret;
413
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700414 ret = rsnd_src_set_route_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700415 if (ret < 0)
416 return ret;
417
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700418 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700419 if (ret < 0)
420 return ret;
421
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700422 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800423 if (ret < 0)
424 return ret;
425
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800426 return 0;
427}
428
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800429static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700430 struct rsnd_dai *rdai)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800431{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800432 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800433
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800434 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800435
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700436 return rsnd_src_start(mod, rdai);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800437}
438
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800439static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700440 struct rsnd_dai *rdai)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800441{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800442 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800443
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800444 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800445
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700446 return rsnd_src_stop(mod, rdai);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800447}
448
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800449static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700450 .name = SRC_NAME,
451 .probe = rsnd_src_probe_gen1,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800452 .init = rsnd_src_init_gen1,
453 .quit = rsnd_src_quit,
454 .start = rsnd_src_start_gen1,
455 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700456};
457
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800458/*
459 * Gen2 functions
460 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800461static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700462 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800463{
464 int ret;
465
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700466 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800467 if (ret < 0)
468 return ret;
469
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700470 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_get_adinr(mod));
471 rsnd_mod_write(mod, SSI_BUSIF_MODE, 1);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800472
473 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
474
475 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
476 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
477
478 return 0;
479}
480
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800481static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700482 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800483{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700484 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800485 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800486 struct rsnd_src *src = rsnd_mod_to_src(mod);
487 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800488 int ret;
489
490 if (convert_rate)
491 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
492 runtime->rate,
493 convert_rate);
494 else
495 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
496
497 return ret;
498}
499
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800500static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700501 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800502{
503 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800504 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800505 struct device *dev = rsnd_priv_to_dev(priv);
506 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800507
508 ret = rsnd_dma_init(priv,
509 rsnd_mod_to_dma(mod),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700510 rsnd_info_is_playback(priv, src),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800511 src->info->dma_id);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800512 if (ret < 0)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800513 dev_err(dev, "SRC DMA failed\n");
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800514
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700515 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
516
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800517 return ret;
518}
519
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800520static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700521 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800522{
523 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
524
525 return 0;
526}
527
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800528static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700529 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800530{
531 int ret;
532
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700533 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800534 if (ret < 0)
535 return ret;
536
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700537 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800538 if (ret < 0)
539 return ret;
540
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700541 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800542 if (ret < 0)
543 return ret;
544
545 return 0;
546}
547
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800548static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700549 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800550{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700551 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800552 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700553 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800554
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800555 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800556
557 rsnd_mod_write(mod, SSI_CTRL, 0x1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700558 rsnd_mod_write(mod, SRC_CTRL, val);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800559
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700560 return rsnd_src_start(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800561}
562
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800563static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700564 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800565{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800566 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800567
568 rsnd_mod_write(mod, SSI_CTRL, 0);
569 rsnd_mod_write(mod, SRC_CTRL, 0);
570
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800571 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800572
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700573 return rsnd_src_stop(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800574}
575
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800576static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700577 .name = SRC_NAME,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800578 .probe = rsnd_src_probe_gen2,
579 .remove = rsnd_src_remove_gen2,
580 .init = rsnd_src_init_gen2,
581 .quit = rsnd_src_quit,
582 .start = rsnd_src_start_gen2,
583 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800584};
585
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800586struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700587{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800588 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100589 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700590
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800591 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700592}
593
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700594static void rsnd_of_parse_src(struct platform_device *pdev,
595 const struct rsnd_of_data *of_data,
596 struct rsnd_priv *priv)
597{
598 struct device_node *src_node;
599 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
600 struct rsnd_src_platform_info *src_info;
601 struct device *dev = &pdev->dev;
602 int nr;
603
604 if (!of_data)
605 return;
606
607 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
608 if (!src_node)
609 return;
610
611 nr = of_get_child_count(src_node);
612 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700613 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700614
615 src_info = devm_kzalloc(dev,
616 sizeof(struct rsnd_src_platform_info) * nr,
617 GFP_KERNEL);
618 if (!src_info) {
619 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700620 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700621 }
622
623 info->src_info = src_info;
624 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -0700625
626rsnd_of_parse_src_end:
627 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700628}
629
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800630int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700631 const struct rsnd_of_data *of_data,
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
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700642 ops = NULL;
643 if (rsnd_is_gen1(priv))
644 ops = &rsnd_src_gen1_ops;
645 if (rsnd_is_gen2(priv))
646 ops = &rsnd_src_gen2_ops;
647 if (!ops) {
648 dev_err(dev, "unknown Generation\n");
649 return -EIO;
650 }
651
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700652 rsnd_of_parse_src(pdev, of_data, priv);
653
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700654 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800655 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700656 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800657 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800658 if (!nr)
659 return 0;
660
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800661 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
662 if (!src) {
663 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700664 return -ENOMEM;
665 }
666
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800667 priv->src_nr = nr;
668 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700669
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800670 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700671 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
672 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800673
674 clk = devm_clk_get(dev, name);
675 if (IS_ERR(clk))
676 return PTR_ERR(clk);
677
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800678 src->info = &info->src_info[i];
679 src->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700680
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}