blob: f7df3b5e2985265f53ccb4097a7fb23e46cfbb90 [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 Morimotoa97a06c2017-06-07 00:20:47 +000080
81 int byte_pos;
82 int period_pos;
83 int byte_per_period;
84 int next_period_byte;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070085};
86
Kuninori Morimoto02534f22015-11-10 05:11:35 +000087/* flags */
88#define RSND_SSI_CLK_PIN_SHARE (1 << 0)
89#define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +000090#define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
91#define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
Kuninori Morimoto02534f22015-11-10 05:11:35 +000092
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070093#define for_each_rsnd_ssi(pos, priv, i) \
94 for (i = 0; \
95 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080096 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070097 i++)
98
Kuninori Morimoto02534f22015-11-10 05:11:35 +000099#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +0000100#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800101#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700102#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000103#define rsnd_ssi_mode_flags(p) ((p)->flags)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000104#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
Kuninori Morimotoc308abe2016-02-09 07:04:09 +0000105#define rsnd_ssi_is_multi_slave(mod, io) \
106 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000107#define rsnd_ssi_is_run_mods(mod, io) \
108 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700109
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000110int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
111{
112 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
113 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
114
115 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI0)
116 return RSND_SSI_HDMI_PORT0;
117
118 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI1)
119 return RSND_SSI_HDMI_PORT1;
120
121 return 0;
122}
123
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000124int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700125{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000126 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700127 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700128 int use_busif = 0;
129
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000130 if (!rsnd_ssi_is_dma_mode(mod))
131 return 0;
132
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700133 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
134 use_busif = 1;
135 if (rsnd_io_to_mod_src(io))
136 use_busif = 1;
137
138 return use_busif;
139}
140
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000141static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
142{
143 rsnd_mod_write(mod, SSISR, 0);
144}
145
146static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
147{
148 return rsnd_mod_read(mod, SSISR);
149}
150
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700151static void rsnd_ssi_status_check(struct rsnd_mod *mod,
152 u32 bit)
153{
154 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
155 struct device *dev = rsnd_priv_to_dev(priv);
156 u32 status;
157 int i;
158
159 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000160 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700161 if (status & bit)
162 return;
163
164 udelay(50);
165 }
166
Kuninori Morimoto1120dbf2016-02-18 08:14:09 +0000167 dev_warn(dev, "%s[%d] status check failed\n",
168 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700169}
170
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000171static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000172{
173 struct rsnd_mod *mod;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000174 enum rsnd_mod_type types[] = {
175 RSND_MOD_SSIM1,
176 RSND_MOD_SSIM2,
177 RSND_MOD_SSIM3,
178 };
179 int i, mask;
180
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000181 mask = 0;
182 for (i = 0; i < ARRAY_SIZE(types); i++) {
183 mod = rsnd_io_to_mod(io, types[i]);
184 if (!mod)
185 continue;
186
187 mask |= 1 << rsnd_mod_id(mod);
188 }
189
190 return mask;
191}
192
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000193static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
194{
195 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
196 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
197
198 return rsnd_ssi_multi_slaves_runtime(io) |
199 1 << rsnd_mod_id(ssi_mod) |
200 1 << rsnd_mod_id(ssi_parent_mod);
201}
202
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000203u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
204{
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000205 if (rsnd_runtime_is_ssi_multi(io))
206 return rsnd_ssi_multi_slaves(io);
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000207
208 return 0;
209}
210
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000211static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800212 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700213{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000214 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700215 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000216 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000217 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000218 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000219 int chan = rsnd_runtime_channel_for_ssi(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000220 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700221 int ssi_clk_mul_table[] = {
222 1, 2, 4, 8, 16, 6, 12,
223 };
224 unsigned int main_rate;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000225 unsigned int rate = rsnd_io_is_play(io) ?
226 rsnd_src_get_out_rate(priv, io) :
227 rsnd_src_get_in_rate(priv, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700228
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000229 if (!rsnd_rdai_is_clk_master(rdai))
230 return 0;
231
232 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
233 return 0;
234
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000235 if (rsnd_ssi_is_multi_slave(mod, io))
236 return 0;
237
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000238 if (ssi->usrcnt > 1) {
239 if (ssi->rate != rate) {
240 dev_err(dev, "SSI parent/child should use same rate\n");
241 return -EINVAL;
242 }
243
244 return 0;
245 }
246
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700247 /*
248 * Find best clock, and try to start ADG
249 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000250 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700251
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000252 /*
Kuninori Morimoto6b8530c2017-03-22 04:02:43 +0000253 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
254 * with it is not allowed. (SSIWSR.WS_MODE with
255 * SSICR.CKDV = 000 is not allowed either).
256 * Skip it. See SSICR.CKDV
257 */
258 if (j == 0)
259 continue;
260
261 /*
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000262 * this driver is assuming that
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000263 * system word is 32bit x chan
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000264 * see rsnd_ssi_init()
265 */
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000266 main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700267
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000268 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
269 if (0 == ret) {
270 ssi->cr_clk = FORCE | SWL_32 |
271 SCKD | SWSD | CKDV(j);
Kuninori Morimoto08bada22015-11-30 08:53:04 +0000272 ssi->wsr = CONT;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700273
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000274 ssi->rate = rate;
275
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000276 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
277 rsnd_mod_name(mod),
278 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700279
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000280 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700281 }
282 }
283
284 dev_err(dev, "unsupported clock rate\n");
285 return -EIO;
286}
287
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000288static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000289 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700290{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000291 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000292 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000293 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700294
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000295 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700296 return;
297
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000298 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
299 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700300
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000301 if (ssi->usrcnt > 1)
302 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700303
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000304 ssi->cr_clk = 0;
305 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700306
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000307 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700308}
309
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000310static void rsnd_ssi_config_init(struct rsnd_mod *mod,
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000311 struct rsnd_dai_stream *io)
312{
313 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
314 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000315 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000316 u32 cr_own;
317 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000318 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000319 int is_tdm;
320
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000321 is_tdm = rsnd_runtime_is_ssi_tdm(io);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000322
323 /*
324 * always use 32bit system word.
325 * see also rsnd_ssi_master_clk_enable()
326 */
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000327 cr_own = FORCE | SWL_32;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000328
329 if (rdai->bit_clk_inv)
330 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000331 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000332 cr_own |= SWSP;
333 if (rdai->data_alignment)
334 cr_own |= SDTA;
335 if (rdai->sys_delay)
336 cr_own |= DEL;
337 if (rsnd_io_is_play(io))
338 cr_own |= TRMD;
339
340 switch (runtime->sample_bits) {
341 case 16:
342 cr_own |= DWL_16;
343 break;
344 case 32:
345 cr_own |= DWL_24;
346 break;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000347 }
348
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000349 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000350 cr_mode = UIEN | OIEN | /* over/under run */
351 DMEN; /* DMA : enable DMA */
352 } else {
353 cr_mode = DIEN; /* PIO : enable Data interrupt */
354 }
355
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000356 /*
357 * TDM Extend Mode
358 * see
359 * rsnd_ssiu_init_gen2()
360 */
361 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000362 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000363 wsr |= WS_MODE;
364 cr_own |= CHNL_8;
365 }
366
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000367 ssi->cr_own = cr_own;
368 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000369 ssi->wsr = wsr;
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000370}
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000371
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000372static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
373{
374 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
375
376 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
377 rsnd_mod_write(mod, SSICR, ssi->cr_own |
378 ssi->cr_clk |
379 ssi->cr_mode); /* without EN */
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000380}
381
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000382static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
383 struct rsnd_dai_stream *io)
384{
385 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
386 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
387
388 ssi->byte_pos = 0;
389 ssi->period_pos = 0;
390 ssi->byte_per_period = runtime->period_size *
391 runtime->channels *
392 samples_to_bytes(runtime, 1);
393 ssi->next_period_byte = ssi->byte_per_period;
394}
395
396static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
397 struct rsnd_dai_stream *io,
398 int additional)
399{
400 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
401 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
402 int pos = ssi->byte_pos + additional;
403
404 pos %= (runtime->periods * ssi->byte_per_period);
405
406 return pos;
407}
408
409static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
410 struct rsnd_dai_stream *io,
411 int byte)
412{
413 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
414
415 ssi->byte_pos += byte;
416
417 if (ssi->byte_pos >= ssi->next_period_byte) {
418 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
419
420 ssi->period_pos++;
421 ssi->next_period_byte += ssi->byte_per_period;
422
423 if (ssi->period_pos >= runtime->periods) {
424 ssi->byte_pos = 0;
425 ssi->period_pos = 0;
426 ssi->next_period_byte = ssi->byte_per_period;
427 }
428
429 return true;
430 }
431
432 return false;
433}
434
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700435/*
436 * SSI mod common functions
437 */
438static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000439 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000440 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700441{
442 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000443 int ret;
444
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000445 if (!rsnd_ssi_is_run_mods(mod, io))
446 return 0;
447
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000448 rsnd_ssi_pointer_init(mod, io);
449
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000450 ssi->usrcnt++;
451
452 rsnd_mod_power_on(mod);
453
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000454 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000455 if (ret < 0)
456 return ret;
457
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000458 if (!rsnd_ssi_is_parent(mod, io))
459 rsnd_ssi_config_init(mod, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700460
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000461 rsnd_ssi_register_setup(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000462
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000463 /* clear error status */
464 rsnd_ssi_status_clear(mod);
465
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700466 return 0;
467}
468
469static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000470 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000471 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700472{
473 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700474 struct device *dev = rsnd_priv_to_dev(priv);
475
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000476 if (!rsnd_ssi_is_run_mods(mod, io))
477 return 0;
478
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100479 if (!ssi->usrcnt) {
480 dev_err(dev, "%s[%d] usrcnt error\n",
481 rsnd_mod_name(mod), rsnd_mod_id(mod));
482 return -EIO;
483 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000484
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000485 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100486 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700487
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000488 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000489
490 rsnd_mod_power_off(mod);
491
492 ssi->usrcnt--;
493
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700494 return 0;
495}
496
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000497static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000498 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000499 struct snd_pcm_substream *substream,
500 struct snd_pcm_hw_params *params)
501{
502 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000503 int chan = params_channels(params);
504
505 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000506 * snd_pcm_ops::hw_params will be called *before*
507 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
508 * in 1st call.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000509 */
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000510 if (ssi->usrcnt) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000511 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000512 * Already working.
513 * It will happen if SSI has parent/child connection.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000514 * it is error if child <-> parent SSI uses
515 * different channels.
516 */
517 if (ssi->chan != chan)
518 return -EIO;
519 }
520
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000521 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000522
523 return 0;
524}
525
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000526static int rsnd_ssi_start(struct rsnd_mod *mod,
527 struct rsnd_dai_stream *io,
528 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000529{
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000530 if (!rsnd_ssi_is_run_mods(mod, io))
531 return 0;
532
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000533 /*
534 * EN will be set via SSIU :: SSI_CONTROL
535 * if Multi channel mode
536 */
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000537 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000538 return 0;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000539
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000540 rsnd_mod_bset(mod, SSICR, EN, EN);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000541
542 return 0;
543}
544
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000545static int rsnd_ssi_stop(struct rsnd_mod *mod,
546 struct rsnd_dai_stream *io,
547 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000548{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700549 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000550 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700551
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000552 if (!rsnd_ssi_is_run_mods(mod, io))
553 return 0;
554
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000555 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000556 * don't stop if not last user
557 * see also
558 * rsnd_ssi_start
559 * rsnd_ssi_interrupt
560 */
561 if (ssi->usrcnt > 1)
562 return 0;
563
564 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000565 * disable all IRQ,
566 * and, wait all data was sent
567 */
568 cr = ssi->cr_own |
569 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700570
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000571 rsnd_mod_write(mod, SSICR, cr | EN);
572 rsnd_ssi_status_check(mod, DIRQ);
573
574 /*
575 * disable SSI,
576 * and, wait idle state
577 */
578 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
579 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000580
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700581 return 0;
582}
583
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000584static int rsnd_ssi_irq(struct rsnd_mod *mod,
585 struct rsnd_dai_stream *io,
586 struct rsnd_priv *priv,
587 int enable)
588{
589 u32 val = 0;
590
591 if (rsnd_is_gen1(priv))
592 return 0;
593
594 if (rsnd_ssi_is_parent(mod, io))
595 return 0;
596
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000597 if (!rsnd_ssi_is_run_mods(mod, io))
598 return 0;
599
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000600 if (enable)
601 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
602
603 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
604
605 return 0;
606}
607
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000608static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
609 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000610{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000611 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000612 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000613 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000614 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000615 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000616
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000617 spin_lock(&priv->lock);
618
619 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000620 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000621 goto rsnd_ssi_interrupt_out;
622
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000623 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000624
625 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000626 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000627 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
628 u32 *buf = (u32 *)(runtime->dma_area +
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000629 rsnd_ssi_pointer_offset(mod, io, 0));
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000630 int shift = 0;
631
632 switch (runtime->sample_bits) {
633 case 32:
634 shift = 8;
635 break;
636 }
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000637
638 /*
639 * 8/16/32 data can be assesse to TDR/RDR register
640 * directly as 32bit data
641 * see rsnd_ssi_init()
642 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000643 if (rsnd_io_is_play(io))
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000644 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000645 else
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000646 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000647
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000648 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000649 }
650
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000651 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000652 if (is_dma && (status & (UIRQ | OIRQ)))
653 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000654
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000655 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000656rsnd_ssi_interrupt_out:
657 spin_unlock(&priv->lock);
658
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000659 if (elapsed)
660 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000661
662 if (stop)
663 snd_pcm_stop_xrun(io->substream);
664
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000665}
666
667static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
668{
669 struct rsnd_mod *mod = data;
670
671 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000672
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000673 return IRQ_HANDLED;
674}
675
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000676/*
677 * SSI PIO
678 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000679static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000680 struct rsnd_dai_stream *io)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000681{
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000682 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
683 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
684
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000685 if (!__rsnd_ssi_is_pin_sharing(mod))
686 return;
687
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000688 if (!rsnd_rdai_is_clk_master(rdai))
689 return;
690
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000691 switch (rsnd_mod_id(mod)) {
692 case 1:
693 case 2:
694 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
695 break;
696 case 4:
697 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
698 break;
699 case 8:
700 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
701 break;
702 }
703}
704
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000705static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
706 struct rsnd_dai_stream *io,
707 struct snd_soc_pcm_runtime *rtd)
708{
709 /*
710 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
711 * and, pcm_new will be called after it.
712 * This function reuse pcm_new at this point.
713 */
714 rsnd_ssi_parent_attach(mod, io);
715
716 return 0;
717}
718
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000719static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
720 struct rsnd_dai_stream *io,
721 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000722{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000723 struct device *dev = rsnd_priv_to_dev(priv);
724 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000725 int ret;
726
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000727 /*
728 * SSIP/SSIU/IRQ are not needed on
729 * SSI Multi slaves
730 */
731 if (rsnd_ssi_is_multi_slave(mod, io))
732 return 0;
733
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000734 /*
735 * It can't judge ssi parent at this point
736 * see rsnd_ssi_pcm_new()
737 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000738
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000739 ret = rsnd_ssiu_attach(io, mod);
740 if (ret < 0)
741 return ret;
742
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000743 /*
744 * SSI might be called again as PIO fallback
745 * It is easy to manual handling for IRQ request/free
746 */
747 ret = request_irq(ssi->irq,
748 rsnd_ssi_interrupt,
749 IRQF_SHARED,
750 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000751
752 return ret;
753}
754
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000755static int rsnd_ssi_pointer(struct rsnd_mod *mod,
756 struct rsnd_dai_stream *io,
757 snd_pcm_uframes_t *pointer)
758{
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000759 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000760 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
761
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000762 *pointer = bytes_to_frames(runtime, ssi->byte_pos);
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000763
764 return 0;
765}
766
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700767static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700768 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000769 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700770 .init = rsnd_ssi_init,
771 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000772 .start = rsnd_ssi_start,
773 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000774 .irq = rsnd_ssi_irq,
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000775 .pointer= rsnd_ssi_pointer,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000776 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000777 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700778};
779
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800780static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000781 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000782 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800783{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800784 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800785 int ret;
786
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000787 /*
788 * SSIP/SSIU/IRQ/DMA are not needed on
789 * SSI Multi slaves
790 */
791 if (rsnd_ssi_is_multi_slave(mod, io))
792 return 0;
793
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000794 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000795 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000796 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000797
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000798 /* SSI probe might be called many times in MUX multi path */
Kuninori Morimotob99305d2016-10-25 00:36:13 +0000799 ret = rsnd_dma_attach(io, mod, &ssi->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700800
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800801 return ret;
802}
803
804static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000805 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000806 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800807{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000808 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto1f8754d2017-05-16 01:48:24 +0000809 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
810
811 /* Do nothing for SSI parent mod */
812 if (ssi_parent_mod == mod)
813 return 0;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000814
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000815 /* PIO will request IRQ again */
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000816 free_irq(ssi->irq, mod);
Kuninori Morimoto0af5c012016-10-19 03:56:26 +0000817
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000818 return 0;
819}
820
821static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000822 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000823 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000824{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800825 struct device *dev = rsnd_priv_to_dev(priv);
826
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800827 /*
828 * fallback to PIO
829 *
830 * SSI .probe might be called again.
831 * see
832 * rsnd_rdai_continuance_probe()
833 */
834 mod->ops = &rsnd_ssi_pio_ops;
835
836 dev_info(dev, "%s[%d] fallback to PIO mode\n",
837 rsnd_mod_name(mod), rsnd_mod_id(mod));
838
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800839 return 0;
840}
841
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000842static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
843 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700844{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000845 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000846 int is_play = rsnd_io_is_play(io);
847 char *name;
848
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000849 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000850 name = is_play ? "rxu" : "txu";
851 else
852 name = is_play ? "rx" : "tx";
853
854 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
855 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700856}
857
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700858static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700859 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000860 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800861 .probe = rsnd_ssi_dma_probe,
862 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700863 .init = rsnd_ssi_init,
864 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000865 .start = rsnd_ssi_start,
866 .stop = rsnd_ssi_stop,
Kuninori Morimotoc8e969a2016-02-18 08:16:43 +0000867 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000868 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000869 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000870 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700871};
872
Kuninori Morimoto05795412014-11-27 08:05:01 +0000873int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
874{
875 return mod->ops == &rsnd_ssi_dma_ops;
876}
877
878
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700879/*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700880 * ssi mod function
881 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000882static void rsnd_ssi_connect(struct rsnd_mod *mod,
883 struct rsnd_dai_stream *io)
884{
885 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
886 enum rsnd_mod_type types[] = {
887 RSND_MOD_SSI,
888 RSND_MOD_SSIM1,
889 RSND_MOD_SSIM2,
890 RSND_MOD_SSIM3,
891 };
892 enum rsnd_mod_type type;
893 int i;
894
895 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
896 for (i = 0; i < ARRAY_SIZE(types); i++) {
897 type = types[i];
898 if (!rsnd_io_to_mod(io, type)) {
899 rsnd_dai_connect(mod, io, type);
900 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
901 return;
902 }
903 }
904}
905
906void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
907 struct device_node *playback,
908 struct device_node *capture)
909{
910 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
911 struct device_node *node;
912 struct device_node *np;
913 struct rsnd_mod *mod;
914 int i;
915
916 node = rsnd_ssi_of_node(priv);
917 if (!node)
918 return;
919
920 i = 0;
921 for_each_child_of_node(node, np) {
922 mod = rsnd_ssi_mod_get(priv, i);
923 if (np == playback)
924 rsnd_ssi_connect(mod, &rdai->playback);
925 if (np == capture)
926 rsnd_ssi_connect(mod, &rdai->capture);
927 i++;
928 }
929
930 of_node_put(node);
931}
932
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000933static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
934 struct rsnd_dai_stream *io,
935 struct device_node *remote_ep)
936{
937 struct device *dev = rsnd_priv_to_dev(priv);
938 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
939 struct rsnd_ssi *ssi;
940
941 if (!mod)
942 return;
943
944 ssi = rsnd_mod_to_ssi(mod);
945
946 if (strstr(remote_ep->full_name, "hdmi0")) {
947 ssi->flags |= RSND_SSI_HDMI0;
948 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
949 rsnd_mod_name(mod), rsnd_mod_id(mod));
950 }
951
952 if (strstr(remote_ep->full_name, "hdmi1")) {
953 ssi->flags |= RSND_SSI_HDMI1;
954 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
955 rsnd_mod_name(mod), rsnd_mod_id(mod));
956 }
957}
958
959void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
960 struct device_node *endpoint,
961 int dai_i)
962{
963 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
964 struct device_node *remote_ep;
965
966 remote_ep = of_graph_get_remote_endpoint(endpoint);
967 if (!remote_ep)
968 return;
969
970 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
971 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
972}
973
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700974struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
975{
Takashi Iwai8b147192013-11-05 18:40:05 +0100976 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
977 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700978
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000979 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700980}
981
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000982int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800983{
984 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
985
986 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
987}
988
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000989static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
990 struct rsnd_mod *mod,
991 enum rsnd_mod_type type)
992{
993 /*
994 * SSIP (= SSI parent) needs to be special, otherwise,
995 * 2nd SSI might doesn't start. see also rsnd_mod_call()
996 *
997 * We can't include parent SSI status on SSI, because we don't know
998 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
999 * ex) trouble case
1000 * Playback: SSI0
1001 * Capture : SSI1 (needs SSI0)
1002 *
1003 * 1) start Capture -> SSI0/SSI1 are started.
1004 * 2) start Playback -> SSI0 doesn't work, because it is already
1005 * marked as "started" on 1)
1006 *
1007 * OTOH, using each mod's status is good for MUX case.
1008 * It doesn't need to start in 2nd start
1009 * ex)
1010 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1011 * |
1012 * IO-1: SRC1 -> CTU2 -+
1013 *
1014 * 1) start IO-0 -> start SSI0
1015 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1016 * already started on 1)
1017 */
1018 if (type == RSND_MOD_SSIP)
1019 return &io->parent_ssi_status;
1020
1021 return rsnd_mod_get_status(io, mod, type);
1022}
1023
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +00001024int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001025{
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001026 struct device_node *node;
1027 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001028 struct device *dev = rsnd_priv_to_dev(priv);
1029 struct rsnd_mod_ops *ops;
1030 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001031 struct rsnd_ssi *ssi;
1032 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001033 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001034
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001035 node = rsnd_ssi_of_node(priv);
1036 if (!node)
1037 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001038
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001039 nr = of_get_child_count(node);
1040 if (!nr) {
1041 ret = -EINVAL;
1042 goto rsnd_ssi_probe_done;
1043 }
1044
Kuninori Morimotodd27d802014-01-23 18:39:40 -08001045 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001046 if (!ssi) {
1047 ret = -ENOMEM;
1048 goto rsnd_ssi_probe_done;
1049 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001050
Kuninori Morimotodd27d802014-01-23 18:39:40 -08001051 priv->ssi = ssi;
1052 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001053
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001054 i = 0;
1055 for_each_child_of_node(node, np) {
1056 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001057
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001058 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1059 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001060
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -08001061 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001062 if (IS_ERR(clk)) {
1063 ret = PTR_ERR(clk);
1064 goto rsnd_ssi_probe_done;
1065 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001066
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001067 if (of_get_property(np, "shared-pin", NULL))
1068 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
1069
1070 if (of_get_property(np, "no-busif", NULL))
1071 ssi->flags |= RSND_SSI_NO_BUSIF;
1072
1073 ssi->irq = irq_of_parse_and_map(np, 0);
1074 if (!ssi->irq) {
1075 ret = -EINVAL;
1076 goto rsnd_ssi_probe_done;
1077 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001078
Julia Lawall51930292016-08-05 10:56:51 +02001079 if (of_property_read_bool(np, "pio-transfer"))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -08001080 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001081 else
1082 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001083
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001084 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +00001085 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001086 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001087 goto rsnd_ssi_probe_done;
1088
1089 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001090 }
1091
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001092 ret = 0;
1093
1094rsnd_ssi_probe_done:
1095 of_node_put(node);
1096
1097 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001098}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001099
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +00001100void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001101{
1102 struct rsnd_ssi *ssi;
1103 int i;
1104
1105 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001106 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001107 }
1108}