* [PATCH] iio: light: veml6030: Generalize hw_init for veml6030 and veml6035
@ 2026-04-20 20:14 Gabriel Braga Lagrotaria
2026-04-20 21:00 ` David Lechner
0 siblings, 1 reply; 4+ messages in thread
From: Gabriel Braga Lagrotaria @ 2026-04-20 20:14 UTC (permalink / raw)
To: javier.carrasco.cruz, jic23, dlechner, nuno.sa, andy
Cc: Ricardo H H Kojo, Ellian Carlos, linux-iio
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.
Signed-off-by: Gabriel Braga Lagrotaria <gabrielblo@ime.usp.br>
Co-developed-by: Ricardo H H Kojo <ricardo.kojo@ime.usp.br>
Signed-off-by: Ricardo H H Kojo <ricardo.kojo@ime.usp.br>
Co-developed-by: Ellian Carlos <elliancarlos@gmail.com>
Signed-off-by: Ellian Carlos <elliancarlos@gmail.com>
---
drivers/iio/light/veml6030.c | 98 +++++++++++++++---------------------
1 file changed, 41 insertions(+), 57 deletions(-)
diff --git a/drivers/iio/light/veml6030.c b/drivers/iio/light/veml6030.c
index 6bcacae38..84a551683 100644
--- a/drivers/iio/light/veml6030.c
+++ b/drivers/iio/light/veml6030.c
@@ -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)
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] iio: light: veml6030: Generalize hw_init for veml6030 and veml6035
2026-04-20 20:14 [PATCH] iio: light: veml6030: Generalize hw_init for veml6030 and veml6035 Gabriel Braga Lagrotaria
@ 2026-04-20 21:00 ` David Lechner
[not found] ` <CAOmGE-1108GJobu8nNc=WnV-+WFeW-LF2j0fQ+aMv9Ke27HOqw@mail.gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: David Lechner @ 2026-04-20 21:00 UTC (permalink / raw)
To: Gabriel Braga Lagrotaria, javier.carrasco.cruz, jic23, nuno.sa,
andy
Cc: Ricardo H H Kojo, Ellian Carlos, linux-iio
On 4/20/26 3:14 PM, Gabriel Braga Lagrotaria wrote:
> 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.
>
> Signed-off-by: Gabriel Braga Lagrotaria <gabrielblo@ime.usp.br>
> Co-developed-by: Ricardo H H Kojo <ricardo.kojo@ime.usp.br>
> Signed-off-by: Ricardo H H Kojo <ricardo.kojo@ime.usp.br>
> Co-developed-by: Ellian Carlos <elliancarlos@gmail.com>
> Signed-off-by: Ellian Carlos <elliancarlos@gmail.com>
> ---
> drivers/iio/light/veml6030.c | 98 +++++++++++++++---------------------
> 1 file changed, 41 insertions(+), 57 deletions(-)
>
> diff --git a/drivers/iio/light/veml6030.c b/drivers/iio/light/veml6030.c
> index 6bcacae38..84a551683 100644
> --- a/drivers/iio/light/veml6030.c
> +++ b/drivers/iio/light/veml6030.c
> @@ -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 {
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.
> + 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?
> +
> +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,
> +};
> +
Or if we want to keep these to avoid duplication...
> +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);
> +}
> +
... then drop these wrapper functions and just put the pointer to the
hw_init_config in the chip info.
> /*
> * 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.
> @@ -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)
> {
...
> + return veml603x_hw_init(indio_dev, dev, &veml6035_hw_init_config);
> }
>
> static int veml6030_probe(struct i2c_client *client)
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-28 18:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-20 20:14 [PATCH] iio: light: veml6030: Generalize hw_init for veml6030 and veml6035 Gabriel Braga Lagrotaria
2026-04-20 21:00 ` David Lechner
[not found] ` <CAOmGE-1108GJobu8nNc=WnV-+WFeW-LF2j0fQ+aMv9Ke27HOqw@mail.gmail.com>
2026-04-28 8:07 ` Andy Shevchenko
[not found] ` <CAOmGE-3_WYpL0AFpV-NcF=yZ2SBvUrSELuc=VXb=UVKGBbxnvw@mail.gmail.com>
2026-04-28 18:07 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox