blob: 678b6cb49f01b7b176ca84f425ee5a91d6486728 [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
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
88static 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
97static 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 Ripardb8302c72016-09-29 23:50:21 +0200101 struct _ccu_mult _cm;
Maxime Ripardaa152332016-08-30 10:38:07 +0200102 unsigned long flags;
Maxime Ripardaa152332016-08-30 10:38:07 +0200103 u32 reg;
104
105 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
106 &parent_rate);
107
Maxime Ripard2beaa602016-09-30 22:16:51 +0200108 _cm.min = cm->mult.min;
Maxime Ripardb8302c72016-09-29 23:50:21 +0200109 _cm.max = 1 << cm->mult.width;
110 ccu_mult_find_best(parent_rate, rate, &_cm);
Maxime Ripardaa152332016-08-30 10:38:07 +0200111
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 Ripardb8302c72016-09-29 23:50:21 +0200117 writel(reg | ((_cm.mult - 1) << cm->mult.shift),
Maxime Ripardaa152332016-08-30 10:38:07 +0200118 cm->common.base + cm->common.reg);
119
120 spin_unlock_irqrestore(cm->common.lock, flags);
121
122 return 0;
123}
124
125static 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
132static 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
139const 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};