Skip to contentSkip to conversation

Support for Avago APDS9306 Ambient Light Sensor

PATCH Superseded Subhajit Ghosh 4 participants 16 messages Feb 28, 2024 – Mar 4, 2024

Subsystems (3)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Subhajit Ghosh

author maintainer
#0 lore raw

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

v7 -> v8:
- Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
- Removed APDS9306_CHANNEL macro for higher readability
- Updated iio_push_event() functions with correct type of events (Light or Intensity)
- Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
- Used scope for guard() functions
- Other fixes as per reviews
https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
https://lore.kernel.org/all/[email protected]/

v7 -> v8 Bindings:
- Updated commit message as mentioned by Jonathan
https://lore.kernel.org/all/20240224143803.27efa14f@jic23-huawei/

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                  | 1355 +++++++++++++++++
 5 files changed, 1383 insertions(+), 49 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
 create mode 100644 drivers/iio/light/apds9306.c

base-commit: 45ec2f5f6ed3ec3a79ba1329ad585497cdcbe663

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

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

Merge very similar schemas for APDS9300 and APDS9960.

---
v7 -> v8:
- No change

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

---
v7 -> v8:
- No change

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 v8 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

---
v7 -> v8:
- No change

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 v8 4/5 dt-bindings: iio: light: Avago APDS9306

In reply to Subhajit Ghosh

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

---
v7 -> v8:
- Updated commit message as mentioned by Jonathan
https://lore.kernel.org/all/20240224143803.27efa14f@jic23-huawei/

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

PATCH v8 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.

---
v7 -> v8:
- Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
- Removed APDS9306_CHANNEL macro for higher readability
- Updated iio_push_event() functions with correct type of events (Light or Intensity)
- Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
- Used scope for guard() functions
- Other fixes as per reviews
https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
https://lore.kernel.org/all/[email protected]/

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 | 1355 ++++++++++++++++++++++++++++++++++
 3 files changed, 1368 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 +1355 -0

1,355 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

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

Quoted 50 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.

---
v7 -> v8:
- Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
- Removed APDS9306_CHANNEL macro for higher readability
- Updated iio_push_event() functions with correct type of events (Light or Intensity)
- Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
- Used scope for guard() functions
- Other fixes as per reviews

    https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
    https://lore.kernel.org/all/[email protected]/

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

    Link: https://lore.kernel.org/all/20240204134056.5dc64e8b@jic23-huawei/
Quoted 8 lines

v2 -> v5:
- 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.

---

Hi Subhajit,

I just happened to notice couple of minor things. I see the series is already in a v8 and don't want to cause extra re-spins. So, perhaps consider these points if you need to do v9 but I am sending these only as 'nits'. I don't think any of my findings are very serious.

...

Quoted 11 lines
drivers/iio/light/apds9306.c new file +11 -0
+/*
+ * As per the datasheet, at HW Gain = 3x, Integration time 100mS (32x),
+ * typical 2000 ADC counts are observed for 49.8 uW per sq cm (340.134 lux)
+ * for apds9306 and 43 uW per sq cm (293.69 lux) for apds9306-065.
+ * Assuming lux per count is linear across all integration time ranges.
+ *
+ * Lux = (raw + offset) * scale; offset can be any value by userspace.
+ * HG = Hardware Gain; ITG = Gain by changing integration time.
+ * Scale table by IIO GTS Helpers = (1 / HG) * (1 / ITG) * Multiplier.
+ *
+ * The Lux values provided in the datasheet are at ITG=32x and HG=3x,

drivers/iio/light/apds9306.c line 107

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+ * at typical 2000 count for both variants of the device.
+ *
+ * Lux per ADC count at 3x and 32x for apds9306 = 340.134 / 2000
+ * Lux per ADC count at 3x and 32x for apds9306-065 = 293.69 / 2000
+ *
+ * The Multiplier for the scale table provided to userspace:
+ * IIO GTS scale Multiplier for apds9306 = (340.134 / 2000) * 32 * 3 = 16.326432
+ * and for apds9306-065 = (293.69 / 2000) * 32 * 3 = 14.09712
+ */
+static struct part_id_gts_multiplier apds9306_gts_mul[] = {

const ?

drivers/iio/light/apds9306.c line 117

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+	{
+		.part_id = 0xB1,
+		.max_scale_int = 16,
+		.max_scale_nano = 3264320,
+	}, {
+		.part_id = 0xB3,
+		.max_scale_int = 14,
+		.max_scale_nano = 9712000,
+	},
+};

