Skip to contentSkip to conversation

Support for Avago APDS9306 Ambient Light Sensor

PATCH Superseded Subhajit Ghosh 3 participants 15 messages Feb 18, 2024 – Feb 27, 2024

Subsystems (3)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Subhajit Ghosh

author maintainer
#0 lore raw

v7 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.

v6 -> v7:
- Made comments to struct part_id_gts_multiplier as kernel doc
- Removed static_asserts for array sizes
- Moved regmap_field from driver private data structure and removed regfield_ prefix to reduce names
- Used "struct apds9306_regfields *rf = &data->rf" in the respective functions to reduce names
- Removed apds9306_runtime_power_on() and apds9306_runtime_power_off() functions in favour of using the runtime_pm calls directly from calling functions.
- Fixed indentations
https://lore.kernel.org/all/[email protected]/#r

v6 -> v7 Bindings:
- Updated commit message
- Removed wrong patch dependency statement from commit messages
- Updates tags
https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Changes as per review
- Update kernel doc for private data
- Change IIO Event Spec definitions
- Update guard mutex lock implementation
- Add pm_runtime_get()
- Update styling

v5 -> v6 Bindings:
- Write proper commit messages
- Add vdd-supply in a separate commit
- Add Interrupt macro in a separate commit

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 (5):
dt-bindings: iio: light: Merge APDS9300 and APDS9960 schemas
dt-bindings: iio: light: adps9300: Add missing vdd-supply
dt-bindings: iio: light: adps9300: Update interrupt definitions
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                  | 1336 +++++++++++++++++
 5 files changed, 1364 insertions(+), 49 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
 create mode 100644 drivers/iio/light/apds9306.c

base-commit: c1ca10ceffbb289ed02feaf005bc9ee6095b4507

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v7 1/5 dt-bindings: iio: light: Merge APDS9300 and APDS9960 schemas

Merge very similar schemas for APDS9300 and APDS9960.

---
v6 -> v7:
- No change
v5 -> v6:
- Write proper commit messages

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
author maintainer
#2 lore raw

PATCH v7 2/5 dt-bindings: iio: light: adps9300: Add missing vdd-supply

In reply to Subhajit Ghosh

All devices covered by the binding have a vdd supply.

---
v6 -> v7:
- Changed the subject line of the commit
- Updated commit message
- Removed wrong patch dependency statement
- Added tag
https://lore.kernel.org/all/20240210170112.6528a3d4@jic23-huawei/
https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Separate commit for individual change as per below review:

---
 .../devicetree/bindings/iio/light/avago,apds9300.yaml          | 3 +++
 1 file changed, 3 insertions(+)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +3 -0
index c610780346e8..a328c8a1daef 100644
@@ -25,6 +25,8 @@ properties:
   interrupts:
     maxItems: 1
 
+  vdd-supply: true
+
 additionalProperties: false
 
 required:
@@ -42,6 +44,7 @@ examples:
             reg = <0x39>;
             interrupt-parent = <&gpio2>;
             interrupts = <29 8>;
+            vdd-supply = <&regulator_3v3>;
         };
     };
 ...
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v7 3/5 dt-bindings: iio: light: adps9300: Update interrupt definitions

In reply to Subhajit Ghosh

Include irq.h and irq level macro in the example for readability

---
v6 -> v7:
- Removed wrong patch dependency statement
- Added tag
https://lore.kernel.org/all/20240210170258.17fd1099@jic23-huawei/
https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Separate commit for individual change as per below review:

---
 .../devicetree/bindings/iio/light/avago,apds9300.yaml         | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +3 -1
index a328c8a1daef..e07a074f6acf 100644
@@ -35,6 +35,8 @@ required:
 
 examples:
   - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;
@@ -43,7 +45,7 @@ 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
author maintainer
#4 lore raw

PATCH v6 4/5 dt-bindings: iio: light: Avago APDS9306

In reply to Subhajit Ghosh

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. Extend avago,apds9300.yaml schema file to support apds9306 device.

