From nobody Mon Jun 8 09:48:03 2026 Received: from outbound11.mail.transip.nl (outbound11.mail.transip.nl [136.144.136.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0AF3326D4A; Thu, 4 Jun 2026 10:06:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=136.144.136.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567594; cv=none; b=UpEqapaPhftTcN4eCF/TEt6NulMRCCgL+CKFO4q0904vXHBTGv4kKTgmml1OW0oVoLbIOotO0G6wWAS8xSBKR8VkHPP6Z7tVbGXMQgL42dXPu2Bhje2QwimYE4jNOUsC4WuXwigEB08hLl4leqgMNCpd1Hy0iYvF6jwm6my3stQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567594; c=relaxed/simple; bh=Y4B6fqi/48NK9GYV1BBgrVjgx7MvJuxjKSvFfsI0q1E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e7XQHNcL8gqg8x0YEV+j1zwHVC2e2a4+/QvxszOXYDv7sNCRRp1uaDjyp88tx+If6GQ1TXd/KdM7HhKblKb3C1aRkuWpLKywQMX9XDautb3dHL2cOaPyIcJ/gR4P3wtvkMvWm8SlOelsk7g9hDwlytzp0BFQHIZSICgqe/3w43Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org; spf=pass smtp.mailfrom=herrie.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b=gyzlTErd; arc=none smtp.client-ip=136.144.136.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=herrie.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b="gyzlTErd" Received: from submission11.mail.transip.nl (unknown [10.103.8.162]) by outbound11.mail.transip.nl (Postfix) with ESMTP id 4gWKwC43fszkQNlV; Thu, 4 Jun 2026 12:06:19 +0200 (CEST) Received: from herrie-desktop.. (180-93-184-31.ftth.glasoperator.nl [31.184.93.180]) by submission11.mail.transip.nl (Postfix) with ESMTPA id 4gWKwB5zMyz3R3nyx; Thu, 4 Jun 2026 12:06:18 +0200 (CEST) From: Herman van Hazendonk To: jic23@kernel.org Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, tomasborquez13@gmail.com, masneyb@onstation.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Herman van Hazendonk Subject: [PATCH v3 1/3] iio: light: isl29018: fix overflow and precision in isl29018_read_lux() Date: Thu, 4 Jun 2026 12:06:15 +0200 Message-ID: <20260604100617.3486541-2-github.com@herrie.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260604100617.3486541-1-github.com@herrie.org> References: <20260604100617.3486541-1-github.com@herrie.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: ClueGetter at submission11.mail.transip.nl DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=transip-a; d=herrie.org; t=1780567579; h=from:subject:to:cc: references:in-reply-to:date:mime-version:content-type; bh=Zco13UcFpBMtSlK+E0umut/oSGpyst/ctTl6uNj/hGw=; b=gyzlTErd4Ct0K37EjRfkq+cFC02kM+DikE/+z9ED8rLQFaWzWwqvfuO3cPReVnWtzk+SDt BUZdreNcDLqBjSDynS/WLVwgCkFYSxCkuHlTAipD00FPtJ+By+m09hCvTGsnEkxNBtpvag KbBRkDuBRta8txuWiKZo3Vy4XwE7noQj84T/OAAu25m3i9KMbS8GiTRD9txLRWtea5GQAw vnARtjXjnoafISGxmp2HAka6JBXCvoJRIzC4l87rl1mnLH7Wy2MtLwKUXLlsTjfMnNL2MI DYK/2ptWkGwzQIzI/JOh5G3slJpda8wpr6jJwu5Dr16sU0m1YgtpO2oOTojRIg== X-Report-Abuse-To: abuse@transip.nl The intermediate calculations in isl29018_read_lux() use 32-bit arithmetic throughout, which overflows in two distinct ways: 1. lux_data * chip->scale.uscale =E2=80=94 at 16-bit integration time and t= he 1000 fc range, scale.uscale is 976562. A full-scale 16-bit reading (65535) gives 65535 * 976562 =E2=89=88 64 billion, far beyond UINT_MAX. The value wraps before the /1000000 division can save it, producing a wildly wrong data_x_range. 2. data_x_range * chip->calibscale =E2=80=94 even after a correct data_x_ra= nge, multiplying by a calibscale of a few hundred (reasonable for a deeply tinted cover glass) pushes the product past INT_MAX, causing *lux to wrap negative. Additionally, dividing lux_data * scale.uscale by 1000000 before applying calibscale discards the fractional-lux remainder. For low ranges where scale.scale is zero, any reading below 1000000/scale.uscale counts truncates to a data_x_range of zero, so the calibscale multiplication cannot rescue it. This creates a dead-band at low light levels that is especially visible when a large cover-glass compensation gain is in use. Fix the overflows by widening the intermediate variables to u64 and using div_u64() for the divisions (plain 64-bit division emits __aeabi_uldivmod on ARM32, which is not available in kernel builds). Preserve the uscale remainder across the first division so that the calibscale multiplication captures the sub-lux contribution. Clamp the final result to INT_MAX before storing it in the signed int *lux out parameter. Signed-off-by: Herman van Hazendonk --- drivers/iio/light/isl29018.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iio/light/isl29018.c b/drivers/iio/light/isl29018.c index b6ab726d1dae..f3312ad670d9 100644 --- a/drivers/iio/light/isl29018.c +++ b/drivers/iio/light/isl29018.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -193,17 +194,22 @@ static int isl29018_read_sensor_input(struct isl29018= _chip *chip, int mode) static int isl29018_read_lux(struct isl29018_chip *chip, int *lux) { int lux_data; - unsigned int data_x_range; + u32 uscale_rem; + u64 uscale_term, data_x_range, result; =20 lux_data =3D isl29018_read_sensor_input(chip, ISL29018_CMD1_OPMODE_ALS_ONCE); if (lux_data < 0) return lux_data; =20 - data_x_range =3D lux_data * chip->scale.scale + - lux_data * chip->scale.uscale / 1000000; - *lux =3D data_x_range * chip->calibscale + - data_x_range * chip->ucalibscale / 1000000; + /* Retain the uscale remainder so calibscale captures sub-lux precision. = */ + uscale_term =3D (u64)lux_data * chip->scale.uscale; + data_x_range =3D (u64)lux_data * chip->scale.scale + + div_u64_rem(uscale_term, 1000000, &uscale_rem); + result =3D data_x_range * chip->calibscale + + div_u64((u64)uscale_rem * chip->calibscale, 1000000) + + div_u64(data_x_range * chip->ucalibscale, 1000000); + *lux =3D (int)min_t(u64, result, INT_MAX); =20 return 0; } --=20 2.43.0 From nobody Mon Jun 8 09:48:03 2026 Received: from outbound8.mail.transip.nl (outbound8.mail.transip.nl [136.144.136.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3E922F069D; Thu, 4 Jun 2026 10:06:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=136.144.136.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567593; cv=none; b=VxKhkBOzzkAveQAZqkUPvHCWhAQNAdsj4N+1KMcEA9D1tVxfASZnUYhyVgnca6AIl+5xpzC9U0m9a6AUcQmNVTjJKFsFod+qdq6YEDNC3JF3MUUUJQOWYqdi44jSLMTJKHJfcCUcFcoduKqPQgH+6OX518hiF0Wdt5EwwRRcqd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567593; c=relaxed/simple; bh=8/R1xH/1u5C7+If5wBxKvUwojFV7On6+CJuVfgKhiPY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F1IYQG+PgVqIGDcMvGvFMbGauNVibAgUexLvmm1HCO8IBzNkkL1A5WDnhC/e9HlrrNdSTqFY1Dd1A5L3r1cqgZ/4Rlws2nzoyNlYAOobGrvyrHjQa3VYfwuOsJZzoH1XcNpsr18+KHL8GopeoHsS3Ua5lJnjZoQiqc/1LD9sF4Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org; spf=pass smtp.mailfrom=herrie.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b=fSFyhpA+; arc=none smtp.client-ip=136.144.136.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=herrie.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b="fSFyhpA+" Received: from submission11.mail.transip.nl (unknown [10.103.8.162]) by outbound8.mail.transip.nl (Postfix) with ESMTP id 4gWKwD1cNRzY76hL; Thu, 4 Jun 2026 12:06:20 +0200 (CEST) Received: from herrie-desktop.. (180-93-184-31.ftth.glasoperator.nl [31.184.93.180]) by submission11.mail.transip.nl (Postfix) with ESMTPA id 4gWKwC4FQNz3R3nyt; Thu, 4 Jun 2026 12:06:19 +0200 (CEST) From: Herman van Hazendonk To: jic23@kernel.org Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, tomasborquez13@gmail.com, masneyb@onstation.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Herman van Hazendonk Subject: [PATCH v3 2/3] dt-bindings: iio: light: isl29018: add isil,cover-comp-gain Date: Thu, 4 Jun 2026 12:06:16 +0200 Message-ID: <20260604100617.3486541-3-github.com@herrie.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260604100617.3486541-1-github.com@herrie.org> References: <20260604100617.3486541-1-github.com@herrie.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: ClueGetter at submission11.mail.transip.nl DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=transip-a; d=herrie.org; t=1780567579; h=from:subject:to:cc: references:in-reply-to:date:mime-version; bh=RCX5QQtXEBvtBWLLFqzdraDn9aAETLjRcUQQZV1kx+A=; b=fSFyhpA+wsc90ssyKSTAx1O8nlX3ddHD4AArSzsVIsglw72x2FrqtvIjIjXtevDa/GqyU/ Xvy11LJD7JotCyWsZ/l+NYSbnqs1hxlhnkcDPLWDHBd0dSZJHbEuh4CFJ7pAGItUom2MJg Xtr54/V3aDjW4pGR2EP0dfHhv9u9kph4Y4utDut57mqnXqmWG0pinLcEC4Op/CQQ9OSVcu Db7mabGK4YNDo8u1+3bx5XlQlvk1y64FM/CBc6xcyR5eSPnh+fx9DpQp+4GSdTjkQtiU/H xuaK9A23pP8lG2P90RSBxq0xRQJeyTO/hGnyWFz18FUWi+fK3KG8Jw023EBpNg== X-Report-Abuse-To: abuse@transip.nl Content-Type: text/plain; charset="utf-8" Document the new optional property that seeds the ISL29018 calibration scale factor at boot from firmware, allowing boards with tinted cover glass to ship with correct luminance readings without a userspace helper. The value is a positive integer (minimum 1, maximum 65535) that is multiplied with the raw lux reading. Userspace can still override it at runtime through in_illuminance0_calibscale. Signed-off-by: Herman van Hazendonk Acked-by: Conor Dooley --- .../devicetree/bindings/iio/light/isl29018.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/light/isl29018.yaml b/Do= cumentation/devicetree/bindings/iio/light/isl29018.yaml index 0ea278b07d1c..92ea2742bbd3 100644 --- a/Documentation/devicetree/bindings/iio/light/isl29018.yaml +++ b/Documentation/devicetree/bindings/iio/light/isl29018.yaml @@ -34,6 +34,19 @@ properties: vcc-supply: description: Regulator that provides power to the sensor =20 + isil,cover-comp-gain: + description: | + Multiplier applied to the ambient-light reading at startup to + compensate for optical loss in the board's cover glass. Boards + that mount the sensor under a tinted or coated window typically + need a value between a few and a few hundred. The value seeds + in_illuminance0_calibscale, so it can still be retuned at + runtime through sysfs. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 65535 + default: 1 + required: - compatible - reg --=20 2.43.0 From nobody Mon Jun 8 09:48:03 2026 Received: from outbound11.mail.transip.nl (outbound11.mail.transip.nl [136.144.136.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14973352F87; Thu, 4 Jun 2026 10:06:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=136.144.136.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567592; cv=none; b=VSDJToINViyUg84B2ixkDhRcWq/lNHw2lF4vwhjrcB6k8uSG9p5TBK/WhF4LdnIYYQgkjk4qA6ZeXgKlGqMjGQpc2fQgF2bLTc/6JwNRP2yGUuVvPZAlHBvvRL7Y2quGEDwZlLwXbk+upXtFHdIxhRqr+XUApY+7qAMg5LdnILA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567592; c=relaxed/simple; bh=hLMSJkLaFcgr9nifWBGyhT3a2LjIiAbZE2LYvUbhrcU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TyOczASNse1VZPZjXfomzttYneZ80WcJcq9S+zKWLezWsqGNDmTUHTvXEBmQYYIxrMo/2z9P2MLvk+ZI9Ai5p+02J7SUZ/C92G/I+cUBotxr+E/PrzoKVUXHTDTUkLNZnux8VfTy3HmLPvuw9wh1Da8sb9uby5u0JOqRCfg+gaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org; spf=pass smtp.mailfrom=herrie.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b=vIenSS77; arc=none smtp.client-ip=136.144.136.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=herrie.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=herrie.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=herrie.org header.i=@herrie.org header.b="vIenSS77" Received: from submission11.mail.transip.nl (unknown [10.103.8.162]) by outbound11.mail.transip.nl (Postfix) with ESMTP id 4gWKwD6gK2zkQP0C; Thu, 4 Jun 2026 12:06:20 +0200 (CEST) Received: from herrie-desktop.. (180-93-184-31.ftth.glasoperator.nl [31.184.93.180]) by submission11.mail.transip.nl (Postfix) with ESMTPA id 4gWKwD1p0Yz3R3nyx; Thu, 4 Jun 2026 12:06:20 +0200 (CEST) From: Herman van Hazendonk To: jic23@kernel.org Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, tomasborquez13@gmail.com, masneyb@onstation.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Herman van Hazendonk Subject: [PATCH v3 3/3] iio: light: isl29018: support cover-glass gain compensation via DT Date: Thu, 4 Jun 2026 12:06:17 +0200 Message-ID: <20260604100617.3486541-4-github.com@herrie.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260604100617.3486541-1-github.com@herrie.org> References: <20260604100617.3486541-1-github.com@herrie.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: ClueGetter at submission11.mail.transip.nl DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=transip-a; d=herrie.org; t=1780567580; h=from:subject:to:cc: references:in-reply-to:date:mime-version; bh=85+MdLr3OIbGUYUhr0bZoSZqPIhO6EkXOi7pDmhyjqw=; b=vIenSS77dzrgPZzgXuEI0fwnl2sYqt3fDUJ/1N7hS8mPFzknJyLE6i8RXjeUfukP9tRnos dxmwv+/En0tUehUr/VBiGbk/GakKSdHDUrykjxjvMkICX39bTH6w2CHgJMK57OXGhvbLbv KTCgFRU91vLA54KMTeWpp27uTRcw7vrP0pNsUUpGWuwPcj+zgTs7cnyRMH2Q19wLoUZ3TJ UDsKwY2VbeC1Cth2NULecd9zndu+rzlmnW05xQClXiYcmHxE0kU0uoNp7bltFmBsFAI7Au Iqvql2zwgMC8MeItyJJH815tXqJeJVmDh8ifc8e7xAPp+73C0PZUdSr3LKn/Sg== X-Report-Abuse-To: abuse@transip.nl Content-Type: text/plain; charset="utf-8" Boards that mount the ISL29018 behind tinted or coated cover glass experience optical loss that effectively reduces the sensor's apparent sensitivity. The existing in_illuminance0_calibscale sysfs attribute can correct for this at runtime, but firmware knows the loss factor at design time and there is no way to seed it without a userspace helper. Add support for an optional "isil,cover-comp-gain" device-tree property that initialises calibscale at probe time. If the property is present but cannot be read, probe returns an error via dev_err_probe() so the root cause is visible in the log. If absent, calibscale defaults to 1 (unity gain, matching the previous behaviour). Userspace can still override the value at runtime through the sysfs attribute. The approach follows the precedent set by the TSL2563 driver. Signed-off-by: Herman van Hazendonk --- drivers/iio/light/isl29018.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/isl29018.c b/drivers/iio/light/isl29018.c index f3312ad670d9..9daacb9b02bc 100644 --- a/drivers/iio/light/isl29018.c +++ b/drivers/iio/light/isl29018.c @@ -711,10 +711,13 @@ static int isl29018_probe(struct i2c_client *client) struct iio_dev *indio_dev; const void *ddata =3D NULL; const char *name; + struct device *dev; int dev_id; int err; =20 - indio_dev =3D devm_iio_device_alloc(&client->dev, sizeof(*chip)); + dev =3D &client->dev; + + indio_dev =3D devm_iio_device_alloc(dev, sizeof(*chip)); if (!indio_dev) return -ENOMEM; =20 @@ -733,8 +736,16 @@ static int isl29018_probe(struct i2c_client *client) mutex_init(&chip->lock); =20 chip->type =3D dev_id; - chip->calibscale =3D 1; chip->ucalibscale =3D 0; + if (device_property_present(dev, "isil,cover-comp-gain")) { + err =3D device_property_read_u32(dev, "isil,cover-comp-gain", + &chip->calibscale); + if (err) + return dev_err_probe(dev, err, + "invalid isil,cover-comp-gain\n"); + } else { + chip->calibscale =3D 1; + } chip->int_time =3D ISL29018_INT_TIME_16; chip->scale =3D isl29018_scales[chip->int_time][0]; chip->suspended =3D false; --=20 2.43.0