iio: light: veml6030: Generalize hw_init for veml6030 and veml6035
Subsystems (2)
PATCH 1/1 iio: light: veml6030: Generalize hw_init for veml6030 and veml6035
Add a new veml603x_hw_init() function to deduplicate the setup logic for veml6030_hw_init() and veml6035_hw_init().
Additionally, introduce struct veml603x_hw_init_config to store and pass the custom configuration values specific to each device variation.
--- drivers/iio/light/veml6030.c | 98 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 57 deletions(-)
drivers/iio/light/veml6030.c +41 -57
@@ -963,19 +963,38 @@ static int veml6030_regfield_init(struct iio_dev *indio_dev)return 0 ; } -/* -* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, -* persistence to 1 x integration time and the threshold -* interrupt disabled by default. First shutdown the sensor, -* update registers and then power on the sensor. -*/ -static int veml6030_hw_init (struct iio_dev *indio_dev,struct device *dev) +struct veml603x_hw_init_config { +const struct iio_gain_sel_pair *gain_sel; +int gain_sel_size; +int gain_max_scale_int; +int gain_max_scale_nano; +unsigned int reg_als_conf_value; +}; + +static const struct veml603x_hw_init_config veml6030_hw_init_config = { + .gain_sel = veml6030_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6030_gain_sel), + .gain_max_scale_int =2 , + .gain_max_scale_nano =150400000 , + .reg_als_conf_value =0x1001 , +}; + +static const struct veml603x_hw_init_config veml6035_hw_init_config = { + .gain_sel = veml6035_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6035_gain_sel), + .gain_max_scale_int =0 , + .gain_max_scale_nano =409600000 , + .reg_als_conf_value =VEML6035_SENS |VEML6035_CHAN_EN |VEML6030_ALS_SD , +}; + +static int veml603x_hw_init (struct iio_dev *indio_dev,struct device *dev, +const struct veml603x_hw_init_config *cfg) {int ret, val;struct veml6030_data *data =iio_priv (indio_dev); - ret =devm_iio_init_iio_gts (dev,2 ,150400000 , - veml6030_gain_sel,ARRAY_SIZE (veml6030_gain_sel), + ret =devm_iio_init_iio_gts (dev, cfg->gain_max_scale_int, cfg->gain_max_scale_nano, + cfg->gain_sel, cfg->gain_sel_size, veml6030_it_sel,ARRAY_SIZE (veml6030_it_sel), &data->gts);if (ret)
@@ -985,7 +1004,7 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)if (ret)return dev_err_probe (dev, ret,"can't shutdown als\n" ); - ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF ,0x1001 ); + ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF , cfg->reg_als_conf_value);if (ret)return dev_err_probe (dev, ret,"can't setup als configs\n" );
@@ -1019,6 +1038,17 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)return ret; } +/* +* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, +* persistence to 1 x integration time and the threshold +* interrupt disabled by default. First shutdown the sensor, +* update registers and then power on the sensor. +*/ +static int veml6030_hw_init (struct iio_dev *indio_dev,struct device *dev) +{ +return veml603x_hw_init (indio_dev, dev, &veml6030_hw_init_config); +} +/* * Set ALS gain to 1/8, integration time to 100 ms, ALS and WHITE * channel enabled, ALS channel interrupt, PSM enabled,
@@ -1028,53 +1058,7 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)*/ static int veml6035_hw_init (struct iio_dev *indio_dev,struct device *dev) { -int ret, val; -struct veml6030_data *data =iio_priv (indio_dev); - - ret =devm_iio_init_iio_gts (dev,0 ,409600000 , - veml6035_gain_sel,ARRAY_SIZE (veml6035_gain_sel), - veml6030_it_sel,ARRAY_SIZE (veml6030_it_sel), - &data->gts); -if (ret) -return dev_err_probe (dev, ret,"failed to init iio gts\n" ); - - ret =veml6030_als_shut_down (data); -if (ret) -return dev_err_probe (dev, ret,"can't shutdown als\n" ); - - ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF , -VEML6035_SENS |VEML6035_CHAN_EN |VEML6030_ALS_SD ); -if (ret) -return dev_err_probe (dev, ret,"can't setup als configs\n" ); - - ret =regmap_update_bits (data->regmap,VEML6030_REG_ALS_PSM , -VEML6030_PSM |VEML6030_PSM_EN ,0x03 ); -if (ret) -return dev_err_probe (dev, ret,"can't setup default PSM\n" ); - - ret =regmap_write (data->regmap,VEML6030_REG_ALS_WH ,0xFFFF ); -if (ret) -return dev_err_probe (dev, ret,"can't setup high threshold\n" ); - - ret =regmap_write (data->regmap,VEML6030_REG_ALS_WL ,0x0000 ); -if (ret) -return dev_err_probe (dev, ret,"can't setup low threshold\n" ); - - ret =veml6030_als_pwr_on (data); -if (ret) -return dev_err_probe (dev, ret,"can't poweron als\n" ); - - ret =devm_add_action_or_reset (dev, veml6030_als_shut_down_action, data); -if (ret <0 ) -return ret; - -/* Clear stale interrupt status bits if any during start */ - ret =regmap_read (data->regmap,VEML6030_REG_ALS_INT , &val); -if (ret <0 ) -return dev_err_probe (dev, ret, -"can't clear als interrupt status\n" ); - -return 0 ; +return veml603x_hw_init (indio_dev, dev, &veml6035_hw_init_config); }static int veml6030_probe (struct i2c_client *client)
Signature · 1 line
2.53.0
David Lechner
reviewerQuoted 21 lines · Gabriel Braga Lagrotaria
Add a new
veml603x_hw_init()function to deduplicate the setup logic forveml6030_hw_init()andveml6035_hw_init().Additionally, introduce struct
veml603x_hw_init_configto store and pass the custom configuration values specific to each device variation.Signed-off-by Gabriel Braga Lagrotaria <[email protected]>Co-developed-by Ricardo H H Kojo <[email protected]>Signed-off-by Ricardo H H Kojo <[email protected]>Co-developed-by Ellian Carlos <[email protected]>Signed-off-by Ellian Carlos <[email protected]>--- drivers/iio/light/veml6030.c | 98 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 57 deletions(-)drivers/iio/light/veml6030.c +0
-0@@ -963,19 +963,38 @@ static int veml6030_regfield_init(struct iio_dev *indio_dev)return 0 ;
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev) · line 966
Quoted 10 lines
drivers/iio/light/veml6030.c +1
-7} -/* -* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, -* persistence to 1 x integration time and the threshold -* interrupt disabled by default. First shutdown the sensor, -* update registers and then power on the sensor. -*/ -static int veml6030_hw_init (struct iio_dev *indio_dev,struct device *dev) +struct veml603x_hw_init_config {
Generally we try to avoid "x" wildcard in names as it is not particularly future-proof. We can just use veml6030 since that is the driver name.
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev) · line 972
Quoted 6 lines
drivers/iio/light/veml6030.c +6
-0+const struct iio_gain_sel_pair *gain_sel; +int gain_sel_size; +int gain_max_scale_int; +int gain_max_scale_nano; +unsigned int reg_als_conf_value; +};
Would it make sense to add this to the chip info instead of creating new tables?
Quoted 7 lines
drivers/iio/light/veml6030.c +7
-0+ +static const struct veml603x_hw_init_config veml6030_hw_init_config = { + .gain_sel = veml6030_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6030_gain_sel), + .gain_max_scale_int =2 , + .gain_max_scale_nano =150400000 , + .reg_als_conf_value =0x1001 ,
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev) · line 989
Quoted 10 lines
drivers/iio/light/veml6030.c +10
-0+}; + +static const struct veml603x_hw_init_config veml6035_hw_init_config = { + .gain_sel = veml6035_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6035_gain_sel), + .gain_max_scale_int =0 , + .gain_max_scale_nano =409600000 , + .reg_als_conf_value =VEML6035_SENS |VEML6035_CHAN_EN |VEML6030_ALS_SD , +}; +
Or if we want to keep these to avoid duplication...
Quoted 27 lines
drivers/iio/light/veml6030.c +6
-3+static int veml603x_hw_init (struct iio_dev *indio_dev,struct device *dev, +const struct veml603x_hw_init_config *cfg) {int ret, val;struct veml6030_data *data =iio_priv (indio_dev); - ret =devm_iio_init_iio_gts (dev,2 ,150400000 , - veml6030_gain_sel,ARRAY_SIZE (veml6030_gain_sel), + ret =devm_iio_init_iio_gts (dev, cfg->gain_max_scale_int, cfg->gain_max_scale_nano, + cfg->gain_sel, cfg->gain_sel_size, veml6030_it_sel,ARRAY_SIZE (veml6030_it_sel), &data->gts);if (ret)@@ -985,7 +1004,7 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)if (ret)return dev_err_probe (dev, ret,"can't shutdown als\n" ); - ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF ,0x1001 ); + ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF , cfg->reg_als_conf_value);if (ret)return dev_err_probe (dev, ret,"can't setup als configs\n" );@@ -1019,6 +1038,17 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)return ret; } +/*
drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev) · line 1051
Quoted 10 lines
drivers/iio/light/veml6030.c +10
-0+* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, +* persistence to 1 x integration time and the threshold +* interrupt disabled by default. First shutdown the sensor, +* update registers and then power on the sensor. +*/ +static int veml6030_hw_init (struct iio_dev *indio_dev,struct device *dev) +{ +return veml603x_hw_init (indio_dev, dev, &veml6030_hw_init_config); +} +
... then drop these wrapper functions and just put the pointer to the hw_init_config in the chip info.
drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev) · line 1054
Quoted 3 lines
drivers/iio/light/veml6030.c +0
-0/* * Set ALS gain to 1/8, integration time to 100 ms, ALS and WHITE * channel enabled, ALS channel interrupt, PSM enabled,
I think this comment and the one a bit above would mame more sense now on the hw_init_config structs since that is where the actual parameters are defined.
Quoted 4 lines
drivers/iio/light/veml6030.c +0
-0@@ -1028,53 +1058,7 @@ static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)*/ static int veml6035_hw_init (struct iio_dev *indio_dev,struct device *dev) {
...
Quoted 4 lines
drivers/iio/light/veml6030.c +1
-0+return veml603x_hw_init (indio_dev, dev, &veml6035_hw_init_config); }static int veml6030_probe (struct i2c_client *client)
A message that is not in the archive (sent off-list or not yet received): CAOmGE-1108GJobu8nNc=WnV-+WFeW-LF2j0fQ+aMv9Ke27HOqw@mail.gmail.com
Quoted 5 lines · Gabriel Braga Lagrotaria de Oliveira
Thank you for the feedback! We have one remaining question regarding the naming conventions: while adding the
init_configpointer to the chip structure, we noticed the structure name itself (veml603x_chip) includes the 'x' wildcard. Should we rename this to a specific model or keep it as-is for the v2 submission?
Stop top-posting!
Quoted 8 lines
Em seg., 20 de abr. de 2026 às 18:00, David Lechner <[email protected]> escreveu:
Quoted 6 lines · Gabriel Braga Lagrotaria
Quoted 5 lines
Add a new
veml603x_hw_init()function to deduplicate the setup logic forveml6030_hw_init()andveml6035_hw_init().Additionally, introduce struct
veml603x_hw_init_configto store and pass the custom configuration values specific to each device variation.
Signature · 2 lines
With Best Regards, Andy Shevchenko
A message that is not in the archive (sent off-list or not yet received): CAOmGE-3_WYpL0AFpV-NcF=yZ2SBvUrSELuc=VXb=UVKGBbxnvw@mail.gmail.com
Jonathan Cameron
maintainerQuoted 5 lines · On Mon, 27 Apr 2026 20:37:23 -0300 Gabriel Braga Lagrotaria de Oliveira…
Thank you for the feedback! We have one remaining question regarding the naming conventions: while adding the
init_configpointer to the chip structure, we noticed the structure name itself (veml603x_chip) includes the 'x' wildcard. Should we rename this to a specific model or keep it as-is for the v2 submission?
Other than top posting, good question.
If it had a lot of instances I'd argue not worth the effort, but as there are only 4, sure an extra patch to tidy that up would remove one more case of wildcards people might copy in future.
Thanks
Jonathan
Quoted 155 lines
Em seg., 27 de abr. de 2026 às 20:32, Gabriel Braga Lagrotaria de Oliveira < [email protected]> escreveu:
Quoted 151 lines
Thank you for the feedback! We have one remaining question regarding the naming conventions: while adding the
init_configpointer to the chip structure, we noticed the structure name itself (veml603x_chip) includes the 'x' wildcard. Should we rename this to a specific model or keep it as-is for the v2 submission?Em seg., 20 de abr. de 2026 às 18:00, David Lechner <[email protected]> escreveu:
Quoted 142 lines · Gabriel Braga Lagrotaria
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev
Quoted 20 lines
Add a new
veml603x_hw_init()function to deduplicate the setup logic forveml6030_hw_init()andveml6035_hw_init().Additionally, introduce struct
veml603x_hw_init_configto store and pass the custom configuration values specific to each device variation.Signed-off-by Gabriel Braga Lagrotaria <[email protected]>Co-developed-by Ricardo H H Kojo <[email protected]>Signed-off-by Ricardo H H Kojo <[email protected]>Co-developed-by Ellian Carlos <[email protected]>Signed-off-by Ellian Carlos <[email protected]>--- drivers/iio/light/veml6030.c | 98 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 57 deletions(-)drivers/iio/light/veml6030.c +0
-0@@ -963,19 +963,38 @@ static int veml6030_regfield_init(struct iio_dev
*indio_dev)drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev)
Quoted 10 lines
drivers/iio/light/veml6030.c +0
-7return 0 ; } -/* -* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, -* persistence to 1 x integration time and the threshold -* interrupt disabled by default. First shutdown the sensor, -* update registers and then power on the sensor. -*/ -static int veml6030_hw_init(struct iio_dev *indio_dev, struct device*dev)
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev) · line 966
Quoted 1 line
drivers/iio/light/veml6030.c +1
-0+struct veml603x_hw_init_config {Generally we try to avoid "x" wildcard in names as it is not particularly future-proof. We can just use veml6030 since that is the driver name.
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev) · line 972
Quoted 6 lines
drivers/iio/light/veml6030.c +6
-0+const struct iio_gain_sel_pair *gain_sel; +int gain_sel_size; +int gain_max_scale_int; +int gain_max_scale_nano; +unsigned int reg_als_conf_value; +};Would it make sense to add this to the chip info instead of creating new tables?
Quoted 5 lines
drivers/iio/light/veml6030.c +5
-0+ +static const struct veml603x_hw_init_config veml6030_hw_init_config = { + .gain_sel = veml6030_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6030_gain_sel), + .gain_max_scale_int =2 ,drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev)
Quoted 10 lines
drivers/iio/light/veml6030.c +10
-0+ .gain_max_scale_nano = 150400000, + .reg_als_conf_value = 0x1001, +}; + +static const struct veml603x_hw_init_config veml6035_hw_init_config = { + .gain_sel = veml6035_gain_sel, + .gain_sel_size =ARRAY_SIZE (veml6035_gain_sel), + .gain_max_scale_int =0 , + .gain_max_scale_nano =409600000 , + .reg_als_conf_value = VEML6035_SENS | VEML6035_CHAN_EN |
VEML6030_ALS_SD,drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev · line 989
Quoted 2 lines
drivers/iio/light/veml6030.c +2
-0+}; +Or if we want to keep these to avoid duplication...
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev · line 990
Quoted 1 line
drivers/iio/light/veml6030.c +1
-0+static int veml603x_hw_init(struct iio_dev *indio_dev, struct device*dev,
drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev *indio_dev)
Quoted 7 lines
drivers/iio/light/veml6030.c +1
-2+ const struct veml603x_hw_init_config *cfg) {int ret, val;struct veml6030_data *data =iio_priv (indio_dev); - ret = devm_iio_init_iio_gts(dev, 2, 150400000, - veml6030_gain_sel,
ARRAY_SIZE(veml6030_gain_sel),drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev · line 996
Quoted 1 line
drivers/iio/light/veml6030.c +1
-0+ ret = devm_iio_init_iio_gts(dev, cfg->gain_max_scale_int,
cfg->gain_max_scale_nano,drivers/iio/light/veml6030.c static int veml6030_regfield_init(struct iio_dev · line 998
Quoted 2 lines
drivers/iio/light/veml6030.c +1
-0+ cfg->gain_sel, cfg->gain_sel_size, veml6030_it_sel,
ARRAY_SIZE(veml6030_it_sel),drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev
Quoted 3 lines
drivers/iio/light/veml6030.c +0
-0&data->gts);if (ret)@@ -985,7 +1004,7 @@ static int veml6030_hw_init(struct iio_dev
*indio_dev, struct device *dev)drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)
Quoted 5 lines
drivers/iio/light/veml6030.c +1
-1if (ret)return dev_err_probe (dev, ret,"can't shutdown als\n" ); - ret =regmap_write (data->regmap,VEML6030_REG_ALS_CONF ,0x1001 ); + ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF,
cfg->reg_als_conf_value);drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev · line 1009
Quoted 2 lines
drivers/iio/light/veml6030.c +0
-0if (ret) return dev_err_probe(dev, ret, "can't setup alsconfigs\n");
drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev
Quoted 2 lines
drivers/iio/light/veml6030.c +0
-0@@ -1019,6 +1038,17 @@ static int veml6030_hw_init(struct iio_dev
*indio_dev, struct device *dev)drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)
Quoted 10 lines
drivers/iio/light/veml6030.c +7
-0return ret; } +/* +* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, +* persistence to 1 x integration time and the threshold +* interrupt disabled by default. First shutdown the sensor, +* update registers and then power on the sensor. +*/ +static int veml6030_hw_init(struct iio_dev *indio_dev, struct device*dev)
drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev) · line 1051
Quoted 4 lines
drivers/iio/light/veml6030.c +4
-0+{ +return veml603x_hw_init (indio_dev, dev, &veml6030_hw_init_config); +} +... then drop these wrapper functions and just put the pointer to the
hw_init_configin the chip info.drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev) · line 1054
Quoted 3 lines
drivers/iio/light/veml6030.c +0
-0/* * Set ALS gain to 1/8, integration time to 100 ms, ALS and WHITE * channel enabled, ALS channel interrupt, PSM enabled, I think this comment and the one a bit above would mame more sense now on the
hw_init_configstructs since that is where the actual parameters are defined.drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev
Quoted 1 line
drivers/iio/light/veml6030.c +0
-0@@ -1028,53 +1058,7 @@ static int veml6030_hw_init(struct iio_dev
*indio_dev, struct device *dev)drivers/iio/light/veml6030.c static int veml6030_hw_init(struct iio_dev · line 1059
Quoted 2 lines
drivers/iio/light/veml6030.c +0
-0*/ static int veml6035_hw_init(struct iio_dev *indio_dev, struct device*dev)
Quoted 1 line
drivers/iio/light/veml6030.c +0
-0{...
Quoted 4 lines
+ return
veml603x_hw_init(indio_dev, dev, &veml6035_hw_init_config);
}static int
veml6030_probe(struct i2c_client *client)