aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/qcom/clk-smd-rpm.c
blob: 859b709385f6b7822a790502ee1a0ed558ab2224 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 2015, Linaro Limited
 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#include <linux/clk-provider.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/soc/qcom/smd-rpm.h>

#include "clk-smd-rpm.h"

#define to_clk_smd_rpm(_hw) container_of(_hw, struct clk_smd_rpm, hw)

static DEFINE_MUTEX(rpm_clk_lock);

static int clk_smd_rpm_set_rate_active(struct clk_smd_rpm *r,
				       unsigned long value)
{
	struct clk_smd_rpm_req req = {
		.key = r->rpm_key,
		.nbytes = sizeof(u32),
		.value = DIV_ROUND_UP(value, 1000), /* RPM expects kHz */
	};

	return qcom_rpm_smd_write(r->rpm, QCOM_SMD_RPM_ACTIVE_STATE,
				  r->rpm_res_type, r->rpm_clk_id, &req,
				  sizeof(req));
}

static int clk_smd_rpm_set_rate_sleep(struct clk_smd_rpm *r,
				      unsigned long value)
{
	struct clk_smd_rpm_req req = {
		.key = r->rpm_key,
		.nbytes = sizeof(u32),
		.value = DIV_ROUND_UP(value, 1000), /* RPM expects kHz */
	};

	return qcom_rpm_smd_write(r->rpm, QCOM_SMD_RPM_SLEEP_STATE,
				  r->rpm_res_type, r->rpm_clk_id, &req,
				  sizeof(req));
}

static void to_active_sleep(struct clk_smd_rpm *r, unsigned long rate,
			    unsigned long *active, unsigned long *sleep)
{
	*active = rate;

	/*
	 * Active-only clocks don't care what the rate is during sleep. So,
	 * they vote for zero.
	 */
	if (r->active_only)
		*sleep = 0;
	else
		*sleep = *active;
}

static int clk_smd_rpm_prepare(struct clk_hw *hw)
{
	struct clk_smd_rpm *r = to_clk_smd_rpm(hw);
	struct clk_smd_rpm *peer = r->peer;
	unsigned long this_rate = 0, this_sleep_rate = 0;
	unsigned long peer_rate = 0, peer_sleep_rate = 0;
	unsigned long active_rate, sleep_rate;
	int ret = 0;

	mutex_lock(&rpm_clk_lock);

	/* Don't send requests to the RPM if the rate has not been set. */
	if (!r->rate)
		goto out;

	to_active_sleep(r, r->rate, &this_rate, &this_sleep_rate);

	/* Take peer clock's rate into account only if it's enabled. */
	if (peer->enabled)
		to_active_sleep(peer, peer->rate,
				&peer_rate, &peer_sleep_rate);

	active_rate = max(this_rate, peer_rate);

	if (r->branch)
		active_rate = !!active_rate;

	ret = clk_smd_rpm_set_rate_active(r, active_rate);
	if (ret)
		goto out;

	sleep_rate = max(this_sleep_rate, peer_sleep_rate);
	if (r->branch)
		sleep_rate = !!sleep_rate;

	ret = clk_smd_rpm_set_rate_sleep(r, sleep_rate);
	if (ret)
		/* Undo the active set vote and restore it */
		ret = clk_smd_rpm_set_rate_active(r, peer_rate);

out:
	if (!ret)
		r->enabled = true;

	mutex_unlock(&rpm_clk_lock);

	return ret;
}

static void clk_smd_rpm_unprepare(struct clk_hw *hw)
{
	struct clk_smd_rpm *r = to_clk_smd_rpm(hw);

	mutex_lock(&rpm_clk_lock);

	if (r->rate) {
		struct clk_smd_rpm *peer = r->peer;
		unsigned long peer_rate = 0, peer_sleep_rate = 0;
		unsigned long active_rate, sleep_rate;
		int ret;

		/* Take peer clock's rate into account only if it's enabled. */
		if (peer->enabled)
			to_active_sleep(peer, peer->rate, &peer_rate,
					&peer_sleep_rate);

		active_rate = r->branch ? !!peer_rate : peer_rate;
		ret = clk_smd_rpm_set_rate_active(r, active_rate);
		if (ret)
			goto out;

		sleep_rate = r->branch ? !!peer_sleep_rate : peer_sleep_rate;
		ret = clk_smd_rpm_set_rate_sleep(r, sleep_rate);
 		if (ret)
			goto out;
	}
	r->enabled = false;

out:
	mutex_unlock(&rpm_clk_lock);
}

