Skip to contentSkip to conversation

Add LTRF216A Driver

PATCH Superseded Shreeya Patel 2 participants 7 messages May 3, 2022 – May 7, 2022

Subsystems (2)

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add LTRF216A Driver

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

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 (3):
dt-bindings: vendor-prefixes: Add 'ltr' as deprecated vendor prefix
dt-bindings: Document ltrf216a light sensor bindings
iio: light: Add support for ltrf216a sensor

 .../bindings/iio/light/liteon,ltrf216a.yaml   |  51 +++
 .../devicetree/bindings/vendor-prefixes.yaml  |   3 +
 drivers/iio/light/Kconfig                     |  10 +
 drivers/iio/light/Makefile                    |   1 +
 drivers/iio/light/ltrf216a.c                  | 343 ++++++++++++++++++
 5 files changed, 408 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 v3 1/3 dt-bindings: vendor-prefixes: Add 'ltr' as deprecated vendor prefix

'liteon' is the correct vendor prefix for devices released by LITE-ON Technology Corp. But one of the released device which uses ltr216a light sensor exposes the vendor prefix name as 'ltr' through ACPI.

Hence, add 'ltr' as a deprecated vendor prefix which would suppress the following warning in case the compatible string used in ltrf216a driver is "ltr,ltrf216a"

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

---

Changes in v2
- Add vendor prefix name as per the alphabetical order.

 Documentation/devicetree/bindings/vendor-prefixes.yaml | 3 +++
 1 file changed, 3 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +3 -0
index 01430973ecec..02f94fba03b6 100644
@@ -716,6 +716,9 @@ patternProperties:
     description: Loongson Technology Corporation Limited
   "^lsi,.*":
     description: LSI Corp. (LSI Logic)
+  "^ltr,.*":
+    description: LITE-ON Technology Corp.
+    deprecated: true
   "^lwn,.*":
     description: Liebherr-Werk Nenzing GmbH
   "^lxa,.*":
Signature · 1 line
2.30.2
#2 lore raw

PATCH v3 2/3 dt-bindings: Document ltrf216a light sensor bindings

In reply to Shreeya Patel

Add devicetree bindings for ltrf216a ambient light sensor.

---

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   | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +51 -0
new file mode 100644
index 000000000000..1389639cd7fd
@@ -0,0 +1,51 @@
+# 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:
+    oneOf:
+      - const: liteon,ltrf216a
+      - const: ltr,ltrf216a
+        deprecated: true
+  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
#3 lore raw

PATCH v3 3/3 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: gitlab.steamos.cloud/shreeya/iio/-/blob/main/LTRF216A.pdf

---

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 | 343 +++++++++++++++++++++++++++++++++++
 3 files changed, 354 insertions(+)
 create mode 100644 drivers/iio/light/ltrf216a.c
drivers/iio/light/Kconfig +10 -0
index a62c7b4b8678..33d2b24ba1da 100644
@@ -318,6 +318,16 @@ config SENSORS_LM3533
 	  changes. The ALS-control output values can be set per zone for the
 	  three current output channels.
 
+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 LTR501
 	tristate "LTR-501ALS-01 light sensor"
 	depends on I2C
drivers/iio/light/Makefile +1 -0
index d10912faf964..8fa91b9fe5b6 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_SENSORS_ISL29028)	+= isl29028.o
 obj-$(CONFIG_ISL29125)		+= isl29125.o
 obj-$(CONFIG_JSA1212)		+= jsa1212.o
 obj-$(CONFIG_SENSORS_LM3533)	+= lm3533-als.o
+obj-$(CONFIG_LTRF216A)		+= ltrf216a.o
 obj-$(CONFIG_LTR501)		+= ltr501.o
 obj-$(CONFIG_LV0104CS)		+= lv0104cs.o
 obj-$(CONFIG_MAX44000)		+= max44000.o
drivers/iio/light/ltrf216a.c new file +343 -0

343 lines not on this page · raw message

Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v3 3/3] iio: light: Add support for ltrf216a sensor

Hi Jonathan,

Just one comment inline related to your previous review.

Quoted 123 lines · Shreeya Patel

From: Zhigang Shi <[email protected]>

Add initial support for ltrf216a ambient light sensor.

Datasheet: gitlab.steamos.cloud/shreeya/iio/-/blob/main/LTRF216A.pdf