---
v6 -> v7:
- Removed wrong patch dependency statement
- Added tag
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Write proper commit messages
- Add vdd-supply in a separate commit
- Add Interrupt macro in a separate commit

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

---
 Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +2 -0
index e07a074f6acf..b750096530bc 100644
@@ -12,11 +12,13 @@ maintainers:
 description: |
   Datasheet: https://www.avagotech.com/docs/AV02-1077EN
   Datasheet: https://www.avagotech.com/docs/AV02-4191EN
+  Datasheet: https://www.avagotech.com/docs/AV02-4755EN
 
 properties:
   compatible:
     enum:
       - avago,apds9300
+      - avago,apds9306
       - avago,apds9960
 
   reg:
Signature · 1 line
2.34.1
author maintainer
#5 lore raw

Re: [PATCH v6 4/5] dt-bindings: iio: light: Avago APDS9306

Quoted 35 lines · Subhajit Ghosh

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor. Extend avago,apds9300.yaml schema file to support apds9306 device.

---
v6 -> v7:
- Removed wrong patch dependency statement
- Added tag

    https://lore.kernel.org/all/[email protected]/
    https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Write proper commit messages
- Add vdd-supply in a separate commit
- Add Interrupt macro in a separate commit

    Link: https://lore.kernel.org/all/[email protected]/

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]/
---
  Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml | 2 ++
  1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +0 -0
index e07a074f6acf..b750096530bc 100644
@@ -12,11 +12,13 @@ maintainers:
  description: |
    Datasheet: https://www.avagotech.com/docs/AV02-1077EN
    Datasheet: https://www.avagotech.com/docs/AV02-4191EN

Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml maintainers: · line 24

Quoted 10 lines
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +2 -0
+  Datasheet: https://www.avagotech.com/docs/AV02-4755EN
 
  properties:
    compatible:
      enum:
        - avago,apds9300
+      - avago,apds9306
        - avago,apds9960
 
    reg:

Hi,

This is actually [PATCH v7 4/5]. I made a copy pasting error in the subject line of this patch while adding the recipients!
Very sorry about that.

Regards,
Subhajit Ghosh

maintainer
#6 lore raw

Re: [PATCH v6 4/5] dt-bindings: iio: light: Avago APDS9306

In reply to Subhajit Ghosh
Quoted 1 line · On Sun, 18 Feb 2024 16:18:25 +1030 Subhajit Ghosh…

Driver support for Avago (Broadcom) APDS9306 Ambient Light Sensor.

I deleted this first line of the patch description. Doesn't seem related to the binding doc change.

Quoted 44 lines

Extend avago,apds9300.yaml schema file to support apds9306 device.

---
v6 -> v7:
- Removed wrong patch dependency statement
- Added tag
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/20240206-gambling-tricycle-510794e20ca8@spud/

v5 -> v6:
- Write proper commit messages
- Add vdd-supply in a separate commit
- Add Interrupt macro in a separate commit

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]/
---
 Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml +2 -0
index e07a074f6acf..b750096530bc 100644
@@ -12,11 +12,13 @@ maintainers:
 description: |
   Datasheet: https://www.avagotech.com/docs/AV02-1077EN
   Datasheet: https://www.avagotech.com/docs/AV02-4191EN
+  Datasheet: https://www.avagotech.com/docs/AV02-4755EN
 
 properties:
   compatible:
     enum:
       - avago,apds9300
+      - avago,apds9306
       - avago,apds9960
 
   reg:
author maintainer
#7 lore raw

PATCH v7 5/5 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.

---
v6 -> v7:
- Made comments to struct part_id_gts_multiplier as kernel doc
- Removed static_asserts for array sizes
- Moved regmap_field types from driver private data structure to a new structure and removed regfield_ prefix to reduce names
- Used "struct apds9306_regfields *rf = &data->rf" in the respective functions to reduce names
- Removed apds9306_runtime_power_on() and apds9306_runtime_power_off() functions in favour of using the runtime_pm calls directly from calling functions.
- Fixed indentations
https://lore.kernel.org/all/[email protected]/#r