...> +static struct iio_event_spec apds9306_event_spec_als[] = {

const ?

Quoted 11 lines
drivers/iio/light/apds9306.c new file +11 -0
+	{
+		.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,

drivers/iio/light/apds9306.c line 242

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	}, {
+		.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[] = {

const ?

drivers/iio/light/apds9306.c line 250

Quoted 8 lines
drivers/iio/light/apds9306.c new file +8 -0
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
+static struct iio_chan_spec apds9306_channels_with_events[] = {

const?

Quoted 16 lines
drivers/iio/light/apds9306.c new file +16 -0
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.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),
+	}, {
+		.type = IIO_INTENSITY,
+		.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) |

drivers/iio/light/apds9306.c line 276

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+						     BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.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[] = {

const?

Quoted 20 lines
drivers/iio/light/apds9306.c new file +20 -0
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+	}, {
+		.type = IIO_INTENSITY,
+		.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),
+		.channel2 = IIO_MOD_LIGHT_CLEAR,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.modified = 1,
+	},
+};

...

Quoted 24 lines
drivers/iio/light/apds9306.c new file +24 -0
+static int apds9306_intg_time_set(struct apds9306_data *data, int val2)
+{
+	struct device *dev = data->dev;
+	struct apds9306_regfields *rf = &data->rf;
+	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_ratelimited(dev, "Unsupported integration time %u\n", val2);
+		return -EINVAL;
+	}
+
+	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
+	if (ret)
+		return ret;
+
+	ret = regmap_field_read(rf->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;

drivers/iio/light/apds9306.c line 600

Quoted 10 lines
drivers/iio/light/apds9306.c new file +10 -0
+
+	if (intg_old == val2)
+		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);

You don't use the 'ret' here, so maybe for the clarity, not assign it.
Or, maybe you wan't to try to squeeze out few cycles for succesful case and check the ret for '0' - in which case you should be able to omit the check right below as well as the call to iio_find_closest_gain_low(). OTOH, this is likely not a "hot path" so I don't care too much about the extra call if you think code is clearer this way.

Quoted 28 lines
drivers/iio/light/apds9306.c new file +28 -0
+	if (gain_new < 0) {
+		dev_err_ratelimited(dev, "Unsupported gain with time\n");
+		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(rf->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(rf->gain, ret);
+}
Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#7 lore raw

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

Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 10 lines

drivers/iio/light/apds9306.c line 600

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+	ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, gain_old,
+						     intg_old, val2, &gain_new);

You don't use the 'ret' here, so maybe for the clarity, not assign it. Or, maybe you wan't to try to squeeze out few cycles for succesful case and check the ret for '0' - in which case you should be able to omit the check right below as well as the call to iio_find_closest_gain_low(). OTOH, this is likely not a "hot path" so I don't care too much about the extra call if you think code is clearer this way.

Quoted 4 lines

drivers/iio/light/apds9306.c line 604

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	if (gain_new < 0) {
+		dev_err_ratelimited(dev, "Unsupported gain with time\n");
+		return gain_new;
+	}

What is the difference between negative response from the function itself and similar in gain_new?

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

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

Quoted 15 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 10 lines

drivers/iio/light/apds9306.c line 600

Quoted 2 lines
drivers/iio/light/apds9306.c new file +2 -0
+	ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, gain_old,
+						     intg_old, val2, &gain_new);

You don't use the 'ret' here, so maybe for the clarity, not assign it. Or, maybe you wan't to try to squeeze out few cycles for succesful case and check the ret for '0' - in which case you should be able to omit the check right below as well as the call to iio_find_closest_gain_low(). OTOH, this is likely not a "hot path" so I don't care too much about the extra call if you think code is clearer this way.

