1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Silicon Labs Si570/Si571 Programmable XO/VCXO
5 * Copyright (C) 2010, 2011 Ericsson AB.
6 * Copyright (C) 2011 Guenter Roeck.
7 * Copyright (C) 2011 - 2021 Xilinx Inc.
9 * Author: Guenter Roeck <guenter.roeck@ericsson.com>
10 * Sören Brinkmann <soren.brinkmann@xilinx.com>
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
22 #define SI570_REG_HS_N1 7
23 #define SI570_REG_N1_RFREQ0 8
24 #define SI570_REG_RFREQ1 9
25 #define SI570_REG_RFREQ2 10
26 #define SI570_REG_RFREQ3 11
27 #define SI570_REG_RFREQ4 12
28 #define SI570_REG_CONTROL 135
29 #define SI570_REG_FREEZE_DCO 137
30 #define SI570_DIV_OFFSET_7PPM 6
32 #define HS_DIV_SHIFT 5
33 #define HS_DIV_MASK 0xe0
34 #define HS_DIV_OFFSET 4
35 #define N1_6_2_MASK 0x1f
36 #define N1_1_0_MASK 0xc0
37 #define RFREQ_37_32_MASK 0x3f
39 #define SI570_MIN_FREQ 10000000L
40 #define SI570_MAX_FREQ 1417500000L
41 #define SI598_MAX_FREQ 525000000L
43 #define FDCO_MIN 4850000000LL
44 #define FDCO_MAX 5670000000LL
46 #define SI570_CNTRL_RECALL (1 << 0)
47 #define SI570_CNTRL_FREEZE_M (1 << 5)
48 #define SI570_CNTRL_NEWFREQ (1 << 6)
50 #define SI570_FREEZE_DCO (1 << 4)
54 * @hw: Clock hw struct
55 * @regmap: Device's regmap
56 * @div_offset: Rgister offset for dividers
57 * @max_freq: Maximum frequency for this device
58 * @fxtal: Factory xtal frequency
59 * @n1: Clock divider N1
60 * @hs_div: Clock divider HSDIV
61 * @rfreq: Clock multiplier RFREQ
62 * @frequency: Current output frequency
63 * @i2c_client: I2C client pointer
67 struct regmap *regmap;
68 unsigned int div_offset;
75 struct i2c_client *i2c_client;
77 #define to_clk_si570(_hw) container_of(_hw, struct clk_si570, hw)
79 enum clk_si570_variant {
85 * si570_get_divs() - Read clock dividers from HW
86 * @data: Pointer to struct clk_si570
87 * @rfreq: Fractional multiplier (output)
88 * @n1: Divider N1 (output)
89 * @hs_div: Divider HSDIV (output)
90 * Returns 0 on success, negative errno otherwise.
92 * Retrieve clock dividers and multipliers from the HW.
94 static int si570_get_divs(struct clk_si570 *data, u64 *rfreq,
95 unsigned int *n1, unsigned int *hs_div)
101 err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset,
102 reg, ARRAY_SIZE(reg));
106 *hs_div = ((reg[0] & HS_DIV_MASK) >> HS_DIV_SHIFT) + HS_DIV_OFFSET;
107 *n1 = ((reg[0] & N1_6_2_MASK) << 2) + ((reg[1] & N1_1_0_MASK) >> 6) + 1;
108 /* Handle invalid cases */
112 tmp = reg[1] & RFREQ_37_32_MASK;
113 tmp = (tmp << 8) + reg[2];
114 tmp = (tmp << 8) + reg[3];
115 tmp = (tmp << 8) + reg[4];
116 tmp = (tmp << 8) + reg[5];
123 * si570_get_defaults() - Get default values
124 * @data: Driver data structure
125 * @fout: Factory frequency output
126 * @skip_recall: If true, don't recall NVM into RAM
127 * Returns 0 on success, negative errno otherwise.
129 static int si570_get_defaults(struct clk_si570 *data, u64 fout,
136 regmap_write(data->regmap, SI570_REG_CONTROL,
139 err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div);
144 * Accept optional precision loss to avoid arithmetic overflows.
145 * Acceptable per Silicon Labs Application Note AN334.
147 fdco = fout * data->n1 * data->hs_div;
148 if (fdco >= (1LL << 36))
149 data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4);
151 data->fxtal = div64_u64(fdco << 28, data->rfreq);
153 data->frequency = fout;
159 * si570_update_rfreq() - Update clock multiplier
160 * @data: Driver data structure
161 * Passes on regmap_bulk_write() return value.
163 static int si570_update_rfreq(struct clk_si570 *data)
167 reg[0] = ((data->n1 - 1) << 6) |
168 ((data->rfreq >> 32) & RFREQ_37_32_MASK);
169 reg[1] = (data->rfreq >> 24) & 0xff;
170 reg[2] = (data->rfreq >> 16) & 0xff;
171 reg[3] = (data->rfreq >> 8) & 0xff;
172 reg[4] = data->rfreq & 0xff;
174 return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 +
175 data->div_offset, reg, ARRAY_SIZE(reg));
179 * si570_calc_divs() - Caluclate clock dividers
180 * @frequency: Target frequency
181 * @data: Driver data structure
182 * @out_rfreq: RFREG fractional multiplier (output)
183 * @out_n1: Clock divider N1 (output)
184 * @out_hs_div: Clock divider HSDIV (output)
185 * Returns 0 on success, negative errno otherwise.
187 * Calculate the clock dividers (@out_hs_div, @out_n1) and clock multiplier
188 * (@out_rfreq) for a given target @frequency.
190 static int si570_calc_divs(unsigned long frequency, struct clk_si570 *data,
191 u64 *out_rfreq, unsigned int *out_n1, unsigned int *out_hs_div)
194 unsigned int n1, hs_div;
195 u64 fdco, best_fdco = ULLONG_MAX;
196 static const uint8_t si570_hs_div_values[] = { 11, 9, 7, 6, 5, 4 };
198 for (i = 0; i < ARRAY_SIZE(si570_hs_div_values); i++) {
199 hs_div = si570_hs_div_values[i];
200 /* Calculate lowest possible value for n1 */
201 n1 = div_u64(div_u64(FDCO_MIN, hs_div), frequency);
205 fdco = (u64)frequency * (u64)hs_div * (u64)n1;
208 if (fdco >= FDCO_MIN && fdco < best_fdco) {
210 *out_hs_div = hs_div;
211 *out_rfreq = div64_u64(fdco << 28, data->fxtal);
214 n1 += (n1 == 1 ? 1 : 2);
218 if (best_fdco == ULLONG_MAX)
224 static unsigned long si570_recalc_rate(struct clk_hw *hw,
225 unsigned long parent_rate)
229 unsigned int n1, hs_div;
230 struct clk_si570 *data = to_clk_si570(hw);
232 err = si570_get_divs(data, &rfreq, &n1, &hs_div);
234 dev_err(&data->i2c_client->dev, "unable to recalc rate\n");
235 return data->frequency;
238 rfreq = div_u64(rfreq, hs_div * n1);
239 rate = (data->fxtal * rfreq) >> 28;
244 static long si570_round_rate(struct clk_hw *hw, unsigned long rate,
245 unsigned long *parent_rate)
249 unsigned int n1, hs_div;
250 struct clk_si570 *data = to_clk_si570(hw);
255 if (div64_u64(abs(rate - data->frequency) * 10000LL,
256 data->frequency) < 35) {
257 rfreq = div64_u64((data->rfreq * rate) +
258 div64_u64(data->frequency, 2), data->frequency);
260 hs_div = data->hs_div;
263 err = si570_calc_divs(rate, data, &rfreq, &n1, &hs_div);
265 dev_err(&data->i2c_client->dev,
266 "unable to round rate\n");
275 * si570_set_frequency() - Adjust output frequency
276 * @data: Driver data structure
277 * @frequency: Target frequency
278 * Returns 0 on success.
280 * Update output frequency for big frequency changes (> 3,500 ppm).
282 static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency)
286 err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1,
292 * The DCO reg should be accessed with a read-modify-write operation
295 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, SI570_FREEZE_DCO);
296 regmap_write(data->regmap, SI570_REG_HS_N1 + data->div_offset,
297 ((data->hs_div - HS_DIV_OFFSET) << HS_DIV_SHIFT) |
298 (((data->n1 - 1) >> 2) & N1_6_2_MASK));
299 si570_update_rfreq(data);
300 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, 0);
301 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_NEWFREQ);
303 /* Applying a new frequency can take up to 10ms */
304 usleep_range(10000, 12000);
310 * si570_set_frequency_small() - Adjust output frequency
311 * @data: Driver data structure
312 * @frequency: Target frequency
313 * Returns 0 on success.
315 * Update output frequency for small frequency changes (< 3,500 ppm).
317 static int si570_set_frequency_small(struct clk_si570 *data,
318 unsigned long frequency)
321 * This is a re-implementation of DIV_ROUND_CLOSEST
322 * using the div64_u64 function lieu of letting the compiler
325 data->rfreq = div64_u64((data->rfreq * frequency) +
326 div_u64(data->frequency, 2), data->frequency);
327 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_FREEZE_M);
328 si570_update_rfreq(data);
329 regmap_write(data->regmap, SI570_REG_CONTROL, 0);
331 /* Applying a new frequency (small change) can take up to 100us */
332 usleep_range(100, 200);
337 static int si570_set_rate(struct clk_hw *hw, unsigned long rate,
338 unsigned long parent_rate)
340 struct clk_si570 *data = to_clk_si570(hw);
341 struct i2c_client *client = data->i2c_client;
344 if (rate < SI570_MIN_FREQ || rate > data->max_freq) {
345 dev_err(&client->dev,
346 "requested frequency %lu Hz is out of range\n", rate);
350 if (div64_u64(abs(rate - data->frequency) * 10000LL,
351 data->frequency) < 35)
352 err = si570_set_frequency_small(data, rate);
354 err = si570_set_frequency(data, rate);
359 data->frequency = rate;
364 static const struct clk_ops si570_clk_ops = {
365 .recalc_rate = si570_recalc_rate,
366 .round_rate = si570_round_rate,
367 .set_rate = si570_set_rate,
370 static bool si570_regmap_is_volatile(struct device *dev, unsigned int reg)
373 case SI570_REG_CONTROL:
380 static bool si570_regmap_is_writeable(struct device *dev, unsigned int reg)
383 case SI570_REG_HS_N1 ... (SI570_REG_RFREQ4 + SI570_DIV_OFFSET_7PPM):
384 case SI570_REG_CONTROL:
385 case SI570_REG_FREEZE_DCO:
392 static const struct regmap_config si570_regmap_config = {
395 .cache_type = REGCACHE_RBTREE,
397 .writeable_reg = si570_regmap_is_writeable,
398 .volatile_reg = si570_regmap_is_volatile,
401 static const struct i2c_device_id si570_id[] = {
408 MODULE_DEVICE_TABLE(i2c, si570_id);
410 static int si570_probe(struct i2c_client *client)
412 struct clk_si570 *data;
413 struct clk_init_data init;
414 const struct i2c_device_id *id = i2c_match_id(si570_id, client);
415 u32 initial_fout, factory_fout, stability;
418 enum clk_si570_variant variant = id->driver_data;
420 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
424 init.ops = &si570_clk_ops;
426 init.num_parents = 0;
427 data->hw.init = &init;
428 data->i2c_client = client;
430 if (variant == si57x) {
431 err = of_property_read_u32(client->dev.of_node,
432 "temperature-stability", &stability);
434 dev_err(&client->dev,
435 "'temperature-stability' property missing\n");
438 /* adjust register offsets for 7ppm devices */
440 data->div_offset = SI570_DIV_OFFSET_7PPM;
442 data->max_freq = SI570_MAX_FREQ;
444 data->max_freq = SI598_MAX_FREQ;
447 if (of_property_read_string(client->dev.of_node, "clock-output-names",
449 init.name = client->dev.of_node->name;
451 err = of_property_read_u32(client->dev.of_node, "factory-fout",
454 dev_err(&client->dev, "'factory-fout' property missing\n");
458 skip_recall = of_property_read_bool(client->dev.of_node,
459 "silabs,skip-recall");
461 data->regmap = devm_regmap_init_i2c(client, &si570_regmap_config);
462 if (IS_ERR(data->regmap)) {
463 dev_err(&client->dev, "failed to allocate register map\n");
464 return PTR_ERR(data->regmap);
467 i2c_set_clientdata(client, data);
468 err = si570_get_defaults(data, factory_fout, skip_recall);
472 err = devm_clk_hw_register(&client->dev, &data->hw);
474 dev_err(&client->dev, "clock registration failed\n");
477 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_hw_simple_get,
480 dev_err(&client->dev, "unable to add clk provider\n");
484 /* Read the requested initial output frequency from device tree */
485 if (!of_property_read_u32(client->dev.of_node, "clock-frequency",
487 err = clk_set_rate(data->hw.clk, initial_fout);
489 of_clk_del_provider(client->dev.of_node);
494 /* Display a message indicating that we've successfully registered */
495 dev_info(&client->dev, "registered, current frequency %llu Hz\n",
501 static int si570_remove(struct i2c_client *client)
503 of_clk_del_provider(client->dev.of_node);
507 static const struct of_device_id clk_si570_of_match[] = {
508 { .compatible = "silabs,si570" },
509 { .compatible = "silabs,si571" },
510 { .compatible = "silabs,si598" },
511 { .compatible = "silabs,si599" },
514 MODULE_DEVICE_TABLE(of, clk_si570_of_match);
516 static struct i2c_driver si570_driver = {
519 .of_match_table = clk_si570_of_match,
521 .probe_new = si570_probe,
522 .remove = si570_remove,
523 .id_table = si570_id,
525 module_i2c_driver(si570_driver);
527 MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
528 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
529 MODULE_DESCRIPTION("Si570 driver");
530 MODULE_LICENSE("GPL");