blob: 0b06ac8dbeec71bd003c31742a537bf074838b18 [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 Morimoto9b99e9a2015-06-15 06:26:25 +0000121static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
122 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000123{
124 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000125 int is_play = rsnd_io_is_play(io);
126
127 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
128 mod,
129 is_play ? "rx" : "tx");
130}
131
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700132int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000133 struct rsnd_dai_stream *io,
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700134 int use_busif)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800135{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000136 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700137 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800138 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800139
140 /*
141 * SSI_MODE0
142 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800143 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700144 !use_busif << ssi_id);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800145
146 /*
147 * SSI_MODE1
148 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800149 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800150 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800151 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800152 case 1:
153 shift = 0;
154 break;
155 case 2:
156 shift = 2;
157 break;
158 case 4:
159 shift = 16;
160 break;
161 }
162
163 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800164 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800165 0x3 << shift,
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000166 rsnd_rdai_is_clk_master(rdai) ?
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800167 0x2 << shift : 0x1 << shift);
168 }
169
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700170 /*
171 * DMA settings for SSIU
172 */
173 if (use_busif) {
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700174 u32 val = 0x76543210;
175 u32 mask = ~0;
176
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700177 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000178 rsnd_get_adinr(ssi_mod, io));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700179 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
180 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700181
182 mask <<= runtime->channels * 4;
183 val = val & mask;
184
185 switch (runtime->sample_bits) {
186 case 16:
187 val |= 0x67452301 & ~mask;
188 break;
189 case 32:
190 val |= 0x76543210 & ~mask;
191 break;
192 }
Kuninori Morimotocdde84d2015-07-15 07:09:27 +0000193 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700194
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700195 }
196
197 return 0;
198}
199
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000200int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
201 struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700202{
203 /*
204 * DMA settings for SSIU
205 */
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000206 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700207
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800208 return 0;
209}
210
Kuninori Morimotof708d942015-01-15 08:07:19 +0000211int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800212{
213 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
214
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000215 if (rsnd_is_gen1(priv))
216 return 0;
217
218 /* enable SSI interrupt if Gen2 */
Kuninori Morimotoefa991d2015-07-15 07:09:47 +0000219 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
220 rsnd_ssi_is_dma_mode(ssi_mod) ?
221 0x0e000000 : 0x0f000000);
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800222
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 */
Kuninori Morimotoefa991d2015-07-15 07:09:47 +0000234 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000235
236 return 0;
237}
238
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000239static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
240 struct rsnd_src *src)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000241{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000242 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243 u32 convert_rate;
244
245 if (!runtime)
246 return 0;
247
248 if (!rsnd_enable_sync_convert(src))
249 return src->convert_rate;
250
251 convert_rate = src->sync.val;
252
253 if (!convert_rate)
254 convert_rate = src->convert_rate;
255
256 if (!convert_rate)
257 convert_rate = runtime->rate;
258
259 return convert_rate;
260}
261
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800262unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800263 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800264 struct snd_pcm_runtime *runtime)
265{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700266 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800267 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700268 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800269
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700270 if (src_mod) {
271 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800272
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700273 /*
274 * return convert rate if SRC is used,
275 * otherwise, return runtime->rate as usual
276 */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000277 rate = rsnd_src_convert_rate(io, src);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700278 }
279
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800280 if (!rate)
281 rate = runtime->rate;
282
283 return rate;
284}
285
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000286static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
287 struct rsnd_dai_stream *io)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800288{
289 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800290 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000291 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800292 u32 fsrate = 0;
293
294 if (convert_rate)
295 fsrate = 0x0400000 / convert_rate * runtime->rate;
296
297 /* set/clear soft reset */
298 rsnd_mod_write(mod, SRC_SWRSR, 0);
299 rsnd_mod_write(mod, SRC_SWRSR, 1);
300
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800301 /* Set channel number and output bit length */
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000302 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod, io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800303
304 /* Enable the initial value of IFS */
305 if (fsrate) {
306 rsnd_mod_write(mod, SRC_IFSCR, 1);
307
308 /* Set initial value of IFS */
309 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
310 }
311
312 /* use DMA transfer */
313 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
314
315 return 0;
316}
317
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000318static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000319 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000320 struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *fe_params)
322{
323 struct rsnd_src *src = rsnd_mod_to_src(mod);
324 struct snd_soc_pcm_runtime *fe = substream->private_data;
325
326 /* default value (mainly for non-DT) */
327 src->convert_rate = src->info->convert_rate;
328
329 /*
330 * SRC assumes that it is used under DPCM if user want to use
331 * sampling rate convert. Then, SRC should be FE.
332 * And then, this function will be called *after* BE settings.
333 * this means, each BE already has fixuped hw_params.
334 * see
335 * dpcm_fe_dai_hw_params()
336 * dpcm_be_dai_hw_params()
337 */
338 if (fe->dai_link->dynamic) {
339 int stream = substream->stream;
340 struct snd_soc_dpcm *dpcm;
341 struct snd_pcm_hw_params *be_params;
342
343 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
344 be_params = &dpcm->hw_params;
345
346 if (params_rate(fe_params) != params_rate(be_params))
347 src->convert_rate = params_rate(be_params);
348 }
349 }
350
351 return 0;
352}
353
354static int rsnd_src_init(struct rsnd_mod *mod,
355 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800356{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800357 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800358
Kuninori Morimoto85642952015-01-15 08:03:22 +0000359 rsnd_mod_hw_start(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800360
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000361 src->err = 0;
362
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000363 /* reset sync convert_rate */
364 src->sync.val = 0;
365
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000366 /*
367 * Initialize the operation of the SRC internal circuits
368 * see rsnd_src_start()
369 */
370 rsnd_mod_write(mod, SRC_SRCIR, 1);
371
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800372 return 0;
373}
374
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800375static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000376 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000377 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800378{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800379 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000380 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800381
Kuninori Morimoto85642952015-01-15 08:03:22 +0000382 rsnd_mod_hw_stop(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800383
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000384 if (src->err)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000385 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
386 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000387
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000388 src->convert_rate = 0;
389
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000390 /* reset sync convert_rate */
391 src->sync.val = 0;
392
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800393 return 0;
394}
395
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000396static int rsnd_src_start(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800397{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800398 /*
399 * Cancel the initialization and operate the SRC function
Kuninori Morimoto603cefa2014-11-27 08:06:43 +0000400 * see rsnd_src_init()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800401 */
402 rsnd_mod_write(mod, SRC_SRCIR, 0);
403
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800404 return 0;
405}
406
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000407static int rsnd_src_stop(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800408{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000409 /* nothing to do */
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800410 return 0;
411}
412
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800413/*
414 * Gen1 functions
415 */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000416static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
417 struct rsnd_mod *mod)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700418{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800419 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700420 u32 mask;
421 int shift;
422 } routes[] = {
423 { 0xF, 0, }, /* 0 */
424 { 0xF, 4, }, /* 1 */
425 { 0xF, 8, }, /* 2 */
426 { 0x7, 12, }, /* 3 */
427 { 0x7, 16, }, /* 4 */
428 { 0x7, 20, }, /* 5 */
429 { 0x7, 24, }, /* 6 */
430 { 0x3, 28, }, /* 7 */
431 { 0x3, 30, }, /* 8 */
432 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700433 u32 mask;
434 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700435 int id;
436
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700437 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300438 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700439 return -EIO;
440
441 /*
442 * SRC_ROUTE_SELECT
443 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000444 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700445 val = val << routes[id].shift;
446 mask = routes[id].mask << routes[id].shift;
447
448 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
449
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800450 return 0;
451}
452
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000453static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
454 struct rsnd_mod *mod)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800455{
456 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800457 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800458 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000459 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800460 u32 mask;
461 u32 val;
462 int shift;
463 int id = rsnd_mod_id(mod);
464 int ret;
465
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700466 /*
467 * SRC_TIMING_SELECT
468 */
469 shift = (id % 4) * 8;
470 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800471
472 /*
473 * ADG is used as source clock if SRC was used,
474 * then, SSI WS is used as destination clock.
475 * SSI WS is used as source clock if SRC is not used
476 * (when playback, source/destination become reverse when capture)
477 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800478 ret = 0;
479 if (convert_rate) {
480 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800481 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800482 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
483 runtime->rate,
484 convert_rate);
485 } else if (8 == id) {
486 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700487 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800488 } else {
489 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700490 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800491 }
492
493 if (ret < 0)
494 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700495
496 switch (id / 4) {
497 case 0:
498 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
499 break;
500 case 1:
501 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
502 break;
503 case 2:
504 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
505 break;
506 }
507
508 return 0;
509}
510
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000511static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
512 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700513{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000514 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800515 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700516
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000517 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800518 if (ret < 0)
519 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700520
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800521 /* Select SRC mode (fixed value) */
522 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800523
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800524 /* Set the restriction value of the FS ratio (98%) */
525 rsnd_mod_write(mod, SRC_MNFSR,
526 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700527
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000528 /* Gen1/Gen2 are not compatible */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000529 if (rsnd_src_convert_rate(io, src))
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000530 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
531
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800532 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800533
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700534 return 0;
535}
536
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800537static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000538 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000539 struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700540{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700541 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700542
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000543 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800544 if (ret < 0)
545 return ret;
546
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000547 ret = rsnd_src_set_route_gen1(io, mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700548 if (ret < 0)
549 return ret;
550
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000551 ret = rsnd_src_set_convert_rate_gen1(mod, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700552 if (ret < 0)
553 return ret;
554
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000555 ret = rsnd_src_set_convert_timing_gen1(io, mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800556 if (ret < 0)
557 return ret;
558
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800559 return 0;
560}
561
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800562static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000563 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000564 struct rsnd_priv *priv)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800565{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800566 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800567
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800568 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800569
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000570 return rsnd_src_start(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800571}
572
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800573static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000574 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000575 struct rsnd_priv *priv)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800576{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800577 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800578
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800579 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800580
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000581 return rsnd_src_stop(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800582}
583
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800584static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700585 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000586 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800587 .init = rsnd_src_init_gen1,
588 .quit = rsnd_src_quit,
589 .start = rsnd_src_start_gen1,
590 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000591 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700592};
593
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800594/*
595 * Gen2 functions
596 */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000597#define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
598#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
599static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
600{
601 struct rsnd_src *src = rsnd_mod_to_src(mod);
602 u32 sys_int_val, int_val, sys_int_mask;
603 int irq = src->info->irq;
604 int id = rsnd_mod_id(mod);
605
606 sys_int_val =
607 sys_int_mask = OUF_SRC(id);
608 int_val = 0x3300;
609
610 /*
611 * IRQ is not supported on non-DT
612 * see
613 * rsnd_src_probe_gen2()
614 */
615 if ((irq <= 0) || !enable) {
616 sys_int_val = 0;
617 int_val = 0;
618 }
619
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000620 /*
621 * WORKAROUND
622 *
623 * ignore over flow error when rsnd_enable_sync_convert()
624 */
625 if (rsnd_enable_sync_convert(src))
626 sys_int_val = sys_int_val & 0xffff;
627
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000628 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
629 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
630 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
631}
632
633static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
634{
635 u32 val = OUF_SRC(rsnd_mod_id(mod));
636
637 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
638 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
639}
640
641static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
642{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000643 struct rsnd_src *src = rsnd_mod_to_src(mod);
644 u32 val0, val1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000645 bool ret = false;
646
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000647 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
648
649 /*
650 * WORKAROUND
651 *
652 * ignore over flow error when rsnd_enable_sync_convert()
653 */
654 if (rsnd_enable_sync_convert(src))
655 val0 = val0 & 0xffff;
656
657 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
658 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000659 struct rsnd_src *src = rsnd_mod_to_src(mod);
660
661 src->err++;
662 ret = true;
663 }
664
665 /* clear error static */
666 rsnd_src_error_clear_gen2(mod);
667
668 return ret;
669}
670
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000671static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
672 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000673{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000674 struct rsnd_src *src = rsnd_mod_to_src(mod);
675 u32 val;
676
677 /*
678 * WORKAROUND
679 *
680 * Enable SRC output if you want to use sync convert together with DVC
681 */
682 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
683 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000684
685 rsnd_mod_write(mod, SRC_CTRL, val);
686
687 rsnd_src_error_clear_gen2(mod);
688
689 rsnd_src_start(mod);
690
691 rsnd_src_irq_enable_gen2(mod);
692
693 return 0;
694}
695
696static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
697{
698 rsnd_src_irq_disable_gen2(mod);
699
700 rsnd_mod_write(mod, SRC_CTRL, 0);
701
702 rsnd_src_error_record_gen2(mod);
703
704 return rsnd_src_stop(mod);
705}
706
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000707static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
708 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000709{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000710 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000711
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000712 spin_lock(&priv->lock);
713
714 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000715 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000716 goto rsnd_src_interrupt_gen2_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000717
718 if (rsnd_src_error_record_gen2(mod)) {
719 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000720 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000721 struct device *dev = rsnd_priv_to_dev(priv);
722
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000723 dev_dbg(dev, "%s[%d] restart\n",
724 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000725
726 _rsnd_src_stop_gen2(mod);
727 if (src->err < 1024)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000728 _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000729 else
730 dev_warn(dev, "no more SRC restart\n");
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000731 }
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000732
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000733rsnd_src_interrupt_gen2_out:
734 spin_unlock(&priv->lock);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000735}
736
737static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
738{
739 struct rsnd_mod *mod = data;
740
741 rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000742
743 return IRQ_HANDLED;
744}
745
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000746static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
747 struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800748{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700749 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
750 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700751 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
752 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000753 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000754 u32 cr, route;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700755 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800756 int ret;
757
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700758 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimotob167a572014-11-27 08:06:34 +0000759 if (!convert_rate)
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700760 ratio = 0;
Kuninori Morimotob167a572014-11-27 08:06:34 +0000761 else if (convert_rate > runtime->rate)
762 ratio = 100 * convert_rate / runtime->rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700763 else
Kuninori Morimotob167a572014-11-27 08:06:34 +0000764 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700765
766 if (ratio > 600) {
767 dev_err(dev, "FSO/FSI ratio error\n");
768 return -EINVAL;
769 }
770
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000771 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800772 if (ret < 0)
773 return ret;
774
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000775 cr = 0x00011110;
776 route = 0x0;
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000777 if (convert_rate) {
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000778 route = 0x1;
779
780 if (rsnd_enable_sync_convert(src)) {
781 cr |= 0x1;
782 route |= rsnd_io_is_play(io) ?
783 (0x1 << 24) : (0x1 << 25);
784 }
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000785 }
786
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000787 rsnd_mod_write(mod, SRC_SRCCR, cr);
788 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
789
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700790 switch (rsnd_mod_id(mod)) {
791 case 5:
792 case 6:
793 case 7:
794 case 8:
795 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
796 break;
797 default:
798 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
799 break;
800 }
801
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800802 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
803
804 return 0;
805}
806
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000807static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
808 struct rsnd_mod *mod)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800809{
810 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800811 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000812 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800813 int ret;
814
815 if (convert_rate)
Kuninori Morimotof708d942015-01-15 08:07:19 +0000816 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800817 runtime->rate,
818 convert_rate);
819 else
Kuninori Morimotof708d942015-01-15 08:07:19 +0000820 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800821
822 return ret;
823}
824
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800825static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000826 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000827 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800828{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800829 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800830 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000831 int irq = src->info->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800832 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800833
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000834 if (irq > 0) {
835 /*
836 * IRQ is not supported on non-DT
837 * see
838 * rsnd_src_irq_enable_gen2()
839 */
840 ret = devm_request_irq(dev, irq,
841 rsnd_src_interrupt_gen2,
842 IRQF_SHARED,
843 dev_name(dev), mod);
844 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000845 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000846 }
847
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000848 ret = rsnd_dma_init(io,
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800849 rsnd_mod_to_dma(mod),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800850 src->info->dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700851
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800852 return ret;
853}
854
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800855static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000856 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000857 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800858{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000859 rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800860
861 return 0;
862}
863
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800864static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000865 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000866 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800867{
868 int ret;
869
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000870 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800871 if (ret < 0)
872 return ret;
873
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000874 ret = rsnd_src_set_convert_rate_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800875 if (ret < 0)
876 return ret;
877
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000878 ret = rsnd_src_set_convert_timing_gen2(io, mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800879 if (ret < 0)
880 return ret;
881
882 return 0;
883}
884
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800885static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000886 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000887 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800888{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000889 rsnd_dma_start(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800890
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000891 return _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800892}
893
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800894static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000895 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000896 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800897{
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000898 int ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800899
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000900 ret = _rsnd_src_stop_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800901
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000902 rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800903
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000904 return ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800905}
906
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000907static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
908 struct rsnd_mod *mod)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000909{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000910 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
911 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000912 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000913 u32 fsrate;
914
915 if (!runtime)
916 return;
917
918 if (!convert_rate)
919 convert_rate = runtime->rate;
920
921 fsrate = 0x0400000 / convert_rate * runtime->rate;
922
923 /* update IFS */
924 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
925}
926
927static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000928 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000929 struct snd_soc_pcm_runtime *rtd)
930{
931 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000932 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
933 struct rsnd_src *src = rsnd_mod_to_src(mod);
934 int ret;
935
936 /*
937 * enable SRC sync convert if possible
938 */
939
940 /*
941 * Gen1 is not supported
942 */
943 if (rsnd_is_gen1(priv))
944 return 0;
945
946 /*
947 * SRC sync convert needs clock master
948 */
949 if (!rsnd_rdai_is_clk_master(rdai))
950 return 0;
951
952 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000953 * enable sync convert
954 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000955 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000956 rsnd_io_is_play(io) ?
957 "SRC Out Rate Switch" :
958 "SRC In Rate Switch",
959 rsnd_src_reconvert_update,
960 &src->sen, 1);
961 if (ret < 0)
962 return ret;
963
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000964 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000965 rsnd_io_is_play(io) ?
966 "SRC Out Rate" :
967 "SRC In Rate",
968 rsnd_src_reconvert_update,
969 &src->sync, 192000);
970
971 return ret;
972}
973
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800974static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700975 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000976 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800977 .probe = rsnd_src_probe_gen2,
978 .remove = rsnd_src_remove_gen2,
979 .init = rsnd_src_init_gen2,
980 .quit = rsnd_src_quit,
981 .start = rsnd_src_start_gen2,
982 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000983 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000984 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800985};
986
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800987struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700988{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800989 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100990 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700991
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800992 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700993}
994
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700995static void rsnd_of_parse_src(struct platform_device *pdev,
996 const struct rsnd_of_data *of_data,
997 struct rsnd_priv *priv)
998{
999 struct device_node *src_node;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001000 struct device_node *np;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001001 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1002 struct rsnd_src_platform_info *src_info;
1003 struct device *dev = &pdev->dev;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001004 int nr, i;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001005
1006 if (!of_data)
1007 return;
1008
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +00001009 src_node = rsnd_src_of_node(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001010 if (!src_node)
1011 return;
1012
1013 nr = of_get_child_count(src_node);
1014 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -07001015 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001016
1017 src_info = devm_kzalloc(dev,
1018 sizeof(struct rsnd_src_platform_info) * nr,
1019 GFP_KERNEL);
1020 if (!src_info) {
1021 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -07001022 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001023 }
1024
1025 info->src_info = src_info;
1026 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -07001027
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001028 i = 0;
1029 for_each_child_of_node(src_node, np) {
1030 src_info[i].irq = irq_of_parse_and_map(np, 0);
1031
1032 i++;
1033 }
1034
Kuninori Morimotof451e482014-05-22 23:24:59 -07001035rsnd_of_parse_src_end:
1036 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001037}
1038
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001039int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001040 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001041 struct rsnd_priv *priv)
1042{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -08001043 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001044 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001045 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -08001046 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -08001047 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001048 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001049 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001050
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001051 ops = NULL;
1052 if (rsnd_is_gen1(priv))
1053 ops = &rsnd_src_gen1_ops;
1054 if (rsnd_is_gen2(priv))
1055 ops = &rsnd_src_gen2_ops;
1056 if (!ops) {
1057 dev_err(dev, "unknown Generation\n");
1058 return -EIO;
1059 }
1060
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001061 rsnd_of_parse_src(pdev, of_data, priv);
1062
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001063 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001064 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001065 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001066 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -08001067 if (!nr)
1068 return 0;
1069
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001070 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
Kuninori Morimoto33363f72015-07-15 07:08:44 +00001071 if (!src)
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001072 return -ENOMEM;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001073
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001074 priv->src_nr = nr;
1075 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001076
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001077 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001078 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1079 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -08001080
1081 clk = devm_clk_get(dev, name);
1082 if (IS_ERR(clk))
1083 return PTR_ERR(clk);
1084
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001085 src->info = &info->src_info[i];
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001086
Kuninori Morimoto2099bc82015-06-15 06:24:54 +00001087 ret = rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001088 if (ret)
1089 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -07001090 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001091
1092 return 0;
1093}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001094
1095void rsnd_src_remove(struct platform_device *pdev,
1096 struct rsnd_priv *priv)
1097{
1098 struct rsnd_src *src;
1099 int i;
1100
1101 for_each_rsnd_src(src, priv, i) {
1102 rsnd_mod_quit(&src->mod);
1103 }
1104}