clk-sun4i-tcon-ch1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015 Maxime Ripard
  4. *
  5. * Maxime Ripard <[email protected]>
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #define TCON_CH1_SCLK2_PARENTS 4
  14. #define TCON_CH1_SCLK2_GATE_BIT BIT(31)
  15. #define TCON_CH1_SCLK2_MUX_MASK 3
  16. #define TCON_CH1_SCLK2_MUX_SHIFT 24
  17. #define TCON_CH1_SCLK2_DIV_MASK 0xf
  18. #define TCON_CH1_SCLK2_DIV_SHIFT 0
  19. #define TCON_CH1_SCLK1_GATE_BIT BIT(15)
  20. #define TCON_CH1_SCLK1_HALF_BIT BIT(11)
  21. struct tcon_ch1_clk {
  22. struct clk_hw hw;
  23. spinlock_t lock;
  24. void __iomem *reg;
  25. };
  26. #define hw_to_tclk(hw) container_of(hw, struct tcon_ch1_clk, hw)
  27. static void tcon_ch1_disable(struct clk_hw *hw)
  28. {
  29. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  30. unsigned long flags;
  31. u32 reg;
  32. spin_lock_irqsave(&tclk->lock, flags);
  33. reg = readl(tclk->reg);
  34. reg &= ~(TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT);
  35. writel(reg, tclk->reg);
  36. spin_unlock_irqrestore(&tclk->lock, flags);
  37. }
  38. static int tcon_ch1_enable(struct clk_hw *hw)
  39. {
  40. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  41. unsigned long flags;
  42. u32 reg;
  43. spin_lock_irqsave(&tclk->lock, flags);
  44. reg = readl(tclk->reg);
  45. reg |= TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT;
  46. writel(reg, tclk->reg);
  47. spin_unlock_irqrestore(&tclk->lock, flags);
  48. return 0;
  49. }
  50. static int tcon_ch1_is_enabled(struct clk_hw *hw)
  51. {
  52. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  53. u32 reg;
  54. reg = readl(tclk->reg);
  55. return reg & (TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT);
  56. }
  57. static u8 tcon_ch1_get_parent(struct clk_hw *hw)
  58. {
  59. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  60. u32 reg;
  61. reg = readl(tclk->reg) >> TCON_CH1_SCLK2_MUX_SHIFT;
  62. reg &= reg >> TCON_CH1_SCLK2_MUX_MASK;
  63. return reg;
  64. }
  65. static int tcon_ch1_set_parent(struct clk_hw *hw, u8 index)
  66. {
  67. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  68. unsigned long flags;
  69. u32 reg;
  70. spin_lock_irqsave(&tclk->lock, flags);
  71. reg = readl(tclk->reg);
  72. reg &= ~(TCON_CH1_SCLK2_MUX_MASK << TCON_CH1_SCLK2_MUX_SHIFT);
  73. reg |= index << TCON_CH1_SCLK2_MUX_SHIFT;
  74. writel(reg, tclk->reg);
  75. spin_unlock_irqrestore(&tclk->lock, flags);
  76. return 0;
  77. };
  78. static unsigned long tcon_ch1_calc_divider(unsigned long rate,
  79. unsigned long parent_rate,
  80. u8 *div,
  81. bool *half)
  82. {
  83. unsigned long best_rate = 0;
  84. u8 best_m = 0, m;
  85. bool is_double;
  86. for (m = 1; m < 16; m++) {
  87. u8 d;
  88. for (d = 1; d < 3; d++) {
  89. unsigned long tmp_rate;
  90. tmp_rate = parent_rate / m / d;
  91. if (tmp_rate > rate)
  92. continue;
  93. if (!best_rate ||
  94. (rate - tmp_rate) < (rate - best_rate)) {
  95. best_rate = tmp_rate;
  96. best_m = m;
  97. is_double = d;
  98. }
  99. }
  100. }
  101. if (div && half) {
  102. *div = best_m;
  103. *half = is_double;
  104. }
  105. return best_rate;
  106. }
  107. static int tcon_ch1_determine_rate(struct clk_hw *hw,
  108. struct clk_rate_request *req)
  109. {
  110. long best_rate = -EINVAL;
  111. int i;
  112. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  113. unsigned long parent_rate;
  114. unsigned long tmp_rate;
  115. struct clk_hw *parent;
  116. parent = clk_hw_get_parent_by_index(hw, i);
  117. if (!parent)
  118. continue;
  119. parent_rate = clk_hw_get_rate(parent);
  120. tmp_rate = tcon_ch1_calc_divider(req->rate, parent_rate,
  121. NULL, NULL);
  122. if (best_rate < 0 ||
  123. (req->rate - tmp_rate) < (req->rate - best_rate)) {
  124. best_rate = tmp_rate;
  125. req->best_parent_rate = parent_rate;
  126. req->best_parent_hw = parent;
  127. }
  128. }
  129. if (best_rate < 0)
  130. return best_rate;
  131. req->rate = best_rate;
  132. return 0;
  133. }
  134. static unsigned long tcon_ch1_recalc_rate(struct clk_hw *hw,
  135. unsigned long parent_rate)
  136. {
  137. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  138. u32 reg;
  139. reg = readl(tclk->reg);
  140. parent_rate /= (reg & TCON_CH1_SCLK2_DIV_MASK) + 1;
  141. if (reg & TCON_CH1_SCLK1_HALF_BIT)
  142. parent_rate /= 2;
  143. return parent_rate;
  144. }
  145. static int tcon_ch1_set_rate(struct clk_hw *hw, unsigned long rate,
  146. unsigned long parent_rate)
  147. {
  148. struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
  149. unsigned long flags;
  150. bool half;
  151. u8 div_m;
  152. u32 reg;
  153. tcon_ch1_calc_divider(rate, parent_rate, &div_m, &half);
  154. spin_lock_irqsave(&tclk->lock, flags);
  155. reg = readl(tclk->reg);
  156. reg &= ~(TCON_CH1_SCLK2_DIV_MASK | TCON_CH1_SCLK1_HALF_BIT);
  157. reg |= (div_m - 1) & TCON_CH1_SCLK2_DIV_MASK;
  158. if (half)
  159. reg |= TCON_CH1_SCLK1_HALF_BIT;
  160. writel(reg, tclk->reg);
  161. spin_unlock_irqrestore(&tclk->lock, flags);
  162. return 0;
  163. }
  164. static const struct clk_ops tcon_ch1_ops = {
  165. .disable = tcon_ch1_disable,
  166. .enable = tcon_ch1_enable,
  167. .is_enabled = tcon_ch1_is_enabled,
  168. .get_parent = tcon_ch1_get_parent,
  169. .set_parent = tcon_ch1_set_parent,
  170. .determine_rate = tcon_ch1_determine_rate,
  171. .recalc_rate = tcon_ch1_recalc_rate,
  172. .set_rate = tcon_ch1_set_rate,
  173. };
  174. static void __init tcon_ch1_setup(struct device_node *node)
  175. {
  176. const char *parents[TCON_CH1_SCLK2_PARENTS];
  177. const char *clk_name = node->name;
  178. struct clk_init_data init;
  179. struct tcon_ch1_clk *tclk;
  180. struct resource res;
  181. struct clk *clk;
  182. void __iomem *reg;
  183. int ret;
  184. of_property_read_string(node, "clock-output-names", &clk_name);
  185. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  186. if (IS_ERR(reg)) {
  187. pr_err("%s: Could not map the clock registers\n", clk_name);
  188. return;
  189. }
  190. ret = of_clk_parent_fill(node, parents, TCON_CH1_SCLK2_PARENTS);
  191. if (ret != TCON_CH1_SCLK2_PARENTS) {
  192. pr_err("%s Could not retrieve the parents\n", clk_name);
  193. goto err_unmap;
  194. }
  195. tclk = kzalloc(sizeof(*tclk), GFP_KERNEL);
  196. if (!tclk)
  197. goto err_unmap;
  198. init.name = clk_name;
  199. init.ops = &tcon_ch1_ops;
  200. init.parent_names = parents;
  201. init.num_parents = TCON_CH1_SCLK2_PARENTS;
  202. init.flags = CLK_SET_RATE_PARENT;
  203. tclk->reg = reg;
  204. tclk->hw.init = &init;
  205. spin_lock_init(&tclk->lock);
  206. clk = clk_register(NULL, &tclk->hw);
  207. if (IS_ERR(clk)) {
  208. pr_err("%s: Couldn't register the clock\n", clk_name);
  209. goto err_free_data;
  210. }
  211. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  212. if (ret) {
  213. pr_err("%s: Couldn't register our clock provider\n", clk_name);
  214. goto err_unregister_clk;
  215. }
  216. return;
  217. err_unregister_clk:
  218. clk_unregister(clk);
  219. err_free_data:
  220. kfree(tclk);
  221. err_unmap:
  222. iounmap(reg);
  223. of_address_to_resource(node, 0, &res);
  224. release_mem_region(res.start, resource_size(&res));
  225. }
  226. CLK_OF_DECLARE(tcon_ch1, "allwinner,sun4i-a10-tcon-ch1-clk",
  227. tcon_ch1_setup);