From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 06/11] hwmon: (ina2xx) Re-initialize chip using regmap functions
Date: Tue, 27 Aug 2024 08:34:50 -0700 [thread overview]
Message-ID: <20240827153455.1344529-7-linux@roeck-us.net> (raw)
In-Reply-To: <20240827153455.1344529-1-linux@roeck-us.net>
If it is necessary to re-initialize the chip, for example because
it has been power cycled, use regmap functions to update register
contents. This ensures that all registers, including the configuration
register and alert registers, are updated to previously configured
values without having to locally cache everything.
For this to work, volatile registers have to be marked as volatile.
Also, the cache needs to be bypassed when reading the calibration
and mask_enable registers. While the calibration register is not
volatile, it will be reset to 0 if the chip has been power cycled.
Most of the bits in the mask_enable register are configuration bits,
except for bit 4 which reports if an alert has ben observed.
Both registers need to be marked as non-volatile to be updated
after a power cycle, but it is necessary to bypass the cache when
reading them to detect if the chip has been power cycled and to
read the alert status.
Another necessary change is to declare ina226_alert_to_reg() as u16.
So far it returned an s16 which is sign extended to a large negative
value which is then sent to regmap as unsigned int, causing an -EINVAL
error return.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/ina2xx.c | 48 ++++++++++++++++++++++++++++++++++--------
1 file changed, 39 insertions(+), 9 deletions(-)
diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c
index ed8764a29d3f..f7d78588e579 100644
--- a/drivers/hwmon/ina2xx.c
+++ b/drivers/hwmon/ina2xx.c
@@ -91,10 +91,39 @@
*/
#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
+static bool ina2xx_writeable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case INA2XX_CONFIG:
+ case INA2XX_CALIBRATION:
+ case INA226_MASK_ENABLE:
+ case INA226_ALERT_LIMIT:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool ina2xx_volatile_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case INA2XX_SHUNT_VOLTAGE:
+ case INA2XX_BUS_VOLTAGE:
+ case INA2XX_POWER:
+ case INA2XX_CURRENT:
+ return true;
+ default:
+ return false;
+ }
+}
+
static const struct regmap_config ina2xx_regmap_config = {
.reg_bits = 8,
.val_bits = 16,
.max_register = INA2XX_MAX_REGISTERS,
+ .cache_type = REGCACHE_MAPLE,
+ .volatile_reg = ina2xx_volatile_reg,
+ .writeable_reg = ina2xx_writeable_reg,
};
enum ina2xx_ids { ina219, ina226 };
@@ -229,16 +258,16 @@ static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
if (*regval == 0) {
unsigned int cal;
- ret = regmap_read(regmap, INA2XX_CALIBRATION, &cal);
+ ret = regmap_read_bypassed(regmap, INA2XX_CALIBRATION, &cal);
if (ret < 0)
return ret;
if (cal == 0) {
dev_warn(dev, "chip not calibrated, reinitializing\n");
- ret = ina2xx_init(data);
- if (ret < 0)
- return ret;
+ regcache_mark_dirty(regmap);
+ regcache_sync(regmap);
+
/*
* Let's make sure the power and current
* registers have been updated before trying
@@ -340,7 +369,7 @@ static int ina226_reg_to_alert(struct ina2xx_data *data, u32 mask, u16 regval)
* Turns alert limit values into register values.
* Opposite of the formula in ina2xx_get_value().
*/
-static s16 ina226_alert_to_reg(struct ina2xx_data *data, u32 mask, int val)
+static u16 ina226_alert_to_reg(struct ina2xx_data *data, u32 mask, int val)
{
switch (mask) {
case INA226_SHUNT_OVER_VOLTAGE_MASK:
@@ -439,16 +468,17 @@ static ssize_t ina226_alarm_show(struct device *dev,
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ina2xx_data *data = dev_get_drvdata(dev);
- int regval;
+ unsigned int mask;
int alarm = 0;
int ret;
- ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val);
+ ret = regmap_read_bypassed(data->regmap, INA226_MASK_ENABLE, &mask);
if (ret)
return ret;
- alarm = (regval & attr->index) &&
- (regval & INA226_ALERT_FUNCTION_FLAG);
+ alarm = (mask & attr->index) &&
+ (mask & INA226_ALERT_FUNCTION_FLAG);
+
return sysfs_emit(buf, "%d\n", alarm);
}
--
2.45.2
next prev parent reply other threads:[~2024-08-27 15:35 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-27 15:34 [PATCH 00/11] hwmon: (ina2xx) Cleanup and convert to use with_info API Guenter Roeck
2024-08-27 15:34 ` [PATCH 01/11] hwmon: (ina2xx) Reorder include files to alphabetic order Guenter Roeck
2024-08-29 14:53 ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 02/11] hwmon: (ina2xx) Replace platform data with device properties Guenter Roeck
2024-08-29 14:53 ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 03/11] hwmon: (ina2xx) Use bit operations Guenter Roeck
2024-08-29 14:53 ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 04/11] hwmon: (ina2xx) Mark regmap_config as const Guenter Roeck
2024-08-29 14:54 ` Tzung-Bi Shih
2024-08-29 16:44 ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 05/11] hwmon: (ina2xx) Use local regmap pointer if used more than once Guenter Roeck
2024-08-29 14:54 ` Tzung-Bi Shih
2024-08-27 15:34 ` Guenter Roeck [this message]
2024-08-29 14:54 ` [PATCH 06/11] hwmon: (ina2xx) Re-initialize chip using regmap functions Tzung-Bi Shih
2024-08-29 16:45 ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 07/11] hwmon: (ina2xx) Set alert latch when enabling alerts Guenter Roeck
2024-08-29 14:54 ` Tzung-Bi Shih
2024-08-29 17:28 ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 08/11] hwmon: (ina2xx) Fix various overflow issues Guenter Roeck
2024-08-29 14:55 ` Tzung-Bi Shih
2024-08-29 16:56 ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 09/11] hwmon: (ina2xx) Consolidate chip initialization code Guenter Roeck
2024-08-29 14:55 ` Tzung-Bi Shih
2024-08-29 16:57 ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 10/11] hwmon: (ina2xx) Move ina2xx_get_value() Guenter Roeck
2024-08-29 14:55 ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 11/11] hwmon: (ina2xx) Convert to use with_info hwmon API Guenter Roeck
2024-08-29 14:55 ` Tzung-Bi Shih
2024-08-29 16:53 ` 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=20240827153455.1344529-7-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-hwmon@vger.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