Skip to contentSkip to conversation

Add Richtek RTQ6056 support

PATCH Superseded cy_huang 4 participants 15 messages Jun 29, 2022 – Jul 7, 2022 1 message not in the archive

Subsystems (2)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

cy_huang

author
#0 lore raw

v2 cover Add Richtek RTQ6056 support

From: ChiYuan Huang <[email protected]>

This patch series is to enable Richtek RTQ6056 support.

The RTQ6056 is a high accuracy current-sense monitor with I2C interface, and the device provides full information for system by reading out the load current and power.

Since v2
- Change the resistor property name to be generic 'shunt-resistor-micro-ohms'.
- Rename file from 'rtq6056-adc' to 'rtq6056'.
- Refine the ABI, if generic already defined it, remove it and check the channel report unit.
- Add copyright text.
- include the correct header.
- change the property parsing name.
- To use iio_chan_spec address field.
- Refine each channel separate and shared_by_all.
- Use pm_runtime and pm_runtime_autosuspend.
- Remove the shutdown callback. From the HW suggestion, it's not recommended to use battery as the power supply.
- Check all scale unit (voltage->mV, current->mA, power->milliWatt).
- Use the read_avail to provide the interface for attribute value list.
- Add comma for the last element in the const integer array.
- Refine each ADC label text.
- In read_label callback, replace snprintf to sysfs_emit.

ChiYuan Huang (2):
dt-bindings: iio: adc: Add rtq6056 adc support
iio: adc: Add rtq6056 support

 .../ABI/testing/sysfs-bus-iio-adc-rtq6056          |   6 +
 .../bindings/iio/adc/richtek,rtq6056.yaml          |  56 ++
 drivers/iio/adc/Kconfig                            |  15 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/rtq6056.c                          | 670 +++++++++++++++++++++
 5 files changed, 748 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-rtq6056
 create mode 100644 Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml
 create mode 100644 drivers/iio/adc/rtq6056.c
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/2 dt-bindings: iio: adc: Add rtq6056 adc support

From: ChiYuan Huang <[email protected]>

Add the documentation for Richtek rtq6056.

---
Since v2
- Change the resistor property name to be generic 'shunt-resistor-micro-ohms'.

---
 .../bindings/iio/adc/richtek,rtq6056.yaml          | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml
Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml new file +56 -0
new file mode 100644
index 00000000..fe45d8b
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/richtek,rtq6056.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: RTQ6056 Bi-Directional Current and Power Monitor with 16-bit ADC
+
+maintainers:
+  - ChiYuan Huang <[email protected]>
+
+description: |
+  The RTQ6056 is a high accuracy current-sense monitor with I2C and SMBus
+  interface, and the device provides full information for system by reading
+  out the loading current and power.
+
+  The device monitors both of the drops across sense resistor and the BUS
+  voltage, converts into the current in amperes, and power in watts through
+  internal analog-to-digital converter ADC. The programmable calibration,
+  adjustable conversion time, and averaging function are also built in for
+  more design flexibility.
+
+  Datasheet is available at
+  https://www.richtek.com/assets/product_file/RTQ6056/DSQ6056-00.pdf
+
+properties:
+  compatible:
+    const: richtek,rtq6056
+
+  reg:
+    maxItems: 1
+
+  "#io-channel-cells":
+    const: 1
+
+  shunt-resistor-micro-ohms:
+    description: Shunt IN+/IN- sensing node resistor
+
+required:
+  - compatible
+  - reg
+  - "#io-channel-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      rtq6056@40 {
+        compatible = "richtek,rtq6056";
+        reg = <0x40>;
+        #io-channel-cells = <1>;
+      };
+    };
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v2 1/2] dt-bindings: iio: adc: Add rtq6056 adc support

Quoted 61 lines · cy_huang

From: ChiYuan Huang <[email protected]>

Add the documentation for Richtek rtq6056.

---
Since v2
- Change the resistor property name to be generic 'shunt-resistor-micro-ohms'.

---
 .../bindings/iio/adc/richtek,rtq6056.yaml          | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml
Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml new file +41 -0
new file mode 100644
index 00000000..fe45d8b
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/richtek,rtq6056.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: RTQ6056 Bi-Directional Current and Power Monitor with 16-bit ADC
+
+maintainers:
+  - ChiYuan Huang <[email protected]>
+
+description: |
+  The RTQ6056 is a high accuracy current-sense monitor with I2C and SMBus
+  interface, and the device provides full information for system by reading
+  out the loading current and power.
+
+  The device monitors both of the drops across sense resistor and the BUS
+  voltage, converts into the current in amperes, and power in watts through
+  internal analog-to-digital converter ADC. The programmable calibration,
+  adjustable conversion time, and averaging function are also built in for
+  more design flexibility.
+
+  Datasheet is available at
+  https://www.richtek.com/assets/product_file/RTQ6056/DSQ6056-00.pdf
+
+properties:
+  compatible:
+    const: richtek,rtq6056
+
+  reg:
+    maxItems: 1
+
+  "#io-channel-cells":
+    const: 1
+
+  shunt-resistor-micro-ohms:
+    description: Shunt IN+/IN- sensing node resistor
+
+required:
+  - compatible
+  - reg

Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml line 51

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml new file +10 -0
+  - "#io-channel-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      rtq6056@40 {

This was not fixed.

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v2 1/2] dt-bindings: iio: adc: Add rtq6056 adc support

Krzysztof Kozlowski <[email protected]> 於 2022年6月29日 週三 下午4:25寫道:

Quoted 67 lines · cy_huang
Quoted 65 lines

From: ChiYuan Huang <[email protected]>

Add the documentation for Richtek rtq6056.

---
Since v2
- Change the resistor property name to be generic 'shunt-resistor-micro-ohms'.

