owl-divider.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // OWL divider clock driver
  4. //
  5. // Copyright (c) 2014 Actions Semi Inc.
  6. // Author: David Liu <liuwei@actions-semi.com>
  7. //
  8. // Copyright (c) 2018 Linaro Ltd.
  9. // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  10. #include <linux/clk-provider.h>
  11. #include <linux/regmap.h>
  12. #include "owl-divider.h"
  13. long owl_divider_helper_round_rate(struct owl_clk_common *common,
  14. const struct owl_divider_hw *div_hw,
  15. unsigned long rate,
  16. unsigned long *parent_rate)
  17. {
  18. return divider_round_rate(&common->hw, rate, parent_rate,
  19. div_hw->table, div_hw->width,
  20. div_hw->div_flags);
  21. }
  22. static long owl_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  23. unsigned long *parent_rate)
  24. {
  25. struct owl_divider *div = hw_to_owl_divider(hw);
  26. return owl_divider_helper_round_rate(&div->common, &div->div_hw,
  27. rate, parent_rate);
  28. }
  29. unsigned long owl_divider_helper_recalc_rate(struct owl_clk_common *common,
  30. const struct owl_divider_hw *div_hw,
  31. unsigned long parent_rate)
  32. {
  33. unsigned long val;
  34. unsigned int reg;
  35. regmap_read(common->regmap, div_hw->reg, &reg);
  36. val = reg >> div_hw->shift;
  37. val &= (1 << div_hw->width) - 1;
  38. return divider_recalc_rate(&common->hw, parent_rate,
  39. val, div_hw->table,
  40. div_hw->div_flags,
  41. div_hw->width);
  42. }
  43. static unsigned long owl_divider_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct owl_divider *div = hw_to_owl_divider(hw);
  47. return owl_divider_helper_recalc_rate(&div->common,
  48. &div->div_hw, parent_rate);
  49. }
  50. int owl_divider_helper_set_rate(const struct owl_clk_common *common,
  51. const struct owl_divider_hw *div_hw,
  52. unsigned long rate,
  53. unsigned long parent_rate)
  54. {
  55. unsigned long val;
  56. unsigned int reg;
  57. val = divider_get_val(rate, parent_rate, div_hw->table,
  58. div_hw->width, 0);
  59. regmap_read(common->regmap, div_hw->reg, &reg);
  60. reg &= ~GENMASK(div_hw->width + div_hw->shift - 1, div_hw->shift);
  61. regmap_write(common->regmap, div_hw->reg,
  62. reg | (val << div_hw->shift));
  63. return 0;
  64. }
  65. static int owl_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  66. unsigned long parent_rate)
  67. {
  68. struct owl_divider *div = hw_to_owl_divider(hw);
  69. return owl_divider_helper_set_rate(&div->common, &div->div_hw,
  70. rate, parent_rate);
  71. }
  72. const struct clk_ops owl_divider_ops = {
  73. .recalc_rate = owl_divider_recalc_rate,
  74. .round_rate = owl_divider_round_rate,
  75. .set_rate = owl_divider_set_rate,
  76. };