clk-master.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 * __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 *clk = NULL;
  104. struct clk_init_data init;
  105. if (!name || !num_parents || !parent_names)
  106. return ERR_PTR(-EINVAL);
  107. master = kzalloc(sizeof(*master), GFP_KERNEL);
  108. if (!master)
  109. return ERR_PTR(-ENOMEM);
  110. init.name = name;
  111. init.ops = &master_ops;
  112. init.parent_names = parent_names;
  113. init.num_parents = num_parents;
  114. init.flags = 0;
  115. master->hw.init = &init;
  116. master->layout = layout;
  117. master->characteristics = characteristics;
  118. master->regmap = regmap;
  119. clk = clk_register(NULL, &master->hw);
  120. if (IS_ERR(clk)) {
  121. kfree(master);
  122. }
  123. return clk;
  124. }
  125. static const struct clk_master_layout at91rm9200_master_layout = {
  126. .mask = 0x31F,
  127. .pres_shift = 2,
  128. };
  129. static const struct clk_master_layout at91sam9x5_master_layout = {
  130. .mask = 0x373,
  131. .pres_shift = 4,
  132. };
  133. static struct clk_master_characteristics * __init
  134. of_at91_clk_master_get_characteristics(struct device_node *np)
  135. {
  136. struct clk_master_characteristics *characteristics;
  137. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  138. if (!characteristics)
  139. return NULL;
  140. if (of_at91_get_clk_range(np, "atmel,clk-output-range", &characteristics->output))
  141. goto out_free_characteristics;
  142. of_property_read_u32_array(np, "atmel,clk-divisors",
  143. characteristics->divisors, 4);
  144. characteristics->have_div3_pres =
  145. of_property_read_bool(np, "atmel,master-clk-have-div3-pres");
  146. return characteristics;
  147. out_free_characteristics:
  148. kfree(characteristics);
  149. return NULL;
  150. }
  151. static void __init
  152. of_at91_clk_master_setup(struct device_node *np,
  153. const struct clk_master_layout *layout)
  154. {
  155. struct clk *clk;
  156. unsigned int num_parents;
  157. const char *parent_names[MASTER_SOURCE_MAX];
  158. const char *name = np->name;
  159. struct clk_master_characteristics *characteristics;
  160. struct regmap *regmap;
  161. num_parents = of_clk_get_parent_count(np);
  162. if (num_parents == 0 || num_parents > MASTER_SOURCE_MAX)
  163. return;
  164. of_clk_parent_fill(np, parent_names, num_parents);
  165. of_property_read_string(np, "clock-output-names", &name);
  166. characteristics = of_at91_clk_master_get_characteristics(np);
  167. if (!characteristics)
  168. return;
  169. regmap = syscon_node_to_regmap(of_get_parent(np));
  170. if (IS_ERR(regmap))
  171. return;
  172. clk = at91_clk_register_master(regmap, name, num_parents,
  173. parent_names, layout,
  174. characteristics);
  175. if (IS_ERR(clk))
  176. goto out_free_characteristics;
  177. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  178. return;
  179. out_free_characteristics:
  180. kfree(characteristics);
  181. }
  182. static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  183. {
  184. of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  185. }
  186. CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  187. of_at91rm9200_clk_master_setup);
  188. static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  189. {
  190. of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  191. }
  192. CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  193. of_at91sam9x5_clk_master_setup);