From patchwork Wed Jan 18 07:26:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Ranostay X-Patchwork-Id: 9522953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 51CD0601C3 for ; Wed, 18 Jan 2017 09:28:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 457F528553 for ; Wed, 18 Jan 2017 09:28:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A51F2855A; Wed, 18 Jan 2017 09:28:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,T_DKIM_INVALID autolearn=no version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C24C728553 for ; Wed, 18 Jan 2017 09:28:42 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 3F4CE267B00; Wed, 18 Jan 2017 10:28:40 +0100 (CET) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id BD679267AF6; Wed, 18 Jan 2017 10:26:20 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 60CF7267AA4; Wed, 18 Jan 2017 08:26:09 +0100 (CET) Received: from mail-pf0-f176.google.com (mail-pf0-f176.google.com [209.85.192.176]) by alsa0.perex.cz (Postfix) with ESMTP id 7D410267A68 for ; Wed, 18 Jan 2017 08:26:07 +0100 (CET) Received: by mail-pf0-f176.google.com with SMTP id y143so2267409pfb.0 for ; Tue, 17 Jan 2017 23:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ranostay-consulting.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=zecE+DoC2LjVvY8L5fYoCMPW626XsNHdBk+j0O4M3Ug=; b=M14s0pLf/4E7BnRBhqPFWFIFgkmG317uqM1TtSeeCtM8o+a7yglfmZjGdFixokzFlP 7zy57gw4xjH2CBU15ZCSDQkqO3Fn3Nb/2rd631n51dFOI2DSrVz0WVXQG67ERA12O8N9 jzk9gPRDzo7iTq/90nVxj7am6Ri5ux+NitTIU5QPrbnsOEg8scP5+lgipQyF24tHzai3 zZpECMXviiTK56y89rjZ1Dpz0xYvXlxxvP3lH/tXszdRcCqtZQD42K1aqV6bD7pJcp41 HjfxUP4J+ByIfiOe0X0q4K88pYtFMzKwoks10SjCgkta2ftMu0QkYt3d//2u1kNlpdMm ifGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=zecE+DoC2LjVvY8L5fYoCMPW626XsNHdBk+j0O4M3Ug=; b=GNJi55uTsnc9jdrYTpWLEXygAc7WfERMSnsqB3P5XZsdSgzCvVuKL+dL39cqW4JrLx zbei5eXzSU4kbwKLXBQfmTg/V3+4lzYzsMWNbRLh0yVruccOtXuDyMkfih2vcZnZktFN AnXAujzhGd4Gn4AwV+R7D3NzDDC7fF+HoQ+OJHtEflDUD6LSAhuUyHxc9hPW0iauNdBl rlkX0xHydEJzoRAN2zQPN/tiUfStjjaWEA/lFUZNELEvCIAqjP5cFk4GhW+qlU68Hu2q aixhggQpZRwXTCW79M4ekDAoR9iPt6sYVGpji3kaxWGvK/D9Pf++dr2zlchKweZ/Huj8 Q0Aw== X-Gm-Message-State: AIkVDXJQEKhkcnyqv9uzVYt9Nj47eneE10AWWf/ymAIIvSO6OQhIBDaV42nK81OqYvYH5A== X-Received: by 10.84.138.3 with SMTP id 3mr3022467plo.108.1484724365857; Tue, 17 Jan 2017 23:26:05 -0800 (PST) Received: from niteshade.hsd1.or.comcast.net (c-73-25-156-150.hsd1.or.comcast.net. [73.25.156.150]) by smtp.gmail.com with ESMTPSA id 16sm60858345pfk.54.2017.01.17.23.26.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Jan 2017 23:26:05 -0800 (PST) From: Matt Ranostay To: alsa-devel@alsa-project.org, linux-omap@vger.kernel.org Date: Tue, 17 Jan 2017 23:26:00 -0800 Message-Id: <20170118072600.15239-1-matt@ranostay.consulting> X-Mailer: git-send-email 2.10.2 Cc: Tony Lindgren , Peter Ujfalusi , Matt Ranostay Subject: [alsa-devel] [PATCH v8] ASoC: omap-mcbsp: Add PM QoS support for McBSP to prevent glitches X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP We can get audio errors if hitting deeper idle states on omaps: [alsa.c:230] error: Fatal problem with alsa output, error -5. [audio.c:614] error: Error in writing audio (Input/output error?)! This seems to happen with off mode idle enabled as power for the whole SoC may get cut off between filling the McBSP fifo using DMA. While active DMA blocks deeper idle states in hardware, McBSP activity does not seem to do so. Basing the QoS latency calculation on the FIFO size, threshold, sample rate, and channels. Based on the original patch by Tony Lindgren Link: https://patchwork.kernel.org/patch/9305867/ Cc: Tony Lindgren Cc: Peter Ujfalusi Signed-off-by: Matt Ranostay Tested-by: Tony Lindgren Acked-by: Peter Ujfalusi --- Changes from v1: * add calculations for latency per number of FIFO locations Changes from v2: * add missing mcbsp.h header change Changes from v3: * base the latency calculations on threshold, buffer size, sample rate, and channels Changes from v4: * using Peter Ujfalusi's suggestions for restoring a higher latency on audio stream completion, or if not applicable remove the QoS request Changes from v5: * clean up latency checking logic * move logic to .prepare and .shutdown to avoid functions that can sleep Changes from v6: * move QoS removal to asoc_mcbsp_remove from omap_mcbsp_cleanup * also remove header include that is unneeded Changes from v7: * fix issue reported by Tony Lindgreen that a player application could close the card after hw_params, and cause an invalid pm_qos_remove_request() sound/soc/omap/mcbsp.h | 3 +++ sound/soc/omap/omap-mcbsp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/sound/soc/omap/mcbsp.h b/sound/soc/omap/mcbsp.h index 61e93b1c185d..46ae1269a698 100644 --- a/sound/soc/omap/mcbsp.h +++ b/sound/soc/omap/mcbsp.h @@ -323,8 +323,11 @@ struct omap_mcbsp { unsigned int fmt; unsigned int in_freq; + unsigned int latency[2]; int clk_div; int wlen; + + struct pm_qos_request pm_qos_req; }; void omap_mcbsp_config(struct omap_mcbsp *mcbsp, diff --git a/sound/soc/omap/omap-mcbsp.c b/sound/soc/omap/omap-mcbsp.c index d018e966e533..6b40bdbef336 100644 --- a/sound/soc/omap/omap-mcbsp.c +++ b/sound/soc/omap/omap-mcbsp.c @@ -157,6 +157,17 @@ static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) { struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); + int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); + int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; + int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; + + if (mcbsp->latency[stream2]) + pm_qos_update_request(&mcbsp->pm_qos_req, + mcbsp->latency[stream2]); + else if (mcbsp->latency[stream1]) + pm_qos_remove_request(&mcbsp->pm_qos_req); + + mcbsp->latency[stream1] = 0; if (!cpu_dai->active) { omap_mcbsp_free(mcbsp); @@ -164,6 +175,28 @@ static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream, } } +static int omap_mcbsp_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); + struct pm_qos_request *pm_qos_req = &mcbsp->pm_qos_req; + int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); + int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; + int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; + int latency = mcbsp->latency[stream2]; + + /* Prevent omap hardware from hitting off between FIFO fills */ + if (!latency || mcbsp->latency[stream1] < latency) + latency = mcbsp->latency[stream1]; + + if (pm_qos_request_active(pm_qos_req)) + pm_qos_update_request(pm_qos_req, latency); + else if (latency) + pm_qos_add_request(pm_qos_req, PM_QOS_CPU_DMA_LATENCY, latency); + + return 0; +} + static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *cpu_dai) { @@ -226,6 +259,7 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, int wlen, channels, wpf; int pkt_size = 0; unsigned int format, div, framesize, master; + unsigned int buffer_size = mcbsp->pdata->buffer_size; dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream); channels = params_channels(params); @@ -240,7 +274,9 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, default: return -EINVAL; } - if (mcbsp->pdata->buffer_size) { + if (buffer_size) { + int latency; + if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) { int period_words, max_thrsh; int divider = 0; @@ -271,6 +307,12 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, /* Use packet mode for non mono streams */ pkt_size = channels; } + + latency = ((((buffer_size - pkt_size) / channels) * 1000) + / (params->rate_num / params->rate_den)); + + mcbsp->latency[substream->stream] = latency; + omap_mcbsp_set_threshold(substream, pkt_size); } @@ -554,6 +596,7 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, static const struct snd_soc_dai_ops mcbsp_dai_ops = { .startup = omap_mcbsp_dai_startup, .shutdown = omap_mcbsp_dai_shutdown, + .prepare = omap_mcbsp_dai_prepare, .trigger = omap_mcbsp_dai_trigger, .delay = omap_mcbsp_dai_delay, .hw_params = omap_mcbsp_dai_hw_params, @@ -835,6 +878,9 @@ static int asoc_mcbsp_remove(struct platform_device *pdev) if (mcbsp->pdata->ops && mcbsp->pdata->ops->free) mcbsp->pdata->ops->free(mcbsp->id); + if (pm_qos_request_active(&mcbsp->pm_qos_req)) + pm_qos_remove_request(&mcbsp->pm_qos_req); + omap_mcbsp_cleanup(mcbsp); clk_put(mcbsp->fclk);