xref: /openbmc/linux/drivers/clk/clk-si570.c (revision 4ce94eab)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Silicon Labs Si570/Si571 Programmable XO/VCXO
4  *
5  * Copyright (C) 2010, 2011 Ericsson AB.
6  * Copyright (C) 2011 Guenter Roeck.
7  * Copyright (C) 2011 - 2021 Xilinx Inc.
8  *
9  * Author: Guenter Roeck <guenter.roeck@ericsson.com>
10  *	   Sören Brinkmann <soren.brinkmann@xilinx.com>
11  */
12 
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>
20 
21 /* Si570 registers */
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
31 
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
38 
39 #define SI570_MIN_FREQ		10000000L
40 #define SI570_MAX_FREQ		1417500000L
41 #define SI598_MAX_FREQ		525000000L
42 
43 #define FDCO_MIN		4850000000LL
44 #define FDCO_MAX		5670000000LL
45 
46 #define SI570_CNTRL_RECALL	(1 << 0)
47 #define SI570_CNTRL_FREEZE_M	(1 << 5)
48 #define SI570_CNTRL_NEWFREQ	(1 << 6)
49 
50 #define SI570_FREEZE_DCO	(1 << 4)
51 
52 /**
53  * struct clk_si570:
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
64  */
65 struct clk_si570 {
66 	struct clk_hw hw;
67 	struct regmap *regmap;
68 	unsigned int div_offset;
69 	u64 max_freq;
70 	u64 fxtal;
71 	unsigned int n1;
72 	unsigned int hs_div;
73 	u64 rfreq;
74 	u64 frequency;
75 	struct i2c_client *i2c_client;
76 };
77 #define to_clk_si570(_hw)	container_of(_hw, struct clk_si570, hw)
78 
79 enum clk_si570_variant {
80 	si57x,
81 	si59x
82 };
83 
84 /**
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.
91  *
92  * Retrieve clock dividers and multipliers from the HW.
93  */
94 static int si570_get_divs(struct clk_si570 *data, u64 *rfreq,
95 		unsigned int *n1, unsigned int *hs_div)
96 {
97 	int err;
98 	u8 reg[6];
99 	u64 tmp;
100 
101 	err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset,
102 			reg, ARRAY_SIZE(reg));
103 	if (err)
104 		return err;
105 
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 */
109 	if (*n1 > 1)
110 		*n1 &= ~1;
111 
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];
117 	*rfreq = tmp;
118 
119 	return 0;
120 }
121 
122 /**
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.
128  */
129 static int si570_get_defaults(struct clk_si570 *data, u64 fout,
130 			      bool skip_recall)
131 {
132 	int err;
133 	u64 fdco;
134 
135 	if (!skip_recall)
136 		regmap_write(data->regmap, SI570_REG_CONTROL,
137 			     SI570_CNTRL_RECALL);
138 
139 	err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div);
140 	if (err)
141 		return err;
142 
143 	/*
144 	 * Accept optional precision loss to avoid arithmetic overflows.
145 	 * Acceptable per Silicon Labs Application Note AN334.
146 	 */
147 	fdco = fout * data->n1 * data->hs_div;
148 	if (fdco >= (1LL << 36))
149 		data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4);
150 	else
151 		data->fxtal = div64_u64(fdco << 28, data->rfreq);
152 
153 	data->frequency = fout;
154 
155 	return 0;
156 }
157 
158 /**
159  * si570_update_rfreq() - Update clock multiplier
160  * @data:	Driver data structure
161  * Passes on regmap_bulk_write() return value.
162  */
163 static int si570_update_rfreq(struct clk_si570 *data)
164 {
165 	u8 reg[5];
166 
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;
173 
174 	return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 +
175 			data->div_offset, reg, ARRAY_SIZE(reg));
176 }
177 
178 /**
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.
186  *
187  * Calculate the clock dividers (@out_hs_div, @out_n1) and clock multiplier
188  * (@out_rfreq) for a given target @frequency.
189  */
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)
192 {
193 	int i;
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 };
197 
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);
202 		if (!n1 || (n1 & 1))
203 			n1++;
204 		while (n1 <= 128) {
205 			fdco = (u64)frequency * (u64)hs_div * (u64)n1;
206 			if (fdco > FDCO_MAX)
207 				break;
208 			if (fdco >= FDCO_MIN && fdco < best_fdco) {
209 				*out_n1 = n1;
210 				*out_hs_div = hs_div;
211 				*out_rfreq = div64_u64(fdco << 28, data->fxtal);
212 				best_fdco = fdco;
213 			}
214 			n1 += (n1 == 1 ? 1 : 2);
215 		}
216 	}
217 
218 	if (best_fdco == ULLONG_MAX)
219 		return -EINVAL;
220 
221 	return 0;
222 }
223 
224 static unsigned long si570_recalc_rate(struct clk_hw *hw,
225 		unsigned long parent_rate)
226 {
227 	int err;
228 	u64 rfreq, rate;
229 	unsigned int n1, hs_div;
230 	struct clk_si570 *data = to_clk_si570(hw);
231 
232 	err = si570_get_divs(data, &rfreq, &n1, &hs_div);
233 	if (err) {
234 		dev_err(&data->i2c_client->dev, "unable to recalc rate\n");
235 		return data->frequency;
236 	}
237 
238 	rfreq = div_u64(rfreq, hs_div * n1);
239 	rate = (data->fxtal * rfreq) >> 28;
240 
241 	return rate;
242 }
243 
244 static long si570_round_rate(struct clk_hw *hw, unsigned long rate,
245 		unsigned long *parent_rate)
246 {
247 	int err;
248 	u64 rfreq;
249 	unsigned int n1, hs_div;
250 	struct clk_si570 *data = to_clk_si570(hw);
251 
252 	if (!rate)
253 		return 0;
254 
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);
259 		n1 = data->n1;
260 		hs_div = data->hs_div;
261 
262 	} else {
263 		err = si570_calc_divs(rate, data, &rfreq, &n1, &hs_div);
264 		if (err) {
265 			dev_err(&data->i2c_client->dev,
266 					"unable to round rate\n");
267 			return 0;
268 		}
269 	}
270 
271 	return rate;
272 }
273 
274 /**
275  * si570_set_frequency() - Adjust output frequency
276  * @data:	Driver data structure
277  * @frequency:	Target frequency
278  * Returns 0 on success.
279  *
280  * Update output frequency for big frequency changes (> 3,500 ppm).
281  */
282 static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency)
283 {
284 	int err;
285 
286 	err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1,
287 			&data->hs_div);
288 	if (err)
289 		return err;
290 
291 	/*
292 	 * The DCO reg should be accessed with a read-modify-write operation
293 	 * per AN334
294 	 */
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);
302 
303 	/* Applying a new frequency can take up to 10ms */
304 	usleep_range(10000, 12000);
305 
306 	return 0;
307 }
308 
309 /**
310  * si570_set_frequency_small() - Adjust output frequency
311  * @data:	Driver data structure
312  * @frequency:	Target frequency
313  * Returns 0 on success.
314  *
315  * Update output frequency for small frequency changes (< 3,500 ppm).
316  */
317 static int si570_set_frequency_small(struct clk_si570 *data,
318 				     unsigned long frequency)
319 {
320 	/*
321 	 * This is a re-implementation of DIV_ROUND_CLOSEST
322 	 * using the div64_u64 function lieu of letting the compiler
323 	 * insert EABI calls
324 	 */
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);
330 
331 	/* Applying a new frequency (small change) can take up to 100us */
332 	usleep_range(100, 200);
333 
334 	return 0;
335 }
336 
337 static int si570_set_rate(struct clk_hw *hw, unsigned long rate,
338 		unsigned long parent_rate)
339 {
340 	struct clk_si570 *data = to_clk_si570(hw);
341 	struct i2c_client *client = data->i2c_client;
342 	int err;
343 
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);
347 		return -EINVAL;
348 	}
349 
350 	if (div64_u64(abs(rate - data->frequency) * 10000LL,
351 				data->frequency) < 35)
352 		err = si570_set_frequency_small(data, rate);
353 	else
354 		err = si570_set_frequency(data, rate);
355 
356 	if (err)
357 		return err;
358 
359 	data->frequency = rate;
360 
361 	return 0;
362 }
363 
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,
368 };
369 
370 static bool si570_regmap_is_volatile(struct device *dev, unsigned int reg)
371 {
372 	switch (reg) {
373 	case SI570_REG_CONTROL:
374 		return true;
375 	default:
376 		return false;
377 	}
378 }
379 
380 static bool si570_regmap_is_writeable(struct device *dev, unsigned int reg)
381 {
382 	switch (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:
386 		return true;
387 	default:
388 		return false;
389 	}
390 }
391 
392 static const struct regmap_config si570_regmap_config = {
393 	.reg_bits = 8,
394 	.val_bits = 8,
395 	.cache_type = REGCACHE_RBTREE,
396 	.max_register = 137,
397 	.writeable_reg = si570_regmap_is_writeable,
398 	.volatile_reg = si570_regmap_is_volatile,
399 };
400 
401 static int si570_probe(struct i2c_client *client,
402 		const struct i2c_device_id *id)
403 {
404 	struct clk_si570 *data;
405 	struct clk_init_data init;
406 	u32 initial_fout, factory_fout, stability;
407 	bool skip_recall;
408 	int err;
409 	enum clk_si570_variant variant = id->driver_data;
410 
411 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
412 	if (!data)
413 		return -ENOMEM;
414 
415 	init.ops = &si570_clk_ops;
416 	init.flags = 0;
417 	init.num_parents = 0;
418 	data->hw.init = &init;
419 	data->i2c_client = client;
420 
421 	if (variant == si57x) {
422 		err = of_property_read_u32(client->dev.of_node,
423 				"temperature-stability", &stability);
424 		if (err) {
425 			dev_err(&client->dev,
426 				  "'temperature-stability' property missing\n");
427 			return err;
428 		}
429 		/* adjust register offsets for 7ppm devices */
430 		if (stability == 7)
431 			data->div_offset = SI570_DIV_OFFSET_7PPM;
432 
433 		data->max_freq = SI570_MAX_FREQ;
434 	} else {
435 		data->max_freq = SI598_MAX_FREQ;
436 	}
437 
438 	if (of_property_read_string(client->dev.of_node, "clock-output-names",
439 			&init.name))
440 		init.name = client->dev.of_node->name;
441 
442 	err = of_property_read_u32(client->dev.of_node, "factory-fout",
443 			&factory_fout);
444 	if (err) {
445 		dev_err(&client->dev, "'factory-fout' property missing\n");
446 		return err;
447 	}
448 
449 	skip_recall = of_property_read_bool(client->dev.of_node,
450 					    "silabs,skip-recall");
451 
452 	data->regmap = devm_regmap_init_i2c(client, &si570_regmap_config);
453 	if (IS_ERR(data->regmap)) {
454 		dev_err(&client->dev, "failed to allocate register map\n");
455 		return PTR_ERR(data->regmap);
456 	}
457 
458 	i2c_set_clientdata(client, data);
459 	err = si570_get_defaults(data, factory_fout, skip_recall);
460 	if (err)
461 		return err;
462 
463 	err = devm_clk_hw_register(&client->dev, &data->hw);
464 	if (err) {
465 		dev_err(&client->dev, "clock registration failed\n");
466 		return err;
467 	}
468 	err = of_clk_add_hw_provider(client->dev.of_node, of_clk_hw_simple_get,
469 				     &data->hw);
470 	if (err) {
471 		dev_err(&client->dev, "unable to add clk provider\n");
472 		return err;
473 	}
474 
475 	/* Read the requested initial output frequency from device tree */
476 	if (!of_property_read_u32(client->dev.of_node, "clock-frequency",
477 				&initial_fout)) {
478 		err = clk_set_rate(data->hw.clk, initial_fout);
479 		if (err) {
480 			of_clk_del_provider(client->dev.of_node);
481 			return err;
482 		}
483 	}
484 
485 	/* Display a message indicating that we've successfully registered */
486 	dev_info(&client->dev, "registered, current frequency %llu Hz\n",
487 			data->frequency);
488 
489 	return 0;
490 }
491 
492 static int si570_remove(struct i2c_client *client)
493 {
494 	of_clk_del_provider(client->dev.of_node);
495 	return 0;
496 }
497 
498 static const struct i2c_device_id si570_id[] = {
499 	{ "si570", si57x },
500 	{ "si571", si57x },
501 	{ "si598", si59x },
502 	{ "si599", si59x },
503 	{ }
504 };
505 MODULE_DEVICE_TABLE(i2c, si570_id);
506 
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" },
512 	{ },
513 };
514 MODULE_DEVICE_TABLE(of, clk_si570_of_match);
515 
516 static struct i2c_driver si570_driver = {
517 	.driver = {
518 		.name = "si570",
519 		.of_match_table = clk_si570_of_match,
520 	},
521 	.probe		= si570_probe,
522 	.remove		= si570_remove,
523 	.id_table	= si570_id,
524 };
525 module_i2c_driver(si570_driver);
526 
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");
531