blob: 0fe5e3068b6befef317ea0e6dc3281a7a14d1481 [file] [log] [blame]
Kuninori Morimotoae5c3222013-07-21 21:36:57 -07001/*
2 * Renesas R-Car SSIU/SSI support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/delay.h>
15#include "rsnd.h"
16#define RSND_SSI_NAME_SIZE 16
17
18/*
19 * SSICR
20 */
21#define FORCE (1 << 31) /* Fixed */
Kuninori Morimoto849fc822013-07-28 18:59:02 -070022#define DMEN (1 << 28) /* DMA Enable */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070023#define UIEN (1 << 27) /* Underflow Interrupt Enable */
24#define OIEN (1 << 26) /* Overflow Interrupt Enable */
25#define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
26#define DIEN (1 << 24) /* Data Interrupt Enable */
27
28#define DWL_8 (0 << 19) /* Data Word Length */
29#define DWL_16 (1 << 19) /* Data Word Length */
30#define DWL_18 (2 << 19) /* Data Word Length */
31#define DWL_20 (3 << 19) /* Data Word Length */
32#define DWL_22 (4 << 19) /* Data Word Length */
33#define DWL_24 (5 << 19) /* Data Word Length */
34#define DWL_32 (6 << 19) /* Data Word Length */
35
36#define SWL_32 (3 << 16) /* R/W System Word Length */
37#define SCKD (1 << 15) /* Serial Bit Clock Direction */
38#define SWSD (1 << 14) /* Serial WS Direction */
39#define SCKP (1 << 13) /* Serial Bit Clock Polarity */
40#define SWSP (1 << 12) /* Serial WS Polarity */
41#define SDTA (1 << 10) /* Serial Data Alignment */
Kuninori Morimotof46a93b2015-11-17 08:28:11 +000042#define PDTA (1 << 9) /* Parallel Data Alignment */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070043#define DEL (1 << 8) /* Serial Data Delay */
44#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
45#define TRMD (1 << 1) /* Transmit/Receive Mode Select */
46#define EN (1 << 0) /* SSI Module Enable */
47
48/*
49 * SSISR
50 */
51#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
52#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
53#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
54#define DIRQ (1 << 24) /* Data Interrupt Status Flag */
55
Kuninori Morimoto849fc822013-07-28 18:59:02 -070056/*
57 * SSIWSR
58 */
59#define CONT (1 << 8) /* WS Continue Function */
60
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070061#define SSI_NAME "ssi"
62
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070063struct rsnd_ssi {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070064 struct rsnd_ssi *parent;
65 struct rsnd_mod mod;
Kuninori Morimoto940e9472015-10-26 08:42:25 +000066 struct rsnd_mod *dma;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070067
Kuninori Morimoto02534f22015-11-10 05:11:35 +000068 u32 flags;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070069 u32 cr_own;
70 u32 cr_clk;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000071 u32 cr_mode;
Kuninori Morimoto919567d2015-04-10 08:50:30 +000072 int chan;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000073 int rate;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070074 int err;
Kuninori Morimoto02534f22015-11-10 05:11:35 +000075 int irq;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070076 unsigned int usrcnt;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070077};
78
Kuninori Morimoto02534f22015-11-10 05:11:35 +000079/* flags */
80#define RSND_SSI_CLK_PIN_SHARE (1 << 0)
81#define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
82
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070083#define for_each_rsnd_ssi(pos, priv, i) \
84 for (i = 0; \
85 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080086 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070087 i++)
88
Kuninori Morimoto02534f22015-11-10 05:11:35 +000089#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
Kuninori Morimoto232c00b2015-10-26 08:38:26 +000090#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
Kuninori Morimotodd27d802014-01-23 18:39:40 -080091#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070092#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto02534f22015-11-10 05:11:35 +000093#define rsnd_ssi_mode_flags(p) ((p)->flags)
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +000094#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070095
Kuninori Morimotob415b4d2015-10-22 03:15:46 +000096int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
Kuninori Morimotod9288d02014-06-22 17:56:23 -070097{
Kuninori Morimotob415b4d2015-10-22 03:15:46 +000098 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimotod9288d02014-06-22 17:56:23 -070099 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700100 int use_busif = 0;
101
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000102 if (!rsnd_ssi_is_dma_mode(mod))
103 return 0;
104
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700105 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
106 use_busif = 1;
107 if (rsnd_io_to_mod_src(io))
108 use_busif = 1;
109
110 return use_busif;
111}
112
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000113static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
114{
115 rsnd_mod_write(mod, SSISR, 0);
116}
117
118static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
119{
120 return rsnd_mod_read(mod, SSISR);
121}
122
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700123static void rsnd_ssi_status_check(struct rsnd_mod *mod,
124 u32 bit)
125{
126 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
127 struct device *dev = rsnd_priv_to_dev(priv);
128 u32 status;
129 int i;
130
131 for (i = 0; i < 1024; i++) {
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000132 status = rsnd_ssi_status_get(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700133 if (status & bit)
134 return;
135
136 udelay(50);
137 }
138
139 dev_warn(dev, "status check failed\n");
140}
141
Kuninori Morimoto37447b42015-10-26 08:40:41 +0000142static int rsnd_ssi_irq_enable(struct rsnd_mod *ssi_mod)
143{
144 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
145
146 if (rsnd_is_gen1(priv))
147 return 0;
148
149 /* enable SSI interrupt if Gen2 */
150 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
151 rsnd_ssi_is_dma_mode(ssi_mod) ?
152 0x0e000000 : 0x0f000000);
153
154 return 0;
155}
156
157static int rsnd_ssi_irq_disable(struct rsnd_mod *ssi_mod)
158{
159 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
160
161 if (rsnd_is_gen1(priv))
162 return 0;
163
164 /* disable SSI interrupt if Gen2 */
165 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
166
167 return 0;
168}
169
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700170static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800171 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700172{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000173 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800174 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700175 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000176 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000177 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000178 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000179 int j, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700180 int ssi_clk_mul_table[] = {
181 1, 2, 4, 8, 16, 6, 12,
182 };
183 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800184 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700185
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000186 if (!rsnd_rdai_is_clk_master(rdai))
187 return 0;
188
189 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
190 return 0;
191
192 if (ssi->usrcnt > 1) {
193 if (ssi->rate != rate) {
194 dev_err(dev, "SSI parent/child should use same rate\n");
195 return -EINVAL;
196 }
197
198 return 0;
199 }
200
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700201 /*
202 * Find best clock, and try to start ADG
203 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000204 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700205
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000206 /*
207 * this driver is assuming that
208 * system word is 64fs (= 2 x 32bit)
209 * see rsnd_ssi_init()
210 */
211 main_rate = rate * 32 * 2 * ssi_clk_mul_table[j];
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700212
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000213 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
214 if (0 == ret) {
215 ssi->cr_clk = FORCE | SWL_32 |
216 SCKD | SWSD | CKDV(j);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700217
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000218 ssi->rate = rate;
219
220 rsnd_mod_write(mod, SSIWSR, CONT);
221
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000222 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
223 rsnd_mod_name(mod),
224 rsnd_mod_id(mod), rate);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700225
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000226 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700227 }
228 }
229
230 dev_err(dev, "unsupported clock rate\n");
231 return -EIO;
232}
233
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000234static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi,
235 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700236{
Kuninori Morimotof708d942015-01-15 08:07:19 +0000237 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000238 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000239 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700240
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000241 if (!rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700242 return;
243
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000244 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
245 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700246
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000247 if (ssi->usrcnt > 1)
248 return;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700249
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000250 ssi->cr_clk = 0;
251 ssi->rate = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700252
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000253 rsnd_adg_ssi_clk_stop(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700254}
255
256/*
257 * SSI mod common functions
258 */
259static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000260 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000261 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700262{
263 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000264 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700265 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
266 u32 cr;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000267 int ret;
268
269 ssi->usrcnt++;
270
271 rsnd_mod_power_on(mod);
272
273 ret = rsnd_ssi_master_clk_start(ssi, io);
274 if (ret < 0)
275 return ret;
276
277 if (rsnd_ssi_is_parent(mod, io))
278 return 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700279
Kuninori Morimotof46a93b2015-11-17 08:28:11 +0000280 cr = FORCE | PDTA;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700281
282 /*
283 * always use 32bit system word for easy clock calculation.
284 * see also rsnd_ssi_master_clk_enable()
285 */
286 cr |= SWL_32;
287
288 /*
289 * init clock settings for SSICR
290 */
291 switch (runtime->sample_bits) {
292 case 16:
293 cr |= DWL_16;
294 break;
295 case 32:
296 cr |= DWL_24;
297 break;
298 default:
299 return -EIO;
300 }
301
302 if (rdai->bit_clk_inv)
303 cr |= SCKP;
304 if (rdai->frm_clk_inv)
305 cr |= SWSP;
306 if (rdai->data_alignment)
307 cr |= SDTA;
308 if (rdai->sys_delay)
309 cr |= DEL;
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000310 if (rsnd_io_is_play(io))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700311 cr |= TRMD;
312
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700313 ssi->cr_own = cr;
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000314
315 if (rsnd_ssi_is_dma_mode(mod)) {
316 cr = UIEN | OIEN | /* over/under run */
317 DMEN; /* DMA : enable DMA */
318 } else {
319 cr = DIEN; /* PIO : enable Data interrupt */
320 }
321
322 ssi->cr_mode = cr;
323
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700324 ssi->err = -1; /* ignore 1st error */
325
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000326 /* clear error status */
327 rsnd_ssi_status_clear(mod);
328
329 rsnd_ssi_irq_enable(mod);
330
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700331 return 0;
332}
333
334static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000335 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000336 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700337{
338 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700339 struct device *dev = rsnd_priv_to_dev(priv);
340
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000341 if (rsnd_ssi_is_parent(mod, io))
342 goto rsnd_ssi_quit_end;
343
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700344 if (ssi->err > 0)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000345 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
346 rsnd_mod_name(mod), rsnd_mod_id(mod), ssi->err);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700347
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700348 ssi->cr_own = 0;
349 ssi->err = 0;
350
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000351 rsnd_ssi_irq_disable(mod);
352
353rsnd_ssi_quit_end:
354 rsnd_ssi_master_clk_stop(ssi, io);
355
356 rsnd_mod_power_off(mod);
357
358 ssi->usrcnt--;
359
360 if (ssi->usrcnt < 0)
361 dev_err(dev, "%s[%d] usrcnt error\n",
362 rsnd_mod_name(mod), rsnd_mod_id(mod));
363
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700364 return 0;
365}
366
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000367static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000368 struct rsnd_dai_stream *io,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000369 struct snd_pcm_substream *substream,
370 struct snd_pcm_hw_params *params)
371{
372 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000373 int chan = params_channels(params);
374
375 /*
376 * Already working.
377 * It will happen if SSI has parent/child connection.
378 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000379 if (ssi->usrcnt > 1) {
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000380 /*
381 * it is error if child <-> parent SSI uses
382 * different channels.
383 */
384 if (ssi->chan != chan)
385 return -EIO;
386 }
387
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000388 ssi->chan = chan;
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000389
390 return 0;
391}
392
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000393static u32 rsnd_ssi_record_error(struct rsnd_ssi *ssi)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700394{
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000395 struct rsnd_mod *mod = rsnd_mod_get(ssi);
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000396 u32 status = rsnd_ssi_status_get(mod);
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000397
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700398 /* under/over flow error */
399 if (status & (UIRQ | OIRQ)) {
400 ssi->err++;
401
402 /* clear error status */
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000403 rsnd_ssi_status_clear(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700404 }
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000405
406 return status;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700407}
408
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000409static int __rsnd_ssi_start(struct rsnd_mod *mod,
410 struct rsnd_dai_stream *io,
411 struct rsnd_priv *priv)
412{
413 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
414 u32 cr;
415
416 cr = ssi->cr_own |
417 ssi->cr_clk |
418 ssi->cr_mode |
419 EN;
420
421 rsnd_mod_write(mod, SSICR, cr);
422
423 return 0;
424}
425
Kuninori Morimoto49229852014-11-27 08:07:17 +0000426static int rsnd_ssi_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000427 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000428 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700429{
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000430 /*
431 * no limit to start
432 * see also
433 * rsnd_ssi_stop
434 * rsnd_ssi_interrupt
435 */
436 return __rsnd_ssi_start(mod, io, priv);
437}
438
439static int __rsnd_ssi_stop(struct rsnd_mod *mod,
440 struct rsnd_dai_stream *io,
441 struct rsnd_priv *priv)
442{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700443 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000444 u32 cr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700445
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000446 /*
447 * disable all IRQ,
448 * and, wait all data was sent
449 */
450 cr = ssi->cr_own |
451 ssi->cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700452
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000453 rsnd_mod_write(mod, SSICR, cr | EN);
454 rsnd_ssi_status_check(mod, DIRQ);
455
456 /*
457 * disable SSI,
458 * and, wait idle state
459 */
460 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
461 rsnd_ssi_status_check(mod, IIRQ);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000462
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700463 return 0;
464}
465
Kuninori Morimoto49229852014-11-27 08:07:17 +0000466static int rsnd_ssi_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000467 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000468 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700469{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700470 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
471
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000472 /*
473 * don't stop if not last user
474 * see also
475 * rsnd_ssi_start
476 * rsnd_ssi_interrupt
477 */
478 if (ssi->usrcnt > 1)
479 return 0;
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000480
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000481 return __rsnd_ssi_stop(mod, io, priv);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700482}
483
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000484static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
485 struct rsnd_dai_stream *io)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000486{
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000487 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000488 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000489 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000490 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000491 u32 status;
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000492 bool elapsed = false;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000493
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000494 spin_lock(&priv->lock);
495
496 /* ignore all cases if not working */
Kuninori Morimotod5bbe7d2015-06-15 06:27:47 +0000497 if (!rsnd_io_is_working(io))
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000498 goto rsnd_ssi_interrupt_out;
499
Kuninori Morimotoe10369d2015-10-26 08:42:09 +0000500 status = rsnd_ssi_record_error(ssi);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000501
502 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000503 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000504 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
505 u32 *buf = (u32 *)(runtime->dma_area +
506 rsnd_dai_pointer_offset(io, 0));
507
508 /*
509 * 8/16/32 data can be assesse to TDR/RDR register
510 * directly as 32bit data
511 * see rsnd_ssi_init()
512 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000513 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000514 rsnd_mod_write(mod, SSITDR, *buf);
515 else
516 *buf = rsnd_mod_read(mod, SSIRDR);
517
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000518 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000519 }
520
Kuninori Morimoto12927a82015-06-15 06:20:54 +0000521 /* DMA only */
522 if (is_dma && (status & (UIRQ | OIRQ))) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000523 /*
524 * restart SSI
525 */
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000526 dev_dbg(dev, "%s[%d] restart\n",
527 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto044930b2015-03-19 04:13:47 +0000528
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000529 __rsnd_ssi_stop(mod, io, priv);
530 __rsnd_ssi_start(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000531 }
532
Kuninori Morimoto69e32a52015-10-26 08:41:36 +0000533 if (ssi->err > 1024) {
534 rsnd_ssi_irq_disable(mod);
535
536 dev_warn(dev, "no more %s[%d] restart\n",
537 rsnd_mod_name(mod), rsnd_mod_id(mod));
538 }
539
Kuninori Morimoto02299d92015-05-21 03:50:23 +0000540rsnd_ssi_interrupt_out:
541 spin_unlock(&priv->lock);
542
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000543 if (elapsed)
544 rsnd_dai_period_elapsed(io);
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000545}
546
547static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
548{
549 struct rsnd_mod *mod = data;
550
551 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
Kuninori Morimoto75defee2015-06-15 06:21:15 +0000552
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000553 return IRQ_HANDLED;
554}
555
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000556/*
557 * SSI PIO
558 */
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000559static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
560 struct rsnd_dai_stream *io,
561 struct rsnd_priv *priv)
562{
563 if (!__rsnd_ssi_is_pin_sharing(mod))
564 return;
565
566 switch (rsnd_mod_id(mod)) {
567 case 1:
568 case 2:
569 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
570 break;
571 case 4:
572 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
573 break;
574 case 8:
575 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
576 break;
577 }
578}
579
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000580static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
581 struct rsnd_dai_stream *io,
582 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000583{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000584 struct device *dev = rsnd_priv_to_dev(priv);
585 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000586 int ret;
587
Kuninori Morimotoe7d850d2015-10-26 08:43:57 +0000588 rsnd_ssi_parent_attach(mod, io, priv);
589
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000590 ret = rsnd_ssiu_attach(io, mod);
591 if (ret < 0)
592 return ret;
593
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000594 ret = devm_request_irq(dev, ssi->irq,
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000595 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000596 IRQF_SHARED,
Kuninori Morimotobfc0cfe2015-06-15 06:26:56 +0000597 dev_name(dev), mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000598
599 return ret;
600}
601
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700602static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700603 .name = SSI_NAME,
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000604 .probe = rsnd_ssi_common_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700605 .init = rsnd_ssi_init,
606 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000607 .start = rsnd_ssi_start,
608 .stop = rsnd_ssi_stop,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000609 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700610};
611
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800612static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000613 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000614 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800615{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800616 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000617 int dma_id = 0; /* not needed */
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800618 int ret;
619
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000620 ret = rsnd_ssi_common_probe(mod, io, priv);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000621 if (ret)
Kuninori Morimotob543b522015-03-26 04:02:32 +0000622 return ret;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000623
Kuninori Morimoto81ecbb62015-10-26 08:39:20 +0000624 ssi->dma = rsnd_dma_attach(io, mod, dma_id);
Kuninori Morimoto232c00b2015-10-26 08:38:26 +0000625 if (IS_ERR(ssi->dma))
626 return PTR_ERR(ssi->dma);
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700627
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800628 return ret;
629}
630
631static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000632 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000633 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800634{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000635 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
636 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000637 int irq = ssi->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000638
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000639 /* PIO will request IRQ again */
Kuninori Morimotob05ce4c2015-10-22 03:13:44 +0000640 devm_free_irq(dev, irq, mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000641
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000642 return 0;
643}
644
645static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000646 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000647 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000648{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800649 struct device *dev = rsnd_priv_to_dev(priv);
650
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800651 /*
652 * fallback to PIO
653 *
654 * SSI .probe might be called again.
655 * see
656 * rsnd_rdai_continuance_probe()
657 */
658 mod->ops = &rsnd_ssi_pio_ops;
659
660 dev_info(dev, "%s[%d] fallback to PIO mode\n",
661 rsnd_mod_name(mod), rsnd_mod_id(mod));
662
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800663 return 0;
664}
665
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000666static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
667 struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700668{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000669 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000670 int is_play = rsnd_io_is_play(io);
671 char *name;
672
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000673 if (rsnd_ssi_use_busif(io))
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000674 name = is_play ? "rxu" : "txu";
675 else
676 name = is_play ? "rx" : "tx";
677
678 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
679 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700680}
681
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700682static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700683 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000684 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800685 .probe = rsnd_ssi_dma_probe,
686 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700687 .init = rsnd_ssi_init,
688 .quit = rsnd_ssi_quit,
Kuninori Morimoto497deba2015-10-26 08:43:01 +0000689 .start = rsnd_ssi_start,
690 .stop = rsnd_ssi_stop,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000691 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto919567d2015-04-10 08:50:30 +0000692 .hw_params = rsnd_ssi_hw_params,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700693};
694
Kuninori Morimoto05795412014-11-27 08:05:01 +0000695int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
696{
697 return mod->ops == &rsnd_ssi_dma_ops;
698}
699
700
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700701/*
702 * Non SSI
703 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700704static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700705 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700706};
707
708/*
709 * ssi mod function
710 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700711struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
712{
Takashi Iwai8b147192013-11-05 18:40:05 +0100713 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
714 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700715
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000716 return rsnd_mod_get(rsnd_ssi_get(priv, id));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700717}
718
Kuninori Morimotob415b4d2015-10-22 03:15:46 +0000719int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800720{
721 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
722
723 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
724}
725
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000726int rsnd_ssi_probe(struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700727{
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000728 struct device_node *node;
729 struct device_node *np;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700730 struct device *dev = rsnd_priv_to_dev(priv);
731 struct rsnd_mod_ops *ops;
732 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700733 struct rsnd_ssi *ssi;
734 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000735 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700736
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000737 node = rsnd_ssi_of_node(priv);
738 if (!node)
739 return -EINVAL;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700740
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000741 nr = of_get_child_count(node);
742 if (!nr) {
743 ret = -EINVAL;
744 goto rsnd_ssi_probe_done;
745 }
746
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800747 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000748 if (!ssi) {
749 ret = -ENOMEM;
750 goto rsnd_ssi_probe_done;
751 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700752
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800753 priv->ssi = ssi;
754 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700755
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000756 i = 0;
757 for_each_child_of_node(node, np) {
758 ssi = rsnd_ssi_get(priv, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700759
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700760 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
761 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700762
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800763 clk = devm_clk_get(dev, name);
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000764 if (IS_ERR(clk)) {
765 ret = PTR_ERR(clk);
766 goto rsnd_ssi_probe_done;
767 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700768
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000769 if (of_get_property(np, "shared-pin", NULL))
770 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
771
772 if (of_get_property(np, "no-busif", NULL))
773 ssi->flags |= RSND_SSI_NO_BUSIF;
774
775 ssi->irq = irq_of_parse_and_map(np, 0);
776 if (!ssi->irq) {
777 ret = -EINVAL;
778 goto rsnd_ssi_probe_done;
779 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700780
781 ops = &rsnd_ssi_non_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000782 if (of_get_property(np, "pio-transfer", NULL))
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800783 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000784 else
785 ops = &rsnd_ssi_dma_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700786
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000787 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
788 RSND_MOD_SSI, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000789 if (ret)
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000790 goto rsnd_ssi_probe_done;
791
792 i++;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700793 }
794
Kuninori Morimoto02534f22015-11-10 05:11:35 +0000795 ret = 0;
796
797rsnd_ssi_probe_done:
798 of_node_put(node);
799
800 return ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700801}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000802
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000803void rsnd_ssi_remove(struct rsnd_priv *priv)
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000804{
805 struct rsnd_ssi *ssi;
806 int i;
807
808 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000809 rsnd_mod_quit(rsnd_mod_get(ssi));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000810 }
811}