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> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/clk.h> |
| 17 | |
| 18 | #include "clock.h" |
| 19 | #include "clock-voter.h" |
| 20 | |
| 21 | static DEFINE_SPINLOCK(voter_clk_lock); |
| 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; |
| 40 | unsigned long flags; |
| 41 | struct clk *clkp; |
| 42 | struct clk_voter *clkh, *v = to_clk_voter(clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 43 | unsigned long cur_rate, new_rate, other_rate = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | |
| 45 | spin_lock_irqsave(&voter_clk_lock, flags); |
| 46 | |
| 47 | if (v->enabled) { |
| 48 | struct clk *parent = v->parent; |
| 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: |
| 71 | spin_unlock_irqrestore(&voter_clk_lock, flags); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int voter_clk_enable(struct clk *clk) |
| 77 | { |
Saravana Kannan | 4d77a4f | 2011-09-26 19:02:02 -0700 | [diff] [blame] | 78 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | unsigned long flags; |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 80 | unsigned long cur_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | struct clk *parent; |
| 82 | struct clk_voter *v = to_clk_voter(clk); |
| 83 | |
| 84 | spin_lock_irqsave(&voter_clk_lock, flags); |
| 85 | parent = v->parent; |
| 86 | |
| 87 | /* |
| 88 | * Increase the rate if this clock is voting for a higher rate |
| 89 | * than the current rate. |
| 90 | */ |
| 91 | cur_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame^] | 92 | if (clk->rate > cur_rate) { |
| 93 | ret = clk_set_rate(parent, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 94 | if (ret) |
| 95 | goto out; |
| 96 | } |
| 97 | v->enabled = true; |
| 98 | out: |
| 99 | spin_unlock_irqrestore(&voter_clk_lock, flags); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static void voter_clk_disable(struct clk *clk) |
| 105 | { |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 106 | unsigned long flags, cur_rate, new_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | struct clk *parent; |
| 108 | struct clk_voter *v = to_clk_voter(clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | |
| 110 | spin_lock_irqsave(&voter_clk_lock, flags); |
| 111 | parent = v->parent; |
| 112 | |
| 113 | /* |
| 114 | * Decrease the rate if this clock was the only one voting for |
| 115 | * the highest rate. |
| 116 | */ |
| 117 | v->enabled = false; |
| 118 | new_rate = voter_clk_aggregate_rate(parent); |
Matt Wagantall | dd63ac3 | 2012-04-05 13:17:31 -0700 | [diff] [blame^] | 119 | cur_rate = max(new_rate, clk->rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 120 | |
| 121 | if (new_rate < cur_rate) |
Matt Wagantall | 8e6126f | 2011-11-08 13:34:19 -0800 | [diff] [blame] | 122 | clk_set_rate(parent, new_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | |
| 124 | spin_unlock_irqrestore(&voter_clk_lock, flags); |
| 125 | } |
| 126 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | static int voter_clk_is_enabled(struct clk *clk) |
| 128 | { |
| 129 | struct clk_voter *v = to_clk_voter(clk); |
| 130 | return v->enabled; |
| 131 | } |
| 132 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 133 | static long voter_clk_round_rate(struct clk *clk, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | { |
| 135 | struct clk_voter *v = to_clk_voter(clk); |
| 136 | return clk_round_rate(v->parent, rate); |
| 137 | } |
| 138 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | static struct clk *voter_clk_get_parent(struct clk *clk) |
| 140 | { |
| 141 | struct clk_voter *v = to_clk_voter(clk); |
| 142 | return v->parent; |
| 143 | } |
| 144 | |
| 145 | static bool voter_clk_is_local(struct clk *clk) |
| 146 | { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | struct clk_ops clk_ops_voter = { |
| 151 | .enable = voter_clk_enable, |
| 152 | .disable = voter_clk_disable, |
| 153 | .set_rate = voter_clk_set_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | .is_enabled = voter_clk_is_enabled, |
| 155 | .round_rate = voter_clk_round_rate, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | .get_parent = voter_clk_get_parent, |
| 157 | .is_local = voter_clk_is_local, |
| 158 | }; |