From patchwork Mon Sep 14 02:55:03 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuninori Morimoto X-Patchwork-Id: 7172491 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id F2514BEEC1 for ; Mon, 14 Sep 2015 02:55:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 158FB20520 for ; Mon, 14 Sep 2015 02:55:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7B0892040F for ; Mon, 14 Sep 2015 02:55:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753868AbbINCzM (ORCPT ); Sun, 13 Sep 2015 22:55:12 -0400 Received: from relmlor4.renesas.com ([210.160.252.174]:35511 "EHLO relmlie3.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753833AbbINCzJ (ORCPT ); Sun, 13 Sep 2015 22:55:09 -0400 Received: from unknown (HELO relmlir4.idc.renesas.com) ([10.200.68.154]) by relmlie3.idc.renesas.com with ESMTP; 14 Sep 2015 11:55:07 +0900 Received: from relmlac2.idc.renesas.com (relmlac2.idc.renesas.com [10.200.69.22]) by relmlir4.idc.renesas.com (Postfix) with ESMTP id B49694A488; Mon, 14 Sep 2015 11:55:07 +0900 (JST) Received: by relmlac2.idc.renesas.com (Postfix, from userid 0) id 97ADC2806D; Mon, 14 Sep 2015 11:55:07 +0900 (JST) Received: from relmlac2.idc.renesas.com (localhost [127.0.0.1]) by relmlac2.idc.renesas.com (Postfix) with ESMTP id 931492806F; Mon, 14 Sep 2015 11:55:07 +0900 (JST) Received: from relmlii1.idc.renesas.com [10.200.68.65] by relmlac2.idc.renesas.com with ESMTP id MAL06793; Mon, 14 Sep 2015 11:55:07 +0900 X-IronPort-AV: E=Sophos;i="5.17,525,1437404400"; d="scan'";a="194327846" Received: from mail-sg2apc01lp0240.outbound.protection.outlook.com (HELO APC01-SG2-obe.outbound.protection.outlook.com) ([65.55.88.240]) by relmlii1.idc.renesas.com with ESMTP/TLS/AES256-SHA; 14 Sep 2015 11:55:06 +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 SG2PR06MB0601.apcprd06.prod.outlook.com (10.161.10.151) with Microsoft SMTP Server (TLS) id 15.1.268.17; Mon, 14 Sep 2015 02:55:03 +0000 Message-ID: <87twqxk92u.wl%kuninori.morimoto.gx@renesas.com> From: Kuninori Morimoto Subject: [PATCH] clk: add CS2000 Fractional-N driver User-Agent: Wanderlust/2.15.9 Emacs/24.3 Mule/6.0 To: Simon , Michael Turquette , Stephen Boyd CC: , , Magnus , MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Date: Mon, 14 Sep 2015 02:55:03 +0000 X-Originating-IP: [211.11.155.144] X-ClientProxiedBy: OS2PR01CA0004.jpnprd01.prod.outlook.com (25.161.74.142) To SG2PR06MB0601.apcprd06.prod.outlook.com (25.161.10.151) X-Microsoft-Exchange-Diagnostics: 1; SG2PR06MB0601; 2:DJEkshd18D9QhpjGW4Wv4d7zHyxNa/US+9JNC4GaK5iTX3sKZ6dCRusp/7fYho2r4Xpk7odY4gvrEpI8rYf64mL399rMXBeagyhnGxjZbpQxkWNhrvHqgkAAEaUCqWvvHXRyN42GODEeWPSL3xCI9zW6uKxaAmYquqCovqoYar8=; 3:Sz8Yqrgesek50dnHkylOA8Mq+Sbtclqt/ABONpTdeJInHXbvYL3XNPUn/TOcDU/rGg/uLsBNC6U1AmkQmfqu24SXD2Z4uTe5ODSjsBb/1Cpy9Zy2TnOJH5iMPcyfjrFfTS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Uyr/UtXZxlMUROi6vsYyt3Bet7s0TPQBkBGNkBPH6QLd5BdSJPtDgtenlRKrCJORnGmVzU6MBZsFyxD9MQxjcvzml9Y5vGn0lsYMeL9oEwA3JYgoZNB+h0tqDXkH7mEnJaSjXxWBQaYxDL9jPuU38gWTIKutUiUWxRhdPW4mHwZ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5005006)(8121501046)(3002001); SRVR:SG2PR06MB0601; BCL:0; PCL:0; RULEID:; SRVR:SG2PR06MB0601; X-Forefront-PRVS: 0699FCD394 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6009001)(199003)(189002)(83506001)(5004730100002)(50986999)(87976001)(5007970100001)(68736005)(77096005)(69596002)(229853001)(42186005)(50466002)(53416004)(5001830100001)(5001770100001)(4001540100001)(81156007)(97736004)(105586002)(33646002)(86362001)(106356001)(4001350100001)(46406003)(77156002)(62966003)(23726002)(189998001)(40100003)(122386002)(36756003)(101416001)(5001960100002)(54356999)(5001860100001)(5001920100001)(46102003)(19580405001)(64706001)(47776003)(92566002)(66066001)(19580395003)(2004002); DIR:OUT; SFP:1102; SCL:1; SRVR:SG2PR06MB0601; H:morimoto-PC.renesas.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SG2PR06MB0601; 23:71pQV5rMreKDMW68B8SVpM7C77uBDiY7BJlwQtgAA?= =?us-ascii?Q?V7uLLCnxVquFR2dV8xTM7uipZi8dX3CFV+3mi6/lOCwprukTJvCMDRx0hHnY?= =?us-ascii?Q?BTTKgIWfIHiAqTBNUuwjjT0PkIcxJEwqXIykd2a5C5Bz+JsJeopgnlYCt8lT?= =?us-ascii?Q?ch95+3jyUzKZQXc4QKwxK96EClqpbbZb3e5/UonOnVilI38iSG9KeHv9WY3B?= =?us-ascii?Q?FISwwjTm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JE2l6eZK/ELtdgAzEie?= =?us-ascii?Q?5vBBfMigeLxnDmkAzZO9vIyulBDwiuaQSH31FKG0uCMqlZyJrMqrm3mFWOoJ?= =?us-ascii?Q?14RwYr5gxILXDEMXwr0cZ1bVoWqohgjBKecx7CnR9R/Um9MMr3Yg10mWfel4?= =?us-ascii?Q?zckberMfv3EtNUVaVY=3D?= X-Microsoft-Exchange-Diagnostics: 1; SG2PR06MB0601; 5:ys87fo0kObtbVdCo8Ok+r6xpToJuE1I1LHUffeQgCG1mu4KbkBmyxnKoTkFXyJqdMDbPf73NWkEIXDtsdjo/0n8mZU9yGzlbcPCgPSgUlzLdk9MixHepU3dT0wVIox6pFZCGqw3nYEaIN6+8+S4KWw==; 24:sR9p/3tc4kgFYUzkmaq4wZvUlTQa6lGOwaHCXlts8GnMmUtZjHC4UvDqYPj42+SmlDNyCa/O656JdnrbbyZ8rnUEt9OvAEk6Ng84XLpN+3M=; 20:dxQdrMkJA0x4FXOjv0MUl53kpIZCQO4hBnaw0ERM6xb6wbe1ghKhaGMfmf9R2AVNx0SdDAvN20GDKPMGafLZvkSHnnu6ztXIygutIBeErIZWVom/hhgvsBt2DAVsVfuMO2pVlGJ6R4DC7VbPpPBYVfUI0YH/aBIw/+7fpP6Sik8= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: renesas.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2015 02:55:03.8797 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB0601 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=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: Kuninori Morimoto This patch adds CS2000 Fractional-N driver as clock provider. It is useful if it supports runtime clock setting, but it supports fixed clock rate only at this point. Signed-off-by: Kuninori Morimoto --- .../devicetree/bindings/clock/cs2000-cp.txt | 20 ++ drivers/clk/Kconfig | 6 + drivers/clk/Makefile | 1 + drivers/clk/clk-cs2000-cp.c | 379 +++++++++++++++++++++ 4 files changed, 406 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..e79cf10 --- /dev/null +++ b/Documentation/devicetree/bindings/clock/cs2000-cp.txt @@ -0,0 +1,20 @@ +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-frequency: clock frequency of CLK_OUT + +Example: + +&i2c2 { + ... + cs2000: clk_multiplier@0x4f { + compatible = "cirrus,cs2000-cp"; + reg = <0x4f>; + clocks = <&rcar_sound 0>, <&x12_clk>; + clock-frequency = <24576000>; /* 1/1 divide */ + }; +}; diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index 42f7120..5933e5f 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..13a1dfa --- /dev/null +++ b/drivers/clk/clk-cs2000-cp.c @@ -0,0 +1,379 @@ +/* + * 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 CLK_MAIN 0 /* CLK_IN on datasheet */ +#define CLK_IN 1 /* REF_CLK on datasheet */ + +#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 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_ERR(ch) ((ch < 0) || (ch >= CH_MAX)) + +struct cs2000_priv { + struct clk *clk_main; + struct clk *clk_in; + 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(c, addr) i2c_smbus_read_byte_data(c, addr) +static void cs2000_write(struct i2c_client *client, u8 addr, u8 val) +{ + struct device *dev = &client->dev; + + dev_dbg(dev, "0x%02x : 0x%02x\n", addr, val); + + i2c_smbus_write_byte_data(client, addr, val); +} + +static void cs2000_bset(struct i2c_client *client, u8 addr, u8 mask, u8 val) +{ + u32 data; + + data = cs2000_read(client, addr); + + data &= ~mask; + data |= (val & mask); + + cs2000_write(client, addr, data); +} + +static int cs2000_ratio_set(struct i2c_client *client, + int ch, u32 rate_in, u64 rate_out) +{ + u32 val; + int i; + + if (CH_ERR(ch)) + return -EINVAL; + + /* + * ratio = rate_out / rate_in * 2^20 + * + * To avoid over flow, rate_out is u64 + * The result should be u32 + */ + + val = rate_out * (1 << 20) / rate_in; + for (i = 0; i < 4; i++) + cs2000_write(client, + Ratio_Add(ch, i), + Ratio_Val(val, i)); + + return 0; +} + +static int cs2000_ratio_select(struct i2c_client *client, int ch) +{ + if (CH_ERR(ch)) + return -EINVAL; + + /* + * FIXME + * + * this driver supports static ratio mode only + * at this point + */ + cs2000_bset(client, DEVICE_CFG1, RSEL_MASK, RSEL(ch)); + cs2000_write(client, DEVICE_CFG2, 0x0); + + return 0; +} + +static int cs2000_enable_dev_config(struct i2c_client *client) +{ + cs2000_bset(client, DEVICE_CFG1, ENDEV1, ENDEV1); + cs2000_bset(client, GLOBAL_CFG, ENDEV2, ENDEV2); + + return 0; +} + +static int cs2000_clk_get(struct i2c_client *client, + u32 *rate_in, u32 *rate_out) +{ + struct cs2000_priv *priv = i2c_get_clientdata(client); + struct device *dev = &client->dev; + struct device_node *np = dev->of_node; + struct clk *clk_main, *clk_in; + + clk_main = of_clk_get(np, CLK_MAIN); + /* not yet provided */ + if (IS_ERR(clk_main)) + return -EPROBE_DEFER; + + clk_in = of_clk_get(np, CLK_IN); + if (IS_ERR(clk_in)) + return PTR_ERR(clk_in); + + *rate_in = clk_get_rate(clk_in); + + if (of_property_read_u32(np, "clock-frequency", rate_out)) + return -EINVAL; + + dev_dbg(dev, "%d -> %d\n", + *rate_in, *rate_out); + + priv->clk_main = clk_main; + priv->clk_in = clk_in; + + return 0; +} + +static void cs2000_clk_put(struct i2c_client *client) +{ + struct cs2000_priv *priv = i2c_get_clientdata(client); + + clk_put(priv->clk_main); + clk_put(priv->clk_in); + + priv->clk_main = NULL; + priv->clk_in = NULL; +} + +static int cs2000_clk_in_bound_rate(struct i2c_client *client, + u32 rate_in) +{ + u32 val; + + if ((32000000 <= rate_in) && + (56000000 > rate_in)) + val = 0x0; + else if ((16000000 <= rate_in) && + (28000000 > rate_in)) + val = 0x1; + else if ((8000000 <= rate_in) && + (14000000 > rate_in)) + val = 0x2; + else + return -EINVAL; + + cs2000_bset(client, FUNC_CFG1, 0x3 << 3, val << 3); + + return 0; +} + +static int cs2000_wait_pll_lock(struct i2c_client *client) +{ + struct device *dev = &client->dev; + u32 val; + int i; + + for (i = 0; i < 256; i++) { + val = cs2000_read(client, DEVICE_CTRL); + if (!(val & PLL_UNLOCK)) + return 0; + udelay(1); + } + + dev_err(dev, "pll lock failed\n"); + + return -EIO; +} + +static int cs2000_clk_out_enable(struct i2c_client *client) +{ + /* enable both AUX_OUT, CLK_OUT */ + cs2000_write(client, DEVICE_CTRL, 0x0); + + return 0; +} + +static int cs2000_clk_register(struct i2c_client *client) +{ + struct cs2000_priv *priv = i2c_get_clientdata(client); + struct device *dev = &client->dev; + struct device_node *np = dev->of_node; + struct clk *clk; + const char *parent_clk_name = __clk_get_name(priv->clk_in); + u32 rate; + + if (of_property_read_u32(np, "clock-frequency", &rate)) + return -EINVAL; + + /* + * FIXME + * + * register this driver as fixed rate clock + * at this point + */ + clk = clk_register_fixed_rate(dev, "clk_out", parent_clk_name, + (parent_clk_name) ? 0 : CLK_IS_ROOT, + rate); + if (IS_ERR(clk)) + return PTR_ERR(clk); + + priv->clk_out = clk; + of_clk_add_provider(np, of_clk_src_simple_get, clk); + + return 0; +} + +static void cs2000_clk_unregister(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_put(priv->clk_out); + + priv->clk_out = NULL; +} + +static int cs2000_version_print(struct i2c_client *client) +{ + struct device *dev = &client->dev; + u32 val = cs2000_read(client, DEVICE_ID); + char *revision; + + /* 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) +{ + cs2000_clk_put(client); + + cs2000_clk_unregister(client); + + 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; + u32 rate_in = 0; + u32 rate_out = 0; + int ret; + int ch = 0; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + i2c_set_clientdata(client, priv); + + ret = cs2000_clk_get(client, &rate_in, &rate_out); + if (ret < 0) + return ret; + + ret = cs2000_enable_dev_config(client); + if (ret < 0) + goto probe_err; + + ret = cs2000_clk_in_bound_rate(client, rate_in); + if (ret < 0) + goto probe_err; + + ret = cs2000_ratio_set(client, ch, rate_in, rate_out); + if (ret < 0) + goto probe_err; + + ret = cs2000_ratio_select(client, ch); + if (ret < 0) + goto probe_err; + + ret = cs2000_wait_pll_lock(client); + if (ret < 0) + goto probe_err; + + ret = cs2000_clk_out_enable(client); + if (ret < 0) + goto probe_err; + + ret = cs2000_clk_register(client); + if (ret < 0) + goto probe_err; + + ret = cs2000_version_print(client); + 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", + .owner = THIS_MODULE, + .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");