Quoted 8 lines
Quoted 4 lines

drivers/iio/light/apds9306.c line 604

Quoted 4 lines
drivers/iio/light/apds9306.c new file +4 -0
+	if (gain_new < 0) {
+		dev_err_ratelimited(dev, "Unsupported gain with time\n");
+		return gain_new;
+	}

What is the difference between negative response from the function itself and similar in gain_new?

-ve response form the function is an error condition.
-ve value in gain_new means - no valid gains could be computed. In case of error conditions from the function, the gain_new is also set to -1. My use case is valid hardware gain so I went for checking only gain_new. Matti will be the best person to answer on this.

Regards,
Subhajit Ghosh

#9 lore raw

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

Quoted 26 lines · Subhajit Ghosh
Quoted 23 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 14 lines
Quoted 2 lines

+    ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, gain_old, +                             intg_old, val2, &gain_new);

You don't use the 'ret' here, so maybe for the clarity, not assign it. Or, maybe you wan't to try to squeeze out few cycles for succesful case and check the ret for '0' - in which case you should be able to omit the check right below as well as the call to iio_find_closest_gain_low(). OTOH, this is likely not a "hot path" so I don't care too much about the extra call if you think code is clearer this way.

Quoted 4 lines

+    if (gain_new < 0) {
+        dev_err_ratelimited(dev, "Unsupported gain with time\n"); +        return gain_new;
+    }

What is the difference between negative response from the function itself and similar in gain_new?

(quoted diff) +0 -2
-ve response form the function is an error condition.
-ve value in gain_new means - no valid gains could be computed.
Quoted 3 lines

In case of error conditions from the function, the gain_new is also set to -1. My use case is valid hardware gain so I went for checking only gain_new. Matti will be the best person to answer on this.

I now rely on the kerneldoc for the iio_gts_find_new_gain_by_old_gain_time() as it seems reasonable to me:

* Return: 0 if an exactly matching supported new gain was found. When a
* non-zero value is returned, the @new_gain will be set to a negative or
* positive value. The negative value means that no gain could be computed.
* Positive value will be the "best possible new gain there could be". There
* can be two reasons why finding the "best possible" new gain is not deemed
* successful. 1) This new value cannot be supported by the hardware. 2) The new
* gain required to maintain the scale would not be an integer. In this case,
* the "best possible" new gain will be a floored optimal gain, which may or
* may not be supported by the hardware.

Eg, if ret is zero, there is no need to check validity of the gain_new but it is guaranteed to be one of the supported gains.

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#10 lore raw

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

Quoted 4 lines · Matti Vaittinen
Quoted 3 lines · Subhajit Ghosh
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 30 lines
Quoted 11 lines
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

+    if (gain_new < 0) {
+        dev_err_ratelimited(dev, "Unsupported gain with time\n"); +        return gain_new;
+    }

What is the difference between negative response from the function itself and
similar in gain_new?

(quoted diff) +0 -2
-ve response form the function is an error condition.
-ve value in gain_new means - no valid gains could be computed.
Quoted 4 lines

In case of error conditions from the function, the gain_new is also set to -1.
My use case is valid hardware gain so I went for checking only gain_new. Matti will be the best person to answer on this.

I now rely on the kerneldoc for the iio_gts_find_new_gain_by_old_gain_time() as it seems reasonable to me:

* Return: 0 if an exactly matching supported new gain was found. When a
* non-zero value is returned, the @new_gain will be set to a negative or
* positive value. The negative value means that no gain could be computed.
* Positive value will be the "best possible new gain there could be". There
* can be two reasons why finding the "best possible" new gain is not deemed
* successful. 1) This new value cannot be supported by the hardware. 2) The new
* gain required to maintain the scale would not be an integer. In this case,
* the "best possible" new gain will be a floored optimal gain, which may or
* may not be supported by the hardware.

Quoted 2 lines

Eg, if ret is zero, there is no need to check validity of the gain_new but it is guaranteed to be one of the supported gains.

Right, but this kernel doc despite being so verbose does not fully answer my question. What is the semantic of that "negative value"? I would expect to have the error code there (maybe different to what the function returns), but at least be able to return it to the upper layers if needed.

