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 */
62 struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
63 struct rsnd_ssi *parent;
66 struct rsnd_dai *rdai;
75 #define for_each_rsnd_ssi(pos, priv, i) \
77 (i < rsnd_ssi_nr(priv)) && \
78 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
81 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
82 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
83 #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))
87 #define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent)
88 #define rsnd_ssi_mode_flags(p) ((p)->info->flags)
89 #define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
91 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
94 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
95 struct device *dev = rsnd_priv_to_dev(priv);
99 for (i = 0; i < 1024; i++) {
100 status = rsnd_mod_read(mod, SSISR);
107 dev_warn(dev, "status check failed\n");
110 static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
111 struct rsnd_dai_stream *io)
113 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
114 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
115 struct device *dev = rsnd_priv_to_dev(priv);
117 int adg_clk_div_table[] = {
118 1, 6, /* see adg.c */
120 int ssi_clk_mul_table[] = {
121 1, 2, 4, 8, 16, 6, 12,
123 unsigned int main_rate;
124 unsigned int rate = rsnd_scu_get_ssi_rate(priv, io, runtime);
127 * Find best clock, and try to start ADG
129 for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
130 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
133 * this driver is assuming that
134 * system word is 64fs (= 2 x 32bit)
135 * see rsnd_ssi_init()
137 main_rate = rate / adg_clk_div_table[i]
138 * 32 * 2 * ssi_clk_mul_table[j];
140 ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
143 ssi->cr_clk = FORCE | SWL_32 |
144 SCKD | SWSD | CKDV(j);
146 dev_dbg(dev, "ssi%d outputs %u Hz\n",
147 rsnd_mod_id(&ssi->mod), rate);
154 dev_err(dev, "unsupported clock rate\n");
158 static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
162 rsnd_adg_ssi_clk_stop(&ssi->mod);
165 static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
166 struct rsnd_dai *rdai,
167 struct rsnd_dai_stream *io)
169 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
170 struct device *dev = rsnd_priv_to_dev(priv);
173 if (0 == ssi->usrcnt) {
174 clk_enable(ssi->clk);
176 if (rsnd_dai_is_clk_master(rdai)) {
177 if (rsnd_ssi_clk_from_parent(ssi))
178 rsnd_ssi_hw_start(ssi->parent, rdai, io);
180 rsnd_ssi_master_clk_start(ssi, io);
189 rsnd_mod_write(&ssi->mod, SSICR, cr);
193 dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod));
196 static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi,
197 struct rsnd_dai *rdai)
199 struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
200 struct device *dev = rsnd_priv_to_dev(priv);
203 if (0 == ssi->usrcnt) /* stop might be called without start */
208 if (0 == ssi->usrcnt) {
211 * and, wait all data was sent
216 rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
217 rsnd_ssi_status_check(&ssi->mod, DIRQ);
221 * and, wait idle state
223 rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */
224 rsnd_ssi_status_check(&ssi->mod, IIRQ);
226 if (rsnd_dai_is_clk_master(rdai)) {
227 if (rsnd_ssi_clk_from_parent(ssi))
228 rsnd_ssi_hw_stop(ssi->parent, rdai);
230 rsnd_ssi_master_clk_stop(ssi);
233 clk_disable(ssi->clk);
236 dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod));
240 * SSI mod common functions
242 static int rsnd_ssi_init(struct rsnd_mod *mod,
243 struct rsnd_dai *rdai,
244 struct rsnd_dai_stream *io)
246 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
247 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
253 * always use 32bit system word for easy clock calculation.
254 * see also rsnd_ssi_master_clk_enable()
259 * init clock settings for SSICR
261 switch (runtime->sample_bits) {
272 if (rdai->bit_clk_inv)
274 if (rdai->frm_clk_inv)
276 if (rdai->data_alignment)
280 if (rsnd_dai_is_play(rdai, io))
288 ssi->err = -1; /* ignore 1st error */
293 static int rsnd_ssi_quit(struct rsnd_mod *mod,
294 struct rsnd_dai *rdai,
295 struct rsnd_dai_stream *io)
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);
302 dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err);
311 static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
313 /* under/over flow error */
314 if (status & (UIRQ | OIRQ)) {
317 /* clear error status */
318 rsnd_mod_write(&ssi->mod, SSISR, 0);
325 static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data)
327 struct rsnd_ssi *ssi = data;
328 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);
331 irqreturn_t ret = IRQ_NONE;
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));
339 rsnd_ssi_record_error(ssi, status);
342 * 8/16/32 data can be assesse to TDR/RDR register
343 * directly as 32bit data
344 * see rsnd_ssi_init()
346 if (rsnd_dai_is_play(rdai, io))
347 rsnd_mod_write(mod, SSITDR, *buf);
349 *buf = rsnd_mod_read(mod, SSIRDR);
351 rsnd_dai_pointer_update(io, sizeof(*buf));
359 static int rsnd_ssi_pio_start(struct rsnd_mod *mod,
360 struct rsnd_dai *rdai,
361 struct rsnd_dai_stream *io)
363 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
366 ssi->cr_etc = UIEN | OIEN | DIEN;
368 rsnd_ssi_hw_start(ssi, rdai, io);
373 static int rsnd_ssi_pio_stop(struct rsnd_mod *mod,
374 struct rsnd_dai *rdai,
375 struct rsnd_dai_stream *io)
377 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
381 rsnd_ssi_hw_stop(ssi, rdai);
386 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
388 .init = rsnd_ssi_init,
389 .quit = rsnd_ssi_quit,
390 .start = rsnd_ssi_pio_start,
391 .stop = rsnd_ssi_pio_stop,
394 static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
395 struct rsnd_dai *rdai,
396 struct rsnd_dai_stream *io)
398 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
399 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
401 /* enable DMA transfer */
406 rsnd_ssi_hw_start(ssi, ssi->rdai, io);
408 /* enable WS continue */
409 if (rsnd_dai_is_clk_master(rdai))
410 rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
415 static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
416 struct rsnd_dai *rdai,
417 struct rsnd_dai_stream *io)
419 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
420 struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod);
424 rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
426 rsnd_ssi_hw_stop(ssi, rdai);
433 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
435 .init = rsnd_ssi_init,
436 .quit = rsnd_ssi_quit,
437 .start = rsnd_ssi_dma_start,
438 .stop = rsnd_ssi_dma_stop,
444 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
451 struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv,
452 int dai_id, int is_play)
454 struct rsnd_ssi *ssi;
459 for_each_rsnd_ssi(ssi, priv, i) {
460 if (rsnd_ssi_dai_id(ssi) != dai_id)
463 has_play = rsnd_ssi_is_play(&ssi->mod);
465 if (is_play == has_play)
472 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
474 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
477 return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
480 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
482 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
484 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
487 int rsnd_ssi_is_play(struct rsnd_mod *mod)
489 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
491 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY);
494 static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
496 if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
499 switch (rsnd_mod_id(&ssi->mod)) {
502 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
505 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
508 ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
513 int rsnd_ssi_probe(struct platform_device *pdev,
514 struct rsnd_priv *priv)
516 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
517 struct rsnd_ssi_platform_info *pinfo;
518 struct device *dev = rsnd_priv_to_dev(priv);
519 struct rsnd_mod_ops *ops;
521 struct rsnd_ssi *ssi;
522 char name[RSND_SSI_NAME_SIZE];
528 nr = info->ssi_info_nr;
529 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
531 dev_err(dev, "SSI allocate failed\n");
538 for_each_rsnd_ssi(ssi, priv, i) {
539 pinfo = &info->ssi_info[i];
541 snprintf(name, RSND_SSI_NAME_SIZE, "ssi.%d", i);
543 clk = devm_clk_get(dev, name);
550 ops = &rsnd_ssi_non_ops;
555 if (pinfo->dma_id > 0) {
557 priv, rsnd_mod_to_dma(&ssi->mod),
558 rsnd_ssi_is_play(&ssi->mod),
561 dev_info(dev, "SSI DMA failed. try PIO transter\n");
563 ops = &rsnd_ssi_dma_ops;
565 dev_dbg(dev, "SSI%d use DMA transfer\n", i);
571 if (!rsnd_ssi_dma_available(ssi) &&
572 rsnd_ssi_pio_available(ssi)) {
573 ret = devm_request_irq(dev, pinfo->pio_irq,
574 &rsnd_ssi_pio_interrupt,
578 dev_err(dev, "SSI request interrupt failed\n");
582 ops = &rsnd_ssi_pio_ops;
584 dev_dbg(dev, "SSI%d use PIO transfer\n", i);
587 rsnd_mod_init(priv, &ssi->mod, ops, RSND_MOD_SSI, i);
589 rsnd_ssi_parent_clk_setup(priv, ssi);
595 void rsnd_ssi_remove(struct platform_device *pdev,
596 struct rsnd_priv *priv)
598 struct rsnd_ssi *ssi;
601 for_each_rsnd_ssi(ssi, priv, i) {
602 if (rsnd_ssi_dma_available(ssi))
603 rsnd_dma_quit(priv, rsnd_mod_to_dma(&ssi->mod));