s5p_mfc_pm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. mfc_err("Failed to get clock: %s\n",
  36. pm->clk_names[i]);
  37. return PTR_ERR(pm->clocks[i]);
  38. }
  39. }
  40. if (dev->variant->use_clock_gating)
  41. pm->clock_gate = pm->clocks[0];
  42. pm_runtime_enable(pm->device);
  43. atomic_set(&clk_ref, 0);
  44. return 0;
  45. }
  46. void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
  47. {
  48. pm_runtime_disable(pm->device);
  49. }
  50. int s5p_mfc_clock_on(void)
  51. {
  52. atomic_inc(&clk_ref);
  53. mfc_debug(3, "+ %d\n", atomic_read(&clk_ref));
  54. return clk_enable(pm->clock_gate);
  55. }
  56. void s5p_mfc_clock_off(void)
  57. {
  58. atomic_dec(&clk_ref);
  59. mfc_debug(3, "- %d\n", atomic_read(&clk_ref));
  60. clk_disable(pm->clock_gate);
  61. }
  62. int s5p_mfc_power_on(void)
  63. {
  64. int i, ret = 0;
  65. ret = pm_runtime_get_sync(pm->device);
  66. if (ret < 0)
  67. return ret;
  68. /* clock control */
  69. for (i = 0; i < pm->num_clocks; i++) {
  70. ret = clk_prepare_enable(pm->clocks[i]);
  71. if (ret < 0) {
  72. mfc_err("clock prepare failed for clock: %s\n",
  73. pm->clk_names[i]);
  74. i++;
  75. goto err;
  76. }
  77. }
  78. /* prepare for software clock gating */
  79. clk_disable(pm->clock_gate);
  80. return 0;
  81. err:
  82. while (--i > 0)
  83. clk_disable_unprepare(pm->clocks[i]);
  84. pm_runtime_put(pm->device);
  85. return ret;
  86. }
  87. int s5p_mfc_power_off(void)
  88. {
  89. int i;
  90. /* finish software clock gating */
  91. clk_enable(pm->clock_gate);
  92. for (i = 0; i < pm->num_clocks; i++)
  93. clk_disable_unprepare(pm->clocks[i]);
  94. return pm_runtime_put_sync(pm->device);
  95. }