clk-mmc-phase.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2014 Google, Inc
  3. * Author: Alexandru M Stan <amstan@chromium.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include "clk.h"
  21. struct rockchip_mmc_clock {
  22. struct clk_hw hw;
  23. void __iomem *reg;
  24. int id;
  25. int shift;
  26. };
  27. #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
  28. #define RK3288_MMC_CLKGEN_DIV 2
  29. static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
  30. unsigned long parent_rate)
  31. {
  32. return parent_rate / RK3288_MMC_CLKGEN_DIV;
  33. }
  34. #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
  35. #define ROCKCHIP_MMC_DEGREE_MASK 0x3
  36. #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
  37. #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
  38. #define PSECS_PER_SEC 1000000000000LL
  39. /*
  40. * Each fine delay is between 40ps-80ps. Assume each fine delay is 60ps to
  41. * simplify calculations. So 45degs could be anywhere between 33deg and 66deg.
  42. */
  43. #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
  44. static int rockchip_mmc_get_phase(struct clk_hw *hw)
  45. {
  46. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  47. unsigned long rate = clk_get_rate(hw->clk);
  48. u32 raw_value;
  49. u16 degrees;
  50. u32 delay_num = 0;
  51. raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
  52. degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
  53. if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
  54. /* degrees/delaynum * 10000 */
  55. unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
  56. 36 * (rate / 1000000);
  57. delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
  58. delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
  59. degrees += delay_num * factor / 10000;
  60. }
  61. return degrees % 360;
  62. }
  63. static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
  64. {
  65. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  66. unsigned long rate = clk_get_rate(hw->clk);
  67. u8 nineties, remainder;
  68. u8 delay_num;
  69. u32 raw_value;
  70. u64 delay;
  71. /* allow 22 to be 22.5 */
  72. degrees++;
  73. /* floor to 22.5 increment */
  74. degrees -= ((degrees) * 10 % 225) / 10;
  75. nineties = degrees / 90;
  76. /* 22.5 multiples */
  77. remainder = (degrees % 90) / 22;
  78. delay = PSECS_PER_SEC;
  79. do_div(delay, rate);
  80. /* / 360 / 22.5 */
  81. do_div(delay, 16);
  82. do_div(delay, ROCKCHIP_MMC_DELAY_ELEMENT_PSEC);
  83. delay *= remainder;
  84. delay_num = (u8) min(delay, 255ULL);
  85. raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
  86. raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
  87. raw_value |= nineties;
  88. writel(HIWORD_UPDATE(raw_value, 0x07ff, mmc_clock->shift), mmc_clock->reg);
  89. pr_debug("%s->set_phase(%d) delay_nums=%u reg[0x%p]=0x%03x actual_degrees=%d\n",
  90. clk_hw_get_name(hw), degrees, delay_num,
  91. mmc_clock->reg, raw_value>>(mmc_clock->shift),
  92. rockchip_mmc_get_phase(hw)
  93. );
  94. return 0;
  95. }
  96. static const struct clk_ops rockchip_mmc_clk_ops = {
  97. .recalc_rate = rockchip_mmc_recalc,
  98. .get_phase = rockchip_mmc_get_phase,
  99. .set_phase = rockchip_mmc_set_phase,
  100. };
  101. struct clk *rockchip_clk_register_mmc(const char *name,
  102. const char *const *parent_names, u8 num_parents,
  103. void __iomem *reg, int shift)
  104. {
  105. struct clk_init_data init;
  106. struct rockchip_mmc_clock *mmc_clock;
  107. struct clk *clk;
  108. mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
  109. if (!mmc_clock)
  110. return NULL;
  111. init.name = name;
  112. init.num_parents = num_parents;
  113. init.parent_names = parent_names;
  114. init.ops = &rockchip_mmc_clk_ops;
  115. mmc_clock->hw.init = &init;
  116. mmc_clock->reg = reg;
  117. mmc_clock->shift = shift;
  118. clk = clk_register(NULL, &mmc_clock->hw);
  119. if (IS_ERR(clk))
  120. goto err_free;
  121. return clk;
  122. err_free:
  123. kfree(mmc_clock);
  124. return NULL;
  125. }