clk-fixup-mux.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include "clk.h"
  16. #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
  17. /**
  18. * struct clk_fixup_mux - imx integer fixup multiplexer clock
  19. * @mux: the parent class
  20. * @ops: pointer to clk_ops of parent class
  21. * @fixup: a hook to fixup the write value
  22. *
  23. * The imx fixup multiplexer clock is a subclass of basic clk_mux
  24. * with an addtional fixup hook.
  25. */
  26. struct clk_fixup_mux {
  27. struct clk_mux mux;
  28. const struct clk_ops *ops;
  29. void (*fixup)(u32 *val);
  30. };
  31. static inline struct clk_fixup_mux *to_clk_fixup_mux(struct clk_hw *hw)
  32. {
  33. struct clk_mux *mux = to_clk_mux(hw);
  34. return container_of(mux, struct clk_fixup_mux, mux);
  35. }
  36. static u8 clk_fixup_mux_get_parent(struct clk_hw *hw)
  37. {
  38. struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw);
  39. return fixup_mux->ops->get_parent(&fixup_mux->mux.hw);
  40. }
  41. static int clk_fixup_mux_set_parent(struct clk_hw *hw, u8 index)
  42. {
  43. struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw);
  44. struct clk_mux *mux = to_clk_mux(hw);
  45. unsigned long flags = 0;
  46. u32 val;
  47. spin_lock_irqsave(mux->lock, flags);
  48. val = readl(mux->reg);
  49. val &= ~(mux->mask << mux->shift);
  50. val |= index << mux->shift;
  51. fixup_mux->fixup(&val);
  52. writel(val, mux->reg);
  53. spin_unlock_irqrestore(mux->lock, flags);
  54. return 0;
  55. }
  56. static const struct clk_ops clk_fixup_mux_ops = {
  57. .get_parent = clk_fixup_mux_get_parent,
  58. .set_parent = clk_fixup_mux_set_parent,
  59. };
  60. struct clk *imx_clk_fixup_mux(const char *name, void __iomem *reg,
  61. u8 shift, u8 width, const char **parents,
  62. int num_parents, void (*fixup)(u32 *val))
  63. {
  64. struct clk_fixup_mux *fixup_mux;
  65. struct clk *clk;
  66. struct clk_init_data init;
  67. if (!fixup)
  68. return ERR_PTR(-EINVAL);
  69. fixup_mux = kzalloc(sizeof(*fixup_mux), GFP_KERNEL);
  70. if (!fixup_mux)
  71. return ERR_PTR(-ENOMEM);
  72. init.name = name;
  73. init.ops = &clk_fixup_mux_ops;
  74. init.parent_names = parents;
  75. init.num_parents = num_parents;
  76. init.flags = 0;
  77. fixup_mux->mux.reg = reg;
  78. fixup_mux->mux.shift = shift;
  79. fixup_mux->mux.mask = BIT(width) - 1;
  80. fixup_mux->mux.lock = &imx_ccm_lock;
  81. fixup_mux->mux.hw.init = &init;
  82. fixup_mux->ops = &clk_mux_ops;
  83. fixup_mux->fixup = fixup;
  84. clk = clk_register(NULL, &fixup_mux->mux.hw);
  85. if (IS_ERR(clk))
  86. kfree(fixup_mux);
  87. return clk;
  88. }