clk-generated.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #define GCK_ID_SSC0 43
  25. #define GCK_ID_SSC1 44
  26. #define GCK_ID_I2S0 54
  27. #define GCK_ID_I2S1 55
  28. #define GCK_ID_CLASSD 59
  29. #define GCK_INDEX_DT_AUDIO_PLL 5
  30. struct clk_generated {
  31. struct clk_hw hw;
  32. struct regmap *regmap;
  33. struct clk_range range;
  34. spinlock_t *lock;
  35. u32 id;
  36. u32 gckdiv;
  37. u8 parent_id;
  38. bool audio_pll_allowed;
  39. };
  40. #define to_clk_generated(hw) \
  41. container_of(hw, struct clk_generated, hw)
  42. static int clk_generated_enable(struct clk_hw *hw)
  43. {
  44. struct clk_generated *gck = to_clk_generated(hw);
  45. unsigned long flags;
  46. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  47. __func__, gck->gckdiv, gck->parent_id);
  48. spin_lock_irqsave(gck->lock, flags);
  49. regmap_write(gck->regmap, AT91_PMC_PCR,
  50. (gck->id & AT91_PMC_PCR_PID_MASK));
  51. regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  52. AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  53. AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  54. AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  55. AT91_PMC_PCR_CMD |
  56. AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  57. AT91_PMC_PCR_GCKEN);
  58. spin_unlock_irqrestore(gck->lock, flags);
  59. return 0;
  60. }
  61. static void clk_generated_disable(struct clk_hw *hw)
  62. {
  63. struct clk_generated *gck = to_clk_generated(hw);
  64. unsigned long flags;
  65. spin_lock_irqsave(gck->lock, flags);
  66. regmap_write(gck->regmap, AT91_PMC_PCR,
  67. (gck->id & AT91_PMC_PCR_PID_MASK));
  68. regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  69. AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  70. AT91_PMC_PCR_CMD);
  71. spin_unlock_irqrestore(gck->lock, flags);
  72. }
  73. static int clk_generated_is_enabled(struct clk_hw *hw)
  74. {
  75. struct clk_generated *gck = to_clk_generated(hw);
  76. unsigned long flags;
  77. unsigned int status;
  78. spin_lock_irqsave(gck->lock, flags);
  79. regmap_write(gck->regmap, AT91_PMC_PCR,
  80. (gck->id & AT91_PMC_PCR_PID_MASK));
  81. regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  82. spin_unlock_irqrestore(gck->lock, flags);
  83. return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  84. }
  85. static unsigned long
  86. clk_generated_recalc_rate(struct clk_hw *hw,
  87. unsigned long parent_rate)
  88. {
  89. struct clk_generated *gck = to_clk_generated(hw);
  90. return DIV_ROUND_CLOSEST(parent_rate, gck->gckdiv + 1);
  91. }
  92. static void clk_generated_best_diff(struct clk_rate_request *req,
  93. struct clk_hw *parent,
  94. unsigned long parent_rate, u32 div,
  95. int *best_diff, long *best_rate)
  96. {
  97. unsigned long tmp_rate;
  98. int tmp_diff;
  99. if (!div)
  100. tmp_rate = parent_rate;
  101. else
  102. tmp_rate = parent_rate / div;
  103. tmp_diff = abs(req->rate - tmp_rate);
  104. if (*best_diff < 0 || *best_diff > tmp_diff) {
  105. *best_rate = tmp_rate;
  106. *best_diff = tmp_diff;
  107. req->best_parent_rate = parent_rate;
  108. req->best_parent_hw = parent;
  109. }
  110. }
  111. static int clk_generated_determine_rate(struct clk_hw *hw,
  112. struct clk_rate_request *req)
  113. {
  114. struct clk_generated *gck = to_clk_generated(hw);
  115. struct clk_hw *parent = NULL;
  116. struct clk_rate_request req_parent = *req;
  117. long best_rate = -EINVAL;
  118. unsigned long min_rate, parent_rate;
  119. int best_diff = -1;
  120. int i;
  121. u32 div;
  122. for (i = 0; i < clk_hw_get_num_parents(hw) - 1; i++) {
  123. parent = clk_hw_get_parent_by_index(hw, i);
  124. if (!parent)
  125. continue;
  126. parent_rate = clk_hw_get_rate(parent);
  127. min_rate = DIV_ROUND_CLOSEST(parent_rate, GENERATED_MAX_DIV + 1);
  128. if (!parent_rate ||
  129. (gck->range.max && min_rate > gck->range.max))
  130. continue;
  131. div = DIV_ROUND_CLOSEST(parent_rate, req->rate);
  132. clk_generated_best_diff(req, parent, parent_rate, div,
  133. &best_diff, &best_rate);
  134. if (!best_diff)
  135. break;
  136. }
  137. /*
  138. * The audio_pll rate can be modified, unlike the five others clocks
  139. * that should never be altered.
  140. * The audio_pll can technically be used by multiple consumers. However,
  141. * with the rate locking, the first consumer to enable to clock will be
  142. * the one definitely setting the rate of the clock.
  143. * Since audio IPs are most likely to request the same rate, we enforce
  144. * that the only clks able to modify gck rate are those of audio IPs.
  145. */
  146. if (!gck->audio_pll_allowed)
  147. goto end;
  148. parent = clk_hw_get_parent_by_index(hw, GCK_INDEX_DT_AUDIO_PLL);
  149. if (!parent)
  150. goto end;
  151. for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
  152. req_parent.rate = req->rate * div;
  153. __clk_determine_rate(parent, &req_parent);
  154. clk_generated_best_diff(req, parent, req_parent.rate, div,
  155. &best_diff, &best_rate);
  156. if (!best_diff)
  157. break;
  158. }
  159. end:
  160. pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  161. __func__, best_rate,
  162. __clk_get_name((req->best_parent_hw)->clk),
  163. req->best_parent_rate);
  164. if (best_rate < 0)
  165. return best_rate;
  166. req->rate = best_rate;
  167. return 0;
  168. }
  169. /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
  170. static int clk_generated_set_parent(struct clk_hw *hw, u8 index)
  171. {
  172. struct clk_generated *gck = to_clk_generated(hw);
  173. if (index >= clk_hw_get_num_parents(hw))
  174. return -EINVAL;
  175. gck->parent_id = index;
  176. return 0;
  177. }
  178. static u8 clk_generated_get_parent(struct clk_hw *hw)
  179. {
  180. struct clk_generated *gck = to_clk_generated(hw);
  181. return gck->parent_id;
  182. }
  183. /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
  184. static int clk_generated_set_rate(struct clk_hw *hw,
  185. unsigned long rate,
  186. unsigned long parent_rate)
  187. {
  188. struct clk_generated *gck = to_clk_generated(hw);
  189. u32 div;
  190. if (!rate)
  191. return -EINVAL;
  192. if (gck->range.max && rate > gck->range.max)
  193. return -EINVAL;
  194. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  195. if (div > GENERATED_MAX_DIV + 1 || !div)
  196. return -EINVAL;
  197. gck->gckdiv = div - 1;
  198. return 0;
  199. }
  200. static const struct clk_ops generated_ops = {
  201. .enable = clk_generated_enable,
  202. .disable = clk_generated_disable,
  203. .is_enabled = clk_generated_is_enabled,
  204. .recalc_rate = clk_generated_recalc_rate,
  205. .determine_rate = clk_generated_determine_rate,
  206. .get_parent = clk_generated_get_parent,
  207. .set_parent = clk_generated_set_parent,
  208. .set_rate = clk_generated_set_rate,
  209. };
  210. /**
  211. * clk_generated_startup - Initialize a given clock to its default parent and
  212. * divisor parameter.
  213. *
  214. * @gck: Generated clock to set the startup parameters for.
  215. *
  216. * Take parameters from the hardware and update local clock configuration
  217. * accordingly.
  218. */
  219. static void clk_generated_startup(struct clk_generated *gck)
  220. {
  221. u32 tmp;
  222. unsigned long flags;
  223. spin_lock_irqsave(gck->lock, flags);
  224. regmap_write(gck->regmap, AT91_PMC_PCR,
  225. (gck->id & AT91_PMC_PCR_PID_MASK));
  226. regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  227. spin_unlock_irqrestore(gck->lock, flags);
  228. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  229. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  230. gck->gckdiv = (tmp & AT91_PMC_PCR_GCKDIV_MASK)
  231. >> AT91_PMC_PCR_GCKDIV_OFFSET;
  232. }
  233. static struct clk_hw * __init
  234. at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock,
  235. const char *name, const char **parent_names,
  236. u8 num_parents, u8 id,
  237. const struct clk_range *range)
  238. {
  239. struct clk_generated *gck;
  240. struct clk_init_data init;
  241. struct clk_hw *hw;
  242. int ret;
  243. gck = kzalloc(sizeof(*gck), GFP_KERNEL);
  244. if (!gck)
  245. return ERR_PTR(-ENOMEM);
  246. init.name = name;
  247. init.ops = &generated_ops;
  248. init.parent_names = parent_names;
  249. init.num_parents = num_parents;
  250. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  251. CLK_SET_RATE_PARENT;
  252. gck->id = id;
  253. gck->hw.init = &init;
  254. gck->regmap = regmap;
  255. gck->lock = lock;
  256. gck->range = *range;
  257. clk_generated_startup(gck);
  258. hw = &gck->hw;
  259. ret = clk_hw_register(NULL, &gck->hw);
  260. if (ret) {
  261. kfree(gck);
  262. hw = ERR_PTR(ret);
  263. } else {
  264. pmc_register_id(id);
  265. }
  266. return hw;
  267. }
  268. static void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  269. {
  270. int num;
  271. u32 id;
  272. const char *name;
  273. struct clk_hw *hw;
  274. unsigned int num_parents;
  275. const char *parent_names[GENERATED_SOURCE_MAX];
  276. struct device_node *gcknp;
  277. struct clk_range range = CLK_RANGE(0, 0);
  278. struct regmap *regmap;
  279. struct clk_generated *gck;
  280. num_parents = of_clk_get_parent_count(np);
  281. if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX)
  282. return;
  283. of_clk_parent_fill(np, parent_names, num_parents);
  284. num = of_get_child_count(np);
  285. if (!num || num > PERIPHERAL_MAX)
  286. return;
  287. regmap = syscon_node_to_regmap(of_get_parent(np));
  288. if (IS_ERR(regmap))
  289. return;
  290. for_each_child_of_node(np, gcknp) {
  291. if (of_property_read_u32(gcknp, "reg", &id))
  292. continue;
  293. if (id < PERIPHERAL_ID_MIN || id >= PERIPHERAL_MAX)
  294. continue;
  295. if (of_property_read_string(np, "clock-output-names", &name))
  296. name = gcknp->name;
  297. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  298. &range);
  299. hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  300. parent_names, num_parents,
  301. id, &range);
  302. gck = to_clk_generated(hw);
  303. if (of_device_is_compatible(np,
  304. "atmel,sama5d2-clk-generated")) {
  305. if (gck->id == GCK_ID_SSC0 || gck->id == GCK_ID_SSC1 ||
  306. gck->id == GCK_ID_I2S0 || gck->id == GCK_ID_I2S1 ||
  307. gck->id == GCK_ID_CLASSD)
  308. gck->audio_pll_allowed = true;
  309. else
  310. gck->audio_pll_allowed = false;
  311. } else {
  312. gck->audio_pll_allowed = false;
  313. }
  314. if (IS_ERR(hw))
  315. continue;
  316. of_clk_add_hw_provider(gcknp, of_clk_hw_simple_get, hw);
  317. }
  318. }
  319. CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  320. of_sama5d2_clk_generated_setup);