s5p_mfc_pm.c 2.7 KB
Newer Older
1
/*
2
 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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"
22
#define MFC_SCLK_NAME		"sclk_mfc"
23 24 25

static struct s5p_mfc_pm *pm;
static struct s5p_mfc_dev *p_dev;
26
static atomic_t clk_ref;
27 28 29 30 31 32 33

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

	pm = &dev->pm;
	p_dev = dev;
34
	pm->use_clock_gating = dev->variant->use_clock_gating;
35 36 37
	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");
38
		ret = PTR_ERR(pm->clock_gate);
39 40
		goto err_g_ip_clk;
	}
41

42 43 44 45
	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");
46
			pm->clock = NULL;
47 48 49
		}
	}

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

54
	return 0;
55

56
	clk_put(pm->clock_gate);
57
	pm->clock_gate = NULL;
58 59 60 61 62 63
err_g_ip_clk:
	return ret;
}

void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
{
64 65 66
	if (dev->variant->version != MFC_VERSION_V6 &&
	    pm->clock) {
		clk_put(pm->clock);
67
		pm->clock = NULL;
68
	}
69
	clk_put(pm->clock_gate);
70
	pm->clock_gate = NULL;
71 72 73 74 75 76
	pm_runtime_disable(pm->device);
}

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

79 80
	if (!pm->use_clock_gating)
		return 0;
81
	return clk_enable(pm->clock_gate);
82 83 84 85 86
}

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

89 90
	if (!pm->use_clock_gating)
		return;
91
	clk_disable(pm->clock_gate);
92 93 94 95
}

int s5p_mfc_power_on(void)
{
96
	int ret;
97 98 99 100

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

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	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);
120
	return ret;
121

122 123 124 125
}

int s5p_mfc_power_off(void)
{
126 127 128 129 130 131 132 133
	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);

134 135 136
	return pm_runtime_put_sync(pm->device);
}