blob: 59ca6e3f46bca88be123d781f23fa5f57ef74ac5 [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 */
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +000014#include <sound/simple_card_utils.h>
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070015#include <linux/delay.h>
16#include "rsnd.h"
17#define RSND_SSI_NAME_SIZE 16
18
19/*
20 * SSICR
21 */
22#define FORCE (1 << 31) /* Fixed */
Kuninori Morimoto849fc822013-07-28 18:59:02 -070023#define DMEN (1 << 28) /* DMA Enable */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070024#define UIEN (1 << 27) /* Underflow Interrupt Enable */
25#define OIEN (1 << 26) /* Overflow Interrupt Enable */
26#define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
27#define DIEN (1 << 24) /* Data Interrupt Enable */
Kuninori Morimoto186fadc2015-11-30 08:54:03 +000028#define CHNL_4 (1 << 22) /* Channels */
29#define CHNL_6 (2 << 22) /* Channels */
30#define CHNL_8 (3 << 22) /* Channels */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070031#define DWL_8 (0 << 19) /* Data Word Length */
32#define DWL_16 (1 << 19) /* Data Word Length */
33#define DWL_18 (2 << 19) /* Data Word Length */
34#define DWL_20 (3 << 19) /* Data Word Length */
35#define DWL_22 (4 << 19) /* Data Word Length */
36#define DWL_24 (5 << 19) /* Data Word Length */
37#define DWL_32 (6 << 19) /* Data Word Length */
38
39#define SWL_32 (3 << 16) /* R/W System Word Length */
40#define SCKD (1 << 15) /* Serial Bit Clock Direction */
41#define SWSD (1 << 14) /* Serial WS Direction */
42#define SCKP (1 << 13) /* Serial Bit Clock Polarity */
43#define SWSP (1 << 12) /* Serial WS Polarity */
44#define SDTA (1 << 10) /* Serial Data Alignment */
Kuninori Morimotof46a93b2015-11-17 08:28:11 +000045#define PDTA (1 << 9) /* Parallel Data Alignment */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070046#define DEL (1 << 8) /* Serial Data Delay */
47#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
48#define TRMD (1 << 1) /* Transmit/Receive Mode Select */
49#define EN (1 << 0) /* SSI Module Enable */
50
51/*
52 * SSISR
53 */
54#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
55#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
56#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
57#define DIRQ (1 << 24) /* Data Interrupt Status Flag */
58
Kuninori Morimoto849fc822013-07-28 18:59:02 -070059/*
60 * SSIWSR
61 */
62#define CONT (1 << 8) /* WS Continue Function */
Kuninori Morimoto186fadc2015-11-30 08:54:03 +000063#define WS_MODE (1 << 0) /* WS Mode */
Kuninori Morimoto849fc822013-07-28 18:59:02 -070064
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070065#define SSI_NAME "ssi"
66
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070067struct rsnd_ssi {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070068 struct rsnd_mod mod;
Kuninori Morimoto940e9472015-10-26 08:42:25 +000069 struct rsnd_mod *dma;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070070
Kuninori Morimoto02534f22015-11-10 05:11:35 +000071 u32 flags;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070072 u32 cr_own;
73 u32 cr_clk;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000074 u32 cr_mode;
Kuninori Morimoto08bada22015-11-30 08:53:04 +000075 u32 wsr;
Kuninori Morimoto919567d2015-04-10 08:50:30 +000076 int chan;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000077 int rate;
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 */
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +000085#define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
86#define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
Kuninori Morimoto02534f22015-11-10 05:11:35 +000087
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070088#define for_each_rsnd_ssi(pos, priv, i) \
89 for (i = 0; \
90 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080091 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070092 i++)
93
Kuninori Morimoto02534f22015-11-10 05:11:35 +000094#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +000095#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
Kuninori Morimotodd27d802014-01-23 18:39:40 -080096#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070097#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto02534f22015-11-10 05:11:35 +000098#define rsnd_ssi_mode_flags(p) ((p)->flags)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000099#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
Kuninori Morimotoc308abe2016-02-09 07:04:09 +0000100#define rsnd_ssi_is_multi_slave(mod, io) \
101 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000102#define rsnd_ssi_is_run_mods(mod, io) \
103 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700104
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000105int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
106{
107 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
108 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
109
110 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI0)
111 return RSND_SSI_HDMI_PORT0;
112
113 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI1)
114 return RSND_SSI_HDMI_PORT1;
115
116 return 0;
117}
118
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000119int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700120{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000121 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700122 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700123 int use_busif = 0;
124
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000125 if (!rsnd_ssi_is_dma_mode(mod))
126 return 0;
127
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700128 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
129 use_busif = 1;
130 if (rsnd_io_to_mod_src(io))
131 use_busif = 1;
132
133 return use_busif;
134}
135
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000136static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
137{
138 rsnd_mod_write(mod, SSISR, 0);
139}
140
141static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
142{
143 return rsnd_mod_read(mod, SSISR);
144}
145
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700146static void rsnd_ssi_status_check(struct rsnd_mod *mod,
147 u32 bit)
148{
149 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
150 struct device *dev = rsnd_priv_to_dev(priv);
151 u32 status;
152 int i;
153
154 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000155 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700156 if (status & bit)
157 return;
158
159 udelay(50);
160 }
161
Kuninori Morimoto1120dbf2016-02-18 08:14:09 +0000162 dev_warn(dev, "%s[%d] status check failed\n",
163 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700164}
165
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000166static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000167{
168 struct rsnd_mod *mod;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000169 enum rsnd_mod_type types[] = {
170 RSND_MOD_SSIM1,
171 RSND_MOD_SSIM2,
172 RSND_MOD_SSIM3,
173 };
174 int i, mask;
175
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000176 mask = 0;
177 for (i = 0; i < ARRAY_SIZE(types); i++) {
178 mod = rsnd_io_to_mod(io, types[i]);
179 if (!mod)
180 continue;
181
182 mask |= 1 << rsnd_mod_id(mod);
183 }
184
185 return mask;
186}
187
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000188static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
189{
190 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
191 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
192
193 return rsnd_ssi_multi_slaves_runtime(io) |
194 1 << rsnd_mod_id(ssi_mod) |
195 1 << rsnd_mod_id(ssi_parent_mod);
196}
197
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000198u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
199{
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000200 if (rsnd_runtime_is_ssi_multi(io))
201 return rsnd_ssi_multi_slaves(io);
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000202
203 return 0;
204}
205
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000206static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800207 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700208{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000209 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700210 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000211 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000212 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000213 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000214 int chan = rsnd_runtime_channel_for_ssi(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000215 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700216 int ssi_clk_mul_table[] = {
217 1, 2, 4, 8, 16, 6, 12,
218 };
219 unsigned int main_rate;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000220 unsigned int rate = rsnd_io_is_play(io) ?
221 rsnd_src_get_out_rate(priv, io) :
222 rsnd_src_get_in_rate(priv, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700223
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000224 if (!rsnd_rdai_is_clk_master(rdai))
225 return 0;
226
227 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
228 return 0;
229
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000230 if (rsnd_ssi_is_multi_slave(mod, io))
231 return 0;
232
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000233 if (ssi->usrcnt > 1) {
234 if (ssi->rate != rate) {
235 dev_err(dev, "SSI parent/child should use same rate\n");
236 return -EINVAL;
237 }
238
239 return 0;
240 }
241
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700242 /*
243 * Find best clock, and try to start ADG
244 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000245 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700246
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000247 /*
Kuninori Morimoto6b8530c2017-03-22 04:02:43 +0000248 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
249 * with it is not allowed. (SSIWSR.WS_MODE with
250 * SSICR.CKDV = 000 is not allowed either).
251 * Skip it. See SSICR.CKDV
252 */
253 if (j == 0)
254 continue;
255
256 /*
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000257 * this driver is assuming that
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000258 * system word is 32bit x chan
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000259 * see rsnd_ssi_init()
260 */
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000261 main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700262
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000263 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
264 if (0 == ret) {
265 ssi->cr_clk = FORCE | SWL_32 |
266 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000267 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700268
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000269 ssi->rate = rate;
270
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000271 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
272 rsnd_mod_name(mod),
273 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700274
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000275 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700276 }
277 }
278
279 dev_err(dev, "unsupported clock rate\n");
280 return -EIO;
281}
282
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000283static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000284 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700285{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000286 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000287 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000288 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700289
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000290 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700291 return;
292
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000293 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
294 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700295
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000296 if (ssi->usrcnt > 1)
297 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700298
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000299 ssi->cr_clk = 0;
300 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700301
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000302 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700303}
304
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000305static void rsnd_ssi_config_init(struct rsnd_mod *mod,
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000306 struct rsnd_dai_stream *io)
307{
308 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
309 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000310 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000311 u32 cr_own;
312 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000313 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000314 int is_tdm;
315
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000316 is_tdm = rsnd_runtime_is_ssi_tdm(io);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000317
318 /*
319 * always use 32bit system word.
320 * see also rsnd_ssi_master_clk_enable()
321 */
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000322 cr_own = FORCE | SWL_32;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000323
324 if (rdai->bit_clk_inv)
325 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000326 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000327 cr_own |= SWSP;
328 if (rdai->data_alignment)
329 cr_own |= SDTA;
330 if (rdai->sys_delay)
331 cr_own |= DEL;
332 if (rsnd_io_is_play(io))
333 cr_own |= TRMD;
334
335 switch (runtime->sample_bits) {
336 case 16:
337 cr_own |= DWL_16;
338 break;
339 case 32:
340 cr_own |= DWL_24;
341 break;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000342 }
343
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000344 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000345 cr_mode = UIEN | OIEN | /* over/under run */
346 DMEN; /* DMA : enable DMA */
347 } else {
348 cr_mode = DIEN; /* PIO : enable Data interrupt */
349 }
350
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000351 /*
352 * TDM Extend Mode
353 * see
354 * rsnd_ssiu_init_gen2()
355 */
356 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000357 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000358 wsr |= WS_MODE;
359 cr_own |= CHNL_8;
360 }
361
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000362 ssi->cr_own = cr_own;
363 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000364 ssi->wsr = wsr;
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000365}
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000366
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000367static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
368{
369 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
370
371 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
372 rsnd_mod_write(mod, SSICR, ssi->cr_own |
373 ssi->cr_clk |
374 ssi->cr_mode); /* without EN */
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000375}
376
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700377/*
378 * SSI mod common functions
379 */
380static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000381 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000382 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700383{
384 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000385 int ret;
386
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000387 if (!rsnd_ssi_is_run_mods(mod, io))
388 return 0;
389
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000390 ssi->usrcnt++;
391
392 rsnd_mod_power_on(mod);
393
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000394 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000395 if (ret < 0)
396 return ret;
397
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000398 if (!rsnd_ssi_is_parent(mod, io))
399 rsnd_ssi_config_init(mod, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700400
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000401 rsnd_ssi_register_setup(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000402
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000403 /* clear error status */
404 rsnd_ssi_status_clear(mod);
405
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700406 return 0;
407}
408
409static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000410 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000411 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700412{
413 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700414 struct device *dev = rsnd_priv_to_dev(priv);
415
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000416 if (!rsnd_ssi_is_run_mods(mod, io))
417 return 0;
418
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100419 if (!ssi->usrcnt) {
420 dev_err(dev, "%s[%d] usrcnt error\n",
421 rsnd_mod_name(mod), rsnd_mod_id(mod));
422 return -EIO;
423 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000424
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000425 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100426 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700427
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000428 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000429
430 rsnd_mod_power_off(mod);
431
432 ssi->usrcnt--;
433
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700434 return 0;
435}
436
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000437static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000438 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000439 struct snd_pcm_substream *substream,
440 struct snd_pcm_hw_params *params)
441{
442 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000443 int chan = params_channels(params);
444
445 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000446 * snd_pcm_ops::hw_params will be called *before*
447 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
448 * in 1st call.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000449 */
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000450 if (ssi->usrcnt) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000451 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000452 * Already working.
453 * It will happen if SSI has parent/child connection.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000454 * it is error if child <-> parent SSI uses
455 * different channels.
456 */
457 if (ssi->chan != chan)
458 return -EIO;
459 }
460
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000461 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000462
463 return 0;
464}
465
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000466static int rsnd_ssi_start(struct rsnd_mod *mod,
467 struct rsnd_dai_stream *io,
468 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000469{
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000470 if (!rsnd_ssi_is_run_mods(mod, io))
471 return 0;
472
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000473 /*
474 * EN will be set via SSIU :: SSI_CONTROL
475 * if Multi channel mode
476 */
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000477 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000478 return 0;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000479
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000480 rsnd_mod_bset(mod, SSICR, EN, EN);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000481
482 return 0;
483}
484
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000485static int rsnd_ssi_stop(struct rsnd_mod *mod,
486 struct rsnd_dai_stream *io,
487 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000488{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700489 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000490 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700491
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000492 if (!rsnd_ssi_is_run_mods(mod, io))
493 return 0;
494
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000495 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000496 * don't stop if not last user
497 * see also
498 * rsnd_ssi_start
499 * rsnd_ssi_interrupt
500 */
501 if (ssi->usrcnt > 1)
502 return 0;
503
504 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000505 * disable all IRQ,
506 * and, wait all data was sent
507 */
508 cr = ssi->cr_own |
509 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700510
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000511 rsnd_mod_write(mod, SSICR, cr | EN);
512 rsnd_ssi_status_check(mod, DIRQ);
513
514 /*
515 * disable SSI,
516 * and, wait idle state
517 */
518 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
519 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000520
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700521 return 0;
522}
523
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000524static int rsnd_ssi_irq(struct rsnd_mod *mod,
525 struct rsnd_dai_stream *io,
526 struct rsnd_priv *priv,
527 int enable)
528{
529 u32 val = 0;
530
531 if (rsnd_is_gen1(priv))
532 return 0;
533
534 if (rsnd_ssi_is_parent(mod, io))
535 return 0;
536
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000537 if (!rsnd_ssi_is_run_mods(mod, io))
538 return 0;
539
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000540 if (enable)
541 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
542
543 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
544
545 return 0;
546}
547
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000548static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
549 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000550{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000551 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000552 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000553 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000554 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000555 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000556
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000557 spin_lock(&priv->lock);
558
559 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000560 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000561 goto rsnd_ssi_interrupt_out;
562
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000563 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000564
565 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000566 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000567 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
568 u32 *buf = (u32 *)(runtime->dma_area +
569 rsnd_dai_pointer_offset(io, 0));
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000570 int shift = 0;
571
572 switch (runtime->sample_bits) {
573 case 32:
574 shift = 8;
575 break;
576 }
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000577
578 /*
579 * 8/16/32 data can be assesse to TDR/RDR register
580 * directly as 32bit data
581 * see rsnd_ssi_init()
582 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000583 if (rsnd_io_is_play(io))
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000584 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000585 else
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000586 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000587
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000588 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000589 }
590
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000591 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000592 if (is_dma && (status & (UIRQ | OIRQ)))
593 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000594
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000595 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000596rsnd_ssi_interrupt_out:
597 spin_unlock(&priv->lock);
598
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000599 if (elapsed)
600 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000601
602 if (stop)
603 snd_pcm_stop_xrun(io->substream);
604
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000605}
606
607static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
608{
609 struct rsnd_mod *mod = data;
610
611 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000612
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000613 return IRQ_HANDLED;
614}
615
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000616/*
617 * SSI PIO
618 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000619static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000620 struct rsnd_dai_stream *io)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000621{
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000622 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
623 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
624
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000625 if (!__rsnd_ssi_is_pin_sharing(mod))
626 return;
627
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000628 if (!rsnd_rdai_is_clk_master(rdai))
629 return;
630
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000631 switch (rsnd_mod_id(mod)) {
632 case 1:
633 case 2:
634 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
635 break;
636 case 4:
637 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
638 break;
639 case 8:
640 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
641 break;
642 }
643}
644
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000645static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
646 struct rsnd_dai_stream *io,
647 struct snd_soc_pcm_runtime *rtd)
648{
649 /*
650 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
651 * and, pcm_new will be called after it.
652 * This function reuse pcm_new at this point.
653 */
654 rsnd_ssi_parent_attach(mod, io);
655
656 return 0;
657}
658
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000659static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
660 struct rsnd_dai_stream *io,
661 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000662{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000663 struct device *dev = rsnd_priv_to_dev(priv);
664 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000665 int ret;
666
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000667 /*
668 * SSIP/SSIU/IRQ are not needed on
669 * SSI Multi slaves
670 */
671 if (rsnd_ssi_is_multi_slave(mod, io))
672 return 0;
673
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000674 /*
675 * It can't judge ssi parent at this point
676 * see rsnd_ssi_pcm_new()
677 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000678
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000679 ret = rsnd_ssiu_attach(io, mod);
680 if (ret < 0)
681 return ret;
682
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000683 /*
684 * SSI might be called again as PIO fallback
685 * It is easy to manual handling for IRQ request/free
686 */
687 ret = request_irq(ssi->irq,
688 rsnd_ssi_interrupt,
689 IRQF_SHARED,
690 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000691
692 return ret;
693}
694
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000695static int rsnd_ssi_pointer(struct rsnd_mod *mod,
696 struct rsnd_dai_stream *io,
697 snd_pcm_uframes_t *pointer)
698{
699 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
700
701 *pointer = bytes_to_frames(runtime, io->byte_pos);
702
703 return 0;
704}
705
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700706static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700707 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000708 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700709 .init = rsnd_ssi_init,
710 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000711 .start = rsnd_ssi_start,
712 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000713 .irq = rsnd_ssi_irq,
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000714 .pointer= rsnd_ssi_pointer,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000715 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000716 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700717};
718
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800719static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000720 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000721 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800722{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800723 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800724 int ret;
725
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000726 /*
727 * SSIP/SSIU/IRQ/DMA are not needed on
728 * SSI Multi slaves
729 */
730 if (rsnd_ssi_is_multi_slave(mod, io))
731 return 0;
732
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000733 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000734 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000735 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000736
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000737 /* SSI probe might be called many times in MUX multi path */
Kuninori Morimotob99305d2016-10-25 00:36:13 +0000738 ret = rsnd_dma_attach(io, mod, &ssi->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700739
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800740 return ret;
741}
742
743static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000744 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000745 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800746{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000747 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto1f8754d2017-05-16 01:48:24 +0000748 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
749
750 /* Do nothing for SSI parent mod */
751 if (ssi_parent_mod == mod)
752 return 0;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000753
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000754 /* PIO will request IRQ again */
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000755 free_irq(ssi->irq, mod);
Kuninori Morimoto0af5c012016-10-19 03:56:26 +0000756
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000757 return 0;
758}
759
760static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000761 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000762 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000763{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800764 struct device *dev = rsnd_priv_to_dev(priv);
765
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800766 /*
767 * fallback to PIO
768 *
769 * SSI .probe might be called again.
770 * see
771 * rsnd_rdai_continuance_probe()
772 */
773 mod->ops = &rsnd_ssi_pio_ops;
774
775 dev_info(dev, "%s[%d] fallback to PIO mode\n",
776 rsnd_mod_name(mod), rsnd_mod_id(mod));
777
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800778 return 0;
779}
780
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000781static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
782 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700783{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000784 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000785 int is_play = rsnd_io_is_play(io);
786 char *name;
787
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000788 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000789 name = is_play ? "rxu" : "txu";
790 else
791 name = is_play ? "rx" : "tx";
792
793 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
794 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700795}
796
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700797static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700798 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000799 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800800 .probe = rsnd_ssi_dma_probe,
801 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700802 .init = rsnd_ssi_init,
803 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000804 .start = rsnd_ssi_start,
805 .stop = rsnd_ssi_stop,
Kuninori Morimotoc8e969a2016-02-18 08:16:43 +0000806 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000807 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000808 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000809 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700810};
811
Kuninori Morimoto05795412014-11-27 08:05:01 +0000812int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
813{
814 return mod->ops == &rsnd_ssi_dma_ops;
815}
816
817
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700818/*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700819 * ssi mod function
820 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000821static void rsnd_ssi_connect(struct rsnd_mod *mod,
822 struct rsnd_dai_stream *io)
823{
824 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
825 enum rsnd_mod_type types[] = {
826 RSND_MOD_SSI,
827 RSND_MOD_SSIM1,
828 RSND_MOD_SSIM2,
829 RSND_MOD_SSIM3,
830 };
831 enum rsnd_mod_type type;
832 int i;
833
834 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
835 for (i = 0; i < ARRAY_SIZE(types); i++) {
836 type = types[i];
837 if (!rsnd_io_to_mod(io, type)) {
838 rsnd_dai_connect(mod, io, type);
839 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
840 return;
841 }
842 }
843}
844
845void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
846 struct device_node *playback,
847 struct device_node *capture)
848{
849 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
850 struct device_node *node;
851 struct device_node *np;
852 struct rsnd_mod *mod;
853 int i;
854
855 node = rsnd_ssi_of_node(priv);
856 if (!node)
857 return;
858
859 i = 0;
860 for_each_child_of_node(node, np) {
861 mod = rsnd_ssi_mod_get(priv, i);
862 if (np == playback)
863 rsnd_ssi_connect(mod, &rdai->playback);
864 if (np == capture)
865 rsnd_ssi_connect(mod, &rdai->capture);
866 i++;
867 }
868
869 of_node_put(node);
870}
871
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000872static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
873 struct rsnd_dai_stream *io,
874 struct device_node *remote_ep)
875{
876 struct device *dev = rsnd_priv_to_dev(priv);
877 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
878 struct rsnd_ssi *ssi;
879
880 if (!mod)
881 return;
882
883 ssi = rsnd_mod_to_ssi(mod);
884
885 if (strstr(remote_ep->full_name, "hdmi0")) {
886 ssi->flags |= RSND_SSI_HDMI0;
887 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
888 rsnd_mod_name(mod), rsnd_mod_id(mod));
889 }
890
891 if (strstr(remote_ep->full_name, "hdmi1")) {
892 ssi->flags |= RSND_SSI_HDMI1;
893 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
894 rsnd_mod_name(mod), rsnd_mod_id(mod));
895 }
896}
897
898void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
899 struct device_node *endpoint,
900 int dai_i)
901{
902 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
903 struct device_node *remote_ep;
904
905 remote_ep = of_graph_get_remote_endpoint(endpoint);
906 if (!remote_ep)
907 return;
908
909 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
910 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
911}
912
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700913struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
914{
Takashi Iwai8b147192013-11-05 18:40:05 +0100915 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
916 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700917
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000918 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700919}
920
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000921int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800922{
923 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
924
925 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
926}
927
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000928static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
929 struct rsnd_mod *mod,
930 enum rsnd_mod_type type)
931{
932 /*
933 * SSIP (= SSI parent) needs to be special, otherwise,
934 * 2nd SSI might doesn't start. see also rsnd_mod_call()
935 *
936 * We can't include parent SSI status on SSI, because we don't know
937 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
938 * ex) trouble case
939 * Playback: SSI0
940 * Capture : SSI1 (needs SSI0)
941 *
942 * 1) start Capture -> SSI0/SSI1 are started.
943 * 2) start Playback -> SSI0 doesn't work, because it is already
944 * marked as "started" on 1)
945 *
946 * OTOH, using each mod's status is good for MUX case.
947 * It doesn't need to start in 2nd start
948 * ex)
949 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
950 * |
951 * IO-1: SRC1 -> CTU2 -+
952 *
953 * 1) start IO-0 -> start SSI0
954 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
955 * already started on 1)
956 */
957 if (type == RSND_MOD_SSIP)
958 return &io->parent_ssi_status;
959
960 return rsnd_mod_get_status(io, mod, type);
961}
962
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000963int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700964{
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000965 struct device_node *node;
966 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700967 struct device *dev = rsnd_priv_to_dev(priv);
968 struct rsnd_mod_ops *ops;
969 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700970 struct rsnd_ssi *ssi;
971 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000972 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700973
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000974 node = rsnd_ssi_of_node(priv);
975 if (!node)
976 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700977
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000978 nr = of_get_child_count(node);
979 if (!nr) {
980 ret = -EINVAL;
981 goto rsnd_ssi_probe_done;
982 }
983
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800984 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000985 if (!ssi) {
986 ret = -ENOMEM;
987 goto rsnd_ssi_probe_done;
988 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700989
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800990 priv->ssi = ssi;
991 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700992
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000993 i = 0;
994 for_each_child_of_node(node, np) {
995 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700996
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700997 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
998 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700999
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -08001000 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001001 if (IS_ERR(clk)) {
1002 ret = PTR_ERR(clk);
1003 goto rsnd_ssi_probe_done;
1004 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001005
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001006 if (of_get_property(np, "shared-pin", NULL))
1007 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
1008
1009 if (of_get_property(np, "no-busif", NULL))
1010 ssi->flags |= RSND_SSI_NO_BUSIF;
1011
1012 ssi->irq = irq_of_parse_and_map(np, 0);
1013 if (!ssi->irq) {
1014 ret = -EINVAL;
1015 goto rsnd_ssi_probe_done;
1016 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001017
Julia Lawall51930292016-08-05 10:56:51 +02001018 if (of_property_read_bool(np, "pio-transfer"))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -08001019 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001020 else
1021 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001022
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001023 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +00001024 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001025 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001026 goto rsnd_ssi_probe_done;
1027
1028 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001029 }
1030
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001031 ret = 0;
1032
1033rsnd_ssi_probe_done:
1034 of_node_put(node);
1035
1036 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001037}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001038
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +00001039void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001040{
1041 struct rsnd_ssi *ssi;
1042 int i;
1043
1044 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001045 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001046 }
1047}