* [PATCH v4 0/2] iio: light: Add Capella CM3217 ambient light sensor
@ 2026-11-27 23:38 Patrick Fischer via B4 Relay
2026-11-27 23:38 ` [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217 Patrick Fischer via B4 Relay
2026-11-27 23:38 ` [PATCH v4 2/2] iio: light: Add Capella CM3217 ambient light sensor driver Patrick Fischer via B4 Relay
0 siblings, 2 replies; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-27 23:38 UTC (permalink / raw)
To: Jonathan Cameron, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kevin Tsai, Svyatoslav Ryhel, linux-iio,
devicetree, linux-tegra, linux-kernel, Krzysztof Kozlowski,
Patrick Fischer
The Capella CM3217 is a small ambient light sensor without registers.
NVIDIA used it in the Tegra Note 7 and the Shield Tablet. Mainline has
no driver for it: cm32181 and cm3232 expect registers and an ID, the
CM3217 has neither.
Patch 1 adds the binding, patch 2 the driver with raw value, scale and
integration time. The datasheet is not public, so the bit fields come
from NVIDIA's downstream drivers and the scale from Capella's published
detection range. Patch 2 explains both.
Tested on a Tegra Note 7 (P1640) with v7.3 and the driver built as a
module, the device created through new_device. In room light all 32
combinations of integration time and scale gave the same illuminance
within the count resolution (6.0 to 7.3 lux), and the first read after
each change matched the second one. The raw counts scale linearly with
both fields. Svyatoslav Ryhel also tried it on the P1988 variant of the
Tegra Note 7.
Probing through the device tree works with the real supply (vdd_dev,
Palmas LDO6), and unloading and reloading the module twice brought the
device back each time. Next to a phone light sensor the readings were
about 7 to 20 times lower. NVIDIA's Android HAL for this tablet
multiplies the count by 2.1505 at the same 2 T/100 ms setting, which is
7.2 times the scale here, so most of that is the dark cover window. With
that factor the tablet was within 15 % of the phone in bright light but
a factor of three lower in dim room light; I could not tell whether that
was my setup.
v3 was run again on the P1640, through the device tree with both
addresses in reg and through new_device without a device tree node: in
both cases the driver binds to 0x10 and claims 0x11, and all 32
settings give the same scale and integration time values as v2. With
the device tree, unloading and reloading the module twice freed and
claimed the second address again. Suspend/resume was not tested.
There is no MAINTAINERS entry: drivers/iio/light/cm* is covered by the
Capella entry and the binding by IIO. I am happy to add one if you
prefer.
The device tree node for the Tegra Note 7 will follow with Svyatoslav's
tegratab series.
Tool use (see Documentation/process/generated-content.rst): this series
was prepared with an LLM coding assistant (Claude Code). It read the
NVIDIA downstream drivers and Capella's product data, wrote the driver,
the binding and the texts from my instructions, and ran the build and
binding checks. I reviewed the code and ran the tests on the device.
---
Changes in v4:
- Binding: drop "without registers or interrupt line" from the
description (Jonathan). Kept Krzysztof's Reviewed-by on patch 1, as
this is the only change to the binding since his review.
- Driver: don't read the second address from firmware at all, it is
always the device address plus one (Jonathan). This also removes the
fallback for new_device and with it the question of how to detect a
missing firmware node (Jonathan, Andy).
- Driver: one lookup helper each for the scale and the integration time
instead of cm3217_find_avail() with a length parameter (Andy).
- Driver: reversed xmas tree order in cm3217_read_count() and probe()
(Andy).
- Driver: devm_i2c_new_dummy_device() call on one line (Andy).
- Driver: include mod_devicetable.h for the device ID tables.
- Commit messages: patch 2 describes the new address handling; both
patches now say what the coding assistant did, in addition to the
"Tool use" paragraph above.
- Rebase onto the current iio togreg branch.
- No functional change on the Tegra Note 7: the scale and integration
time tables in the object file are identical to v3, and the second
address is still 0x11.
- Link to v3: https://patch.msgid.link/20261113-upstream-iio-cm3217-v3-0-c4d5b2485dff@tsv-stahl-riesa.de
Changes in v3:
- Binding: list both I2C addresses in reg, one description each
(Conor).
- Driver: take the second address from the second reg entry, with 0x11
as fallback when there is no firmware node (new_device). Svyatoslav,
I kept your Tested-by as a device tree with only the first address
still works through the fallback, but a quick retest would be
welcome.
- Driver: write the resolution constant as 0.01875 lux times 800 ms in
nanolux * ms, so that NANO is no longer part of it (Andy).
- Driver: split the scale into its integer and nano parts with a named
helper macro (Andy).
- Driver: static_assert() on one line, the conversion margin added to
conv_ms with a comment, FIELD_MODIFY() for IT_T, and the error return
in probe inside scoped_guard() (Andy).
- No functional change: the scale and integration time tables in the
object file are identical to v2. struct cm3217_data was checked with
pahole: one 2-byte hole on 32-bit ARM, and no field order makes it
smaller as ktime_t needs 8-byte alignment, so the order stays (Andy).
- Tested again on the device, see above.
- Link to v2: https://patch.msgid.link/20261106-upstream-iio-cm3217-v2-0-d97adde9171c@tsv-stahl-riesa.de
Changes in v2:
- Binding: describe the two I2C addresses under reg, including what each
one is used for (Jonathan).
- Driver: replace the scale list that was recomputed on every change of
the integration time by a const table built at compile time;
read_avail() returns the row for the current integration time, so no
table is rewritten while a caller may hold a pointer to it (Jonathan,
Sashiko).
- Driver: write the resolution constant with NANO and explain its unit
(Jonathan).
- Driver: add cm3217_power_down() for the shutdown write used in three
places (Jonathan), and a helper for the table lookups in write_raw()
so the loop counter can live in the for statement (Jonathan).
- Driver: take the conversion time from the integration time table and
drop the separate table in milliseconds.
- Pick up Svyatoslav's Tested-by on patch 2 (given on v1).
- Commit messages: name the downstream trees, note that the 4 T field
value differs from the vendor header, drop "driver" from the binding
text.
- Rebase onto the current iio togreg branch (based on v7.4-rc1).
- No functional change intended. The 32 scale values match the v1
computation. Run again on the Tegra Note 7 (P1640) as a module: each
integration time lists the same scales as with v1, all 32 settings are
accepted and read back, invalid values are rejected, and the sensor is
shut down when the device is removed.
- Link to v1: https://patch.msgid.link/20261101-upstream-iio-cm3217-v1-0-5e1724cae7b7@tsv-stahl-riesa.de
---
Patrick Fischer (2):
dt-bindings: iio: light: Add Capella CM3217
iio: light: Add Capella CM3217 ambient light sensor driver
.../bindings/iio/light/capella,cm3217.yaml | 49 +++
drivers/iio/light/Kconfig | 10 +
drivers/iio/light/Makefile | 1 +
drivers/iio/light/cm3217.c | 420 +++++++++++++++++++++
4 files changed, 480 insertions(+)
---
base-commit: 92c9d64c0cf749c8369403a32630562f0e855a6c
change-id: 20261101-upstream-iio-cm3217-e3f14c4760e5
Best regards,
--
Patrick Fischer <admin@tsv-stahl-riesa.de>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217
2026-11-27 23:38 [PATCH v4 0/2] iio: light: Add Capella CM3217 ambient light sensor Patrick Fischer via B4 Relay
@ 2026-11-27 23:38 ` Patrick Fischer via B4 Relay
2026-11-28 0:31 ` Jonathan Cameron
2026-11-27 23:38 ` [PATCH v4 2/2] iio: light: Add Capella CM3217 ambient light sensor driver Patrick Fischer via B4 Relay
1 sibling, 1 reply; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-27 23:38 UTC (permalink / raw)
To: Jonathan Cameron, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kevin Tsai, Svyatoslav Ryhel, linux-iio,
devicetree, linux-tegra, linux-kernel, Krzysztof Kozlowski,
Patrick Fischer
From: Patrick Fischer <admin@tsv-stahl-riesa.de>
The Capella CM3217 is an ambient light sensor with an I2C interface but
without registers and without an interrupt line. It always answers on
the two fixed addresses 0x10 and 0x11, which are both listed in reg.
The sensor is used in the NVIDIA Tegra Note 7 and the NVIDIA Shield
Tablet.
Tool use: Claude Code wrote this binding.
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
.../bindings/iio/light/capella,cm3217.yaml | 49 ++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml b/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml
new file mode 100644
index 000000000000..a03cdf2bd375
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/capella,cm3217.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Capella CM3217 ambient light sensor
+
+maintainers:
+ - Patrick Fischer <admin@tsv-stahl-riesa.de>
+
+description:
+ The CM3217 is an I2C ambient light sensor.
+
+properties:
+ compatible:
+ const: capella,cm3217
+
+ reg:
+ items:
+ - description:
+ Command byte 1 (shutdown and IT_T) on write, high byte of the light
+ count on read. Fixed at 0x10.
+ - description:
+ Command byte 2 (FD_IT) on write, low byte of the light count on
+ read. Fixed at 0x11.
+
+ vdd-supply: true
+
+required:
+ - compatible
+ - reg
+ - vdd-supply
+
+additionalProperties: false
+
+examples:
+ - |
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ light-sensor@10 {
+ compatible = "capella,cm3217";
+ reg = <0x10>, <0x11>;
+ vdd-supply = <&vdd_2v85_sen>;
+ };
+ };
+...
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4 2/2] iio: light: Add Capella CM3217 ambient light sensor driver
2026-11-27 23:38 [PATCH v4 0/2] iio: light: Add Capella CM3217 ambient light sensor Patrick Fischer via B4 Relay
2026-11-27 23:38 ` [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217 Patrick Fischer via B4 Relay
@ 2026-11-27 23:38 ` Patrick Fischer via B4 Relay
1 sibling, 0 replies; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-27 23:38 UTC (permalink / raw)
To: Jonathan Cameron, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kevin Tsai, Svyatoslav Ryhel, linux-iio,
devicetree, linux-tegra, linux-kernel, Patrick Fischer
From: Patrick Fischer <admin@tsv-stahl-riesa.de>
The CM3217 has no registers and no ID. Writing a byte to address 0x10
or 0x11 sets command byte 1 or 2, reading a byte from 0x10 or 0x11
returns the high or low byte of the 16-bit light count. The second
address always follows the first one. The binding lists both, but the
driver derives the second one from the first instead of reading it from
firmware, which also covers new_device.
Command byte 1 holds the shutdown bit and the IT_T field (1/2 T, 1 T,
2 T or 4 T), command byte 2 the FD_IT field (800 ms down to 66 ms). The
count scales linearly with both. The driver exposes FD_IT as the
integration time and IT_T through the scale, so the scale list offers
four ranges for each integration time.
The datasheet is not public. The bit fields, the reserved bits that are
written as 1 and the startup setting of 2 T and 100 ms come from NVIDIA's
downstream drivers (drivers/staging/iio/light/cm3217.c in the 3.4 kernel
rel-tegranote-r8 and the 3.10 kernel
android-tegra-flounder-3.10-nougat-mr1.1). One value differs: the vendor
header defines 4 T as 4 << 2, which is outside the two-bit field. On a
Tegra Note 7 field value 3 gave four times the 1 T count, so the driver
uses that. For the resolution, Capella gives a range of 0.01875 to
119156 lux. The driver takes 0.01875 lux per count at 4 T and 800 ms.
This matches both the upper end (65535 counts at 1/2 T and 66 ms) and
the 0.3 lux per count that the NVIDIA 3.4 driver uses at 2 T and 100 ms.
The sensor has no data-ready flag, and a new setting only takes effect
when the conversion in progress ends. A conversion takes IT_T times
FD_IT, up to 3.2 s. Leaving shutdown starts a new one at once, so the
driver passes through shutdown on every change and waits one conversion
period plus a margin before it reads the count. The timing was measured
on a Tegra Note 7.
Tool use: Claude Code wrote this driver from the downstream drivers and
from my measurements on the device.
Tested-by: Svyatoslav Ryhel <clamor95@gmail.com> # Tegratab P1988
Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
Svyatoslav's Tested-by was given on v1. Since then there was no
functional change apart from how the driver gets the second address
(v1: fixed 0x11, v4: device address plus one, which gives the same
0x11).
---
drivers/iio/light/Kconfig | 10 ++
drivers/iio/light/Makefile | 1 +
drivers/iio/light/cm3217.c | 420 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 431 insertions(+)
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 0b266084e837..c097c3232af4 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -196,6 +196,16 @@ config BH1780
To compile this driver as a module, choose M here: the module will
be called bh1780.
+config CM3217
+ tristate "Capella CM3217 ambient light sensor"
+ depends on I2C
+ help
+ Say Y here to build support for the Capella Microsystems CM3217
+ ambient light sensor.
+
+ To compile this driver as a module, choose M here: the module will
+ be called cm3217.
+
config CM32181
depends on I2C
tristate "CM32181 driver"
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 9fc046e636a2..5daff8ef0daa 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_AS7343) += as7343.o
obj-$(CONFIG_BH1745) += bh1745.o
obj-$(CONFIG_BH1750) += bh1750.o
obj-$(CONFIG_BH1780) += bh1780.o
+obj-$(CONFIG_CM3217) += cm3217.o
obj-$(CONFIG_CM32181) += cm32181.o
obj-$(CONFIG_CM3232) += cm3232.o
obj-$(CONFIG_CM3323) += cm3323.o
diff --git a/drivers/iio/light/cm3217.c b/drivers/iio/light/cm3217.c
new file mode 100644
index 000000000000..5edd4d21d08c
--- /dev/null
+++ b/drivers/iio/light/cm3217.c
@@ -0,0 +1,420 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Capella CM3217 ambient light sensor driver
+ *
+ * The CM3217 has no registers. It answers on two fixed I2C addresses:
+ * writing a byte to 0x10 or 0x11 sets command byte 1 or 2, reading a byte
+ * from 0x10 or 0x11 returns the MSB or LSB of the 16-bit light count.
+ *
+ * Copyright (C) 2026 Patrick Fischer <admin@tsv-stahl-riesa.de>
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/build_bug.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/ktime.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/regulator/consumer.h>
+#include <linux/time64.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+#include <linux/iio/iio.h>
+
+#define CM3217_CMD1_RSVD BIT(5) /* reserved, set to 1 */
+#define CM3217_CMD1_IT_T GENMASK(3, 2) /* 1/2 T, 1 T, 2 T, 4 T */
+#define CM3217_CMD1_WDM BIT(1) /* set to 1 like the vendor driver */
+#define CM3217_CMD1_SD BIT(0) /* shutdown when set */
+
+#define CM3217_CMD2_FD_IT GENMASK(7, 5)
+
+#define CM3217_IT_T_2T 2
+#define CM3217_FD_IT_100MS 5
+
+/*
+ * The finest resolution is 0.01875 lux per count, at 4 T and 800 ms. The
+ * resolution is inversely proportional to IT_T and FD_IT, so keep it as
+ * nanolux per count times milliseconds.
+ */
+#define CM3217_NLUX_MS (18750000ULL * 800) /* 0.01875 lux * 800 ms */
+
+#define CM3217_MARGIN_MS 10
+
+/* FD_IT settings in seconds, indexed by the field value */
+static const int cm3217_int_time_avail[][2] = {
+ { 0, 800000 }, { 0, 400000 }, { 0, 266000 }, { 0, 200000 },
+ { 0, 130000 }, { 0, 100000 }, { 0, 80000 }, { 0, 66000 },
+};
+
+/* Split nanolux into the integer and nano parts of IIO_VAL_INT_PLUS_NANO */
+#define CM3217_NLUX_TO_VAL(nlux) { (nlux) / NANO, (nlux) % NANO }
+
+/* Scale in lux per count at FD_IT = ms milliseconds and IT_T = 1/2 T << it_t */
+#define CM3217_SCALE(ms, it_t) \
+ CM3217_NLUX_TO_VAL((CM3217_NLUX_MS << (3 - (it_t))) / (ms))
+
+#define CM3217_SCALES(ms) { \
+ CM3217_SCALE(ms, 0), CM3217_SCALE(ms, 1), \
+ CM3217_SCALE(ms, 2), CM3217_SCALE(ms, 3), \
+}
+
+/* Available scales, indexed by FD_IT and then by IT_T */
+static const int cm3217_scale_avail[][4][2] = {
+ CM3217_SCALES(800), CM3217_SCALES(400),
+ CM3217_SCALES(266), CM3217_SCALES(200),
+ CM3217_SCALES(130), CM3217_SCALES(100),
+ CM3217_SCALES(80), CM3217_SCALES(66),
+};
+
+static_assert(ARRAY_SIZE(cm3217_scale_avail) == ARRAY_SIZE(cm3217_int_time_avail));
+
+struct cm3217_data {
+ struct i2c_client *client;
+ struct i2c_client *client_lsb;
+ /* protects the command bytes and ready */
+ struct mutex lock;
+ u8 cmd1;
+ u8 cmd2;
+ ktime_t ready;
+};
+
+static unsigned int cm3217_conv_ms(struct cm3217_data *data)
+ __must_hold(&data->lock)
+{
+ unsigned int fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
+ unsigned int it_t = FIELD_GET(CM3217_CMD1_IT_T, data->cmd1);
+
+ return ((cm3217_int_time_avail[fd_it][1] / USEC_PER_MSEC) << it_t) / 2;
+}
+
+static int cm3217_power_down(struct cm3217_data *data, u8 cmd1)
+{
+ return i2c_smbus_write_byte(data->client, cmd1 | CM3217_CMD1_SD);
+}
+
+/*
+ * A new setting only takes effect when the conversion in progress ends,
+ * which can take up to 3.2 s. Leaving shutdown starts a new conversion
+ * right away, so pass through shutdown on every change.
+ */
+static int cm3217_write_cmd(struct cm3217_data *data, u8 cmd1, u8 cmd2)
+ __must_hold(&data->lock)
+{
+ unsigned int conv_ms;
+ int ret;
+
+ ret = cm3217_power_down(data, cmd1);
+ if (ret)
+ return ret;
+
+ ret = i2c_smbus_write_byte(data->client_lsb, cmd2);
+ if (ret)
+ return ret;
+
+ ret = i2c_smbus_write_byte(data->client, cmd1);
+ if (ret)
+ return ret;
+
+ data->cmd1 = cmd1;
+ data->cmd2 = cmd2;
+
+ /*
+ * The first count is ready after one conversion. The conversion time
+ * is only the nominal one, so add 10 % and a fixed margin.
+ */
+ conv_ms = cm3217_conv_ms(data);
+ conv_ms += conv_ms / 10 + CM3217_MARGIN_MS;
+ data->ready = ktime_add_ms(ktime_get(), conv_ms);
+
+ return 0;
+}
+
+static int cm3217_read_count(struct cm3217_data *data, int *val)
+ __must_hold(&data->lock)
+{
+ int lsb, msb;
+ s64 wait_ms;
+
+ wait_ms = ktime_ms_delta(data->ready, ktime_get());
+ if (wait_ms > 0)
+ msleep(wait_ms);
+
+ lsb = i2c_smbus_read_byte(data->client_lsb);
+ if (lsb < 0)
+ return lsb;
+
+ msb = i2c_smbus_read_byte(data->client);
+ if (msb < 0)
+ return msb;
+
+ *val = msb << 8 | lsb;
+
+ return 0;
+}
+
+static const struct iio_chan_spec cm3217_channels[] = {
+ {
+ .type = IIO_LIGHT,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
+ .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME),
+ },
+};
+
+static int cm3217_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct cm3217_data *data = iio_priv(indio_dev);
+ unsigned int fd_it, it_t;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
+ it_t = FIELD_GET(CM3217_CMD1_IT_T, data->cmd1);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ ret = cm3217_read_count(data, val);
+ if (ret)
+ return ret;
+ return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SCALE:
+ *val = cm3217_scale_avail[fd_it][it_t][0];
+ *val2 = cm3217_scale_avail[fd_it][it_t][1];
+ return IIO_VAL_INT_PLUS_NANO;
+ case IIO_CHAN_INFO_INT_TIME:
+ *val = cm3217_int_time_avail[fd_it][0];
+ *val2 = cm3217_int_time_avail[fd_it][1];
+ return IIO_VAL_INT_PLUS_MICRO;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int cm3217_read_avail(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ const int **vals, int *type, int *length,
+ long mask)
+{
+ struct cm3217_data *data = iio_priv(indio_dev);
+ unsigned int fd_it;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ scoped_guard(mutex, &data->lock)
+ fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
+ *vals = (const int *)cm3217_scale_avail[fd_it];
+ *length = 2 * ARRAY_SIZE(cm3217_scale_avail[0]);
+ *type = IIO_VAL_INT_PLUS_NANO;
+ return IIO_AVAIL_LIST;
+ case IIO_CHAN_INFO_INT_TIME:
+ *vals = (const int *)cm3217_int_time_avail;
+ *length = 2 * ARRAY_SIZE(cm3217_int_time_avail);
+ *type = IIO_VAL_INT_PLUS_MICRO;
+ return IIO_AVAIL_LIST;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int cm3217_find_scale_avail(unsigned int fd_it, int val, int val2)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(cm3217_scale_avail[0]); i++) {
+ if (cm3217_scale_avail[fd_it][i][0] == val &&
+ cm3217_scale_avail[fd_it][i][1] == val2)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int cm3217_find_int_time_avail(int val, int val2)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(cm3217_int_time_avail); i++) {
+ if (cm3217_int_time_avail[i][0] == val &&
+ cm3217_int_time_avail[i][1] == val2)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int cm3217_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct cm3217_data *data = iio_priv(indio_dev);
+ unsigned int fd_it;
+ u8 cmd1, cmd2;
+ int i;
+
+ guard(mutex)(&data->lock);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ fd_it = FIELD_GET(CM3217_CMD2_FD_IT, data->cmd2);
+ i = cm3217_find_scale_avail(fd_it, val, val2);
+ if (i < 0)
+ return i;
+
+ cmd1 = data->cmd1;
+ FIELD_MODIFY(CM3217_CMD1_IT_T, &cmd1, i);
+
+ return cm3217_write_cmd(data, cmd1, data->cmd2);
+ case IIO_CHAN_INFO_INT_TIME:
+ i = cm3217_find_int_time_avail(val, val2);
+ if (i < 0)
+ return i;
+
+ cmd2 = FIELD_PREP(CM3217_CMD2_FD_IT, i);
+
+ return cm3217_write_cmd(data, data->cmd1, cmd2);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int cm3217_write_raw_get_fmt(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ return IIO_VAL_INT_PLUS_NANO;
+ case IIO_CHAN_INFO_INT_TIME:
+ return IIO_VAL_INT_PLUS_MICRO;
+ default:
+ return -EINVAL;
+ }
+}
+
+static const struct iio_info cm3217_info = {
+ .read_raw = cm3217_read_raw,
+ .read_avail = cm3217_read_avail,
+ .write_raw = cm3217_write_raw,
+ .write_raw_get_fmt = cm3217_write_raw_get_fmt,
+};
+
+static void cm3217_power_off(void *_data)
+{
+ struct cm3217_data *data = _data;
+
+ cm3217_power_down(data, data->cmd1);
+}
+
+static int cm3217_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ struct iio_dev *indio_dev;
+ struct cm3217_data *data;
+ u8 cmd1, cmd2;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ data = iio_priv(indio_dev);
+ data->client = client;
+ i2c_set_clientdata(client, indio_dev);
+
+ 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 vdd supply\n");
+
+ /*
+ * The binding lists both addresses, but they are fixed and the second
+ * one always follows the first, so there is no need to look it up.
+ */
+ data->client_lsb = devm_i2c_new_dummy_device(dev, client->adapter, client->addr + 1);
+ if (IS_ERR(data->client_lsb))
+ return dev_err_probe(dev, PTR_ERR(data->client_lsb),
+ "failed to register second address\n");
+
+ /* Start with the setting of the vendor driver: 2 T, 100 ms */
+ cmd1 = CM3217_CMD1_RSVD | CM3217_CMD1_WDM |
+ FIELD_PREP(CM3217_CMD1_IT_T, CM3217_IT_T_2T);
+ cmd2 = FIELD_PREP(CM3217_CMD2_FD_IT, CM3217_FD_IT_100MS);
+
+ scoped_guard(mutex, &data->lock) {
+ ret = cm3217_write_cmd(data, cmd1, cmd2);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to power on\n");
+ }
+
+ ret = devm_add_action_or_reset(dev, cm3217_power_off, data);
+ if (ret)
+ return ret;
+
+ indio_dev->name = "cm3217";
+ indio_dev->info = &cm3217_info;
+ indio_dev->channels = cm3217_channels;
+ indio_dev->num_channels = ARRAY_SIZE(cm3217_channels);
+ indio_dev->modes = INDIO_DIRECT_MODE;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static int cm3217_suspend(struct device *dev)
+{
+ struct cm3217_data *data = iio_priv(dev_get_drvdata(dev));
+
+ guard(mutex)(&data->lock);
+
+ return cm3217_power_down(data, data->cmd1);
+}
+
+static int cm3217_resume(struct device *dev)
+{
+ struct cm3217_data *data = iio_priv(dev_get_drvdata(dev));
+
+ guard(mutex)(&data->lock);
+
+ return cm3217_write_cmd(data, data->cmd1, data->cmd2);
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(cm3217_pm_ops, cm3217_suspend, cm3217_resume);
+
+static const struct i2c_device_id cm3217_id[] = {
+ { .name = "cm3217" },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, cm3217_id);
+
+static const struct of_device_id cm3217_of_match[] = {
+ { .compatible = "capella,cm3217" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, cm3217_of_match);
+
+static struct i2c_driver cm3217_driver = {
+ .driver = {
+ .name = "cm3217",
+ .of_match_table = cm3217_of_match,
+ .pm = pm_sleep_ptr(&cm3217_pm_ops),
+ },
+ .probe = cm3217_probe,
+ .id_table = cm3217_id,
+};
+module_i2c_driver(cm3217_driver);
+
+MODULE_AUTHOR("Patrick Fischer <admin@tsv-stahl-riesa.de>");
+MODULE_DESCRIPTION("Capella CM3217 ambient light sensor driver");
+MODULE_LICENSE("GPL");
--
2.56.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217
2026-11-27 23:38 ` [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217 Patrick Fischer via B4 Relay
@ 2026-11-28 0:31 ` Jonathan Cameron
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2026-11-28 0:31 UTC (permalink / raw)
To: Patrick Fischer via B4 Relay
Cc: admin, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
David Lechner, Nuno Sá,
Andy Shevchenko, Kevin Tsai, Svyatoslav Ryhel, linux-iio,
devicetree, linux-tegra, linux-kernel, Krzysztof Kozlowski
On Sat, 28 Nov 2026 00:38:56 +0100
Patrick Fischer via B4 Relay <devnull+admin.tsv-stahl-riesa.de@kernel.org> wrote:
> From: Patrick Fischer <admin@tsv-stahl-riesa.de>
>
> The Capella CM3217 is an ambient light sensor with an I2C interface but
> without registers and without an interrupt line. It always answers on
> the two fixed addresses 0x10 and 0x11, which are both listed in reg.
>
> The sensor is used in the NVIDIA Tegra Note 7 and the NVIDIA Shield
> Tablet.
>
> Tool use: Claude Code wrote this binding.
Comments on that belong below the ---
If nothing else comes up, I'll drop it whilst applying.
>
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
> Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
> ---
> .../bindings/iio/light/capella,cm3217.yaml | 49 ++++++++++++++++++++++
> 1 file changed, 49 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml b/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml
> new file mode 100644
> index 000000000000..a03cdf2bd375
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/light/capella,cm3217.yaml
> @@ -0,0 +1,49 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/light/capella,cm3217.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Capella CM3217 ambient light sensor
> +
> +maintainers:
> + - Patrick Fischer <admin@tsv-stahl-riesa.de>
> +
> +description:
> + The CM3217 is an I2C ambient light sensor.
> +
> +properties:
> + compatible:
> + const: capella,cm3217
> +
> + reg:
> + items:
> + - description:
> + Command byte 1 (shutdown and IT_T) on write, high byte of the light
> + count on read. Fixed at 0x10.
> + - description:
> + Command byte 2 (FD_IT) on write, low byte of the light count on
> + read. Fixed at 0x11.
> +
> + vdd-supply: true
> +
> +required:
> + - compatible
> + - reg
> + - vdd-supply
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + i2c {
> + #address-cells = <1>;
> + #size-cells = <0>;
> +
> + light-sensor@10 {
> + compatible = "capella,cm3217";
> + reg = <0x10>, <0x11>;
> + vdd-supply = <&vdd_2v85_sen>;
> + };
> + };
> +...
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-28 0:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 23:38 [PATCH v4 0/2] iio: light: Add Capella CM3217 ambient light sensor Patrick Fischer via B4 Relay
2026-11-27 23:38 ` [PATCH v4 1/2] dt-bindings: iio: light: Add Capella CM3217 Patrick Fischer via B4 Relay
2026-11-28 0:31 ` Jonathan Cameron
2026-11-27 23:38 ` [PATCH v4 2/2] iio: light: Add Capella CM3217 ambient light sensor driver Patrick Fischer via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®