Skip to contentSkip to conversation

Support for Avago APDS9306 Ambient Light Sensor

PATCH Superseded Subhajit Ghosh 6 participants 24 messages Jan 21, 2024 – Feb 4, 2024

Subsystems (3)

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Subhajit Ghosh

author maintainer
#0 lore raw

v5 cover Support for Avago APDS9306 Ambient Light Sensor

Support for Avago APDS9306 Ambient Light Sensor

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

Following features are supported:
- I2C interface
- 2 channels - als and clear
- Raw data for als and clear channels
- Up to 20 bit resolution
- 20 bit data register for each channel
- Common Configurable items for both channels
- Integration Time
- Scale
- Interrupt (event) interface
- High and Low threshold interrupts for each channel
- Selection of interrupt channels - als or clear
- Selection of interrupt mode - threshold or adaptive
- Level selection for adaptive threshold interrupts
- Persistence (Period) level selection for interrupts

root@stm32mp1:~# tree -I 'dev|name|of_node|power|subsystem|uevent' \
/sys/bus/iio/devices/iio:device0/
|-- events
|   |-- in_illuminance_thresh_either_en
|   |-- in_intensity_clear_thresh_either_en
|   |-- thresh_adaptive_either_en
|   |-- thresh_adaptive_either_value
|   |-- thresh_adaptive_either_values_available
|   |-- thresh_either_period
|   |-- thresh_either_period_available
|   |-- thresh_falling_value
|   `-- thresh_rising_value
|-- in_illuminance_raw
|-- in_illuminance_scale
|-- in_illuminance_scale_available
|-- in_intensity_clear_raw
|-- integration_time
|-- integration_time_available
|-- sampling_frequency
|-- sampling_frequency_available
`-- waiting_for_supplier

1 directory, 18 files

v2 -> v5:
- Bumped up the version:
RFC->v0->v1->v2->v3 (Earlier scheme) v1->v2->v3->v4->v5 (Scheme after review) (Current scheme)

- Added separate patch to merge schemas for APDS9300 and APDS9906. Added APDS9306 support on top of that.

- Removed scale attribute for Intensity channel:

- Dropped caching of hardware gain, repeat rate and integration time and updated code as per earlier reviews.

- Added descriptive commit messages
- Fixed wrongly formatted commit messages
- Added changelog in right positions

- Link to v2:
https://lore.kernel.org/lkml/[email protected]/

v2 -> v5 Bindings:
- Removed 'required' for Interrupts and 'oneOf' for compatibility strings as per below reviews:

- Implemented changes as per previous reviews:

Subhajit Ghosh (3):
dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas
dt-bindings: iio: light: Avago APDS9306
iio: light: Add support for APDS9306 Light Sensor

 .../bindings/iio/light/avago,apds9300.yaml    |   20 +-
 .../bindings/iio/light/avago,apds9960.yaml    |   44 -
 drivers/iio/light/Kconfig                     |   12 +
 drivers/iio/light/Makefile                    |    1 +
 drivers/iio/light/apds9306.c                  | 1315 +++++++++++++++++
 5 files changed, 1343 insertions(+), 49 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
 create mode 100644 drivers/iio/light/apds9306.c

base-commit: 9d1694dc91ce7b80bc96d6d8eaf1a1eca668d847

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v5 1/3 dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas

Squashing Avago (Broadcom) APDS9300 and APDS9960 schemas into one file and removing the other. This is done as per the below review:

This patch series adds the driver support and device tree binding schemas for APDS9306 Ambient Light Sensor. It was pointed out in earlier reviews that the schemas for APDS9300 and APDS9960 looks similar and should be merged. This particular patch does the first operation of merging APDS9300 and APDS9960 schema files.

---
v2 -> v5:
- Removed 'required' for Interrupts and 'oneOf' for compatibility strings as per below reviews:

---
 .../bindings/iio/light/avago,apds9300.yaml    | 11 +++--
 .../bindings/iio/light/avago,apds9960.yaml    | 44 -------------------
 2 files changed, 7 insertions(+), 48 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +7 -4
index 206af44f2c43..c610780346e8 100644
@@ -4,17 +4,20 @@
 $id: http://devicetree.org/schemas/iio/light/avago,apds9300.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Avago APDS9300 ambient light sensor
+title: Avago Gesture/RGB/ALS/Proximity sensors
 
 maintainers:
-  - Jonathan Cameron <[email protected]>
+  - Subhajit Ghosh <[email protected]>
 
 description: |
-  Datasheet at https://www.avagotech.com/docs/AV02-1077EN
+  Datasheet: https://www.avagotech.com/docs/AV02-1077EN
+  Datasheet: https://www.avagotech.com/docs/AV02-4191EN
 
 properties:
   compatible:
-    const: avago,apds9300
+    enum:
+      - avago,apds9300
+      - avago,apds9960
 
   reg:
     maxItems: 1
Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml deleted +0 -44

44 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v5 1/3] dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas

Quoted 3 lines · On Sun, 21 Jan 2024 15:47:32 +1030 Subhajit Ghosh…

Squashing Avago (Broadcom) APDS9300 and APDS9960 schemas into one file and removing the other. This is done as per the below review:

Sounds like a Suggested-by tag to reflect the ideas would be sensible here.

Quoted 3 lines

This patch series adds the driver support and device tree binding schemas for APDS9306 Ambient Light Sensor.

This sentence isn't relevant to this patch, so I'd drop it. We don't need additional motivation.

Quoted 4 lines

It was pointed out in earlier reviews
that the schemas for APDS9300 and APDS9960 looks similar and should be merged. This particular patch does the first operation of merging APDS9300 and APDS9960 schema files.

You have a reference above which is enough.

"Merge very similar schemas for APDS9300 and APDS9960."
is sufficient description alongside a suggested by tag and if you like a link tag to as above. Note however that Link is an official tag so belongs in the tag block at the end, not inline in the text.

Quoted 1 line
Link ...
Quoted 92 lines

---
v2 -> v5:
- Removed 'required' for Interrupts and 'oneOf' for compatibility strings as per below reviews:

   Link: https://lore.kernel.org/lkml/20231028142944.7e210eb6@jic23-huawei/
   Link: https://lore.kernel.org/lkml/[email protected]/
---
 .../bindings/iio/light/avago,apds9300.yaml    | 11 +++--
 .../bindings/iio/light/avago,apds9960.yaml    | 44 -------------------
 2 files changed, 7 insertions(+), 48 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +7 -4
index 206af44f2c43..c610780346e8 100644
@@ -4,17 +4,20 @@
 $id: http://devicetree.org/schemas/iio/light/avago,apds9300.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Avago APDS9300 ambient light sensor
+title: Avago Gesture/RGB/ALS/Proximity sensors
 
 maintainers:
-  - Jonathan Cameron <[email protected]>
+  - Subhajit Ghosh <[email protected]>
 
 description: |
-  Datasheet at https://www.avagotech.com/docs/AV02-1077EN
+  Datasheet: https://www.avagotech.com/docs/AV02-1077EN
+  Datasheet: https://www.avagotech.com/docs/AV02-4191EN
 
 properties:
   compatible:
-    const: avago,apds9300
+    enum:
+      - avago,apds9300
+      - avago,apds9960
 
   reg:
     maxItems: 1
Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml deleted +0 -44
deleted file mode 100644
index f06e0fda5629..000000000000
@@ -1,44 +0,0 @@
-# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
-%YAML 1.2
----
-$id: http://devicetree.org/schemas/iio/light/avago,apds9960.yaml#
-$schema: http://devicetree.org/meta-schemas/core.yaml#
-
-title: Avago APDS9960 gesture/RGB/ALS/proximity sensor
-
-maintainers:
-  - Matt Ranostay <[email protected]>
-
-description: |
-  Datasheet at https://www.avagotech.com/docs/AV02-4191EN
-
-properties:
-  compatible:
-    const: avago,apds9960
-
-  reg:
-    maxItems: 1
-
-  interrupts:
-    maxItems: 1
-
-additionalProperties: false
-
-required:
-  - compatible
-  - reg
-
-examples:
-  - |
-    i2c {
-        #address-cells = <1>;
-        #size-cells = <0>;
-
-        light-sensor@39 {
-            compatible = "avago,apds9960";
-            reg = <0x39>;
-            interrupt-parent = <&gpio1>;
-            interrupts = <16 1>;
-        };
-    };
-...
author maintainer
#3 lore raw

Re: [PATCH v5 1/3] dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas

Quoted 22 lines · Jonathan Cameron
Quoted 3 lines · On Sun, 21 Jan 2024 15:47:32 +1030 Subhajit Ghosh…

Squashing Avago (Broadcom) APDS9300 and APDS9960 schemas into one file and removing the other. This is done as per the below review:

Sounds like a Suggested-by tag to reflect the ideas would be sensible here.

Quoted 3 lines

This patch series adds the driver support and device tree binding schemas for APDS9306 Ambient Light Sensor.

This sentence isn't relevant to this patch, so I'd drop it. We don't need additional motivation.

