mux.c 6.4 KB

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