Commit | Line | Data |
---|---|---|
dc11bae7 DL |
1 | /* |
2 | * Copyright (c) 2017, Linaro Ltd. All rights reserved. | |
3 | * | |
4 | * Author: Daniel Lezcano <daniel.lezcano@linaro.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | #include <linux/clk.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/of.h> | |
21 | #include <linux/of_address.h> | |
22 | #include <linux/of_irq.h> | |
23 | #include <linux/slab.h> | |
24 | ||
25 | #include "timer-of.h" | |
26 | ||
5bbf4ad9 | 27 | static __init void timer_of_irq_exit(struct of_timer_irq *of_irq) |
dc11bae7 DL |
28 | { |
29 | struct timer_of *to = container_of(of_irq, struct timer_of, of_irq); | |
30 | ||
31 | struct clock_event_device *clkevt = &to->clkevt; | |
32 | ||
33 | of_irq->percpu ? free_percpu_irq(of_irq->irq, clkevt) : | |
34 | free_irq(of_irq->irq, clkevt); | |
35 | } | |
36 | ||
5bbf4ad9 DL |
37 | static __init int timer_of_irq_init(struct device_node *np, |
38 | struct of_timer_irq *of_irq) | |
dc11bae7 DL |
39 | { |
40 | int ret; | |
41 | struct timer_of *to = container_of(of_irq, struct timer_of, of_irq); | |
42 | struct clock_event_device *clkevt = &to->clkevt; | |
43 | ||
32f2fea6 SS |
44 | if (of_irq->name) { |
45 | of_irq->irq = ret = of_irq_get_byname(np, of_irq->name); | |
46 | if (ret < 0) { | |
47 | pr_err("Failed to get interrupt %s for %s\n", | |
48 | of_irq->name, np->full_name); | |
49 | return ret; | |
50 | } | |
51 | } else { | |
52 | of_irq->irq = irq_of_parse_and_map(np, of_irq->index); | |
53 | } | |
dc11bae7 | 54 | if (!of_irq->irq) { |
469869d1 | 55 | pr_err("Failed to map interrupt for %pOF\n", np); |
dc11bae7 DL |
56 | return -EINVAL; |
57 | } | |
58 | ||
59 | ret = of_irq->percpu ? | |
60 | request_percpu_irq(of_irq->irq, of_irq->handler, | |
61 | np->full_name, clkevt) : | |
62 | request_irq(of_irq->irq, of_irq->handler, | |
63 | of_irq->flags ? of_irq->flags : IRQF_TIMER, | |
64 | np->full_name, clkevt); | |
65 | if (ret) { | |
469869d1 | 66 | pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np); |
dc11bae7 DL |
67 | return ret; |
68 | } | |
69 | ||
70 | clkevt->irq = of_irq->irq; | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
5bbf4ad9 | 75 | static __init void timer_of_clk_exit(struct of_timer_clk *of_clk) |
dc11bae7 DL |
76 | { |
77 | of_clk->rate = 0; | |
78 | clk_disable_unprepare(of_clk->clk); | |
79 | clk_put(of_clk->clk); | |
80 | } | |
81 | ||
5bbf4ad9 DL |
82 | static __init int timer_of_clk_init(struct device_node *np, |
83 | struct of_timer_clk *of_clk) | |
dc11bae7 DL |
84 | { |
85 | int ret; | |
86 | ||
87 | of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) : | |
88 | of_clk_get(np, of_clk->index); | |
89 | if (IS_ERR(of_clk->clk)) { | |
469869d1 | 90 | pr_err("Failed to get clock for %pOF\n", np); |
dc11bae7 DL |
91 | return PTR_ERR(of_clk->clk); |
92 | } | |
93 | ||
94 | ret = clk_prepare_enable(of_clk->clk); | |
95 | if (ret) { | |
469869d1 | 96 | pr_err("Failed for enable clock for %pOF\n", np); |
dc11bae7 DL |
97 | goto out_clk_put; |
98 | } | |
99 | ||
100 | of_clk->rate = clk_get_rate(of_clk->clk); | |
101 | if (!of_clk->rate) { | |
102 | ret = -EINVAL; | |
469869d1 | 103 | pr_err("Failed to get clock rate for %pOF\n", np); |
dc11bae7 DL |
104 | goto out_clk_disable; |
105 | } | |
106 | ||
107 | of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ); | |
108 | out: | |
109 | return ret; | |
110 | ||
111 | out_clk_disable: | |
112 | clk_disable_unprepare(of_clk->clk); | |
113 | out_clk_put: | |
114 | clk_put(of_clk->clk); | |
115 | ||
116 | goto out; | |
117 | } | |
118 | ||
5bbf4ad9 | 119 | static __init void timer_of_base_exit(struct of_timer_base *of_base) |
dc11bae7 DL |
120 | { |
121 | iounmap(of_base->base); | |
122 | } | |
123 | ||
5bbf4ad9 DL |
124 | static __init int timer_of_base_init(struct device_node *np, |
125 | struct of_timer_base *of_base) | |
dc11bae7 DL |
126 | { |
127 | const char *name = of_base->name ? of_base->name : np->full_name; | |
128 | ||
129 | of_base->base = of_io_request_and_map(np, of_base->index, name); | |
9e80dbd8 | 130 | if (IS_ERR(of_base->base)) { |
dc11bae7 | 131 | pr_err("Failed to iomap (%s)\n", name); |
9e80dbd8 | 132 | return PTR_ERR(of_base->base); |
dc11bae7 DL |
133 | } |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | int __init timer_of_init(struct device_node *np, struct timer_of *to) | |
139 | { | |
b7dcc4ea | 140 | int ret = -EINVAL; |
dc11bae7 DL |
141 | int flags = 0; |
142 | ||
143 | if (to->flags & TIMER_OF_BASE) { | |
5bbf4ad9 | 144 | ret = timer_of_base_init(np, &to->of_base); |
dc11bae7 DL |
145 | if (ret) |
146 | goto out_fail; | |
147 | flags |= TIMER_OF_BASE; | |
148 | } | |
149 | ||
150 | if (to->flags & TIMER_OF_CLOCK) { | |
5bbf4ad9 | 151 | ret = timer_of_clk_init(np, &to->of_clk); |
dc11bae7 DL |
152 | if (ret) |
153 | goto out_fail; | |
154 | flags |= TIMER_OF_CLOCK; | |
155 | } | |
156 | ||
157 | if (to->flags & TIMER_OF_IRQ) { | |
5bbf4ad9 | 158 | ret = timer_of_irq_init(np, &to->of_irq); |
dc11bae7 DL |
159 | if (ret) |
160 | goto out_fail; | |
161 | flags |= TIMER_OF_IRQ; | |
162 | } | |
163 | ||
164 | if (!to->clkevt.name) | |
165 | to->clkevt.name = np->name; | |
dc11bae7 DL |
166 | return ret; |
167 | ||
168 | out_fail: | |
169 | if (flags & TIMER_OF_IRQ) | |
5bbf4ad9 | 170 | timer_of_irq_exit(&to->of_irq); |
dc11bae7 DL |
171 | |
172 | if (flags & TIMER_OF_CLOCK) | |
5bbf4ad9 | 173 | timer_of_clk_exit(&to->of_clk); |
dc11bae7 DL |
174 | |
175 | if (flags & TIMER_OF_BASE) | |
5bbf4ad9 | 176 | timer_of_base_exit(&to->of_base); |
b7dcc4ea | 177 | return ret; |
dc11bae7 | 178 | } |
f48729a9 | 179 | |
558de282 BG |
180 | /** |
181 | * timer_of_cleanup - release timer_of ressources | |
182 | * @to: timer_of structure | |
183 | * | |
184 | * Release the ressources that has been used in timer_of_init(). | |
185 | * This function should be called in init error cases | |
186 | */ | |
187 | void __init timer_of_cleanup(struct timer_of *to) | |
f48729a9 BG |
188 | { |
189 | if (to->flags & TIMER_OF_IRQ) | |
5bbf4ad9 | 190 | timer_of_irq_exit(&to->of_irq); |
f48729a9 BG |
191 | |
192 | if (to->flags & TIMER_OF_CLOCK) | |
5bbf4ad9 | 193 | timer_of_clk_exit(&to->of_clk); |
f48729a9 BG |
194 | |
195 | if (to->flags & TIMER_OF_BASE) | |
5bbf4ad9 | 196 | timer_of_base_exit(&to->of_base); |
f48729a9 | 197 | } |