Received: by 2002:ab2:2441:0:b0:1f3:1f8c:d0c6 with SMTP id k1csp52285lqe; Wed, 3 Apr 2024 22:01:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXS9DnkjReeMDu93LWG1xuF67SzkO3nQ+FSujP3GDLpo50iqS77lGt3UVCKM7TcGDEMEZSVN+RF9E2bK5mTmK9n+Nl2fMneRT6ZS/ivKQ== X-Google-Smtp-Source: AGHT+IHby/4SpxU/LtnO6PsmondsNjSHxGkg+xLAsTSalTMa5vQhGA8wM1K7EU6VBnvNInQ+n2dC X-Received: by 2002:a05:6808:1707:b0:3c4:f52a:b878 with SMTP id bc7-20020a056808170700b003c4f52ab878mr1620885oib.8.1712206913136; Wed, 03 Apr 2024 22:01:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712206913; cv=pass; d=google.com; s=arc-20160816; b=Xe2dLBm+8IhnT2+79fTzwaghverot43ZE3CpBJ2TY1xVuSm7L/IjhBzhHlTEXknG1b n53lEaywAtNaYm9W4NwsbLOauFnAeU7nNXp5R03UPlvdcRqpFuEw2i3MIRLicKkQVLhB bCbHQ2JgE/H+c5XT99le3IwFqQm7EPvlgCigWG6aODu3z6BFtlCFIXRSl5rlAPMjQAvu D5g12zA7bpq7QypkcYNeaBQzUVol7SqEAV3GgfIOhTYGN9arWs6srTVxijarSJalIWvW fI6zVG1rd2oBEy4s+bSU//Rdh5D31MlbyjqxbrExVXTWeIbP1RNB+hfoJJGu07SuMvFP cVYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=mgxMFGMLiyvRHocrY7mys4FJGn03uvkeqEMF4FdYaC4=; fh=v4XjIS8ZnkfRSzpdvZ0kX2qBiNaW+cViNlbs+XMLZ44=; b=zE2qqamgnjBd0OiA/HPAh0eB+Z0Ng3nZTZXTZbDT27+QLO1CtOz0PZmIGGeVysCuHv cKyuAVqIUGwbNzxCn2Tw4M3VR979X6JSzUo09l9PfGfeUb4WbPcAga20J3XDC+bTendY 6feXtOOfph3ge/O9+iebs7sN7kfDO5otHvdvO9JtMGBmncYWRUdIMm1CTmXaEKVbTzHx Lw9MrT7g28ru+OKc/Zr+pGkTa3nI4iT0nzAugoVRE8pNH1/QCAk03KTmyqT28tuCiAjQ 0czi0UgiBdx0Bc6tfhWOXl7hhyM2b5/Wg+Z4RW0bEX8uzxYH/CN3WIekWs349yRGSDbo Nrkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-130875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130875-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v20-20020a05620a0f1400b0078be9dd4553si7107230qkl.419.2024.04.03.22.01.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 22:01:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-130875-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130875-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8E55A1C21DE4 for ; Thu, 4 Apr 2024 05:01:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67231524A5; Thu, 4 Apr 2024 05:00:54 +0000 (UTC) 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 303CD45C06; Thu, 4 Apr 2024 05:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=153.127.30.23 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712206853; cv=none; b=ON37B7/dyEBiNXHaOSp9OMXtveiAynih/pncXLY5ewCCWndZ3TQtaq7n7qh1ZQYSdtDJMKflzBXplBZTp0+pfFyOO6P2UOtnxwceWv20MKMAINrqYv8xOxP42CIotpslk1kuoPvCFQ2qzLl2zxRTOZ2Nwe2b1kGPUV+iQqpOHSE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712206853; c=relaxed/simple; bh=AS26eaglS7RJBQUnl7T6NslU0tdNz/37Vg4rHtcxbk4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SnKaSDP3pJOUufwcb9GDPjKgLRfSzWOQY+4B7aIQIVNd6SeUz3SrkQuJ6nI0uhH8sh5+w2UdGZeqg2WMUuIDm7GIIHBUzqS4whkxg3XiBgBEtrnSoFFYgZyl1dLMkWuwMta7KVfypp7RjOJUMMqYPSSlPiuaKZ7X5cHIEmHPj4c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=users.sourceforge.jp; spf=fail smtp.mailfrom=users.sourceforge.jp; arc=none smtp.client-ip=153.127.30.23 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 (al128006.dynamic.ppp.asahi-net.or.jp [111.234.128.6]) by sakura.ysato.name (Postfix) with ESMTPSA id 415841C0675; Thu, 4 Apr 2024 14:00:49 +0900 (JST) From: Yoshinori Sato To: linux-sh@vger.kernel.org Cc: Yoshinori Sato , Damien Le Moal , Niklas Cassel , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Geert Uytterhoeven , Michael Turquette , Stephen Boyd , David Airlie , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Thomas Gleixner , Bjorn Helgaas , Lorenzo Pieralisi , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Greg Kroah-Hartman , Jiri Slaby , Magnus Damm , Daniel Lezcano , Rich Felker , John Paul Adrian Glaubitz , Lee Jones , Helge Deller , Heiko Stuebner , Shawn Guo , Sebastian Reichel , Chris Morgan , Linus Walleij , Arnd Bergmann , David Rientjes , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Vlastimil Babka , Baoquan He , Andrew Morton , Guenter Roeck , Kefeng Wang , Stephen Rothwell , Javier Martinez Canillas , Guo Ren , Azeem Shaikh , Max Filippov , Jonathan Corbet , Jacky Huang , Herve Codina , Manikanta Guntupalli , Anup Patel , Biju Das , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Sam Ravnborg , Sergey Shtylyov , 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: [PATCH v7 08/37] clocksource: sh_tmu: CLOCKSOURCE support. Date: Thu, 4 Apr 2024 13:59:33 +0900 Message-Id: <4a9b59733c7a8e7d042f3987ca6bf601eea5b30d.1712205900.git.ysato@users.sourceforge.jp> 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: 8bit Allows initialization as CLOCKSOURCE. Signed-off-by: Yoshinori Sato --- drivers/clocksource/sh_tmu.c | 198 ++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 64 deletions(-) diff --git a/drivers/clocksource/sh_tmu.c b/drivers/clocksource/sh_tmu.c index beffff81c00f..59f9da7fd987 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 @@ -51,6 +53,7 @@ struct sh_tmu_channel { struct sh_tmu_device { struct platform_device *pdev; + struct device_node *np; void __iomem *mapbase; struct clk *clk; @@ -65,6 +68,7 @@ struct sh_tmu_device { bool has_clockevent; bool has_clocksource; + const char *name; }; #define TSTR -1 /* shared register */ @@ -148,8 +152,8 @@ static int __sh_tmu_enable(struct sh_tmu_channel *ch) /* enable clock */ ret = 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; } @@ -174,9 +178,10 @@ static int sh_tmu_enable(struct sh_tmu_channel *ch) if (ch->enable_count++ > 0) return 0; - 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); } @@ -202,8 +207,10 @@ static void sh_tmu_disable(struct sh_tmu_channel *ch) __sh_tmu_disable(ch); - 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); + } } static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta, @@ -245,7 +252,7 @@ static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id) return IRQ_HANDLED; } -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 +299,8 @@ static void sh_tmu_clocksource_suspend(struct clocksource *cs) if (--ch->enable_count == 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); } } @@ -304,7 +312,8 @@ static void sh_tmu_clocksource_resume(struct clocksource *cs) return; if (ch->enable_count++ == 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 +333,14 @@ static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch, cs->mask = CLOCKSOURCE_MASK(32); cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; - 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); clocksource_register_hz(cs, ch->tmu->rate); return 0; } -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_device *ced) { return container_of(ced, struct sh_tmu_channel, ced); } @@ -364,8 +373,8 @@ static int sh_tmu_clock_event_set_state(struct clock_event_device *ced, if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced)) sh_tmu_disable(ch); - 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; } @@ -417,20 +426,22 @@ static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch, ced->set_state_shutdown = sh_tmu_clock_event_shutdown; ced->set_state_periodic = sh_tmu_clock_event_set_periodic; ced->set_state_oneshot = sh_tmu_clock_event_set_oneshot; - ced->suspend = sh_tmu_clock_event_suspend; - ced->resume = sh_tmu_clock_event_resume; + if (ch->tmu->pdev) { + ced->suspend = sh_tmu_clock_event_suspend; + ced->resume = sh_tmu_clock_event_resume; + } - dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n", - ch->index); + pr_info("%s ch%u: used for clock events\n", + ch->tmu->name, ch->index); clockevents_config_and_register(ced, ch->tmu->rate, 0x300, 0xffffffff); ret = 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; } } @@ -465,28 +476,36 @@ static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index, else ch->base = tmu->mapbase + 8 + ch->index * 12; - ch->irq = platform_get_irq(tmu->pdev, index); + if (tmu->pdev) + ch->irq = platform_get_irq(tmu->pdev, index); + if (tmu->np) + ch->irq = of_irq_get(tmu->np, index); + if (ch->irq < 0) return ch->irq; ch->cs_enabled = false; ch->enable_count = 0; - return sh_tmu_register(ch, dev_name(&tmu->pdev->dev), - clockevent, clocksource); + return sh_tmu_register(ch, tmu->name, clockevent, clocksource); } static int sh_tmu_map_memory(struct sh_tmu_device *tmu) { struct resource *res; - res = 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 = 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 = ioremap(res->start, resource_size(res)); } + if (tmu->np) + tmu->mapbase = of_iomap(tmu->np, 0); - tmu->mapbase = ioremap(res->start, resource_size(res)); if (tmu->mapbase == NULL) return -ENXIO; @@ -495,7 +514,12 @@ static int sh_tmu_map_memory(struct sh_tmu_device *tmu) static int sh_tmu_parse_dt(struct sh_tmu_device *tmu) { - struct device_node *np = tmu->pdev->dev.of_node; + struct device_node *np; + + if (tmu->pdev) + np = tmu->pdev->dev.of_node; + if (tmu->np) + np = tmu->np; tmu->model = SH_TMU; tmu->num_channels = 3; @@ -503,45 +527,19 @@ static int sh_tmu_parse_dt(struct sh_tmu_device *tmu) of_property_read_u32(np, "#renesas,channels", &tmu->num_channels); if (tmu->num_channels != 2 && tmu->num_channels != 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; } return 0; } -static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) +static int sh_tmu_setup(struct sh_tmu_device *tmu) { unsigned int i; int ret; - tmu->pdev = pdev; - - raw_spin_lock_init(&tmu->lock); - - if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { - ret = sh_tmu_parse_dt(tmu); - if (ret < 0) - return ret; - } else if (pdev->dev.platform_data) { - const struct platform_device_id *id = pdev->id_entry; - struct sh_timer_config *cfg = pdev->dev.platform_data; - - tmu->model = id->driver_data; - tmu->num_channels = hweight8(cfg->channels_mask); - } else { - dev_err(&tmu->pdev->dev, "missing platform data\n"); - return -ENXIO; - } - - /* Get hold of clock. */ - tmu->clk = clk_get(&tmu->pdev->dev, "fck"); - if (IS_ERR(tmu->clk)) { - dev_err(&tmu->pdev->dev, "cannot get clock\n"); - return PTR_ERR(tmu->clk); - } - ret = clk_prepare(tmu->clk); if (ret < 0) goto err_clk_put; @@ -557,7 +555,7 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) /* Map the memory resource. */ ret = sh_tmu_map_memory(tmu); 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; } @@ -580,8 +578,6 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) goto err_unmap; } - platform_set_drvdata(pdev, tmu); - return 0; err_unmap: @@ -594,6 +590,39 @@ static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) return ret; } +static int sh_tmu_setup_pdev(struct sh_tmu_device *tmu, struct platform_device *pdev) +{ + int ret; + + tmu->pdev = pdev; + + raw_spin_lock_init(&tmu->lock); + + if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { + ret = sh_tmu_parse_dt(tmu); + if (ret < 0) + return ret; + } else if (pdev->dev.platform_data) { + const struct platform_device_id *id = pdev->id_entry; + struct sh_timer_config *cfg = pdev->dev.platform_data; + + tmu->model = id->driver_data; + tmu->num_channels = hweight8(cfg->channels_mask); + } else { + dev_err(&tmu->pdev->dev, "missing platform data\n"); + return -ENXIO; + } + + tmu->name = dev_name(&pdev->dev); + tmu->clk = clk_get(&tmu->pdev->dev, "fck"); + if (IS_ERR(tmu->clk)) { + dev_err(&tmu->pdev->dev, "cannot get clock\n"); + return PTR_ERR(tmu->clk); + } + + return sh_tmu_setup(tmu); +} + static int sh_tmu_probe(struct platform_device *pdev) { struct sh_tmu_device *tmu = platform_get_drvdata(pdev); @@ -613,12 +642,13 @@ static int sh_tmu_probe(struct platform_device *pdev) if (tmu == NULL) return -ENOMEM; - ret = sh_tmu_setup(tmu, pdev); + ret = sh_tmu_setup_pdev(tmu, pdev); if (ret) { kfree(tmu); pm_runtime_idle(&pdev->dev); return ret; } + platform_set_drvdata(pdev, tmu); if (is_sh_early_platform_device(pdev)) return 0; @@ -632,6 +662,45 @@ static int sh_tmu_probe(struct platform_device *pdev) return 0; } +static int sh_tmu_setup_of(struct sh_tmu_device *tmu, struct device_node *np) +{ + int ret; + + tmu->np = np; + raw_spin_lock_init(&tmu->lock); + + ret = sh_tmu_parse_dt(tmu); + if (ret < 0) + return ret; + + tmu->clk = of_clk_get(np, 0); + tmu->name = of_node_full_name(np); + + if (IS_ERR(tmu->clk)) { + pr_err("%pOF: cannot get clock\n", np); + return PTR_ERR(tmu->clk); + } + + return sh_tmu_setup(tmu); +} + +static int __init sh_tmu_of_register(struct device_node *np) +{ + struct sh_tmu_device *tmu; + int ret; + + tmu = kzalloc(sizeof(*tmu), GFP_KERNEL); + if (tmu == NULL) + return -ENOMEM; + + ret = sh_tmu_setup_of(tmu, np); + if (ret) { + kfree(tmu); + pr_warn("%pOF: Timer register failed (%d)", np, ret); + } + return ret; +} + static const struct platform_device_id sh_tmu_id_table[] = { { "sh-tmu", SH_TMU }, { "sh-tmu-sh3", SH_TMU_SH3 }, @@ -665,6 +734,7 @@ static void __exit sh_tmu_exit(void) platform_driver_unregister(&sh_tmu_device_driver); } +TIMER_OF_DECLARE(sh_tmu, "renesas,tmu", sh_tmu_of_register); #ifdef CONFIG_SUPERH sh_early_platform_init("earlytimer", &sh_tmu_device_driver); #endif -- 2.39.2