blob: 2d39a8bc367c37c1d17867cebefbda181ae58694 [file] [log] [blame]
Jerome Brunet22f65a32018-05-16 10:50:40 +02001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
Michael Turquette1c50da42016-06-06 23:16:17 -07002/*
Michael Turquette1c50da42016-06-06 23:16:17 -07003 * Copyright (c) 2016 AmLogic, Inc.
4 * Author: Michael Turquette <mturquette@baylibre.com>
Michael Turquette1c50da42016-06-06 23:16:17 -07005 */
6
7/*
8 * MultiPhase Locked Loops are outputs from a PLL with additional frequency
9 * scaling capabilities. MPLL rates are calculated as:
10 *
11 * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
12 */
13
14#include <linux/clk-provider.h>
Jerome Brunet889c2b72019-02-01 13:58:41 +010015#include <linux/module.h>
16#include <linux/spinlock.h>
17
18#include "clk-regmap.h"
19#include "clk-mpll.h"
Michael Turquette1c50da42016-06-06 23:16:17 -070020
Jerome Brunet007e6e52017-03-09 11:41:50 +010021#define SDM_DEN 16384
Jerome Brunet007e6e52017-03-09 11:41:50 +010022#define N2_MIN 4
Jerome Brunetb68fb782017-03-09 11:41:53 +010023#define N2_MAX 511
Michael Turquette1c50da42016-06-06 23:16:17 -070024
Jerome Brunetc763e612018-02-12 15:58:40 +010025static inline struct meson_clk_mpll_data *
26meson_clk_mpll_data(struct clk_regmap *clk)
27{
28 return (struct meson_clk_mpll_data *)clk->data;
29}
Michael Turquette1c50da42016-06-06 23:16:17 -070030
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020031static long rate_from_params(unsigned long parent_rate,
Jerome Brunetc763e612018-02-12 15:58:40 +010032 unsigned int sdm,
33 unsigned int n2)
Jerome Brunet007e6e52017-03-09 11:41:50 +010034{
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020035 unsigned long divisor = (SDM_DEN * n2) + sdm;
36
37 if (n2 < N2_MIN)
38 return -EINVAL;
39
Martin Blumenstinglb6093382017-04-01 15:02:25 +020040 return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
Jerome Brunet007e6e52017-03-09 11:41:50 +010041}
42
43static void params_from_rate(unsigned long requested_rate,
44 unsigned long parent_rate,
Jerome Brunetc763e612018-02-12 15:58:40 +010045 unsigned int *sdm,
Jerome Brunetbae11062018-05-15 18:36:51 +020046 unsigned int *n2,
47 u8 flags)
Jerome Brunet007e6e52017-03-09 11:41:50 +010048{
49 uint64_t div = parent_rate;
Jerome Brunetbae11062018-05-15 18:36:51 +020050 uint64_t frac = do_div(div, requested_rate);
51
52 frac *= SDM_DEN;
53
54 if (flags & CLK_MESON_MPLL_ROUND_CLOSEST)
55 *sdm = DIV_ROUND_CLOSEST_ULL(frac, requested_rate);
56 else
57 *sdm = DIV_ROUND_UP_ULL(frac, requested_rate);
58
59 if (*sdm == SDM_DEN) {
60 *sdm = 0;
61 div += 1;
62 }
Jerome Brunet007e6e52017-03-09 11:41:50 +010063
64 if (div < N2_MIN) {
65 *n2 = N2_MIN;
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020066 *sdm = 0;
Jerome Brunet007e6e52017-03-09 11:41:50 +010067 } else if (div > N2_MAX) {
68 *n2 = N2_MAX;
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020069 *sdm = SDM_DEN - 1;
Jerome Brunet007e6e52017-03-09 11:41:50 +010070 } else {
71 *n2 = div;
Jerome Brunet007e6e52017-03-09 11:41:50 +010072 }
73}
74
Michael Turquette1c50da42016-06-06 23:16:17 -070075static unsigned long mpll_recalc_rate(struct clk_hw *hw,
76 unsigned long parent_rate)
77{
Jerome Brunetc763e612018-02-12 15:58:40 +010078 struct clk_regmap *clk = to_clk_regmap(hw);
79 struct meson_clk_mpll_data *mpll = meson_clk_mpll_data(clk);
80 unsigned int sdm, n2;
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020081 long rate;
Michael Turquette1c50da42016-06-06 23:16:17 -070082
Jerome Brunetc763e612018-02-12 15:58:40 +010083 sdm = meson_parm_read(clk->map, &mpll->sdm);
84 n2 = meson_parm_read(clk->map, &mpll->n2);
Michael Turquette1c50da42016-06-06 23:16:17 -070085
Martin Blumenstingl88e4ac62017-04-01 15:02:24 +020086 rate = rate_from_params(parent_rate, sdm, n2);
Jerome Brunetc763e612018-02-12 15:58:40 +010087 return rate < 0 ? 0 : rate;
Jerome Brunet007e6e52017-03-09 11:41:50 +010088}
Michael Turquette1c50da42016-06-06 23:16:17 -070089
Jerome Brunet007e6e52017-03-09 11:41:50 +010090static long mpll_round_rate(struct clk_hw *hw,
91 unsigned long rate,
92 unsigned long *parent_rate)
93{
Jerome Brunetbae11062018-05-15 18:36:51 +020094 struct clk_regmap *clk = to_clk_regmap(hw);
95 struct meson_clk_mpll_data *mpll = meson_clk_mpll_data(clk);
Jerome Brunetc763e612018-02-12 15:58:40 +010096 unsigned int sdm, n2;
Jerome Brunet007e6e52017-03-09 11:41:50 +010097
Jerome Brunetbae11062018-05-15 18:36:51 +020098 params_from_rate(rate, *parent_rate, &sdm, &n2, mpll->flags);
Jerome Brunet007e6e52017-03-09 11:41:50 +010099 return rate_from_params(*parent_rate, sdm, n2);
100}
101
102static int mpll_set_rate(struct clk_hw *hw,
103 unsigned long rate,
104 unsigned long parent_rate)
105{
Jerome Brunetc763e612018-02-12 15:58:40 +0100106 struct clk_regmap *clk = to_clk_regmap(hw);
107 struct meson_clk_mpll_data *mpll = meson_clk_mpll_data(clk);
108 unsigned int sdm, n2;
Jerome Brunet007e6e52017-03-09 11:41:50 +0100109 unsigned long flags = 0;
110
Jerome Brunetbae11062018-05-15 18:36:51 +0200111 params_from_rate(rate, parent_rate, &sdm, &n2, mpll->flags);
Jerome Brunet007e6e52017-03-09 11:41:50 +0100112
113 if (mpll->lock)
114 spin_lock_irqsave(mpll->lock, flags);
115 else
116 __acquire(mpll->lock);
117
Jerome Brunet19855c82019-05-13 14:31:12 +0200118 /* Set the fractional part */
Jerome Brunetc763e612018-02-12 15:58:40 +0100119 meson_parm_write(clk->map, &mpll->sdm, sdm);
Jerome Brunet19855c82019-05-13 14:31:12 +0200120
121 /* Set the integer divider part */
122 meson_parm_write(clk->map, &mpll->n2, n2);
123
124 if (mpll->lock)
125 spin_unlock_irqrestore(mpll->lock, flags);
126 else
127 __release(mpll->lock);
128
129 return 0;
130}
131
132static void mpll_init(struct clk_hw *hw)
133{
134 struct clk_regmap *clk = to_clk_regmap(hw);
135 struct meson_clk_mpll_data *mpll = meson_clk_mpll_data(clk);
136
137 if (mpll->init_count)
138 regmap_multi_reg_write(clk->map, mpll->init_regs,
139 mpll->init_count);
140
141 /* Enable the fractional part */
Jerome Brunetc763e612018-02-12 15:58:40 +0100142 meson_parm_write(clk->map, &mpll->sdm_en, 1);
Jerome Brunet007e6e52017-03-09 11:41:50 +0100143
Jerome Brunetf9b3eee2019-05-13 14:31:09 +0200144 /* Set spread spectrum if possible */
145 if (MESON_PARM_APPLICABLE(&mpll->ssen)) {
146 unsigned int ss =
147 mpll->flags & CLK_MESON_MPLL_SPREAD_SPECTRUM ? 1 : 0;
148 meson_parm_write(clk->map, &mpll->ssen, ss);
149 }
Jerome Brunet007e6e52017-03-09 11:41:50 +0100150
Jerome Brunetc763e612018-02-12 15:58:40 +0100151 /* Set the magic misc bit if required */
152 if (MESON_PARM_APPLICABLE(&mpll->misc))
153 meson_parm_write(clk->map, &mpll->misc, 1);
Jerome Brunet007e6e52017-03-09 11:41:50 +0100154}
155
Michael Turquette1c50da42016-06-06 23:16:17 -0700156const struct clk_ops meson_clk_mpll_ro_ops = {
Jerome Brunet007e6e52017-03-09 11:41:50 +0100157 .recalc_rate = mpll_recalc_rate,
158 .round_rate = mpll_round_rate,
Jerome Brunet007e6e52017-03-09 11:41:50 +0100159};
Jerome Brunet889c2b72019-02-01 13:58:41 +0100160EXPORT_SYMBOL_GPL(meson_clk_mpll_ro_ops);
Jerome Brunet007e6e52017-03-09 11:41:50 +0100161
162const struct clk_ops meson_clk_mpll_ops = {
163 .recalc_rate = mpll_recalc_rate,
164 .round_rate = mpll_round_rate,
165 .set_rate = mpll_set_rate,
Jerome Brunet19855c82019-05-13 14:31:12 +0200166 .init = mpll_init,
Michael Turquette1c50da42016-06-06 23:16:17 -0700167};
Jerome Brunet889c2b72019-02-01 13:58:41 +0100168EXPORT_SYMBOL_GPL(meson_clk_mpll_ops);
169
170MODULE_DESCRIPTION("Amlogic MPLL driver");
171MODULE_AUTHOR("Michael Turquette <mturquette@baylibre.com>");
172MODULE_LICENSE("GPL v2");