Skip to contentSkip to conversation

Add LTRF216A Driver

PATCH Superseded Shreeya Patel 5 participants 14 messages Jun 15, 2022 – Jul 6, 2022

Subsystems (2)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v6 cover Add LTRF216A Driver

This patchset adds support for ltrf216a Ambient Light Sensor and documents the DT bindings for the same.

Changes in v6
- Fix some errors reported by kernel test robot.
- Add protocol details for the datasheet link.
- Remove useless assignments.
- Add unit details for read data delay macro.
- Use pm_sleep_ptr().

Changes in v5
- Add power management support.
- Add reset functionality.
- Use readx_poll_timeout() to get data.
- Cleanup some of the redundant code.
- Update int_time_fac after I2C write is successful.
- Rename mutex to lock.
- Use Reverse Xmas tree pattern for all variable definitions.
- Improve error handling messages and add error codes.
- Add one more MODULE_AUTHOR.
- Remove cleardata which was reading data for infrared light.
- Remove patch for deprecated vendor prefix [PATCH v4 3/3].
- Remove deprecated string from DT binding document.

Changes in v4
- Add more descriptive comment for mutex lock
- Fix mutex locking in read_raw()
- Use i2c_smbus_read_i2c_block_data()

Changes in v3
- Use u16 instead of u8 for int_time_fac
- Reorder headers in ltrf216a.c file
- Remove int_time_mapping table and use int_time_available
- Fix indentation in the bindings file.

Changes in v2
- Add support for 25ms and 50ms integration time.
- Rename some of the macros as per names given in datasheet
- Add a comment for the mutex lock
- Use read_avail callback instead of attributes and set the appropriate _available bit.
- Use FIELD_PREP() at appropriate places.
- Add a constant lookup table for integration time and reg val
- Use BIT() macro for magic numbers.
- Improve error handling at few places.
- Use get_unaligned_le24() and div_u64()
- Use probe_new() callback and devm functions
- Return errors in probe using dev_err_probe()
- Use DEFINE_SIMPLE_DEV_PM_OPS()
- Correct the formula for lux to use 0.45 instead of 0.8
- Add interrupt and power supply property in DT bindings
- Add vendor prefix name as per the alphabetical order.

Shreeya Patel (2):
dt-bindings: Document ltrf216a light sensor bindings
iio: light: Add support for ltrf216a sensor

 .../bindings/iio/light/liteon,ltrf216a.yaml   |  50 ++
 drivers/iio/light/Kconfig                     |  10 +
 drivers/iio/light/Makefile                    |   1 +
 drivers/iio/light/ltrf216a.c                  | 429 ++++++++++++++++++
 4 files changed, 490 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
 create mode 100644 drivers/iio/light/ltrf216a.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v6 1/2 dt-bindings: Document ltrf216a light sensor bindings

Add devicetree bindings for ltrf216a ambient light sensor.

---
Changes in v5
- Remove deprecated string 'ltr' from the bindings.

Changes in v3
- Fix indentation in the example section

Changes in v2
- Take over the maintainership for the bindings
- Add interrupt and power supply property in DT bindings

 .../bindings/iio/light/liteon,ltrf216a.yaml   | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +50 -0
new file mode 100644
index 000000000000..f256ff2e744c
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/liteon,ltrf216a.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LTRF216A Ambient Light Sensor
+
+maintainers:
+  - Shreeya Patel <[email protected]>
+
+description:
+  Ambient light sensing with an i2c interface.
+
+properties:
+  compatible:
+    const:
+      - liteon,ltrf216a
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        light-sensor@53 {
+            compatible = "liteon,ltrf216a";
+            reg = <0x53>;
+            vdd-supply = <&vdd_regulator>;
+            interrupt-parent = <&gpio0>;
+            interrupts = <5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH v6 1/2] dt-bindings: Document ltrf216a light sensor bindings

Quoted 10 lines · Shreeya Patel

Add devicetree bindings for ltrf216a ambient light sensor.

