Lines Matching +full:clock +full:- +full:frequency
1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Copyright (C) 2011 - 2021 Xilinx Inc.
14 #include <linux/clk-provider.h>
54 * @hw: Clock hw struct
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
74 u64 frequency; member
85 * si570_get_divs() - Read clock dividers from HW
92 * Retrieve clock dividers and multipliers from the HW.
101 err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset, in si570_get_divs()
123 * si570_get_defaults() - Get default values
125 * @fout: Factory frequency output
136 regmap_write(data->regmap, SI570_REG_CONTROL, in si570_get_defaults()
139 err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div); in si570_get_defaults()
147 fdco = fout * data->n1 * data->hs_div; in si570_get_defaults()
149 data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4); in si570_get_defaults()
151 data->fxtal = div64_u64(fdco << 28, data->rfreq); in si570_get_defaults()
153 data->frequency = fout; in si570_get_defaults()
159 * si570_update_rfreq() - Update clock multiplier
167 reg[0] = ((data->n1 - 1) << 6) | in si570_update_rfreq()
168 ((data->rfreq >> 32) & RFREQ_37_32_MASK); in si570_update_rfreq()
169 reg[1] = (data->rfreq >> 24) & 0xff; in si570_update_rfreq()
170 reg[2] = (data->rfreq >> 16) & 0xff; in si570_update_rfreq()
171 reg[3] = (data->rfreq >> 8) & 0xff; in si570_update_rfreq()
172 reg[4] = data->rfreq & 0xff; in si570_update_rfreq()
174 return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 + in si570_update_rfreq()
175 data->div_offset, reg, ARRAY_SIZE(reg)); in si570_update_rfreq()
179 * si570_calc_divs() - Caluclate clock dividers
180 * @frequency: Target frequency
183 * @out_n1: Clock divider N1 (output)
184 * @out_hs_div: Clock divider HSDIV (output)
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, in si570_calc_divs() argument
201 n1 = div_u64(div_u64(FDCO_MIN, hs_div), frequency); in si570_calc_divs()
205 fdco = (u64)frequency * (u64)hs_div * (u64)n1; in si570_calc_divs()
211 *out_rfreq = div64_u64(fdco << 28, data->fxtal); in si570_calc_divs()
219 return -EINVAL; in si570_calc_divs()
234 dev_err(&data->i2c_client->dev, "unable to recalc rate\n"); in si570_recalc_rate()
235 return data->frequency; in si570_recalc_rate()
239 rate = (data->fxtal * rfreq) >> 28; in si570_recalc_rate()
255 if (div64_u64(abs(rate - data->frequency) * 10000LL, in si570_round_rate()
256 data->frequency) < 35) { in si570_round_rate()
257 rfreq = div64_u64((data->rfreq * rate) + in si570_round_rate()
258 div64_u64(data->frequency, 2), data->frequency); in si570_round_rate()
259 n1 = data->n1; in si570_round_rate()
260 hs_div = data->hs_div; in si570_round_rate()
265 dev_err(&data->i2c_client->dev, in si570_round_rate()
275 * si570_set_frequency() - Adjust output frequency
277 * @frequency: Target frequency
280 * Update output frequency for big frequency changes (> 3,500 ppm).
282 static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency) in si570_set_frequency() argument
286 err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1, in si570_set_frequency()
287 &data->hs_div); in si570_set_frequency()
292 * The DCO reg should be accessed with a read-modify-write operation in si570_set_frequency()
295 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, SI570_FREEZE_DCO); in si570_set_frequency()
296 regmap_write(data->regmap, SI570_REG_HS_N1 + data->div_offset, in si570_set_frequency()
297 ((data->hs_div - HS_DIV_OFFSET) << HS_DIV_SHIFT) | in si570_set_frequency()
298 (((data->n1 - 1) >> 2) & N1_6_2_MASK)); in si570_set_frequency()
300 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, 0); in si570_set_frequency()
301 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_NEWFREQ); in si570_set_frequency()
303 /* Applying a new frequency can take up to 10ms */ in si570_set_frequency()
310 * si570_set_frequency_small() - Adjust output frequency
312 * @frequency: Target frequency
315 * Update output frequency for small frequency changes (< 3,500 ppm).
318 unsigned long frequency) in si570_set_frequency_small() argument
321 * This is a re-implementation of DIV_ROUND_CLOSEST in si570_set_frequency_small()
325 data->rfreq = div64_u64((data->rfreq * frequency) + in si570_set_frequency_small()
326 div_u64(data->frequency, 2), data->frequency); in si570_set_frequency_small()
327 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_FREEZE_M); in si570_set_frequency_small()
329 regmap_write(data->regmap, SI570_REG_CONTROL, 0); in si570_set_frequency_small()
331 /* Applying a new frequency (small change) can take up to 100us */ in si570_set_frequency_small()
341 struct i2c_client *client = data->i2c_client; in si570_set_rate()
344 if (rate < SI570_MIN_FREQ || rate > data->max_freq) { in si570_set_rate()
345 dev_err(&client->dev, in si570_set_rate()
346 "requested frequency %lu Hz is out of range\n", rate); in si570_set_rate()
347 return -EINVAL; in si570_set_rate()
350 if (div64_u64(abs(rate - data->frequency) * 10000LL, in si570_set_rate()
351 data->frequency) < 35) in si570_set_rate()
359 data->frequency = rate; in si570_set_rate()
418 enum clk_si570_variant variant = id->driver_data; in si570_probe()
420 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); in si570_probe()
422 return -ENOMEM; in si570_probe()
427 data->hw.init = &init; in si570_probe()
428 data->i2c_client = client; in si570_probe()
431 err = of_property_read_u32(client->dev.of_node, in si570_probe()
432 "temperature-stability", &stability); in si570_probe()
434 dev_err(&client->dev, in si570_probe()
435 "'temperature-stability' property missing\n"); in si570_probe()
440 data->div_offset = SI570_DIV_OFFSET_7PPM; in si570_probe()
442 data->max_freq = SI570_MAX_FREQ; in si570_probe()
444 data->max_freq = SI598_MAX_FREQ; in si570_probe()
447 if (of_property_read_string(client->dev.of_node, "clock-output-names", in si570_probe()
449 init.name = client->dev.of_node->name; in si570_probe()
451 err = of_property_read_u32(client->dev.of_node, "factory-fout", in si570_probe()
454 dev_err(&client->dev, "'factory-fout' property missing\n"); in si570_probe()
458 skip_recall = of_property_read_bool(client->dev.of_node, in si570_probe()
459 "silabs,skip-recall"); in si570_probe()
461 data->regmap = devm_regmap_init_i2c(client, &si570_regmap_config); in si570_probe()
462 if (IS_ERR(data->regmap)) { in si570_probe()
463 dev_err(&client->dev, "failed to allocate register map\n"); in si570_probe()
464 return PTR_ERR(data->regmap); in si570_probe()
472 err = devm_clk_hw_register(&client->dev, &data->hw); in si570_probe()
474 dev_err(&client->dev, "clock registration failed\n"); in si570_probe()
477 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_hw_simple_get, in si570_probe()
478 &data->hw); in si570_probe()
480 dev_err(&client->dev, "unable to add clk provider\n"); in si570_probe()
484 /* Read the requested initial output frequency from device tree */ in si570_probe()
485 if (!of_property_read_u32(client->dev.of_node, "clock-frequency", in si570_probe()
487 err = clk_set_rate(data->hw.clk, initial_fout); in si570_probe()
489 of_clk_del_provider(client->dev.of_node); in si570_probe()
495 dev_info(&client->dev, "registered, current frequency %llu Hz\n", in si570_probe()
496 data->frequency); in si570_probe()
503 of_clk_del_provider(client->dev.of_node); in si570_remove()