// SPDX-License-Identifier: GPL-2.0 /* * Analog Devices AXI PWM generator * * Copyright 2024 Analog Devices Inc. * Copyright 2024 Baylibre SAS * * Device docs: https://analogdevicesinc.github.io/hdl/library/axi_pwm_gen/index.html * * Limitations: * - The writes to registers for period and duty are shadowed until * LOAD_CONFIG is written to AXI_PWMGEN_REG_RSTN, at which point * they take effect. * - Writing LOAD_CONFIG also has the effect of re-synchronizing all * enabled channels, which could cause glitching on other channels. It * is therefore expected that channels are assigned harmonic periods * and all have a single user coordinating this. * - Supports normal polarity. Does not support changing polarity. * - On disable, the PWM output becomes low (inactive).
*/ #include <linux/adi-axi-common.h> #include <linux/bits.h> #include <linux/clk.h> #include <linux/err.h> #include <linux/io.h> #include <linux/minmax.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/pwm.h> #include <linux/regmap.h> #include <linux/slab.h>
/* This represents a hardware configuration for one channel */ struct axi_pwmgen_waveform {
u32 period_cnt;
u32 duty_cycle_cnt;
u32 duty_offset_cnt;
};
if (wfhw->period_cnt == 0) { /* * The specified period is too short for the hardware. * So round up .period_cnt to 1 (i.e. the smallest * possible period). With .duty_cycle and .duty_offset * being less than or equal to .period, their rounded * value must be 0.
*/
wfhw->period_cnt = 1;
wfhw->duty_cycle_cnt = 0;
wfhw->duty_offset_cnt = 0;
ret = 1;
} else {
wfhw->duty_cycle_cnt = min_t(u64,
mul_u64_u32_div(wf->duty_length_ns, ddata->clk_rate_hz, NSEC_PER_SEC),
U32_MAX);
wfhw->duty_offset_cnt = min_t(u64,
mul_u64_u32_div(wf->duty_offset_ns, ddata->clk_rate_hz, NSEC_PER_SEC),
U32_MAX);
}
}
ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &wfhw->period_cnt); if (ret) return ret;
ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &wfhw->duty_cycle_cnt); if (ret) return ret;
ret = regmap_read(regmap, AXI_PWMGEN_CHX_OFFSET(ch), &wfhw->duty_offset_cnt); if (ret) return ret;
if (wfhw->duty_cycle_cnt > wfhw->period_cnt)
wfhw->duty_cycle_cnt = wfhw->period_cnt;
/* XXX: is this the actual behaviour of the hardware? */ if (wfhw->duty_offset_cnt >= wfhw->period_cnt) {
wfhw->duty_cycle_cnt = 0;
wfhw->duty_offset_cnt = 0;
}
ret = regmap_read(regmap, AXI_PWMGEN_REG_CORE_MAGIC, &val); if (ret) return ret;
if (val != AXI_PWMGEN_REG_CORE_MAGIC_VAL) return dev_err_probe(dev, -ENODEV, "failed to read expected value from register: got %08x, expected %08x\n",
val, AXI_PWMGEN_REG_CORE_MAGIC_VAL);
ret = regmap_read(regmap, ADI_AXI_REG_VERSION, &val); if (ret) return ret;
if (ADI_AXI_PCORE_VER_MAJOR(val) != 2) { return dev_err_probe(dev, -ENODEV, "Unsupported peripheral version %u.%u.%u\n",
ADI_AXI_PCORE_VER_MAJOR(val),
ADI_AXI_PCORE_VER_MINOR(val),
ADI_AXI_PCORE_VER_PATCH(val));
}
/* Enable the core */
ret = regmap_clear_bits(regmap, AXI_PWMGEN_REG_RSTN, AXI_PWMGEN_REG_RSTN_RESET); if (ret) return ret;
/* * Enable force align so that changes to PWM period and duty cycle take * effect immediately. Otherwise, the effect of the change is delayed * until the period of all channels run out, which can be long after the * apply function returns.
*/
ret = regmap_set_bits(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_REG_CONFIG_FORCE_ALIGN); if (ret) return ret;
ret = regmap_read(regmap, AXI_PWMGEN_REG_NPWM, &val); if (ret) return ret;
/* * Using NULL here instead of "axi" for backwards compatibility. There * are some dtbs that don't give clock-names and have the "ext" clock * as the one and only clock (due to mistake in the original bindings).
*/
axi_clk = devm_clk_get_enabled(dev, NULL); if (IS_ERR(axi_clk)) return dev_err_probe(dev, PTR_ERR(axi_clk), "failed to get axi clock\n");
clk = devm_clk_get_optional_enabled(dev, "ext"); if (IS_ERR(clk)) return dev_err_probe(dev, PTR_ERR(clk), "failed to get ext clock\n");
/* * If there is no "ext" clock, it means the HDL was compiled with * ASYNC_CLK_EN=0. In this case, the AXI clock is also used for the * PWM output clock.
*/ if (!clk)
clk = axi_clk;
ret = devm_clk_rate_exclusive_get(dev, clk); if (ret) return dev_err_probe(dev, ret, "failed to get exclusive rate\n");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sergiu Cuciurean ");
MODULE_AUTHOR("Trevor Gamblin ");
MODULE_DESCRIPTION("Driver for the Analog Devices AXI PWM generator");
Messung V0.5
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
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.