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