sun4i_dotclock.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2016 Free Electrons
  3. * Copyright (C) 2016 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/regmap.h>
  14. #include "sun4i_tcon.h"
  15. struct sun4i_dclk {
  16. struct clk_hw hw;
  17. struct regmap *regmap;
  18. };
  19. static inline struct sun4i_dclk *hw_to_dclk(struct clk_hw *hw)
  20. {
  21. return container_of(hw, struct sun4i_dclk, hw);
  22. }
  23. static void sun4i_dclk_disable(struct clk_hw *hw)
  24. {
  25. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  26. regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  27. BIT(SUN4I_TCON0_DCLK_GATE_BIT), 0);
  28. }
  29. static int sun4i_dclk_enable(struct clk_hw *hw)
  30. {
  31. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  32. return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  33. BIT(SUN4I_TCON0_DCLK_GATE_BIT),
  34. BIT(SUN4I_TCON0_DCLK_GATE_BIT));
  35. }
  36. static int sun4i_dclk_is_enabled(struct clk_hw *hw)
  37. {
  38. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  39. u32 val;
  40. regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
  41. return val & BIT(SUN4I_TCON0_DCLK_GATE_BIT);
  42. }
  43. static unsigned long sun4i_dclk_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  47. u32 val;
  48. regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
  49. val >>= SUN4I_TCON0_DCLK_DIV_SHIFT;
  50. val &= SUN4I_TCON0_DCLK_DIV_WIDTH;
  51. if (!val)
  52. val = 1;
  53. return parent_rate / val;
  54. }
  55. static long sun4i_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
  56. unsigned long *parent_rate)
  57. {
  58. return *parent_rate / DIV_ROUND_CLOSEST(*parent_rate, rate);
  59. }
  60. static int sun4i_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  61. unsigned long parent_rate)
  62. {
  63. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  64. int div = DIV_ROUND_CLOSEST(parent_rate, rate);
  65. return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  66. GENMASK(6, 0), div);
  67. }
  68. static int sun4i_dclk_get_phase(struct clk_hw *hw)
  69. {
  70. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  71. u32 val;
  72. regmap_read(dclk->regmap, SUN4I_TCON0_IO_POL_REG, &val);
  73. val >>= 28;
  74. val &= 3;
  75. return val * 120;
  76. }
  77. static int sun4i_dclk_set_phase(struct clk_hw *hw, int degrees)
  78. {
  79. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  80. regmap_update_bits(dclk->regmap, SUN4I_TCON0_IO_POL_REG,
  81. GENMASK(29, 28),
  82. degrees / 120);
  83. return 0;
  84. }
  85. static const struct clk_ops sun4i_dclk_ops = {
  86. .disable = sun4i_dclk_disable,
  87. .enable = sun4i_dclk_enable,
  88. .is_enabled = sun4i_dclk_is_enabled,
  89. .recalc_rate = sun4i_dclk_recalc_rate,
  90. .round_rate = sun4i_dclk_round_rate,
  91. .set_rate = sun4i_dclk_set_rate,
  92. .get_phase = sun4i_dclk_get_phase,
  93. .set_phase = sun4i_dclk_set_phase,
  94. };
  95. int sun4i_dclk_create(struct device *dev, struct sun4i_tcon *tcon)
  96. {
  97. const char *clk_name, *parent_name;
  98. struct clk_init_data init;
  99. struct sun4i_dclk *dclk;
  100. parent_name = __clk_get_name(tcon->sclk0);
  101. of_property_read_string_index(dev->of_node, "clock-output-names", 0,
  102. &clk_name);
  103. dclk = devm_kzalloc(dev, sizeof(*dclk), GFP_KERNEL);
  104. if (!dclk)
  105. return -ENOMEM;
  106. init.name = clk_name;
  107. init.ops = &sun4i_dclk_ops;
  108. init.parent_names = &parent_name;
  109. init.num_parents = 1;
  110. dclk->regmap = tcon->regs;
  111. dclk->hw.init = &init;
  112. tcon->dclk = clk_register(dev, &dclk->hw);
  113. if (IS_ERR(tcon->dclk))
  114. return PTR_ERR(tcon->dclk);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(sun4i_dclk_create);
  118. int sun4i_dclk_free(struct sun4i_tcon *tcon)
  119. {
  120. clk_unregister(tcon->dclk);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(sun4i_dclk_free);