From patchwork Tue Jan 19 14:51:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 8063211 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 10D329F744 for ; Tue, 19 Jan 2016 14:55:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2490B2038A for ; Tue, 19 Jan 2016 14:55:33 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4471320434 for ; Tue, 19 Jan 2016 14:55:32 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aLXfM-00086M-OR; Tue, 19 Jan 2016 14:53:52 +0000 Received: from 212-186-180-163.dynamic.surfer.at ([212.186.180.163] helo=cgate.sperl.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aLXe9-0007IH-HP; Tue, 19 Jan 2016 14:52:44 +0000 Received: from raspcm.intern.sperl.org (account martin@sperl.org [10.10.10.41] verified) by sperl.org (CommuniGate Pro SMTP 6.1.2) with ESMTPSA id 6392112; Tue, 19 Jan 2016 14:51:49 +0000 From: kernel@martin.sperl.org To: Rob Herring , Stephen Warren , Lee Jones , Eric Anholt , Michael Turquette , Stephen Boyd , Remi Pommarel , devicetree@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org Subject: [RFC 4/9] clk: bcm2835: reorganize bcm2835_clock_determine_rate Date: Tue, 19 Jan 2016 14:51:35 +0000 Message-Id: <1453215100-2382-5-git-send-email-kernel@martin.sperl.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1453215100-2382-1-git-send-email-kernel@martin.sperl.org> References: <1453215100-2382-1-git-send-email-kernel@martin.sperl.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160119_065238_467158_7E42B1E5 X-CRM114-Status: GOOD ( 11.96 ) X-Spam-Score: -0.9 (/) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Martin Sperl MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl Reorganize bcm2835_clock_determine_rate so that the per clock frequency calculation is separated from the selection logic. This allows for different clock selection policies to be use while keeping code common. Signed-off-by: Martin Sperl --- drivers/clk/bcm/clk-bcm2835.c | 98 +++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/drivers/clk/bcm/clk-bcm2835.c b/drivers/clk/bcm/clk-bcm2835.c index 48c1ad0..f1ab525 100644 --- a/drivers/clk/bcm/clk-bcm2835.c +++ b/drivers/clk/bcm/clk-bcm2835.c @@ -1668,44 +1668,86 @@ unlock_exit: return 0; } -static int bcm2835_clock_determine_rate(struct clk_hw *hw, - struct clk_rate_request *req) +struct bcm2835_rates { + struct clk_hw *parent; + unsigned long rate; + unsigned long prate; + u32 div; + divmash dmash; +}; + +static int bcm2835_clock_determine_rate_set(struct clk_rate_request *req, + struct bcm2835_rates *best) { - struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); - struct clk_hw *parent, *best_parent = NULL; - unsigned long rate, best_rate = 0; - unsigned long prate, best_prate = 0; + req->best_parent_hw = best->parent; + req->best_parent_rate = best->prate; + req->rate = best->rate; + + return 0; +} + +static int bcm2835_clock_determine_closest_rate(struct clk_hw *hw, + struct clk_rate_request *req, + struct bcm2835_rates *rates, + size_t rate_cnt) +{ + struct bcm2835_rates *best = NULL; size_t i; - divmash dm; - u32 div; - /* - * Select parent clock that results in the closest but lower rate - */ - for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { - parent = clk_hw_get_parent_by_index(hw, i); - if (!parent) + /* find best matching */ + for (i = 0; i < rate_cnt; i++) { + if (rates[i].rate > req->rate) + continue; + if (!best) { + best = &rates[i]; + continue; + } + /* find the closest */ + if (rates[i].rate > best->rate) { + best = &rates[i]; + continue; + } + /* if identical then use highest divider */ + if ((rates[i].rate == best->rate) && + (rates[i].div > best->div)) { + best = &rates[i]; continue; - prate = clk_hw_get_rate(parent); - dm = bcm2835_clock_choose_div(hw, req->rate, prate, true); - div = divmash_get_div(dm); - rate = bcm2835_clock_rate_from_divisor(clock, prate, div); - if (rate > best_rate && rate <= req->rate) { - best_parent = parent; - best_prate = prate; - best_rate = rate; } } - if (!best_parent) - return -EINVAL; + if (best) + return bcm2835_clock_determine_rate_set(req, best); + + /* nothing found */ + return -EINVAL; +} - req->best_parent_hw = best_parent; - req->best_parent_rate = best_prate; +static int bcm2835_clock_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); + struct bcm2835_rates rates[BIT(CM_SRC_BITS)]; + size_t i, rate_cnt = 0; + divmash dm; - req->rate = best_rate; + /* fill in rates */ + for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { + rates[rate_cnt].parent = clk_hw_get_parent_by_index(hw, i); + if (!rates[rate_cnt].parent) + continue; + rates[rate_cnt].prate = clk_hw_get_rate( + rates[rate_cnt].parent); + dm = bcm2835_clock_choose_div( + hw, req->rate, rates[rate_cnt].prate, true); + rates[rate_cnt].div = divmash_get_div(dm); + rates[rate_cnt].rate = bcm2835_clock_rate_from_divisor( + clock, rates[rate_cnt].prate, rates[rate_cnt].div); + rate_cnt++; + } - return 0; + /* choose the "closest" one */ + return bcm2835_clock_determine_closest_rate(hw, req, rates, + rate_cnt); } static int _bcm2835_clk_set_parent(struct bcm2835_cprman *cprman,