Lines Matching +full:soc +full:- +full:s
1 // SPDX-License-Identifier: GPL-2.0-only
3 * clk-dfll.c - Tegra DFLL clock source common code
5 * Copyright (C) 2012-2019 NVIDIA Corporation. All rights reserved.
11 * SoC. These IP blocks together are also known at NVIDIA as
12 * "CL-DVFS". To try to avoid confusion, this code refers to them
18 * DFLL can be operated in either open-loop mode or closed-loop mode.
19 * In open-loop mode, the DFLL generates an output clock appropriate
20 * to the supply voltage. In closed-loop mode, when configured with a
25 * variation. In the case of the CPU, it's important to note that the
27 * performance-measurement code and any code that relies on the CPU
32 #include <linux/clk-provider.h>
49 #include "clk-dfll.h"
53 * DFLL control registers - access via dfll_{readl,writel}
89 #define FORCE_MIN -2048
139 * I2C output control registers - access via dfll_i2c_{readl,writel}
171 * Integrated I2C controller registers - relative to td->i2c_controller_base
191 * integrates the DVCO counter over - used for debug rate monitoring and
206 * enum dfll_ctrl_mode - DFLL hardware operating mode
207 * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield)
224 * enum dfll_tune_range - voltage range that the driver believes it's in
226 * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode)
229 * DVCO's voltage; these states represent the ranges that the driver
245 * struct dfll_rate_req - target DFLL rate request data
262 struct tegra_dfll_soc_data *soc; member
332 return __raw_readl(td->base + offs); in dfll_readl()
338 __raw_writel(val, td->base + offs); in dfll_writel()
346 /* I2C output control registers - for addresses above DFLL_I2C_CFG */
350 return __raw_readl(td->i2c_base + offs); in dfll_i2c_readl()
355 __raw_writel(val, td->i2c_base + offs); in dfll_i2c_writel()
364 * dfll_is_running - is the DFLL currently generating a clock?
372 return td->mode >= DFLL_OPEN_LOOP; in dfll_is_running()
380 * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL
393 ret = clk_enable(td->ref_clk); in tegra_dfll_runtime_resume()
399 ret = clk_enable(td->soc_clk); in tegra_dfll_runtime_resume()
402 clk_disable(td->ref_clk); in tegra_dfll_runtime_resume()
406 ret = clk_enable(td->i2c_clk); in tegra_dfll_runtime_resume()
409 clk_disable(td->soc_clk); in tegra_dfll_runtime_resume()
410 clk_disable(td->ref_clk); in tegra_dfll_runtime_resume()
419 * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL
429 clk_disable(td->ref_clk); in tegra_dfll_runtime_suspend()
430 clk_disable(td->soc_clk); in tegra_dfll_runtime_suspend()
431 clk_disable(td->i2c_clk); in tegra_dfll_runtime_suspend()
438 * DFLL tuning operations (per-voltage-range tuning settings)
442 * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage
446 * the low-voltage range. These settings are valid for any voltage,
451 td->tune_range = DFLL_TUNE_LOW; in dfll_tune_low()
453 dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune0_low, DFLL_TUNE0); in dfll_tune_low()
454 dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune1, DFLL_TUNE1); in dfll_tune_low()
457 if (td->soc->set_clock_trimmers_low) in dfll_tune_low()
458 td->soc->set_clock_trimmers_low(); in dfll_tune_low()
466 * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate
484 * dfll_set_mode - change the DFLL control mode
488 * Change the DFLL's operating mode between disabled, open-loop mode,
489 * and closed-loop mode, or vice versa.
494 td->mode = mode; in dfll_set_mode()
495 dfll_writel(td, mode - 1, DFLL_CTRL); in dfll_set_mode()
509 min_uv = td->lut_uv[out_min]; in get_dvco_rate_below()
511 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); in get_dvco_rate_below()
528 * DFLL-to-I2C controller interface
532 * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests
558 * DFLL-to-PWM controller interface
562 * dfll_pwm_set_output_enabled - enable/disable PWM voltage requests
576 ret = pinctrl_select_state(td->pwm_pin, td->pwm_enable_state); in dfll_pwm_set_output_enabled()
578 dev_err(td->dev, "setting enable state failed\n"); in dfll_pwm_set_output_enabled()
579 return -EINVAL; in dfll_pwm_set_output_enabled()
583 div = DIV_ROUND_UP(td->ref_rate, td->pwm_rate); in dfll_pwm_set_output_enabled()
593 ret = pinctrl_select_state(td->pwm_pin, td->pwm_disable_state); in dfll_pwm_set_output_enabled()
595 dev_warn(td->dev, "setting disable state failed\n"); in dfll_pwm_set_output_enabled()
607 * dfll_set_force_output_value - set fixed value for force output
626 * dfll_set_force_output_enabled - enable/disable force output
646 * dfll_force_output - force output a fixed value
657 return -EINVAL; in dfll_force_output()
660 if ((td->mode < DFLL_CLOSED_LOOP) && in dfll_force_output()
669 * dfll_load_lut - load the voltage lookup table
672 * Load the voltage-to-PMIC register value lookup table into the DFLL
673 * IP block memory. Look-up tables can be loaded at any time.
681 if (i < td->lut_min) in dfll_load_i2c_lut()
682 lut_index = td->lut_min; in dfll_load_i2c_lut()
683 else if (i > td->lut_max) in dfll_load_i2c_lut()
684 lut_index = td->lut_max; in dfll_load_i2c_lut()
688 val = regulator_list_hardware_vsel(td->vdd_reg, in dfll_load_i2c_lut()
689 td->lut[lut_index]); in dfll_load_i2c_lut()
690 __raw_writel(val, td->lut_base + i * 4); in dfll_load_i2c_lut()
697 * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface
700 * During DFLL driver initialization, program the DFLL-I2C interface
703 * voltage-set commands, which are then passed to the DFLL's internal
710 if (td->i2c_slave_addr > 0x7f) { in dfll_init_i2c_if()
711 val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT; in dfll_init_i2c_if()
714 val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT; in dfll_init_i2c_if()
720 dfll_i2c_writel(td, td->i2c_reg, DFLL_I2C_VDD_REG_ADDR); in dfll_init_i2c_if()
722 val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8); in dfll_init_i2c_if()
724 val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT; in dfll_init_i2c_if()
728 __raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR); in dfll_init_i2c_if()
733 * dfll_init_out_if - prepare DFLL-to-PMIC interface
744 td->lut_min = td->lut_bottom; in dfll_init_out_if()
745 td->lut_max = td->lut_size - 1; in dfll_init_out_if()
746 td->lut_safe = td->lut_min + (td->lut_min < td->lut_max ? 1 : 0); in dfll_init_out_if()
752 val = (td->lut_safe << DFLL_OUTPUT_CFG_SAFE_SHIFT) | in dfll_init_out_if()
753 (td->lut_max << DFLL_OUTPUT_CFG_MAX_SHIFT) | in dfll_init_out_if()
754 (td->lut_min << DFLL_OUTPUT_CFG_MIN_SHIFT); in dfll_init_out_if()
763 if (td->pmu_if == TEGRA_DFLL_PMU_PWM) { in dfll_init_out_if()
764 u32 vinit = td->reg_init_uV; in dfll_init_out_if()
765 int vstep = td->soc->alignment.step_uv; in dfll_init_out_if()
766 unsigned long vmin = td->lut_uv[0]; in dfll_init_out_if()
772 vsel = DIV_ROUND_UP((vinit - vmin), vstep); in dfll_init_out_if()
782 * Set/get the DFLL's targeted output clock rate
786 * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate
792 * to the integrator during rate changes. Returns -ENOENT if a suitable
800 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); in find_lut_index_for_rate()
804 align_step = dev_pm_opp_get_voltage(opp) / td->soc->alignment.step_uv; in find_lut_index_for_rate()
807 for (i = td->lut_bottom; i < td->lut_size; i++) { in find_lut_index_for_rate()
808 if ((td->lut_uv[i] / td->soc->alignment.step_uv) >= align_step) in find_lut_index_for_rate()
812 return -ENOENT; in find_lut_index_for_rate()
816 * dfll_calculate_rate_request - calculate DFLL parameters for a given rate
818 * @req: DFLL-rate-request structure
821 * Populate the DFLL-rate-request record @req fields with the scale_bits
823 * success, or -EINVAL if the requested rate in req->rate is too high
838 req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1; in dfll_calculate_rate_request()
839 if (rate < td->dvco_rate_min) { in dfll_calculate_rate_request()
843 td->dvco_rate_min / 1000); in dfll_calculate_rate_request()
845 dev_err(td->dev, "%s: Rate %lu is too low\n", in dfll_calculate_rate_request()
847 return -EINVAL; in dfll_calculate_rate_request()
849 req->scale_bits = scale - 1; in dfll_calculate_rate_request()
850 rate = td->dvco_rate_min; in dfll_calculate_rate_request()
854 val = DVCO_RATE_TO_MULT(rate, td->ref_rate); in dfll_calculate_rate_request()
856 dev_err(td->dev, "%s: Rate %lu is above dfll range\n", in dfll_calculate_rate_request()
858 return -EINVAL; in dfll_calculate_rate_request()
860 req->mult_bits = val; in dfll_calculate_rate_request()
861 req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate); in dfll_calculate_rate_request()
862 req->rate = dfll_scale_dvco_rate(req->scale_bits, in dfll_calculate_rate_request()
863 req->dvco_target_rate); in dfll_calculate_rate_request()
864 req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate); in dfll_calculate_rate_request()
865 if (req->lut_index < 0) in dfll_calculate_rate_request()
866 return req->lut_index; in dfll_calculate_rate_request()
872 * dfll_set_frequency_request - start the frequency change operation
877 * frequency represented by @req. DFLL must be in closed-loop mode.
884 int coef = 128; /* FIXME: td->cg_scale? */; in dfll_set_frequency_request()
886 force_val = (req->lut_index - td->lut_safe) * coef / td->cg; in dfll_set_frequency_request()
889 val |= req->mult_bits << DFLL_FREQ_REQ_MULT_SHIFT; in dfll_set_frequency_request()
890 val |= req->scale_bits << DFLL_FREQ_REQ_SCALE_SHIFT; in dfll_set_frequency_request()
900 * tegra_dfll_request_rate - set the next rate for the DFLL to tune to
905 * settings. In closed-loop mode, update new settings immediately to
908 * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL
909 * if @rate is outside the DFLL's tunable range.
916 if (td->mode == DFLL_UNINITIALIZED) { in dfll_request_rate()
917 dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n", in dfll_request_rate()
918 __func__, mode_name[td->mode]); in dfll_request_rate()
919 return -EPERM; in dfll_request_rate()
926 td->last_unrounded_rate = rate; in dfll_request_rate()
927 td->last_req = req; in dfll_request_rate()
929 if (td->mode == DFLL_CLOSED_LOOP) in dfll_request_rate()
930 dfll_set_frequency_request(td, &td->last_req); in dfll_request_rate()
936 * DFLL enable/disable & open-loop <-> closed-loop transitions
940 * dfll_disable - switch from open-loop mode to disabled mode
944 * or -EPERM if the DFLL is not currently in open-loop mode.
948 if (td->mode != DFLL_OPEN_LOOP) { in dfll_disable()
949 dev_err(td->dev, "cannot disable DFLL in %s mode\n", in dfll_disable()
950 mode_name[td->mode]); in dfll_disable()
951 return -EINVAL; in dfll_disable()
955 pm_runtime_put_sync(td->dev); in dfll_disable()
961 * dfll_enable - switch a disabled DFLL to open-loop mode
965 * or -EPERM if the DFLL is not currently disabled.
969 if (td->mode != DFLL_DISABLED) { in dfll_enable()
970 dev_err(td->dev, "cannot enable DFLL in %s mode\n", in dfll_enable()
971 mode_name[td->mode]); in dfll_enable()
972 return -EPERM; in dfll_enable()
975 pm_runtime_get_sync(td->dev); in dfll_enable()
982 * dfll_set_open_loop_config - prepare to switch to open-loop mode
985 * Prepare to switch the DFLL to open-loop mode. This switches the
986 * DFLL to the low-voltage tuning range, ensures that I2C output
988 * The DFLL's low-voltage tuning range parameters must be
997 if (td->tune_range != DFLL_TUNE_LOW) in dfll_set_open_loop_config()
1008 * tegra_dfll_lock - switch from open-loop to closed-loop mode
1012 * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the
1013 * DFLL is not currently in open-loop mode.
1017 struct dfll_rate_req *req = &td->last_req; in dfll_lock()
1019 switch (td->mode) { in dfll_lock()
1024 if (req->rate == 0) { in dfll_lock()
1025 dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n", in dfll_lock()
1027 return -EINVAL; in dfll_lock()
1030 if (td->pmu_if == TEGRA_DFLL_PMU_PWM) in dfll_lock()
1041 BUG_ON(td->mode > DFLL_CLOSED_LOOP); in dfll_lock()
1042 dev_err(td->dev, "%s: Cannot lock DFLL in %s mode\n", in dfll_lock()
1043 __func__, mode_name[td->mode]); in dfll_lock()
1044 return -EPERM; in dfll_lock()
1049 * tegra_dfll_unlock - switch from closed-loop to open-loop mode
1053 * or -EPERM if the DFLL is not currently in open-loop mode.
1057 switch (td->mode) { in dfll_unlock()
1061 if (td->pmu_if == TEGRA_DFLL_PMU_PWM) in dfll_unlock()
1071 BUG_ON(td->mode > DFLL_CLOSED_LOOP); in dfll_unlock()
1072 dev_err(td->dev, "%s: Cannot unlock DFLL in %s mode\n", in dfll_unlock()
1073 __func__, mode_name[td->mode]); in dfll_unlock()
1074 return -EPERM; in dfll_unlock()
1125 return td->last_unrounded_rate; in dfll_clk_recalc_rate()
1128 /* Must use determine_rate since it allows for rates exceeding 2^31-1 */
1136 ret = dfll_calculate_rate_request(td, &req, clk_req->rate); in dfll_clk_determine_rate()
1172 * dfll_register_clk - register the DFLL output clock with the clock framework
1175 * Register the DFLL's output clock with the Linux clock framework and register
1176 * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL
1177 * or -ENOMEM upon failure.
1183 dfll_clk_init_data.name = td->output_clock_name; in dfll_register_clk()
1184 td->dfll_clk_hw.init = &dfll_clk_init_data; in dfll_register_clk()
1186 td->dfll_clk = clk_register(td->dev, &td->dfll_clk_hw); in dfll_register_clk()
1187 if (IS_ERR(td->dfll_clk)) { in dfll_register_clk()
1188 dev_err(td->dev, "DFLL clock registration error\n"); in dfll_register_clk()
1189 return -EINVAL; in dfll_register_clk()
1192 ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get, in dfll_register_clk()
1193 td->dfll_clk); in dfll_register_clk()
1195 dev_err(td->dev, "of_clk_add_provider() failed\n"); in dfll_register_clk()
1197 clk_unregister(td->dfll_clk); in dfll_register_clk()
1205 * dfll_unregister_clk - unregister the DFLL output clock
1208 * Unregister the DFLL's output clock from the Linux clock framework
1213 of_clk_del_provider(td->dev->of_node); in dfll_unregister_clk()
1214 clk_unregister(td->dfll_clk); in dfll_unregister_clk()
1215 td->dfll_clk = NULL; in dfll_unregister_clk()
1228 * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq
1242 * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor
1245 * If the DFLL is enabled, return the last rate reported by the DFLL's
1246 * internal monitoring hardware. This works in both open-loop and
1247 * closed-loop mode, and takes the output scaler setting into account.
1256 u32 v, s; in dfll_read_monitor_rate() local
1264 pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate); in dfll_read_monitor_rate()
1266 s = dfll_readl(td, DFLL_FREQ_REQ); in dfll_read_monitor_rate()
1267 s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT; in dfll_read_monitor_rate()
1268 post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate); in dfll_read_monitor_rate()
1294 *val = (td->mode == DFLL_CLOSED_LOOP); in attr_lock_get()
1323 static int attr_registers_show(struct seq_file *s, void *data) in attr_registers_show() argument
1326 struct tegra_dfll *td = s->private; in attr_registers_show()
1328 seq_puts(s, "CONTROL REGISTERS:\n"); in attr_registers_show()
1334 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, val); in attr_registers_show()
1337 seq_puts(s, "\nI2C and INTR REGISTERS:\n"); in attr_registers_show()
1339 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, in attr_registers_show()
1342 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, in attr_registers_show()
1345 if (td->pmu_if == TEGRA_DFLL_PMU_I2C) { in attr_registers_show()
1346 seq_puts(s, "\nINTEGRATED I2C CONTROLLER REGISTERS:\n"); in attr_registers_show()
1348 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, in attr_registers_show()
1349 __raw_readl(td->i2c_controller_base + offs)); in attr_registers_show()
1351 seq_puts(s, "\nLUT:\n"); in attr_registers_show()
1353 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, in attr_registers_show()
1354 __raw_readl(td->lut_base + offs)); in attr_registers_show()
1366 if (!td || (td->mode == DFLL_UNINITIALIZED)) in dfll_debug_init()
1370 td->debugfs_dir = root; in dfll_debug_init()
1388 * dfll_set_default_params - program non-output related DFLL parameters
1399 val = DIV_ROUND_UP(td->ref_rate, td->sample_rate * 32); in dfll_set_default_params()
1403 val = (td->force_mode << DFLL_PARAMS_FORCE_MODE_SHIFT) | in dfll_set_default_params()
1404 (td->cf << DFLL_PARAMS_CF_PARAM_SHIFT) | in dfll_set_default_params()
1405 (td->ci << DFLL_PARAMS_CI_PARAM_SHIFT) | in dfll_set_default_params()
1406 (td->cg << DFLL_PARAMS_CG_PARAM_SHIFT) | in dfll_set_default_params()
1407 (td->cg_scale ? DFLL_PARAMS_CG_SCALE : 0); in dfll_set_default_params()
1411 dfll_writel(td, td->droop_ctrl, DFLL_DROOP_CTRL); in dfll_set_default_params()
1416 * dfll_init_clks - clk_get() the DFLL source clocks
1425 td->ref_clk = devm_clk_get(td->dev, "ref"); in dfll_init_clks()
1426 if (IS_ERR(td->ref_clk)) { in dfll_init_clks()
1427 dev_err(td->dev, "missing ref clock\n"); in dfll_init_clks()
1428 return PTR_ERR(td->ref_clk); in dfll_init_clks()
1431 td->soc_clk = devm_clk_get(td->dev, "soc"); in dfll_init_clks()
1432 if (IS_ERR(td->soc_clk)) { in dfll_init_clks()
1433 dev_err(td->dev, "missing soc clock\n"); in dfll_init_clks()
1434 return PTR_ERR(td->soc_clk); in dfll_init_clks()
1437 td->i2c_clk = devm_clk_get(td->dev, "i2c"); in dfll_init_clks()
1438 if (IS_ERR(td->i2c_clk)) { in dfll_init_clks()
1439 dev_err(td->dev, "missing i2c clock\n"); in dfll_init_clks()
1440 return PTR_ERR(td->i2c_clk); in dfll_init_clks()
1442 td->i2c_clk_rate = clk_get_rate(td->i2c_clk); in dfll_init_clks()
1448 * dfll_init - Prepare the DFLL IP block for use
1460 td->ref_rate = clk_get_rate(td->ref_clk); in dfll_init()
1461 if (td->ref_rate != REF_CLOCK_RATE) { in dfll_init()
1462 dev_err(td->dev, "unexpected ref clk rate %lu, expecting %lu", in dfll_init()
1463 td->ref_rate, REF_CLOCK_RATE); in dfll_init()
1464 return -EINVAL; in dfll_init()
1467 reset_control_deassert(td->dvco_rst); in dfll_init()
1469 ret = clk_prepare(td->ref_clk); in dfll_init()
1471 dev_err(td->dev, "failed to prepare ref_clk\n"); in dfll_init()
1475 ret = clk_prepare(td->soc_clk); in dfll_init()
1477 dev_err(td->dev, "failed to prepare soc_clk\n"); in dfll_init()
1481 ret = clk_prepare(td->i2c_clk); in dfll_init()
1483 dev_err(td->dev, "failed to prepare i2c_clk\n"); in dfll_init()
1487 td->last_unrounded_rate = 0; in dfll_init()
1489 pm_runtime_enable(td->dev); in dfll_init()
1490 pm_runtime_get_sync(td->dev); in dfll_init()
1495 if (td->soc->init_clock_trimmers) in dfll_init()
1496 td->soc->init_clock_trimmers(); in dfll_init()
1502 pm_runtime_put_sync(td->dev); in dfll_init()
1507 clk_unprepare(td->soc_clk); in dfll_init()
1509 clk_unprepare(td->ref_clk); in dfll_init()
1511 reset_control_assert(td->dvco_rst); in dfll_init()
1517 * tegra_dfll_suspend - check DFLL is disabled
1528 dev_err(td->dev, "DFLL still enabled while suspending\n"); in tegra_dfll_suspend()
1529 return -EBUSY; in tegra_dfll_suspend()
1532 reset_control_assert(td->dvco_rst); in tegra_dfll_suspend()
1539 * tegra_dfll_resume - reinitialize DFLL on resume
1551 reset_control_deassert(td->dvco_rst); in tegra_dfll_resume()
1553 pm_runtime_get_sync(td->dev); in tegra_dfll_resume()
1558 if (td->soc->init_clock_trimmers) in tegra_dfll_resume()
1559 td->soc->init_clock_trimmers(); in tegra_dfll_resume()
1565 pm_runtime_put_sync(td->dev); in tegra_dfll_resume()
1576 * Find a PMIC voltage register-to-voltage mapping for the given voltage.
1583 if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM)) in find_vdd_map_entry_exact()
1584 return -EINVAL; in find_vdd_map_entry_exact()
1586 align_step = uV / td->soc->alignment.step_uv; in find_vdd_map_entry_exact()
1587 n_voltages = regulator_count_voltages(td->vdd_reg); in find_vdd_map_entry_exact()
1589 reg_uV = regulator_list_voltage(td->vdd_reg, i); in find_vdd_map_entry_exact()
1593 reg_volt_id = reg_uV / td->soc->alignment.step_uv; in find_vdd_map_entry_exact()
1599 dev_err(td->dev, "no voltage map entry for %d uV\n", uV); in find_vdd_map_entry_exact()
1600 return -EINVAL; in find_vdd_map_entry_exact()
1604 * Find a PMIC voltage register-to-voltage mapping for the given voltage,
1611 if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM)) in find_vdd_map_entry_min()
1612 return -EINVAL; in find_vdd_map_entry_min()
1614 align_step = uV / td->soc->alignment.step_uv; in find_vdd_map_entry_min()
1615 n_voltages = regulator_count_voltages(td->vdd_reg); in find_vdd_map_entry_min()
1617 reg_uV = regulator_list_voltage(td->vdd_reg, i); in find_vdd_map_entry_min()
1621 reg_volt_id = reg_uV / td->soc->alignment.step_uv; in find_vdd_map_entry_min()
1627 dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV); in find_vdd_map_entry_min()
1628 return -EINVAL; in find_vdd_map_entry_min()
1632 * dfll_build_pwm_lut - build the PWM regulator lookup table
1636 * Look-up table in h/w is ignored when PWM is used as DFLL interface to PMIC.
1637 * In this case closed loop output is controlling duty cycle directly. The s/w
1638 * look-up that maps PWM duty cycle to voltage is still built by this function.
1645 int v_min = td->soc->cvb->min_millivolts * 1000; in dfll_build_pwm_lut()
1648 reg_volt = td->lut_uv[i]; in dfll_build_pwm_lut()
1655 td->lut[i] = i; in dfll_build_pwm_lut()
1661 td->lut_size = i; in dfll_build_pwm_lut()
1663 (lut_bottom + 1 >= td->lut_size)) { in dfll_build_pwm_lut()
1664 dev_err(td->dev, "no voltage above DFLL minimum %d mV\n", in dfll_build_pwm_lut()
1665 td->soc->cvb->min_millivolts); in dfll_build_pwm_lut()
1666 return -EINVAL; in dfll_build_pwm_lut()
1668 td->lut_bottom = lut_bottom; in dfll_build_pwm_lut()
1671 rate = get_dvco_rate_below(td, td->lut_bottom); in dfll_build_pwm_lut()
1673 dev_err(td->dev, "no opp below DFLL minimum voltage %d mV\n", in dfll_build_pwm_lut()
1674 td->soc->cvb->min_millivolts); in dfll_build_pwm_lut()
1675 return -EINVAL; in dfll_build_pwm_lut()
1677 td->dvco_rate_min = rate; in dfll_build_pwm_lut()
1683 * dfll_build_i2c_lut - build the I2C voltage register lookup table
1687 * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with
1689 * This function builds the look-up table based on the OPP table provided by
1690 * the soc-specific platform driver (td->soc->opp_dev) and the PMIC
1691 * register-to-voltage mapping queried from the regulator framework.
1693 * On success, fills in td->lut and returns 0, or -err on failure.
1698 int ret = -EINVAL; in dfll_build_i2c_lut()
1701 v = td->soc->cvb->min_millivolts * 1000; in dfll_build_i2c_lut()
1705 td->lut[0] = lut; in dfll_build_i2c_lut()
1706 td->lut_bottom = 0; in dfll_build_i2c_lut()
1711 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); in dfll_build_i2c_lut()
1716 if (v_opp <= td->soc->cvb->min_millivolts * 1000) in dfll_build_i2c_lut()
1717 td->dvco_rate_min = dev_pm_opp_get_freq(opp); in dfll_build_i2c_lut()
1722 v += max(1UL, (v_max - v) / (MAX_DFLL_VOLTAGES - j)); in dfll_build_i2c_lut()
1729 if (selector != td->lut[j - 1]) in dfll_build_i2c_lut()
1730 td->lut[j++] = selector; in dfll_build_i2c_lut()
1733 v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp; in dfll_build_i2c_lut()
1737 if (selector != td->lut[j - 1]) in dfll_build_i2c_lut()
1738 td->lut[j++] = selector; in dfll_build_i2c_lut()
1743 td->lut_size = j; in dfll_build_i2c_lut()
1745 if (!td->dvco_rate_min) in dfll_build_i2c_lut()
1746 dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n", in dfll_build_i2c_lut()
1747 td->soc->cvb->min_millivolts); in dfll_build_i2c_lut()
1750 for (j = 0; j < td->lut_size; j++) in dfll_build_i2c_lut()
1751 td->lut_uv[j] = in dfll_build_i2c_lut()
1752 regulator_list_voltage(td->vdd_reg, in dfll_build_i2c_lut()
1753 td->lut[j]); in dfll_build_i2c_lut()
1766 opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate); in dfll_build_lut()
1768 dev_err(td->dev, "couldn't get vmax opp, empty opp table?\n"); in dfll_build_lut()
1769 return -EINVAL; in dfll_build_lut()
1774 if (td->pmu_if == TEGRA_DFLL_PMU_PWM) in dfll_build_lut()
1781 * read_dt_param - helper function for reading required parameters from the DT
1792 int err = of_property_read_u32(td->dev->of_node, param, dest); in read_dt_param()
1795 dev_err(td->dev, "failed to read DT parameter %s: %d\n", in read_dt_param()
1804 * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem
1809 * Returns 0 on success or -err on failure.
1819 if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate)) in dfll_fetch_i2c_params()
1820 return -EINVAL; in dfll_fetch_i2c_params()
1822 regmap = regulator_get_regmap(td->vdd_reg); in dfll_fetch_i2c_params()
1826 td->i2c_slave_addr = i2c_client->addr; in dfll_fetch_i2c_params()
1828 ret = regulator_get_hardware_vsel_register(td->vdd_reg, in dfll_fetch_i2c_params()
1832 dev_err(td->dev, in dfll_fetch_i2c_params()
1834 return -EINVAL; in dfll_fetch_i2c_params()
1836 td->i2c_reg = vsel_reg; in dfll_fetch_i2c_params()
1846 if (!td->soc->alignment.step_uv || !td->soc->alignment.offset_uv) { in dfll_fetch_pwm_params()
1847 dev_err(td->dev, in dfll_fetch_pwm_params()
1849 return -EINVAL; in dfll_fetch_pwm_params()
1852 td->lut_uv[i] = td->soc->alignment.offset_uv + in dfll_fetch_pwm_params()
1853 i * td->soc->alignment.step_uv; in dfll_fetch_pwm_params()
1855 ret = read_dt_param(td, "nvidia,pwm-tristate-microvolts", in dfll_fetch_pwm_params()
1856 &td->reg_init_uV); in dfll_fetch_pwm_params()
1858 dev_err(td->dev, "couldn't get initialized voltage\n"); in dfll_fetch_pwm_params()
1859 return -EINVAL; in dfll_fetch_pwm_params()
1862 ret = read_dt_param(td, "nvidia,pwm-period-nanoseconds", &pwm_period); in dfll_fetch_pwm_params()
1864 dev_err(td->dev, "couldn't get PWM period\n"); in dfll_fetch_pwm_params()
1865 return -EINVAL; in dfll_fetch_pwm_params()
1867 td->pwm_rate = (NSEC_PER_SEC / pwm_period) * (MAX_DFLL_VOLTAGES - 1); in dfll_fetch_pwm_params()
1869 td->pwm_pin = devm_pinctrl_get(td->dev); in dfll_fetch_pwm_params()
1870 if (IS_ERR(td->pwm_pin)) { in dfll_fetch_pwm_params()
1871 dev_err(td->dev, "DT: missing pinctrl device\n"); in dfll_fetch_pwm_params()
1872 return PTR_ERR(td->pwm_pin); in dfll_fetch_pwm_params()
1875 td->pwm_enable_state = pinctrl_lookup_state(td->pwm_pin, in dfll_fetch_pwm_params()
1877 if (IS_ERR(td->pwm_enable_state)) { in dfll_fetch_pwm_params()
1878 dev_err(td->dev, "DT: missing pwm enabled state\n"); in dfll_fetch_pwm_params()
1879 return PTR_ERR(td->pwm_enable_state); in dfll_fetch_pwm_params()
1882 td->pwm_disable_state = pinctrl_lookup_state(td->pwm_pin, in dfll_fetch_pwm_params()
1884 if (IS_ERR(td->pwm_disable_state)) { in dfll_fetch_pwm_params()
1885 dev_err(td->dev, "DT: missing pwm disabled state\n"); in dfll_fetch_pwm_params()
1886 return PTR_ERR(td->pwm_disable_state); in dfll_fetch_pwm_params()
1893 * dfll_fetch_common_params - read DFLL parameters from the device tree
1897 * Returns 0 on success or -EINVAL on any failure.
1903 ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl); in dfll_fetch_common_params()
1904 ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate); in dfll_fetch_common_params()
1905 ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode); in dfll_fetch_common_params()
1906 ok &= read_dt_param(td, "nvidia,cf", &td->cf); in dfll_fetch_common_params()
1907 ok &= read_dt_param(td, "nvidia,ci", &td->ci); in dfll_fetch_common_params()
1908 ok &= read_dt_param(td, "nvidia,cg", &td->cg); in dfll_fetch_common_params()
1909 td->cg_scale = of_property_read_bool(td->dev->of_node, in dfll_fetch_common_params()
1910 "nvidia,cg-scale"); in dfll_fetch_common_params()
1912 if (of_property_read_string(td->dev->of_node, "clock-output-names", in dfll_fetch_common_params()
1913 &td->output_clock_name)) { in dfll_fetch_common_params()
1914 dev_err(td->dev, "missing clock-output-names property\n"); in dfll_fetch_common_params()
1918 return ok ? 0 : -EINVAL; in dfll_fetch_common_params()
1922 * API exported to per-SoC platform drivers
1926 * tegra_dfll_register - probe a Tegra DFLL device
1928 * @soc: Per-SoC integration and characterization data for this DFLL instance
1931 * by a SoC-specific shim driver that passes in per-SoC integration
1932 * and configuration data via @soc. Returns 0 on success or -err on failure.
1935 struct tegra_dfll_soc_data *soc) in tegra_dfll_register() argument
1941 if (!soc) { in tegra_dfll_register()
1942 dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n"); in tegra_dfll_register()
1943 return -EINVAL; in tegra_dfll_register()
1946 td = devm_kzalloc(&pdev->dev, sizeof(*td), GFP_KERNEL); in tegra_dfll_register()
1948 return -ENOMEM; in tegra_dfll_register()
1949 td->dev = &pdev->dev; in tegra_dfll_register()
1952 td->soc = soc; in tegra_dfll_register()
1954 td->dvco_rst = devm_reset_control_get(td->dev, "dvco"); in tegra_dfll_register()
1955 if (IS_ERR(td->dvco_rst)) { in tegra_dfll_register()
1956 dev_err(td->dev, "couldn't get dvco reset\n"); in tegra_dfll_register()
1957 return PTR_ERR(td->dvco_rst); in tegra_dfll_register()
1962 dev_err(td->dev, "couldn't parse device tree parameters\n"); in tegra_dfll_register()
1966 if (of_property_read_bool(td->dev->of_node, "nvidia,pwm-to-pmic")) { in tegra_dfll_register()
1967 td->pmu_if = TEGRA_DFLL_PMU_PWM; in tegra_dfll_register()
1970 td->vdd_reg = devm_regulator_get(td->dev, "vdd-cpu"); in tegra_dfll_register()
1971 if (IS_ERR(td->vdd_reg)) { in tegra_dfll_register()
1972 dev_err(td->dev, "couldn't get vdd_cpu regulator\n"); in tegra_dfll_register()
1973 return PTR_ERR(td->vdd_reg); in tegra_dfll_register()
1975 td->pmu_if = TEGRA_DFLL_PMU_I2C; in tegra_dfll_register()
1983 dev_err(td->dev, "couldn't build LUT\n"); in tegra_dfll_register()
1989 dev_err(td->dev, "no control register resource\n"); in tegra_dfll_register()
1990 return -ENODEV; in tegra_dfll_register()
1993 td->base = devm_ioremap(td->dev, mem->start, resource_size(mem)); in tegra_dfll_register()
1994 if (!td->base) { in tegra_dfll_register()
1995 dev_err(td->dev, "couldn't ioremap DFLL control registers\n"); in tegra_dfll_register()
1996 return -ENODEV; in tegra_dfll_register()
2001 dev_err(td->dev, "no i2c_base resource\n"); in tegra_dfll_register()
2002 return -ENODEV; in tegra_dfll_register()
2005 td->i2c_base = devm_ioremap(td->dev, mem->start, resource_size(mem)); in tegra_dfll_register()
2006 if (!td->i2c_base) { in tegra_dfll_register()
2007 dev_err(td->dev, "couldn't ioremap i2c_base resource\n"); in tegra_dfll_register()
2008 return -ENODEV; in tegra_dfll_register()
2013 dev_err(td->dev, "no i2c_controller_base resource\n"); in tegra_dfll_register()
2014 return -ENODEV; in tegra_dfll_register()
2017 td->i2c_controller_base = devm_ioremap(td->dev, mem->start, in tegra_dfll_register()
2019 if (!td->i2c_controller_base) { in tegra_dfll_register()
2020 dev_err(td->dev, in tegra_dfll_register()
2022 return -ENODEV; in tegra_dfll_register()
2027 dev_err(td->dev, "no lut_base resource\n"); in tegra_dfll_register()
2028 return -ENODEV; in tegra_dfll_register()
2031 td->lut_base = devm_ioremap(td->dev, mem->start, resource_size(mem)); in tegra_dfll_register()
2032 if (!td->lut_base) { in tegra_dfll_register()
2033 dev_err(td->dev, in tegra_dfll_register()
2035 return -ENODEV; in tegra_dfll_register()
2040 dev_err(&pdev->dev, "DFLL clock init error\n"); in tegra_dfll_register()
2051 dev_err(&pdev->dev, "DFLL clk registration failed\n"); in tegra_dfll_register()
2062 * tegra_dfll_unregister - release all of the DFLL driver resources for a device
2067 * soc pointer upon success or -EBUSY if the DFLL is still active.
2074 if (td->mode != DFLL_DISABLED) { in tegra_dfll_unregister()
2075 dev_err(&pdev->dev, in tegra_dfll_unregister()
2077 return ERR_PTR(-EBUSY); in tegra_dfll_unregister()
2080 debugfs_remove_recursive(td->debugfs_dir); in tegra_dfll_unregister()
2083 pm_runtime_disable(&pdev->dev); in tegra_dfll_unregister()
2085 clk_unprepare(td->ref_clk); in tegra_dfll_unregister()
2086 clk_unprepare(td->soc_clk); in tegra_dfll_unregister()
2087 clk_unprepare(td->i2c_clk); in tegra_dfll_unregister()
2089 reset_control_assert(td->dvco_rst); in tegra_dfll_unregister()
2091 return td->soc; in tegra_dfll_unregister()