Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +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> |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 12 | |
| 13 | #include "ccu_frac.h" |
| 14 | #include "ccu_gate.h" |
| 15 | #include "ccu_nm.h" |
| 16 | |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 17 | struct _ccu_nm { |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 18 | unsigned long n, min_n, max_n; |
| 19 | unsigned long m, min_m, max_m; |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
Jernej Skrabec | 65b6657 | 2018-11-04 19:26:40 +0100 | [diff] [blame^] | 22 | static unsigned long ccu_nm_calc_rate(unsigned long parent, |
| 23 | unsigned long n, unsigned long m) |
| 24 | { |
| 25 | u64 rate = parent; |
| 26 | |
| 27 | rate *= n; |
| 28 | do_div(rate, m); |
| 29 | |
| 30 | return rate; |
| 31 | } |
| 32 | |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 33 | static void ccu_nm_find_best(unsigned long parent, unsigned long rate, |
| 34 | struct _ccu_nm *nm) |
| 35 | { |
| 36 | unsigned long best_rate = 0; |
| 37 | unsigned long best_n = 0, best_m = 0; |
| 38 | unsigned long _n, _m; |
| 39 | |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 40 | for (_n = nm->min_n; _n <= nm->max_n; _n++) { |
| 41 | for (_m = nm->min_m; _m <= nm->max_m; _m++) { |
Jernej Skrabec | 65b6657 | 2018-11-04 19:26:40 +0100 | [diff] [blame^] | 42 | unsigned long tmp_rate = ccu_nm_calc_rate(parent, |
| 43 | _n, _m); |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 44 | |
| 45 | if (tmp_rate > rate) |
| 46 | continue; |
| 47 | |
| 48 | if ((rate - tmp_rate) < (rate - best_rate)) { |
| 49 | best_rate = tmp_rate; |
| 50 | best_n = _n; |
| 51 | best_m = _m; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | nm->n = best_n; |
| 57 | nm->m = best_m; |
| 58 | } |
| 59 | |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 60 | static void ccu_nm_disable(struct clk_hw *hw) |
| 61 | { |
| 62 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
| 63 | |
| 64 | return ccu_gate_helper_disable(&nm->common, nm->enable); |
| 65 | } |
| 66 | |
| 67 | static int ccu_nm_enable(struct clk_hw *hw) |
| 68 | { |
| 69 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
| 70 | |
| 71 | return ccu_gate_helper_enable(&nm->common, nm->enable); |
| 72 | } |
| 73 | |
| 74 | static int ccu_nm_is_enabled(struct clk_hw *hw) |
| 75 | { |
| 76 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
| 77 | |
| 78 | return ccu_gate_helper_is_enabled(&nm->common, nm->enable); |
| 79 | } |
| 80 | |
| 81 | static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw, |
| 82 | unsigned long parent_rate) |
| 83 | { |
| 84 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 85 | unsigned long rate; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 86 | unsigned long n, m; |
| 87 | u32 reg; |
| 88 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 89 | if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac)) { |
| 90 | rate = ccu_frac_helper_read_rate(&nm->common, &nm->frac); |
| 91 | |
| 92 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 93 | rate /= nm->fixed_post_div; |
| 94 | |
| 95 | return rate; |
| 96 | } |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 97 | |
| 98 | reg = readl(nm->common.base + nm->common.reg); |
| 99 | |
| 100 | n = reg >> nm->n.shift; |
| 101 | n &= (1 << nm->n.width) - 1; |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 102 | n += nm->n.offset; |
| 103 | if (!n) |
| 104 | n++; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 105 | |
| 106 | m = reg >> nm->m.shift; |
| 107 | m &= (1 << nm->m.width) - 1; |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 108 | m += nm->m.offset; |
| 109 | if (!m) |
| 110 | m++; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 111 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 112 | if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm)) |
| 113 | rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n); |
| 114 | else |
Jernej Skrabec | 65b6657 | 2018-11-04 19:26:40 +0100 | [diff] [blame^] | 115 | rate = ccu_nm_calc_rate(parent_rate, n, m); |
Chen-Yu Tsai | 392ba5f | 2017-10-12 16:37:00 +0800 | [diff] [blame] | 116 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 117 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 118 | rate /= nm->fixed_post_div; |
| 119 | |
| 120 | return rate; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate, |
| 124 | unsigned long *parent_rate) |
| 125 | { |
| 126 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 127 | struct _ccu_nm _nm; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 128 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 129 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 130 | rate *= nm->fixed_post_div; |
Chen-Yu Tsai | 4cdbc40 | 2017-10-12 16:36:58 +0800 | [diff] [blame] | 131 | |
Jernej Skrabec | 2d2b61c | 2018-03-01 22:34:27 +0100 | [diff] [blame] | 132 | if (rate < nm->min_rate) { |
| 133 | rate = nm->min_rate; |
| 134 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 135 | rate /= nm->fixed_post_div; |
| 136 | return rate; |
| 137 | } |
| 138 | |
Jernej Skrabec | cb54fbd | 2018-08-09 18:52:13 +0200 | [diff] [blame] | 139 | if (nm->max_rate && rate > nm->max_rate) { |
| 140 | rate = nm->max_rate; |
| 141 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 142 | rate /= nm->fixed_post_div; |
| 143 | return rate; |
| 144 | } |
| 145 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 146 | if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) { |
| 147 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 148 | rate /= nm->fixed_post_div; |
Chen-Yu Tsai | 392ba5f | 2017-10-12 16:37:00 +0800 | [diff] [blame] | 149 | return rate; |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) { |
| 153 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 154 | rate /= nm->fixed_post_div; |
| 155 | return rate; |
| 156 | } |
Chen-Yu Tsai | 392ba5f | 2017-10-12 16:37:00 +0800 | [diff] [blame] | 157 | |
Chen-Yu Tsai | 4162c5c | 2017-03-24 16:33:05 +0800 | [diff] [blame] | 158 | _nm.min_n = nm->n.min ?: 1; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 159 | _nm.max_n = nm->n.max ?: 1 << nm->n.width; |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 160 | _nm.min_m = 1; |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 161 | _nm.max_m = nm->m.max ?: 1 << nm->m.width; |
Maxime Ripard | 87ba9e5 | 2016-09-06 12:29:04 +0200 | [diff] [blame] | 162 | |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 163 | ccu_nm_find_best(*parent_rate, rate, &_nm); |
Jernej Skrabec | 65b6657 | 2018-11-04 19:26:40 +0100 | [diff] [blame^] | 164 | rate = ccu_nm_calc_rate(*parent_rate, _nm.n, _nm.m); |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 165 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 166 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 167 | rate /= nm->fixed_post_div; |
| 168 | |
| 169 | return rate; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate, |
| 173 | unsigned long parent_rate) |
| 174 | { |
| 175 | struct ccu_nm *nm = hw_to_ccu_nm(hw); |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 176 | struct _ccu_nm _nm; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 177 | unsigned long flags; |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 178 | u32 reg; |
| 179 | |
Chen-Yu Tsai | 7d333ef | 2017-12-08 16:35:10 +0800 | [diff] [blame] | 180 | /* Adjust target rate according to post-dividers */ |
| 181 | if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV) |
| 182 | rate = rate * nm->fixed_post_div; |
| 183 | |
Jernej Škrabec | b64dfec | 2017-07-30 18:41:47 +0200 | [diff] [blame] | 184 | if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) { |
| 185 | spin_lock_irqsave(nm->common.lock, flags); |
| 186 | |
| 187 | /* most SoCs require M to be 0 if fractional mode is used */ |
| 188 | reg = readl(nm->common.base + nm->common.reg); |
| 189 | reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift); |
| 190 | writel(reg, nm->common.base + nm->common.reg); |
| 191 | |
| 192 | spin_unlock_irqrestore(nm->common.lock, flags); |
| 193 | |
| 194 | ccu_frac_helper_enable(&nm->common, &nm->frac); |
| 195 | |
Jernej Škrabec | 1d42460 | 2017-07-30 18:41:50 +0200 | [diff] [blame] | 196 | return ccu_frac_helper_set_rate(&nm->common, &nm->frac, |
| 197 | rate, nm->lock); |
Jernej Škrabec | b64dfec | 2017-07-30 18:41:47 +0200 | [diff] [blame] | 198 | } else { |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 199 | ccu_frac_helper_disable(&nm->common, &nm->frac); |
Jernej Škrabec | b64dfec | 2017-07-30 18:41:47 +0200 | [diff] [blame] | 200 | } |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 201 | |
Chen-Yu Tsai | 95ad8ed | 2017-03-24 16:33:06 +0800 | [diff] [blame] | 202 | _nm.min_n = nm->n.min ?: 1; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 203 | _nm.max_n = nm->n.max ?: 1 << nm->n.width; |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 204 | _nm.min_m = 1; |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 205 | _nm.max_m = nm->m.max ?: 1 << nm->m.width; |
Maxime Ripard | 87ba9e5 | 2016-09-06 12:29:04 +0200 | [diff] [blame] | 206 | |
Chen-Yu Tsai | 392ba5f | 2017-10-12 16:37:00 +0800 | [diff] [blame] | 207 | if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) { |
| 208 | ccu_sdm_helper_enable(&nm->common, &nm->sdm, rate); |
| 209 | |
| 210 | /* Sigma delta modulation requires specific N and M factors */ |
| 211 | ccu_sdm_helper_get_factors(&nm->common, &nm->sdm, rate, |
| 212 | &_nm.m, &_nm.n); |
| 213 | } else { |
| 214 | ccu_sdm_helper_disable(&nm->common, &nm->sdm); |
| 215 | ccu_nm_find_best(parent_rate, rate, &_nm); |
| 216 | } |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 217 | |
| 218 | spin_lock_irqsave(nm->common.lock, flags); |
| 219 | |
| 220 | reg = readl(nm->common.base + nm->common.reg); |
| 221 | reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift); |
| 222 | reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift); |
| 223 | |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 224 | reg |= (_nm.n - nm->n.offset) << nm->n.shift; |
| 225 | reg |= (_nm.m - nm->m.offset) << nm->m.shift; |
| 226 | writel(reg, nm->common.base + nm->common.reg); |
Maxime Ripard | 6174a1e | 2016-06-29 21:05:31 +0200 | [diff] [blame] | 227 | |
| 228 | spin_unlock_irqrestore(nm->common.lock, flags); |
| 229 | |
| 230 | ccu_helper_wait_for_lock(&nm->common, nm->lock); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | const struct clk_ops ccu_nm_ops = { |
| 236 | .disable = ccu_nm_disable, |
| 237 | .enable = ccu_nm_enable, |
| 238 | .is_enabled = ccu_nm_is_enabled, |
| 239 | |
| 240 | .recalc_rate = ccu_nm_recalc_rate, |
| 241 | .round_rate = ccu_nm_round_rate, |
| 242 | .set_rate = ccu_nm_set_rate, |
| 243 | }; |