Hence 2 ARs I see:
1) clarify the kernel documentation there;
2) update the semantic of the gain_new to simplify caller's code.

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

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

Quoted 38 lines · Andy Shevchenko
Quoted 4 lines · Matti Vaittinen
Quoted 3 lines · Subhajit Ghosh
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 29 lines
Quoted 11 lines
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

+    if (gain_new < 0) {
+        dev_err_ratelimited(dev, "Unsupported gain with time\n"); +        return gain_new;
+    }

What is the difference between negative response from the function itself and
similar in gain_new?

(quoted diff) +0 -2
-ve response form the function is an error condition.
-ve value in gain_new means - no valid gains could be computed.
Quoted 4 lines

In case of error conditions from the function, the gain_new is also set to -1.
My use case is valid hardware gain so I went for checking only gain_new. Matti will be the best person to answer on this.

I now rely on the kerneldoc for the iio_gts_find_new_gain_by_old_gain_time() as it seems reasonable to me:

* Return: 0 if an exactly matching supported new gain was found. When a
* non-zero value is returned, the @new_gain will be set to a negative or
* positive value. The negative value means that no gain could be computed.
* Positive value will be the "best possible new gain there could be". There
* can be two reasons why finding the "best possible" new gain is not deemed
* successful. 1) This new value cannot be supported by the hardware. 2) The new
* gain required to maintain the scale would not be an integer. In this case,
* the "best possible" new gain will be a floored optimal gain, which may or
* may not be supported by the hardware.

Quoted 5 lines
Quoted 2 lines

Eg, if ret is zero, there is no need to check validity of the gain_new but it is guaranteed to be one of the supported gains.

Right, but this kernel doc despite being so verbose does not fully answer my question. What is the semantic of that "negative value"?

Current approach is to always investigate the function return value as error if the 'new_gain' is negative. Or, caller specific error if new_gain is unsuitable in some other way. When this is done, the absolute value of the negative 'new_gain' does not matter.

Quoted 3 lines

I would expect to have
the error code there (maybe different to what the function returns), but at least be able to return it to the upper layers if needed.

I am not sure I see the benefit of returning the new_gain over returning the error returned by the function. Well, maybe the benefit to be able to not evaluate the value returned by the iio_gts_find_new_gain_by_old_gain_time() - although I'm not sure I love it.

Quoted 3 lines

Hence 2 ARs I see:
1) clarify the kernel documentation there;
2) update the semantic of the gain_new to simplify caller's code.

Yes, I agree. Patches welcome :) By the very least the kerneldoc can be improved. I'm undecided on benefits of having the error code in 'new_gain'.

The GTS API fixes shouldn't be required in the context of this driver series though.

Yours,

	--Matti
Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#12 lore raw

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

Quoted 59 lines · On Thu, 29 Feb 2024 17:35:01 +0200 Matti Vaittinen <[email protected]>…
Quoted 38 lines · Andy Shevchenko
Quoted 4 lines · Matti Vaittinen
Quoted 3 lines · Subhajit Ghosh
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 2/28/24 14:24, Subhajit Ghosh wrote:

...

Quoted 29 lines
Quoted 11 lines
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

+    if (gain_new < 0) {
+        dev_err_ratelimited(dev, "Unsupported gain with time\n"); +        return gain_new;
+    }

What is the difference between negative response from the function itself and
similar in gain_new?

(quoted diff) +0 -2
-ve response form the function is an error condition.
-ve value in gain_new means - no valid gains could be computed.
Quoted 4 lines

In case of error conditions from the function, the gain_new is also set to -1.
My use case is valid hardware gain so I went for checking only gain_new. Matti will be the best person to answer on this.

I now rely on the kerneldoc for the iio_gts_find_new_gain_by_old_gain_time() as it seems reasonable to me:

* Return: 0 if an exactly matching supported new gain was found. When a
* non-zero value is returned, the @new_gain will be set to a negative or
* positive value. The negative value means that no gain could be computed.
* Positive value will be the "best possible new gain there could be". There
* can be two reasons why finding the "best possible" new gain is not deemed
* successful. 1) This new value cannot be supported by the hardware. 2) The new
* gain required to maintain the scale would not be an integer. In this case,
* the "best possible" new gain will be a floored optimal gain, which may or
* may not be supported by the hardware.

