blob: 68393a9f91bfb302f5a83e431aefad501258edd0 [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 Morimotoae5c3222013-07-21 21:36:57 -070060struct rsnd_ssi {
61 struct clk *clk;
62 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
63 struct rsnd_ssi *parent;
64 struct rsnd_mod mod;
65
66 struct rsnd_dai *rdai;
67 struct rsnd_dai_stream *io;
68 u32 cr_own;
69 u32 cr_clk;
70 u32 cr_etc;
71 int err;
Kuninori Morimoto849fc822013-07-28 18:59:02 -070072 int dma_offset;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070073 unsigned int usrcnt;
74 unsigned int rate;
75};
76
77struct rsnd_ssiu {
78 u32 ssi_mode0;
79 u32 ssi_mode1;
80
81 int ssi_nr;
82 struct rsnd_ssi *ssi;
83};
84
85#define for_each_rsnd_ssi(pos, priv, i) \
86 for (i = 0; \
87 (i < rsnd_ssi_nr(priv)) && \
88 ((pos) = ((struct rsnd_ssiu *)((priv)->ssiu))->ssi + i); \
89 i++)
90
91#define rsnd_ssi_nr(priv) (((struct rsnd_ssiu *)((priv)->ssiu))->ssi_nr)
92#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto849fc822013-07-28 18:59:02 -070093#define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
94#define rsnd_ssi_pio_available(ssi) ((ssi)->info->pio_irq > 0)
95#define rsnd_ssi_dma_available(ssi) \
96 rsnd_dma_available(rsnd_mod_to_dma(&(ssi)->mod))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070097#define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070098#define rsnd_ssi_mode_flags(p) ((p)->info->flags)
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -070099#define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700100#define rsnd_ssi_to_ssiu(ssi)\
101 (((struct rsnd_ssiu *)((ssi) - rsnd_mod_id(&(ssi)->mod))) - 1)
102
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700103static void rsnd_ssi_mode_set(struct rsnd_priv *priv,
104 struct rsnd_dai *rdai,
105 struct rsnd_ssi *ssi)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700106{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700107 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotocdcfcac2013-10-17 22:50:59 -0700108 struct rsnd_mod *scu;
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700109 struct rsnd_ssiu *ssiu = rsnd_ssi_to_ssiu(ssi);
110 int id = rsnd_mod_id(&ssi->mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700111 u32 flags;
112 u32 val;
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700113
114 scu = rsnd_scu_mod_get(priv, rsnd_mod_id(&ssi->mod));
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700115
116 /*
117 * SSI_MODE0
118 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700119
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700120 /* see also BUSIF_MODE */
121 if (rsnd_scu_hpbif_is_enable(scu)) {
122 ssiu->ssi_mode0 &= ~(1 << id);
123 dev_dbg(dev, "SSI%d uses DEPENDENT mode\n", id);
124 } else {
125 ssiu->ssi_mode0 |= (1 << id);
126 dev_dbg(dev, "SSI%d uses INDEPENDENT mode\n", id);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700127 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700128
129 /*
130 * SSI_MODE1
131 */
132#define ssi_parent_set(p, sync, adg, ext) \
133 do { \
134 ssi->parent = ssiu->ssi + p; \
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800135 if (rsnd_dai_is_clk_master(rdai)) \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700136 val = adg; \
137 else \
138 val = ext; \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700139 } while (0)
140
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700141 flags = rsnd_ssi_mode_flags(ssi);
142 if (flags & RSND_SSI_CLK_PIN_SHARE) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700143
144 val = 0;
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700145 switch (id) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700146 case 1:
147 ssi_parent_set(0, (1 << 4), (0x2 << 0), (0x1 << 0));
148 break;
149 case 2:
150 ssi_parent_set(0, (1 << 4), (0x2 << 2), (0x1 << 2));
151 break;
152 case 4:
153 ssi_parent_set(3, (1 << 20), (0x2 << 16), (0x1 << 16));
154 break;
155 case 8:
156 ssi_parent_set(7, 0, 0, 0);
157 break;
158 }
159
160 ssiu->ssi_mode1 |= val;
161 }
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700162
163 rsnd_mod_write(&ssi->mod, SSI_MODE0, ssiu->ssi_mode0);
164 rsnd_mod_write(&ssi->mod, SSI_MODE1, ssiu->ssi_mode1);
165}
166
167static void rsnd_ssi_status_check(struct rsnd_mod *mod,
168 u32 bit)
169{
170 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
171 struct device *dev = rsnd_priv_to_dev(priv);
172 u32 status;
173 int i;
174
175 for (i = 0; i < 1024; i++) {
176 status = rsnd_mod_read(mod, SSISR);
177 if (status & bit)
178 return;
179
180 udelay(50);
181 }
182
183 dev_warn(dev, "status check failed\n");
184}
185
186static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800187 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700188{
189 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800190 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700191 struct device *dev = rsnd_priv_to_dev(priv);
192 int i, j, ret;
193 int adg_clk_div_table[] = {
194 1, 6, /* see adg.c */
195 };
196 int ssi_clk_mul_table[] = {
197 1, 2, 4, 8, 16, 6, 12,
198 };
199 unsigned int main_rate;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800200 unsigned int rate = rsnd_scu_get_ssi_rate(priv, &ssi->mod, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700201
202 /*
203 * Find best clock, and try to start ADG
204 */
205 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
206 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
207
208 /*
209 * this driver is assuming that
210 * system word is 64fs (= 2 x 32bit)
Kuninori Morimoto729aca52013-12-19 19:26:44 -0800211 * see rsnd_ssi_init()
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700212 */
213 main_rate = rate / adg_clk_div_table[i]
214 * 32 * 2 * ssi_clk_mul_table[j];
215
216 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
217 if (0 == ret) {
218 ssi->rate = rate;
219 ssi->cr_clk = FORCE | SWL_32 |
220 SCKD | SWSD | CKDV(j);
221
222 dev_dbg(dev, "ssi%d outputs %u Hz\n",
223 rsnd_mod_id(&ssi->mod), rate);
224
225 return 0;
226 }
227 }
228 }
229
230 dev_err(dev, "unsupported clock rate\n");
231 return -EIO;
232}
233
234static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
235{
236 ssi->rate = 0;
237 ssi->cr_clk = 0;
238 rsnd_adg_ssi_clk_stop(&ssi->mod);
239}
240
241static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
242 struct rsnd_dai *rdai,
243 struct rsnd_dai_stream *io)
244{
245 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
246 struct device *dev = rsnd_priv_to_dev(priv);
247 u32 cr;
248
249 if (0 == ssi->usrcnt) {
250 clk_enable(ssi->clk);
251
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800252 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700253 if (rsnd_ssi_clk_from_parent(ssi))
254 rsnd_ssi_hw_start(ssi->parent, rdai, io);
255 else
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800256 rsnd_ssi_master_clk_start(ssi, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700257 }
258 }
259
260 cr = ssi->cr_own |
261 ssi->cr_clk |
262 ssi->cr_etc |
263 EN;
264
265 rsnd_mod_write(&ssi->mod, SSICR, cr);
266
267 ssi->usrcnt++;
268
269 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
270}
271
272static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
273 struct rsnd_dai *rdai)
274{
275 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
276 struct device *dev = rsnd_priv_to_dev(priv);
277 u32 cr;
278
279 if (0 == ssi->usrcnt) /* stop might be called without start */
280 return;
281
282 ssi->usrcnt--;
283
284 if (0 == ssi->usrcnt) {
285 /*
286 * disable all IRQ,
287 * and, wait all data was sent
288 */
289 cr = ssi->cr_own |
290 ssi->cr_clk;
291
292 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
293 rsnd_ssi_status_check(&ssi->mod, DIRQ);
294
295 /*
296 * disable SSI,
297 * and, wait idle state
298 */
299 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
300 rsnd_ssi_status_check(&ssi->mod, IIRQ);
301
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800302 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700303 if (rsnd_ssi_clk_from_parent(ssi))
304 rsnd_ssi_hw_stop(ssi->parent, rdai);
305 else
306 rsnd_ssi_master_clk_stop(ssi);
307 }
308
309 clk_disable(ssi->clk);
310 }
311
312 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
313}
314
315/*
316 * SSI mod common functions
317 */
318static int rsnd_ssi_init(struct rsnd_mod *mod,
319 struct rsnd_dai *rdai,
320 struct rsnd_dai_stream *io)
321{
322 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
323 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700324 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
325 u32 cr;
326
327 cr = FORCE;
328
329 /*
330 * always use 32bit system word for easy clock calculation.
331 * see also rsnd_ssi_master_clk_enable()
332 */
333 cr |= SWL_32;
334
335 /*
336 * init clock settings for SSICR
337 */
338 switch (runtime->sample_bits) {
339 case 16:
340 cr |= DWL_16;
341 break;
342 case 32:
343 cr |= DWL_24;
344 break;
345 default:
346 return -EIO;
347 }
348
349 if (rdai->bit_clk_inv)
350 cr |= SCKP;
351 if (rdai->frm_clk_inv)
352 cr |= SWSP;
353 if (rdai->data_alignment)
354 cr |= SDTA;
355 if (rdai->sys_delay)
356 cr |= DEL;
357 if (rsnd_dai_is_play(rdai, io))
358 cr |= TRMD;
359
360 /*
361 * set ssi parameter
362 */
363 ssi->rdai = rdai;
364 ssi->io = io;
365 ssi->cr_own = cr;
366 ssi->err = -1; /* ignore 1st error */
367
Kuninori Morimoto92eba042013-10-17 22:51:40 -0700368 rsnd_ssi_mode_set(priv, rdai, ssi);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700369
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700370 return 0;
371}
372
373static int rsnd_ssi_quit(struct rsnd_mod *mod,
374 struct rsnd_dai *rdai,
375 struct rsnd_dai_stream *io)
376{
377 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
378 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
379 struct device *dev = rsnd_priv_to_dev(priv);
380
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700381 if (ssi->err > 0)
382 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
383
384 ssi->rdai = NULL;
385 ssi->io = NULL;
386 ssi->cr_own = 0;
387 ssi->err = 0;
388
389 return 0;
390}
391
392static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
393{
394 /* under/over flow error */
395 if (status & (UIRQ | OIRQ)) {
396 ssi->err++;
397
398 /* clear error status */
399 rsnd_mod_write(&ssi->mod, SSISR, 0);
400 }
401}
402
403/*
404 * SSI PIO
405 */
406static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
407{
408 struct rsnd_ssi *ssi = data;
409 struct rsnd_dai_stream *io = ssi->io;
410 u32 status = rsnd_mod_read(&ssi->mod, SSISR);
411 irqreturn_t ret = IRQ_NONE;
412
413 if (io && (status & DIRQ)) {
414 struct rsnd_dai *rdai = ssi->rdai;
415 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
416 u32 *buf = (u32 *)(runtime->dma_area +
417 rsnd_dai_pointer_offset(io, 0));
418
419 rsnd_ssi_record_error(ssi, status);
420
421 /*
422 * 8/16/32 data can be assesse to TDR/RDR register
423 * directly as 32bit data
424 * see rsnd_ssi_init()
425 */
426 if (rsnd_dai_is_play(rdai, io))
427 rsnd_mod_write(&ssi->mod, SSITDR, *buf);
428 else
429 *buf = rsnd_mod_read(&ssi->mod, SSIRDR);
430
431 rsnd_dai_pointer_update(io, sizeof(*buf));
432
433 ret = IRQ_HANDLED;
434 }
435
436 return ret;
437}
438
439static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
440 struct rsnd_dai *rdai,
441 struct rsnd_dai_stream *io)
442{
443 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
444 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700445
446 /* enable PIO IRQ */
447 ssi->cr_etc = UIEN | OIEN | DIEN;
448
Kuninori Morimoto52ea2a72013-12-19 19:28:19 -0800449 /* enable PIO interrupt if gen2 */
450 if (rsnd_is_gen2(priv))
451 rsnd_mod_write(&ssi->mod, INT_ENABLE, 0x0f000000);
Kuninori Morimoto507d4662013-11-28 18:43:45 -0800452
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700453 rsnd_ssi_hw_start(ssi, rdai, io);
454
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700455 return 0;
456}
457
458static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
459 struct rsnd_dai *rdai,
460 struct rsnd_dai_stream *io)
461{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700462 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
463
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700464 ssi->cr_etc = 0;
465
466 rsnd_ssi_hw_stop(ssi, rdai);
467
468 return 0;
469}
470
471static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
472 .name = "ssi (pio)",
473 .init = rsnd_ssi_init,
474 .quit = rsnd_ssi_quit,
475 .start = rsnd_ssi_pio_start,
476 .stop = rsnd_ssi_pio_stop,
477};
478
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700479static int rsnd_ssi_dma_inquiry(struct rsnd_dma *dma, dma_addr_t *buf, int *len)
480{
481 struct rsnd_ssi *ssi = rsnd_dma_to_ssi(dma);
482 struct rsnd_dai_stream *io = ssi->io;
483 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
484
485 *len = io->byte_per_period;
486 *buf = runtime->dma_addr +
487 rsnd_dai_pointer_offset(io, ssi->dma_offset + *len);
488 ssi->dma_offset = *len; /* it cares A/B plane */
489
490 return 0;
491}
492
493static int rsnd_ssi_dma_complete(struct rsnd_dma *dma)
494{
495 struct rsnd_ssi *ssi = rsnd_dma_to_ssi(dma);
496 struct rsnd_dai_stream *io = ssi->io;
497 u32 status = rsnd_mod_read(&ssi->mod, SSISR);
498
499 rsnd_ssi_record_error(ssi, status);
500
501 rsnd_dai_pointer_update(ssi->io, io->byte_per_period);
502
503 return 0;
504}
505
506static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
507 struct rsnd_dai *rdai,
508 struct rsnd_dai_stream *io)
509{
510 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
511 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
512
513 /* enable DMA transfer */
514 ssi->cr_etc = DMEN;
515 ssi->dma_offset = 0;
516
517 rsnd_dma_start(dma);
518
519 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
520
521 /* enable WS continue */
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800522 if (rsnd_dai_is_clk_master(rdai))
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700523 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
524
525 return 0;
526}
527
528static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
529 struct rsnd_dai *rdai,
530 struct rsnd_dai_stream *io)
531{
532 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
533 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
534
535 ssi->cr_etc = 0;
536
537 rsnd_ssi_hw_stop(ssi, rdai);
538
539 rsnd_dma_stop(dma);
540
541 return 0;
542}
543
544static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
545 .name = "ssi (dma)",
546 .init = rsnd_ssi_init,
547 .quit = rsnd_ssi_quit,
548 .start = rsnd_ssi_dma_start,
549 .stop = rsnd_ssi_dma_stop,
550};
551
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700552/*
553 * Non SSI
554 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700555static struct rsnd_mod_ops rsnd_ssi_non_ops = {
556 .name = "ssi (non)",
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700557};
558
559/*
560 * ssi mod function
561 */
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700562struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
563 int dai_id, int is_play)
564{
565 struct rsnd_ssi *ssi;
566 int i, has_play;
567
568 is_play = !!is_play;
569
570 for_each_rsnd_ssi(ssi, priv, i) {
571 if (rsnd_ssi_dai_id(ssi) != dai_id)
572 continue;
573
574 has_play = !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY);
575
576 if (is_play == has_play)
577 return &ssi->mod;
578 }
579
580 return NULL;
581}
582
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700583struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
584{
Takashi Iwai8b147192013-11-05 18:40:05 +0100585 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
586 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700587
588 return &(((struct rsnd_ssiu *)(priv->ssiu))->ssi + id)->mod;
589}
590
591int rsnd_ssi_probe(struct platform_device *pdev,
592 struct rcar_snd_info *info,
593 struct rsnd_priv *priv)
594{
595 struct rsnd_ssi_platform_info *pinfo;
596 struct device *dev = rsnd_priv_to_dev(priv);
597 struct rsnd_mod_ops *ops;
598 struct clk *clk;
599 struct rsnd_ssiu *ssiu;
600 struct rsnd_ssi *ssi;
601 char name[RSND_SSI_NAME_SIZE];
602 int i, nr, ret;
603
604 /*
605 * init SSI
606 */
607 nr = info->ssi_info_nr;
608 ssiu = devm_kzalloc(dev, sizeof(*ssiu) + (sizeof(*ssi) * nr),
609 GFP_KERNEL);
610 if (!ssiu) {
611 dev_err(dev, "SSI allocate failed\n");
612 return -ENOMEM;
613 }
614
615 priv->ssiu = ssiu;
616 ssiu->ssi = (struct rsnd_ssi *)(ssiu + 1);
617 ssiu->ssi_nr = nr;
618
619 for_each_rsnd_ssi(ssi, priv, i) {
620 pinfo = &info->ssi_info[i];
621
622 snprintf(name, RSND_SSI_NAME_SIZE, "ssi.%d", i);
623
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800624 clk = devm_clk_get(dev, name);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700625 if (IS_ERR(clk))
626 return PTR_ERR(clk);
627
628 ssi->info = pinfo;
629 ssi->clk = clk;
630
631 ops = &rsnd_ssi_non_ops;
632
633 /*
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700634 * SSI DMA case
635 */
636 if (pinfo->dma_id > 0) {
637 ret = rsnd_dma_init(
638 priv, rsnd_mod_to_dma(&ssi->mod),
639 (rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY),
640 pinfo->dma_id,
641 rsnd_ssi_dma_inquiry,
642 rsnd_ssi_dma_complete);
643 if (ret < 0)
644 dev_info(dev, "SSI DMA failed. try PIO transter\n");
645 else
646 ops = &rsnd_ssi_dma_ops;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700647
648 dev_dbg(dev, "SSI%d use DMA transfer\n", i);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700649 }
650
651 /*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700652 * SSI PIO case
653 */
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700654 if (!rsnd_ssi_dma_available(ssi) &&
655 rsnd_ssi_pio_available(ssi)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700656 ret = devm_request_irq(dev, pinfo->pio_irq,
657 &rsnd_ssi_pio_interrupt,
658 IRQF_SHARED,
659 dev_name(dev), ssi);
660 if (ret) {
661 dev_err(dev, "SSI request interrupt failed\n");
662 return ret;
663 }
664
665 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700666
667 dev_dbg(dev, "SSI%d use PIO transfer\n", i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700668 }
669
670 rsnd_mod_init(priv, &ssi->mod, ops, i);
671 }
672
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700673 dev_dbg(dev, "ssi probed\n");
674
675 return 0;
676}
677
678void rsnd_ssi_remove(struct platform_device *pdev,
679 struct rsnd_priv *priv)
680{
681 struct rsnd_ssi *ssi;
682 int i;
683
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700684 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700685 if (rsnd_ssi_dma_available(ssi))
686 rsnd_dma_quit(priv, rsnd_mod_to_dma(&ssi->mod));
687 }
688
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700689}