Skip to contentSkip to conversation

adding support for Microchip PAC193X Power Monitor

PATCH In review [email protected] 6 participants 10 messages Feb 22, 2024 – Apr 5, 2024

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v5 cover adding support for Microchip PAC193X Power Monitor

From: Marius Cristea <[email protected]>

Adding support for Microchip PAC193X series of Power Monitor with Accumulator chip family. This driver covers the following part numbers:
- PAC1931, PAC1932, PAC1933 and PAC1934

This device is at the boundary between IIO and HWMON (if you are
looking just at the "shunt resistors, vsense, power, energy"). The device also has ADC internally that can measure voltages (up to 4 channels) and also currents (up to 4 channels). The current is measured as voltage across the shunt_resistor.

I have started with a simple driver (this one that is more appropriate to be
a HWMON) and willing to add more functionality later (like data buffering that is quite important for example if someone wants to profile power consumption of the processor itself, or a peripheral device, or a battery, this kind of functionality was requested by our customers).

The above statement it's a left over comment / attempt to summarize the
discussion of whether IIO or HWMON was a better home for a driver for this device. Based on current feature set that's not an obvious decision, but there are other planned features that fit better in IIO.

Differences related to previous patch:
v5:
- fix review comments:
- remove | from device tree binding (not needed because there is no formatting to preserve).
- update in_shunt_resistor_X attribute to in_shunt_resistorX
- use channel enable to reset the energy counter for each acctive channels
- update values of IIO elements to be in naturally aligned power of 2
- use address in the attribute (IIO_DEVICE_ATTR) to extract address field
- update the code to use mod_delayed_work
- change from kfree to ACPI_FREE
- fix "0-day" issue related to double counter increment
- check functions return in case of errors (like devm_kzalloc that could fail)
- fix coding style issues

v4:
- remove the "reset_accumulators" proprietary attribute
- add enable/disable for energy channels
- remove "reset_accumulators" attribute
- remove unused/redundant defines
- rename variable to be more relevant into a certain context
- make "storagebits" naturally aligned power of 2
- fix coding style issues
- use to_iio_dev_attr to access address field in the IIO_DEVICE_ATTR()
- remove unnecesarry "break" from switch case
- remove double increment and initialization of a variable
- use address as index in IIO_DEVICE_ATTR
- properly handle memory allocation failure

v3:
- this version was sent also to HWMON list
- fix review comments:
- drop redundant description from device tree bindings
- reorder "patternProperties:" to follow "properties:" in device tree bindings
- update comments to proper describe code
- use numbers instead of defines for clarity in some part of the code
- use the new "guard(mutex)"
- use "clamp()" instead of duplicating code
- remove extra layer of checking in some switch cases
- use "i2c_get_match_data()"
- replace while with for loops for the code to look cleaner
- reverse the logic to reduce indent.
- add comment related to channels numbering
- remove memory duplicate when creating dynamic channels
- add "devm_add_action_or_reset" to handle the "cancel_delayed_work_sync"
- remove "pac1934_remove()" function

v2:
- fix review comments:
- change the device tree bindings
- use label property
- fix coding style issues
- remove unused headers
- use get_unaligned_bexx instead of own functions
- change to use a system work queue
- use probe_new instead of old probe

v1:
- first version committed to review

Marius Cristea (2):
dt-bindings: iio: adc: adding support for PAC193X
iio: adc: adding support for PAC193x

 .../ABI/testing/sysfs-bus-iio-adc-pac1934     |    9 +
 .../bindings/iio/adc/microchip,pac1934.yaml   |  120 ++
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   11 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/pac1934.c                     | 1637 +++++++++++++++++
 6 files changed, 1785 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-pac1934
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml
 create mode 100644 drivers/iio/adc/pac1934.c

base-commit: b1a1eaf6183697b77f7243780a25f35c7c0c8bdf

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v5 1/2 dt-bindings: iio: adc: adding support for PAC193X

From: Marius Cristea <[email protected]>

