clk-sun6i-ar100.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 long ar100_determine_rate(struct clk_hw *hw, unsigned long rate,
  41. unsigned long *best_parent_rate,
  42. struct clk **best_parent_clk)
  43. {
  44. int nparents = __clk_get_num_parents(hw->clk);
  45. long best_rate = -EINVAL;
  46. int i;
  47. *best_parent_clk = NULL;
  48. for (i = 0; i < nparents; i++) {
  49. unsigned long parent_rate;
  50. unsigned long tmp_rate;
  51. struct clk *parent;
  52. unsigned long div;
  53. int shift;
  54. parent = clk_get_parent_by_index(hw->clk, i);
  55. parent_rate = __clk_get_rate(parent);
  56. div = DIV_ROUND_UP(parent_rate, rate);
  57. /*
  58. * The AR100 clk contains 2 divisors:
  59. * - one power of 2 divisor
  60. * - one regular divisor
  61. *
  62. * First check if we can safely shift (or divide by a power
  63. * of 2) without losing precision on the requested rate.
  64. */
  65. shift = ffs(div) - 1;
  66. if (shift > SUN6I_AR100_SHIFT_MAX)
  67. shift = SUN6I_AR100_SHIFT_MAX;
  68. div >>= shift;
  69. /*
  70. * Then if the divisor is still bigger than what the HW
  71. * actually supports, use a bigger shift (or power of 2
  72. * divider) value and accept to lose some precision.
  73. */
  74. while (div > SUN6I_AR100_DIV_MAX) {
  75. shift++;
  76. div >>= 1;
  77. if (shift > SUN6I_AR100_SHIFT_MAX)
  78. break;
  79. }
  80. /*
  81. * If the shift value (or power of 2 divider) is bigger
  82. * than what the HW actually support, skip this parent.
  83. */
  84. if (shift > SUN6I_AR100_SHIFT_MAX)
  85. continue;
  86. tmp_rate = (parent_rate >> shift) / div;
  87. if (!*best_parent_clk || tmp_rate > best_rate) {
  88. *best_parent_clk = parent;
  89. *best_parent_rate = parent_rate;
  90. best_rate = tmp_rate;
  91. }
  92. }
  93. return best_rate;
  94. }
  95. static int ar100_set_parent(struct clk_hw *hw, u8 index)
  96. {
  97. struct ar100_clk *clk = to_ar100_clk(hw);
  98. u32 val = readl(clk->reg);
  99. if (index >= SUN6I_AR100_MAX_PARENTS)
  100. return -EINVAL;
  101. val &= ~(SUN6I_AR100_MUX_MASK << SUN6I_AR100_MUX_SHIFT);
  102. val |= (index << SUN6I_AR100_MUX_SHIFT);
  103. writel(val, clk->reg);
  104. return 0;
  105. }
  106. static u8 ar100_get_parent(struct clk_hw *hw)
  107. {
  108. struct ar100_clk *clk = to_ar100_clk(hw);
  109. return (readl(clk->reg) >> SUN6I_AR100_MUX_SHIFT) &
  110. SUN6I_AR100_MUX_MASK;
  111. }
  112. static int ar100_set_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. unsigned long div = parent_rate / rate;
  116. struct ar100_clk *clk = to_ar100_clk(hw);
  117. u32 val = readl(clk->reg);
  118. int shift;
  119. if (parent_rate % rate)
  120. return -EINVAL;
  121. shift = ffs(div) - 1;
  122. if (shift > SUN6I_AR100_SHIFT_MAX)
  123. shift = SUN6I_AR100_SHIFT_MAX;
  124. div >>= shift;
  125. if (div > SUN6I_AR100_DIV_MAX)
  126. return -EINVAL;
  127. val &= ~((SUN6I_AR100_SHIFT_MASK << SUN6I_AR100_SHIFT_SHIFT) |
  128. (SUN6I_AR100_DIV_MASK << SUN6I_AR100_DIV_SHIFT));
  129. val |= (shift << SUN6I_AR100_SHIFT_SHIFT) |
  130. (div << SUN6I_AR100_DIV_SHIFT);
  131. writel(val, clk->reg);
  132. return 0;
  133. }
  134. static struct clk_ops ar100_ops = {
  135. .recalc_rate = ar100_recalc_rate,
  136. .determine_rate = ar100_determine_rate,
  137. .set_parent = ar100_set_parent,
  138. .get_parent = ar100_get_parent,
  139. .set_rate = ar100_set_rate,
  140. };
  141. static int sun6i_a31_ar100_clk_probe(struct platform_device *pdev)
  142. {
  143. const char *parents[SUN6I_AR100_MAX_PARENTS];
  144. struct device_node *np = pdev->dev.of_node;
  145. const char *clk_name = np->name;
  146. struct clk_init_data init;
  147. struct ar100_clk *ar100;
  148. struct resource *r;
  149. struct clk *clk;
  150. int nparents;
  151. int i;
  152. ar100 = devm_kzalloc(&pdev->dev, sizeof(*ar100), GFP_KERNEL);
  153. if (!ar100)
  154. return -ENOMEM;
  155. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. ar100->reg = devm_ioremap_resource(&pdev->dev, r);
  157. if (IS_ERR(ar100->reg))
  158. return PTR_ERR(ar100->reg);
  159. nparents = of_clk_get_parent_count(np);
  160. if (nparents > SUN6I_AR100_MAX_PARENTS)
  161. nparents = SUN6I_AR100_MAX_PARENTS;
  162. for (i = 0; i < nparents; i++)
  163. parents[i] = of_clk_get_parent_name(np, i);
  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. static struct platform_driver sun6i_a31_ar100_clk_driver = {
  181. .driver = {
  182. .name = "sun6i-a31-ar100-clk",
  183. .of_match_table = sun6i_a31_ar100_clk_dt_ids,
  184. },
  185. .probe = sun6i_a31_ar100_clk_probe,
  186. };
  187. module_platform_driver(sun6i_a31_ar100_clk_driver);
  188. MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>");
  189. MODULE_DESCRIPTION("Allwinner A31 AR100 clock Driver");
  190. MODULE_LICENSE("GPL v2");