clock-pcom.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2007 Google, Inc.
  3. * Copyright (c) 2007-2012, The Linux Foundation. All rights reserved.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/module.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/clkdev.h>
  21. #include <mach/clk.h>
  22. #include "proc_comm.h"
  23. #include "clock.h"
  24. #include "clock-pcom.h"
  25. struct clk_pcom {
  26. unsigned id;
  27. unsigned long flags;
  28. struct msm_clk msm_clk;
  29. };
  30. static inline struct clk_pcom *to_clk_pcom(struct clk_hw *hw)
  31. {
  32. return container_of(to_msm_clk(hw), struct clk_pcom, msm_clk);
  33. }
  34. static int pc_clk_enable(struct clk_hw *hw)
  35. {
  36. unsigned id = to_clk_pcom(hw)->id;
  37. int rc = msm_proc_comm(PCOM_CLKCTL_RPC_ENABLE, &id, NULL);
  38. if (rc < 0)
  39. return rc;
  40. else
  41. return (int)id < 0 ? -EINVAL : 0;
  42. }
  43. static void pc_clk_disable(struct clk_hw *hw)
  44. {
  45. unsigned id = to_clk_pcom(hw)->id;
  46. msm_proc_comm(PCOM_CLKCTL_RPC_DISABLE, &id, NULL);
  47. }
  48. static int pc_clk_reset(struct clk_hw *hw, enum clk_reset_action action)
  49. {
  50. int rc;
  51. unsigned id = to_clk_pcom(hw)->id;
  52. if (action == CLK_RESET_ASSERT)
  53. rc = msm_proc_comm(PCOM_CLKCTL_RPC_RESET_ASSERT, &id, NULL);
  54. else
  55. rc = msm_proc_comm(PCOM_CLKCTL_RPC_RESET_DEASSERT, &id, NULL);
  56. if (rc < 0)
  57. return rc;
  58. else
  59. return (int)id < 0 ? -EINVAL : 0;
  60. }
  61. static int pc_clk_set_rate(struct clk_hw *hw, unsigned long new_rate,
  62. unsigned long p_rate)
  63. {
  64. struct clk_pcom *p = to_clk_pcom(hw);
  65. unsigned id = p->id, rate = new_rate;
  66. int rc;
  67. /*
  68. * The rate _might_ be rounded off to the nearest KHz value by the
  69. * remote function. So a return value of 0 doesn't necessarily mean
  70. * that the exact rate was set successfully.
  71. */
  72. if (p->flags & CLKFLAG_MIN)
  73. rc = msm_proc_comm(PCOM_CLKCTL_RPC_MIN_RATE, &id, &rate);
  74. else
  75. rc = msm_proc_comm(PCOM_CLKCTL_RPC_SET_RATE, &id, &rate);
  76. if (rc < 0)
  77. return rc;
  78. else
  79. return (int)id < 0 ? -EINVAL : 0;
  80. }
  81. static unsigned long pc_clk_recalc_rate(struct clk_hw *hw, unsigned long p_rate)
  82. {
  83. unsigned id = to_clk_pcom(hw)->id;
  84. if (msm_proc_comm(PCOM_CLKCTL_RPC_RATE, &id, NULL))
  85. return 0;
  86. else
  87. return id;
  88. }
  89. static int pc_clk_is_enabled(struct clk_hw *hw)
  90. {
  91. unsigned id = to_clk_pcom(hw)->id;
  92. if (msm_proc_comm(PCOM_CLKCTL_RPC_ENABLED, &id, NULL))
  93. return 0;
  94. else
  95. return id;
  96. }
  97. static long pc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  98. unsigned long *p_rate)
  99. {
  100. /* Not really supported; pc_clk_set_rate() does rounding on it's own. */
  101. return rate;
  102. }
  103. static struct clk_ops clk_ops_pcom = {
  104. .enable = pc_clk_enable,
  105. .disable = pc_clk_disable,
  106. .set_rate = pc_clk_set_rate,
  107. .recalc_rate = pc_clk_recalc_rate,
  108. .is_enabled = pc_clk_is_enabled,
  109. .round_rate = pc_clk_round_rate,
  110. };
  111. static int msm_clock_pcom_probe(struct platform_device *pdev)
  112. {
  113. const struct pcom_clk_pdata *pdata = pdev->dev.platform_data;
  114. int i, ret;
  115. for (i = 0; i < pdata->num_lookups; i++) {
  116. const struct clk_pcom_desc *desc = &pdata->lookup[i];
  117. struct clk *c;
  118. struct clk_pcom *p;
  119. struct clk_hw *hw;
  120. struct clk_init_data init;
  121. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  122. if (!p)
  123. return -ENOMEM;
  124. p->id = desc->id;
  125. p->flags = desc->flags;
  126. p->msm_clk.reset = pc_clk_reset;
  127. hw = &p->msm_clk.hw;
  128. hw->init = &init;
  129. init.name = desc->name;
  130. init.ops = &clk_ops_pcom;
  131. init.num_parents = 0;
  132. init.flags = CLK_IS_ROOT;
  133. if (!(p->flags & CLKFLAG_AUTO_OFF))
  134. init.flags |= CLK_IGNORE_UNUSED;
  135. c = devm_clk_register(&pdev->dev, hw);
  136. ret = clk_register_clkdev(c, desc->con, desc->dev);
  137. if (ret)
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. static struct platform_driver msm_clock_pcom_driver = {
  143. .probe = msm_clock_pcom_probe,
  144. .driver = {
  145. .name = "msm-clock-pcom",
  146. .owner = THIS_MODULE,
  147. },
  148. };
  149. module_platform_driver(msm_clock_pcom_driver);
  150. MODULE_LICENSE("GPL v2");