aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/versatile/clk-vexpress-spc.c
blob: d3f8fb44ccadb04fb81e24bbc962e3bac088135f (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
/*
 * Copyright (C) 2012 ARM Limited
 * Copyright (C) 2012 Linaro
 *
 * Author: Viresh Kumar <viresh.kumar@linaro.org>
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

/* SPC clock programming interface for Vexpress cpus */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/vexpress.h>

struct clk_spc {
	struct clk_hw hw;
	spinlock_t *lock;
	int cluster;
};

#define to_clk_spc(spc) container_of(spc, struct clk_spc, hw)

static unsigned long spc_recalc_rate(struct clk_hw *hw,
		unsigned long parent_rate)
{
	struct clk_spc *spc = to_clk_spc(hw);
	u32 freq;

	if (vexpress_spc_get_performance(spc->cluster, &freq)) {
		return -EIO;
		pr_err("%s: Failed", __func__);
	}

	return freq * 1000;
}

static long spc_round_rate(struct clk_hw *hw, unsigned long drate,
		unsigned long *parent_rate)
{
	return drate;
}

static int spc_set_rate(struct clk_hw *hw, unsigned long rate,
		unsigned long parent_rate)
{
	struct clk_spc *spc = to_clk_spc(hw);

	return vexpress_spc_set_performance(spc->cluster, rate / 1000);
}

static struct clk_ops clk_spc_ops = {
	.recalc_rate = spc_recalc_rate,
	.round_rate = spc_round_rate,
	.set_rate = spc_set_rate,
};

struct clk *vexpress_clk_register_spc(const char *name, int cluster_id)
{
	struct clk_init_data init;
	struct clk_spc *spc;
	struct clk *clk;

	if (!name) {
		pr_err("Invalid name passed");
		return ERR_PTR(-EINVAL);
	}

	spc = kzalloc(sizeof(*spc), GFP_KERNEL);
	if (!spc) {
		pr_err("could not allocate spc clk\n");
		return ERR_PTR(-ENOMEM);
	}

	spc->hw.init = &init;
	spc->cluster = cluster_id;

	init.name = name;
	init.ops = &clk_spc_ops;
	init.flags = CLK_IS_ROOT | CLK_GET_RATE_NOCACHE;
	init.num_parents = 0;

	clk = clk_register(NULL, &spc->hw);
	if (!IS_ERR_OR_NULL(clk))
		return clk;

	pr_err("clk register failed\n");
	kfree(spc);

	return NULL;
}

#if defined(CONFIG_OF)
void __init vexpress_clk_of_register_spc(void)
{
	char name[9] = "cluster";
	struct device_node *node = NULL;
	struct clk *clk;
	const u32 *val;
	int cluster_id = 0, len;

	if (!of_find_compatible_node(NULL, NULL, "arm,spc")) {
		pr_debug("%s: No SPC found, Exiting!!\n", __func__);
		return;
	}

	while ((node = of_find_node_by_name(node, "cluster"))) {
		val = of_get_property(node, "reg", &len);
		if (val && len == 4)
			cluster_id = be32_to_cpup(val);

		name[7] = cluster_id + '0';
		clk = vexpress_clk_register_spc(name, cluster_id);
		if (IS_ERR(clk))
			return;

		pr_debug("Registered clock '%s'\n", name);
		clk_register_clkdev(clk, name, NULL);
	}
}
CLK_OF_DECLARE(spc, "arm,spc", vexpress_clk_of_register_spc);
#endif