From nobody Fri Dec 19 22:04:10 2025 Received: from sakura.ysato.name (ik1-413-38519.vs.sakura.ne.jp [153.127.30.23]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 78D5B2D60F; Tue, 9 Jan 2024 08:23:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=users.sourceforge.jp Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=users.sourceforge.jp Received: from SIOS1075.ysato.name (ZM005235.ppp.dion.ne.jp [222.8.5.235]) by sakura.ysato.name (Postfix) with ESMTPSA id 946F31C05E5; Tue, 9 Jan 2024 17:23:53 +0900 (JST) From: Yoshinori Sato To: linux-sh@vger.kernel.org Cc: Yoshinori Sato , Damien Le Moal , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Geert Uytterhoeven , Michael Turquette , Stephen Boyd , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Thomas Gleixner , Lorenzo Pieralisi , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Bjorn Helgaas , Greg Kroah-Hartman , Jiri Slaby , Magnus Damm , Daniel Lezcano , Rich Felker , John Paul Adrian Glaubitz , Lee Jones , Helge Deller , Heiko Stuebner , Jernej Skrabec , Chris Morgan , Yang Xiwen , Sebastian Reichel , Linus Walleij , Randy Dunlap , Arnd Bergmann , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, David Rientjes , Baoquan He , Andrew Morton , Guenter Roeck , Stephen Rothwell , Azeem Shaikh , Javier Martinez Canillas , Max Filippov , Palmer Dabbelt , Bin Meng , Jonathan Corbet , Jacky Huang , Lukas Bulwahn , Biju Das , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Sam Ravnborg , Sergey Shtylyov , Michael Karcher , Laurent Pinchart , linux-ide@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-clk@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-pci@vger.kernel.org, linux-serial@vger.kernel.org, linux-fbdev@vger.kernel.org Subject: [DO NOT MERGE v6 08/37] clocksource: sh_tmu: CLOCKSOURCE support. Date: Tue, 9 Jan 2024 17:23:05 +0900 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allows initialization as CLOCKSOURCE. Signed-off-by: Yoshinori Sato --- drivers/clocksource/sh_tmu.c | 161 +++++++++++++++++++++++------------ 1 file changed, 106 insertions(+), 55 deletions(-) diff --git a/drivers/clocksource/sh_tmu.c b/drivers/clocksource/sh_tmu.c index beffff81c00f..e4ae83c9f7d4 100644 --- a/drivers/clocksource/sh_tmu.c +++ b/drivers/clocksource/sh_tmu.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -65,6 +67,7 @@ struct sh_tmu_device { =20 bool has_clockevent; bool has_clocksource; + const char *name; }; =20 #define TSTR -1 /* shared register */ @@ -148,8 +151,8 @@ static int __sh_tmu_enable(struct sh_tmu_channel *ch) /* enable clock */ ret =3D clk_enable(ch->tmu->clk); if (ret) { - dev_err(&ch->tmu->pdev->dev, "ch%u: cannot enable clock\n", - ch->index); + pr_err("%s ch%u: cannot enable clock\n", + ch->tmu->name, ch->index); return ret; } =20 @@ -174,9 +177,10 @@ static int sh_tmu_enable(struct sh_tmu_channel *ch) if (ch->enable_count++ > 0) return 0; =20 - pm_runtime_get_sync(&ch->tmu->pdev->dev); - dev_pm_syscore_device(&ch->tmu->pdev->dev, true); - + if (ch->tmu->pdev) { + pm_runtime_get_sync(&ch->tmu->pdev->dev); + dev_pm_syscore_device(&ch->tmu->pdev->dev, true); + } return __sh_tmu_enable(ch); } =20 @@ -202,8 +206,10 @@ static void sh_tmu_disable(struct sh_tmu_channel *ch) =20 __sh_tmu_disable(ch); =20 - dev_pm_syscore_device(&ch->tmu->pdev->dev, false); - pm_runtime_put(&ch->tmu->pdev->dev); + if (ch->tmu->pdev) { + dev_pm_syscore_device(&ch->tmu->pdev->dev, false); + pm_runtime_put(&ch->tmu->pdev->dev); + } } =20 static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta, @@ -245,7 +251,7 @@ static irqreturn_t sh_tmu_interrupt(int irq, void *dev_= id) return IRQ_HANDLED; } =20 -static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs) +static inline struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs) { return container_of(cs, struct sh_tmu_channel, cs); } @@ -292,7 +298,8 @@ static void sh_tmu_clocksource_suspend(struct clocksour= ce *cs) =20 if (--ch->enable_count =3D=3D 0) { __sh_tmu_disable(ch); - dev_pm_genpd_suspend(&ch->tmu->pdev->dev); + if (ch->tmu->pdev) + dev_pm_genpd_suspend(&ch->tmu->pdev->dev); } } =20 @@ -304,7 +311,8 @@ static void sh_tmu_clocksource_resume(struct clocksourc= e *cs) return; =20 if (ch->enable_count++ =3D=3D 0) { - dev_pm_genpd_resume(&ch->tmu->pdev->dev); + if (ch->tmu->pdev) + dev_pm_genpd_resume(&ch->tmu->pdev->dev); __sh_tmu_enable(ch); } } @@ -324,14 +332,14 @@ static int sh_tmu_register_clocksource(struct sh_tmu_= channel *ch, cs->mask =3D CLOCKSOURCE_MASK(32); cs->flags =3D CLOCK_SOURCE_IS_CONTINUOUS; =20 - dev_info(&ch->tmu->pdev->dev, "ch%u: used as clock source\n", - ch->index); + pr_info("%s ch%u: used as clock source\n", + ch->tmu->name, ch->index); =20 clocksource_register_hz(cs, ch->tmu->rate); return 0; } =20 -static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced) +static inline struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_devi= ce *ced) { return container_of(ced, struct sh_tmu_channel, ced); } @@ -364,8 +372,8 @@ static int sh_tmu_clock_event_set_state(struct clock_ev= ent_device *ced, if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced)) sh_tmu_disable(ch); =20 - dev_info(&ch->tmu->pdev->dev, "ch%u: used for %s clock events\n", - ch->index, periodic ? "periodic" : "oneshot"); + pr_info("%s ch%u: used for %s clock events\n", + ch->tmu->name, ch->index, periodic ? "periodic" : "oneshot"); sh_tmu_clock_event_start(ch, periodic); return 0; } @@ -403,7 +411,8 @@ static void sh_tmu_clock_event_resume(struct clock_even= t_device *ced) } =20 static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch, - const char *name) + const char *name, + struct device_node *np) { struct clock_event_device *ced =3D &ch->ced; int ret; @@ -417,30 +426,32 @@ static void sh_tmu_register_clockevent(struct sh_tmu_= channel *ch, ced->set_state_shutdown =3D sh_tmu_clock_event_shutdown; ced->set_state_periodic =3D sh_tmu_clock_event_set_periodic; ced->set_state_oneshot =3D sh_tmu_clock_event_set_oneshot; - ced->suspend =3D sh_tmu_clock_event_suspend; - ced->resume =3D sh_tmu_clock_event_resume; - - dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n", - ch->index); + if (ch->tmu->pdev) { + ced->suspend =3D sh_tmu_clock_event_suspend; + ced->resume =3D sh_tmu_clock_event_resume; + } + pr_info("%s ch%u: used for clock events\n", + ch->tmu->name, ch->index); =20 clockevents_config_and_register(ced, ch->tmu->rate, 0x300, 0xffffffff); =20 ret =3D request_irq(ch->irq, sh_tmu_interrupt, IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, - dev_name(&ch->tmu->pdev->dev), ch); + ch->tmu->name, ch); if (ret) { - dev_err(&ch->tmu->pdev->dev, "ch%u: failed to request irq %d\n", - ch->index, ch->irq); + pr_err("%s ch%u: failed to request irq %d\n", + ch->tmu->name, ch->index, ch->irq); return; } } =20 static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name, + struct device_node *np, bool clockevent, bool clocksource) { if (clockevent) { ch->tmu->has_clockevent =3D true; - sh_tmu_register_clockevent(ch, name); + sh_tmu_register_clockevent(ch, name, np); } else if (clocksource) { ch->tmu->has_clocksource =3D true; sh_tmu_register_clocksource(ch, name); @@ -451,7 +462,8 @@ static int sh_tmu_register(struct sh_tmu_channel *ch, c= onst char *name, =20 static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int in= dex, bool clockevent, bool clocksource, - struct sh_tmu_device *tmu) + struct sh_tmu_device *tmu, + struct device_node *np) { /* Skip unused channels. */ if (!clockevent && !clocksource) @@ -465,53 +477,59 @@ static int sh_tmu_channel_setup(struct sh_tmu_channel= *ch, unsigned int index, else ch->base =3D tmu->mapbase + 8 + ch->index * 12; =20 - ch->irq =3D platform_get_irq(tmu->pdev, index); + if (tmu->pdev) + ch->irq =3D platform_get_irq(tmu->pdev, index); + else + ch->irq =3D of_irq_get(np, index); if (ch->irq < 0) return ch->irq; =20 ch->cs_enabled =3D false; ch->enable_count =3D 0; =20 - return sh_tmu_register(ch, dev_name(&tmu->pdev->dev), + return sh_tmu_register(ch, tmu->name, np, clockevent, clocksource); } =20 -static int sh_tmu_map_memory(struct sh_tmu_device *tmu) +static int sh_tmu_map_memory(struct sh_tmu_device *tmu, struct device_node= *np) { struct resource *res; =20 - res =3D platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0); - if (!res) { - dev_err(&tmu->pdev->dev, "failed to get I/O memory\n"); - return -ENXIO; - } + if (tmu->pdev) { + res =3D platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0); + if (!res) { + pr_err("sh_tmu failed to get I/O memory\n"); + return -ENXIO; + } + + tmu->mapbase =3D ioremap(res->start, resource_size(res)); + } else + tmu->mapbase =3D of_iomap(np, 0); =20 - tmu->mapbase =3D ioremap(res->start, resource_size(res)); if (tmu->mapbase =3D=3D NULL) return -ENXIO; =20 return 0; } =20 -static int sh_tmu_parse_dt(struct sh_tmu_device *tmu) +static int sh_tmu_parse_dt(struct sh_tmu_device *tmu, struct device_node *= np) { - struct device_node *np =3D tmu->pdev->dev.of_node; - tmu->model =3D SH_TMU; tmu->num_channels =3D 3; =20 of_property_read_u32(np, "#renesas,channels", &tmu->num_channels); =20 if (tmu->num_channels !=3D 2 && tmu->num_channels !=3D 3) { - dev_err(&tmu->pdev->dev, "invalid number of channels %u\n", - tmu->num_channels); + pr_err("%s: invalid number of channels %u\n", + tmu->name, tmu->num_channels); return -EINVAL; } =20 return 0; } =20 -static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device = *pdev) +static int sh_tmu_setup(struct sh_tmu_device *tmu, + struct platform_device *pdev, struct device_node *np) { unsigned int i; int ret; @@ -520,8 +538,13 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, str= uct platform_device *pdev) =20 raw_spin_lock_init(&tmu->lock); =20 - if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { - ret =3D sh_tmu_parse_dt(tmu); + if (pdev) + tmu->name =3D dev_name(&pdev->dev); + else + tmu->name =3D of_node_full_name(np); + + if (IS_ENABLED(CONFIG_OF) && np) { + ret =3D sh_tmu_parse_dt(tmu, np); if (ret < 0) return ret; } else if (pdev->dev.platform_data) { @@ -531,14 +554,17 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, st= ruct platform_device *pdev) tmu->model =3D id->driver_data; tmu->num_channels =3D hweight8(cfg->channels_mask); } else { - dev_err(&tmu->pdev->dev, "missing platform data\n"); + pr_err("%s missing platform data\n", tmu->name); return -ENXIO; } =20 /* Get hold of clock. */ - tmu->clk =3D clk_get(&tmu->pdev->dev, "fck"); + if (pdev) + tmu->clk =3D clk_get(&tmu->pdev->dev, "fck"); + else + tmu->clk =3D of_clk_get(np, 0); if (IS_ERR(tmu->clk)) { - dev_err(&tmu->pdev->dev, "cannot get clock\n"); + pr_err("%s: cannot get clock\n", tmu->name); return PTR_ERR(tmu->clk); } =20 @@ -555,9 +581,9 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, stru= ct platform_device *pdev) clk_disable(tmu->clk); =20 /* Map the memory resource. */ - ret =3D sh_tmu_map_memory(tmu); + ret =3D sh_tmu_map_memory(tmu, np); if (ret < 0) { - dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n"); + pr_err("%s: failed to remap I/O memory\n", tmu->name); goto err_clk_unprepare; } =20 @@ -575,12 +601,12 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, st= ruct platform_device *pdev) */ for (i =3D 0; i < tmu->num_channels; ++i) { ret =3D sh_tmu_channel_setup(&tmu->channels[i], i, - i =3D=3D 0, i =3D=3D 1, tmu); + i =3D=3D 0, i =3D=3D 1, tmu, np); if (ret < 0) goto err_unmap; } - - platform_set_drvdata(pdev, tmu); + if (pdev) + platform_set_drvdata(pdev, tmu); =20 return 0; =20 @@ -594,6 +620,7 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, stru= ct platform_device *pdev) return ret; } =20 +#ifndef CONFIG_SH_DEVICE_TREE static int sh_tmu_probe(struct platform_device *pdev) { struct sh_tmu_device *tmu =3D platform_get_drvdata(pdev); @@ -613,7 +640,7 @@ static int sh_tmu_probe(struct platform_device *pdev) if (tmu =3D=3D NULL) return -ENOMEM; =20 - ret =3D sh_tmu_setup(tmu, pdev); + ret =3D sh_tmu_setup(tmu, pdev, pdev->dev.of_node); if (ret) { kfree(tmu); pm_runtime_idle(&pdev->dev); @@ -631,7 +658,26 @@ static int sh_tmu_probe(struct platform_device *pdev) =20 return 0; } +#else +static int __init sh_tmu_of_register(struct device_node *np) +{ + struct sh_tmu_device *tmu; + int ret; + + tmu =3D kzalloc(sizeof(*tmu), GFP_KERNEL); + if (tmu =3D=3D NULL) + return -ENOMEM; + + ret =3D sh_tmu_setup(tmu, NULL, np); + if (ret) { + kfree(tmu); + pr_warn("%pOF: Timer register failed (%d)", np, ret); + } + return ret; +} +#endif =20 +#ifndef CONFIG_SH_DEVICE_TREE static const struct platform_device_id sh_tmu_id_table[] =3D { { "sh-tmu", SH_TMU }, { "sh-tmu-sh3", SH_TMU_SH3 }, @@ -665,12 +711,17 @@ static void __exit sh_tmu_exit(void) platform_driver_unregister(&sh_tmu_device_driver); } =20 +subsys_initcall(sh_tmu_init); +module_exit(sh_tmu_exit); +#endif + #ifdef CONFIG_SUPERH +#ifdef CONFIG_SH_DEVICE_TREE +TIMER_OF_DECLARE(sh_tmu, "renesas,tmu", sh_tmu_of_register); +#else sh_early_platform_init("earlytimer", &sh_tmu_device_driver); #endif - -subsys_initcall(sh_tmu_init); -module_exit(sh_tmu_exit); +#endif =20 MODULE_AUTHOR("Magnus Damm"); MODULE_DESCRIPTION("SuperH TMU Timer Driver"); --=20 2.39.2