blob: cd38a43b976fae6739605ae381e3813e6b4d6736 [file] [log] [blame]
Kuninori Morimoto1e0edd42018-06-12 05:58:38 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// Renesas R-Car SRC support
4//
5// Copyright (C) 2013 Renesas Solutions Corp.
6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Kuninori Morimoto2b627862018-02-13 02:08:53 +00007
8/*
9 * you can enable below define if you don't need
10 * SSI interrupt status debug message when debugging
11 * see rsnd_dbg_irq_status()
12 *
13 * #define RSND_DEBUG_NO_IRQ_STATUS 1
14 */
15
Kuninori Morimoto07539c12013-07-21 21:36:35 -070016#include "rsnd.h"
17
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070018#define SRC_NAME "src"
19
Kuninori Morimotocfcefe02015-01-08 01:52:36 +000020/* SCU_SYSTEM_STATUS0/1 */
21#define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
22
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080023struct rsnd_src {
Kuninori Morimoto07539c12013-07-21 21:36:35 -070024 struct rsnd_mod mod;
Kuninori Morimoto940e9472015-10-26 08:42:25 +000025 struct rsnd_mod *dma;
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000026 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
27 struct rsnd_kctrl_cfg_s sync; /* sync convert */
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +000028 u32 convert_rate; /* sampling rate convert */
Kuninori Morimotoadf6a682015-11-10 05:11:55 +000029 int irq;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070030};
31
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080032#define RSND_SRC_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070033
Kuninori Morimotoadf6a682015-11-10 05:11:55 +000034#define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
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 Morimoto98efeea2015-11-30 08:50:32 +000058static void rsnd_src_activation(struct rsnd_mod *mod)
Kuninori Morimoto379febf2015-07-15 07:12:18 +000059{
60 rsnd_mod_write(mod, SRC_SWRSR, 0);
61 rsnd_mod_write(mod, SRC_SWRSR, 1);
62}
63
Kuninori Morimoto475a3612015-11-30 08:51:35 +000064static void rsnd_src_halt(struct rsnd_mod *mod)
65{
66 rsnd_mod_write(mod, SRC_SRCIR, 1);
67 rsnd_mod_write(mod, SRC_SWRSR, 0);
68}
69
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +000070static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
71 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +000072{
73 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +000074 int is_play = rsnd_io_is_play(io);
75
76 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
77 mod,
78 is_play ? "rx" : "tx");
79}
80
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +000081static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +000082 struct rsnd_mod *mod)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000083{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000084 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +000085 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000086 u32 convert_rate;
87
88 if (!runtime)
89 return 0;
90
Kuninori Morimotoab2049f2016-03-07 05:07:57 +000091 if (!rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000092 return src->convert_rate;
93
94 convert_rate = src->sync.val;
95
96 if (!convert_rate)
97 convert_rate = src->convert_rate;
98
99 if (!convert_rate)
100 convert_rate = runtime->rate;
101
102 return convert_rate;
103}
104
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000105unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
106 struct rsnd_dai_stream *io,
107 int is_in)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800108{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700109 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000110 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700111 unsigned int rate = 0;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000112 int is_play = rsnd_io_is_play(io);
113
114 /*
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000115 * Playback
116 * runtime_rate -> [SRC] -> convert_rate
117 *
118 * Capture
119 * convert_rate -> [SRC] -> runtime_rate
120 */
121
122 if (is_play == is_in)
123 return runtime->rate;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800124
Kuninori Morimoto1a9be9e2016-03-07 05:07:29 +0000125 /*
126 * return convert rate if SRC is used,
127 * otherwise, return runtime->rate as usual
128 */
129 if (src_mod)
130 rate = rsnd_src_convert_rate(io, src_mod);
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700131
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800132 if (!rate)
133 rate = runtime->rate;
134
135 return rate;
136}
137
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000138static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac192015-06-15 06:25:20 +0000139 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000140 struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *fe_params)
142{
143 struct rsnd_src *src = rsnd_mod_to_src(mod);
144 struct snd_soc_pcm_runtime *fe = substream->private_data;
145
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000146 /*
147 * SRC assumes that it is used under DPCM if user want to use
148 * sampling rate convert. Then, SRC should be FE.
149 * And then, this function will be called *after* BE settings.
150 * this means, each BE already has fixuped hw_params.
151 * see
152 * dpcm_fe_dai_hw_params()
153 * dpcm_be_dai_hw_params()
154 */
Kuninori Morimotofc99d232017-04-06 07:24:36 +0000155 src->convert_rate = 0;
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000156 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
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000161 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000162 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);
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000178 int is_play = rsnd_io_is_play(io);
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000179 int use_src = 0;
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000180 u32 fin, fout;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000181 u32 ifscr, fsrate, adinr;
182 u32 cr, route;
183 u32 bsdsr, bsisr;
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000184 u32 i_busif, o_busif, tmp;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000185 uint ratio;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800186
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000187 if (!runtime)
188 return;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800189
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000190 fin = rsnd_src_get_in_rate(priv, io);
191 fout = rsnd_src_get_out_rate(priv, io);
192
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000193 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000194 if (fin == fout)
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000195 ratio = 0;
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000196 else if (fin > fout)
197 ratio = 100 * fin / fout;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000198 else
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000199 ratio = 100 * fout / fin;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000200
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000201 if (ratio > 600) {
202 dev_err(dev, "FSO/FSI ratio error\n");
203 return;
204 }
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000205
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000206 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
207
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000208 /*
Kuninori Morimotob65cb7a2017-08-21 07:03:01 +0000209 * SRC_ADINR
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000210 */
Kuninori Morimotoc45f7262015-11-30 08:49:33 +0000211 adinr = rsnd_get_adinr_bit(mod, io) |
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000212 rsnd_runtime_channel_original(io);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000213
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000214 /*
Kuninori Morimotob65cb7a2017-08-21 07:03:01 +0000215 * SRC_IFSCR / SRC_IFSVR
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000216 */
217 ifscr = 0;
218 fsrate = 0;
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000219 if (use_src) {
Hiroyuki Yokoyama93ca33c2016-07-25 01:52:43 +0000220 u64 n;
221
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000222 ifscr = 1;
Hiroyuki Yokoyama93ca33c2016-07-25 01:52:43 +0000223 n = (u64)0x0400000 * fin;
224 do_div(n, fout);
225 fsrate = n;
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000226 }
227
228 /*
Kuninori Morimotob65cb7a2017-08-21 07:03:01 +0000229 * SRC_SRCCR / SRC_ROUTE_MODE0
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000230 */
231 cr = 0x00011110;
232 route = 0x0;
Kuninori Morimoto67923f72016-10-27 01:05:21 +0000233 if (use_src) {
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000234 route = 0x1;
235
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000236 if (rsnd_src_sync_is_enabled(mod)) {
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000237 cr |= 0x1;
238 route |= rsnd_io_is_play(io) ?
239 (0x1 << 24) : (0x1 << 25);
240 }
241 }
242
243 /*
244 * SRC_BSDSR / SRC_BSISR
245 */
246 switch (rsnd_mod_id(mod)) {
247 case 5:
248 case 6:
249 case 7:
250 case 8:
251 bsdsr = 0x02400000; /* 6 - 1/6 */
252 bsisr = 0x00100060; /* 6 - 1/6 */
253 break;
254 default:
255 bsdsr = 0x01800000; /* 6 - 1/6 */
256 bsisr = 0x00100060 ;/* 6 - 1/6 */
257 break;
258 }
259
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000260 /* BUSIF_MODE */
261 tmp = rsnd_get_busif_shift(io, mod);
262 i_busif = ( is_play ? tmp : 0) | 1;
263 o_busif = (!is_play ? tmp : 0) | 1;
264
Kuninori Morimoto0fbab952016-02-18 08:13:45 +0000265 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
266
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000267 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
268 rsnd_mod_write(mod, SRC_ADINR, adinr);
269 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
270 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
271 rsnd_mod_write(mod, SRC_SRCCR, cr);
272 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
273 rsnd_mod_write(mod, SRC_BSISR, bsisr);
274 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
275
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000276 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
277 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
278
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000279 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
280
Kuninori Morimoto0102eed2016-03-07 05:09:14 +0000281 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800282}
283
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000284static int rsnd_src_irq(struct rsnd_mod *mod,
285 struct rsnd_dai_stream *io,
286 struct rsnd_priv *priv,
287 int enable)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000288{
289 struct rsnd_src *src = rsnd_mod_to_src(mod);
290 u32 sys_int_val, int_val, sys_int_mask;
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000291 int irq = src->irq;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000292 int id = rsnd_mod_id(mod);
293
294 sys_int_val =
295 sys_int_mask = OUF_SRC(id);
296 int_val = 0x3300;
297
298 /*
299 * IRQ is not supported on non-DT
300 * see
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000301 * rsnd_src_probe_()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000302 */
303 if ((irq <= 0) || !enable) {
304 sys_int_val = 0;
305 int_val = 0;
306 }
307
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000308 /*
309 * WORKAROUND
310 *
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000311 * ignore over flow error when rsnd_src_sync_is_enabled()
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000312 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000313 if (rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000314 sys_int_val = sys_int_val & 0xffff;
315
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000316 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
317 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
318 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000319
320 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000321}
322
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000323static void rsnd_src_status_clear(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000324{
325 u32 val = OUF_SRC(rsnd_mod_id(mod));
326
Kuninori Morimoto42b197e2016-10-26 04:28:42 +0000327 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
328 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000329}
330
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000331static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000332{
Kuninori Morimoto2b627862018-02-13 02:08:53 +0000333 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
334 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000335 u32 val0, val1;
Kuninori Morimoto2b627862018-02-13 02:08:53 +0000336 u32 status0, status1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000337 bool ret = false;
338
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000339 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
340
341 /*
342 * WORKAROUND
343 *
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000344 * ignore over flow error when rsnd_src_sync_is_enabled()
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000345 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000346 if (rsnd_src_sync_is_enabled(mod))
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000347 val0 = val0 & 0xffff;
348
Kuninori Morimoto2b627862018-02-13 02:08:53 +0000349 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
350 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
351 if ((status0 & val0) || (status1 & val1)) {
352 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x, 0x%08x\n",
353 rsnd_mod_name(mod), rsnd_mod_id(mod),
354 status0, status1);
355
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000356 ret = true;
Kuninori Morimoto2b627862018-02-13 02:08:53 +0000357 }
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000358
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000359 return ret;
360}
361
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000362static int rsnd_src_start(struct rsnd_mod *mod,
363 struct rsnd_dai_stream *io,
364 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000365{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000366 u32 val;
367
368 /*
369 * WORKAROUND
370 *
371 * Enable SRC output if you want to use sync convert together with DVC
372 */
Kuninori Morimotoab2049f2016-03-07 05:07:57 +0000373 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000374 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000375
376 rsnd_mod_write(mod, SRC_CTRL, val);
377
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000378 return 0;
379}
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000380
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000381static int rsnd_src_stop(struct rsnd_mod *mod,
382 struct rsnd_dai_stream *io,
383 struct rsnd_priv *priv)
384{
Kuninori Morimoto31739a62016-01-21 02:00:13 +0000385 rsnd_mod_write(mod, SRC_CTRL, 0);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000386
387 return 0;
388}
389
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000390static int rsnd_src_init(struct rsnd_mod *mod,
391 struct rsnd_dai_stream *io,
392 struct rsnd_priv *priv)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000393{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000394 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000395
Kuninori Morimotoef30da1c52017-02-02 05:01:05 +0000396 /* reset sync convert_rate */
397 src->sync.val = 0;
398
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000399 rsnd_mod_power_on(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000400
Kuninori Morimoto98efeea2015-11-30 08:50:32 +0000401 rsnd_src_activation(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000402
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000403 rsnd_src_set_convert_rate(io, mod);
404
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000405 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000406
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000407 return 0;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000408}
409
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000410static int rsnd_src_quit(struct rsnd_mod *mod,
411 struct rsnd_dai_stream *io,
412 struct rsnd_priv *priv)
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000413{
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000414 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000415
Kuninori Morimoto475a3612015-11-30 08:51:35 +0000416 rsnd_src_halt(mod);
417
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000418 rsnd_mod_power_off(mod);
419
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000420 /* reset sync convert_rate */
421 src->sync.val = 0;
422
423 return 0;
Kuninori Morimotob761bf22015-10-26 08:40:59 +0000424}
425
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000426static void __rsnd_src_interrupt(struct rsnd_mod *mod,
427 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000428{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000429 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000430 bool stop = false;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000431
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000432 spin_lock(&priv->lock);
433
434 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000435 if (!rsnd_io_is_working(io))
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000436 goto rsnd_src_interrupt_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000437
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000438 if (rsnd_src_error_occurred(mod))
439 stop = true;
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000440
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000441 rsnd_src_status_clear(mod);
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000442rsnd_src_interrupt_out:
Kuninori Morimoto8cc225f2015-11-26 11:11:03 +0000443
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000444 spin_unlock(&priv->lock);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000445
446 if (stop)
447 snd_pcm_stop_xrun(io->substream);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000448}
449
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000450static irqreturn_t rsnd_src_interrupt(int irq, void *data)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000451{
452 struct rsnd_mod *mod = data;
453
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000454 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000455
456 return IRQ_HANDLED;
457}
458
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000459static int rsnd_src_probe_(struct rsnd_mod *mod,
460 struct rsnd_dai_stream *io,
461 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800462{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800463 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800464 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000465 int irq = src->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800466 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800467
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000468 if (irq > 0) {
469 /*
470 * IRQ is not supported on non-DT
471 * see
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000472 * rsnd_src_irq()
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000473 */
474 ret = devm_request_irq(dev, irq,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000475 rsnd_src_interrupt,
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000476 IRQF_SHARED,
477 dev_name(dev), mod);
478 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000479 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000480 }
481
Kuninori Morimotob99305d2016-10-25 00:36:13 +0000482 ret = rsnd_dma_attach(io, mod, &src->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700483
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800484 return ret;
485}
486
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000487static int rsnd_src_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac192015-06-15 06:25:20 +0000488 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000489 struct snd_soc_pcm_runtime *rtd)
490{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000491 struct rsnd_src *src = rsnd_mod_to_src(mod);
492 int ret;
493
494 /*
495 * enable SRC sync convert if possible
496 */
497
498 /*
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000499 * It can't use SRC Synchronous convert
500 * when Capture if it uses CMD
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000501 */
Kuninori Morimoto61a219f2016-03-07 05:06:17 +0000502 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
Kuninori Morimoto7115cb92015-11-10 07:39:17 +0000503 return 0;
504
505 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000506 * enable sync convert
507 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000508 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000509 rsnd_io_is_play(io) ?
510 "SRC Out Rate Switch" :
511 "SRC In Rate Switch",
Kuninori Morimotof0b04d82017-06-07 00:11:48 +0000512 rsnd_kctrl_accept_anytime,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000513 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000514 &src->sen, 1);
515 if (ret < 0)
516 return ret;
517
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000518 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000519 rsnd_io_is_play(io) ?
520 "SRC Out Rate" :
521 "SRC In Rate",
Kuninori Morimotof0b04d82017-06-07 00:11:48 +0000522 rsnd_kctrl_accept_runtime,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000523 rsnd_src_set_convert_rate,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000524 &src->sync, 192000);
525
526 return ret;
527}
528
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000529static struct rsnd_mod_ops rsnd_src_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700530 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000531 .dma_req = rsnd_src_dma_req,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000532 .probe = rsnd_src_probe_,
533 .init = rsnd_src_init,
534 .quit = rsnd_src_quit,
535 .start = rsnd_src_start,
536 .stop = rsnd_src_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000537 .irq = rsnd_src_irq,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000538 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto75916f62015-11-10 05:10:48 +0000539 .pcm_new = rsnd_src_pcm_new,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800540};
541
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800542struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700543{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800544 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100545 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700546
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000547 return rsnd_mod_get(rsnd_src_get(priv, id));
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700548}
549
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000550int rsnd_src_probe(struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700551{
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000552 struct device_node *node;
553 struct device_node *np;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700554 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800555 struct rsnd_src *src;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800556 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800557 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000558 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700559
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000560 /* This driver doesn't support Gen1 at this point */
561 if (rsnd_is_gen1(priv))
562 return 0;
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -0700563
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000564 node = rsnd_src_of_node(priv);
565 if (!node)
566 return 0; /* not used is not error */
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700567
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000568 nr = of_get_child_count(node);
569 if (!nr) {
570 ret = -EINVAL;
571 goto rsnd_src_probe_done;
572 }
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800573
Kees Cooka86854d2018-06-12 14:07:58 -0700574 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000575 if (!src) {
576 ret = -ENOMEM;
577 goto rsnd_src_probe_done;
578 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700579
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800580 priv->src_nr = nr;
581 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700582
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000583 i = 0;
584 for_each_child_of_node(node, np) {
Sergei Shtylyovde196512016-05-12 01:36:40 +0300585 if (!of_device_is_available(np))
586 goto skip;
587
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000588 src = rsnd_src_get(priv, i);
589
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700590 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
591 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800592
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000593 src->irq = irq_of_parse_and_map(np, 0);
594 if (!src->irq) {
595 ret = -EINVAL;
Julia Lawall53ba2aa2017-07-15 09:19:07 +0200596 of_node_put(np);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000597 goto rsnd_src_probe_done;
598 }
Kuninori Morimotoef749402013-12-19 19:28:51 -0800599
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000600 clk = devm_clk_get(dev, name);
601 if (IS_ERR(clk)) {
602 ret = PTR_ERR(clk);
Julia Lawall53ba2aa2017-07-15 09:19:07 +0200603 of_node_put(np);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000604 goto rsnd_src_probe_done;
605 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700606
Kuninori Morimotoe8e7b7b2015-11-10 05:09:52 +0000607 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000608 &rsnd_src_ops, clk, rsnd_mod_get_status,
609 RSND_MOD_SRC, i);
Julia Lawall53ba2aa2017-07-15 09:19:07 +0200610 if (ret) {
611 of_node_put(np);
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000612 goto rsnd_src_probe_done;
Julia Lawall53ba2aa2017-07-15 09:19:07 +0200613 }
Kuninori Morimotoadf6a682015-11-10 05:11:55 +0000614
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}