linux/sound/soc/sh/rcar/ssi.c
<<
>>
Prefs
   1/*
   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 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 */
  27#define CHNL_4          (1 << 22)       /* Channels */
  28#define CHNL_6          (2 << 22)       /* Channels */
  29#define CHNL_8          (3 << 22)       /* Channels */
  30#define DWL_8           (0 << 19)       /* Data Word Length */
  31#define DWL_16          (1 << 19)       /* Data Word Length */
  32#define DWL_18          (2 << 19)       /* Data Word Length */
  33#define DWL_20          (3 << 19)       /* Data Word Length */
  34#define DWL_22          (4 << 19)       /* Data Word Length */
  35#define DWL_24          (5 << 19)       /* Data Word Length */
  36#define DWL_32          (6 << 19)       /* Data Word Length */
  37
  38#define SWL_32          (3 << 16)       /* R/W System Word Length */
  39#define SCKD            (1 << 15)       /* Serial Bit Clock Direction */
  40#define SWSD            (1 << 14)       /* Serial WS Direction */
  41#define SCKP            (1 << 13)       /* Serial Bit Clock Polarity */
  42#define SWSP            (1 << 12)       /* Serial WS Polarity */
  43#define SDTA            (1 << 10)       /* Serial Data Alignment */
  44#define PDTA            (1 <<  9)       /* Parallel Data Alignment */
  45#define DEL             (1 <<  8)       /* Serial Data Delay */
  46#define CKDV(v)         (v <<  4)       /* Serial Clock Division Ratio */
  47#define TRMD            (1 <<  1)       /* Transmit/Receive Mode Select */
  48#define EN              (1 <<  0)       /* SSI Module Enable */
  49
  50/*
  51 * SSISR
  52 */
  53#define UIRQ            (1 << 27)       /* Underflow Error Interrupt Status */
  54#define OIRQ            (1 << 26)       /* Overflow Error Interrupt Status */
  55#define IIRQ            (1 << 25)       /* Idle Mode Interrupt Status */
  56#define DIRQ            (1 << 24)       /* Data Interrupt Status Flag */
  57
  58/*
  59 * SSIWSR
  60 */
  61#define CONT            (1 << 8)        /* WS Continue Function */
  62#define WS_MODE         (1 << 0)        /* WS Mode */
  63
  64#define SSI_NAME "ssi"
  65
  66struct rsnd_ssi {
  67        struct rsnd_mod mod;
  68        struct rsnd_mod *dma;
  69
  70        u32 flags;
  71        u32 cr_own;
  72        u32 cr_clk;
  73        u32 cr_mode;
  74        u32 wsr;
  75        int chan;
  76        int rate;
  77        int irq;
  78        unsigned int usrcnt;
  79};
  80
  81/* flags */
  82#define RSND_SSI_CLK_PIN_SHARE          (1 << 0)
  83#define RSND_SSI_NO_BUSIF               (1 << 1) /* SSI+DMA without BUSIF */
  84
  85#define for_each_rsnd_ssi(pos, priv, i)                                 \
  86        for (i = 0;                                                     \
  87             (i < rsnd_ssi_nr(priv)) &&                                 \
  88                ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i));         \
  89             i++)
  90
  91#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
  92#define rsnd_ssi_to_dma(mod) ((ssi)->dma)
  93#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
  94#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
  95#define rsnd_ssi_mode_flags(p) ((p)->flags)
  96#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
  97#define rsnd_ssi_is_multi_slave(mod, io) \
  98        (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
  99#define rsnd_ssi_is_run_mods(mod, io) \
 100        (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
 101
 102int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
 103{
 104        struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
 105        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 106        int use_busif = 0;
 107
 108        if (!rsnd_ssi_is_dma_mode(mod))
 109                return 0;
 110
 111        if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
 112                use_busif = 1;
 113        if (rsnd_io_to_mod_src(io))
 114                use_busif = 1;
 115
 116        return use_busif;
 117}
 118
 119static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
 120{
 121        rsnd_mod_write(mod, SSISR, 0);
 122}
 123
 124static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
 125{
 126        return rsnd_mod_read(mod, SSISR);
 127}
 128
 129static void rsnd_ssi_status_check(struct rsnd_mod *mod,
 130                                  u32 bit)
 131{
 132        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 133        struct device *dev = rsnd_priv_to_dev(priv);
 134        u32 status;
 135        int i;
 136
 137        for (i = 0; i < 1024; i++) {
 138                status = rsnd_ssi_status_get(mod);
 139                if (status & bit)
 140                        return;
 141
 142                udelay(50);
 143        }
 144
 145        dev_warn(dev, "%s[%d] status check failed\n",
 146                 rsnd_mod_name(mod), rsnd_mod_id(mod));
 147}
 148
 149static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
 150{
 151        struct rsnd_mod *mod;
 152        enum rsnd_mod_type types[] = {
 153                RSND_MOD_SSIM1,
 154                RSND_MOD_SSIM2,
 155                RSND_MOD_SSIM3,
 156        };
 157        int i, mask;
 158
 159        mask = 0;
 160        for (i = 0; i < ARRAY_SIZE(types); i++) {
 161                mod = rsnd_io_to_mod(io, types[i]);
 162                if (!mod)
 163                        continue;
 164
 165                mask |= 1 << rsnd_mod_id(mod);
 166        }
 167
 168        return mask;
 169}
 170
 171static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
 172{
 173        struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
 174        struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
 175
 176        return rsnd_ssi_multi_slaves_runtime(io) |
 177                1 << rsnd_mod_id(ssi_mod) |
 178                1 << rsnd_mod_id(ssi_parent_mod);
 179}
 180
 181u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
 182{
 183        if (rsnd_runtime_is_ssi_multi(io))
 184                return rsnd_ssi_multi_slaves(io);
 185
 186        return 0;
 187}
 188
 189static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
 190                                     struct rsnd_dai_stream *io)
 191{
 192        struct rsnd_priv *priv = rsnd_io_to_priv(io);
 193        struct device *dev = rsnd_priv_to_dev(priv);
 194        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 195        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 196        struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
 197        int chan = rsnd_runtime_channel_for_ssi(io);
 198        int j, ret;
 199        int ssi_clk_mul_table[] = {
 200                1, 2, 4, 8, 16, 6, 12,
 201        };
 202        unsigned int main_rate;
 203        unsigned int rate = rsnd_io_is_play(io) ?
 204                rsnd_src_get_out_rate(priv, io) :
 205                rsnd_src_get_in_rate(priv, io);
 206
 207        if (!rsnd_rdai_is_clk_master(rdai))
 208                return 0;
 209
 210        if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
 211                return 0;
 212
 213        if (rsnd_ssi_is_multi_slave(mod, io))
 214                return 0;
 215
 216        if (ssi->usrcnt > 1) {
 217                if (ssi->rate != rate) {
 218                        dev_err(dev, "SSI parent/child should use same rate\n");
 219                        return -EINVAL;
 220                }
 221
 222                return 0;
 223        }
 224
 225        /*
 226         * Find best clock, and try to start ADG
 227         */
 228        for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
 229
 230                /*
 231                 * this driver is assuming that
 232                 * system word is 32bit x chan
 233                 * see rsnd_ssi_init()
 234                 */
 235                main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
 236
 237                ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
 238                if (0 == ret) {
 239                        ssi->cr_clk     = FORCE | SWL_32 |
 240                                SCKD | SWSD | CKDV(j);
 241                        ssi->wsr = CONT;
 242
 243                        ssi->rate = rate;
 244
 245                        dev_dbg(dev, "%s[%d] outputs %u Hz\n",
 246                                rsnd_mod_name(mod),
 247                                rsnd_mod_id(mod), rate);
 248
 249                        return 0;
 250                }
 251        }
 252
 253        dev_err(dev, "unsupported clock rate\n");
 254        return -EIO;
 255}
 256
 257static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
 258                                     struct rsnd_dai_stream *io)
 259{
 260        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 261        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 262        struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
 263
 264        if (!rsnd_rdai_is_clk_master(rdai))
 265                return;
 266
 267        if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
 268                return;
 269
 270        if (ssi->usrcnt > 1)
 271                return;
 272
 273        ssi->cr_clk     = 0;
 274        ssi->rate       = 0;
 275
 276        rsnd_adg_ssi_clk_stop(mod);
 277}
 278
 279static void rsnd_ssi_config_init(struct rsnd_mod *mod,
 280                                struct rsnd_dai_stream *io)
 281{
 282        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 283        struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
 284        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 285        u32 cr_own;
 286        u32 cr_mode;
 287        u32 wsr;
 288        int is_tdm;
 289
 290        is_tdm = rsnd_runtime_is_ssi_tdm(io);
 291
 292        /*
 293         * always use 32bit system word.
 294         * see also rsnd_ssi_master_clk_enable()
 295         */
 296        cr_own = FORCE | SWL_32 | PDTA;
 297
 298        if (rdai->bit_clk_inv)
 299                cr_own |= SCKP;
 300        if (rdai->frm_clk_inv ^ is_tdm)
 301                cr_own |= SWSP;
 302        if (rdai->data_alignment)
 303                cr_own |= SDTA;
 304        if (rdai->sys_delay)
 305                cr_own |= DEL;
 306        if (rsnd_io_is_play(io))
 307                cr_own |= TRMD;
 308
 309        switch (runtime->sample_bits) {
 310        case 16:
 311                cr_own |= DWL_16;
 312                break;
 313        case 32:
 314                cr_own |= DWL_24;
 315                break;
 316        }
 317
 318        if (rsnd_ssi_is_dma_mode(mod)) {
 319                cr_mode = UIEN | OIEN | /* over/under run */
 320                          DMEN;         /* DMA : enable DMA */
 321        } else {
 322                cr_mode = DIEN;         /* PIO : enable Data interrupt */
 323        }
 324
 325        /*
 326         * TDM Extend Mode
 327         * see
 328         *      rsnd_ssiu_init_gen2()
 329         */
 330        wsr = ssi->wsr;
 331        if (is_tdm) {
 332                wsr     |= WS_MODE;
 333                cr_own  |= CHNL_8;
 334        }
 335
 336        ssi->cr_own     = cr_own;
 337        ssi->cr_mode    = cr_mode;
 338        ssi->wsr        = wsr;
 339}
 340
 341static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
 342{
 343        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 344
 345        rsnd_mod_write(mod, SSIWSR,     ssi->wsr);
 346        rsnd_mod_write(mod, SSICR,      ssi->cr_own     |
 347                                        ssi->cr_clk     |
 348                                        ssi->cr_mode); /* without EN */
 349}
 350
 351/*
 352 *      SSI mod common functions
 353 */
 354static int rsnd_ssi_init(struct rsnd_mod *mod,
 355                         struct rsnd_dai_stream *io,
 356                         struct rsnd_priv *priv)
 357{
 358        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 359        int ret;
 360
 361        if (!rsnd_ssi_is_run_mods(mod, io))
 362                return 0;
 363
 364        ssi->usrcnt++;
 365
 366        rsnd_mod_power_on(mod);
 367
 368        ret = rsnd_ssi_master_clk_start(mod, io);
 369        if (ret < 0)
 370                return ret;
 371
 372        if (!rsnd_ssi_is_parent(mod, io))
 373                rsnd_ssi_config_init(mod, io);
 374
 375        rsnd_ssi_register_setup(mod);
 376
 377        /* clear error status */
 378        rsnd_ssi_status_clear(mod);
 379
 380        return 0;
 381}
 382
 383static int rsnd_ssi_quit(struct rsnd_mod *mod,
 384                         struct rsnd_dai_stream *io,
 385                         struct rsnd_priv *priv)
 386{
 387        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 388        struct device *dev = rsnd_priv_to_dev(priv);
 389
 390        if (!rsnd_ssi_is_run_mods(mod, io))
 391                return 0;
 392
 393        if (!ssi->usrcnt) {
 394                dev_err(dev, "%s[%d] usrcnt error\n",
 395                        rsnd_mod_name(mod), rsnd_mod_id(mod));
 396                return -EIO;
 397        }
 398
 399        if (!rsnd_ssi_is_parent(mod, io))
 400                ssi->cr_own     = 0;
 401
 402        rsnd_ssi_master_clk_stop(mod, io);
 403
 404        rsnd_mod_power_off(mod);
 405
 406        ssi->usrcnt--;
 407
 408        return 0;
 409}
 410
 411static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
 412                              struct rsnd_dai_stream *io,
 413                              struct snd_pcm_substream *substream,
 414                              struct snd_pcm_hw_params *params)
 415{
 416        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 417        int chan = params_channels(params);
 418
 419        /*
 420         * Already working.
 421         * It will happen if SSI has parent/child connection.
 422         */
 423        if (ssi->usrcnt > 1) {
 424                /*
 425                 * it is error if child <-> parent SSI uses
 426                 * different channels.
 427                 */
 428                if (ssi->chan != chan)
 429                        return -EIO;
 430        }
 431
 432        ssi->chan = chan;
 433
 434        return 0;
 435}
 436
 437static int rsnd_ssi_start(struct rsnd_mod *mod,
 438                          struct rsnd_dai_stream *io,
 439                          struct rsnd_priv *priv)
 440{
 441        if (!rsnd_ssi_is_run_mods(mod, io))
 442                return 0;
 443
 444        /*
 445         * EN will be set via SSIU :: SSI_CONTROL
 446         * if Multi channel mode
 447         */
 448        if (rsnd_ssi_multi_slaves_runtime(io))
 449                return 0;
 450
 451        rsnd_mod_bset(mod, SSICR, EN, EN);
 452
 453        return 0;
 454}
 455
 456static int rsnd_ssi_stop(struct rsnd_mod *mod,
 457                         struct rsnd_dai_stream *io,
 458                         struct rsnd_priv *priv)
 459{
 460        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 461        u32 cr;
 462
 463        if (!rsnd_ssi_is_run_mods(mod, io))
 464                return 0;
 465
 466        /*
 467         * don't stop if not last user
 468         * see also
 469         *      rsnd_ssi_start
 470         *      rsnd_ssi_interrupt
 471         */
 472        if (ssi->usrcnt > 1)
 473                return 0;
 474
 475        /*
 476         * disable all IRQ,
 477         * and, wait all data was sent
 478         */
 479        cr  =   ssi->cr_own     |
 480                ssi->cr_clk;
 481
 482        rsnd_mod_write(mod, SSICR, cr | EN);
 483        rsnd_ssi_status_check(mod, DIRQ);
 484
 485        /*
 486         * disable SSI,
 487         * and, wait idle state
 488         */
 489        rsnd_mod_write(mod, SSICR, cr); /* disabled all */
 490        rsnd_ssi_status_check(mod, IIRQ);
 491
 492        return 0;
 493}
 494
 495static int rsnd_ssi_irq(struct rsnd_mod *mod,
 496                        struct rsnd_dai_stream *io,
 497                        struct rsnd_priv *priv,
 498                        int enable)
 499{
 500        u32 val = 0;
 501
 502        if (rsnd_is_gen1(priv))
 503                return 0;
 504
 505        if (rsnd_ssi_is_parent(mod, io))
 506                return 0;
 507
 508        if (!rsnd_ssi_is_run_mods(mod, io))
 509                return 0;
 510
 511        if (enable)
 512                val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
 513
 514        rsnd_mod_write(mod, SSI_INT_ENABLE, val);
 515
 516        return 0;
 517}
 518
 519static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
 520                                 struct rsnd_dai_stream *io)
 521{
 522        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 523        int is_dma = rsnd_ssi_is_dma_mode(mod);
 524        u32 status;
 525        bool elapsed = false;
 526        bool stop = false;
 527
 528        spin_lock(&priv->lock);
 529
 530        /* ignore all cases if not working */
 531        if (!rsnd_io_is_working(io))
 532                goto rsnd_ssi_interrupt_out;
 533
 534        status = rsnd_ssi_status_get(mod);
 535
 536        /* PIO only */
 537        if (!is_dma && (status & DIRQ)) {
 538                struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
 539                u32 *buf = (u32 *)(runtime->dma_area +
 540                                   rsnd_dai_pointer_offset(io, 0));
 541
 542                /*
 543                 * 8/16/32 data can be assesse to TDR/RDR register
 544                 * directly as 32bit data
 545                 * see rsnd_ssi_init()
 546                 */
 547                if (rsnd_io_is_play(io))
 548                        rsnd_mod_write(mod, SSITDR, *buf);
 549                else
 550                        *buf = rsnd_mod_read(mod, SSIRDR);
 551
 552                elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
 553        }
 554
 555        /* DMA only */
 556        if (is_dma && (status & (UIRQ | OIRQ)))
 557                stop = true;
 558
 559        rsnd_ssi_status_clear(mod);
 560rsnd_ssi_interrupt_out:
 561        spin_unlock(&priv->lock);
 562
 563        if (elapsed)
 564                rsnd_dai_period_elapsed(io);
 565
 566        if (stop)
 567                snd_pcm_stop_xrun(io->substream);
 568
 569}
 570
 571static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
 572{
 573        struct rsnd_mod *mod = data;
 574
 575        rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
 576
 577        return IRQ_HANDLED;
 578}
 579
 580/*
 581 *              SSI PIO
 582 */
 583static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
 584                                   struct rsnd_dai_stream *io)
 585{
 586        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 587        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 588
 589        if (!__rsnd_ssi_is_pin_sharing(mod))
 590                return;
 591
 592        if (!rsnd_rdai_is_clk_master(rdai))
 593                return;
 594
 595        switch (rsnd_mod_id(mod)) {
 596        case 1:
 597        case 2:
 598                rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
 599                break;
 600        case 4:
 601                rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
 602                break;
 603        case 8:
 604                rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
 605                break;
 606        }
 607}
 608
 609static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
 610                            struct rsnd_dai_stream *io,
 611                            struct snd_soc_pcm_runtime *rtd)
 612{
 613        /*
 614         * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
 615         * and, pcm_new will be called after it.
 616         * This function reuse pcm_new at this point.
 617         */
 618        rsnd_ssi_parent_attach(mod, io);
 619
 620        return 0;
 621}
 622
 623static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
 624                                 struct rsnd_dai_stream *io,
 625                                 struct rsnd_priv *priv)
 626{
 627        struct device *dev = rsnd_priv_to_dev(priv);
 628        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 629        int ret;
 630
 631        /*
 632         * SSIP/SSIU/IRQ are not needed on
 633         * SSI Multi slaves
 634         */
 635        if (rsnd_ssi_is_multi_slave(mod, io))
 636                return 0;
 637
 638        /*
 639         * It can't judge ssi parent at this point
 640         * see rsnd_ssi_pcm_new()
 641         */
 642
 643        ret = rsnd_ssiu_attach(io, mod);
 644        if (ret < 0)
 645                return ret;
 646
 647        ret = devm_request_irq(dev, ssi->irq,
 648                               rsnd_ssi_interrupt,
 649                               IRQF_SHARED,
 650                               dev_name(dev), mod);
 651
 652        return ret;
 653}
 654
 655static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
 656        .name   = SSI_NAME,
 657        .probe  = rsnd_ssi_common_probe,
 658        .init   = rsnd_ssi_init,
 659        .quit   = rsnd_ssi_quit,
 660        .start  = rsnd_ssi_start,
 661        .stop   = rsnd_ssi_stop,
 662        .irq    = rsnd_ssi_irq,
 663        .pcm_new = rsnd_ssi_pcm_new,
 664        .hw_params = rsnd_ssi_hw_params,
 665};
 666
 667static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
 668                              struct rsnd_dai_stream *io,
 669                              struct rsnd_priv *priv)
 670{
 671        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 672        int dma_id = 0; /* not needed */
 673        int ret;
 674
 675        /*
 676         * SSIP/SSIU/IRQ/DMA are not needed on
 677         * SSI Multi slaves
 678         */
 679        if (rsnd_ssi_is_multi_slave(mod, io))
 680                return 0;
 681
 682        ret = rsnd_ssi_common_probe(mod, io, priv);
 683        if (ret)
 684                return ret;
 685
 686        /* SSI probe might be called many times in MUX multi path */
 687        ret = rsnd_dma_attach(io, mod, &ssi->dma, dma_id);
 688
 689        return ret;
 690}
 691
 692static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
 693                               struct rsnd_dai_stream *io,
 694                               struct rsnd_priv *priv)
 695{
 696        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 697        struct device *dev = rsnd_priv_to_dev(priv);
 698        int irq = ssi->irq;
 699
 700        /* PIO will request IRQ again */
 701        devm_free_irq(dev, irq, mod);
 702
 703        return 0;
 704}
 705
 706static int rsnd_ssi_fallback(struct rsnd_mod *mod,
 707                             struct rsnd_dai_stream *io,
 708                             struct rsnd_priv *priv)
 709{
 710        struct device *dev = rsnd_priv_to_dev(priv);
 711
 712        /*
 713         * fallback to PIO
 714         *
 715         * SSI .probe might be called again.
 716         * see
 717         *      rsnd_rdai_continuance_probe()
 718         */
 719        mod->ops = &rsnd_ssi_pio_ops;
 720
 721        dev_info(dev, "%s[%d] fallback to PIO mode\n",
 722                 rsnd_mod_name(mod), rsnd_mod_id(mod));
 723
 724        return 0;
 725}
 726
 727static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
 728                                         struct rsnd_mod *mod)
 729{
 730        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 731        int is_play = rsnd_io_is_play(io);
 732        char *name;
 733
 734        if (rsnd_ssi_use_busif(io))
 735                name = is_play ? "rxu" : "txu";
 736        else
 737                name = is_play ? "rx" : "tx";
 738
 739        return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
 740                                        mod, name);
 741}
 742
 743static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
 744        .name   = SSI_NAME,
 745        .dma_req = rsnd_ssi_dma_req,
 746        .probe  = rsnd_ssi_dma_probe,
 747        .remove = rsnd_ssi_dma_remove,
 748        .init   = rsnd_ssi_init,
 749        .quit   = rsnd_ssi_quit,
 750        .start  = rsnd_ssi_start,
 751        .stop   = rsnd_ssi_stop,
 752        .irq    = rsnd_ssi_irq,
 753        .pcm_new = rsnd_ssi_pcm_new,
 754        .fallback = rsnd_ssi_fallback,
 755        .hw_params = rsnd_ssi_hw_params,
 756};
 757
 758int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
 759{
 760        return mod->ops == &rsnd_ssi_dma_ops;
 761}
 762
 763
 764/*
 765 *              Non SSI
 766 */
 767static struct rsnd_mod_ops rsnd_ssi_non_ops = {
 768        .name   = SSI_NAME,
 769};
 770
 771/*
 772 *              ssi mod function
 773 */
 774static void rsnd_ssi_connect(struct rsnd_mod *mod,
 775                             struct rsnd_dai_stream *io)
 776{
 777        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 778        enum rsnd_mod_type types[] = {
 779                RSND_MOD_SSI,
 780                RSND_MOD_SSIM1,
 781                RSND_MOD_SSIM2,
 782                RSND_MOD_SSIM3,
 783        };
 784        enum rsnd_mod_type type;
 785        int i;
 786
 787        /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
 788        for (i = 0; i < ARRAY_SIZE(types); i++) {
 789                type = types[i];
 790                if (!rsnd_io_to_mod(io, type)) {
 791                        rsnd_dai_connect(mod, io, type);
 792                        rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
 793                        return;
 794                }
 795        }
 796}
 797
 798void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
 799                            struct device_node *playback,
 800                            struct device_node *capture)
 801{
 802        struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
 803        struct device_node *node;
 804        struct device_node *np;
 805        struct rsnd_mod *mod;
 806        int i;
 807
 808        node = rsnd_ssi_of_node(priv);
 809        if (!node)
 810                return;
 811
 812        i = 0;
 813        for_each_child_of_node(node, np) {
 814                mod = rsnd_ssi_mod_get(priv, i);
 815                if (np == playback)
 816                        rsnd_ssi_connect(mod, &rdai->playback);
 817                if (np == capture)
 818                        rsnd_ssi_connect(mod, &rdai->capture);
 819                i++;
 820        }
 821
 822        of_node_put(node);
 823}
 824
 825struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
 826{
 827        if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
 828                id = 0;
 829
 830        return rsnd_mod_get(rsnd_ssi_get(priv, id));
 831}
 832
 833int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
 834{
 835        struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
 836
 837        return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
 838}
 839
 840static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
 841                                struct rsnd_mod *mod,
 842                                enum rsnd_mod_type type)
 843{
 844        /*
 845         * SSIP (= SSI parent) needs to be special, otherwise,
 846         * 2nd SSI might doesn't start. see also rsnd_mod_call()
 847         *
 848         * We can't include parent SSI status on SSI, because we don't know
 849         * how many SSI requests parent SSI. Thus, it is localed on "io" now.
 850         * ex) trouble case
 851         *      Playback: SSI0
 852         *      Capture : SSI1 (needs SSI0)
 853         *
 854         * 1) start Capture  -> SSI0/SSI1 are started.
 855         * 2) start Playback -> SSI0 doesn't work, because it is already
 856         *                      marked as "started" on 1)
 857         *
 858         * OTOH, using each mod's status is good for MUX case.
 859         * It doesn't need to start in 2nd start
 860         * ex)
 861         *      IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
 862         *                          |
 863         *      IO-1: SRC1 -> CTU2 -+
 864         *
 865         * 1) start IO-0 ->     start SSI0
 866         * 2) start IO-1 ->     SSI0 doesn't need to start, because it is
 867         *                      already started on 1)
 868         */
 869        if (type == RSND_MOD_SSIP)
 870                return &io->parent_ssi_status;
 871
 872        return rsnd_mod_get_status(io, mod, type);
 873}
 874
 875int rsnd_ssi_probe(struct rsnd_priv *priv)
 876{
 877        struct device_node *node;
 878        struct device_node *np;
 879        struct device *dev = rsnd_priv_to_dev(priv);
 880        struct rsnd_mod_ops *ops;
 881        struct clk *clk;
 882        struct rsnd_ssi *ssi;
 883        char name[RSND_SSI_NAME_SIZE];
 884        int i, nr, ret;
 885
 886        node = rsnd_ssi_of_node(priv);
 887        if (!node)
 888                return -EINVAL;
 889
 890        nr = of_get_child_count(node);
 891        if (!nr) {
 892                ret = -EINVAL;
 893                goto rsnd_ssi_probe_done;
 894        }
 895
 896        ssi     = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
 897        if (!ssi) {
 898                ret = -ENOMEM;
 899                goto rsnd_ssi_probe_done;
 900        }
 901
 902        priv->ssi       = ssi;
 903        priv->ssi_nr    = nr;
 904
 905        i = 0;
 906        for_each_child_of_node(node, np) {
 907                ssi = rsnd_ssi_get(priv, i);
 908
 909                snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
 910                         SSI_NAME, i);
 911
 912                clk = devm_clk_get(dev, name);
 913                if (IS_ERR(clk)) {
 914                        ret = PTR_ERR(clk);
 915                        goto rsnd_ssi_probe_done;
 916                }
 917
 918                if (of_get_property(np, "shared-pin", NULL))
 919                        ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
 920
 921                if (of_get_property(np, "no-busif", NULL))
 922                        ssi->flags |= RSND_SSI_NO_BUSIF;
 923
 924                ssi->irq = irq_of_parse_and_map(np, 0);
 925                if (!ssi->irq) {
 926                        ret = -EINVAL;
 927                        goto rsnd_ssi_probe_done;
 928                }
 929
 930                ops = &rsnd_ssi_non_ops;
 931                if (of_get_property(np, "pio-transfer", NULL))
 932                        ops = &rsnd_ssi_pio_ops;
 933                else
 934                        ops = &rsnd_ssi_dma_ops;
 935
 936                ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
 937                                    rsnd_ssi_get_status, RSND_MOD_SSI, i);
 938                if (ret)
 939                        goto rsnd_ssi_probe_done;
 940
 941                i++;
 942        }
 943
 944        ret = 0;
 945
 946rsnd_ssi_probe_done:
 947        of_node_put(node);
 948
 949        return ret;
 950}
 951
 952void rsnd_ssi_remove(struct rsnd_priv *priv)
 953{
 954        struct rsnd_ssi *ssi;
 955        int i;
 956
 957        for_each_rsnd_ssi(ssi, priv, i) {
 958                rsnd_mod_quit(rsnd_mod_get(ssi));
 959        }
 960}
 961