Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 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 | |
Daniel Lezcano | cf7f46b | 2018-01-08 14:28:45 +0100 | [diff] [blame] | 27 | /** |
| 28 | * timer_of_irq_exit - Release the interrupt |
| 29 | * @of_irq: an of_timer_irq structure pointer |
| 30 | * |
| 31 | * Free the irq resource |
| 32 | */ |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 33 | static __init void timer_of_irq_exit(struct of_timer_irq *of_irq) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 34 | { |
| 35 | struct timer_of *to = container_of(of_irq, struct timer_of, of_irq); |
| 36 | |
| 37 | struct clock_event_device *clkevt = &to->clkevt; |
| 38 | |
| 39 | of_irq->percpu ? free_percpu_irq(of_irq->irq, clkevt) : |
| 40 | free_irq(of_irq->irq, clkevt); |
| 41 | } |
| 42 | |
Daniel Lezcano | cf7f46b | 2018-01-08 14:28:45 +0100 | [diff] [blame] | 43 | /** |
| 44 | * timer_of_irq_init - Request the interrupt |
| 45 | * @np: a device tree node pointer |
| 46 | * @of_irq: an of_timer_irq structure pointer |
| 47 | * |
| 48 | * Get the interrupt number from the DT from its definition and |
| 49 | * request it. The interrupt is gotten by falling back the following way: |
| 50 | * |
| 51 | * - Get interrupt number by name |
| 52 | * - Get interrupt number by index |
| 53 | * |
| 54 | * When the interrupt is per CPU, 'request_percpu_irq()' is called, |
| 55 | * otherwise 'request_irq()' is used. |
| 56 | * |
| 57 | * Returns 0 on success, < 0 otherwise |
| 58 | */ |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 59 | static __init int timer_of_irq_init(struct device_node *np, |
| 60 | struct of_timer_irq *of_irq) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 61 | { |
| 62 | int ret; |
| 63 | struct timer_of *to = container_of(of_irq, struct timer_of, of_irq); |
| 64 | struct clock_event_device *clkevt = &to->clkevt; |
| 65 | |
Sergei Shtylyov | 32f2fea | 2017-07-17 21:00:44 +0300 | [diff] [blame] | 66 | if (of_irq->name) { |
| 67 | of_irq->irq = ret = of_irq_get_byname(np, of_irq->name); |
| 68 | if (ret < 0) { |
| 69 | pr_err("Failed to get interrupt %s for %s\n", |
| 70 | of_irq->name, np->full_name); |
| 71 | return ret; |
| 72 | } |
| 73 | } else { |
| 74 | of_irq->irq = irq_of_parse_and_map(np, of_irq->index); |
| 75 | } |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 76 | if (!of_irq->irq) { |
Rob Herring | 469869d | 2017-07-18 16:42:53 -0500 | [diff] [blame] | 77 | pr_err("Failed to map interrupt for %pOF\n", np); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | ret = of_irq->percpu ? |
| 82 | request_percpu_irq(of_irq->irq, of_irq->handler, |
| 83 | np->full_name, clkevt) : |
| 84 | request_irq(of_irq->irq, of_irq->handler, |
| 85 | of_irq->flags ? of_irq->flags : IRQF_TIMER, |
| 86 | np->full_name, clkevt); |
| 87 | if (ret) { |
Rob Herring | 469869d | 2017-07-18 16:42:53 -0500 | [diff] [blame] | 88 | pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | clkevt->irq = of_irq->irq; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Daniel Lezcano | cf7f46b | 2018-01-08 14:28:45 +0100 | [diff] [blame] | 97 | /** |
| 98 | * timer_of_clk_exit - Release the clock resources |
| 99 | * @of_clk: a of_timer_clk structure pointer |
| 100 | * |
| 101 | * Disables and releases the refcount on the clk |
| 102 | */ |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 103 | static __init void timer_of_clk_exit(struct of_timer_clk *of_clk) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 104 | { |
| 105 | of_clk->rate = 0; |
| 106 | clk_disable_unprepare(of_clk->clk); |
| 107 | clk_put(of_clk->clk); |
| 108 | } |
| 109 | |
Daniel Lezcano | cf7f46b | 2018-01-08 14:28:45 +0100 | [diff] [blame] | 110 | /** |
| 111 | * timer_of_clk_init - Initialize the clock resources |
| 112 | * @np: a device tree node pointer |
| 113 | * @of_clk: a of_timer_clk structure pointer |
| 114 | * |
| 115 | * Get the clock by name or by index, enable it and get the rate |
| 116 | * |
| 117 | * Returns 0 on success, < 0 otherwise |
| 118 | */ |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 119 | static __init int timer_of_clk_init(struct device_node *np, |
| 120 | struct of_timer_clk *of_clk) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) : |
| 125 | of_clk_get(np, of_clk->index); |
| 126 | if (IS_ERR(of_clk->clk)) { |
Rob Herring | 469869d | 2017-07-18 16:42:53 -0500 | [diff] [blame] | 127 | pr_err("Failed to get clock for %pOF\n", np); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 128 | return PTR_ERR(of_clk->clk); |
| 129 | } |
| 130 | |
| 131 | ret = clk_prepare_enable(of_clk->clk); |
| 132 | if (ret) { |
Rob Herring | 469869d | 2017-07-18 16:42:53 -0500 | [diff] [blame] | 133 | pr_err("Failed for enable clock for %pOF\n", np); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 134 | goto out_clk_put; |
| 135 | } |
| 136 | |
| 137 | of_clk->rate = clk_get_rate(of_clk->clk); |
| 138 | if (!of_clk->rate) { |
| 139 | ret = -EINVAL; |
Rob Herring | 469869d | 2017-07-18 16:42:53 -0500 | [diff] [blame] | 140 | pr_err("Failed to get clock rate for %pOF\n", np); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 141 | goto out_clk_disable; |
| 142 | } |
| 143 | |
| 144 | of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ); |
| 145 | out: |
| 146 | return ret; |
| 147 | |
| 148 | out_clk_disable: |
| 149 | clk_disable_unprepare(of_clk->clk); |
| 150 | out_clk_put: |
| 151 | clk_put(of_clk->clk); |
| 152 | |
| 153 | goto out; |
| 154 | } |
| 155 | |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 156 | static __init void timer_of_base_exit(struct of_timer_base *of_base) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 157 | { |
| 158 | iounmap(of_base->base); |
| 159 | } |
| 160 | |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 161 | static __init int timer_of_base_init(struct device_node *np, |
| 162 | struct of_timer_base *of_base) |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 163 | { |
Daniel Lezcano | 9aea417 | 2018-01-08 14:28:49 +0100 | [diff] [blame] | 164 | of_base->base = of_base->name ? |
| 165 | of_io_request_and_map(np, of_base->index, of_base->name) : |
| 166 | of_iomap(np, of_base->index); |
Dan Carpenter | 9e80dbd | 2017-07-10 10:22:25 +0300 | [diff] [blame] | 167 | if (IS_ERR(of_base->base)) { |
Daniel Lezcano | 9aea417 | 2018-01-08 14:28:49 +0100 | [diff] [blame] | 168 | pr_err("Failed to iomap (%s)\n", of_base->name); |
Dan Carpenter | 9e80dbd | 2017-07-10 10:22:25 +0300 | [diff] [blame] | 169 | return PTR_ERR(of_base->base); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int __init timer_of_init(struct device_node *np, struct timer_of *to) |
| 176 | { |
Arnd Bergmann | b7dcc4e | 2017-06-21 23:49:54 +0200 | [diff] [blame] | 177 | int ret = -EINVAL; |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 178 | int flags = 0; |
| 179 | |
| 180 | if (to->flags & TIMER_OF_BASE) { |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 181 | ret = timer_of_base_init(np, &to->of_base); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 182 | if (ret) |
| 183 | goto out_fail; |
| 184 | flags |= TIMER_OF_BASE; |
| 185 | } |
| 186 | |
| 187 | if (to->flags & TIMER_OF_CLOCK) { |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 188 | ret = timer_of_clk_init(np, &to->of_clk); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 189 | if (ret) |
| 190 | goto out_fail; |
| 191 | flags |= TIMER_OF_CLOCK; |
| 192 | } |
| 193 | |
| 194 | if (to->flags & TIMER_OF_IRQ) { |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 195 | ret = timer_of_irq_init(np, &to->of_irq); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 196 | if (ret) |
| 197 | goto out_fail; |
| 198 | flags |= TIMER_OF_IRQ; |
| 199 | } |
| 200 | |
| 201 | if (!to->clkevt.name) |
| 202 | to->clkevt.name = np->name; |
Daniel Lezcano | 1c63c1c | 2018-01-08 14:28:48 +0100 | [diff] [blame] | 203 | |
| 204 | to->np = np; |
| 205 | |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 206 | return ret; |
| 207 | |
| 208 | out_fail: |
| 209 | if (flags & TIMER_OF_IRQ) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 210 | timer_of_irq_exit(&to->of_irq); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 211 | |
| 212 | if (flags & TIMER_OF_CLOCK) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 213 | timer_of_clk_exit(&to->of_clk); |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 214 | |
| 215 | if (flags & TIMER_OF_BASE) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 216 | timer_of_base_exit(&to->of_base); |
Arnd Bergmann | b7dcc4e | 2017-06-21 23:49:54 +0200 | [diff] [blame] | 217 | return ret; |
Daniel Lezcano | dc11bae | 2017-06-05 00:18:43 +0200 | [diff] [blame] | 218 | } |
Benjamin Gaignard | f48729a | 2017-10-23 11:58:37 +0200 | [diff] [blame] | 219 | |
Benjamin Gaignard | 558de28 | 2017-11-14 09:52:38 +0100 | [diff] [blame] | 220 | /** |
| 221 | * timer_of_cleanup - release timer_of ressources |
| 222 | * @to: timer_of structure |
| 223 | * |
| 224 | * Release the ressources that has been used in timer_of_init(). |
| 225 | * This function should be called in init error cases |
| 226 | */ |
| 227 | void __init timer_of_cleanup(struct timer_of *to) |
Benjamin Gaignard | f48729a | 2017-10-23 11:58:37 +0200 | [diff] [blame] | 228 | { |
| 229 | if (to->flags & TIMER_OF_IRQ) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 230 | timer_of_irq_exit(&to->of_irq); |
Benjamin Gaignard | f48729a | 2017-10-23 11:58:37 +0200 | [diff] [blame] | 231 | |
| 232 | if (to->flags & TIMER_OF_CLOCK) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 233 | timer_of_clk_exit(&to->of_clk); |
Benjamin Gaignard | f48729a | 2017-10-23 11:58:37 +0200 | [diff] [blame] | 234 | |
| 235 | if (to->flags & TIMER_OF_BASE) |
Daniel Lezcano | 5bbf4ad | 2018-01-08 14:28:44 +0100 | [diff] [blame] | 236 | timer_of_base_exit(&to->of_base); |
Benjamin Gaignard | f48729a | 2017-10-23 11:58:37 +0200 | [diff] [blame] | 237 | } |