---
 .../bindings/iio/adc/richtek,rtq6056.yaml          | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml
Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml new file +45 -0
new file mode 100644
index 00000000..fe45d8b
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/richtek,rtq6056.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: RTQ6056 Bi-Directional Current and Power Monitor with 16-bit ADC
+
+maintainers:
+  - ChiYuan Huang <[email protected]>
+
+description: |
+  The RTQ6056 is a high accuracy current-sense monitor with I2C and SMBus
+  interface, and the device provides full information for system by reading
+  out the loading current and power.
+
+  The device monitors both of the drops across sense resistor and the BUS
+  voltage, converts into the current in amperes, and power in watts through
+  internal analog-to-digital converter ADC. The programmable calibration,
+  adjustable conversion time, and averaging function are also built in for
+  more design flexibility.
+
+  Datasheet is available at
+  https://www.richtek.com/assets/product_file/RTQ6056/DSQ6056-00.pdf
+
+properties:
+  compatible:
+    const: richtek,rtq6056
+
+  reg:
+    maxItems: 1
+
+  "#io-channel-cells":
+    const: 1
+
+  shunt-resistor-micro-ohms:
+    description: Shunt IN+/IN- sensing node resistor
+
+required:
+  - compatible
+  - reg
+  - "#io-channel-cells"
+
+additionalProperties: false
+
Quoted 9 lines

Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml line 51

Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/richtek,rtq6056.yaml new file +6 -0
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      rtq6056@40 {

This was not fixed.

Sorry, too many changes to check. I really forgot it.

Quoted 3 lines

Best regards,
Krzysztof

author
#4 lore raw

PATCH v2 2/2 iio: adc: Add rtq6056 support

In reply to cy_huang

From: ChiYuan Huang <[email protected]>

Add Richtek rtq6056 supporting.

It can be used for the system to monitor load current and power with 16-bit resolution.

---
Since v2
- Rename file from 'rtq6056-adc' to 'rtq6056'.
- Refine the ABI, if generic already defined it, remove it and check the channel report unit.
- Add copyright text.
- include the correct header.
- change the property parsing name.
- To use iio_chan_spec address field.
- Refine each channel separate and shared_by_all.
- Use pm_runtime and pm_runtime_autosuspend.
- Remove the shutdown callback. From the HW suggestion, it's not recommended to use battery as the power supply.
- Check all scale unit (voltage->mV, current->mA, power->milliWatt).
- Use the read_avail to provide the interface for attribute value list.
- Add comma for the last element in the const integer array.
- Refine each ADC label text.
- In read_label callback, replace snprintf to sysfs_emit.

---
 .../ABI/testing/sysfs-bus-iio-adc-rtq6056          |   6 +
 drivers/iio/adc/Kconfig                            |  15 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/rtq6056.c                          | 670 +++++++++++++++++++++
 4 files changed, 692 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-rtq6056
 create mode 100644 drivers/iio/adc/rtq6056.c
Documentation/ABI/testing/sysfs-bus-iio-adc-rtq6056 new file +6 -0
new file mode 100644
index 00000000..db54343
@@ -0,0 +1,6 @@
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage0_integration_time
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage1_integration_time
+KernelVersion:	5.15.31
+Contact:	[email protected]
+Description:
+		Each voltage conversion time in uS
drivers/iio/adc/Kconfig +15 -0
index 48ace74..caebd1a 100644
@@ -908,6 +908,21 @@ config ROCKCHIP_SARADC
 	  To compile this driver as a module, choose M here: the
 	  module will be called rockchip_saradc.
 
+config RICHTEK_RTQ6056
+	tristate "Richtek RTQ6056 Current and Power Monitor ADC"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to enable RQT6056 ADC support.
+	  RTQ6056 is a high accuracy current-sense monitor with I2C and SMBus
+	  compatible interface, and the device provides full information for
+	  system by reading out the load current and power.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called rtq6056.
+
 config RZG2L_ADC
 	tristate "Renesas RZ/G2L ADC driver"
 	depends on ARCH_RZG2L || COMPILE_TEST
drivers/iio/adc/Makefile +1 -0
index 39d806f..cda7580 100644
@@ -84,6 +84,7 @@ obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
 obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o
 obj-$(CONFIG_RN5T618_ADC) += rn5t618-adc.o
 obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
+obj-$(CONFIG_RICHTEK_RTQ6056) += rtq6056.o
 obj-$(CONFIG_RZG2L_ADC) += rzg2l_adc.o
 obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o
 obj-$(CONFIG_SPEAR_ADC) += spear_adc.o
drivers/iio/adc/rtq6056.c new file +670 -0

670 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Quoted 4 lines · cy_huang

Add Richtek rtq6056 supporting.

It can be used for the system to monitor load current and power with 16-bit resolution.

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_channel(struct rtq6056_priv *priv,
+                                   struct iio_chan_spec const *ch,
+                                   int *val)
+{
+       struct device *dev = priv->dev;
+       unsigned int addr = ch->address;

drivers/iio/adc/rtq6056.c line 171

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+       unsigned int regval;
+       int ret;
+
+       pm_runtime_get_sync(dev);
+
+       ret = regmap_read(priv->regmap, addr, &regval);
+       if (ret) {
+               pm_runtime_put(dev);
+               return ret;
+       }

You can optimize this to

       pm_runtime_get_sync(dev);
       ret = regmap_read(priv->regmap, addr, &regval);
       pm_runtime_mark_last_busy(dev);
       pm_runtime_put(dev);
       if (ret)
           return ret;

drivers/iio/adc/rtq6056.c line 180

Quoted 8 lines
drivers/iio/adc/rtq6056.c new file +7 -0
+       /* Power and VBUS is unsigned 16-bit, others are signed 16-bit */
+       if (addr == RTQ6056_REG_BUSVOLT || addr == RTQ6056_REG_POWER)
+               *val = regval;
+       else
+               *val = sign_extend32(regval, 16);
 
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_put(dev);

...and get rid of these.

Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}

...

Quoted 1 line

+ *val2 = 1000000000;

NANO ?

...

Quoted 1 line

+ *val2 = 1000;

MILLI ?

Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+
+       return IIO_VAL_INT;
+}

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_label(struct iio_dev *indio_dev,
+                                 struct iio_chan_spec const *chan,
+                                 char *label)
+{
+       return sysfs_emit(label, "%s\n", rtq6056_channel_labels[chan->channel]);
+}

