clk-utmi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/io.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include "pmc.h"
  22. #define UTMI_FIXED_MUL 40
  23. struct clk_utmi {
  24. struct clk_hw hw;
  25. struct at91_pmc *pmc;
  26. unsigned int irq;
  27. wait_queue_head_t wait;
  28. };
  29. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  30. static irqreturn_t clk_utmi_irq_handler(int irq, void *dev_id)
  31. {
  32. struct clk_utmi *utmi = (struct clk_utmi *)dev_id;
  33. wake_up(&utmi->wait);
  34. disable_irq_nosync(utmi->irq);
  35. return IRQ_HANDLED;
  36. }
  37. static int clk_utmi_prepare(struct clk_hw *hw)
  38. {
  39. struct clk_utmi *utmi = to_clk_utmi(hw);
  40. struct at91_pmc *pmc = utmi->pmc;
  41. u32 tmp = at91_pmc_read(AT91_CKGR_UCKR) | AT91_PMC_UPLLEN |
  42. AT91_PMC_UPLLCOUNT | AT91_PMC_BIASEN;
  43. pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  44. while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU)) {
  45. enable_irq(utmi->irq);
  46. wait_event(utmi->wait,
  47. pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  48. }
  49. return 0;
  50. }
  51. static int clk_utmi_is_prepared(struct clk_hw *hw)
  52. {
  53. struct clk_utmi *utmi = to_clk_utmi(hw);
  54. struct at91_pmc *pmc = utmi->pmc;
  55. return !!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  56. }
  57. static void clk_utmi_unprepare(struct clk_hw *hw)
  58. {
  59. struct clk_utmi *utmi = to_clk_utmi(hw);
  60. struct at91_pmc *pmc = utmi->pmc;
  61. u32 tmp = at91_pmc_read(AT91_CKGR_UCKR) & ~AT91_PMC_UPLLEN;
  62. pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  63. }
  64. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  65. unsigned long parent_rate)
  66. {
  67. /* UTMI clk is a fixed clk multiplier */
  68. return parent_rate * UTMI_FIXED_MUL;
  69. }
  70. static const struct clk_ops utmi_ops = {
  71. .prepare = clk_utmi_prepare,
  72. .unprepare = clk_utmi_unprepare,
  73. .is_prepared = clk_utmi_is_prepared,
  74. .recalc_rate = clk_utmi_recalc_rate,
  75. };
  76. static struct clk * __init
  77. at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  78. const char *name, const char *parent_name)
  79. {
  80. int ret;
  81. struct clk_utmi *utmi;
  82. struct clk *clk = NULL;
  83. struct clk_init_data init;
  84. utmi = kzalloc(sizeof(*utmi), GFP_KERNEL);
  85. if (!utmi)
  86. return ERR_PTR(-ENOMEM);
  87. init.name = name;
  88. init.ops = &utmi_ops;
  89. init.parent_names = parent_name ? &parent_name : NULL;
  90. init.num_parents = parent_name ? 1 : 0;
  91. init.flags = CLK_SET_RATE_GATE;
  92. utmi->hw.init = &init;
  93. utmi->pmc = pmc;
  94. utmi->irq = irq;
  95. init_waitqueue_head(&utmi->wait);
  96. irq_set_status_flags(utmi->irq, IRQ_NOAUTOEN);
  97. ret = request_irq(utmi->irq, clk_utmi_irq_handler,
  98. IRQF_TRIGGER_HIGH, "clk-utmi", utmi);
  99. if (ret)
  100. return ERR_PTR(ret);
  101. clk = clk_register(NULL, &utmi->hw);
  102. if (IS_ERR(clk))
  103. kfree(utmi);
  104. return clk;
  105. }
  106. static void __init
  107. of_at91_clk_utmi_setup(struct device_node *np, struct at91_pmc *pmc)
  108. {
  109. unsigned int irq;
  110. struct clk *clk;
  111. const char *parent_name;
  112. const char *name = np->name;
  113. parent_name = of_clk_get_parent_name(np, 0);
  114. of_property_read_string(np, "clock-output-names", &name);
  115. irq = irq_of_parse_and_map(np, 0);
  116. if (!irq)
  117. return;
  118. clk = at91_clk_register_utmi(pmc, irq, name, parent_name);
  119. if (IS_ERR(clk))
  120. return;
  121. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  122. return;
  123. }
  124. void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  125. struct at91_pmc *pmc)
  126. {
  127. of_at91_clk_utmi_setup(np, pmc);
  128. }