Quoted 4 lines

It was pointed out in earlier reviews
that the schemas for APDS9300 and APDS9960 looks similar and should be merged. This particular patch does the first operation of merging APDS9300 and APDS9960 schema files.

You have a reference above which is enough.

Quoted 7 lines

"Merge very similar schemas for APDS9300 and APDS9960."
is sufficient description alongside a suggested by tag and if you like a link tag to as above. Note however that Link is an official tag so belongs in the tag block at the end, not inline in the text.

Quoted 1 line
Link ...

Thank you so much Jonathan for taking time to explain these things.

Regards,
Subhajit Ghosh

Reviewed-by on v5 1/3
#4 lore raw

Re: [PATCH v5 1/3] dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas

In reply to Subhajit Ghosh
Quoted 5 lines · Subhajit Ghosh

Squashing Avago (Broadcom) APDS9300 and APDS9960 schemas into one file and removing the other. This is done as per the below review:

This patch series adds the driver support and device tree binding schemas

Please do not use "This commit/patch/change", but imperative mood. See longer explanation here:
https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Also, this is one commit, not patch series. Please write commit msg explaining why and what you are doing.

Quoted 4 lines

for APDS9306 Ambient Light Sensor. It was pointed out in earlier reviews that the schemas for APDS9300 and APDS9960 looks similar and should be merged. This particular patch does the first operation of merging APDS9300 and APDS9960 schema files.

https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Quoted 2 lines

With changes above and what Jonathan asks:

---

This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:

Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions, under or above your Signed-off-by tag. Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.

https://elixir.bootlin.com/linux/v6.5-rc3/source/Documentation/process/submitting-patches.rst#L577

Best regards,
Krzysztof

author maintainer
#5 lore raw

Re: [PATCH v5 1/3] dt-bindings: iio: light: Squash APDS9300 and APDS9960 schemas

Quoted 44 lines · Krzysztof Kozlowski
Quoted 5 lines · Subhajit Ghosh

Squashing Avago (Broadcom) APDS9300 and APDS9960 schemas into one file and removing the other. This is done as per the below review:

This patch series adds the driver support and device tree binding schemas

Please do not use "This commit/patch/change", but imperative mood. See longer explanation here:
https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Also, this is one commit, not patch series. Please write commit msg explaining why and what you are doing.

Quoted 4 lines

for APDS9306 Ambient Light Sensor. It was pointed out in earlier reviews that the schemas for APDS9300 and APDS9960 looks similar and should be merged. This particular patch does the first operation of merging APDS9300 and APDS9960 schema files.

https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Quoted 2 lines

With changes above and what Jonathan asks:

---

This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:

Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions, under or above your Signed-off-by tag. Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.

Quoted 6 lines

https://elixir.bootlin.com/linux/v6.5-rc3/source/Documentation/process/submitting-patches.rst#L577

Best regards,
Krzysztof

Thank you so much Krysztof for the useful resources. Although I got the working knowledge from "A Beginner's Guide to Linux Kernel Development" course offered by Linux Foundation but there are gaps in my understanding. Time to study more.

Regards,
Subhajit Ghosh

author maintainer
#6 lore raw

PATCH v5 2/3 dt-bindings: iio: light: Avago APDS9306

In reply to Subhajit Ghosh

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file. Adding vdd-supply property.

---
v2 -> v5:
- Implemented changes as per previous reviews:

---
 .../bindings/iio/light/avago,apds9300.yaml        | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +11 -4
index c610780346e8..bee73a590424 100644
@@ -4,19 +4,21 @@
 $id: http://devicetree.org/schemas/iio/light/avago,apds9300.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Avago Gesture/RGB/ALS/Proximity sensors
+title: Avago (Broadcom) Gesture/RGB/ALS/Proximity sensors
 
 maintainers:
   - Subhajit Ghosh <[email protected]>
 
 description: |
-  Datasheet: https://www.avagotech.com/docs/AV02-1077EN
-  Datasheet: https://www.avagotech.com/docs/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-1077EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4755EN
 
 properties:
   compatible:
     enum:
       - avago,apds9300
+      - avago,apds9306
       - avago,apds9960
 
   reg:
@@ -25,6 +27,8 @@ properties:
   interrupts:
     maxItems: 1
 
+  vdd-supply: true
+
 additionalProperties: false
 
 required:
@@ -33,6 +37,8 @@ required:
 
 examples:
   - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;
@@ -41,7 +47,8 @@ examples:
             compatible = "avago,apds9300";
             reg = <0x39>;
             interrupt-parent = <&gpio2>;
-            interrupts = <29 8>;
+            interrupts = <29 IRQ_TYPE_LEVEL_LOW>;
+            vdd-supply = <&regulator_3v3>;
         };
     };
 ...
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: light: Avago APDS9306

Quoted 3 lines · On Sun, 21 Jan 2024 15:47:33 +1030 Subhajit Ghosh…

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file.

This is an unrelated change, so should probably be in a separate patch.

Quoted 1 line

Adding vdd-supply property.

This one is reasonable to have in same patch as the new device addition as, whilst I assume it's valid for the existing devices, you are adding it to incorporate something that device also has.
Could also be a separate precursor patch.

Quoted 21 lines

---
v2 -> v5:
- Implemented changes as per previous reviews:

   Link: https://lore.kernel.org/lkml/20231028142944.7e210eb6@jic23-huawei/
   Link: https://lore.kernel.org/lkml/[email protected]/
---
 .../bindings/iio/light/avago,apds9300.yaml        | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +1 -1
index c610780346e8..bee73a590424 100644
@@ -4,19 +4,21 @@
 $id: http://devicetree.org/schemas/iio/light/avago,apds9300.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Avago Gesture/RGB/ALS/Proximity sensors
+title: Avago (Broadcom) Gesture/RGB/ALS/Proximity sensors

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml line 15

Quoted 10 lines
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +3 -2
 
 maintainers:
   - Subhajit Ghosh <[email protected]>
 
 description: |
-  Datasheet: https://www.avagotech.com/docs/AV02-1077EN
-  Datasheet: https://www.avagotech.com/docs/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-1077EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4755EN

Old links seem to still work, so why the change?

Quoted 37 lines
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +7 -1
 properties:
   compatible:
     enum:
       - avago,apds9300
+      - avago,apds9306
       - avago,apds9960
 
   reg:
@@ -25,6 +27,8 @@ properties:
   interrupts:
     maxItems: 1
 
+  vdd-supply: true
+
 additionalProperties: false
 
 required:
@@ -33,6 +37,8 @@ required:
 
 examples:
   - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;
@@ -41,7 +47,8 @@ examples:
             compatible = "avago,apds9300";
             reg = <0x39>;
             interrupt-parent = <&gpio2>;
-            interrupts = <29 8>;
+            interrupts = <29 IRQ_TYPE_LEVEL_LOW>;
+            vdd-supply = <&regulator_3v3>;
         };
     };
...
author maintainer
#8 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: light: Avago APDS9306

Quoted 8 lines · Jonathan Cameron
Quoted 3 lines · On Sun, 21 Jan 2024 15:47:33 +1030 Subhajit Ghosh…

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file.

This is an unrelated change, so should probably be in a separate patch.

Understood.

Quoted 11 lines
Quoted 2 lines

Adding vdd-supply property.

This one is reasonable to have in same patch as the new device addition as, whilst I assume it's valid for the existing devices, you are adding it to incorporate something that device also has.
Could also be a separate precursor patch.

Quoted 3 lines
Quoted 8 lines

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml line 15

Quoted 6 lines

description: |

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +3 -2
-  Datasheet: https://www.avagotech.com/docs/AV02-1077EN
-  Datasheet: https://www.avagotech.com/docs/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-1077EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4191EN
+  Datasheet: https://docs.broadcom.com/doc/AV02-4755EN

Old links seem to still work, so why the change?

My bad. I will revert to keep things simple. No reason, just thought of updating.

Thanks for the review.

Regards,
Subhajit Ghosh

#9 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: light: Avago APDS9306

In reply to Subhajit Ghosh
Quoted 4 lines · Subhajit Ghosh

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file. Adding vdd-supply property.

Why? Do other devices have it?

Quoted 11 lines

required:

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +2 -0
@@ -33,6 +37,8 @@ required:
 
 examples:
   - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml examples: · line 50

Quoted 5 lines
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +1 -1
@@ -41,7 +47,8 @@ examples:
             compatible = "avago,apds9300";
             reg = <0x39>;
             interrupt-parent = <&gpio2>;
-            interrupts = <29 8>;
+            interrupts = <29 IRQ_TYPE_LEVEL_LOW>;

Separate change.

Best regards,
Krzysztof

author maintainer
#10 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: light: Avago APDS9306

Quoted 7 lines · Krzysztof Kozlowski
Quoted 4 lines · Subhajit Ghosh

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file. Adding vdd-supply property.

Why? Do other devices have it?

Are you referring to vdd-supply? No, I guess, haven't checked actually. If other devices don't have, can you suggest the best way to handle that?

