mux.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #undef pr_fmt
  24. #define pr_fmt(fmt) "%s: " fmt, __func__
  25. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  26. static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
  27. {
  28. struct clk_mux *mux = to_clk_mux(hw);
  29. int num_parents = __clk_get_num_parents(hw->clk);
  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_mux *mux = to_clk_mux(hw);
  58. u32 val;
  59. unsigned long flags = 0;
  60. if (mux->table) {
  61. index = mux->table[index];
  62. } else {
  63. if (mux->flags & CLK_MUX_INDEX_BIT)
  64. index = (1 << ffs(index));
  65. if (mux->flags & CLK_MUX_INDEX_ONE)
  66. index++;
  67. }
  68. if (mux->lock)
  69. spin_lock_irqsave(mux->lock, flags);
  70. if (mux->flags & CLK_MUX_HIWORD_MASK) {
  71. val = mux->mask << (mux->shift + 16);
  72. } else {
  73. val = ti_clk_ll_ops->clk_readl(mux->reg);
  74. val &= ~(mux->mask << mux->shift);
  75. }
  76. val |= index << mux->shift;
  77. ti_clk_ll_ops->clk_writel(val, mux->reg);
  78. if (mux->lock)
  79. spin_unlock_irqrestore(mux->lock, flags);
  80. return 0;
  81. }
  82. const struct clk_ops ti_clk_mux_ops = {
  83. .get_parent = ti_clk_mux_get_parent,
  84. .set_parent = ti_clk_mux_set_parent,
  85. .determine_rate = __clk_mux_determine_rate,
  86. };
  87. static struct clk *_register_mux(struct device *dev, const char *name,
  88. const char **parent_names, u8 num_parents,
  89. unsigned long flags, void __iomem *reg,
  90. u8 shift, u32 mask, u8 clk_mux_flags,
  91. u32 *table, spinlock_t *lock)
  92. {
  93. struct clk_mux *mux;
  94. struct clk *clk;
  95. struct clk_init_data init;
  96. /* allocate the mux */
  97. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  98. if (!mux) {
  99. pr_err("%s: could not allocate mux clk\n", __func__);
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. init.name = name;
  103. init.ops = &ti_clk_mux_ops;
  104. init.flags = flags | CLK_IS_BASIC;
  105. init.parent_names = parent_names;
  106. init.num_parents = num_parents;
  107. /* struct clk_mux assignments */
  108. mux->reg = reg;
  109. mux->shift = shift;
  110. mux->mask = mask;
  111. mux->flags = clk_mux_flags;
  112. mux->lock = lock;
  113. mux->table = table;
  114. mux->hw.init = &init;
  115. clk = clk_register(dev, &mux->hw);
  116. if (IS_ERR(clk))
  117. kfree(mux);
  118. return clk;
  119. }
  120. /**
  121. * of_mux_clk_setup - Setup function for simple mux rate clock
  122. * @node: DT node for the clock
  123. *
  124. * Sets up a basic clock multiplexer.
  125. */
  126. static void of_mux_clk_setup(struct device_node *node)
  127. {
  128. struct clk *clk;
  129. void __iomem *reg;
  130. int num_parents;
  131. const char **parent_names;
  132. int i;
  133. u8 clk_mux_flags = 0;
  134. u32 mask = 0;
  135. u32 shift = 0;
  136. u32 flags = CLK_SET_RATE_NO_REPARENT;
  137. num_parents = of_clk_get_parent_count(node);
  138. if (num_parents < 2) {
  139. pr_err("mux-clock %s must have parents\n", node->name);
  140. return;
  141. }
  142. parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
  143. if (!parent_names)
  144. goto cleanup;
  145. for (i = 0; i < num_parents; i++)
  146. parent_names[i] = of_clk_get_parent_name(node, i);
  147. reg = ti_clk_get_reg_addr(node, 0);
  148. if (!reg)
  149. goto cleanup;
  150. of_property_read_u32(node, "ti,bit-shift", &shift);
  151. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  152. clk_mux_flags |= CLK_MUX_INDEX_ONE;
  153. if (of_property_read_bool(node, "ti,set-rate-parent"))
  154. flags |= CLK_SET_RATE_PARENT;
  155. /* Generate bit-mask based on parent info */
  156. mask = num_parents;
  157. if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
  158. mask--;
  159. mask = (1 << fls(mask)) - 1;
  160. clk = _register_mux(NULL, node->name, parent_names, num_parents, flags,
  161. reg, shift, mask, clk_mux_flags, NULL, NULL);
  162. if (!IS_ERR(clk))
  163. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  164. cleanup:
  165. kfree(parent_names);
  166. }
  167. CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
  168. static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
  169. {
  170. struct clk_mux *mux;
  171. int num_parents;
  172. u32 val;
  173. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  174. if (!mux)
  175. return;
  176. mux->reg = ti_clk_get_reg_addr(node, 0);
  177. if (!mux->reg)
  178. goto cleanup;
  179. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  180. mux->shift = val;
  181. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  182. mux->flags |= CLK_MUX_INDEX_ONE;
  183. num_parents = of_clk_get_parent_count(node);
  184. if (num_parents < 2) {
  185. pr_err("%s must have parents\n", node->name);
  186. goto cleanup;
  187. }
  188. mux->mask = num_parents - 1;
  189. mux->mask = (1 << fls(mux->mask)) - 1;
  190. if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
  191. return;
  192. cleanup:
  193. kfree(mux);
  194. }
  195. CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
  196. of_ti_composite_mux_clk_setup);