]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/clocksource/timer-of.c
Merge tag 'linux-kselftest-4.13-rc6-fixes' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-artful-kernel.git] / drivers / clocksource / timer-of.c
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
27 static __init void timer_irq_exit(struct of_timer_irq *of_irq)
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
37 static __init int timer_irq_init(struct device_node *np,
38 struct of_timer_irq *of_irq)
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
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 }
54 if (!of_irq->irq) {
55 pr_err("Failed to map interrupt for %s\n", np->full_name);
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) {
66 pr_err("Failed to request irq %d for %s\n", of_irq->irq,
67 np->full_name);
68 return ret;
69 }
70
71 clkevt->irq = of_irq->irq;
72
73 return 0;
74 }
75
76 static __init void timer_clk_exit(struct of_timer_clk *of_clk)
77 {
78 of_clk->rate = 0;
79 clk_disable_unprepare(of_clk->clk);
80 clk_put(of_clk->clk);
81 }
82
83 static __init int timer_clk_init(struct device_node *np,
84 struct of_timer_clk *of_clk)
85 {
86 int ret;
87
88 of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
89 of_clk_get(np, of_clk->index);
90 if (IS_ERR(of_clk->clk)) {
91 pr_err("Failed to get clock for %s\n", np->full_name);
92 return PTR_ERR(of_clk->clk);
93 }
94
95 ret = clk_prepare_enable(of_clk->clk);
96 if (ret) {
97 pr_err("Failed for enable clock for %s\n", np->full_name);
98 goto out_clk_put;
99 }
100
101 of_clk->rate = clk_get_rate(of_clk->clk);
102 if (!of_clk->rate) {
103 ret = -EINVAL;
104 pr_err("Failed to get clock rate for %s\n", np->full_name);
105 goto out_clk_disable;
106 }
107
108 of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
109 out:
110 return ret;
111
112 out_clk_disable:
113 clk_disable_unprepare(of_clk->clk);
114 out_clk_put:
115 clk_put(of_clk->clk);
116
117 goto out;
118 }
119
120 static __init void timer_base_exit(struct of_timer_base *of_base)
121 {
122 iounmap(of_base->base);
123 }
124
125 static __init int timer_base_init(struct device_node *np,
126 struct of_timer_base *of_base)
127 {
128 const char *name = of_base->name ? of_base->name : np->full_name;
129
130 of_base->base = of_io_request_and_map(np, of_base->index, name);
131 if (!of_base->base) {
132 pr_err("Failed to iomap (%s)\n", name);
133 return -ENXIO;
134 }
135
136 return 0;
137 }
138
139 int __init timer_of_init(struct device_node *np, struct timer_of *to)
140 {
141 int ret = -EINVAL;
142 int flags = 0;
143
144 if (to->flags & TIMER_OF_BASE) {
145 ret = timer_base_init(np, &to->of_base);
146 if (ret)
147 goto out_fail;
148 flags |= TIMER_OF_BASE;
149 }
150
151 if (to->flags & TIMER_OF_CLOCK) {
152 ret = timer_clk_init(np, &to->of_clk);
153 if (ret)
154 goto out_fail;
155 flags |= TIMER_OF_CLOCK;
156 }
157
158 if (to->flags & TIMER_OF_IRQ) {
159 ret = timer_irq_init(np, &to->of_irq);
160 if (ret)
161 goto out_fail;
162 flags |= TIMER_OF_IRQ;
163 }
164
165 if (!to->clkevt.name)
166 to->clkevt.name = np->name;
167 return ret;
168
169 out_fail:
170 if (flags & TIMER_OF_IRQ)
171 timer_irq_exit(&to->of_irq);
172
173 if (flags & TIMER_OF_CLOCK)
174 timer_clk_exit(&to->of_clk);
175
176 if (flags & TIMER_OF_BASE)
177 timer_base_exit(&to->of_base);
178 return ret;
179 }