...

Quoted 1 line

+ /* calibration = 5120000 / (Rshunt (uohm) * current lsb (1mA)) */

uOhm

...

drivers/iio/adc/rtq6056.c line 397

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static ssize_t shunt_resistor_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
+{
+       struct rtq6056_priv *priv = iio_priv(dev_to_iio_dev(dev));
+       int vals[2] = { priv->shunt_resistor_uohm, 1000000 };

MICRO ?

Quoted 2 lines
(quoted diff) +2 -0
+       return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
+}

...

Quoted 1 line

+ ret = rtq6056_set_shunt_resistor(priv, val * 1000000 + val_fract);

MICRO ?

Quoted 2 lines
(quoted diff) +2 -0
+       if (ret)
+               return ret;

...

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

...

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+       pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
+       pm_runtime_use_autosuspend(dev);
+       pm_runtime_set_active(dev);
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_enable(dev);

drivers/iio/adc/rtq6056.c line 578

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+
+       /* By default, use 2000 micro-ohm resistor */
+       shunt_resistor_uohm = 2000;
+       device_property_read_u32(dev, "shunt-resistor-micro-ohms",
+                                &shunt_resistor_uohm);
+
+       ret = rtq6056_set_shunt_resistor(priv, shunt_resistor_uohm);
+       if (ret) {
+               dev_err(dev, "Failed to init shunt resistor\n");
+               goto err_probe;

return dev_err_probe();

(see below how)

drivers/iio/adc/rtq6056.c line 591

Quoted 13 lines
drivers/iio/adc/rtq6056.c new file +13 -0
+       }
+
+       indio_dev->name = "rtq6056";
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->channels = rtq6056_channels;
+       indio_dev->num_channels = ARRAY_SIZE(rtq6056_channels);
+       indio_dev->info = &rtq6056_info;
+
+       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+                                             rtq6056_buffer_trigger_handler,
+                                             NULL);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio trigger buffer\n");

Ditto.

Quoted 1 line

+ goto err_probe;

It is a sign of wrong ordering, either do not use devm_ calls after non-devm_ or make the latter wrapped into devm_add_action_or_reset(). See below for additional information.

Quoted 16 lines
drivers/iio/adc/rtq6056.c new file +16 -0
+       }
+
+       ret = devm_iio_device_register(dev, indio_dev);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio device\n");
+               goto err_probe;
+       }
+
+       return 0;
+
+err_probe:
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return ret;

...

drivers/iio/adc/rtq6056.c line 613

Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+static int rtq6056_remove(struct i2c_client *i2c)
+{
+       struct device *dev = &i2c->dev;

Another (but usually not good option) is to call devm_..._unregister() here.

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return 0;
+}

...

drivers/iio/adc/rtq6056.c line 648

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct dev_pm_ops rtq6056_pm_ops = {
+       SET_RUNTIME_PM_OPS(rtq6056_runtime_suspend, rtq6056_runtime_resume, NULL)

RUNTIME_PM_OPS()

Quoted 1 line

+};

...

drivers/iio/adc/rtq6056.c line 652

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct of_device_id rtq6056_device_match[] = {
+       { .compatible = "richtek,rtq6056", },

In this case the inner comma is not needed.

Quoted 2 lines
(quoted diff) +2 -0
+       {}
+};

...

drivers/iio/adc/rtq6056.c line 661

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static struct i2c_driver rtq6056_driver = {
+       .driver = {
+               .name = "rtq6056",
+               .of_match_table = rtq6056_device_match,
 
+               .pm = &rtq6056_pm_ops,

pm_ptr()

Quoted 1 line

+ },

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 47 lines · cy_huang
Quoted 5 lines

Add Richtek rtq6056 supporting.

It can be used for the system to monitor load current and power with 16-bit resolution.

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_channel(struct rtq6056_priv *priv,
+                                   struct iio_chan_spec const *ch,
+                                   int *val)
+{
+       struct device *dev = priv->dev;
+       unsigned int addr = ch->address;

drivers/iio/adc/rtq6056.c line 171

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+       unsigned int regval;
+       int ret;
+
+       pm_runtime_get_sync(dev);
+
+       ret = regmap_read(priv->regmap, addr, &regval);
+       if (ret) {
+               pm_runtime_put(dev);
+               return ret;
+       }

You can optimize this to

       pm_runtime_get_sync(dev);
       ret = regmap_read(priv->regmap, addr, &regval);
       pm_runtime_mark_last_busy(dev);
       pm_runtime_put(dev);
       if (ret)
           return ret;

drivers/iio/adc/rtq6056.c line 180

Quoted 8 lines
drivers/iio/adc/rtq6056.c new file +7 -0
+       /* Power and VBUS is unsigned 16-bit, others are signed 16-bit */
+       if (addr == RTQ6056_REG_BUSVOLT || addr == RTQ6056_REG_POWER)
+               *val = regval;
+       else
+               *val = sign_extend32(regval, 16);
 
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_put(dev);

...and get rid of these.

Quoted 9 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}

...

Quoted 1 line

+ *val2 = 1000000000;

NANO ?

Yes, unit is 2.5 microvolt. I have all listed all unit comments in the source code.

Quoted 6 lines

...

Quoted 1 line

+ *val2 = 1000;

MILLI ?

Yes.

Quoted 4 lines
Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

No, sample time is (vshunt convesion time + vbus conversion time) * average sample.
And the sample freq returns the unit by HZ (sample frequency per second)

Quoted 21 lines
Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+
+       return IIO_VAL_INT;
+}

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_label(struct iio_dev *indio_dev,
+                                 struct iio_chan_spec const *chan,
+                                 char *label)
+{
+       return sysfs_emit(label, "%s\n", rtq6056_channel_labels[chan->channel]);
+}

