clk-flexgen.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * clk-flexgen.c
  3. *
  4. * Copyright (C) ST-Microelectronics SA 2013
  5. * Author: Maxime Coquelin <maxime.coquelin@st.com> for ST-Microelectronics.
  6. * License terms: GNU General Public License (GPL), version 2 */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/string.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. struct clkgen_data {
  17. unsigned long flags;
  18. bool mode;
  19. };
  20. struct flexgen {
  21. struct clk_hw hw;
  22. /* Crossbar */
  23. struct clk_mux mux;
  24. /* Pre-divisor's gate */
  25. struct clk_gate pgate;
  26. /* Pre-divisor */
  27. struct clk_divider pdiv;
  28. /* Final divisor's gate */
  29. struct clk_gate fgate;
  30. /* Final divisor */
  31. struct clk_divider fdiv;
  32. /* Asynchronous mode control */
  33. struct clk_gate sync;
  34. /* hw control flags */
  35. bool control_mode;
  36. };
  37. #define to_flexgen(_hw) container_of(_hw, struct flexgen, hw)
  38. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  39. static int flexgen_enable(struct clk_hw *hw)
  40. {
  41. struct flexgen *flexgen = to_flexgen(hw);
  42. struct clk_hw *pgate_hw = &flexgen->pgate.hw;
  43. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  44. __clk_hw_set_clk(pgate_hw, hw);
  45. __clk_hw_set_clk(fgate_hw, hw);
  46. clk_gate_ops.enable(pgate_hw);
  47. clk_gate_ops.enable(fgate_hw);
  48. pr_debug("%s: flexgen output enabled\n", clk_hw_get_name(hw));
  49. return 0;
  50. }
  51. static void flexgen_disable(struct clk_hw *hw)
  52. {
  53. struct flexgen *flexgen = to_flexgen(hw);
  54. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  55. /* disable only the final gate */
  56. __clk_hw_set_clk(fgate_hw, hw);
  57. clk_gate_ops.disable(fgate_hw);
  58. pr_debug("%s: flexgen output disabled\n", clk_hw_get_name(hw));
  59. }
  60. static int flexgen_is_enabled(struct clk_hw *hw)
  61. {
  62. struct flexgen *flexgen = to_flexgen(hw);
  63. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  64. __clk_hw_set_clk(fgate_hw, hw);
  65. if (!clk_gate_ops.is_enabled(fgate_hw))
  66. return 0;
  67. return 1;
  68. }
  69. static u8 flexgen_get_parent(struct clk_hw *hw)
  70. {
  71. struct flexgen *flexgen = to_flexgen(hw);
  72. struct clk_hw *mux_hw = &flexgen->mux.hw;
  73. __clk_hw_set_clk(mux_hw, hw);
  74. return clk_mux_ops.get_parent(mux_hw);
  75. }
  76. static int flexgen_set_parent(struct clk_hw *hw, u8 index)
  77. {
  78. struct flexgen *flexgen = to_flexgen(hw);
  79. struct clk_hw *mux_hw = &flexgen->mux.hw;
  80. __clk_hw_set_clk(mux_hw, hw);
  81. return clk_mux_ops.set_parent(mux_hw, index);
  82. }
  83. static inline unsigned long
  84. clk_best_div(unsigned long parent_rate, unsigned long rate)
  85. {
  86. return parent_rate / rate + ((rate > (2*(parent_rate % rate))) ? 0 : 1);
  87. }
  88. static long flexgen_round_rate(struct clk_hw *hw, unsigned long rate,
  89. unsigned long *prate)
  90. {
  91. unsigned long div;
  92. /* Round div according to exact prate and wished rate */
  93. div = clk_best_div(*prate, rate);
  94. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  95. *prate = rate * div;
  96. return rate;
  97. }
  98. return *prate / div;
  99. }
  100. static unsigned long flexgen_recalc_rate(struct clk_hw *hw,
  101. unsigned long parent_rate)
  102. {
  103. struct flexgen *flexgen = to_flexgen(hw);
  104. struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
  105. struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
  106. unsigned long mid_rate;
  107. __clk_hw_set_clk(pdiv_hw, hw);
  108. __clk_hw_set_clk(fdiv_hw, hw);
  109. mid_rate = clk_divider_ops.recalc_rate(pdiv_hw, parent_rate);
  110. return clk_divider_ops.recalc_rate(fdiv_hw, mid_rate);
  111. }
  112. static int flexgen_set_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. struct flexgen *flexgen = to_flexgen(hw);
  116. struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
  117. struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
  118. struct clk_hw *sync_hw = &flexgen->sync.hw;
  119. struct clk_gate *config = to_clk_gate(sync_hw);
  120. unsigned long div = 0;
  121. int ret = 0;
  122. u32 reg;
  123. __clk_hw_set_clk(pdiv_hw, hw);
  124. __clk_hw_set_clk(fdiv_hw, hw);
  125. if (flexgen->control_mode) {
  126. reg = readl(config->reg);
  127. reg &= ~BIT(config->bit_idx);
  128. writel(reg, config->reg);
  129. }
  130. div = clk_best_div(parent_rate, rate);
  131. /*
  132. * pdiv is mainly targeted for low freq results, while fdiv
  133. * should be used for div <= 64. The other way round can
  134. * lead to 'duty cycle' issues.
  135. */
  136. if (div <= 64) {
  137. clk_divider_ops.set_rate(pdiv_hw, parent_rate, parent_rate);
  138. ret = clk_divider_ops.set_rate(fdiv_hw, rate, rate * div);
  139. } else {
  140. clk_divider_ops.set_rate(fdiv_hw, parent_rate, parent_rate);
  141. ret = clk_divider_ops.set_rate(pdiv_hw, rate, rate * div);
  142. }
  143. return ret;
  144. }
  145. static const struct clk_ops flexgen_ops = {
  146. .enable = flexgen_enable,
  147. .disable = flexgen_disable,
  148. .is_enabled = flexgen_is_enabled,
  149. .get_parent = flexgen_get_parent,
  150. .set_parent = flexgen_set_parent,
  151. .round_rate = flexgen_round_rate,
  152. .recalc_rate = flexgen_recalc_rate,
  153. .set_rate = flexgen_set_rate,
  154. };
  155. static struct clk *clk_register_flexgen(const char *name,
  156. const char **parent_names, u8 num_parents,
  157. void __iomem *reg, spinlock_t *lock, u32 idx,
  158. unsigned long flexgen_flags, bool mode) {
  159. struct flexgen *fgxbar;
  160. struct clk *clk;
  161. struct clk_init_data init;
  162. u32 xbar_shift;
  163. void __iomem *xbar_reg, *fdiv_reg;
  164. fgxbar = kzalloc(sizeof(struct flexgen), GFP_KERNEL);
  165. if (!fgxbar)
  166. return ERR_PTR(-ENOMEM);
  167. init.name = name;
  168. init.ops = &flexgen_ops;
  169. init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE | flexgen_flags;
  170. init.parent_names = parent_names;
  171. init.num_parents = num_parents;
  172. xbar_reg = reg + 0x18 + (idx & ~0x3);
  173. xbar_shift = (idx % 4) * 0x8;
  174. fdiv_reg = reg + 0x164 + idx * 4;
  175. /* Crossbar element config */
  176. fgxbar->mux.lock = lock;
  177. fgxbar->mux.mask = BIT(6) - 1;
  178. fgxbar->mux.reg = xbar_reg;
  179. fgxbar->mux.shift = xbar_shift;
  180. fgxbar->mux.table = NULL;
  181. /* Pre-divider's gate config (in xbar register)*/
  182. fgxbar->pgate.lock = lock;
  183. fgxbar->pgate.reg = xbar_reg;
  184. fgxbar->pgate.bit_idx = xbar_shift + 6;
  185. /* Pre-divider config */
  186. fgxbar->pdiv.lock = lock;
  187. fgxbar->pdiv.reg = reg + 0x58 + idx * 4;
  188. fgxbar->pdiv.width = 10;
  189. /* Final divider's gate config */
  190. fgxbar->fgate.lock = lock;
  191. fgxbar->fgate.reg = fdiv_reg;
  192. fgxbar->fgate.bit_idx = 6;
  193. /* Final divider config */
  194. fgxbar->fdiv.lock = lock;
  195. fgxbar->fdiv.reg = fdiv_reg;
  196. fgxbar->fdiv.width = 6;
  197. /* Final divider sync config */
  198. fgxbar->sync.lock = lock;
  199. fgxbar->sync.reg = fdiv_reg;
  200. fgxbar->sync.bit_idx = 7;
  201. fgxbar->control_mode = mode;
  202. fgxbar->hw.init = &init;
  203. clk = clk_register(NULL, &fgxbar->hw);
  204. if (IS_ERR(clk))
  205. kfree(fgxbar);
  206. else
  207. pr_debug("%s: parent %s rate %u\n",
  208. __clk_get_name(clk),
  209. __clk_get_name(clk_get_parent(clk)),
  210. (unsigned int)clk_get_rate(clk));
  211. return clk;
  212. }
  213. static const char ** __init flexgen_get_parents(struct device_node *np,
  214. int *num_parents)
  215. {
  216. const char **parents;
  217. unsigned int nparents;
  218. nparents = of_clk_get_parent_count(np);
  219. if (WARN_ON(!nparents))
  220. return NULL;
  221. parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
  222. if (!parents)
  223. return NULL;
  224. *num_parents = of_clk_parent_fill(np, parents, nparents);
  225. return parents;
  226. }
  227. static const struct clkgen_data clkgen_audio = {
  228. .flags = CLK_SET_RATE_PARENT,
  229. };
  230. static const struct clkgen_data clkgen_video = {
  231. .flags = CLK_SET_RATE_PARENT,
  232. .mode = 1,
  233. };
  234. static const struct of_device_id flexgen_of_match[] = {
  235. {
  236. .compatible = "st,flexgen-audio",
  237. .data = &clkgen_audio,
  238. },
  239. {
  240. .compatible = "st,flexgen-video",
  241. .data = &clkgen_video,
  242. },
  243. {}
  244. };
  245. static void __init st_of_flexgen_setup(struct device_node *np)
  246. {
  247. struct device_node *pnode;
  248. void __iomem *reg;
  249. struct clk_onecell_data *clk_data;
  250. const char **parents;
  251. int num_parents, i;
  252. spinlock_t *rlock = NULL;
  253. const struct of_device_id *match;
  254. struct clkgen_data *data = NULL;
  255. unsigned long flex_flags = 0;
  256. int ret;
  257. bool clk_mode = 0;
  258. pnode = of_get_parent(np);
  259. if (!pnode)
  260. return;
  261. reg = of_iomap(pnode, 0);
  262. if (!reg)
  263. return;
  264. parents = flexgen_get_parents(np, &num_parents);
  265. if (!parents)
  266. return;
  267. match = of_match_node(flexgen_of_match, np);
  268. if (match) {
  269. data = (struct clkgen_data *)match->data;
  270. flex_flags = data->flags;
  271. clk_mode = data->mode;
  272. }
  273. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  274. if (!clk_data)
  275. goto err;
  276. ret = of_property_count_strings(np, "clock-output-names");
  277. if (ret <= 0) {
  278. pr_err("%s: Failed to get number of output clocks (%d)",
  279. __func__, clk_data->clk_num);
  280. goto err;
  281. }
  282. clk_data->clk_num = ret;
  283. clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
  284. GFP_KERNEL);
  285. if (!clk_data->clks)
  286. goto err;
  287. rlock = kzalloc(sizeof(spinlock_t), GFP_KERNEL);
  288. if (!rlock)
  289. goto err;
  290. spin_lock_init(rlock);
  291. for (i = 0; i < clk_data->clk_num; i++) {
  292. struct clk *clk;
  293. const char *clk_name;
  294. if (of_property_read_string_index(np, "clock-output-names",
  295. i, &clk_name)) {
  296. break;
  297. }
  298. of_clk_detect_critical(np, i, &flex_flags);
  299. /*
  300. * If we read an empty clock name then the output is unused
  301. */
  302. if (*clk_name == '\0')
  303. continue;
  304. clk = clk_register_flexgen(clk_name, parents, num_parents,
  305. reg, rlock, i, flex_flags, clk_mode);
  306. if (IS_ERR(clk))
  307. goto err;
  308. clk_data->clks[i] = clk;
  309. }
  310. kfree(parents);
  311. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  312. return;
  313. err:
  314. if (clk_data)
  315. kfree(clk_data->clks);
  316. kfree(clk_data);
  317. kfree(parents);
  318. kfree(rlock);
  319. }
  320. CLK_OF_DECLARE(flexgen, "st,flexgen", st_of_flexgen_setup);