From: Nicolin Chen <nicoleotsuka@gmail.com>
To: jdelvare@suse.com, linux@roeck-us.net
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
corbet@lwn.net, linux-doc@vger.kernel.org
Subject: [PATCH v2 1/2] hwmon: (ina3221) Do not read-back to cache reg_config
Date: Wed, 17 Apr 2019 16:12:09 -0700 [thread overview]
Message-ID: <20190417231210.5762-2-nicoleotsuka@gmail.com> (raw)
In-Reply-To: <20190417231210.5762-1-nicoleotsuka@gmail.com>
Reading back the CONFIG register increases an extra I2C
transaction. This's not necessary and could be replaced
with a local variable caching the register settings.
So this patch replaces two readback regmap_read() calls
with a tmp variable.
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
---
drivers/hwmon/ina3221.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
index 74d39d212931..62040aac653c 100644
--- a/drivers/hwmon/ina3221.c
+++ b/drivers/hwmon/ina3221.c
@@ -309,21 +309,22 @@ static int ina3221_write_chip(struct device *dev, u32 attr, long val)
{
struct ina3221_data *ina = dev_get_drvdata(dev);
int ret, idx;
+ u32 tmp;
switch (attr) {
case hwmon_chip_samples:
idx = find_closest(val, ina3221_avg_samples,
ARRAY_SIZE(ina3221_avg_samples));
- ret = regmap_update_bits(ina->regmap, INA3221_CONFIG,
- INA3221_CONFIG_AVG_MASK,
- idx << INA3221_CONFIG_AVG_SHIFT);
+ tmp = (ina->reg_config & ~INA3221_CONFIG_AVG_MASK) |
+ (idx << INA3221_CONFIG_AVG_SHIFT);
+ ret = regmap_write(ina->regmap, INA3221_CONFIG, tmp);
if (ret)
return ret;
/* Update reg_config accordingly */
- return regmap_read(ina->regmap, INA3221_CONFIG,
- &ina->reg_config);
+ ina->reg_config = tmp;
+ return 0;
default:
return -EOPNOTSUPP;
}
@@ -359,6 +360,7 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable)
struct ina3221_data *ina = dev_get_drvdata(dev);
u16 config, mask = INA3221_CONFIG_CHx_EN(channel);
u16 config_old = ina->reg_config & mask;
+ u32 tmp;
int ret;
config = enable ? mask : 0;
@@ -377,14 +379,13 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable)
}
/* Enable or disable the channel */
- ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config);
+ tmp = (ina->reg_config & ~mask) | (config & mask);
+ ret = regmap_write(ina->regmap, INA3221_CONFIG, tmp);
if (ret)
goto fail;
/* Cache the latest config register value */
- ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
- if (ret)
- goto fail;
+ ina->reg_config = tmp;
/* For disabling routine, decrease refcount or suspend() at last */
if (!enable)
--
2.17.1
next prev parent reply other threads:[~2019-04-17 23:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-17 23:12 [PATCH v2 0/2] hwmon: (ina3221) Two changes for INA3221 Nicolin Chen
2019-04-17 23:12 ` Nicolin Chen [this message]
2019-04-18 13:37 ` [PATCH v2 1/2] hwmon: (ina3221) Do not read-back to cache reg_config Guenter Roeck
2019-04-17 23:12 ` [PATCH v2 2/2] hwmon: (ina3221) Add voltage conversion time settings Nicolin Chen
2019-04-18 13:45 ` Guenter Roeck
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=20190417231210.5762-2-nicoleotsuka@gmail.com \
--to=nicoleotsuka@gmail.com \
--cc=corbet@lwn.net \
--cc=jdelvare@suse.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
/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;
as well as URLs for NNTP newsgroup(s).