From: David Lechner <dlechner@baylibre.com>
To: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Cc: jic23@kernel.org, lars@metafoo.de, mcoquelin.stm32@gmail.com,
alexandre.torgue@foss.st.com, u.kleine-koenig@baylibre.com,
tgamblin@baylibre.com, fabrice.gasnier@st.com,
benjamin.gaignard@linaro.org, lee@kernel.org,
linux-iio@vger.kernel.org,
linux-stm32@st-md-mailman.stormreply.com,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] iio: trigger: stm32-timer-trigger: Add check for clk_enable()
Date: Mon, 11 Nov 2024 15:14:58 -0600 [thread overview]
Message-ID: <b2f6db15-51a8-498d-ab5b-52f0f6a2e098@baylibre.com> (raw)
In-Reply-To: <CANeGvZVE6fX5hV-p1xXsGR=Z=pABzDtvV9wY_XBbLwJUWNVtyQ@mail.gmail.com>
On 11/11/24 2:36 PM, Jiasheng Jiang wrote:
> On Mon, Nov 11, 2024 at 2:45 PM David Lechner <dlechner@baylibre.com> wrote:
>>
>> On 11/11/24 1:19 PM, Jiasheng Jiang wrote:
>>> Add check for the return value of clk_enable() in order to catch the
>>> potential exception.
>>>
>>> Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
>>> ---
>>> Changelog:
>>>
>>> v2 -> v3:
>>>
>>> 1. Simplify code with cleanup helpers.
>>>
>>> v1 -> v2:
>>>
>>> 1. Remove unsuitable dev_err_probe().
>>> ---
>>
>> ...
>>
>>> @@ -492,21 +495,25 @@ static int stm32_counter_write_raw(struct iio_dev *indio_dev,
>>> return -EINVAL;
>>>
>>> case IIO_CHAN_INFO_ENABLE:
>>> - mutex_lock(&priv->lock);
>>> - if (val) {
>>> - if (!priv->enabled) {
>>> - priv->enabled = true;
>>> - clk_enable(priv->clk);
>>> - }
>>> - regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>>> - } else {
>>> - regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>>> - if (priv->enabled) {
>>> - priv->enabled = false;
>>> - clk_disable(priv->clk);
>>> +
>>> + scoped_guard(mutex, &priv->lock) {
>>> + if (val) {
>>> + if (!priv->enabled) {
>>> + priv->enabled = true;
>>> + ret = clk_enable(priv->clk);
>>> + if (ret)
>>> + return ret;
>>> + }
>>> + regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>>> + } else {
>>> + regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>>> + if (priv->enabled) {
>>> + priv->enabled = false;
>>> + clk_disable(priv->clk);
>>> + }
>>> }
>>> }
>>> - mutex_unlock(&priv->lock);
>>> +
>>> return 0;
>>> }
>>
>>
>> Another way to do this that avoids changing the indent
>> so much is placing braces around the case body like this.
>> This also avoids the compile error from using guard after
>> case directly.
>>
>>
>> case IIO_CHAN_INFO_ENABLE: {
>> guard(mutex)(&priv->lock);
>>
>> if (val) {
>> if (!priv->enabled) {
>> priv->enabled = true;
>> ret = clk_enable(priv->clk);
>> if (ret)
>> return ret;
>> }
>> regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>> } else {
>> regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
>> if (priv->enabled) {
>> priv->enabled = false;
>> clk_disable(priv->clk);
>> }
>> }
>>
>> return 0;
>> }
>>
>
> Looks great.
> But there is no indentation between "switch" and "case".
> As a result, the closing braces of "switch" and "case" will
> be placed in the same column.
>
> Like this:
>
> switch(mask) {
> case IIO_CHAN_INFO_ENABLE: {
>
> }
> }
>
> -Jiasheng
Usually, there is a default: case as well, so we could move the
final return and make it look like this:
switch (mask) {
case IIO_CHAN_INFO_RAW:
return regmap_write(priv->regmap, TIM_CNT, val);
case IIO_CHAN_INFO_SCALE:
/* fixed scale */
return -EINVAL;
case IIO_CHAN_INFO_ENABLE: {
guard(mutex)(&priv->lock);
if (val) {
if (!priv->enabled) {
priv->enabled = true;
clk_enable(priv->clk);
}
regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
} else {
regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
if (priv->enabled) {
priv->enabled = false;
clk_disable(priv->clk);
}
}
return 0;
}
default:
return -EINVAL;
}
And it is unusual, but I found kvm_arm_pmu_v3_get_attr() that
also has this double inline brace at the end of a switch statement.
}
}
So even if it doesn't look so nice, it does seem to be the
"correct" style.
next prev parent reply other threads:[~2024-11-11 22:31 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-11 19:19 [PATCH v3] iio: trigger: stm32-timer-trigger: Add check for clk_enable() Jiasheng Jiang
2024-11-11 19:45 ` David Lechner
2024-11-11 20:36 ` Jiasheng Jiang
2024-11-11 21:14 ` David Lechner [this message]
2024-11-11 22:18 ` Jiasheng Jiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b2f6db15-51a8-498d-ab5b-52f0f6a2e098@baylibre.com \
--to=dlechner@baylibre.com \
--cc=alexandre.torgue@foss.st.com \
--cc=benjamin.gaignard@linaro.org \
--cc=fabrice.gasnier@st.com \
--cc=jiashengjiangcool@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=lee@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=tgamblin@baylibre.com \
--cc=u.kleine-koenig@baylibre.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox