add support for Microchip PAC194X Power Monitor
Subsystems (2)
v4 cover add support for Microchip PAC194X Power Monitor
From: Marius Cristea <[email protected]>
Add support for Microchip PAC194X and PAC195X series of Power Monitor with Accumulator chip family. This driver covers the following part numbers:
- PAC1941, PAC1941-2, PAC1942, PAC1942-2, PAC1943, PAC1944
- PAC1951, PAC1951-2, PAC1952, PAC1952-2, PAC1953, PAC1954
The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X-1/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
Differences related to previous patch:
v4:
rewrite the driver to keep just basic functionality
- fix review comments for device tree binding:
change compatible from "microchip,pac194(1/2)2" to "microchip,pac194(1/2)-2" rewrite the interrupts and interrupt-names
add gpio-controller
add range for input voltage and current sense voltage
remove the long average mode from hardware accumulator
- fix review comments for the driver:
remove custom attributes
fix include files
remove "non standard" frequency
add range parsing from the device tree for Vbus and Vsense
free acpi allocated buffers in case of early exit
v3:
- fix review comments device tree binding:
rewrite commit message
change the way full scale for voltage and current is set. Add a
properties to describe if the input is bipolar or not.
fix the "dtschema/dtc warnings/errors"
- fix review comments driver:
simplify the driver to include just the basic functionality. More
features will be added later.
fix coding style issues
change to lower case the "to_pac1944_chip_info()" to be more like
other container_of
remove the PAC1944_DEV_ATTR() macro
drop the __func__ bit from error messages
remove unneeded casts
change the logic to reset the accumulator when is enabled change from {} to { }
remove unreachable() from the code
rewrite the code to keep the error paths out of line replace scoped_guard() with guard(), where was possible use to_delayed_work() to get from work to delayed work remove active_channels[] array
print info message in case FW disagrees with what is found on the bus
v2:
- fix review comments device tree binding:
remove underscore from names
add names to the interrupts and list them
add a better description for new properties
- fix review comments driver:
fix coding style issues
use bitmap for checking the active channels
keep the "pac1944_get_unaligned_be56" here because the change wasn't acceted
into the asm-generic.
document new added attributes
remove the "scan" part till we support buffered capture
remove "unlikely" marking
add masks up in some array of const structures to avoid some case statements remove pac1944_mutex_destroy function
replace some functions with a macro (just for testing)
replace dev_err with dev_err_probe in functions used in pac1944_probe
v1:
- first version committed to review
Marius Cristea (2):
dt-bindings: iio: adc: add support for PAC1944
iio: adc: add support for PAC194X
.../bindings/iio/adc/microchip,pac1944.yaml | 315 +++ MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2172 +++++++++++++++++ 5 files changed, 2507 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml create mode 100644 drivers/iio/adc/pac1944.c
base-commit: d2a4ec19d2a2e54c23b5180e939994d3da4a6b91
Signature · 1 line
2.51.0
PATCH v4 1/2 dt-bindings: iio: adc: add support for PAC1944
From: Marius Cristea <[email protected]>
This is the device tree schema for iio driver for Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications.
The PAC194X/5X-1 is named shortly PAC194X/5X.
--- .../bindings/iio/adc/microchip,pac1944.yaml | 315 ++++++++++++++++++ MAINTAINERS | 6 + 2 files changed, 321 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +315 -0
@@ -0,0 +1,315 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/microchip,pac1944.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip PAC194x and PAC195x Power Monitors with Accumulator + +maintainers: + - Marius Cristea <[email protected]> + +description: | + This device is part of the Microchip family of Power Monitors with + Accumulator. The datasheet for PAC1941-1, PAC1941-2, PAC1942-1, PAC1942-2, + PAC1943-1 and PAC1944-1 can be found here: + https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC194X-Family-Data-Sheet-DS20006543.pdf + The datasheet for PAC1951-1, PAC1951-2, PAC1952-1, PAC1952-2, PAC1953-1 and + PAC1954-1 can be found here: + https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC195X-Family-Data-Sheet-DS20006539.pdf + +properties: + compatible: + enum: + - microchip,pac1941 + - microchip,pac1941-2 + - microchip,pac1942 + - microchip,pac1942-2 + - microchip,pac1943 + - microchip,pac1944 + - microchip,pac1951 + - microchip,pac1951-2 + - microchip,pac1952 + - microchip,pac1952-2 + - microchip,pac1953 + - microchip,pac1954 + + reg: + maxItems: 1 + + vdd-supply: true + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + items: + - description: + ALERT1 output. Asserted when a user-programmed over/under + voltage, current, or power limit routed to ALERT1 is exceeded. + - description: + ALERT2 output. Asserted when a user-programmed over/under + voltage, current, or power limit routed to ALERT2 is exceeded. + minItems: 1 + + interrupt-names: + oneOf: + - items: + - const: alert1 + - items: + - const: alert2 + - items: + - const: alert1 + - const: alert2 + + gpio-controller: true + + "#gpio-cells": + const: 2 + description: | + GPIO 0 corresponds to the SLOW/ALERT1/GPIO multi-function pin and GPIO 1 + corresponds to the ALERT2/GPIO multi-function pin. The second cell takes + standard GPIO flags. + + powerdown-gpios: + description: | + A GPIO used to trigger a change in sampling rate (lowering the chip + power consumption). Driving this GPIO low puts the device in Power-Down + state (all circuitry is powered down, including SMBus). It should be + marked GPIO_ACTIVE_LOW. + maxItems: 1 + + slow-gpios: + description: | + SLOW input pin. If this pin is forced high, the sampling rate is forced + to 8 SPS. When it is forced low, the sampling rate is 1024 SPS, unless + a different sample rate has been programmed. This pin is multi-function: + SLOW input, ALERT1 output, or general-purpose (open-drain) GPIO; when + used as slow-gpios the ALERT1 function is not available. It should be + marked GPIO_ACTIVE_HIGH. + maxItems: 1 + +patternProperties: + "^channel@[1-4]$": + type: object + $ref: adc.yaml + description: + Represents the external channels which are connected to the ADC. + + properties: + reg: + items: + minimum: 1 + maximum: 4 + + shunt-resistor-micro-ohms: + description: + Value in micro Ohms of the shunt resistor connected between + the SENSE+ and SENSE- inputs, across which the current is measured. + Value is needed to compute the scaling of the measured current. + minimum: 1 + + microchip,vbus-input-range-microvolt: + description: | + In order to increase measurement resolution while keeping the same + number of bits, the device has a configurable VBUS full-scale range + (FSR). The range should be set by hardware design and it should not + be changed during runtime. The bipolar capability for VBUS enables + accurate offset measurement and correction. + The VBUS could be configured into the following full-scale range: + - VBUS has unipolar 0V to 32V FSR (default) for PAC195X or 0V to 9V + (default) for PAC194X. + - VBUS has bipolar -32V to 32V FSR for PAC195X or -9V to 9V for + PAC194X. The actual range is limited to about -200 mV due to the + impact of the ESD structures. + - VBUS has bipolar -16V to 16V FSR for PAC195X or -4.5V to 4.5V for + PAC194X. The actual range is limited to about -200 mV due to the + impact of the ESD structures. + + microchip,vsense-input-range-microvolt: + description: | + In order to decrease the power dissipation on the shunt resistor + and, at the same time, to increase measurement resolution while + keeping the same number of bits, the device has a configurable + VSENSE full-scale scale (FSR). The range should be set by hardware + design and it should not be changed during runtime. + The VSENSE could be configured into the following full-scale range: + - VSENSE has unipolar 0V to 100 mV FSR (default) + - VSENSE has bipolar -100 mV to 100 mV FSR + - VSENSE has bipolar -50 mV to 50 mV FSR + oneOf: + - items: + - const: 0 + - const: 100000 + - items: + - const: -50000 + - const: 50000 + - items: + - const: -100000 + - const: 100000 + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The Hardware Accumulator may be used to accumulate VPOWER or VSENSE + values for any channel. By setting the accumulator for a channel + to accumulate the VPOWER values gives a measure of accumulated power + over a time period, which is equivalent to energy. Setting the + accumulator for a channel to accumulate VSENSE values gives a measure + of accumulated current, which is equivalent to charge. This allows the + accumulator to be used as a coulomb counter. + This functionality needs to be setup once and must not be changed + during the runtime, just in case the user wants to measure the charge + or the energy consumed from board power up till the user has control + or during a reboot of the system. + The Hardware Accumulator could be configured to accumulate VPOWER + or VSENSE + <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE + enum: [0, 1] + default: 0 + + required: + - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells" + +allOf: + - if: + properties: + compatible: + pattern: "^microchip,pac194" + then: + patternProperties: + "^channel@[1-4]$": + properties: + microchip,vbus-input-range-microvolt: + oneOf: + - items: + - const: 0 + - const: 9000000 + - items: + - const: -9000000 + - const: 9000000 + - items: + - const: -4500000 + - const: 4500000 + default: [0, 9000000] + - if: + properties: + compatible: + pattern: "^microchip,pac195" + then: + patternProperties: + "^channel@[1-4]$": + properties: + microchip,vbus-input-range-microvolt: + oneOf: + - items: + - const: 0 + - const: 32000000 + - items: + - const: -32000000 + - const: 32000000 + - items: + - const: -16000000 + - const: 16000000 + default: [0, 32000000] + - if: + properties: + compatible: + pattern: "^microchip,pac19[45]1(-2)?$" + then: + patternProperties: + "^channel@[2-4]$": false + - if: + properties: + compatible: + pattern: "^microchip,pac19[45]2(-2)?$" + then: + patternProperties: + "^channel@[3-4]$": false + - if: + properties: + compatible: + pattern: "^microchip,pac19[45]3(-2)?$" + then: + properties: + channel@4: false + - if: + required: + - slow-gpios + then: + properties: + interrupts: + maxItems: 1 + interrupt-names: + oneOf: + - items: + - const: alert2 + - if: + properties: + interrupt-names: + contains: + const: alert1 + required: + - interrupt-names + then: + properties: + slow-gpios: false + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@10 { + compatible = "microchip,pac1954"; + reg = <0x10>; + vdd-supply = <&vdd>; + + #address-cells = <1>; + #size-cells = <0>; + + channel@1 { + reg = <0x1>; + shunt-resistor-micro-ohms = <24900>; + label = "CPU"; + microchip,vbus-input-range-microvolt = <0 32000000>; + microchip,vsense-input-range-microvolt = <(-50000) 50000>; + }; + + channel@3 { + reg = <0x3>; + shunt-resistor-micro-ohms = <75000>; + label = "MEM"; + microchip,vbus-input-range-microvolt = <(-16000000) 16000000>; + microchip,vsense-input-range-microvolt = <0 100000>; + }; + + channel@4 { + reg = <0x4>; + shunt-resistor-micro-ohms = <100000>; + label = "NET"; + microchip,vbus-input-range-microvolt = <(-32000000) 32000000>; + microchip,vsense-input-range-microvolt = <(-100000) 100000>; + microchip,accumulation-mode = <1>; + }; + }; + }; + +...
MAINTAINERS +6 -0
@@ -17385,6 +17385,12 @@ S: Supported F: Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml F: drivers/iio/adc/pac1934.c +MICROCHIP PAC1944 ADC DRIVER +M: Marius Cristea <[email protected]> +L: [email protected] +S: Supported +F: Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml + MICROCHIP PCI1XXXX GP DRIVER M: Vaibhaav Ram T.L <[email protected]> M: Kumaravel Thiagarajan <[email protected]>
Signature · 1 line
2.51.0
[email protected]
AI reviewRe: [PATCH v4 1/2] dt-bindings: iio: adc: add support for PAC1944
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The binding defines GPIO controller capabilities but omits `gpio-controller` and `#gpio-cells`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding defines GPIO controller capabilities but omits gpio-controller and #gpio-cells from the required list and examples block. --
commit a57d647e4dad8345f6c818b69281dba50b43a7f4
Author: Marius Cristea <[email protected]>
dt-bindings: iio: adc: add support for PAC1944
This commit introduces the device tree schema for the Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. It covers variants for high-side and low-side current sensing or floating VBUS applications.
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 186
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +6
-0+required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells"
Should gpio-controller and #gpio-cells be added to this required list?
According to the dt-bindings guidelines, when a binding defines hardware provider capabilities such as a GPIO controller, the standard properties should be explicitly required to prevent incomplete device tree nodes from passing schema validation.
[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 286
Quoted 13 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +13
-0+examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@10 { + compatible = "microchip,pac1954"; + reg = <0x10>; + vdd-supply = <&vdd>; + + #address-cells = <1>; + #size-cells = <0>;
Should gpio-controller and #gpio-cells also be included in this example block?
If they are added to the required properties list, omitting them from the examples section might cause dt_binding_check to fail.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v4 1/2] dt-bindings: iio: adc: add support for PAC1944
Quoted 26 lines · On Fri, May 15, 2026 at 09:38:47AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding defines GPIO controller capabilities but omitsgpio-controllerand#gpio-cellsfrom therequiredlist andexamplesblock. --commit
a57d647e4dad8345f6c818b69281dba50b43a7f4
Author: Marius Cristea <[email protected]>dt-bindings: iio: adc: add support for PAC1944
This commit introduces the device tree schema for the Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. It covers variants for high-side and low-side current sensing or floating VBUS applications.
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +0
-0[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 186
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +6
-0+required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells"
Quoted 6 lines
Should
gpio-controllerand#gpio-cellsbe added to thisrequiredlist?According to the dt-bindings guidelines, when a binding defines hardware provider capabilities such as a GPIO controller, the standard properties should be explicitly required to prevent incomplete device tree nodes from passing schema validation.
Same here as the other series, bot is not capable of deducing that interrupts and gpio use have some mutual exclusion.
Quoted 24 lines
[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 286
Quoted 13 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +13
-0+examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@10 { + compatible = "microchip,pac1954"; + reg = <0x10>; + vdd-supply = <&vdd>; + + #address-cells = <1>; + #size-cells = <0>;Should
gpio-controllerand#gpio-cellsalso be included in this example block?If they are added to the required properties list, omitting them from the examples section might cause
dt_binding_checkto fail.Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v4 1/2] dt-bindings: iio: adc: add support for PAC1944
In reply to [email protected]Quoted 11 lines · On Fri, May 15, 2026 at 12:29:45PM +0300, [email protected] wrote:
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +11
-0+ + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The Hardware Accumulator may be used to accumulate VPOWER or VSENSE + values for any channel. By setting the accumulator for a channel + to accumulate the VPOWER values gives a measure of accumulated power + over a time period, which is equivalent to energy. Setting the + accumulator for a channel to accumulate VSENSE values gives a measure + of accumulated current, which is equivalent to charge. This allows the + accumulator to be used as a coulomb counter.
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 173
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ This functionality needs to be setup once and must not be changed + during the runtime, just in case the user wants to measure the charge + or the energy consumed from board power up till the user has control + or during a reboot of the system. + The Hardware Accumulator could be configured to accumulate VPOWER + or VSENSE + <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE + enum: [0, 1] + default: 0
I think you should make this a string property, 0/1 don't mean anything here, whereas "vpower" and "vsense" would be very clear.
Otherwise, this looks okay to me I think, other than
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 260
Quoted 11 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +11
-0+ - if: + required: + - slow-gpios + then: + properties: + interrupts: + maxItems: 1 + interrupt-names: + oneOf: + - items: + - const: alert2
this, which shouldn't need the "oneOf" at all.
PATCH v4 2/2 iio: adc: add support for PAC194X
In reply to [email protected]From: Marius Cristea <[email protected]>
This is the iio driver for Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
--- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2172 +++++++++++++++++++++++++++++++++++++ 4 files changed, 2186 insertions(+) create mode 100644 drivers/iio/adc/pac1944.c
MAINTAINERS +1 -0
@@ -17390,6 +17390,7 @@ M: Marius Cristea <[email protected]> L: [email protected] S: Supported F: Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml +F: drivers/iio/adc/pac1944.c MICROCHIP PCI1XXXX GP DRIVER M: Vaibhaav Ram T.L <[email protected]>
drivers/iio/adc/Kconfig +12 -0
@@ -1290,6 +1290,18 @@ config PAC1934 This driver can also be built as a module. If so, the module will be called pac1934. +config PAC1944 + tristate "Microchip Technology PAC1944/PAC1954 driver" + depends on I2C + help + Say yes here to build support for Microchip Technology's PAC1941, + PAC1941-2, PAC1942, PAC1942-2, PAC1943, PAC1944, PAC1951, + PAC1951-2, PAC1952, PAC1952-2, PAC1953, PAC1954 + Single/Multi-Channel Power Monitor with Accumulator. + + This driver can also be built as a module. If so, the module + will be called pac1944. + config PALMAS_GPADC tristate "TI Palmas General Purpose ADC" depends on MFD_PALMAS
drivers/iio/adc/Makefile +1 -0
@@ -113,6 +113,7 @@ obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1934) += pac1944.o obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o obj-$(CONFIG_QCOM_SPMI_ADC5) += qcom-spmi-adc5.o
drivers/iio/adc/pac1944.c new file +2172 -0
2,172 lines not on this page · raw message
Signature · 1 line
2.51.0
[email protected]
AI reviewRe: [PATCH v4 2/2] iio: adc: add support for PAC194X
Thank you for your contribution! Sashiko AI review found 12 potential issue(s) to consider: - [High] Makefile typo prevents driver compilation. - [High] Unvalidated shunt resistor allows…
Thank you for your contribution! Sashiko AI review found 12 potential issue(s) to consider:
- [High] Makefile typo prevents driver compilation.
- [High] Unvalidated shunt resistor allows divide-by-zero kernel panic.
- [High] Missing type check on ACPI object returned by acpi_evaluate_dsm.
- [High] Incomplete check of i2c_transfer return value leads to uninitialized memory use.
- [High] Register parsing misalignment when channels are disabled.
- [High] TOCTOU race in pac1944_retrieve_data() causes data loss.
- [High] Unprotected lockless reads of shared state cause data corruption and torn reads.
- [High] Unconditional re-arming of delayed work causes Use-After-Free.
- [High] Passing stack memory to i2c_transfer() violates DMA mapping rules.
- [High] Accumulator saturation applies in the wrong direction upon overflow.
- [Medium] Power scale calculation overflows 32-bit signed integers.
- [Medium] ACPI package object leaked on error paths during probe.
--
commit f9f3f02e24d9b1eec599682a12c6ee4c2e8e0b5f
Author: Marius Cristea <[email protected]>
Subject: iio: adc: add support for PAC194X
This commit adds the IIO driver for the Microchip PAC194X and PAC195X series of Power Monitors. It supports multiple voltage and current channels.
drivers/iio/adc/Makefile obj-$(CONFIG_NPCM_ADC) += npcm_adc.o · line 116
Quoted 9 lines
drivers/iio/adc/Makefile +1
-0@@ -113,6 +113,7 @@ obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1934) += pac1944.o
This isn't a bug, but there is a typo in the config name. Should this be CONFIG_PAC1944?
Quoted 1 line
drivers/iio/adc/Makefile +0
-0obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
[ ... ]
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +0
-0
[ ... ]
drivers/iio/adc/pac1944.c line 573
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+static int pac1944_i2c_read (struct i2c_client *client,u8 reg_addr, +void *databuf,u8 len) +{ +struct i2c_msg msgs[2 ] = { + { + .addr = client->addr, + .len =1 , + .buf = (u8 *)®_addr,
Can this cause a panic when CONFIG_VMAP_STACK is enabled?
Since reg_addr is on the stack, it might not be DMA-safe for I2C host controllers that use DMA. Should this be placed in a bounce buffer?
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ .flags = 0 + },
[ ... ]
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+static ssize_t pac1944_shunt_value_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +struct iio_dev *indio_dev =dev_to_iio_dev (dev);
drivers/iio/adc/pac1944.c line 614
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+struct pac1944_chip_info *info =iio_priv (indio_dev); +int sh_val; + +if (kstrtouint (buf,10 , &sh_val)) { +dev_err (dev,"Shunt value is not valid\n" ); +return -EINVAL ; + } + +scoped_guard (mutex, &info->lock) + info->shunts[this_attr->address] = sh_val;
Does this allow writing a value of 0?
If info->shunts[idx] is set to 0, will the division operations in pac1944_in_power_acc_scale_show() and pac1944_read_raw() trigger a divide-by-zero hardware exception?
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+ +return count; +}
[ ... ]
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+static int pac1944_reg_snapshot (struct pac1944_chip_info *info,bool do_refresh, +u8 refresh_addr,u32 wait_time) +{
[ ... ]
drivers/iio/adc/pac1944.c line 752
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+/* Read the data registers. */ + ret =pac1944_i2c_read (client,PAC1944_ACC_COUNT_REG_ADDR , + (u8 *)info->chip_reg_data.meas_regs, +PAC1944_MEAS_REG_SNAPSHOT_LEN ); +if (ret <0 ) {
What happens if i2c_transfer() executes only the first message and returns 1?
The condition ret < 0 will pass, but the meas_regs buffer might remain uninitialized. Should this check that ret equals the number of messages instead?
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+dev_err (dev,"cannot read PAC1944 regs from 0x%02X\n" ,PAC1944_ACC_COUNT_REG_ADDR ); +return ret; + } + + offset_reg_data_p = &info->chip_reg_data.meas_regs[0 ]; + + info->chip_reg_data.acc_count =get_unaligned_be32 (offset_reg_data_p); + + offset_reg_data_p +=PAC1944_ACC_REG_LEN ; +
drivers/iio/adc/pac1944.c line 772
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* +* Check if the channel is active (within the data read from the chip), +* skip all fields if disabled. +*/ +for_each_set_bit (cnt, &info->active_channels_mask, info->phys_channels) { +/* skip if the energy accumulation is disabled */ +if (!info->enable_acc[cnt]) { + offset_reg_data_p +=PAC1944_VACC_REG_LEN ; +continue ; + }
If a channel is entirely inactive (its bit is not set in active_channels_mask), the loop skips it completely.
Will this fail to advance offset_reg_data_p past the inactive channel's fixed slot in the buffer, causing subsequent reads to be structurally misaligned?
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ + stored_value = info->chip_reg_data.acc_val[cnt];
[ ... ]
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+if (smpl_mode <=PAC1944_SAMP_8SPS ) { + tmp_s64 = info->chip_reg_data.vacc[cnt]; +/* +* Find how much shift is required by the sample rate. +* The chip's sampling rate is 2^shift samples/sec. +*/ + shift = shift_map_tbl[smpl_mode]; + inc = tmp_s64 >> shift;
drivers/iio/adc/pac1944.c line 820
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ }else { +dev_err (dev,"Invalid sample rate index: %d!\n" , smpl_mode); +return -EINVAL ; + } + +if (check_add_overflow (stored_value, inc, &stored_value)) { +if (stored_value <0 ) + info->chip_reg_data.acc_val[cnt] =S64_MIN ; +else + info->chip_reg_data.acc_val[cnt] =S64_MAX ;
Does this clamp in the wrong direction?
When two large positive values overflow, check_add_overflow() places the wrapped negative result into stored_value.
Will the condition stored_value < 0 evaluate to true and clamp the positive overflow to S64_MIN instead of S64_MAX?
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+ +dev_err (dev,"Overflow detected on channel [%d]!\n" , cnt +1 ); + }else {
[ ... ]
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+static int pac1944_retrieve_data (struct pac1944_chip_info *info,u32 wait_time) +{ +int ret; +
drivers/iio/adc/pac1944.c line 897
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* +* Check if the minimal elapsed time has passed and if so, +* re-read the chip, otherwise the cached info is just fine. +*/ +if (!time_after (jiffies, info->chip_reg_data.jiffies_tstamp + +msecs_to_jiffies (PAC1944_MIN_POLLING_TIME_MS ))) +return 0 ; + +/* We need to re-read the chip values */ + ret =pac1944_reg_snapshot (info, true,PAC1944_REFRESH_REG_ADDR , wait_time);
Is there a race condition here if multiple threads read sysfs concurrently?
If the cache is stale, both threads could pass the time_after() check locklessly, serialize on the lock in pac1944_reg_snapshot(), and issue sequential REFRESH commands.
Since REFRESH resets the chip accumulators, will the second thread wipe out the data and read near-zero values? Should the expiration check be re-evaluated inside the mutex lock?
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +/*
[ ... ]
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+static int pac1944_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +int ret, idx; +u64 tmp; + + ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + +/* +* In the datasheet channels are referred to as 1 to 4. For AVG the index +* should be between 5 to 8. To calculate PAC1944_CH_VOLTAGE_AVERAGE and
drivers/iio/adc/pac1944.c line 1268
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* PAC1944_CH_CURRENT_AVERAGE real index, we need to remove the added +* offset (PAC1944_MAX_CH). +*/ + idx = (chan->channel -1 ) %PAC1944_MAX_CH ; + +switch (mask) { +case IIO_CHAN_INFO_RAW : +switch (chan->type) { +case IIO_VOLTAGE : + *val = info->chip_reg_data.vbus[idx];
Could this result in torn reads or corrupted data?
The fields in info->chip_reg_data are read locklessly if the cache is deemed fresh.
At the same time, pac1944_work_periodic_rfsh() can execute pac1944_reg_snapshot() and modify these fields under info->lock. Could a reader capture an intermediate state?
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+return IIO_VAL_INT ;
[ ... ]
drivers/iio/adc/pac1944.c line 1359
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+case PAC1944_VPOWER_1_ADDR : +case PAC1944_VPOWER_2_ADDR : +case PAC1944_VPOWER_3_ADDR : +case PAC1944_VPOWER_4_ADDR : +if (info->is_pac195x_family) + tmp =PAC195X_PRODUCT_VOLTAGE_PV_FSR ; +else + tmp =PAC194X_PRODUCT_VOLTAGE_PV_FSR ; + +do_div (tmp, info->shunts[idx]); + *val = (int )tmp;
Can this overflow the 32-bit signed integer?
For a typical 1 mOhm shunt, PAC195X_PRODUCT_VOLTAGE_PV_FSR (3.2 trillion) divided by 1000 is 3.2 billion, which exceeds the maximum value of a signed 32-bit integer. Will this expose a negative scale to userspace?
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+ if ((info->chip_reg_data.vbus_mode[idx] == PAC1944_UNIPOLAR_FSR_CFG &&
[ ... ]
drivers/iio/adc/pac1944.c line 1449
Quoted 13 lines
drivers/iio/adc/pac1944.c new file +13
-0+static void pac1944_work_periodic_rfsh (struct work_struct *work) +{ +struct pac1944_chip_info *info =to_pac1944_chip_info (to_delayed_work (work)); +struct i2c_client *client = info->client; + +dev_dbg (&client->dev,"Periodic refresh\n" ); + +pac1944_reg_snapshot (info, true,PAC1944_REFRESH_REG_ADDR , +PAC1944_MIN_UPDATE_WAIT_TIME_US ); + +mod_delayed_work (system_percpu_wq, &info->work_chip_rfsh, +msecs_to_jiffies (PAC1944_MAX_RFSH_LIMIT_MS )); +}
Can this re-arm the work after the device is unbound?
The driver uses devm_add_action_or_reset() to call cancel_delayed_work_sync(), but if that runs concurrently with this periodic refresh, could mod_delayed_work() schedule the work again on freed memory?
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+ +/* +* Universal Unique Identifier (UUID),
[ ... ]
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+static int pac1944_acpi_parse_channel_config (struct i2c_client *client, +struct pac1944_chip_info *info) +{ +struct device *dev = &client->dev; +const struct acpi_device_id *id; +unsigned short bi_dir_mask; +union acpi_object *rez; +acpi_handle handle; +guid_t guid; +int i; + + handle =ACPI_HANDLE (dev); + + id =acpi_match_device (dev->driver->acpi_match_table, dev);
drivers/iio/adc/pac1944.c line 1485
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (!id) +return -ENODEV ; + +guid_parse (PAC1944_DSM_UUID , &guid); + + rez =acpi_evaluate_dsm (handle, &guid,0 ,PAC1944_ACPI_GET_NAMES ,NULL ); +if (!rez) +return -EINVAL ; + +if (rez->package.count >PAC1944_MAX_CH ) {
What happens if a buggy firmware returns an ACPI_TYPE_INTEGER instead of ACPI_TYPE_PACKAGE?
The code directly accesses rez->package.count without verifying rez->type == ACPI_TYPE_PACKAGE. Could this read overlapping union members and cause an out-of-bounds dereference?
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+ACPI_FREE (rez); +return -EINVAL ; + }
[ ... ]
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+for_each_set_bit (i, &info->active_channels_mask, info->phys_channels) { +if (rez->package.elements[i].type !=ACPI_TYPE_INTEGER ) { +ACPI_FREE (rez); +return -EINVAL ; + } +
drivers/iio/adc/pac1944.c line 1555
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ bi_dir_mask = rez->package.elements[i].integer.value; + +if (bi_dir_mask ==PAC1944_UNIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (dev,"VBUS{%d} mode set to: %d\n" , i, bi_dir_mask); + info->chip_reg_data.vbus_mode[i] = bi_dir_mask; + }else { +return dev_err_probe (dev, -EINVAL ,"invalid vbus-mode value on %i\n" , i); + }
Does this leak the memory for the rez object?
It looks like ACPI_FREE(rez) is bypassed when returning an error here.
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +if (rez->package.elements[i +PAC1944_MAX_CH ].type !=ACPI_TYPE_INTEGER ) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
David Lechner
reviewerRe: [PATCH v4 2/2] iio: adc: add support for PAC194X
In reply to [email protected]Quoted 11 lines · On 5/15/26 4:29 AM, [email protected] wrote:
From: Marius Cristea <[email protected]>
This is the iio driver for Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
Quoted 8 lines
Signed-off-by Marius Cristea <[email protected]>--- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2172 +++++++++++++++++++++++++++++++++++++ 4 files changed, 2186 insertions(+) create mode 100644 drivers/iio/adc/
Can we please split this up into multiple patches by feature added?
We are already drowning in reviews here in the IIO subsystem. It takes many times longer to review things that are not split up into bite-sized pieces (aim for 500 lines in a patch).
Jonathan Cameron
maintainerRe: [PATCH v4 2/2] iio: adc: add support for PAC194X
In reply to [email protected]Quoted 6 lines · On Fri, 15 May 2026 12:29:46 +0300 <[email protected]> wrote:
From: Marius Cristea <[email protected]>
This is the iio driver for Microchip PAC194X and PAC195X series of Power Monitors with Accumulator. The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
Quoted 6 lines
There are two versions of the PAC194X/5X: the PAC194X/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
Signed-off-by Marius Cristea <[email protected]>
If you can find a way to break this up and add features over a series of patches that would make it a lot easier to review.
The huge nature of the patch means review quality will be down (I'm definitely tired now!).
Sashiko had fun
https://sashiko.dev/#/patchset/20260515092946.10791-1-marius.cristea%40microchip.com
Not all are necessarily write but some definitely are.
The DMA + stack stuff is relevant for i2c which will by default bounce data anyway.
Biggest thing in here is I wonder if we can make the _acc bit a modifier and then fit the majority if not all of the custom attrs related to that into standard channel definitions.
Jonathan
drivers/iio/adc/Makefile obj-$(CONFIG_NPCM_ADC) += npcm_adc.o · line 116
Quoted 9 lines
drivers/iio/adc/Makefile +1
-0@@ -113,6 +113,7 @@ obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1934) += pac1944.o
Sashiko got this one. I doubt many humans would notice but that's the wrong CONFIG.
Anyhow, I'll not point out everything it raised - will assume you've dealt with any that are correct.
Quoted 16 lines
drivers/iio/adc/Makefile +0
-0obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o obj-$(CONFIG_QCOM_SPMI_ADC5) += qcom-spmi-adc5.odrivers/iio/adc/pac1944.c new file +7
-0@@ -0,0 +1,2172 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* IIO driver for PAC194X and PAC195X series chips +* +* Copyright (C) 2022-2026 Microchip Technology Inc. and its subsidiaries +* +* Author: Marius Cristea [email protected]
drivers/iio/adc/pac1944.c line 17
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* +* Datasheet for PAC1941, PAC1942, PAC1943 and PAC1944 can be found here: +* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC194X-Family-Data-Sheet-DS20006543.pdf +* Datasheet for PAC1951, PAC1952, PAC1953 and PAC1954 can be found here: +* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC195X-Family-Data-Sheet-DS20006539.pdf +*/ + +#include <linux/acpi.h> +#include <linux/bitfield.h> +#include <linux/delay.h>
This looks too minimal.
Apply the somewhat fuzzy include what we use stuff we use for kernel. dev_printk.h is missing for instance as is mod_devicetable.h I haven't looked beyond those.
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/iio/iio.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/module.h>
drivers/iio/adc/pac1944.c line 88
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+#include <linux/mutex.h> +#include <linux/property.h> +#include <linux/unaligned.h> + +/* Maximum power-product value - 32 V * 0.1 V */ +#define PAC195X_PRODUCT_VOLTAGE_PV_FSR 3200000000000UL + +/* Maximum power-product value - 9 V * 0.1 V */ +#define PAC194X_PRODUCT_VOLTAGE_PV_FSR 900000000000UL
Can we use some of the multiplier types to express that in a form that doesn't leave us counting zeros. Same for other cases with more than say 6 zeros.
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+ +#define PAC1944_MEAS_REG_SNAPSHOT_LEN 80 +#define PAC1944_CTRL_REG_SNAPSHOT_LEN 24 +static const int pac1944_adc_range_tbl[][2 ] = { + [PAC1944_ADC_FULL_RANGE_BIPOLAR ] = {-9000000 ,9000000 }, + [PAC1944_ADC_HALF_RANGE_BIPOLAR ] = {-4500000 ,4500000 }, + [PAC1944_ADC_FULL_RANGE_UNIPOLAR ] = {0 ,9000000 }, +}; +
drivers/iio/adc/pac1944.c line 400
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+static const int pac1954_adc_range_tbl[][2 ] = { + [PAC1944_ADC_FULL_RANGE_BIPOLAR ] = {-32000000 ,32000000 }, + [PAC1944_ADC_HALF_RANGE_BIPOLAR ] = {-16000000 ,16000000 }, + [PAC1944_ADC_FULL_RANGE_UNIPOLAR ] = {0 ,32000000 }, +}; + +static const int pac1944_adc_vsense_range_tbl[][2 ] = { + [PAC1944_ADC_FULL_RANGE_BIPOLAR ] = {-100000 ,100000 }, + [PAC1944_ADC_HALF_RANGE_BIPOLAR ] = {-50000 ,50000 }, + [PAC1944_ADC_FULL_RANGE_UNIPOLAR ] = {0 ,100000 },
Trivial but I'm trying to standardize some formatting beyond what kernel wide kernel style says.
[PAC1944_ADC_FULL_RANGE_UNIPOLAR] = { 0, 100000 },drivers/iio/adc/pac1944.c line 406
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+}; + +/** +* struct reg_data - data from the registers +* @vsense_mode:array of values, Full Scale Range (FSR) mode for V Sense +* @vbus_mode: array of values, Full Scale Range (FSR) mode for V Bus
Order doesn't match.
Quoted 46 lines
+ * @accumulation_mode: array of values, accumulation mode for hardware accumulator
+ * @meas_regs: snapshot of raw measurements registers
+ * @ctrl_act_reg: snapshot of thectrl_actregister
+ * @ctrl_lat_reg: snapshot of thectrl_latregister
+ * @acc_count: snapshot of theacc_countregister
+ * @acc_val: accumulated values per second
+ * @vacc: accumulated vpower values
+ * @vpower: snapshot of vpower registers
+ * @vbus: snapshot of vbus registers
+ * @vbus_avg: averages of vbus registers
+ * @vsense: snapshot of vsense registers
+ * @vsense_avg: averages of vsense registers
+ * @jiffies_tstamp: chip's uptime
+ */drivers/iio/adc/pac1944.c new file +32
-0+struct reg_data { +u8 vbus_mode[PAC1944_MAX_CH ]; +u8 vsense_mode[PAC1944_MAX_CH ]; +u8 accumulation_mode[PAC1944_MAX_CH ]; +u8 meas_regs[PAC1944_MEAS_REG_SNAPSHOT_LEN ]; +u16 ctrl_act_reg; +u16 ctrl_lat_reg; +u32 acc_count; +s64 acc_val[PAC1944_MAX_CH ]; +s64 vacc[PAC1944_MAX_CH ]; +s32 vpower[PAC1944_MAX_CH ]; +s32 vbus[PAC1944_MAX_CH ]; +s32 vbus_avg[PAC1944_MAX_CH ]; +s32 vsense[PAC1944_MAX_CH ]; +s32 vsense_avg[PAC1944_MAX_CH ]; +unsigned long jiffies_tstamp; +}; + +/** +* struct pac1944_chip_info - chip configuration +* @channels: array of values, true means that channel is active +* @iio_info: pointer to iio_info structure +* @client: a pointer to the i2c client associated with the device +* @lock: lock to prevent concurrent reads/writes +* @work_chip_rfsh: chip refresh workqueue implementation +* @active_channels_mask: active channels +* @phys_channels: number of physical channels for the device +* @shunts: array of values, shunt resistor values +* @chip_reg_data: pointer to structure, containing data from the device registers +* @labels: array of string, name of each channel +* @is_pac195x_family: true if device is part of the PAC195x family +* @sampling_mode: sampling mode used by the device
drivers/iio/adc/pac1944.c line 462
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* @num_enabled_channels: count of how many chip channels are currently enabled +* @enable_acc: array of values, true means that accumulation channel is measured +*/ +struct pac1944_chip_info { +const struct iio_chan_spec *channels; +struct iio_info iio_info; +struct i2c_client *client; +struct mutex lock;/* lock to prevent concurrent reads/writes */ +struct delayed_work work_chip_rfsh; +unsigned long active_channels_mask;
DECLARE_BITMAP() Is probably the right option even if it's only ever going to be 1 unsigned long. Makes it obvious it's a bitmap.
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+u8 phys_channels; +u32 shunts[PAC1944_MAX_CH ]; +struct reg_data chip_reg_data; +char *labels[PAC1944_MAX_CH ]; +bool is_pac195x_family; +u8 sampling_mode; +u8 num_enabled_channels; +bool enable_acc[PAC1944_MAX_CH ];
drivers/iio/adc/pac1944.c
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+}; + +/* Low-level I2c functions used to transfer more then 32 bytes at once */ +static int pac1944_i2c_read (struct i2c_client *client,u8 reg_addr, +void *databuf,u8 len) +{ +struct i2c_msg msgs[2 ] = { + { + .addr = client->addr,
Should be indented one table more than {
.addr = client->addr,
Is this the same as i2c_smbus_read_i2c_block_data() ?
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+ .len = 1, + .buf = (u8 *)®_addr, + .flags = 0 + }, + { + .addr = client->addr, + .len = len, + .buf = databuf, + .flags = I2C_M_RD + } + }; + +return i2c_transfer (client->adapter, msgs,ARRAY_SIZE (msgs)); +}
drivers/iio/adc/pac1944.c line 643
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+ +#define PAC1944_VPOWER_CHANNEL (_index, _address) { \ + .type = IIO_POWER, \ + .address = (_address), \ + .indexed = 1, \ + .channel = (_index) + 1, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .ext_info = pac1944_ext_info \
If these are only used once, just put the value setting in line at the place they are used. Separate macros just make us go looking for them.
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+} + +static int pac1944_send_refresh (struct pac1944_chip_info *info,u8 refresh_addr, +u32 wait_time) +{ +struct i2c_client *client = info->client; +int ret; + +/* Writing a REFRESH or a REFRESH_V command. */ + ret =i2c_smbus_write_byte (client, refresh_addr);
drivers/iio/adc/pac1944.c line 703
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (ret) { +dev_err (&client->dev,"%s - cannot send Refresh cmd (0x%02X) to PAC1944\n" , + __func__, refresh_addr); +return ret; + } + +/* Register data retrieval timestamp. */ + info->chip_reg_data.jiffies_tstamp = jiffies; +/* Wait till the data is available. */ +usleep_range (wait_time, wait_time +100 );
fsleep()
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+ +return 0 ; +} + +static int pac1944_reg_snapshot (struct pac1944_chip_info *info,bool do_refresh, +u8 refresh_addr,u32 wait_time) +{
...
Quoted 41 lines
drivers/iio/adc/pac1944.c new file +41
-0+/* +* Check if the channel is active (within the data read from the chip), +* skip all fields if disabled. +*/ +for_each_set_bit (cnt, &info->active_channels_mask, info->phys_channels) { +/* skip if the energy accumulation is disabled */ +if (!info->enable_acc[cnt]) { + offset_reg_data_p +=PAC1944_VACC_REG_LEN ; +continue ; + } + + stored_value = info->chip_reg_data.acc_val[cnt]; + + info->chip_reg_data.vacc[cnt] =pac1944_get_unaligned_be56 (offset_reg_data_p); + + is_unipolar = true; +switch (info->chip_reg_data.accumulation_mode[cnt]) { +case PAC1944_ACCMODE_VPOWER : +if (info->chip_reg_data.vbus_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG || + info->chip_reg_data.vsense_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG ) + is_unipolar = false; +break ; +case PAC1944_ACCMODE_VSENSE : +if (info->chip_reg_data.vsense_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG ) + is_unipolar = false; +break ; +default : +return -EINVAL ; + } + +if (!is_unipolar) + info->chip_reg_data.vacc[cnt] = +sign_extend64 (info->chip_reg_data.vacc[cnt],55 ); + +/* +* Integrate the accumulated power or current over the elapsed +* interval. +*/ + smpl_mode =FIELD_GET (PAC1944_CTRL_SAMPLE_MASK , info->chip_reg_data.ctrl_lat_reg); + +if (smpl_mode <=PAC1944_SAMP_8SPS ) {
drivers/iio/adc/pac1944.c line 813
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ tmp_s64 = info->chip_reg_data.vacc[cnt]; +/* +* Find how much shift is required by the sample rate. +* The chip's sampling rate is 2^shift samples/sec. +*/ + shift = shift_map_tbl[smpl_mode]; + inc = tmp_s64 >> shift; + }else { +dev_err (dev,"Invalid sample rate index: %d!\n" , smpl_mode); +return -EINVAL ;
Flip condition to handle error first. Then indent reduces on the rest.
drivers/iio/adc/pac1944.c line 822
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+ } + +if (check_add_overflow (stored_value, inc, &stored_value)) { +if (stored_value <0 ) + info->chip_reg_data.acc_val[cnt] =S64_MIN ; +else + info->chip_reg_data.acc_val[cnt] =S64_MAX ; + +dev_err (dev,"Overflow detected on channel [%d]!\n" , cnt +1 );
Why carry on in this case? Perhaps add a comment.
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+ }else { + info->chip_reg_data.acc_val[cnt] = stored_value; + } + + offset_reg_data_p +=PAC1944_VACC_REG_LEN ; + }
...
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +17
-0+} +static ssize_t pac1944_in_power_acc_raw_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +s64 curr_energy, int_part; +int ret; +int rem; + + ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + +/*
drivers/iio/adc/pac1944.c line 934
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* Expresses the 64 bit energy value as a +* 64 bit integer and a 32 bit nano value +*/ + curr_energy = info->chip_reg_data.acc_val[this_attr->address]; + int_part =div_s64_rem (curr_energy,1000000000 , &rem); + +if (rem <0 ) +return sysfs_emit (buf,"-%lld.%09u\n" ,abs (int_part), -rem); +else +return sysfs_emit (buf,"%lld.%09u\n" , int_part,abs (rem));
It's coming ultimately from a single 64 bit value? Can we use a different scale if it's a modified channel and hence use the IIO_VAL_INT64 and standard read_raw stuff?
Quoted 36 lines
drivers/iio/adc/pac1944.c new file +35
-0+} + +static ssize_t pac1944_in_power_acc_scale_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +unsigned int shunt, rem; +u64 tmp, ref; + +if (info->is_pac195x_family) + ref =PAC195X_MAX_VPOWER_RSHIFTED_BY_29B ; +else + ref =PAC194X_MAX_VPOWER_RSHIFTED_BY_29B ; + +if ((info->chip_reg_data.vbus_mode[this_attr->address] ==PAC1944_UNIPOLAR_FSR_CFG && + info->chip_reg_data.vsense_mode[this_attr->address] ==PAC1944_UNIPOLAR_FSR_CFG ) || + info->chip_reg_data.vbus_mode[this_attr->address] ==PAC1944_BIPOLAR_HALF_FSR_CFG || + info->chip_reg_data.vsense_mode[this_attr->address] ==PAC1944_BIPOLAR_HALF_FSR_CFG ) + ref = ref >>1 ; + + shunt = info->shunts[this_attr->address]; + + tmp =div_u64 (ref *1000000000LL , shunt); + rem =do_div (tmp,1000000000LL ); + +return sysfs_emit (buf,"%lld.%09u\n" , tmp, rem); +} + +static ssize_t pac1944_in_current_acc_raw_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{
drivers/iio/adc/pac1944.c line 1013
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +int ret; + + ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + +return sysfs_emit (buf,"%lld\n" , info->chip_reg_data.acc_val[this_attr->address]);
We do have 64 bit types for standard channels so maybe this works fine.
Quoted 29 lines
drivers/iio/adc/pac1944.c new file +29
-0+} + +static ssize_t pac1944_in_current_acc_scale_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +u64 tmp_u64, ref; +int shunt, rem; + +/* +* Currents - scale for mA - depends on the channel's shunt value +* (100mV * 1000000) / (2^16 * shunt(uOhm)) +*/ + ref =PAC1944_MAX_VSENSE_NANO ; + +switch (info->chip_reg_data.vsense_mode[this_attr->address]) { +case PAC1944_UNIPOLAR_FSR_CFG : +case PAC1944_BIPOLAR_HALF_FSR_CFG : + shunt = info->shunts[this_attr->address]; +break ; +case PAC1944_BIPOLAR_FSR_CFG : + ref = ref <<1 ; + shunt = info->shunts[this_attr->address]; +break ; +default : +return -EINVAL ;
drivers/iio/adc/pac1944.c line 1052
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ } + +/* +* Increasing precision +* (100mV * 1000000 * 1000000000) / 2^16 ) +*/ + tmp_u64 =div_u64 (ref, shunt); + rem =do_div (tmp_u64,1000000000LL ); + +return sysfs_emit (buf,"%lld.%09u\n" , tmp_u64, rem);
How bit does tmp_u64 really get? Can we represent it as a normal channel attribute - IIO_VAL_INT_PLUS_NANO?
this matters for discussion below about using standard channel info mask elements for these.
If we can't then fair enough that we have to do something like this but an ext_info would be preferable so the naming etc is constructed.
Quoted 31 lines
drivers/iio/adc/pac1944.c new file +31
-0+} + +static IIO_DEVICE_ATTR(in_energy1_raw, 0444, pac1944_in_power_acc_raw_show, NULL, 0); +static IIO_DEVICE_ATTR(in_energy2_raw, 0444, pac1944_in_power_acc_raw_show, NULL, 1); +static IIO_DEVICE_ATTR(in_energy3_raw, 0444, pac1944_in_power_acc_raw_show, NULL, 2); +static IIO_DEVICE_ATTR(in_energy4_raw, 0444, pac1944_in_power_acc_raw_show, NULL, 3); + +static IIO_DEVICE_ATTR(in_energy1_scale, 0444, pac1944_in_power_acc_scale_show, NULL, 0); +static IIO_DEVICE_ATTR(in_energy2_scale, 0444, pac1944_in_power_acc_scale_show, NULL, 1); +static IIO_DEVICE_ATTR(in_energy3_scale, 0444, pac1944_in_power_acc_scale_show, NULL, 2); +static IIO_DEVICE_ATTR(in_energy4_scale, 0444, pac1944_in_power_acc_scale_show, NULL, 3); + +static IIO_DEVICE_ATTR(in_energy1_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 0); +static IIO_DEVICE_ATTR(in_energy2_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 1); +static IIO_DEVICE_ATTR(in_energy3_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 2); +static IIO_DEVICE_ATTR(in_energy4_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 3); + +static IIO_DEVICE_ATTR(in_current_acc1_raw, 0444, pac1944_in_current_acc_raw_show, NULL, 0); +static IIO_DEVICE_ATTR(in_current_acc2_raw, 0444, pac1944_in_current_acc_raw_show, NULL, 1); +static IIO_DEVICE_ATTR(in_current_acc3_raw, 0444, pac1944_in_current_acc_raw_show, NULL, 2); +static IIO_DEVICE_ATTR(in_current_acc4_raw, 0444, pac1944_in_current_acc_raw_show, NULL, 3); + +static IIO_DEVICE_ATTR(in_current_acc1_scale, 0444, pac1944_in_current_acc_scale_show, NULL, 0); +static IIO_DEVICE_ATTR(in_current_acc2_scale, 0444, pac1944_in_current_acc_scale_show, NULL, 1); +static IIO_DEVICE_ATTR(in_current_acc3_scale, 0444, pac1944_in_current_acc_scale_show, NULL, 2); +static IIO_DEVICE_ATTR(in_current_acc4_scale, 0444, pac1944_in_current_acc_scale_show, NULL, 3); +
drivers/iio/adc/pac1944.c line 1093
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+static IIO_DEVICE_ATTR(in_current_acc1_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 0); +static IIO_DEVICE_ATTR(in_current_acc2_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 1); +static IIO_DEVICE_ATTR(in_current_acc3_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 2); +static IIO_DEVICE_ATTR(in_current_acc4_en, 0644, pac1944_in_enable_acc_show, + pac1944_in_enable_acc_store, 3); + +static struct attribute *pac1944_power_acc_attr[] = {
These are all standard ABI elements I think. I'd much prefer if we worked out how to build the channels rather than doing it as custom attributes like this.
The acc modifier suggestion below might help with that.
They might be beyond represenable scale though. In which case ignore this but scatter some more comments on that about so I remember on v5.
Quoted 42 lines
drivers/iio/adc/pac1944.c new file +42
-0+ &iio_dev_attr_in_energy1_raw.dev_attr.attr, + &iio_dev_attr_in_energy2_raw.dev_attr.attr, + &iio_dev_attr_in_energy3_raw.dev_attr.attr, + &iio_dev_attr_in_energy4_raw.dev_attr.attr, + &iio_dev_attr_in_energy1_scale.dev_attr.attr, + &iio_dev_attr_in_energy2_scale.dev_attr.attr, + &iio_dev_attr_in_energy3_scale.dev_attr.attr, + &iio_dev_attr_in_energy4_scale.dev_attr.attr, + &iio_dev_attr_in_energy1_en.dev_attr.attr, + &iio_dev_attr_in_energy2_en.dev_attr.attr, + &iio_dev_attr_in_energy3_en.dev_attr.attr, + &iio_dev_attr_in_energy4_en.dev_attr.attr, +NULL +}; + +static struct attribute *pac1944_current_acc_attr[] = { + &iio_dev_attr_in_current_acc1_raw.dev_attr.attr, + &iio_dev_attr_in_current_acc2_raw.dev_attr.attr, + &iio_dev_attr_in_current_acc3_raw.dev_attr.attr, + &iio_dev_attr_in_current_acc4_raw.dev_attr.attr, + &iio_dev_attr_in_current_acc1_scale.dev_attr.attr, + &iio_dev_attr_in_current_acc2_scale.dev_attr.attr, + &iio_dev_attr_in_current_acc3_scale.dev_attr.attr, + &iio_dev_attr_in_current_acc4_scale.dev_attr.attr, + &iio_dev_attr_in_current_acc1_en.dev_attr.attr, + &iio_dev_attr_in_current_acc2_en.dev_attr.attr, + &iio_dev_attr_in_current_acc3_en.dev_attr.attr, + &iio_dev_attr_in_current_acc4_en.dev_attr.attr, +NULL +}; + +static int pac1944_prep_custom_attributes (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +struct attribute **pac1944_custom_attrs, **tmp_attr; +struct i2c_client *client = info->client; +struct attribute_group *pac1944_group; +int active_channels_count =0 ; +int custom_attr_cnt; +int ch, i, j; + + active_channels_count = info->num_enabled_channels;
drivers/iio/adc/pac1944.c line 1145
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + pac1944_group =devm_kzalloc (&client->dev,sizeof (*pac1944_group),GFP_KERNEL ); +if (!pac1944_group) +return -ENOMEM ; + +/* +* Attributes for channel X: +* - in_shunt_value_X +* - one of attributes: +* - in_power_accX_raw,in_power_accX_scale and in_power_accX_en
space after ,
Hmm. this does make me wonder, should we just have a modifier for acc? That would make these separate channels but that should work out ok.
acc is a maths modifier like SUM_SQUARED_* ones. Might make this simpler even though it is a little bit of an obscure way to use modifiers.
Quoted 72 lines
drivers/iio/adc/pac1944.c new file +72
-0+* - in_current_accX_raw, in_current_accX_scale and in_current_accX_en +*/ + custom_attr_cnt =PAC1944_COMMON_DEVATTR * active_channels_count; + custom_attr_cnt +=PAC1944_ACC_DEVATTR * active_channels_count; + + pac1944_custom_attrs =devm_kcalloc (&client->dev, custom_attr_cnt +1 , +sizeof (*pac1944_custom_attrs),GFP_KERNEL ); +if (!pac1944_custom_attrs) +return -ENOMEM ; + + j =0 ; + +for_each_set_bit (ch, &info->active_channels_mask, info->phys_channels) { +for (i =0 ; i <PAC1944_COMMON_DEVATTR ; i++) + pac1944_custom_attrs[j++] = + pac1944_all_attrs[PAC1944_COMMON_DEVATTR * ch + i]; + +switch (info->chip_reg_data.accumulation_mode[ch]) { +case PAC1944_ACCMODE_VPOWER : + tmp_attr = pac1944_power_acc_attr; +break ; +case PAC1944_ACCMODE_VSENSE : + tmp_attr = pac1944_current_acc_attr; +break ; +default : +return -EINVAL ; + } + + pac1944_custom_attrs[j++] = tmp_attr[ch]; + pac1944_custom_attrs[j++] = tmp_attr[PAC1944_MAX_CH + ch]; + pac1944_custom_attrs[j++] = tmp_attr[2 *PAC1944_MAX_CH + ch]; + } + + pac1944_group->attrs = pac1944_custom_attrs; + info->iio_info.attrs = pac1944_group; + +return 0 ; +} + +static int pac1944_frequency_set (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +unsigned int mode) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client; +u16 tmp_u16; +int ret; + + ret =i2c_smbus_read_word_swapped (client,PAC1944_CTRL_ACT_REG_ADDR ); +if (ret <0 ) { +dev_err (&client->dev,"cannot read PAC1944 regs from 0x%02X\n" , +PAC1944_CTRL_ACT_REG_ADDR ); +return ret; + } + + tmp_u16 = ret; + tmp_u16 &= ~PAC1944_CTRL_SAMPLE_MASK ; + tmp_u16 |=FIELD_PREP (PAC1944_CTRL_SAMPLE_MASK , mode); + + scoped_guard(mutex, &info->lock) { + ret =i2c_smbus_write_word_swapped (client,PAC1944_CTRL_REG_ADDR , tmp_u16); +if (ret <0 ) { +dev_err (&client->dev,"Failed to configure sampling mode\n" ); +return ret; + } + + info->sampling_mode = mode; + info->chip_reg_data.ctrl_act_reg = tmp_u16; + } + +return pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +}
drivers/iio/adc/pac1944.c line 1227
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +static int pac1944_frequency_get (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); + +return info->sampling_mode; +} + +static const struct iio_enum sampling_mode_enum = {
Add a comment somewhere here on why we need these rather than using standard handling via info_mask and read raw.
Quoted 65 lines
drivers/iio/adc/pac1944.c new file +64
-0+ .items = pac1944_frequency_avail, + .num_items = ARRAY_SIZE(pac1944_frequency_avail), + .set = pac1944_frequency_set, + .get = pac1944_frequency_get, +}; + +static const struct iio_chan_spec_ext_info pac1944_ext_info[] = { +IIO_ENUM ("sampling_frequency" ,IIO_SHARED_BY_ALL , &sampling_mode_enum), + { + .name ="sampling_frequency_available" , + .shared =IIO_SHARED_BY_ALL , + .read = iio_enum_available_read, + .private = (uintptr_t )&sampling_mode_enum, + }, + { } +}; +static int pac1944_read_label (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,char *label) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +int idx; + +/* +* In the datasheet channels are referred to as 1 to 4. For AVG the index +* should be between 5 to 8. To calculate PAC1944_CH_VOLTAGE_AVERAGE and +* PAC1944_CH_CURRENT_AVERAGE real index, we need to remove the added +* offset (PAC1944_MAX_CH). +*/ + idx = (chan->channel -1 ) %PAC1944_MAX_CH ; + +switch (chan->address) { +case PAC1944_VBUS_1_ADDR : +case PAC1944_VBUS_2_ADDR : +case PAC1944_VBUS_3_ADDR : +case PAC1944_VBUS_4_ADDR : +if (info->labels[idx]) +return sysfs_emit (label,"%s_VBUS_%d\n" , info->labels[idx], idx +1 ); + +return sysfs_emit (label,"VBUS_%d\n" , idx +1 ); +case PAC1944_VBUS_AVG_1_ADDR : +case PAC1944_VBUS_AVG_2_ADDR : +case PAC1944_VBUS_AVG_3_ADDR : +case PAC1944_VBUS_AVG_4_ADDR : +if (info->labels[idx]) +return sysfs_emit (label,"%s_VBUS_AVG_%d\n" , info->labels[idx], idx +1 ); + +return sysfs_emit (label,"VBUS_AVG_%d\n" , idx +1 ); +case PAC1944_VSENSE_1_ADDR : +case PAC1944_VSENSE_2_ADDR : +case PAC1944_VSENSE_3_ADDR : +case PAC1944_VSENSE_4_ADDR : +if (info->labels[idx]) +return sysfs_emit (label,"%s_IBUS_%d\n" , info->labels[idx], idx +1 ); + +return sysfs_emit (label,"IBUS_%d\n" , idx +1 ); +case PAC1944_VSENSE_AVG_1_ADDR : +case PAC1944_VSENSE_AVG_2_ADDR : +case PAC1944_VSENSE_AVG_3_ADDR : +case PAC1944_VSENSE_AVG_4_ADDR : +if (info->labels[idx]) +return sysfs_emit (label,"%s_IBUS_AVG_%d\n" , info->labels[idx], idx +1 ); + +return sysfs_emit (label,"IBUS_AVG_%d\n" , idx +1 ); +case PAC1944_VPOWER_1_ADDR :
drivers/iio/adc/pac1944.c line 1434
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+case PAC1944_VPOWER_2_ADDR : +case PAC1944_VPOWER_3_ADDR : +case PAC1944_VPOWER_4_ADDR : +if (info->labels[idx]) +return sysfs_emit (label,"%s_POWER_%d\n" , info->labels[idx], idx +1 ); + +return sysfs_emit (label,"POWER_%d\n" , idx +1 ); + } + +return 0 ;
Add a comment on when we get here. If it's only on error return an error code. End of day so I'm not checking ;)
Quoted 113 lines
drivers/iio/adc/pac1944.c new file +112
-0+} + +/* +* Universal Unique Identifier (UUID), +* 721F1534-5D27-4B60-9DF4-41A3C4B7DA3A, +* is reserved to Microchip for the PAC194x and PAC195x. +*/ +#define PAC1944_DSM_UUID "721F1534-5D27-4B60-9DF4-41A3C4B7DA3A" + +/* +* documentation related to the ACPI device definition +* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/UserGuides/PAC194X_5X-UEFI-BIOS-Integration-and-Microsoft-Windows-10-and-Windows-11-Energy-Meter-Interface-Device-Driver-Users-Guide-DS50003155.pdf +*/ +static int pac1944_acpi_parse_channel_config (struct i2c_client *client, +struct pac1944_chip_info *info) +{ +struct device *dev = &client->dev; +const struct acpi_device_id *id; +unsigned short bi_dir_mask; +union acpi_object *rez; +acpi_handle handle; +guid_t guid; +int i; + + handle =ACPI_HANDLE (dev); + + id =acpi_match_device (dev->driver->acpi_match_table, dev); +if (!id) +return -ENODEV ; + +guid_parse (PAC1944_DSM_UUID , &guid); + + rez =acpi_evaluate_dsm (handle, &guid,0 ,PAC1944_ACPI_GET_NAMES ,NULL ); +if (!rez) +return -EINVAL ; + +if (rez->package.count >PAC1944_MAX_CH ) { +ACPI_FREE (rez); +return -EINVAL ; + } + +for (i =0 ; i < rez->package.count; i++) { +if (rez->package.elements[i].type !=ACPI_TYPE_STRING ) { +ACPI_FREE (rez); +return -EINVAL ; + } + + info->labels[i] =devm_kmemdup (dev, rez->package.elements[i].string.pointer, + (size_t )rez->package.elements[i].string.length +1 , +GFP_KERNEL ); +if (!info->labels[i]) { +ACPI_FREE (rez); +return -ENOMEM ; + } + + info->labels[i][rez->package.elements[i].string.length] ='\0' ; + } + +ACPI_FREE (rez); + + rez =acpi_evaluate_dsm (handle, &guid,1 ,PAC1944_ACPI_GET_UOHMS_VALS ,NULL ); +if (!rez) +return -EINVAL ; + +if (rez->package.count >PAC1944_MAX_CH ) { +ACPI_FREE (rez); +return -EINVAL ; + } + +for (i =0 ; i < rez->package.count; i++) { +if (rez->package.elements[i].type !=ACPI_TYPE_INTEGER ) { +ACPI_FREE (rez); +return -EINVAL ; + } + + info->shunts[i] = rez->package.elements[i].integer.value; +if (info->shunts[i]) +set_bit (i, &info->active_channels_mask); + } + +ACPI_FREE (rez); + + rez =acpi_evaluate_dsm (handle, &guid,1 ,PAC1944_ACPI_GET_BIPOLAR_SETTINGS ,NULL ); +if (!rez) +return -EINVAL ; + +if (rez->package.count !=2 *PAC1944_MAX_CH ) { +ACPI_FREE (rez); +return -EINVAL ; + } + +for_each_set_bit (i, &info->active_channels_mask, info->phys_channels) { +if (rez->package.elements[i].type !=ACPI_TYPE_INTEGER ) { +ACPI_FREE (rez); +return -EINVAL ; + } + + bi_dir_mask = rez->package.elements[i].integer.value; + +if (bi_dir_mask ==PAC1944_UNIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (dev,"VBUS{%d} mode set to: %d\n" , i, bi_dir_mask); + info->chip_reg_data.vbus_mode[i] = bi_dir_mask; + }else { +return dev_err_probe (dev, -EINVAL ,"invalid vbus-mode value on %i\n" , i); + } + +if (rez->package.elements[i +PAC1944_MAX_CH ].type !=ACPI_TYPE_INTEGER ) { +ACPI_FREE (rez); +return -EINVAL ; + }
drivers/iio/adc/pac1944.c line 1570
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + bi_dir_mask = rez->package.elements[i +PAC1944_MAX_CH ].integer.value; + +if (bi_dir_mask ==PAC1944_UNIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (dev,"VSENSE{%d} mode set to: %d\n" , i, bi_dir_mask); + info->chip_reg_data.vsense_mode[i] = bi_dir_mask; + }else { +return dev_err_probe (dev, -EINVAL ,"invalid vsense-mode value on %i\n" , i);
Probably flip condition to error out first and reduce indent on the others. Same applies in other cases above. Or use a switch so the error is in the default.
drivers/iio/adc/pac1944.c line 1583
Quoted 13 lines
drivers/iio/adc/pac1944.c new file +13
-0+ } + } + +ACPI_FREE (rez); + +return 0 ; +} + +static int pac1944_check_range (s32 *vals,const int ranges[][2 ],int num_ranges) +{ +int i; + +for (i =0 ; i < num_ranges; i++) {
for (unsigned int i = 0; ...
Quoted 38 lines
drivers/iio/adc/pac1944.c new file +38
-0+if (vals[0 ] == ranges[i][0 ] && vals[1 ] == ranges[i][1 ]) +return i; + } + +return -EINVAL ; +} + +static int pac1944_setup_adc_vbus_range (const struct fwnode_handle *child, +struct pac1944_chip_info *info,int idx) +{ +const char *prop_name ="microchip,vbus-input-range-microvolt" ; +struct i2c_client *client = info->client; +struct device *dev = &client->dev; +bool is_bipolar, is_half_fsr; +unsigned int tbl_len; +s32 vals[2 ]; +int ret; + +/* default value is unipolar and Full Scale Range */ + is_bipolar = false; + is_half_fsr = false; + + ret =fwnode_property_read_u32_array (child, prop_name, vals,2 ); +if (!ret) { +if (info->is_pac195x_family) { + tbl_len =ARRAY_SIZE (pac1954_adc_range_tbl); + ret =pac1944_check_range (vals, pac1954_adc_range_tbl, tbl_len); + }else { + tbl_len =ARRAY_SIZE (pac1944_adc_range_tbl); + ret =pac1944_check_range (vals, pac1944_adc_range_tbl, tbl_len); + } +if (ret <0 ) { +return dev_err_probe (dev, -EINVAL , +"Invalid value %d, %d for prop %s\n" , + vals[0 ], vals[1 ], prop_name); + } + +if (ret !=PAC1944_ADC_FULL_RANGE_UNIPOLAR )
drivers/iio/adc/pac1944.c line 1631
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ is_bipolar = true; +if (ret ==PAC1944_ADC_HALF_RANGE_BIPOLAR ) + is_half_fsr = true; + } + + info->chip_reg_data.vbus_mode[idx] =PAC1944_UNIPOLAR_FSR_CFG ; +if (is_half_fsr) + info->chip_reg_data.vbus_mode[idx] =PAC1944_BIPOLAR_HALF_FSR_CFG ; +else if (is_bipolar) + info->chip_reg_data.vbus_mode[idx] =PAC1944_BIPOLAR_FSR_CFG ;
Similar to below for this function most if not all comments apply (note I review upwards)
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +return 0 ; +} + +static int pac1944_setup_adc_vsense_range (const struct fwnode_handle *child, +struct pac1944_chip_info *info,int idx) +{ +const char *prop_name ="microchip,vsense-input-range-microvolt" ; +struct i2c_client *client = info->client; +struct device *dev = &client->dev;
drivers/iio/adc/pac1944.c line 1651
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+bool is_bipolar, is_half_fsr; +unsigned int tbl_len; +s32 vals[2 ]; +int ret; + +/* default value is unipolar and Full Scale Range */ + is_bipolar = false; + is_half_fsr = false; + + ret =fwnode_property_read_u32_array (child, prop_name, vals,2 );
ARRAY_SIZE(vals)
drivers/iio/adc/pac1944.c line 1654
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+if (!ret) { + tbl_len =ARRAY_SIZE (pac1944_adc_vsense_range_tbl); + ret =pac1944_check_range (vals, pac1944_adc_vsense_range_tbl, tbl_len);
Seems fine as:
ret = pac1944_check_range(vals, pac1944_adc_vsense_range_tbl, ARRAY_SIZE(pac1944_adc_vsense_range_tbl));
and if anything slightly more readable without the local variable.
drivers/iio/adc/pac1944.c line 1619
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+if (ret <0 ) { +return dev_err_probe (dev, -EINVAL , +"Invalid value %d, %d for prop %s\n" , + vals[0 ], vals[1 ], prop_name); + }
No brackets as single statement. The coding style is a little unclear on these but we tend to use that convention in IIO even if several lines due to breaks for long lines.
drivers/iio/adc/pac1944.c line 1671
Quoted 12 lines
drivers/iio/adc/pac1944.c new file +12
-0+ +if (ret !=PAC1944_ADC_FULL_RANGE_UNIPOLAR ) + is_bipolar = true; +if (ret ==PAC1944_ADC_HALF_RANGE_BIPOLAR ) + is_half_fsr = true; + } + + info->chip_reg_data.vsense_mode[idx] =PAC1944_UNIPOLAR_FSR_CFG ; +if (is_half_fsr) + info->chip_reg_data.vsense_mode[idx] =PAC1944_BIPOLAR_HALF_FSR_CFG ; +else if (is_bipolar) + info->chip_reg_data.vsense_mode[idx] =PAC1944_BIPOLAR_FSR_CFG ;
I'd put the default in an else - Makes it easier to spot that this is a choice between modes.
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+ +return 0 ; +} + +static int pac1944_of_parse_channel_config (struct i2c_client *client, +struct pac1944_chip_info *info) +{ +struct device *dev = &client->dev; +unsigned int current_channel; +int ret, acc_mode; +u32 idx;
drivers/iio/adc/pac1944.c line 1692
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + current_channel =1 ; + +device_for_each_child_node_scoped (dev, child) { + ret =fwnode_property_read_u32 (child,"reg" , &idx); +if (ret) +return dev_err_probe (dev, ret,"reading invalid channel index\n" ); + +if (current_channel > info->phys_channels || idx > info->phys_channels || idx ==0 ) +return dev_err_probe (dev, -EINVAL ,"invalid channel index %d value\n" , idx);
Perhaps split this and check current_channel on entering the loop - before reading the reg property.
Also I'd break the longest lines in here as they are getting rather unreadable.
drivers/iio/adc/pac1944.c line 1694
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +/* adjust idx to match channel index (1 to 4) from the datasheet */
This comment seems odd given you are adjusting it down - > I'd assume to 0..3
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +18
-0+ idx--; + +if (test_and_set_bit (idx, &info->active_channels_mask)) +return dev_err_probe (dev, -EINVAL ,"duplicate reg value %u\n" , idx +1 ); + + ret =fwnode_property_read_u32 (child,"shunt-resistor-micro-ohms" , + &info->shunts[idx]); +if (ret) +return dev_err_probe (dev, ret,"%s: invalid shunt-resistor value\n" , +fwnode_get_name (child)); + +fwnode_property_read_string (child,"label" , (const char **)&info->labels[idx]); + + ret =pac1944_setup_adc_vbus_range (child, info, idx); +if (ret) +return ret; + + ret =pac1944_setup_adc_vsense_range (child, info, idx);
drivers/iio/adc/pac1944.c line 1722
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (ret) +return ret; + + ret =fwnode_property_read_u32 (child,"microchip,accumulation-mode" , &acc_mode); +if (ret) +return dev_err_probe (dev, ret,"invalid accumulation-mode value on %s\n" , +fwnode_get_name (child)); +if (acc_mode ==PAC1944_ACCMODE_VPOWER || + acc_mode ==PAC1944_ACCMODE_VSENSE ) { +dev_dbg (dev,"Accumulation{%d} mode set to: %d\n" , idx, acc_mode);
This feels noisy - can we not see that from channels registered?
drivers/iio/adc/pac1944.c line 1727
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+ info->chip_reg_data.accumulation_mode[idx] = acc_mode; + }else { +return dev_err_probe (dev, -EINVAL , +"invalid mode for accumulator value on %s\n" , +fwnode_get_name (child));
If possible to do easily flip logic so only the bad path is out of line.
if (acc_mode != PAC... && acc_mode != ... return dev_err. dev_dbg...
Keeping to that form where possible makes the code slightly easier to review.
Quoted 20 lines
drivers/iio/adc/pac1944.c new file +19
-0+ } + current_channel++; + } + +return 0 ; +} + +static int pac1944_chip_identify (struct iio_dev *indio_dev,struct pac1944_chip_info *info) +{ +struct i2c_client *client = info->client; +u8 chip_rev[3 ]; +int ret; + + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_PID_REG_ADDR , +sizeof (chip_rev), + chip_rev); +if (ret <0 ) +return dev_err_probe (&client->dev, ret,"cannot read revision\n" ); +
drivers/iio/adc/pac1944.c line 1761
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+dev_info (&client->dev,"Chip revision: 0x%02X\n" , chip_rev[2 ]); + +if (chip_rev[0 ] >=PAC_PRODUCT_ID_1941 && chip_rev[0 ] <=PAC_PRODUCT_ID_1942_2 ) { + info->is_pac195x_family = false; + }else if (chip_rev[0 ] >=PAC_PRODUCT_ID_1951 && chip_rev[0 ] <=PAC_PRODUCT_ID_1952_2 ) { + info->is_pac195x_family = true; + }else { +dev_err (&client->dev, +"product ID (0x%02X, 0x%02X, 0x%02X) for this part doesn't match\n" , + chip_rev[0 ], chip_rev[1 ], chip_rev[2 ]);
Given you fallback to matching via fallbacks compat, this probably wants to not be a dev_err() dev_info() or dev_dbg(). Will look very ugly otherwise if we are using that path.
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+return -EINVAL ; + } + +switch (chip_rev[0 ]) {
...
drivers/iio/adc/pac1944.c line 1818
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+case PAC_PRODUCT_ID_1952_2 : + info->phys_channels = pac1952_2_chip_config.phys_channels; + indio_dev->name = pac1952_2_chip_config.name; +break ; +default : +return -EINVAL ; + } + +return 0 ;
I'd return rather than break above. Means we know we are done in each leg.
Quoted 70 lines
drivers/iio/adc/pac1944.c new file +70
-0+} + +static int pac1944_chip_configure (struct pac1944_chip_info *info) +{ +struct i2c_client *client = info->client; +u32 wait_time; +u16 cfg =0 ; +u8 acc_cfg; +int ret, i; + +/* +* Count how many channels are enabled and store this information within +* the driver data. +*/ + info->num_enabled_channels =hweight_long (info->active_channels_mask); + +/* get sampling rate from PAC */ + ret =i2c_smbus_read_word_swapped (client,PAC1944_CTRL_REG_ADDR ); +if (ret <0 ) +return dev_err_probe (&client->dev, ret,"cannot read CTRL reg\n" ); + + info->sampling_mode =FIELD_GET (PAC1944_CTRL_SAMPLE_MASK , ret); + +if (info->sampling_mode >PAC1944_SAMP_8SPS ) +return dev_err_probe (&client->dev, -EINVAL ,"Unsupported sample mode\n" ); + +/* +* The current/voltage can be measured unidirectional, bidirectional or half FSR +* no SLOW triggered REFRESH, clear POR +*/ +for_each_set_bit (i, &info->active_channels_mask,PAC1944_MAX_CH ) + cfg |= (info->chip_reg_data.vbus_mode[i] << (6 -2 * i)) | + info->chip_reg_data.vsense_mode[i] << (14 -2 * i); + + ret =i2c_smbus_write_word_swapped (client,PAC1944_NEG_PWR_FSR_REG_ADDR , cfg); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write NEG_PWR_FSR reg\n" ); + + ret =i2c_smbus_write_word_data (client,PAC1944_SLOW_REG_ADDR ,0 ); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write SLOW reg\n" ); + +/* Write the CHANNEL_N_OFF from CTRL REGISTER */ + cfg =FIELD_PREP (PAC1944_CTRL_SAMPLE_MASK , info->sampling_mode) | +FIELD_PREP (PAC1944_CTRL_GPIO_ALERT2_MASK ,0 ) | +FIELD_PREP (PAC1944_CTRL_SLOW_ALERT1_MASK ,0 ) | +FIELD_PREP (PAC1944_CTRL_CH_1_OFF_MASK , !test_bit (0 , &info->active_channels_mask)) | +FIELD_PREP (PAC1944_CTRL_CH_2_OFF_MASK , !test_bit (1 , &info->active_channels_mask)) | +FIELD_PREP (PAC1944_CTRL_CH_3_OFF_MASK , !test_bit (2 , &info->active_channels_mask)) | +FIELD_PREP (PAC1944_CTRL_CH_4_OFF_MASK , !test_bit (3 , &info->active_channels_mask)); + + ret =i2c_smbus_write_word_swapped (client,PAC1944_CTRL_REG_ADDR , cfg); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write CTRL reg\n" ); + + acc_cfg =ACCUM_REG (info->chip_reg_data.accumulation_mode[0 ], + info->chip_reg_data.accumulation_mode[1 ], + info->chip_reg_data.accumulation_mode[2 ], + info->chip_reg_data.accumulation_mode[3 ]); + + ret =i2c_smbus_write_byte_data (client,PAC1944_ACCUM_CFG_REG_ADDR , acc_cfg); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write ACCUM_CFG reg\n" ); + +/* +* Sending a REFRESH to the chip, so the new settings take place +* as well as resetting the accumulators +*/ + ret =i2c_smbus_write_byte (client,PAC1944_REFRESH_REG_ADDR ); +if (ret)
drivers/iio/adc/pac1944.c line 1898
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+return dev_err_probe (&client->dev, ret,"cannot write REFRESH reg\n" ); + +/* +* Get the current (in the chip) sampling speed and compute the +* required timeout based on its value the timeout is 1/sampling_speed +* wait the maximum amount of time to be on the safe side - the +* maximum wait time is for 8sps +*/ + wait_time = (1024 / pac1944_samp_rate_map_tbl[info->sampling_mode]) *1000 ; +usleep_range (wait_time, wait_time +100 );
fsleep() for this sort of fuzzy sleep at least this many usecs.
Quoted 37 lines
drivers/iio/adc/pac1944.c new file +36
-0+ +INIT_DELAYED_WORK (&info->work_chip_rfsh, pac1944_work_periodic_rfsh); +/* Setup the latest moment for reading the regs before saturation */ +mod_delayed_work (system_percpu_wq, &info->work_chip_rfsh, +msecs_to_jiffies (PAC1944_MAX_RFSH_LIMIT_MS )); + +return devm_add_action_or_reset (&client->dev, pac1944_cancel_delayed_work, + &info->work_chip_rfsh); +} +static int pac1944_prep_iio_channels (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +struct device *dev = &info->client->dev; +int channel_size, attribute_count, cnt; +struct iio_chan_spec *ch_sp; + +/* Finding out dynamically how many IIO channels we need */ + attribute_count =0 ; + channel_size =0 ; + +for_each_set_bit (cnt, &info->active_channels_mask, info->phys_channels) { +/* add the size of the properties of one chip physical channel */ + channel_size +=sizeof (pac1944_single_channel); +/* count how many enabled channels we have */ + attribute_count +=ARRAY_SIZE (pac1944_single_channel); +dev_dbg (dev,":%s: Channel %d active\n" , __func__, cnt +1 ); + } + + ch_sp =devm_kzalloc (dev, channel_size,GFP_KERNEL ); +if (!ch_sp) +return -ENOMEM ; + +/* +* Send the updated dynamic channel structure information towards IIO +* prepare the required field for IIO class registration +*/
drivers/iio/adc/pac1944.c line 1953
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ indio_dev->num_channels = attribute_count; + indio_dev->channels = ch_sp; + +/* Populate the dynamic channels and make all the adjustments */ +for_each_set_bit (cnt, &info->active_channels_mask, info->phys_channels) { +memcpy (ch_sp, pac1944_single_channel,sizeof (pac1944_single_channel)); +/* +* In the datasheet channels are referred to as 1 to 4, so we will +* adjust the channel to match channel index (1 to 4) from the +* datasheet.
Hmm. Ok I guess. We don't normally apply such an offset, relying datasheet_name
+ labels to indicate such a relationship. However it doesn't hurt in this case so I guess why not.
Quoted 75 lines
drivers/iio/adc/pac1944.c new file +75
-0+*/ + ch_sp[PAC1944_CH_POWER ].channel = cnt +1 ; + ch_sp[PAC1944_CH_POWER ].address = cnt +PAC1944_VPOWER_1_ADDR ; + ch_sp[PAC1944_CH_VOLTAGE ].channel = cnt +1 ; + ch_sp[PAC1944_CH_VOLTAGE ].address = cnt +PAC1944_VBUS_1_ADDR ; + ch_sp[PAC1944_CH_CURRENT ].channel = cnt +1 ; + ch_sp[PAC1944_CH_CURRENT ].address = cnt +PAC1944_VSENSE_1_ADDR ; +/* +* In order to be able to use labels for PAC1944_CH_VOLTAGE and +* PAC1944_CH_VOLTAGE_AVERAGE, respectively PAC1944_CH_CURRENT +* and PAC1944_CH_CURRENT_AVERAGE we need to use different channel numbers. +* We will add +5 (+1 to maximum PAC channels). +*/ + ch_sp[PAC1944_CH_VOLTAGE_AVERAGE ].channel = cnt +PAC1944_MAX_CH +1 ; + ch_sp[PAC1944_CH_VOLTAGE_AVERAGE ].address = cnt +PAC1944_VBUS_AVG_1_ADDR ; + ch_sp[PAC1944_CH_CURRENT_AVERAGE ].channel = cnt +PAC1944_MAX_CH +1 ; + ch_sp[PAC1944_CH_CURRENT_AVERAGE ].address = cnt +PAC1944_VSENSE_AVG_1_ADDR ; + +/* advance the pointer */ + ch_sp +=ARRAY_SIZE (pac1944_single_channel); + } + +return 0 ; +} + +static const struct iio_info pac1944_info = { + .read_raw = pac1944_read_raw, + .read_label = pac1944_read_label, +}; + +static int pac1944_probe (struct i2c_client *client) +{ +const struct pac1944_features *chip; +struct device *dev = &client->dev; +struct pac1944_chip_info *info; +struct iio_dev *indio_dev; +int cnt, ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*info)); +if (!indio_dev) +return -ENOMEM ; + + info =iio_priv (indio_dev); + info->client = client; + + ret =pac1944_chip_identify (indio_dev, info); +if (ret <0 ) { +dev_dbg (dev,"Failed to identify the device based on HW ID\n" ); +/* +* If failed to identify the hardware based on internal registers, +* try using fallback compatible in device tree to deal with +* some newer part number. +*/ + chip =i2c_get_match_data (client); +if (!chip) +return -EINVAL ; + + info->phys_channels = chip->phys_channels; + indio_dev->name = chip->name; + +switch (chip->prod_id) { +case PAC_PRODUCT_ID_1941 : +case PAC_PRODUCT_ID_1942 : +case PAC_PRODUCT_ID_1943 : +case PAC_PRODUCT_ID_1944 : +case PAC_PRODUCT_ID_1941_2 : +case PAC_PRODUCT_ID_1942_2 : + info->is_pac195x_family = false; +break ; +case PAC_PRODUCT_ID_1951 : +case PAC_PRODUCT_ID_1952 : +case PAC_PRODUCT_ID_1953 : +case PAC_PRODUCT_ID_1954 : +case PAC_PRODUCT_ID_1951_2 : +case PAC_PRODUCT_ID_1952_2 :
drivers/iio/adc/pac1944.c line 2038
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ info->is_pac195x_family = true; +break ; +default : +dev_err (&client->dev, +"Unknown product ID (0x%02X)\n" , chip->prod_id); +return -EINVAL ; + } + } + +for (cnt =0 ; cnt < info->phys_channels; cnt++) {
For modern kernel style we can do
for (unsigned int cnt = 0; cnt < info->phys_channels; cnt++)
and keep cnt's scope tightly defined.
drivers/iio/adc/pac1944.c line 2047
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+/* always start with accumulation channels enabled */ + info->enable_acc[cnt] = true; + } + +if (ACPI_HANDLE (dev)) + ret =pac1944_acpi_parse_channel_config (client, info); +else + ret =pac1944_of_parse_channel_config (client, info); +
Trivial: No blank line here to keep it tightly coupled with the stuff it's checking.
Quoted 12 lines
drivers/iio/adc/pac1944.c new file +12
-0+if (ret) +return dev_err_probe (dev, ret,"parameter parsing returned an error\n" ); + + ret =devm_mutex_init (dev, &info->lock); +if (ret <0 ) +return ret; + + ret =pac1944_chip_configure (info); +if (ret <0 ) +return ret; + + ret =pac1944_prep_iio_channels (info, indio_dev);
drivers/iio/adc/pac1944.c line 2069
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (ret <0 ) +return ret; + + info->iio_info = pac1944_info; + indio_dev->info = &info->iio_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + + ret =pac1944_prep_custom_attributes (info, indio_dev); +if (ret <0 ) +return dev_err_probe (dev, ret,"Can't configure custom attributes for device\n" );
Probably wrap the longest of these. They are way over 80 chars and it won't hurt readability. I don't mind a little over 80 but this is near enough 100.
Quoted 25 lines
drivers/iio/adc/pac1944.c new file +25
-0+ + ret =pac1944_reg_snapshot (info, true,PAC1944_REFRESH_REG_ADDR , +PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + + ret =devm_iio_device_register (dev, indio_dev); +if (ret <0 ) +return dev_err_probe (dev, ret,"Can't register IIO device\n" ); + +return 0 ; +} + +static const struct i2c_device_id pac1944_id[] = { + { .name ="pac1941" , .driver_data = (kernel_ulong_t)&pac1941_chip_config }, + { .name ="pac1941-2" , .driver_data = (kernel_ulong_t)&pac1941_2_chip_config }, + { .name ="pac1942" , .driver_data = (kernel_ulong_t)&pac1942_chip_config }, + { .name ="pac1942-2" , .driver_data = (kernel_ulong_t)&pac1942_2_chip_config }, + { .name ="pac1943" , .driver_data = (kernel_ulong_t)&pac1943_chip_config }, + { .name ="pac1944" , .driver_data = (kernel_ulong_t)&pac1944_chip_config }, + { .name ="pac1951" , .driver_data = (kernel_ulong_t)&pac1951_chip_config }, + { .name ="pac1951-2" , .driver_data = (kernel_ulong_t)&pac1951_2_chip_config }, + { .name ="pac1952" , .driver_data = (kernel_ulong_t)&pac1952_chip_config }, + { .name ="pac1952-2" , .driver_data = (kernel_ulong_t)&pac1952_2_chip_config }, + { .name ="pac1953" , .driver_data = (kernel_ulong_t)&pac1953_chip_config },
drivers/iio/adc/pac1944.c line 2104
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ { .name = "pac1954", .driver_data = (kernel_ulong_t)&pac1954_chip_config }, + { } +}; +MODULE_DEVICE_TABLE (i2c, pac1944_id); + +static const struct of_device_id pac1944_of_match[] = { + { + .compatible ="microchip,pac1941" , + .data = &pac1941_chip_config + },
Small thing but it's fine for kernel style to use
}, {when you have lots of structures like this. Nice to reduce the scrolling a little.
Or given these aren't any longer than the i2c_device_id entrees you can just do
{ .compatible = "microchip,pac1941", .data = &pac1941_chip_config }.etc.
Quoted 40 lines
drivers/iio/adc/pac1944.c new file +40
-0+ { + .compatible = "microchip,pac1941-2", + .data = &pac1941_2_chip_config + }, + { + .compatible = "microchip,pac1942", + .data = &pac1942_chip_config + }, + { + .compatible = "microchip,pac1942-2", + .data = &pac1942_2_chip_config + }, + { + .compatible = "microchip,pac1943", + .data = &pac1943_chip_config + }, + { + .compatible = "microchip,pac1944", + .data = &pac1944_chip_config + }, + { + .compatible = "microchip,pac1951", + .data = &pac1951_chip_config + }, + { + .compatible = "microchip,pac1951-2", + .data = &pac1951_2_chip_config + }, + { + .compatible = "microchip,pac1952", + .data = &pac1952_chip_config + }, + { + .compatible = "microchip,pac1952-2", + .data = &pac1952_2_chip_config + }, + { + .compatible = "microchip,pac1953", + .data = &pac1953_chip_config + },
drivers/iio/adc/pac1944.c line 2154
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ { + .compatible = "microchip,pac1954", + .data = &pac1954_chip_config + }, + { } +}; +MODULE_DEVICE_TABLE (of, pac1944_of_match); + +static const struct acpi_device_id pac1944_acpi_match[] = { + {"MCHP1940" , .driver_data = (kernel_ulong_t)&pac1944_chip_config },
For consistency .id =
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +18
-0+ { } +}; +MODULE_DEVICE_TABLE (acpi, pac1944_acpi_match); + +static struct i2c_driver pac1944_driver = { + .driver = { + .name ="pac1944" , + .of_match_table = pac1944_of_match, + .acpi_match_table = pac1944_acpi_match + }, + .probe = pac1944_probe, + .id_table = pac1944_id, +}; +module_i2c_driver (pac1944_driver); + +MODULE_AUTHOR ("Marius Cristea <[email protected]>" ); +MODULE_DESCRIPTION ("Microchip PAC194X and PAC195X Power Monitor" ); +MODULE_LICENSE ("GPL" );
David Lechner
reviewerQuoted 8 lines · On 5/15/26 4:29 AM, [email protected] wrote:
From: Marius Cristea <[email protected]>
Add support for Microchip PAC194X and PAC195X series of Power Monitor with Accumulator chip family. This driver covers the following part numbers:
- PAC1941, PAC1941-2, PAC1942, PAC1942-2, PAC1943, PAC1944
- PAC1951, PAC1951-2, PAC1952, PAC1952-2, PAC1953, PAC1954
Quoted 8 lines
The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X-1/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
I assume we already had the hwmon vs. iio conversation? I would expect something in the cover letter justifying why this should be an iio device rather than hwmon since it looks like it was designed for monitoring e.g. CPU voltage.
Quoted 6 lines · On Fri, May 15, 2026 at 12:29:44PM +0300, [email protected] wrote:
Add support for Microchip PAC194X and PAC195X series of Power Monitor with Accumulator chip family. This driver covers the following part numbers:
- PAC1941, PAC1941-2, PAC1942, PAC1942-2, PAC1943, PAC1944
- PAC1951, PAC1951-2, PAC1952, PAC1952-2, PAC1953, PAC1954
Quoted 7 lines
The PAC194X family supports 9V Full-Scale Range and the PAC195X supports 32V Full-Scale Range.
There are two versions of the PAC194X/5X: the PAC194X-1/5X-1 devices are for high-side current sensing and the PAC194X/5X-2 devices are for low-side current sensing or floating VBUS applications. The PAC194X/5X-1 is named shortly PAC194X/5X.
It still has many subtle issues...
I am not able to review in a couple of weeks, so I recommend to read mailing lists for the previous reviews against other big driver submissions and improve yours accordingly.
So far, on a very fast glance I have noticed:
- the IWYU principle is not being followed
- the Kconfig help text can be improved to list part numbers for better maintainability
Signature · 2 lines
With Best Regards, Andy Shevchenko