linux/sound/soc/sh/rcar/src.c
<<
>>
Prefs
   1/*
   2 * Renesas R-Car SRC support
   3 *
   4 * Copyright (C) 2013 Renesas Solutions Corp.
   5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11#include "rsnd.h"
  12
  13#define SRC_NAME "src"
  14
  15/* SRCx_STATUS */
  16#define OUF_SRCO        ((1 << 12) | (1 << 13))
  17#define OUF_SRCI        ((1 <<  9) | (1 <<  8))
  18
  19/* SCU_SYSTEM_STATUS0/1 */
  20#define OUF_SRC(id)     ((1 << (id + 16)) | (1 << id))
  21
  22struct rsnd_src {
  23        struct rsnd_mod mod;
  24        struct rsnd_mod *dma;
  25        struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
  26        struct rsnd_kctrl_cfg_s sync; /* sync convert */
  27        u32 convert_rate; /* sampling rate convert */
  28        int err;
  29        int irq;
  30};
  31
  32#define RSND_SRC_NAME_SIZE 16
  33
  34#define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
  35#define rsnd_src_to_dma(src) ((src)->dma)
  36#define rsnd_src_nr(priv) ((priv)->src_nr)
  37#define rsnd_enable_sync_convert(src) ((src)->sen.val)
  38
  39#define rsnd_mod_to_src(_mod)                           \
  40        container_of((_mod), struct rsnd_src, mod)
  41
  42#define for_each_rsnd_src(pos, priv, i)                         \
  43        for ((i) = 0;                                           \
  44             ((i) < rsnd_src_nr(priv)) &&                       \
  45             ((pos) = (struct rsnd_src *)(priv)->src + i);      \
  46             i++)
  47
  48
  49/*
  50 *              image of SRC (Sampling Rate Converter)
  51 *
  52 * 96kHz   <-> +-----+  48kHz   +-----+  48kHz  +-------+
  53 * 48kHz   <-> | SRC | <------> | SSI | <-----> | codec |
  54 * 44.1kHz <-> +-----+          +-----+         +-------+
  55 * ...
  56 *
  57 */
  58
  59/*
  60 * src.c is caring...
  61 *
  62 * Gen1
  63 *
  64 * [mem] -> [SRU] -> [SSI]
  65 *        |--------|
  66 *
  67 * Gen2
  68 *
  69 * [mem] -> [SRC] -> [SSIU] -> [SSI]
  70 *        |-----------------|
  71 */
  72
  73static void rsnd_src_activation(struct rsnd_mod *mod)
  74{
  75        rsnd_mod_write(mod, SRC_SWRSR, 0);
  76        rsnd_mod_write(mod, SRC_SWRSR, 1);
  77}
  78
  79static void rsnd_src_halt(struct rsnd_mod *mod)
  80{
  81        rsnd_mod_write(mod, SRC_SRCIR, 1);
  82        rsnd_mod_write(mod, SRC_SWRSR, 0);
  83}
  84
  85static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
  86                                         struct rsnd_mod *mod)
  87{
  88        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  89        int is_play = rsnd_io_is_play(io);
  90
  91        return rsnd_dma_request_channel(rsnd_src_of_node(priv),
  92                                        mod,
  93                                        is_play ? "rx" : "tx");
  94}
  95
  96static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
  97                                 struct rsnd_src *src)
  98{
  99        struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
 100        u32 convert_rate;
 101
 102        if (!runtime)
 103                return 0;
 104
 105        if (!rsnd_enable_sync_convert(src))
 106                return src->convert_rate;
 107
 108        convert_rate = src->sync.val;
 109
 110        if (!convert_rate)
 111                convert_rate = src->convert_rate;
 112
 113        if (!convert_rate)
 114                convert_rate = runtime->rate;
 115
 116        return convert_rate;
 117}
 118
 119unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
 120                                   struct rsnd_dai_stream *io,
 121                                   struct snd_pcm_runtime *runtime)
 122{
 123        struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
 124        struct rsnd_src *src;
 125        unsigned int rate = 0;
 126
 127        if (src_mod) {
 128                src = rsnd_mod_to_src(src_mod);
 129
 130                /*
 131                 * return convert rate if SRC is used,
 132                 * otherwise, return runtime->rate as usual
 133                 */
 134                rate = rsnd_src_convert_rate(io, src);
 135        }
 136
 137        if (!rate)
 138                rate = runtime->rate;
 139
 140        return rate;
 141}
 142
 143static int rsnd_src_hw_params(struct rsnd_mod *mod,
 144                              struct rsnd_dai_stream *io,
 145                              struct snd_pcm_substream *substream,
 146                              struct snd_pcm_hw_params *fe_params)
 147{
 148        struct rsnd_src *src = rsnd_mod_to_src(mod);
 149        struct snd_soc_pcm_runtime *fe = substream->private_data;
 150
 151        /*
 152         * SRC assumes that it is used under DPCM if user want to use
 153         * sampling rate convert. Then, SRC should be FE.
 154         * And then, this function will be called *after* BE settings.
 155         * this means, each BE already has fixuped hw_params.
 156         * see
 157         *      dpcm_fe_dai_hw_params()
 158         *      dpcm_be_dai_hw_params()
 159         */
 160        if (fe->dai_link->dynamic) {
 161                int stream = substream->stream;
 162                struct snd_soc_dpcm *dpcm;
 163                struct snd_pcm_hw_params *be_params;
 164
 165                list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
 166                        be_params = &dpcm->hw_params;
 167
 168                        if (params_rate(fe_params) != params_rate(be_params))
 169                                src->convert_rate = params_rate(be_params);
 170                }
 171        }
 172
 173        return 0;
 174}
 175
 176static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
 177                                      struct rsnd_mod *mod)
 178{
 179        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 180        struct device *dev = rsnd_priv_to_dev(priv);
 181        struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
 182        struct rsnd_src *src = rsnd_mod_to_src(mod);
 183        u32 convert_rate = rsnd_src_convert_rate(io, src);
 184        u32 ifscr, fsrate, adinr;
 185        u32 cr, route;
 186        u32 bsdsr, bsisr;
 187        uint ratio;
 188
 189        if (!runtime)
 190                return;
 191
 192        /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
 193        if (!convert_rate)
 194                ratio = 0;
 195        else if (convert_rate > runtime->rate)
 196                ratio = 100 * convert_rate / runtime->rate;
 197        else
 198                ratio = 100 * runtime->rate / convert_rate;
 199
 200        if (ratio > 600) {
 201                dev_err(dev, "FSO/FSI ratio error\n");
 202                return;
 203        }
 204
 205        /*
 206         *      SRC_ADINR
 207         */
 208        adinr = rsnd_get_adinr_bit(mod, io) |
 209                rsnd_get_adinr_chan(mod, io);
 210
 211        /*
 212         *      SRC_IFSCR / SRC_IFSVR
 213         */
 214        ifscr = 0;
 215        fsrate = 0;
 216        if (convert_rate) {
 217                ifscr = 1;
 218                fsrate = 0x0400000 / convert_rate * runtime->rate;
 219        }
 220
 221        /*
 222         *      SRC_SRCCR / SRC_ROUTE_MODE0
 223         */
 224        cr      = 0x00011110;
 225        route   = 0x0;
 226        if (convert_rate) {
 227                route   = 0x1;
 228
 229                if (rsnd_enable_sync_convert(src)) {
 230                        cr |= 0x1;
 231                        route |= rsnd_io_is_play(io) ?
 232                                (0x1 << 24) : (0x1 << 25);
 233                }
 234        }
 235
 236        /*
 237         * SRC_BSDSR / SRC_BSISR
 238         */
 239        switch (rsnd_mod_id(mod)) {
 240        case 5:
 241        case 6:
 242        case 7:
 243        case 8:
 244                bsdsr = 0x02400000; /* 6 - 1/6 */
 245                bsisr = 0x00100060; /* 6 - 1/6 */
 246                break;
 247        default:
 248                bsdsr = 0x01800000; /* 6 - 1/6 */
 249                bsisr = 0x00100060 ;/* 6 - 1/6 */
 250                break;
 251        }
 252
 253        rsnd_mod_write(mod, SRC_SRCIR, 1);      /* initialize */
 254        rsnd_mod_write(mod, SRC_ADINR, adinr);
 255        rsnd_mod_write(mod, SRC_IFSCR, ifscr);
 256        rsnd_mod_write(mod, SRC_IFSVR, fsrate);
 257        rsnd_mod_write(mod, SRC_SRCCR, cr);
 258        rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
 259        rsnd_mod_write(mod, SRC_BSISR, bsisr);
 260        rsnd_mod_write(mod, SRC_SRCIR, 0);      /* cancel initialize */
 261
 262        rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
 263        rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
 264        rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
 265        rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
 266
 267        if (convert_rate)
 268                rsnd_adg_set_convert_clk_gen2(mod, io,
 269                                              runtime->rate,
 270                                              convert_rate);
 271        else
 272                rsnd_adg_set_convert_timing_gen2(mod, io);
 273}
 274
 275#define rsnd_src_irq_enable(mod)  rsnd_src_irq_ctrol(mod, 1)
 276#define rsnd_src_irq_disable(mod) rsnd_src_irq_ctrol(mod, 0)
 277static void rsnd_src_irq_ctrol(struct rsnd_mod *mod, int enable)
 278{
 279        struct rsnd_src *src = rsnd_mod_to_src(mod);
 280        u32 sys_int_val, int_val, sys_int_mask;
 281        int irq = src->irq;
 282        int id = rsnd_mod_id(mod);
 283
 284        sys_int_val =
 285        sys_int_mask = OUF_SRC(id);
 286        int_val = 0x3300;
 287
 288        /*
 289         * IRQ is not supported on non-DT
 290         * see
 291         *      rsnd_src_probe_()
 292         */
 293        if ((irq <= 0) || !enable) {
 294                sys_int_val = 0;
 295                int_val = 0;
 296        }
 297
 298        /*
 299         * WORKAROUND
 300         *
 301         * ignore over flow error when rsnd_enable_sync_convert()
 302         */
 303        if (rsnd_enable_sync_convert(src))
 304                sys_int_val = sys_int_val & 0xffff;
 305
 306        rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
 307        rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
 308        rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
 309}
 310
 311static void rsnd_src_status_clear(struct rsnd_mod *mod)
 312{
 313        u32 val = OUF_SRC(rsnd_mod_id(mod));
 314
 315        rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
 316        rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
 317}
 318
 319static bool rsnd_src_record_error(struct rsnd_mod *mod)
 320{
 321        struct rsnd_src *src = rsnd_mod_to_src(mod);
 322        u32 val0, val1;
 323        bool ret = false;
 324
 325        val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
 326
 327        /*
 328         * WORKAROUND
 329         *
 330         * ignore over flow error when rsnd_enable_sync_convert()
 331         */
 332        if (rsnd_enable_sync_convert(src))
 333                val0 = val0 & 0xffff;
 334
 335        if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
 336            (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
 337                struct rsnd_src *src = rsnd_mod_to_src(mod);
 338
 339                src->err++;
 340                ret = true;
 341        }
 342
 343        return ret;
 344}
 345
 346static int rsnd_src_start(struct rsnd_mod *mod,
 347                          struct rsnd_dai_stream *io,
 348                          struct rsnd_priv *priv)
 349{
 350        struct rsnd_src *src = rsnd_mod_to_src(mod);
 351        u32 val;
 352
 353        /*
 354         * WORKAROUND
 355         *
 356         * Enable SRC output if you want to use sync convert together with DVC
 357         */
 358        val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
 359                0x01 : 0x11;
 360
 361        rsnd_mod_write(mod, SRC_CTRL, val);
 362
 363        return 0;
 364}
 365
 366static int rsnd_src_stop(struct rsnd_mod *mod,
 367                         struct rsnd_dai_stream *io,
 368                         struct rsnd_priv *priv)
 369{
 370        /*
 371         * stop SRC output only
 372         * see rsnd_src_quit
 373         */
 374        rsnd_mod_write(mod, SRC_CTRL, 0x01);
 375
 376        return 0;
 377}
 378
 379static int rsnd_src_init(struct rsnd_mod *mod,
 380                         struct rsnd_dai_stream *io,
 381                         struct rsnd_priv *priv)
 382{
 383        struct rsnd_src *src = rsnd_mod_to_src(mod);
 384
 385        rsnd_mod_power_on(mod);
 386
 387        rsnd_src_activation(mod);
 388
 389        rsnd_src_set_convert_rate(io, mod);
 390
 391        rsnd_src_status_clear(mod);
 392
 393        rsnd_src_irq_enable(mod);
 394
 395        src->err = 0;
 396
 397        /* reset sync convert_rate */
 398        src->sync.val = 0;
 399
 400        return 0;
 401}
 402
 403static int rsnd_src_quit(struct rsnd_mod *mod,
 404                         struct rsnd_dai_stream *io,
 405                         struct rsnd_priv *priv)
 406{
 407        struct rsnd_src *src = rsnd_mod_to_src(mod);
 408        struct device *dev = rsnd_priv_to_dev(priv);
 409
 410        rsnd_src_irq_disable(mod);
 411
 412        /* stop both out/in */
 413        rsnd_mod_write(mod, SRC_CTRL, 0);
 414
 415        rsnd_src_halt(mod);
 416
 417        rsnd_mod_power_off(mod);
 418
 419        if (src->err)
 420                dev_warn(dev, "%s[%d] under/over flow err = %d\n",
 421                         rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
 422
 423        src->convert_rate = 0;
 424
 425        /* reset sync convert_rate */
 426        src->sync.val = 0;
 427
 428        return 0;
 429}
 430
 431static void __rsnd_src_interrupt(struct rsnd_mod *mod,
 432                                 struct rsnd_dai_stream *io)
 433{
 434        struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
 435        struct rsnd_src *src = rsnd_mod_to_src(mod);
 436        struct device *dev = rsnd_priv_to_dev(priv);
 437
 438        spin_lock(&priv->lock);
 439
 440        /* ignore all cases if not working */
 441        if (!rsnd_io_is_working(io))
 442                goto rsnd_src_interrupt_out;
 443
 444        if (rsnd_src_record_error(mod)) {
 445
 446                dev_dbg(dev, "%s[%d] restart\n",
 447                        rsnd_mod_name(mod), rsnd_mod_id(mod));
 448
 449                rsnd_src_stop(mod, io, priv);
 450                rsnd_src_start(mod, io, priv);
 451        }
 452
 453        if (src->err > 1024) {
 454                rsnd_src_irq_disable(mod);
 455
 456                dev_warn(dev, "no more %s[%d] restart\n",
 457                         rsnd_mod_name(mod), rsnd_mod_id(mod));
 458        }
 459
 460        rsnd_src_status_clear(mod);
 461rsnd_src_interrupt_out:
 462
 463        spin_unlock(&priv->lock);
 464}
 465
 466static irqreturn_t rsnd_src_interrupt(int irq, void *data)
 467{
 468        struct rsnd_mod *mod = data;
 469
 470        rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
 471
 472        return IRQ_HANDLED;
 473}
 474
 475static int rsnd_src_probe_(struct rsnd_mod *mod,
 476                           struct rsnd_dai_stream *io,
 477                           struct rsnd_priv *priv)
 478{
 479        struct rsnd_src *src = rsnd_mod_to_src(mod);
 480        struct device *dev = rsnd_priv_to_dev(priv);
 481        int irq = src->irq;
 482        int ret;
 483
 484        if (irq > 0) {
 485                /*
 486                 * IRQ is not supported on non-DT
 487                 * see
 488                 *      rsnd_src_irq_enable()
 489                 */
 490                ret = devm_request_irq(dev, irq,
 491                                       rsnd_src_interrupt,
 492                                       IRQF_SHARED,
 493                                       dev_name(dev), mod);
 494                if (ret)
 495                        return ret;
 496        }
 497
 498        src->dma = rsnd_dma_attach(io, mod, 0);
 499        if (IS_ERR(src->dma))
 500                return PTR_ERR(src->dma);
 501
 502        return ret;
 503}
 504
 505static int rsnd_src_pcm_new(struct rsnd_mod *mod,
 506                            struct rsnd_dai_stream *io,
 507                            struct snd_soc_pcm_runtime *rtd)
 508{
 509        struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
 510        struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
 511        struct rsnd_src *src = rsnd_mod_to_src(mod);
 512        int ret;
 513
 514        /*
 515         * enable SRC sync convert if possible
 516         */
 517
 518        /*
 519         * SRC sync convert needs clock master
 520         */
 521        if (!rsnd_rdai_is_clk_master(rdai))
 522                return 0;
 523
 524        /*
 525         * SRC In doesn't work if DVC was enabled
 526         */
 527        if (dvc && !rsnd_io_is_play(io))
 528                return 0;
 529
 530        /*
 531         * enable sync convert
 532         */
 533        ret = rsnd_kctrl_new_s(mod, io, rtd,
 534                               rsnd_io_is_play(io) ?
 535                               "SRC Out Rate Switch" :
 536                               "SRC In Rate Switch",
 537                               rsnd_src_set_convert_rate,
 538                               &src->sen, 1);
 539        if (ret < 0)
 540                return ret;
 541
 542        ret = rsnd_kctrl_new_s(mod, io, rtd,
 543                               rsnd_io_is_play(io) ?
 544                               "SRC Out Rate" :
 545                               "SRC In Rate",
 546                               rsnd_src_set_convert_rate,
 547                               &src->sync, 192000);
 548
 549        return ret;
 550}
 551
 552static struct rsnd_mod_ops rsnd_src_ops = {
 553        .name   = SRC_NAME,
 554        .dma_req = rsnd_src_dma_req,
 555        .probe  = rsnd_src_probe_,
 556        .init   = rsnd_src_init,
 557        .quit   = rsnd_src_quit,
 558        .start  = rsnd_src_start,
 559        .stop   = rsnd_src_stop,
 560        .hw_params = rsnd_src_hw_params,
 561        .pcm_new = rsnd_src_pcm_new,
 562};
 563
 564struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
 565{
 566        if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
 567                id = 0;
 568
 569        return rsnd_mod_get(rsnd_src_get(priv, id));
 570}
 571
 572int rsnd_src_probe(struct rsnd_priv *priv)
 573{
 574        struct device_node *node;
 575        struct device_node *np;
 576        struct device *dev = rsnd_priv_to_dev(priv);
 577        struct rsnd_src *src;
 578        struct clk *clk;
 579        char name[RSND_SRC_NAME_SIZE];
 580        int i, nr, ret;
 581
 582        /* This driver doesn't support Gen1 at this point */
 583        if (rsnd_is_gen1(priv))
 584                return 0;
 585
 586        node = rsnd_src_of_node(priv);
 587        if (!node)
 588                return 0; /* not used is not error */
 589
 590        nr = of_get_child_count(node);
 591        if (!nr) {
 592                ret = -EINVAL;
 593                goto rsnd_src_probe_done;
 594        }
 595
 596        src     = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
 597        if (!src) {
 598                ret = -ENOMEM;
 599                goto rsnd_src_probe_done;
 600        }
 601
 602        priv->src_nr    = nr;
 603        priv->src       = src;
 604
 605        i = 0;
 606        for_each_child_of_node(node, np) {
 607                src = rsnd_src_get(priv, i);
 608
 609                snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
 610                         SRC_NAME, i);
 611
 612                src->irq = irq_of_parse_and_map(np, 0);
 613                if (!src->irq) {
 614                        ret = -EINVAL;
 615                        goto rsnd_src_probe_done;
 616                }
 617
 618                clk = devm_clk_get(dev, name);
 619                if (IS_ERR(clk)) {
 620                        ret = PTR_ERR(clk);
 621                        goto rsnd_src_probe_done;
 622                }
 623
 624                ret = rsnd_mod_init(priv, rsnd_mod_get(src),
 625                                    &rsnd_src_ops, clk, RSND_MOD_SRC, i);
 626                if (ret)
 627                        goto rsnd_src_probe_done;
 628
 629                i++;
 630        }
 631
 632        ret = 0;
 633
 634rsnd_src_probe_done:
 635        of_node_put(node);
 636
 637        return ret;
 638}
 639
 640void rsnd_src_remove(struct rsnd_priv *priv)
 641{
 642        struct rsnd_src *src;
 643        int i;
 644
 645        for_each_rsnd_src(src, priv, i) {
 646                rsnd_mod_quit(rsnd_mod_get(src));
 647        }
 648}
 649