From patchwork Mon Sep 14 10:59:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuninori Morimoto X-Patchwork-Id: 7174531 X-Patchwork-Delegate: geert@linux-m68k.org 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 6DDD5BEEC1 for ; Mon, 14 Sep 2015 10:59:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F04B820664 for ; Mon, 14 Sep 2015 10:59:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 476FB2065B for ; Mon, 14 Sep 2015 10:59:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755451AbbINK7r (ORCPT ); Mon, 14 Sep 2015 06:59:47 -0400 Received: from relmlor4.renesas.com ([210.160.252.174]:43074 "EHLO relmlie3.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753759AbbINK7o (ORCPT ); Mon, 14 Sep 2015 06:59:44 -0400 Received: from unknown (HELO relmlir1.idc.renesas.com) ([10.200.68.151]) by relmlie3.idc.renesas.com with ESMTP; 14 Sep 2015 19:59:42 +0900 Received: from relmlac1.idc.renesas.com (relmlac1.idc.renesas.com [10.200.69.21]) by relmlir1.idc.renesas.com (Postfix) with ESMTP id AF5FF4E6FD; Mon, 14 Sep 2015 19:59:42 +0900 (JST) Received: by relmlac1.idc.renesas.com (Postfix, from userid 0) id 8BFCE8002E; Mon, 14 Sep 2015 19:59:42 +0900 (JST) Received: from relmlac1.idc.renesas.com (localhost [127.0.0.1]) by relmlac1.idc.renesas.com (Postfix) with ESMTP id 82EB48002D; Mon, 14 Sep 2015 19:59:42 +0900 (JST) Received: from relmlii2.idc.renesas.com [10.200.68.66] by relmlac1.idc.renesas.com with ESMTP id VAT04852; Mon, 14 Sep 2015 19:59:42 +0900 X-IronPort-AV: E=Sophos;i="5.17,527,1437404400"; d="scan'";a="195564133" Received: from mail-hk2apc01lp0216.outbound.protection.outlook.com (HELO APC01-HK2-obe.outbound.protection.outlook.com) ([65.55.88.216]) by relmlii2.idc.renesas.com with ESMTP/TLS/AES256-SHA; 14 Sep 2015 19:59:41 +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 SG2PR06MB0602.apcprd06.prod.outlook.com (10.161.10.152) with Microsoft SMTP Server (TLS) id 15.1.268.17; Mon, 14 Sep 2015 10:59:36 +0000 Message-ID: <87d1xljmn7.wl%kuninori.morimoto.gx@renesas.com> From: Kuninori Morimoto To: Stephen Boyd , Simon , Michael Turquette CC: Magnus , Linux-SH , Linux-Kernel , Geert Uytterhoeven , Subject: [PATCH 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") Date: Mon, 14 Sep 2015 10:59:36 +0000 X-Originating-IP: [211.11.155.144] X-ClientProxiedBy: OS1PR01CA0013.jpnprd01.prod.outlook.com (25.161.225.151) To SG2PR06MB0602.apcprd06.prod.outlook.com (25.161.10.152) X-Microsoft-Exchange-Diagnostics: 1; SG2PR06MB0602; 2:TSLxv9yKuciZGJr7mYgDcCe2vjohlPP/68Vhl8ndioV3qbdcfAbZ6TiuPGC/JeurSlHIjVGEB6TIXrM7hM2NMm9akVdbJtlUcwEGHcPSq2OYyjkCocb6Rs48/ohlBGctmbEUAx7YAjN/bsdVr3Ue12glRdOCBt5z2cqlQLrZNdE=; 3:W9fxfAwV09yYxADl4wHnacwUtej08B20irWir/ee+1LoKfLV/L883LXbocemATEFxz3cWkYbUMF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SPke94re5v9FpdTwIYLz/FeeO0nJdOIG0pHeQ9P4mIVy2A7xrhUvjStlqsSwRPgf59sw64fGDB4e1mEVSo/VED5QajrwtwLYPPXFf5A9y1Of5pXZnyxwukHbtpuFjCvyL5z09hRtDssbnfNvTqxbT67UtskxhHgaGlf6UR3MkxJBMGGRuJ9ix4Veb74VyKoBFkG35pgMTycD 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:SG2PR06MB0602; BCL:0; PCL:0; RULEID:; SRVR:SG2PR06MB0602; X-Forefront-PRVS: 0699FCD394 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6009001)(189002)(199003)(189998001)(68736005)(87976001)(50466002)(69596002)(5001830100001)(122386002)(5001770100001)(81156007)(4001540100001)(77096005)(4001350100001)(97736004)(46406003)(5004730100002)(47776003)(36756003)(5007970100001)(64706001)(66066001)(46102003)(50986999)(5001960100002)(101416001)(54356999)(105586002)(42186005)(229853001)(86362001)(5001860100001)(106356001)(23726002)(53416004)(77156002)(575784001)(83506001)(62966003)(40100003)(33646002)(92566002)(19580395003)(19580405001)(2004002); DIR:OUT; SFP:1102; SCL:1; SRVR:SG2PR06MB0602; H:morimoto-PC.renesas.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SG2PR06MB0602; 23:eZUJRJzoTPUEq9HBpYzDCWSnDaMKJFN98rkK65CnN?= =?us-ascii?Q?Cc0nn46Ske8pbRoadz4E8ZIiY5F2cYoehsrdKw/8IsvQTvtQQbwe3ZsDX+mI?= =?us-ascii?Q?CtG6rur6d4EWvB5yDpZinACU5qUwY4ei9qu+N7OGoJb14sEpDtOwPMWQ+0gN?= =?us-ascii?Q?Xrh0WKmVVfwVHH0z29QasVrdAJQDQt+7Zcs1FrzDhFthaiosmhAX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AunLmAMFR34sKOeqPxGhSm6UIfzbDw9EJDPOU7L2?= =?us-ascii?Q?fymvwqAba5ca8oLEZPpkk56OyyNdenQfUpiWzRDWt0lwGjKlDPKeSGEPX/EZ?= =?us-ascii?Q?1wbEM0keFmPA8TuLhc8lVIYsK7n6VYkNTd9TdAZCVOhV4RzItQhO7uWCBl6T?= =?us-ascii?Q?YwlkN6BiVlvGupuSRI=3D?= X-Microsoft-Exchange-Diagnostics: 1; SG2PR06MB0602; 5:4xC7dIPo8dRZzZf9XiaILQNCjtJJd5jvWY8Y9t+66Mec+PK1sZqsOz+49BM7tLJAjZfdCzQurT8sJtRl0I0gR8EjnqkrStX+pfA4PGowuvpivyDL5tA+iWBGH/OYrsLJypXqKxkG/ez+yngNGUBA4w==; 24:P6nl1jJwvTmYokx72qwSdatZQRMfo+kuc14G1ul9/Kh4gkRJbsfxNi0bFhhaNO3fdrb8ERC+MZVYnwvcp/f71RNPmaqfjtNkDncQ6UlPAMU=; 20:Ju9Af7G0JOoULwp1IdC8kT0JGaF2O6QS3KRhALo2NXOdXdlV10/GonqHrL5njlE0Ky7uwvG5ZfeY6NeuvKFQBZsBsHbWRco6pwe+8Yu20rV8A7KqFhCi5RDX/tKpzYG9L4PcytVt7hsmEqB8I6V80h7WNZNq5u1MbUail0xEFco= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: renesas.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2015 10:59:36.5251 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB0602 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. It is useful if it supports runtime clock setting, but it supports fixed clock rate only at this point. Signed-off-by: Kuninori Morimoto --- v2 -> v3 - cs2000_ratio_set() used "val = (u64)rate_out << 20"; .../devicetree/bindings/clock/cs2000-cp.txt | 22 ++ drivers/clk/Kconfig | 6 + drivers/clk/Makefile | 1 + drivers/clk/clk-cs2000-cp.c | 383 +++++++++++++++++++++ 4 files changed, 412 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..a4479f0 --- /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-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-names = "clk_in", "ref_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..163b0cb --- /dev/null +++ b/drivers/clk/clk-cs2000-cp.c @@ -0,0 +1,383 @@ +/* + * 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 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)) + +struct cs2000_priv { + 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(c, addr) i2c_smbus_read_byte_data(c, addr) +static int 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); + + return i2c_smbus_write_byte_data(client, addr, val); +} + +static int cs2000_bset(struct i2c_client *client, u8 addr, u8 mask, u8 val) +{ + s32 data; + + data = cs2000_read(client, addr); + if (data < 0) + return data; + + data &= ~mask; + data |= (val & mask); + + return cs2000_write(client, addr, data); +} + +static int cs2000_ratio_set(struct i2c_client *client, + int ch, u32 rate_in, u32 rate_out) +{ + u64 val; + unsigned int i; + int ret; + + if (CH_SIZE_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 = (u64)rate_out << 20; + do_div(val, rate_in); + for (i = 0; i < 4; i++) { + ret = cs2000_write(client, + Ratio_Add(ch, i), + Ratio_Val(val, i)); + if (ret < 0) + return ret; + } + + return 0; +} + +static int cs2000_ratio_select(struct i2c_client *client, 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(client, DEVICE_CFG1, RSEL_MASK, RSEL(ch)); + if (ret < 0) + return ret; + + ret = cs2000_write(client, DEVICE_CFG2, 0x0); + if (ret < 0) + return ret; + + return 0; +} + +static int cs2000_enable_dev_config(struct i2c_client *client) +{ + int ret; + + ret = cs2000_bset(client, DEVICE_CFG1, ENDEV1, ENDEV1); + if (ret < 0) + return ret; + + ret = cs2000_bset(client, GLOBAL_CFG, ENDEV2, ENDEV2); + if (ret < 0) + return ret; + + 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_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"); + if (IS_ERR(ref_clk)) + return PTR_ERR(ref_clk); + + *rate_in = clk_get_rate(ref_clk); + + if (of_property_read_u32(np, "clock-frequency", rate_out)) + return -EINVAL; + + dev_dbg(dev, "%d -> %d\n", + *rate_in, *rate_out); + + priv->clk_in = clk_in; + priv->ref_clk = ref_clk; + + return 0; +} + +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; + + return cs2000_bset(client, FUNC_CFG1, 0x3 << 3, val << 3); +} + +static int cs2000_wait_pll_lock(struct i2c_client *client) +{ + struct device *dev = &client->dev; + s32 val; + unsigned int i; + + for (i = 0; i < 256; i++) { + val = cs2000_read(client, 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 i2c_client *client) +{ + /* enable both AUX_OUT, CLK_OUT */ + return cs2000_write(client, DEVICE_CTRL, 0x0); +} + +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->ref_clk); + 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; + s32 val = cs2000_read(client, 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) +{ + 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");