clk-generated.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2015 Atmel Corporation,
  3. * Nicolas Ferre <nicolas.ferre@atmel.com>
  4. *
  5. * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/clk-provider.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/clk/at91_pmc.h>
  16. #include <linux/of.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/regmap.h>
  19. #include "pmc.h"
  20. #define PERIPHERAL_MAX 64
  21. #define PERIPHERAL_ID_MIN 2
  22. #define GENERATED_SOURCE_MAX 6
  23. #define GENERATED_MAX_DIV 255
  24. struct clk_generated {
  25. struct clk_hw hw;
  26. struct regmap *regmap;
  27. struct clk_range range;
  28. spinlock_t *lock;
  29. u32 id;
  30. u32 gckdiv;
  31. u8 parent_id;
  32. };
  33. #define to_clk_generated(hw) \
  34. container_of(hw, struct clk_generated, hw)
  35. static int clk_generated_enable(struct clk_hw *hw)
  36. {
  37. struct clk_generated *gck = to_clk_generated(hw);
  38. unsigned long flags;
  39. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  40. __func__, gck->gckdiv, gck->parent_id);
  41. spin_lock_irqsave(gck->lock, flags);
  42. regmap_write(gck->regmap, AT91_PMC_PCR,
  43. (gck->id & AT91_PMC_PCR_PID_MASK));
  44. regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  45. AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  46. AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  47. AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  48. AT91_PMC_PCR_CMD |
  49. AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  50. AT91_PMC_PCR_GCKEN);
  51. spin_unlock_irqrestore(gck->lock, flags);
  52. return 0;
  53. }
  54. static void clk_generated_disable(struct clk_hw *hw)
  55. {
  56. struct clk_generated *gck = to_clk_generated(hw);
  57. unsigned long flags;
  58. spin_lock_irqsave(gck->lock, flags);
  59. regmap_write(gck->regmap, AT91_PMC_PCR,
  60. (gck->id & AT91_PMC_PCR_PID_MASK));
  61. regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  62. AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  63. AT91_PMC_PCR_CMD);
  64. spin_unlock_irqrestore(gck->lock, flags);
  65. }
  66. static int clk_generated_is_enabled(struct clk_hw *hw)
  67. {
  68. struct clk_generated *gck = to_clk_generated(hw);
  69. unsigned long flags;
  70. unsigned int status;
  71. spin_lock_irqsave(gck->lock, flags);
  72. regmap_write(gck->regmap, AT91_PMC_PCR,
  73. (gck->id & AT91_PMC_PCR_PID_MASK));
  74. regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  75. spin_unlock_irqrestore(gck->lock, flags);
  76. return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  77. }
  78. static unsigned long
  79. clk_generated_recalc_rate(struct clk_hw *hw,
  80. unsigned long parent_rate)
  81. {
  82. struct clk_generated *gck = to_clk_generated(hw);
  83. return DIV_ROUND_CLOSEST(parent_rate, gck->gckdiv + 1);
  84. }
  85. static int clk_generated_determine_rate(struct clk_hw *hw,
  86. struct clk_rate_request *req)
  87. {
  88. struct clk_generated *gck = to_clk_generated(hw);
  89. struct clk_hw *parent = NULL;
  90. long best_rate = -EINVAL;
  91. unsigned long tmp_rate, min_rate;
  92. int best_diff = -1;
  93. int tmp_diff;
  94. int i;
  95. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  96. u32 div;
  97. unsigned long parent_rate;
  98. parent = clk_hw_get_parent_by_index(hw, i);
  99. if (!parent)
  100. continue;
  101. parent_rate = clk_hw_get_rate(parent);
  102. min_rate = DIV_ROUND_CLOSEST(parent_rate, GENERATED_MAX_DIV + 1);
  103. if (!parent_rate ||
  104. (gck->range.max && min_rate > gck->range.max))
  105. continue;
  106. for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
  107. tmp_rate = DIV_ROUND_CLOSEST(parent_rate, div);
  108. tmp_diff = abs(req->rate - tmp_rate);
  109. if (best_diff < 0 || best_diff > tmp_diff) {
  110. best_rate = tmp_rate;
  111. best_diff = tmp_diff;
  112. req->best_parent_rate = parent_rate;
  113. req->best_parent_hw = parent;
  114. }
  115. if (!best_diff || tmp_rate < req->rate)
  116. break;
  117. }
  118. if (!best_diff)
  119. break;
  120. }
  121. pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  122. __func__, best_rate,
  123. __clk_get_name((req->best_parent_hw)->clk),
  124. req->best_parent_rate);
  125. if (best_rate < 0)
  126. return best_rate;
  127. req->rate = best_rate;
  128. return 0;
  129. }
  130. /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
  131. static int clk_generated_set_parent(struct clk_hw *hw, u8 index)
  132. {
  133. struct clk_generated *gck = to_clk_generated(hw);
  134. if (index >= clk_hw_get_num_parents(hw))
  135. return -EINVAL;
  136. gck->parent_id = index;
  137. return 0;
  138. }
  139. static u8 clk_generated_get_parent(struct clk_hw *hw)
  140. {
  141. struct clk_generated *gck = to_clk_generated(hw);
  142. return gck->parent_id;
  143. }
  144. /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
  145. static int clk_generated_set_rate(struct clk_hw *hw,
  146. unsigned long rate,
  147. unsigned long parent_rate)
  148. {
  149. struct clk_generated *gck = to_clk_generated(hw);
  150. u32 div;
  151. if (!rate)
  152. return -EINVAL;
  153. if (gck->range.max && rate > gck->range.max)
  154. return -EINVAL;
  155. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  156. if (div > GENERATED_MAX_DIV + 1 || !div)
  157. return -EINVAL;
  158. gck->gckdiv = div - 1;
  159. return 0;
  160. }
  161. static const struct clk_ops generated_ops = {
  162. .enable = clk_generated_enable,
  163. .disable = clk_generated_disable,
  164. .is_enabled = clk_generated_is_enabled,
  165. .recalc_rate = clk_generated_recalc_rate,
  166. .determine_rate = clk_generated_determine_rate,
  167. .get_parent = clk_generated_get_parent,
  168. .set_parent = clk_generated_set_parent,
  169. .set_rate = clk_generated_set_rate,
  170. };
  171. /**
  172. * clk_generated_startup - Initialize a given clock to its default parent and
  173. * divisor parameter.
  174. *
  175. * @gck: Generated clock to set the startup parameters for.
  176. *
  177. * Take parameters from the hardware and update local clock configuration
  178. * accordingly.
  179. */
  180. static void clk_generated_startup(struct clk_generated *gck)
  181. {
  182. u32 tmp;
  183. unsigned long flags;
  184. spin_lock_irqsave(gck->lock, flags);
  185. regmap_write(gck->regmap, AT91_PMC_PCR,
  186. (gck->id & AT91_PMC_PCR_PID_MASK));
  187. regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  188. spin_unlock_irqrestore(gck->lock, flags);
  189. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  190. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  191. gck->gckdiv = (tmp & AT91_PMC_PCR_GCKDIV_MASK)
  192. >> AT91_PMC_PCR_GCKDIV_OFFSET;
  193. }
  194. static struct clk_hw * __init
  195. at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock,
  196. const char *name, const char **parent_names,
  197. u8 num_parents, u8 id,
  198. const struct clk_range *range)
  199. {
  200. struct clk_generated *gck;
  201. struct clk_init_data init;
  202. struct clk_hw *hw;
  203. int ret;
  204. gck = kzalloc(sizeof(*gck), GFP_KERNEL);
  205. if (!gck)
  206. return ERR_PTR(-ENOMEM);
  207. init.name = name;
  208. init.ops = &generated_ops;
  209. init.parent_names = parent_names;
  210. init.num_parents = num_parents;
  211. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  212. gck->id = id;
  213. gck->hw.init = &init;
  214. gck->regmap = regmap;
  215. gck->lock = lock;
  216. gck->range = *range;
  217. clk_generated_startup(gck);
  218. hw = &gck->hw;
  219. ret = clk_hw_register(NULL, &gck->hw);
  220. if (ret) {
  221. kfree(gck);
  222. hw = ERR_PTR(ret);
  223. }
  224. return hw;
  225. }
  226. static void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  227. {
  228. int num;
  229. u32 id;
  230. const char *name;
  231. struct clk_hw *hw;
  232. unsigned int num_parents;
  233. const char *parent_names[GENERATED_SOURCE_MAX];
  234. struct device_node *gcknp;
  235. struct clk_range range = CLK_RANGE(0, 0);
  236. struct regmap *regmap;
  237. num_parents = of_clk_get_parent_count(np);
  238. if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX)
  239. return;
  240. of_clk_parent_fill(np, parent_names, num_parents);
  241. num = of_get_child_count(np);
  242. if (!num || num > PERIPHERAL_MAX)
  243. return;
  244. regmap = syscon_node_to_regmap(of_get_parent(np));
  245. if (IS_ERR(regmap))
  246. return;
  247. for_each_child_of_node(np, gcknp) {
  248. if (of_property_read_u32(gcknp, "reg", &id))
  249. continue;
  250. if (id < PERIPHERAL_ID_MIN || id >= PERIPHERAL_MAX)
  251. continue;
  252. if (of_property_read_string(np, "clock-output-names", &name))
  253. name = gcknp->name;
  254. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  255. &range);
  256. hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  257. parent_names, num_parents,
  258. id, &range);
  259. if (IS_ERR(hw))
  260. continue;
  261. of_clk_add_hw_provider(gcknp, of_clk_hw_simple_get, hw);
  262. }
  263. }
  264. CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  265. of_sama5d2_clk_generated_setup);