Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for IDT Versaclock 5
0004  *
0005  * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
0006  */
0007 
0008 /*
0009  * Possible optimizations:
0010  * - Use spread spectrum
0011  * - Use integer divider in FOD if applicable
0012  */
0013 
0014 #include <linux/clk.h>
0015 #include <linux/clk-provider.h>
0016 #include <linux/delay.h>
0017 #include <linux/i2c.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/mod_devicetable.h>
0020 #include <linux/module.h>
0021 #include <linux/of.h>
0022 #include <linux/of_platform.h>
0023 #include <linux/rational.h>
0024 #include <linux/regmap.h>
0025 #include <linux/slab.h>
0026 
0027 #include <dt-bindings/clk/versaclock.h>
0028 
0029 /* VersaClock5 registers */
0030 #define VC5_OTP_CONTROL             0x00
0031 
0032 /* Factory-reserved register block */
0033 #define VC5_RSVD_DEVICE_ID          0x01
0034 #define VC5_RSVD_ADC_GAIN_7_0           0x02
0035 #define VC5_RSVD_ADC_GAIN_15_8          0x03
0036 #define VC5_RSVD_ADC_OFFSET_7_0         0x04
0037 #define VC5_RSVD_ADC_OFFSET_15_8        0x05
0038 #define VC5_RSVD_TEMPY              0x06
0039 #define VC5_RSVD_OFFSET_TBIN            0x07
0040 #define VC5_RSVD_GAIN               0x08
0041 #define VC5_RSVD_TEST_NP            0x09
0042 #define VC5_RSVD_UNUSED             0x0a
0043 #define VC5_RSVD_BANDGAP_TRIM_UP        0x0b
0044 #define VC5_RSVD_BANDGAP_TRIM_DN        0x0c
0045 #define VC5_RSVD_CLK_R_12_CLK_AMP_4     0x0d
0046 #define VC5_RSVD_CLK_R_34_CLK_AMP_4     0x0e
0047 #define VC5_RSVD_CLK_AMP_123            0x0f
0048 
0049 /* Configuration register block */
0050 #define VC5_PRIM_SRC_SHDN           0x10
0051 #define VC5_PRIM_SRC_SHDN_EN_XTAL       BIT(7)
0052 #define VC5_PRIM_SRC_SHDN_EN_CLKIN      BIT(6)
0053 #define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ   BIT(3)
0054 #define VC5_PRIM_SRC_SHDN_SP            BIT(1)
0055 #define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN       BIT(0)
0056 
0057 #define VC5_VCO_BAND                0x11
0058 #define VC5_XTAL_X1_LOAD_CAP            0x12
0059 #define VC5_XTAL_X2_LOAD_CAP            0x13
0060 #define VC5_REF_DIVIDER             0x15
0061 #define VC5_REF_DIVIDER_SEL_PREDIV2     BIT(7)
0062 #define VC5_REF_DIVIDER_REF_DIV(n)      ((n) & 0x3f)
0063 
0064 #define VC5_VCO_CTRL_AND_PREDIV         0x16
0065 #define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV   BIT(7)
0066 
0067 #define VC5_FEEDBACK_INT_DIV            0x17
0068 #define VC5_FEEDBACK_INT_DIV_BITS       0x18
0069 #define VC5_FEEDBACK_FRAC_DIV(n)        (0x19 + (n))
0070 #define VC5_RC_CONTROL0             0x1e
0071 #define VC5_RC_CONTROL1             0x1f
0072 
0073 /* These registers are named "Unused Factory Reserved Registers" */
0074 #define VC5_RESERVED_X0(idx)        (0x20 + ((idx) * 0x10))
0075 #define VC5_RESERVED_X0_BYPASS_SYNC BIT(7) /* bypass_sync<idx> bit */
0076 
0077 /* Output divider control for divider 1,2,3,4 */
0078 #define VC5_OUT_DIV_CONTROL(idx)    (0x21 + ((idx) * 0x10))
0079 #define VC5_OUT_DIV_CONTROL_RESET   BIT(7)
0080 #define VC5_OUT_DIV_CONTROL_SELB_NORM   BIT(3)
0081 #define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
0082 #define VC5_OUT_DIV_CONTROL_INT_MODE    BIT(1)
0083 #define VC5_OUT_DIV_CONTROL_EN_FOD  BIT(0)
0084 
0085 #define VC5_OUT_DIV_FRAC(idx, n)    (0x22 + ((idx) * 0x10) + (n))
0086 #define VC5_OUT_DIV_FRAC4_OD_SCEE   BIT(1)
0087 
0088 #define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
0089 #define VC5_OUT_DIV_SPREAD_MOD(idx, n)  (0x29 + ((idx) * 0x10) + (n))
0090 #define VC5_OUT_DIV_SKEW_INT(idx, n)    (0x2b + ((idx) * 0x10) + (n))
0091 #define VC5_OUT_DIV_INT(idx, n)     (0x2d + ((idx) * 0x10) + (n))
0092 #define VC5_OUT_DIV_SKEW_FRAC(idx)  (0x2f + ((idx) * 0x10))
0093 
0094 /* Clock control register for clock 1,2 */
0095 #define VC5_CLK_OUTPUT_CFG(idx, n)  (0x60 + ((idx) * 0x2) + (n))
0096 #define VC5_CLK_OUTPUT_CFG0_CFG_SHIFT   5
0097 #define VC5_CLK_OUTPUT_CFG0_CFG_MASK GENMASK(7, VC5_CLK_OUTPUT_CFG0_CFG_SHIFT)
0098 
0099 #define VC5_CLK_OUTPUT_CFG0_CFG_LVPECL  (VC5_LVPECL)
0100 #define VC5_CLK_OUTPUT_CFG0_CFG_CMOS        (VC5_CMOS)
0101 #define VC5_CLK_OUTPUT_CFG0_CFG_HCSL33  (VC5_HCSL33)
0102 #define VC5_CLK_OUTPUT_CFG0_CFG_LVDS        (VC5_LVDS)
0103 #define VC5_CLK_OUTPUT_CFG0_CFG_CMOS2       (VC5_CMOS2)
0104 #define VC5_CLK_OUTPUT_CFG0_CFG_CMOSD       (VC5_CMOSD)
0105 #define VC5_CLK_OUTPUT_CFG0_CFG_HCSL25  (VC5_HCSL25)
0106 
0107 #define VC5_CLK_OUTPUT_CFG0_PWR_SHIFT   3
0108 #define VC5_CLK_OUTPUT_CFG0_PWR_MASK GENMASK(4, VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
0109 #define VC5_CLK_OUTPUT_CFG0_PWR_18  (0<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
0110 #define VC5_CLK_OUTPUT_CFG0_PWR_25  (2<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
0111 #define VC5_CLK_OUTPUT_CFG0_PWR_33  (3<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
0112 #define VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT  0
0113 #define VC5_CLK_OUTPUT_CFG0_SLEW_MASK GENMASK(1, VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
0114 #define VC5_CLK_OUTPUT_CFG0_SLEW_80 (0<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
0115 #define VC5_CLK_OUTPUT_CFG0_SLEW_85 (1<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
0116 #define VC5_CLK_OUTPUT_CFG0_SLEW_90 (2<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
0117 #define VC5_CLK_OUTPUT_CFG0_SLEW_100    (3<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
0118 #define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF   BIT(0)
0119 
0120 #define VC5_CLK_OE_SHDN             0x68
0121 #define VC5_CLK_OS_SHDN             0x69
0122 
0123 #define VC5_GLOBAL_REGISTER         0x76
0124 #define VC5_GLOBAL_REGISTER_GLOBAL_RESET    BIT(5)
0125 
0126 /* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
0127 #define VC5_PLL_VCO_MIN             2500000000UL
0128 #define VC5_PLL_VCO_MAX             3000000000UL
0129 
0130 /* VC5 Input mux settings */
0131 #define VC5_MUX_IN_XIN      BIT(0)
0132 #define VC5_MUX_IN_CLKIN    BIT(1)
0133 
0134 /* Maximum number of clk_out supported by this driver */
0135 #define VC5_MAX_CLK_OUT_NUM 5
0136 
0137 /* Maximum number of FODs supported by this driver */
0138 #define VC5_MAX_FOD_NUM 4
0139 
0140 /* flags to describe chip features */
0141 /* chip has built-in oscilator */
0142 #define VC5_HAS_INTERNAL_XTAL   BIT(0)
0143 /* chip has PFD requency doubler */
0144 #define VC5_HAS_PFD_FREQ_DBL    BIT(1)
0145 /* chip has bits to disable FOD sync */
0146 #define VC5_HAS_BYPASS_SYNC_BIT BIT(2)
0147 
0148 /* Supported IDT VC5 models. */
0149 enum vc5_model {
0150     IDT_VC5_5P49V5923,
0151     IDT_VC5_5P49V5925,
0152     IDT_VC5_5P49V5933,
0153     IDT_VC5_5P49V5935,
0154     IDT_VC6_5P49V6901,
0155     IDT_VC6_5P49V6965,
0156 };
0157 
0158 /* Structure to describe features of a particular VC5 model */
0159 struct vc5_chip_info {
0160     const enum vc5_model    model;
0161     const unsigned int  clk_fod_cnt;
0162     const unsigned int  clk_out_cnt;
0163     const u32       flags;
0164 };
0165 
0166 struct vc5_driver_data;
0167 
0168 struct vc5_hw_data {
0169     struct clk_hw       hw;
0170     struct vc5_driver_data  *vc5;
0171     u32         div_int;
0172     u32         div_frc;
0173     unsigned int        num;
0174 };
0175 
0176 struct vc5_out_data {
0177     struct clk_hw       hw;
0178     struct vc5_driver_data  *vc5;
0179     unsigned int        num;
0180     unsigned int        clk_output_cfg0;
0181     unsigned int        clk_output_cfg0_mask;
0182 };
0183 
0184 struct vc5_driver_data {
0185     struct i2c_client   *client;
0186     struct regmap       *regmap;
0187     const struct vc5_chip_info  *chip_info;
0188 
0189     struct clk      *pin_xin;
0190     struct clk      *pin_clkin;
0191     unsigned char       clk_mux_ins;
0192     struct clk_hw       clk_mux;
0193     struct clk_hw       clk_mul;
0194     struct clk_hw       clk_pfd;
0195     struct vc5_hw_data  clk_pll;
0196     struct vc5_hw_data  clk_fod[VC5_MAX_FOD_NUM];
0197     struct vc5_out_data clk_out[VC5_MAX_CLK_OUT_NUM];
0198 };
0199 
0200 /*
0201  * VersaClock5 i2c regmap
0202  */
0203 static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
0204 {
0205     /* Factory reserved regs, make them read-only */
0206     if (reg <= 0xf)
0207         return false;
0208 
0209     /* Factory reserved regs, make them read-only */
0210     if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
0211         return false;
0212 
0213     return true;
0214 }
0215 
0216 static const struct regmap_config vc5_regmap_config = {
0217     .reg_bits = 8,
0218     .val_bits = 8,
0219     .cache_type = REGCACHE_RBTREE,
0220     .max_register = 0x76,
0221     .writeable_reg = vc5_regmap_is_writeable,
0222 };
0223 
0224 /*
0225  * VersaClock5 input multiplexer between XTAL and CLKIN divider
0226  */
0227 static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
0228 {
0229     struct vc5_driver_data *vc5 =
0230         container_of(hw, struct vc5_driver_data, clk_mux);
0231     const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
0232     unsigned int src;
0233 
0234     regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
0235     src &= mask;
0236 
0237     if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
0238         return 0;
0239 
0240     if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
0241         return 1;
0242 
0243     dev_warn(&vc5->client->dev,
0244          "Invalid clock input configuration (%02x)\n", src);
0245     return 0;
0246 }
0247 
0248 static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
0249 {
0250     struct vc5_driver_data *vc5 =
0251         container_of(hw, struct vc5_driver_data, clk_mux);
0252     const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
0253     u8 src;
0254 
0255     if ((index > 1) || !vc5->clk_mux_ins)
0256         return -EINVAL;
0257 
0258     if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
0259         if (index == 0)
0260             src = VC5_PRIM_SRC_SHDN_EN_XTAL;
0261         if (index == 1)
0262             src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
0263     } else {
0264         if (index != 0)
0265             return -EINVAL;
0266 
0267         if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
0268             src = VC5_PRIM_SRC_SHDN_EN_XTAL;
0269         else if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
0270             src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
0271         else /* Invalid; should have been caught by vc5_probe() */
0272             return -EINVAL;
0273     }
0274 
0275     return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
0276 }
0277 
0278 static const struct clk_ops vc5_mux_ops = {
0279     .set_parent = vc5_mux_set_parent,
0280     .get_parent = vc5_mux_get_parent,
0281 };
0282 
0283 static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
0284                      unsigned long parent_rate)
0285 {
0286     struct vc5_driver_data *vc5 =
0287         container_of(hw, struct vc5_driver_data, clk_mul);
0288     unsigned int premul;
0289 
0290     regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
0291     if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
0292         parent_rate *= 2;
0293 
0294     return parent_rate;
0295 }
0296 
0297 static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
0298                    unsigned long *parent_rate)
0299 {
0300     if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
0301         return rate;
0302     else
0303         return -EINVAL;
0304 }
0305 
0306 static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
0307                 unsigned long parent_rate)
0308 {
0309     struct vc5_driver_data *vc5 =
0310         container_of(hw, struct vc5_driver_data, clk_mul);
0311     u32 mask;
0312 
0313     if ((parent_rate * 2) == rate)
0314         mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
0315     else
0316         mask = 0;
0317 
0318     regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
0319                VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
0320                mask);
0321 
0322     return 0;
0323 }
0324 
0325 static const struct clk_ops vc5_dbl_ops = {
0326     .recalc_rate    = vc5_dbl_recalc_rate,
0327     .round_rate = vc5_dbl_round_rate,
0328     .set_rate   = vc5_dbl_set_rate,
0329 };
0330 
0331 static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
0332                      unsigned long parent_rate)
0333 {
0334     struct vc5_driver_data *vc5 =
0335         container_of(hw, struct vc5_driver_data, clk_pfd);
0336     unsigned int prediv, div;
0337 
0338     regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
0339 
0340     /* The bypass_prediv is set, PLL fed from Ref_in directly. */
0341     if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
0342         return parent_rate;
0343 
0344     regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
0345 
0346     /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
0347     if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
0348         return parent_rate / 2;
0349     else
0350         return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
0351 }
0352 
0353 static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
0354                    unsigned long *parent_rate)
0355 {
0356     unsigned long idiv;
0357 
0358     /* PLL cannot operate with input clock above 50 MHz. */
0359     if (rate > 50000000)
0360         return -EINVAL;
0361 
0362     /* CLKIN within range of PLL input, feed directly to PLL. */
0363     if (*parent_rate <= 50000000)
0364         return *parent_rate;
0365 
0366     idiv = DIV_ROUND_UP(*parent_rate, rate);
0367     if (idiv > 127)
0368         return -EINVAL;
0369 
0370     return *parent_rate / idiv;
0371 }
0372 
0373 static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
0374                 unsigned long parent_rate)
0375 {
0376     struct vc5_driver_data *vc5 =
0377         container_of(hw, struct vc5_driver_data, clk_pfd);
0378     unsigned long idiv;
0379     u8 div;
0380 
0381     /* CLKIN within range of PLL input, feed directly to PLL. */
0382     if (parent_rate <= 50000000) {
0383         regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
0384                    VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
0385                    VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
0386         regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
0387         return 0;
0388     }
0389 
0390     idiv = DIV_ROUND_UP(parent_rate, rate);
0391 
0392     /* We have dedicated div-2 predivider. */
0393     if (idiv == 2)
0394         div = VC5_REF_DIVIDER_SEL_PREDIV2;
0395     else
0396         div = VC5_REF_DIVIDER_REF_DIV(idiv);
0397 
0398     regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
0399     regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
0400                VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
0401 
0402     return 0;
0403 }
0404 
0405 static const struct clk_ops vc5_pfd_ops = {
0406     .recalc_rate    = vc5_pfd_recalc_rate,
0407     .round_rate = vc5_pfd_round_rate,
0408     .set_rate   = vc5_pfd_set_rate,
0409 };
0410 
0411 /*
0412  * VersaClock5 PLL/VCO
0413  */
0414 static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
0415                      unsigned long parent_rate)
0416 {
0417     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0418     struct vc5_driver_data *vc5 = hwdata->vc5;
0419     u32 div_int, div_frc;
0420     u8 fb[5];
0421 
0422     regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
0423 
0424     div_int = (fb[0] << 4) | (fb[1] >> 4);
0425     div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
0426 
0427     /* The PLL divider has 12 integer bits and 24 fractional bits */
0428     return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
0429 }
0430 
0431 static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
0432                    unsigned long *parent_rate)
0433 {
0434     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0435     u32 div_int;
0436     u64 div_frc;
0437 
0438     if (rate < VC5_PLL_VCO_MIN)
0439         rate = VC5_PLL_VCO_MIN;
0440     if (rate > VC5_PLL_VCO_MAX)
0441         rate = VC5_PLL_VCO_MAX;
0442 
0443     /* Determine integer part, which is 12 bit wide */
0444     div_int = rate / *parent_rate;
0445     if (div_int > 0xfff)
0446         rate = *parent_rate * 0xfff;
0447 
0448     /* Determine best fractional part, which is 24 bit wide */
0449     div_frc = rate % *parent_rate;
0450     div_frc *= BIT(24) - 1;
0451     do_div(div_frc, *parent_rate);
0452 
0453     hwdata->div_int = div_int;
0454     hwdata->div_frc = (u32)div_frc;
0455 
0456     return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
0457 }
0458 
0459 static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
0460                 unsigned long parent_rate)
0461 {
0462     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0463     struct vc5_driver_data *vc5 = hwdata->vc5;
0464     u8 fb[5];
0465 
0466     fb[0] = hwdata->div_int >> 4;
0467     fb[1] = hwdata->div_int << 4;
0468     fb[2] = hwdata->div_frc >> 16;
0469     fb[3] = hwdata->div_frc >> 8;
0470     fb[4] = hwdata->div_frc;
0471 
0472     return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
0473 }
0474 
0475 static const struct clk_ops vc5_pll_ops = {
0476     .recalc_rate    = vc5_pll_recalc_rate,
0477     .round_rate = vc5_pll_round_rate,
0478     .set_rate   = vc5_pll_set_rate,
0479 };
0480 
0481 static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
0482                      unsigned long parent_rate)
0483 {
0484     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0485     struct vc5_driver_data *vc5 = hwdata->vc5;
0486     /* VCO frequency is divided by two before entering FOD */
0487     u32 f_in = parent_rate / 2;
0488     u32 div_int, div_frc;
0489     u8 od_int[2];
0490     u8 od_frc[4];
0491 
0492     regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
0493              od_int, 2);
0494     regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
0495              od_frc, 4);
0496 
0497     div_int = (od_int[0] << 4) | (od_int[1] >> 4);
0498     div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
0499           (od_frc[2] << 6) | (od_frc[3] >> 2);
0500 
0501     /* Avoid division by zero if the output is not configured. */
0502     if (div_int == 0 && div_frc == 0)
0503         return 0;
0504 
0505     /* The PLL divider has 12 integer bits and 30 fractional bits */
0506     return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
0507 }
0508 
0509 static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
0510                    unsigned long *parent_rate)
0511 {
0512     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0513     /* VCO frequency is divided by two before entering FOD */
0514     u32 f_in = *parent_rate / 2;
0515     u32 div_int;
0516     u64 div_frc;
0517 
0518     /* Determine integer part, which is 12 bit wide */
0519     div_int = f_in / rate;
0520     /*
0521      * WARNING: The clock chip does not output signal if the integer part
0522      *          of the divider is 0xfff and fractional part is non-zero.
0523      *          Clamp the divider at 0xffe to keep the code simple.
0524      */
0525     if (div_int > 0xffe) {
0526         div_int = 0xffe;
0527         rate = f_in / div_int;
0528     }
0529 
0530     /* Determine best fractional part, which is 30 bit wide */
0531     div_frc = f_in % rate;
0532     div_frc <<= 24;
0533     do_div(div_frc, rate);
0534 
0535     hwdata->div_int = div_int;
0536     hwdata->div_frc = (u32)div_frc;
0537 
0538     return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
0539 }
0540 
0541 static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
0542                 unsigned long parent_rate)
0543 {
0544     struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
0545     struct vc5_driver_data *vc5 = hwdata->vc5;
0546     u8 data[14] = {
0547         hwdata->div_frc >> 22, hwdata->div_frc >> 14,
0548         hwdata->div_frc >> 6, hwdata->div_frc << 2,
0549         0, 0, 0, 0, 0,
0550         0, 0,
0551         hwdata->div_int >> 4, hwdata->div_int << 4,
0552         0
0553     };
0554 
0555     regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
0556               data, 14);
0557 
0558     /*
0559      * Toggle magic bit in undocumented register for unknown reason.
0560      * This is what the IDT timing commander tool does and the chip
0561      * datasheet somewhat implies this is needed, but the register
0562      * and the bit is not documented.
0563      */
0564     regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
0565                VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
0566     regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
0567                VC5_GLOBAL_REGISTER_GLOBAL_RESET,
0568                VC5_GLOBAL_REGISTER_GLOBAL_RESET);
0569     return 0;
0570 }
0571 
0572 static const struct clk_ops vc5_fod_ops = {
0573     .recalc_rate    = vc5_fod_recalc_rate,
0574     .round_rate = vc5_fod_round_rate,
0575     .set_rate   = vc5_fod_set_rate,
0576 };
0577 
0578 static int vc5_clk_out_prepare(struct clk_hw *hw)
0579 {
0580     struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
0581     struct vc5_driver_data *vc5 = hwdata->vc5;
0582     const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
0583             VC5_OUT_DIV_CONTROL_SEL_EXT |
0584             VC5_OUT_DIV_CONTROL_EN_FOD;
0585     unsigned int src;
0586     int ret;
0587 
0588     /*
0589      * When enabling a FOD, all currently enabled FODs are briefly
0590      * stopped in order to synchronize all of them. This causes a clock
0591      * disruption to any unrelated chips that might be already using
0592      * other clock outputs. Bypass the sync feature to avoid the issue,
0593      * which is possible on the VersaClock 6E family via reserved
0594      * registers.
0595      */
0596     if (vc5->chip_info->flags & VC5_HAS_BYPASS_SYNC_BIT) {
0597         ret = regmap_update_bits(vc5->regmap,
0598                      VC5_RESERVED_X0(hwdata->num),
0599                      VC5_RESERVED_X0_BYPASS_SYNC,
0600                      VC5_RESERVED_X0_BYPASS_SYNC);
0601         if (ret)
0602             return ret;
0603     }
0604 
0605     /*
0606      * If the input mux is disabled, enable it first and
0607      * select source from matching FOD.
0608      */
0609     regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
0610     if ((src & mask) == 0) {
0611         src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
0612         ret = regmap_update_bits(vc5->regmap,
0613                      VC5_OUT_DIV_CONTROL(hwdata->num),
0614                      mask | VC5_OUT_DIV_CONTROL_RESET, src);
0615         if (ret)
0616             return ret;
0617     }
0618 
0619     /* Enable the clock buffer */
0620     regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
0621                VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
0622                VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
0623     if (hwdata->clk_output_cfg0_mask) {
0624         dev_dbg(&vc5->client->dev, "Update output %d mask 0x%0X val 0x%0X\n",
0625             hwdata->num, hwdata->clk_output_cfg0_mask,
0626             hwdata->clk_output_cfg0);
0627 
0628         regmap_update_bits(vc5->regmap,
0629             VC5_CLK_OUTPUT_CFG(hwdata->num, 0),
0630             hwdata->clk_output_cfg0_mask,
0631             hwdata->clk_output_cfg0);
0632     }
0633 
0634     return 0;
0635 }
0636 
0637 static void vc5_clk_out_unprepare(struct clk_hw *hw)
0638 {
0639     struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
0640     struct vc5_driver_data *vc5 = hwdata->vc5;
0641 
0642     /* Disable the clock buffer */
0643     regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
0644                VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
0645 }
0646 
0647 static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
0648 {
0649     struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
0650     struct vc5_driver_data *vc5 = hwdata->vc5;
0651     const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
0652             VC5_OUT_DIV_CONTROL_SEL_EXT |
0653             VC5_OUT_DIV_CONTROL_EN_FOD;
0654     const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
0655                   VC5_OUT_DIV_CONTROL_EN_FOD;
0656     const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
0657               VC5_OUT_DIV_CONTROL_SEL_EXT;
0658     unsigned int src;
0659 
0660     regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
0661     src &= mask;
0662 
0663     if (src == 0)   /* Input mux set to DISABLED */
0664         return 0;
0665 
0666     if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
0667         return 0;
0668 
0669     if (src == extclk)
0670         return 1;
0671 
0672     dev_warn(&vc5->client->dev,
0673          "Invalid clock output configuration (%02x)\n", src);
0674     return 0;
0675 }
0676 
0677 static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
0678 {
0679     struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
0680     struct vc5_driver_data *vc5 = hwdata->vc5;
0681     const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
0682             VC5_OUT_DIV_CONTROL_SELB_NORM |
0683             VC5_OUT_DIV_CONTROL_SEL_EXT |
0684             VC5_OUT_DIV_CONTROL_EN_FOD;
0685     const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
0686               VC5_OUT_DIV_CONTROL_SEL_EXT;
0687     u8 src = VC5_OUT_DIV_CONTROL_RESET;
0688 
0689     if (index == 0)
0690         src |= VC5_OUT_DIV_CONTROL_EN_FOD;
0691     else
0692         src |= extclk;
0693 
0694     return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
0695                   mask, src);
0696 }
0697 
0698 static const struct clk_ops vc5_clk_out_ops = {
0699     .prepare    = vc5_clk_out_prepare,
0700     .unprepare  = vc5_clk_out_unprepare,
0701     .set_parent = vc5_clk_out_set_parent,
0702     .get_parent = vc5_clk_out_get_parent,
0703 };
0704 
0705 static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
0706                      void *data)
0707 {
0708     struct vc5_driver_data *vc5 = data;
0709     unsigned int idx = clkspec->args[0];
0710 
0711     if (idx >= vc5->chip_info->clk_out_cnt)
0712         return ERR_PTR(-EINVAL);
0713 
0714     return &vc5->clk_out[idx].hw;
0715 }
0716 
0717 static int vc5_map_index_to_output(const enum vc5_model model,
0718                    const unsigned int n)
0719 {
0720     switch (model) {
0721     case IDT_VC5_5P49V5933:
0722         return (n == 0) ? 0 : 3;
0723     case IDT_VC5_5P49V5923:
0724     case IDT_VC5_5P49V5925:
0725     case IDT_VC5_5P49V5935:
0726     case IDT_VC6_5P49V6901:
0727     case IDT_VC6_5P49V6965:
0728     default:
0729         return n;
0730     }
0731 }
0732 
0733 static int vc5_update_mode(struct device_node *np_output,
0734                struct vc5_out_data *clk_out)
0735 {
0736     u32 value;
0737 
0738     if (!of_property_read_u32(np_output, "idt,mode", &value)) {
0739         clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_CFG_MASK;
0740         switch (value) {
0741         case VC5_CLK_OUTPUT_CFG0_CFG_LVPECL:
0742         case VC5_CLK_OUTPUT_CFG0_CFG_CMOS:
0743         case VC5_CLK_OUTPUT_CFG0_CFG_HCSL33:
0744         case VC5_CLK_OUTPUT_CFG0_CFG_LVDS:
0745         case VC5_CLK_OUTPUT_CFG0_CFG_CMOS2:
0746         case VC5_CLK_OUTPUT_CFG0_CFG_CMOSD:
0747         case VC5_CLK_OUTPUT_CFG0_CFG_HCSL25:
0748             clk_out->clk_output_cfg0 |=
0749                 value << VC5_CLK_OUTPUT_CFG0_CFG_SHIFT;
0750             break;
0751         default:
0752             return -EINVAL;
0753         }
0754     }
0755     return 0;
0756 }
0757 
0758 static int vc5_update_power(struct device_node *np_output,
0759                 struct vc5_out_data *clk_out)
0760 {
0761     u32 value;
0762 
0763     if (!of_property_read_u32(np_output, "idt,voltage-microvolt",
0764                   &value)) {
0765         clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_PWR_MASK;
0766         switch (value) {
0767         case 1800000:
0768             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_18;
0769             break;
0770         case 2500000:
0771             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_25;
0772             break;
0773         case 3300000:
0774             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_33;
0775             break;
0776         default:
0777             return -EINVAL;
0778         }
0779     }
0780     return 0;
0781 }
0782 
0783 static int vc5_map_cap_value(u32 femtofarads)
0784 {
0785     int mapped_value;
0786 
0787     /*
0788      * The datasheet explicitly states 9000 - 25000 with 0.5pF
0789      * steps, but the Programmer's guide shows the steps are 0.430pF.
0790      * After getting feedback from Renesas, the .5pF steps were the
0791      * goal, but 430nF was the actual values.
0792      * Because of this, the actual range goes to 22760 instead of 25000
0793      */
0794     if (femtofarads < 9000 || femtofarads > 22760)
0795         return -EINVAL;
0796 
0797     /*
0798      * The Programmer's guide shows XTAL[5:0] but in reality,
0799      * XTAL[0] and XTAL[1] are both LSB which makes the math
0800      * strange.  With clarfication from Renesas, setting the
0801      * values should be simpler by ignoring XTAL[0]
0802      */
0803     mapped_value = DIV_ROUND_CLOSEST(femtofarads - 9000, 430);
0804 
0805     /*
0806      * Since the calculation ignores XTAL[0], there is one
0807      * special case where mapped_value = 32.  In reality, this means
0808      * the real mapped value should be 111111b.  In other cases,
0809      * the mapped_value needs to be shifted 1 to the left.
0810      */
0811     if (mapped_value > 31)
0812         mapped_value = 0x3f;
0813     else
0814         mapped_value <<= 1;
0815 
0816     return mapped_value;
0817 }
0818 static int vc5_update_cap_load(struct device_node *node, struct vc5_driver_data *vc5)
0819 {
0820     u32 value;
0821     int mapped_value;
0822 
0823     if (!of_property_read_u32(node, "idt,xtal-load-femtofarads", &value)) {
0824         mapped_value = vc5_map_cap_value(value);
0825         if (mapped_value < 0)
0826             return mapped_value;
0827 
0828         /*
0829          * The mapped_value is really the high 6 bits of
0830          * VC5_XTAL_X1_LOAD_CAP and VC5_XTAL_X2_LOAD_CAP, so
0831          * shift the value 2 places.
0832          */
0833         regmap_update_bits(vc5->regmap, VC5_XTAL_X1_LOAD_CAP, ~0x03, mapped_value << 2);
0834         regmap_update_bits(vc5->regmap, VC5_XTAL_X2_LOAD_CAP, ~0x03, mapped_value << 2);
0835     }
0836 
0837     return 0;
0838 }
0839 
0840 static int vc5_update_slew(struct device_node *np_output,
0841                struct vc5_out_data *clk_out)
0842 {
0843     u32 value;
0844 
0845     if (!of_property_read_u32(np_output, "idt,slew-percent", &value)) {
0846         clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_SLEW_MASK;
0847         switch (value) {
0848         case 80:
0849             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_80;
0850             break;
0851         case 85:
0852             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_85;
0853             break;
0854         case 90:
0855             clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_90;
0856             break;
0857         case 100:
0858             clk_out->clk_output_cfg0 |=
0859                 VC5_CLK_OUTPUT_CFG0_SLEW_100;
0860             break;
0861         default:
0862             return -EINVAL;
0863         }
0864     }
0865     return 0;
0866 }
0867 
0868 static int vc5_get_output_config(struct i2c_client *client,
0869                  struct vc5_out_data *clk_out)
0870 {
0871     struct device_node *np_output;
0872     char *child_name;
0873     int ret = 0;
0874 
0875     child_name = kasprintf(GFP_KERNEL, "OUT%d", clk_out->num + 1);
0876     if (!child_name)
0877         return -ENOMEM;
0878 
0879     np_output = of_get_child_by_name(client->dev.of_node, child_name);
0880     kfree(child_name);
0881     if (!np_output)
0882         return 0;
0883 
0884     ret = vc5_update_mode(np_output, clk_out);
0885     if (ret)
0886         goto output_error;
0887 
0888     ret = vc5_update_power(np_output, clk_out);
0889     if (ret)
0890         goto output_error;
0891 
0892     ret = vc5_update_slew(np_output, clk_out);
0893 
0894 output_error:
0895     if (ret) {
0896         dev_err(&client->dev,
0897             "Invalid clock output configuration OUT%d\n",
0898             clk_out->num + 1);
0899     }
0900 
0901     of_node_put(np_output);
0902 
0903     return ret;
0904 }
0905 
0906 static const struct of_device_id clk_vc5_of_match[];
0907 
0908 static int vc5_probe(struct i2c_client *client)
0909 {
0910     unsigned int oe, sd, src_mask = 0, src_val = 0;
0911     struct vc5_driver_data *vc5;
0912     struct clk_init_data init;
0913     const char *parent_names[2];
0914     unsigned int n, idx = 0;
0915     int ret;
0916 
0917     vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
0918     if (!vc5)
0919         return -ENOMEM;
0920 
0921     i2c_set_clientdata(client, vc5);
0922     vc5->client = client;
0923     vc5->chip_info = of_device_get_match_data(&client->dev);
0924 
0925     vc5->pin_xin = devm_clk_get(&client->dev, "xin");
0926     if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
0927         return -EPROBE_DEFER;
0928 
0929     vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
0930     if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
0931         return -EPROBE_DEFER;
0932 
0933     vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
0934     if (IS_ERR(vc5->regmap))
0935         return dev_err_probe(&client->dev, PTR_ERR(vc5->regmap),
0936                      "failed to allocate register map\n");
0937 
0938     ret = of_property_read_u32(client->dev.of_node, "idt,shutdown", &sd);
0939     if (!ret) {
0940         src_mask |= VC5_PRIM_SRC_SHDN_EN_GBL_SHDN;
0941         if (sd)
0942             src_val |= VC5_PRIM_SRC_SHDN_EN_GBL_SHDN;
0943     } else if (ret != -EINVAL) {
0944         return dev_err_probe(&client->dev, ret,
0945                      "could not read idt,shutdown\n");
0946     }
0947 
0948     ret = of_property_read_u32(client->dev.of_node,
0949                    "idt,output-enable-active", &oe);
0950     if (!ret) {
0951         src_mask |= VC5_PRIM_SRC_SHDN_SP;
0952         if (oe)
0953             src_val |= VC5_PRIM_SRC_SHDN_SP;
0954     } else if (ret != -EINVAL) {
0955         return dev_err_probe(&client->dev, ret,
0956                      "could not read idt,output-enable-active\n");
0957     }
0958 
0959     regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, src_mask, src_val);
0960 
0961     /* Register clock input mux */
0962     memset(&init, 0, sizeof(init));
0963 
0964     if (!IS_ERR(vc5->pin_xin)) {
0965         vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
0966         parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
0967     } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
0968         vc5->pin_xin = clk_register_fixed_rate(&client->dev,
0969                                "internal-xtal", NULL,
0970                                0, 25000000);
0971         if (IS_ERR(vc5->pin_xin))
0972             return PTR_ERR(vc5->pin_xin);
0973         vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
0974         parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
0975     }
0976 
0977     if (!IS_ERR(vc5->pin_clkin)) {
0978         vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
0979         parent_names[init.num_parents++] =
0980             __clk_get_name(vc5->pin_clkin);
0981     }
0982 
0983     if (!init.num_parents)
0984         return dev_err_probe(&client->dev, -EINVAL,
0985                      "no input clock specified!\n");
0986 
0987     /* Configure Optional Loading Capacitance for external XTAL */
0988     if (!(vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)) {
0989         ret = vc5_update_cap_load(client->dev.of_node, vc5);
0990         if (ret)
0991             goto err_clk_register;
0992     }
0993 
0994     init.name = kasprintf(GFP_KERNEL, "%pOFn.mux", client->dev.of_node);
0995     init.ops = &vc5_mux_ops;
0996     init.flags = 0;
0997     init.parent_names = parent_names;
0998     vc5->clk_mux.init = &init;
0999     ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
1000     if (ret)
1001         goto err_clk_register;
1002     kfree(init.name);   /* clock framework made a copy of the name */
1003 
1004     if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
1005         /* Register frequency doubler */
1006         memset(&init, 0, sizeof(init));
1007         init.name = kasprintf(GFP_KERNEL, "%pOFn.dbl",
1008                       client->dev.of_node);
1009         init.ops = &vc5_dbl_ops;
1010         init.flags = CLK_SET_RATE_PARENT;
1011         init.parent_names = parent_names;
1012         parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
1013         init.num_parents = 1;
1014         vc5->clk_mul.init = &init;
1015         ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
1016         if (ret)
1017             goto err_clk_register;
1018         kfree(init.name); /* clock framework made a copy of the name */
1019     }
1020 
1021     /* Register PFD */
1022     memset(&init, 0, sizeof(init));
1023     init.name = kasprintf(GFP_KERNEL, "%pOFn.pfd", client->dev.of_node);
1024     init.ops = &vc5_pfd_ops;
1025     init.flags = CLK_SET_RATE_PARENT;
1026     init.parent_names = parent_names;
1027     if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
1028         parent_names[0] = clk_hw_get_name(&vc5->clk_mul);
1029     else
1030         parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
1031     init.num_parents = 1;
1032     vc5->clk_pfd.init = &init;
1033     ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
1034     if (ret)
1035         goto err_clk_register;
1036     kfree(init.name);   /* clock framework made a copy of the name */
1037 
1038     /* Register PLL */
1039     memset(&init, 0, sizeof(init));
1040     init.name = kasprintf(GFP_KERNEL, "%pOFn.pll", client->dev.of_node);
1041     init.ops = &vc5_pll_ops;
1042     init.flags = CLK_SET_RATE_PARENT;
1043     init.parent_names = parent_names;
1044     parent_names[0] = clk_hw_get_name(&vc5->clk_pfd);
1045     init.num_parents = 1;
1046     vc5->clk_pll.num = 0;
1047     vc5->clk_pll.vc5 = vc5;
1048     vc5->clk_pll.hw.init = &init;
1049     ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
1050     if (ret)
1051         goto err_clk_register;
1052     kfree(init.name); /* clock framework made a copy of the name */
1053 
1054     /* Register FODs */
1055     for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
1056         idx = vc5_map_index_to_output(vc5->chip_info->model, n);
1057         memset(&init, 0, sizeof(init));
1058         init.name = kasprintf(GFP_KERNEL, "%pOFn.fod%d",
1059                       client->dev.of_node, idx);
1060         init.ops = &vc5_fod_ops;
1061         init.flags = CLK_SET_RATE_PARENT;
1062         init.parent_names = parent_names;
1063         parent_names[0] = clk_hw_get_name(&vc5->clk_pll.hw);
1064         init.num_parents = 1;
1065         vc5->clk_fod[n].num = idx;
1066         vc5->clk_fod[n].vc5 = vc5;
1067         vc5->clk_fod[n].hw.init = &init;
1068         ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
1069         if (ret)
1070             goto err_clk_register;
1071         kfree(init.name); /* clock framework made a copy of the name */
1072     }
1073 
1074     /* Register MUX-connected OUT0_I2C_SELB output */
1075     memset(&init, 0, sizeof(init));
1076     init.name = kasprintf(GFP_KERNEL, "%pOFn.out0_sel_i2cb",
1077                   client->dev.of_node);
1078     init.ops = &vc5_clk_out_ops;
1079     init.flags = CLK_SET_RATE_PARENT;
1080     init.parent_names = parent_names;
1081     parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
1082     init.num_parents = 1;
1083     vc5->clk_out[0].num = idx;
1084     vc5->clk_out[0].vc5 = vc5;
1085     vc5->clk_out[0].hw.init = &init;
1086     ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
1087     if (ret)
1088         goto err_clk_register;
1089     kfree(init.name); /* clock framework made a copy of the name */
1090 
1091     /* Register FOD-connected OUTx outputs */
1092     for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
1093         idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
1094         parent_names[0] = clk_hw_get_name(&vc5->clk_fod[idx].hw);
1095         if (n == 1)
1096             parent_names[1] = clk_hw_get_name(&vc5->clk_mux);
1097         else
1098             parent_names[1] =
1099                 clk_hw_get_name(&vc5->clk_out[n - 1].hw);
1100 
1101         memset(&init, 0, sizeof(init));
1102         init.name = kasprintf(GFP_KERNEL, "%pOFn.out%d",
1103                       client->dev.of_node, idx + 1);
1104         init.ops = &vc5_clk_out_ops;
1105         init.flags = CLK_SET_RATE_PARENT;
1106         init.parent_names = parent_names;
1107         init.num_parents = 2;
1108         vc5->clk_out[n].num = idx;
1109         vc5->clk_out[n].vc5 = vc5;
1110         vc5->clk_out[n].hw.init = &init;
1111         ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[n].hw);
1112         if (ret)
1113             goto err_clk_register;
1114         kfree(init.name); /* clock framework made a copy of the name */
1115 
1116         /* Fetch Clock Output configuration from DT (if specified) */
1117         ret = vc5_get_output_config(client, &vc5->clk_out[n]);
1118         if (ret)
1119             goto err_clk;
1120     }
1121 
1122     ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
1123     if (ret) {
1124         dev_err_probe(&client->dev, ret,
1125                   "unable to add clk provider\n");
1126         goto err_clk;
1127     }
1128 
1129     return 0;
1130 
1131 err_clk_register:
1132     dev_err_probe(&client->dev, ret,
1133               "unable to register %s\n", init.name);
1134     kfree(init.name); /* clock framework made a copy of the name */
1135 err_clk:
1136     if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
1137         clk_unregister_fixed_rate(vc5->pin_xin);
1138     return ret;
1139 }
1140 
1141 static int vc5_remove(struct i2c_client *client)
1142 {
1143     struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
1144 
1145     of_clk_del_provider(client->dev.of_node);
1146 
1147     if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
1148         clk_unregister_fixed_rate(vc5->pin_xin);
1149 
1150     return 0;
1151 }
1152 
1153 static int __maybe_unused vc5_suspend(struct device *dev)
1154 {
1155     struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
1156 
1157     regcache_cache_only(vc5->regmap, true);
1158     regcache_mark_dirty(vc5->regmap);
1159 
1160     return 0;
1161 }
1162 
1163 static int __maybe_unused vc5_resume(struct device *dev)
1164 {
1165     struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
1166     int ret;
1167 
1168     regcache_cache_only(vc5->regmap, false);
1169     ret = regcache_sync(vc5->regmap);
1170     if (ret)
1171         dev_err(dev, "Failed to restore register map: %d\n", ret);
1172     return ret;
1173 }
1174 
1175 static const struct vc5_chip_info idt_5p49v5923_info = {
1176     .model = IDT_VC5_5P49V5923,
1177     .clk_fod_cnt = 2,
1178     .clk_out_cnt = 3,
1179     .flags = 0,
1180 };
1181 
1182 static const struct vc5_chip_info idt_5p49v5925_info = {
1183     .model = IDT_VC5_5P49V5925,
1184     .clk_fod_cnt = 4,
1185     .clk_out_cnt = 5,
1186     .flags = 0,
1187 };
1188 
1189 static const struct vc5_chip_info idt_5p49v5933_info = {
1190     .model = IDT_VC5_5P49V5933,
1191     .clk_fod_cnt = 2,
1192     .clk_out_cnt = 3,
1193     .flags = VC5_HAS_INTERNAL_XTAL,
1194 };
1195 
1196 static const struct vc5_chip_info idt_5p49v5935_info = {
1197     .model = IDT_VC5_5P49V5935,
1198     .clk_fod_cnt = 4,
1199     .clk_out_cnt = 5,
1200     .flags = VC5_HAS_INTERNAL_XTAL,
1201 };
1202 
1203 static const struct vc5_chip_info idt_5p49v6901_info = {
1204     .model = IDT_VC6_5P49V6901,
1205     .clk_fod_cnt = 4,
1206     .clk_out_cnt = 5,
1207     .flags = VC5_HAS_PFD_FREQ_DBL,
1208 };
1209 
1210 static const struct vc5_chip_info idt_5p49v6965_info = {
1211     .model = IDT_VC6_5P49V6965,
1212     .clk_fod_cnt = 4,
1213     .clk_out_cnt = 5,
1214     .flags = VC5_HAS_BYPASS_SYNC_BIT,
1215 };
1216 
1217 static const struct i2c_device_id vc5_id[] = {
1218     { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
1219     { "5p49v5925", .driver_data = IDT_VC5_5P49V5925 },
1220     { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
1221     { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
1222     { "5p49v6901", .driver_data = IDT_VC6_5P49V6901 },
1223     { "5p49v6965", .driver_data = IDT_VC6_5P49V6965 },
1224     { }
1225 };
1226 MODULE_DEVICE_TABLE(i2c, vc5_id);
1227 
1228 static const struct of_device_id clk_vc5_of_match[] = {
1229     { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
1230     { .compatible = "idt,5p49v5925", .data = &idt_5p49v5925_info },
1231     { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
1232     { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
1233     { .compatible = "idt,5p49v6901", .data = &idt_5p49v6901_info },
1234     { .compatible = "idt,5p49v6965", .data = &idt_5p49v6965_info },
1235     { },
1236 };
1237 MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
1238 
1239 static SIMPLE_DEV_PM_OPS(vc5_pm_ops, vc5_suspend, vc5_resume);
1240 
1241 static struct i2c_driver vc5_driver = {
1242     .driver = {
1243         .name = "vc5",
1244         .pm = &vc5_pm_ops,
1245         .of_match_table = clk_vc5_of_match,
1246     },
1247     .probe_new  = vc5_probe,
1248     .remove     = vc5_remove,
1249     .id_table   = vc5_id,
1250 };
1251 module_i2c_driver(vc5_driver);
1252 
1253 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
1254 MODULE_DESCRIPTION("IDT VersaClock 5 driver");
1255 MODULE_LICENSE("GPL");