clk-fixup-div.c 3.2 KB

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