v5 -> v6:
- Changes as per review
- Update kernel doc for private data
- Change IIO Event Spec definitions
- Update guard mutex lock implementation
- Add pm_runtime_get()
- Update styling

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 | 1336 ++++++++++++++++++++++++++++++++++
 3 files changed, 1349 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 +1336 -0

1,336 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

Quoted 11 lines · On Sun, 18 Feb 2024 16:18:26 +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 4 lines

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

I applied this but then got some build warnings that made me look more closely at the int_src handling.

This is confusing because of the less than helpful datasheet defintion of a 2 bit register that takes values 0 and 1 only.

I thought about trying to fix this up whilst applying but the event code issue is too significant to do without a means to test it.

Jonathan

Quoted 7 lines
drivers/iio/light/apds9306.c new file +0 -0
new file mode 100644
index 000000000000..c18e0d48556b
@@ -0,0 +1,1336 @@

....

Quoted 15 lines
drivers/iio/light/apds9306.c new file +15 -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,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),

drivers/iio/light/apds9306.c line 244

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	}, {
+		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,
+		.mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
+			BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
+static struct iio_event_spec apds9306_event_spec_clear[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,

Specify dir.

drivers/iio/light/apds9306.c line 249

Quoted 5 lines
drivers/iio/light/apds9306.c new file +5 -0
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
+#define APDS9306_CHANNEL(_type) \

I think the code would be easier to read without this macro. It will be a few lines longer but simpler

Quoted 33 lines
drivers/iio/light/apds9306.c new file +33 -0
+	.type = _type, \
+	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | \
+		BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+	.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | \
+		BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+
+static struct iio_chan_spec apds9306_channels_with_events[] = {
+	{
+		APDS9306_CHANNEL(IIO_LIGHT)
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+		.event_spec = apds9306_event_spec_als,
+		.num_event_specs = ARRAY_SIZE(apds9306_event_spec_als),
+	}, {
+		APDS9306_CHANNEL(IIO_INTENSITY)
+		.channel2 = IIO_MOD_LIGHT_CLEAR,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.modified = 1,
+		.event_spec = apds9306_event_spec_clear,
+		.num_event_specs = ARRAY_SIZE(apds9306_event_spec_clear),
+	},
+};
+
+static struct iio_chan_spec apds9306_channels_without_events[] = {
+	{
+		APDS9306_CHANNEL(IIO_LIGHT)
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+	}, {
+		APDS9306_CHANNEL(IIO_INTENSITY)
+		.channel2 = IIO_MOD_LIGHT_CLEAR,

drivers/iio/light/apds9306.c line 292

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.modified = 1,
+	},
+};
+
+/* INT_PERSISTENCE available */
+IIO_CONST_ATTR(thresh_either_period_available, "[0 1 15]");
+
+/* ALS_THRESH_VAR available */
+IIO_CONST_ATTR(thresh_adaptive_either_values_available, "[0 1 7]");

These need to be static

Quoted 54 lines
drivers/iio/light/apds9306.c new file +53 -0
+
+static struct attribute *apds9306_event_attributes[] = {
+	&iio_const_attr_thresh_either_period_available.dev_attr.attr,
+	&iio_const_attr_thresh_adaptive_either_values_available.dev_attr.attr,
+	NULL
+};
 
+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);
+	struct apds9306_regfields *rf = &data->rf;
+	int ret, delay, intg_time, intg_time_idx, repeat_rate_idx, int_src;
+	int status = 0;
+	u8 buff[3];
+
+	ret = pm_runtime_resume_and_get(data->dev);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->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)
+		return intg_time;
+
+	/* 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, data->read_data_available ||
+				       (status & (APDS9306_ALS_DATA_STAT_MASK |

drivers/iio/light/apds9306.c

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+						  APDS9306_ALS_INT_STAT_MASK)),
+				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
+
+	if (ret)
+		return ret;
+
+	/* 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),

You are pushing an event on channel 0 or 1 (which is non obvious as that int_src is a 2 bit value again). However you don't use indexed channels so this is wrong.
It's also pushing IIO_LIGHT for both channels which makes no sense as you only have one IIO_LIGHT channel.

Quoted 16 lines
drivers/iio/light/apds9306.c new file +16 -0
+			       iio_get_time_ns(indio_dev));
+
+	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
+	if (ret) {
+		dev_err_ratelimited(dev, "read data failed\n");
+		return ret;
+	}
+
+	*val = get_unaligned_le24(&buff);
+
+	pm_runtime_mark_last_busy(data->dev);
+	pm_runtime_put_autosuspend(data->dev);
+
+	return 0;
+}
+

...

Quoted 17 lines
drivers/iio/light/apds9306.c new file +17 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	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_ratelimited(data->dev, "status reg read failed\n");
+		return IRQ_HANDLED;

drivers/iio/light/apds9306.c line 944

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	}
+
+	ret = regmap_field_read(rf->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));

As commented on elsewhere I'm not seeing the relationship between the event pushed here and the channels this device provides (one of which is modified for starters).

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;
+}

...

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -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)

drivers/iio/light/apds9306.c line 1024

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

Call the local value int_src - it's not obvious to a reviewer what relationship between that and int_src is. I had to go read the datasheet to find out.

drivers/iio/light/apds9306.c line 1036

Quoted 12 lines
drivers/iio/light/apds9306.c new file +12 -0
+		if (ret)
+			return ret;
+
+		ret = regmap_field_read(rf->int_en, &int_en);
+		if (ret)
+			return ret;
+
+		if (chan->type == IIO_LIGHT)
+			return int_en & event_ch_is_light;
+
+		if (chan->type == IIO_INTENSITY)
+			return int_en & !event_ch_is_light;

This is the specific line the compiler doesn't like drivers/iio/light/apds9306.c:1036:39: warning: dubious: x & !y

I would match int_src against specific values rather than using tricks based on what those values happen to be.

			return int_en && (int_src == APDS9306_INT_SRC_CLEAR);
Quoted 18 lines
drivers/iio/light/apds9306.c new file +18 -0
+
+		return -EINVAL;
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		ret = regmap_field_read(rf->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)

drivers/iio/light/apds9306.c line 1064

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+{
+	struct apds9306_data *data = iio_priv(indio_dev);
+	struct apds9306_regfields *rf = &data->rf;
+	int ret, val;
+
+	state = !!state;
+
+	switch (type) {
+	case IIO_EV_TYPE_THRESH:
+		guard(mutex)(&data->mutex);

Better to add explicit scope if you are going to use a guard.

	case IIO_EV_TYPE_THRESH: {
		guard(mutex)(&data->mutex);

so we can see where the scope ends more clearly.

Quoted 44 lines
drivers/iio/light/apds9306.c new file +44 -0
+
+		/*
+		 * If interrupt is enabled, the channel is set before enabling
+		 * the interrupt. In case of disable, no need to switch
+		 * 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;
+
+			ret = regmap_field_write(rf->int_src, val);
+			if (ret)
+				return ret;
+		}
+
+		ret = regmap_field_read(rf->int_en, &val);
+		if (ret)
+			return ret;
+
+		if (val == state)
+			return 0;
+
+		ret = regmap_field_write(rf->int_en, state);
+		if (ret)
+			return ret;
+
+		if (state)
+			return pm_runtime_resume_and_get(data->dev);
+
+		pm_runtime_mark_last_busy(data->dev);
+		pm_runtime_put_autosuspend(data->dev);
+
+		return 0;
+	case IIO_EV_TYPE_THRESH_ADAPTIVE:
+		return regmap_field_write(rf->int_thresh_var_en, state);
+	default:
+		return -EINVAL;
+	}
+}
author maintainer
#9 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

Quoted 21 lines · Jonathan Cameron
Quoted 11 lines · On Sun, 18 Feb 2024 16:18:26 +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 4 lines

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

I applied this but then got some build warnings that made me look more closely at the int_src handling.

Quoted 7 lines

This is confusing because of the less than helpful datasheet defintion of a 2 bit register that takes values 0 and 1 only.

I thought about trying to fix this up whilst applying but the event code issue is too significant to do without a means to test it.

Jonathan

Appreciate that you tried to fix all the issues for me.

Quoted 1 line

...

Quoted 25 lines

...

Quoted 23 lines
drivers/iio/light/apds9306.c new file +23 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	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_ratelimited(data->dev, "status reg read failed\n");
+		return IRQ_HANDLED;
+	}
+
+	ret = regmap_field_read(rf->int_src, &int_ch);
+	if (ret)
+		return ret;
+
Quoted 8 lines

drivers/iio/light/apds9306.c line 944

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	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));

As commented on elsewhere I'm not seeing the relationship between the event pushed here and the channels this device provides (one of which is modified for starters).

Yes, I will check which interrupt channel is enabled then push appropriate event. Earlier versions wrongly had both channels as IIO_LIGHT which were fixed in later revisions. I forgot to change the event part!

Quoted 26 lines
Quoted 13 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;
+}

...

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	int int_en, event_ch_is_light, ret;
+
+	switch (type) {
Quoted 8 lines

drivers/iio/light/apds9306.c line 1024

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	case IIO_EV_TYPE_THRESH:
+		guard(mutex)(&data->mutex);
+
+		ret = regmap_field_read(rf->int_src, &event_ch_is_light);

Call the local value int_src - it's not obvious to a reviewer what relationship between that and int_src is. I had to go read the datasheet to find out.

This unique name was suggested in a previous review:
https://lore.kernel.org/all/20240121152332.6b15666a@jic23-huawei/
I will change it next version. int_src is logical.

Quoted 15 lines

drivers/iio/light/apds9306.c line 1036

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -0
+		if (ret)
+			return ret;
+
+		ret = regmap_field_read(rf->int_en, &int_en);
+		if (ret)
+			return ret;
+
+		if (chan->type == IIO_LIGHT)
+			return int_en & event_ch_is_light;
+
+		if (chan->type == IIO_INTENSITY)
+			return int_en & !event_ch_is_light;

This is the specific line the compiler doesn't like drivers/iio/light/apds9306.c:1036:39: warning: dubious: x & !y

I am using gcc 12.2.0 for cross compiling. I definitely do not want to send patches with warnings in them. Can you please let me know the gcc version or flags using which you got the above warning? Should I always use the latest released version?

Quoted 5 lines

I would match int_src against specific values rather than using tricks based on what those values happen to be.

			return int_en && (int_src == APDS9306_INT_SRC_CLEAR);

I will implement this.

Thank you for taking time to review the code in detail and also appreciate your suggestions.

Regards,
Subhajit Ghosh

maintainer
#10 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

Quoted 35 lines
Quoted 27 lines
Quoted 14 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;
+}  

...

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	int int_en, event_ch_is_light, ret;
+
+	switch (type) {
Quoted 8 lines

drivers/iio/light/apds9306.c line 1024

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	case IIO_EV_TYPE_THRESH:
+		guard(mutex)(&data->mutex);
+
+		ret = regmap_field_read(rf->int_src, &event_ch_is_light);  

Call the local value int_src - it's not obvious to a reviewer what relationship between that and int_src is. I had to go read the datasheet to find out.

Quoted 3 lines

This unique name was suggested in a previous review:
https://lore.kernel.org/all/20240121152332.6b15666a@jic23-huawei/
I will change it next version. int_src is logical.

Ah, I failed to register that it was a multi bit field (which it isn't really but we should track what the datasheet says). If it had been a single bit then it would have made sense to name it as a boolean flag. Not so when in theory it could take 4 values (even if only 2 are defined).

Quoted 15 lines
Quoted 15 lines

drivers/iio/light/apds9306.c line 1036

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -0
+		if (ret)
+			return ret;
+
+		ret = regmap_field_read(rf->int_en, &int_en);
+		if (ret)
+			return ret;
+
+		if (chan->type == IIO_LIGHT)
+			return int_en & event_ch_is_light;
+
+		if (chan->type == IIO_INTENSITY)
+			return int_en & !event_ch_is_light;  

This is the specific line the compiler doesn't like drivers/iio/light/apds9306.c:1036:39: warning: dubious: x & !y

Quoted 4 lines

I am using gcc 12.2.0 for cross compiling. I definitely do not want to send patches with warnings in them. Can you please let me know the gcc version or flags using which you got the above warning? Should I always use the latest released version?

Version shouldn't matter that much but this was x86 build with gcc 13.2.1 W=1 is maybe what is showing this up as it enables a bunch more warnings.

IIO is almost clean with W=1 though a few minor things have gotten in recently that I need to tidy up.

Quoted 13 lines
Quoted 5 lines

I would match int_src against specific values rather than using tricks based on what those values happen to be.

			return int_en && (int_src == APDS9306_INT_SRC_CLEAR);  

I will implement this.

Thank you for taking time to review the code in detail and also appreciate your suggestions.

Regards,
Subhajit Ghosh

author maintainer
#11 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

In reply to Jonathan Cameron
Quoted 85 lines · Jonathan Cameron
Quoted 11 lines · On Sun, 18 Feb 2024 16:18:26 +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 4 lines

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

I applied this but then got some build warnings that made me look more closely at the int_src handling.

This is confusing because of the less than helpful datasheet defintion of a 2 bit register that takes values 0 and 1 only.

I thought about trying to fix this up whilst applying but the event code issue is too significant to do without a means to test it.

Jonathan

Quoted 55 lines
drivers/iio/light/apds9306.c new file +55 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	int ret, delay, intg_time, intg_time_idx, repeat_rate_idx, int_src;
+	int status = 0;
+	u8 buff[3];
+
+	ret = pm_runtime_resume_and_get(data->dev);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->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)
+		return intg_time;
+
+	/* 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, data->read_data_available ||
+				       (status & (APDS9306_ALS_DATA_STAT_MASK |
+						  APDS9306_ALS_INT_STAT_MASK)),
+				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
+
+	if (ret)
+		return ret;
+
+	/* If we reach here before the interrupt handler we push an event */
+	if ((status & APDS9306_ALS_INT_STAT_MASK))
Quoted 8 lines

