tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / mali / linux / mali_osk_pm.c
1 /*
2  * Copyright (C) 2011-2012 ARM Limited. All rights reserved.
3  *
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  *
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 /**
12  * @file mali_osk_pm.c
13  * Implementation of the callback functions from common power management
14  */
15
16 #include <linux/sched.h>
17
18 #ifdef CONFIG_PM_RUNTIME
19 #include <linux/pm_runtime.h>
20 #endif /* CONFIG_PM_RUNTIME */
21 #include <linux/platform_device.h>
22 #include <linux/version.h>
23 #include "mali_osk.h"
24 #include "mali_kernel_common.h"
25 #include "mali_kernel_linux.h"
26
27 static _mali_osk_atomic_t mali_pm_ref_count;
28
29 void _mali_osk_pm_dev_enable(void) /* @@@@ todo: change to init of some kind.. or change the way or where atomics are initialized? */
30 {
31         _mali_osk_atomic_init(&mali_pm_ref_count, 0);
32 }
33
34 void _mali_osk_pm_dev_disable(void) /* @@@@ todo: change to term of some kind */
35 {
36         _mali_osk_atomic_term(&mali_pm_ref_count);
37 }
38
39 /* Can NOT run in atomic context */
40 _mali_osk_errcode_t _mali_osk_pm_dev_ref_add(void)
41 {
42 #ifdef CONFIG_PM_RUNTIME
43         int err;
44         MALI_DEBUG_ASSERT_POINTER(mali_platform_device);
45         err = pm_runtime_get_sync(&(mali_platform_device->dev));
46 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
47         pm_runtime_mark_last_busy(&(mali_platform_device->dev));
48 #endif
49         if (0 > err)
50         {
51                 MALI_PRINT_ERROR(("Mali OSK PM: pm_runtime_get_sync() returned error code %d\n", err));
52                 return _MALI_OSK_ERR_FAULT;
53         }
54         _mali_osk_atomic_inc(&mali_pm_ref_count);
55         MALI_DEBUG_PRINT(4, ("Mali OSK PM: Power ref taken (%u)\n", _mali_osk_atomic_read(&mali_pm_ref_count)));
56 #endif
57         return _MALI_OSK_ERR_OK;
58 }
59
60 /* Can run in atomic context */
61 void _mali_osk_pm_dev_ref_dec(void)
62 {
63 #ifdef CONFIG_PM_RUNTIME
64         MALI_DEBUG_ASSERT_POINTER(mali_platform_device);
65         _mali_osk_atomic_dec(&mali_pm_ref_count);
66 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
67         pm_runtime_mark_last_busy(&(mali_platform_device->dev));
68         pm_runtime_put_autosuspend(&(mali_platform_device->dev));
69 #else
70         pm_runtime_put(&(mali_platform_device->dev));
71 #endif
72         MALI_DEBUG_PRINT(4, ("Mali OSK PM: Power ref released (%u)\n", _mali_osk_atomic_read(&mali_pm_ref_count)));
73 #endif
74 }
75
76 /* Can run in atomic context */
77 mali_bool _mali_osk_pm_dev_ref_add_no_power_on(void)
78 {
79 #ifdef CONFIG_PM_RUNTIME
80         u32 ref;
81         MALI_DEBUG_ASSERT_POINTER(mali_platform_device);
82         pm_runtime_get_noresume(&(mali_platform_device->dev));
83         ref = _mali_osk_atomic_read(&mali_pm_ref_count);
84         MALI_DEBUG_PRINT(4, ("Mali OSK PM: No-power ref taken (%u)\n", _mali_osk_atomic_read(&mali_pm_ref_count)));
85         return ref > 0 ? MALI_TRUE : MALI_FALSE;
86 #else
87         return MALI_TRUE;
88 #endif
89 }
90
91 /* Can run in atomic context */
92 void _mali_osk_pm_dev_ref_dec_no_power_on(void)
93 {
94 #ifdef CONFIG_PM_RUNTIME
95         MALI_DEBUG_ASSERT_POINTER(mali_platform_device);
96 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
97         pm_runtime_put_autosuspend(&(mali_platform_device->dev));
98 #else
99         pm_runtime_put(&(mali_platform_device->dev));
100 #endif
101         MALI_DEBUG_PRINT(4, ("Mali OSK PM: No-power ref released (%u)\n", _mali_osk_atomic_read(&mali_pm_ref_count)));
102 #endif
103 }