blob: fefa6ad5de8b088d44fb9d27b05b2b85f8e3b3ec [file] [log] [blame]
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001/*
2 * Renesas R-Car SSIU/SSI support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/delay.h>
15#include "rsnd.h"
16#define RSND_SSI_NAME_SIZE 16
17
18/*
19 * SSICR
20 */
21#define FORCE (1 << 31) /* Fixed */
Kuninori Morimoto849fc822013-07-28 18:59:02 -070022#define DMEN (1 << 28) /* DMA Enable */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070023#define UIEN (1 << 27) /* Underflow Interrupt Enable */
24#define OIEN (1 << 26) /* Overflow Interrupt Enable */
25#define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
26#define DIEN (1 << 24) /* Data Interrupt Enable */
Kuninori Morimoto186fadc2015-11-30 08:54:03 +000027#define CHNL_4 (1 << 22) /* Channels */
28#define CHNL_6 (2 << 22) /* Channels */
29#define CHNL_8 (3 << 22) /* Channels */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070030#define DWL_8 (0 << 19) /* Data Word Length */
31#define DWL_16 (1 << 19) /* Data Word Length */
32#define DWL_18 (2 << 19) /* Data Word Length */
33#define DWL_20 (3 << 19) /* Data Word Length */
34#define DWL_22 (4 << 19) /* Data Word Length */
35#define DWL_24 (5 << 19) /* Data Word Length */
36#define DWL_32 (6 << 19) /* Data Word Length */
37
38#define SWL_32 (3 << 16) /* R/W System Word Length */
39#define SCKD (1 << 15) /* Serial Bit Clock Direction */
40#define SWSD (1 << 14) /* Serial WS Direction */
41#define SCKP (1 << 13) /* Serial Bit Clock Polarity */
42#define SWSP (1 << 12) /* Serial WS Polarity */
43#define SDTA (1 << 10) /* Serial Data Alignment */
Kuninori Morimotof46a93b2015-11-17 08:28:11 +000044#define PDTA (1 << 9) /* Parallel Data Alignment */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070045#define DEL (1 << 8) /* Serial Data Delay */
46#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
47#define TRMD (1 << 1) /* Transmit/Receive Mode Select */
48#define EN (1 << 0) /* SSI Module Enable */
49
50/*
51 * SSISR
52 */
53#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
54#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
55#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
56#define DIRQ (1 << 24) /* Data Interrupt Status Flag */
57
Kuninori Morimoto849fc822013-07-28 18:59:02 -070058/*
59 * SSIWSR
60 */
61#define CONT (1 << 8) /* WS Continue Function */
Kuninori Morimoto186fadc2015-11-30 08:54:03 +000062#define WS_MODE (1 << 0) /* WS Mode */
Kuninori Morimoto849fc822013-07-28 18:59:02 -070063
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070064#define SSI_NAME "ssi"
65
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070066struct rsnd_ssi {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070067 struct rsnd_mod mod;
Kuninori Morimoto940e9472015-10-26 08:42:25 +000068 struct rsnd_mod *dma;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070069
Kuninori Morimoto02534f22015-11-10 05:11:35 +000070 u32 flags;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070071 u32 cr_own;
72 u32 cr_clk;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000073 u32 cr_mode;
Kuninori Morimoto08bada22015-11-30 08:53:04 +000074 u32 wsr;
Kuninori Morimoto919567d2015-04-10 08:50:30 +000075 int chan;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000076 int rate;
Kuninori Morimoto02534f22015-11-10 05:11:35 +000077 int irq;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070078 unsigned int usrcnt;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070079};
80
Kuninori Morimoto02534f22015-11-10 05:11:35 +000081/* flags */
82#define RSND_SSI_CLK_PIN_SHARE (1 << 0)
83#define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
84
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070085#define for_each_rsnd_ssi(pos, priv, i) \
86 for (i = 0; \
87 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080088 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070089 i++)
90
Kuninori Morimoto02534f22015-11-10 05:11:35 +000091#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +000092#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
Kuninori Morimotodd27d802014-01-23 18:39:40 -080093#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070094#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto02534f22015-11-10 05:11:35 +000095#define rsnd_ssi_mode_flags(p) ((p)->flags)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000096#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
Kuninori Morimotoc308abe2016-02-09 07:04:09 +000097#define rsnd_ssi_is_multi_slave(mod, io) \
98 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +000099#define rsnd_ssi_is_run_mods(mod, io) \
100 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700101
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000102int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700103{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000104 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700105 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700106 int use_busif = 0;
107
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000108 if (!rsnd_ssi_is_dma_mode(mod))
109 return 0;
110
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700111 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
112 use_busif = 1;
113 if (rsnd_io_to_mod_src(io))
114 use_busif = 1;
115
116 return use_busif;
117}
118
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000119static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
120{
121 rsnd_mod_write(mod, SSISR, 0);
122}
123
124static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
125{
126 return rsnd_mod_read(mod, SSISR);
127}
128
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700129static void rsnd_ssi_status_check(struct rsnd_mod *mod,
130 u32 bit)
131{
132 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
133 struct device *dev = rsnd_priv_to_dev(priv);
134 u32 status;
135 int i;
136
137 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000138 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700139 if (status & bit)
140 return;
141
142 udelay(50);
143 }
144
Kuninori Morimoto1120dbf2016-02-18 08:14:09 +0000145 dev_warn(dev, "%s[%d] status check failed\n",
146 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700147}
148
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000149static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000150{
151 struct rsnd_mod *mod;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000152 enum rsnd_mod_type types[] = {
153 RSND_MOD_SSIM1,
154 RSND_MOD_SSIM2,
155 RSND_MOD_SSIM3,
156 };
157 int i, mask;
158
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000159 mask = 0;
160 for (i = 0; i < ARRAY_SIZE(types); i++) {
161 mod = rsnd_io_to_mod(io, types[i]);
162 if (!mod)
163 continue;
164
165 mask |= 1 << rsnd_mod_id(mod);
166 }
167
168 return mask;
169}
170
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000171static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
172{
173 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
174 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimoto019433d2017-11-01 07:16:58 +0000175 u32 mods;
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000176
Kuninori Morimoto019433d2017-11-01 07:16:58 +0000177 mods = rsnd_ssi_multi_slaves_runtime(io) |
178 1 << rsnd_mod_id(ssi_mod);
179
180 if (ssi_parent_mod)
181 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
182
183 return mods;
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000184}
185
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000186u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
187{
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000188 if (rsnd_runtime_is_ssi_multi(io))
189 return rsnd_ssi_multi_slaves(io);
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000190
191 return 0;
192}
193
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000194static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800195 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700196{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000197 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700198 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000199 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000200 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000201 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000202 int chan = rsnd_runtime_channel_for_ssi(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000203 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700204 int ssi_clk_mul_table[] = {
205 1, 2, 4, 8, 16, 6, 12,
206 };
207 unsigned int main_rate;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000208 unsigned int rate = rsnd_io_is_play(io) ?
209 rsnd_src_get_out_rate(priv, io) :
210 rsnd_src_get_in_rate(priv, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700211
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000212 if (!rsnd_rdai_is_clk_master(rdai))
213 return 0;
214
215 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
216 return 0;
217
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000218 if (rsnd_ssi_is_multi_slave(mod, io))
219 return 0;
220
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000221 if (ssi->usrcnt > 1) {
222 if (ssi->rate != rate) {
223 dev_err(dev, "SSI parent/child should use same rate\n");
224 return -EINVAL;
225 }
226
227 return 0;
228 }
229
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700230 /*
231 * Find best clock, and try to start ADG
232 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000233 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700234
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000235 /*
Kuninori Morimoto28fdd862017-03-22 04:02:43 +0000236 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
237 * with it is not allowed. (SSIWSR.WS_MODE with
238 * SSICR.CKDV = 000 is not allowed either).
239 * Skip it. See SSICR.CKDV
240 */
241 if (j == 0)
242 continue;
243
244 /*
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000245 * this driver is assuming that
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000246 * system word is 32bit x chan
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000247 * see rsnd_ssi_init()
248 */
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000249 main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700250
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000251 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
252 if (0 == ret) {
253 ssi->cr_clk = FORCE | SWL_32 |
254 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000255 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700256
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000257 ssi->rate = rate;
258
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000259 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
260 rsnd_mod_name(mod),
261 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700262
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000263 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700264 }
265 }
266
267 dev_err(dev, "unsupported clock rate\n");
268 return -EIO;
269}
270
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000271static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000272 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700273{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000274 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000275 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000276 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700277
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000278 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700279 return;
280
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000281 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
282 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700283
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000284 if (ssi->usrcnt > 1)
285 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700286
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000287 ssi->cr_clk = 0;
288 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700289
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000290 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700291}
292
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000293static void rsnd_ssi_config_init(struct rsnd_mod *mod,
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000294 struct rsnd_dai_stream *io)
295{
296 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
297 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000298 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000299 u32 cr_own;
300 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000301 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000302 int is_tdm;
303
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000304 is_tdm = rsnd_runtime_is_ssi_tdm(io);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000305
306 /*
307 * always use 32bit system word.
308 * see also rsnd_ssi_master_clk_enable()
309 */
310 cr_own = FORCE | SWL_32 | PDTA;
311
312 if (rdai->bit_clk_inv)
313 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000314 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000315 cr_own |= SWSP;
316 if (rdai->data_alignment)
317 cr_own |= SDTA;
318 if (rdai->sys_delay)
319 cr_own |= DEL;
320 if (rsnd_io_is_play(io))
321 cr_own |= TRMD;
322
323 switch (runtime->sample_bits) {
324 case 16:
325 cr_own |= DWL_16;
326 break;
327 case 32:
328 cr_own |= DWL_24;
329 break;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000330 }
331
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000332 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000333 cr_mode = UIEN | OIEN | /* over/under run */
334 DMEN; /* DMA : enable DMA */
335 } else {
336 cr_mode = DIEN; /* PIO : enable Data interrupt */
337 }
338
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000339 /*
340 * TDM Extend Mode
341 * see
342 * rsnd_ssiu_init_gen2()
343 */
344 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000345 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000346 wsr |= WS_MODE;
347 cr_own |= CHNL_8;
348 }
349
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000350 ssi->cr_own = cr_own;
351 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000352 ssi->wsr = wsr;
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000353}
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000354
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000355static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
356{
357 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
358
359 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
360 rsnd_mod_write(mod, SSICR, ssi->cr_own |
361 ssi->cr_clk |
362 ssi->cr_mode); /* without EN */
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000363}
364
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700365/*
366 * SSI mod common functions
367 */
368static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000369 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000370 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700371{
372 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000373 int ret;
374
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000375 if (!rsnd_ssi_is_run_mods(mod, io))
376 return 0;
377
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000378 ssi->usrcnt++;
379
380 rsnd_mod_power_on(mod);
381
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000382 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000383 if (ret < 0)
384 return ret;
385
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000386 if (!rsnd_ssi_is_parent(mod, io))
387 rsnd_ssi_config_init(mod, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700388
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000389 rsnd_ssi_register_setup(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000390
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000391 /* clear error status */
392 rsnd_ssi_status_clear(mod);
393
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700394 return 0;
395}
396
397static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000398 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000399 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700400{
401 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700402 struct device *dev = rsnd_priv_to_dev(priv);
403
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000404 if (!rsnd_ssi_is_run_mods(mod, io))
405 return 0;
406
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100407 if (!ssi->usrcnt) {
408 dev_err(dev, "%s[%d] usrcnt error\n",
409 rsnd_mod_name(mod), rsnd_mod_id(mod));
410 return -EIO;
411 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000412
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000413 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100414 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700415
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000416 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000417
418 rsnd_mod_power_off(mod);
419
420 ssi->usrcnt--;
421
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700422 return 0;
423}
424
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000425static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000426 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000427 struct snd_pcm_substream *substream,
428 struct snd_pcm_hw_params *params)
429{
430 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000431 int chan = params_channels(params);
432
433 /*
434 * Already working.
435 * It will happen if SSI has parent/child connection.
436 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000437 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000438 /*
439 * it is error if child <-> parent SSI uses
440 * different channels.
441 */
442 if (ssi->chan != chan)
443 return -EIO;
444 }
445
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000446 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000447
448 return 0;
449}
450
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000451static int rsnd_ssi_start(struct rsnd_mod *mod,
452 struct rsnd_dai_stream *io,
453 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000454{
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000455 if (!rsnd_ssi_is_run_mods(mod, io))
456 return 0;
457
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000458 /*
459 * EN will be set via SSIU :: SSI_CONTROL
460 * if Multi channel mode
461 */
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000462 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000463 return 0;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000464
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000465 rsnd_mod_bset(mod, SSICR, EN, EN);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000466
467 return 0;
468}
469
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000470static int rsnd_ssi_stop(struct rsnd_mod *mod,
471 struct rsnd_dai_stream *io,
472 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000473{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700474 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000475 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700476
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000477 if (!rsnd_ssi_is_run_mods(mod, io))
478 return 0;
479
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000480 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000481 * don't stop if not last user
482 * see also
483 * rsnd_ssi_start
484 * rsnd_ssi_interrupt
485 */
486 if (ssi->usrcnt > 1)
487 return 0;
488
489 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000490 * disable all IRQ,
491 * and, wait all data was sent
492 */
493 cr = ssi->cr_own |
494 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700495
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000496 rsnd_mod_write(mod, SSICR, cr | EN);
497 rsnd_ssi_status_check(mod, DIRQ);
498
499 /*
500 * disable SSI,
501 * and, wait idle state
502 */
503 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
504 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000505
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700506 return 0;
507}
508
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000509static int rsnd_ssi_irq(struct rsnd_mod *mod,
510 struct rsnd_dai_stream *io,
511 struct rsnd_priv *priv,
512 int enable)
513{
514 u32 val = 0;
515
516 if (rsnd_is_gen1(priv))
517 return 0;
518
519 if (rsnd_ssi_is_parent(mod, io))
520 return 0;
521
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000522 if (!rsnd_ssi_is_run_mods(mod, io))
523 return 0;
524
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000525 if (enable)
526 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
527
528 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
529
530 return 0;
531}
532
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000533static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
534 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000535{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000536 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000537 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000538 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000539 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000540 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000541
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000542 spin_lock(&priv->lock);
543
544 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000545 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000546 goto rsnd_ssi_interrupt_out;
547
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000548 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000549
550 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000551 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000552 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
553 u32 *buf = (u32 *)(runtime->dma_area +
554 rsnd_dai_pointer_offset(io, 0));
555
556 /*
557 * 8/16/32 data can be assesse to TDR/RDR register
558 * directly as 32bit data
559 * see rsnd_ssi_init()
560 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000561 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000562 rsnd_mod_write(mod, SSITDR, *buf);
563 else
564 *buf = rsnd_mod_read(mod, SSIRDR);
565
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000566 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000567 }
568
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000569 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000570 if (is_dma && (status & (UIRQ | OIRQ)))
571 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000572
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000573 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000574rsnd_ssi_interrupt_out:
575 spin_unlock(&priv->lock);
576
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000577 if (elapsed)
578 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000579
580 if (stop)
581 snd_pcm_stop_xrun(io->substream);
582
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000583}
584
585static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
586{
587 struct rsnd_mod *mod = data;
588
589 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000590
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000591 return IRQ_HANDLED;
592}
593
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000594/*
595 * SSI PIO
596 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000597static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000598 struct rsnd_dai_stream *io)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000599{
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000600 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
601 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
602
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000603 if (!__rsnd_ssi_is_pin_sharing(mod))
604 return;
605
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000606 if (!rsnd_rdai_is_clk_master(rdai))
607 return;
608
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000609 switch (rsnd_mod_id(mod)) {
610 case 1:
611 case 2:
612 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
613 break;
614 case 4:
615 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
616 break;
617 case 8:
618 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
619 break;
620 }
621}
622
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000623static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
624 struct rsnd_dai_stream *io,
625 struct snd_soc_pcm_runtime *rtd)
626{
627 /*
628 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
629 * and, pcm_new will be called after it.
630 * This function reuse pcm_new at this point.
631 */
632 rsnd_ssi_parent_attach(mod, io);
633
634 return 0;
635}
636
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000637static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
638 struct rsnd_dai_stream *io,
639 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000640{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000641 struct device *dev = rsnd_priv_to_dev(priv);
642 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000643 int ret;
644
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000645 /*
646 * SSIP/SSIU/IRQ are not needed on
647 * SSI Multi slaves
648 */
649 if (rsnd_ssi_is_multi_slave(mod, io))
650 return 0;
651
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000652 /*
653 * It can't judge ssi parent at this point
654 * see rsnd_ssi_pcm_new()
655 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000656
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000657 ret = rsnd_ssiu_attach(io, mod);
658 if (ret < 0)
659 return ret;
660
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000661 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000662 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000663 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000664 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000665
666 return ret;
667}
668
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700669static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700670 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000671 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700672 .init = rsnd_ssi_init,
673 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000674 .start = rsnd_ssi_start,
675 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000676 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000677 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000678 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700679};
680
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800681static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000682 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000683 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800684{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800685 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000686 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800687 int ret;
688
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000689 /*
690 * SSIP/SSIU/IRQ/DMA are not needed on
691 * SSI Multi slaves
692 */
693 if (rsnd_ssi_is_multi_slave(mod, io))
694 return 0;
695
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000696 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000697 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000698 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000699
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000700 /* SSI probe might be called many times in MUX multi path */
701 ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700702
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800703 return ret;
704}
705
706static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000707 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000708 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800709{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000710 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto1cb145c2017-08-09 02:16:20 +0000711 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000712 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000713 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000714
Kuninori Morimoto1cb145c2017-08-09 02:16:20 +0000715 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
716 if (pure_ssi_mod != mod)
Kuninori Morimoto24978c22017-05-16 01:48:24 +0000717 return 0;
718
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000719 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000720 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000721
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000722 return 0;
723}
724
725static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000726 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000727 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000728{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800729 struct device *dev = rsnd_priv_to_dev(priv);
730
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800731 /*
732 * fallback to PIO
733 *
734 * SSI .probe might be called again.
735 * see
736 * rsnd_rdai_continuance_probe()
737 */
738 mod->ops = &rsnd_ssi_pio_ops;
739
740 dev_info(dev, "%s[%d] fallback to PIO mode\n",
741 rsnd_mod_name(mod), rsnd_mod_id(mod));
742
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800743 return 0;
744}
745
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000746static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
747 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700748{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000749 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000750 int is_play = rsnd_io_is_play(io);
751 char *name;
752
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000753 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000754 name = is_play ? "rxu" : "txu";
755 else
756 name = is_play ? "rx" : "tx";
757
758 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
759 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700760}
761
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700762static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700763 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000764 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800765 .probe = rsnd_ssi_dma_probe,
766 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700767 .init = rsnd_ssi_init,
768 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000769 .start = rsnd_ssi_start,
770 .stop = rsnd_ssi_stop,
Kuninori Morimotoc8e969a2016-02-18 08:16:43 +0000771 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000772 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000773 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000774 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700775};
776
Kuninori Morimoto05795412014-11-27 08:05:01 +0000777int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
778{
779 return mod->ops == &rsnd_ssi_dma_ops;
780}
781
782
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700783/*
784 * Non SSI
785 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700786static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700787 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700788};
789
790/*
791 * ssi mod function
792 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000793static void rsnd_ssi_connect(struct rsnd_mod *mod,
794 struct rsnd_dai_stream *io)
795{
796 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
797 enum rsnd_mod_type types[] = {
798 RSND_MOD_SSI,
799 RSND_MOD_SSIM1,
800 RSND_MOD_SSIM2,
801 RSND_MOD_SSIM3,
802 };
803 enum rsnd_mod_type type;
804 int i;
805
806 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
807 for (i = 0; i < ARRAY_SIZE(types); i++) {
808 type = types[i];
809 if (!rsnd_io_to_mod(io, type)) {
810 rsnd_dai_connect(mod, io, type);
811 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
812 return;
813 }
814 }
815}
816
817void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
818 struct device_node *playback,
819 struct device_node *capture)
820{
821 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
822 struct device_node *node;
823 struct device_node *np;
824 struct rsnd_mod *mod;
825 int i;
826
827 node = rsnd_ssi_of_node(priv);
828 if (!node)
829 return;
830
831 i = 0;
832 for_each_child_of_node(node, np) {
833 mod = rsnd_ssi_mod_get(priv, i);
834 if (np == playback)
835 rsnd_ssi_connect(mod, &rdai->playback);
836 if (np == capture)
837 rsnd_ssi_connect(mod, &rdai->capture);
838 i++;
839 }
840
841 of_node_put(node);
842}
843
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700844struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
845{
Takashi Iwai8b147192013-11-05 18:40:05 +0100846 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
847 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700848
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000849 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700850}
851
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000852int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800853{
854 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
855
856 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
857}
858
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000859static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
860 struct rsnd_mod *mod,
861 enum rsnd_mod_type type)
862{
863 /*
864 * SSIP (= SSI parent) needs to be special, otherwise,
865 * 2nd SSI might doesn't start. see also rsnd_mod_call()
866 *
867 * We can't include parent SSI status on SSI, because we don't know
868 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
869 * ex) trouble case
870 * Playback: SSI0
871 * Capture : SSI1 (needs SSI0)
872 *
873 * 1) start Capture -> SSI0/SSI1 are started.
874 * 2) start Playback -> SSI0 doesn't work, because it is already
875 * marked as "started" on 1)
876 *
877 * OTOH, using each mod's status is good for MUX case.
878 * It doesn't need to start in 2nd start
879 * ex)
880 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
881 * |
882 * IO-1: SRC1 -> CTU2 -+
883 *
884 * 1) start IO-0 -> start SSI0
885 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
886 * already started on 1)
887 */
888 if (type == RSND_MOD_SSIP)
889 return &io->parent_ssi_status;
890
891 return rsnd_mod_get_status(io, mod, type);
892}
893
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000894int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700895{
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000896 struct device_node *node;
897 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700898 struct device *dev = rsnd_priv_to_dev(priv);
899 struct rsnd_mod_ops *ops;
900 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700901 struct rsnd_ssi *ssi;
902 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000903 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700904
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000905 node = rsnd_ssi_of_node(priv);
906 if (!node)
907 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700908
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000909 nr = of_get_child_count(node);
910 if (!nr) {
911 ret = -EINVAL;
912 goto rsnd_ssi_probe_done;
913 }
914
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800915 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000916 if (!ssi) {
917 ret = -ENOMEM;
918 goto rsnd_ssi_probe_done;
919 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700920
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800921 priv->ssi = ssi;
922 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700923
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000924 i = 0;
925 for_each_child_of_node(node, np) {
926 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700927
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700928 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
929 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700930
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800931 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000932 if (IS_ERR(clk)) {
933 ret = PTR_ERR(clk);
934 goto rsnd_ssi_probe_done;
935 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700936
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000937 if (of_get_property(np, "shared-pin", NULL))
938 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
939
940 if (of_get_property(np, "no-busif", NULL))
941 ssi->flags |= RSND_SSI_NO_BUSIF;
942
943 ssi->irq = irq_of_parse_and_map(np, 0);
944 if (!ssi->irq) {
945 ret = -EINVAL;
946 goto rsnd_ssi_probe_done;
947 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700948
949 ops = &rsnd_ssi_non_ops;
Julia Lawall51930292016-08-05 10:56:51 +0200950 if (of_property_read_bool(np, "pio-transfer"))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800951 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000952 else
953 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700954
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000955 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000956 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000957 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000958 goto rsnd_ssi_probe_done;
959
960 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700961 }
962
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000963 ret = 0;
964
965rsnd_ssi_probe_done:
966 of_node_put(node);
967
968 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700969}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000970
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000971void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000972{
973 struct rsnd_ssi *ssi;
974 int i;
975
976 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000977 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000978 }
979}