From patchwork Sat Dec 14 08:58:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 13908421 X-Patchwork-Delegate: geert@linux-m68k.org Received: from mail.zeus03.de (zeus03.de [194.117.254.33]) (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 4636515B980 for ; Sat, 14 Dec 2024 08:58:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.117.254.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734166731; cv=none; b=IJftFbmgTXFKeBUHGbuT2W4fa17/pkY5XLrdVX2VpIMnjS/+nHhJAbzjTh3cfkv1XAYm2aSiNZ/pAzW9n3JrLDtpG2h2uz3i4XZFMh+vf4OX92C2N+91EsV7WxiIXquCmF+vS/IX6QRl5QwUP1WNRExDf1go+UqFDlEZmdGTSds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734166731; c=relaxed/simple; bh=xj6/6OFbAAgY+sFOgX1xWlAYLZB7CnDEf3EYuh5aqSM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NxSHHeTGAUV61h2mtUDm+yqpGRHL5M9awmYdx/v12y4zhPQGsJwwd4zNBdf6XWG+izqHW9MrZLkwk7xoi5tQ+F6gOt4YHa/WKkzZDhNEwus3SPIpfEPmKNzbTC0MUhG7ewqECmEJsSdBvVEdxVla9nSSb+Z/rH1rulTHgT/v03A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com; spf=pass smtp.mailfrom=sang-engineering.com; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b=GstDXItX; arc=none smtp.client-ip=194.117.254.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b="GstDXItX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=9teRMbCJtAxThOYi72k/gx05ceyxqfnfPYuwz7nN4OU=; b=GstDXI tXXr269JM2ViNvRI+TFMnCafeQbiT2LtB3q4qN8AavYcXVTiG1zSUvMYTXcG4vYm BD2KCRcxKG+8f+BIzZakzzCVb3bESJu+7Z7gO3QeCpmzNGZuz0NmCBqsSaYWR7JM iNJPzwaG1OPT7DgoUfhZYrjr6cpzQelRBEbKyy8krglusW+48nyLReWinHxZz/Q6 BBTonRiun0GKn4kWxQ12CiHtFB+H7gTXCPG6ix7gU/HIJ2QsBxqH6C4wR6DrARDP GQhg2I6dEHYVHC2cqifbVZI9l7sLUOenbIJXNx1Otd37ub/fZ/b5ct/39XUBahMB ac/fVgoD748cgLeQ== Received: (qmail 3036687 invoked from network); 14 Dec 2024 09:58:38 +0100 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 14 Dec 2024 09:58:38 +0100 X-UD-Smtp-Session: l3s3148p1@OWA0Jzcp1IFehhtJ From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, Wolfram Sang , Yury Norov , Rasmus Villemoes Subject: [RFC PATCH 1/2] bitops: add generic parity calculation for u8 Date: Sat, 14 Dec 2024 09:58:31 +0100 Message-ID: <20241214085833.8695-2-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241214085833.8695-1-wsa+renesas@sang-engineering.com> References: <20241214085833.8695-1-wsa+renesas@sang-engineering.com> Precedence: bulk X-Mailing-List: linux-renesas-soc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There are multiple open coded implementations for getting the parity of a byte in the kernel, even using different approaches. Take the pretty efficient version from SPD5118 driver and make it generally available by putting it into the bitops header. As long as there is just one parity calculation helper, the creation of a distinct 'parity.h' header was discarded. Also, the usage of hweight8() for architectures having a popcnt instruction is postponed until a use case within hot paths is desired. The motivation for this patch is the frequent use of odd parity in the I3C specification and to simplify drivers there. Changes compared to the original SPD5118 version are the addition of kernel documentation, switching the return type from bool to int, and renaming the argument of the function. Signed-off-by: Wolfram Sang Tested-by: Guenter Roeck Reviewed-by: Geert Uytterhoeven Acked-by: Yury Norov Reviewed-by: Kuan-Wei Chiu Tested-by: Kuan-Wei Chiu --- include/linux/bitops.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index ba35bbf07798..4ed430934ffc 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -229,6 +229,37 @@ static inline int get_count_order_long(unsigned long l) return (int)fls_long(--l); } +/** + * get_parity8 - get the parity of an u8 value + * @value: the value to be examined + * + * Determine the parity of the u8 argument. + * + * Returns: + * 0 for even parity, 1 for odd parity + * + * Note: This function informs you about the current parity. Example to bail + * out when parity is odd: + * + * if (get_parity8(val) == 1) + * return -EBADMSG; + * + * If you need to calculate a parity bit, you need to draw the conclusion from + * this result yourself. Example to enforce odd parity, parity bit is bit 7: + * + * if (get_parity8(val) == 0) + * val |= BIT(7); + */ +static inline int get_parity8(u8 val) +{ + /* + * One explanation of this algorithm: + * https://funloop.org/codex/problem/parity/README.html + */ + val ^= val >> 4; + return (0x6996 >> (val & 0xf)) & 1; +} + /** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word From patchwork Sat Dec 14 08:58:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 13908420 X-Patchwork-Delegate: geert@linux-m68k.org Received: from mail.zeus03.de (zeus03.de [194.117.254.33]) (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 860C0161328 for ; Sat, 14 Dec 2024 08:58:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.117.254.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734166731; cv=none; b=lRrld9ICMptaWVwVjErylXqy+zOVialXmiQTfWl73dvhrj9o/ivqLllucgy3D5wu++joIkPmPw2OARtcLZgcP0Sr5U0AVj77Q6fGWFSJMXVB45f0iCPivR2A8Ei/5x+H6mkWQVsp6M1MIOq1UgoeMlLO2tQmYKTZv5XfS73Xom0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734166731; c=relaxed/simple; bh=AmlVvrQ8iOfKbH3YXZgneXCCi7K2VfqQc3W7DMkumpk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q9nUiUldXaKayu8a+/IhJ27ijLtOi1wjn5WzHCm56gygSbq4joDxAoKt7hwhlXSCGBD45kWB21ngeUnrUKa7HRwLpWYcNShO/p22zNkH6PQ5V0HKGAvwHSmikm0NvesqtN5Lx0awlgpDqAGVM6Cv1Cgmay4woH2KppAIULPXDME= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com; spf=pass smtp.mailfrom=sang-engineering.com; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b=ch0UNMRd; arc=none smtp.client-ip=194.117.254.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b="ch0UNMRd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=V/U1WDJaY7VuSaVdnDsrNgusQyOaSAv9Mkt7sm9FcRo=; b=ch0UNM RdctcsuKdT0jzhUWxGFPyqjFXA+nUbxMmmHzwUtm0u5pHecp5wepmPxC50xs3qfw DAPCoCcoaoa+7xQmGlQszhrvL9rpPGEZF4tX7XRrciGNCEmJ2UEqp0Qiy0clDAF3 iMygep41I/j74Aq0ml9RfscLW4M52qIXFpglqDxcJBJ5jjIy2q/HO4HQyf+yG4a5 q1P+D1ItmouATvjDIO0hx2fu1bwk1ABKCVWdkmSRLEfhHdLxZhPZIvfVu8OJKZDY ti6RrFKx8HxgdDr8aiauA1BNqaLh0rLq/F7ZPomUncnclfWnmMKr74M1NZ2ICGe3 zqXg/C3+DYnjyE5w== Received: (qmail 3036721 invoked from network); 14 Dec 2024 09:58:39 +0100 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 14 Dec 2024 09:58:39 +0100 X-UD-Smtp-Session: l3s3148p1@zTtCJzcp3oFehhtJ From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, Wolfram Sang , Jean Delvare , Guenter Roeck Subject: [RFC PATCH 2/2] hwmon: (spd5118) Use generic parity calculation Date: Sat, 14 Dec 2024 09:58:32 +0100 Message-ID: <20241214085833.8695-3-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241214085833.8695-1-wsa+renesas@sang-engineering.com> References: <20241214085833.8695-1-wsa+renesas@sang-engineering.com> Precedence: bulk X-Mailing-List: linux-renesas-soc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Make use of the new generic helper for calculating the parity. Signed-off-by: Wolfram Sang Tested-by: Guenter Roeck Reviewed-by: Geert Uytterhoeven Reviewed-by: Kuan-Wei Chiu --- drivers/hwmon/spd5118.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/hwmon/spd5118.c b/drivers/hwmon/spd5118.c index 6cee48a3e5c3..07ab3a47b00c 100644 --- a/drivers/hwmon/spd5118.c +++ b/drivers/hwmon/spd5118.c @@ -291,12 +291,6 @@ static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types typ } } -static inline bool spd5118_parity8(u8 w) -{ - w ^= w >> 4; - return (0x6996 >> (w & 0xf)) & 1; -} - /* * Bank and vendor id are 8-bit fields with seven data bits and odd parity. * Vendor IDs 0 and 0x7f are invalid. @@ -304,7 +298,7 @@ static inline bool spd5118_parity8(u8 w) */ static bool spd5118_vendor_valid(u8 bank, u8 id) { - if (!spd5118_parity8(bank) || !spd5118_parity8(id)) + if (get_parity8(bank) == 0 || get_parity8(id) == 0) return false; id &= 0x7f;