1 // SPDX-License-Identifier: GPL-2.0
3 * LMK04832 Ultra Low-Noise JESD204B Compliant Clock Jitter Cleaner
4 * Pin compatible with the LMK0482x family
6 * Datasheet: https://www.ti.com/lit/ds/symlink/lmk04832.pdf
8 * Copyright (c) 2020, Xiphos Systems Corp.
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/device.h>
16 #include <linux/gcd.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <linux/spi/spi.h>
22 /* 0x000 - 0x00d System Functions */
23 #define LMK04832_REG_RST3W 0x000
24 #define LMK04832_BIT_RESET BIT(7)
25 #define LMK04832_BIT_SPI_3WIRE_DIS BIT(4)
26 #define LMK04832_REG_POWERDOWN 0x002
27 #define LMK04832_REG_ID_DEV_TYPE 0x003
28 #define LMK04832_REG_ID_PROD_MSB 0x004
29 #define LMK04832_REG_ID_PROD_LSB 0x005
30 #define LMK04832_REG_ID_MASKREV 0x006
31 #define LMK04832_REG_ID_VNDR_MSB 0x00c
32 #define LMK04832_REG_ID_VNDR_LSB 0x00d
34 /* 0x100 - 0x137 Device Clock and SYSREF Clock Output Control */
35 #define LMK04832_REG_CLKOUT_CTRL0(ch) (0x100 + (ch >> 1) * 8)
36 #define LMK04832_BIT_DCLK_DIV_LSB GENMASK(7, 0)
37 #define LMK04832_REG_CLKOUT_CTRL1(ch) (0x101 + (ch >> 1) * 8)
38 #define LMK04832_BIT_DCLKX_Y_DDLY_LSB GENMASK(7, 0)
39 #define LMK04832_REG_CLKOUT_CTRL2(ch) (0x102 + (ch >> 1) * 8)
40 #define LMK04832_BIT_CLKOUTX_Y_PD BIT(7)
41 #define LMK04832_BIT_DCLKX_Y_DDLY_PD BIT(4)
42 #define LMK04832_BIT_DCLKX_Y_DDLY_MSB GENMASK(3, 2)
43 #define LMK04832_BIT_DCLK_DIV_MSB GENMASK(1, 0)
44 #define LMK04832_REG_CLKOUT_SRC_MUX(ch) (0x103 + (ch % 2) + (ch >> 1) * 8)
45 #define LMK04832_BIT_CLKOUT_SRC_MUX BIT(5)
46 #define LMK04832_REG_CLKOUT_CTRL3(ch) (0x103 + (ch >> 1) * 8)
47 #define LMK04832_BIT_DCLKX_Y_PD BIT(4)
48 #define LMK04832_BIT_DCLKX_Y_DCC BIT(2)
49 #define LMK04832_BIT_DCLKX_Y_HS BIT(0)
50 #define LMK04832_REG_CLKOUT_CTRL4(ch) (0x104 + (ch >> 1) * 8)
51 #define LMK04832_BIT_SCLK_PD BIT(4)
52 #define LMK04832_BIT_SCLKX_Y_DIS_MODE GENMASK(3, 2)
53 #define LMK04832_REG_SCLKX_Y_ADLY(ch) (0x105 + (ch >> 1) * 8)
54 #define LMK04832_REG_SCLKX_Y_DDLY(ch) (0x106 + (ch >> 1) * 8)
55 #define LMK04832_BIT_SCLKX_Y_DDLY GENMASK(3, 0)
56 #define LMK04832_REG_CLKOUT_FMT(ch) (0x107 + (ch >> 1) * 8)
57 #define LMK04832_BIT_CLKOUT_FMT(ch) (ch % 2 ? 0xf0 : 0x0f)
58 #define LMK04832_VAL_CLKOUT_FMT_POWERDOWN 0x00
59 #define LMK04832_VAL_CLKOUT_FMT_LVDS 0x01
60 #define LMK04832_VAL_CLKOUT_FMT_HSDS6 0x02
61 #define LMK04832_VAL_CLKOUT_FMT_HSDS8 0x03
62 #define LMK04832_VAL_CLKOUT_FMT_LVPECL1600 0x04
63 #define LMK04832_VAL_CLKOUT_FMT_LVPECL2000 0x05
64 #define LMK04832_VAL_CLKOUT_FMT_LCPECL 0x06
65 #define LMK04832_VAL_CLKOUT_FMT_CML16 0x07
66 #define LMK04832_VAL_CLKOUT_FMT_CML24 0x08
67 #define LMK04832_VAL_CLKOUT_FMT_CML32 0x09
68 #define LMK04832_VAL_CLKOUT_FMT_CMOS_OFF_INV 0x0a
69 #define LMK04832_VAL_CLKOUT_FMT_CMOS_NOR_OFF 0x0b
70 #define LMK04832_VAL_CLKOUT_FMT_CMOS_INV_INV 0x0c
71 #define LMK04832_VAL_CLKOUT_FMT_CMOS_INV_NOR 0x0d
72 #define LMK04832_VAL_CLKOUT_FMT_CMOS_NOR_INV 0x0e
73 #define LMK04832_VAL_CLKOUT_FMT_CMOS_NOR_NOR 0x0f
75 /* 0x138 - 0x145 SYSREF, SYNC, and Device Config */
76 #define LMK04832_REG_VCO_OSCOUT 0x138
77 #define LMK04832_BIT_VCO_MUX GENMASK(6, 5)
78 #define LMK04832_VAL_VCO_MUX_VCO0 0x00
79 #define LMK04832_VAL_VCO_MUX_VCO1 0x01
80 #define LMK04832_VAL_VCO_MUX_EXT 0x02
81 #define LMK04832_REG_SYSREF_OUT 0x139
82 #define LMK04832_BIT_SYSREF_REQ_EN BIT(6)
83 #define LMK04832_BIT_SYSREF_MUX GENMASK(1, 0)
84 #define LMK04832_VAL_SYSREF_MUX_NORMAL_SYNC 0x00
85 #define LMK04832_VAL_SYSREF_MUX_RECLK 0x01
86 #define LMK04832_VAL_SYSREF_MUX_PULSER 0x02
87 #define LMK04832_VAL_SYSREF_MUX_CONTINUOUS 0x03
88 #define LMK04832_REG_SYSREF_DIV_MSB 0x13a
89 #define LMK04832_BIT_SYSREF_DIV_MSB GENMASK(4, 0)
90 #define LMK04832_REG_SYSREF_DIV_LSB 0x13b
91 #define LMK04832_REG_SYSREF_DDLY_MSB 0x13c
92 #define LMK04832_BIT_SYSREF_DDLY_MSB GENMASK(4, 0)
93 #define LMK04832_REG_SYSREF_DDLY_LSB 0x13d
94 #define LMK04832_REG_SYSREF_PULSE_CNT 0x13e
95 #define LMK04832_REG_FB_CTRL 0x13f
96 #define LMK04832_BIT_PLL2_RCLK_MUX BIT(7)
97 #define LMK04832_VAL_PLL2_RCLK_MUX_OSCIN 0x00
98 #define LMK04832_VAL_PLL2_RCLK_MUX_CLKIN 0x01
99 #define LMK04832_BIT_PLL2_NCLK_MUX BIT(5)
100 #define LMK04832_VAL_PLL2_NCLK_MUX_PLL2_P 0x00
101 #define LMK04832_VAL_PLL2_NCLK_MUX_FB_MUX 0x01
102 #define LMK04832_BIT_FB_MUX_EN BIT(0)
103 #define LMK04832_REG_MAIN_PD 0x140
104 #define LMK04832_BIT_PLL1_PD BIT(7)
105 #define LMK04832_BIT_VCO_LDO_PD BIT(6)
106 #define LMK04832_BIT_VCO_PD BIT(5)
107 #define LMK04832_BIT_OSCIN_PD BIT(4)
108 #define LMK04832_BIT_SYSREF_GBL_PD BIT(3)
109 #define LMK04832_BIT_SYSREF_PD BIT(2)
110 #define LMK04832_BIT_SYSREF_DDLY_PD BIT(1)
111 #define LMK04832_BIT_SYSREF_PLSR_PD BIT(0)
112 #define LMK04832_REG_SYNC 0x143
113 #define LMK04832_BIT_SYNC_CLR BIT(7)
114 #define LMK04832_BIT_SYNC_1SHOT_EN BIT(6)
115 #define LMK04832_BIT_SYNC_POL BIT(5)
116 #define LMK04832_BIT_SYNC_EN BIT(4)
117 #define LMK04832_BIT_SYNC_MODE GENMASK(1, 0)
118 #define LMK04832_VAL_SYNC_MODE_OFF 0x00
119 #define LMK04832_VAL_SYNC_MODE_ON 0x01
120 #define LMK04832_VAL_SYNC_MODE_PULSER_PIN 0x02
121 #define LMK04832_VAL_SYNC_MODE_PULSER_SPI 0x03
122 #define LMK04832_REG_SYNC_DIS 0x144
124 /* 0x146 - 0x14a CLKin Control */
125 #define LMK04832_REG_CLKIN_SEL0 0x148
126 #define LMK04832_REG_CLKIN_SEL1 0x149
127 #define LMK04832_REG_CLKIN_RST 0x14a
128 #define LMK04832_BIT_SDIO_RDBK_TYPE BIT(6)
129 #define LMK04832_BIT_CLKIN_SEL_MUX GENMASK(5, 3)
130 #define LMK04832_VAL_CLKIN_SEL_MUX_SPI_RDBK 0x06
131 #define LMK04832_BIT_CLKIN_SEL_TYPE GENMASK(2, 0)
132 #define LMK04832_VAL_CLKIN_SEL_TYPE_OUT 0x03
134 /* 0x14b - 0x152 Holdover */
136 /* 0x153 - 0x15f PLL1 Configuration */
137 #define LMK04832_REG_PLL1_LD 0x15f
138 #define LMK04832_BIT_PLL1_LD_MUX GENMASK(7, 3)
139 #define LMK04832_VAL_PLL1_LD_MUX_SPI_RDBK 0x07
140 #define LMK04832_BIT_PLL1_LD_TYPE GENMASK(2, 0)
141 #define LMK04832_VAL_PLL1_LD_TYPE_OUT_PP 0x03
143 /* 0x160 - 0x16e PLL2 Configuration */
144 #define LMK04832_REG_PLL2_R_MSB 0x160
145 #define LMK04832_BIT_PLL2_R_MSB GENMASK(3, 0)
146 #define LMK04832_REG_PLL2_R_LSB 0x161
147 #define LMK04832_REG_PLL2_MISC 0x162
148 #define LMK04832_BIT_PLL2_MISC_P GENMASK(7, 5)
149 #define LMK04832_BIT_PLL2_MISC_REF_2X_EN BIT(0)
150 #define LMK04832_REG_PLL2_N_CAL_0 0x163
151 #define LMK04832_BIT_PLL2_N_CAL_0 GENMASK(1, 0)
152 #define LMK04832_REG_PLL2_N_CAL_1 0x164
153 #define LMK04832_REG_PLL2_N_CAL_2 0x165
154 #define LMK04832_REG_PLL2_N_0 0x166
155 #define LMK04832_BIT_PLL2_N_0 GENMASK(1, 0)
156 #define LMK04832_REG_PLL2_N_1 0x167
157 #define LMK04832_REG_PLL2_N_2 0x168
158 #define LMK04832_REG_PLL2_DLD_CNT_MSB 0x16a
159 #define LMK04832_REG_PLL2_DLD_CNT_LSB 0x16b
160 #define LMK04832_REG_PLL2_LD 0x16e
161 #define LMK04832_BIT_PLL2_LD_MUX GENMASK(7, 3)
162 #define LMK04832_VAL_PLL2_LD_MUX_PLL2_DLD 0x02
163 #define LMK04832_BIT_PLL2_LD_TYPE GENMASK(2, 0)
164 #define LMK04832_VAL_PLL2_LD_TYPE_OUT_PP 0x03
166 /* 0x16F - 0x555 Misc Registers */
167 #define LMK04832_REG_PLL2_PD 0x173
168 #define LMK04832_BIT_PLL2_PRE_PD BIT(6)
169 #define LMK04832_BIT_PLL2_PD BIT(5)
170 #define LMK04832_REG_PLL1R_RST 0x177
171 #define LMK04832_REG_CLR_PLL_LOST 0x182
172 #define LMK04832_REG_RB_PLL_LD 0x183
173 #define LMK04832_REG_RB_CLK_DAC_VAL_MSB 0x184
174 #define LMK04832_REG_RB_DAC_VAL_LSB 0x185
175 #define LMK04832_REG_RB_HOLDOVER 0x188
176 #define LMK04832_REG_SPI_LOCK 0x555
178 enum lmk04832_device_types {
183 * struct lmk04832_device_info - Holds static device information that is
184 * specific to the chip revision
186 * @pid: Product Identifier
187 * @maskrev: IC version identifier
188 * @num_channels: Number of available output channels (clkout count)
189 * @vco0_range: {min, max} of the VCO0 operating range (in MHz)
190 * @vco1_range: {min, max} of the VCO1 operating range (in MHz)
192 struct lmk04832_device_info {
196 unsigned int vco0_range[2];
197 unsigned int vco1_range[2];
200 static const struct lmk04832_device_info lmk04832_device_info[] = {
202 .pid = 0x63d1, /* WARNING PROD_ID is inverted in the datasheet */
205 .vco0_range = { 2440, 2580 },
206 .vco1_range = { 2945, 3255 },
210 enum lmk04832_rdbk_type {
218 struct lmk04832 *lmk;
224 struct lmk04832 *lmk;
232 * struct lmk04832 - The LMK04832 device structure
234 * @dev: reference to a struct device, linked to the spi_device
235 * @regmap: struct regmap instance use to access the chip
236 * @sync_mode: operational mode for SYNC signal
237 * @sysref_mux: select SYSREF source
238 * @sysref_pulse_cnt: number of SYSREF pulses generated while not in continuous
240 * @sysref_ddly: SYSREF digital delay value
241 * @oscin: PLL2 input clock
242 * @vco: reference to the internal VCO clock
243 * @sclk: reference to the internal sysref clock (SCLK)
244 * @vco_rate: user provided VCO rate
245 * @reset_gpio: reference to the reset GPIO
246 * @dclk: list of internal device clock references.
247 * Each pair of clkout clocks share a single device clock (DCLKX_Y)
248 * @clkout: list of output clock references
249 * @clk_data: holds clkout related data like clk_hw* and number of clocks
253 struct regmap *regmap;
255 unsigned int sync_mode;
256 unsigned int sysref_mux;
257 unsigned int sysref_pulse_cnt;
258 unsigned int sysref_ddly;
263 unsigned int vco_rate;
265 struct gpio_desc *reset_gpio;
267 struct lmk_dclk *dclk;
268 struct lmk_clkout *clkout;
269 struct clk_hw_onecell_data *clk_data;
272 static bool lmk04832_regmap_rd_regs(struct device *dev, unsigned int reg)
275 case LMK04832_REG_RST3W ... LMK04832_REG_ID_MASKREV:
276 case LMK04832_REG_ID_VNDR_MSB:
277 case LMK04832_REG_ID_VNDR_LSB:
278 case LMK04832_REG_CLKOUT_CTRL0(0) ... LMK04832_REG_PLL2_DLD_CNT_LSB:
279 case LMK04832_REG_PLL2_LD:
280 case LMK04832_REG_PLL2_PD:
281 case LMK04832_REG_PLL1R_RST:
282 case LMK04832_REG_CLR_PLL_LOST ... LMK04832_REG_RB_DAC_VAL_LSB:
283 case LMK04832_REG_RB_HOLDOVER:
284 case LMK04832_REG_SPI_LOCK:
291 static bool lmk04832_regmap_wr_regs(struct device *dev, unsigned int reg)
294 case LMK04832_REG_RST3W:
295 case LMK04832_REG_POWERDOWN:
297 case LMK04832_REG_ID_DEV_TYPE ... LMK04832_REG_ID_MASKREV:
298 case LMK04832_REG_ID_VNDR_MSB:
299 case LMK04832_REG_ID_VNDR_LSB:
301 case LMK04832_REG_CLKOUT_CTRL0(0) ... LMK04832_REG_PLL2_DLD_CNT_LSB:
302 case LMK04832_REG_PLL2_LD:
303 case LMK04832_REG_PLL2_PD:
304 case LMK04832_REG_PLL1R_RST:
305 case LMK04832_REG_CLR_PLL_LOST ... LMK04832_REG_RB_DAC_VAL_LSB:
306 case LMK04832_REG_RB_HOLDOVER:
307 case LMK04832_REG_SPI_LOCK:
314 static const struct regmap_config regmap_config = {
318 .use_single_read = 1,
319 .use_single_write = 1,
320 .read_flag_mask = 0x80,
321 .write_flag_mask = 0x00,
322 .readable_reg = lmk04832_regmap_rd_regs,
323 .writeable_reg = lmk04832_regmap_wr_regs,
324 .cache_type = REGCACHE_NONE,
325 .max_register = LMK04832_REG_SPI_LOCK,
328 static int lmk04832_vco_is_enabled(struct clk_hw *hw)
330 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
334 ret = regmap_read(lmk->regmap, LMK04832_REG_MAIN_PD, &tmp);
338 return !(FIELD_GET(LMK04832_BIT_OSCIN_PD, tmp) |
339 FIELD_GET(LMK04832_BIT_VCO_PD, tmp) |
340 FIELD_GET(LMK04832_BIT_VCO_LDO_PD, tmp));
343 static int lmk04832_vco_prepare(struct clk_hw *hw)
345 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
348 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_PLL2_PD,
349 LMK04832_BIT_PLL2_PRE_PD |
350 LMK04832_BIT_PLL2_PD,
355 return regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
356 LMK04832_BIT_VCO_LDO_PD |
357 LMK04832_BIT_VCO_PD |
358 LMK04832_BIT_OSCIN_PD, 0x00);
361 static void lmk04832_vco_unprepare(struct clk_hw *hw)
363 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
365 regmap_update_bits(lmk->regmap, LMK04832_REG_PLL2_PD,
366 LMK04832_BIT_PLL2_PRE_PD | LMK04832_BIT_PLL2_PD,
369 /* Don't set LMK04832_BIT_OSCIN_PD since other clocks depend on it */
370 regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
371 LMK04832_BIT_VCO_LDO_PD | LMK04832_BIT_VCO_PD, 0xff);
374 static unsigned long lmk04832_vco_recalc_rate(struct clk_hw *hw,
377 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
378 const unsigned int pll2_p[] = {8, 2, 2, 3, 4, 5, 6, 7};
379 unsigned int pll2_n, p, pll2_r;
380 unsigned int pll2_misc;
381 unsigned long vco_rate;
385 ret = regmap_read(lmk->regmap, LMK04832_REG_PLL2_MISC, &pll2_misc);
389 p = FIELD_GET(LMK04832_BIT_PLL2_MISC_P, pll2_misc);
391 ret = regmap_bulk_read(lmk->regmap, LMK04832_REG_PLL2_N_0, &tmp, 3);
395 pll2_n = FIELD_PREP(0x030000, tmp[0]) |
396 FIELD_PREP(0x00ff00, tmp[1]) |
397 FIELD_PREP(0x0000ff, tmp[2]);
399 ret = regmap_bulk_read(lmk->regmap, LMK04832_REG_PLL2_R_MSB, &tmp, 2);
403 pll2_r = FIELD_PREP(0x0f00, tmp[0]) |
404 FIELD_PREP(0x00ff, tmp[1]);
406 vco_rate = (prate << FIELD_GET(LMK04832_BIT_PLL2_MISC_REF_2X_EN,
407 pll2_misc)) * pll2_n * pll2_p[p] / pll2_r;
413 * lmk04832_check_vco_ranges - Check requested VCO frequency against VCO ranges
415 * @lmk: Reference to the lmk device
416 * @rate: Desired output rate for the VCO
418 * The LMK04832 has 2 internal VCO, each with independent operating ranges.
419 * Use the device_info structure to determine which VCO to use based on rate.
421 * Returns: VCO_MUX value or negative errno.
423 static int lmk04832_check_vco_ranges(struct lmk04832 *lmk, unsigned long rate)
425 struct spi_device *spi = to_spi_device(lmk->dev);
426 const struct lmk04832_device_info *info;
427 unsigned long mhz = rate / 1000000;
429 info = &lmk04832_device_info[spi_get_device_id(spi)->driver_data];
431 if (mhz >= info->vco0_range[0] && mhz <= info->vco0_range[1])
432 return LMK04832_VAL_VCO_MUX_VCO0;
434 if (mhz >= info->vco1_range[0] && mhz <= info->vco1_range[1])
435 return LMK04832_VAL_VCO_MUX_VCO1;
437 dev_err(lmk->dev, "%lu Hz is out of VCO ranges\n", rate);
442 * lmk04832_calc_pll2_params - Get PLL2 parameters used to set the VCO frequency
444 * @prate: parent rate to the PLL2, usually OSCin
445 * @rate: Desired output rate for the VCO
446 * @n: reference to PLL2_N
447 * @p: reference to PLL2_P
448 * @r: reference to PLL2_R
450 * This functions assumes LMK04832_BIT_PLL2_MISC_REF_2X_EN is set since it is
451 * recommended in the datasheet because a higher phase detector frequencies
452 * makes the design of wider loop bandwidth filters possible.
454 * the VCO rate can be calculated using the following expression:
456 * VCO = OSCin * 2 * PLL2_N * PLL2_P / PLL2_R
458 * Returns: vco rate or negative errno.
460 static long lmk04832_calc_pll2_params(unsigned long prate, unsigned long rate,
461 unsigned int *n, unsigned int *p,
464 unsigned int pll2_n, pll2_p, pll2_r;
465 unsigned long num, div;
467 /* Set PLL2_P to a fixed value to simplify optimizations */
470 div = gcd(rate, prate);
472 num = DIV_ROUND_CLOSEST(rate, div);
473 pll2_r = DIV_ROUND_CLOSEST(prate, div);
478 pll2_r = pll2_r << 2;
482 if (pll2_n < 1 || pll2_n > 0x03ffff)
484 if (pll2_r < 1 || pll2_r > 0xfff)
491 return DIV_ROUND_CLOSEST(prate * 2 * pll2_p * pll2_n, pll2_r);
494 static long lmk04832_vco_round_rate(struct clk_hw *hw, unsigned long rate,
495 unsigned long *prate)
497 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
498 unsigned int n, p, r;
502 ret = lmk04832_check_vco_ranges(lmk, rate);
506 vco_rate = lmk04832_calc_pll2_params(*prate, rate, &n, &p, &r);
508 dev_err(lmk->dev, "PLL2 parameters out of range\n");
512 if (rate != vco_rate)
518 static int lmk04832_vco_set_rate(struct clk_hw *hw, unsigned long rate,
521 struct lmk04832 *lmk = container_of(hw, struct lmk04832, vco);
522 unsigned int n, p, r;
527 vco_mux = lmk04832_check_vco_ranges(lmk, rate);
531 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_VCO_OSCOUT,
532 LMK04832_BIT_VCO_MUX,
533 FIELD_PREP(LMK04832_BIT_VCO_MUX, vco_mux));
537 vco_rate = lmk04832_calc_pll2_params(prate, rate, &n, &p, &r);
539 dev_err(lmk->dev, "failed to determine PLL2 parameters\n");
543 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_PLL2_R_MSB,
544 LMK04832_BIT_PLL2_R_MSB,
545 FIELD_GET(0x000700, r));
549 ret = regmap_write(lmk->regmap, LMK04832_REG_PLL2_R_LSB,
550 FIELD_GET(0x0000ff, r));
554 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_PLL2_MISC,
555 LMK04832_BIT_PLL2_MISC_P,
556 FIELD_PREP(LMK04832_BIT_PLL2_MISC_P, p));
561 * PLL2_N registers must be programmed after other PLL2 dividers are
562 * programmed to ensure proper VCO frequency calibration
564 ret = regmap_write(lmk->regmap, LMK04832_REG_PLL2_N_0,
565 FIELD_GET(0x030000, n));
568 ret = regmap_write(lmk->regmap, LMK04832_REG_PLL2_N_1,
569 FIELD_GET(0x00ff00, n));
573 return regmap_write(lmk->regmap, LMK04832_REG_PLL2_N_2,
574 FIELD_GET(0x0000ff, n));
577 static const struct clk_ops lmk04832_vco_ops = {
578 .is_enabled = lmk04832_vco_is_enabled,
579 .prepare = lmk04832_vco_prepare,
580 .unprepare = lmk04832_vco_unprepare,
581 .recalc_rate = lmk04832_vco_recalc_rate,
582 .round_rate = lmk04832_vco_round_rate,
583 .set_rate = lmk04832_vco_set_rate,
587 * lmk04832_register_vco - Initialize the internal VCO and clock distribution
588 * path in PLL2 single loop mode.
590 static int lmk04832_register_vco(struct lmk04832 *lmk)
592 const char *parent_names[1];
593 struct clk_init_data init;
596 init.name = "lmk-vco";
597 parent_names[0] = __clk_get_name(lmk->oscin);
598 init.parent_names = parent_names;
600 init.ops = &lmk04832_vco_ops;
601 init.num_parents = 1;
603 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_VCO_OSCOUT,
604 LMK04832_BIT_VCO_MUX,
605 FIELD_PREP(LMK04832_BIT_VCO_MUX,
606 LMK04832_VAL_VCO_MUX_VCO1));
610 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_FB_CTRL,
611 LMK04832_BIT_PLL2_RCLK_MUX |
612 LMK04832_BIT_PLL2_NCLK_MUX,
613 FIELD_PREP(LMK04832_BIT_PLL2_RCLK_MUX,
614 LMK04832_VAL_PLL2_RCLK_MUX_OSCIN)|
615 FIELD_PREP(LMK04832_BIT_PLL2_NCLK_MUX,
616 LMK04832_VAL_PLL2_NCLK_MUX_PLL2_P));
620 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_PLL2_MISC,
621 LMK04832_BIT_PLL2_MISC_REF_2X_EN,
622 LMK04832_BIT_PLL2_MISC_REF_2X_EN);
626 ret = regmap_write(lmk->regmap, LMK04832_REG_PLL2_LD,
627 FIELD_PREP(LMK04832_BIT_PLL2_LD_MUX,
628 LMK04832_VAL_PLL2_LD_MUX_PLL2_DLD) |
629 FIELD_PREP(LMK04832_BIT_PLL2_LD_TYPE,
630 LMK04832_VAL_PLL2_LD_TYPE_OUT_PP));
634 lmk->vco.init = &init;
635 return devm_clk_hw_register(lmk->dev, &lmk->vco);
638 static int lmk04832_clkout_set_ddly(struct lmk04832 *lmk, int id)
640 const int dclk_div_adj[] = {0, 0, -2, -2, 0, 3, -1, 0};
641 unsigned int sclkx_y_ddly = 10;
642 unsigned int dclkx_y_ddly;
643 unsigned int dclkx_y_div;
644 unsigned int sysref_ddly;
645 unsigned int dclkx_y_hs;
646 unsigned int lsb, msb;
649 ret = regmap_update_bits(lmk->regmap,
650 LMK04832_REG_CLKOUT_CTRL2(id),
651 LMK04832_BIT_DCLKX_Y_DDLY_PD,
652 FIELD_PREP(LMK04832_BIT_DCLKX_Y_DDLY_PD, 0));
656 ret = regmap_read(lmk->regmap, LMK04832_REG_SYSREF_DDLY_LSB, &lsb);
660 ret = regmap_read(lmk->regmap, LMK04832_REG_SYSREF_DDLY_MSB, &msb);
664 sysref_ddly = FIELD_GET(LMK04832_BIT_SYSREF_DDLY_MSB, msb) << 8 | lsb;
666 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL0(id), &lsb);
670 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL2(id), &msb);
674 dclkx_y_div = FIELD_GET(LMK04832_BIT_DCLK_DIV_MSB, msb) << 8 | lsb;
676 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL3(id), &lsb);
680 dclkx_y_hs = FIELD_GET(LMK04832_BIT_DCLKX_Y_HS, lsb);
682 dclkx_y_ddly = sysref_ddly + 1 -
683 dclk_div_adj[dclkx_y_div < 6 ? dclkx_y_div : 7] -
684 dclkx_y_hs + sclkx_y_ddly;
686 if (dclkx_y_ddly < 7 || dclkx_y_ddly > 0x3fff) {
687 dev_err(lmk->dev, "DCLKX_Y_DDLY out of range (%d)\n",
692 ret = regmap_write(lmk->regmap,
693 LMK04832_REG_SCLKX_Y_DDLY(id),
694 FIELD_GET(LMK04832_BIT_SCLKX_Y_DDLY, sclkx_y_ddly));
698 ret = regmap_write(lmk->regmap, LMK04832_REG_CLKOUT_CTRL1(id),
699 FIELD_GET(0x00ff, dclkx_y_ddly));
703 dev_dbg(lmk->dev, "clkout%02u: sysref_ddly=%u, dclkx_y_ddly=%u, "
704 "dclk_div_adj=%+d, dclkx_y_hs=%u, sclkx_y_ddly=%u\n",
705 id, sysref_ddly, dclkx_y_ddly,
706 dclk_div_adj[dclkx_y_div < 6 ? dclkx_y_div : 7],
707 dclkx_y_hs, sclkx_y_ddly);
709 return regmap_update_bits(lmk->regmap, LMK04832_REG_CLKOUT_CTRL2(id),
710 LMK04832_BIT_DCLKX_Y_DDLY_MSB,
711 FIELD_GET(0x0300, dclkx_y_ddly));
714 /** lmk04832_sclk_sync - Establish deterministic phase relationship between sclk
717 * @lmk: Reference to the lmk device
719 * The synchronization sequence:
720 * - in the datasheet https://www.ti.com/lit/ds/symlink/lmk04832.pdf, p.31
721 * (8.3.3.1 How to enable SYSREF)
722 * - Ti forum: https://e2e.ti.com/support/clock-and-timing/f/48/t/970972
724 * Returns 0 or negative errno.
726 static int lmk04832_sclk_sync_sequence(struct lmk04832 *lmk)
731 /* 1. (optional) mute all sysref_outputs during synchronization */
732 /* 2. Enable and write device clock digital delay to applicable clocks */
733 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
734 LMK04832_BIT_SYSREF_DDLY_PD,
735 FIELD_PREP(LMK04832_BIT_SYSREF_DDLY_PD, 0));
739 for (i = 0; i < lmk->clk_data->num; i += 2) {
740 ret = lmk04832_clkout_set_ddly(lmk, i);
746 * 3. Configure SYNC_MODE to SYNC_PIN and SYSREF_MUX to Normal SYNC,
747 * and clear SYSREF_REQ_EN (see 6.)
749 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYSREF_OUT,
750 LMK04832_BIT_SYSREF_REQ_EN |
751 LMK04832_BIT_SYSREF_MUX,
752 FIELD_PREP(LMK04832_BIT_SYSREF_REQ_EN, 0) |
753 FIELD_PREP(LMK04832_BIT_SYSREF_MUX,
754 LMK04832_VAL_SYSREF_MUX_NORMAL_SYNC));
758 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
759 LMK04832_BIT_SYNC_MODE,
760 FIELD_PREP(LMK04832_BIT_SYNC_MODE,
761 LMK04832_VAL_SYNC_MODE_ON));
765 /* 4. Clear SYNXC_DISx or applicable clocks and clear SYNC_DISSYSREF */
766 ret = regmap_write(lmk->regmap, LMK04832_REG_SYNC_DIS, 0x00);
771 * 5. If SCLKX_Y_DDLY != 0, Set SYSREF_CLR=1 for at least 15 clock
772 * distribution path cycles (VCO cycles), then back to 0. In
773 * PLL2-only use case, this will be complete in less than one SPI
774 * transaction. If SYSREF local digital delay is not used, this step
777 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
778 LMK04832_BIT_SYNC_CLR,
779 FIELD_PREP(LMK04832_BIT_SYNC_CLR, 0x01));
783 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
784 LMK04832_BIT_SYNC_CLR,
785 FIELD_PREP(LMK04832_BIT_SYNC_CLR, 0x00));
790 * 6. Toggle SYNC_POL state between inverted and not inverted.
791 * If you use an external signal on the SYNC pin instead of toggling
792 * SYNC_POL, make sure that SYSREF_REQ_EN=0 so that the SYSREF_MUX
793 * does not shift into continuous SYSREF mode.
795 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
796 LMK04832_BIT_SYNC_POL,
797 FIELD_PREP(LMK04832_BIT_SYNC_POL, 0x01));
801 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
802 LMK04832_BIT_SYNC_POL,
803 FIELD_PREP(LMK04832_BIT_SYNC_POL, 0x00));
807 /* 7. Set all SYNC_DISx=1, including SYNC_DISSYSREF */
808 ret = regmap_write(lmk->regmap, LMK04832_REG_SYNC_DIS, 0xff);
812 /* 8. Restore state of SYNC_MODE and SYSREF_MUX to desired values */
813 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYSREF_OUT,
814 LMK04832_BIT_SYSREF_MUX,
815 FIELD_PREP(LMK04832_BIT_SYSREF_MUX,
820 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYNC,
821 LMK04832_BIT_SYNC_MODE,
822 FIELD_PREP(LMK04832_BIT_SYNC_MODE,
828 * 9. (optional) if SCLKx_y_DIS_MODE was used to mute SYSREF outputs
829 * during the SYNC event, restore SCLKx_y_DIS_MODE=0 for active state,
830 * or set SYSREF_GBL_PD=0 if SCLKx_y_DIS_MODE is set to a conditional
835 * 10. (optional) To reduce power consumption, after the synchronization
836 * event is complete, DCLKx_y_DDLY_PD=1 and SYSREF_DDLY_PD=1 disable the
837 * digital delay counters (which are only used immediately after the
838 * SYNC pulse to delay the output by some number of VCO counts).
844 static int lmk04832_sclk_is_enabled(struct clk_hw *hw)
846 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
850 ret = regmap_read(lmk->regmap, LMK04832_REG_MAIN_PD, &tmp);
854 return FIELD_GET(LMK04832_BIT_SYSREF_PD, tmp);
857 static int lmk04832_sclk_prepare(struct clk_hw *hw)
859 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
861 return regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
862 LMK04832_BIT_SYSREF_PD, 0x00);
865 static void lmk04832_sclk_unprepare(struct clk_hw *hw)
867 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
869 regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
870 LMK04832_BIT_SYSREF_PD, LMK04832_BIT_SYSREF_PD);
873 static unsigned long lmk04832_sclk_recalc_rate(struct clk_hw *hw,
876 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
877 unsigned int sysref_div;
881 ret = regmap_bulk_read(lmk->regmap, LMK04832_REG_SYSREF_DIV_MSB, &tmp, 2);
885 sysref_div = FIELD_GET(LMK04832_BIT_SYSREF_DIV_MSB, tmp[0]) << 8 |
888 return DIV_ROUND_CLOSEST(prate, sysref_div);
891 static long lmk04832_sclk_round_rate(struct clk_hw *hw, unsigned long rate,
892 unsigned long *prate)
894 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
895 unsigned long sclk_rate;
896 unsigned int sysref_div;
898 sysref_div = DIV_ROUND_CLOSEST(*prate, rate);
899 sclk_rate = DIV_ROUND_CLOSEST(*prate, sysref_div);
901 if (sysref_div < 0x07 || sysref_div > 0x1fff) {
902 dev_err(lmk->dev, "SYSREF divider out of range\n");
906 if (rate != sclk_rate)
912 static int lmk04832_sclk_set_rate(struct clk_hw *hw, unsigned long rate,
915 struct lmk04832 *lmk = container_of(hw, struct lmk04832, sclk);
916 unsigned int sysref_div;
919 sysref_div = DIV_ROUND_CLOSEST(prate, rate);
921 if (sysref_div < 0x07 || sysref_div > 0x1fff) {
922 dev_err(lmk->dev, "SYSREF divider out of range\n");
926 ret = regmap_write(lmk->regmap, LMK04832_REG_SYSREF_DIV_MSB,
927 FIELD_GET(0x1f00, sysref_div));
931 ret = regmap_write(lmk->regmap, LMK04832_REG_SYSREF_DIV_LSB,
932 FIELD_GET(0x00ff, sysref_div));
936 ret = lmk04832_sclk_sync_sequence(lmk);
938 dev_err(lmk->dev, "SYNC sequence failed\n");
943 static const struct clk_ops lmk04832_sclk_ops = {
944 .is_enabled = lmk04832_sclk_is_enabled,
945 .prepare = lmk04832_sclk_prepare,
946 .unprepare = lmk04832_sclk_unprepare,
947 .recalc_rate = lmk04832_sclk_recalc_rate,
948 .round_rate = lmk04832_sclk_round_rate,
949 .set_rate = lmk04832_sclk_set_rate,
952 static int lmk04832_register_sclk(struct lmk04832 *lmk)
954 const char *parent_names[1];
955 struct clk_init_data init;
958 init.name = "lmk-sclk";
959 parent_names[0] = clk_hw_get_name(&lmk->vco);
960 init.parent_names = parent_names;
962 init.ops = &lmk04832_sclk_ops;
963 init.flags = CLK_SET_RATE_PARENT;
964 init.num_parents = 1;
966 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_SYSREF_OUT,
967 LMK04832_BIT_SYSREF_MUX,
968 FIELD_PREP(LMK04832_BIT_SYSREF_MUX,
973 ret = regmap_write(lmk->regmap, LMK04832_REG_SYSREF_DDLY_LSB,
974 FIELD_GET(0x00ff, lmk->sysref_ddly));
978 ret = regmap_write(lmk->regmap, LMK04832_REG_SYSREF_DDLY_MSB,
979 FIELD_GET(0x1f00, lmk->sysref_ddly));
983 ret = regmap_write(lmk->regmap, LMK04832_REG_SYSREF_PULSE_CNT,
984 ilog2(lmk->sysref_pulse_cnt));
988 ret = regmap_update_bits(lmk->regmap, LMK04832_REG_MAIN_PD,
989 LMK04832_BIT_SYSREF_DDLY_PD |
990 LMK04832_BIT_SYSREF_PLSR_PD,
991 FIELD_PREP(LMK04832_BIT_SYSREF_DDLY_PD, 0) |
992 FIELD_PREP(LMK04832_BIT_SYSREF_PLSR_PD, 0));
996 ret = regmap_write(lmk->regmap, LMK04832_REG_SYNC,
997 FIELD_PREP(LMK04832_BIT_SYNC_POL, 0) |
998 FIELD_PREP(LMK04832_BIT_SYNC_EN, 1) |
999 FIELD_PREP(LMK04832_BIT_SYNC_MODE, lmk->sync_mode));
1003 ret = regmap_write(lmk->regmap, LMK04832_REG_SYNC_DIS, 0xff);
1007 lmk->sclk.init = &init;
1008 return devm_clk_hw_register(lmk->dev, &lmk->sclk);
1011 static int lmk04832_dclk_is_enabled(struct clk_hw *hw)
1013 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1014 struct lmk04832 *lmk = dclk->lmk;
1018 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL3(dclk->id),
1023 return !FIELD_GET(LMK04832_BIT_DCLKX_Y_PD, tmp);
1026 static int lmk04832_dclk_prepare(struct clk_hw *hw)
1028 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1029 struct lmk04832 *lmk = dclk->lmk;
1031 return regmap_update_bits(lmk->regmap,
1032 LMK04832_REG_CLKOUT_CTRL3(dclk->id),
1033 LMK04832_BIT_DCLKX_Y_PD, 0x00);
1036 static void lmk04832_dclk_unprepare(struct clk_hw *hw)
1038 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1039 struct lmk04832 *lmk = dclk->lmk;
1041 regmap_update_bits(lmk->regmap,
1042 LMK04832_REG_CLKOUT_CTRL3(dclk->id),
1043 LMK04832_BIT_DCLKX_Y_PD, 0xff);
1046 static unsigned long lmk04832_dclk_recalc_rate(struct clk_hw *hw,
1047 unsigned long prate)
1049 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1050 struct lmk04832 *lmk = dclk->lmk;
1051 unsigned int dclk_div;
1052 unsigned int lsb, msb;
1056 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL0(dclk->id),
1061 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL2(dclk->id),
1066 dclk_div = FIELD_GET(LMK04832_BIT_DCLK_DIV_MSB, msb) << 8 | lsb;
1067 rate = DIV_ROUND_CLOSEST(prate, dclk_div);
1072 static long lmk04832_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
1073 unsigned long *prate)
1075 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1076 struct lmk04832 *lmk = dclk->lmk;
1077 unsigned long dclk_rate;
1078 unsigned int dclk_div;
1080 dclk_div = DIV_ROUND_CLOSEST(*prate, rate);
1081 dclk_rate = DIV_ROUND_CLOSEST(*prate, dclk_div);
1083 if (dclk_div < 1 || dclk_div > 0x3ff) {
1084 dev_err(lmk->dev, "%s_div out of range\n", clk_hw_get_name(hw));
1088 if (rate != dclk_rate)
1094 static int lmk04832_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
1095 unsigned long prate)
1097 struct lmk_dclk *dclk = container_of(hw, struct lmk_dclk, hw);
1098 struct lmk04832 *lmk = dclk->lmk;
1099 unsigned int dclk_div;
1102 dclk_div = DIV_ROUND_CLOSEST(prate, rate);
1104 if (dclk_div > 0x3ff) {
1105 dev_err(lmk->dev, "%s_div out of range\n", clk_hw_get_name(hw));
1109 /* Enable Duty Cycle Correction */
1110 if (dclk_div == 1) {
1111 ret = regmap_update_bits(lmk->regmap,
1112 LMK04832_REG_CLKOUT_CTRL3(dclk->id),
1113 LMK04832_BIT_DCLKX_Y_DCC,
1114 FIELD_PREP(LMK04832_BIT_DCLKX_Y_DCC, 1));
1120 * While using Divide-by-2 or Divide-by-3 for DCLK_X_Y_DIV, SYNC
1121 * procedure requires to first program Divide-by-4 and then back to
1122 * Divide-by-2 or Divide-by-3 before doing SYNC.
1124 if (dclk_div == 2 || dclk_div == 3) {
1125 ret = regmap_update_bits(lmk->regmap,
1126 LMK04832_REG_CLKOUT_CTRL2(dclk->id),
1127 LMK04832_BIT_DCLK_DIV_MSB, 0x00);
1131 ret = regmap_write(lmk->regmap,
1132 LMK04832_REG_CLKOUT_CTRL0(dclk->id), 0x04);
1137 ret = regmap_write(lmk->regmap, LMK04832_REG_CLKOUT_CTRL0(dclk->id),
1138 FIELD_GET(0x0ff, dclk_div));
1142 ret = regmap_update_bits(lmk->regmap,
1143 LMK04832_REG_CLKOUT_CTRL2(dclk->id),
1144 LMK04832_BIT_DCLK_DIV_MSB,
1145 FIELD_GET(0x300, dclk_div));
1149 ret = lmk04832_sclk_sync_sequence(lmk);
1151 dev_err(lmk->dev, "SYNC sequence failed\n");
1156 static const struct clk_ops lmk04832_dclk_ops = {
1157 .is_enabled = lmk04832_dclk_is_enabled,
1158 .prepare = lmk04832_dclk_prepare,
1159 .unprepare = lmk04832_dclk_unprepare,
1160 .recalc_rate = lmk04832_dclk_recalc_rate,
1161 .round_rate = lmk04832_dclk_round_rate,
1162 .set_rate = lmk04832_dclk_set_rate,
1165 static int lmk04832_clkout_is_enabled(struct clk_hw *hw)
1167 struct lmk_clkout *clkout = container_of(hw, struct lmk_clkout, hw);
1168 struct lmk04832 *lmk = clkout->lmk;
1169 unsigned int clkoutx_y_pd;
1170 unsigned int sclkx_y_pd;
1176 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_CTRL2(clkout->id),
1181 enabled = !FIELD_GET(LMK04832_BIT_CLKOUTX_Y_PD, clkoutx_y_pd);
1183 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_SRC_MUX(clkout->id),
1188 if (FIELD_GET(LMK04832_BIT_CLKOUT_SRC_MUX, tmp)) {
1189 ret = regmap_read(lmk->regmap,
1190 LMK04832_REG_CLKOUT_CTRL4(clkout->id),
1195 enabled = enabled && !FIELD_GET(LMK04832_BIT_SCLK_PD, sclkx_y_pd);
1198 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_FMT(clkout->id),
1204 fmt = FIELD_GET(0xf0, tmp);
1206 fmt = FIELD_GET(0x0f, tmp);
1208 return enabled && !fmt;
1211 static int lmk04832_clkout_prepare(struct clk_hw *hw)
1213 struct lmk_clkout *clkout = container_of(hw, struct lmk_clkout, hw);
1214 struct lmk04832 *lmk = clkout->lmk;
1218 if (clkout->format == LMK04832_VAL_CLKOUT_FMT_POWERDOWN)
1219 dev_err(lmk->dev, "prepared %s but format is powerdown\n",
1220 clk_hw_get_name(hw));
1222 ret = regmap_update_bits(lmk->regmap,
1223 LMK04832_REG_CLKOUT_CTRL2(clkout->id),
1224 LMK04832_BIT_CLKOUTX_Y_PD, 0x00);
1228 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_SRC_MUX(clkout->id),
1233 if (FIELD_GET(LMK04832_BIT_CLKOUT_SRC_MUX, tmp)) {
1234 ret = regmap_update_bits(lmk->regmap,
1235 LMK04832_REG_CLKOUT_CTRL4(clkout->id),
1236 LMK04832_BIT_SCLK_PD, 0x00);
1241 return regmap_update_bits(lmk->regmap,
1242 LMK04832_REG_CLKOUT_FMT(clkout->id),
1243 LMK04832_BIT_CLKOUT_FMT(clkout->id),
1244 clkout->format << 4 * (clkout->id % 2));
1247 static void lmk04832_clkout_unprepare(struct clk_hw *hw)
1249 struct lmk_clkout *clkout = container_of(hw, struct lmk_clkout, hw);
1250 struct lmk04832 *lmk = clkout->lmk;
1252 regmap_update_bits(lmk->regmap, LMK04832_REG_CLKOUT_FMT(clkout->id),
1253 LMK04832_BIT_CLKOUT_FMT(clkout->id),
1257 static int lmk04832_clkout_set_parent(struct clk_hw *hw, uint8_t index)
1259 struct lmk_clkout *clkout = container_of(hw, struct lmk_clkout, hw);
1260 struct lmk04832 *lmk = clkout->lmk;
1262 return regmap_update_bits(lmk->regmap,
1263 LMK04832_REG_CLKOUT_SRC_MUX(clkout->id),
1264 LMK04832_BIT_CLKOUT_SRC_MUX,
1265 FIELD_PREP(LMK04832_BIT_CLKOUT_SRC_MUX,
1269 static uint8_t lmk04832_clkout_get_parent(struct clk_hw *hw)
1271 struct lmk_clkout *clkout = container_of(hw, struct lmk_clkout, hw);
1272 struct lmk04832 *lmk = clkout->lmk;
1276 ret = regmap_read(lmk->regmap, LMK04832_REG_CLKOUT_SRC_MUX(clkout->id),
1281 return FIELD_GET(LMK04832_BIT_CLKOUT_SRC_MUX, tmp);
1284 static const struct clk_ops lmk04832_clkout_ops = {
1285 .is_enabled = lmk04832_clkout_is_enabled,
1286 .prepare = lmk04832_clkout_prepare,
1287 .unprepare = lmk04832_clkout_unprepare,
1288 .determine_rate = __clk_mux_determine_rate,
1289 .set_parent = lmk04832_clkout_set_parent,
1290 .get_parent = lmk04832_clkout_get_parent,
1293 static int lmk04832_register_clkout(struct lmk04832 *lmk, const int num)
1295 char name[] = "lmk-clkoutXX";
1296 char dclk_name[] = "lmk-dclkXX_YY";
1297 const char *parent_names[2];
1298 struct clk_init_data init;
1299 int dclk_num = num / 2;
1303 sprintf(dclk_name, "lmk-dclk%02d_%02d", num, num + 1);
1304 init.name = dclk_name;
1305 parent_names[0] = clk_hw_get_name(&lmk->vco);
1306 init.parent_names = parent_names;
1307 init.ops = &lmk04832_dclk_ops;
1308 init.flags = CLK_SET_RATE_PARENT;
1309 init.num_parents = 1;
1311 lmk->dclk[dclk_num].id = num;
1312 lmk->dclk[dclk_num].lmk = lmk;
1313 lmk->dclk[dclk_num].hw.init = &init;
1315 ret = devm_clk_hw_register(lmk->dev, &lmk->dclk[dclk_num].hw);
1319 sprintf(dclk_name, "lmk-dclk%02d_%02d", num - 1, num);
1322 if (of_property_read_string_index(lmk->dev->of_node,
1323 "clock-output-names",
1325 sprintf(name, "lmk-clkout%02d", num);
1329 parent_names[0] = dclk_name;
1330 parent_names[1] = clk_hw_get_name(&lmk->sclk);
1331 init.parent_names = parent_names;
1332 init.ops = &lmk04832_clkout_ops;
1333 init.flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT;
1334 init.num_parents = ARRAY_SIZE(parent_names);
1336 lmk->clkout[num].id = num;
1337 lmk->clkout[num].lmk = lmk;
1338 lmk->clkout[num].hw.init = &init;
1339 lmk->clk_data->hws[num] = &lmk->clkout[num].hw;
1341 /* Set initial parent */
1342 regmap_update_bits(lmk->regmap,
1343 LMK04832_REG_CLKOUT_SRC_MUX(num),
1344 LMK04832_BIT_CLKOUT_SRC_MUX,
1345 FIELD_PREP(LMK04832_BIT_CLKOUT_SRC_MUX,
1346 lmk->clkout[num].sysref));
1348 return devm_clk_hw_register(lmk->dev, &lmk->clkout[num].hw);
1351 static int lmk04832_set_spi_rdbk(const struct lmk04832 *lmk, const int rdbk_pin)
1355 int val = FIELD_PREP(LMK04832_BIT_CLKIN_SEL_MUX,
1356 LMK04832_VAL_CLKIN_SEL_MUX_SPI_RDBK) |
1357 FIELD_PREP(LMK04832_BIT_CLKIN_SEL_TYPE,
1358 LMK04832_VAL_CLKIN_SEL_TYPE_OUT);
1360 dev_info(lmk->dev, "setting up 4-wire mode\n");
1361 ret = regmap_write(lmk->regmap, LMK04832_REG_RST3W,
1362 LMK04832_BIT_SPI_3WIRE_DIS);
1367 case RDBK_CLKIN_SEL0:
1368 reg = LMK04832_REG_CLKIN_SEL0;
1370 case RDBK_CLKIN_SEL1:
1371 reg = LMK04832_REG_CLKIN_SEL1;
1374 reg = LMK04832_REG_CLKIN_RST;
1377 reg = LMK04832_REG_PLL1_LD;
1378 val = FIELD_PREP(LMK04832_BIT_PLL1_LD_MUX,
1379 LMK04832_VAL_PLL1_LD_MUX_SPI_RDBK) |
1380 FIELD_PREP(LMK04832_BIT_PLL1_LD_TYPE,
1381 LMK04832_VAL_PLL1_LD_TYPE_OUT_PP);
1387 return regmap_write(lmk->regmap, reg, val);
1390 static int lmk04832_probe(struct spi_device *spi)
1392 const struct lmk04832_device_info *info;
1393 int rdbk_pin = RDBK_CLKIN_SEL1;
1394 struct device_node *child;
1395 struct lmk04832 *lmk;
1400 info = &lmk04832_device_info[spi_get_device_id(spi)->driver_data];
1402 lmk = devm_kzalloc(&spi->dev, sizeof(struct lmk04832), GFP_KERNEL);
1406 lmk->dev = &spi->dev;
1408 lmk->oscin = devm_clk_get(lmk->dev, "oscin");
1409 if (IS_ERR(lmk->oscin)) {
1410 dev_err(lmk->dev, "failed to get oscin clock\n");
1411 return PTR_ERR(lmk->oscin);
1414 ret = clk_prepare_enable(lmk->oscin);
1418 lmk->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset",
1421 lmk->dclk = devm_kcalloc(lmk->dev, info->num_channels >> 1,
1422 sizeof(struct lmk_dclk), GFP_KERNEL);
1425 goto err_disable_oscin;
1428 lmk->clkout = devm_kcalloc(lmk->dev, info->num_channels,
1429 sizeof(*lmk->clkout), GFP_KERNEL);
1432 goto err_disable_oscin;
1435 lmk->clk_data = devm_kzalloc(lmk->dev, struct_size(lmk->clk_data, hws,
1436 info->num_channels),
1438 if (!lmk->clk_data) {
1440 goto err_disable_oscin;
1443 device_property_read_u32(lmk->dev, "ti,vco-hz", &lmk->vco_rate);
1445 lmk->sysref_ddly = 8;
1446 device_property_read_u32(lmk->dev, "ti,sysref-ddly", &lmk->sysref_ddly);
1448 lmk->sysref_mux = LMK04832_VAL_SYSREF_MUX_CONTINUOUS;
1449 device_property_read_u32(lmk->dev, "ti,sysref-mux",
1452 lmk->sync_mode = LMK04832_VAL_SYNC_MODE_OFF;
1453 device_property_read_u32(lmk->dev, "ti,sync-mode",
1456 lmk->sysref_pulse_cnt = 4;
1457 device_property_read_u32(lmk->dev, "ti,sysref-pulse-count",
1458 &lmk->sysref_pulse_cnt);
1460 for_each_child_of_node(lmk->dev->of_node, child) {
1463 ret = of_property_read_u32(child, "reg", ®);
1465 dev_err(lmk->dev, "missing reg property in child: %s\n",
1468 goto err_disable_oscin;
1471 of_property_read_u32(child, "ti,clkout-fmt",
1472 &lmk->clkout[reg].format);
1474 if (lmk->clkout[reg].format >= 0x0a && reg % 2 == 0
1475 && reg != 8 && reg != 10)
1476 dev_err(lmk->dev, "invalid format for clkout%02d\n",
1479 lmk->clkout[reg].sysref =
1480 of_property_read_bool(child, "ti,clkout-sysref");
1483 lmk->regmap = devm_regmap_init_spi(spi, ®map_config);
1484 if (IS_ERR(lmk->regmap)) {
1485 dev_err(lmk->dev, "%s: regmap allocation failed: %ld\n",
1487 __func__, PTR_ERR(lmk->regmap));
1488 ret = PTR_ERR(lmk->regmap);
1489 goto err_disable_oscin;
1492 regmap_write(lmk->regmap, LMK04832_REG_RST3W, LMK04832_BIT_RESET);
1494 if (!(spi->mode & SPI_3WIRE)) {
1495 device_property_read_u32(lmk->dev, "ti,spi-4wire-rdbk",
1497 ret = lmk04832_set_spi_rdbk(lmk, rdbk_pin);
1499 goto err_disable_oscin;
1502 regmap_bulk_read(lmk->regmap, LMK04832_REG_ID_PROD_MSB, &tmp, 3);
1503 if ((tmp[0] << 8 | tmp[1]) != info->pid || tmp[2] != info->maskrev) {
1504 dev_err(lmk->dev, "unsupported device type: pid 0x%04x, maskrev 0x%02x\n",
1505 tmp[0] << 8 | tmp[1], tmp[2]);
1507 goto err_disable_oscin;
1510 ret = lmk04832_register_vco(lmk);
1512 dev_err(lmk->dev, "failed to init device clock path\n");
1513 goto err_disable_oscin;
1516 if (lmk->vco_rate) {
1517 dev_info(lmk->dev, "setting VCO rate to %u Hz\n", lmk->vco_rate);
1518 ret = clk_set_rate(lmk->vco.clk, lmk->vco_rate);
1520 dev_err(lmk->dev, "failed to set VCO rate\n");
1521 goto err_disable_oscin;
1525 ret = lmk04832_register_sclk(lmk);
1527 dev_err(lmk->dev, "failed to init SYNC/SYSREF clock path\n");
1528 goto err_disable_oscin;
1531 for (i = 0; i < info->num_channels; i++) {
1532 ret = lmk04832_register_clkout(lmk, i);
1534 dev_err(lmk->dev, "failed to register clk %d\n", i);
1535 goto err_disable_oscin;
1539 lmk->clk_data->num = info->num_channels;
1540 ret = devm_of_clk_add_hw_provider(lmk->dev, of_clk_hw_onecell_get,
1543 dev_err(lmk->dev, "failed to add provider (%d)\n", ret);
1544 goto err_disable_oscin;
1547 spi_set_drvdata(spi, lmk);
1552 clk_disable_unprepare(lmk->oscin);
1557 static void lmk04832_remove(struct spi_device *spi)
1559 struct lmk04832 *lmk = spi_get_drvdata(spi);
1561 clk_disable_unprepare(lmk->oscin);
1564 static const struct spi_device_id lmk04832_id[] = {
1565 { "lmk04832", LMK04832 },
1568 MODULE_DEVICE_TABLE(spi, lmk04832_id);
1570 static const struct of_device_id lmk04832_of_id[] = {
1571 { .compatible = "ti,lmk04832" },
1574 MODULE_DEVICE_TABLE(of, lmk04832_of_id);
1576 static struct spi_driver lmk04832_driver = {
1579 .of_match_table = lmk04832_of_id,
1581 .probe = lmk04832_probe,
1582 .remove = lmk04832_remove,
1583 .id_table = lmk04832_id,
1585 module_spi_driver(lmk04832_driver);
1587 MODULE_AUTHOR("Liam Beguin <lvb@xiphos.com>");
1588 MODULE_DESCRIPTION("Texas Instruments LMK04832");
1589 MODULE_LICENSE("GPL v2");