summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/t30/acm_t30.c
blob: 6c058be9c734afb747a5d65b71d41bf83431f971 (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
137
138
139
140
141
/*
 * drivers/video/tegra/host/t30/acm_t30.c
 *
 * Tegra Graphics Host Power Management for Tegra3
 *
 * Copyright (c) 2010-2011, NVIDIA Corporation.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/slab.h>
#include <linux/err.h>
#include "../dev.h"
#include "../chip_support.h"
#include "../nvhost_acm.h"
#include "t30.h"
DEFINE_MUTEX(client_list_lock);

struct nvhost_module_client {
	struct list_head node;
	unsigned long rate[NVHOST_MODULE_MAX_CLOCKS];
	void *priv;
};

int t30_acm_get_rate(struct nvhost_module *mod, unsigned long *rate,
			    int index)
{
	struct clk *c;

	c = mod->clk[index];
	if (IS_ERR_OR_NULL(c))
		return -EINVAL;

	/* Need to enable client to get correct rate */
	nvhost_module_busy(mod);
	*rate = clk_get_rate(c);
	nvhost_module_idle(mod);
	return 0;
}

static int t30_acm_update_rate(struct nvhost_module *mod, int index)
{
	unsigned long rate = 0;
	struct nvhost_module_client *m;

	if (!mod->clk[index])
		return -EINVAL;

	list_for_each_entry(m, &mod->client_list, node) {
		rate = max(m->rate[index], rate);
	}
	if (!rate)
		rate = clk_round_rate(mod->clk[index],
				mod->desc->clocks[index].default_rate);

	return clk_set_rate(mod->clk[index], rate);
}

int t30_acm_set_rate(struct nvhost_module *mod, void *priv,
			    unsigned long rate, int index)
{
	struct nvhost_module_client *m;
	int ret;

	mutex_lock(&client_list_lock);
	list_for_each_entry(m, &mod->client_list, node) {
		if (m->priv == priv) {
			rate = clk_round_rate(mod->clk[index], rate);
			m->rate[index] = rate;
			break;
		}
	}
	ret = t30_acm_update_rate(mod, index);
	mutex_unlock(&client_list_lock);
	return ret;
}

int t30_acm_add_client(struct nvhost_module *mod, void *priv)
{
	int i;
	unsigned long rate;
	struct nvhost_module_client *client;

	client = kzalloc(sizeof(*client), GFP_KERNEL);
	if (!client)
		return -ENOMEM;

	INIT_LIST_HEAD(&client->node);
	client->priv = priv;

	for (i = 0; i < mod->num_clks; i++) {
		rate = clk_round_rate(mod->clk[i],
				mod->desc->clocks[i].default_rate);
		client->rate[i] = rate;
	}
	mutex_lock(&client_list_lock);
	list_add_tail(&client->node, &mod->client_list);
	mutex_unlock(&client_list_lock);
	return 0;
}

void t30_acm_remove_client(struct nvhost_module *mod, void *priv)
{
	int i;
	struct nvhost_module_client *m;

	mutex_lock(&client_list_lock);
	list_for_each_entry(m, &mod->client_list, node) {
		if (priv == m->priv) {
			list_del(&m->node);
			break;
		}
	}
	m->priv = NULL;
	kfree(m);
	for (i = 0; i < mod->num_clks; i++)
		t30_acm_update_rate(mod, i);
	mutex_unlock(&client_list_lock);
}

int nvhost_init_t30_acm(struct nvhost_master *host)
{
	host->op.acm.get_rate = t30_acm_get_rate;
	host->op.acm.set_rate = t30_acm_set_rate;
	host->op.acm.add_client = t30_acm_add_client;
	host->op.acm.remove_client = t30_acm_remove_client;

	return 0;
}