This is the device tree schema for iio driver for Microchip PAC193X series of Power Monitors with Accumulator.

---
 .../bindings/iio/adc/microchip,pac1934.yaml   | 120 ++++++++++++++++++
 1 file changed, 120 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml
Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml new file +120 -0
new file mode 100644
index 000000000000..52be5ada1eb6
@@ -0,0 +1,120 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/microchip,pac1934.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip PAC1934 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 PAC1931, PAC1932, PAC1933 and PAC1934 can be found here:
+    https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/PAC1931-Family-Data-Sheet-DS20005850E.pdf
+
+properties:
+  compatible:
+    enum:
+      - microchip,pac1931
+      - microchip,pac1932
+      - microchip,pac1933
+      - microchip,pac1934
+
+  reg:
+    maxItems: 1
+
+  "#address-cells":
+    const: 1
+
+  "#size-cells":
+    const: 0
+
+  interrupts:
+    maxItems: 1
+
+  slow-io-gpios:
+    description:
+      A GPIO used to trigger a change is sampling rate (lowering the chip power
+      consumption). If configured in SLOW mode, if this pin is forced high,
+      sampling rate is forced to eight samples/second. When it is forced low,
+      the sampling rate is 1024 samples/second unless a different sample rate
+      has been programmed.
+
+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.
+
+    required:
+      - reg
+      - shunt-resistor-micro-ohms
+
+    unevaluatedProperties: false
+
+required:
+  - compatible
+  - reg
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        power-monitor@10 {
+            compatible = "microchip,pac1934";
+            reg = <0x10>;
+
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            channel@1 {
+                reg = <0x1>;
+                shunt-resistor-micro-ohms = <24900000>;
+                label = "CPU";
+            };
+
+            channel@2 {
+                reg = <0x2>;
+                shunt-resistor-micro-ohms = <49900000>;
+                label = "GPU";
+            };
+
+            channel@3 {
+                reg = <0x3>;
+                shunt-resistor-micro-ohms = <75000000>;
+                label = "MEM";
+                bipolar;
+            };
+
+            channel@4 {
+                reg = <0x4>;
+                shunt-resistor-micro-ohms = <100000000>;
+                label = "NET";
+                bipolar;
+            };
+        };
+    };
+
+...
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v5 1/2] dt-bindings: iio: adc: adding support for PAC193X

Quoted 5 lines · On Thu, 22 Feb 2024 18:42:05 +0200, [email protected] wrote:

From: Marius Cristea <[email protected]>

This is the device tree schema for iio driver for Microchip PAC193X series of Power Monitors with Accumulator.

Quoted 7 lines
---
 .../bindings/iio/adc/microchip,pac1934.yaml   | 120 ++++++++++++++++++
 1 file changed, 120 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml:51:9: [warning] wrong indentation: expected 6 but found 8 (indentation)

dtschema/dtc warnings/errors:

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#3 lore raw

Re: [PATCH v5 1/2] dt-bindings: iio: adc: adding support for PAC193X

In reply to [email protected]
kernel test robot noticed the following build warnings:

Hi,

kernel test robot noticed the following build warnings:

[auto build test WARNING on b1a1eaf6183697b77f7243780a25f35c7c0c8bdf]

