clk-regmap-divider.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_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. return divider_round_rate(hw, rate, prate, NULL, divider->width,
  27. CLK_DIVIDER_ROUND_CLOSEST);
  28. }
  29. static int div_set_rate(struct clk_hw *hw, unsigned long rate,
  30. unsigned long parent_rate)
  31. {
  32. struct clk_regmap_div *divider = to_clk_regmap_div(hw);
  33. struct clk_regmap *clkr = &divider->clkr;
  34. u32 div;
  35. div = divider_get_val(rate, parent_rate, NULL, divider->width,
  36. CLK_DIVIDER_ROUND_CLOSEST);
  37. return regmap_update_bits(clkr->regmap, divider->reg,
  38. (BIT(divider->width) - 1) << divider->shift,
  39. div << divider->shift);
  40. }
  41. static unsigned long div_recalc_rate(struct clk_hw *hw,
  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. regmap_read(clkr->regmap, divider->reg, &div);
  48. div >>= divider->shift;
  49. div &= BIT(divider->width) - 1;
  50. return divider_recalc_rate(hw, parent_rate, div, NULL,
  51. CLK_DIVIDER_ROUND_CLOSEST);
  52. }
  53. const struct clk_ops clk_regmap_div_ops = {
  54. .round_rate = div_round_rate,
  55. .set_rate = div_set_rate,
  56. .recalc_rate = div_recalc_rate,
  57. };
  58. EXPORT_SYMBOL_GPL(clk_regmap_div_ops);