iio:light: add driver for veml6046x00 RGBIR color sensor
Andreas Klinger
maintainerv6 cover iio:light: add driver for veml6046x00 RGBIR color sensor
This patchset adds an IIO driver for Vishay veml6046x00 RGBIR color sensor
Changes in v6:
- Thanks to the in-depth review of Andy many datatype improvements were realized.
- According to Jonathans review change the channel types from IIO_LIGHT to IIO_INTENSITY.
Changes in v5:
- Thanks to the feedback of Andy and further explanations of Jonathan many improvements could be implemented.
- add documentation in kernel-doc format
- iio_push_to_buffers_with_ts() is not used as also testing against linux-stable where it is not available so far.
Changes in v4:
- implement feedback from Andy and Jonathan
- implement feedback from vendor (reading interrupt register as bulk read)
Changes in v3:
- implement a lot of feedback from Jonathan
- change scale value to real factor of lux per raw count instead of hardware gain
- optimize code by using more lookup tables
- remove unimplemented threshold functionality
Changes in v2:
- fix missing include for example in vishay,veml6046x00.yaml
Andreas Klinger (3):
dt-bindings: iio: light: veml6046x00: add color sensor
iio: light: add support for veml6046x00 RGBIR color sensor
MAINTAINER: add maintainer for veml6046x00
.../iio/light/vishay,veml6046x00.yaml | 51 + MAINTAINERS | 6 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/veml6046x00.c | 1037 +++++++++++++++++ 5 files changed, 1108 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml create mode 100644 drivers/iio/light/veml6046x00.c
Signature · 1 line
2.39.5
Andreas Klinger
maintainerPATCH v6 1/3 dt-bindings: iio: light: veml6046x00: add color sensor
Add a new compatible for Vishay high accuracy RGBIR color sensor veml6046x00.
--- .../iio/light/vishay,veml6046x00.yaml | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml
Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml new file +51 -0
@@ -0,0 +1,51 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/light/vishay,veml6046x00.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Vishay VEML6046X00 High accuracy RGBIR color sensor + +maintainers: + - Andreas Klinger <[email protected]> + +description: + VEML6046X00 datasheet at https://www.vishay.com/docs/80173/veml6046x00.pdf + +properties: + compatible: + enum: + - vishay,veml6046x00 + + reg: + maxItems: 1 + + vdd-supply: true + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + color-sensor@29 { + compatible = "vishay,veml6046x00"; + reg = <0x29>; + vdd-supply = <&vdd_reg>; + interrupt-parent = <&gpio2>; + interrupts = <3 IRQ_TYPE_EDGE_FALLING>; + }; + }; +...
Signature · 1 line
2.39.5
Andreas Klinger
maintainerPATCH v6 2/3 iio: light: add support for veml6046x00 RGBIR color sensor
In reply to Andreas KlingerAdd Vishay VEML6046X00 high accuracy RGBIR color sensor.
This sensor provides three colour (red, green and blue) as well as one infrared (IR) channel through I2C.
Support direct and buffered mode.
An optional interrupt for signaling green colour threshold underflow or overflow is not supported so far.
--- drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/veml6046x00.c | 1037 +++++++++++++++++++++++++++++++ 3 files changed, 1051 insertions(+) create mode 100644 drivers/iio/light/veml6046x00.c
drivers/iio/light/Kconfig +13 -0
@@ -724,6 +724,19 @@ config VEML6040 To compile this driver as a module, choose M here: the module will be called veml6040. +config VEML6046X00 + tristate "VEML6046X00 RGBIR color sensor" + select REGMAP_I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + depends on I2C + help + Say Y here if you want to build a driver for the Vishay VEML6046X00 + high accuracy RGBIR color sensor. + + To compile this driver as a module, choose M here: the + module will be called veml6046x00. + config VEML6070 tristate "VEML6070 UV A light sensor" depends on I2C
drivers/iio/light/Makefile +1 -0
@@ -67,6 +67,7 @@ obj-$(CONFIG_VCNL4035) += vcnl4035.o obj-$(CONFIG_VEML3235) += veml3235.o obj-$(CONFIG_VEML6030) += veml6030.o obj-$(CONFIG_VEML6040) += veml6040.o +obj-$(CONFIG_VEML6046X00) += veml6046x00.o obj-$(CONFIG_VEML6070) += veml6070.o obj-$(CONFIG_VEML6075) += veml6075.o obj-$(CONFIG_VL6180) += vl6180.o
drivers/iio/light/veml6046x00.c new file +1037 -0
1,037 lines not on this page · raw message
Signature · 1 line
2.39.5
Andreas Klinger
maintainerPATCH v6 3/3 MAINTAINER: add maintainer for veml6046x00
In reply to Andreas KlingerAdd maintainer for Vishay veml6046x00 RGBIR color sensor driver and dt binding.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -26424,6 +26424,12 @@ S: Maintained F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml F: drivers/iio/light/veml6030.c +VISHAY VEML6046X00 RGBIR COLOR SENSOR DRIVER +M: Andreas Klinger <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml +F: drivers/iio/light/veml6046x00.c + VISHAY VEML6075 UVA AND UVB LIGHT SENSOR DRIVER M: Javier Carrasco <[email protected]> S: Maintained
Signature · 1 line
2.39.5
Re: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
In reply to Andreas KlingerQuoted 9 lines · Andreas Klinger
Add Vishay VEML6046X00 high accuracy RGBIR color sensor.
This sensor provides three colour (red, green and blue) as well as one infrared (IR) channel through I2C.
Support direct and buffered mode.
An optional interrupt for signaling green colour threshold underflow or overflow is not supported so far.
...
drivers/iio/light/veml6046x00.c line 66
Quoted 4 lines
drivers/iio/light/veml6046x00.c new file +4
-0+#define VEML6046X00_GAIN_1 0x0 +#define VEML6046X00_GAIN_2 0x1 +#define VEML6046X00_GAIN_0_66 0x2 +#define VEML6046X00_GAIN_0_5 0x3
Is it defined as hexadecimal in the datasheet? Otherwise use plain decimal numbers.
...
drivers/iio/light/veml6046x00.c line 481
Quoted 13 lines
drivers/iio/light/veml6046x00.c new file +13
-0+static int veml6046x00_get_it_usec (struct veml6046x00_data *data,unsigned int *it_usec) +{ +int ret; +unsigned int reg; + + ret =regmap_field_read (data->rf.it, ®); +if (ret) +return ret; + +if (reg >=ARRAY_SIZE (veml6046x00_it)) +return -EINVAL ; + + *it_usec = (unsigned int )veml6046x00_it[reg][1 ];
Is this casting needed? According to the C standard unsigned has higher rank than signed.
Quoted 2 lines
(quoted diff) +2
-0+ return IIO_VAL_INT_PLUS_MICRO; +}
...
drivers/iio/light/veml6046x00.c line 636
Quoted 8 lines
drivers/iio/light/veml6046x00.c new file +7
-0+static int veml6046x00_wait_data_available (struct iio_dev *iio,unsigned int usecs) +{ +struct veml6046x00_data *data =iio_priv (iio); +int ret; + + ret =veml6046x00_read_data_ready (data); +if (ret ==0 ) {
What's wrong with the regular pattern, i.e.
if (ret) return ret;
?
Quoted 6 lines
drivers/iio/light/veml6046x00.c new file +6
-0+fsleep (usecs); + ret =veml6046x00_read_data_ready (data); + } + +return ret; +}
...
drivers/iio/light/veml6046x00.c line 706
Quoted 13 lines
drivers/iio/light/veml6046x00.c new file +13
-0+ ret =veml6046x00_wait_data_available (iio, it_usec *4 ); +if (ret <0 ) +return ret; +if (ret ==0 ) +return -EAGAIN ; + +if (!iio_device_claim_direct (iio)) +return -EBUSY ; + + ret =regmap_bulk_read (data->regmap, addr, ®,sizeof (reg)); +iio_device_release_direct (iio); +if (ret) +return ret;
All error paths above are missing the below runtime PM calls, is it on purpose? If so, comment is a must to explain what's going on here.
Quoted 12 lines
drivers/iio/light/veml6046x00.c new file +12
-0+pm_runtime_mark_last_busy (dev); +pm_runtime_put_autosuspend (dev); + + *val =le16_to_cpu (reg); + +return IIO_VAL_INT ; + +no_data: +pm_runtime_mark_last_busy (dev); +pm_runtime_put_autosuspend (dev); + +return -EAGAIN ;
...
drivers/iio/light/veml6046x00.c line 847
Quoted 10 lines
drivers/iio/light/veml6046x00.c new file +10
-0+static irqreturn_t veml6046x00_trig_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *iio = pf->indio_dev; +struct veml6046x00_data *data =iio_priv (iio); +int ret; +struct { +__le16 chans[4 ]; +aligned_s64 timestamp; + } scan = {};
Do you need this (nullification) assignment?
Quoted 12 lines
drivers/iio/light/veml6046x00.c new file +12
-0+ ret =regmap_bulk_read (data->regmap,VEML6046X00_REG_R , + &scan.chans,sizeof (scan.chans)); +if (ret) + goto done; + +iio_push_to_buffers_with_timestamp (iio, &scan,iio_get_time_ns (iio)); + +done: +iio_trigger_notify_done (iio->trig); + +return IRQ_HANDLED ; +}
...
Quoted 5 lines
drivers/iio/light/veml6046x00.c new file +5
-0+static int veml6046x00_validate_part_id (struct veml6046x00_data *data) +{ +struct device *dev =regmap_get_device (data->regmap); +unsigned int part_id; +int ret;
drivers/iio/light/veml6046x00.c line 876
Quoted 10 lines
drivers/iio/light/veml6046x00.c new file +10
-0+__le16 reg; + + ret =regmap_bulk_read (data->regmap,VEML6046X00_REG_ID , + ®,sizeof (reg)); +if (ret) +return dev_err_probe (dev, ret,"Failed to read ID\n" ); + + part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);
For 0 it will print 0 and not 0x0000. Is it okay?
Quoted 2 lines
(quoted diff) +2
-0+ return 0; +}
...
drivers/iio/light/veml6046x00.c line 887
Quoted 7 lines
drivers/iio/light/veml6046x00.c new file +7
-0+static int veml6046x00_setup_device (struct iio_dev *iio) +{ +struct veml6046x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; +__le16 reg16; +u8 reg[2 ] = {VEML6046X00_CONF0_AF ,0x00 };
0x00 looks like a part of the reg. If so, this should be actually typed as __le16. Otherwise, in case this is just a simple terminator, use (decimal) 0.
drivers/iio/light/veml6046x00.c line 892
Quoted 4 lines
drivers/iio/light/veml6046x00.c new file +4
-0+ ret =regmap_bulk_write (data->regmap,VEML6046X00_REG_CONF0 , + reg,sizeof (reg)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set configuration\n" );
So, this actually will be as simple as
reg16 = cpu_to_le16(VEML6046X00_CONF0_AF); ret = regmap_bulk_write(data->regmap, VEML6046X00_REG_CONF0, ®16, sizeof(reg16)); if (ret) return dev_err_probe(dev, ret, "Failed to set configuration\n");
Quoted 19 lines
drivers/iio/light/veml6046x00.c new file +19
-0+ reg16 =cpu_to_le16 (0 ); + ret =regmap_bulk_write (data->regmap,VEML6046X00_REG_THDL , + ®16,sizeof (reg16)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set low threshold\n" ); + + reg16 =cpu_to_le16 (U16_MAX ); + ret =regmap_bulk_write (data->regmap,VEML6046X00_REG_THDH , + ®16,sizeof (reg16)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set high threshold\n" ); + + ret =regmap_bulk_read (data->regmap,VEML6046X00_REG_INT , + ®16,sizeof (reg16)); +if (ret) +return dev_err_probe (dev, ret,"Failed to clear interrupts\n" ); + +return 0 ; +}
...
drivers/iio/light/veml6046x00.c line 1013
Quoted 4 lines
drivers/iio/light/veml6046x00.c new file +4
-0+static const struct of_device_id veml6046x00_of_match[] = { + { + .compatible ="vishay,veml6046x00" , + },
Can be just a single line.
{ .compatible = "vishay,veml6046x00" },Quoted 2 lines
(quoted diff) +2
-0+ { } +};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
In reply to Andreas KlingerQuoted 11 lines · On Tue, 15 Jul 2025 10:58:09 +0200 Andreas Klinger <[email protected]> wrote:
Add Vishay VEML6046X00 high accuracy RGBIR color sensor.
This sensor provides three colour (red, green and blue) as well as one infrared (IR) channel through I2C.
Support direct and buffered mode.
An optional interrupt for signaling green colour threshold underflow or overflow is not supported so far.
Signed-off-by Andreas Klinger <[email protected]>
A few minor things inline. We are now at the point where anything other than fixes is 6.18 material so no great rush.
Quoted 22 lines
drivers/iio/light/veml6046x00.c new file +16
-0@@ -0,0 +1,1037 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* VEML6046X00 High Accuracy RGBIR Color Sensor +* +* Copyright (c) 2025 Andreas Klinger <[email protected]> +*/ + +#include <linux/array_size.h> +#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/dev_printk.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/mod_devicetable.h>
drivers/iio/light/veml6046x00.c line 26
Quoted 10 lines
drivers/iio/light/veml6046x00.c new file +10
-0+#include <linux/pm_runtime.h> +#include <linux/regmap.h> +#include <linux/time.h> +#include <linux/types.h> +#include <linux/units.h> + +#include <asm/byteorder.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h>
Check these includes. This one for example is only use for custom attributes and you don't seem to have any.
Quoted 21 lines
drivers/iio/light/veml6046x00.c new file +19
-0+#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> + +static int veml6046x00_single_read (struct iio_dev *iio, +enum iio_modifier modifier,int *val) +{ +struct veml6046x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +unsigned int addr, it_usec; +int ret; +__le16 reg; + +switch (modifier) { +case IIO_MOD_LIGHT_RED : + addr =VEML6046X00_REG_R ; +break ; +case IIO_MOD_LIGHT_GREEN : + addr =VEML6046X00_REG_G ; +break ;
drivers/iio/light/veml6046x00.c line 669
Quoted 10 lines
drivers/iio/light/veml6046x00.c new file +10
-0+case IIO_MOD_LIGHT_BLUE : + addr =VEML6046X00_REG_B ; +break ; +case IIO_MOD_LIGHT_IR : + addr =VEML6046X00_REG_IR ; +break ; +default : +return -EINVAL ; + } + ret =pm_runtime_resume_and_get (dev);
Will be interesting to consider the new ACQUIRE stuff that will (I think) hit cleanup.h in the new cycle can be applied to runtime pm. Note I'm not asking for a change, but more saying I might look into updating this code during the next cycle.
drivers/iio/light/veml6046x00.c line 682
Quoted 13 lines
drivers/iio/light/veml6046x00.c new file +13
-0+if (ret) +return ret; + + ret =veml6046x00_get_it_usec (data, &it_usec); +if (ret <0 ) { +dev_err (dev,"Failed to get integration time ret: %d" , ret); + goto no_data; + } + + ret =regmap_field_write (data->rf.mode,1 ); +if (ret) { +dev_err (dev,"Failed to write mode ret: %d" , ret); + goto no_data;
I'm struggling a bit with why these error paths result in the runtime pm calls, but some below do not. Seems to be a lack of consistency that will leave missbalanced counts.
drivers/iio/light/veml6046x00.c line 695
Quoted 13 lines
drivers/iio/light/veml6046x00.c new file +13
-0+ } + + ret =regmap_field_write (data->rf.trig,1 ); +if (ret) { +dev_err (dev,"Failed to write trigger ret: %d" , ret); + goto no_data; + } + +/* integration time + 12.5 % to ensure completion */ +fsleep (it_usec + it_usec /8 ); + + ret =veml6046x00_wait_data_available (iio, it_usec *4 ); +if (ret <0 )
here for example.
Quoted 36 lines
drivers/iio/light/veml6046x00.c new file +33
-0+return ret; +if (ret ==0 ) +return -EAGAIN ; + +if (!iio_device_claim_direct (iio)) +return -EBUSY ; + + ret =regmap_bulk_read (data->regmap, addr, ®,sizeof (reg)); +iio_device_release_direct (iio); +if (ret) +return ret; + +pm_runtime_mark_last_busy (dev); +pm_runtime_put_autosuspend (dev); + + *val =le16_to_cpu (reg); + +return IIO_VAL_INT ; + +no_data: +pm_runtime_mark_last_busy (dev); +pm_runtime_put_autosuspend (dev); + +return -EAGAIN ; +} +static int veml6046x00_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev; +struct veml6046x00_data *data; +struct iio_dev *iio; +struct regmap *regmap; +int ret; +
drivers/iio/light/veml6046x00.c
Quoted 10 lines
drivers/iio/light/veml6046x00.c new file +10
-0+ regmap =devm_regmap_init_i2c (i2c, &veml6046x00_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap),"Failed to set regmap\n" ); + + iio =devm_iio_device_alloc (dev,sizeof (*data)); +if (!iio) +return -ENOMEM ; + + data =iio_priv (iio); +/* struct iio_dev is retrieved via get_drv_data(). */
dev_get_drvdata()
drivers/iio/light/veml6046x00.c line 982
Quoted 12 lines
drivers/iio/light/veml6046x00.c new file +11
-0+i2c_set_clientdata (i2c, iio); + data->regmap = regmap; + + ret =devm_iio_triggered_buffer_setup (dev, iio,NULL , + veml6046x00_trig_handler, + &veml6046x00_buffer_setup_ops); +if (ret) +return dev_err_probe (dev, ret, +"Failed to register triggered buffer" ); + +pm_runtime_mark_last_busy (dev);
Given this will now merge after 6.17-rc1 you could drop this as now incorporated in the next call. It's harmless and I'll hopefully remember to tweak all code for next cycle that has pm_runtime_mark_last_busy() in it.
Quoted 8 lines
drivers/iio/light/veml6046x00.c new file +8
-0+pm_runtime_put_autosuspend (dev); + + ret =devm_iio_device_register (dev, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to register iio device" ); + +return 0 ; +}
Andreas Klinger
maintainerRe: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
In reply to Andy ShevchenkoHi Andy,
thanks for the detailed review again. Some comments below.
Andy Shevchenko <[email protected]> schrieb am Mi, 16. Jul 10:34:
Quoted 7 lines
drivers/iio/light/veml6046x00.c line 66
Quoted 4 lines
drivers/iio/light/veml6046x00.c new file +4
-0+#define VEML6046X00_GAIN_1 0x0 +#define VEML6046X00_GAIN_2 0x1 +#define VEML6046X00_GAIN_0_66 0x2 +#define VEML6046X00_GAIN_0_5 0x3Is it defined as hexadecimal in the datasheet? Otherwise use plain decimal numbers.
It's in the datasheet defined exactly the way i did.
Quoted 9 lines
Quoted 9 lines
drivers/iio/light/veml6046x00.c new file +9
-0+static int veml6046x00_validate_part_id (struct veml6046x00_data *data) +{ +struct device *dev =regmap_get_device (data->regmap); +unsigned int part_id; +int ret; +__le16 reg; + + ret =regmap_bulk_read (data->regmap,VEML6046X00_REG_ID , + ®,sizeof (reg));
Quoted 8 lines
drivers/iio/light/veml6046x00.c line 876
Quoted 6 lines
drivers/iio/light/veml6046x00.c new file +6
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to read ID\n" ); + + part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);For 0 it will print 0 and not
0x0000. Is it okay?
I just tried and it prints 0x00 if the part_id is 0.
Best regards,
Andreas
Re: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
Quoted 1 line · Andreas Klinger
Andy Shevchenko <[email protected]> schrieb am Mi, 16. Jul 10:34:
...
Quoted 9 lines
Quoted 7 lines
drivers/iio/light/veml6046x00.c line 66
Quoted 4 lines
drivers/iio/light/veml6046x00.c new file +4
-0+#define VEML6046X00_GAIN_1 0x0 +#define VEML6046X00_GAIN_2 0x1 +#define VEML6046X00_GAIN_0_66 0x2 +#define VEML6046X00_GAIN_0_5 0x3Is it defined as hexadecimal in the datasheet? Otherwise use plain decimal numbers.
It's in the datasheet defined exactly the way i did.
OK!
...
Quoted 7 lines
Quoted 5 lines
drivers/iio/light/veml6046x00.c line 876
Quoted 3 lines
drivers/iio/light/veml6046x00.c new file +3
-0+ part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);For 0 it will print 0 and not
0x0000. Is it okay?I just tried and it prints
0x00if thepart_idis 0.
This is interesting... So it's not 0, nor 0x0000?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andreas Klinger
maintainerRe: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
Hi Andy,
Andy Shevchenko <[email protected]> schrieb am Do, 21. Aug 12:43:
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
drivers/iio/light/veml6046x00.c line 876
Quoted 3 lines
drivers/iio/light/veml6046x00.c new file +3
-0+ part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);For 0 it will print 0 and not
0x0000. Is it okay?I just tried and it prints
0x00if thepart_idis 0.This is interesting... So it's not 0, nor
0x0000?
No. It prints 0x00 on my BeagleBoneBlack with kernel 6.16.0-rc5.
Andreas
Re: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
Quoted 9 lines · Andreas Klinger
Hi Andy,
Andy Shevchenko <[email protected]> schrieb am Do, 21. Aug 12:43:
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/iio/light/veml6046x00.c line 876
Quoted 3 lines
drivers/iio/light/veml6046x00.c new file +3
-0+ part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);For 0 it will print 0 and not
0x0000. Is it okay?
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I just tried and it prints
0x00if thepart_idis 0.This is interesting... So it's not 0, nor
0x0000?No. It prints
0x00on my BeagleBoneBlack with kernel 6.16.0-rc5.
I think this makes sense because of the '#' -flag. The "0x" is appended because of it, and this consumes 2 characters from the 4 character field, leaving only 2 chars left for the value.
What I find interesting is that gcc on my PC does:
printf("%#04x\n", 0);
printf("%#04x\n", 1);
printf("%#04x\n", 10);
printf("%#04x\n", 17);00000x010x0a0x11
gcc version 15.2.1 20250808 (Red Hat 15.2.1-1) (GCC)
It'd be nice to learn why the zero is treated differently? Andy, did you have some insight as you asked this?
Yours,
-- Matti
Re: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
Fri, Aug 22, 2025 at 08:39:35AM +0300, Matti Vaittinen kirjoitti:
Quoted 30 lines · Andreas Klinger
Quoted 7 lines
Andy Shevchenko <[email protected]> schrieb am Do, 21. Aug 12:43:
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/iio/light/veml6046x00.c line 876
Quoted 3 lines
drivers/iio/light/veml6046x00.c new file +3
-0+ part_id =le16_to_cpu (reg); +if (part_id !=0x0001 ) +dev_info (dev,"Unknown ID %#04x\n" , part_id);For 0 it will print 0 and not
0x0000. Is it okay?Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I just tried and it prints
0x00if thepart_idis 0.This is interesting... So it's not 0, nor
0x0000?No. It prints
0x00on my BeagleBoneBlack with kernel 6.16.0-rc5.I think this makes sense because of the '#' -flag. The "0x" is appended because of it, and this consumes 2 characters from the 4 character field, leaving only 2 chars left for the value.
What I find interesting is that gcc on my PC does:
printf("%#04x\n", 0); printf("%#04x\n", 1); printf("%#04x\n", 10); printf("%#04x\n", 17);0000
0x010x0a0x11
Quoted 4 lines
gcc version 15.2.1 20250808 (Red Hat 15.2.1-1) (GCC)
It'd be nice to learn why the zero is treated differently? Andy, did you have some insight as you asked this?
Nice, we have so many variants now on how to treat 0 with %#x cases... My understanding was that it should print plain 0 without even 0x prefix, but since we specify 04 it prints 4 of them, so this behaviour seems consistent to me, the 0x00 case seems buggy.
Now to the standards...
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
Read this:
"For x or X conversion specifiers, a **non-zero** result shall have 0x (or 0X) prefixed to it."
** -- is my marking to make a point.
So, 0x00 is a bug in Andreas case and has to be fixed somewhere.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 2/3] iio: light: add support for veml6046x00 RGBIR color sensor
Fri, Aug 22, 2025 at 10:23:56AM +0300, Andy Shevchenko kirjoitti:
Quoted 3 lines
Fri, Aug 22, 2025 at 08:39:35AM +0300, Matti Vaittinen kirjoitti:
Quoted 2 lines · Andreas Klinger
Quoted 1 line
Andy Shevchenko <[email protected]> schrieb am Do, 21. Aug 12:43:
+Cc: Rasmus (I am wondering what your opinion is on %#x for plain 0)
...
Quoted 40 lines
Quoted 28 lines
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ part_id = le16_to_cpu(reg); + if (part_id != 0x0001) + dev_info(dev, "Unknown ID %#04x\n", part_id);For 0 it will print 0 and not
0x0000. Is it okay?I just tried and it prints
0x00if thepart_idis 0.This is interesting... So it's not 0, nor
0x0000?No. It prints
0x00on my BeagleBoneBlack with kernel 6.16.0-rc5.I think this makes sense because of the '#' -flag. The "0x" is appended because of it, and this consumes 2 characters from the 4 character field, leaving only 2 chars left for the value.
What I find interesting is that gcc on my PC does:
printf("%#04x\n", 0); printf("%#04x\n", 1); printf("%#04x\n", 10); printf("%#04x\n", 17);0000
0x010x0a0x11Quoted 4 lines
gcc version 15.2.1 20250808 (Red Hat 15.2.1-1) (GCC)
It'd be nice to learn why the zero is treated differently? Andy, did you have some insight as you asked this?
Nice, we have so many variants now on how to treat 0 with %#x cases... My understanding was that it should print plain 0 without even 0x prefix, but since we specify 04 it prints 4 of them, so this behaviour seems consistent to me, the
0x00case seems buggy.Now to the standards...
Quoted 8 lines
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
Read this:
"For x or X conversion specifiers, a **non-zero** result shall have 0x (or 0X) prefixed to it."** -- is my marking to make a point.
So,
0x00is a bug in Andreas case and has to be fixed somewhere.
Note, even if we want to deviate the kernel implementation, the 0x00 is still buggy since 04 defines *minimum* digits of the *value* to be printed. 0x00 prints only 2 out of 4.
Signature · 2 lines
With Best Regards, Andy Shevchenko