url:    https://github.com/intel-lab-lkp/linux/commits/marius-cristea-microchip-com/dt-bindings-iio-adc-adding-support-for-PAC193X/20240223-004332
base:   b1a1eaf6183697b77f7243780a25f35c7c0c8bdf
patch link:    https://lore.kernel.org/r/20240222164206.65700-2-marius.cristea%40microchip.com
patch subject: [PATCH v5 1/2] dt-bindings: iio: adc: adding support for PAC193X
compiler: loongarch64-linux-gcc (GCC) 13.2.0
reproduce: (https://download.01.org/0day-ci/archive/20240224/[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]/

dtcheck warnings: (new ones prefixed by >>)

>> Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml:51:9: [warning] wrong indentation: expected 6 but found 8 (indentation)

vim +51 Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml

     8	
     9	maintainers:
    10	  - Marius Cristea <[email protected]>
    11	
    12	description: |
    13	  This device is part of the Microchip family of Power Monitors with
    14	  Accumulator.
    15	  The datasheet for PAC1931, PAC1932, PAC1933 and PAC1934 can be found here:
    16	    https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/PAC1931-Family-Data-Sheet-DS20005850E.pdf
    17	
    18	properties:
    19	  compatible:
    20	    enum:
    21	      - microchip,pac1931
    22	      - microchip,pac1932
    23	      - microchip,pac1933
    24	      - microchip,pac1934
    25	
    26	  reg:
    27	    maxItems: 1
    28	
    29	  "#address-cells":
    30	    const: 1
    31	
    32	  "#size-cells":
    33	    const: 0
    34	
    35	  interrupts:
    36	    maxItems: 1
    37	
    38	  slow-io-gpios:
    39	    description:
    40	      A GPIO used to trigger a change is sampling rate (lowering the chip power
    41	      consumption). If configured in SLOW mode, if this pin is forced high,
    42	      sampling rate is forced to eight samples/second. When it is forced low,
    43	      the sampling rate is 1024 samples/second unless a different sample rate
    44	      has been programmed.
    45	
    46	patternProperties:
    47	  "^channel@[1-4]+$":
    48	    type: object
    49	    $ref: adc.yaml
    50	    description:
  > 51	        Represents the external channels which are connected to the ADC.
    52	
    53	    properties:
    54	      reg:
    55	        items:
    56	          minimum: 1
    57	          maximum: 4
    58	
    59	      shunt-resistor-micro-ohms:
    60	        description:
    61	          Value in micro Ohms of the shunt resistor connected between
    62	          the SENSE+ and SENSE- inputs, across which the current is measured.
    63	          Value is needed to compute the scaling of the measured current.
    64	
    65	    required:
    66	      - reg
    67	      - shunt-resistor-micro-ohms
    68	
    69	    unevaluatedProperties: false
    70	
    71	required:
    72	  - compatible
    73	  - reg
    74	  - "#address-cells"
    75	  - "#size-cells"
    76	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#4 lore raw

Re: [PATCH v5 1/2] dt-bindings: iio: adc: adding support for PAC193X

Quoted 22 lines · On Sat, 24 Feb 2024 15:34:51 +0800 kernel test robot <[email protected]> wrote:

Hi,

kernel test robot noticed the following build warnings:

[auto build test WARNING on b1a1eaf6183697b77f7243780a25f35c7c0c8bdf]

url:    https://github.com/intel-lab-lkp/linux/commits/marius-cristea-microchip-com/dt-bindings-iio-adc-adding-support-for-PAC193X/20240223-004332
base:   b1a1eaf6183697b77f7243780a25f35c7c0c8bdf
patch link:    https://lore.kernel.org/r/20240222164206.65700-2-marius.cristea%40microchip.com
patch subject: [PATCH v5 1/2] dt-bindings: iio: adc: adding support for PAC193X
compiler: loongarch64-linux-gcc (GCC) 13.2.0
reproduce: (https://download.01.org/0day-ci/archive/20240224/[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]/

dtcheck warnings: (new ones prefixed by >>)

Quoted 1 line
Quoted 1 line

Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml:51:9: [warning] wrong indentation: expected 6 but found 8 (indentation)

Quoted 7 lines
    45	
    46	patternProperties:
    47	  "^channel@[1-4]+$":
    48	    type: object
    49	    $ref: adc.yaml
    50	    description:
Quoted 1 line

51 Represents the external channels which are connected to the ADC.

I've fixed this up whilst applying.

Quoted 26 lines
    52	
    53	    properties:
    54	      reg:
    55	        items:
    56	          minimum: 1
    57	          maximum: 4
    58	
    59	      shunt-resistor-micro-ohms:
    60	        description:
    61	          Value in micro Ohms of the shunt resistor connected between
    62	          the SENSE+ and SENSE- inputs, across which the current is measured.
    63	          Value is needed to compute the scaling of the measured current.
    64	
    65	    required:
    66	      - reg
    67	      - shunt-resistor-micro-ohms
    68	
    69	    unevaluatedProperties: false
    70	
    71	required:
    72	  - compatible
    73	  - reg
    74	  - "#address-cells"
    75	  - "#size-cells"
    76	
author maintainer
#5 lore raw

PATCH v5 2/2 iio: adc: adding support for PAC193x

In reply to [email protected]

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip
PAC193X series of Power Monitor with Accumulator chip family.

---
 .../ABI/testing/sysfs-bus-iio-adc-pac1934     |    9 +
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   11 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/pac1934.c                     | 1637 +++++++++++++++++
 5 files changed, 1665 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-pac1934
 create mode 100644 drivers/iio/adc/pac1934.c
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1934 new file +9 -0
new file mode 100644
index 000000000000..625b7f867847
@@ -0,0 +1,9 @@
+What:		/sys/bus/iio/devices/iio:deviceX/in_shunt_resistorY
+KernelVersion:	6.7
+Contact:	[email protected]
+Description:
+		The value of the shunt resistor may be known only at runtime
+		and set by a client application. This attribute allows to
+		set its value in micro-ohms. X is the IIO index of the device.
+		Y is the channel number. The value is used to calculate
+		current, power and accumulated energy.
MAINTAINERS +7 -0
index 35147477e8e4..5eea1e98e0f2 100644
@@ -14231,6 +14231,13 @@ 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 PAC1934 POWER/ENERGY MONITOR DRIVER
+M:	Marius Cristea <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/iio/adc/microchip,pac1934.yaml
+F:	drivers/iio/adc/pac1934.c
+
 MICROCHIP PCI1XXXX GP DRIVER
 M:	Vaibhaav Ram T.L <[email protected]>
 M:	Kumaravel Thiagarajan <[email protected]>
drivers/iio/adc/Kconfig +11 -0
index 3b73c509bd68..cec7d2299671 100644
@@ -930,6 +930,17 @@ config NPCM_ADC
 	  This driver can also be built as a module. If so, the module
 	  will be called npcm_adc.
 
+config PAC1934
+	tristate "Microchip Technology PAC1934 driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Microchip Technology's PAC1931,
+	  PAC1932, PAC1933, PAC1934 Single/Multi-Channel Power Monitor with
+	  Accumulator.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called pac1934.
+
 config PALMAS_GPADC
 	tristate "TI Palmas General Purpose ADC"
 	depends on MFD_PALMAS
drivers/iio/adc/Makefile +1 -0
index d2fda54a3259..bbf9613fc339 100644
@@ -86,6 +86,7 @@ obj-$(CONFIG_MP2629_ADC) += mp2629_adc.o
 obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_NPCM_ADC) += npcm_adc.o
+obj-$(CONFIG_PAC1934) += pac1934.o
 obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
 obj-$(CONFIG_QCOM_SPMI_ADC5) += qcom-spmi-adc5.o
 obj-$(CONFIG_QCOM_SPMI_IADC) += qcom-spmi-iadc.o
drivers/iio/adc/pac1934.c new file +1637 -0

1,637 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#6 lore raw

Re: [PATCH v5 2/2] iio: adc: adding support for PAC193x

Quoted 6 lines · On Thu, 22 Feb 2024 18:42:06 +0200 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip
PAC193X series of Power Monitor with Accumulator chip family.

So I had a few comments on this, but nothing that can't be cleaned up later.
+ I'll fix the thing the bots didn't like on the bindings.

Series applied to the togreg branch of iio.git and pushed out as testing for 0-day to take a look at it.

Thanks,

Jonathan

Quoted 5 lines
drivers/iio/adc/pac1934.c new file +0 -0
new file mode 100644
index 000000000000..a67fdaba940d

drivers/iio/adc/pac1934.c line 1084

Quoted 10 lines
drivers/iio/adc/pac1934.c new file +8 -0
 
 
+
+/*
+ * documentation related to the ACPI device definition
+ * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
+ */
+static bool pac1934_acpi_parse_channel_config(struct i2c_client *client,
+					      struct pac1934_chip_info *info)
+{

This should probably also return an int with 0 good and anything else having a more meaningful error value if we can come up with one.

drivers/iio/adc/pac1934.c line 1156

Quoted 2 lines
drivers/iio/adc/pac1934.c new file +2 -0
+static bool pac1934_of_parse_channel_config(struct i2c_client *client,
+					    struct pac1934_chip_info *info)

pac1934_fw_parse_channel_config seeing as code isn't of specific now.

Also return negative error or zero so that you can provide a more meaningful return value in probe + it will be handy when we make use of the device_for_each_child_node_scoped() in here (that's so new I'm not asking people to use it this cycle).

Can make these changes later as part of the changes to use new infrastructure I mention below.

drivers/iio/adc/pac1934.c line 1167

Quoted 11 lines
drivers/iio/adc/pac1934.c new file +11 -0
+{
+	struct fwnode_handle *node, *fwnode;
+	struct device *dev = &client->dev;
+	unsigned int current_channel;
+	int idx, ret;
+
+	info->sample_rate_value = 1024;
+	current_channel = 1;
+
+	fwnode = dev_fwnode(dev);
+	fwnode_for_each_available_child_node(fwnode, node) {

Ah this. Did we discuss this in earlier versions?
Been a while so I've forgotten.

device_for_each_child_node() is actually implemented to only
allow for available nodes. So you can use that directly here. It's weird, and we've raised it several times with the fwnode folk.

Seeing as we'll probably convert this shortly to device_for_each_child_node_scoped() we can deal with that detail as part of that patch.

Quoted 62 lines
drivers/iio/adc/pac1934.c new file +61 -0
+		ret = fwnode_property_read_u32(node, "reg", &idx);
+		if (ret) {
+			dev_err_probe(dev, ret,
+				      "reading invalid channel index\n");
+			goto err_fwnode;
+		}
+		/* 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) {
+			dev_err_probe(dev, -EINVAL,
+				      "%s: invalid channel_index %d value\n",
+				      fwnode_get_name(node), idx);
+			goto err_fwnode;
+		}
+
+		/* enable channel */
+		info->active_channels[idx] = true;
+
+		ret = fwnode_property_read_u32(node, "shunt-resistor-micro-ohms",
+					       &info->shunts[idx]);
+		if (ret) {
+			dev_err_probe(dev, ret,
+				      "%s: invalid shunt-resistor value: %d\n",
+				      fwnode_get_name(node), info->shunts[idx]);
+			goto err_fwnode;
+		}
+
+		if (fwnode_property_present(node, "label")) {
+			ret = fwnode_property_read_string(node, "label",
+							  (const char **)&info->labels[idx]);
+			if (ret) {
+				dev_err_probe(dev, ret,
+					      "%s: invalid rail-name value\n",
+					      fwnode_get_name(node));
+				goto err_fwnode;
+			}
+		}
+
+		info->bi_dir[idx] = fwnode_property_read_bool(node, "bipolar");
+
+		current_channel++;
+	}
+
+	return true;
+
+err_fwnode:
+	fwnode_handle_put(node);
+
+	return false;
+}
 
+static int pac1934_prep_iio_channels(struct pac1934_chip_info *info, struct iio_dev *indio_dev)
+{
+	struct iio_chan_spec *ch_sp;
+	int channel_size, attribute_count, cnt;
+	void *dyn_ch_struct, *tmp_data;
+	struct device *dev = &info->client->dev;
+
+	/* find out dynamically how many IIO channels we need */
+	attribute_count = 0;

drivers/iio/adc/pac1934.c

Quoted 10 lines
drivers/iio/adc/pac1934.c new file +10 -0
+	channel_size = 0;
+	for (cnt = 0; cnt < info->phys_channels; cnt++) {
+		if (!info->active_channels[cnt])
+			continue;
+
+		/* add the size of the properties of one chip physical channel */
+		channel_size += sizeof(pac1934_single_channel);
+		/* count how many enabled channels we have */
+		attribute_count += ARRAY_SIZE(pac1934_single_channel);
+		dev_info(dev, ":%s: Channel %d active\n",

dev_dbg()
This is too noisy really given I'd assume that's easy to establish after the driver is loaded. If nothing else comes up I'll make this dev_dbg whilst applying.

Quoted 2 lines
drivers/iio/adc/pac1934.c new file +2 -0
+			 __func__, cnt + 1);
+	}
#7 lore raw

Re: [PATCH v5 2/2] iio: adc: adding support for PAC193x

Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Thu, 22 Feb 2024 18:42:06 +0200 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip
PAC193X series of Power Monitor with Accumulator chip family.

Quoted 6 lines
Quoted 1 line

So I had a few comments on this, but nothing that can't be cleaned up later.
+ I'll fix the thing the bots didn't like on the bindings.

Series applied to the togreg branch of iio.git and pushed out as testing for 0-day to take a look at it.

I tested this out on v6.9-rc2 and prompted a backtrace when collectd started running:

	------------[ cut here ]------------
	UBSAN: array-index-out-of-bounds in /home/conor/stuff/linux/drivers/iio/adc/pac1934.c:857:25
	index 7 is out of range for type 'u32 [4]'
	CPU: 1 PID: 179 Comm: iiod Not tainted 6.9.0-rc2-dirty #1
	Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
	Call Trace:
	[<ffffffff80006bba>] dump_backtrace+0x28/0x30
	[<ffffffff80bd67d8>] show_stack+0x38/0x44
	[<ffffffff80be7820>] dump_stack_lvl+0x6e/0x9a
	[<ffffffff80be7864>] dump_stack+0x18/0x20
	[<ffffffff80be1452>] ubsan_epilogue+0x10/0x46
	[<ffffffff80615358>] __ubsan_handle_out_of_bounds+0x6a/0x78
	[<ffffffff80981f3a>] pac1934_read_raw+0x20c/0x34c
	[<ffffffff80977c4c>] iio_read_channel_info+0x5c/0xbe
	[<ffffffff8073516e>] dev_attr_show+0x1c/0x4a
	[<ffffffff80387292>] sysfs_kf_seq_show+0x80/0xcc
	[<ffffffff80385b12>] kernfs_seq_show+0x3c/0x4a
	[<ffffffff8031e3d8>] seq_read_iter+0x136/0x2e4
	[<ffffffff80385cde>] kernfs_fop_read_iter+0x38/0x16a
	[<ffffffff802e904a>] vfs_read+0x1be/0x2ba
	[<ffffffff802e9c48>] ksys_read+0x64/0xd2
	[<ffffffff802e9cd6>] __riscv_sys_read+0x20/0x28
	[<ffffffff80be838a>] do_trap_ecall_u+0xee/0x204
	[<ffffffff80bf57d0>] ret_from_exception+0x0/0x64
	---[ end trace ]---

The device itself only has 4 channels, but in sysfs there are "fake" channels for the average voltages and currents too. UBSAN points at:

	case PAC1934_VSENSE_AVG_4_ADDR:
		*val = PAC1934_MAX_VSENSE_RSHIFTED_BY_16B;
		if (chan->scan_type.sign == 'u')
			*val2 = info->shunts[channel];
		else
			*val2 = info->shunts[channel] >> 1;
		return IIO_VAL_FRACTIONAL;

And info->shunts is only valid for the 4 real channels, so I guess the averaged channels probably need to do a [channel - 4] or similar. I dunno if that relation between averaged channels and their "real" counterparts is fixed or if different pac devices need different values, but on my system here that would work.

I do quite like that UBSAN points out the line in question :)

Cheers,
Conor.

author maintainer
#8 lore raw

Re: [PATCH v5 2/2] iio: adc: adding support for PAC193x

Hi Conor,

Thanks for reporting the bug. I have detect it and I'm already
working on a patch for it.

Thanks,
Marius

Quoted 68 lines · Conor Dooley
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Thu, 22 Feb 2024 18:42:06 +0200 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip
PAC193X series of Power Monitor with Accumulator chip family.

Quoted 7 lines
Quoted 1 line

So I had a few comments on this, but nothing that can't be cleaned up later.
+ I'll fix the thing the bots didn't like on the bindings.

Series applied to the togreg branch of iio.git and pushed out as testing for 0-day to take a look at it.

I tested this out on v6.9-rc2 and prompted a backtrace when collectd started running:

        ------------[ cut here ]------------
        UBSAN: array-index-out-of-bounds in
/home/conor/stuff/linux/drivers/iio/adc/pac1934.c:857:25
        index 7 is out of range for type 'u32 [4]'
        CPU: 1 PID: 179 Comm: iiod Not tainted 6.9.0-rc2-dirty #1
        Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
        Call Trace:
        [<ffffffff80006bba>] dump_backtrace+0x28/0x30
        [<ffffffff80bd67d8>] show_stack+0x38/0x44
        [<ffffffff80be7820>] dump_stack_lvl+0x6e/0x9a
        [<ffffffff80be7864>] dump_stack+0x18/0x20
        [<ffffffff80be1452>] ubsan_epilogue+0x10/0x46
        [<ffffffff80615358>] __ubsan_handle_out_of_bounds+0x6a/0x78
        [<ffffffff80981f3a>] pac1934_read_raw+0x20c/0x34c
        [<ffffffff80977c4c>] iio_read_channel_info+0x5c/0xbe
        [<ffffffff8073516e>] dev_attr_show+0x1c/0x4a
        [<ffffffff80387292>] sysfs_kf_seq_show+0x80/0xcc
        [<ffffffff80385b12>] kernfs_seq_show+0x3c/0x4a
        [<ffffffff8031e3d8>] seq_read_iter+0x136/0x2e4
        [<ffffffff80385cde>] kernfs_fop_read_iter+0x38/0x16a
        [<ffffffff802e904a>] vfs_read+0x1be/0x2ba
        [<ffffffff802e9c48>] ksys_read+0x64/0xd2
        [<ffffffff802e9cd6>] __riscv_sys_read+0x20/0x28
        [<ffffffff80be838a>] do_trap_ecall_u+0xee/0x204
        [<ffffffff80bf57d0>] ret_from_exception+0x0/0x64
        ---[ end trace ]---

The device itself only has 4 channels, but in sysfs there are "fake" channels for the average voltages and currents too. UBSAN points at:
        case PAC1934_VSENSE_AVG_4_ADDR:
                *val = PAC1934_MAX_VSENSE_RSHIFTED_BY_16B; if (chan->scan_type.sign == 'u')
                        *val2 = info->shunts[channel]; else
                        *val2 = info->shunts[channel] >> 1; return IIO_VAL_FRACTIONAL;

And info->shunts is only valid for the 4 real channels, so I guess the
averaged channels probably need to do a [channel - 4] or similar. I dunno if that relation between averaged channels and their "real" counterparts is fixed or if different pac devices need different values,
but on my system here that would work.

I do quite like that UBSAN points out the line in question :)

Cheers,
Conor.

#9 lore raw

Re: [PATCH v5 2/2] iio: adc: adding support for PAC193x

Quoted 4 lines · On Fri, Apr 05, 2024 at 12:53:38PM +0000, [email protected] wrote:

Hi Conor,

Thanks for reporting the bug. I have detect it and I'm already
working on a patch for it.

Oh cool, keep me posted :)