Merge remote-tracking branches 'asoc/topic/rk3036', 'asoc/topic/rockchip', 'asoc...
[sfrench/cifs-2.6.git] / sound / soc / codecs / rt5514-spi.c
index 87a587fa90fe81ecfa019fcf805a425f648f6836..ed6e5373916c390fb15dfc9efd943c9c55f7d819 100644 (file)
@@ -43,9 +43,7 @@ struct rt5514_dsp {
        struct mutex dma_lock;
        struct snd_pcm_substream *substream;
        unsigned int buf_base, buf_limit, buf_rp;
-       size_t buf_size;
-       size_t dma_offset;
-       size_t dsp_offset;
+       size_t buf_size, get_size, dma_offset;
 };
 
 static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = {
@@ -80,6 +78,8 @@ static void rt5514_spi_copy_work(struct work_struct *work)
                container_of(work, struct rt5514_dsp, copy_work.work);
        struct snd_pcm_runtime *runtime;
        size_t period_bytes, truncated_bytes = 0;
+       unsigned int cur_wp, remain_data;
+       u8 buf[8];
 
        mutex_lock(&rt5514_dsp->dma_lock);
        if (!rt5514_dsp->substream) {
@@ -90,8 +90,24 @@ static void rt5514_spi_copy_work(struct work_struct *work)
        runtime = rt5514_dsp->substream->runtime;
        period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream);
 
-       if (rt5514_dsp->buf_size - rt5514_dsp->dsp_offset <  period_bytes)
-               period_bytes = rt5514_dsp->buf_size - rt5514_dsp->dsp_offset;
+       if (rt5514_dsp->get_size >= rt5514_dsp->buf_size) {
+               rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf,
+                       sizeof(buf));
+               cur_wp = buf[0] | buf[1] << 8 | buf[2] << 16 |
+                                       buf[3] << 24;
+
+               if (cur_wp >= rt5514_dsp->buf_rp)
+                       remain_data = (cur_wp - rt5514_dsp->buf_rp);
+               else
+                       remain_data =
+                               (rt5514_dsp->buf_limit - rt5514_dsp->buf_rp) +
+                               (cur_wp - rt5514_dsp->buf_base);
+
+               if (remain_data < period_bytes) {
+                       schedule_delayed_work(&rt5514_dsp->copy_work, 5);
+                       goto done;
+               }
+       }
 
        if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) {
                rt5514_spi_burst_read(rt5514_dsp->buf_rp,
@@ -112,24 +128,62 @@ static void rt5514_spi_copy_work(struct work_struct *work)
                        runtime->dma_area + rt5514_dsp->dma_offset +
                        truncated_bytes, period_bytes - truncated_bytes);
 
-                       rt5514_dsp->buf_rp = rt5514_dsp->buf_base +
-                               period_bytes - truncated_bytes;
+               rt5514_dsp->buf_rp = rt5514_dsp->buf_base + period_bytes -
+                       truncated_bytes;
        }
 
+       rt5514_dsp->get_size += period_bytes;
        rt5514_dsp->dma_offset += period_bytes;
        if (rt5514_dsp->dma_offset >= runtime->dma_bytes)
                rt5514_dsp->dma_offset = 0;
 
-       rt5514_dsp->dsp_offset += period_bytes;
-
        snd_pcm_period_elapsed(rt5514_dsp->substream);
 
-       if (rt5514_dsp->dsp_offset < rt5514_dsp->buf_size)
-               schedule_delayed_work(&rt5514_dsp->copy_work, 5);
+       schedule_delayed_work(&rt5514_dsp->copy_work, 5);
+
 done:
        mutex_unlock(&rt5514_dsp->dma_lock);
 }
 
