blob: dc5bcb7cc6d7992a81a28605dd6a98ce930801d2 [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;
Gabriel FERNANDEZfc755c82014-07-15 17:20:26 +020073 bool nrst_present;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010074 struct clkgen_field ndiv;
75 struct clkgen_field ref_bw;
76 struct clkgen_field nreset;
77 struct clkgen_field npda;
78 struct clkgen_field lock_status;
79
Gabriel FERNANDEZfc755c82014-07-15 17:20:26 +020080 struct clkgen_field nrst[QUADFS_MAX_CHAN];
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010081 struct clkgen_field nsb[QUADFS_MAX_CHAN];
82 struct clkgen_field en[QUADFS_MAX_CHAN];
83 struct clkgen_field mdiv[QUADFS_MAX_CHAN];
84 struct clkgen_field pe[QUADFS_MAX_CHAN];
85 struct clkgen_field sdiv[QUADFS_MAX_CHAN];
86 struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
87
88 const struct clk_ops *pll_ops;
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020089 const struct stm_fs *rtbl;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010090 u8 rtbl_cnt;
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +020091 int (*get_rate)(unsigned long , const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +010092 unsigned long *);
93};
94
95static const struct clk_ops st_quadfs_pll_c65_ops;
96static const struct clk_ops st_quadfs_pll_c32_ops;
97static const struct clk_ops st_quadfs_fs216c65_ops;
98static const struct clk_ops st_quadfs_fs432c65_ops;
99static const struct clk_ops st_quadfs_fs660c32_ops;
100
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200101static int clk_fs216c65_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_fs432c65_get_rate(unsigned long, const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100104 unsigned long *);
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200105static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100106 unsigned long *);
107/*
108 * Values for all of the standalone instances of this clock
109 * generator found in STiH415 and STiH416 SYSCFG register banks. Note
110 * that the individual channel standby control bits (nsb) are in the
111 * first register along with the PLL control bits.
112 */
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200113static const struct clkgen_quadfs_data st_fs216c65_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100114 /* 416 specific */
115 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
116 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
117 CLKGEN_FIELD(0x0, 0x1, 11),
118 CLKGEN_FIELD(0x0, 0x1, 12),
119 CLKGEN_FIELD(0x0, 0x1, 13) },
120 .nsdiv_present = true,
121 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
122 CLKGEN_FIELD(0x0, 0x1, 19),
123 CLKGEN_FIELD(0x0, 0x1, 20),
124 CLKGEN_FIELD(0x0, 0x1, 21) },
125 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
126 CLKGEN_FIELD(0x14, 0x1f, 0),
127 CLKGEN_FIELD(0x24, 0x1f, 0),
128 CLKGEN_FIELD(0x34, 0x1f, 0) },
129 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
130 CLKGEN_FIELD(0x20, 0x1, 0),
131 CLKGEN_FIELD(0x30, 0x1, 0),
132 CLKGEN_FIELD(0x40, 0x1, 0) },
133 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
134 .bwfilter_present = true,
135 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
136 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
137 CLKGEN_FIELD(0x18, 0xffff, 0),
138 CLKGEN_FIELD(0x28, 0xffff, 0),
139 CLKGEN_FIELD(0x38, 0xffff, 0) },
140 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
141 CLKGEN_FIELD(0x1C, 0x7, 0),
142 CLKGEN_FIELD(0x2C, 0x7, 0),
143 CLKGEN_FIELD(0x3C, 0x7, 0) },
144 .pll_ops = &st_quadfs_pll_c65_ops,
145 .rtbl = fs216c65_rtbl,
146 .rtbl_cnt = ARRAY_SIZE(fs216c65_rtbl),
147 .get_rate = clk_fs216c65_get_rate,
148};
149
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200150static const struct clkgen_quadfs_data st_fs432c65_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100151 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
152 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
153 CLKGEN_FIELD(0x0, 0x1, 11),
154 CLKGEN_FIELD(0x0, 0x1, 12),
155 CLKGEN_FIELD(0x0, 0x1, 13) },
156 .nsdiv_present = true,
157 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
158 CLKGEN_FIELD(0x0, 0x1, 19),
159 CLKGEN_FIELD(0x0, 0x1, 20),
160 CLKGEN_FIELD(0x0, 0x1, 21) },
161 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
162 CLKGEN_FIELD(0x14, 0x1f, 0),
163 CLKGEN_FIELD(0x24, 0x1f, 0),
164 CLKGEN_FIELD(0x34, 0x1f, 0) },
165 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
166 CLKGEN_FIELD(0x20, 0x1, 0),
167 CLKGEN_FIELD(0x30, 0x1, 0),
168 CLKGEN_FIELD(0x40, 0x1, 0) },
169 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
170 .bwfilter_present = true,
171 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
172 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
173 CLKGEN_FIELD(0x18, 0xffff, 0),
174 CLKGEN_FIELD(0x28, 0xffff, 0),
175 CLKGEN_FIELD(0x38, 0xffff, 0) },
176 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
177 CLKGEN_FIELD(0x1C, 0x7, 0),
178 CLKGEN_FIELD(0x2C, 0x7, 0),
179 CLKGEN_FIELD(0x3C, 0x7, 0) },
180 .pll_ops = &st_quadfs_pll_c65_ops,
181 .rtbl = fs432c65_rtbl,
182 .rtbl_cnt = ARRAY_SIZE(fs432c65_rtbl),
183 .get_rate = clk_fs432c65_get_rate,
184};
185
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200186static const struct clkgen_quadfs_data st_fs660c32_E_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100187 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
188 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
189 CLKGEN_FIELD(0x0, 0x1, 11),
190 CLKGEN_FIELD(0x0, 0x1, 12),
191 CLKGEN_FIELD(0x0, 0x1, 13) },
192 .nsdiv_present = true,
193 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
194 CLKGEN_FIELD(0x0, 0x1, 19),
195 CLKGEN_FIELD(0x0, 0x1, 20),
196 CLKGEN_FIELD(0x0, 0x1, 21) },
197 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
198 CLKGEN_FIELD(0x14, 0x1f, 0),
199 CLKGEN_FIELD(0x24, 0x1f, 0),
200 CLKGEN_FIELD(0x34, 0x1f, 0) },
201 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
202 CLKGEN_FIELD(0x20, 0x1, 0),
203 CLKGEN_FIELD(0x30, 0x1, 0),
204 CLKGEN_FIELD(0x40, 0x1, 0) },
205 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
206 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
207 CLKGEN_FIELD(0x18, 0x7fff, 0),
208 CLKGEN_FIELD(0x28, 0x7fff, 0),
209 CLKGEN_FIELD(0x38, 0x7fff, 0) },
210 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
211 CLKGEN_FIELD(0x1C, 0xf, 0),
212 CLKGEN_FIELD(0x2C, 0xf, 0),
213 CLKGEN_FIELD(0x3C, 0xf, 0) },
214 .lockstatus_present = true,
215 .lock_status = CLKGEN_FIELD(0xAC, 0x1, 0),
216 .pll_ops = &st_quadfs_pll_c32_ops,
217 .rtbl = fs660c32_rtbl,
218 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
219 .get_rate = clk_fs660c32_dig_get_rate,
220};
221
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200222static const struct clkgen_quadfs_data st_fs660c32_F_416 = {
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100223 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
224 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
225 CLKGEN_FIELD(0x0, 0x1, 11),
226 CLKGEN_FIELD(0x0, 0x1, 12),
227 CLKGEN_FIELD(0x0, 0x1, 13) },
228 .nsdiv_present = true,
229 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
230 CLKGEN_FIELD(0x0, 0x1, 19),
231 CLKGEN_FIELD(0x0, 0x1, 20),
232 CLKGEN_FIELD(0x0, 0x1, 21) },
233 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
234 CLKGEN_FIELD(0x14, 0x1f, 0),
235 CLKGEN_FIELD(0x24, 0x1f, 0),
236 CLKGEN_FIELD(0x34, 0x1f, 0) },
237 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
238 CLKGEN_FIELD(0x20, 0x1, 0),
239 CLKGEN_FIELD(0x30, 0x1, 0),
240 CLKGEN_FIELD(0x40, 0x1, 0) },
241 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
242 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
243 CLKGEN_FIELD(0x18, 0x7fff, 0),
244 CLKGEN_FIELD(0x28, 0x7fff, 0),
245 CLKGEN_FIELD(0x38, 0x7fff, 0) },
246 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
247 CLKGEN_FIELD(0x1C, 0xf, 0),
248 CLKGEN_FIELD(0x2C, 0xf, 0),
249 CLKGEN_FIELD(0x3C, 0xf, 0) },
250 .lockstatus_present = true,
251 .lock_status = CLKGEN_FIELD(0xEC, 0x1, 0),
252 .pll_ops = &st_quadfs_pll_c32_ops,
253 .rtbl = fs660c32_rtbl,
254 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
255 .get_rate = clk_fs660c32_dig_get_rate,
256};
257
Gabriel FERNANDEZ51306d52014-07-15 17:20:27 +0200258static const struct clkgen_quadfs_data st_fs660c32_C_407 = {
259 .nrst_present = true,
260 .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0),
261 CLKGEN_FIELD(0x2f0, 0x1, 1),
262 CLKGEN_FIELD(0x2f0, 0x1, 2),
263 CLKGEN_FIELD(0x2f0, 0x1, 3) },
264 .npda = CLKGEN_FIELD(0x2f0, 0x1, 12),
265 .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8),
266 CLKGEN_FIELD(0x2f0, 0x1, 9),
267 CLKGEN_FIELD(0x2f0, 0x1, 10),
268 CLKGEN_FIELD(0x2f0, 0x1, 11) },
269 .nsdiv_present = true,
270 .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24),
271 CLKGEN_FIELD(0x308, 0x1, 24),
272 CLKGEN_FIELD(0x30c, 0x1, 24),
273 CLKGEN_FIELD(0x310, 0x1, 24) },
274 .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15),
275 CLKGEN_FIELD(0x308, 0x1f, 15),
276 CLKGEN_FIELD(0x30c, 0x1f, 15),
277 CLKGEN_FIELD(0x310, 0x1f, 15) },
278 .en = { CLKGEN_FIELD(0x2fc, 0x1, 0),
279 CLKGEN_FIELD(0x2fc, 0x1, 1),
280 CLKGEN_FIELD(0x2fc, 0x1, 2),
281 CLKGEN_FIELD(0x2fc, 0x1, 3) },
282 .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16),
283 .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0),
284 CLKGEN_FIELD(0x308, 0x7fff, 0),
285 CLKGEN_FIELD(0x30c, 0x7fff, 0),
286 CLKGEN_FIELD(0x310, 0x7fff, 0) },
287 .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20),
288 CLKGEN_FIELD(0x308, 0xf, 20),
289 CLKGEN_FIELD(0x30c, 0xf, 20),
290 CLKGEN_FIELD(0x310, 0xf, 20) },
291 .lockstatus_present = true,
292 .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
293 .powerup_polarity = 1,
294 .standby_polarity = 1,
295 .pll_ops = &st_quadfs_pll_c32_ops,
296 .rtbl = fs660c32_rtbl,
297 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
298 .get_rate = clk_fs660c32_dig_get_rate,
299};
300
Gabriel FERNANDEZ58de9b82014-07-15 17:20:28 +0200301static const struct clkgen_quadfs_data st_fs660c32_D_407 = {
302 .nrst_present = true,
303 .nrst = { CLKGEN_FIELD(0x2a0, 0x1, 0),
304 CLKGEN_FIELD(0x2a0, 0x1, 1),
305 CLKGEN_FIELD(0x2a0, 0x1, 2),
306 CLKGEN_FIELD(0x2a0, 0x1, 3) },
307 .ndiv = CLKGEN_FIELD(0x2a4, 0x7, 16),
308 .pe = { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
309 CLKGEN_FIELD(0x2b8, 0x7fff, 0),
310 CLKGEN_FIELD(0x2bc, 0x7fff, 0),
311 CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
312 .sdiv = { CLKGEN_FIELD(0x2b4, 0xf, 20),
313 CLKGEN_FIELD(0x2b8, 0xf, 20),
314 CLKGEN_FIELD(0x2bc, 0xf, 20),
315 CLKGEN_FIELD(0x2c0, 0xf, 20) },
316 .npda = CLKGEN_FIELD(0x2a0, 0x1, 12),
317 .nsb = { CLKGEN_FIELD(0x2a0, 0x1, 8),
318 CLKGEN_FIELD(0x2a0, 0x1, 9),
319 CLKGEN_FIELD(0x2a0, 0x1, 10),
320 CLKGEN_FIELD(0x2a0, 0x1, 11) },
321 .nsdiv_present = true,
322 .nsdiv = { CLKGEN_FIELD(0x2b4, 0x1, 24),
323 CLKGEN_FIELD(0x2b8, 0x1, 24),
324 CLKGEN_FIELD(0x2bc, 0x1, 24),
325 CLKGEN_FIELD(0x2c0, 0x1, 24) },
326 .mdiv = { CLKGEN_FIELD(0x2b4, 0x1f, 15),
327 CLKGEN_FIELD(0x2b8, 0x1f, 15),
328 CLKGEN_FIELD(0x2bc, 0x1f, 15),
329 CLKGEN_FIELD(0x2c0, 0x1f, 15) },
330 .en = { CLKGEN_FIELD(0x2ac, 0x1, 0),
331 CLKGEN_FIELD(0x2ac, 0x1, 1),
332 CLKGEN_FIELD(0x2ac, 0x1, 2),
333 CLKGEN_FIELD(0x2ac, 0x1, 3) },
334 .lockstatus_present = true,
335 .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
336 .powerup_polarity = 1,
337 .standby_polarity = 1,
338 .pll_ops = &st_quadfs_pll_c32_ops,
339 .rtbl = fs660c32_rtbl,
340 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
341 .get_rate = clk_fs660c32_dig_get_rate,};
342
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100343/**
344 * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
345 *
346 * Traits of this clock:
347 * prepare - clk_(un)prepare only ensures parent is (un)prepared
348 * enable - clk_enable and clk_disable are functional & control the Fsyn
349 * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
350 * parent - fixed parent. No clk_set_parent support
351 */
352
353/**
354 * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
355 * its parent clock, found inside a type of
356 * ST quad channel frequency synthesizer block
357 *
358 * @hw: handle between common and hardware-specific interfaces.
359 * @ndiv: regmap field for the ndiv control.
360 * @regs_base: base address of the configuration registers.
361 * @lock: spinlock.
362 *
363 */
364struct st_clk_quadfs_pll {
365 struct clk_hw hw;
366 void __iomem *regs_base;
367 spinlock_t *lock;
368 struct clkgen_quadfs_data *data;
369 u32 ndiv;
370};
371
372#define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
373
374static int quadfs_pll_enable(struct clk_hw *hw)
375{
376 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
377 unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
378
379 if (pll->lock)
380 spin_lock_irqsave(pll->lock, flags);
381
382 /*
383 * Bring block out of reset if we have reset control.
384 */
385 if (pll->data->reset_present)
386 CLKGEN_WRITE(pll, nreset, 1);
387
388 /*
389 * Use a fixed input clock noise bandwidth filter for the moment
390 */
391 if (pll->data->bwfilter_present)
392 CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
393
394
395 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
396
397 /*
398 * Power up the PLL
399 */
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200400 CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100401
402 if (pll->lock)
403 spin_unlock_irqrestore(pll->lock, flags);
404
405 if (pll->data->lockstatus_present)
406 while (!CLKGEN_READ(pll, lock_status)) {
407 if (time_after(jiffies, timeout))
408 return -ETIMEDOUT;
409 cpu_relax();
410 }
411
412 return 0;
413}
414
415static void quadfs_pll_disable(struct clk_hw *hw)
416{
417 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
418 unsigned long flags = 0;
419
420 if (pll->lock)
421 spin_lock_irqsave(pll->lock, flags);
422
423 /*
424 * Powerdown the PLL and then put block into soft reset if we have
425 * reset control.
426 */
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200427 CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100428
429 if (pll->data->reset_present)
430 CLKGEN_WRITE(pll, nreset, 0);
431
432 if (pll->lock)
433 spin_unlock_irqrestore(pll->lock, flags);
434}
435
436static int quadfs_pll_is_enabled(struct clk_hw *hw)
437{
438 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
439 u32 npda = CLKGEN_READ(pll, npda);
440
441 return !!npda;
442}
443
444int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
445 unsigned long *rate)
446{
447 unsigned long nd = fs->ndiv + 16; /* ndiv value */
448
449 *rate = input * nd;
450
451 return 0;
452}
453
454static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
455 unsigned long parent_rate)
456{
457 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
458 unsigned long rate = 0;
459 struct stm_fs params;
460
461 params.ndiv = CLKGEN_READ(pll, ndiv);
462 if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
463 pr_err("%s:%s error calculating rate\n",
464 __clk_get_name(hw->clk), __func__);
465
466 pll->ndiv = params.ndiv;
467
468 return rate;
469}
470
471int clk_fs660c32_vco_get_params(unsigned long input,
472 unsigned long output, struct stm_fs *fs)
473{
474/* Formula
475 VCO frequency = (fin x ndiv) / pdiv
476 ndiv = VCOfreq * pdiv / fin
477 */
478 unsigned long pdiv = 1, n;
479
480 /* Output clock range: 384Mhz to 660Mhz */
481 if (output < 384000000 || output > 660000000)
482 return -EINVAL;
483
484 if (input > 40000000)
485 /* This means that PDIV would be 2 instead of 1.
486 Not supported today. */
487 return -EINVAL;
488
489 input /= 1000;
490 output /= 1000;
491
492 n = output * pdiv / input;
493 if (n < 16)
494 n = 16;
495 fs->ndiv = n - 16; /* Converting formula value to reg value */
496
497 return 0;
498}
499
500static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw, unsigned long rate
501 , unsigned long *prate)
502{
503 struct stm_fs params;
504
505 if (!clk_fs660c32_vco_get_params(*prate, rate, &params))
506 clk_fs660c32_vco_get_rate(*prate, &params, &rate);
507
508 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
509 __func__, __clk_get_name(hw->clk),
510 rate, (unsigned int)params.sdiv,
511 (unsigned int)params.mdiv,
512 (unsigned int)params.pe, (unsigned int)params.nsdiv);
513
514 return rate;
515}
516
517static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
518 unsigned long parent_rate)
519{
520 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
521 struct stm_fs params;
522 long hwrate = 0;
523 unsigned long flags = 0;
524
525 if (!rate || !parent_rate)
526 return -EINVAL;
527
528 if (!clk_fs660c32_vco_get_params(parent_rate, rate, &params))
529 clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
530
531 pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
532 __func__, __clk_get_name(hw->clk),
533 hwrate, (unsigned int)params.ndiv);
534
535 if (!hwrate)
536 return -EINVAL;
537
538 pll->ndiv = params.ndiv;
539
540 if (pll->lock)
541 spin_lock_irqsave(pll->lock, flags);
542
543 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
544
545 if (pll->lock)
546 spin_unlock_irqrestore(pll->lock, flags);
547
548 return 0;
549}
550
551static const struct clk_ops st_quadfs_pll_c65_ops = {
552 .enable = quadfs_pll_enable,
553 .disable = quadfs_pll_disable,
554 .is_enabled = quadfs_pll_is_enabled,
555};
556
557static const struct clk_ops st_quadfs_pll_c32_ops = {
558 .enable = quadfs_pll_enable,
559 .disable = quadfs_pll_disable,
560 .is_enabled = quadfs_pll_is_enabled,
561 .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
562 .round_rate = quadfs_pll_fs660c32_round_rate,
563 .set_rate = quadfs_pll_fs660c32_set_rate,
564};
565
566static struct clk * __init st_clk_register_quadfs_pll(
567 const char *name, const char *parent_name,
568 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
569 spinlock_t *lock)
570{
571 struct st_clk_quadfs_pll *pll;
572 struct clk *clk;
573 struct clk_init_data init;
574
575 /*
576 * Sanity check required pointers.
577 */
578 if (WARN_ON(!name || !parent_name))
579 return ERR_PTR(-EINVAL);
580
581 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
582 if (!pll)
583 return ERR_PTR(-ENOMEM);
584
585 init.name = name;
586 init.ops = quadfs->pll_ops;
587 init.flags = CLK_IS_BASIC;
588 init.parent_names = &parent_name;
589 init.num_parents = 1;
590
591 pll->data = quadfs;
592 pll->regs_base = reg;
593 pll->lock = lock;
594 pll->hw.init = &init;
595
596 clk = clk_register(NULL, &pll->hw);
597
598 if (IS_ERR(clk))
599 kfree(pll);
600
601 return clk;
602}
603
604/**
605 * DOC: A digital frequency synthesizer
606 *
607 * Traits of this clock:
608 * prepare - clk_(un)prepare only ensures parent is (un)prepared
609 * enable - clk_enable and clk_disable are functional
610 * rate - set rate is functional
611 * parent - fixed parent. No clk_set_parent support
612 */
613
614/**
615 * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
616 * frequency synthesizer (fsynth) block.
617 *
618 * @hw: handle between common and hardware-specific interfaces
619 *
620 * @nsb: regmap field in the output control register for the digital
621 * standby of this fsynth channel. This control is active low so
622 * the channel is in standby when the control bit is cleared.
623 *
624 * @nsdiv: regmap field in the output control register for
625 * for the optional divide by 3 of this fsynth channel. This control
626 * is active low so the divide by 3 is active when the control bit is
627 * cleared and the divide is bypassed when the bit is set.
628 */
629struct st_clk_quadfs_fsynth {
630 struct clk_hw hw;
631 void __iomem *regs_base;
632 spinlock_t *lock;
633 struct clkgen_quadfs_data *data;
634
635 u32 chan;
636 /*
637 * Cached hardware values from set_rate so we can program the
638 * hardware in enable. There are two reasons for this:
639 *
640 * 1. The registers may not be writable until the parent has been
641 * enabled.
642 *
643 * 2. It restores the clock rate when a driver does an enable
644 * on PM restore, after a suspend to RAM has lost the hardware
645 * setup.
646 */
647 u32 md;
648 u32 pe;
649 u32 sdiv;
650 u32 nsdiv;
651};
652
653#define to_quadfs_fsynth(_hw) \
654 container_of(_hw, struct st_clk_quadfs_fsynth, hw)
655
656static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
657{
658 /*
659 * Pulse the program enable register lsb to make the hardware take
660 * notice of the new md/pe values with a glitchless transition.
661 */
662 CLKGEN_WRITE(fs, en[fs->chan], 1);
663 CLKGEN_WRITE(fs, en[fs->chan], 0);
664}
665
666static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
667{
668 unsigned long flags = 0;
669
670 /*
671 * Ensure the md/pe parameters are ignored while we are
672 * reprogramming them so we can get a glitchless change
673 * when fine tuning the speed of a running clock.
674 */
675 CLKGEN_WRITE(fs, en[fs->chan], 0);
676
677 CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
678 CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
679 CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
680
681 if (fs->lock)
682 spin_lock_irqsave(fs->lock, flags);
683
684 if (fs->data->nsdiv_present)
685 CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
686
687 if (fs->lock)
688 spin_unlock_irqrestore(fs->lock, flags);
689}
690
691static int quadfs_fsynth_enable(struct clk_hw *hw)
692{
693 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
694 unsigned long flags = 0;
695
696 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
697
698 quadfs_fsynth_program_rate(fs);
699
700 if (fs->lock)
701 spin_lock_irqsave(fs->lock, flags);
702
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200703 CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100704
Gabriel FERNANDEZfc755c82014-07-15 17:20:26 +0200705 if (fs->data->nrst_present)
706 CLKGEN_WRITE(fs, nrst[fs->chan], 0);
707
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100708 if (fs->lock)
709 spin_unlock_irqrestore(fs->lock, flags);
710
711 quadfs_fsynth_program_enable(fs);
712
713 return 0;
714}
715
716static void quadfs_fsynth_disable(struct clk_hw *hw)
717{
718 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
719 unsigned long flags = 0;
720
721 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
722
723 if (fs->lock)
724 spin_lock_irqsave(fs->lock, flags);
725
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200726 CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100727
728 if (fs->lock)
729 spin_unlock_irqrestore(fs->lock, flags);
730}
731
732static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
733{
734 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
735 u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
736
737 pr_debug("%s: %s enable bit = 0x%x\n",
738 __func__, __clk_get_name(hw->clk), nsb);
739
Gabriel FERNANDEZ8f26df82014-07-15 17:20:25 +0200740 return fs->data->standby_polarity ? !nsb : !!nsb;
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100741}
742
743#define P15 (uint64_t)(1 << 15)
744
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200745static int clk_fs216c65_get_rate(unsigned long input, const struct stm_fs *fs,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100746 unsigned long *rate)
747{
748 uint64_t res;
749 unsigned long ns;
750 unsigned long nd = 8; /* ndiv stuck at 0 => val = 8 */
751 unsigned long s;
752 long m;
753
754 m = fs->mdiv - 32;
755 s = 1 << (fs->sdiv + 1);
756 ns = (fs->nsdiv ? 1 : 3);
757
758 res = (uint64_t)(s * ns * P15 * (uint64_t)(m + 33));
759 res = res - (s * ns * fs->pe);
760 *rate = div64_u64(P15 * nd * input * 32, res);
761
762 return 0;
763}
764
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200765static int clk_fs432c65_get_rate(unsigned long input, const struct stm_fs *fs,
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100766 unsigned long *rate)
767{
768 uint64_t res;
769 unsigned long nd = 16; /* ndiv value; stuck at 0 (30Mhz input) */
770 long m;
771 unsigned long sd;
772 unsigned long ns;
773
774 m = fs->mdiv - 32;
775 sd = 1 << (fs->sdiv + 1);
776 ns = (fs->nsdiv ? 1 : 3);
777
778 res = (uint64_t)(sd * ns * P15 * (uint64_t)(m + 33));
779 res = res - (sd * ns * fs->pe);
780 *rate = div64_u64(P15 * nd * input * 32, res);
781
782 return 0;
783}
784
785#define P20 (uint64_t)(1 << 20)
786
787static int clk_fs660c32_dig_get_rate(unsigned long input,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200788 const struct stm_fs *fs, unsigned long *rate)
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100789{
790 unsigned long s = (1 << fs->sdiv);
791 unsigned long ns;
792 uint64_t res;
793
794 /*
795 * 'nsdiv' is a register value ('BIN') which is translated
796 * to a decimal value according to following rules.
797 *
798 * nsdiv ns.dec
799 * 0 3
800 * 1 1
801 */
802 ns = (fs->nsdiv == 1) ? 1 : 3;
803
804 res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
805 *rate = (unsigned long)div64_u64(input * P20 * 32, res);
806
807 return 0;
808}
809
810static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
811 struct stm_fs *params)
812{
813 /*
814 * Get the initial hardware values for recalc_rate
815 */
816 params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
817 params->pe = CLKGEN_READ(fs, pe[fs->chan]);
818 params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
819
820 if (fs->data->nsdiv_present)
821 params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
822 else
823 params->nsdiv = 1;
824
825 /*
826 * If All are NULL then assume no clock rate is programmed.
827 */
828 if (!params->mdiv && !params->pe && !params->sdiv)
829 return 1;
830
831 fs->md = params->mdiv;
832 fs->pe = params->pe;
833 fs->sdiv = params->sdiv;
834 fs->nsdiv = params->nsdiv;
835
836 return 0;
837}
838
839static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
840 unsigned long prate, struct stm_fs *params)
841{
842 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
843 int (*clk_fs_get_rate)(unsigned long ,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200844 const struct stm_fs *, unsigned long *);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100845 struct stm_fs prev_params;
846 unsigned long prev_rate, rate = 0;
847 unsigned long diff_rate, prev_diff_rate = ~0;
848 int index;
849
850 clk_fs_get_rate = fs->data->get_rate;
851
852 for (index = 0; index < fs->data->rtbl_cnt; index++) {
853 prev_rate = rate;
854
855 *params = fs->data->rtbl[index];
856 prev_params = *params;
857
858 clk_fs_get_rate(prate, &fs->data->rtbl[index], &rate);
859
860 diff_rate = abs(drate - rate);
861
862 if (diff_rate > prev_diff_rate) {
863 rate = prev_rate;
864 *params = prev_params;
865 break;
866 }
867
868 prev_diff_rate = diff_rate;
869
870 if (drate == rate)
871 return rate;
872 }
873
874
875 if (index == fs->data->rtbl_cnt)
876 *params = prev_params;
877
878 return rate;
879}
880
881static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
882 unsigned long parent_rate)
883{
884 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
885 unsigned long rate = 0;
886 struct stm_fs params;
887 int (*clk_fs_get_rate)(unsigned long ,
Gabriel FERNANDEZ4abb1b42014-07-15 17:20:18 +0200888 const struct stm_fs *, unsigned long *);
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +0100889
890 clk_fs_get_rate = fs->data->get_rate;
891
892 if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
893 return 0;
894
895 if (clk_fs_get_rate(parent_rate, &params, &rate)) {
896 pr_err("%s:%s error calculating rate\n",
897 __clk_get_name(hw->clk), __func__);
898 }
899
900 pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
901
902 return rate;
903}
904
905static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
906 unsigned long *prate)
907{
908 struct stm_fs params;
909
910 rate = quadfs_find_best_rate(hw, rate, *prate, &params);
911
912 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
913 __func__, __clk_get_name(hw->clk),
914 rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
915 (unsigned int)params.pe, (unsigned int)params.nsdiv);
916
917 return rate;
918}
919
920
921static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
922 struct stm_fs *params)
923{
924 fs->md = params->mdiv;
925 fs->pe = params->pe;
926 fs->sdiv = params->sdiv;
927 fs->nsdiv = params->nsdiv;
928
929 /*
930 * In some integrations you can only change the fsynth programming when
931 * the parent entity containing it is enabled.
932 */
933 quadfs_fsynth_program_rate(fs);
934 quadfs_fsynth_program_enable(fs);
935}
936
937static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
938 unsigned long parent_rate)
939{
940 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
941 struct stm_fs params;
942 long hwrate;
943 int uninitialized_var(i);
944
945 if (!rate || !parent_rate)
946 return -EINVAL;
947
948 memset(&params, 0, sizeof(struct stm_fs));
949
950 hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
951 if (!hwrate)
952 return -EINVAL;
953
954 quadfs_program_and_enable(fs, &params);
955
956 return 0;
957}
958
959
960
961static const struct clk_ops st_quadfs_ops = {
962 .enable = quadfs_fsynth_enable,
963 .disable = quadfs_fsynth_disable,
964 .is_enabled = quadfs_fsynth_is_enabled,
965 .round_rate = quadfs_round_rate,
966 .set_rate = quadfs_set_rate,
967 .recalc_rate = quadfs_recalc_rate,
968};
969
970static struct clk * __init st_clk_register_quadfs_fsynth(
971 const char *name, const char *parent_name,
972 struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
973 spinlock_t *lock)
974{
975 struct st_clk_quadfs_fsynth *fs;
976 struct clk *clk;
977 struct clk_init_data init;
978
979 /*
980 * Sanity check required pointers, note that nsdiv3 is optional.
981 */
982 if (WARN_ON(!name || !parent_name))
983 return ERR_PTR(-EINVAL);
984
985 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
986 if (!fs)
987 return ERR_PTR(-ENOMEM);
988
989 init.name = name;
990 init.ops = &st_quadfs_ops;
991 init.flags = CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
992 init.parent_names = &parent_name;
993 init.num_parents = 1;
994
995 fs->data = quadfs;
996 fs->regs_base = reg;
997 fs->chan = chan;
998 fs->lock = lock;
999 fs->hw.init = &init;
1000
1001 clk = clk_register(NULL, &fs->hw);
1002
1003 if (IS_ERR(clk))
1004 kfree(fs);
1005
1006 return clk;
1007}
1008
1009static struct of_device_id quadfs_of_match[] = {
1010 {
1011 .compatible = "st,stih416-quadfs216",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +02001012 .data = &st_fs216c65_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001013 },
1014 {
1015 .compatible = "st,stih416-quadfs432",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +02001016 .data = &st_fs432c65_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001017 },
1018 {
1019 .compatible = "st,stih416-quadfs660-E",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +02001020 .data = &st_fs660c32_E_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001021 },
1022 {
1023 .compatible = "st,stih416-quadfs660-F",
Gabriel FERNANDEZ79bb8aa12014-07-15 17:20:20 +02001024 .data = &st_fs660c32_F_416
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001025 },
Gabriel FERNANDEZ51306d52014-07-15 17:20:27 +02001026 {
1027 .compatible = "st,stih407-quadfs660-C",
1028 .data = &st_fs660c32_C_407
1029 },
1030 {
1031 .compatible = "st,stih407-quadfs660-D",
1032 .data = &st_fs660c32_D_407
1033 },
Gabriel FERNANDEZ58de9b82014-07-15 17:20:28 +02001034 {
1035 .compatible = "st,stih407-quadfs660-D",
1036 .data = (void *)&st_fs660c32_D_407
1037 },
Gabriel FERNANDEZ5f7aa902014-02-27 16:24:17 +01001038 {}
1039};
1040
1041static void __init st_of_create_quadfs_fsynths(
1042 struct device_node *np, const char *pll_name,
1043 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
1044 spinlock_t *lock)
1045{
1046 struct clk_onecell_data *clk_data;
1047 int fschan;
1048
1049 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
1050 if (!clk_data)
1051 return;
1052
1053 clk_data->clk_num = QUADFS_MAX_CHAN;
1054 clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
1055 GFP_KERNEL);
1056
1057 if (!clk_data->clks) {
1058 kfree(clk_data);
1059 return;
1060 }
1061
1062 for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
1063 struct clk *clk;
1064 const char *clk_name;
1065
1066 if (of_property_read_string_index(np, "clock-output-names",
1067 fschan, &clk_name)) {
1068 break;
1069 }
1070
1071 /*
1072 * If we read an empty clock name then the channel is unused
1073 */
1074 if (*clk_name == '\0')
1075 continue;
1076
1077 clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
1078 quadfs, reg, fschan, lock);
1079
1080 /*
1081 * If there was an error registering this clock output, clean
1082 * up and move on to the next one.
1083 */
1084 if (!IS_ERR(clk)) {
1085 clk_data->clks[fschan] = clk;
1086 pr_debug("%s: parent %s rate %u\n",
1087 __clk_get_name(clk),
1088 __clk_get_name(clk_get_parent(clk)),
1089 (unsigned int)clk_get_rate(clk));
1090 }
1091 }
1092
1093 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
1094}
1095
1096static void __init st_of_quadfs_setup(struct device_node *np)
1097{
1098 const struct of_device_id *match;
1099 struct clk *clk;
1100 const char *pll_name, *clk_parent_name;
1101 void __iomem *reg;
1102 spinlock_t *lock;
1103
1104 match = of_match_node(quadfs_of_match, np);
1105 if (WARN_ON(!match))
1106 return;
1107
1108 reg = of_iomap(np, 0);
1109 if (!reg)
1110 return;
1111
1112 clk_parent_name = of_clk_get_parent_name(np, 0);
1113 if (!clk_parent_name)
1114 return;
1115
1116 pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
1117 if (!pll_name)
1118 return;
1119
1120 lock = kzalloc(sizeof(*lock), GFP_KERNEL);
1121 if (!lock)
1122 goto err_exit;
1123
1124 spin_lock_init(lock);
1125
1126 clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name,
1127 (struct clkgen_quadfs_data *) match->data, reg, lock);
1128 if (IS_ERR(clk))
1129 goto err_exit;
1130 else
1131 pr_debug("%s: parent %s rate %u\n",
1132 __clk_get_name(clk),
1133 __clk_get_name(clk_get_parent(clk)),
1134 (unsigned int)clk_get_rate(clk));
1135
1136 st_of_create_quadfs_fsynths(np, pll_name,
1137 (struct clkgen_quadfs_data *)match->data,
1138 reg, lock);
1139
1140err_exit:
1141 kfree(pll_name); /* No longer need local copy of the PLL name */
1142}
1143CLK_OF_DECLARE(quadfs, "st,quadfs", st_of_quadfs_setup);