...

Quoted 1 line

+ /* calibration = 5120000 / (Rshunt (uohm) * current lsb (1mA)) */

uOhm

...

Quoted 8 lines

drivers/iio/adc/rtq6056.c line 397

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static ssize_t shunt_resistor_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
+{
+       struct rtq6056_priv *priv = iio_priv(dev_to_iio_dev(dev));
+       int vals[2] = { priv->shunt_resistor_uohm, 1000000 };

MICRO ?

Yes, for this kind of sense resistor, it will choose 2 milli-Ohm, 1 milli-Ohms,, 0.5 milli-Ohms, or less.

Quoted 9 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
+}

...

Quoted 1 line

+ ret = rtq6056_set_shunt_resistor(priv, val * 1000000 + val_fract);

MICRO ?

Yes

Quoted 10 lines
Quoted 2 lines
(quoted diff) +2 -0
+       if (ret)
+               return ret;

...

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);
Quoted 5 lines

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

Quoted 38 lines

...

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+       pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
+       pm_runtime_use_autosuspend(dev);
+       pm_runtime_set_active(dev);
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_enable(dev);

drivers/iio/adc/rtq6056.c line 578

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+
+       /* By default, use 2000 micro-ohm resistor */
+       shunt_resistor_uohm = 2000;
+       device_property_read_u32(dev, "shunt-resistor-micro-ohms",
+                                &shunt_resistor_uohm);
+
+       ret = rtq6056_set_shunt_resistor(priv, shunt_resistor_uohm);
+       if (ret) {
+               dev_err(dev, "Failed to init shunt resistor\n");
+               goto err_probe;

return dev_err_probe();

(see below how)

drivers/iio/adc/rtq6056.c line 591

Quoted 13 lines
drivers/iio/adc/rtq6056.c new file +13 -0
+       }
+
+       indio_dev->name = "rtq6056";
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->channels = rtq6056_channels;
+       indio_dev->num_channels = ARRAY_SIZE(rtq6056_channels);
+       indio_dev->info = &rtq6056_info;
+
+       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+                                             rtq6056_buffer_trigger_handler,
+                                             NULL);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio trigger buffer\n");

Ditto.

Quoted 6 lines
Quoted 1 line

+ goto err_probe;

It is a sign of wrong ordering, either do not use devm_ calls after non-devm_ or make the latter wrapped into devm_add_action_or_reset(). See below for additional information.

I think the another way is to register all using devm_ and to call the pm_runtime at the last.

Quoted 67 lines
Quoted 16 lines
drivers/iio/adc/rtq6056.c new file +16 -0
+       }
+
+       ret = devm_iio_device_register(dev, indio_dev);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio device\n");
+               goto err_probe;
+       }
+
+       return 0;
+
+err_probe:
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return ret;

...

drivers/iio/adc/rtq6056.c line 613

Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+static int rtq6056_remove(struct i2c_client *i2c)
+{
+       struct device *dev = &i2c->dev;

Another (but usually not good option) is to call devm_..._unregister() here.

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return 0;
+}

...

drivers/iio/adc/rtq6056.c line 648

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct dev_pm_ops rtq6056_pm_ops = {
+       SET_RUNTIME_PM_OPS(rtq6056_runtime_suspend, rtq6056_runtime_resume, NULL)

RUNTIME_PM_OPS()

Quoted 1 line

+};

...

drivers/iio/adc/rtq6056.c line 652

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct of_device_id rtq6056_device_match[] = {
+       { .compatible = "richtek,rtq6056", },

In this case the inner comma is not needed.

Quoted 2 lines
(quoted diff) +2 -0
+       {}
+};

...

