xref: /openbmc/linux/drivers/clk/clk-si5341.c (revision 95298d63)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Silicon Labs Si5340, Si5341, Si5342, Si5344 and Si5345
4  * Copyright (C) 2019 Topic Embedded Products
5  * Author: Mike Looijmans <mike.looijmans@topic.nl>
6  *
7  * The Si5341 has 10 outputs and 5 synthesizers.
8  * The Si5340 is a smaller version of the Si5341 with only 4 outputs.
9  * The Si5345 is similar to the Si5341, with the addition of fractional input
10  * dividers and automatic input selection.
11  * The Si5342 and Si5344 are smaller versions of the Si5345.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/delay.h>
17 #include <linux/gcd.h>
18 #include <linux/math64.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <asm/unaligned.h>
24 
25 #define SI5341_NUM_INPUTS 4
26 
27 #define SI5340_MAX_NUM_OUTPUTS 4
28 #define SI5341_MAX_NUM_OUTPUTS 10
29 #define SI5342_MAX_NUM_OUTPUTS 2
30 #define SI5344_MAX_NUM_OUTPUTS 4
31 #define SI5345_MAX_NUM_OUTPUTS 10
32 
33 #define SI5340_NUM_SYNTH 4
34 #define SI5341_NUM_SYNTH 5
35 #define SI5342_NUM_SYNTH 2
36 #define SI5344_NUM_SYNTH 4
37 #define SI5345_NUM_SYNTH 5
38 
39 /* Range of the synthesizer fractional divider */
40 #define SI5341_SYNTH_N_MIN	10
41 #define SI5341_SYNTH_N_MAX	4095
42 
43 /* The chip can get its input clock from 3 input pins or an XTAL */
44 
45 /* There is one PLL running at 13500–14256 MHz */
46 #define SI5341_PLL_VCO_MIN 13500000000ull
47 #define SI5341_PLL_VCO_MAX 14256000000ull
48 
49 /* The 5 frequency synthesizers obtain their input from the PLL */
50 struct clk_si5341_synth {
51 	struct clk_hw hw;
52 	struct clk_si5341 *data;
53 	u8 index;
54 };
55 #define to_clk_si5341_synth(_hw) \
56 	container_of(_hw, struct clk_si5341_synth, hw)
57 
58 /* The output stages can be connected to any synth (full mux) */
59 struct clk_si5341_output {
60 	struct clk_hw hw;
61 	struct clk_si5341 *data;
62 	u8 index;
63 };
64 #define to_clk_si5341_output(_hw) \
65 	container_of(_hw, struct clk_si5341_output, hw)
66 
67 struct clk_si5341 {
68 	struct clk_hw hw;
69 	struct regmap *regmap;
70 	struct i2c_client *i2c_client;
71 	struct clk_si5341_synth synth[SI5341_NUM_SYNTH];
72 	struct clk_si5341_output clk[SI5341_MAX_NUM_OUTPUTS];
73 	struct clk *input_clk[SI5341_NUM_INPUTS];
74 	const char *input_clk_name[SI5341_NUM_INPUTS];
75 	const u16 *reg_output_offset;
76 	const u16 *reg_rdiv_offset;
77 	u64 freq_vco; /* 13500–14256 MHz */
78 	u8 num_outputs;
79 	u8 num_synth;
80 	u16 chip_id;
81 };
82 #define to_clk_si5341(_hw)	container_of(_hw, struct clk_si5341, hw)
83 
84 struct clk_si5341_output_config {
85 	u8 out_format_drv_bits;
86 	u8 out_cm_ampl_bits;
87 	bool synth_master;
88 	bool always_on;
89 };
90 
91 #define SI5341_PAGE		0x0001
92 #define SI5341_PN_BASE		0x0002
93 #define SI5341_DEVICE_REV	0x0005
94 #define SI5341_STATUS		0x000C
95 #define SI5341_SOFT_RST		0x001C
96 #define SI5341_IN_SEL		0x0021
97 #define SI5341_XAXB_CFG		0x090E
98 #define SI5341_IN_EN		0x0949
99 #define SI5341_INX_TO_PFD_EN	0x094A
100 
101 /* Input selection */
102 #define SI5341_IN_SEL_MASK	0x06
103 #define SI5341_IN_SEL_SHIFT	1
104 #define SI5341_IN_SEL_REGCTRL	0x01
105 #define SI5341_INX_TO_PFD_SHIFT	4
106 
107 /* XTAL config bits */
108 #define SI5341_XAXB_CFG_EXTCLK_EN	BIT(0)
109 #define SI5341_XAXB_CFG_PDNB		BIT(1)
110 
111 /* Input dividers (48-bit) */
112 #define SI5341_IN_PDIV(x)	(0x0208 + ((x) * 10))
113 #define SI5341_IN_PSET(x)	(0x020E + ((x) * 10))
114 #define SI5341_PX_UPD		0x0230
115 
116 /* PLL configuration */
117 #define SI5341_PLL_M_NUM	0x0235
118 #define SI5341_PLL_M_DEN	0x023B
119 
120 /* Output configuration */
121 #define SI5341_OUT_CONFIG(output)	\
122 			((output)->data->reg_output_offset[(output)->index])
123 #define SI5341_OUT_FORMAT(output)	(SI5341_OUT_CONFIG(output) + 1)
124 #define SI5341_OUT_CM(output)		(SI5341_OUT_CONFIG(output) + 2)
125 #define SI5341_OUT_MUX_SEL(output)	(SI5341_OUT_CONFIG(output) + 3)
126 #define SI5341_OUT_R_REG(output)	\
127 			((output)->data->reg_rdiv_offset[(output)->index])
128 
129 /* Synthesize N divider */
130 #define SI5341_SYNTH_N_NUM(x)	(0x0302 + ((x) * 11))
131 #define SI5341_SYNTH_N_DEN(x)	(0x0308 + ((x) * 11))
132 #define SI5341_SYNTH_N_UPD(x)	(0x030C + ((x) * 11))
133 
134 /* Synthesizer output enable, phase bypass, power mode */
135 #define SI5341_SYNTH_N_CLK_TO_OUTX_EN	0x0A03
136 #define SI5341_SYNTH_N_PIBYP		0x0A04
137 #define SI5341_SYNTH_N_PDNB		0x0A05
138 #define SI5341_SYNTH_N_CLK_DIS		0x0B4A
139 
140 #define SI5341_REGISTER_MAX	0xBFF
141 
142 /* SI5341_OUT_CONFIG bits */
143 #define SI5341_OUT_CFG_PDN		BIT(0)
144 #define SI5341_OUT_CFG_OE		BIT(1)
145 #define SI5341_OUT_CFG_RDIV_FORCE2	BIT(2)
146 
147 /* Static configuration (to be moved to firmware) */
148 struct si5341_reg_default {
149 	u16 address;
150 	u8 value;
151 };
152 
153 static const char * const si5341_input_clock_names[] = {
154 	"in0", "in1", "in2", "xtal"
155 };
156 
157 /* Output configuration registers 0..9 are not quite logically organized */
158 /* Also for si5345 */
159 static const u16 si5341_reg_output_offset[] = {
160 	0x0108,
161 	0x010D,
162 	0x0112,
163 	0x0117,
164 	0x011C,
165 	0x0121,
166 	0x0126,
167 	0x012B,
168 	0x0130,
169 	0x013A,
170 };
171 
172 /* for si5340, si5342 and si5344 */
173 static const u16 si5340_reg_output_offset[] = {
174 	0x0112,
175 	0x0117,
176 	0x0126,
177 	0x012B,
178 };
179 
180 /* The location of the R divider registers */
181 static const u16 si5341_reg_rdiv_offset[] = {
182 	0x024A,
183 	0x024D,
184 	0x0250,
185 	0x0253,
186 	0x0256,
187 	0x0259,
188 	0x025C,
189 	0x025F,
190 	0x0262,
191 	0x0268,
192 };
193 static const u16 si5340_reg_rdiv_offset[] = {
194 	0x0250,
195 	0x0253,
196 	0x025C,
197 	0x025F,
198 };
199 
200 /*
201  * Programming sequence from ClockBuilder, settings to initialize the system
202  * using only the XTAL input, without pre-divider.
203  * This also contains settings that aren't mentioned anywhere in the datasheet.
204  * The "known" settings like synth and output configuration are done later.
205  */
206 static const struct si5341_reg_default si5341_reg_defaults[] = {
207 	{ 0x0017, 0x3A }, /* INT mask (disable interrupts) */
208 	{ 0x0018, 0xFF }, /* INT mask */
209 	{ 0x0021, 0x0F }, /* Select XTAL as input */
210 	{ 0x0022, 0x00 }, /* Not in datasheet */
211 	{ 0x002B, 0x02 }, /* SPI config */
212 	{ 0x002C, 0x20 }, /* LOS enable for XTAL */
213 	{ 0x002D, 0x00 }, /* LOS timing */
214 	{ 0x002E, 0x00 },
215 	{ 0x002F, 0x00 },
216 	{ 0x0030, 0x00 },
217 	{ 0x0031, 0x00 },
218 	{ 0x0032, 0x00 },
219 	{ 0x0033, 0x00 },
220 	{ 0x0034, 0x00 },
221 	{ 0x0035, 0x00 },
222 	{ 0x0036, 0x00 },
223 	{ 0x0037, 0x00 },
224 	{ 0x0038, 0x00 }, /* LOS setting (thresholds) */
225 	{ 0x0039, 0x00 },
226 	{ 0x003A, 0x00 },
227 	{ 0x003B, 0x00 },
228 	{ 0x003C, 0x00 },
229 	{ 0x003D, 0x00 }, /* LOS setting (thresholds) end */
230 	{ 0x0041, 0x00 }, /* LOS0_DIV_SEL */
231 	{ 0x0042, 0x00 }, /* LOS1_DIV_SEL */
232 	{ 0x0043, 0x00 }, /* LOS2_DIV_SEL */
233 	{ 0x0044, 0x00 }, /* LOS3_DIV_SEL */
234 	{ 0x009E, 0x00 }, /* Not in datasheet */
235 	{ 0x0102, 0x01 }, /* Enable outputs */
236 	{ 0x013F, 0x00 }, /* Not in datasheet */
237 	{ 0x0140, 0x00 }, /* Not in datasheet */
238 	{ 0x0141, 0x40 }, /* OUT LOS */
239 	{ 0x0202, 0x00 }, /* XAXB_FREQ_OFFSET (=0)*/
240 	{ 0x0203, 0x00 },
241 	{ 0x0204, 0x00 },
242 	{ 0x0205, 0x00 },
243 	{ 0x0206, 0x00 }, /* PXAXB (2^x) */
244 	{ 0x0208, 0x00 }, /* Px divider setting (usually 0) */
245 	{ 0x0209, 0x00 },
246 	{ 0x020A, 0x00 },
247 	{ 0x020B, 0x00 },
248 	{ 0x020C, 0x00 },
249 	{ 0x020D, 0x00 },
250 	{ 0x020E, 0x00 },
251 	{ 0x020F, 0x00 },
252 	{ 0x0210, 0x00 },
253 	{ 0x0211, 0x00 },
254 	{ 0x0212, 0x00 },
255 	{ 0x0213, 0x00 },
256 	{ 0x0214, 0x00 },
257 	{ 0x0215, 0x00 },
258 	{ 0x0216, 0x00 },
259 	{ 0x0217, 0x00 },
260 	{ 0x0218, 0x00 },
261 	{ 0x0219, 0x00 },
262 	{ 0x021A, 0x00 },
263 	{ 0x021B, 0x00 },
264 	{ 0x021C, 0x00 },
265 	{ 0x021D, 0x00 },
266 	{ 0x021E, 0x00 },
267 	{ 0x021F, 0x00 },
268 	{ 0x0220, 0x00 },
269 	{ 0x0221, 0x00 },
270 	{ 0x0222, 0x00 },
271 	{ 0x0223, 0x00 },
272 	{ 0x0224, 0x00 },
273 	{ 0x0225, 0x00 },
274 	{ 0x0226, 0x00 },
275 	{ 0x0227, 0x00 },
276 	{ 0x0228, 0x00 },
277 	{ 0x0229, 0x00 },
278 	{ 0x022A, 0x00 },
279 	{ 0x022B, 0x00 },
280 	{ 0x022C, 0x00 },
281 	{ 0x022D, 0x00 },
282 	{ 0x022E, 0x00 },
283 	{ 0x022F, 0x00 }, /* Px divider setting (usually 0) end */
284 	{ 0x026B, 0x00 }, /* DESIGN_ID (ASCII string) */
285 	{ 0x026C, 0x00 },
286 	{ 0x026D, 0x00 },
287 	{ 0x026E, 0x00 },
288 	{ 0x026F, 0x00 },
289 	{ 0x0270, 0x00 },
290 	{ 0x0271, 0x00 },
291 	{ 0x0272, 0x00 }, /* DESIGN_ID (ASCII string) end */
292 	{ 0x0339, 0x1F }, /* N_FSTEP_MSK */
293 	{ 0x033B, 0x00 }, /* Nx_FSTEPW (Frequency step) */
294 	{ 0x033C, 0x00 },
295 	{ 0x033D, 0x00 },
296 	{ 0x033E, 0x00 },
297 	{ 0x033F, 0x00 },
298 	{ 0x0340, 0x00 },
299 	{ 0x0341, 0x00 },
300 	{ 0x0342, 0x00 },
301 	{ 0x0343, 0x00 },
302 	{ 0x0344, 0x00 },
303 	{ 0x0345, 0x00 },
304 	{ 0x0346, 0x00 },
305 	{ 0x0347, 0x00 },
306 	{ 0x0348, 0x00 },
307 	{ 0x0349, 0x00 },
308 	{ 0x034A, 0x00 },
309 	{ 0x034B, 0x00 },
310 	{ 0x034C, 0x00 },
311 	{ 0x034D, 0x00 },
312 	{ 0x034E, 0x00 },
313 	{ 0x034F, 0x00 },
314 	{ 0x0350, 0x00 },
315 	{ 0x0351, 0x00 },
316 	{ 0x0352, 0x00 },
317 	{ 0x0353, 0x00 },
318 	{ 0x0354, 0x00 },
319 	{ 0x0355, 0x00 },
320 	{ 0x0356, 0x00 },
321 	{ 0x0357, 0x00 },
322 	{ 0x0358, 0x00 }, /* Nx_FSTEPW (Frequency step) end */
323 	{ 0x0359, 0x00 }, /* Nx_DELAY */
324 	{ 0x035A, 0x00 },
325 	{ 0x035B, 0x00 },
326 	{ 0x035C, 0x00 },
327 	{ 0x035D, 0x00 },
328 	{ 0x035E, 0x00 },
329 	{ 0x035F, 0x00 },
330 	{ 0x0360, 0x00 },
331 	{ 0x0361, 0x00 },
332 	{ 0x0362, 0x00 }, /* Nx_DELAY end */
333 	{ 0x0802, 0x00 }, /* Not in datasheet */
334 	{ 0x0803, 0x00 }, /* Not in datasheet */
335 	{ 0x0804, 0x00 }, /* Not in datasheet */
336 	{ 0x090E, 0x02 }, /* XAXB_EXTCLK_EN=0 XAXB_PDNB=1 (use XTAL) */
337 	{ 0x091C, 0x04 }, /* ZDM_EN=4 (Normal mode) */
338 	{ 0x0943, 0x00 }, /* IO_VDD_SEL=0 (0=1v8, use 1=3v3) */
339 	{ 0x0949, 0x00 }, /* IN_EN (disable input clocks) */
340 	{ 0x094A, 0x00 }, /* INx_TO_PFD_EN (disabled) */
341 	{ 0x0A02, 0x00 }, /* Not in datasheet */
342 	{ 0x0B44, 0x0F }, /* PDIV_ENB (datasheet does not mention what it is) */
343 };
344 
345 /* Read and interpret a 44-bit followed by a 32-bit value in the regmap */
346 static int si5341_decode_44_32(struct regmap *regmap, unsigned int reg,
347 	u64 *val1, u32 *val2)
348 {
349 	int err;
350 	u8 r[10];
351 
352 	err = regmap_bulk_read(regmap, reg, r, 10);
353 	if (err < 0)
354 		return err;
355 
356 	*val1 = ((u64)((r[5] & 0x0f) << 8 | r[4]) << 32) |
357 		 (get_unaligned_le32(r));
358 	*val2 = get_unaligned_le32(&r[6]);
359 
360 	return 0;
361 }
362 
363 static int si5341_encode_44_32(struct regmap *regmap, unsigned int reg,
364 	u64 n_num, u32 n_den)
365 {
366 	u8 r[10];
367 
368 	/* Shift left as far as possible without overflowing */
369 	while (!(n_num & BIT_ULL(43)) && !(n_den & BIT(31))) {
370 		n_num <<= 1;
371 		n_den <<= 1;
372 	}
373 
374 	/* 44 bits (6 bytes) numerator */
375 	put_unaligned_le32(n_num, r);
376 	r[4] = (n_num >> 32) & 0xff;
377 	r[5] = (n_num >> 40) & 0x0f;
378 	/* 32 bits denominator */
379 	put_unaligned_le32(n_den, &r[6]);
380 
381 	/* Program the fraction */
382 	return regmap_bulk_write(regmap, reg, r, sizeof(r));
383 }
384 
385 /* VCO, we assume it runs at a constant frequency */
386 static unsigned long si5341_clk_recalc_rate(struct clk_hw *hw,
387 		unsigned long parent_rate)
388 {
389 	struct clk_si5341 *data = to_clk_si5341(hw);
390 	int err;
391 	u64 res;
392 	u64 m_num;
393 	u32 m_den;
394 	unsigned int shift;
395 
396 	/* Assume that PDIV is not being used, just read the PLL setting */
397 	err = si5341_decode_44_32(data->regmap, SI5341_PLL_M_NUM,
398 				&m_num, &m_den);
399 	if (err < 0)
400 		return 0;
401 
402 	if (!m_num || !m_den)
403 		return 0;
404 
405 	/*
406 	 * Though m_num is 64-bit, only the upper bits are actually used. While
407 	 * calculating m_num and m_den, they are shifted as far as possible to
408 	 * the left. To avoid 96-bit division here, we just shift them back so
409 	 * we can do with just 64 bits.
410 	 */
411 	shift = 0;
412 	res = m_num;
413 	while (res & 0xffff00000000ULL) {
414 		++shift;
415 		res >>= 1;
416 	}
417 	res *= parent_rate;
418 	do_div(res, (m_den >> shift));
419 
420 	/* We cannot return the actual frequency in 32 bit, store it locally */
421 	data->freq_vco = res;
422 
423 	/* Report kHz since the value is out of range */
424 	do_div(res, 1000);
425 
426 	return (unsigned long)res;
427 }
428 
429 static int si5341_clk_get_selected_input(struct clk_si5341 *data)
430 {
431 	int err;
432 	u32 val;
433 
434 	err = regmap_read(data->regmap, SI5341_IN_SEL, &val);
435 	if (err < 0)
436 		return err;
437 
438 	return (val & SI5341_IN_SEL_MASK) >> SI5341_IN_SEL_SHIFT;
439 }
440 
441 static u8 si5341_clk_get_parent(struct clk_hw *hw)
442 {
443 	struct clk_si5341 *data = to_clk_si5341(hw);
444 	int res = si5341_clk_get_selected_input(data);
445 
446 	if (res < 0)
447 		return 0; /* Apparently we cannot report errors */
448 
449 	return res;
450 }
451 
452 static int si5341_clk_reparent(struct clk_si5341 *data, u8 index)
453 {
454 	int err;
455 	u8 val;
456 
457 	val = (index << SI5341_IN_SEL_SHIFT) & SI5341_IN_SEL_MASK;
458 	/* Enable register-based input selection */
459 	val |= SI5341_IN_SEL_REGCTRL;
460 
461 	err = regmap_update_bits(data->regmap,
462 		SI5341_IN_SEL, SI5341_IN_SEL_REGCTRL | SI5341_IN_SEL_MASK, val);
463 	if (err < 0)
464 		return err;
465 
466 	if (index < 3) {
467 		/* Enable input buffer for selected input */
468 		err = regmap_update_bits(data->regmap,
469 				SI5341_IN_EN, 0x07, BIT(index));
470 		if (err < 0)
471 			return err;
472 
473 		/* Enables the input to phase detector */
474 		err = regmap_update_bits(data->regmap, SI5341_INX_TO_PFD_EN,
475 				0x7 << SI5341_INX_TO_PFD_SHIFT,
476 				BIT(index + SI5341_INX_TO_PFD_SHIFT));
477 		if (err < 0)
478 			return err;
479 
480 		/* Power down XTAL oscillator and buffer */
481 		err = regmap_update_bits(data->regmap, SI5341_XAXB_CFG,
482 				SI5341_XAXB_CFG_PDNB, 0);
483 		if (err < 0)
484 			return err;
485 
486 		/*
487 		 * Set the P divider to "1". There's no explanation in the
488 		 * datasheet of these registers, but the clockbuilder software
489 		 * programs a "1" when the input is being used.
490 		 */
491 		err = regmap_write(data->regmap, SI5341_IN_PDIV(index), 1);
492 		if (err < 0)
493 			return err;
494 
495 		err = regmap_write(data->regmap, SI5341_IN_PSET(index), 1);
496 		if (err < 0)
497 			return err;
498 
499 		/* Set update PDIV bit */
500 		err = regmap_write(data->regmap, SI5341_PX_UPD, BIT(index));
501 		if (err < 0)
502 			return err;
503 	} else {
504 		/* Disable all input buffers */
505 		err = regmap_update_bits(data->regmap, SI5341_IN_EN, 0x07, 0);
506 		if (err < 0)
507 			return err;
508 
509 		/* Disable input to phase detector */
510 		err = regmap_update_bits(data->regmap, SI5341_INX_TO_PFD_EN,
511 				0x7 << SI5341_INX_TO_PFD_SHIFT, 0);
512 		if (err < 0)
513 			return err;
514 
515 		/* Power up XTAL oscillator and buffer */
516 		err = regmap_update_bits(data->regmap, SI5341_XAXB_CFG,
517 				SI5341_XAXB_CFG_PDNB, SI5341_XAXB_CFG_PDNB);
518 		if (err < 0)
519 			return err;
520 	}
521 
522 	return 0;
523 }
524 
525 static int si5341_clk_set_parent(struct clk_hw *hw, u8 index)
526 {
527 	struct clk_si5341 *data = to_clk_si5341(hw);
528 
529 	return si5341_clk_reparent(data, index);
530 }
531 
532 static const struct clk_ops si5341_clk_ops = {
533 	.set_parent = si5341_clk_set_parent,
534 	.get_parent = si5341_clk_get_parent,
535 	.recalc_rate = si5341_clk_recalc_rate,
536 };
537 
538 /* Synthesizers, there are 5 synthesizers that connect to any of the outputs */
539 
540 /* The synthesizer is on if all power and enable bits are set */
541 static int si5341_synth_clk_is_on(struct clk_hw *hw)
542 {
543 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
544 	int err;
545 	u32 val;
546 	u8 index = synth->index;
547 
548 	err = regmap_read(synth->data->regmap,
549 			SI5341_SYNTH_N_CLK_TO_OUTX_EN, &val);
550 	if (err < 0)
551 		return 0;
552 
553 	if (!(val & BIT(index)))
554 		return 0;
555 
556 	err = regmap_read(synth->data->regmap, SI5341_SYNTH_N_PDNB, &val);
557 	if (err < 0)
558 		return 0;
559 
560 	if (!(val & BIT(index)))
561 		return 0;
562 
563 	/* This bit must be 0 for the synthesizer to receive clock input */
564 	err = regmap_read(synth->data->regmap, SI5341_SYNTH_N_CLK_DIS, &val);
565 	if (err < 0)
566 		return 0;
567 
568 	return !(val & BIT(index));
569 }
570 
571 static void si5341_synth_clk_unprepare(struct clk_hw *hw)
572 {
573 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
574 	u8 index = synth->index; /* In range 0..5 */
575 	u8 mask = BIT(index);
576 
577 	/* Disable output */
578 	regmap_update_bits(synth->data->regmap,
579 		SI5341_SYNTH_N_CLK_TO_OUTX_EN, mask, 0);
580 	/* Power down */
581 	regmap_update_bits(synth->data->regmap,
582 		SI5341_SYNTH_N_PDNB, mask, 0);
583 	/* Disable clock input to synth (set to 1 to disable) */
584 	regmap_update_bits(synth->data->regmap,
585 		SI5341_SYNTH_N_CLK_DIS, mask, mask);
586 }
587 
588 static int si5341_synth_clk_prepare(struct clk_hw *hw)
589 {
590 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
591 	int err;
592 	u8 index = synth->index;
593 	u8 mask = BIT(index);
594 
595 	/* Power up */
596 	err = regmap_update_bits(synth->data->regmap,
597 		SI5341_SYNTH_N_PDNB, mask, mask);
598 	if (err < 0)
599 		return err;
600 
601 	/* Enable clock input to synth (set bit to 0 to enable) */
602 	err = regmap_update_bits(synth->data->regmap,
603 		SI5341_SYNTH_N_CLK_DIS, mask, 0);
604 	if (err < 0)
605 		return err;
606 
607 	/* Enable output */
608 	return regmap_update_bits(synth->data->regmap,
609 		SI5341_SYNTH_N_CLK_TO_OUTX_EN, mask, mask);
610 }
611 
612 /* Synth clock frequency: Fvco * n_den / n_den, with Fvco in 13500-14256 MHz */
613 static unsigned long si5341_synth_clk_recalc_rate(struct clk_hw *hw,
614 		unsigned long parent_rate)
615 {
616 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
617 	u64 f;
618 	u64 n_num;
619 	u32 n_den;
620 	int err;
621 
622 	err = si5341_decode_44_32(synth->data->regmap,
623 			SI5341_SYNTH_N_NUM(synth->index), &n_num, &n_den);
624 	if (err < 0)
625 		return err;
626 
627 	/*
628 	 * n_num and n_den are shifted left as much as possible, so to prevent
629 	 * overflow in 64-bit math, we shift n_den 4 bits to the right
630 	 */
631 	f = synth->data->freq_vco;
632 	f *= n_den >> 4;
633 
634 	/* Now we need to to 64-bit division: f/n_num */
635 	/* And compensate for the 4 bits we dropped */
636 	f = div64_u64(f, (n_num >> 4));
637 
638 	return f;
639 }
640 
641 static long si5341_synth_clk_round_rate(struct clk_hw *hw, unsigned long rate,
642 		unsigned long *parent_rate)
643 {
644 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
645 	u64 f;
646 
647 	/* The synthesizer accuracy is such that anything in range will work */
648 	f = synth->data->freq_vco;
649 	do_div(f, SI5341_SYNTH_N_MAX);
650 	if (rate < f)
651 		return f;
652 
653 	f = synth->data->freq_vco;
654 	do_div(f, SI5341_SYNTH_N_MIN);
655 	if (rate > f)
656 		return f;
657 
658 	return rate;
659 }
660 
661 static int si5341_synth_program(struct clk_si5341_synth *synth,
662 	u64 n_num, u32 n_den, bool is_integer)
663 {
664 	int err;
665 	u8 index = synth->index;
666 
667 	err = si5341_encode_44_32(synth->data->regmap,
668 			SI5341_SYNTH_N_NUM(index), n_num, n_den);
669 
670 	err = regmap_update_bits(synth->data->regmap,
671 		SI5341_SYNTH_N_PIBYP, BIT(index), is_integer ? BIT(index) : 0);
672 	if (err < 0)
673 		return err;
674 
675 	return regmap_write(synth->data->regmap,
676 		SI5341_SYNTH_N_UPD(index), 0x01);
677 }
678 
679 
680 static int si5341_synth_clk_set_rate(struct clk_hw *hw, unsigned long rate,
681 		unsigned long parent_rate)
682 {
683 	struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
684 	u64 n_num;
685 	u32 n_den;
686 	u32 r;
687 	u32 g;
688 	bool is_integer;
689 
690 	n_num = synth->data->freq_vco;
691 
692 	/* see if there's an integer solution */
693 	r = do_div(n_num, rate);
694 	is_integer = (r == 0);
695 	if (is_integer) {
696 		/* Integer divider equal to n_num */
697 		n_den = 1;
698 	} else {
699 		/* Calculate a fractional solution */
700 		g = gcd(r, rate);
701 		n_den = rate / g;
702 		n_num *= n_den;
703 		n_num += r / g;
704 	}
705 
706 	dev_dbg(&synth->data->i2c_client->dev,
707 			"%s(%u): n=0x%llx d=0x%x %s\n", __func__,
708 				synth->index, n_num, n_den,
709 				is_integer ? "int" : "frac");
710 
711 	return si5341_synth_program(synth, n_num, n_den, is_integer);
712 }
713 
714 static const struct clk_ops si5341_synth_clk_ops = {
715 	.is_prepared = si5341_synth_clk_is_on,
716 	.prepare = si5341_synth_clk_prepare,
717 	.unprepare = si5341_synth_clk_unprepare,
718 	.recalc_rate = si5341_synth_clk_recalc_rate,
719 	.round_rate = si5341_synth_clk_round_rate,
720 	.set_rate = si5341_synth_clk_set_rate,
721 };
722 
723 static int si5341_output_clk_is_on(struct clk_hw *hw)
724 {
725 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
726 	int err;
727 	u32 val;
728 
729 	err = regmap_read(output->data->regmap,
730 			SI5341_OUT_CONFIG(output), &val);
731 	if (err < 0)
732 		return err;
733 
734 	/* Bit 0=PDN, 1=OE so only a value of 0x2 enables the output */
735 	return (val & 0x03) == SI5341_OUT_CFG_OE;
736 }
737 
738 /* Disables and then powers down the output */
739 static void si5341_output_clk_unprepare(struct clk_hw *hw)
740 {
741 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
742 
743 	regmap_update_bits(output->data->regmap,
744 			SI5341_OUT_CONFIG(output),
745 			SI5341_OUT_CFG_OE, 0);
746 	regmap_update_bits(output->data->regmap,
747 			SI5341_OUT_CONFIG(output),
748 			SI5341_OUT_CFG_PDN, SI5341_OUT_CFG_PDN);
749 }
750 
751 /* Powers up and then enables the output */
752 static int si5341_output_clk_prepare(struct clk_hw *hw)
753 {
754 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
755 	int err;
756 
757 	err = regmap_update_bits(output->data->regmap,
758 			SI5341_OUT_CONFIG(output),
759 			SI5341_OUT_CFG_PDN, 0);
760 	if (err < 0)
761 		return err;
762 
763 	return regmap_update_bits(output->data->regmap,
764 			SI5341_OUT_CONFIG(output),
765 			SI5341_OUT_CFG_OE, SI5341_OUT_CFG_OE);
766 }
767 
768 static unsigned long si5341_output_clk_recalc_rate(struct clk_hw *hw,
769 		unsigned long parent_rate)
770 {
771 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
772 	int err;
773 	u32 val;
774 	u32 r_divider;
775 	u8 r[3];
776 
777 	err = regmap_bulk_read(output->data->regmap,
778 			SI5341_OUT_R_REG(output), r, 3);
779 	if (err < 0)
780 		return err;
781 
782 	/* Calculate value as 24-bit integer*/
783 	r_divider = r[2] << 16 | r[1] << 8 | r[0];
784 
785 	/* If Rx_REG is zero, the divider is disabled, so return a "0" rate */
786 	if (!r_divider)
787 		return 0;
788 
789 	/* Divider is 2*(Rx_REG+1) */
790 	r_divider += 1;
791 	r_divider <<= 1;
792 
793 	err = regmap_read(output->data->regmap,
794 			SI5341_OUT_CONFIG(output), &val);
795 	if (err < 0)
796 		return err;
797 
798 	if (val & SI5341_OUT_CFG_RDIV_FORCE2)
799 		r_divider = 2;
800 
801 	return parent_rate / r_divider;
802 }
803 
804 static long si5341_output_clk_round_rate(struct clk_hw *hw, unsigned long rate,
805 		unsigned long *parent_rate)
806 {
807 	unsigned long r;
808 
809 	r = *parent_rate >> 1;
810 
811 	/* If rate is an even divisor, no changes to parent required */
812 	if (r && !(r % rate))
813 		return (long)rate;
814 
815 	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
816 		if (rate > 200000000) {
817 			/* minimum r-divider is 2 */
818 			r = 2;
819 		} else {
820 			/* Take a parent frequency near 400 MHz */
821 			r = (400000000u / rate) & ~1;
822 		}
823 		*parent_rate = r * rate;
824 	} else {
825 		/* We cannot change our parent's rate, report what we can do */
826 		r /= rate;
827 		rate = *parent_rate / (r << 1);
828 	}
829 
830 	return rate;
831 }
832 
833 static int si5341_output_clk_set_rate(struct clk_hw *hw, unsigned long rate,
834 		unsigned long parent_rate)
835 {
836 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
837 	/* Frequency divider is (r_div + 1) * 2 */
838 	u32 r_div = (parent_rate / rate) >> 1;
839 	int err;
840 	u8 r[3];
841 
842 	if (r_div <= 1)
843 		r_div = 0;
844 	else if (r_div >= BIT(24))
845 		r_div = BIT(24) - 1;
846 	else
847 		--r_div;
848 
849 	/* For a value of "2", we set the "OUT0_RDIV_FORCE2" bit */
850 	err = regmap_update_bits(output->data->regmap,
851 			SI5341_OUT_CONFIG(output),
852 			SI5341_OUT_CFG_RDIV_FORCE2,
853 			(r_div == 0) ? SI5341_OUT_CFG_RDIV_FORCE2 : 0);
854 	if (err < 0)
855 		return err;
856 
857 	/* Always write Rx_REG, because a zero value disables the divider */
858 	r[0] = r_div ? (r_div & 0xff) : 1;
859 	r[1] = (r_div >> 8) & 0xff;
860 	r[2] = (r_div >> 16) & 0xff;
861 	err = regmap_bulk_write(output->data->regmap,
862 			SI5341_OUT_R_REG(output), r, 3);
863 
864 	return 0;
865 }
866 
867 static int si5341_output_reparent(struct clk_si5341_output *output, u8 index)
868 {
869 	return regmap_update_bits(output->data->regmap,
870 		SI5341_OUT_MUX_SEL(output), 0x07, index);
871 }
872 
873 static int si5341_output_set_parent(struct clk_hw *hw, u8 index)
874 {
875 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
876 
877 	if (index >= output->data->num_synth)
878 		return -EINVAL;
879 
880 	return si5341_output_reparent(output, index);
881 }
882 
883 static u8 si5341_output_get_parent(struct clk_hw *hw)
884 {
885 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
886 	int err;
887 	u32 val;
888 
889 	err = regmap_read(output->data->regmap,
890 			SI5341_OUT_MUX_SEL(output), &val);
891 
892 	return val & 0x7;
893 }
894 
895 static const struct clk_ops si5341_output_clk_ops = {
896 	.is_prepared = si5341_output_clk_is_on,
897 	.prepare = si5341_output_clk_prepare,
898 	.unprepare = si5341_output_clk_unprepare,
899 	.recalc_rate = si5341_output_clk_recalc_rate,
900 	.round_rate = si5341_output_clk_round_rate,
901 	.set_rate = si5341_output_clk_set_rate,
902 	.set_parent = si5341_output_set_parent,
903 	.get_parent = si5341_output_get_parent,
904 };
905 
906 /*
907  * The chip can be bought in a pre-programmed version, or one can program the
908  * NVM in the chip to boot up in a preset mode. This routine tries to determine
909  * if that's the case, or if we need to reset and program everything from
910  * scratch. Returns negative error, or true/false.
911  */
912 static int si5341_is_programmed_already(struct clk_si5341 *data)
913 {
914 	int err;
915 	u8 r[4];
916 
917 	/* Read the PLL divider value, it must have a non-zero value */
918 	err = regmap_bulk_read(data->regmap, SI5341_PLL_M_DEN,
919 			r, ARRAY_SIZE(r));
920 	if (err < 0)
921 		return err;
922 
923 	return !!get_unaligned_le32(r);
924 }
925 
926 static struct clk_hw *
927 of_clk_si5341_get(struct of_phandle_args *clkspec, void *_data)
928 {
929 	struct clk_si5341 *data = _data;
930 	unsigned int idx = clkspec->args[1];
931 	unsigned int group = clkspec->args[0];
932 
933 	switch (group) {
934 	case 0:
935 		if (idx >= data->num_outputs) {
936 			dev_err(&data->i2c_client->dev,
937 				"invalid output index %u\n", idx);
938 			return ERR_PTR(-EINVAL);
939 		}
940 		return &data->clk[idx].hw;
941 	case 1:
942 		if (idx >= data->num_synth) {
943 			dev_err(&data->i2c_client->dev,
944 				"invalid synthesizer index %u\n", idx);
945 			return ERR_PTR(-EINVAL);
946 		}
947 		return &data->synth[idx].hw;
948 	case 2:
949 		if (idx > 0) {
950 			dev_err(&data->i2c_client->dev,
951 				"invalid PLL index %u\n", idx);
952 			return ERR_PTR(-EINVAL);
953 		}
954 		return &data->hw;
955 	default:
956 		dev_err(&data->i2c_client->dev, "invalid group %u\n", group);
957 		return ERR_PTR(-EINVAL);
958 	}
959 }
960 
961 static int si5341_probe_chip_id(struct clk_si5341 *data)
962 {
963 	int err;
964 	u8 reg[4];
965 	u16 model;
966 
967 	err = regmap_bulk_read(data->regmap, SI5341_PN_BASE, reg,
968 				ARRAY_SIZE(reg));
969 	if (err < 0) {
970 		dev_err(&data->i2c_client->dev, "Failed to read chip ID\n");
971 		return err;
972 	}
973 
974 	model = get_unaligned_le16(reg);
975 
976 	dev_info(&data->i2c_client->dev, "Chip: %x Grade: %u Rev: %u\n",
977 		 model, reg[2], reg[3]);
978 
979 	switch (model) {
980 	case 0x5340:
981 		data->num_outputs = SI5340_MAX_NUM_OUTPUTS;
982 		data->num_synth = SI5340_NUM_SYNTH;
983 		data->reg_output_offset = si5340_reg_output_offset;
984 		data->reg_rdiv_offset = si5340_reg_rdiv_offset;
985 		break;
986 	case 0x5341:
987 		data->num_outputs = SI5341_MAX_NUM_OUTPUTS;
988 		data->num_synth = SI5341_NUM_SYNTH;
989 		data->reg_output_offset = si5341_reg_output_offset;
990 		data->reg_rdiv_offset = si5341_reg_rdiv_offset;
991 		break;
992 	case 0x5342:
993 		data->num_outputs = SI5342_MAX_NUM_OUTPUTS;
994 		data->num_synth = SI5342_NUM_SYNTH;
995 		data->reg_output_offset = si5340_reg_output_offset;
996 		data->reg_rdiv_offset = si5340_reg_rdiv_offset;
997 		break;
998 	case 0x5344:
999 		data->num_outputs = SI5344_MAX_NUM_OUTPUTS;
1000 		data->num_synth = SI5344_NUM_SYNTH;
1001 		data->reg_output_offset = si5340_reg_output_offset;
1002 		data->reg_rdiv_offset = si5340_reg_rdiv_offset;
1003 		break;
1004 	case 0x5345:
1005 		data->num_outputs = SI5345_MAX_NUM_OUTPUTS;
1006 		data->num_synth = SI5345_NUM_SYNTH;
1007 		data->reg_output_offset = si5341_reg_output_offset;
1008 		data->reg_rdiv_offset = si5341_reg_rdiv_offset;
1009 		break;
1010 	default:
1011 		dev_err(&data->i2c_client->dev, "Model '%x' not supported\n",
1012 			model);
1013 		return -EINVAL;
1014 	}
1015 
1016 	data->chip_id = model;
1017 
1018 	return 0;
1019 }
1020 
1021 /* Read active settings into the regmap cache for later reference */
1022 static int si5341_read_settings(struct clk_si5341 *data)
1023 {
1024 	int err;
1025 	u8 i;
1026 	u8 r[10];
1027 
1028 	err = regmap_bulk_read(data->regmap, SI5341_PLL_M_NUM, r, 10);
1029 	if (err < 0)
1030 		return err;
1031 
1032 	err = regmap_bulk_read(data->regmap,
1033 				SI5341_SYNTH_N_CLK_TO_OUTX_EN, r, 3);
1034 	if (err < 0)
1035 		return err;
1036 
1037 	err = regmap_bulk_read(data->regmap,
1038 				SI5341_SYNTH_N_CLK_DIS, r, 1);
1039 	if (err < 0)
1040 		return err;
1041 
1042 	for (i = 0; i < data->num_synth; ++i) {
1043 		err = regmap_bulk_read(data->regmap,
1044 					SI5341_SYNTH_N_NUM(i), r, 10);
1045 		if (err < 0)
1046 			return err;
1047 	}
1048 
1049 	for (i = 0; i < data->num_outputs; ++i) {
1050 		err = regmap_bulk_read(data->regmap,
1051 					data->reg_output_offset[i], r, 4);
1052 		if (err < 0)
1053 			return err;
1054 
1055 		err = regmap_bulk_read(data->regmap,
1056 					data->reg_rdiv_offset[i], r, 3);
1057 		if (err < 0)
1058 			return err;
1059 	}
1060 
1061 	return 0;
1062 }
1063 
1064 static int si5341_write_multiple(struct clk_si5341 *data,
1065 	const struct si5341_reg_default *values, unsigned int num_values)
1066 {
1067 	unsigned int i;
1068 	int res;
1069 
1070 	for (i = 0; i < num_values; ++i) {
1071 		res = regmap_write(data->regmap,
1072 			values[i].address, values[i].value);
1073 		if (res < 0) {
1074 			dev_err(&data->i2c_client->dev,
1075 				"Failed to write %#x:%#x\n",
1076 				values[i].address, values[i].value);
1077 			return res;
1078 		}
1079 	}
1080 
1081 	return 0;
1082 }
1083 
1084 static const struct si5341_reg_default si5341_preamble[] = {
1085 	{ 0x0B25, 0x00 },
1086 	{ 0x0502, 0x01 },
1087 	{ 0x0505, 0x03 },
1088 	{ 0x0957, 0x1F },
1089 	{ 0x0B4E, 0x1A },
1090 };
1091 
1092 static const struct si5341_reg_default si5345_preamble[] = {
1093 	{ 0x0B25, 0x00 },
1094 	{ 0x0540, 0x01 },
1095 };
1096 
1097 static int si5341_send_preamble(struct clk_si5341 *data)
1098 {
1099 	int res;
1100 	u32 revision;
1101 
1102 	/* For revision 2 and up, the values are slightly different */
1103 	res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
1104 	if (res < 0)
1105 		return res;
1106 
1107 	/* Write "preamble" as specified by datasheet */
1108 	res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xD8 : 0xC0);
1109 	if (res < 0)
1110 		return res;
1111 
1112 	/* The si5342..si5345 require a different preamble */
1113 	if (data->chip_id > 0x5341)
1114 		res = si5341_write_multiple(data,
1115 			si5345_preamble, ARRAY_SIZE(si5345_preamble));
1116 	else
1117 		res = si5341_write_multiple(data,
1118 			si5341_preamble, ARRAY_SIZE(si5341_preamble));
1119 	if (res < 0)
1120 		return res;
1121 
1122 	/* Datasheet specifies a 300ms wait after sending the preamble */
1123 	msleep(300);
1124 
1125 	return 0;
1126 }
1127 
1128 /* Perform a soft reset and write post-amble */
1129 static int si5341_finalize_defaults(struct clk_si5341 *data)
1130 {
1131 	int res;
1132 	u32 revision;
1133 
1134 	res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
1135 	if (res < 0)
1136 		return res;
1137 
1138 	dev_dbg(&data->i2c_client->dev, "%s rev=%u\n", __func__, revision);
1139 
1140 	res = regmap_write(data->regmap, SI5341_SOFT_RST, 0x01);
1141 	if (res < 0)
1142 		return res;
1143 
1144 	/* The si5342..si5345 have an additional post-amble */
1145 	if (data->chip_id > 0x5341) {
1146 		res = regmap_write(data->regmap, 0x540, 0x0);
1147 		if (res < 0)
1148 			return res;
1149 	}
1150 
1151 	/* Datasheet does not explain these nameless registers */
1152 	res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xDB : 0xC3);
1153 	if (res < 0)
1154 		return res;
1155 	res = regmap_write(data->regmap, 0x0B25, 0x02);
1156 	if (res < 0)
1157 		return res;
1158 
1159 	return 0;
1160 }
1161 
1162 
1163 static const struct regmap_range si5341_regmap_volatile_range[] = {
1164 	regmap_reg_range(0x000C, 0x0012), /* Status */
1165 	regmap_reg_range(0x001C, 0x001E), /* reset, finc/fdec */
1166 	regmap_reg_range(0x00E2, 0x00FE), /* NVM, interrupts, device ready */
1167 	/* Update bits for P divider and synth config */
1168 	regmap_reg_range(SI5341_PX_UPD, SI5341_PX_UPD),
1169 	regmap_reg_range(SI5341_SYNTH_N_UPD(0), SI5341_SYNTH_N_UPD(0)),
1170 	regmap_reg_range(SI5341_SYNTH_N_UPD(1), SI5341_SYNTH_N_UPD(1)),
1171 	regmap_reg_range(SI5341_SYNTH_N_UPD(2), SI5341_SYNTH_N_UPD(2)),
1172 	regmap_reg_range(SI5341_SYNTH_N_UPD(3), SI5341_SYNTH_N_UPD(3)),
1173 	regmap_reg_range(SI5341_SYNTH_N_UPD(4), SI5341_SYNTH_N_UPD(4)),
1174 };
1175 
1176 static const struct regmap_access_table si5341_regmap_volatile = {
1177 	.yes_ranges = si5341_regmap_volatile_range,
1178 	.n_yes_ranges = ARRAY_SIZE(si5341_regmap_volatile_range),
1179 };
1180 
1181 /* Pages 0, 1, 2, 3, 9, A, B are valid, so there are 12 pages */
1182 static const struct regmap_range_cfg si5341_regmap_ranges[] = {
1183 	{
1184 		.range_min = 0,
1185 		.range_max = SI5341_REGISTER_MAX,
1186 		.selector_reg = SI5341_PAGE,
1187 		.selector_mask = 0xff,
1188 		.selector_shift = 0,
1189 		.window_start = 0,
1190 		.window_len = 256,
1191 	},
1192 };
1193 
1194 static const struct regmap_config si5341_regmap_config = {
1195 	.reg_bits = 8,
1196 	.val_bits = 8,
1197 	.cache_type = REGCACHE_RBTREE,
1198 	.ranges = si5341_regmap_ranges,
1199 	.num_ranges = ARRAY_SIZE(si5341_regmap_ranges),
1200 	.max_register = SI5341_REGISTER_MAX,
1201 	.volatile_table = &si5341_regmap_volatile,
1202 };
1203 
1204 static int si5341_dt_parse_dt(struct i2c_client *client,
1205 	struct clk_si5341_output_config *config)
1206 {
1207 	struct device_node *child;
1208 	struct device_node *np = client->dev.of_node;
1209 	u32 num;
1210 	u32 val;
1211 
1212 	memset(config, 0, sizeof(struct clk_si5341_output_config) *
1213 				SI5341_MAX_NUM_OUTPUTS);
1214 
1215 	for_each_child_of_node(np, child) {
1216 		if (of_property_read_u32(child, "reg", &num)) {
1217 			dev_err(&client->dev, "missing reg property of %s\n",
1218 				child->name);
1219 			goto put_child;
1220 		}
1221 
1222 		if (num >= SI5341_MAX_NUM_OUTPUTS) {
1223 			dev_err(&client->dev, "invalid clkout %d\n", num);
1224 			goto put_child;
1225 		}
1226 
1227 		if (!of_property_read_u32(child, "silabs,format", &val)) {
1228 			/* Set cm and ampl conservatively to 3v3 settings */
1229 			switch (val) {
1230 			case 1: /* normal differential */
1231 				config[num].out_cm_ampl_bits = 0x33;
1232 				break;
1233 			case 2: /* low-power differential */
1234 				config[num].out_cm_ampl_bits = 0x13;
1235 				break;
1236 			case 4: /* LVCMOS */
1237 				config[num].out_cm_ampl_bits = 0x33;
1238 				/* Set SI recommended impedance for LVCMOS */
1239 				config[num].out_format_drv_bits |= 0xc0;
1240 				break;
1241 			default:
1242 				dev_err(&client->dev,
1243 					"invalid silabs,format %u for %u\n",
1244 					val, num);
1245 				goto put_child;
1246 			}
1247 			config[num].out_format_drv_bits &= ~0x07;
1248 			config[num].out_format_drv_bits |= val & 0x07;
1249 			/* Always enable the SYNC feature */
1250 			config[num].out_format_drv_bits |= 0x08;
1251 		}
1252 
1253 		if (!of_property_read_u32(child, "silabs,common-mode", &val)) {
1254 			if (val > 0xf) {
1255 				dev_err(&client->dev,
1256 					"invalid silabs,common-mode %u\n",
1257 					val);
1258 				goto put_child;
1259 			}
1260 			config[num].out_cm_ampl_bits &= 0xf0;
1261 			config[num].out_cm_ampl_bits |= val & 0x0f;
1262 		}
1263 
1264 		if (!of_property_read_u32(child, "silabs,amplitude", &val)) {
1265 			if (val > 0xf) {
1266 				dev_err(&client->dev,
1267 					"invalid silabs,amplitude %u\n",
1268 					val);
1269 				goto put_child;
1270 			}
1271 			config[num].out_cm_ampl_bits &= 0x0f;
1272 			config[num].out_cm_ampl_bits |= (val << 4) & 0xf0;
1273 		}
1274 
1275 		if (of_property_read_bool(child, "silabs,disable-high"))
1276 			config[num].out_format_drv_bits |= 0x10;
1277 
1278 		config[num].synth_master =
1279 			of_property_read_bool(child, "silabs,synth-master");
1280 
1281 		config[num].always_on =
1282 			of_property_read_bool(child, "always-on");
1283 	}
1284 
1285 	return 0;
1286 
1287 put_child:
1288 	of_node_put(child);
1289 	return -EINVAL;
1290 }
1291 
1292 /*
1293  * If not pre-configured, calculate and set the PLL configuration manually.
1294  * For low-jitter performance, the PLL should be set such that the synthesizers
1295  * only need integer division.
1296  * Without any user guidance, we'll set the PLL to 14GHz, which still allows
1297  * the chip to generate any frequency on its outputs, but jitter performance
1298  * may be sub-optimal.
1299  */
1300 static int si5341_initialize_pll(struct clk_si5341 *data)
1301 {
1302 	struct device_node *np = data->i2c_client->dev.of_node;
1303 	u32 m_num = 0;
1304 	u32 m_den = 0;
1305 	int sel;
1306 
1307 	if (of_property_read_u32(np, "silabs,pll-m-num", &m_num)) {
1308 		dev_err(&data->i2c_client->dev,
1309 			"PLL configuration requires silabs,pll-m-num\n");
1310 	}
1311 	if (of_property_read_u32(np, "silabs,pll-m-den", &m_den)) {
1312 		dev_err(&data->i2c_client->dev,
1313 			"PLL configuration requires silabs,pll-m-den\n");
1314 	}
1315 
1316 	if (!m_num || !m_den) {
1317 		dev_err(&data->i2c_client->dev,
1318 			"PLL configuration invalid, assume 14GHz\n");
1319 		sel = si5341_clk_get_selected_input(data);
1320 		if (sel < 0)
1321 			return sel;
1322 
1323 		m_den = clk_get_rate(data->input_clk[sel]) / 10;
1324 		m_num = 1400000000;
1325 	}
1326 
1327 	return si5341_encode_44_32(data->regmap,
1328 			SI5341_PLL_M_NUM, m_num, m_den);
1329 }
1330 
1331 static int si5341_clk_select_active_input(struct clk_si5341 *data)
1332 {
1333 	int res;
1334 	int err;
1335 	int i;
1336 
1337 	res = si5341_clk_get_selected_input(data);
1338 	if (res < 0)
1339 		return res;
1340 
1341 	/* If the current register setting is invalid, pick the first input */
1342 	if (!data->input_clk[res]) {
1343 		dev_dbg(&data->i2c_client->dev,
1344 			"Input %d not connected, rerouting\n", res);
1345 		res = -ENODEV;
1346 		for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
1347 			if (data->input_clk[i]) {
1348 				res = i;
1349 				break;
1350 			}
1351 		}
1352 		if (res < 0) {
1353 			dev_err(&data->i2c_client->dev,
1354 				"No clock input available\n");
1355 			return res;
1356 		}
1357 	}
1358 
1359 	/* Make sure the selected clock is also enabled and routed */
1360 	err = si5341_clk_reparent(data, res);
1361 	if (err < 0)
1362 		return err;
1363 
1364 	err = clk_prepare_enable(data->input_clk[res]);
1365 	if (err < 0)
1366 		return err;
1367 
1368 	return res;
1369 }
1370 
1371 static int si5341_probe(struct i2c_client *client,
1372 		const struct i2c_device_id *id)
1373 {
1374 	struct clk_si5341 *data;
1375 	struct clk_init_data init;
1376 	struct clk *input;
1377 	const char *root_clock_name;
1378 	const char *synth_clock_names[SI5341_NUM_SYNTH];
1379 	int err;
1380 	unsigned int i;
1381 	struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS];
1382 	bool initialization_required;
1383 
1384 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1385 	if (!data)
1386 		return -ENOMEM;
1387 
1388 	data->i2c_client = client;
1389 
1390 	for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
1391 		input = devm_clk_get(&client->dev, si5341_input_clock_names[i]);
1392 		if (IS_ERR(input)) {
1393 			if (PTR_ERR(input) == -EPROBE_DEFER)
1394 				return -EPROBE_DEFER;
1395 			data->input_clk_name[i] = si5341_input_clock_names[i];
1396 		} else {
1397 			data->input_clk[i] = input;
1398 			data->input_clk_name[i] = __clk_get_name(input);
1399 		}
1400 	}
1401 
1402 	err = si5341_dt_parse_dt(client, config);
1403 	if (err)
1404 		return err;
1405 
1406 	if (of_property_read_string(client->dev.of_node, "clock-output-names",
1407 			&init.name))
1408 		init.name = client->dev.of_node->name;
1409 	root_clock_name = init.name;
1410 
1411 	data->regmap = devm_regmap_init_i2c(client, &si5341_regmap_config);
1412 	if (IS_ERR(data->regmap))
1413 		return PTR_ERR(data->regmap);
1414 
1415 	i2c_set_clientdata(client, data);
1416 
1417 	err = si5341_probe_chip_id(data);
1418 	if (err < 0)
1419 		return err;
1420 
1421 	if (of_property_read_bool(client->dev.of_node, "silabs,reprogram")) {
1422 		initialization_required = true;
1423 	} else {
1424 		err = si5341_is_programmed_already(data);
1425 		if (err < 0)
1426 			return err;
1427 
1428 		initialization_required = !err;
1429 	}
1430 
1431 	if (initialization_required) {
1432 		/* Populate the regmap cache in preparation for "cache only" */
1433 		err = si5341_read_settings(data);
1434 		if (err < 0)
1435 			return err;
1436 
1437 		err = si5341_send_preamble(data);
1438 		if (err < 0)
1439 			return err;
1440 
1441 		/*
1442 		 * We intend to send all 'final' register values in a single
1443 		 * transaction. So cache all register writes until we're done
1444 		 * configuring.
1445 		 */
1446 		regcache_cache_only(data->regmap, true);
1447 
1448 		/* Write the configuration pairs from the firmware blob */
1449 		err = si5341_write_multiple(data, si5341_reg_defaults,
1450 					ARRAY_SIZE(si5341_reg_defaults));
1451 		if (err < 0)
1452 			return err;
1453 	}
1454 
1455 	/* Input must be up and running at this point */
1456 	err = si5341_clk_select_active_input(data);
1457 	if (err < 0)
1458 		return err;
1459 
1460 	if (initialization_required) {
1461 		/* PLL configuration is required */
1462 		err = si5341_initialize_pll(data);
1463 		if (err < 0)
1464 			return err;
1465 	}
1466 
1467 	/* Register the PLL */
1468 	init.parent_names = data->input_clk_name;
1469 	init.num_parents = SI5341_NUM_INPUTS;
1470 	init.ops = &si5341_clk_ops;
1471 	init.flags = 0;
1472 	data->hw.init = &init;
1473 
1474 	err = devm_clk_hw_register(&client->dev, &data->hw);
1475 	if (err) {
1476 		dev_err(&client->dev, "clock registration failed\n");
1477 		return err;
1478 	}
1479 
1480 	init.num_parents = 1;
1481 	init.parent_names = &root_clock_name;
1482 	init.ops = &si5341_synth_clk_ops;
1483 	for (i = 0; i < data->num_synth; ++i) {
1484 		synth_clock_names[i] = devm_kasprintf(&client->dev, GFP_KERNEL,
1485 				"%s.N%u", client->dev.of_node->name, i);
1486 		init.name = synth_clock_names[i];
1487 		data->synth[i].index = i;
1488 		data->synth[i].data = data;
1489 		data->synth[i].hw.init = &init;
1490 		err = devm_clk_hw_register(&client->dev, &data->synth[i].hw);
1491 		if (err) {
1492 			dev_err(&client->dev,
1493 				"synth N%u registration failed\n", i);
1494 		}
1495 	}
1496 
1497 	init.num_parents = data->num_synth;
1498 	init.parent_names = synth_clock_names;
1499 	init.ops = &si5341_output_clk_ops;
1500 	for (i = 0; i < data->num_outputs; ++i) {
1501 		init.name = kasprintf(GFP_KERNEL, "%s.%d",
1502 			client->dev.of_node->name, i);
1503 		init.flags = config[i].synth_master ? CLK_SET_RATE_PARENT : 0;
1504 		data->clk[i].index = i;
1505 		data->clk[i].data = data;
1506 		data->clk[i].hw.init = &init;
1507 		if (config[i].out_format_drv_bits & 0x07) {
1508 			regmap_write(data->regmap,
1509 				SI5341_OUT_FORMAT(&data->clk[i]),
1510 				config[i].out_format_drv_bits);
1511 			regmap_write(data->regmap,
1512 				SI5341_OUT_CM(&data->clk[i]),
1513 				config[i].out_cm_ampl_bits);
1514 		}
1515 		err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
1516 		kfree(init.name); /* clock framework made a copy of the name */
1517 		if (err) {
1518 			dev_err(&client->dev,
1519 				"output %u registration failed\n", i);
1520 			return err;
1521 		}
1522 		if (config[i].always_on)
1523 			clk_prepare(data->clk[i].hw.clk);
1524 	}
1525 
1526 	err = of_clk_add_hw_provider(client->dev.of_node, of_clk_si5341_get,
1527 			data);
1528 	if (err) {
1529 		dev_err(&client->dev, "unable to add clk provider\n");
1530 		return err;
1531 	}
1532 
1533 	if (initialization_required) {
1534 		/* Synchronize */
1535 		regcache_cache_only(data->regmap, false);
1536 		err = regcache_sync(data->regmap);
1537 		if (err < 0)
1538 			return err;
1539 
1540 		err = si5341_finalize_defaults(data);
1541 		if (err < 0)
1542 			return err;
1543 	}
1544 
1545 	/* Free the names, clk framework makes copies */
1546 	for (i = 0; i < data->num_synth; ++i)
1547 		 devm_kfree(&client->dev, (void *)synth_clock_names[i]);
1548 
1549 	return 0;
1550 }
1551 
1552 static const struct i2c_device_id si5341_id[] = {
1553 	{ "si5340", 0 },
1554 	{ "si5341", 1 },
1555 	{ "si5342", 2 },
1556 	{ "si5344", 4 },
1557 	{ "si5345", 5 },
1558 	{ }
1559 };
1560 MODULE_DEVICE_TABLE(i2c, si5341_id);
1561 
1562 static const struct of_device_id clk_si5341_of_match[] = {
1563 	{ .compatible = "silabs,si5340" },
1564 	{ .compatible = "silabs,si5341" },
1565 	{ .compatible = "silabs,si5342" },
1566 	{ .compatible = "silabs,si5344" },
1567 	{ .compatible = "silabs,si5345" },
1568 	{ }
1569 };
1570 MODULE_DEVICE_TABLE(of, clk_si5341_of_match);
1571 
1572 static struct i2c_driver si5341_driver = {
1573 	.driver = {
1574 		.name = "si5341",
1575 		.of_match_table = clk_si5341_of_match,
1576 	},
1577 	.probe		= si5341_probe,
1578 	.id_table	= si5341_id,
1579 };
1580 module_i2c_driver(si5341_driver);
1581 
1582 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
1583 MODULE_DESCRIPTION("Si5341 driver");
1584 MODULE_LICENSE("GPL");
1585