clk-regmap-divider.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, 2017, 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bitops.h>
  7. #include <linux/regmap.h>
  8. #include <linux/export.h>
  9. #include "clk-regmap-divider.h"
  10. #include "clk-debug.h"
  11. static inline struct clk_regmap_div *to_clk_regmap_div(struct clk_hw *hw)
  12. {
  13. return container_of(to_clk_regmap(hw), struct clk_regmap_div, clkr);
  14. }
  15. static long div_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  16. unsigned long *prate)
  17. {
  18. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  19. struct clk_regmap *clkr = &divider->clkr;
  20. u32 val;
  21. int ret;
  22. ret = clk_runtime_get_regmap(clkr);
  23. if (ret)
  24. return ret;
  25. regmap_read(clkr->regmap, divider->reg, &val);
  26. val >>= divider->shift;
  27. val &= BIT(divider->width) - 1;
  28. clk_runtime_put_regmap(clkr);
  29. return divider_ro_round_rate(hw, rate, prate, NULL, divider->width,
  30. CLK_DIVIDER_ROUND_CLOSEST, val);
  31. }
  32. static long div_round_rate(struct clk_hw *hw, unsigned long rate,
  33. unsigned long *prate)
  34. {
  35. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  36. return divider_round_rate(hw, rate, prate, divider->table,
  37. divider->width,
  38. CLK_DIVIDER_ROUND_CLOSEST |
  39. divider->flags);
  40. }
  41. static int div_set_rate(struct clk_hw *hw, unsigned long rate,
  42. unsigned long parent_rate)
  43. {
  44. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  45. struct clk_regmap *clkr = &divider->clkr;
  46. u32 div;
  47. div = divider_get_val(rate, parent_rate, divider->table,
  48. divider->width, CLK_DIVIDER_ROUND_CLOSEST |
  49. divider->flags);
  50. return regmap_update_bits(clkr->regmap, divider->reg,
  51. (BIT(divider->width) - 1) << divider->shift,
  52. div << divider->shift);
  53. }
  54. static unsigned long div_recalc_rate(struct clk_hw *hw,
  55. unsigned long parent_rate)
  56. {
  57. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  58. struct clk_regmap *clkr = &divider->clkr;
  59. u32 div;
  60. regmap_read(clkr->regmap, divider->reg, &div);
  61. div >>= divider->shift;
  62. div &= BIT(divider->width) - 1;
  63. return divider_recalc_rate(hw, parent_rate, div, divider->table,
  64. CLK_DIVIDER_ROUND_CLOSEST | divider->flags,
  65. divider->width);
  66. }
  67. static long clk_regmap_div_list_rate(struct clk_hw *hw, unsigned int n,
  68. unsigned long fmax)
  69. {
  70. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  71. struct clk_hw *parent_hw = clk_hw_get_parent(hw);
  72. struct clk_regmap *clkr = &divider->clkr;
  73. struct clk_regmap *parent_clkr = to_clk_regmap(parent_hw);
  74. u32 div;
  75. int ret;
  76. ret = clk_runtime_get_regmap(clkr);
  77. if (ret)
  78. return ret;
  79. regmap_read(clkr->regmap, divider->reg, &div);
  80. div >>= divider->shift;
  81. div &= BIT(divider->width) - 1;
  82. div += 1;
  83. clk_runtime_put_regmap(clkr);
  84. if (parent_clkr && parent_clkr->ops && parent_clkr->ops->list_rate)
  85. return (parent_clkr->ops->list_rate(parent_hw, n, fmax) / div);
  86. return -EINVAL;
  87. }
  88. static struct clk_regmap_ops clk_regmap_div_regmap_ops = {
  89. .list_rate = clk_regmap_div_list_rate,
  90. };
  91. static int clk_regmap_div_init(struct clk_hw *hw)
  92. {
  93. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  94. struct clk_regmap *clkr = &divider->clkr;
  95. if (!clkr->ops)
  96. clkr->ops = &clk_regmap_div_regmap_ops;
  97. return 0;
  98. }
  99. const struct clk_ops clk_regmap_div_ops = {
  100. .round_rate = div_round_rate,
  101. .set_rate = div_set_rate,
  102. .recalc_rate = div_recalc_rate,
  103. };
  104. EXPORT_SYMBOL_GPL(clk_regmap_div_ops);
  105. const struct clk_ops clk_regmap_div_ro_ops = {
  106. .round_rate = div_round_ro_rate,
  107. .recalc_rate = div_recalc_rate,
  108. .init = clk_regmap_div_init,
  109. .debug_init = clk_common_debug_init,
  110. };
  111. EXPORT_SYMBOL_GPL(clk_regmap_div_ro_ops);