clk.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
  3. * Author(s): Ludovic Barre, <ludovic.barre@st.com> for STMicroelectronics.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <drivers/clk.h>
  11. static const struct clk_ops *ops;
  12. int clk_enable(unsigned long id)
  13. {
  14. assert((ops != NULL) && (ops->enable != NULL));
  15. return ops->enable(id);
  16. }
  17. void clk_disable(unsigned long id)
  18. {
  19. assert((ops != NULL) && (ops->disable != NULL));
  20. ops->disable(id);
  21. }
  22. unsigned long clk_get_rate(unsigned long id)
  23. {
  24. assert((ops != NULL) && (ops->get_rate != NULL));
  25. return ops->get_rate(id);
  26. }
  27. int clk_set_rate(unsigned long id, unsigned long rate, unsigned long *orate)
  28. {
  29. unsigned long lrate;
  30. assert((ops != NULL) && (ops->set_rate != NULL));
  31. if (orate != NULL) {
  32. return ops->set_rate(id, rate, orate);
  33. }
  34. /* In case the caller is not interested in the output rate */
  35. return ops->set_rate(id, rate, &lrate);
  36. }
  37. int clk_get_parent(unsigned long id)
  38. {
  39. assert((ops != NULL) && (ops->get_parent != NULL));
  40. return ops->get_parent(id);
  41. }
  42. int clk_set_parent(unsigned long id, unsigned long parent_id)
  43. {
  44. assert((ops != NULL) && (ops->set_parent != NULL));
  45. return ops->set_parent(id, parent_id);
  46. }
  47. bool clk_is_enabled(unsigned long id)
  48. {
  49. assert((ops != NULL) && (ops->is_enabled != NULL));
  50. return ops->is_enabled(id);
  51. }
  52. /*
  53. * Initialize the clk. The fields in the provided clk
  54. * ops pointer must be valid.
  55. */
  56. void clk_register(const struct clk_ops *ops_ptr)
  57. {
  58. assert((ops_ptr != NULL) &&
  59. (ops_ptr->enable != NULL) &&
  60. (ops_ptr->disable != NULL) &&
  61. (ops_ptr->get_rate != NULL) &&
  62. (ops_ptr->get_parent != NULL) &&
  63. (ops_ptr->is_enabled != NULL));
  64. ops = ops_ptr;
  65. }