adding support for Microchip PAC194X Power Monitor
Subsystems (2)
v3 cover adding support for Microchip PAC194X Power Monitor
From: Marius Cristea <[email protected]>
Adding 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:
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: adding support for PAC194X
iio: adc: adding support for PAC194X
.../ABI/testing/sysfs-bus-iio-adc-pac1944 | 17 + .../bindings/iio/adc/microchip,pac1944.yaml | 204 ++ MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2841 +++++++++++++++++ 6 files changed, 3082 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml create mode 100644 drivers/iio/adc/pac1944.c
base-commit: 0c86e33819785fe50616b6ee3fb35c1e4be406d5
Signature · 1 line
2.48.1
PATCH v3 1/2 dt-bindings: iio: adc: adding support for PAC194X
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 | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +204 -0
@@ -0,0 +1,204 @@ +# 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 PAC1944 and PAC1954 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-1, 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-1, 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,pac19412 + - microchip,pac1942 + - microchip,pac19422 + - microchip,pac1943 + - microchip,pac1944 + - microchip,pac1951 + - microchip,pac19512 + - microchip,pac1952 + - microchip,pac19522 + - microchip,pac1953 + - microchip,pac1954 + + reg: + maxItems: 1 + + vdd-supply: true + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + maxItems: 2 + + interrupt-names: + description: + alert1 indicates a HIGH or LOW limit was exceeded. + alert2 indicates a THERM limit was exceeded. + items: + - const: alert1 + - const: alert2 + +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. + + microchip,vbus-half-range: + $ref: /schemas/types.yaml#/definitions/flag + description: | + In order to increase measurement resolution and keeping the same + number the of bits the device has a configurable VBUS full range scale + (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,vbus-bipolar: + $ref: /schemas/types.yaml#/definitions/flag + description: + If provided, the channel is to be used in bipolar mode. The + actual range is limited to about -200 mV due to the impact of the ESD + structures. + + microchip,vsense-half-range: + $ref: /schemas/types.yaml#/definitions/flag + description: | + In order to decrease the power dissipation on the shunt resistor and + in the same time to increase measurement resolution by keeping the + same number the of bits the device has a configurable VSENSE full + range 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 + + microchip,vsense-bipolar: + $ref: /schemas/types.yaml#/definitions/flag + description: + If provided, the channel is to be used in bipolar mode. + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The Hardware Accumulator may be used to accumulate VPOWER, VSENSE or + VBUS values for any channel. By setting the accumulator for a channel + to accumulate the VPOWER values gives a measure of accumulated power + into 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. For either VSENSE or + VBUS, many samples may be accumulated on chip and the result collected + by the host and divided by the accumulator counter count value to + yield an average value with a very long integration time to reduce + noise. This feature is also very useful for system calibration, + allowing many averages to be accumulated for fast averaging/noise + reduction. + 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, VSENSE or VBUS + <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE + <2> - Accumulator accumulates VBUS + maximum: 2 + default: 0 + + required: + - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells" + +additionalProperties: 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,vsense-half-range; + microchip,vsense-bipolar; + }; + + channel@3 { + reg = <0x3>; + shunt-resistor-micro-ohms = <75000>; + label = "MEM"; + microchip,vbus-half-range; + microchip,vbus-bipolar; + microchip,vsense-half-range; + }; + + channel@4 { + reg = <0x4>; + shunt-resistor-micro-ohms = <100000>; + label = "NET"; + microchip,vbus-bipolar; + }; + }; + }; + +...
Signature · 1 line
2.48.1
David Lechner
reviewerRe: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Quoted 66 lines · On 6/6/25 4:39 AM, [email protected] wrote:
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.
Signed-off-by Marius Cristea <[email protected]>--- .../bindings/iio/adc/microchip,pac1944.yaml | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +41
-0@@ -0,0 +1,204 @@ +# 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 PAC1944 and PAC1954 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-1, 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-1, 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,pac19412 + - microchip,pac1942 + - microchip,pac19422 + - microchip,pac1943 + - microchip,pac1944 + - microchip,pac1951 + - microchip,pac19512 + - microchip,pac1952 + - microchip,pac19522 + - microchip,pac1953 + - microchip,pac1954 + + reg: + maxItems: 1 + + vdd-supply: true +
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 51
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + maxItems: 2 + + interrupt-names:
Needs minItems: 1 if we want to allow a single named interrupt.
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 58
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +7
-0+ description: + alert1 indicates a HIGH or LOW limit was exceeded. + alert2 indicates a THERM limit was exceeded. + items: + - const: alert1 + - const: alert2 +
I am having deja vu. I just commented on an identical interrupts binding in a different series [1]. In this case though, alert1 and alert2 are the actual pin names, so that is fine. But each pin can be programmed to indicate lots of different things, so drop descriptions or change them to describe the pin, not an arbitrary function. I don't even see THERM in the datasheet, so I'm guessing that was just a copy/ paste from something else anyway.
[1]: https://lore.kernel.org/linux-iio/[email protected]/
---
Even if the driver doesn't use them (yet), we could consider adding gpio-controller and #gpio-cells properties since these chips have pins that can operate as GPIOs.
And we could add a powerdown-gpios property for the /PWRDN pin.
We want to try to make the bindings as complete as possible, if we can [2].
[2]: https://docs.kernel.org/devicetree/bindings/writing-bindings.html
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 60
Quoted 2 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +2
-0+patternProperties: + "^channel@[1-4]+$":
Drop the +. Only 1 to 4 are allowed, not 11, 111, etc.
Also, we could further restrict things based on the actual number of channels on a chip like this:
allOf:
- if:
properties:
compatible:
pattern: "^pac19[45]1"
then:
properties:
channel@1:
reg:
items:
maximum: 1
patternProperties:
^channel@[2-4]$": false
- if:
properties:
compatible:
pattern: "^pac19[45]2"
then:
patternProperties:
^channel@[1-2]$":
reg:
items:
maximum: 2
patternProperties:
^channel@[3-4]$": false
- if:
properties:
compatible:
pattern: "^pac19[45]3"
then:
patternProperties:
^channel@[1-3]$":
reg:
items:
maximum: 3
properties:
channel@4: falseQuoted 32 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +32
-0+ 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. + + microchip,vbus-half-range: + $ref: /schemas/types.yaml#/definitions/flag + description: | + In order to increase measurement resolution and keeping the same + number the of bits the device has a configurable VBUS full range scale + (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
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 102
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ PAC194X. The actual range is limited to about -200 mV due to the + impact of the ESD structures. + + microchip,vbus-bipolar: + $ref: /schemas/types.yaml#/definitions/flag + description: + If provided, the channel is to be used in bipolar mode. The + actual range is limited to about -200 mV due to the impact of the ESD + structures. +
Using Jonathan's suggestion from v2 to just have a single property with 3 different ranges to chose from seems simpler that this. It would only require one property and would be self-documenting. The description could be shortened to just a couple of lines.
Otherwise, we also need to add:
- if:
required:
microchip,vbus-half-range
then:
required:
microchip,vbus-bipolar
to validate that that there are only 3 possibilities.
Also, swapping the word order to range-half would be more consistent with the existing adi,range-double property that serves a similar purpose.
Same applies to vsense.
Quoted 26 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +26
-0+ microchip,vsense-half-range: + $ref: /schemas/types.yaml#/definitions/flag + description: | + In order to decrease the power dissipation on the shunt resistor and + in the same time to increase measurement resolution by keeping the + same number the of bits the device has a configurable VSENSE full + range 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 + + microchip,vsense-bipolar: + $ref: /schemas/types.yaml#/definitions/flag + description: + If provided, the channel is to be used in bipolar mode. + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The Hardware Accumulator may be used to accumulate VPOWER, VSENSE or + VBUS values for any channel. By setting the accumulator for a channel + to accumulate the VPOWER values gives a measure of accumulated power + into a time period, which is equivalent to energy. Setting the + accumulator for a channel to accumulate VSENSE values gives a measure
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ of accumulated current, which is equivalent to charge. This allows the + accumulator to be used as a coulomb counter. For either VSENSE or + VBUS, many samples may be accumulated on chip and the result collected + by the host and divided by the accumulator counter count value to + yield an average value with a very long integration time to reduce + noise. This feature is also very useful for system calibration, + allowing many averages to be accumulated for fast averaging/noise + reduction. + This functionality needs to be setup once and must not be changed + during the runtime,
Why not? The datasheet says there is a REFRESH command to allow changing it at runtime.
Quoted 37 lines
just in case the user wants to measure the charge
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +36
-0+ 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, VSENSE or VBUS + <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE + <2> - Accumulator accumulates VBUS + maximum: 2 + default: 0 + + required: + - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells" + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@10 { + compatible = "microchip,pac1954"; + reg = <0x10>; + vdd-supply = <&vdd>; +
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 184
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ #address-cells = <1>; + #size-cells = <0>; + + channel@1 { + reg = <0x1>; + shunt-resistor-micro-ohms = <24900>; + label = "CPU"; + microchip,vsense-half-range; + microchip,vsense-bipolar; + };
Seems odd to leave a channel unconfigured since the shunt resistor value is required and there is a 3 channel version of the chip that could be used if only 3 channels were wired up.
Quoted 20 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +20
-0+ + channel@3 { + reg = <0x3>; + shunt-resistor-micro-ohms = <75000>; + label = "MEM"; + microchip,vbus-half-range; + microchip,vbus-bipolar; + microchip,vsense-half-range; + }; + + channel@4 { + reg = <0x4>; + shunt-resistor-micro-ohms = <100000>; + label = "NET"; + microchip,vbus-bipolar; + }; + }; + }; + +...
Re: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Hi David,
Thank you for the feedback. Please see my comments below...
Quoted 85 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 81 lines · On 6/6/25 4:39 AM, [email protected] wrote:
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.
Signed-off-by Marius Cristea <[email protected]>---
.../bindings/iio/adc/microchip,pac1944.yaml | 204
++++++++++++++++++1 file changed, 204 insertions(+)create mode 100644
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamldiff --git
a/Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlb/Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlnew file mode 100644
index 000000000000..4a2cf6b64055
---/dev/null
+++b/Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +7
-0@@ -0,0 +1,204 @@ +# 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 PAC1944 and PAC1954 Power Monitors withAccumulator
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +6
-0+ +maintainers: + - Marius Cristea <[email protected]> + +description: | + This device is part of the Microchip family of Power Monitorswith
+ Accumulator. The datasheet for PAC1941-1, PAC1941-1, 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-1, 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.pdfDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +26
-0+ +properties: + compatible: + enum: + - microchip,pac1941 + - microchip,pac19412 + - microchip,pac1942 + - microchip,pac19422 + - microchip,pac1943 + - microchip,pac1944 + - microchip,pac1951 + - microchip,pac19512 + - microchip,pac1952 + - microchip,pac19522 + - microchip,pac1953 + - microchip,pac1954 + + reg: + maxItems: 1 + + vdd-supply: true + + "#address-cells": + const: 1 + + "#size-cells":
Quoted 9 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 51
Quoted 6 lines
+ const: 0
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +5
-0+ + interrupts: + maxItems: 2 + + interrupt-names:Needs minItems: 1 if we want to allow a single named interrupt.
the driver as it is right now it doesn't support any interrupt. I was thinking to add them here, just in case there will be a request to be added later.
Quoted 8 lines
Quoted 7 lines
+ description:
+ alert1 indicates a HIGH or LOW limit was exceeded. + alert2 indicates a THERM limit was exceeded. + items:
+ - const: alert1
+ - const: alert2
+
Quoted 9 lines
I am having deja vu. I just commented on an identical interrupts binding
in a different series [1]. In this case though, alert1 and alert2 are the actual pin names, so that is fine. But each pin can be programmed to indicate lots of different things, so drop descriptions or change them to describe the pin, not an arbitrary function. I don't even see THERM in the datasheet, so I'm guessing that was just a copy/ paste from something else anyway.
sorry here, I will fix it.
Quoted 126 lines
[1]:
https://lore.kernel.org/linux-iio/[email protected]/---
Even if the driver doesn't use them (yet), we could consider adding gpio-controller and #gpio-cells properties since these chips have pins
that can operate as GPIOs.And we could add a powerdown-gpios property for the /PWRDN pin.
We want to try to make the bindings as complete as possible, if we can [2].
[2]:
https://docs.kernel.org/devicetree/bindings/writing-bindings.htmlQuoted 2 lines
+patternProperties:
+ "^channel@[1-4]+$":Drop the +. Only 1 to 4 are allowed, not 11, 111, etc.
Also, we could further restrict things based on the actual number of channels on a chip like this:
allOf:
- if:
properties:
compatible:
pattern: "^pac19[45]1"
then:
properties:
channel@1:
reg:
items:
maximum: 1
patternProperties:
^channel@[2-4]$": false
- if:
properties:
compatible:
pattern: "^pac19[45]2"
then:
patternProperties:
^channel@[1-2]$":
reg:
items:
maximum: 2
patternProperties:
^channel@[3-4]$": false
- if:
properties:
compatible:
pattern: "^pac19[45]3"
then:
patternProperties:
^channel@[1-3]$":
reg:
items:
maximum: 3
properties:
channel@4: falseQuoted 50 lines
+ type: object
+ $ref:adc.yaml
+ description:
+ Represents the external channels which are connected to the ADC.Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ + properties: + reg: + items: + minimum: 1 + maximum: 4 + + shunt-resistor-micro-ohms: + description: + Value in micro Ohms of the shunt resistor connectedbetween
+ the SENSE+ and SENSE- inputs, across which the current is measured.
+ Value is needed to compute the scaling of the measured current.
+
+ microchip,vbus-half-range:
+ $ref: /schemas/types.yaml#/definitions/flag
+ description: |
+ In order to increase measurement resolution and keeping the same
+ number the of bits the device has a configurable VBUS full range scale
+ (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,vbus-bipolar:Quoted 8 lines
+ $ref: /schemas/types.yaml#/definitions/flag
+ description:
+ If provided, the channel is to be used in bipolar mode. The
+ actual range is limited to about -200 mV due to the impact of the ESD
+ structures.
+
Quoted 7 lines
Using Jonathan's suggestion from v2 to just have a single property with 3 different
ranges to chose from seems simpler that this. It would only require one property
and would be self-documenting. The description could be shortened to just a couple
of lines.
I was thinking to add the range for this property, but it looks (for me at least) more complicated from the checking point of view. The driver is supporting two family of devices that has, each, 3 different voltage range as an input.
Quoted 11 lines
Otherwise, we also need to add:
- if:
required:
microchip,vbus-half-range
then:
required:
microchip,vbus-bipolarto validate that that there are only 3 possibilities.
Those properties was just an alternative solution to the range. If we agree that this is the way forward, I will add the check.
Quoted 57 lines
Also, swapping the word order to range-half would be more consistent with
the existing adi,range-double property that serves a similar purpose.Same applies to vsense.
Quoted 50 lines
+ microchip,vsense-half-range:
+ $ref: /schemas/types.yaml#/definitions/flag
+ description: |
+ In order to decrease the power dissipation on the shunt resistor and
+ in the same time to increase measurement resolution by keeping the
+ same number the of bits the device has a configurable VSENSE full
+ range 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 FSRDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ + microchip,vsense-bipolar: + $ref: /schemas/types.yaml#/definitions/flag + description: + If provided, the channel is to be used in bipolar mode. + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The Hardware Accumulator may be used to accumulateVPOWER, VSENSE or
+ VBUS values for any channel. By setting the accumulator for a channel
+ to accumulate the VPOWER values gives a measure of accumulated power
+ into 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. For either VSENSE or
+ VBUS, many samples may be accumulated on chip and the result collected
+ by the host and divided by the accumulator counter count value to
+ yield an average value with a very long integration time to reduce
+ noise. This feature is also very useful for system calibration,
+ allowing many averages to be accumulated for fast averaging/noise
Quoted 8 lines
Quoted 4 lines
+ reduction.
+ This functionality needs to be setup once and must not be changed
+ during the runtime,Why not? The datasheet says there is a REFRESH command to allow changing it
at runtime.
Yes sure, from the data sheet point of view it is possible to change the accumulation mode during the runtime. From the customer point of view once the channel has a certain functionality the operation mode will not be changed. E.g. once a channel is used as a coulomb counter nobody will change this operation mode in order not to loose any charging or discharging current for that channel. Also being only one hardware register per channel it will be hard to have enabled/available into the sysfs all the functionality and to "guess" the one that is working at a point in time.
Quoted 46 lines
Quoted 46 lines
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, VSENSE or VBUS
+ <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE
+ <2> - Accumulator accumulates VBUS
+ maximum: 2
+ default: 0Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +33
-0+ + required: + - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - vdd-supply + - "#address-cells" + - "#size-cells" + +additionalProperties: 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>;
Quoted 8 lines
Quoted 4 lines
+ label = "CPU";
+ microchip,vsense-half-range;
+ microchip,vsense-bipolar;
+ };Seems odd to leave a channel unconfigured since the shunt resistor value is required and there is a 3 channel version of the chip that could be used if only 3 channels were wired up.
Yes, indeed. The reason I was given this example was because, sometimes the client will use a chip with more channels that are needed in order to develop some other functions later but on the same hardware. E.g. a board that could monitor a battery charge/discharge current, but the same hardware/board on a more expensive product could also support a second battery that has a second charging monitoring channel. The parts are pin compatible and sometime the clients will buy the part with more channels and use only part of those channels. In order to save some power each channel could be enabled or disabled.
Quoted 21 lines
Quoted 21 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +20
-0+ + channel@3 { + reg = <0x3>; + shunt-resistor-micro-ohms = <75000>; + label = "MEM"; + microchip,vbus-half-range; + microchip,vbus-bipolar; + microchip,vsense-half-range; + }; + + channel@4 { + reg = <0x4>; + shunt-resistor-micro-ohms = <100000>; + label = "NET"; + microchip,vbus-bipolar; + }; + }; + }; + +...
David Lechner
reviewerRe: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Quoted 4 lines · On 6/10/25 9:46 AM, [email protected] wrote:
Hi David,
Thank you for the feedback. Please see my comments below...
...
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
+ interrupts:
+ maxItems: 2
+
+ interrupt-names:Needs minItems: 1 if we want to allow a single named interrupt.
the driver as it is right now it doesn't support any interrupt. I was thinking to add them here, just in case there will be a request to be added later.
Making the bindings complete even if the driver isn't using it yet is the right thing to do. :-)
I meant allowing just a single interrupt wired up though. So it doesn't matter how the driver would handle it.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
+ description:
+ alert1 indicates a HIGH or LOW limit was exceeded. + alert2 indicates a THERM limit was exceeded. + items:
+ - const: alert1
+ - const: alert2
+
...
Quoted 47 lines
Quoted 39 lines
Quoted 30 lines
+
+ microchip,vbus-half-range:
+ $ref: /schemas/types.yaml#/definitions/flag
+ description: |
+ In order to increase measurement resolution and keeping the same
+ number the of bits the device has a configurable VBUS full range scale
+ (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,vbus-bipolar:Quoted 8 lines
+ $ref: /schemas/types.yaml#/definitions/flag
+ description:
+ If provided, the channel is to be used in bipolar mode. The
+ actual range is limited to about -200 mV due to the impact of the ESD
+ structures.
+Quoted 7 lines
Using Jonathan's suggestion from v2 to just have a single property with 3 different
ranges to chose from seems simpler that this. It would only require one property
and would be self-documenting. The description could be shortened to just a couple
of lines.
Quoted 5 lines
I was thinking to add the range for this property, but it looks (for me at least) more complicated from the checking point of view. The driver is supporting two family of devices that has, each, 3 different voltage range as an input.
Usually, having a consistent binding for the same thing among similar devices is more important than how easy it is to implement in the driver.
Since this seems to be a common pattern, we could probably justify an iio_property_match_ranges() helper function that would simplify the implementation in drivers that would need to use such a property. Then in each driver it would just be a matter of making a static const array lookup table of ranges for each device and calling the helper function.
Re: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Quoted 11 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 4 lines · On 6/10/25 9:46 AM, [email protected] wrote:
Hi David,
Thank you for the feedback. Please see my comments below...
...
...
Quoted 73 lines
Quoted 60 lines
Quoted 51 lines
Quoted 40 lines
+
+ microchip,vbus-half-range:
+ $ref: /schemas/types.yaml#/definitions/flag + description: |
+ In order to increase measurement resolution and keeping
the same
+ number the of bits the device has a configurable VBUS
full range scale
+ (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,vbus-bipolar:
+ $ref: /schemas/types.yaml#/definitions/flagQuoted 8 lines
+ description:
+ If provided, the channel is to be used in bipolar mode.
The
+ actual range is limited to about -200 mV due to the impact of the ESD
+ structures.
+Using Jonathan's suggestion from v2 to just have a single property
Quoted 8 lines
with 3 different
ranges to chose from seems simpler that this. It would only require
one property
and would be self-documenting. The description could be shortened to
just a couple
of lines.Quoted 8 lines
I was thinking to add the range for this property, but it looks (for me
at least) more complicated from the checking point of view. The driver
is supporting two family of devices that has, each, 3 different voltage
range as an input.Usually, having a consistent binding for the same thing among similar devices is more important than how easy it is to implement in the driver.
Quoted 8 lines
Since this seems to be a common pattern, we could probably justify an
iio_property_match_ranges()helper function that would simplify the implementation in drivers that would need to use such a property. Then
in each driver it would just be a matter of making a static const array
lookup table of ranges for each device and calling the helper function.
Sorry for not explaining very well. I have implemented the range into the driver and I was working well, but I had issues defining the range into the device binding and the checker was failing. That was the reason that I've dropped the range from the binding. Also I had some issues enforcing a certain "available" ranges for a particular part into the binding.
David Lechner
reviewerRe: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Quoted 9 lines · On 6/10/25 11:04 AM, [email protected] wrote:
Quoted 8 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 4 lines · On 6/10/25 9:46 AM, [email protected] wrote:
Hi David,
Thank you for the feedback. Please see my comments below...
...
Quoted 34 lines
Quoted 25 lines
Quoted 12 lines
Quoted 11 lines
Using Jonathan's suggestion from v2 to just have a single property
with 3 different
ranges to chose from seems simpler that this. It would only require
one property
and would be self-documenting. The description could be shortened to
just a couple
of lines.Quoted 8 lines
I was thinking to add the range for this property, but it looks (for me
at least) more complicated from the checking point of view. The driver
is supporting two family of devices that has, each, 3 different voltage
range as an input.Usually, having a consistent binding for the same thing among similar devices is more important than how easy it is to implement in the driver.
Quoted 8 lines
Since this seems to be a common pattern, we could probably justify an
iio_property_match_ranges()helper function that would simplify the implementation in drivers that would need to use such a property. Then
in each driver it would just be a matter of making a static const array
lookup table of ranges for each device and calling the helper function.
Quoted 6 lines
Sorry for not explaining very well. I have implemented the range into the driver and I was working well, but I had issues defining the range into the device binding and the checker was failing. That was the reason that I've dropped the range from the binding. Also I had some issues enforcing a certain "available" ranges for a particular part into the binding.
What did you try?
The usual way is to define all possibilities and then limit it by compatible. I think something like this should work:
patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
items:
- enum: [-32000000, -16000000, -9000000, -4500000, 0]
- enum: [4500000, 9000000, 16000000, 32000000]
allOf:
- if:
properties:
compatible:
pattern: "^microchip,pac194"
then:
patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
oneOf:
- items:
- const: 0
- const: 9000000
- items:
- const: -9000000
- const: 9000000
- items:
- const: -4500000
- const: 4500000
default:
items:
- const: 0
- const: 9000000
- if:
properties:
compatible:
pattern: "^microchip,pac195"
then:
patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
oneOf:
- items:
- const: 0
- const: 32000000
- items:
- const: -32000000
- const: 32000000
- items:
- const: -16000000
- const: 16000000
default:
items:
- const: 0
- const: 32000000
Re: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
Hi David,
Thanks for the suggestion.
//Marius
Quoted 127 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 9 lines · On 6/10/25 11:04 AM, [email protected] wrote:
Quoted 8 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 4 lines · On 6/10/25 9:46 AM, [email protected] wrote:
Hi David,
Thank you for the feedback. Please see my comments below...
...
Quoted 38 lines
Quoted 27 lines
Quoted 13 lines
Quoted 12 lines
Using Jonathan's suggestion from v2 to just have a single property
with 3 different
ranges to chose from seems simpler that this. It would only require
one property
and would be self-documenting. The description could be shortened
to
just a couple
of lines.Quoted 8 lines
I was thinking to add the range for this property, but it looks (for me
at least) more complicated from the checking point of view. The driver
is supporting two family of devices that has, each, 3 different voltage
range as an input.Usually, having a consistent binding for the same thing among similar
devices is more important than how easy it is to implement in the driver.Quoted 10 lines
Since this seems to be a common pattern, we could probably justify an
iio_property_match_ranges()helper function that would simplify the
implementation in drivers that would need to use such a property. Then
in each driver it would just be a matter of making a static const array
lookup table of ranges for each device and calling the helper function.Quoted 9 lines
Sorry for not explaining very well. I have implemented the range into
the driver and I was working well, but I had issues defining the range
into the device binding and the checker was failing. That was the reason that I've dropped the range from the binding. Also I had some
issues enforcing a certain "available" ranges for a particular part into the binding.What did you try?
The usual way is to define all possibilities and then limit it by compatible.
I think something like this should work:patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
items:
- enum: [-32000000, -16000000, -9000000, -4500000, 0]
- enum: [4500000, 9000000, 16000000, 32000000]allOf:
- if:
properties:
compatible:
pattern: "^microchip,pac194"
then:
patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
oneOf:
- items:
- const: 0
- const: 9000000
- items:
- const: -9000000
- const: 9000000
- items:
- const: -4500000
- const: 4500000
default:
items:
- const: 0
- const: 9000000
- if:
properties:
compatible:
pattern: "^microchip,pac195"
then:
patternProperties:
"^channel@[1-4]$":
properties:
microchip,input-range-microvolt:
oneOf:
- items:
- const: 0
- const: 32000000
- items:
- const: -32000000
- const: 32000000
- items:
- const: -16000000
- const: 16000000
default:
items:
- const: 0
- const: 32000000
Re: [PATCH v3 1/2] dt-bindings: iio: adc: adding support for PAC194X
In reply to [email protected]On Fri, Jun 06, 2025 at 12:39:28PM +0300, [email protected] wrote:
David had a bunch of comments, so much to say, but..
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 54
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +4
-0+ interrupt-names: + description: + alert1 indicates a HIGH or LOW limit was exceeded. + alert2 indicates a THERM limit was exceeded.
You should be able to merge this description into the items list,
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 56
Quoted 2 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +2
-0+ items: + - const: alert1
by adding description: into these
Quoted 1 line
+ - const: alert2
e.g.
items:
- const: sdmmc-3v3
description: pad configuration for 3.3 V
- const: sdmmc-1v8
description: pad configuration for 1.8 V
- const: sdmmc-3v3-drv
description: pull-up/down configuration for 3.3 V
- const: sdmmc-1v8-drv
description: pull-up/down configuration for 1.8 V
PATCH v3 2/2 iio: adc: adding 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.
--- .../ABI/testing/sysfs-bus-iio-adc-pac1944 | 17 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2841 +++++++++++++++++ 5 files changed, 2878 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 create mode 100644 drivers/iio/adc/pac1944.c
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +17 -0
@@ -0,0 +1,17 @@ +What: /sys/bus/iio/devices/iio:deviceX/slow_alert1_cfg +KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PAC device, a specific ALERT + signal to the SLOW/ALERT1 pin. The SLOW/ALERT1 pin must be configured for the + ALERT function in order to control the device hardware pin (this is the default + functionality of the device hardware pin). + +What: /sys/bus/iio/devices/iio:deviceX/gpio_alert2_cfg +KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PAC device, a specific ALERT + signal to the GPIO/ALERT2 hardware pin. The GPIO/ALERT2 pin must be configured + for ALERT function in order to control the device hardware pin (this is the + default functionality of the device hardware pin).
MAINTAINERS +7 -0
@@ -15920,6 +15920,13 @@ 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 +F: drivers/iio/adc/pac1944.c + MICROCHIP PCI1XXXX GP DRIVER M: Vaibhaav Ram T.L <[email protected]> M: Kumaravel Thiagarajan <[email protected]>
drivers/iio/adc/Kconfig +12 -0
@@ -1148,6 +1148,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
@@ -103,6 +103,7 @@ obj-$(CONFIG_NCT7201) += nct7201.o obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1944) += 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 +2841 -0
2,841 lines not on this page · raw message
Signature · 1 line
2.48.1
David Lechner
reviewerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 11 lines · On 6/6/25 4:39 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 7 lines
Signed-off-by Marius Cristea <[email protected]>--- .../ABI/testing/sysfs-bus-iio-adc-pac1944 | 17 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2841 +++++++++++++++++
Oof. Very hard to make a review of something this big. Is it possible to break it down into more patches? e.g. start with just the basic functionality, then add the accumulator in a separate patch, add events in a separate patch, etc. It's not always possible, but 500 lines per patch is a nice number to aim for.
Quoted 17 lines
5 files changed, 2878 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 create mode 100644 drivers/iio/adc/pac1944.cDocumentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +7
-0@@ -0,0 +1,17 @@ +What: /sys/bus/iio/devices/iio:deviceX/slow_alert1_cfg +KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PAC device, a specific ALERT + signal to the SLOW/ALERT1 pin. The SLOW/ALERT1 pin must be configured for the + ALERT function in order to control the device hardware pin (this is the default
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 line 17
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +10
-0+ functionality of the device hardware pin). + +What: /sys/bus/iio/devices/iio:deviceX/gpio_alert2_cfg +KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PAC device, a specific ALERT + signal to the GPIO/ALERT2 hardware pin. The GPIO/ALERT2 pin must be configured + for ALERT function in order to control the device hardware pin (this is the + default functionality of the device hardware pin).
What is the use case for needing these? In otherwords, why can't the driver just make best use of available resources as it sees fit?
MAINTAINERS S: Supported · line 15926
Quoted 12 lines
MAINTAINERS +4
-0@@ -15920,6 +15920,13 @@ 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
Missing the ABI docs, but hopefuly we can get rid of that.
Quoted 60 lines
MAINTAINERS +3
-0+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]> M: Kumaravel Thiagarajan <[email protected]>drivers/iio/adc/Kconfig +12
-0@@ -1148,6 +1148,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_PALMASdrivers/iio/adc/Makefile +1
-0@@ -103,6 +103,7 @@ obj-$(CONFIG_NCT7201) += nct7201.o obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1944) += 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.odrivers/iio/adc/pac1944.c new file +13
-0@@ -0,0 +1,2841 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* IIO driver for PAC194X and PAC195X series chips +* +* Copyright (C) 2022-2025 Microchip Technology Inc. and its subsidiaries +* +* Author: Marius Cristea [email protected] +* +* 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 +*/
drivers/iio/adc/pac1944.c line 23
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +#include <linux/acpi.h> +#include <linux/bitfield.h> +#include <linux/delay.h> +#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/unaligned.h>
This seems incomplete. Expecting at least linux/module.h, linux/property.h, etc.
drivers/iio/adc/pac1944.c line 619
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +static const struct pac1944_features pac1944_chip_config[] = {
I know there are lots of existing drivers that use arrays like this, but it just makes lots of extra code to read without any practial benifits.
Better is to just make standalone structs for each chip.
Quoted 31 lines
drivers/iio/adc/pac1944.c new file +31
-0+ /* PAC194X Family */ + [PAC1941] = { + .phys_channels = 1, + .prod_id = PAC_PRODUCT_ID_1941, + .name = "pac1941", + }, + [PAC1942] = { + .phys_channels = 2, + .prod_id = PAC_PRODUCT_ID_1942, + .name = "pac1942", + }, + [PAC1943] = { + .phys_channels = 3, + .prod_id = PAC_PRODUCT_ID_1943, + .name = "pac1943", + }, + [PAC1944] = { + .phys_channels = 4, + .prod_id = PAC_PRODUCT_ID_1944, + .name = "pac1944", + }, + [PAC1941_2] = { + .phys_channels = 1, + .prod_id = PAC_PRODUCT_ID_1941_2, + .name = "pac1941_2", + }, + [PAC1942_2] = { + .phys_channels = 2, + .prod_id = PAC_PRODUCT_ID_1942_2, + .name = "pac1942_2", + },
drivers/iio/adc/pac1944.c line 660
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ /* PAC195X Family */ + [PAC1951] = { + .phys_channels = 1, + .prod_id = PAC_PRODUCT_ID_1951, + .name = "pac1951", + }, + [PAC1952] = { + .phys_channels = 2, + .prod_id = PAC_PRODUCT_ID_1952, + .name = "pac1952_1",
Odd that this is the only one to have _1.
Quoted 23 lines
drivers/iio/adc/pac1944.c new file +23
-0+ }, + [PAC1953 ] = { + .phys_channels =3 , + .prod_id =PAC_PRODUCT_ID_1953 , + .name ="pac1953" , + }, + [PAC1954 ] = { + .phys_channels =4 , + .prod_id =PAC_PRODUCT_ID_1954 , + .name ="pac1954" , + }, + [PAC1951_2 ] = { + .phys_channels =1 , + .prod_id =PAC_PRODUCT_ID_1951_2 , + .name ="pac1951_2" , + }, + [PAC1952_2 ] = { + .phys_channels =2 , + .prod_id =PAC_PRODUCT_ID_1952_2 , + .name ="pac1952_2" , + }, +}; +
...
drivers/iio/adc/pac1944.c line 926
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+static IIO_DEVICE_ATTR(in_current1_shunt_resistor, 0644, + pac1944_shunt_value_show, pac1944_shunt_value_store, 0); +static IIO_DEVICE_ATTR(in_current2_shunt_resistor, 0644, + pac1944_shunt_value_show, pac1944_shunt_value_store, 1); +static IIO_DEVICE_ATTR(in_current3_shunt_resistor, 0644, + pac1944_shunt_value_show, pac1944_shunt_value_store, 2); +static IIO_DEVICE_ATTR(in_current4_shunt_resistor, 0644, + pac1944_shunt_value_show, pac1944_shunt_value_store, 3);
These are specified in the devicetree. Why are there also sysfs attribtes?
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+ +static IIO_DEVICE_ATTR(slow_alert1_cfg, 0644, pac1944_slow_alert1_show, + pac1944_slow_alert1_store, 0); +static IIO_DEVICE_ATTR(gpio_alert2_cfg, 0644, pac1944_gpio_alert2_show, + pac1944_gpio_alert2_store, 0); +
...
drivers/iio/adc/pac1944.c line 1709
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-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; +}
Pulled this out of order for context:
Quoted 24 lines
drivers/iio/adc/pac1944.c new file +23
-0+/* Available Sample Modes */ +static const char *const pac1944_frequency_avail[] = { +"1024_ADAP" , +"256_ADAP" , +"64_ADAP" , +"8_ADAP" , +"1024" , +"256" , +"64" , +"8" , +"single_shot_1x" , +"single_shot_8x" , +"fast" , +"burst" , +}; + +static const struct iio_enum sampling_mode_enum = { + .items = pac1944_frequency_avail, + .num_items =ARRAY_SIZE (pac1944_frequency_avail), + .set = pac1944_frequency_set, + .get = pac1944_frequency_get, +}; +
drivers/iio/adc/pac1944.c line 478
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+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, + }, + { } +};
sampling_frequency{_avialable} are already standard attributes in IIO and is defined to be a number in Hz. So we will need to find a way to make this work with the standard attribute (can use IIO_CHAN_INFO_SAMPLE_FREQ, by the way). And figure out how the other parts fit into other existing IIO features.
Quoted 22 lines
drivers/iio/adc/pac1944.c new file +22
-0+ +static int pac1944_of_parse_channel_config (struct i2c_client *client, +struct pac1944_chip_info *info) +{ +unsigned int current_channel; +struct device *dev = &client->dev; +int idx, ret, temp; +bool is_bipolar, is_half_fsr; + + 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" ); + +/* adjust idx to match channel index (1 to 4) from the datasheet */ + idx--; + +if (current_channel >= (info->phys_channels +1 ) || + idx >= info->phys_channels || idx <0 ) + return dev_err_probe(dev, -EINVAL, "invalid channel index %d value\n",
drivers/iio/adc/pac1944.c line 2327
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ idx + 1); + +/* enable channel */ +set_bit (idx, &info->active_channels_mask); + + 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: %d\n" , +fwnode_get_name (child), info->shunts[idx]);
If there is an error, then info->shunts[idx] is not set, so doesn't make sense to include it in the error message.
drivers/iio/adc/pac1944.c line 2329
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +if (fwnode_property_present (child,"label" ))
No point in checking if property is present if not checking the return value of read_string()
drivers/iio/adc/pac1944.c line 2335
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+fwnode_property_read_string (child,"label" , + (const char **)&info->labels[idx]); + + is_bipolar = false; +if (fwnode_property_present (child,"microchip,vbus-bipolar" )) + is_bipolar = true;
Just do:
is_bipolar = fwnode_property_read_bool(child, "microchip,vbus-bipolar");
drivers/iio/adc/pac1944.c line 2339
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ + is_half_fsr = false; +if (fwnode_property_present (child,"microchip,vbus-half-range" )) + is_half_fsr = true;
ditto
Quoted 42 lines
drivers/iio/adc/pac1944.c new file +42
-0+ +/* default value is unipolar and Full Scale Range */ + 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 ; + + is_bipolar = false; +if (fwnode_property_present (child,"microchip,vsense-bipolar" )) + is_bipolar = true; + + is_half_fsr = false; +if (fwnode_property_present (child,"microchip,vsense-half-range" )) + is_half_fsr = true; + +/* default value is unipolar and Full Scale Range */ + 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 ; + + ret =fwnode_property_read_u32 (child,"microchip,accumulation-mode" , &temp); +if (ret) +return dev_err_probe (dev, ret,"invalid accumulation-mode value on %s\n" , +fwnode_get_name (child)); +if (temp ==PAC1944_ACCMODE_VPOWER || + temp ==PAC1944_ACCMODE_VSENSE || + temp ==PAC1944_ACCMODE_VBUS ) { +dev_dbg (dev,"Accumulation{%d} mode set to: %d\n" , idx, temp); + info->chip_reg_data.accumulation_mode[idx] = temp; + }else { +return dev_err_probe (dev, -EINVAL , +"invalid mode for accumulator value on %s\n" , +fwnode_get_name (child)); + } + current_channel++; + } + +return 0 ; +}
...
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ +static const struct i2c_device_id pac1944_id[] = { + { .name ="pac1941" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1941 ] }, + { .name ="pac19412" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1941_2 ] },
drivers/iio/adc/pac1944.c line 2764
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ { .name = "pac1942", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1942] }, + { .name = "pac19422", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1942_2] }, + { .name = "pac1943", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1943] }, + { .name = "pac1944", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1944] }, + { .name = "pac1951", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1951] }, + { .name = "pac19512", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1951_2] }, + { .name = "pac1952", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1952] }, + { .name = "pac19522", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1952_2] }, + { .name = "pac1953", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1953] }, + { .name = "pac1954", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1954] },
Drop the .name and .driver_data here. Thre is an effort to do this in all drivers so that we can eventually get rid of the (kernel_ulong_t) casts.
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ { } +}; +MODULE_DEVICE_TABLE (i2c, pac1944_id); +
Re: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 24 lines · David Lechner
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 11 lines · On 6/6/25 4:39 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]>---
.../ABI/testing/sysfs-bus-iio-adc-pac1944 | 17 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 2841
+++++++++++++++++
Quoted 7 lines
Oof. Very hard to make a review of something this big. Is it possible to break it down into more patches? e.g. start with just the basic functionality, then add the accumulator in a separate patch, add events
in a separate patch, etc. It's not always possible, but 500 lines per patch is a nice number to aim for.
The first 2 version of the driver was much bigger. I will try make it smaller and build on top of that.
Quoted 35 lines
Quoted 35 lines
5 files changed, 2878 insertions(+)create mode 100644
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944
create mode 100644drivers/iio/adc/pac1944.cDocumentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +1
-0@@ -0,0 +1,17 @@ +What:/sys/bus/iio/devices/iio:deviceX/slow_alert1_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the SLOW/ALERT1 pin. The SLOW/ALERT1 pin must be configured for the
+ ALERT function in order to control the device hardware pin (this is the default
+ functionality of the device hardware pin).Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +2
-0+ +What:/sys/bus/iio/devices/iio:deviceX/gpio_alert2_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the GPIO/ALERT2 hardware pin. The GPIO/ALERT2 pin must be configured
Quoted 9 lines
Quoted 3 lines
+ for ALERT function in order to control the device hardware pin (this is the
+ default functionality of the device hardware pin).What is the use case for needing these? In otherwords, why can't the driver just
make best use of available resources as it sees fit?
Here inside the PAC the user could choose what limit to be routeed outside the chip. For sure, all of the limits could be routed to the same hardware pin, but there are some use cases where the user will want to connect that output pin to a safety hardware (e.g. over-current protection or over-voltage and over-current) and in this case we need a way to allow the user to do the setup.
Quoted 19 lines
Quoted 19 lines
subsidiaries
+ *
+ * Author: Marius Cristea [email protected]
+ *
+ * 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
+ */Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +5
-0+ +#include <linux/acpi.h> +#include <linux/bitfield.h> +#include <linux/delay.h> +#include <linux/err.h>
Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +5
-0+#include <linux/i2c.h> +#include <linux/iio/iio.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/unaligned.h>This seems incomplete. Expecting at least
linux/module.h,linux/property.h, etc.
I will double check. Most probably those module are already included by a header.
Quoted 18 lines
...
Quoted 4 lines
+static IIO_DEVICE_ATTR(in_current1_shunt_resistor, 0644, +
pac1944_shunt_value_show,pac1944_shunt_value_store, 0);
+static IIO_DEVICE_ATTR(in_current2_shunt_resistor, 0644,Quoted 8 lines
+
pac1944_shunt_value_show,pac1944_shunt_value_store, 1);
+static IIO_DEVICE_ATTR(in_current3_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 2);
+static IIO_DEVICE_ATTR(in_current4_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 3);These are specified in the devicetree. Why are there also sysfs attribtes?
Yes, you could put a generic shunt resistor into the device tree but this resistor will have a tolerance. Because the end user could calibrate the system, it could also save the calculated/calibrated shunt resistor somewhere and restore that calibrated value each time the driver is loaded.
Quoted 19 lines
Quoted 19 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +15
-0+/* Available Sample Modes */ +static const char * const pac1944_frequency_avail[] = { + "1024_ADAP", + "256_ADAP", + "64_ADAP", + "8_ADAP", + "1024", + "256", + "64", + "8", + "single_shot_1x", + "single_shot_8x", + "fast", + "burst", +};
...
Quoted 13 lines
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +3
-0+ +static const struct iio_chan_spec_ext_info pac1944_ext_info[] = { + IIO_ENUM("sampling_frequency", IIO_SHARED_BY_ALL,
&sampling_mode_enum),Quoted 8 lines
+ {
+ .name = "sampling_frequency_available", + .shared =IIO_SHARED_BY_ALL,
+ .read =iio_enum_available_read, + .private = (uintptr_t)&sampling_mode_enum, + },
+ { }
+};
Quoted 8 lines
sampling_frequency{_avialable} are already standard attributes in IIO and is
defined to be a number in Hz. So we will need to find a way to make this
work with the standard attribute (can useIIO_CHAN_INFO_SAMPLE_FREQ, by the way).
And figure out how the other parts fit into other existing IIO features.
I can change to the standard attributes but I still have some question related to how to handle the ADAPTIVE sampling frequency that the chip supports and that it could be used to lower the power consumption of the chip.
Quoted 1 line
David Lechner
reviewerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 1 line · On 6/10/25 10:07 AM, [email protected] wrote:
On Fri, 2025-06-06 at 12:02 -0500, David Lechner wrote:
...
Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +1
-0@@ -0,0 +1,17 @@ +What:/sys/bus/iio/devices/iio:deviceX/slow_alert1_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the SLOW/ALERT1 pin. The SLOW/ALERT1 pin must be configured for the
+ ALERT function in order to control the device hardware pin (this is the default
+ functionality of the device hardware pin).Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +2
-0+ +What:/sys/bus/iio/devices/iio:deviceX/gpio_alert2_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the GPIO/ALERT2 hardware pin. The GPIO/ALERT2 pin must be configuredQuoted 9 lines
Quoted 3 lines
+ for ALERT function in order to control the device hardware pin (this is the
+ default functionality of the device hardware pin).What is the use case for needing these? In otherwords, why can't the driver just
make best use of available resources as it sees fit?
Quoted 7 lines
Here inside the PAC the user could choose what limit to be routeed outside the chip. For sure, all of the limits could be routed to the same hardware pin, but there are some use cases where the user will want to connect that output pin to a safety hardware (e.g. over-current protection or over-voltage and over-current) and in this case we need a way to allow the user to do the setup.
This sounds like it depends on what is wired to the alert pin, so sounds like something that should be specified in the devicetree.
I.e. in the devicetree, have a bool property microchip,alert1-is-safety to indicate the ALERT1 pin is wired to the safety hardware. (It could still be also wired as an interrupt input at the same time - or not, doesn't really matter.)
Then, on the event attributes add a boolean "safety" attribute to allow routing the signal to either the pin that was flagged as the safety pin or not. This would allow the user to chose which signals control the safety hardware at runtime without them having to know how the hardware is actually wired up.
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
subsidiaries
+ *
+ * Author: Marius Cristea [email protected]
+ *
+ * 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
+ */Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +5
-0+ +#include <linux/acpi.h> +#include <linux/bitfield.h> +#include <linux/delay.h> +#include <linux/err.h>Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +5
-0+#include <linux/i2c.h> +#include <linux/iio/iio.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/unaligned.h>This seems incomplete. Expecting at least
linux/module.h,linux/property.h, etc.
Quoted 3 lines
I will double check. Most probably those module are already included by a header.
Even if some things happens to be indirectly included by another header, we still prefer to have the "primary" header included for everything that is actually used in this file. E.g. since this file uses the MODULE_DESCRIPTION() macro, we expect to see linux/module.h even if another header already includes that indirectly.
Usually the "primary" header is the one where a symbol/macro is defined, but not always, e.g. if there is a linux/something.h that incldues asm/something.h, we would prefer the linux/ one rather than including the asm/ one directly.
Quoted 19 lines
Quoted 18 lines
...
Quoted 4 lines
+static IIO_DEVICE_ATTR(in_current1_shunt_resistor, 0644, +
pac1944_shunt_value_show,pac1944_shunt_value_store, 0);
+static IIO_DEVICE_ATTR(in_current2_shunt_resistor, 0644,Quoted 8 lines
+
pac1944_shunt_value_show,pac1944_shunt_value_store, 1);
+static IIO_DEVICE_ATTR(in_current3_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 2);
+static IIO_DEVICE_ATTR(in_current4_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 3);These are specified in the devicetree. Why are there also sysfs attribtes?
Quoted 6 lines
Yes, you could put a generic shunt resistor into the device tree but this resistor will have a tolerance. Because the end user could calibrate the system, it could also save the calculated/calibrated shunt resistor somewhere and restore that calibrated value each time the driver is loaded.
If changing the resistor value changes the measured raw value, we could probably use one of the existing standard calibration attributes instead, like calibbias or calibscale.
Quoted 43 lines
Quoted 20 lines
Quoted 20 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +15
-0+/* Available Sample Modes */ +static const char * const pac1944_frequency_avail[] = { + "1024_ADAP", + "256_ADAP", + "64_ADAP", + "8_ADAP", + "1024", + "256", + "64", + "8", + "single_shot_1x", + "single_shot_8x", + "fast", + "burst", +};...
Quoted 13 lines
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +3
-0+ +static const struct iio_chan_spec_ext_info pac1944_ext_info[] = { + IIO_ENUM("sampling_frequency", IIO_SHARED_BY_ALL,
&sampling_mode_enum),Quoted 8 lines
+ {
+ .name = "sampling_frequency_available", + .shared =IIO_SHARED_BY_ALL,
+ .read =iio_enum_available_read, + .private = (uintptr_t)&sampling_mode_enum, + },
+ { }
+};Quoted 8 lines
sampling_frequency{_avialable} are already standard attributes in IIO and is
defined to be a number in Hz. So we will need to find a way to make this
work with the standard attribute (can useIIO_CHAN_INFO_SAMPLE_FREQ, by the way).
And figure out how the other parts fit into other existing IIO features.
Quoted 6 lines
I can change to the standard attributes but I still have some question related to how to handle the ADAPTIVE sampling frequency that the chip supports and that it could be used to lower the power consumption of the chip.
Quoted 1 line
From a quick look at one of the datasheets, it sounds like this "adaptive" mode only applies when using an accumulator. And it doesn't actually change the sample rate, but rather other factors, like scale and the accumulator counter incitement. So it seems like it would be a separate custom boolean attribute.
Also, I noticed that the fast mode and burst mode make the sampling frequency dependent on the number of enabled channels. So to handle this, normally, that would mean that IIO_CHAN_INFO_SAMP_FREQ would need to be IIO_SEPARATE rather than IIO_SHARED_BY_ALL.
But since these chips support can work both ways (there are modes where sample rate doesn't depend on the number of channels enabled and there are modes where it does), I'm not sure what the right way to handle that would be here. Maybe Jonathan will have some suggestion?
Jonathan Cameron
maintainerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 52 lines · On Tue, 10 Jun 2025 11:11:32 -0500 David Lechner <[email protected]> wrote:
Quoted 1 line · On 6/10/25 10:07 AM, [email protected] wrote:
On Fri, 2025-06-06 at 12:02 -0500, David Lechner wrote:
...
Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +1
-0@@ -0,0 +1,17 @@ +What:/sys/bus/iio/devices/iio:deviceX/slow_alert1_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the SLOW/ALERT1 pin. The SLOW/ALERT1 pin must be configured for the
+ ALERT function in order to control the device hardware pin (this is the default
+ functionality of the device hardware pin).Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +2
-0+ +What:/sys/bus/iio/devices/iio:deviceX/gpio_alert2_cfg
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +4
-0+KernelVersion: 6.16 +Contact: [email protected] +Description: + A read/write property used to route, inside the PACdevice, a specific ALERT
+ signal to the GPIO/ALERT2 hardware pin. The GPIO/ALERT2 pin must be configuredQuoted 9 lines
Quoted 3 lines
+ for ALERT function in order to control the device hardware pin (this is the
+ default functionality of the device hardware pin).What is the use case for needing these? In otherwords, why can't the driver just
make best use of available resources as it sees fit?Quoted 7 lines
Here inside the PAC the user could choose what limit to be routeed outside the chip. For sure, all of the limits could be routed to the same hardware pin, but there are some use cases where the user will want to connect that output pin to a safety hardware (e.g. over-current protection or over-voltage and over-current) and in this case we need a way to allow the user to do the setup.
Quoted 2 lines
This sounds like it depends on what is wired to the alert pin, so sounds like something that should be specified in the devicetree.
Absolutely agree.
These corners tend to be tricky to handle cleanly
in either DT or userspace interfaces though and sometimes we've just decided not to be fully flexible to keep that sane.
Quoted 11 lines
I.e. in the devicetree, have a bool property microchip,alert1-is-safety to indicate the ALERT1 pin is wired to the safety hardware. (It could still be also wired as an interrupt input at the same time - or not, doesn't really matter.)
Then, on the event attributes add a boolean "safety" attribute to allow routing the signal to either the pin that was flagged as the safety pin or not. This would allow the user to chose which signals control the safety hardware at runtime without them having to know how the hardware is actually wired up.
I'd question if the use cases are such that it makes sense to expose the option to user space. Generally if you have a way to trigger a safety circuit it's there for a reason and you want it always on for that reason (i.e. you might tweak exactly when a cut off on power usage fires, but you always do it on power usage).
We probably want to propose a binding to he DT folk + take a look at what the ABI David suggests looks like in the driver. That's messy if it isn't double wired to the interrupt controller though as we then have events that never fire.
Quoted 26 lines
Quoted 19 lines
Quoted 18 lines
...
Quoted 4 lines
+static IIO_DEVICE_ATTR(in_current1_shunt_resistor, 0644, +
pac1944_shunt_value_show,pac1944_shunt_value_store, 0);
+static IIO_DEVICE_ATTR(in_current2_shunt_resistor, 0644,Quoted 8 lines
+
pac1944_shunt_value_show,pac1944_shunt_value_store, 1);
+static IIO_DEVICE_ATTR(in_current3_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 2);
+static IIO_DEVICE_ATTR(in_current4_shunt_resistor, 0644, +pac1944_shunt_value_show,pac1944_shunt_value_store, 3);These are specified in the devicetree. Why are there also sysfs attribtes?
Quoted 6 lines
Yes, you could put a generic shunt resistor into the device tree but this resistor will have a tolerance. Because the end user could calibrate the system, it could also save the calculated/calibrated shunt resistor somewhere and restore that calibrated value each time the driver is loaded.
Quoted 3 lines
If changing the resistor value changes the measured raw value, we could probably use one of the existing standard calibration attributes instead, like calibbias or calibscale.
We have precedence for shunt resistor ABI for this reason, so I agree with David if we were starting from scratch (and it's a viable option to use the calibration attributes) but we can't drop the existing ABI and it does limited harm to carry on using it.
Quoted 44 lines
Quoted 44 lines
Quoted 21 lines
Quoted 21 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +15
-0+/* Available Sample Modes */ +static const char * const pac1944_frequency_avail[] = { + "1024_ADAP", + "256_ADAP", + "64_ADAP", + "8_ADAP", + "1024", + "256", + "64", + "8", + "single_shot_1x", + "single_shot_8x", + "fast", + "burst", +};...
Quoted 21 lines
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +3
-0+ +static const struct iio_chan_spec_ext_info pac1944_ext_info[] = { + IIO_ENUM("sampling_frequency", IIO_SHARED_BY_ALL,
&sampling_mode_enum),Quoted 8 lines
+ {
+ .name = "sampling_frequency_available", + .shared =IIO_SHARED_BY_ALL,
+ .read =iio_enum_available_read, + .private = (uintptr_t)&sampling_mode_enum, + },
+ { }
+};sampling_frequency{_avialable} are already standard attributes in IIO and is
defined to be a number in Hz. So we will need to find a way to make this
work with the standard attribute (can useIIO_CHAN_INFO_SAMPLE_FREQ, by the way).
And figure out how the other parts fit into other existing IIO features.
Quoted 4 lines
Quoted 4 lines
I can change to the standard attributes but I still have some question related to how to handle the ADAPTIVE sampling frequency that the chip supports and that it could be used to lower the power consumption of the chip.
Gah. Second device to do something like this that we've seen this month. (ADXL313 does something similar when enough activity detection modes are turned on). We don't have a good answer yet :(
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
From a quick look at one of the datasheets, it sounds like this "adaptive" mode only applies when using an accumulator. And it doesn't actually change the sample rate, but rather other factors, like scale and the accumulator counter incitement. So it seems like it would be a separate custom boolean attribute.
Generally when I see this sort of thing my first instinct is hide it. What use is it for userspace to see that the frequency is changing? Maybe we just report the highest (or lowest?) frequency that might be going on? Here it doesn't seem to be coupled to anything else though (unlikely the accelerometer) so I'm not sure how the driver would guess it makes sense to enable it?
Quoted 10 lines
Also, I noticed that the fast mode and burst mode make the sampling frequency dependent on the number of enabled channels. So to handle this, normally, that would mean that
IIO_CHAN_INFO_SAMP_FREQwould need to beIIO_SEPARATErather thanIIO_SHARED_BY_ALL.But since these chips support can work both ways (there are modes where sample rate doesn't depend on the number of channels enabled and there are modes where it does), I'm not sure what the right way to handle that would be here. Maybe Jonathan will have some suggestion?
Oh goody a new way to stretch that already much stretched interface. It's actually more fun because it's not the sum of the number of channels for fast mode but 1 more than that.
Given we can't do both ways of reporting it at the same time we will probably have to just do IIO_SHARED_BY_ALL. We can only then report the effective sampling frequency as it is fixed in these modes (so we can't attempt to match a userspace request by cranking it down if there are fewer channels enabled or similar).
So we needsampling_frequency_available where the fastest frequency presented changes depending on the enabled channels. Lower values match the 8/64/256/1024 values however many are enabled.
if sampling_frequency == top value then we just change it if more channels are enabled. ABI always allows for other attributes to change 'randomly' when you touch a different one. That flexibility is a pain for users but necessary for cases like this :(
I'm not sure how we pick between burst and fast mode though as seems to be about auto offset stuff.
Jonathan
Andy Shevchenko
reviewerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
In reply to [email protected]Quoted 2 lines · On Tue, Jun 10, 2025 at 03:07:34PM +0000, [email protected] wrote:
Quoted 1 line · David Lechner
On 6/6/25 4:39 AM, [email protected] wrote:
...
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+#include <linux/acpi.h> +#include <linux/bitfield.h> +#include <linux/delay.h> +#include <linux/err.h> Quoted 8 lines
drivers/iio/adc/pac1944.c line 23
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+#include <linux/i2c.h> +#include <linux/iio/iio.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/unaligned.h> This seems incomplete. Expecting at least
linux/module.h,linux/property.h, etc.
Quoted 2 lines
I will double check. Most probably those module are already included by a header.
And this is exactly the problem needs to be addressed. Read about IFYU principle. That's what we want.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
In reply to [email protected]Quoted 6 lines · On Fri, 6 Jun 2025 12:39:29 +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]>
Hi Marius,
I entirely agree with David on this wanting splitting up into a base driver + patches that add features to that in order to make it easier to review. That also potentially allows me to pick up the basic support whilst any more controversial parts are still under discussion.
Jonathan
Quoted 18 lines
drivers/iio/adc/Makefile +1
-0@@ -103,6 +103,7 @@ obj-$(CONFIG_NCT7201) += nct7201.o obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1944) += 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.odrivers/iio/adc/pac1944.c new file +0
-0
drivers/iio/adc/pac1944.c line 453
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +4
-0@@ -0,0 +1,2841 @@ +/* Available Sample Modes */ +static const char *const pac1944_frequency_avail[] = { +"1024_ADAP" , +"256_ADAP" ,
This adaptive mode shouldn't be controlled via this standard ABI. That needs to be considered separately.
drivers/iio/adc/pac1944.c line 459
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+"64_ADAP" , +"8_ADAP" , +"1024" , +"256" , +"64" , + "8",
This does not look even close to ABI complaint.
The numbers cases are fine. The others not really.
drivers/iio/adc/pac1944.c line 460
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+ "single_shot_1x",
That has nothing directly to do with the sampling frequency. Some others look suspicious. I'd stick to normal sampling_frequency handling and have the discussion about the other modes in here at a later date.
Quoted 30 lines
drivers/iio/adc/pac1944.c new file +28
-0+"single_shot_8x" , +"fast" , + "burst", +}; + +static const struct pac1944_features pac1944_chip_config[] = { +/* PAC194X Family */ + [PAC1941 ] = { + .phys_channels =1 , + .prod_id =PAC_PRODUCT_ID_1941 , + .name ="pac1941" , + }, + [PAC1942 ] = { + .phys_channels =2 , + .prod_id =PAC_PRODUCT_ID_1942 , + .name ="pac1942" , + }, + [PAC1943 ] = { + .phys_channels =3 , + .prod_id =PAC_PRODUCT_ID_1943 , + .name ="pac1943" , + }, + [PAC1944 ] = { + .phys_channels =4 , + .prod_id =PAC_PRODUCT_ID_1944 , + .name ="pac1944" , + }, + [PAC1941_2 ] = {
drivers/iio/adc/pac1944.c line 651
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ .phys_channels = 1, + .prod_id = PAC_PRODUCT_ID_1941_2, + .name = "pac1941_2", + }, + [PAC1942_2] = { + .phys_channels = 2, + .prod_id = PAC_PRODUCT_ID_1942_2, + .name = "pac1942_2", + }, +/* PAC195X Family */
As mentioned below - just add the family in here as well so no function is needed to look it up.
Quoted 34 lines
drivers/iio/adc/pac1944.c new file +34
-0+ [PAC1951 ] = { + .phys_channels =1 , + .prod_id =PAC_PRODUCT_ID_1951 , + .name ="pac1951" , + }, + [PAC1952 ] = { + .phys_channels =2 , + .prod_id =PAC_PRODUCT_ID_1952 , + .name ="pac1952_1" , + }, + [PAC1953 ] = { + .phys_channels =3 , + .prod_id =PAC_PRODUCT_ID_1953 , + .name ="pac1953" , + }, + [PAC1954 ] = { + .phys_channels =4 , + .prod_id =PAC_PRODUCT_ID_1954 , + .name ="pac1954" , + }, + [PAC1951_2 ] = { + .phys_channels =1 , + .prod_id =PAC_PRODUCT_ID_1951_2 , + .name ="pac1951_2" , + }, + [PAC1952_2 ] = { + .phys_channels =2 , + .prod_id =PAC_PRODUCT_ID_1952_2 , + .name ="pac1952_2" , + }, +}; + +static inline u64 pac1944_get_unaligned_be56 (const u8 *p) +{
drivers/iio/adc/pac1944.c line 695
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+return (u64 )p[0 ] <<48 | (u64 )p[1 ] <<40 | (u64 )p[2 ] <<32 | + (u64 )p[3 ] <<24 | p[4 ] <<16 | p[5 ] <<8 | p[6 ]; +} + +/* 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,
{
.addr = ...etc
Quoted 30 lines
drivers/iio/adc/pac1944.c new file +30
-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)); +} + +static int pac1944_disable_alert_reg (struct device *dev,u32 mask,u8 *status) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client; +int ret; +u32 val; +u8 buf[PAC1944_ALERT_ENABLE_REG_LEN ]; + + ret =i2c_smbus_read_i2c_block_data (client, +PAC1944_ALERT_ENABLE_REG_ADDR , +PAC1944_ALERT_ENABLE_REG_LEN , + status); +if (ret <0 ) { +dev_err (dev,"failing to disable allert reg\n" ); +return ret;
drivers/iio/adc/pac1944.c line 735
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ } + + val =get_unaligned_be24 (status); + val = val & (~mask); +put_unaligned_be24 (val, &buf[0 ]); + +/* disable appropriate bit from the Alert enable register */ +return i2c_smbus_write_block_data (client,PAC1944_ALERT_ENABLE_REG_ADDR , +PAC1944_ALERT_ENABLE_REG_LEN , + (u8 *)&buf[0 ]);
I'd just use buf instead of that complex casting and array index (it's a u8 * anyway so definitely no casting!
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +8
-0+} +static int pac1944_update_alert_16b (struct device *dev,u8 addr,u32 mask, +u16 value) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client; +int ret;
drivers/iio/adc/pac1944.c line 776
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+__be16 tmp_be16; +u8 status[PAC1944_ALERT_ENABLE_REG_LEN ]; + + ret =pac1944_disable_alert_reg (dev, mask, &status[0 ]); +if (ret) { +dev_err (dev,"failed to disable alert reg\n" ); +return ret; + } + + tmp_be16 =cpu_to_be16 (value);
As below - I think this wants to be using i2c_smbus_write_word_swapped()
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + ret =i2c_smbus_write_word_data (client, addr, tmp_be16); +if (ret) { +dev_err (dev,"failing to write at 0x%x\n" , addr); +return ret; + } + +return pac1944_restore_alert_reg (indio_dev, &status[0 ]); +} +
drivers/iio/adc/pac1944.c line 796
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+static int pac1944_update_alert_24b (struct device *dev,u8 addr,u32 mask, +u32 value) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client; +int ret; +u8 status[PAC1944_ALERT_ENABLE_REG_LEN ], tmp[3 ]; + + ret =pac1944_disable_alert_reg (dev, mask, &status[0 ]);
If you are going to parse an array just use status as the parameter, not &status[0]
drivers/iio/adc/pac1944.c line 802
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+if (ret) { +dev_err (dev,"failed to disable alert reg\n" ); +return ret; + } + +put_unaligned_be24 (value, &tmp[0 ]);
, tmp) is fine here.
drivers/iio/adc/pac1944.c line 810
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+ + ret =i2c_smbus_write_block_data (client, addr,ARRAY_SIZE (tmp), (u8 *)&tmp[0 ]); +if (ret) { +dev_err (dev,"failing to write at 0x%x\n" , addr); +return ret; + } + +return pac1944_restore_alert_reg (indio_dev, &status[0 ]);
, status);
probably appropriate choice here as well.
drivers/iio/adc/pac1944.c line 1266
Quoted 13 lines
drivers/iio/adc/pac1944.c new file +12
-0+} + +static int pac1944_retrieve_data (struct pac1944_chip_info *info,u32 wait_time) +{ +int ret =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 ))) {
If you can exit early like here, it is worth flipping the logic.
if (!time_after(jiff.....)) return 0; ret = ...
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +17
-0+/* +* We need to re-read the chip values +* call the pac1944_reg_snapshot +*/ + ret =pac1944_reg_snapshot (info, true,PAC1944_REFRESH_REG_ADDR , + wait_time); +/* +* Re-schedule the work for the read registers timeout +* (to prevent chip regs saturation) +*/ +cancel_delayed_work_sync (&info->work_chip_rfsh); +schedule_delayed_work (&info->work_chip_rfsh, +msecs_to_jiffies (PAC1944_MAX_RFSH_LIMIT_MS )); + } + +return ret; +}
drivers/iio/adc/pac1944.c
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+static struct attribute *pac1944_power_acc_attr[] = { + &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,
These look like standard read_raw / info_mask based attributes will work. So do that, not custom attributes that are both harder to review and don't work with in kernel consumers (which we may well see for a power monitoring chip).
Quoted 25 lines
drivers/iio/adc/pac1944.c new file +25
-0+ &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 struct attribute *pac1944_voltage_acc_attr[] = { + &iio_dev_attr_in_voltage_acc1_raw.dev_attr.attr,
drivers/iio/adc/pac1944.c line 1588
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ &iio_dev_attr_in_voltage_acc2_raw.dev_attr.attr, + &iio_dev_attr_in_voltage_acc3_raw.dev_attr.attr, + &iio_dev_attr_in_voltage_acc4_raw.dev_attr.attr, + &iio_dev_attr_in_voltage_acc1_scale.dev_attr.attr, + &iio_dev_attr_in_voltage_acc2_scale.dev_attr.attr, + &iio_dev_attr_in_voltage_acc3_scale.dev_attr.attr, + &iio_dev_attr_in_voltage_acc4_scale.dev_attr.attr, + &iio_dev_attr_in_voltage_acc1_en.dev_attr.attr, + &iio_dev_attr_in_voltage_acc2_en.dev_attr.attr, + &iio_dev_attr_in_voltage_acc3_en.dev_attr.attr,
Where is this ABI documented?
Quoted 89 lines
drivers/iio/adc/pac1944.c new file +89
-0+ &iio_dev_attr_in_voltage_acc4_en.dev_attr.attr, +NULL +}; + +static int pac1944_prep_custom_attributes (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +int ch, i, j; +int active_channels_count =0 ; +struct attribute **pac1944_custom_attrs, **tmp_attr; +struct attribute_group *pac1944_group; +int custom_attr_cnt; +struct i2c_client *client = info->client; + + active_channels_count = info->num_enabled_channels; + + 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 pair attributes: +* - in_power_accX_raw and in_power_accX_scale +* - in_current_accX_raw and in_current_accX_scale +* - in_voltage_accX_raw and in_voltage_accX_scale +* Shared attributes: +* - slow_alert1_cfg +* - gpio_alert2_cfg +* NULL +*/ + custom_attr_cnt =PAC1944_COMMON_DEVATTR * active_channels_count; + custom_attr_cnt +=PAC1944_ACC_DEVATTR * active_channels_count; + custom_attr_cnt +=PAC1944_SHARED_DEVATTRS_COUNT ; + + pac1944_custom_attrs =devm_kzalloc (&client->dev, custom_attr_cnt * +sizeof (*pac1944_group) +1 ,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 ; +case PAC1944_ACCMODE_VBUS : + tmp_attr = pac1944_voltage_acc_attr; +break ; +default : +return -EINVAL ; + } + + pac1944_custom_attrs[j++] = tmp_attr[ch]; + pac1944_custom_attrs[j++] = pac1944_power_acc_attr[PAC1944_MAX_CH + ch]; + pac1944_custom_attrs[j++] = pac1944_power_acc_attr[2 *PAC1944_MAX_CH + ch]; + } + +for (i =0 ; i <PAC1944_SHARED_DEVATTRS_COUNT ; i++) + pac1944_custom_attrs[j++] = + pac1944_all_attrs[PAC1944_COMMON_DEVATTR *PAC1944_MAX_CH + i]; + + 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; +int ret; +u16 tmp_u16; +__be16 tmp_be16; + + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_CTRL_ACT_REG_ADDR , +sizeof (tmp_u16), (u8 *)&tmp_be16);
drivers/iio/adc/pac1944.c line 1687
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (ret <0 ) { +dev_err (&client->dev,"cannot read PAC1944 regs from 0x%02X\n" , +PAC1944_CTRL_ACT_REG_ADDR ); +return ret; + } + + tmp_u16 =be16_to_cpu (tmp_be16); + tmp_u16 &= ~PAC1944_CTRL_SAMPLE_MASK ; + tmp_u16 |=FIELD_PREP (PAC1944_CTRL_SAMPLE_MASK , mode); + tmp_be16 =cpu_to_be16 (tmp_u16);
Do that cpu_to_be16() inline below.
Quoted 19 lines
drivers/iio/adc/pac1944.c new file +19
-0+ + scoped_guard(mutex, &info->lock) { + ret =i2c_smbus_write_word_data (client,PAC1944_CTRL_REG_ADDR , tmp_be16); +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 ); +} + +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);
drivers/iio/adc/pac1944.c line 1716
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +return info->sampling_mode; +} + +static const struct iio_enum sampling_mode_enum = { + .items = pac1944_frequency_avail, + .num_items =ARRAY_SIZE (pac1944_frequency_avail), + .set = pac1944_frequency_set, + .get = pac1944_frequency_get, +};
Use the standard read_raw, and info_mask_shared, read_avail etc for this not the path meant for custom ABI.
drivers/iio/adc/pac1944.c line 1869
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+ +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; + +/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ;
Comment as suggested below would read better.
drivers/iio/adc/pac1944.c line 1875
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+ +/* +* 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).
Wrap to 80 chars
drivers/iio/adc/pac1944.c line 1749
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+*/ +if (idx >=PAC1944_MAX_CH ) + idx = idx -PAC1944_MAX_CH ;
Is this same as
idx %= PA1944_MAX_CH;
or maybe keep the more obvious single subtraction version.
if (idx >= PAC1944_MAX_CH) idx -= PAC1944_MAX_CH;
Various repeats of this occur elsewhere so apply an changes to all of them.
drivers/iio/adc/pac1944.c line 1887
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+ +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 ); +else
Technically else not needed as returned in other path. I don't mind keeping it if you strong prefer though.
Quoted 76 lines
drivers/iio/adc/pac1944.c new file +75
-0+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 ); +else +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 ); +else +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 ); +else +return sysfs_emit (label,"IBUS_AVG_%d\n" , idx +1 ); +case PAC1944_VPOWER_1_ADDR : +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 ); +else +return sysfs_emit (label,"POWER_%d\n" , idx +1 ); + } + +return 0 ; +} +static int pac1944_write_thresh (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan,enum iio_event_type type, +enum iio_event_direction dir,enum iio_event_info info, +int val,int val2) +{ +struct pac1944_chip_info *chip_info =iio_priv (indio_dev); +int idx, ret; + +/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ; + +guard (mutex)(&chip_info->lock); + +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_OV_LIMIT_REG_ADDR + idx, + pac1944_overvoltage_mask_tbl[idx], val); +if (ret) +return ret; + + chip_info->overvoltage[idx] = val; +return 0 ; +case IIO_EV_DIR_FALLING : + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_UV_LIMIT_REG_ADDR + idx, + pac1944_undervoltage_mask_tbl[idx], val); +if (ret) +return ret; + + chip_info->undervoltage[idx] = val; +return 0 ; +default : +return -EINVAL ; + } +case IIO_CURRENT :
drivers/iio/adc/pac1944.c line 2022
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+switch (dir) { +case IIO_EV_DIR_RISING : + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_OC_LIMIT_REG_ADDR + idx, + pac1944_overcurrent_mask_tbl[idx], val); +if (ret) +return ret; + + chip_info->overcurrent[idx] = val; +return ret;
return 0;
Quoted 35 lines
drivers/iio/adc/pac1944.c new file +35
-0+case IIO_EV_DIR_FALLING : + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_UC_LIMIT_REG_ADDR + idx, + pac1944_undercurrent_mask_tbl[idx], val); +if (ret) +return ret; + + chip_info->undercurrent[idx] = val; +return 0 ; +default : +return -EINVAL ; + } +case IIO_POWER : +switch (dir) { +case IIO_EV_DIR_RISING : + ret =pac1944_update_alert_24b (&indio_dev->dev, +PAC1944_OP_LIMIT_REG_ADDR + idx, + pac1944_overpower_mask_tbl[idx], + val); +if (ret) +return ret; + + chip_info->overpower[idx] = val; +return 0 ; +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} + +static int pac1944_read_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type,
drivers/iio/adc/pac1944.c line 2067
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ enum iio_event_direction dir) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +int idx; +u32 tmp; + +/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ; + + scoped_guard(mutex, &info->lock) {
guard() is fine and will reduce indent which will slightly help readability. The tiny bit of maths at the end isn't significant enough to care if we release the lock before it or not.
Quoted 20 lines
drivers/iio/adc/pac1944.c new file +20
-0+switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + tmp =FIELD_GET (PAC1944_OV_MASK , info->alert_enable); +break ; +case IIO_EV_DIR_FALLING : + tmp =FIELD_GET (PAC1944_UV_MASK , info->alert_enable); +break ; +default : +return -EINVAL ; + } +return -EINVAL ; +case IIO_CURRENT : +switch (dir) { +case IIO_EV_DIR_RISING : + tmp =FIELD_GET (PAC1944_OC_MASK , info->alert_enable); +break ; +case IIO_EV_DIR_FALLING : + tmp =FIELD_GET (PAC1944_UC_MASK , info->alert_enable);
drivers/iio/adc/pac1944.c line 2097
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+break ; +default : +return -EINVAL ; + } +return -EINVAL ; +case IIO_POWER : +if (dir ==IIO_EV_DIR_RISING ) + tmp =FIELD_GET (PAC1944_OP_MASK , info->alert_enable); +else +return -EINVAL ;
Flip logic to simplify.
if (dir != IIO_EV_DIR_RISING) return -EINVal tmp = FIELD_GET(...
Of just use a switch to keep it looking like the other code.
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+break ; +default : +return -EINVAL ; + } + } + + tmp = tmp >> (3 - idx); + +return tmp &0x01 ; +} + +static int pac1944_write_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type,
drivers/iio/adc/pac1944.c line 2121
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ enum iio_event_direction dir, + bool state) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client; +int idx, val, mask, ret; +bool update = false; +u8 tmp[PAC1944_ALERT_ENABLE_REG_LEN ]; + +/* into the datasheet channels are noted from 1 to 4 */
In the datasheet channels are referred to as 1 to 4
Quoted 67 lines
drivers/iio/adc/pac1944.c new file +66
-0+ idx = chan->channel -1 ; + +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + mask = pac1944_overvoltage_mask_tbl[idx]; +break ; +case IIO_EV_DIR_FALLING : + mask = pac1944_undervoltage_mask_tbl[idx]; +break ; +default : +return -EINVAL ; + } +break ; +case IIO_CURRENT : +switch (dir) { +case IIO_EV_DIR_RISING : + mask = pac1944_overcurrent_mask_tbl[idx]; +break ; +case IIO_EV_DIR_FALLING : + mask = pac1944_undercurrent_mask_tbl[idx]; +break ; +default : +return -EINVAL ; + } +break ; +case IIO_POWER : +if (dir !=IIO_EV_DIR_RISING ) +return -EINVAL ; + + mask = pac1944_overpower_mask_tbl[idx]; +break ; +default : +return -EINVAL ; + } + +guard (mutex)(&info->lock); + + val = info->alert_enable & mask; +if (state && !val) { +/* enable the event in hardware */ + info->alert_enable |= mask; + update = true; + }else if (!state && val) { +/* disable the event in hardware */ + info->alert_enable &= ~mask; + update = true; + } + +/* do not update if not needed */ +if (update) { +put_unaligned_be24 (info->alert_enable, &tmp[0 ]); + +/* update the Alert enable register */ + ret =pac1944_restore_alert_reg (indio_dev, &tmp[0 ]); +if (ret) { +dev_err (&client->dev,"failing to restore alert reg\n" ); +return ret; + } + } + +return 0 ; +} + +static int pac1944_of_parse_channel_config(struct i2c_client *client,
drivers/iio/adc/pac1944.c
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ struct pac1944_chip_info *info) +{ +unsigned int current_channel; +struct device *dev = &client->dev; +int idx, ret, temp; +bool is_bipolar, is_half_fsr; + + current_channel =1 ; + +device_for_each_child_node_scoped (dev, child) {
Perhaps it is worth factoring out the parsing of a single channel as a helper function.
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+ ret =fwnode_property_read_u32 (child,"reg" , &idx); +if (ret) +return dev_err_probe (dev, ret,"reading invalid channel index\n" ); + +/* adjust idx to match channel index (1 to 4) from the datasheet */ + idx--; + +if (current_channel >= (info->phys_channels +1 ) || + idx >= info->phys_channels || idx <0 ) +return dev_err_probe (dev, -EINVAL ,"invalid channel index %d value\n" , + idx +1 ); + +/* enable channel */ +set_bit (idx, &info->active_channels_mask);
drivers/iio/adc/pac1944.c line 2331
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + 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: %d\n" , +fwnode_get_name (child), info->shunts[idx]); + +if (fwnode_property_present (child,"label" )) +fwnode_property_read_string (child,"label" , + (const char **)&info->labels[idx]);
Given you don't check for errors for fwnode_property_read_string() is this not the same after dropping the fwnode_property_present() check?
drivers/iio/adc/pac1944.c line 2335
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ + is_bipolar = false; +if (fwnode_property_present (child,"microchip,vbus-bipolar" )) + is_bipolar = true;
is_bipolar = fwnode_property_read_bool() probably appropriate here.
drivers/iio/adc/pac1944.c line 2339
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ + is_half_fsr = false; +if (fwnode_property_present (child,"microchip,vbus-half-range" )) + is_half_fsr = true;
same here and the other cases below.
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+ +/* default value is unipolar and Full Scale Range */ + 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 ; + + is_bipolar = false; +if (fwnode_property_present (child,"microchip,vsense-bipolar" )) + is_bipolar = true; + + is_half_fsr = false; +if (fwnode_property_present (child,"microchip,vsense-half-range" ))
drivers/iio/adc/pac1944.c line 2363
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ is_half_fsr = true; + +/* default value is unipolar and Full Scale Range */ + 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 ; + + ret =fwnode_property_read_u32 (child,"microchip,accumulation-mode" , &temp);
Given temp is only used for this, give it a meaninful name. acc_mode or something like that.
Quoted 17 lines
drivers/iio/adc/pac1944.c new file +17
-0+if (ret) +return dev_err_probe (dev, ret,"invalid accumulation-mode value on %s\n" , +fwnode_get_name (child)); +if (temp ==PAC1944_ACCMODE_VPOWER || + temp ==PAC1944_ACCMODE_VSENSE || + temp ==PAC1944_ACCMODE_VBUS ) { +dev_dbg (dev,"Accumulation{%d} mode set to: %d\n" , idx, temp); + info->chip_reg_data.accumulation_mode[idx] = temp; + }else { +return dev_err_probe (dev, -EINVAL , +"invalid mode for accumulator value on %s\n" , +fwnode_get_name (child)); + } + current_channel++; + } + +return 0 ;
drivers/iio/adc/pac1944.c line 2390
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+} + +static void pac1944_cancel_delayed_work (void *dwork) +{ +cancel_delayed_work_sync (dwork); +} + +static int pac1944_chip_identify (struct pac1944_chip_info *info) +{ +int ret =0 ;
Always set below so don't initialize.
drivers/iio/adc/pac1944.c line 2401
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+struct i2c_client *client = info->client; +u8 chip_rev_info[3 ]; + + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_PID_REG_ADDR , +sizeof (chip_rev_info), + chip_rev_info); +if (ret <0 ) +return dev_err_probe (&client->dev, ret,"cannot read revision\n" ); + +dev_info (&client->dev,"Chip revision: 0x%02X\n" , chip_rev_info[2 ]); + info->chip_revision = chip_rev_info[2 ];
Why do we keep a copy? Doesn't seem to be used again.
drivers/iio/adc/pac1944.c line 2411
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ info->chip_variant = chip_rev_info[0 ]; + +switch (chip_rev_info[0 ]) { +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;
This is a little odd. We set one element of info in here, but return the value of ret for the others. I'd be inclined to just set all the stuff in info directly in this function.
I'm also not sure why that is_pac194x_family isn't just encoded in the chip specific structures? Why do we need to do it explicitly here.
Quoted 21 lines
drivers/iio/adc/pac1944.c new file +21
-0+return chip_rev_info[0 ] -PAC_PRODUCT_ID_1941 ; +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 : + info->is_pac195x_family = true; +return (chip_rev_info[0 ] -PAC_PRODUCT_ID_1951 ) + + (PAC_PRODUCT_ID_1942_2 -PAC_PRODUCT_ID_1941 ) +1 ; +default : +dev_err (&client->dev, +"product ID (0x%02X, 0x%02X, 0x%02X) for this part doesn't match\n" , + chip_rev_info[0 ], chip_rev_info[1 ], chip_rev_info[2 ]); +return -EINVAL ; + } +} + +static int pac1944_chip_configure (struct pac1944_chip_info *info) +{ +int cnt, ret;
drivers/iio/adc/pac1944.c line 2442
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+struct i2c_client *client = info->client; +u8 regs[PAC1944_ALERTS_REG_LEN ]; +u8 *offset_p; +u32 wait_time; +u8 tmp_u8; +__be16 tmp_be16; +u16 cfg; + +/* +* Counting how many channels are enabled and store
Count how many...
drivers/iio/adc/pac1944.c line 2443
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+* this information within the driver data
Wrap at 80 chars. Also, when it's a sentence, nice to add a . at the end.
Quoted 15 lines
drivers/iio/adc/pac1944.c new file +15
-0+*/ + info->num_enabled_channels =hweight_long (info->active_channels_mask); + +/* get sampling rate from PAC */ + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_CTRL_REG_ADDR , +sizeof (tmp_be16), (u8 *)&tmp_be16); +if (ret <0 ) +return dev_err_probe (&client->dev, ret,"cannot read CTRL reg\n" ); + + info->sampling_mode =FIELD_GET (PAC1944_CTRL_SAMPLE_MASK ,be16_to_cpu (tmp_be16)); + +/* +* The current/voltage can be measured unidirectional, bidirectional or half FSR +* no SLOW triggered REFRESH, clear POR +*/
drivers/iio/adc/pac1944.c line 2468
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ cfg =FIELD_PREP (PAC1944_NEG_PWR_CFG_VS1_MASK , info->chip_reg_data.vsense_mode[0 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VS2_MASK , info->chip_reg_data.vsense_mode[1 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VS3_MASK , info->chip_reg_data.vsense_mode[2 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VS4_MASK , info->chip_reg_data.vsense_mode[3 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VB1_MASK , info->chip_reg_data.vbus_mode[0 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VB2_MASK , info->chip_reg_data.vbus_mode[1 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VB3_MASK , info->chip_reg_data.vbus_mode[2 ]) | +FIELD_PREP (PAC1944_NEG_PWR_CFG_VB4_MASK , info->chip_reg_data.vbus_mode[3 ]); + + ret =i2c_smbus_write_word_data (client,PAC1944_NEG_PWR_FSR_REG_ADDR ,cpu_to_be16 (cfg));
Been a while, but from what I recall smbus has a defined byte ordering. So independent of CPU endianness if a u16 is written it should end up in the same order on the bus. For that reason the swaps for word_data read/write, if needed, should be unconditional.
ret = i2c_smbus_write_word_swapped()
I'm also a little curious that a device supports smbus_write_word, but not smbus_read_word which would simplify the read above? Looking at the family datasheet I'm not seeing either word protocol as supported.
For the block access above, it's a different game as there is no ordering on bus going on (as we don't know the word length) hence the swap is correctly be16_to_cpu().
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+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) |
drivers/iio/adc/pac1944.c line 2489
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-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_data (client,PAC1944_CTRL_REG_ADDR ,cpu_to_be16 (cfg)); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write CTRL reg\n" ); + + tmp_u8 = ACCUM_REG(info->chip_reg_data.accumulation_mode[0],
Might as well give tmp_u8 a more meaningful name as it is only used for this.
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-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 , tmp_u8); +if (ret) +return dev_err_probe (&client->dev, ret,"cannot write ACCUM_CFG reg\n" ); + +/* reading all alerts, status and limits related registers */ + ret =pac1944_i2c_read (client,PAC1944_ALERT_STATUS_REG_ADDR , regs,sizeof (regs)); +if (ret <0 ) +return dev_err_probe (&client->dev, ret,"cannot read ALERT_STATUS reg\n" ); + + offset_p = ®s[0 ];
...
drivers/iio/adc/pac1944.c line 2678
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+static int pac1944_probe (struct i2c_client *client) +{ +struct pac1944_chip_info *info; +struct iio_dev *indio_dev; +const struct pac1944_features *chip; +int cnt, ret; +struct device *dev = &client->dev;
Where there is no other reason to pick an order for declarations, go with reverse xmas tree. It's not important (to me anyway) but might as well pick a style and keep to it.
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+ + 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 (info); +if (ret <0 ) { +dev_dbg (dev,"Failed to identify the device based on HW ID\n" );
drivers/iio/adc/pac1944.c line 2699
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* +* 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->chip_variant = chip->prod_id;
What is this for? It's set but not I think used.
drivers/iio/adc/pac1944.c line 2704
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+ info->phys_channels = chip->phys_channels; + indio_dev->name = chip->name; + }else { + info->phys_channels = pac1944_chip_config[ret].phys_channels; + indio_dev->name = pac1944_chip_config[ret].name;
This doesn't handle the the family bit of info or setting chip_variant. So looks like it is missing at least some stuff overthe case where we don't match.
Quoted 58 lines
drivers/iio/adc/pac1944.c new file +58
-0+ } + +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +/* 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); + +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); +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" ); + + ret =pac1944_reg_snapshot (info, true, false, +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)&pac1944_chip_config[PAC1941 ] }, + { .name ="pac19412" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1941_2 ] }, + { .name ="pac1942" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1942 ] }, + { .name ="pac19422" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1942_2 ] }, + { .name ="pac1943" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1943 ] }, + { .name ="pac1944" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1944 ] }, + { .name ="pac1951" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1951 ] }, + { .name ="pac19512" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1951_2 ] }, + { .name ="pac1952" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1952 ] }, + { .name ="pac19522" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1952_2 ] },
drivers/iio/adc/pac1944.c line 2772
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ { .name = "pac1953", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1953] }, + { .name = "pac1954", .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1954] }, + { } +}; +MODULE_DEVICE_TABLE (i2c, pac1944_id); + +static const struct of_device_id pac1944_of_match[] = { + { + .compatible ="microchip,pac1941" , + .data = (void *)&pac1944_chip_config[PAC1941 ]
Why do we need the cast to a void * ?
They should be const pointers anyway so assignment to a const void * should be fine without cast.
Quoted 30 lines
drivers/iio/adc/pac1944.c new file +29
-0+ }, + { + .compatible = "microchip,pac19412", + .data = (void *)&pac1944_chip_config[PAC1941_2] + }, + { } +}; +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[PAC1944 ] }, + { } +}; +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" );
Re: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 20 lines · Jonathan Cameron
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 6 lines · On Fri, 6 Jun 2025 12:39:29 +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]>Hi Marius,
Quoted 7 lines
I entirely agree with David on this wanting splitting up into a base driver + patches that add features to that in order to make it easier to review. That also potentially allows me to pick up the basic support whilst any more controversial parts are still under discussion.
Jonathan
I will try to make it smaller and build on that.
Quoted 37 lines
Quoted 19 lines
drivers/iio/adc/Makefile +0
-0@@ -103,6 +103,7 @@ obj-$(CONFIG_NCT7201) += nct7201.oobj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1944) += 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 +0
-0drivers/iio/adc/pac1944.c line 453
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +4
-0@@ -0,0 +1,2841 @@ +/* Available Sample Modes */ +static const char *const pac1944_frequency_avail[] = { + "1024_ADAP", + "256_ADAP",This adaptive mode shouldn't be controlled via this standard ABI. That needs to be considered separately.
Quoted 6 lines
+ "64_ADAP",
+ "8_ADAP",
+ "1024",
+ "256",
+ "64",
+ "8",This does not look even close to ABI complaint.
Quoted 12 lines
The numbers cases are fine. The others not really.
Quoted 1 line
+ "
single_shot_1x",That has nothing directly to do with the sampling frequency. Some others look suspicious. I'd stick to normal
sampling_frequencyhandling and have the discussion about the other modes in here at a later date.Quoted 4 lines
+ "
single_shot_8x",
+ "fast",
+ "burst",
+};
The adaptive mode is used to lower the power consumption of the device and keep the "same" calculation into the driver. I will try to look over an ABI to put the device into a low power mode and in this way I could change the frequency to ADAPTIVE.
The "single_shot_1x" and "single_shot_8x" is a special case when the user could take only one measurement and that measurement could be triggered/synched.
Quoted 2 lines
Quoted 2 lines
...
Quoted 15 lines
Quoted 5 lines
Quoted 9 lines
+static struct attribute *pac1944_power_acc_attr[] = { + &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,
Quoted 7 lines
These look like standard
read_raw/info_maskbased attributes will work.
So do that, not custom attributes that are both harder to review and don't work with in kernel consumers (which we may well see for a power
monitoring chip).
Because the device could support different modes of operation for the same accumulator, but only one at a time. I was trying to add to the sysfs only the mode that was set from the device tree.
Quoted 2 lines
Jonathan Cameron
maintainerRe: [PATCH v3 2/2] iio: adc: adding support for PAC194X
Quoted 81 lines · On Tue, 10 Jun 2025 15:58:13 +0000 <[email protected]> wrote:
Quoted 20 lines · Jonathan Cameron
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 6 lines · On Fri, 6 Jun 2025 12:39:29 +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]>Hi Marius,
Quoted 7 lines
I entirely agree with David on this wanting splitting up into a base driver + patches that add features to that in order to make it easier to review. That also potentially allows me to pick up the basic support whilst any more controversial parts are still under discussion.
Jonathan
I will try to make it smaller and build on that.
Quoted 37 lines
Quoted 19 lines
drivers/iio/adc/Makefile +0
-0@@ -103,6 +103,7 @@ obj-$(CONFIG_NCT7201) += nct7201.oobj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o +obj-$(CONFIG_PAC1944) += 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 +0
-0drivers/iio/adc/pac1944.c line 453
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +4
-0@@ -0,0 +1,2841 @@ +/* Available Sample Modes */ +static const char *const pac1944_frequency_avail[] = { + "1024_ADAP", + "256_ADAP",This adaptive mode shouldn't be controlled via this standard ABI. That needs to be considered separately.
Quoted 6 lines
+ "64_ADAP",
+ "8_ADAP",
+ "1024",
+ "256",
+ "64",
+ "8",This does not look even close to ABI complaint.
Quoted 12 lines
The numbers cases are fine. The others not really.
Quoted 1 line
+ "
single_shot_1x",That has nothing directly to do with the sampling frequency. Some others look suspicious. I'd stick to normal
sampling_frequencyhandling and have the discussion about the other modes in here at a later date.Quoted 4 lines
+ "
single_shot_8x",
+ "fast",
+ "burst",
+};
Quoted 4 lines
The adaptive mode is used to lower the power consumption of the device and keep the "same" calculation into the driver. I will try to look over an ABI to put the device into a low power mode and in this way I could change the frequency to ADAPTIVE.
We deliberately don't support 'mode' type switches for power saving in the ABI. They are just too varied for userspace to 'generically' know when to turn them on. Snag here is that it is a straight trade off between a clever algorithm which will introduce some error and straight forward sampling at high frequency. I don't currently have a good idea on how we'd control that. If we also had a dataready trigger involved (rather than accumulating etc) then maybe we'd do it via trigger selection, but that's not appropriate here given this is about driving the accumulators, not a dataready style readback of data (if we did that, we could just accumulate in user space instead of the device - a very different thing!).
Quoted 4 lines
The "
single_shot_1x" and "single_shot_8x" is a special case when the user could take only one measurement and that measurement could be triggered/synched.
8x seems to be about oversampling - but on a single trigger. In that sense it is a bit unusual. Looks like we could only do it effectively on an external IIO trigger? e.g. hrtimer or something like that?
single shot 1x looks like a similar mode to use only on an external trigger (or a sysfs read). Can we just enable that only if we aren't using this devices own trigger?
fast vs burst is an open question but taking just say fast that ends up being just another sampling frequency, be it one that changes with number of enabled channels.
Quoted 28 lines
Quoted 3 lines
Quoted 3 lines
...
Quoted 15 lines
Quoted 5 lines
Quoted 9 lines
+static struct attribute *pac1944_power_acc_attr[] = { + &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,
Quoted 7 lines
These look like standard
read_raw/info_maskbased attributes will work.
So do that, not custom attributes that are both harder to review and don't work with in kernel consumers (which we may well see for a power
monitoring chip).
Quoted 3 lines
Because the device could support different modes of operation for the same accumulator, but only one at a time. I was trying to add to the sysfs only the mode that was set from the device tree.
Create the iio_chan_spec dynamically instead of the attributes added.
Jonathan
Quoted 3 lines
Quoted 3 lines