Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. 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> |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 16 | #include <mach/clk-provider.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 17 | |
| 18 | #include "rpm_resources.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | #include "clock-rpm.h" |
| 20 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 21 | #define __clk_rpmrs_set_rate(r, value, ctx) \ |
| 22 | ((r)->rpmrs_data->set_rate_fn((r), (value), (ctx))) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 23 | |
| 24 | #define clk_rpmrs_set_rate_sleep(r, value) \ |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 25 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_sleep_id) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 26 | |
| 27 | #define clk_rpmrs_set_rate_active(r, value) \ |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 28 | __clk_rpmrs_set_rate((r), (value), (r)->rpmrs_data->ctx_active_id) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 29 | |
| 30 | static int clk_rpmrs_set_rate(struct rpm_clk *r, uint32_t value, |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 31 | uint32_t context) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 32 | { |
| 33 | struct msm_rpm_iv_pair iv = { |
| 34 | .id = r->rpm_clk_id, |
| 35 | .value = value, |
| 36 | }; |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 37 | return msm_rpmrs_set(context, &iv, 1); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static 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 Katragadda | 92862ca | 2013-06-21 14:36:18 -0700 | [diff] [blame] | 45 | return (rc < 0) ? rc : iv.value * 1000; |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 48 | static 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 Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 56 | if (!r->branch) |
Deepak Katragadda | 92862ca | 2013-06-21 14:36:18 -0700 | [diff] [blame] | 57 | r->c.rate = iv.value * 1000; |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 62 | static int clk_rpmrs_is_enabled(struct rpm_clk *r) |
| 63 | { |
| 64 | return !!clk_rpmrs_get_rate(r); |
| 65 | } |
| 66 | |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 67 | static int clk_rpmrs_set_rate_smd(struct rpm_clk *r, uint32_t value, |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 68 | uint32_t context) |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 69 | { |
| 70 | struct msm_rpm_kvp kvp = { |
Vikram Mulukutla | 4aba6c0 | 2012-07-10 11:12:05 -0700 | [diff] [blame] | 71 | .key = r->rpm_key, |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 72 | .data = (void *)&value, |
| 73 | .length = sizeof(value), |
| 74 | }; |
| 75 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 76 | return msm_rpm_send_message(context, r->rpm_res_type, r->rpm_clk_id, |
| 77 | &kvp, 1); |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 80 | static int clk_rpmrs_handoff_smd(struct rpm_clk *r) |
| 81 | { |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 82 | if (!r->branch) |
| 83 | r->c.rate = INT_MAX; |
Saravana Kannan | ca1528d | 2012-10-11 22:40:12 -0700 | [diff] [blame] | 84 | |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 88 | static int clk_rpmrs_is_enabled_smd(struct rpm_clk *r) |
| 89 | { |
| 90 | return !!r->c.prepare_count; |
| 91 | } |
| 92 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 93 | struct clk_rpmrs_data { |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 94 | int (*set_rate_fn)(struct rpm_clk *r, uint32_t value, uint32_t context); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 95 | int (*get_rate_fn)(struct rpm_clk *r); |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 96 | int (*handoff_fn)(struct rpm_clk *r); |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 97 | int (*is_enabled)(struct rpm_clk *r); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 98 | int ctx_active_id; |
| 99 | int ctx_sleep_id; |
| 100 | }; |
| 101 | |
| 102 | struct clk_rpmrs_data clk_rpmrs_data = { |
| 103 | .set_rate_fn = clk_rpmrs_set_rate, |
| 104 | .get_rate_fn = clk_rpmrs_get_rate, |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 105 | .handoff_fn = clk_rpmrs_handoff, |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 106 | .is_enabled = clk_rpmrs_is_enabled, |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 107 | .ctx_active_id = MSM_RPM_CTX_SET_0, |
| 108 | .ctx_sleep_id = MSM_RPM_CTX_SET_SLEEP, |
| 109 | }; |
| 110 | |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 111 | struct clk_rpmrs_data clk_rpmrs_data_smd = { |
| 112 | .set_rate_fn = clk_rpmrs_set_rate_smd, |
Vikram Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 113 | .handoff_fn = clk_rpmrs_handoff_smd, |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 114 | .is_enabled = clk_rpmrs_is_enabled_smd, |
Vikram Mulukutla | a085dc8 | 2012-05-18 11:21:44 -0700 | [diff] [blame] | 115 | .ctx_active_id = MSM_RPM_CTX_ACTIVE_SET, |
| 116 | .ctx_sleep_id = MSM_RPM_CTX_SLEEP_SET, |
| 117 | }; |
| 118 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 119 | static DEFINE_MUTEX(rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 120 | |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 121 | static 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 Katragadda | 92862ca | 2013-06-21 14:36:18 -0700 | [diff] [blame] | 125 | *active_khz = DIV_ROUND_UP(rate, 1000); |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 126 | |
| 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 Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 137 | static int rpm_clk_prepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | struct rpm_clk *r = to_rpm_clk(clk); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 140 | uint32_t value; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 141 | int rc = 0; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 142 | unsigned long this_khz, this_sleep_khz; |
| 143 | unsigned long peer_khz = 0, peer_sleep_khz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | struct rpm_clk *peer = r->peer; |
| 145 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 146 | mutex_lock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 147 | |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 148 | to_active_sleep_khz(r, r->c.rate, &this_khz, &this_sleep_khz); |
| 149 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 150 | /* Don't send requests to the RPM if the rate has not been set. */ |
| 151 | if (this_khz == 0) |
| 152 | goto out; |
| 153 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | /* Take peer clock's rate into account only if it's enabled. */ |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 155 | if (peer->enabled) |
| 156 | to_active_sleep_khz(peer, peer->c.rate, |
| 157 | &peer_khz, &peer_sleep_khz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 159 | value = max(this_khz, peer_khz); |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 160 | if (r->branch) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 161 | value = !!value; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 162 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 163 | rc = clk_rpmrs_set_rate_active(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | if (rc) |
| 165 | goto out; |
| 166 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 167 | value = max(this_sleep_khz, peer_sleep_khz); |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 168 | if (r->branch) |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 169 | value = !!value; |
| 170 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 171 | rc = clk_rpmrs_set_rate_sleep(r, value); |
Matt Wagantall | 735f01a | 2011-08-12 12:40:28 -0700 | [diff] [blame] | 172 | if (rc) { |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 173 | /* Undo the active set vote and restore it to peer_khz */ |
| 174 | value = peer_khz; |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 175 | rc = clk_rpmrs_set_rate_active(r, value); |
Matt Wagantall | 735f01a | 2011-08-12 12:40:28 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 178 | out: |
| 179 | if (!rc) |
| 180 | r->enabled = true; |
| 181 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 182 | mutex_unlock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | |
| 184 | return rc; |
| 185 | } |
| 186 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 187 | static void rpm_clk_unprepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | struct rpm_clk *r = to_rpm_clk(clk); |
| 190 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 191 | mutex_lock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 193 | if (r->c.rate) { |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 194 | uint32_t value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | struct rpm_clk *peer = r->peer; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 196 | unsigned long peer_khz = 0, peer_sleep_khz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | int rc; |
| 198 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 199 | /* Take peer clock's rate into account only if it's enabled. */ |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 200 | if (peer->enabled) |
| 201 | to_active_sleep_khz(peer, peer->c.rate, |
| 202 | &peer_khz, &peer_sleep_khz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 204 | value = r->branch ? !!peer_khz : peer_khz; |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 205 | rc = clk_rpmrs_set_rate_active(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | if (rc) |
| 207 | goto out; |
| 208 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 209 | value = r->branch ? !!peer_sleep_khz : peer_sleep_khz; |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 210 | rc = clk_rpmrs_set_rate_sleep(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | } |
| 212 | r->enabled = false; |
| 213 | out: |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 214 | mutex_unlock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | |
| 216 | return; |
| 217 | } |
| 218 | |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 219 | static int rpm_clk_set_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | struct rpm_clk *r = to_rpm_clk(clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 222 | unsigned long this_khz, this_sleep_khz; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | int rc = 0; |
| 224 | |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 225 | mutex_lock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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 | |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 232 | to_active_sleep_khz(r, rate, &this_khz, &this_sleep_khz); |
| 233 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 234 | /* Take peer clock's rate into account only if it's enabled. */ |
Tianyi Gou | 88fe5c2 | 2013-03-27 15:15:29 -0700 | [diff] [blame] | 235 | if (peer->enabled) |
| 236 | to_active_sleep_khz(peer, peer->c.rate, |
| 237 | &peer_khz, &peer_sleep_khz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 238 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 239 | value = max(this_khz, peer_khz); |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 240 | rc = clk_rpmrs_set_rate_active(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 241 | if (rc) |
| 242 | goto out; |
| 243 | |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 244 | value = max(this_sleep_khz, peer_sleep_khz); |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 245 | rc = clk_rpmrs_set_rate_sleep(r, value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 246 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | |
| 248 | out: |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 249 | mutex_unlock(&rpm_clock_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
Vikram Mulukutla | 32a0f46 | 2013-02-20 15:10:40 -0800 | [diff] [blame] | 254 | static 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 Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 262 | static unsigned long rpm_clk_get_rate(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 263 | { |
| 264 | struct rpm_clk *r = to_rpm_clk(clk); |
Vikram Mulukutla | 0f44f92 | 2012-05-16 21:52:26 -0700 | [diff] [blame] | 265 | if (r->rpmrs_data->get_rate_fn) |
| 266 | return r->rpmrs_data->get_rate_fn(r); |
| 267 | else |
Vikram Mulukutla | 9505c3c | 2012-06-18 19:08:40 -0700 | [diff] [blame] | 268 | return clk->rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int rpm_clk_is_enabled(struct clk *clk) |
| 272 | { |
Vikram Mulukutla | 120299a | 2013-06-14 15:28:10 -0700 | [diff] [blame] | 273 | struct rpm_clk *r = to_rpm_clk(clk); |
| 274 | return r->rpmrs_data->is_enabled(r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 277 | static long rpm_clk_round_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | { |
| 279 | /* Not supported. */ |
| 280 | return rate; |
| 281 | } |
| 282 | |
| 283 | static bool rpm_clk_is_local(struct clk *clk) |
| 284 | { |
| 285 | return false; |
| 286 | } |
| 287 | |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 288 | static enum handoff rpm_clk_handoff(struct clk *clk) |
| 289 | { |
| 290 | struct rpm_clk *r = to_rpm_clk(clk); |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 291 | 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 Mulukutla | e2300de | 2012-08-10 04:58:50 -0700 | [diff] [blame] | 299 | rc = r->rpmrs_data->handoff_fn(r); |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 300 | if (rc < 0) |
| 301 | return HANDOFF_DISABLED_CLK; |
| 302 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 303 | /* |
| 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 Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 315 | return HANDOFF_ENABLED_CLK; |
| 316 | } |
| 317 | |
Tianyi Gou | 7134091 | 2012-10-09 19:35:01 -0700 | [diff] [blame] | 318 | #define RPM_MISC_CLK_TYPE 0x306b6c63 |
| 319 | #define RPM_SCALING_ENABLE_ID 0x2 |
| 320 | |
| 321 | void 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | struct clk_ops clk_ops_rpm = { |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 340 | .prepare = rpm_clk_prepare, |
| 341 | .unprepare = rpm_clk_unprepare, |
Matt Wagantall | 77952c4 | 2011-11-08 18:45:48 -0800 | [diff] [blame] | 342 | .set_rate = rpm_clk_set_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 343 | .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 Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 347 | .handoff = rpm_clk_handoff, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 348 | }; |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 349 | |
| 350 | struct clk_ops clk_ops_rpm_branch = { |
Stephen Boyd | 27fae3b | 2012-09-21 11:00:39 -0700 | [diff] [blame] | 351 | .prepare = rpm_clk_prepare, |
| 352 | .unprepare = rpm_clk_unprepare, |
Vikram Mulukutla | 32a0f46 | 2013-02-20 15:10:40 -0800 | [diff] [blame] | 353 | .set_rate = rpm_branch_clk_set_rate, |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 354 | .is_local = rpm_clk_is_local, |
Matt Wagantall | 39c94ef | 2012-05-16 13:24:41 -0700 | [diff] [blame] | 355 | .handoff = rpm_clk_handoff, |
Stephen Boyd | a683511 | 2012-01-26 14:40:05 -0800 | [diff] [blame] | 356 | }; |