drivers/iio/adc/rtq6056.c line 661

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static struct i2c_driver rtq6056_driver = {
+       .driver = {
+               .name = "rtq6056",
+               .of_match_table = rtq6056_device_match,
 
+               .pm = &rtq6056_pm_ops,

pm_ptr()

Quoted 1 line

+ },

--
With Best Regards,
Andy Shevchenko

#7 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 52 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 50 lines · cy_huang
Quoted 5 lines

Add Richtek rtq6056 supporting.

It can be used for the system to monitor load current and power with 16-bit resolution.

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_channel(struct rtq6056_priv *priv,
+                                   struct iio_chan_spec const *ch,
+                                   int *val)
+{
+       struct device *dev = priv->dev;
+       unsigned int addr = ch->address;

drivers/iio/adc/rtq6056.c line 171

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+       unsigned int regval;
+       int ret;
+
+       pm_runtime_get_sync(dev);
+
+       ret = regmap_read(priv->regmap, addr, &regval);
+       if (ret) {
+               pm_runtime_put(dev);
+               return ret;
+       }

You can optimize this to

       pm_runtime_get_sync(dev);
       ret = regmap_read(priv->regmap, addr, &regval);
       pm_runtime_mark_last_busy(dev);
       pm_runtime_put(dev);
       if (ret)
           return ret;

drivers/iio/adc/rtq6056.c line 180

Quoted 8 lines
drivers/iio/adc/rtq6056.c new file +7 -0
+       /* Power and VBUS is unsigned 16-bit, others are signed 16-bit */
+       if (addr == RTQ6056_REG_BUSVOLT || addr == RTQ6056_REG_POWER)
+               *val = regval;
+       else
+               *val = sign_extend32(regval, 16);
 
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_put(dev);

...and get rid of these.

Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}
Quoted 8 lines
Quoted 6 lines

...

Quoted 1 line

+ *val2 = 1000000000;

NANO ?

Yes, unit is 2.5 microvolt. I have all listed all unit comments in the source code.a

Sorry, I found this scale is wrong.
For voltage channel, standard binding uses millivolt as the reported value. From this case, It must be (val * 2500) /1000000
So the '*val2' must equal to 1000000

Quoted 7 lines
Quoted 6 lines

...

Quoted 1 line

+ *val2 = 1000;

MILLI ?

Yes.

As the above one. Must be millivolt as the unit. Channel unit is 2.5mV.
This value is correct and equal to (val * 2500) / 1000

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

No, sample time is (vshunt convesion time + vbus conversion time) * average sample.
And the sample freq returns the unit by HZ (sample frequency per second)

The 'sample time' is unit by micro-second like as you mentioned.

Quoted 172 lines
Quoted 21 lines
Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+
+       return IIO_VAL_INT;
+}

...

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+static int rtq6056_adc_read_label(struct iio_dev *indio_dev,
+                                 struct iio_chan_spec const *chan,
+                                 char *label)
+{
+       return sysfs_emit(label, "%s\n", rtq6056_channel_labels[chan->channel]);
+}

...

Quoted 1 line

+ /* calibration = 5120000 / (Rshunt (uohm) * current lsb (1mA)) */

uOhm

...

Quoted 8 lines

drivers/iio/adc/rtq6056.c line 397

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static ssize_t shunt_resistor_show(struct device *dev,
+                                  struct device_attribute *attr, char *buf)
+{
+       struct rtq6056_priv *priv = iio_priv(dev_to_iio_dev(dev));
+       int vals[2] = { priv->shunt_resistor_uohm, 1000000 };

MICRO ?

Yes, for this kind of sense resistor, it will choose 2 milli-Ohm, 1 milli-Ohms,, 0.5 milli-Ohms, or less.

Quoted 9 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
+}

...

Quoted 1 line

+ ret = rtq6056_set_shunt_resistor(priv, val * 1000000 + val_fract);

MICRO ?

Yes

Quoted 10 lines
Quoted 2 lines
(quoted diff) +2 -0
+       if (ret)
+               return ret;

...

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);
Quoted 5 lines

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

Quoted 38 lines

...

Quoted 5 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+       pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
+       pm_runtime_use_autosuspend(dev);
+       pm_runtime_set_active(dev);
+       pm_runtime_mark_last_busy(dev);
+       pm_runtime_enable(dev);

drivers/iio/adc/rtq6056.c line 578

Quoted 10 lines
drivers/iio/adc/rtq6056.c new file +10 -0
+
+       /* By default, use 2000 micro-ohm resistor */
+       shunt_resistor_uohm = 2000;
+       device_property_read_u32(dev, "shunt-resistor-micro-ohms",
+                                &shunt_resistor_uohm);
+
+       ret = rtq6056_set_shunt_resistor(priv, shunt_resistor_uohm);
+       if (ret) {
+               dev_err(dev, "Failed to init shunt resistor\n");
+               goto err_probe;

return dev_err_probe();

(see below how)

drivers/iio/adc/rtq6056.c line 591

Quoted 13 lines
drivers/iio/adc/rtq6056.c new file +13 -0
+       }
+
+       indio_dev->name = "rtq6056";
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->channels = rtq6056_channels;
+       indio_dev->num_channels = ARRAY_SIZE(rtq6056_channels);
+       indio_dev->info = &rtq6056_info;
+
+       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+                                             rtq6056_buffer_trigger_handler,
+                                             NULL);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio trigger buffer\n");

Ditto.

Quoted 6 lines
Quoted 1 line

+ goto err_probe;

It is a sign of wrong ordering, either do not use devm_ calls after non-devm_ or make the latter wrapped into devm_add_action_or_reset(). See below for additional information.

I think the another way is to register all using devm_ and to call the pm_runtime at the last.

Quoted 67 lines
Quoted 16 lines
drivers/iio/adc/rtq6056.c new file +16 -0
+       }
+
+       ret = devm_iio_device_register(dev, indio_dev);
+       if (ret) {
+               dev_err(dev, "Failed to allocate iio device\n");
+               goto err_probe;
+       }
+
+       return 0;
+
+err_probe:
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return ret;

...

drivers/iio/adc/rtq6056.c line 613

Quoted 3 lines
drivers/iio/adc/rtq6056.c new file +3 -0
+static int rtq6056_remove(struct i2c_client *i2c)
+{
+       struct device *dev = &i2c->dev;

Another (but usually not good option) is to call devm_..._unregister() here.

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +6 -0
+       pm_runtime_dont_use_autosuspend(dev);
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+
+       return 0;
+}

...

drivers/iio/adc/rtq6056.c line 648

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct dev_pm_ops rtq6056_pm_ops = {
+       SET_RUNTIME_PM_OPS(rtq6056_runtime_suspend, rtq6056_runtime_resume, NULL)

RUNTIME_PM_OPS()

Quoted 1 line

+};

...

drivers/iio/adc/rtq6056.c line 652

Quoted 2 lines
drivers/iio/adc/rtq6056.c new file +2 -0
+static const struct of_device_id rtq6056_device_match[] = {
+       { .compatible = "richtek,rtq6056", },

In this case the inner comma is not needed.

Quoted 2 lines
(quoted diff) +2 -0
+       {}
+};

