clk-fixup-mux.c 2.4 KB

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