Quoted 5 lines
Quoted 2 lines

Eg, if ret is zero, there is no need to check validity of the gain_new but it is guaranteed to be one of the supported gains.

Right, but this kernel doc despite being so verbose does not fully answer my question. What is the semantic of that "negative value"?

Current approach is to always investigate the function return value as error if the 'new_gain' is negative. Or, caller specific error if new_gain is unsuitable in some other way. When this is done, the absolute value of the negative 'new_gain' does not matter.

Quoted 3 lines

I would expect to have
the error code there (maybe different to what the function returns), but at least be able to return it to the upper layers if needed.

I am not sure I see the benefit of returning the new_gain over returning the error returned by the function. Well, maybe the benefit to be able to not evaluate the value returned by the iio_gts_find_new_gain_by_old_gain_time() - although I'm not sure I love it.

Quoted 6 lines
Quoted 3 lines

Hence 2 ARs I see:
1) clarify the kernel documentation there;
2) update the semantic of the gain_new to simplify caller's code.

Yes, I agree. Patches welcome :) By the very least the kerneldoc can be improved. I'm undecided on benefits of having the error code in 'new_gain'.

It's definitely a weird bit of API and would benefit from a rethink.

Quoted 3 lines

The GTS API fixes shouldn't be required in the context of this driver series though.

Agreed.

Quoted 4 lines

Yours,

	--Matti
author maintainer
#13 lore raw

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

In reply to Matti Vaittinen
Quoted 15 lines · Matti Vaittinen
Quoted 14 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 14 lines
Quoted 14 lines

---
v7 -> v8:
  - Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
  - Removed APDS9306_CHANNEL macro for higher readability
  - Updated iio_push_event() functions with correct type of events (Light or Intensity)
  - Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
  - Used scope for guard() functions
  - Other fixes as per reviews
    https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
    https://lore.kernel.org/all/[email protected]/

...

Hi Matti,

Quoted 6 lines
Quoted 1 line

---

Hi Subhajit,

I just happened to notice couple of minor things. I see the series is already in a v8 and don't want to cause extra re-spins. So, perhaps consider these points if you need to do v9 but I am sending these only as 'nits'. I don't think any of my findings are very serious. Thank for reviewing. I will do as many re-spins as it takes to get things correct if required.

It is best possible source of learning for me.

Quoted 31 lines

...

Quoted 29 lines
drivers/iio/light/apds9306.c new file +29 -0
+static int apds9306_intg_time_set(struct apds9306_data *data, int val2)
+{
+    struct device *dev = data->dev;
+    struct apds9306_regfields *rf = &data->rf;
+    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_ratelimited(dev, "Unsupported integration time %u\n", val2);
+        return -EINVAL;
+    }
+
+    ret = regmap_field_read(rf->intg_time, &intg_time_idx);
+    if (ret)
+        return ret;
+
+    ret = regmap_field_read(rf->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)
+        return 0;
+
+    gain_old = iio_gts_find_gain_by_sel(&data->gts, gain_idx);
Quoted 8 lines
Quoted 5 lines

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

You don't use the 'ret' here, so maybe for the clarity, not assign it.
Or, maybe you wan't to try to squeeze out few cycles for succesful case and check the ret for '0' - in which case you should be able to omit the check right below as well as the call to iio_find_closest_gain_low(). OTOH, this is likely not a "hot path" so I don't care too much about the extra call if you think code is clearer this way.

I will stick to the first option and remove the unused ret. The code looks linear and clearer that way. Although it depends upon further reviews.

Regards,
Subhajit Ghosh

Quoted 31 lines
Quoted 29 lines

+    if (gain_new < 0) {
+        dev_err_ratelimited(dev, "Unsupported gain with time\n"); +        return gain_new;
+    }

drivers/iio/light/apds9306.c new file +24 -0
+
+    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(rf->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(rf->gain, ret);
+}
maintainer
#14 lore raw

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

