From patchwork Sun Jan 1 17:57:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2596 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 46FF940F49 for ; Sun, 1 Jan 2023 18:57:53 +0100 (CET) Received: by mail-ed1-f69.google.com with SMTP id w18-20020a05640234d200b0048cc3aa4993sf1436689edc.7 for ; Sun, 01 Jan 2023 09:57:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672595873; cv=pass; d=google.com; s=arc-20160816; b=cyQEOnVbaEbx4IanaCacFV5YYTGJODJ+7QO07mrnP6HdQknaw2xmC5vo3jNOiAronA DR5UzA9lTrXYa/LkiNX/ve0gSNr9a6TUPYNpXhMbvThKkGkXnbwsrdQ1b4RjdI0TXkQC dXr6so3ZvYtYsp88KVhSwsa1Wj9PQ0rNVgIwMuxpSMexM+X/UsytrBwjWxRxg/p6s20H Bng2k/gkslzKTxwvk2VHv8+2gkbNz9Gsbvf2JraN8yhCFI7C78dklDLE10prHUAs1W6J nmIg1mBbV+4nl4sPtFhjP9dCttnOCvTOHhI+Sv3WTyM2XNvqYSZ8oXPrR1RcEjHuizFu 373A== 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=RLIzkNp469XKkAogbbn5ChGAknk43nbkJtxJlDwotHE=; b=vwoZW+2iy0zvc8PzorE6d5Ou0ULwPpu0o3ogo/pOt/X7PDutmFV5+Gpi/g2Ql7YATO Iag0jTGQoi6kSddFe9lgCdmg4hE65yUXYGlCk4KkPpRcbcCDGPHVT4U+v0v9hbo5beXC as38A+rqC2bgAacOzFJqUGSOWs5HFGZT5Hb9sJJIkknDmK4APGzZu0I8XwmH/OJpoilg 1IQ3HnHUx/ITFsZESitQWGOXaz3DvK4hp9XcSQxBVjNjH9uhaxV62kQ6exSmGA0LBJ4D LyNtOU6tg6HCwmX5LXGiRrDHTdh2fjlngtkmBeY+lJ7LsovkL/o7I/aGux5y96gAluG5 TnjQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=WnAI8Gzd; 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=RLIzkNp469XKkAogbbn5ChGAknk43nbkJtxJlDwotHE=; b=QRS+FOc4E4G5ZRefL2cvWYhXGEu1k1cbsga3APLU1JwpYHoqFD7KlXspt6+8D8Pb3U nrnhhvrPrU6gf0X77GU51aIeP5RuhOdMxIAnXOxLXLXtt93saFssWy3IyqO0e574Rlp0 uSLjzUF+PkMN/65i6oryDgzTGiL3emMwdk++Q= 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=RLIzkNp469XKkAogbbn5ChGAknk43nbkJtxJlDwotHE=; b=TYDkrHcczl1ejTVKwB+TgEfWE/6m761f5RdeVnb8kYeLGPMHOPRDql8bst5pRChn3a 3/tfRV84bUDEH8tyfD8tgO3rIVY+MW93RR494kbz5+XptNwZRLFHF/wKLIownq+H8s2s bfTkr0ctX6amL7h2DSxjwcjRoqH0pJEs8TbhBFCZkckzPDqEQRYkPiBdZpdQ+ITj6re6 aaMr6dMt5tQeGMhtU+aaddFNayWnwW4ZwbG6ikVwY53cCIMZR6Ykxq9XOjroHT0LdRLu 1065X6pG/en7Ro/KH5AsjwYG68Jb5Thr5ST0JlZwIPM/fsCkWInPABAWi2pNGb5nP5ie W1Dw== X-Gm-Message-State: AFqh2krEAe90sKNFViIZhMLgC0DXJvvGfVs1s3Z5Guzta4ByVH7yQuUF 0KHUr1aLm2cKlolWknaoEC+94Y2d X-Google-Smtp-Source: AMrXdXtF2X9YgE5Y0kXsek5zSjPnCYFtMZZBIvVW9RZtfcFIg3UbNkIGdoHgh5GRIU2B2nkYZKSHPg== X-Received: by 2002:a50:e608:0:b0:46b:203:f389 with SMTP id y8-20020a50e608000000b0046b0203f389mr3526777edm.303.1672595872765; Sun, 01 Jan 2023 09:57:52 -0800 (PST) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:3d62:b0:7ad:9efd:4692 with SMTP id r2-20020a1709063d6200b007ad9efd4692ls2016004ejf.4.-pod-prod-gmail; Sun, 01 Jan 2023 09:57:51 -0800 (PST) X-Received: by 2002:a17:906:9e23:b0:7aa:7598:128c with SMTP id fp35-20020a1709069e2300b007aa7598128cmr26833383ejc.37.1672595871731; Sun, 01 Jan 2023 09:57:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672595871; cv=none; d=google.com; s=arc-20160816; b=Cg87F/kTv+PlwCHQWJ28XljWQWovOfge9Bt2BFCJ1qmLwecA7jpfs9o81nZSji5GBf licLgSsp5IS+esQ2VmcRyseOn9yUt8O7fdJodqrDGsZpDAQYRR27Bfvwbs+B7+CGFKqk B1ZTUYAOlLg5H7I3XsCXzqSPhYX4jr/pf/iCNrQBb8DailzpOrMkZGBMT0d/RiVVI5C+ zu0BSpkLsr7e1T9qXm+W3y6B+ldz8TuS9dYh6lfad5ipvkYuEqUeyQbadwpkfYSrBf7v owrnOSpOIq4qPmBus67ArpXA+ErhxBnM0DcE3X4Q9Y1wFuXD+pj/klXBLiJh+ATPlEzA aRPQ== 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=oblVMZgZfte7LXSyrB/POmBldgu6kOYgd6zW5q8+W0Y=; b=CMhy6n55tPG4zHp7tBbjPL9wx314eK4ubnb7E0yL+mUVR9HbbJCyANI1rhZ4/u7GZ6 MCtpdWs9q+c033++qV/vmAxpuBqqDGAShY8AMIQZjHJZXsz+PtgM2UoOdT/SWJHgwjAE 9DSMoZ19AYkN9PUjJDc1XZAMGcx60fnjJiAMMk3ByynKOqz5h/oZtotUsiOFcLHGWyyM A/cct8LHQeDRd4HfMPdDA9kQYrt1xuw7FBIdk+O5E7eAuRzmmB7CCShwRIGZoCc3Nqba JR+KksuZLdbIFfMimZ7DzajqfK/8X3DKNKBc4eTLnPqr12I0kCGRcyy9i0FpvcPnR4wF /Scg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=WnAI8Gzd; 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 vu12-20020a170907a64c00b007ec2dce8b01sor12127085ejc.3.2023.01.01.09.57.51 for (Google Transport Security); Sun, 01 Jan 2023 09:57:51 -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:11cf:b0:7c2:3b8a:9f0d with SMTP id va15-20020a17090711cf00b007c23b8a9f0dmr33781690ejb.51.1672595871493; Sun, 01 Jan 2023 09:57:51 -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 q2-20020a1709063d4200b0082ddfb47d06sm12273018ejf.148.2023.01.01.09.57.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Jan 2023 09:57:51 -0800 (PST) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: angelo@amarulasolutions.com, michael@amarulasolutions.com, tommaso.merciai@amarulasolutions.com, Chen-Yu Tsai , linux-amarula@amarulasolutions.com, anthony@amarulasolutions.com, jagan@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 v2 03/11] clk: imx8mn: add gate driver Date: Sun, 1 Jan 2023 18:57:32 +0100 Message-Id: <20230101175740.1010258-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20230101175740.1010258-1-dario.binacchi@amarulasolutions.com> References: <20230101175740.1010258-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=WnAI8Gzd; 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 --- (no changes since v1) 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);