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, |
| 36 | unsigned long parent_rate, |
| 37 | unsigned long rate, |
| 38 | void *data) |
| 39 | { |
| 40 | struct ccu_mult *cm = data; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 41 | struct _ccu_mult _cm; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 42 | |
Maxime Ripard | 6e0d50d | 2016-09-29 22:57:26 +0200 | [diff] [blame] | 43 | _cm.min = 1; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 44 | _cm.max = 1 << cm->mult.width; |
| 45 | ccu_mult_find_best(parent_rate, rate, &_cm); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 46 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 47 | return parent_rate * _cm.mult; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void ccu_mult_disable(struct clk_hw *hw) |
| 51 | { |
| 52 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 53 | |
| 54 | return ccu_gate_helper_disable(&cm->common, cm->enable); |
| 55 | } |
| 56 | |
| 57 | static int ccu_mult_enable(struct clk_hw *hw) |
| 58 | { |
| 59 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 60 | |
| 61 | return ccu_gate_helper_enable(&cm->common, cm->enable); |
| 62 | } |
| 63 | |
| 64 | static int ccu_mult_is_enabled(struct clk_hw *hw) |
| 65 | { |
| 66 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 67 | |
| 68 | return ccu_gate_helper_is_enabled(&cm->common, cm->enable); |
| 69 | } |
| 70 | |
| 71 | static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw, |
| 72 | unsigned long parent_rate) |
| 73 | { |
| 74 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 75 | unsigned long val; |
| 76 | u32 reg; |
| 77 | |
Maxime Ripard | d77e813 | 2016-10-13 12:44:55 +0200 | [diff] [blame^] | 78 | if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac)) |
| 79 | return ccu_frac_helper_read_rate(&cm->common, &cm->frac); |
| 80 | |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 81 | reg = readl(cm->common.base + cm->common.reg); |
| 82 | val = reg >> cm->mult.shift; |
| 83 | val &= (1 << cm->mult.width) - 1; |
| 84 | |
| 85 | ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, |
| 86 | &parent_rate); |
| 87 | |
| 88 | return parent_rate * (val + 1); |
| 89 | } |
| 90 | |
| 91 | static int ccu_mult_determine_rate(struct clk_hw *hw, |
| 92 | struct clk_rate_request *req) |
| 93 | { |
| 94 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 95 | |
| 96 | return ccu_mux_helper_determine_rate(&cm->common, &cm->mux, |
| 97 | req, ccu_mult_round_rate, cm); |
| 98 | } |
| 99 | |
| 100 | static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate, |
| 101 | unsigned long parent_rate) |
| 102 | { |
| 103 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 104 | struct _ccu_mult _cm; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 105 | unsigned long flags; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 106 | u32 reg; |
| 107 | |
Maxime Ripard | d77e813 | 2016-10-13 12:44:55 +0200 | [diff] [blame^] | 108 | if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate)) |
| 109 | return ccu_frac_helper_set_rate(&cm->common, &cm->frac, rate); |
| 110 | else |
| 111 | ccu_frac_helper_disable(&cm->common, &cm->frac); |
| 112 | |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 113 | ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, |
| 114 | &parent_rate); |
| 115 | |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 116 | _cm.min = cm->mult.min; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 117 | _cm.max = 1 << cm->mult.width; |
| 118 | ccu_mult_find_best(parent_rate, rate, &_cm); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 119 | |
| 120 | spin_lock_irqsave(cm->common.lock, flags); |
| 121 | |
| 122 | reg = readl(cm->common.base + cm->common.reg); |
| 123 | reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift); |
| 124 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 125 | writel(reg | ((_cm.mult - 1) << cm->mult.shift), |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 126 | cm->common.base + cm->common.reg); |
| 127 | |
| 128 | spin_unlock_irqrestore(cm->common.lock, flags); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static u8 ccu_mult_get_parent(struct clk_hw *hw) |
| 134 | { |
| 135 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 136 | |
| 137 | return ccu_mux_helper_get_parent(&cm->common, &cm->mux); |
| 138 | } |
| 139 | |
| 140 | static int ccu_mult_set_parent(struct clk_hw *hw, u8 index) |
| 141 | { |
| 142 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 143 | |
| 144 | return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index); |
| 145 | } |
| 146 | |
| 147 | const struct clk_ops ccu_mult_ops = { |
| 148 | .disable = ccu_mult_disable, |
| 149 | .enable = ccu_mult_enable, |
| 150 | .is_enabled = ccu_mult_is_enabled, |
| 151 | |
| 152 | .get_parent = ccu_mult_get_parent, |
| 153 | .set_parent = ccu_mult_set_parent, |
| 154 | |
| 155 | .determine_rate = ccu_mult_determine_rate, |
| 156 | .recalc_rate = ccu_mult_recalc_rate, |
| 157 | .set_rate = ccu_mult_set_rate, |
| 158 | }; |