clk-master.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 MASTER_SOURCE_MAX 4
  18. #define MASTER_PRES_MASK 0x7
  19. #define MASTER_PRES_MAX MASTER_PRES_MASK
  20. #define MASTER_DIV_SHIFT 8
  21. #define MASTER_DIV_MASK 0x3
  22. struct clk_master_characteristics {
  23. struct clk_range output;
  24. u32 divisors[4];
  25. u8 have_div3_pres;
  26. };
  27. struct clk_master_layout {
  28. u32 mask;
  29. u8 pres_shift;
  30. };
  31. #define to_clk_master(hw) container_of(hw, struct clk_master, hw)
  32. struct clk_master {
  33. struct clk_hw hw;
  34. struct regmap *regmap;
  35. const struct clk_master_layout *layout;
  36. const struct clk_master_characteristics *characteristics;
  37. };
  38. static inline bool clk_master_ready(struct regmap *regmap)
  39. {
  40. unsigned int status;
  41. regmap_read(regmap, AT91_PMC_SR, &status);
  42. return status & AT91_PMC_MCKRDY ? 1 : 0;
  43. }
  44. static int clk_master_prepare(struct clk_hw *hw)
  45. {
  46. struct clk_master *master = to_clk_master(hw);
  47. while (!clk_master_ready(master->regmap))
  48. cpu_relax();
  49. return 0;
  50. }
  51. static int clk_master_is_prepared(struct clk_hw *hw)
  52. {
  53. struct clk_master *master = to_clk_master(hw);
  54. return clk_master_ready(master->regmap);
  55. }
  56. static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  57. unsigned long parent_rate)
  58. {
  59. u8 pres;
  60. u8 div;
  61. unsigned long rate = parent_rate;
  62. struct clk_master *master = to_clk_master(hw);
  63. const struct clk_master_layout *layout = master->layout;
  64. const struct clk_master_characteristics *characteristics =
  65. master->characteristics;
  66. unsigned int mckr;
  67. regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  68. mckr &= layout->mask;
  69. pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK;
  70. div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  71. if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX)
  72. rate /= 3;
  73. else
  74. rate >>= pres;
  75. rate /= characteristics->divisors[div];
  76. if (rate < characteristics->output.min)
  77. pr_warn("master clk is underclocked");
  78. else if (rate > characteristics->output.max)
  79. pr_warn("master clk is overclocked");
  80. return rate;
  81. }
  82. static u8 clk_master_get_parent(struct clk_hw *hw)
  83. {
  84. struct clk_master *master = to_clk_master(hw);
  85. unsigned int mckr;
  86. regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  87. return mckr & AT91_PMC_CSS;
  88. }
  89. static const struct clk_ops master_ops = {
  90. .prepare = clk_master_prepare,
  91. .is_prepared = clk_master_is_prepared,
  92. .recalc_rate = clk_master_recalc_rate,
  93. .get_parent = clk_master_get_parent,
  94. };
  95. static struct clk_hw * __init
  96. at91_clk_register_master(struct regmap *regmap,
  97. const char *name, int num_parents,
  98. const char **parent_names,
  99. const struct clk_master_layout *layout,
  100. const struct clk_master_characteristics *characteristics)
  101. {
  102. struct clk_master *master;
  103. struct clk_init_data init;
  104. struct clk_hw *hw;
  105. int ret;
  106. if (!name || !num_parents || !parent_names)
  107. return ERR_PTR(-EINVAL);
  108. master = kzalloc(sizeof(*master), GFP_KERNEL);
  109. if (!master)
  110. return ERR_PTR(-ENOMEM);
  111. init.name = name;
  112. init.ops = &master_ops;
  113. init.parent_names = parent_names;
  114. init.num_parents = num_parents;
  115. init.flags = 0;
  116. master->hw.init = &init;
  117. master->layout = layout;
  118. master->characteristics = characteristics;
  119. master->regmap = regmap;
  120. hw = &master->hw;
  121. ret = clk_hw_register(NULL, &master->hw);
  122. if (ret) {
  123. kfree(master);
  124. hw = ERR_PTR(ret);
  125. }
  126. return hw;
  127. }
  128. static const struct clk_master_layout at91rm9200_master_layout = {
  129. .mask = 0x31F,
  130. .pres_shift = 2,
  131. };
  132. static const struct clk_master_layout at91sam9x5_master_layout = {
  133. .mask = 0x373,
  134. .pres_shift = 4,
  135. };
  136. static struct clk_master_characteristics * __init
  137. of_at91_clk_master_get_characteristics(struct device_node *np)
  138. {
  139. struct clk_master_characteristics *characteristics;
  140. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  141. if (!characteristics)
  142. return NULL;
  143. if (of_at91_get_clk_range(np, "atmel,clk-output-range", &characteristics->output))
  144. goto out_free_characteristics;
  145. of_property_read_u32_array(np, "atmel,clk-divisors",
  146. characteristics->divisors, 4);
  147. characteristics->have_div3_pres =
  148. of_property_read_bool(np, "atmel,master-clk-have-div3-pres");
  149. return characteristics;
  150. out_free_characteristics:
  151. kfree(characteristics);
  152. return NULL;
  153. }
  154. static void __init
  155. of_at91_clk_master_setup(struct device_node *np,
  156. const struct clk_master_layout *layout)
  157. {
  158. struct clk_hw *hw;
  159. unsigned int num_parents;
  160. const char *parent_names[MASTER_SOURCE_MAX];
  161. const char *name = np->name;
  162. struct clk_master_characteristics *characteristics;
  163. struct regmap *regmap;
  164. num_parents = of_clk_get_parent_count(np);
  165. if (num_parents == 0 || num_parents > MASTER_SOURCE_MAX)
  166. return;
  167. of_clk_parent_fill(np, parent_names, num_parents);
  168. of_property_read_string(np, "clock-output-names", &name);
  169. characteristics = of_at91_clk_master_get_characteristics(np);
  170. if (!characteristics)
  171. return;
  172. regmap = syscon_node_to_regmap(of_get_parent(np));
  173. if (IS_ERR(regmap))
  174. return;
  175. hw = at91_clk_register_master(regmap, name, num_parents,
  176. parent_names, layout,
  177. characteristics);
  178. if (IS_ERR(hw))
  179. goto out_free_characteristics;
  180. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  181. return;
  182. out_free_characteristics:
  183. kfree(characteristics);
  184. }
  185. static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  186. {
  187. of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  188. }
  189. CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  190. of_at91rm9200_clk_master_setup);
  191. static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  192. {
  193. of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  194. }
  195. CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  196. of_at91sam9x5_clk_master_setup);