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, "ssi%d outputs %u Hz\n",
163 rsnd_mod_id(&ssi->mod), rate);
170 dev_err(dev, "unsupported clock rate\n");
174 static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
178 rsnd_adg_ssi_clk_stop(&ssi->mod);
181 static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
182 struct rsnd_dai *rdai,
183 struct rsnd_dai_stream *io)
185 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
186 struct device *dev = rsnd_priv_to_dev(priv);
189 if (0 == ssi->usrcnt) {
190 clk_prepare_enable(ssi->clk);
192 if (rsnd_dai_is_clk_master(rdai)) {
193 if (rsnd_ssi_clk_from_parent(ssi))
194 rsnd_ssi_hw_start(ssi->parent, rdai, io);
196 rsnd_ssi_master_clk_start(ssi, io);
205 rsnd_mod_write(&ssi->mod, SSICR, cr);
209 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
212 static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
213 struct rsnd_dai *rdai)
215 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
216 struct device *dev = rsnd_priv_to_dev(priv);
219 if (0 == ssi->usrcnt) /* stop might be called without start */
224 if (0 == ssi->usrcnt) {
227 * and, wait all data was sent
232 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
233 rsnd_ssi_status_check(&ssi->mod, DIRQ);
237 * and, wait idle state
239 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
240 rsnd_ssi_status_check(&ssi->mod, IIRQ);
242 if (rsnd_dai_is_clk_master(rdai)) {
243 if (rsnd_ssi_clk_from_parent(ssi))
244 rsnd_ssi_hw_stop(ssi->parent, rdai);
246 rsnd_ssi_master_clk_stop(ssi);
249 clk_disable_unprepare(ssi->clk);
252 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
256 * SSI mod common functions
258 static int rsnd_ssi_init(struct rsnd_mod *mod,
259 struct rsnd_dai *rdai)
261 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
262 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
263 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
269 * always use 32bit system word for easy clock calculation.
270 * see also rsnd_ssi_master_clk_enable()
275 * init clock settings for SSICR
277 switch (runtime->sample_bits) {
288 if (rdai->bit_clk_inv)
290 if (rdai->frm_clk_inv)
292 if (rdai->data_alignment)
296 if (rsnd_dai_is_play(rdai, io))
304 ssi->err = -1; /* ignore 1st error */
309 static int rsnd_ssi_quit(struct rsnd_mod *mod,
310 struct rsnd_dai *rdai)
312 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
313 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
314 struct device *dev = rsnd_priv_to_dev(priv);
317 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
326 static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
328 /* under/over flow error */
329 if (status & (UIRQ | OIRQ)) {
332 /* clear error status */
333 rsnd_mod_write(&ssi->mod, SSISR, 0);
340 static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
342 struct rsnd_ssi *ssi = data;
343 struct rsnd_mod *mod = &ssi->mod;
344 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
345 u32 status = rsnd_mod_read(mod, SSISR);
346 irqreturn_t ret = IRQ_NONE;
348 if (io && (status & DIRQ)) {
349 struct rsnd_dai *rdai = ssi->rdai;
350 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
351 u32 *buf = (u32 *)(runtime->dma_area +
352 rsnd_dai_pointer_offset(io, 0));
354 rsnd_ssi_record_error(ssi, status);
357 * 8/16/32 data can be assesse to TDR/RDR register
358 * directly as 32bit data
359 * see rsnd_ssi_init()
361 if (rsnd_dai_is_play(rdai, io))
362 rsnd_mod_write(mod, SSITDR, *buf);
364 *buf = rsnd_mod_read(mod, SSIRDR);
366 rsnd_dai_pointer_update(io, sizeof(*buf));
374 static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
375 struct rsnd_dai *rdai)
377 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
378 struct device *dev = rsnd_priv_to_dev(priv);
379 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
380 int irq = ssi->info->pio_irq;
383 ret = devm_request_irq(dev, irq,
384 rsnd_ssi_pio_interrupt,
388 dev_err(dev, "SSI request interrupt failed\n");
390 dev_dbg(dev, "%s (PIO) is probed\n", rsnd_mod_name(mod));
395 static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
396 struct rsnd_dai *rdai)
398 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
399 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
402 ssi->cr_etc = UIEN | OIEN | DIEN;
404 rsnd_src_ssiu_start(mod, rdai, 0);
406 rsnd_src_enable_ssi_irq(mod, rdai);
408 rsnd_ssi_hw_start(ssi, rdai, io);
413 static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
414 struct rsnd_dai *rdai)
416 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
420 rsnd_ssi_hw_stop(ssi, rdai);
422 rsnd_src_ssiu_stop(mod, rdai, 0);
427 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
429 .probe = rsnd_ssi_pio_probe,
430 .init = rsnd_ssi_init,
431 .quit = rsnd_ssi_quit,
432 .start = rsnd_ssi_pio_start,
433 .stop = rsnd_ssi_pio_stop,
436 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
437 struct rsnd_dai *rdai)
439 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
440 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
441 struct device *dev = rsnd_priv_to_dev(priv);
442 int dma_id = ssi->info->dma_id;
446 priv, rsnd_mod_to_dma(mod),
447 rsnd_info_is_playback(priv, ssi),
451 dev_err(dev, "SSI DMA failed\n");
453 dev_dbg(dev, "%s (DMA) is probed\n", rsnd_mod_name(mod));
458 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
459 struct rsnd_dai *rdai)
461 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
466 static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
467 struct rsnd_dai *rdai)
469 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
470 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
471 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
473 /* enable DMA transfer */
476 rsnd_src_ssiu_start(mod, rdai, rsnd_ssi_use_busif(mod));
480 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
482 /* enable WS continue */
483 if (rsnd_dai_is_clk_master(rdai))
484 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
489 static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
490 struct rsnd_dai *rdai)
492 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
493 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
497 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
499 rsnd_ssi_hw_stop(ssi, rdai);
503 rsnd_src_ssiu_stop(mod, rdai, 1);
508 static char *rsnd_ssi_dma_name(struct rsnd_mod *mod)
510 return rsnd_ssi_use_busif(mod) ? "ssiu" : SSI_NAME;
513 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
515 .dma_name = rsnd_ssi_dma_name,
516 .probe = rsnd_ssi_dma_probe,
517 .remove = rsnd_ssi_dma_remove,
518 .init = rsnd_ssi_init,
519 .quit = rsnd_ssi_quit,
520 .start = rsnd_ssi_dma_start,
521 .stop = rsnd_ssi_dma_stop,
527 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
534 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
536 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
539 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
542 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
544 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
546 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
549 static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
551 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
554 switch (rsnd_mod_id(&ssi->mod)) {
557 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
560 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
563 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
569 static void rsnd_of_parse_ssi(struct platform_device *pdev,
570 const struct rsnd_of_data *of_data,
571 struct rsnd_priv *priv)
573 struct device_node *node;
574 struct device_node *np;
575 struct rsnd_ssi_platform_info *ssi_info;
576 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
577 struct device *dev = &pdev->dev;
583 node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
587 nr = of_get_child_count(node);
589 goto rsnd_of_parse_ssi_end;
591 ssi_info = devm_kzalloc(dev,
592 sizeof(struct rsnd_ssi_platform_info) * nr,
595 dev_err(dev, "ssi info allocation error\n");
596 goto rsnd_of_parse_ssi_end;
599 info->ssi_info = ssi_info;
600 info->ssi_info_nr = nr;
603 for_each_child_of_node(node, np) {
606 ssi_info = info->ssi_info + i;
611 if (of_get_property(np, "shared-pin", NULL))
612 ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
617 ssi_info->pio_irq = irq_of_parse_and_map(np, 0);
622 ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
625 if (of_get_property(np, "no-busif", NULL))
626 ssi_info->flags |= RSND_SSI_NO_BUSIF;
629 rsnd_of_parse_ssi_end:
633 int rsnd_ssi_probe(struct platform_device *pdev,
634 const struct rsnd_of_data *of_data,
635 struct rsnd_priv *priv)
637 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
638 struct rsnd_ssi_platform_info *pinfo;
639 struct device *dev = rsnd_priv_to_dev(priv);
640 struct rsnd_mod_ops *ops;
642 struct rsnd_ssi *ssi;
643 char name[RSND_SSI_NAME_SIZE];
646 rsnd_of_parse_ssi(pdev, of_data, priv);
651 nr = info->ssi_info_nr;
652 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
654 dev_err(dev, "SSI allocate failed\n");
661 for_each_rsnd_ssi(ssi, priv, i) {
662 pinfo = &info->ssi_info[i];
664 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
667 clk = devm_clk_get(dev, name);
674 ops = &rsnd_ssi_non_ops;
675 if (pinfo->dma_id > 0)
676 ops = &rsnd_ssi_dma_ops;
677 else if (rsnd_ssi_pio_available(ssi))
678 ops = &rsnd_ssi_pio_ops;
680 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
682 rsnd_ssi_parent_clk_setup(priv, ssi);