Quoted 5 lines
Quoted 5 lines

required:

(quoted diff) +0 -0
@@ -33,6 +37,8 @@ required:
 
Quoted 15 lines

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml examples: · line 50

Quoted 13 lines
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +3 -1
  examples:
    - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
      i2c {
          #address-cells = <1>;
          #size-cells = <0>;
@@ -41,7 +47,8 @@ examples:
              compatible = "avago,apds9300";
              reg = <0x39>;
              interrupt-parent = <&gpio2>;
-            interrupts = <29 8>;
+            interrupts = <29 IRQ_TYPE_LEVEL_LOW>;

Separate change.

Understood.

Quoted 4 lines

Best regards,
Krzysztof

Regards,
Subhajit Ghosh

#11 lore raw

Re: [PATCH v5 2/3] dt-bindings: iio: light: Avago APDS9306

Quoted 10 lines · Subhajit Ghosh
Quoted 7 lines · Krzysztof Kozlowski
Quoted 4 lines · Subhajit Ghosh

Adding device tree support for APDS9306 Ambient Light Sensor. Updating datasheet hyperlinks.
Adding interrupt definition macro and header file. Adding vdd-supply property.

Why? Do other devices have it?

Are you referring to vdd-supply? No, I guess, haven't checked actually. If other devices don't have, can you suggest the best way to handle that?

Commit msg should explain that. I assume all devices need it, so just like Jonathan suggested: this might be separate patch.

Best regards,
Krzysztof

author maintainer
#12 lore raw

PATCH v5 3/3 iio: light: Add support for APDS9306 Light Sensor

In reply to Subhajit Ghosh

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

---
v2 -> v5:
- Removed scale attribute for Intensity channel:

- Dropped caching of hardware gain, repeat rate and integration time and updated code as per earlier reviews.

---
 drivers/iio/light/Kconfig    |   12 +
 drivers/iio/light/Makefile   |    1 +
 drivers/iio/light/apds9306.c | 1315 ++++++++++++++++++++++++++++++++++
 3 files changed, 1328 insertions(+)
 create mode 100644 drivers/iio/light/apds9306.c
drivers/iio/light/Kconfig +12 -0
index 143003232d1c..fd972dd0364d 100644
@@ -73,6 +73,18 @@ config APDS9300
 	  To compile this driver as a module, choose M here: the
 	  module will be called apds9300.
 
+config APDS9306
+	tristate "Avago APDS9306 Ambient Light Sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  If you say Y or M here, you get support for Avago APDS9306
+	  Ambient Light Sensor.
+
+	  If built as a dynamically linked module, it will be called
+	  apds9306.
+
 config APDS9960
 	tristate "Avago APDS9960 gesture/RGB/ALS/proximity sensor"
 	select REGMAP_I2C
drivers/iio/light/Makefile +1 -0
index 2e5fdb33e0e9..a30f906e91ba 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_ADUX1020)		+= adux1020.o
 obj-$(CONFIG_AL3010)		+= al3010.o
 obj-$(CONFIG_AL3320A)		+= al3320a.o
 obj-$(CONFIG_APDS9300)		+= apds9300.o
+obj-$(CONFIG_APDS9306)		+= apds9306.o
 obj-$(CONFIG_APDS9960)		+= apds9960.o
 obj-$(CONFIG_AS73211)		+= as73211.o
 obj-$(CONFIG_BH1750)		+= bh1750.o
drivers/iio/light/apds9306.c new file +1315 -0

1,315 lines not on this page · raw message

Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Le 21/01/2024 à 06:17, Subhajit Ghosh a écrit :

Quoted 11 lines

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

Quoted 5 lines

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

---

Hi,

a few nits and a few real comment/question below.

Just my 2c.

CJ
...

drivers/iio/light/apds9306.c line 59

Quoted 3 lines
drivers/iio/light/apds9306.c new file +3 -0
+#define APDS9306_ALS_THRES_VAL_MAX	(BIT(20) - 1)
+#define APDS9306_ALS_THRES_VAR_VAL_MAX	(BIT(3) - 1)
+#define APDS9306_ALS_PERSIST_VAL_MAX	(BIT(4) - 1)

Nit: GENMASK()?

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+#define APDS9306_ALS_READ_DATA_DELAY_US	20000
+#define APDS9306_NUM_REPEAT_RATES	7

...

Quoted 17 lines
drivers/iio/light/apds9306.c new file +17 -0
+static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
+{
+	struct device *dev = data->dev;
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	int ret, delay, intg_time, intg_time_idx, repeat_rate_idx, int_src;
+	int status = 0;
+	u8 buff[3];
+
+	ret = apds9306_runtime_power_on(data->dev);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->regfield_intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->regfield_repeat_rate, &repeat_rate_idx);

drivers/iio/light/apds9306.c line 506

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->regfield_int_src, &int_src);
+	if (ret)
+		return ret;
+
+	intg_time = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx);
+	if (intg_time < 0)
+		delay = apds9306_repeat_rate_period[repeat_rate_idx];

'delay' is always overwritten by the line below.

Quoted 28 lines
drivers/iio/light/apds9306.c new file +28 -0
+
+	/*
+	 * Whichever is greater - integration time period or
+	 * sampling period.
+	 */
+	delay = max(intg_time,
+		    apds9306_repeat_rate_period[repeat_rate_idx]);
+
+	/*
+	 * Clear stale data flag that might have been set by the interrupt
+	 * handler if it got data available flag set in the status reg.
+	 */
+	data->read_data_available = 0;
+
+	/*
+	 * If this function runs parallel with the interrupt handler, either
+	 * this reads and clears the status registers or the interrupt handler
+	 * does. The interrupt handler sets a flag for read data available
+	 * in our private structure which we read here.
+	 */
+	ret = regmap_read_poll_timeout(data->regmap, APDS9306_MAIN_STATUS_REG,
+				status, (status & (APDS9306_ALS_DATA_STAT_MASK |
+				APDS9306_ALS_INT_STAT_MASK)) ||
+				data->read_data_available,
+				APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
+	if (ret)
+		return ret;
+

drivers/iio/light/apds9306.c line 544

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	/* If we reach here before the interrupt handler we push an event */
+	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
+		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT, int_src,
+			       IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),
+			       iio_get_time_ns(indio_dev));
+	}
+
+	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
+	if (ret) {
+		dev_err(dev, "read data failed\n");

Would dev_err_ratelimited() make sense here?

Quoted 7 lines
drivers/iio/light/apds9306.c new file +7 -0
+		return ret;
+	}
+
+	*val = get_unaligned_le24(&buff);
+
+	return apds9306_runtime_power_off(dev);
+}

...

drivers/iio/light/apds9306.c line 578

Quoted 9 lines
drivers/iio/light/apds9306.c new file +9 -0
+static int apds9306_intg_time_set(struct apds9306_data *data, int val2)
+{
+	struct device *dev = data->dev;
+	int ret, intg_old, gain_old, gain_new, gain_new_closest, intg_time_idx;
+	int gain_idx;
+	bool ok;
+
+	if (!iio_gts_valid_time(&data->gts, val2)) {
+		dev_err(dev, "Unsupported integration time %u\n", val2);

Would dev_err_ratelimited() make sense here?

Quoted 16 lines
drivers/iio/light/apds9306.c new file +16 -0
+		return -EINVAL;
+	}
+
+	ret = regmap_field_read(data->regfield_intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(data->regfield_gain, &gain_idx);
+	if (ret)
+		return ret;
+
+	intg_old = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx);
+	if (ret < 0)
+		return ret;
+
+	if (intg_old == val2)

drivers/iio/light/apds9306.c line 604

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		return 0;
+
+	gain_old = iio_gts_find_gain_by_sel(&data->gts, gain_idx);
+	if (gain_old < 0)
+		return gain_old;
+
+	ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, gain_old, intg_old,
+						     val2, &gain_new);
+	if (gain_new < 0) {
+		dev_err(dev, "Unsupported gain with time\n");

Would dev_err_ratelimited() make sense here?

Quoted 26 lines
drivers/iio/light/apds9306.c new file +26 -0
+		return gain_new;
+	}
+
+	gain_new_closest = iio_find_closest_gain_low(&data->gts, gain_new, &ok);
+	if (gain_new_closest < 0) {
+		gain_new_closest = iio_gts_get_min_gain(&data->gts);
+		if (gain_new_closest < 0)
+			return gain_new_closest;
+	}
+	if (!ok)
+		dev_dbg(dev, "Unable to find optimum gain, setting minimum");
+
+	ret = iio_gts_find_sel_by_int_time(&data->gts, val2);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_field_write(data->regfield_intg_time, ret);
+	if (ret)
+		return ret;
+
+	ret = iio_gts_find_sel_by_gain(&data->gts, gain_new_closest);
+	if (ret < 0)
+		return ret;
+
+	return regmap_field_write(data->regfield_gain, ret);
+}

...

drivers/iio/light/apds9306.c line 730

