Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4496458rdb; Tue, 12 Dec 2023 00:38:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGq6nm9daonNHT6R5gwzOcUX87RphZY+MEjnLfjQNZzYRFF8j9o1Bd1DcgvGvM7C54Wxgte X-Received: by 2002:a05:6808:15a9:b0:3b6:a8f8:fa0d with SMTP id t41-20020a05680815a900b003b6a8f8fa0dmr7374536oiw.15.1702370332724; Tue, 12 Dec 2023 00:38:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702370332; cv=none; d=google.com; s=arc-20160816; b=iDeNDg4NUcqd6K2xXvjpEQtJgQVXxGWWoCPMhieynyKturovz8j+kyfDiwSLwYJY0u zFuJOoo7jTJRM2QQboHjjDqUgdL4yjrNzif6dbMxVqCFPRRVt0qnwN8k71GlpibWjHQz far5GrPJ9ZCwRiNrdqujXHnQeXC5hVMiSRQ37zGCw3UNiOXqFr14DSkm6QAFzFUunK3t 2jDol3puyZlOQfwxA8Jk2CaWNHHX69FeofOZf7w8DbIJGtRb9lTWnL6i/j6vTQ5hKgVi tGYNPvwrZAjOF8kqnxDWtHUY+9OLYBYmOBtsI+LphJNwIUGICFp75VInRLR5aXADlF/a la7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gVNbK16yCTm/jTMraGo1b2HVcpvGO6//RUZvrNcExkI=; fh=Q2sTNM8dSgd7oN1IFrquoerfHL8GFakmFnYKDXK7xvQ=; b=efu0qSC3vfXjfi7gbo/+WWW0ezrzApMqYZkYxhJqbFG9t9zzvtXe7fAdbnGnD5KK6t 0XhTmPFZ9ei4ajaL7lJw6CQidd55rRCjSZvh0zFvtSncAA64r0Rg9r5EjEGEPjVbPaDC jO/NHiZbRnZzHdUePKpF+8GbtlOp08VU5/BRp94yHxhqJ1gQDWQ4J00pGeMlASmP577I OsQd14OT7bzauCVB3XYEDtuH5cBXywqd8/rybuYOwiad/QB5Tz0l4ag+bp8pfyU2zNdw nGa0PpK3h8cvww40RAJOaczanoqDbxD0PMpA8XMYztUksHsEl1ZIT6iEajMUSX4R/+aN FeRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b="Hgc/9Et8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id kr5-20020a056a004b4500b006ce115d5514si7377569pfb.324.2023.12.12.00.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 00:38:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b="Hgc/9Et8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 0DB558067A87; Tue, 12 Dec 2023 00:38:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229809AbjLLIif (ORCPT + 99 others); Tue, 12 Dec 2023 03:38:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229783AbjLLIie (ORCPT ); Tue, 12 Dec 2023 03:38:34 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5030ADB; Tue, 12 Dec 2023 00:38:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1702370320; x=1733906320; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=OlQSdutGnnJ/XqxX1PeaHL7qn3fxyzFw6HN2ZKxiL8Q=; b=Hgc/9Et8Trg1izaDThwh10GX2op8r7jhPMZNIhxLgw77tCYXmV3o/ZgA TItgjLQkBNWs5iyHuUabO58p+Q6GWqg8vGVBoQZt9bOP8DDyIeknWl/9m GomSZrFzzuALQGI25XZHLw8RINMKx1KGwD9j+xZycf7wwU+/2Ckglegyn 8nla2vhNCcAi+PPY5Mutb9EzyR+LgEx++dprTLQ3dT2ggL25Cvt1YGqww 5R5pZlqPplqFWFk8iGUui4wFMB4ZxTT5vM3BoVibvHLyPJPr8qjHyjNes KGoX0U7Oa6DSgZr9Mzy68pDFa6mWp/RedWg4JHM/lCdxUeJxDms4h76os A==; X-CSE-ConnectionGUID: wGJ38t4bSIaNu9ouYmhVbA== X-CSE-MsgGUID: Qgbb/YblTWedaUAGm9uKwA== X-ThreatScanner-Verdict: Negative X-IronPort-AV: E=Sophos;i="6.04,269,1695711600"; d="asc'?scan'208";a="13518116" X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa2.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 12 Dec 2023 01:38:39 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 12 Dec 2023 01:38:14 -0700 Received: from wendy (10.10.85.11) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Tue, 12 Dec 2023 01:38:10 -0700 Date: Tue, 12 Dec 2023 08:37:39 +0000 From: Conor Dooley To: Chen Wang CC: Conor Dooley , Chen Wang , , , , , , , , , , , , , , , , , , , Subject: Re: [PATCH v6 3/4] clk: sophgo: Add SG2042 clock generator driver Message-ID: <20231212-unnerving-rule-1052a5b7253e@wendy> References: <20231208-opposite-stand-fc92fbaaed9c@spud> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="wQrnDV9zD8q41CAE" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Dec 2023 00:38:50 -0800 (PST) --wQrnDV9zD8q41CAE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Dec 12, 2023 at 10:22:28AM +0800, Chen Wang wrote: > On 2023/12/9 0:47, Conor Dooley wrote: > > On Fri, Dec 08, 2023 at 09:14:32AM +0800, Chen Wang wrote: > > > +#define ENCODE_PLL_CTRL(fbdiv, p1, p2, refdiv) \ > > > + (((fbdiv & 0xfff) << 16) | ((p2 & 0x7) << 12) | ((p1 & 0x7) << 8) |= (refdiv & 0x3f)) > > IMO this should be a function not a macro. > Would like to listen why it should be a function instead of a macro? Any > experiences you can share with me? Readability. A function, which could be inlined allows you to break this up and make it easier to read. > > > +/* > > > + * Based on input rate/prate/fbdiv/refdiv, look up the postdiv1_2 ta= ble > > > + * to get the closest postdiiv combination. > > > + * @rate: FOUTPOSTDIV > > > + * @prate: parent rate, i.e. FREF > > > + * @fbdiv: FBDIV > > > + * @refdiv: REFDIV > > > + * @postdiv1: POSTDIV1, output > > > + * @postdiv2: POSTDIV2, output > > > + * See TRM: > > > + * FOUTPOSTDIV =3D FREF * FBDIV / REFDIV / (POSTDIV1 * POSTDIV2) > > > + * So we get following formula to get POSTDIV1 and POSTDIV2: > > > + * POSTDIV =3D (prate/REFDIV) x FBDIV/rate > > > + * above POSTDIV =3D POSTDIV1*POSTDIV2 > > > + */ > > > +static int __sg2042_pll_get_postdiv_1_2( > > > + unsigned long rate, > > > + unsigned long prate, > > > + unsigned int fbdiv, > > > + unsigned int refdiv, > > > + unsigned int *postdiv1, > > > + unsigned int *postdiv2) > > This is not the coding style btw. > Agree, will fix this. > > > +{ > > > + int index =3D 0; > > > + int ret =3D 0; > > > + u64 tmp0; > > > + > > > + /* prate/REFDIV and result save to tmp0 */ > > > + tmp0 =3D prate; > > > + do_div(tmp0, refdiv); > > > + > > > + /* ((prate/REFDIV) x FBDIV) and result save to tmp0 */ > > > + tmp0 *=3D fbdiv; > > > + > > > + /* ((prate/REFDIV) x FBDIV)/rate and result save to tmp0 */ > > > + do_div(tmp0, rate); > > > + > > > + /* tmp0 is POSTDIV1*POSTDIV2, now we calculate div1 and div2 value = */ > > > + if (tmp0 <=3D 7) { > > > + /* (div1 * div2) <=3D 7, no need to use array search */ > > > + *postdiv1 =3D tmp0; > > > + *postdiv2 =3D 1; > > > + } else { > > > + /* (div1 * div2) > 7, use array search */ > > > + for (index =3D 0; index < ARRAY_SIZE(postdiv1_2); index++) { > > > + if (tmp0 > postdiv1_2[index][POSTDIV_RESULT_INDEX]) { > > > + continue; > > > + } else { > > > + /* found it */ > > > + break; > > > + } > > > + } > > > + if (index < ARRAY_SIZE(postdiv1_2)) { > > > + *postdiv1 =3D postdiv1_2[index][1]; > > > + *postdiv2 =3D postdiv1_2[index][0]; > > > + } else { > > > + pr_debug("%s can not find in postdiv array!\n", __func__); > > > + ret =3D -EINVAL; > > > + } > > > + } > > > + > > > + return ret; > > > +} > > Reading this function it makes me wonder if (and I am far from the best > > person to comment, someone like Stephen is vastly more qualified) you > > should model this as several "stages", each implemented by the > > "standard" clocks - like clk_divider etc. The code here is quite > > complicated IMO as it seems to be trying to implement several stages of > > division in one go. >=20 > The objective of __sg2042_pll_get_postdiv_1_2() is straightforward: based= on > the formula defined by the TRM, with input rate/prate/fbdiv/refdiv, we can > get the possiblle combination of POSTDIV1 and POSTDIV2 by looking up the > table of postdiv1_2. We will later use it to setup the clock register. >=20 > Though the codes looks a bit complicated, but accually it is calculate wi= th > the formula : POSTDIV =3D (prate/REFDIV) x FBDIV/rate, I just separate it= into > several steps to make it easy to understand, I have listed the formula in > the comment on top of the function. I understand what you are doing, I did something similar myself previously. My suggestion/question was about using the "standard" types of clock that the core provides to represent as many of the clocks in this driver as is feasible. > > There's quite a lot in the driver and I will admit that I have not read > > it all my any means (I skimmed from here onwards), but in general my > > advice would be to try and reuse the generic code as much as possible. > Agree, I will double check and try to optimize the code in next revision. --wQrnDV9zD8q41CAE Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQRh246EGq/8RLhDjO14tDGHoIJi0gUCZXgbxQAKCRB4tDGHoIJi 0nOCAQD2K8NI7O2nSzjOsquexbQTw8D7tl5+jg2IZJ/dyjMnmQEAi/IZQy64yp5f zo77ZimRDysmDn+1uyqEqxszNT5r/g4= =9lTh -----END PGP SIGNATURE----- --wQrnDV9zD8q41CAE--