clk-fixup-mux.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  17. * struct clk_fixup_mux - imx integer fixup multiplexer clock
  18. * @mux: the parent class
  19. * @ops: pointer to clk_ops of parent class
  20. * @fixup: a hook to fixup the write value
  21. *
  22. * The imx fixup multiplexer clock is a subclass of basic clk_mux
  23. * with an addtional fixup hook.
  24. */
  25. struct clk_fixup_mux {
  26. struct clk_mux mux;
  27. const struct clk_ops *ops;
  28. void (*fixup)(u32 *val);
  29. };
  30. static inline struct clk_fixup_mux *to_clk_fixup_mux(struct clk_hw *hw)
  31. {
  32. struct clk_mux *mux = to_clk_mux(hw);
  33. return container_of(mux, struct clk_fixup_mux, mux);
  34. }
  35. static u8 clk_fixup_mux_get_parent(struct clk_hw *hw)
  36. {
  37. struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw);
  38. return fixup_mux->ops->get_parent(&fixup_mux->mux.hw);
  39. }
  40. static int clk_fixup_mux_set_parent(struct clk_hw *hw, u8 index)
  41. {
  42. struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw);
  43. struct clk_mux *mux = to_clk_mux(hw);
  44. unsigned long flags = 0;
  45. u32 val;
  46. spin_lock_irqsave(mux->lock, flags);
  47. val = readl(mux->reg);
  48. val &= ~(mux->mask << mux->shift);
  49. val |= index << mux->shift;
  50. fixup_mux->fixup(&val);
  51. writel(val, mux->reg);
  52. spin_unlock_irqrestore(mux->lock, flags);
  53. return 0;
  54. }
  55. static const struct clk_ops clk_fixup_mux_ops = {
  56. .get_parent = clk_fixup_mux_get_parent,
  57. .set_parent = clk_fixup_mux_set_parent,
  58. };
  59. struct clk *imx_clk_fixup_mux(const char *name, void __iomem *reg,
  60. u8 shift, u8 width, const char **parents,
  61. int num_parents, void (*fixup)(u32 *val))
  62. {
  63. struct clk_fixup_mux *fixup_mux;
  64. struct clk *clk;
  65. struct clk_init_data init;
  66. if (!fixup)
  67. return ERR_PTR(-EINVAL);
  68. fixup_mux = kzalloc(sizeof(*fixup_mux), GFP_KERNEL);
  69. if (!fixup_mux)
  70. return ERR_PTR(-ENOMEM);
  71. init.name = name;
  72. init.ops = &clk_fixup_mux_ops;
  73. init.parent_names = parents;
  74. init.num_parents = num_parents;
  75. init.flags = 0;
  76. fixup_mux->mux.reg = reg;
  77. fixup_mux->mux.shift = shift;
  78. fixup_mux->mux.mask = BIT(width) - 1;
  79. fixup_mux->mux.lock = &imx_ccm_lock;
  80. fixup_mux->mux.hw.init = &init;
  81. fixup_mux->ops = &clk_mux_ops;
  82. fixup_mux->fixup = fixup;
  83. clk = clk_register(NULL, &fixup_mux->mux.hw);
  84. if (IS_ERR(clk))
  85. kfree(fixup_mux);
  86. return clk;
  87. }