clk-mux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 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 = readl(mux->reg) >> mux->shift;
  40. val &= (1 << mux->width) - 1;
  41. if (val && (mux->flags & CLK_MUX_INDEX_BIT))
  42. val = ffs(val) - 1;
  43. if (val && (mux->flags & CLK_MUX_INDEX_ONE))
  44. val--;
  45. if (val >= __clk_get_num_parents(hw->clk))
  46. return -EINVAL;
  47. return val;
  48. }
  49. EXPORT_SYMBOL_GPL(clk_mux_get_parent);
  50. static int clk_mux_set_parent(struct clk_hw *hw, u8 index)
  51. {
  52. struct clk_mux *mux = to_clk_mux(hw);
  53. u32 val;
  54. unsigned long flags = 0;
  55. if (mux->flags & CLK_MUX_INDEX_BIT)
  56. index = (1 << ffs(index));
  57. if (mux->flags & CLK_MUX_INDEX_ONE)
  58. index++;
  59. if (mux->lock)
  60. spin_lock_irqsave(mux->lock, flags);
  61. val = readl(mux->reg);
  62. val &= ~(((1 << mux->width) - 1) << mux->shift);
  63. val |= index << mux->shift;
  64. writel(val, mux->reg);
  65. if (mux->lock)
  66. spin_unlock_irqrestore(mux->lock, flags);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(clk_mux_set_parent);
  70. struct clk_ops clk_mux_ops = {
  71. .get_parent = clk_mux_get_parent,
  72. .set_parent = clk_mux_set_parent,
  73. };
  74. EXPORT_SYMBOL_GPL(clk_mux_ops);
  75. struct clk *clk_register_mux(struct device *dev, const char *name,
  76. char **parent_names, u8 num_parents, unsigned long flags,
  77. void __iomem *reg, u8 shift, u8 width,
  78. u8 clk_mux_flags, spinlock_t *lock)
  79. {
  80. struct clk_mux *mux;
  81. mux = kmalloc(sizeof(struct clk_mux), GFP_KERNEL);
  82. if (!mux) {
  83. pr_err("%s: could not allocate mux clk\n", __func__);
  84. return ERR_PTR(-ENOMEM);
  85. }
  86. /* struct clk_mux assignments */
  87. mux->reg = reg;
  88. mux->shift = shift;
  89. mux->width = width;
  90. mux->flags = clk_mux_flags;
  91. mux->lock = lock;
  92. return clk_register(dev, name, &clk_mux_ops, &mux->hw,
  93. parent_names, num_parents, flags);
  94. }