From: Joshua Crofts <joshua.crofts1@gmail.com>
To: "Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org,
Joshua Crofts <joshua.crofts1@gmail.com>
Subject: [PATCH 2/2] iio: light: veml3328: add support for new device
Date: Sat, 16 May 2026 23:50:54 +0200 [thread overview]
Message-ID: <20260516-veml3328-v1-2-1d4b663e2fe3@gmail.com> (raw)
In-Reply-To: <20260516-veml3328-v1-0-1d4b663e2fe3@gmail.com>
Add support for the Vishay VEML3328 RGB/IR light sensor communicating
via I2C (SMBus compatible).
Also add a new entry for said driver into Kconfig and Makefile.
Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
---
drivers/iio/light/Kconfig | 11 ++
drivers/iio/light/Makefile | 1 +
drivers/iio/light/veml3328.c | 405 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 417 insertions(+)
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index eff33e456c70..1c00bb0de6c8 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -699,6 +699,17 @@ config VEML3235
To compile this driver as a module, choose M here: the
module will be called veml3235.
+config VEML3328
+ tristate "VEML3328 RGBCIR light sensor"
+ select REGMAP_I2C
+ depends on I2C
+ help
+ Say Y here if you want to build a driver for the Vishay VEML3328
+ RGB IR light sensor.
+
+ To compile this driver as a module, choose M here: the
+ module will be called veml3328
+
config VEML6030
tristate "VEML6030 and VEML6035 ambient light sensors"
select REGMAP_I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index c0048e0d5ca8..e03c6e23a9a4 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -65,6 +65,7 @@ obj-$(CONFIG_US5182D) += us5182d.o
obj-$(CONFIG_VCNL4000) += vcnl4000.o
obj-$(CONFIG_VCNL4035) += vcnl4035.o
obj-$(CONFIG_VEML3235) += veml3235.o
+obj-$(CONFIG_VEML3328) += veml3328.o
obj-$(CONFIG_VEML6030) += veml6030.o
obj-$(CONFIG_VEML6040) += veml6040.o
obj-$(CONFIG_VEML6046X00) += veml6046x00.o
diff --git a/drivers/iio/light/veml3328.c b/drivers/iio/light/veml3328.c
new file mode 100644
index 000000000000..9eb5429c813d
--- /dev/null
+++ b/drivers/iio/light/veml3328.c
@@ -0,0 +1,405 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Vishay VEML3328 RGBCIR light sensor driver
+ *
+ * Copyright (c) 2026 Joshua Crofts <joshua.crofts1@gmail.com>
+ *
+ * Datasheet: https://www.vishay.com/docs/84968/veml3328.pdf
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+
+#include <linux/iio/iio.h>
+
+#define VEML3328_REG_CONF 0x00
+#define VEML3328_REG_ID 0x0c
+#define VEML3328_REG_DATA_C 0x04
+#define VEML3328_REG_DATA_R 0x05
+#define VEML3328_REG_DATA_G 0x06
+#define VEML3328_REG_DATA_B 0x07
+#define VEML3328_REG_DATA_IR 0x08
+
+#define VEML3328_IT_MASK GENMASK(5, 4)
+#define VEML3328_GAIN_MASK GENMASK(11, 10)
+
+#define VEML3328_ID_VAL 0x28
+
+#define VEML3328_CONF_SD0 BIT(0)
+#define VEML3328_CONF_SD1 BIT(15)
+#define VEML3328_SHUTDOWN (VEML3328_CONF_SD0 | VEML3328_CONF_SD1)
+
+struct veml3328_data {
+ struct regmap *regmap;
+ struct device *dev;
+ struct mutex lock;
+};
+
+static const struct regmap_config veml3328_regmap_config = {
+ .name = "veml3328",
+ .reg_bits = 8,
+ .val_bits = 16,
+ .max_register = VEML3328_REG_ID,
+ .val_format_endian = REGMAP_ENDIAN_LITTLE,
+};
+
+#define VEML3328_CHAN_SPEC(_color, _addr) { \
+ .type = IIO_INTENSITY, \
+ .modified = 1, \
+ .channel2 = IIO_MOD_LIGHT_##_color, \
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | \
+ BIT(IIO_CHAN_INFO_SCALE), \
+ .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | \
+ BIT(IIO_CHAN_INFO_SCALE), \
+ .address = _addr, \
+}
+
+static const struct iio_chan_spec veml3328_channels[] = {
+ VEML3328_CHAN_SPEC(CLEAR, VEML3328_REG_DATA_C),
+ VEML3328_CHAN_SPEC(RED, VEML3328_REG_DATA_R),
+ VEML3328_CHAN_SPEC(GREEN, VEML3328_REG_DATA_G),
+ VEML3328_CHAN_SPEC(BLUE, VEML3328_REG_DATA_B),
+ VEML3328_CHAN_SPEC(IR, VEML3328_REG_DATA_IR),
+};
+
+/* scale values per datasheet */
+static const int veml3328_scale_vals[][2] = {
+ { 0, 500000 },
+ { 1, 0 },
+ { 2, 0 },
+ { 4, 0 },
+};
+
+/* integration times in microseconds */
+static const int veml3328_it_times[][2] = {
+ { 0, 50000 },
+ { 0, 100000 },
+ { 0, 200000 },
+ { 0, 400000 },
+};
+
+static int veml3328_power_down(struct veml3328_data *data)
+{
+ return regmap_update_bits(data->regmap, VEML3328_REG_CONF,
+ VEML3328_SHUTDOWN, VEML3328_SHUTDOWN);
+}
+
+static int veml3328_power_up(struct veml3328_data *data)
+{
+ int ret;
+
+ ret = regmap_update_bits(data->regmap, VEML3328_REG_CONF,
+ VEML3328_SHUTDOWN, 0);
+ if (ret < 0)
+ return ret;
+
+ fsleep(veml3328_it_times[3][1]);
+
+ return 0;
+}
+
+static void veml3328_power_down_action(void *data)
+{
+ veml3328_power_down(data);
+}
+
+static int veml3328_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct veml3328_data *data = iio_priv(indio_dev);
+ struct regmap *regmap = data->regmap;
+ unsigned int reg_val;
+ int ret;
+ int reg;
+
+ guard(mutex)(&data->lock);
+
+ ret = pm_runtime_resume_and_get(data->dev);
+ if (ret < 0)
+ return ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ ret = regmap_read(regmap, chan->address, ®_val);
+ if (ret < 0)
+ goto exit;
+
+ *val = reg_val;
+ ret = IIO_VAL_INT;
+ break;
+
+ case IIO_CHAN_INFO_INT_TIME:
+ ret = regmap_read(regmap, VEML3328_REG_CONF, ®_val);
+ if (ret < 0)
+ goto exit;
+
+ reg = FIELD_GET(VEML3328_IT_MASK, reg_val);
+ if (reg >= ARRAY_SIZE(veml3328_it_times)) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ *val = veml3328_it_times[reg][0];
+ *val2 = veml3328_it_times[reg][1];
+ ret = IIO_VAL_INT_PLUS_MICRO;
+ break;
+
+ case IIO_CHAN_INFO_SCALE:
+ ret = regmap_read(regmap, VEML3328_REG_CONF, ®_val);
+ if (ret < 0)
+ goto exit;
+
+ reg = FIELD_GET(VEML3328_GAIN_MASK, reg_val);
+ if (reg >= ARRAY_SIZE(veml3328_scale_vals)) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ *val = veml3328_scale_vals[reg][0];
+ *val2 = veml3328_scale_vals[reg][1];
+ ret = IIO_VAL_INT_PLUS_MICRO;
+ break;
+
+ default:
+ ret = -EINVAL;
+ }
+
+exit:
+ pm_runtime_put_autosuspend(data->dev);
+
+ return ret;
+}
+
+static int veml3328_read_avail(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ const int **vals, int *type, int *length,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_INT_TIME:
+ *length = ARRAY_SIZE(veml3328_it_times) * 2;
+ *vals = (const int *)veml3328_it_times;
+ *type = IIO_VAL_INT_PLUS_MICRO;
+ return IIO_AVAIL_LIST;
+
+ case IIO_CHAN_INFO_SCALE:
+ *length = ARRAY_SIZE(veml3328_scale_vals) * 2;
+ *vals = (const int *)veml3328_scale_vals;
+ *type = IIO_VAL_INT_PLUS_MICRO;
+ return IIO_AVAIL_LIST;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int veml3328_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct veml3328_data *data = iio_priv(indio_dev);
+ struct regmap *regmap = data->regmap;
+ int ret;
+ int i;
+
+ guard(mutex)(&data->lock);
+
+ ret = pm_runtime_resume_and_get(data->dev);
+ if (ret < 0)
+ return ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_INT_TIME:
+ if (val != 0) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(veml3328_it_times); i++) {
+ if (veml3328_it_times[i][1] == val2)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(veml3328_it_times)) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ ret = regmap_update_bits(regmap, VEML3328_REG_CONF,
+ VEML3328_IT_MASK,
+ FIELD_PREP(VEML3328_IT_MASK, i));
+ break;
+
+ case IIO_CHAN_INFO_SCALE:
+ for (i = 0; i < ARRAY_SIZE(veml3328_scale_vals); i++) {
+ if (val == veml3328_scale_vals[i][0] &&
+ val2 == veml3328_scale_vals[i][1])
+ break;
+ }
+
+ if (i == ARRAY_SIZE(veml3328_scale_vals)) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
+ ret = regmap_update_bits(regmap, VEML3328_REG_CONF,
+ VEML3328_GAIN_MASK,
+ FIELD_PREP(VEML3328_GAIN_MASK, i));
+
+ break;
+
+ default:
+ ret = -EINVAL;
+ }
+
+exit:
+ pm_runtime_put_autosuspend(data->dev);
+
+ return ret;
+}
+
+static int veml3328_write_raw_get_fmt(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ case IIO_CHAN_INFO_INT_TIME:
+ return IIO_VAL_INT_PLUS_MICRO;
+ default:
+ return -EINVAL;
+ }
+}
+
+static const struct iio_info veml3328_info = {
+ .read_raw = veml3328_read_raw,
+ .write_raw = veml3328_write_raw,
+ .read_avail = veml3328_read_avail,
+ .write_raw_get_fmt = veml3328_write_raw_get_fmt,
+};
+
+static int veml3328_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ struct veml3328_data *data;
+ struct iio_dev *indio_dev;
+ unsigned int reg_val;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ data = iio_priv(indio_dev);
+ i2c_set_clientdata(client, indio_dev);
+ data->dev = dev;
+
+ data->regmap = devm_regmap_init_i2c(client, &veml3328_regmap_config);
+ if (IS_ERR(data->regmap))
+ return dev_err_probe(dev, PTR_ERR(data->regmap),
+ "Failed to initialize regmap\n");
+
+ ret = devm_mutex_init(dev, &data->lock);
+ if (ret)
+ return ret;
+
+ ret = devm_regulator_get_enable(dev, "vdd");
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable regulator\n");
+
+ ret = regmap_read(data->regmap, VEML3328_REG_ID, ®_val);
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Failed to read ID register\n");
+
+ if ((reg_val & 0xff) != VEML3328_ID_VAL)
+ return dev_err_probe(dev, -ENODEV, "Invalid device ID\n");
+
+ ret = veml3328_power_up(data);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to power on sensor\n");
+
+ ret = devm_add_action_or_reset(dev, veml3328_power_down_action, data);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register teardown\n");
+
+ indio_dev->name = "veml3328";
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->info = &veml3328_info;
+ indio_dev->channels = veml3328_channels;
+ indio_dev->num_channels = ARRAY_SIZE(veml3328_channels);
+
+ pm_runtime_set_active(dev);
+ pm_runtime_set_autosuspend_delay(dev, 2000);
+ pm_runtime_use_autosuspend(dev);
+
+ ret = devm_pm_runtime_enable(dev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static int veml3328_runtime_suspend(struct device *dev)
+{
+ struct veml3328_data *data = iio_priv(dev_get_drvdata(dev));
+ int ret;
+
+ ret = veml3328_power_down(data);
+ if (ret < 0)
+ dev_err(dev, "Failed to suspend: %d\n", ret);
+
+ return ret;
+}
+
+static int veml3328_runtime_resume(struct device *dev)
+{
+ struct veml3328_data *data = iio_priv(dev_get_drvdata(dev));
+ int ret;
+
+ ret = veml3328_power_up(data);
+ if (ret < 0)
+ dev_err(dev, "Failed to resume: %d\n", ret);
+
+ return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(veml3328_pm_ops, veml3328_runtime_suspend,
+ veml3328_runtime_resume, NULL);
+
+static const struct of_device_id veml3328_of_match[] = {
+ { .compatible = "vishay,veml3328" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, veml3328_of_match);
+
+static const struct i2c_device_id veml3328_id[] = {
+ { "veml3328" },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, veml3328_id);
+
+static struct i2c_driver veml3328_driver = {
+ .driver = {
+ .name = "veml3328",
+ .of_match_table = veml3328_of_match,
+ .pm = pm_ptr(&veml3328_pm_ops),
+ },
+ .probe = veml3328_probe,
+ .id_table = veml3328_id,
+};
+module_i2c_driver(veml3328_driver);
+
+MODULE_AUTHOR("Joshua Crofts <joshua.crofts1@gmail.com>");
+MODULE_DESCRIPTION("VEML3328 RGB Light Sensor");
+MODULE_LICENSE("GPL");
--
2.34.1
next prev parent reply other threads:[~2026-05-16 21:51 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-16 21:50 [PATCH 0/2] iio: light: veml3328: add support for new sensor Joshua Crofts
2026-05-16 21:50 ` [PATCH 1/2] iio: light: veml3328: add devicetree binding " Joshua Crofts
2026-05-17 8:38 ` Krzysztof Kozlowski
2026-05-17 10:38 ` Joshua Crofts
2026-05-17 13:08 ` Jonathan Cameron
2026-05-17 13:10 ` Jonathan Cameron
2026-05-17 14:26 ` Joshua Crofts
2026-05-18 14:18 ` Joshua Crofts
2026-05-18 15:26 ` Jonathan Cameron
2026-05-16 21:50 ` Joshua Crofts [this message]
2026-05-16 22:30 ` [PATCH 2/2] iio: light: veml3328: add support for new device Joshua Crofts
2026-05-17 7:53 ` Andy Shevchenko
2026-05-17 11:39 ` Joshua Crofts
2026-05-17 13:34 ` Jonathan Cameron
2026-05-17 14:21 ` Joshua Crofts
2026-05-17 15:55 ` Jonathan Cameron
2026-05-17 17:11 ` Joshua Crofts
2026-05-18 6:44 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260516-veml3328-v1-2-1d4b663e2fe3@gmail.com \
--to=joshua.crofts1@gmail.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome