schedule_delayed_work使用

schedule_delayed_work

目的是解决spdif播放音频前800ms音频不出的问题。但是spdif信号锁定很差音响就会丢失前面的音频

diff --git a/soc/x2000/spdif/spdif_dma.c b/soc/x2000/spdif/spdif_dma.c
index cc923da..936ff4c 100644
--- a/soc/x2000/spdif/spdif_dma.c
+++ b/soc/x2000/spdif/spdif_dma.c
@@ -13,6 +13,7 @@
 #include <linux/dmaengine.h>
 #include <linux/hrtimer.h>
 #include <linux/err.h>
+#include <linux/workqueue.h>
 
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
@@ -64,8 +65,11 @@ struct spdif_dma_data {
     int period_ns;
     unsigned char hrtimer_state;
     struct hrtimer hrtimer;
+    struct delayed_work delayed_work;
 };
 
+DEFINE_SPINLOCK(spdif_dma_lock);
+
 static inline void *m_dma_alloc_coherent(int size)
 {
     dma_addr_t dma_handle;
@@ -98,10 +102,13 @@ static enum hrtimer_restart spdif_hrtimer_callback(struct hrtimer *hrtimer)
     return HRTIMER_RESTART;
 }
 
+static void spdif_dma_start(struct work_struct *work);
+
 static int spdif_dma_pcm_open(struct snd_pcm_substream *substream)
 {
     struct snd_pcm_runtime *runtime = substream->runtime;
     struct spdif_dma_data *spdif_dma;
+    printk("%s %d\n",__func__,__LINE__);
 
     int ret = snd_soc_set_runtime_hwparams(substream, &spdif_pcm_hardware);
     if (ret) {
@@ -134,6 +141,7 @@ static int spdif_dma_pcm_open(struct snd_pcm_substream *substream)
     hrtimer_init(&spdif_dma->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
     spdif_dma->hrtimer.function = spdif_hrtimer_callback;
     spdif_dma->substream = substream;
+    INIT_DELAYED_WORK(&spdif_dma->delayed_work, spdif_dma_start);
 
     runtime->private_data = spdif_dma;
 
@@ -321,24 +329,37 @@ static int spdif_dma_pcm_hw_free(struct snd_pcm_substream *substream)
     return 0;
 }
 
+static void spdif_dma_start(struct work_struct *work)
+{
+    printk("%s %d\n",__func__,__LINE__);
+    unsigned long flags;
+    spin_lock_irqsave(&spdif_dma_lock, flags);
+    struct delayed_work *delayed_work = container_of(work, struct delayed_work, work);
+    struct spdif_dma_data *spdif_dma = container_of(delayed_work, struct spdif_dma_data, delayed_work);
+    audio_dma_start(spdif_dma->dma_dev, spdif_dma->dma_desc);
+    if (spdif_dma->hrtimer_state == State_idle) {
+        hrtimer_forward_now(&spdif_dma->hrtimer,
+                    ns_to_ktime(spdif_dma->period_ns));
+        hrtimer_start_expires(&spdif_dma->hrtimer, HRTIMER_MODE_ABS);
+    }
+
+    spdif_dma->hrtimer_state = State_running;
+    spin_unlock_irqrestore(&spdif_dma_lock, flags);
+}
+
 static int spdif_dma_trigger(struct snd_pcm_substream *substream, int cmd)
 {
     struct snd_pcm_runtime *runtime = substream->runtime;
     struct spdif_dma_data *spdif_dma = runtime->private_data;
     int ret = 0;
+    printk("%s %d\n",__func__,__LINE__);
 
     switch (cmd) {
     case SNDRV_PCM_TRIGGER_START:
     case SNDRV_PCM_TRIGGER_RESUME:
     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
-        audio_dma_start(spdif_dma->dma_dev, spdif_dma->dma_desc);
-        if (spdif_dma->hrtimer_state == State_idle) {
-            hrtimer_forward_now(&spdif_dma->hrtimer,
-                        ns_to_ktime(spdif_dma->period_ns));
-            hrtimer_start_expires(&spdif_dma->hrtimer, HRTIMER_MODE_ABS);
-        }
-
-        spdif_dma->hrtimer_state = State_running;
+        schedule_delayed_work(&spdif_dma->delayed_work, msecs_to_jiffies(500));
+        printk("%s %d\n",__func__,__LINE__);
         break;
     case SNDRV_PCM_TRIGGER_STOP:
     case SNDRV_PCM_TRIGGER_SUSPEND:
@@ -350,10 +371,10 @@ static int spdif_dma_trigger(struct snd_pcm_substream *substream, int cmd)
         ret = -EINVAL;
         break;
     }
-
     return ret;
 }
 
+
 static int debug = 0;
 module_param(debug, int, 0644);
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c2h5oh2012

有用记得给点打赏yo

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值