clk-h8s2678.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * H8S2678 clock driver
  3. *
  4. * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/err.h>
  8. #include <linux/device.h>
  9. #include <linux/of_address.h>
  10. #include <linux/slab.h>
  11. static DEFINE_SPINLOCK(clklock);
  12. #define MAX_FREQ 33333333
  13. #define MIN_FREQ 8000000
  14. struct pll_clock {
  15. struct clk_hw hw;
  16. void __iomem *sckcr;
  17. void __iomem *pllcr;
  18. };
  19. #define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw)
  20. static unsigned long pll_recalc_rate(struct clk_hw *hw,
  21. unsigned long parent_rate)
  22. {
  23. struct pll_clock *pll_clock = to_pll_clock(hw);
  24. int mul = 1 << (readb(pll_clock->pllcr) & 3);
  25. return parent_rate * mul;
  26. }
  27. static long pll_round_rate(struct clk_hw *hw, unsigned long rate,
  28. unsigned long *prate)
  29. {
  30. int i, m = -1;
  31. long offset[3];
  32. if (rate > MAX_FREQ)
  33. rate = MAX_FREQ;
  34. if (rate < MIN_FREQ)
  35. rate = MIN_FREQ;
  36. for (i = 0; i < 3; i++)
  37. offset[i] = abs(rate - (*prate * (1 << i)));
  38. for (i = 0; i < 3; i++)
  39. if (m < 0)
  40. m = i;
  41. else
  42. m = (offset[i] < offset[m])?i:m;
  43. return *prate * (1 << m);
  44. }
  45. static int pll_set_rate(struct clk_hw *hw, unsigned long rate,
  46. unsigned long parent_rate)
  47. {
  48. int pll;
  49. unsigned char val;
  50. unsigned long flags;
  51. struct pll_clock *pll_clock = to_pll_clock(hw);
  52. pll = ((rate / parent_rate) / 2) & 0x03;
  53. spin_lock_irqsave(&clklock, flags);
  54. val = readb(pll_clock->sckcr);
  55. val |= 0x08;
  56. writeb(val, pll_clock->sckcr);
  57. val = readb(pll_clock->pllcr);
  58. val &= ~0x03;
  59. val |= pll;
  60. writeb(val, pll_clock->pllcr);
  61. spin_unlock_irqrestore(&clklock, flags);
  62. return 0;
  63. }
  64. static const struct clk_ops pll_ops = {
  65. .recalc_rate = pll_recalc_rate,
  66. .round_rate = pll_round_rate,
  67. .set_rate = pll_set_rate,
  68. };
  69. static void __init h8s2678_pll_clk_setup(struct device_node *node)
  70. {
  71. int num_parents;
  72. struct clk *clk;
  73. const char *clk_name = node->name;
  74. const char *parent_name;
  75. struct pll_clock *pll_clock;
  76. struct clk_init_data init;
  77. num_parents = of_clk_get_parent_count(node);
  78. if (num_parents < 1) {
  79. pr_err("%s: no parent found", clk_name);
  80. return;
  81. }
  82. pll_clock = kzalloc(sizeof(*pll_clock), GFP_KERNEL);
  83. if (!pll_clock)
  84. return;
  85. pll_clock->sckcr = of_iomap(node, 0);
  86. if (pll_clock->sckcr == NULL) {
  87. pr_err("%s: failed to map divide register", clk_name);
  88. goto free_clock;
  89. }
  90. pll_clock->pllcr = of_iomap(node, 1);
  91. if (pll_clock->pllcr == NULL) {
  92. pr_err("%s: failed to map multiply register", clk_name);
  93. goto unmap_sckcr;
  94. }
  95. parent_name = of_clk_get_parent_name(node, 0);
  96. init.name = clk_name;
  97. init.ops = &pll_ops;
  98. init.flags = CLK_IS_BASIC;
  99. init.parent_names = &parent_name;
  100. init.num_parents = 1;
  101. pll_clock->hw.init = &init;
  102. clk = clk_register(NULL, &pll_clock->hw);
  103. if (IS_ERR(clk)) {
  104. pr_err("%s: failed to register %s div clock (%ld)\n",
  105. __func__, clk_name, PTR_ERR(clk));
  106. goto unmap_pllcr;
  107. }
  108. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  109. return;
  110. unmap_pllcr:
  111. iounmap(pll_clock->pllcr);
  112. unmap_sckcr:
  113. iounmap(pll_clock->sckcr);
  114. free_clock:
  115. kfree(pll_clock);
  116. }
  117. CLK_OF_DECLARE(h8s2678_div_clk, "renesas,h8s2678-pll-clock",
  118. h8s2678_pll_clk_setup);