1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Silicon Labs Si5341/Si5340 Clock generator
4 * Copyright (C) 2019 Topic Embedded Products
5 * Author: Mike Looijmans <mike.looijmans@topic.nl>
9 #include <linux/clk-provider.h>
10 #include <linux/delay.h>
11 #include <linux/gcd.h>
12 #include <linux/math64.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <asm/unaligned.h>
19 #define SI5341_NUM_INPUTS 4
21 #define SI5341_MAX_NUM_OUTPUTS 10
22 #define SI5340_MAX_NUM_OUTPUTS 4
24 #define SI5341_NUM_SYNTH 5
25 #define SI5340_NUM_SYNTH 4
27 /* Range of the synthesizer fractional divider */
28 #define SI5341_SYNTH_N_MIN 10
29 #define SI5341_SYNTH_N_MAX 4095
31 /* The chip can get its input clock from 3 input pins or an XTAL */
33 /* There is one PLL running at 13500–14256 MHz */
34 #define SI5341_PLL_VCO_MIN 13500000000ull
35 #define SI5341_PLL_VCO_MAX 14256000000ull
37 /* The 5 frequency synthesizers obtain their input from the PLL */
38 struct clk_si5341_synth
{
40 struct clk_si5341
*data
;
43 #define to_clk_si5341_synth(_hw) \
44 container_of(_hw, struct clk_si5341_synth, hw)
46 /* The output stages can be connected to any synth (full mux) */
47 struct clk_si5341_output
{
49 struct clk_si5341
*data
;
52 #define to_clk_si5341_output(_hw) \
53 container_of(_hw, struct clk_si5341_output, hw)
57 struct regmap
*regmap
;
58 struct i2c_client
*i2c_client
;
59 struct clk_si5341_synth synth
[SI5341_NUM_SYNTH
];
60 struct clk_si5341_output clk
[SI5341_MAX_NUM_OUTPUTS
];
61 struct clk
*input_clk
[SI5341_NUM_INPUTS
];
62 const char *input_clk_name
[SI5341_NUM_INPUTS
];
63 const u16
*reg_output_offset
;
64 const u16
*reg_rdiv_offset
;
65 u64 freq_vco
; /* 13500–14256 MHz */
69 #define to_clk_si5341(_hw) container_of(_hw, struct clk_si5341, hw)
71 struct clk_si5341_output_config
{
72 u8 out_format_drv_bits
;
78 #define SI5341_PAGE 0x0001
79 #define SI5341_PN_BASE 0x0002
80 #define SI5341_DEVICE_REV 0x0005
81 #define SI5341_STATUS 0x000C
82 #define SI5341_SOFT_RST 0x001C
83 #define SI5341_IN_SEL 0x0021
84 #define SI5341_XAXB_CFG 0x090E
85 #define SI5341_IN_EN 0x0949
86 #define SI5341_INX_TO_PFD_EN 0x094A
89 #define SI5341_IN_SEL_MASK 0x06
90 #define SI5341_IN_SEL_SHIFT 1
91 #define SI5341_IN_SEL_REGCTRL 0x01
92 #define SI5341_INX_TO_PFD_SHIFT 4
94 /* XTAL config bits */
95 #define SI5341_XAXB_CFG_EXTCLK_EN BIT(0)
96 #define SI5341_XAXB_CFG_PDNB BIT(1)
98 /* Input dividers (48-bit) */
99 #define SI5341_IN_PDIV(x) (0x0208 + ((x) * 10))
100 #define SI5341_IN_PSET(x) (0x020E + ((x) * 10))
101 #define SI5341_PX_UPD 0x0230
103 /* PLL configuration */
104 #define SI5341_PLL_M_NUM 0x0235
105 #define SI5341_PLL_M_DEN 0x023B
107 /* Output configuration */
108 #define SI5341_OUT_CONFIG(output) \
109 ((output)->data->reg_output_offset[(output)->index])
110 #define SI5341_OUT_FORMAT(output) (SI5341_OUT_CONFIG(output) + 1)
111 #define SI5341_OUT_CM(output) (SI5341_OUT_CONFIG(output) + 2)
112 #define SI5341_OUT_MUX_SEL(output) (SI5341_OUT_CONFIG(output) + 3)
113 #define SI5341_OUT_R_REG(output) \
114 ((output)->data->reg_rdiv_offset[(output)->index])
116 /* Synthesize N divider */
117 #define SI5341_SYNTH_N_NUM(x) (0x0302 + ((x) * 11))
118 #define SI5341_SYNTH_N_DEN(x) (0x0308 + ((x) * 11))
119 #define SI5341_SYNTH_N_UPD(x) (0x030C + ((x) * 11))
121 /* Synthesizer output enable, phase bypass, power mode */
122 #define SI5341_SYNTH_N_CLK_TO_OUTX_EN 0x0A03
123 #define SI5341_SYNTH_N_PIBYP 0x0A04
124 #define SI5341_SYNTH_N_PDNB 0x0A05
125 #define SI5341_SYNTH_N_CLK_DIS 0x0B4A
127 #define SI5341_REGISTER_MAX 0xBFF
129 /* SI5341_OUT_CONFIG bits */
130 #define SI5341_OUT_CFG_PDN BIT(0)
131 #define SI5341_OUT_CFG_OE BIT(1)
132 #define SI5341_OUT_CFG_RDIV_FORCE2 BIT(2)
134 /* Static configuration (to be moved to firmware) */
135 struct si5341_reg_default
{
140 static const char * const si5341_input_clock_names
[] = {
141 "in0", "in1", "in2", "xtal"
144 /* Output configuration registers 0..9 are not quite logically organized */
145 static const u16 si5341_reg_output_offset
[] = {
158 static const u16 si5340_reg_output_offset
[] = {
165 /* The location of the R divider registers */
166 static const u16 si5341_reg_rdiv_offset
[] = {
178 static const u16 si5340_reg_rdiv_offset
[] = {
186 * Programming sequence from ClockBuilder, settings to initialize the system
187 * using only the XTAL input, without pre-divider.
188 * This also contains settings that aren't mentioned anywhere in the datasheet.
189 * The "known" settings like synth and output configuration are done later.
191 static const struct si5341_reg_default si5341_reg_defaults
[] = {
192 { 0x0017, 0x3A }, /* INT mask (disable interrupts) */
193 { 0x0018, 0xFF }, /* INT mask */
194 { 0x0021, 0x0F }, /* Select XTAL as input */
195 { 0x0022, 0x00 }, /* Not in datasheet */
196 { 0x002B, 0x02 }, /* SPI config */
197 { 0x002C, 0x20 }, /* LOS enable for XTAL */
198 { 0x002D, 0x00 }, /* LOS timing */
209 { 0x0038, 0x00 }, /* LOS setting (thresholds) */
214 { 0x003D, 0x00 }, /* LOS setting (thresholds) end */
215 { 0x0041, 0x00 }, /* LOS0_DIV_SEL */
216 { 0x0042, 0x00 }, /* LOS1_DIV_SEL */
217 { 0x0043, 0x00 }, /* LOS2_DIV_SEL */
218 { 0x0044, 0x00 }, /* LOS3_DIV_SEL */
219 { 0x009E, 0x00 }, /* Not in datasheet */
220 { 0x0102, 0x01 }, /* Enable outputs */
221 { 0x013F, 0x00 }, /* Not in datasheet */
222 { 0x0140, 0x00 }, /* Not in datasheet */
223 { 0x0141, 0x40 }, /* OUT LOS */
224 { 0x0202, 0x00 }, /* XAXB_FREQ_OFFSET (=0)*/
228 { 0x0206, 0x00 }, /* PXAXB (2^x) */
229 { 0x0208, 0x00 }, /* Px divider setting (usually 0) */
268 { 0x022F, 0x00 }, /* Px divider setting (usually 0) end */
269 { 0x026B, 0x00 }, /* DESIGN_ID (ASCII string) */
276 { 0x0272, 0x00 }, /* DESIGN_ID (ASCII string) end */
277 { 0x0339, 0x1F }, /* N_FSTEP_MSK */
278 { 0x033B, 0x00 }, /* Nx_FSTEPW (Frequency step) */
307 { 0x0358, 0x00 }, /* Nx_FSTEPW (Frequency step) end */
308 { 0x0359, 0x00 }, /* Nx_DELAY */
317 { 0x0362, 0x00 }, /* Nx_DELAY end */
318 { 0x0802, 0x00 }, /* Not in datasheet */
319 { 0x0803, 0x00 }, /* Not in datasheet */
320 { 0x0804, 0x00 }, /* Not in datasheet */
321 { 0x090E, 0x02 }, /* XAXB_EXTCLK_EN=0 XAXB_PDNB=1 (use XTAL) */
322 { 0x091C, 0x04 }, /* ZDM_EN=4 (Normal mode) */
323 { 0x0943, 0x00 }, /* IO_VDD_SEL=0 (0=1v8, use 1=3v3) */
324 { 0x0949, 0x00 }, /* IN_EN (disable input clocks) */
325 { 0x094A, 0x00 }, /* INx_TO_PFD_EN (disabled) */
326 { 0x0A02, 0x00 }, /* Not in datasheet */
327 { 0x0B44, 0x0F }, /* PDIV_ENB (datasheet does not mention what it is) */
330 /* Read and interpret a 44-bit followed by a 32-bit value in the regmap */
331 static int si5341_decode_44_32(struct regmap
*regmap
, unsigned int reg
,
332 u64
*val1
, u32
*val2
)
337 err
= regmap_bulk_read(regmap
, reg
, r
, 10);
341 *val1
= ((u64
)((r
[5] & 0x0f) << 8 | r
[4]) << 32) |
342 (get_unaligned_le32(r
));
343 *val2
= get_unaligned_le32(&r
[6]);
348 static int si5341_encode_44_32(struct regmap
*regmap
, unsigned int reg
,
349 u64 n_num
, u32 n_den
)
353 /* Shift left as far as possible without overflowing */
354 while (!(n_num
& BIT_ULL(43)) && !(n_den
& BIT(31))) {
359 /* 44 bits (6 bytes) numerator */
360 put_unaligned_le32(n_num
, r
);
361 r
[4] = (n_num
>> 32) & 0xff;
362 r
[5] = (n_num
>> 40) & 0x0f;
363 /* 32 bits denominator */
364 put_unaligned_le32(n_den
, &r
[6]);
366 /* Program the fraction */
367 return regmap_bulk_write(regmap
, reg
, r
, sizeof(r
));
370 /* VCO, we assume it runs at a constant frequency */
371 static unsigned long si5341_clk_recalc_rate(struct clk_hw
*hw
,
372 unsigned long parent_rate
)
374 struct clk_si5341
*data
= to_clk_si5341(hw
);
381 /* Assume that PDIV is not being used, just read the PLL setting */
382 err
= si5341_decode_44_32(data
->regmap
, SI5341_PLL_M_NUM
,
387 if (!m_num
|| !m_den
)
391 * Though m_num is 64-bit, only the upper bits are actually used. While
392 * calculating m_num and m_den, they are shifted as far as possible to
393 * the left. To avoid 96-bit division here, we just shift them back so
394 * we can do with just 64 bits.
398 while (res
& 0xffff00000000ULL
) {
403 do_div(res
, (m_den
>> shift
));
405 /* We cannot return the actual frequency in 32 bit, store it locally */
406 data
->freq_vco
= res
;
408 /* Report kHz since the value is out of range */
411 return (unsigned long)res
;
414 static int si5341_clk_get_selected_input(struct clk_si5341
*data
)
419 err
= regmap_read(data
->regmap
, SI5341_IN_SEL
, &val
);
423 return (val
& SI5341_IN_SEL_MASK
) >> SI5341_IN_SEL_SHIFT
;
426 static u8
si5341_clk_get_parent(struct clk_hw
*hw
)
428 struct clk_si5341
*data
= to_clk_si5341(hw
);
429 int res
= si5341_clk_get_selected_input(data
);
432 return 0; /* Apparently we cannot report errors */
437 static int si5341_clk_reparent(struct clk_si5341
*data
, u8 index
)
442 val
= (index
<< SI5341_IN_SEL_SHIFT
) & SI5341_IN_SEL_MASK
;
443 /* Enable register-based input selection */
444 val
|= SI5341_IN_SEL_REGCTRL
;
446 err
= regmap_update_bits(data
->regmap
,
447 SI5341_IN_SEL
, SI5341_IN_SEL_REGCTRL
| SI5341_IN_SEL_MASK
, val
);
452 /* Enable input buffer for selected input */
453 err
= regmap_update_bits(data
->regmap
,
454 SI5341_IN_EN
, 0x07, BIT(index
));
458 /* Enables the input to phase detector */
459 err
= regmap_update_bits(data
->regmap
, SI5341_INX_TO_PFD_EN
,
460 0x7 << SI5341_INX_TO_PFD_SHIFT
,
461 BIT(index
+ SI5341_INX_TO_PFD_SHIFT
));
465 /* Power down XTAL oscillator and buffer */
466 err
= regmap_update_bits(data
->regmap
, SI5341_XAXB_CFG
,
467 SI5341_XAXB_CFG_PDNB
, 0);
472 * Set the P divider to "1". There's no explanation in the
473 * datasheet of these registers, but the clockbuilder software
474 * programs a "1" when the input is being used.
476 err
= regmap_write(data
->regmap
, SI5341_IN_PDIV(index
), 1);
480 err
= regmap_write(data
->regmap
, SI5341_IN_PSET(index
), 1);
484 /* Set update PDIV bit */
485 err
= regmap_write(data
->regmap
, SI5341_PX_UPD
, BIT(index
));
489 /* Disable all input buffers */
490 err
= regmap_update_bits(data
->regmap
, SI5341_IN_EN
, 0x07, 0);
494 /* Disable input to phase detector */
495 err
= regmap_update_bits(data
->regmap
, SI5341_INX_TO_PFD_EN
,
496 0x7 << SI5341_INX_TO_PFD_SHIFT
, 0);
500 /* Power up XTAL oscillator and buffer */
501 err
= regmap_update_bits(data
->regmap
, SI5341_XAXB_CFG
,
502 SI5341_XAXB_CFG_PDNB
, SI5341_XAXB_CFG_PDNB
);
510 static int si5341_clk_set_parent(struct clk_hw
*hw
, u8 index
)
512 struct clk_si5341
*data
= to_clk_si5341(hw
);
514 return si5341_clk_reparent(data
, index
);
517 static const struct clk_ops si5341_clk_ops
= {
518 .set_parent
= si5341_clk_set_parent
,
519 .get_parent
= si5341_clk_get_parent
,
520 .recalc_rate
= si5341_clk_recalc_rate
,
523 /* Synthesizers, there are 5 synthesizers that connect to any of the outputs */
525 /* The synthesizer is on if all power and enable bits are set */
526 static int si5341_synth_clk_is_on(struct clk_hw
*hw
)
528 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
531 u8 index
= synth
->index
;
533 err
= regmap_read(synth
->data
->regmap
,
534 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, &val
);
538 if (!(val
& BIT(index
)))
541 err
= regmap_read(synth
->data
->regmap
, SI5341_SYNTH_N_PDNB
, &val
);
545 if (!(val
& BIT(index
)))
548 /* This bit must be 0 for the synthesizer to receive clock input */
549 err
= regmap_read(synth
->data
->regmap
, SI5341_SYNTH_N_CLK_DIS
, &val
);
553 return !(val
& BIT(index
));
556 static void si5341_synth_clk_unprepare(struct clk_hw
*hw
)
558 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
559 u8 index
= synth
->index
; /* In range 0..5 */
560 u8 mask
= BIT(index
);
563 regmap_update_bits(synth
->data
->regmap
,
564 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, mask
, 0);
566 regmap_update_bits(synth
->data
->regmap
,
567 SI5341_SYNTH_N_PDNB
, mask
, 0);
568 /* Disable clock input to synth (set to 1 to disable) */
569 regmap_update_bits(synth
->data
->regmap
,
570 SI5341_SYNTH_N_CLK_DIS
, mask
, mask
);
573 static int si5341_synth_clk_prepare(struct clk_hw
*hw
)
575 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
577 u8 index
= synth
->index
;
578 u8 mask
= BIT(index
);
581 err
= regmap_update_bits(synth
->data
->regmap
,
582 SI5341_SYNTH_N_PDNB
, mask
, mask
);
586 /* Enable clock input to synth (set bit to 0 to enable) */
587 err
= regmap_update_bits(synth
->data
->regmap
,
588 SI5341_SYNTH_N_CLK_DIS
, mask
, 0);
593 return regmap_update_bits(synth
->data
->regmap
,
594 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, mask
, mask
);
597 /* Synth clock frequency: Fvco * n_den / n_den, with Fvco in 13500-14256 MHz */
598 static unsigned long si5341_synth_clk_recalc_rate(struct clk_hw
*hw
,
599 unsigned long parent_rate
)
601 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
607 err
= si5341_decode_44_32(synth
->data
->regmap
,
608 SI5341_SYNTH_N_NUM(synth
->index
), &n_num
, &n_den
);
613 * n_num and n_den are shifted left as much as possible, so to prevent
614 * overflow in 64-bit math, we shift n_den 4 bits to the right
616 f
= synth
->data
->freq_vco
;
619 /* Now we need to to 64-bit division: f/n_num */
620 /* And compensate for the 4 bits we dropped */
621 f
= div64_u64(f
, (n_num
>> 4));
626 static long si5341_synth_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
627 unsigned long *parent_rate
)
629 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
632 /* The synthesizer accuracy is such that anything in range will work */
633 f
= synth
->data
->freq_vco
;
634 do_div(f
, SI5341_SYNTH_N_MAX
);
638 f
= synth
->data
->freq_vco
;
639 do_div(f
, SI5341_SYNTH_N_MIN
);
646 static int si5341_synth_program(struct clk_si5341_synth
*synth
,
647 u64 n_num
, u32 n_den
, bool is_integer
)
650 u8 index
= synth
->index
;
652 err
= si5341_encode_44_32(synth
->data
->regmap
,
653 SI5341_SYNTH_N_NUM(index
), n_num
, n_den
);
655 err
= regmap_update_bits(synth
->data
->regmap
,
656 SI5341_SYNTH_N_PIBYP
, BIT(index
), is_integer
? BIT(index
) : 0);
660 return regmap_write(synth
->data
->regmap
,
661 SI5341_SYNTH_N_UPD(index
), 0x01);
665 static int si5341_synth_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
666 unsigned long parent_rate
)
668 struct clk_si5341_synth
*synth
= to_clk_si5341_synth(hw
);
675 n_num
= synth
->data
->freq_vco
;
677 /* see if there's an integer solution */
678 r
= do_div(n_num
, rate
);
679 is_integer
= (r
== 0);
681 /* Integer divider equal to n_num */
684 /* Calculate a fractional solution */
691 dev_dbg(&synth
->data
->i2c_client
->dev
,
692 "%s(%u): n=0x%llx d=0x%x %s\n", __func__
,
693 synth
->index
, n_num
, n_den
,
694 is_integer
? "int" : "frac");
696 return si5341_synth_program(synth
, n_num
, n_den
, is_integer
);
699 static const struct clk_ops si5341_synth_clk_ops
= {
700 .is_prepared
= si5341_synth_clk_is_on
,
701 .prepare
= si5341_synth_clk_prepare
,
702 .unprepare
= si5341_synth_clk_unprepare
,
703 .recalc_rate
= si5341_synth_clk_recalc_rate
,
704 .round_rate
= si5341_synth_clk_round_rate
,
705 .set_rate
= si5341_synth_clk_set_rate
,
708 static int si5341_output_clk_is_on(struct clk_hw
*hw
)
710 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
714 err
= regmap_read(output
->data
->regmap
,
715 SI5341_OUT_CONFIG(output
), &val
);
719 /* Bit 0=PDN, 1=OE so only a value of 0x2 enables the output */
720 return (val
& 0x03) == SI5341_OUT_CFG_OE
;
723 /* Disables and then powers down the output */
724 static void si5341_output_clk_unprepare(struct clk_hw
*hw
)
726 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
728 regmap_update_bits(output
->data
->regmap
,
729 SI5341_OUT_CONFIG(output
),
730 SI5341_OUT_CFG_OE
, 0);
731 regmap_update_bits(output
->data
->regmap
,
732 SI5341_OUT_CONFIG(output
),
733 SI5341_OUT_CFG_PDN
, SI5341_OUT_CFG_PDN
);
736 /* Powers up and then enables the output */
737 static int si5341_output_clk_prepare(struct clk_hw
*hw
)
739 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
742 err
= regmap_update_bits(output
->data
->regmap
,
743 SI5341_OUT_CONFIG(output
),
744 SI5341_OUT_CFG_PDN
, 0);
748 return regmap_update_bits(output
->data
->regmap
,
749 SI5341_OUT_CONFIG(output
),
750 SI5341_OUT_CFG_OE
, SI5341_OUT_CFG_OE
);
753 static unsigned long si5341_output_clk_recalc_rate(struct clk_hw
*hw
,
754 unsigned long parent_rate
)
756 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
762 err
= regmap_bulk_read(output
->data
->regmap
,
763 SI5341_OUT_R_REG(output
), r
, 3);
767 /* Calculate value as 24-bit integer*/
768 r_divider
= r
[2] << 16 | r
[1] << 8 | r
[0];
770 /* If Rx_REG is zero, the divider is disabled, so return a "0" rate */
774 /* Divider is 2*(Rx_REG+1) */
778 err
= regmap_read(output
->data
->regmap
,
779 SI5341_OUT_CONFIG(output
), &val
);
783 if (val
& SI5341_OUT_CFG_RDIV_FORCE2
)
786 return parent_rate
/ r_divider
;
789 static long si5341_output_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
790 unsigned long *parent_rate
)
794 r
= *parent_rate
>> 1;
796 /* If rate is an even divisor, no changes to parent required */
797 if (r
&& !(r
% rate
))
800 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
) {
801 if (rate
> 200000000) {
802 /* minimum r-divider is 2 */
805 /* Take a parent frequency near 400 MHz */
806 r
= (400000000u / rate
) & ~1;
808 *parent_rate
= r
* rate
;
810 /* We cannot change our parent's rate, report what we can do */
812 rate
= *parent_rate
/ (r
<< 1);
818 static int si5341_output_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
819 unsigned long parent_rate
)
821 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
822 /* Frequency divider is (r_div + 1) * 2 */
823 u32 r_div
= (parent_rate
/ rate
) >> 1;
829 else if (r_div
>= BIT(24))
834 /* For a value of "2", we set the "OUT0_RDIV_FORCE2" bit */
835 err
= regmap_update_bits(output
->data
->regmap
,
836 SI5341_OUT_CONFIG(output
),
837 SI5341_OUT_CFG_RDIV_FORCE2
,
838 (r_div
== 0) ? SI5341_OUT_CFG_RDIV_FORCE2
: 0);
842 /* Always write Rx_REG, because a zero value disables the divider */
843 r
[0] = r_div
? (r_div
& 0xff) : 1;
844 r
[1] = (r_div
>> 8) & 0xff;
845 r
[2] = (r_div
>> 16) & 0xff;
846 err
= regmap_bulk_write(output
->data
->regmap
,
847 SI5341_OUT_R_REG(output
), r
, 3);
852 static int si5341_output_reparent(struct clk_si5341_output
*output
, u8 index
)
854 return regmap_update_bits(output
->data
->regmap
,
855 SI5341_OUT_MUX_SEL(output
), 0x07, index
);
858 static int si5341_output_set_parent(struct clk_hw
*hw
, u8 index
)
860 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
862 if (index
>= output
->data
->num_synth
)
865 return si5341_output_reparent(output
, index
);
868 static u8
si5341_output_get_parent(struct clk_hw
*hw
)
870 struct clk_si5341_output
*output
= to_clk_si5341_output(hw
);
874 err
= regmap_read(output
->data
->regmap
,
875 SI5341_OUT_MUX_SEL(output
), &val
);
880 static const struct clk_ops si5341_output_clk_ops
= {
881 .is_prepared
= si5341_output_clk_is_on
,
882 .prepare
= si5341_output_clk_prepare
,
883 .unprepare
= si5341_output_clk_unprepare
,
884 .recalc_rate
= si5341_output_clk_recalc_rate
,
885 .round_rate
= si5341_output_clk_round_rate
,
886 .set_rate
= si5341_output_clk_set_rate
,
887 .set_parent
= si5341_output_set_parent
,
888 .get_parent
= si5341_output_get_parent
,
892 * The chip can be bought in a pre-programmed version, or one can program the
893 * NVM in the chip to boot up in a preset mode. This routine tries to determine
894 * if that's the case, or if we need to reset and program everything from
895 * scratch. Returns negative error, or true/false.
897 static int si5341_is_programmed_already(struct clk_si5341
*data
)
902 /* Read the PLL divider value, it must have a non-zero value */
903 err
= regmap_bulk_read(data
->regmap
, SI5341_PLL_M_DEN
,
908 return !!get_unaligned_le32(r
);
911 static struct clk_hw
*
912 of_clk_si5341_get(struct of_phandle_args
*clkspec
, void *_data
)
914 struct clk_si5341
*data
= _data
;
915 unsigned int idx
= clkspec
->args
[1];
916 unsigned int group
= clkspec
->args
[0];
920 if (idx
>= data
->num_outputs
) {
921 dev_err(&data
->i2c_client
->dev
,
922 "invalid output index %u\n", idx
);
923 return ERR_PTR(-EINVAL
);
925 return &data
->clk
[idx
].hw
;
927 if (idx
>= data
->num_synth
) {
928 dev_err(&data
->i2c_client
->dev
,
929 "invalid synthesizer index %u\n", idx
);
930 return ERR_PTR(-EINVAL
);
932 return &data
->synth
[idx
].hw
;
935 dev_err(&data
->i2c_client
->dev
,
936 "invalid PLL index %u\n", idx
);
937 return ERR_PTR(-EINVAL
);
941 dev_err(&data
->i2c_client
->dev
, "invalid group %u\n", group
);
942 return ERR_PTR(-EINVAL
);
946 static int si5341_probe_chip_id(struct clk_si5341
*data
)
952 err
= regmap_bulk_read(data
->regmap
, SI5341_PN_BASE
, reg
,
955 dev_err(&data
->i2c_client
->dev
, "Failed to read chip ID\n");
959 model
= get_unaligned_le16(reg
);
961 dev_info(&data
->i2c_client
->dev
, "Chip: %x Grade: %u Rev: %u\n",
962 model
, reg
[2], reg
[3]);
966 data
->num_outputs
= SI5340_MAX_NUM_OUTPUTS
;
967 data
->num_synth
= SI5340_NUM_SYNTH
;
968 data
->reg_output_offset
= si5340_reg_output_offset
;
969 data
->reg_rdiv_offset
= si5340_reg_rdiv_offset
;
972 data
->num_outputs
= SI5341_MAX_NUM_OUTPUTS
;
973 data
->num_synth
= SI5341_NUM_SYNTH
;
974 data
->reg_output_offset
= si5341_reg_output_offset
;
975 data
->reg_rdiv_offset
= si5341_reg_rdiv_offset
;
978 dev_err(&data
->i2c_client
->dev
, "Model '%x' not supported\n",
986 /* Read active settings into the regmap cache for later reference */
987 static int si5341_read_settings(struct clk_si5341
*data
)
993 err
= regmap_bulk_read(data
->regmap
, SI5341_PLL_M_NUM
, r
, 10);
997 err
= regmap_bulk_read(data
->regmap
,
998 SI5341_SYNTH_N_CLK_TO_OUTX_EN
, r
, 3);
1002 err
= regmap_bulk_read(data
->regmap
,
1003 SI5341_SYNTH_N_CLK_DIS
, r
, 1);
1007 for (i
= 0; i
< data
->num_synth
; ++i
) {
1008 err
= regmap_bulk_read(data
->regmap
,
1009 SI5341_SYNTH_N_NUM(i
), r
, 10);
1014 for (i
= 0; i
< data
->num_outputs
; ++i
) {
1015 err
= regmap_bulk_read(data
->regmap
,
1016 data
->reg_output_offset
[i
], r
, 4);
1020 err
= regmap_bulk_read(data
->regmap
,
1021 data
->reg_rdiv_offset
[i
], r
, 3);
1029 static int si5341_write_multiple(struct clk_si5341
*data
,
1030 const struct si5341_reg_default
*values
, unsigned int num_values
)
1035 for (i
= 0; i
< num_values
; ++i
) {
1036 res
= regmap_write(data
->regmap
,
1037 values
[i
].address
, values
[i
].value
);
1039 dev_err(&data
->i2c_client
->dev
,
1040 "Failed to write %#x:%#x\n",
1041 values
[i
].address
, values
[i
].value
);
1049 static const struct si5341_reg_default si5341_preamble
[] = {
1057 static int si5341_send_preamble(struct clk_si5341
*data
)
1062 /* For revision 2 and up, the values are slightly different */
1063 res
= regmap_read(data
->regmap
, SI5341_DEVICE_REV
, &revision
);
1067 /* Write "preamble" as specified by datasheet */
1068 res
= regmap_write(data
->regmap
, 0xB24, revision
< 2 ? 0xD8 : 0xC0);
1071 res
= si5341_write_multiple(data
,
1072 si5341_preamble
, ARRAY_SIZE(si5341_preamble
));
1076 /* Datasheet specifies a 300ms wait after sending the preamble */
1082 /* Perform a soft reset and write post-amble */
1083 static int si5341_finalize_defaults(struct clk_si5341
*data
)
1088 res
= regmap_read(data
->regmap
, SI5341_DEVICE_REV
, &revision
);
1092 dev_dbg(&data
->i2c_client
->dev
, "%s rev=%u\n", __func__
, revision
);
1094 res
= regmap_write(data
->regmap
, SI5341_SOFT_RST
, 0x01);
1098 /* Datasheet does not explain these nameless registers */
1099 res
= regmap_write(data
->regmap
, 0xB24, revision
< 2 ? 0xDB : 0xC3);
1102 res
= regmap_write(data
->regmap
, 0x0B25, 0x02);
1110 static const struct regmap_range si5341_regmap_volatile_range
[] = {
1111 regmap_reg_range(0x000C, 0x0012), /* Status */
1112 regmap_reg_range(0x001C, 0x001E), /* reset, finc/fdec */
1113 regmap_reg_range(0x00E2, 0x00FE), /* NVM, interrupts, device ready */
1114 /* Update bits for P divider and synth config */
1115 regmap_reg_range(SI5341_PX_UPD
, SI5341_PX_UPD
),
1116 regmap_reg_range(SI5341_SYNTH_N_UPD(0), SI5341_SYNTH_N_UPD(0)),
1117 regmap_reg_range(SI5341_SYNTH_N_UPD(1), SI5341_SYNTH_N_UPD(1)),
1118 regmap_reg_range(SI5341_SYNTH_N_UPD(2), SI5341_SYNTH_N_UPD(2)),
1119 regmap_reg_range(SI5341_SYNTH_N_UPD(3), SI5341_SYNTH_N_UPD(3)),
1120 regmap_reg_range(SI5341_SYNTH_N_UPD(4), SI5341_SYNTH_N_UPD(4)),
1123 static const struct regmap_access_table si5341_regmap_volatile
= {
1124 .yes_ranges
= si5341_regmap_volatile_range
,
1125 .n_yes_ranges
= ARRAY_SIZE(si5341_regmap_volatile_range
),
1128 /* Pages 0, 1, 2, 3, 9, A, B are valid, so there are 12 pages */
1129 static const struct regmap_range_cfg si5341_regmap_ranges
[] = {
1132 .range_max
= SI5341_REGISTER_MAX
,
1133 .selector_reg
= SI5341_PAGE
,
1134 .selector_mask
= 0xff,
1135 .selector_shift
= 0,
1141 static const struct regmap_config si5341_regmap_config
= {
1144 .cache_type
= REGCACHE_RBTREE
,
1145 .ranges
= si5341_regmap_ranges
,
1146 .num_ranges
= ARRAY_SIZE(si5341_regmap_ranges
),
1147 .max_register
= SI5341_REGISTER_MAX
,
1148 .volatile_table
= &si5341_regmap_volatile
,
1151 static int si5341_dt_parse_dt(struct i2c_client
*client
,
1152 struct clk_si5341_output_config
*config
)
1154 struct device_node
*child
;
1155 struct device_node
*np
= client
->dev
.of_node
;
1159 memset(config
, 0, sizeof(struct clk_si5341_output_config
) *
1160 SI5341_MAX_NUM_OUTPUTS
);
1162 for_each_child_of_node(np
, child
) {
1163 if (of_property_read_u32(child
, "reg", &num
)) {
1164 dev_err(&client
->dev
, "missing reg property of %s\n",
1169 if (num
>= SI5341_MAX_NUM_OUTPUTS
) {
1170 dev_err(&client
->dev
, "invalid clkout %d\n", num
);
1174 if (!of_property_read_u32(child
, "silabs,format", &val
)) {
1175 /* Set cm and ampl conservatively to 3v3 settings */
1177 case 1: /* normal differential */
1178 config
[num
].out_cm_ampl_bits
= 0x33;
1180 case 2: /* low-power differential */
1181 config
[num
].out_cm_ampl_bits
= 0x13;
1183 case 4: /* LVCMOS */
1184 config
[num
].out_cm_ampl_bits
= 0x33;
1185 /* Set SI recommended impedance for LVCMOS */
1186 config
[num
].out_format_drv_bits
|= 0xc0;
1189 dev_err(&client
->dev
,
1190 "invalid silabs,format %u for %u\n",
1194 config
[num
].out_format_drv_bits
&= ~0x07;
1195 config
[num
].out_format_drv_bits
|= val
& 0x07;
1196 /* Always enable the SYNC feature */
1197 config
[num
].out_format_drv_bits
|= 0x08;
1200 if (!of_property_read_u32(child
, "silabs,common-mode", &val
)) {
1202 dev_err(&client
->dev
,
1203 "invalid silabs,common-mode %u\n",
1207 config
[num
].out_cm_ampl_bits
&= 0xf0;
1208 config
[num
].out_cm_ampl_bits
|= val
& 0x0f;
1211 if (!of_property_read_u32(child
, "silabs,amplitude", &val
)) {
1213 dev_err(&client
->dev
,
1214 "invalid silabs,amplitude %u\n",
1218 config
[num
].out_cm_ampl_bits
&= 0x0f;
1219 config
[num
].out_cm_ampl_bits
|= (val
<< 4) & 0xf0;
1222 if (of_property_read_bool(child
, "silabs,disable-high"))
1223 config
[num
].out_format_drv_bits
|= 0x10;
1225 config
[num
].synth_master
=
1226 of_property_read_bool(child
, "silabs,synth-master");
1228 config
[num
].always_on
=
1229 of_property_read_bool(child
, "always-on");
1240 * If not pre-configured, calculate and set the PLL configuration manually.
1241 * For low-jitter performance, the PLL should be set such that the synthesizers
1242 * only need integer division.
1243 * Without any user guidance, we'll set the PLL to 14GHz, which still allows
1244 * the chip to generate any frequency on its outputs, but jitter performance
1245 * may be sub-optimal.
1247 static int si5341_initialize_pll(struct clk_si5341
*data
)
1249 struct device_node
*np
= data
->i2c_client
->dev
.of_node
;
1254 if (of_property_read_u32(np
, "silabs,pll-m-num", &m_num
)) {
1255 dev_err(&data
->i2c_client
->dev
,
1256 "PLL configuration requires silabs,pll-m-num\n");
1258 if (of_property_read_u32(np
, "silabs,pll-m-den", &m_den
)) {
1259 dev_err(&data
->i2c_client
->dev
,
1260 "PLL configuration requires silabs,pll-m-den\n");
1263 if (!m_num
|| !m_den
) {
1264 dev_err(&data
->i2c_client
->dev
,
1265 "PLL configuration invalid, assume 14GHz\n");
1266 sel
= si5341_clk_get_selected_input(data
);
1270 m_den
= clk_get_rate(data
->input_clk
[sel
]) / 10;
1274 return si5341_encode_44_32(data
->regmap
,
1275 SI5341_PLL_M_NUM
, m_num
, m_den
);
1278 static int si5341_clk_select_active_input(struct clk_si5341
*data
)
1284 res
= si5341_clk_get_selected_input(data
);
1288 /* If the current register setting is invalid, pick the first input */
1289 if (!data
->input_clk
[res
]) {
1290 dev_dbg(&data
->i2c_client
->dev
,
1291 "Input %d not connected, rerouting\n", res
);
1293 for (i
= 0; i
< SI5341_NUM_INPUTS
; ++i
) {
1294 if (data
->input_clk
[i
]) {
1300 dev_err(&data
->i2c_client
->dev
,
1301 "No clock input available\n");
1306 /* Make sure the selected clock is also enabled and routed */
1307 err
= si5341_clk_reparent(data
, res
);
1311 err
= clk_prepare_enable(data
->input_clk
[res
]);
1318 static int si5341_probe(struct i2c_client
*client
,
1319 const struct i2c_device_id
*id
)
1321 struct clk_si5341
*data
;
1322 struct clk_init_data init
;
1324 const char *root_clock_name
;
1325 const char *synth_clock_names
[SI5341_NUM_SYNTH
];
1328 struct clk_si5341_output_config config
[SI5341_MAX_NUM_OUTPUTS
];
1329 bool initialization_required
;
1331 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1335 data
->i2c_client
= client
;
1337 for (i
= 0; i
< SI5341_NUM_INPUTS
; ++i
) {
1338 input
= devm_clk_get(&client
->dev
, si5341_input_clock_names
[i
]);
1339 if (IS_ERR(input
)) {
1340 if (PTR_ERR(input
) == -EPROBE_DEFER
)
1341 return -EPROBE_DEFER
;
1342 data
->input_clk_name
[i
] = si5341_input_clock_names
[i
];
1344 data
->input_clk
[i
] = input
;
1345 data
->input_clk_name
[i
] = __clk_get_name(input
);
1349 err
= si5341_dt_parse_dt(client
, config
);
1353 if (of_property_read_string(client
->dev
.of_node
, "clock-output-names",
1355 init
.name
= client
->dev
.of_node
->name
;
1356 root_clock_name
= init
.name
;
1358 data
->regmap
= devm_regmap_init_i2c(client
, &si5341_regmap_config
);
1359 if (IS_ERR(data
->regmap
))
1360 return PTR_ERR(data
->regmap
);
1362 i2c_set_clientdata(client
, data
);
1364 err
= si5341_probe_chip_id(data
);
1368 if (of_property_read_bool(client
->dev
.of_node
, "silabs,reprogram")) {
1369 initialization_required
= true;
1371 err
= si5341_is_programmed_already(data
);
1375 initialization_required
= !err
;
1378 if (initialization_required
) {
1379 /* Populate the regmap cache in preparation for "cache only" */
1380 err
= si5341_read_settings(data
);
1384 err
= si5341_send_preamble(data
);
1389 * We intend to send all 'final' register values in a single
1390 * transaction. So cache all register writes until we're done
1393 regcache_cache_only(data
->regmap
, true);
1395 /* Write the configuration pairs from the firmware blob */
1396 err
= si5341_write_multiple(data
, si5341_reg_defaults
,
1397 ARRAY_SIZE(si5341_reg_defaults
));
1402 /* Input must be up and running at this point */
1403 err
= si5341_clk_select_active_input(data
);
1407 if (initialization_required
) {
1408 /* PLL configuration is required */
1409 err
= si5341_initialize_pll(data
);
1414 /* Register the PLL */
1415 init
.parent_names
= data
->input_clk_name
;
1416 init
.num_parents
= SI5341_NUM_INPUTS
;
1417 init
.ops
= &si5341_clk_ops
;
1419 data
->hw
.init
= &init
;
1421 err
= devm_clk_hw_register(&client
->dev
, &data
->hw
);
1423 dev_err(&client
->dev
, "clock registration failed\n");
1427 init
.num_parents
= 1;
1428 init
.parent_names
= &root_clock_name
;
1429 init
.ops
= &si5341_synth_clk_ops
;
1430 for (i
= 0; i
< data
->num_synth
; ++i
) {
1431 synth_clock_names
[i
] = devm_kasprintf(&client
->dev
, GFP_KERNEL
,
1432 "%s.N%u", client
->dev
.of_node
->name
, i
);
1433 init
.name
= synth_clock_names
[i
];
1434 data
->synth
[i
].index
= i
;
1435 data
->synth
[i
].data
= data
;
1436 data
->synth
[i
].hw
.init
= &init
;
1437 err
= devm_clk_hw_register(&client
->dev
, &data
->synth
[i
].hw
);
1439 dev_err(&client
->dev
,
1440 "synth N%u registration failed\n", i
);
1444 init
.num_parents
= data
->num_synth
;
1445 init
.parent_names
= synth_clock_names
;
1446 init
.ops
= &si5341_output_clk_ops
;
1447 for (i
= 0; i
< data
->num_outputs
; ++i
) {
1448 init
.name
= kasprintf(GFP_KERNEL
, "%s.%d",
1449 client
->dev
.of_node
->name
, i
);
1450 init
.flags
= config
[i
].synth_master
? CLK_SET_RATE_PARENT
: 0;
1451 data
->clk
[i
].index
= i
;
1452 data
->clk
[i
].data
= data
;
1453 data
->clk
[i
].hw
.init
= &init
;
1454 if (config
[i
].out_format_drv_bits
& 0x07) {
1455 regmap_write(data
->regmap
,
1456 SI5341_OUT_FORMAT(&data
->clk
[i
]),
1457 config
[i
].out_format_drv_bits
);
1458 regmap_write(data
->regmap
,
1459 SI5341_OUT_CM(&data
->clk
[i
]),
1460 config
[i
].out_cm_ampl_bits
);
1462 err
= devm_clk_hw_register(&client
->dev
, &data
->clk
[i
].hw
);
1463 kfree(init
.name
); /* clock framework made a copy of the name */
1465 dev_err(&client
->dev
,
1466 "output %u registration failed\n", i
);
1469 if (config
[i
].always_on
)
1470 clk_prepare(data
->clk
[i
].hw
.clk
);
1473 err
= of_clk_add_hw_provider(client
->dev
.of_node
, of_clk_si5341_get
,
1476 dev_err(&client
->dev
, "unable to add clk provider\n");
1480 if (initialization_required
) {
1482 regcache_cache_only(data
->regmap
, false);
1483 err
= regcache_sync(data
->regmap
);
1487 err
= si5341_finalize_defaults(data
);
1492 /* Free the names, clk framework makes copies */
1493 for (i
= 0; i
< data
->num_synth
; ++i
)
1494 devm_kfree(&client
->dev
, (void *)synth_clock_names
[i
]);
1499 static const struct i2c_device_id si5341_id
[] = {
1504 MODULE_DEVICE_TABLE(i2c
, si5341_id
);
1506 static const struct of_device_id clk_si5341_of_match
[] = {
1507 { .compatible
= "silabs,si5340" },
1508 { .compatible
= "silabs,si5341" },
1511 MODULE_DEVICE_TABLE(of
, clk_si5341_of_match
);
1513 static struct i2c_driver si5341_driver
= {
1516 .of_match_table
= clk_si5341_of_match
,
1518 .probe
= si5341_probe
,
1519 .id_table
= si5341_id
,
1521 module_i2c_driver(si5341_driver
);
1523 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
1524 MODULE_DESCRIPTION("Si5341 driver");
1525 MODULE_LICENSE("GPL");