Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Maxime Ripard |
| 3 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk-provider.h> |
| 12 | |
| 13 | #include "ccu_gate.h" |
| 14 | #include "ccu_mult.h" |
| 15 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 16 | struct _ccu_mult { |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 17 | unsigned long mult, min, max; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 18 | }; |
| 19 | |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 20 | static void ccu_mult_find_best(unsigned long parent, unsigned long rate, |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 21 | struct _ccu_mult *mult) |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 22 | { |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 23 | int _mult; |
| 24 | |
| 25 | _mult = rate / parent; |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 26 | if (_mult < mult->min) |
| 27 | _mult = mult->min; |
| 28 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 29 | if (_mult > mult->max) |
| 30 | _mult = mult->max; |
| 31 | |
| 32 | mult->mult = _mult; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux, |
Maxime Ripard | 10a8d9b | 2017-05-17 09:40:31 +0200 | [diff] [blame] | 36 | struct clk_hw *parent, |
| 37 | unsigned long *parent_rate, |
| 38 | unsigned long rate, |
| 39 | void *data) |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 40 | { |
| 41 | struct ccu_mult *cm = data; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 42 | struct _ccu_mult _cm; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 43 | |
Maxime Ripard | c9520be | 2017-01-21 23:41:56 +0100 | [diff] [blame] | 44 | _cm.min = cm->mult.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 45 | |
| 46 | if (cm->mult.max) |
| 47 | _cm.max = cm->mult.max; |
| 48 | else |
| 49 | _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1; |
| 50 | |
Maxime Ripard | 10a8d9b | 2017-05-17 09:40:31 +0200 | [diff] [blame] | 51 | ccu_mult_find_best(*parent_rate, rate, &_cm); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 52 | |
Maxime Ripard | 10a8d9b | 2017-05-17 09:40:31 +0200 | [diff] [blame] | 53 | return *parent_rate * _cm.mult; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void ccu_mult_disable(struct clk_hw *hw) |
| 57 | { |
| 58 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 59 | |
| 60 | return ccu_gate_helper_disable(&cm->common, cm->enable); |
| 61 | } |
| 62 | |
| 63 | static int ccu_mult_enable(struct clk_hw *hw) |
| 64 | { |
| 65 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 66 | |
| 67 | return ccu_gate_helper_enable(&cm->common, cm->enable); |
| 68 | } |
| 69 | |
| 70 | static int ccu_mult_is_enabled(struct clk_hw *hw) |
| 71 | { |
| 72 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 73 | |
| 74 | return ccu_gate_helper_is_enabled(&cm->common, cm->enable); |
| 75 | } |
| 76 | |
| 77 | static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw, |
| 78 | unsigned long parent_rate) |
| 79 | { |
| 80 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 81 | unsigned long val; |
| 82 | u32 reg; |
| 83 | |
Maxime Ripard | d77e813 | 2016-10-13 12:44:55 +0200 | [diff] [blame] | 84 | if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac)) |
| 85 | return ccu_frac_helper_read_rate(&cm->common, &cm->frac); |
| 86 | |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 87 | reg = readl(cm->common.base + cm->common.reg); |
| 88 | val = reg >> cm->mult.shift; |
| 89 | val &= (1 << cm->mult.width) - 1; |
| 90 | |
Maxime Ripard | d754b15 | 2017-05-17 09:40:35 +0200 | [diff] [blame] | 91 | parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1, |
| 92 | parent_rate); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 93 | |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 94 | return parent_rate * (val + cm->mult.offset); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int ccu_mult_determine_rate(struct clk_hw *hw, |
| 98 | struct clk_rate_request *req) |
| 99 | { |
| 100 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 101 | |
| 102 | return ccu_mux_helper_determine_rate(&cm->common, &cm->mux, |
| 103 | req, ccu_mult_round_rate, cm); |
| 104 | } |
| 105 | |
| 106 | static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate, |
| 107 | unsigned long parent_rate) |
| 108 | { |
| 109 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 110 | struct _ccu_mult _cm; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 111 | unsigned long flags; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 112 | u32 reg; |
| 113 | |
Jernej Škrabec | 1e92ae6 | 2017-07-30 18:41:48 +0200 | [diff] [blame] | 114 | if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate)) { |
| 115 | ccu_frac_helper_enable(&cm->common, &cm->frac); |
| 116 | |
Jernej Škrabec | 1d42460 | 2017-07-30 18:41:50 +0200 | [diff] [blame] | 117 | return ccu_frac_helper_set_rate(&cm->common, &cm->frac, |
| 118 | rate, cm->lock); |
Jernej Škrabec | 1e92ae6 | 2017-07-30 18:41:48 +0200 | [diff] [blame] | 119 | } else { |
Maxime Ripard | d77e813 | 2016-10-13 12:44:55 +0200 | [diff] [blame] | 120 | ccu_frac_helper_disable(&cm->common, &cm->frac); |
Jernej Škrabec | 1e92ae6 | 2017-07-30 18:41:48 +0200 | [diff] [blame] | 121 | } |
Maxime Ripard | d77e813 | 2016-10-13 12:44:55 +0200 | [diff] [blame] | 122 | |
Maxime Ripard | d754b15 | 2017-05-17 09:40:35 +0200 | [diff] [blame] | 123 | parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1, |
| 124 | parent_rate); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 125 | |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 126 | _cm.min = cm->mult.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 127 | |
| 128 | if (cm->mult.max) |
| 129 | _cm.max = cm->mult.max; |
| 130 | else |
| 131 | _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1; |
| 132 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 133 | ccu_mult_find_best(parent_rate, rate, &_cm); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 134 | |
| 135 | spin_lock_irqsave(cm->common.lock, flags); |
| 136 | |
| 137 | reg = readl(cm->common.base + cm->common.reg); |
| 138 | reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift); |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 139 | reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 140 | |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 141 | writel(reg, cm->common.base + cm->common.reg); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 142 | |
| 143 | spin_unlock_irqrestore(cm->common.lock, flags); |
| 144 | |
Chen-Yu Tsai | cf71901 | 2017-04-05 14:37:42 +0800 | [diff] [blame] | 145 | ccu_helper_wait_for_lock(&cm->common, cm->lock); |
| 146 | |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static u8 ccu_mult_get_parent(struct clk_hw *hw) |
| 151 | { |
| 152 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 153 | |
| 154 | return ccu_mux_helper_get_parent(&cm->common, &cm->mux); |
| 155 | } |
| 156 | |
| 157 | static int ccu_mult_set_parent(struct clk_hw *hw, u8 index) |
| 158 | { |
| 159 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 160 | |
| 161 | return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index); |
| 162 | } |
| 163 | |
| 164 | const struct clk_ops ccu_mult_ops = { |
| 165 | .disable = ccu_mult_disable, |
| 166 | .enable = ccu_mult_enable, |
| 167 | .is_enabled = ccu_mult_is_enabled, |
| 168 | |
| 169 | .get_parent = ccu_mult_get_parent, |
| 170 | .set_parent = ccu_mult_set_parent, |
| 171 | |
| 172 | .determine_rate = ccu_mult_determine_rate, |
| 173 | .recalc_rate = ccu_mult_recalc_rate, |
| 174 | .set_rate = ccu_mult_set_rate, |
| 175 | }; |