1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018 SiFive
4 * For SiFive's PWM IP block documentation please refer Chapter 14 of
5 * Reference Manual : https://static.dev.sifive.com/FU540-C000-v1.0.pdf
8 * - When changing both duty cycle and period, we cannot prevent in
9 * software that the output might produce a period with mixed
10 * settings (new period length and old duty cycle).
11 * - The hardware cannot generate a 100% duty cycle.
12 * - The hardware generates only inverted output.
14 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
20 #include <linux/bitfield.h>
22 /* Register offsets */
23 #define PWM_SIFIVE_PWMCFG 0x0
24 #define PWM_SIFIVE_PWMCOUNT 0x8
25 #define PWM_SIFIVE_PWMS 0x10
26 #define PWM_SIFIVE_PWMCMP0 0x20
29 #define PWM_SIFIVE_PWMCFG_SCALE GENMASK(3, 0)
30 #define PWM_SIFIVE_PWMCFG_STICKY BIT(8)
31 #define PWM_SIFIVE_PWMCFG_ZERO_CMP BIT(9)
32 #define PWM_SIFIVE_PWMCFG_DEGLITCH BIT(10)
33 #define PWM_SIFIVE_PWMCFG_EN_ALWAYS BIT(12)
34 #define PWM_SIFIVE_PWMCFG_EN_ONCE BIT(13)
35 #define PWM_SIFIVE_PWMCFG_CENTER BIT(16)
36 #define PWM_SIFIVE_PWMCFG_GANG BIT(24)
37 #define PWM_SIFIVE_PWMCFG_IP BIT(28)
39 /* PWM_SIFIVE_SIZE_PWMCMP is used to calculate offset for pwmcmpX registers */
40 #define PWM_SIFIVE_SIZE_PWMCMP 4
41 #define PWM_SIFIVE_CMPWIDTH 16
42 #define PWM_SIFIVE_DEFAULT_PERIOD 10000000
44 struct pwm_sifive_ddata {
46 struct mutex lock; /* lock to protect user_count */
47 struct notifier_block notifier;
50 unsigned int real_period;
51 unsigned int approx_period;
56 struct pwm_sifive_ddata *pwm_sifive_chip_to_ddata(struct pwm_chip *c)
58 return container_of(c, struct pwm_sifive_ddata, chip);
61 static int pwm_sifive_request(struct pwm_chip *chip, struct pwm_device *pwm)
63 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
65 mutex_lock(&ddata->lock);
67 mutex_unlock(&ddata->lock);
72 static void pwm_sifive_free(struct pwm_chip *chip, struct pwm_device *pwm)
74 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
76 mutex_lock(&ddata->lock);
78 mutex_unlock(&ddata->lock);
81 static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata,
84 unsigned long long num;
85 unsigned long scale_pow;
89 * The PWM unit is used with pwmzerocmp=0, so the only way to modify the
90 * period length is using pwmscale which provides the number of bits the
91 * counter is shifted before being feed to the comparators. A period
92 * lasts (1 << (PWM_SIFIVE_CMPWIDTH + pwmscale)) clock ticks.
93 * (1 << (PWM_SIFIVE_CMPWIDTH + scale)) * 10^9/rate = period
95 scale_pow = div64_ul(ddata->approx_period * (u64)rate, NSEC_PER_SEC);
96 scale = clamp(ilog2(scale_pow) - PWM_SIFIVE_CMPWIDTH, 0, 0xf);
98 val = PWM_SIFIVE_PWMCFG_EN_ALWAYS |
99 FIELD_PREP(PWM_SIFIVE_PWMCFG_SCALE, scale);
100 writel(val, ddata->regs + PWM_SIFIVE_PWMCFG);
102 /* As scale <= 15 the shift operation cannot overflow. */
103 num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale);
104 ddata->real_period = div64_ul(num, rate);
105 dev_dbg(ddata->chip.dev,
106 "New real_period = %u ns\n", ddata->real_period);
109 static void pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
110 struct pwm_state *state)
112 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
115 duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP0 +
116 pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP);
118 state->enabled = duty > 0;
120 val = readl(ddata->regs + PWM_SIFIVE_PWMCFG);
121 if (!(val & PWM_SIFIVE_PWMCFG_EN_ALWAYS))
122 state->enabled = false;
124 state->period = ddata->real_period;
126 (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
127 state->polarity = PWM_POLARITY_INVERSED;
130 static int pwm_sifive_enable(struct pwm_chip *chip, bool enable)
132 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
136 ret = clk_enable(ddata->clk);
138 dev_err(ddata->chip.dev, "Enable clk failed\n");
144 clk_disable(ddata->clk);
149 static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
150 const struct pwm_state *state)
152 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
153 struct pwm_state cur_state;
154 unsigned int duty_cycle;
155 unsigned long long num;
160 if (state->polarity != PWM_POLARITY_INVERSED)
163 ret = clk_enable(ddata->clk);
165 dev_err(ddata->chip.dev, "Enable clk failed\n");
169 mutex_lock(&ddata->lock);
170 cur_state = pwm->state;
171 enabled = cur_state.enabled;
173 duty_cycle = state->duty_cycle;
178 * The problem of output producing mixed setting as mentioned at top,
179 * occurs here. To minimize the window for this problem, we are
180 * calculating the register values first and then writing them
183 num = (u64)duty_cycle * (1U << PWM_SIFIVE_CMPWIDTH);
184 frac = DIV64_U64_ROUND_CLOSEST(num, state->period);
185 /* The hardware cannot generate a 100% duty cycle */
186 frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
188 if (state->period != ddata->approx_period) {
189 if (ddata->user_count != 1) {
193 ddata->approx_period = state->period;
194 pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk));
197 writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP0 +
198 pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP);
200 if (state->enabled != enabled)
201 pwm_sifive_enable(chip, state->enabled);
204 clk_disable(ddata->clk);
205 mutex_unlock(&ddata->lock);
209 static const struct pwm_ops pwm_sifive_ops = {
210 .request = pwm_sifive_request,
211 .free = pwm_sifive_free,
212 .get_state = pwm_sifive_get_state,
213 .apply = pwm_sifive_apply,
214 .owner = THIS_MODULE,
217 static int pwm_sifive_clock_notifier(struct notifier_block *nb,
218 unsigned long event, void *data)
220 struct clk_notifier_data *ndata = data;
221 struct pwm_sifive_ddata *ddata =
222 container_of(nb, struct pwm_sifive_ddata, notifier);
224 if (event == POST_RATE_CHANGE)
225 pwm_sifive_update_clock(ddata, ndata->new_rate);
230 static int pwm_sifive_probe(struct platform_device *pdev)
232 struct device *dev = &pdev->dev;
233 struct pwm_sifive_ddata *ddata;
234 struct pwm_chip *chip;
237 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
241 mutex_init(&ddata->lock);
244 chip->ops = &pwm_sifive_ops;
247 ddata->regs = devm_platform_ioremap_resource(pdev, 0);
248 if (IS_ERR(ddata->regs))
249 return PTR_ERR(ddata->regs);
251 ddata->clk = devm_clk_get(dev, NULL);
252 if (IS_ERR(ddata->clk))
253 return dev_err_probe(dev, PTR_ERR(ddata->clk),
254 "Unable to find controller clock\n");
256 ret = clk_prepare_enable(ddata->clk);
258 dev_err(dev, "failed to enable clock for pwm: %d\n", ret);
262 /* Watch for changes to underlying clock frequency */
263 ddata->notifier.notifier_call = pwm_sifive_clock_notifier;
264 ret = clk_notifier_register(ddata->clk, &ddata->notifier);
266 dev_err(dev, "failed to register clock notifier: %d\n", ret);
270 ret = pwmchip_add(chip);
272 dev_err(dev, "cannot register PWM: %d\n", ret);
276 platform_set_drvdata(pdev, ddata);
277 dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm);
282 clk_notifier_unregister(ddata->clk, &ddata->notifier);
284 clk_disable_unprepare(ddata->clk);
289 static int pwm_sifive_remove(struct platform_device *dev)
291 struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev);
292 bool is_enabled = false;
293 struct pwm_device *pwm;
296 for (ch = 0; ch < ddata->chip.npwm; ch++) {
297 pwm = &ddata->chip.pwms[ch];
298 if (pwm->state.enabled) {
304 clk_disable(ddata->clk);
306 clk_disable_unprepare(ddata->clk);
307 pwmchip_remove(&ddata->chip);
308 clk_notifier_unregister(ddata->clk, &ddata->notifier);
313 static const struct of_device_id pwm_sifive_of_match[] = {
314 { .compatible = "sifive,pwm0" },
317 MODULE_DEVICE_TABLE(of, pwm_sifive_of_match);
319 static struct platform_driver pwm_sifive_driver = {
320 .probe = pwm_sifive_probe,
321 .remove = pwm_sifive_remove,
323 .name = "pwm-sifive",
324 .of_match_table = pwm_sifive_of_match,
327 module_platform_driver(pwm_sifive_driver);
329 MODULE_DESCRIPTION("SiFive PWM driver");
330 MODULE_LICENSE("GPL v2");