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 and approx_period */
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 /* Called holding ddata->lock */
82 static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata,
85 unsigned long long num;
86 unsigned long scale_pow;
90 * The PWM unit is used with pwmzerocmp=0, so the only way to modify the
91 * period length is using pwmscale which provides the number of bits the
92 * counter is shifted before being feed to the comparators. A period
93 * lasts (1 << (PWM_SIFIVE_CMPWIDTH + pwmscale)) clock ticks.
94 * (1 << (PWM_SIFIVE_CMPWIDTH + scale)) * 10^9/rate = period
96 scale_pow = div64_ul(ddata->approx_period * (u64)rate, NSEC_PER_SEC);
97 scale = clamp(ilog2(scale_pow) - PWM_SIFIVE_CMPWIDTH, 0, 0xf);
99 val = PWM_SIFIVE_PWMCFG_EN_ALWAYS |
100 FIELD_PREP(PWM_SIFIVE_PWMCFG_SCALE, scale);
101 writel(val, ddata->regs + PWM_SIFIVE_PWMCFG);
103 /* As scale <= 15 the shift operation cannot overflow. */
104 num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale);
105 ddata->real_period = div64_ul(num, rate);
106 dev_dbg(ddata->chip.dev,
107 "New real_period = %u ns\n", ddata->real_period);
110 static void pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
111 struct pwm_state *state)
113 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
116 duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP0 +
117 pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP);
119 state->enabled = duty > 0;
121 val = readl(ddata->regs + PWM_SIFIVE_PWMCFG);
122 if (!(val & PWM_SIFIVE_PWMCFG_EN_ALWAYS))
123 state->enabled = false;
125 state->period = ddata->real_period;
127 (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
128 state->polarity = PWM_POLARITY_INVERSED;
131 static int pwm_sifive_enable(struct pwm_chip *chip, bool enable)
133 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
137 ret = clk_enable(ddata->clk);
139 dev_err(ddata->chip.dev, "Enable clk failed\n");
145 clk_disable(ddata->clk);
150 static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
151 const struct pwm_state *state)
153 struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
154 struct pwm_state cur_state;
155 unsigned int duty_cycle;
156 unsigned long long num;
161 if (state->polarity != PWM_POLARITY_INVERSED)
164 ret = clk_enable(ddata->clk);
166 dev_err(ddata->chip.dev, "Enable clk failed\n");
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 = DIV_ROUND_CLOSEST_ULL(num, state->period);
185 /* The hardware cannot generate a 100% duty cycle */
186 frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
188 mutex_lock(&ddata->lock);
189 if (state->period != ddata->approx_period) {
191 * Don't let a 2nd user change the period underneath the 1st user.
192 * However if ddate->approx_period == 0 this is the first time we set
193 * any period, so let whoever gets here first set the period so other
194 * users who agree on the period won't fail.
196 if (ddata->user_count != 1 && ddata->approx_period) {
197 mutex_unlock(&ddata->lock);
201 ddata->approx_period = state->period;
202 pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk));
204 mutex_unlock(&ddata->lock);
206 writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP0 +
207 pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP);
209 if (state->enabled != enabled)
210 pwm_sifive_enable(chip, state->enabled);
213 clk_disable(ddata->clk);
217 static const struct pwm_ops pwm_sifive_ops = {
218 .request = pwm_sifive_request,
219 .free = pwm_sifive_free,
220 .get_state = pwm_sifive_get_state,
221 .apply = pwm_sifive_apply,
222 .owner = THIS_MODULE,
225 static int pwm_sifive_clock_notifier(struct notifier_block *nb,
226 unsigned long event, void *data)
228 struct clk_notifier_data *ndata = data;
229 struct pwm_sifive_ddata *ddata =
230 container_of(nb, struct pwm_sifive_ddata, notifier);
232 if (event == POST_RATE_CHANGE) {
233 mutex_lock(&ddata->lock);
234 pwm_sifive_update_clock(ddata, ndata->new_rate);
235 mutex_unlock(&ddata->lock);
241 static int pwm_sifive_probe(struct platform_device *pdev)
243 struct device *dev = &pdev->dev;
244 struct pwm_sifive_ddata *ddata;
245 struct pwm_chip *chip;
246 struct resource *res;
249 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
253 mutex_init(&ddata->lock);
256 chip->ops = &pwm_sifive_ops;
257 chip->of_xlate = of_pwm_xlate_with_flags;
258 chip->of_pwm_n_cells = 3;
262 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
263 ddata->regs = devm_ioremap_resource(dev, res);
264 if (IS_ERR(ddata->regs))
265 return PTR_ERR(ddata->regs);
267 ddata->clk = devm_clk_get(dev, NULL);
268 if (IS_ERR(ddata->clk)) {
269 if (PTR_ERR(ddata->clk) != -EPROBE_DEFER)
270 dev_err(dev, "Unable to find controller clock\n");
271 return PTR_ERR(ddata->clk);
274 ret = clk_prepare_enable(ddata->clk);
276 dev_err(dev, "failed to enable clock for pwm: %d\n", ret);
280 /* Watch for changes to underlying clock frequency */
281 ddata->notifier.notifier_call = pwm_sifive_clock_notifier;
282 ret = clk_notifier_register(ddata->clk, &ddata->notifier);
284 dev_err(dev, "failed to register clock notifier: %d\n", ret);
288 ret = pwmchip_add(chip);
290 dev_err(dev, "cannot register PWM: %d\n", ret);
294 platform_set_drvdata(pdev, ddata);
295 dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm);
300 clk_notifier_unregister(ddata->clk, &ddata->notifier);
302 clk_disable_unprepare(ddata->clk);
307 static int pwm_sifive_remove(struct platform_device *dev)
309 struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev);
310 bool is_enabled = false;
311 struct pwm_device *pwm;
314 for (ch = 0; ch < ddata->chip.npwm; ch++) {
315 pwm = &ddata->chip.pwms[ch];
316 if (pwm->state.enabled) {
322 clk_disable(ddata->clk);
324 clk_disable_unprepare(ddata->clk);
325 ret = pwmchip_remove(&ddata->chip);
326 clk_notifier_unregister(ddata->clk, &ddata->notifier);
331 static const struct of_device_id pwm_sifive_of_match[] = {
332 { .compatible = "sifive,pwm0" },
335 MODULE_DEVICE_TABLE(of, pwm_sifive_of_match);
337 static struct platform_driver pwm_sifive_driver = {
338 .probe = pwm_sifive_probe,
339 .remove = pwm_sifive_remove,
341 .name = "pwm-sifive",
342 .of_match_table = pwm_sifive_of_match,
345 module_platform_driver(pwm_sifive_driver);
347 MODULE_DESCRIPTION("SiFive PWM driver");
348 MODULE_LICENSE("GPL v2");