1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017, Linaro Ltd. All rights reserved.
8 #include <linux/interrupt.h>
10 #include <linux/of_address.h>
11 #include <linux/of_irq.h>
12 #include <linux/slab.h>
17 * timer_of_irq_exit - Release the interrupt
18 * @of_irq: an of_timer_irq structure pointer
20 * Free the irq resource
22 static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
24 struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
26 struct clock_event_device *clkevt = &to->clkevt;
28 of_irq->percpu ? free_percpu_irq(of_irq->irq, clkevt) :
29 free_irq(of_irq->irq, clkevt);
33 * timer_of_irq_init - Request the interrupt
34 * @np: a device tree node pointer
35 * @of_irq: an of_timer_irq structure pointer
37 * Get the interrupt number from the DT from its definition and
38 * request it. The interrupt is gotten by falling back the following way:
40 * - Get interrupt number by name
41 * - Get interrupt number by index
43 * When the interrupt is per CPU, 'request_percpu_irq()' is called,
44 * otherwise 'request_irq()' is used.
46 * Returns 0 on success, < 0 otherwise
48 static __init int timer_of_irq_init(struct device_node *np,
49 struct of_timer_irq *of_irq)
52 struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
53 struct clock_event_device *clkevt = &to->clkevt;
56 of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
58 pr_err("Failed to get interrupt %s for %s\n",
59 of_irq->name, np->full_name);
63 of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
66 pr_err("Failed to map interrupt for %pOF\n", np);
70 ret = of_irq->percpu ?
71 request_percpu_irq(of_irq->irq, of_irq->handler,
72 np->full_name, clkevt) :
73 request_irq(of_irq->irq, of_irq->handler,
74 of_irq->flags ? of_irq->flags : IRQF_TIMER,
75 np->full_name, clkevt);
77 pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
81 clkevt->irq = of_irq->irq;
87 * timer_of_clk_exit - Release the clock resources
88 * @of_clk: a of_timer_clk structure pointer
90 * Disables and releases the refcount on the clk
92 static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
95 clk_disable_unprepare(of_clk->clk);
100 * timer_of_clk_init - Initialize the clock resources
101 * @np: a device tree node pointer
102 * @of_clk: a of_timer_clk structure pointer
104 * Get the clock by name or by index, enable it and get the rate
106 * Returns 0 on success, < 0 otherwise
108 static __init int timer_of_clk_init(struct device_node *np,
109 struct of_timer_clk *of_clk)
113 of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
114 of_clk_get(np, of_clk->index);
115 if (IS_ERR(of_clk->clk)) {
116 ret = PTR_ERR(of_clk->clk);
117 if (ret != -EPROBE_DEFER)
118 pr_err("Failed to get clock for %pOF\n", np);
122 ret = clk_prepare_enable(of_clk->clk);
124 pr_err("Failed for enable clock for %pOF\n", np);
128 of_clk->rate = clk_get_rate(of_clk->clk);
131 pr_err("Failed to get clock rate for %pOF\n", np);
132 goto out_clk_disable;
135 of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
140 clk_disable_unprepare(of_clk->clk);
142 clk_put(of_clk->clk);
147 static __init void timer_of_base_exit(struct of_timer_base *of_base)
149 iounmap(of_base->base);
152 static __init int timer_of_base_init(struct device_node *np,
153 struct of_timer_base *of_base)
155 of_base->base = of_base->name ?
156 of_io_request_and_map(np, of_base->index, of_base->name) :
157 of_iomap(np, of_base->index);
158 if (IS_ERR(of_base->base)) {
159 pr_err("Failed to iomap (%s)\n", of_base->name);
160 return PTR_ERR(of_base->base);
166 int __init timer_of_init(struct device_node *np, struct timer_of *to)
171 if (to->flags & TIMER_OF_BASE) {
172 ret = timer_of_base_init(np, &to->of_base);
175 flags |= TIMER_OF_BASE;
178 if (to->flags & TIMER_OF_CLOCK) {
179 ret = timer_of_clk_init(np, &to->of_clk);
182 flags |= TIMER_OF_CLOCK;
185 if (to->flags & TIMER_OF_IRQ) {
186 ret = timer_of_irq_init(np, &to->of_irq);
189 flags |= TIMER_OF_IRQ;
192 if (!to->clkevt.name)
193 to->clkevt.name = np->name;
200 if (flags & TIMER_OF_IRQ)
201 timer_of_irq_exit(&to->of_irq);
203 if (flags & TIMER_OF_CLOCK)
204 timer_of_clk_exit(&to->of_clk);
206 if (flags & TIMER_OF_BASE)
207 timer_of_base_exit(&to->of_base);
212 * timer_of_cleanup - release timer_of ressources
213 * @to: timer_of structure
215 * Release the ressources that has been used in timer_of_init().
216 * This function should be called in init error cases
218 void __init timer_of_cleanup(struct timer_of *to)
220 if (to->flags & TIMER_OF_IRQ)
221 timer_of_irq_exit(&to->of_irq);
223 if (to->flags & TIMER_OF_CLOCK)
224 timer_of_clk_exit(&to->of_clk);
226 if (to->flags & TIMER_OF_BASE)
227 timer_of_base_exit(&to->of_base);