clk-programmable.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include <linux/wait.h>
  17. #include <linux/sched.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/regmap.h>
  20. #include "pmc.h"
  21. #define PROG_SOURCE_MAX 5
  22. #define PROG_ID_MAX 7
  23. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  24. #define PROG_PRES_MASK 0x7
  25. #define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
  26. #define PROG_MAX_RM9200_CSS 3
  27. struct clk_programmable_layout {
  28. u8 pres_shift;
  29. u8 css_mask;
  30. u8 have_slck_mck;
  31. };
  32. struct clk_programmable {
  33. struct clk_hw hw;
  34. struct regmap *regmap;
  35. u8 id;
  36. const struct clk_programmable_layout *layout;
  37. };
  38. #define to_clk_programmable(hw) container_of(hw, struct clk_programmable, hw)
  39. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  40. unsigned long parent_rate)
  41. {
  42. struct clk_programmable *prog = to_clk_programmable(hw);
  43. unsigned int pckr;
  44. regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  45. return parent_rate >> PROG_PRES(prog->layout, pckr);
  46. }
  47. static int clk_programmable_determine_rate(struct clk_hw *hw,
  48. struct clk_rate_request *req)
  49. {
  50. struct clk_hw *parent;
  51. long best_rate = -EINVAL;
  52. unsigned long parent_rate;
  53. unsigned long tmp_rate;
  54. int shift;
  55. int i;
  56. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  57. parent = clk_hw_get_parent_by_index(hw, i);
  58. if (!parent)
  59. continue;
  60. parent_rate = clk_hw_get_rate(parent);
  61. for (shift = 0; shift < PROG_PRES_MASK; shift++) {
  62. tmp_rate = parent_rate >> shift;
  63. if (tmp_rate <= req->rate)
  64. break;
  65. }
  66. if (tmp_rate > req->rate)
  67. continue;
  68. if (best_rate < 0 ||
  69. (req->rate - tmp_rate) < (req->rate - best_rate)) {
  70. best_rate = tmp_rate;
  71. req->best_parent_rate = parent_rate;
  72. req->best_parent_hw = parent;
  73. }
  74. if (!best_rate)
  75. break;
  76. }
  77. if (best_rate < 0)
  78. return best_rate;
  79. req->rate = best_rate;
  80. return 0;
  81. }
  82. static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
  83. {
  84. struct clk_programmable *prog = to_clk_programmable(hw);
  85. const struct clk_programmable_layout *layout = prog->layout;
  86. unsigned int mask = layout->css_mask;
  87. unsigned int pckr = 0;
  88. if (layout->have_slck_mck)
  89. mask |= AT91_PMC_CSSMCK_MCK;
  90. if (index > layout->css_mask) {
  91. if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
  92. return -EINVAL;
  93. pckr |= AT91_PMC_CSSMCK_MCK;
  94. }
  95. regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
  96. return 0;
  97. }
  98. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  99. {
  100. struct clk_programmable *prog = to_clk_programmable(hw);
  101. const struct clk_programmable_layout *layout = prog->layout;
  102. unsigned int pckr;
  103. u8 ret;
  104. regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  105. ret = pckr & layout->css_mask;
  106. if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
  107. ret = PROG_MAX_RM9200_CSS + 1;
  108. return ret;
  109. }
  110. static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
  111. unsigned long parent_rate)
  112. {
  113. struct clk_programmable *prog = to_clk_programmable(hw);
  114. const struct clk_programmable_layout *layout = prog->layout;
  115. unsigned long div = parent_rate / rate;
  116. unsigned int pckr;
  117. int shift = 0;
  118. regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  119. if (!div)
  120. return -EINVAL;
  121. shift = fls(div) - 1;
  122. if (div != (1 << shift))
  123. return -EINVAL;
  124. if (shift >= PROG_PRES_MASK)
  125. return -EINVAL;
  126. regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
  127. PROG_PRES_MASK << layout->pres_shift,
  128. shift << layout->pres_shift);
  129. return 0;
  130. }
  131. static const struct clk_ops programmable_ops = {
  132. .recalc_rate = clk_programmable_recalc_rate,
  133. .determine_rate = clk_programmable_determine_rate,
  134. .get_parent = clk_programmable_get_parent,
  135. .set_parent = clk_programmable_set_parent,
  136. .set_rate = clk_programmable_set_rate,
  137. };
  138. static struct clk * __init
  139. at91_clk_register_programmable(struct regmap *regmap,
  140. const char *name, const char **parent_names,
  141. u8 num_parents, u8 id,
  142. const struct clk_programmable_layout *layout)
  143. {
  144. struct clk_programmable *prog;
  145. struct clk *clk = NULL;
  146. struct clk_init_data init;
  147. if (id > PROG_ID_MAX)
  148. return ERR_PTR(-EINVAL);
  149. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  150. if (!prog)
  151. return ERR_PTR(-ENOMEM);
  152. init.name = name;
  153. init.ops = &programmable_ops;
  154. init.parent_names = parent_names;
  155. init.num_parents = num_parents;
  156. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  157. prog->id = id;
  158. prog->layout = layout;
  159. prog->hw.init = &init;
  160. prog->regmap = regmap;
  161. clk = clk_register(NULL, &prog->hw);
  162. if (IS_ERR(clk))
  163. kfree(prog);
  164. return clk;
  165. }
  166. static const struct clk_programmable_layout at91rm9200_programmable_layout = {
  167. .pres_shift = 2,
  168. .css_mask = 0x3,
  169. .have_slck_mck = 0,
  170. };
  171. static const struct clk_programmable_layout at91sam9g45_programmable_layout = {
  172. .pres_shift = 2,
  173. .css_mask = 0x3,
  174. .have_slck_mck = 1,
  175. };
  176. static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  177. .pres_shift = 4,
  178. .css_mask = 0x7,
  179. .have_slck_mck = 0,
  180. };
  181. static void __init
  182. of_at91_clk_prog_setup(struct device_node *np,
  183. const struct clk_programmable_layout *layout)
  184. {
  185. int num;
  186. u32 id;
  187. struct clk *clk;
  188. int num_parents;
  189. const char *parent_names[PROG_SOURCE_MAX];
  190. const char *name;
  191. struct device_node *progclknp;
  192. struct regmap *regmap;
  193. num_parents = of_clk_get_parent_count(np);
  194. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  195. return;
  196. of_clk_parent_fill(np, parent_names, num_parents);
  197. num = of_get_child_count(np);
  198. if (!num || num > (PROG_ID_MAX + 1))
  199. return;
  200. regmap = syscon_node_to_regmap(of_get_parent(np));
  201. if (IS_ERR(regmap))
  202. return;
  203. for_each_child_of_node(np, progclknp) {
  204. if (of_property_read_u32(progclknp, "reg", &id))
  205. continue;
  206. if (of_property_read_string(np, "clock-output-names", &name))
  207. name = progclknp->name;
  208. clk = at91_clk_register_programmable(regmap, name,
  209. parent_names, num_parents,
  210. id, layout);
  211. if (IS_ERR(clk))
  212. continue;
  213. of_clk_add_provider(progclknp, of_clk_src_simple_get, clk);
  214. }
  215. }
  216. static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  217. {
  218. of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  219. }
  220. CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  221. of_at91rm9200_clk_prog_setup);
  222. static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  223. {
  224. of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  225. }
  226. CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  227. of_at91sam9g45_clk_prog_setup);
  228. static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  229. {
  230. of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  231. }
  232. CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  233. of_at91sam9x5_clk_prog_setup);