Quoted 9 lines
drivers/iio/light/apds9306.c new file +9 -0
+static int apds9306_event_period_get(struct apds9306_data *data, int *val)
+{
+	int period, ret;
+
+	ret = regmap_field_read(data->regfield_int_persist_val, &period);
+	if (ret)
+		return ret;
+
+	if (period > APDS9306_ALS_PERSIST_VAL_MAX)

Nit: in_range() to be consistent with code below?

Quoted 14 lines
drivers/iio/light/apds9306.c new file +14 -0
+		return -EINVAL;
+
+	*val = period;
+
+	return ret;
+}
+
+static int apds9306_event_period_set(struct apds9306_data *data, int val)
+{
+	if (!in_range(val, 0, APDS9306_ALS_PERSIST_VAL_MAX))
+		return -EINVAL;
+
+	return regmap_field_write(data->regfield_int_persist_val, val);
+}

...

Quoted 21 lines
drivers/iio/light/apds9306.c new file +21 -0
+static int apds9306_event_thresh_set(struct apds9306_data *data, int dir,
+				     int val)
+{
+	int var;
+	u8 buff[3];
+
+	if (dir == IIO_EV_DIR_RISING)
+		var = APDS9306_ALS_THRES_UP_0_REG;
+	else if (dir == IIO_EV_DIR_FALLING)
+		var = APDS9306_ALS_THRES_LOW_0_REG;
+	else
+		return -EINVAL;
+
+	if (!in_range(val, 0, APDS9306_ALS_THRES_VAL_MAX))
+		return -EINVAL;
+
+	put_unaligned_le24(val, buff);
+
+	return regmap_bulk_write(data->regmap, var, buff, sizeof(buff));
+}
+

drivers/iio/light/apds9306.c line 798

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+static int apds9306_event_thresh_adaptive_get(struct apds9306_data *data,
+					      int *val)
+{
+	int thr_adpt, ret;
+
+	ret = regmap_field_read(data->regfield_int_thresh_var_val, &thr_adpt);
+	if (ret)
+		return ret;
+
+	if (thr_adpt > APDS9306_ALS_THRES_VAR_VAL_MAX)

Nit: in_range()? to be consistent with code below and above.

Quoted 15 lines
drivers/iio/light/apds9306.c new file +15 -0
+		return -EINVAL;
+
+	*val = thr_adpt;
+
+	return ret;
+}
+
+static int apds9306_event_thresh_adaptive_set(struct apds9306_data *data,
+		int val)
+{
+	if (!in_range(val, 0, APDS9306_ALS_THRES_VAR_VAL_MAX))
+		return -EINVAL;
+
+	return regmap_field_write(data->regfield_int_thresh_var_val, val);
+}

...

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+static irqreturn_t apds9306_irq_handler(int irq, void *priv)
+{
+	struct iio_dev *indio_dev = priv;
+	struct apds9306_data *data = iio_priv(indio_dev);

drivers/iio/light/apds9306.c line 934

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	int ret, status, int_ch;
+
+	/*
+	 * The interrupt line is released and the interrupt flag is
+	 * cleared as a result of reading the status register. All the
+	 * status flags are cleared as a result of this read.
+	 */
+	ret = regmap_read(data->regmap, APDS9306_MAIN_STATUS_REG, &status);
+	if (ret < 0) {
+		dev_err(data->dev, "status reg read failed\n");

Would dev_err_ratelimited() make sense here?

drivers/iio/light/apds9306.c line 946

Quoted 12 lines
drivers/iio/light/apds9306.c new file +12 -0
+		return IRQ_HANDLED;
+	}
+
+	ret = regmap_field_read(data->regfield_int_src, &int_ch);
+	if (ret)
+		return ret;
+
+	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
+		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT, int_ch,
+				   IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),
+				   iio_get_time_ns(indio_dev));
+	}

Nit: superfluous {}

Quoted 12 lines
drivers/iio/light/apds9306.c new file +12 -0
+
+	/*
+	 * If a one-shot read through sysfs is underway at the same time
+	 * as this interrupt handler is executing and a read data available
+	 * flag was set, this flag is set to inform read_poll_timeout()
+	 * to exit.
+	 */
+	if ((status & APDS9306_ALS_DATA_STAT_MASK))
+		data->read_data_available = 1;
+
+	return IRQ_HANDLED;
+}

drivers/iio/light/apds9306.c line 968

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+
+static int apds9306_read_event(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 apds9306_data *data = iio_priv(indio_dev);
+	int ret;

Other functions below that look really similar have a:
guard(mutex)(&data->mutex);

Is it needed here?

Quoted 19 lines
drivers/iio/light/apds9306.c new file +19 -0
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		if (dir == IIO_EV_DIR_EITHER && info == IIO_EV_INFO_PERIOD)
+			ret = apds9306_event_period_get(data, val);
+		else
+			ret = apds9306_event_thresh_get(data, dir, val);
+		if (ret)
+			return ret;
+		return IIO_VAL_INT;
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		ret = apds9306_event_thresh_adaptive_get(data, val);
+		if (ret)
+			return ret;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}

...

#14 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 1 line · Christophe JAILLET

Le 21/01/2024 à 06:17, Subhajit Ghosh a écrit :

...

Quoted 5 lines

drivers/iio/light/apds9306.c line 59

Quoted 3 lines
drivers/iio/light/apds9306.c new file +3 -0
+#define APDS9306_ALS_THRES_VAL_MAX	(BIT(20) - 1)
+#define APDS9306_ALS_THRES_VAR_VAL_MAX	(BIT(3) - 1)
+#define APDS9306_ALS_PERSIST_VAL_MAX	(BIT(4) - 1)

Nit: GENMASK()?

I think no, these are plain numbers given in the form of BIT(x) - 1 to show the HW limit on the values (in bits).

...

Quoted 5 lines

drivers/iio/light/apds9306.c line 506

Quoted 3 lines
drivers/iio/light/apds9306.c new file +3 -0
+	intg_time = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx);
+	if (intg_time < 0)
+		delay = apds9306_repeat_rate_period[repeat_rate_idx];

'delay' is always overwritten by the line below.

Seems like entire conditional here is not needed as it's implied in the max(). And max() should take both signed or unsigned types, different types might have a side effect.

Quoted 6 lines
Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+	/*
+	 * Whichever is greater - integration time period or
+	 * sampling period.
+	 */
+	delay = max(intg_time,
+		    apds9306_repeat_rate_period[repeat_rate_idx]);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#15 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 5 lines · Andy Shevchenko

On Sun, Jan 21, 2024 at 10:22:50AM +0100, Christophe JAILLET wrote:

Seems like entire conditional here is not needed as it's implied in the max(). And max() should take both signed or unsigned types, different types might have a side effect.

Ok, understood.

Quoted 8 lines
Quoted 7 lines

drivers/iio/light/apds9306.c line 513

Quoted 7 lines
drivers/iio/light/apds9306.c new file +6 -0
+	/*
+	 * Whichever is greater - integration time period or
+	 * sampling period.
+	 */
+	delay = max(intg_time,
+		    apds9306_repeat_rate_period[repeat_rate_idx]);

Thanks for the review.

Regards,
Subhajit Ghosh

author maintainer
#16 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

In reply to Christophe JAILLET
Quoted 41 lines · Christophe JAILLET

Le 21/01/2024 à 06:17, Subhajit Ghosh a écrit :

Quoted 11 lines

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

Quoted 5 lines

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

---

Hi,

a few nits and a few real comment/question below.

Just my 2c.

CJ
...

