clk-generated.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. if (div > GENERATED_MAX_DIV + 1)
  133. div = GENERATED_MAX_DIV + 1;
  134. clk_generated_best_diff(req, parent, parent_rate, div,
  135. &best_diff, &best_rate);
  136. if (!best_diff)
  137. break;
  138. }
  139. /*
  140. * The audio_pll rate can be modified, unlike the five others clocks
  141. * that should never be altered.
  142. * The audio_pll can technically be used by multiple consumers. However,
  143. * with the rate locking, the first consumer to enable to clock will be
  144. * the one definitely setting the rate of the clock.
  145. * Since audio IPs are most likely to request the same rate, we enforce
  146. * that the only clks able to modify gck rate are those of audio IPs.
  147. */
  148. if (!gck->audio_pll_allowed)
  149. goto end;
  150. parent = clk_hw_get_parent_by_index(hw, GCK_INDEX_DT_AUDIO_PLL);
  151. if (!parent)
  152. goto end;
  153. for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
  154. req_parent.rate = req->rate * div;
  155. __clk_determine_rate(parent, &req_parent);
  156. clk_generated_best_diff(req, parent, req_parent.rate, div,
  157. &best_diff, &best_rate);
  158. if (!best_diff)
  159. break;
  160. }
  161. end:
  162. pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  163. __func__, best_rate,
  164. __clk_get_name((req->best_parent_hw)->clk),
  165. req->best_parent_rate);
  166. if (best_rate < 0)
  167. return best_rate;
  168. req->rate = best_rate;
  169. return 0;
  170. }
  171. /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
  172. static int clk_generated_set_parent(struct clk_hw *hw, u8 index)
  173. {
  174. struct clk_generated *gck = to_clk_generated(hw);
  175. if (index >= clk_hw_get_num_parents(hw))
  176. return -EINVAL;
  177. gck->parent_id = index;
  178. return 0;
  179. }
  180. static u8 clk_generated_get_parent(struct clk_hw *hw)
  181. {
  182. struct clk_generated *gck = to_clk_generated(hw);
  183. return gck->parent_id;
  184. }
  185. /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
  186. static int clk_generated_set_rate(struct clk_hw *hw,
  187. unsigned long rate,
  188. unsigned long parent_rate)
  189. {
  190. struct clk_generated *gck = to_clk_generated(hw);
  191. u32 div;
  192. if (!rate)
  193. return -EINVAL;
  194. if (gck->range.max && rate > gck->range.max)
  195. return -EINVAL;
  196. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  197. if (div > GENERATED_MAX_DIV + 1 || !div)
  198. return -EINVAL;
  199. gck->gckdiv = div - 1;
  200. return 0;
  201. }
  202. static const struct clk_ops generated_ops = {
  203. .enable = clk_generated_enable,
  204. .disable = clk_generated_disable,
  205. .is_enabled = clk_generated_is_enabled,
  206. .recalc_rate = clk_generated_recalc_rate,
  207. .determine_rate = clk_generated_determine_rate,
  208. .get_parent = clk_generated_get_parent,
  209. .set_parent = clk_generated_set_parent,
  210. .set_rate = clk_generated_set_rate,
  211. };
  212. /**
  213. * clk_generated_startup - Initialize a given clock to its default parent and
  214. * divisor parameter.
  215. *
  216. * @gck: Generated clock to set the startup parameters for.
  217. *
  218. * Take parameters from the hardware and update local clock configuration
  219. * accordingly.
  220. */
  221. static void clk_generated_startup(struct clk_generated *gck)
  222. {
  223. u32 tmp;
  224. unsigned long flags;
  225. spin_lock_irqsave(gck->lock, flags);
  226. regmap_write(gck->regmap, AT91_PMC_PCR,
  227. (gck->id & AT91_PMC_PCR_PID_MASK));
  228. regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  229. spin_unlock_irqrestore(gck->lock, flags);
  230. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  231. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  232. gck->gckdiv = (tmp & AT91_PMC_PCR_GCKDIV_MASK)
  233. >> AT91_PMC_PCR_GCKDIV_OFFSET;
  234. }
  235. static struct clk_hw * __init
  236. at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock,
  237. const char *name, const char **parent_names,
  238. u8 num_parents, u8 id, bool pll_audio,
  239. const struct clk_range *range)
  240. {
  241. struct clk_generated *gck;
  242. struct clk_init_data init;
  243. struct clk_hw *hw;
  244. int ret;
  245. gck = kzalloc(sizeof(*gck), GFP_KERNEL);
  246. if (!gck)
  247. return ERR_PTR(-ENOMEM);
  248. init.name = name;
  249. init.ops = &generated_ops;
  250. init.parent_names = parent_names;
  251. init.num_parents = num_parents;
  252. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  253. CLK_SET_RATE_PARENT;
  254. gck->id = id;
  255. gck->hw.init = &init;
  256. gck->regmap = regmap;
  257. gck->lock = lock;
  258. gck->range = *range;
  259. gck->audio_pll_allowed = pll_audio;
  260. clk_generated_startup(gck);
  261. hw = &gck->hw;
  262. ret = clk_hw_register(NULL, &gck->hw);
  263. if (ret) {
  264. kfree(gck);
  265. hw = ERR_PTR(ret);
  266. } else {
  267. pmc_register_id(id);
  268. }
  269. return hw;
  270. }
  271. static void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  272. {
  273. int num;
  274. u32 id;
  275. const char *name;
  276. struct clk_hw *hw;
  277. unsigned int num_parents;
  278. const char *parent_names[GENERATED_SOURCE_MAX];
  279. struct device_node *gcknp;
  280. struct clk_range range = CLK_RANGE(0, 0);
  281. struct regmap *regmap;
  282. num_parents = of_clk_get_parent_count(np);
  283. if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX)
  284. return;
  285. of_clk_parent_fill(np, parent_names, num_parents);
  286. num = of_get_child_count(np);
  287. if (!num || num > PERIPHERAL_MAX)
  288. return;
  289. regmap = syscon_node_to_regmap(of_get_parent(np));
  290. if (IS_ERR(regmap))
  291. return;
  292. for_each_child_of_node(np, gcknp) {
  293. bool pll_audio = false;
  294. if (of_property_read_u32(gcknp, "reg", &id))
  295. continue;
  296. if (id < PERIPHERAL_ID_MIN || id >= PERIPHERAL_MAX)
  297. continue;
  298. if (of_property_read_string(np, "clock-output-names", &name))
  299. name = gcknp->name;
  300. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  301. &range);
  302. if (of_device_is_compatible(np, "atmel,sama5d2-clk-generated") &&
  303. (id == GCK_ID_I2S0 || id == GCK_ID_I2S1 ||
  304. id == GCK_ID_CLASSD))
  305. pll_audio = true;
  306. hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  307. parent_names, num_parents,
  308. id, pll_audio, &range);
  309. if (IS_ERR(hw))
  310. continue;
  311. of_clk_add_hw_provider(gcknp, of_clk_hw_simple_get, hw);
  312. }
  313. }
  314. CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  315. of_sama5d2_clk_generated_setup);