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 free_irq(of_irq->irq, clkevt);
32 * timer_of_irq_init - Request the interrupt
33 * @np: a device tree node pointer
34 * @of_irq: an of_timer_irq structure pointer
36 * Get the interrupt number from the DT from its definition and
37 * request it. The interrupt is gotten by falling back the following way:
39 * - Get interrupt number by name
40 * - Get interrupt number by index
42 * Returns 0 on success, < 0 otherwise
44 static __init int timer_of_irq_init(struct device_node *np,
45 struct of_timer_irq *of_irq)
48 struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
49 struct clock_event_device *clkevt = &to->clkevt;
52 of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
54 pr_err("Failed to get interrupt %s for %pOF\n",
59 of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
62 pr_err("Failed to map interrupt for %pOF\n", np);
66 ret = request_irq(of_irq->irq, of_irq->handler,
67 of_irq->flags ? of_irq->flags : IRQF_TIMER,
68 np->full_name, clkevt);
70 pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
74 clkevt->irq = of_irq->irq;
80 * timer_of_clk_exit - Release the clock resources
81 * @of_clk: a of_timer_clk structure pointer
83 * Disables and releases the refcount on the clk
85 static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
88 clk_disable_unprepare(of_clk->clk);
93 * timer_of_clk_init - Initialize the clock resources
94 * @np: a device tree node pointer
95 * @of_clk: a of_timer_clk structure pointer
97 * Get the clock by name or by index, enable it and get the rate
99 * Returns 0 on success, < 0 otherwise
101 static __init int timer_of_clk_init(struct device_node *np,
102 struct of_timer_clk *of_clk)
106 of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
107 of_clk_get(np, of_clk->index);
108 if (IS_ERR(of_clk->clk)) {
109 ret = PTR_ERR(of_clk->clk);
110 if (ret != -EPROBE_DEFER)
111 pr_err("Failed to get clock for %pOF\n", np);
115 ret = clk_prepare_enable(of_clk->clk);
117 pr_err("Failed for enable clock for %pOF\n", np);
121 of_clk->rate = clk_get_rate(of_clk->clk);
124 pr_err("Failed to get clock rate for %pOF\n", np);
125 goto out_clk_disable;
128 of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
133 clk_disable_unprepare(of_clk->clk);
135 clk_put(of_clk->clk);
140 static __init void timer_of_base_exit(struct of_timer_base *of_base)
142 iounmap(of_base->base);
145 static __init int timer_of_base_init(struct device_node *np,
146 struct of_timer_base *of_base)
148 of_base->base = of_base->name ?
149 of_io_request_and_map(np, of_base->index, of_base->name) :
150 of_iomap(np, of_base->index);
151 if (IS_ERR_OR_NULL(of_base->base)) {
152 pr_err("Failed to iomap (%s:%s)\n", np->name, of_base->name);
153 return of_base->base ? PTR_ERR(of_base->base) : -ENOMEM;
159 int __init timer_of_init(struct device_node *np, struct timer_of *to)
164 if (to->flags & TIMER_OF_BASE) {
165 ret = timer_of_base_init(np, &to->of_base);
168 flags |= TIMER_OF_BASE;
171 if (to->flags & TIMER_OF_CLOCK) {
172 ret = timer_of_clk_init(np, &to->of_clk);
175 flags |= TIMER_OF_CLOCK;
178 if (to->flags & TIMER_OF_IRQ) {
179 ret = timer_of_irq_init(np, &to->of_irq);
182 flags |= TIMER_OF_IRQ;
185 if (!to->clkevt.name)
186 to->clkevt.name = np->full_name;
193 if (flags & TIMER_OF_IRQ)
194 timer_of_irq_exit(&to->of_irq);
196 if (flags & TIMER_OF_CLOCK)
197 timer_of_clk_exit(&to->of_clk);
199 if (flags & TIMER_OF_BASE)
200 timer_of_base_exit(&to->of_base);
205 * timer_of_cleanup - release timer_of resources
206 * @to: timer_of structure
208 * Release the resources that has been used in timer_of_init().
209 * This function should be called in init error cases
211 void __init timer_of_cleanup(struct timer_of *to)
213 if (to->flags & TIMER_OF_IRQ)
214 timer_of_irq_exit(&to->of_irq);
216 if (to->flags & TIMER_OF_CLOCK)
217 timer_of_clk_exit(&to->of_clk);
219 if (to->flags & TIMER_OF_BASE)
220 timer_of_base_exit(&to->of_base);