+static irqreturn_t rt5514_spi_irq(int irq, void *data)
+{
+       struct rt5514_dsp *rt5514_dsp = data;
+       u8 buf[8];
+
+       rt5514_dsp->get_size = 0;
+
+       /**
+        * The address area x1800XXXX is the register address, and it cannot
+        * support spi burst read perfectly. So we use the spi burst read
+        * individually to make sure the data correctly.
+        */
+       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
+               sizeof(buf));
+       rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
+                               buf[3] << 24;
+
+       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
+               sizeof(buf));
+       rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
+                               buf[3] << 24;
+
+       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf,
+               sizeof(buf));
+       rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
+                               buf[3] << 24;
+
+       if (rt5514_dsp->buf_rp % 8)
+               rt5514_dsp->buf_rp = (rt5514_dsp->buf_rp / 8) * 8;
+
+       rt5514_dsp->buf_size = rt5514_dsp->buf_limit - rt5514_dsp->buf_base;
+
+       if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
+               rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
+               schedule_delayed_work(&rt5514_dsp->copy_work, 0);
+
+       return IRQ_HANDLED;
+}
+
 /* PCM for streaming audio from the DSP buffer */
 static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream)
 {
@@ -150,6 +204,7 @@ static int rt5514_spi_hw_params(struct snd_pcm_substream *substream,
        ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
                        params_buffer_bytes(hw_params));
        rt5514_dsp->substream = substream;
+       rt5514_dsp->dma_offset = 0;
        mutex_unlock(&rt5514_dsp->dma_lock);
 
        return ret;
@@ -170,59 +225,6 @@ static int rt5514_spi_hw_free(struct snd_pcm_substream *substream)
        return snd_pcm_lib_free_vmalloc_buffer(substream);
 }
 
-static int rt5514_spi_prepare(struct snd_pcm_substream *substream)
-{
-       struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct rt5514_dsp *rt5514_dsp =
-                       snd_soc_platform_get_drvdata(rtd->platform);
-       u8 buf[8];
-
-       rt5514_dsp->dma_offset = 0;
-       rt5514_dsp->dsp_offset = 0;
-
-       /**
-        * The address area x1800XXXX is the register address, and it cannot
-        * support spi burst read perfectly. So we use the spi burst read
-        * individually to make sure the data correctly.
-       */
-       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
-               sizeof(buf));
-       rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
-                               buf[3] << 24;
-
-       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
-               sizeof(buf));
-       rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
-                               buf[3] << 24;
-
-       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_RP, (u8 *)&buf,
-               sizeof(buf));
-       rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
-                               buf[3] << 24;
-
-       rt5514_spi_burst_read(RT5514_BUFFER_VOICE_SIZE, (u8 *)&buf,
-               sizeof(buf));
-       rt5514_dsp->buf_size = buf[0] | buf[1] << 8 | buf[2] << 16 |
-                               buf[3] << 24;
-
-       return 0;
-}
-
-static int rt5514_spi_trigger(struct snd_pcm_substream *substream, int cmd)
-{
-       struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct rt5514_dsp *rt5514_dsp =
-                       snd_soc_platform_get_drvdata(rtd->platform);
-
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
-               if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
-                       rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
-                       schedule_delayed_work(&rt5514_dsp->copy_work, 0);
-       }
-
-       return 0;
-}
-
 static snd_pcm_uframes_t rt5514_spi_pcm_pointer(
                struct snd_pcm_substream *substream)
 {
@@ -238,8 +240,6 @@ static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
        .open           = rt5514_spi_pcm_open,
        .hw_params      = rt5514_spi_hw_params,
        .hw_free        = rt5514_spi_hw_free,
-       .trigger        = rt5514_spi_trigger,
-       .prepare        = rt5514_spi_prepare,
        .pointer        = rt5514_spi_pcm_pointer,
        .mmap           = snd_pcm_lib_mmap_vmalloc,
        .page           = snd_pcm_lib_get_vmalloc_page,
@@ -248,6 +248,7 @@ static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
 static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform)
 {
        struct rt5514_dsp *rt5514_dsp;
+       int ret;
 
        rt5514_dsp = devm_kzalloc(platform->dev, sizeof(*rt5514_dsp),
                        GFP_KERNEL);
@@ -257,6 +258,17 @@ static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform)
        INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work);
        snd_soc_platform_set_drvdata(platform, rt5514_dsp);
 
+       if (rt5514_spi->irq) {
+               ret = devm_request_threaded_irq(&rt5514_spi->dev,
+                       rt5514_spi->irq, NULL, rt5514_spi_irq,
+                       IRQF_TRIGGER_RISING | IRQF_ONESHOT, "rt5514-spi",
+                       rt5514_dsp);
+               if (ret)
+                       dev_err(&rt5514_spi->dev,
+                               "%s Failed to reguest IRQ: %d\n", __func__,
+                               ret);
+       }
+
        return 0;
 }