Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clocksource / timer-ti-dm.c
blob33eeabf9c3d12bd2c6220d49d0ab26612f41daf6
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * linux/arch/arm/plat-omap/dmtimer.c
5 * OMAP Dual-Mode Timers
7 * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com/
8 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
9 * Thara Gopinath <thara@ti.com>
11 * dmtimer adaptation to platform_driver.
13 * Copyright (C) 2005 Nokia Corporation
14 * OMAP2 support by Juha Yrjola
15 * API improvements and OMAP2 clock framework support by Timo Teras
17 * Copyright (C) 2009 Texas Instruments
18 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 #include <linux/cpu_pm.h>
24 #include <linux/module.h>
25 #include <linux/io.h>
26 #include <linux/device.h>
27 #include <linux/err.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
32 #include <linux/platform_data/dmtimer-omap.h>
34 #include <clocksource/timer-ti-dm.h>
36 static u32 omap_reserved_systimers;
37 static LIST_HEAD(omap_timer_list);
38 static DEFINE_SPINLOCK(dm_timer_lock);
40 enum {
41 REQUEST_ANY = 0,
42 REQUEST_BY_ID,
43 REQUEST_BY_CAP,
44 REQUEST_BY_NODE,
47 /**
48 * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode
49 * @timer: timer pointer over which read operation to perform
50 * @reg: lowest byte holds the register offset
52 * The posted mode bit is encoded in reg. Note that in posted mode write
53 * pending bit must be checked. Otherwise a read of a non completed write
54 * will produce an error.
56 static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
58 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
59 return __omap_dm_timer_read(timer, reg, timer->posted);
62 /**
63 * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode
64 * @timer: timer pointer over which write operation is to perform
65 * @reg: lowest byte holds the register offset
66 * @value: data to write into the register
68 * The posted mode bit is encoded in reg. Note that in posted mode the write
69 * pending bit must be checked. Otherwise a write on a register which has a
70 * pending write will be lost.
72 static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
73 u32 value)
75 WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
76 __omap_dm_timer_write(timer, reg, value, timer->posted);
79 static void omap_timer_restore_context(struct omap_dm_timer *timer)
81 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG,
82 timer->context.twer);
83 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG,
84 timer->context.tcrr);
85 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG,
86 timer->context.tldr);
87 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG,
88 timer->context.tmar);
89 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
90 timer->context.tsicr);
91 writel_relaxed(timer->context.tier, timer->irq_ena);
92 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG,
93 timer->context.tclr);
96 static void omap_timer_save_context(struct omap_dm_timer *timer)
98 timer->context.tclr =
99 omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
100 timer->context.twer =
101 omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG);
102 timer->context.tldr =
103 omap_dm_timer_read_reg(timer, OMAP_TIMER_LOAD_REG);
104 timer->context.tmar =
105 omap_dm_timer_read_reg(timer, OMAP_TIMER_MATCH_REG);
106 timer->context.tier = readl_relaxed(timer->irq_ena);
107 timer->context.tsicr =
108 omap_dm_timer_read_reg(timer, OMAP_TIMER_IF_CTRL_REG);
111 static int omap_timer_context_notifier(struct notifier_block *nb,
112 unsigned long cmd, void *v)
114 struct omap_dm_timer *timer;
116 timer = container_of(nb, struct omap_dm_timer, nb);
118 switch (cmd) {
119 case CPU_CLUSTER_PM_ENTER:
120 if ((timer->capability & OMAP_TIMER_ALWON) ||
121 !atomic_read(&timer->enabled))
122 break;
123 omap_timer_save_context(timer);
124 break;
125 case CPU_CLUSTER_PM_ENTER_FAILED:
126 case CPU_CLUSTER_PM_EXIT:
127 if ((timer->capability & OMAP_TIMER_ALWON) ||
128 !atomic_read(&timer->enabled))
129 break;
130 omap_timer_restore_context(timer);
131 break;
134 return NOTIFY_OK;
137 static int omap_dm_timer_reset(struct omap_dm_timer *timer)
139 u32 l, timeout = 100000;
141 if (timer->revision != 1)
142 return -EINVAL;
144 omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
146 do {
147 l = __omap_dm_timer_read(timer,
148 OMAP_TIMER_V1_SYS_STAT_OFFSET, 0);
149 } while (!l && timeout--);
151 if (!timeout) {
152 dev_err(&timer->pdev->dev, "Timer failed to reset\n");
153 return -ETIMEDOUT;
156 /* Configure timer for smart-idle mode */
157 l = __omap_dm_timer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET, 0);
158 l |= 0x2 << 0x3;
159 __omap_dm_timer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l, 0);
161 timer->posted = 0;
163 return 0;
166 static int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
168 int ret;
169 const char *parent_name;
170 struct clk *parent;
171 struct dmtimer_platform_data *pdata;
173 if (unlikely(!timer) || IS_ERR(timer->fclk))
174 return -EINVAL;
176 switch (source) {
177 case OMAP_TIMER_SRC_SYS_CLK:
178 parent_name = "timer_sys_ck";
179 break;
180 case OMAP_TIMER_SRC_32_KHZ:
181 parent_name = "timer_32k_ck";
182 break;
183 case OMAP_TIMER_SRC_EXT_CLK:
184 parent_name = "timer_ext_ck";
185 break;
186 default:
187 return -EINVAL;
190 pdata = timer->pdev->dev.platform_data;
193 * FIXME: Used for OMAP1 devices only because they do not currently
194 * use the clock framework to set the parent clock. To be removed
195 * once OMAP1 migrated to using clock framework for dmtimers
197 if (pdata && pdata->set_timer_src)
198 return pdata->set_timer_src(timer->pdev, source);
200 #if defined(CONFIG_COMMON_CLK)
201 /* Check if the clock has configurable parents */
202 if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
203 return 0;
204 #endif
206 parent = clk_get(&timer->pdev->dev, parent_name);
207 if (IS_ERR(parent)) {
208 pr_err("%s: %s not found\n", __func__, parent_name);
209 return -EINVAL;
212 ret = clk_set_parent(timer->fclk, parent);
213 if (ret < 0)
214 pr_err("%s: failed to set %s as parent\n", __func__,
215 parent_name);
217 clk_put(parent);
219 return ret;
222 static void omap_dm_timer_enable(struct omap_dm_timer *timer)
224 pm_runtime_get_sync(&timer->pdev->dev);
227 static void omap_dm_timer_disable(struct omap_dm_timer *timer)
229 pm_runtime_put_sync(&timer->pdev->dev);
232 static int omap_dm_timer_prepare(struct omap_dm_timer *timer)
234 int rc;
237 * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
238 * do not call clk_get() for these devices.
240 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
241 timer->fclk = clk_get(&timer->pdev->dev, "fck");
242 if (WARN_ON_ONCE(IS_ERR(timer->fclk))) {
243 dev_err(&timer->pdev->dev, ": No fclk handle.\n");
244 return -EINVAL;
248 omap_dm_timer_enable(timer);
250 if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
251 rc = omap_dm_timer_reset(timer);
252 if (rc) {
253 omap_dm_timer_disable(timer);
254 return rc;
258 __omap_dm_timer_enable_posted(timer);
259 omap_dm_timer_disable(timer);
261 return 0;
264 static inline u32 omap_dm_timer_reserved_systimer(int id)
266 return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
269 int omap_dm_timer_reserve_systimer(int id)
271 if (omap_dm_timer_reserved_systimer(id))
272 return -ENODEV;
274 omap_reserved_systimers |= (1 << (id - 1));
276 return 0;
279 static struct omap_dm_timer *_omap_dm_timer_request(int req_type, void *data)
281 struct omap_dm_timer *timer = NULL, *t;
282 struct device_node *np = NULL;
283 unsigned long flags;
284 u32 cap = 0;
285 int id = 0;
287 switch (req_type) {
288 case REQUEST_BY_ID:
289 id = *(int *)data;
290 break;
291 case REQUEST_BY_CAP:
292 cap = *(u32 *)data;
293 break;
294 case REQUEST_BY_NODE:
295 np = (struct device_node *)data;
296 break;
297 default:
298 /* REQUEST_ANY */
299 break;
302 spin_lock_irqsave(&dm_timer_lock, flags);
303 list_for_each_entry(t, &omap_timer_list, node) {
304 if (t->reserved)
305 continue;
307 switch (req_type) {
308 case REQUEST_BY_ID:
309 if (id == t->pdev->id) {
310 timer = t;
311 timer->reserved = 1;
312 goto found;
314 break;
315 case REQUEST_BY_CAP:
316 if (cap == (t->capability & cap)) {
318 * If timer is not NULL, we have already found
319 * one timer. But it was not an exact match
320 * because it had more capabilities than what
321 * was required. Therefore, unreserve the last
322 * timer found and see if this one is a better
323 * match.
325 if (timer)
326 timer->reserved = 0;
327 timer = t;
328 timer->reserved = 1;
330 /* Exit loop early if we find an exact match */
331 if (t->capability == cap)
332 goto found;
334 break;
335 case REQUEST_BY_NODE:
336 if (np == t->pdev->dev.of_node) {
337 timer = t;
338 timer->reserved = 1;
339 goto found;
341 break;
342 default:
343 /* REQUEST_ANY */
344 timer = t;
345 timer->reserved = 1;
346 goto found;
349 found:
350 spin_unlock_irqrestore(&dm_timer_lock, flags);
352 if (timer && omap_dm_timer_prepare(timer)) {
353 timer->reserved = 0;
354 timer = NULL;
357 if (!timer)
358 pr_debug("%s: timer request failed!\n", __func__);
360 return timer;
363 static struct omap_dm_timer *omap_dm_timer_request(void)
365 return _omap_dm_timer_request(REQUEST_ANY, NULL);
368 static struct omap_dm_timer *omap_dm_timer_request_specific(int id)
370 /* Requesting timer by ID is not supported when device tree is used */
371 if (of_have_populated_dt()) {
372 pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
373 __func__);
374 return NULL;
377 return _omap_dm_timer_request(REQUEST_BY_ID, &id);
381 * omap_dm_timer_request_by_cap - Request a timer by capability
382 * @cap: Bit mask of capabilities to match
384 * Find a timer based upon capabilities bit mask. Callers of this function
385 * should use the definitions found in the plat/dmtimer.h file under the
386 * comment "timer capabilities used in hwmod database". Returns pointer to
387 * timer handle on success and a NULL pointer on failure.
389 struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap)
391 return _omap_dm_timer_request(REQUEST_BY_CAP, &cap);
395 * omap_dm_timer_request_by_node - Request a timer by device-tree node
396 * @np: Pointer to device-tree timer node
398 * Request a timer based upon a device node pointer. Returns pointer to
399 * timer handle on success and a NULL pointer on failure.
401 static struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
403 if (!np)
404 return NULL;
406 return _omap_dm_timer_request(REQUEST_BY_NODE, np);
409 static int omap_dm_timer_free(struct omap_dm_timer *timer)
411 if (unlikely(!timer))
412 return -EINVAL;
414 clk_put(timer->fclk);
416 WARN_ON(!timer->reserved);
417 timer->reserved = 0;
418 return 0;
421 int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
423 if (timer)
424 return timer->irq;
425 return -EINVAL;
428 #if defined(CONFIG_ARCH_OMAP1)
429 #include <mach/hardware.h>
431 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
433 return NULL;
437 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
438 * @inputmask: current value of idlect mask
440 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
442 int i = 0;
443 struct omap_dm_timer *timer = NULL;
444 unsigned long flags;
446 /* If ARMXOR cannot be idled this function call is unnecessary */
447 if (!(inputmask & (1 << 1)))
448 return inputmask;
450 /* If any active timer is using ARMXOR return modified mask */
451 spin_lock_irqsave(&dm_timer_lock, flags);
452 list_for_each_entry(timer, &omap_timer_list, node) {
453 u32 l;
455 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
456 if (l & OMAP_TIMER_CTRL_ST) {
457 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
458 inputmask &= ~(1 << 1);
459 else
460 inputmask &= ~(1 << 2);
462 i++;
464 spin_unlock_irqrestore(&dm_timer_lock, flags);
466 return inputmask;
469 #else
471 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
473 if (timer && !IS_ERR(timer->fclk))
474 return timer->fclk;
475 return NULL;
478 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
480 BUG();
482 return 0;
485 #endif
487 int omap_dm_timer_trigger(struct omap_dm_timer *timer)
489 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
490 pr_err("%s: timer not available or enabled.\n", __func__);
491 return -EINVAL;
494 omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
495 return 0;
498 static int omap_dm_timer_start(struct omap_dm_timer *timer)
500 u32 l;
502 if (unlikely(!timer))
503 return -EINVAL;
505 omap_dm_timer_enable(timer);
507 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
508 if (!(l & OMAP_TIMER_CTRL_ST)) {
509 l |= OMAP_TIMER_CTRL_ST;
510 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
513 return 0;
516 static int omap_dm_timer_stop(struct omap_dm_timer *timer)
518 unsigned long rate = 0;
520 if (unlikely(!timer))
521 return -EINVAL;
523 if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
524 rate = clk_get_rate(timer->fclk);
526 __omap_dm_timer_stop(timer, timer->posted, rate);
528 omap_dm_timer_disable(timer);
529 return 0;
532 static int omap_dm_timer_set_load(struct omap_dm_timer *timer,
533 unsigned int load)
535 if (unlikely(!timer))
536 return -EINVAL;
538 omap_dm_timer_enable(timer);
539 omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
541 omap_dm_timer_disable(timer);
542 return 0;
545 static int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
546 unsigned int match)
548 u32 l;
550 if (unlikely(!timer))
551 return -EINVAL;
553 omap_dm_timer_enable(timer);
554 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
555 if (enable)
556 l |= OMAP_TIMER_CTRL_CE;
557 else
558 l &= ~OMAP_TIMER_CTRL_CE;
559 omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
560 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
562 omap_dm_timer_disable(timer);
563 return 0;
566 static int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
567 int toggle, int trigger, int autoreload)
569 u32 l;
571 if (unlikely(!timer))
572 return -EINVAL;
574 omap_dm_timer_enable(timer);
575 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
576 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
577 OMAP_TIMER_CTRL_PT | (0x03 << 10) | OMAP_TIMER_CTRL_AR);
578 if (def_on)
579 l |= OMAP_TIMER_CTRL_SCPWM;
580 if (toggle)
581 l |= OMAP_TIMER_CTRL_PT;
582 l |= trigger << 10;
583 if (autoreload)
584 l |= OMAP_TIMER_CTRL_AR;
585 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
587 omap_dm_timer_disable(timer);
588 return 0;
591 static int omap_dm_timer_get_pwm_status(struct omap_dm_timer *timer)
593 u32 l;
595 if (unlikely(!timer))
596 return -EINVAL;
598 omap_dm_timer_enable(timer);
599 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
600 omap_dm_timer_disable(timer);
602 return l;
605 static int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer,
606 int prescaler)
608 u32 l;
610 if (unlikely(!timer) || prescaler < -1 || prescaler > 7)
611 return -EINVAL;
613 omap_dm_timer_enable(timer);
614 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
615 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
616 if (prescaler >= 0) {
617 l |= OMAP_TIMER_CTRL_PRE;
618 l |= prescaler << 2;
620 omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
622 omap_dm_timer_disable(timer);
623 return 0;
626 static int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
627 unsigned int value)
629 if (unlikely(!timer))
630 return -EINVAL;
632 omap_dm_timer_enable(timer);
633 __omap_dm_timer_int_enable(timer, value);
635 omap_dm_timer_disable(timer);
636 return 0;
640 * omap_dm_timer_set_int_disable - disable timer interrupts
641 * @timer: pointer to timer handle
642 * @mask: bit mask of interrupts to be disabled
644 * Disables the specified timer interrupts for a timer.
646 static int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask)
648 u32 l = mask;
650 if (unlikely(!timer))
651 return -EINVAL;
653 omap_dm_timer_enable(timer);
655 if (timer->revision == 1)
656 l = readl_relaxed(timer->irq_ena) & ~mask;
658 writel_relaxed(l, timer->irq_dis);
659 l = omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
660 omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
662 omap_dm_timer_disable(timer);
663 return 0;
666 static unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
668 unsigned int l;
670 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
671 pr_err("%s: timer not available or enabled.\n", __func__);
672 return 0;
675 l = readl_relaxed(timer->irq_stat);
677 return l;
680 static int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
682 if (unlikely(!timer || !atomic_read(&timer->enabled)))
683 return -EINVAL;
685 __omap_dm_timer_write_status(timer, value);
687 return 0;
690 static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
692 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
693 pr_err("%s: timer not iavailable or enabled.\n", __func__);
694 return 0;
697 return __omap_dm_timer_read_counter(timer, timer->posted);
700 static int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
702 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
703 pr_err("%s: timer not available or enabled.\n", __func__);
704 return -EINVAL;
707 omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
709 /* Save the context */
710 timer->context.tcrr = value;
711 return 0;
714 int omap_dm_timers_active(void)
716 struct omap_dm_timer *timer;
718 list_for_each_entry(timer, &omap_timer_list, node) {
719 if (!timer->reserved)
720 continue;
722 if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
723 OMAP_TIMER_CTRL_ST) {
724 return 1;
727 return 0;
730 static int __maybe_unused omap_dm_timer_runtime_suspend(struct device *dev)
732 struct omap_dm_timer *timer = dev_get_drvdata(dev);
734 atomic_set(&timer->enabled, 0);
736 if (timer->capability & OMAP_TIMER_ALWON || !timer->func_base)
737 return 0;
739 omap_timer_save_context(timer);
741 return 0;
744 static int __maybe_unused omap_dm_timer_runtime_resume(struct device *dev)
746 struct omap_dm_timer *timer = dev_get_drvdata(dev);
748 if (!(timer->capability & OMAP_TIMER_ALWON) && timer->func_base)
749 omap_timer_restore_context(timer);
751 atomic_set(&timer->enabled, 1);
753 return 0;
756 static const struct dev_pm_ops omap_dm_timer_pm_ops = {
757 SET_RUNTIME_PM_OPS(omap_dm_timer_runtime_suspend,
758 omap_dm_timer_runtime_resume, NULL)
761 static const struct of_device_id omap_timer_match[];
764 * omap_dm_timer_probe - probe function called for every registered device
765 * @pdev: pointer to current timer platform device
767 * Called by driver framework at the end of device registration for all
768 * timer devices.
770 static int omap_dm_timer_probe(struct platform_device *pdev)
772 unsigned long flags;
773 struct omap_dm_timer *timer;
774 struct device *dev = &pdev->dev;
775 const struct dmtimer_platform_data *pdata;
776 int ret;
778 pdata = of_device_get_match_data(dev);
779 if (!pdata)
780 pdata = dev_get_platdata(dev);
781 else
782 dev->platform_data = (void *)pdata;
784 if (!pdata) {
785 dev_err(dev, "%s: no platform data.\n", __func__);
786 return -ENODEV;
789 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
790 if (!timer)
791 return -ENOMEM;
793 timer->irq = platform_get_irq(pdev, 0);
794 if (timer->irq < 0)
795 return timer->irq;
797 timer->fclk = ERR_PTR(-ENODEV);
798 timer->io_base = devm_platform_ioremap_resource(pdev, 0);
799 if (IS_ERR(timer->io_base))
800 return PTR_ERR(timer->io_base);
802 platform_set_drvdata(pdev, timer);
804 if (dev->of_node) {
805 if (of_find_property(dev->of_node, "ti,timer-alwon", NULL))
806 timer->capability |= OMAP_TIMER_ALWON;
807 if (of_find_property(dev->of_node, "ti,timer-dsp", NULL))
808 timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
809 if (of_find_property(dev->of_node, "ti,timer-pwm", NULL))
810 timer->capability |= OMAP_TIMER_HAS_PWM;
811 if (of_find_property(dev->of_node, "ti,timer-secure", NULL))
812 timer->capability |= OMAP_TIMER_SECURE;
813 } else {
814 timer->id = pdev->id;
815 timer->capability = pdata->timer_capability;
816 timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
819 if (!(timer->capability & OMAP_TIMER_ALWON)) {
820 timer->nb.notifier_call = omap_timer_context_notifier;
821 cpu_pm_register_notifier(&timer->nb);
824 if (pdata)
825 timer->errata = pdata->timer_errata;
827 timer->pdev = pdev;
829 pm_runtime_enable(dev);
831 if (!timer->reserved) {
832 ret = pm_runtime_get_sync(dev);
833 if (ret < 0) {
834 dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
835 __func__);
836 goto err_get_sync;
838 __omap_dm_timer_init_regs(timer);
839 pm_runtime_put(dev);
842 /* add the timer element to the list */
843 spin_lock_irqsave(&dm_timer_lock, flags);
844 list_add_tail(&timer->node, &omap_timer_list);
845 spin_unlock_irqrestore(&dm_timer_lock, flags);
847 dev_dbg(dev, "Device Probed.\n");
849 return 0;
851 err_get_sync:
852 pm_runtime_put_noidle(dev);
853 pm_runtime_disable(dev);
854 return ret;
858 * omap_dm_timer_remove - cleanup a registered timer device
859 * @pdev: pointer to current timer platform device
861 * Called by driver framework whenever a timer device is unregistered.
862 * In addition to freeing platform resources it also deletes the timer
863 * entry from the local list.
865 static int omap_dm_timer_remove(struct platform_device *pdev)
867 struct omap_dm_timer *timer;
868 unsigned long flags;
869 int ret = -EINVAL;
871 spin_lock_irqsave(&dm_timer_lock, flags);
872 list_for_each_entry(timer, &omap_timer_list, node)
873 if (!strcmp(dev_name(&timer->pdev->dev),
874 dev_name(&pdev->dev))) {
875 if (!(timer->capability & OMAP_TIMER_ALWON))
876 cpu_pm_unregister_notifier(&timer->nb);
877 list_del(&timer->node);
878 ret = 0;
879 break;
881 spin_unlock_irqrestore(&dm_timer_lock, flags);
883 pm_runtime_disable(&pdev->dev);
885 return ret;
888 static const struct omap_dm_timer_ops dmtimer_ops = {
889 .request_by_node = omap_dm_timer_request_by_node,
890 .request_specific = omap_dm_timer_request_specific,
891 .request = omap_dm_timer_request,
892 .set_source = omap_dm_timer_set_source,
893 .get_irq = omap_dm_timer_get_irq,
894 .set_int_enable = omap_dm_timer_set_int_enable,
895 .set_int_disable = omap_dm_timer_set_int_disable,
896 .free = omap_dm_timer_free,
897 .enable = omap_dm_timer_enable,
898 .disable = omap_dm_timer_disable,
899 .get_fclk = omap_dm_timer_get_fclk,
900 .start = omap_dm_timer_start,
901 .stop = omap_dm_timer_stop,
902 .set_load = omap_dm_timer_set_load,
903 .set_match = omap_dm_timer_set_match,
904 .set_pwm = omap_dm_timer_set_pwm,
905 .get_pwm_status = omap_dm_timer_get_pwm_status,
906 .set_prescaler = omap_dm_timer_set_prescaler,
907 .read_counter = omap_dm_timer_read_counter,
908 .write_counter = omap_dm_timer_write_counter,
909 .read_status = omap_dm_timer_read_status,
910 .write_status = omap_dm_timer_write_status,
913 static const struct dmtimer_platform_data omap3plus_pdata = {
914 .timer_errata = OMAP_TIMER_ERRATA_I103_I767,
915 .timer_ops = &dmtimer_ops,
918 static const struct of_device_id omap_timer_match[] = {
920 .compatible = "ti,omap2420-timer",
923 .compatible = "ti,omap3430-timer",
924 .data = &omap3plus_pdata,
927 .compatible = "ti,omap4430-timer",
928 .data = &omap3plus_pdata,
931 .compatible = "ti,omap5430-timer",
932 .data = &omap3plus_pdata,
935 .compatible = "ti,am335x-timer",
936 .data = &omap3plus_pdata,
939 .compatible = "ti,am335x-timer-1ms",
940 .data = &omap3plus_pdata,
943 .compatible = "ti,dm816-timer",
944 .data = &omap3plus_pdata,
948 MODULE_DEVICE_TABLE(of, omap_timer_match);
950 static struct platform_driver omap_dm_timer_driver = {
951 .probe = omap_dm_timer_probe,
952 .remove = omap_dm_timer_remove,
953 .driver = {
954 .name = "omap_timer",
955 .of_match_table = of_match_ptr(omap_timer_match),
956 .pm = &omap_dm_timer_pm_ops,
960 module_platform_driver(omap_dm_timer_driver);
962 MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
963 MODULE_LICENSE("GPL");
964 MODULE_AUTHOR("Texas Instruments Inc");