1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for IDT Versaclock 5
9 * Possible optimizations:
10 * - Use spread spectrum
11 * - Use integer divider in FOD if applicable
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/rational.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
27 /* VersaClock5 registers */
28 #define VC5_OTP_CONTROL 0x00
30 /* Factory-reserved register block */
31 #define VC5_RSVD_DEVICE_ID 0x01
32 #define VC5_RSVD_ADC_GAIN_7_0 0x02
33 #define VC5_RSVD_ADC_GAIN_15_8 0x03
34 #define VC5_RSVD_ADC_OFFSET_7_0 0x04
35 #define VC5_RSVD_ADC_OFFSET_15_8 0x05
36 #define VC5_RSVD_TEMPY 0x06
37 #define VC5_RSVD_OFFSET_TBIN 0x07
38 #define VC5_RSVD_GAIN 0x08
39 #define VC5_RSVD_TEST_NP 0x09
40 #define VC5_RSVD_UNUSED 0x0a
41 #define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
42 #define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
43 #define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
44 #define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
45 #define VC5_RSVD_CLK_AMP_123 0x0f
47 /* Configuration register block */
48 #define VC5_PRIM_SRC_SHDN 0x10
49 #define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
50 #define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
51 #define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ BIT(3)
52 #define VC5_PRIM_SRC_SHDN_SP BIT(1)
53 #define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
55 #define VC5_VCO_BAND 0x11
56 #define VC5_XTAL_X1_LOAD_CAP 0x12
57 #define VC5_XTAL_X2_LOAD_CAP 0x13
58 #define VC5_REF_DIVIDER 0x15
59 #define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
60 #define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
62 #define VC5_VCO_CTRL_AND_PREDIV 0x16
63 #define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
65 #define VC5_FEEDBACK_INT_DIV 0x17
66 #define VC5_FEEDBACK_INT_DIV_BITS 0x18
67 #define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
68 #define VC5_RC_CONTROL0 0x1e
69 #define VC5_RC_CONTROL1 0x1f
70 /* Register 0x20 is factory reserved */
72 /* Output divider control for divider 1,2,3,4 */
73 #define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
74 #define VC5_OUT_DIV_CONTROL_RESET BIT(7)
75 #define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
76 #define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
77 #define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
78 #define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
80 #define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
81 #define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
83 #define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
84 #define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
85 #define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
86 #define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
87 #define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
88 /* Registers 0x30, 0x40, 0x50 are factory reserved */
90 /* Clock control register for clock 1,2 */
91 #define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
92 #define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
94 #define VC5_CLK_OE_SHDN 0x68
95 #define VC5_CLK_OS_SHDN 0x69
97 #define VC5_GLOBAL_REGISTER 0x76
98 #define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
100 /* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
101 #define VC5_PLL_VCO_MIN 2500000000UL
102 #define VC5_PLL_VCO_MAX 3000000000UL
104 /* VC5 Input mux settings */
105 #define VC5_MUX_IN_XIN BIT(0)
106 #define VC5_MUX_IN_CLKIN BIT(1)
108 /* Maximum number of clk_out supported by this driver */
109 #define VC5_MAX_CLK_OUT_NUM 5
111 /* Maximum number of FODs supported by this driver */
112 #define VC5_MAX_FOD_NUM 4
114 /* flags to describe chip features */
115 /* chip has built-in oscilator */
116 #define VC5_HAS_INTERNAL_XTAL BIT(0)
117 /* chip has PFD requency doubler */
118 #define VC5_HAS_PFD_FREQ_DBL BIT(1)
120 /* Supported IDT VC5 models. */
130 /* Structure to describe features of a particular VC5 model */
131 struct vc5_chip_info {
132 const enum vc5_model model;
133 const unsigned int clk_fod_cnt;
134 const unsigned int clk_out_cnt;
138 struct vc5_driver_data;
142 struct vc5_driver_data *vc5;
148 struct vc5_driver_data {
149 struct i2c_client *client;
150 struct regmap *regmap;
151 const struct vc5_chip_info *chip_info;
154 struct clk *pin_clkin;
155 unsigned char clk_mux_ins;
156 struct clk_hw clk_mux;
157 struct clk_hw clk_mul;
158 struct clk_hw clk_pfd;
159 struct vc5_hw_data clk_pll;
160 struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
161 struct vc5_hw_data clk_out[VC5_MAX_CLK_OUT_NUM];
164 static const char * const vc5_mux_names[] = {
168 static const char * const vc5_dbl_names[] = {
172 static const char * const vc5_pfd_names[] = {
176 static const char * const vc5_pll_names[] = {
180 static const char * const vc5_fod_names[] = {
181 "fod0", "fod1", "fod2", "fod3",
184 static const char * const vc5_clk_out_names[] = {
185 "out0_sel_i2cb", "out1", "out2", "out3", "out4",
189 * VersaClock5 i2c regmap
191 static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
193 /* Factory reserved regs, make them read-only */
197 /* Factory reserved regs, make them read-only */
198 if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
204 static const struct regmap_config vc5_regmap_config = {
207 .cache_type = REGCACHE_RBTREE,
208 .max_register = 0x76,
209 .writeable_reg = vc5_regmap_is_writeable,
213 * VersaClock5 input multiplexer between XTAL and CLKIN divider
215 static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
217 struct vc5_driver_data *vc5 =
218 container_of(hw, struct vc5_driver_data, clk_mux);
219 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
222 regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
225 if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
228 if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
231 dev_warn(&vc5->client->dev,
232 "Invalid clock input configuration (%02x)\n", src);
236 static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
238 struct vc5_driver_data *vc5 =
239 container_of(hw, struct vc5_driver_data, clk_mux);
240 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
243 if ((index > 1) || !vc5->clk_mux_ins)
246 if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
248 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
250 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
255 if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
256 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
257 else if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
258 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
259 else /* Invalid; should have been caught by vc5_probe() */
263 return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
266 static const struct clk_ops vc5_mux_ops = {
267 .set_parent = vc5_mux_set_parent,
268 .get_parent = vc5_mux_get_parent,
271 static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
272 unsigned long parent_rate)
274 struct vc5_driver_data *vc5 =
275 container_of(hw, struct vc5_driver_data, clk_mul);
278 regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
279 if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
285 static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
286 unsigned long *parent_rate)
288 if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
294 static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
295 unsigned long parent_rate)
297 struct vc5_driver_data *vc5 =
298 container_of(hw, struct vc5_driver_data, clk_mul);
301 if ((parent_rate * 2) == rate)
302 mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
306 regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
307 VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
313 static const struct clk_ops vc5_dbl_ops = {
314 .recalc_rate = vc5_dbl_recalc_rate,
315 .round_rate = vc5_dbl_round_rate,
316 .set_rate = vc5_dbl_set_rate,
319 static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
320 unsigned long parent_rate)
322 struct vc5_driver_data *vc5 =
323 container_of(hw, struct vc5_driver_data, clk_pfd);
324 unsigned int prediv, div;
326 regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
328 /* The bypass_prediv is set, PLL fed from Ref_in directly. */
329 if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
332 regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
334 /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
335 if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
336 return parent_rate / 2;
338 return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
341 static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
342 unsigned long *parent_rate)
346 /* PLL cannot operate with input clock above 50 MHz. */
350 /* CLKIN within range of PLL input, feed directly to PLL. */
351 if (*parent_rate <= 50000000)
354 idiv = DIV_ROUND_UP(*parent_rate, rate);
358 return *parent_rate / idiv;
361 static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
362 unsigned long parent_rate)
364 struct vc5_driver_data *vc5 =
365 container_of(hw, struct vc5_driver_data, clk_pfd);
369 /* CLKIN within range of PLL input, feed directly to PLL. */
370 if (parent_rate <= 50000000) {
371 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
372 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
373 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
374 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
378 idiv = DIV_ROUND_UP(parent_rate, rate);
380 /* We have dedicated div-2 predivider. */
382 div = VC5_REF_DIVIDER_SEL_PREDIV2;
384 div = VC5_REF_DIVIDER_REF_DIV(idiv);
386 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
387 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
388 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
393 static const struct clk_ops vc5_pfd_ops = {
394 .recalc_rate = vc5_pfd_recalc_rate,
395 .round_rate = vc5_pfd_round_rate,
396 .set_rate = vc5_pfd_set_rate,
400 * VersaClock5 PLL/VCO
402 static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
403 unsigned long parent_rate)
405 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
406 struct vc5_driver_data *vc5 = hwdata->vc5;
407 u32 div_int, div_frc;
410 regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
412 div_int = (fb[0] << 4) | (fb[1] >> 4);
413 div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
415 /* The PLL divider has 12 integer bits and 24 fractional bits */
416 return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
419 static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
420 unsigned long *parent_rate)
422 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
426 if (rate < VC5_PLL_VCO_MIN)
427 rate = VC5_PLL_VCO_MIN;
428 if (rate > VC5_PLL_VCO_MAX)
429 rate = VC5_PLL_VCO_MAX;
431 /* Determine integer part, which is 12 bit wide */
432 div_int = rate / *parent_rate;
434 rate = *parent_rate * 0xfff;
436 /* Determine best fractional part, which is 24 bit wide */
437 div_frc = rate % *parent_rate;
438 div_frc *= BIT(24) - 1;
439 do_div(div_frc, *parent_rate);
441 hwdata->div_int = div_int;
442 hwdata->div_frc = (u32)div_frc;
444 return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
447 static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
448 unsigned long parent_rate)
450 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
451 struct vc5_driver_data *vc5 = hwdata->vc5;
454 fb[0] = hwdata->div_int >> 4;
455 fb[1] = hwdata->div_int << 4;
456 fb[2] = hwdata->div_frc >> 16;
457 fb[3] = hwdata->div_frc >> 8;
458 fb[4] = hwdata->div_frc;
460 return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
463 static const struct clk_ops vc5_pll_ops = {
464 .recalc_rate = vc5_pll_recalc_rate,
465 .round_rate = vc5_pll_round_rate,
466 .set_rate = vc5_pll_set_rate,
469 static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
470 unsigned long parent_rate)
472 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
473 struct vc5_driver_data *vc5 = hwdata->vc5;
474 /* VCO frequency is divided by two before entering FOD */
475 u32 f_in = parent_rate / 2;
476 u32 div_int, div_frc;
480 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
482 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
485 div_int = (od_int[0] << 4) | (od_int[1] >> 4);
486 div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
487 (od_frc[2] << 6) | (od_frc[3] >> 2);
489 /* Avoid division by zero if the output is not configured. */
490 if (div_int == 0 && div_frc == 0)
493 /* The PLL divider has 12 integer bits and 30 fractional bits */
494 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
497 static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
498 unsigned long *parent_rate)
500 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
501 /* VCO frequency is divided by two before entering FOD */
502 u32 f_in = *parent_rate / 2;
506 /* Determine integer part, which is 12 bit wide */
507 div_int = f_in / rate;
509 * WARNING: The clock chip does not output signal if the integer part
510 * of the divider is 0xfff and fractional part is non-zero.
511 * Clamp the divider at 0xffe to keep the code simple.
513 if (div_int > 0xffe) {
515 rate = f_in / div_int;
518 /* Determine best fractional part, which is 30 bit wide */
519 div_frc = f_in % rate;
521 do_div(div_frc, rate);
523 hwdata->div_int = div_int;
524 hwdata->div_frc = (u32)div_frc;
526 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
529 static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
530 unsigned long parent_rate)
532 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
533 struct vc5_driver_data *vc5 = hwdata->vc5;
535 hwdata->div_frc >> 22, hwdata->div_frc >> 14,
536 hwdata->div_frc >> 6, hwdata->div_frc << 2,
539 hwdata->div_int >> 4, hwdata->div_int << 4,
543 regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
547 * Toggle magic bit in undocumented register for unknown reason.
548 * This is what the IDT timing commander tool does and the chip
549 * datasheet somewhat implies this is needed, but the register
550 * and the bit is not documented.
552 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
553 VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
554 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
555 VC5_GLOBAL_REGISTER_GLOBAL_RESET,
556 VC5_GLOBAL_REGISTER_GLOBAL_RESET);
560 static const struct clk_ops vc5_fod_ops = {
561 .recalc_rate = vc5_fod_recalc_rate,
562 .round_rate = vc5_fod_round_rate,
563 .set_rate = vc5_fod_set_rate,
566 static int vc5_clk_out_prepare(struct clk_hw *hw)
568 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
569 struct vc5_driver_data *vc5 = hwdata->vc5;
570 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
571 VC5_OUT_DIV_CONTROL_SEL_EXT |
572 VC5_OUT_DIV_CONTROL_EN_FOD;
577 * If the input mux is disabled, enable it first and
578 * select source from matching FOD.
580 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
581 if ((src & mask) == 0) {
582 src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
583 ret = regmap_update_bits(vc5->regmap,
584 VC5_OUT_DIV_CONTROL(hwdata->num),
585 mask | VC5_OUT_DIV_CONTROL_RESET, src);
590 /* Enable the clock buffer */
591 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
592 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
593 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
597 static void vc5_clk_out_unprepare(struct clk_hw *hw)
599 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
600 struct vc5_driver_data *vc5 = hwdata->vc5;
602 /* Disable the clock buffer */
603 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
604 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
607 static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
609 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
610 struct vc5_driver_data *vc5 = hwdata->vc5;
611 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
612 VC5_OUT_DIV_CONTROL_SEL_EXT |
613 VC5_OUT_DIV_CONTROL_EN_FOD;
614 const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
615 VC5_OUT_DIV_CONTROL_EN_FOD;
616 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
617 VC5_OUT_DIV_CONTROL_SEL_EXT;
620 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
623 if (src == 0) /* Input mux set to DISABLED */
626 if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
632 dev_warn(&vc5->client->dev,
633 "Invalid clock output configuration (%02x)\n", src);
637 static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
639 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
640 struct vc5_driver_data *vc5 = hwdata->vc5;
641 const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
642 VC5_OUT_DIV_CONTROL_SELB_NORM |
643 VC5_OUT_DIV_CONTROL_SEL_EXT |
644 VC5_OUT_DIV_CONTROL_EN_FOD;
645 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
646 VC5_OUT_DIV_CONTROL_SEL_EXT;
647 u8 src = VC5_OUT_DIV_CONTROL_RESET;
650 src |= VC5_OUT_DIV_CONTROL_EN_FOD;
654 return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
658 static const struct clk_ops vc5_clk_out_ops = {
659 .prepare = vc5_clk_out_prepare,
660 .unprepare = vc5_clk_out_unprepare,
661 .set_parent = vc5_clk_out_set_parent,
662 .get_parent = vc5_clk_out_get_parent,
665 static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
668 struct vc5_driver_data *vc5 = data;
669 unsigned int idx = clkspec->args[0];
671 if (idx >= vc5->chip_info->clk_out_cnt)
672 return ERR_PTR(-EINVAL);
674 return &vc5->clk_out[idx].hw;
677 static int vc5_map_index_to_output(const enum vc5_model model,
678 const unsigned int n)
681 case IDT_VC5_5P49V5933:
682 return (n == 0) ? 0 : 3;
683 case IDT_VC5_5P49V5923:
684 case IDT_VC5_5P49V5925:
685 case IDT_VC5_5P49V5935:
686 case IDT_VC6_5P49V6901:
687 case IDT_VC6_5P49V6965:
693 static const struct of_device_id clk_vc5_of_match[];
695 static int vc5_probe(struct i2c_client *client,
696 const struct i2c_device_id *id)
698 struct vc5_driver_data *vc5;
699 struct clk_init_data init;
700 const char *parent_names[2];
701 unsigned int n, idx = 0;
704 vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
708 i2c_set_clientdata(client, vc5);
709 vc5->client = client;
710 vc5->chip_info = of_device_get_match_data(&client->dev);
712 vc5->pin_xin = devm_clk_get(&client->dev, "xin");
713 if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
714 return -EPROBE_DEFER;
716 vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
717 if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
718 return -EPROBE_DEFER;
720 vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
721 if (IS_ERR(vc5->regmap)) {
722 dev_err(&client->dev, "failed to allocate register map\n");
723 return PTR_ERR(vc5->regmap);
726 /* Register clock input mux */
727 memset(&init, 0, sizeof(init));
729 if (!IS_ERR(vc5->pin_xin)) {
730 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
731 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
732 } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
733 vc5->pin_xin = clk_register_fixed_rate(&client->dev,
734 "internal-xtal", NULL,
736 if (IS_ERR(vc5->pin_xin))
737 return PTR_ERR(vc5->pin_xin);
738 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
739 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
742 if (!IS_ERR(vc5->pin_clkin)) {
743 vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
744 parent_names[init.num_parents++] =
745 __clk_get_name(vc5->pin_clkin);
748 if (!init.num_parents) {
749 dev_err(&client->dev, "no input clock specified!\n");
753 init.name = vc5_mux_names[0];
754 init.ops = &vc5_mux_ops;
756 init.parent_names = parent_names;
757 vc5->clk_mux.init = &init;
758 ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
760 dev_err(&client->dev, "unable to register %s\n", init.name);
764 if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
765 /* Register frequency doubler */
766 memset(&init, 0, sizeof(init));
767 init.name = vc5_dbl_names[0];
768 init.ops = &vc5_dbl_ops;
769 init.flags = CLK_SET_RATE_PARENT;
770 init.parent_names = vc5_mux_names;
771 init.num_parents = 1;
772 vc5->clk_mul.init = &init;
773 ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
775 dev_err(&client->dev, "unable to register %s\n",
782 memset(&init, 0, sizeof(init));
783 init.name = vc5_pfd_names[0];
784 init.ops = &vc5_pfd_ops;
785 init.flags = CLK_SET_RATE_PARENT;
786 if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
787 init.parent_names = vc5_dbl_names;
789 init.parent_names = vc5_mux_names;
790 init.num_parents = 1;
791 vc5->clk_pfd.init = &init;
792 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
794 dev_err(&client->dev, "unable to register %s\n", init.name);
799 memset(&init, 0, sizeof(init));
800 init.name = vc5_pll_names[0];
801 init.ops = &vc5_pll_ops;
802 init.flags = CLK_SET_RATE_PARENT;
803 init.parent_names = vc5_pfd_names;
804 init.num_parents = 1;
805 vc5->clk_pll.num = 0;
806 vc5->clk_pll.vc5 = vc5;
807 vc5->clk_pll.hw.init = &init;
808 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
810 dev_err(&client->dev, "unable to register %s\n", init.name);
815 for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
816 idx = vc5_map_index_to_output(vc5->chip_info->model, n);
817 memset(&init, 0, sizeof(init));
818 init.name = vc5_fod_names[idx];
819 init.ops = &vc5_fod_ops;
820 init.flags = CLK_SET_RATE_PARENT;
821 init.parent_names = vc5_pll_names;
822 init.num_parents = 1;
823 vc5->clk_fod[n].num = idx;
824 vc5->clk_fod[n].vc5 = vc5;
825 vc5->clk_fod[n].hw.init = &init;
826 ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
828 dev_err(&client->dev, "unable to register %s\n",
834 /* Register MUX-connected OUT0_I2C_SELB output */
835 memset(&init, 0, sizeof(init));
836 init.name = vc5_clk_out_names[0];
837 init.ops = &vc5_clk_out_ops;
838 init.flags = CLK_SET_RATE_PARENT;
839 init.parent_names = vc5_mux_names;
840 init.num_parents = 1;
841 vc5->clk_out[0].num = idx;
842 vc5->clk_out[0].vc5 = vc5;
843 vc5->clk_out[0].hw.init = &init;
844 ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
846 dev_err(&client->dev, "unable to register %s\n",
851 /* Register FOD-connected OUTx outputs */
852 for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
853 idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
854 parent_names[0] = vc5_fod_names[idx];
856 parent_names[1] = vc5_mux_names[0];
858 parent_names[1] = vc5_clk_out_names[n - 1];
860 memset(&init, 0, sizeof(init));
861 init.name = vc5_clk_out_names[idx + 1];
862 init.ops = &vc5_clk_out_ops;
863 init.flags = CLK_SET_RATE_PARENT;
864 init.parent_names = parent_names;
865 init.num_parents = 2;
866 vc5->clk_out[n].num = idx;
867 vc5->clk_out[n].vc5 = vc5;
868 vc5->clk_out[n].hw.init = &init;
869 ret = devm_clk_hw_register(&client->dev,
870 &vc5->clk_out[n].hw);
872 dev_err(&client->dev, "unable to register %s\n",
878 ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
880 dev_err(&client->dev, "unable to add clk provider\n");
887 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
888 clk_unregister_fixed_rate(vc5->pin_xin);
892 static int vc5_remove(struct i2c_client *client)
894 struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
896 of_clk_del_provider(client->dev.of_node);
898 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
899 clk_unregister_fixed_rate(vc5->pin_xin);
904 static int __maybe_unused vc5_suspend(struct device *dev)
906 struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
908 regcache_cache_only(vc5->regmap, true);
909 regcache_mark_dirty(vc5->regmap);
914 static int __maybe_unused vc5_resume(struct device *dev)
916 struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
919 regcache_cache_only(vc5->regmap, false);
920 ret = regcache_sync(vc5->regmap);
922 dev_err(dev, "Failed to restore register map: %d\n", ret);
926 static const struct vc5_chip_info idt_5p49v5923_info = {
927 .model = IDT_VC5_5P49V5923,
933 static const struct vc5_chip_info idt_5p49v5925_info = {
934 .model = IDT_VC5_5P49V5925,
940 static const struct vc5_chip_info idt_5p49v5933_info = {
941 .model = IDT_VC5_5P49V5933,
944 .flags = VC5_HAS_INTERNAL_XTAL,
947 static const struct vc5_chip_info idt_5p49v5935_info = {
948 .model = IDT_VC5_5P49V5935,
951 .flags = VC5_HAS_INTERNAL_XTAL,
954 static const struct vc5_chip_info idt_5p49v6901_info = {
955 .model = IDT_VC6_5P49V6901,
958 .flags = VC5_HAS_PFD_FREQ_DBL,
961 static const struct vc5_chip_info idt_5p49v6965_info = {
962 .model = IDT_VC6_5P49V6965,
968 static const struct i2c_device_id vc5_id[] = {
969 { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
970 { "5p49v5925", .driver_data = IDT_VC5_5P49V5925 },
971 { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
972 { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
973 { "5p49v6901", .driver_data = IDT_VC6_5P49V6901 },
974 { "5p49v6965", .driver_data = IDT_VC6_5P49V6965 },
977 MODULE_DEVICE_TABLE(i2c, vc5_id);
979 static const struct of_device_id clk_vc5_of_match[] = {
980 { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
981 { .compatible = "idt,5p49v5925", .data = &idt_5p49v5925_info },
982 { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
983 { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
984 { .compatible = "idt,5p49v6901", .data = &idt_5p49v6901_info },
985 { .compatible = "idt,5p49v6965", .data = &idt_5p49v6965_info },
988 MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
990 static SIMPLE_DEV_PM_OPS(vc5_pm_ops, vc5_suspend, vc5_resume);
992 static struct i2c_driver vc5_driver = {
996 .of_match_table = clk_vc5_of_match,
999 .remove = vc5_remove,
1002 module_i2c_driver(vc5_driver);
1005 MODULE_DESCRIPTION("IDT VersaClock 5 driver");
1006 MODULE_LICENSE("GPL");