timer-of.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017, Linaro Ltd. All rights reserved.
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/slab.h>
  13. #include "timer-of.h"
  14. /**
  15. * timer_of_irq_exit - Release the interrupt
  16. * @of_irq: an of_timer_irq structure pointer
  17. *
  18. * Free the irq resource
  19. */
  20. static void timer_of_irq_exit(struct of_timer_irq *of_irq)
  21. {
  22. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  23. struct clock_event_device *clkevt = &to->clkevt;
  24. if (of_irq->percpu)
  25. free_percpu_irq(of_irq->irq, clkevt);
  26. else
  27. free_irq(of_irq->irq, clkevt);
  28. }
  29. /**
  30. * timer_of_irq_init - Request the interrupt
  31. * @np: a device tree node pointer
  32. * @of_irq: an of_timer_irq structure pointer
  33. *
  34. * Get the interrupt number from the DT from its definition and
  35. * request it. The interrupt is gotten by falling back the following way:
  36. *
  37. * - Get interrupt number by name
  38. * - Get interrupt number by index
  39. *
  40. * When the interrupt is per CPU, 'request_percpu_irq()' is called,
  41. * otherwise 'request_irq()' is used.
  42. *
  43. * Returns 0 on success, < 0 otherwise
  44. */
  45. static int timer_of_irq_init(struct device_node *np,
  46. struct of_timer_irq *of_irq)
  47. {
  48. int ret;
  49. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  50. struct clock_event_device *clkevt = &to->clkevt;
  51. if (of_irq->name) {
  52. of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
  53. if (ret < 0) {
  54. pr_err("Failed to get interrupt %s for %pOF\n",
  55. of_irq->name, np);
  56. return ret;
  57. }
  58. } else {
  59. of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
  60. }
  61. if (!of_irq->irq) {
  62. pr_err("Failed to map interrupt for %pOF\n", np);
  63. return -EINVAL;
  64. }
  65. ret = of_irq->percpu ?
  66. request_percpu_irq(of_irq->irq, of_irq->handler,
  67. np->full_name, clkevt) :
  68. request_irq(of_irq->irq, of_irq->handler,
  69. of_irq->flags ? of_irq->flags : IRQF_TIMER,
  70. np->full_name, clkevt);
  71. if (ret) {
  72. pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
  73. return ret;
  74. }
  75. clkevt->irq = of_irq->irq;
  76. return 0;
  77. }
  78. /**
  79. * timer_of_clk_exit - Release the clock resources
  80. * @of_clk: a of_timer_clk structure pointer
  81. *
  82. * Disables and releases the refcount on the clk
  83. */
  84. static void timer_of_clk_exit(struct of_timer_clk *of_clk)
  85. {
  86. of_clk->rate = 0;
  87. clk_disable_unprepare(of_clk->clk);
  88. clk_put(of_clk->clk);
  89. }
  90. /**
  91. * timer_of_clk_init - Initialize the clock resources
  92. * @np: a device tree node pointer
  93. * @of_clk: a of_timer_clk structure pointer
  94. *
  95. * Get the clock by name or by index, enable it and get the rate
  96. *
  97. * Returns 0 on success, < 0 otherwise
  98. */
  99. static int timer_of_clk_init(struct device_node *np,
  100. struct of_timer_clk *of_clk)
  101. {
  102. int ret;
  103. of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
  104. of_clk_get(np, of_clk->index);
  105. if (IS_ERR(of_clk->clk)) {
  106. ret = PTR_ERR(of_clk->clk);
  107. if (ret != -EPROBE_DEFER)
  108. pr_err("Failed to get clock for %pOF\n", np);
  109. goto out;
  110. }
  111. ret = clk_prepare_enable(of_clk->clk);
  112. if (ret) {
  113. pr_err("Failed for enable clock for %pOF\n", np);
  114. goto out_clk_put;
  115. }
  116. of_clk->rate = clk_get_rate(of_clk->clk);
  117. if (!of_clk->rate) {
  118. ret = -EINVAL;
  119. pr_err("Failed to get clock rate for %pOF\n", np);
  120. goto out_clk_disable;
  121. }
  122. of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
  123. out:
  124. return ret;
  125. out_clk_disable:
  126. clk_disable_unprepare(of_clk->clk);
  127. out_clk_put:
  128. clk_put(of_clk->clk);
  129. goto out;
  130. }
  131. static void timer_of_base_exit(struct of_timer_base *of_base)
  132. {
  133. iounmap(of_base->base);
  134. }
  135. static int timer_of_base_init(struct device_node *np,
  136. struct of_timer_base *of_base)
  137. {
  138. of_base->base = of_base->name ?
  139. of_io_request_and_map(np, of_base->index, of_base->name) :
  140. of_iomap(np, of_base->index);
  141. if (IS_ERR_OR_NULL(of_base->base)) {
  142. pr_err("Failed to iomap (%s:%s)\n", np->name, of_base->name);
  143. return of_base->base ? PTR_ERR(of_base->base) : -ENOMEM;
  144. }
  145. return 0;
  146. }
  147. int timer_of_init(struct device_node *np, struct timer_of *to)
  148. {
  149. int ret = -EINVAL;
  150. int flags = 0;
  151. if (to->flags & TIMER_OF_BASE) {
  152. ret = timer_of_base_init(np, &to->of_base);
  153. if (ret)
  154. goto out_fail;
  155. flags |= TIMER_OF_BASE;
  156. }
  157. if (to->flags & TIMER_OF_CLOCK) {
  158. ret = timer_of_clk_init(np, &to->of_clk);
  159. if (ret)
  160. goto out_fail;
  161. flags |= TIMER_OF_CLOCK;
  162. }
  163. if (to->flags & TIMER_OF_IRQ) {
  164. ret = timer_of_irq_init(np, &to->of_irq);
  165. if (ret)
  166. goto out_fail;
  167. flags |= TIMER_OF_IRQ;
  168. }
  169. if (!to->clkevt.name)
  170. to->clkevt.name = np->full_name;
  171. to->np = np;
  172. return ret;
  173. out_fail:
  174. if (flags & TIMER_OF_IRQ)
  175. timer_of_irq_exit(&to->of_irq);
  176. if (flags & TIMER_OF_CLOCK)
  177. timer_of_clk_exit(&to->of_clk);
  178. if (flags & TIMER_OF_BASE)
  179. timer_of_base_exit(&to->of_base);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(timer_of_init);
  183. /**
  184. * timer_of_cleanup - release timer_of ressources
  185. * @to: timer_of structure
  186. *
  187. * Release the ressources that has been used in timer_of_init().
  188. * This function should be called in init error cases
  189. */
  190. void timer_of_cleanup(struct timer_of *to)
  191. {
  192. if (to->flags & TIMER_OF_IRQ)
  193. timer_of_irq_exit(&to->of_irq);
  194. if (to->flags & TIMER_OF_CLOCK)
  195. timer_of_clk_exit(&to->of_clk);
  196. if (to->flags & TIMER_OF_BASE)
  197. timer_of_base_exit(&to->of_base);
  198. }