2 * Renesas R-Car SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
14 #include <linux/delay.h>
16 #define RSND_SSI_NAME_SIZE 16
21 #define FORCE (1 << 31) /* Fixed */
22 #define DMEN (1 << 28) /* DMA Enable */
23 #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 */
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 */
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 */
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 */
58 #define CONT (1 << 8) /* WS Continue Function */
60 #define SSI_NAME "ssi"
64 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
65 struct rsnd_ssi *parent;
68 struct rsnd_dai *rdai;
77 #define for_each_rsnd_ssi(pos, priv, i) \
79 (i < rsnd_ssi_nr(priv)) && \
80 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
83 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
84 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
85 #define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
86 #define rsnd_ssi_pio_available(ssi) ((ssi)->info->pio_irq > 0)
87 #define rsnd_ssi_dma_available(ssi) \
88 rsnd_dma_available(rsnd_mod_to_dma(&(ssi)->mod))
89 #define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
90 #define rsnd_ssi_mode_flags(p) ((p)->info->flags)
91 #define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
93 static int rsnd_ssi_use_busif(struct rsnd_mod *mod)
95 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
96 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
99 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
101 if (rsnd_io_to_mod_src(io))
107 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
110 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
111 struct device *dev = rsnd_priv_to_dev(priv);
115 for (i = 0; i < 1024; i++) {
116 status = rsnd_mod_read(mod, SSISR);
123 dev_warn(dev, "status check failed\n");
126 static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
127 struct rsnd_dai_stream *io)
129 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
130 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
131 struct device *dev = rsnd_priv_to_dev(priv);
133 int adg_clk_div_table[] = {
134 1, 6, /* see adg.c */
136 int ssi_clk_mul_table[] = {
137 1, 2, 4, 8, 16, 6, 12,
139 unsigned int main_rate;
140 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
143 * Find best clock, and try to start ADG
145 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
146 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
149 * this driver is assuming that
150 * system word is 64fs (= 2 x 32bit)
151 * see rsnd_ssi_init()
153 main_rate = rate / adg_clk_div_table[i]
154 * 32 * 2 * ssi_clk_mul_table[j];
156 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
159 ssi->cr_clk = FORCE | SWL_32 |
160 SCKD | SWSD | CKDV(j);
162 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
163 rsnd_mod_name(&ssi->mod),
164 rsnd_mod_id(&ssi->mod), rate);
171 dev_err(dev, "unsupported clock rate\n");
175 static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
179 rsnd_adg_ssi_clk_stop(&ssi->mod);
182 static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
183 struct rsnd_dai *rdai,
184 struct rsnd_dai_stream *io)
186 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
187 struct device *dev = rsnd_priv_to_dev(priv);
190 if (0 == ssi->usrcnt) {
191 clk_prepare_enable(ssi->clk);
193 if (rsnd_dai_is_clk_master(rdai)) {
194 if (rsnd_ssi_clk_from_parent(ssi))
195 rsnd_ssi_hw_start(ssi->parent, rdai, io);
197 rsnd_ssi_master_clk_start(ssi, io);
206 rsnd_mod_write(&ssi->mod, SSICR, cr);
210 dev_dbg(dev, "%s[%d] hw started\n",
211 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
214 static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
215 struct rsnd_dai *rdai)
217 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
218 struct device *dev = rsnd_priv_to_dev(priv);
221 if (0 == ssi->usrcnt) /* stop might be called without start */
226 if (0 == ssi->usrcnt) {
229 * and, wait all data was sent
234 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
235 rsnd_ssi_status_check(&ssi->mod, DIRQ);
239 * and, wait idle state
241 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
242 rsnd_ssi_status_check(&ssi->mod, IIRQ);
244 if (rsnd_dai_is_clk_master(rdai)) {
245 if (rsnd_ssi_clk_from_parent(ssi))
246 rsnd_ssi_hw_stop(ssi->parent, rdai);
248 rsnd_ssi_master_clk_stop(ssi);
251 clk_disable_unprepare(ssi->clk);
254 dev_dbg(dev, "%s[%d] hw stopped\n",
255 rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
259 * SSI mod common functions
261 static int rsnd_ssi_init(struct rsnd_mod *mod,
262 struct rsnd_dai *rdai)
264 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
265 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
266 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
272 * always use 32bit system word for easy clock calculation.
273 * see also rsnd_ssi_master_clk_enable()
278 * init clock settings for SSICR
280 switch (runtime->sample_bits) {
291 if (rdai->bit_clk_inv)
293 if (rdai->frm_clk_inv)
295 if (rdai->data_alignment)
299 if (rsnd_dai_is_play(rdai, io))
307 ssi->err = -1; /* ignore 1st error */
312 static int rsnd_ssi_quit(struct rsnd_mod *mod,
313 struct rsnd_dai *rdai)
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);
320 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
329 static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
331 /* under/over flow error */
332 if (status & (UIRQ | OIRQ)) {
335 /* clear error status */
336 rsnd_mod_write(&ssi->mod, SSISR, 0);
343 static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
345 struct rsnd_ssi *ssi = data;
346 struct rsnd_mod *mod = &ssi->mod;
347 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
348 u32 status = rsnd_mod_read(mod, SSISR);
349 irqreturn_t ret = IRQ_NONE;
351 if (io && (status & DIRQ)) {
352 struct rsnd_dai *rdai = ssi->rdai;
353 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
354 u32 *buf = (u32 *)(runtime->dma_area +
355 rsnd_dai_pointer_offset(io, 0));
357 rsnd_ssi_record_error(ssi, status);
360 * 8/16/32 data can be assesse to TDR/RDR register
361 * directly as 32bit data
362 * see rsnd_ssi_init()
364 if (rsnd_dai_is_play(rdai, io))
365 rsnd_mod_write(mod, SSITDR, *buf);
367 *buf = rsnd_mod_read(mod, SSIRDR);
369 rsnd_dai_pointer_update(io, sizeof(*buf));
377 static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
378 struct rsnd_dai *rdai)
380 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
381 struct device *dev = rsnd_priv_to_dev(priv);
382 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
383 int irq = ssi->info->pio_irq;
386 ret = devm_request_irq(dev, irq,
387 rsnd_ssi_pio_interrupt,
391 dev_err(dev, "%s[%d] (PIO) request interrupt failed\n",
392 rsnd_mod_name(mod), rsnd_mod_id(mod));
394 dev_dbg(dev, "%s[%d] (PIO) is probed\n",
395 rsnd_mod_name(mod), rsnd_mod_id(mod));
400 static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
401 struct rsnd_dai *rdai)
403 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
404 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
407 ssi->cr_etc = UIEN | OIEN | DIEN;
409 rsnd_src_ssiu_start(mod, rdai, 0);
411 rsnd_src_enable_ssi_irq(mod, rdai);
413 rsnd_ssi_hw_start(ssi, rdai, io);
418 static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
419 struct rsnd_dai *rdai)
421 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
425 rsnd_ssi_hw_stop(ssi, rdai);
427 rsnd_src_ssiu_stop(mod, rdai, 0);
432 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
434 .probe = rsnd_ssi_pio_probe,
435 .init = rsnd_ssi_init,
436 .quit = rsnd_ssi_quit,
437 .start = rsnd_ssi_pio_start,
438 .stop = rsnd_ssi_pio_stop,
441 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
442 struct rsnd_dai *rdai)
444 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
445 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
446 struct device *dev = rsnd_priv_to_dev(priv);
447 int dma_id = ssi->info->dma_id;
451 priv, rsnd_mod_to_dma(mod),
452 rsnd_info_is_playback(priv, ssi),
456 dev_err(dev, "%s[%d] (DMA) is failed\n",
457 rsnd_mod_name(mod), rsnd_mod_id(mod));
459 dev_dbg(dev, "%s[%d] (DMA) is probed\n",
460 rsnd_mod_name(mod), rsnd_mod_id(mod));
465 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
466 struct rsnd_dai *rdai)
468 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
473 static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
474 struct rsnd_dai *rdai)
476 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
477 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
478 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
480 /* enable DMA transfer */
483 rsnd_src_ssiu_start(mod, rdai, rsnd_ssi_use_busif(mod));
487 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
489 /* enable WS continue */
490 if (rsnd_dai_is_clk_master(rdai))
491 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
496 static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
497 struct rsnd_dai *rdai)
499 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
500 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
504 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
506 rsnd_ssi_hw_stop(ssi, rdai);
510 rsnd_src_ssiu_stop(mod, rdai, 1);
515 static char *rsnd_ssi_dma_name(struct rsnd_mod *mod)
517 return rsnd_ssi_use_busif(mod) ? "ssiu" : SSI_NAME;
520 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
522 .dma_name = rsnd_ssi_dma_name,
523 .probe = rsnd_ssi_dma_probe,
524 .remove = rsnd_ssi_dma_remove,
525 .init = rsnd_ssi_init,
526 .quit = rsnd_ssi_quit,
527 .start = rsnd_ssi_dma_start,
528 .stop = rsnd_ssi_dma_stop,
534 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
541 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
543 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
546 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
549 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
551 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
553 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
556 static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
558 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
561 switch (rsnd_mod_id(&ssi->mod)) {
564 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
567 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
570 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
576 static void rsnd_of_parse_ssi(struct platform_device *pdev,
577 const struct rsnd_of_data *of_data,
578 struct rsnd_priv *priv)
580 struct device_node *node;
581 struct device_node *np;
582 struct rsnd_ssi_platform_info *ssi_info;
583 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
584 struct device *dev = &pdev->dev;
590 node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
594 nr = of_get_child_count(node);
596 goto rsnd_of_parse_ssi_end;
598 ssi_info = devm_kzalloc(dev,
599 sizeof(struct rsnd_ssi_platform_info) * nr,
602 dev_err(dev, "ssi info allocation error\n");
603 goto rsnd_of_parse_ssi_end;
606 info->ssi_info = ssi_info;
607 info->ssi_info_nr = nr;
610 for_each_child_of_node(node, np) {
613 ssi_info = info->ssi_info + i;
618 if (of_get_property(np, "shared-pin", NULL))
619 ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
624 ssi_info->pio_irq = irq_of_parse_and_map(np, 0);
629 ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
632 if (of_get_property(np, "no-busif", NULL))
633 ssi_info->flags |= RSND_SSI_NO_BUSIF;
636 rsnd_of_parse_ssi_end:
640 int rsnd_ssi_probe(struct platform_device *pdev,
641 const struct rsnd_of_data *of_data,
642 struct rsnd_priv *priv)
644 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
645 struct rsnd_ssi_platform_info *pinfo;
646 struct device *dev = rsnd_priv_to_dev(priv);
647 struct rsnd_mod_ops *ops;
649 struct rsnd_ssi *ssi;
650 char name[RSND_SSI_NAME_SIZE];
653 rsnd_of_parse_ssi(pdev, of_data, priv);
658 nr = info->ssi_info_nr;
659 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
661 dev_err(dev, "SSI allocate failed\n");
668 for_each_rsnd_ssi(ssi, priv, i) {
669 pinfo = &info->ssi_info[i];
671 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
674 clk = devm_clk_get(dev, name);
681 ops = &rsnd_ssi_non_ops;
682 if (pinfo->dma_id > 0)
683 ops = &rsnd_ssi_dma_ops;
684 else if (rsnd_ssi_pio_available(ssi))
685 ops = &rsnd_ssi_pio_ops;
687 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
689 rsnd_ssi_parent_clk_setup(priv, ssi);