clk-sun6i-ar100.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2014 Free Electrons
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  6. *
  7. * Allwinner A31 AR100 clock driver
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #define SUN6I_AR100_MAX_PARENTS 4
  15. #define SUN6I_AR100_SHIFT_MASK 0x3
  16. #define SUN6I_AR100_SHIFT_MAX SUN6I_AR100_SHIFT_MASK
  17. #define SUN6I_AR100_SHIFT_SHIFT 4
  18. #define SUN6I_AR100_DIV_MASK 0x1f
  19. #define SUN6I_AR100_DIV_MAX (SUN6I_AR100_DIV_MASK + 1)
  20. #define SUN6I_AR100_DIV_SHIFT 8
  21. #define SUN6I_AR100_MUX_MASK 0x3
  22. #define SUN6I_AR100_MUX_SHIFT 16
  23. struct ar100_clk {
  24. struct clk_hw hw;
  25. void __iomem *reg;
  26. };
  27. static inline struct ar100_clk *to_ar100_clk(struct clk_hw *hw)
  28. {
  29. return container_of(hw, struct ar100_clk, hw);
  30. }
  31. static unsigned long ar100_recalc_rate(struct clk_hw *hw,
  32. unsigned long parent_rate)
  33. {
  34. struct ar100_clk *clk = to_ar100_clk(hw);
  35. u32 val = readl(clk->reg);
  36. int shift = (val >> SUN6I_AR100_SHIFT_SHIFT) & SUN6I_AR100_SHIFT_MASK;
  37. int div = (val >> SUN6I_AR100_DIV_SHIFT) & SUN6I_AR100_DIV_MASK;
  38. return (parent_rate >> shift) / (div + 1);
  39. }
  40. static int ar100_determine_rate(struct clk_hw *hw,
  41. struct clk_rate_request *req)
  42. {
  43. int nparents = clk_hw_get_num_parents(hw);
  44. long best_rate = -EINVAL;
  45. int i;
  46. req->best_parent_hw = NULL;
  47. for (i = 0; i < nparents; i++) {
  48. unsigned long parent_rate;
  49. unsigned long tmp_rate;
  50. struct clk_hw *parent;
  51. unsigned long div;
  52. int shift;
  53. parent = clk_hw_get_parent_by_index(hw, i);
  54. parent_rate = clk_hw_get_rate(parent);
  55. div = DIV_ROUND_UP(parent_rate, req->rate);
  56. /*
  57. * The AR100 clk contains 2 divisors:
  58. * - one power of 2 divisor
  59. * - one regular divisor
  60. *
  61. * First check if we can safely shift (or divide by a power
  62. * of 2) without losing precision on the requested rate.
  63. */
  64. shift = ffs(div) - 1;
  65. if (shift > SUN6I_AR100_SHIFT_MAX)
  66. shift = SUN6I_AR100_SHIFT_MAX;
  67. div >>= shift;
  68. /*
  69. * Then if the divisor is still bigger than what the HW
  70. * actually supports, use a bigger shift (or power of 2
  71. * divider) value and accept to lose some precision.
  72. */
  73. while (div > SUN6I_AR100_DIV_MAX) {
  74. shift++;
  75. div >>= 1;
  76. if (shift > SUN6I_AR100_SHIFT_MAX)
  77. break;
  78. }
  79. /*
  80. * If the shift value (or power of 2 divider) is bigger
  81. * than what the HW actually support, skip this parent.
  82. */
  83. if (shift > SUN6I_AR100_SHIFT_MAX)
  84. continue;
  85. tmp_rate = (parent_rate >> shift) / div;
  86. if (!req->best_parent_hw || tmp_rate > best_rate) {
  87. req->best_parent_hw = parent;
  88. req->best_parent_rate = parent_rate;
  89. best_rate = tmp_rate;
  90. }
  91. }
  92. if (best_rate < 0)
  93. return best_rate;
  94. req->rate = best_rate;
  95. return 0;
  96. }
  97. static int ar100_set_parent(struct clk_hw *hw, u8 index)
  98. {
  99. struct ar100_clk *clk = to_ar100_clk(hw);
  100. u32 val = readl(clk->reg);
  101. if (index >= SUN6I_AR100_MAX_PARENTS)
  102. return -EINVAL;
  103. val &= ~(SUN6I_AR100_MUX_MASK << SUN6I_AR100_MUX_SHIFT);
  104. val |= (index << SUN6I_AR100_MUX_SHIFT);
  105. writel(val, clk->reg);
  106. return 0;
  107. }
  108. static u8 ar100_get_parent(struct clk_hw *hw)
  109. {
  110. struct ar100_clk *clk = to_ar100_clk(hw);
  111. return (readl(clk->reg) >> SUN6I_AR100_MUX_SHIFT) &
  112. SUN6I_AR100_MUX_MASK;
  113. }
  114. static int ar100_set_rate(struct clk_hw *hw, unsigned long rate,
  115. unsigned long parent_rate)
  116. {
  117. unsigned long div = parent_rate / rate;
  118. struct ar100_clk *clk = to_ar100_clk(hw);
  119. u32 val = readl(clk->reg);
  120. int shift;
  121. if (parent_rate % rate)
  122. return -EINVAL;
  123. shift = ffs(div) - 1;
  124. if (shift > SUN6I_AR100_SHIFT_MAX)
  125. shift = SUN6I_AR100_SHIFT_MAX;
  126. div >>= shift;
  127. if (div > SUN6I_AR100_DIV_MAX)
  128. return -EINVAL;
  129. val &= ~((SUN6I_AR100_SHIFT_MASK << SUN6I_AR100_SHIFT_SHIFT) |
  130. (SUN6I_AR100_DIV_MASK << SUN6I_AR100_DIV_SHIFT));
  131. val |= (shift << SUN6I_AR100_SHIFT_SHIFT) |
  132. (div << SUN6I_AR100_DIV_SHIFT);
  133. writel(val, clk->reg);
  134. return 0;
  135. }
  136. static struct clk_ops ar100_ops = {
  137. .recalc_rate = ar100_recalc_rate,
  138. .determine_rate = ar100_determine_rate,
  139. .set_parent = ar100_set_parent,
  140. .get_parent = ar100_get_parent,
  141. .set_rate = ar100_set_rate,
  142. };
  143. static int sun6i_a31_ar100_clk_probe(struct platform_device *pdev)
  144. {
  145. const char *parents[SUN6I_AR100_MAX_PARENTS];
  146. struct device_node *np = pdev->dev.of_node;
  147. const char *clk_name = np->name;
  148. struct clk_init_data init;
  149. struct ar100_clk *ar100;
  150. struct resource *r;
  151. struct clk *clk;
  152. int nparents;
  153. ar100 = devm_kzalloc(&pdev->dev, sizeof(*ar100), GFP_KERNEL);
  154. if (!ar100)
  155. return -ENOMEM;
  156. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. ar100->reg = devm_ioremap_resource(&pdev->dev, r);
  158. if (IS_ERR(ar100->reg))
  159. return PTR_ERR(ar100->reg);
  160. nparents = of_clk_get_parent_count(np);
  161. if (nparents > SUN6I_AR100_MAX_PARENTS)
  162. nparents = SUN6I_AR100_MAX_PARENTS;
  163. of_clk_parent_fill(np, parents, nparents);
  164. of_property_read_string(np, "clock-output-names", &clk_name);
  165. init.name = clk_name;
  166. init.ops = &ar100_ops;
  167. init.parent_names = parents;
  168. init.num_parents = nparents;
  169. init.flags = 0;
  170. ar100->hw.init = &init;
  171. clk = clk_register(&pdev->dev, &ar100->hw);
  172. if (IS_ERR(clk))
  173. return PTR_ERR(clk);
  174. return of_clk_add_provider(np, of_clk_src_simple_get, clk);
  175. }
  176. static const struct of_device_id sun6i_a31_ar100_clk_dt_ids[] = {
  177. { .compatible = "allwinner,sun6i-a31-ar100-clk" },
  178. { /* sentinel */ }
  179. };
  180. MODULE_DEVICE_TABLE(of, sun6i_a31_ar100_clk_dt_ids);
  181. static struct platform_driver sun6i_a31_ar100_clk_driver = {
  182. .driver = {
  183. .name = "sun6i-a31-ar100-clk",
  184. .of_match_table = sun6i_a31_ar100_clk_dt_ids,
  185. },
  186. .probe = sun6i_a31_ar100_clk_probe,
  187. };
  188. module_platform_driver(sun6i_a31_ar100_clk_driver);
  189. MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>");
  190. MODULE_DESCRIPTION("Allwinner A31 AR100 clock Driver");
  191. MODULE_LICENSE("GPL v2");