timer-of.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "timer-of.h"
  25. static __init void timer_irq_exit(struct of_timer_irq *of_irq)
  26. {
  27. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  28. struct clock_event_device *clkevt = &to->clkevt;
  29. of_irq->percpu ? free_percpu_irq(of_irq->irq, clkevt) :
  30. free_irq(of_irq->irq, clkevt);
  31. }
  32. static __init int timer_irq_init(struct device_node *np,
  33. struct of_timer_irq *of_irq)
  34. {
  35. int ret;
  36. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  37. struct clock_event_device *clkevt = &to->clkevt;
  38. if (of_irq->name) {
  39. of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
  40. if (ret < 0) {
  41. pr_err("Failed to get interrupt %s for %s\n",
  42. of_irq->name, np->full_name);
  43. return ret;
  44. }
  45. } else {
  46. of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
  47. }
  48. if (!of_irq->irq) {
  49. pr_err("Failed to map interrupt for %pOF\n", np);
  50. return -EINVAL;
  51. }
  52. ret = of_irq->percpu ?
  53. request_percpu_irq(of_irq->irq, of_irq->handler,
  54. np->full_name, clkevt) :
  55. request_irq(of_irq->irq, of_irq->handler,
  56. of_irq->flags ? of_irq->flags : IRQF_TIMER,
  57. np->full_name, clkevt);
  58. if (ret) {
  59. pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
  60. return ret;
  61. }
  62. clkevt->irq = of_irq->irq;
  63. return 0;
  64. }
  65. static __init void timer_clk_exit(struct of_timer_clk *of_clk)
  66. {
  67. of_clk->rate = 0;
  68. clk_disable_unprepare(of_clk->clk);
  69. clk_put(of_clk->clk);
  70. }
  71. static __init int timer_clk_init(struct device_node *np,
  72. struct of_timer_clk *of_clk)
  73. {
  74. int ret;
  75. of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
  76. of_clk_get(np, of_clk->index);
  77. if (IS_ERR(of_clk->clk)) {
  78. pr_err("Failed to get clock for %pOF\n", np);
  79. return PTR_ERR(of_clk->clk);
  80. }
  81. ret = clk_prepare_enable(of_clk->clk);
  82. if (ret) {
  83. pr_err("Failed for enable clock for %pOF\n", np);
  84. goto out_clk_put;
  85. }
  86. of_clk->rate = clk_get_rate(of_clk->clk);
  87. if (!of_clk->rate) {
  88. ret = -EINVAL;
  89. pr_err("Failed to get clock rate for %pOF\n", np);
  90. goto out_clk_disable;
  91. }
  92. of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
  93. out:
  94. return ret;
  95. out_clk_disable:
  96. clk_disable_unprepare(of_clk->clk);
  97. out_clk_put:
  98. clk_put(of_clk->clk);
  99. goto out;
  100. }
  101. static __init void timer_base_exit(struct of_timer_base *of_base)
  102. {
  103. iounmap(of_base->base);
  104. }
  105. static __init int timer_base_init(struct device_node *np,
  106. struct of_timer_base *of_base)
  107. {
  108. const char *name = of_base->name ? of_base->name : np->full_name;
  109. of_base->base = of_io_request_and_map(np, of_base->index, name);
  110. if (IS_ERR(of_base->base)) {
  111. pr_err("Failed to iomap (%s)\n", name);
  112. return PTR_ERR(of_base->base);
  113. }
  114. return 0;
  115. }
  116. int __init timer_of_init(struct device_node *np, struct timer_of *to)
  117. {
  118. int ret = -EINVAL;
  119. int flags = 0;
  120. if (to->flags & TIMER_OF_BASE) {
  121. ret = timer_base_init(np, &to->of_base);
  122. if (ret)
  123. goto out_fail;
  124. flags |= TIMER_OF_BASE;
  125. }
  126. if (to->flags & TIMER_OF_CLOCK) {
  127. ret = timer_clk_init(np, &to->of_clk);
  128. if (ret)
  129. goto out_fail;
  130. flags |= TIMER_OF_CLOCK;
  131. }
  132. if (to->flags & TIMER_OF_IRQ) {
  133. ret = timer_irq_init(np, &to->of_irq);
  134. if (ret)
  135. goto out_fail;
  136. flags |= TIMER_OF_IRQ;
  137. }
  138. if (!to->clkevt.name)
  139. to->clkevt.name = np->name;
  140. return ret;
  141. out_fail:
  142. if (flags & TIMER_OF_IRQ)
  143. timer_irq_exit(&to->of_irq);
  144. if (flags & TIMER_OF_CLOCK)
  145. timer_clk_exit(&to->of_clk);
  146. if (flags & TIMER_OF_BASE)
  147. timer_base_exit(&to->of_base);
  148. return ret;
  149. }