drivers/iio/light/apds9306.c line 518

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+			       int_src, IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),

You are pushing an event on channel 0 or 1 (which is non obvious as that int_src is a 2 bit value again). However you don't use indexed channels so this is wrong.
It's also pushing IIO_LIGHT for both channels which makes no sense as you only have one IIO_LIGHT channel.

Hi Jonathan,

For the above fix I am supplying the second parameter to IIO_UNMOD_EVENT_CODE() as "0" which gives me the below output from userspace:
./iio_event_monitor /dev/iio:device0
Event: time: xx, type: illuminance, channel: 0, evtype: thresh, direction: either
Event: time: yy, type: intensity, channel: 0, evtype: thresh, direction: either

As I do not have indexed channels, I have used zero for both Light and Intensity channel numbers. Should I make the intensity type as channel one for the output to look like this?
Event: time: xx, type: illuminance, channel: 0, evtype: thresh, direction: either
Event: time: yy, type: intensity, channel: 1, evtype: thresh, direction: either

What do you think?

Regards,
Subhajit Ghosh

Quoted 20 lines
Quoted 18 lines
drivers/iio/light/apds9306.c new file +16 -0
+			       iio_get_time_ns(indio_dev));
+
+	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
+	if (ret) {
+		dev_err_ratelimited(dev, "read data failed\n");
+		return ret;
+	}
+
+	*val = get_unaligned_le24(&buff);
+
+	pm_runtime_mark_last_busy(data->dev);
+	pm_runtime_put_autosuspend(data->dev);
+
+	return 0;
+}
+

