summaryrefslogtreecommitdiffstats
path: root/drivers/clk/tegra/clk-periph.c
blob: c970f63afa80eea1b61d1add55225f3bc1979253 (plain) (blame)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2013 Lucas Stach <l.stach@pengutronix.de>
 *
 * Based on the Linux Tegra clock code
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <io.h>
#include <malloc.h>
#include <linux/clk.h>
#include <linux/err.h>

#include "clk.h"

#define to_clk_periph(_hw) container_of(_hw, struct tegra_clk_periph, hw)

static int clk_periph_get_parent(struct clk *hw)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->mux->ops->get_parent(periph->mux);
}

static int clk_periph_set_parent(struct clk *hw, u8 index)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->mux->ops->set_parent(periph->mux, index);
}

static unsigned long clk_periph_recalc_rate(struct clk *hw,
					    unsigned long parent_rate)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->div->ops->recalc_rate(periph->div, parent_rate);
}

static long clk_periph_round_rate(struct clk *hw, unsigned long rate,
				  unsigned long *prate)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->div->ops->round_rate(periph->div, rate, prate);
}

static int clk_periph_set_rate(struct clk *hw, unsigned long rate,
			       unsigned long parent_rate)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->div->ops->set_rate(periph->div, rate, parent_rate);
}

static int clk_periph_is_enabled(struct clk *hw)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);

	return periph->gate->ops->is_enabled(periph->gate);
}

static int clk_periph_enable(struct clk *hw)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);
	u32 reg;

	reg = readl(periph->rst_reg);
	reg |= (1 << periph->rst_shift);
	writel(reg, periph->rst_reg);

	periph->gate->ops->enable(periph->gate);

	udelay(2);

	reg = readl(periph->rst_reg);
	reg &= ~(1 << periph->rst_shift);
	writel(reg, periph->rst_reg);

	return 0;
}

static void clk_periph_disable(struct clk *hw)
{
	struct tegra_clk_periph *periph = to_clk_periph(hw);
	u32 reg;

	reg = readl(periph->rst_reg);
	reg |= (1 << periph->rst_shift);
	writel(reg, periph->rst_reg);

	udelay(2);

	periph->gate->ops->disable(periph->gate);
}

const struct clk_ops tegra_clk_periph_ops = {
	.get_parent = clk_periph_get_parent,
	.set_parent = clk_periph_set_parent,
	.recalc_rate = clk_periph_recalc_rate,
	.round_rate = clk_periph_round_rate,
	.set_rate = clk_periph_set_rate,
	.is_enabled = clk_periph_is_enabled,
	.enable = clk_periph_enable,
	.disable = clk_periph_disable,
};

const struct clk_ops tegra_clk_periph_nodiv_ops = {
	.get_parent = clk_periph_get_parent,
	.set_parent = clk_periph_set_parent,
	.is_enabled = clk_periph_is_enabled,
	.enable = clk_periph_enable,
	.disable = clk_periph_disable,
};

struct clk *_tegra_clk_register_periph(const char *name,
		const char **parent_names, int num_parents,
		void __iomem *clk_base, u32 reg_offset, u8 id, u8 flags,
		bool has_div)
{
	struct tegra_clk_periph *periph;
	int ret;

	periph = kzalloc(sizeof(*periph), GFP_KERNEL);
	if (!periph) {
		pr_err("%s: could not allocate peripheral clk\n",
		       __func__);
		goto out_periph;
	}

	periph->mux = clk_mux_alloc(NULL, clk_base + reg_offset, 30, 2,
				    parent_names, num_parents, 0);
	if (!periph->mux)
		goto out_mux;

	periph->gate = clk_gate_alloc(NULL, NULL, clk_base + 0x10 +
				      ((id >> 3) & 0xc), id & 0x1f, 0);
	if (!periph->gate)
		goto out_gate;

	if (has_div) {
		periph->div = tegra_clk_divider_alloc(NULL, NULL, clk_base +
				reg_offset, 0, TEGRA_DIVIDER_ROUND_UP, 0, 8, 1);
		if (!periph->div)
			goto out_div;
	}

	periph->hw.name = name;
	periph->hw.ops = has_div ? &tegra_clk_periph_ops :
				   &tegra_clk_periph_nodiv_ops;
	periph->hw.parent_names = parent_names;
	periph->hw.num_parents = num_parents;
	periph->flags = flags;
	periph->rst_reg = clk_base + 0x4 + ((id >> 3) & 0xc);
	periph->rst_shift = id & 0x1f;

	ret = clk_register(&periph->hw);
	if (ret)
		goto out_register;

	return &periph->hw;

out_register:
	tegra_clk_divider_free(periph->div);
out_div:
	clk_gate_free(periph->gate);
out_gate:
	clk_mux_free(periph->mux);
out_mux:
	kfree(periph);
out_periph:
	return NULL;
}

struct clk *tegra_clk_register_periph_nodiv(const char *name,
		const char **parent_names, int num_parents,
		void __iomem *clk_base, u32 reg_offset, u8 id, u8 flags)
{
	return _tegra_clk_register_periph(name, parent_names, num_parents,
					  clk_base, reg_offset, id, flags,
					  false);
}

struct clk *tegra_clk_register_periph(const char *name,
		const char **parent_names, int num_parents,
		void __iomem *clk_base, u32 reg_offset, u8 id, u8 flags)
{
	return _tegra_clk_register_periph(name, parent_names, num_parents,
					  clk_base, reg_offset, id, flags,
					  true);
}