---

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 | 343 +++++++++++++++++++++++++++++++++++
  3 files changed, 354 insertions(+)

create mode 100644 drivers/iio/light/ltrf216a.c

drivers/iio/light/Kconfig +10 -0
index a62c7b4b8678..33d2b24ba1da 100644
@@ -318,6 +318,16 @@ config SENSORS_LM3533
  	  changes. The ALS-control output values can be set per zone for the
  	  three current output channels.
 
+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 LTR501
  	tristate "LTR-501ALS-01 light sensor"
  	depends on I2C
drivers/iio/light/Makefile +1 -0
index d10912faf964..8fa91b9fe5b6 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_SENSORS_ISL29028)	+= isl29028.o
  obj-$(CONFIG_ISL29125)		+= isl29125.o
  obj-$(CONFIG_JSA1212)		+= jsa1212.o
  obj-$(CONFIG_SENSORS_LM3533)	+= lm3533-als.o
+obj-$(CONFIG_LTRF216A)		+= ltrf216a.o
  obj-$(CONFIG_LTR501)		+= ltr501.o
  obj-$(CONFIG_LV0104CS)		+= lv0104cs.o
  obj-$(CONFIG_MAX44000)		+= max44000.o
drivers/iio/light/ltrf216a.c new file +46 -0
new file mode 100644
index 000000000000..1ad1eb4a4c6d
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * LTRF216A Ambient Light Sensor
+ *
+ * Copyright (C) 2021 Lite-On Technology Corp (Singapore)
+ * Author: Shi Zhigang <[email protected]>
+ *
+ * IIO driver for LTRF216A (7-bit I2C slave address 0x53).
+ */
+
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/iio/iio.h>
+#include <asm/unaligned.h>
+
+#define LTRF216A_DRV_NAME "ltrf216a"
+
+#define LTRF216A_MAIN_CTRL		0x00
+
+#define LTRF216A_ALS_DATA_STATUS	BIT(3)
+#define LTRF216A_ALS_ENABLE_MASK	BIT(1)
+
+#define LTRF216A_ALS_MEAS_RES		0x04
+#define LTRF216A_MAIN_STATUS		0x07
+#define LTRF216A_CLEAR_DATA_0		0x0A
+
+#define LTRF216A_ALS_DATA_0		0x0D
+
+static const int ltrf216a_int_time_available[5][2] = {
+	{0, 400000},
+	{0, 200000},
+	{0, 100000},
+	{0, 50000},
+	{0, 25000},
+};
+
+static const int ltrf216a_int_time_reg[5][2] = {
+	{400, 0x03},
+	{200, 0x13},
+	{100, 0x22},

drivers/iio/light/ltrf216a.c line 56

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+	{50, 0x31},
+	{25, 0x40},
+};
+
+struct ltrf216a_data {
+	struct i2c_client *client;
+	u32 int_time;
+	u16 int_time_fac;
+	u8 als_gain_fac;
+	struct mutex mutex; /* Protect read and write operations */

I wasn't really sure about your comment related to the lock description here. I see we are using these locks in read_raw and write_raw functions only and hence I've added the above comment.

Thanks,
Shreeya Patel

maintainer
#5 lore raw

Re: [PATCH v3 3/3] iio: light: Add support for ltrf216a sensor

Quoted 2 lines · On Tue, 3 May 2022 22:37:49 +0530 Shreeya Patel <[email protected]>…

Hi Jonathan,

Hi Shreeya,

Quoted 14 lines

Just one comment inline related to your previous review.

Quoted 10 lines · Shreeya Patel

From: Zhigang Shi <[email protected]>

Add initial support for ltrf216a ambient light sensor.

Datasheet: gitlab.steamos.cloud/shreeya/iio/-/blob/main/LTRF216A.pdf

---

...

Quoted 11 lines

drivers/iio/light/ltrf216a.c line 56

Quoted 6 lines
drivers/iio/light/ltrf216a.c new file +6 -0
+struct ltrf216a_data {
+	struct i2c_client *client;
+	u32 int_time;
+	u16 int_time_fac;
+	u8 als_gain_fac;
+	struct mutex mutex; /* Protect read and write operations */  

I wasn't really sure about your comment related to the lock description here.
I see we are using these locks in read_raw and write_raw functions only and hence I've added the above comment.

A lock should always ensure consistency of data (either in software or in hardware registers) so that we don't end up with odd results due to race conditions between multiple writers / readers.

The comment for a lock should call out what 'data' is being protected.

In this particular case I'm not sure what that is.

Take the *_get_lux() call in read_raw()

That performs a pair of calls to _read_data(). The _read_data() calls just check for valid data and then read the channels. The i2c accesses will be protected by the underlying bus locks and I can't otherwise see anything in those calls that needs protecting with locks (all the data is local).

Finally we have some maths done with data->als_gain_fac and data->int_time_fac

als_gain_fac is currently a constant in the driver (it's set only in probe I think).

int_time_fac is more interesting.
That is set alongside a register write in _set_int_time().

So I 'think' the entire purpose of the lock is to ensure that the value of integration time doesn't not change whilst a reading is progress (so we can do the right maths).

Hence the comment should be something along the lines of

/*
* Ensure cached value of integration time is consistent with hardware setting
* and remains constant during a measurement of Lux.
*/

This extra detail makes it easy to tell where the lock must be taken which is very useful for anyone modifying the driver in the future.

If they expand the scope of the lock, then they should also update the documentation to match.

Quoted 5 lines

Thanks,
Shreeya Patel

maintainer
#6 lore raw

Re: [PATCH v3 3/3] iio: light: Add support for ltrf216a sensor

In reply to Shreeya Patel
Quoted 8 lines · On Tue, 3 May 2022 20:13:54 +0530 Shreeya Patel <[email protected]>…

From: Zhigang Shi <[email protected]>

Add initial support for ltrf216a ambient light sensor.

Datasheet: gitlab.steamos.cloud/shreeya/iio/-/blob/main/LTRF216A.pdf

One locking bug, otherwise just the lock documentation to resolve.

Thanks,

Jonathan

drivers/iio/light/ltrf216a.c line 56

Quoted 7 lines
drivers/iio/light/ltrf216a.c new file +7 -0
+
+struct ltrf216a_data {
+	struct i2c_client *client;
+	u32 int_time;
+	u16 int_time_fac;
+	u8 als_gain_fac;
+	struct mutex mutex; /* Protect read and write operations */

See other branch of thread for feedback on that comment

Thanks for highlighting that btw. I'd forgotten about it so might have missed it on a fresh read through.

Quoted 13 lines
drivers/iio/light/ltrf216a.c new file +12 -0
+};
+
+/* open air. need to update based on TP transmission rate. */
+#define WIN_FAC	1
+
 
+
+static int ltrf216a_read_data(struct ltrf216a_data *data, u8 addr)
+{
+	int i, ret = -1, tries = 25;
+	u8 buf[3];
+
+	while (tries--) {

drivers/iio/light/ltrf216a.c line 163

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+		ret = i2c_smbus_read_byte_data(data->client, LTRF216A_MAIN_STATUS);
+		if (ret < 0)
+			return ret;
+		if (ret & LTRF216A_ALS_DATA_STATUS)
+			break;
+		msleep(20);
+	}
+
+	for (i = 0; i < 3; i++) {
+		ret = i2c_smbus_read_byte_data(data->client, addr);

Might be worth seeing if the device copes with i2c_smbus_read_i2c_block_data()
The datasheet doesn't mention it though so it might well not work.

Quoted 14 lines
drivers/iio/light/ltrf216a.c new file +13 -0
+		if (ret < 0)
+			return ret;
+		buf[i] = ret;
+		addr++;
+	}
+
+	return get_unaligned_le24(&buf[0]);
+}
 
+
+static int ltrf216a_read_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, int *val,
+			     int *val2, long mask)
+{

drivers/iio/light/ltrf216a.c line 203

Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10 -0
+	int ret;
+	struct ltrf216a_data *data = iio_priv(indio_dev);
+
+	mutex_lock(&data->mutex);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		ret = ltrf216a_get_lux(data);
+		if (ret < 0)
+			return ret;

Check your locking. This path doesn't unlock the mutex.

Quoted 17 lines
drivers/iio/light/ltrf216a.c new file +16 -0
+		*val = ret;
+		ret = IIO_VAL_INT;
+		break;
+	case IIO_CHAN_INFO_INT_TIME:
+		ret = ltrf216a_get_int_time(data, val, val2);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+
 
+