From patchwork Tue May 30 21:51:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Belloni X-Patchwork-Id: 9755493 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 15951602F0 for ; Tue, 30 May 2017 22:03:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 08432283BA for ; Tue, 30 May 2017 22:03:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F18E52846B; Tue, 30 May 2017 22:03:36 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.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 A4890283BA for ; Tue, 30 May 2017 22:03:36 +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:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: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=f7wahDMvB1EmCdgNU3Sao9KI+7/kDG07dikbR1duB2U=; b=pfLITIIhv+xMZ5Q1AY9M5H/TBW p1bqQ4w1k1o/4yWzMpkhv+TKk1wfwP1B+lPtftBEIt7Py2nuYLw94ha9pU+kkEAJ7QMqDMaD6d5JX fthShGouT8i8F8KgfVblGkoGrezKyUEd/SWrlThnfrC2geEZZROMJ3kL5Gr+Lyiv+5yAG1dTWOg/G c8vK9M7fnJBaQD53wYwp6FU2yhuPwqF7Kvag+vT/KJ0y3NUrD00T0ws1vXFGMTag09OeCOomtpfAm TKAkhN1Is7Ovw5/cN+FcqUrNPnxdm1pY1hTn8DsNnETi7fKe7oKiW6VLpAVij3DXsyOTqLb7PHB/J klqqjrkg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dFpEk-000141-Tx; Tue, 30 May 2017 22:03:34 +0000 Received: from mail.free-electrons.com ([62.4.15.54]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dFp4N-0005I0-3D for linux-arm-kernel@lists.infradead.org; Tue, 30 May 2017 21:52:57 +0000 Received: by mail.free-electrons.com (Postfix, from userid 110) id 30A0720F81; Tue, 30 May 2017 23:52:38 +0200 (CEST) Received: from localhost (unknown [88.191.26.124]) by mail.free-electrons.com (Postfix) with ESMTPSA id D761620F38; Tue, 30 May 2017 23:52:27 +0200 (CEST) From: Alexandre Belloni To: Nicolas Ferre Subject: [PATCH 47/58] clocksource/drivers: timer-atmel-tcbclksrc: add clockevent device Date: Tue, 30 May 2017 23:51:28 +0200 Message-Id: <20170530215139.9983-48-alexandre.belloni@free-electrons.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170530215139.9983-1-alexandre.belloni@free-electrons.com> References: <20170530215139.9983-1-alexandre.belloni@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170530_145251_666391_C87EAC55 X-CRM114-Status: GOOD ( 15.50 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Boris Brezillon , Alexandre Belloni , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP It is possible to compare and get interrupts on the channel that is used for the clocksource. Enable that to have a solution using only one TCB channel. Signed-off-by: Alexandre Belloni --- drivers/clocksource/timer-atmel-tcbclksrc.c | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/drivers/clocksource/timer-atmel-tcbclksrc.c b/drivers/clocksource/timer-atmel-tcbclksrc.c index f18d177bfdea..462b04e9fed8 100644 --- a/drivers/clocksource/timer-atmel-tcbclksrc.c +++ b/drivers/clocksource/timer-atmel-tcbclksrc.c @@ -12,6 +12,7 @@ static struct atmel_tcb_clksrc { char name[20]; struct clocksource clksrc; + struct clock_event_device clkevt; struct regmap *regmap; struct clk *clk[2]; int channels[2]; @@ -24,6 +25,11 @@ static struct atmel_tcb_clksrc { .mask = CLOCKSOURCE_MASK(32), .flags = CLOCK_SOURCE_IS_CONTINUOUS, }, + .clkevt = { + .features = CLOCK_EVT_FEAT_ONESHOT, + /* Should be lower than at91rm9200's system timer */ + .rating = 125, + }, }; static u64 tc_get_cycles(struct clocksource *cs) @@ -58,6 +64,72 @@ static u64 notrace tc_sched_clock_read32(void) return tc_get_cycles32(&tc.clksrc); } +static int tcb_clkevt_next_event(unsigned long delta, + struct clock_event_device *d) +{ + u32 old, next, cur; + + + regmap_read(tc.regmap, ATMEL_TC_CV(tc.channels[0]), &old); + next = old + delta; + regmap_write(tc.regmap, ATMEL_TC_RC(tc.channels[0]), next); + regmap_read(tc.regmap, ATMEL_TC_CV(tc.channels[0]), &cur); + + /* check whether the delta elapsed while setting the register */ + if ((next < old && cur < old && cur > next) || + (next > old && (cur < old || cur > next))) { + /* + * Clear the CPCS bit in the status register to avoid + * generating a spurious interrupt next time a valid + * timer event is configured. + */ + regmap_read(tc.regmap, ATMEL_TC_SR(tc.channels[0]), &old); + return -ETIME; + } + + regmap_write(tc.regmap, ATMEL_TC_IER(tc.channels[0]), ATMEL_TC_CPCS); + + return 0; +} + +static irqreturn_t tc_clkevt_irq(int irq, void *handle) +{ + unsigned int sr; + + regmap_read(tc.regmap, ATMEL_TC_SR(tc.channels[0]), &sr); + if (sr & ATMEL_TC_CPCS) { + tc.clkevt.event_handler(&tc.clkevt); + return IRQ_HANDLED; + } + + return IRQ_NONE; +} + +static int tcb_clkevt_oneshot(struct clock_event_device *dev) +{ + if (clockevent_state_oneshot(dev)) + return 0; + + /* + * Because both clockevent devices may share the same IRQ, we don't want + * the less likely one to stay requested + */ + return request_irq(tc.irq, tc_clkevt_irq, IRQF_TIMER | IRQF_SHARED, + tc.name, &tc); +} + +static int tcb_clkevt_shutdown(struct clock_event_device *dev) +{ + regmap_write(tc.regmap, ATMEL_TC_IDR(tc.channels[0]), 0xff); + if (tc.bits == 16) + regmap_write(tc.regmap, ATMEL_TC_IDR(tc.channels[1]), 0xff); + + if (!clockevent_state_detached(dev)) + free_irq(tc.irq, &tc); + + return 0; +} + static void __init tcb_setup_dual_chan(struct atmel_tcb_clksrc *tc, int mck_divisor_idx) { @@ -188,6 +260,15 @@ static int __init tcb_clksrc_register(struct device_node *node, tc.registered = true; + /* Set up and register clockevents */ + tc.clkevt.name = tc.name; + tc.clkevt.cpumask = cpumask_of(0); + tc.clkevt.set_next_event = tcb_clkevt_next_event; + tc.clkevt.set_state_oneshot = tcb_clkevt_oneshot; + tc.clkevt.set_state_shutdown = tcb_clkevt_shutdown; + clockevents_config_and_register(&tc.clkevt, divided_rate, 1, + BIT(tc.bits) - 1); + return 0; err_disable_t1: