// SPDX-License-Identifier: GPL-2.0-only /* * Driver for Allwinner sun4i Pulse Width Modulation Controller * * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com> * * Limitations: * - When outputing the source clock directly, the PWM logic will be bypassed * and the currently running period is not guaranteed to be completed
*/
clk_rate = clk_get_rate(sun4ichip->clk); if (!clk_rate) return -EINVAL;
val = sun4i_pwm_readl(sun4ichip, PWM_CTRL_REG);
/* * PWM chapter in H6 manual has a diagram which explains that if bypass * bit is set, no other setting has any meaning. Even more, experiment * proved that also enable bit is ignored in this case.
*/ if ((val & BIT_CH(PWM_BYPASS, pwm->hwpwm)) &&
sun4ichip->data->has_direct_mod_clk_output) {
state->period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, clk_rate);
state->duty_cycle = DIV_ROUND_UP_ULL(state->period, 2);
state->polarity = PWM_POLARITY_NORMAL;
state->enabled = true; return 0;
}
/* Skip calculation of other parameters if we bypass them */ if (*bypass) return 0;
if (sun4ichip->data->has_prescaler_bypass) { /* First, test without any prescaler when available */
prescaler = PWM_PRESCAL_MASK; /* * When not using any prescaler, the clock period in nanoseconds * is not an integer so round it half up instead of * truncating to get less surprising values.
*/
div = clk_rate * state->period + NSEC_PER_SEC / 2;
do_div(div, NSEC_PER_SEC); if (div - 1 > PWM_PRD_MASK)
prescaler = 0;
}
if (prescaler == 0) { /* Go up from the first divider */ for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) { unsignedint pval = prescaler_table[prescaler];
if (!pval) continue;
div = clk_rate;
do_div(div, pval);
div = div * state->period;
do_div(div, NSEC_PER_SEC); if (div - 1 <= PWM_PRD_MASK) break;
}
if (!cstate.enabled) {
ret = clk_prepare_enable(sun4ichip->clk); if (ret) {
dev_err(pwmchip_parent(chip), "failed to enable PWM clock\n"); return ret;
}
}
ret = sun4i_pwm_calculate(sun4ichip, state, &duty, &period, &prescaler,
&bypass); if (ret) {
dev_err(pwmchip_parent(chip), "period exceeds the maximum value\n"); if (!cstate.enabled)
clk_disable_unprepare(sun4ichip->clk); return ret;
}
ctrl = sun4i_pwm_readl(sun4ichip, PWM_CTRL_REG);
if (sun4ichip->data->has_direct_mod_clk_output) { if (bypass) {
ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm); /* We can skip other parameter */
sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG); return 0;
}
ctrl &= ~BIT_CH(PWM_BYPASS, pwm->hwpwm);
}
if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) { /* Prescaler changed, the clock has to be gated */
ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG);
if (state->enabled)
ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG);
if (state->enabled) return 0;
/* We need a full period to elapse before disabling the channel. */
delay_us = DIV_ROUND_UP_ULL(cstate.period, NSEC_PER_USEC); if ((delay_us / 500) > MAX_UDELAY_MS)
msleep(delay_us / 1000 + 1); else
usleep_range(delay_us, delay_us * 2);
/* * All hardware variants need a source clock that is divided and * then feeds the counter that defines the output wave form. In the * device tree this clock is either unnamed or called "mod". * Some variants (e.g. H6) need another clock to access the * hardware registers; this is called "bus". * So we request "mod" first (and ignore the corner case that a * parent provides a "mod" clock while the right one would be the * unnamed one of the PWM device) and if this is not found we fall * back to the first clock of the PWM.
*/
sun4ichip->clk = devm_clk_get_optional(&pdev->dev, "mod"); if (IS_ERR(sun4ichip->clk)) return dev_err_probe(&pdev->dev, PTR_ERR(sun4ichip->clk), "get mod clock failed\n");
if (!sun4ichip->clk) {
sun4ichip->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(sun4ichip->clk)) return dev_err_probe(&pdev->dev, PTR_ERR(sun4ichip->clk), "get unnamed clock failed\n");
}
sun4ichip->bus_clk = devm_clk_get_optional(&pdev->dev, "bus"); if (IS_ERR(sun4ichip->bus_clk)) return dev_err_probe(&pdev->dev, PTR_ERR(sun4ichip->bus_clk), "get bus clock failed\n");
/* Deassert reset */
ret = reset_control_deassert(sun4ichip->rst); if (ret) {
dev_err(&pdev->dev, "cannot deassert reset control: %pe\n",
ERR_PTR(ret)); return ret;
}
/* * We're keeping the bus clock on for the sake of simplicity. * Actually it only needs to be on for hardware register accesses.
*/
ret = clk_prepare_enable(sun4ichip->bus_clk); if (ret) {
dev_err(&pdev->dev, "cannot prepare and enable bus_clk %pe\n",
ERR_PTR(ret)); goto err_bus;
}
chip->ops = &sun4i_pwm_ops;
ret = pwmchip_add(chip); if (ret < 0) {
dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); goto err_pwm_add;
}
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.