linux/sound/soc/sh/rcar/core.c
<<
>>
Prefs
   1/*
   2 * Renesas R-Car SRU/SCU/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
  15/*
  16 * Renesas R-Car sound device structure
  17 *
  18 * Gen1
  19 *
  20 * SRU          : Sound Routing Unit
  21 *  - SRC       : Sampling Rate Converter
  22 *  - CMD
  23 *    - CTU     : Channel Count Conversion Unit
  24 *    - MIX     : Mixer
  25 *    - DVC     : Digital Volume and Mute Function
  26 *  - SSI       : Serial Sound Interface
  27 *
  28 * Gen2
  29 *
  30 * SCU          : Sampling Rate Converter Unit
  31 *  - SRC       : Sampling Rate Converter
  32 *  - CMD
  33 *   - CTU      : Channel Count Conversion Unit
  34 *   - MIX      : Mixer
  35 *   - DVC      : Digital Volume and Mute Function
  36 * SSIU         : Serial Sound Interface Unit
  37 *  - SSI       : Serial Sound Interface
  38 */
  39
  40/*
  41 *      driver data Image
  42 *
  43 * rsnd_priv
  44 *   |
  45 *   | ** this depends on Gen1/Gen2
  46 *   |
  47 *   +- gen
  48 *   |
  49 *   | ** these depend on data path
  50 *   | ** gen and platform data control it
  51 *   |
  52 *   +- rdai[0]
  53 *   |   |               sru     ssiu      ssi
  54 *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
  55 *   |   |
  56 *   |   |               sru     ssiu      ssi
  57 *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
  58 *   |
  59 *   +- rdai[1]
  60 *   |   |               sru     ssiu      ssi
  61 *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
  62 *   |   |
  63 *   |   |               sru     ssiu      ssi
  64 *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
  65 *   ...
  66 *   |
  67 *   | ** these control ssi
  68 *   |
  69 *   +- ssi
  70 *   |  |
  71 *   |  +- ssi[0]
  72 *   |  +- ssi[1]
  73 *   |  +- ssi[2]
  74 *   |  ...
  75 *   |
  76 *   | ** these control src
  77 *   |
  78 *   +- src
  79 *      |
  80 *      +- src[0]
  81 *      +- src[1]
  82 *      +- src[2]
  83 *      ...
  84 *
  85 *
  86 * for_each_rsnd_dai(xx, priv, xx)
  87 *  rdai[0] => rdai[1] => rdai[2] => ...
  88 *
  89 * for_each_rsnd_mod(xx, rdai, xx)
  90 *  [mod] => [mod] => [mod] => ...
  91 *
  92 * rsnd_dai_call(xxx, fn )
  93 *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
  94 *
  95 */
  96#include <linux/pm_runtime.h>
  97#include "rsnd.h"
  98
  99#define RSND_RATES SNDRV_PCM_RATE_8000_96000
 100#define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
 101
 102static const struct rsnd_of_data rsnd_of_data_gen1 = {
 103        .flags = RSND_GEN1,
 104};
 105
 106static const struct rsnd_of_data rsnd_of_data_gen2 = {
 107        .flags = RSND_GEN2,
 108};
 109
 110static const struct of_device_id rsnd_of_match[] = {
 111        { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
 112        { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
 113        {},
 114};
 115MODULE_DEVICE_TABLE(of, rsnd_of_match);
 116
 117/*
 118 *      rsnd_platform functions
 119 */
 120#define rsnd_platform_call(priv, dai, func, param...)   \
 121        (!(priv->info->func) ? 0 :              \
 122         priv->info->func(param))
 123
 124#define rsnd_is_enable_path(io, name) \
 125        ((io)->info ? (io)->info->name : NULL)
 126#define rsnd_info_id(priv, io, name) \
 127        ((io)->info->name - priv->info->name##_info)
 128
 129/*
 130 *      rsnd_mod functions
 131 */
 132char *rsnd_mod_name(struct rsnd_mod *mod)
 133{
 134        if (!mod || !mod->ops)
 135                return "unknown";
 136
 137        return mod->ops->name;
 138}
 139
 140struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
 141                                  struct rsnd_mod *mod)
 142{
 143        if (!mod || !mod->ops || !mod->ops->dma_req)
 144                return NULL;
 145
 146        return mod->ops->dma_req(io, mod);
 147}
 148
 149int rsnd_mod_init(struct rsnd_priv *priv,
 150                  struct rsnd_mod *mod,
 151                   struct rsnd_mod_ops *ops,
 152                   struct clk *clk,
 153                   enum rsnd_mod_type type,
 154                   int id)
 155{
 156        int ret = clk_prepare(clk);
 157
 158        if (ret)
 159                return ret;
 160
 161        mod->id         = id;
 162        mod->ops        = ops;
 163        mod->type       = type;
 164        mod->clk        = clk;
 165        mod->priv       = priv;
 166
 167        return ret;
 168}
 169
 170void rsnd_mod_quit(struct rsnd_mod *mod)
 171{
 172        if (mod->clk)
 173                clk_unprepare(mod->clk);
 174}
 175
 176void rsnd_mod_interrupt(struct rsnd_mod *mod,
 177                        void (*callback)(struct rsnd_mod *mod,
 178                                         struct rsnd_dai_stream *io))
 179{
 180        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 181        struct rsnd_dai_stream *io;
 182        struct rsnd_dai *rdai;
 183        int i, j;
 184
 185        for_each_rsnd_dai(rdai, priv, j) {
 186
 187                for (i = 0; i < RSND_MOD_MAX; i++) {
 188                        io = &rdai->playback;
 189                        if (mod == io->mod[i])
 190                                callback(mod, io);
 191
 192                        io = &rdai->capture;
 193                        if (mod == io->mod[i])
 194                                callback(mod, io);
 195                }
 196        }
 197}
 198
 199int rsnd_io_is_working(struct rsnd_dai_stream *io)
 200{
 201        /* see rsnd_dai_stream_init/quit() */
 202        return !!io->substream;
 203}
 204
 205/*
 206 *      settting function
 207 */
 208u32 rsnd_get_adinr(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
 209{
 210        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 211        struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
 212        struct device *dev = rsnd_priv_to_dev(priv);
 213        u32 adinr = runtime->channels;
 214
 215        switch (runtime->sample_bits) {
 216        case 16:
 217                adinr |= (8 << 16);
 218                break;
 219        case 32:
 220                adinr |= (0 << 16);
 221                break;
 222        default:
 223                dev_warn(dev, "not supported sample bits\n");
 224                return 0;
 225        }
 226
 227        return adinr;
 228}
 229
 230/*
 231 *      rsnd_dai functions
 232 */
 233#define __rsnd_mod_call(mod, io, func, param...)                \
 234({                                                              \
 235        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
 236        struct device *dev = rsnd_priv_to_dev(priv);            \
 237        u32 mask = 0xF << __rsnd_mod_shift_##func;                      \
 238        u8 val  = (mod->status >> __rsnd_mod_shift_##func) & 0xF;       \
 239        u8 add  = ((val + __rsnd_mod_add_##func) & 0xF);                \
 240        int ret = 0;                                                    \
 241        int called = 0;                                                 \
 242        if (val == __rsnd_mod_call_##func) {                            \
 243                called = 1;                                             \
 244                ret = (mod)->ops->func(mod, io, param);                 \
 245                mod->status = (mod->status & ~mask) +                   \
 246                        (add << __rsnd_mod_shift_##func);               \
 247        }                                                               \
 248        dev_dbg(dev, "%s[%d] 0x%08x %s\n",                              \
 249                rsnd_mod_name(mod), rsnd_mod_id(mod), mod->status,      \
 250                called ? #func : "");                                   \
 251        ret;                                                            \
 252})
 253
 254#define rsnd_mod_call(mod, io, func, param...)  \
 255        (!(mod) ? -ENODEV :                     \
 256         !((mod)->ops->func) ? 0 :              \
 257         __rsnd_mod_call(mod, io, func, param))
 258
 259#define rsnd_dai_call(fn, io, param...)                         \
 260({                                                              \
 261        struct rsnd_mod *mod;                                   \
 262        int ret = 0, i;                                         \
 263        for (i = 0; i < RSND_MOD_MAX; i++) {                    \
 264                mod = (io)->mod[i];                             \
 265                if (!mod)                                       \
 266                        continue;                               \
 267                ret = rsnd_mod_call(mod, io, fn, param);        \
 268                if (ret < 0)                                    \
 269                        break;                                  \
 270        }                                                       \
 271        ret;                                                    \
 272})
 273
 274static int rsnd_dai_connect(struct rsnd_mod *mod,
 275                            struct rsnd_dai_stream *io)
 276{
 277        if (!mod)
 278                return -EIO;
 279
 280        if (io->mod[mod->type]) {
 281                struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 282                struct device *dev = rsnd_priv_to_dev(priv);
 283
 284                dev_err(dev, "%s[%d] is not empty\n",
 285                        rsnd_mod_name(mod),
 286                        rsnd_mod_id(mod));
 287                return -EIO;
 288        }
 289
 290        io->mod[mod->type] = mod;
 291
 292        return 0;
 293}
 294
 295static void rsnd_dai_disconnect(struct rsnd_mod *mod,
 296                                struct rsnd_dai_stream *io)
 297{
 298        io->mod[mod->type] = NULL;
 299}
 300
 301struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
 302{
 303        if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
 304                return NULL;
 305
 306        return priv->rdai + id;
 307}
 308
 309#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
 310static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
 311{
 312        struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
 313
 314        return rsnd_rdai_get(priv, dai->id);
 315}
 316
 317/*
 318 *      rsnd_soc_dai functions
 319 */
 320int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
 321{
 322        struct snd_pcm_substream *substream = io->substream;
 323        struct snd_pcm_runtime *runtime = substream->runtime;
 324        int pos = io->byte_pos + additional;
 325
 326        pos %= (runtime->periods * io->byte_per_period);
 327
 328        return pos;
 329}
 330
 331bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
 332{
 333        io->byte_pos += byte;
 334
 335        if (io->byte_pos >= io->next_period_byte) {
 336                struct snd_pcm_substream *substream = io->substream;
 337                struct snd_pcm_runtime *runtime = substream->runtime;
 338
 339                io->period_pos++;
 340                io->next_period_byte += io->byte_per_period;
 341
 342                if (io->period_pos >= runtime->periods) {
 343                        io->byte_pos = 0;
 344                        io->period_pos = 0;
 345                        io->next_period_byte = io->byte_per_period;
 346                }
 347
 348                return true;
 349        }
 350
 351        return false;
 352}
 353
 354void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
 355{
 356        struct snd_pcm_substream *substream = io->substream;
 357
 358        /*
 359         * this function should be called...
 360         *
 361         * - if rsnd_dai_pointer_update() returns true
 362         * - without spin lock
 363         */
 364
 365        snd_pcm_period_elapsed(substream);
 366}
 367
 368static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
 369                                struct snd_pcm_substream *substream)
 370{
 371        struct snd_pcm_runtime *runtime = substream->runtime;
 372
 373        io->substream           = substream;
 374        io->byte_pos            = 0;
 375        io->period_pos          = 0;
 376        io->byte_per_period     = runtime->period_size *
 377                                  runtime->channels *
 378                                  samples_to_bytes(runtime, 1);
 379        io->next_period_byte    = io->byte_per_period;
 380}
 381
 382static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
 383{
 384        io->substream           = NULL;
 385}
 386
 387static
 388struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
 389{
 390        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 391
 392        return  rtd->cpu_dai;
 393}
 394
 395static
 396struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
 397                                        struct snd_pcm_substream *substream)
 398{
 399        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 400                return &rdai->playback;
 401        else
 402                return &rdai->capture;
 403}
 404
 405static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
 406                            struct snd_soc_dai *dai)
 407{
 408        struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
 409        struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
 410        struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
 411        int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
 412        int ret;
 413        unsigned long flags;
 414
 415        spin_lock_irqsave(&priv->lock, flags);
 416
 417        switch (cmd) {
 418        case SNDRV_PCM_TRIGGER_START:
 419                rsnd_dai_stream_init(io, substream);
 420
 421                ret = rsnd_platform_call(priv, dai, start, ssi_id);
 422                if (ret < 0)
 423                        goto dai_trigger_end;
 424
 425                ret = rsnd_dai_call(init, io, priv);
 426                if (ret < 0)
 427                        goto dai_trigger_end;
 428
 429                ret = rsnd_dai_call(start, io, priv);
 430                if (ret < 0)
 431                        goto dai_trigger_end;
 432                break;
 433        case SNDRV_PCM_TRIGGER_STOP:
 434                ret = rsnd_dai_call(stop, io, priv);
 435                if (ret < 0)
 436                        goto dai_trigger_end;
 437
 438                ret = rsnd_dai_call(quit, io, priv);
 439                if (ret < 0)
 440                        goto dai_trigger_end;
 441
 442                ret = rsnd_platform_call(priv, dai, stop, ssi_id);
 443                if (ret < 0)
 444                        goto dai_trigger_end;
 445
 446                rsnd_dai_stream_quit(io);
 447                break;
 448        default:
 449                ret = -EINVAL;
 450        }
 451
 452dai_trigger_end:
 453        spin_unlock_irqrestore(&priv->lock, flags);
 454
 455        return ret;
 456}
 457
 458static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 459{
 460        struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
 461
 462        /* set master/slave audio interface */
 463        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 464        case SND_SOC_DAIFMT_CBM_CFM:
 465                rdai->clk_master = 0;
 466                break;
 467        case SND_SOC_DAIFMT_CBS_CFS:
 468                rdai->clk_master = 1; /* codec is slave, cpu is master */
 469                break;
 470        default:
 471                return -EINVAL;
 472        }
 473
 474        /* set format */
 475        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 476        case SND_SOC_DAIFMT_I2S:
 477                rdai->sys_delay = 0;
 478                rdai->data_alignment = 0;
 479                rdai->frm_clk_inv = 0;
 480                break;
 481        case SND_SOC_DAIFMT_LEFT_J:
 482                rdai->sys_delay = 1;
 483                rdai->data_alignment = 0;
 484                rdai->frm_clk_inv = 1;
 485                break;
 486        case SND_SOC_DAIFMT_RIGHT_J:
 487                rdai->sys_delay = 1;
 488                rdai->data_alignment = 1;
 489                rdai->frm_clk_inv = 1;
 490                break;
 491        }
 492
 493        /* set clock inversion */
 494        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 495        case SND_SOC_DAIFMT_NB_IF:
 496                rdai->bit_clk_inv =  rdai->bit_clk_inv;
 497                rdai->frm_clk_inv = !rdai->frm_clk_inv;
 498                break;
 499        case SND_SOC_DAIFMT_IB_NF:
 500                rdai->bit_clk_inv = !rdai->bit_clk_inv;
 501                rdai->frm_clk_inv =  rdai->frm_clk_inv;
 502                break;
 503        case SND_SOC_DAIFMT_IB_IF:
 504                rdai->bit_clk_inv = !rdai->bit_clk_inv;
 505                rdai->frm_clk_inv = !rdai->frm_clk_inv;
 506                break;
 507        case SND_SOC_DAIFMT_NB_NF:
 508        default:
 509                break;
 510        }
 511
 512        return 0;
 513}
 514
 515static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
 516        .trigger        = rsnd_soc_dai_trigger,
 517        .set_fmt        = rsnd_soc_dai_set_fmt,
 518};
 519
 520#define rsnd_path_parse(priv, io, type)                         \
 521({                                                              \
 522        struct rsnd_mod *mod;                                   \
 523        int ret = 0;                                            \
 524        int id = -1;                                            \
 525                                                                \
 526        if (rsnd_is_enable_path(io, type)) {                    \
 527                id = rsnd_info_id(priv, io, type);              \
 528                if (id >= 0) {                                  \
 529                        mod = rsnd_##type##_mod_get(priv, id);  \
 530                        ret = rsnd_dai_connect(mod, io);        \
 531                }                                               \
 532        }                                                       \
 533        ret;                                                    \
 534})
 535
 536#define rsnd_path_break(priv, io, type)                         \
 537{                                                               \
 538        struct rsnd_mod *mod;                                   \
 539        int id = -1;                                            \
 540                                                                \
 541        if (rsnd_is_enable_path(io, type)) {                    \
 542                id = rsnd_info_id(priv, io, type);              \
 543                if (id >= 0) {                                  \
 544                        mod = rsnd_##type##_mod_get(priv, id);  \
 545                        rsnd_dai_disconnect(mod, io);           \
 546                }                                               \
 547        }                                                       \
 548}
 549
 550static int rsnd_path_init(struct rsnd_priv *priv,
 551                          struct rsnd_dai *rdai,
 552                          struct rsnd_dai_stream *io)
 553{
 554        int ret;
 555
 556        /*
 557         * Gen1 is created by SRU/SSI, and this SRU is base module of
 558         * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
 559         *
 560         * Easy image is..
 561         *      Gen1 SRU = Gen2 SCU + SSIU + etc
 562         *
 563         * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
 564         * using fixed path.
 565         */
 566
 567        /* SRC */
 568        ret = rsnd_path_parse(priv, io, src);
 569        if (ret < 0)
 570                return ret;
 571
 572        /* SSI */
 573        ret = rsnd_path_parse(priv, io, ssi);
 574        if (ret < 0)
 575                return ret;
 576
 577        /* DVC */
 578        ret = rsnd_path_parse(priv, io, dvc);
 579        if (ret < 0)
 580                return ret;
 581
 582        return ret;
 583}
 584
 585static void rsnd_of_parse_dai(struct platform_device *pdev,
 586                              const struct rsnd_of_data *of_data,
 587                              struct rsnd_priv *priv)
 588{
 589        struct device_node *dai_node,   *dai_np;
 590        struct device_node *ssi_node,   *ssi_np;
 591        struct device_node *src_node,   *src_np;
 592        struct device_node *dvc_node,   *dvc_np;
 593        struct device_node *playback, *capture;
 594        struct rsnd_dai_platform_info *dai_info;
 595        struct rcar_snd_info *info = rsnd_priv_to_info(priv);
 596        struct device *dev = &pdev->dev;
 597        int nr, i;
 598        int dai_i, ssi_i, src_i, dvc_i;
 599
 600        if (!of_data)
 601                return;
 602
 603        dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
 604        if (!dai_node)
 605                return;
 606
 607        nr = of_get_child_count(dai_node);
 608        if (!nr)
 609                return;
 610
 611        dai_info = devm_kzalloc(dev,
 612                                sizeof(struct rsnd_dai_platform_info) * nr,
 613                                GFP_KERNEL);
 614        if (!dai_info) {
 615                dev_err(dev, "dai info allocation error\n");
 616                return;
 617        }
 618
 619        info->dai_info_nr       = nr;
 620        info->dai_info          = dai_info;
 621
 622        ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
 623        src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
 624        dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
 625
 626#define mod_parse(name)                                                 \
 627if (name##_node) {                                                      \
 628        struct rsnd_##name##_platform_info *name##_info;                \
 629                                                                        \
 630        name##_i = 0;                                                   \
 631        for_each_child_of_node(name##_node, name##_np) {                \
 632                name##_info = info->name##_info + name##_i;             \
 633                                                                        \
 634                if (name##_np == playback)                              \
 635                        dai_info->playback.name = name##_info;          \
 636                if (name##_np == capture)                               \
 637                        dai_info->capture.name = name##_info;           \
 638                                                                        \
 639                name##_i++;                                             \
 640        }                                                               \
 641}
 642
 643        /*
 644         * parse all dai
 645         */
 646        dai_i = 0;
 647        for_each_child_of_node(dai_node, dai_np) {
 648                dai_info = info->dai_info + dai_i;
 649
 650                for (i = 0;; i++) {
 651
 652                        playback = of_parse_phandle(dai_np, "playback", i);
 653                        capture  = of_parse_phandle(dai_np, "capture", i);
 654
 655                        if (!playback && !capture)
 656                                break;
 657
 658                        mod_parse(ssi);
 659                        mod_parse(src);
 660                        mod_parse(dvc);
 661
 662                        of_node_put(playback);
 663                        of_node_put(capture);
 664                }
 665
 666                dai_i++;
 667        }
 668}
 669
 670static int rsnd_dai_probe(struct platform_device *pdev,
 671                          const struct rsnd_of_data *of_data,
 672                          struct rsnd_priv *priv)
 673{
 674        struct snd_soc_dai_driver *drv;
 675        struct rcar_snd_info *info = rsnd_priv_to_info(priv);
 676        struct rsnd_dai *rdai;
 677        struct rsnd_ssi_platform_info *pmod, *cmod;
 678        struct device *dev = rsnd_priv_to_dev(priv);
 679        int dai_nr;
 680        int i;
 681
 682        rsnd_of_parse_dai(pdev, of_data, priv);
 683
 684        dai_nr = info->dai_info_nr;
 685        if (!dai_nr) {
 686                dev_err(dev, "no dai\n");
 687                return -EIO;
 688        }
 689
 690        drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
 691        rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
 692        if (!drv || !rdai) {
 693                dev_err(dev, "dai allocate failed\n");
 694                return -ENOMEM;
 695        }
 696
 697        priv->rdai_nr   = dai_nr;
 698        priv->daidrv    = drv;
 699        priv->rdai      = rdai;
 700
 701        for (i = 0; i < dai_nr; i++) {
 702
 703                pmod = info->dai_info[i].playback.ssi;
 704                cmod = info->dai_info[i].capture.ssi;
 705
 706                /*
 707                 *      init rsnd_dai
 708                 */
 709                snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
 710                rdai[i].priv = priv;
 711
 712                /*
 713                 *      init snd_soc_dai_driver
 714                 */
 715                drv[i].name     = rdai[i].name;
 716                drv[i].ops      = &rsnd_soc_dai_ops;
 717                if (pmod) {
 718                        snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE,
 719                                 "DAI%d Playback", i);
 720
 721                        drv[i].playback.rates           = RSND_RATES;
 722                        drv[i].playback.formats         = RSND_FMTS;
 723                        drv[i].playback.channels_min    = 2;
 724                        drv[i].playback.channels_max    = 2;
 725                        drv[i].playback.stream_name     = rdai[i].playback.name;
 726
 727                        rdai[i].playback.info = &info->dai_info[i].playback;
 728                        rdai[i].playback.rdai = rdai + i;
 729                        rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
 730                }
 731                if (cmod) {
 732                        snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE,
 733                                 "DAI%d Capture", i);
 734
 735                        drv[i].capture.rates            = RSND_RATES;
 736                        drv[i].capture.formats          = RSND_FMTS;
 737                        drv[i].capture.channels_min     = 2;
 738                        drv[i].capture.channels_max     = 2;
 739                        drv[i].capture.stream_name      = rdai[i].capture.name;
 740
 741                        rdai[i].capture.info = &info->dai_info[i].capture;
 742                        rdai[i].capture.rdai = rdai + i;
 743                        rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
 744                }
 745
 746                dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
 747                        pmod ? "play"    : " -- ",
 748                        cmod ? "capture" : "  --   ");
 749        }
 750
 751        return 0;
 752}
 753
 754/*
 755 *              pcm ops
 756 */
 757static struct snd_pcm_hardware rsnd_pcm_hardware = {
 758        .info =         SNDRV_PCM_INFO_INTERLEAVED      |
 759                        SNDRV_PCM_INFO_MMAP             |
 760                        SNDRV_PCM_INFO_MMAP_VALID,
 761        .buffer_bytes_max       = 64 * 1024,
 762        .period_bytes_min       = 32,
 763        .period_bytes_max       = 8192,
 764        .periods_min            = 1,
 765        .periods_max            = 32,
 766        .fifo_size              = 256,
 767};
 768
 769static int rsnd_pcm_open(struct snd_pcm_substream *substream)
 770{
 771        struct snd_pcm_runtime *runtime = substream->runtime;
 772        int ret = 0;
 773
 774        snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
 775
 776        ret = snd_pcm_hw_constraint_integer(runtime,
 777                                            SNDRV_PCM_HW_PARAM_PERIODS);
 778
 779        return ret;
 780}
 781
 782static int rsnd_hw_params(struct snd_pcm_substream *substream,
 783                         struct snd_pcm_hw_params *hw_params)
 784{
 785        struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
 786        struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
 787        struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
 788        int ret;
 789
 790        ret = rsnd_dai_call(hw_params, io, substream, hw_params);
 791        if (ret)
 792                return ret;
 793
 794        return snd_pcm_lib_malloc_pages(substream,
 795                                        params_buffer_bytes(hw_params));
 796}
 797
 798static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
 799{
 800        struct snd_pcm_runtime *runtime = substream->runtime;
 801        struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
 802        struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
 803        struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
 804
 805        return bytes_to_frames(runtime, io->byte_pos);
 806}
 807
 808static struct snd_pcm_ops rsnd_pcm_ops = {
 809        .open           = rsnd_pcm_open,
 810        .ioctl          = snd_pcm_lib_ioctl,
 811        .hw_params      = rsnd_hw_params,
 812        .hw_free        = snd_pcm_lib_free_pages,
 813        .pointer        = rsnd_pointer,
 814};
 815
 816/*
 817 *              snd_kcontrol
 818 */
 819#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
 820static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
 821                           struct snd_ctl_elem_info *uinfo)
 822{
 823        struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
 824
 825        if (cfg->texts) {
 826                uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 827                uinfo->count = cfg->size;
 828                uinfo->value.enumerated.items = cfg->max;
 829                if (uinfo->value.enumerated.item >= cfg->max)
 830                        uinfo->value.enumerated.item = cfg->max - 1;
 831                strlcpy(uinfo->value.enumerated.name,
 832                        cfg->texts[uinfo->value.enumerated.item],
 833                        sizeof(uinfo->value.enumerated.name));
 834        } else {
 835                uinfo->count = cfg->size;
 836                uinfo->value.integer.min = 0;
 837                uinfo->value.integer.max = cfg->max;
 838                uinfo->type = (cfg->max == 1) ?
 839                        SNDRV_CTL_ELEM_TYPE_BOOLEAN :
 840                        SNDRV_CTL_ELEM_TYPE_INTEGER;
 841        }
 842
 843        return 0;
 844}
 845
 846static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
 847                          struct snd_ctl_elem_value *uc)
 848{
 849        struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
 850        int i;
 851
 852        for (i = 0; i < cfg->size; i++)
 853                if (cfg->texts)
 854                        uc->value.enumerated.item[i] = cfg->val[i];
 855                else
 856                        uc->value.integer.value[i] = cfg->val[i];
 857
 858        return 0;
 859}
 860
 861static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
 862                          struct snd_ctl_elem_value *uc)
 863{
 864        struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
 865        struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
 866        int i, change = 0;
 867
 868        for (i = 0; i < cfg->size; i++) {
 869                if (cfg->texts) {
 870                        change |= (uc->value.enumerated.item[i] != cfg->val[i]);
 871                        cfg->val[i] = uc->value.enumerated.item[i];
 872                } else {
 873                        change |= (uc->value.integer.value[i] != cfg->val[i]);
 874                        cfg->val[i] = uc->value.integer.value[i];
 875                }
 876        }
 877
 878        if (change)
 879                cfg->update(cfg->io, mod);
 880
 881        return change;
 882}
 883
 884static int __rsnd_kctrl_new(struct rsnd_mod *mod,
 885                            struct rsnd_dai_stream *io,
 886                            struct snd_soc_pcm_runtime *rtd,
 887                            const unsigned char *name,
 888                            struct rsnd_kctrl_cfg *cfg,
 889                            void (*update)(struct rsnd_dai_stream *io,
 890                                           struct rsnd_mod *mod))
 891{
 892        struct snd_soc_card *soc_card = rtd->card;
 893        struct snd_card *card = rtd->card->snd_card;
 894        struct snd_kcontrol *kctrl;
 895        struct snd_kcontrol_new knew = {
 896                .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
 897                .name           = name,
 898                .info           = rsnd_kctrl_info,
 899                .index          = rtd - soc_card->rtd,
 900                .get            = rsnd_kctrl_get,
 901                .put            = rsnd_kctrl_put,
 902                .private_value  = (unsigned long)cfg,
 903        };
 904        int ret;
 905
 906        kctrl = snd_ctl_new1(&knew, mod);
 907        if (!kctrl)
 908                return -ENOMEM;
 909
 910        ret = snd_ctl_add(card, kctrl);
 911        if (ret < 0) {
 912                snd_ctl_free_one(kctrl);
 913                return ret;
 914        }
 915
 916        cfg->update = update;
 917        cfg->card = card;
 918        cfg->kctrl = kctrl;
 919        cfg->io = io;
 920
 921        return 0;
 922}
 923
 924void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
 925{
 926        snd_ctl_remove(cfg->card, cfg->kctrl);
 927}
 928
 929int rsnd_kctrl_new_m(struct rsnd_mod *mod,
 930                     struct rsnd_dai_stream *io,
 931                     struct snd_soc_pcm_runtime *rtd,
 932                     const unsigned char *name,
 933                     void (*update)(struct rsnd_dai_stream *io,
 934                                    struct rsnd_mod *mod),
 935                     struct rsnd_kctrl_cfg_m *_cfg,
 936                     u32 max)
 937{
 938        _cfg->cfg.max   = max;
 939        _cfg->cfg.size  = RSND_DVC_CHANNELS;
 940        _cfg->cfg.val   = _cfg->val;
 941        return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
 942}
 943
 944int rsnd_kctrl_new_s(struct rsnd_mod *mod,
 945                     struct rsnd_dai_stream *io,
 946                     struct snd_soc_pcm_runtime *rtd,
 947                     const unsigned char *name,
 948                     void (*update)(struct rsnd_dai_stream *io,
 949                                    struct rsnd_mod *mod),
 950                     struct rsnd_kctrl_cfg_s *_cfg,
 951                     u32 max)
 952{
 953        _cfg->cfg.max   = max;
 954        _cfg->cfg.size  = 1;
 955        _cfg->cfg.val   = &_cfg->val;
 956        return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
 957}
 958
 959int rsnd_kctrl_new_e(struct rsnd_mod *mod,
 960                     struct rsnd_dai_stream *io,
 961                     struct snd_soc_pcm_runtime *rtd,
 962                     const unsigned char *name,
 963                     struct rsnd_kctrl_cfg_s *_cfg,
 964                     void (*update)(struct rsnd_dai_stream *io,
 965                                    struct rsnd_mod *mod),
 966                     const char * const *texts,
 967                     u32 max)
 968{
 969        _cfg->cfg.max   = max;
 970        _cfg->cfg.size  = 1;
 971        _cfg->cfg.val   = &_cfg->val;
 972        _cfg->cfg.texts = texts;
 973        return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
 974}
 975
 976/*
 977 *              snd_soc_platform
 978 */
 979
 980#define PREALLOC_BUFFER         (32 * 1024)
 981#define PREALLOC_BUFFER_MAX     (32 * 1024)
 982
 983static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
 984{
 985        struct snd_soc_dai *dai = rtd->cpu_dai;
 986        struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
 987        int ret;
 988
 989        ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
 990        if (ret)
 991                return ret;
 992
 993        ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
 994        if (ret)
 995                return ret;
 996
 997        return snd_pcm_lib_preallocate_pages_for_all(
 998                rtd->pcm,
 999                SNDRV_DMA_TYPE_DEV,
1000                rtd->card->snd_card->dev,
1001                PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1002}
1003
1004static struct snd_soc_platform_driver rsnd_soc_platform = {
1005        .ops            = &rsnd_pcm_ops,
1006        .pcm_new        = rsnd_pcm_new,
1007};
1008
1009static const struct snd_soc_component_driver rsnd_soc_component = {
1010        .name           = "rsnd",
1011};
1012
1013static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1014                                       struct rsnd_dai_stream *io)
1015{
1016        int ret;
1017
1018        ret = rsnd_dai_call(probe, io, priv);
1019        if (ret == -EAGAIN) {
1020                /*
1021                 * Fallback to PIO mode
1022                 */
1023
1024                /*
1025                 * call "remove" for SSI/SRC/DVC
1026                 * SSI will be switch to PIO mode if it was DMA mode
1027                 * see
1028                 *      rsnd_dma_init()
1029                 *      rsnd_ssi_fallback()
1030                 */
1031                rsnd_dai_call(remove, io, priv);
1032
1033                /*
1034                 * remove SRC/DVC from DAI,
1035                 */
1036                rsnd_path_break(priv, io, src);
1037                rsnd_path_break(priv, io, dvc);
1038
1039                /*
1040                 * fallback
1041                 */
1042                rsnd_dai_call(fallback, io, priv);
1043
1044                /*
1045                 * retry to "probe".
1046                 * DAI has SSI which is PIO mode only now.
1047                 */
1048                ret = rsnd_dai_call(probe, io, priv);
1049        }
1050
1051        return ret;
1052}
1053
1054/*
1055 *      rsnd probe
1056 */
1057static int rsnd_probe(struct platform_device *pdev)
1058{
1059        struct rcar_snd_info *info;
1060        struct rsnd_priv *priv;
1061        struct device *dev = &pdev->dev;
1062        struct rsnd_dai *rdai;
1063        const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1064        const struct rsnd_of_data *of_data;
1065        int (*probe_func[])(struct platform_device *pdev,
1066                            const struct rsnd_of_data *of_data,
1067                            struct rsnd_priv *priv) = {
1068                rsnd_gen_probe,
1069                rsnd_dma_probe,
1070                rsnd_ssi_probe,
1071                rsnd_src_probe,
1072                rsnd_dvc_probe,
1073                rsnd_adg_probe,
1074                rsnd_dai_probe,
1075        };
1076        int ret, i;
1077
1078        info = NULL;
1079        of_data = NULL;
1080        if (of_id) {
1081                info = devm_kzalloc(&pdev->dev,
1082                                    sizeof(struct rcar_snd_info), GFP_KERNEL);
1083                of_data = of_id->data;
1084        } else {
1085                info = pdev->dev.platform_data;
1086        }
1087
1088        if (!info) {
1089                dev_err(dev, "driver needs R-Car sound information\n");
1090                return -ENODEV;
1091        }
1092
1093        /*
1094         *      init priv data
1095         */
1096        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1097        if (!priv) {
1098                dev_err(dev, "priv allocate failed\n");
1099                return -ENODEV;
1100        }
1101
1102        priv->pdev      = pdev;
1103        priv->info      = info;
1104        spin_lock_init(&priv->lock);
1105
1106        /*
1107         *      init each module
1108         */
1109        for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1110                ret = probe_func[i](pdev, of_data, priv);
1111                if (ret)
1112                        return ret;
1113        }
1114
1115        for_each_rsnd_dai(rdai, priv, i) {
1116                ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1117                if (ret)
1118                        goto exit_snd_probe;
1119
1120                ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1121                if (ret)
1122                        goto exit_snd_probe;
1123        }
1124
1125        dev_set_drvdata(dev, priv);
1126
1127        /*
1128         *      asoc register
1129         */
1130        ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1131        if (ret < 0) {
1132                dev_err(dev, "cannot snd soc register\n");
1133                return ret;
1134        }
1135
1136        ret = snd_soc_register_component(dev, &rsnd_soc_component,
1137                                         priv->daidrv, rsnd_rdai_nr(priv));
1138        if (ret < 0) {
1139                dev_err(dev, "cannot snd dai register\n");
1140                goto exit_snd_soc;
1141        }
1142
1143        pm_runtime_enable(dev);
1144
1145        dev_info(dev, "probed\n");
1146        return ret;
1147
1148exit_snd_soc:
1149        snd_soc_unregister_platform(dev);
1150exit_snd_probe:
1151        for_each_rsnd_dai(rdai, priv, i) {
1152                rsnd_dai_call(remove, &rdai->playback, priv);
1153                rsnd_dai_call(remove, &rdai->capture, priv);
1154        }
1155
1156        return ret;
1157}
1158
1159static int rsnd_remove(struct platform_device *pdev)
1160{
1161        struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1162        struct rsnd_dai *rdai;
1163        void (*remove_func[])(struct platform_device *pdev,
1164                              struct rsnd_priv *priv) = {
1165                rsnd_ssi_remove,
1166                rsnd_src_remove,
1167                rsnd_dvc_remove,
1168        };
1169        int ret = 0, i;
1170
1171        pm_runtime_disable(&pdev->dev);
1172
1173        for_each_rsnd_dai(rdai, priv, i) {
1174                ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1175                ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1176        }
1177
1178        for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1179                remove_func[i](pdev, priv);
1180
1181        snd_soc_unregister_component(&pdev->dev);
1182        snd_soc_unregister_platform(&pdev->dev);
1183
1184        return ret;
1185}
1186
1187static struct platform_driver rsnd_driver = {
1188        .driver = {
1189                .name   = "rcar_sound",
1190                .of_match_table = rsnd_of_match,
1191        },
1192        .probe          = rsnd_probe,
1193        .remove         = rsnd_remove,
1194};
1195module_platform_driver(rsnd_driver);
1196
1197MODULE_LICENSE("GPL");
1198MODULE_DESCRIPTION("Renesas R-Car audio driver");
1199MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1200MODULE_ALIAS("platform:rcar-pcm-audio");
1201