blob: 5870434bbc58e467acb34ff10dc95e882552d0db [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 Morimotob4c83b12015-12-17 03:00:10 +000097#define rsnd_ssi_is_multi_slave(ssi, io) ((mod) != rsnd_io_to_mod_ssi(io))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070098
Kuninori Morimotob415b4d2015-10-22 03:15:46 +000099int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700100{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000101 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700102 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700103 int use_busif = 0;
104
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000105 if (!rsnd_ssi_is_dma_mode(mod))
106 return 0;
107
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700108 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
109 use_busif = 1;
110 if (rsnd_io_to_mod_src(io))
111 use_busif = 1;
112
113 return use_busif;
114}
115
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000116static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
117{
118 rsnd_mod_write(mod, SSISR, 0);
119}
120
121static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
122{
123 return rsnd_mod_read(mod, SSISR);
124}
125
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700126static void rsnd_ssi_status_check(struct rsnd_mod *mod,
127 u32 bit)
128{
129 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
130 struct device *dev = rsnd_priv_to_dev(priv);
131 u32 status;
132 int i;
133
134 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000135 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700136 if (status & bit)
137 return;
138
139 udelay(50);
140 }
141
142 dev_warn(dev, "status check failed\n");
143}
144
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000145static int rsnd_ssi_irq_enable(struct rsnd_mod *ssi_mod)
146{
147 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
148
149 if (rsnd_is_gen1(priv))
150 return 0;
151
152 /* enable SSI interrupt if Gen2 */
153 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
154 rsnd_ssi_is_dma_mode(ssi_mod) ?
155 0x0e000000 : 0x0f000000);
156
157 return 0;
158}
159
160static int rsnd_ssi_irq_disable(struct rsnd_mod *ssi_mod)
161{
162 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
163
164 if (rsnd_is_gen1(priv))
165 return 0;
166
167 /* disable SSI interrupt if Gen2 */
168 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
169
170 return 0;
171}
172
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000173u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
174{
175 struct rsnd_mod *mod;
176 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
177 struct rsnd_priv *priv = rsnd_io_to_priv(io);
178 struct device *dev = rsnd_priv_to_dev(priv);
179 enum rsnd_mod_type types[] = {
180 RSND_MOD_SSIM1,
181 RSND_MOD_SSIM2,
182 RSND_MOD_SSIM3,
183 };
184 int i, mask;
185
186 switch (runtime->channels) {
187 case 2: /* Multi channel is not needed for Stereo */
188 return 0;
189 case 6:
190 break;
191 default:
192 dev_err(dev, "unsupported channel\n");
193 return 0;
194 }
195
196 mask = 0;
197 for (i = 0; i < ARRAY_SIZE(types); i++) {
198 mod = rsnd_io_to_mod(io, types[i]);
199 if (!mod)
200 continue;
201
202 mask |= 1 << rsnd_mod_id(mod);
203 }
204
205 return mask;
206}
207
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700208static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800209 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700210{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000211 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800212 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700213 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000214 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000215 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000216 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000217 int slots = rsnd_get_slot_width(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000218 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700219 int ssi_clk_mul_table[] = {
220 1, 2, 4, 8, 16, 6, 12,
221 };
222 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800223 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700224
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000225 if (!rsnd_rdai_is_clk_master(rdai))
226 return 0;
227
228 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
229 return 0;
230
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000231 if (rsnd_ssi_is_multi_slave(mod, io))
232 return 0;
233
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000234 if (ssi->usrcnt > 1) {
235 if (ssi->rate != rate) {
236 dev_err(dev, "SSI parent/child should use same rate\n");
237 return -EINVAL;
238 }
239
240 return 0;
241 }
242
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700243 /*
244 * Find best clock, and try to start ADG
245 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000246 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700247
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000248 /*
249 * this driver is assuming that
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000250 * system word is 32bit x slots
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000251 * see rsnd_ssi_init()
252 */
Kuninori Morimoto8ec85e72015-11-30 08:53:27 +0000253 main_rate = rate * 32 * slots * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700254
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000255 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
256 if (0 == ret) {
257 ssi->cr_clk = FORCE | SWL_32 |
258 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000259 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700260
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000261 ssi->rate = rate;
262
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000263 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
264 rsnd_mod_name(mod),
265 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700266
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000267 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700268 }
269 }
270
271 dev_err(dev, "unsupported clock rate\n");
272 return -EIO;
273}
274
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000275static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi,
276 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700277{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000278 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000279 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000280 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700281
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000282 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700283 return;
284
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000285 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
286 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700287
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000288 if (ssi->usrcnt > 1)
289 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700290
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000291 ssi->cr_clk = 0;
292 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700293
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000294 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700295}
296
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000297static int rsnd_ssi_config_init(struct rsnd_ssi *ssi,
298 struct rsnd_dai_stream *io)
299{
300 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
301 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
302 u32 cr_own;
303 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000304 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000305 int is_tdm;
306
Kuninori Morimotoc1402842015-12-17 02:57:27 +0000307 is_tdm = (rsnd_get_slot_width(io) >= 6) ? 1 : 0;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000308
309 /*
310 * always use 32bit system word.
311 * see also rsnd_ssi_master_clk_enable()
312 */
313 cr_own = FORCE | SWL_32 | PDTA;
314
315 if (rdai->bit_clk_inv)
316 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000317 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000318 cr_own |= SWSP;
319 if (rdai->data_alignment)
320 cr_own |= SDTA;
321 if (rdai->sys_delay)
322 cr_own |= DEL;
323 if (rsnd_io_is_play(io))
324 cr_own |= TRMD;
325
326 switch (runtime->sample_bits) {
327 case 16:
328 cr_own |= DWL_16;
329 break;
330 case 32:
331 cr_own |= DWL_24;
332 break;
333 default:
334 return -EINVAL;
335 }
336
337 if (rsnd_ssi_is_dma_mode(rsnd_mod_get(ssi))) {
338 cr_mode = UIEN | OIEN | /* over/under run */
339 DMEN; /* DMA : enable DMA */
340 } else {
341 cr_mode = DIEN; /* PIO : enable Data interrupt */
342 }
343
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000344 /*
345 * TDM Extend Mode
346 * see
347 * rsnd_ssiu_init_gen2()
348 */
349 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000350 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000351 wsr |= WS_MODE;
352 cr_own |= CHNL_8;
353 }
354
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000355 ssi->cr_own = cr_own;
356 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000357 ssi->wsr = wsr;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000358
359 return 0;
360}
361
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700362/*
363 * SSI mod common functions
364 */
365static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000366 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000367 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700368{
369 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000370 int ret;
371
372 ssi->usrcnt++;
373
374 rsnd_mod_power_on(mod);
375
376 ret = rsnd_ssi_master_clk_start(ssi, io);
377 if (ret < 0)
378 return ret;
379
380 if (rsnd_ssi_is_parent(mod, io))
381 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700382
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000383 ret = rsnd_ssi_config_init(ssi, io);
384 if (ret < 0)
385 return ret;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000386
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000387 /* clear error status */
388 rsnd_ssi_status_clear(mod);
389
390 rsnd_ssi_irq_enable(mod);
391
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700392 return 0;
393}
394
395static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000396 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000397 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700398{
399 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700400 struct device *dev = rsnd_priv_to_dev(priv);
401
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100402 if (!ssi->usrcnt) {
403 dev_err(dev, "%s[%d] usrcnt error\n",
404 rsnd_mod_name(mod), rsnd_mod_id(mod));
405 return -EIO;
406 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000407
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100408 if (!rsnd_ssi_is_parent(mod, io)) {
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100409 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700410
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100411 rsnd_ssi_irq_disable(mod);
412 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000413
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000414 rsnd_ssi_master_clk_stop(ssi, io);
415
416 rsnd_mod_power_off(mod);
417
418 ssi->usrcnt--;
419
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700420 return 0;
421}
422
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000423static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000424 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000425 struct snd_pcm_substream *substream,
426 struct snd_pcm_hw_params *params)
427{
428 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000429 int chan = params_channels(params);
430
431 /*
432 * Already working.
433 * It will happen if SSI has parent/child connection.
434 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000435 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000436 /*
437 * it is error if child <-> parent SSI uses
438 * different channels.
439 */
440 if (ssi->chan != chan)
441 return -EIO;
442 }
443
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000444 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000445
446 return 0;
447}
448
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000449static int rsnd_ssi_start(struct rsnd_mod *mod,
450 struct rsnd_dai_stream *io,
451 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000452{
453 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
454 u32 cr;
455
456 cr = ssi->cr_own |
457 ssi->cr_clk |
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000458 ssi->cr_mode;
459
460 /*
461 * EN will be set via SSIU :: SSI_CONTROL
462 * if Multi channel mode
463 */
464 if (!rsnd_ssi_multi_slaves(io))
465 cr |= EN;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000466
467 rsnd_mod_write(mod, SSICR, cr);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000468 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000469
470 return 0;
471}
472
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000473static int rsnd_ssi_stop(struct rsnd_mod *mod,
474 struct rsnd_dai_stream *io,
475 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000476{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700477 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000478 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700479
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 Morimotobfc0cfe2015-06-15 06:26:56 +0000509static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
510 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000511{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000512 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000513 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000514 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000515 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000516 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000517
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000518 spin_lock(&priv->lock);
519
520 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000521 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000522 goto rsnd_ssi_interrupt_out;
523
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000524 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000525
526 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000527 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000528 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
529 u32 *buf = (u32 *)(runtime->dma_area +
530 rsnd_dai_pointer_offset(io, 0));
531
532 /*
533 * 8/16/32 data can be assesse to TDR/RDR register
534 * directly as 32bit data
535 * see rsnd_ssi_init()
536 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000537 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000538 rsnd_mod_write(mod, SSITDR, *buf);
539 else
540 *buf = rsnd_mod_read(mod, SSIRDR);
541
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000542 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000543 }
544
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000545 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000546 if (is_dma && (status & (UIRQ | OIRQ)))
547 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000548
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000549 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000550rsnd_ssi_interrupt_out:
551 spin_unlock(&priv->lock);
552
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000553 if (elapsed)
554 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000555
556 if (stop)
557 snd_pcm_stop_xrun(io->substream);
558
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000559}
560
561static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
562{
563 struct rsnd_mod *mod = data;
564
565 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000566
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000567 return IRQ_HANDLED;
568}
569
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000570/*
571 * SSI PIO
572 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000573static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
574 struct rsnd_dai_stream *io,
575 struct rsnd_priv *priv)
576{
577 if (!__rsnd_ssi_is_pin_sharing(mod))
578 return;
579
580 switch (rsnd_mod_id(mod)) {
581 case 1:
582 case 2:
583 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
584 break;
585 case 4:
586 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
587 break;
588 case 8:
589 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
590 break;
591 }
592}
593
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000594static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
595 struct rsnd_dai_stream *io,
596 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000597{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000598 struct device *dev = rsnd_priv_to_dev(priv);
599 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000600 int ret;
601
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000602 /*
603 * SSIP/SSIU/IRQ are not needed on
604 * SSI Multi slaves
605 */
606 if (rsnd_ssi_is_multi_slave(mod, io))
607 return 0;
608
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000609 rsnd_ssi_parent_attach(mod, io, priv);
610
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000611 ret = rsnd_ssiu_attach(io, mod);
612 if (ret < 0)
613 return ret;
614
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000615 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000616 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000617 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000618 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000619
620 return ret;
621}
622
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700623static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700624 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000625 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700626 .init = rsnd_ssi_init,
627 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000628 .start = rsnd_ssi_start,
629 .stop = rsnd_ssi_stop,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000630 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700631};
632
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800633static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000634 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000635 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800636{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800637 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000638 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800639 int ret;
640
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000641 /*
642 * SSIP/SSIU/IRQ/DMA are not needed on
643 * SSI Multi slaves
644 */
645 if (rsnd_ssi_is_multi_slave(mod, io))
646 return 0;
647
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000648 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000649 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000650 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000651
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000652 /* SSI probe might be called many times in MUX multi path */
653 ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700654
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800655 return ret;
656}
657
658static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000659 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000660 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800661{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000662 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
663 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000664 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000665
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000666 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000667 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000668
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000669 return 0;
670}
671
672static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000673 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000674 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000675{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800676 struct device *dev = rsnd_priv_to_dev(priv);
677
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800678 /*
679 * fallback to PIO
680 *
681 * SSI .probe might be called again.
682 * see
683 * rsnd_rdai_continuance_probe()
684 */
685 mod->ops = &rsnd_ssi_pio_ops;
686
687 dev_info(dev, "%s[%d] fallback to PIO mode\n",
688 rsnd_mod_name(mod), rsnd_mod_id(mod));
689
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800690 return 0;
691}
692
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000693static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
694 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700695{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000696 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000697 int is_play = rsnd_io_is_play(io);
698 char *name;
699
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000700 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000701 name = is_play ? "rxu" : "txu";
702 else
703 name = is_play ? "rx" : "tx";
704
705 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
706 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700707}
708
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700709static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700710 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000711 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800712 .probe = rsnd_ssi_dma_probe,
713 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700714 .init = rsnd_ssi_init,
715 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000716 .start = rsnd_ssi_start,
717 .stop = rsnd_ssi_stop,
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}