linux/sound/soc/codecs/rt5514-spi.c
<<
>>
Prefs
   1/*
   2 * rt5514-spi.c  --  RT5514 SPI driver
   3 *
   4 * Copyright 2015 Realtek Semiconductor Corp.
   5 * Author: Oder Chiou <oder_chiou@realtek.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
  12#include <linux/module.h>
  13#include <linux/input.h>
  14#include <linux/spi/spi.h>
  15#include <linux/device.h>
  16#include <linux/init.h>
  17#include <linux/delay.h>
  18#include <linux/interrupt.h>
  19#include <linux/irq.h>
  20#include <linux/slab.h>
  21#include <linux/gpio.h>
  22#include <linux/sched.h>
  23#include <linux/uaccess.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/pm_qos.h>
  26#include <linux/sysfs.h>
  27#include <linux/clk.h>
  28#include <sound/core.h>
  29#include <sound/pcm.h>
  30#include <sound/pcm_params.h>
  31#include <sound/soc.h>
  32#include <sound/soc-dapm.h>
  33#include <sound/initval.h>
  34#include <sound/tlv.h>
  35
  36#include "rt5514-spi.h"
  37
  38static struct spi_device *rt5514_spi;
  39
  40struct rt5514_dsp {
  41        struct device *dev;
  42        struct delayed_work copy_work;
  43        struct mutex dma_lock;
  44        struct snd_pcm_substream *substream;
  45        unsigned int buf_base, buf_limit, buf_rp;
  46        size_t buf_size;
  47        size_t dma_offset;
  48        size_t dsp_offset;
  49};
  50
  51static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = {
  52        .info                   = SNDRV_PCM_INFO_MMAP |
  53                                  SNDRV_PCM_INFO_MMAP_VALID |
  54                                  SNDRV_PCM_INFO_INTERLEAVED,
  55        .formats                = SNDRV_PCM_FMTBIT_S16_LE,
  56        .period_bytes_min       = PAGE_SIZE,
  57        .period_bytes_max       = 0x20000 / 8,
  58        .periods_min            = 8,
  59        .periods_max            = 8,
  60        .channels_min           = 1,
  61        .channels_max           = 1,
  62        .buffer_bytes_max       = 0x20000,
  63};
  64
  65static struct snd_soc_dai_driver rt5514_spi_dai = {
  66        .name = "rt5514-dsp-cpu-dai",
  67        .id = 0,
  68        .capture = {
  69                .stream_name = "DSP Capture",
  70                .channels_min = 1,
  71                .channels_max = 1,
  72                .rates = SNDRV_PCM_RATE_16000,
  73                .formats = SNDRV_PCM_FMTBIT_S16_LE,
  74        },
  75};
  76
  77static void rt5514_spi_copy_work(struct work_struct *work)
  78{
  79        struct rt5514_dsp *rt5514_dsp =
  80                container_of(work, struct rt5514_dsp, copy_work.work);
  81        struct snd_pcm_runtime *runtime;
  82        size_t period_bytes, truncated_bytes = 0;
  83
  84        mutex_lock(&rt5514_dsp->dma_lock);
  85        if (!rt5514_dsp->substream) {
  86                dev_err(rt5514_dsp->dev, "No pcm substream\n");
  87                goto done;
  88        }
  89
  90        runtime = rt5514_dsp->substream->runtime;
  91        period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream);
  92
  93        if (rt5514_dsp->buf_size - rt5514_dsp->dsp_offset <  period_bytes)
  94                period_bytes = rt5514_dsp->buf_size - rt5514_dsp->dsp_offset;
  95
  96        if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) {
  97                rt5514_spi_burst_read(rt5514_dsp->buf_rp,
  98                        runtime->dma_area + rt5514_dsp->dma_offset,
  99                        period_bytes);
 100
 101                if (rt5514_dsp->buf_rp + period_bytes == rt5514_dsp->buf_limit)
 102                        rt5514_dsp->buf_rp = rt5514_dsp->buf_base;
 103                else
 104                        rt5514_dsp->buf_rp += period_bytes;
 105        } else {
 106                truncated_bytes = rt5514_dsp->buf_limit - rt5514_dsp->buf_rp;
 107                rt5514_spi_burst_read(rt5514_dsp->buf_rp,
 108                        runtime->dma_area + rt5514_dsp->dma_offset,
 109                        truncated_bytes);
 110
 111                rt5514_spi_burst_read(rt5514_dsp->buf_base,
 112                        runtime->dma_area + rt5514_dsp->dma_offset +
 113                        truncated_bytes, period_bytes - truncated_bytes);
 114
 115                        rt5514_dsp->buf_rp = rt5514_dsp->buf_base +
 116                                period_bytes - truncated_bytes;
 117        }
 118
 119        rt5514_dsp->dma_offset += period_bytes;
 120        if (rt5514_dsp->dma_offset >= runtime->dma_bytes)
 121                rt5514_dsp->dma_offset = 0;
 122
 123        rt5514_dsp->dsp_offset += period_bytes;
 124
 125        snd_pcm_period_elapsed(rt5514_dsp->substream);
 126
 127        if (rt5514_dsp->dsp_offset < rt5514_dsp->buf_size)
 128                schedule_delayed_work(&rt5514_dsp->copy_work, 5);
 129done:
 130        mutex_unlock(&rt5514_dsp->dma_lock);
 131}
 132
 133/* PCM for streaming audio from the DSP buffer */
 134static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream)
 135{
 136        snd_soc_set_runtime_hwparams(substream, &rt5514_spi_pcm_hardware);
 137
 138        return 0;
 139}
 140
 141static int rt5514_spi_hw_params(struct snd_pcm_substream *substream,
 142                               struct snd_pcm_hw_params *hw_params)
 143{
 144        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 145        struct rt5514_dsp *rt5514_dsp =
 146                        snd_soc_platform_get_drvdata(rtd->platform);
 147        int ret;
 148
 149        mutex_lock(&rt5514_dsp->dma_lock);
 150        ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
 151                        params_buffer_bytes(hw_params));
 152        rt5514_dsp->substream = substream;
 153        mutex_unlock(&rt5514_dsp->dma_lock);
 154
 155        return ret;
 156}
 157
 158static int rt5514_spi_hw_free(struct snd_pcm_substream *substream)
 159{
 160        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 161        struct rt5514_dsp *rt5514_dsp =
 162                        snd_soc_platform_get_drvdata(rtd->platform);
 163
 164        mutex_lock(&rt5514_dsp->dma_lock);
 165        rt5514_dsp->substream = NULL;
 166        mutex_unlock(&rt5514_dsp->dma_lock);
 167
 168        cancel_delayed_work_sync(&rt5514_dsp->copy_work);
 169
 170        return snd_pcm_lib_free_vmalloc_buffer(substream);
 171}
 172
 173static int rt5514_spi_prepare(struct snd_pcm_substream *substream)
 174{
 175        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 176        struct rt5514_dsp *rt5514_dsp =
 177                        snd_soc_platform_get_drvdata(rtd->platform);
 178        u8 buf[8];
 179
 180        rt5514_dsp->dma_offset = 0;
 181        rt5514_dsp->dsp_offset = 0;
 182
 183        /**
 184         * The address area x1800XXXX is the register address, and it cannot
 185         * support spi burst read perfectly. So we use the spi burst read
 186         * individually to make sure the data correctly.
 187        */
 188        rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
 189                sizeof(buf));
 190        rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
 191                                buf[3] << 24;
 192
 193        rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
 194                sizeof(buf));
 195        rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
 196                                buf[3] << 24;
 197
 198        rt5514_spi_burst_read(RT5514_BUFFER_VOICE_RP, (u8 *)&buf,
 199                sizeof(buf));
 200        rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
 201                                buf[3] << 24;
 202
 203        rt5514_spi_burst_read(RT5514_BUFFER_VOICE_SIZE, (u8 *)&buf,
 204                sizeof(buf));
 205        rt5514_dsp->buf_size = buf[0] | buf[1] << 8 | buf[2] << 16 |
 206                                buf[3] << 24;
 207
 208        return 0;
 209}
 210
 211static int rt5514_spi_trigger(struct snd_pcm_substream *substream, int cmd)
 212{
 213        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 214        struct rt5514_dsp *rt5514_dsp =
 215                        snd_soc_platform_get_drvdata(rtd->platform);
 216
 217        if (cmd == SNDRV_PCM_TRIGGER_START) {
 218                if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
 219                        rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
 220                        schedule_delayed_work(&rt5514_dsp->copy_work, 0);
 221        }
 222
 223        return 0;
 224}
 225
 226static snd_pcm_uframes_t rt5514_spi_pcm_pointer(
 227                struct snd_pcm_substream *substream)
 228{
 229        struct snd_pcm_runtime *runtime = substream->runtime;
 230        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 231        struct rt5514_dsp *rt5514_dsp =
 232                snd_soc_platform_get_drvdata(rtd->platform);
 233
 234        return bytes_to_frames(runtime, rt5514_dsp->dma_offset);
 235}
 236
 237static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
 238        .open           = rt5514_spi_pcm_open,
 239        .hw_params      = rt5514_spi_hw_params,
 240        .hw_free        = rt5514_spi_hw_free,
 241        .trigger        = rt5514_spi_trigger,
 242        .prepare        = rt5514_spi_prepare,
 243        .pointer        = rt5514_spi_pcm_pointer,
 244        .mmap           = snd_pcm_lib_mmap_vmalloc,
 245        .page           = snd_pcm_lib_get_vmalloc_page,
 246};
 247
 248static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform)
 249{
 250        struct rt5514_dsp *rt5514_dsp;
 251
 252        rt5514_dsp = devm_kzalloc(platform->dev, sizeof(*rt5514_dsp),
 253                        GFP_KERNEL);
 254
 255        rt5514_dsp->dev = &rt5514_spi->dev;
 256        mutex_init(&rt5514_dsp->dma_lock);
 257        INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work);
 258        snd_soc_platform_set_drvdata(platform, rt5514_dsp);
 259
 260        return 0;
 261}
 262
 263static struct snd_soc_platform_driver rt5514_spi_platform = {
 264        .probe = rt5514_spi_pcm_probe,
 265        .ops = &rt5514_spi_pcm_ops,
 266};
 267
 268static const struct snd_soc_component_driver rt5514_spi_dai_component = {
 269        .name           = "rt5514-spi-dai",
 270};
 271
 272/**
 273 * rt5514_spi_burst_read - Read data from SPI by rt5514 address.
 274 * @addr: Start address.
 275 * @rxbuf: Data Buffer for reading.
 276 * @len: Data length, it must be a multiple of 8.
 277 *
 278 *
 279 * Returns true for success.
 280 */
 281int rt5514_spi_burst_read(unsigned int addr, u8 *rxbuf, size_t len)
 282{
 283        u8 spi_cmd = RT5514_SPI_CMD_BURST_READ;
 284        int status;
 285        u8 write_buf[8];
 286        unsigned int i, end, offset = 0;
 287
 288        struct spi_message message;
 289        struct spi_transfer x[3];
 290
 291        while (offset < len) {
 292                if (offset + RT5514_SPI_BUF_LEN <= len)
 293                        end = RT5514_SPI_BUF_LEN;
 294                else
 295                        end = len % RT5514_SPI_BUF_LEN;
 296
 297                write_buf[0] = spi_cmd;
 298                write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
 299                write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
 300                write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
 301                write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
 302
 303                spi_message_init(&message);
 304                memset(x, 0, sizeof(x));
 305
 306                x[0].len = 5;
 307                x[0].tx_buf = write_buf;
 308                spi_message_add_tail(&x[0], &message);
 309
 310                x[1].len = 4;
 311                x[1].tx_buf = write_buf;
 312                spi_message_add_tail(&x[1], &message);
 313
 314                x[2].len = end;
 315                x[2].rx_buf = rxbuf + offset;
 316                spi_message_add_tail(&x[2], &message);
 317
 318                status = spi_sync(rt5514_spi, &message);
 319
 320                if (status)
 321                        return false;
 322
 323                offset += RT5514_SPI_BUF_LEN;
 324        }
 325
 326        for (i = 0; i < len; i += 8) {
 327                write_buf[0] = rxbuf[i + 0];
 328                write_buf[1] = rxbuf[i + 1];
 329                write_buf[2] = rxbuf[i + 2];
 330                write_buf[3] = rxbuf[i + 3];
 331                write_buf[4] = rxbuf[i + 4];
 332                write_buf[5] = rxbuf[i + 5];
 333                write_buf[6] = rxbuf[i + 6];
 334                write_buf[7] = rxbuf[i + 7];
 335
 336                rxbuf[i + 0] = write_buf[7];
 337                rxbuf[i + 1] = write_buf[6];
 338                rxbuf[i + 2] = write_buf[5];
 339                rxbuf[i + 3] = write_buf[4];
 340                rxbuf[i + 4] = write_buf[3];
 341                rxbuf[i + 5] = write_buf[2];
 342                rxbuf[i + 6] = write_buf[1];
 343                rxbuf[i + 7] = write_buf[0];
 344        }
 345
 346        return true;
 347}
 348
 349/**
 350 * rt5514_spi_burst_write - Write data to SPI by rt5514 address.
 351 * @addr: Start address.
 352 * @txbuf: Data Buffer for writng.
 353 * @len: Data length, it must be a multiple of 8.
 354 *
 355 *
 356 * Returns true for success.
 357 */
 358int rt5514_spi_burst_write(u32 addr, const u8 *txbuf, size_t len)
 359{
 360        u8 spi_cmd = RT5514_SPI_CMD_BURST_WRITE;
 361        u8 *write_buf;
 362        unsigned int i, end, offset = 0;
 363
 364        write_buf = kmalloc(RT5514_SPI_BUF_LEN + 6, GFP_KERNEL);
 365
 366        if (write_buf == NULL)
 367                return -ENOMEM;
 368
 369        while (offset < len) {
 370                if (offset + RT5514_SPI_BUF_LEN <= len)
 371                        end = RT5514_SPI_BUF_LEN;
 372                else
 373                        end = len % RT5514_SPI_BUF_LEN;
 374
 375                write_buf[0] = spi_cmd;
 376                write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
 377                write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
 378                write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
 379                write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
 380
 381                for (i = 0; i < end; i += 8) {
 382                        write_buf[i + 12] = txbuf[offset + i + 0];
 383                        write_buf[i + 11] = txbuf[offset + i + 1];
 384                        write_buf[i + 10] = txbuf[offset + i + 2];
 385                        write_buf[i +  9] = txbuf[offset + i + 3];
 386                        write_buf[i +  8] = txbuf[offset + i + 4];
 387                        write_buf[i +  7] = txbuf[offset + i + 5];
 388                        write_buf[i +  6] = txbuf[offset + i + 6];
 389                        write_buf[i +  5] = txbuf[offset + i + 7];
 390                }
 391
 392                write_buf[end + 5] = spi_cmd;
 393
 394                spi_write(rt5514_spi, write_buf, end + 6);
 395
 396                offset += RT5514_SPI_BUF_LEN;
 397        }
 398
 399        kfree(write_buf);
 400
 401        return 0;
 402}
 403EXPORT_SYMBOL_GPL(rt5514_spi_burst_write);
 404
 405static int rt5514_spi_probe(struct spi_device *spi)
 406{
 407        int ret;
 408
 409        rt5514_spi = spi;
 410
 411        ret = devm_snd_soc_register_platform(&spi->dev, &rt5514_spi_platform);
 412        if (ret < 0) {
 413                dev_err(&spi->dev, "Failed to register platform.\n");
 414                return ret;
 415        }
 416
 417        ret = devm_snd_soc_register_component(&spi->dev,
 418                                              &rt5514_spi_dai_component,
 419                                              &rt5514_spi_dai, 1);
 420        if (ret < 0) {
 421                dev_err(&spi->dev, "Failed to register component.\n");
 422                return ret;
 423        }
 424
 425        return 0;
 426}
 427
 428static const struct of_device_id rt5514_of_match[] = {
 429        { .compatible = "realtek,rt5514", },
 430        {},
 431};
 432MODULE_DEVICE_TABLE(of, rt5514_of_match);
 433
 434static struct spi_driver rt5514_spi_driver = {
 435        .driver = {
 436                .name = "rt5514",
 437                .of_match_table = of_match_ptr(rt5514_of_match),
 438        },
 439        .probe = rt5514_spi_probe,
 440};
 441module_spi_driver(rt5514_spi_driver);
 442
 443MODULE_DESCRIPTION("RT5514 SPI driver");
 444MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
 445MODULE_LICENSE("GPL v2");
 446