blob: b925580646a0e692d6e6dadaa11865a94c14e572 [file] [log] [blame]
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001/*
2 * Copyright (C) 2014 STMicroelectronics R&D Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
10/*
11 * Authors:
12 * Stephen Gallimore <stephen.gallimore@st.com>,
13 * Pankaj Dev <pankaj.dev@st.com>.
14 */
15
16#include <linux/slab.h>
17#include <linux/of_address.h>
18#include <linux/clk-provider.h>
19
20#include "clkgen.h"
21
22/*
23 * Maximum input clock to the PLL before we divide it down by 2
24 * although in reality in actual systems this has never been seen to
25 * be used.
26 */
27#define QUADFS_NDIV_THRESHOLD 30000000
28
29#define PLL_BW_GOODREF (0L)
30#define PLL_BW_VBADREF (1L)
31#define PLL_BW_BADREF (2L)
32#define PLL_BW_VGOODREF (3L)
33
34#define QUADFS_MAX_CHAN 4
35
36struct stm_fs {
37 unsigned long ndiv;
38 unsigned long mdiv;
39 unsigned long pe;
40 unsigned long sdiv;
41 unsigned long nsdiv;
42};
43
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020044static const struct stm_fs fs216c65_rtbl[] = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010045 { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 312.5 Khz */
46 { .mdiv = 0x17, .pe = 0x25ed, .sdiv = 0x1, .nsdiv = 0 }, /* 27 MHz */
47 { .mdiv = 0x1a, .pe = 0x7b36, .sdiv = 0x2, .nsdiv = 1 }, /* 36.87 MHz */
48 { .mdiv = 0x13, .pe = 0x0, .sdiv = 0x2, .nsdiv = 1 }, /* 48 MHz */
49 { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x1, .nsdiv = 1 }, /* 108 MHz */
50};
51
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020052static const struct stm_fs fs432c65_rtbl[] = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010053 { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 625 Khz */
54 { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x2, .nsdiv = 1 }, /* 108 MHz */
55 { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x0, .nsdiv = 1 }, /* 297 MHz */
56};
57
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020058static const struct stm_fs fs660c32_rtbl[] = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010059 { .mdiv = 0x01, .pe = 0x2aaa, .sdiv = 0x8, .nsdiv = 0 }, /* 600 KHz */
60 { .mdiv = 0x02, .pe = 0x3d33, .sdiv = 0x0, .nsdiv = 0 }, /* 148.5 Mhz */
61 { .mdiv = 0x13, .pe = 0x5bcc, .sdiv = 0x0, .nsdiv = 1 }, /* 297 Mhz */
62 { .mdiv = 0x0e, .pe = 0x1025, .sdiv = 0x0, .nsdiv = 1 }, /* 333 Mhz */
63 { .mdiv = 0x0b, .pe = 0x715f, .sdiv = 0x0, .nsdiv = 1 }, /* 350 Mhz */
64};
65
66struct clkgen_quadfs_data {
67 bool reset_present;
68 bool bwfilter_present;
69 bool lockstatus_present;
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +020070 bool powerup_polarity;
71 bool standby_polarity;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010072 bool nsdiv_present;
73 struct clkgen_field ndiv;
74 struct clkgen_field ref_bw;
75 struct clkgen_field nreset;
76 struct clkgen_field npda;
77 struct clkgen_field lock_status;
78
79 struct clkgen_field nsb[QUADFS_MAX_CHAN];
80 struct clkgen_field en[QUADFS_MAX_CHAN];
81 struct clkgen_field mdiv[QUADFS_MAX_CHAN];
82 struct clkgen_field pe[QUADFS_MAX_CHAN];
83 struct clkgen_field sdiv[QUADFS_MAX_CHAN];
84 struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
85
86 const struct clk_ops *pll_ops;
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020087 const struct stm_fs *rtbl;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010088 u8 rtbl_cnt;
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020089 int (*get_rate)(unsigned long , const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010090 unsigned long *);
91};
92
93static const struct clk_ops st_quadfs_pll_c65_ops;
94static const struct clk_ops st_quadfs_pll_c32_ops;
95static const struct clk_ops st_quadfs_fs216c65_ops;
96static const struct clk_ops st_quadfs_fs432c65_ops;
97static const struct clk_ops st_quadfs_fs660c32_ops;
98
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020099static int clk_fs216c65_get_rate(unsigned long, const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100100 unsigned long *);
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200101static int clk_fs432c65_get_rate(unsigned long, const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100102 unsigned long *);
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200103static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100104 unsigned long *);
105/*
106 * Values for all of the standalone instances of this clock
107 * generator found in STiH415 and STiH416 SYSCFG register banks. Note
108 * that the individual channel standby control bits (nsb) are in the
109 * first register along with the PLL control bits.
110 */
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200111static const struct clkgen_quadfs_data st_fs216c65_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100112 /* 416 specific */
113 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
114 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
115 CLKGEN_FIELD(0x0, 0x1, 11),
116 CLKGEN_FIELD(0x0, 0x1, 12),
117 CLKGEN_FIELD(0x0, 0x1, 13) },
118 .nsdiv_present = true,
119 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
120 CLKGEN_FIELD(0x0, 0x1, 19),
121 CLKGEN_FIELD(0x0, 0x1, 20),
122 CLKGEN_FIELD(0x0, 0x1, 21) },
123 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
124 CLKGEN_FIELD(0x14, 0x1f, 0),
125 CLKGEN_FIELD(0x24, 0x1f, 0),
126 CLKGEN_FIELD(0x34, 0x1f, 0) },
127 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
128 CLKGEN_FIELD(0x20, 0x1, 0),
129 CLKGEN_FIELD(0x30, 0x1, 0),
130 CLKGEN_FIELD(0x40, 0x1, 0) },
131 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
132 .bwfilter_present = true,
133 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
134 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
135 CLKGEN_FIELD(0x18, 0xffff, 0),
136 CLKGEN_FIELD(0x28, 0xffff, 0),
137 CLKGEN_FIELD(0x38, 0xffff, 0) },
138 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
139 CLKGEN_FIELD(0x1C, 0x7, 0),
140 CLKGEN_FIELD(0x2C, 0x7, 0),
141 CLKGEN_FIELD(0x3C, 0x7, 0) },
142 .pll_ops = &st_quadfs_pll_c65_ops,
143 .rtbl = fs216c65_rtbl,
144 .rtbl_cnt = ARRAY_SIZE(fs216c65_rtbl),
145 .get_rate = clk_fs216c65_get_rate,
146};
147
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200148static const struct clkgen_quadfs_data st_fs432c65_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100149 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
150 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
151 CLKGEN_FIELD(0x0, 0x1, 11),
152 CLKGEN_FIELD(0x0, 0x1, 12),
153 CLKGEN_FIELD(0x0, 0x1, 13) },
154 .nsdiv_present = true,
155 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
156 CLKGEN_FIELD(0x0, 0x1, 19),
157 CLKGEN_FIELD(0x0, 0x1, 20),
158 CLKGEN_FIELD(0x0, 0x1, 21) },
159 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
160 CLKGEN_FIELD(0x14, 0x1f, 0),
161 CLKGEN_FIELD(0x24, 0x1f, 0),
162 CLKGEN_FIELD(0x34, 0x1f, 0) },
163 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
164 CLKGEN_FIELD(0x20, 0x1, 0),
165 CLKGEN_FIELD(0x30, 0x1, 0),
166 CLKGEN_FIELD(0x40, 0x1, 0) },
167 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
168 .bwfilter_present = true,
169 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
170 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
171 CLKGEN_FIELD(0x18, 0xffff, 0),
172 CLKGEN_FIELD(0x28, 0xffff, 0),
173 CLKGEN_FIELD(0x38, 0xffff, 0) },
174 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
175 CLKGEN_FIELD(0x1C, 0x7, 0),
176 CLKGEN_FIELD(0x2C, 0x7, 0),
177 CLKGEN_FIELD(0x3C, 0x7, 0) },
178 .pll_ops = &st_quadfs_pll_c65_ops,
179 .rtbl = fs432c65_rtbl,
180 .rtbl_cnt = ARRAY_SIZE(fs432c65_rtbl),
181 .get_rate = clk_fs432c65_get_rate,
182};
183
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200184static const struct clkgen_quadfs_data st_fs660c32_E_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100185 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
186 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
187 CLKGEN_FIELD(0x0, 0x1, 11),
188 CLKGEN_FIELD(0x0, 0x1, 12),
189 CLKGEN_FIELD(0x0, 0x1, 13) },
190 .nsdiv_present = true,
191 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
192 CLKGEN_FIELD(0x0, 0x1, 19),
193 CLKGEN_FIELD(0x0, 0x1, 20),
194 CLKGEN_FIELD(0x0, 0x1, 21) },
195 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
196 CLKGEN_FIELD(0x14, 0x1f, 0),
197 CLKGEN_FIELD(0x24, 0x1f, 0),
198 CLKGEN_FIELD(0x34, 0x1f, 0) },
199 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
200 CLKGEN_FIELD(0x20, 0x1, 0),
201 CLKGEN_FIELD(0x30, 0x1, 0),
202 CLKGEN_FIELD(0x40, 0x1, 0) },
203 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
204 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
205 CLKGEN_FIELD(0x18, 0x7fff, 0),
206 CLKGEN_FIELD(0x28, 0x7fff, 0),
207 CLKGEN_FIELD(0x38, 0x7fff, 0) },
208 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
209 CLKGEN_FIELD(0x1C, 0xf, 0),
210 CLKGEN_FIELD(0x2C, 0xf, 0),
211 CLKGEN_FIELD(0x3C, 0xf, 0) },
212 .lockstatus_present = true,
213 .lock_status = CLKGEN_FIELD(0xAC, 0x1, 0),
214 .pll_ops = &st_quadfs_pll_c32_ops,
215 .rtbl = fs660c32_rtbl,
216 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
217 .get_rate = clk_fs660c32_dig_get_rate,
218};
219
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200220static const struct clkgen_quadfs_data st_fs660c32_F_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100221 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
222 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
223 CLKGEN_FIELD(0x0, 0x1, 11),
224 CLKGEN_FIELD(0x0, 0x1, 12),
225 CLKGEN_FIELD(0x0, 0x1, 13) },
226 .nsdiv_present = true,
227 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
228 CLKGEN_FIELD(0x0, 0x1, 19),
229 CLKGEN_FIELD(0x0, 0x1, 20),
230 CLKGEN_FIELD(0x0, 0x1, 21) },
231 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
232 CLKGEN_FIELD(0x14, 0x1f, 0),
233 CLKGEN_FIELD(0x24, 0x1f, 0),
234 CLKGEN_FIELD(0x34, 0x1f, 0) },
235 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
236 CLKGEN_FIELD(0x20, 0x1, 0),
237 CLKGEN_FIELD(0x30, 0x1, 0),
238 CLKGEN_FIELD(0x40, 0x1, 0) },
239 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
240 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
241 CLKGEN_FIELD(0x18, 0x7fff, 0),
242 CLKGEN_FIELD(0x28, 0x7fff, 0),
243 CLKGEN_FIELD(0x38, 0x7fff, 0) },
244 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
245 CLKGEN_FIELD(0x1C, 0xf, 0),
246 CLKGEN_FIELD(0x2C, 0xf, 0),
247 CLKGEN_FIELD(0x3C, 0xf, 0) },
248 .lockstatus_present = true,
249 .lock_status = CLKGEN_FIELD(0xEC, 0x1, 0),
250 .pll_ops = &st_quadfs_pll_c32_ops,
251 .rtbl = fs660c32_rtbl,
252 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
253 .get_rate = clk_fs660c32_dig_get_rate,
254};
255
256/**
257 * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
258 *
259 * Traits of this clock:
260 * prepare - clk_(un)prepare only ensures parent is (un)prepared
261 * enable - clk_enable and clk_disable are functional & control the Fsyn
262 * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
263 * parent - fixed parent. No clk_set_parent support
264 */
265
266/**
267 * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
268 * its parent clock, found inside a type of
269 * ST quad channel frequency synthesizer block
270 *
271 * @hw: handle between common and hardware-specific interfaces.
272 * @ndiv: regmap field for the ndiv control.
273 * @regs_base: base address of the configuration registers.
274 * @lock: spinlock.
275 *
276 */
277struct st_clk_quadfs_pll {
278 struct clk_hw hw;
279 void __iomem *regs_base;
280 spinlock_t *lock;
281 struct clkgen_quadfs_data *data;
282 u32 ndiv;
283};
284
285#define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
286
287static int quadfs_pll_enable(struct clk_hw *hw)
288{
289 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
290 unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
291
292 if (pll->lock)
293 spin_lock_irqsave(pll->lock, flags);
294
295 /*
296 * Bring block out of reset if we have reset control.
297 */
298 if (pll->data->reset_present)
299 CLKGEN_WRITE(pll, nreset, 1);
300
301 /*
302 * Use a fixed input clock noise bandwidth filter for the moment
303 */
304 if (pll->data->bwfilter_present)
305 CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
306
307
308 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
309
310 /*
311 * Power up the PLL
312 */
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200313 CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100314
315 if (pll->lock)
316 spin_unlock_irqrestore(pll->lock, flags);
317
318 if (pll->data->lockstatus_present)
319 while (!CLKGEN_READ(pll, lock_status)) {
320 if (time_after(jiffies, timeout))
321 return -ETIMEDOUT;
322 cpu_relax();
323 }
324
325 return 0;
326}
327
328static void quadfs_pll_disable(struct clk_hw *hw)
329{
330 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
331 unsigned long flags = 0;
332
333 if (pll->lock)
334 spin_lock_irqsave(pll->lock, flags);
335
336 /*
337 * Powerdown the PLL and then put block into soft reset if we have
338 * reset control.
339 */
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200340 CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100341
342 if (pll->data->reset_present)
343 CLKGEN_WRITE(pll, nreset, 0);
344
345 if (pll->lock)
346 spin_unlock_irqrestore(pll->lock, flags);
347}
348
349static int quadfs_pll_is_enabled(struct clk_hw *hw)
350{
351 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
352 u32 npda = CLKGEN_READ(pll, npda);
353
354 return !!npda;
355}
356
357int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
358 unsigned long *rate)
359{
360 unsigned long nd = fs->ndiv + 16; /* ndiv value */
361
362 *rate = input * nd;
363
364 return 0;
365}
366
367static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
368 unsigned long parent_rate)
369{
370 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
371 unsigned long rate = 0;
372 struct stm_fs params;
373
374 params.ndiv = CLKGEN_READ(pll, ndiv);
375 if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
376 pr_err("%s:%s error calculating rate\n",
377 __clk_get_name(hw->clk), __func__);
378
379 pll->ndiv = params.ndiv;
380
381 return rate;
382}
383
384int clk_fs660c32_vco_get_params(unsigned long input,
385 unsigned long output, struct stm_fs *fs)
386{
387/* Formula
388 VCO frequency = (fin x ndiv) / pdiv
389 ndiv = VCOfreq * pdiv / fin
390 */
391 unsigned long pdiv = 1, n;
392
393 /* Output clock range: 384Mhz to 660Mhz */
394 if (output < 384000000 || output > 660000000)
395 return -EINVAL;
396
397 if (input > 40000000)
398 /* This means that PDIV would be 2 instead of 1.
399 Not supported today. */
400 return -EINVAL;
401
402 input /= 1000;
403 output /= 1000;
404
405 n = output * pdiv / input;
406 if (n < 16)
407 n = 16;
408 fs->ndiv = n - 16; /* Converting formula value to reg value */
409
410 return 0;
411}
412
413static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw, unsigned long rate
414 , unsigned long *prate)
415{
416 struct stm_fs params;
417
418 if (!clk_fs660c32_vco_get_params(*prate, rate, &params))
419 clk_fs660c32_vco_get_rate(*prate, &params, &rate);
420
421 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
422 __func__, __clk_get_name(hw->clk),
423 rate, (unsigned int)params.sdiv,
424 (unsigned int)params.mdiv,
425 (unsigned int)params.pe, (unsigned int)params.nsdiv);
426
427 return rate;
428}
429
430static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
431 unsigned long parent_rate)
432{
433 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
434 struct stm_fs params;
435 long hwrate = 0;
436 unsigned long flags = 0;
437
438 if (!rate || !parent_rate)
439 return -EINVAL;
440
441 if (!clk_fs660c32_vco_get_params(parent_rate, rate, &params))
442 clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
443
444 pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
445 __func__, __clk_get_name(hw->clk),
446 hwrate, (unsigned int)params.ndiv);
447
448 if (!hwrate)
449 return -EINVAL;
450
451 pll->ndiv = params.ndiv;
452
453 if (pll->lock)
454 spin_lock_irqsave(pll->lock, flags);
455
456 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
457
458 if (pll->lock)
459 spin_unlock_irqrestore(pll->lock, flags);
460
461 return 0;
462}
463
464static const struct clk_ops st_quadfs_pll_c65_ops = {
465 .enable = quadfs_pll_enable,
466 .disable = quadfs_pll_disable,
467 .is_enabled = quadfs_pll_is_enabled,
468};
469
470static const struct clk_ops st_quadfs_pll_c32_ops = {
471 .enable = quadfs_pll_enable,
472 .disable = quadfs_pll_disable,
473 .is_enabled = quadfs_pll_is_enabled,
474 .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
475 .round_rate = quadfs_pll_fs660c32_round_rate,
476 .set_rate = quadfs_pll_fs660c32_set_rate,
477};
478
479static struct clk * __init st_clk_register_quadfs_pll(
480 const char *name, const char *parent_name,
481 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
482 spinlock_t *lock)
483{
484 struct st_clk_quadfs_pll *pll;
485 struct clk *clk;
486 struct clk_init_data init;
487
488 /*
489 * Sanity check required pointers.
490 */
491 if (WARN_ON(!name || !parent_name))
492 return ERR_PTR(-EINVAL);
493
494 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
495 if (!pll)
496 return ERR_PTR(-ENOMEM);
497
498 init.name = name;
499 init.ops = quadfs->pll_ops;
500 init.flags = CLK_IS_BASIC;
501 init.parent_names = &parent_name;
502 init.num_parents = 1;
503
504 pll->data = quadfs;
505 pll->regs_base = reg;
506 pll->lock = lock;
507 pll->hw.init = &init;
508
509 clk = clk_register(NULL, &pll->hw);
510
511 if (IS_ERR(clk))
512 kfree(pll);
513
514 return clk;
515}
516
517/**
518 * DOC: A digital frequency synthesizer
519 *
520 * Traits of this clock:
521 * prepare - clk_(un)prepare only ensures parent is (un)prepared
522 * enable - clk_enable and clk_disable are functional
523 * rate - set rate is functional
524 * parent - fixed parent. No clk_set_parent support
525 */
526
527/**
528 * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
529 * frequency synthesizer (fsynth) block.
530 *
531 * @hw: handle between common and hardware-specific interfaces
532 *
533 * @nsb: regmap field in the output control register for the digital
534 * standby of this fsynth channel. This control is active low so
535 * the channel is in standby when the control bit is cleared.
536 *
537 * @nsdiv: regmap field in the output control register for
538 * for the optional divide by 3 of this fsynth channel. This control
539 * is active low so the divide by 3 is active when the control bit is
540 * cleared and the divide is bypassed when the bit is set.
541 */
542struct st_clk_quadfs_fsynth {
543 struct clk_hw hw;
544 void __iomem *regs_base;
545 spinlock_t *lock;
546 struct clkgen_quadfs_data *data;
547
548 u32 chan;
549 /*
550 * Cached hardware values from set_rate so we can program the
551 * hardware in enable. There are two reasons for this:
552 *
553 * 1. The registers may not be writable until the parent has been
554 * enabled.
555 *
556 * 2. It restores the clock rate when a driver does an enable
557 * on PM restore, after a suspend to RAM has lost the hardware
558 * setup.
559 */
560 u32 md;
561 u32 pe;
562 u32 sdiv;
563 u32 nsdiv;
564};
565
566#define to_quadfs_fsynth(_hw) \
567 container_of(_hw, struct st_clk_quadfs_fsynth, hw)
568
569static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
570{
571 /*
572 * Pulse the program enable register lsb to make the hardware take
573 * notice of the new md/pe values with a glitchless transition.
574 */
575 CLKGEN_WRITE(fs, en[fs->chan], 1);
576 CLKGEN_WRITE(fs, en[fs->chan], 0);
577}
578
579static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
580{
581 unsigned long flags = 0;
582
583 /*
584 * Ensure the md/pe parameters are ignored while we are
585 * reprogramming them so we can get a glitchless change
586 * when fine tuning the speed of a running clock.
587 */
588 CLKGEN_WRITE(fs, en[fs->chan], 0);
589
590 CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
591 CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
592 CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
593
594 if (fs->lock)
595 spin_lock_irqsave(fs->lock, flags);
596
597 if (fs->data->nsdiv_present)
598 CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
599
600 if (fs->lock)
601 spin_unlock_irqrestore(fs->lock, flags);
602}
603
604static int quadfs_fsynth_enable(struct clk_hw *hw)
605{
606 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
607 unsigned long flags = 0;
608
609 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
610
611 quadfs_fsynth_program_rate(fs);
612
613 if (fs->lock)
614 spin_lock_irqsave(fs->lock, flags);
615
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200616 CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100617
618 if (fs->lock)
619 spin_unlock_irqrestore(fs->lock, flags);
620
621 quadfs_fsynth_program_enable(fs);
622
623 return 0;
624}
625
626static void quadfs_fsynth_disable(struct clk_hw *hw)
627{
628 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
629 unsigned long flags = 0;
630
631 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
632
633 if (fs->lock)
634 spin_lock_irqsave(fs->lock, flags);
635
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200636 CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100637
638 if (fs->lock)
639 spin_unlock_irqrestore(fs->lock, flags);
640}
641
642static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
643{
644 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
645 u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
646
647 pr_debug("%s: %s enable bit = 0x%x\n",
648 __func__, __clk_get_name(hw->clk), nsb);
649
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200650 return fs->data->standby_polarity ? !nsb : !!nsb;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100651}
652
653#define P15 (uint64_t)(1 << 15)
654
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200655static int clk_fs216c65_get_rate(unsigned long input, const struct stm_fs *fs,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100656 unsigned long *rate)
657{
658 uint64_t res;
659 unsigned long ns;
660 unsigned long nd = 8; /* ndiv stuck at 0 => val = 8 */
661 unsigned long s;
662 long m;
663
664 m = fs->mdiv - 32;
665 s = 1 << (fs->sdiv + 1);
666 ns = (fs->nsdiv ? 1 : 3);
667
668 res = (uint64_t)(s * ns * P15 * (uint64_t)(m + 33));
669 res = res - (s * ns * fs->pe);
670 *rate = div64_u64(P15 * nd * input * 32, res);
671
672 return 0;
673}
674
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200675static int clk_fs432c65_get_rate(unsigned long input, const struct stm_fs *fs,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100676 unsigned long *rate)
677{
678 uint64_t res;
679 unsigned long nd = 16; /* ndiv value; stuck at 0 (30Mhz input) */
680 long m;
681 unsigned long sd;
682 unsigned long ns;
683
684 m = fs->mdiv - 32;
685 sd = 1 << (fs->sdiv + 1);
686 ns = (fs->nsdiv ? 1 : 3);
687
688 res = (uint64_t)(sd * ns * P15 * (uint64_t)(m + 33));
689 res = res - (sd * ns * fs->pe);
690 *rate = div64_u64(P15 * nd * input * 32, res);
691
692 return 0;
693}
694
695#define P20 (uint64_t)(1 << 20)
696
697static int clk_fs660c32_dig_get_rate(unsigned long input,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200698 const struct stm_fs *fs, unsigned long *rate)
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100699{
700 unsigned long s = (1 << fs->sdiv);
701 unsigned long ns;
702 uint64_t res;
703
704 /*
705 * 'nsdiv' is a register value ('BIN') which is translated
706 * to a decimal value according to following rules.
707 *
708 * nsdiv ns.dec
709 * 0 3
710 * 1 1
711 */
712 ns = (fs->nsdiv == 1) ? 1 : 3;
713
714 res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
715 *rate = (unsigned long)div64_u64(input * P20 * 32, res);
716
717 return 0;
718}
719
720static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
721 struct stm_fs *params)
722{
723 /*
724 * Get the initial hardware values for recalc_rate
725 */
726 params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
727 params->pe = CLKGEN_READ(fs, pe[fs->chan]);
728 params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
729
730 if (fs->data->nsdiv_present)
731 params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
732 else
733 params->nsdiv = 1;
734
735 /*
736 * If All are NULL then assume no clock rate is programmed.
737 */
738 if (!params->mdiv && !params->pe && !params->sdiv)
739 return 1;
740
741 fs->md = params->mdiv;
742 fs->pe = params->pe;
743 fs->sdiv = params->sdiv;
744 fs->nsdiv = params->nsdiv;
745
746 return 0;
747}
748
749static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
750 unsigned long prate, struct stm_fs *params)
751{
752 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
753 int (*clk_fs_get_rate)(unsigned long ,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200754 const struct stm_fs *, unsigned long *);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100755 struct stm_fs prev_params;
756 unsigned long prev_rate, rate = 0;
757 unsigned long diff_rate, prev_diff_rate = ~0;
758 int index;
759
760 clk_fs_get_rate = fs->data->get_rate;
761
762 for (index = 0; index < fs->data->rtbl_cnt; index++) {
763 prev_rate = rate;
764
765 *params = fs->data->rtbl[index];
766 prev_params = *params;
767
768 clk_fs_get_rate(prate, &fs->data->rtbl[index], &rate);
769
770 diff_rate = abs(drate - rate);
771
772 if (diff_rate > prev_diff_rate) {
773 rate = prev_rate;
774 *params = prev_params;
775 break;
776 }
777
778 prev_diff_rate = diff_rate;
779
780 if (drate == rate)
781 return rate;
782 }
783
784
785 if (index == fs->data->rtbl_cnt)
786 *params = prev_params;
787
788 return rate;
789}
790
791static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
792 unsigned long parent_rate)
793{
794 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
795 unsigned long rate = 0;
796 struct stm_fs params;
797 int (*clk_fs_get_rate)(unsigned long ,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200798 const struct stm_fs *, unsigned long *);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100799
800 clk_fs_get_rate = fs->data->get_rate;
801
802 if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
803 return 0;
804
805 if (clk_fs_get_rate(parent_rate, &params, &rate)) {
806 pr_err("%s:%s error calculating rate\n",
807 __clk_get_name(hw->clk), __func__);
808 }
809
810 pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
811
812 return rate;
813}
814
815static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
816 unsigned long *prate)
817{
818 struct stm_fs params;
819
820 rate = quadfs_find_best_rate(hw, rate, *prate, &params);
821
822 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
823 __func__, __clk_get_name(hw->clk),
824 rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
825 (unsigned int)params.pe, (unsigned int)params.nsdiv);
826
827 return rate;
828}
829
830
831static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
832 struct stm_fs *params)
833{
834 fs->md = params->mdiv;
835 fs->pe = params->pe;
836 fs->sdiv = params->sdiv;
837 fs->nsdiv = params->nsdiv;
838
839 /*
840 * In some integrations you can only change the fsynth programming when
841 * the parent entity containing it is enabled.
842 */
843 quadfs_fsynth_program_rate(fs);
844 quadfs_fsynth_program_enable(fs);
845}
846
847static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
848 unsigned long parent_rate)
849{
850 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
851 struct stm_fs params;
852 long hwrate;
853 int uninitialized_var(i);
854
855 if (!rate || !parent_rate)
856 return -EINVAL;
857
858 memset(&params, 0, sizeof(struct stm_fs));
859
860 hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
861 if (!hwrate)
862 return -EINVAL;
863
864 quadfs_program_and_enable(fs, &params);
865
866 return 0;
867}
868
869
870
871static const struct clk_ops st_quadfs_ops = {
872 .enable = quadfs_fsynth_enable,
873 .disable = quadfs_fsynth_disable,
874 .is_enabled = quadfs_fsynth_is_enabled,
875 .round_rate = quadfs_round_rate,
876 .set_rate = quadfs_set_rate,
877 .recalc_rate = quadfs_recalc_rate,
878};
879
880static struct clk * __init st_clk_register_quadfs_fsynth(
881 const char *name, const char *parent_name,
882 struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
883 spinlock_t *lock)
884{
885 struct st_clk_quadfs_fsynth *fs;
886 struct clk *clk;
887 struct clk_init_data init;
888
889 /*
890 * Sanity check required pointers, note that nsdiv3 is optional.
891 */
892 if (WARN_ON(!name || !parent_name))
893 return ERR_PTR(-EINVAL);
894
895 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
896 if (!fs)
897 return ERR_PTR(-ENOMEM);
898
899 init.name = name;
900 init.ops = &st_quadfs_ops;
901 init.flags = CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
902 init.parent_names = &parent_name;
903 init.num_parents = 1;
904
905 fs->data = quadfs;
906 fs->regs_base = reg;
907 fs->chan = chan;
908 fs->lock = lock;
909 fs->hw.init = &init;
910
911 clk = clk_register(NULL, &fs->hw);
912
913 if (IS_ERR(clk))
914 kfree(fs);
915
916 return clk;
917}
918
919static struct of_device_id quadfs_of_match[] = {
920 {
921 .compatible = "st,stih416-quadfs216",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +0200922 .data = &st_fs216c65_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100923 },
924 {
925 .compatible = "st,stih416-quadfs432",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +0200926 .data = &st_fs432c65_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100927 },
928 {
929 .compatible = "st,stih416-quadfs660-E",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +0200930 .data = &st_fs660c32_E_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100931 },
932 {
933 .compatible = "st,stih416-quadfs660-F",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +0200934 .data = &st_fs660c32_F_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100935 },
936 {}
937};
938
939static void __init st_of_create_quadfs_fsynths(
940 struct device_node *np, const char *pll_name,
941 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
942 spinlock_t *lock)
943{
944 struct clk_onecell_data *clk_data;
945 int fschan;
946
947 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
948 if (!clk_data)
949 return;
950
951 clk_data->clk_num = QUADFS_MAX_CHAN;
952 clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
953 GFP_KERNEL);
954
955 if (!clk_data->clks) {
956 kfree(clk_data);
957 return;
958 }
959
960 for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
961 struct clk *clk;
962 const char *clk_name;
963
964 if (of_property_read_string_index(np, "clock-output-names",
965 fschan, &clk_name)) {
966 break;
967 }
968
969 /*
970 * If we read an empty clock name then the channel is unused
971 */
972 if (*clk_name == '\0')
973 continue;
974
975 clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
976 quadfs, reg, fschan, lock);
977
978 /*
979 * If there was an error registering this clock output, clean
980 * up and move on to the next one.
981 */
982 if (!IS_ERR(clk)) {
983 clk_data->clks[fschan] = clk;
984 pr_debug("%s: parent %s rate %u\n",
985 __clk_get_name(clk),
986 __clk_get_name(clk_get_parent(clk)),
987 (unsigned int)clk_get_rate(clk));
988 }
989 }
990
991 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
992}
993
994static void __init st_of_quadfs_setup(struct device_node *np)
995{
996 const struct of_device_id *match;
997 struct clk *clk;
998 const char *pll_name, *clk_parent_name;
999 void __iomem *reg;
1000 spinlock_t *lock;
1001
1002 match = of_match_node(quadfs_of_match, np);
1003 if (WARN_ON(!match))
1004 return;
1005
1006 reg = of_iomap(np, 0);
1007 if (!reg)
1008 return;
1009
1010 clk_parent_name = of_clk_get_parent_name(np, 0);
1011 if (!clk_parent_name)
1012 return;
1013
1014 pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
1015 if (!pll_name)
1016 return;
1017
1018 lock = kzalloc(sizeof(*lock), GFP_KERNEL);
1019 if (!lock)
1020 goto err_exit;
1021
1022 spin_lock_init(lock);
1023
1024 clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name,
1025 (struct clkgen_quadfs_data *) match->data, reg, lock);
1026 if (IS_ERR(clk))
1027 goto err_exit;
1028 else
1029 pr_debug("%s: parent %s rate %u\n",
1030 __clk_get_name(clk),
1031 __clk_get_name(clk_get_parent(clk)),
1032 (unsigned int)clk_get_rate(clk));
1033
1034 st_of_create_quadfs_fsynths(np, pll_name,
1035 (struct clkgen_quadfs_data *)match->data,
1036 reg, lock);
1037
1038err_exit:
1039 kfree(pll_name); /* No longer need local copy of the PLL name */
1040}
1041CLK_OF_DECLARE(quadfs, "st,quadfs", st_of_quadfs_setup);