clk-audio-sync.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/slab.h>
  7. #include <linux/err.h>
  8. #include "clk.h"
  9. static unsigned long clk_sync_source_recalc_rate(struct clk_hw *hw,
  10. unsigned long parent_rate)
  11. {
  12. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  13. return sync->rate;
  14. }
  15. static long clk_sync_source_round_rate(struct clk_hw *hw, unsigned long rate,
  16. unsigned long *prate)
  17. {
  18. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  19. if (rate > sync->max_rate)
  20. return -EINVAL;
  21. else
  22. return rate;
  23. }
  24. static int clk_sync_source_set_rate(struct clk_hw *hw, unsigned long rate,
  25. unsigned long parent_rate)
  26. {
  27. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  28. sync->rate = rate;
  29. return 0;
  30. }
  31. const struct clk_ops tegra_clk_sync_source_ops = {
  32. .round_rate = clk_sync_source_round_rate,
  33. .set_rate = clk_sync_source_set_rate,
  34. .recalc_rate = clk_sync_source_recalc_rate,
  35. };
  36. struct clk *tegra_clk_register_sync_source(const char *name,
  37. unsigned long max_rate)
  38. {
  39. struct tegra_clk_sync_source *sync;
  40. struct clk_init_data init;
  41. struct clk *clk;
  42. sync = kzalloc(sizeof(*sync), GFP_KERNEL);
  43. if (!sync) {
  44. pr_err("%s: could not allocate sync source clk\n", __func__);
  45. return ERR_PTR(-ENOMEM);
  46. }
  47. sync->max_rate = max_rate;
  48. init.ops = &tegra_clk_sync_source_ops;
  49. init.name = name;
  50. init.flags = 0;
  51. init.parent_names = NULL;
  52. init.num_parents = 0;
  53. /* Data in .init is copied by clk_register(), so stack variable OK */
  54. sync->hw.init = &init;
  55. clk = clk_register(NULL, &sync->hw);
  56. if (IS_ERR(clk))
  57. kfree(sync);
  58. return clk;
  59. }