Merge branch 'fix/cache' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[cascardo/linux.git] / drivers / media / platform / s5p-mfc / s5p_mfc_pm.c
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
13 #include <linux/clk.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #ifdef CONFIG_PM_RUNTIME
17 #include <linux/pm_runtime.h>
18 #endif
19 #include "s5p_mfc_common.h"
20 #include "s5p_mfc_debug.h"
21 #include "s5p_mfc_pm.h"
22
23 #define MFC_GATE_CLK_NAME       "mfc"
24 #define MFC_SCLK_NAME           "sclk_mfc"
25 #define MFC_SCLK_RATE           (200 * 1000000)
26
27 #define CLK_DEBUG
28
29 static struct s5p_mfc_pm *pm;
30 static struct s5p_mfc_dev *p_dev;
31
32 #ifdef CLK_DEBUG
33 static atomic_t clk_ref;
34 #endif
35
36 int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
37 {
38         int ret = 0;
39
40         pm = &dev->pm;
41         p_dev = dev;
42         pm->clock_gate = clk_get(&dev->plat_dev->dev, MFC_GATE_CLK_NAME);
43         if (IS_ERR(pm->clock_gate)) {
44                 mfc_err("Failed to get clock-gating control\n");
45                 ret = PTR_ERR(pm->clock_gate);
46                 goto err_g_ip_clk;
47         }
48
49         ret = clk_prepare(pm->clock_gate);
50         if (ret) {
51                 mfc_err("Failed to prepare clock-gating control\n");
52                 goto err_p_ip_clk;
53         }
54
55         if (dev->variant->version != MFC_VERSION_V6) {
56                 pm->clock = clk_get(&dev->plat_dev->dev, MFC_SCLK_NAME);
57                 if (IS_ERR(pm->clock)) {
58                         mfc_info("Failed to get MFC special clock control\n");
59                 } else {
60                         clk_set_rate(pm->clock, MFC_SCLK_RATE);
61                         ret = clk_prepare_enable(pm->clock);
62                         if (ret) {
63                                 mfc_err("Failed to enable MFC special clock\n");
64                                 goto err_s_clk;
65                         }
66                 }
67         }
68
69         atomic_set(&pm->power, 0);
70 #ifdef CONFIG_PM_RUNTIME
71         pm->device = &dev->plat_dev->dev;
72         pm_runtime_enable(pm->device);
73 #endif
74 #ifdef CLK_DEBUG
75         atomic_set(&clk_ref, 0);
76 #endif
77         return 0;
78
79 err_s_clk:
80         clk_put(pm->clock);
81 err_p_ip_clk:
82         clk_put(pm->clock_gate);
83 err_g_ip_clk:
84         return ret;
85 }
86
87 void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
88 {
89         if (dev->variant->version != MFC_VERSION_V6 &&
90             pm->clock) {
91                 clk_disable_unprepare(pm->clock);
92                 clk_put(pm->clock);
93         }
94         clk_unprepare(pm->clock_gate);
95         clk_put(pm->clock_gate);
96 #ifdef CONFIG_PM_RUNTIME
97         pm_runtime_disable(pm->device);
98 #endif
99 }
100
101 int s5p_mfc_clock_on(void)
102 {
103         int ret;
104 #ifdef CLK_DEBUG
105         atomic_inc(&clk_ref);
106         mfc_debug(3, "+ %d\n", atomic_read(&clk_ref));
107 #endif
108         ret = clk_enable(pm->clock_gate);
109         return ret;
110 }
111
112 void s5p_mfc_clock_off(void)
113 {
114 #ifdef CLK_DEBUG
115         atomic_dec(&clk_ref);
116         mfc_debug(3, "- %d\n", atomic_read(&clk_ref));
117 #endif
118         clk_disable(pm->clock_gate);
119 }
120
121 int s5p_mfc_power_on(void)
122 {
123 #ifdef CONFIG_PM_RUNTIME
124         return pm_runtime_get_sync(pm->device);
125 #else
126         atomic_set(&pm->power, 1);
127         return 0;
128 #endif
129 }
130
131 int s5p_mfc_power_off(void)
132 {
133 #ifdef CONFIG_PM_RUNTIME
134         return pm_runtime_put_sync(pm->device);
135 #else
136         atomic_set(&pm->power, 0);
137         return 0;
138 #endif
139 }
140
141