summaryrefslogtreecommitdiff
path: root/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
blob: 796dac85746ace68c3941046006181d12eaaf642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
 *
 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/clk.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include "s5p_mfc_common.h"
#include "s5p_mfc_debug.h"
#include "s5p_mfc_pm.h"

#define MFC_GATE_CLK_NAME	"mfc"
#define MFC_SCLK_NAME		"sclk_mfc"

static struct s5p_mfc_pm *pm;
static struct s5p_mfc_dev *p_dev;
static atomic_t clk_ref;

int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
{
	int ret = 0;

	pm = &dev->pm;
	p_dev = dev;
	pm->use_clock_gating = dev->variant->use_clock_gating;
	pm->clock_gate = clk_get(&dev->plat_dev->dev, MFC_GATE_CLK_NAME);
	if (IS_ERR(pm->clock_gate)) {
		mfc_err("Failed to get clock-gating control\n");
		ret = PTR_ERR(pm->clock_gate);
		goto err_g_ip_clk;
	}

	if (dev->variant->version != MFC_VERSION_V6) {
		pm->clock = clk_get(&dev->plat_dev->dev, MFC_SCLK_NAME);
		if (IS_ERR(pm->clock)) {
			mfc_info("Failed to get MFC special clock control\n");
			pm->clock = NULL;
		}
	}

	pm->device = &dev->plat_dev->dev;
	pm_runtime_enable(pm->device);
	atomic_set(&clk_ref, 0);

	return 0;

	clk_put(pm->clock_gate);
	pm->clock_gate = NULL;
err_g_ip_clk:
	return ret;
}

void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
{
	if (dev->variant->version != MFC_VERSION_V6 &&
	    pm->clock) {
		clk_put(pm->clock);
		pm->clock = NULL;
	}
	clk_put(pm->clock_gate);
	pm->clock_gate = NULL;
	pm_runtime_disable(pm->device);
}

int s5p_mfc_clock_on(void)
{
	atomic_inc(&clk_ref);
	mfc_debug(3, "+ %d\n", atomic_read(&clk_ref));

	if (!pm->use_clock_gating)
		return 0;
	return clk_enable(pm->clock_gate);
}

void s5p_mfc_clock_off(void)
{
	atomic_dec(&clk_ref);
	mfc_debug(3, "- %d\n", atomic_read(&clk_ref));

	if (!pm->use_clock_gating)
		return;
	clk_disable(pm->clock_gate);
}

int s5p_mfc_power_on(void)
{
	int ret;

	ret = pm_runtime_get_sync(pm->device);
	if (ret)
		return ret;

	ret = clk_prepare_enable(pm->clock_gate);
	if (ret)
		goto err_pm;

	if (pm->clock) {
		ret = clk_prepare_enable(pm->clock);
		if (ret)
			goto err_gate;
	}

	if (pm->use_clock_gating)
		clk_disable(pm->clock_gate);
	return 0;

err_gate:
	clk_disable_unprepare(pm->clock_gate);
err_pm:
	pm_runtime_put_sync(pm->device);
	return ret;

}

int s5p_mfc_power_off(void)
{
	if (pm->clock)
		clk_disable_unprepare(pm->clock);

	if (pm->use_clock_gating)
		clk_unprepare(pm->clock_gate);
	else
		clk_disable_unprepare(pm->clock_gate);

	return pm_runtime_put_sync(pm->device);
}