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_CONFIG, 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/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/pwm.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
32 #define AXI_PWMGEN_REG_CORE_VERSION 0x00
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_CONFIG 0x10
37 #define AXI_PWMGEN_REG_NPWM 0x14
38 #define AXI_PWMGEN_CHX_PERIOD(ch) (0x40 + (4 * (ch)))
39 #define AXI_PWMGEN_CHX_DUTY(ch) (0x80 + (4 * (ch)))
40 #define AXI_PWMGEN_CHX_OFFSET(ch) (0xC0 + (4 * (ch)))
41 #define AXI_PWMGEN_REG_CORE_MAGIC_VAL 0x601A3471 /* Identification number to test during setup */
42 #define AXI_PWMGEN_LOAD_CONFIG BIT(1)
43 #define AXI_PWMGEN_REG_CONFIG_RESET BIT(0)
45 struct axi_pwmgen_ddata {
46 struct regmap *regmap;
47 unsigned long clk_rate_hz;
50 static const struct regmap_config axi_pwmgen_regmap_config = {
57 static int axi_pwmgen_apply(struct pwm_chip *chip, struct pwm_device *pwm,
58 const struct pwm_state *state)
60 struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
61 unsigned int ch = pwm->hwpwm;
62 struct regmap *regmap = ddata->regmap;
63 u64 period_cnt, duty_cnt;
66 if (state->polarity != PWM_POLARITY_NORMAL)
70 period_cnt = mul_u64_u64_div_u64(state->period, ddata->clk_rate_hz, NSEC_PER_SEC);
71 if (period_cnt > UINT_MAX)
72 period_cnt = UINT_MAX;
77 ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), period_cnt);
81 duty_cnt = mul_u64_u64_div_u64(state->duty_cycle, ddata->clk_rate_hz, NSEC_PER_SEC);
82 if (duty_cnt > UINT_MAX)
85 ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), duty_cnt);
89 ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), 0);
93 ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), 0);
98 return regmap_write(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_LOAD_CONFIG);
101 static int axi_pwmgen_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
102 struct pwm_state *state)
104 struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
105 struct regmap *regmap = ddata->regmap;
106 unsigned int ch = pwm->hwpwm;
110 ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &cnt);
114 state->enabled = cnt != 0;
116 state->period = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
118 ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &cnt);
122 state->duty_cycle = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
124 state->polarity = PWM_POLARITY_NORMAL;
129 static const struct pwm_ops axi_pwmgen_pwm_ops = {
130 .apply = axi_pwmgen_apply,
131 .get_state = axi_pwmgen_get_state,
134 static int axi_pwmgen_setup(struct regmap *regmap, struct device *dev)
139 ret = regmap_read(regmap, AXI_PWMGEN_REG_CORE_MAGIC, &val);
143 if (val != AXI_PWMGEN_REG_CORE_MAGIC_VAL)
144 return dev_err_probe(dev, -ENODEV,
145 "failed to read expected value from register: got %08x, expected %08x\n",
146 val, AXI_PWMGEN_REG_CORE_MAGIC_VAL);
148 ret = regmap_read(regmap, AXI_PWMGEN_REG_CORE_VERSION, &val);
152 if (ADI_AXI_PCORE_VER_MAJOR(val) != 2) {
153 return dev_err_probe(dev, -ENODEV, "Unsupported peripheral version %u.%u.%u\n",
154 ADI_AXI_PCORE_VER_MAJOR(val),
155 ADI_AXI_PCORE_VER_MINOR(val),
156 ADI_AXI_PCORE_VER_PATCH(val));
159 /* Enable the core */
160 ret = regmap_clear_bits(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_REG_CONFIG_RESET);
164 ret = regmap_read(regmap, AXI_PWMGEN_REG_NPWM, &val);
168 /* Return the number of PWMs */
172 static int axi_pwmgen_probe(struct platform_device *pdev)
174 struct device *dev = &pdev->dev;
175 struct regmap *regmap;
176 struct pwm_chip *chip;
177 struct axi_pwmgen_ddata *ddata;
179 void __iomem *io_base;
182 io_base = devm_platform_ioremap_resource(pdev, 0);
184 return PTR_ERR(io_base);
186 regmap = devm_regmap_init_mmio(dev, io_base, &axi_pwmgen_regmap_config);
188 return dev_err_probe(dev, PTR_ERR(regmap),
189 "failed to init register map\n");
191 ret = axi_pwmgen_setup(regmap, dev);
195 chip = devm_pwmchip_alloc(dev, ret, sizeof(*ddata));
197 return PTR_ERR(chip);
198 ddata = pwmchip_get_drvdata(chip);
199 ddata->regmap = regmap;
201 clk = devm_clk_get_enabled(dev, NULL);
203 return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
205 ret = devm_clk_rate_exclusive_get(dev, clk);
207 return dev_err_probe(dev, ret, "failed to get exclusive rate\n");
209 ddata->clk_rate_hz = clk_get_rate(clk);
210 if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC)
211 return dev_err_probe(dev, -EINVAL,
212 "Invalid clock rate: %lu\n", ddata->clk_rate_hz);
214 chip->ops = &axi_pwmgen_pwm_ops;
217 ret = devm_pwmchip_add(dev, chip);
219 return dev_err_probe(dev, ret, "could not add PWM chip\n");
224 static const struct of_device_id axi_pwmgen_ids[] = {
225 { .compatible = "adi,axi-pwmgen-2.00.a" },
228 MODULE_DEVICE_TABLE(of, axi_pwmgen_ids);
230 static struct platform_driver axi_pwmgen_driver = {
232 .name = "axi-pwmgen",
233 .of_match_table = axi_pwmgen_ids,
235 .probe = axi_pwmgen_probe,
237 module_platform_driver(axi_pwmgen_driver);
239 MODULE_LICENSE("GPL");
242 MODULE_DESCRIPTION("Driver for the Analog Devices AXI PWM generator");