blob: 9183e0145503f65368b4a018eeb93f9a19eb8de6 [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 Morimotod9288d02014-06-22 17:56:23 -0700109int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
110 struct rsnd_dai *rdai,
111 int use_busif)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800112{
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700113 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
114 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800115 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800116
117 /*
118 * SSI_MODE0
119 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800120 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700121 !use_busif << ssi_id);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800122
123 /*
124 * SSI_MODE1
125 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800126 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800127 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800128 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800129 case 1:
130 shift = 0;
131 break;
132 case 2:
133 shift = 2;
134 break;
135 case 4:
136 shift = 16;
137 break;
138 }
139
140 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800141 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800142 0x3 << shift,
143 rsnd_dai_is_clk_master(rdai) ?
144 0x2 << shift : 0x1 << shift);
145 }
146
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700147 /*
148 * DMA settings for SSIU
149 */
150 if (use_busif) {
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700151 u32 val = 0x76543210;
152 u32 mask = ~0;
153
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700154 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
155 rsnd_get_adinr(ssi_mod));
156 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
157 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700158
159 mask <<= runtime->channels * 4;
160 val = val & mask;
161
162 switch (runtime->sample_bits) {
163 case 16:
164 val |= 0x67452301 & ~mask;
165 break;
166 case 32:
167 val |= 0x76543210 & ~mask;
168 break;
169 }
170 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
171
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700172 }
173
174 return 0;
175}
176
177int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
178 struct rsnd_dai *rdai,
179 int use_busif)
180{
181 /*
182 * DMA settings for SSIU
183 */
184 if (use_busif)
185 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
186
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800187 return 0;
188}
189
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800190int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700191 struct rsnd_dai *rdai)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800192{
193 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
194
195 /* enable PIO interrupt if Gen2 */
196 if (rsnd_is_gen2(priv))
197 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
198
199 return 0;
200}
201
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800202unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800203 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800204 struct snd_pcm_runtime *runtime)
205{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700206 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800207 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700208 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800209
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700210 if (src_mod) {
211 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800212
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700213 /*
214 * return convert rate if SRC is used,
215 * otherwise, return runtime->rate as usual
216 */
217 rate = rsnd_src_convert_rate(src);
218 }
219
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800220 if (!rate)
221 rate = runtime->rate;
222
223 return rate;
224}
225
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800226static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700227 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800228{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700229 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800230 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800231 struct rsnd_src *src = rsnd_mod_to_src(mod);
232 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800233 u32 fsrate = 0;
234
235 if (convert_rate)
236 fsrate = 0x0400000 / convert_rate * runtime->rate;
237
238 /* set/clear soft reset */
239 rsnd_mod_write(mod, SRC_SWRSR, 0);
240 rsnd_mod_write(mod, SRC_SWRSR, 1);
241
242 /*
243 * Initialize the operation of the SRC internal circuits
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800244 * see rsnd_src_start()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800245 */
246 rsnd_mod_write(mod, SRC_SRCIR, 1);
247
248 /* Set channel number and output bit length */
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700249 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800250
251 /* Enable the initial value of IFS */
252 if (fsrate) {
253 rsnd_mod_write(mod, SRC_IFSCR, 1);
254
255 /* Set initial value of IFS */
256 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
257 }
258
259 /* use DMA transfer */
260 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
261
262 return 0;
263}
264
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800265static int rsnd_src_init(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700266 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800267{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800268 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800269
Ben Dooks79861bb2014-04-10 18:16:33 -0700270 clk_prepare_enable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800271
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800272 return 0;
273}
274
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800275static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700276 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800277{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800278 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800279
Ben Dooks79861bb2014-04-10 18:16:33 -0700280 clk_disable_unprepare(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800281
282 return 0;
283}
284
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800285static int rsnd_src_start(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700286 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800287{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800288 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800289
290 /*
291 * Cancel the initialization and operate the SRC function
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800292 * see rsnd_src_set_convert_rate()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800293 */
294 rsnd_mod_write(mod, SRC_SRCIR, 0);
295
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800296 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800297 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
298
299 return 0;
300}
301
302
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800303static int rsnd_src_stop(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700304 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800305{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800306 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800307
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800308 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800309 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
310
311 return 0;
312}
313
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800314/*
315 * Gen1 functions
316 */
317static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700318 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700319{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700320 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800321 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700322 u32 mask;
323 int shift;
324 } routes[] = {
325 { 0xF, 0, }, /* 0 */
326 { 0xF, 4, }, /* 1 */
327 { 0xF, 8, }, /* 2 */
328 { 0x7, 12, }, /* 3 */
329 { 0x7, 16, }, /* 4 */
330 { 0x7, 20, }, /* 5 */
331 { 0x7, 24, }, /* 6 */
332 { 0x3, 28, }, /* 7 */
333 { 0x3, 30, }, /* 8 */
334 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700335 u32 mask;
336 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700337 int id;
338
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700339 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300340 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700341 return -EIO;
342
343 /*
344 * SRC_ROUTE_SELECT
345 */
346 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
347 val = val << routes[id].shift;
348 mask = routes[id].mask << routes[id].shift;
349
350 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
351
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800352 return 0;
353}
354
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800355static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700356 struct rsnd_dai *rdai)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800357{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700358 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800359 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800360 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800361 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800362 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800363 u32 mask;
364 u32 val;
365 int shift;
366 int id = rsnd_mod_id(mod);
367 int ret;
368
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700369 /*
370 * SRC_TIMING_SELECT
371 */
372 shift = (id % 4) * 8;
373 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800374
375 /*
376 * ADG is used as source clock if SRC was used,
377 * then, SSI WS is used as destination clock.
378 * SSI WS is used as source clock if SRC is not used
379 * (when playback, source/destination become reverse when capture)
380 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800381 ret = 0;
382 if (convert_rate) {
383 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800384 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800385 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
386 runtime->rate,
387 convert_rate);
388 } else if (8 == id) {
389 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700390 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800391 } else {
392 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700393 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800394 }
395
396 if (ret < 0)
397 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700398
399 switch (id / 4) {
400 case 0:
401 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
402 break;
403 case 1:
404 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
405 break;
406 case 2:
407 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
408 break;
409 }
410
411 return 0;
412}
413
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800414static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700415 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700416{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800417 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700418
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700419 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800420 if (ret < 0)
421 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700422
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800423 /* Select SRC mode (fixed value) */
424 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800425
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800426 /* Set the restriction value of the FS ratio (98%) */
427 rsnd_mod_write(mod, SRC_MNFSR,
428 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700429
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800430 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800431
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700432 return 0;
433}
434
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700435static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
436 struct rsnd_dai *rdai)
437{
438 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
439 struct device *dev = rsnd_priv_to_dev(priv);
440
441 dev_dbg(dev, "%s (Gen1) is probed\n", rsnd_mod_name(mod));
442
443 return 0;
444}
445
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800446static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700447 struct rsnd_dai *rdai)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700448{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700449 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700450
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700451 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800452 if (ret < 0)
453 return ret;
454
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700455 ret = rsnd_src_set_route_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700456 if (ret < 0)
457 return ret;
458
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700459 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700460 if (ret < 0)
461 return ret;
462
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700463 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800464 if (ret < 0)
465 return ret;
466
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800467 return 0;
468}
469
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800470static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700471 struct rsnd_dai *rdai)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800472{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800473 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800474
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800475 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800476
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700477 return rsnd_src_start(mod, rdai);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800478}
479
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800480static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700481 struct rsnd_dai *rdai)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800482{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800483 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800484
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800485 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800486
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700487 return rsnd_src_stop(mod, rdai);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800488}
489
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800490static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700491 .name = SRC_NAME,
492 .probe = rsnd_src_probe_gen1,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800493 .init = rsnd_src_init_gen1,
494 .quit = rsnd_src_quit,
495 .start = rsnd_src_start_gen1,
496 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700497};
498
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800499/*
500 * Gen2 functions
501 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800502static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700503 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800504{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700505 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
506 struct device *dev = rsnd_priv_to_dev(priv);
507 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
508 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
509 struct rsnd_src *src = rsnd_mod_to_src(mod);
510 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800511 int ret;
512
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700513 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
514 if (!rsnd_src_convert_rate(src))
515 ratio = 0;
516 else if (rsnd_src_convert_rate(src) > runtime->rate)
517 ratio = 100 * rsnd_src_convert_rate(src) / runtime->rate;
518 else
519 ratio = 100 * runtime->rate / rsnd_src_convert_rate(src);
520
521 if (ratio > 600) {
522 dev_err(dev, "FSO/FSI ratio error\n");
523 return -EINVAL;
524 }
525
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700526 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800527 if (ret < 0)
528 return ret;
529
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800530 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
531
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700532 switch (rsnd_mod_id(mod)) {
533 case 5:
534 case 6:
535 case 7:
536 case 8:
537 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
538 break;
539 default:
540 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
541 break;
542 }
543
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800544 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
545
546 return 0;
547}
548
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800549static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700550 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800551{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700552 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800553 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800554 struct rsnd_src *src = rsnd_mod_to_src(mod);
555 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800556 int ret;
557
558 if (convert_rate)
559 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
560 runtime->rate,
561 convert_rate);
562 else
563 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
564
565 return ret;
566}
567
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800568static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700569 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800570{
571 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800572 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800573 struct device *dev = rsnd_priv_to_dev(priv);
574 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800575
576 ret = rsnd_dma_init(priv,
577 rsnd_mod_to_dma(mod),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700578 rsnd_info_is_playback(priv, src),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800579 src->info->dma_id);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800580 if (ret < 0)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800581 dev_err(dev, "SRC DMA failed\n");
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800582
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700583 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
584
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800585 return ret;
586}
587
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800588static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700589 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800590{
591 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
592
593 return 0;
594}
595
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800596static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700597 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800598{
599 int ret;
600
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700601 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800602 if (ret < 0)
603 return ret;
604
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700605 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800606 if (ret < 0)
607 return ret;
608
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700609 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800610 if (ret < 0)
611 return ret;
612
613 return 0;
614}
615
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800616static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700617 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800618{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700619 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800620 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700621 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800622
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800623 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800624
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700625 rsnd_mod_write(mod, SRC_CTRL, val);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800626
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700627 return rsnd_src_start(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800628}
629
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800630static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700631 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800632{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800633 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800634
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800635 rsnd_mod_write(mod, SRC_CTRL, 0);
636
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800637 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800638
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700639 return rsnd_src_stop(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800640}
641
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800642static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700643 .name = SRC_NAME,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800644 .probe = rsnd_src_probe_gen2,
645 .remove = rsnd_src_remove_gen2,
646 .init = rsnd_src_init_gen2,
647 .quit = rsnd_src_quit,
648 .start = rsnd_src_start_gen2,
649 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800650};
651
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800652struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700653{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800654 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100655 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700656
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800657 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700658}
659
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700660static void rsnd_of_parse_src(struct platform_device *pdev,
661 const struct rsnd_of_data *of_data,
662 struct rsnd_priv *priv)
663{
664 struct device_node *src_node;
665 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
666 struct rsnd_src_platform_info *src_info;
667 struct device *dev = &pdev->dev;
668 int nr;
669
670 if (!of_data)
671 return;
672
673 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
674 if (!src_node)
675 return;
676
677 nr = of_get_child_count(src_node);
678 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700679 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700680
681 src_info = devm_kzalloc(dev,
682 sizeof(struct rsnd_src_platform_info) * nr,
683 GFP_KERNEL);
684 if (!src_info) {
685 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700686 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700687 }
688
689 info->src_info = src_info;
690 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -0700691
692rsnd_of_parse_src_end:
693 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700694}
695
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800696int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700697 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700698 struct rsnd_priv *priv)
699{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800700 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700701 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800702 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800703 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800704 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800705 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700706 int i, nr;
707
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700708 ops = NULL;
709 if (rsnd_is_gen1(priv))
710 ops = &rsnd_src_gen1_ops;
711 if (rsnd_is_gen2(priv))
712 ops = &rsnd_src_gen2_ops;
713 if (!ops) {
714 dev_err(dev, "unknown Generation\n");
715 return -EIO;
716 }
717
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700718 rsnd_of_parse_src(pdev, of_data, priv);
719
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700720 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800721 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700722 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800723 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800724 if (!nr)
725 return 0;
726
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800727 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
728 if (!src) {
729 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700730 return -ENOMEM;
731 }
732
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800733 priv->src_nr = nr;
734 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700735
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800736 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700737 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
738 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800739
740 clk = devm_clk_get(dev, name);
741 if (IS_ERR(clk))
742 return PTR_ERR(clk);
743
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800744 src->info = &info->src_info[i];
745 src->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700746
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800747 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800748
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800749 dev_dbg(dev, "SRC%d probed\n", i);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700750 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700751
752 return 0;
753}