plugins: cache wall clk timestamp only when data is available

When querying mmap position for the first time after detection,
there may be no data ready in ADSP side, causing old wall clk
timestamp cached. This may cause unexpected behavior when we
query mmap position next time as the wall clk difference may
be very big and finally it may cause crash for lab reading in
pal side.

Cache wall clk timestamp only when frame counter changes after
query.

Change-Id: I068ae58ad73908815e29d686cbf4dc79c4001cee
diff --git a/plugins/tinyalsa/src/agm_pcm_plugin.c b/plugins/tinyalsa/src/agm_pcm_plugin.c
index 53c86b3..8c271d7 100644
--- a/plugins/tinyalsa/src/agm_pcm_plugin.c
+++ b/plugins/tinyalsa/src/agm_pcm_plugin.c
@@ -76,6 +76,7 @@
     snd_pcm_uframes_t avail_min; /* RW: min available frames for wakeup */
     uint32_t wall_clk_msw;
     uint32_t wall_clk_lsw;
+    uint32_t frame_counter;
 };
 
 struct agm_mmap_buffer_port {
@@ -299,6 +300,7 @@
         if (frame_cnt1 != frame_cnt2)
             continue;
 
+        pos_buf->frame_counter = frame_cnt1;
         return 0;
     }
 
@@ -316,6 +318,7 @@
     int ret = 0;
     uint32_t period_size = priv->period_size; /** in frames */
     uint32_t crossed_boundary = 0;
+    uint32_t old_frame_counter = priv->pos_buf->frame_counter;
 
     do {
         ret = agm_pcm_plugin_get_shared_pos(priv->pos_buf,
@@ -371,8 +374,11 @@
         }
 
         priv->pos_buf->hw_ptr = new_hw_ptr;
-        priv->pos_buf->wall_clk_lsw = wall_clk_lsw;
-        priv->pos_buf->wall_clk_msw = wall_clk_msw;
+        // cache wall clk only when there's data update in shared buffer
+        if (priv->pos_buf->frame_counter != old_frame_counter) {
+            priv->pos_buf->wall_clk_lsw = wall_clk_lsw;
+            priv->pos_buf->wall_clk_msw = wall_clk_msw;
+        }
         clock_gettime(CLOCK_MONOTONIC, &priv->pos_buf->tstamp);
     }