Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752303AbeAPMq6 (ORCPT + 1 other); Tue, 16 Jan 2018 07:46:58 -0500 Received: from mx08-00178001.pphosted.com ([91.207.212.93]:33829 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750852AbeAPMpW (ORCPT ); Tue, 16 Jan 2018 07:45:22 -0500 From: Fabrice Gasnier To: , , , , CC: , , , , , , , , Subject: [PATCH 2/8] pwm: stm32: protect common prescaler for all channels Date: Tue, 16 Jan 2018 13:43:45 +0100 Message-ID: <1516106631-18722-3-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1516106631-18722-1-git-send-email-fabrice.gasnier@st.com> References: <1516106631-18722-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.47] X-ClientProxiedBy: SFHDAG8NODE2.st.com (10.75.127.23) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-01-16_05:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: There may be a race, when configuring two pwm channels, with different prescaler values, when there's no active channel yet. Add mutex lock to avoid concurrent access on pwm apply state. This is also precursor patch for pwm capture support. Signed-off-by: Fabrice Gasnier --- drivers/pwm/pwm-stm32.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c index 59524f9..3ac55df 100644 --- a/drivers/pwm/pwm-stm32.c +++ b/drivers/pwm/pwm-stm32.c @@ -21,6 +21,7 @@ struct stm32_pwm { struct pwm_chip chip; + struct mutex lock; /* protect pwm config/enable */ struct clk *clk; struct regmap *regmap; u32 max_arr; @@ -213,9 +214,23 @@ static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, return ret; } +static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm, + struct pwm_state *state) +{ + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); + int ret; + + /* protect common prescaler for all active channels */ + mutex_lock(&priv->lock); + ret = stm32_pwm_apply(chip, pwm, state); + mutex_unlock(&priv->lock); + + return ret; +} + static const struct pwm_ops stm32pwm_ops = { .owner = THIS_MODULE, - .apply = stm32_pwm_apply, + .apply = stm32_pwm_apply_locked, }; static int stm32_pwm_set_breakinput(struct stm32_pwm *priv, @@ -335,6 +350,7 @@ static int stm32_pwm_probe(struct platform_device *pdev) if (!priv) return -ENOMEM; + mutex_init(&priv->lock); priv->regmap = ddata->regmap; priv->clk = ddata->clk; priv->max_arr = ddata->max_arr; -- 1.9.1