adding support for Microchip PAC194X Power Monitor
Subsystems (2)
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
Differences related to previous patch:
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 | 9 + .../bindings/iio/adc/microchip,pac1944.yaml | 168 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 3528 +++++++++++++++++ 6 files changed, 3726 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
Signature · 1 line
2.43.0
PATCH 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.
--- .../bindings/iio/adc/microchip,pac1944.yaml | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +168 -0
@@ -0,0 +1,168 @@ +# 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_1 + - microchip,pac1941_2 + - microchip,pac1942_1 + - microchip,pac1942_2 + - microchip,pac1943_1 + - microchip,pac1944_1 + - microchip,pac1951_1 + - microchip,pac1951_2 + - microchip,pac1952_1 + - microchip,pac1952_2 + - microchip,pac1953_1 + - microchip,pac1954_1 + + reg: + maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + maxItems: 2 + + 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. + + microchip,gpio: + type: boolean + description: + In default mode, this pin is a GPIO input pin. It can be + configured to be an output pin, as well as the ALERT2 + function. This pin is an open-drain configuration and + needs a pull-up resistor to VDD. + +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-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VBUS could be configured into the following full scale range (FSR) + <0> - VBUS has unipolar +32V to 0V FSR (default) + <1> - VBUS has bipolar +32V to -32V FSR + <2> - VBUS has bipolar +16V to -16V FSR + maximum: 2 + + microchip,vsense-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VSENSE could be configured into the following full scale range (FSR) + <0> - VSENSE has unipolar +100 mV to 0V FSR (default) + <1> - VSENSE has bipolar +100 mV to -100 mV FSR + <2> - VSENSE has bipolar +50 mV to -50 mV FSR + maximum: 2 + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + 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 + + 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,pac1954_1"; + reg = <0x10>; + + #address-cells = <1>; + #size-cells = <0>; + + status = "okay"; + + channel@1 { + reg = <0x1>; + shunt-resistor-micro-ohms = <24900>; + label = "CPU"; + microchip,vbus-mode = <0>; + microchip,vsense-mode = <0>; + microchip,accumulation-mode = <0>; + }; + + channel@3 { + reg = <0x3>; + shunt-resistor-micro-ohms = <75000>; + label = "MEM"; + microchip,vbus-mode = <0>; + microchip,vsense-mode = <0>; + microchip,accumulation-mode = <0>; + }; + + channel@4 { + reg = <0x4>; + shunt-resistor-micro-ohms = <100000>; + label = "NET"; + microchip,vbus-mode = <0>; + microchip,vsense-mode = <0>; + microchip,accumulation-mode = <0>; + }; + }; + }; + +...
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH v1 1/2] dt-bindings: iio: adc: adding support for PAC194X
Quoted 4 lines · On Fri, 19 Jul 2024 20:38:54 +0300 <[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.
Hi Marius
Good to add a tiny bit either here or in below description for why there are so many part numbers.
Looks like mixture of number of channels and high vs low side monitors. And the big number is about voltage range?
Other comments inline.
Thanks,
Jonathan
Quoted 48 lines
Signed-off-by Marius Cristea <[email protected]>--- .../bindings/iio/adc/microchip,pac1944.yaml | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +35
-0@@ -0,0 +1,168 @@ +# 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_1 + - microchip,pac1941_2 + - microchip,pac1942_1 + - microchip,pac1942_2 + - microchip,pac1943_1 + - microchip,pac1944_1 + - microchip,pac1951_1 + - microchip,pac1951_2 + - microchip,pac1952_1 + - microchip,pac1952_2 + - microchip,pac1953_1 + - microchip,pac1954_1 + + reg:
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 45
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + maxItems: 2
Need names as annoyingly people often wire just the second one (I've never understood why!)
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +6
-0+ + 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,
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 61
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ the sampling rate is 1024 samples/second unless a different sample rate + has been programmed. + + microchip,gpio: + type: boolean + description: + In default mode, this pin is a GPIO input pin. It can be + configured to be an output pin, as well as the ALERT2 + function. This pin is an open-drain configuration and + needs a pull-up resistor to VDD.
This one is a bit odd.
Can we do that configuration based on whether anyone requests it or if it is wired as alert2?
So I don't think we should need this binding, but we may need the stuff to provide a gpio.
Also, oddly short wrap.
Quoted 14 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +14
-0+ +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 +
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 85
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ 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-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VBUS could be configured into the following full scale range (FSR)
Key with these range things is to make it clear why they are a wiring thing rather than something userspace should control. Perhaps you can add some detail on that here?
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +6
-0+ <0> - VBUS has unipolar +32V to 0V FSR (default) + <1> - VBUS has bipolar +32V to -32V FSR + <2> - VBUS has bipolar +16V to -16V FSR + maximum: 2 + + microchip,vsense-mode:
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 101
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VSENSE could be configured into the following full scale range (FSR) + <0> - VSENSE has unipolar +100 mV to 0V FSR (default) + <1> - VSENSE has bipolar +100 mV to -100 mV FSR + <2> - VSENSE has bipolar +50 mV to -50 mV FSR + maximum: 2 + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32
Why is this a hardware thing? Is it specific to particular wiring? Not sure this one belong in DT.
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 106
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +5
-0+ description: + The Hardware Accumulator could be configured to accumulate + VPOWER, VSENSE or VBUS + <0> - Accumulator accumulates VPOWER (default) + <1> - Accumulator accumulates VSENSE
Add some info on what this is for (charge measurement)
Thankfully there is a nice section on why you'd want to do this in the datasheet as only the power option made sense to me.
There is a nice statement that you might do this as an extreme form of oversampling as well.
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +4
-0+ <2> - Accumulator accumulates VBUS + maximum: 2 + + required:
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 120
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - "#address-cells" + - "#size-cells"
Basic power supplies should always be here as chips tend not to run without power. Note that doesn't mean a dts needs to actually list them if they are always on as the regulator framework will provide stubs etc.
Jonathan
Re: [PATCH v1 1/2] dt-bindings: iio: adc: adding support for PAC194X
In reply to [email protected]Quoted 21 lines · On 19/07/2024 19:38, [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.Signed-off-by Marius Cristea <[email protected]>--- .../bindings/iio/adc/microchip,pac1944.yaml | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yamlDocumentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +4
-0@@ -0,0 +1,168 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/microchip,pac1944.yaml#
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 14
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+$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:
Wrap it according to Linux coding style, so at 80.
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 22
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +8
-0+ 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_1
Underscores are not allowed.
What does _1 and _2 mean?
Quoted 13 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +13
-0+ - microchip,pac1941_2 + - microchip,pac1942_1 + - microchip,pac1942_2 + - microchip,pac1943_1 + - microchip,pac1944_1 + - microchip,pac1951_1 + - microchip,pac1951_2 + - microchip,pac1952_1 + - microchip,pac1952_2 + - microchip,pac1953_1 + - microchip,pac1954_1 + + reg:
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 45
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + interrupts: + maxItems: 2
You need to list and describe the items.
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 47
Quoted 2 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +2
-0+ + slow-io-gpios:
Missing maxItems
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 58
Quoted 11 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +11
-0+ 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. + + microchip,gpio: + type: boolean + description: + In default mode, this pin is a GPIO input pin. It can be
Which pin?
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 59
Quoted 1 line
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +1
-0+ configured to be an output pin, as well as the ALERT2
output is also GPIO, so maybe you meant some interrupts? But then you have interrupts property for that...
Quoted 68 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +68
-0+ function. This pin is an open-drain configuration and + needs a pull-up resistor to VDD. + +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-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VBUS could be configured into the following full scale range (FSR) + <0> - VBUS has unipolar +32V to 0V FSR (default) + <1> - VBUS has bipolar +32V to -32V FSR + <2> - VBUS has bipolar +16V to -16V FSR + maximum: 2 + + microchip,vsense-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The VSENSE could be configured into the following full scale range (FSR) + <0> - VSENSE has unipolar +100 mV to 0V FSR (default) + <1> - VSENSE has bipolar +100 mV to -100 mV FSR + <2> - VSENSE has bipolar +50 mV to -50 mV FSR + maximum: 2 + + microchip,accumulation-mode: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + 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 + + required: + - reg + - shunt-resistor-micro-ohms + + unevaluatedProperties: false + +required: + - compatible + - reg + - "#address-cells" + - "#size-cells" + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>;
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml line 137
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/microchip,pac1944.yaml new file +10
-0+ #size-cells = <0>; + + power-monitor@10 { + compatible = "microchip,pac1954_1"; + reg = <0x10>; + + #address-cells = <1>; + #size-cells = <0>; + + status = "okay";
Drop
Best regards,
Krzysztof
PATCH 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.
--- .../ABI/testing/sysfs-bus-iio-adc-pac1944 | 9 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 3528 +++++++++++++++++ 5 files changed, 3558 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 +9 -0
@@ -0,0 +1,9 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_shunt_resistorY +KernelVersion: 6.10 +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
@@ -14793,6 +14793,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 +13 -0
@@ -989,6 +989,19 @@ 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 + depends on IIO + 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
@@ -90,6 +90,7 @@ 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_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 +3528 -0
2,952 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
Re: [PATCH v1 2/2] iio: adc: adding support for PAC194X
Le 19/07/2024 à 19:38, [email protected] a écrit :
Quoted 13 lines
From: Marius Cristea <[email protected]>
This is the iio driver for Microchip PAC194X and PAC195X series of Power Monitors with Accumulator.
Signed-off-by Marius Cristea <[email protected]>--- .../ABI/testing/sysfs-bus-iio-adc-pac1944 | 9 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 3528 +++++++++++++++++ 5 files changed, 3558 insertions(+)
Quoted 3 lines
create mode 100644
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944create mode 100644drivers/iio/adc/pac1944.c
Hi,
below a few comments and nitpicks.
- You could slightly improve style by removing extra spaces in
several places where you have <SPACE><SPACE>= or =<SPACE><SPACE>.
- pac1944_oc_limit_nsamples_store(),
pac1944_uc_limit_nsamples_store(),
pac1944_op_limit_nsamples_store(),
pac1944_ov_limit_nsamples_store(),
pac1944_uv_limit_nsamples_store()
look really similar and could be defined with a macro with 3 parameters to save some LoC
- same for pac1944_slow_alert_store() andpac1944_gpio_alert_store(), if it makes sense for just 2 functions
- the same could be done for some show functions
- (unlikely(check_add_overflow()): unlikely is not needed, I think
that it is already handled that way (see __must_check_overflow())
...
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+static int pac1944_send_refresh (struct pac1944_chip_info *info, +u8 refresh_cmd,u32 wait_time) +{ +struct i2c_client *client = info->client; +int ret; + +/* Writing a REFRESH or a REFRESH_V command */ + ret =i2c_smbus_write_byte (client, refresh_cmd); +if (ret) { + dev_err(&client->dev, "%s - cannot send Refresh cmd (0x%02X) to PAC1944\n",
drivers/iio/adc/pac1944.c line 1445
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ __func__, refresh_cmd); +return ret; + } + +/* Register data retrieval timestamp */ + info->chip_reg_data.jiffies_tstamp = jiffies; +/* Wait till the data is available */ +usleep_range (wait_time, wait_time +100 ); + +return ret;
Nitpick : return 0;
Quoted 1 line
+}
...
drivers/iio/adc/pac1944.c line 1686
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+static int pac1944_retrieve_data (struct pac1944_chip_info *info,u32 wait_time) +{ +int ret;
Missing newline
Quoted 24 lines
drivers/iio/adc/pac1944.c new file +24
-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 ))) { +/* +* 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; +}
...
Quoted 12 lines
drivers/iio/adc/pac1944.c new file +12
-0+static ssize_t pac1944_in_voltage_acc_raw_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +int ret; +s64 acc_voltage; +u32 samples_count; +u64 tmp_u64; +
drivers/iio/adc/pac1944.c line 1882
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return 0 ; + + acc_voltage = info->chip_reg_data.acc_val[this_attr->address]; + samples_count = info->chip_reg_data.total_samples_nr[this_attr->address]; + + tmp_u64 =div_u64 (abs (acc_voltage), samples_count); + +if (unlikely (is_negative (acc_voltage)))
unlikely() does not seem really needed here. It does not look like a hot path.
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+return sysfs_emit (buf,"-%lld\n" , tmp_u64); + else + return sysfs_emit(buf, "%lld\n", tmp_u64); +}
...
drivers/iio/adc/pac1944.c line 2409
Quoted 12 lines
drivers/iio/adc/pac1944.c new file +12
-0+static int pac1944_read_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; + +/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ; + +switch (chan->type) {
Should it be protected by chip_info->lock, as done in pac1944_read_event_config()?
Quoted 36 lines
drivers/iio/adc/pac1944.c new file +36
-0+case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + *val = chip_info->overvoltage[idx]; +return IIO_VAL_INT ; +case IIO_EV_DIR_FALLING : + *val = chip_info->undervoltage[idx]; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +case IIO_CURRENT : +switch (dir) { +case IIO_EV_DIR_RISING : + *val = chip_info->overcurrent[idx]; +return IIO_VAL_INT ; +case IIO_EV_DIR_FALLING : + *val = chip_info->undercurrent[idx]; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +case IIO_POWER : +switch (dir) { +case IIO_EV_DIR_RISING : + *val = chip_info->overpower[idx]; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} + +static int pac1944_write_thresh(struct iio_dev *indio_dev,
drivers/iio/adc/pac1944.c line 2455
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ 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 ; +
Why are scoped_guard() in each cases? Could'nt it be done as ni pac1944_read_event_config()?
Quoted 13 lines
drivers/iio/adc/pac1944.c new file +13
-0+switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + scoped_guard(mutex, &chip_info->lock) { + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_OV_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01UV_MASK >> idx), + val); +if (!ret) + chip_info->overvoltage[idx] = val; + } +return ret;
...
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+static int pac1944_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +int state) +{
drivers/iio/adc/pac1944.c line 2643
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+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 */ + idx = chan->channel -1 ; + +guard (mutex)(&info->lock);
This could maybe be moved after the switch(), if this makes sense?
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+ +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING :
...
Quoted 16 lines
drivers/iio/adc/pac1944.c new file +16
-0+static int pac1944_prep_iio_channels (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +struct device *dev = &info->client->dev; +struct iio_chan_spec *ch_sp; +int channel_size, attribute_count; +int cnt; +void *dyn_ch_struct, *tmp_data; + +/* Finding out dynamically how many IIO channels we need */ + attribute_count =0 ; + channel_size =0 ; +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +if (!info->active_channels[cnt]) +continue ; +
drivers/iio/adc/pac1944.c line 3289
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* add the size of the properties of one chip physical channel */ + channel_size +=sizeof (pac1944_single_channel); +/* count how many enabled channels we have */ + attribute_count +=ARRAY_SIZE (pac1944_single_channel); +dev_info (dev,":%s: Channel %d active\n" , __func__, cnt +1 ); + } + + dyn_ch_struct =devm_kzalloc (dev, channel_size,GFP_KERNEL ); +if (!dyn_ch_struct) +return -EINVAL ;
-ENOMEM?
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+ + tmp_data = dyn_ch_struct; +/* Populate the dynamic channels and make all the adjustments */ +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +if (!info->active_channels[cnt]) +continue ;
...
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+static int pac1944_probe (struct i2c_client *client) +{ +struct pac1944_chip_info *info; +struct iio_dev *indio_dev; +const struct pac1944_features *chip; +bool match = false; +int cnt, ret; +struct device *dev = &client->dev; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*info)); +if (!indio_dev)
drivers/iio/adc/pac1944.c line 3371
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+return -ENOMEM ; + + info =iio_priv (indio_dev); + info->client = client; + + ret =pac1944_chip_identify (info); +if (ret <0 ) { +/* +* If failed to identify the hardware based on internal +* registers,try using fallback compatible in device tree to
Nitpick: space missing after the comma.
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* deal with some newer part number. +*/ + chip =i2c_get_match_data (client); +if (!chip) +return -EINVAL ; + + info->chip_variant = chip->prod_id; + info->phys_channels = chip->phys_channels; + indio_dev->name = chip->name; + }else {
...
drivers/iio/adc/pac1944.c line 3429
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+ ret =pac1944_reg_snapshot (info, true, false, +PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + + ret =devm_iio_device_register (&client->dev, indio_dev); +
Nitpick: needeless new line
drivers/iio/adc/pac1944.c line 3435
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+if (ret <0 ) +return dev_err_probe (dev, ret, +"Can't register IIO device\n" ); + +return 0 ; +}
CJ
...
kernel test robot
build botRe: [PATCH v1 2/2] iio: adc: adding support for PAC194X
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 jic23-iio/togreg]
[also build test WARNING on linus/master v6.10 next-20240719] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/marius-cristea-microchip-com/dt-bindings-iio-adc-adding-support-for-PAC194X/20240720-014249 base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg patch link: https://lore.kernel.org/r/20240719173855.53261-3-marius.cristea%40microchip.com patch subject: [PATCH v1 2/2] iio: adc: adding support for PAC194X config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20240720/[email protected]/config) compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project ad154281230d83ee551e12d5be48bb956ef47ed3) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240720/[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 >>):
In file included from drivers/iio/adc/pac1944.c:16:
In file included from include/linux/acpi.h:14:
In file included from include/linux/device.h:32:
In file included from include/linux/device/driver.h:21:
In file included from include/linux/module.h:19:
In file included from include/linux/elf.h:6:
In file included from arch/s390/include/asm/elf.h:173:
In file included from arch/s390/include/asm/mmu_context.h:11:
In file included from arch/s390/include/asm/pgalloc.h:18:
In file included from include/linux/mm.h:2258:
include/linux/vmstat.h:500:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
500 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
501 | item];
| ~~~~
include/linux/vmstat.h:507:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
507 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
508 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
514 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
include/linux/vmstat.h:519:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
519 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
520 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:528:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
528 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
529 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/iio/adc/pac1944.c:1691:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
1691 | if (time_after(jiffies, info->chip_reg_data.jiffies_tstamp +
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1692 | msecs_to_jiffies(PAC1944_MIN_POLLING_TIME_MS))) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/jiffies.h:128:2: note: expanded from macro 'time_after'
128 | (typecheck(unsigned long, a) && \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129 | typecheck(unsigned long, b) && \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130 | ((long)((b) - (a)) < 0))
| ~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iio/adc/pac1944.c:1709:9: note: uninitialized use occurs here
1709 | return ret;
| ^~~
drivers/iio/adc/pac1944.c:1691:2: note: remove the 'if' if its condition is always true
1691 | if (time_after(jiffies, info->chip_reg_data.jiffies_tstamp +
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1692 | msecs_to_jiffies(PAC1944_MIN_POLLING_TIME_MS))) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iio/adc/pac1944.c:1686:9: note: initialize the variable 'ret' to silence this warning
1686 | int ret;
| ^
| = 0
drivers/iio/adc/pac1944.c:2048:16: warning: variable 'idx' set but not used [-Wunused-but-set-variable]
2048 | int ch, i, j, idx;
| ^7 warnings generated.
vim +1691 drivers/iio/adc/pac1944.c
1683
1684 static int pac1944_retrieve_data(struct pac1944_chip_info *info, u32 wait_time)
1685 {
1686 int ret;
1687 /*
1688 * Check if the minimal elapsed time has passed and if so,
1689 * re-read the chip, otherwise the cached info is just fine
1690 */
> 1691 if (time_after(jiffies, info->chip_reg_data.jiffies_tstamp +
1692 msecs_to_jiffies(PAC1944_MIN_POLLING_TIME_MS))) {
1693 /*
1694 * We need to re-read the chip values
1695 * call the pac1944_reg_snapshot
1696 */
1697 ret = pac1944_reg_snapshot(info, true,
1698 PAC1944_REFRESH_REG_ADDR,
1699 wait_time);
1700 /*
1701 * Re-schedule the work for the read registers timeout
1702 * (to prevent chip regs saturation)
1703 */
1704 cancel_delayed_work_sync(&info->work_chip_rfsh);
1705 schedule_delayed_work(&info->work_chip_rfsh,
1706 msecs_to_jiffies(PAC1944_MAX_RFSH_LIMIT_MS));
1707 }
1708
1709 return ret;
1710 }
1711
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v1 2/2] iio: adc: adding support for PAC194X
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 jic23-iio/togreg]
[also build test WARNING on linus/master v6.10 next-20240719] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/marius-cristea-microchip-com/dt-bindings-iio-adc-adding-support-for-PAC194X/20240720-014249 base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg patch link: https://lore.kernel.org/r/20240719173855.53261-3-marius.cristea%40microchip.com patch subject: [PATCH v1 2/2] iio: adc: adding support for PAC194X config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20240721/[email protected]/config) compiler: sh4-linux-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240721/[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/pac1944.c: In function 'pac1944_prep_custom_attributes':
>> drivers/iio/adc/pac1944.c:2048:23: warning: variable 'idx' set but not used [-Wunused-but-set-variable]
2048 | int ch, i, j, idx;
| ^~~vim +/idx +2048 drivers/iio/adc/pac1944.c
2044
2045 static int pac1944_prep_custom_attributes(struct pac1944_chip_info *info,
2046 struct iio_dev *indio_dev)
2047 {
> 2048 int ch, i, j, idx;
2049 int active_channels_count = 0;
2050 struct attribute **pac1944_custom_attrs, **tmp_attr;
2051 struct attribute_group *pac1944_group;
2052 int custom_attr_cnt;
2053 struct i2c_client *client = info->client;
2054
2055 for (i = 0 ; i < info->phys_channels; i++)
2056 if (info->active_channels[i])
2057 active_channels_count++;
2058
2059 pac1944_group = devm_kzalloc(&client->dev, sizeof(*pac1944_group), GFP_KERNEL);
2060 if (!pac1944_group)
2061 return -ENOMEM;
2062
2063 /*
2064 * Attributes for channel X:
2065 * - in_shunt_value_X
2066 * - in_oc_limit_nsamples
2067 * - in_uc_limit_nsamples
2068 * - in_op_limit_nsamples
2069 * - in_ov_limit_nsamples
2070 * - in_uv_limit_nsamples
2071 * - one of pair attributes:
2072 * - in_power_accX_raw and in_power_accX_scale
2073 * - in_current_accX_raw and in_current_accX_scale
2074 * - in_voltage_accX_raw and in_voltage_accX_scale
2075 * Shared attributes:
2076 * - in_acc_fullness
2077 * - in_alert_enable
2078 * - in_slow_alert1
2079 * - gpio_alert2
2080 * - out_alert_status
2081 * NULL
2082 */
2083 custom_attr_cnt = PAC1944_COMMON_DEVATTR * active_channels_count;
2084 custom_attr_cnt += PAC1944_ACC_DEVATTR * active_channels_count;
2085 custom_attr_cnt += PAC1944_SHARED_DEVATTRS_COUNT;
2086
2087 pac1944_custom_attrs = devm_kzalloc(&client->dev, custom_attr_cnt *
2088 sizeof(*pac1944_group) + 1, GFP_KERNEL);
2089 if (!pac1944_custom_attrs)
2090 return -ENOMEM;
2091
2092 j = 0;
2093 for (ch = 0 ; ch < info->phys_channels; ch++) {
2094 if (!info->active_channels[ch])
2095 continue;
2096
2097 for (i = 0; i < PAC1944_COMMON_DEVATTR; i++)
2098 pac1944_custom_attrs[j++] =
2099 pac1944_all_attrs[PAC1944_COMMON_DEVATTR * ch + i];
2100
2101 idx = ch;
2102 switch (info->chip_reg_data.accumulation_mode[ch]) {
2103 case PAC1944_ACCMODE_VPOWER:
2104 tmp_attr = pac1944_power_acc_attr;
2105 break;
2106 case PAC1944_ACCMODE_VSENSE:
2107 tmp_attr = pac1944_current_acc_attr;
2108 break;
2109 case PAC1944_ACCMODE_VBUS:
2110 tmp_attr = pac1944_voltage_acc_attr;
2111 break;
2112 default:
2113 return -EINVAL;
2114 }
2115
2116 pac1944_custom_attrs[j++] = tmp_attr[ch];
2117 pac1944_custom_attrs[j++] = pac1944_power_acc_attr[PAC1944_MAX_CH + ch];
2118 pac1944_custom_attrs[j++] = pac1944_power_acc_attr[2 * PAC1944_MAX_CH + ch];
2119 }
2120
2121 for (i = 0; i < PAC1944_SHARED_DEVATTRS_COUNT; i++)
2122 pac1944_custom_attrs[j++] =
2123 pac1944_all_attrs[PAC1944_COMMON_DEVATTR * PAC1944_MAX_CH + i];
2124
2125 pac1944_group->attrs = pac1944_custom_attrs;
2126 info->iio_info.attrs = pac1944_group;
2127
2128 return 0;
2129 }
2130
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Jonathan Cameron
maintainerRe: [PATCH v1 2/2] iio: adc: adding support for PAC194X
In reply to [email protected]Quoted 5 lines · On Fri, 19 Jul 2024 20:38:55 +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.
Quoted 7 lines
Signed-off-by Marius Cristea <[email protected]>--- .../ABI/testing/sysfs-bus-iio-adc-pac1944 | 9 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/pac1944.c | 3528 +++++++++++++++++
ouch.
For super large drivers like this it can be a good idea to build them up in bite sized pieces as it makes it less likely a reviewer thinks 'not today - it's beer o'clock'...
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 line 1
Quoted 11 lines
5 files changed, 3558 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 +1
-0@@ -0,0 +1,9 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_shunt_resistorY
Discussion about shunt resistor attributes is ongoing.
https://lore.kernel.org/all/20240720104812.5d59e91a@jic23-huawei/
One key thing is we can't have duplicate attribute docs. So much like the ones in that thread we need to move them to a common place.
Also as you'll see we were falling away from this form because it's odd and not clear what it is associated with.
May be better to associate with a particular current channel.
Anyhow, lets continue that discussion in that thread rather than duplicating here.
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 line 9
Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-pac1944 new file +8
-0+KernelVersion: 6.10 +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.
Various comments inline.
If there is any path to reducing duplication consider it.
I'm not a great fan of macros for functions but maybe they are needed here or a change in data structuring to allow more reuse.
Quoted 17 lines
drivers/iio/adc/pac1944.c new file +11
-0@@ -0,0 +1,3528 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* IIO driver for PAC194X and PAC195X series chips +* +* Copyright (C) 2022-2024 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:
drivers/iio/adc/pac1944.c
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/PAC195X-Family-Data-Sheet-DS20006539.pdf +* +*/ + +static const unsigned int samp_rate_fast_mode_tbl[] = { + [PAC1944_1_CHANNEL_ACTIVE ] =2560 , + [PAC1944_2_CHANNELS_ACTIVE ] =1707 , + [PAC1944_3_CHANNELS_ACTIVE ] =1280 , + [PAC1944_4_CHANNELS_ACTIVE ] =1024
Add a trailing comma to these. Not obvious there will never be more entries.
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+}; + +/* Available Sample Modes */ +static const char *const pac1944_frequency_avail[] = { +"1024_ADAP" , +"256_ADAP" ,
drivers/iio/adc/pac1944.c line 451
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+"64_ADAP" , +"8_ADAP" , +"1024" , +"256" , +"64" , +"8" , +"single_shot_1x" , +"single_shot_8x" , +"fast" , +"burst"
Coma after that.
However, this is not standard use of a standard attribute. Can't do that as normal software is going to break. So we need to figure out what the interface should look like. Starting point provide some docs.
drivers/iio/adc/pac1944.c line 533
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +10
-0+}; +*/ +struct pac1944_chip_info { +const struct iio_chan_spec *channels; +struct iio_info iio_info; +struct i2c_client *client; +struct mutex lock;/* lock to prevent concurrent reads/writes */ +struct delayed_work work_chip_rfsh; +u8 phys_channels; +bool active_channels[PAC1944_MAX_CH ];
Perhaps use a bitmap for this. Mainly because then you can do for_each_set_bit() etc to simplify scanning over them.
drivers/iio/adc/pac1944.c line 637
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +2
-0+ +static inline u64 pac1944_get_unaligned_be56 (u8 *p)
Add to the generic routines. Maybe another user will turn up ;)
Quoted 22 lines
drivers/iio/adc/pac1944.c new file +20
-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 ]; +} + + +static ssize_t pac1944_op_limit_nsamples_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); +int ret, idx; +u8 val, val_tmp; + + ret =kstrtou8 (buf,10 , &val_tmp); +if (ret) +return ret; +
drivers/iio/adc/pac1944.c line 975
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+if (val_tmp >3 ) +return -EINVAL ; + + idx = this_attr->address; + + scoped_guard(mutex, &info->lock) { + val = val_tmp << (6 - (idx *2 )); + ret =pac1944_update_alert_byte_data (dev,PAC1944_OP_LIMIT_NSAMPLES_REG_ADDR , + (int )(PAC1944_CH01OP_MASK >> idx), val); +if (!ret)
If it's an error return that.
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +13
-0+ info->op_limit_nsamples[idx] = val_tmp; + } + +return count; +} + +static ssize_t pac1944_alert_status_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct pac1944_chip_info *info =iio_priv (indio_dev); +struct i2c_client *client = info->client;
drivers/iio/adc/pac1944.c line 1201
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+int ret; +u32 tmp; +u8 status[3 ]; + + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_ALERT_STATUS_REG_ADDR , +ARRAY_SIZE (status), (u8 *)status); +if (ret <0 ) { +dev_err (dev,"%s - cannot read PAC1944 regs from 0x%02X\n" , + __func__,PAC1944_ALERT_STATUS_REG_ADDR ); +return 0 ;
Why return 0?
drivers/iio/adc/pac1944.c
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +7
-0+ } + tmp =get_unaligned_be24 (&status[0 ]); + +return sysfs_emit (buf,"%u\n" , tmp); +} +static struct attribute *pac1944_all_attrs[] = { +PAC1944_DEV_ATTR (in_shunt_resistor1),
Everything in here needs documenting not just the shunt resistor.
Quoted 35 lines
drivers/iio/adc/pac1944.c new file +34
-0+PAC1944_DEV_ATTR (in_oc_limit_nsamples1), +PAC1944_DEV_ATTR (in_uc_limit_nsamples1), +PAC1944_DEV_ATTR (in_op_limit_nsamples1), +PAC1944_DEV_ATTR (in_ov_limit_nsamples1), +PAC1944_DEV_ATTR (in_uv_limit_nsamples1), +PAC1944_DEV_ATTR (in_shunt_resistor2), +PAC1944_DEV_ATTR (in_oc_limit_nsamples2), +PAC1944_DEV_ATTR (in_uc_limit_nsamples2), +PAC1944_DEV_ATTR (in_op_limit_nsamples2), +PAC1944_DEV_ATTR (in_ov_limit_nsamples2), +PAC1944_DEV_ATTR (in_uv_limit_nsamples2), +PAC1944_DEV_ATTR (in_shunt_resistor3), +PAC1944_DEV_ATTR (in_oc_limit_nsamples3), +PAC1944_DEV_ATTR (in_uc_limit_nsamples3), +PAC1944_DEV_ATTR (in_op_limit_nsamples3), +PAC1944_DEV_ATTR (in_ov_limit_nsamples3), +PAC1944_DEV_ATTR (in_uv_limit_nsamples3), +PAC1944_DEV_ATTR (in_shunt_resistor4), +PAC1944_DEV_ATTR (in_oc_limit_nsamples4), +PAC1944_DEV_ATTR (in_uc_limit_nsamples4), +PAC1944_DEV_ATTR (in_op_limit_nsamples4), +PAC1944_DEV_ATTR (in_ov_limit_nsamples4), +PAC1944_DEV_ATTR (in_uv_limit_nsamples4), +PAC1944_DEV_ATTR (in_acc_fullness), +PAC1944_DEV_ATTR (in_alert_enable), +PAC1944_DEV_ATTR (in_slow_alert), +PAC1944_DEV_ATTR (in_gpio_alert), +PAC1944_DEV_ATTR (out_alert_status), +NULL +}; + +#define PAC1944_VSENSE_CHANNEL (_index, _si, _address, _ev_spec, _num_ev_spec) {\ + .type = IIO_CURRENT, \ + .address = (_address), \
drivers/iio/adc/pac1944.c line 1382
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ .indexed = 1, \ + .channel = (_index) + 1, \ + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .scan_index = (_si), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = 16, \ + .storagebits = 16, \ + .endianness = IIO_CPU, \
You don't yet do buffered capture. Until you do, better not to specify this stuff as it is unused and might be wrong.. (see scan_index comments later).
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+ }, \ + .event_spec = (_ev_spec), \ + .num_event_specs = (_num_ev_spec), \ + .ext_info = pac1944_ext_info \
drivers/iio/adc/pac1944.c line 1455
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+} +static int pac1944_reg_snapshot (struct pac1944_chip_info *info, +bool do_refresh,u8 refresh_cmd,u32 wait_time) +{ +int ret; +struct i2c_client *client = info->client; +u8 shift, idx; +u8 *offset_reg_data_p; +int cnt;
Combine this with ret;
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +18
-0+u32 count, inc_count; +u32 fs =0 ; +s64 stored_value, tmp_s64; +s64 inc =0 ; +u16 tmp_u16; +bool is_unipolar; + +guard (mutex)(&info->lock); + +if (do_refresh) { + ret =pac1944_send_refresh (info, refresh_cmd, wait_time); +if (ret <0 ) { +dev_err (&client->dev,"%s - cannot send refresh towards PAC1944\n" , + __func__); +return ret; + } + } +
drivers/iio/adc/pac1944.c line 1483
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* Read the ctrl/status registers for this snapshot */ + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_CTRL_ACT_REG_ADDR , +sizeof (tmp_u16), (u8 *)&tmp_u16); +if (ret <0 ) { +dev_err (&client->dev,"%s - cannot read PAC1944 regs from 0x%02X\n" , + __func__,PAC1944_CTRL_ACT_REG_ADDR ); +return ret; + } + +be16_to_cpus (&tmp_u16);
As in cases below I'd prefer a read into __be16 etc and be16_to_cpu() on the correct type.
drivers/iio/adc/pac1944.c line 1535
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+ info->chip_reg_data.ctrl_act_reg = tmp_u16; + + info->chip_reg_data.vacc[cnt] = +pac1944_get_unaligned_be56 (offset_reg_data_p); + is_unipolar = true; + +switch (info->chip_reg_data.accumulation_mode[cnt]) { +case PAC1944_ACCMODE_VPOWER : +if (info->chip_reg_data.vbus_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG || + info->chip_reg_data.vsense_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG ) + is_unipolar = FALSE;
false
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+break ; +case PAC1944_ACCMODE_VBUS : +if (info->chip_reg_data.vbus_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG ) + is_unipolar = FALSE;
drivers/iio/adc/pac1944.c line 1549
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+break ; +case PAC1944_ACCMODE_VSENSE : +if (info->chip_reg_data.vsense_mode[cnt] !=PAC1944_UNIPOLAR_FSR_CFG ) + is_unipolar = FALSE; +break ; + } + +if (!is_unipolar) + info->chip_reg_data.vacc[cnt] = +sign_extend64 (info->chip_reg_data.vacc[cnt],55 );
Add another tab before the sign_extent64()
Quoted 21 lines
drivers/iio/adc/pac1944.c new file +21
-0+ +if (info->chip_reg_data.accumulation_mode[cnt] !=PAC1944_ACCMODE_VBUS ) { +/* +* Integrate the accumulated power or current over +* the elapsed interval. +*/ + tmp_u16 = info->chip_reg_data.ctrl_lat_reg >>12 ; + + tmp_s64 = info->chip_reg_data.vacc[cnt]; +if (tmp_u16 <PAC1944_SAMP_FAST_MODE ) { +/* +* Find how much shift is required by the sample rate +* The chip's sampling rate is 2^shift samples/sec +*/ + shift = shift_map_tbl[tmp_u16]; + inc = tmp_s64 >> shift; + }else if (tmp_u16 <=PAC1944_SAMP_BURST_MODE ) { + idx = info->num_enabled_channels -1 ; + +if (tmp_u16 ==PAC1944_SAMP_FAST_MODE ) + fs = samp_rate_fast_mode_tbl[idx];
drivers/iio/adc/pac1944.c line 1580
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+else +/* tmp_u16 == PAC1944_SAMP_BURST_MODE) */ + fs = samp_rate_burst_mode_tbl[idx]; + + inc =div_u64 (abs (tmp_s64), fs); +if (tmp_s64 <0 ) + inc = -inc; + }else { +dev_err (&client->dev,"Invalid sample rate index: %d!\n" , + tmp_u16);
This isn't expected to occur. I'd just error out. Device is likely in dead state anyway if we see this.
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ } + }else { + count = info->chip_reg_data.total_samples_nr[cnt]; + inc_count = info->chip_reg_data.acc_count; + +/* Check if total number of samples will overflow */ +if (unlikely (check_add_overflow (count, inc_count, &count))) { +dev_err (&client->dev, +"Number of samples on channel [%d] overflow!\n" , + cnt +1 );
drivers/iio/adc/pac1944.c line 1600
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ info->chip_reg_data.total_samples_nr[cnt] =0 ; + info->chip_reg_data.acc_val[cnt] =0 ; + } + + info->chip_reg_data.total_samples_nr[cnt] += inc_count; + + inc = info->chip_reg_data.vacc[cnt]; + } + +if (unlikely (check_add_overflow (stored_value, inc, &stored_value))) {
Don't bother with unlikely marking. These are only worth doing if you have numbers to back them up to show significant perf improvement.
Let the branch predictors do their work here. Or if CPU is mostly in order the unlikely isn't that useful anyway.
Quoted 13 lines
drivers/iio/adc/pac1944.c new file +13
-0+if (is_negative (stored_value)) + info->chip_reg_data.acc_val[cnt] =S64_MIN ; +else + info->chip_reg_data.acc_val[cnt] =S64_MAX ; + +dev_err (&client->dev,"Overflow detected on channel [%d]!\n" , + cnt +1 ); + }else { + info->chip_reg_data.acc_val[cnt] += inc; + } + + offset_reg_data_p +=PAC1944_VACC_REG_LEN ; + }
...
Quoted 21 lines
drivers/iio/adc/pac1944.c new file +21
-0+ +static int pac1944_prep_custom_attributes (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +int ch, i, j, idx; +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; + +for (i =0 ; i < info->phys_channels; i++) +if (info->active_channels[i]) + active_channels_count++; + + pac1944_group =devm_kzalloc (&client->dev,sizeof (*pac1944_group),GFP_KERNEL ); +if (!pac1944_group) +return -ENOMEM ; + +/* +* Attributes for channel X:
drivers/iio/adc/pac1944.c line 2074
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+* - in_shunt_value_X +* - in_oc_limit_nsamples +* - in_uc_limit_nsamples +* - in_op_limit_nsamples +* - in_ov_limit_nsamples +* - in_uv_limit_nsamples +* - 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
These all need documentation. Potentailly these should be in_voltageX_acc_raw.
I'm not sure they make sense as separate channels unless people will want to capture them via a buffered mode. Given the whole point is to get a number over a long time period I'm not sure that usecase exists. So maybe we treat them like existing average info_mask element.
drivers/iio/adc/pac1944.c line 2078
Quoted 4 lines
drivers/iio/adc/pac1944.c new file +4
-0+* Shared attributes: +* - in_acc_fullness +* - in_alert_enable +* - in_slow_alert1
Not sure why these can't be handled with normal event controls, but with docs it will be easier to tell.
Quoted 60 lines
drivers/iio/adc/pac1944.c new file +60
-0+* - gpio_alert2 +* - out_alert_status +* 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 (ch =0 ; ch < info->phys_channels; ch++) { +if (!info->active_channels[ch]) +continue ; + +for (i =0 ; i <PAC1944_COMMON_DEVATTR ; i++) + pac1944_custom_attrs[j++] = + pac1944_all_attrs[PAC1944_COMMON_DEVATTR * ch + i]; + + idx = ch; +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_be16, tmp_u16;
drivers/iio/adc/pac1944.c line 2148
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_CTRL_ACT_REG_ADDR , +sizeof (tmp_u16), (u8 *)&tmp_u16); +if (ret <0 ) { +dev_err (&indio_dev->dev,"%s - cannot read PAC1944 regs from 0x%02X\n" , + __func__,PAC1944_CTRL_ACT_REG_ADDR ); +return ret; + } + +be16_to_cpus (&tmp_u16);
As below - read into a __be16 then convert to u16 for next bit.
drivers/iio/adc/pac1944.c line 2149
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+ tmp_u16 &= (u16 )0x00ff ;
Odd to nock out 4 bits that you then don't set.
Why not 0xfff?
drivers/iio/adc/pac1944.c line 2150
Quoted 1 line
drivers/iio/adc/pac1944.c new file +1
-0+ tmp_u16 |= (u16)(mode <<12 );
FIELD_PREP;
Quoted 20 lines
drivers/iio/adc/pac1944.c new file +20
-0+ tmp_be16 = tmp_u16; +cpu_to_be16s (&tmp_be16); + + scoped_guard(mutex, &info->lock) { + ret =i2c_smbus_write_word_data (client,PAC1944_CTRL_REG_ADDR , tmp_be16); +if (ret <0 ) { +dev_err (&indio_dev->dev,"Failed to configure sampling mode\n" ); +return ret; + } + + info->sampling_mode = mode; + info->chip_reg_data.ctrl_act_reg = tmp_u16; + } + + ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + +return 0 ; +}
drivers/iio/adc/pac1944.c line 2190
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+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, +}; + +static const struct iio_chan_spec_ext_info pac1944_ext_info[] = { +IIO_ENUM ("sampling_frequency" ,IIO_SHARED_BY_ALL , &sampling_mode_enum),
Why not use the normal info_mask elements? Where possible I'd prefer to see that to ext_info stuff.
Quoted 16 lines
drivers/iio/adc/pac1944.c new file +16
-0+ { + .name = "sampling_frequency_available", + .shared = IIO_SHARED_BY_ALL, + .read = iio_enum_available_read, + .private = (uintptr_t)&sampling_mode_enum, + }, + {} +}; + +static int pac1944_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +int ret, idx; +u64 tmp; +
drivers/iio/adc/pac1944.c line 2216
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ ret =pac1944_retrieve_data (info,PAC1944_MIN_UPDATE_WAIT_TIME_US ); +if (ret <0 ) +return ret; + +/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ; + +/* +* For AVG the index should be between 5 to 8. +* To calculate PAC1944_CH_VOLTAGE_AVERAGE and
Very short wrap. Go for < 80 chars (just)
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+* PAC1944_CH_CURRENT_AVERAGE real index, we need +* to remove the added offset (PAC1944_MAX_CH). +*/
..
Quoted 27 lines
drivers/iio/adc/pac1944.c new file +27
-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 ; + +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : + scoped_guard(mutex, &chip_info->lock) { + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_OV_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01UV_MASK >> idx), + val); +if (!ret) + chip_info->overvoltage[idx] = val; + } +return ret; +case IIO_EV_DIR_FALLING : + scoped_guard(mutex, &chip_info->lock) {
drivers/iio/adc/pac1944.c line 2480
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_UV_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01UV_MASK >> idx), + val); +if (!ret) + chip_info->undervoltage[idx] = val; + } +return ret; +default : +return -EINVAL ;
Given we don't care about performance of error paths I'd just take guard(mutex)(&chip_info->lock)
outside the outer switch statement.
Quoted 55 lines
drivers/iio/adc/pac1944.c new file +55
-0+ } +case IIO_CURRENT : +switch (dir) { +case IIO_EV_DIR_RISING : + scoped_guard(mutex, &chip_info->lock) { + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_OC_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01OC_MASK >> idx), + val); +if (!ret) + chip_info->overcurrent[idx] = val; + } +return ret; +case IIO_EV_DIR_FALLING : + scoped_guard(mutex, &chip_info->lock) { + ret =pac1944_update_alert_16b (&indio_dev->dev, +PAC1944_UC_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01UC_MASK >> idx), + val); +if (!ret) + chip_info->undercurrent[idx] = val; + } +return ret; +default : +return -EINVAL ; + } +case IIO_POWER : +switch (dir) { +case IIO_EV_DIR_RISING : + scoped_guard(mutex, &chip_info->lock) { + ret =pac1944_update_alert_24b (&indio_dev->dev, +PAC1944_OP_LIMIT_REG_ADDR + idx, + (int )(PAC1944_CH01OP_MASK >> idx), + val); +if (!ret) + chip_info->overpower[idx] = val; + } +return ret; +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, +enum iio_event_direction dir) +{ +struct pac1944_chip_info *info =iio_priv (indio_dev); +int idx; + +/* into the datasheet channels are noted from 1 to 4 */
drivers/iio/adc/pac1944.c line 2545
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ idx = chan->channel -1 ; + + scoped_guard(mutex, &info->lock) { +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : +switch (idx) { +case 0 : +return FIELD_GET (PAC1944_CH01OV_MASK , info->alert_enable);
As below - look these masks up in some array of const structures to avoid all these case statements across 0..3
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+case 1 : +return FIELD_GET (PAC1944_CH02OV_MASK , info->alert_enable); +case 2 : +return FIELD_GET (PAC1944_CH03OV_MASK , info->alert_enable); +case 3 : +return FIELD_GET (PAC1944_CH04OV_MASK , info->alert_enable); +default : +return -EINVAL ;
...
Quoted 14 lines
drivers/iio/adc/pac1944.c new file +14
-0+} + +static int pac1944_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +int 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 ]; +
drivers/iio/adc/pac1944.c line 2649
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* into the datasheet channels are noted from 1 to 4 */ + idx = chan->channel -1 ; + +guard (mutex)(&info->lock); + +switch (chan->type) { +case IIO_VOLTAGE : +switch (dir) { +case IIO_EV_DIR_RISING : +switch (idx) {
Use data? So define an array (or maybe array of structures) that lets you just look these up directly.
Should save a lot of code.
Quoted 16 lines
drivers/iio/adc/pac1944.c new file +16
-0+case 0 : + mask =PAC1944_CH01OV_MASK ; +break ; +case 1 : + mask =PAC1944_CH02OV_MASK ; +break ; +case 2 : + mask =PAC1944_CH03OV_MASK ; +break ; +case 3 : + mask =PAC1944_CH04OV_MASK ; +break ; +default : +return -EINVAL ; + } +break ;
...
drivers/iio/adc/pac1944.c line 2802
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+/* +* documentation related to the ACPI device definition +* https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/UserGuides/PAC194X_5X-UEFI-BIOS-Integration-and-Microsoft-Windows-10-and-Windows-11-Energy-Meter-Interface-Device-Driver-Users-Guide-DS50003155.pdf +*/ +static bool pac1944_acpi_parse_channel_config(struct i2c_client *client,
return an int to avoid eating useful error info.
Quoted 61 lines
drivers/iio/adc/pac1944.c new file +61
-0+ struct pac1944_chip_info *info) +{ +acpi_handle handle; +union acpi_object *rez; +struct device *dev = &client->dev; +unsigned short bi_dir_mask; +int i; +guid_t guid; +const struct acpi_device_id *id; + + handle =ACPI_HANDLE (dev); + + id =acpi_match_device (dev->driver->acpi_match_table, dev); +if (!id) +return false; + +guid_parse (PAC1944_DSM_UUID , &guid); + + rez =acpi_evaluate_dsm (handle, &guid,0 ,PAC1944_ACPI_GET_NAMES ,NULL ); +if (!rez) +return false; + +for (i =0 ; i < rez->package.count; i++) { + info->labels[i] =devm_kmemdup (dev, rez->package.elements[i].string.pointer, + (size_t )rez->package.elements[i].string.length +1 , +GFP_KERNEL ); + info->labels[i][rez->package.elements[i].string.length] ='\0' ; + } + +ACPI_FREE (rez); + + rez =acpi_evaluate_dsm (handle, &guid,1 ,PAC1944_ACPI_GET_UOHMS_VALS ,NULL ); +if (!rez) +return false; + +for (i =0 ; i < rez->package.count; i++) { + info->shunts[i] = rez->package.elements[i].integer.value; + info->active_channels[i] = (info->shunts[i] !=0 ); + } + +ACPI_FREE (rez); + + rez =acpi_evaluate_dsm (handle, &guid,1 ,PAC1944_ACPI_GET_BIPOLAR_SETTINGS ,NULL ); +if (!rez) +return false; + +for (i =0 ; i < (rez->package.count >>1 ); i++) { +if (!info->active_channels[i]) +continue ; + + bi_dir_mask = rez->package.elements[i].integer.value; + +if (bi_dir_mask ==PAC1944_UNIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (dev,"VBUS{%d} mode set to: %d\n" , + i, bi_dir_mask); + info->chip_reg_data.vbus_mode[i] = bi_dir_mask; + }else { +dev_err (dev,"invalid vbus-mode value on %i\n" , i); + }
drivers/iio/adc/pac1944.c line 2873
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ + bi_dir_mask = rez->package.elements[i +PAC1944_MAX_CH ].integer.value; + +if (bi_dir_mask ==PAC1944_UNIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_FSR_CFG || + bi_dir_mask ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (dev,"VSENSE{%d} mode set to: %d\n" , i, bi_dir_mask); + info->chip_reg_data.vsense_mode[i] = bi_dir_mask; + }else { +dev_err (dev,"invalid vsense-mode value on %i\n" , i);
If you get an error due to something we don't know how to parse then should return an error code and fail probe. If we don't this stuff tends not to get fixed.
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+ } + } + +ACPI_FREE (rez); + + rez =acpi_evaluate_dsm (handle, &guid,1 ,PAC1944_ACPI_GET_SAMP ,NULL ); +if (!rez)
drivers/iio/adc/pac1944.c line 2890
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+return false; + + info->sample_rate_value = rez->package.elements[0 ].integer.value; + +ACPI_FREE (rez); + +return true; +} + +static bool pac1944_of_parse_channel_config(struct i2c_client *client,
return an int so the error codes aren't eaten up.
drivers/iio/adc/pac1944.c line 2901
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+ struct pac1944_chip_info *info) +{ +struct fwnode_handle *node, *fwnode; +unsigned int current_channel; +int idx, ret; +int temp; + + current_channel =1 ; + + fwnode =dev_fwnode (&client->dev); +fwnode_for_each_available_child_node (fwnode, node) {
Ah. This old one - there was a debate about improving the docs on this but not sure it ever got resolved.
Just use
device_for_each_child_node_scoped()
It only applies to available nodes and you can avoid the goto fun.
drivers/iio/adc/pac1944.c line 2906
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+ ret =fwnode_property_read_u32 (node,"reg" , &idx); +if (ret) { +dev_err_probe (&client->dev, ret, +"reading invalid channel index\n" ); + goto err_fwnode;
return dev_err_probe() etc once using the scope loop that releases the fwnode automatically.
Quoted 108 lines
drivers/iio/adc/pac1944.c new file +108
-0+ } +/* 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 (&client->dev, +"invalid channel_index %d value on %s\n" , + (idx +1 ),fwnode_get_name (node)); + 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 (&client->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" )) { +fwnode_property_read_string (node,"label" , + (const char **)&info->labels[idx]); + } + + ret =fwnode_property_read_u32 (node,"microchip,vbus-mode" , &temp); +if (ret) { +dev_err (&client->dev, +"invalid vbus-mode value on %s\n" , +fwnode_get_name (node)); + goto err_fwnode; + } +if (temp ==PAC1944_UNIPOLAR_FSR_CFG || + temp ==PAC1944_BIPOLAR_FSR_CFG || + temp ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (&client->dev, +"VBUS{%d} mode set to: %d\n" , + idx, temp); + info->chip_reg_data.vbus_mode[idx] = temp; + }else { +dev_err (&client->dev, +"invalid vbus-mode value on %s\n" , +fwnode_get_name (node)); + goto err_fwnode; + } + + ret =fwnode_property_read_u32 (node,"microchip,vsense-mode" , &temp); +if (ret) { +dev_err (&client->dev, +"invalid vsense-mode value on %s\n" , +fwnode_get_name (node)); + goto err_fwnode; + } +if (temp ==PAC1944_UNIPOLAR_FSR_CFG || + temp ==PAC1944_BIPOLAR_FSR_CFG || + temp ==PAC1944_BIPOLAR_HALF_FSR_CFG ) { +dev_dbg (&client->dev, +"VSENSE{%d} mode set to: %d\n" , + idx, temp); + info->chip_reg_data.vsense_mode[idx] = temp; + }else { +dev_err (&client->dev, +"invalid vsense-mode value on %s\n" , +fwnode_get_name (node)); + goto err_fwnode; + } + + ret =fwnode_property_read_u32 (node,"microchip,accumulation-mode" , &temp); +if (ret) { +dev_err (&client->dev, +"invalid accumulation-mode value on %s\n" , +fwnode_get_name (node)); + goto err_fwnode; + } +if (temp ==PAC1944_ACCMODE_VPOWER || + temp ==PAC1944_ACCMODE_VSENSE || + temp ==PAC1944_ACCMODE_VBUS ) { +dev_dbg (&client->dev, +"Accumulation{%d} mode set to: %d\n" , + idx, temp); + info->chip_reg_data.accumulation_mode[idx] = temp; + }else { +dev_err (&client->dev, +"invalid mode for accumulator value on %s\n" , +fwnode_get_name (node)); + } + current_channel++; + } + +return true; + +err_fwnode: +fwnode_handle_put (node); + +return false; +} + +static void pac1944_cancel_delayed_work (void *dwork) +{ +cancel_delayed_work_sync (dwork); +} + +static int pac1944_chip_identify (struct pac1944_chip_info *info) +{
drivers/iio/adc/pac1944.c line 3024
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+int ret =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 ) { +dev_err (&client->dev,"cannot read revision\n" ); +return ret;
deV_err_probe()
Quoted 5 lines
drivers/iio/adc/pac1944.c new file +5
-0+ } + +dev_info (&client->dev,"Chip revision: 0x%02X\n" , chip_rev_info[2 ]); + info->chip_revision = chip_rev_info[2 ]; + info->chip_variant = chip_rev_info[0 ];
drivers/iio/adc/pac1944.c line 3039
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +switch (chip_rev_info[0 ]) { +case PAC_PRODUCT_ID_1941_1 : +case PAC_PRODUCT_ID_1942_1 : +case PAC_PRODUCT_ID_1943_1 : +case PAC_PRODUCT_ID_1944_1 : +case PAC_PRODUCT_ID_1941_2 : +case PAC_PRODUCT_ID_1942_2 : + info->is_pac195x_family = false; +return (chip_rev_info[0 ] -PAC_PRODUCT_ID_1941_1 );
drop the outer brackets as don't add anything
drivers/iio/adc/pac1944.c line 3048
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+case PAC_PRODUCT_ID_1951_1 : +case PAC_PRODUCT_ID_1952_1 : +case PAC_PRODUCT_ID_1953_1 : +case PAC_PRODUCT_ID_1954_1 : +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_1 ) + + (PAC_PRODUCT_ID_1942_2 -PAC_PRODUCT_ID_1941_1 ) +1 );
Same here. The outermost brackets aren't needed.
drivers/iio/adc/pac1944.c line 3059
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+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 =0 ;
ret always set before use.
Also don't combine declarations that set values with ones that don't. It isn't good for readability as can easily miss one that isn't set amongst many that are.
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+struct i2c_client *client = info->client; +u8 regs[PAC1944_ALERTS_REG_LEN ]; +u8 *offset_p; +u32 wait_time; +u8 tmp_u8; +u16 tmp_u16;
drivers/iio/adc/pac1944.c line 3075
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ +/* +* Counting how many channels are enabled and store +* this information within the driver data +*/ + cnt =0 ; + info->num_enabled_channels =0 ; +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +if (info->active_channels[cnt]) + info->num_enabled_channels++;
with a bitmap, consider hweight()
drivers/iio/adc/pac1944.c line 3083
Quoted 8 lines
drivers/iio/adc/pac1944.c new file +8
-0+ } + +/* get sampling rate from PAC */ + ret =i2c_smbus_read_i2c_block_data (client,PAC1944_CTRL_REG_ADDR , +sizeof (tmp_u16), (u8 *)&tmp_u16); +if (ret <0 ) { +dev_err (&client->dev,"cannot read 0x%02X reg\n" ,PAC1944_CTRL_REG_ADDR ); +return ret;
If only used from probe, return dev_err_probe()
etc
drivers/iio/adc/pac1944.c
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ } +be16_to_cpus (&tmp_u16);
Better to keep types clean. So read into a local __be16 then convert.
drivers/iio/adc/pac1944.c line 3087
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+ + info->sampling_mode =FIELD_GET (PAC1944_CTRL_SAMPLE_MASK , tmp_u16);
can do that conversion inline so no need to set tmp_u16 at all be16_to_cpu(tmp_be16)
Quoted 6 lines
drivers/iio/adc/pac1944.c new file +6
-0+ +/* +* The current/voltage can be measured unidirectional, bidirectional or half FSR +* no SLOW triggered REFRESH, clear POR +*/ +
drivers/iio/adc/pac1944.c line 3103
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ tmp_u16 =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 ]); + +cpu_to_be16s (&tmp_u16);
Again, I'd prefer to see explicit type for the __be16 storage.
One of the static analysers really doesn't like them being stored in non __be types.
drivers/iio/adc/pac1944.c line 3112
Quoted 9 lines
drivers/iio/adc/pac1944.c new file +9
-0+ + ret =i2c_smbus_write_word_data (client,PAC1944_NEG_PWR_FSR_REG_ADDR , tmp_u16); +if (ret) { +dev_err (&client->dev,"cannot write 0x%02X reg\n" , +PAC1944_NEG_PWR_FSR_REG_ADDR ); +return ret; + } + + tmp_u16 =0 ;
just put the 0 inline.
Quoted 30 lines
drivers/iio/adc/pac1944.c new file +30
-0+ ret =i2c_smbus_write_word_data (client,PAC1944_SLOW_REG_ADDR , tmp_u16); +if (ret <0 ) { +dev_err (&client->dev,"cannot write 0x%02X reg\n" , +PAC1944_SLOW_REG_ADDR ); +return ret; + } + +/* Write the CHANNEL_N_OFF from CTRL REGISTER */ + tmp_u16 =FIELD_PREP (PAC1944_CTRL_SAMPLE_MASK , info->sampling_mode) | +FIELD_PREP (PAC1944_CTRL_GPIO_ALERT2_MASK ,0 ) | +FIELD_PREP (PAC1944_CTRL_SLOW_ALERT1_MASK ,0 ) | +FIELD_PREP (PAC1944_CTRL_CH_1_OFF_MASK , !(info->active_channels[0 ])) | +FIELD_PREP (PAC1944_CTRL_CH_2_OFF_MASK , !(info->active_channels[1 ])) | +FIELD_PREP (PAC1944_CTRL_CH_3_OFF_MASK , !(info->active_channels[2 ])) | +FIELD_PREP (PAC1944_CTRL_CH_4_OFF_MASK , !(info->active_channels[3 ])); + +cpu_to_be16s (&tmp_u16); + + ret =i2c_smbus_write_word_data (client,PAC1944_CTRL_REG_ADDR , tmp_u16); +if (ret) { +dev_err (&client->dev,"cannot write 0x%02X reg\n" , +PAC1944_CTRL_REG_ADDR ); +return ret; + } + + tmp_u8 =ACCUM_REG (info->chip_reg_data.accumulation_mode[0 ], + info->chip_reg_data.accumulation_mode[1 ], + info->chip_reg_data.accumulation_mode[2 ], + info->chip_reg_data.accumulation_mode[3 ]); +
drivers/iio/adc/pac1944.c line 3152
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ ret =i2c_smbus_write_byte_data (client,PAC1944_ACCUM_CFG_REG_ADDR , tmp_u8); +if (ret) { +dev_err (&client->dev,"cannot write 0x%02X reg\n" , +PAC1944_ACCUM_CFG_REG_ADDR ); +return ret; + } + +/* reading all alerts, status and limits related registers */ + ret =pac1944_i2c_read (client,PAC1944_ALERT_STATUS_REG_ADDR , + (u8 *)regs,sizeof (regs));
regs is an u8 * already.
Quoted 71 lines
drivers/iio/adc/pac1944.c new file +71
-0+if (ret <0 ) { +dev_err (&client->dev,"cannot read 0x%02X reg\n" , +PAC1944_ALERT_STATUS_REG_ADDR ); +return ret; + } + + offset_p = ®s[0 ]; + +/* skip alert_status register*/ + offset_p +=3 ; + + info->slow_alert1 =get_unaligned_be24 (offset_p); + offset_p +=3 ; + + info->gpio_alert2 =get_unaligned_be24 (offset_p); + offset_p +=3 ; + + tmp_u16 =get_unaligned_be16 (offset_p); + offset_p +=2 ; + + info->acc_fullness =get_unaligned_be16 (offset_p); + offset_p +=2 ; + +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) { + info->overcurrent[cnt] =get_unaligned_be16 (offset_p); + offset_p +=2 ; + } + +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) { + info->undercurrent[cnt] =get_unaligned_be16 (offset_p); + offset_p +=2 ; + } + +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) { + info->overpower[cnt] =get_unaligned_be24 (offset_p); + offset_p +=3 ; + } + +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) { + info->overvoltage[cnt] =get_unaligned_be16 (offset_p); + offset_p +=2 ; + } + +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) { + info->undervoltage[cnt] =get_unaligned_be16 (offset_p); + offset_p +=2 ; + } + + offset_p +=1 ; +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) + info->oc_limit_nsamples[cnt] = (*offset_p >> (2 * cnt)) &0x03 ; + + offset_p +=1 ; +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) + info->uc_limit_nsamples[cnt] = (*offset_p >> (2 * cnt)) &0x03 ; + + offset_p +=1 ; +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) + info->op_limit_nsamples[cnt] = (*offset_p >> (2 * cnt)) &0x03 ; + + offset_p +=1 ; +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) + info->ov_limit_nsamples[cnt] = (*offset_p >> (2 * cnt)) &0x03 ; + + offset_p +=1 ; +for (cnt =0 ; cnt <PAC1944_MAX_CH ; cnt++) + info->uv_limit_nsamples[cnt] = (*offset_p >> (2 * cnt)) &0x03 ; + + offset_p +=1 ; + info->alert_enable =get_unaligned_be24 (offset_p); +
drivers/iio/adc/pac1944.c line 3233
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+/* +* Sending a REFRESH to the chip, so the new settings take place +* as well as resetting the accumulators +*/ + ret =i2c_smbus_write_byte (client,PAC1944_REFRESH_REG_ADDR ); +if (ret) { +dev_err (&client->dev,"cannot write 0x%02X reg\n" , +PAC1944_REFRESH_REG_ADDR ); +return ret; + }
return dev_err_probe()..
Quoted 27 lines
drivers/iio/adc/pac1944.c new file +27
-0+ +/* +* Get the current (in the chip) sampling speed and compute the +* required timeout based on its value the timeout is 1/sampling_speed +* wait the maximum amount of time to be on the safe side - the +* maximum wait time is for 8sps +*/ + wait_time = (1024 / samp_rate_map_tbl[info->sampling_mode]) *1000 ; +usleep_range (wait_time, wait_time +100 ); + +INIT_DELAYED_WORK (&info->work_chip_rfsh, pac1944_work_periodic_rfsh); +/* Setup the latest moment for reading the regs before saturation */ +schedule_delayed_work (&info->work_chip_rfsh, +msecs_to_jiffies (PAC1944_MAX_RFSH_LIMIT_MS )); + +return devm_add_action_or_reset (&client->dev, pac1944_cancel_delayed_work, + &info->work_chip_rfsh); +} + +static const struct iio_chan_spec pac1944_single_channel[] = { +PAC1944_VPOWER_CHANNEL (0 ,0 ,PAC1944_VPOWER_1_ADDR , pac1944_single_event, +ARRAY_SIZE (pac1944_single_event)), +PAC1944_VBUS_CHANNEL (0 ,0 ,PAC1944_VBUS_1_ADDR , pac1944_events, +ARRAY_SIZE (pac1944_events)), +PAC1944_VSENSE_CHANNEL (0 ,0 ,PAC1944_VSENSE_1_ADDR , pac1944_events, +ARRAY_SIZE (pac1944_events)), +PAC1944_VBUS_AVG_CHANNEL (0 ,0 ,PAC1944_VBUS_AVG_1_ADDR ),
drivers/iio/adc/pac1944.c line 3270
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +10
-0+ PAC1944_VSENSE_AVG_CHANNEL(0, 0, PAC1944_VSENSE_AVG_1_ADDR), +}; + +static int pac1944_prep_iio_channels (struct pac1944_chip_info *info, +struct iio_dev *indio_dev) +{ +struct device *dev = &info->client->dev; +struct iio_chan_spec *ch_sp; +int channel_size, attribute_count; +int cnt;
combine with line above.
drivers/iio/adc/pac1944.c line 3277
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+void *dyn_ch_struct, *tmp_data; + +/* Finding out dynamically how many IIO channels we need */ + attribute_count =0 ; + channel_size =0 ; +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +if (!info->active_channels[cnt])
If you use a bitmap for this you can easily loop over the bits that are set.
drivers/iio/adc/pac1944.c line 3284
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+continue ; + +/* add the size of the properties of one chip physical channel */ + channel_size +=sizeof (pac1944_single_channel); +/* count how many enabled channels we have */ + attribute_count +=ARRAY_SIZE (pac1944_single_channel); +dev_info (dev,":%s: Channel %d active\n" , __func__, cnt +1 );
Noisy - make this a dev_dbg()
drivers/iio/adc/pac1944.c line 3291
Quoted 7 lines
drivers/iio/adc/pac1944.c new file +7
-0+ } + + dyn_ch_struct =devm_kzalloc (dev, channel_size,GFP_KERNEL ); +if (!dyn_ch_struct) +return -EINVAL ; + + tmp_data = dyn_ch_struct;
tmp_data is a misleading name. It's data we keep, just a pointer into it. So maybe think up a better name or just drag ch_sp setting out here.
drivers/iio/adc/pac1944.c line 3303
Quoted 12 lines
drivers/iio/adc/pac1944.c new file +12
-0+/* Populate the dynamic channels and make all the adjustments */ +for (cnt =0 ; cnt < info->phys_channels; cnt++) { +if (!info->active_channels[cnt]) +continue ; + +memcpy (tmp_data, pac1944_single_channel,sizeof (pac1944_single_channel)); + ch_sp = (struct iio_chan_spec *)tmp_data; + ch_sp[PAC1944_CH_POWER ].channel = cnt +1 ; + ch_sp[PAC1944_CH_POWER ].scan_index = cnt; + ch_sp[PAC1944_CH_POWER ].address = cnt +PAC1944_VPOWER_1_ADDR ; + ch_sp[PAC1944_CH_VOLTAGE ].channel = cnt +1 ; + ch_sp[PAC1944_CH_VOLTAGE ].scan_index = cnt;
3 channels all have the same scan index. That doesn't look right. You aren't doing buffered capture so I guess that doesn't get used. If so just don't set it.
drivers/iio/adc/pac1944.c line 3314
Quoted 11 lines
drivers/iio/adc/pac1944.c new file +11
-0+ ch_sp[PAC1944_CH_VOLTAGE ].address = cnt +PAC1944_VBUS_1_ADDR ; + ch_sp[PAC1944_CH_CURRENT ].channel = cnt +1 ; + ch_sp[PAC1944_CH_CURRENT ].scan_index = cnt; + ch_sp[PAC1944_CH_CURRENT ].address = cnt +PAC1944_VSENSE_1_ADDR ; +/* +* In order to be able to use labels for PAC1944_CH_VOLTAGE and +* PAC1944_CH_VOLTAGE_AVERAGE, respectively PAC1944_CH_CURRENT +* and PAC1944_CH_CURRENT_AVERAGE we need to use different channel numbers. +* We will add +5 (+1 to maximum PAC channels). +*/ + ch_sp[PAC1944_CH_VOLTAGE_AVERAGE ].channel = cnt +5 ;
Probably use a define for the max channel count then +1 to that. I've not figured out where the +1 comes from yet though.
Quoted 18 lines
drivers/iio/adc/pac1944.c new file +18
-0+ ch_sp[PAC1944_CH_VOLTAGE_AVERAGE ].scan_index = cnt; + ch_sp[PAC1944_CH_VOLTAGE_AVERAGE ].address = cnt +PAC1944_VBUS_AVG_1_ADDR ; + ch_sp[PAC1944_CH_CURRENT_AVERAGE ].channel = cnt +5 ; + ch_sp[PAC1944_CH_CURRENT_AVERAGE ].scan_index = cnt; + ch_sp[PAC1944_CH_CURRENT_AVERAGE ].address = cnt +PAC1944_VSENSE_AVG_1_ADDR ; + +/* advance the pointer */ + tmp_data +=sizeof (pac1944_single_channel); + } + +/* +* Send the updated dynamic channel structure information towards IIO +* prepare the required field for IIO class registration +*/ + indio_dev->num_channels = attribute_count; + indio_dev->channels = (const struct iio_chan_spec *)dyn_ch_struct; + +return 0 ;
drivers/iio/adc/pac1944.c line 3402
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +8
-0+} + +static int pac1944_probe (struct i2c_client *client) +{ + +mutex_init (&info->lock); + ret =devm_add_action_or_reset (&client->dev, pac1944_mutex_destroy, + &info->lock);
The is finally a devm_mutex_init() upstream so use that instead. Previously I'd have said it was not worth the bother; now it's easy!
Quoted 30 lines
drivers/iio/adc/pac1944.c new file +30
-0+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 PAC194x/5x 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 (&client->dev, indio_dev); + +if (ret <0 ) +return dev_err_probe (dev, ret, +"Can't register IIO device\n" );
drivers/iio/adc/pac1944.c line 3457
Quoted 10 lines
drivers/iio/adc/pac1944.c new file +9
-0+ +return 0 ; +} + + +static const struct of_device_id pac1944_of_match[] = { + { + .compatible ="microchip,pac1941_1" , + .data = (void *)&pac1944_chip_config[PAC1941_1 ]
Casts shouldn't be needed in this case I think?
drivers/iio/adc/pac1944.c line 3507
Quoted 3 lines
drivers/iio/adc/pac1944.c new file +3
-0+ }, + +/* using MCHP1940 to be compatible with BIOS ACPI */
Not sure we need the comment as the ID is a valid one.
drivers/iio/adc/pac1944.c line 3509
Quoted 2 lines
drivers/iio/adc/pac1944.c new file +2
-0+static const struct acpi_device_id pac1944_acpi_match[] = { + {"MCHP1940" , .driver_data = (kernel_ulong_t)&pac1944_chip_config[PAC1944_1 ] },
*Contented sigh* Nice to check one of these an find out that indeed that is a valid ACPI ID :)
Quoted 19 lines
drivers/iio/adc/pac1944.c new file +19
-0+ {} +}; +MODULE_DEVICE_TABLE (acpi, pac1944_acpi_match); + +static struct i2c_driver pac1944_driver = { + .driver = { + .name ="pac1944" , + .of_match_table = pac1944_of_match, + .acpi_match_table = pac1944_acpi_match + }, + .probe = pac1944_probe, + .id_table = pac1944_id, +}; + +module_i2c_driver (pac1944_driver); + +MODULE_AUTHOR ("Marius Cristea <[email protected]>" ); +MODULE_DESCRIPTION ("Microchip PAC194X and PAC195X Power Monitor" ); +MODULE_LICENSE ("GPL v2" );