1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices AXI PWM generator
5 * Copyright 2024 Analog Devices Inc.
6 * Copyright 2024 Baylibre SAS
8 * Device docs: https://analogdevicesinc.github.io/hdl/library/axi_pwm_gen/index.html
11 * - The writes to registers for period and duty are shadowed until
12 * LOAD_CONFIG is written to AXI_PWMGEN_REG_RSTN, at which point
14 * - Writing LOAD_CONFIG also has the effect of re-synchronizing all
15 * enabled channels, which could cause glitching on other channels. It
16 * is therefore expected that channels are assigned harmonic periods
17 * and all have a single user coordinating this.
18 * - Supports normal polarity. Does not support changing polarity.
19 * - On disable, the PWM output becomes low (inactive).
21 #include <linux/bits.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
24 #include <linux/fpga/adi-axi-common.h>
26 #include <linux/minmax.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/pwm.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
33 #define AXI_PWMGEN_REG_ID 0x04
34 #define AXI_PWMGEN_REG_SCRATCHPAD 0x08
35 #define AXI_PWMGEN_REG_CORE_MAGIC 0x0C
36 #define AXI_PWMGEN_REG_RSTN 0x10
37 #define AXI_PWMGEN_REG_RSTN_LOAD_CONFIG BIT(1)
38 #define AXI_PWMGEN_REG_RSTN_RESET BIT(0)
39 #define AXI_PWMGEN_REG_NPWM 0x14
40 #define AXI_PWMGEN_REG_CONFIG 0x18
41 #define AXI_PWMGEN_REG_CONFIG_FORCE_ALIGN BIT(1)
42 #define AXI_PWMGEN_CHX_PERIOD(ch) (0x40 + (4 * (ch)))
43 #define AXI_PWMGEN_CHX_DUTY(ch) (0x80 + (4 * (ch)))
44 #define AXI_PWMGEN_CHX_OFFSET(ch) (0xC0 + (4 * (ch)))
45 #define AXI_PWMGEN_REG_CORE_MAGIC_VAL 0x601A3471 /* Identification number to test during setup */
47 struct axi_pwmgen_ddata {
48 struct regmap *regmap;
49 unsigned long clk_rate_hz;
52 static const struct regmap_config axi_pwmgen_regmap_config = {
59 /* This represents a hardware configuration for one channel */
60 struct axi_pwmgen_waveform {
66 static struct axi_pwmgen_ddata *axi_pwmgen_ddata_from_chip(struct pwm_chip *chip)
68 return pwmchip_get_drvdata(chip);
71 static int axi_pwmgen_round_waveform_tohw(struct pwm_chip *chip,
72 struct pwm_device *pwm,
73 const struct pwm_waveform *wf,
76 struct axi_pwmgen_waveform *wfhw = _wfhw;
77 struct axi_pwmgen_ddata *ddata = axi_pwmgen_ddata_from_chip(chip);
79 if (wf->period_length_ns == 0) {
80 *wfhw = (struct axi_pwmgen_waveform){
86 /* With ddata->clk_rate_hz < NSEC_PER_SEC this won't overflow. */
87 wfhw->period_cnt = min_t(u64,
88 mul_u64_u32_div(wf->period_length_ns, ddata->clk_rate_hz, NSEC_PER_SEC),
91 if (wfhw->period_cnt == 0) {
93 * The specified period is too short for the hardware.
94 * Let's round .duty_cycle down to 0 to get a (somewhat)
98 wfhw->duty_cycle_cnt = 0;
99 wfhw->duty_offset_cnt = 0;
101 wfhw->duty_cycle_cnt = min_t(u64,
102 mul_u64_u32_div(wf->duty_length_ns, ddata->clk_rate_hz, NSEC_PER_SEC),
104 wfhw->duty_offset_cnt = min_t(u64,
105 mul_u64_u32_div(wf->duty_offset_ns, ddata->clk_rate_hz, NSEC_PER_SEC),
110 dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> PERIOD: %08x, DUTY: %08x, OFFSET: %08x\n",
111 pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
112 ddata->clk_rate_hz, wfhw->period_cnt, wfhw->duty_cycle_cnt, wfhw->duty_offset_cnt);
117 static int axi_pwmgen_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
118 const void *_wfhw, struct pwm_waveform *wf)
120 const struct axi_pwmgen_waveform *wfhw = _wfhw;
121 struct axi_pwmgen_ddata *ddata = axi_pwmgen_ddata_from_chip(chip);
123 wf->period_length_ns = DIV64_U64_ROUND_UP((u64)wfhw->period_cnt * NSEC_PER_SEC,
126 wf->duty_length_ns = DIV64_U64_ROUND_UP((u64)wfhw->duty_cycle_cnt * NSEC_PER_SEC,
129 wf->duty_offset_ns = DIV64_U64_ROUND_UP((u64)wfhw->duty_offset_cnt * NSEC_PER_SEC,
135 static int axi_pwmgen_write_waveform(struct pwm_chip *chip,
136 struct pwm_device *pwm,
139 const struct axi_pwmgen_waveform *wfhw = _wfhw;
140 struct axi_pwmgen_ddata *ddata = axi_pwmgen_ddata_from_chip(chip);
141 struct regmap *regmap = ddata->regmap;
142 unsigned int ch = pwm->hwpwm;
145 ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), wfhw->period_cnt);
149 ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), wfhw->duty_cycle_cnt);
153 ret = regmap_write(regmap, AXI_PWMGEN_CHX_OFFSET(ch), wfhw->duty_offset_cnt);
157 return regmap_write(regmap, AXI_PWMGEN_REG_RSTN, AXI_PWMGEN_REG_RSTN_LOAD_CONFIG);
160 static int axi_pwmgen_read_waveform(struct pwm_chip *chip,
161 struct pwm_device *pwm,
164 struct axi_pwmgen_waveform *wfhw = _wfhw;
165 struct axi_pwmgen_ddata *ddata = axi_pwmgen_ddata_from_chip(chip);
166 struct regmap *regmap = ddata->regmap;
167 unsigned int ch = pwm->hwpwm;
170 ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &wfhw->period_cnt);
174 ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &wfhw->duty_cycle_cnt);
178 ret = regmap_read(regmap, AXI_PWMGEN_CHX_OFFSET(ch), &wfhw->duty_offset_cnt);
182 if (wfhw->duty_cycle_cnt > wfhw->period_cnt)
183 wfhw->duty_cycle_cnt = wfhw->period_cnt;
185 /* XXX: is this the actual behaviour of the hardware? */
186 if (wfhw->duty_offset_cnt >= wfhw->period_cnt) {
187 wfhw->duty_cycle_cnt = 0;
188 wfhw->duty_offset_cnt = 0;
194 static const struct pwm_ops axi_pwmgen_pwm_ops = {
195 .sizeof_wfhw = sizeof(struct axi_pwmgen_waveform),
196 .round_waveform_tohw = axi_pwmgen_round_waveform_tohw,
197 .round_waveform_fromhw = axi_pwmgen_round_waveform_fromhw,
198 .read_waveform = axi_pwmgen_read_waveform,
199 .write_waveform = axi_pwmgen_write_waveform,
202 static int axi_pwmgen_setup(struct regmap *regmap, struct device *dev)
207 ret = regmap_read(regmap, AXI_PWMGEN_REG_CORE_MAGIC, &val);
211 if (val != AXI_PWMGEN_REG_CORE_MAGIC_VAL)
212 return dev_err_probe(dev, -ENODEV,
213 "failed to read expected value from register: got %08x, expected %08x\n",
214 val, AXI_PWMGEN_REG_CORE_MAGIC_VAL);
216 ret = regmap_read(regmap, ADI_AXI_REG_VERSION, &val);
220 if (ADI_AXI_PCORE_VER_MAJOR(val) != 2) {
221 return dev_err_probe(dev, -ENODEV, "Unsupported peripheral version %u.%u.%u\n",
222 ADI_AXI_PCORE_VER_MAJOR(val),
223 ADI_AXI_PCORE_VER_MINOR(val),
224 ADI_AXI_PCORE_VER_PATCH(val));
227 /* Enable the core */
228 ret = regmap_clear_bits(regmap, AXI_PWMGEN_REG_RSTN, AXI_PWMGEN_REG_RSTN_RESET);
233 * Enable force align so that changes to PWM period and duty cycle take
234 * effect immediately. Otherwise, the effect of the change is delayed
235 * until the period of all channels run out, which can be long after the
236 * apply function returns.
238 ret = regmap_set_bits(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_REG_CONFIG_FORCE_ALIGN);
242 ret = regmap_read(regmap, AXI_PWMGEN_REG_NPWM, &val);
246 /* Return the number of PWMs */
250 static int axi_pwmgen_probe(struct platform_device *pdev)
252 struct device *dev = &pdev->dev;
253 struct regmap *regmap;
254 struct pwm_chip *chip;
255 struct axi_pwmgen_ddata *ddata;
257 void __iomem *io_base;
260 io_base = devm_platform_ioremap_resource(pdev, 0);
262 return PTR_ERR(io_base);
264 regmap = devm_regmap_init_mmio(dev, io_base, &axi_pwmgen_regmap_config);
266 return dev_err_probe(dev, PTR_ERR(regmap),
267 "failed to init register map\n");
269 ret = axi_pwmgen_setup(regmap, dev);
273 chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
275 return PTR_ERR(chip);
276 ddata = pwmchip_get_drvdata(chip);
277 ddata->regmap = regmap;
279 clk = devm_clk_get_enabled(dev, NULL);
281 return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
283 ret = devm_clk_rate_exclusive_get(dev, clk);
285 return dev_err_probe(dev, ret, "failed to get exclusive rate\n");
287 ddata->clk_rate_hz = clk_get_rate(clk);
288 if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC)
289 return dev_err_probe(dev, -EINVAL,
290 "Invalid clock rate: %lu\n", ddata->clk_rate_hz);
292 chip->ops = &axi_pwmgen_pwm_ops;
295 ret = devm_pwmchip_add(dev, chip);
297 return dev_err_probe(dev, ret, "could not add PWM chip\n");
302 static const struct of_device_id axi_pwmgen_ids[] = {
303 { .compatible = "adi,axi-pwmgen-2.00.a" },
306 MODULE_DEVICE_TABLE(of, axi_pwmgen_ids);
308 static struct platform_driver axi_pwmgen_driver = {
310 .name = "axi-pwmgen",
311 .of_match_table = axi_pwmgen_ids,
313 .probe = axi_pwmgen_probe,
315 module_platform_driver(axi_pwmgen_driver);
317 MODULE_LICENSE("GPL");
320 MODULE_DESCRIPTION("Driver for the Analog Devices AXI PWM generator");