clk-generated.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Atmel Corporation,
  4. * Nicolas Ferre <[email protected]>
  5. *
  6. * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/clkdev.h>
  11. #include <linux/clk/at91_pmc.h>
  12. #include <linux/of.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/regmap.h>
  15. #include "pmc.h"
  16. #define GENERATED_MAX_DIV 255
  17. struct clk_generated {
  18. struct clk_hw hw;
  19. struct regmap *regmap;
  20. struct clk_range range;
  21. spinlock_t *lock;
  22. u32 *mux_table;
  23. u32 id;
  24. u32 gckdiv;
  25. const struct clk_pcr_layout *layout;
  26. struct at91_clk_pms pms;
  27. u8 parent_id;
  28. int chg_pid;
  29. };
  30. #define to_clk_generated(hw) \
  31. container_of(hw, struct clk_generated, hw)
  32. static int clk_generated_set(struct clk_generated *gck, int status)
  33. {
  34. unsigned long flags;
  35. unsigned int enable = status ? AT91_PMC_PCR_GCKEN : 0;
  36. spin_lock_irqsave(gck->lock, flags);
  37. regmap_write(gck->regmap, gck->layout->offset,
  38. (gck->id & gck->layout->pid_mask));
  39. regmap_update_bits(gck->regmap, gck->layout->offset,
  40. AT91_PMC_PCR_GCKDIV_MASK | gck->layout->gckcss_mask |
  41. gck->layout->cmd | enable,
  42. field_prep(gck->layout->gckcss_mask, gck->parent_id) |
  43. gck->layout->cmd |
  44. FIELD_PREP(AT91_PMC_PCR_GCKDIV_MASK, gck->gckdiv) |
  45. enable);
  46. spin_unlock_irqrestore(gck->lock, flags);
  47. return 0;
  48. }
  49. static int clk_generated_enable(struct clk_hw *hw)
  50. {
  51. struct clk_generated *gck = to_clk_generated(hw);
  52. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  53. __func__, gck->gckdiv, gck->parent_id);
  54. clk_generated_set(gck, 1);
  55. return 0;
  56. }
  57. static void clk_generated_disable(struct clk_hw *hw)
  58. {
  59. struct clk_generated *gck = to_clk_generated(hw);
  60. unsigned long flags;
  61. spin_lock_irqsave(gck->lock, flags);
  62. regmap_write(gck->regmap, gck->layout->offset,
  63. (gck->id & gck->layout->pid_mask));
  64. regmap_update_bits(gck->regmap, gck->layout->offset,
  65. gck->layout->cmd | AT91_PMC_PCR_GCKEN,
  66. gck->layout->cmd);
  67. spin_unlock_irqrestore(gck->lock, flags);
  68. }
  69. static int clk_generated_is_enabled(struct clk_hw *hw)
  70. {
  71. struct clk_generated *gck = to_clk_generated(hw);
  72. unsigned long flags;
  73. unsigned int status;
  74. spin_lock_irqsave(gck->lock, flags);
  75. regmap_write(gck->regmap, gck->layout->offset,
  76. (gck->id & gck->layout->pid_mask));
  77. regmap_read(gck->regmap, gck->layout->offset, &status);
  78. spin_unlock_irqrestore(gck->lock, flags);
  79. return !!(status & AT91_PMC_PCR_GCKEN);
  80. }
  81. static unsigned long
  82. clk_generated_recalc_rate(struct clk_hw *hw,
  83. unsigned long parent_rate)
  84. {
  85. struct clk_generated *gck = to_clk_generated(hw);
  86. return DIV_ROUND_CLOSEST(parent_rate, gck->gckdiv + 1);
  87. }
  88. static void clk_generated_best_diff(struct clk_rate_request *req,
  89. struct clk_hw *parent,
  90. unsigned long parent_rate, u32 div,
  91. int *best_diff, long *best_rate)
  92. {
  93. unsigned long tmp_rate;
  94. int tmp_diff;
  95. if (!div)
  96. tmp_rate = parent_rate;
  97. else
  98. tmp_rate = parent_rate / div;
  99. if (tmp_rate < req->min_rate || tmp_rate > req->max_rate)
  100. return;
  101. tmp_diff = abs(req->rate - tmp_rate);
  102. if (*best_diff < 0 || *best_diff >= tmp_diff) {
  103. *best_rate = tmp_rate;
  104. *best_diff = tmp_diff;
  105. req->best_parent_rate = parent_rate;
  106. req->best_parent_hw = parent;
  107. }
  108. }
  109. static int clk_generated_determine_rate(struct clk_hw *hw,
  110. struct clk_rate_request *req)
  111. {
  112. struct clk_generated *gck = to_clk_generated(hw);
  113. struct clk_hw *parent = NULL;
  114. long best_rate = -EINVAL;
  115. unsigned long min_rate, parent_rate;
  116. int best_diff = -1;
  117. int i;
  118. u32 div;
  119. /* do not look for a rate that is outside of our range */
  120. if (gck->range.max && req->rate > gck->range.max)
  121. req->rate = gck->range.max;
  122. if (gck->range.min && req->rate < gck->range.min)
  123. req->rate = gck->range.min;
  124. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  125. if (gck->chg_pid == i)
  126. continue;
  127. parent = clk_hw_get_parent_by_index(hw, i);
  128. if (!parent)
  129. continue;
  130. parent_rate = clk_hw_get_rate(parent);
  131. min_rate = DIV_ROUND_CLOSEST(parent_rate, GENERATED_MAX_DIV + 1);
  132. if (!parent_rate ||
  133. (gck->range.max && min_rate > gck->range.max))
  134. continue;
  135. div = DIV_ROUND_CLOSEST(parent_rate, req->rate);
  136. if (div > GENERATED_MAX_DIV + 1)
  137. div = GENERATED_MAX_DIV + 1;
  138. clk_generated_best_diff(req, parent, parent_rate, div,
  139. &best_diff, &best_rate);
  140. if (!best_diff)
  141. break;
  142. }
  143. /*
  144. * The audio_pll rate can be modified, unlike the five others clocks
  145. * that should never be altered.
  146. * The audio_pll can technically be used by multiple consumers. However,
  147. * with the rate locking, the first consumer to enable to clock will be
  148. * the one definitely setting the rate of the clock.
  149. * Since audio IPs are most likely to request the same rate, we enforce
  150. * that the only clks able to modify gck rate are those of audio IPs.
  151. */
  152. if (gck->chg_pid < 0)
  153. goto end;
  154. parent = clk_hw_get_parent_by_index(hw, gck->chg_pid);
  155. if (!parent)
  156. goto end;
  157. for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
  158. struct clk_rate_request req_parent;
  159. clk_hw_forward_rate_request(hw, req, parent, &req_parent, req->rate * div);
  160. if (__clk_determine_rate(parent, &req_parent))
  161. continue;
  162. clk_generated_best_diff(req, parent, req_parent.rate, div,
  163. &best_diff, &best_rate);
  164. if (!best_diff)
  165. break;
  166. }
  167. end:
  168. pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  169. __func__, best_rate,
  170. __clk_get_name((req->best_parent_hw)->clk),
  171. req->best_parent_rate);
  172. if (best_rate < 0 || (gck->range.max && best_rate > gck->range.max))
  173. return -EINVAL;
  174. req->rate = best_rate;
  175. return 0;
  176. }
  177. /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
  178. static int clk_generated_set_parent(struct clk_hw *hw, u8 index)
  179. {
  180. struct clk_generated *gck = to_clk_generated(hw);
  181. if (index >= clk_hw_get_num_parents(hw))
  182. return -EINVAL;
  183. if (gck->mux_table)
  184. gck->parent_id = clk_mux_index_to_val(gck->mux_table, 0, index);
  185. else
  186. gck->parent_id = index;
  187. return 0;
  188. }
  189. static u8 clk_generated_get_parent(struct clk_hw *hw)
  190. {
  191. struct clk_generated *gck = to_clk_generated(hw);
  192. return gck->parent_id;
  193. }
  194. /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
  195. static int clk_generated_set_rate(struct clk_hw *hw,
  196. unsigned long rate,
  197. unsigned long parent_rate)
  198. {
  199. struct clk_generated *gck = to_clk_generated(hw);
  200. u32 div;
  201. if (!rate)
  202. return -EINVAL;
  203. if (gck->range.max && rate > gck->range.max)
  204. return -EINVAL;
  205. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  206. if (div > GENERATED_MAX_DIV + 1 || !div)
  207. return -EINVAL;
  208. gck->gckdiv = div - 1;
  209. return 0;
  210. }
  211. static int clk_generated_save_context(struct clk_hw *hw)
  212. {
  213. struct clk_generated *gck = to_clk_generated(hw);
  214. gck->pms.status = clk_generated_is_enabled(&gck->hw);
  215. return 0;
  216. }
  217. static void clk_generated_restore_context(struct clk_hw *hw)
  218. {
  219. struct clk_generated *gck = to_clk_generated(hw);
  220. if (gck->pms.status)
  221. clk_generated_set(gck, gck->pms.status);
  222. }
  223. static const struct clk_ops generated_ops = {
  224. .enable = clk_generated_enable,
  225. .disable = clk_generated_disable,
  226. .is_enabled = clk_generated_is_enabled,
  227. .recalc_rate = clk_generated_recalc_rate,
  228. .determine_rate = clk_generated_determine_rate,
  229. .get_parent = clk_generated_get_parent,
  230. .set_parent = clk_generated_set_parent,
  231. .set_rate = clk_generated_set_rate,
  232. .save_context = clk_generated_save_context,
  233. .restore_context = clk_generated_restore_context,
  234. };
  235. /**
  236. * clk_generated_startup - Initialize a given clock to its default parent and
  237. * divisor parameter.
  238. *
  239. * @gck: Generated clock to set the startup parameters for.
  240. *
  241. * Take parameters from the hardware and update local clock configuration
  242. * accordingly.
  243. */
  244. static void clk_generated_startup(struct clk_generated *gck)
  245. {
  246. u32 tmp;
  247. unsigned long flags;
  248. spin_lock_irqsave(gck->lock, flags);
  249. regmap_write(gck->regmap, gck->layout->offset,
  250. (gck->id & gck->layout->pid_mask));
  251. regmap_read(gck->regmap, gck->layout->offset, &tmp);
  252. spin_unlock_irqrestore(gck->lock, flags);
  253. gck->parent_id = field_get(gck->layout->gckcss_mask, tmp);
  254. gck->gckdiv = FIELD_GET(AT91_PMC_PCR_GCKDIV_MASK, tmp);
  255. }
  256. struct clk_hw * __init
  257. at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock,
  258. const struct clk_pcr_layout *layout,
  259. const char *name, const char **parent_names,
  260. u32 *mux_table, u8 num_parents, u8 id,
  261. const struct clk_range *range,
  262. int chg_pid)
  263. {
  264. struct clk_generated *gck;
  265. struct clk_init_data init;
  266. struct clk_hw *hw;
  267. int ret;
  268. gck = kzalloc(sizeof(*gck), GFP_KERNEL);
  269. if (!gck)
  270. return ERR_PTR(-ENOMEM);
  271. init.name = name;
  272. init.ops = &generated_ops;
  273. init.parent_names = parent_names;
  274. init.num_parents = num_parents;
  275. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  276. if (chg_pid >= 0)
  277. init.flags |= CLK_SET_RATE_PARENT;
  278. gck->id = id;
  279. gck->hw.init = &init;
  280. gck->regmap = regmap;
  281. gck->lock = lock;
  282. gck->range = *range;
  283. gck->chg_pid = chg_pid;
  284. gck->layout = layout;
  285. gck->mux_table = mux_table;
  286. clk_generated_startup(gck);
  287. hw = &gck->hw;
  288. ret = clk_hw_register(NULL, &gck->hw);
  289. if (ret) {
  290. kfree(gck);
  291. hw = ERR_PTR(ret);
  292. }
  293. return hw;
  294. }