Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 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> |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 15 | #include <mach/clk-provider.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "rpm_resources.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include "clock-rpm.h" |
| 19 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 20 | #define __clk_rpmrs_set_rate(r, value, ctx, noirq) \ |
| 21 | ((r)->rpmrs_data->set_rate_fn((r), (value), (ctx), (noirq))) |
| 22 | |
| 23 | #define clk_rpmrs_set_rate_sleep(r, value) \ |
| 24 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_sleep_id, 0) |
| 25 | |
| 26 | #define clk_rpmrs_set_rate_sleep_noirq(r, value) \ |
| 27 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_sleep_id, 1) |
| 28 | |
| 29 | #define clk_rpmrs_set_rate_active(r, value) \ |
| 30 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_active_id, 0) |
| 31 | |
| 32 | #define clk_rpmrs_set_rate_active_noirq(r, value) \ |
| 33 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_active_id, 1) |
| 34 | |
| 35 | static int clk_rpmrs_set_rate(struct rpm_clk *r, uint32_t value, |
| 36 | uint32_t context, int noirq) |
| 37 | { |
| 38 | struct msm_rpm_iv_pair iv = { |
| 39 | .id = r->rpm_clk_id, |
| 40 | .value = value, |
| 41 | }; |
| 42 | if (noirq) |
| 43 | return msm_rpmrs_set_noirq(context, &iv, 1); |
| 44 | else |
| 45 | return msm_rpmrs_set(context, &iv, 1); |
| 46 | } |
| 47 | |
| 48 | static int clk_rpmrs_get_rate(struct rpm_clk *r) |
| 49 | { |
| 50 | int rc; |
| 51 | struct msm_rpm_iv_pair iv = { .id = r->rpm_status_id, }; |
| 52 | rc = msm_rpm_get_status(&iv, 1); |
Stephen Boyd | c7fc3b1 | 2012-05-17 14:42:46 -0700 | [diff] [blame] | 53 | return (rc < 0) ? rc : iv.value * r->factor; |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 56 | static int clk_rpmrs_handoff(struct rpm_clk *r) |
| 57 | { |
| 58 | struct msm_rpm_iv_pair iv = { .id = r->rpm_status_id, }; |
| 59 | int rc = msm_rpm_get_status(&iv, 1); |
| 60 | |
| 61 | if (rc < 0) |
| 62 | return rc; |
| 63 | |
| 64 | if (!r->branch) { |
| 65 | r->last_set_khz = iv.value; |
Vikram Mulukutla | af72b71 | 2012-08-24 16:03:10 -0700 | [diff] [blame] | 66 | if (!r->active_only) |
| 67 | r->last_set_sleep_khz = iv.value; |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 68 | r->c.rate = iv.value * r->factor; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 74 | static int clk_rpmrs_set_rate_smd(struct rpm_clk *r, uint32_t value, |
| 75 | uint32_t context, int noirq) |
| 76 | { |
| 77 | struct msm_rpm_kvp kvp = { |
Vikram Mulukutla | 4aba6c0 | 2012-07-10 11:12:05 -0700 | [diff] [blame] | 78 | .key = r->rpm_key, |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 79 | .data = (void *)&value, |
| 80 | .length = sizeof(value), |
| 81 | }; |
| 82 | |
| 83 | if (noirq) |
| 84 | return msm_rpm_send_message_noirq(context, |
| 85 | r->rpm_res_type, r->rpm_clk_id, &kvp, 1); |
| 86 | else |
| 87 | return msm_rpm_send_message(context, r->rpm_res_type, |
| 88 | r->rpm_clk_id, &kvp, 1); |
| 89 | } |
| 90 | |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 91 | static int clk_rpmrs_handoff_smd(struct rpm_clk *r) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 96 | struct clk_rpmrs_data { |
| 97 | int (*set_rate_fn)(struct rpm_clk *r, uint32_t value, |
| 98 | uint32_t context, int noirq); |
| 99 | int (*get_rate_fn)(struct rpm_clk *r); |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 100 | int (*handoff_fn)(struct rpm_clk *r); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 101 | int ctx_active_id; |
| 102 | int ctx_sleep_id; |
| 103 | }; |
| 104 | |
| 105 | struct clk_rpmrs_data clk_rpmrs_data = { |
| 106 | .set_rate_fn = clk_rpmrs_set_rate, |
| 107 | .get_rate_fn = clk_rpmrs_get_rate, |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 108 | .handoff_fn = clk_rpmrs_handoff, |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 109 | .ctx_active_id = MSM_RPM_CTX_SET_0, |
| 110 | .ctx_sleep_id = MSM_RPM_CTX_SET_SLEEP, |
| 111 | }; |
| 112 | |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 113 | struct clk_rpmrs_data clk_rpmrs_data_smd = { |
| 114 | .set_rate_fn = clk_rpmrs_set_rate_smd, |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 115 | .handoff_fn = clk_rpmrs_handoff_smd, |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 116 | .ctx_active_id = MSM_RPM_CTX_ACTIVE_SET, |
| 117 | .ctx_sleep_id = MSM_RPM_CTX_SLEEP_SET, |
| 118 | }; |
| 119 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 120 | static DEFINE_SPINLOCK(rpm_clock_lock); |
| 121 | |
| 122 | static int rpm_clk_enable(struct clk *clk) |
| 123 | { |
| 124 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | struct rpm_clk *r = to_rpm_clk(clk); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 126 | uint32_t value; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 127 | int rc = 0; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 128 | unsigned long this_khz, this_sleep_khz; |
| 129 | unsigned long peer_khz = 0, peer_sleep_khz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | struct rpm_clk *peer = r->peer; |
| 131 | |
| 132 | spin_lock_irqsave(&rpm_clock_lock, flags); |
| 133 | |
| 134 | this_khz = r->last_set_khz; |
| 135 | /* Don't send requests to the RPM if the rate has not been set. */ |
| 136 | if (this_khz == 0) |
| 137 | goto out; |
| 138 | |
| 139 | this_sleep_khz = r->last_set_sleep_khz; |
| 140 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | /* Take peer clock's rate into account only if it's enabled. */ |
| 142 | if (peer->enabled) { |
| 143 | peer_khz = peer->last_set_khz; |
| 144 | peer_sleep_khz = peer->last_set_sleep_khz; |
| 145 | } |
| 146 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 147 | value = max(this_khz, peer_khz); |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 148 | if (r->branch) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 149 | value = !!value; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 150 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 151 | rc = clk_rpmrs_set_rate_active_noirq(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 152 | if (rc) |
| 153 | goto out; |
| 154 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 155 | value = max(this_sleep_khz, peer_sleep_khz); |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 156 | if (r->branch) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 157 | value = !!value; |
| 158 | |
| 159 | rc = clk_rpmrs_set_rate_sleep_noirq(r, value); |
Matt Wagantall | 735f01a | 2011-08-12 12:40:28 -0700 | [diff] [blame] | 160 | if (rc) { |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 161 | /* Undo the active set vote and restore it to peer_khz */ |
| 162 | value = peer_khz; |
| 163 | rc = clk_rpmrs_set_rate_active_noirq(r, value); |
Matt Wagantall | 735f01a | 2011-08-12 12:40:28 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | out: |
| 167 | if (!rc) |
| 168 | r->enabled = true; |
| 169 | |
| 170 | spin_unlock_irqrestore(&rpm_clock_lock, flags); |
| 171 | |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | static void rpm_clk_disable(struct clk *clk) |
| 176 | { |
| 177 | unsigned long flags; |
| 178 | struct rpm_clk *r = to_rpm_clk(clk); |
| 179 | |
| 180 | spin_lock_irqsave(&rpm_clock_lock, flags); |
| 181 | |
| 182 | if (r->last_set_khz) { |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 183 | uint32_t value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 184 | struct rpm_clk *peer = r->peer; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 185 | unsigned long peer_khz = 0, peer_sleep_khz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | int rc; |
| 187 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | /* Take peer clock's rate into account only if it's enabled. */ |
| 189 | if (peer->enabled) { |
| 190 | peer_khz = peer->last_set_khz; |
| 191 | peer_sleep_khz = peer->last_set_sleep_khz; |
| 192 | } |
| 193 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 194 | value = r->branch ? !!peer_khz : peer_khz; |
| 195 | rc = clk_rpmrs_set_rate_active_noirq(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | if (rc) |
| 197 | goto out; |
| 198 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 199 | value = r->branch ? !!peer_sleep_khz : peer_sleep_khz; |
| 200 | rc = clk_rpmrs_set_rate_sleep_noirq(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | } |
| 202 | r->enabled = false; |
| 203 | out: |
| 204 | spin_unlock_irqrestore(&rpm_clock_lock, flags); |
| 205 | |
| 206 | return; |
| 207 | } |
| 208 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 209 | static int rpm_clk_set_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 210 | { |
| 211 | unsigned long flags; |
| 212 | struct rpm_clk *r = to_rpm_clk(clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 213 | unsigned long this_khz, this_sleep_khz; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 214 | int rc = 0; |
| 215 | |
Stephen Boyd | c7fc3b1 | 2012-05-17 14:42:46 -0700 | [diff] [blame] | 216 | this_khz = DIV_ROUND_UP(rate, r->factor); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 217 | |
| 218 | spin_lock_irqsave(&rpm_clock_lock, flags); |
| 219 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | /* Active-only clocks don't care what the rate is during sleep. So, |
| 221 | * they vote for zero. */ |
| 222 | if (r->active_only) |
| 223 | this_sleep_khz = 0; |
| 224 | else |
| 225 | this_sleep_khz = this_khz; |
| 226 | |
| 227 | if (r->enabled) { |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 228 | uint32_t value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | struct rpm_clk *peer = r->peer; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 230 | unsigned long peer_khz = 0, peer_sleep_khz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 231 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 232 | /* Take peer clock's rate into account only if it's enabled. */ |
| 233 | if (peer->enabled) { |
| 234 | peer_khz = peer->last_set_khz; |
| 235 | peer_sleep_khz = peer->last_set_sleep_khz; |
| 236 | } |
| 237 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 238 | value = max(this_khz, peer_khz); |
| 239 | rc = clk_rpmrs_set_rate_active_noirq(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | if (rc) |
| 241 | goto out; |
| 242 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 243 | value = max(this_sleep_khz, peer_sleep_khz); |
| 244 | rc = clk_rpmrs_set_rate_sleep_noirq(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 245 | } |
| 246 | if (!rc) { |
| 247 | r->last_set_khz = this_khz; |
| 248 | r->last_set_sleep_khz = this_sleep_khz; |
| 249 | } |
| 250 | |
| 251 | out: |
| 252 | spin_unlock_irqrestore(&rpm_clock_lock, flags); |
| 253 | |
| 254 | return rc; |
| 255 | } |
| 256 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 257 | static unsigned long rpm_clk_get_rate(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 258 | { |
| 259 | struct rpm_clk *r = to_rpm_clk(clk); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 260 | if (r->rpmrs_data->get_rate_fn) |
| 261 | return r->rpmrs_data->get_rate_fn(r); |
| 262 | else |
Vikram Mulukutla | 9505c3c | 2012-06-18 19:08:40 -0700 | [diff] [blame] | 263 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static int rpm_clk_is_enabled(struct clk *clk) |
| 267 | { |
| 268 | return !!(rpm_clk_get_rate(clk)); |
| 269 | } |
| 270 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 271 | static long rpm_clk_round_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | { |
| 273 | /* Not supported. */ |
| 274 | return rate; |
| 275 | } |
| 276 | |
| 277 | static bool rpm_clk_is_local(struct clk *clk) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 282 | static enum handoff rpm_clk_handoff(struct clk *clk) |
| 283 | { |
| 284 | struct rpm_clk *r = to_rpm_clk(clk); |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 285 | int rc; |
| 286 | |
| 287 | /* |
| 288 | * Querying an RPM clock's status will return 0 unless the clock's |
| 289 | * rate has previously been set through the RPM. When handing off, |
| 290 | * assume these clocks are enabled (unless the RPM call fails) so |
| 291 | * child clocks of these RPM clocks can still be handed off. |
| 292 | */ |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 293 | rc = r->rpmrs_data->handoff_fn(r); |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 294 | if (rc < 0) |
| 295 | return HANDOFF_DISABLED_CLK; |
| 296 | |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 297 | return HANDOFF_ENABLED_CLK; |
| 298 | } |
| 299 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | struct clk_ops clk_ops_rpm = { |
| 301 | .enable = rpm_clk_enable, |
| 302 | .disable = rpm_clk_disable, |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 303 | .set_rate = rpm_clk_set_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | .get_rate = rpm_clk_get_rate, |
| 305 | .is_enabled = rpm_clk_is_enabled, |
| 306 | .round_rate = rpm_clk_round_rate, |
| 307 | .is_local = rpm_clk_is_local, |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 308 | .handoff = rpm_clk_handoff, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 309 | }; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 310 | |
| 311 | struct clk_ops clk_ops_rpm_branch = { |
| 312 | .enable = rpm_clk_enable, |
| 313 | .disable = rpm_clk_disable, |
| 314 | .is_local = rpm_clk_is_local, |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 315 | .handoff = rpm_clk_handoff, |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 316 | }; |