blob: 1d7a5e209d5ee709905c9dcc7d4ee84cc449727a [file] [log] [blame]
Saravana Kannanc85ecf92013-01-21 17:58:35 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/err.h>
Stephen Boyd27fae3b2012-09-21 11:00:39 -070015#include <linux/mutex.h>
Matt Wagantall33d01f52012-02-23 23:27:44 -080016#include <mach/clk-provider.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070017
18#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include "clock-rpm.h"
20
Stephen Boyd27fae3b2012-09-21 11:00:39 -070021#define __clk_rpmrs_set_rate(r, value, ctx) \
22 ((r)->rpmrs_data->set_rate_fn((r), (value), (ctx)))
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070023
24#define clk_rpmrs_set_rate_sleep(r, value) \
Stephen Boyd27fae3b2012-09-21 11:00:39 -070025 __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_sleep_id)
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070026
27#define clk_rpmrs_set_rate_active(r, value) \
Stephen Boyd27fae3b2012-09-21 11:00:39 -070028 __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_active_id)
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070029
30static int clk_rpmrs_set_rate(struct rpm_clk *r, uint32_t value,
Stephen Boyd27fae3b2012-09-21 11:00:39 -070031 uint32_t context)
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070032{
33 struct msm_rpm_iv_pair iv = {
34 .id = r->rpm_clk_id,
35 .value = value,
36 };
Stephen Boyd27fae3b2012-09-21 11:00:39 -070037 return msm_rpmrs_set(context, &iv, 1);
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070038}
39
40static int clk_rpmrs_get_rate(struct rpm_clk *r)
41{
42 int rc;
43 struct msm_rpm_iv_pair iv = { .id = r->rpm_status_id, };
44 rc = msm_rpm_get_status(&iv, 1);
Deepak Katragadda92862ca2013-06-21 14:36:18 -070045 return (rc < 0) ? rc : iv.value * 1000;
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070046}
47
Vikram Mulukutlae2300de2012-08-10 04:58:50 -070048static int clk_rpmrs_handoff(struct rpm_clk *r)
49{
50 struct msm_rpm_iv_pair iv = { .id = r->rpm_status_id, };
51 int rc = msm_rpm_get_status(&iv, 1);
52
53 if (rc < 0)
54 return rc;
55
Tianyi Gou88fe5c22013-03-27 15:15:29 -070056 if (!r->branch)
Deepak Katragadda92862ca2013-06-21 14:36:18 -070057 r->c.rate = iv.value * 1000;
Vikram Mulukutlae2300de2012-08-10 04:58:50 -070058
59 return 0;
60}
61
Vikram Mulukutla120299a2013-06-14 15:28:10 -070062static int clk_rpmrs_is_enabled(struct rpm_clk *r)
63{
64 return !!clk_rpmrs_get_rate(r);
65}
66
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -070067static int clk_rpmrs_set_rate_smd(struct rpm_clk *r, uint32_t value,
Stephen Boyd27fae3b2012-09-21 11:00:39 -070068 uint32_t context)
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -070069{
70 struct msm_rpm_kvp kvp = {
Vikram Mulukutla4aba6c02012-07-10 11:12:05 -070071 .key = r->rpm_key,
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -070072 .data = (void *)&value,
73 .length = sizeof(value),
74 };
75
Stephen Boyd27fae3b2012-09-21 11:00:39 -070076 return msm_rpm_send_message(context, r->rpm_res_type, r->rpm_clk_id,
77 &kvp, 1);
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -070078}
79
Vikram Mulukutlae2300de2012-08-10 04:58:50 -070080static int clk_rpmrs_handoff_smd(struct rpm_clk *r)
81{
Tianyi Gou88fe5c22013-03-27 15:15:29 -070082 if (!r->branch)
83 r->c.rate = INT_MAX;
Saravana Kannanca1528d2012-10-11 22:40:12 -070084
Vikram Mulukutlae2300de2012-08-10 04:58:50 -070085 return 0;
86}
87
Vikram Mulukutla120299a2013-06-14 15:28:10 -070088static int clk_rpmrs_is_enabled_smd(struct rpm_clk *r)
89{
90 return !!r->c.prepare_count;
91}
92
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070093struct clk_rpmrs_data {
Stephen Boyd27fae3b2012-09-21 11:00:39 -070094 int (*set_rate_fn)(struct rpm_clk *r, uint32_t value, uint32_t context);
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070095 int (*get_rate_fn)(struct rpm_clk *r);
Vikram Mulukutlae2300de2012-08-10 04:58:50 -070096 int (*handoff_fn)(struct rpm_clk *r);
Vikram Mulukutla120299a2013-06-14 15:28:10 -070097 int (*is_enabled)(struct rpm_clk *r);
Vikram Mulukutla0f44f922012-05-16 21:52:26 -070098 int ctx_active_id;
99 int ctx_sleep_id;
100};
101
102struct clk_rpmrs_data clk_rpmrs_data = {
103 .set_rate_fn = clk_rpmrs_set_rate,
104 .get_rate_fn = clk_rpmrs_get_rate,
Vikram Mulukutlae2300de2012-08-10 04:58:50 -0700105 .handoff_fn = clk_rpmrs_handoff,
Vikram Mulukutla120299a2013-06-14 15:28:10 -0700106 .is_enabled = clk_rpmrs_is_enabled,
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700107 .ctx_active_id = MSM_RPM_CTX_SET_0,
108 .ctx_sleep_id = MSM_RPM_CTX_SET_SLEEP,
109};
110
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -0700111struct clk_rpmrs_data clk_rpmrs_data_smd = {
112 .set_rate_fn = clk_rpmrs_set_rate_smd,
Vikram Mulukutlae2300de2012-08-10 04:58:50 -0700113 .handoff_fn = clk_rpmrs_handoff_smd,
Vikram Mulukutla120299a2013-06-14 15:28:10 -0700114 .is_enabled = clk_rpmrs_is_enabled_smd,
Vikram Mulukutlaa085dc82012-05-18 11:21:44 -0700115 .ctx_active_id = MSM_RPM_CTX_ACTIVE_SET,
116 .ctx_sleep_id = MSM_RPM_CTX_SLEEP_SET,
117};
118
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700119static DEFINE_MUTEX(rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700121static void to_active_sleep_khz(struct rpm_clk *r, unsigned long rate,
122 unsigned long *active_khz, unsigned long *sleep_khz)
123{
124 /* Convert the rate (hz) to khz */
Deepak Katragadda92862ca2013-06-21 14:36:18 -0700125 *active_khz = DIV_ROUND_UP(rate, 1000);
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700126
127 /*
128 * Active-only clocks don't care what the rate is during sleep. So,
129 * they vote for zero.
130 */
131 if (r->active_only)
132 *sleep_khz = 0;
133 else
134 *sleep_khz = *active_khz;
135}
136
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700137static int rpm_clk_prepare(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 struct rpm_clk *r = to_rpm_clk(clk);
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700140 uint32_t value;
Stephen Boyda6835112012-01-26 14:40:05 -0800141 int rc = 0;
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700142 unsigned long this_khz, this_sleep_khz;
143 unsigned long peer_khz = 0, peer_sleep_khz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144 struct rpm_clk *peer = r->peer;
145
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700146 mutex_lock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700148 to_active_sleep_khz(r, r->c.rate, &this_khz, &this_sleep_khz);
149
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 /* Don't send requests to the RPM if the rate has not been set. */
151 if (this_khz == 0)
152 goto out;
153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154 /* Take peer clock's rate into account only if it's enabled. */
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700155 if (peer->enabled)
156 to_active_sleep_khz(peer, peer->c.rate,
157 &peer_khz, &peer_sleep_khz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700159 value = max(this_khz, peer_khz);
Stephen Boyda6835112012-01-26 14:40:05 -0800160 if (r->branch)
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700161 value = !!value;
Stephen Boyda6835112012-01-26 14:40:05 -0800162
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700163 rc = clk_rpmrs_set_rate_active(r, value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 if (rc)
165 goto out;
166
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700167 value = max(this_sleep_khz, peer_sleep_khz);
Stephen Boyda6835112012-01-26 14:40:05 -0800168 if (r->branch)
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700169 value = !!value;
170
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700171 rc = clk_rpmrs_set_rate_sleep(r, value);
Matt Wagantall735f01a2011-08-12 12:40:28 -0700172 if (rc) {
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700173 /* Undo the active set vote and restore it to peer_khz */
174 value = peer_khz;
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700175 rc = clk_rpmrs_set_rate_active(r, value);
Matt Wagantall735f01a2011-08-12 12:40:28 -0700176 }
177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178out:
179 if (!rc)
180 r->enabled = true;
181
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700182 mutex_unlock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183
184 return rc;
185}
186
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700187static void rpm_clk_unprepare(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 struct rpm_clk *r = to_rpm_clk(clk);
190
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700191 mutex_lock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700193 if (r->c.rate) {
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700194 uint32_t value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 struct rpm_clk *peer = r->peer;
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700196 unsigned long peer_khz = 0, peer_sleep_khz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 int rc;
198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 /* Take peer clock's rate into account only if it's enabled. */
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700200 if (peer->enabled)
201 to_active_sleep_khz(peer, peer->c.rate,
202 &peer_khz, &peer_sleep_khz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700204 value = r->branch ? !!peer_khz : peer_khz;
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700205 rc = clk_rpmrs_set_rate_active(r, value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 if (rc)
207 goto out;
208
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700209 value = r->branch ? !!peer_sleep_khz : peer_sleep_khz;
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700210 rc = clk_rpmrs_set_rate_sleep(r, value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 }
212 r->enabled = false;
213out:
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700214 mutex_unlock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215
216 return;
217}
218
Matt Wagantall77952c42011-11-08 18:45:48 -0800219static int rpm_clk_set_rate(struct clk *clk, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 struct rpm_clk *r = to_rpm_clk(clk);
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700222 unsigned long this_khz, this_sleep_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 int rc = 0;
224
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700225 mutex_lock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 if (r->enabled) {
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700228 uint32_t value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 struct rpm_clk *peer = r->peer;
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700230 unsigned long peer_khz = 0, peer_sleep_khz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700232 to_active_sleep_khz(r, rate, &this_khz, &this_sleep_khz);
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 /* Take peer clock's rate into account only if it's enabled. */
Tianyi Gou88fe5c22013-03-27 15:15:29 -0700235 if (peer->enabled)
236 to_active_sleep_khz(peer, peer->c.rate,
237 &peer_khz, &peer_sleep_khz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700239 value = max(this_khz, peer_khz);
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700240 rc = clk_rpmrs_set_rate_active(r, value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 if (rc)
242 goto out;
243
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700244 value = max(this_sleep_khz, peer_sleep_khz);
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700245 rc = clk_rpmrs_set_rate_sleep(r, value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247
248out:
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700249 mutex_unlock(&rpm_clock_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250
251 return rc;
252}
253
Vikram Mulukutla32a0f462013-02-20 15:10:40 -0800254static int rpm_branch_clk_set_rate(struct clk *clk, unsigned long rate)
255{
256 if (rate == clk->rate)
257 return 0;
258
259 return -EPERM;
260}
261
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700262static unsigned long rpm_clk_get_rate(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263{
264 struct rpm_clk *r = to_rpm_clk(clk);
Vikram Mulukutla0f44f922012-05-16 21:52:26 -0700265 if (r->rpmrs_data->get_rate_fn)
266 return r->rpmrs_data->get_rate_fn(r);
267 else
Vikram Mulukutla9505c3c2012-06-18 19:08:40 -0700268 return clk->rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269}
270
271static int rpm_clk_is_enabled(struct clk *clk)
272{
Vikram Mulukutla120299a2013-06-14 15:28:10 -0700273 struct rpm_clk *r = to_rpm_clk(clk);
274 return r->rpmrs_data->is_enabled(r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275}
276
Matt Wagantall9de3bfb2011-11-03 20:13:12 -0700277static long rpm_clk_round_rate(struct clk *clk, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278{
279 /* Not supported. */
280 return rate;
281}
282
283static bool rpm_clk_is_local(struct clk *clk)
284{
285 return false;
286}
287
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700288static enum handoff rpm_clk_handoff(struct clk *clk)
289{
290 struct rpm_clk *r = to_rpm_clk(clk);
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700291 int rc;
292
293 /*
294 * Querying an RPM clock's status will return 0 unless the clock's
295 * rate has previously been set through the RPM. When handing off,
296 * assume these clocks are enabled (unless the RPM call fails) so
297 * child clocks of these RPM clocks can still be handed off.
298 */
Vikram Mulukutlae2300de2012-08-10 04:58:50 -0700299 rc = r->rpmrs_data->handoff_fn(r);
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700300 if (rc < 0)
301 return HANDOFF_DISABLED_CLK;
302
Saravana Kannanc85ecf92013-01-21 17:58:35 -0800303 /*
304 * Since RPM handoff code may update the software rate of the clock by
305 * querying the RPM, we need to make sure our request to RPM now
306 * matches the software rate of the clock. When we send the request
307 * to RPM, we also need to update any other state info we would
308 * normally update. So, call the appropriate clock function instead
309 * of directly using the RPM driver APIs.
310 */
311 rc = rpm_clk_prepare(clk);
312 if (rc < 0)
313 return HANDOFF_DISABLED_CLK;
314
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700315 return HANDOFF_ENABLED_CLK;
316}
317
Tianyi Gou71340912012-10-09 19:35:01 -0700318#define RPM_MISC_CLK_TYPE 0x306b6c63
319#define RPM_SCALING_ENABLE_ID 0x2
320
321void enable_rpm_scaling(void)
322{
323 int rc, value = 0x1;
324 struct msm_rpm_kvp kvp = {
325 .key = RPM_SMD_KEY_ENABLE,
326 .data = (void *)&value,
327 .length = sizeof(value),
328 };
329
330 rc = msm_rpm_send_message_noirq(MSM_RPM_CTX_SLEEP_SET,
331 RPM_MISC_CLK_TYPE, RPM_SCALING_ENABLE_ID, &kvp, 1);
332 WARN(rc < 0, "RPM clock scaling (sleep set) did not enable!\n");
333
334 rc = msm_rpm_send_message_noirq(MSM_RPM_CTX_ACTIVE_SET,
335 RPM_MISC_CLK_TYPE, RPM_SCALING_ENABLE_ID, &kvp, 1);
336 WARN(rc < 0, "RPM clock scaling (active set) did not enable!\n");
337}
338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339struct clk_ops clk_ops_rpm = {
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700340 .prepare = rpm_clk_prepare,
341 .unprepare = rpm_clk_unprepare,
Matt Wagantall77952c42011-11-08 18:45:48 -0800342 .set_rate = rpm_clk_set_rate,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 .get_rate = rpm_clk_get_rate,
344 .is_enabled = rpm_clk_is_enabled,
345 .round_rate = rpm_clk_round_rate,
346 .is_local = rpm_clk_is_local,
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700347 .handoff = rpm_clk_handoff,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348};
Stephen Boyda6835112012-01-26 14:40:05 -0800349
350struct clk_ops clk_ops_rpm_branch = {
Stephen Boyd27fae3b2012-09-21 11:00:39 -0700351 .prepare = rpm_clk_prepare,
352 .unprepare = rpm_clk_unprepare,
Vikram Mulukutla32a0f462013-02-20 15:10:40 -0800353 .set_rate = rpm_branch_clk_set_rate,
Stephen Boyda6835112012-01-26 14:40:05 -0800354 .is_local = rpm_clk_is_local,
Matt Wagantall39c94ef2012-05-16 13:24:41 -0700355 .handoff = rpm_clk_handoff,
Stephen Boyda6835112012-01-26 14:40:05 -0800356};