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
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);
889 err = regmap_read(output->data->regmap,
890 SI5341_OUT_MUX_SEL(output), &val);
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,
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.
912 static int si5341_is_programmed_already(struct clk_si5341 *data)
917 /* Read the PLL divider value, it must have a non-zero value */
918 err = regmap_bulk_read(data->regmap, SI5341_PLL_M_DEN,
923 return !!get_unaligned_le32(r);
926 static struct clk_hw *
927 of_clk_si5341_get(struct of_phandle_args *clkspec, void *_data)
929 struct clk_si5341 *data = _data;
930 unsigned int idx = clkspec->args[1];
931 unsigned int group = clkspec->args[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);
940 return &data->clk[idx].hw;
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);
947 return &data->synth[idx].hw;
950 dev_err(&data->i2c_client->dev,
951 "invalid PLL index %u\n", idx);
952 return ERR_PTR(-EINVAL);
956 dev_err(&data->i2c_client->dev, "invalid group %u\n", group);
957 return ERR_PTR(-EINVAL);
961 static int si5341_probe_chip_id(struct clk_si5341 *data)
967 err = regmap_bulk_read(data->regmap, SI5341_PN_BASE, reg,
970 dev_err(&data->i2c_client->dev, "Failed to read chip ID\n");
974 model = get_unaligned_le16(reg);
976 dev_info(&data->i2c_client->dev, "Chip: %x Grade: %u Rev: %u\n",
977 model, reg[2], reg[3]);
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;
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;
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;
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;
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;
1011 dev_err(&data->i2c_client->dev, "Model '%x' not supported\n",
1016 data->chip_id = model;
1021 /* Read active settings into the regmap cache for later reference */
1022 static int si5341_read_settings(struct clk_si5341 *data)
1028 err = regmap_bulk_read(data->regmap, SI5341_PLL_M_NUM, r, 10);
1032 err = regmap_bulk_read(data->regmap,
1033 SI5341_SYNTH_N_CLK_TO_OUTX_EN, r, 3);
1037 err = regmap_bulk_read(data->regmap,
1038 SI5341_SYNTH_N_CLK_DIS, r, 1);
1042 for (i = 0; i < data->num_synth; ++i) {
1043 err = regmap_bulk_read(data->regmap,
1044 SI5341_SYNTH_N_NUM(i), r, 10);
1049 for (i = 0; i < data->num_outputs; ++i) {
1050 err = regmap_bulk_read(data->regmap,
1051 data->reg_output_offset[i], r, 4);
1055 err = regmap_bulk_read(data->regmap,
1056 data->reg_rdiv_offset[i], r, 3);
1064 static int si5341_write_multiple(struct clk_si5341 *data,
1065 const struct si5341_reg_default *values, unsigned int num_values)
1070 for (i = 0; i < num_values; ++i) {
1071 res = regmap_write(data->regmap,
1072 values[i].address, values[i].value);
1074 dev_err(&data->i2c_client->dev,
1075 "Failed to write %#x:%#x\n",
1076 values[i].address, values[i].value);
1084 static const struct si5341_reg_default si5341_preamble[] = {
1092 static const struct si5341_reg_default si5345_preamble[] = {
1097 static int si5341_send_preamble(struct clk_si5341 *data)
1102 /* For revision 2 and up, the values are slightly different */
1103 res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
1107 /* Write "preamble" as specified by datasheet */
1108 res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xD8 : 0xC0);
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));
1117 res = si5341_write_multiple(data,
1118 si5341_preamble, ARRAY_SIZE(si5341_preamble));
1122 /* Datasheet specifies a 300ms wait after sending the preamble */
1128 /* Perform a soft reset and write post-amble */
1129 static int si5341_finalize_defaults(struct clk_si5341 *data)
1134 res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
1138 dev_dbg(&data->i2c_client->dev, "%s rev=%u\n", __func__, revision);
1140 res = regmap_write(data->regmap, SI5341_SOFT_RST, 0x01);
1144 /* The si5342..si5345 have an additional post-amble */
1145 if (data->chip_id > 0x5341) {
1146 res = regmap_write(data->regmap, 0x540, 0x0);
1151 /* Datasheet does not explain these nameless registers */
1152 res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xDB : 0xC3);
1155 res = regmap_write(data->regmap, 0x0B25, 0x02);
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)),
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),
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[] = {
1185 .range_max = SI5341_REGISTER_MAX,
1186 .selector_reg = SI5341_PAGE,
1187 .selector_mask = 0xff,
1188 .selector_shift = 0,
1194 static const struct regmap_config si5341_regmap_config = {
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,
1204 static int si5341_dt_parse_dt(struct i2c_client *client,
1205 struct clk_si5341_output_config *config)
1207 struct device_node *child;
1208 struct device_node *np = client->dev.of_node;
1212 memset(config, 0, sizeof(struct clk_si5341_output_config) *
1213 SI5341_MAX_NUM_OUTPUTS);
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",
1222 if (num >= SI5341_MAX_NUM_OUTPUTS) {
1223 dev_err(&client->dev, "invalid clkout %d\n", num);
1227 if (!of_property_read_u32(child, "silabs,format", &val)) {
1228 /* Set cm and ampl conservatively to 3v3 settings */
1230 case 1: /* normal differential */
1231 config[num].out_cm_ampl_bits = 0x33;
1233 case 2: /* low-power differential */
1234 config[num].out_cm_ampl_bits = 0x13;
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;
1242 dev_err(&client->dev,
1243 "invalid silabs,format %u for %u\n",
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;
1253 if (!of_property_read_u32(child, "silabs,common-mode", &val)) {
1255 dev_err(&client->dev,
1256 "invalid silabs,common-mode %u\n",
1260 config[num].out_cm_ampl_bits &= 0xf0;
1261 config[num].out_cm_ampl_bits |= val & 0x0f;
1264 if (!of_property_read_u32(child, "silabs,amplitude", &val)) {
1266 dev_err(&client->dev,
1267 "invalid silabs,amplitude %u\n",
1271 config[num].out_cm_ampl_bits &= 0x0f;
1272 config[num].out_cm_ampl_bits |= (val << 4) & 0xf0;
1275 if (of_property_read_bool(child, "silabs,disable-high"))
1276 config[num].out_format_drv_bits |= 0x10;
1278 config[num].synth_master =
1279 of_property_read_bool(child, "silabs,synth-master");
1281 config[num].always_on =
1282 of_property_read_bool(child, "always-on");
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.
1300 static int si5341_initialize_pll(struct clk_si5341 *data)
1302 struct device_node *np = data->i2c_client->dev.of_node;
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");
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");
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);
1323 m_den = clk_get_rate(data->input_clk[sel]) / 10;
1327 return si5341_encode_44_32(data->regmap,
1328 SI5341_PLL_M_NUM, m_num, m_den);
1331 static int si5341_clk_select_active_input(struct clk_si5341 *data)
1337 res = si5341_clk_get_selected_input(data);
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);
1346 for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
1347 if (data->input_clk[i]) {
1353 dev_err(&data->i2c_client->dev,
1354 "No clock input available\n");
1359 /* Make sure the selected clock is also enabled and routed */
1360 err = si5341_clk_reparent(data, res);
1364 err = clk_prepare_enable(data->input_clk[res]);
1371 static int si5341_probe(struct i2c_client *client,
1372 const struct i2c_device_id *id)
1374 struct clk_si5341 *data;
1375 struct clk_init_data init;
1377 const char *root_clock_name;
1378 const char *synth_clock_names[SI5341_NUM_SYNTH];
1381 struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS];
1382 bool initialization_required;
1384 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1388 data->i2c_client = client;
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];
1397 data->input_clk[i] = input;
1398 data->input_clk_name[i] = __clk_get_name(input);
1402 err = si5341_dt_parse_dt(client, config);
1406 if (of_property_read_string(client->dev.of_node, "clock-output-names",
1408 init.name = client->dev.of_node->name;
1409 root_clock_name = init.name;
1411 data->regmap = devm_regmap_init_i2c(client, &si5341_regmap_config);
1412 if (IS_ERR(data->regmap))
1413 return PTR_ERR(data->regmap);
1415 i2c_set_clientdata(client, data);
1417 err = si5341_probe_chip_id(data);
1421 if (of_property_read_bool(client->dev.of_node, "silabs,reprogram")) {
1422 initialization_required = true;
1424 err = si5341_is_programmed_already(data);
1428 initialization_required = !err;
1431 if (initialization_required) {
1432 /* Populate the regmap cache in preparation for "cache only" */
1433 err = si5341_read_settings(data);
1437 err = si5341_send_preamble(data);
1442 * We intend to send all 'final' register values in a single
1443 * transaction. So cache all register writes until we're done
1446 regcache_cache_only(data->regmap, true);
1448 /* Write the configuration pairs from the firmware blob */
1449 err = si5341_write_multiple(data, si5341_reg_defaults,
1450 ARRAY_SIZE(si5341_reg_defaults));
1455 /* Input must be up and running at this point */
1456 err = si5341_clk_select_active_input(data);
1460 if (initialization_required) {
1461 /* PLL configuration is required */
1462 err = si5341_initialize_pll(data);
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;
1472 data->hw.init = &init;
1474 err = devm_clk_hw_register(&client->dev, &data->hw);
1476 dev_err(&client->dev, "clock registration failed\n");
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);
1492 dev_err(&client->dev,
1493 "synth N%u registration failed\n", i);
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);
1515 err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
1516 kfree(init.name); /* clock framework made a copy of the name */
1518 dev_err(&client->dev,
1519 "output %u registration failed\n", i);
1522 if (config[i].always_on)
1523 clk_prepare(data->clk[i].hw.clk);
1526 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_si5341_get,
1529 dev_err(&client->dev, "unable to add clk provider\n");
1533 if (initialization_required) {
1535 regcache_cache_only(data->regmap, false);
1536 err = regcache_sync(data->regmap);
1540 err = si5341_finalize_defaults(data);
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]);
1552 static const struct i2c_device_id si5341_id[] = {
1560 MODULE_DEVICE_TABLE(i2c, si5341_id);
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" },
1570 MODULE_DEVICE_TABLE(of, clk_si5341_of_match);
1572 static struct i2c_driver si5341_driver = {
1575 .of_match_table = clk_si5341_of_match,
1577 .probe = si5341_probe,
1578 .id_table = si5341_id,
1580 module_i2c_driver(si5341_driver);
1583 MODULE_DESCRIPTION("Si5341 driver");
1584 MODULE_LICENSE("GPL");