clk-mux.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  27. static u8 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 numeric
  34. * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1
  35. * 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 = 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 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 << index;
  66. if (mux->flags & CLK_MUX_INDEX_ONE)
  67. index++;
  68. }
  69. if (mux->lock)
  70. spin_lock_irqsave(mux->lock, flags);
  71. else
  72. __acquire(mux->lock);
  73. if (mux->flags & CLK_MUX_HIWORD_MASK) {
  74. val = mux->mask << (mux->shift + 16);
  75. } else {
  76. val = clk_readl(mux->reg);
  77. val &= ~(mux->mask << mux->shift);
  78. }
  79. val |= index << mux->shift;
  80. clk_writel(val, mux->reg);
  81. if (mux->lock)
  82. spin_unlock_irqrestore(mux->lock, flags);
  83. else
  84. __release(mux->lock);
  85. return 0;
  86. }
  87. const struct clk_ops clk_mux_ops = {
  88. .get_parent = clk_mux_get_parent,
  89. .set_parent = clk_mux_set_parent,
  90. .determine_rate = __clk_mux_determine_rate,
  91. };
  92. EXPORT_SYMBOL_GPL(clk_mux_ops);
  93. const struct clk_ops clk_mux_ro_ops = {
  94. .get_parent = clk_mux_get_parent,
  95. };
  96. EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
  97. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  98. const char * const *parent_names, u8 num_parents,
  99. unsigned long flags,
  100. void __iomem *reg, u8 shift, u32 mask,
  101. u8 clk_mux_flags, u32 *table, spinlock_t *lock)
  102. {
  103. struct clk_mux *mux;
  104. struct clk *clk;
  105. struct clk_init_data init;
  106. u8 width = 0;
  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. clk = clk_register(dev, &mux->hw);
  137. if (IS_ERR(clk))
  138. kfree(mux);
  139. return clk;
  140. }
  141. EXPORT_SYMBOL_GPL(clk_register_mux_table);
  142. struct clk *clk_register_mux(struct device *dev, const char *name,
  143. const char * const *parent_names, u8 num_parents,
  144. unsigned long flags,
  145. void __iomem *reg, u8 shift, u8 width,
  146. u8 clk_mux_flags, spinlock_t *lock)
  147. {
  148. u32 mask = BIT(width) - 1;
  149. return clk_register_mux_table(dev, name, parent_names, num_parents,
  150. flags, reg, shift, mask, clk_mux_flags,
  151. NULL, lock);
  152. }
  153. EXPORT_SYMBOL_GPL(clk_register_mux);
  154. void clk_unregister_mux(struct clk *clk)
  155. {
  156. struct clk_mux *mux;
  157. struct clk_hw *hw;
  158. hw = __clk_get_hw(clk);
  159. if (!hw)
  160. return;
  161. mux = to_clk_mux(hw);
  162. clk_unregister(clk);
  163. kfree(mux);
  164. }
  165. EXPORT_SYMBOL_GPL(clk_unregister_mux);