blob: c48a6c7cd08eda60ed5fe6de7b2a9f6d0b1accd5 [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 */
22#define UIEN (1 << 27) /* Underflow Interrupt Enable */
23#define OIEN (1 << 26) /* Overflow Interrupt Enable */
24#define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
25#define DIEN (1 << 24) /* Data Interrupt Enable */
26
27#define DWL_8 (0 << 19) /* Data Word Length */
28#define DWL_16 (1 << 19) /* Data Word Length */
29#define DWL_18 (2 << 19) /* Data Word Length */
30#define DWL_20 (3 << 19) /* Data Word Length */
31#define DWL_22 (4 << 19) /* Data Word Length */
32#define DWL_24 (5 << 19) /* Data Word Length */
33#define DWL_32 (6 << 19) /* Data Word Length */
34
35#define SWL_32 (3 << 16) /* R/W System Word Length */
36#define SCKD (1 << 15) /* Serial Bit Clock Direction */
37#define SWSD (1 << 14) /* Serial WS Direction */
38#define SCKP (1 << 13) /* Serial Bit Clock Polarity */
39#define SWSP (1 << 12) /* Serial WS Polarity */
40#define SDTA (1 << 10) /* Serial Data Alignment */
41#define DEL (1 << 8) /* Serial Data Delay */
42#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
43#define TRMD (1 << 1) /* Transmit/Receive Mode Select */
44#define EN (1 << 0) /* SSI Module Enable */
45
46/*
47 * SSISR
48 */
49#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
50#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
51#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
52#define DIRQ (1 << 24) /* Data Interrupt Status Flag */
53
54struct rsnd_ssi {
55 struct clk *clk;
56 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
57 struct rsnd_ssi *parent;
58 struct rsnd_mod mod;
59
60 struct rsnd_dai *rdai;
61 struct rsnd_dai_stream *io;
62 u32 cr_own;
63 u32 cr_clk;
64 u32 cr_etc;
65 int err;
66 unsigned int usrcnt;
67 unsigned int rate;
68};
69
70struct rsnd_ssiu {
71 u32 ssi_mode0;
72 u32 ssi_mode1;
73
74 int ssi_nr;
75 struct rsnd_ssi *ssi;
76};
77
78#define for_each_rsnd_ssi(pos, priv, i) \
79 for (i = 0; \
80 (i < rsnd_ssi_nr(priv)) && \
81 ((pos) = ((struct rsnd_ssiu *)((priv)->ssiu))->ssi + i); \
82 i++)
83
84#define rsnd_ssi_nr(priv) (((struct rsnd_ssiu *)((priv)->ssiu))->ssi_nr)
85#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
86#define rsnd_ssi_is_pio(ssi) ((ssi)->info->pio_irq > 0)
87#define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
88#define rsnd_rdai_is_clk_master(rdai) ((rdai)->clk_master)
89#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#define rsnd_ssi_to_ssiu(ssi)\
92 (((struct rsnd_ssiu *)((ssi) - rsnd_mod_id(&(ssi)->mod))) - 1)
93
94static void rsnd_ssi_mode_init(struct rsnd_priv *priv,
95 struct rsnd_ssiu *ssiu)
96{
97 struct rsnd_ssi *ssi;
98 u32 flags;
99 u32 val;
100 int i;
101
102 /*
103 * SSI_MODE0
104 */
105 ssiu->ssi_mode0 = 0;
106 for_each_rsnd_ssi(ssi, priv, i)
107 ssiu->ssi_mode0 |= (1 << i);
108
109 /*
110 * SSI_MODE1
111 */
112#define ssi_parent_set(p, sync, adg, ext) \
113 do { \
114 ssi->parent = ssiu->ssi + p; \
115 if (flags & RSND_SSI_CLK_FROM_ADG) \
116 val = adg; \
117 else \
118 val = ext; \
119 if (flags & RSND_SSI_SYNC) \
120 val |= sync; \
121 } while (0)
122
123 ssiu->ssi_mode1 = 0;
124 for_each_rsnd_ssi(ssi, priv, i) {
125 flags = rsnd_ssi_mode_flags(ssi);
126
127 if (!(flags & RSND_SSI_CLK_PIN_SHARE))
128 continue;
129
130 val = 0;
131 switch (i) {
132 case 1:
133 ssi_parent_set(0, (1 << 4), (0x2 << 0), (0x1 << 0));
134 break;
135 case 2:
136 ssi_parent_set(0, (1 << 4), (0x2 << 2), (0x1 << 2));
137 break;
138 case 4:
139 ssi_parent_set(3, (1 << 20), (0x2 << 16), (0x1 << 16));
140 break;
141 case 8:
142 ssi_parent_set(7, 0, 0, 0);
143 break;
144 }
145
146 ssiu->ssi_mode1 |= val;
147 }
148}
149
150static void rsnd_ssi_mode_set(struct rsnd_ssi *ssi)
151{
152 struct rsnd_ssiu *ssiu = rsnd_ssi_to_ssiu(ssi);
153
154 rsnd_mod_write(&ssi->mod, SSI_MODE0, ssiu->ssi_mode0);
155 rsnd_mod_write(&ssi->mod, SSI_MODE1, ssiu->ssi_mode1);
156}
157
158static void rsnd_ssi_status_check(struct rsnd_mod *mod,
159 u32 bit)
160{
161 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
162 struct device *dev = rsnd_priv_to_dev(priv);
163 u32 status;
164 int i;
165
166 for (i = 0; i < 1024; i++) {
167 status = rsnd_mod_read(mod, SSISR);
168 if (status & bit)
169 return;
170
171 udelay(50);
172 }
173
174 dev_warn(dev, "status check failed\n");
175}
176
177static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
178 unsigned int rate)
179{
180 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
181 struct device *dev = rsnd_priv_to_dev(priv);
182 int i, j, ret;
183 int adg_clk_div_table[] = {
184 1, 6, /* see adg.c */
185 };
186 int ssi_clk_mul_table[] = {
187 1, 2, 4, 8, 16, 6, 12,
188 };
189 unsigned int main_rate;
190
191 /*
192 * Find best clock, and try to start ADG
193 */
194 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
195 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
196
197 /*
198 * this driver is assuming that
199 * system word is 64fs (= 2 x 32bit)
200 * see rsnd_ssi_start()
201 */
202 main_rate = rate / adg_clk_div_table[i]
203 * 32 * 2 * ssi_clk_mul_table[j];
204
205 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
206 if (0 == ret) {
207 ssi->rate = rate;
208 ssi->cr_clk = FORCE | SWL_32 |
209 SCKD | SWSD | CKDV(j);
210
211 dev_dbg(dev, "ssi%d outputs %u Hz\n",
212 rsnd_mod_id(&ssi->mod), rate);
213
214 return 0;
215 }
216 }
217 }
218
219 dev_err(dev, "unsupported clock rate\n");
220 return -EIO;
221}
222
223static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
224{
225 ssi->rate = 0;
226 ssi->cr_clk = 0;
227 rsnd_adg_ssi_clk_stop(&ssi->mod);
228}
229
230static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
231 struct rsnd_dai *rdai,
232 struct rsnd_dai_stream *io)
233{
234 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
235 struct device *dev = rsnd_priv_to_dev(priv);
236 u32 cr;
237
238 if (0 == ssi->usrcnt) {
239 clk_enable(ssi->clk);
240
241 if (rsnd_rdai_is_clk_master(rdai)) {
242 struct snd_pcm_runtime *runtime;
243
244 runtime = rsnd_io_to_runtime(io);
245
246 if (rsnd_ssi_clk_from_parent(ssi))
247 rsnd_ssi_hw_start(ssi->parent, rdai, io);
248 else
249 rsnd_ssi_master_clk_start(ssi, runtime->rate);
250 }
251 }
252
253 cr = ssi->cr_own |
254 ssi->cr_clk |
255 ssi->cr_etc |
256 EN;
257
258 rsnd_mod_write(&ssi->mod, SSICR, cr);
259
260 ssi->usrcnt++;
261
262 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
263}
264
265static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
266 struct rsnd_dai *rdai)
267{
268 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
269 struct device *dev = rsnd_priv_to_dev(priv);
270 u32 cr;
271
272 if (0 == ssi->usrcnt) /* stop might be called without start */
273 return;
274
275 ssi->usrcnt--;
276
277 if (0 == ssi->usrcnt) {
278 /*
279 * disable all IRQ,
280 * and, wait all data was sent
281 */
282 cr = ssi->cr_own |
283 ssi->cr_clk;
284
285 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
286 rsnd_ssi_status_check(&ssi->mod, DIRQ);
287
288 /*
289 * disable SSI,
290 * and, wait idle state
291 */
292 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
293 rsnd_ssi_status_check(&ssi->mod, IIRQ);
294
295 if (rsnd_rdai_is_clk_master(rdai)) {
296 if (rsnd_ssi_clk_from_parent(ssi))
297 rsnd_ssi_hw_stop(ssi->parent, rdai);
298 else
299 rsnd_ssi_master_clk_stop(ssi);
300 }
301
302 clk_disable(ssi->clk);
303 }
304
305 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
306}
307
308/*
309 * SSI mod common functions
310 */
311static int rsnd_ssi_init(struct rsnd_mod *mod,
312 struct rsnd_dai *rdai,
313 struct rsnd_dai_stream *io)
314{
315 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
316 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
317 struct device *dev = rsnd_priv_to_dev(priv);
318 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
319 u32 cr;
320
321 cr = FORCE;
322
323 /*
324 * always use 32bit system word for easy clock calculation.
325 * see also rsnd_ssi_master_clk_enable()
326 */
327 cr |= SWL_32;
328
329 /*
330 * init clock settings for SSICR
331 */
332 switch (runtime->sample_bits) {
333 case 16:
334 cr |= DWL_16;
335 break;
336 case 32:
337 cr |= DWL_24;
338 break;
339 default:
340 return -EIO;
341 }
342
343 if (rdai->bit_clk_inv)
344 cr |= SCKP;
345 if (rdai->frm_clk_inv)
346 cr |= SWSP;
347 if (rdai->data_alignment)
348 cr |= SDTA;
349 if (rdai->sys_delay)
350 cr |= DEL;
351 if (rsnd_dai_is_play(rdai, io))
352 cr |= TRMD;
353
354 /*
355 * set ssi parameter
356 */
357 ssi->rdai = rdai;
358 ssi->io = io;
359 ssi->cr_own = cr;
360 ssi->err = -1; /* ignore 1st error */
361
362 rsnd_ssi_mode_set(ssi);
363
364 dev_dbg(dev, "%s.%d init\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
365
366 return 0;
367}
368
369static int rsnd_ssi_quit(struct rsnd_mod *mod,
370 struct rsnd_dai *rdai,
371 struct rsnd_dai_stream *io)
372{
373 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
374 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
375 struct device *dev = rsnd_priv_to_dev(priv);
376
377 dev_dbg(dev, "%s.%d quit\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
378
379 if (ssi->err > 0)
380 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
381
382 ssi->rdai = NULL;
383 ssi->io = NULL;
384 ssi->cr_own = 0;
385 ssi->err = 0;
386
387 return 0;
388}
389
390static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
391{
392 /* under/over flow error */
393 if (status & (UIRQ | OIRQ)) {
394 ssi->err++;
395
396 /* clear error status */
397 rsnd_mod_write(&ssi->mod, SSISR, 0);
398 }
399}
400
401/*
402 * SSI PIO
403 */
404static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
405{
406 struct rsnd_ssi *ssi = data;
407 struct rsnd_dai_stream *io = ssi->io;
408 u32 status = rsnd_mod_read(&ssi->mod, SSISR);
409 irqreturn_t ret = IRQ_NONE;
410
411 if (io && (status & DIRQ)) {
412 struct rsnd_dai *rdai = ssi->rdai;
413 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
414 u32 *buf = (u32 *)(runtime->dma_area +
415 rsnd_dai_pointer_offset(io, 0));
416
417 rsnd_ssi_record_error(ssi, status);
418
419 /*
420 * 8/16/32 data can be assesse to TDR/RDR register
421 * directly as 32bit data
422 * see rsnd_ssi_init()
423 */
424 if (rsnd_dai_is_play(rdai, io))
425 rsnd_mod_write(&ssi->mod, SSITDR, *buf);
426 else
427 *buf = rsnd_mod_read(&ssi->mod, SSIRDR);
428
429 rsnd_dai_pointer_update(io, sizeof(*buf));
430
431 ret = IRQ_HANDLED;
432 }
433
434 return ret;
435}
436
437static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
438 struct rsnd_dai *rdai,
439 struct rsnd_dai_stream *io)
440{
441 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
442 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
443 struct device *dev = rsnd_priv_to_dev(priv);
444
445 /* enable PIO IRQ */
446 ssi->cr_etc = UIEN | OIEN | DIEN;
447
448 rsnd_ssi_hw_start(ssi, rdai, io);
449
450 dev_dbg(dev, "%s.%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
451
452 return 0;
453}
454
455static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
456 struct rsnd_dai *rdai,
457 struct rsnd_dai_stream *io)
458{
459 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
460 struct device *dev = rsnd_priv_to_dev(priv);
461 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
462
463 dev_dbg(dev, "%s.%d stop\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
464
465 ssi->cr_etc = 0;
466
467 rsnd_ssi_hw_stop(ssi, rdai);
468
469 return 0;
470}
471
472static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
473 .name = "ssi (pio)",
474 .init = rsnd_ssi_init,
475 .quit = rsnd_ssi_quit,
476 .start = rsnd_ssi_pio_start,
477 .stop = rsnd_ssi_pio_stop,
478};
479
480/*
481 * Non SSI
482 */
483static int rsnd_ssi_non(struct rsnd_mod *mod,
484 struct rsnd_dai *rdai,
485 struct rsnd_dai_stream *io)
486{
487 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
488 struct device *dev = rsnd_priv_to_dev(priv);
489
490 dev_dbg(dev, "%s\n", __func__);
491
492 return 0;
493}
494
495static struct rsnd_mod_ops rsnd_ssi_non_ops = {
496 .name = "ssi (non)",
497 .init = rsnd_ssi_non,
498 .quit = rsnd_ssi_non,
499 .start = rsnd_ssi_non,
500 .stop = rsnd_ssi_non,
501};
502
503/*
504 * ssi mod function
505 */
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700506struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
507 int dai_id, int is_play)
508{
509 struct rsnd_ssi *ssi;
510 int i, has_play;
511
512 is_play = !!is_play;
513
514 for_each_rsnd_ssi(ssi, priv, i) {
515 if (rsnd_ssi_dai_id(ssi) != dai_id)
516 continue;
517
518 has_play = !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY);
519
520 if (is_play == has_play)
521 return &ssi->mod;
522 }
523
524 return NULL;
525}
526
Kuninori Morimotoae5c3222013-07-21 21:36:57 -0700527struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
528{
529 BUG_ON(id < 0 || id >= rsnd_ssi_nr(priv));
530
531 return &(((struct rsnd_ssiu *)(priv->ssiu))->ssi + id)->mod;
532}
533
534int rsnd_ssi_probe(struct platform_device *pdev,
535 struct rcar_snd_info *info,
536 struct rsnd_priv *priv)
537{
538 struct rsnd_ssi_platform_info *pinfo;
539 struct device *dev = rsnd_priv_to_dev(priv);
540 struct rsnd_mod_ops *ops;
541 struct clk *clk;
542 struct rsnd_ssiu *ssiu;
543 struct rsnd_ssi *ssi;
544 char name[RSND_SSI_NAME_SIZE];
545 int i, nr, ret;
546
547 /*
548 * init SSI
549 */
550 nr = info->ssi_info_nr;
551 ssiu = devm_kzalloc(dev, sizeof(*ssiu) + (sizeof(*ssi) * nr),
552 GFP_KERNEL);
553 if (!ssiu) {
554 dev_err(dev, "SSI allocate failed\n");
555 return -ENOMEM;
556 }
557
558 priv->ssiu = ssiu;
559 ssiu->ssi = (struct rsnd_ssi *)(ssiu + 1);
560 ssiu->ssi_nr = nr;
561
562 for_each_rsnd_ssi(ssi, priv, i) {
563 pinfo = &info->ssi_info[i];
564
565 snprintf(name, RSND_SSI_NAME_SIZE, "ssi.%d", i);
566
567 clk = clk_get(dev, name);
568 if (IS_ERR(clk))
569 return PTR_ERR(clk);
570
571 ssi->info = pinfo;
572 ssi->clk = clk;
573
574 ops = &rsnd_ssi_non_ops;
575
576 /*
577 * SSI PIO case
578 */
579 if (rsnd_ssi_is_pio(ssi)) {
580 ret = devm_request_irq(dev, pinfo->pio_irq,
581 &rsnd_ssi_pio_interrupt,
582 IRQF_SHARED,
583 dev_name(dev), ssi);
584 if (ret) {
585 dev_err(dev, "SSI request interrupt failed\n");
586 return ret;
587 }
588
589 ops = &rsnd_ssi_pio_ops;
590 }
591
592 rsnd_mod_init(priv, &ssi->mod, ops, i);
593 }
594
595 rsnd_ssi_mode_init(priv, ssiu);
596
597 dev_dbg(dev, "ssi probed\n");
598
599 return 0;
600}
601
602void rsnd_ssi_remove(struct platform_device *pdev,
603 struct rsnd_priv *priv)
604{
605 struct rsnd_ssi *ssi;
606 int i;
607
608 for_each_rsnd_ssi(ssi, priv, i)
609 clk_put(ssi->clk);
610}