From nobody Fri Dec 19 11:27:27 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50A6AEE14C3 for ; Wed, 6 Sep 2023 20:01:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238860AbjIFUBo (ORCPT ); Wed, 6 Sep 2023 16:01:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243634AbjIFUAl (ORCPT ); Wed, 6 Sep 2023 16:00:41 -0400 Received: from mail.zeus03.de (www.zeus03.de [194.117.254.33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8539219B5 for ; Wed, 6 Sep 2023 13:00:30 -0700 (PDT) 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=xJsHUvQhrLbRjhVbZzw9GQZf+nq+ia33sWzU4P2ylJo=; b=LLsSji ouIYueos70JY0o2u+vtusywFfMoaEnLidS6PvOdmV4mHgxGynGnGfcixrXic068k IetzyAorzwCMqmRanV98lI/fGLmh+onLxnL3uUgb5e6RpTjzOSYt0wDjvJ/21Ah1 +0kKr8neBy5ptgfxZjICHX2SJzuJsfRYN5sVo5mRwYDnpXfXYf8+l6EIBtYzSD6a tbLBr2+rSAZwzYLg7rP8iCn7bzZSqr8zPOwAfSHPEKD8U9bbZWvT5ClvSkyB+rUP nSxkHiT+2kemliuzqSYlp1moanQNvTaTFoYz9QrFDavMhQa3XCGZcnYvemLSG2hL RG+5fConjg1c2SAA== Received: (qmail 2929864 invoked from network); 6 Sep 2023 22:00:28 +0200 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 6 Sep 2023 22:00:28 +0200 X-UD-Smtp-Session: l3s3148p1@CvrqMbYELIsgAQnoAFZhALrSGIaWNE/A From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: Wolfram Sang , Andi Shyti , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/5] i2c: rcar: calculate divider instead of brute-forcing it Date: Wed, 6 Sep 2023 22:00:21 +0200 Message-Id: <20230906200024.5305-4-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230906200024.5305-1-wsa+renesas@sang-engineering.com> References: <20230906200024.5305-1-wsa+renesas@sang-engineering.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Instead of trying all values, we can actually compute it as the comment suggests. It is unclear what the comment means with "involved", it works nicely. Signed-off-by: Wolfram Sang Reviewed-by: Geert Uytterhoeven --- @Geert: I hope the formulas are more clear now? @Andi: I don't think that replacing 0x3f with a define helps understanding the code, but I am open for discussion. drivers/i2c/busses/i2c-rcar.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c index 4bf47e35094f..2585092bed52 100644 --- a/drivers/i2c/busses/i2c-rcar.c +++ b/drivers/i2c/busses/i2c-rcar.c @@ -303,24 +303,16 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_p= riv *priv) round =3D (round + 500) / 1000; =20 /* - * SCL =3D ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) - * - * Calculation result (=3D SCL) should be less than - * bus_speed for hardware safety - * - * We could use something along the lines of - * div =3D ick / (bus_speed + 1) + 1; - * scgd =3D (div - 20 - round + 7) / 8; - * scl =3D ick / (20 + (scgd * 8) + round); - * (not fully verified) but that would get pretty involved + * SCL =3D ick / (20 + 8 * SCGD + F[(ticf + tr + intd) * ick]) + * 20 + 8 * SCGD + F[...] =3D ick / SCL + * SCGD =3D ((ick / SCL) - 20 - F[...]) / 8 + * Result (=3D SCL) should be less than bus_speed for hardware safety */ - for (scgd =3D 0; scgd < 0x40; scgd++) { - scl =3D ick / (20 + (scgd * 8) + round); - if (scl <=3D t.bus_freq_hz) - break; - } + scgd =3D DIV_ROUND_UP(ick, t.bus_freq_hz ?: 1); + scgd =3D DIV_ROUND_UP(scgd - 20 - round, 8); + scl =3D ick / (20 + 8 * scgd + round); =20 - if (scgd =3D=3D 0x40) + if (scgd > 0x3f) goto err_no_val; =20 dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u, SCGD: %u\n", --=20 2.35.1