mux.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. const struct clk_ops ti_clk_mux_ops = {
  79. .get_parent = ti_clk_mux_get_parent,
  80. .set_parent = ti_clk_mux_set_parent,
  81. .determine_rate = __clk_mux_determine_rate,
  82. };
  83. static struct clk *_register_mux(struct device *dev, const char *name,
  84. const char * const *parent_names,
  85. u8 num_parents, unsigned long flags,
  86. struct clk_omap_reg *reg, u8 shift, u32 mask,
  87. s8 latch, u8 clk_mux_flags, u32 *table)
  88. {
  89. struct clk_omap_mux *mux;
  90. struct clk *clk;
  91. struct clk_init_data init;
  92. /* allocate the mux */
  93. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  94. if (!mux)
  95. return ERR_PTR(-ENOMEM);
  96. init.name = name;
  97. init.ops = &ti_clk_mux_ops;
  98. init.flags = flags | CLK_IS_BASIC;
  99. init.parent_names = parent_names;
  100. init.num_parents = num_parents;
  101. /* struct clk_mux assignments */
  102. memcpy(&mux->reg, reg, sizeof(*reg));
  103. mux->shift = shift;
  104. mux->mask = mask;
  105. mux->latch = latch;
  106. mux->flags = clk_mux_flags;
  107. mux->table = table;
  108. mux->hw.init = &init;
  109. clk = ti_clk_register(dev, &mux->hw, name);
  110. if (IS_ERR(clk))
  111. kfree(mux);
  112. return clk;
  113. }
  114. struct clk *ti_clk_register_mux(struct ti_clk *setup)
  115. {
  116. struct ti_clk_mux *mux;
  117. u32 flags;
  118. u8 mux_flags = 0;
  119. struct clk_omap_reg reg;
  120. u32 mask;
  121. mux = setup->data;
  122. flags = CLK_SET_RATE_NO_REPARENT;
  123. mask = mux->num_parents;
  124. if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
  125. mask--;
  126. mask = (1 << fls(mask)) - 1;
  127. reg.index = mux->module;
  128. reg.offset = mux->reg;
  129. reg.ptr = NULL;
  130. if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
  131. mux_flags |= CLK_MUX_INDEX_ONE;
  132. if (mux->flags & CLKF_SET_RATE_PARENT)
  133. flags |= CLK_SET_RATE_PARENT;
  134. return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
  135. flags, &reg, mux->bit_shift, mask, -EINVAL,
  136. mux_flags, NULL);
  137. }
  138. /**
  139. * of_mux_clk_setup - Setup function for simple mux rate clock
  140. * @node: DT node for the clock
  141. *
  142. * Sets up a basic clock multiplexer.
  143. */
  144. static void of_mux_clk_setup(struct device_node *node)
  145. {
  146. struct clk *clk;
  147. struct clk_omap_reg reg;
  148. unsigned int num_parents;
  149. const char **parent_names;
  150. u8 clk_mux_flags = 0;
  151. u32 mask = 0;
  152. u32 shift = 0;
  153. s32 latch = -EINVAL;
  154. u32 flags = CLK_SET_RATE_NO_REPARENT;
  155. num_parents = of_clk_get_parent_count(node);
  156. if (num_parents < 2) {
  157. pr_err("mux-clock %s must have parents\n", node->name);
  158. return;
  159. }
  160. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  161. if (!parent_names)
  162. goto cleanup;
  163. of_clk_parent_fill(node, parent_names, num_parents);
  164. if (ti_clk_get_reg_addr(node, 0, &reg))
  165. goto cleanup;
  166. of_property_read_u32(node, "ti,bit-shift", &shift);
  167. of_property_read_u32(node, "ti,latch-bit", &latch);
  168. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  169. clk_mux_flags |= CLK_MUX_INDEX_ONE;
  170. if (of_property_read_bool(node, "ti,set-rate-parent"))
  171. flags |= CLK_SET_RATE_PARENT;
  172. /* Generate bit-mask based on parent info */
  173. mask = num_parents;
  174. if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
  175. mask--;
  176. mask = (1 << fls(mask)) - 1;
  177. clk = _register_mux(NULL, node->name, parent_names, num_parents,
  178. flags, &reg, shift, mask, latch, clk_mux_flags,
  179. NULL);
  180. if (!IS_ERR(clk))
  181. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  182. cleanup:
  183. kfree(parent_names);
  184. }
  185. CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
  186. struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
  187. {
  188. struct clk_omap_mux *mux;
  189. int num_parents;
  190. if (!setup)
  191. return NULL;
  192. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  193. if (!mux)
  194. return ERR_PTR(-ENOMEM);
  195. mux->shift = setup->bit_shift;
  196. mux->latch = -EINVAL;
  197. mux->reg.index = setup->module;
  198. mux->reg.offset = setup->reg;
  199. if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
  200. mux->flags |= CLK_MUX_INDEX_ONE;
  201. num_parents = setup->num_parents;
  202. mux->mask = num_parents - 1;
  203. mux->mask = (1 << fls(mux->mask)) - 1;
  204. return &mux->hw;
  205. }
  206. static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
  207. {
  208. struct clk_omap_mux *mux;
  209. unsigned int num_parents;
  210. u32 val;
  211. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  212. if (!mux)
  213. return;
  214. if (ti_clk_get_reg_addr(node, 0, &mux->reg))
  215. goto cleanup;
  216. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  217. mux->shift = val;
  218. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  219. mux->flags |= CLK_MUX_INDEX_ONE;
  220. num_parents = of_clk_get_parent_count(node);
  221. if (num_parents < 2) {
  222. pr_err("%s must have parents\n", node->name);
  223. goto cleanup;
  224. }
  225. mux->mask = num_parents - 1;
  226. mux->mask = (1 << fls(mux->mask)) - 1;
  227. if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
  228. return;
  229. cleanup:
  230. kfree(mux);
  231. }
  232. CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
  233. of_ti_composite_mux_clk_setup);