static int clk_smd_rpm_set_rate(struct clk_hw *hw, unsigned long rate,
				unsigned long parent_rate)
{
	struct clk_smd_rpm *r = to_clk_smd_rpm(hw);
	int ret = 0;

	mutex_lock(&rpm_clk_lock);

	if (r->enabled) {
		struct clk_smd_rpm *peer = r->peer;
		unsigned long active_rate, sleep_rate;
		unsigned long this_rate = 0, this_sleep_rate = 0;
		unsigned long peer_rate = 0, peer_sleep_rate = 0;

		to_active_sleep(r, rate, &this_rate, &this_sleep_rate);

		/* Take peer clock's rate into account only if it's enabled. */
		if (peer->enabled)
			to_active_sleep(peer, peer->rate,
					&peer_rate, &peer_sleep_rate);

		active_rate = max(this_rate, peer_rate);
		ret = clk_smd_rpm_set_rate_active(r, active_rate);
		if (ret)
			goto out;

		sleep_rate = max(this_sleep_rate, peer_sleep_rate);
		ret = clk_smd_rpm_set_rate_sleep(r, sleep_rate);
		if (ret)
			goto out;
	}
	r->rate = rate;
out:
	mutex_unlock(&rpm_clk_lock);

	return ret;
}

static long clk_smd_rpm_round_rate(struct clk_hw *hw, unsigned long rate,
				   unsigned long *parent_rate)
{

	/*
 	 * RPM handles rate rounding and we don't have a way to
	 * know what the rate will be, so just return whatever
	 * rate is requested.
	 */
	return rate;
}

static unsigned long clk_smd_rpm_recalc_rate(struct clk_hw *hw,
					     unsigned long parent_rate)
{
	struct clk_smd_rpm *r = to_clk_smd_rpm(hw);

	/*
 	 * RPM handles rate rounding and we don't have a way to
	 * know what the rate will be, so just return whatever
	 * rate was set.
	 */
	return r->rate;
}

int clk_smd_rpm_enable_scaling(struct qcom_smd_rpm *rpm)
{
	int ret;
	struct clk_smd_rpm_req req = {
		.key = QCOM_RPM_SMD_KEY_ENABLE,
		.nbytes = sizeof(u32),
		.value = 1,
	};

	ret = qcom_rpm_smd_write(rpm, QCOM_SMD_RPM_SLEEP_STATE,
				QCOM_SMD_RPM_MISC_CLK,
				QCOM_RPM_SCALING_ENABLE_ID, &req, sizeof(req));
	if (ret) {
		pr_err("RPM clock scaling (sleep set) not enabled!\n");
		return ret;
	}

	ret = qcom_rpm_smd_write(rpm, QCOM_SMD_RPM_ACTIVE_STATE,
				 QCOM_SMD_RPM_MISC_CLK,
				 QCOM_RPM_SCALING_ENABLE_ID, &req, sizeof(req));
	if (ret) {
		pr_err("RPM clock scaling (active set) not enabled!\n");
		return ret;
	}

	pr_debug("%s: RPM clock scaling is enabled\n", __func__);
	return 0;
}
EXPORT_SYMBOL_GPL(clk_smd_rpm_enable_scaling);

const struct clk_ops clk_smd_rpm_ops = {
	.prepare = clk_smd_rpm_prepare,
	.unprepare = clk_smd_rpm_unprepare,
	.set_rate = clk_smd_rpm_set_rate,
	.round_rate = clk_smd_rpm_round_rate,
	.recalc_rate = clk_smd_rpm_recalc_rate,
};
EXPORT_SYMBOL_GPL(clk_smd_rpm_ops);

const struct clk_ops clk_smd_rpm_branch_ops = {
	.prepare = clk_smd_rpm_prepare,
	.unprepare = clk_smd_rpm_unprepare,
	.round_rate = clk_smd_rpm_round_rate,
	.recalc_rate = clk_smd_rpm_recalc_rate,
};
EXPORT_SYMBOL_GPL(clk_smd_rpm_branch_ops);