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