clk-programmable.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. } else {
  164. pmc_register_pck(id);
  165. }
  166. return hw;
  167. }
  168. static const struct clk_programmable_layout at91rm9200_programmable_layout = {
  169. .pres_shift = 2,
  170. .css_mask = 0x3,
  171. .have_slck_mck = 0,
  172. };
  173. static const struct clk_programmable_layout at91sam9g45_programmable_layout = {
  174. .pres_shift = 2,
  175. .css_mask = 0x3,
  176. .have_slck_mck = 1,
  177. };
  178. static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  179. .pres_shift = 4,
  180. .css_mask = 0x7,
  181. .have_slck_mck = 0,
  182. };
  183. static void __init
  184. of_at91_clk_prog_setup(struct device_node *np,
  185. const struct clk_programmable_layout *layout)
  186. {
  187. int num;
  188. u32 id;
  189. struct clk_hw *hw;
  190. unsigned int num_parents;
  191. const char *parent_names[PROG_SOURCE_MAX];
  192. const char *name;
  193. struct device_node *progclknp;
  194. struct regmap *regmap;
  195. num_parents = of_clk_get_parent_count(np);
  196. if (num_parents == 0 || num_parents > PROG_SOURCE_MAX)
  197. return;
  198. of_clk_parent_fill(np, parent_names, num_parents);
  199. num = of_get_child_count(np);
  200. if (!num || num > (PROG_ID_MAX + 1))
  201. return;
  202. regmap = syscon_node_to_regmap(of_get_parent(np));
  203. if (IS_ERR(regmap))
  204. return;
  205. for_each_child_of_node(np, progclknp) {
  206. if (of_property_read_u32(progclknp, "reg", &id))
  207. continue;
  208. if (of_property_read_string(np, "clock-output-names", &name))
  209. name = progclknp->name;
  210. hw = at91_clk_register_programmable(regmap, name,
  211. parent_names, num_parents,
  212. id, layout);
  213. if (IS_ERR(hw))
  214. continue;
  215. of_clk_add_hw_provider(progclknp, of_clk_hw_simple_get, hw);
  216. }
  217. }
  218. static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  219. {
  220. of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  221. }
  222. CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  223. of_at91rm9200_clk_prog_setup);
  224. static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  225. {
  226. of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  227. }
  228. CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  229. of_at91sam9g45_clk_prog_setup);
  230. static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  231. {
  232. of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  233. }
  234. CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  235. of_at91sam9x5_clk_prog_setup);