...

maintainer
#12 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

Quoted 102 lines · On Tue, 27 Feb 2024 23:42:48 +1030 Subhajit Ghosh…
Quoted 85 lines · Jonathan Cameron
Quoted 11 lines · On Sun, 18 Feb 2024 16:18:26 +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 4 lines

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

I applied this but then got some build warnings that made me look more closely at the int_src handling.

This is confusing because of the less than helpful datasheet defintion of a 2 bit register that takes values 0 and 1 only.

I thought about trying to fix this up whilst applying but the event code issue is too significant to do without a means to test it.

Jonathan

Quoted 55 lines
drivers/iio/light/apds9306.c new file +55 -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);
+	struct apds9306_regfields *rf = &data->rf;
+	int ret, delay, intg_time, intg_time_idx, repeat_rate_idx, int_src;
+	int status = 0;
+	u8 buff[3];
+
+	ret = pm_runtime_resume_and_get(data->dev);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->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)
+		return intg_time;
+
+	/* 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, data->read_data_available ||
+				       (status & (APDS9306_ALS_DATA_STAT_MASK |
+						  APDS9306_ALS_INT_STAT_MASK)),
+				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
+
+	if (ret)
+		return ret;
+
+	/* If we reach here before the interrupt handler we push an event */
+	if ((status & APDS9306_ALS_INT_STAT_MASK))
Quoted 8 lines

