blob: 34234813f7421764865be355e4a639584ca96e20 [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;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070067 u32 cr_own;
68 u32 cr_clk;
69 u32 cr_etc;
70 int err;
71 unsigned int usrcnt;
72 unsigned int rate;
73};
74
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070075#define for_each_rsnd_ssi(pos, priv, i) \
76 for (i = 0; \
77 (i < rsnd_ssi_nr(priv)) && \
Kuninori Morimotodd27d802014-01-23 18:39:40 -080078 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070079 i++)
80
Kuninori Morimotodd27d802014-01-23 18:39:40 -080081#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070082#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
Kuninori Morimoto849fc822013-07-28 18:59:02 -070083#define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
84#define rsnd_ssi_pio_available(ssi) ((ssi)->info->pio_irq > 0)
85#define rsnd_ssi_dma_available(ssi) \
86 rsnd_dma_available(rsnd_mod_to_dma(&(ssi)->mod))
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070087#define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070088#define rsnd_ssi_mode_flags(p) ((p)->info->flags)
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -070089#define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070090
Kuninori Morimotoae5c3222013-07-21 21:36:57 -070091static void rsnd_ssi_status_check(struct rsnd_mod *mod,
92 u32 bit)
93{
94 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
95 struct device *dev = rsnd_priv_to_dev(priv);
96 u32 status;
97 int i;
98
99 for (i = 0; i < 1024; i++) {
100 status = rsnd_mod_read(mod, SSISR);
101 if (status & bit)
102 return;
103
104 udelay(50);
105 }
106
107 dev_warn(dev, "status check failed\n");
108}
109
110static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800111 struct rsnd_dai_stream *io)
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700112{
113 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800114 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700115 struct device *dev = rsnd_priv_to_dev(priv);
116 int i, j, ret;
117 int adg_clk_div_table[] = {
118 1, 6, /* see adg.c */
119 };
120 int ssi_clk_mul_table[] = {
121 1, 2, 4, 8, 16, 6, 12,
122 };
123 unsigned int main_rate;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800124 unsigned int rate = rsnd_scu_get_ssi_rate(priv, io, runtime);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700125
126 /*
127 * Find best clock, and try to start ADG
128 */
129 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
130 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
131
132 /*
133 * this driver is assuming that
134 * system word is 64fs (= 2 x 32bit)
Kuninori Morimoto729aca52013-12-19 19:26:44 -0800135 * see rsnd_ssi_init()
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700136 */
137 main_rate = rate / adg_clk_div_table[i]
138 * 32 * 2 * ssi_clk_mul_table[j];
139
140 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
141 if (0 == ret) {
142 ssi->rate = rate;
143 ssi->cr_clk = FORCE | SWL_32 |
144 SCKD | SWSD | CKDV(j);
145
146 dev_dbg(dev, "ssi%d outputs %u Hz\n",
147 rsnd_mod_id(&ssi->mod), rate);
148
149 return 0;
150 }
151 }
152 }
153
154 dev_err(dev, "unsupported clock rate\n");
155 return -EIO;
156}
157
158static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
159{
160 ssi->rate = 0;
161 ssi->cr_clk = 0;
162 rsnd_adg_ssi_clk_stop(&ssi->mod);
163}
164
165static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
166 struct rsnd_dai *rdai,
167 struct rsnd_dai_stream *io)
168{
169 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
170 struct device *dev = rsnd_priv_to_dev(priv);
171 u32 cr;
172
173 if (0 == ssi->usrcnt) {
174 clk_enable(ssi->clk);
175
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800176 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700177 if (rsnd_ssi_clk_from_parent(ssi))
178 rsnd_ssi_hw_start(ssi->parent, rdai, io);
179 else
Kuninori Morimotoadcf7d52013-12-19 19:28:39 -0800180 rsnd_ssi_master_clk_start(ssi, io);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700181 }
182 }
183
184 cr = ssi->cr_own |
185 ssi->cr_clk |
186 ssi->cr_etc |
187 EN;
188
189 rsnd_mod_write(&ssi->mod, SSICR, cr);
190
191 ssi->usrcnt++;
192
193 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
194}
195
196static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
197 struct rsnd_dai *rdai)
198{
199 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
200 struct device *dev = rsnd_priv_to_dev(priv);
201 u32 cr;
202
203 if (0 == ssi->usrcnt) /* stop might be called without start */
204 return;
205
206 ssi->usrcnt--;
207
208 if (0 == ssi->usrcnt) {
209 /*
210 * disable all IRQ,
211 * and, wait all data was sent
212 */
213 cr = ssi->cr_own |
214 ssi->cr_clk;
215
216 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
217 rsnd_ssi_status_check(&ssi->mod, DIRQ);
218
219 /*
220 * disable SSI,
221 * and, wait idle state
222 */
223 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
224 rsnd_ssi_status_check(&ssi->mod, IIRQ);
225
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800226 if (rsnd_dai_is_clk_master(rdai)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700227 if (rsnd_ssi_clk_from_parent(ssi))
228 rsnd_ssi_hw_stop(ssi->parent, rdai);
229 else
230 rsnd_ssi_master_clk_stop(ssi);
231 }
232
233 clk_disable(ssi->clk);
234 }
235
236 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
237}
238
239/*
240 * SSI mod common functions
241 */
242static int rsnd_ssi_init(struct rsnd_mod *mod,
243 struct rsnd_dai *rdai,
244 struct rsnd_dai_stream *io)
245{
246 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700247 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
248 u32 cr;
249
250 cr = FORCE;
251
252 /*
253 * always use 32bit system word for easy clock calculation.
254 * see also rsnd_ssi_master_clk_enable()
255 */
256 cr |= SWL_32;
257
258 /*
259 * init clock settings for SSICR
260 */
261 switch (runtime->sample_bits) {
262 case 16:
263 cr |= DWL_16;
264 break;
265 case 32:
266 cr |= DWL_24;
267 break;
268 default:
269 return -EIO;
270 }
271
272 if (rdai->bit_clk_inv)
273 cr |= SCKP;
274 if (rdai->frm_clk_inv)
275 cr |= SWSP;
276 if (rdai->data_alignment)
277 cr |= SDTA;
278 if (rdai->sys_delay)
279 cr |= DEL;
280 if (rsnd_dai_is_play(rdai, io))
281 cr |= TRMD;
282
283 /*
284 * set ssi parameter
285 */
286 ssi->rdai = rdai;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700287 ssi->cr_own = cr;
288 ssi->err = -1; /* ignore 1st error */
289
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700290 return 0;
291}
292
293static int rsnd_ssi_quit(struct rsnd_mod *mod,
294 struct rsnd_dai *rdai,
295 struct rsnd_dai_stream *io)
296{
297 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
298 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
299 struct device *dev = rsnd_priv_to_dev(priv);
300
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700301 if (ssi->err > 0)
302 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
303
304 ssi->rdai = NULL;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700305 ssi->cr_own = 0;
306 ssi->err = 0;
307
308 return 0;
309}
310
311static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
312{
313 /* under/over flow error */
314 if (status & (UIRQ | OIRQ)) {
315 ssi->err++;
316
317 /* clear error status */
318 rsnd_mod_write(&ssi->mod, SSISR, 0);
319 }
320}
321
322/*
323 * SSI PIO
324 */
325static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
326{
327 struct rsnd_ssi *ssi = data;
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800328 struct rsnd_mod *mod = &ssi->mod;
329 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
330 u32 status = rsnd_mod_read(mod, SSISR);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700331 irqreturn_t ret = IRQ_NONE;
332
333 if (io && (status & DIRQ)) {
334 struct rsnd_dai *rdai = ssi->rdai;
335 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
336 u32 *buf = (u32 *)(runtime->dma_area +
337 rsnd_dai_pointer_offset(io, 0));
338
339 rsnd_ssi_record_error(ssi, status);
340
341 /*
342 * 8/16/32 data can be assesse to TDR/RDR register
343 * directly as 32bit data
344 * see rsnd_ssi_init()
345 */
346 if (rsnd_dai_is_play(rdai, io))
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800347 rsnd_mod_write(mod, SSITDR, *buf);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700348 else
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800349 *buf = rsnd_mod_read(mod, SSIRDR);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700350
351 rsnd_dai_pointer_update(io, sizeof(*buf));
352
353 ret = IRQ_HANDLED;
354 }
355
356 return ret;
357}
358
359static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
360 struct rsnd_dai *rdai,
361 struct rsnd_dai_stream *io)
362{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700363 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700364
365 /* enable PIO IRQ */
366 ssi->cr_etc = UIEN | OIEN | DIEN;
367
368 rsnd_ssi_hw_start(ssi, rdai, io);
369
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700370 return 0;
371}
372
373static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
374 struct rsnd_dai *rdai,
375 struct rsnd_dai_stream *io)
376{
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700377 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
378
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700379 ssi->cr_etc = 0;
380
381 rsnd_ssi_hw_stop(ssi, rdai);
382
383 return 0;
384}
385
386static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
387 .name = "ssi (pio)",
388 .init = rsnd_ssi_init,
389 .quit = rsnd_ssi_quit,
390 .start = rsnd_ssi_pio_start,
391 .stop = rsnd_ssi_pio_stop,
392};
393
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700394static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
395 struct rsnd_dai *rdai,
396 struct rsnd_dai_stream *io)
397{
398 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
399 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
400
401 /* enable DMA transfer */
402 ssi->cr_etc = DMEN;
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700403
404 rsnd_dma_start(dma);
405
406 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
407
408 /* enable WS continue */
Kuninori Morimotoe779a202014-01-23 18:39:12 -0800409 if (rsnd_dai_is_clk_master(rdai))
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700410 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
411
412 return 0;
413}
414
415static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
416 struct rsnd_dai *rdai,
417 struct rsnd_dai_stream *io)
418{
419 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
420 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
421
422 ssi->cr_etc = 0;
423
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800424 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
425
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700426 rsnd_ssi_hw_stop(ssi, rdai);
427
428 rsnd_dma_stop(dma);
429
430 return 0;
431}
432
433static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
434 .name = "ssi (dma)",
435 .init = rsnd_ssi_init,
436 .quit = rsnd_ssi_quit,
437 .start = rsnd_ssi_dma_start,
438 .stop = rsnd_ssi_dma_stop,
439};
440
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700441/*
442 * Non SSI
443 */
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700444static struct rsnd_mod_ops rsnd_ssi_non_ops = {
445 .name = "ssi (non)",
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700446};
447
448/*
449 * ssi mod function
450 */
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700451struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
452 int dai_id, int is_play)
453{
Kuninori Morimoto78f13d02014-03-03 20:49:50 -0800454 struct rsnd_dai_platform_info *dai_info = NULL;
455 struct rsnd_dai_path_info *path_info = NULL;
456 struct rsnd_ssi_platform_info *target_info = NULL;
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700457 struct rsnd_ssi *ssi;
458 int i, has_play;
459
Kuninori Morimoto78f13d02014-03-03 20:49:50 -0800460 if (priv->rdai)
461 dai_info = priv->rdai[dai_id].info;
462 if (dai_info)
463 path_info = (is_play) ? &dai_info->playback : &dai_info->capture;
464 if (path_info)
465 target_info = path_info->ssi;
466
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700467 is_play = !!is_play;
468
469 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimoto78f13d02014-03-03 20:49:50 -0800470 if (target_info == ssi->info)
471 return &ssi->mod;
472
473 /* for compatible */
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700474 if (rsnd_ssi_dai_id(ssi) != dai_id)
475 continue;
476
Kuninori Morimoto32f27eb2014-01-23 18:41:17 -0800477 has_play = rsnd_ssi_is_play(&ssi->mod);
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700478
479 if (is_play == has_play)
480 return &ssi->mod;
481 }
482
483 return NULL;
484}
485
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700486struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
487{
Takashi Iwai8b147192013-11-05 18:40:05 +0100488 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
489 id = 0;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700490
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800491 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700492}
493
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800494int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
495{
496 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
497
498 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
499}
500
Kuninori Morimoto32f27eb2014-01-23 18:41:17 -0800501int rsnd_ssi_is_play(struct rsnd_mod *mod)
502{
503 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
504
505 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY);
506}
507
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800508static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
509{
510 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
511 return;
512
513 switch (rsnd_mod_id(&ssi->mod)) {
514 case 1:
515 case 2:
516 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
517 break;
518 case 4:
519 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
520 break;
521 case 8:
522 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
523 break;
524 }
525}
526
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700527int rsnd_ssi_probe(struct platform_device *pdev,
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700528 struct rsnd_priv *priv)
529{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800530 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700531 struct rsnd_ssi_platform_info *pinfo;
532 struct device *dev = rsnd_priv_to_dev(priv);
533 struct rsnd_mod_ops *ops;
534 struct clk *clk;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700535 struct rsnd_ssi *ssi;
536 char name[RSND_SSI_NAME_SIZE];
537 int i, nr, ret;
538
539 /*
540 * init SSI
541 */
542 nr = info->ssi_info_nr;
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800543 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
544 if (!ssi) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700545 dev_err(dev, "SSI allocate failed\n");
546 return -ENOMEM;
547 }
548
Kuninori Morimotodd27d802014-01-23 18:39:40 -0800549 priv->ssi = ssi;
550 priv->ssi_nr = nr;
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700551
552 for_each_rsnd_ssi(ssi, priv, i) {
553 pinfo = &info->ssi_info[i];
554
555 snprintf(name, RSND_SSI_NAME_SIZE, "ssi.%d", i);
556
Kuninori Morimoto60dbb4f2013-12-03 22:09:33 -0800557 clk = devm_clk_get(dev, name);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700558 if (IS_ERR(clk))
559 return PTR_ERR(clk);
560
561 ssi->info = pinfo;
562 ssi->clk = clk;
563
564 ops = &rsnd_ssi_non_ops;
565
566 /*
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700567 * SSI DMA case
568 */
569 if (pinfo->dma_id > 0) {
570 ret = rsnd_dma_init(
571 priv, rsnd_mod_to_dma(&ssi->mod),
Kuninori Morimoto32f27eb2014-01-23 18:41:17 -0800572 rsnd_ssi_is_play(&ssi->mod),
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800573 pinfo->dma_id);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700574 if (ret < 0)
575 dev_info(dev, "SSI DMA failed. try PIO transter\n");
576 else
577 ops = &rsnd_ssi_dma_ops;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700578
579 dev_dbg(dev, "SSI%d use DMA transfer\n", i);
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700580 }
581
582 /*
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700583 * SSI PIO case
584 */
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700585 if (!rsnd_ssi_dma_available(ssi) &&
586 rsnd_ssi_pio_available(ssi)) {
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700587 ret = devm_request_irq(dev, pinfo->pio_irq,
588 &rsnd_ssi_pio_interrupt,
589 IRQF_SHARED,
590 dev_name(dev), ssi);
591 if (ret) {
592 dev_err(dev, "SSI request interrupt failed\n");
593 return ret;
594 }
595
596 ops = &rsnd_ssi_pio_ops;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700597
598 dev_dbg(dev, "SSI%d use PIO transfer\n", i);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700599 }
600
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800601 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800602
603 rsnd_ssi_parent_clk_setup(priv, ssi);
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700604 }
605
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700606 return 0;
607}
608
609void rsnd_ssi_remove(struct platform_device *pdev,
610 struct rsnd_priv *priv)
611{
612 struct rsnd_ssi *ssi;
613 int i;
614
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700615 for_each_rsnd_ssi(ssi, priv, i) {
Kuninori Morimoto849fc822013-07-28 18:59:02 -0700616 if (rsnd_ssi_dma_available(ssi))
617 rsnd_dma_quit(priv, rsnd_mod_to_dma(&ssi->mod));
618 }
619
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700620}