clk-regmap-divider.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, 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. static inline struct clk_regmap_div *to_clk_regmap_div(struct clk_hw *hw)
  11. {
  12. return container_of(to_clk_regmap(hw), struct clk_regmap_div, clkr);
  13. }
  14. static long div_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  15. unsigned long *prate)
  16. {
  17. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  18. struct clk_regmap *clkr = &divider->clkr;
  19. u32 val;
  20. regmap_read(clkr->regmap, divider->reg, &val);
  21. val >>= divider->shift;
  22. val &= BIT(divider->width) - 1;
  23. return divider_ro_round_rate(hw, rate, prate, NULL, divider->width,
  24. CLK_DIVIDER_ROUND_CLOSEST, val);
  25. }
  26. static long div_round_rate(struct clk_hw *hw, unsigned long rate,
  27. unsigned long *prate)
  28. {
  29. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  30. return divider_round_rate(hw, rate, prate, NULL, divider->width,
  31. CLK_DIVIDER_ROUND_CLOSEST);
  32. }
  33. static int div_set_rate(struct clk_hw *hw, unsigned long rate,
  34. unsigned long parent_rate)
  35. {
  36. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  37. struct clk_regmap *clkr = &divider->clkr;
  38. u32 div;
  39. div = divider_get_val(rate, parent_rate, NULL, divider->width,
  40. CLK_DIVIDER_ROUND_CLOSEST);
  41. return regmap_update_bits(clkr->regmap, divider->reg,
  42. (BIT(divider->width) - 1) << divider->shift,
  43. div << divider->shift);
  44. }
  45. static unsigned long div_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  49. struct clk_regmap *clkr = &divider->clkr;
  50. u32 div;
  51. regmap_read(clkr->regmap, divider->reg, &div);
  52. div >>= divider->shift;
  53. div &= BIT(divider->width) - 1;
  54. return divider_recalc_rate(hw, parent_rate, div, NULL,
  55. CLK_DIVIDER_ROUND_CLOSEST, divider->width);
  56. }
  57. const struct clk_ops clk_regmap_div_ops = {
  58. .round_rate = div_round_rate,
  59. .set_rate = div_set_rate,
  60. .recalc_rate = div_recalc_rate,
  61. };
  62. EXPORT_SYMBOL_GPL(clk_regmap_div_ops);
  63. const struct clk_ops clk_regmap_div_ro_ops = {
  64. .round_rate = div_round_ro_rate,
  65. .recalc_rate = div_recalc_rate,
  66. };
  67. EXPORT_SYMBOL_GPL(clk_regmap_div_ro_ops);