From patchwork Wed Jul 4 14:50:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanley Chu X-Patchwork-Id: 10507183 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7FB7B600F5 for ; Wed, 4 Jul 2018 15:05:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DE5C289B9 for ; Wed, 4 Jul 2018 15:05:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61D1128D83; Wed, 4 Jul 2018 15:05:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BBDC5289B9 for ; Wed, 4 Jul 2018 15:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xkRyXh2NAJr3rmu+tTMLq66ejnaxElNE+dx54KU/tL8=; b=rGLL+egggflIi3 MXmjXaeTMBJkVdGEwUW9CCqZS4oFf/lxZYRjmpXDFiLoAHs9KLf0bshq5lVQs5ddqNF5rObiwb9YY CDfmuF/i7A5MQuLZwUZ37hfipGU4UEz6gFYbbDPe3TEUaJN7H3nTWmXjx8zsMYXKbP6t7cJ1iHNjK 15kefwpHburBTJHJliygpfs0+a7P4Bpv/hv8Fuwj5ih6yXtQf/lEhTqg9N4R8e8PjmaPvrgiLhVBu m8M4FBvZilKlzCUS9levGLUGAJjRNJtMb5yt+qeyirgBqPYstp4BJqVrvnfNGTsbCqplWj86C6k0j NGmj37nsdPLfkSrv6MWQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fajLr-0003PR-U8 for patchwork-linux-mediatek@patchwork.kernel.org; Wed, 04 Jul 2018 15:05:51 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fajLp-0001vO-0a for linux-mediatek@bombadil.infradead.org; Wed, 04 Jul 2018 15:05:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=W/bjJB/BsfyVtCnvDiVxF+CFUJHW/lcd2m9w8G7DXZo=; b=rRToNZDhK/jr1xjN+RwYFoEWP mqntHSfhc4ybRsCuvXJmuGrHhHB2pAc3tn2rZQkSTdiUd9lJhXwdpgqbhbSPigEkZTH7C8ihYCHXh 4zOvKFH1DwjRUyNGLK//d806r2iMIIyLWKY1O4LxGRPNYkaJE+4qktvHI5i8UcO2haB9HuMaFMUeO ZrT54FlrkYC881zF3kYgfxOsnHe/ZFttc+wnnBk6Fn700o5U2AX/Xwr2Y8JMDeokQaaawPxGvI7Ax eNj0b9bXdRTz8vSoodTFp7p1tuWa7byyazHH9b20sW5BtRlmZ9Tp1PVlmYuh0o6njEV8tHI8fXF8u Df549Zlsg==; Received: from [210.61.82.183] (helo=mailgw01.mediatek.com) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1faj7b-0007FB-DO for linux-mediatek@lists.infradead.org; Wed, 04 Jul 2018 14:51:10 +0000 X-UUID: 9f726b09acb14334bc97cb77ff888140-20180704 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 2068433798; Wed, 04 Jul 2018 22:50:34 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs01n2.mediatek.inc (172.21.101.79) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Wed, 4 Jul 2018 22:50:32 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Wed, 4 Jul 2018 22:50:32 +0800 From: Stanley Chu To: Matthias Brugger , Daniel Lezcano , Thomas Gleixner , Rob Herring Subject: [PATCH v6 4/5] clocksource/drivers/timer-mediatek: Convert the driver to timer-of Date: Wed, 4 Jul 2018 22:50:19 +0800 Message-ID: <1530715820-3406-5-git-send-email-stanley.chu@mediatek.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1530715820-3406-1-git-send-email-stanley.chu@mediatek.com> References: <1530715820-3406-1-git-send-email-stanley.chu@mediatek.com> MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180704_155108_033761_B3FDD2FE X-CRM114-Status: GOOD ( 21.47 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, Stanley Chu , linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, wsd_upstream@mediatek.com Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Convert the driver to use the timer_of helpers. This allows to remove custom proprietary structure, factors out and simplifies the code. Signed-off-by: Stanley Chu --- drivers/clocksource/timer-mediatek.c | 205 +++++++++++++--------------------- 1 file changed, 80 insertions(+), 125 deletions(-) diff --git a/drivers/clocksource/timer-mediatek.c b/drivers/clocksource/timer-mediatek.c index e3657d2..e57c4d7 100644 --- a/drivers/clocksource/timer-mediatek.c +++ b/drivers/clocksource/timer-mediatek.c @@ -18,16 +18,13 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt -#include #include +#include #include -#include #include -#include -#include -#include #include #include +#include "timer-of.h" #define TIMER_CLK_EVT (1) #define TIMER_CLK_SRC (2) @@ -59,12 +56,6 @@ #define GPT_CNT_REG(val) (0x08 + (0x10 * (val))) #define GPT_CMP_REG(val) (0x0C + (0x10 * (val))) -struct mtk_clock_event_device { - void __iomem *gpt_base; - u32 ticks_per_jiffy; - struct clock_event_device dev; -}; - static void __iomem *gpt_sched_reg __read_mostly; static u64 notrace mtk_gpt_read_sched_clock(void) @@ -72,36 +63,30 @@ static u64 notrace mtk_gpt_read_sched_clock(void) return readl_relaxed(gpt_sched_reg); } -static inline struct mtk_clock_event_device *to_mtk_clk( - struct clock_event_device *c) -{ - return container_of(c, struct mtk_clock_event_device, dev); -} - -static void mtk_gpt_clkevt_time_stop(struct mtk_clock_event_device *evt, u8 timer) +static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer) { u32 val; - val = readl(evt->gpt_base + GPT_CTRL_REG(timer)); - writel(val & ~GPT_CTRL_ENABLE, evt->gpt_base + - GPT_CTRL_REG(timer)); + val = readl(timer_of_base(to) + GPT_CTRL_REG(timer)); + writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) + + GPT_CTRL_REG(timer)); } -static void mtk_gpt_clkevt_time_setup(struct mtk_clock_event_device *evt, - unsigned long delay, u8 timer) +static void mtk_gpt_clkevt_time_setup(struct timer_of *to, + unsigned long delay, u8 timer) { - writel(delay, evt->gpt_base + GPT_CMP_REG(timer)); + writel(delay, timer_of_base(to) + GPT_CMP_REG(timer)); } -static void mtk_gpt_clkevt_time_start(struct mtk_clock_event_device *evt, - bool periodic, u8 timer) +static void mtk_gpt_clkevt_time_start(struct timer_of *to, + bool periodic, u8 timer) { u32 val; /* Acknowledge interrupt */ - writel(GPT_IRQ_ACK(timer), evt->gpt_base + GPT_IRQ_ACK_REG); + writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG); - val = readl(evt->gpt_base + GPT_CTRL_REG(timer)); + val = readl(timer_of_base(to) + GPT_CTRL_REG(timer)); /* Clear 2 bit timer operation mode field */ val &= ~GPT_CTRL_OP(0x3); @@ -112,160 +97,130 @@ static void mtk_gpt_clkevt_time_start(struct mtk_clock_event_device *evt, val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT); writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR, - evt->gpt_base + GPT_CTRL_REG(timer)); + timer_of_base(to) + GPT_CTRL_REG(timer)); } static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk) { - mtk_gpt_clkevt_time_stop(to_mtk_clk(clk), TIMER_CLK_EVT); + mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT); + return 0; } static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk) { - struct mtk_clock_event_device *evt = to_mtk_clk(clk); + struct timer_of *to = to_timer_of(clk); + + mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT); + mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT); + mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT); - mtk_gpt_clkevt_time_stop(evt, TIMER_CLK_EVT); - mtk_gpt_clkevt_time_setup(evt, evt->ticks_per_jiffy, TIMER_CLK_EVT); - mtk_gpt_clkevt_time_start(evt, true, TIMER_CLK_EVT); return 0; } static int mtk_gpt_clkevt_next_event(unsigned long event, - struct clock_event_device *clk) + struct clock_event_device *clk) { - struct mtk_clock_event_device *evt = to_mtk_clk(clk); + struct timer_of *to = to_timer_of(clk); - mtk_gpt_clkevt_time_stop(evt, TIMER_CLK_EVT); - mtk_gpt_clkevt_time_setup(evt, event, TIMER_CLK_EVT); - mtk_gpt_clkevt_time_start(evt, false, TIMER_CLK_EVT); + mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT); + mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT); + mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT); return 0; } static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id) { - struct mtk_clock_event_device *evt = dev_id; + struct clock_event_device *clkevt = (struct clock_event_device *)dev_id; + struct timer_of *to = to_timer_of(clkevt); /* Acknowledge timer0 irq */ - writel(GPT_IRQ_ACK(TIMER_CLK_EVT), evt->gpt_base + GPT_IRQ_ACK_REG); - evt->dev.event_handler(&evt->dev); + writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG); + clkevt->event_handler(clkevt); return IRQ_HANDLED; } static void -__init mtk_gpt_setup(struct mtk_clock_event_device *evt, u8 timer, u8 option) +__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option) { writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE, - evt->gpt_base + GPT_CTRL_REG(timer)); + timer_of_base(to) + GPT_CTRL_REG(timer)); writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1, - evt->gpt_base + GPT_CLK_REG(timer)); + timer_of_base(to) + GPT_CLK_REG(timer)); - writel(0x0, evt->gpt_base + GPT_CMP_REG(timer)); + writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer)); writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE, - evt->gpt_base + GPT_CTRL_REG(timer)); + timer_of_base(to) + GPT_CTRL_REG(timer)); } -static void mtk_gpt_enable_irq(struct mtk_clock_event_device *evt, u8 timer) +static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer) { u32 val; /* Disable all interrupts */ - writel(0x0, evt->gpt_base + GPT_IRQ_EN_REG); + writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG); /* Acknowledge all spurious pending interrupts */ - writel(0x3f, evt->gpt_base + GPT_IRQ_ACK_REG); + writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG); - val = readl(evt->gpt_base + GPT_IRQ_EN_REG); + val = readl(timer_of_base(to) + GPT_IRQ_EN_REG); writel(val | GPT_IRQ_ENABLE(timer), - evt->gpt_base + GPT_IRQ_EN_REG); + timer_of_base(to) + GPT_IRQ_EN_REG); } +static struct timer_of to = { + .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK, + + .clkevt = { + .name = "mtk-clkevt", + .rating = 300, + .cpumask = cpu_possible_mask, + }, + + .of_irq = { + .flags = IRQF_TIMER | IRQF_IRQPOLL, + }, +}; + static int __init mtk_gpt_init(struct device_node *node) { - struct mtk_clock_event_device *evt; - struct resource res; - unsigned long rate = 0; - struct clk *clk; - - evt = kzalloc(sizeof(*evt), GFP_KERNEL); - if (!evt) - return -ENOMEM; - - evt->dev.name = "mtk_tick"; - evt->dev.rating = 300; - evt->dev.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; - evt->dev.set_state_shutdown = mtk_gpt_clkevt_shutdown; - evt->dev.set_state_periodic = mtk_gpt_clkevt_set_periodic; - evt->dev.set_state_oneshot = mtk_gpt_clkevt_shutdown; - evt->dev.tick_resume = mtk_gpt_clkevt_shutdown; - evt->dev.set_next_event = mtk_gpt_clkevt_next_event; - evt->dev.cpumask = cpu_possible_mask; - - evt->gpt_base = of_io_request_and_map(node, 0, "mtk-timer-gpt"); - if (IS_ERR(evt->gpt_base)) { - pr_err("Can't get resource\n"); - goto err_kzalloc; - } - - evt->dev.irq = irq_of_parse_and_map(node, 0); - if (evt->dev.irq <= 0) { - pr_err("Can't parse IRQ\n"); - goto err_mem; - } - - clk = of_clk_get(node, 0); - if (IS_ERR(clk)) { - pr_err("Can't get timer clock\n"); - goto err_irq; - } - - if (clk_prepare_enable(clk)) { - pr_err("Can't prepare clock\n"); - goto err_clk_put; - } - rate = clk_get_rate(clk); - - if (request_irq(evt->dev.irq, mtk_gpt_interrupt, - IRQF_TIMER | IRQF_IRQPOLL, "mtk_timer", evt)) { - pr_err("failed to setup irq %d\n", evt->dev.irq); - goto err_clk_disable; - } - - evt->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ); + int ret; + + to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; + to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown; + to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic; + to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown; + to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown; + to.clkevt.set_next_event = mtk_gpt_clkevt_next_event; + to.of_irq.handler = mtk_gpt_interrupt; + + ret = timer_of_init(node, &to); + if (ret) + goto err; /* Configure clock source */ - mtk_gpt_setup(evt, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN); - clocksource_mmio_init(evt->gpt_base + GPT_CNT_REG(TIMER_CLK_SRC), - node->name, rate, 300, 32, clocksource_mmio_readl_up); - gpt_sched_reg = evt->gpt_base + GPT_CNT_REG(TIMER_CLK_SRC); - sched_clock_register(mtk_gpt_read_sched_clock, 32, rate); + mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN); + clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC), + node->name, timer_of_rate(&to), 300, 32, + clocksource_mmio_readl_up); + gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC); + sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to)); /* Configure clock event */ - mtk_gpt_setup(evt, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT); - clockevents_config_and_register(&evt->dev, rate, TIMER_SYNC_TICKS, - 0xffffffff); + mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT); + clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), + TIMER_SYNC_TICKS, 0xffffffff); - mtk_gpt_enable_irq(evt, TIMER_CLK_EVT); + mtk_gpt_enable_irq(&to, TIMER_CLK_EVT); return 0; -err_clk_disable: - clk_disable_unprepare(clk); -err_clk_put: - clk_put(clk); -err_irq: - irq_dispose_mapping(evt->dev.irq); -err_mem: - iounmap(evt->gpt_base); - of_address_to_resource(node, 0, &res); - release_mem_region(res.start, resource_size(&res)); -err_kzalloc: - kfree(evt); - - return -EINVAL; +err: + timer_of_cleanup(&to); + return ret; } TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);