// SPDX-License-Identifier: GPL-2.0+ /* * Copyright (C) 2021 Sean Anderson <sean.anderson@seco.com> * * Limitations: * - When changing both duty cycle and period, we may end up with one cycle * with the old duty cycle and the new period. This is because the counters * may only be reloaded by first stopping them, or by letting them be * automatically reloaded at the end of a cycle. If this automatic reload * happens after we set TLR0 but before we set TLR1 then we will have a * bad cycle. This could probably be fixed by reading TCR0 just before * reprogramming, but I think it would add complexity for little gain. * - Cannot produce 100% duty cycle by configuring the TLRs. This might be * possible by stopping the counters at an appropriate point in the cycle, * but this is not (yet) implemented. * - Only produces "normal" output. * - Always produces low output if disabled.
*/
/* * The following functions are "common" to drivers for this device, and may be * exported at a future date.
*/
u32 xilinx_timer_tlr_cycles(struct xilinx_timer_priv *priv, u32 tcsr,
u64 cycles)
{
WARN_ON(cycles < 2 || cycles - 2 > priv->max);
/* cycles has a max of 2^32 + 2, so we can't overflow */ return DIV64_U64_ROUND_UP(cycles * NSEC_PER_SEC,
clk_get_rate(priv->clk));
}
/* * The idea here is to capture whether the PWM is actually running (e.g. * because we or the bootloader set it up) and we need to be careful to ensure * we don't cause a glitch. According to the data sheet, to enable the PWM we * need to * * - Set both timers to generate mode (MDT=1) * - Set both timers to PWM mode (PWMA=1) * - Enable the generate out signals (GENT=1) * * In addition, * * - The timer must be running (ENT=1) * - The timer must auto-reload TLR into TCR (ARHT=1) * - We must not be in the process of loading TLR into TCR (LOAD=0) * - Cascade mode must be disabled (CASC=0) * * If any of these differ from usual, then the PWM is either disabled, or is * running in a mode that this driver does not support.
*/ #define TCSR_PWM_SET (TCSR_GENT | TCSR_ARHT | TCSR_ENT | TCSR_PWMA) #define TCSR_PWM_CLEAR (TCSR_MDT | TCSR_LOAD) #define TCSR_PWM_MASK (TCSR_PWM_SET | TCSR_PWM_CLEAR)
if (state->polarity != PWM_POLARITY_NORMAL) return -EINVAL;
/* * To be representable by TLR, cycles must be between 2 and * priv->max + 2. To enforce this we can reduce the cycles, but we may * not increase them. Caveat emptor: while this does result in more * predictable rounding, it may also result in a completely different * duty cycle (% high time) than what was requested.
*/
rate = clk_get_rate(priv->clk); /* Avoid overflow */
period_cycles = min_t(u64, state->period, U32_MAX * NSEC_PER_SEC);
period_cycles = mul_u64_u32_div(period_cycles, rate, NSEC_PER_SEC);
period_cycles = min_t(u64, period_cycles, priv->max + 2); if (period_cycles < 2) return -ERANGE;
/* * If we specify 100% duty cycle, we will get 0% instead, so decrease * the duty cycle count by one.
*/ if (duty_cycles >= period_cycles)
duty_cycles = period_cycles - 1;
/* Round down to 0% duty cycle for unrepresentable duty cycles */ if (duty_cycles < 2)
duty_cycles = period_cycles;
if (state->enabled) { /* * If the PWM is already running, then the counters will be * reloaded at the end of the current cycle.
*/ if (!xilinx_timer_pwm_enabled(tcsr0, tcsr1)) { /* Load TLR into TCR */
regmap_write(priv->map, TCSR0, tcsr0 | TCSR_LOAD);
regmap_write(priv->map, TCSR1, tcsr1 | TCSR_LOAD); /* Enable timers all at once with ENALL */
tcsr0 = (TCSR_PWM_SET & ~TCSR_ENT) | (tcsr0 & TCSR_UDT);
tcsr1 = TCSR_PWM_SET | TCSR_ENALL | (tcsr1 & TCSR_UDT);
regmap_write(priv->map, TCSR0, tcsr0);
regmap_write(priv->map, TCSR1, tcsr1);
}
} else {
regmap_write(priv->map, TCSR0, 0);
regmap_write(priv->map, TCSR1, 0);
}
/* * 100% duty cycle results in constant low output. This may be (very) * wrong if rate > 1 GHz, so fix this if you have such hardware :)
*/ if (state->period == state->duty_cycle)
state->duty_cycle = 0;
/* If there are no PWM cells, this binding is for a timer */
ret = of_property_read_u32(np, "#pwm-cells", &pwm_cells); if (ret == -EINVAL) return -ENODEV; if (ret) return dev_err_probe(dev, ret, "could not read #pwm-cells\n");
/* * The polarity of the Generate Out signals must be active high for PWM * mode to work. We could determine this from the device tree, but * alas, such properties are not allowed to be used.
*/
priv->clk = devm_clk_get_enabled(dev, "s_axi_aclk"); if (IS_ERR(priv->clk)) return dev_err_probe(dev, PTR_ERR(priv->clk), "Could not get clock\n");
ret = devm_clk_rate_exclusive_get(dev, priv->clk); if (ret) return dev_err_probe(dev, ret, "Failed to lock clock rate\n");
chip->ops = &xilinx_pwm_ops;
ret = devm_pwmchip_add(dev, chip); if (ret) return dev_err_probe(dev, ret, "Could not register PWM chip\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.