From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796386018; b=0KTicKOyH2twJzMsMnh5jzKkrRsVEpBOiTUwDnywful+xscRhOKmg3blowCmCiY3lbpf qK0vDAH2+gxAElJaFbOYyt/dpxpfb5b4exAqbmmcCG/fTrcz5BPyg6xMWXci90SQ+5yB4 /AG5q2gwJ7SPr4nr3tbB/NVNf6TRD0rCbCEwAGAf0kzpilJ9JTn66cnWSBtSsDcVn7Lmn F8UCgaS04nimCe4zwm9jMstKQOdemO2PfRGy9E4W4b589AlUj7WHFAcM4bZNo3Yw5oeTd tDrQGt/12d4tktRt/A9uXJ0TGpJ1N6Lvhwshja6Xnl150kgQj+2xCXK5DQOe/zpDe5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796386018; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=GVhAaMVkNSkvXSRKisNOkxo+blsoWMsUJAxgAoGrdiM=; b=IvoUBkseqx42TGV1roRNOq6/PPU/d5DJI5F2HRNsSoykPcK/d3Cr0svDfEq0plgeYBbF KK0q3Hh6Ljx9W8wJL1If6wELO5JDCBeZz8W/+FbSmJvnss3lsOYvF8Rm4hgVwoL1u0KFL cfNEUXJY4bAjB+KfAhUXu20iWJrzl8S6nWCpwybF+l/UFRhciYPp/UKfi5hBSZ1GG/+nX rOglOUyWl8NqeolcP3FzzxFakHviUz/lRxS6RURAxowIAFXnujXjwcgPVus9KNh6Drwgp bsUPu4SBmsx2AXLB9EjG6BpPKNwfXu+jg2Fg/auJkck2XNbMbi9qss9qtwsNIxhR+IQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=UXUwYTLn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D8F606A322; Fri, 4 Dec 2026 12:06:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=UXUwYTLn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12FAC1F000C2; Fri, 4 Dec 2026 12:06:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796386018; bh=GVhAaMVkNSkvXSRKisNOkxo+blsoWMsUJAxgAoGrdiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=UXUwYTLnJr/IKAm+4SIeO6vy6tnEG7v6B3XRhaAI68L4qbCiJWmNhTneSBXFpL1U2 yJVkypz4gnX7K7QzzxS20s16odf/qVZiLVra9xZm9+52d0dVAV6FulRiAf6YFEJpJ+ A5tiNniLXDNlvLalbq2EM+5lQENWmH6Gm7614n6U= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Arnav Kapoor , Jonathan Cameron , Sasha Levin Subject: [PATCH 6.6 0764/1167] iio: proximity: sx9360: fix sampling frequency write and available range Date: Fri, 4 Dec 2026 19:03:11 +0900 Message-ID: <20261204100721.982065426@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Arnav Kapoor commit d252425c1533bfacd5940faa3371af82196bff5b upstream. GNRL_CTRL1/2 hold the scan period in units of 8192 / FOSC, but sx9360_set_samp_freq() computes freq * 8192 / FOSC and mis-scales val2, which is in micro units. Any integer frequency then programs a period of 0, and fractional parts above ~0.26 Hz overflow the int, writing 0x10 to GNRL_CTRL1 outside the SCANPERIOD field. Compute FOSC / (freq * 8192), rounded to the closest value, and reject frequencies outside the 1..2047 period range. The available range doesn't match the limits in the comments next to it either: 4 MHz / 8192 is 488.28125 Hz, not 448.28125 Hz, 4 MHz / (8192 * 2047) is 0.238535 Hz, and (max - min) / 2047 is 0.238419 Hz. Use those values. Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support") Signed-off-by: Arnav Kapoor Cc: stable@vger.kernel.org Signed-off-by: Jonathan Cameron [ sx9360_set_samp_freq() still uses mutex_lock()/mutex_unlock() here, not guard(); keep that and add the new checks before taking the lock. ] Signed-off-by: Arnav Kapoor Signed-off-by: Sasha Levin --- drivers/iio/proximity/sx9360.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c index 2c4e14a4fe9fb..55031cec72708 100644 --- a/drivers/iio/proximity/sx9360.c +++ b/drivers/iio/proximity/sx9360.c @@ -15,11 +15,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include @@ -45,6 +47,7 @@ #define SX9360_REG_GNRL_CTRL1_SCANPERIOD_MASK GENMASK(2, 0) #define SX9360_REG_GNRL_CTRL2 0x12 #define SX9360_REG_GNRL_CTRL2_PERIOD_102MS 0x32 +#define SX9360_REG_GNRL_SCANPERIOD_MAX GENMASK(10, 0) #define SX9360_REG_GNRL_REG_2_PERIOD_MS(_r) \ (((_r) * 8192) / (SX9360_FOSC_HZ / 1000)) #define SX9360_REG_GNRL_FREQ_2_REG(_f) (((_f) * 8192) / SX9360_FOSC_HZ) @@ -184,9 +187,9 @@ static const struct { int val; int val2; } sx9360_samp_freq_interval[] = { - { 0, 281250 }, /* 4MHz / (8192 * 2047) */ - { 0, 281250 }, - { 448, 281250 }, /* 4MHz / 8192 */ + { 0, 238535 }, /* 4MHz / (8192 * 2047) */ + { 0, 238419 }, + { 488, 281250 }, /* 4MHz / 8192 */ }; static const struct regmap_range sx9360_writable_reg_ranges[] = { @@ -387,11 +390,26 @@ static int sx9360_read_avail(struct iio_dev *indio_dev, static int sx9360_set_samp_freq(struct sx_common_data *data, int val, int val2) { - int ret, reg; + unsigned int period_ticks; + u64 freq_uHz; __be16 buf; + int ret; + + if (val < 0 || val2 < 0) + return -EINVAL; + + freq_uHz = (u64)val * MICROHZ_PER_HZ + val2; + if (!freq_uHz) + return -EINVAL; + + /* The register holds the scan period in ticks of 8192 FOSC cycles. */ + period_ticks = DIV64_U64_ROUND_CLOSEST((u64)SX9360_FOSC_HZ * MICROHZ_PER_HZ, + freq_uHz * 8192); + if (!period_ticks || period_ticks > SX9360_REG_GNRL_SCANPERIOD_MAX) + return -EINVAL; + + buf = cpu_to_be16(period_ticks); - reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ); - buf = cpu_to_be16(reg); mutex_lock(&data->mutex); ret = regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf, -- 2.53.0