From: Joao Paulo Menezes Linaris <jplinaris@usp.br>
To: ilpo.jarvinen@linux.intel.com, wbg@kernel.org
Cc: Joao Paulo Menezes Linaris <jplinaris@usp.br>,
Guilherme Dias <guilhermeabreu200105@usp.br>,
linux-iio@vger.kernel.org
Subject: [PATCH] counter: intel-qep: Replace manual mutex logic with lock guards
Date: Wed, 22 Apr 2026 18:05:37 -0300 [thread overview]
Message-ID: <20260422210537.12127-1-jplinaris@usp.br> (raw)
Use scoped_guard() for handling mutex lock instead of locking and unlocking mutex explicitly. This improves readability by eliminating the need for gotos and by clearly indicating mutex will be locked only when execution is in scoped_guard() scope.
Signed-off-by: Joao Paulo Menezes Linaris <jplinaris@usp.br>
Co-developed-by: Guilherme Dias <guilhermeabreu200105@usp.br>
Signed-off-by: Guilherme Dias <guilhermeabreu200105@usp.br>
---
drivers/counter/intel-qep.c | 126 +++++++++++++++++-------------------
1 file changed, 58 insertions(+), 68 deletions(-)
diff --git a/drivers/counter/intel-qep.c b/drivers/counter/intel-qep.c
index c49c17805..199a8d562 100644
--- a/drivers/counter/intel-qep.c
+++ b/drivers/counter/intel-qep.c
@@ -188,25 +188,22 @@ static int intel_qep_ceiling_write(struct counter_device *counter,
struct counter_count *count, u64 max)
{
struct intel_qep *qep = counter_priv(counter);
- int ret = 0;
/* Intel QEP ceiling configuration only supports 32-bit values */
if (max != (u32)max)
return -ERANGE;
- mutex_lock(&qep->lock);
- if (qep->enabled) {
- ret = -EBUSY;
- goto out;
- }
+ /* Lock mutex while execution in scoped_guard() scope */
+ scoped_guard(mutex, &qep->lock){
+ if (qep->enabled)
+ return -EBUSY;
- pm_runtime_get_sync(qep->dev);
- intel_qep_writel(qep, INTEL_QEPMAX, max);
- pm_runtime_put(qep->dev);
+ pm_runtime_get_sync(qep->dev);
+ intel_qep_writel(qep, INTEL_QEPMAX, max);
+ pm_runtime_put(qep->dev);
+ }
-out:
- mutex_unlock(&qep->lock);
- return ret;
+ return 0;
}
static int intel_qep_enable_read(struct counter_device *counter,
@@ -226,28 +223,28 @@ static int intel_qep_enable_write(struct counter_device *counter,
u32 reg;
bool changed;
- mutex_lock(&qep->lock);
- changed = val ^ qep->enabled;
- if (!changed)
- goto out;
-
- pm_runtime_get_sync(qep->dev);
- reg = intel_qep_readl(qep, INTEL_QEPCON);
- if (val) {
- /* Enable peripheral and keep runtime PM always on */
- reg |= INTEL_QEPCON_EN;
- pm_runtime_get_noresume(qep->dev);
- } else {
- /* Let runtime PM be idle and disable peripheral */
- pm_runtime_put_noidle(qep->dev);
- reg &= ~INTEL_QEPCON_EN;
+ /* Lock mutex while execution in scoped_guard() scope */
+ scoped_guard(mutex, &qep->lock){
+ changed = val ^ qep->enabled;
+ if (!changed)
+ return 0;
+
+ pm_runtime_get_sync(qep->dev);
+ reg = intel_qep_readl(qep, INTEL_QEPCON);
+ if (val) {
+ /* Enable peripheral and keep runtime PM always on */
+ reg |= INTEL_QEPCON_EN;
+ pm_runtime_get_noresume(qep->dev);
+ } else {
+ /* Let runtime PM be idle and disable peripheral */
+ pm_runtime_put_noidle(qep->dev);
+ reg &= ~INTEL_QEPCON_EN;
+ }
+ intel_qep_writel(qep, INTEL_QEPCON, reg);
+ pm_runtime_put(qep->dev);
+ qep->enabled = val;
}
- intel_qep_writel(qep, INTEL_QEPCON, reg);
- pm_runtime_put(qep->dev);
- qep->enabled = val;
-out:
- mutex_unlock(&qep->lock);
return 0;
}
@@ -279,7 +276,6 @@ static int intel_qep_spike_filter_ns_write(struct counter_device *counter,
struct intel_qep *qep = counter_priv(counter);
u32 reg;
bool enable;
- int ret = 0;
/*
* Spike filter length is (MAX_COUNT + 2) clock periods.
@@ -300,25 +296,23 @@ static int intel_qep_spike_filter_ns_write(struct counter_device *counter,
if (length > INTEL_QEPFLT_MAX_COUNT(length))
return -ERANGE;
- mutex_lock(&qep->lock);
- if (qep->enabled) {
- ret = -EBUSY;
- goto out;
+ /* Lock mutex while execution in scoped_guard() scope */
+ scoped_guard(mutex, &qep->lock){
+ if (qep->enabled)
+ return -EBUSY;
+
+ pm_runtime_get_sync(qep->dev);
+ reg = intel_qep_readl(qep, INTEL_QEPCON);
+ if (enable)
+ reg |= INTEL_QEPCON_FLT_EN;
+ else
+ reg &= ~INTEL_QEPCON_FLT_EN;
+ intel_qep_writel(qep, INTEL_QEPFLT, length);
+ intel_qep_writel(qep, INTEL_QEPCON, reg);
+ pm_runtime_put(qep->dev);
}
- pm_runtime_get_sync(qep->dev);
- reg = intel_qep_readl(qep, INTEL_QEPCON);
- if (enable)
- reg |= INTEL_QEPCON_FLT_EN;
- else
- reg &= ~INTEL_QEPCON_FLT_EN;
- intel_qep_writel(qep, INTEL_QEPFLT, length);
- intel_qep_writel(qep, INTEL_QEPCON, reg);
- pm_runtime_put(qep->dev);
-
-out:
- mutex_unlock(&qep->lock);
- return ret;
+ return 0;
}
static int intel_qep_preset_enable_read(struct counter_device *counter,
@@ -342,28 +336,24 @@ static int intel_qep_preset_enable_write(struct counter_device *counter,
{
struct intel_qep *qep = counter_priv(counter);
u32 reg;
- int ret = 0;
-
- mutex_lock(&qep->lock);
- if (qep->enabled) {
- ret = -EBUSY;
- goto out;
- }
- pm_runtime_get_sync(qep->dev);
- reg = intel_qep_readl(qep, INTEL_QEPCON);
- if (val)
- reg &= ~INTEL_QEPCON_COUNT_RST_MODE;
- else
- reg |= INTEL_QEPCON_COUNT_RST_MODE;
+ /* Lock mutex while execution in scoped_guard() scope */
+ scoped_guard(mutex, &qep->lock){
+ if (qep->enabled)
+ return -EBUSY;
- intel_qep_writel(qep, INTEL_QEPCON, reg);
- pm_runtime_put(qep->dev);
+ pm_runtime_get_sync(qep->dev);
+ reg = intel_qep_readl(qep, INTEL_QEPCON);
+ if (val)
+ reg &= ~INTEL_QEPCON_COUNT_RST_MODE;
+ else
+ reg |= INTEL_QEPCON_COUNT_RST_MODE;
-out:
- mutex_unlock(&qep->lock);
+ intel_qep_writel(qep, INTEL_QEPCON, reg);
+ pm_runtime_put(qep->dev);
+ }
- return ret;
+ return 0;
}
static struct counter_comp intel_qep_count_ext[] = {
--
2.34.1
next reply other threads:[~2026-04-22 21:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-22 21:05 Joao Paulo Menezes Linaris [this message]
2026-04-23 11:42 ` [PATCH] counter: intel-qep: Replace manual mutex logic with lock guards Ilpo Järvinen
2026-04-30 22:03 ` Joao Paulo Menezes Linaris
-- strict thread matches above, loose matches on Subject: below --
2026-05-08 3:40 Joao Paulo Menezes Linaris
2026-05-08 5:18 ` Maxwell Doose
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=20260422210537.12127-1-jplinaris@usp.br \
--to=jplinaris@usp.br \
--cc=guilhermeabreu200105@usp.br \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-iio@vger.kernel.org \
--cc=wbg@kernel.org \
/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