clk-fixup-div.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_div(_hw) container_of(_hw, struct clk_divider, hw)
  17. #define div_mask(d) ((1 << (d->width)) - 1)
  18. /**
  19. * struct clk_fixup_div - imx integer fixup divider clock
  20. * @divider: the parent class
  21. * @ops: pointer to clk_ops of parent class
  22. * @fixup: a hook to fixup the write value
  23. *
  24. * The imx fixup divider clock is a subclass of basic clk_divider
  25. * with an addtional fixup hook.
  26. */
  27. struct clk_fixup_div {
  28. struct clk_divider divider;
  29. const struct clk_ops *ops;
  30. void (*fixup)(u32 *val);
  31. };
  32. static inline struct clk_fixup_div *to_clk_fixup_div(struct clk_hw *hw)
  33. {
  34. struct clk_divider *divider = to_clk_div(hw);
  35. return container_of(divider, struct clk_fixup_div, divider);
  36. }
  37. static unsigned long clk_fixup_div_recalc_rate(struct clk_hw *hw,
  38. unsigned long parent_rate)
  39. {
  40. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  41. return fixup_div->ops->recalc_rate(&fixup_div->divider.hw, parent_rate);
  42. }
  43. static long clk_fixup_div_round_rate(struct clk_hw *hw, unsigned long rate,
  44. unsigned long *prate)
  45. {
  46. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  47. return fixup_div->ops->round_rate(&fixup_div->divider.hw, rate, prate);
  48. }
  49. static int clk_fixup_div_set_rate(struct clk_hw *hw, unsigned long rate,
  50. unsigned long parent_rate)
  51. {
  52. struct clk_fixup_div *fixup_div = to_clk_fixup_div(hw);
  53. struct clk_divider *div = to_clk_div(hw);
  54. unsigned int divider, value;
  55. unsigned long flags = 0;
  56. u32 val;
  57. divider = parent_rate / rate;
  58. /* Zero based divider */
  59. value = divider - 1;
  60. if (value > div_mask(div))
  61. value = div_mask(div);
  62. spin_lock_irqsave(div->lock, flags);
  63. val = readl(div->reg);
  64. val &= ~(div_mask(div) << div->shift);
  65. val |= value << div->shift;
  66. fixup_div->fixup(&val);
  67. writel(val, div->reg);
  68. spin_unlock_irqrestore(div->lock, flags);
  69. return 0;
  70. }
  71. static const struct clk_ops clk_fixup_div_ops = {
  72. .recalc_rate = clk_fixup_div_recalc_rate,
  73. .round_rate = clk_fixup_div_round_rate,
  74. .set_rate = clk_fixup_div_set_rate,
  75. };
  76. struct clk *imx_clk_fixup_divider(const char *name, const char *parent,
  77. void __iomem *reg, u8 shift, u8 width,
  78. void (*fixup)(u32 *val))
  79. {
  80. struct clk_fixup_div *fixup_div;
  81. struct clk *clk;
  82. struct clk_init_data init;
  83. if (!fixup)
  84. return ERR_PTR(-EINVAL);
  85. fixup_div = kzalloc(sizeof(*fixup_div), GFP_KERNEL);
  86. if (!fixup_div)
  87. return ERR_PTR(-ENOMEM);
  88. init.name = name;
  89. init.ops = &clk_fixup_div_ops;
  90. init.flags = CLK_SET_RATE_PARENT;
  91. init.parent_names = parent ? &parent : NULL;
  92. init.num_parents = parent ? 1 : 0;
  93. fixup_div->divider.reg = reg;
  94. fixup_div->divider.shift = shift;
  95. fixup_div->divider.width = width;
  96. fixup_div->divider.lock = &imx_ccm_lock;
  97. fixup_div->divider.hw.init = &init;
  98. fixup_div->ops = &clk_divider_ops;
  99. fixup_div->fixup = fixup;
  100. clk = clk_register(NULL, &fixup_div->divider.hw);
  101. if (IS_ERR(clk))
  102. kfree(fixup_div);
  103. return clk;
  104. }