clk-rk-muxgrf.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include "clk-rk-muxgrf.h"
  11. static rt_err_t rockchip_muxgrf_set_parent_common(struct rt_clk_cell *cell, struct rt_syscon *grf,
  12. rt_uint8_t index)
  13. {
  14. rt_uint32_t mask, val;
  15. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  16. mask = RT_GENMASK(rk_cell->mux_width + rk_cell->mux_shift - 1, rk_cell->mux_shift);
  17. val = index;
  18. val <<= rk_cell->mux_shift;
  19. if (rk_cell->mux_flags & CLK_MUX_HIWORD_MASK)
  20. {
  21. return rt_syscon_write(grf, rk_cell->muxdiv_offset, val | (mask << 16));
  22. }
  23. return rt_syscon_update_bits(grf, rk_cell->muxdiv_offset, mask, val);
  24. }
  25. static rt_uint8_t rockchip_muxgrf_get_parent_common(struct rt_clk_cell *cell, struct rt_syscon *grf)
  26. {
  27. rt_uint32_t mask, val;
  28. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  29. mask = RT_GENMASK(rk_cell->mux_width - 1, 0);
  30. rt_syscon_read(grf, rk_cell->muxdiv_offset, &val);
  31. val >>= rk_cell->mux_shift;
  32. val &= mask;
  33. return val;
  34. }
  35. static rt_err_t rockchip_muxgrf_set_parent(struct rt_clk_cell *cell, rt_uint8_t index)
  36. {
  37. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  38. return rockchip_muxgrf_set_parent_common(cell, rk_cell->provider->grf, index);
  39. }
  40. static rt_uint8_t rockchip_muxgrf_get_parent(struct rt_clk_cell *cell)
  41. {
  42. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  43. return rockchip_muxgrf_get_parent_common(cell, rk_cell->provider->grf);
  44. }
  45. const struct rt_clk_ops rockchip_muxgrf_clk_ops =
  46. {
  47. .set_parent = rockchip_muxgrf_set_parent,
  48. .get_parent = rockchip_muxgrf_get_parent,
  49. };
  50. static rt_err_t rockchip_muxpmugrf_set_parent(struct rt_clk_cell *cell, rt_uint8_t index)
  51. {
  52. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  53. return rockchip_muxgrf_set_parent_common(cell, rk_cell->provider->pmugrf, index);
  54. }
  55. static rt_uint8_t rockchip_muxpmugrf_get_parent(struct rt_clk_cell *cell)
  56. {
  57. struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);
  58. return rockchip_muxgrf_get_parent_common(cell, rk_cell->provider->pmugrf);
  59. }
  60. const struct rt_clk_ops rockchip_muxpmugrf_clk_ops =
  61. {
  62. .set_parent = rockchip_muxpmugrf_set_parent,
  63. .get_parent = rockchip_muxpmugrf_get_parent,
  64. };