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 <narmstrong@baylibre.com>
  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 int meson_clk_cpu_dyndiv_determine_rate(struct clk_hw *hw,
  25. struct clk_rate_request *req)
  26. {
  27. struct clk_regmap *clk = to_clk_regmap(hw);
  28. struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
  29. return divider_determine_rate(hw, req, NULL, data->div.width, 0);
  30. }
  31. static int meson_clk_cpu_dyndiv_set_rate(struct clk_hw *hw, unsigned long rate,
  32. unsigned long parent_rate)
  33. {
  34. struct clk_regmap *clk = to_clk_regmap(hw);
  35. struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
  36. unsigned int val;
  37. int ret;
  38. ret = divider_get_val(rate, parent_rate, NULL, data->div.width, 0);
  39. if (ret < 0)
  40. return ret;
  41. val = (unsigned int)ret << data->div.shift;
  42. /* Write the SYS_CPU_DYN_ENABLE bit before changing the divider */
  43. meson_parm_write(clk->map, &data->dyn, 1);
  44. /* Update the divider while removing the SYS_CPU_DYN_ENABLE bit */
  45. return regmap_update_bits(clk->map, data->div.reg_off,
  46. SETPMASK(data->div.width, data->div.shift) |
  47. SETPMASK(data->dyn.width, data->dyn.shift),
  48. val);
  49. };
  50. const struct clk_ops meson_clk_cpu_dyndiv_ops = {
  51. .recalc_rate = meson_clk_cpu_dyndiv_recalc_rate,
  52. .determine_rate = meson_clk_cpu_dyndiv_determine_rate,
  53. .set_rate = meson_clk_cpu_dyndiv_set_rate,
  54. };
  55. EXPORT_SYMBOL_NS_GPL(meson_clk_cpu_dyndiv_ops, CLK_MESON);
  56. MODULE_DESCRIPTION("Amlogic CPU Dynamic Clock divider");
  57. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  58. MODULE_LICENSE("GPL");
  59. MODULE_IMPORT_NS(CLK_MESON);