In reply to Subhajit Ghosh
Quoted 14 lines · On Wed, 28 Feb 2024 22:54:08 +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 7 lines

---
v7 -> v8:
- Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
- Removed APDS9306_CHANNEL macro for higher readability
- Updated iio_push_event() functions with correct type of events (Light or Intensity)

Partly right. Need to push the modified part for the intensity channel. The event should match the channel description.

I also noted some missing elements in the event specs (sorry missed those before!). Whilst what you have will work, that's just because the error checking is relaxed in the IIO core and we don't complain if they aren't fully specified. What you have creates the correct attributes, but that's a side effect of how we use the data, not what data should be provided.

Thanks,

Jonathan

Quoted 13 lines

- Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
- Used scope for guard() functions
- Other fixes as per reviews
https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
https://lore.kernel.org/all/[email protected]/

drivers/iio/light/Makefile +0 -0
index 2e5fdb33e0e9..a30f906e91ba 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_ADUX1020)		+= adux1020.o

...

Quoted 19 lines
drivers/iio/light/Makefile +19 -0
+	GAIN_SCALE_ITIME_US(3125, APDS9306_MEAS_MODE_3125US, BIT(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),
+	}, {
+		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,

drivers/iio/light/Makefile

Quoted 10 lines
drivers/iio/light/Makefile +10 -0
+		.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,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),

Can't configure the threshold for this channel?

Whilst the IIO core doesn't check these for missing entries in shared attributes, you driver should replicate the parts that are in mask_shared_by_all above. The code that builds the attributes expects duplication of entries so they are here to provide an easy place for us to visually check what is supported.

I think that means this event spec will be identical to that for the als channel. So reuse that.

Let us know if you copied this pattern from another driver as we should fix any that have gotten through review doing this.

