clk-mux.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  3. * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Simple multiplexer clock implementation
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/err.h>
  17. /*
  18. * DOC: basic adjustable multiplexer clock that cannot gate
  19. *
  20. * Traits of this clock:
  21. * prepare - clk_prepare only ensures that parents are prepared
  22. * enable - clk_enable only ensures that parents are enabled
  23. * rate - rate is only affected by parent switching. No clk_set_rate support
  24. * parent - parent is adjustable through clk_set_parent
  25. */
  26. static u8 clk_mux_get_parent(struct clk_hw *hw)
  27. {
  28. struct clk_mux *mux = to_clk_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 numeric
  33. * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1
  34. * 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 = 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 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 << index;
  65. if (mux->flags & CLK_MUX_INDEX_ONE)
  66. index++;
  67. }
  68. if (mux->lock)
  69. spin_lock_irqsave(mux->lock, flags);
  70. else
  71. __acquire(mux->lock);
  72. if (mux->flags & CLK_MUX_HIWORD_MASK) {
  73. val = mux->mask << (mux->shift + 16);
  74. } else {
  75. val = clk_readl(mux->reg);
  76. val &= ~(mux->mask << mux->shift);
  77. }
  78. val |= index << mux->shift;
  79. clk_writel(val, mux->reg);
  80. if (mux->lock)
  81. spin_unlock_irqrestore(mux->lock, flags);
  82. else
  83. __release(mux->lock);
  84. return 0;
  85. }
  86. const struct clk_ops clk_mux_ops = {
  87. .get_parent = clk_mux_get_parent,
  88. .set_parent = clk_mux_set_parent,
  89. .determine_rate = __clk_mux_determine_rate,
  90. };
  91. EXPORT_SYMBOL_GPL(clk_mux_ops);
  92. const struct clk_ops clk_mux_ro_ops = {
  93. .get_parent = clk_mux_get_parent,
  94. };
  95. EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
  96. struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
  97. const char * const *parent_names, u8 num_parents,
  98. unsigned long flags,
  99. void __iomem *reg, u8 shift, u32 mask,
  100. u8 clk_mux_flags, u32 *table, spinlock_t *lock)
  101. {
  102. struct clk_mux *mux;
  103. struct clk_hw *hw;
  104. struct clk_init_data init;
  105. u8 width = 0;
  106. int ret;
  107. if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
  108. width = fls(mask) - ffs(mask) + 1;
  109. if (width + shift > 16) {
  110. pr_err("mux value exceeds LOWORD field\n");
  111. return ERR_PTR(-EINVAL);
  112. }
  113. }
  114. /* allocate the mux */
  115. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  116. if (!mux) {
  117. pr_err("%s: could not allocate mux clk\n", __func__);
  118. return ERR_PTR(-ENOMEM);
  119. }
  120. init.name = name;
  121. if (clk_mux_flags & CLK_MUX_READ_ONLY)
  122. init.ops = &clk_mux_ro_ops;
  123. else
  124. init.ops = &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. mux->reg = reg;
  130. mux->shift = shift;
  131. mux->mask = mask;
  132. mux->flags = clk_mux_flags;
  133. mux->lock = lock;
  134. mux->table = table;
  135. mux->hw.init = &init;
  136. hw = &mux->hw;
  137. ret = clk_hw_register(dev, hw);
  138. if (ret) {
  139. kfree(mux);
  140. hw = ERR_PTR(ret);
  141. }
  142. return hw;
  143. }
  144. EXPORT_SYMBOL_GPL(clk_hw_register_mux_table);
  145. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  146. const char * const *parent_names, u8 num_parents,
  147. unsigned long flags,
  148. void __iomem *reg, u8 shift, u32 mask,
  149. u8 clk_mux_flags, u32 *table, spinlock_t *lock)
  150. {
  151. struct clk_hw *hw;
  152. hw = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
  153. flags, reg, shift, mask, clk_mux_flags,
  154. table, lock);
  155. if (IS_ERR(hw))
  156. return ERR_CAST(hw);
  157. return hw->clk;
  158. }
  159. EXPORT_SYMBOL_GPL(clk_register_mux_table);
  160. struct clk *clk_register_mux(struct device *dev, const char *name,
  161. const char * const *parent_names, u8 num_parents,
  162. unsigned long flags,
  163. void __iomem *reg, u8 shift, u8 width,
  164. u8 clk_mux_flags, spinlock_t *lock)
  165. {
  166. u32 mask = BIT(width) - 1;
  167. return clk_register_mux_table(dev, name, parent_names, num_parents,
  168. flags, reg, shift, mask, clk_mux_flags,
  169. NULL, lock);
  170. }
  171. EXPORT_SYMBOL_GPL(clk_register_mux);
  172. struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
  173. const char * const *parent_names, u8 num_parents,
  174. unsigned long flags,
  175. void __iomem *reg, u8 shift, u8 width,
  176. u8 clk_mux_flags, spinlock_t *lock)
  177. {
  178. u32 mask = BIT(width) - 1;
  179. return clk_hw_register_mux_table(dev, name, parent_names, num_parents,
  180. flags, reg, shift, mask, clk_mux_flags,
  181. NULL, lock);
  182. }
  183. EXPORT_SYMBOL_GPL(clk_hw_register_mux);
  184. void clk_unregister_mux(struct clk *clk)
  185. {
  186. struct clk_mux *mux;
  187. struct clk_hw *hw;
  188. hw = __clk_get_hw(clk);
  189. if (!hw)
  190. return;
  191. mux = to_clk_mux(hw);
  192. clk_unregister(clk);
  193. kfree(mux);
  194. }
  195. EXPORT_SYMBOL_GPL(clk_unregister_mux);
  196. void clk_hw_unregister_mux(struct clk_hw *hw)
  197. {
  198. struct clk_mux *mux;
  199. mux = to_clk_mux(hw);
  200. clk_hw_unregister(hw);
  201. kfree(mux);
  202. }
  203. EXPORT_SYMBOL_GPL(clk_hw_unregister_mux);