From patchwork Sat Dec 31 10:47:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2585 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 75FE63F18E for ; Sat, 31 Dec 2022 11:47:57 +0100 (CET) Received: by mail-ed1-f69.google.com with SMTP id x13-20020a05640226cd00b0047ac11c9774sf15701244edd.17 for ; Sat, 31 Dec 2022 02:47:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1672483677; cv=pass; d=google.com; s=arc-20160816; b=rLLgySSif6c60q2aOQaWvbaHP54Rs3QWBqTPro0D9UM3LneR3yHaokaCd9xu8oyUEJ +NPYiqt8z/wUiCfik0EgxeOu+nE53Svx7rooQTt70eZT/ZEVRF7hjX4lPeRn6KPMOZu+ pUNxFkySgbXxmE+mANOkaGyacASEpFd2BfVDNggl8vBGpPTay1OlrNmzhQYuGVGqU+yb K3PrYOW7awEVJQX3iRAbwfm7JFWVc4iX5IvgzcLO9XACKITTPLBOYoc2XmBXHmkyNDT6 HFjB+m0kIlqQ1+zZAIH1L9JHnzLDrrUf26TyZytlDo9P7hzKohBTt2M+3NTk8ZZpVXeY Qipw== 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=fQblfmBwuEisKQBYhp8i7LEEY57iXu2aC298XTFz0xI=; b=zr/y6oNYfm06RR7KnEA+4ngeNdUF/UTvnwTS+cCbyD3YaOnMxRXjBTGWIiujiDd1Jr FgGd8krGa+Lr1Dfe+e2KdRrDFI2x7qmpYc2aV87N4907z2M9tjL7IreiagXi+QNzbGme CiigLQAF79VfVprNRLofVJCbukd8Hg/1JOl218rvOi3COZybyuESKuaYR+h0Ghmh3hDu pEjSJMN2hyVUkR7P5sRryyUJfZalk+I7dJh9Vup/9Zap+tMckaOKEPcQAVCeT9v1WJnE os+BcHv1HrdLYOqivH/P79kwGc9DM4/NbWAT4/Bp0jNCICcOGTAHdr8cVxr6bjdkwAeE bYPw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=QoTb9Sof; 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=fQblfmBwuEisKQBYhp8i7LEEY57iXu2aC298XTFz0xI=; b=SV0Dv8/OTKiEE2Vyh/Nbw+fwByJPQMAdYy6oCVz1yejaklTmnYgqqJqEEqmayx0KWU WYO7r9kCcrmnxEky9AUaWIkXiuFSQPpEZSP8U/zIQjM0//XVixac4uxs+TmJLK1END4H vhe/EqutAsDG+vfnXlGqrEVP1xG82WT4keqeM= 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=fQblfmBwuEisKQBYhp8i7LEEY57iXu2aC298XTFz0xI=; b=bjWHumK2Xw62qxil98f2QbV/haTbzt5fgAlc3tUPvRH+jYHQs5Qf7Aiw3hOI3DZbLO FdbQDf5O0IZIfaazXnl8O7x4F71oYubS0nT2j5OK2hjgoV7DEDDFvshfpjGPCwwHsgFP DWdVhkjhY1CgZzJzRBBb8O0YPuTZxMRaRLgXoBfGniqAE+7pd7CXLk0kLiLKoV5LQi/f kUDNEoba/Ll2oy3aDenJ6azTkdcNLH2zEqeDWiz8xDjqm2SKz9EiTcaDkaZlHc/zLglu UAZYnN80Pu55C5vBKPiZKNaFJzX3zKgxl2fCssoBtjlXsUtM5xU52rPCwvT+3HVsr2w3 FMOw== X-Gm-Message-State: AFqh2kplSgAD2I9ZPb/Wt7HzVcGNnkSdPx1Pb7VjTQo2mhCHjPb2y1Se s/VS4Skif0ReGsu/u6zEIUU5xHD3 X-Google-Smtp-Source: AMrXdXu/DYMsoi63KXSu9+5TXifUVbc7J7jC09tDODv15zNC6Il3XYdHrECC3rEjUoYN+o9Opo1now== X-Received: by 2002:a05:6402:5507:b0:468:15f1:54b5 with SMTP id fi7-20020a056402550700b0046815f154b5mr2996361edb.8.1672483677298; Sat, 31 Dec 2022 02:47:57 -0800 (PST) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:36d7:b0:7c1:381b:406d with SMTP id b23-20020a17090636d700b007c1381b406dls951516ejc.11.-pod-prod-gmail; Sat, 31 Dec 2022 02:47:56 -0800 (PST) X-Received: by 2002:a17:907:b9d8:b0:7c1:6040:2318 with SMTP id xa24-20020a170907b9d800b007c160402318mr31184122ejc.35.1672483676201; Sat, 31 Dec 2022 02:47:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672483676; cv=none; d=google.com; s=arc-20160816; b=gmIs9YNKjMYkwX4tDog9j2NOYoMKlWKx9R0isOrqBgIrk5Ru7WLli38KMsMvDShmfl jyQQ+hiOQIEXdAxTF1xTUYWDYNLQjnHDntVwU6kfxipD/wfsNY5MnL+Xr4WyJ8koxtrx omatSHBiKn64YHr9psuFq1f54b2JRCSUjyJUJufG1srMYT5FSd4nV9UZ4M1ZdVJJ8sWU BjFnirsVslIMYAiO9KpKwAgEgCBOL2uLBA021SjVENXVAMhitwFGpz4ZODPPadOYeDPr Y13LrMvCvp/Xw/Ce2HteCN7gccow+cVOuMipCNQeUeX8xDcJ/YGtt0kEeJ4f1QKD5L9b L/QQ== 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=hDUNpRYxOH0Qfmue9rixWxBdtGSeAjE61Y6tD2lDkOk=; b=UFZ/xX7Yc+Zcu7vdD/Pbq1k4lm/zUS5cqjaYnO93kdb+7RYz1gpN5fRVJPKKsN/hvG GF6WreiJ8QP+9rozcX5+p71A9r0eS8dLO+95rKYCUdNVrBVJoCrDrJECO3xjvR1rKAM/ MulwdUdoN7Q296gnUCBG2uL7c1HZY8MSbJ2fZoEF9keahKqBsZVWUzWTqiQA24i4d8+u orJMuHblPKNT9T0J9rt+yPTxNxnk110yQynpMnX/d9dxMH3QDQsBYt8w0WG2BSZFVRVX Kg+4/kyd55UeQaunRvUNwJyC3TBySH6zT0ruOv1IiDJWQUWUn1qYti22dYKUCvOQc+T+ w/8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=QoTb9Sof; 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 c23-20020a1709060fd700b007bd533d62aesor10791798ejk.86.2022.12.31.02.47.56 for (Google Transport Security); Sat, 31 Dec 2022 02:47:56 -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:8e93:b0:7bf:3627:df32 with SMTP id tx19-20020a1709078e9300b007bf3627df32mr30498842ejc.75.1672483675879; Sat, 31 Dec 2022 02:47:55 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Dec 2022 02:47:55 -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 04/11] clk: imx8mn: add mux driver Date: Sat, 31 Dec 2022 11:47:29 +0100 Message-Id: <20221231104736.12635-5-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=QoTb9Sof; 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 mux clocks to be initialized directly from the device tree. Currently all i.MX mux clocks are initialized by legacy code with hardwired parameters. This approach has generated setup functions with unclear names: git grep "#define imx_clk_hw_mux" drivers/clk/imx/clk.h drivers/clk/imx/clk.h:#define imx_clk_hw_mux2(name, reg, shift, width, parents, num_parents) \ drivers/clk/imx/clk.h:#define imx_clk_hw_mux(name, reg, shift, width, parents, num_parents) \ drivers/clk/imx/clk.h:#define imx_clk_hw_mux_flags(name, reg, shift, width, parents, num_parents, flags) \ drivers/clk/imx/clk.h:#define imx_clk_hw_mux_ldb(name, reg, shift, width, parents, num_parents) \ drivers/clk/imx/clk.h:#define imx_clk_hw_mux2_flags(name, reg, shift, width, parents, num_parents, 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-mux.c | 258 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 drivers/clk/imx/clk-mux.c diff --git a/drivers/clk/imx/Makefile b/drivers/clk/imx/Makefile index 72e1f08d49dc..1cffc5bebbe1 100644 --- a/drivers/clk/imx/Makefile +++ b/drivers/clk/imx/Makefile @@ -15,6 +15,7 @@ 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 +mxc-clk-objs += clk-mux.o mxc-clk-objs += clk-pfd.o mxc-clk-objs += clk-pfdv2.o mxc-clk-objs += clk-pllv1.o diff --git a/drivers/clk/imx/clk-mux.c b/drivers/clk/imx/clk-mux.c new file mode 100644 index 000000000000..426738d25582 --- /dev/null +++ b/drivers/clk/imx/clk-mux.c @@ -0,0 +1,258 @@ +// 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_mux(_hw) container_of(_hw, struct clk_imx_mux, hw) + +struct clk_imx_mux { + struct clk_hw hw; + struct imx_clk_reg reg; + u32 mask; + u8 shift; + u8 saved_parent; +}; + +static int imx_clk_mux_write(const struct imx_clk_reg *reg, u32 val) +{ + int ret = 0; + + if (reg->base) { + writel(val, reg->base + reg->offset); + } else if (reg->regmap) { + ret = regmap_write(reg->regmap, reg->offset, val); + } else { + pr_err("memory address not set\n"); + ret = -EIO; + } + + return ret; +} + +static int imx_clk_mux_read(const struct imx_clk_reg *reg, u32 *val) +{ + int ret = 0; + + if (reg->base) { + *val = readl(reg->base + reg->offset); + } else if (reg->regmap) { + ret = regmap_read(reg->regmap, reg->offset, val); + } else { + pr_err("memory address not set\n"); + ret = -EIO; + } + + return ret; +} + +static u8 imx_clk_mux_get_parent(struct clk_hw *hw) +{ + + struct clk_imx_mux *mux = to_clk_imx_mux(hw); + int num_parents = clk_hw_get_num_parents(hw); + unsigned int val; + int ret; + + ret = imx_clk_mux_read(&mux->reg, &val); + if (ret) + return ret; + + val = (val >> mux->shift) && mux->mask; + + if (val >= num_parents) + return -EINVAL; + + return val; +} + +static int imx_clk_mux_set_parent(struct clk_hw *hw, u8 index) +{ + struct clk_imx_mux *mux = to_clk_imx_mux(hw); + unsigned int val; + int ret; + + ret = imx_clk_mux_read(&mux->reg, &val); + if (ret) + return ret; + + val &= ~(mux->mask << mux->shift); + val |= index << mux->shift; + return imx_clk_mux_write(&mux->reg, val); +} + +/** + * imx_clk_mux_save_context - Save the parent selcted in the mux + * @hw: pointer struct clk_hw + * + * Save the parent mux value. + */ +static int imx_clk_mux_save_context(struct clk_hw *hw) +{ + struct clk_imx_mux *mux = to_clk_imx_mux(hw); + + mux->saved_parent = imx_clk_mux_get_parent(hw); + return 0; +} + +/** + * imx_clk_mux_restore_context - Restore the parent in the mux + * @hw: pointer struct clk_hw + * + * Restore the saved parent mux value. + */ +static void imx_clk_mux_restore_context(struct clk_hw *hw) +{ + struct clk_imx_mux *mux = to_clk_imx_mux(hw); + + imx_clk_mux_set_parent(hw, mux->saved_parent); +} + +const struct clk_ops imx_clk_mux_ops = { + .get_parent = imx_clk_mux_get_parent, + .set_parent = imx_clk_mux_set_parent, + .determine_rate = __clk_mux_determine_rate, + .save_context = imx_clk_mux_save_context, + .restore_context = imx_clk_mux_restore_context, +}; + +static void imx_clk_hw_unregister_mux(struct clk_hw *hw) +{ + struct clk_imx_mux *mux = to_clk_imx_mux(hw); + + clk_hw_unregister(hw); + kfree(mux); +} + +static struct clk_hw *imx_clk_hw_register_mux(struct device_node *node, + const char *name, + const char * const *parent_names, + u8 num_parents, + unsigned long flags, + struct imx_clk_reg *reg, u8 shift, + u32 mask) +{ + struct clk_init_data init = { NULL }; + struct clk_imx_mux *mux; + struct clk_hw *hw; + + int ret; + + mux = kzalloc(sizeof(*mux), GFP_KERNEL); + if (!mux) + return ERR_PTR(-ENOMEM); + + init.name = name; + init.flags = flags; + init.ops = &imx_clk_mux_ops; + init.parent_names = parent_names; + init.num_parents = num_parents; + + /* struct clk_mux assignments */ + memcpy(&mux->reg, reg, sizeof(*reg)); + mux->hw.init = &init; + + hw = &mux->hw; + ret = of_clk_hw_register(node, hw); + if (ret) { + kfree(mux); + return ERR_PTR(ret); + } + + return hw; +} + +/** + * of_imx_mux_clk_setup() - Setup function for imx mux clock + * @node: device node for the clock + */ +static void __init of_imx_mux_clk_setup(struct device_node *node) +{ + struct clk_hw *hw; + unsigned int num_parents; + const char **parent_names; + const char *name = node->name; + struct imx_clk_reg reg = {}; + u32 shift = 0; + u32 flags = CLK_SET_RATE_NO_REPARENT; + u32 val; + u32 mask; + + reg.regmap = syscon_regmap_lookup_by_phandle(node, "fsl,anatop"); + if (!IS_ERR(reg.regmap)) { + if (of_property_read_u32_index(node, "fsl,anatop", 1, &val)) { + pr_err("missing register offset for %pOFn\n", node); + return; + } + + reg.offset = val; + } else { + reg.base = of_iomap(node, 0); + if (IS_ERR(reg.base)) { + pr_err("failed to get register address for %pOFn\n", + node); + return; + } + } + + num_parents = of_clk_get_parent_count(node); + if (num_parents < 2) { + pr_err("%pOFn must have parents\n", node); + return; + } + + parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL); + if (!parent_names) + return; + + of_clk_parent_fill(node, parent_names, num_parents); + + of_property_read_u32(node, "fsl,bit-shift", &shift); + + if (of_property_read_bool(node, "fsl,is-critical")) + flags |= CLK_IS_CRITICAL; + + if (of_property_read_bool(node, "fsl,ops-parent-enable")) + flags |= CLK_OPS_PARENT_ENABLE; + + if (of_property_read_bool(node, "fsl,set-rate-parent")) + flags |= CLK_SET_RATE_PARENT; + + /* Generate bit-mask based on parent info */ + mask = num_parents - 1; + mask = (1 << fls(mask)) - 1; + + of_property_read_string(node, "clock-output-names", &name); + + hw = imx_clk_hw_register_mux(node, name, parent_names, num_parents, + flags, ®, shift, mask); + if (IS_ERR(hw)) + goto free_parent_names; + + if (of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw)) { + imx_clk_hw_unregister_mux(hw); + goto free_parent_names; + } + + pr_debug("name: %s, offset: 0x%x, shift: %d, mask: 0x%x\n", name, + reg.offset, shift, mask); + +free_parent_names: + kfree(parent_names); +} +CLK_OF_DECLARE(fsl_imx8mn_mux_clk, "fsl,imx8mn-mux-clock", + of_imx_mux_clk_setup);