Quoted 54 lines
drivers/iio/light/Makefile +52 -0
+	},
+};
+
+static struct iio_chan_spec apds9306_channels_with_events[] = {
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.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),
+	}, {
+		.type = IIO_INTENSITY,
+		.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),
+		.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[] = {
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+	}, {
+		.type = IIO_INTENSITY,
+		.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),
+		.channel2 = IIO_MOD_LIGHT_CLEAR,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.modified = 1,
+	},
+};
 
 
+static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
+{

...

drivers/iio/light/Makefile

Quoted 6 lines
drivers/iio/light/Makefile +6 -0
+	/* If we reach here before the interrupt handler we push an event */
+	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
+		ev_code = IIO_UNMOD_EVENT_CODE((int_src == APDS9306_INT_SRC_ALS ?
+						IIO_LIGHT : IIO_INTENSITY), 0,
+					       IIO_EV_TYPE_THRESH,
+					       IIO_EV_DIR_EITHER);

As below. The intensity channel is modified, so you need to push an event for a modified channel for that - otherwise it won't match with the channel.

Quoted 20 lines
drivers/iio/light/Makefile +19 -0
+
+		iio_push_event(indio_dev, ev_code, 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;
+}
+
 
+

drivers/iio/light/Makefile

Quoted 10 lines
drivers/iio/light/Makefile +10 -0
+static int apds9306_sampling_freq_set(struct apds9306_data *data, int val,
+				      int val2)
+{
+	struct apds9306_regfields *rf = &data->rf;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(apds9306_repeat_rate_freq); i++) {
+		if (apds9306_repeat_rate_freq[i][0] == val &&
+		    apds9306_repeat_rate_freq[i][1] == val2)
+			break;

Up to you, but you could simplify this.

			return regmap_field_write(rf->repeate_rate, i);
	}

	return -EINVAL;
Quoted 10 lines
drivers/iio/light/Makefile +10 -0
+	}
+
+	if (i == ARRAY_SIZE(apds9306_repeat_rate_freq))
+		return -EINVAL;
+
+	return regmap_field_write(rf->repeat_rate, i);
+}
+
+static irqreturn_t apds9306_irq_handler(int irq, void *priv)
+{

...

drivers/iio/light/Makefile

Quoted 3 lines
drivers/iio/light/Makefile +3 -0
+	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
+		ev_code = IIO_UNMOD_EVENT_CODE((int_src == APDS9306_INT_SRC_ALS ?
+						IIO_LIGHT : IIO_INTENSITY), 0,

That ternary is not good for readability, particularly not with the trailing 0, Use a local variable, though you won't need this anyway after the fix below.

drivers/iio/light/Makefile

Quoted 2 lines
drivers/iio/light/Makefile +2 -0
+					       IIO_EV_TYPE_THRESH,
+					       IIO_EV_DIR_EITHER);

The INTENSITY channel is a modified channel so events on it need to report as modified. Right now you are reporting an event code that userspace will not expect an unmodified intensity event because it is looking for something like

	IIO_MOD_EVENT_CODE(IIO_INTENSITY, 0, IIO_MOD_LIGHT_CLEAR,
			   IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER);
Quoted 15 lines
drivers/iio/light/Makefile +15 -0
+
+		iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));
+	}
+
+	/*
+	 * 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;
+}
author maintainer
#15 lore raw

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

Quoted 76 lines · Jonathan Cameron
Quoted 14 lines · On Wed, 28 Feb 2024 22:54:08 +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 7 lines

---
v7 -> v8:
- Renamed APDS9306_INT_CH_CLEAR to APDS9306_INT_SRC_CLEAR macro for higher readability
- Removed APDS9306_CHANNEL macro for higher readability
- Updated iio_push_event() functions with correct type of events (Light or Intensity)

Partly right. Need to push the modified part for the intensity channel. The event should match the channel description.

I also noted some missing elements in the event specs (sorry missed those before!). Whilst what you have will work, that's just because the error checking is relaxed in the IIO core and we don't complain if they aren't fully specified. What you have creates the correct attributes, but that's a side effect of how we use the data, not what data should be provided.

Thanks,

Jonathan

Quoted 13 lines

- Updated variable name "event_ch_is_light" to "int_src" and change as per review to fix compiler warning
- Used scope for guard() functions
- Other fixes as per reviews

    https://lore.kernel.org/all/20240224151340.3f2f51e8@jic23-huawei/
    https://lore.kernel.org/all/[email protected]/
drivers/iio/light/Makefile +0 -0
index 2e5fdb33e0e9..a30f906e91ba 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_ADUX1020)		+= adux1020.o

...

Quoted 23 lines
drivers/iio/light/Makefile +23 -0
+	GAIN_SCALE_ITIME_US(3125, APDS9306_MEAS_MODE_3125US, BIT(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),
+	}, {
+		.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/Makefile

Quoted 6 lines
drivers/iio/light/Makefile +6 -0
+
+static struct iio_event_spec apds9306_event_spec_clear[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),

Can't configure the threshold for this channel?

Same threshold regs for both als and clear channels.

Quoted 6 lines

Whilst the IIO core doesn't check these for missing entries in shared attributes, you driver should replicate the parts that are in mask_shared_by_all above. The code that builds the attributes expects duplication of entries so they are here to provide an easy place for us to visually check what is supported.

I understand this approach now.

Quoted 3 lines

I think that means this event spec will be identical to that for the als channel. So reuse that.

Yes, correct.
I tried using struct iio_event_spec apds9306_event_spec_als[] for both light and clear channels and the ABI is identical to the previous version.

Quoted 3 lines

Let us know if you copied this pattern from another driver as we should fix any that have gotten through review doing this.

Initially I referenced many drivers but after so many iterations it does not resemble anything that I have looked at previously.

Thank you for reviewing.

Regards,
Subhajit Ghosh

Quoted 53 lines
Quoted 51 lines
drivers/iio/light/Makefile +50 -0
+	},
+};
+
+static struct iio_chan_spec apds9306_channels_with_events[] = {
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.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),
+	}, {
+		.type = IIO_INTENSITY,
+		.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),
+		.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[] = {
+	{
+		.type = IIO_LIGHT,
+		.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),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+	}, {
+		.type = IIO_INTENSITY,
+		.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),
+		.channel2 = IIO_MOD_LIGHT_CLEAR,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.modified = 1,
+	},
+};