aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-exynos/clock-domain.c
blob: 187dfa134973b5aa331e0da3b293a12bc2532439 (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
/* linux/arch/arm/mach-exynos/clock-domain.c
 *
 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * EXYNOS - Clock Domain 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/kernel.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cpufreq.h>
#include <linux/list.h>
#include <linux/rculist.h>
#include <linux/rcupdate.h>
#include <linux/device.h>

#include <plat/clock.h>

#include <mach/clock-domain.h>

static LIST_HEAD(clock_domain_list);
/* Lock to allow exclusive modification to the clock domain list */
static DEFINE_MUTEX(clock_domain_list_lock);

static struct clock_domain *find_clock_domain(unsigned int flag)
{
	struct clock_domain *tmp_domain, *domain = ERR_PTR(-ENODEV);

	list_for_each_entry_rcu(tmp_domain, &clock_domain_list, node) {
		if (tmp_domain->flag & flag) {
			domain = tmp_domain;
			break;
		}
	}

	return domain;
}

int clock_domain_enabled(unsigned int flag)
{
	struct clock_domain *domain;
	struct clock *clock;

	domain = find_clock_domain(flag);
	if (IS_ERR(domain)) {
		pr_err("Unable to find Clock Domain\n");
		return -EINVAL;
	}

	list_for_each_entry_rcu(clock, &domain->domain_list, node) {
		if (clock->clk->usage)
			return -EINVAL;
	}

	return 0;
}

int clock_add_domain(unsigned int flag, struct clk *clk)
{
	struct clock_domain *domain = NULL;
	struct clock *clock;

	/* allocate new clock node */
	clock = kzalloc(sizeof(struct clock), GFP_KERNEL);
	if (!clock) {
		pr_err("Unable to create new Clock node\n");
		return -ENOMEM;
	}

	/* Hold our list modification lock here */
	mutex_lock(&clock_domain_list_lock);

	/* Check for existing list for 'dev' */
	domain = find_clock_domain(flag);
	if (IS_ERR(domain)) {
		/*
		 * Allocate a new Clock Doamin.*/
		domain = kzalloc(sizeof(struct clock_domain), GFP_KERNEL);
		if (!domain) {
			mutex_unlock(&clock_domain_list_lock);
			kfree(clock);
			pr_err("Unable to create Clock Domain structure\n");
			return -ENOMEM;
		}

		domain->flag = flag;
		INIT_LIST_HEAD(&domain->domain_list);

		list_add_rcu(&domain->node, &clock_domain_list);
	}

	clock->clk = clk;

	list_add_rcu(&clock->node, &domain->domain_list);
	mutex_unlock(&clock_domain_list_lock);

	return 0;
}