drivers/iio/light/apds9306.c line 518

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+		iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
+			       int_src, IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),  

You are pushing an event on channel 0 or 1 (which is non obvious as that int_src is a 2 bit value again). However you don't use indexed channels so this is wrong.
It's also pushing IIO_LIGHT for both channels which makes no sense as you only have one IIO_LIGHT channel.

Hi Jonathan,

For the above fix I am supplying the second parameter to IIO_UNMOD_EVENT_CODE() as "0" which gives me the below output from userspace:
./iio_event_monitor /dev/iio:device0
Event: time: xx, type: illuminance, channel: 0, evtype: thresh, direction: either
Event: time: yy, type: intensity, channel: 0, evtype: thresh, direction: either

Quoted 6 lines

As I do not have indexed channels, I have used zero for both Light and Intensity channel numbers. Should I make the intensity type as channel one for the output to look like this?
Event: time: xx, type: illuminance, channel: 0, evtype: thresh, direction: either
Event: time: yy, type: intensity, channel: 1, evtype: thresh, direction: either

No need. It's not an ABI bug if you did have that mix of channels, but you'd need to make them indexed in the chan_spec to match. Don't bother.

You should however us a modified event for the intensity channel seeing as it is .modified = 1, IIO_MOD_LIGHT_CLEAR

