Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752055AbbKRFkV (ORCPT ); Wed, 18 Nov 2015 00:40:21 -0500 Received: from relmlor4.renesas.com ([210.160.252.174]:51225 "EHLO relmlie3.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751206AbbKRFkR (ORCPT ); Wed, 18 Nov 2015 00:40:17 -0500 X-IronPort-AV: E=Sophos;i="5.20,311,1444662000"; d="scan'";a="200038007" Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=kuninori.morimoto.gx@renesas.com; Message-ID: <87mvubx440.wl%kuninori.morimoto.gx@renesas.com> From: Kuninori Morimoto To: Kuninori Morimoto CC: Stephen Boyd , Simon , Michael Turquette , Magnus , Linux-SH , Linux-Kernel , Subject: Re: [PATCH v6] clk: add CS2000 Fractional-N driver In-Reply-To: <87k2pqlkx1.wl%kuninori.morimoto.gx@renesas.com> References: <87k2pqlkx1.wl%kuninori.morimoto.gx@renesas.com> User-Agent: Wanderlust/2.15.9 Emacs/24.3 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset="US-ASCII" Date: Wed, 18 Nov 2015 05:40:11 +0000 X-Originating-IP: [211.11.155.144] X-ClientProxiedBy: TY1PR01CA0001.jpnprd01.prod.outlook.com (25.161.131.139) To SG2PR06MB1019.apcprd06.prod.outlook.com (25.161.9.27) X-Microsoft-Exchange-Diagnostics: 1;SG2PR06MB1019;2:2/u6JS3eC5FbTsh7NEaeqbNCwcmJddGX8PuUXkgaxHAlwiMRQgFryzLA8vHgxfQfw/mokHxmAhX4azR8olmUClwhqK5VrI9EovrEP6pz8jGZ658ntY7i9X/0t23hD8I+qHDm6VQ9yyEb2ZUvTC9njSGf9SdLG77eY7FCPIWJbf8=;3:SygdBLqR6B7OIFRjmo4yf+fo5p76MPhGYllY0cdY7HBc2iBzLlwsqVfL/MmGvGj61NFWHTEwudZCtqr25ruoYR3w8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isyJZJCHdYp8xNk4idLrF5FCQJ2fPGKpvrJ57zEOEL6vzDihGH2kU9lRxWSYa8AWYkMKo/zQNjwtgsexA0uHwR5Ru2vKcNmo3glzoLtT9zTQMeJq1ZFIyLA1TNCnCVffTuuo6VLAfZqsNV6gkxjaU2P6dOoFxZA8vpSbHpgpF18q59ejzEZitZZYaNBY3Cxwpl+DG9qIo7MxAcRxVRPxC46SXFwg3laO2L6Z8xfjFp8D6dMp8X++kk+PtVB4zVr6x9WNsMCK3Rn0HG7bs7wU2KYBzroTvg X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(85106069007906); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(520078)(5005006)(8121501046)(10201501046)(3002001);SRVR:SG2PR06MB1019;BCL:0;PCL:0;RULEID:;SRVR:SG2PR06MB1019; X-Forefront-PRVS: 0764C4A8CD X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(6009001)(199003)(189002)(106356001)(105586002)(66066001)(83506001)(87976001)(53416004)(86362001)(42186005)(69596002)(586003)(5008740100001)(19580405001)(19580395003)(50466002)(92566002)(6200100001)(47776003)(46406003)(54356999)(76176999)(33646002)(15975445007)(77096005)(2950100001)(122386002)(101416001)(40100003)(5007970100001)(50986999)(36756003)(4001450100002)(5004730100002)(5001960100002)(7049001)(5001920100001)(189998001)(4001350100001)(110136002)(81156007)(23726002)(97736004)(2004002)(422495003);DIR:OUT;SFP:1102;SCL:1;SRVR:SG2PR06MB1019;H:morimoto-PC.renesas.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;SG2PR06MB1019;23:GGSVGxQGWKgmXMGUp1RYIpOG3x1EZYYdQSAVh9rjQ?= =?us-ascii?Q?16q/6wX83FXYcfmdYKh8PgB6f42UeD/oM1YEtLwQ6lY4SbK8XVghhIFK4OTH?= =?us-ascii?Q?WfgUcipDtvf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RUrRmjzwJ/CrKbOT1El+ooNc?= =?us-ascii?Q?YeSLLzHMfDuUCHKYtuEln3fWL74h9+DlqUJ7Uk9+c8XjW2X2XTWz88yYC68o?= =?us-ascii?Q?qg1qqWJhSZ0/hTya4vxgkMJlBhTctZuodxjaUGUo00Eyb3HAv4Y/2MbYrpM1?= =?us-ascii?Q?WAZx+I4+SvYF/OzXMaKdntkhBQ+8LH9LB+r9usiixTHpmuph+sPCbsaXHzsk?= =?us-ascii?Q?zrJirh6ASvUn763BGGHNA5yk8Jm4Wfizz146S3Xfk8nxHobUqaswz/e39lMe?= =?us-ascii?Q?9gce11AqxgspB8B868=3D?= X-Microsoft-Exchange-Diagnostics: 1;SG2PR06MB1019;5:niPE1JcMiXTqd1ZJiZZ6E8jZ8gPeiNfX3LXNFzpsipbxI1lXjrx3pSD0EBZeKKyxmVSt4wC0bfxZW3jF7cp8RTLn000C0mKoJwxivd6y1spj385fS0T4ZmZOmmugbiA+bi8mKigviHjczpFlMRxFdA==;24:xW3pNZdxrgr7dVcwtFWGD1ifUS0rwL1MtRaVxHmTvIYQ3lQljmae895bzRm/+EGG6ifbmNUUPyK0NPMCkVx/euG5e15IlB5MJZ+Cmt0X4js=;20:rZlGUCCk2d1OXCQK+5jczBJmTz3bSPmhQQKsPmRrbX6pimfe3dGYgztulplkRQHDnmcUP3uhYm14GwhWJ/7AVxw8tl3m8CJxAaxRS1CvwTEdhnAttSMQSJ92jICw7QuGroeyJ0qq5FCfrQoI4OfFICsWNErImlW3ULnfbvcUaiw= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: renesas.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2015 05:40:11.9397 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB1019 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 15634 Lines: 613 Hi ping ? > > From: Kuninori Morimoto > > This patch adds CS2000 Fractional-N driver as clock provider. > > Signed-off-by: Kuninori Morimoto > --- > v5 -> v6 > > - remove redundant variable from each functions > - multi line code become one line > - end of comment has dot > - cs2000_wait_pll_lock() returns -ETIMEDOUT instead of -EIO > - removed explicit casting from ratio function > - removed magic number > > .../devicetree/bindings/clock/cs2000-cp.txt | 22 + > drivers/clk/Kconfig | 6 + > drivers/clk/Makefile | 1 + > drivers/clk/clk-cs2000-cp.c | 509 +++++++++++++++++++++ > 4 files changed, 538 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..54e6df0 > --- /dev/null > +++ b/Documentation/devicetree/bindings/clock/cs2000-cp.txt > @@ -0,0 +1,22 @@ > +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-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"; > + }; > +}; > diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig > index a1fa611..b8c347f 100644 > --- a/drivers/clk/Kconfig > +++ b/drivers/clk/Kconfig > @@ -116,6 +116,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 366350a..86afe50 100644 > --- a/drivers/clk/Makefile > +++ b/drivers/clk/Makefile > @@ -21,6 +21,7 @@ obj-$(CONFIG_MACH_ASM9260) += clk-asm9260.o > obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN) += clk-axi-clkgen.o > obj-$(CONFIG_ARCH_AXXIA) += clk-axm5516.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..cc90d87 > --- /dev/null > +++ b/drivers/clk/clk-cs2000-cp.c > @@ -0,0 +1,509 @@ > +/* > + * 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 RATIO_REG_SIZE 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_ID */ > +#define REVISION_MASK (0x7) > +#define REVISION_B2_B3 (0x4) > +#define REVISION_C1 (0x6) > + > +/* 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) > +{ > + int ret; > + > + ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1, > + enable ? ENDEV1 : 0); > + if (ret < 0) > + return ret; > + > + ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2, > + enable ? ENDEV2 : 0); > + 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 -ETIMEDOUT; > +} > + > +static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable) > +{ > + /* enable both AUX_OUT, CLK_OUT */ > + return cs2000_write(priv, DEVICE_CTRL, enable ? 0 : 0x3); > +} > + > +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 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 or unsigned long. > + */ > + > + rate_out = (u64)ratio * rate_in; > + return 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 < RATIO_REG_SIZE; 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 < RATIO_REG_SIZE; 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 clk_hw *hw) > +{ > + struct cs2000_priv *priv = hw_to_priv(hw); > + 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 ret; > +} > + > +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, > + .prepare = cs2000_enable, > + .unprepare = 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 ch = 0; /* it uses ch0 only at this point */ > + int rate; > + int ret; > + > + of_property_read_string(np, "clock-output-names", &name); > + > + /* > + * set default rate as 1/1. > + * otherwise .set_rate which setup ratio > + * is never called if user requests 1/1 rate > + */ > + rate = clk_get_rate(priv->ref_clk); > + ret = __cs2000_set_rate(priv, ch, rate, rate); > + if (ret < 0) > + return ret; > + > + 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_SET_RATE_GATE; > + init.parent_names = parent_names; > + init.num_parents = ARRAY_SIZE(parent_names); > + > + priv->hw.init = &init; > + > + clk = clk_register(dev, &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_version_print(struct cs2000_priv *priv) > +{ > + struct i2c_client *client = priv_to_client(priv); > + struct device *dev = &client->dev; > + s32 val; > + const char *revision; > + > + val = cs2000_read(priv, DEVICE_ID); > + if (val < 0) > + return val; > + > + /* CS2000 should be 0x0 */ > + if (val >> 3) > + return -EIO; > + > + switch (val & REVISION_MASK) { > + case REVISION_B2_B3: > + revision = "B2 / B3"; > + break; > + case REVISION_C1: > + 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_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"); > -- > 1.9.1 > > -- > To unsubscribe from this list: send the line "unsubscribe linux-sh" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/