clk-div.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/err.h>
  7. #include <linux/slab.h>
  8. #include "clk.h"
  9. /**
  10. * struct clk_div - mxs integer divider clock
  11. * @divider: the parent class
  12. * @ops: pointer to clk_ops of parent class
  13. * @reg: register address
  14. * @busy: busy bit shift
  15. *
  16. * The mxs divider clock is a subclass of basic clk_divider with an
  17. * addtional busy bit.
  18. */
  19. struct clk_div {
  20. struct clk_divider divider;
  21. const struct clk_ops *ops;
  22. void __iomem *reg;
  23. u8 busy;
  24. };
  25. static inline struct clk_div *to_clk_div(struct clk_hw *hw)
  26. {
  27. struct clk_divider *divider = to_clk_divider(hw);
  28. return container_of(divider, struct clk_div, divider);
  29. }
  30. static unsigned long clk_div_recalc_rate(struct clk_hw *hw,
  31. unsigned long parent_rate)
  32. {
  33. struct clk_div *div = to_clk_div(hw);
  34. return div->ops->recalc_rate(&div->divider.hw, parent_rate);
  35. }
  36. static long clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  37. unsigned long *prate)
  38. {
  39. struct clk_div *div = to_clk_div(hw);
  40. return div->ops->round_rate(&div->divider.hw, rate, prate);
  41. }
  42. static int clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  43. unsigned long parent_rate)
  44. {
  45. struct clk_div *div = to_clk_div(hw);
  46. int ret;
  47. ret = div->ops->set_rate(&div->divider.hw, rate, parent_rate);
  48. if (!ret)
  49. ret = mxs_clk_wait(div->reg, div->busy);
  50. return ret;
  51. }
  52. static const struct clk_ops clk_div_ops = {
  53. .recalc_rate = clk_div_recalc_rate,
  54. .round_rate = clk_div_round_rate,
  55. .set_rate = clk_div_set_rate,
  56. };
  57. struct clk *mxs_clk_div(const char *name, const char *parent_name,
  58. void __iomem *reg, u8 shift, u8 width, u8 busy)
  59. {
  60. struct clk_div *div;
  61. struct clk *clk;
  62. struct clk_init_data init;
  63. div = kzalloc(sizeof(*div), GFP_KERNEL);
  64. if (!div)
  65. return ERR_PTR(-ENOMEM);
  66. init.name = name;
  67. init.ops = &clk_div_ops;
  68. init.flags = CLK_SET_RATE_PARENT;
  69. init.parent_names = (parent_name ? &parent_name: NULL);
  70. init.num_parents = (parent_name ? 1 : 0);
  71. div->reg = reg;
  72. div->busy = busy;
  73. div->divider.reg = reg;
  74. div->divider.shift = shift;
  75. div->divider.width = width;
  76. div->divider.flags = CLK_DIVIDER_ONE_BASED;
  77. div->divider.lock = &mxs_lock;
  78. div->divider.hw.init = &init;
  79. div->ops = &clk_divider_ops;
  80. clk = clk_register(NULL, &div->divider.hw);
  81. if (IS_ERR(clk))
  82. kfree(div);
  83. return clk;
  84. }