blob: 79dc7a3c78e6de44756639a94897d2a3c53f60d1 [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 */
42#define DEL (1 << 8) /* Serial Data Delay */
43#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
44#define TRMD (1 << 1) /* Transmit/Receive Mode Select */
45#define EN (1 << 0) /* SSI Module Enable */
46
47/*
48 * SSISR
49 */
50#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
51#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
52#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
53#define DIRQ (1 << 24) /* Data Interrupt Status Flag */
54
Kuninori Morimoto849fc822013-07-28 18:59:02 -070055/*
56 * SSIWSR
57 */
58#define CONT (1 << 8) /* WS Continue Function */
59
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070060#define SSI_NAME "ssi"
61
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070062struct rsnd_ssi {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070063 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
64 struct rsnd_ssi *parent;
65 struct rsnd_mod mod;
66
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070067 u32 cr_own;
68 u32 cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070069 int err;
70 unsigned int usrcnt;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070071};
72
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070073#define for_each_rsnd_ssi(pos, priv, i) \
74 for (i = 0; \
75 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080076 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070077 i++)
78
Kuninori Morimotodd27d802014-01-23 18:39:40 -080079#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070080#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto849fc822013-07-28 18:59:02 -070081#define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
Kuninori Morimoto6cfad782014-11-27 08:08:10 +000082#define rsnd_ssi_pio_available(ssi) ((ssi)->info->irq > 0)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070083#define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070084#define rsnd_ssi_mode_flags(p) ((p)->info->flags)
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -070085#define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
Kuninori Morimoto04e627b2015-02-20 10:30:02 +000086#define rsnd_ssi_of_node(priv) \
87 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,ssi")
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070088
Kuninori Morimotoe879a9d2015-02-20 10:29:41 +000089int rsnd_ssi_use_busif(struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -070090{
91 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
92 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
93 int use_busif = 0;
94
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +000095 if (!rsnd_ssi_is_dma_mode(mod))
96 return 0;
97
Kuninori Morimotod9288d02014-06-22 17:56:23 -070098 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
99 use_busif = 1;
100 if (rsnd_io_to_mod_src(io))
101 use_busif = 1;
102
103 return use_busif;
104}
105
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700106static void rsnd_ssi_status_check(struct rsnd_mod *mod,
107 u32 bit)
108{
109 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
110 struct device *dev = rsnd_priv_to_dev(priv);
111 u32 status;
112 int i;
113
114 for (i = 0; i < 1024; i++) {
115 status = rsnd_mod_read(mod, SSISR);
116 if (status & bit)
117 return;
118
119 udelay(50);
120 }
121
122 dev_warn(dev, "status check failed\n");
123}
124
125static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800126 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700127{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000128 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800129 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700130 struct device *dev = rsnd_priv_to_dev(priv);
131 int i, j, ret;
132 int adg_clk_div_table[] = {
133 1, 6, /* see adg.c */
134 };
135 int ssi_clk_mul_table[] = {
136 1, 2, 4, 8, 16, 6, 12,
137 };
138 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800139 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700140
141 /*
142 * Find best clock, and try to start ADG
143 */
144 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
145 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
146
147 /*
148 * this driver is assuming that
149 * system word is 64fs (= 2 x 32bit)
Kuninori Morimoto729aca52013-12-19 19:26:44 -0800150 * see rsnd_ssi_init()
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700151 */
152 main_rate = rate / adg_clk_div_table[i]
153 * 32 * 2 * ssi_clk_mul_table[j];
154
155 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
156 if (0 == ret) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700157 ssi->cr_clk = FORCE | SWL_32 |
158 SCKD | SWSD | CKDV(j);
159
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800160 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
161 rsnd_mod_name(&ssi->mod),
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700162 rsnd_mod_id(&ssi->mod), rate);
163
164 return 0;
165 }
166 }
167 }
168
169 dev_err(dev, "unsupported clock rate\n");
170 return -EIO;
171}
172
173static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
174{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700175 ssi->cr_clk = 0;
176 rsnd_adg_ssi_clk_stop(&ssi->mod);
177}
178
179static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700180 struct rsnd_dai_stream *io)
181{
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000182 struct rsnd_priv *priv = rsnd_io_to_priv(io);
Kuninori Morimotof708d942015-01-15 08:07:19 +0000183 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700184 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto05795412014-11-27 08:05:01 +0000185 u32 cr_mode;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700186 u32 cr;
187
188 if (0 == ssi->usrcnt) {
Kuninori Morimoto85642952015-01-15 08:03:22 +0000189 rsnd_mod_hw_start(&ssi->mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700190
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000191 if (rsnd_rdai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700192 if (rsnd_ssi_clk_from_parent(ssi))
Kuninori Morimotof708d942015-01-15 08:07:19 +0000193 rsnd_ssi_hw_start(ssi->parent, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700194 else
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800195 rsnd_ssi_master_clk_start(ssi, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700196 }
197 }
198
Kuninori Morimoto05795412014-11-27 08:05:01 +0000199 cr_mode = rsnd_ssi_is_dma_mode(&ssi->mod) ?
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000200 DMEN : /* DMA : enable DMA */
201 DIEN; /* PIO : enable Data interrupt */
Kuninori Morimoto05795412014-11-27 08:05:01 +0000202
203
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700204 cr = ssi->cr_own |
205 ssi->cr_clk |
Kuninori Morimoto05795412014-11-27 08:05:01 +0000206 cr_mode |
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000207 UIEN | OIEN | EN;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700208
209 rsnd_mod_write(&ssi->mod, SSICR, cr);
210
Kuninori Morimotod1b64052014-11-27 08:05:34 +0000211 /* enable WS continue */
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000212 if (rsnd_rdai_is_clk_master(rdai))
Kuninori Morimotod1b64052014-11-27 08:05:34 +0000213 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
214
Kuninori Morimoto3ba84f42014-11-27 08:05:44 +0000215 /* clear error status */
216 rsnd_mod_write(&ssi->mod, SSISR, 0);
217
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700218 ssi->usrcnt++;
219
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800220 dev_dbg(dev, "%s[%d] hw started\n",
221 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700222}
223
Kuninori Morimotof708d942015-01-15 08:07:19 +0000224static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700225{
226 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
Kuninori Morimotof708d942015-01-15 08:07:19 +0000227 struct rsnd_dai_stream *io = rsnd_mod_to_io(&ssi->mod);
228 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700229 struct device *dev = rsnd_priv_to_dev(priv);
230 u32 cr;
231
232 if (0 == ssi->usrcnt) /* stop might be called without start */
233 return;
234
235 ssi->usrcnt--;
236
237 if (0 == ssi->usrcnt) {
238 /*
239 * disable all IRQ,
240 * and, wait all data was sent
241 */
242 cr = ssi->cr_own |
243 ssi->cr_clk;
244
245 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
246 rsnd_ssi_status_check(&ssi->mod, DIRQ);
247
248 /*
249 * disable SSI,
250 * and, wait idle state
251 */
252 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
253 rsnd_ssi_status_check(&ssi->mod, IIRQ);
254
Kuninori Morimoto3ed64482015-01-15 08:04:51 +0000255 if (rsnd_rdai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700256 if (rsnd_ssi_clk_from_parent(ssi))
Kuninori Morimotof708d942015-01-15 08:07:19 +0000257 rsnd_ssi_hw_stop(ssi->parent);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700258 else
259 rsnd_ssi_master_clk_stop(ssi);
260 }
261
Kuninori Morimoto85642952015-01-15 08:03:22 +0000262 rsnd_mod_hw_stop(&ssi->mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700263 }
264
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800265 dev_dbg(dev, "%s[%d] hw stopped\n",
266 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700267}
268
269/*
270 * SSI mod common functions
271 */
272static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000273 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700274{
275 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700276 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000277 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700278 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
279 u32 cr;
280
281 cr = FORCE;
282
283 /*
284 * always use 32bit system word for easy clock calculation.
285 * see also rsnd_ssi_master_clk_enable()
286 */
287 cr |= SWL_32;
288
289 /*
290 * init clock settings for SSICR
291 */
292 switch (runtime->sample_bits) {
293 case 16:
294 cr |= DWL_16;
295 break;
296 case 32:
297 cr |= DWL_24;
298 break;
299 default:
300 return -EIO;
301 }
302
303 if (rdai->bit_clk_inv)
304 cr |= SCKP;
305 if (rdai->frm_clk_inv)
306 cr |= SWSP;
307 if (rdai->data_alignment)
308 cr |= SDTA;
309 if (rdai->sys_delay)
310 cr |= DEL;
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000311 if (rsnd_io_is_play(io))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700312 cr |= TRMD;
313
314 /*
315 * set ssi parameter
316 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700317 ssi->cr_own = cr;
318 ssi->err = -1; /* ignore 1st error */
319
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700320 return 0;
321}
322
323static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000324 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700325{
326 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700327 struct device *dev = rsnd_priv_to_dev(priv);
328
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700329 if (ssi->err > 0)
Kuninori Morimoto337b0b42015-01-15 08:08:57 +0000330 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
331 rsnd_mod_name(mod), rsnd_mod_id(mod), ssi->err);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700332
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700333 ssi->cr_own = 0;
334 ssi->err = 0;
335
336 return 0;
337}
338
339static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
340{
341 /* under/over flow error */
342 if (status & (UIRQ | OIRQ)) {
343 ssi->err++;
344
345 /* clear error status */
346 rsnd_mod_write(&ssi->mod, SSISR, 0);
347 }
348}
349
Kuninori Morimoto49229852014-11-27 08:07:17 +0000350static int rsnd_ssi_start(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000351 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700352{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700353 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700354 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700355
Kuninori Morimotof708d942015-01-15 08:07:19 +0000356 rsnd_src_ssiu_start(mod, rsnd_ssi_use_busif(mod));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700357
Kuninori Morimotof708d942015-01-15 08:07:19 +0000358 rsnd_ssi_hw_start(ssi, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700359
Kuninori Morimotof708d942015-01-15 08:07:19 +0000360 rsnd_src_ssi_irq_enable(mod);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000361
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700362 return 0;
363}
364
Kuninori Morimoto49229852014-11-27 08:07:17 +0000365static int rsnd_ssi_stop(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000366 struct rsnd_priv *priv)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700367{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700368 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
369
Kuninori Morimotof708d942015-01-15 08:07:19 +0000370 rsnd_src_ssi_irq_disable(mod);
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000371
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000372 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
373
Kuninori Morimotof708d942015-01-15 08:07:19 +0000374 rsnd_ssi_hw_stop(ssi);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700375
Kuninori Morimotof708d942015-01-15 08:07:19 +0000376 rsnd_src_ssiu_stop(mod);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700377
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700378 return 0;
379}
380
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000381static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000382{
383 struct rsnd_ssi *ssi = data;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000384 struct rsnd_mod *mod = &ssi->mod;
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000385 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000386 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000387 int is_dma = rsnd_ssi_is_dma_mode(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000388 u32 status = rsnd_mod_read(mod, SSISR);
389
390 if (!io)
391 return IRQ_NONE;
392
393 /* PIO only */
Kuninori Morimoto765ae7c2015-01-15 08:09:13 +0000394 if (!is_dma && (status & DIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000395 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
396 u32 *buf = (u32 *)(runtime->dma_area +
397 rsnd_dai_pointer_offset(io, 0));
398
399 /*
400 * 8/16/32 data can be assesse to TDR/RDR register
401 * directly as 32bit data
402 * see rsnd_ssi_init()
403 */
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000404 if (rsnd_io_is_play(io))
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000405 rsnd_mod_write(mod, SSITDR, *buf);
406 else
407 *buf = rsnd_mod_read(mod, SSIRDR);
408
409 rsnd_dai_pointer_update(io, sizeof(*buf));
410 }
411
412 /* PIO / DMA */
413 if (status & (UIRQ | OIRQ)) {
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000414 struct device *dev = rsnd_priv_to_dev(priv);
415
416 /*
417 * restart SSI
418 */
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000419 dev_dbg(dev, "%s[%d] restart\n",
420 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto044930b2015-03-19 04:13:47 +0000421
422 rsnd_ssi_stop(mod, priv);
423 if (ssi->err < 1024)
424 rsnd_ssi_start(mod, priv);
425 else
426 dev_warn(dev, "no more SSI restart\n");
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000427 }
428
429 rsnd_ssi_record_error(ssi, status);
430
431 return IRQ_HANDLED;
432}
433
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000434/*
435 * SSI PIO
436 */
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000437static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000438 struct rsnd_priv *priv)
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000439{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000440 struct device *dev = rsnd_priv_to_dev(priv);
441 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000442 int ret;
443
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000444 ret = devm_request_irq(dev, ssi->info->irq,
445 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000446 IRQF_SHARED,
447 dev_name(dev), ssi);
448 if (ret)
449 dev_err(dev, "%s[%d] (PIO) request interrupt failed\n",
450 rsnd_mod_name(mod), rsnd_mod_id(mod));
451 else
452 dev_dbg(dev, "%s[%d] (PIO) is probed\n",
453 rsnd_mod_name(mod), rsnd_mod_id(mod));
454
455 return ret;
456}
457
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700458static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700459 .name = SSI_NAME,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800460 .probe = rsnd_ssi_pio_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700461 .init = rsnd_ssi_init,
462 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000463 .start = rsnd_ssi_start,
464 .stop = rsnd_ssi_stop,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700465};
466
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800467static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000468 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800469{
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800470 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800471 struct device *dev = rsnd_priv_to_dev(priv);
472 int dma_id = ssi->info->dma_id;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800473 int ret;
474
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000475 ret = devm_request_irq(dev, ssi->info->irq,
476 rsnd_ssi_interrupt,
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000477 IRQF_SHARED,
478 dev_name(dev), ssi);
479 if (ret)
480 goto rsnd_ssi_dma_probe_fail;
481
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800482 ret = rsnd_dma_init(
483 priv, rsnd_mod_to_dma(mod),
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800484 dma_id);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000485 if (ret)
486 goto rsnd_ssi_dma_probe_fail;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800487
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000488 dev_dbg(dev, "%s[%d] (DMA) is probed\n",
489 rsnd_mod_name(mod), rsnd_mod_id(mod));
490
491 return ret;
492
493rsnd_ssi_dma_probe_fail:
494 dev_err(dev, "%s[%d] (DMA) is failed\n",
495 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700496
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800497 return ret;
498}
499
500static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000501 struct rsnd_priv *priv)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800502{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000503 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
504 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000505 int irq = ssi->info->irq;
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000506
Kuninori Morimoto8a2ff422015-02-20 10:24:06 +0000507 rsnd_dma_quit(rsnd_mod_to_dma(mod));
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000508
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000509 /* PIO will request IRQ again */
510 devm_free_irq(dev, irq, ssi);
511
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000512 return 0;
513}
514
515static int rsnd_ssi_fallback(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000516 struct rsnd_priv *priv)
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000517{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800518 struct device *dev = rsnd_priv_to_dev(priv);
519
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800520 /*
521 * fallback to PIO
522 *
523 * SSI .probe might be called again.
524 * see
525 * rsnd_rdai_continuance_probe()
526 */
527 mod->ops = &rsnd_ssi_pio_ops;
528
529 dev_info(dev, "%s[%d] fallback to PIO mode\n",
530 rsnd_mod_name(mod), rsnd_mod_id(mod));
531
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800532 return 0;
533}
534
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700535static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000536 struct rsnd_priv *priv)
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700537{
Kuninori Morimoto49229852014-11-27 08:07:17 +0000538 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700539
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000540 rsnd_dma_start(dma);
541
Kuninori Morimoto1ceeab22015-01-15 08:09:26 +0000542 rsnd_ssi_start(mod, priv);
543
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700544 return 0;
545}
546
547static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000548 struct rsnd_priv *priv)
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700549{
Kuninori Morimoto49229852014-11-27 08:07:17 +0000550 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700551
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000552 rsnd_ssi_stop(mod, priv);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700553
Kuninori Morimoto1ceeab22015-01-15 08:09:26 +0000554 rsnd_dma_stop(dma);
555
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700556 return 0;
557}
558
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000559static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_mod *mod)
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700560{
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000561 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
562 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
563 int is_play = rsnd_io_is_play(io);
564 char *name;
565
566 if (rsnd_ssi_use_busif(mod))
567 name = is_play ? "rxu" : "txu";
568 else
569 name = is_play ? "rx" : "tx";
570
571 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
572 mod, name);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700573}
574
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700575static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700576 .name = SSI_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000577 .dma_req = rsnd_ssi_dma_req,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800578 .probe = rsnd_ssi_dma_probe,
579 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700580 .init = rsnd_ssi_init,
581 .quit = rsnd_ssi_quit,
582 .start = rsnd_ssi_dma_start,
583 .stop = rsnd_ssi_dma_stop,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000584 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700585};
586
Kuninori Morimoto05795412014-11-27 08:05:01 +0000587int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
588{
589 return mod->ops == &rsnd_ssi_dma_ops;
590}
591
592
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700593/*
594 * Non SSI
595 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700596static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700597 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700598};
599
600/*
601 * ssi mod function
602 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700603struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
604{
Takashi Iwai8b147192013-11-05 18:40:05 +0100605 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
606 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700607
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800608 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700609}
610
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800611int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
612{
613 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
614
615 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
616}
617
618static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
619{
620 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
621 return;
622
623 switch (rsnd_mod_id(&ssi->mod)) {
624 case 1:
625 case 2:
626 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
627 break;
628 case 4:
629 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
630 break;
631 case 8:
632 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
633 break;
634 }
635}
636
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700637
638static void rsnd_of_parse_ssi(struct platform_device *pdev,
639 const struct rsnd_of_data *of_data,
640 struct rsnd_priv *priv)
641{
642 struct device_node *node;
643 struct device_node *np;
644 struct rsnd_ssi_platform_info *ssi_info;
645 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
646 struct device *dev = &pdev->dev;
647 int nr, i;
648
649 if (!of_data)
650 return;
651
Kuninori Morimoto04e627b2015-02-20 10:30:02 +0000652 node = rsnd_ssi_of_node(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700653 if (!node)
654 return;
655
656 nr = of_get_child_count(node);
657 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700658 goto rsnd_of_parse_ssi_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700659
660 ssi_info = devm_kzalloc(dev,
661 sizeof(struct rsnd_ssi_platform_info) * nr,
662 GFP_KERNEL);
663 if (!ssi_info) {
664 dev_err(dev, "ssi info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700665 goto rsnd_of_parse_ssi_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700666 }
667
668 info->ssi_info = ssi_info;
669 info->ssi_info_nr = nr;
670
671 i = -1;
672 for_each_child_of_node(node, np) {
673 i++;
674
675 ssi_info = info->ssi_info + i;
676
677 /*
678 * pin settings
679 */
680 if (of_get_property(np, "shared-pin", NULL))
681 ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
682
683 /*
684 * irq
685 */
Kuninori Morimoto6cfad782014-11-27 08:08:10 +0000686 ssi_info->irq = irq_of_parse_and_map(np, 0);
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700687
688 /*
689 * DMA
690 */
691 ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
692 0 : 1;
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700693
694 if (of_get_property(np, "no-busif", NULL))
695 ssi_info->flags |= RSND_SSI_NO_BUSIF;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700696 }
Kuninori Morimotof451e482014-05-22 23:24:59 -0700697
698rsnd_of_parse_ssi_end:
699 of_node_put(node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700700}
701
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700702int rsnd_ssi_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700703 const struct rsnd_of_data *of_data,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700704 struct rsnd_priv *priv)
705{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800706 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700707 struct rsnd_ssi_platform_info *pinfo;
708 struct device *dev = rsnd_priv_to_dev(priv);
709 struct rsnd_mod_ops *ops;
710 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700711 struct rsnd_ssi *ssi;
712 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000713 int i, nr, ret;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700714
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700715 rsnd_of_parse_ssi(pdev, of_data, priv);
716
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700717 /*
718 * init SSI
719 */
720 nr = info->ssi_info_nr;
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800721 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
722 if (!ssi) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700723 dev_err(dev, "SSI allocate failed\n");
724 return -ENOMEM;
725 }
726
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800727 priv->ssi = ssi;
728 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700729
730 for_each_rsnd_ssi(ssi, priv, i) {
731 pinfo = &info->ssi_info[i];
732
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700733 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
734 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700735
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800736 clk = devm_clk_get(dev, name);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700737 if (IS_ERR(clk))
738 return PTR_ERR(clk);
739
740 ssi->info = pinfo;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700741
742 ops = &rsnd_ssi_non_ops;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800743 if (pinfo->dma_id > 0)
744 ops = &rsnd_ssi_dma_ops;
745 else if (rsnd_ssi_pio_available(ssi))
746 ops = &rsnd_ssi_pio_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700747
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000748 ret = rsnd_mod_init(&ssi->mod, ops, clk, RSND_MOD_SSI, i);
749 if (ret)
750 return ret;
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800751
752 rsnd_ssi_parent_clk_setup(priv, ssi);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700753 }
754
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700755 return 0;
756}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000757
758void rsnd_ssi_remove(struct platform_device *pdev,
759 struct rsnd_priv *priv)
760{
761 struct rsnd_ssi *ssi;
762 int i;
763
764 for_each_rsnd_ssi(ssi, priv, i) {
765 rsnd_mod_quit(&ssi->mod);
766 }
767}