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 | |
Jon Hunter | b153883 | 2012-09-28 11:43:30 -0500 | [diff] [blame] | 38 | #include <linux/clk.h> |
Suman Anna | ea05d2e | 2015-10-05 18:28:21 -0500 | [diff] [blame] | 39 | #include <linux/clk-provider.h> |
Axel Lin | 869dec1 | 2011-11-02 09:49:46 +0800 | [diff] [blame] | 40 | #include <linux/module.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 41 | #include <linux/io.h> |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 42 | #include <linux/device.h> |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 43 | #include <linux/err.h> |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 44 | #include <linux/pm_runtime.h> |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 45 | #include <linux/of.h> |
| 46 | #include <linux/of_device.h> |
Jon Hunter | 40fc3bb | 2012-09-28 11:34:49 -0500 | [diff] [blame] | 47 | #include <linux/platform_device.h> |
| 48 | #include <linux/platform_data/dmtimer-omap.h> |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 49 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 50 | #include <plat/dmtimer.h> |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 51 | |
Jon Hunter | b7b4ff7 | 2012-06-05 12:34:51 -0500 | [diff] [blame] | 52 | static u32 omap_reserved_systimers; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 53 | static LIST_HEAD(omap_timer_list); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 54 | static DEFINE_SPINLOCK(dm_timer_lock); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 55 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 56 | enum { |
| 57 | REQUEST_ANY = 0, |
| 58 | REQUEST_BY_ID, |
| 59 | REQUEST_BY_CAP, |
| 60 | REQUEST_BY_NODE, |
| 61 | }; |
| 62 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 63 | /** |
| 64 | * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode |
| 65 | * @timer: timer pointer over which read operation to perform |
| 66 | * @reg: lowest byte holds the register offset |
| 67 | * |
| 68 | * The posted mode bit is encoded in reg. Note that in posted mode write |
| 69 | * pending bit must be checked. Otherwise a read of a non completed write |
| 70 | * will produce an error. |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 71 | */ |
| 72 | 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] | 73 | { |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 74 | WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET); |
| 75 | return __omap_dm_timer_read(timer, reg, timer->posted); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 78 | /** |
| 79 | * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode |
| 80 | * @timer: timer pointer over which write operation is to perform |
| 81 | * @reg: lowest byte holds the register offset |
| 82 | * @value: data to write into the register |
| 83 | * |
| 84 | * The posted mode bit is encoded in reg. Note that in posted mode the write |
| 85 | * pending bit must be checked. Otherwise a write on a register which has a |
| 86 | * pending write will be lost. |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 87 | */ |
| 88 | static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg, |
| 89 | u32 value) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 90 | { |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 91 | WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET); |
| 92 | __omap_dm_timer_write(timer, reg, value, timer->posted); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 95 | static void omap_timer_restore_context(struct omap_dm_timer *timer) |
| 96 | { |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 97 | omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, |
| 98 | timer->context.twer); |
| 99 | omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, |
| 100 | timer->context.tcrr); |
| 101 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, |
| 102 | timer->context.tldr); |
| 103 | omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, |
| 104 | timer->context.tmar); |
| 105 | omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, |
| 106 | timer->context.tsicr); |
Victor Kamensky | 834cacf | 2014-04-15 20:37:47 +0300 | [diff] [blame] | 107 | writel_relaxed(timer->context.tier, timer->irq_ena); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 108 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, |
| 109 | timer->context.tclr); |
| 110 | } |
| 111 | |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 112 | static int omap_dm_timer_reset(struct omap_dm_timer *timer) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 113 | { |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 114 | u32 l, timeout = 100000; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 115 | |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 116 | if (timer->revision != 1) |
| 117 | return -EINVAL; |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 118 | |
Jon Hunter | ffc957b | 2012-07-06 16:46:35 -0500 | [diff] [blame] | 119 | omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06); |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 120 | |
| 121 | do { |
| 122 | l = __omap_dm_timer_read(timer, |
| 123 | OMAP_TIMER_V1_SYS_STAT_OFFSET, 0); |
| 124 | } while (!l && timeout--); |
| 125 | |
| 126 | if (!timeout) { |
| 127 | dev_err(&timer->pdev->dev, "Timer failed to reset\n"); |
| 128 | return -ETIMEDOUT; |
| 129 | } |
| 130 | |
| 131 | /* Configure timer for smart-idle mode */ |
| 132 | l = __omap_dm_timer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET, 0); |
| 133 | l |= 0x2 << 0x3; |
| 134 | __omap_dm_timer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l, 0); |
| 135 | |
| 136 | timer->posted = 0; |
| 137 | |
| 138 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Neil Armstrong | 31a7448 | 2015-11-02 12:14:14 +0100 | [diff] [blame] | 141 | static int omap_dm_timer_of_set_source(struct omap_dm_timer *timer) |
| 142 | { |
| 143 | int ret; |
| 144 | struct clk *parent; |
| 145 | |
| 146 | /* |
| 147 | * FIXME: OMAP1 devices do not use the clock framework for dmtimers so |
| 148 | * do not call clk_get() for these devices. |
| 149 | */ |
| 150 | if (!timer->fclk) |
| 151 | return -ENODEV; |
| 152 | |
| 153 | parent = clk_get(&timer->pdev->dev, NULL); |
| 154 | if (IS_ERR(parent)) |
| 155 | return -ENODEV; |
| 156 | |
| 157 | ret = clk_set_parent(timer->fclk, parent); |
| 158 | if (ret < 0) |
| 159 | pr_err("%s: failed to set parent\n", __func__); |
| 160 | |
| 161 | clk_put(parent); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
Jon Hunter | b0cadb3 | 2012-09-28 12:21:09 -0500 | [diff] [blame] | 166 | static int omap_dm_timer_prepare(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 167 | { |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 168 | int rc; |
| 169 | |
Jon Hunter | bca4580 | 2012-06-05 12:34:58 -0500 | [diff] [blame] | 170 | /* |
| 171 | * FIXME: OMAP1 devices do not use the clock framework for dmtimers so |
| 172 | * do not call clk_get() for these devices. |
| 173 | */ |
| 174 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) { |
| 175 | timer->fclk = clk_get(&timer->pdev->dev, "fck"); |
Russell King | 8628795 | 2013-02-24 10:46:59 +0000 | [diff] [blame] | 176 | if (WARN_ON_ONCE(IS_ERR(timer->fclk))) { |
Jon Hunter | bca4580 | 2012-06-05 12:34:58 -0500 | [diff] [blame] | 177 | dev_err(&timer->pdev->dev, ": No fclk handle.\n"); |
| 178 | return -EINVAL; |
| 179 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 180 | } |
| 181 | |
Jon Hunter | 7b44cf2 | 2012-07-06 16:45:04 -0500 | [diff] [blame] | 182 | omap_dm_timer_enable(timer); |
| 183 | |
Jon Hunter | ae6672c | 2012-07-11 13:47:38 -0500 | [diff] [blame] | 184 | if (timer->capability & OMAP_TIMER_NEEDS_RESET) { |
| 185 | rc = omap_dm_timer_reset(timer); |
| 186 | if (rc) { |
| 187 | omap_dm_timer_disable(timer); |
| 188 | return rc; |
| 189 | } |
| 190 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 191 | |
Jon Hunter | 7b44cf2 | 2012-07-06 16:45:04 -0500 | [diff] [blame] | 192 | __omap_dm_timer_enable_posted(timer); |
| 193 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 194 | |
Neil Armstrong | 31a7448 | 2015-11-02 12:14:14 +0100 | [diff] [blame] | 195 | rc = omap_dm_timer_of_set_source(timer); |
| 196 | if (rc == -ENODEV) |
| 197 | return omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ); |
| 198 | |
| 199 | return rc; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jon Hunter | b7b4ff7 | 2012-06-05 12:34:51 -0500 | [diff] [blame] | 202 | static inline u32 omap_dm_timer_reserved_systimer(int id) |
| 203 | { |
| 204 | return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0; |
| 205 | } |
| 206 | |
| 207 | int omap_dm_timer_reserve_systimer(int id) |
| 208 | { |
| 209 | if (omap_dm_timer_reserved_systimer(id)) |
| 210 | return -ENODEV; |
| 211 | |
| 212 | omap_reserved_systimers |= (1 << (id - 1)); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 217 | static struct omap_dm_timer *_omap_dm_timer_request(int req_type, void *data) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 218 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 219 | struct omap_dm_timer *timer = NULL, *t; |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 220 | struct device_node *np = NULL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 221 | unsigned long flags; |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 222 | u32 cap = 0; |
| 223 | int id = 0; |
| 224 | |
| 225 | switch (req_type) { |
| 226 | case REQUEST_BY_ID: |
| 227 | id = *(int *)data; |
| 228 | break; |
| 229 | case REQUEST_BY_CAP: |
| 230 | cap = *(u32 *)data; |
| 231 | break; |
| 232 | case REQUEST_BY_NODE: |
| 233 | np = (struct device_node *)data; |
| 234 | break; |
| 235 | default: |
| 236 | /* REQUEST_ANY */ |
| 237 | break; |
| 238 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 239 | |
| 240 | spin_lock_irqsave(&dm_timer_lock, flags); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 241 | list_for_each_entry(t, &omap_timer_list, node) { |
| 242 | if (t->reserved) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 243 | continue; |
| 244 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 245 | switch (req_type) { |
| 246 | case REQUEST_BY_ID: |
| 247 | if (id == t->pdev->id) { |
| 248 | timer = t; |
| 249 | timer->reserved = 1; |
| 250 | goto found; |
| 251 | } |
| 252 | break; |
| 253 | case REQUEST_BY_CAP: |
| 254 | if (cap == (t->capability & cap)) { |
| 255 | /* |
| 256 | * If timer is not NULL, we have already found |
| 257 | * one timer but it was not an exact match |
| 258 | * because it had more capabilites that what |
| 259 | * was required. Therefore, unreserve the last |
| 260 | * timer found and see if this one is a better |
| 261 | * match. |
| 262 | */ |
| 263 | if (timer) |
| 264 | timer->reserved = 0; |
| 265 | timer = t; |
| 266 | timer->reserved = 1; |
| 267 | |
| 268 | /* Exit loop early if we find an exact match */ |
| 269 | if (t->capability == cap) |
| 270 | goto found; |
| 271 | } |
| 272 | break; |
| 273 | case REQUEST_BY_NODE: |
| 274 | if (np == t->pdev->dev.of_node) { |
| 275 | timer = t; |
| 276 | timer->reserved = 1; |
| 277 | goto found; |
| 278 | } |
| 279 | break; |
| 280 | default: |
| 281 | /* REQUEST_ANY */ |
| 282 | timer = t; |
| 283 | timer->reserved = 1; |
| 284 | goto found; |
| 285 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 286 | } |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 287 | found: |
Timo Kokkonen | c5491d1 | 2012-08-12 13:45:34 +0300 | [diff] [blame] | 288 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 289 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 290 | if (timer && omap_dm_timer_prepare(timer)) { |
| 291 | timer->reserved = 0; |
| 292 | timer = NULL; |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 293 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 294 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 295 | if (!timer) |
| 296 | pr_debug("%s: timer request failed!\n", __func__); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 297 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 298 | return timer; |
| 299 | } |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 300 | |
| 301 | struct omap_dm_timer *omap_dm_timer_request(void) |
| 302 | { |
| 303 | return _omap_dm_timer_request(REQUEST_ANY, NULL); |
| 304 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 305 | EXPORT_SYMBOL_GPL(omap_dm_timer_request); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 306 | |
| 307 | struct omap_dm_timer *omap_dm_timer_request_specific(int id) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 308 | { |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 309 | /* Requesting timer by ID is not supported when device tree is used */ |
| 310 | if (of_have_populated_dt()) { |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 311 | pr_warn("%s: Please use omap_dm_timer_request_by_cap/node()\n", |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 312 | __func__); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 316 | return _omap_dm_timer_request(REQUEST_BY_ID, &id); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 317 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 318 | EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 319 | |
Jon Hunter | 373fe0b | 2012-09-06 15:28:00 -0500 | [diff] [blame] | 320 | /** |
| 321 | * omap_dm_timer_request_by_cap - Request a timer by capability |
| 322 | * @cap: Bit mask of capabilities to match |
| 323 | * |
| 324 | * Find a timer based upon capabilities bit mask. Callers of this function |
| 325 | * should use the definitions found in the plat/dmtimer.h file under the |
| 326 | * comment "timer capabilities used in hwmod database". Returns pointer to |
| 327 | * timer handle on success and a NULL pointer on failure. |
| 328 | */ |
| 329 | struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap) |
| 330 | { |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 331 | return _omap_dm_timer_request(REQUEST_BY_CAP, &cap); |
Jon Hunter | 373fe0b | 2012-09-06 15:28:00 -0500 | [diff] [blame] | 332 | } |
| 333 | EXPORT_SYMBOL_GPL(omap_dm_timer_request_by_cap); |
| 334 | |
Jon Hunter | 8fc7fcb | 2013-03-19 12:38:17 -0500 | [diff] [blame] | 335 | /** |
| 336 | * omap_dm_timer_request_by_node - Request a timer by device-tree node |
| 337 | * @np: Pointer to device-tree timer node |
| 338 | * |
| 339 | * Request a timer based upon a device node pointer. Returns pointer to |
| 340 | * timer handle on success and a NULL pointer on failure. |
| 341 | */ |
| 342 | struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np) |
| 343 | { |
| 344 | if (!np) |
| 345 | return NULL; |
| 346 | |
| 347 | return _omap_dm_timer_request(REQUEST_BY_NODE, np); |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(omap_dm_timer_request_by_node); |
| 350 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 351 | int omap_dm_timer_free(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 352 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 353 | if (unlikely(!timer)) |
| 354 | return -EINVAL; |
| 355 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 356 | clk_put(timer->fclk); |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 357 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 358 | WARN_ON(!timer->reserved); |
| 359 | timer->reserved = 0; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 360 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 361 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(omap_dm_timer_free); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 363 | |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 364 | void omap_dm_timer_enable(struct omap_dm_timer *timer) |
| 365 | { |
NeilBrown | 9cc268d | 2013-03-19 12:38:15 -0500 | [diff] [blame] | 366 | int c; |
| 367 | |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 368 | pm_runtime_get_sync(&timer->pdev->dev); |
NeilBrown | 9cc268d | 2013-03-19 12:38:15 -0500 | [diff] [blame] | 369 | |
| 370 | if (!(timer->capability & OMAP_TIMER_ALWON)) { |
| 371 | if (timer->get_context_loss_count) { |
| 372 | c = timer->get_context_loss_count(&timer->pdev->dev); |
| 373 | if (c != timer->ctx_loss_count) { |
| 374 | omap_timer_restore_context(timer); |
| 375 | timer->ctx_loss_count = c; |
| 376 | } |
Jon Hunter | 385c4c7 | 2013-03-19 12:38:16 -0500 | [diff] [blame] | 377 | } else { |
| 378 | omap_timer_restore_context(timer); |
NeilBrown | 9cc268d | 2013-03-19 12:38:15 -0500 | [diff] [blame] | 379 | } |
| 380 | } |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 381 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 382 | EXPORT_SYMBOL_GPL(omap_dm_timer_enable); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 383 | |
| 384 | void omap_dm_timer_disable(struct omap_dm_timer *timer) |
| 385 | { |
Jon Hunter | 54f32a3 | 2012-07-13 15:12:03 -0500 | [diff] [blame] | 386 | pm_runtime_put_sync(&timer->pdev->dev); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 387 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 388 | EXPORT_SYMBOL_GPL(omap_dm_timer_disable); |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 389 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 390 | int omap_dm_timer_get_irq(struct omap_dm_timer *timer) |
| 391 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 392 | if (timer) |
| 393 | return timer->irq; |
| 394 | return -EINVAL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 395 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 397 | |
| 398 | #if defined(CONFIG_ARCH_OMAP1) |
Tony Lindgren | 7136f8d | 2012-10-31 12:38:43 -0700 | [diff] [blame] | 399 | #include <mach/hardware.h> |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 400 | /** |
| 401 | * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR |
| 402 | * @inputmask: current value of idlect mask |
| 403 | */ |
| 404 | __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask) |
| 405 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 406 | int i = 0; |
| 407 | struct omap_dm_timer *timer = NULL; |
| 408 | unsigned long flags; |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 409 | |
| 410 | /* If ARMXOR cannot be idled this function call is unnecessary */ |
| 411 | if (!(inputmask & (1 << 1))) |
| 412 | return inputmask; |
| 413 | |
| 414 | /* If any active timer is using ARMXOR return modified mask */ |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 415 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 416 | list_for_each_entry(timer, &omap_timer_list, node) { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 417 | u32 l; |
| 418 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 419 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 420 | if (l & OMAP_TIMER_CTRL_ST) { |
| 421 | if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0) |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 422 | inputmask &= ~(1 << 1); |
| 423 | else |
| 424 | inputmask &= ~(1 << 2); |
| 425 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 426 | i++; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 427 | } |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 428 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 429 | |
| 430 | return inputmask; |
| 431 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 432 | EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask); |
Tony Lindgren | a569c6e | 2006-04-02 17:46:21 +0100 | [diff] [blame] | 433 | |
Tony Lindgren | 140455f | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 434 | #else |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 435 | |
| 436 | struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer) |
| 437 | { |
Russell King | 8628795 | 2013-02-24 10:46:59 +0000 | [diff] [blame] | 438 | if (timer && !IS_ERR(timer->fclk)) |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 439 | return timer->fclk; |
| 440 | return NULL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 441 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 443 | |
| 444 | __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask) |
| 445 | { |
| 446 | BUG(); |
Dirk Behme | 2121880 | 2006-12-06 17:14:00 -0800 | [diff] [blame] | 447 | |
| 448 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 449 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 450 | EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 451 | |
| 452 | #endif |
| 453 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 454 | int omap_dm_timer_trigger(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 455 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 456 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 457 | pr_err("%s: timer not available or enabled.\n", __func__); |
| 458 | return -EINVAL; |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 459 | } |
| 460 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 461 | omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 462 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 463 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 464 | EXPORT_SYMBOL_GPL(omap_dm_timer_trigger); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 465 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 466 | int omap_dm_timer_start(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 467 | { |
| 468 | u32 l; |
| 469 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 470 | if (unlikely(!timer)) |
| 471 | return -EINVAL; |
| 472 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 473 | omap_dm_timer_enable(timer); |
| 474 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 475 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 476 | if (!(l & OMAP_TIMER_CTRL_ST)) { |
| 477 | l |= OMAP_TIMER_CTRL_ST; |
| 478 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
| 479 | } |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 480 | |
| 481 | /* Save the context */ |
| 482 | timer->context.tclr = l; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 483 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 484 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 485 | EXPORT_SYMBOL_GPL(omap_dm_timer_start); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 486 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 487 | int omap_dm_timer_stop(struct omap_dm_timer *timer) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 488 | { |
Tony Lindgren | caf64f2 | 2011-03-29 15:54:48 -0700 | [diff] [blame] | 489 | unsigned long rate = 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 490 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 491 | if (unlikely(!timer)) |
| 492 | return -EINVAL; |
| 493 | |
Jon Hunter | 6615975 | 2012-06-05 12:34:57 -0500 | [diff] [blame] | 494 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 495 | rate = clk_get_rate(timer->fclk); |
Tony Lindgren | caf64f2 | 2011-03-29 15:54:48 -0700 | [diff] [blame] | 496 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 497 | __omap_dm_timer_stop(timer, timer->posted, rate); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 498 | |
Tarun Kanti DebBarma | dffc9da | 2012-03-05 16:11:00 -0800 | [diff] [blame] | 499 | /* |
| 500 | * Since the register values are computed and written within |
| 501 | * __omap_dm_timer_stop, we need to use read to retrieve the |
| 502 | * context. |
| 503 | */ |
| 504 | timer->context.tclr = |
| 505 | omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Tarun Kanti DebBarma | dffc9da | 2012-03-05 16:11:00 -0800 | [diff] [blame] | 506 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 507 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 508 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(omap_dm_timer_stop); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 510 | |
Paul Walmsley | f248076 | 2009-04-23 21:11:10 -0600 | [diff] [blame] | 511 | 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] | 512 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 513 | int ret; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 514 | char *parent_name = NULL; |
Jon Hunter | d7aba55 | 2012-07-18 20:10:12 -0500 | [diff] [blame] | 515 | struct clk *parent; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 516 | struct dmtimer_platform_data *pdata; |
| 517 | |
| 518 | if (unlikely(!timer)) |
| 519 | return -EINVAL; |
| 520 | |
| 521 | pdata = timer->pdev->dev.platform_data; |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 522 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 523 | if (source < 0 || source >= 3) |
Paul Walmsley | f248076 | 2009-04-23 21:11:10 -0600 | [diff] [blame] | 524 | return -EINVAL; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 525 | |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 526 | /* |
| 527 | * FIXME: Used for OMAP1 devices only because they do not currently |
| 528 | * use the clock framework to set the parent clock. To be removed |
| 529 | * once OMAP1 migrated to using clock framework for dmtimers |
| 530 | */ |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 531 | if (pdata && pdata->set_timer_src) |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 532 | return pdata->set_timer_src(timer->pdev, source); |
| 533 | |
Russell King | 8628795 | 2013-02-24 10:46:59 +0000 | [diff] [blame] | 534 | if (IS_ERR(timer->fclk)) |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 535 | return -EINVAL; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 536 | |
Suman Anna | ea05d2e | 2015-10-05 18:28:21 -0500 | [diff] [blame] | 537 | #if defined(CONFIG_COMMON_CLK) |
| 538 | /* Check if the clock has configurable parents */ |
| 539 | if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2) |
| 540 | return 0; |
| 541 | #endif |
| 542 | |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 543 | switch (source) { |
| 544 | case OMAP_TIMER_SRC_SYS_CLK: |
Jon Hunter | c59b537 | 2012-06-05 12:35:00 -0500 | [diff] [blame] | 545 | parent_name = "timer_sys_ck"; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | case OMAP_TIMER_SRC_32_KHZ: |
Jon Hunter | c59b537 | 2012-06-05 12:35:00 -0500 | [diff] [blame] | 549 | parent_name = "timer_32k_ck"; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | case OMAP_TIMER_SRC_EXT_CLK: |
Jon Hunter | c59b537 | 2012-06-05 12:35:00 -0500 | [diff] [blame] | 553 | parent_name = "timer_ext_ck"; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | |
| 557 | parent = clk_get(&timer->pdev->dev, parent_name); |
Russell King | 8628795 | 2013-02-24 10:46:59 +0000 | [diff] [blame] | 558 | if (IS_ERR(parent)) { |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 559 | pr_err("%s: %s not found\n", __func__, parent_name); |
Jon Hunter | d7aba55 | 2012-07-18 20:10:12 -0500 | [diff] [blame] | 560 | return -EINVAL; |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Jon Hunter | d7aba55 | 2012-07-18 20:10:12 -0500 | [diff] [blame] | 563 | ret = clk_set_parent(timer->fclk, parent); |
Russell King | c48cd65 | 2013-03-13 20:44:21 +0000 | [diff] [blame] | 564 | if (ret < 0) |
Jon Hunter | 2b2d352 | 2012-06-05 12:34:59 -0500 | [diff] [blame] | 565 | pr_err("%s: failed to set %s as parent\n", __func__, |
| 566 | parent_name); |
| 567 | |
| 568 | clk_put(parent); |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 569 | |
| 570 | return ret; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 571 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 572 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_source); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 573 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 574 | 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] | 575 | unsigned int load) |
| 576 | { |
| 577 | u32 l; |
| 578 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 579 | if (unlikely(!timer)) |
| 580 | return -EINVAL; |
| 581 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 582 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 583 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 584 | if (autoreload) |
| 585 | l |= OMAP_TIMER_CTRL_AR; |
| 586 | else |
| 587 | l &= ~OMAP_TIMER_CTRL_AR; |
| 588 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
| 589 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load); |
Richard Woodruff | 0f0d080 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 590 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 591 | omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 592 | /* Save the context */ |
| 593 | timer->context.tclr = l; |
| 594 | timer->context.tldr = load; |
| 595 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 596 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 597 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 598 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_load); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 599 | |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 600 | /* Optimized set_load which removes costly spin wait in timer_start */ |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 601 | 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] | 602 | unsigned int load) |
| 603 | { |
| 604 | u32 l; |
| 605 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 606 | if (unlikely(!timer)) |
| 607 | return -EINVAL; |
| 608 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 609 | omap_dm_timer_enable(timer); |
| 610 | |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 611 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 612 | if (autoreload) { |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 613 | l |= OMAP_TIMER_CTRL_AR; |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 614 | omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load); |
| 615 | } else { |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 616 | l &= ~OMAP_TIMER_CTRL_AR; |
Paul Walmsley | 64ce290 | 2008-12-10 17:36:34 -0800 | [diff] [blame] | 617 | } |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 618 | l |= OMAP_TIMER_CTRL_ST; |
| 619 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 620 | __omap_dm_timer_load_start(timer, l, load, timer->posted); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 621 | |
| 622 | /* Save the context */ |
| 623 | timer->context.tclr = l; |
| 624 | timer->context.tldr = load; |
| 625 | timer->context.tcrr = load; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 626 | return 0; |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 627 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 628 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start); |
Richard Woodruff | 3fddd09 | 2008-07-03 12:24:30 +0300 | [diff] [blame] | 629 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 630 | 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] | 631 | unsigned int match) |
| 632 | { |
| 633 | u32 l; |
| 634 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 635 | if (unlikely(!timer)) |
| 636 | return -EINVAL; |
| 637 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 638 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 639 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 640 | if (enable) |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 641 | l |= OMAP_TIMER_CTRL_CE; |
| 642 | else |
| 643 | l &= ~OMAP_TIMER_CTRL_CE; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 644 | omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match); |
Jon Hunter | 991ad16 | 2012-10-04 18:17:42 -0500 | [diff] [blame] | 645 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 646 | |
| 647 | /* Save the context */ |
| 648 | timer->context.tclr = l; |
| 649 | timer->context.tmar = match; |
| 650 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 651 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 652 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 653 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_match); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 654 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 655 | 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] | 656 | int toggle, int trigger) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 657 | { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 658 | u32 l; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 659 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 660 | if (unlikely(!timer)) |
| 661 | return -EINVAL; |
| 662 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 663 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 664 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 665 | l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM | |
| 666 | OMAP_TIMER_CTRL_PT | (0x03 << 10)); |
| 667 | if (def_on) |
| 668 | l |= OMAP_TIMER_CTRL_SCPWM; |
| 669 | if (toggle) |
| 670 | l |= OMAP_TIMER_CTRL_PT; |
| 671 | l |= trigger << 10; |
| 672 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 673 | |
| 674 | /* Save the context */ |
| 675 | timer->context.tclr = l; |
| 676 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 677 | return 0; |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 678 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 679 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 680 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 681 | 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] | 682 | { |
| 683 | u32 l; |
| 684 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 685 | if (unlikely(!timer)) |
| 686 | return -EINVAL; |
| 687 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 688 | omap_dm_timer_enable(timer); |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 689 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG); |
| 690 | l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2)); |
| 691 | if (prescaler >= 0x00 && prescaler <= 0x07) { |
| 692 | l |= OMAP_TIMER_CTRL_PRE; |
| 693 | l |= prescaler << 2; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 694 | } |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 695 | omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 696 | |
| 697 | /* Save the context */ |
| 698 | timer->context.tclr = l; |
| 699 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 700 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 701 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 702 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 703 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 704 | int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 705 | unsigned int value) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 706 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 707 | if (unlikely(!timer)) |
| 708 | return -EINVAL; |
| 709 | |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 710 | omap_dm_timer_enable(timer); |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 711 | __omap_dm_timer_int_enable(timer, value); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 712 | |
| 713 | /* Save the context */ |
| 714 | timer->context.tier = value; |
| 715 | timer->context.twer = value; |
| 716 | omap_dm_timer_disable(timer); |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 717 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 718 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 719 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 720 | |
Jon Hunter | 4249d96 | 2012-07-13 14:03:18 -0500 | [diff] [blame] | 721 | /** |
| 722 | * omap_dm_timer_set_int_disable - disable timer interrupts |
| 723 | * @timer: pointer to timer handle |
| 724 | * @mask: bit mask of interrupts to be disabled |
| 725 | * |
| 726 | * Disables the specified timer interrupts for a timer. |
| 727 | */ |
| 728 | int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask) |
| 729 | { |
| 730 | u32 l = mask; |
| 731 | |
| 732 | if (unlikely(!timer)) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | omap_dm_timer_enable(timer); |
| 736 | |
| 737 | if (timer->revision == 1) |
Victor Kamensky | 834cacf | 2014-04-15 20:37:47 +0300 | [diff] [blame] | 738 | l = readl_relaxed(timer->irq_ena) & ~mask; |
Jon Hunter | 4249d96 | 2012-07-13 14:03:18 -0500 | [diff] [blame] | 739 | |
Victor Kamensky | 834cacf | 2014-04-15 20:37:47 +0300 | [diff] [blame] | 740 | writel_relaxed(l, timer->irq_dis); |
Jon Hunter | 4249d96 | 2012-07-13 14:03:18 -0500 | [diff] [blame] | 741 | l = omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask; |
| 742 | omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, l); |
| 743 | |
| 744 | /* Save the context */ |
| 745 | timer->context.tier &= ~mask; |
| 746 | timer->context.twer &= ~mask; |
| 747 | omap_dm_timer_disable(timer); |
| 748 | return 0; |
| 749 | } |
| 750 | EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_disable); |
| 751 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 752 | unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer) |
| 753 | { |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 754 | unsigned int l; |
| 755 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 756 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 757 | pr_err("%s: timer not available or enabled.\n", __func__); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 758 | return 0; |
| 759 | } |
| 760 | |
Victor Kamensky | 834cacf | 2014-04-15 20:37:47 +0300 | [diff] [blame] | 761 | l = readl_relaxed(timer->irq_stat); |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 762 | |
| 763 | return l; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 764 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 765 | EXPORT_SYMBOL_GPL(omap_dm_timer_read_status); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 766 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 767 | 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] | 768 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 769 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) |
| 770 | return -EINVAL; |
| 771 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 772 | __omap_dm_timer_write_status(timer, value); |
Jon Hunter | 1eaff7101 | 2012-10-04 17:01:14 -0500 | [diff] [blame] | 773 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 774 | return 0; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 775 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 776 | EXPORT_SYMBOL_GPL(omap_dm_timer_write_status); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 777 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 778 | unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer) |
| 779 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 780 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 781 | pr_err("%s: timer not iavailable or enabled.\n", __func__); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | |
Tony Lindgren | ee17f11 | 2011-09-16 15:44:20 -0700 | [diff] [blame] | 785 | return __omap_dm_timer_read_counter(timer, timer->posted); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 786 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 788 | |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 789 | 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] | 790 | { |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 791 | if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) { |
| 792 | pr_err("%s: timer not available or enabled.\n", __func__); |
| 793 | return -EINVAL; |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 794 | } |
| 795 | |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 796 | omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value); |
Tarun Kanti DebBarma | b481113 | 2011-09-20 17:00:24 +0530 | [diff] [blame] | 797 | |
| 798 | /* Save the context */ |
| 799 | timer->context.tcrr = value; |
Tarun Kanti DebBarma | ab4eb8b | 2011-09-20 17:00:26 +0530 | [diff] [blame] | 800 | return 0; |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 801 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 802 | EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter); |
Timo Teras | 83379c8 | 2006-06-26 16:16:23 -0700 | [diff] [blame] | 803 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 804 | int omap_dm_timers_active(void) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 805 | { |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 806 | struct omap_dm_timer *timer; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 807 | |
Tarun Kanti DebBarma | 3392cdd | 2011-09-20 17:00:20 +0530 | [diff] [blame] | 808 | list_for_each_entry(timer, &omap_timer_list, node) { |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 809 | if (!timer->reserved) |
Timo Teras | 12583a7 | 2006-09-25 12:41:42 +0300 | [diff] [blame] | 810 | continue; |
| 811 | |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 812 | if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) & |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 813 | OMAP_TIMER_CTRL_ST) { |
Timo Teras | 77900a2 | 2006-06-26 16:16:12 -0700 | [diff] [blame] | 814 | return 1; |
Timo Teras | fa4bb62 | 2006-09-25 12:41:35 +0300 | [diff] [blame] | 815 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 816 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 817 | return 0; |
| 818 | } |
Timo Kokkonen | 6c366e3 | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 819 | EXPORT_SYMBOL_GPL(omap_dm_timers_active); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 820 | |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 821 | static const struct of_device_id omap_timer_match[]; |
| 822 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 823 | /** |
| 824 | * omap_dm_timer_probe - probe function called for every registered device |
| 825 | * @pdev: pointer to current timer platform device |
| 826 | * |
| 827 | * Called by driver framework at the end of device registration for all |
| 828 | * timer devices. |
| 829 | */ |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 830 | static int omap_dm_timer_probe(struct platform_device *pdev) |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 831 | { |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 832 | unsigned long flags; |
| 833 | struct omap_dm_timer *timer; |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 834 | struct resource *mem, *irq; |
| 835 | struct device *dev = &pdev->dev; |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 836 | const struct of_device_id *match; |
| 837 | const struct dmtimer_platform_data *pdata; |
Suman Anna | a76fc9d | 2015-03-16 20:14:02 -0500 | [diff] [blame] | 838 | int ret; |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 839 | |
| 840 | match = of_match_device(of_match_ptr(omap_timer_match), dev); |
| 841 | pdata = match ? match->data : dev->platform_data; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 842 | |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 843 | if (!pdata && !dev->of_node) { |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 844 | dev_err(dev, "%s: no platform data.\n", __func__); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 845 | return -ENODEV; |
| 846 | } |
| 847 | |
| 848 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 849 | if (unlikely(!irq)) { |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 850 | dev_err(dev, "%s: no IRQ resource.\n", __func__); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 851 | return -ENODEV; |
| 852 | } |
| 853 | |
| 854 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 855 | if (unlikely(!mem)) { |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 856 | dev_err(dev, "%s: no memory resource.\n", __func__); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 857 | return -ENODEV; |
| 858 | } |
| 859 | |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 860 | timer = devm_kzalloc(dev, sizeof(struct omap_dm_timer), GFP_KERNEL); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 861 | if (!timer) { |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 862 | dev_err(dev, "%s: memory alloc failed!\n", __func__); |
| 863 | return -ENOMEM; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 864 | } |
| 865 | |
Russell King | 8628795 | 2013-02-24 10:46:59 +0000 | [diff] [blame] | 866 | timer->fclk = ERR_PTR(-ENODEV); |
Thierry Reding | 5857bd9 | 2013-01-21 11:08:55 +0100 | [diff] [blame] | 867 | timer->io_base = devm_ioremap_resource(dev, mem); |
| 868 | if (IS_ERR(timer->io_base)) |
| 869 | return PTR_ERR(timer->io_base); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 870 | |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 871 | if (dev->of_node) { |
| 872 | if (of_find_property(dev->of_node, "ti,timer-alwon", NULL)) |
| 873 | timer->capability |= OMAP_TIMER_ALWON; |
| 874 | if (of_find_property(dev->of_node, "ti,timer-dsp", NULL)) |
| 875 | timer->capability |= OMAP_TIMER_HAS_DSP_IRQ; |
| 876 | if (of_find_property(dev->of_node, "ti,timer-pwm", NULL)) |
| 877 | timer->capability |= OMAP_TIMER_HAS_PWM; |
| 878 | if (of_find_property(dev->of_node, "ti,timer-secure", NULL)) |
| 879 | timer->capability |= OMAP_TIMER_SECURE; |
| 880 | } else { |
| 881 | timer->id = pdev->id; |
| 882 | timer->capability = pdata->timer_capability; |
| 883 | timer->reserved = omap_dm_timer_reserved_systimer(timer->id); |
Tony Lindgren | f56f52e | 2012-11-09 14:54:17 -0800 | [diff] [blame] | 884 | timer->get_context_loss_count = pdata->get_context_loss_count; |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 885 | } |
| 886 | |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 887 | if (pdata) |
| 888 | timer->errata = pdata->timer_errata; |
| 889 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 890 | timer->irq = irq->start; |
| 891 | timer->pdev = pdev; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 892 | |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 893 | /* Skip pm_runtime_enable for OMAP1 */ |
Jon Hunter | 6615975 | 2012-06-05 12:34:57 -0500 | [diff] [blame] | 894 | if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) { |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 895 | pm_runtime_enable(dev); |
| 896 | pm_runtime_irq_safe(dev); |
Tarun Kanti DebBarma | ffe07ce | 2011-09-20 17:00:21 +0530 | [diff] [blame] | 897 | } |
| 898 | |
Tony Lindgren | 0dad9fa | 2011-09-21 16:38:51 -0700 | [diff] [blame] | 899 | if (!timer->reserved) { |
Suman Anna | a76fc9d | 2015-03-16 20:14:02 -0500 | [diff] [blame] | 900 | ret = pm_runtime_get_sync(dev); |
| 901 | if (ret < 0) { |
| 902 | dev_err(dev, "%s: pm_runtime_get_sync failed!\n", |
| 903 | __func__); |
| 904 | goto err_get_sync; |
| 905 | } |
Tony Lindgren | 0dad9fa | 2011-09-21 16:38:51 -0700 | [diff] [blame] | 906 | __omap_dm_timer_init_regs(timer); |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 907 | pm_runtime_put(dev); |
Tony Lindgren | 0dad9fa | 2011-09-21 16:38:51 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 910 | /* add the timer element to the list */ |
| 911 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 912 | list_add_tail(&timer->node, &omap_timer_list); |
| 913 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 914 | |
Tarun Kanti DebBarma | 74dd9ec | 2012-04-20 18:09:20 +0530 | [diff] [blame] | 915 | dev_dbg(dev, "Device Probed.\n"); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 916 | |
| 917 | return 0; |
Suman Anna | a76fc9d | 2015-03-16 20:14:02 -0500 | [diff] [blame] | 918 | |
| 919 | err_get_sync: |
| 920 | pm_runtime_put_noidle(dev); |
| 921 | pm_runtime_disable(dev); |
| 922 | return ret; |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /** |
| 926 | * omap_dm_timer_remove - cleanup a registered timer device |
| 927 | * @pdev: pointer to current timer platform device |
| 928 | * |
| 929 | * Called by driver framework whenever a timer device is unregistered. |
| 930 | * In addition to freeing platform resources it also deletes the timer |
| 931 | * entry from the local list. |
| 932 | */ |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 933 | static int omap_dm_timer_remove(struct platform_device *pdev) |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 934 | { |
| 935 | struct omap_dm_timer *timer; |
| 936 | unsigned long flags; |
| 937 | int ret = -EINVAL; |
| 938 | |
| 939 | spin_lock_irqsave(&dm_timer_lock, flags); |
| 940 | list_for_each_entry(timer, &omap_timer_list, node) |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 941 | if (!strcmp(dev_name(&timer->pdev->dev), |
| 942 | dev_name(&pdev->dev))) { |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 943 | list_del(&timer->node); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 944 | ret = 0; |
| 945 | break; |
| 946 | } |
| 947 | spin_unlock_irqrestore(&dm_timer_lock, flags); |
| 948 | |
Suman Anna | 51b7e57 | 2015-03-16 20:14:03 -0500 | [diff] [blame] | 949 | pm_runtime_disable(&pdev->dev); |
| 950 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 951 | return ret; |
| 952 | } |
| 953 | |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 954 | static const struct dmtimer_platform_data omap3plus_pdata = { |
| 955 | .timer_errata = OMAP_TIMER_ERRATA_I103_I767, |
| 956 | }; |
| 957 | |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 958 | static const struct of_device_id omap_timer_match[] = { |
Jon Hunter | d1c6ccf | 2013-03-19 12:38:19 -0500 | [diff] [blame] | 959 | { |
| 960 | .compatible = "ti,omap2420-timer", |
| 961 | }, |
| 962 | { |
| 963 | .compatible = "ti,omap3430-timer", |
| 964 | .data = &omap3plus_pdata, |
| 965 | }, |
| 966 | { |
| 967 | .compatible = "ti,omap4430-timer", |
| 968 | .data = &omap3plus_pdata, |
| 969 | }, |
| 970 | { |
| 971 | .compatible = "ti,omap5430-timer", |
| 972 | .data = &omap3plus_pdata, |
| 973 | }, |
| 974 | { |
| 975 | .compatible = "ti,am335x-timer", |
| 976 | .data = &omap3plus_pdata, |
| 977 | }, |
| 978 | { |
| 979 | .compatible = "ti,am335x-timer-1ms", |
| 980 | .data = &omap3plus_pdata, |
| 981 | }, |
Neil Armstrong | 8c0cabd | 2015-10-22 11:18:53 +0200 | [diff] [blame] | 982 | { |
| 983 | .compatible = "ti,dm816-timer", |
| 984 | .data = &omap3plus_pdata, |
| 985 | }, |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 986 | {}, |
| 987 | }; |
| 988 | MODULE_DEVICE_TABLE(of, omap_timer_match); |
| 989 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 990 | static struct platform_driver omap_dm_timer_driver = { |
| 991 | .probe = omap_dm_timer_probe, |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 992 | .remove = omap_dm_timer_remove, |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 993 | .driver = { |
| 994 | .name = "omap_timer", |
Jon Hunter | 9725f44 | 2012-05-14 10:41:37 -0500 | [diff] [blame] | 995 | .of_match_table = of_match_ptr(omap_timer_match), |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 996 | }, |
| 997 | }; |
| 998 | |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 999 | early_platform_init("earlytimer", &omap_dm_timer_driver); |
Srinivas Kandagatla | e4e9f7e | 2012-12-16 11:30:02 -0800 | [diff] [blame] | 1000 | module_platform_driver(omap_dm_timer_driver); |
Tarun Kanti DebBarma | df28472 | 2011-09-20 17:00:19 +0530 | [diff] [blame] | 1001 | |
| 1002 | MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver"); |
| 1003 | MODULE_LICENSE("GPL"); |
| 1004 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 1005 | MODULE_AUTHOR("Texas Instruments Inc"); |