Received: by 2002:a05:7412:d024:b0:f9:90c9:de9f with SMTP id bd36csp282685rdb; Wed, 20 Dec 2023 12:05:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEK8FHV8YWA1f0cZOONN1r7YF5Pb3IYbtEzt0j6J4szx2zlgvMuLmBuoog5TFUdQ0ZqRZ9P X-Received: by 2002:a05:620a:38e:b0:780:ed85:2c08 with SMTP id q14-20020a05620a038e00b00780ed852c08mr5236126qkm.2.1703102750568; Wed, 20 Dec 2023 12:05:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703102750; cv=none; d=google.com; s=arc-20160816; b=eln1KtpdyhOrYH+Li9ocCY18+RquBGiqyKgJuMhLy9A44UpqnxoK3zy/kQstWw+G1E 4cWCGTEbTikecv/pmJinhe3cRtOstLG8BAOs3ArXip2zEKNqCkxnfkggB3O8BH2FpGxl sxWJMCN36EFtXRBmV2upyvy1L1JpOcbEblfWiY7D2jNgSmYaEkk0i0Vv6avRBOP1EYO2 wcBhDgLh5HN2hlCZK+RZV7MY9GS+UToeDf8pZ2KFKoWBfUm7E0hnnVaUGPDOtSFIxx+P 47Zo85JhoCs0BtdUvzffaEzvDmRUhDy9e9R6sGXqz1YVbfAbPnUBJhnSV0htztDizdcL tkiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:date:to:cc:from:subject:references:in-reply-to :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:dkim-signature; bh=o1HVrNLsHWvpnod9z/6rEnP8cjNVe7+jGltF+VAYnZE=; fh=wN15VK9BAQnS/b5l++cvaNWFusa3kl98s5uEMK+By2U=; b=KGjfnV2vzkrhjwQOlvvy7C4JF1RgrZaUUvN1dmvOJcrWs72vO82WiZeve5iRntPf1L /L8GSnjq+SaqgRa1Y79QdEfMxrP11rMLCeHOS1s7lszWgXcr7dAXAhP/z8/6CyfFZ6aF NSTrTRUr7dV/D/qSgNSwH+9uviHS45bakGinKWqyGC/l1iHB/4bOoxymS0vsrDUsYQDp 4Uxeop0VkHW5zbUH2RcVDMwonyRFci6zNjIOgzRcp/+OVaJiM0KK2Wq05mgZ+S1Vfezv DF/wRq5fcMy6d4zYPSNR0/2tclv1Oi0ZwK7j6gaGCUqYEgeYTsnAdnUtF7g2nqHjEnJP 73wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GYCEt0zb; spf=pass (google.com: domain of linux-kernel+bounces-7407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7407-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u3-20020a05620a0c4300b00780dd26ad19si543943qki.96.2023.12.20.12.05.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 12:05:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GYCEt0zb; spf=pass (google.com: domain of linux-kernel+bounces-7407-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7407-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 552E81C2302A for ; Wed, 20 Dec 2023 20:05:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05D3C487B0; Wed, 20 Dec 2023 20:05:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GYCEt0zb" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2A6F947771; Wed, 20 Dec 2023 20:05:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77BD5C433C9; Wed, 20 Dec 2023 20:05:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1703102741; bh=4Na6zHOF3IuiCd92bkpwf7VYHr722ENTcJQapD0mvbI=; h=In-Reply-To:References:Subject:From:Cc:To:Date:From; b=GYCEt0zbeTjsCwTyUEClYJa+WEHAfzX5NHOgLwDmBrNkFf4NoxSX5rDb0ErBRwuYD B+kwSxTjp4m7890M4CfzAaAuYDGbvUZygPGFtxe/s/v9iCUvHB6t9eQtXcqXP9EJVg o68t5tufAT2VaRce+2PL3spq5BIGez4rNf8qdcskBLLtUT8/XqkAI1a9coMQd5R3vX mX+t6ChBiK6dLC5A9o09V9giirFqH0DNHmF3m1fu72qBIpFqyMwp5lWE48QUCBEtEY ju+YaJ1H7jLRtuFTAVFgV48WabEeYq1gMa5JLsaWn8bUAaHgjqysCDKF+yfHr3CoJY fJyhzkoiUOXSw== Message-ID: Content-Type: text/plain; charset="utf-8" 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 In-Reply-To: <20231124151847.1915-3-ansuelsmth@gmail.com> References: <20231124151847.1915-1-ansuelsmth@gmail.com> <20231124151847.1915-3-ansuelsmth@gmail.com> Subject: Re: [PATCH v7 2/3] clk: qcom: clk-rcg2: add support for rcg2 freq multi ops From: Stephen Boyd Cc: Christian Marangi To: Andy Gross , Bjorn Andersson , Christian Marangi , Konrad Dybcio , Michael Turquette , linux-arm-msm@vger.kernel.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 20 Dec 2023 12:05:39 -0800 User-Agent: alot/0.10 Quoting Christian Marangi (2023-11-24 07:18:46) > Some RCG frequency can be reached by multiple configuration. >=20 > Add clk_rcg2_fm_ops ops to support these special RCG configurations. >=20 > These alternative ops will select the frequency using a CEIL policy. >=20 > When the correct frequency is found, the correct config is selected by > calculating the final rate (by checking the defined parent and values > in the config that is being checked) and deciding based on the one that > is less different than the requested one. >=20 > These check are skipped if there is just on config for the requested s/on/one/ > freq. >=20 > qcom_find_freq_multi is added to search the freq with the new struct > freq_multi_tbl. > __clk_rcg2_select_conf is used to select the correct conf by simulating > the final clock. > If a conf can't be found due to parent not reachable, a WARN is printed > and -EINVAL is returned. >=20 > Signed-off-by: Christian Marangi [...] > diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c > index e22baf3a7112..2393c7df0402 100644 > --- a/drivers/clk/qcom/clk-rcg2.c > +++ b/drivers/clk/qcom/clk-rcg2.c > @@ -266,6 +266,115 @@ static int _freq_tbl_determine_rate(struct clk_hw *= hw, const struct freq_tbl *f, > return 0; > } > =20 > +static const struct freq_conf * > +__clk_rcg2_select_conf(struct clk_hw *hw, const struct freq_multi_tbl *f, > + unsigned long req_rate) > +{ > + unsigned long rate_diff, best_rate_diff =3D ULONG_MAX; > + const struct freq_conf *conf, *best_conf =3D NULL; > + struct clk_rcg2 *rcg =3D to_clk_rcg2(hw); > + const char *name =3D clk_hw_get_name(hw); > + unsigned long parent_rate, rate; > + struct clk_hw *p; > + int index, i; > + > + /* Exit early if only one config is defined */ > + if (f->num_confs =3D=3D 1) { > + best_conf =3D f->confs; > + goto exit; > + } > + > + /* Search in each provided config the one that is near the wanted= rate */ > + for (i =3D 0, conf =3D f->confs; i < f->num_confs; i++, conf++) { > + index =3D qcom_find_src_index(hw, rcg->parent_map, conf->= src); > + if (index < 0) > + continue; > + > + p =3D clk_hw_get_parent_by_index(hw, index); > + if (!p) > + continue; > + > + parent_rate =3D clk_hw_get_rate(p); > + rate =3D calc_rate(parent_rate, conf->n, conf->m, conf->n= , conf->pre_div); > + > + if (rate =3D=3D req_rate) { > + best_conf =3D conf; > + goto exit; > + } > + > + rate_diff =3D abs(req_rate - rate); Use abs_diff() please. > + if (rate_diff < best_rate_diff) { > + best_rate_diff =3D rate_diff; > + best_conf =3D conf; > + } > + } > + > + /* > + * Very unlikely. Warn if we couldn't find a correct config > + * due to parent not found in every config. > + */ > + if (unlikely(!best_conf)) { > + WARN(1, "%s: can't find a configuration for rate %lu.", Needs a newline in the format string and should drop the period. > + name, req_rate); > + return ERR_PTR(-EINVAL); > + } > + > +exit: > + return best_conf; > +} > + [...] > + > static int clk_rcg2_determine_rate(struct clk_hw *hw, > struct clk_rate_request *req) > { > @@ -377,6 +494,30 @@ static int __clk_rcg2_set_rate(struct clk_hw *hw, un= signed long rate, > return clk_rcg2_configure(rcg, f); > } > =20 > +static int __clk_rcg2_fm_set_rate(struct clk_hw *hw, unsigned long rate) > +{ > + struct clk_rcg2 *rcg =3D to_clk_rcg2(hw); > + const struct freq_multi_tbl *f; > + const struct freq_conf *conf; > + struct freq_tbl f_tbl; f_tbl =3D { } In case we want to add more fields so this isn't a time bomb. > + > + f =3D qcom_find_freq_multi(rcg->freq_multi_tbl, rate); > + if (!f || !f->confs) > + return -EINVAL; > + > + conf =3D __clk_rcg2_select_conf(hw, f, rate); > + if (IS_ERR(conf)) > + return PTR_ERR(conf); > + > + f_tbl.freq =3D f->freq; > + f_tbl.src =3D conf->src; > + f_tbl.pre_div =3D conf->pre_div; > + f_tbl.m =3D conf->m; > + f_tbl.n =3D conf->n; > + > + return clk_rcg2_configure(rcg, &f_tbl); > +} > + > static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate, > unsigned long parent_rate) > {