From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795954697; b=wsPVdCLsI96217Y6XoeT9QWNE35rpqAK82ZlijFXRlYBCcASSnbhVftF7R4VtzxLNm/2 G7X4pqO85E7FoEro11wiOhjUH3lERmGypWK1e+eWLWy/utHBC3rKDvclian3jfaw/pB5p AvyX6QI07QUADNwImmMGs/kNh9UEV1hDnDBJCvCnRONvnjb60XWjSdp2Y3qpZn0+lG37F vqsM/dalp8CLCFWC2TIp7b3BjypNvzKt67wmCEJnvYAPIGFRLhmfr08VWhinPA9b7KbqZ HPt2/yeUsVQZB25BS/fJ++Y36PQofqXIeVDt8T6yr2ZO8vfzPnbHTHCSgyYdxA00ypw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795954697; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=3dzcHHUyE52gVZtroJZW479/g69tC3Ht1fyZ+FTyKmU=; b=X8I5QzIuNbKVYxjqTzL/3EhMM8qf87Kbcnx9IUucAzOQ3umKwaD5RicpvL6B4E3ax1Ou HjayAPEIzfcJpWB18QnMmhhw7zwHSxhE6L0XnKbCeeulK/ugYntcill0n/GsPC9+dEQ2s 0xUCW2kJyZBxBBNYAG57s85ZwVALMyzjxguLjsBQq+TR0RV6xy3wIKzfifGXlg8vctu5K BTWaYtiSzv68MyTZlK82H2EsLLt54e5/7UIuAiKJKxyFlVW43MhkUQJFX1uH1ZADmDHjb ZFLI2eBHV2KL8mCcx8gGNcQGRDDHGHZbfZUO7j0yvFJcnXmTJ38fLAA8XJOa1FNhYCw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OqPhtWZi; arc=pass header.oldest-pass=0 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 479B9606A31C; Sun, 29 Nov 2026 12:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OqPhtWZi Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795954696; b=F3lgGr1Hb6k+c3qosyYv87jannvMpFXB1DWwZABctlJb6v7HKo0SolmesLlzpuaFdO+y 8efbEYRGp9b4GpMWWmLT1zE0AnXoXN0Bk1OTGFVZA+xNVFUR+yfvTzNhjiTNYleLAjZyo Fne5pJkVuTMzlUTytc5tM8sAM5TsfEKfUJSviWWC2JO9B0enddi1e520KIQQzlYPw1ktE s0/irU3HvvXyayLHmrD4vq8xV3ESZLyDKE8iPpYeCkyGdM0DUQD0EmxcbbQROdfJTNZs8 ET2G4dXn2ynt+PKzz3jxhcphJBgE7VMoJSemxMnGQeYeZHgxUHtLxTYqOobzBLI1r/w== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795954696; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=3dzcHHUyE52gVZtroJZW479/g69tC3Ht1fyZ+FTyKmU=; b=Z/ZAtOgjjw3NNPFeP5wXwaqV9X6kI5qiDAsaYfUakg/Sg87C5bNz4j1t3ShK3KMFpgJE EUHsZhK78HcnT4jZ1tSfXk5xpb5bFWoDjgTZ2MIsmLpRiV1HU0IeKnU88ApzR1ahd/Td5 5VNLv4D/3cQedJ/Pk+UDCjDvOn2PDJzth/WldvBIsLUqJn2Tr0I0j3+s4U18xYGp/XdvL C2a52GQBKOqD20klMSgMQi01Xcj03DxsftvFadGjz/wP9zhR3Maz81KZaFprz7latpCP3 xMV8oexScJhKC8afBFEsxwL5UTsdAFdU/a+CNr7hZN9AFERL/3N0qEv/RsvyUPjTTRA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 832A91F00898; Sun, 29 Nov 2026 12:18:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795954696; bh=3dzcHHUyE52gVZtroJZW479/g69tC3Ht1fyZ+FTyKmU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=OqPhtWZi2rHqauG1IDFqPJto5Zw0ngqmRwEscgmSN+vmCs1vlc5nyAbH1EuOenVLD WkKNaOtOronzxrVNDKpDTODE5tObenuHc9zMyI+9MEh+NUvYENMVepzhwN7Cbxuu9n 7b+SeFBa7mZo05JYa7qC/ru7zcN0NXxQbksFmjIFse0Mkc5VsfrRjiphGHq/YdvkkQ vW09KdeZrLn7hRsZWOKYPH8PuKqwcSybXc5qUTJ8UvPd3lAJBzgWjV99QBcIuCMn5r oPw6yoZaoS9EsXFEWoJTNX+CuLNpYOHDmGqkPw2i3acFhNbXqCB30EbgtTBxwUSokj ySaBnT8VV47Wg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 10A544E474; Sun, 29 Nov 2026 12:18:08 +0000 (UTC) From: Raffaele Tranfaglia via B4 Relay Date: Sun, 29 Nov 2026 12:18:06 +0000 Subject: [PATCH v3 2/3] regmap: mask values to val_bits on write X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261129-regmap-kunit-val-bits-v3-2-499917221d04@gmail.com> References: <20261129-regmap-kunit-val-bits-v3-0-499917221d04@gmail.com> In-Reply-To: <20261129-regmap-kunit-val-bits-v3-0-499917221d04@gmail.com> To: Mark Brown , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Johan Hovold , Liam Girdwood , Jaroslav Kysela , Takashi Iwai Cc: Andy Shevchenko , Jonathan Cameron , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, driver-core@lists.linux.dev, linux-sound@vger.kernel.org, Neo Chang , Raffaele Tranfaglia X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795954687; l=3926; i=raffaele.tranfaglia@gmail.com; s=20261011; h=from:subject:message-id; bh=BgLjede5PCxK48qkoIHySfxu0kq1jesTpuZqm5xxiZw=; b=xSWBGyIZ8z8S4bI5xgT570MopISESlhIHcShYZZTLQeBsZJJ1mZ68sbruauyevZO2HsSDhsrW tGIVZADxqN9Crt7Gi6no/Vl6XfysRDz3TuehvovoeZtSHQeFTf6Gxxc X-Developer-Key: i=raffaele.tranfaglia@gmail.com; a=ed25519; pk=mweuSGNM6vguCTdqa+aiHwMXdgk70suj2wolzCp6XGM= X-Endpoint-Received: by B4 Relay for raffaele.tranfaglia@gmail.com/20261011 with auth_id=1160 X-Original-From: Raffaele Tranfaglia Reply-To: raffaele.tranfaglia@gmail.com Content-Transfer-Encoding: 7bit From: Raffaele Tranfaglia regmap_write() takes an unsigned int, also for regmaps with val_bits smaller than 32. What happens to the bits above val_bits depends on the bus and on the cache type: - buses with a raw write() go through the value formatters, which drop the bits that do not fit in the formatted value; - buses and users with only reg_write() get the value unmodified. Some of them, like the SMBus backends of regmap-i2c, then return -EINVAL; - the cache may store a value that differs from the one written to the device. For example the adux1020 driver writes ~u16, which is promoted to int, to a 16-bit register. This works on I2C adapters and fails with -EINVAL on SMBus-only ones. Mask the value to val_bits in _regmap_write(), before updating the cache, so that all buses and cache types behave the same. Do the same for the values cached by _regmap_multi_reg_write(), which does not use _regmap_write() on devices supporting multi register writes. val_bits is documented as mandatory, but some users providing their own reg_read() and reg_write() leave it at 0, for example because their registers have different sizes. Do not mask values for these. Suggested-by: Andy Shevchenko Signed-off-by: Raffaele Tranfaglia --- drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regmap.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h index ea375cc8ae87..6a7efb10127b 100644 --- a/drivers/base/regmap/internal.h +++ b/drivers/base/regmap/internal.h @@ -33,6 +33,7 @@ struct regmap_format { size_t reg_bytes; size_t pad_bytes; size_t val_bytes; + unsigned int val_mask; s8 reg_shift; void (*format_write)(struct regmap *map, unsigned int reg, unsigned int val); diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c index fc0029600184..ff09e0d3c357 100644 --- a/drivers/base/regmap/regmap.c +++ b/drivers/base/regmap/regmap.c @@ -6,6 +6,7 @@ // // Author: Mark Brown +#include #include #include #include @@ -1093,6 +1094,11 @@ struct regmap *__regmap_init(struct device *dev, map->format.reg_shift = config->reg_shift; map->format.reg_bytes = BITS_TO_BYTES(config->reg_bits); map->format.val_bytes = BITS_TO_BYTES(config->val_bits); + /* Some users with their own register accessors don't set val_bits */ + if (config->val_bits) + map->format.val_mask = GENMASK(config->val_bits - 1, 0); + else + map->format.val_mask = UINT_MAX; map->format.buf_size = BITS_TO_BYTES(config->reg_bits + config->val_bits + config->pad_bits); if (config->reg_stride) map->reg_stride = config->reg_stride; @@ -1983,6 +1989,9 @@ int _regmap_write(struct regmap *map, unsigned int reg, if (!regmap_writeable(map, reg)) return -EIO; + /* Make sure that the cache and the device see the same value */ + val &= map->format.val_mask; + if (!map->cache_bypass && !map->defer_caching) { ret = regcache_write(map, reg, val); if (ret != 0) @@ -2011,6 +2020,8 @@ int _regmap_write(struct regmap *map, unsigned int reg, * @reg: Register to write to * @val: Value to be written * + * If the regmap_config sets val_bits, any bits of @val above it are ignored. + * * A value of zero will be returned on success, a negative errno will * be returned in error cases. */ @@ -2608,7 +2619,7 @@ static int _regmap_multi_reg_write(struct regmap *map, if (!map->cache_bypass) { for (i = 0; i < num_regs; i++) { - unsigned int val = regs[i].def; + unsigned int val = regs[i].def & map->format.val_mask; unsigned int reg = regs[i].reg; ret = regcache_write(map, reg, val); if (ret) { -- 2.56.0