blob: 9a0bbc455de719ec297b38f939cd8ede5225796c [file] [log] [blame]
Tony Lindgren92105bb2005-09-07 17:20:26 +01001/*
2 * linux/arch/arm/plat-omap/dmtimer.c
3 *
4 * OMAP Dual-Mode Timers
5 *
Tarun Kanti DebBarma97933d62011-09-20 17:00:17 +05306 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
7 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
8 * Thara Gopinath <thara@ti.com>
9 *
10 * dmtimer adaptation to platform_driver.
11 *
Tony Lindgren92105bb2005-09-07 17:20:26 +010012 * Copyright (C) 2005 Nokia Corporation
Timo Teras77900a22006-06-26 16:16:12 -070013 * OMAP2 support by Juha Yrjola
14 * API improvements and OMAP2 clock framework support by Timo Teras
Tony Lindgren92105bb2005-09-07 17:20:26 +010015 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070016 * Copyright (C) 2009 Texas Instruments
17 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
18 *
Tony Lindgren92105bb2005-09-07 17:20:26 +010019 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
27 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, write to the Free Software Foundation, Inc.,
35 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
Axel Lin869dec12011-11-02 09:49:46 +080038#include <linux/module.h>
Russell Kingfced80c2008-09-06 12:10:45 +010039#include <linux/io.h>
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +053040#include <linux/device.h>
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053041#include <linux/err.h>
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +053042#include <linux/pm_runtime.h>
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053043
Tony Lindgrence491cf2009-10-20 09:40:47 -070044#include <plat/dmtimer.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010045
Tony Lindgren2c799ce2012-02-24 10:34:35 -080046#include <mach/hardware.h>
47
Jon Hunterb7b4ff72012-06-05 12:34:51 -050048static u32 omap_reserved_systimers;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +053049static LIST_HEAD(omap_timer_list);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053050static DEFINE_SPINLOCK(dm_timer_lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +010051
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053052/**
53 * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode
54 * @timer: timer pointer over which read operation to perform
55 * @reg: lowest byte holds the register offset
56 *
57 * The posted mode bit is encoded in reg. Note that in posted mode write
58 * pending bit must be checked. Otherwise a read of a non completed write
59 * will produce an error.
Richard Woodruff0f0d0802008-07-03 12:24:30 +030060 */
61static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
Tony Lindgren92105bb2005-09-07 17:20:26 +010062{
Tony Lindgrenee17f112011-09-16 15:44:20 -070063 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
64 return __omap_dm_timer_read(timer, reg, timer->posted);
Timo Teras77900a22006-06-26 16:16:12 -070065}
66
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +053067/**
68 * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode
69 * @timer: timer pointer over which write operation is to perform
70 * @reg: lowest byte holds the register offset
71 * @value: data to write into the register
72 *
73 * The posted mode bit is encoded in reg. Note that in posted mode the write
74 * pending bit must be checked. Otherwise a write on a register which has a
75 * pending write will be lost.
Richard Woodruff0f0d0802008-07-03 12:24:30 +030076 */
77static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
78 u32 value)
Timo Teras77900a22006-06-26 16:16:12 -070079{
Tony Lindgrenee17f112011-09-16 15:44:20 -070080 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
81 __omap_dm_timer_write(timer, reg, value, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +010082}
83
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053084static void omap_timer_restore_context(struct omap_dm_timer *timer)
85{
Tarun Kanti DebBarmadffc9da2012-03-05 16:11:00 -080086 if (timer->revision == 1)
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +053087 __raw_writel(timer->context.tistat, timer->sys_stat);
88
89 __raw_writel(timer->context.tisr, timer->irq_stat);
90 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG,
91 timer->context.twer);
92 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG,
93 timer->context.tcrr);
94 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG,
95 timer->context.tldr);
96 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG,
97 timer->context.tmar);
98 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
99 timer->context.tsicr);
100 __raw_writel(timer->context.tier, timer->irq_ena);
101 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG,
102 timer->context.tclr);
103}
104
Timo Teras77900a22006-06-26 16:16:12 -0700105static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100106{
Timo Teras77900a22006-06-26 16:16:12 -0700107 int c;
108
Tony Lindgrenee17f112011-09-16 15:44:20 -0700109 if (!timer->sys_stat)
110 return;
111
Timo Teras77900a22006-06-26 16:16:12 -0700112 c = 0;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700113 while (!(__raw_readl(timer->sys_stat) & 1)) {
Timo Teras77900a22006-06-26 16:16:12 -0700114 c++;
115 if (c > 100000) {
116 printk(KERN_ERR "Timer failed to reset\n");
117 return;
118 }
119 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120}
121
Timo Teras77900a22006-06-26 16:16:12 -0700122static void omap_dm_timer_reset(struct omap_dm_timer *timer)
123{
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530124 omap_dm_timer_enable(timer);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530125 if (timer->pdev->id != 1) {
Timo Terase32f7ec2006-06-26 16:16:13 -0700126 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
127 omap_dm_timer_wait_for_reset(timer);
128 }
Timo Teras77900a22006-06-26 16:16:12 -0700129
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530130 __omap_dm_timer_reset(timer, 0, 0);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530131 omap_dm_timer_disable(timer);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300132 timer->posted = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700133}
134
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530135int omap_dm_timer_prepare(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700136{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530137 int ret;
138
Jon Hunterbca45802012-06-05 12:34:58 -0500139 /*
140 * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
141 * do not call clk_get() for these devices.
142 */
143 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
144 timer->fclk = clk_get(&timer->pdev->dev, "fck");
145 if (WARN_ON_ONCE(IS_ERR_OR_NULL(timer->fclk))) {
146 timer->fclk = NULL;
147 dev_err(&timer->pdev->dev, ": No fclk handle.\n");
148 return -EINVAL;
149 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530150 }
151
Jon Hunter66159752012-06-05 12:34:57 -0500152 if (timer->capability & OMAP_TIMER_NEEDS_RESET)
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530153 omap_dm_timer_reset(timer);
154
155 ret = omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
156
157 timer->posted = 1;
158 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700159}
160
Jon Hunterb7b4ff72012-06-05 12:34:51 -0500161static inline u32 omap_dm_timer_reserved_systimer(int id)
162{
163 return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
164}
165
166int omap_dm_timer_reserve_systimer(int id)
167{
168 if (omap_dm_timer_reserved_systimer(id))
169 return -ENODEV;
170
171 omap_reserved_systimers |= (1 << (id - 1));
172
173 return 0;
174}
175
Timo Teras77900a22006-06-26 16:16:12 -0700176struct omap_dm_timer *omap_dm_timer_request(void)
177{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530178 struct omap_dm_timer *timer = NULL, *t;
Timo Teras77900a22006-06-26 16:16:12 -0700179 unsigned long flags;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530180 int ret = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700181
182 spin_lock_irqsave(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530183 list_for_each_entry(t, &omap_timer_list, node) {
184 if (t->reserved)
Timo Teras77900a22006-06-26 16:16:12 -0700185 continue;
186
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530187 timer = t;
Timo Teras83379c82006-06-26 16:16:23 -0700188 timer->reserved = 1;
Timo Teras77900a22006-06-26 16:16:12 -0700189 break;
190 }
Timo Kokkonenc5491d12012-08-12 13:45:34 +0300191 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530192
193 if (timer) {
194 ret = omap_dm_timer_prepare(timer);
195 if (ret) {
196 timer->reserved = 0;
197 timer = NULL;
198 }
199 }
Timo Teras77900a22006-06-26 16:16:12 -0700200
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530201 if (!timer)
202 pr_debug("%s: timer request failed!\n", __func__);
Timo Teras83379c82006-06-26 16:16:23 -0700203
Timo Teras77900a22006-06-26 16:16:12 -0700204 return timer;
205}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700206EXPORT_SYMBOL_GPL(omap_dm_timer_request);
Timo Teras77900a22006-06-26 16:16:12 -0700207
208struct omap_dm_timer *omap_dm_timer_request_specific(int id)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100209{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530210 struct omap_dm_timer *timer = NULL, *t;
Timo Teras77900a22006-06-26 16:16:12 -0700211 unsigned long flags;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530212 int ret = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100213
Timo Teras77900a22006-06-26 16:16:12 -0700214 spin_lock_irqsave(&dm_timer_lock, flags);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530215 list_for_each_entry(t, &omap_timer_list, node) {
216 if (t->pdev->id == id && !t->reserved) {
217 timer = t;
218 timer->reserved = 1;
219 break;
220 }
Timo Teras77900a22006-06-26 16:16:12 -0700221 }
Timo Kokkonenc5491d12012-08-12 13:45:34 +0300222 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100223
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530224 if (timer) {
225 ret = omap_dm_timer_prepare(timer);
226 if (ret) {
227 timer->reserved = 0;
228 timer = NULL;
229 }
230 }
Timo Teras77900a22006-06-26 16:16:12 -0700231
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530232 if (!timer)
233 pr_debug("%s: timer%d request failed!\n", __func__, id);
Timo Teras83379c82006-06-26 16:16:23 -0700234
Timo Teras77900a22006-06-26 16:16:12 -0700235 return timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100236}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700237EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100238
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530239int omap_dm_timer_free(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700240{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530241 if (unlikely(!timer))
242 return -EINVAL;
243
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530244 clk_put(timer->fclk);
Timo Terasfa4bb622006-09-25 12:41:35 +0300245
Timo Teras77900a22006-06-26 16:16:12 -0700246 WARN_ON(!timer->reserved);
247 timer->reserved = 0;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530248 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700249}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700250EXPORT_SYMBOL_GPL(omap_dm_timer_free);
Timo Teras77900a22006-06-26 16:16:12 -0700251
Timo Teras12583a72006-09-25 12:41:42 +0300252void omap_dm_timer_enable(struct omap_dm_timer *timer)
253{
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530254 pm_runtime_get_sync(&timer->pdev->dev);
Timo Teras12583a72006-09-25 12:41:42 +0300255}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700256EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
Timo Teras12583a72006-09-25 12:41:42 +0300257
258void omap_dm_timer_disable(struct omap_dm_timer *timer)
259{
Jon Hunter54f32a32012-07-13 15:12:03 -0500260 pm_runtime_put_sync(&timer->pdev->dev);
Timo Teras12583a72006-09-25 12:41:42 +0300261}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700262EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
Timo Teras12583a72006-09-25 12:41:42 +0300263
Timo Teras77900a22006-06-26 16:16:12 -0700264int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
265{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530266 if (timer)
267 return timer->irq;
268 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700269}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700270EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
Timo Teras77900a22006-06-26 16:16:12 -0700271
272#if defined(CONFIG_ARCH_OMAP1)
273
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100274/**
275 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
276 * @inputmask: current value of idlect mask
277 */
278__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
279{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530280 int i = 0;
281 struct omap_dm_timer *timer = NULL;
282 unsigned long flags;
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100283
284 /* If ARMXOR cannot be idled this function call is unnecessary */
285 if (!(inputmask & (1 << 1)))
286 return inputmask;
287
288 /* If any active timer is using ARMXOR return modified mask */
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530289 spin_lock_irqsave(&dm_timer_lock, flags);
290 list_for_each_entry(timer, &omap_timer_list, node) {
Timo Teras77900a22006-06-26 16:16:12 -0700291 u32 l;
292
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530293 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras77900a22006-06-26 16:16:12 -0700294 if (l & OMAP_TIMER_CTRL_ST) {
295 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100296 inputmask &= ~(1 << 1);
297 else
298 inputmask &= ~(1 << 2);
299 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530300 i++;
Timo Teras77900a22006-06-26 16:16:12 -0700301 }
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530302 spin_unlock_irqrestore(&dm_timer_lock, flags);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100303
304 return inputmask;
305}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700306EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Tony Lindgrena569c6e2006-04-02 17:46:21 +0100307
Tony Lindgren140455f2010-02-12 12:26:48 -0800308#else
Timo Teras77900a22006-06-26 16:16:12 -0700309
310struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
311{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530312 if (timer)
313 return timer->fclk;
314 return NULL;
Timo Teras77900a22006-06-26 16:16:12 -0700315}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700316EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
Timo Teras77900a22006-06-26 16:16:12 -0700317
318__u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
319{
320 BUG();
Dirk Behme21218802006-12-06 17:14:00 -0800321
322 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700323}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700324EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
Timo Teras77900a22006-06-26 16:16:12 -0700325
326#endif
327
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530328int omap_dm_timer_trigger(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700329{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530330 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
331 pr_err("%s: timer not available or enabled.\n", __func__);
332 return -EINVAL;
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530333 }
334
Timo Teras77900a22006-06-26 16:16:12 -0700335 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530336 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700337}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700338EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
Timo Teras77900a22006-06-26 16:16:12 -0700339
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530340int omap_dm_timer_start(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700341{
342 u32 l;
343
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530344 if (unlikely(!timer))
345 return -EINVAL;
346
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530347 omap_dm_timer_enable(timer);
348
Jon Hunter1c2d0762012-06-05 12:34:55 -0500349 if (!(timer->capability & OMAP_TIMER_ALWON)) {
Tony Lindgren6e740f92012-10-29 15:20:45 -0700350 if (timer->get_context_loss_count &&
351 timer->get_context_loss_count(&timer->pdev->dev) !=
Jon Hunter0b30ec12012-06-05 12:34:56 -0500352 timer->ctx_loss_count)
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530353 omap_timer_restore_context(timer);
354 }
355
Timo Teras77900a22006-06-26 16:16:12 -0700356 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
357 if (!(l & OMAP_TIMER_CTRL_ST)) {
358 l |= OMAP_TIMER_CTRL_ST;
359 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
360 }
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530361
362 /* Save the context */
363 timer->context.tclr = l;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530364 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700365}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700366EXPORT_SYMBOL_GPL(omap_dm_timer_start);
Timo Teras77900a22006-06-26 16:16:12 -0700367
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530368int omap_dm_timer_stop(struct omap_dm_timer *timer)
Timo Teras77900a22006-06-26 16:16:12 -0700369{
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700370 unsigned long rate = 0;
Timo Teras77900a22006-06-26 16:16:12 -0700371
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530372 if (unlikely(!timer))
373 return -EINVAL;
374
Jon Hunter66159752012-06-05 12:34:57 -0500375 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530376 rate = clk_get_rate(timer->fclk);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700377
Tony Lindgrenee17f112011-09-16 15:44:20 -0700378 __omap_dm_timer_stop(timer, timer->posted, rate);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530379
Tony Lindgren6e740f92012-10-29 15:20:45 -0700380 if (!(timer->capability & OMAP_TIMER_ALWON)) {
381 if (timer->get_context_loss_count)
382 timer->ctx_loss_count =
383 timer->get_context_loss_count(&timer->pdev->dev);
384 }
Tarun Kanti DebBarmadffc9da2012-03-05 16:11:00 -0800385
386 /*
387 * Since the register values are computed and written within
388 * __omap_dm_timer_stop, we need to use read to retrieve the
389 * context.
390 */
391 timer->context.tclr =
392 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
393 timer->context.tisr = __raw_readl(timer->irq_stat);
394 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530395 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700396}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700397EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
Timo Teras77900a22006-06-26 16:16:12 -0700398
Paul Walmsleyf2480762009-04-23 21:11:10 -0600399int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530401 int ret;
Jon Hunter2b2d3522012-06-05 12:34:59 -0500402 char *parent_name = NULL;
403 struct clk *fclk, *parent;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530404 struct dmtimer_platform_data *pdata;
405
406 if (unlikely(!timer))
407 return -EINVAL;
408
409 pdata = timer->pdev->dev.platform_data;
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530410
Timo Teras77900a22006-06-26 16:16:12 -0700411 if (source < 0 || source >= 3)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600412 return -EINVAL;
Timo Teras77900a22006-06-26 16:16:12 -0700413
Jon Hunter2b2d3522012-06-05 12:34:59 -0500414 /*
415 * FIXME: Used for OMAP1 devices only because they do not currently
416 * use the clock framework to set the parent clock. To be removed
417 * once OMAP1 migrated to using clock framework for dmtimers
418 */
419 if (pdata->set_timer_src)
420 return pdata->set_timer_src(timer->pdev, source);
421
422 fclk = clk_get(&timer->pdev->dev, "fck");
423 if (IS_ERR_OR_NULL(fclk)) {
424 pr_err("%s: fck not found\n", __func__);
425 return -EINVAL;
426 }
427
428 switch (source) {
429 case OMAP_TIMER_SRC_SYS_CLK:
Jon Hunterc59b5372012-06-05 12:35:00 -0500430 parent_name = "timer_sys_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500431 break;
432
433 case OMAP_TIMER_SRC_32_KHZ:
Jon Hunterc59b5372012-06-05 12:35:00 -0500434 parent_name = "timer_32k_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500435 break;
436
437 case OMAP_TIMER_SRC_EXT_CLK:
Jon Hunterc59b5372012-06-05 12:35:00 -0500438 parent_name = "timer_ext_ck";
Jon Hunter2b2d3522012-06-05 12:34:59 -0500439 break;
440 }
441
442 parent = clk_get(&timer->pdev->dev, parent_name);
443 if (IS_ERR_OR_NULL(parent)) {
444 pr_err("%s: %s not found\n", __func__, parent_name);
445 ret = -EINVAL;
446 goto out;
447 }
448
449 ret = clk_set_parent(fclk, parent);
450 if (IS_ERR_VALUE(ret))
451 pr_err("%s: failed to set %s as parent\n", __func__,
452 parent_name);
453
454 clk_put(parent);
455out:
456 clk_put(fclk);
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530457
458 return ret;
Timo Teras77900a22006-06-26 16:16:12 -0700459}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700460EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
Timo Teras77900a22006-06-26 16:16:12 -0700461
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530462int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
Timo Teras77900a22006-06-26 16:16:12 -0700463 unsigned int load)
464{
465 u32 l;
466
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530467 if (unlikely(!timer))
468 return -EINVAL;
469
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530470 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700471 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
472 if (autoreload)
473 l |= OMAP_TIMER_CTRL_AR;
474 else
475 l &= ~OMAP_TIMER_CTRL_AR;
476 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
477 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
Richard Woodruff0f0d0802008-07-03 12:24:30 +0300478
Timo Teras77900a22006-06-26 16:16:12 -0700479 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530480 /* Save the context */
481 timer->context.tclr = l;
482 timer->context.tldr = load;
483 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530484 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700485}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700486EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
Timo Teras77900a22006-06-26 16:16:12 -0700487
Richard Woodruff3fddd092008-07-03 12:24:30 +0300488/* Optimized set_load which removes costly spin wait in timer_start */
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530489int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
Richard Woodruff3fddd092008-07-03 12:24:30 +0300490 unsigned int load)
491{
492 u32 l;
493
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530494 if (unlikely(!timer))
495 return -EINVAL;
496
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530497 omap_dm_timer_enable(timer);
498
Jon Hunter1c2d0762012-06-05 12:34:55 -0500499 if (!(timer->capability & OMAP_TIMER_ALWON)) {
Tony Lindgren6e740f92012-10-29 15:20:45 -0700500 if (timer->get_context_loss_count &&
501 timer->get_context_loss_count(&timer->pdev->dev) !=
Jon Hunter0b30ec12012-06-05 12:34:56 -0500502 timer->ctx_loss_count)
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530503 omap_timer_restore_context(timer);
504 }
505
Richard Woodruff3fddd092008-07-03 12:24:30 +0300506 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Paul Walmsley64ce2902008-12-10 17:36:34 -0800507 if (autoreload) {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300508 l |= OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800509 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
510 } else {
Richard Woodruff3fddd092008-07-03 12:24:30 +0300511 l &= ~OMAP_TIMER_CTRL_AR;
Paul Walmsley64ce2902008-12-10 17:36:34 -0800512 }
Richard Woodruff3fddd092008-07-03 12:24:30 +0300513 l |= OMAP_TIMER_CTRL_ST;
514
Tony Lindgrenee17f112011-09-16 15:44:20 -0700515 __omap_dm_timer_load_start(timer, l, load, timer->posted);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530516
517 /* Save the context */
518 timer->context.tclr = l;
519 timer->context.tldr = load;
520 timer->context.tcrr = load;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530521 return 0;
Richard Woodruff3fddd092008-07-03 12:24:30 +0300522}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700523EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
Richard Woodruff3fddd092008-07-03 12:24:30 +0300524
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530525int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
Timo Teras77900a22006-06-26 16:16:12 -0700526 unsigned int match)
527{
528 u32 l;
529
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530530 if (unlikely(!timer))
531 return -EINVAL;
532
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530533 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700534 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
Timo Teras83379c82006-06-26 16:16:23 -0700535 if (enable)
Timo Teras77900a22006-06-26 16:16:12 -0700536 l |= OMAP_TIMER_CTRL_CE;
537 else
538 l &= ~OMAP_TIMER_CTRL_CE;
539 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
540 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530541
542 /* Save the context */
543 timer->context.tclr = l;
544 timer->context.tmar = match;
545 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530546 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700548EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100549
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530550int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
Timo Teras77900a22006-06-26 16:16:12 -0700551 int toggle, int trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552{
Timo Teras77900a22006-06-26 16:16:12 -0700553 u32 l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530555 if (unlikely(!timer))
556 return -EINVAL;
557
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530558 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700559 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
560 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
561 OMAP_TIMER_CTRL_PT | (0x03 << 10));
562 if (def_on)
563 l |= OMAP_TIMER_CTRL_SCPWM;
564 if (toggle)
565 l |= OMAP_TIMER_CTRL_PT;
566 l |= trigger << 10;
567 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530568
569 /* Save the context */
570 timer->context.tclr = l;
571 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530572 return 0;
Timo Teras77900a22006-06-26 16:16:12 -0700573}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700574EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
Timo Teras77900a22006-06-26 16:16:12 -0700575
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530576int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
Timo Teras77900a22006-06-26 16:16:12 -0700577{
578 u32 l;
579
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530580 if (unlikely(!timer))
581 return -EINVAL;
582
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530583 omap_dm_timer_enable(timer);
Timo Teras77900a22006-06-26 16:16:12 -0700584 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
585 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
586 if (prescaler >= 0x00 && prescaler <= 0x07) {
587 l |= OMAP_TIMER_CTRL_PRE;
588 l |= prescaler << 2;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589 }
Timo Teras77900a22006-06-26 16:16:12 -0700590 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530591
592 /* Save the context */
593 timer->context.tclr = l;
594 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530595 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100596}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700597EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100598
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530599int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
Timo Teras77900a22006-06-26 16:16:12 -0700600 unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530602 if (unlikely(!timer))
603 return -EINVAL;
604
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530605 omap_dm_timer_enable(timer);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700606 __omap_dm_timer_int_enable(timer, value);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530607
608 /* Save the context */
609 timer->context.tier = value;
610 timer->context.twer = value;
611 omap_dm_timer_disable(timer);
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530612 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700614EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100615
616unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
617{
Timo Terasfa4bb622006-09-25 12:41:35 +0300618 unsigned int l;
619
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530620 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
621 pr_err("%s: timer not available or enabled.\n", __func__);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530622 return 0;
623 }
624
Tony Lindgrenee17f112011-09-16 15:44:20 -0700625 l = __raw_readl(timer->irq_stat);
Timo Terasfa4bb622006-09-25 12:41:35 +0300626
627 return l;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700629EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530631int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530633 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev)))
634 return -EINVAL;
635
Tony Lindgrenee17f112011-09-16 15:44:20 -0700636 __omap_dm_timer_write_status(timer, value);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530637 /* Save the context */
638 timer->context.tisr = value;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530639 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100640}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700641EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
644{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530645 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
646 pr_err("%s: timer not iavailable or enabled.\n", __func__);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530647 return 0;
648 }
649
Tony Lindgrenee17f112011-09-16 15:44:20 -0700650 return __omap_dm_timer_read_counter(timer, timer->posted);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100651}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700652EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100653
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530654int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
Timo Teras83379c82006-06-26 16:16:23 -0700655{
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530656 if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
657 pr_err("%s: timer not available or enabled.\n", __func__);
658 return -EINVAL;
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530659 }
660
Timo Terasfa4bb622006-09-25 12:41:35 +0300661 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
Tarun Kanti DebBarmab4811132011-09-20 17:00:24 +0530662
663 /* Save the context */
664 timer->context.tcrr = value;
Tarun Kanti DebBarmaab4eb8b2011-09-20 17:00:26 +0530665 return 0;
Timo Teras83379c82006-06-26 16:16:23 -0700666}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700667EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
Timo Teras83379c82006-06-26 16:16:23 -0700668
Timo Teras77900a22006-06-26 16:16:12 -0700669int omap_dm_timers_active(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100670{
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530671 struct omap_dm_timer *timer;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672
Tarun Kanti DebBarma3392cdd2011-09-20 17:00:20 +0530673 list_for_each_entry(timer, &omap_timer_list, node) {
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530674 if (!timer->reserved)
Timo Teras12583a72006-09-25 12:41:42 +0300675 continue;
676
Timo Teras77900a22006-06-26 16:16:12 -0700677 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
Timo Terasfa4bb622006-09-25 12:41:35 +0300678 OMAP_TIMER_CTRL_ST) {
Timo Teras77900a22006-06-26 16:16:12 -0700679 return 1;
Timo Terasfa4bb622006-09-25 12:41:35 +0300680 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100681 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682 return 0;
683}
Timo Kokkonen6c366e32009-03-23 18:07:46 -0700684EXPORT_SYMBOL_GPL(omap_dm_timers_active);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530686/**
687 * omap_dm_timer_probe - probe function called for every registered device
688 * @pdev: pointer to current timer platform device
689 *
690 * Called by driver framework at the end of device registration for all
691 * timer devices.
692 */
693static int __devinit omap_dm_timer_probe(struct platform_device *pdev)
694{
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530695 unsigned long flags;
696 struct omap_dm_timer *timer;
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530697 struct resource *mem, *irq;
698 struct device *dev = &pdev->dev;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530699 struct dmtimer_platform_data *pdata = pdev->dev.platform_data;
700
701 if (!pdata) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530702 dev_err(dev, "%s: no platform data.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530703 return -ENODEV;
704 }
705
706 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
707 if (unlikely(!irq)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530708 dev_err(dev, "%s: no IRQ resource.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530709 return -ENODEV;
710 }
711
712 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
713 if (unlikely(!mem)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530714 dev_err(dev, "%s: no memory resource.\n", __func__);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530715 return -ENODEV;
716 }
717
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530718 timer = devm_kzalloc(dev, sizeof(struct omap_dm_timer), GFP_KERNEL);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530719 if (!timer) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530720 dev_err(dev, "%s: memory alloc failed!\n", __func__);
721 return -ENOMEM;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530722 }
723
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530724 timer->io_base = devm_request_and_ioremap(dev, mem);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530725 if (!timer->io_base) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530726 dev_err(dev, "%s: region already claimed.\n", __func__);
727 return -ENOMEM;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530728 }
729
730 timer->id = pdev->id;
731 timer->irq = irq->start;
Jon Hunterb7b4ff72012-06-05 12:34:51 -0500732 timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530733 timer->pdev = pdev;
Jon Hunterd1c16912012-06-05 12:34:52 -0500734 timer->capability = pdata->timer_capability;
Tony Lindgren6e740f92012-10-29 15:20:45 -0700735 timer->get_context_loss_count = pdata->get_context_loss_count;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530736
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530737 /* Skip pm_runtime_enable for OMAP1 */
Jon Hunter66159752012-06-05 12:34:57 -0500738 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530739 pm_runtime_enable(dev);
740 pm_runtime_irq_safe(dev);
Tarun Kanti DebBarmaffe07ce2011-09-20 17:00:21 +0530741 }
742
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700743 if (!timer->reserved) {
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530744 pm_runtime_get_sync(dev);
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700745 __omap_dm_timer_init_regs(timer);
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530746 pm_runtime_put(dev);
Tony Lindgren0dad9fa2011-09-21 16:38:51 -0700747 }
748
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530749 /* add the timer element to the list */
750 spin_lock_irqsave(&dm_timer_lock, flags);
751 list_add_tail(&timer->node, &omap_timer_list);
752 spin_unlock_irqrestore(&dm_timer_lock, flags);
753
Tarun Kanti DebBarma74dd9ec2012-04-20 18:09:20 +0530754 dev_dbg(dev, "Device Probed.\n");
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530755
756 return 0;
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530757}
758
759/**
760 * omap_dm_timer_remove - cleanup a registered timer device
761 * @pdev: pointer to current timer platform device
762 *
763 * Called by driver framework whenever a timer device is unregistered.
764 * In addition to freeing platform resources it also deletes the timer
765 * entry from the local list.
766 */
767static int __devexit omap_dm_timer_remove(struct platform_device *pdev)
768{
769 struct omap_dm_timer *timer;
770 unsigned long flags;
771 int ret = -EINVAL;
772
773 spin_lock_irqsave(&dm_timer_lock, flags);
774 list_for_each_entry(timer, &omap_timer_list, node)
775 if (timer->pdev->id == pdev->id) {
776 list_del(&timer->node);
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530777 ret = 0;
778 break;
779 }
780 spin_unlock_irqrestore(&dm_timer_lock, flags);
781
782 return ret;
783}
784
785static struct platform_driver omap_dm_timer_driver = {
786 .probe = omap_dm_timer_probe,
Arnd Bergmann4c23c8d2011-10-01 18:42:47 +0200787 .remove = __devexit_p(omap_dm_timer_remove),
Tarun Kanti DebBarmadf284722011-09-20 17:00:19 +0530788 .driver = {
789 .name = "omap_timer",
790 },
791};
792
793static int __init omap_dm_timer_driver_init(void)
794{
795 return platform_driver_register(&omap_dm_timer_driver);
796}
797
798static void __exit omap_dm_timer_driver_exit(void)
799{
800 platform_driver_unregister(&omap_dm_timer_driver);
801}
802
803early_platform_init("earlytimer", &omap_dm_timer_driver);
804module_init(omap_dm_timer_driver_init);
805module_exit(omap_dm_timer_driver_exit);
806
807MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
808MODULE_LICENSE("GPL");
809MODULE_ALIAS("platform:" DRIVER_NAME);
810MODULE_AUTHOR("Texas Instruments Inc");