blob: 46feddd78ee26f0e7fa90e40500914ef5b4edfe3 [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 Morimoto947f4eb2017-06-16 00:02:59 +0000211unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000212 int param1, int param2, int *idx)
213{
214 int ssi_clk_mul_table[] = {
215 1, 2, 4, 8, 16, 6, 12,
216 };
217 int j, ret;
Kuninori Morimoto947f4eb2017-06-16 00:02:59 +0000218 unsigned int main_rate;
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000219
220 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
221
222 /*
223 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
224 * with it is not allowed. (SSIWSR.WS_MODE with
225 * SSICR.CKDV = 000 is not allowed either).
226 * Skip it. See SSICR.CKDV
227 */
228 if (j == 0)
229 continue;
230
231 /*
232 * this driver is assuming that
233 * system word is 32bit x chan
234 * see rsnd_ssi_init()
235 */
236 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
237
238 ret = rsnd_adg_clk_query(priv, main_rate);
239 if (ret < 0)
240 continue;
241
242 if (idx)
243 *idx = j;
244
245 return main_rate;
246 }
247
Kuninori Morimoto947f4eb2017-06-16 00:02:59 +0000248 return 0;
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000249}
250
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000251static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800252 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700253{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000254 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700255 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000256 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000257 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000258 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000259 int chan = rsnd_runtime_channel_for_ssi(io);
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000260 int idx, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700261 unsigned int main_rate;
Kuninori Morimotocbf14942016-03-07 05:08:33 +0000262 unsigned int rate = rsnd_io_is_play(io) ?
263 rsnd_src_get_out_rate(priv, io) :
264 rsnd_src_get_in_rate(priv, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700265
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000266 if (!rsnd_rdai_is_clk_master(rdai))
267 return 0;
268
269 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
270 return 0;
271
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000272 if (rsnd_ssi_is_multi_slave(mod, io))
273 return 0;
274
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000275 if (ssi->usrcnt > 1) {
276 if (ssi->rate != rate) {
277 dev_err(dev, "SSI parent/child should use same rate\n");
278 return -EINVAL;
279 }
280
281 return 0;
282 }
283
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000284 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
Kuninori Morimoto947f4eb2017-06-16 00:02:59 +0000285 if (!main_rate) {
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000286 dev_err(dev, "unsupported clock rate\n");
287 return -EIO;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700288 }
289
Kuninori Morimotoef4cf5d2017-06-15 00:50:02 +0000290 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
291 if (ret < 0)
292 return ret;
293
294 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
295 ssi->wsr = CONT;
296 ssi->rate = rate;
297
298 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
299 rsnd_mod_name(mod),
300 rsnd_mod_id(mod), rate);
301
302 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700303}
304
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000305static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000306 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700307{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000308 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000309 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000310 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700311
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000312 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700313 return;
314
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000315 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
316 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700317
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000318 if (ssi->usrcnt > 1)
319 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700320
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000321 ssi->cr_clk = 0;
322 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700323
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000324 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700325}
326
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000327static void rsnd_ssi_config_init(struct rsnd_mod *mod,
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000328 struct rsnd_dai_stream *io)
329{
330 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
331 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000332 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000333 u32 cr_own;
334 u32 cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000335 u32 wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000336 int is_tdm;
337
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000338 is_tdm = rsnd_runtime_is_ssi_tdm(io);
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000339
340 /*
341 * always use 32bit system word.
342 * see also rsnd_ssi_master_clk_enable()
343 */
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000344 cr_own = FORCE | SWL_32;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000345
346 if (rdai->bit_clk_inv)
347 cr_own |= SCKP;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000348 if (rdai->frm_clk_inv ^ is_tdm)
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000349 cr_own |= SWSP;
350 if (rdai->data_alignment)
351 cr_own |= SDTA;
352 if (rdai->sys_delay)
353 cr_own |= DEL;
354 if (rsnd_io_is_play(io))
355 cr_own |= TRMD;
356
357 switch (runtime->sample_bits) {
358 case 16:
359 cr_own |= DWL_16;
360 break;
361 case 32:
362 cr_own |= DWL_24;
363 break;
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000364 }
365
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000366 if (rsnd_ssi_is_dma_mode(mod)) {
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000367 cr_mode = UIEN | OIEN | /* over/under run */
368 DMEN; /* DMA : enable DMA */
369 } else {
370 cr_mode = DIEN; /* PIO : enable Data interrupt */
371 }
372
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000373 /*
374 * TDM Extend Mode
375 * see
376 * rsnd_ssiu_init_gen2()
377 */
378 wsr = ssi->wsr;
Kuninori Morimotof98ed112015-12-02 07:34:28 +0000379 if (is_tdm) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000380 wsr |= WS_MODE;
381 cr_own |= CHNL_8;
382 }
383
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000384 ssi->cr_own = cr_own;
385 ssi->cr_mode = cr_mode;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000386 ssi->wsr = wsr;
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000387}
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000388
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000389static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
390{
391 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
392
393 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
394 rsnd_mod_write(mod, SSICR, ssi->cr_own |
395 ssi->cr_clk |
396 ssi->cr_mode); /* without EN */
Kuninori Morimoto840ada32015-11-30 08:52:38 +0000397}
398
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000399static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
400 struct rsnd_dai_stream *io)
401{
402 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
403 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
404
405 ssi->byte_pos = 0;
406 ssi->period_pos = 0;
407 ssi->byte_per_period = runtime->period_size *
408 runtime->channels *
409 samples_to_bytes(runtime, 1);
410 ssi->next_period_byte = ssi->byte_per_period;
411}
412
413static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
414 struct rsnd_dai_stream *io,
415 int additional)
416{
417 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
418 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
419 int pos = ssi->byte_pos + additional;
420
421 pos %= (runtime->periods * ssi->byte_per_period);
422
423 return pos;
424}
425
426static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
427 struct rsnd_dai_stream *io,
428 int byte)
429{
430 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
431
432 ssi->byte_pos += byte;
433
434 if (ssi->byte_pos >= ssi->next_period_byte) {
435 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
436
437 ssi->period_pos++;
438 ssi->next_period_byte += ssi->byte_per_period;
439
440 if (ssi->period_pos >= runtime->periods) {
441 ssi->byte_pos = 0;
442 ssi->period_pos = 0;
443 ssi->next_period_byte = ssi->byte_per_period;
444 }
445
446 return true;
447 }
448
449 return false;
450}
451
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700452/*
453 * SSI mod common functions
454 */
455static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000456 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000457 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700458{
459 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000460 int ret;
461
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000462 if (!rsnd_ssi_is_run_mods(mod, io))
463 return 0;
464
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000465 rsnd_ssi_pointer_init(mod, io);
466
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000467 ssi->usrcnt++;
468
469 rsnd_mod_power_on(mod);
470
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000471 ret = rsnd_ssi_master_clk_start(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000472 if (ret < 0)
473 return ret;
474
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000475 if (!rsnd_ssi_is_parent(mod, io))
476 rsnd_ssi_config_init(mod, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700477
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000478 rsnd_ssi_register_setup(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000479
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000480 /* clear error status */
481 rsnd_ssi_status_clear(mod);
482
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700483 return 0;
484}
485
486static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000487 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000488 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700489{
490 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700491 struct device *dev = rsnd_priv_to_dev(priv);
492
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000493 if (!rsnd_ssi_is_run_mods(mod, io))
494 return 0;
495
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100496 if (!ssi->usrcnt) {
497 dev_err(dev, "%s[%d] usrcnt error\n",
498 rsnd_mod_name(mod), rsnd_mod_id(mod));
499 return -EIO;
500 }
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000501
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000502 if (!rsnd_ssi_is_parent(mod, io))
Andrzej Hajdae5d9cfc2015-12-24 08:02:39 +0100503 ssi->cr_own = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700504
Kuninori Morimoto26d34b12016-02-18 08:14:37 +0000505 rsnd_ssi_master_clk_stop(mod, io);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000506
507 rsnd_mod_power_off(mod);
508
509 ssi->usrcnt--;
510
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700511 return 0;
512}
513
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000514static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000515 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000516 struct snd_pcm_substream *substream,
517 struct snd_pcm_hw_params *params)
518{
519 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000520 int chan = params_channels(params);
521
522 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000523 * snd_pcm_ops::hw_params will be called *before*
524 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
525 * in 1st call.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000526 */
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000527 if (ssi->usrcnt) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000528 /*
Kuninori Morimoto6bf66b12016-12-07 02:05:22 +0000529 * Already working.
530 * It will happen if SSI has parent/child connection.
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000531 * it is error if child <-> parent SSI uses
532 * different channels.
533 */
534 if (ssi->chan != chan)
535 return -EIO;
536 }
537
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000538 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000539
540 return 0;
541}
542
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000543static int rsnd_ssi_start(struct rsnd_mod *mod,
544 struct rsnd_dai_stream *io,
545 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000546{
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000547 if (!rsnd_ssi_is_run_mods(mod, io))
548 return 0;
549
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000550 /*
551 * EN will be set via SSIU :: SSI_CONTROL
552 * if Multi channel mode
553 */
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000554 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000555 return 0;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000556
Kuninori Morimoto0dc6bf72016-02-18 08:17:18 +0000557 rsnd_mod_bset(mod, SSICR, EN, EN);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000558
559 return 0;
560}
561
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000562static int rsnd_ssi_stop(struct rsnd_mod *mod,
563 struct rsnd_dai_stream *io,
564 struct rsnd_priv *priv)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000565{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700566 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000567 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700568
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000569 if (!rsnd_ssi_is_run_mods(mod, io))
570 return 0;
571
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000572 /*
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000573 * don't stop if not last user
574 * see also
575 * rsnd_ssi_start
576 * rsnd_ssi_interrupt
577 */
578 if (ssi->usrcnt > 1)
579 return 0;
580
581 /*
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000582 * disable all IRQ,
583 * and, wait all data was sent
584 */
585 cr = ssi->cr_own |
586 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700587
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000588 rsnd_mod_write(mod, SSICR, cr | EN);
589 rsnd_ssi_status_check(mod, DIRQ);
590
591 /*
592 * disable SSI,
593 * and, wait idle state
594 */
595 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
596 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000597
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700598 return 0;
599}
600
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000601static int rsnd_ssi_irq(struct rsnd_mod *mod,
602 struct rsnd_dai_stream *io,
603 struct rsnd_priv *priv,
604 int enable)
605{
606 u32 val = 0;
607
608 if (rsnd_is_gen1(priv))
609 return 0;
610
611 if (rsnd_ssi_is_parent(mod, io))
612 return 0;
613
Kuninori Morimotofd9adcf2016-02-18 08:19:12 +0000614 if (!rsnd_ssi_is_run_mods(mod, io))
615 return 0;
616
Kuninori Morimoto615fb6c2016-02-18 08:18:16 +0000617 if (enable)
618 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
619
620 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
621
622 return 0;
623}
624
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000625static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
626 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000627{
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000628 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000629 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000630 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000631 bool elapsed = false;
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000632 bool stop = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000633
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000634 spin_lock(&priv->lock);
635
636 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000637 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000638 goto rsnd_ssi_interrupt_out;
639
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000640 status = rsnd_ssi_status_get(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000641
642 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000643 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000644 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
645 u32 *buf = (u32 *)(runtime->dma_area +
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000646 rsnd_ssi_pointer_offset(mod, io, 0));
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000647 int shift = 0;
648
649 switch (runtime->sample_bits) {
650 case 32:
651 shift = 8;
652 break;
653 }
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000654
655 /*
656 * 8/16/32 data can be assesse to TDR/RDR register
657 * directly as 32bit data
658 * see rsnd_ssi_init()
659 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000660 if (rsnd_io_is_play(io))
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000661 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000662 else
Kuninori Morimoto7819a942017-05-24 01:17:10 +0000663 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000664
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000665 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000666 }
667
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000668 /* DMA only */
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000669 if (is_dma && (status & (UIRQ | OIRQ)))
670 stop = true;
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000671
Kuninori Morimoto5342dff2015-11-26 11:13:40 +0000672 rsnd_ssi_status_clear(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000673rsnd_ssi_interrupt_out:
674 spin_unlock(&priv->lock);
675
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000676 if (elapsed)
677 rsnd_dai_period_elapsed(io);
Kuninori Morimoto6a25c8d2016-01-26 04:56:14 +0000678
679 if (stop)
680 snd_pcm_stop_xrun(io->substream);
681
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000682}
683
684static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
685{
686 struct rsnd_mod *mod = data;
687
688 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000689
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000690 return IRQ_HANDLED;
691}
692
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000693/*
694 * SSI PIO
695 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000696static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000697 struct rsnd_dai_stream *io)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000698{
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000699 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
700 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
701
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000702 if (!__rsnd_ssi_is_pin_sharing(mod))
703 return;
704
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000705 if (!rsnd_rdai_is_clk_master(rdai))
706 return;
707
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000708 switch (rsnd_mod_id(mod)) {
709 case 1:
710 case 2:
711 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
712 break;
713 case 4:
714 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
715 break;
716 case 8:
717 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
718 break;
719 }
720}
721
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000722static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
723 struct rsnd_dai_stream *io,
724 struct snd_soc_pcm_runtime *rtd)
725{
726 /*
727 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
728 * and, pcm_new will be called after it.
729 * This function reuse pcm_new at this point.
730 */
731 rsnd_ssi_parent_attach(mod, io);
732
733 return 0;
734}
735
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000736static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
737 struct rsnd_dai_stream *io,
738 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000739{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000740 struct device *dev = rsnd_priv_to_dev(priv);
741 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000742 int ret;
743
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000744 /*
745 * SSIP/SSIU/IRQ are not needed on
746 * SSI Multi slaves
747 */
748 if (rsnd_ssi_is_multi_slave(mod, io))
749 return 0;
750
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000751 /*
752 * It can't judge ssi parent at this point
753 * see rsnd_ssi_pcm_new()
754 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000755
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000756 ret = rsnd_ssiu_attach(io, mod);
757 if (ret < 0)
758 return ret;
759
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000760 /*
761 * SSI might be called again as PIO fallback
762 * It is easy to manual handling for IRQ request/free
763 */
764 ret = request_irq(ssi->irq,
765 rsnd_ssi_interrupt,
766 IRQF_SHARED,
767 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000768
769 return ret;
770}
771
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000772static int rsnd_ssi_pointer(struct rsnd_mod *mod,
773 struct rsnd_dai_stream *io,
774 snd_pcm_uframes_t *pointer)
775{
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000776 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000777 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
778
Kuninori Morimotoa97a06c2017-06-07 00:20:47 +0000779 *pointer = bytes_to_frames(runtime, ssi->byte_pos);
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000780
781 return 0;
782}
783
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700784static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700785 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000786 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700787 .init = rsnd_ssi_init,
788 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000789 .start = rsnd_ssi_start,
790 .stop = rsnd_ssi_stop,
Kuninori Morimotob5b442a2016-01-26 04:56:57 +0000791 .irq = rsnd_ssi_irq,
Kuninori Morimoto07b7acb2017-06-07 00:20:01 +0000792 .pointer= rsnd_ssi_pointer,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000793 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000794 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700795};
796
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800797static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000798 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000799 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800800{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800801 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800802 int ret;
803
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000804 /*
805 * SSIP/SSIU/IRQ/DMA are not needed on
806 * SSI Multi slaves
807 */
808 if (rsnd_ssi_is_multi_slave(mod, io))
809 return 0;
810
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000811 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000812 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000813 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000814
Kuninori Morimoto355cb84fb2016-01-21 01:58:33 +0000815 /* SSI probe might be called many times in MUX multi path */
Kuninori Morimotob99305d2016-10-25 00:36:13 +0000816 ret = rsnd_dma_attach(io, mod, &ssi->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700817
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800818 return ret;
819}
820
821static int rsnd_ssi_dma_remove(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 Morimotoff8f30e2014-03-03 20:50:49 -0800824{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000825 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto1f8754d2017-05-16 01:48:24 +0000826 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
827
828 /* Do nothing for SSI parent mod */
829 if (ssi_parent_mod == mod)
830 return 0;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000831
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000832 /* PIO will request IRQ again */
Kuninori Morimoto701172dc2016-10-25 00:36:34 +0000833 free_irq(ssi->irq, mod);
Kuninori Morimoto0af5c012016-10-19 03:56:26 +0000834
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000835 return 0;
836}
837
838static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000839 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000840 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000841{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800842 struct device *dev = rsnd_priv_to_dev(priv);
843
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800844 /*
845 * fallback to PIO
846 *
847 * SSI .probe might be called again.
848 * see
849 * rsnd_rdai_continuance_probe()
850 */
851 mod->ops = &rsnd_ssi_pio_ops;
852
853 dev_info(dev, "%s[%d] fallback to PIO mode\n",
854 rsnd_mod_name(mod), rsnd_mod_id(mod));
855
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800856 return 0;
857}
858
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000859static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
860 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700861{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000862 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000863 int is_play = rsnd_io_is_play(io);
864 char *name;
865
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000866 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000867 name = is_play ? "rxu" : "txu";
868 else
869 name = is_play ? "rx" : "tx";
870
871 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
872 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700873}
874
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700875static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700876 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000877 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800878 .probe = rsnd_ssi_dma_probe,
879 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700880 .init = rsnd_ssi_init,
881 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000882 .start = rsnd_ssi_start,
883 .stop = rsnd_ssi_stop,
Kuninori Morimotoc8e969a2016-02-18 08:16:43 +0000884 .irq = rsnd_ssi_irq,
Kuninori Morimoto098bd892016-02-18 08:17:52 +0000885 .pcm_new = rsnd_ssi_pcm_new,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000886 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000887 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700888};
889
Kuninori Morimoto05795412014-11-27 08:05:01 +0000890int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
891{
892 return mod->ops == &rsnd_ssi_dma_ops;
893}
894
895
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700896/*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700897 * ssi mod function
898 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000899static void rsnd_ssi_connect(struct rsnd_mod *mod,
900 struct rsnd_dai_stream *io)
901{
902 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
903 enum rsnd_mod_type types[] = {
904 RSND_MOD_SSI,
905 RSND_MOD_SSIM1,
906 RSND_MOD_SSIM2,
907 RSND_MOD_SSIM3,
908 };
909 enum rsnd_mod_type type;
910 int i;
911
912 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
913 for (i = 0; i < ARRAY_SIZE(types); i++) {
914 type = types[i];
915 if (!rsnd_io_to_mod(io, type)) {
916 rsnd_dai_connect(mod, io, type);
Kuninori Morimoto1ff95932017-06-15 00:49:27 +0000917 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
918 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000919 return;
920 }
921 }
922}
923
924void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
925 struct device_node *playback,
926 struct device_node *capture)
927{
928 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
929 struct device_node *node;
930 struct device_node *np;
931 struct rsnd_mod *mod;
932 int i;
933
934 node = rsnd_ssi_of_node(priv);
935 if (!node)
936 return;
937
938 i = 0;
939 for_each_child_of_node(node, np) {
940 mod = rsnd_ssi_mod_get(priv, i);
941 if (np == playback)
942 rsnd_ssi_connect(mod, &rdai->playback);
943 if (np == capture)
944 rsnd_ssi_connect(mod, &rdai->capture);
945 i++;
946 }
947
948 of_node_put(node);
949}
950
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000951static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
952 struct rsnd_dai_stream *io,
953 struct device_node *remote_ep)
954{
955 struct device *dev = rsnd_priv_to_dev(priv);
956 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
957 struct rsnd_ssi *ssi;
958
959 if (!mod)
960 return;
961
962 ssi = rsnd_mod_to_ssi(mod);
963
964 if (strstr(remote_ep->full_name, "hdmi0")) {
965 ssi->flags |= RSND_SSI_HDMI0;
966 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
967 rsnd_mod_name(mod), rsnd_mod_id(mod));
968 }
969
970 if (strstr(remote_ep->full_name, "hdmi1")) {
971 ssi->flags |= RSND_SSI_HDMI1;
972 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
973 rsnd_mod_name(mod), rsnd_mod_id(mod));
974 }
975}
976
977void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
978 struct device_node *endpoint,
979 int dai_i)
980{
981 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
982 struct device_node *remote_ep;
983
984 remote_ep = of_graph_get_remote_endpoint(endpoint);
985 if (!remote_ep)
986 return;
987
988 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
989 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
990}
991
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700992struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
993{
Takashi Iwai8b147192013-11-05 18:40:05 +0100994 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
995 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700996
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000997 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700998}
999
Kuninori Morimotob415b4d2015-10-22 03:15:46 +00001000int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -08001001{
1002 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1003
1004 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
1005}
1006
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +00001007static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1008 struct rsnd_mod *mod,
1009 enum rsnd_mod_type type)
1010{
1011 /*
1012 * SSIP (= SSI parent) needs to be special, otherwise,
1013 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1014 *
1015 * We can't include parent SSI status on SSI, because we don't know
1016 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1017 * ex) trouble case
1018 * Playback: SSI0
1019 * Capture : SSI1 (needs SSI0)
1020 *
1021 * 1) start Capture -> SSI0/SSI1 are started.
1022 * 2) start Playback -> SSI0 doesn't work, because it is already
1023 * marked as "started" on 1)
1024 *
1025 * OTOH, using each mod's status is good for MUX case.
1026 * It doesn't need to start in 2nd start
1027 * ex)
1028 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1029 * |
1030 * IO-1: SRC1 -> CTU2 -+
1031 *
1032 * 1) start IO-0 -> start SSI0
1033 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1034 * already started on 1)
1035 */
1036 if (type == RSND_MOD_SSIP)
1037 return &io->parent_ssi_status;
1038
1039 return rsnd_mod_get_status(io, mod, type);
1040}
1041
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +00001042int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001043{
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001044 struct device_node *node;
1045 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001046 struct device *dev = rsnd_priv_to_dev(priv);
1047 struct rsnd_mod_ops *ops;
1048 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001049 struct rsnd_ssi *ssi;
1050 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001051 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001052
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001053 node = rsnd_ssi_of_node(priv);
1054 if (!node)
1055 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001056
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001057 nr = of_get_child_count(node);
1058 if (!nr) {
1059 ret = -EINVAL;
1060 goto rsnd_ssi_probe_done;
1061 }
1062
Kuninori Morimotodd27d802014-01-23 18:39:40 -08001063 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001064 if (!ssi) {
1065 ret = -ENOMEM;
1066 goto rsnd_ssi_probe_done;
1067 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001068
Kuninori Morimotodd27d802014-01-23 18:39:40 -08001069 priv->ssi = ssi;
1070 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001071
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001072 i = 0;
1073 for_each_child_of_node(node, np) {
1074 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001075
Kuninori Morimoto8aefda52014-05-22 23:25:43 -07001076 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1077 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001078
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -08001079 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001080 if (IS_ERR(clk)) {
1081 ret = PTR_ERR(clk);
1082 goto rsnd_ssi_probe_done;
1083 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001084
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001085 if (of_get_property(np, "shared-pin", NULL))
1086 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
1087
1088 if (of_get_property(np, "no-busif", NULL))
1089 ssi->flags |= RSND_SSI_NO_BUSIF;
1090
1091 ssi->irq = irq_of_parse_and_map(np, 0);
1092 if (!ssi->irq) {
1093 ret = -EINVAL;
1094 goto rsnd_ssi_probe_done;
1095 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001096
Julia Lawall51930292016-08-05 10:56:51 +02001097 if (of_property_read_bool(np, "pio-transfer"))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -08001098 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001099 else
1100 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001101
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001102 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +00001103 rsnd_ssi_get_status, RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001104 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001105 goto rsnd_ssi_probe_done;
1106
1107 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001108 }
1109
Kuninori Morimoto02534f22015-11-10 05:11:35 +00001110 ret = 0;
1111
1112rsnd_ssi_probe_done:
1113 of_node_put(node);
1114
1115 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001116}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001117
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +00001118void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001119{
1120 struct rsnd_ssi *ssi;
1121 int i;
1122
1123 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +00001124 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +00001125 }
1126}