From patchwork Sun Jun 11 09:01:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Oltmanns X-Patchwork-Id: 106065 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1950084vqr; Sun, 11 Jun 2023 02:51:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Aon8kSJXtLZahEnLrWzFgQLEJ8sxeSjqpw+PFRRzggc5tLHN1xWYzbiRDbBcSaLjnvsFY X-Received: by 2002:a05:6a20:a108:b0:10b:8221:3348 with SMTP id q8-20020a056a20a10800b0010b82213348mr6606236pzk.50.1686477088827; Sun, 11 Jun 2023 02:51:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686477088; cv=none; d=google.com; s=arc-20160816; b=s8ctHOkKyYuvhVig1KJORIdYKf2s3GL5ypZKygerxyfBa+4JDsaJQB4wpih9TlMy+t i0tKggbiQ5ntV5lHLFw5H3X4RI6/tmAiucSraeCicxb7OGFtPcQ87ZbZqM7X7TS/A3Jn Y+rGq9S5ZYzaqnygoYA4BNv8l6bHTKbitkTldzsHoE8pMGuDnmcYc/fpcYg0hCLKpaxU E/OV8tXXwW1ktWNWsVWt0uud1oIQatHFYZ1YBChEjV+dnPgxR3Q8fMay8rfvvN4NlM8e zP34oXJjpAKmpT/dWl0GRpaJrAVTlR1cLDXhlST/MYOIKi7Y9jUdG7bcTurZT0yQED8h JDSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uUQkMyU05f1o8TsGE6GyWXIQbMt2IGEtftkSbTpsBoU=; b=JIPB6FBiHaB1RFWcQJ2i4uYb3R2kjz5NELXm8CWVu3bvQ+9quI5dndKDJ2gd4VK7AM VdJf/ph4K4NQZrS8uck5Vwx/N6woRarAGCyVeDlZx2gmLGJ9upUPi6EK7zHRz/xXPggy gCa7XQ8773l+o/shtAcbvIxemZghBoPzo/pWrveFW6OgZ/bBz3iP+mK/QhRGRCGgHQWX 83g1WaBH3UxflMO/fBblAyRs7ScHG3nPOShWZiOgnq0qxiIArvq4JGH2Ssatfo2lkg1V 34SMFD1aNIUEOwdd2o+DYL+3cP/E44ZVpE7t3L+Er7u+EANJFCZTFU/3a5Z252WVjJu6 355w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=VOSqdeAw; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e186-20020a6369c3000000b005429411d104si5455300pgc.897.2023.06.11.02.51.16; Sun, 11 Jun 2023 02:51:28 -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=VOSqdeAw; 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 S233467AbjFKJCJ (ORCPT + 99 others); Sun, 11 Jun 2023 05:02:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233445AbjFKJCF (ORCPT ); Sun, 11 Jun 2023 05:02:05 -0400 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82AC4184; Sun, 11 Jun 2023 02:02:03 -0700 (PDT) Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (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-102.mailbox.org (Postfix) with ESMTPS id 4Qf82b70y9z9sWm; Sun, 11 Jun 2023 11:01:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oltmanns.dev; s=MBO0001; t=1686474120; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uUQkMyU05f1o8TsGE6GyWXIQbMt2IGEtftkSbTpsBoU=; b=VOSqdeAw3YXVBI7ydhqrMTOvwqQp9qU9pDAxyjfzV89AcD/juV+PRdNqLXT0vV5EEIPliB 3Tu5t1GyUtw/ZDX2FTgDL0bLxXfSO+DjtLMNyxqXpfIM3jpVHhv9L96pYvy++01Jh+EpeZ LAy5zzQmYANOZDXucyUKQ6TQkJ5jpETU54nSwLoC33pgperm5+TStBG/0+xsSOi9K6jizL mJFA6BBTNLdU19yc69skxhvZz4WRTj1dnpRzTSMQfQxAJG98OnaNpRv00vYZh2eEIY+sE1 CzL/ERMrljTtm1r1q4nnXZ5rkMFTdJYM64aZRq3vDArQi5VfcUN8bQ7Ofk9Asw== From: Frank Oltmanns To: Andre Przywara , Chen-Yu Tsai , Frank Oltmanns , Jernej Skrabec , Maxime Ripard , Michael Turquette , Roman Beranek , Samuel Holland , Stephen Boyd Cc: linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sunxi@lists.linux.dev Subject: [PATCH v2 1/2] clk: sunxi-ng: nkm: consider alternative parent rates when finding rate Date: Sun, 11 Jun 2023 11:01:42 +0200 Message-ID: <20230611090143.132257-2-frank@oltmanns.dev> In-Reply-To: <20230611090143.132257-1-frank@oltmanns.dev> References: <20230611090143.132257-1-frank@oltmanns.dev> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4Qf82b70y9z9sWm 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768399400064186776?= X-GMAIL-MSGID: =?utf-8?q?1768399400064186776?= 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 | 66 +++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c index a0978a50edae..c49d5879fe73 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,10 +17,49 @@ struct _ccu_nkm { unsigned long m, min_m, max_m; }; -static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, - struct _ccu_nkm *nkm) +static unsigned long optimal_parent_rate(unsigned long rate, unsigned long n, + unsigned long k, unsigned long m) { - unsigned long best_rate = 0; + unsigned long _rate, parent; + + // We must first try to find the desired parent rate that is rounded up, because there are + // cases where truncating makes us miss the requested rate. + // E.g. rate=449035712, n=11, k=3, m=16 + // When truncating, we'd get parent=217714284 and calculating the rate from that would give + // us 449035710. When rounding up, we get parent=217714285 which would give us the requested + // rate of 449035712. + parent = DIV_ROUND_UP(rate * m, n * k); + + // But there are other cases, where rounding up the parent gives us a too high rate. + // Therefore, we need to check for this case and, if necessary, truncate the parent rate + // instead of rounding up. + _rate = parent * n * k / m; + if (_rate > rate) + parent = rate * m / (n * k); + return parent; +} + +/** + * ccu_nkm_find_best - Find the best nkm combination for a given rate + * + * @parent: rate of parent clock. This is used either as an input or out parameter: + * - In cases where the parent clock can be set, this parameter will be updated to contain + * the optimal rate for the parent to achieve the best rate for the nkm clock. + * - In cases where the parent clock can not be set, this parameter must contain the + * current rate of the parent, which is used to determine the best combination of n, k, + * and m. + * @rate: requested rate. + * @nkm: Input/output parameter that contains the clocks constraints on the n, k, m combinations and + * is updated in this function to contain the resulting best n, k, m combination. + * @parent_hw: parent clock. If set, this function assumes that the parent clock can be updated to a + * rate that would be best to in order to get as close as possible to @rate. This + * parameter must be set to NULL if this function shall not try to find the optimal + * parent rate for the requested rate. + */ +static unsigned long ccu_nkm_find_best(unsigned long *parent, unsigned long rate, + struct _ccu_nkm *nkm, struct clk_hw *parent_hw) +{ + 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; @@ -28,12 +68,17 @@ static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { unsigned long tmp_rate; - tmp_rate = parent * _n * _k / _m; - + if (parent_hw) { + tmp_parent = optimal_parent_rate(rate, _n, _k, _m); + tmp_parent = clk_hw_round_rate(parent_hw, tmp_parent); + } + 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; @@ -46,6 +91,8 @@ static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, nkm->k = best_k; nkm->m = best_m; + *parent = best_parent_rate; + return best_rate; } @@ -106,7 +153,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 +171,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, NULL); + else + rate = ccu_nkm_find_best(parent_rate, rate, &_nkm, parent_hw); if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) rate /= nkm->fixed_post_div; @@ -159,7 +209,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, NULL); spin_lock_irqsave(nkm->common.lock, flags); From patchwork Sun Jun 11 09:01:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Oltmanns X-Patchwork-Id: 106061 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1937503vqr; Sun, 11 Jun 2023 02:08:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5uDfoBmEmQHrxfiA1rPFIgHm9CFMdtlz1yklCSA0KW9iGKCBKSxNQz9wjQiGIrXBcb6/lP X-Received: by 2002:aa7:c38a:0:b0:510:e790:48fe with SMTP id k10-20020aa7c38a000000b00510e79048femr2547568edq.6.1686474481747; Sun, 11 Jun 2023 02:08:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686474481; cv=none; d=google.com; s=arc-20160816; b=nqG7ns6XrPSrUOpAIN/oOnDWEZsIP1fbFw8xKelEzmqw+AybliDwy8I87Quqqd+FGm rF063qlpZEkfJL6UVm7tSA+o8rdIOj6CGYie4iSobsalu/0HYmI7RO6/t899rd7JN53V 67VdjFJOSiXpBNy7Mfx4hBxqGgBUSliodGv/5gyjAvg38PnH/0uWdLyBU3LeR04LJ1lh ydX9V7xL3bknryDLr+9bMGr1FJCGy/2ex/zjZv12Rc+f9rhibMW75dQacvHG/rapTPgK HzktSUvgUL3+dRDn8fFHUYSvEoR+LtbEuPRx3GBSIXodY+LINi/RT1e9f7OXs/67NmNr cPtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6yxavVTkiMowdP7zQF1qUAVlWmfkvPV68RHvZhsPS3g=; b=PWcilcN2luhAQ8sLaP1NFkPGugXcLRiuzTFMPPn0H/vFyesXRy2fozAIVHcazRU1p6 QJGoXBa9S2M0nxhsW016Jqk41VSvMaxi5lud+2zRlRseWPy0xVg0KG+40dKk5W/1i2+j GMmUqQe+CxF3kwQ3Wpeq8XD3gdSWjTJVp8nmqAcJo/3svqxZ9t5mV18j6weTAFUmejT1 pPgaach0LjGS4KTc1M2jrrs9A6blHWGOadRuaOfyE58SYeZgEBaxVXeuE40UaFWF+I7N sqdqjz9kRDtZowvj9YijmSIBKHMoq+H22aLzCFzOshNAKSxOnd5dnLlTcnL2n3/3IQU4 C2dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=lQavwJ2w; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o19-20020aa7d3d3000000b0050bcc56b523si4288656edr.384.2023.06.11.02.07.38; Sun, 11 Jun 2023 02:08:01 -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=lQavwJ2w; 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 S233528AbjFKJCN (ORCPT + 99 others); Sun, 11 Jun 2023 05:02:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233484AbjFKJCK (ORCPT ); Sun, 11 Jun 2023 05:02:10 -0400 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [IPv6:2001:67c:2050:0:465::101]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1727136; Sun, 11 Jun 2023 02:02:06 -0700 (PDT) Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (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-101.mailbox.org (Postfix) with ESMTPS id 4Qf82g1C9jz9sb8; Sun, 11 Jun 2023 11:02:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oltmanns.dev; s=MBO0001; t=1686474123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6yxavVTkiMowdP7zQF1qUAVlWmfkvPV68RHvZhsPS3g=; b=lQavwJ2wTt1AaLWwl3wPuVfxFeYPMInwxrXJwQd39ugE/gaa+DhLCxh+r7yczbCKkcP3K0 cIZYpYzUDluSmD5eNwQAoDRpeb9QcV27//CfeMdkmRPvmY4xSc6ZeDimBXPuSO9IiVVilV OncUnzYoQ2dmmDRENiunTthW9DZNbmEoxO9Kn2rDIUopGPCAiHQU7ljlH3Suh5MaXACNJ3 upBsRq9rYVfjLwLUlDKNF22pGg/EYtJzzqqwrnacUNVhoPjEydVtz7WZtopgoKId/rREok p1vmz0YpPQ4+xYB6yh71sY/dDUmcc5WlRgdqGjJHTnK899GyoNU2fnfGV1PQiA== From: Frank Oltmanns To: Andre Przywara , Chen-Yu Tsai , Frank Oltmanns , Jernej Skrabec , Maxime Ripard , Michael Turquette , Roman Beranek , Samuel Holland , Stephen Boyd Cc: linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sunxi@lists.linux.dev Subject: [PATCH v2 2/2] clk: sunxi-ng: a64: allow pll-mipi to set parent's rate Date: Sun, 11 Jun 2023 11:01:43 +0200 Message-ID: <20230611090143.132257-3-frank@oltmanns.dev> In-Reply-To: <20230611090143.132257-1-frank@oltmanns.dev> References: <20230611090143.132257-1-frank@oltmanns.dev> MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768396666582389733?= X-GMAIL-MSGID: =?utf-8?q?1768396666582389733?= The nkm clock now supports setting the parent's rate. Utilize this option to find the optimal rate for pll-mipi. Signed-off-by: Frank Oltmanns --- drivers/clk/sunxi-ng/ccu-sun50i-a64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/clk/sunxi-ng/ccu-sun50i-a64.c b/drivers/clk/sunxi-ng/ccu-sun50i-a64.c index eb36f8f77d55..125ae097d96c 100644 --- a/drivers/clk/sunxi-ng/ccu-sun50i-a64.c +++ b/drivers/clk/sunxi-ng/ccu-sun50i-a64.c @@ -179,7 +179,8 @@ static struct ccu_nkm pll_mipi_clk = { .common = { .reg = 0x040, .hw.init = CLK_HW_INIT("pll-mipi", "pll-video0", - &ccu_nkm_ops, CLK_SET_RATE_UNGATE), + &ccu_nkm_ops, + CLK_SET_RATE_UNGATE | CLK_SET_RATE_PARENT), }, };