mux.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * TI Multiplexer Clock
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/clk/ti.h>
  23. #include "clock.h"
  24. #undef pr_fmt
  25. #define pr_fmt(fmt) "%s: " fmt, __func__
  26. static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
  27. {
  28. struct clk_omap_mux *mux = to_clk_omap_mux(hw);
  29. int num_parents = clk_hw_get_num_parents(hw);
  30. u32 val;
  31. /*
  32. * FIXME need a mux-specific flag to determine if val is bitwise or
  33. * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
  34. * from 0x1 to 0x7 (index starts at one)
  35. * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
  36. * val = 0x4 really means "bit 2, index starts at bit 0"
  37. */
  38. val = ti_clk_ll_ops->clk_readl(&mux->reg) >> mux->shift;
  39. val &= mux->mask;
  40. if (mux->table) {
  41. int i;
  42. for (i = 0; i < num_parents; i++)
  43. if (mux->table[i] == val)
  44. return i;
  45. return -EINVAL;
  46. }
  47. if (val && (mux->flags & CLK_MUX_INDEX_BIT))
  48. val = ffs(val) - 1;
  49. if (val && (mux->flags & CLK_MUX_INDEX_ONE))
  50. val--;
  51. if (val >= num_parents)
  52. return -EINVAL;
  53. return val;
  54. }
  55. static int ti_clk_mux_set_parent(struct clk_hw *hw, u8 index)
  56. {
  57. struct clk_omap_mux *mux = to_clk_omap_mux(hw);
  58. u32 val;
  59. if (mux->table) {
  60. index = mux->table[index];
  61. } else {
  62. if (mux->flags & CLK_MUX_INDEX_BIT)
  63. index = (1 << ffs(index));
  64. if (mux->flags & CLK_MUX_INDEX_ONE)
  65. index++;
  66. }
  67. if (mux->flags & CLK_MUX_HIWORD_MASK) {
  68. val = mux->mask << (mux->shift + 16);
  69. } else {
  70. val = ti_clk_ll_ops->clk_readl(&mux->reg);
  71. val &= ~(mux->mask << mux->shift);
  72. }
  73. val |= index << mux->shift;
  74. ti_clk_ll_ops->clk_writel(val, &mux->reg);
  75. ti_clk_latch(&mux->reg, mux->latch);
  76. return 0;
  77. }
  78. /**
  79. * clk_mux_save_context - Save the parent selcted in the mux
  80. * @hw: pointer struct clk_hw
  81. *
  82. * Save the parent mux value.
  83. */
  84. static int clk_mux_save_context(struct clk_hw *hw)
  85. {
  86. struct clk_mux *mux = to_clk_mux(hw);
  87. mux->saved_parent = ti_clk_mux_get_parent(hw);
  88. return 0;
  89. }
  90. /**
  91. * clk_mux_restore_context - Restore the parent in the mux
  92. * @hw: pointer struct clk_hw
  93. *
  94. * Restore the saved parent mux value.
  95. */
  96. static void clk_mux_restore_context(struct clk_hw *hw)
  97. {
  98. struct clk_mux *mux = to_clk_mux(hw);
  99. ti_clk_mux_set_parent(hw, mux->saved_parent);
  100. }
  101. const struct clk_ops ti_clk_mux_ops = {
  102. .get_parent = ti_clk_mux_get_parent,
  103. .set_parent = ti_clk_mux_set_parent,
  104. .determine_rate = __clk_mux_determine_rate,
  105. .save_context = clk_mux_save_context,
  106. .restore_context = clk_mux_restore_context,
  107. };
  108. static struct clk *_register_mux(struct device *dev, const char *name,
  109. const char * const *parent_names,
  110. u8 num_parents, unsigned long flags,
  111. struct clk_omap_reg *reg, u8 shift, u32 mask,
  112. s8 latch, u8 clk_mux_flags, u32 *table)
  113. {
  114. struct clk_omap_mux *mux;
  115. struct clk *clk;
  116. struct clk_init_data init;
  117. /* allocate the mux */
  118. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  119. if (!mux) {
  120. pr_err("%s: could not allocate mux clk\n", __func__);
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. init.name = name;
  124. init.ops = &ti_clk_mux_ops;
  125. init.flags = flags | CLK_IS_BASIC;
  126. init.parent_names = parent_names;
  127. init.num_parents = num_parents;
  128. /* struct clk_mux assignments */
  129. memcpy(&mux->reg, reg, sizeof(*reg));
  130. mux->shift = shift;
  131. mux->mask = mask;
  132. mux->latch = latch;
  133. mux->flags = clk_mux_flags;
  134. mux->table = table;
  135. mux->hw.init = &init;
  136. clk = ti_clk_register(dev, &mux->hw, name);
  137. if (IS_ERR(clk))
  138. kfree(mux);
  139. return clk;
  140. }
  141. struct clk *ti_clk_register_mux(struct ti_clk *setup)
  142. {
  143. struct ti_clk_mux *mux;
  144. u32 flags;
  145. u8 mux_flags = 0;
  146. struct clk_omap_reg reg;
  147. u32 mask;
  148. mux = setup->data;
  149. flags = CLK_SET_RATE_NO_REPARENT;
  150. mask = mux->num_parents;
  151. if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
  152. mask--;
  153. mask = (1 << fls(mask)) - 1;
  154. reg.index = mux->module;
  155. reg.offset = mux->reg;
  156. reg.ptr = NULL;
  157. if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
  158. mux_flags |= CLK_MUX_INDEX_ONE;
  159. if (mux->flags & CLKF_SET_RATE_PARENT)
  160. flags |= CLK_SET_RATE_PARENT;
  161. return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
  162. flags, &reg, mux->bit_shift, mask, -EINVAL,
  163. mux_flags, NULL);
  164. }
  165. /**
  166. * of_mux_clk_setup - Setup function for simple mux rate clock
  167. * @node: DT node for the clock
  168. *
  169. * Sets up a basic clock multiplexer.
  170. */
  171. static void of_mux_clk_setup(struct device_node *node)
  172. {
  173. struct clk *clk;
  174. struct clk_omap_reg reg;
  175. unsigned int num_parents;
  176. const char **parent_names;
  177. u8 clk_mux_flags = 0;
  178. u32 mask = 0;
  179. u32 shift = 0;
  180. s32 latch = -EINVAL;
  181. u32 flags = CLK_SET_RATE_NO_REPARENT;
  182. num_parents = of_clk_get_parent_count(node);
  183. if (num_parents < 2) {
  184. pr_err("mux-clock %s must have parents\n", node->name);
  185. return;
  186. }
  187. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  188. if (!parent_names)
  189. goto cleanup;
  190. of_clk_parent_fill(node, parent_names, num_parents);
  191. if (ti_clk_get_reg_addr(node, 0, &reg))
  192. goto cleanup;
  193. of_property_read_u32(node, "ti,bit-shift", &shift);
  194. of_property_read_u32(node, "ti,latch-bit", &latch);
  195. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  196. clk_mux_flags |= CLK_MUX_INDEX_ONE;
  197. if (of_property_read_bool(node, "ti,set-rate-parent"))
  198. flags |= CLK_SET_RATE_PARENT;
  199. /* Generate bit-mask based on parent info */
  200. mask = num_parents;
  201. if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
  202. mask--;
  203. mask = (1 << fls(mask)) - 1;
  204. clk = _register_mux(NULL, node->name, parent_names, num_parents,
  205. flags, &reg, shift, mask, latch, clk_mux_flags,
  206. NULL);
  207. if (!IS_ERR(clk))
  208. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  209. cleanup:
  210. kfree(parent_names);
  211. }
  212. CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
  213. struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
  214. {
  215. struct clk_omap_mux *mux;
  216. int num_parents;
  217. if (!setup)
  218. return NULL;
  219. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  220. if (!mux)
  221. return ERR_PTR(-ENOMEM);
  222. mux->shift = setup->bit_shift;
  223. mux->latch = -EINVAL;
  224. mux->reg.index = setup->module;
  225. mux->reg.offset = setup->reg;
  226. if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
  227. mux->flags |= CLK_MUX_INDEX_ONE;
  228. num_parents = setup->num_parents;
  229. mux->mask = num_parents - 1;
  230. mux->mask = (1 << fls(mux->mask)) - 1;
  231. return &mux->hw;
  232. }
  233. static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
  234. {
  235. struct clk_omap_mux *mux;
  236. unsigned int num_parents;
  237. u32 val;
  238. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  239. if (!mux)
  240. return;
  241. if (ti_clk_get_reg_addr(node, 0, &mux->reg))
  242. goto cleanup;
  243. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  244. mux->shift = val;
  245. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  246. mux->flags |= CLK_MUX_INDEX_ONE;
  247. num_parents = of_clk_get_parent_count(node);
  248. if (num_parents < 2) {
  249. pr_err("%s must have parents\n", node->name);
  250. goto cleanup;
  251. }
  252. mux->mask = num_parents - 1;
  253. mux->mask = (1 << fls(mux->mask)) - 1;
  254. if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
  255. return;
  256. cleanup:
  257. kfree(mux);
  258. }
  259. CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
  260. of_ti_composite_mux_clk_setup);