clk-cpu-dyndiv.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Copyright (c) 2019 BayLibre, SAS.
  4. * Author: Neil Armstrong <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/module.h>
  8. #include "clk-regmap.h"
  9. #include "clk-cpu-dyndiv.h"
  10. static inline struct meson_clk_cpu_dyndiv_data *
  11. meson_clk_cpu_dyndiv_data(struct clk_regmap *clk)
  12. {
  13. return (struct meson_clk_cpu_dyndiv_data *)clk->data;
  14. }
  15. static unsigned long meson_clk_cpu_dyndiv_recalc_rate(struct clk_hw *hw,
  16. unsigned long prate)
  17. {
  18. struct clk_regmap *clk = to_clk_regmap(hw);
  19. struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
  20. return divider_recalc_rate(hw, prate,
  21. meson_parm_read(clk->map, &data->div),
  22. NULL, 0, data->div.width);
  23. }
  24. static long meson_clk_cpu_dyndiv_round_rate(struct clk_hw *hw,
  25. unsigned long rate,
  26. unsigned long *prate)
  27. {
  28. struct clk_regmap *clk = to_clk_regmap(hw);
  29. struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
  30. return divider_round_rate(hw, rate, prate, NULL, data->div.width, 0);
  31. }
  32. static int meson_clk_cpu_dyndiv_set_rate(struct clk_hw *hw, unsigned long rate,
  33. unsigned long parent_rate)
  34. {
  35. struct clk_regmap *clk = to_clk_regmap(hw);
  36. struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
  37. unsigned int val;
  38. int ret;
  39. ret = divider_get_val(rate, parent_rate, NULL, data->div.width, 0);
  40. if (ret < 0)
  41. return ret;
  42. val = (unsigned int)ret << data->div.shift;
  43. /* Write the SYS_CPU_DYN_ENABLE bit before changing the divider */
  44. meson_parm_write(clk->map, &data->dyn, 1);
  45. /* Update the divider while removing the SYS_CPU_DYN_ENABLE bit */
  46. return regmap_update_bits(clk->map, data->div.reg_off,
  47. SETPMASK(data->div.width, data->div.shift) |
  48. SETPMASK(data->dyn.width, data->dyn.shift),
  49. val);
  50. };
  51. const struct clk_ops meson_clk_cpu_dyndiv_ops = {
  52. .recalc_rate = meson_clk_cpu_dyndiv_recalc_rate,
  53. .round_rate = meson_clk_cpu_dyndiv_round_rate,
  54. .set_rate = meson_clk_cpu_dyndiv_set_rate,
  55. };
  56. EXPORT_SYMBOL_GPL(meson_clk_cpu_dyndiv_ops);
  57. MODULE_DESCRIPTION("Amlogic CPU Dynamic Clock divider");
  58. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  59. MODULE_LICENSE("GPL v2");