Maxime Ripard | df6561e | 2016-06-29 21:05:32 +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 | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 12 | |
| 13 | #include "ccu_gate.h" |
| 14 | #include "ccu_nkm.h" |
| 15 | |
| 16 | struct _ccu_nkm { |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 17 | unsigned long n, min_n, max_n; |
| 18 | unsigned long k, min_k, max_k; |
| 19 | unsigned long m, min_m, max_m; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static void ccu_nkm_find_best(unsigned long parent, unsigned long rate, |
| 23 | struct _ccu_nkm *nkm) |
| 24 | { |
| 25 | unsigned long best_rate = 0; |
| 26 | unsigned long best_n = 0, best_k = 0, best_m = 0; |
| 27 | unsigned long _n, _k, _m; |
| 28 | |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 29 | for (_k = nkm->min_k; _k <= nkm->max_k; _k++) { |
| 30 | for (_n = nkm->min_n; _n <= nkm->max_n; _n++) { |
| 31 | for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 32 | unsigned long tmp_rate; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 33 | |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 34 | tmp_rate = parent * _n * _k / _m; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 35 | |
Maxime Ripard | ee28648 | 2016-09-29 22:53:12 +0200 | [diff] [blame] | 36 | if (tmp_rate > rate) |
| 37 | continue; |
| 38 | if ((rate - tmp_rate) < (rate - best_rate)) { |
| 39 | best_rate = tmp_rate; |
| 40 | best_n = _n; |
| 41 | best_k = _k; |
| 42 | best_m = _m; |
| 43 | } |
| 44 | } |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | nkm->n = best_n; |
| 49 | nkm->k = best_k; |
| 50 | nkm->m = best_m; |
| 51 | } |
| 52 | |
| 53 | static void ccu_nkm_disable(struct clk_hw *hw) |
| 54 | { |
| 55 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 56 | |
| 57 | return ccu_gate_helper_disable(&nkm->common, nkm->enable); |
| 58 | } |
| 59 | |
| 60 | static int ccu_nkm_enable(struct clk_hw *hw) |
| 61 | { |
| 62 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 63 | |
| 64 | return ccu_gate_helper_enable(&nkm->common, nkm->enable); |
| 65 | } |
| 66 | |
| 67 | static int ccu_nkm_is_enabled(struct clk_hw *hw) |
| 68 | { |
| 69 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 70 | |
| 71 | return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable); |
| 72 | } |
| 73 | |
| 74 | static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw, |
| 75 | unsigned long parent_rate) |
| 76 | { |
| 77 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 78 | unsigned long n, m, k; |
| 79 | u32 reg; |
| 80 | |
| 81 | reg = readl(nkm->common.base + nkm->common.reg); |
| 82 | |
| 83 | n = reg >> nkm->n.shift; |
| 84 | n &= (1 << nkm->n.width) - 1; |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 85 | n += nkm->n.offset; |
| 86 | if (!n) |
| 87 | n++; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 88 | |
| 89 | k = reg >> nkm->k.shift; |
| 90 | k &= (1 << nkm->k.width) - 1; |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 91 | k += nkm->k.offset; |
| 92 | if (!k) |
| 93 | k++; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 94 | |
| 95 | m = reg >> nkm->m.shift; |
| 96 | m &= (1 << nkm->m.width) - 1; |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 97 | m += nkm->m.offset; |
| 98 | if (!m) |
| 99 | m++; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 100 | |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 101 | return parent_rate * n * k / m; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 104 | static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, |
| 105 | unsigned long parent_rate, |
| 106 | unsigned long rate, |
| 107 | void *data) |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 108 | { |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 109 | struct ccu_nkm *nkm = data; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 110 | struct _ccu_nkm _nkm; |
| 111 | |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 112 | _nkm.min_n = nkm->n.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 113 | _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width; |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 114 | _nkm.min_k = nkm->k.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 115 | _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width; |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 116 | _nkm.min_m = 1; |
Maxime Ripard | 87ba9e5 | 2016-09-06 12:29:04 +0200 | [diff] [blame] | 117 | _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 118 | |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 119 | ccu_nkm_find_best(parent_rate, rate, &_nkm); |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 120 | |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 121 | return parent_rate * _nkm.n * _nkm.k / _nkm.m; |
| 122 | } |
| 123 | |
| 124 | static int ccu_nkm_determine_rate(struct clk_hw *hw, |
| 125 | struct clk_rate_request *req) |
| 126 | { |
| 127 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 128 | |
| 129 | return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux, |
| 130 | req, ccu_nkm_round_rate, nkm); |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate, |
| 134 | unsigned long parent_rate) |
| 135 | { |
| 136 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 137 | struct _ccu_nkm _nkm; |
| 138 | unsigned long flags; |
| 139 | u32 reg; |
| 140 | |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 141 | _nkm.min_n = nkm->n.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 142 | _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width; |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 143 | _nkm.min_k = nkm->k.min; |
Maxime Ripard | 0c3c8e1 | 2016-10-14 12:08:19 +0200 | [diff] [blame] | 144 | _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width; |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 145 | _nkm.min_m = 1; |
Maxime Ripard | 87ba9e5 | 2016-09-06 12:29:04 +0200 | [diff] [blame] | 146 | _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 147 | |
| 148 | ccu_nkm_find_best(parent_rate, rate, &_nkm); |
| 149 | |
| 150 | spin_lock_irqsave(nkm->common.lock, flags); |
| 151 | |
| 152 | reg = readl(nkm->common.base + nkm->common.reg); |
| 153 | reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift); |
| 154 | reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift); |
| 155 | reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift); |
| 156 | |
Maxime Ripard | e66f81b | 2016-11-08 18:12:34 +0100 | [diff] [blame] | 157 | reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift; |
| 158 | reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift; |
| 159 | reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift; |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 160 | writel(reg, nkm->common.base + nkm->common.reg); |
| 161 | |
| 162 | spin_unlock_irqrestore(nkm->common.lock, flags); |
| 163 | |
| 164 | ccu_helper_wait_for_lock(&nkm->common, nkm->lock); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 169 | static u8 ccu_nkm_get_parent(struct clk_hw *hw) |
| 170 | { |
| 171 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 172 | |
| 173 | return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux); |
| 174 | } |
| 175 | |
| 176 | static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index) |
| 177 | { |
| 178 | struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); |
| 179 | |
| 180 | return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index); |
| 181 | } |
| 182 | |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 183 | const struct clk_ops ccu_nkm_ops = { |
| 184 | .disable = ccu_nkm_disable, |
| 185 | .enable = ccu_nkm_enable, |
| 186 | .is_enabled = ccu_nkm_is_enabled, |
| 187 | |
Chen-Yu Tsai | a365835 | 2016-07-26 15:04:28 +0800 | [diff] [blame] | 188 | .get_parent = ccu_nkm_get_parent, |
| 189 | .set_parent = ccu_nkm_set_parent, |
| 190 | |
| 191 | .determine_rate = ccu_nkm_determine_rate, |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 192 | .recalc_rate = ccu_nkm_recalc_rate, |
Maxime Ripard | df6561e | 2016-06-29 21:05:32 +0200 | [diff] [blame] | 193 | .set_rate = ccu_nkm_set_rate, |
| 194 | }; |