Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1711098ybv; Fri, 14 Feb 2020 04:46:23 -0800 (PST) X-Google-Smtp-Source: APXvYqzT4r9cpwhuhe88afwtM0PPcEJI980Wfc7B/m/lMg3puBEosne0eo/xTf/NixfNRaiduYhU X-Received: by 2002:a05:6830:13d3:: with SMTP id e19mr2122222otq.135.1581684383171; Fri, 14 Feb 2020 04:46:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581684383; cv=none; d=google.com; s=arc-20160816; b=j+jPx/X1oN1CZEOX4O/qBBxEur2rJk+CdRm0i49OCWgahkdWN0ie1Asm+zKix+D5/8 i/+CXRpnRXz1w9VFnxXV7UGDATnpqrwuAcISPUX3XWokRLSOCBOTsSeJePC3AWoMaMv4 PXBPH+PLlzbPq8u36bg4NJHJ5r9wd8f0ggY7J14H496r3rN3V9iqv078twW/tJ0EVvGo qXWhPlzaHHl0j3xaD7gFwl02RlNdBKkSQQmwOQxbK2Yyfro1MD96mT+qfU1sG++yIx3r kzeKr90JTplTPHLHuVllj/7Gu28PzSV03ChsM+f5JDI7fW2Y+hecshnJ5GYOm7VvOfCC B9xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=RsCSDCDFTe0wWS69WP64v5B7Y4cFPScur6Ypw0ChpYU=; b=FgLWl1Ya14ujakuGpREjea9tHMHmnhRimSiAoRcL9cBslbj3yEhw/H8yBag/nRbyJN 0TNhfTJOhVM97CC8J7O6K7O8kwCMYr9+hkh5cHaI6VOr/qsbwKRu3WvHUMaASEUJ8kbz LrzfEk8ddOgetU/x4QsxnmsASrInzJBOwFd1j+Tdl+oeLJM88/k1Trjw3593cYVAaJOR l8oxF4W1NNpRZUOezb0pKjWLNAxT8m0qIpUKNAIbG2cqXHyXjX5LLQavULDGq2Wyqvjp JOepr2+VUhPoHOa2MPKPQMRBengldaXBvDVOipso+TPqF/UcYyblky2sAi7Ssm63NiD6 q+vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EJVEl0Pj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i78si2818580oib.1.2020.02.14.04.46.11; Fri, 14 Feb 2020 04:46:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EJVEl0Pj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729125AbgBNMpp (ORCPT + 99 others); Fri, 14 Feb 2020 07:45:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:38126 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727822AbgBNMpp (ORCPT ); Fri, 14 Feb 2020 07:45:45 -0500 Received: from archlinux (cpc149474-cmbg20-2-0-cust94.5-4.cable.virginm.net [82.4.196.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 45AA820675; Fri, 14 Feb 2020 12:45:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581684343; bh=L11LgvfvPife2NQLp25fa0qA4b8xQO9XV9Ewz0cn3OA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=EJVEl0PjCob4ckz1ww9HSHih0z/eutsImM7F2lSFEZx4eb6+IzWYvzEqONq/xeqAO kcuBuai37ObfoxWSjExVa+R1wv0SN0BCei+agomUauxuxoeeKdc7NP3Kn7KfkSXevU wJA5r7d3dZQxUFEfcjjP2WHEJBlMXGCh+Xg5RX+k= Date: Fri, 14 Feb 2020 12:45:37 +0000 From: Jonathan Cameron To: Fabrice Gasnier Cc: , , , , , , , Subject: Re: [PATCH v2] counter: stm32-timer-cnt: add power management support Message-ID: <20200214124537.1b870746@archlinux> In-Reply-To: <1581355198-30428-1-git-send-email-fabrice.gasnier@st.com> References: <1581355198-30428-1-git-send-email-fabrice.gasnier@st.com> X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 10 Feb 2020 18:19:58 +0100 Fabrice Gasnier wrote: > Add suspend/resume PM sleep ops. When going to low power, enforce the > counter isn't active. Gracefully restore its state upon resume in case > it's been left enabled prior to suspend. > > Acked-by: William Breathitt Gray > Signed-off-by: Fabrice Gasnier Looks good to me. Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it. Thanks, Jonathan > --- > Changes in v2: > - Don't refuse to suspend in case the counter has been left enabled. > Gracefully disable it and restore its state upon resume. > --- > drivers/counter/stm32-timer-cnt.c | 63 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 63 insertions(+) > > diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c > index 3eafcce..50496f4 100644 > --- a/drivers/counter/stm32-timer-cnt.c > +++ b/drivers/counter/stm32-timer-cnt.c > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > #include > > #define TIM_CCMR_CCXS (BIT(8) | BIT(0)) > @@ -20,11 +21,20 @@ > #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \ > TIM_CCER_CC2P | TIM_CCER_CC2NP) > > +struct stm32_timer_regs { > + u32 cr1; > + u32 cnt; > + u32 smcr; > + u32 arr; > +}; > + > struct stm32_timer_cnt { > struct counter_device counter; > struct regmap *regmap; > struct clk *clk; > u32 ceiling; > + bool enabled; > + struct stm32_timer_regs bak; > }; > > /** > @@ -224,6 +234,9 @@ static ssize_t stm32_count_enable_write(struct counter_device *counter, > clk_disable(priv->clk); > } > > + /* Keep enabled state to properly handle low power states */ > + priv->enabled = enable; > + > return len; > } > > @@ -358,10 +371,59 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev) > priv->counter.num_signals = ARRAY_SIZE(stm32_signals); > priv->counter.priv = priv; > > + platform_set_drvdata(pdev, priv); > + > /* Register Counter device */ > return devm_counter_register(dev, &priv->counter); > } > > +static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev) > +{ > + struct stm32_timer_cnt *priv = dev_get_drvdata(dev); > + > + /* Only take care of enabled counter: don't disturb other MFD child */ > + if (priv->enabled) { > + /* Backup registers that may get lost in low power mode */ > + regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr); > + regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr); > + regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt); > + regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1); > + > + /* Disable the counter */ > + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); > + clk_disable(priv->clk); > + } > + > + return pinctrl_pm_select_sleep_state(dev); > +} > + > +static int __maybe_unused stm32_timer_cnt_resume(struct device *dev) > +{ > + struct stm32_timer_cnt *priv = dev_get_drvdata(dev); > + int ret; > + > + ret = pinctrl_pm_select_default_state(dev); > + if (ret) > + return ret; > + > + if (priv->enabled) { > + clk_enable(priv->clk); > + > + /* Restore registers that may have been lost */ > + regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr); > + regmap_write(priv->regmap, TIM_ARR, priv->bak.arr); > + regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt); > + > + /* Also re-enables the counter */ > + regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1); > + } > + > + return 0; > +} > + > +static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend, > + stm32_timer_cnt_resume); > + > static const struct of_device_id stm32_timer_cnt_of_match[] = { > { .compatible = "st,stm32-timer-counter", }, > {}, > @@ -373,6 +435,7 @@ static struct platform_driver stm32_timer_cnt_driver = { > .driver = { > .name = "stm32-timer-counter", > .of_match_table = stm32_timer_cnt_of_match, > + .pm = &stm32_timer_cnt_pm_ops, > }, > }; > module_platform_driver(stm32_timer_cnt_driver);