---
Changes in v5
- Remove deprecated string 'ltr' from the bindings.

Changes in v3
- Fix indentation in the example section

Quoted 8 lines

Changes in v2
- Take over the maintainership for the bindings
- Add interrupt and power supply property in DT bindings

 .../bindings/iio/light/liteon,ltrf216a.yaml   | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/linux-dt-review/Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml: properties:compatible:const: ['liteon,ltrf216a'] is not of type 'string'
from schema $id: http://devicetree.org/meta-schemas/string-array.yaml#
/builds/robherring/linux-dt-review/Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml: ignoring, error in schema: properties: compatible: const Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.example.dtb:0:0: /example-0/i2c/light-sensor@53: failed to match any schema with compatible: ['liteon,ltrf216a']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

maintainer
#3 lore raw

Re: [PATCH v6 1/2] dt-bindings: Document ltrf216a light sensor bindings

In reply to Shreeya Patel
Quoted 32 lines · On Wed, 15 Jun 2022 19:21:29 +0530 Shreeya Patel <[email protected]>…

Add devicetree bindings for ltrf216a ambient light sensor.

---
Changes in v5
- Remove deprecated string 'ltr' from the bindings.

Changes in v3
- Fix indentation in the example section

Changes in v2
- Take over the maintainership for the bindings
- Add interrupt and power supply property in DT bindings

 .../bindings/iio/light/liteon,ltrf216a.yaml   | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +8 -0
new file mode 100644
index 000000000000..f256ff2e744c
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/liteon,ltrf216a.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LTRF216A Ambient Light Sensor
+

Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +10 -0
+maintainers:
+  - Shreeya Patel <[email protected]>
+
+description:
+  Ambient light sensing with an i2c interface.
+
+properties:
+  compatible:
+    const:
+      - liteon,ltrf216a

I assume you figured this out from the build bot error.

const: liteon,ltrf216a

Please make sure to do what that message from Rob's bot says and test your bindings before sending v7.

