blob: 90c3f58821dbdb5c742487ee627b4e07c94a6738 [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 Morimoto26d34b12016-02-18 08:14:37 +0000279static int 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;
316 default:
317 return -EINVAL;
318 }
319
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000320 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000321 cr_mode = UIEN | OIEN | /* over/under run */
322 DMEN; /* DMA : enable DMA */
323 } else {
324 cr_mode = DIEN; /* PIO : enable Data interrupt */
325 }
326
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000327 /*
328 * TDM Extend Mode
329 * see
330 * rsnd_ssiu_init_gen2()
331 */
332 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000333 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000334 wsr |= WS_MODE;
335 cr_own |= CHNL_8;
336 }
337
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000338 ssi->cr_own = cr_own;
339 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000340 ssi->wsr = wsr;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000341
342 return 0;
343}
344
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700345/*
346 * SSI mod common functions
347 */
348static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000349 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000350 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700351{
352 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000353 int ret;
354
355 ssi->usrcnt++;
356
357 rsnd_mod_power_on(mod);
358
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000359 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000360 if (ret < 0)
361 return ret;
362
363 if (rsnd_ssi_is_parent(mod, io))
364 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700365
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000366 ret = rsnd_ssi_config_init(mod, io);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000367 if (ret < 0)
368 return ret;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000369
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000370 /* clear error status */
371 rsnd_ssi_status_clear(mod);
372
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700373 return 0;
374}
375
376static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000377 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000378 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700379{
380 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700381 struct device *dev = rsnd_priv_to_dev(priv);
382
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100383 if (!ssi->usrcnt) {
384 dev_err(dev, "%s[%d] usrcnt error\n",
385 rsnd_mod_name(mod), rsnd_mod_id(mod));
386 return -EIO;
387 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000388
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000389 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100390 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700391
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000392 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000393
394 rsnd_mod_power_off(mod);
395
396 ssi->usrcnt--;
397
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700398 return 0;
399}
400
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000401static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000402 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000403 struct snd_pcm_substream *substream,
404 struct snd_pcm_hw_params *params)
405{
406 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000407 int chan = params_channels(params);
408
409 /*
410 * Already working.
411 * It will happen if SSI has parent/child connection.
412 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000413 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000414 /*
415 * it is error if child <-> parent SSI uses
416 * different channels.
417 */
418 if (ssi->chan != chan)
419 return -EIO;
420 }
421
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000422 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000423
424 return 0;
425}
426
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000427static int rsnd_ssi_start(struct rsnd_mod *mod,
428 struct rsnd_dai_stream *io,
429 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000430{
431 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
432 u32 cr;
433
434 cr = ssi->cr_own |
435 ssi->cr_clk |
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000436 ssi->cr_mode;
437
438 /*
439 * EN will be set via SSIU :: SSI_CONTROL
440 * if Multi channel mode
441 */
442 if (!rsnd_ssi_multi_slaves(io))
443 cr |= EN;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000444
445 rsnd_mod_write(mod, SSICR, cr);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000446 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000447
448 return 0;
449}
450
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000451static int rsnd_ssi_stop(struct rsnd_mod *mod,
452 struct rsnd_dai_stream *io,
453 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000454{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700455 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000456 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700457
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000458 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000459 * don't stop if not last user
460 * see also
461 * rsnd_ssi_start
462 * rsnd_ssi_interrupt
463 */
464 if (ssi->usrcnt > 1)
465 return 0;
466
467 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000468 * disable all IRQ,
469 * and, wait all data was sent
470 */
471 cr = ssi->cr_own |
472 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700473
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000474 rsnd_mod_write(mod, SSICR, cr | EN);
475 rsnd_ssi_status_check(mod, DIRQ);
476
477 /*
478 * disable SSI,
479 * and, wait idle state
480 */
481 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
482 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000483
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700484 return 0;
485}
486
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000487static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
488 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000489{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000490 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000491 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000492 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000493 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000494 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000495
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000496 spin_lock(&priv->lock);
497
498 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000499 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000500 goto rsnd_ssi_interrupt_out;
501
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000502 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000503
504 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000505 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000506 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
507 u32 *buf = (u32 *)(runtime->dma_area +
508 rsnd_dai_pointer_offset(io, 0));
509
510 /*
511 * 8/16/32 data can be assesse to TDR/RDR register
512 * directly as 32bit data
513 * see rsnd_ssi_init()
514 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000515 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000516 rsnd_mod_write(mod, SSITDR, *buf);
517 else
518 *buf = rsnd_mod_read(mod, SSIRDR);
519
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000520 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000521 }
522
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000523 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000524 if (is_dma && (status & (UIRQ | OIRQ)))
525 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000526
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000527 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000528rsnd_ssi_interrupt_out:
529 spin_unlock(&priv->lock);
530
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000531 if (elapsed)
532 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000533
534 if (stop)
535 snd_pcm_stop_xrun(io->substream);
536
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000537}
538
539static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
540{
541 struct rsnd_mod *mod = data;
542
543 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000544
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000545 return IRQ_HANDLED;
546}
547
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000548/*
549 * SSI PIO
550 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000551static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
552 struct rsnd_dai_stream *io,
553 struct rsnd_priv *priv)
554{
555 if (!__rsnd_ssi_is_pin_sharing(mod))
556 return;
557
558 switch (rsnd_mod_id(mod)) {
559 case 1:
560 case 2:
561 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
562 break;
563 case 4:
564 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
565 break;
566 case 8:
567 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
568 break;
569 }
570}
571
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000572static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
573 struct rsnd_dai_stream *io,
574 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000575{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000576 struct device *dev = rsnd_priv_to_dev(priv);
577 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000578 int ret;
579
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000580 /*
581 * SSIP/SSIU/IRQ are not needed on
582 * SSI Multi slaves
583 */
584 if (rsnd_ssi_is_multi_slave(mod, io))
585 return 0;
586
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000587 rsnd_ssi_parent_attach(mod, io, priv);
588
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000589 ret = rsnd_ssiu_attach(io, mod);
590 if (ret < 0)
591 return ret;
592
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000593 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000594 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000595 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000596 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000597
598 return ret;
599}
600
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700601static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700602 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000603 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700604 .init = rsnd_ssi_init,
605 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000606 .start = rsnd_ssi_start,
607 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000608 .irq = rsnd_ssi_irq,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000609 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700610};
611
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800612static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000613 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000614 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800615{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800616 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000617 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800618 int ret;
619
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000620 /*
621 * SSIP/SSIU/IRQ/DMA are not needed on
622 * SSI Multi slaves
623 */
624 if (rsnd_ssi_is_multi_slave(mod, io))
625 return 0;
626
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000627 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000628 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000629 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000630
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000631 /* SSI probe might be called many times in MUX multi path */
632 ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700633
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800634 return ret;
635}
636
637static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000638 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000639 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800640{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000641 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
642 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000643 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000644
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000645 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000646 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000647
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000648 return 0;
649}
650
651static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000652 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000653 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000654{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800655 struct device *dev = rsnd_priv_to_dev(priv);
656
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800657 /*
658 * fallback to PIO
659 *
660 * SSI .probe might be called again.
661 * see
662 * rsnd_rdai_continuance_probe()
663 */
664 mod->ops = &rsnd_ssi_pio_ops;
665
666 dev_info(dev, "%s[%d] fallback to PIO mode\n",
667 rsnd_mod_name(mod), rsnd_mod_id(mod));
668
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800669 return 0;
670}
671
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000672static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
673 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700674{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000675 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000676 int is_play = rsnd_io_is_play(io);
677 char *name;
678
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000679 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000680 name = is_play ? "rxu" : "txu";
681 else
682 name = is_play ? "rx" : "tx";
683
684 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
685 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700686}
687
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700688static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700689 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000690 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800691 .probe = rsnd_ssi_dma_probe,
692 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700693 .init = rsnd_ssi_init,
694 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000695 .start = rsnd_ssi_start,
696 .stop = rsnd_ssi_stop,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000697 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000698 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700699};
700
Kuninori Morimoto05795412014-11-27 08:05:01 +0000701int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
702{
703 return mod->ops == &rsnd_ssi_dma_ops;
704}
705
706
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700707/*
708 * Non SSI
709 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700710static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700711 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700712};
713
714/*
715 * ssi mod function
716 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000717static void rsnd_ssi_connect(struct rsnd_mod *mod,
718 struct rsnd_dai_stream *io)
719{
720 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
721 enum rsnd_mod_type types[] = {
722 RSND_MOD_SSI,
723 RSND_MOD_SSIM1,
724 RSND_MOD_SSIM2,
725 RSND_MOD_SSIM3,
726 };
727 enum rsnd_mod_type type;
728 int i;
729
730 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
731 for (i = 0; i < ARRAY_SIZE(types); i++) {
732 type = types[i];
733 if (!rsnd_io_to_mod(io, type)) {
734 rsnd_dai_connect(mod, io, type);
735 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
736 return;
737 }
738 }
739}
740
741void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
742 struct device_node *playback,
743 struct device_node *capture)
744{
745 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
746 struct device_node *node;
747 struct device_node *np;
748 struct rsnd_mod *mod;
749 int i;
750
751 node = rsnd_ssi_of_node(priv);
752 if (!node)
753 return;
754
755 i = 0;
756 for_each_child_of_node(node, np) {
757 mod = rsnd_ssi_mod_get(priv, i);
758 if (np == playback)
759 rsnd_ssi_connect(mod, &rdai->playback);
760 if (np == capture)
761 rsnd_ssi_connect(mod, &rdai->capture);
762 i++;
763 }
764
765 of_node_put(node);
766}
767
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700768struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
769{
Takashi Iwai8b147192013-11-05 18:40:05 +0100770 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
771 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700772
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000773 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700774}
775
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000776int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800777{
778 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
779
780 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
781}
782
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000783static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
784 struct rsnd_mod *mod,
785 enum rsnd_mod_type type)
786{
787 /*
788 * SSIP (= SSI parent) needs to be special, otherwise,
789 * 2nd SSI might doesn't start. see also rsnd_mod_call()
790 *
791 * We can't include parent SSI status on SSI, because we don't know
792 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
793 * ex) trouble case
794 * Playback: SSI0
795 * Capture : SSI1 (needs SSI0)
796 *
797 * 1) start Capture -> SSI0/SSI1 are started.
798 * 2) start Playback -> SSI0 doesn't work, because it is already
799 * marked as "started" on 1)
800 *
801 * OTOH, using each mod's status is good for MUX case.
802 * It doesn't need to start in 2nd start
803 * ex)
804 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
805 * |
806 * IO-1: SRC1 -> CTU2 -+
807 *
808 * 1) start IO-0 -> start SSI0
809 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
810 * already started on 1)
811 */
812 if (type == RSND_MOD_SSIP)
813 return &io->parent_ssi_status;
814
815 return rsnd_mod_get_status(io, mod, type);
816}
817
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000818int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700819{
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000820 struct device_node *node;
821 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700822 struct device *dev = rsnd_priv_to_dev(priv);
823 struct rsnd_mod_ops *ops;
824 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700825 struct rsnd_ssi *ssi;
826 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000827 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700828
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000829 node = rsnd_ssi_of_node(priv);
830 if (!node)
831 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700832
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000833 nr = of_get_child_count(node);
834 if (!nr) {
835 ret = -EINVAL;
836 goto rsnd_ssi_probe_done;
837 }
838
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800839 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000840 if (!ssi) {
841 ret = -ENOMEM;
842 goto rsnd_ssi_probe_done;
843 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700844
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800845 priv->ssi = ssi;
846 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700847
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000848 i = 0;
849 for_each_child_of_node(node, np) {
850 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700851
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700852 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
853 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700854
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800855 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000856 if (IS_ERR(clk)) {
857 ret = PTR_ERR(clk);
858 goto rsnd_ssi_probe_done;
859 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700860
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000861 if (of_get_property(np, "shared-pin", NULL))
862 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
863
864 if (of_get_property(np, "no-busif", NULL))
865 ssi->flags |= RSND_SSI_NO_BUSIF;
866
867 ssi->irq = irq_of_parse_and_map(np, 0);
868 if (!ssi->irq) {
869 ret = -EINVAL;
870 goto rsnd_ssi_probe_done;
871 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700872
873 ops = &rsnd_ssi_non_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000874 if (of_get_property(np, "pio-transfer", NULL))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800875 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000876 else
877 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700878
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000879 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000880 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000881 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000882 goto rsnd_ssi_probe_done;
883
884 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700885 }
886
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000887 ret = 0;
888
889rsnd_ssi_probe_done:
890 of_node_put(node);
891
892 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700893}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000894
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000895void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000896{
897 struct rsnd_ssi *ssi;
898 int i;
899
900 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000901 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000902 }
903}