Add support for Microchip PAC1711 Power Monitor
Subsystems (3)
v4 cover Add support for Microchip PAC1711 Power Monitor
The PAC1711, PAC1721, PAC1811 and PAC1821 products are single-channel power monitors with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
Reasons why these devices are more suitable for the IIO subsystem:
1. Hardware capabilities and sampling rates:
The device is fundamentally a high-speed ADC with advanced hardware features (e.g., measuring low voltage over a current shunt, calculate power and/or acumulate power/current). When
configured to sample a single channel (e.g. current exclusively), the PAC1711 can achieve sampling rates of up to 16,384 samples/second. High-rate sampling is specifically required to enable accurate power/current profiling.
2. Future Features:
The currently submitted version of the driver provides the basic hardware interface that could be suitable for HWMON, but later on it is needed to introduce features such as IIO buffer support. High-frequency buffered data capture is essential for profiling high-resolution current consumption on processors, peripherals, battery systems, and industrial low-voltage automation.
3. Consistency:
It maintains a unified API across the entire Microchip power and current monitor device family with similar hardware capabilities.
---
Changes in v4:
- fix review comments device tree binding:
move the reasoning for defining accumulation-source as a DT property
from the binding description to the commit message
- fix review comments driver:
add a new patch in the series to add IIO_CHARGE as a standard channel
type (instead of using custom attributes for Coulomb Counter)
remove chip_variant from pac1711_chip_info struct
remove TO_PAC1711_CHIP_INFO define
use reverse xmas and correct other coding style issues flagged
use dev_err_probe() instead of dev_err() in pac1711_chip_identify() correct sample rate update
restructure pac1711_features struct to reflect hardware
differencies
rewrite defines using unit defines such as MILLI, MICRO
add comment to explain sample rate update wait time
add device_property_present() check where needed
add porposed function pac1711_get_features_from_id() to be used as a
helper in pac1711_chip_identify()
remove early return in pac1711_set_acc_enable()
protected 64-bit read operations with guards
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- fix review comments device tree binding:
remove interrupts and GPIO properties
change VSENSEP to VSENSE+, VSENSEN to VSENSE-
explain why we need the accumulation mode as a devicetree property
- fix review comments driver:
use NANO macro in defines
use anonymous enums
rename is_pac18x1_family to has_16bit_resolution
use more meaningful names for local variables instead of tmp use guard instead of scoped_guard where possible
replace 'Increasing precision' comment from
pac1711_in_coulomb_counter_scale_show()
rename pac1711_work_periodic_rfsh() to pac1711_work_periodic_refresh() remove ret initialization from pac1711_chip_configure() add explanation for background worker instead of polling data in a
comment
remove dynamic allocation of the always available channels return 0 in the end of pac1711_retrieve_data()
remove comma after NULL entries
use FIELD_MODIFY() instead of FIELD_PREP()
use consistent one line style
verify length instead of return code for I2C reads
remove return error code when accumulator overflows correct refresh logic and jiffies timestamp calculation add mutex in pac1711_in_power_acc_raw_show()
add mutex when reading power raw values
correct wait_time calculation
initialize all struct fields when identifying the chip fails handle return code correctly for i2c_smbus_read_i2c_block_data() correct power scale calculation to avoid overflow
change vals type from s32 to u32 in pac1711_init_vbus_vsense_ranges() use IIO_ENERGY instead of custom attribute for vpower accumulation add more comments to explain accumulation functionality validate I2C transfer lengths and return -EIO for short transfers
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- fix review comments device tree binding:
add PAC1721, PAC1811 and PAC1821 part numbers
add Vbus/Vsense input ranges in attribute definition change accumulation-mode from int to string type remove size and address cells
correct interrupts definition and add attributes for the two
alerts, microchip,gpio0-mode and microchip,gpio1-mode
remove microchip,gpio attribute
remove "vbus" accumulation mode
- fix review comments driver:
add PAC1721, PAC1811 and PAC1821 part numbers
run pahole on reg_data and pac1711_chip_info structs remove average registers - VBUS_AVG and VSENSE_AVG
add PAC1721, PAC1811, PAC1821 to features/compatible
add missing headers
remove rarely used defines like PAC1711_POWER_24B_RES and use the
numerical value inline instead
use ARRAY_SIZE() instead of define for the number of accumulator
related attributes
add explanation for bytes length defines
use spacing convention space after { and before }
remove pac1711_shift_map_tbl in order to use just
pac1711_samp_rate_map_tbl and an index saved in struct
use read_avail() for sampling_rate
change mutex comment in reg_data
use fsleep instead of usleep
use a local __be16 variable in endianess transformations add missing error returns after dev_err
add info_mask_shared_by_all for sampling_frequency
use dev_info instead of dev_err_probe in chip_identify generalize input setup functions into one
remove device_property_present
rename pac1711_single_channel into pac1711_chan_spec rename pac1711_of_parse_channel_config into pac1711_parse_fw remove unneccessary comments in probe
define in_shunt_resistor as ext_info instead of custom attribute use calculations only with 16-bit resolution instead of multiple
shifting (12-bit registers are left shifted)
add scale computations based on new voltage of 9V for PAC1721/PAC1821
v1:
- first version committed to review
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Ariana Lazar (3):
iio: core: add IIO_CHARGE channel type
dt-bindings: iio: adc: add support for PAC1711
iio: adc: add support for PAC1711
Documentation/ABI/testing/sysfs-bus-iio | 13 + .../bindings/iio/adc/microchip,pac1711.yaml | 169 +++ MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1711.c | 1231 ++++++++++++++++++++ drivers/iio/industrialio-core.c | 1 + include/uapi/linux/iio/types.h | 1 + tools/iio/iio_event_monitor.c | 2 + 9 files changed, 1436 insertions(+)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20250901-pac1711-d3bacda400fd
Best regards,
Signature · 1 line
Ariana Lazar <[email protected]>
PATCH v4 1/3 iio: core: add IIO_CHARGE channel type
Add a new channel type for Coulomb counters, which accumulate current measurements over time. The sysfs attribute is in_chargeY_raw and after applying in_chargeY_scale, the value exposed is in milliCoulombs, following the SI convention used by the IIO subsystem.
This new channel type will first be used by the pac1711 driver to report Coulomb counter measurements.
--- Documentation/ABI/testing/sysfs-bus-iio | 13 +++++++++++++ drivers/iio/industrialio-core.c | 1 + include/uapi/linux/iio/types.h | 1 + tools/iio/iio_event_monitor.c | 2 ++ 4 files changed, 17 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +13 -0
@@ -510,6 +510,8 @@ What: /sys/bus/iio/devices/iio:deviceX/in_intensity_blue_scale What: /sys/bus/iio/devices/iio:deviceX/in_concentration_co2_scale What: /sys/bus/iio/devices/iio:deviceX/in_volumeflow_scale What: /sys/bus/iio/devices/iio:deviceX/in_volumeflowY_scale +What: /sys/bus/iio/devices/iio:deviceX/in_charge_scale +What: /sys/bus/iio/devices/iio:deviceX/in_chargeY_scale KernelVersion: 2.6.35 Contact: [email protected] Description:
@@ -1781,6 +1783,7 @@ What: /sys/.../iio:deviceX/in_energy_en What: /sys/.../iio:deviceX/in_distance_en What: /sys/.../iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_en What: /sys/.../iio:deviceX/in_steps_en +What: /sys/.../iio:deviceX/in_charge_en KernelVersion: 3.19 Contact: [email protected] Description:
@@ -2509,3 +2512,13 @@ Description: Raw (unscaled) volumetric flow rate reading from the channel. To convert to standard units (cubic metres per second, m^3/s) apply the channel's _scale (and _offset, when present). + +What: /sys/bus/iio/devices/iio:deviceX/in_charge_raw +What: /sys/bus/iio/devices/iio:deviceX/in_chargeY_raw +KernelVersion: 7.4 +Contact: [email protected] +Description: + Raw charge value read from the channel. The device acts as + a Coulomb counter by accumulating current measurements over + time. Units after application of scale are milliCoulombs. X + is the IIO index of the device.
drivers/iio/industrialio-core.c +1 -0
@@ -100,6 +100,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_ALTCURRENT] = "altcurrent", [IIO_COVERAGE] = "coverage", [IIO_VOLUMEFLOW] = "volumeflow", + [IIO_CHARGE] = "charge", };static const char *const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
@@ -55,6 +55,7 @@ enum iio_chan_type {IIO_ALTCURRENT ,IIO_COVERAGE ,IIO_VOLUMEFLOW , + IIO_CHARGE, }; enum iio_modifier {
tools/iio/iio_event_monitor.c +2 -0
@@ -67,6 +67,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_ALTCURRENT] = "altcurrent", [IIO_COVERAGE] = "coverage", [IIO_VOLUMEFLOW] = "volumeflow", + [IIO_CHARGE] = "charge", };static const char *const iio_ev_type_text[] = {
@@ -198,6 +199,7 @@ static bool event_is_known(struct iio_event_data *event)case IIO_ALTCURRENT :case IIO_COVERAGE :case IIO_VOLUMEFLOW : +case IIO_CHARGE :break ;default :return false;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 1/3] iio: core: add IIO_CHARGE channel type
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Add a new channel type for Coulomb counters, which accumulate current measurements over time. The sysfs attribute is
in_chargeY_rawand after applyingin_chargeY_scale, the value exposed is in milliCoulombs, following the SI convention used by the IIO subsystem.This new channel type will first be used by the pac1711 driver to report Coulomb counter measurements.
Signed-off-by Ariana Lazar <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 2/3 dt-bindings: iio: adc: add support for PAC1711
In reply to Ariana LazarThis is the device tree schema for Microchip PAC1711, PAC1721, PAC1811 and PAC1821 single-channel power monitor with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
The accumulation-source property aims to specify what kind of hardware is intended to be used/available for the user. There are two main cases here:
- the user wants to measure also the current/power consumed before the driver insertion (e.g. from the boot to user control) and if this is a runtime setting, the hardware accumulator will be reset by the default configuration the driver starts with.
- the driver does not know what type of hardware it's dealing with. In case the part is monitoring the charge/discharge current it does not make sense in user-space to change the accumulator to calculate energy. Same if the hardware is intended to calculate energy it does not make sense in user-space to change to Coulomb counter. Changing the setting from one mode to another will reset the hardware accumulator inside the chip.
--- .../bindings/iio/adc/microchip,pac1711.yaml | 169 +++++++++++++++++++++ MAINTAINERS | 6 + 2 files changed, 175 insertions(+)
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml new file +169 -0
@@ -0,0 +1,169 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/microchip,pac1711.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip PAC1711 Power Monitors with Accumulator + +maintainers: + - Ariana Lazar <[email protected]> + +description: | + This device is part of the Microchip family of Power Monitors with Accumulator. + Datasheet links: + [PAC1711]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/PAC1711-Data-Sheet-DS20007058.pdf + [PAC1721]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/PAC1721-Single-Channel-Power-Monitor-with-Accumulator-DS20007088.pdf + [PAC1811]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC1811-Data-Sheet-DS20007066.pdf + [PAC1821]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC1821-Data-Sheet-DS20007097.pdf + + PAC1711, PAC1721, PAC1811 and PAC1821 are Single Channel Power Monitors + with Accumulator, having 12-bit or 16-bit resolution. The devices PAC1711 + and PAC1811 can measure up to 42V Full-Scale Range, respectively 9V + Full-Scale Range for PAC1721 and PAC1821. + +properties: + compatible: + enum: + - microchip,pac1711 + - microchip,pac1721 + - microchip,pac1811 + - microchip,pac1821 + + reg: + maxItems: 1 + + vdd-supply: true + + "#io-channel-cells": + const: 1 + + shunt-resistor-micro-ohms: + description: + Value in micro Ohms of the shunt resistor connected between the VSENSE+ + and VSENSE- inputs, across which the current is measured. This value is + needed to compute the scaling of the measured current. + + label: + description: Unique name to identify which device this is. + + microchip,vbus-input-range-microvolt: + description: | + Specifies the voltage range in microvolts chosen for the voltage full + scale range (FSR). The range should be set as <minimum, maximum> by + hardware design and should not be changed during runtime. + + The VBUS could be configured into the following full scale range: + - for PAC1711 or PAC1811: + - VBUS has unipolar 0V to 42V FSR (default) + - VBUS has bipolar -42V to 42V FSR + - VBUS has bipolar -21V to 21V FSR + - for PAC1721 or PAC1821: + - VBUS has unipolar 0V to 9V FSR (default) + - VBUS has bipolar -9V to 9V FSR + - VBUS has bipolar -4.5V to 4.5V FSR + + microchip,vsense-input-range-microvolt: + description: | + Specifies the voltage range in microvolts chosen for the current full + scale range (FSR). The current is calculated by dividing the vsense + voltage by the value of the shunt resistor. The range should be set as + <minimum, maximum> 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 0 mV to 100 mV FSR (default) + - VSENSE has bipolar -100 mV to 100 mV FSR + - VSENSE has bipolar -50 mV to 50 mV FSR + oneOf: + - items: + - const: 0 + - const: 100000 + - items: + - const: -100000 + - const: 100000 + - items: + - const: -50000 + - const: 50000 + + microchip,accumulation-source: + $ref: /schemas/types.yaml#/definitions/string + description: | + The Hardware Accumulator may be used to accumulate VPOWER or VSENSE values + for any channel. By setting the accumulator for a channel to accumulate + the VPOWER values gives a measure of accumulated power over a time period, + which is equivalent to energy. Setting the accumulator for a channel to + accumulate VSENSE values gives a measure of accumulated current, which is + equivalent to charge. + + The Hardware Accumulator could be configured as: + "energy" - Accumulator accumulates VPOWER (energy) + "charge" - Accumulator accumulates VSENSE (Coulomb Counter) + enum: [energy, charge] + default: energy + +required: + - compatible + - reg + - vdd-supply + - shunt-resistor-micro-ohms + +allOf: + - if: + properties: + compatible: + pattern: "^microchip,pac1[78]11$" + then: + properties: + microchip,vbus-input-range-microvolt: + oneOf: + - items: + - const: 0 + - const: 42000000 + - items: + - const: -42000000 + - const: 42000000 + - items: + - const: -21000000 + - const: 21000000 + default: [0, 42000000] + - if: + properties: + compatible: + pattern: "^microchip,pac1[78]21$" + then: + properties: + microchip,vbus-input-range-microvolt: + oneOf: + - items: + - const: 0 + - const: 9000000 + - items: + - const: -9000000 + - const: 9000000 + - items: + - const: -4500000 + - const: 4500000 + default: [0, 9000000] + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@40 { + compatible = "microchip,pac1711"; + reg = <0x40>; + + shunt-resistor-micro-ohms = <11000000>; + label = "VDD3V3"; + vdd-supply = <&vdd>; + microchip,vbus-input-range-microvolt = <(-21000000) 21000000>; + microchip,vsense-input-range-microvolt = <(-50000) 50000>; + microchip,accumulation-source = "energy"; + }; + }; +...
MAINTAINERS +6 -0
@@ -17889,6 +17889,12 @@ F: Documentation/devicetree/bindings/nvmem/microchip,sama7g5-otpc.yaml F: drivers/nvmem/microchip-otpc.c F: include/dt-bindings/nvmem/microchip,sama7g5-otpc.h +MICROCHIP PAC1711 POWER/CURRENT MONITOR DRIVER +M: Ariana Lazar <[email protected]> +L: [email protected] +S: Supported +F: Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml + MICROCHIP PAC1921 POWER/CURRENT MONITOR DRIVER M: Matteo Martelli <[email protected]> L: [email protected]
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 2/3] dt-bindings: iio: adc: add support for PAC1711
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] dt-bindings: missing `contains:` in `compatible` property matching causes conflicting constraints --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dt-bindings: missing contains: in compatible property matching causes conflicting constraints --
commit fc07b1cc31cfe375fd7611b7a1adec3398c98d59
Author: Ariana Lazar <[email protected]>
dt-bindings: iio: adc: add support for PAC1711
This commit introduces device tree schema for the Microchip PAC1711, PAC1721, PAC1811, and PAC1821 single-channel power monitors. It outlines the required and optional properties, including accumulation sources and voltage ranges.
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml line 115
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml new file +5
-0+allOf: + - if: + properties: + compatible: + pattern: "^microchip,pac1[78]11$"
[Severity: High]
In device tree bindings, the compatible property is evaluated as an array of strings. Since the JSON Schema pattern keyword is restricted to string instances, applying it directly to an array instance causes it to be ignored and unconditionally evaluate to true.
Will this cause the condition to evaluate to true for all device tree instances? Should this use the contains keyword instead, such as
properties: compatible: contains: { pattern: "^microchip,pac1[78]11$" } ?
Quoted 3 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml new file +3
-0+ then: + properties: + microchip,vbus-input-range-microvolt:
[ ... ]
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml line 133
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml new file +4
-0+ - if: + properties: + compatible: + pattern: "^microchip,pac1[78]21$"
[Severity: High]
Because both this conditional block and the previous one apply pattern directly to the compatible property instead of using contains, both conditions will simultaneously evaluate to true.
Does this result in mutually exclusive constraints being applied to microchip,vbus-input-range-microvolt simultaneously? If the schema demands the array simultaneously match both the 42V and 9V constraints, won't this cause dt_binding_check validation to fail even for valid device trees like the provided example?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v4 2/3] dt-bindings: iio: adc: add support for PAC1711
In reply to Ariana LazarQuoted 24 lines · Ariana Lazar
This is the device tree schema for Microchip PAC1711, PAC1721, PAC1811 and PAC1821 single-channel power monitor with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
The accumulation-source property aims to specify what kind of hardware is intended to be used/available for the user. There are two main cases here:
- the user wants to measure also the current/power consumed before the driver insertion (e.g. from the boot to user control) and if this is a runtime setting, the hardware accumulator will be reset by the default configuration the driver starts with.
- the driver does not know what type of hardware it's dealing with. In case the part is monitoring the charge/discharge current it does not make sense in user-space to change the accumulator to calculate energy. Same if the hardware is intended to calculate energy it does not make sense in user-space to change to Coulomb counter. Changing the setting from one mode to another will reset the hardware accumulator inside the chip.
Quoted 6 lines
Signed-off-by Ariana Lazar <[email protected]>--- .../bindings/iio/adc/microchip,pac1711.yaml | 169 +++++++++++++++++++++ MAINTAINERS | 6 + 2 files changed, 175 insertions(+)
PATCH v4 3/3 iio: adc: add support for PAC1711
In reply to Ariana LazarThis is the iio driver for Microchip PAC1711, PAC1721, PAC1811 and PAC1821 single-channel power monitors with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
--- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1711.c | 1231 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1244 insertions(+)
MAINTAINERS +1 -0
@@ -17894,6 +17894,7 @@ M: Ariana Lazar <[email protected]> L: [email protected] S: Supported F: Documentation/devicetree/bindings/iio/adc/microchip,pac1711.yaml +F: drivers/iio/adc/pac1711.c MICROCHIP PAC1921 POWER/CURRENT MONITOR DRIVER M: Matteo Martelli <[email protected]>
drivers/iio/adc/Kconfig +11 -0
@@ -1320,6 +1320,17 @@ config NXP_SAR_ADC This driver can also be built as a module. If so, the module will be called nxp_sar_adc. +config PAC1711 + tristate "Microchip Technology PAC1711 driver" + depends on I2C + help + Say yes here to build support for Microchip Technology's PAC1711, + PAC1721, PAC1811 and PAC1821 Single-Channel Power Monitors with + Accumulator. + + This driver can also be built as a module. If so, the module + will be called pac1711. + config PAC1921 tristate "Microchip Technology PAC1921 driver" depends on I2C
drivers/iio/adc/Makefile +1 -0
@@ -114,6 +114,7 @@ obj-$(CONFIG_NAU7802) += nau7802.o obj-$(CONFIG_NCT7201) += nct7201.o obj-$(CONFIG_NPCM_ADC) += npcm_adc.o obj-$(CONFIG_NXP_SAR_ADC) += nxp-sar-adc.o +obj-$(CONFIG_PAC1711) += pac1711.o obj-$(CONFIG_PAC1921) += pac1921.o obj-$(CONFIG_PAC1934) += pac1934.o obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
drivers/iio/adc/pac1711.c new file +1231 -0
1,231 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 3/3] iio: adc: add support for PAC1711
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] iio: integer overflow in scale calculation yields negative scale - [High] iio: skipped conversion…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] iio: integer overflow in scale calculation yields negative scale
- [High] iio: skipped conversion wait during sample rate change causes corrupted reads
- [High] iio: fixed 50ms polling rate limits abort slow hardware conversions
- [Medium] iio: data race exposing un-sign-extended vbus/vsense and shunt to userspace
- [Medium] iio: missing devicetree fallback name assignment exposes (null) to sysfs
- [Medium] iio: TOCTOU race in pac1711_set_acc_enable due to split mutex scopes
- [Low] iio: incompatible pointer type assignment to vals triggers build failure --
commit 8dfe13196844f805df3c10995b75092fcc0ad04a
Author: Ariana Lazar <[email protected]>
iio: adc: add support for PAC1711
This commit adds the IIO driver for Microchip PAC1711, PAC1721, PAC1811 and PAC1821 single-channel power monitors with accumulator.
Quoted 5 lines
drivers/iio/adc/pac1711.c new file +0
-0
[ ... ]
drivers/iio/adc/pac1711.c line 456
Quoted 13 lines
drivers/iio/adc/pac1711.c new file +13
-0+static int pac1711_reg_snapshot (struct pac1711_chip_info *info,bool do_refresh, +u8 refresh_cmd,u32 wait_time,bool *refreshed) +{ +guard (mutex)(&info->lock); + +/* No hardware refresh is needed */ +if (refreshed) + *refreshed = false; + +/* Return early if the minimum polling time hasn't elapsed */ +if (!time_after (jiffies, info->chip_reg_data.jiffies_tstamp + +msecs_to_jiffies (PAC1711_MIN_POLLING_TIME_MS ))) +return 0 ;
[Severity: High]
Does this hardcoded 50ms check prematurely abort slow hardware conversions?
If the sample rate is configured to be slower than 50ms per conversion (for example, 8 samples per second takes 125ms), it seems polling just past 50ms will continuously trigger new refresh commands and reset the device before conversions complete.
[ ... ]
Quoted 2 lines
drivers/iio/adc/pac1711.c new file +2
-0+static int pac1711_set_acc_enable (struct pac1711_chip_info *info,int val) +{
[ ... ]
drivers/iio/adc/pac1711.c line 523
Quoted 8 lines
drivers/iio/adc/pac1711.c new file +8
-0+ scoped_guard(mutex, &info->lock) { +if (val == info->enable_acc) +return 0 ; + } + +cancel_delayed_work_sync (&info->work_chip_refresh); + + scoped_guard(mutex, &info->lock) {
[Severity: Medium]
Could this lock drop around cancel_delayed_work_sync() open a race condition where concurrent calls modify the device configuration?
It appears the device could be left in the opposite state of the requested configuration while still reporting success.
[ ... ]
Quoted 3 lines
drivers/iio/adc/pac1711.c new file +3
-0+static int pac1711_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{
[ ... ]
drivers/iio/adc/pac1711.c line 656
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =pac1711_retrieve_data (info,PAC1711_MIN_UPDATE_WAIT_TIME_US ); +if (ret) +return ret; + +switch (chan->type) { +case IIO_VOLTAGE : + *val = info->chip_reg_data.vbus; +return IIO_VAL_INT ;
[Severity: Medium]
Unlike the IIO_POWER case below which uses a scoped_guard, does reading info->chip_reg_data.vbus without taking info->lock expose readers to an intermediate un-sign-extended state?
In pac1711_reg_snapshot_locked(), vbus is updated in two steps:
info->chip_reg_data.vbus = get_unaligned_be16(offset_reg_data_p); if (info->vbus_mode != PAC1711_FULL_RANGE_UNIPOLAR) info->chip_reg_data.vbus = sign_extend32(info->chip_reg_data.vbus, 15);
[ ... ]
Quoted 2 lines
drivers/iio/adc/pac1711.c new file +2
-0+case IIO_CHAN_INFO_SCALE : +switch (chan->address) {
[ ... ]
Quoted 2 lines
drivers/iio/adc/pac1711.c new file +2
-0+case PAC1711_VPOWER_REG_ADDR : +case PAC1711_VACC_REG_ADDR :
[ ... ]
drivers/iio/adc/pac1711.c line 724
Quoted 9 lines
drivers/iio/adc/pac1711.c new file +9
-0+/* +* Power - uW - it will use the combined scale +* for current and voltage +* current(mA) * voltage(mV) = power (uW) +*/ + tmp = info->features->voltage_pv_fsr; + +do_div (tmp, info->shunt); + *val = (int )tmp;
[Severity: High]
Does casting the 64-bit unsigned tmp to a 32-bit signed int overflow if info->shunt is small?
For a 1 mOhm shunt, tmp could be 4,200,000,000, which would wrap around the maximum signed 32-bit integer and produce a negative scale factor.
[ ... ]
Quoted 3 lines
drivers/iio/adc/pac1711.c new file +3
-0+static int pac1711_write_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int val,int val2,long mask) +{
[ ... ]
Quoted 2 lines
drivers/iio/adc/pac1711.c new file +2
-0+case IIO_CHAN_INFO_SAMP_FREQ : + scoped_guard(mutex, &info->lock) {
[ ... ]
drivers/iio/adc/pac1711.c line 803
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+ info->sample_rate_idx = new_idx; + info->chip_reg_data.ctrl_act_reg = val_u16; + +/* Force register snapshot and timestamp update with a refresh. */ + refresh_time =msecs_to_jiffies (PAC1711_MIN_POLLING_TIME_MS ) +1 ; + info->chip_reg_data.jiffies_tstamp = jiffies - refresh_time; + } + +/* We must wait for cycle completion before updating the sample rate value. */ + ret =pac1711_retrieve_data (info, ((1024 *1000 ) / old_samp_rate));
[Severity: High]
Since info->lock is dropped here, can a concurrent reader intercept the artificially aged jiffies_tstamp and trigger a hardware refresh?
The original thread's call to pac1711_retrieve_data() could then see a fresh timestamp, skip the wait entirely, and return prematurely, leading to corrupted reads.
Additionally, since fsleep() occurs in pac1711_send_refresh() after the command is sent, the hardware has already snapshotted the state before the wait happens.
[ ... ]
drivers/iio/adc/pac1711.c line 1122
Quoted 7 lines
drivers/iio/adc/pac1711.c new file +7
-0+static int pac1711_read_avail (struct iio_dev *indio_dev,struct iio_chan_spec const *channel, +const int **vals,int *type,int *length,long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + *type =IIO_VAL_INT ; + *vals = pac1711_samp_rate_map_tbl;
[Severity: Low]
Does this incompatible pointer type assignment trigger a build failure with the compiler flag to treat warnings as errors?
pac1711_samp_rate_map_tbl is defined as an array of unsigned int, but vals expects a const int **.
[ ... ]
Quoted 2 lines
drivers/iio/adc/pac1711.c new file +2
-0+static int pac1711_probe (struct i2c_client *client) +{
[ ... ]
drivers/iio/adc/pac1711.c line 1159
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+ ret =pac1711_chip_identify (indio_dev, info); +if (ret == -ENODEV ) { +/* +* If it fails to identify the hardware based on internal +* registers, use compatible from devicetree. +*/ + info->features =i2c_get_match_data (client); +if (!info->features) +return -EINVAL ; + }else if (ret) {
[Severity: Medium]
If the devicetree fallback path is taken, it appears indio_dev->name is never initialized before calling devm_iio_device_register().
Will this expose a (null) sysfs attribute and break userspace tools?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
kernel test robot
build botRe: [PATCH v4 3/3] iio: adc: add support for PAC1711
In reply to Ariana Lazarkernel test robot noticed the following build warnings:
Hi Ariana,
kernel test robot noticed the following build warnings:
[auto build test WARNING on cee9395acd8043be0644b25c34bfa86623f2b935]
url: https://github.com/intel-lab-lkp/linux/commits/Ariana-Lazar/iio-core-add-IIO_CHARGE-channel-type/20261007-174153 base: cee9395acd8043be0644b25c34bfa86623f2b935 patch link: https://lore.kernel.org/r/20261007-pac1711-v4-3-6ef34d7a7947%40microchip.com patch subject: [PATCH v4 3/3] iio: adc: add support for PAC1711 config: s390-allyesconfig (https://download.01.org/0day-ci/archive/20261009/[email protected]/config) compiler: s390-linux-gcc (GCC) 16.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261009/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/iio/adc/pac1711.c:134:51: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073667551616' to '-42000000' [-Woverflow]
134 | [PAC1711_FULL_RANGE_BIPOLAR] = { -PAC1711_VBUS_FSR_UV, PAC1711_VBUS_FSR_UV },
| ^
drivers/iio/adc/pac1711.c:135:51: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073688551616' to '-21000000' [-Woverflow]
135 | [PAC1711_HALF_RANGE_BIPOLAR] = { -PAC1711_VBUS_FSR_HALF_UV,
| ^
drivers/iio/adc/pac1711.c:140:51: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073700551616' to '-9000000' [-Woverflow]
140 | [PAC1711_FULL_RANGE_BIPOLAR] = { -PAC1721_VBUS_FSR_UV, PAC1721_VBUS_FSR_UV },
| ^
drivers/iio/adc/pac1711.c:141:51: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073705051616' to '-4500000' [-Woverflow]
141 | [PAC1711_HALF_RANGE_BIPOLAR] = { -PAC1721_VBUS_FSR_HALF_UV,
| ^
drivers/iio/adc/pac1711.c:148:42: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073709451616' to '-100000' [-Woverflow]
148 | [PAC1711_FULL_RANGE_BIPOLAR] = { -PAC1711_VSENSE_FSR_UV, PAC1711_VSENSE_FSR_UV },
| ^
drivers/iio/adc/pac1711.c:149:42: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073709501616' to '-50000' [-Woverflow]
149 | [PAC1711_HALF_RANGE_BIPOLAR] = { -PAC1711_VSENSE_FSR_HALF_UV, PAC1711_VSENSE_FSR_HALF_UV },
| ^vim +134 drivers/iio/adc/pac1711.c
130
131 static const int pac1711_vbus_range_tbl[2][3][2] = {
132 [PAC1711_VOLTAGE_RANGE_IDX] = {
133 [PAC1711_FULL_RANGE_UNIPOLAR] = { 0, PAC1711_VBUS_FSR_UV },
> 134 [PAC1711_FULL_RANGE_BIPOLAR] = { -PAC1711_VBUS_FSR_UV, PAC1711_VBUS_FSR_UV }, 135 [PAC1711_HALF_RANGE_BIPOLAR] = { -PAC1711_VBUS_FSR_HALF_UV,
136 PAC1711_VBUS_FSR_HALF_UV },
137 },
138 [PAC1721_VOLTAGE_RANGE_IDX] = {
139 [PAC1711_FULL_RANGE_UNIPOLAR] = { 0, PAC1721_VBUS_FSR_UV },
140 [PAC1711_FULL_RANGE_BIPOLAR] = { -PAC1721_VBUS_FSR_UV, PAC1721_VBUS_FSR_UV },
141 [PAC1711_HALF_RANGE_BIPOLAR] = { -PAC1721_VBUS_FSR_HALF_UV,
142 PAC1721_VBUS_FSR_HALF_UV },
143 },
144 };
145
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Jonathan Cameron
maintainerRe: [PATCH v4 3/3] iio: adc: add support for PAC1711
In reply to Ariana LazarQuoted 11 lines · On Wed, 7 Oct 2026 17:41:55 +0300 Ariana Lazar <[email protected]>…
This is the iio driver for Microchip PAC1711, PAC1721, PAC1811 and PAC1821 single-channel power monitors with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
Signed-off-by Ariana Lazar <[email protected]>
+CC Neil as another user for the be56 patch in here.
I've suggested Ariana carry Neil's patch as well as a precursor to this one and that way I'll get it with whoever's series merges first.
A few minor comments inline. Coming together nicely.
Jonathan
Quoted 33 lines
drivers/iio/adc/pac1711.c new file +27
-0@@ -0,0 +1,1231 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* IIO driver for PAC1711 Single-Channel DC Power/Energy Monitor +* +* Copyright (C) 2025 Microchip Technology Inc. and its subsidiaries +* +* Author: Ariana Lazar <[email protected]> +* +* Datasheet links: +* [PAC1711]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/PAC1711-Data-Sheet-DS20007058.pdf +* [PAC1721]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/PAC1721-Single-Channel-Power-Monitor-with-Accumulator-DS20007088.pdf +* [PAC1811]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC1811-Data-Sheet-DS20007066.pdf +* [PAC1821]: https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC1821-Data-Sheet-DS20007097.pdf +*/ +#include <linux/array_size.h> +#include <linux/bits.h> +#include <linux/bitfield.h> +#include <linux/cleanup.h> +#include <linux/delay.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/jiffies.h> +#include <linux/kstrtox.h> +#include <linux/module.h> +#include <linux/math64.h> +#include <linux/mutex.h>
drivers/iio/adc/pac1711.c line 37
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+#include <linux/overflow.h> +#include <linux/property.h> +#include <linux/types.h> +#include <linux/unaligned.h> +#include <linux/units.h> +#include <linux/workqueue.h> +#include <asm/byteorder.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h>
I may be missing something, but please check if you still need this last include.
drivers/iio/adc/pac1711.c line 288
Quoted 6 lines
drivers/iio/adc/pac1711.c new file +6
-0+ +static inline u64 pac1711_get_unaligned_be56 (u8 *p) +{ +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 ]; +}
Neil Amstrong has a patch on list adding a generic version of this.
https://lore.kernel.org/all/20261013-topic-sm8x50-iio-max34417-adc-v3-2-d3604ae4a229@linaro.org/
If you can add that to start of your series, then I'll deal with picking it up with whoever's series lands first.
+CC Neil for info on this.
Quoted 5 lines
drivers/iio/adc/pac1711.c new file +5
-0+ +static int pac1711_reg_snapshot_locked (struct pac1711_chip_info *info,bool do_refresh, +u8 refresh_cmd,u32 wait_time) +{ +struct i2c_client *client = info->client;
...
drivers/iio/adc/pac1711.c line 421
Quoted 8 lines
drivers/iio/adc/pac1711.c new file +8
-0+ + offset_reg_data_p +=PAC1711_VACC_REG_LEN ; + +/* VBUS */ + info->chip_reg_data.vbus =get_unaligned_be16 (offset_reg_data_p); + +if (info->vbus_mode !=PAC1711_FULL_RANGE_UNIPOLAR ) + info->chip_reg_data.vbus =sign_extend32 (info->chip_reg_data.vbus,15 );
This is the sign extension vs read elsewhere bug that sashiko pointed out.
drivers/iio/adc/pac1711.c line 589
Quoted 7 lines
drivers/iio/adc/pac1711.c new file +7
-0+ +static const struct iio_chan_spec_ext_info pac1711_ext_info[] = { + { + .name ="in_shunt_resistor" , + .read = pac1711_read_shunt_resistor, + .write = pac1711_write_shunt_resistor, + .shared =IIO_SHARED_BY_ALL ,
Hmm. We'd normally put shared by all stuff in all channels, but shunt resistor is a bit unusual as it only affects a few of them.
I think I'd still put it in the lot.
Quoted 70 lines
drivers/iio/adc/pac1711.c new file +70
-0+ }, + { } +}; + +#define PAC1711_VBUS_CHANNEL (_index, _address) { \ + .type = IIO_VOLTAGE, \ + .address = (_address), \ + .indexed = 1, \ + .channel = (_index), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ +} + +#define PAC1711_VSENSE_CHANNEL (_index, _address) { \ + .type = IIO_CURRENT, \ + .address = (_address), \ + .indexed = 1, \ + .channel = (_index), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .ext_info = pac1711_ext_info, \ +} + +#define PAC1711_VPOWER_CHANNEL (_index, _address) { \ + .type = IIO_POWER, \ + .address = (_address), \ + .indexed = 1, \ + .channel = (_index), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ +} + +#define PAC1711_ACC_CHANNEL (_index, _address, _type) { \ + .type = (_type), \ + .address = (_address), \ + .indexed = 1, \ + .channel = (_index), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_ENABLE), \ + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ +} + +static int pac1711_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct pac1711_chip_info *info =iio_priv (indio_dev); +u64 tmp =0 ; +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =pac1711_retrieve_data (info,PAC1711_MIN_UPDATE_WAIT_TIME_US ); +if (ret) +return ret; + +switch (chan->type) { +case IIO_VOLTAGE : + *val = info->chip_reg_data.vbus; +return IIO_VAL_INT ; +case IIO_CURRENT : + *val = info->chip_reg_data.vsense; +return IIO_VAL_INT ;
drivers/iio/adc/pac1711.c line 669
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+case IIO_POWER : + scoped_guard(mutex, &info->lock) { + *val = (u32 )info->chip_reg_data.vpower; + *val2 = (u32)(info->chip_reg_data.vpower >>32 ); + } +return IIO_VAL_INT_64 ; +case IIO_ENERGY : +case IIO_CHARGE : + scoped_guard(mutex, &info->lock) { + *val = (u32 )info->chip_reg_data.acc_val;
Could use lower_32_bits / upper_32_bits.
Maybe we should just have a helper for this given it is becoming rather more common.
iio_fill_val_int_64(info->chip_reg_data.acc_val, val, val2);
I don't mind if you want to leave that a follow up for someone else to do though.
As below on preferring guard where we don't need to control the scope to part of a case block.
Quoted 6 lines
drivers/iio/adc/pac1711.c new file +6
-0+ *val2 = (u32)(info->chip_reg_data.acc_val >>32 ); + } +return IIO_VAL_INT_64 ; +default : +return -EINVAL ; + }
...
drivers/iio/adc/pac1711.c line 742
Quoted 3 lines
drivers/iio/adc/pac1711.c new file +3
-0+ } +case IIO_CHAN_INFO_SAMP_FREQ : + scoped_guard(mutex, &info->lock) {
Slightly preference for:
case IIO_CHAN_INFO_SAMP_FREQ: {
guard(mutex)(&info->lock);
*val = ...
return IIO_VAL_INT;
}mostly because it reduces the indent a little.
Quoted 11 lines
drivers/iio/adc/pac1711.c new file +11
-0+ *val = pac1711_samp_rate_map_tbl[info->sample_rate_idx]; + } +return IIO_VAL_INT ; +case IIO_CHAN_INFO_ENABLE : + scoped_guard(mutex, &info->lock) { + *val = info->enable_acc; + } +return IIO_VAL_INT ; +default : +return -EINVAL ; + }
drivers/iio/adc/pac1711.c line 854
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +7
-0+} + +static int pac1711_chip_identify (struct iio_dev *indio_dev,struct pac1711_chip_info *info) +{ +struct i2c_client *client = info->client; +struct device *dev = &client->dev; +u8 chip_rev_info[3 ] = {};
{ }; preferred. I picked the standard for IIO on this a while back as a fairly random choice. Not important really.
Quoted 30 lines
drivers/iio/adc/pac1711.c new file +30
-0+int ret; + + ret =i2c_smbus_read_i2c_block_data (client,PAC1711_PID_REG_ADDR , +sizeof (chip_rev_info), chip_rev_info); +if (ret !=sizeof (chip_rev_info)) { + ret = ret <0 ? ret : -EIO ; +return dev_err_probe (dev, ret,"product ID not recognized\n" ); + } + + info->features =pac1711_get_features_from_id (chip_rev_info[0 ]); +if (!info->features) +return -ENODEV ; + + indio_dev->name = info->features->name; + +return 0 ; +} + +static int pac1711_check_range (s32 *vals,bool is_vbus,unsigned int voltage_range_idx) +{ +int num_ranges =ARRAY_SIZE (pac1711_vbus_range_tbl[PAC1711_VOLTAGE_RANGE_IDX ]); +const int (*ranges)[3 ][2 ]; +int i; + +if (is_vbus) { +switch (voltage_range_idx) { +case PAC1711_VOLTAGE_RANGE_IDX : + ranges = &pac1711_vbus_range_tbl[PAC1711_VOLTAGE_RANGE_IDX ]; +break ; +case PAC1721_VOLTAGE_RANGE_IDX :
drivers/iio/adc/pac1711.c line 894
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+ ranges = &pac1711_vbus_range_tbl[PAC1721_VOLTAGE_RANGE_IDX ]; +break ; +default : +return -EINVAL ; + } + }else { + ranges = &pac1711_vsense_range_tbl; + } + +for (i =0 ; i < num_ranges; i++) {
for (unsigned int i = 0; i < num_ranges; i++)
look for other places where you can do this as keeps scope of i tighter and saves a line of code.
Quoted 16 lines
drivers/iio/adc/pac1711.c new file +16
-0+if (vals[0 ] == (*ranges)[i][0 ] && vals[1 ] == (*ranges)[i][1 ]) +return i; + } + +return -EINVAL ; +} + +static int pac1711_init_vbus_vsense_ranges (struct pac1711_chip_info *info,bool is_vbus) +{ +struct i2c_client *client = info->client; +struct device *dev = &client->dev; +const char *prop_name; +u32 vals[2 ]; +int ret; + +if (is_vbus)
drivers/iio/adc/pac1711.c line 920
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+ prop_name ="microchip,vbus-input-range-microvolt" ; + else + prop_name ="microchip,vsense-input-range-microvolt" ; + +if (device_property_present (dev, prop_name)) { + ret =device_property_read_u32_array (dev, prop_name, vals,2 ); +if (ret) +return dev_err_probe (dev, ret, +"Cannot read %s property\n" , prop_name); +
Perhaps add a comment here that we really do want to treat them as signed.
Quoted 38 lines
drivers/iio/adc/pac1711.c new file +38
-0+ ret =pac1711_check_range ((s32 *)vals, is_vbus, + info->features->voltage_range_idx); +if (ret <0 ) +return dev_err_probe (dev, ret, +"Invalid value %u, %u for prop %s\n" , + vals[0 ], vals[1 ], prop_name); + }else { + ret =PAC1711_FULL_RANGE_UNIPOLAR ; + } + +if (is_vbus) + info->vbus_mode = ret; +else + info->vsense_mode = ret; + +return 0 ; +} + +static int pac1711_parse_fw (struct i2c_client *client,struct pac1711_chip_info *info) +{ +struct device *dev = &client->dev; +const char *acc_mode; +int ret; + + ret =device_property_read_u32 (dev,"shunt-resistor-micro-ohms" , &info->shunt); +if (ret) +return dev_err_probe (dev, ret,"Shunt resistor property error\n" ); + +if (!info->shunt) +return dev_err_probe (dev, -EINVAL ,"Invalid value for shunt resistor\n" ); + + ret =pac1711_init_vbus_vsense_ranges (info, true); +if (ret) +return ret; + + ret =pac1711_init_vbus_vsense_ranges (info, false); +if (ret) +return ret;
drivers/iio/adc/pac1711.c line 968
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+ +if (device_property_present (dev,"microchip,accumulation-source" )) { + ret =device_property_read_string (dev,"microchip,accumulation-source" , &acc_mode); +if (ret) +return dev_err_probe (dev, ret,"Cannot read accumulation-source\n" ); + +if (!strcmp (acc_mode,PAC1711_ACC_ENERGY_STR )) + info->accumulation_mode =PAC1711_ACCMODE_VPOWER ; +else if (!strcmp (acc_mode,PAC1711_ACC_CHARGE_STR )) + info->accumulation_mode =PAC1711_ACCMODE_VSENSE ;
Can we use fwnode_property_match_property_string() for this? Could add a device variant for that to make it simpler still.
Quoted 11 lines
drivers/iio/adc/pac1711.c new file +11
-0+else +return dev_err_probe (dev, -EINVAL , +"invalid accumulation-source value %s\n" , acc_mode); + }else { + info->accumulation_mode =PAC1711_ACCMODE_VPOWER ; + acc_mode =PAC1711_ACC_ENERGY_STR ; + } + +dev_dbg (dev,"Accumulation source set to: %s\n" , acc_mode); + +return 0 ;
drivers/iio/adc/pac1711.c
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +8
-0+} +static int pac1711_setup_channels (struct iio_dev *indio_dev,struct pac1711_chip_info *info) +{ +switch (info->accumulation_mode) { +case PAC1711_ACCMODE_VPOWER : + indio_dev->channels = pac1711_energy_chan_spec; + indio_dev->num_channels =ARRAY_SIZE (pac1711_energy_chan_spec); +break ;
As you are respinning anyway, I'd slightly prefer a return 0 instead of break. Saves a couple of lines of code and looking down to see if anything else happens. If that means more churn for some future feature patch you haven't posted yet feel free to ignore this.
Quoted 8 lines
drivers/iio/adc/pac1711.c new file +8
-0+case PAC1711_ACCMODE_VSENSE : + indio_dev->channels = pac1711_charge_chan_spec; + indio_dev->num_channels =ARRAY_SIZE (pac1711_charge_chan_spec); +break ; +default : +return -EINVAL ; + } +
drivers/iio/adc/pac1711.c line 1122
Quoted 10 lines
drivers/iio/adc/pac1711.c new file +10
-0+return 0 ; +} + +static int pac1711_read_avail (struct iio_dev *indio_dev,struct iio_chan_spec const *channel, +const int **vals,int *type,int *length,long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + *type =IIO_VAL_INT ; + *vals = pac1711_samp_rate_map_tbl;
I think sashiko mentioned this. The map_tbl is unsigned yet we are using it
as signed here. Maybe just make it signed? Looks like all the values fit anyway.
Quoted 6 lines
drivers/iio/adc/pac1711.c new file +6
-0+ *length =ARRAY_SIZE (pac1711_samp_rate_map_tbl); +return IIO_AVAIL_LIST ; + } + +return -EINVAL ; +}
Re: [PATCH v4 3/3] iio: adc: add support for PAC1711
Hi Jonathan,
Thank you for the review.
Quoted 11 lines
drivers/iio/adc/pac1711.c line 288
Quoted 5 lines
drivers/iio/adc/pac1711.c new file +5
-0+static inline u64 pac1711_get_unaligned_be56 (u8 *p) +{ + 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]; +}Neil Amstrong has a patch on list adding a generic version of this.
https://lore.kernel.org/all/20261013-topic-sm8x50-iio-max34417-adc-v3-2-d3604ae4a229@linaro.org/If you can add that to start of your series, then I'll deal with picking it up with whoever's series lands first.
+CC Neil for info on this.
Sure, I will include be56 helper patch in the series and add Neil to CC.
Quoted 37 lines
Quoted 27 lines
(quoted diff) +2
-0+ +static int pac1711_read_raw(struct iio_dev *indio_dev, struct
iio_chan_specconst *chan,
+ int *val, int *val2, long mask)drivers/iio/adc/pac1711.c new file +8
-0+{ + struct pac1711_chip_info *info = iio_priv(indio_dev); + u64 tmp = 0; + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret = pac1711_retrieve_data(info,
PAC1711_MIN_UPDATE_WAIT_TIME_US);
+ if (ret)
+ return ret;
+
+ switch (chan->type) {
+ caseIIO_VOLTAGE:
+ *val =info->chip_reg_data.vbus; + returnIIO_VAL_INT;
+ caseIIO_CURRENT:
+ *val =info->chip_reg_data.vsense; + returnIIO_VAL_INT;
+ caseIIO_POWER:
+scoped_guard(mutex, &info->lock){ + *val = (u32)info-Quoted 1 line
chip_reg_data.vpower;
Quoted 9 lines
+ *val2 = (u32)(info-
Quoted 1 line
chip_reg_data.vpower >> 32);
+ }
+ returnIIO_VAL_INT_64;
+ caseIIO_ENERGY:
+ caseIIO_CHARGE:
+scoped_guard(mutex, &info->lock){ + *val = (u32)info-Quoted 1 line
chip_reg_data.acc_val;
Quoted 8 lines
Could use
lower_32_bits/upper_32_bits.
Maybe we should just have a helper for this given it is becoming rather more common. iio_fill_val_int_64(info-Quoted 1 line
chip_reg_data.acc_val, val, val2);
I don't mind if you want to leave that a follow up for someone else to do though.
About this, would you prefer me to add iio_fill_val_int_64() as a precursor patch in this series, with PAC1711 as its first user? Or should I use lower_32_bits() and upper_32_bits() in the driver implementation and leave the generic helper for a separate follow-up series?
Quoted 37 lines
Quoted 37 lines
(quoted diff) +2
-0+ +static int pac1711_parse_fw(struct i2c_client *client, struct
pac1711_chip_info*info)drivers/iio/adc/pac1711.c new file +6
-0+{ + struct device *dev = &client->dev; + const char *acc_mode; + int ret; + + ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
&info->shunt);
+ if (ret)
+ returndev_err_probe(dev, ret, "Shunt resistor property error\n");
+
+ if (!info->shunt)
+ returndev_err_probe(dev, -EINVAL, "Invalid value for shunt resistor\n");drivers/iio/adc/pac1711.c new file +10
-0+ + ret = pac1711_init_vbus_vsense_ranges(info, true); + if (ret) + return ret; + + ret = pac1711_init_vbus_vsense_ranges(info, false); + if (ret) + return ret; + + if (device_property_present(dev, "microchip,accumulation-source")) {
+ ret =device_property_read_string(dev, "microchip,accumulation-source", &acc_mode);
+ if (ret)
+ returndev_err_probe(dev, ret, "Cannot read accumulation-source\n");
+
+ if (!strcmp(acc_mode,PAC1711_ACC_ENERGY_STR))
Quoted 10 lines
Quoted 5 lines
+
info->accumulation_mode=PAC1711_ACCMODE_VPOWER;
+ else if (!strcmp(acc_mode,PAC1711_ACC_CHARGE_STR)) +info->accumulation_mode=PAC1711_ACCMODE_VSENSE;Can we use
fwnode_property_match_property_string()for this? Could add a device variant for that to make it simpler still.
Regarding this comment, I was wondering if device_property_match_property_string() is the function you want me to use in the driver implementation instead of strcmp():
https://elixir.bootlin.com/linux/v7.3-rc7/C/ident/device_property_match_property_string
Best regards,
Ariana
Jonathan Cameron
maintainerRe: [PATCH v4 3/3] iio: adc: add support for PAC1711
Quoted 13 lines
Quoted 11 lines
Quoted 2 lines
+ *val = (u32)info-
Quoted 1 line
chip_reg_data.acc_val;
Could use
lower_32_bits/upper_32_bits.
Maybe we should just have a helper for this given it is becoming rather more common. iio_fill_val_int_64(info-Quoted 1 line
chip_reg_data.acc_val, val, val2);
I don't mind if you want to leave that a follow up for someone else to do though.
Quoted 5 lines
About this, would you prefer me to add
iio_fill_val_int_64()as a precursor patch in this series, with PAC1711 as its first user? Or should I uselower_32_bits()andupper_32_bits()in the driver implementation and leave the generic helper for a separate follow-up series?
Prefer the helper - but not a strong preference.
Quoted 49 lines
Quoted 38 lines
Quoted 38 lines
(quoted diff) +2
-0+ +static int pac1711_parse_fw(struct i2c_client *client, struct
pac1711_chip_info*info)drivers/iio/adc/pac1711.c new file +6
-0+{ + struct device *dev = &client->dev; + const char *acc_mode; + int ret; + + ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
&info->shunt);
+ if (ret)
+ returndev_err_probe(dev, ret, "Shunt resistor property error\n");
+
+ if (!info->shunt)
+ returndev_err_probe(dev, -EINVAL, "Invalid value for shunt resistor\n");drivers/iio/adc/pac1711.c new file +10
-0+ + ret = pac1711_init_vbus_vsense_ranges(info, true); + if (ret) + return ret; + + ret = pac1711_init_vbus_vsense_ranges(info, false); + if (ret) + return ret; + + if (device_property_present(dev, "microchip,accumulation-source")) {
+ ret =device_property_read_string(dev, "microchip,accumulation-source", &acc_mode);
+ if (ret)
+ returndev_err_probe(dev, ret, "Cannot read accumulation-source\n");
+
+ if (!strcmp(acc_mode,PAC1711_ACC_ENERGY_STR))Quoted 10 lines
Quoted 5 lines
+
info->accumulation_mode=PAC1711_ACCMODE_VPOWER;
+ else if (!strcmp(acc_mode,PAC1711_ACC_CHARGE_STR)) +info->accumulation_mode=PAC1711_ACCMODE_VSENSE;Can we use
fwnode_property_match_property_string()for this? Could add a device variant for that to make it simpler still.
Quoted 5 lines
Regarding this comment, I was wondering if
device_property_match_property_string()is the function you want me to use in the driver implementation instead ofstrcmp():https://elixir.bootlin.com/linux/v7.3-rc7/C/ident/device_property_match_property_string
Huh. I wonder why my search didn't find that. Indeed!
Jonathan
Quoted 7 lines
Best regards,
Ariana
Jonathan Cameron
maintainerQuoted 9 lines · On Wed, 7 Oct 2026 17:41:52 +0300 Ariana Lazar <[email protected]>…
The PAC1711, PAC1721, PAC1811 and PAC1821 products are single-channel power monitors with accumulator. The PAC1711 and PAC1721 devices use 12-bit resolution for voltage and current measurements and 24 bits for power calculations, while PAC1811 and PAC1821 have 16-bit resolution and use 32 bits for power calculations. The 56-bit accumulator register accumulates power (energy) or current (Coulomb counter).
PAC1711 and PAC1811 measure up to 42V Full-Scale Range, respectively 9V for PAC1721 and PAC1821.
I think the sashiko comments for this one are a mixed back but the one about intermediate unsigned state in particular looks like a good spot. I didn't look closely at all of them.
https://sashiko.dev/#/patchset/20261007-pac1711-v4-0-6ef34d7a7947%40microchip.com
Whilst I do plan to enable sashiko emails for IIO fairly soon (subject perhaps to more discussions) in the meantime, if you want to speed up review of your patches, you can reply to the thread addressing what sashiko has found. That way you save reviewers from trying to figure out what is true and what is a false positive.
Jonathan