* [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor
@ 2026-12-01 8:17 Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 1/2] dt-bindings: hwmon: Add TI INA4230 4-channel I2C " Alexey Charkov
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Alexey Charkov @ 2026-12-01 8:17 UTC (permalink / raw)
To: Alexey Charkov, Guenter Roeck, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: linux-hwmon, devicetree, linux-kernel, linux-doc,
Krzysztof Kozlowski
TI INA4230 is a 4-channel power monitor with I2C interface, similar in
operation to INA3221 (3-channel) and INA219 (single-channel) but with
a different register layout, different alerting mechanism and slightly
different support for directly reading calculated current/power/energy
values (pre-multiplied by the device itself and needing only to be scaled
by the driver depending on its selected LSB unit values).
In this initial implementation, the driver supports reading voltage,
current, power and energy values, but does not yet support alerts, which
can be added separately if needed. Also the support for the device's
internal 32-bit energy counter reset is not yet implemented.
An example device tree using this binding and driver is available at [1]
(not currently upstreamed, as the device in question is in engineering
phase and not yet publicly available)
[1] https://github.com/flipperdevices/flipper-linux-kernel/blob/flipper-devel/arch/arm64/boot/dts/rockchip/rk3576-flipper-one.dtsi
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Changes in v6:
- Add the optional interrupts property to the binding for the alert pin, and
keep ti,alert-polarity-active-high for alerts routed to SMBus Alert
(Sashiko, Rob Herring, Guenter Roeck). Krzysztof, I kept your R-b as this
is the only change to the binding - please shout if you'd rather re-review
- Drop the dummy in0 channel: bus voltages are now in0-in3 and shunt
voltages in4-in7 (Guenter Roeck, Sashiko)
- Flatten the current register table and look registers up only after the
attribute checks (Guenter Roeck, Sashiko)
- Size all arithmetic from the datasheet register widths and the u32 DT
cells, so nothing overflows on 32-bit platforms: report energy through
energy64 and clamp power to LONG_MAX (Sashiko)
- Compute the shunt range in 64 bits, as the 32-bit calculation overflowed
and picked the wrong shunt gain at default values (Sashiko)
- Treat power as unsigned, as per the datasheet (Sashiko)
- Take update_interval writes as long and clamp them (Sashiko, Guenter Roeck)
- Add a comment on how regmap_noinc_read() reads the 32-bit energy
registers (Guenter Roeck)
- Account for averaging in the reported update_interval: the chip updates
its registers only once all averages are complete, so my v5 NB on this
was wrong (Sashiko)
- Program CONFIG1 at probe: contrary to my v5 NB, the regcache sync on
resume had nothing to restore, so disconnected channels stayed enabled in
hardware (Sashiko)
- Set max_register and describe the readable and writeable registers:
without max_register, regcache_sync() covered only register 0 and
restored nothing on resume
- Keep CONFIG2 cached and issue the self-clearing reset with a plain SMBus
write below regmap, so shunt ranges and alert polarity survive suspend
(Sashiko)
- Drop the driver's own copies of CONFIG1 and CONFIG2 and rely on the regmap
cache alone: regmap updates its cache before the bus write, so a separate
copy could only drift from what the next sync applies
- Write registers one at a time, as the register pointer does not
auto-increment: syncing the adjacent CONFIG1 and CONFIG2 as one block left
CONFIG2 unwritten and failed resume with -EIO
- Write CONFIG2 at probe, so the alert polarity applies even when every
channel is disconnected
- Stop reloading CONFIG1 from the chip on suspend (Sashiko)
- Initialise the chip directly in probe, hold one runtime PM reference per
enabled channel, and drop them from a devm action that runs after the
hwmon device is gone, closing the race with inN_enable writes (Sashiko)
- Refuse to enable disconnected channels through inN_enable (Sashiko), and
reject inN_enable values other than 0 and 1
- Size the conversion time for one channel when none is enabled, instead of
a U32_MAX sentinel that find_closest() took as -1 on 32-bit builds
- Include linux/regulator/consumer.h directly
- Reject calibration values that round to zero, and compare the full-scale
shunt drop against the 4x gain range without truncating it first
- Correct the maximum update interval and drop unused includes
- Restore the configuration when a zero reading reveals that the chip has
lost its calibration after an external reset (e.g. an I2C general call)
or a voltage glitch, as ina2xx does
- Add Documentation/hwmon/ina4230.rst, including the limits of reset
detection
- Cite the datasheet equations behind the power and energy scale
- NB: Sashiko's concerns about pm_runtime_set_suspended() failing with a
non-zero usage count and about usage count underflow don't apply:
__pm_runtime_set_status() only checks disable_depth, and
pm_runtime_put_noidle() saturates at zero. The code in question is gone
after the PM rework anyway
- NB: Sashiko's concern about the sign of the bus voltage: the datasheet
calls VBUS "2's complement value, however always positive", and 48 V is
30000 LSBs, well within s16
- Tested on a board with five INA4230s on a shared I2C bus: probe-time
programming, runtime suspend/resume via inN_enable (including registers
clobbered while suspended), system suspend/resume via pm_test=devices
(restoring all chips from power-on state), suspend-to-RAM, and recovery
from an I2C general call reset
- Link to v5: https://lore.kernel.org/r/20260330-ina4230-v5-0-eeb322d95b3a@flipper.net
Changes in v5:
- Reworded per-channel subnodes description in the binding for clarity (Sashiko)
- NB: Sashiko's suggestion to allow interrupts in the binding sounds premature,
as the alerts mechanism is not implemented yet and there are no known users
to test it. If anyone has hardware with the alert pins wired to an interrupt
line - please shout and we can test/extend it together
- Avoid division by zero when setting the conversion time with all inputs
disabled (Sashiko)
- Added the missed HWMON_I_ENABLE bits (Sashiko)
- Dropped extra sysfs attributes for reading/writing shunt values, as the
implementation was potentially racy and it's unlikely anyone would resolder
the shunts on a running system (Sashiko)
- Skip pm_runtime_put_noidle() for disabled (not just disconnected) channels
in remove and probe error path to avoid refcount underflow (Sashiko)
- Mark CONFIG2 register as volatile, as the reset bits in it are self-clearing
- NB: Sashiko's inquiry about the update interval being underreported due to
not accounting for the number of averaging samples: no, the hardware still
reports updates after each channels * (vbus_ct + vsh_ct), but the reported
value changes slowly due to the averaging
- NB: Sashiko's inquiry about regmap_noinc_read(): same as Guenter's AI
feedback. No, it doesn't break the byte order, as it uses byte-sized reads
- NB: Sashiko's inquiry about potential falling of ina->reg_config1 out of sync
with the hardware upon failed regmap_write() calls: yes, but it will be
written at the next successful write call, so the worst that can happen is
the averaging / conversion time can be wrong for a while. And it will return
a failed status for the failed write call too, so no big deal.
- NB: Sashiko's inquiry about reg_config1 not being written out during probe:
it is written out when the device is runtime-resumed as the refcount gets
incremented during the probe function
- Link to v4: https://lore.kernel.org/r/20260326-ina4230-v4-0-c1e312c09de7@flipper.net
Changes in v4:
- Aligned the maximum value of ti,maximum-expected-current-microamp property
in the binding with the one expected by the driver (Guenter Roeck)
"2147A ought to be enough for anybody (c)"
- Actually requested the optional vs-supply regulator in the driver (Guenter Roeck)
- Program the ALERT_POL bit according to the value of ti,alert-polarity-active-high
even though the alerts themselves are not yet implemented (Guenter Roeck)
- Added a check for manually disabled channels in the is_enabled() function to
avoid reading invalid data from them (Guenter Roeck)
- Dropped support for the single-shot mode as its operation is not clearly
documented in the datasheet and there is no pressing need to support it (Guenter Roeck)
- NB: AI feedback regarding regmap_noinc_read() producing incorrect byte order on LE
hosts is incorrect, as its implementation does a byte-wise read and doesn't care
about the regmap value width or endianness flags, so it produces a 4-byte output
buffer in the same byte order as the device returns, which is BE in this case
- NB: AI feedback regarding fail-path pm_runtime_put_noidle() potentially being
unbalanced if the probe loop failed early is technically correct but practically
irrelevant, as the driver will simply fail to load, and the usage count won't
decrease beyond zero anyway. The alternatives are cumbersome for no real benefit
- Link to v3: https://lore.kernel.org/r/20260310-ina4230-v3-0-06ab3a77c570@flipper.net
Changes in v3:
- Updated the description of the ti,maximum-expected-current-microamp property
in the binding to clarify how it is used, and drop the irrelevant mention of
the PMbus (Guenter Roeck)
- Use div64_u64() instead of do_div() for the final division in the calibration value
calculation to avoid overflows in the denominator (Guenter Roeck)
- Avoid overflow while scaling the voltage values on 32-bit platforms (Guenter Roeck)
- Use regmap_noinc_read() instead of regmap_raw_read() for reading the energy values
to ensure that the regmap / bus driver don't wander off to adjacent registers
during the read operation (on INA4230 the whole 32 bits should be read from
the same register offset) (Guenter Roeck)
- Remove redundant call to ina4230_set_calibration() in the current read path,
as the calibration value is already set when enabling the channel and restored
across PM changes via regcache_sync() (Guenter Roeck)
- Add missing write_enable() function to make hwmon_in_enable writes work as
advertised in is_visible() (Guenter Roeck)
- Add a check for disabled channels before calling pm_runtime_put_noidle() on them
to avoid refcount underflow due to imbalanced get_sync/put_noidle calls (Guenter Roeck)
- Dropped unused include of linux/debugfs.h
- Add missing return checks on regmap_write() calls
- uO -> uOhm in the error message to avoid confusion
- Move probe-time calibration after enabling runtime PM to avoid it being reverted
by the PM sync
- Link to v2: https://lore.kernel.org/r/20260302-ina4230-v2-0-55b49d19d2ab@flipper.net
Changes in v2:
- Replace u64/u64 division with do_div() (kernel test robot)
- Add an example with ti,maximum-expected-current-microamp property in
bindings (Krzysztof Kozlowski)
- Include the newly added binding in MAINTAINERS file (Krzysztof Kozlowski)
- Use dev_err_probe() where appropriate in the driver (Krzysztof Kozlowski)
- Switch to devm_regmap_field_bulk_alloc() instead of an open-coded loop
- Add a bounds check for the calculated calibration value,
and a corresponding error message
- Link to v1: https://lore.kernel.org/r/20260225-ina4230-v1-0-92b1de981d46@flipper.net
---
Alexey Charkov (2):
dt-bindings: hwmon: Add TI INA4230 4-channel I2C power monitor
hwmon: Add support for TI INA4230 power monitor
.../devicetree/bindings/hwmon/ti,ina4230.yaml | 138 +++
Documentation/hwmon/ina4230.rst | 82 ++
Documentation/hwmon/index.rst | 1 +
MAINTAINERS | 8 +
drivers/hwmon/Kconfig | 11 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/ina4230.c | 1096 ++++++++++++++++++++
7 files changed, 1337 insertions(+)
---
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
change-id: 20260219-ina4230-74a02409153d
Best regards,
--
Alexey Charkov <alchark@flipper.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v6 1/2] dt-bindings: hwmon: Add TI INA4230 4-channel I2C power monitor
2026-12-01 8:17 [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor Alexey Charkov
@ 2026-12-01 8:17 ` Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 2/2] hwmon: Add support for TI INA4230 " Alexey Charkov
2026-12-01 8:37 ` [PATCH v6 0/2] Add support for Texas Instruments " sashiko-bot
2 siblings, 0 replies; 6+ messages in thread
From: Alexey Charkov @ 2026-12-01 8:17 UTC (permalink / raw)
To: Alexey Charkov, Guenter Roeck, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: linux-hwmon, devicetree, linux-kernel, linux-doc,
Krzysztof Kozlowski
Add TI INA4230, which is a 48V 4-channel 16-bit I2C-based
current/voltage/power/energy monitor with alert function.
Link: https://www.ti.com/product/INA4230
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
.../devicetree/bindings/hwmon/ti,ina4230.yaml | 138 +++++++++++++++++++++
MAINTAINERS | 6 +
2 files changed, 144 insertions(+)
diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml b/Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml
new file mode 100644
index 000000000000..40bf0841f1d2
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml
@@ -0,0 +1,138 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/ti,ina4230.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Texas Instruments INA4230 quad-channel power monitors
+
+maintainers:
+ - Alexey Charkov <alchark@flipper.net>
+
+description: |
+ The INA4230 is a 48V quad-channel 16-bit current, voltage, power and energy
+ monitor with an I2C interface.
+
+ Datasheet:
+ https://www.ti.com/product/INA4230
+
+properties:
+ compatible:
+ enum:
+ - ti,ina4230
+
+ reg:
+ maxItems: 1
+
+ "#address-cells":
+ description: Required only if a child node is present.
+ const: 1
+
+ "#size-cells":
+ description: Required only if a child node is present.
+ const: 0
+
+ interrupts:
+ description: Optional alert interrupt.
+ maxItems: 1
+
+ vs-supply:
+ description: phandle to the regulator that provides the VS supply typically
+ in range from 1.7 V to 5.5 V.
+
+ ti,alert-polarity-active-high:
+ description: Alert pin is asserted based on the value of Alert polarity Bit
+ of the CONFIG2 register. Default value is 0, for which the alert pin
+ toggles from high to low during faults. When this property is set, the
+ corresponding register bit is set to 1, and the alert pin toggles from
+ low to high during faults.
+ $ref: /schemas/types.yaml#/definitions/flag
+
+patternProperties:
+ "^input@[0-3]$":
+ description: Optional subnodes for four input channels. Each subnode
+ describes one input channel. Input channels default to enabled in the
+ chip. Unless channels are explicitly disabled in device-tree, input
+ channels will be enabled.
+ type: object
+ additionalProperties: false
+ properties:
+ reg:
+ description: Must be 0, 1, 2 or 3, corresponding to the IN1, IN2, IN3
+ or IN4 ports of the INA4230, respectively.
+ enum: [ 0, 1, 2, 3 ]
+
+ label:
+ description: name of the input source
+
+ shunt-resistor-micro-ohms:
+ description: shunt resistor value in micro-Ohm
+
+ ti,maximum-expected-current-microamp:
+ description: |
+ This value indicates the maximum current in microamps that you can
+ expect to measure with ina4230 in your circuit.
+
+ This value will be used to calculate the Current_LSB to maximize the
+ available precision while ensuring your expected maximum current fits
+ within the chip's ADC range. It will also enable built-in shunt gain
+ to increase ADC granularity by a factor of 4 if the provided maximum
+ current / shunt resistance combination does not produce more than
+ 20.48 mV drop at the shunt.
+ minimum: 32768
+ maximum: 2147483647
+ default: 32768000
+
+ required:
+ - reg
+
+required:
+ - compatible
+ - reg
+
+allOf:
+ - $ref: hwmon-common.yaml#
+
+unevaluatedProperties: false
+
+examples:
+ - |
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ power-sensor@44 {
+ compatible = "ti,ina4230";
+ reg = <0x44>;
+ vs-supply = <&vdd_3v0>;
+ ti,alert-polarity-active-high;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ input@0 {
+ reg = <0x0>;
+ /*
+ * Input channels are enabled by default in the device and so
+ * to disable, must be explicitly disabled in device-tree.
+ */
+ status = "disabled";
+ };
+
+ input@1 {
+ reg = <0x1>;
+ shunt-resistor-micro-ohms = <50000>;
+ ti,maximum-expected-current-microamp = <300000>;
+ };
+
+ input@2 {
+ reg = <0x2>;
+ label = "VDD_5V";
+ shunt-resistor-micro-ohms = <10000>;
+ ti,maximum-expected-current-microamp = <5000000>;
+ };
+
+ input@3 {
+ reg = <0x3>;
+ };
+ };
+ };
diff --git a/MAINTAINERS b/MAINTAINERS
index e7652ab2061a..5aed784b245d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13166,6 +13166,12 @@ S: Maintained
F: Documentation/hwmon/ina233.rst
F: drivers/hwmon/pmbus/ina233.c
+INA4230 HWMON DRIVER
+M: Alexey Charkov <alchark@flipper.net>
+L: linux-hwmon@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml
+
INDEX OF FURTHER KERNEL DOCUMENTATION
M: Carlos Bilbao <carlos.bilbao@kernel.org>
S: Maintained
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v6 2/2] hwmon: Add support for TI INA4230 power monitor
2026-12-01 8:17 [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 1/2] dt-bindings: hwmon: Add TI INA4230 4-channel I2C " Alexey Charkov
@ 2026-12-01 8:17 ` Alexey Charkov
2026-12-01 8:37 ` sashiko-bot
2026-12-01 8:37 ` [PATCH v6 0/2] Add support for Texas Instruments " sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Alexey Charkov @ 2026-12-01 8:17 UTC (permalink / raw)
To: Alexey Charkov, Guenter Roeck, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: linux-hwmon, devicetree, linux-kernel, linux-doc
Add a driver for the TI INA4230, a 4-channel power monitor with I2C
interface.
The driver supports voltage, current, power and energy measurements, but
skips the alert functionality in this initial implementation.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Documentation/hwmon/ina4230.rst | 82 +++
Documentation/hwmon/index.rst | 1 +
MAINTAINERS | 2 +
drivers/hwmon/Kconfig | 11 +
drivers/hwmon/Makefile | 1 +
drivers/hwmon/ina4230.c | 1096 +++++++++++++++++++++++++++++++++++++++
6 files changed, 1193 insertions(+)
diff --git a/Documentation/hwmon/ina4230.rst b/Documentation/hwmon/ina4230.rst
new file mode 100644
index 000000000000..0cc286c78d0b
--- /dev/null
+++ b/Documentation/hwmon/ina4230.rst
@@ -0,0 +1,82 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver ina4230
+=====================
+
+Supported chips:
+
+ * Texas Instruments INA4230
+
+ Prefix: 'ina4230'
+
+ Addresses: I2C 0x40 - 0x4f
+
+ Datasheet: Publicly available at the Texas Instruments website
+
+ https://www.ti.com/product/INA4230
+
+Author: Alexey Charkov <alchark@flipper.net>
+
+Description
+-----------
+
+The Texas Instruments INA4230 monitors shunt voltage and bus voltage on up to
+four channels. It calculates current, power and accumulated energy for each
+channel in hardware, scaled by a per-channel calibration value.
+
+The driver derives the calibration from the shunt resistance and the maximum
+expected current of each channel, as described in
+Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml. The current LSB is
+the maximum expected current divided by 2^15, and the 4x shunt gain is enabled
+on channels where that current drops no more than 20.48 mV across the shunt.
+
+By default, all connected channels are enabled, with 4 samples averaged and a
+1.1 ms conversion time for both bus and shunt voltage.
+
+Alert functions are not supported.
+
+Sysfs entries
+-------------
+
+======================= =======================================================
+in[0-3]_label Voltage channel labels
+in[0-3]_enable Voltage channel enable controls
+in[0-3]_input Bus voltage (mV) channels
+in[4-7]_input Shunt voltage (mV) for channels 1 to 4 respectively
+curr[1-4]_input Current (mA) measurement channels
+power[1-4]_input Power (uW) measurement channels
+energy[1-4]_input Energy (uJ) accumulated by the chip since its last
+ reset. Overflow of the 32-bit hardware accumulator is
+ not handled.
+samples Number of samples used in the averaging mode.
+
+ Supports the list of number of samples:
+
+ 1, 4, 16, 64, 128, 256, 512, 1024
+
+update_interval Data conversion time in millisecond, following:
+
+ update_interval = C x S x (BC + SC)
+
+ * C: number of enabled channels
+ * S: number of samples
+ * BC: bus-voltage conversion time in millisecond
+ * SC: shunt-voltage conversion time in millisecond
+
+ Affects both Bus- and Shunt-voltage conversion time.
+ Note that setting update_interval to 0ms sets both BC
+ and SC to 140 us (minimum conversion time).
+======================= =======================================================
+
+Chip reset
+----------
+
+The chip returns to its power-up defaults after an externally triggered reset,
+such as an I2C general call, or a voltage glitch. When a current, power or
+energy reading comes back zero, the driver checks whether the chip has lost its
+calibration and, if so, restores its configuration. The read then waits up to
+100 ms for a fresh conversion and fails with -EAGAIN if none arrives, as with
+long averaging settings. The energy accumulators restart from zero after such
+a reset. A reset goes unnoticed while only voltages are being read, and until
+it is noticed, shunt voltages on channels using the 4x gain read four times
+too low.
diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
index ddfcd077d96e..f113760f6efc 100644
--- a/Documentation/hwmon/index.rst
+++ b/Documentation/hwmon/index.rst
@@ -106,6 +106,7 @@ Hardware Monitoring Kernel Drivers
ina233
ina238
ina3221
+ ina4230
inspur-ipsps1
intel-m10-bmc-hwmon
ionic-hwmon
diff --git a/MAINTAINERS b/MAINTAINERS
index 5aed784b245d..06ac8a4ac374 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13171,6 +13171,8 @@ M: Alexey Charkov <alchark@flipper.net>
L: linux-hwmon@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/hwmon/ti,ina4230.yaml
+F: Documentation/hwmon/ina4230.rst
+F: drivers/hwmon/ina4230.c
INDEX OF FURTHER KERNEL DOCUMENTATION
M: Carlos Bilbao <carlos.bilbao@kernel.org>
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 1035b8ac6e57..82de2a03833b 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -2501,6 +2501,17 @@ config SENSORS_INA3221
This driver can also be built as a module. If so, the module
will be called ina3221.
+config SENSORS_INA4230
+ tristate "Texas Instruments INA4230 Quad Current/Voltage Monitor"
+ depends on I2C
+ select REGMAP_I2C
+ help
+ If you say yes here you get support for the TI INA4230 Quad
+ Current/Voltage Monitor.
+
+ This driver can also be built as a module. If so, the module
+ will be called ina4230.
+
config SENSORS_SPD5118
tristate "SPD5118 Compliant Temperature Sensors"
depends on I3C_OR_I2C
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index d1f1d5d24227..03d5fa4b02ac 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -111,6 +111,7 @@ obj-$(CONFIG_SENSORS_INA209) += ina209.o
obj-$(CONFIG_SENSORS_INA2XX) += ina2xx.o
obj-$(CONFIG_SENSORS_INA238) += ina238.o
obj-$(CONFIG_SENSORS_INA3221) += ina3221.o
+obj-$(CONFIG_SENSORS_INA4230) += ina4230.o
obj-$(CONFIG_SENSORS_INTEL_M10_BMC_HWMON) += intel-m10-bmc-hwmon.o
obj-$(CONFIG_SENSORS_IONIC) += ionic-hwmon.o
obj-$(CONFIG_SENSORS_ISL28022) += isl28022.o
diff --git a/drivers/hwmon/ina4230.c b/drivers/hwmon/ina4230.c
new file mode 100644
index 000000000000..cb7750abdbf5
--- /dev/null
+++ b/drivers/hwmon/ina4230.c
@@ -0,0 +1,1096 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * INA4230 Quad Current/Voltage Monitor
+ *
+ * Based on INA3221 driver by Texas Instruments Incorporated - https://www.ti.com/
+ * Adapted for INA4230 by Alexey Charkov <alchark@flipper.net>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/byteorder/generic.h>
+#include <linux/hwmon.h>
+#include <linux/i2c.h>
+#include <linux/math.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/util_macros.h>
+
+#define INA4230_DRIVER_NAME "ina4230"
+
+#define INA4230_SHUNT_VOLTAGE_CH1 0x00
+#define INA4230_BUS_VOLTAGE_CH1 0x01
+#define INA4230_CURRENT_CH1 0x02
+#define INA4230_POWER_CH1 0x03
+#define INA4230_ENERGY_CH1 0x04
+#define INA4230_CALIBRATION_CH1 0x05
+#define INA4230_ALERT_LIMIT1 0x06
+#define INA4230_ALERT_CONFIG1 0x07
+#define INA4230_SHUNT_VOLTAGE_CH2 0x08
+#define INA4230_BUS_VOLTAGE_CH2 0x09
+#define INA4230_CURRENT_CH2 0x0A
+#define INA4230_POWER_CH2 0x0B
+#define INA4230_ENERGY_CH2 0x0C
+#define INA4230_CALIBRATION_CH2 0x0D
+#define INA4230_ALERT_LIMIT2 0x0E
+#define INA4230_ALERT_CONFIG2 0x0F
+#define INA4230_SHUNT_VOLTAGE_CH3 0x10
+#define INA4230_BUS_VOLTAGE_CH3 0x11
+#define INA4230_CURRENT_CH3 0x12
+#define INA4230_POWER_CH3 0x13
+#define INA4230_ENERGY_CH3 0x14
+#define INA4230_CALIBRATION_CH3 0x15
+#define INA4230_ALERT_LIMIT3 0x16
+#define INA4230_ALERT_CONFIG3 0x17
+#define INA4230_SHUNT_VOLTAGE_CH4 0x18
+#define INA4230_BUS_VOLTAGE_CH4 0x19
+#define INA4230_CURRENT_CH4 0x1A
+#define INA4230_POWER_CH4 0x1B
+#define INA4230_ENERGY_CH4 0x1C
+#define INA4230_CALIBRATION_CH4 0x1D
+#define INA4230_ALERT_LIMIT4 0x1E
+#define INA4230_ALERT_CONFIG4 0x1F
+#define INA4230_CONFIG1 0x20
+#define INA4230_CONFIG2 0x21
+#define INA4230_FLAGS 0x22
+#define INA4230_MANUFACTURER_ID 0x7E
+#define INA4230_DEVICE_ID 0x7F
+
+#define INA4230_CALIBRATION_MASK GENMASK(14, 0)
+
+#define INA4230_ALERT_CHANNEL_MASK GENMASK(4, 3)
+#define INA4230_ALERT_MASK GENMASK(2, 0)
+/* Shunt voltage over limit */
+#define INA4230_ALERT_MASK_SOL 0x1
+/* Shunt voltage under limit */
+#define INA4230_ALERT_MASK_SUL 0x2
+/* Bus voltage over limit */
+#define INA4230_ALERT_MASK_BOL 0x3
+/* Bus voltage under limit */
+#define INA4230_ALERT_MASK_BUL 0x4
+/* Power over limit */
+#define INA4230_ALERT_MASK_POL 0x5
+
+#define INA4230_CONFIG1_ACTIVE_CHANNEL_MASK GENMASK(15, 12)
+#define INA4230_CONFIG1_AVG_MASK GENMASK(11, 9)
+#define INA4230_CONFIG1_VBUSCT_MASK GENMASK(8, 6)
+#define INA4230_CONFIG1_VSHCT_MASK GENMASK(5, 3)
+#define INA4230_CONFIG1_MODE_MASK GENMASK(2, 0)
+#define INA4230_MODE_POWERDOWN 0
+#define INA4230_MODE_SHUNT_SINGLE 1
+#define INA4230_MODE_BUS_SINGLE 2
+#define INA4230_MODE_BUS_SHUNT_SINGLE 3
+#define INA4230_MODE_POWERDOWN1 4
+#define INA4230_MODE_SHUNT_CONTINUOUS 5
+#define INA4230_MODE_BUS_CONTINUOUS 6
+#define INA4230_MODE_BUS_SHUNT_CONTINUOUS 7
+
+#define INA4230_CONFIG2_RST BIT(15)
+#define INA4230_CONFIG2_ACC_RST_MASK GENMASK(11, 8)
+#define INA4230_CONFIG2_CNVR_MASK BIT(7)
+#define INA4230_CONFIG2_ENOF_MASK BIT(6)
+#define INA4230_CONFIG2_ALERT_LATCH BIT(5)
+#define INA4230_CONFIG2_ALERT_POL BIT(4)
+#define INA4230_CONFIG2_RANGE_MASK GENMASK(3, 0)
+#define INA4230_CONFIG2_RANGE_CH(x) \
+ FIELD_PREP(INA4230_CONFIG2_RANGE_MASK, BIT((x)))
+
+#define INA4230_FLAGS_LIMIT4_ALERT BIT(15)
+#define INA4230_FLAGS_LIMIT3_ALERT BIT(14)
+#define INA4230_FLAGS_LIMIT2_ALERT BIT(13)
+#define INA4230_FLAGS_LIMIT1_ALERT BIT(12)
+#define INA4230_FLAGS_ENERGY_OVERFLOW_CH4 BIT(11)
+#define INA4230_FLAGS_ENERGY_OVERFLOW_CH3 BIT(10)
+#define INA4230_FLAGS_ENERGY_OVERFLOW_CH2 BIT(9)
+#define INA4230_FLAGS_ENERGY_OVERFLOW_CH1 BIT(8)
+#define INA4230_FLAGS_CVRF BIT(7)
+#define INA4230_FLAGS_MATH_OVERFLOW BIT(6)
+
+#define INA4230_RSHUNT_DEFAULT 10000
+/* Longest wait for fresh data after reset recovery, under the hwmon lock */
+#define INA4230_RECOVERY_TIMEOUT_US (100 * USEC_PER_MSEC)
+#define INA4230_CONFIG_DEFAULT \
+ (FIELD_PREP(INA4230_CONFIG1_ACTIVE_CHANNEL_MASK, 0xF) | \
+ FIELD_PREP(INA4230_CONFIG1_AVG_MASK, 0x1) | \
+ FIELD_PREP(INA4230_CONFIG1_VBUSCT_MASK, 0x4) | \
+ FIELD_PREP(INA4230_CONFIG1_VSHCT_MASK, 0x4) | \
+ FIELD_PREP(INA4230_CONFIG1_MODE_MASK, 0x7))
+#define INA4230_CONFIG_CHx_EN(x) \
+ FIELD_PREP(INA4230_CONFIG1_ACTIVE_CHANNEL_MASK, BIT((x)))
+
+enum ina4230_channels {
+ INA4230_CHANNEL1,
+ INA4230_CHANNEL2,
+ INA4230_CHANNEL3,
+ INA4230_CHANNEL4,
+ INA4230_NUM_CHANNELS
+};
+
+/**
+ * struct ina4230_input - channel input source specific information
+ * @label: label of channel input source
+ * @shunt_resistor: shunt resistor value of channel input source
+ * @shunt_gain: gain of shunt voltage for current calculation
+ * @max_expected_current: maximum expected current in micro-Ampere for ADC
+ * calibration
+ * @current_lsb_uA: current LSB in micro-Amperes
+ * @disconnected: connection status of channel input source
+ */
+struct ina4230_input {
+ const char *label;
+ u32 shunt_resistor;
+ int shunt_gain;
+ u32 max_expected_current;
+ u32 current_lsb_uA;
+ bool disconnected;
+};
+
+/**
+ * struct ina4230_data - device specific information
+ * @pm_dev: Device pointer for pm runtime
+ * @regmap: Register map of the device
+ * @inputs: Array of channel input source specific structures
+ * @alert_active_high: flag indicating alert polarity is active high
+ */
+struct ina4230_data {
+ struct device *pm_dev;
+ struct regmap *regmap;
+ struct ina4230_input inputs[INA4230_NUM_CHANNELS];
+ bool alert_active_high;
+};
+
+/* Returns 1 if the channel is enabled, 0 if not, or a negative error code */
+static int ina4230_is_enabled(struct ina4230_data *ina, int channel)
+{
+ unsigned int config;
+ int ret;
+
+ if (!pm_runtime_active(ina->pm_dev) || ina->inputs[channel].disconnected)
+ return 0;
+
+ ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+ if (ret)
+ return ret;
+
+ return !!(config & INA4230_CONFIG_CHx_EN(channel));
+}
+
+/* Lookup table for Bus and Shunt conversion times in usec */
+static const u16 ina4230_conv_time[] = {
+ 140, 204, 332, 588, 1100, 2116, 4156, 8244,
+};
+
+/* Lookup table for number of samples used in averaging mode */
+static const int ina4230_avg_samples[] = {
+ 1, 4, 16, 64, 128, 256, 512, 1024,
+};
+
+/* Converting update_interval in msec to conversion time in usec */
+static inline u32 ina4230_interval_ms_to_conv_time(u16 config, long interval)
+{
+ /* With no channel enabled, size the conversion time for one */
+ u32 channels = hweight16(config & INA4230_CONFIG1_ACTIVE_CHANNEL_MASK) ?: 1;
+ u32 samples_idx = FIELD_GET(INA4230_CONFIG1_AVG_MASK, config);
+ u32 samples = ina4230_avg_samples[samples_idx];
+
+ /* Maximum supported interval is 8244 * 4 * 2 * 1024 = 67534848 usec */
+ interval = clamp_val(interval, 0, 67535);
+
+ /* Bisect the result to Bus and Shunt conversion times */
+ return DIV_ROUND_CLOSEST(interval * 1000 / 2, channels * samples);
+}
+
+/* Converting CONFIG register value to update_interval in usec */
+static inline u32 ina4230_reg_to_interval_us(u16 config)
+{
+ u32 channels = hweight16(config & INA4230_CONFIG1_ACTIVE_CHANNEL_MASK);
+ u32 vbus_ct_idx = FIELD_GET(INA4230_CONFIG1_VBUSCT_MASK, config);
+ u32 vsh_ct_idx = FIELD_GET(INA4230_CONFIG1_VSHCT_MASK, config);
+ u32 samples_idx = FIELD_GET(INA4230_CONFIG1_AVG_MASK, config);
+ u32 samples = ina4230_avg_samples[samples_idx];
+ u32 vbus_ct = ina4230_conv_time[vbus_ct_idx];
+ u32 vsh_ct = ina4230_conv_time[vsh_ct_idx];
+
+ /*
+ * Calculate total conversion time including the time to obtain
+ * the first averaged sample after starting conversion.
+ * Subsequent conversions will actually be available after
+ * channels * (vbus_ct + vsh_ct) usec
+ */
+ return channels * (vbus_ct + vsh_ct) * samples;
+}
+
+static const u8 ina4230_calibration_reg[] = {
+ INA4230_CALIBRATION_CH1,
+ INA4230_CALIBRATION_CH2,
+ INA4230_CALIBRATION_CH3,
+ INA4230_CALIBRATION_CH4,
+};
+
+static int ina4230_set_calibration(struct ina4230_data *ina, int channel)
+{
+ struct ina4230_input *input = &ina->inputs[channel];
+ u8 reg = ina4230_calibration_reg[channel];
+ u64 n, d;
+ u32 calibration;
+ int ret;
+
+ /* Use the 4x gain only if the full-scale drop is within 20.48 mV (in pV) */
+ input->shunt_gain = (u64)input->max_expected_current * input->shunt_resistor >
+ 20480ULL * 1000000 ? 1 : 4;
+
+ ret = regmap_update_bits(ina->regmap, INA4230_CONFIG2,
+ INA4230_CONFIG2_RANGE_CH(channel),
+ input->shunt_gain == 4 ?
+ INA4230_CONFIG2_RANGE_CH(channel) : 0);
+ if (ret)
+ return ret;
+
+ /* Spread a u32 current over the s16 CURRENT register: at most 2^17 */
+ input->current_lsb_uA = DIV_ROUND_UP_ULL(input->max_expected_current, 32768);
+ n = 5120000000ULL;
+ d = (u64)input->current_lsb_uA * input->shunt_resistor * input->shunt_gain;
+ /* Ensure rounding to the closest integer */
+ n += d / 2;
+ n = div64_u64(n, d);
+ if (n > INA4230_CALIBRATION_MASK) {
+ dev_err(ina->pm_dev,
+ "Shunt %uuOhm too low for expected current %uuA, cannot calibrate channel %d\n",
+ input->shunt_resistor, input->max_expected_current, channel + 1);
+ return -ERANGE;
+ }
+ if (!n) {
+ dev_err(ina->pm_dev,
+ "Shunt %uuOhm too high for expected current %uuA, cannot calibrate channel %d\n",
+ input->shunt_resistor, input->max_expected_current, channel + 1);
+ return -ERANGE;
+ }
+
+ calibration = n & INA4230_CALIBRATION_MASK;
+
+ return regmap_write(ina->regmap, reg, calibration);
+}
+
+static const u8 ina4230_in_reg[] = {
+ INA4230_BUS_VOLTAGE_CH1,
+ INA4230_BUS_VOLTAGE_CH2,
+ INA4230_BUS_VOLTAGE_CH3,
+ INA4230_BUS_VOLTAGE_CH4,
+ INA4230_SHUNT_VOLTAGE_CH1,
+ INA4230_SHUNT_VOLTAGE_CH2,
+ INA4230_SHUNT_VOLTAGE_CH3,
+ INA4230_SHUNT_VOLTAGE_CH4,
+};
+
+static const u8 ina4230_curr_reg[] = {
+ INA4230_CURRENT_CH1, INA4230_CURRENT_CH2,
+ INA4230_CURRENT_CH3, INA4230_CURRENT_CH4
+};
+
+static const u8 ina4230_power_reg[] = {
+ INA4230_POWER_CH1, INA4230_POWER_CH2, INA4230_POWER_CH3, INA4230_POWER_CH4
+};
+
+static const u8 ina4230_energy_reg[] = {
+ INA4230_ENERGY_CH1, INA4230_ENERGY_CH2,
+ INA4230_ENERGY_CH3, INA4230_ENERGY_CH4
+};
+
+static int ina4230_read_chip(struct device *dev, u32 attr, long *val)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int config;
+ int ret;
+
+ ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+ if (ret)
+ return ret;
+
+ switch (attr) {
+ case hwmon_chip_samples:
+ *val = ina4230_avg_samples[FIELD_GET(INA4230_CONFIG1_AVG_MASK, config)];
+ return 0;
+ case hwmon_chip_update_interval:
+ /* Return in msec */
+ *val = ina4230_reg_to_interval_us(config);
+ *val = DIV_ROUND_CLOSEST(*val, 1000);
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int ina4230_read_in(struct device *dev, u32 attr, int channel, long *val)
+{
+ const bool is_shunt = channel > INA4230_CHANNEL4;
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int regval;
+ u8 reg;
+ int ret;
+
+ switch (attr) {
+ case hwmon_in_input:
+ reg = ina4230_in_reg[channel];
+
+ /*
+ * Translate shunt channel index to sensor channel index
+ */
+ channel %= INA4230_NUM_CHANNELS;
+
+ ret = ina4230_is_enabled(ina, channel);
+ if (ret <= 0)
+ return ret ?: -ENODATA;
+
+ ret = regmap_read(ina->regmap, reg, ®val);
+ if (ret)
+ return ret;
+
+ /*
+ * Scale of shunt voltage (uV): LSB is 2.5uV or 625nV
+ * depending on gain setting
+ * Scale of bus voltage (mV): LSB is 1.6mV
+ */
+ if (is_shunt)
+ *val = mult_frac((long)(s16)regval,
+ 2500 / ina->inputs[channel].shunt_gain,
+ 1000000);
+ else
+ *val = mult_frac((long)(s16)regval,
+ 1600,
+ 1000);
+ return 0;
+ case hwmon_in_enable:
+ ret = ina4230_is_enabled(ina, channel);
+ if (ret < 0)
+ return ret;
+
+ *val = ret;
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+/*
+ * Restore the cached configuration to the chip. Writing CONFIG1 restarts the
+ * conversions, so write it last, once the ranges and calibration are in place.
+ */
+static int ina4230_restore(struct ina4230_data *ina)
+{
+ int ret;
+
+ ret = regcache_sync_region(ina->regmap, 0, INA4230_CONFIG1 - 1);
+ if (ret)
+ return ret;
+
+ ret = regcache_sync_region(ina->regmap, INA4230_CONFIG2,
+ INA4230_DEVICE_ID);
+ if (ret)
+ return ret;
+
+ return regcache_sync_region(ina->regmap, INA4230_CONFIG1, INA4230_CONFIG1);
+}
+
+/*
+ * The chip returns to its power-up defaults after an externally triggered
+ * reset, such as an I2C general call, or a voltage glitch. Its calibration is
+ * then zero and so are all current, power and energy readings. When a reading
+ * comes back zero, check the calibration and restore the configuration if the
+ * chip has lost it.
+ *
+ * Returns 1 if the chip was reinitialized, 0 if it was not reset, or a
+ * negative error code.
+ */
+static int ina4230_recover_reset(struct ina4230_data *ina, int channel)
+{
+ unsigned int cal, config, flags;
+ u32 timeout;
+ int ret;
+
+ ret = regmap_read_bypassed(ina->regmap,
+ ina4230_calibration_reg[channel], &cal);
+ if (ret || cal)
+ return ret;
+
+ dev_warn_ratelimited(ina->pm_dev, "chip was reset, reinitializing\n");
+
+ ret = ina4230_restore(ina);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+ if (ret)
+ return ret;
+
+ /*
+ * Restoring CONFIG1 restarts the conversions. Wait for the first cycle,
+ * but within a bound: with long averaging a cycle takes over a minute.
+ */
+ timeout = min(2 * ina4230_reg_to_interval_us(config),
+ INA4230_RECOVERY_TIMEOUT_US);
+ ret = regmap_read_poll_timeout(ina->regmap, INA4230_FLAGS, flags,
+ flags & INA4230_FLAGS_CVRF,
+ USEC_PER_MSEC, timeout);
+ if (ret == -ETIMEDOUT)
+ return -EAGAIN;
+ if (ret)
+ return ret;
+
+ return 1;
+}
+
+static int ina4230_read_checked(struct ina4230_data *ina, int channel,
+ unsigned int reg, unsigned int *regval)
+{
+ int ret;
+
+ ret = regmap_read(ina->regmap, reg, regval);
+ if (ret || *regval)
+ return ret;
+
+ ret = ina4230_recover_reset(ina, channel);
+ if (ret <= 0)
+ return ret;
+
+ return regmap_read(ina->regmap, reg, regval);
+}
+
+static int ina4230_read_power(struct device *dev, u32 attr, int channel, long *val)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int regval;
+ int ret;
+
+ switch (attr) {
+ case hwmon_power_input:
+ ret = ina4230_is_enabled(ina, channel);
+ if (ret <= 0)
+ return ret ?: -ENODATA;
+
+ ret = ina4230_read_checked(ina, channel,
+ ina4230_power_reg[channel], ®val);
+ if (ret)
+ return ret;
+
+ /*
+ * Power = 32 x CURRENT_LSB x POWER (datasheet equation 4).
+ * u16 * 2^17 * 32 needs 38 bits, more than a 32-bit long holds.
+ */
+ *val = min_t(u64, (u64)regval *
+ ina->inputs[channel].current_lsb_uA * 32, LONG_MAX);
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int ina4230_read_energy(struct device *dev, u32 attr, int channel, s64 *val)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ u8 reg = ina4230_energy_reg[channel];
+ int ret;
+ __be32 regval;
+
+ switch (attr) {
+ case hwmon_energy_input:
+ ret = ina4230_is_enabled(ina, channel);
+ if (ret <= 0)
+ return ret ?: -ENODATA;
+
+ /*
+ * Read four bytes without incrementing the register address,
+ * which the device returns in its endianness format (BE32).
+ * Note that this internally produces a single 4-byte I2C read
+ * transaction and not two consecutive 16-bit register reads,
+ * so the result is in fact a four-byte big-endian number
+ */
+ ret = regmap_noinc_read(ina->regmap, reg, ®val, sizeof(regval));
+ if (ret)
+ return ret;
+
+ /* A reset restarts the accumulation from zero */
+ if (!regval) {
+ ret = ina4230_recover_reset(ina, channel);
+ if (ret < 0)
+ return ret;
+ }
+
+ /*
+ * Energy = 32 x CURRENT_LSB x ENERGY (datasheet equation 5).
+ * u32 * 2^17 * 32 needs 54 bits, hence the 64-bit energy attribute.
+ */
+ *val = (u64)be32_to_cpu(regval) *
+ ina->inputs[channel].current_lsb_uA * 32;
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int ina4230_read_curr(struct device *dev, u32 attr,
+ int channel, long *val)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int regval;
+ int ret;
+
+ switch (attr) {
+ case hwmon_curr_input:
+ ret = ina4230_is_enabled(ina, channel);
+ if (ret <= 0)
+ return ret ?: -ENODATA;
+
+ ret = ina4230_read_checked(ina, channel,
+ ina4230_curr_reg[channel], ®val);
+ if (ret)
+ return ret;
+
+ /* s16 * 2^17 needs 33 bits; divided by 1000 it fits a long */
+ *val = div_s64((s64)(s16)regval *
+ ina->inputs[channel].current_lsb_uA, 1000);
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int ina4230_write_chip(struct device *dev, u32 attr, long val)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int config;
+ int idx, ret;
+ u32 tmp;
+
+ switch (attr) {
+ case hwmon_chip_samples:
+ idx = find_closest(val, ina4230_avg_samples,
+ ARRAY_SIZE(ina4230_avg_samples));
+
+ return regmap_update_bits(ina->regmap, INA4230_CONFIG1,
+ INA4230_CONFIG1_AVG_MASK,
+ FIELD_PREP(INA4230_CONFIG1_AVG_MASK, idx));
+ case hwmon_chip_update_interval:
+ ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+ if (ret)
+ return ret;
+
+ tmp = ina4230_interval_ms_to_conv_time(config, val);
+ idx = find_closest(tmp, ina4230_conv_time,
+ ARRAY_SIZE(ina4230_conv_time));
+
+ return regmap_update_bits(ina->regmap, INA4230_CONFIG1,
+ INA4230_CONFIG1_VBUSCT_MASK |
+ INA4230_CONFIG1_VSHCT_MASK,
+ FIELD_PREP(INA4230_CONFIG1_VBUSCT_MASK, idx) |
+ FIELD_PREP(INA4230_CONFIG1_VSHCT_MASK, idx));
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int ina4230_write_enable(struct device *dev, int channel, bool enable)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+ unsigned int config, mask = INA4230_CONFIG_CHx_EN(channel);
+ int ret;
+
+ ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+ if (ret)
+ return ret;
+
+ /* Bypass if enable status is not being changed */
+ if (!!(config & mask) == enable)
+ return 0;
+
+ if (enable && ina->inputs[channel].disconnected)
+ return -ENODEV;
+
+ /* For enabling routine, increase refcount and resume() at first */
+ if (enable) {
+ ret = pm_runtime_resume_and_get(ina->pm_dev);
+ if (ret < 0) {
+ dev_err(dev, "Failed to get PM runtime\n");
+ return ret;
+ }
+ }
+
+ /*
+ * Even if the bus write fails, the cache takes the new value for the next
+ * sync to apply, so the reference follows the cached enable bit.
+ */
+ ret = regmap_update_bits(ina->regmap, INA4230_CONFIG1, mask,
+ enable ? mask : 0);
+
+ /* For disabling routine, decrease refcount or suspend() at last */
+ if (!enable)
+ pm_runtime_put_sync(ina->pm_dev);
+
+ return ret;
+}
+
+static int ina4230_read(struct device *dev, enum hwmon_sensor_types type,
+ u32 attr, int channel, long *val)
+{
+ int ret;
+
+ switch (type) {
+ case hwmon_chip:
+ ret = ina4230_read_chip(dev, attr, val);
+ break;
+ case hwmon_in:
+ ret = ina4230_read_in(dev, attr, channel, val);
+ break;
+ case hwmon_curr:
+ ret = ina4230_read_curr(dev, attr, channel, val);
+ break;
+ case hwmon_power:
+ ret = ina4230_read_power(dev, attr, channel, val);
+ break;
+ case hwmon_energy64:
+ ret = ina4230_read_energy(dev, attr, channel, (s64 *)val);
+ break;
+ default:
+ ret = -EOPNOTSUPP;
+ break;
+ }
+ return ret;
+}
+
+static int ina4230_write(struct device *dev, enum hwmon_sensor_types type,
+ u32 attr, int channel, long val)
+{
+ int ret;
+
+ switch (type) {
+ case hwmon_chip:
+ ret = ina4230_write_chip(dev, attr, val);
+ break;
+ case hwmon_in:
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ ret = ina4230_write_enable(dev, channel, val);
+ break;
+ default:
+ ret = -EOPNOTSUPP;
+ break;
+ }
+ return ret;
+}
+
+static int ina4230_read_string(struct device *dev, enum hwmon_sensor_types type,
+ u32 attr, int channel, const char **str)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+
+ *str = ina->inputs[channel].label;
+
+ return 0;
+}
+
+static umode_t ina4230_is_visible(const void *drvdata,
+ enum hwmon_sensor_types type,
+ u32 attr, int channel)
+{
+ const struct ina4230_data *ina = drvdata;
+ const struct ina4230_input *input = NULL;
+
+ switch (type) {
+ case hwmon_chip:
+ switch (attr) {
+ case hwmon_chip_samples:
+ case hwmon_chip_update_interval:
+ return 0644;
+ default:
+ return 0;
+ }
+ case hwmon_in:
+ switch (attr) {
+ case hwmon_in_label:
+ if (channel <= INA4230_CHANNEL4)
+ input = &ina->inputs[channel];
+ /* Hide label node if label is not provided */
+ return (input && input->label) ? 0444 : 0;
+ case hwmon_in_input:
+ return 0444;
+ case hwmon_in_enable:
+ return 0644;
+ default:
+ return 0;
+ }
+ case hwmon_curr:
+ switch (attr) {
+ case hwmon_curr_input:
+ return 0444;
+ default:
+ return 0;
+ }
+ case hwmon_power:
+ switch (attr) {
+ case hwmon_power_input:
+ return 0444;
+ default:
+ return 0;
+ }
+ case hwmon_energy64:
+ switch (attr) {
+ case hwmon_energy_input:
+ return 0444;
+ default:
+ return 0;
+ }
+ default:
+ return 0;
+ }
+}
+
+static const struct hwmon_channel_info * const ina4230_info[] = {
+ HWMON_CHANNEL_INFO(chip,
+ HWMON_C_SAMPLES,
+ HWMON_C_UPDATE_INTERVAL),
+ HWMON_CHANNEL_INFO(in,
+ /* 0-3: input voltage Channels */
+ HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
+ HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
+ HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
+ HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
+ /* 4-7: shunt voltage Channels */
+ HWMON_I_INPUT,
+ HWMON_I_INPUT,
+ HWMON_I_INPUT,
+ HWMON_I_INPUT),
+ HWMON_CHANNEL_INFO(curr,
+ /* 1-4: current channels*/
+ HWMON_C_INPUT,
+ HWMON_C_INPUT,
+ HWMON_C_INPUT,
+ HWMON_C_INPUT),
+ HWMON_CHANNEL_INFO(power,
+ /* 1-4: power channels*/
+ HWMON_P_INPUT,
+ HWMON_P_INPUT,
+ HWMON_P_INPUT,
+ HWMON_P_INPUT),
+ HWMON_CHANNEL_INFO(energy64,
+ /* 1-4: energy channels*/
+ HWMON_E_INPUT,
+ HWMON_E_INPUT,
+ HWMON_E_INPUT,
+ HWMON_E_INPUT),
+ NULL
+};
+
+static const struct hwmon_ops ina4230_hwmon_ops = {
+ .is_visible = ina4230_is_visible,
+ .read_string = ina4230_read_string,
+ .read = ina4230_read,
+ .write = ina4230_write,
+};
+
+static const struct hwmon_chip_info ina4230_chip_info = {
+ .ops = &ina4230_hwmon_ops,
+ .info = ina4230_info,
+};
+
+static const struct regmap_range ina4230_vol_ranges[] = {
+ regmap_reg_range(INA4230_SHUNT_VOLTAGE_CH1, INA4230_ENERGY_CH1),
+ regmap_reg_range(INA4230_SHUNT_VOLTAGE_CH2, INA4230_ENERGY_CH2),
+ regmap_reg_range(INA4230_SHUNT_VOLTAGE_CH3, INA4230_ENERGY_CH3),
+ regmap_reg_range(INA4230_SHUNT_VOLTAGE_CH4, INA4230_ENERGY_CH4),
+ /*
+ * CONFIG2 stays cached despite its self-clearing RST and ACC_RST bits:
+ * the driver sets RST only below regmap and never sets ACC_RST, so the
+ * cache holds the configuration to restore after a reset.
+ */
+ regmap_reg_range(INA4230_FLAGS, INA4230_FLAGS),
+};
+
+static const struct regmap_access_table ina4230_volatile_table = {
+ .yes_ranges = ina4230_vol_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ina4230_vol_ranges),
+};
+
+static const struct regmap_range ina4230_rd_ranges[] = {
+ regmap_reg_range(INA4230_SHUNT_VOLTAGE_CH1, INA4230_FLAGS),
+ regmap_reg_range(INA4230_MANUFACTURER_ID, INA4230_DEVICE_ID),
+};
+
+static const struct regmap_access_table ina4230_rd_table = {
+ .yes_ranges = ina4230_rd_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ina4230_rd_ranges),
+};
+
+static const struct regmap_range ina4230_wr_ranges[] = {
+ regmap_reg_range(INA4230_CALIBRATION_CH1, INA4230_ALERT_CONFIG1),
+ regmap_reg_range(INA4230_CALIBRATION_CH2, INA4230_ALERT_CONFIG2),
+ regmap_reg_range(INA4230_CALIBRATION_CH3, INA4230_ALERT_CONFIG3),
+ regmap_reg_range(INA4230_CALIBRATION_CH4, INA4230_ALERT_CONFIG4),
+ regmap_reg_range(INA4230_CONFIG1, INA4230_CONFIG2),
+};
+
+static const struct regmap_access_table ina4230_wr_table = {
+ .yes_ranges = ina4230_wr_ranges,
+ .n_yes_ranges = ARRAY_SIZE(ina4230_wr_ranges),
+};
+
+static const struct regmap_config ina4230_regmap_config = {
+ .reg_bits = 8,
+ .val_bits = 16,
+ .max_register = INA4230_DEVICE_ID,
+ .use_single_write = true,
+
+ .cache_type = REGCACHE_MAPLE,
+ .rd_table = &ina4230_rd_table,
+ .wr_table = &ina4230_wr_table,
+ .volatile_table = &ina4230_volatile_table,
+};
+
+static int ina4230_probe_child_from_dt(struct device *dev,
+ struct device_node *child,
+ struct ina4230_data *ina)
+{
+ struct ina4230_input *input;
+ u32 val;
+ int ret;
+
+ ret = of_property_read_u32(child, "reg", &val);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "missing reg property of %pOFn\n", child);
+ else if (val > INA4230_CHANNEL4)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid reg %d of %pOFn\n", val, child);
+
+ input = &ina->inputs[val];
+
+ /* Log the disconnected channel input */
+ if (!of_device_is_available(child)) {
+ input->disconnected = true;
+ return 0;
+ }
+
+ /* Save the connected input label if available */
+ of_property_read_string(child, "label", &input->label);
+
+ /* Overwrite default shunt resistor value optionally */
+ if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
+ if (!val)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid shunt resistor value %u of %pOFn\n",
+ val, child);
+
+ input->shunt_resistor = val;
+ }
+
+ /* Save the expected maxcurrent */
+ if (!of_property_read_u32(child, "ti,maximum-expected-current-microamp", &val)) {
+ if (val < 32768)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid max current value %u of %pOFn\n",
+ val, child);
+
+ input->max_expected_current = val;
+ }
+
+ return 0;
+}
+
+static int ina4230_probe_from_dt(struct device *dev, struct ina4230_data *ina)
+{
+ const struct device_node *np = dev->of_node;
+ int ret;
+
+ /* Compatible with non-DT platforms */
+ if (!np)
+ return 0;
+
+ ina->alert_active_high = of_property_read_bool(np, "ti,alert-polarity-active-high");
+
+ for_each_child_of_node_scoped(np, child) {
+ ret = ina4230_probe_child_from_dt(dev, child, ina);
+ if (ret)
+ return ret;
+ }
+
+ ret = devm_regulator_get_enable_optional(dev, "vs");
+ if (ret && ret != -ENODEV)
+ return dev_err_probe(dev, ret, "Failed to get regulator\n");
+
+ return 0;
+}
+
+/* Reset the chip and restore whatever configuration the cache holds */
+static int ina4230_init_chip(struct ina4230_data *ina)
+{
+ int ret;
+
+ /* Reset below regmap, keeping the self-clearing RST bit out of the cache */
+ ret = i2c_smbus_write_word_swapped(to_i2c_client(ina->pm_dev),
+ INA4230_CONFIG2, INA4230_CONFIG2_RST);
+ if (ret) {
+ dev_err(ina->pm_dev, "Unable to reset device\n");
+ return ret;
+ }
+
+ return ina4230_restore(ina);
+}
+
+static void ina4230_pm_disable(void *data)
+{
+ struct ina4230_data *ina = data;
+ unsigned int config;
+ int i;
+
+ pm_runtime_disable(ina->pm_dev);
+ pm_runtime_set_suspended(ina->pm_dev);
+
+ /* A cached register, so this cannot reach the bus and fail */
+ regmap_read(ina->regmap, INA4230_CONFIG1, &config);
+
+ /* Drop the references held for the enabled channels */
+ for (i = 0; i < INA4230_NUM_CHANNELS; i++) {
+ if (config & INA4230_CONFIG_CHx_EN(i))
+ pm_runtime_put_noidle(ina->pm_dev);
+ }
+}
+
+static int ina4230_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ unsigned int config1, config2 = 0;
+ struct ina4230_data *ina;
+ struct device *hwmon_dev;
+ int i, ret;
+
+ ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
+ if (!ina)
+ return -ENOMEM;
+
+ ina->regmap = devm_regmap_init_i2c(client, &ina4230_regmap_config);
+ if (IS_ERR(ina->regmap))
+ return PTR_ERR(ina->regmap);
+
+ for (i = 0; i < INA4230_NUM_CHANNELS; i++) {
+ ina->inputs[i].shunt_resistor = INA4230_RSHUNT_DEFAULT;
+ /* Default for 1mA LSB current measurements */
+ ina->inputs[i].max_expected_current = 32768000;
+ }
+
+ ret = ina4230_probe_from_dt(dev, ina);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Unable to probe from device tree\n");
+
+ /* Start from the driver default configuration */
+ config1 = INA4230_CONFIG_DEFAULT;
+
+ if (ina->alert_active_high)
+ config2 |= INA4230_CONFIG2_ALERT_POL;
+
+ /* Disable channels if their inputs are disconnected */
+ for (i = 0; i < INA4230_NUM_CHANNELS; i++) {
+ if (ina->inputs[i].disconnected)
+ config1 &= ~INA4230_CONFIG_CHx_EN(i);
+ }
+
+ ina->pm_dev = dev;
+ dev_set_drvdata(dev, ina);
+
+ ret = ina4230_init_chip(ina);
+ if (ret)
+ return ret;
+
+ /*
+ * Program the configuration, which also puts it in the cache that later
+ * reads, updates and restores rely on. CONFIG1 goes last, as writing it
+ * restarts the conversions.
+ */
+ ret = regmap_write(ina->regmap, INA4230_CONFIG2, config2);
+ if (ret)
+ return ret;
+
+ for (i = 0; i < INA4230_NUM_CHANNELS; i++) {
+ if (!ina->inputs[i].disconnected) {
+ ret = ina4230_set_calibration(ina, i);
+ if (ret)
+ return ret;
+ }
+ }
+
+ ret = regmap_write(ina->regmap, INA4230_CONFIG1, config1);
+ if (ret)
+ return ret;
+
+ /* Hold one runtime PM reference per enabled channel */
+ pm_runtime_set_active(dev);
+ for (i = 0; i < INA4230_NUM_CHANNELS; i++) {
+ if (config1 & INA4230_CONFIG_CHx_EN(i))
+ pm_runtime_get_noresume(dev);
+ }
+ pm_runtime_enable(dev);
+
+ /* Registered first, so it runs only after the hwmon device is gone */
+ ret = devm_add_action_or_reset(dev, ina4230_pm_disable, ina);
+ if (ret)
+ return ret;
+
+ hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina,
+ &ina4230_chip_info,
+ NULL);
+ if (IS_ERR(hwmon_dev))
+ return dev_err_probe(dev, PTR_ERR(hwmon_dev),
+ "Unable to register hwmon device\n");
+
+ return 0;
+}
+
+static int ina4230_suspend(struct device *dev)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+
+ regcache_cache_only(ina->regmap, true);
+
+ return 0;
+}
+
+static int ina4230_resume(struct device *dev)
+{
+ struct ina4230_data *ina = dev_get_drvdata(dev);
+
+ regcache_cache_only(ina->regmap, false);
+
+ return ina4230_init_chip(ina);
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(ina4230_pm, ina4230_suspend, ina4230_resume,
+ NULL);
+
+static const struct of_device_id ina4230_of_match_table[] = {
+ { .compatible = "ti,ina4230", },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, ina4230_of_match_table);
+
+static const struct i2c_device_id ina4230_ids[] = {
+ { "ina4230" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(i2c, ina4230_ids);
+
+static struct i2c_driver ina4230_i2c_driver = {
+ .probe = ina4230_probe,
+ .driver = {
+ .name = INA4230_DRIVER_NAME,
+ .of_match_table = ina4230_of_match_table,
+ .pm = pm_ptr(&ina4230_pm),
+ },
+ .id_table = ina4230_ids,
+};
+module_i2c_driver(ina4230_i2c_driver);
+
+MODULE_AUTHOR("Alexey Charkov <alchark@flipper.net>");
+MODULE_DESCRIPTION("Texas Instruments INA4230 HWMon Driver");
+MODULE_LICENSE("GPL");
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v6 2/2] hwmon: Add support for TI INA4230 power monitor
2026-12-01 8:17 ` [PATCH v6 2/2] hwmon: Add support for TI INA4230 " Alexey Charkov
@ 2026-12-01 8:37 ` sashiko-bot
2026-12-01 8:49 ` Alexey Charkov
0 siblings, 1 reply; 6+ messages in thread
From: sashiko-bot @ 2026-12-01 8:37 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-hwmon, devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] hwmon: ina4230: missing regcache_mark_dirty() in
ina4230_restore()
- [Medium] hwmon: ina4230: wrong regcache error assumption in
ina4230_write_enable()
- [Medium] hwmon: (ina4230) missing power-down in ina4230_suspend() and
destructive reset in ina4230_resume()
- [Low] hwmon: ina4230: duplicate dev_err_probe() and DT-only regulator
enable
- [Low] hwmon: ina4230: positional initializer in struct i2c_device_id
table
--
commit 1dfb5515b4af2021a38a681fc5d177a15b51cd15
Author: Alexey Charkov <alchark@flipper.net>
hwmon: Add support for TI INA4230 power monitor
This commit adds a hwmon driver for the Texas Instruments INA4230 4-channel
I2C current, voltage, power, and energy monitor.
> diff --git a/drivers/hwmon/ina4230.c b/drivers/hwmon/ina4230.c
> new file mode 100644
> index 0000000000000..cb7750abdbf55
> --- /dev/null
> +++ b/drivers/hwmon/ina4230.c
[ ... ]
> +/*
> + * Restore the cached configuration to the chip. Writing CONFIG1 restarts the
> + * conversions, so write it last, once the ranges and calibration are in place.
> + */
> +static int ina4230_restore(struct ina4230_data *ina)
> +{
> + int ret;
> +
> + ret = regcache_sync_region(ina->regmap, 0, INA4230_CONFIG1 - 1);
> + if (ret)
> + return ret;
> +
> + ret = regcache_sync_region(ina->regmap, INA4230_CONFIG2,
> + INA4230_DEVICE_ID);
> + if (ret)
> + return ret;
> +
> + return regcache_sync_region(ina->regmap, INA4230_CONFIG1, INA4230_CONFIG1);
> +}
[Severity: High]
Does ina4230_restore() need to call regcache_mark_dirty(ina->regmap) before
syncing the regions?
regcache_sync_region() returns immediately without writing anything to the
bus when map->cache_dirty is false, and map->cache_dirty is only set when
regcache_mark_dirty() is called or a register is written while in
cache_only mode. After an external chip reset (in ina4230_recover_reset())
or a suspend/resume cycle with no intervening cached writes (where
ina4230_resume() -> ina4230_init_chip() resets the chip), ina4230_restore()
is a no-op, leaving the hardware calibration and configuration registers at
their reset defaults so current, power, and energy readings stay zero and
4x gain shunt voltages read four times too low.
[ ... ]
> +static int ina4230_write_enable(struct device *dev, int channel, bool enable)
> +{
> + struct ina4230_data *ina = dev_get_drvdata(dev);
> + unsigned int config, mask = INA4230_CONFIG_CHx_EN(channel);
> + int ret;
> +
> + ret = regmap_read(ina->regmap, INA4230_CONFIG1, &config);
> + if (ret)
> + return ret;
> +
> + /* Bypass if enable status is not being changed */
> + if (!!(config & mask) == enable)
> + return 0;
> +
> + if (enable && ina->inputs[channel].disconnected)
> + return -ENODEV;
> +
> + /* For enabling routine, increase refcount and resume() at first */
> + if (enable) {
> + ret = pm_runtime_resume_and_get(ina->pm_dev);
> + if (ret < 0) {
> + dev_err(dev, "Failed to get PM runtime\n");
> + return ret;
> + }
> + }
> +
> + /*
> + * Even if the bus write fails, the cache takes the new value for the next
> + * sync to apply, so the reference follows the cached enable bit.
> + */
> + ret = regmap_update_bits(ina->regmap, INA4230_CONFIG1, mask,
> + enable ? mask : 0);
> +
> + /* For disabling routine, decrease refcount or suspend() at last */
> + if (!enable)
> + pm_runtime_put_sync(ina->pm_dev);
> +
> + return ret;
> +}
[Severity: Medium]
Does INA4230_CONFIG1 actually remain in the regmap cache when
regmap_update_bits() fails on the I2C bus?
For regmap_i2c with a cache, _regmap_raw_write_impl() drops the written
register entry from the cache on a bus write error:
drivers/base/regmap/regmap.c:_regmap_raw_write_impl() {
...
ret = map->write(map->bus_context, work_buf,
map->format.reg_bytes +
map->format.pad_bytes +
val_len);
kfree(buf);
if (ret != 0 && !map->cache_bypass && map->format.parse_val)
map->cache_ops->drop(map, reg, reg + 1);
...
}
If enabling a channel fails, the hardware bit is still cleared and the next
read repopulates the cache from the chip, leaking the runtime PM reference
taken by pm_runtime_resume_and_get(). If disabling the last active channel
fails, pm_runtime_put_sync() suspends the device into cache_only mode with
INA4230_CONFIG1 evicted from the cache, so later regmap_read(INA4230_CONFIG1)
calls fail with -EBUSY, and ina4230_pm_disable() on unbind ignores the failed
regmap_read() and loops over an uninitialized stack variable config when
calling pm_runtime_put_noidle().
[ ... ]
> +static int ina4230_probe_from_dt(struct device *dev, struct ina4230_data *ina)
> +{
> + const struct device_node *np = dev->of_node;
> + int ret;
> +
> + /* Compatible with non-DT platforms */
> + if (!np)
> + return 0;
> +
> + ina->alert_active_high = of_property_read_bool(np, "ti,alert-polarity-active-high");
> +
> + for_each_child_of_node_scoped(np, child) {
> + ret = ina4230_probe_child_from_dt(dev, child, ina);
> + if (ret)
> + return ret;
> + }
> +
> + ret = devm_regulator_get_enable_optional(dev, "vs");
> + if (ret && ret != -ENODEV)
> + return dev_err_probe(dev, ret, "Failed to get regulator\n");
> +
> + return 0;
> +}
[ ... ]
> + ret = ina4230_probe_from_dt(dev, ina);
> + if (ret)
> + return dev_err_probe(dev, ret,
> + "Unable to probe from device tree\n");
[Severity: Low]
Every error path in ina4230_probe_from_dt() and ina4230_probe_child_from_dt()
already calls dev_err_probe(), so should ina4230_probe() return ret directly
to avoid logging a duplicate error and overwriting the deferred-probe reason?
Also, should devm_regulator_get_enable_optional(dev, "vs") run before the
!np early return (or in ina4230_probe()) so the supply is enabled on non-DT
platforms as well?
[ ... ]
> +static int ina4230_suspend(struct device *dev)
> +{
> + struct ina4230_data *ina = dev_get_drvdata(dev);
> +
> + regcache_cache_only(ina->regmap, true);
> +
> + return 0;
> +}
> +
> +static int ina4230_resume(struct device *dev)
> +{
> + struct ina4230_data *ina = dev_get_drvdata(dev);
> +
> + regcache_cache_only(ina->regmap, false);
> +
> + return ina4230_init_chip(ina);
> +}
> +
> +static DEFINE_RUNTIME_DEV_PM_OPS(ina4230_pm, ina4230_suspend, ina4230_resume,
> + NULL);
[Severity: Medium]
Should ina4230_suspend() put the chip into INA4230_MODE_POWERDOWN, and does
ina4230_resume() need to issue a full chip reset via ina4230_init_chip()?
Because DEFINE_RUNTIME_DEV_PM_OPS() also wires these callbacks for system
sleep via pm_runtime_force_suspend() and pm_runtime_force_resume(), leaving
INA4230_CONFIG1 untouched in ina4230_suspend() keeps active channels running
continuous conversions throughout system suspend. On resume, calling
ina4230_init_chip() writes INA4230_CONFIG2_RST, which zeroes all four
hardware energy accumulators even when vs remained powered across suspend,
and if ina4230_init_chip() fails, cache_only is left false while the device
remains suspended.
[ ... ]
> +static const struct i2c_device_id ina4230_ids[] = {
> + { "ina4230" },
> + { /* sentinel */ }
> +};
[Severity: Low]
This isn't a bug, but new struct i2c_device_id entries are expected to use
named initializers ({ .name = "ina4230" }).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-ina4230-v6-0-6b389e3b48bd@flipper.net?part=2
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor
2026-12-01 8:17 [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 1/2] dt-bindings: hwmon: Add TI INA4230 4-channel I2C " Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 2/2] hwmon: Add support for TI INA4230 " Alexey Charkov
@ 2026-12-01 8:37 ` sashiko-bot
2 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-01 8:37 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-hwmon
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("hwmon: Add support for TI INA4230 power monitor"): 5
potential issues (1 High, 2 Medium, 2 Low)
A detailed review has been sent as a separate reply to patch 2.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-ina4230-v6-0-6b389e3b48bd@flipper.net
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v6 2/2] hwmon: Add support for TI INA4230 power monitor
2026-12-01 8:37 ` sashiko-bot
@ 2026-12-01 8:49 ` Alexey Charkov
0 siblings, 0 replies; 6+ messages in thread
From: Alexey Charkov @ 2026-12-01 8:49 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-hwmon, devicetree, robh, conor+dt
On Tue, Dec 1, 2026 at 12:37 PM <sashiko-bot@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 5 potential
> issues to consider:
> - [High] hwmon: ina4230: missing regcache_mark_dirty() in
> ina4230_restore()
> - [Medium] hwmon: ina4230: wrong regcache error assumption in
> ina4230_write_enable()
> - [Medium] hwmon: (ina4230) missing power-down in ina4230_suspend() and
> destructive reset in ina4230_resume()
> - [Low] hwmon: ina4230: duplicate dev_err_probe() and DT-only regulator
> enable
> - [Low] hwmon: ina4230: positional initializer in struct i2c_device_id
> table
All findings seem valid, I will address them in the next version. Thanks bot.
Best regards,
Alexey
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-01 8:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 8:17 [PATCH v6 0/2] Add support for Texas Instruments INA4230 power monitor Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 1/2] dt-bindings: hwmon: Add TI INA4230 4-channel I2C " Alexey Charkov
2026-12-01 8:17 ` [PATCH v6 2/2] hwmon: Add support for TI INA4230 " Alexey Charkov
2026-12-01 8:37 ` sashiko-bot
2026-12-01 8:49 ` Alexey Charkov
2026-12-01 8:37 ` [PATCH v6 0/2] Add support for Texas Instruments " sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox