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 | |
| 78 | reg = readl(cm->common.base + cm->common.reg); |
| 79 | val = reg >> cm->mult.shift; |
| 80 | val &= (1 << cm->mult.width) - 1; |
| 81 | |
| 82 | ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, |
| 83 | &parent_rate); |
| 84 | |
| 85 | return parent_rate * (val + 1); |
| 86 | } |
| 87 | |
| 88 | static int ccu_mult_determine_rate(struct clk_hw *hw, |
| 89 | struct clk_rate_request *req) |
| 90 | { |
| 91 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 92 | |
| 93 | return ccu_mux_helper_determine_rate(&cm->common, &cm->mux, |
| 94 | req, ccu_mult_round_rate, cm); |
| 95 | } |
| 96 | |
| 97 | static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate, |
| 98 | unsigned long parent_rate) |
| 99 | { |
| 100 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 101 | struct _ccu_mult _cm; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 102 | unsigned long flags; |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 103 | u32 reg; |
| 104 | |
| 105 | ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, |
| 106 | &parent_rate); |
| 107 | |
Maxime Ripard | 2beaa60 | 2016-09-30 22:16:51 +0200 | [diff] [blame] | 108 | _cm.min = cm->mult.min; |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 109 | _cm.max = 1 << cm->mult.width; |
| 110 | ccu_mult_find_best(parent_rate, rate, &_cm); |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 111 | |
| 112 | spin_lock_irqsave(cm->common.lock, flags); |
| 113 | |
| 114 | reg = readl(cm->common.base + cm->common.reg); |
| 115 | reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift); |
| 116 | |
Maxime Ripard | b8302c7 | 2016-09-29 23:50:21 +0200 | [diff] [blame] | 117 | writel(reg | ((_cm.mult - 1) << cm->mult.shift), |
Maxime Ripard | aa15233 | 2016-08-30 10:38:07 +0200 | [diff] [blame] | 118 | cm->common.base + cm->common.reg); |
| 119 | |
| 120 | spin_unlock_irqrestore(cm->common.lock, flags); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static u8 ccu_mult_get_parent(struct clk_hw *hw) |
| 126 | { |
| 127 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 128 | |
| 129 | return ccu_mux_helper_get_parent(&cm->common, &cm->mux); |
| 130 | } |
| 131 | |
| 132 | static int ccu_mult_set_parent(struct clk_hw *hw, u8 index) |
| 133 | { |
| 134 | struct ccu_mult *cm = hw_to_ccu_mult(hw); |
| 135 | |
| 136 | return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index); |
| 137 | } |
| 138 | |
| 139 | const struct clk_ops ccu_mult_ops = { |
| 140 | .disable = ccu_mult_disable, |
| 141 | .enable = ccu_mult_enable, |
| 142 | .is_enabled = ccu_mult_is_enabled, |
| 143 | |
| 144 | .get_parent = ccu_mult_get_parent, |
| 145 | .set_parent = ccu_mult_set_parent, |
| 146 | |
| 147 | .determine_rate = ccu_mult_determine_rate, |
| 148 | .recalc_rate = ccu_mult_recalc_rate, |
| 149 | .set_rate = ccu_mult_set_rate, |
| 150 | }; |