blob: 2bdd456f89e4c885af79ac65b4654e8c9d06d022 [file] [log] [blame]
Marek Vasut3e1aec4e2017-01-12 02:03:24 +01001/*
2 * Driver for IDT Versaclock 5
3 *
4 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/*
18 * Possible optimizations:
19 * - Use spread spectrum
20 * - Use integer divider in FOD if applicable
21 */
22
23#include <linux/clk.h>
24#include <linux/clk-provider.h>
25#include <linux/delay.h>
26#include <linux/i2c.h>
27#include <linux/interrupt.h>
28#include <linux/mod_devicetable.h>
29#include <linux/module.h>
30#include <linux/of.h>
31#include <linux/of_platform.h>
32#include <linux/rational.h>
33#include <linux/regmap.h>
34#include <linux/slab.h>
35
36/* VersaClock5 registers */
37#define VC5_OTP_CONTROL 0x00
38
39/* Factory-reserved register block */
40#define VC5_RSVD_DEVICE_ID 0x01
41#define VC5_RSVD_ADC_GAIN_7_0 0x02
42#define VC5_RSVD_ADC_GAIN_15_8 0x03
43#define VC5_RSVD_ADC_OFFSET_7_0 0x04
44#define VC5_RSVD_ADC_OFFSET_15_8 0x05
45#define VC5_RSVD_TEMPY 0x06
46#define VC5_RSVD_OFFSET_TBIN 0x07
47#define VC5_RSVD_GAIN 0x08
48#define VC5_RSVD_TEST_NP 0x09
49#define VC5_RSVD_UNUSED 0x0a
50#define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
51#define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
52#define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
53#define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
54#define VC5_RSVD_CLK_AMP_123 0x0f
55
56/* Configuration register block */
57#define VC5_PRIM_SRC_SHDN 0x10
58#define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
59#define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
Marek Vasut8c1ebe92017-07-09 15:28:12 +020060#define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ BIT(3)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +010061#define VC5_PRIM_SRC_SHDN_SP BIT(1)
62#define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
63
64#define VC5_VCO_BAND 0x11
65#define VC5_XTAL_X1_LOAD_CAP 0x12
66#define VC5_XTAL_X2_LOAD_CAP 0x13
67#define VC5_REF_DIVIDER 0x15
68#define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
69#define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
70
71#define VC5_VCO_CTRL_AND_PREDIV 0x16
72#define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
73
74#define VC5_FEEDBACK_INT_DIV 0x17
75#define VC5_FEEDBACK_INT_DIV_BITS 0x18
76#define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
77#define VC5_RC_CONTROL0 0x1e
78#define VC5_RC_CONTROL1 0x1f
79/* Register 0x20 is factory reserved */
80
81/* Output divider control for divider 1,2,3,4 */
82#define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
83#define VC5_OUT_DIV_CONTROL_RESET BIT(7)
84#define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
85#define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
86#define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
87#define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
88
89#define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
90#define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
91
92#define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
93#define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
94#define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
95#define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
96#define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
97/* Registers 0x30, 0x40, 0x50 are factory reserved */
98
99/* Clock control register for clock 1,2 */
100#define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
101#define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
102
103#define VC5_CLK_OE_SHDN 0x68
104#define VC5_CLK_OS_SHDN 0x69
105
106#define VC5_GLOBAL_REGISTER 0x76
107#define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
108
109/* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
110#define VC5_PLL_VCO_MIN 2500000000UL
111#define VC5_PLL_VCO_MAX 3000000000UL
112
113/* VC5 Input mux settings */
114#define VC5_MUX_IN_XIN BIT(0)
115#define VC5_MUX_IN_CLKIN BIT(1)
116
Alexey Firago9adddb02017-04-07 12:12:22 +0300117/* Maximum number of clk_out supported by this driver */
Alexey Firago1193e142017-04-07 12:12:24 +0300118#define VC5_MAX_CLK_OUT_NUM 5
Alexey Firago9adddb02017-04-07 12:12:22 +0300119
120/* Maximum number of FODs supported by this driver */
Alexey Firago1193e142017-04-07 12:12:24 +0300121#define VC5_MAX_FOD_NUM 4
Alexey Firago9adddb02017-04-07 12:12:22 +0300122
123/* flags to describe chip features */
124/* chip has built-in oscilator */
125#define VC5_HAS_INTERNAL_XTAL BIT(0)
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200126/* chip has PFD requency doubler */
127#define VC5_HAS_PFD_FREQ_DBL BIT(1)
Alexey Firago9adddb02017-04-07 12:12:22 +0300128
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100129/* Supported IDT VC5 models. */
130enum vc5_model {
131 IDT_VC5_5P49V5923,
132 IDT_VC5_5P49V5933,
Alexey Firago1193e142017-04-07 12:12:24 +0300133 IDT_VC5_5P49V5935,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100134};
135
Alexey Firago9adddb02017-04-07 12:12:22 +0300136/* Structure to describe features of a particular VC5 model */
137struct vc5_chip_info {
138 const enum vc5_model model;
139 const unsigned int clk_fod_cnt;
140 const unsigned int clk_out_cnt;
141 const u32 flags;
142};
143
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100144struct vc5_driver_data;
145
146struct vc5_hw_data {
147 struct clk_hw hw;
148 struct vc5_driver_data *vc5;
149 u32 div_int;
150 u32 div_frc;
151 unsigned int num;
152};
153
154struct vc5_driver_data {
155 struct i2c_client *client;
156 struct regmap *regmap;
Alexey Firago9adddb02017-04-07 12:12:22 +0300157 const struct vc5_chip_info *chip_info;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100158
159 struct clk *pin_xin;
160 struct clk *pin_clkin;
161 unsigned char clk_mux_ins;
162 struct clk_hw clk_mux;
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200163 struct clk_hw clk_mul;
Marek Vasut55997db2017-07-09 15:28:11 +0200164 struct clk_hw clk_pfd;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100165 struct vc5_hw_data clk_pll;
Alexey Firago9adddb02017-04-07 12:12:22 +0300166 struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
167 struct vc5_hw_data clk_out[VC5_MAX_CLK_OUT_NUM];
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100168};
169
170static const char * const vc5_mux_names[] = {
171 "mux"
172};
173
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200174static const char * const vc5_dbl_names[] = {
175 "dbl"
176};
177
Marek Vasut55997db2017-07-09 15:28:11 +0200178static const char * const vc5_pfd_names[] = {
179 "pfd"
180};
181
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100182static const char * const vc5_pll_names[] = {
183 "pll"
184};
185
186static const char * const vc5_fod_names[] = {
187 "fod0", "fod1", "fod2", "fod3",
188};
189
190static const char * const vc5_clk_out_names[] = {
191 "out0_sel_i2cb", "out1", "out2", "out3", "out4",
192};
193
194/*
195 * VersaClock5 i2c regmap
196 */
197static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
198{
199 /* Factory reserved regs, make them read-only */
200 if (reg <= 0xf)
201 return false;
202
203 /* Factory reserved regs, make them read-only */
204 if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
205 return false;
206
207 return true;
208}
209
210static const struct regmap_config vc5_regmap_config = {
211 .reg_bits = 8,
212 .val_bits = 8,
213 .cache_type = REGCACHE_RBTREE,
214 .max_register = 0x76,
215 .writeable_reg = vc5_regmap_is_writeable,
216};
217
218/*
219 * VersaClock5 input multiplexer between XTAL and CLKIN divider
220 */
221static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
222{
223 struct vc5_driver_data *vc5 =
224 container_of(hw, struct vc5_driver_data, clk_mux);
225 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
226 unsigned int src;
227
228 regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
229 src &= mask;
230
231 if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
232 return 0;
233
234 if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
235 return 1;
236
237 dev_warn(&vc5->client->dev,
238 "Invalid clock input configuration (%02x)\n", src);
239 return 0;
240}
241
242static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
243{
244 struct vc5_driver_data *vc5 =
245 container_of(hw, struct vc5_driver_data, clk_mux);
246 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
247 u8 src;
248
249 if ((index > 1) || !vc5->clk_mux_ins)
250 return -EINVAL;
251
252 if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
253 if (index == 0)
254 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
255 if (index == 1)
256 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
257 } else {
258 if (index != 0)
259 return -EINVAL;
260
261 if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
262 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
263 if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
264 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
265 }
266
267 return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
268}
269
Marek Vasut55997db2017-07-09 15:28:11 +0200270static const struct clk_ops vc5_mux_ops = {
271 .set_parent = vc5_mux_set_parent,
272 .get_parent = vc5_mux_get_parent,
273};
274
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200275static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
276 unsigned long parent_rate)
277{
278 struct vc5_driver_data *vc5 =
279 container_of(hw, struct vc5_driver_data, clk_mul);
280 unsigned int premul;
281
282 regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
283 if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
284 parent_rate *= 2;
285
286 return parent_rate;
287}
288
289static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
290 unsigned long *parent_rate)
291{
292 if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
293 return rate;
294 else
295 return -EINVAL;
296}
297
298static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
299 unsigned long parent_rate)
300{
301 struct vc5_driver_data *vc5 =
302 container_of(hw, struct vc5_driver_data, clk_mul);
303 u32 mask;
304
305 if ((parent_rate * 2) == rate)
306 mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
307 else
308 mask = 0;
309
310 regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
311 VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
312 mask);
313
314 return 0;
315}
316
317static const struct clk_ops vc5_dbl_ops = {
318 .recalc_rate = vc5_dbl_recalc_rate,
319 .round_rate = vc5_dbl_round_rate,
320 .set_rate = vc5_dbl_set_rate,
321};
322
Marek Vasut55997db2017-07-09 15:28:11 +0200323static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100324 unsigned long parent_rate)
325{
326 struct vc5_driver_data *vc5 =
Marek Vasut55997db2017-07-09 15:28:11 +0200327 container_of(hw, struct vc5_driver_data, clk_pfd);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100328 unsigned int prediv, div;
329
330 regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
331
332 /* The bypass_prediv is set, PLL fed from Ref_in directly. */
333 if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
334 return parent_rate;
335
336 regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
337
338 /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
339 if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
340 return parent_rate / 2;
341 else
342 return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
343}
344
Marek Vasut55997db2017-07-09 15:28:11 +0200345static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100346 unsigned long *parent_rate)
347{
348 unsigned long idiv;
349
350 /* PLL cannot operate with input clock above 50 MHz. */
351 if (rate > 50000000)
352 return -EINVAL;
353
354 /* CLKIN within range of PLL input, feed directly to PLL. */
355 if (*parent_rate <= 50000000)
356 return *parent_rate;
357
358 idiv = DIV_ROUND_UP(*parent_rate, rate);
359 if (idiv > 127)
360 return -EINVAL;
361
362 return *parent_rate / idiv;
363}
364
Marek Vasut55997db2017-07-09 15:28:11 +0200365static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100366 unsigned long parent_rate)
367{
368 struct vc5_driver_data *vc5 =
Marek Vasut55997db2017-07-09 15:28:11 +0200369 container_of(hw, struct vc5_driver_data, clk_pfd);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100370 unsigned long idiv;
371 u8 div;
372
373 /* CLKIN within range of PLL input, feed directly to PLL. */
374 if (parent_rate <= 50000000) {
375 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
376 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
377 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
378 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
379 return 0;
380 }
381
382 idiv = DIV_ROUND_UP(parent_rate, rate);
383
384 /* We have dedicated div-2 predivider. */
385 if (idiv == 2)
386 div = VC5_REF_DIVIDER_SEL_PREDIV2;
387 else
388 div = VC5_REF_DIVIDER_REF_DIV(idiv);
389
390 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
391 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
392 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
393
394 return 0;
395}
396
Marek Vasut55997db2017-07-09 15:28:11 +0200397static const struct clk_ops vc5_pfd_ops = {
398 .recalc_rate = vc5_pfd_recalc_rate,
399 .round_rate = vc5_pfd_round_rate,
400 .set_rate = vc5_pfd_set_rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100401};
402
403/*
404 * VersaClock5 PLL/VCO
405 */
406static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
407 unsigned long parent_rate)
408{
409 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
410 struct vc5_driver_data *vc5 = hwdata->vc5;
411 u32 div_int, div_frc;
412 u8 fb[5];
413
414 regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
415
416 div_int = (fb[0] << 4) | (fb[1] >> 4);
417 div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
418
419 /* The PLL divider has 12 integer bits and 24 fractional bits */
420 return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
421}
422
423static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
424 unsigned long *parent_rate)
425{
426 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
427 u32 div_int;
428 u64 div_frc;
429
430 if (rate < VC5_PLL_VCO_MIN)
431 rate = VC5_PLL_VCO_MIN;
432 if (rate > VC5_PLL_VCO_MAX)
433 rate = VC5_PLL_VCO_MAX;
434
435 /* Determine integer part, which is 12 bit wide */
436 div_int = rate / *parent_rate;
437 if (div_int > 0xfff)
438 rate = *parent_rate * 0xfff;
439
440 /* Determine best fractional part, which is 24 bit wide */
441 div_frc = rate % *parent_rate;
442 div_frc *= BIT(24) - 1;
443 do_div(div_frc, *parent_rate);
444
445 hwdata->div_int = div_int;
446 hwdata->div_frc = (u32)div_frc;
447
448 return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
449}
450
451static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
452 unsigned long parent_rate)
453{
454 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
455 struct vc5_driver_data *vc5 = hwdata->vc5;
456 u8 fb[5];
457
458 fb[0] = hwdata->div_int >> 4;
459 fb[1] = hwdata->div_int << 4;
460 fb[2] = hwdata->div_frc >> 16;
461 fb[3] = hwdata->div_frc >> 8;
462 fb[4] = hwdata->div_frc;
463
464 return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
465}
466
467static const struct clk_ops vc5_pll_ops = {
468 .recalc_rate = vc5_pll_recalc_rate,
469 .round_rate = vc5_pll_round_rate,
470 .set_rate = vc5_pll_set_rate,
471};
472
473static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
474 unsigned long parent_rate)
475{
476 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
477 struct vc5_driver_data *vc5 = hwdata->vc5;
478 /* VCO frequency is divided by two before entering FOD */
479 u32 f_in = parent_rate / 2;
480 u32 div_int, div_frc;
481 u8 od_int[2];
482 u8 od_frc[4];
483
484 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
485 od_int, 2);
486 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
487 od_frc, 4);
488
489 div_int = (od_int[0] << 4) | (od_int[1] >> 4);
490 div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
491 (od_frc[2] << 6) | (od_frc[3] >> 2);
492
Marek Vasut3bded562017-07-09 15:28:07 +0200493 /* Avoid division by zero if the output is not configured. */
494 if (div_int == 0 && div_frc == 0)
495 return 0;
496
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100497 /* The PLL divider has 12 integer bits and 30 fractional bits */
498 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
499}
500
501static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
502 unsigned long *parent_rate)
503{
504 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
505 /* VCO frequency is divided by two before entering FOD */
506 u32 f_in = *parent_rate / 2;
507 u32 div_int;
508 u64 div_frc;
509
510 /* Determine integer part, which is 12 bit wide */
511 div_int = f_in / rate;
512 /*
513 * WARNING: The clock chip does not output signal if the integer part
514 * of the divider is 0xfff and fractional part is non-zero.
515 * Clamp the divider at 0xffe to keep the code simple.
516 */
517 if (div_int > 0xffe) {
518 div_int = 0xffe;
519 rate = f_in / div_int;
520 }
521
522 /* Determine best fractional part, which is 30 bit wide */
523 div_frc = f_in % rate;
524 div_frc <<= 24;
525 do_div(div_frc, rate);
526
527 hwdata->div_int = div_int;
528 hwdata->div_frc = (u32)div_frc;
529
530 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
531}
532
533static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
534 unsigned long parent_rate)
535{
536 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
537 struct vc5_driver_data *vc5 = hwdata->vc5;
538 u8 data[14] = {
539 hwdata->div_frc >> 22, hwdata->div_frc >> 14,
540 hwdata->div_frc >> 6, hwdata->div_frc << 2,
541 0, 0, 0, 0, 0,
542 0, 0,
543 hwdata->div_int >> 4, hwdata->div_int << 4,
544 0
545 };
546
547 regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
548 data, 14);
549
550 /*
551 * Toggle magic bit in undocumented register for unknown reason.
552 * This is what the IDT timing commander tool does and the chip
553 * datasheet somewhat implies this is needed, but the register
554 * and the bit is not documented.
555 */
556 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
557 VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
558 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
559 VC5_GLOBAL_REGISTER_GLOBAL_RESET,
560 VC5_GLOBAL_REGISTER_GLOBAL_RESET);
561 return 0;
562}
563
564static const struct clk_ops vc5_fod_ops = {
565 .recalc_rate = vc5_fod_recalc_rate,
566 .round_rate = vc5_fod_round_rate,
567 .set_rate = vc5_fod_set_rate,
568};
569
570static int vc5_clk_out_prepare(struct clk_hw *hw)
571{
572 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
573 struct vc5_driver_data *vc5 = hwdata->vc5;
Marek Vasut718f4692017-07-09 15:28:10 +0200574 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
575 VC5_OUT_DIV_CONTROL_SEL_EXT |
576 VC5_OUT_DIV_CONTROL_EN_FOD;
577 unsigned int src;
578 int ret;
579
580 /*
581 * If the input mux is disabled, enable it first and
582 * select source from matching FOD.
583 */
584 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
585 if ((src & mask) == 0) {
586 src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
587 ret = regmap_update_bits(vc5->regmap,
588 VC5_OUT_DIV_CONTROL(hwdata->num),
589 mask | VC5_OUT_DIV_CONTROL_RESET, src);
590 if (ret)
591 return ret;
592 }
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100593
594 /* Enable the clock buffer */
595 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
596 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
597 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
598 return 0;
599}
600
601static void vc5_clk_out_unprepare(struct clk_hw *hw)
602{
603 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
604 struct vc5_driver_data *vc5 = hwdata->vc5;
605
Marek Vasuta4decf52017-07-09 15:28:08 +0200606 /* Disable the clock buffer */
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100607 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
608 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
609}
610
611static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
612{
613 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
614 struct vc5_driver_data *vc5 = hwdata->vc5;
615 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
616 VC5_OUT_DIV_CONTROL_SEL_EXT |
617 VC5_OUT_DIV_CONTROL_EN_FOD;
618 const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
619 VC5_OUT_DIV_CONTROL_EN_FOD;
620 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
621 VC5_OUT_DIV_CONTROL_SEL_EXT;
622 unsigned int src;
623
624 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
625 src &= mask;
626
Marek Vasut325b7b92017-07-09 15:28:09 +0200627 if (src == 0) /* Input mux set to DISABLED */
628 return 0;
629
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100630 if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
631 return 0;
632
633 if (src == extclk)
634 return 1;
635
636 dev_warn(&vc5->client->dev,
637 "Invalid clock output configuration (%02x)\n", src);
638 return 0;
639}
640
641static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
642{
643 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
644 struct vc5_driver_data *vc5 = hwdata->vc5;
645 const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
646 VC5_OUT_DIV_CONTROL_SELB_NORM |
647 VC5_OUT_DIV_CONTROL_SEL_EXT |
648 VC5_OUT_DIV_CONTROL_EN_FOD;
649 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
650 VC5_OUT_DIV_CONTROL_SEL_EXT;
651 u8 src = VC5_OUT_DIV_CONTROL_RESET;
652
653 if (index == 0)
654 src |= VC5_OUT_DIV_CONTROL_EN_FOD;
655 else
656 src |= extclk;
657
658 return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
659 mask, src);
660}
661
662static const struct clk_ops vc5_clk_out_ops = {
663 .prepare = vc5_clk_out_prepare,
664 .unprepare = vc5_clk_out_unprepare,
665 .set_parent = vc5_clk_out_set_parent,
666 .get_parent = vc5_clk_out_get_parent,
667};
668
669static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
670 void *data)
671{
672 struct vc5_driver_data *vc5 = data;
673 unsigned int idx = clkspec->args[0];
674
Alexey Firago9adddb02017-04-07 12:12:22 +0300675 if (idx >= vc5->chip_info->clk_out_cnt)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100676 return ERR_PTR(-EINVAL);
677
678 return &vc5->clk_out[idx].hw;
679}
680
681static int vc5_map_index_to_output(const enum vc5_model model,
682 const unsigned int n)
683{
684 switch (model) {
685 case IDT_VC5_5P49V5933:
686 return (n == 0) ? 0 : 3;
687 case IDT_VC5_5P49V5923:
Alexey Firago1193e142017-04-07 12:12:24 +0300688 case IDT_VC5_5P49V5935:
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100689 default:
690 return n;
691 }
692}
693
694static const struct of_device_id clk_vc5_of_match[];
695
696static int vc5_probe(struct i2c_client *client,
697 const struct i2c_device_id *id)
698{
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100699 struct vc5_driver_data *vc5;
700 struct clk_init_data init;
701 const char *parent_names[2];
Alexey Firago9adddb02017-04-07 12:12:22 +0300702 unsigned int n, idx = 0;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100703 int ret;
704
705 vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
706 if (vc5 == NULL)
707 return -ENOMEM;
708
709 i2c_set_clientdata(client, vc5);
710 vc5->client = client;
Alexey Firago9adddb02017-04-07 12:12:22 +0300711 vc5->chip_info = of_device_get_match_data(&client->dev);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100712
713 vc5->pin_xin = devm_clk_get(&client->dev, "xin");
714 if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
715 return -EPROBE_DEFER;
716
717 vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
718 if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
719 return -EPROBE_DEFER;
720
721 vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
722 if (IS_ERR(vc5->regmap)) {
723 dev_err(&client->dev, "failed to allocate register map\n");
724 return PTR_ERR(vc5->regmap);
725 }
726
727 /* Register clock input mux */
728 memset(&init, 0, sizeof(init));
729
730 if (!IS_ERR(vc5->pin_xin)) {
731 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
732 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
Alexey Firago9adddb02017-04-07 12:12:22 +0300733 } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100734 vc5->pin_xin = clk_register_fixed_rate(&client->dev,
735 "internal-xtal", NULL,
736 0, 25000000);
737 if (IS_ERR(vc5->pin_xin))
738 return PTR_ERR(vc5->pin_xin);
739 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
740 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
741 }
742
743 if (!IS_ERR(vc5->pin_clkin)) {
744 vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
745 parent_names[init.num_parents++] =
746 __clk_get_name(vc5->pin_clkin);
747 }
748
749 if (!init.num_parents) {
750 dev_err(&client->dev, "no input clock specified!\n");
751 return -EINVAL;
752 }
753
754 init.name = vc5_mux_names[0];
755 init.ops = &vc5_mux_ops;
756 init.flags = 0;
757 init.parent_names = parent_names;
758 vc5->clk_mux.init = &init;
759 ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
760 if (ret) {
761 dev_err(&client->dev, "unable to register %s\n", init.name);
762 goto err_clk;
763 }
764
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200765 if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
766 /* Register frequency doubler */
767 memset(&init, 0, sizeof(init));
768 init.name = vc5_dbl_names[0];
769 init.ops = &vc5_dbl_ops;
770 init.flags = CLK_SET_RATE_PARENT;
771 init.parent_names = vc5_mux_names;
772 init.num_parents = 1;
773 vc5->clk_mul.init = &init;
774 ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
775 if (ret) {
776 dev_err(&client->dev, "unable to register %s\n",
777 init.name);
778 goto err_clk;
779 }
780 }
781
Marek Vasut55997db2017-07-09 15:28:11 +0200782 /* Register PFD */
783 memset(&init, 0, sizeof(init));
784 init.name = vc5_pfd_names[0];
785 init.ops = &vc5_pfd_ops;
786 init.flags = CLK_SET_RATE_PARENT;
Marek Vasut8c1ebe92017-07-09 15:28:12 +0200787 if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
788 init.parent_names = vc5_dbl_names;
789 else
790 init.parent_names = vc5_mux_names;
Marek Vasut55997db2017-07-09 15:28:11 +0200791 init.num_parents = 1;
792 vc5->clk_pfd.init = &init;
793 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
794 if (ret) {
795 dev_err(&client->dev, "unable to register %s\n", init.name);
796 goto err_clk;
797 }
798
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100799 /* Register PLL */
800 memset(&init, 0, sizeof(init));
801 init.name = vc5_pll_names[0];
802 init.ops = &vc5_pll_ops;
803 init.flags = CLK_SET_RATE_PARENT;
Marek Vasut55997db2017-07-09 15:28:11 +0200804 init.parent_names = vc5_pfd_names;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100805 init.num_parents = 1;
806 vc5->clk_pll.num = 0;
807 vc5->clk_pll.vc5 = vc5;
808 vc5->clk_pll.hw.init = &init;
809 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
810 if (ret) {
811 dev_err(&client->dev, "unable to register %s\n", init.name);
812 goto err_clk;
813 }
814
815 /* Register FODs */
Alexey Firago9adddb02017-04-07 12:12:22 +0300816 for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
817 idx = vc5_map_index_to_output(vc5->chip_info->model, n);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100818 memset(&init, 0, sizeof(init));
819 init.name = vc5_fod_names[idx];
820 init.ops = &vc5_fod_ops;
821 init.flags = CLK_SET_RATE_PARENT;
822 init.parent_names = vc5_pll_names;
823 init.num_parents = 1;
824 vc5->clk_fod[n].num = idx;
825 vc5->clk_fod[n].vc5 = vc5;
826 vc5->clk_fod[n].hw.init = &init;
827 ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
828 if (ret) {
829 dev_err(&client->dev, "unable to register %s\n",
830 init.name);
831 goto err_clk;
832 }
833 }
834
835 /* Register MUX-connected OUT0_I2C_SELB output */
836 memset(&init, 0, sizeof(init));
837 init.name = vc5_clk_out_names[0];
838 init.ops = &vc5_clk_out_ops;
839 init.flags = CLK_SET_RATE_PARENT;
840 init.parent_names = vc5_mux_names;
841 init.num_parents = 1;
842 vc5->clk_out[0].num = idx;
843 vc5->clk_out[0].vc5 = vc5;
844 vc5->clk_out[0].hw.init = &init;
845 ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
846 if (ret) {
847 dev_err(&client->dev, "unable to register %s\n",
848 init.name);
849 goto err_clk;
850 }
851
852 /* Register FOD-connected OUTx outputs */
Alexey Firago9adddb02017-04-07 12:12:22 +0300853 for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
854 idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100855 parent_names[0] = vc5_fod_names[idx];
856 if (n == 1)
857 parent_names[1] = vc5_mux_names[0];
858 else
859 parent_names[1] = vc5_clk_out_names[n - 1];
860
861 memset(&init, 0, sizeof(init));
862 init.name = vc5_clk_out_names[idx + 1];
863 init.ops = &vc5_clk_out_ops;
864 init.flags = CLK_SET_RATE_PARENT;
865 init.parent_names = parent_names;
866 init.num_parents = 2;
867 vc5->clk_out[n].num = idx;
868 vc5->clk_out[n].vc5 = vc5;
869 vc5->clk_out[n].hw.init = &init;
870 ret = devm_clk_hw_register(&client->dev,
871 &vc5->clk_out[n].hw);
872 if (ret) {
873 dev_err(&client->dev, "unable to register %s\n",
874 init.name);
875 goto err_clk;
876 }
877 }
878
879 ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
880 if (ret) {
881 dev_err(&client->dev, "unable to add clk provider\n");
882 goto err_clk;
883 }
884
885 return 0;
886
887err_clk:
Alexey Firago9adddb02017-04-07 12:12:22 +0300888 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100889 clk_unregister_fixed_rate(vc5->pin_xin);
890 return ret;
891}
892
893static int vc5_remove(struct i2c_client *client)
894{
895 struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
896
897 of_clk_del_provider(client->dev.of_node);
898
Alexey Firago9adddb02017-04-07 12:12:22 +0300899 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100900 clk_unregister_fixed_rate(vc5->pin_xin);
901
902 return 0;
903}
904
Alexey Firago9adddb02017-04-07 12:12:22 +0300905static const struct vc5_chip_info idt_5p49v5923_info = {
906 .model = IDT_VC5_5P49V5923,
907 .clk_fod_cnt = 2,
908 .clk_out_cnt = 3,
909 .flags = 0,
910};
911
912static const struct vc5_chip_info idt_5p49v5933_info = {
913 .model = IDT_VC5_5P49V5933,
914 .clk_fod_cnt = 2,
915 .clk_out_cnt = 3,
916 .flags = VC5_HAS_INTERNAL_XTAL,
917};
918
Alexey Firago1193e142017-04-07 12:12:24 +0300919static const struct vc5_chip_info idt_5p49v5935_info = {
920 .model = IDT_VC5_5P49V5935,
921 .clk_fod_cnt = 4,
922 .clk_out_cnt = 5,
923 .flags = VC5_HAS_INTERNAL_XTAL,
924};
925
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100926static const struct i2c_device_id vc5_id[] = {
927 { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
928 { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
Alexey Firago1193e142017-04-07 12:12:24 +0300929 { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100930 { }
931};
932MODULE_DEVICE_TABLE(i2c, vc5_id);
933
934static const struct of_device_id clk_vc5_of_match[] = {
Alexey Firago9adddb02017-04-07 12:12:22 +0300935 { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
936 { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
Alexey Firago1193e142017-04-07 12:12:24 +0300937 { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100938 { },
939};
940MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
941
942static struct i2c_driver vc5_driver = {
943 .driver = {
944 .name = "vc5",
945 .of_match_table = clk_vc5_of_match,
946 },
947 .probe = vc5_probe,
948 .remove = vc5_remove,
949 .id_table = vc5_id,
950};
951module_i2c_driver(vc5_driver);
952
953MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
954MODULE_DESCRIPTION("IDT VersaClock 5 driver");
955MODULE_LICENSE("GPL");