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 | #include <linux/err.h> |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/clk.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 | #include "clock-voter.h" |
| 18 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 19 | static DEFINE_MUTEX(voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | |
| 21 | /* Aggregate the rate of clocks that are currently on. */ |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 22 | static unsigned long voter_clk_aggregate_rate(const struct clk *parent) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | { |
| 24 | struct clk *clk; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 25 | unsigned long rate = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | |
| 27 | list_for_each_entry(clk, &parent->children, siblings) { |
| 28 | struct clk_voter *v = to_clk_voter(clk); |
| 29 | if (v->enabled) |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 30 | rate = max(clk->rate, rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | } |
| 32 | return rate; |
| 33 | } |
| 34 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 35 | static int voter_clk_set_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | { |
| 37 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | struct clk *clkp; |
| 39 | struct clk_voter *clkh, *v = to_clk_voter(clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 40 | unsigned long cur_rate, new_rate, other_rate = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 41 | |
Vikram Mulukutla | a1b2007 | 2013-02-04 15:07:35 -0800 | [diff] [blame] | 42 | if (v->is_branch) |
| 43 | return 0; |
| 44 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 45 | mutex_lock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 46 | |
| 47 | if (v->enabled) { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 48 | struct clk *parent = clk->parent; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Get the aggregate rate without this clock's vote and update |
| 52 | * if the new rate is different than the current rate |
| 53 | */ |
| 54 | list_for_each_entry(clkp, &parent->children, siblings) { |
| 55 | clkh = to_clk_voter(clkp); |
| 56 | if (clkh->enabled && clkh != v) |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 57 | other_rate = max(clkp->rate, other_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 60 | cur_rate = max(other_rate, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 61 | new_rate = max(other_rate, rate); |
| 62 | |
| 63 | if (new_rate != cur_rate) { |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 64 | ret = clk_set_rate(parent, new_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | if (ret) |
| 66 | goto unlock; |
| 67 | } |
| 68 | } |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 69 | clk->rate = rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | unlock: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 71 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 76 | static int voter_clk_prepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | { |
Saravana Kannan | 4d77a4f | 2011-09-26 19:02:02 -0700 | [diff] [blame] | 78 | int ret = 0; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 79 | unsigned long cur_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 80 | struct clk *parent; |
| 81 | struct clk_voter *v = to_clk_voter(clk); |
| 82 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 83 | mutex_lock(&voter_clk_lock); |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 84 | parent = clk->parent; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 85 | |
Patrick Daly | 94517d6 | 2013-03-22 14:37:24 -0700 | [diff] [blame] | 86 | if (v->is_branch) { |
| 87 | v->enabled = true; |
| 88 | goto out; |
| 89 | } |
| 90 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Increase the rate if this clock is voting for a higher rate |
| 93 | * than the current rate. |
| 94 | */ |
| 95 | cur_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 96 | if (clk->rate > cur_rate) { |
| 97 | ret = clk_set_rate(parent, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | if (ret) |
| 99 | goto out; |
| 100 | } |
| 101 | v->enabled = true; |
| 102 | out: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 103 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 108 | static void voter_clk_unprepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | { |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 110 | unsigned long cur_rate, new_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | struct clk *parent; |
| 112 | struct clk_voter *v = to_clk_voter(clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 113 | |
Vikram Mulukutla | a1b2007 | 2013-02-04 15:07:35 -0800 | [diff] [blame] | 114 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 115 | mutex_lock(&voter_clk_lock); |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 116 | parent = clk->parent; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * Decrease the rate if this clock was the only one voting for |
| 120 | * the highest rate. |
| 121 | */ |
| 122 | v->enabled = false; |
Patrick Daly | 94517d6 | 2013-03-22 14:37:24 -0700 | [diff] [blame] | 123 | if (v->is_branch) |
| 124 | goto out; |
| 125 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | new_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 127 | cur_rate = max(new_rate, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | |
| 129 | if (new_rate < cur_rate) |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 130 | clk_set_rate(parent, new_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 131 | |
Patrick Daly | 94517d6 | 2013-03-22 14:37:24 -0700 | [diff] [blame] | 132 | out: |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 133 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 136 | static int voter_clk_is_enabled(struct clk *clk) |
| 137 | { |
| 138 | struct clk_voter *v = to_clk_voter(clk); |
| 139 | return v->enabled; |
| 140 | } |
| 141 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 142 | static long voter_clk_round_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | { |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 144 | return clk_round_rate(clk->parent, rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static bool voter_clk_is_local(struct clk *clk) |
| 148 | { |
| 149 | return true; |
| 150 | } |
| 151 | |
Matt Wagantall | bdd6e90 | 2012-05-09 20:48:25 -0700 | [diff] [blame] | 152 | static enum handoff voter_clk_handoff(struct clk *clk) |
Matt Wagantall | 35e78fc | 2012-04-05 14:18:44 -0700 | [diff] [blame] | 153 | { |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 154 | if (!clk->rate) |
| 155 | return HANDOFF_DISABLED_CLK; |
Matt Wagantall | 35e78fc | 2012-04-05 14:18:44 -0700 | [diff] [blame] | 156 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 157 | /* |
| 158 | * Send the default rate to the parent if necessary and update the |
| 159 | * software state of the voter clock. |
| 160 | */ |
| 161 | if (voter_clk_prepare(clk) < 0) |
| 162 | return HANDOFF_DISABLED_CLK; |
| 163 | |
| 164 | return HANDOFF_ENABLED_CLK; |
Matt Wagantall | 35e78fc | 2012-04-05 14:18:44 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | struct clk_ops clk_ops_voter = { |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 168 | .prepare = voter_clk_prepare, |
| 169 | .unprepare = voter_clk_unprepare, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 170 | .set_rate = voter_clk_set_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | .is_enabled = voter_clk_is_enabled, |
| 172 | .round_rate = voter_clk_round_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | .is_local = voter_clk_is_local, |
Matt Wagantall | bdd6e90 | 2012-05-09 20:48:25 -0700 | [diff] [blame] | 174 | .handoff = voter_clk_handoff, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 175 | }; |