blob: b3d965ee9b7765de985e9d5ead80c80e7cc59b1e [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 Morimotoda599fd2015-07-15 07:11:21 +000033#define rsnd_src_nr(priv) ((priv)->src_nr)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000034#define rsnd_enable_sync_convert(src) ((src)->sen.val)
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +000035#define rsnd_src_of_node(priv) \
36 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
37
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080038#define rsnd_mod_to_src(_mod) \
39 container_of((_mod), struct rsnd_src, mod)
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080040
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080041#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080042 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080043 ((i) < rsnd_src_nr(priv)) && \
44 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080045 i++)
46
47
Kuninori Morimotoef749402013-12-19 19:28:51 -080048/*
49 * image of SRC (Sampling Rate Converter)
50 *
51 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
52 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
53 * 44.1kHz <-> +-----+ +-----+ +-------+
54 * ...
55 *
56 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070057
Kuninori Morimoto41c62212014-01-23 18:39:56 -080058/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080059 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080060 *
61 * Gen1
62 *
63 * [mem] -> [SRU] -> [SSI]
64 * |--------|
65 *
66 * Gen2
67 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080068 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080069 * |-----------------|
70 */
71
72/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080073 * How to use SRC bypass mode for debugging
74 *
75 * SRC has bypass mode, and it is useful for debugging.
76 * In Gen2 case,
77 * SRCm_MODE controls whether SRC is used or not
78 * SSI_MODE0 controls whether SSIU which receives SRC data
79 * is used or not.
80 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
81 * but SRC bypass mode needs SSI_MODE0 only.
82 *
83 * This driver request
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080084 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080085 * u32 convert_rate;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070086 * int dma_id;
Kuninori Morimoto41c62212014-01-23 18:39:56 -080087 * }
88 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080089 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080090 * above convert_rate, and it controls
91 * whether SRC is used or not.
92 *
93 * ex) doesn't use SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070094 * static struct rsnd_dai_platform_info rsnd_dai = {
95 * .playback = { .ssi = &rsnd_ssi[0], },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080096 * };
97 *
98 * ex) uses SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070099 * static struct rsnd_src_platform_info rsnd_src[] = {
100 * RSND_SCU(48000, 0),
101 * ...
102 * };
103 * static struct rsnd_dai_platform_info rsnd_dai = {
104 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800105 * };
106 *
107 * ex) uses SRC bypass mode
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700108 * static struct rsnd_src_platform_info rsnd_src[] = {
109 * RSND_SCU(0, 0),
110 * ...
111 * };
112 * static struct rsnd_dai_platform_info rsnd_dai = {
113 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800114 * };
115 *
116 */
117
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800118/*
119 * Gen1/Gen2 common functions
120 */
Kuninori Morimoto379febf2015-07-15 07:12:18 +0000121static void rsnd_src_soft_reset(struct rsnd_mod *mod)
122{
123 rsnd_mod_write(mod, SRC_SWRSR, 0);
124 rsnd_mod_write(mod, SRC_SWRSR, 1);
125}
126
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000127
128#define rsnd_src_initialize_lock(mod) __rsnd_src_initialize_lock(mod, 1)
129#define rsnd_src_initialize_unlock(mod) __rsnd_src_initialize_lock(mod, 0)
130static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
131{
132 rsnd_mod_write(mod, SRC_SRCIR, enable);
133}
134
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000135static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
136 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000137{
138 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000139 int is_play = rsnd_io_is_play(io);
140
141 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
142 mod,
143 is_play ? "rx" : "tx");
144}
145
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700146int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000147 struct rsnd_dai_stream *io,
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700148 int use_busif)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800149{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000150 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700151 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800152 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800153
154 /*
155 * SSI_MODE0
156 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800157 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700158 !use_busif << ssi_id);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800159
160 /*
161 * SSI_MODE1
162 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800163 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800164 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800165 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800166 case 1:
167 shift = 0;
168 break;
169 case 2:
170 shift = 2;
171 break;
172 case 4:
173 shift = 16;
174 break;
175 }
176
177 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800178 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800179 0x3 << shift,
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000180 rsnd_rdai_is_clk_master(rdai) ?
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800181 0x2 << shift : 0x1 << shift);
182 }
183
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700184 /*
185 * DMA settings for SSIU
186 */
187 if (use_busif) {
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700188 u32 val = 0x76543210;
189 u32 mask = ~0;
190
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700191 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000192 rsnd_get_adinr(ssi_mod, io));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700193 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
194 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700195
196 mask <<= runtime->channels * 4;
197 val = val & mask;
198
199 switch (runtime->sample_bits) {
200 case 16:
201 val |= 0x67452301 & ~mask;
202 break;
203 case 32:
204 val |= 0x76543210 & ~mask;
205 break;
206 }
Kuninori Morimotocdde84d2015-07-15 07:09:27 +0000207 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700208
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700209 }
210
211 return 0;
212}
213
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000214int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
215 struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700216{
217 /*
218 * DMA settings for SSIU
219 */
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000220 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700221
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800222 return 0;
223}
224
Kuninori Morimotof708d942015-01-15 08:07:19 +0000225int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800226{
227 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
228
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000229 if (rsnd_is_gen1(priv))
230 return 0;
231
232 /* enable SSI interrupt if Gen2 */
Kuninori Morimotoefa991d2015-07-15 07:09:47 +0000233 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
234 rsnd_ssi_is_dma_mode(ssi_mod) ?
235 0x0e000000 : 0x0f000000);
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800236
237 return 0;
238}
239
Kuninori Morimotof708d942015-01-15 08:07:19 +0000240int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000241{
242 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
243
244 if (rsnd_is_gen1(priv))
245 return 0;
246
247 /* disable SSI interrupt if Gen2 */
Kuninori Morimotoefa991d2015-07-15 07:09:47 +0000248 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000249
250 return 0;
251}
252
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000253static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
254 struct rsnd_src *src)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000255{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000256 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
257 u32 convert_rate;
258
259 if (!runtime)
260 return 0;
261
262 if (!rsnd_enable_sync_convert(src))
263 return src->convert_rate;
264
265 convert_rate = src->sync.val;
266
267 if (!convert_rate)
268 convert_rate = src->convert_rate;
269
270 if (!convert_rate)
271 convert_rate = runtime->rate;
272
273 return convert_rate;
274}
275
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800276unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800277 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800278 struct snd_pcm_runtime *runtime)
279{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700280 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800281 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700282 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800283
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700284 if (src_mod) {
285 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800286
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700287 /*
288 * return convert rate if SRC is used,
289 * otherwise, return runtime->rate as usual
290 */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000291 rate = rsnd_src_convert_rate(io, src);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700292 }
293
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800294 if (!rate)
295 rate = runtime->rate;
296
297 return rate;
298}
299
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000300static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
301 struct rsnd_dai_stream *io)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800302{
303 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800304 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000305 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800306 u32 fsrate = 0;
307
308 if (convert_rate)
309 fsrate = 0x0400000 / convert_rate * runtime->rate;
310
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800311 /* Set channel number and output bit length */
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000312 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod, io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800313
314 /* Enable the initial value of IFS */
315 if (fsrate) {
316 rsnd_mod_write(mod, SRC_IFSCR, 1);
317
318 /* Set initial value of IFS */
319 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
320 }
321
322 /* use DMA transfer */
323 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
324
325 return 0;
326}
327
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000328static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000329 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000330 struct snd_pcm_substream *substream,
331 struct snd_pcm_hw_params *fe_params)
332{
333 struct rsnd_src *src = rsnd_mod_to_src(mod);
334 struct snd_soc_pcm_runtime *fe = substream->private_data;
335
336 /* default value (mainly for non-DT) */
337 src->convert_rate = src->info->convert_rate;
338
339 /*
340 * SRC assumes that it is used under DPCM if user want to use
341 * sampling rate convert. Then, SRC should be FE.
342 * And then, this function will be called *after* BE settings.
343 * this means, each BE already has fixuped hw_params.
344 * see
345 * dpcm_fe_dai_hw_params()
346 * dpcm_be_dai_hw_params()
347 */
348 if (fe->dai_link->dynamic) {
349 int stream = substream->stream;
350 struct snd_soc_dpcm *dpcm;
351 struct snd_pcm_hw_params *be_params;
352
353 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
354 be_params = &dpcm->hw_params;
355
356 if (params_rate(fe_params) != params_rate(be_params))
357 src->convert_rate = params_rate(be_params);
358 }
359 }
360
361 return 0;
362}
363
364static int rsnd_src_init(struct rsnd_mod *mod,
365 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800366{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800367 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800368
Kuninori Morimoto85642952015-01-15 08:03:22 +0000369 rsnd_mod_hw_start(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800370
Kuninori Morimoto379febf2015-07-15 07:12:18 +0000371 rsnd_src_soft_reset(mod);
372
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000373 rsnd_src_initialize_lock(mod);
374
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000375 src->err = 0;
376
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000377 /* reset sync convert_rate */
378 src->sync.val = 0;
379
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800380 return 0;
381}
382
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800383static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000384 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000385 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800386{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800387 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000388 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800389
Kuninori Morimoto85642952015-01-15 08:03:22 +0000390 rsnd_mod_hw_stop(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800391
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000392 if (src->err)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000393 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
394 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000395
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000396 src->convert_rate = 0;
397
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000398 /* reset sync convert_rate */
399 src->sync.val = 0;
400
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800401 return 0;
402}
403
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000404static int rsnd_src_start(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800405{
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000406 rsnd_src_initialize_unlock(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800407
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800408 return 0;
409}
410
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000411static int rsnd_src_stop(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800412{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000413 /* nothing to do */
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800414 return 0;
415}
416
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800417/*
418 * Gen1 functions
419 */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000420static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
421 struct rsnd_mod *mod)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700422{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800423 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700424 u32 mask;
425 int shift;
426 } routes[] = {
427 { 0xF, 0, }, /* 0 */
428 { 0xF, 4, }, /* 1 */
429 { 0xF, 8, }, /* 2 */
430 { 0x7, 12, }, /* 3 */
431 { 0x7, 16, }, /* 4 */
432 { 0x7, 20, }, /* 5 */
433 { 0x7, 24, }, /* 6 */
434 { 0x3, 28, }, /* 7 */
435 { 0x3, 30, }, /* 8 */
436 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700437 u32 mask;
438 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700439 int id;
440
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700441 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300442 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700443 return -EIO;
444
445 /*
446 * SRC_ROUTE_SELECT
447 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000448 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700449 val = val << routes[id].shift;
450 mask = routes[id].mask << routes[id].shift;
451
452 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
453
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800454 return 0;
455}
456
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000457static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
458 struct rsnd_mod *mod)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800459{
460 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800461 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800462 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000463 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800464 u32 mask;
465 u32 val;
466 int shift;
467 int id = rsnd_mod_id(mod);
468 int ret;
469
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700470 /*
471 * SRC_TIMING_SELECT
472 */
473 shift = (id % 4) * 8;
474 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800475
476 /*
477 * ADG is used as source clock if SRC was used,
478 * then, SSI WS is used as destination clock.
479 * SSI WS is used as source clock if SRC is not used
480 * (when playback, source/destination become reverse when capture)
481 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800482 ret = 0;
483 if (convert_rate) {
484 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800485 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800486 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
487 runtime->rate,
488 convert_rate);
489 } else if (8 == id) {
490 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700491 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800492 } else {
493 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700494 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800495 }
496
497 if (ret < 0)
498 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700499
500 switch (id / 4) {
501 case 0:
502 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
503 break;
504 case 1:
505 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
506 break;
507 case 2:
508 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
509 break;
510 }
511
512 return 0;
513}
514
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000515static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
516 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700517{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000518 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800519 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700520
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000521 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800522 if (ret < 0)
523 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700524
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800525 /* Select SRC mode (fixed value) */
526 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800527
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800528 /* Set the restriction value of the FS ratio (98%) */
529 rsnd_mod_write(mod, SRC_MNFSR,
530 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700531
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000532 /* Gen1/Gen2 are not compatible */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000533 if (rsnd_src_convert_rate(io, src))
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000534 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
535
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800536 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800537
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700538 return 0;
539}
540
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800541static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000542 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000543 struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700544{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700545 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700546
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000547 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800548 if (ret < 0)
549 return ret;
550
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000551 ret = rsnd_src_set_route_gen1(io, mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700552 if (ret < 0)
553 return ret;
554
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000555 ret = rsnd_src_set_convert_rate_gen1(mod, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700556 if (ret < 0)
557 return ret;
558
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000559 ret = rsnd_src_set_convert_timing_gen1(io, mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800560 if (ret < 0)
561 return ret;
562
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800563 return 0;
564}
565
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800566static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000567 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000568 struct rsnd_priv *priv)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800569{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800570 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800571
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800572 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800573
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000574 return rsnd_src_start(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800575}
576
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800577static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000578 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000579 struct rsnd_priv *priv)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800580{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800581 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800582
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800583 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800584
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000585 return rsnd_src_stop(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800586}
587
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800588static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700589 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000590 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800591 .init = rsnd_src_init_gen1,
592 .quit = rsnd_src_quit,
593 .start = rsnd_src_start_gen1,
594 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000595 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700596};
597
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800598/*
599 * Gen2 functions
600 */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000601#define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
602#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
603static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
604{
605 struct rsnd_src *src = rsnd_mod_to_src(mod);
606 u32 sys_int_val, int_val, sys_int_mask;
607 int irq = src->info->irq;
608 int id = rsnd_mod_id(mod);
609
610 sys_int_val =
611 sys_int_mask = OUF_SRC(id);
612 int_val = 0x3300;
613
614 /*
615 * IRQ is not supported on non-DT
616 * see
617 * rsnd_src_probe_gen2()
618 */
619 if ((irq <= 0) || !enable) {
620 sys_int_val = 0;
621 int_val = 0;
622 }
623
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000624 /*
625 * WORKAROUND
626 *
627 * ignore over flow error when rsnd_enable_sync_convert()
628 */
629 if (rsnd_enable_sync_convert(src))
630 sys_int_val = sys_int_val & 0xffff;
631
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000632 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
633 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
634 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
635}
636
637static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
638{
639 u32 val = OUF_SRC(rsnd_mod_id(mod));
640
641 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
642 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
643}
644
645static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
646{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000647 struct rsnd_src *src = rsnd_mod_to_src(mod);
648 u32 val0, val1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000649 bool ret = false;
650
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000651 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
652
653 /*
654 * WORKAROUND
655 *
656 * ignore over flow error when rsnd_enable_sync_convert()
657 */
658 if (rsnd_enable_sync_convert(src))
659 val0 = val0 & 0xffff;
660
661 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
662 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000663 struct rsnd_src *src = rsnd_mod_to_src(mod);
664
665 src->err++;
666 ret = true;
667 }
668
669 /* clear error static */
670 rsnd_src_error_clear_gen2(mod);
671
672 return ret;
673}
674
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000675static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
676 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000677{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000678 struct rsnd_src *src = rsnd_mod_to_src(mod);
679 u32 val;
680
681 /*
682 * WORKAROUND
683 *
684 * Enable SRC output if you want to use sync convert together with DVC
685 */
686 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
687 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000688
689 rsnd_mod_write(mod, SRC_CTRL, val);
690
691 rsnd_src_error_clear_gen2(mod);
692
693 rsnd_src_start(mod);
694
695 rsnd_src_irq_enable_gen2(mod);
696
697 return 0;
698}
699
700static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
701{
702 rsnd_src_irq_disable_gen2(mod);
703
704 rsnd_mod_write(mod, SRC_CTRL, 0);
705
706 rsnd_src_error_record_gen2(mod);
707
708 return rsnd_src_stop(mod);
709}
710
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000711static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
712 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000713{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000714 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000715
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000716 spin_lock(&priv->lock);
717
718 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000719 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000720 goto rsnd_src_interrupt_gen2_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000721
722 if (rsnd_src_error_record_gen2(mod)) {
723 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000724 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000725 struct device *dev = rsnd_priv_to_dev(priv);
726
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000727 dev_dbg(dev, "%s[%d] restart\n",
728 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000729
730 _rsnd_src_stop_gen2(mod);
731 if (src->err < 1024)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000732 _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000733 else
734 dev_warn(dev, "no more SRC restart\n");
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000735 }
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000736
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000737rsnd_src_interrupt_gen2_out:
738 spin_unlock(&priv->lock);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000739}
740
741static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
742{
743 struct rsnd_mod *mod = data;
744
745 rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000746
747 return IRQ_HANDLED;
748}
749
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000750static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
751 struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800752{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700753 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
754 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700755 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
756 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000757 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000758 u32 cr, route;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700759 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800760 int ret;
761
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700762 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimotob167a572014-11-27 08:06:34 +0000763 if (!convert_rate)
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700764 ratio = 0;
Kuninori Morimotob167a572014-11-27 08:06:34 +0000765 else if (convert_rate > runtime->rate)
766 ratio = 100 * convert_rate / runtime->rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700767 else
Kuninori Morimotob167a572014-11-27 08:06:34 +0000768 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700769
770 if (ratio > 600) {
771 dev_err(dev, "FSO/FSI ratio error\n");
772 return -EINVAL;
773 }
774
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000775 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800776 if (ret < 0)
777 return ret;
778
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000779 cr = 0x00011110;
780 route = 0x0;
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000781 if (convert_rate) {
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000782 route = 0x1;
783
784 if (rsnd_enable_sync_convert(src)) {
785 cr |= 0x1;
786 route |= rsnd_io_is_play(io) ?
787 (0x1 << 24) : (0x1 << 25);
788 }
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000789 }
790
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000791 rsnd_mod_write(mod, SRC_SRCCR, cr);
792 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
793
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700794 switch (rsnd_mod_id(mod)) {
795 case 5:
796 case 6:
797 case 7:
798 case 8:
799 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
800 break;
801 default:
802 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
803 break;
804 }
805
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800806 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
807
808 return 0;
809}
810
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000811static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
812 struct rsnd_mod *mod)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800813{
814 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800815 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000816 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800817 int ret;
818
819 if (convert_rate)
Kuninori Morimotof708d942015-01-15 08:07:19 +0000820 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800821 runtime->rate,
822 convert_rate);
823 else
Kuninori Morimotof708d942015-01-15 08:07:19 +0000824 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800825
826 return ret;
827}
828
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800829static int rsnd_src_probe_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 Morimoto76c6fb52014-03-03 20:50:41 -0800832{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800833 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800834 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000835 int irq = src->info->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800836 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800837
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000838 if (irq > 0) {
839 /*
840 * IRQ is not supported on non-DT
841 * see
842 * rsnd_src_irq_enable_gen2()
843 */
844 ret = devm_request_irq(dev, irq,
845 rsnd_src_interrupt_gen2,
846 IRQF_SHARED,
847 dev_name(dev), mod);
848 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000849 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000850 }
851
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000852 ret = rsnd_dma_init(io,
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800853 rsnd_mod_to_dma(mod),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800854 src->info->dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700855
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800856 return ret;
857}
858
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800859static int rsnd_src_remove_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 Morimoto76c6fb52014-03-03 20:50:41 -0800862{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000863 rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800864
865 return 0;
866}
867
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800868static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000869 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000870 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800871{
872 int ret;
873
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000874 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800875 if (ret < 0)
876 return ret;
877
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000878 ret = rsnd_src_set_convert_rate_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800879 if (ret < 0)
880 return ret;
881
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000882 ret = rsnd_src_set_convert_timing_gen2(io, mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800883 if (ret < 0)
884 return ret;
885
886 return 0;
887}
888
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800889static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000890 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000891 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800892{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000893 rsnd_dma_start(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800894
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000895 return _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800896}
897
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800898static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000899 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000900 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800901{
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000902 int ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800903
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000904 ret = _rsnd_src_stop_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800905
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000906 rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800907
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000908 return ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800909}
910
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000911static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
912 struct rsnd_mod *mod)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000913{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000914 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
915 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000916 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000917 u32 fsrate;
918
919 if (!runtime)
920 return;
921
922 if (!convert_rate)
923 convert_rate = runtime->rate;
924
925 fsrate = 0x0400000 / convert_rate * runtime->rate;
926
927 /* update IFS */
928 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
929}
930
931static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000932 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000933 struct snd_soc_pcm_runtime *rtd)
934{
935 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000936 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
937 struct rsnd_src *src = rsnd_mod_to_src(mod);
938 int ret;
939
940 /*
941 * enable SRC sync convert if possible
942 */
943
944 /*
945 * Gen1 is not supported
946 */
947 if (rsnd_is_gen1(priv))
948 return 0;
949
950 /*
951 * SRC sync convert needs clock master
952 */
953 if (!rsnd_rdai_is_clk_master(rdai))
954 return 0;
955
956 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000957 * enable sync convert
958 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000959 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000960 rsnd_io_is_play(io) ?
961 "SRC Out Rate Switch" :
962 "SRC In Rate Switch",
963 rsnd_src_reconvert_update,
964 &src->sen, 1);
965 if (ret < 0)
966 return ret;
967
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000968 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000969 rsnd_io_is_play(io) ?
970 "SRC Out Rate" :
971 "SRC In Rate",
972 rsnd_src_reconvert_update,
973 &src->sync, 192000);
974
975 return ret;
976}
977
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800978static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700979 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000980 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800981 .probe = rsnd_src_probe_gen2,
982 .remove = rsnd_src_remove_gen2,
983 .init = rsnd_src_init_gen2,
984 .quit = rsnd_src_quit,
985 .start = rsnd_src_start_gen2,
986 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000987 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000988 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800989};
990
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800991struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700992{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800993 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100994 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700995
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800996 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700997}
998
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700999static void rsnd_of_parse_src(struct platform_device *pdev,
1000 const struct rsnd_of_data *of_data,
1001 struct rsnd_priv *priv)
1002{
1003 struct device_node *src_node;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001004 struct device_node *np;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001005 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1006 struct rsnd_src_platform_info *src_info;
1007 struct device *dev = &pdev->dev;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001008 int nr, i;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001009
1010 if (!of_data)
1011 return;
1012
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +00001013 src_node = rsnd_src_of_node(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001014 if (!src_node)
1015 return;
1016
1017 nr = of_get_child_count(src_node);
1018 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -07001019 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001020
1021 src_info = devm_kzalloc(dev,
1022 sizeof(struct rsnd_src_platform_info) * nr,
1023 GFP_KERNEL);
1024 if (!src_info) {
1025 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -07001026 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001027 }
1028
1029 info->src_info = src_info;
1030 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -07001031
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001032 i = 0;
1033 for_each_child_of_node(src_node, np) {
1034 src_info[i].irq = irq_of_parse_and_map(np, 0);
1035
1036 i++;
1037 }
1038
Kuninori Morimotof451e482014-05-22 23:24:59 -07001039rsnd_of_parse_src_end:
1040 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001041}
1042
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001043int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001044 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001045 struct rsnd_priv *priv)
1046{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -08001047 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001048 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001049 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -08001050 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -08001051 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001052 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001053 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001054
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001055 ops = NULL;
1056 if (rsnd_is_gen1(priv))
1057 ops = &rsnd_src_gen1_ops;
1058 if (rsnd_is_gen2(priv))
1059 ops = &rsnd_src_gen2_ops;
1060 if (!ops) {
1061 dev_err(dev, "unknown Generation\n");
1062 return -EIO;
1063 }
1064
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001065 rsnd_of_parse_src(pdev, of_data, priv);
1066
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001067 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001068 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001069 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001070 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -08001071 if (!nr)
1072 return 0;
1073
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001074 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
Kuninori Morimoto33363f72015-07-15 07:08:44 +00001075 if (!src)
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001076 return -ENOMEM;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001077
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001078 priv->src_nr = nr;
1079 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001080
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001081 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001082 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1083 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -08001084
1085 clk = devm_clk_get(dev, name);
1086 if (IS_ERR(clk))
1087 return PTR_ERR(clk);
1088
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001089 src->info = &info->src_info[i];
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001090
Kuninori Morimoto2099bc82015-06-15 06:24:54 +00001091 ret = rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001092 if (ret)
1093 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -07001094 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001095
1096 return 0;
1097}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001098
1099void rsnd_src_remove(struct platform_device *pdev,
1100 struct rsnd_priv *priv)
1101{
1102 struct rsnd_src *src;
1103 int i;
1104
1105 for_each_rsnd_src(src, priv, i) {
1106 rsnd_mod_quit(&src->mod);
1107 }
1108}