So IIO_MOD_EVENT_CODE would be appropriate.

Quoted 26 lines

What do you think?

Regards,
Subhajit Ghosh

Quoted 20 lines
Quoted 18 lines
drivers/iio/light/apds9306.c new file +16 -0
+			       iio_get_time_ns(indio_dev));
+
+	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
+	if (ret) {
+		dev_err_ratelimited(dev, "read data failed\n");
+		return ret;
+	}
+
+	*val = get_unaligned_le24(&buff);
+
+	pm_runtime_mark_last_busy(data->dev);
+	pm_runtime_put_autosuspend(data->dev);
+
+	return 0;
+}
+  

...

#13 lore raw

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

In reply to Subhajit Ghosh
Quoted 13 lines · 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 2 lines

+/*
+ * Available scales with gain 1x - 18x, timings 3.125, 25, 50, 100, 200, 400 mS

"mS" --> "ms."

Quoted 2 lines

+ * Time impacts to gain: 1x, 8x, 16x, 32x, 64x, 128x
+ */

...

drivers/iio/light/apds9306.c line 511

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -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, data->read_data_available ||
+				       (status & (APDS9306_ALS_DATA_STAT_MASK |
+						  APDS9306_ALS_INT_STAT_MASK)),
+				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
 
+

Redundant blank line

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

