Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp25153940rwd; Sun, 2 Jul 2023 11:21:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlHJMCkg1P8UYmUg5srnxxXr24jn+V2cwuvr7BUXo3UAXRXniIhN+exPHYhIdoiPeGtBw18f X-Received: by 2002:a17:903:244e:b0:1b8:6658:480 with SMTP id l14-20020a170903244e00b001b866580480mr5060681pls.67.1688322062952; Sun, 02 Jul 2023 11:21:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688322062; cv=none; d=google.com; s=arc-20160816; b=mj4SJEl9at47r2DyfmnPyTEB2HtOsp81Bdsc/VtrFZTmyRAxdDhaXHxISlF4A/qa4j GAGjoli3QkjfkCh3BR4F0b4VNNuRgUZgM3LDkTZNith2h1NXKYshi0taruZx2FvwaRZ0 3myIJWIrgh5B8jvVZ08gbR8oSITB+Bfj0lh8rsqX1Wia3WdNLbUSb7sojwmnf+RSr9wh oGKaUTlkPdLWvTRST9p63i9dhIJONIwN6DU2nSUaKWDoCvDgNIvZZ+CPdu1omwb2Ve/g XaWd8USibx9X+zgBqp8FZ8LMFoaKOeYFBkJX+jsAsKgCXM2L/q8eCRFin+U8B68y/vHH LKiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=x9fmM0fT8uj3/mQjTtgGNj5UoFoMoqnDLGSIr5/oXTs=; fh=qochKExQAM0bhcnc+r+ZD43ZS//6TaZetJnkaAgsyOw=; b=WGk1o0TGV2pF1uVSYahXwprJkxIGSlIevZs/kiOVqQk7GM6EASWmZt5nroOVp4dTxo 7Jklt/ZFR8/tzFGs0O6/Zg1XKMDQcHzrKqtexRsikbDW8z1FfCTnc4NPGZEzIJVksz22 mFrmAwga1iPStSqOMcdspg8HGQNV416M+UJHJ7B+FdwsprRz2HdqFzPfwI0UY2v5uJXP a11Ofc0dJ0bDfW8E5ZmEsjO9vOPhWarlA462YJ4zH7CXfOFy5d1QfONVsgfpnwxZfkiB tnpBFQaxtgzCRcwzJKSMeKY4Y0rJXzIjXJutiLmvcUhE90qEBMokAVlZ9uqNl9iFqNjs lQOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=SdiVR4Ma; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oltmanns.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jj13-20020a170903048d00b001b873b80747si4851440plb.39.2023.07.02.11.20.44; Sun, 02 Jul 2023 11:21:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=SdiVR4Ma; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oltmanns.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229954AbjGBRz7 (ORCPT + 99 others); Sun, 2 Jul 2023 13:55:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbjGBRz4 (ORCPT ); Sun, 2 Jul 2023 13:55:56 -0400 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [IPv6:2001:67c:2050:0:465::201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5008DE69; Sun, 2 Jul 2023 10:55:49 -0700 (PDT) Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4QvGtp5jn5z9sWl; Sun, 2 Jul 2023 19:55:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oltmanns.dev; s=MBO0001; t=1688320546; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x9fmM0fT8uj3/mQjTtgGNj5UoFoMoqnDLGSIr5/oXTs=; b=SdiVR4MaEnrn4e0/1imGtdk3j96t4YqmDLtnEiKreTm9t9nW9wTtLAWgB4thPn4V4JasHy 9Fl+GZdP/qJ569jahiW8JknXFSmuShk1mepjZk1c0Ll7t9lPAteyRE6+z3lRi+SyV1BBvX QJgwvV2EPK7nKW4Q83/zEJ+8a67eWbr1wjAKojZJIe336Oh3CM26whugevkZ01NUGmKIn5 3BQt70mxv2bBDP3kav/PMzxptjGmCz5T3yeMUR3iZANJ6I93kbiz1MRPcOnYLCv+yno409 ddVtXdPlbBuz2pRBhVJwrAqqMhSX5CshXfS8uPe1MCkJ2Z5bnbP+svkIj2B08w== From: Frank Oltmanns Date: Sun, 02 Jul 2023 19:55:20 +0200 Subject: [PATCH v3 1/8] clk: sunxi-ng: nkm: consider alternative parent rates when determining rate MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230702-pll-mipi_set_rate_parent-v3-1-46dcb8aa9cbc@oltmanns.dev> References: <20230702-pll-mipi_set_rate_parent-v3-0-46dcb8aa9cbc@oltmanns.dev> In-Reply-To: <20230702-pll-mipi_set_rate_parent-v3-0-46dcb8aa9cbc@oltmanns.dev> To: Maxime Ripard , Michael Turquette , Stephen Boyd , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Andre Przywara , Roman Beranek Cc: linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Frank Oltmanns X-Developer-Signature: v=1; a=openpgp-sha256; l=3396; i=frank@oltmanns.dev; h=from:subject:message-id; bh=HVQ5eb3Px+mMKJ3qzsFpElQcZwMVX6kra1Fj/PYKI+o=; b=owEB7QES/pANAwAIAZppogiUStPHAcsmYgBkobobH6sAdwPDpCnBt7BVKXWjmOzHtCN5tQzCd jGIoubVUyqJAbMEAAEIAB0WIQQC/SV7f5DmuaVET5aaaaIIlErTxwUCZKG6GwAKCRCaaaIIlErT x1eYDACPfjAKC+7Gsc70ClrDjNtQWbTy/MzHDJHYfwaPbG56Q4xfN9pNwZE9amuRcJPke0LF6Lc jANjHUPkOOSGHWDIda+BopPu28R48OdFZNuUdLtL3fyzA0AgodLkcKZkVUzz08uMxJQIEGxb05q fD0/8WnY0GgkxNhIVDByiE8mZCAt/syOzNvYMdERMJ/Q/XEPTcQGs9BBa2Vibo0C/RJ/R76kEXh w4y54LeEst5NL/AKNp5c0GgS6jEpYsFJMMT1DQcLiigWTP84++MuPuKH5Pzyi18d9TlCtfLIVou UXR5fJ/EAcGqleurg+PUaLQJARw8k9aAX1uqgE2jClgy5V7tdymilCOpmXSo0SmsUO1V7e0SWyM E5bLdYwWOkseHRnelpLGbzBXUhlIsbbV/uSfgg3v9fyTkx7QBl4lrg1s49sjqUJ0C2LuarpFuwM KpWmBrC+LgPBHklEAQZ/edq0sfeQFMsNOlCqRY8Wx1om8/AFe0cKQZuYooQg7BN/fHd7Y= X-Developer-Key: i=frank@oltmanns.dev; a=openpgp; fpr=02FD257B7F90E6B9A5444F969A69A208944AD3C7 X-Rspamd-Queue-Id: 4QvGtp5jn5z9sWl X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In case the CLK_SET_RATE_PARENT flag is set, consider using a different parent rate when determining a new rate. To find the best match for the requested rate, perform the following steps for each NKM combination: - calculate the optimal parent rate, - find the best parent rate that the parent clock actually supports - use that parent rate to calculate the effective rate. In case the clk does not support setting the parent rate, use the same algorithm as before. Signed-off-by: Frank Oltmanns --- drivers/clk/sunxi-ng/ccu_nkm.c | 48 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c index a0978a50edae..d83843e69c25 100644 --- a/drivers/clk/sunxi-ng/ccu_nkm.c +++ b/drivers/clk/sunxi-ng/ccu_nkm.c @@ -6,6 +6,7 @@ #include #include +#include #include "ccu_gate.h" #include "ccu_nkm.h" @@ -16,6 +17,44 @@ struct _ccu_nkm { unsigned long m, min_m, max_m; }; +static unsigned long ccu_nkm_find_best_with_parent_adj(unsigned long *parent, unsigned long rate, + struct _ccu_nkm *nkm, struct clk_hw *phw) +{ + unsigned long best_rate = 0, best_parent_rate = *parent, tmp_parent = *parent; + unsigned long best_n = 0, best_k = 0, best_m = 0; + unsigned long _n, _k, _m; + + for (_k = nkm->min_k; _k <= nkm->max_k; _k++) { + for (_n = nkm->min_n; _n <= nkm->max_n; _n++) { + for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { + unsigned long tmp_rate; + + tmp_parent = clk_hw_round_rate(phw, rate * _m / (_n * _k)); + + tmp_rate = tmp_parent * _n * _k / _m; + if (tmp_rate > rate) + continue; + + if ((rate - tmp_rate) < (rate - best_rate)) { + best_rate = tmp_rate; + best_parent_rate = tmp_parent; + best_n = _n; + best_k = _k; + best_m = _m; + } + } + } + } + + nkm->n = best_n; + nkm->k = best_k; + nkm->m = best_m; + + *parent = best_parent_rate; + + return best_rate; +} + static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, struct _ccu_nkm *nkm) { @@ -106,7 +145,7 @@ static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw, } static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, - struct clk_hw *hw, + struct clk_hw *parent_hw, unsigned long *parent_rate, unsigned long rate, void *data) @@ -124,7 +163,10 @@ static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) rate *= nkm->fixed_post_div; - rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm); + if (!clk_hw_can_set_rate_parent(&nkm->common.hw)) + rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm); + else + rate = ccu_nkm_find_best_with_parent_adj(parent_rate, rate, &_nkm, parent_hw); if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) rate /= nkm->fixed_post_div; @@ -159,7 +201,7 @@ static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate, _nkm.min_m = 1; _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; - ccu_nkm_find_best(parent_rate, rate, &_nkm); + ccu_nkm_find_best(&parent_rate, rate, &_nkm); spin_lock_irqsave(nkm->common.lock, flags); -- 2.41.0