From patchwork Fri Sep 18 01:59:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuninori Morimoto X-Patchwork-Id: 7212411 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Original-To: patchwork-linux-sh@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 31D4D9F32B for ; Fri, 18 Sep 2015 02:00:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9621E206B5 for ; Fri, 18 Sep 2015 02:00:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C29922062E for ; Fri, 18 Sep 2015 02:00:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752415AbbIRCAF (ORCPT ); Thu, 17 Sep 2015 22:00:05 -0400 Received: from relmlor3.renesas.com ([210.160.252.173]:9837 "EHLO relmlie2.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752043AbbIRCAC (ORCPT ); Thu, 17 Sep 2015 22:00:02 -0400 Received: from unknown (HELO relmlir1.idc.renesas.com) ([10.200.68.151]) by relmlie2.idc.renesas.com with ESMTP; 18 Sep 2015 11:00:01 +0900 Received: from relmlac3.idc.renesas.com (relmlac3.idc.renesas.com [10.200.69.23]) by relmlir1.idc.renesas.com (Postfix) with ESMTP id 5392F4294E; Fri, 18 Sep 2015 11:00:01 +0900 (JST) Received: by relmlac3.idc.renesas.com (Postfix, from userid 0) id 30C5F1806F; Fri, 18 Sep 2015 11:00:01 +0900 (JST) Received: from relmlac3.idc.renesas.com (localhost [127.0.0.1]) by relmlac3.idc.renesas.com (Postfix) with ESMTP id 2762C1800A; Fri, 18 Sep 2015 11:00:01 +0900 (JST) Received: from relmlii1.idc.renesas.com [10.200.68.65] by relmlac3.idc.renesas.com with ESMTP id LAU20833; Fri, 18 Sep 2015 11:00:01 +0900 X-IronPort-AV: E=Sophos;i="5.17,550,1437404400"; d="scan'";a="194700233" Received: from mail-hk2apc01lp0207.outbound.protection.outlook.com (HELO APC01-HK2-obe.outbound.protection.outlook.com) ([65.55.88.207]) by relmlii1.idc.renesas.com with ESMTP/TLS/AES256-SHA; 18 Sep 2015 11:00:00 +0900 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=kuninori.morimoto.gx@renesas.com; Received: from morimoto-PC.renesas.com (211.11.155.144) by HK2PR06MB0594.apcprd06.prod.outlook.com (10.161.187.145) with Microsoft SMTP Server (TLS) id 15.1.268.17; Fri, 18 Sep 2015 01:59:59 +0000 Message-ID: <87eghw1of5.wl%kuninori.morimoto.gx@renesas.com> From: Kuninori Morimoto Subject: [PATCH 1/1 v3] clk: add CS2000 Fractional-N driver User-Agent: Wanderlust/2.15.9 Emacs/24.3 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") To: Simon CC: Magnus , In-Reply-To: <87fv2c1ogc.wl%kuninori.morimoto.gx@renesas.com> References: <87twqs1osz.wl%kuninori.morimoto.gx@renesas.com> <87fv2c1ogc.wl%kuninori.morimoto.gx@renesas.com> Date: Fri, 18 Sep 2015 01:59:59 +0000 X-Originating-IP: [211.11.155.144] X-ClientProxiedBy: TY1PR01CA0003.jpnprd01.prod.outlook.com (25.161.131.141) To HK2PR06MB0594.apcprd06.prod.outlook.com (25.161.187.145) X-Microsoft-Exchange-Diagnostics: 1; HK2PR06MB0594; 2:OyaO0ghjI4fo4gzgElEfSxSHvcS7hIeVErF3DWzdLWsb6uk8XuOjgqwWyGH3OJZbBL9dXqcaJUt5KKQPjwpv24BAuaB9dr+SmVqWZYpGt1TQIH02LIiGREJVQF21xPM7ev6m1nTsXyWS/m3LUVLY5NwuRfyhsBIuYUF5B49noMk=; 3:I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X5STGMuPkhS1+9onIQDDRjZcVwVFrz46mSz8flx/6uIdcb390/K0NFcaQNHBExQ05rgn3VDEeLAEBOUSfIlMosJUOa4VxsKB8FXzMl22upnwmf/rhNpX6pYRQ0pCOZxS73y6JgiedNiSxLMh/XdXYo58xfLCN6XMrVEdtN+bAC59NwAkbnMeQfcFSsh6bb2OTCynVg0u5GwFlEk5LfZ55W+K+qgveCFjZdndSIZtRzBFWdi9DzmRLluqlKr7yfMs9SRA2JnHu48iMyIux2h5D9cPKL6xwG5uFda3dxc= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(8121501046)(5005006)(520078)(520075)(3002001); SRVR:HK2PR06MB0594; BCL:0; PCL:0; RULEID:; SRVR:HK2PR06MB0594; X-Forefront-PRVS: 0703B549E4 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6009001)(199003)(189002)(36756003)(87976001)(77156002)(229853001)(97736004)(5001960100002)(53416004)(81156007)(40100003)(189998001)(68736005)(105586002)(76176999)(110136002)(92566002)(2950100001)(4001540100001)(62966003)(83506001)(50986999)(122386002)(42186005)(33646002)(77096005)(66066001)(47776003)(46406003)(4001350100001)(23726002)(69596002)(50466002)(19580405001)(5007970100001)(64706001)(46102003)(5001860100001)(54356999)(106356001)(19580395003)(101416001)(86362001)(5004730100002)(5001830100001)(2004002); DIR:OUT; SFP:1102; SCL:1; SRVR:HK2PR06MB0594; H:morimoto-PC.renesas.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HK2PR06MB0594; 23:YPH3P2ey7a07LqQtH10VNI9CBW0g1L81qFls2ACMf?= =?us-ascii?Q?9SqslCG6bR50g7lVPlWqMnPCy2Wtiwz91eXONoYUTU79uaW+izlestDNGlGl?= =?us-ascii?Q?Oq6PmuWxI0f/+JwD94FktvwrX45CK3lBD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?us-ascii?Q?NncP+Zo79lTdTBTP7CtyQIlaK7WWJturlIaYznV4hJ09SVUzTQMqaXdFeDMB?= =?us-ascii?Q?Bjt+v2qsvVxbp+OS60Ps8eYC556XPQhmqGHaDYWvSdp43PXNekVIbLcF0D+h?= =?us-ascii?Q?ZTPgNx+v/2gUJXkm/JT4mbTcM4cJCUq4eg5UUuaQNQKWBwyCumqRuUxr/BIR?= =?us-ascii?Q?NnlbGwPialQwysYObsDvuCC+26kBm17Dj04EsT45aMNkUZ0VrGNavYH5yoex?= =?us-ascii?Q?z2SfEZU0brWYSlqLq0=3D?= X-Microsoft-Exchange-Diagnostics: 1; HK2PR06MB0594; 5:kow1Oj7wg0oESEnBJxtEGc+TvERtpqvmPa1763si/YbdQW+ajn8R+x3jAmbyTpKZKrCwFqGNWs7Ho10kueDnYHFvLoqaNhCpA34vYN4GjwlUTGWdnIefoFat62bhTbORRoekpBj+1bNIbb8p9aYIgg==; 24:GWiNm93fQZU5jcGels7J0uA41RKXGbL3qWOwNXG9d7zJ73WIHkC0/AJ0dyeHmQNQoYv3W1RQ49WnoXALxs2qVKE3cL2M6HHvU3HMwdcGgks=; 20:ikyrFPgMCRMlOUP17IAo05kxtkamuHqH19AgkRTxepsYiylWBFSs1Un3SB2XwABQ+ifXESPazTwOXXp1k5pSJgfMYXrfOiJ8AgcfNU8IwtdrP1VbUbguKSE8A5OtEJkqSfvaTsCEJb8OYhVUiw8hcUs98VQM+EY0aR9QomtjKD4= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: renesas.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2015 01:59:59.0378 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HK2PR06MB0594 Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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: Kuninori Morimoto This patch adds CS2000 Fractional-N driver as clock provider. Signed-off-by: Kuninori Morimoto --- v2 -> v3 - no change .../devicetree/bindings/clock/cs2000-cp.txt | 24 + drivers/clk/Kconfig | 6 + drivers/clk/Makefile | 1 + drivers/clk/clk-cs2000-cp.c | 530 +++++++++++++++++++++ 4 files changed, 561 insertions(+) create mode 100644 Documentation/devicetree/bindings/clock/cs2000-cp.txt create mode 100644 drivers/clk/clk-cs2000-cp.c diff --git a/Documentation/devicetree/bindings/clock/cs2000-cp.txt b/Documentation/devicetree/bindings/clock/cs2000-cp.txt new file mode 100644 index 0000000..b21033d --- /dev/null +++ b/Documentation/devicetree/bindings/clock/cs2000-cp.txt @@ -0,0 +1,24 @@ +CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier + +Required properties: + +- compatible: "cirrus,cs2000-cp" +- reg: The chip select number on the I2C bus +- clocks: common clock binding for CLK_IN, XTI/REF_CLK +- clock-names: CLK_IN : clk_in, XTI/REF_CLK : ref_clk +- clock-frequency: clock frequency of CLK_OUT +- #clock-cells: must be <0> + +Example: + +&i2c2 { + ... + cs2000: clk_multiplier@4f { + #clock-cells = <0>; + compatible = "cirrus,cs2000-cp"; + reg = <0x4f>; + clocks = <&rcar_sound 0>, <&x12_clk>; + clock-names = "clk_in", "ref_clk"; + clock-frequency = <24576000>; /* 1/1 divide */ + }; +}; diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index 42f7120..0e961b2 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -95,6 +95,12 @@ config COMMON_CLK_CDCE925 Given a target output frequency, the driver will set the PLL and divider to best approximate the desired output. +config COMMON_CLK_CS2000_CP + tristate "Clock driver for CS2000 Fractional-N Clock Synthesizer & Clock Multiplier" + depends on I2C + help + If you say yes here you get support for the CS2000 clock multiplier. + config COMMON_CLK_S2MPS11 tristate "Clock driver for S2MPS1X/S5M8767 MFD" depends on MFD_SEC_CORE diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index 9d31e2c..2fb77a8 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -21,6 +21,7 @@ obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN) += clk-axi-clkgen.o obj-$(CONFIG_ARCH_AXXIA) += clk-axm5516.o obj-$(CONFIG_ARCH_BCM2835) += clk-bcm2835.o obj-$(CONFIG_COMMON_CLK_CDCE706) += clk-cdce706.o +obj-$(CONFIG_COMMON_CLK_CS2000_CP) += clk-cs2000-cp.o obj-$(CONFIG_ARCH_CLPS711X) += clk-clps711x.o obj-$(CONFIG_ARCH_EFM32) += clk-efm32gg.o obj-$(CONFIG_ARCH_HIGHBANK) += clk-highbank.o diff --git a/drivers/clk/clk-cs2000-cp.c b/drivers/clk/clk-cs2000-cp.c new file mode 100644 index 0000000..ab8360f --- /dev/null +++ b/drivers/clk/clk-cs2000-cp.c @@ -0,0 +1,530 @@ +/* + * CS2000 -- CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier + * + * Copyright (C) 2015 Renesas Electronics Corporation + * Kuninori Morimoto + * + * 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. + */ +#include +#include +#include +#include +#include +#include + +#define CH_MAX 4 + +#define DEVICE_ID 0x1 +#define DEVICE_CTRL 0x2 +#define DEVICE_CFG1 0x3 +#define DEVICE_CFG2 0x4 +#define GLOBAL_CFG 0x5 +#define Ratio_Add(x, nth) (6 + (x * 4) + (nth)) +#define Ratio_Val(x, nth) ((x >> (24 - (8 * nth))) & 0xFF) +#define Val_Ratio(x, nth) ((x & 0xFF) << (24 - (8 * nth))) +#define FUNC_CFG1 0x16 +#define FUNC_CFG2 0x17 + +/* DEVICE_CTRL */ +#define PLL_UNLOCK (1 << 7) + +/* DEVICE_CFG1 */ +#define RSEL(x) (((x) & 0x3) << 3) +#define RSEL_MASK RSEL(0x3) +#define ENDEV1 (0x1) + +/* GLOBAL_CFG */ +#define ENDEV2 (0x1) + +#define CH_SIZE_ERR(ch) ((ch < 0) || (ch >= CH_MAX)) +#define hw_to_priv(_hw) container_of(_hw, struct cs2000_priv, hw) +#define priv_to_client(priv) (priv->client) +#define priv_to_dev(priv) (&(priv_to_client(priv)->dev)) + +#define CLK_IN 0 +#define REF_CLK 1 +#define CLK_MAX 2 + +struct cs2000_priv { + struct clk_hw hw; + struct i2c_client *client; + struct clk *clk_in; + struct clk *ref_clk; + struct clk *clk_out; +}; + +static const struct of_device_id cs2000_of_match[] = { + { .compatible = "cirrus,cs2000-cp", }, + {}, +}; +MODULE_DEVICE_TABLE(of, cs2000_of_match); + +static const struct i2c_device_id cs2000_id[] = { + { "cs2000-cp", }, + {} +}; +MODULE_DEVICE_TABLE(i2c, cs2000_id); + +#define cs2000_read(priv, addr) \ + i2c_smbus_read_byte_data(priv_to_client(priv), addr) +#define cs2000_write(priv, addr, val) \ + i2c_smbus_write_byte_data(priv_to_client(priv), addr, val) + +static int cs2000_bset(struct cs2000_priv *priv, u8 addr, u8 mask, u8 val) +{ + s32 data; + + data = cs2000_read(priv, addr); + if (data < 0) + return data; + + data &= ~mask; + data |= (val & mask); + + return cs2000_write(priv, addr, data); +} + +static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable) +{ + u32 val; + int ret; + + val = enable ? ENDEV1 : 0; + ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1, val); + if (ret < 0) + return ret; + + val = enable ? ENDEV2 : 0; + ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2, val); + if (ret < 0) + return ret; + + return 0; +} + +static int cs2000_clk_in_bound_rate(struct cs2000_priv *priv, + u32 rate_in) +{ + u32 val; + + if (rate_in >= 32000000 && + rate_in < 56000000) + val = 0x0; + else if (rate_in >= 16000000 && + rate_in < 28000000) + val = 0x1; + else if (rate_in >= 8000000 && + rate_in < 14000000) + val = 0x2; + else + return -EINVAL; + + return cs2000_bset(priv, FUNC_CFG1, 0x3 << 3, val << 3); +} + +static int cs2000_wait_pll_lock(struct cs2000_priv *priv) +{ + struct device *dev = priv_to_dev(priv); + s32 val; + unsigned int i; + + for (i = 0; i < 256; i++) { + val = cs2000_read(priv, DEVICE_CTRL); + if (val < 0) + return val; + if (!(val & PLL_UNLOCK)) + return 0; + udelay(1); + } + + dev_err(dev, "pll lock failed\n"); + + return -EIO; +} + +static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable) +{ + u32 val = enable ? 0 : 0x3; + + /* enable both AUX_OUT, CLK_OUT */ + return cs2000_write(priv, DEVICE_CTRL, val); +} + +static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out) +{ + u64 ratio; + + /* + * ratio = rate_out / rate_in * 2^20 + * + * To avoid over flow, rate_out is u64 + * The result should be u32 + */ + ratio = (u64)rate_out << 20; + do_div(ratio, rate_in); + + return (u32)ratio; +} + +static unsigned long cs2000_ratio_to_rate(u32 ratio, u32 rate_in) +{ + u64 rate_out; + + /* + * ratio = rate_out / rate_in * 2^20 + * + * To avoid over flow, rate_out is u64 + * The result should be u32 + */ + + rate_out = (u64)ratio * rate_in; + return (unsigned long)(rate_out >> 20); +} + +static int cs2000_ratio_set(struct cs2000_priv *priv, + int ch, u32 rate_in, u32 rate_out) +{ + u32 val; + unsigned int i; + int ret; + + if (CH_SIZE_ERR(ch)) + return -EINVAL; + + val = cs2000_rate_to_ratio(rate_in, rate_out); + for (i = 0; i < 4; i++) { + ret = cs2000_write(priv, + Ratio_Add(ch, i), + Ratio_Val(val, i)); + if (ret < 0) + return ret; + } + + return 0; +} + +static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch) +{ + u32 tmp, val; + unsigned int i; + + val = 0; + for (i = 0; i < 4; i++) { + tmp = cs2000_read(priv, + Ratio_Add(ch, i)); + if (tmp < 0) + return 0; + + val |= Val_Ratio(tmp, i); + } + + return val; +} + +static int cs2000_ratio_select(struct cs2000_priv *priv, int ch) +{ + int ret; + + if (CH_SIZE_ERR(ch)) + return -EINVAL; + + /* + * FIXME + * + * this driver supports static ratio mode only + * at this point + */ + ret = cs2000_bset(priv, DEVICE_CFG1, RSEL_MASK, RSEL(ch)); + if (ret < 0) + return ret; + + ret = cs2000_write(priv, DEVICE_CFG2, 0x0); + if (ret < 0) + return ret; + + return 0; +} + +static unsigned long cs2000_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct cs2000_priv *priv = hw_to_priv(hw); + int ch = 0; /* it uses ch0 only at this point */ + u32 ratio; + + ratio = cs2000_ratio_get(priv, ch); + + return cs2000_ratio_to_rate(ratio, parent_rate); +} + +static long cs2000_round_rate(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + u32 ratio; + + ratio = cs2000_rate_to_ratio(*parent_rate, rate); + + return cs2000_ratio_to_rate(ratio, *parent_rate); +} + +static int __cs2000_set_rate(struct cs2000_priv *priv, int ch, + unsigned long rate, unsigned long parent_rate) + +{ + int ret; + + ret = cs2000_clk_in_bound_rate(priv, parent_rate); + if (ret < 0) + return ret; + + ret = cs2000_ratio_set(priv, ch, parent_rate, rate); + if (ret < 0) + return ret; + + ret = cs2000_ratio_select(priv, ch); + if (ret < 0) + return ret; + + return 0; +} + +static int cs2000_set_rate(struct clk_hw *hw, + unsigned long rate, unsigned long parent_rate) +{ + struct cs2000_priv *priv = hw_to_priv(hw); + int ch = 0; /* it uses ch0 only at this point */ + + return __cs2000_set_rate(priv, ch, rate, parent_rate); +} + +static int __cs2000_enable(struct cs2000_priv *priv) +{ + int ret; + + ret = cs2000_enable_dev_config(priv, true); + if (ret < 0) + return ret; + + ret = cs2000_clk_out_enable(priv, true); + if (ret < 0) + return ret; + + ret = cs2000_wait_pll_lock(priv); + if (ret < 0) + return ret; + + return 0; +} + +static int cs2000_enable(struct clk_hw *hw) +{ + struct cs2000_priv *priv = hw_to_priv(hw); + + return __cs2000_enable(priv); +} + +static void cs2000_disable(struct clk_hw *hw) +{ + struct cs2000_priv *priv = hw_to_priv(hw); + + cs2000_enable_dev_config(priv, false); + + cs2000_clk_out_enable(priv, false); +} + +static u8 cs2000_get_parent(struct clk_hw *hw) +{ + /* always return REF_CLK */ + return REF_CLK; +} + +static const struct clk_ops cs2000_ops = { + .get_parent = cs2000_get_parent, + .recalc_rate = cs2000_recalc_rate, + .round_rate = cs2000_round_rate, + .set_rate = cs2000_set_rate, + .enable = cs2000_enable, + .disable = cs2000_disable, +}; + +static int cs2000_clk_get(struct cs2000_priv *priv) +{ + struct i2c_client *client = priv_to_client(priv); + struct device *dev = &client->dev; + struct clk *clk_in, *ref_clk; + + clk_in = devm_clk_get(dev, "clk_in"); + /* not yet provided */ + if (IS_ERR(clk_in)) + return -EPROBE_DEFER; + + ref_clk = devm_clk_get(dev, "ref_clk"); + /* not yet provided */ + if (IS_ERR(ref_clk)) + return -EPROBE_DEFER; + + priv->clk_in = clk_in; + priv->ref_clk = ref_clk; + + return 0; +} + +static int cs2000_clk_register(struct cs2000_priv *priv) +{ + struct device *dev = priv_to_dev(priv); + struct device_node *np = dev->of_node; + struct clk_init_data init; + const char *name = np->name; + struct clk *clk; + static const char *parent_names[CLK_MAX]; + int ret; + + of_property_read_string(np, "clock-output-names", &name); + + parent_names[CLK_IN] = __clk_get_name(priv->clk_in); + parent_names[REF_CLK] = __clk_get_name(priv->ref_clk); + + init.name = name; + init.ops = &cs2000_ops; + init.flags = CLK_IS_BASIC | CLK_SET_RATE_GATE; + init.parent_names = parent_names; + init.num_parents = ARRAY_SIZE(parent_names); + + priv->hw.init = &init; + + clk = clk_register(NULL, &priv->hw); + if (IS_ERR(clk)) + return PTR_ERR(clk); + + ret = of_clk_add_provider(np, of_clk_src_simple_get, clk); + if (ret < 0) { + clk_unregister(clk); + return ret; + } + + priv->clk_out = clk; + + return 0; +} + +static int cs2000_clk_init(struct cs2000_priv *priv) +{ + struct device *dev = priv_to_dev(priv); + struct device_node *np = dev->of_node; + u32 rate; + int ch = 0; /* it uses ch0 only at this point */ + int ret; + + if (of_property_read_u32(np, "clock-frequency", &rate)) + return 0; + + ret = __cs2000_set_rate(priv, ch, rate, clk_get_rate(priv->ref_clk)); + if (ret < 0) + return ret; + + ret = __cs2000_enable(priv); + if (ret < 0) + return ret; + + return 0; +} + +static int cs2000_version_print(struct cs2000_priv *priv) +{ + struct i2c_client *client = priv_to_client(priv); + struct device *dev = &client->dev; + s32 val = cs2000_read(priv, DEVICE_ID); + const char *revision; + + if (val < 0) + return val; + + /* CS2000 should be 0x0 */ + if (0 != (val >> 3)) + return -EIO; + + switch (val & 0x7) { + case 0x4: + revision = "B2 / B3"; + break; + case 0x6: + revision = "C1"; + break; + default: + return -EIO; + } + + dev_info(dev, "revision - %s\n", revision); + + return 0; +} + +static int cs2000_remove(struct i2c_client *client) +{ + struct cs2000_priv *priv = i2c_get_clientdata(client); + struct device *dev = &client->dev; + struct device_node *np = dev->of_node; + + of_clk_del_provider(np); + + clk_unregister(priv->clk_out); + + return 0; +} + +static int cs2000_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct cs2000_priv *priv; + struct device *dev = &client->dev; + int ret; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->client = client; + i2c_set_clientdata(client, priv); + + ret = cs2000_clk_get(priv); + if (ret < 0) + return ret; + + ret = cs2000_clk_register(priv); + if (ret < 0) + return ret; + + ret = cs2000_clk_init(priv); + if (ret < 0) + return ret; + + ret = cs2000_version_print(priv); + if (ret < 0) + goto probe_err; + + return 0; + +probe_err: + cs2000_remove(client); + + return ret; +} + +static struct i2c_driver cs2000_driver = { + .driver = { + .name = "cs2000-cp", + .of_match_table = cs2000_of_match, + }, + .probe = cs2000_probe, + .remove = cs2000_remove, + .id_table = cs2000_id, +}; + +module_i2c_driver(cs2000_driver); + +MODULE_DESCRIPTION("CS2000-CP driver"); +MODULE_AUTHOR("Kuninori Morimoto "); +MODULE_LICENSE("GPL v2");