blob: 826302464650611aebb4a9e6b1afed677a148825 [file] [log] [blame]
Maxime Ripardaa152332016-08-30 10:38:07 +02001/*
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 Ripardb8302c72016-09-29 23:50:21 +020016struct _ccu_mult {
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020017 unsigned long mult, min, max;
Maxime Ripardb8302c72016-09-29 23:50:21 +020018};
19
Maxime Ripardaa152332016-08-30 10:38:07 +020020static void ccu_mult_find_best(unsigned long parent, unsigned long rate,
Maxime Ripardb8302c72016-09-29 23:50:21 +020021 struct _ccu_mult *mult)
Maxime Ripardaa152332016-08-30 10:38:07 +020022{
Maxime Ripardb8302c72016-09-29 23:50:21 +020023 int _mult;
24
25 _mult = rate / parent;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020026 if (_mult < mult->min)
27 _mult = mult->min;
28
Maxime Ripardb8302c72016-09-29 23:50:21 +020029 if (_mult > mult->max)
30 _mult = mult->max;
31
32 mult->mult = _mult;
Maxime Ripardaa152332016-08-30 10:38:07 +020033}
34
35static 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 Ripardb8302c72016-09-29 23:50:21 +020041 struct _ccu_mult _cm;
Maxime Ripardaa152332016-08-30 10:38:07 +020042
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020043 _cm.min = 1;
Maxime Ripardb8302c72016-09-29 23:50:21 +020044 _cm.max = 1 << cm->mult.width;
45 ccu_mult_find_best(parent_rate, rate, &_cm);
Maxime Ripardaa152332016-08-30 10:38:07 +020046
Maxime Ripardb8302c72016-09-29 23:50:21 +020047 return parent_rate * _cm.mult;
Maxime Ripardaa152332016-08-30 10:38:07 +020048}
49
50static 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
57static 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
64static 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
71static 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 Ripardd77e8132016-10-13 12:44:55 +020078 if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac))
79 return ccu_frac_helper_read_rate(&cm->common, &cm->frac);
80
Maxime Ripardaa152332016-08-30 10:38:07 +020081 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
91static 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
100static 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 Ripardb8302c72016-09-29 23:50:21 +0200104 struct _ccu_mult _cm;
Maxime Ripardaa152332016-08-30 10:38:07 +0200105 unsigned long flags;
Maxime Ripardaa152332016-08-30 10:38:07 +0200106 u32 reg;
107
Maxime Ripardd77e8132016-10-13 12:44:55 +0200108 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 Ripardaa152332016-08-30 10:38:07 +0200113 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
114 &parent_rate);
115
Maxime Ripard2beaa602016-09-30 22:16:51 +0200116 _cm.min = cm->mult.min;
Maxime Ripardb8302c72016-09-29 23:50:21 +0200117 _cm.max = 1 << cm->mult.width;
118 ccu_mult_find_best(parent_rate, rate, &_cm);
Maxime Ripardaa152332016-08-30 10:38:07 +0200119
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 Ripardb8302c72016-09-29 23:50:21 +0200125 writel(reg | ((_cm.mult - 1) << cm->mult.shift),
Maxime Ripardaa152332016-08-30 10:38:07 +0200126 cm->common.base + cm->common.reg);
127
128 spin_unlock_irqrestore(cm->common.lock, flags);
129
130 return 0;
131}
132
133static 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
140static 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
147const 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};