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/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_hw * __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_hw *hw;
  142. struct clk_init_data init;
  143. int ret;
  144. if (id > PROG_ID_MAX)
  145. return ERR_PTR(-EINVAL);
  146. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  147. if (!prog)
  148. return ERR_PTR(-ENOMEM);
  149. init.name = name;
  150. init.ops = &programmable_ops;
  151. init.parent_names = parent_names;
  152. init.num_parents = num_parents;
  153. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  154. prog->id = id;
  155. prog->layout = layout;
  156. prog->hw.init = &init;
  157. prog->regmap = regmap;
  158. hw = &prog->hw;
  159. ret = clk_hw_register(NULL, &prog->hw);
  160. if (ret) {
  161. kfree(prog);
  162. hw = ERR_PTR(ret);
  163. }
  164. return hw;
  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_hw *hw;
  188. unsigned 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. hw = at91_clk_register_programmable(regmap, name,
  209. parent_names, num_parents,
  210. id, layout);
  211. if (IS_ERR(hw))
  212. continue;
  213. of_clk_add_hw_provider(progclknp, of_clk_hw_simple_get, hw);
  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);