Quoted 32 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +32 -0
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        light-sensor@53 {
+            compatible = "liteon,ltrf216a";
+            reg = <0x53>;
+            vdd-supply = <&vdd_regulator>;
+            interrupt-parent = <&gpio0>;
+            interrupts = <5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
#4 lore raw

Re: [PATCH v6 1/2] dt-bindings: Document ltrf216a light sensor bindings

Quoted 47 lines · Jonathan Cameron
Quoted 32 lines · On Wed, 15 Jun 2022 19:21:29 +0530 Shreeya Patel <[email protected]>…

Add devicetree bindings for ltrf216a ambient light sensor.

---
Changes in v5
- Remove deprecated string 'ltr' from the bindings.

Changes in v3
- Fix indentation in the example section

Changes in v2
- Take over the maintainership for the bindings
- Add interrupt and power supply property in DT bindings

  .../bindings/iio/light/liteon,ltrf216a.yaml   | 50 +++++++++++++++++++
  1 file changed, 50 insertions(+)

create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml

Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +8 -0
new file mode 100644
index 000000000000..f256ff2e744c
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/liteon,ltrf216a.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LTRF216A Ambient Light Sensor
+

Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +10 -0
+maintainers:
+  - Shreeya Patel <[email protected]>
+
+description:
+  Ambient light sensing with an i2c interface.
+
+properties:
+  compatible:
+    const:
+      - liteon,ltrf216a

I assume you figured this out from the build bot error.

Quoted 4 lines

const: liteon,ltrf216a

Please make sure to do what that message from Rob's bot says and test your bindings before sending v7.

Hi Jonathan,

Sorry for the noise, I wasn't seeing this error before when I tested it on my machine. I just did a make clean and was able to see the error after that. I have fixed it now for v7.

Thanks,
Shreeya Patel

Quoted 33 lines
Quoted 33 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +32 -0
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        light-sensor@53 {
+            compatible = "liteon,ltrf216a";
+            reg = <0x53>;
+            vdd-supply = <&vdd_regulator>;
+            interrupt-parent = <&gpio0>;
+            interrupts = <5 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
#5 lore raw

PATCH v6 2/2 iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel

From: Zhigang Shi <[email protected]>

Add initial support for ltrf216a ambient light sensor.

Datasheet: https://bit.ly/3MRTYwY

---

Note :-

This patch generates the below mentioned warnings due to not documenting the 'ltr' string in vendors-prefix.yaml and liteon,ltrf216a.yaml files. The thread for the discussion of not documenting 'ltr' as deprecated prefix can be found here.
https://lore.kernel.org/lkml/[email protected]/

There are released devices which uses ltr216a light sensor and exposes the vendor prefix name as 'ltr' through ACPI. Hence, we would like to add this string under compatible property which would help probe the light sensor driver.

WARNING: DT compatible string "ltr,ltrf216a" appears un-documented -- check ./Documentation/devicetree/bindings/
#474: FILE: drivers/iio/light/ltrf216a.c:421:
+ { .compatible = "ltr,ltrf216a", },

WARNING: DT compatible string vendor "ltr" appears un-documented -- check ./Documentation/devicetree/bindings/vendor-prefixes.yaml #474: FILE: drivers/iio/light/ltrf216a.c:421:
+ { .compatible = "ltr,ltrf216a", },

Changes in v6
- Fix some errors reported by kernel test robot.
- Add protocol details for the datasheet link.
- Remove useless assignments.
- Add unit details for read data delay macro.
- Use pm_sleep_ptr().

Changes in v5
- Add power management support.
- Add reset functionality.
- Use readx_poll_timeout() to get data.
- Cleanup some of the redundant code.
- Update int_time_fac after I2C write is successful.
- Rename mutex to lock.
- Use Reverse Xmas tree pattern for all variable definitions.
- Improve error handling messages and add error codes.
- Add one more MODULE_AUTHOR.
- Remove cleardata which was reading data for infrared light.

Changes in v4
- Add more descriptive comment for mutex lock
- Fix mutex locking in read_raw()
- Use i2c_smbus_read_i2c_block_data()

Changes in v3
- Use u16 instead of u8 for int_time_fac
- Reorder headers in ltrf216a.c file
- Remove int_time_mapping table and use int_time_available

Changes in v2
- Add support for 25ms and 50ms integration time.
- Rename some of the macros as per names given in datasheet
- Add a comment for the mutex lock
- Use read_avail callback instead of attributes and set the appropriate _available bit.
- Use FIELD_PREP() at appropriate places.
- Add a constant lookup table for integration time and reg val
- Use BIT() macro for magic numbers.
- Improve error handling at few places.
- Use get_unaligned_le24() and div_u64()
- Use probe_new() callback and devm functions
- Return errors in probe using dev_err_probe()
- Use DEFINE_SIMPLE_DEV_PM_OPS()
- Correct the formula for lux to use 0.45 instead of 0.8

 drivers/iio/light/Kconfig    |  10 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/ltrf216a.c | 429 +++++++++++++++++++++++++++++++++++
 3 files changed, 440 insertions(+)
 create mode 100644 drivers/iio/light/ltrf216a.c
drivers/iio/light/Kconfig +10 -0
index a62c7b4b8678..6c95431d12f6 100644
@@ -332,6 +332,16 @@ config LTR501
 	  This driver can also be built as a module.  If so, the module
 	  will be called ltr501.
 
+config LTRF216A
+	tristate "Liteon LTRF216A Light Sensor"
+	depends on I2C
+	help
+	  If you say Y or M here, you get support for Liteon LTRF216A
+	  Ambient Light Sensor.
+
+	  If built as a dynamically linked module, it will be called
+	  ltrf216a.
+
 config LV0104CS
 	tristate "LV0104CS Ambient Light Sensor"
 	depends on I2C
drivers/iio/light/Makefile +1 -0
index d10912faf964..6f23817fae6f 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_ISL29125)		+= isl29125.o
 obj-$(CONFIG_JSA1212)		+= jsa1212.o
 obj-$(CONFIG_SENSORS_LM3533)	+= lm3533-als.o
 obj-$(CONFIG_LTR501)		+= ltr501.o
+obj-$(CONFIG_LTRF216A)		+= ltrf216a.o
 obj-$(CONFIG_LV0104CS)		+= lv0104cs.o
 obj-$(CONFIG_MAX44000)		+= max44000.o
 obj-$(CONFIG_MAX44009)		+= max44009.o
drivers/iio/light/ltrf216a.c new file +429 -0

429 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

Quoted 4 lines · Shreeya Patel

From: Zhigang Shi <[email protected]>

Add initial support for ltrf216a ambient light sensor.

This doesn't clarify why regmap API for SMBus can't be used.

...

Quoted 1 line

Datasheet: https://bit.ly/3MRTYwY

These kinds of links tend to disappear, please use the real link.

...

Quoted 1 line

No, the new feature may not be reported.

...

drivers/iio/light/ltrf216a.c line 21

Quoted 11 lines
drivers/iio/light/ltrf216a.c new file +11 -0
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/iopoll.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>

+ blank line

Quoted 1 line

+#include <linux/iio/iio.h>

+ blank line

Quoted 1 line

+#include <asm/unaligned.h>

...

Quoted 3 lines

+/*
+ * Window Factor is needed when the device is under Window glass
+ * with coated tinted ink. This is to compensate the light loss

compensate for the

Quoted 3 lines

+ * due to the lower transmission rate of the window glass and helps
+ * in calculating lux.
+ */

...

drivers/iio/light/ltrf216a.c line 180

Quoted 12 lines
drivers/iio/light/ltrf216a.c new file +11 -0
+static int ltrf216a_set_power_state(struct ltrf216a_data *data, bool on)
+{
+       struct device *dev = &data->client->dev;
+       int ret, suspended;
+
+       if (on) {
+               suspended = pm_runtime_suspended(dev);
+               ret = pm_runtime_get_sync(dev);
 
+               /* Allow one integration cycle before allowing a reading */
+               if (suspended)
+                       msleep(ltrf216a_int_time_reg[0][0]);

Even if the get_sync() failed?

Also, how do you take care about reference count in the case of failed get_sync90?

Quoted 7 lines
drivers/iio/light/ltrf216a.c new file +7 -0
+       } else {
+               pm_runtime_mark_last_busy(dev);
+               ret = pm_runtime_put_autosuspend(dev);
+       }
+
+       return ret;
+}

...

Quoted 19 lines
drivers/iio/light/ltrf216a.c new file +19 -0
+static int ltrf216a_read_raw(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan, int *val,
+                            int *val2, long mask)
+{
+       struct ltrf216a_data *data = iio_priv(indio_dev);
+       int ret;
+
+       mutex_lock(&data->lock);
+
+       switch (mask) {
+       case IIO_CHAN_INFO_PROCESSED:
+               ret = ltrf216a_get_lux(data);
+               if (ret < 0)
+                       break;
+               *val = ret;
+               ret = IIO_VAL_INT;
+               break;
+       case IIO_CHAN_INFO_INT_TIME:
+               ret = ltrf216a_get_int_time(data, val, val2);

drivers/iio/light/ltrf216a.c line 269

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       mutex_unlock(&data->lock);
+
+       return ret;
+}

You can refactor this function to call mutex lock/unlock as many times as cases you have and return directly.

...

drivers/iio/light/ltrf216a.c line 340

Quoted 6 lines
drivers/iio/light/ltrf216a.c new file +6 -0
+       /* reset sensor, chip fails to respond to this, so ignore any errors */
+       ltrf216a_reset(indio_dev);
+
+       ret = pm_runtime_set_active(&client->dev);
+       if (ret)
+               goto error_power_down;

Why do you need to power down here?

drivers/iio/light/ltrf216a.c line 346

Quoted 5 lines
drivers/iio/light/ltrf216a.c new file +5 -0
+       pm_runtime_enable(&client->dev);
+       pm_runtime_set_autosuspend_delay(&client->dev, 5000);
+       pm_runtime_use_autosuspend(&client->dev);
+
+       ltrf216a_set_power_state(data, true);

The below code suggests that you are mixing badly devm_ with non-devm_ APIs, don't do this. You have to group devm_ first followed by non-devm_ calls.

...

drivers/iio/light/ltrf216a.c line 381

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+static int ltrf216a_remove(struct i2c_client *client)
+{
+       struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+       iio_device_unregister(indio_dev);
+       pm_runtime_disable(&client->dev);
+       pm_runtime_set_suspended(&client->dev);
+       ltrf216a_disable(indio_dev);
+
+       return 0;

I believe the ordering of freeing resources and reverting state is not in reverse. See above why.

Quoted 1 line

+}

...

drivers/iio/light/ltrf216a.c line 399

Quoted 2 lines
drivers/iio/light/ltrf216a.c new file +2 -0
+static DEFINE_SIMPLE_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend,
+                               ltrf216a_runtime_resume);

Are you sure you are using proper macro? SIMPLE is for system sleep, while the function names suggest that this is about runtime PM.

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

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

Quoted 13 lines

drivers/iio/light/ltrf216a.c line 340

Quoted 9 lines
drivers/iio/light/ltrf216a.c new file +6 -0
+       /* reset sensor, chip fails to respond to this, so ignore any errors */
+       ltrf216a_reset(indio_dev);
+
+       ret = pm_runtime_set_active(&client->dev);
+       if (ret)
+               goto error_power_down;  

Why do you need to power down here?

Quoted 1 line

+ pm_runtime_enable(&client->dev);

We now have devm_pm_runtime_enable() which will also deal with disabling use_autosuspend for you and should help you sort out some of the ordering.

Quoted 11 lines

drivers/iio/light/ltrf216a.c line 346

Quoted 4 lines
drivers/iio/light/ltrf216a.c new file +4 -0
+       pm_runtime_set_autosuspend_delay(&client->dev, 5000);
+       pm_runtime_use_autosuspend(&client->dev);
+
+       ltrf216a_set_power_state(data, true);  

The below code suggests that you are mixing badly devm_ with non-devm_ APIs, don't do this. You have to group devm_ first followed by non-devm_ calls.

...

Quoted 13 lines

drivers/iio/light/ltrf216a.c line 381

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+static int ltrf216a_remove(struct i2c_client *client)
+{
+       struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+       iio_device_unregister(indio_dev);
+       pm_runtime_disable(&client->dev);
+       pm_runtime_set_suspended(&client->dev);
+       ltrf216a_disable(indio_dev);
+
+       return 0;  

I believe the ordering of freeing resources and reverting state is not in reverse. See above why.

In particular you are calling disable there which is already handled by devm_ unwinding.

Quoted 3 lines
Quoted 2 lines

+}

#8 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel

Hello Shreeya,

drivers/iio/light/ltrf216a.c line 176

Quoted 8 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +8 -0
+static int ltrf216a_set_power_state(struct ltrf216a_data *data, bool on)
+{
+	struct device *dev = &data->client->dev;
+	int ret, suspended;
+
+	if (on) {
+		suspended = pm_runtime_suspended(dev);
+		ret = pm_runtime_get_sync(dev);

The pm_runtime_get_sync() returns 1 if device was already resumed and as Andy said there is no reason not to handle the RPM error. Hence we can rewrite it like this:

if (on) {
	ret = pm_runtime_resume_and_get(dev);
	if (ret < 0) {
		dev_err(&data->client->dev, "failed to resume runtime PM: %d\n", ret);
		return ret;
	}

	/* Allow one integration cycle before allowing a reading */
	if (!ret)
		msleep(ltrf216a_int_time_reg[0][0]);
} else {
	pm_runtime_mark_last_busy(dev);
	ret = pm_runtime_put_autosuspend(dev);
}
Signature · 2 lines
Best regards,
Dmitry
#9 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel

drivers/iio/light/ltrf216a.c line 227

Quoted 6 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +6 -0
+static int ltrf216a_get_lux(struct ltrf216a_data *data)
+{
+	int greendata;
+	u64 lux, div;
+
+	ltrf216a_set_power_state(data, true);

All the power-on errors should be handled.

Signature · 2 lines
Best regards,
Dmitry
#10 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel

drivers/iio/light/ltrf216a.c line 399

Quoted 2 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +2 -0
+static DEFINE_SIMPLE_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend,
+				ltrf216a_runtime_resume);

static DEFINE_RUNTIME_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend,

				 ltrf216a_runtime_resume, NULL);
Signature · 2 lines
Best regards,
Dmitry
#11 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel

drivers/iio/light/ltrf216a.c line 382

Quoted 11 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +11 -0
+static int ltrf216a_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	ltrf216a_disable(indio_dev);
+
+	return 0;
+}

As Jonathan said, there is no need to disable sensor in the remove() since you're using devm_add_action_or_reset().

If you're going to use devm_pm_runtime_enable(), then let's also use devm_iio_device_register() and in this case the ltrf216a_remove() is not needed anymore at all since the removal will be handled by the driver core entirely, i.e. you'll need to drop ltrf216a_remove().

Signature · 2 lines
Best regards,
Dmitry
#12 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel
Quoted 14 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +14 -0
+static int ltrf216a_probe(struct i2c_client *client)
+{
+	struct ltrf216a_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+

drivers/iio/light/ltrf216a.c line 336

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+	mutex_init(&data->lock);
+
+	indio_dev->info = &ltrf216a_info;
+	indio_dev->name = LTRF216A_DRV_NAME;
+	indio_dev->channels = ltrf216a_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ltrf216a_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	/* reset sensor, chip fails to respond to this, so ignore any errors */
+	ltrf216a_reset(indio_dev);

Shouldn't SW resetting be done after enabling sensor? Perhaps that's why it fails to respond?

Signature · 2 lines
Best regards,
Dmitry
#13 lore raw

Re: [PATCH v6 2/2] iio: light: Add support for ltrf216a sensor

Quoted 19 lines · Dmitry Osipenko
Quoted 18 lines · Shreeya Patel
drivers/iio/light/ltrf216a.c new file +18 -0
+static int ltrf216a_probe(struct i2c_client *client)
+{
+	struct ltrf216a_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	mutex_init(&data->lock);
+
+	indio_dev->info = &ltrf216a_info;
+	indio_dev->name = LTRF216A_DRV_NAME;
Quoted 8 lines

drivers/iio/light/ltrf216a.c line 336

Quoted 6 lines
drivers/iio/light/ltrf216a.c new file +6 -0
+	indio_dev->channels = ltrf216a_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ltrf216a_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	/* reset sensor, chip fails to respond to this, so ignore any errors */
+	ltrf216a_reset(indio_dev);

Shouldn't SW resetting be done after enabling sensor? Perhaps that's why it fails to respond?

We tried to reset the device through i2c-tool to see if that works :-

(root@steamdeck ~)# i2cset -f 0 0x53 0x00 0x10
warning! This program can confuse your i2c bus, cause data loss and worse! Dangerous! Writing to a serial eeprom on a memory dimm
may render your memory useless and make your system unbootable! I will write to device file /dev/i2c-0, chip address 0x53, data address 0x00, data 0x10, mode byte.
Continue? [y/n] y
error: write failed
(1)(b+)(root@steamdeck ~)#

But the problem here is that the light sensor resets itself instantaneously while the i2c transaction is still in progress. So it never replies with the proper stop bit that is expected at the end of a transaction. Hence, we decided to ignore the error.

Thanks,
Shreeya Patel

Quoted 1 line