blob: 76a477a3ccb5d88e18fd8398d9ad2b2616a99f48 [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 Morimotoab2049f2016-03-07 05:07:57 +000036#define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->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
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000105 if (!rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000106 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 Morimotocbf14942016-03-07 05:08:33 +0000119unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
120 struct rsnd_dai_stream *io,
121 int is_in)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800122{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000124 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700125 unsigned int rate = 0;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000126 int is_play = rsnd_io_is_play(io);
127
128 /*
129 *
130 * Playback
131 * runtime_rate -> [SRC] -> convert_rate
132 *
133 * Capture
134 * convert_rate -> [SRC] -> runtime_rate
135 */
136
137 if (is_play == is_in)
138 return runtime->rate;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800139
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +0000140 /*
141 * return convert rate if SRC is used,
142 * otherwise, return runtime->rate as usual
143 */
144 if (src_mod)
145 rate = rsnd_src_convert_rate(io, src_mod);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700146
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800147 if (!rate)
148 rate = runtime->rate;
149
150 return rate;
151}
152
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000153static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac192015-06-15 06:25:20 +0000154 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000155 struct snd_pcm_substream *substream,
156 struct snd_pcm_hw_params *fe_params)
157{
158 struct rsnd_src *src = rsnd_mod_to_src(mod);
159 struct snd_soc_pcm_runtime *fe = substream->private_data;
160
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000161 /*
162 * SRC assumes that it is used under DPCM if user want to use
163 * sampling rate convert. Then, SRC should be FE.
164 * And then, this function will be called *after* BE settings.
165 * this means, each BE already has fixuped hw_params.
166 * see
167 * dpcm_fe_dai_hw_params()
168 * dpcm_be_dai_hw_params()
169 */
Kuninori Morimotofc99d232017-04-06 07:24:36 +0000170 src->convert_rate = 0;
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000171 if (fe->dai_link->dynamic) {
172 int stream = substream->stream;
173 struct snd_soc_dpcm *dpcm;
174 struct snd_pcm_hw_params *be_params;
175
176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
177 be_params = &dpcm->hw_params;
178
179 if (params_rate(fe_params) != params_rate(be_params))
180 src->convert_rate = params_rate(be_params);
181 }
182 }
183
184 return 0;
185}
186
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000187static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
188 struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800189{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000190 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000191 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000192 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000193 int is_play = rsnd_io_is_play(io);
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000194 int use_src = 0;
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000195 u32 fin, fout;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000196 u32 ifscr, fsrate, adinr;
197 u32 cr, route;
198 u32 bsdsr, bsisr;
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000199 u32 i_busif, o_busif, tmp;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000200 uint ratio;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800201
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000202 if (!runtime)
203 return;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800204
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000205 fin = rsnd_src_get_in_rate(priv, io);
206 fout = rsnd_src_get_out_rate(priv, io);
207
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000208 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000209 if (fin == fout)
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000210 ratio = 0;
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000211 else if (fin > fout)
212 ratio = 100 * fin / fout;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000213 else
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000214 ratio = 100 * fout / fin;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000215
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000216 if (ratio > 600) {
217 dev_err(dev, "FSO/FSI ratio error\n");
218 return;
219 }
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000220
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000221 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
222
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000223 /*
224 * SRC_ADINR
225 */
Kuninori Morimotoc45f7262015-11-30 08:49:33 +0000226 adinr = rsnd_get_adinr_bit(mod, io) |
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000227 rsnd_runtime_channel_original(io);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000228
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000229 /*
230 * SRC_IFSCR / SRC_IFSVR
231 */
232 ifscr = 0;
233 fsrate = 0;
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000234 if (use_src) {
Hiroyuki Yokoyama93ca33c2016-07-25 01:52:43 +0000235 u64 n;
236
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000237 ifscr = 1;
Hiroyuki Yokoyama93ca33c2016-07-25 01:52:43 +0000238 n = (u64)0x0400000 * fin;
239 do_div(n, fout);
240 fsrate = n;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000241 }
242
243 /*
244 * SRC_SRCCR / SRC_ROUTE_MODE0
245 */
246 cr = 0x00011110;
247 route = 0x0;
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000248 if (use_src) {
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000249 route = 0x1;
250
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000251 if (rsnd_src_sync_is_enabled(mod)) {
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000252 cr |= 0x1;
253 route |= rsnd_io_is_play(io) ?
254 (0x1 << 24) : (0x1 << 25);
255 }
256 }
257
258 /*
259 * SRC_BSDSR / SRC_BSISR
260 */
261 switch (rsnd_mod_id(mod)) {
262 case 5:
263 case 6:
264 case 7:
265 case 8:
266 bsdsr = 0x02400000; /* 6 - 1/6 */
267 bsisr = 0x00100060; /* 6 - 1/6 */
268 break;
269 default:
270 bsdsr = 0x01800000; /* 6 - 1/6 */
271 bsisr = 0x00100060 ;/* 6 - 1/6 */
272 break;
273 }
274
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000275 /* BUSIF_MODE */
276 tmp = rsnd_get_busif_shift(io, mod);
277 i_busif = ( is_play ? tmp : 0) | 1;
278 o_busif = (!is_play ? tmp : 0) | 1;
279
Kuninori Morimoto0fbab952016-02-18 08:13:45 +0000280 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
281
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000282 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
283 rsnd_mod_write(mod, SRC_ADINR, adinr);
284 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
285 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
286 rsnd_mod_write(mod, SRC_SRCCR, cr);
287 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
288 rsnd_mod_write(mod, SRC_BSISR, bsisr);
289 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
290
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000291 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
292 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
293
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000294 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
295
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000296 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800297}
298
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000299static int rsnd_src_irq(struct rsnd_mod *mod,
300 struct rsnd_dai_stream *io,
301 struct rsnd_priv *priv,
302 int enable)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000303{
304 struct rsnd_src *src = rsnd_mod_to_src(mod);
305 u32 sys_int_val, int_val, sys_int_mask;
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000306 int irq = src->irq;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000307 int id = rsnd_mod_id(mod);
308
309 sys_int_val =
310 sys_int_mask = OUF_SRC(id);
311 int_val = 0x3300;
312
313 /*
314 * IRQ is not supported on non-DT
315 * see
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000316 * rsnd_src_probe_()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000317 */
318 if ((irq <= 0) || !enable) {
319 sys_int_val = 0;
320 int_val = 0;
321 }
322
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000323 /*
324 * WORKAROUND
325 *
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000326 * ignore over flow error when rsnd_src_sync_is_enabled()
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000327 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000328 if (rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000329 sys_int_val = sys_int_val & 0xffff;
330
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000331 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
332 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
333 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000334
335 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000336}
337
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000338static void rsnd_src_status_clear(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000339{
340 u32 val = OUF_SRC(rsnd_mod_id(mod));
341
Kuninori Morimoto42b197e2016-10-26 04:28:42 +0000342 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
343 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000344}
345
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000346static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000347{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000348 u32 val0, val1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000349 bool ret = false;
350
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000351 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
352
353 /*
354 * WORKAROUND
355 *
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000356 * ignore over flow error when rsnd_src_sync_is_enabled()
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000357 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000358 if (rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000359 val0 = val0 & 0xffff;
360
361 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000362 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000363 ret = true;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000364
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000365 return ret;
366}
367
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000368static int rsnd_src_start(struct rsnd_mod *mod,
369 struct rsnd_dai_stream *io,
370 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000371{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000372 u32 val;
373
374 /*
375 * WORKAROUND
376 *
377 * Enable SRC output if you want to use sync convert together with DVC
378 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000379 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000380 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000381
382 rsnd_mod_write(mod, SRC_CTRL, val);
383
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000384 return 0;
385}
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000386
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000387static int rsnd_src_stop(struct rsnd_mod *mod,
388 struct rsnd_dai_stream *io,
389 struct rsnd_priv *priv)
390{
Kuninori Morimoto31739a62016-01-21 02:00:13 +0000391 rsnd_mod_write(mod, SRC_CTRL, 0);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000392
393 return 0;
394}
395
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000396static int rsnd_src_init(struct rsnd_mod *mod,
397 struct rsnd_dai_stream *io,
398 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000399{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000400 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000401
Kuninori Morimotoef30da1c52017-02-02 05:01:05 +0000402 /* reset sync convert_rate */
403 src->sync.val = 0;
404
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000405 rsnd_mod_power_on(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000406
Kuninori Morimoto98efeea2015-11-30 08:50:32 +0000407 rsnd_src_activation(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000408
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000409 rsnd_src_set_convert_rate(io, mod);
410
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000411 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000412
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000413 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000414}
415
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000416static int rsnd_src_quit(struct rsnd_mod *mod,
417 struct rsnd_dai_stream *io,
418 struct rsnd_priv *priv)
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000419{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000420 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000421
Kuninori Morimoto475a3612015-11-30 08:51:35 +0000422 rsnd_src_halt(mod);
423
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000424 rsnd_mod_power_off(mod);
425
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000426 /* reset sync convert_rate */
427 src->sync.val = 0;
428
429 return 0;
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000430}
431
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000432static void __rsnd_src_interrupt(struct rsnd_mod *mod,
433 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000434{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000435 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000436 bool stop = false;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000437
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000438 spin_lock(&priv->lock);
439
440 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000441 if (!rsnd_io_is_working(io))
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000442 goto rsnd_src_interrupt_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000443
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000444 if (rsnd_src_error_occurred(mod))
445 stop = true;
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000446
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000447 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000448rsnd_src_interrupt_out:
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000449
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000450 spin_unlock(&priv->lock);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000451
452 if (stop)
453 snd_pcm_stop_xrun(io->substream);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000454}
455
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000456static irqreturn_t rsnd_src_interrupt(int irq, void *data)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000457{
458 struct rsnd_mod *mod = data;
459
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000460 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000461
462 return IRQ_HANDLED;
463}
464
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000465static int rsnd_src_probe_(struct rsnd_mod *mod,
466 struct rsnd_dai_stream *io,
467 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800468{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800469 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800470 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000471 int irq = src->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800472 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800473
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000474 if (irq > 0) {
475 /*
476 * IRQ is not supported on non-DT
477 * see
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000478 * rsnd_src_irq()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000479 */
480 ret = devm_request_irq(dev, irq,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000481 rsnd_src_interrupt,
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000482 IRQF_SHARED,
483 dev_name(dev), mod);
484 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000485 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000486 }
487
Kuninori Morimotob99305d2016-10-25 00:36:13 +0000488 ret = rsnd_dma_attach(io, mod, &src->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700489
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800490 return ret;
491}
492
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000493static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac192015-06-15 06:25:20 +0000494 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000495 struct snd_soc_pcm_runtime *rtd)
496{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000497 struct rsnd_src *src = rsnd_mod_to_src(mod);
498 int ret;
499
500 /*
501 * enable SRC sync convert if possible
502 */
503
504 /*
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000505 * It can't use SRC Synchronous convert
506 * when Capture if it uses CMD
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000507 */
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000508 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000509 return 0;
510
511 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000512 * enable sync convert
513 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000514 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000515 rsnd_io_is_play(io) ?
516 "SRC Out Rate Switch" :
517 "SRC In Rate Switch",
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000518 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000519 &src->sen, 1);
520 if (ret < 0)
521 return ret;
522
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000523 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000524 rsnd_io_is_play(io) ?
525 "SRC Out Rate" :
526 "SRC In Rate",
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000527 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000528 &src->sync, 192000);
529
530 return ret;
531}
532
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000533static struct rsnd_mod_ops rsnd_src_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700534 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000535 .dma_req = rsnd_src_dma_req,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000536 .probe = rsnd_src_probe_,
537 .init = rsnd_src_init,
538 .quit = rsnd_src_quit,
539 .start = rsnd_src_start,
540 .stop = rsnd_src_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000541 .irq = rsnd_src_irq,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000542 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000543 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800544};
545
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800546struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700547{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800548 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100549 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700550
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000551 return rsnd_mod_get(rsnd_src_get(priv, id));
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700552}
553
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000554int rsnd_src_probe(struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700555{
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000556 struct device_node *node;
557 struct device_node *np;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700558 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800559 struct rsnd_src *src;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800560 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800561 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000562 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700563
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000564 /* This driver doesn't support Gen1 at this point */
565 if (rsnd_is_gen1(priv))
566 return 0;
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700567
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000568 node = rsnd_src_of_node(priv);
569 if (!node)
570 return 0; /* not used is not error */
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700571
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000572 nr = of_get_child_count(node);
573 if (!nr) {
574 ret = -EINVAL;
575 goto rsnd_src_probe_done;
576 }
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800577
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800578 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000579 if (!src) {
580 ret = -ENOMEM;
581 goto rsnd_src_probe_done;
582 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700583
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800584 priv->src_nr = nr;
585 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700586
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000587 i = 0;
588 for_each_child_of_node(node, np) {
Sergei Shtylyovde196512016-05-12 01:36:40 +0300589 if (!of_device_is_available(np))
590 goto skip;
591
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000592 src = rsnd_src_get(priv, i);
593
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700594 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
595 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800596
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000597 src->irq = irq_of_parse_and_map(np, 0);
598 if (!src->irq) {
599 ret = -EINVAL;
600 goto rsnd_src_probe_done;
601 }
Kuninori Morimotoef749402013-12-19 19:28:51 -0800602
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000603 clk = devm_clk_get(dev, name);
604 if (IS_ERR(clk)) {
605 ret = PTR_ERR(clk);
606 goto rsnd_src_probe_done;
607 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700608
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000609 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000610 &rsnd_src_ops, clk, rsnd_mod_get_status,
611 RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000612 if (ret)
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000613 goto rsnd_src_probe_done;
614
Sergei Shtylyovde196512016-05-12 01:36:40 +0300615skip:
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000616 i++;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700617 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700618
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000619 ret = 0;
620
621rsnd_src_probe_done:
622 of_node_put(node);
623
624 return ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700625}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000626
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000627void rsnd_src_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000628{
629 struct rsnd_src *src;
630 int i;
631
632 for_each_rsnd_src(src, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000633 rsnd_mod_quit(rsnd_mod_get(src));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000634 }
635}