From patchwork Thu Dec 15 07:28:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Zhao X-Patchwork-Id: 5750 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id DD0E824056 for ; Thu, 15 Dec 2011 07:28:40 +0000 (UTC) Received: from mail-ey0-f180.google.com (mail-ey0-f180.google.com [209.85.215.180]) by fiordland.canonical.com (Postfix) with ESMTP id D339BA1816D for ; Thu, 15 Dec 2011 07:28:40 +0000 (UTC) Received: by mail-ey0-f180.google.com with SMTP id k10so1764271eaa.11 for ; Wed, 14 Dec 2011 23:28:40 -0800 (PST) Received: by 10.204.131.74 with SMTP id w10mr470588bks.36.1323934120642; Wed, 14 Dec 2011 23:28:40 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.205.129.2 with SMTP id hg2cs32687bkc; Wed, 14 Dec 2011 23:28:40 -0800 (PST) Received: by 10.236.123.52 with SMTP id u40mr2517216yhh.126.1323934117991; Wed, 14 Dec 2011 23:28:37 -0800 (PST) Received: from VA3EHSOBE002.bigfish.com (va3ehsobe002.messaging.microsoft.com. [216.32.180.12]) by mx.google.com with ESMTPS id j68si4081332yhn.125.2011.12.14.23.28.37 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 14 Dec 2011 23:28:37 -0800 (PST) Received-SPF: neutral (google.com: 216.32.180.12 is neither permitted nor denied by best guess record for domain of B20223@freescale.com) client-ip=216.32.180.12; Authentication-Results: mx.google.com; spf=neutral (google.com: 216.32.180.12 is neither permitted nor denied by best guess record for domain of B20223@freescale.com) smtp.mail=B20223@freescale.com Received: from mail37-va3-R.bigfish.com (10.7.14.242) by VA3EHSOBE002.bigfish.com (10.7.40.22) with Microsoft SMTP Server id 14.1.225.23; Thu, 15 Dec 2011 07:28:38 +0000 Received: from mail37-va3 (localhost [127.0.0.1]) by mail37-va3-R.bigfish.com (Postfix) with ESMTP id 571686A03B2; Thu, 15 Dec 2011 07:28:42 +0000 (UTC) X-SpamScore: 10 X-BigFish: VS10(z1039ozc8kzz1202hzz8275dhz2dh2a8h668h839h61h) X-Spam-TCS-SCL: 0:0 X-Forefront-Antispam-Report: CIP:70.37.183.190; KIP:(null); UIP:(null); IPV:NLI; H:mail.freescale.net; RD:none; EFVD:NLI X-FB-SS: 0, Received: from mail37-va3 (localhost.localdomain [127.0.0.1]) by mail37-va3 (MessageSwitch) id 132393412156708_31637; Thu, 15 Dec 2011 07:28:41 +0000 (UTC) Received: from VA3EHSMHS001.bigfish.com (unknown [10.7.14.248]) by mail37-va3.bigfish.com (Postfix) with ESMTP id 06759120259; Thu, 15 Dec 2011 07:28:41 +0000 (UTC) Received: from mail.freescale.net (70.37.183.190) by VA3EHSMHS001.bigfish.com (10.7.99.11) with Microsoft SMTP Server (TLS) id 14.1.225.23; Thu, 15 Dec 2011 07:28:33 +0000 Received: from az33smr01.freescale.net (10.64.34.199) by 039-SN1MMR1-002.039d.mgd.msft.net (10.84.1.15) with Microsoft SMTP Server id 14.1.355.3; Thu, 15 Dec 2011 01:28:32 -0600 Received: from b20223-02.ap.freescale.net (b20223-02.ap.freescale.net [10.192.242.124]) by az33smr01.freescale.net (8.13.1/8.13.0) with ESMTP id pBF7SRKh023208; Thu, 15 Dec 2011 01:28:30 -0600 (CST) From: Richard Zhao To: CC: , Richard Zhao , Sascha Hauer , Richard Zhao Subject: [RFC V2 3/8] clk: Add support for a generic clock multiplexer Date: Thu, 15 Dec 2011 15:28:20 +0800 Message-ID: <1323934105-23255-2-git-send-email-richard.zhao@freescale.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1323934105-23255-1-git-send-email-richard.zhao@freescale.com> References: <1323854638-3455-1-git-send-email-richard.zhao@linaro.org> <1323934105-23255-1-git-send-email-richard.zhao@freescale.com> MIME-Version: 1.0 X-OriginatorOrg: freescale.com This patch adds support for a common type of clock multiplexer. The multiplexer is described with register, shift and width and an array of clocks which correspond to the bit value. Signed-off-by: Sascha Hauer Signed-off-by: Richard Zhao --- drivers/clk/Kconfig | 4 +++ drivers/clk/Makefile | 1 + drivers/clk/clk-mux.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/clk.h | 47 ++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 0 deletions(-) create mode 100644 drivers/clk/clk-mux.c diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index 3bf7a66..c93478e 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -24,6 +24,10 @@ config GENERIC_CLK_DIVIDER bool depends on GENERIC_CLK +config GENERIC_CLK_MUX + bool + depends on GENERIC_CLK + config GENERIC_CLK_DEBUG bool "Clock tree representation in debugs" depends on GENERIC_CLK diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index fc71576..522dcbf 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -3,3 +3,4 @@ obj-$(CONFIG_CLKDEV_LOOKUP) += clkdev.o obj-$(CONFIG_GENERIC_CLK) += clk.o obj-$(CONFIG_GENERIC_CLK_BASIC) += clk-basic.o obj-$(CONFIG_GENERIC_CLK_DIVIDER) += clk-divider.o +obj-$(CONFIG_GENERIC_CLK_MUX) += clk-mux.o diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c new file mode 100644 index 0000000..05e813e --- /dev/null +++ b/drivers/clk/clk-mux.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2011 Sascha Hauer, Pengutronix + * Copyright (C) 2011 Richard Zhao, Linaro + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Standard functionality for the common clock API. + */ +#include +#include +#include +#include + +#define to_clk_mux(ck) container_of(ck, struct clk_mux, clk) + +static struct clk *clk_mux_get_parent(struct clk *clk) +{ + struct clk_mux *mux = to_clk_mux(clk); + u32 val; + + val = readl(mux->reg) >> mux->shift; + val &= (1 << mux->width) - 1; + + if (val >= mux->num_clks) + return ERR_PTR(-EINVAL); + + return mux->clks[val]; +} + +static int clk_mux_set_parent(struct clk *clk, struct clk *parent) +{ + struct clk_mux *mux = to_clk_mux(clk); + u32 val; + int i; + unsigned long flags = 0; + + for (i = 0; i < mux->num_clks; i++) + if (mux->clks[i] == parent) + break; + + if (i == mux->num_clks) + return -EINVAL; + + if (mux->lock) + spin_lock_irqsave(mux->lock, flags); + + val = readl(mux->reg); + val &= ~(((1 << mux->width) - 1) << mux->shift); + val |= i << mux->shift; + writel(val, mux->reg); + + if (mux->lock) + spin_unlock_irqrestore(mux->lock, flags); + + return 0; +} + +struct clk_hw_ops clk_mux_ops = { + .get_parent = clk_mux_get_parent, + .set_parent = clk_mux_set_parent, +}; +EXPORT_SYMBOL_GPL(clk_mux_ops); diff --git a/include/linux/clk.h b/include/linux/clk.h index 1fa2bca..ea225e5 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -217,6 +217,53 @@ extern struct clk_hw_ops clk_divider_ops; #endif /* CONFIG_GENERIC_CLK_DIVIDER */ +#ifdef CONFIG_GENERIC_CLK_MUX + +#include + +/** + * clock multiplexer + * + * @reg the register this multiplexer can be configured with + * @shift the shift to the start bit of this multiplexer + * @width the width in bits of this multiplexer + * @num_clks number of parent clocks + * @lock register lock + * @clks array of possible parents for this multiplexer. Can contain + * holes with NULL in it for invalid register settings + * + * This clock implements get_parent/set_parent. prepare/unprepare, + * enable/disable and get_rate operations are passed through to the parent, + * the rate is not adjustable. + */ +struct clk_mux { + struct clk clk; + void __iomem *reg; + unsigned char shift; + unsigned char width; + unsigned char num_clks; + spinlock_t *lock; + struct clk **clks; +}; + +extern struct clk_hw_ops clk_mux_ops; + +#define DEFINE_CLK_MUX(_name, _reg, _shift, _width, _clks, _lock) \ + struct clk_mux _name = { \ + .clk = { \ + .name = #_name, \ + .ops = &clk_mux_ops, \ + }, \ + .reg = (_reg), \ + .shift = (_shift), \ + .width = (_width), \ + .clks = (_clks), \ + .num_clks = ARRAY_SIZE(_clks), \ + .lock = (_lock), \ + } + +#endif /* CONFIG_GENERIC_CLK_MUX */ + /** * clk_init - initialize the data structures in a struct clk * @dev: device initializing this clk, placeholder for now