blob: 5693bb5c420b67939c509a3ee4664980ba405b28 [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 Morimotocfcefe02015-01-08 01:52:36 +000015/* SRCx_STATUS */
16#define OUF_SRCO ((1 << 12) | (1 << 13))
17#define OUF_SRCI ((1 << 9) | (1 << 8))
18
19/* SCU_SYSTEM_STATUS0/1 */
20#define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
21
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080022struct rsnd_src {
23 struct rsnd_src_platform_info *info; /* rcar_snd.h */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070024 struct rsnd_mod mod;
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000025 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
26 struct rsnd_kctrl_cfg_s sync; /* sync convert */
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +000027 u32 convert_rate; /* sampling rate convert */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +000028 int err;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070029};
30
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080031#define RSND_SRC_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070032
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000033#define rsnd_enable_sync_convert(src) ((src)->sen.val)
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +000034#define rsnd_src_of_node(priv) \
35 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
36
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080037#define rsnd_mod_to_src(_mod) \
38 container_of((_mod), struct rsnd_src, mod)
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080039
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080040#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080041 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080042 ((i) < rsnd_src_nr(priv)) && \
43 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080044 i++)
45
46
Kuninori Morimotoef749402013-12-19 19:28:51 -080047/*
48 * image of SRC (Sampling Rate Converter)
49 *
50 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
51 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
52 * 44.1kHz <-> +-----+ +-----+ +-------+
53 * ...
54 *
55 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070056
Kuninori Morimoto41c62212014-01-23 18:39:56 -080057/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080058 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080059 *
60 * Gen1
61 *
62 * [mem] -> [SRU] -> [SSI]
63 * |--------|
64 *
65 * Gen2
66 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080067 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080068 * |-----------------|
69 */
70
71/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080072 * How to use SRC bypass mode for debugging
73 *
74 * SRC has bypass mode, and it is useful for debugging.
75 * In Gen2 case,
76 * SRCm_MODE controls whether SRC is used or not
77 * SSI_MODE0 controls whether SSIU which receives SRC data
78 * is used or not.
79 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
80 * but SRC bypass mode needs SSI_MODE0 only.
81 *
82 * This driver request
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080083 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080084 * u32 convert_rate;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070085 * int dma_id;
Kuninori Morimoto41c62212014-01-23 18:39:56 -080086 * }
87 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080088 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080089 * above convert_rate, and it controls
90 * whether SRC is used or not.
91 *
92 * ex) doesn't use SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070093 * static struct rsnd_dai_platform_info rsnd_dai = {
94 * .playback = { .ssi = &rsnd_ssi[0], },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080095 * };
96 *
97 * ex) uses SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070098 * static struct rsnd_src_platform_info rsnd_src[] = {
99 * RSND_SCU(48000, 0),
100 * ...
101 * };
102 * static struct rsnd_dai_platform_info rsnd_dai = {
103 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800104 * };
105 *
106 * ex) uses SRC bypass mode
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700107 * static struct rsnd_src_platform_info rsnd_src[] = {
108 * RSND_SCU(0, 0),
109 * ...
110 * };
111 * static struct rsnd_dai_platform_info rsnd_dai = {
112 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800113 * };
114 *
115 */
116
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800117/*
118 * Gen1/Gen2 common functions
119 */
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000120static struct dma_chan *rsnd_src_dma_req(struct rsnd_mod *mod)
121{
122 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
123 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
124 int is_play = rsnd_io_is_play(io);
125
126 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
127 mod,
128 is_play ? "rx" : "tx");
129}
130
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700131int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000132 struct rsnd_dai_stream *io,
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700133 int use_busif)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800134{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000135 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700136 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800137 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800138
139 /*
140 * SSI_MODE0
141 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800142 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700143 !use_busif << ssi_id);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800144
145 /*
146 * SSI_MODE1
147 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800148 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800149 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800150 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800151 case 1:
152 shift = 0;
153 break;
154 case 2:
155 shift = 2;
156 break;
157 case 4:
158 shift = 16;
159 break;
160 }
161
162 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800163 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800164 0x3 << shift,
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000165 rsnd_rdai_is_clk_master(rdai) ?
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800166 0x2 << shift : 0x1 << shift);
167 }
168
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700169 /*
170 * DMA settings for SSIU
171 */
172 if (use_busif) {
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700173 u32 val = 0x76543210;
174 u32 mask = ~0;
175
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700176 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000177 rsnd_get_adinr(ssi_mod, io));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700178 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
179 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700180
181 mask <<= runtime->channels * 4;
182 val = val & mask;
183
184 switch (runtime->sample_bits) {
185 case 16:
186 val |= 0x67452301 & ~mask;
187 break;
188 case 32:
189 val |= 0x76543210 & ~mask;
190 break;
191 }
192 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
193
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700194 }
195
196 return 0;
197}
198
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000199int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
200 struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700201{
202 /*
203 * DMA settings for SSIU
204 */
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000205 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700206
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800207 return 0;
208}
209
Kuninori Morimotof708d942015-01-15 08:07:19 +0000210int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800211{
212 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
213
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000214 if (rsnd_is_gen1(priv))
215 return 0;
216
217 /* enable SSI interrupt if Gen2 */
218 if (rsnd_ssi_is_dma_mode(ssi_mod))
219 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
220 else
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800221 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
222
223 return 0;
224}
225
Kuninori Morimotof708d942015-01-15 08:07:19 +0000226int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000227{
228 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
229
230 if (rsnd_is_gen1(priv))
231 return 0;
232
233 /* disable SSI interrupt if Gen2 */
234 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
235
236 return 0;
237}
238
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000239static u32 rsnd_src_convert_rate(struct rsnd_src *src)
240{
241 struct rsnd_mod *mod = &src->mod;
242 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
243 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
244 u32 convert_rate;
245
246 if (!runtime)
247 return 0;
248
249 if (!rsnd_enable_sync_convert(src))
250 return src->convert_rate;
251
252 convert_rate = src->sync.val;
253
254 if (!convert_rate)
255 convert_rate = src->convert_rate;
256
257 if (!convert_rate)
258 convert_rate = runtime->rate;
259
260 return convert_rate;
261}
262
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800263unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800264 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800265 struct snd_pcm_runtime *runtime)
266{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700267 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800268 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700269 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800270
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700271 if (src_mod) {
272 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800273
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700274 /*
275 * return convert rate if SRC is used,
276 * otherwise, return runtime->rate as usual
277 */
278 rate = rsnd_src_convert_rate(src);
279 }
280
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800281 if (!rate)
282 rate = runtime->rate;
283
284 return rate;
285}
286
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000287static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
288 struct rsnd_dai_stream *io)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800289{
290 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800291 struct rsnd_src *src = rsnd_mod_to_src(mod);
292 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800293 u32 fsrate = 0;
294
295 if (convert_rate)
296 fsrate = 0x0400000 / convert_rate * runtime->rate;
297
298 /* set/clear soft reset */
299 rsnd_mod_write(mod, SRC_SWRSR, 0);
300 rsnd_mod_write(mod, SRC_SWRSR, 1);
301
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800302 /* Set channel number and output bit length */
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000303 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod, io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800304
305 /* Enable the initial value of IFS */
306 if (fsrate) {
307 rsnd_mod_write(mod, SRC_IFSCR, 1);
308
309 /* Set initial value of IFS */
310 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
311 }
312
313 /* use DMA transfer */
314 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
315
316 return 0;
317}
318
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000319static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000320 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000321 struct snd_pcm_substream *substream,
322 struct snd_pcm_hw_params *fe_params)
323{
324 struct rsnd_src *src = rsnd_mod_to_src(mod);
325 struct snd_soc_pcm_runtime *fe = substream->private_data;
326
327 /* default value (mainly for non-DT) */
328 src->convert_rate = src->info->convert_rate;
329
330 /*
331 * SRC assumes that it is used under DPCM if user want to use
332 * sampling rate convert. Then, SRC should be FE.
333 * And then, this function will be called *after* BE settings.
334 * this means, each BE already has fixuped hw_params.
335 * see
336 * dpcm_fe_dai_hw_params()
337 * dpcm_be_dai_hw_params()
338 */
339 if (fe->dai_link->dynamic) {
340 int stream = substream->stream;
341 struct snd_soc_dpcm *dpcm;
342 struct snd_pcm_hw_params *be_params;
343
344 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
345 be_params = &dpcm->hw_params;
346
347 if (params_rate(fe_params) != params_rate(be_params))
348 src->convert_rate = params_rate(be_params);
349 }
350 }
351
352 return 0;
353}
354
355static int rsnd_src_init(struct rsnd_mod *mod,
356 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800357{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800358 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800359
Kuninori Morimoto85642952015-01-15 08:03:22 +0000360 rsnd_mod_hw_start(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800361
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000362 src->err = 0;
363
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000364 /* reset sync convert_rate */
365 src->sync.val = 0;
366
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000367 /*
368 * Initialize the operation of the SRC internal circuits
369 * see rsnd_src_start()
370 */
371 rsnd_mod_write(mod, SRC_SRCIR, 1);
372
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800373 return 0;
374}
375
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800376static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000377 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000378 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800379{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800380 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000381 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800382
Kuninori Morimoto85642952015-01-15 08:03:22 +0000383 rsnd_mod_hw_stop(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800384
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000385 if (src->err)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000386 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
387 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000388
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000389 src->convert_rate = 0;
390
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000391 /* reset sync convert_rate */
392 src->sync.val = 0;
393
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800394 return 0;
395}
396
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000397static int rsnd_src_start(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800398{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800399 /*
400 * Cancel the initialization and operate the SRC function
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000401 * see rsnd_src_init()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800402 */
403 rsnd_mod_write(mod, SRC_SRCIR, 0);
404
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800405 return 0;
406}
407
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000408static int rsnd_src_stop(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800409{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000410 /* nothing to do */
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800411 return 0;
412}
413
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800414/*
415 * Gen1 functions
416 */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000417static int rsnd_src_set_route_gen1(struct rsnd_mod *mod)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700418{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700419 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800420 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700421 u32 mask;
422 int shift;
423 } routes[] = {
424 { 0xF, 0, }, /* 0 */
425 { 0xF, 4, }, /* 1 */
426 { 0xF, 8, }, /* 2 */
427 { 0x7, 12, }, /* 3 */
428 { 0x7, 16, }, /* 4 */
429 { 0x7, 20, }, /* 5 */
430 { 0x7, 24, }, /* 6 */
431 { 0x3, 28, }, /* 7 */
432 { 0x3, 30, }, /* 8 */
433 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700434 u32 mask;
435 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700436 int id;
437
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700438 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300439 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700440 return -EIO;
441
442 /*
443 * SRC_ROUTE_SELECT
444 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000445 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700446 val = val << routes[id].shift;
447 mask = routes[id].mask << routes[id].shift;
448
449 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
450
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800451 return 0;
452}
453
Kuninori Morimotof708d942015-01-15 08:07:19 +0000454static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800455{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700456 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800457 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800458 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800459 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800460 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800461 u32 mask;
462 u32 val;
463 int shift;
464 int id = rsnd_mod_id(mod);
465 int ret;
466
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700467 /*
468 * SRC_TIMING_SELECT
469 */
470 shift = (id % 4) * 8;
471 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800472
473 /*
474 * ADG is used as source clock if SRC was used,
475 * then, SSI WS is used as destination clock.
476 * SSI WS is used as source clock if SRC is not used
477 * (when playback, source/destination become reverse when capture)
478 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800479 ret = 0;
480 if (convert_rate) {
481 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800482 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800483 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
484 runtime->rate,
485 convert_rate);
486 } else if (8 == id) {
487 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700488 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800489 } else {
490 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700491 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800492 }
493
494 if (ret < 0)
495 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700496
497 switch (id / 4) {
498 case 0:
499 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
500 break;
501 case 1:
502 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
503 break;
504 case 2:
505 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
506 break;
507 }
508
509 return 0;
510}
511
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000512static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
513 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700514{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000515 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800516 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700517
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000518 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800519 if (ret < 0)
520 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700521
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800522 /* Select SRC mode (fixed value) */
523 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800524
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800525 /* Set the restriction value of the FS ratio (98%) */
526 rsnd_mod_write(mod, SRC_MNFSR,
527 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700528
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000529 /* Gen1/Gen2 are not compatible */
530 if (rsnd_src_convert_rate(src))
531 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
532
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800533 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800534
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700535 return 0;
536}
537
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800538static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000539 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000540 struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700541{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700542 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700543
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000544 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800545 if (ret < 0)
546 return ret;
547
Kuninori Morimotof708d942015-01-15 08:07:19 +0000548 ret = rsnd_src_set_route_gen1(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700549 if (ret < 0)
550 return ret;
551
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000552 ret = rsnd_src_set_convert_rate_gen1(mod, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700553 if (ret < 0)
554 return ret;
555
Kuninori Morimotof708d942015-01-15 08:07:19 +0000556 ret = rsnd_src_set_convert_timing_gen1(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800557 if (ret < 0)
558 return ret;
559
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800560 return 0;
561}
562
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800563static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000564 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000565 struct rsnd_priv *priv)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800566{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800567 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800568
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800569 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800570
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000571 return rsnd_src_start(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800572}
573
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800574static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000575 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000576 struct rsnd_priv *priv)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800577{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800578 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800579
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800580 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800581
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000582 return rsnd_src_stop(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800583}
584
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800585static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700586 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000587 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800588 .init = rsnd_src_init_gen1,
589 .quit = rsnd_src_quit,
590 .start = rsnd_src_start_gen1,
591 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000592 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700593};
594
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800595/*
596 * Gen2 functions
597 */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000598#define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
599#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
600static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
601{
602 struct rsnd_src *src = rsnd_mod_to_src(mod);
603 u32 sys_int_val, int_val, sys_int_mask;
604 int irq = src->info->irq;
605 int id = rsnd_mod_id(mod);
606
607 sys_int_val =
608 sys_int_mask = OUF_SRC(id);
609 int_val = 0x3300;
610
611 /*
612 * IRQ is not supported on non-DT
613 * see
614 * rsnd_src_probe_gen2()
615 */
616 if ((irq <= 0) || !enable) {
617 sys_int_val = 0;
618 int_val = 0;
619 }
620
621 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
622 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
623 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
624}
625
626static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
627{
628 u32 val = OUF_SRC(rsnd_mod_id(mod));
629
630 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
631 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
632}
633
634static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
635{
636 u32 val = OUF_SRC(rsnd_mod_id(mod));
637 bool ret = false;
638
639 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
640 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
641 struct rsnd_src *src = rsnd_mod_to_src(mod);
642
643 src->err++;
644 ret = true;
645 }
646
647 /* clear error static */
648 rsnd_src_error_clear_gen2(mod);
649
650 return ret;
651}
652
653static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
654{
655 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
656 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
657
658 rsnd_mod_write(mod, SRC_CTRL, val);
659
660 rsnd_src_error_clear_gen2(mod);
661
662 rsnd_src_start(mod);
663
664 rsnd_src_irq_enable_gen2(mod);
665
666 return 0;
667}
668
669static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
670{
671 rsnd_src_irq_disable_gen2(mod);
672
673 rsnd_mod_write(mod, SRC_CTRL, 0);
674
675 rsnd_src_error_record_gen2(mod);
676
677 return rsnd_src_stop(mod);
678}
679
680static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
681{
682 struct rsnd_mod *mod = data;
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000683 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000684
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000685 spin_lock(&priv->lock);
686
687 /* ignore all cases if not working */
688 if (!rsnd_mod_is_working(mod))
689 goto rsnd_src_interrupt_gen2_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000690
691 if (rsnd_src_error_record_gen2(mod)) {
692 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000693 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000694 struct device *dev = rsnd_priv_to_dev(priv);
695
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000696 dev_dbg(dev, "%s[%d] restart\n",
697 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000698
699 _rsnd_src_stop_gen2(mod);
700 if (src->err < 1024)
701 _rsnd_src_start_gen2(mod);
702 else
703 dev_warn(dev, "no more SRC restart\n");
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000704 }
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000705rsnd_src_interrupt_gen2_out:
706 spin_unlock(&priv->lock);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000707
708 return IRQ_HANDLED;
709}
710
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000711static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
712 struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800713{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700714 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
715 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700716 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
717 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotob167a572014-11-27 08:06:34 +0000718 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000719 u32 cr, route;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700720 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800721 int ret;
722
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700723 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimotob167a572014-11-27 08:06:34 +0000724 if (!convert_rate)
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700725 ratio = 0;
Kuninori Morimotob167a572014-11-27 08:06:34 +0000726 else if (convert_rate > runtime->rate)
727 ratio = 100 * convert_rate / runtime->rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700728 else
Kuninori Morimotob167a572014-11-27 08:06:34 +0000729 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700730
731 if (ratio > 600) {
732 dev_err(dev, "FSO/FSI ratio error\n");
733 return -EINVAL;
734 }
735
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000736 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800737 if (ret < 0)
738 return ret;
739
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000740 cr = 0x00011110;
741 route = 0x0;
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000742 if (convert_rate) {
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000743 route = 0x1;
744
745 if (rsnd_enable_sync_convert(src)) {
746 cr |= 0x1;
747 route |= rsnd_io_is_play(io) ?
748 (0x1 << 24) : (0x1 << 25);
749 }
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000750 }
751
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000752 rsnd_mod_write(mod, SRC_SRCCR, cr);
753 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
754
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700755 switch (rsnd_mod_id(mod)) {
756 case 5:
757 case 6:
758 case 7:
759 case 8:
760 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
761 break;
762 default:
763 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
764 break;
765 }
766
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800767 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
768
769 return 0;
770}
771
Kuninori Morimotof708d942015-01-15 08:07:19 +0000772static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800773{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700774 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800775 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800776 struct rsnd_src *src = rsnd_mod_to_src(mod);
777 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800778 int ret;
779
780 if (convert_rate)
Kuninori Morimotof708d942015-01-15 08:07:19 +0000781 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800782 runtime->rate,
783 convert_rate);
784 else
Kuninori Morimotof708d942015-01-15 08:07:19 +0000785 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800786
787 return ret;
788}
789
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800790static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000791 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000792 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800793{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800794 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800795 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000796 int irq = src->info->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800797 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800798
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000799 if (irq > 0) {
800 /*
801 * IRQ is not supported on non-DT
802 * see
803 * rsnd_src_irq_enable_gen2()
804 */
805 ret = devm_request_irq(dev, irq,
806 rsnd_src_interrupt_gen2,
807 IRQF_SHARED,
808 dev_name(dev), mod);
809 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000810 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000811 }
812
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800813 ret = rsnd_dma_init(priv,
814 rsnd_mod_to_dma(mod),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800815 src->info->dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700816
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800817 return ret;
818}
819
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800820static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000821 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000822 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800823{
Kuninori Morimoto8a2ff422015-02-20 10:24:06 +0000824 rsnd_dma_quit(rsnd_mod_to_dma(mod));
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800825
826 return 0;
827}
828
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800829static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000830 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000831 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800832{
833 int ret;
834
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000835 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800836 if (ret < 0)
837 return ret;
838
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000839 ret = rsnd_src_set_convert_rate_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800840 if (ret < 0)
841 return ret;
842
Kuninori Morimotof708d942015-01-15 08:07:19 +0000843 ret = rsnd_src_set_convert_timing_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800844 if (ret < 0)
845 return ret;
846
847 return 0;
848}
849
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800850static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000851 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000852 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800853{
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000854 rsnd_dma_start(rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800855
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000856 return _rsnd_src_start_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800857}
858
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800859static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000860 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000861 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800862{
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000863 int ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800864
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000865 ret = _rsnd_src_stop_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800866
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000867 rsnd_dma_stop(rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800868
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000869 return ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800870}
871
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000872static void rsnd_src_reconvert_update(struct rsnd_mod *mod)
873{
874 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
875 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
876 struct rsnd_src *src = rsnd_mod_to_src(mod);
877 u32 convert_rate = rsnd_src_convert_rate(src);
878 u32 fsrate;
879
880 if (!runtime)
881 return;
882
883 if (!convert_rate)
884 convert_rate = runtime->rate;
885
886 fsrate = 0x0400000 / convert_rate * runtime->rate;
887
888 /* update IFS */
889 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
890}
891
892static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000893 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000894 struct snd_soc_pcm_runtime *rtd)
895{
896 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000897 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
898 struct rsnd_src *src = rsnd_mod_to_src(mod);
899 int ret;
900
901 /*
902 * enable SRC sync convert if possible
903 */
904
905 /*
906 * Gen1 is not supported
907 */
908 if (rsnd_is_gen1(priv))
909 return 0;
910
911 /*
912 * SRC sync convert needs clock master
913 */
914 if (!rsnd_rdai_is_clk_master(rdai))
915 return 0;
916
917 /*
918 * We can't use SRC sync convert
919 * if it has DVC
920 */
921 if (rsnd_io_to_mod_dvc(io))
922 return 0;
923
924 /*
925 * enable sync convert
926 */
927 ret = rsnd_kctrl_new_s(mod, rtd,
928 rsnd_io_is_play(io) ?
929 "SRC Out Rate Switch" :
930 "SRC In Rate Switch",
931 rsnd_src_reconvert_update,
932 &src->sen, 1);
933 if (ret < 0)
934 return ret;
935
936 ret = rsnd_kctrl_new_s(mod, rtd,
937 rsnd_io_is_play(io) ?
938 "SRC Out Rate" :
939 "SRC In Rate",
940 rsnd_src_reconvert_update,
941 &src->sync, 192000);
942
943 return ret;
944}
945
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800946static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700947 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000948 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800949 .probe = rsnd_src_probe_gen2,
950 .remove = rsnd_src_remove_gen2,
951 .init = rsnd_src_init_gen2,
952 .quit = rsnd_src_quit,
953 .start = rsnd_src_start_gen2,
954 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000955 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000956 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800957};
958
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800959struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700960{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800961 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100962 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700963
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800964 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700965}
966
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700967static void rsnd_of_parse_src(struct platform_device *pdev,
968 const struct rsnd_of_data *of_data,
969 struct rsnd_priv *priv)
970{
971 struct device_node *src_node;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000972 struct device_node *np;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700973 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
974 struct rsnd_src_platform_info *src_info;
975 struct device *dev = &pdev->dev;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000976 int nr, i;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700977
978 if (!of_data)
979 return;
980
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +0000981 src_node = rsnd_src_of_node(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700982 if (!src_node)
983 return;
984
985 nr = of_get_child_count(src_node);
986 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700987 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700988
989 src_info = devm_kzalloc(dev,
990 sizeof(struct rsnd_src_platform_info) * nr,
991 GFP_KERNEL);
992 if (!src_info) {
993 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700994 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700995 }
996
997 info->src_info = src_info;
998 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -0700999
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001000 i = 0;
1001 for_each_child_of_node(src_node, np) {
1002 src_info[i].irq = irq_of_parse_and_map(np, 0);
1003
1004 i++;
1005 }
1006
Kuninori Morimotof451e482014-05-22 23:24:59 -07001007rsnd_of_parse_src_end:
1008 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001009}
1010
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001011int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001012 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001013 struct rsnd_priv *priv)
1014{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -08001015 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001016 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001017 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -08001018 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -08001019 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001020 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001021 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001022
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001023 ops = NULL;
1024 if (rsnd_is_gen1(priv))
1025 ops = &rsnd_src_gen1_ops;
1026 if (rsnd_is_gen2(priv))
1027 ops = &rsnd_src_gen2_ops;
1028 if (!ops) {
1029 dev_err(dev, "unknown Generation\n");
1030 return -EIO;
1031 }
1032
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001033 rsnd_of_parse_src(pdev, of_data, priv);
1034
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001035 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001036 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001037 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001038 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -08001039 if (!nr)
1040 return 0;
1041
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001042 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1043 if (!src) {
1044 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001045 return -ENOMEM;
1046 }
1047
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001048 priv->src_nr = nr;
1049 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001050
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001051 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001052 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1053 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -08001054
1055 clk = devm_clk_get(dev, name);
1056 if (IS_ERR(clk))
1057 return PTR_ERR(clk);
1058
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001059 src->info = &info->src_info[i];
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001060
Kuninori Morimoto2099bc82015-06-15 06:24:54 +00001061 ret = rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001062 if (ret)
1063 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -07001064 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001065
1066 return 0;
1067}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001068
1069void rsnd_src_remove(struct platform_device *pdev,
1070 struct rsnd_priv *priv)
1071{
1072 struct rsnd_src *src;
1073 int i;
1074
1075 for_each_rsnd_src(src, priv, i) {
1076 rsnd_mod_quit(&src->mod);
1077 }
1078}