...

drivers/iio/adc/rtq6056.c line 661

Quoted 6 lines
drivers/iio/adc/rtq6056.c new file +5 -0
+static struct i2c_driver rtq6056_driver = {
+       .driver = {
+               .name = "rtq6056",
+               .of_match_table = rtq6056_device_match,
 
+               .pm = &rtq6056_pm_ops,

pm_ptr()

Quoted 1 line

+ },

--
With Best Regards,
Andy Shevchenko

#8 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Quoted 3 lines · ChiYuan Huang

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 2 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 1 line · cy_huang

...

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

No, sample time is (vshunt convesion time + vbus conversion time) * average sample.
And the sample freq returns the unit by HZ (sample frequency per second)

The 'sample time' is unit by micro-second like as you mentioned.

Ah, then it should be MICRO, so we will get Hz.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

I think for the safest side it should be given to the timestamp member. No?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Andy Shevchenko <[email protected]> 於 2022年7月5日 週二 清晨5:52寫道:

Quoted 35 lines · ChiYuan Huang
Quoted 3 lines

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 2 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 1 line · cy_huang

...

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

No, sample time is (vshunt convesion time + vbus conversion time) * average sample.
And the sample freq returns the unit by HZ (sample frequency per second)

The 'sample time' is unit by micro-second like as you mentioned.

Ah, then it should be MICRO, so we will get Hz.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}

...

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

I think for the safest side it should be given to the timestamp member. No?

Sorry, following your comment, Why to use 'align' for the timestamp member? the data member already guarantee 2 * 4 = 8 byte, then timestamp will be 8 byte aligned, right?

what you mentioned is to put __aligned(8) only for timestamp.

I try to put aligned in two ways ( one is only for timestamp, another is the whole struct). the result is the same.
From my thinking, in this case, the struct is already 8 byte aligned for timestamp member. don't you think to put 'aligned' is redundant?

Quoted 3 lines

--
With Best Regards,
Andy Shevchenko

#10 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Quoted 5 lines · ChiYuan Huang

Andy Shevchenko <[email protected]> 於 2022年7月5日 週二 清晨5:52寫道:

Quoted 4 lines · ChiYuan Huang
Quoted 3 lines

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 2 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 1 line · cy_huang

...

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

Quoted 5 lines
Quoted 2 lines

I think for the safest side it should be given to the timestamp member. No?

Sorry, following your comment, Why to use 'align' for the timestamp member? the data member already guarantee 2 * 4 = 8 byte, then timestamp will be 8 byte aligned, right?

Today it's true, tomorrow it might be different. Imagine if this driver will cover a new (version of) hardware and needs an additional channel, how do you guarantee alignment in that case? So, current approach is working, but fragile.

Quoted 1 line

what you mentioned is to put __aligned(8) only for timestamp.

Yes.

Quoted 4 lines

I try to put aligned in two ways ( one is only for timestamp, another is the whole struct). the result is the same.
From my thinking, in this case, the struct is already 8 byte aligned for timestamp member. don't you think to put 'aligned' is redundant?

No.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Andy Shevchenko <[email protected]> 於 2022年7月5日 週二 下午5:05寫道:

Quoted 37 lines · ChiYuan Huang
Quoted 5 lines

Andy Shevchenko <[email protected]> 於 2022年7月5日 週二 清晨5:52寫道:

Quoted 4 lines · ChiYuan Huang
Quoted 3 lines

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 2 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 1 line · cy_huang

...

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

Quoted 5 lines
Quoted 2 lines

I think for the safest side it should be given to the timestamp member. No?

Sorry, following your comment, Why to use 'align' for the timestamp member? the data member already guarantee 2 * 4 = 8 byte, then timestamp will be 8 byte aligned, right?

Today it's true, tomorrow it might be different. Imagine if this driver will cover a new (version of) hardware and needs an additional channel, how do you guarantee alignment in that case? So, current approach is working, but fragile.

Quoted 1 line

what you mentioned is to put __aligned(8) only for timestamp.

Quoted 9 lines

Yes.

Quoted 4 lines

I try to put aligned in two ways ( one is only for timestamp, another is the whole struct). the result is the same.
From my thinking, in this case, the struct is already 8 byte aligned for timestamp member. don't you think to put 'aligned' is redundant?

No.

Thanks, I think I can get your point. if it need to be compatible with others, this part will be a trap.
Then it's better to add the align for timestamp member.

I'll submit the v4 for this.

And very sorry about another mail.
I just confused about the text.

Quoted 3 lines

--
With Best Regards,
Andy Shevchenko

A message that is not in the archive (sent off-list or not yet received): CADiBU3_qL1xf+VirMxD5q3wOH_UGSpXZi6pCjX2H44zbeXBqdg@mail.gmail.com

#13 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Quoted 1 line · ChiYuan Huang

ChiYuan Huang <[email protected]> 於 2022年7月5日 週二 下午5:30寫道:

...

Quoted 1 line

Sorry to bother you by the personal mail.

No problem asking this in ML, it's normal development process. Moreover it's good that others may browse mail archives and find the answers. Private mails won't help in that sense. Hence Cc'ing mailing list again.

Quoted 8 lines

I try to put the 'align' in timestamp member.
checkpatch.pl show me the below warning.

WARNING: Missing a blank line after declarations
#448: FILE: drivers/iio/adc/rtq6056.c:448:
+ u16 vals[RTQ6056_MAX_CHANNEL];
+ int64_t timestamp __aligned(8);

Quoted 5 lines

WARNING: externs should be avoided in .c files
#448: FILE: drivers/iio/adc/rtq6056.c:448:
+ int64_t timestamp __aligned(8);