...

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+static int apds9306_init_iio_gts(struct apds9306_data *data)
+{
+	int i, ret, part_id;
+

drivers/iio/light/apds9306.c line 1142

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	ret = regmap_read(data->regmap, APDS9306_PART_ID_REG, &part_id);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ARRAY_SIZE(apds9306_gts_mul); i++)
+		if (part_id == apds9306_gts_mul[i].part_id)
+			break;
+
+	if (i == ARRAY_SIZE(apds9306_gts_mul))
+		return -ENXIO;

Strange choice of the error code, why not (one of) -ENOENT / -ENODATA ?

drivers/iio/light/apds9306.c line 1151

Quoted 9 lines
drivers/iio/light/apds9306.c new file +8 -0
+	return devm_iio_init_iio_gts(data->dev,
+				     apds9306_gts_mul[i].max_scale_int,
+				     apds9306_gts_mul[i].max_scale_nano,
+				     apds9306_gains, ARRAY_SIZE(apds9306_gains),
+				     apds9306_itimes, ARRAY_SIZE(apds9306_itimes),
+				     &data->gts);
 
+
+	return -ENXIO;

Dead code.

Quoted 1 line

+}

...

Jonathan, are you going to apply this and addressing comments at the same time? Or should it be another version?

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

Re: [PATCH v7 5/5] iio: light: Add support for APDS9306 Light Sensor

Quoted 77 lines · On Mon, 26 Feb 2024 16:12:23 +0200 Andy Shevchenko…
Quoted 13 lines · 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 2 lines

+/*
+ * Available scales with gain 1x - 18x, timings 3.125, 25, 50, 100, 200, 400 mS

"mS" --> "ms."

Quoted 2 lines

+ * Time impacts to gain: 1x, 8x, 16x, 32x, 64x, 128x
+ */

...

drivers/iio/light/apds9306.c line 511

Quoted 13 lines
drivers/iio/light/apds9306.c new file +12 -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, data->read_data_available ||
+				       (status & (APDS9306_ALS_DATA_STAT_MASK |
+						  APDS9306_ALS_INT_STAT_MASK)),
+				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);  
 
+  

Redundant blank line

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

...

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+static int apds9306_init_iio_gts(struct apds9306_data *data)
+{
+	int i, ret, part_id;
+

drivers/iio/light/apds9306.c line 1142

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	ret = regmap_read(data->regmap, APDS9306_PART_ID_REG, &part_id);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ARRAY_SIZE(apds9306_gts_mul); i++)
+		if (part_id == apds9306_gts_mul[i].part_id)
+			break;
+
+	if (i == ARRAY_SIZE(apds9306_gts_mul))
+		return -ENXIO;  

Strange choice of the error code, why not (one of) -ENOENT / -ENODATA ?

drivers/iio/light/apds9306.c line 1151

Quoted 9 lines
drivers/iio/light/apds9306.c new file +8 -0
+	return devm_iio_init_iio_gts(data->dev,
+				     apds9306_gts_mul[i].max_scale_int,
+				     apds9306_gts_mul[i].max_scale_nano,
+				     apds9306_gains, ARRAY_SIZE(apds9306_gains),
+				     apds9306_itimes, ARRAY_SIZE(apds9306_itimes),
+				     &data->gts);  
 
+
+	return -ENXIO;  

Dead code.

Quoted 7 lines
Quoted 1 line

+}

...

Jonathan, are you going to apply this and addressing comments at the same time? Or should it be another version?

The multibit field pretending to be a boolean was too complex for to want to modify whilst applying. So yes, v8 with that tidied up and your comments sorted out

Jonathan