blob: d520690409a2944bf0491637196655c41a817fa3 [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 {
Kuninori Morimoto07539c12013-07-21 21:36:35 -070023 struct rsnd_mod mod;
Kuninori Morimoto940e9472015-10-26 08:42:25 +000024 struct rsnd_mod *dma;
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 Morimotoadf6a682015-11-10 05:11:55 +000028 int irq;
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 Morimotoadf6a682015-11-10 05:11:55 +000033#define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +000034#define rsnd_src_to_dma(src) ((src)->dma)
Kuninori Morimotoda599fd2015-07-15 07:11:21 +000035#define rsnd_src_nr(priv) ((priv)->src_nr)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000036#define rsnd_enable_sync_convert(src) ((src)->sen.val)
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +000037
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
Kuninori Morimoto98efeea2015-11-30 08:50:32 +000072static void rsnd_src_activation(struct rsnd_mod *mod)
Kuninori Morimoto379febf2015-07-15 07:12:18 +000073{
74 rsnd_mod_write(mod, SRC_SWRSR, 0);
75 rsnd_mod_write(mod, SRC_SWRSR, 1);
76}
77
Kuninori Morimoto475a3612015-11-30 08:51:35 +000078static void rsnd_src_halt(struct rsnd_mod *mod)
79{
80 rsnd_mod_write(mod, SRC_SRCIR, 1);
81 rsnd_mod_write(mod, SRC_SWRSR, 0);
82}
83
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +000084static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
85 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +000086{
87 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +000088 int is_play = rsnd_io_is_play(io);
89
90 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
91 mod,
92 is_play ? "rx" : "tx");
93}
94
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +000095static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +000096 struct rsnd_mod *mod)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000097{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000098 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +000099 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000100 u32 convert_rate;
101
102 if (!runtime)
103 return 0;
104
105 if (!rsnd_enable_sync_convert(src))
106 return src->convert_rate;
107
108 convert_rate = src->sync.val;
109
110 if (!convert_rate)
111 convert_rate = src->convert_rate;
112
113 if (!convert_rate)
114 convert_rate = runtime->rate;
115
116 return convert_rate;
117}
118
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800119unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800120 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800121 struct snd_pcm_runtime *runtime)
122{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700124 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800125
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +0000126 /*
127 * return convert rate if SRC is used,
128 * otherwise, return runtime->rate as usual
129 */
130 if (src_mod)
131 rate = rsnd_src_convert_rate(io, src_mod);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700132
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800133 if (!rate)
134 rate = runtime->rate;
135
136 return rate;
137}
138
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000139static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000140 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000141 struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *fe_params)
143{
144 struct rsnd_src *src = rsnd_mod_to_src(mod);
145 struct snd_soc_pcm_runtime *fe = substream->private_data;
146
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000147 /*
148 * SRC assumes that it is used under DPCM if user want to use
149 * sampling rate convert. Then, SRC should be FE.
150 * And then, this function will be called *after* BE settings.
151 * this means, each BE already has fixuped hw_params.
152 * see
153 * dpcm_fe_dai_hw_params()
154 * dpcm_be_dai_hw_params()
155 */
156 if (fe->dai_link->dynamic) {
157 int stream = substream->stream;
158 struct snd_soc_dpcm *dpcm;
159 struct snd_pcm_hw_params *be_params;
160
161 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
162 be_params = &dpcm->hw_params;
163
164 if (params_rate(fe_params) != params_rate(be_params))
165 src->convert_rate = params_rate(be_params);
166 }
167 }
168
169 return 0;
170}
171
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000172static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
173 struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800174{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000175 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000176 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000177 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
178 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +0000179 u32 convert_rate = rsnd_src_convert_rate(io, mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000180 u32 ifscr, fsrate, adinr;
181 u32 cr, route;
182 u32 bsdsr, bsisr;
183 uint ratio;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800184
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000185 if (!runtime)
186 return;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800187
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000188 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
189 if (!convert_rate)
190 ratio = 0;
191 else if (convert_rate > runtime->rate)
192 ratio = 100 * convert_rate / runtime->rate;
193 else
194 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000195
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000196 if (ratio > 600) {
197 dev_err(dev, "FSO/FSI ratio error\n");
198 return;
199 }
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000200
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000201 /*
202 * SRC_ADINR
203 */
Kuninori Morimotoc45f7262015-11-30 08:49:33 +0000204 adinr = rsnd_get_adinr_bit(mod, io) |
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000205 rsnd_runtime_channel_original(io);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000206
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000207 /*
208 * SRC_IFSCR / SRC_IFSVR
209 */
210 ifscr = 0;
211 fsrate = 0;
212 if (convert_rate) {
213 ifscr = 1;
214 fsrate = 0x0400000 / convert_rate * runtime->rate;
215 }
216
217 /*
218 * SRC_SRCCR / SRC_ROUTE_MODE0
219 */
220 cr = 0x00011110;
221 route = 0x0;
222 if (convert_rate) {
223 route = 0x1;
224
225 if (rsnd_enable_sync_convert(src)) {
226 cr |= 0x1;
227 route |= rsnd_io_is_play(io) ?
228 (0x1 << 24) : (0x1 << 25);
229 }
230 }
231
232 /*
233 * SRC_BSDSR / SRC_BSISR
234 */
235 switch (rsnd_mod_id(mod)) {
236 case 5:
237 case 6:
238 case 7:
239 case 8:
240 bsdsr = 0x02400000; /* 6 - 1/6 */
241 bsisr = 0x00100060; /* 6 - 1/6 */
242 break;
243 default:
244 bsdsr = 0x01800000; /* 6 - 1/6 */
245 bsisr = 0x00100060 ;/* 6 - 1/6 */
246 break;
247 }
248
Kuninori Morimoto0fbab952016-02-18 08:13:45 +0000249 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
250
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000251 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
252 rsnd_mod_write(mod, SRC_ADINR, adinr);
253 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
254 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
255 rsnd_mod_write(mod, SRC_SRCCR, cr);
256 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
257 rsnd_mod_write(mod, SRC_BSISR, bsisr);
258 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
259
Kuninori Morimotobf4e8d72015-11-30 08:50:08 +0000260 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
261 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000262 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
263
264 if (convert_rate)
265 rsnd_adg_set_convert_clk_gen2(mod, io,
266 runtime->rate,
267 convert_rate);
268 else
269 rsnd_adg_set_convert_timing_gen2(mod, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800270}
271
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000272static int rsnd_src_irq(struct rsnd_mod *mod,
273 struct rsnd_dai_stream *io,
274 struct rsnd_priv *priv,
275 int enable)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000276{
277 struct rsnd_src *src = rsnd_mod_to_src(mod);
278 u32 sys_int_val, int_val, sys_int_mask;
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000279 int irq = src->irq;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000280 int id = rsnd_mod_id(mod);
281
282 sys_int_val =
283 sys_int_mask = OUF_SRC(id);
284 int_val = 0x3300;
285
286 /*
287 * IRQ is not supported on non-DT
288 * see
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000289 * rsnd_src_probe_()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000290 */
291 if ((irq <= 0) || !enable) {
292 sys_int_val = 0;
293 int_val = 0;
294 }
295
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000296 /*
297 * WORKAROUND
298 *
299 * ignore over flow error when rsnd_enable_sync_convert()
300 */
301 if (rsnd_enable_sync_convert(src))
302 sys_int_val = sys_int_val & 0xffff;
303
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000304 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
305 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
306 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000307
308 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000309}
310
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000311static void rsnd_src_status_clear(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000312{
313 u32 val = OUF_SRC(rsnd_mod_id(mod));
314
315 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
316 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
317}
318
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000319static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000320{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000321 struct rsnd_src *src = rsnd_mod_to_src(mod);
322 u32 val0, val1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000323 bool ret = false;
324
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000325 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
326
327 /*
328 * WORKAROUND
329 *
330 * ignore over flow error when rsnd_enable_sync_convert()
331 */
332 if (rsnd_enable_sync_convert(src))
333 val0 = val0 & 0xffff;
334
335 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000336 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000337 ret = true;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000338
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000339 return ret;
340}
341
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000342static int rsnd_src_start(struct rsnd_mod *mod,
343 struct rsnd_dai_stream *io,
344 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000345{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000346 struct rsnd_src *src = rsnd_mod_to_src(mod);
347 u32 val;
348
349 /*
350 * WORKAROUND
351 *
352 * Enable SRC output if you want to use sync convert together with DVC
353 */
354 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
355 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000356
357 rsnd_mod_write(mod, SRC_CTRL, val);
358
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000359 return 0;
360}
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000361
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000362static int rsnd_src_stop(struct rsnd_mod *mod,
363 struct rsnd_dai_stream *io,
364 struct rsnd_priv *priv)
365{
Kuninori Morimoto31739a62016-01-21 02:00:13 +0000366 rsnd_mod_write(mod, SRC_CTRL, 0);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000367
368 return 0;
369}
370
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000371static int rsnd_src_init(struct rsnd_mod *mod,
372 struct rsnd_dai_stream *io,
373 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000374{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000375 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000376
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000377 rsnd_mod_power_on(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000378
Kuninori Morimoto98efeea2015-11-30 08:50:32 +0000379 rsnd_src_activation(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000380
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000381 rsnd_src_set_convert_rate(io, mod);
382
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000383 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000384
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000385 /* reset sync convert_rate */
386 src->sync.val = 0;
387
388 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000389}
390
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000391static int rsnd_src_quit(struct rsnd_mod *mod,
392 struct rsnd_dai_stream *io,
393 struct rsnd_priv *priv)
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000394{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000395 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000396
Kuninori Morimoto475a3612015-11-30 08:51:35 +0000397 rsnd_src_halt(mod);
398
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000399 rsnd_mod_power_off(mod);
400
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000401 src->convert_rate = 0;
402
403 /* reset sync convert_rate */
404 src->sync.val = 0;
405
406 return 0;
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000407}
408
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000409static void __rsnd_src_interrupt(struct rsnd_mod *mod,
410 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000411{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000412 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000413 bool stop = false;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000414
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000415 spin_lock(&priv->lock);
416
417 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000418 if (!rsnd_io_is_working(io))
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000419 goto rsnd_src_interrupt_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000420
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000421 if (rsnd_src_error_occurred(mod))
422 stop = true;
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000423
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000424 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000425rsnd_src_interrupt_out:
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000426
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000427 spin_unlock(&priv->lock);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000428
429 if (stop)
430 snd_pcm_stop_xrun(io->substream);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000431}
432
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000433static irqreturn_t rsnd_src_interrupt(int irq, void *data)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000434{
435 struct rsnd_mod *mod = data;
436
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000437 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000438
439 return IRQ_HANDLED;
440}
441
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000442static int rsnd_src_probe_(struct rsnd_mod *mod,
443 struct rsnd_dai_stream *io,
444 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800445{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800446 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800447 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000448 int irq = src->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800449 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800450
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000451 if (irq > 0) {
452 /*
453 * IRQ is not supported on non-DT
454 * see
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000455 * rsnd_src_irq()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000456 */
457 ret = devm_request_irq(dev, irq,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000458 rsnd_src_interrupt,
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000459 IRQF_SHARED,
460 dev_name(dev), mod);
461 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000462 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000463 }
464
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000465 ret = rsnd_dma_attach(io, mod, &src->dma, 0);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700466
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800467 return ret;
468}
469
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000470static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000471 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000472 struct snd_soc_pcm_runtime *rtd)
473{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000474 struct rsnd_src *src = rsnd_mod_to_src(mod);
475 int ret;
476
477 /*
478 * enable SRC sync convert if possible
479 */
480
481 /*
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000482 * It can't use SRC Synchronous convert
483 * when Capture if it uses CMD
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000484 */
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000485 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000486 return 0;
487
488 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000489 * enable sync convert
490 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000491 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000492 rsnd_io_is_play(io) ?
493 "SRC Out Rate Switch" :
494 "SRC In Rate Switch",
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000495 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000496 &src->sen, 1);
497 if (ret < 0)
498 return ret;
499
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000500 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000501 rsnd_io_is_play(io) ?
502 "SRC Out Rate" :
503 "SRC In Rate",
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000504 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000505 &src->sync, 192000);
506
507 return ret;
508}
509
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000510static struct rsnd_mod_ops rsnd_src_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700511 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000512 .dma_req = rsnd_src_dma_req,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000513 .probe = rsnd_src_probe_,
514 .init = rsnd_src_init,
515 .quit = rsnd_src_quit,
516 .start = rsnd_src_start,
517 .stop = rsnd_src_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000518 .irq = rsnd_src_irq,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000519 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000520 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800521};
522
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800523struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700524{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800525 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100526 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700527
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000528 return rsnd_mod_get(rsnd_src_get(priv, id));
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700529}
530
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000531int rsnd_src_probe(struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700532{
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000533 struct device_node *node;
534 struct device_node *np;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700535 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800536 struct rsnd_src *src;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800537 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800538 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000539 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700540
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000541 /* This driver doesn't support Gen1 at this point */
542 if (rsnd_is_gen1(priv))
543 return 0;
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700544
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000545 node = rsnd_src_of_node(priv);
546 if (!node)
547 return 0; /* not used is not error */
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700548
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000549 nr = of_get_child_count(node);
550 if (!nr) {
551 ret = -EINVAL;
552 goto rsnd_src_probe_done;
553 }
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800554
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800555 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000556 if (!src) {
557 ret = -ENOMEM;
558 goto rsnd_src_probe_done;
559 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700560
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800561 priv->src_nr = nr;
562 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700563
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000564 i = 0;
565 for_each_child_of_node(node, np) {
566 src = rsnd_src_get(priv, i);
567
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700568 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
569 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800570
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000571 src->irq = irq_of_parse_and_map(np, 0);
572 if (!src->irq) {
573 ret = -EINVAL;
574 goto rsnd_src_probe_done;
575 }
Kuninori Morimotoef749402013-12-19 19:28:51 -0800576
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000577 clk = devm_clk_get(dev, name);
578 if (IS_ERR(clk)) {
579 ret = PTR_ERR(clk);
580 goto rsnd_src_probe_done;
581 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700582
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000583 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000584 &rsnd_src_ops, clk, rsnd_mod_get_status,
585 RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000586 if (ret)
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000587 goto rsnd_src_probe_done;
588
589 i++;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700590 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700591
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000592 ret = 0;
593
594rsnd_src_probe_done:
595 of_node_put(node);
596
597 return ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700598}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000599
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000600void rsnd_src_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000601{
602 struct rsnd_src *src;
603 int i;
604
605 for_each_rsnd_src(src, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000606 rsnd_mod_quit(rsnd_mod_get(src));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000607 }
608}