total: 0 errors, 2 warnings, 651 lines checked

You may run `git grep -n 'align(8)'

Quoted 1 line

Does it mean that I only can put the align for the whole struct?

It means that checkpatch is full of false positives.

Btw, use s64 as a type for timestamp as it's more common.

1. Jonathan, shan't we unify this across the IIO drivers? It seems they are using (besides s64): int64_t, u64.

2. Also:

drivers/iio/light/vcnl4000.c:911: u16 buffer[8] __aligned(8)

drivers/iio/light/vcnl4035.c:106: u8 buffer[ALIGN(sizeof(u16),
sizeof(s64)) + sizeof(s64)] __aligned(8);

drivers/iio/light/si1145.c:190: u8 buffer[24] __aligned(8);

// misplaced aligned()
drivers/iio/adc/mt6360-adc.c-265- u16 values[MT6360_CHAN_MAX];
drivers/iio/adc/mt6360-adc.c-266- int64_t timestamp;
drivers/iio/adc/mt6360-adc.c:267: } data __aligned(8);

drivers/iio/adc/stm32-adc.c:250: u16
buffer[STM32_ADC_MAX_SQ + 4] __aligned(8);

drivers/iio/adc/mxs-lradc-adc.c:119: u32
buffer[10] __aligned(8);

drivers/iio/adc/ti-adc0832.c:37: u8 data[24] __aligned(8);

drivers/iio/adc/ti-ads124s08.c:108: u32
buffer[ADS124S08_MAX_CHANNELS + sizeof(s64)/sizeof(u32)] __aligned(8);

drivers/iio/amplifiers/ada4250.c:304: u8 data[2] __aligned(8) = {};

drivers/iio/chemical/atlas-sensor.c:95: __be32 buffer[6] __aligned(8);

drivers/iio/health/afe4403.c:79: s32 buffer[8] __aligned(8);

drivers/iio/health/afe4404.c:95: s32 buffer[10] __aligned(8);

drivers/iio/imu/adis16475.c:107: __be16
data[ADIS16475_MAX_SCAN_DATA] __aligned(8);

drivers/iio/imu/adis16480.c:174: __be16
data[ADIS16495_BURST_MAX_DATA] __aligned(8);

drivers/iio/imu/bmi160/bmi160.h:19: __le16 buf[12] __aligned(8);

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c:568: u8
iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE] __aligned(8);

// ??? haven't checked context
drivers/iio/light/acpi-als.c:65: s32
evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8);

drivers/iio/light/si1145.c:190: u8 buffer[24] __aligned(8);

drivers/iio/magnetometer/rm3100-core.c:82: u8
buffer[RM3100_SCAN_BYTES] __aligned(8);

drivers/iio/potentiostat/lmp91000.c:75: u32 buffer[4] __aligned(8);

drivers/iio/pressure/mpl3115.c:160: u8 buffer[16] __aligned(8);

drivers/iio/proximity/isl29501.c:941: u32 buffer[4] __aligned(8) =
{}; /* 1x16-bit + naturally aligned ts */

need conversion.

3. Why do we need explicit garbage in the code? :-)

drivers/iio/light/rpr0521.c-204-                __le16 channels[3];
drivers/iio/light/rpr0521.c-205-                u8 garbage;
drivers/iio/light/rpr0521.c:206:                s64 ts __aligned(8);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#14 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

Quoted 24 lines · On Tue, 5 Jul 2022 13:01:43 +0200 Andy Shevchenko <[email protected]>…
Quoted 1 line · ChiYuan Huang

ChiYuan Huang <[email protected]> 於 2022年7月5日 週二 下午5:30寫道:

...

Quoted 1 line

Sorry to bother you by the personal mail.

No problem asking this in ML, it's normal development process. Moreover it's good that others may browse mail archives and find the answers. Private mails won't help in that sense. Hence Cc'ing mailing list again.

Quoted 12 lines

I try to put the 'align' in timestamp member.
checkpatch.pl show me the below warning.

WARNING: Missing a blank line after declarations
#448: FILE: drivers/iio/adc/rtq6056.c:448:
+ u16 vals[RTQ6056_MAX_CHANNEL];
+ int64_t timestamp __aligned(8);

WARNING: externs should be avoided in .c files
#448: FILE: drivers/iio/adc/rtq6056.c:448:
+ int64_t timestamp __aligned(8);

Quoted 7 lines
Quoted 1 line

total: 0 errors, 2 warnings, 651 lines checked

You may run `git grep -n 'align(8)'

Quoted 1 line

Does it mean that I only can put the align for the whole struct?

It means that checkpatch is full of false positives.

I fixed some of those in the past by fixing checkpatch, but I guess not all of them...

Quoted 5 lines

Btw, use s64 as a type for timestamp as it's more common.

1. Jonathan, shan't we unify this across the IIO drivers? It seems they are using (besides s64): int64_t, u64.

Should all be s64. Tidying that up would be good (be careful, there are a few hardware timestamps where the format is defined by the hardware though!)

Quoted 4 lines

2. Also:

drivers/iio/light/vcnl4000.c:911: u16 buffer[8] __aligned(8)

For these, what are you proposing changing? Moving the __aligned earlier?

The oddity of all this lot is that the timestamp isn't at a fixed location. It depends on what channels are enabled. Hence we can't enforce things via a more readable structure.

Quoted 10 lines

drivers/iio/light/vcnl4035.c:106: u8 buffer[ALIGN(sizeof(u16),
sizeof(s64)) + sizeof(s64)] __aligned(8);

drivers/iio/light/si1145.c:190: u8 buffer[24] __aligned(8);

