Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/plat-omap/dmtimer.c |
| 3 | * |
| 4 | * OMAP Dual-Mode Timers |
| 5 | * |
Tarun Kanti DebBarma | 97933d6 | 2011-09-20 17:00:17 +0530 | [diff] [blame] | 6 | * 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 Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 12 | * Copyright (C) 2005 Nokia Corporation |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 13 | * OMAP2 support by Juha Yrjola |
| 14 | * API improvements and OMAP2 clock framework support by Timo Teras |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 15 | * |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 16 | * Copyright (C) 2009 Texas Instruments |
| 17 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
| 18 | * |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 19 | * 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 Lin | 869dec1 | 2011-11-02 09:49:46 +0800 | [diff] [blame] | 38 | #include <linux/module.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 39 | #include <linux/io.h> |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 40 | #include <linux/slab.h> |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 41 | #include <linux/err.h> |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 42 | #include <linux/pm_runtime.h> |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 43 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 44 | #include <plat/dmtimer.h> |
Jon Hunter | 0b30ec1 | 2012-06-05 12:34:56 -0500 | [diff] [blame] | 45 | #include <plat/omap-pm.h> |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 46 | |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 47 | #include <mach/hardware.h> |
| 48 | |
Jon Hunter | b7b4ff7 | 2012-06-05 12:34:51 -0500 | [diff] [blame] | 49 | static u32 omap_reserved_systimers; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 50 | static LIST_HEAD(omap_timer_list); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 51 | static DEFINE_SPINLOCK(dm_timer_lock); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 52 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 53 | /** |
| 54 | * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode |
| 55 | * @timer: timer pointer over which read operation to perform |
| 56 | * @reg: lowest byte holds the register offset |
| 57 | * |
| 58 | * The posted mode bit is encoded in reg. Note that in posted mode write |
| 59 | * pending bit must be checked. Otherwise a read of a non completed write |
| 60 | * will produce an error. |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 61 | */ |
| 62 | static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 63 | { |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 64 | WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET); |
| 65 | return __omap_dm_timer_read(timer, reg, timer->posted); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 68 | /** |
| 69 | * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode |
| 70 | * @timer: timer pointer over which write operation is to perform |
| 71 | * @reg: lowest byte holds the register offset |
| 72 | * @value: data to write into the register |
| 73 | * |
| 74 | * The posted mode bit is encoded in reg. Note that in posted mode the write |
| 75 | * pending bit must be checked. Otherwise a write on a register which has a |
| 76 | * pending write will be lost. |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 77 | */ |
| 78 | static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg, |
| 79 | u32 value) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 80 | { |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 81 | WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET); |
| 82 | __omap_dm_timer_write(timer, reg, value, timer->posted); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 85 | static void omap_timer_restore_context(struct omap_dm_timer *timer) |
| 86 | { |
Tarun Kanti DebBarma | dffc9da | 2012-03-05 16:11:00 -0800 | [diff] [blame] | 87 | if (timer->revision == 1) |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 88 | __raw_writel(timer->context.tistat, timer->sys_stat); |
| 89 | |
| 90 | __raw_writel(timer->context.tisr, timer->irq_stat); |
| 91 | omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, |
| 92 | timer->context.twer); |
| 93 | omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, |
| 94 | timer->context.tcrr); |
| 95 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, |
| 96 | timer->context.tldr); |
| 97 | omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, |
| 98 | timer->context.tmar); |
| 99 | omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, |
| 100 | timer->context.tsicr); |
| 101 | __raw_writel(timer->context.tier, timer->irq_ena); |
| 102 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, |
| 103 | timer->context.tclr); |
| 104 | } |
| 105 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 106 | static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 107 | { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 108 | int c; |
| 109 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 110 | if (!timer->sys_stat) |
| 111 | return; |
| 112 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 113 | c = 0; |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 114 | while (!(__raw_readl(timer->sys_stat) & 1)) { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 115 | c++; |
| 116 | if (c > 100000) { |
| 117 | printk(KERN_ERR "Timer failed to reset\n"); |
| 118 | return; |
| 119 | } |
| 120 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 123 | static void omap_dm_timer_reset(struct omap_dm_timer *timer) |
| 124 | { |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 125 | omap_dm_timer_enable(timer); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 126 | if (timer->pdev->id != 1) { |
Timo Teras | e32f7ec | 2006-06-26 16:16:13 -0700 | [diff] [blame] | 127 | omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06); |
| 128 | omap_dm_timer_wait_for_reset(timer); |
| 129 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 130 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 131 | __omap_dm_timer_reset(timer, 0, 0); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 132 | omap_dm_timer_disable(timer); |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 133 | timer->posted = 1; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 136 | int omap_dm_timer_prepare(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 137 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 138 | int ret; |
| 139 | |
Jon Hunter | bca4580 | 2012-06-05 12:34:58 -0500 | [diff] [blame^] | 140 | /* |
| 141 | * FIXME: OMAP1 devices do not use the clock framework for dmtimers so |
| 142 | * do not call clk_get() for these devices. |
| 143 | */ |
| 144 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) { |
| 145 | timer->fclk = clk_get(&timer->pdev->dev, "fck"); |
| 146 | if (WARN_ON_ONCE(IS_ERR_OR_NULL(timer->fclk))) { |
| 147 | timer->fclk = NULL; |
| 148 | dev_err(&timer->pdev->dev, ": No fclk handle.\n"); |
| 149 | return -EINVAL; |
| 150 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 151 | } |
| 152 | |
Jon Hunter | 6615975 | 2012-06-05 12:34:57 -0500 | [diff] [blame] | 153 | if (timer->capability & OMAP_TIMER_NEEDS_RESET) |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 154 | omap_dm_timer_reset(timer); |
| 155 | |
| 156 | ret = omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ); |
| 157 | |
| 158 | timer->posted = 1; |
| 159 | return ret; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jon Hunter | b7b4ff7 | 2012-06-05 12:34:51 -0500 | [diff] [blame] | 162 | static inline u32 omap_dm_timer_reserved_systimer(int id) |
| 163 | { |
| 164 | return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0; |
| 165 | } |
| 166 | |
| 167 | int omap_dm_timer_reserve_systimer(int id) |
| 168 | { |
| 169 | if (omap_dm_timer_reserved_systimer(id)) |
| 170 | return -ENODEV; |
| 171 | |
| 172 | omap_reserved_systimers |= (1 << (id - 1)); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 177 | struct omap_dm_timer *omap_dm_timer_request(void) |
| 178 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 179 | struct omap_dm_timer *timer = NULL, *t; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 180 | unsigned long flags; |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 181 | int ret = 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 182 | |
| 183 | spin_lock_irqsave(&dm_timer_lock, flags); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 184 | list_for_each_entry(t, &omap_timer_list, node) { |
| 185 | if (t->reserved) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 186 | continue; |
| 187 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 188 | timer = t; |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 189 | timer->reserved = 1; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 190 | break; |
| 191 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 192 | |
| 193 | if (timer) { |
| 194 | ret = omap_dm_timer_prepare(timer); |
| 195 | if (ret) { |
| 196 | timer->reserved = 0; |
| 197 | timer = NULL; |
| 198 | } |
| 199 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 200 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 201 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 202 | if (!timer) |
| 203 | pr_debug("%s: timer request failed!\n", __func__); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 204 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 205 | return timer; |
| 206 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(omap_dm_timer_request); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 208 | |
| 209 | struct omap_dm_timer *omap_dm_timer_request_specific(int id) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 210 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 211 | struct omap_dm_timer *timer = NULL, *t; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 212 | unsigned long flags; |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 213 | int ret = 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 214 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 215 | spin_lock_irqsave(&dm_timer_lock, flags); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 216 | list_for_each_entry(t, &omap_timer_list, node) { |
| 217 | if (t->pdev->id == id && !t->reserved) { |
| 218 | timer = t; |
| 219 | timer->reserved = 1; |
| 220 | break; |
| 221 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 222 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 223 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 224 | if (timer) { |
| 225 | ret = omap_dm_timer_prepare(timer); |
| 226 | if (ret) { |
| 227 | timer->reserved = 0; |
| 228 | timer = NULL; |
| 229 | } |
| 230 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 231 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 232 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 233 | if (!timer) |
| 234 | pr_debug("%s: timer%d request failed!\n", __func__, id); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 235 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 236 | return timer; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 237 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 239 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 240 | int omap_dm_timer_free(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 241 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 242 | if (unlikely(!timer)) |
| 243 | return -EINVAL; |
| 244 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 245 | clk_put(timer->fclk); |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 246 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 247 | WARN_ON(!timer->reserved); |
| 248 | timer->reserved = 0; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 249 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 250 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(omap_dm_timer_free); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 252 | |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 253 | void omap_dm_timer_enable(struct omap_dm_timer *timer) |
| 254 | { |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 255 | pm_runtime_get_sync(&timer->pdev->dev); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 256 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(omap_dm_timer_enable); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 258 | |
| 259 | void omap_dm_timer_disable(struct omap_dm_timer *timer) |
| 260 | { |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 261 | pm_runtime_put(&timer->pdev->dev); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 262 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 263 | EXPORT_SYMBOL_GPL(omap_dm_timer_disable); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 264 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 265 | int omap_dm_timer_get_irq(struct omap_dm_timer *timer) |
| 266 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 267 | if (timer) |
| 268 | return timer->irq; |
| 269 | return -EINVAL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 270 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 272 | |
| 273 | #if defined(CONFIG_ARCH_OMAP1) |
| 274 | |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 275 | /** |
| 276 | * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR |
| 277 | * @inputmask: current value of idlect mask |
| 278 | */ |
| 279 | __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask) |
| 280 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 281 | int i = 0; |
| 282 | struct omap_dm_timer *timer = NULL; |
| 283 | unsigned long flags; |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 284 | |
| 285 | /* If ARMXOR cannot be idled this function call is unnecessary */ |
| 286 | if (!(inputmask & (1 << 1))) |
| 287 | return inputmask; |
| 288 | |
| 289 | /* If any active timer is using ARMXOR return modified mask */ |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 290 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 291 | list_for_each_entry(timer, &omap_timer_list, node) { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 292 | u32 l; |
| 293 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 294 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 295 | if (l & OMAP_TIMER_CTRL_ST) { |
| 296 | if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0) |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 297 | inputmask &= ~(1 << 1); |
| 298 | else |
| 299 | inputmask &= ~(1 << 2); |
| 300 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 301 | i++; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 302 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 303 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 304 | |
| 305 | return inputmask; |
| 306 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 307 | EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask); |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 308 | |
Tony Lindgren | 140455f | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 309 | #else |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 310 | |
| 311 | struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer) |
| 312 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 313 | if (timer) |
| 314 | return timer->fclk; |
| 315 | return NULL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 316 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 318 | |
| 319 | __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask) |
| 320 | { |
| 321 | BUG(); |
Dirk Behme | 2121880 | 2006-12-06 17:14:00 -0800 | [diff] [blame] | 322 | |
| 323 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 324 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 326 | |
| 327 | #endif |
| 328 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 329 | int omap_dm_timer_trigger(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 330 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 331 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 332 | pr_err("%s: timer not available or enabled.\n", __func__); |
| 333 | return -EINVAL; |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 334 | } |
| 335 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 336 | omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 337 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 338 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 339 | EXPORT_SYMBOL_GPL(omap_dm_timer_trigger); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 340 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 341 | int omap_dm_timer_start(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 342 | { |
| 343 | u32 l; |
| 344 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 345 | if (unlikely(!timer)) |
| 346 | return -EINVAL; |
| 347 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 348 | omap_dm_timer_enable(timer); |
| 349 | |
Jon Hunter | 1c2d076 | 2012-06-05 12:34:55 -0500 | [diff] [blame] | 350 | if (!(timer->capability & OMAP_TIMER_ALWON)) { |
Jon Hunter | 0b30ec1 | 2012-06-05 12:34:56 -0500 | [diff] [blame] | 351 | if (omap_pm_get_dev_context_loss_count(&timer->pdev->dev) != |
| 352 | timer->ctx_loss_count) |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 353 | omap_timer_restore_context(timer); |
| 354 | } |
| 355 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 356 | 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 DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 361 | |
| 362 | /* Save the context */ |
| 363 | timer->context.tclr = l; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 364 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 365 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 366 | EXPORT_SYMBOL_GPL(omap_dm_timer_start); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 367 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 368 | int omap_dm_timer_stop(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 369 | { |
Tony Lindgren | caf64f2 | 2011-03-29 15:54:48 -0700 | [diff] [blame] | 370 | unsigned long rate = 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 371 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 372 | if (unlikely(!timer)) |
| 373 | return -EINVAL; |
| 374 | |
Jon Hunter | 6615975 | 2012-06-05 12:34:57 -0500 | [diff] [blame] | 375 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 376 | rate = clk_get_rate(timer->fclk); |
Tony Lindgren | caf64f2 | 2011-03-29 15:54:48 -0700 | [diff] [blame] | 377 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 378 | __omap_dm_timer_stop(timer, timer->posted, rate); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 379 | |
Jon Hunter | 0b30ec1 | 2012-06-05 12:34:56 -0500 | [diff] [blame] | 380 | if (!(timer->capability & OMAP_TIMER_ALWON)) |
Tarun Kanti DebBarma | dffc9da | 2012-03-05 16:11:00 -0800 | [diff] [blame] | 381 | timer->ctx_loss_count = |
Jon Hunter | 0b30ec1 | 2012-06-05 12:34:56 -0500 | [diff] [blame] | 382 | omap_pm_get_dev_context_loss_count(&timer->pdev->dev); |
Tarun Kanti DebBarma | dffc9da | 2012-03-05 16:11:00 -0800 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Since the register values are computed and written within |
| 386 | * __omap_dm_timer_stop, we need to use read to retrieve the |
| 387 | * context. |
| 388 | */ |
| 389 | timer->context.tclr = |
| 390 | omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 391 | timer->context.tisr = __raw_readl(timer->irq_stat); |
| 392 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 393 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 394 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 395 | EXPORT_SYMBOL_GPL(omap_dm_timer_stop); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 396 | |
Paul Walmsley | f248076 | 2009-04-23 21:11:10 -0600 | [diff] [blame] | 397 | int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 398 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 399 | int ret; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 400 | struct dmtimer_platform_data *pdata; |
| 401 | |
| 402 | if (unlikely(!timer)) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | pdata = timer->pdev->dev.platform_data; |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 406 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 407 | if (source < 0 || source >= 3) |
Paul Walmsley | f248076 | 2009-04-23 21:11:10 -0600 | [diff] [blame] | 408 | return -EINVAL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 409 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 410 | ret = pdata->set_timer_src(timer->pdev, source); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 411 | |
| 412 | return ret; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 413 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_source); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 415 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 416 | int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload, |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 417 | unsigned int load) |
| 418 | { |
| 419 | u32 l; |
| 420 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 421 | if (unlikely(!timer)) |
| 422 | return -EINVAL; |
| 423 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 424 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 425 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 426 | if (autoreload) |
| 427 | l |= OMAP_TIMER_CTRL_AR; |
| 428 | else |
| 429 | l &= ~OMAP_TIMER_CTRL_AR; |
| 430 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
| 431 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load); |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 432 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 433 | omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 434 | /* Save the context */ |
| 435 | timer->context.tclr = l; |
| 436 | timer->context.tldr = load; |
| 437 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 438 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 439 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 440 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_load); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 441 | |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 442 | /* Optimized set_load which removes costly spin wait in timer_start */ |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 443 | int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload, |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 444 | unsigned int load) |
| 445 | { |
| 446 | u32 l; |
| 447 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 448 | if (unlikely(!timer)) |
| 449 | return -EINVAL; |
| 450 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 451 | omap_dm_timer_enable(timer); |
| 452 | |
Jon Hunter | 1c2d076 | 2012-06-05 12:34:55 -0500 | [diff] [blame] | 453 | if (!(timer->capability & OMAP_TIMER_ALWON)) { |
Jon Hunter | 0b30ec1 | 2012-06-05 12:34:56 -0500 | [diff] [blame] | 454 | if (omap_pm_get_dev_context_loss_count(&timer->pdev->dev) != |
| 455 | timer->ctx_loss_count) |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 456 | omap_timer_restore_context(timer); |
| 457 | } |
| 458 | |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 459 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 460 | if (autoreload) { |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 461 | l |= OMAP_TIMER_CTRL_AR; |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 462 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load); |
| 463 | } else { |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 464 | l &= ~OMAP_TIMER_CTRL_AR; |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 465 | } |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 466 | l |= OMAP_TIMER_CTRL_ST; |
| 467 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 468 | __omap_dm_timer_load_start(timer, l, load, timer->posted); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 469 | |
| 470 | /* Save the context */ |
| 471 | timer->context.tclr = l; |
| 472 | timer->context.tldr = load; |
| 473 | timer->context.tcrr = load; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 474 | return 0; |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 475 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 476 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start); |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 477 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 478 | int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable, |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 479 | unsigned int match) |
| 480 | { |
| 481 | u32 l; |
| 482 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 483 | if (unlikely(!timer)) |
| 484 | return -EINVAL; |
| 485 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 486 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 487 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 488 | if (enable) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 489 | l |= OMAP_TIMER_CTRL_CE; |
| 490 | else |
| 491 | l &= ~OMAP_TIMER_CTRL_CE; |
| 492 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
| 493 | omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 494 | |
| 495 | /* Save the context */ |
| 496 | timer->context.tclr = l; |
| 497 | timer->context.tmar = match; |
| 498 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 499 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 500 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 501 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_match); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 502 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 503 | int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on, |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 504 | int toggle, int trigger) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 505 | { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 506 | u32 l; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 507 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 508 | if (unlikely(!timer)) |
| 509 | return -EINVAL; |
| 510 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 511 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 512 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 513 | l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM | |
| 514 | OMAP_TIMER_CTRL_PT | (0x03 << 10)); |
| 515 | if (def_on) |
| 516 | l |= OMAP_TIMER_CTRL_SCPWM; |
| 517 | if (toggle) |
| 518 | l |= OMAP_TIMER_CTRL_PT; |
| 519 | l |= trigger << 10; |
| 520 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 521 | |
| 522 | /* Save the context */ |
| 523 | timer->context.tclr = l; |
| 524 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 525 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 526 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 527 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 528 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 529 | int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 530 | { |
| 531 | u32 l; |
| 532 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 533 | if (unlikely(!timer)) |
| 534 | return -EINVAL; |
| 535 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 536 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 537 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 538 | l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2)); |
| 539 | if (prescaler >= 0x00 && prescaler <= 0x07) { |
| 540 | l |= OMAP_TIMER_CTRL_PRE; |
| 541 | l |= prescaler << 2; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 542 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 543 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 544 | |
| 545 | /* Save the context */ |
| 546 | timer->context.tclr = l; |
| 547 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 548 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 549 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 550 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 551 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 552 | int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 553 | unsigned int value) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 554 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 555 | if (unlikely(!timer)) |
| 556 | return -EINVAL; |
| 557 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 558 | omap_dm_timer_enable(timer); |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 559 | __omap_dm_timer_int_enable(timer, value); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 560 | |
| 561 | /* Save the context */ |
| 562 | timer->context.tier = value; |
| 563 | timer->context.twer = value; |
| 564 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 565 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 566 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 567 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 568 | |
| 569 | unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer) |
| 570 | { |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 571 | unsigned int l; |
| 572 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 573 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 574 | pr_err("%s: timer not available or enabled.\n", __func__); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 575 | return 0; |
| 576 | } |
| 577 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 578 | l = __raw_readl(timer->irq_stat); |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 579 | |
| 580 | return l; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 581 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 582 | EXPORT_SYMBOL_GPL(omap_dm_timer_read_status); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 583 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 584 | int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 585 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 586 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) |
| 587 | return -EINVAL; |
| 588 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 589 | __omap_dm_timer_write_status(timer, value); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 590 | /* Save the context */ |
| 591 | timer->context.tisr = value; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 592 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 593 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 594 | EXPORT_SYMBOL_GPL(omap_dm_timer_write_status); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 595 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 596 | unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer) |
| 597 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 598 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 599 | pr_err("%s: timer not iavailable or enabled.\n", __func__); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 603 | return __omap_dm_timer_read_counter(timer, timer->posted); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 604 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 605 | EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 606 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 607 | int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value) |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 608 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 609 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 610 | pr_err("%s: timer not available or enabled.\n", __func__); |
| 611 | return -EINVAL; |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 612 | } |
| 613 | |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 614 | omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 615 | |
| 616 | /* Save the context */ |
| 617 | timer->context.tcrr = value; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 618 | return 0; |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 619 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 620 | EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 621 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 622 | int omap_dm_timers_active(void) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 623 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 624 | struct omap_dm_timer *timer; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 625 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 626 | list_for_each_entry(timer, &omap_timer_list, node) { |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 627 | if (!timer->reserved) |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 628 | continue; |
| 629 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 630 | if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) & |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 631 | OMAP_TIMER_CTRL_ST) { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 632 | return 1; |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 633 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 634 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 635 | return 0; |
| 636 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 637 | EXPORT_SYMBOL_GPL(omap_dm_timers_active); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 638 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 639 | /** |
| 640 | * omap_dm_timer_probe - probe function called for every registered device |
| 641 | * @pdev: pointer to current timer platform device |
| 642 | * |
| 643 | * Called by driver framework at the end of device registration for all |
| 644 | * timer devices. |
| 645 | */ |
| 646 | static int __devinit omap_dm_timer_probe(struct platform_device *pdev) |
| 647 | { |
| 648 | int ret; |
| 649 | unsigned long flags; |
| 650 | struct omap_dm_timer *timer; |
| 651 | struct resource *mem, *irq, *ioarea; |
| 652 | struct dmtimer_platform_data *pdata = pdev->dev.platform_data; |
| 653 | |
| 654 | if (!pdata) { |
| 655 | dev_err(&pdev->dev, "%s: no platform data.\n", __func__); |
| 656 | return -ENODEV; |
| 657 | } |
| 658 | |
| 659 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 660 | if (unlikely(!irq)) { |
| 661 | dev_err(&pdev->dev, "%s: no IRQ resource.\n", __func__); |
| 662 | return -ENODEV; |
| 663 | } |
| 664 | |
| 665 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 666 | if (unlikely(!mem)) { |
| 667 | dev_err(&pdev->dev, "%s: no memory resource.\n", __func__); |
| 668 | return -ENODEV; |
| 669 | } |
| 670 | |
| 671 | ioarea = request_mem_region(mem->start, resource_size(mem), |
| 672 | pdev->name); |
| 673 | if (!ioarea) { |
| 674 | dev_err(&pdev->dev, "%s: region already claimed.\n", __func__); |
| 675 | return -EBUSY; |
| 676 | } |
| 677 | |
| 678 | timer = kzalloc(sizeof(struct omap_dm_timer), GFP_KERNEL); |
| 679 | if (!timer) { |
| 680 | dev_err(&pdev->dev, "%s: no memory for omap_dm_timer.\n", |
| 681 | __func__); |
| 682 | ret = -ENOMEM; |
| 683 | goto err_free_ioregion; |
| 684 | } |
| 685 | |
| 686 | timer->io_base = ioremap(mem->start, resource_size(mem)); |
| 687 | if (!timer->io_base) { |
| 688 | dev_err(&pdev->dev, "%s: ioremap failed.\n", __func__); |
| 689 | ret = -ENOMEM; |
| 690 | goto err_free_mem; |
| 691 | } |
| 692 | |
| 693 | timer->id = pdev->id; |
| 694 | timer->irq = irq->start; |
Jon Hunter | b7b4ff7 | 2012-06-05 12:34:51 -0500 | [diff] [blame] | 695 | timer->reserved = omap_dm_timer_reserved_systimer(timer->id); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 696 | timer->pdev = pdev; |
Jon Hunter | d1c1691 | 2012-06-05 12:34:52 -0500 | [diff] [blame] | 697 | timer->capability = pdata->timer_capability; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 698 | |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 699 | /* Skip pm_runtime_enable for OMAP1 */ |
Jon Hunter | 6615975 | 2012-06-05 12:34:57 -0500 | [diff] [blame] | 700 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) { |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 701 | pm_runtime_enable(&pdev->dev); |
| 702 | pm_runtime_irq_safe(&pdev->dev); |
| 703 | } |
| 704 | |
Tony Lindgren | 0dad9fa | 2011-09-21 16:38:51 -0700 | [diff] [blame] | 705 | if (!timer->reserved) { |
| 706 | pm_runtime_get_sync(&pdev->dev); |
| 707 | __omap_dm_timer_init_regs(timer); |
| 708 | pm_runtime_put(&pdev->dev); |
| 709 | } |
| 710 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 711 | /* add the timer element to the list */ |
| 712 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 713 | list_add_tail(&timer->node, &omap_timer_list); |
| 714 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 715 | |
| 716 | dev_dbg(&pdev->dev, "Device Probed.\n"); |
| 717 | |
| 718 | return 0; |
| 719 | |
| 720 | err_free_mem: |
| 721 | kfree(timer); |
| 722 | |
| 723 | err_free_ioregion: |
| 724 | release_mem_region(mem->start, resource_size(mem)); |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * omap_dm_timer_remove - cleanup a registered timer device |
| 731 | * @pdev: pointer to current timer platform device |
| 732 | * |
| 733 | * Called by driver framework whenever a timer device is unregistered. |
| 734 | * In addition to freeing platform resources it also deletes the timer |
| 735 | * entry from the local list. |
| 736 | */ |
| 737 | static int __devexit omap_dm_timer_remove(struct platform_device *pdev) |
| 738 | { |
| 739 | struct omap_dm_timer *timer; |
| 740 | unsigned long flags; |
| 741 | int ret = -EINVAL; |
| 742 | |
| 743 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 744 | list_for_each_entry(timer, &omap_timer_list, node) |
| 745 | if (timer->pdev->id == pdev->id) { |
| 746 | list_del(&timer->node); |
| 747 | kfree(timer); |
| 748 | ret = 0; |
| 749 | break; |
| 750 | } |
| 751 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | static struct platform_driver omap_dm_timer_driver = { |
| 757 | .probe = omap_dm_timer_probe, |
Arnd Bergmann | 4c23c8d | 2011-10-01 18:42:47 +0200 | [diff] [blame] | 758 | .remove = __devexit_p(omap_dm_timer_remove), |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 759 | .driver = { |
| 760 | .name = "omap_timer", |
| 761 | }, |
| 762 | }; |
| 763 | |
| 764 | static int __init omap_dm_timer_driver_init(void) |
| 765 | { |
| 766 | return platform_driver_register(&omap_dm_timer_driver); |
| 767 | } |
| 768 | |
| 769 | static void __exit omap_dm_timer_driver_exit(void) |
| 770 | { |
| 771 | platform_driver_unregister(&omap_dm_timer_driver); |
| 772 | } |
| 773 | |
| 774 | early_platform_init("earlytimer", &omap_dm_timer_driver); |
| 775 | module_init(omap_dm_timer_driver_init); |
| 776 | module_exit(omap_dm_timer_driver_exit); |
| 777 | |
| 778 | MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver"); |
| 779 | MODULE_LICENSE("GPL"); |
| 780 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 781 | MODULE_AUTHOR("Texas Instruments Inc"); |