clk-fixup-mux.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. .determine_rate = clk_hw_determine_rate_no_reparent,
  52. .get_parent = clk_fixup_mux_get_parent,
  53. .set_parent = clk_fixup_mux_set_parent,
  54. };
  55. struct clk_hw *imx_clk_hw_fixup_mux(const char *name, void __iomem *reg,
  56. u8 shift, u8 width, const char * const *parents,
  57. int num_parents, void (*fixup)(u32 *val))
  58. {
  59. struct clk_fixup_mux *fixup_mux;
  60. struct clk_hw *hw;
  61. struct clk_init_data init;
  62. int ret;
  63. if (!fixup)
  64. return ERR_PTR(-EINVAL);
  65. fixup_mux = kzalloc(sizeof(*fixup_mux), GFP_KERNEL);
  66. if (!fixup_mux)
  67. return ERR_PTR(-ENOMEM);
  68. init.name = name;
  69. init.ops = &clk_fixup_mux_ops;
  70. init.parent_names = parents;
  71. init.num_parents = num_parents;
  72. init.flags = 0;
  73. fixup_mux->mux.reg = reg;
  74. fixup_mux->mux.shift = shift;
  75. fixup_mux->mux.mask = BIT(width) - 1;
  76. fixup_mux->mux.lock = &imx_ccm_lock;
  77. fixup_mux->mux.hw.init = &init;
  78. fixup_mux->ops = &clk_mux_ops;
  79. fixup_mux->fixup = fixup;
  80. hw = &fixup_mux->mux.hw;
  81. ret = clk_hw_register(NULL, hw);
  82. if (ret) {
  83. kfree(fixup_mux);
  84. return ERR_PTR(ret);
  85. }
  86. return hw;
  87. }