linux/sound/soc/pxa/mmp-sspa.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * linux/sound/soc/pxa/mmp-sspa.c
   4 * Base on pxa2xx-ssp.c
   5 *
   6 * Copyright (C) 2011 Marvell International Ltd.
   7 */
   8#include <linux/init.h>
   9#include <linux/module.h>
  10#include <linux/platform_device.h>
  11#include <linux/delay.h>
  12#include <linux/clk.h>
  13#include <linux/slab.h>
  14#include <linux/io.h>
  15#include <linux/dmaengine.h>
  16#include <linux/pm_runtime.h>
  17
  18#include <sound/core.h>
  19#include <sound/pcm.h>
  20#include <sound/initval.h>
  21#include <sound/pcm_params.h>
  22#include <sound/soc.h>
  23#include <sound/pxa2xx-lib.h>
  24#include <sound/dmaengine_pcm.h>
  25#include "mmp-sspa.h"
  26
  27/*
  28 * SSPA audio private data
  29 */
  30struct sspa_priv {
  31        void __iomem *tx_base;
  32        void __iomem *rx_base;
  33
  34        struct snd_dmaengine_dai_dma_data playback_dma_data;
  35        struct snd_dmaengine_dai_dma_data capture_dma_data;
  36        struct clk *clk;
  37        struct clk *audio_clk;
  38        struct clk *sysclk;
  39
  40        int running_cnt;
  41        u32 sp;
  42        u32 ctrl;
  43};
  44
  45static void mmp_sspa_tx_enable(struct sspa_priv *sspa)
  46{
  47        unsigned int sspa_sp = sspa->sp;
  48
  49        sspa_sp &= ~SSPA_SP_MSL;
  50        sspa_sp |= SSPA_SP_S_EN;
  51        sspa_sp |= SSPA_SP_WEN;
  52        __raw_writel(sspa_sp, sspa->tx_base + SSPA_SP);
  53}
  54
  55static void mmp_sspa_tx_disable(struct sspa_priv *sspa)
  56{
  57        unsigned int sspa_sp = sspa->sp;
  58
  59        sspa_sp &= ~SSPA_SP_MSL;
  60        sspa_sp &= ~SSPA_SP_S_EN;
  61        sspa_sp |= SSPA_SP_WEN;
  62        __raw_writel(sspa_sp, sspa->tx_base + SSPA_SP);
  63}
  64
  65static void mmp_sspa_rx_enable(struct sspa_priv *sspa)
  66{
  67        unsigned int sspa_sp = sspa->sp;
  68
  69        sspa_sp |= SSPA_SP_S_EN;
  70        sspa_sp |= SSPA_SP_WEN;
  71        __raw_writel(sspa_sp, sspa->rx_base + SSPA_SP);
  72}
  73
  74static void mmp_sspa_rx_disable(struct sspa_priv *sspa)
  75{
  76        unsigned int sspa_sp = sspa->sp;
  77
  78        sspa_sp &= ~SSPA_SP_S_EN;
  79        sspa_sp |= SSPA_SP_WEN;
  80        __raw_writel(sspa_sp, sspa->rx_base + SSPA_SP);
  81}
  82
  83static int mmp_sspa_startup(struct snd_pcm_substream *substream,
  84        struct snd_soc_dai *dai)
  85{
  86        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(dai);
  87
  88        clk_prepare_enable(sspa->sysclk);
  89        clk_prepare_enable(sspa->clk);
  90
  91        return 0;
  92}
  93
  94static void mmp_sspa_shutdown(struct snd_pcm_substream *substream,
  95        struct snd_soc_dai *dai)
  96{
  97        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(dai);
  98
  99        clk_disable_unprepare(sspa->clk);
 100        clk_disable_unprepare(sspa->sysclk);
 101}
 102
 103/*
 104 * Set the SSP ports SYSCLK.
 105 */
 106static int mmp_sspa_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
 107                                    int clk_id, unsigned int freq, int dir)
 108{
 109        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(cpu_dai);
 110        struct device *dev = cpu_dai->component->dev;
 111        int ret = 0;
 112
 113        if (dev->of_node)
 114                return -ENOTSUPP;
 115
 116        switch (clk_id) {
 117        case MMP_SSPA_CLK_AUDIO:
 118                ret = clk_set_rate(sspa->audio_clk, freq);
 119                if (ret)
 120                        return ret;
 121                break;
 122        case MMP_SSPA_CLK_PLL:
 123        case MMP_SSPA_CLK_VCXO:
 124                /* not support yet */
 125                return -EINVAL;
 126        default:
 127                return -EINVAL;
 128        }
 129
 130        return 0;
 131}
 132
 133static int mmp_sspa_set_dai_pll(struct snd_soc_dai *cpu_dai, int pll_id,
 134                                 int source, unsigned int freq_in,
 135                                 unsigned int freq_out)
 136{
 137        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(cpu_dai);
 138        struct device *dev = cpu_dai->component->dev;
 139        int ret = 0;
 140
 141        if (dev->of_node)
 142                return -ENOTSUPP;
 143
 144        switch (pll_id) {
 145        case MMP_SYSCLK:
 146                ret = clk_set_rate(sspa->sysclk, freq_out);
 147                if (ret)
 148                        return ret;
 149                break;
 150        case MMP_SSPA_CLK:
 151                ret = clk_set_rate(sspa->clk, freq_out);
 152                if (ret)
 153                        return ret;
 154                break;
 155        default:
 156                return -ENODEV;
 157        }
 158
 159        return 0;
 160}
 161
 162/*
 163 * Set up the sspa dai format.
 164 */
 165static int mmp_sspa_set_dai_fmt(struct snd_soc_dai *cpu_dai,
 166                                 unsigned int fmt)
 167{
 168        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(cpu_dai);
 169
 170        /* reset port settings */
 171        sspa->sp   = SSPA_SP_WEN | SSPA_SP_S_RST | SSPA_SP_FFLUSH;
 172        sspa->ctrl = 0;
 173
 174        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 175        case SND_SOC_DAIFMT_CBS_CFS:
 176                sspa->sp |= SSPA_SP_MSL;
 177                break;
 178        case SND_SOC_DAIFMT_CBM_CFM:
 179                break;
 180        default:
 181                return -EINVAL;
 182        }
 183
 184        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 185        case SND_SOC_DAIFMT_NB_NF:
 186                sspa->sp |= SSPA_SP_FSP;
 187                break;
 188        default:
 189                return -EINVAL;
 190        }
 191
 192        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 193        case SND_SOC_DAIFMT_I2S:
 194                sspa->ctrl |= SSPA_CTL_XDATDLY(1);
 195                break;
 196        default:
 197                return -EINVAL;
 198        }
 199
 200        /* Since we are configuring the timings for the format by hand
 201         * we have to defer some things until hw_params() where we
 202         * know parameters like the sample size.
 203         */
 204        return 0;
 205}
 206
 207/*
 208 * Set the SSPA audio DMA parameters and sample size.
 209 * Can be called multiple times by oss emulation.
 210 */
 211static int mmp_sspa_hw_params(struct snd_pcm_substream *substream,
 212                               struct snd_pcm_hw_params *params,
 213                               struct snd_soc_dai *dai)
 214{
 215        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(dai);
 216        struct device *dev = dai->component->dev;
 217        u32 sspa_ctrl = sspa->ctrl;
 218        int bits;
 219        int bitval;
 220
 221        switch (params_format(params)) {
 222        case SNDRV_PCM_FORMAT_S8:
 223                bits = 8;
 224                bitval = SSPA_CTL_8_BITS;
 225                break;
 226        case SNDRV_PCM_FORMAT_S16_LE:
 227                bits = 16;
 228                bitval = SSPA_CTL_16_BITS;
 229                break;
 230        case SNDRV_PCM_FORMAT_S24_3LE:
 231                bits = 24;
 232                bitval = SSPA_CTL_24_BITS;
 233                break;
 234        case SNDRV_PCM_FORMAT_S32_LE:
 235                bits = 32;
 236                bitval = SSPA_CTL_32_BITS;
 237                break;
 238        default:
 239                return -EINVAL;
 240        }
 241
 242        if (dev->of_node || params_channels(params) == 2)
 243                sspa_ctrl |= SSPA_CTL_XPH;
 244
 245        sspa_ctrl &= ~SSPA_CTL_XWDLEN1_MASK;
 246        sspa_ctrl |= SSPA_CTL_XWDLEN1(bitval);
 247
 248        sspa_ctrl &= ~SSPA_CTL_XSSZ1_MASK;
 249        sspa_ctrl |= SSPA_CTL_XSSZ1(bitval);
 250
 251        sspa_ctrl &= ~SSPA_CTL_XSSZ2_MASK;
 252        sspa_ctrl |= SSPA_CTL_XSSZ2(bitval);
 253
 254        sspa->sp &= ~SSPA_SP_FWID_MASK;
 255        sspa->sp |= SSPA_SP_FWID(bits - 1);
 256
 257        sspa->sp &= ~SSPA_TXSP_FPER_MASK;
 258        sspa->sp |= SSPA_TXSP_FPER(bits * 2 - 1);
 259
 260        if (dev->of_node) {
 261                clk_set_rate(sspa->clk, params_rate(params) *
 262                                        params_channels(params) * bits);
 263        }
 264
 265        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 266                __raw_writel(sspa_ctrl, sspa->tx_base + SSPA_CTL);
 267                __raw_writel(0x1, sspa->tx_base + SSPA_FIFO_UL);
 268        } else {
 269                __raw_writel(sspa_ctrl, sspa->rx_base + SSPA_CTL);
 270                __raw_writel(0x0, sspa->rx_base + SSPA_FIFO_UL);
 271        }
 272
 273        return 0;
 274}
 275
 276static int mmp_sspa_trigger(struct snd_pcm_substream *substream, int cmd,
 277                             struct snd_soc_dai *dai)
 278{
 279        struct sspa_priv *sspa = snd_soc_dai_get_drvdata(dai);
 280        int ret = 0;
 281
 282        switch (cmd) {
 283        case SNDRV_PCM_TRIGGER_START:
 284        case SNDRV_PCM_TRIGGER_RESUME:
 285        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 286                /*
 287                 * whatever playback or capture, must enable rx.
 288                 * this is a hw issue, so need check if rx has been
 289                 * enabled or not; if has been enabled by another
 290                 * stream, do not enable again.
 291                 */
 292                if (!sspa->running_cnt)
 293                        mmp_sspa_rx_enable(sspa);
 294
 295                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 296                        mmp_sspa_tx_enable(sspa);
 297
 298                sspa->running_cnt++;
 299                break;
 300
 301        case SNDRV_PCM_TRIGGER_STOP:
 302        case SNDRV_PCM_TRIGGER_SUSPEND:
 303        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 304                sspa->running_cnt--;
 305
 306                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 307                        mmp_sspa_tx_disable(sspa);
 308
 309                /* have no capture stream, disable rx port */
 310                if (!sspa->running_cnt)
 311                        mmp_sspa_rx_disable(sspa);
 312                break;
 313
 314        default:
 315                ret = -EINVAL;
 316        }
 317
 318        return ret;
 319}
 320
 321static int mmp_sspa_probe(struct snd_soc_dai *dai)
 322{
 323        struct sspa_priv *sspa = dev_get_drvdata(dai->dev);
 324
 325        snd_soc_dai_init_dma_data(dai,
 326                                &sspa->playback_dma_data,
 327                                &sspa->capture_dma_data);
 328
 329        snd_soc_dai_set_drvdata(dai, sspa);
 330        return 0;
 331}
 332
 333#define MMP_SSPA_RATES SNDRV_PCM_RATE_8000_192000
 334#define MMP_SSPA_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
 335                SNDRV_PCM_FMTBIT_S16_LE | \
 336                SNDRV_PCM_FMTBIT_S24_3LE | \
 337                SNDRV_PCM_FMTBIT_S32_LE)
 338
 339static const struct snd_soc_dai_ops mmp_sspa_dai_ops = {
 340        .startup        = mmp_sspa_startup,
 341        .shutdown       = mmp_sspa_shutdown,
 342        .trigger        = mmp_sspa_trigger,
 343        .hw_params      = mmp_sspa_hw_params,
 344        .set_sysclk     = mmp_sspa_set_dai_sysclk,
 345        .set_pll        = mmp_sspa_set_dai_pll,
 346        .set_fmt        = mmp_sspa_set_dai_fmt,
 347};
 348
 349static struct snd_soc_dai_driver mmp_sspa_dai = {
 350        .probe = mmp_sspa_probe,
 351        .playback = {
 352                .channels_min = 1,
 353                .channels_max = 128,
 354                .rates = MMP_SSPA_RATES,
 355                .formats = MMP_SSPA_FORMATS,
 356        },
 357        .capture = {
 358                .channels_min = 1,
 359                .channels_max = 2,
 360                .rates = MMP_SSPA_RATES,
 361                .formats = MMP_SSPA_FORMATS,
 362        },
 363        .ops = &mmp_sspa_dai_ops,
 364};
 365
 366#define MMP_PCM_INFO (SNDRV_PCM_INFO_MMAP |     \
 367                SNDRV_PCM_INFO_MMAP_VALID |     \
 368                SNDRV_PCM_INFO_INTERLEAVED |    \
 369                SNDRV_PCM_INFO_PAUSE |          \
 370                SNDRV_PCM_INFO_RESUME |         \
 371                SNDRV_PCM_INFO_NO_PERIOD_WAKEUP)
 372
 373static const struct snd_pcm_hardware mmp_pcm_hardware[] = {
 374        {
 375                .info                   = MMP_PCM_INFO,
 376                .period_bytes_min       = 1024,
 377                .period_bytes_max       = 2048,
 378                .periods_min            = 2,
 379                .periods_max            = 32,
 380                .buffer_bytes_max       = 4096,
 381                .fifo_size              = 32,
 382        },
 383        {
 384                .info                   = MMP_PCM_INFO,
 385                .period_bytes_min       = 1024,
 386                .period_bytes_max       = 2048,
 387                .periods_min            = 2,
 388                .periods_max            = 32,
 389                .buffer_bytes_max       = 4096,
 390                .fifo_size              = 32,
 391        },
 392};
 393
 394static const struct snd_dmaengine_pcm_config mmp_pcm_config = {
 395        .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
 396        .pcm_hardware = mmp_pcm_hardware,
 397        .prealloc_buffer_size = 4096,
 398};
 399
 400static int mmp_pcm_mmap(struct snd_soc_component *component,
 401                        struct snd_pcm_substream *substream,
 402                        struct vm_area_struct *vma)
 403{
 404        vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
 405        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 406        return remap_pfn_range(vma, vma->vm_start,
 407                substream->dma_buffer.addr >> PAGE_SHIFT,
 408                vma->vm_end - vma->vm_start, vma->vm_page_prot);
 409}
 410
 411static int mmp_sspa_open(struct snd_soc_component *component,
 412                         struct snd_pcm_substream *substream)
 413{
 414        struct sspa_priv *sspa = snd_soc_component_get_drvdata(component);
 415
 416        pm_runtime_get_sync(component->dev);
 417
 418        /* we can only change the settings if the port is not in use */
 419        if ((__raw_readl(sspa->tx_base + SSPA_SP) & SSPA_SP_S_EN) ||
 420            (__raw_readl(sspa->rx_base + SSPA_SP) & SSPA_SP_S_EN)) {
 421                dev_err(component->dev,
 422                        "can't change hardware dai format: stream is in use\n");
 423                return -EBUSY;
 424        }
 425
 426        __raw_writel(sspa->sp, sspa->tx_base + SSPA_SP);
 427        __raw_writel(sspa->sp, sspa->rx_base + SSPA_SP);
 428
 429        sspa->sp &= ~(SSPA_SP_S_RST | SSPA_SP_FFLUSH);
 430        __raw_writel(sspa->sp, sspa->tx_base + SSPA_SP);
 431        __raw_writel(sspa->sp, sspa->rx_base + SSPA_SP);
 432
 433        /*
 434         * FIXME: hw issue, for the tx serial port,
 435         * can not config the master/slave mode;
 436         * so must clean this bit.
 437         * The master/slave mode has been set in the
 438         * rx port.
 439         */
 440        __raw_writel(sspa->sp & ~SSPA_SP_MSL, sspa->tx_base + SSPA_SP);
 441
 442        __raw_writel(sspa->ctrl, sspa->tx_base + SSPA_CTL);
 443        __raw_writel(sspa->ctrl, sspa->rx_base + SSPA_CTL);
 444
 445        return 0;
 446}
 447
 448static int mmp_sspa_close(struct snd_soc_component *component,
 449                          struct snd_pcm_substream *substream)
 450{
 451        pm_runtime_put_sync(component->dev);
 452        return 0;
 453}
 454
 455static const struct snd_soc_component_driver mmp_sspa_component = {
 456        .name           = "mmp-sspa",
 457        .mmap           = mmp_pcm_mmap,
 458        .open           = mmp_sspa_open,
 459        .close          = mmp_sspa_close,
 460};
 461
 462static int asoc_mmp_sspa_probe(struct platform_device *pdev)
 463{
 464        struct sspa_priv *sspa;
 465        int ret;
 466
 467        sspa = devm_kzalloc(&pdev->dev,
 468                                sizeof(struct sspa_priv), GFP_KERNEL);
 469        if (!sspa)
 470                return -ENOMEM;
 471
 472        if (pdev->dev.of_node) {
 473                sspa->rx_base = devm_platform_ioremap_resource(pdev, 0);
 474                if (IS_ERR(sspa->rx_base))
 475                        return PTR_ERR(sspa->rx_base);
 476
 477                sspa->tx_base = devm_platform_ioremap_resource(pdev, 1);
 478                if (IS_ERR(sspa->tx_base))
 479                        return PTR_ERR(sspa->tx_base);
 480
 481                sspa->clk = devm_clk_get(&pdev->dev, "bitclk");
 482                if (IS_ERR(sspa->clk))
 483                        return PTR_ERR(sspa->clk);
 484
 485                sspa->audio_clk = devm_clk_get(&pdev->dev, "audio");
 486                if (IS_ERR(sspa->audio_clk))
 487                        return PTR_ERR(sspa->audio_clk);
 488        } else {
 489                struct resource *res;
 490
 491                res = platform_get_resource(pdev, IORESOURCE_IO, 0);
 492                if (res == NULL)
 493                        return -ENODEV;
 494
 495                sspa->rx_base = devm_ioremap(&pdev->dev, res->start, 0x30);
 496                if (!sspa->rx_base)
 497                        return -ENOMEM;
 498
 499                sspa->tx_base = devm_ioremap(&pdev->dev,
 500                                             res->start + 0x80, 0x30);
 501                if (!sspa->tx_base)
 502                        return -ENOMEM;
 503
 504                sspa->clk = devm_clk_get(&pdev->dev, NULL);
 505                if (IS_ERR(sspa->clk))
 506                        return PTR_ERR(sspa->clk);
 507
 508                sspa->audio_clk = clk_get(NULL, "mmp-audio");
 509                if (IS_ERR(sspa->audio_clk))
 510                        return PTR_ERR(sspa->audio_clk);
 511
 512                sspa->sysclk = clk_get(NULL, "mmp-sysclk");
 513                if (IS_ERR(sspa->sysclk)) {
 514                        clk_put(sspa->audio_clk);
 515                        return PTR_ERR(sspa->sysclk);
 516                }
 517        }
 518        platform_set_drvdata(pdev, sspa);
 519
 520        sspa->playback_dma_data.maxburst = 4;
 521        sspa->capture_dma_data.maxburst = 4;
 522        /* You know, these addresses are actually ignored. */
 523        sspa->capture_dma_data.addr = SSPA_D;
 524        sspa->playback_dma_data.addr = 0x80 + SSPA_D;
 525
 526        if (pdev->dev.of_node) {
 527                ret = devm_snd_dmaengine_pcm_register(&pdev->dev,
 528                                                      &mmp_pcm_config, 0);
 529                if (ret)
 530                        return ret;
 531        }
 532
 533        ret = devm_snd_soc_register_component(&pdev->dev, &mmp_sspa_component,
 534                                              &mmp_sspa_dai, 1);
 535        if (ret)
 536                return ret;
 537
 538        pm_runtime_enable(&pdev->dev);
 539        clk_prepare_enable(sspa->audio_clk);
 540
 541        return 0;
 542}
 543
 544static int asoc_mmp_sspa_remove(struct platform_device *pdev)
 545{
 546        struct sspa_priv *sspa = platform_get_drvdata(pdev);
 547
 548        clk_disable_unprepare(sspa->audio_clk);
 549        pm_runtime_disable(&pdev->dev);
 550
 551        if (pdev->dev.of_node)
 552                return 0;
 553
 554        clk_put(sspa->audio_clk);
 555        clk_put(sspa->sysclk);
 556        return 0;
 557}
 558
 559#ifdef CONFIG_OF
 560static const struct of_device_id mmp_sspa_of_match[] = {
 561        { .compatible = "marvell,mmp-sspa" },
 562        {},
 563};
 564
 565MODULE_DEVICE_TABLE(of, mmp_sspa_of_match);
 566#endif
 567
 568static struct platform_driver asoc_mmp_sspa_driver = {
 569        .driver = {
 570                .name = "mmp-sspa-dai",
 571                .of_match_table = of_match_ptr(mmp_sspa_of_match),
 572        },
 573        .probe = asoc_mmp_sspa_probe,
 574        .remove = asoc_mmp_sspa_remove,
 575};
 576
 577module_platform_driver(asoc_mmp_sspa_driver);
 578
 579MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
 580MODULE_DESCRIPTION("MMP SSPA SoC Interface");
 581MODULE_LICENSE("GPL");
 582MODULE_ALIAS("platform:mmp-sspa-dai");
 583