From patchwork Tue Nov 13 16:20:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Vesa X-Patchwork-Id: 10681095 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B9A0B14D6 for ; Tue, 13 Nov 2018 16:21:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95AE52AF07 for ; Tue, 13 Nov 2018 16:21:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89E802AF38; Tue, 13 Nov 2018 16:21:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EA9D22AF07 for ; Tue, 13 Nov 2018 16:21:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:In-Reply-To:References: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Q+AxFtWsaek/7kXQpn6vxlpUYM9WRIVxXwaDf4EBKE4=; b=OoM1ZBi20zz6B/ XMFH685rf7rClxgoiQ7ct0q90kcQ1xi6Kdbv2Aqva5DXjzp76JN9zFQzDTz/aiT4F7xLXz4CQdinx fbtPfJ+qMbsNI2tyj0H4BJ5KUBFV0PnoJ5AWFXO7mtCDeZBIcUiBHRcL5/x1HkZdZBai0+s1vE+ZC UlnMp9fczMn1836WKOQWDoqQA4TfG7I3npwi/7zzFbxvKGLIHbSzLJdkaVy3fGJldSY99DDoSYq4P dmAsSyAxwXJiGcRlU+yxW4I90IOPadgoWNZzQqdruBVhVcxwIHTEZ+ilh+oC9LQYN65y+W5jUmtYj JfqjWymUReEhs2xceYvA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gMbR9-0000q9-AJ; Tue, 13 Nov 2018 16:21:11 +0000 Received: from mail-db3eur04on0607.outbound.protection.outlook.com ([2a01:111:f400:fe0c::607] helo=EUR04-DB3-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gMbQa-0008Ja-J1 for linux-arm-kernel@lists.infradead.org; Tue, 13 Nov 2018 16:20:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HwnztoQH1KrtAgE2pm8PGQog3cKpXNUfx9Vua0NEWkk=; b=QmdxtfiLRn0UVul3wCMTikwObOTd0ocy9B1RK70KOlWoueTRgaW8WJGzZ6dMGbcHIaZTFh3Zqe7aoF/rX/YrQbnaDQO30oVl483BYuTPB005Ffqt571Un6Ka9F2ubncA/0/ZJeHwgOChQ62/7r9DrWtUsZSQVQBO3sLdNbSGpgY= Received: from AM6PR0402MB3654.eurprd04.prod.outlook.com (52.133.28.145) by AM6PR0402MB3750.eurprd04.prod.outlook.com (52.133.29.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1294.26; Tue, 13 Nov 2018 16:20:00 +0000 Received: from AM6PR0402MB3654.eurprd04.prod.outlook.com ([fe80::5195:5e4c:be83:408b]) by AM6PR0402MB3654.eurprd04.prod.outlook.com ([fe80::5195:5e4c:be83:408b%4]) with mapi id 15.20.1294.045; Tue, 13 Nov 2018 16:20:00 +0000 From: Abel Vesa To: Sascha Hauer , Lucas Stach , "A.s. Dong" , Stephen Boyd Subject: [PATCH v13 4/5] clk: imx: Add imx composite clock Thread-Topic: [PATCH v13 4/5] clk: imx: Add imx composite clock Thread-Index: AQHUe2y5M2CYbJVqGEqsp036cvwO6A== Date: Tue, 13 Nov 2018 16:20:00 +0000 Message-ID: <1542125975-8448-5-git-send-email-abel.vesa@nxp.com> References: <1542125975-8448-1-git-send-email-abel.vesa@nxp.com> In-Reply-To: <1542125975-8448-1-git-send-email-abel.vesa@nxp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: AM6PR0202CA0065.eurprd02.prod.outlook.com (2603:10a6:20b:3a::42) To AM6PR0402MB3654.eurprd04.prod.outlook.com (2603:10a6:209:19::17) x-originating-ip: [95.76.156.53] authentication-results: spf=none (sender IP is ) smtp.mailfrom=abel.vesa@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; AM6PR0402MB3750; 6:kcqPmMllbldbwTjN6A4QFzntHarIRNY04G9eBq2epLfFvLRK1KecUiIghH1C7LI/XNPvAZhcGd5N84ZjM+1c90HHxOQLQpU/eHY/law36Kw0yp+FEfSwlj8KHtki384JGMQgz8cMhwZcvmsOj4bJUTJXbbbFbVWUVrvRFy1PUVOmWhiTJeBBsL7y2MRbvUEsk2dc2/MJHaegevD1/Da23P1L5rqolcg2V0S3h7cdMVLe29h/vAUFRmoEkrH2lVmak0edV9t222QDHf/iDPFoPpGYuK4fOotIPk7hDMkaO/1GH99KjwGjSS+WVVidx8t5kpOXf++2A/7UGsACCXUimtE7hgSFL15gQodIkzsXlgo7ZjyOUkzKqcd35BT8kKkOIJnvShSL853Q1+l2AAj8bhPLj1e4UFDAroLRqCAc607r0MolJyqHep9sGZSb+sUQYckJ+eT3rE79SEY6BThh2w==; 5:9Ud/VHmtaPHzYRT0semawsjLz2cP8XHDK2BgEg1mJTiMsPxkEH6DcYhG4Zeawv/THh0vXeeCvS2Aqr7IvU/+EFXl0xfNFkLZ0cY3K2I+0hNf/gy3M4lgHArlkAVN4fvFlea2OLnPXc5+8+PgC/KUVL7zu/bYu/uLFX0tzCYg5ZA=; 7:UQRzZ0GbF/vsJZzpxJznRXLNrRr0/0EOav8/zFnr/R/iL/qTgu4Ck7Q9JBjEp13oZpX2ziRR11blw18GityWMt3yKJJ+29bZeHV1PtMSApjII++2OzQaWQiCvTGVOw2bx3cJzQR77T0zXCQIBxVLAg== x-ms-office365-filtering-correlation-id: 3f146422-ad7a-4fc1-2c19-08d64983dbf8 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390060)(7020095)(4652040)(8989299)(5600074)(711020)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020); SRVR:AM6PR0402MB3750; x-ms-traffictypediagnostic: AM6PR0402MB3750: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(185117386973197); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(3231406)(944501410)(52105112)(3002001)(10201501046)(6055026)(148016)(149066)(150057)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123558120)(20161123564045)(201708071742011)(7699051)(76991095); SRVR:AM6PR0402MB3750; BCL:0; PCL:0; RULEID:; SRVR:AM6PR0402MB3750; x-forefront-prvs: 085551F5A8 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(366004)(376002)(39860400002)(346002)(396003)(136003)(199004)(189003)(105586002)(2906002)(44832011)(7416002)(386003)(6116002)(106356001)(26005)(3846002)(76176011)(446003)(5660300001)(11346002)(316002)(4326008)(478600001)(68736007)(14454004)(186003)(7736002)(110136005)(54906003)(305945005)(52116002)(2616005)(476003)(102836004)(6506007)(486006)(14444005)(71190400001)(71200400001)(53936002)(86362001)(2900100001)(6486002)(575784001)(99286004)(25786009)(6436002)(6512007)(8936002)(256004)(97736004)(81166006)(36756003)(66066001)(81156014)(8676002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM6PR0402MB3750; H:AM6PR0402MB3654.eurprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: U+9FOFcMJWnVrfs4KMSm3bSom+dVQIXUHPkxiA1M3fkNW1NG5ZacHesPjtJy9ilLJpVeHVekxytQWEg2FTGvUJ9h+iQwLZRw8e207Th8IVeMYDB7aw1obozhtVpYZtAsAvNvGkP05/D5cpIsAGV9JVvcc3xDDWiSh3ocVCCmPB3ddoGCt6dgQisXp/OgHmuHCWH7eO6DR8UFhpbxiGOWl6F8rFnFPxxlTP8J9hAZwiAynm5VqbU0GOlqOWtoQf2lHC5MV2DRY96MR0DGuMK3/AeyT6MAfnFXuVf8L4d9uBEA2J2c+awYfolK0/sXnhtKpAVvb1LgEGhOaxaG9jc0htKKOEsNQ2XxJwS8C7tlgGU= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f146422-ad7a-4fc1-2c19-08d64983dbf8 X-MS-Exchange-CrossTenant-originalarrivaltime: 13 Nov 2018 16:20:00.4165 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR0402MB3750 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181113_082036_631872_59931134 X-CRM114-Status: GOOD ( 19.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Rob Herring , Abel Vesa , "devicetree@vger.kernel.org" , Michael Turquette , "linux-kernel@vger.kernel.org" , Abel Vesa , dl-linux-imx , Fabio Estevam , Shawn Guo , "linux-clk@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Since a lot of clocks on imx8m are formed by a mux, gate, predivider and divider, the idea here is to combine all of those into one composite clock, but we need to deal with both predivider and divider at the same time and therefore we add the imx8m_clk_composite_divider_ops and register the composite clock with those. Signed-off-by: Abel Vesa Suggested-by: Sascha Hauer Reviewed-by: Sascha Hauer --- drivers/clk/imx/Makefile | 1 + drivers/clk/imx/clk-composite-8m.c | 178 +++++++++++++++++++++++++++++++++++++ drivers/clk/imx/clk.h | 16 ++++ 3 files changed, 195 insertions(+) create mode 100644 drivers/clk/imx/clk-composite-8m.c diff --git a/drivers/clk/imx/Makefile b/drivers/clk/imx/Makefile index b87513c..237444b 100644 --- a/drivers/clk/imx/Makefile +++ b/drivers/clk/imx/Makefile @@ -3,6 +3,7 @@ obj-y += \ clk.o \ clk-busy.o \ + clk-composite-8m.o \ clk-cpu.o \ clk-fixup-div.o \ clk-fixup-mux.o \ diff --git a/drivers/clk/imx/clk-composite-8m.c b/drivers/clk/imx/clk-composite-8m.c new file mode 100644 index 0000000..bcd31d8 --- /dev/null +++ b/drivers/clk/imx/clk-composite-8m.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2018 NXP + */ + +#include +#include +#include + +#include "clk.h" + +#define PCG_PREDIV_SHIFT 16 +#define PCG_PREDIV_WIDTH 3 +#define PCG_PREDIV_MAX 8 + +#define PCG_DIV_SHIFT 0 +#define PCG_DIV_WIDTH 6 +#define PCG_DIV_MAX 64 + +#define PCG_PCS_SHIFT 24 +#define PCG_PCS_MASK 0x7 + +#define PCG_CGC_SHIFT 28 + +static unsigned long imx8m_clk_composite_divider_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct clk_divider *divider = to_clk_divider(hw); + unsigned long prediv_rate; + unsigned int prediv_value; + unsigned int div_value; + + prediv_value = readl(divider->reg) >> divider->shift; + prediv_value &= clk_div_mask(divider->width); + + prediv_rate = divider_recalc_rate(hw, parent_rate, prediv_value, + NULL, divider->flags, + divider->width); + + div_value = readl(divider->reg) >> PCG_DIV_SHIFT; + div_value &= clk_div_mask(PCG_DIV_WIDTH); + + return divider_recalc_rate(hw, prediv_rate, div_value, NULL, + divider->flags, PCG_DIV_WIDTH); +} + +static int imx8m_clk_composite_compute_dividers(unsigned long rate, + unsigned long parent_rate, + int *prediv, int *postdiv) +{ + int div1, div2; + int error = INT_MAX; + int ret = -EINVAL; + + *prediv = 1; + *postdiv = 1; + + for (div1 = 1; div1 <= PCG_PREDIV_MAX; div1++) { + for (div2 = 1; div2 <= PCG_DIV_MAX; div2++) { + int new_error = ((parent_rate / div1) / div2) - rate; + + if (abs(new_error) < abs(error)) { + *prediv = div1; + *postdiv = div2; + error = new_error; + ret = 0; + } + } + } + return ret; +} + +static long imx8m_clk_composite_divider_round_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long *prate) +{ + int prediv_value; + int div_value; + + imx8m_clk_composite_compute_dividers(rate, *prate, + &prediv_value, &div_value); + rate = DIV_ROUND_UP(*prate, prediv_value); + + return DIV_ROUND_UP(rate, div_value); + +} + +static int imx8m_clk_composite_divider_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct clk_divider *divider = to_clk_divider(hw); + unsigned long flags = 0; + int prediv_value; + int div_value; + int ret = 0; + u32 val; + + ret = imx8m_clk_composite_compute_dividers(rate, parent_rate, + &prediv_value, &div_value); + if (ret) + return -EINVAL; + + spin_lock_irqsave(divider->lock, flags); + + val = readl(divider->reg); + val &= ~((clk_div_mask(divider->width) << divider->shift) | + (clk_div_mask(PCG_DIV_WIDTH) << PCG_DIV_SHIFT)); + + val |= (u32)(prediv_value - 1) << divider->shift; + val |= (u32)(div_value - 1) << PCG_DIV_SHIFT; + writel(val, divider->reg); + + spin_unlock_irqrestore(divider->lock, flags); + + return ret; +} + +static const struct clk_ops imx8m_clk_composite_divider_ops = { + .recalc_rate = imx8m_clk_composite_divider_recalc_rate, + .round_rate = imx8m_clk_composite_divider_round_rate, + .set_rate = imx8m_clk_composite_divider_set_rate, +}; + +struct clk *imx8m_clk_composite_flags(const char *name, + const char **parent_names, + int num_parents, void __iomem *reg, + unsigned long flags) +{ + struct clk_hw *hw = NULL, *mux_hw = NULL; + struct clk_hw *div_hw = NULL, *gate_hw = NULL; + struct clk_divider *div = NULL; + struct clk_gate *gate = NULL; + struct clk_mux *mux = NULL; + + mux = kzalloc(sizeof(*mux), GFP_KERNEL); + if (!mux) + goto fail; + + mux_hw = &mux->hw; + mux->reg = reg; + mux->shift = PCG_PCS_SHIFT; + mux->mask = PCG_PCS_MASK; + + div = kzalloc(sizeof(*div), GFP_KERNEL); + if (!div) + goto fail; + + div_hw = &div->hw; + div->reg = reg; + div->shift = PCG_PREDIV_SHIFT; + div->width = PCG_PREDIV_WIDTH; + div->lock = &imx_ccm_lock; + div->flags = CLK_DIVIDER_ROUND_CLOSEST; + + gate = kzalloc(sizeof(*gate), GFP_KERNEL); + if (!gate) + goto fail; + + gate_hw = &gate->hw; + gate->reg = reg; + gate->bit_idx = PCG_CGC_SHIFT; + + hw = clk_hw_register_composite(NULL, name, parent_names, num_parents, + mux_hw, &clk_mux_ops, div_hw, + &imx8m_clk_composite_divider_ops, + gate_hw, &clk_gate_ops, flags); + if (IS_ERR(hw)) + goto fail; + + return hw->clk; + +fail: + kfree(gate); + kfree(div); + kfree(mux); + return ERR_CAST(hw); +} diff --git a/drivers/clk/imx/clk.h b/drivers/clk/imx/clk.h index 864cd8a..cb3e92c 100644 --- a/drivers/clk/imx/clk.h +++ b/drivers/clk/imx/clk.h @@ -239,4 +239,20 @@ struct clk *imx_clk_cpu(const char *name, const char *parent_name, struct clk *div, struct clk *mux, struct clk *pll, struct clk *step); +struct clk *imx8m_clk_composite_flags(const char *name, + const char **parent_names, + int num_parents, void __iomem *reg, + unsigned long flags); + +#define __imx8m_clk_composite(name, parent_names, reg, flags) \ + imx8m_clk_composite_flags(name, parent_names, \ + ARRAY_SIZE(parent_names), reg, \ + flags | CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE) + +#define imx8m_clk_composite(name, parent_names, reg) \ + __imx8m_clk_composite(name, parent_names, reg, 0) + +#define imx8m_clk_composite_critical(name, parent_names, reg) \ + __imx8m_clk_composite(name, parent_names, reg, CLK_IS_CRITICAL) + #endif