1 // SPDX-License-Identifier: GPL-2.0
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>
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.
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>
25 #define SI5341_NUM_INPUTS 4
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
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
39 /* Range of the synthesizer fractional divider */
40 #define SI5341_SYNTH_N_MIN 10
41 #define SI5341_SYNTH_N_MAX 4095
43 /* The chip can get its input clock from 3 input pins or an XTAL */
45 /* There is one PLL running at 13500–14256 MHz */
46 #define SI5341_PLL_VCO_MIN 13500000000ull
47 #define SI5341_PLL_VCO_MAX 14256000000ull
49 /* The 5 frequency synthesizers obtain their input from the PLL */
50 struct clk_si5341_synth
{
52 struct clk_si5341
*data
;
55 #define to_clk_si5341_synth(_hw) \
56 container_of(_hw, struct clk_si5341_synth, hw)
58 /* The output stages can be connected to any synth (full mux) */
59 struct clk_si5341_output
{
61 struct clk_si5341
*data
;
64 #define to_clk_si5341_output(_hw) \
65 container_of(_hw, struct clk_si5341_output, 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 */
82 #define to_clk_si5341(_hw) container_of(_hw, struct clk_si5341, hw)
84 struct clk_si5341_output_config
{
85 u8 out_format_drv_bits
;
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
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
107 /* XTAL config bits */
108 #define SI5341_XAXB_CFG_EXTCLK_EN BIT(0)
109 #define SI5341_XAXB_CFG_PDNB BIT(1)
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
116 /* PLL configuration */
117 #define SI5341_PLL_M_NUM 0x0235
118 #define SI5341_PLL_M_DEN 0x023B
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])
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))
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
140 #define SI5341_REGISTER_MAX 0xBFF
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)
147 /* Static configuration (to be moved to firmware) */
148 struct si5341_reg_default
{
153 static const char * const si5341_input_clock_names
[] = {
154 "in0", "in1", "in2", "xtal"
157 /* Output configuration registers 0..9 are not quite logically organized */
158 /* Also for si5345 */
159 static const u16 si5341_reg_output_offset
[] = {
172 /* for si5340, si5342 and si5344 */
173 static const u16 si5340_reg_output_offset
[] = {
180 /* The location of the R divider registers */
181 static const u16 si5341_reg_rdiv_offset
[] = {
193 static const u16 si5340_reg_rdiv_offset
[] = {
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.
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 */
224 { 0x0038, 0x00 }, /* LOS setting (thresholds) */
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)*/
243 { 0x0206, 0x00 }, /* PXAXB (2^x) */
244 { 0x0208, 0x00 }, /* Px divider setting (usually 0) */
283 { 0x022F, 0x00 }, /* Px divider setting (usually 0) end */
284 { 0x026B, 0x00 }, /* DESIGN_ID (ASCII string) */
291 { 0x0272, 0x00 }, /* DESIGN_ID (ASCII string) end */
292 { 0x0339, 0x1F }, /* N_FSTEP_MSK */
293 { 0x033B, 0x00 }, /* Nx_FSTEPW (Frequency step) */
322 { 0x0358, 0x00 }, /* Nx_FSTEPW (Frequency step) end */
323 { 0x0359, 0x00 }, /* Nx_DELAY */
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) */
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
)
352 err
= regmap_bulk_read(regmap
, reg
, r
, 10);
356 *val1
= ((u64
)((r
[5] & 0x0f) << 8 | r
[4]) << 32) |
357 (get_unaligned_le32(r
));
358 *val2
= get_unaligned_le32(&r
[6]);
363 static int si5341_encode_44_32(struct regmap
*regmap
, unsigned int reg
,
364 u64 n_num
, u32 n_den
)
368 /* Shift left as far as possible without overflowing */
369 while (!(n_num
& BIT_ULL(43)) && !(n_den
& BIT(31))) {
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]);
381 /* Program the fraction */
382 return regmap_bulk_write(regmap
, reg
, r
, sizeof(r
));
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
)
389 struct clk_si5341
*data
= to_clk_si5341(hw
);
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
,
402 if (!m_num
|| !m_den
)
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.
413 while (res
& 0xffff00000000ULL
) {
418 do_div(res
, (m_den
>> shift
));
420 /* We cannot return the actual frequency in 32 bit, store it locally */
421 data
->freq_vco
= res
;
423 /* Report kHz since the value is out of range */
426 return (unsigned long)res
;
429 static int si5341_clk_get_selected_input(struct clk_si5341
*data
)
434 err
= regmap_read(data
->regmap
, SI5341_IN_SEL
, &val
);
438 return (val
& SI5341_IN_SEL_MASK
) >> SI5341_IN_SEL_SHIFT
;
441 static u8
si5341_clk_get_parent(struct clk_hw
*hw
)
443 struct clk_si5341
*data
= to_clk_si5341(hw
);
444 int res
= si5341_clk_get_selected_input(data
);
447 return 0; /* Apparently we cannot report errors */
452 static int si5341_clk_reparent(struct clk_si5341
*data
, u8 index
)
457 val
= (index
<< SI5341_IN_SEL_SHIFT
) & SI5341_IN_SEL_MASK
;
458 /* Enable register-based input selection */
459 val
|= SI5341_IN_SEL_REGCTRL
;
461 err
= regmap_update_bits(data
->regmap
,
462 SI5341_IN_SEL
, SI5341_IN_SEL_REGCTRL
| SI5341_IN_SEL_MASK
, val
);
467 /* Enable input buffer for selected input */
468 err
= regmap_update_bits(data
->regmap
,
469 SI5341_IN_EN
, 0x07, BIT(index
));
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
));
480 /* Power down XTAL oscillator and buffer */
481 err
= regmap_update_bits(data
->regmap
, SI5341_XAXB_CFG
,
482 SI5341_XAXB_CFG_PDNB
, 0);
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.
491 err
= regmap_write(data
->regmap
, SI5341_IN_PDIV(index
), 1);
495 err
= regmap_write(data
->regmap
, SI5341_IN_PSET(index
), 1);
499 /* Set update PDIV bit */
500 err
= regmap_write(data
->regmap
, SI5341_PX_UPD
, BIT(index
));
504 /* Disable all input buffers */
505 err
= regmap_update_bits(data
->regmap
, SI5341_IN_EN
, 0x07, 0);
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);
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
);
525 static int si5341_clk_set_parent(struct clk_hw
*hw
, u8 index
)
527 struct clk_si5341
*data
= to_clk_si5341(hw
);
529 return si5341_clk_reparent(data
, index
);
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
,
538 /* Synthesizers, there are 5 synthesizers that connect to any of the outputs */
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
)
543 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
546 u8 index
= synth
->index
;
548 err
= regmap_read(synth
->data
->regmap
,
549 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, &val
);
553 if (!(val
& BIT(index
)))
556 err
= regmap_read(synth
->data
->regmap
, SI5341_SYNTH_N_PDNB
, &val
);
560 if (!(val
& BIT(index
)))
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
);
568 return !(val
& BIT(index
));
571 static void si5341_synth_clk_unprepare(struct clk_hw
*hw
)
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
);
578 regmap_update_bits(synth
->data
->regmap
,
579 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, mask
, 0);
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
);
588 static int si5341_synth_clk_prepare(struct clk_hw
*hw
)
590 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
592 u8 index
= synth
->index
;
593 u8 mask
= BIT(index
);
596 err
= regmap_update_bits(synth
->data
->regmap
,
597 SI5341_SYNTH_N_PDNB
, mask
, mask
);
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);
608 return regmap_update_bits(synth
->data
->regmap
,
609 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, mask
, mask
);
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
)
616 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
622 err
= si5341_decode_44_32(synth
->data
->regmap
,
623 SI5341_SYNTH_N_NUM(synth
->index
), &n_num
, &n_den
);
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
631 f
= synth
->data
->freq_vco
;
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));
641 static long si5341_synth_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
642 unsigned long *parent_rate
)
644 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
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
);
653 f
= synth
->data
->freq_vco
;
654 do_div(f
, SI5341_SYNTH_N_MIN
);
661 static int si5341_synth_program(struct clk_si5341_synth
*synth
,
662 u64 n_num
, u32 n_den
, bool is_integer
)
665 u8 index
= synth
->index
;
667 err
= si5341_encode_44_32(synth
->data
->regmap
,
668 SI5341_SYNTH_N_NUM(index
), n_num
, n_den
);
670 err
= regmap_update_bits(synth
->data
->regmap
,
671 SI5341_SYNTH_N_PIBYP
, BIT(index
), is_integer
? BIT(index
) : 0);
675 return regmap_write(synth
->data
->regmap
,
676 SI5341_SYNTH_N_UPD(index
), 0x01);
680 static int si5341_synth_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
681 unsigned long parent_rate
)
683 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
690 n_num
= synth
->data
->freq_vco
;
692 /* see if there's an integer solution */
693 r
= do_div(n_num
, rate
);
694 is_integer
= (r
== 0);
696 /* Integer divider equal to n_num */
699 /* Calculate a fractional solution */
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");
711 return si5341_synth_program(synth
, n_num
, n_den
, is_integer
);
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
,
723 static int si5341_output_clk_is_on(struct clk_hw
*hw
)
725 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
729 err
= regmap_read(output
->data
->regmap
,
730 SI5341_OUT_CONFIG(output
), &val
);
734 /* Bit 0=PDN, 1=OE so only a value of 0x2 enables the output */
735 return (val
& 0x03) == SI5341_OUT_CFG_OE
;
738 /* Disables and then powers down the output */
739 static void si5341_output_clk_unprepare(struct clk_hw
*hw
)
741 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
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
);
751 /* Powers up and then enables the output */
752 static int si5341_output_clk_prepare(struct clk_hw
*hw
)
754 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
757 err
= regmap_update_bits(output
->data
->regmap
,
758 SI5341_OUT_CONFIG(output
),
759 SI5341_OUT_CFG_PDN
, 0);
763 return regmap_update_bits(output
->data
->regmap
,
764 SI5341_OUT_CONFIG(output
),
765 SI5341_OUT_CFG_OE
, SI5341_OUT_CFG_OE
);
768 static unsigned long si5341_output_clk_recalc_rate(struct clk_hw
*hw
,
769 unsigned long parent_rate
)
771 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
777 err
= regmap_bulk_read(output
->data
->regmap
,
778 SI5341_OUT_R_REG(output
), r
, 3);
782 /* Calculate value as 24-bit integer*/
783 r_divider
= r
[2] << 16 | r
[1] << 8 | r
[0];
785 /* If Rx_REG is zero, the divider is disabled, so return a "0" rate */
789 /* Divider is 2*(Rx_REG+1) */
793 err
= regmap_read(output
->data
->regmap
,
794 SI5341_OUT_CONFIG(output
), &val
);
798 if (val
& SI5341_OUT_CFG_RDIV_FORCE2
)
801 return parent_rate
/ r_divider
;
804 static long si5341_output_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
805 unsigned long *parent_rate
)
809 r
= *parent_rate
>> 1;
811 /* If rate is an even divisor, no changes to parent required */
812 if (r
&& !(r
% rate
))
815 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
) {
816 if (rate
> 200000000) {
817 /* minimum r-divider is 2 */
820 /* Take a parent frequency near 400 MHz */
821 r
= (400000000u / rate
) & ~1;
823 *parent_rate
= r
* rate
;
825 /* We cannot change our parent's rate, report what we can do */
827 rate
= *parent_rate
/ (r
<< 1);
833 static int si5341_output_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
834 unsigned long parent_rate
)
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;
844 else if (r_div
>= BIT(24))
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);
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);
867 static int si5341_output_reparent(struct clk_si5341_output
*output
, u8 index
)
869 return regmap_update_bits(output
->data
->regmap
,
870 SI5341_OUT_MUX_SEL(output
), 0x07, index
);
873 static int si5341_output_set_parent(struct clk_hw
*hw
, u8 index
)
875 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
877 if (index
>= output
->data
->num_synth
)
880 return si5341_output_reparent(output
, index
);
883 static u8
si5341_output_get_parent(struct clk_hw
*hw
)
885 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
888 regmap_read(output
->data
->regmap
, SI5341_OUT_MUX_SEL(output
), &val
);
893 static const struct clk_ops si5341_output_clk_ops
= {
894 .is_prepared
= si5341_output_clk_is_on
,
895 .prepare
= si5341_output_clk_prepare
,
896 .unprepare
= si5341_output_clk_unprepare
,
897 .recalc_rate
= si5341_output_clk_recalc_rate
,
898 .round_rate
= si5341_output_clk_round_rate
,
899 .set_rate
= si5341_output_clk_set_rate
,
900 .set_parent
= si5341_output_set_parent
,
901 .get_parent
= si5341_output_get_parent
,
905 * The chip can be bought in a pre-programmed version, or one can program the
906 * NVM in the chip to boot up in a preset mode. This routine tries to determine
907 * if that's the case, or if we need to reset and program everything from
908 * scratch. Returns negative error, or true/false.
910 static int si5341_is_programmed_already(struct clk_si5341
*data
)
915 /* Read the PLL divider value, it must have a non-zero value */
916 err
= regmap_bulk_read(data
->regmap
, SI5341_PLL_M_DEN
,
921 return !!get_unaligned_le32(r
);
924 static struct clk_hw
*
925 of_clk_si5341_get(struct of_phandle_args
*clkspec
, void *_data
)
927 struct clk_si5341
*data
= _data
;
928 unsigned int idx
= clkspec
->args
[1];
929 unsigned int group
= clkspec
->args
[0];
933 if (idx
>= data
->num_outputs
) {
934 dev_err(&data
->i2c_client
->dev
,
935 "invalid output index %u\n", idx
);
936 return ERR_PTR(-EINVAL
);
938 return &data
->clk
[idx
].hw
;
940 if (idx
>= data
->num_synth
) {
941 dev_err(&data
->i2c_client
->dev
,
942 "invalid synthesizer index %u\n", idx
);
943 return ERR_PTR(-EINVAL
);
945 return &data
->synth
[idx
].hw
;
948 dev_err(&data
->i2c_client
->dev
,
949 "invalid PLL index %u\n", idx
);
950 return ERR_PTR(-EINVAL
);
954 dev_err(&data
->i2c_client
->dev
, "invalid group %u\n", group
);
955 return ERR_PTR(-EINVAL
);
959 static int si5341_probe_chip_id(struct clk_si5341
*data
)
965 err
= regmap_bulk_read(data
->regmap
, SI5341_PN_BASE
, reg
,
968 dev_err(&data
->i2c_client
->dev
, "Failed to read chip ID\n");
972 model
= get_unaligned_le16(reg
);
974 dev_info(&data
->i2c_client
->dev
, "Chip: %x Grade: %u Rev: %u\n",
975 model
, reg
[2], reg
[3]);
979 data
->num_outputs
= SI5340_MAX_NUM_OUTPUTS
;
980 data
->num_synth
= SI5340_NUM_SYNTH
;
981 data
->reg_output_offset
= si5340_reg_output_offset
;
982 data
->reg_rdiv_offset
= si5340_reg_rdiv_offset
;
985 data
->num_outputs
= SI5341_MAX_NUM_OUTPUTS
;
986 data
->num_synth
= SI5341_NUM_SYNTH
;
987 data
->reg_output_offset
= si5341_reg_output_offset
;
988 data
->reg_rdiv_offset
= si5341_reg_rdiv_offset
;
991 data
->num_outputs
= SI5342_MAX_NUM_OUTPUTS
;
992 data
->num_synth
= SI5342_NUM_SYNTH
;
993 data
->reg_output_offset
= si5340_reg_output_offset
;
994 data
->reg_rdiv_offset
= si5340_reg_rdiv_offset
;
997 data
->num_outputs
= SI5344_MAX_NUM_OUTPUTS
;
998 data
->num_synth
= SI5344_NUM_SYNTH
;
999 data
->reg_output_offset
= si5340_reg_output_offset
;
1000 data
->reg_rdiv_offset
= si5340_reg_rdiv_offset
;
1003 data
->num_outputs
= SI5345_MAX_NUM_OUTPUTS
;
1004 data
->num_synth
= SI5345_NUM_SYNTH
;
1005 data
->reg_output_offset
= si5341_reg_output_offset
;
1006 data
->reg_rdiv_offset
= si5341_reg_rdiv_offset
;
1009 dev_err(&data
->i2c_client
->dev
, "Model '%x' not supported\n",
1014 data
->chip_id
= model
;
1019 /* Read active settings into the regmap cache for later reference */
1020 static int si5341_read_settings(struct clk_si5341
*data
)
1026 err
= regmap_bulk_read(data
->regmap
, SI5341_PLL_M_NUM
, r
, 10);
1030 err
= regmap_bulk_read(data
->regmap
,
1031 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, r
, 3);
1035 err
= regmap_bulk_read(data
->regmap
,
1036 SI5341_SYNTH_N_CLK_DIS
, r
, 1);
1040 for (i
= 0; i
< data
->num_synth
; ++i
) {
1041 err
= regmap_bulk_read(data
->regmap
,
1042 SI5341_SYNTH_N_NUM(i
), r
, 10);
1047 for (i
= 0; i
< data
->num_outputs
; ++i
) {
1048 err
= regmap_bulk_read(data
->regmap
,
1049 data
->reg_output_offset
[i
], r
, 4);
1053 err
= regmap_bulk_read(data
->regmap
,
1054 data
->reg_rdiv_offset
[i
], r
, 3);
1062 static int si5341_write_multiple(struct clk_si5341
*data
,
1063 const struct si5341_reg_default
*values
, unsigned int num_values
)
1068 for (i
= 0; i
< num_values
; ++i
) {
1069 res
= regmap_write(data
->regmap
,
1070 values
[i
].address
, values
[i
].value
);
1072 dev_err(&data
->i2c_client
->dev
,
1073 "Failed to write %#x:%#x\n",
1074 values
[i
].address
, values
[i
].value
);
1082 static const struct si5341_reg_default si5341_preamble
[] = {
1090 static const struct si5341_reg_default si5345_preamble
[] = {
1095 static int si5341_send_preamble(struct clk_si5341
*data
)
1100 /* For revision 2 and up, the values are slightly different */
1101 res
= regmap_read(data
->regmap
, SI5341_DEVICE_REV
, &revision
);
1105 /* Write "preamble" as specified by datasheet */
1106 res
= regmap_write(data
->regmap
, 0xB24, revision
< 2 ? 0xD8 : 0xC0);
1110 /* The si5342..si5345 require a different preamble */
1111 if (data
->chip_id
> 0x5341)
1112 res
= si5341_write_multiple(data
,
1113 si5345_preamble
, ARRAY_SIZE(si5345_preamble
));
1115 res
= si5341_write_multiple(data
,
1116 si5341_preamble
, ARRAY_SIZE(si5341_preamble
));
1120 /* Datasheet specifies a 300ms wait after sending the preamble */
1126 /* Perform a soft reset and write post-amble */
1127 static int si5341_finalize_defaults(struct clk_si5341
*data
)
1132 res
= regmap_read(data
->regmap
, SI5341_DEVICE_REV
, &revision
);
1136 dev_dbg(&data
->i2c_client
->dev
, "%s rev=%u\n", __func__
, revision
);
1138 res
= regmap_write(data
->regmap
, SI5341_SOFT_RST
, 0x01);
1142 /* The si5342..si5345 have an additional post-amble */
1143 if (data
->chip_id
> 0x5341) {
1144 res
= regmap_write(data
->regmap
, 0x540, 0x0);
1149 /* Datasheet does not explain these nameless registers */
1150 res
= regmap_write(data
->regmap
, 0xB24, revision
< 2 ? 0xDB : 0xC3);
1153 res
= regmap_write(data
->regmap
, 0x0B25, 0x02);
1161 static const struct regmap_range si5341_regmap_volatile_range
[] = {
1162 regmap_reg_range(0x000C, 0x0012), /* Status */
1163 regmap_reg_range(0x001C, 0x001E), /* reset, finc/fdec */
1164 regmap_reg_range(0x00E2, 0x00FE), /* NVM, interrupts, device ready */
1165 /* Update bits for P divider and synth config */
1166 regmap_reg_range(SI5341_PX_UPD
, SI5341_PX_UPD
),
1167 regmap_reg_range(SI5341_SYNTH_N_UPD(0), SI5341_SYNTH_N_UPD(0)),
1168 regmap_reg_range(SI5341_SYNTH_N_UPD(1), SI5341_SYNTH_N_UPD(1)),
1169 regmap_reg_range(SI5341_SYNTH_N_UPD(2), SI5341_SYNTH_N_UPD(2)),
1170 regmap_reg_range(SI5341_SYNTH_N_UPD(3), SI5341_SYNTH_N_UPD(3)),
1171 regmap_reg_range(SI5341_SYNTH_N_UPD(4), SI5341_SYNTH_N_UPD(4)),
1174 static const struct regmap_access_table si5341_regmap_volatile
= {
1175 .yes_ranges
= si5341_regmap_volatile_range
,
1176 .n_yes_ranges
= ARRAY_SIZE(si5341_regmap_volatile_range
),
1179 /* Pages 0, 1, 2, 3, 9, A, B are valid, so there are 12 pages */
1180 static const struct regmap_range_cfg si5341_regmap_ranges
[] = {
1183 .range_max
= SI5341_REGISTER_MAX
,
1184 .selector_reg
= SI5341_PAGE
,
1185 .selector_mask
= 0xff,
1186 .selector_shift
= 0,
1192 static const struct regmap_config si5341_regmap_config
= {
1195 .cache_type
= REGCACHE_RBTREE
,
1196 .ranges
= si5341_regmap_ranges
,
1197 .num_ranges
= ARRAY_SIZE(si5341_regmap_ranges
),
1198 .max_register
= SI5341_REGISTER_MAX
,
1199 .volatile_table
= &si5341_regmap_volatile
,
1202 static int si5341_dt_parse_dt(struct i2c_client
*client
,
1203 struct clk_si5341_output_config
*config
)
1205 struct device_node
*child
;
1206 struct device_node
*np
= client
->dev
.of_node
;
1210 memset(config
, 0, sizeof(struct clk_si5341_output_config
) *
1211 SI5341_MAX_NUM_OUTPUTS
);
1213 for_each_child_of_node(np
, child
) {
1214 if (of_property_read_u32(child
, "reg", &num
)) {
1215 dev_err(&client
->dev
, "missing reg property of %s\n",
1220 if (num
>= SI5341_MAX_NUM_OUTPUTS
) {
1221 dev_err(&client
->dev
, "invalid clkout %d\n", num
);
1225 if (!of_property_read_u32(child
, "silabs,format", &val
)) {
1226 /* Set cm and ampl conservatively to 3v3 settings */
1228 case 1: /* normal differential */
1229 config
[num
].out_cm_ampl_bits
= 0x33;
1231 case 2: /* low-power differential */
1232 config
[num
].out_cm_ampl_bits
= 0x13;
1234 case 4: /* LVCMOS */
1235 config
[num
].out_cm_ampl_bits
= 0x33;
1236 /* Set SI recommended impedance for LVCMOS */
1237 config
[num
].out_format_drv_bits
|= 0xc0;
1240 dev_err(&client
->dev
,
1241 "invalid silabs,format %u for %u\n",
1245 config
[num
].out_format_drv_bits
&= ~0x07;
1246 config
[num
].out_format_drv_bits
|= val
& 0x07;
1247 /* Always enable the SYNC feature */
1248 config
[num
].out_format_drv_bits
|= 0x08;
1251 if (!of_property_read_u32(child
, "silabs,common-mode", &val
)) {
1253 dev_err(&client
->dev
,
1254 "invalid silabs,common-mode %u\n",
1258 config
[num
].out_cm_ampl_bits
&= 0xf0;
1259 config
[num
].out_cm_ampl_bits
|= val
& 0x0f;
1262 if (!of_property_read_u32(child
, "silabs,amplitude", &val
)) {
1264 dev_err(&client
->dev
,
1265 "invalid silabs,amplitude %u\n",
1269 config
[num
].out_cm_ampl_bits
&= 0x0f;
1270 config
[num
].out_cm_ampl_bits
|= (val
<< 4) & 0xf0;
1273 if (of_property_read_bool(child
, "silabs,disable-high"))
1274 config
[num
].out_format_drv_bits
|= 0x10;
1276 config
[num
].synth_master
=
1277 of_property_read_bool(child
, "silabs,synth-master");
1279 config
[num
].always_on
=
1280 of_property_read_bool(child
, "always-on");
1291 * If not pre-configured, calculate and set the PLL configuration manually.
1292 * For low-jitter performance, the PLL should be set such that the synthesizers
1293 * only need integer division.
1294 * Without any user guidance, we'll set the PLL to 14GHz, which still allows
1295 * the chip to generate any frequency on its outputs, but jitter performance
1296 * may be sub-optimal.
1298 static int si5341_initialize_pll(struct clk_si5341
*data
)
1300 struct device_node
*np
= data
->i2c_client
->dev
.of_node
;
1305 if (of_property_read_u32(np
, "silabs,pll-m-num", &m_num
)) {
1306 dev_err(&data
->i2c_client
->dev
,
1307 "PLL configuration requires silabs,pll-m-num\n");
1309 if (of_property_read_u32(np
, "silabs,pll-m-den", &m_den
)) {
1310 dev_err(&data
->i2c_client
->dev
,
1311 "PLL configuration requires silabs,pll-m-den\n");
1314 if (!m_num
|| !m_den
) {
1315 dev_err(&data
->i2c_client
->dev
,
1316 "PLL configuration invalid, assume 14GHz\n");
1317 sel
= si5341_clk_get_selected_input(data
);
1321 m_den
= clk_get_rate(data
->input_clk
[sel
]) / 10;
1325 return si5341_encode_44_32(data
->regmap
,
1326 SI5341_PLL_M_NUM
, m_num
, m_den
);
1329 static int si5341_clk_select_active_input(struct clk_si5341
*data
)
1335 res
= si5341_clk_get_selected_input(data
);
1339 /* If the current register setting is invalid, pick the first input */
1340 if (!data
->input_clk
[res
]) {
1341 dev_dbg(&data
->i2c_client
->dev
,
1342 "Input %d not connected, rerouting\n", res
);
1344 for (i
= 0; i
< SI5341_NUM_INPUTS
; ++i
) {
1345 if (data
->input_clk
[i
]) {
1351 dev_err(&data
->i2c_client
->dev
,
1352 "No clock input available\n");
1357 /* Make sure the selected clock is also enabled and routed */
1358 err
= si5341_clk_reparent(data
, res
);
1362 err
= clk_prepare_enable(data
->input_clk
[res
]);
1369 static int si5341_probe(struct i2c_client
*client
,
1370 const struct i2c_device_id
*id
)
1372 struct clk_si5341
*data
;
1373 struct clk_init_data init
;
1375 const char *root_clock_name
;
1376 const char *synth_clock_names
[SI5341_NUM_SYNTH
];
1379 struct clk_si5341_output_config config
[SI5341_MAX_NUM_OUTPUTS
];
1380 bool initialization_required
;
1382 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1386 data
->i2c_client
= client
;
1388 for (i
= 0; i
< SI5341_NUM_INPUTS
; ++i
) {
1389 input
= devm_clk_get(&client
->dev
, si5341_input_clock_names
[i
]);
1390 if (IS_ERR(input
)) {
1391 if (PTR_ERR(input
) == -EPROBE_DEFER
)
1392 return -EPROBE_DEFER
;
1393 data
->input_clk_name
[i
] = si5341_input_clock_names
[i
];
1395 data
->input_clk
[i
] = input
;
1396 data
->input_clk_name
[i
] = __clk_get_name(input
);
1400 err
= si5341_dt_parse_dt(client
, config
);
1404 if (of_property_read_string(client
->dev
.of_node
, "clock-output-names",
1406 init
.name
= client
->dev
.of_node
->name
;
1407 root_clock_name
= init
.name
;
1409 data
->regmap
= devm_regmap_init_i2c(client
, &si5341_regmap_config
);
1410 if (IS_ERR(data
->regmap
))
1411 return PTR_ERR(data
->regmap
);
1413 i2c_set_clientdata(client
, data
);
1415 err
= si5341_probe_chip_id(data
);
1419 if (of_property_read_bool(client
->dev
.of_node
, "silabs,reprogram")) {
1420 initialization_required
= true;
1422 err
= si5341_is_programmed_already(data
);
1426 initialization_required
= !err
;
1429 if (initialization_required
) {
1430 /* Populate the regmap cache in preparation for "cache only" */
1431 err
= si5341_read_settings(data
);
1435 err
= si5341_send_preamble(data
);
1440 * We intend to send all 'final' register values in a single
1441 * transaction. So cache all register writes until we're done
1444 regcache_cache_only(data
->regmap
, true);
1446 /* Write the configuration pairs from the firmware blob */
1447 err
= si5341_write_multiple(data
, si5341_reg_defaults
,
1448 ARRAY_SIZE(si5341_reg_defaults
));
1453 /* Input must be up and running at this point */
1454 err
= si5341_clk_select_active_input(data
);
1458 if (initialization_required
) {
1459 /* PLL configuration is required */
1460 err
= si5341_initialize_pll(data
);
1465 /* Register the PLL */
1466 init
.parent_names
= data
->input_clk_name
;
1467 init
.num_parents
= SI5341_NUM_INPUTS
;
1468 init
.ops
= &si5341_clk_ops
;
1470 data
->hw
.init
= &init
;
1472 err
= devm_clk_hw_register(&client
->dev
, &data
->hw
);
1474 dev_err(&client
->dev
, "clock registration failed\n");
1478 init
.num_parents
= 1;
1479 init
.parent_names
= &root_clock_name
;
1480 init
.ops
= &si5341_synth_clk_ops
;
1481 for (i
= 0; i
< data
->num_synth
; ++i
) {
1482 synth_clock_names
[i
] = devm_kasprintf(&client
->dev
, GFP_KERNEL
,
1483 "%s.N%u", client
->dev
.of_node
->name
, i
);
1484 init
.name
= synth_clock_names
[i
];
1485 data
->synth
[i
].index
= i
;
1486 data
->synth
[i
].data
= data
;
1487 data
->synth
[i
].hw
.init
= &init
;
1488 err
= devm_clk_hw_register(&client
->dev
, &data
->synth
[i
].hw
);
1490 dev_err(&client
->dev
,
1491 "synth N%u registration failed\n", i
);
1495 init
.num_parents
= data
->num_synth
;
1496 init
.parent_names
= synth_clock_names
;
1497 init
.ops
= &si5341_output_clk_ops
;
1498 for (i
= 0; i
< data
->num_outputs
; ++i
) {
1499 init
.name
= kasprintf(GFP_KERNEL
, "%s.%d",
1500 client
->dev
.of_node
->name
, i
);
1501 init
.flags
= config
[i
].synth_master
? CLK_SET_RATE_PARENT
: 0;
1502 data
->clk
[i
].index
= i
;
1503 data
->clk
[i
].data
= data
;
1504 data
->clk
[i
].hw
.init
= &init
;
1505 if (config
[i
].out_format_drv_bits
& 0x07) {
1506 regmap_write(data
->regmap
,
1507 SI5341_OUT_FORMAT(&data
->clk
[i
]),
1508 config
[i
].out_format_drv_bits
);
1509 regmap_write(data
->regmap
,
1510 SI5341_OUT_CM(&data
->clk
[i
]),
1511 config
[i
].out_cm_ampl_bits
);
1513 err
= devm_clk_hw_register(&client
->dev
, &data
->clk
[i
].hw
);
1514 kfree(init
.name
); /* clock framework made a copy of the name */
1516 dev_err(&client
->dev
,
1517 "output %u registration failed\n", i
);
1520 if (config
[i
].always_on
)
1521 clk_prepare(data
->clk
[i
].hw
.clk
);
1524 err
= of_clk_add_hw_provider(client
->dev
.of_node
, of_clk_si5341_get
,
1527 dev_err(&client
->dev
, "unable to add clk provider\n");
1531 if (initialization_required
) {
1533 regcache_cache_only(data
->regmap
, false);
1534 err
= regcache_sync(data
->regmap
);
1538 err
= si5341_finalize_defaults(data
);
1543 /* Free the names, clk framework makes copies */
1544 for (i
= 0; i
< data
->num_synth
; ++i
)
1545 devm_kfree(&client
->dev
, (void *)synth_clock_names
[i
]);
1550 static const struct i2c_device_id si5341_id
[] = {
1558 MODULE_DEVICE_TABLE(i2c
, si5341_id
);
1560 static const struct of_device_id clk_si5341_of_match
[] = {
1561 { .compatible
= "silabs,si5340" },
1562 { .compatible
= "silabs,si5341" },
1563 { .compatible
= "silabs,si5342" },
1564 { .compatible
= "silabs,si5344" },
1565 { .compatible
= "silabs,si5345" },
1568 MODULE_DEVICE_TABLE(of
, clk_si5341_of_match
);
1570 static struct i2c_driver si5341_driver
= {
1573 .of_match_table
= clk_si5341_of_match
,
1575 .probe
= si5341_probe
,
1576 .id_table
= si5341_id
,
1578 module_i2c_driver(si5341_driver
);
1580 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
1581 MODULE_DESCRIPTION("Si5341 driver");
1582 MODULE_LICENSE("GPL");