blob: b5c6f0c274c38cb13dd094b2c1888ef1f9be0789 [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 Morimotoae5c3222013-07-21 21:36:57 -070099
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000100int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700101{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000102 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700103 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700104 int use_busif = 0;
105
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000106 if (!rsnd_ssi_is_dma_mode(mod))
107 return 0;
108
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700109 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
110 use_busif = 1;
111 if (rsnd_io_to_mod_src(io))
112 use_busif = 1;
113
114 return use_busif;
115}
116
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000117static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
118{
119 rsnd_mod_write(mod, SSISR, 0);
120}
121
122static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
123{
124 return rsnd_mod_read(mod, SSISR);
125}
126
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700127static void rsnd_ssi_status_check(struct rsnd_mod *mod,
128 u32 bit)
129{
130 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
131 struct device *dev = rsnd_priv_to_dev(priv);
132 u32 status;
133 int i;
134
135 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000136 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700137 if (status & bit)
138 return;
139
140 udelay(50);
141 }
142
Kuninori Morimoto1120dbf2016-02-18 08:14:09 +0000143 dev_warn(dev, "%s[%d] status check failed\n",
144 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700145}
146
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000147static int rsnd_ssi_irq(struct rsnd_mod *mod,
148 struct rsnd_dai_stream *io,
149 struct rsnd_priv *priv,
150 int enable)
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000151{
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000152 u32 val = 0;
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000153
154 if (rsnd_is_gen1(priv))
155 return 0;
156
Kuninori Morimoto5bf5d8f2016-02-18 08:16:04 +0000157 if (rsnd_ssi_is_parent(mod, io))
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000158 return 0;
159
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000160 if (enable)
161 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
162
163 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000164
165 return 0;
166}
167
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000168u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
169{
170 struct rsnd_mod *mod;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000171 enum rsnd_mod_type types[] = {
172 RSND_MOD_SSIM1,
173 RSND_MOD_SSIM2,
174 RSND_MOD_SSIM3,
175 };
176 int i, mask;
177
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000178 mask = 0;
179 for (i = 0; i < ARRAY_SIZE(types); i++) {
180 mod = rsnd_io_to_mod(io, types[i]);
181 if (!mod)
182 continue;
183
184 mask |= 1 << rsnd_mod_id(mod);
185 }
186
187 return mask;
188}
189
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000190static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800191 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700192{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000193 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800194 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700195 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000196 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000197 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000198 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000199 int slots = rsnd_get_slot_width(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000200 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700201 int ssi_clk_mul_table[] = {
202 1, 2, 4, 8, 16, 6, 12,
203 };
204 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800205 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700206
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000207 if (!rsnd_rdai_is_clk_master(rdai))
208 return 0;
209
210 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
211 return 0;
212
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000213 if (rsnd_ssi_is_multi_slave(mod, io))
214 return 0;
215
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000216 if (ssi->usrcnt > 1) {
217 if (ssi->rate != rate) {
218 dev_err(dev, "SSI parent/child should use same rate\n");
219 return -EINVAL;
220 }
221
222 return 0;
223 }
224
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700225 /*
226 * Find best clock, and try to start ADG
227 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000228 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700229
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000230 /*
231 * this driver is assuming that
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000232 * system word is 32bit x slots
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000233 * see rsnd_ssi_init()
234 */
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000235 main_rate = rate * 32 * slots * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700236
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000237 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
238 if (0 == ret) {
239 ssi->cr_clk = FORCE | SWL_32 |
240 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000241 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700242
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000243 ssi->rate = rate;
244
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000245 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
246 rsnd_mod_name(mod),
247 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700248
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000249 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700250 }
251 }
252
253 dev_err(dev, "unsupported clock rate\n");
254 return -EIO;
255}
256
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000257static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000258 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700259{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000260 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000261 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000262 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700263
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000264 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700265 return;
266
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000267 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
268 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700269
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000270 if (ssi->usrcnt > 1)
271 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700272
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000273 ssi->cr_clk = 0;
274 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700275
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000276 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700277}
278
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000279static void rsnd_ssi_config_init(struct rsnd_mod *mod,
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000280 struct rsnd_dai_stream *io)
281{
282 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
283 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000284 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000285 u32 cr_own;
286 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000287 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000288 int is_tdm;
289
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000290 is_tdm = (rsnd_get_slot_width(io) >= 6) ? 1 : 0;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000291
292 /*
293 * always use 32bit system word.
294 * see also rsnd_ssi_master_clk_enable()
295 */
296 cr_own = FORCE | SWL_32 | PDTA;
297
298 if (rdai->bit_clk_inv)
299 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000300 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000301 cr_own |= SWSP;
302 if (rdai->data_alignment)
303 cr_own |= SDTA;
304 if (rdai->sys_delay)
305 cr_own |= DEL;
306 if (rsnd_io_is_play(io))
307 cr_own |= TRMD;
308
309 switch (runtime->sample_bits) {
310 case 16:
311 cr_own |= DWL_16;
312 break;
313 case 32:
314 cr_own |= DWL_24;
315 break;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000316 }
317
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000318 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000319 cr_mode = UIEN | OIEN | /* over/under run */
320 DMEN; /* DMA : enable DMA */
321 } else {
322 cr_mode = DIEN; /* PIO : enable Data interrupt */
323 }
324
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000325 /*
326 * TDM Extend Mode
327 * see
328 * rsnd_ssiu_init_gen2()
329 */
330 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000331 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000332 wsr |= WS_MODE;
333 cr_own |= CHNL_8;
334 }
335
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000336 ssi->cr_own = cr_own;
337 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000338 ssi->wsr = wsr;
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000339}
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000340
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000341static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
342{
343 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
344
345 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
346 rsnd_mod_write(mod, SSICR, ssi->cr_own |
347 ssi->cr_clk |
348 ssi->cr_mode); /* without EN */
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000349}
350
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700351/*
352 * SSI mod common functions
353 */
354static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000355 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000356 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700357{
358 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000359 int ret;
360
361 ssi->usrcnt++;
362
363 rsnd_mod_power_on(mod);
364
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000365 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000366 if (ret < 0)
367 return ret;
368
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000369 if (!rsnd_ssi_is_parent(mod, io))
370 rsnd_ssi_config_init(mod, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700371
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000372 rsnd_ssi_register_setup(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000373
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000374 /* clear error status */
375 rsnd_ssi_status_clear(mod);
376
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700377 return 0;
378}
379
380static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000381 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000382 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700383{
384 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700385 struct device *dev = rsnd_priv_to_dev(priv);
386
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100387 if (!ssi->usrcnt) {
388 dev_err(dev, "%s[%d] usrcnt error\n",
389 rsnd_mod_name(mod), rsnd_mod_id(mod));
390 return -EIO;
391 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000392
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000393 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100394 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700395
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000396 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000397
398 rsnd_mod_power_off(mod);
399
400 ssi->usrcnt--;
401
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700402 return 0;
403}
404
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000405static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000406 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000407 struct snd_pcm_substream *substream,
408 struct snd_pcm_hw_params *params)
409{
410 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000411 int chan = params_channels(params);
412
413 /*
414 * Already working.
415 * It will happen if SSI has parent/child connection.
416 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000417 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000418 /*
419 * it is error if child <-> parent SSI uses
420 * different channels.
421 */
422 if (ssi->chan != chan)
423 return -EIO;
424 }
425
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000426 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000427
428 return 0;
429}
430
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000431static int rsnd_ssi_start(struct rsnd_mod *mod,
432 struct rsnd_dai_stream *io,
433 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000434{
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000435 /*
436 * EN will be set via SSIU :: SSI_CONTROL
437 * if Multi channel mode
438 */
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000439 if (rsnd_ssi_multi_slaves(io))
440 return 0;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000441
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000442 rsnd_mod_bset(mod, SSICR, EN, EN);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000443
444 return 0;
445}
446
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000447static int rsnd_ssi_stop(struct rsnd_mod *mod,
448 struct rsnd_dai_stream *io,
449 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000450{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700451 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000452 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700453
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000454 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000455 * don't stop if not last user
456 * see also
457 * rsnd_ssi_start
458 * rsnd_ssi_interrupt
459 */
460 if (ssi->usrcnt > 1)
461 return 0;
462
463 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000464 * disable all IRQ,
465 * and, wait all data was sent
466 */
467 cr = ssi->cr_own |
468 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700469
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000470 rsnd_mod_write(mod, SSICR, cr | EN);
471 rsnd_ssi_status_check(mod, DIRQ);
472
473 /*
474 * disable SSI,
475 * and, wait idle state
476 */
477 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
478 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000479
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700480 return 0;
481}
482
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000483static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
484 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000485{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000486 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000487 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000488 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000489 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000490 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000491
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000492 spin_lock(&priv->lock);
493
494 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000495 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000496 goto rsnd_ssi_interrupt_out;
497
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000498 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000499
500 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000501 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000502 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
503 u32 *buf = (u32 *)(runtime->dma_area +
504 rsnd_dai_pointer_offset(io, 0));
505
506 /*
507 * 8/16/32 data can be assesse to TDR/RDR register
508 * directly as 32bit data
509 * see rsnd_ssi_init()
510 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000511 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000512 rsnd_mod_write(mod, SSITDR, *buf);
513 else
514 *buf = rsnd_mod_read(mod, SSIRDR);
515
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000516 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000517 }
518
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000519 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000520 if (is_dma && (status & (UIRQ | OIRQ)))
521 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000522
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000523 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000524rsnd_ssi_interrupt_out:
525 spin_unlock(&priv->lock);
526
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000527 if (elapsed)
528 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000529
530 if (stop)
531 snd_pcm_stop_xrun(io->substream);
532
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000533}
534
535static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
536{
537 struct rsnd_mod *mod = data;
538
539 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000540
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000541 return IRQ_HANDLED;
542}
543
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000544/*
545 * SSI PIO
546 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000547static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000548 struct rsnd_dai_stream *io)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000549{
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000550 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
551 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
552
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000553 if (!__rsnd_ssi_is_pin_sharing(mod))
554 return;
555
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000556 if (!rsnd_rdai_is_clk_master(rdai))
557 return;
558
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000559 switch (rsnd_mod_id(mod)) {
560 case 1:
561 case 2:
562 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
563 break;
564 case 4:
565 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
566 break;
567 case 8:
568 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
569 break;
570 }
571}
572
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000573static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
574 struct rsnd_dai_stream *io,
575 struct snd_soc_pcm_runtime *rtd)
576{
577 /*
578 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
579 * and, pcm_new will be called after it.
580 * This function reuse pcm_new at this point.
581 */
582 rsnd_ssi_parent_attach(mod, io);
583
584 return 0;
585}
586
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000587static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
588 struct rsnd_dai_stream *io,
589 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000590{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000591 struct device *dev = rsnd_priv_to_dev(priv);
592 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000593 int ret;
594
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000595 /*
596 * SSIP/SSIU/IRQ are not needed on
597 * SSI Multi slaves
598 */
599 if (rsnd_ssi_is_multi_slave(mod, io))
600 return 0;
601
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000602 /*
603 * It can't judge ssi parent at this point
604 * see rsnd_ssi_pcm_new()
605 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000606
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000607 ret = rsnd_ssiu_attach(io, mod);
608 if (ret < 0)
609 return ret;
610
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000611 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000612 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000613 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000614 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000615
616 return ret;
617}
618
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700619static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700620 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000621 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700622 .init = rsnd_ssi_init,
623 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000624 .start = rsnd_ssi_start,
625 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000626 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000627 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000628 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700629};
630
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800631static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000632 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000633 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800634{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800635 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000636 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800637 int ret;
638
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000639 /*
640 * SSIP/SSIU/IRQ/DMA are not needed on
641 * SSI Multi slaves
642 */
643 if (rsnd_ssi_is_multi_slave(mod, io))
644 return 0;
645
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000646 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000647 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000648 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000649
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000650 /* SSI probe might be called many times in MUX multi path */
651 ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700652
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800653 return ret;
654}
655
656static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000657 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000658 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800659{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000660 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
661 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000662 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000663
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000664 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000665 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000666
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000667 return 0;
668}
669
670static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000671 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000672 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000673{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800674 struct device *dev = rsnd_priv_to_dev(priv);
675
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800676 /*
677 * fallback to PIO
678 *
679 * SSI .probe might be called again.
680 * see
681 * rsnd_rdai_continuance_probe()
682 */
683 mod->ops = &rsnd_ssi_pio_ops;
684
685 dev_info(dev, "%s[%d] fallback to PIO mode\n",
686 rsnd_mod_name(mod), rsnd_mod_id(mod));
687
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800688 return 0;
689}
690
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000691static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
692 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700693{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000694 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000695 int is_play = rsnd_io_is_play(io);
696 char *name;
697
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000698 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000699 name = is_play ? "rxu" : "txu";
700 else
701 name = is_play ? "rx" : "tx";
702
703 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
704 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700705}
706
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700707static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700708 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000709 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800710 .probe = rsnd_ssi_dma_probe,
711 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700712 .init = rsnd_ssi_init,
713 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000714 .start = rsnd_ssi_start,
715 .stop = rsnd_ssi_stop,
Kuninori Morimotoc8e969a2016-02-18 08:16:43 +0000716 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000717 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000718 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000719 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700720};
721
Kuninori Morimoto05795412014-11-27 08:05:01 +0000722int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
723{
724 return mod->ops == &rsnd_ssi_dma_ops;
725}
726
727
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700728/*
729 * Non SSI
730 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700731static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700732 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700733};
734
735/*
736 * ssi mod function
737 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000738static void rsnd_ssi_connect(struct rsnd_mod *mod,
739 struct rsnd_dai_stream *io)
740{
741 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
742 enum rsnd_mod_type types[] = {
743 RSND_MOD_SSI,
744 RSND_MOD_SSIM1,
745 RSND_MOD_SSIM2,
746 RSND_MOD_SSIM3,
747 };
748 enum rsnd_mod_type type;
749 int i;
750
751 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
752 for (i = 0; i < ARRAY_SIZE(types); i++) {
753 type = types[i];
754 if (!rsnd_io_to_mod(io, type)) {
755 rsnd_dai_connect(mod, io, type);
756 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
757 return;
758 }
759 }
760}
761
762void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
763 struct device_node *playback,
764 struct device_node *capture)
765{
766 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
767 struct device_node *node;
768 struct device_node *np;
769 struct rsnd_mod *mod;
770 int i;
771
772 node = rsnd_ssi_of_node(priv);
773 if (!node)
774 return;
775
776 i = 0;
777 for_each_child_of_node(node, np) {
778 mod = rsnd_ssi_mod_get(priv, i);
779 if (np == playback)
780 rsnd_ssi_connect(mod, &rdai->playback);
781 if (np == capture)
782 rsnd_ssi_connect(mod, &rdai->capture);
783 i++;
784 }
785
786 of_node_put(node);
787}
788
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700789struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
790{
Takashi Iwai8b147192013-11-05 18:40:05 +0100791 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
792 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700793
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000794 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700795}
796
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000797int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800798{
799 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
800
801 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
802}
803
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000804static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
805 struct rsnd_mod *mod,
806 enum rsnd_mod_type type)
807{
808 /*
809 * SSIP (= SSI parent) needs to be special, otherwise,
810 * 2nd SSI might doesn't start. see also rsnd_mod_call()
811 *
812 * We can't include parent SSI status on SSI, because we don't know
813 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
814 * ex) trouble case
815 * Playback: SSI0
816 * Capture : SSI1 (needs SSI0)
817 *
818 * 1) start Capture -> SSI0/SSI1 are started.
819 * 2) start Playback -> SSI0 doesn't work, because it is already
820 * marked as "started" on 1)
821 *
822 * OTOH, using each mod's status is good for MUX case.
823 * It doesn't need to start in 2nd start
824 * ex)
825 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
826 * |
827 * IO-1: SRC1 -> CTU2 -+
828 *
829 * 1) start IO-0 -> start SSI0
830 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
831 * already started on 1)
832 */
833 if (type == RSND_MOD_SSIP)
834 return &io->parent_ssi_status;
835
836 return rsnd_mod_get_status(io, mod, type);
837}
838
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000839int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700840{
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000841 struct device_node *node;
842 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700843 struct device *dev = rsnd_priv_to_dev(priv);
844 struct rsnd_mod_ops *ops;
845 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700846 struct rsnd_ssi *ssi;
847 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000848 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700849
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000850 node = rsnd_ssi_of_node(priv);
851 if (!node)
852 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700853
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000854 nr = of_get_child_count(node);
855 if (!nr) {
856 ret = -EINVAL;
857 goto rsnd_ssi_probe_done;
858 }
859
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800860 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000861 if (!ssi) {
862 ret = -ENOMEM;
863 goto rsnd_ssi_probe_done;
864 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700865
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800866 priv->ssi = ssi;
867 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700868
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000869 i = 0;
870 for_each_child_of_node(node, np) {
871 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700872
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700873 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
874 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700875
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800876 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000877 if (IS_ERR(clk)) {
878 ret = PTR_ERR(clk);
879 goto rsnd_ssi_probe_done;
880 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700881
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000882 if (of_get_property(np, "shared-pin", NULL))
883 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
884
885 if (of_get_property(np, "no-busif", NULL))
886 ssi->flags |= RSND_SSI_NO_BUSIF;
887
888 ssi->irq = irq_of_parse_and_map(np, 0);
889 if (!ssi->irq) {
890 ret = -EINVAL;
891 goto rsnd_ssi_probe_done;
892 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700893
894 ops = &rsnd_ssi_non_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000895 if (of_get_property(np, "pio-transfer", NULL))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800896 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000897 else
898 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700899
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000900 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000901 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000902 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000903 goto rsnd_ssi_probe_done;
904
905 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700906 }
907
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000908 ret = 0;
909
910rsnd_ssi_probe_done:
911 of_node_put(node);
912
913 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700914}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000915
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000916void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000917{
918 struct rsnd_ssi *ssi;
919 int i;
920
921 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000922 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000923 }
924}