drivers/iio/light/apds9306.c line 968

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+
+static int apds9306_read_event(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 apds9306_data *data = iio_priv(indio_dev);
+    int ret;

Other functions below that look really similar have a:
   guard(mutex)(&data->mutex);

Quoted 1 line

Is it needed here?

You are right, don't think so. Regmap lock is being used.
I will review the locking mechanism. Acknowledging all other comments.

Thanks for the review.

Regards,
Subhajit Ghosh

maintainer
#17 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

In reply to Subhajit Ghosh
Quoted 11 lines · On Sun, 21 Jan 2024 15:47:34 +1030 Subhajit Ghosh…

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

Quoted 6 lines

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

---
v2 -> v5:

Why did you jump to v5? Some internal or private reviews perhaps? Better for those tracking on the list if you just used v3.

Quoted 6 lines

- Removed scale attribute for Intensity channel:

   Link: https://lore.kernel.org/all/20231204095108.22f89718@jic23-huawei/

- Dropped caching of hardware gain, repeat rate and integration time and updated code as per earlier reviews.

...

A few, mostly very minor comments inline to add to Christophe's review.

Thanks,

Jonathan

drivers/iio/light/apds9306.c line 7

Quoted 13 lines
drivers/iio/light/apds9306.c new file +7 -0
new file mode 100644
index 000000000000..8ed5899050ed
@@ -0,0 +1,1315 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * APDS-9306/APDS-9306-065 Ambient Light Sensor
+ * I2C Address: 0x52
+ * Datasheet: https://docs.broadcom.com/doc/AV02-4755EN
+ *
+ * Copyright (C) 2023 Subhajit Ghosh <[email protected]>

Given you are still changing it, feel free to include 2024!

Quoted 1 line
drivers/iio/light/apds9306.c new file +1 -0
+ */

...

drivers/iio/light/apds9306.c line 126

Quoted 8 lines
drivers/iio/light/apds9306.c new file +8 -0
+static const int apds9306_repeat_rate_freq[][2] = {
+	{40, 0},
+	{20, 0},
+	{10, 0},
+	{5,  0},
+	{2,  0},
+	{1,  0},
+	{0, 500000},

Prefer

	{ 40, 0 },

etc and whilst I don't really like forcing alignment like this, if you are going to do it be consistent. The last 50000 is one space too far to the left I think.

Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+};
+static_assert(ARRAY_SIZE(apds9306_repeat_rate_freq) ==
+		APDS9306_NUM_REPEAT_RATES);
+
+static const int apds9306_repeat_rate_period[] = {
+	25000, 50000, 100000, 200000, 500000, 1000000, 2000000,

drivers/iio/light/apds9306.c line 142

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+};
+static_assert(ARRAY_SIZE(apds9306_repeat_rate_period) ==
+		APDS9306_NUM_REPEAT_RATES);
+
+/**
+ * struct apds9306_data - apds9306 private data and registers definitions
+ *
+ * @dev:	Pointer to the device structure
+ * @gts:	IIO Gain Time Scale structure
+ * @mutex:	Lock for protecting register access, adc reads and power

ADC. I guess the double comment is to keep checkpatch happy?

Just ignore it being dumb as you have a comment up here and put all the info here rather than splitting it up like this.

Quoted 49 lines

+ * @regmap: Regmap structure pointer
+ * @regfield_sw_reset: Reg: MAIN_CTRL, Field: SW_Reset
+ * @regfield_en: Reg: MAIN_CTRL, Field: ALS_EN
+ * @regfield_intg_time: Reg: ALS_MEAS_RATE, Field: ALS Resolution/Bit Width
+ * @regfield_repeat_rate: Reg: ALS_MEAS_RATE, Field: ALS Measurement Rate
+ * @regfield_gain: Reg: ALS_GAIN, Field: ALS Gain Range
+ * @regfield_int_src: Reg: INT_CFG, Field: ALS Interrupt Source
+ * @regfield_int_thresh_var_en: Reg: INT_CFG, Field: ALS Var Interrupt Mode
+ * @regfield_int_en: Reg: INT_CFG, Field: ALS Interrupt Enable
+ * @regfield_int_persist_val: Reg: INT_PERSISTENCE, Field: ALS_PERSIST
+ * @regfield_int_thresh_var_val: Reg: ALS_THRSH_VAR, Field: ALS_THRES_VAR
+ * @nlux_per_count: nano lux per ADC count for a particular model
+ * @read_data_available: Flag set by IRQ handler for ADC data available
+ */

drivers/iio/light/apds9306.c new file +34 -0
+struct apds9306_data {
+	struct device *dev;
+	struct iio_gts gts;
+	/*
+	 * Protects device settings changes where some calculations are required
+	 * before or after setting or getting the raw settings values from regmap
+	 * writes or reads respectively.
+	 */
+	struct mutex mutex;
+
+	struct regmap *regmap;
+	struct regmap_field *regfield_sw_reset;
+	struct regmap_field *regfield_en;
+	struct regmap_field *regfield_intg_time;
+	struct regmap_field *regfield_repeat_rate;
+	struct regmap_field *regfield_gain;
+	struct regmap_field *regfield_int_src;
+	struct regmap_field *regfield_int_thresh_var_en;
+	struct regmap_field *regfield_int_en;
+	struct regmap_field *regfield_int_persist_val;
+	struct regmap_field *regfield_int_thresh_var_val;
+
+	int nlux_per_count;
+	int read_data_available;
+};
 
+
+static struct iio_event_spec apds9306_event_spec_als[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,

drivers/iio/light/apds9306.c line 234

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+	}, {
+		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+			BIT(IIO_EV_INFO_ENABLE),
+	}, {
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),

What's the intent of this final entry?
The type will default to IIO_EV_TYPE_THRESH anyway but if that
the intent you should specify it. There isn't an 'obvious'
default for type in the same way there sort of is for dir (as it's either direction).

Quoted 14 lines
drivers/iio/light/apds9306.c new file +12 -0
+	},
+};
 
+
 
+
+static int apds9306_runtime_power_on(struct device *dev)
+{
+	int ret;
+
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
+		dev_err(dev, "runtime resume failed: %d\n", ret);
+

drivers/iio/light/apds9306.c line 478

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	return ret;
+}
+
+static int apds9306_runtime_power_off(struct device *dev)
+{
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
+}

I'm not entirely convinced these two wrappers are worthwhile given they aren't that common used and locally obscure what is going on when it could be apparent at the few call sites.

drivers/iio/light/apds9306.c line 1025

Quoted 13 lines
drivers/iio/light/apds9306.c new file +13 -0
+static int apds9306_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 apds9306_data *data = iio_priv(indio_dev);
+	int int_en, int_ch, ret;
+
+	guard(mutex)(&data->mutex);
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		ret = regmap_field_read(data->regfield_int_src, &int_ch);

int_ch is a not particularly informative name.

event_ch_is_light perhaps?

Quoted 38 lines
drivers/iio/light/apds9306.c new file +38 -0
+		if (ret)
+			return ret;
+		ret = regmap_field_read(data->regfield_int_en, &int_en);
+		if (ret)
+			return ret;
+		if (chan->type == IIO_LIGHT)
+			return int_en & int_ch;
+		else if (chan->type == IIO_INTENSITY)
+			return int_en & !int_ch;
+		return -EINVAL;
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		ret = regmap_field_read(data->regfield_int_thresh_var_en, &int_en);
+		if (ret)
+			return ret;
+		return int_en;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int apds9306_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 apds9306_data *data = iio_priv(indio_dev);
+	int ret, val;
+
+	state = !!state;
+
+	guard(mutex)(&data->mutex);
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		/*
+		 * If interrupt is enabled, the channel is set before enabling
+		 * the interrupt. In case of disable, no need to switch

drivers/iio/light/apds9306.c line 1073

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		 * channels. In case of different channel is selected while
+		 * interrupt in on, just change the channel.
+		 */
+		if (state) {
+			if (chan->type == IIO_LIGHT)
+				val = 1;
+			else if (chan->type == IIO_INTENSITY)
+				val = 0;
+			else
+				return -EINVAL;

Blank line here and similar.

drivers/iio/light/apds9306.c line 1082

Quoted 9 lines
drivers/iio/light/apds9306.c new file +9 -0
+			ret = regmap_field_write(data->regfield_int_src, val);
+			if (ret)
+				return ret;
+		}
+		ret = regmap_field_read(data->regfield_int_en, &val);
+		if (ret)
+			return ret;
+		if (val == state)
+			return 0;

Blank line. Basically add one whenever a block of related code ends.

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -0
+		ret = regmap_field_write(data->regfield_int_en, state);
+		if (ret)
+			return ret;
+		if (state)
+			return apds9306_runtime_power_on(data->dev);
+		return apds9306_runtime_power_off(data->dev);
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		return regmap_field_write(data->regfield_int_thresh_var_en, state);
+	default:
+		return -EINVAL;
+	}
+}

..

drivers/iio/light/apds9306.c line 1143

Quoted 8 lines
drivers/iio/light/apds9306.c new file +8 -0
+static void apds9306_powerdown(void *ptr)
+{
+	struct apds9306_data *data = (struct apds9306_data *)ptr;
+	int ret;
+
+	ret = regmap_field_write(data->regfield_int_thresh_var_en, 0);
+	if (ret)
+		return;

blank line here ideally.

Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+	ret = regmap_field_write(data->regfield_int_en, 0);
+	if (ret)
+		return;
+
+	apds9306_power_state(data, false);
+}

...

Quoted 57 lines
drivers/iio/light/apds9306.c new file +57 -0
+
+static int apds9306_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct apds9306_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+
+	mutex_init(&data->mutex);
+
+	data->regmap = devm_regmap_init_i2c(client, &apds9306_regmap);
+	if (IS_ERR(data->regmap))
+		return dev_err_probe(dev, PTR_ERR(data->regmap),
+				     "regmap initialization failed\n");
+
+	data->dev = dev;
+	i2c_set_clientdata(client, indio_dev);
+
+	ret = apds9306_regfield_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "regfield initialization failed\n");
+
+	ret = devm_regulator_get_enable(dev, "vdd");
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable regulator\n");
+
+	indio_dev->name = "apds9306";
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	if (client->irq) {
+		indio_dev->info = &apds9306_info;
+		indio_dev->channels = apds9306_channels_with_events;
+		indio_dev->num_channels =
+				ARRAY_SIZE(apds9306_channels_with_events);
+		ret = devm_request_threaded_irq(dev, client->irq, NULL,
+				apds9306_irq_handler, IRQF_ONESHOT,
+					"apds9306_event", indio_dev);
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to assign interrupt.\n");
+	} else {
+		indio_dev->info = &apds9306_info_no_events;
+		indio_dev->channels = apds9306_channels_without_events;
+		indio_dev->num_channels = ARRAY_SIZE(apds9306_channels_without_events);
+	}
+
+	ret = apds9306_pm_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed pm init\n");
+
+	ret = apds9306_device_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to init device\n");

