Lines Matching +full:clk +full:- +full:out +full:- +full:frequency +full:- +full:hz

1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Copyright (C) 2011 - 2021 Xilinx Inc.
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
54 * @max_freq: Maximum frequency for this device
68 * @fxtal: Factory xtal frequency
72 * @frequency: Current output frequency
84 u64 frequency; member
90 * si570_get_divs() - Read clock dividers from HW
106 err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset, in si570_get_divs()
128 * si570_get_defaults() - Get default values
130 * @fout: Factory frequency output
141 regmap_write(data->regmap, SI570_REG_CONTROL, in si570_get_defaults()
144 err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div); in si570_get_defaults()
152 fdco = fout * data->n1 * data->hs_div; in si570_get_defaults()
154 data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4); in si570_get_defaults()
156 data->fxtal = div64_u64(fdco << 28, data->rfreq); in si570_get_defaults()
158 data->frequency = fout; in si570_get_defaults()
164 * si570_update_rfreq() - Update clock multiplier
172 reg[0] = ((data->n1 - 1) << 6) | in si570_update_rfreq()
173 ((data->rfreq >> 32) & RFREQ_37_32_MASK); in si570_update_rfreq()
174 reg[1] = (data->rfreq >> 24) & 0xff; in si570_update_rfreq()
175 reg[2] = (data->rfreq >> 16) & 0xff; in si570_update_rfreq()
176 reg[3] = (data->rfreq >> 8) & 0xff; in si570_update_rfreq()
177 reg[4] = data->rfreq & 0xff; in si570_update_rfreq()
179 return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 + in si570_update_rfreq()
180 data->div_offset, reg, ARRAY_SIZE(reg)); in si570_update_rfreq()
184 * si570_calc_divs() - Caluclate clock dividers
185 * @frequency: Target frequency
193 * (@out_rfreq) for a given target @frequency.
195 static int si570_calc_divs(unsigned long frequency, struct clk_si570 *data, in si570_calc_divs() argument
206 n1 = div_u64(div_u64(FDCO_MIN, hs_div), frequency); in si570_calc_divs()
210 fdco = (u64)frequency * (u64)hs_div * (u64)n1; in si570_calc_divs()
216 *out_rfreq = div64_u64(fdco << 28, data->fxtal); in si570_calc_divs()
224 return -EINVAL; in si570_calc_divs()
239 dev_err(&data->i2c_client->dev, "unable to recalc rate\n"); in si570_recalc_rate()
240 return data->frequency; in si570_recalc_rate()
244 rate = (data->fxtal * rfreq) >> 28; in si570_recalc_rate()
260 if (div64_u64(abs(rate - data->frequency) * 10000LL, in si570_round_rate()
261 data->frequency) < 35) { in si570_round_rate()
262 rfreq = div64_u64((data->rfreq * rate) + in si570_round_rate()
263 div64_u64(data->frequency, 2), data->frequency); in si570_round_rate()
264 n1 = data->n1; in si570_round_rate()
265 hs_div = data->hs_div; in si570_round_rate()
270 dev_err(&data->i2c_client->dev, in si570_round_rate()
280 * si570_set_frequency() - Adjust output frequency
282 * @frequency: Target frequency
285 * Update output frequency for big frequency changes (> 3,500 ppm).
287 static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency) in si570_set_frequency() argument
291 err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1, in si570_set_frequency()
292 &data->hs_div); in si570_set_frequency()
297 * The DCO reg should be accessed with a read-modify-write operation in si570_set_frequency()
300 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, SI570_FREEZE_DCO); in si570_set_frequency()
301 regmap_write(data->regmap, SI570_REG_HS_N1 + data->div_offset, in si570_set_frequency()
302 ((data->hs_div - HS_DIV_OFFSET) << HS_DIV_SHIFT) | in si570_set_frequency()
303 (((data->n1 - 1) >> 2) & N1_6_2_MASK)); in si570_set_frequency()
305 regmap_write(data->regmap, SI570_REG_FREEZE_DCO, 0); in si570_set_frequency()
306 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_NEWFREQ); in si570_set_frequency()
308 /* Applying a new frequency can take up to 10ms */ in si570_set_frequency()
315 * si570_set_frequency_small() - Adjust output frequency
317 * @frequency: Target frequency
320 * Update output frequency for small frequency changes (< 3,500 ppm).
323 unsigned long frequency) in si570_set_frequency_small() argument
326 * This is a re-implementation of DIV_ROUND_CLOSEST in si570_set_frequency_small()
330 data->rfreq = div64_u64((data->rfreq * frequency) + in si570_set_frequency_small()
331 div_u64(data->frequency, 2), data->frequency); in si570_set_frequency_small()
332 regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_FREEZE_M); in si570_set_frequency_small()
334 regmap_write(data->regmap, SI570_REG_CONTROL, 0); in si570_set_frequency_small()
336 /* Applying a new frequency (small change) can take up to 100us */ in si570_set_frequency_small()
346 struct i2c_client *client = data->i2c_client; in si570_set_rate()
349 if (rate < SI570_MIN_FREQ || rate > data->info->max_freq) { in si570_set_rate()
350 dev_err(&client->dev, in si570_set_rate()
351 "requested frequency %lu Hz is out of range\n", rate); in si570_set_rate()
352 return -EINVAL; in si570_set_rate()
355 if (div64_u64(abs(rate - data->frequency) * 10000LL, in si570_set_rate()
356 data->frequency) < 35) in si570_set_rate()
364 data->frequency = rate; in si570_set_rate()
414 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); in si570_probe()
416 return -ENOMEM; in si570_probe()
421 data->hw.init = &init; in si570_probe()
422 data->i2c_client = client; in si570_probe()
424 data->info = i2c_get_match_data(client); in si570_probe()
425 if (data->info->has_temperature_stability) { in si570_probe()
426 err = of_property_read_u32(client->dev.of_node, in si570_probe()
427 "temperature-stability", &stability); in si570_probe()
429 dev_err(&client->dev, in si570_probe()
430 "'temperature-stability' property missing\n"); in si570_probe()
435 data->div_offset = SI570_DIV_OFFSET_7PPM; in si570_probe()
438 if (of_property_read_string(client->dev.of_node, "clock-output-names", in si570_probe()
440 init.name = client->dev.of_node->name; in si570_probe()
442 err = of_property_read_u32(client->dev.of_node, "factory-fout", in si570_probe()
445 dev_err(&client->dev, "'factory-fout' property missing\n"); in si570_probe()
449 skip_recall = of_property_read_bool(client->dev.of_node, in si570_probe()
450 "silabs,skip-recall"); in si570_probe()
452 data->regmap = devm_regmap_init_i2c(client, &si570_regmap_config); in si570_probe()
453 if (IS_ERR(data->regmap)) { in si570_probe()
454 dev_err(&client->dev, "failed to allocate register map\n"); in si570_probe()
455 return PTR_ERR(data->regmap); in si570_probe()
463 err = devm_clk_hw_register(&client->dev, &data->hw); in si570_probe()
465 dev_err(&client->dev, "clock registration failed\n"); in si570_probe()
468 err = devm_of_clk_add_hw_provider(&client->dev, of_clk_hw_simple_get, in si570_probe()
469 &data->hw); in si570_probe()
471 dev_err(&client->dev, "unable to add clk provider\n"); in si570_probe()
475 /* Read the requested initial output frequency from device tree */ in si570_probe()
476 if (!of_property_read_u32(client->dev.of_node, "clock-frequency", in si570_probe()
478 err = clk_set_rate(data->hw.clk, initial_fout); in si570_probe()
484 dev_info(&client->dev, "registered, current frequency %llu Hz\n", in si570_probe()
485 data->frequency); in si570_probe()