clk-mux.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/err.h>
  18. /*
  19. * DOC: basic adjustable multiplexer clock that cannot gate
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_prepare only ensures that parents are prepared
  23. * enable - clk_enable only ensures that parents are enabled
  24. * rate - rate is only affected by parent switching. No clk_set_rate support
  25. * parent - parent is adjustable through clk_set_parent
  26. */
  27. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  28. static u8 clk_mux_get_parent(struct clk_hw *hw)
  29. {
  30. struct clk_mux *mux = to_clk_mux(hw);
  31. int num_parents = __clk_get_num_parents(hw->clk);
  32. u32 val;
  33. /*
  34. * FIXME need a mux-specific flag to determine if val is bitwise or numeric
  35. * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1
  36. * to 0x7 (index starts at one)
  37. * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
  38. * val = 0x4 really means "bit 2, index starts at bit 0"
  39. */
  40. val = clk_readl(mux->reg) >> mux->shift;
  41. val &= mux->mask;
  42. if (mux->table) {
  43. int i;
  44. for (i = 0; i < num_parents; i++)
  45. if (mux->table[i] == val)
  46. return i;
  47. return -EINVAL;
  48. }
  49. if (val && (mux->flags & CLK_MUX_INDEX_BIT))
  50. val = ffs(val) - 1;
  51. if (val && (mux->flags & CLK_MUX_INDEX_ONE))
  52. val--;
  53. if (val >= num_parents)
  54. return -EINVAL;
  55. return val;
  56. }
  57. static int clk_mux_set_parent(struct clk_hw *hw, u8 index)
  58. {
  59. struct clk_mux *mux = to_clk_mux(hw);
  60. u32 val;
  61. unsigned long flags = 0;
  62. if (mux->table)
  63. index = mux->table[index];
  64. else {
  65. if (mux->flags & CLK_MUX_INDEX_BIT)
  66. index = 1 << index;
  67. if (mux->flags & CLK_MUX_INDEX_ONE)
  68. index++;
  69. }
  70. if (mux->lock)
  71. spin_lock_irqsave(mux->lock, flags);
  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. return 0;
  83. }
  84. const struct clk_ops clk_mux_ops = {
  85. .get_parent = clk_mux_get_parent,
  86. .set_parent = clk_mux_set_parent,
  87. .determine_rate = __clk_mux_determine_rate,
  88. };
  89. EXPORT_SYMBOL_GPL(clk_mux_ops);
  90. const struct clk_ops clk_mux_ro_ops = {
  91. .get_parent = clk_mux_get_parent,
  92. };
  93. EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
  94. struct clk *clk_register_mux_table(struct device *dev, const char *name,
  95. const char * const *parent_names, u8 num_parents,
  96. unsigned long flags,
  97. void __iomem *reg, u8 shift, u32 mask,
  98. u8 clk_mux_flags, u32 *table, spinlock_t *lock)
  99. {
  100. struct clk_mux *mux;
  101. struct clk *clk;
  102. struct clk_init_data init;
  103. u8 width = 0;
  104. if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
  105. width = fls(mask) - ffs(mask) + 1;
  106. if (width + shift > 16) {
  107. pr_err("mux value exceeds LOWORD field\n");
  108. return ERR_PTR(-EINVAL);
  109. }
  110. }
  111. /* allocate the mux */
  112. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  113. if (!mux) {
  114. pr_err("%s: could not allocate mux clk\n", __func__);
  115. return ERR_PTR(-ENOMEM);
  116. }
  117. init.name = name;
  118. if (clk_mux_flags & CLK_MUX_READ_ONLY)
  119. init.ops = &clk_mux_ro_ops;
  120. else
  121. init.ops = &clk_mux_ops;
  122. init.flags = flags | CLK_IS_BASIC;
  123. init.parent_names = parent_names;
  124. init.num_parents = num_parents;
  125. /* struct clk_mux assignments */
  126. mux->reg = reg;
  127. mux->shift = shift;
  128. mux->mask = mask;
  129. mux->flags = clk_mux_flags;
  130. mux->lock = lock;
  131. mux->table = table;
  132. mux->hw.init = &init;
  133. clk = clk_register(dev, &mux->hw);
  134. if (IS_ERR(clk))
  135. kfree(mux);
  136. return clk;
  137. }
  138. EXPORT_SYMBOL_GPL(clk_register_mux_table);
  139. struct clk *clk_register_mux(struct device *dev, const char *name,
  140. const char * const *parent_names, u8 num_parents,
  141. unsigned long flags,
  142. void __iomem *reg, u8 shift, u8 width,
  143. u8 clk_mux_flags, spinlock_t *lock)
  144. {
  145. u32 mask = BIT(width) - 1;
  146. return clk_register_mux_table(dev, name, parent_names, num_parents,
  147. flags, reg, shift, mask, clk_mux_flags,
  148. NULL, lock);
  149. }
  150. EXPORT_SYMBOL_GPL(clk_register_mux);
  151. void clk_unregister_mux(struct clk *clk)
  152. {
  153. struct clk_mux *mux;
  154. struct clk_hw *hw;
  155. hw = __clk_get_hw(clk);
  156. if (!hw)
  157. return;
  158. mux = to_clk_mux(hw);
  159. clk_unregister(clk);
  160. kfree(mux);
  161. }
  162. EXPORT_SYMBOL_GPL(clk_unregister_mux);