clk-regmap-divider.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/bitops.h>
  15. #include <linux/regmap.h>
  16. #include <linux/export.h>
  17. #include "clk-regmap-divider.h"
  18. static inline struct clk_regmap_div *to_clk_regmap_div(struct clk_hw *hw)
  19. {
  20. return container_of(to_clk_regmap(hw), struct clk_regmap_div, clkr);
  21. }
  22. static long div_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  23. unsigned long *prate)
  24. {
  25. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  26. struct clk_regmap *clkr = &divider->clkr;
  27. u32 val;
  28. regmap_read(clkr->regmap, divider->reg, &val);
  29. val >>= divider->shift;
  30. val &= BIT(divider->width) - 1;
  31. return divider_ro_round_rate(hw, rate, prate, NULL, divider->width,
  32. CLK_DIVIDER_ROUND_CLOSEST, val);
  33. }
  34. static long div_round_rate(struct clk_hw *hw, unsigned long rate,
  35. unsigned long *prate)
  36. {
  37. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  38. return divider_round_rate(hw, rate, prate, NULL, divider->width,
  39. CLK_DIVIDER_ROUND_CLOSEST);
  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, NULL, divider->width,
  48. CLK_DIVIDER_ROUND_CLOSEST);
  49. return regmap_update_bits(clkr->regmap, divider->reg,
  50. (BIT(divider->width) - 1) << divider->shift,
  51. div << divider->shift);
  52. }
  53. static unsigned long div_recalc_rate(struct clk_hw *hw,
  54. unsigned long parent_rate)
  55. {
  56. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  57. struct clk_regmap *clkr = &divider->clkr;
  58. u32 div;
  59. regmap_read(clkr->regmap, divider->reg, &div);
  60. div >>= divider->shift;
  61. div &= BIT(divider->width) - 1;
  62. return divider_recalc_rate(hw, parent_rate, div, NULL,
  63. CLK_DIVIDER_ROUND_CLOSEST, divider->width);
  64. }
  65. const struct clk_ops clk_regmap_div_ops = {
  66. .round_rate = div_round_rate,
  67. .set_rate = div_set_rate,
  68. .recalc_rate = div_recalc_rate,
  69. };
  70. EXPORT_SYMBOL_GPL(clk_regmap_div_ops);
  71. const struct clk_ops clk_regmap_div_ro_ops = {
  72. .round_rate = div_round_ro_rate,
  73. .recalc_rate = div_recalc_rate,
  74. };
  75. EXPORT_SYMBOL_GPL(clk_regmap_div_ro_ops);