clk-programmable.c 7.0 KB

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