From patchwork Wed Feb 14 13:43:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 128343 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp622149ljc; Wed, 14 Feb 2018 05:43:57 -0800 (PST) X-Google-Smtp-Source: AH8x226JTaPVS72Dk0WccUQm+YrNkC0Xjzuuuyuoiz7QZzU49jeZviDbX/y8P3naZnkfNcP3azJV X-Received: by 10.101.86.1 with SMTP id l1mr3183698pgs.140.1518615836975; Wed, 14 Feb 2018 05:43:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518615836; cv=none; d=google.com; s=arc-20160816; b=UTScZxFbp6xG/NnK944yK02u1mhw9PNGmASz1YNXGVf1etJD/zVmyqPI5CqME/FPRJ UkxeMc1DLQbdSjRYVOs6GR4aCxn0AepJnJYnt1GF/gmyAonxINL4TthtsyJ9cQ65MElT 7mnFPsARYKgEXcInyDtovVC3uTBnr9lLTqcWo/I7DzSjAwGoxh0lVOtIJgofFLNoiJms WO0BAnM6re0/uhQdidnvZE6GE5pg9S0jSGFWzLhO9dYShDt1T231ELTOrEgMfXFnutdL n3iYDDcJiSP2ieqocipNLX8nQOXfbySbP2iSOnkQhZ+yAsIBrbVyRb6250p4Mc5aR0Ix DkTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=khIZOpGmG2HxeeBspaK1a22MkjUU3vUn32g7U5HJaSw=; b=bu26hGQXTQNVPueGqLTudmjCq0NhWvCKPQrYZ3TCefuvUi8JHXB8jfGUsMkZJa9d9i T1fs3OvBxCPSpuYmVMk19WQPaoBsu9X0MwA+hd/mhzvthSZ5u3Nt5/Y58wZNIipKiLRI uAhLHKnTbrB/HW7Hhaagn0gPs8roYec58bhO6B+YziA5F3wspyv3bSdhFNvU26GPO4kF v/srXoYJc85Cc9/oQiWW6zjYSynt4hM+qFlr5NZZrhA+sNTxuVrRAsYsN4ARAWVe1NDz VLIyxmRnksoHUIOKnBI9RRmaNPpTpEUeBrkwf0cShKCu/1lbdJ1KSF51I/ChsCIzbN+p bKTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=pcHeISXu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c23si1478660pfe.336.2018.02.14.05.43.56; Wed, 14 Feb 2018 05:43:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=pcHeISXu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030392AbeBNNnu (ORCPT + 28 others); Wed, 14 Feb 2018 08:43:50 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:37055 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030362AbeBNNns (ORCPT ); Wed, 14 Feb 2018 08:43:48 -0500 Received: by mail-wm0-f65.google.com with SMTP id v71so22111060wmv.2 for ; Wed, 14 Feb 2018 05:43:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=khIZOpGmG2HxeeBspaK1a22MkjUU3vUn32g7U5HJaSw=; b=pcHeISXu+WU03b/i4PR+v2tyg+ptViQ0I9LpX4Iy18KQREHwaLaPAALaRRwNRuhEfq 8wuz6ysumOys/pC0wfiCGxVE47pm6ANxQk07kCdVJ4eoxVSQmOD9Npqy1knl6omLAxFp xM9dfaHN8UfNlqeF3tDe4TPa6UzYUXpBlAzwnwz3boSTib0qnNF1bjqZSEsXpjuguK21 zVUZ3I81BknY1tqK59v1TmjAI105zyZ6DNlP1K44lBSRLOs+g+IgbvKIJIpbQcSTHlQN viEdjOGjvdBY5WgRcwqOUrcn7lVyG5MdbqJ1jp/En80sAu71xlT5FOzcg3jIwKsnV+b4 BgzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=khIZOpGmG2HxeeBspaK1a22MkjUU3vUn32g7U5HJaSw=; b=exWGtfTFGNpz8NZqVdu78S38OxYwIfgWJt46KIxwk+KcGQ/OqxdWCl6LNVQWVFsBAW 441ERKLHjBV21zrLa8A4g60GKgOAgWa6iSp0fCA1LJHD79P+fIvsv7wQvT1P5S8YikDu 6d/RhC1005RjfMsuyOd8B44yT1hq8bsawzAEQwJdh9urvEKFDy8bnTeooLKrXzfJkTeQ zvG/rtJsyhoGvH1491oFsI2xtXiQxPAijuEN5tBwZfClMTGExUsOaZXlC5K4a6NlWCRU H35zoVM9wZOcu2VeYzsT+OvyPuavgVpP7vRGxB8lp2SZOEXjzOhS5DGy/tVKPzeTOrpY YiNA== X-Gm-Message-State: APf1xPAGRAFi/YUV92C/Ps1awWFDeqZ5P4capSgESoJq6QTNJ1ugOEcu IfLK95t8CbAB3G91jN+o/a8Uv63X X-Received: by 10.28.25.67 with SMTP id 64mr3956499wmz.86.1518615826934; Wed, 14 Feb 2018 05:43:46 -0800 (PST) Received: from boomer.lan (cag06-3-82-243-161-21.fbx.proxad.net. [82.243.161.21]) by smtp.googlemail.com with ESMTPSA id k5sm6337694wmg.47.2018.02.14.05.43.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 14 Feb 2018 05:43:46 -0800 (PST) From: Jerome Brunet To: Michael Turquette , Stephen Boyd Cc: Jerome Brunet , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/8] clk: mux: add helper function for index/value translation Date: Wed, 14 Feb 2018 14:43:34 +0100 Message-Id: <20180214134340.17242-3-jbrunet@baylibre.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180214134340.17242-1-jbrunet@baylibre.com> References: <20180214134340.17242-1-jbrunet@baylibre.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add helper functions for the translation between parent index and register value in the generic multiplexer function. The purpose of this change is avoid duplicating the code in other clock providers, using the same generic logic. Signed-off-by: Jerome Brunet --- drivers/clk/clk-mux.c | 75 +++++++++++++++++++++++++------------------- include/linux/clk-provider.h | 4 +++ 2 files changed, 47 insertions(+), 32 deletions(-) -- 2.14.3 diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c index 39cabe157163..ac4a042f8658 100644 --- a/drivers/clk/clk-mux.c +++ b/drivers/clk/clk-mux.c @@ -26,35 +26,24 @@ * parent - parent is adjustable through clk_set_parent */ -static u8 clk_mux_get_parent(struct clk_hw *hw) +int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags, + unsigned int val) { - struct clk_mux *mux = to_clk_mux(hw); int num_parents = clk_hw_get_num_parents(hw); - u32 val; - /* - * FIXME need a mux-specific flag to determine if val is bitwise or numeric - * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1 - * to 0x7 (index starts at one) - * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so - * val = 0x4 really means "bit 2, index starts at bit 0" - */ - val = clk_readl(mux->reg) >> mux->shift; - val &= mux->mask; - - if (mux->table) { + if (table) { int i; for (i = 0; i < num_parents; i++) - if (mux->table[i] == val) + if (table[i] == val) return i; return -EINVAL; } - if (val && (mux->flags & CLK_MUX_INDEX_BIT)) + if (val && (flags & CLK_MUX_INDEX_BIT)) val = ffs(val) - 1; - if (val && (mux->flags & CLK_MUX_INDEX_ONE)) + if (val && (flags & CLK_MUX_INDEX_ONE)) val--; if (val >= num_parents) @@ -62,36 +51,58 @@ static u8 clk_mux_get_parent(struct clk_hw *hw) return val; } +EXPORT_SYMBOL_GPL(clk_mux_val_to_index); -static int clk_mux_set_parent(struct clk_hw *hw, u8 index) +unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index) { - struct clk_mux *mux = to_clk_mux(hw); - u32 val; - unsigned long flags = 0; + unsigned int val = index; - if (mux->table) { - index = mux->table[index]; + if (table) { + val = table[index]; } else { - if (mux->flags & CLK_MUX_INDEX_BIT) - index = 1 << index; + if (flags & CLK_MUX_INDEX_BIT) + val = 1 << index; - if (mux->flags & CLK_MUX_INDEX_ONE) - index++; + if (flags & CLK_MUX_INDEX_ONE) + val++; } + return val; +} +EXPORT_SYMBOL_GPL(clk_mux_index_to_val); + +static u8 clk_mux_get_parent(struct clk_hw *hw) +{ + struct clk_mux *mux = to_clk_mux(hw); + u32 val; + + val = clk_readl(mux->reg) >> mux->shift; + val &= mux->mask; + + return clk_mux_val_to_index(hw, mux->table, mux->flags, val); +} + +static int clk_mux_set_parent(struct clk_hw *hw, u8 index) +{ + struct clk_mux *mux = to_clk_mux(hw); + u32 val = clk_mux_index_to_val(mux->table, mux->flags, index); + unsigned long flags = 0; + u32 reg; + if (mux->lock) spin_lock_irqsave(mux->lock, flags); else __acquire(mux->lock); if (mux->flags & CLK_MUX_HIWORD_MASK) { - val = mux->mask << (mux->shift + 16); + reg = mux->mask << (mux->shift + 16); } else { - val = clk_readl(mux->reg); - val &= ~(mux->mask << mux->shift); + reg = clk_readl(mux->reg); + reg &= ~(mux->mask << mux->shift); } - val |= index << mux->shift; - clk_writel(val, mux->reg); + val = val << mux->shift; + reg |= val; + clk_writel(reg, mux->reg); if (mux->lock) spin_unlock_irqrestore(mux->lock, flags); diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index d8ba26d03332..fe720d679c31 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -511,6 +511,10 @@ struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name, void __iomem *reg, u8 shift, u32 mask, u8 clk_mux_flags, u32 *table, spinlock_t *lock); +int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags, + unsigned int val); +unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index); + void clk_unregister_mux(struct clk *clk); void clk_hw_unregister_mux(struct clk_hw *hw);