drivers/iio/light/apds9306.c line 1272

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+
+	ret = devm_add_action_or_reset(dev, apds9306_powerdown, data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add action or reset\n");
+
+	ret = devm_iio_device_register(dev, indio_dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed iio device registration\n");
+
+	pm_runtime_put_autosuspend(dev);

Where is the matching get? I don't recall any of the pm functions leaving us with the reference count raised except for the where it is called out in the function name.

The runtime pm reference counters are protected against underflowing so this probably just has no impact. Still good to only have it if necessary and if you do need the power to be on until this point, force it to do so by an appropriate pm_runtime_get().

Quoted 3 lines
drivers/iio/light/apds9306.c new file +3 -0
+
+	return 0;
+}
author maintainer
#18 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 17 lines · Jonathan Cameron
Quoted 14 lines · On Sun, 21 Jan 2024 15:47:34 +1030 Subhajit Ghosh…

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

Quoted 6 lines
Quoted 3 lines

---
v2 -> v5:

Why did you jump to v5? Some internal or private reviews perhaps? Better for those tracking on the list if you just used v3.

Wish I had someone to review my code before sending it to kernel community! I do this in my own time.

v5 was suggested by you. Now I understand that Suggested-by: tag has to be used :)
https://lore.kernel.org/all/20231028143631.2545f93e@jic23-huawei/

Quoted 17 lines
Quoted 8 lines

- Removed scale attribute for Intensity channel:

    Link: https://lore.kernel.org/all/20231204095108.22f89718@jic23-huawei/

- Dropped caching of hardware gain, repeat rate and integration time and updated code as per earlier reviews.

...

A few, mostly very minor comments inline to add to Christophe's review.

Thanks,

Jonathan

Quoted 15 lines

drivers/iio/light/apds9306.c line 7

Quoted 13 lines
drivers/iio/light/apds9306.c new file +7 -0
new file mode 100644
index 000000000000..8ed5899050ed
@@ -0,0 +1,1315 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * APDS-9306/APDS-9306-065 Ambient Light Sensor
+ * I2C Address: 0x52
+ * Datasheet: https://docs.broadcom.com/doc/AV02-4755EN
+ *
+ * Copyright (C) 2023 Subhajit Ghosh <[email protected]>

Given you are still changing it, feel free to include 2024!

I sincerely hope that I don't have to update it to 2025!

Quoted 97 lines
Quoted 2 lines
drivers/iio/light/apds9306.c new file +1 -0
+ */

...

drivers/iio/light/apds9306.c line 126

Quoted 8 lines
drivers/iio/light/apds9306.c new file +8 -0
+static const int apds9306_repeat_rate_freq[][2] = {
+	{40, 0},
+	{20, 0},
+	{10, 0},
+	{5,  0},
+	{2,  0},
+	{1,  0},
+	{0, 500000},

Prefer

	{ 40, 0 },

etc and whilst I don't really like forcing alignment like this, if you are going to do it be consistent. The last 50000 is one space too far to the left I think.

Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+};
+static_assert(ARRAY_SIZE(apds9306_repeat_rate_freq) ==
+		APDS9306_NUM_REPEAT_RATES);
+
+static const int apds9306_repeat_rate_period[] = {
+	25000, 50000, 100000, 200000, 500000, 1000000, 2000000,

drivers/iio/light/apds9306.c line 142

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+};
+static_assert(ARRAY_SIZE(apds9306_repeat_rate_period) ==
+		APDS9306_NUM_REPEAT_RATES);
+
+/**
+ * struct apds9306_data - apds9306 private data and registers definitions
+ *
+ * @dev:	Pointer to the device structure
+ * @gts:	IIO Gain Time Scale structure
+ * @mutex:	Lock for protecting register access, adc reads and power

ADC. I guess the double comment is to keep checkpatch happy?

Just ignore it being dumb as you have a comment up here and put all the info here rather than splitting it up like this.

Quoted 57 lines

+ * @regmap: Regmap structure pointer
+ * @regfield_sw_reset: Reg: MAIN_CTRL, Field: SW_Reset
+ * @regfield_en: Reg: MAIN_CTRL, Field: ALS_EN
+ * @regfield_intg_time: Reg: ALS_MEAS_RATE, Field: ALS Resolution/Bit Width
+ * @regfield_repeat_rate: Reg: ALS_MEAS_RATE, Field: ALS Measurement Rate
+ * @regfield_gain: Reg: ALS_GAIN, Field: ALS Gain Range
+ * @regfield_int_src: Reg: INT_CFG, Field: ALS Interrupt Source
+ * @regfield_int_thresh_var_en: Reg: INT_CFG, Field: ALS Var Interrupt Mode
+ * @regfield_int_en: Reg: INT_CFG, Field: ALS Interrupt Enable
+ * @regfield_int_persist_val: Reg: INT_PERSISTENCE, Field: ALS_PERSIST
+ * @regfield_int_thresh_var_val: Reg: ALS_THRSH_VAR, Field: ALS_THRES_VAR
+ * @nlux_per_count: nano lux per ADC count for a particular model
+ * @read_data_available: Flag set by IRQ handler for ADC data available
+ */

drivers/iio/light/apds9306.c new file +42 -0
+struct apds9306_data {
+	struct device *dev;
+	struct iio_gts gts;
+	/*
+	 * Protects device settings changes where some calculations are required
+	 * before or after setting or getting the raw settings values from regmap
+	 * writes or reads respectively.
+	 */
+	struct mutex mutex;
+
+	struct regmap *regmap;
+	struct regmap_field *regfield_sw_reset;
+	struct regmap_field *regfield_en;
+	struct regmap_field *regfield_intg_time;
+	struct regmap_field *regfield_repeat_rate;
+	struct regmap_field *regfield_gain;
+	struct regmap_field *regfield_int_src;
+	struct regmap_field *regfield_int_thresh_var_en;
+	struct regmap_field *regfield_int_en;
+	struct regmap_field *regfield_int_persist_val;
+	struct regmap_field *regfield_int_thresh_var_val;
+
+	int nlux_per_count;
+	int read_data_available;
+};
 
+
+static struct iio_event_spec apds9306_event_spec_als[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+	}, {
+		.type = IIO_EV_TYPE_THRESH,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+	}, {
+		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+			BIT(IIO_EV_INFO_ENABLE),
Quoted 8 lines

drivers/iio/light/apds9306.c

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+	}, {
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),

What's the intent of this final entry?
The type will default to IIO_EV_TYPE_THRESH anyway but if that
the intent you should specify it. There isn't an 'obvious'
default for type in the same way there sort of is for dir (as it's either direction).

Understood, let me experiment and see the ABI difference, if any and get back to you.

Quoted 21 lines
Quoted 21 lines
drivers/iio/light/apds9306.c new file +18 -0
+	},
+};
 
+
 
+
+static int apds9306_runtime_power_on(struct device *dev)
+{
+	int ret;
+
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
+		dev_err(dev, "runtime resume failed: %d\n", ret);
+
+	return ret;
+}
+
+static int apds9306_runtime_power_off(struct device *dev)
+{
+	pm_runtime_mark_last_busy(dev);
Quoted 8 lines

drivers/iio/light/apds9306.c line 478

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
+}

I'm not entirely convinced these two wrappers are worthwhile given they aren't that common used and locally obscure what is going on when it could be apparent at the few call sites.

The above was suggested by Andy.
https://lore.kernel.org/linux-devicetree/[email protected]/

Apologies for my ignorance - "it could be apparent at the few call sites" - I did not understand the above statement. Can you please elaborate?

