From nobody Mon Jun 15 02:47:43 2026 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0229E3D091F for ; Tue, 7 Apr 2026 17:03:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775581413; cv=none; b=UrLDzXBpVBu956wZ4gS3XroR+7Gavjui2bNmN97ogF73HDUHmg7XQlxRgfMoM7gptfCTBlQR+DUWlnGh4S8Uv4Bln97oLiYnpBq5ftk9fj/TAizr+yKjW+KhUa80l6MRJgnNEWbwfoaEu7+pVj7f9/BKckbBeSIW4gyIJKqJ3Aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775581413; c=relaxed/simple; bh=O4uHzkKNl3IGefNGYCIuKpLs23GHSxTNT7OQCSZWAb4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o1gZDMV5V00Z0Y8JsZpFj9zA3R2XwM0Q+DmjiWoVYC3nPFosRcoIrvEjfxGCI0q4+AHpioRXnW/qV35tgT6OHosOCOMwzyJEQVNOYKUsfOnfwsLUZNl75VHlnYHcg83OHxjNx1CKELI+JVsm0RekEBpIdGqSuzPeAg8sYbWKQ48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fwGTc+No; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fwGTc+No" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-35d932cc948so3144508a91.2 for ; Tue, 07 Apr 2026 10:03:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775581411; x=1776186211; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sZoeSKMzor941OtTI2XswOre9sDwC8ZQogmJadnwKsI=; b=fwGTc+NoQzs7CC/0HsmFHA2t1fWwpmdRHuWZ41u+LDvPAEkRC51wI2OiNQ8kTsM5FV r7jonuR+8Xc5B+YyoFkbOxC9qXW/z8bhYXD7vnYNBd9eTpIvLcTVbiQ6nliT1rEfNTwb sUiRJXKvrAEA9Z6bGEfosa/qc/FvpTbDFKKxT73pnA29ZdCQb4ckDIzDtPyONcTFsGU1 kXaHN+GB7C666e/avzv8mTBCz3jeRGrMfBnfIjK/IxHe52diXz+R4OjRYnJvxgsYzudG DCxSk80oi9Z3BsG6AOO3101JQDm1LpmV7sV23mSaHgFR2ax0t2ZEkqt8Kx8DrYDIOxZp QeSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775581411; x=1776186211; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=sZoeSKMzor941OtTI2XswOre9sDwC8ZQogmJadnwKsI=; b=QqKiPJEdHNxjVUJSNCiYx+3vZ3cKrJy4qi5z4SOPYHQc2McDoBBwSatPl7MtIsK3iC gxP0X/q5I/USNxcpkSE4Dg34w04a8i55fWQPj5585JipMcRJbdT8minHdVGdU5YPaNLC LjDd0Tld9TbG7EaLWT9XQIRPN5t6AfKhTO4aa6X93cikpaBiuEFpSXBFaSeW4VdSC45/ psWQB0S9qDX3ZnpmRBO3NAAkdGK80S4/yVbmCznJ7Rot27LM2nfDs57V5H5+zRp7UB6J ZWzmRsNsxVJ56qX5stQopzKVLzIxEkBJIToYoqXliz5SewJyOMOpM/bekyH6C30Xb6mW LuFA== X-Forwarded-Encrypted: i=1; AJvYcCUgWqJ8VCnsZOiMqUcZcvSqbwAF4/GTBP0C5vY1vpHxcEFLNWUIu0XiCVLexP/n29DjVFPsfghc5DLpxXg=@vger.kernel.org X-Gm-Message-State: AOJu0YxaEBPjwMlzqAaLepbVyP4tCYCkD4xZ4YXOuLWNvpx7qJzGwe8c EnP9Q+5GAeCtQRe0vwvgGTwgC3uOl368VQt3ZsD1mwy0lqLA/oCfoHOh X-Gm-Gg: AeBDies1SXwHm+a3C+a4vXp2Lnz50aFqQf1KjacEiXwxwsWF43ZrxyoRH62g5czA90H L+ePPtooGXPkplZ0qWTNSTlAleMg6TfWM+uAY/l9osB8g+wmsTjqWLyQ51kDJdm0rQ2Nsblcua/ vYtKeui/g4ehSVz71JokjgRxYksXeLAmRaKZxYWJxhJfYlb+iZSbBIlgO/KGhff7xJpLTP5EHDC sJViuSqS0NKBk+rnyDLM5ifOwpbQGSIbROxDj7/KyQLSigqpnpxX2gzzQ+Ychq2i1X61Gi2PYkD +YOR3td0H4p++QIdgny6S2LM6V/rpFWHIRgWwL21Fj6rzEseEGmQr5I/z99EpEP4O1hNtngqlyL XS+qMhPRfarrDYOe8pWd56IKWgjJxIUih6OZ9oTnaMMGvyin7dV49qk9MRDmu/WeTu1xJdzOd9Y biFl3y/A2P0mu7yL4KN8l/LY6LQVE9ptqRw7Y+FtBGkQd6UdEb1MGZ9TnNOL0dHGKvQNR/IU3uv zLEhZOUsr7uOXAl2P111TmlpwuvX49e/8SVjp13F0Z8H5t0YVMgqnCaHQ== X-Received: by 2002:a17:903:32cd:b0:2b2:4697:78f5 with SMTP id d9443c01a7336-2b281798164mr173228185ad.36.1775581411233; Tue, 07 Apr 2026 10:03:31 -0700 (PDT) Received: from lorddaniel-VivoBook-ASUSLaptop-K3502ZA-S3502ZA.www.tendawifi.com ([14.139.108.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b27478cb4fsm196617905ad.29.2026.04.07.10.03.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 10:03:30 -0700 (PDT) From: Piyush Patle To: Mark Brown Cc: Liam Girdwood , Jaroslav Kysela , Takashi Iwai , Thierry Reding , Jonathan Hunter , Sheetal , Kuninori Morimoto , linux-sound@vger.kernel.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] ASoC: tegra210_adx: simplify byte map get/put logic Date: Tue, 7 Apr 2026 22:33:07 +0530 Message-Id: <20260407170308.100238-2-piyushpatle228@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260407170308.100238-1-piyushpatle228@gmail.com> References: <20260407170308.100238-1-piyushpatle228@gmail.com> 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 Content-Type: text/plain; charset="utf-8" The byte-map controls ("Byte Map N") already expose a value range of [0, 256] to userspace via SOC_SINGLE_EXT(), where 256 is the "disabled" sentinel. The driver stored this state as a byte-packed u32 map[] array plus a separate byte_mask[] bitmap tracking which slots were enabled, because 256 does not fit in a byte. As a result get_byte_map() had to consult byte_mask[] to decide whether to report the stored byte or 256, and put_byte_map() had to keep the two arrays in sync on every write. Store each slot as a u16 holding the control value directly (0..255 enabled, 256 disabled). This is the native representation for what userspace already sees, so get_byte_map() becomes a direct return and put_byte_map() becomes a compare-and-store. The hardware-facing packed RAM word and the IN_BYTE_EN mask are now derived on the fly inside tegra210_adx_write_map_ram() from the slot array, which is the only place that needs to know about the hardware layout. This also lets us drop the byte_mask field from struct tegra210_adx. Slots are initialised to 256 in probe() so the default reported value stays "disabled", matching previous behaviour. Values written from userspace that fall outside [0, 255] are clamped to 256 ("disabled") exactly as before -- no userspace-visible change. As a side effect this also fixes a latent bug in the previous put_byte_map(): because it compared the enable mask rather than the stored byte, changing a slot from one enabled value to another enabled value (e.g. 42 -> 99) would early-return without persisting the new value. Also fix a potential undefined behavior when constructing the packed RAM word by ensuring the shift operates on a u32 value. Addresses TODO left in tegra210_adx_get_byte_map(). Signed-off-by: Piyush Patle --- sound/soc/tegra/tegra210_adx.c | 80 ++++++++++++++++------------------ sound/soc/tegra/tegra210_adx.h | 5 ++- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/sound/soc/tegra/tegra210_adx.c b/sound/soc/tegra/tegra210_adx.c index 95875c75ddf8..67948459e884 100644 --- a/sound/soc/tegra/tegra210_adx.c +++ b/sound/soc/tegra/tegra210_adx.c @@ -47,6 +47,7 @@ static const struct reg_default tegra264_adx_reg_defaults= [] =3D { =20 static void tegra210_adx_write_map_ram(struct tegra210_adx *adx) { + unsigned int byte_mask[TEGRA264_ADX_BYTE_MASK_COUNT] =3D { 0 }; int i; =20 regmap_write(adx->regmap, TEGRA210_ADX_CFG_RAM_CTRL + @@ -55,15 +56,28 @@ static void tegra210_adx_write_map_ram(struct tegra210_= adx *adx) TEGRA210_ADX_CFG_RAM_CTRL_ADDR_INIT_EN | TEGRA210_ADX_CFG_RAM_CTRL_RW_WRITE); =20 - for (i =3D 0; i < adx->soc_data->ram_depth; i++) + for (i =3D 0; i < adx->soc_data->ram_depth; i++) { + u32 word =3D 0; + int b; + + for (b =3D 0; b < 4; b++) { + unsigned int slot =3D i * 4 + b; + u16 val =3D adx->map[slot]; + + if (val >=3D 256) + continue; + + word |=3D (u32)val << (b * 8); + byte_mask[slot / 32] |=3D 1U << (slot % 32); + } regmap_write(adx->regmap, TEGRA210_ADX_CFG_RAM_DATA + - adx->soc_data->cya_offset, - adx->map[i]); + adx->soc_data->cya_offset, word); + } =20 for (i =3D 0; i < adx->soc_data->byte_mask_size; i++) regmap_write(adx->regmap, TEGRA210_ADX_IN_BYTE_EN0 + (i * TEGRA210_ADX_AUDIOCIF_CH_STRIDE), - adx->byte_mask[i]); + byte_mask[i]); } =20 static int tegra210_adx_startup(struct snd_pcm_substream *substream, @@ -188,27 +202,10 @@ static int tegra210_adx_get_byte_map(struct snd_kcont= rol *kcontrol, { struct snd_soc_component *cmpnt =3D snd_kcontrol_chip(kcontrol); struct tegra210_adx *adx =3D snd_soc_component_get_drvdata(cmpnt); - struct soc_mixer_control *mc; - unsigned char *bytes_map =3D (unsigned char *)adx->map; - int enabled; + struct soc_mixer_control *mc =3D + (struct soc_mixer_control *)kcontrol->private_value; =20 - mc =3D (struct soc_mixer_control *)kcontrol->private_value; - enabled =3D adx->byte_mask[mc->reg / 32] & (1 << (mc->reg % 32)); - - /* - * TODO: Simplify this logic to just return from bytes_map[] - * - * Presently below is required since bytes_map[] is - * tightly packed and cannot store the control value of 256. - * Byte mask state is used to know if 256 needs to be returned. - * Note that for control value of 256, the put() call stores 0 - * in the bytes_map[] and disables the corresponding bit in - * byte_mask[]. - */ - if (enabled) - ucontrol->value.integer.value[0] =3D bytes_map[mc->reg]; - else - ucontrol->value.integer.value[0] =3D 256; + ucontrol->value.integer.value[0] =3D adx->map[mc->reg]; =20 return 0; } @@ -218,23 +215,22 @@ static int tegra210_adx_put_byte_map(struct snd_kcont= rol *kcontrol, { struct snd_soc_component *cmpnt =3D snd_kcontrol_chip(kcontrol); struct tegra210_adx *adx =3D snd_soc_component_get_drvdata(cmpnt); - unsigned char *bytes_map =3D (unsigned char *)adx->map; - int value =3D ucontrol->value.integer.value[0]; struct soc_mixer_control *mc =3D (struct soc_mixer_control *)kcontrol->private_value; - unsigned int mask_val =3D adx->byte_mask[mc->reg / 32]; + unsigned int value =3D ucontrol->value.integer.value[0]; =20 - if (value >=3D 0 && value <=3D 255) - mask_val |=3D (1 << (mc->reg % 32)); - else - mask_val &=3D ~(1 << (mc->reg % 32)); + /* + * Match the previous behaviour: any value outside [0, 255] is + * treated as the "disabled" sentinel (256). Negative values from + * userspace fold in through the unsigned cast and are caught here. + */ + if (value > 255) + value =3D 256; =20 - if (mask_val =3D=3D adx->byte_mask[mc->reg / 32]) + if (adx->map[mc->reg] =3D=3D value) return 0; =20 - /* Update byte map and slot */ - bytes_map[mc->reg] =3D value % 256; - adx->byte_mask[mc->reg / 32] =3D mask_val; + adx->map[mc->reg] =3D value; =20 return 1; } @@ -675,7 +671,7 @@ static int tegra210_adx_platform_probe(struct platform_= device *pdev) const struct of_device_id *match; struct tegra210_adx_soc_data *soc_data; void __iomem *regs; - int err; + int err, i; =20 adx =3D devm_kzalloc(dev, sizeof(*adx), GFP_KERNEL); if (!adx) @@ -700,16 +696,14 @@ static int tegra210_adx_platform_probe(struct platfor= m_device *pdev) =20 regcache_cache_only(adx->regmap, true); =20 - adx->map =3D devm_kzalloc(dev, soc_data->ram_depth * sizeof(*adx->map), - GFP_KERNEL); + adx->map =3D devm_kcalloc(dev, soc_data->ram_depth * 4, + sizeof(*adx->map), GFP_KERNEL); if (!adx->map) return -ENOMEM; =20 - adx->byte_mask =3D devm_kzalloc(dev, - soc_data->byte_mask_size * sizeof(*adx->byte_mask), - GFP_KERNEL); - if (!adx->byte_mask) - return -ENOMEM; + /* Initialize all byte map slots as disabled (value 256). */ + for (i =3D 0; i < soc_data->ram_depth * 4; i++) + adx->map[i] =3D 256; =20 tegra210_adx_dais[TEGRA_ADX_IN_DAI_ID].playback.channels_max =3D adx->soc_data->max_ch; diff --git a/sound/soc/tegra/tegra210_adx.h b/sound/soc/tegra/tegra210_adx.h index 176a4e40de0a..afe95e45458f 100644 --- a/sound/soc/tegra/tegra210_adx.h +++ b/sound/soc/tegra/tegra210_adx.h @@ -8,6 +8,8 @@ #ifndef __TEGRA210_ADX_H__ #define __TEGRA210_ADX_H__ =20 +#include + /* Register offsets from TEGRA210_ADX*_BASE */ #define TEGRA210_ADX_RX_STATUS 0x0c #define TEGRA210_ADX_RX_INT_STATUS 0x10 @@ -88,8 +90,7 @@ struct tegra210_adx_soc_data { =20 struct tegra210_adx { struct regmap *regmap; - unsigned int *map; - unsigned int *byte_mask; + u16 *map; const struct tegra210_adx_soc_data *soc_data; }; =20 --=20 2.34.1 From nobody Mon Jun 15 02:47:43 2026 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 121C43C7DF7 for ; Tue, 7 Apr 2026 17:03:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775581428; cv=none; b=GRoBD3r9CvHtIZ9HuLao/XiAG+VTVA6/uM4Fbt2HkoT798TkjljCEtkJg20L4UOu6on67itE3Oq5G05T5pTIuVJX5qy3RGsaDkshi9GJ+lSCBvIhZZ0UqCmHbg2oP8GhjP6DzOyd7X0RsHbTMxPn4kRNmxnidOMmfzMMskVamBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775581428; c=relaxed/simple; bh=LEaAiWuc2iDIokGArBD4Y2Hs4D2o0VD9O5lvKNgWjEk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M5wMS/LOso0918H8PSeD7j9McWEzREZoDEN1yLtGZYtUfHKUCt1pFZjKZOSOAl1JW7wef33GA7VZhQ6w85w60s8Klw2R3gIeN4G0ADFPtfrpSVK1TCD1Ib8kaCsDjY9dMsuacCr0ezhD585ecfyS+ieb3eacQY2q8+WoaSIsoyc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eGQIXz5n; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eGQIXz5n" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-c757a9251faso1856729a12.1 for ; Tue, 07 Apr 2026 10:03:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775581426; x=1776186226; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0fesHMBqfmQJa8k2eFKVrT51rcc3PDADSOTcCGgAHuI=; b=eGQIXz5n3boH74uQVLyhloS2PGw2S9ZJu3TKHSzEXjmWJh1Y3rVGXD9m2qNm6pfT0s TC9IPdZsxn6WKl8zzAO39M7joL+DY0PQ0zopd4mAa54nd4qwrLG8NstuPzNchPIUOaPf LIzCDXgmq840lmExwyx+npq8VFT2nH/2nFEEaCrj8y9Yz/1ICBoZWvWLYKZ12P9Q40I9 k+wxxFlPK6o+dkTkzGM6UrUW7JZ6e5kIHPGq/VbF/vzM50IwzDj/7t8+AEtmOTPfgaIW UxraawVvXXKHv28b0vWLYV96pjhsSV3f+KTVVYoAdCBU8Q2BMSpjV8uY+2cx7FGKNBK4 XQ9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775581426; x=1776186226; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=0fesHMBqfmQJa8k2eFKVrT51rcc3PDADSOTcCGgAHuI=; b=fZQ419xGqn3DHgtq9KUBWNdV7eOl13D/S+A/W3/JKLH/LLkZl2J/7yAMbooMhGazjd K2mCBkDwV4g9xkbzZcOA8lG5C8VaU+QjIN/c3gPdBE1qRAw/FM0qUf/SPDsDkiwukeV/ CnFjWYR40Lzk8zwysgDm1HX7bRo9dVet4u1XqG5+w/rTk0UAFp/4KJOhJi3XfXE8HIA/ TEZhNSuLbL61J6eb4pq4mC/1isZnM7W3/A/QpsYQ9ORzIax1C97y2k8wC5I6500hcUev a6kqw3jpWEEzGWHT0eq3lqhxQFWNBrUBrMXtgFLBb+QvyoJ0jzuYAJ4VzyEZFm56L0cc T8Ew== X-Forwarded-Encrypted: i=1; AJvYcCUyfqVUNC9QJxabo2s1pr7wMPbXNLTdCtPcQ0drx/KhaSYalWlNpPVdWFfUMphmSWrZEHVEAsRVywJ7f28=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1EYaUWFsJ4SBYY7Wu5CmdzMQh8X5ej5uXTXQjHM6JKFSs2nWQ yle6jGXhr2fl5qq2L0vFYfM6jJjMUFY+ep3haXvykywUN2Bluz/nz31E X-Gm-Gg: AeBDiesvJmGEI3hPhILVHmpHt1rGNFVMXbhIQVN15nsNDzX43bqkoTa89whA8wcCzd3 60+ovQqTH8QB1509YQfTJcCgobKgKzMJgVw6OdzFQVsQjtCZf3AaIUuMMQ4CfvrViE0fotnz6gT NCqphBhdjMf5Sa2J2pWhXmsiADXZOI65qla4A8bTYA4lHQmBcZTAk8z6S4UlTA3OBGppESoSI5p IiPVMHGLDyyJ/ba5kpBjEO68BHkcgThWW7mpZS/6DU5RZWmFDfRX75andvRQYNR7iwQRYfHqK5k D7MGmVwkUkm+HXKZRIiGBgOOifgyxrWChhlIDEsah3pmPnbrqVr3sT31+lHT113HjhA8eW34qTh zfQq8x3CeGgzs7UzNNe/BsL7sW9wMILc/W1LclcieZ45Ijp7sG9mYZklTLavkD5WP+mUwmqG8P0 o/4HR20/S972HnXfst8rIPpnmiPZl6STKL6Zf9J6U6D7KRWnZ1ecaYRkLjkUdQKxIYBy4tqIpwx QVJCaAYp535OfhVvUeqbpLoThJL119Nf5s16izYjr/y3ddBd0SHutGbNw== X-Received: by 2002:a17:902:f541:b0:2b2:4e5a:9473 with SMTP id d9443c01a7336-2b2816dcedbmr171355955ad.21.1775581426079; Tue, 07 Apr 2026 10:03:46 -0700 (PDT) Received: from lorddaniel-VivoBook-ASUSLaptop-K3502ZA-S3502ZA.www.tendawifi.com ([14.139.108.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b27478cb4fsm196617905ad.29.2026.04.07.10.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 10:03:45 -0700 (PDT) From: Piyush Patle To: Mark Brown Cc: Liam Girdwood , Jaroslav Kysela , Takashi Iwai , Thierry Reding , Jonathan Hunter , Sheetal , Kuninori Morimoto , linux-sound@vger.kernel.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] ASoC: tegra210_amx: simplify byte map get/put logic Date: Tue, 7 Apr 2026 22:33:08 +0530 Message-Id: <20260407170308.100238-3-piyushpatle228@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260407170308.100238-1-piyushpatle228@gmail.com> References: <20260407170308.100238-1-piyushpatle228@gmail.com> 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 Content-Type: text/plain; charset="utf-8" The byte-map controls ("Byte Map N") already expose a value range of [0, 256] to userspace via SOC_SINGLE_EXT(), where 256 is the "disabled" sentinel. The driver stored this state as a byte-packed u32 map[] array plus a separate byte_mask[] bitmap tracking which slots were enabled, because 256 does not fit in a byte. As a result get_byte_map() had to consult byte_mask[] to decide whether to report the stored byte or 256, and put_byte_map() had to keep the two arrays in sync on every write. Store each slot as a u16 holding the control value directly (0..255 enabled, 256 disabled). This is the native representation for what userspace already sees, so get_byte_map() becomes a direct return and put_byte_map() becomes a compare-and-store. The hardware-facing packed RAM word and the OUT_BYTE_EN mask are now derived on the fly inside tegra210_amx_write_map_ram() from the slot array, which is the only place that needs to know about the hardware layout. This also lets us drop the byte_mask field from struct tegra210_amx. Slots are initialised to 256 in probe() so the default reported value stays "disabled", matching previous behaviour. Values written from userspace that fall outside [0, 255] are clamped to 256 ("disabled") exactly as before -- no userspace-visible change. As a side effect this also fixes a latent bug in the previous put_byte_map(): because it compared the enable mask rather than the stored byte, changing a slot from one enabled value to another enabled value (e.g. 42 -> 99) would early-return without persisting the new value. Also fix a potential undefined behavior when constructing the packed RAM word by ensuring the shift operates on a u32 value. Addresses TODO left in tegra210_amx_get_byte_map(). Signed-off-by: Piyush Patle --- sound/soc/tegra/tegra210_amx.c | 77 ++++++++++++++++------------------ sound/soc/tegra/tegra210_amx.h | 5 ++- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/sound/soc/tegra/tegra210_amx.c b/sound/soc/tegra/tegra210_amx.c index bfda82505298..4dd158e6e974 100644 --- a/sound/soc/tegra/tegra210_amx.c +++ b/sound/soc/tegra/tegra210_amx.c @@ -60,6 +60,7 @@ static const struct reg_default tegra264_amx_reg_defaults= [] =3D { =20 static void tegra210_amx_write_map_ram(struct tegra210_amx *amx) { + unsigned int byte_mask[TEGRA264_AMX_BYTE_MASK_COUNT] =3D { 0 }; int i; =20 regmap_write(amx->regmap, TEGRA210_AMX_CFG_RAM_CTRL + amx->soc_data->reg_= offset, @@ -67,14 +68,28 @@ static void tegra210_amx_write_map_ram(struct tegra210_= amx *amx) TEGRA210_AMX_CFG_RAM_CTRL_ADDR_INIT_EN | TEGRA210_AMX_CFG_RAM_CTRL_RW_WRITE); =20 - for (i =3D 0; i < amx->soc_data->ram_depth; i++) + for (i =3D 0; i < amx->soc_data->ram_depth; i++) { + u32 word =3D 0; + int b; + + for (b =3D 0; b < 4; b++) { + unsigned int slot =3D i * 4 + b; + u16 val =3D amx->map[slot]; + + if (val >=3D 256) + continue; + + word |=3D (u32)val << (b * 8); + byte_mask[slot / 32] |=3D 1U << (slot % 32); + } regmap_write(amx->regmap, TEGRA210_AMX_CFG_RAM_DATA + amx->soc_data->reg= _offset, - amx->map[i]); + word); + } =20 for (i =3D 0; i < amx->soc_data->byte_mask_size; i++) regmap_write(amx->regmap, TEGRA210_AMX_OUT_BYTE_EN0 + (i * TEGRA210_AMX_AUDIOCIF_CH_STRIDE), - amx->byte_mask[i]); + byte_mask[i]); } =20 static int tegra210_amx_startup(struct snd_pcm_substream *substream, @@ -212,26 +227,8 @@ static int tegra210_amx_get_byte_map(struct snd_kcontr= ol *kcontrol, struct soc_mixer_control *mc =3D (struct soc_mixer_control *)kcontrol->private_value; struct tegra210_amx *amx =3D snd_soc_component_get_drvdata(cmpnt); - unsigned char *bytes_map =3D (unsigned char *)amx->map; - int reg =3D mc->reg; - int enabled; =20 - enabled =3D amx->byte_mask[reg / 32] & (1 << (reg % 32)); - - /* - * TODO: Simplify this logic to just return from bytes_map[] - * - * Presently below is required since bytes_map[] is - * tightly packed and cannot store the control value of 256. - * Byte mask state is used to know if 256 needs to be returned. - * Note that for control value of 256, the put() call stores 0 - * in the bytes_map[] and disables the corresponding bit in - * byte_mask[]. - */ - if (enabled) - ucontrol->value.integer.value[0] =3D bytes_map[reg]; - else - ucontrol->value.integer.value[0] =3D 256; + ucontrol->value.integer.value[0] =3D amx->map[mc->reg]; =20 return 0; } @@ -243,22 +240,20 @@ static int tegra210_amx_put_byte_map(struct snd_kcont= rol *kcontrol, (struct soc_mixer_control *)kcontrol->private_value; struct snd_soc_component *cmpnt =3D snd_kcontrol_chip(kcontrol); struct tegra210_amx *amx =3D snd_soc_component_get_drvdata(cmpnt); - unsigned char *bytes_map =3D (unsigned char *)amx->map; - int reg =3D mc->reg; - int value =3D ucontrol->value.integer.value[0]; - unsigned int mask_val =3D amx->byte_mask[reg / 32]; + unsigned int value =3D ucontrol->value.integer.value[0]; =20 - if (value >=3D 0 && value <=3D 255) - mask_val |=3D (1 << (reg % 32)); - else - mask_val &=3D ~(1 << (reg % 32)); + /* + * Match the previous behaviour: any value outside [0, 255] is + * treated as the "disabled" sentinel (256). Negative values from + * userspace fold in through the unsigned cast and are caught here. + */ + if (value > 255) + value =3D 256; =20 - if (mask_val =3D=3D amx->byte_mask[reg / 32]) + if (amx->map[mc->reg] =3D=3D value) return 0; =20 - /* Update byte map and slot */ - bytes_map[reg] =3D value % 256; - amx->byte_mask[reg / 32] =3D mask_val; + amx->map[mc->reg] =3D value; =20 return 1; } @@ -727,7 +722,7 @@ static int tegra210_amx_platform_probe(struct platform_= device *pdev) struct device *dev =3D &pdev->dev; struct tegra210_amx *amx; void __iomem *regs; - int err; + int err, i; =20 amx =3D devm_kzalloc(dev, sizeof(*amx), GFP_KERNEL); if (!amx) @@ -750,16 +745,14 @@ static int tegra210_amx_platform_probe(struct platfor= m_device *pdev) =20 regcache_cache_only(amx->regmap, true); =20 - amx->map =3D devm_kzalloc(dev, amx->soc_data->ram_depth * sizeof(*amx->ma= p), - GFP_KERNEL); + amx->map =3D devm_kcalloc(dev, amx->soc_data->ram_depth * 4, + sizeof(*amx->map), GFP_KERNEL); if (!amx->map) return -ENOMEM; =20 - amx->byte_mask =3D devm_kzalloc(dev, - amx->soc_data->byte_mask_size * sizeof(*amx->byte_mask), - GFP_KERNEL); - if (!amx->byte_mask) - return -ENOMEM; + /* Initialize all byte map slots as disabled (value 256). */ + for (i =3D 0; i < amx->soc_data->ram_depth * 4; i++) + amx->map[i] =3D 256; =20 tegra210_amx_dais[TEGRA_AMX_OUT_DAI_ID].capture.channels_max =3D amx->soc_data->max_ch; diff --git a/sound/soc/tegra/tegra210_amx.h b/sound/soc/tegra/tegra210_amx.h index 50a237b197ba..6df9ab0fe220 100644 --- a/sound/soc/tegra/tegra210_amx.h +++ b/sound/soc/tegra/tegra210_amx.h @@ -8,6 +8,8 @@ #ifndef __TEGRA210_AMX_H__ #define __TEGRA210_AMX_H__ =20 +#include + /* Register offsets from TEGRA210_AMX*_BASE */ #define TEGRA210_AMX_RX_STATUS 0x0c #define TEGRA210_AMX_RX_INT_STATUS 0x10 @@ -105,8 +107,7 @@ struct tegra210_amx_soc_data { =20 struct tegra210_amx { const struct tegra210_amx_soc_data *soc_data; - unsigned int *map; - unsigned int *byte_mask; + u16 *map; struct regmap *regmap; }; =20 --=20 2.34.1