blob: 90674137aa9061e4812934261760cfa8a3ef5a85 [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 Morimotoae5c3222013-07-21 21:36:57 -070077 int err;
Kuninori Morimoto02534f22015-11-10 05:11:35 +000078 int irq;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070079 unsigned int usrcnt;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070080};
81
Kuninori Morimoto02534f22015-11-10 05:11:35 +000082/* flags */
83#define RSND_SSI_CLK_PIN_SHARE (1 << 0)
84#define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
85
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070086#define for_each_rsnd_ssi(pos, priv, i) \
87 for (i = 0; \
88 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080089 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070090 i++)
91
Kuninori Morimoto02534f22015-11-10 05:11:35 +000092#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +000093#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
Kuninori Morimotodd27d802014-01-23 18:39:40 -080094#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070095#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto02534f22015-11-10 05:11:35 +000096#define rsnd_ssi_mode_flags(p) ((p)->flags)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000097#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000098#define rsnd_ssi_is_multi_slave(ssi, io) ((mod) != rsnd_io_to_mod_ssi(io))
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
143 dev_warn(dev, "status check failed\n");
144}
145
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000146static int rsnd_ssi_irq_enable(struct rsnd_mod *ssi_mod)
147{
148 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
149
150 if (rsnd_is_gen1(priv))
151 return 0;
152
153 /* enable SSI interrupt if Gen2 */
154 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
155 rsnd_ssi_is_dma_mode(ssi_mod) ?
156 0x0e000000 : 0x0f000000);
157
158 return 0;
159}
160
161static int rsnd_ssi_irq_disable(struct rsnd_mod *ssi_mod)
162{
163 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
164
165 if (rsnd_is_gen1(priv))
166 return 0;
167
168 /* disable SSI interrupt if Gen2 */
169 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
170
171 return 0;
172}
173
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000174u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
175{
176 struct rsnd_mod *mod;
177 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
178 struct rsnd_priv *priv = rsnd_io_to_priv(io);
179 struct device *dev = rsnd_priv_to_dev(priv);
180 enum rsnd_mod_type types[] = {
181 RSND_MOD_SSIM1,
182 RSND_MOD_SSIM2,
183 RSND_MOD_SSIM3,
184 };
185 int i, mask;
186
187 switch (runtime->channels) {
188 case 2: /* Multi channel is not needed for Stereo */
189 return 0;
190 case 6:
191 break;
192 default:
193 dev_err(dev, "unsupported channel\n");
194 return 0;
195 }
196
197 mask = 0;
198 for (i = 0; i < ARRAY_SIZE(types); i++) {
199 mod = rsnd_io_to_mod(io, types[i]);
200 if (!mod)
201 continue;
202
203 mask |= 1 << rsnd_mod_id(mod);
204 }
205
206 return mask;
207}
208
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700209static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800210 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700211{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000212 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800213 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700214 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000215 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000216 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000217 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000218 int slots = rsnd_get_slot_width(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000219 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700220 int ssi_clk_mul_table[] = {
221 1, 2, 4, 8, 16, 6, 12,
222 };
223 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800224 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700225
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000226 if (!rsnd_rdai_is_clk_master(rdai))
227 return 0;
228
229 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
230 return 0;
231
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000232 if (rsnd_ssi_is_multi_slave(mod, io))
233 return 0;
234
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000235 if (ssi->usrcnt > 1) {
236 if (ssi->rate != rate) {
237 dev_err(dev, "SSI parent/child should use same rate\n");
238 return -EINVAL;
239 }
240
241 return 0;
242 }
243
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700244 /*
245 * Find best clock, and try to start ADG
246 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000247 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700248
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000249 /*
250 * this driver is assuming that
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000251 * system word is 32bit x slots
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000252 * see rsnd_ssi_init()
253 */
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000254 main_rate = rate * 32 * slots * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700255
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000256 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
257 if (0 == ret) {
258 ssi->cr_clk = FORCE | SWL_32 |
259 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000260 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700261
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000262 ssi->rate = rate;
263
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000264 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
265 rsnd_mod_name(mod),
266 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700267
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000268 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700269 }
270 }
271
272 dev_err(dev, "unsupported clock rate\n");
273 return -EIO;
274}
275
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000276static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi,
277 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700278{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000279 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000280 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000281 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700282
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000283 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700284 return;
285
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000286 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
287 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700288
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000289 if (ssi->usrcnt > 1)
290 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700291
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000292 ssi->cr_clk = 0;
293 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700294
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000295 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700296}
297
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000298static int rsnd_ssi_config_init(struct rsnd_ssi *ssi,
299 struct rsnd_dai_stream *io)
300{
301 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
302 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
303 u32 cr_own;
304 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000305 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000306 int is_tdm;
307
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000308 is_tdm = (rsnd_get_slot_width(io) >= 6) ? 1 : 0;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000309
310 /*
311 * always use 32bit system word.
312 * see also rsnd_ssi_master_clk_enable()
313 */
314 cr_own = FORCE | SWL_32 | PDTA;
315
316 if (rdai->bit_clk_inv)
317 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000318 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000319 cr_own |= SWSP;
320 if (rdai->data_alignment)
321 cr_own |= SDTA;
322 if (rdai->sys_delay)
323 cr_own |= DEL;
324 if (rsnd_io_is_play(io))
325 cr_own |= TRMD;
326
327 switch (runtime->sample_bits) {
328 case 16:
329 cr_own |= DWL_16;
330 break;
331 case 32:
332 cr_own |= DWL_24;
333 break;
334 default:
335 return -EINVAL;
336 }
337
338 if (rsnd_ssi_is_dma_mode(rsnd_mod_get(ssi))) {
339 cr_mode = UIEN | OIEN | /* over/under run */
340 DMEN; /* DMA : enable DMA */
341 } else {
342 cr_mode = DIEN; /* PIO : enable Data interrupt */
343 }
344
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000345 /*
346 * TDM Extend Mode
347 * see
348 * rsnd_ssiu_init_gen2()
349 */
350 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000351 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000352 wsr |= WS_MODE;
353 cr_own |= CHNL_8;
354 }
355
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000356 ssi->cr_own = cr_own;
357 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000358 ssi->wsr = wsr;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000359
360 return 0;
361}
362
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700363/*
364 * SSI mod common functions
365 */
366static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000367 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000368 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700369{
370 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000371 int ret;
372
373 ssi->usrcnt++;
374
375 rsnd_mod_power_on(mod);
376
377 ret = rsnd_ssi_master_clk_start(ssi, io);
378 if (ret < 0)
379 return ret;
380
381 if (rsnd_ssi_is_parent(mod, io))
382 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700383
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000384 ret = rsnd_ssi_config_init(ssi, io);
385 if (ret < 0)
386 return ret;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000387
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700388 ssi->err = -1; /* ignore 1st error */
389
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000390 /* clear error status */
391 rsnd_ssi_status_clear(mod);
392
393 rsnd_ssi_irq_enable(mod);
394
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700395 return 0;
396}
397
398static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000399 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000400 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700401{
402 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700403 struct device *dev = rsnd_priv_to_dev(priv);
404
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100405 if (!ssi->usrcnt) {
406 dev_err(dev, "%s[%d] usrcnt error\n",
407 rsnd_mod_name(mod), rsnd_mod_id(mod));
408 return -EIO;
409 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000410
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100411 if (!rsnd_ssi_is_parent(mod, io)) {
412 if (ssi->err > 0)
413 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
414 rsnd_mod_name(mod), rsnd_mod_id(mod),
415 ssi->err);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700416
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100417 ssi->cr_own = 0;
418 ssi->err = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700419
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100420 rsnd_ssi_irq_disable(mod);
421 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000422
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000423 rsnd_ssi_master_clk_stop(ssi, io);
424
425 rsnd_mod_power_off(mod);
426
427 ssi->usrcnt--;
428
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700429 return 0;
430}
431
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000432static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000433 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000434 struct snd_pcm_substream *substream,
435 struct snd_pcm_hw_params *params)
436{
437 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000438 int chan = params_channels(params);
439
440 /*
441 * Already working.
442 * It will happen if SSI has parent/child connection.
443 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000444 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000445 /*
446 * it is error if child <-> parent SSI uses
447 * different channels.
448 */
449 if (ssi->chan != chan)
450 return -EIO;
451 }
452
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000453 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000454
455 return 0;
456}
457
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000458static u32 rsnd_ssi_record_error(struct rsnd_ssi *ssi)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700459{
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000460 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000461 u32 status = rsnd_ssi_status_get(mod);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000462
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700463 /* under/over flow error */
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000464 if (status & (UIRQ | OIRQ))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700465 ssi->err++;
466
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000467 return status;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700468}
469
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000470static int __rsnd_ssi_start(struct rsnd_mod *mod,
471 struct rsnd_dai_stream *io,
472 struct rsnd_priv *priv)
473{
474 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
475 u32 cr;
476
477 cr = ssi->cr_own |
478 ssi->cr_clk |
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000479 ssi->cr_mode;
480
481 /*
482 * EN will be set via SSIU :: SSI_CONTROL
483 * if Multi channel mode
484 */
485 if (!rsnd_ssi_multi_slaves(io))
486 cr |= EN;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000487
488 rsnd_mod_write(mod, SSICR, cr);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000489 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000490
491 return 0;
492}
493
Kuninori Morimoto49229852014-11-27 08:07:17 +0000494static int rsnd_ssi_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000495 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000496 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700497{
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000498 /*
499 * no limit to start
500 * see also
501 * rsnd_ssi_stop
502 * rsnd_ssi_interrupt
503 */
504 return __rsnd_ssi_start(mod, io, priv);
505}
506
507static int __rsnd_ssi_stop(struct rsnd_mod *mod,
508 struct rsnd_dai_stream *io,
509 struct rsnd_priv *priv)
510{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700511 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000512 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700513
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000514 /*
515 * disable all IRQ,
516 * and, wait all data was sent
517 */
518 cr = ssi->cr_own |
519 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700520
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000521 rsnd_mod_write(mod, SSICR, cr | EN);
522 rsnd_ssi_status_check(mod, DIRQ);
523
524 /*
525 * disable SSI,
526 * and, wait idle state
527 */
528 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
529 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000530
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700531 return 0;
532}
533
Kuninori Morimoto49229852014-11-27 08:07:17 +0000534static int rsnd_ssi_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000535 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000536 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700537{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700538 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
539
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000540 /*
541 * don't stop if not last user
542 * see also
543 * rsnd_ssi_start
544 * rsnd_ssi_interrupt
545 */
546 if (ssi->usrcnt > 1)
547 return 0;
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000548
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000549 return __rsnd_ssi_stop(mod, io, priv);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700550}
551
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000552static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
553 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000554{
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000555 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000556 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000557 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000558 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000559 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000560 bool elapsed = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000561
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000562 spin_lock(&priv->lock);
563
564 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000565 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000566 goto rsnd_ssi_interrupt_out;
567
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000568 status = rsnd_ssi_record_error(ssi);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000569
570 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000571 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000572 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
573 u32 *buf = (u32 *)(runtime->dma_area +
574 rsnd_dai_pointer_offset(io, 0));
575
576 /*
577 * 8/16/32 data can be assesse to TDR/RDR register
578 * directly as 32bit data
579 * see rsnd_ssi_init()
580 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000581 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000582 rsnd_mod_write(mod, SSITDR, *buf);
583 else
584 *buf = rsnd_mod_read(mod, SSIRDR);
585
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000586 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000587 }
588
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000589 /* DMA only */
590 if (is_dma && (status & (UIRQ | OIRQ))) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000591 /*
592 * restart SSI
593 */
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000594 dev_dbg(dev, "%s[%d] restart\n",
595 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto044930b2015-03-19 04:13:47 +0000596
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000597 __rsnd_ssi_stop(mod, io, priv);
598 __rsnd_ssi_start(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000599 }
600
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000601 if (ssi->err > 1024) {
602 rsnd_ssi_irq_disable(mod);
603
604 dev_warn(dev, "no more %s[%d] restart\n",
605 rsnd_mod_name(mod), rsnd_mod_id(mod));
606 }
607
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000608 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000609rsnd_ssi_interrupt_out:
610 spin_unlock(&priv->lock);
611
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000612 if (elapsed)
613 rsnd_dai_period_elapsed(io);
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000614}
615
616static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
617{
618 struct rsnd_mod *mod = data;
619
620 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000621
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000622 return IRQ_HANDLED;
623}
624
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000625/*
626 * SSI PIO
627 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000628static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
629 struct rsnd_dai_stream *io,
630 struct rsnd_priv *priv)
631{
632 if (!__rsnd_ssi_is_pin_sharing(mod))
633 return;
634
635 switch (rsnd_mod_id(mod)) {
636 case 1:
637 case 2:
638 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
639 break;
640 case 4:
641 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
642 break;
643 case 8:
644 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
645 break;
646 }
647}
648
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000649static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
650 struct rsnd_dai_stream *io,
651 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000652{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000653 struct device *dev = rsnd_priv_to_dev(priv);
654 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000655 int ret;
656
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000657 /*
658 * SSIP/SSIU/IRQ are not needed on
659 * SSI Multi slaves
660 */
661 if (rsnd_ssi_is_multi_slave(mod, io))
662 return 0;
663
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000664 rsnd_ssi_parent_attach(mod, io, priv);
665
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000666 ret = rsnd_ssiu_attach(io, mod);
667 if (ret < 0)
668 return ret;
669
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000670 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000671 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000672 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000673 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000674
675 return ret;
676}
677
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700678static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700679 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000680 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700681 .init = rsnd_ssi_init,
682 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000683 .start = rsnd_ssi_start,
684 .stop = rsnd_ssi_stop,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000685 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700686};
687
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800688static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000689 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000690 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800691{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800692 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000693 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800694 int ret;
695
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000696 /*
697 * SSIP/SSIU/IRQ/DMA are not needed on
698 * SSI Multi slaves
699 */
700 if (rsnd_ssi_is_multi_slave(mod, io))
701 return 0;
702
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000703 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000704 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000705 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000706
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000707 /* SSI probe might be called many times in MUX multi path */
708 ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700709
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800710 return ret;
711}
712
713static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000714 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000715 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800716{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000717 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
718 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000719 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000720
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000721 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000722 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000723
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000724 return 0;
725}
726
727static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000728 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000729 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000730{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800731 struct device *dev = rsnd_priv_to_dev(priv);
732
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800733 /*
734 * fallback to PIO
735 *
736 * SSI .probe might be called again.
737 * see
738 * rsnd_rdai_continuance_probe()
739 */
740 mod->ops = &rsnd_ssi_pio_ops;
741
742 dev_info(dev, "%s[%d] fallback to PIO mode\n",
743 rsnd_mod_name(mod), rsnd_mod_id(mod));
744
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800745 return 0;
746}
747
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000748static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
749 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700750{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000751 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000752 int is_play = rsnd_io_is_play(io);
753 char *name;
754
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000755 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000756 name = is_play ? "rxu" : "txu";
757 else
758 name = is_play ? "rx" : "tx";
759
760 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
761 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700762}
763
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700764static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700765 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000766 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800767 .probe = rsnd_ssi_dma_probe,
768 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700769 .init = rsnd_ssi_init,
770 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000771 .start = rsnd_ssi_start,
772 .stop = rsnd_ssi_stop,
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;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000950 if (of_get_property(np, "pio-transfer", NULL))
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}