Quoted 183 lines
Quoted 6 lines
drivers/iio/light/apds9306.c new file +3 -0
+static int apds9306_read_event_config(struct iio_dev *indio_dev,
+				      const struct iio_chan_spec *chan,
+				      enum iio_event_type type,

drivers/iio/light/apds9306.c line 1025

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+				      enum iio_event_direction dir)
+{
+	struct apds9306_data *data = iio_priv(indio_dev);
+	int int_en, int_ch, ret;
+
+	guard(mutex)(&data->mutex);
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		ret = regmap_field_read(data->regfield_int_src, &int_ch);

int_ch is a not particularly informative name.

event_ch_is_light perhaps?

Quoted 38 lines
drivers/iio/light/apds9306.c new file +38 -0
+		if (ret)
+			return ret;
+		ret = regmap_field_read(data->regfield_int_en, &int_en);
+		if (ret)
+			return ret;
+		if (chan->type == IIO_LIGHT)
+			return int_en & int_ch;
+		else if (chan->type == IIO_INTENSITY)
+			return int_en & !int_ch;
+		return -EINVAL;
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		ret = regmap_field_read(data->regfield_int_thresh_var_en, &int_en);
+		if (ret)
+			return ret;
+		return int_en;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int apds9306_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 apds9306_data *data = iio_priv(indio_dev);
+	int ret, val;
+
+	state = !!state;
+
+	guard(mutex)(&data->mutex);
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		/*
+		 * If interrupt is enabled, the channel is set before enabling
+		 * the interrupt. In case of disable, no need to switch

drivers/iio/light/apds9306.c line 1073

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		 * channels. In case of different channel is selected while
+		 * interrupt in on, just change the channel.
+		 */
+		if (state) {
+			if (chan->type == IIO_LIGHT)
+				val = 1;
+			else if (chan->type == IIO_INTENSITY)
+				val = 0;
+			else
+				return -EINVAL;

Blank line here and similar.

drivers/iio/light/apds9306.c line 1082

Quoted 9 lines
drivers/iio/light/apds9306.c new file +9 -0
+			ret = regmap_field_write(data->regfield_int_src, val);
+			if (ret)
+				return ret;
+		}
+		ret = regmap_field_read(data->regfield_int_en, &val);
+		if (ret)
+			return ret;
+		if (val == state)
+			return 0;

Blank line. Basically add one whenever a block of related code ends.

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -0
+		ret = regmap_field_write(data->regfield_int_en, state);
+		if (ret)
+			return ret;
+		if (state)
+			return apds9306_runtime_power_on(data->dev);
+		return apds9306_runtime_power_off(data->dev);
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		return regmap_field_write(data->regfield_int_thresh_var_en, state);
+	default:
+		return -EINVAL;
+	}
+}

..

drivers/iio/light/apds9306.c line 1143

Quoted 8 lines
drivers/iio/light/apds9306.c new file +8 -0
+static void apds9306_powerdown(void *ptr)
+{
+	struct apds9306_data *data = (struct apds9306_data *)ptr;
+	int ret;
+
+	ret = regmap_field_write(data->regfield_int_thresh_var_en, 0);
+	if (ret)
+		return;

blank line here ideally.

Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+	ret = regmap_field_write(data->regfield_int_en, 0);
+	if (ret)
+		return;
+
+	apds9306_power_state(data, false);
+}

...

Quoted 63 lines
drivers/iio/light/apds9306.c new file +63 -0
+
+static int apds9306_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct apds9306_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+
+	mutex_init(&data->mutex);
+
+	data->regmap = devm_regmap_init_i2c(client, &apds9306_regmap);
+	if (IS_ERR(data->regmap))
+		return dev_err_probe(dev, PTR_ERR(data->regmap),
+				     "regmap initialization failed\n");
+
+	data->dev = dev;
+	i2c_set_clientdata(client, indio_dev);
+
+	ret = apds9306_regfield_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "regfield initialization failed\n");
+
+	ret = devm_regulator_get_enable(dev, "vdd");
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable regulator\n");
+
+	indio_dev->name = "apds9306";
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	if (client->irq) {
+		indio_dev->info = &apds9306_info;
+		indio_dev->channels = apds9306_channels_with_events;
+		indio_dev->num_channels =
+				ARRAY_SIZE(apds9306_channels_with_events);
+		ret = devm_request_threaded_irq(dev, client->irq, NULL,
+				apds9306_irq_handler, IRQF_ONESHOT,
+					"apds9306_event", indio_dev);
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to assign interrupt.\n");
+	} else {
+		indio_dev->info = &apds9306_info_no_events;
+		indio_dev->channels = apds9306_channels_without_events;
+		indio_dev->num_channels = ARRAY_SIZE(apds9306_channels_without_events);
+	}
+
+	ret = apds9306_pm_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed pm init\n");
+
+	ret = apds9306_device_init(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to init device\n");
+
+	ret = devm_add_action_or_reset(dev, apds9306_powerdown, data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add action or reset\n");
+
+	ret = devm_iio_device_register(dev, indio_dev);
Quoted 9 lines

drivers/iio/light/apds9306.c line 1272

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "failed iio device registration\n");
+
+	pm_runtime_put_autosuspend(dev);

Where is the matching get? I don't recall any of the pm functions leaving us with the reference count raised except for the where it is called out in the function name.

I blindly copy pasted your below suggestion.
https://lore.kernel.org/all/20231028162025.4259f1cc@jic23-huawei/

"this lot of runtime pm stuff isn't initializing the device, so I don't see it as making sense in here. I'd push it out to the caller with the power up before init and the autosuspend etc after.
I'll note that I'd expect to see a a pm_runtime_put_autosuspend() at the end of probe to put device to sleep soon after loading."

Quoted 4 lines

The runtime pm reference counters are protected against underflowing so this probably just has no impact. Still good to only have it if necessary and if you do need the power to be on until this point, force it to do so by an appropriate pm_runtime_get().

I will use a pm_runtime_get() in the apds9306_pm_init() function above.

Quoted 6 lines

drivers/iio/light/apds9306.c line 442

Quoted 5 lines
drivers/iio/light/apds9306.c new file +3 -0
+
+	return 0;
+}

Thank you for your review.

Regards,
Subhajit Ghosh

#19 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 22 lines · On Mon, 22 Jan 2024 21:26:03 +1030 Subhajit Ghosh…
Quoted 21 lines · Jonathan Cameron
Quoted 17 lines · On Sun, 21 Jan 2024 15:47:34 +1030 Subhajit Ghosh…

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. It has two channels - ALS and CLEAR. The ALS (Ambient Light Sensor) channel approximates the response of the human-eye providing direct read out where the output count is proportional to ambient light levels. It is internally temperature compensated and rejects 50Hz and 60Hz flicker caused by artificial light sources. Hardware interrupt configuration is optional. It is a low power device with 20 bit resolution and has configurable adaptive interrupt mode and interrupt persistence mode. The device also features inbuilt hardware gain, multiple integration time selection options and sampling frequency selection options.

This driver also uses the IIO GTS (Gain Time Scale) Helpers Namespace for Scales, Gains and Integration time implementation.

---
v2 -> v5:

Quoted 7 lines
Quoted 2 lines

Why did you jump to v5? Some internal or private reviews perhaps? Better for those tracking on the list if you just used v3.

Wish I had someone to review my code before sending it to kernel community! I do this in my own time.

v5 was suggested by you. Now I understand that Suggested-by: tag has to be used :)
https://lore.kernel.org/all/20231028143631.2545f93e@jic23-huawei/

oops :) It's fine, I'd just forgotten that discussion entirely!

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/light/apds9306.c new file +1 -0
new file mode 100644
index 000000000000..8ed5899050ed
@@ -0,0 +1,1315 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
Quoted 8 lines

