clk-audio-sync.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include "clk.h"
  20. static unsigned long clk_sync_source_recalc_rate(struct clk_hw *hw,
  21. unsigned long parent_rate)
  22. {
  23. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  24. return sync->rate;
  25. }
  26. static long clk_sync_source_round_rate(struct clk_hw *hw, unsigned long rate,
  27. unsigned long *prate)
  28. {
  29. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  30. if (rate > sync->max_rate)
  31. return -EINVAL;
  32. else
  33. return rate;
  34. }
  35. static int clk_sync_source_set_rate(struct clk_hw *hw, unsigned long rate,
  36. unsigned long parent_rate)
  37. {
  38. struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
  39. sync->rate = rate;
  40. return 0;
  41. }
  42. const struct clk_ops tegra_clk_sync_source_ops = {
  43. .round_rate = clk_sync_source_round_rate,
  44. .set_rate = clk_sync_source_set_rate,
  45. .recalc_rate = clk_sync_source_recalc_rate,
  46. };
  47. struct clk *tegra_clk_register_sync_source(const char *name,
  48. unsigned long rate, unsigned long max_rate)
  49. {
  50. struct tegra_clk_sync_source *sync;
  51. struct clk_init_data init;
  52. struct clk *clk;
  53. sync = kzalloc(sizeof(*sync), GFP_KERNEL);
  54. if (!sync) {
  55. pr_err("%s: could not allocate sync source clk\n", __func__);
  56. return ERR_PTR(-ENOMEM);
  57. }
  58. sync->rate = rate;
  59. sync->max_rate = max_rate;
  60. init.ops = &tegra_clk_sync_source_ops;
  61. init.name = name;
  62. init.flags = CLK_IS_ROOT;
  63. init.parent_names = NULL;
  64. init.num_parents = 0;
  65. /* Data in .init is copied by clk_register(), so stack variable OK */
  66. sync->hw.init = &init;
  67. clk = clk_register(NULL, &sync->hw);
  68. if (IS_ERR(clk))
  69. kfree(sync);
  70. return clk;
  71. }