blob: 261b50217c48d94f0a66f44c513f685755ddae07 [file] [log] [blame]
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08002 * Renesas R-Car SRC support
Kuninori Morimoto07539c12013-07-21 21:36:35 -07003 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include "rsnd.h"
12
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070013#define SRC_NAME "src"
14
Kuninori Morimotocfcefe02015-01-08 01:52:36 +000015/* SRCx_STATUS */
16#define OUF_SRCO ((1 << 12) | (1 << 13))
17#define OUF_SRCI ((1 << 9) | (1 << 8))
18
19/* SCU_SYSTEM_STATUS0/1 */
20#define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
21
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080022struct rsnd_src {
23 struct rsnd_src_platform_info *info; /* rcar_snd.h */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070024 struct rsnd_mod mod;
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000025 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
26 struct rsnd_kctrl_cfg_s sync; /* sync convert */
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +000027 u32 convert_rate; /* sampling rate convert */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +000028 int err;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070029};
30
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080031#define RSND_SRC_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070032
Kuninori Morimotoda599fd2015-07-15 07:11:21 +000033#define rsnd_src_nr(priv) ((priv)->src_nr)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +000034#define rsnd_enable_sync_convert(src) ((src)->sen.val)
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +000035#define rsnd_src_of_node(priv) \
36 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
37
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080038#define rsnd_mod_to_src(_mod) \
39 container_of((_mod), struct rsnd_src, mod)
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080040
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080041#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080042 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080043 ((i) < rsnd_src_nr(priv)) && \
44 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080045 i++)
46
47
Kuninori Morimotoef749402013-12-19 19:28:51 -080048/*
49 * image of SRC (Sampling Rate Converter)
50 *
51 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
52 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
53 * 44.1kHz <-> +-----+ +-----+ +-------+
54 * ...
55 *
56 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070057
Kuninori Morimoto41c62212014-01-23 18:39:56 -080058/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080059 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080060 *
61 * Gen1
62 *
63 * [mem] -> [SRU] -> [SSI]
64 * |--------|
65 *
66 * Gen2
67 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080068 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080069 * |-----------------|
70 */
71
72/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080073 * How to use SRC bypass mode for debugging
74 *
75 * SRC has bypass mode, and it is useful for debugging.
76 * In Gen2 case,
77 * SRCm_MODE controls whether SRC is used or not
78 * SSI_MODE0 controls whether SSIU which receives SRC data
79 * is used or not.
80 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
81 * but SRC bypass mode needs SSI_MODE0 only.
82 *
83 * This driver request
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080084 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080085 * u32 convert_rate;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070086 * int dma_id;
Kuninori Morimoto41c62212014-01-23 18:39:56 -080087 * }
88 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080089 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080090 * above convert_rate, and it controls
91 * whether SRC is used or not.
92 *
93 * ex) doesn't use SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070094 * static struct rsnd_dai_platform_info rsnd_dai = {
95 * .playback = { .ssi = &rsnd_ssi[0], },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080096 * };
97 *
98 * ex) uses SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070099 * static struct rsnd_src_platform_info rsnd_src[] = {
100 * RSND_SCU(48000, 0),
101 * ...
102 * };
103 * static struct rsnd_dai_platform_info rsnd_dai = {
104 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800105 * };
106 *
107 * ex) uses SRC bypass mode
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700108 * static struct rsnd_src_platform_info rsnd_src[] = {
109 * RSND_SCU(0, 0),
110 * ...
111 * };
112 * static struct rsnd_dai_platform_info rsnd_dai = {
113 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800114 * };
115 *
116 */
117
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800118/*
119 * Gen1/Gen2 common functions
120 */
Kuninori Morimoto379febf2015-07-15 07:12:18 +0000121static void rsnd_src_soft_reset(struct rsnd_mod *mod)
122{
123 rsnd_mod_write(mod, SRC_SWRSR, 0);
124 rsnd_mod_write(mod, SRC_SWRSR, 1);
125}
126
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000127
128#define rsnd_src_initialize_lock(mod) __rsnd_src_initialize_lock(mod, 1)
129#define rsnd_src_initialize_unlock(mod) __rsnd_src_initialize_lock(mod, 0)
130static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
131{
132 rsnd_mod_write(mod, SRC_SRCIR, enable);
133}
134
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000135static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
136 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000137{
138 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000139 int is_play = rsnd_io_is_play(io);
140
141 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
142 mod,
143 is_play ? "rx" : "tx");
144}
145
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700146int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000147 struct rsnd_dai_stream *io,
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700148 int use_busif)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800149{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000150 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800151 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800152
153 /*
154 * SSI_MODE0
155 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800156 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700157 !use_busif << ssi_id);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800158
159 /*
160 * SSI_MODE1
161 */
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000162 if (rsnd_ssi_is_pin_sharing(io)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800163 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800164 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800165 case 1:
166 shift = 0;
167 break;
168 case 2:
169 shift = 2;
170 break;
171 case 4:
172 shift = 16;
173 break;
174 }
175
176 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800177 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800178 0x3 << shift,
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000179 rsnd_rdai_is_clk_master(rdai) ?
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800180 0x2 << shift : 0x1 << shift);
181 }
182
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700183 /*
184 * DMA settings for SSIU
185 */
186 if (use_busif) {
Kuninori Morimoto46890322015-07-15 07:14:47 +0000187 u32 val = rsnd_get_dalign(ssi_mod, io);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700188
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700189 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
Kuninori Morimoto3023b382015-07-15 07:14:05 +0000190 rsnd_get_adinr_bit(ssi_mod, io));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700191 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
192 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
Kuninori Morimoto1cc71952014-07-30 23:52:26 -0700193
Kuninori Morimotocdde84d2015-07-15 07:09:27 +0000194 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
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
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800297 /* Set channel number and output bit length */
Kuninori Morimoto3023b382015-07-15 07:14:05 +0000298 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr_bit(mod, io));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800299
300 /* Enable the initial value of IFS */
301 if (fsrate) {
302 rsnd_mod_write(mod, SRC_IFSCR, 1);
303
304 /* Set initial value of IFS */
305 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
306 }
307
308 /* use DMA transfer */
309 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
310
311 return 0;
312}
313
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000314static int rsnd_src_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000315 struct rsnd_dai_stream *io,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000316 struct snd_pcm_substream *substream,
317 struct snd_pcm_hw_params *fe_params)
318{
319 struct rsnd_src *src = rsnd_mod_to_src(mod);
320 struct snd_soc_pcm_runtime *fe = substream->private_data;
321
322 /* default value (mainly for non-DT) */
323 src->convert_rate = src->info->convert_rate;
324
325 /*
326 * SRC assumes that it is used under DPCM if user want to use
327 * sampling rate convert. Then, SRC should be FE.
328 * And then, this function will be called *after* BE settings.
329 * this means, each BE already has fixuped hw_params.
330 * see
331 * dpcm_fe_dai_hw_params()
332 * dpcm_be_dai_hw_params()
333 */
334 if (fe->dai_link->dynamic) {
335 int stream = substream->stream;
336 struct snd_soc_dpcm *dpcm;
337 struct snd_pcm_hw_params *be_params;
338
339 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
340 be_params = &dpcm->hw_params;
341
342 if (params_rate(fe_params) != params_rate(be_params))
343 src->convert_rate = params_rate(be_params);
344 }
345 }
346
347 return 0;
348}
349
350static int rsnd_src_init(struct rsnd_mod *mod,
351 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800352{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800353 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800354
Kuninori Morimotoc9929342015-10-22 03:15:04 +0000355 rsnd_mod_power_on(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800356
Kuninori Morimoto379febf2015-07-15 07:12:18 +0000357 rsnd_src_soft_reset(mod);
358
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000359 rsnd_src_initialize_lock(mod);
360
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 Morimoto1b7b08ef2014-01-23 18:41:36 -0800366 return 0;
367}
368
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800369static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000370 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000371 struct rsnd_priv *priv)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800372{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800373 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000374 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800375
Kuninori Morimotoc9929342015-10-22 03:15:04 +0000376 rsnd_mod_power_off(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800377
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000378 if (src->err)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000379 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
380 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000381
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000382 src->convert_rate = 0;
383
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000384 /* reset sync convert_rate */
385 src->sync.val = 0;
386
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800387 return 0;
388}
389
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000390static int rsnd_src_start(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800391{
Kuninori Morimotod1ade512015-07-15 07:13:47 +0000392 rsnd_src_initialize_unlock(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800393
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800394 return 0;
395}
396
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000397static int rsnd_src_stop(struct rsnd_mod *mod)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800398{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000399 /* nothing to do */
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800400 return 0;
401}
402
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800403/*
404 * Gen1 functions
405 */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000406static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
407 struct rsnd_mod *mod)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700408{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800409 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700410 u32 mask;
411 int shift;
412 } routes[] = {
413 { 0xF, 0, }, /* 0 */
414 { 0xF, 4, }, /* 1 */
415 { 0xF, 8, }, /* 2 */
416 { 0x7, 12, }, /* 3 */
417 { 0x7, 16, }, /* 4 */
418 { 0x7, 20, }, /* 5 */
419 { 0x7, 24, }, /* 6 */
420 { 0x3, 28, }, /* 7 */
421 { 0x3, 30, }, /* 8 */
422 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700423 u32 mask;
424 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700425 int id;
426
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700427 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300428 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700429 return -EIO;
430
431 /*
432 * SRC_ROUTE_SELECT
433 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000434 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700435 val = val << routes[id].shift;
436 mask = routes[id].mask << routes[id].shift;
437
438 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
439
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800440 return 0;
441}
442
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000443static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
444 struct rsnd_mod *mod)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800445{
446 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800447 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800448 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000449 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800450 u32 mask;
451 u32 val;
452 int shift;
453 int id = rsnd_mod_id(mod);
454 int ret;
455
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700456 /*
457 * SRC_TIMING_SELECT
458 */
459 shift = (id % 4) * 8;
460 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800461
462 /*
463 * ADG is used as source clock if SRC was used,
464 * then, SSI WS is used as destination clock.
465 * SSI WS is used as source clock if SRC is not used
466 * (when playback, source/destination become reverse when capture)
467 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800468 ret = 0;
469 if (convert_rate) {
470 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800471 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800472 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
473 runtime->rate,
474 convert_rate);
475 } else if (8 == id) {
476 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700477 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800478 } else {
479 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700480 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800481 }
482
483 if (ret < 0)
484 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700485
486 switch (id / 4) {
487 case 0:
488 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
489 break;
490 case 1:
491 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
492 break;
493 case 2:
494 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
495 break;
496 }
497
498 return 0;
499}
500
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000501static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
502 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700503{
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000504 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800505 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700506
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000507 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800508 if (ret < 0)
509 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700510
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800511 /* Select SRC mode (fixed value) */
512 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800513
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800514 /* Set the restriction value of the FS ratio (98%) */
515 rsnd_mod_write(mod, SRC_MNFSR,
516 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700517
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000518 /* Gen1/Gen2 are not compatible */
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000519 if (rsnd_src_convert_rate(io, src))
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000520 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
521
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800522 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800523
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700524 return 0;
525}
526
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800527static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000528 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000529 struct rsnd_priv *priv)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700530{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700531 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700532
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000533 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800534 if (ret < 0)
535 return ret;
536
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000537 ret = rsnd_src_set_route_gen1(io, mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700538 if (ret < 0)
539 return ret;
540
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000541 ret = rsnd_src_set_convert_rate_gen1(mod, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700542 if (ret < 0)
543 return ret;
544
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000545 ret = rsnd_src_set_convert_timing_gen1(io, mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800546 if (ret < 0)
547 return ret;
548
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800549 return 0;
550}
551
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800552static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000553 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000554 struct rsnd_priv *priv)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800555{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800556 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800557
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800558 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800559
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000560 return rsnd_src_start(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800561}
562
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800563static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000564 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000565 struct rsnd_priv *priv)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800566{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800567 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800568
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800569 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800570
Kuninori Morimotof0ef0cb2014-11-27 08:07:28 +0000571 return rsnd_src_stop(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800572}
573
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800574static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700575 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000576 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800577 .init = rsnd_src_init_gen1,
578 .quit = rsnd_src_quit,
579 .start = rsnd_src_start_gen1,
580 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000581 .hw_params = rsnd_src_hw_params,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700582};
583
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800584/*
585 * Gen2 functions
586 */
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000587#define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
588#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
589static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
590{
591 struct rsnd_src *src = rsnd_mod_to_src(mod);
592 u32 sys_int_val, int_val, sys_int_mask;
593 int irq = src->info->irq;
594 int id = rsnd_mod_id(mod);
595
596 sys_int_val =
597 sys_int_mask = OUF_SRC(id);
598 int_val = 0x3300;
599
600 /*
601 * IRQ is not supported on non-DT
602 * see
603 * rsnd_src_probe_gen2()
604 */
605 if ((irq <= 0) || !enable) {
606 sys_int_val = 0;
607 int_val = 0;
608 }
609
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000610 /*
611 * WORKAROUND
612 *
613 * ignore over flow error when rsnd_enable_sync_convert()
614 */
615 if (rsnd_enable_sync_convert(src))
616 sys_int_val = sys_int_val & 0xffff;
617
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000618 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
619 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
620 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
621}
622
623static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
624{
625 u32 val = OUF_SRC(rsnd_mod_id(mod));
626
627 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
628 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
629}
630
631static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
632{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000633 struct rsnd_src *src = rsnd_mod_to_src(mod);
634 u32 val0, val1;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000635 bool ret = false;
636
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000637 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
638
639 /*
640 * WORKAROUND
641 *
642 * ignore over flow error when rsnd_enable_sync_convert()
643 */
644 if (rsnd_enable_sync_convert(src))
645 val0 = val0 & 0xffff;
646
647 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
648 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000649 struct rsnd_src *src = rsnd_mod_to_src(mod);
650
651 src->err++;
652 ret = true;
653 }
654
655 /* clear error static */
656 rsnd_src_error_clear_gen2(mod);
657
658 return ret;
659}
660
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000661static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
662 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000663{
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000664 struct rsnd_src *src = rsnd_mod_to_src(mod);
665 u32 val;
666
Kuninori Morimoto46890322015-07-15 07:14:47 +0000667 val = rsnd_get_dalign(mod, io);
668
669 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, val);
670
Kuninori Morimoto1a1bf582015-07-15 07:10:22 +0000671 /*
672 * WORKAROUND
673 *
674 * Enable SRC output if you want to use sync convert together with DVC
675 */
676 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
677 0x01 : 0x11;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000678
679 rsnd_mod_write(mod, SRC_CTRL, val);
680
681 rsnd_src_error_clear_gen2(mod);
682
683 rsnd_src_start(mod);
684
685 rsnd_src_irq_enable_gen2(mod);
686
687 return 0;
688}
689
690static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
691{
692 rsnd_src_irq_disable_gen2(mod);
693
694 rsnd_mod_write(mod, SRC_CTRL, 0);
695
696 rsnd_src_error_record_gen2(mod);
697
698 return rsnd_src_stop(mod);
699}
700
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000701static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
702 struct rsnd_dai_stream *io)
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000703{
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000704 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000705
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000706 spin_lock(&priv->lock);
707
708 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000709 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000710 goto rsnd_src_interrupt_gen2_out;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000711
712 if (rsnd_src_error_record_gen2(mod)) {
713 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000714 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000715 struct device *dev = rsnd_priv_to_dev(priv);
716
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000717 dev_dbg(dev, "%s[%d] restart\n",
718 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000719
720 _rsnd_src_stop_gen2(mod);
721 if (src->err < 1024)
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000722 _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto639b2312015-03-19 04:14:04 +0000723 else
724 dev_warn(dev, "no more SRC restart\n");
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000725 }
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000726
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000727rsnd_src_interrupt_gen2_out:
728 spin_unlock(&priv->lock);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000729}
730
731static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
732{
733 struct rsnd_mod *mod = data;
734
735 rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000736
737 return IRQ_HANDLED;
738}
739
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000740static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
741 struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800742{
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700743 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
744 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700745 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
746 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000747 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000748 u32 cr, route;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700749 uint ratio;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800750 int ret;
751
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700752 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
Kuninori Morimotob167a572014-11-27 08:06:34 +0000753 if (!convert_rate)
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700754 ratio = 0;
Kuninori Morimotob167a572014-11-27 08:06:34 +0000755 else if (convert_rate > runtime->rate)
756 ratio = 100 * convert_rate / runtime->rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700757 else
Kuninori Morimotob167a572014-11-27 08:06:34 +0000758 ratio = 100 * runtime->rate / convert_rate;
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700759
760 if (ratio > 600) {
761 dev_err(dev, "FSO/FSI ratio error\n");
762 return -EINVAL;
763 }
764
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000765 ret = rsnd_src_set_convert_rate(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800766 if (ret < 0)
767 return ret;
768
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000769 cr = 0x00011110;
770 route = 0x0;
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000771 if (convert_rate) {
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000772 route = 0x1;
773
774 if (rsnd_enable_sync_convert(src)) {
775 cr |= 0x1;
776 route |= rsnd_io_is_play(io) ?
777 (0x1 << 24) : (0x1 << 25);
778 }
Kuninori Morimoto933cc8c2014-11-27 08:07:07 +0000779 }
780
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000781 rsnd_mod_write(mod, SRC_SRCCR, cr);
782 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
783
Kuninori Morimoto054cd7f2014-07-30 23:52:04 -0700784 switch (rsnd_mod_id(mod)) {
785 case 5:
786 case 6:
787 case 7:
788 case 8:
789 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
790 break;
791 default:
792 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
793 break;
794 }
795
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800796 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
797
798 return 0;
799}
800
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000801static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
802 struct rsnd_mod *mod)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800803{
804 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800805 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000806 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800807 int ret;
808
809 if (convert_rate)
Kuninori Morimotof708d942015-01-15 08:07:19 +0000810 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800811 runtime->rate,
812 convert_rate);
813 else
Kuninori Morimotof708d942015-01-15 08:07:19 +0000814 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800815
816 return ret;
817}
818
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800819static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000820 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000821 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800822{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800823 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800824 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000825 int irq = src->info->irq;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800826 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800827
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000828 if (irq > 0) {
829 /*
830 * IRQ is not supported on non-DT
831 * see
832 * rsnd_src_irq_enable_gen2()
833 */
834 ret = devm_request_irq(dev, irq,
835 rsnd_src_interrupt_gen2,
836 IRQF_SHARED,
837 dev_name(dev), mod);
838 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000839 return ret;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000840 }
841
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000842 ret = rsnd_dma_init(io,
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800843 rsnd_mod_to_dma(mod),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800844 src->info->dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700845
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800846 return ret;
847}
848
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800849static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000850 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000851 struct rsnd_priv *priv)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800852{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000853 rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800854
855 return 0;
856}
857
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800858static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000859 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000860 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800861{
862 int ret;
863
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000864 ret = rsnd_src_init(mod, priv);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800865 if (ret < 0)
866 return ret;
867
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000868 ret = rsnd_src_set_convert_rate_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800869 if (ret < 0)
870 return ret;
871
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000872 ret = rsnd_src_set_convert_timing_gen2(io, mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800873 if (ret < 0)
874 return ret;
875
876 return 0;
877}
878
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800879static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000880 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000881 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800882{
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000883 rsnd_dma_start(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800884
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000885 return _rsnd_src_start_gen2(mod, io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800886}
887
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800888static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000889 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000890 struct rsnd_priv *priv)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800891{
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000892 int ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800893
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000894 ret = _rsnd_src_stop_gen2(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800895
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000896 rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800897
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000898 return ret;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800899}
900
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000901static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
902 struct rsnd_mod *mod)
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000903{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000904 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
905 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto88c61cf2015-06-15 06:27:13 +0000906 u32 convert_rate = rsnd_src_convert_rate(io, src);
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000907 u32 fsrate;
908
909 if (!runtime)
910 return;
911
912 if (!convert_rate)
913 convert_rate = runtime->rate;
914
915 fsrate = 0x0400000 / convert_rate * runtime->rate;
916
917 /* update IFS */
918 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
919}
920
Kuninori Morimotob9bfe9d2015-09-10 06:49:12 +0000921static int rsnd_src_pcm_new_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000922 struct rsnd_dai_stream *io,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000923 struct snd_soc_pcm_runtime *rtd)
924{
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000925 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
926 struct rsnd_src *src = rsnd_mod_to_src(mod);
927 int ret;
928
929 /*
930 * enable SRC sync convert if possible
931 */
932
933 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000934 * SRC sync convert needs clock master
935 */
936 if (!rsnd_rdai_is_clk_master(rdai))
937 return 0;
938
939 /*
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000940 * enable sync convert
941 */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000942 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000943 rsnd_io_is_play(io) ?
944 "SRC Out Rate Switch" :
945 "SRC In Rate Switch",
946 rsnd_src_reconvert_update,
947 &src->sen, 1);
948 if (ret < 0)
949 return ret;
950
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000951 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto43cb6952015-04-01 04:15:16 +0000952 rsnd_io_is_play(io) ?
953 "SRC Out Rate" :
954 "SRC In Rate",
955 rsnd_src_reconvert_update,
956 &src->sync, 192000);
957
958 return ret;
959}
960
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800961static struct rsnd_mod_ops rsnd_src_gen2_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700962 .name = SRC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000963 .dma_req = rsnd_src_dma_req,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800964 .probe = rsnd_src_probe_gen2,
965 .remove = rsnd_src_remove_gen2,
966 .init = rsnd_src_init_gen2,
967 .quit = rsnd_src_quit,
968 .start = rsnd_src_start_gen2,
969 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto3b7843f2015-03-26 04:02:51 +0000970 .hw_params = rsnd_src_hw_params,
Kuninori Morimotob9bfe9d2015-09-10 06:49:12 +0000971 .pcm_new = rsnd_src_pcm_new_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800972};
973
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800974struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700975{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800976 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100977 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700978
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000979 return rsnd_mod_get((struct rsnd_src *)(priv->src) + id);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700980}
981
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700982static void rsnd_of_parse_src(struct platform_device *pdev,
983 const struct rsnd_of_data *of_data,
984 struct rsnd_priv *priv)
985{
986 struct device_node *src_node;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000987 struct device_node *np;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700988 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
989 struct rsnd_src_platform_info *src_info;
990 struct device *dev = &pdev->dev;
Kuninori Morimotocfcefe02015-01-08 01:52:36 +0000991 int nr, i;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700992
993 if (!of_data)
994 return;
995
Kuninori Morimoto82e76ed2015-02-20 10:30:22 +0000996 src_node = rsnd_src_of_node(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700997 if (!src_node)
998 return;
999
1000 nr = of_get_child_count(src_node);
1001 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -07001002 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001003
1004 src_info = devm_kzalloc(dev,
1005 sizeof(struct rsnd_src_platform_info) * nr,
1006 GFP_KERNEL);
1007 if (!src_info) {
1008 dev_err(dev, "src info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -07001009 goto rsnd_of_parse_src_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001010 }
1011
1012 info->src_info = src_info;
1013 info->src_info_nr = nr;
Kuninori Morimotof451e482014-05-22 23:24:59 -07001014
Kuninori Morimotocfcefe02015-01-08 01:52:36 +00001015 i = 0;
1016 for_each_child_of_node(src_node, np) {
1017 src_info[i].irq = irq_of_parse_and_map(np, 0);
1018
1019 i++;
1020 }
1021
Kuninori Morimotof451e482014-05-22 23:24:59 -07001022rsnd_of_parse_src_end:
1023 of_node_put(src_node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001024}
1025
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001026int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001027 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001028 struct rsnd_priv *priv)
1029{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -08001030 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001031 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001032 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -08001033 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -08001034 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001035 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001036 int i, nr, ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001037
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001038 ops = NULL;
Kuninori Morimoto43624952015-10-14 08:56:07 +00001039 if (rsnd_is_gen1(priv)) {
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001040 ops = &rsnd_src_gen1_ops;
Kuninori Morimoto43624952015-10-14 08:56:07 +00001041 dev_warn(dev, "Gen1 support will be removed soon\n");
1042 }
Kuninori Morimoto033e7ed2014-05-22 23:25:37 -07001043 if (rsnd_is_gen2(priv))
1044 ops = &rsnd_src_gen2_ops;
1045 if (!ops) {
1046 dev_err(dev, "unknown Generation\n");
1047 return -EIO;
1048 }
1049
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001050 rsnd_of_parse_src(pdev, of_data, priv);
1051
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001052 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001053 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001054 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001055 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -08001056 if (!nr)
1057 return 0;
1058
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001059 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
Kuninori Morimoto33363f72015-07-15 07:08:44 +00001060 if (!src)
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001061 return -ENOMEM;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001062
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001063 priv->src_nr = nr;
1064 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001065
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001066 for_each_rsnd_src(src, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001067 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1068 SRC_NAME, i);
Kuninori Morimotoef749402013-12-19 19:28:51 -08001069
1070 clk = devm_clk_get(dev, name);
1071 if (IS_ERR(clk))
1072 return PTR_ERR(clk);
1073
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001074 src->info = &info->src_info[i];
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001075
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001076 ret = rsnd_mod_init(priv, rsnd_mod_get(src), ops, clk, RSND_MOD_SRC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001077 if (ret)
1078 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -07001079 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001080
1081 return 0;
1082}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001083
1084void rsnd_src_remove(struct platform_device *pdev,
1085 struct rsnd_priv *priv)
1086{
1087 struct rsnd_src *src;
1088 int i;
1089
1090 for_each_rsnd_src(src, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001091 rsnd_mod_quit(rsnd_mod_get(src));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001092 }
1093}