Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 15 | #include <linux/mutex.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 16 | #include <linux/clk.h> |
| 17 | |
| 18 | #include "clock.h" |
| 19 | #include "clock-voter.h" |
| 20 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 21 | static DEFINE_MUTEX(voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | |
| 23 | /* Aggregate the rate of clocks that are currently on. */ |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 24 | static unsigned long voter_clk_aggregate_rate(const struct clk *parent) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | { |
| 26 | struct clk *clk; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 27 | unsigned long rate = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
| 29 | list_for_each_entry(clk, &parent->children, siblings) { |
| 30 | struct clk_voter *v = to_clk_voter(clk); |
| 31 | if (v->enabled) |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 32 | rate = max(clk->rate, rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | } |
| 34 | return rate; |
| 35 | } |
| 36 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 37 | static int voter_clk_set_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | { |
| 39 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | struct clk *clkp; |
| 41 | struct clk_voter *clkh, *v = to_clk_voter(clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 42 | unsigned long cur_rate, new_rate, other_rate = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 44 | mutex_lock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | |
| 46 | if (v->enabled) { |
| 47 | struct clk *parent = v->parent; |
| 48 | |
| 49 | /* |
| 50 | * Get the aggregate rate without this clock's vote and update |
| 51 | * if the new rate is different than the current rate |
| 52 | */ |
| 53 | list_for_each_entry(clkp, &parent->children, siblings) { |
| 54 | clkh = to_clk_voter(clkp); |
| 55 | if (clkh->enabled && clkh != v) |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 56 | other_rate = max(clkp->rate, other_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 59 | cur_rate = max(other_rate, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 60 | new_rate = max(other_rate, rate); |
| 61 | |
| 62 | if (new_rate != cur_rate) { |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 63 | ret = clk_set_rate(parent, new_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 64 | if (ret) |
| 65 | goto unlock; |
| 66 | } |
| 67 | } |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 68 | clk->rate = rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | unlock: |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 70 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 75 | static int voter_clk_prepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 76 | { |
Saravana Kannan | 4d77a4f | 2011-09-26 19:02:02 -0700 | [diff] [blame] | 77 | int ret = 0; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 78 | unsigned long cur_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | struct clk *parent; |
| 80 | struct clk_voter *v = to_clk_voter(clk); |
| 81 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 82 | mutex_lock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | parent = v->parent; |
| 84 | |
| 85 | /* |
| 86 | * Increase the rate if this clock is voting for a higher rate |
| 87 | * than the current rate. |
| 88 | */ |
| 89 | cur_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 90 | if (clk->rate > cur_rate) { |
| 91 | ret = clk_set_rate(parent, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | if (ret) |
| 93 | goto out; |
| 94 | } |
| 95 | v->enabled = true; |
| 96 | out: |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 97 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 102 | static void voter_clk_unprepare(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 103 | { |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 104 | unsigned long cur_rate, new_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 105 | struct clk *parent; |
| 106 | struct clk_voter *v = to_clk_voter(clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 108 | mutex_lock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | parent = v->parent; |
| 110 | |
| 111 | /* |
| 112 | * Decrease the rate if this clock was the only one voting for |
| 113 | * the highest rate. |
| 114 | */ |
| 115 | v->enabled = false; |
| 116 | new_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame] | 117 | cur_rate = max(new_rate, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | |
| 119 | if (new_rate < cur_rate) |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 120 | clk_set_rate(parent, new_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 121 | |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 122 | mutex_unlock(&voter_clk_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | static int voter_clk_is_enabled(struct clk *clk) |
| 126 | { |
| 127 | struct clk_voter *v = to_clk_voter(clk); |
| 128 | return v->enabled; |
| 129 | } |
| 130 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 131 | static long voter_clk_round_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 132 | { |
| 133 | struct clk_voter *v = to_clk_voter(clk); |
| 134 | return clk_round_rate(v->parent, rate); |
| 135 | } |
| 136 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 137 | static struct clk *voter_clk_get_parent(struct clk *clk) |
| 138 | { |
| 139 | struct clk_voter *v = to_clk_voter(clk); |
| 140 | return v->parent; |
| 141 | } |
| 142 | |
| 143 | static bool voter_clk_is_local(struct clk *clk) |
| 144 | { |
| 145 | return true; |
| 146 | } |
| 147 | |
Matt Wagantall | bdd6e90 | 2012-05-09 20:48:25 -0700 | [diff] [blame] | 148 | static enum handoff voter_clk_handoff(struct clk *clk) |
Matt Wagantall | 35e78fc | 2012-04-05 14:18:44 -0700 | [diff] [blame] | 149 | { |
| 150 | /* Apply default rate vote */ |
| 151 | if (clk->rate) |
| 152 | return HANDOFF_ENABLED_CLK; |
| 153 | |
| 154 | return HANDOFF_DISABLED_CLK; |
| 155 | } |
| 156 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | struct clk_ops clk_ops_voter = { |
Stephen Boyd | 2c2875f | 2012-01-24 17:36:34 -0800 | [diff] [blame^] | 158 | .prepare = voter_clk_prepare, |
| 159 | .unprepare = voter_clk_unprepare, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | .set_rate = voter_clk_set_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | .is_enabled = voter_clk_is_enabled, |
| 162 | .round_rate = voter_clk_round_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 163 | .get_parent = voter_clk_get_parent, |
| 164 | .is_local = voter_clk_is_local, |
Matt Wagantall | bdd6e90 | 2012-05-09 20:48:25 -0700 | [diff] [blame] | 165 | .handoff = voter_clk_handoff, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | }; |