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