blob: 6a63f8fcf9919f8b9fe2329bcdc08f4c8d8156a2 [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,
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000178 struct rsnd_dai *rdai)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700179{
180 /*
181 * DMA settings for SSIU
182 */
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000183 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700184
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800185 return 0;
186}
187
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000188int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700189 struct rsnd_dai *rdai)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800190{
191 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
192
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000193 if (rsnd_is_gen1(priv))
194 return 0;
195
196 /* enable SSI interrupt if Gen2 */
197 if (rsnd_ssi_is_dma_mode(ssi_mod))
198 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
199 else
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800200 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
201
202 return 0;
203}
204
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000205int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod,
206 struct rsnd_dai *rdai)
207{
208 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
209
210 if (rsnd_is_gen1(priv))
211 return 0;
212
213 /* disable SSI interrupt if Gen2 */
214 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
215
216 return 0;
217}
218
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800219unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800220 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800221 struct snd_pcm_runtime *runtime)
222{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700223 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800224 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700225 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800226
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700227 if (src_mod) {
228 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800229
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700230 /*
231 * return convert rate if SRC is used,
232 * otherwise, return runtime->rate as usual
233 */
234 rate = rsnd_src_convert_rate(src);
235 }
236
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800237 if (!rate)
238 rate = runtime->rate;
239
240 return rate;
241}
242
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800243static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700244 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800245{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700246 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800247 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800248 struct rsnd_src *src = rsnd_mod_to_src(mod);
249 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800250 u32 fsrate = 0;
251
252 if (convert_rate)
253 fsrate = 0x0400000 / convert_rate * runtime->rate;
254
255 /* set/clear soft reset */
256 rsnd_mod_write(mod, SRC_SWRSR, 0);
257 rsnd_mod_write(mod, SRC_SWRSR, 1);
258
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800259 /* Set channel number and output bit length */
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700260 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800261
262 /* Enable the initial value of IFS */
263 if (fsrate) {
264 rsnd_mod_write(mod, SRC_IFSCR, 1);
265
266 /* Set initial value of IFS */
267 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
268 }
269
270 /* use DMA transfer */
271 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
272
273 return 0;
274}
275
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800276static int rsnd_src_init(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700277 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800278{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800279 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800280
Ben Dooks79861bb2014-04-10 18:16:33 -0700281 clk_prepare_enable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800282
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000283 /*
284 * Initialize the operation of the SRC internal circuits
285 * see rsnd_src_start()
286 */
287 rsnd_mod_write(mod, SRC_SRCIR, 1);
288
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800289 return 0;
290}
291
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800292static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700293 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800294{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800295 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800296
Ben Dooks79861bb2014-04-10 18:16:33 -0700297 clk_disable_unprepare(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800298
299 return 0;
300}
301
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800302static int rsnd_src_start(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700303 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800304{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800305 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800306
307 /*
308 * Cancel the initialization and operate the SRC function
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000309 * see rsnd_src_init()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800310 */
311 rsnd_mod_write(mod, SRC_SRCIR, 0);
312
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800313 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800314 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
315
316 return 0;
317}
318
319
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800320static int rsnd_src_stop(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700321 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800322{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800323 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800324
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800325 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800326 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
327
328 return 0;
329}
330
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800331/*
332 * Gen1 functions
333 */
334static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700335 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700336{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700337 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800338 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700339 u32 mask;
340 int shift;
341 } routes[] = {
342 { 0xF, 0, }, /* 0 */
343 { 0xF, 4, }, /* 1 */
344 { 0xF, 8, }, /* 2 */
345 { 0x7, 12, }, /* 3 */
346 { 0x7, 16, }, /* 4 */
347 { 0x7, 20, }, /* 5 */
348 { 0x7, 24, }, /* 6 */
349 { 0x3, 28, }, /* 7 */
350 { 0x3, 30, }, /* 8 */
351 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700352 u32 mask;
353 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700354 int id;
355
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700356 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300357 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700358 return -EIO;
359
360 /*
361 * SRC_ROUTE_SELECT
362 */
363 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
364 val = val << routes[id].shift;
365 mask = routes[id].mask << routes[id].shift;
366
367 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
368
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800369 return 0;
370}
371
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800372static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700373 struct rsnd_dai *rdai)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800374{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700375 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800376 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800377 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800378 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800379 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800380 u32 mask;
381 u32 val;
382 int shift;
383 int id = rsnd_mod_id(mod);
384 int ret;
385
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700386 /*
387 * SRC_TIMING_SELECT
388 */
389 shift = (id % 4) * 8;
390 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800391
392 /*
393 * ADG is used as source clock if SRC was used,
394 * then, SSI WS is used as destination clock.
395 * SSI WS is used as source clock if SRC is not used
396 * (when playback, source/destination become reverse when capture)
397 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800398 ret = 0;
399 if (convert_rate) {
400 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800401 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800402 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
403 runtime->rate,
404 convert_rate);
405 } else if (8 == id) {
406 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700407 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800408 } else {
409 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700410 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800411 }
412
413 if (ret < 0)
414 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700415
416 switch (id / 4) {
417 case 0:
418 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
419 break;
420 case 1:
421 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
422 break;
423 case 2:
424 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
425 break;
426 }
427
428 return 0;
429}
430
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800431static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700432 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700433{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800434 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700435
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700436 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800437 if (ret < 0)
438 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700439
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800440 /* Select SRC mode (fixed value) */
441 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800442
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800443 /* Set the restriction value of the FS ratio (98%) */
444 rsnd_mod_write(mod, SRC_MNFSR,
445 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700446
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800447 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800448
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700449 return 0;
450}
451
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700452static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
453 struct rsnd_dai *rdai)
454{
455 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
456 struct device *dev = rsnd_priv_to_dev(priv);
457
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800458 dev_dbg(dev, "%s[%d] (Gen1) is probed\n",
459 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700460
461 return 0;
462}
463
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800464static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700465 struct rsnd_dai *rdai)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700466{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700467 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700468
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700469 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800470 if (ret < 0)
471 return ret;
472
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700473 ret = rsnd_src_set_route_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700474 if (ret < 0)
475 return ret;
476
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700477 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700478 if (ret < 0)
479 return ret;
480
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700481 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800482 if (ret < 0)
483 return ret;
484
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800485 return 0;
486}
487
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800488static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700489 struct rsnd_dai *rdai)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800490{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800491 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800492
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800493 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800494
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700495 return rsnd_src_start(mod, rdai);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800496}
497
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800498static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700499 struct rsnd_dai *rdai)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800500{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800501 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800502
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800503 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800504
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700505 return rsnd_src_stop(mod, rdai);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800506}
507
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800508static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700509 .name = SRC_NAME,
510 .probe = rsnd_src_probe_gen1,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800511 .init = rsnd_src_init_gen1,
512 .quit = rsnd_src_quit,
513 .start = rsnd_src_start_gen1,
514 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700515};
516
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800517/*
518 * Gen2 functions
519 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800520static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700521 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800522{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700523 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
524 struct device *dev = rsnd_priv_to_dev(priv);
525 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
526 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
527 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotob167a572014-11-27 08:06:34 +0000528 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700529 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800530 int ret;
531
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700532 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimotob167a572014-11-27 08:06:34 +0000533 if (!convert_rate)
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700534 ratio = 0;
Kuninori Morimotob167a572014-11-27 08:06:34 +0000535 else if (convert_rate > runtime->rate)
536 ratio = 100 * convert_rate / runtime->rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700537 else
Kuninori Morimotob167a572014-11-27 08:06:34 +0000538 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700539
540 if (ratio > 600) {
541 dev_err(dev, "FSO/FSI ratio error\n");
542 return -EINVAL;
543 }
544
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700545 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800546 if (ret < 0)
547 return ret;
548
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800549 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
550
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700551 switch (rsnd_mod_id(mod)) {
552 case 5:
553 case 6:
554 case 7:
555 case 8:
556 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
557 break;
558 default:
559 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
560 break;
561 }
562
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800563 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
564
565 return 0;
566}
567
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800568static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700569 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800570{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700571 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800572 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800573 struct rsnd_src *src = rsnd_mod_to_src(mod);
574 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800575 int ret;
576
577 if (convert_rate)
578 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
579 runtime->rate,
580 convert_rate);
581 else
582 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
583
584 return ret;
585}
586
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800587static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700588 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800589{
590 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800591 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800592 struct device *dev = rsnd_priv_to_dev(priv);
593 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800594
595 ret = rsnd_dma_init(priv,
596 rsnd_mod_to_dma(mod),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700597 rsnd_info_is_playback(priv, src),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800598 src->info->dma_id);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800599 if (ret < 0)
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800600 dev_err(dev, "%s[%d] (Gen2) failed\n",
601 rsnd_mod_name(mod), rsnd_mod_id(mod));
602 else
603 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
604 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700605
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800606 return ret;
607}
608
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800609static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700610 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800611{
612 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
613
614 return 0;
615}
616
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800617static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700618 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800619{
620 int ret;
621
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700622 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800623 if (ret < 0)
624 return ret;
625
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700626 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800627 if (ret < 0)
628 return ret;
629
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700630 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800631 if (ret < 0)
632 return ret;
633
634 return 0;
635}
636
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800637static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700638 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800639{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700640 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800641 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700642 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800643
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800644 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800645
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700646 rsnd_mod_write(mod, SRC_CTRL, val);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800647
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700648 return rsnd_src_start(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800649}
650
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800651static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700652 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800653{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800654 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800655
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800656 rsnd_mod_write(mod, SRC_CTRL, 0);
657
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800658 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800659
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700660 return rsnd_src_stop(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800661}
662
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800663static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700664 .name = SRC_NAME,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800665 .probe = rsnd_src_probe_gen2,
666 .remove = rsnd_src_remove_gen2,
667 .init = rsnd_src_init_gen2,
668 .quit = rsnd_src_quit,
669 .start = rsnd_src_start_gen2,
670 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800671};
672
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800673struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700674{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800675 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100676 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700677
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800678 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700679}
680
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700681static void rsnd_of_parse_src(struct platform_device *pdev,
682 const struct rsnd_of_data *of_data,
683 struct rsnd_priv *priv)
684{
685 struct device_node *src_node;
686 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
687 struct rsnd_src_platform_info *src_info;
688 struct device *dev = &pdev->dev;
689 int nr;
690
691 if (!of_data)
692 return;
693
694 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
695 if (!src_node)
696 return;
697
698 nr = of_get_child_count(src_node);
699 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700700 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700701
702 src_info = devm_kzalloc(dev,
703 sizeof(struct rsnd_src_platform_info) * nr,
704 GFP_KERNEL);
705 if (!src_info) {
706 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700707 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700708 }
709
710 info->src_info = src_info;
711 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -0700712
713rsnd_of_parse_src_end:
714 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700715}
716
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800717int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700718 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700719 struct rsnd_priv *priv)
720{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800721 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700722 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800723 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800724 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800725 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800726 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700727 int i, nr;
728
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700729 ops = NULL;
730 if (rsnd_is_gen1(priv))
731 ops = &rsnd_src_gen1_ops;
732 if (rsnd_is_gen2(priv))
733 ops = &rsnd_src_gen2_ops;
734 if (!ops) {
735 dev_err(dev, "unknown Generation\n");
736 return -EIO;
737 }
738
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700739 rsnd_of_parse_src(pdev, of_data, priv);
740
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700741 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800742 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700743 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800744 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800745 if (!nr)
746 return 0;
747
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800748 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
749 if (!src) {
750 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700751 return -ENOMEM;
752 }
753
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800754 priv->src_nr = nr;
755 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700756
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800757 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700758 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
759 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800760
761 clk = devm_clk_get(dev, name);
762 if (IS_ERR(clk))
763 return PTR_ERR(clk);
764
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800765 src->info = &info->src_info[i];
766 src->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700767
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800768 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800769
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800770 dev_dbg(dev, "SRC%d probed\n", i);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700771 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700772
773 return 0;
774}