blob: 7c8cf2e04e9450cefe0980355a23bdf4b6933f1a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Maxime Ripardaa152332016-08-30 10:38:07 +02002/*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
Maxime Ripardaa152332016-08-30 10:38:07 +02005 */
6
7#include <linux/clk-provider.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -07008#include <linux/io.h>
Maxime Ripardaa152332016-08-30 10:38:07 +02009
10#include "ccu_gate.h"
11#include "ccu_mult.h"
12
Maxime Ripardb8302c72016-09-29 23:50:21 +020013struct _ccu_mult {
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020014 unsigned long mult, min, max;
Maxime Ripardb8302c72016-09-29 23:50:21 +020015};
16
Maxime Ripardaa152332016-08-30 10:38:07 +020017static void ccu_mult_find_best(unsigned long parent, unsigned long rate,
Maxime Ripardb8302c72016-09-29 23:50:21 +020018 struct _ccu_mult *mult)
Maxime Ripardaa152332016-08-30 10:38:07 +020019{
Maxime Ripardb8302c72016-09-29 23:50:21 +020020 int _mult;
21
22 _mult = rate / parent;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020023 if (_mult < mult->min)
24 _mult = mult->min;
25
Maxime Ripardb8302c72016-09-29 23:50:21 +020026 if (_mult > mult->max)
27 _mult = mult->max;
28
29 mult->mult = _mult;
Maxime Ripardaa152332016-08-30 10:38:07 +020030}
31
32static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux,
Maxime Ripard10a8d9b2017-05-17 09:40:31 +020033 struct clk_hw *parent,
34 unsigned long *parent_rate,
35 unsigned long rate,
36 void *data)
Maxime Ripardaa152332016-08-30 10:38:07 +020037{
38 struct ccu_mult *cm = data;
Maxime Ripardb8302c72016-09-29 23:50:21 +020039 struct _ccu_mult _cm;
Maxime Ripardaa152332016-08-30 10:38:07 +020040
Maxime Ripardc9520be2017-01-21 23:41:56 +010041 _cm.min = cm->mult.min;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +020042
43 if (cm->mult.max)
44 _cm.max = cm->mult.max;
45 else
46 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
47
Maxime Ripard10a8d9b2017-05-17 09:40:31 +020048 ccu_mult_find_best(*parent_rate, rate, &_cm);
Maxime Ripardaa152332016-08-30 10:38:07 +020049
Maxime Ripard10a8d9b2017-05-17 09:40:31 +020050 return *parent_rate * _cm.mult;
Maxime Ripardaa152332016-08-30 10:38:07 +020051}
52
53static void ccu_mult_disable(struct clk_hw *hw)
54{
55 struct ccu_mult *cm = hw_to_ccu_mult(hw);
56
57 return ccu_gate_helper_disable(&cm->common, cm->enable);
58}
59
60static int ccu_mult_enable(struct clk_hw *hw)
61{
62 struct ccu_mult *cm = hw_to_ccu_mult(hw);
63
64 return ccu_gate_helper_enable(&cm->common, cm->enable);
65}
66
67static int ccu_mult_is_enabled(struct clk_hw *hw)
68{
69 struct ccu_mult *cm = hw_to_ccu_mult(hw);
70
71 return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
72}
73
74static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw,
75 unsigned long parent_rate)
76{
77 struct ccu_mult *cm = hw_to_ccu_mult(hw);
78 unsigned long val;
79 u32 reg;
80
Maxime Ripardd77e8132016-10-13 12:44:55 +020081 if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac))
82 return ccu_frac_helper_read_rate(&cm->common, &cm->frac);
83
Maxime Ripardaa152332016-08-30 10:38:07 +020084 reg = readl(cm->common.base + cm->common.reg);
85 val = reg >> cm->mult.shift;
86 val &= (1 << cm->mult.width) - 1;
87
Maxime Ripardd754b152017-05-17 09:40:35 +020088 parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
89 parent_rate);
Maxime Ripardaa152332016-08-30 10:38:07 +020090
Maxime Riparde66f81b2016-11-08 18:12:34 +010091 return parent_rate * (val + cm->mult.offset);
Maxime Ripardaa152332016-08-30 10:38:07 +020092}
93
94static int ccu_mult_determine_rate(struct clk_hw *hw,
95 struct clk_rate_request *req)
96{
97 struct ccu_mult *cm = hw_to_ccu_mult(hw);
98
99 return ccu_mux_helper_determine_rate(&cm->common, &cm->mux,
100 req, ccu_mult_round_rate, cm);
101}
102
103static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate,
104 unsigned long parent_rate)
105{
106 struct ccu_mult *cm = hw_to_ccu_mult(hw);
Maxime Ripardb8302c72016-09-29 23:50:21 +0200107 struct _ccu_mult _cm;
Maxime Ripardaa152332016-08-30 10:38:07 +0200108 unsigned long flags;
Maxime Ripardaa152332016-08-30 10:38:07 +0200109 u32 reg;
110
Jernej Škrabec1e92ae62017-07-30 18:41:48 +0200111 if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate)) {
112 ccu_frac_helper_enable(&cm->common, &cm->frac);
113
Jernej Škrabec1d424602017-07-30 18:41:50 +0200114 return ccu_frac_helper_set_rate(&cm->common, &cm->frac,
115 rate, cm->lock);
Jernej Škrabec1e92ae62017-07-30 18:41:48 +0200116 } else {
Maxime Ripardd77e8132016-10-13 12:44:55 +0200117 ccu_frac_helper_disable(&cm->common, &cm->frac);
Jernej Škrabec1e92ae62017-07-30 18:41:48 +0200118 }
Maxime Ripardd77e8132016-10-13 12:44:55 +0200119
Maxime Ripardd754b152017-05-17 09:40:35 +0200120 parent_rate = ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
121 parent_rate);
Maxime Ripardaa152332016-08-30 10:38:07 +0200122
Maxime Ripard2beaa602016-09-30 22:16:51 +0200123 _cm.min = cm->mult.min;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +0200124
125 if (cm->mult.max)
126 _cm.max = cm->mult.max;
127 else
128 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
129
Maxime Ripardb8302c72016-09-29 23:50:21 +0200130 ccu_mult_find_best(parent_rate, rate, &_cm);
Maxime Ripardaa152332016-08-30 10:38:07 +0200131
132 spin_lock_irqsave(cm->common.lock, flags);
133
134 reg = readl(cm->common.base + cm->common.reg);
135 reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift);
Maxime Riparde66f81b2016-11-08 18:12:34 +0100136 reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift);
Maxime Ripardaa152332016-08-30 10:38:07 +0200137
Maxime Riparde66f81b2016-11-08 18:12:34 +0100138 writel(reg, cm->common.base + cm->common.reg);
Maxime Ripardaa152332016-08-30 10:38:07 +0200139
140 spin_unlock_irqrestore(cm->common.lock, flags);
141
Chen-Yu Tsaicf719012017-04-05 14:37:42 +0800142 ccu_helper_wait_for_lock(&cm->common, cm->lock);
143
Maxime Ripardaa152332016-08-30 10:38:07 +0200144 return 0;
145}
146
147static u8 ccu_mult_get_parent(struct clk_hw *hw)
148{
149 struct ccu_mult *cm = hw_to_ccu_mult(hw);
150
151 return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
152}
153
154static int ccu_mult_set_parent(struct clk_hw *hw, u8 index)
155{
156 struct ccu_mult *cm = hw_to_ccu_mult(hw);
157
158 return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
159}
160
161const struct clk_ops ccu_mult_ops = {
162 .disable = ccu_mult_disable,
163 .enable = ccu_mult_enable,
164 .is_enabled = ccu_mult_is_enabled,
165
166 .get_parent = ccu_mult_get_parent,
167 .set_parent = ccu_mult_set_parent,
168
169 .determine_rate = ccu_mult_determine_rate,
170 .recalc_rate = ccu_mult_recalc_rate,
171 .set_rate = ccu_mult_set_rate,
172};