clk-fixup-div.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/err.h>
  7. #include <linux/io.h>
  8. #include <linux/slab.h>
  9. #include "clk.h"
  10. #define div_mask(d) ((1 << (d->width)) - 1)
  11. /**
  12. * struct clk_fixup_div - imx integer fixup divider clock
  13. * @divider: 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 divider clock is a subclass of basic clk_divider
  18. * with an addtional fixup hook.
  19. */
  20. struct clk_fixup_div {
  21. struct clk_divider divider;
  22. const struct clk_ops *ops;
  23. void (*fixup)(u32 *val);
  24. };
  25. static inline struct clk_fixup_div *to_clk_fixup_div(struct clk_hw *hw)
  26. {
  27. struct clk_divider *divider = to_clk_divider(hw);
  28. return container_of(divider, struct clk_fixup_div, divider);
  29. }
  30. static unsigned long clk_fixup_div_recalc_rate(struct clk_hw *hw,
  31. unsigned long parent_rate)
  32. {
  33. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  34. return fixup_div->ops->recalc_rate(&fixup_div->divider.hw, parent_rate);
  35. }
  36. static long clk_fixup_div_round_rate(struct clk_hw *hw, unsigned long rate,
  37. unsigned long *prate)
  38. {
  39. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  40. return fixup_div->ops->round_rate(&fixup_div->divider.hw, rate, prate);
  41. }
  42. static int clk_fixup_div_set_rate(struct clk_hw *hw, unsigned long rate,
  43. unsigned long parent_rate)
  44. {
  45. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  46. struct clk_divider *div = to_clk_divider(hw);
  47. unsigned int divider, value;
  48. unsigned long flags;
  49. u32 val;
  50. divider = parent_rate / rate;
  51. /* Zero based divider */
  52. value = divider - 1;
  53. if (value > div_mask(div))
  54. value = div_mask(div);
  55. spin_lock_irqsave(div->lock, flags);
  56. val = readl(div->reg);
  57. val &= ~(div_mask(div) << div->shift);
  58. val |= value << div->shift;
  59. fixup_div->fixup(&val);
  60. writel(val, div->reg);
  61. spin_unlock_irqrestore(div->lock, flags);
  62. return 0;
  63. }
  64. static const struct clk_ops clk_fixup_div_ops = {
  65. .recalc_rate = clk_fixup_div_recalc_rate,
  66. .round_rate = clk_fixup_div_round_rate,
  67. .set_rate = clk_fixup_div_set_rate,
  68. };
  69. struct clk_hw *imx_clk_hw_fixup_divider(const char *name, const char *parent,
  70. void __iomem *reg, u8 shift, u8 width,
  71. void (*fixup)(u32 *val))
  72. {
  73. struct clk_fixup_div *fixup_div;
  74. struct clk_hw *hw;
  75. struct clk_init_data init;
  76. int ret;
  77. if (!fixup)
  78. return ERR_PTR(-EINVAL);
  79. fixup_div = kzalloc(sizeof(*fixup_div), GFP_KERNEL);
  80. if (!fixup_div)
  81. return ERR_PTR(-ENOMEM);
  82. init.name = name;
  83. init.ops = &clk_fixup_div_ops;
  84. init.flags = CLK_SET_RATE_PARENT;
  85. init.parent_names = parent ? &parent : NULL;
  86. init.num_parents = parent ? 1 : 0;
  87. fixup_div->divider.reg = reg;
  88. fixup_div->divider.shift = shift;
  89. fixup_div->divider.width = width;
  90. fixup_div->divider.lock = &imx_ccm_lock;
  91. fixup_div->divider.hw.init = &init;
  92. fixup_div->ops = &clk_divider_ops;
  93. fixup_div->fixup = fixup;
  94. hw = &fixup_div->divider.hw;
  95. ret = clk_hw_register(NULL, hw);
  96. if (ret) {
  97. kfree(fixup_div);
  98. return ERR_PTR(ret);
  99. }
  100. return hw;
  101. }