Add LTRF216A Driver
Subsystems (2)
v2 cover Add LTRF216A Driver
This patchset adds support for ltrf216a Ambient Light Sensor and documents the DT bindings for the same.
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 | 52 +++ .../devicetree/bindings/vendor-prefixes.yaml | 3 + drivers/iio/light/Kconfig | 10 + drivers/iio/light/Makefile | 1 + drivers/iio/light/ltrf216a.c | 349 ++++++++++++++++++ 5 files changed, 415 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
PATCH v2 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
@@ -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
PATCH v2 2/3 dt-bindings: Document ltrf216a light sensor bindings
In reply to Shreeya PatelAdd devicetree bindings for ltrf216a ambient light sensor.
---
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 | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +52 -0
@@ -0,0 +1,52 @@ +# 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
Re: [PATCH v2 2/3] dt-bindings: Document ltrf216a light sensor bindings
On 21/04/2022 16:01, Shreeya Patel wrote:
Thank you for your patch. There is something to discuss/improve.
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml line 46
Quoted 13 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +13
-0+additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + + #address-cells = <1>; + #size-cells = <0>; + + light-sensor@53 { + compatible = "liteon,ltrf216a";
You have here unusual indentation - looks like 8 spaces, while the rest of example is properly indented with 4 spaces. Please use 4 spaces for entire DTS example.
All rest looks good, so with indentation fixes:
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml line 52
Quoted 6 lines
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml new file +6
-0+ reg = <0x53>; + vdd-supply = <&vdd_regulator>; + interrupt-parent = <&gpio0>; + interrupts = <5 IRQ_TYPE_LEVEL_LOW>; + }; + };
Best regards,
Krzysztof
PATCH v2 3/3 iio: light: Add support for ltrf216a sensor
In reply to Shreeya PatelFrom: Zhigang Shi <[email protected]>
Add initial support for ltrf216a ambient light sensor.
Datasheet: gitlab.steamos.cloud/shreeya/iio/-/blob/main/LTRF216A.pdf
---
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 | 349 +++++++++++++++++++++++++++++++++++ 3 files changed, 360 insertions(+) create mode 100644 drivers/iio/light/ltrf216a.c
drivers/iio/light/Kconfig +10 -0
@@ -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
@@ -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 +349 -0
349 lines not on this page · raw message
Signature · 1 line
2.30.2
Jonathan Cameron
maintainerRe: [PATCH v2 3/3] iio: light: Add support for ltrf216a sensor
Quoted 8 lines · On Thu, 21 Apr 2022 19:31:33 +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
Co-developed-by Shreeya Patel <[email protected]>Signed-off-by Shreeya Patel <[email protected]>Signed-off-by Zhigang Shi <[email protected]>
Hi Shreeya,
Looking pretty good. Just a few minor things in here - I very nearly just made the changes whilst applying but the one about reusing the available array is slightly more complex than I like to do without bouncing it back to the author.
Thanks,
Jonathan
Quoted 38 lines
---
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
- Useread_availcallback instead of attributes and set the appropriate_availablebit.
- UseFIELD_PREP()at appropriate places.
- Add a constant lookup table for integration time and reg val
- UseBIT()macro for magic numbers.
- Improve error handling at few places.
- Useget_unaligned_le24()anddiv_u64()
- Useprobe_new()callback and devm functions
- Return errors in probe usingdev_err_probe()
- UseDEFINE_SIMPLE_DEV_PM_OPS()
- Correct the formula for lux to use 0.45 instead of 0.8drivers/iio/light/Kconfig | 10 + drivers/iio/light/Makefile | 1 + drivers/iio/light/ltrf216a.c | 349 +++++++++++++++++++++++++++++++++++ 3 files changed, 360 insertions(+) create mode 100644 drivers/iio/light/ltrf216a.cdrivers/iio/light/ltrf216a.c new file +6
-0@@ -0,0 +1,349 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* LTRF216A Ambient Light Sensor +* +* Copyright (C) 2021 Lite-On Technology Corp (Singapore) +* Author: Shi Zhigang <[email protected]>
drivers/iio/light/ltrf216a.c line 16
Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10
-0+* +* IIO driver for LTRF216A (7-bit I2C slave address 0x53). +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/i2c.h> +#include <linux/mutex.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h>
I don't think you are using anything from iio/sysfs.h any more so please drop this header (unless I'm missing something!)
drivers/iio/light/ltrf216a.c line 22
Quoted 6 lines
drivers/iio/light/ltrf216a.c new file +6
-0+#include <linux/mod_devicetable.h> +#include <linux/bitfield.h> +#include <linux/pm.h> +#include <linux/delay.h> +#include <asm/unaligned.h> +
Where no other reason to have a particular order for headers IIO preference is 3 blocks of headers. First set are the non IIO related ones in alphabetical order. Second block typically IIO specific ones. Final block the asm includes
So here best order is something like
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/mutex.h>
#include <linux/pm.h>
#include <linux/iio/iio.h>
#include <asm/unaligned.h>
drivers/iio/light/ltrf216a.c line 36
Quoted 3 lines
drivers/iio/light/ltrf216a.c new file +3
-0+#define LTRF216A_ALS_DATA_0 0x0D + +static const int int_time_mapping[] = {400000 ,200000 ,100000 ,50000 ,25000 };
You should use the array below for the same matching purpose as this one and avoid duplicating data.
Quoted 13 lines
drivers/iio/light/ltrf216a.c new file +13
-0+ +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 59
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; + u8 int_time_fac; + u8 als_gain_fac; + struct mutex mutex; /* Protect read and write operations */
This could probably have been more descriptive. I think you are also ensuring that the cached state and the device state are kept in sync.
Quoted 4 lines
drivers/iio/light/ltrf216a.c new file +2
-0+}; +
drivers/iio/light/ltrf216a.c line 185
Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10
-0+static int ltrf216a_get_lux (struct ltrf216a_data *data) +{ +int greendata, cleardata; +u64 lux, div; + + greendata =ltrf216a_read_data (data,LTRF216A_ALS_DATA_0 ); + cleardata =ltrf216a_read_data (data,LTRF216A_CLEAR_DATA_0 ); + +if (greendata <0 || cleardata <0 ) { +return -EINVAL ;
As this is an error case and you correctly return directly there is no need to have else. That will reduce indentation and allow last line to simply be
return div_u64(lux, div);
Quoted 10 lines
drivers/iio/light/ltrf216a.c new file +10
-0+ + }else { + lux = greendata *45 *WIN_FAC *100 ; + div = data->als_gain_fac * data->int_time_fac *100 ; + lux =div_u64 (lux, div); + } + +return lux; +} +