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 void rsnd_ssi_status_check(struct rsnd_mod *mod,
96 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
97 struct device *dev = rsnd_priv_to_dev(priv);
101 for (i = 0; i < 1024; i++) {
102 status = rsnd_mod_read(mod, SSISR);
109 dev_warn(dev, "status check failed\n");
112 static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
113 struct rsnd_dai_stream *io)
115 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
116 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
117 struct device *dev = rsnd_priv_to_dev(priv);
119 int adg_clk_div_table[] = {
120 1, 6, /* see adg.c */
122 int ssi_clk_mul_table[] = {
123 1, 2, 4, 8, 16, 6, 12,
125 unsigned int main_rate;
126 unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
129 * Find best clock, and try to start ADG
131 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
132 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
135 * this driver is assuming that
136 * system word is 64fs (= 2 x 32bit)
137 * see rsnd_ssi_init()
139 main_rate = rate / adg_clk_div_table[i]
140 * 32 * 2 * ssi_clk_mul_table[j];
142 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
145 ssi->cr_clk = FORCE | SWL_32 |
146 SCKD | SWSD | CKDV(j);
148 dev_dbg(dev, "ssi%d outputs %u Hz\n",
149 rsnd_mod_id(&ssi->mod), rate);
156 dev_err(dev, "unsupported clock rate\n");
160 static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
164 rsnd_adg_ssi_clk_stop(&ssi->mod);
167 static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
168 struct rsnd_dai *rdai,
169 struct rsnd_dai_stream *io)
171 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
172 struct device *dev = rsnd_priv_to_dev(priv);
175 if (0 == ssi->usrcnt) {
176 clk_prepare_enable(ssi->clk);
178 if (rsnd_dai_is_clk_master(rdai)) {
179 if (rsnd_ssi_clk_from_parent(ssi))
180 rsnd_ssi_hw_start(ssi->parent, rdai, io);
182 rsnd_ssi_master_clk_start(ssi, io);
191 rsnd_mod_write(&ssi->mod, SSICR, cr);
195 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
198 static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
199 struct rsnd_dai *rdai)
201 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
202 struct device *dev = rsnd_priv_to_dev(priv);
205 if (0 == ssi->usrcnt) /* stop might be called without start */
210 if (0 == ssi->usrcnt) {
213 * and, wait all data was sent
218 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
219 rsnd_ssi_status_check(&ssi->mod, DIRQ);
223 * and, wait idle state
225 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
226 rsnd_ssi_status_check(&ssi->mod, IIRQ);
228 if (rsnd_dai_is_clk_master(rdai)) {
229 if (rsnd_ssi_clk_from_parent(ssi))
230 rsnd_ssi_hw_stop(ssi->parent, rdai);
232 rsnd_ssi_master_clk_stop(ssi);
235 clk_disable_unprepare(ssi->clk);
238 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
242 * SSI mod common functions
244 static int rsnd_ssi_init(struct rsnd_mod *mod,
245 struct rsnd_dai *rdai)
247 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
248 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
249 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
255 * always use 32bit system word for easy clock calculation.
256 * see also rsnd_ssi_master_clk_enable()
261 * init clock settings for SSICR
263 switch (runtime->sample_bits) {
274 if (rdai->bit_clk_inv)
276 if (rdai->frm_clk_inv)
278 if (rdai->data_alignment)
282 if (rsnd_dai_is_play(rdai, io))
290 ssi->err = -1; /* ignore 1st error */
292 rsnd_src_ssi_mode_init(mod, rdai);
297 static int rsnd_ssi_quit(struct rsnd_mod *mod,
298 struct rsnd_dai *rdai)
300 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
301 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
302 struct device *dev = rsnd_priv_to_dev(priv);
305 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
314 static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
316 /* under/over flow error */
317 if (status & (UIRQ | OIRQ)) {
320 /* clear error status */
321 rsnd_mod_write(&ssi->mod, SSISR, 0);
328 static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
330 struct rsnd_ssi *ssi = data;
331 struct rsnd_mod *mod = &ssi->mod;
332 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
333 u32 status = rsnd_mod_read(mod, SSISR);
334 irqreturn_t ret = IRQ_NONE;
336 if (io && (status & DIRQ)) {
337 struct rsnd_dai *rdai = ssi->rdai;
338 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
339 u32 *buf = (u32 *)(runtime->dma_area +
340 rsnd_dai_pointer_offset(io, 0));
342 rsnd_ssi_record_error(ssi, status);
345 * 8/16/32 data can be assesse to TDR/RDR register
346 * directly as 32bit data
347 * see rsnd_ssi_init()
349 if (rsnd_dai_is_play(rdai, io))
350 rsnd_mod_write(mod, SSITDR, *buf);
352 *buf = rsnd_mod_read(mod, SSIRDR);
354 rsnd_dai_pointer_update(io, sizeof(*buf));
362 static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
363 struct rsnd_dai *rdai)
365 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
366 struct device *dev = rsnd_priv_to_dev(priv);
367 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
368 int irq = ssi->info->pio_irq;
371 ret = devm_request_irq(dev, irq,
372 rsnd_ssi_pio_interrupt,
376 dev_err(dev, "SSI request interrupt failed\n");
378 dev_dbg(dev, "%s (PIO) is probed\n", rsnd_mod_name(mod));
383 static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
384 struct rsnd_dai *rdai)
386 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
387 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
390 ssi->cr_etc = UIEN | OIEN | DIEN;
392 rsnd_src_enable_ssi_irq(mod, rdai);
394 rsnd_ssi_hw_start(ssi, rdai, io);
399 static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
400 struct rsnd_dai *rdai)
402 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
406 rsnd_ssi_hw_stop(ssi, rdai);
411 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
413 .probe = rsnd_ssi_pio_probe,
414 .init = rsnd_ssi_init,
415 .quit = rsnd_ssi_quit,
416 .start = rsnd_ssi_pio_start,
417 .stop = rsnd_ssi_pio_stop,
420 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
421 struct rsnd_dai *rdai)
423 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
424 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
425 struct device *dev = rsnd_priv_to_dev(priv);
426 int dma_id = ssi->info->dma_id;
430 priv, rsnd_mod_to_dma(mod),
431 rsnd_info_is_playback(priv, ssi),
435 dev_err(dev, "SSI DMA failed\n");
437 dev_dbg(dev, "%s (DMA) is probed\n", rsnd_mod_name(mod));
442 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
443 struct rsnd_dai *rdai)
445 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
450 static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
451 struct rsnd_dai *rdai)
453 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
454 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
455 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
457 /* enable DMA transfer */
462 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
464 /* enable WS continue */
465 if (rsnd_dai_is_clk_master(rdai))
466 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
471 static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
472 struct rsnd_dai *rdai)
474 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
475 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
479 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
481 rsnd_ssi_hw_stop(ssi, rdai);
488 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
490 .probe = rsnd_ssi_dma_probe,
491 .remove = rsnd_ssi_dma_remove,
492 .init = rsnd_ssi_init,
493 .quit = rsnd_ssi_quit,
494 .start = rsnd_ssi_dma_start,
495 .stop = rsnd_ssi_dma_stop,
501 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
508 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
510 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
513 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
516 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
518 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
520 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
523 static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
525 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
528 switch (rsnd_mod_id(&ssi->mod)) {
531 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
534 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
537 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
543 static void rsnd_of_parse_ssi(struct platform_device *pdev,
544 const struct rsnd_of_data *of_data,
545 struct rsnd_priv *priv)
547 struct device_node *node;
548 struct device_node *np;
549 struct rsnd_ssi_platform_info *ssi_info;
550 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
551 struct device *dev = &pdev->dev;
557 node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
561 nr = of_get_child_count(node);
563 goto rsnd_of_parse_ssi_end;
565 ssi_info = devm_kzalloc(dev,
566 sizeof(struct rsnd_ssi_platform_info) * nr,
569 dev_err(dev, "ssi info allocation error\n");
570 goto rsnd_of_parse_ssi_end;
573 info->ssi_info = ssi_info;
574 info->ssi_info_nr = nr;
577 for_each_child_of_node(node, np) {
580 ssi_info = info->ssi_info + i;
585 if (of_get_property(np, "shared-pin", NULL))
586 ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
591 ssi_info->pio_irq = irq_of_parse_and_map(np, 0);
596 ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
600 rsnd_of_parse_ssi_end:
604 int rsnd_ssi_probe(struct platform_device *pdev,
605 const struct rsnd_of_data *of_data,
606 struct rsnd_priv *priv)
608 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
609 struct rsnd_ssi_platform_info *pinfo;
610 struct device *dev = rsnd_priv_to_dev(priv);
611 struct rsnd_mod_ops *ops;
613 struct rsnd_ssi *ssi;
614 char name[RSND_SSI_NAME_SIZE];
617 rsnd_of_parse_ssi(pdev, of_data, priv);
622 nr = info->ssi_info_nr;
623 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
625 dev_err(dev, "SSI allocate failed\n");
632 for_each_rsnd_ssi(ssi, priv, i) {
633 pinfo = &info->ssi_info[i];
635 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
638 clk = devm_clk_get(dev, name);
645 ops = &rsnd_ssi_non_ops;
646 if (pinfo->dma_id > 0)
647 ops = &rsnd_ssi_dma_ops;
648 else if (rsnd_ssi_pio_available(ssi))
649 ops = &rsnd_ssi_pio_ops;
651 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
653 rsnd_ssi_parent_clk_setup(priv, ssi);