From nobody Thu Apr 18 01:37:43 2024 Delivered-To: importer2@patchew.org Received-SPF: pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; envelope-from=linux-kernel-owner@vger.kernel.org; helo=vger.kernel.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mx.zohomail.com with SMTP id 16440413115501000.2585341073396; Fri, 4 Feb 2022 22:08:31 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348884AbiBEGIa (ORCPT ); Sat, 5 Feb 2022 01:08:30 -0500 Received: from mail.pr-group.ru ([178.18.215.3]:55884 "EHLO mail.pr-group.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236890AbiBEGI1 (ORCPT ); Sat, 5 Feb 2022 01:08:27 -0500 Received: from localhost.localdomain ([178.70.66.234]) (authenticated user i.bornyakov@metrotek.ru) by mail.pr-group.ru with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Sat, 5 Feb 2022 09:08:10 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=metrotek.ru; s=mail; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding; bh=yNGCQoHG8+MFI4xF4xpFYp+Wgphi4GDGw/OoJhRKimA=; b=Fo2nDNbyVbR0MFSF87yhLPh3AOTbbsz3Adb1KpRqXD4jQl26aRrZG17W30z3j4iDronhKGYzhAbuH PLQWQ24MMgFehZZrcEphEqtvvnlJSXcPs1T0Fo1V3fNfeU9WmJwa4kgt33fgMVoEt89xOhkabNw1H9 /zklc4OW4k/Te4WGLZnVEtgDbkTsg9qXu+17fzK0IBE9sTKMb5Z3CzLgsFZDpWXRKWc18D5eMqYjrs 2nklDi9IWuhsbsaWLtYaaDT30RU9a3MnAxQWkJ6KdD/rsjFeKEnx1u+NCEmgdiQtHvJ1SMyCY0yyLj lBbXpiFuvPrDm58hozTEb5H7q5J4YPw== X-Kerio-Anti-Spam: Build: [Engines: 2.16.1.1366, Stamp: 3], Multi: [Enabled, t: (0.000013,0.020896)], BW: [Enabled, t: (0.000026,0.000002)], RTDA: [Enabled, t: (0.077268), Hit: No, Details: v2.24.0; Id: 15.52k2di.1fr47ujjd.804g; mclb], total: 0(700) X-Spam-Status: No, hits=0.0 required=4.0 tests=KERIO_ANTI_SPAM: -0.000, AWL: 0.808, BAYES_00: -1.665, CUSTOM_RULE_FROM: ALLOW, TOTAL_SCORE: -0.857,autolearn=ham X-Spam-Level: X-Footer: bWV0cm90ZWsucnU= From: Ivan Bornyakov Cc: shawnguo@kernel.org, s.hauer@pengutronix.de, kernel@pengutronix.de, festevam@gmail.com, linux-imx@nxp.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, system@metrotek.ru, Ivan Bornyakov Subject: [PATCH] bus: imx-weim: add DT overlay support for WEIM bus Date: Sat, 5 Feb 2022 08:50:05 +0300 Message-Id: <20220205055006.23447-1-i.bornyakov@metrotek.ru> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1644041314355100001 Content-Type: text/plain; charset="utf-8" Add OF reconfiguration notifier handler for WEIM bus to setup Chip Select timings on runtime creation of child devices. However, it is not possible to load another DT overlay with conflicting CS timings with previously loaded overlay, even if the first one is unloaded. The reason is that there is no acces to CS timing property of a device node being removed, thus we can't track which of configured CS are available for re-configuration. Signed-off-by: Ivan Bornyakov --- drivers/bus/imx-weim.c | 136 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 127 insertions(+), 9 deletions(-) diff --git a/drivers/bus/imx-weim.c b/drivers/bus/imx-weim.c index bccb275b65ba..a09e1d33a554 100644 --- a/drivers/bus/imx-weim.c +++ b/drivers/bus/imx-weim.c @@ -64,6 +64,11 @@ struct cs_timing_state { struct cs_timing cs[MAX_CS_COUNT]; }; =20 +struct weim_data { + void __iomem *base; + struct cs_timing_state timing_state; +}; + static const struct of_device_id weim_id_table[] =3D { /* i.MX1/21 */ { .compatible =3D "fsl,imx1-weim", .data =3D &imx1_weim_devtype, }, @@ -128,21 +133,26 @@ static int imx_weim_gpr_setup(struct platform_device = *pdev) } =20 /* Parse and set the timing for this device. */ -static int weim_timing_setup(struct device *dev, - struct device_node *np, void __iomem *base, - const struct imx_weim_devtype *devtype, - struct cs_timing_state *ts) +static int weim_timing_setup(struct device *dev, struct device_node *np, + const struct imx_weim_devtype *devtype) { u32 cs_idx, value[MAX_CS_REGS_COUNT]; int i, ret; int reg_idx, num_regs; struct cs_timing *cst; + struct weim_data *priv; + struct cs_timing_state *ts; + void __iomem *base; =20 if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT)) return -EINVAL; if (WARN_ON(devtype->cs_count > MAX_CS_COUNT)) return -EINVAL; =20 + priv =3D dev_get_drvdata(dev); + base =3D priv->base; + ts =3D &priv->timing_state; + ret =3D of_property_read_u32_array(np, "fsl,weim-cs-timing", value, devtype->cs_regs_count); if (ret) @@ -189,14 +199,15 @@ static int weim_timing_setup(struct device *dev, return 0; } =20 -static int weim_parse_dt(struct platform_device *pdev, void __iomem *base) +static int weim_parse_dt(struct platform_device *pdev) { const struct of_device_id *of_id =3D of_match_device(weim_id_table, &pdev->dev); const struct imx_weim_devtype *devtype =3D of_id->data; struct device_node *child; int ret, have_child =3D 0; - struct cs_timing_state ts =3D {}; + struct weim_data *priv; + void __iomem *base; u32 reg; =20 if (devtype =3D=3D &imx50_weim_devtype) { @@ -205,6 +216,9 @@ static int weim_parse_dt(struct platform_device *pdev, = void __iomem *base) return ret; } =20 + priv =3D dev_get_drvdata(&pdev->dev); + base =3D priv->base; + if (of_property_read_bool(pdev->dev.of_node, "fsl,burst-clk-enable")) { if (devtype->wcr_bcm) { reg =3D readl(base + devtype->wcr_offset); @@ -229,7 +243,7 @@ static int weim_parse_dt(struct platform_device *pdev, = void __iomem *base) } =20 for_each_available_child_of_node(pdev->dev.of_node, child) { - ret =3D weim_timing_setup(&pdev->dev, child, base, devtype, &ts); + ret =3D weim_timing_setup(&pdev->dev, child, devtype); if (ret) dev_warn(&pdev->dev, "%pOF set timing failed.\n", child); @@ -248,17 +262,25 @@ static int weim_parse_dt(struct platform_device *pdev= , void __iomem *base) =20 static int weim_probe(struct platform_device *pdev) { + struct weim_data *priv; struct resource *res; struct clk *clk; void __iomem *base; int ret; =20 + priv =3D devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + /* get the resource */ res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); base =3D devm_ioremap_resource(&pdev->dev, res); if (IS_ERR(base)) return PTR_ERR(base); =20 + priv->base =3D base; + dev_set_drvdata(&pdev->dev, priv); + /* get the clock */ clk =3D devm_clk_get(&pdev->dev, NULL); if (IS_ERR(clk)) @@ -269,7 +291,7 @@ static int weim_probe(struct platform_device *pdev) return ret; =20 /* parse the device node */ - ret =3D weim_parse_dt(pdev, base); + ret =3D weim_parse_dt(pdev); if (ret) clk_disable_unprepare(clk); else @@ -278,6 +300,82 @@ static int weim_probe(struct platform_device *pdev) return ret; } =20 +#if IS_ENABLED(CONFIG_OF_DYNAMIC) +static int of_weim_notify(struct notifier_block *nb, unsigned long action, + void *arg) +{ + const struct imx_weim_devtype *devtype; + struct of_reconfig_data *rd =3D arg; + const struct of_device_id *of_id; + struct platform_device *pdev; + int ret =3D NOTIFY_OK; + + switch (of_reconfig_get_state_change(action, rd)) { + case OF_RECONFIG_CHANGE_ADD: + of_id =3D of_match_node(weim_id_table, rd->dn->parent); + if (!of_id) + return NOTIFY_OK; /* not for us */ + + devtype =3D of_id->data; + + pdev =3D of_find_device_by_node(rd->dn->parent); + if (!pdev) { + pr_err("%s: could not find platform device for '%pOF'\n", + __func__, rd->dn->parent); + + return notifier_from_errno(-EINVAL); + } + + if (weim_timing_setup(&pdev->dev, rd->dn, devtype)) + dev_warn(&pdev->dev, + "Failed to setup timing for '%pOF'\n", rd->dn); + + if (!of_node_check_flag(rd->dn, OF_POPULATED)) { + if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) { + dev_err(&pdev->dev, + "Failed to create child device '%pOF'\n", + rd->dn); + ret =3D notifier_from_errno(-EINVAL); + } + } + + platform_device_put(pdev); + + break; + case OF_RECONFIG_CHANGE_REMOVE: + if (!of_node_check_flag(rd->dn, OF_POPULATED)) + return NOTIFY_OK; /* device already destroyed */ + + of_id =3D of_match_node(weim_id_table, rd->dn->parent); + if (!of_id) + return NOTIFY_OK; /* not for us */ + + pdev =3D of_find_device_by_node(rd->dn); + if (!pdev) { + dev_err(&pdev->dev, + "Could not find platform device for '%pOF'\n", + rd->dn); + + ret =3D notifier_from_errno(-EINVAL); + } else { + of_platform_device_destroy(&pdev->dev, NULL); + platform_device_put(pdev); + } + + break; + default: + break; + } + + return ret; +} + +struct notifier_block weim_of_notifier =3D { + .notifier_call =3D of_weim_notify, +}; +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + + static struct platform_driver weim_driver =3D { .driver =3D { .name =3D "imx-weim", @@ -285,7 +383,27 @@ static struct platform_driver weim_driver =3D { }, .probe =3D weim_probe, }; -module_platform_driver(weim_driver); + +static int __init weim_init(void) +{ +#if IS_ENABLED(CONFIG_OF_DYNAMIC) + WARN_ON(of_reconfig_notifier_register(&weim_of_notifier)); +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + + return platform_driver_register(&weim_driver); +} +module_init(weim_init); + +static void __exit weim_exit(void) +{ +#if IS_ENABLED(CONFIG_OF_DYNAMIC) + of_reconfig_notifier_unregister(&weim_of_notifier); +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + + return platform_driver_unregister(&weim_driver); + +} +module_exit(weim_exit); =20 MODULE_AUTHOR("Freescale Semiconductor Inc."); MODULE_DESCRIPTION("i.MX EIM Controller Driver"); --=20 2.34.1