// misplaced aligned()
drivers/iio/adc/mt6360-adc.c-265- u16 values[MT6360_CHAN_MAX];
drivers/iio/adc/mt6360-adc.c-266- int64_t timestamp;
drivers/iio/adc/mt6360-adc.c:267: } data __aligned(8);

This one is indeed a bug.

Quoted 44 lines

drivers/iio/adc/stm32-adc.c:250: u16
buffer[STM32_ADC_MAX_SQ + 4] __aligned(8);

drivers/iio/adc/mxs-lradc-adc.c:119: u32
buffer[10] __aligned(8);

drivers/iio/adc/ti-adc0832.c:37: u8 data[24] __aligned(8);

drivers/iio/adc/ti-ads124s08.c:108: u32
buffer[ADS124S08_MAX_CHANNELS + sizeof(s64)/sizeof(u32)] __aligned(8);

drivers/iio/amplifiers/ada4250.c:304: u8 data[2] __aligned(8) = {};

drivers/iio/chemical/atlas-sensor.c:95: __be32 buffer[6] __aligned(8);

drivers/iio/health/afe4403.c:79: s32 buffer[8] __aligned(8);

drivers/iio/health/afe4404.c:95: s32 buffer[10] __aligned(8);

drivers/iio/imu/adis16475.c:107: __be16
data[ADIS16475_MAX_SCAN_DATA] __aligned(8);

drivers/iio/imu/adis16480.c:174: __be16
data[ADIS16495_BURST_MAX_DATA] __aligned(8);

drivers/iio/imu/bmi160/bmi160.h:19: __le16 buf[12] __aligned(8);

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c:568: u8
iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE] __aligned(8);

// ??? haven't checked context
drivers/iio/light/acpi-als.c:65: s32
evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8);

drivers/iio/light/si1145.c:190: u8 buffer[24] __aligned(8);

drivers/iio/magnetometer/rm3100-core.c:82: u8
buffer[RM3100_SCAN_BYTES] __aligned(8);

drivers/iio/potentiostat/lmp91000.c:75: u32 buffer[4] __aligned(8);

drivers/iio/pressure/mpl3115.c:160: u8 buffer[16] __aligned(8);

Quoted 7 lines

drivers/iio/proximity/isl29501.c:941: u32 buffer[4] __aligned(8) =
{}; /* 1x16-bit + naturally aligned ts */

need conversion.

3. Why do we need explicit garbage in the code? :-)

If I recall correctly, because the hardware explicitly writes garbage into that location during the read (it probably claims it's status flags of some such, but as far as we are concerned it's garbage).

Note there is a nice comment just above this structure explaining why :)

Jonathan

Quoted 5 lines
drivers/iio/light/rpr0521.c-204-                __le16 channels[3];
drivers/iio/light/rpr0521.c-205-                u8 garbage;
drivers/iio/light/rpr0521.c:206:                s64 ts __aligned(8);
maintainer
#15 lore raw

Re: [PATCH v2 2/2] iio: adc: Add rtq6056 support

In reply to ChiYuan Huang
Quoted 46 lines · On Tue, 5 Jul 2022 09:41:39 +0800 ChiYuan Huang <[email protected]> wrote:

Andy Shevchenko <[email protected]> 於 2022年7月5日 週二 清晨5:52寫道:

Quoted 35 lines · ChiYuan Huang
Quoted 3 lines

ChiYuan Huang <[email protected]> 於 2022年7月4日 週一 上午11:16寫道:

Quoted 2 lines

Andy Shevchenko <[email protected]> 於 2022年7月1日 週五 下午6:05寫道:

Quoted 1 line · cy_huang

...

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ *val = DIV_ROUND_UP(1000000, sample_time);

USEC_PER_SEC ?

No, sample time is (vshunt convesion time + vbus conversion time) * average sample.
And the sample freq returns the unit by HZ (sample frequency per second)

The 'sample time' is unit by micro-second like as you mentioned.

Ah, then it should be MICRO, so we will get Hz.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return IIO_VAL_INT;
+}  

...

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/rtq6056.c line 452

Quoted 4 lines
drivers/iio/adc/rtq6056.c new file +4 -0
+       struct {
+               u16 vals[RTQ6056_MAX_CHANNEL];
+               int64_t timestamp;
+       } data __aligned(8);  

Hmm... alignment of this struct will be at least 4 bytes, but shouldn't we rather be sure that the timestamp member is aligned properly? Otherwise this seems fragile and dependent on RTQ6056_MAX_CHANNEL % 4 == 0.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Yap, from the 'max channel', it already guarantee this struct will be aligned at lease 4.
Actually, It can be removed.

I think for the safest side it should be given to the timestamp member. No?

Sorry, following your comment, Why to use 'align' for the timestamp member? the data member already guarantee 2 * 4 = 8 byte, then timestamp will be 8 byte aligned, right?

Quoted 6 lines

what you mentioned is to put __aligned(8) only for timestamp.

I try to put aligned in two ways ( one is only for timestamp, another is the whole struct). the result is the same.
From my thinking, in this case, the struct is already 8 byte aligned for timestamp member. don't you think to put 'aligned' is redundant?

On the 8 byte alignment question... Look up alignment of s64 on x86_32... It's 4 byte aligned. We had a lot of 'fun' fixing this a few years ago.

So the marking of __aligned(8) for the timestamp does 2 things (and it takes a fairly close reading of the c spec to check this).

1) Forces alignment of the timestamp. Needed so we can cheaply write the timestamp
2) Forces alignment of the containing structure.

The combination of these 2 enforces the padding being consistent across architectures whether or not they align s64 to 4 or 8 bytes. This last part is the subtle element that explains why on some architectures you need the __aligned(8) on the timestamp not the outer structure.

Jonathan

Quoted 3 lines
Quoted 3 lines

--
With Best Regards,
Andy Shevchenko