drivers/iio/light/apds9306.c line 7

Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+/*
+ * APDS-9306/APDS-9306-065 Ambient Light Sensor
+ * I2C Address: 0x52
+ * Datasheet: https://docs.broadcom.com/doc/AV02-4755EN
+ *
+ * Copyright (C) 2023 Subhajit Ghosh <[email protected]>  

Given you are still changing it, feel free to include 2024!

Quoted 1 line

I sincerely hope that I don't have to update it to 2025!

:)

...

Quoted 29 lines
Quoted 21 lines
Quoted 21 lines
drivers/iio/light/apds9306.c new file +18 -0
+	},
+};  
 
+  
 
+
+static int apds9306_runtime_power_on(struct device *dev)
+{
+	int ret;
+
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
+		dev_err(dev, "runtime resume failed: %d\n", ret);
+
+	return ret;
+}
+
+static int apds9306_runtime_power_off(struct device *dev)
+{
+	pm_runtime_mark_last_busy(dev);
Quoted 8 lines

drivers/iio/light/apds9306.c line 478

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
+}  

I'm not entirely convinced these two wrappers are worthwhile given they aren't that common used and locally obscure what is going on when it could be apparent at the few call sites.

Quoted 2 lines

The above was suggested by Andy.
https://lore.kernel.org/linux-devicetree/[email protected]/

Ah. Fair enough. I'm not that bothered so if Andy prefers this then fine to keep it.

Quoted 3 lines

Apologies for my ignorance - "it could be apparent at the few call sites" - I did not understand the above statement. Can you please elaborate?

I meant what was going on (the fact it's using autosuspend for example) could be visible where it is called in a way that is hidden by the wrappers. Not a big thing though.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/iio/light/apds9306.c new file +6 -0
+
+	ret = devm_add_action_or_reset(dev, apds9306_powerdown, data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add action or reset\n");
+
+	ret = devm_iio_device_register(dev, indio_dev);
Quoted 9 lines

drivers/iio/light/apds9306.c line 1272

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "failed iio device registration\n");
+
+	pm_runtime_put_autosuspend(dev);  

Where is the matching get? I don't recall any of the pm functions leaving us with the reference count raised except for the where it is called out in the function name.

Quoted 8 lines

I blindly copy pasted your below suggestion.
https://lore.kernel.org/all/20231028162025.4259f1cc@jic23-huawei/

"this lot of runtime pm stuff isn't initializing the device, so I don't see it as making sense in here. I'd push it out to the caller with the power up before init and the autosuspend etc after.
I'll note that I'd expect to see a a pm_runtime_put_autosuspend() at the end of probe to put device to sleep soon after loading."

Ah. Seems I'm being very inconsistent today. However I would expect you to need the device to remain powered up until end of probe (assuming it needs to be powered up to read/write registers?) so indeed a get to raise the reference count would be the way to ensure that.

Quoted 6 lines
Quoted 5 lines

The runtime pm reference counters are protected against underflowing so this probably just has no impact. Still good to only have it if necessary and if you do need the power to be on until this point, force it to do so by an appropriate pm_runtime_get().

I will use a pm_runtime_get() in the apds9306_pm_init() function above.

Great. That makes sense.

Quoted 7 lines
Quoted 6 lines

drivers/iio/light/apds9306.c line 442

Quoted 5 lines
drivers/iio/light/apds9306.c new file +3 -0
+
+	return 0;
+}  

Thank you for your review.

Sorry for the inconsistencies!

Jonathan

Quoted 4 lines

Regards,
Subhajit Ghosh

author maintainer
#20 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 7 lines · Jonathan Cameron
Quoted 2 lines
Quoted 1 line

Thank you for your review.

Sorry for the inconsistencies!

Jonathan

Quoted 1 line

No worries. I owe an an apology for sending patches almost 3 months apart, as a consequence these things are bound to happen. I can't keep track of things which happened 3 weeks back and I don't even deal with these much code! Regards,
Subhajit Ghosh

author maintainer
#21 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

In reply to Subhajit Ghosh

Hi Jonathan,

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/light/apds9306.c new file +17 -0
+
+static struct iio_event_spec apds9306_event_spec_als[] = {
+    {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_RISING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_FALLING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+    }, {
+        .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+            BIT(IIO_EV_INFO_ENABLE),
Quoted 8 lines
Quoted 2 lines

+    }, {
+        .mask_separate = BIT(IIO_EV_INFO_ENABLE),

What's the intent of this final entry?
The type will default to IIO_EV_TYPE_THRESH anyway but if that the intent you should specify it.   There isn't an 'obvious' default for type in the same way there sort of is for dir (as it's either direction).

Quoted 2 lines

Understood, let me experiment and see the ABI difference, if any and get back to you.

This device has two channels - ALS and CLEAR. One interrupt enable option and one Channel selection option (Clear or ALS). According to our previous discussions:
https://lore.kernel.org/all/20230415183543.6d5e3392@jic23-huawei/
the event_spec was updated to have two interrupt enable attributes - one for CLEAR and one for ALS. (Intensity channel and Illuminance channel)

If I remove the final entry I am getting only one enable option (intensity channel):
/sys/bus/iio/devices/iio:device0/

|-- events
|   |-- in_intensity_clear_thresh_either_en
|   |-- thresh_adaptive_either_en
|   |-- thresh_adaptive_either_value
|   |-- thresh_adaptive_either_values_available
|   |-- thresh_either_period
|   |-- thresh_either_period_available
|   |-- thresh_falling_value
|   `-- thresh_rising_value

The last entry gives be the following event attributes, enable attributes for both intensity and illuminance channels:
/sys/bus/iio/devices/iio:device0/

|-- events
|   |-- in_illuminance_thresh_either_en
|   |-- in_intensity_clear_thresh_either_en
|   |-- thresh_adaptive_either_en
|   |-- thresh_adaptive_either_value
|   |-- thresh_adaptive_either_values_available
|   |-- thresh_either_period
|   |-- thresh_either_period_available
|   |-- thresh_falling_value
|   `-- thresh_rising_value

Please let me know if this sounds ok to you.

Regards,
Subhajit Ghosh

maintainer
#22 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 61 lines · On Sun, 4 Feb 2024 21:53:55 +1030 Subhajit Ghosh…

Hi Jonathan,

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/light/apds9306.c new file +17 -0
+
+static struct iio_event_spec apds9306_event_spec_als[] = {
+    {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_RISING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_FALLING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+    }, {
+        .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+            BIT(IIO_EV_INFO_ENABLE),
Quoted 8 lines
Quoted 2 lines

+    }, {
+        .mask_separate = BIT(IIO_EV_INFO_ENABLE),

What's the intent of this final entry?
The type will default to IIO_EV_TYPE_THRESH anyway but if that the intent you should specify it.   There isn't an 'obvious' default for type in the same way there sort of is for dir (as it's either direction).

Quoted 2 lines

Understood, let me experiment and see the ABI difference, if any and get back to you.

This device has two channels - ALS and CLEAR. One interrupt enable option and one Channel selection option (Clear or ALS). According to our previous discussions:
https://lore.kernel.org/all/20230415183543.6d5e3392@jic23-huawei/
the event_spec was updated to have two interrupt enable attributes - one for CLEAR and one for ALS. (Intensity channel and Illuminance channel)

If I remove the final entry I am getting only one enable option (intensity channel):
/sys/bus/iio/devices/iio:device0/

|-- events
|   |-- in_intensity_clear_thresh_either_en
|   |-- thresh_adaptive_either_en
|   |-- thresh_adaptive_either_value
|   |-- thresh_adaptive_either_values_available
|   |-- thresh_either_period
|   |-- thresh_either_period_available
|   |-- thresh_falling_value
|   `-- thresh_rising_value

The last entry gives be the following event attributes, enable attributes for both intensity and illuminance channels:
/sys/bus/iio/devices/iio:device0/

|-- events
|   |-- in_illuminance_thresh_either_en
|   |-- in_intensity_clear_thresh_either_en
|   |-- thresh_adaptive_either_en
|   |-- thresh_adaptive_either_value
|   |-- thresh_adaptive_either_values_available
|   |-- thresh_either_period
|   |-- thresh_either_period_available
|   |-- thresh_falling_value
|   `-- thresh_rising_value
Quoted 1 line

Please let me know if this sounds ok to you.

Looks like coincidence of enum values being 0.
It's really

{
	.type = IIO_EV_TYPE_THRESH, /* Value 0 */
	.dir = IIO_EV_DIR_EITHER, /* value 0 */
	.mask_separate = BIT(IIO_EV_INFO_ENABLE),

Dropping 'defaults' for these things is fine if they are the obvious default or other parameters mean they aren't used, but that isn't the case here so please be explicit for all the values that are used.

You can put this final mask a few lines earlier as the other fields match anyway.

	{
        	.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
        	.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
	},..
Quoted 3 lines

Regards,
Subhajit Ghosh

author maintainer
#23 lore raw

Re: [PATCH v5 3/3] iio: light: Add support for APDS9306 Light Sensor

Quoted 38 lines · Jonathan Cameron
Quoted 35 lines · On Sun, 4 Feb 2024 21:53:55 +1030 Subhajit Ghosh…

Hi Jonathan,

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/light/apds9306.c new file +17 -0
+
+static struct iio_event_spec apds9306_event_spec_als[] = {
+    {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_RISING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .dir = IIO_EV_DIR_FALLING,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
+    }, {
+        .type = IIO_EV_TYPE_THRESH,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+    }, {
+        .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+        .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+            BIT(IIO_EV_INFO_ENABLE),
Quoted 8 lines
Quoted 2 lines

+    }, {
+        .mask_separate = BIT(IIO_EV_INFO_ENABLE),

What's the intent of this final entry?
The type will default to IIO_EV_TYPE_THRESH anyway but if that the intent you should specify it.   There isn't an 'obvious' default for type in the same way there sort of is for dir (as it's either direction).

Quoted 2 lines

Understood, let me experiment and see the ABI difference, if any and get back to you.

This device has two channels - ALS and CLEAR. One interrupt enable option and one Channel selection option (Clear or ALS). According to our previous discussions:
https://lore.kernel.org/all/20230415183543.6d5e3392@jic23-huawei/
the event_spec was updated to have two interrupt enable attributes - one for CLEAR and one for ALS. (Intensity channel and Illuminance channel)

...

Quoted 13 lines
Quoted 2 lines

Please let me know if this sounds ok to you.

Looks like coincidence of enum values being 0.
It's really

{
	.type = IIO_EV_TYPE_THRESH, /* Value 0 */
	.dir = IIO_EV_DIR_EITHER, /* value 0 */
	.mask_separate = BIT(IIO_EV_INFO_ENABLE),

Dropping 'defaults' for these things is fine if they are the obvious default or other parameters mean they aren't used, but that isn't the case here so please be explicit for all the values that are used.

Quoted 9 lines

You can put this final mask a few lines earlier as the other fields match anyway.

	{
         	.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
         	.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
	},..

Thank you Jonathan. The above works as intended.

Regards,
Subhajit Ghosh