s5p_mfc_pm.c 2.4 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 22
 *
 * 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"

static struct s5p_mfc_pm *pm;
static struct s5p_mfc_dev *p_dev;
23
static atomic_t clk_ref;
24 25 26

int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
{
27
	int i;
28 29 30

	pm = &dev->pm;
	p_dev = dev;
31

32 33 34 35 36 37 38 39 40 41 42 43
	pm->num_clocks = dev->variant->num_clocks;
	pm->clk_names = dev->variant->clk_names;
	pm->device = &dev->plat_dev->dev;
	pm->clock_gate = NULL;

	/* clock control */
	for (i = 0; i < pm->num_clocks; i++) {
		pm->clocks[i] = devm_clk_get(pm->device, pm->clk_names[i]);
		if (IS_ERR(pm->clocks[i])) {
			mfc_err("Failed to get clock: %s\n",
				pm->clk_names[i]);
			return PTR_ERR(pm->clocks[i]);
44 45 46
		}
	}

47 48 49
	if (dev->variant->use_clock_gating)
		pm->clock_gate = pm->clocks[0];

50 51 52 53 54 55 56 57 58 59 60 61 62
	pm_runtime_enable(pm->device);
	atomic_set(&clk_ref, 0);
	return 0;
}

void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
{
	pm_runtime_disable(pm->device);
}

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

65
	return clk_enable(pm->clock_gate);
66 67 68 69 70
}

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

73
	clk_disable(pm->clock_gate);
74 75 76 77
}

int s5p_mfc_power_on(void)
{
78
	int i, ret = 0;
79 80

	ret = pm_runtime_get_sync(pm->device);
81
	if (ret < 0)
82
		return ret;
83

84 85 86 87 88 89 90 91 92
	/* clock control */
	for (i = 0; i < pm->num_clocks; i++) {
		ret = clk_prepare_enable(pm->clocks[i]);
		if (ret < 0) {
			mfc_err("clock prepare failed for clock: %s\n",
				pm->clk_names[i]);
			i++;
			goto err;
		}
93 94
	}

95 96
	/* prepare for software clock gating */
	clk_disable(pm->clock_gate);
97

98 99 100 101 102
	return 0;
err:
	while (--i > 0)
		clk_disable_unprepare(pm->clocks[i]);
	pm_runtime_put(pm->device);
103
	return ret;
104 105 106 107
}

int s5p_mfc_power_off(void)
{
108 109 110 111
	int i;

	/* finish software clock gating */
	clk_enable(pm->clock_gate);
112

113 114
	for (i = 0; i < pm->num_clocks; i++)
		clk_disable_unprepare(pm->clocks[i]);
115

116 117 118
	return pm_runtime_put_sync(pm->device);
}