s5p_mfc_pm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * linux/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
  3. *
  4. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include "s5p_mfc_common.h"
  17. #include "s5p_mfc_debug.h"
  18. #include "s5p_mfc_pm.h"
  19. static struct s5p_mfc_pm *pm;
  20. static struct s5p_mfc_dev *p_dev;
  21. static atomic_t clk_ref;
  22. int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
  23. {
  24. int i;
  25. pm = &dev->pm;
  26. p_dev = dev;
  27. pm->num_clocks = dev->variant->num_clocks;
  28. pm->clk_names = dev->variant->clk_names;
  29. pm->device = &dev->plat_dev->dev;
  30. pm->clock_gate = NULL;
  31. /* clock control */
  32. for (i = 0; i < pm->num_clocks; i++) {
  33. pm->clocks[i] = devm_clk_get(pm->device, pm->clk_names[i]);
  34. if (IS_ERR(pm->clocks[i])) {
  35. /* additional clocks are optional */
  36. if (i && PTR_ERR(pm->clocks[i]) == -ENOENT) {
  37. pm->clocks[i] = NULL;
  38. continue;
  39. }
  40. mfc_err("Failed to get clock: %s\n",
  41. pm->clk_names[i]);
  42. return PTR_ERR(pm->clocks[i]);
  43. }
  44. }
  45. if (dev->variant->use_clock_gating)
  46. pm->clock_gate = pm->clocks[0];
  47. pm_runtime_enable(pm->device);
  48. atomic_set(&clk_ref, 0);
  49. return 0;
  50. }
  51. void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
  52. {
  53. pm_runtime_disable(pm->device);
  54. }
  55. int s5p_mfc_clock_on(void)
  56. {
  57. atomic_inc(&clk_ref);
  58. mfc_debug(3, "+ %d\n", atomic_read(&clk_ref));
  59. return clk_enable(pm->clock_gate);
  60. }
  61. void s5p_mfc_clock_off(void)
  62. {
  63. atomic_dec(&clk_ref);
  64. mfc_debug(3, "- %d\n", atomic_read(&clk_ref));
  65. clk_disable(pm->clock_gate);
  66. }
  67. int s5p_mfc_power_on(void)
  68. {
  69. int i, ret = 0;
  70. ret = pm_runtime_get_sync(pm->device);
  71. if (ret < 0) {
  72. pm_runtime_put_noidle(pm->device);
  73. return ret;
  74. }
  75. /* clock control */
  76. for (i = 0; i < pm->num_clocks; i++) {
  77. ret = clk_prepare_enable(pm->clocks[i]);
  78. if (ret < 0) {
  79. mfc_err("clock prepare failed for clock: %s\n",
  80. pm->clk_names[i]);
  81. i++;
  82. goto err;
  83. }
  84. }
  85. /* prepare for software clock gating */
  86. clk_disable(pm->clock_gate);
  87. return 0;
  88. err:
  89. while (--i > 0)
  90. clk_disable_unprepare(pm->clocks[i]);
  91. pm_runtime_put(pm->device);
  92. return ret;
  93. }
  94. int s5p_mfc_power_off(void)
  95. {
  96. int i;
  97. /* finish software clock gating */
  98. clk_enable(pm->clock_gate);
  99. for (i = 0; i < pm->num_clocks; i++)
  100. clk_disable_unprepare(pm->clocks[i]);
  101. return pm_runtime_put_sync(pm->device);
  102. }