blob: 6f7080b017fa534bd2c3b2e1cf1108770aaa2fb8 [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 {
63 struct clk *clk;
64 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
65 struct rsnd_ssi *parent;
66 struct rsnd_mod mod;
67
68 struct rsnd_dai *rdai;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070069 u32 cr_own;
70 u32 cr_clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070071 int err;
72 unsigned int usrcnt;
73 unsigned int rate;
74};
75
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070076#define for_each_rsnd_ssi(pos, priv, i) \
77 for (i = 0; \
78 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080079 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070080 i++)
81
Kuninori Morimotodd27d802014-01-23 18:39:40 -080082#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070083#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto849fc822013-07-28 18:59:02 -070084#define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
85#define rsnd_ssi_pio_available(ssi) ((ssi)->info->pio_irq > 0)
86#define rsnd_ssi_dma_available(ssi) \
87 rsnd_dma_available(rsnd_mod_to_dma(&(ssi)->mod))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070088#define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070089#define rsnd_ssi_mode_flags(p) ((p)->info->flags)
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -070090#define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070091
Kuninori Morimotod9288d02014-06-22 17:56:23 -070092static int rsnd_ssi_use_busif(struct rsnd_mod *mod)
93{
94 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
95 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
96 int use_busif = 0;
97
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +000098 if (!rsnd_ssi_is_dma_mode(mod))
99 return 0;
100
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700101 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
102 use_busif = 1;
103 if (rsnd_io_to_mod_src(io))
104 use_busif = 1;
105
106 return use_busif;
107}
108
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700109static void rsnd_ssi_status_check(struct rsnd_mod *mod,
110 u32 bit)
111{
112 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
113 struct device *dev = rsnd_priv_to_dev(priv);
114 u32 status;
115 int i;
116
117 for (i = 0; i < 1024; i++) {
118 status = rsnd_mod_read(mod, SSISR);
119 if (status & bit)
120 return;
121
122 udelay(50);
123 }
124
125 dev_warn(dev, "status check failed\n");
126}
127
128static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800129 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700130{
131 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800132 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700133 struct device *dev = rsnd_priv_to_dev(priv);
134 int i, j, ret;
135 int adg_clk_div_table[] = {
136 1, 6, /* see adg.c */
137 };
138 int ssi_clk_mul_table[] = {
139 1, 2, 4, 8, 16, 6, 12,
140 };
141 unsigned int main_rate;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800142 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700143
144 /*
145 * Find best clock, and try to start ADG
146 */
147 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
148 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
149
150 /*
151 * this driver is assuming that
152 * system word is 64fs (= 2 x 32bit)
Kuninori Morimoto729aca52013-12-19 19:26:44 -0800153 * see rsnd_ssi_init()
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700154 */
155 main_rate = rate / adg_clk_div_table[i]
156 * 32 * 2 * ssi_clk_mul_table[j];
157
158 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
159 if (0 == ret) {
160 ssi->rate = rate;
161 ssi->cr_clk = FORCE | SWL_32 |
162 SCKD | SWSD | CKDV(j);
163
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800164 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
165 rsnd_mod_name(&ssi->mod),
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700166 rsnd_mod_id(&ssi->mod), rate);
167
168 return 0;
169 }
170 }
171 }
172
173 dev_err(dev, "unsupported clock rate\n");
174 return -EIO;
175}
176
177static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
178{
179 ssi->rate = 0;
180 ssi->cr_clk = 0;
181 rsnd_adg_ssi_clk_stop(&ssi->mod);
182}
183
184static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
185 struct rsnd_dai *rdai,
186 struct rsnd_dai_stream *io)
187{
188 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
189 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto05795412014-11-27 08:05:01 +0000190 u32 cr_mode;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700191 u32 cr;
192
193 if (0 == ssi->usrcnt) {
Ben Dooks79861bb2014-04-10 18:16:33 -0700194 clk_prepare_enable(ssi->clk);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700195
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800196 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700197 if (rsnd_ssi_clk_from_parent(ssi))
198 rsnd_ssi_hw_start(ssi->parent, rdai, io);
199 else
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800200 rsnd_ssi_master_clk_start(ssi, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700201 }
202 }
203
Kuninori Morimoto05795412014-11-27 08:05:01 +0000204 cr_mode = rsnd_ssi_is_dma_mode(&ssi->mod) ?
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000205 DMEN : /* DMA : enable DMA */
206 DIEN; /* PIO : enable Data interrupt */
Kuninori Morimoto05795412014-11-27 08:05:01 +0000207
208
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700209 cr = ssi->cr_own |
210 ssi->cr_clk |
Kuninori Morimoto05795412014-11-27 08:05:01 +0000211 cr_mode |
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000212 UIEN | OIEN | EN;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700213
214 rsnd_mod_write(&ssi->mod, SSICR, cr);
215
Kuninori Morimotod1b64052014-11-27 08:05:34 +0000216 /* enable WS continue */
217 if (rsnd_dai_is_clk_master(rdai))
218 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
219
Kuninori Morimoto3ba84f42014-11-27 08:05:44 +0000220 /* clear error status */
221 rsnd_mod_write(&ssi->mod, SSISR, 0);
222
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700223 ssi->usrcnt++;
224
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800225 dev_dbg(dev, "%s[%d] hw started\n",
226 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700227}
228
229static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
230 struct rsnd_dai *rdai)
231{
232 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
233 struct device *dev = rsnd_priv_to_dev(priv);
234 u32 cr;
235
236 if (0 == ssi->usrcnt) /* stop might be called without start */
237 return;
238
239 ssi->usrcnt--;
240
241 if (0 == ssi->usrcnt) {
242 /*
243 * disable all IRQ,
244 * and, wait all data was sent
245 */
246 cr = ssi->cr_own |
247 ssi->cr_clk;
248
249 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
250 rsnd_ssi_status_check(&ssi->mod, DIRQ);
251
252 /*
253 * disable SSI,
254 * and, wait idle state
255 */
256 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
257 rsnd_ssi_status_check(&ssi->mod, IIRQ);
258
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800259 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700260 if (rsnd_ssi_clk_from_parent(ssi))
261 rsnd_ssi_hw_stop(ssi->parent, rdai);
262 else
263 rsnd_ssi_master_clk_stop(ssi);
264 }
265
Ben Dooks79861bb2014-04-10 18:16:33 -0700266 clk_disable_unprepare(ssi->clk);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700267 }
268
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800269 dev_dbg(dev, "%s[%d] hw stopped\n",
270 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700271}
272
273/*
274 * SSI mod common functions
275 */
276static int rsnd_ssi_init(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700277 struct rsnd_dai *rdai)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700278{
279 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700280 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700281 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
282 u32 cr;
283
284 cr = FORCE;
285
286 /*
287 * always use 32bit system word for easy clock calculation.
288 * see also rsnd_ssi_master_clk_enable()
289 */
290 cr |= SWL_32;
291
292 /*
293 * init clock settings for SSICR
294 */
295 switch (runtime->sample_bits) {
296 case 16:
297 cr |= DWL_16;
298 break;
299 case 32:
300 cr |= DWL_24;
301 break;
302 default:
303 return -EIO;
304 }
305
306 if (rdai->bit_clk_inv)
307 cr |= SCKP;
308 if (rdai->frm_clk_inv)
309 cr |= SWSP;
310 if (rdai->data_alignment)
311 cr |= SDTA;
312 if (rdai->sys_delay)
313 cr |= DEL;
314 if (rsnd_dai_is_play(rdai, io))
315 cr |= TRMD;
316
317 /*
318 * set ssi parameter
319 */
320 ssi->rdai = rdai;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700321 ssi->cr_own = cr;
322 ssi->err = -1; /* ignore 1st error */
323
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700324 return 0;
325}
326
327static int rsnd_ssi_quit(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700328 struct rsnd_dai *rdai)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700329{
330 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
331 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
332 struct device *dev = rsnd_priv_to_dev(priv);
333
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700334 if (ssi->err > 0)
335 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
336
337 ssi->rdai = NULL;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700338 ssi->cr_own = 0;
339 ssi->err = 0;
340
341 return 0;
342}
343
344static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
345{
346 /* under/over flow error */
347 if (status & (UIRQ | OIRQ)) {
348 ssi->err++;
349
350 /* clear error status */
351 rsnd_mod_write(&ssi->mod, SSISR, 0);
352 }
353}
354
355/*
356 * SSI PIO
357 */
Kuninori Morimoto49229852014-11-27 08:07:17 +0000358static int rsnd_ssi_start(struct rsnd_mod *mod,
359 struct rsnd_dai *rdai)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700360{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700361 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700362 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700363
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000364 rsnd_src_ssiu_start(mod, rdai, rsnd_ssi_use_busif(mod));
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700365
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700366 rsnd_ssi_hw_start(ssi, rdai, io);
367
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000368 rsnd_src_ssi_irq_enable(mod, rdai);
369
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700370 return 0;
371}
372
Kuninori Morimoto49229852014-11-27 08:07:17 +0000373static int rsnd_ssi_stop(struct rsnd_mod *mod,
374 struct rsnd_dai *rdai)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700375{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700376 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
377
Kuninori Morimotoc17dba82014-11-27 08:05:09 +0000378 rsnd_src_ssi_irq_disable(mod, rdai);
379
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000380 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
381
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700382 rsnd_ssi_hw_stop(ssi, rdai);
383
Kuninori Morimoto660cdce2014-11-27 08:03:39 +0000384 rsnd_src_ssiu_stop(mod, rdai);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700385
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700386 return 0;
387}
388
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000389static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
390{
391 struct rsnd_ssi *ssi = data;
392 struct rsnd_dai *rdai = ssi->rdai;
393 struct rsnd_mod *mod = &ssi->mod;
394 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
395 u32 status = rsnd_mod_read(mod, SSISR);
396
397 if (!io)
398 return IRQ_NONE;
399
400 /* PIO only */
401 if (status & DIRQ) {
402 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
403 u32 *buf = (u32 *)(runtime->dma_area +
404 rsnd_dai_pointer_offset(io, 0));
405
406 /*
407 * 8/16/32 data can be assesse to TDR/RDR register
408 * directly as 32bit data
409 * see rsnd_ssi_init()
410 */
411 if (rsnd_dai_is_play(rdai, io))
412 rsnd_mod_write(mod, SSITDR, *buf);
413 else
414 *buf = rsnd_mod_read(mod, SSIRDR);
415
416 rsnd_dai_pointer_update(io, sizeof(*buf));
417 }
418
419 /* PIO / DMA */
420 if (status & (UIRQ | OIRQ)) {
421 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
422 struct device *dev = rsnd_priv_to_dev(priv);
423
424 /*
425 * restart SSI
426 */
427 rsnd_ssi_stop(mod, rdai);
428 rsnd_ssi_start(mod, rdai);
429
430 dev_dbg(dev, "%s[%d] restart\n",
431 rsnd_mod_name(mod), rsnd_mod_id(mod));
432 }
433
434 rsnd_ssi_record_error(ssi, status);
435
436 return IRQ_HANDLED;
437}
438
439static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
440 struct rsnd_dai *rdai)
441{
442 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
443 struct device *dev = rsnd_priv_to_dev(priv);
444 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
445 int irq = ssi->info->pio_irq;
446 int ret;
447
448 ret = devm_request_irq(dev, irq,
449 rsnd_ssi_pio_interrupt,
450 IRQF_SHARED,
451 dev_name(dev), ssi);
452 if (ret)
453 dev_err(dev, "%s[%d] (PIO) request interrupt failed\n",
454 rsnd_mod_name(mod), rsnd_mod_id(mod));
455 else
456 dev_dbg(dev, "%s[%d] (PIO) is probed\n",
457 rsnd_mod_name(mod), rsnd_mod_id(mod));
458
459 return ret;
460}
461
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700462static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700463 .name = SSI_NAME,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800464 .probe = rsnd_ssi_pio_probe,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700465 .init = rsnd_ssi_init,
466 .quit = rsnd_ssi_quit,
Kuninori Morimoto49229852014-11-27 08:07:17 +0000467 .start = rsnd_ssi_start,
468 .stop = rsnd_ssi_stop,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700469};
470
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800471static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700472 struct rsnd_dai *rdai)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800473{
474 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
475 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800476 struct device *dev = rsnd_priv_to_dev(priv);
477 int dma_id = ssi->info->dma_id;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800478 int ret;
479
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000480 ret = devm_request_irq(dev, ssi->info->pio_irq,
481 rsnd_ssi_pio_interrupt,
482 IRQF_SHARED,
483 dev_name(dev), ssi);
484 if (ret)
485 goto rsnd_ssi_dma_probe_fail;
486
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800487 ret = rsnd_dma_init(
488 priv, rsnd_mod_to_dma(mod),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700489 rsnd_info_is_playback(priv, ssi),
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800490 dma_id);
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000491 if (ret)
492 goto rsnd_ssi_dma_probe_fail;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800493
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000494 dev_dbg(dev, "%s[%d] (DMA) is probed\n",
495 rsnd_mod_name(mod), rsnd_mod_id(mod));
496
497 return ret;
498
499rsnd_ssi_dma_probe_fail:
500 dev_err(dev, "%s[%d] (DMA) is failed\n",
501 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700502
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800503 return ret;
504}
505
506static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700507 struct rsnd_dai *rdai)
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800508{
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000509 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
510 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
511 struct device *dev = rsnd_priv_to_dev(priv);
512 int irq = ssi->info->pio_irq;
513
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000514 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
515
Kuninori Morimoto4e7d6062014-11-27 08:07:47 +0000516 /* PIO will request IRQ again */
517 devm_free_irq(dev, irq, ssi);
518
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000519 return 0;
520}
521
522static int rsnd_ssi_fallback(struct rsnd_mod *mod,
523 struct rsnd_dai *rdai)
524{
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800525 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
526 struct device *dev = rsnd_priv_to_dev(priv);
527
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800528 /*
529 * fallback to PIO
530 *
531 * SSI .probe might be called again.
532 * see
533 * rsnd_rdai_continuance_probe()
534 */
535 mod->ops = &rsnd_ssi_pio_ops;
536
537 dev_info(dev, "%s[%d] fallback to PIO mode\n",
538 rsnd_mod_name(mod), rsnd_mod_id(mod));
539
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800540 return 0;
541}
542
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700543static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700544 struct rsnd_dai *rdai)
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700545{
Kuninori Morimoto49229852014-11-27 08:07:17 +0000546 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700547
Kuninori Morimoto49229852014-11-27 08:07:17 +0000548 rsnd_ssi_start(mod, rdai);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700549
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000550 rsnd_dma_start(dma);
551
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700552 return 0;
553}
554
555static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700556 struct rsnd_dai *rdai)
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700557{
Kuninori Morimoto49229852014-11-27 08:07:17 +0000558 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700559
Kuninori Morimoto7b466fc2014-11-27 08:05:54 +0000560 rsnd_dma_stop(dma);
561
Kuninori Morimoto49229852014-11-27 08:07:17 +0000562 rsnd_ssi_stop(mod, rdai);
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700563
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700564 return 0;
565}
566
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700567static char *rsnd_ssi_dma_name(struct rsnd_mod *mod)
568{
569 return rsnd_ssi_use_busif(mod) ? "ssiu" : SSI_NAME;
570}
571
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700572static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700573 .name = SSI_NAME,
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700574 .dma_name = rsnd_ssi_dma_name,
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800575 .probe = rsnd_ssi_dma_probe,
576 .remove = rsnd_ssi_dma_remove,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700577 .init = rsnd_ssi_init,
578 .quit = rsnd_ssi_quit,
579 .start = rsnd_ssi_dma_start,
580 .stop = rsnd_ssi_dma_stop,
Kuninori Morimoto97463e12014-11-27 08:02:43 +0000581 .fallback = rsnd_ssi_fallback,
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700582};
583
Kuninori Morimoto05795412014-11-27 08:05:01 +0000584int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
585{
586 return mod->ops == &rsnd_ssi_dma_ops;
587}
588
589
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700590/*
591 * Non SSI
592 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700593static struct rsnd_mod_ops rsnd_ssi_non_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700594 .name = SSI_NAME,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700595};
596
597/*
598 * ssi mod function
599 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700600struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
601{
Takashi Iwai8b147192013-11-05 18:40:05 +0100602 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
603 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700604
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800605 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700606}
607
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800608int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
609{
610 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
611
612 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
613}
614
615static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
616{
617 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
618 return;
619
620 switch (rsnd_mod_id(&ssi->mod)) {
621 case 1:
622 case 2:
623 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
624 break;
625 case 4:
626 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
627 break;
628 case 8:
629 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
630 break;
631 }
632}
633
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700634
635static void rsnd_of_parse_ssi(struct platform_device *pdev,
636 const struct rsnd_of_data *of_data,
637 struct rsnd_priv *priv)
638{
639 struct device_node *node;
640 struct device_node *np;
641 struct rsnd_ssi_platform_info *ssi_info;
642 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
643 struct device *dev = &pdev->dev;
644 int nr, i;
645
646 if (!of_data)
647 return;
648
649 node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
650 if (!node)
651 return;
652
653 nr = of_get_child_count(node);
654 if (!nr)
Kuninori Morimotof451e482014-05-22 23:24:59 -0700655 goto rsnd_of_parse_ssi_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700656
657 ssi_info = devm_kzalloc(dev,
658 sizeof(struct rsnd_ssi_platform_info) * nr,
659 GFP_KERNEL);
660 if (!ssi_info) {
661 dev_err(dev, "ssi info allocation error\n");
Kuninori Morimotof451e482014-05-22 23:24:59 -0700662 goto rsnd_of_parse_ssi_end;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700663 }
664
665 info->ssi_info = ssi_info;
666 info->ssi_info_nr = nr;
667
668 i = -1;
669 for_each_child_of_node(node, np) {
670 i++;
671
672 ssi_info = info->ssi_info + i;
673
674 /*
675 * pin settings
676 */
677 if (of_get_property(np, "shared-pin", NULL))
678 ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
679
680 /*
681 * irq
682 */
683 ssi_info->pio_irq = irq_of_parse_and_map(np, 0);
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700684
685 /*
686 * DMA
687 */
688 ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
689 0 : 1;
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700690
691 if (of_get_property(np, "no-busif", NULL))
692 ssi_info->flags |= RSND_SSI_NO_BUSIF;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700693 }
Kuninori Morimotof451e482014-05-22 23:24:59 -0700694
695rsnd_of_parse_ssi_end:
696 of_node_put(node);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700697}
698
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700699int rsnd_ssi_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700700 const struct rsnd_of_data *of_data,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700701 struct rsnd_priv *priv)
702{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800703 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700704 struct rsnd_ssi_platform_info *pinfo;
705 struct device *dev = rsnd_priv_to_dev(priv);
706 struct rsnd_mod_ops *ops;
707 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700708 struct rsnd_ssi *ssi;
709 char name[RSND_SSI_NAME_SIZE];
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800710 int i, nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700711
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700712 rsnd_of_parse_ssi(pdev, of_data, priv);
713
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700714 /*
715 * init SSI
716 */
717 nr = info->ssi_info_nr;
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800718 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
719 if (!ssi) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700720 dev_err(dev, "SSI allocate failed\n");
721 return -ENOMEM;
722 }
723
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800724 priv->ssi = ssi;
725 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700726
727 for_each_rsnd_ssi(ssi, priv, i) {
728 pinfo = &info->ssi_info[i];
729
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700730 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
731 SSI_NAME, i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700732
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800733 clk = devm_clk_get(dev, name);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700734 if (IS_ERR(clk))
735 return PTR_ERR(clk);
736
737 ssi->info = pinfo;
738 ssi->clk = clk;
739
740 ops = &rsnd_ssi_non_ops;
Kuninori Morimotoff8f30e2014-03-03 20:50:49 -0800741 if (pinfo->dma_id > 0)
742 ops = &rsnd_ssi_dma_ops;
743 else if (rsnd_ssi_pio_available(ssi))
744 ops = &rsnd_ssi_pio_ops;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700745
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800746 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800747
748 rsnd_ssi_parent_clk_setup(priv, ssi);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700749 }
750
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700751 return 0;
752}