From patchwork Sat Dec 31 10:47:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2584 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 006B540F4F for ; Sat, 31 Dec 2022 11:47:55 +0100 (CET) Received: by mail-ed1-f69.google.com with SMTP id x13-20020a05640226cd00b0047ac11c9774sf15701230edd.17 for ; Sat, 31 Dec 2022 02:47:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672483675; cv=pass; d=google.com; s=arc-20160816; b=GctU8Y1nE8CQNL42urSPTe4yeD7g4Rmgnhx1UBYERvg0927qxg01txXQG3aJt9WwNm yh4WCMthqOgKYusw1Eov0wFdZpATWnUizXtHA0f0UUUTLNfcDEVr1o8ts2nul6JC7abl 7VMr+r96eYYY+ek0fgnp8j6rPb9CJ40JD3ZcGK+CGJe05ARbaP9RSunUmLJrBMCtx6xS e2T2wuQEToXh3IpJ7KmMz9a0gd2K+MyRtzCsZ4MzAD0wxl9ZtxPtmz4DpkDjogq5DCbp Krt14C09TVK+gIh9Ydc3Ktqlr9kHG3rZA5aLoVQ9zCdHgzpK28i/qWh+RBfndAtE7rNn UXag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=YipOd7ZXnX68BeUujFd2bcjKtHMT799vhepygxHJsMo=; b=bD0D8O3Ww4wa72UtmKr5OxDT5dp0OxrQvjZx/IUSm/bCWC/h9qzHlWAIv5Y+igfn6o iuFfI+dgU3lBTjW96+KC73gmZwI7UmqVyEXFbn2GP72zoUnpSluDjjhbLcG3cCD8/2NF /nrou/DxmcVJKT6B4FaQRg2V2aG9tkryIyxQnqWv1kHanrWuMbXJYbDsPFuRFBQLiQao Z+BkOYSFJPZRajMKRoq2DtDhSvnG0JdekPCvRQYfYdR1i5FBPWguWPwL1dqvfRLwOyJK NDgFbAdPiHfIw9YqyiZ/Rsfis2jrLy7OQZK4IdrPV9bD8qq1I+jaeMe6RrjImbT0jj8H RBRw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I8RIy1Ok; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=YipOd7ZXnX68BeUujFd2bcjKtHMT799vhepygxHJsMo=; b=XfnGIphq5ZsS08nbGzTQenVe3yHZwJY/ny79CnTQD2DUSQ+K6cXl5Ho9AVUqX9hIHD hrSajcwsBdCzDtseXi0vSRRK3b/STgLQdbuMuqNz8kZU24umUU8drPYmih9tzwwSCWcG SfCibGjnF/Eg+W3uKlKqdwXiPZt7bNernPfDI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YipOd7ZXnX68BeUujFd2bcjKtHMT799vhepygxHJsMo=; b=cZwROl2+/J6c2uVUIqgim5I96WNabsV4dkMdO1noKI0ImmBJXPHKU3qCDu2Fusn1gs Bdpu7F1HFD4xUmvSJ9nCCsT5QKXuQ6oGJBYXBTV/FHPSgA1hOUKMkFGXOSufqcK1loi6 byFAiR4BFIeHz9/NdUkGKEFaitvngDA9TCN19oL6wagrBoa2wUd7ZR1it0Xt4OuPiXJC Up63fJxGnhHoSJjnpdwSWB7/uplbxrDChtPgmVTrjocZ6yFtRY4NTzM/oA7simwTkd7G JQIV6Uu+8BvL+sMZA27TDRVtWIdzllZkq3IysLAldV2k4Vf58W61c2HaDG51+pJxVJCU j8cw== X-Gm-Message-State: AFqh2kqeXN8KBq9aiX1TIb2E6U1OC4z0AyxZcDoaOi9TiASa+WtcTBAP 7ZArwXHN6Qk9pwxOReqOfsdWtV1c X-Google-Smtp-Source: AMrXdXuzwm0+flWcTRNmg/LXnZD6K9zaULjBt31BfcUThPRk6jLrmfgqYTS+34/Z4+IvsbD18UGjFg== X-Received: by 2002:a17:906:c0a:b0:7bb:52b2:4162 with SMTP id s10-20020a1709060c0a00b007bb52b24162mr3244320ejf.608.1672483675440; Sat, 31 Dec 2022 02:47:55 -0800 (PST) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:55c1:b0:7c1:2050:cc5f with SMTP id z1-20020a17090655c100b007c12050cc5fls952470ejp.0.-pod-prod-gmail; Sat, 31 Dec 2022 02:47:54 -0800 (PST) X-Received: by 2002:a17:906:6dd4:b0:7c1:1fcf:266c with SMTP id j20-20020a1709066dd400b007c11fcf266cmr29549151ejt.14.1672483674227; Sat, 31 Dec 2022 02:47:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672483674; cv=none; d=google.com; s=arc-20160816; b=jmQUYjvUoC/ic5Klt5BsUPpiDD6Bw74o2cQh6UdZFSPRXmlO488IWu/AD2il2HhdI0 3rGDq4TUK8JjttAOHDYXLs0Kn9UnfOF58siW3bOdyz+j5MoG/RF5WX0OkWArHwBFepyl JpKjzsW6D/TVdxLVlUHoDHblOf9ClG1kWm6I4mX2WxrWhQRFIpVjn0xUZqw0DVqGmlom uGsd2fO2Mg3Ol6vXpQDxVy+mXp8hHSFLdubkLP+0XXX1xm7QCFzfv82OWU9f0O+OhLWv XfPMNJejm4dQPEAhS/FtvK1rqt/t+p7UyfJECCGYxCcko5pZQZEts8xJW/hDsOWj1vCJ 4ODQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=IcEaICywm+0WRzTCyxMBWQ3K+HlDrikikPFrWzM/DaQ=; b=a370vwcDE3aknq+VZbd8WqlmdbF2h0bJ7MvnF1XvHGrkxOyDVLBBI/Pmrzki2KQvUd VH2DogYaO6K/tuAjhTYt1OvvJQ1rIan/QFDcIFqhvE96BKu+9sG+p1u/vihqkSB2XjBx ftlKbhtyOAhTsnhhkkQzaThRuJ0iY3rhtdk8H19xwoHbs+oAKm+mSv8I+EOMZxc9h9hW mPsoG2V7uDJiR0xbSSNdz8V4h5ZEjoA1nVyOdvbOFh9ntAFH/76Qqcx6rLdrn1oDNt8o 99kpOgpN4K7TyDYPFVpP89KcyMcC/ZMee06if1DqPyXwQv1VjBeiFo0NBIrRRLScgIPq a/pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I8RIy1Ok; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id tb18-20020a1709078b9200b0084cb4d270d1sor352539ejc.22.2022.12.31.02.47.54 for (Google Transport Security); Sat, 31 Dec 2022 02:47:54 -0800 (PST) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; X-Received: by 2002:a17:907:8312:b0:7c1:bb4:ea20 with SMTP id mq18-20020a170907831200b007c10bb4ea20mr28492064ejc.71.1672483674014; Sat, 31 Dec 2022 02:47:54 -0800 (PST) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-80-180-23-57.retail.telecomitalia.it. [80.180.23.57]) by smtp.gmail.com with ESMTPSA id z4-20020a17090655c400b0083ffb81f01esm10765438ejp.136.2022.12.31.02.47.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Dec 2022 02:47:53 -0800 (PST) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: tommaso.merciai@amarulasolutions.com, linux-amarula@amarulasolutions.com, Chen-Yu Tsai , jagan@amarulasolutions.com, angelo@amarulasolutions.com, anthony@amarulasolutions.com, michael@amarulasolutions.com, Dario Binacchi , Abel Vesa , Fabio Estevam , Michael Turquette , NXP Linux Team , Pengutronix Kernel Team , Sascha Hauer , Shawn Guo , Stephen Boyd , linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org Subject: [RFC PATCH 03/11] clk: imx8mn: add gate driver Date: Sat, 31 Dec 2022 11:47:28 +0100 Message-Id: <20221231104736.12635-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20221231104736.12635-1-dario.binacchi@amarulasolutions.com> References: <20221231104736.12635-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I8RIy1Ok; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , The patch adds support for imx8mn gate clocks to be initialized directly from the device tree. Currently all i.MX gate clocks are initialized by legacy code with hardwired parameters. This approach has generated a proliferation of setup functions with unclear names: git grep "#define imx_clk_hw_gate" drivers/clk/imx/clk.h drivers/clk/imx/clk.h:#define imx_clk_hw_gate(name, parent, reg, shift) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate2(name, parent, reg, shift) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate_dis(name, parent, reg, shift) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate_dis_flags(name, parent, reg, shift, flags) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate_flags(name, parent, reg, shift, flags) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate2_flags(name, parent, reg, shift, flags) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate2_shared(name, parent, reg, shift, shared_count) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate2_shared2(name, parent, reg, shift, shared_count) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate3(name, parent, reg, shift) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate3_flags(name, parent, reg, shift, flags) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate4(name, parent, reg, shift) \ drivers/clk/imx/clk.h:#define imx_clk_hw_gate4_flags(name, parent, reg, shift, flags) \ So, let's start with this specific clock driver and hope that other variants can be handled in the future, causing the legacy code to be removed. Signed-off-by: Dario Binacchi --- drivers/clk/imx/Makefile | 1 + drivers/clk/imx/clk-gate.c | 156 +++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 drivers/clk/imx/clk-gate.c diff --git a/drivers/clk/imx/Makefile b/drivers/clk/imx/Makefile index e8aacb0ee6ac..72e1f08d49dc 100644 --- a/drivers/clk/imx/Makefile +++ b/drivers/clk/imx/Makefile @@ -11,6 +11,7 @@ mxc-clk-objs += clk-divider-gate.o mxc-clk-objs += clk-fixup-div.o mxc-clk-objs += clk-fixup-mux.o mxc-clk-objs += clk-frac-pll.o +mxc-clk-objs += clk-gate.o mxc-clk-objs += clk-gate2.o mxc-clk-objs += clk-gate-93.o mxc-clk-objs += clk-gate-exclusive.o diff --git a/drivers/clk/imx/clk-gate.c b/drivers/clk/imx/clk-gate.c new file mode 100644 index 000000000000..841ff9a37f30 --- /dev/null +++ b/drivers/clk/imx/clk-gate.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Amarula Solutions + * + * Dario Binacchi + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "clk.h" + +#undef pr_fmt +#define pr_fmt(fmt) "%s: " fmt, __func__ + +#define to_clk_imx_gate(_hw) container_of(_hw, struct clk_imx_gate, hw) + +struct clk_imx_gate { + struct clk_hw hw; + struct imx_clk_reg reg; + u32 enable_mask; +}; + +static int imx_clk_gate_enable(struct clk_hw *hw) +{ + struct clk_imx_gate *gate = to_clk_imx_gate(hw); + struct imx_clk_reg *reg = &gate->reg; + + return regmap_update_bits(reg->regmap, reg->offset, gate->enable_mask, + gate->enable_mask); +} + +static void imx_clk_gate_disable(struct clk_hw *hw) +{ + struct clk_imx_gate *gate = to_clk_imx_gate(hw); + struct imx_clk_reg *reg = &gate->reg; + + regmap_update_bits(reg->regmap, reg->offset, gate->enable_mask, 0); +} + +static int imx_clk_gate_is_enabled(struct clk_hw *hw) +{ + struct clk_imx_gate *gate = to_clk_imx_gate(hw); + struct imx_clk_reg *reg = &gate->reg; + unsigned int val; + + if (regmap_read(reg->regmap, reg->offset, &val)) + return -EIO; + + return !!(val & gate->enable_mask); +} + +const struct clk_ops imx_clk_gate_ops = { + .enable = &imx_clk_gate_enable, + .disable = &imx_clk_gate_disable, + .is_enabled = &imx_clk_gate_is_enabled, +}; + +static void imx_clk_hw_unregister_gate(struct clk_hw *hw) +{ + struct clk_imx_gate *gate = to_clk_imx_gate(hw); + + clk_hw_unregister(hw); + kfree(gate); +} + +static struct clk_hw *imx_clk_hw_register_gate(struct device_node *node, + const char *name, + unsigned long flags, + struct imx_clk_reg *reg, + u8 enable_bit) +{ + struct clk_parent_data pdata = { .index = 0 }; + struct clk_init_data init = { NULL }; + struct clk_imx_gate *gate; + struct clk_hw *hw; + int ret; + + gate = kzalloc(sizeof(*gate), GFP_KERNEL); + if (!gate) + return ERR_PTR(-ENOMEM); + + init.name = name; + init.flags = flags; + init.ops = &imx_clk_gate_ops; + init.parent_data = &pdata; + init.num_parents = 1; + + memcpy(&gate->reg, reg, sizeof(*reg)); + gate->enable_mask = BIT(enable_bit); + gate->hw.init = &init; + + hw = &gate->hw; + ret = of_clk_hw_register(node, hw); + if (ret) { + kfree(gate); + return ERR_PTR(ret); + } + + return hw; +} + +/** + * of_imx_gate_clk_setup() - Setup function for imx gate clock + * @node: device node for the clock + */ +static void __init of_imx_gate_clk_setup(struct device_node *node) +{ + struct clk_hw *hw; + struct imx_clk_reg reg; + const char *name = node->name; + u8 enable_bit = 0; + u32 val; + + reg.regmap = syscon_regmap_lookup_by_phandle(node, "fsl,anatop"); + if (IS_ERR(reg.regmap)) { + pr_err("missing regmap for %pOFn\n", node); + return; + } + + if (of_property_read_u32_index(node, "fsl,anatop", 1, &val)) { + pr_err("missing register offset for %pOFn\n", node); + return; + } + + reg.offset = val; + + if (!of_property_read_u32(node, "fsl,bit-shift", &val)) + enable_bit = val; + + if (of_clk_get_parent_count(node) != 1) { + pr_err("%pOFn must have 1 parent clock\n", node); + return; + } + + of_property_read_string(node, "clock-output-names", &name); + + hw = imx_clk_hw_register_gate(node, name, 0, ®, enable_bit); + if (IS_ERR(hw)) + return; + + if (of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw)) { + imx_clk_hw_unregister_gate(hw); + return; + } + + pr_debug("name: %s, offset: 0x%x, enable-bit: %d\n", name, reg.offset, + enable_bit); +} +CLK_OF_DECLARE(fsl_imx8mn_gate_clk, "fsl,imx8mn-gate-clock", + of_imx_gate_clk_setup);