mux.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  27. static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
  28. {
  29. struct clk_mux *mux = to_clk_mux(hw);
  30. int num_parents = clk_hw_get_num_parents(hw);
  31. u32 val;
  32. /*
  33. * FIXME need a mux-specific flag to determine if val is bitwise or
  34. * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
  35. * from 0x1 to 0x7 (index starts at one)
  36. * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
  37. * val = 0x4 really means "bit 2, index starts at bit 0"
  38. */
  39. val = ti_clk_ll_ops->clk_readl(mux->reg) >> mux->shift;
  40. val &= mux->mask;
  41. if (mux->table) {
  42. int i;
  43. for (i = 0; i < num_parents; i++)
  44. if (mux->table[i] == val)
  45. return i;
  46. return -EINVAL;
  47. }
  48. if (val && (mux->flags & CLK_MUX_INDEX_BIT))
  49. val = ffs(val) - 1;
  50. if (val && (mux->flags & CLK_MUX_INDEX_ONE))
  51. val--;
  52. if (val >= num_parents)
  53. return -EINVAL;
  54. return val;
  55. }
  56. static int ti_clk_mux_set_parent(struct clk_hw *hw, u8 index)
  57. {
  58. struct clk_mux *mux = to_clk_mux(hw);
  59. u32 val;
  60. unsigned long flags = 0;
  61. if (mux->table) {
  62. index = mux->table[index];
  63. } else {
  64. if (mux->flags & CLK_MUX_INDEX_BIT)
  65. index = (1 << ffs(index));
  66. if (mux->flags & CLK_MUX_INDEX_ONE)
  67. index++;
  68. }
  69. if (mux->lock)
  70. spin_lock_irqsave(mux->lock, flags);
  71. if (mux->flags & CLK_MUX_HIWORD_MASK) {
  72. val = mux->mask << (mux->shift + 16);
  73. } else {
  74. val = ti_clk_ll_ops->clk_readl(mux->reg);
  75. val &= ~(mux->mask << mux->shift);
  76. }
  77. val |= index << mux->shift;
  78. ti_clk_ll_ops->clk_writel(val, mux->reg);
  79. if (mux->lock)
  80. spin_unlock_irqrestore(mux->lock, flags);
  81. return 0;
  82. }
  83. const struct clk_ops ti_clk_mux_ops = {
  84. .get_parent = ti_clk_mux_get_parent,
  85. .set_parent = ti_clk_mux_set_parent,
  86. .determine_rate = __clk_mux_determine_rate,
  87. };
  88. static struct clk *_register_mux(struct device *dev, const char *name,
  89. const char **parent_names, u8 num_parents,
  90. unsigned long flags, void __iomem *reg,
  91. u8 shift, u32 mask, u8 clk_mux_flags,
  92. u32 *table, spinlock_t *lock)
  93. {
  94. struct clk_mux *mux;
  95. struct clk *clk;
  96. struct clk_init_data init;
  97. /* allocate the mux */
  98. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  99. if (!mux) {
  100. pr_err("%s: could not allocate mux clk\n", __func__);
  101. return ERR_PTR(-ENOMEM);
  102. }
  103. init.name = name;
  104. init.ops = &ti_clk_mux_ops;
  105. init.flags = flags | CLK_IS_BASIC;
  106. init.parent_names = parent_names;
  107. init.num_parents = num_parents;
  108. /* struct clk_mux assignments */
  109. mux->reg = reg;
  110. mux->shift = shift;
  111. mux->mask = mask;
  112. mux->flags = clk_mux_flags;
  113. mux->lock = lock;
  114. mux->table = table;
  115. mux->hw.init = &init;
  116. clk = clk_register(dev, &mux->hw);
  117. if (IS_ERR(clk))
  118. kfree(mux);
  119. return clk;
  120. }
  121. struct clk *ti_clk_register_mux(struct ti_clk *setup)
  122. {
  123. struct ti_clk_mux *mux;
  124. u32 flags;
  125. u8 mux_flags = 0;
  126. struct clk_omap_reg *reg_setup;
  127. u32 reg;
  128. u32 mask;
  129. reg_setup = (struct clk_omap_reg *)&reg;
  130. mux = setup->data;
  131. flags = CLK_SET_RATE_NO_REPARENT;
  132. mask = mux->num_parents;
  133. if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
  134. mask--;
  135. mask = (1 << fls(mask)) - 1;
  136. reg_setup->index = mux->module;
  137. reg_setup->offset = mux->reg;
  138. if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
  139. mux_flags |= CLK_MUX_INDEX_ONE;
  140. if (mux->flags & CLKF_SET_RATE_PARENT)
  141. flags |= CLK_SET_RATE_PARENT;
  142. return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
  143. flags, (void __iomem *)reg, mux->bit_shift, mask,
  144. mux_flags, NULL, NULL);
  145. }
  146. /**
  147. * of_mux_clk_setup - Setup function for simple mux rate clock
  148. * @node: DT node for the clock
  149. *
  150. * Sets up a basic clock multiplexer.
  151. */
  152. static void of_mux_clk_setup(struct device_node *node)
  153. {
  154. struct clk *clk;
  155. void __iomem *reg;
  156. int num_parents;
  157. const char **parent_names;
  158. u8 clk_mux_flags = 0;
  159. u32 mask = 0;
  160. u32 shift = 0;
  161. u32 flags = CLK_SET_RATE_NO_REPARENT;
  162. num_parents = of_clk_get_parent_count(node);
  163. if (num_parents < 2) {
  164. pr_err("mux-clock %s must have parents\n", node->name);
  165. return;
  166. }
  167. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  168. if (!parent_names)
  169. goto cleanup;
  170. of_clk_parent_fill(node, parent_names, num_parents);
  171. reg = ti_clk_get_reg_addr(node, 0);
  172. if (IS_ERR(reg))
  173. goto cleanup;
  174. of_property_read_u32(node, "ti,bit-shift", &shift);
  175. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  176. clk_mux_flags |= CLK_MUX_INDEX_ONE;
  177. if (of_property_read_bool(node, "ti,set-rate-parent"))
  178. flags |= CLK_SET_RATE_PARENT;
  179. /* Generate bit-mask based on parent info */
  180. mask = num_parents;
  181. if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
  182. mask--;
  183. mask = (1 << fls(mask)) - 1;
  184. clk = _register_mux(NULL, node->name, parent_names, num_parents,
  185. flags, reg, shift, mask, clk_mux_flags, NULL,
  186. NULL);
  187. if (!IS_ERR(clk))
  188. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  189. cleanup:
  190. kfree(parent_names);
  191. }
  192. CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
  193. struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
  194. {
  195. struct clk_mux *mux;
  196. struct clk_omap_reg *reg;
  197. int num_parents;
  198. if (!setup)
  199. return NULL;
  200. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  201. if (!mux)
  202. return ERR_PTR(-ENOMEM);
  203. reg = (struct clk_omap_reg *)&mux->reg;
  204. mux->shift = setup->bit_shift;
  205. reg->index = setup->module;
  206. reg->offset = setup->reg;
  207. if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
  208. mux->flags |= CLK_MUX_INDEX_ONE;
  209. num_parents = setup->num_parents;
  210. mux->mask = num_parents - 1;
  211. mux->mask = (1 << fls(mux->mask)) - 1;
  212. return &mux->hw;
  213. }
  214. static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
  215. {
  216. struct clk_mux *mux;
  217. int num_parents;
  218. u32 val;
  219. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  220. if (!mux)
  221. return;
  222. mux->reg = ti_clk_get_reg_addr(node, 0);
  223. if (IS_ERR(mux->reg))
  224. goto cleanup;
  225. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  226. mux->shift = val;
  227. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  228. mux->flags |= CLK_MUX_INDEX_ONE;
  229. num_parents = of_clk_get_parent_count(node);
  230. if (num_parents < 2) {
  231. pr_err("%s must have parents\n", node->name);
  232. goto cleanup;
  233. }
  234. mux->mask = num_parents - 1;
  235. mux->mask = (1 << fls(mux->mask)) - 1;
  236. if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
  237. return;
  238. cleanup:
  239. kfree(mux);
  240. }
  241. CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
  242. of_ti_composite_mux_clk_setup);