aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-exynos/dev.c
blob: a866a9ad68a16a9fdd32a34b147a4f2e558ba844 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* linux/arch/arm/mach-exynos/dev.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com
 *
 * EXYNOS4 Device List support
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/device.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/slab.h>

#include <mach/dev.h>
#ifdef CONFIG_ARCH_EXYNOS4
#include <mach/busfreq_exynos4.h>
#else
#include <mach/busfreq_exynos5.h>
#endif

static LIST_HEAD(domains_list);
static DEFINE_MUTEX(domains_mutex);

static struct device_domain *find_device_domain(struct device *dev)
{
	struct device_domain *tmp_domain, *domain = ERR_PTR(-ENODEV);

	mutex_lock(&domains_mutex);
	list_for_each_entry(tmp_domain, &domains_list, node) {
		if (tmp_domain->device == dev) {
			domain = tmp_domain;
			break;
		}
	}

	mutex_unlock(&domains_mutex);
	return domain;
}

int dev_add(struct device_domain *dev, struct device *device)
{
	if (!dev || !device)
		return -EINVAL;

	mutex_lock(&domains_mutex);
	INIT_LIST_HEAD(&dev->domain_list);
	dev->device = device;
	list_add(&dev->node, &domains_list);
	mutex_unlock(&domains_mutex);

	return 0;
}

struct device *dev_get(const char *name)
{
	struct device_domain *domain;

	mutex_lock(&domains_mutex);
	list_for_each_entry(domain, &domains_list, node)
		if (strcmp(name, dev_name(domain->device)) == 0)
			goto found;

	mutex_unlock(&domains_mutex);
	return ERR_PTR(-ENODEV);
found:
	mutex_unlock(&domains_mutex);
	return domain->device;
}

void dev_put(const char *name)
{
	return;
}

int dev_lock(struct device *device, struct device *dev,
		unsigned long freq)
{
	struct device_domain *domain;
	struct domain_lock *lock;
	int ret = 0;

	domain = find_device_domain(device);

	if (IS_ERR(domain)) {
		dev_err(dev, "Can't find device domain.\n");
		return -EINVAL;
	}

	mutex_lock(&domains_mutex);
	list_for_each_entry(lock, &domain->domain_list, node) {
		if (lock->device == dev) {
			/* If the lock already exist, only update the freq */
			lock->freq = freq;
			goto out;
		}
	}

	lock = kzalloc(sizeof(struct domain_lock), GFP_KERNEL);
	if (!lock) {
		dev_err(device, "Unable to create domain_lock");
		ret = -ENOMEM;
		goto out;
	}

	lock->device = dev;
	lock->freq = freq;
	list_add(&lock->node, &domain->domain_list);

out:
	mutex_unlock(&domains_mutex);
	exynos_request_apply(freq);
	return ret;
}

int dev_unlock(struct device *device, struct device *dev)
{
	struct device_domain *domain;
	struct domain_lock *lock;

	domain = find_device_domain(device);

	if (IS_ERR(domain)) {
		dev_err(dev, "Can't find device domain.\n");
		return -EINVAL;
	}

	mutex_lock(&domains_mutex);
	list_for_each_entry(lock, &domain->domain_list, node) {
		if (lock->device == dev) {
			list_del(&lock->node);
			kfree(lock);
			break;
		}
	}

	mutex_unlock(&domains_mutex);

	return 0;
}

unsigned long dev_max_freq(struct device *device)
{
	struct device_domain *domain;
	struct domain_lock *lock;
	unsigned long freq = 0;

	domain = find_device_domain(device);
	if (IS_ERR(domain)) {
		dev_dbg(device, "Can't find device domain.\n");
		return freq;
	}

	mutex_lock(&domains_mutex);
	list_for_each_entry(lock, &domain->domain_list, node)
		if (lock->freq > freq)
			freq = lock->freq;

	mutex_unlock(&domains_mutex);

	return freq;
}

int dev_lock_list(struct device *device, char *buf)
{
	struct device_domain *domain;
	struct domain_lock *lock;
	int count = 0;

	domain = find_device_domain(device);
	if (IS_ERR(domain)) {
		dev_dbg(device, "Can't find device domain.\n");
		return 0;
	}

	mutex_lock(&domains_mutex);
	count = sprintf(buf, "Lock List\n");
	list_for_each_entry(lock, &domain->domain_list, node)
		count += sprintf(buf + count, "%s : %lu\n", dev_name(lock->device), lock->freq);

	mutex_unlock(&domains_mutex);

	return count;
}