1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale FlexTimer Module (FTM) PWM Driver
5 * Copyright 2012-2013 Freescale Semiconductor, Inc.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/platform_device.h>
17 #include <linux/pwm.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/fsl/ftm.h>
22 #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
36 struct fsl_pwm_periodcfg {
37 enum fsl_pwm_clk clk_select;
39 unsigned int mod_period;
45 struct regmap *regmap;
47 /* This value is valid iff a pwm is running */
48 struct fsl_pwm_periodcfg period;
51 struct clk *clk[FSL_PWM_CLK_MAX];
53 const struct fsl_ftm_soc *soc;
56 static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
58 return container_of(chip, struct fsl_pwm_chip, chip);
61 static void ftm_clear_write_protection(struct fsl_pwm_chip *fpc)
65 regmap_read(fpc->regmap, FTM_FMS, &val);
66 if (val & FTM_FMS_WPEN)
67 regmap_set_bits(fpc->regmap, FTM_MODE, FTM_MODE_WPDIS);
70 static void ftm_set_write_protection(struct fsl_pwm_chip *fpc)
72 regmap_set_bits(fpc->regmap, FTM_FMS, FTM_FMS_WPEN);
75 static bool fsl_pwm_periodcfg_are_equal(const struct fsl_pwm_periodcfg *a,
76 const struct fsl_pwm_periodcfg *b)
78 if (a->clk_select != b->clk_select)
80 if (a->clk_ps != b->clk_ps)
82 if (a->mod_period != b->mod_period)
87 static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
90 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
92 ret = clk_prepare_enable(fpc->ipg_clk);
93 if (!ret && fpc->soc->has_enable_bits) {
94 mutex_lock(&fpc->lock);
95 regmap_set_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16));
96 mutex_unlock(&fpc->lock);
102 static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
104 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
106 if (fpc->soc->has_enable_bits) {
107 mutex_lock(&fpc->lock);
108 regmap_clear_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16));
109 mutex_unlock(&fpc->lock);
112 clk_disable_unprepare(fpc->ipg_clk);
115 static unsigned int fsl_pwm_ticks_to_ns(struct fsl_pwm_chip *fpc,
119 unsigned long long exval;
121 rate = clk_get_rate(fpc->clk[fpc->period.clk_select]);
123 exval *= 1000000000UL;
124 do_div(exval, rate >> fpc->period.clk_ps);
128 static bool fsl_pwm_calculate_period_clk(struct fsl_pwm_chip *fpc,
129 unsigned int period_ns,
130 enum fsl_pwm_clk index,
131 struct fsl_pwm_periodcfg *periodcfg
134 unsigned long long c;
137 c = clk_get_rate(fpc->clk[index]);
139 do_div(c, 1000000000UL);
144 for (ps = 0; ps < 8 ; ++ps, c >>= 1) {
146 periodcfg->clk_select = index;
147 periodcfg->clk_ps = ps;
148 periodcfg->mod_period = c - 1;
155 static bool fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
156 unsigned int period_ns,
157 struct fsl_pwm_periodcfg *periodcfg)
159 enum fsl_pwm_clk m0, m1;
160 unsigned long fix_rate, ext_rate;
163 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, FSL_PWM_CLK_SYS,
168 fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
169 ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
171 if (fix_rate > ext_rate) {
172 m0 = FSL_PWM_CLK_FIX;
173 m1 = FSL_PWM_CLK_EXT;
175 m0 = FSL_PWM_CLK_EXT;
176 m1 = FSL_PWM_CLK_FIX;
179 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, m0, periodcfg);
183 return fsl_pwm_calculate_period_clk(fpc, period_ns, m1, periodcfg);
186 static unsigned int fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
187 unsigned int duty_ns)
189 unsigned long long duty;
191 unsigned int period = fpc->period.mod_period + 1;
192 unsigned int period_ns = fsl_pwm_ticks_to_ns(fpc, period);
194 duty = (unsigned long long)duty_ns * period;
195 do_div(duty, period_ns);
197 return (unsigned int)duty;
200 static bool fsl_pwm_is_any_pwm_enabled(struct fsl_pwm_chip *fpc,
201 struct pwm_device *pwm)
205 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
212 static bool fsl_pwm_is_other_pwm_enabled(struct fsl_pwm_chip *fpc,
213 struct pwm_device *pwm)
217 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
218 if (~(val | BIT(pwm->hwpwm)) & 0xFF)
224 static int fsl_pwm_apply_config(struct fsl_pwm_chip *fpc,
225 struct pwm_device *pwm,
226 const struct pwm_state *newstate)
231 struct fsl_pwm_periodcfg periodcfg;
232 bool do_write_period = false;
234 if (!fsl_pwm_calculate_period(fpc, newstate->period, &periodcfg)) {
235 dev_err(fpc->chip.dev, "failed to calculate new period\n");
239 if (!fsl_pwm_is_any_pwm_enabled(fpc, pwm))
240 do_write_period = true;
242 * The Freescale FTM controller supports only a single period for
243 * all PWM channels, therefore verify if the newly computed period
244 * is different than the current period being used. In such case
245 * we allow to change the period only if no other pwm is running.
247 else if (!fsl_pwm_periodcfg_are_equal(&fpc->period, &periodcfg)) {
248 if (fsl_pwm_is_other_pwm_enabled(fpc, pwm)) {
249 dev_err(fpc->chip.dev,
250 "Cannot change period for PWM %u, disable other PWMs first\n",
254 if (fpc->period.clk_select != periodcfg.clk_select) {
256 enum fsl_pwm_clk oldclk = fpc->period.clk_select;
257 enum fsl_pwm_clk newclk = periodcfg.clk_select;
259 ret = clk_prepare_enable(fpc->clk[newclk]);
262 clk_disable_unprepare(fpc->clk[oldclk]);
264 do_write_period = true;
267 ftm_clear_write_protection(fpc);
269 if (do_write_period) {
270 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
271 FTM_SC_CLK(periodcfg.clk_select));
272 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
274 regmap_write(fpc->regmap, FTM_MOD, periodcfg.mod_period);
276 fpc->period = periodcfg;
279 duty = fsl_pwm_calculate_duty(fpc, newstate->duty_cycle);
281 regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
282 FTM_CSC_MSB | FTM_CSC_ELSB);
283 regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
286 if (newstate->polarity == PWM_POLARITY_INVERSED)
287 reg_polarity = BIT(pwm->hwpwm);
289 regmap_update_bits(fpc->regmap, FTM_POL, BIT(pwm->hwpwm), reg_polarity);
291 ftm_set_write_protection(fpc);
296 static int fsl_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
297 const struct pwm_state *newstate)
299 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
300 struct pwm_state *oldstate = &pwm->state;
304 * oldstate to newstate : action
306 * disabled to disabled : ignore
307 * enabled to disabled : disable
308 * enabled to enabled : update settings
309 * disabled to enabled : update settings + enable
312 mutex_lock(&fpc->lock);
314 if (!newstate->enabled) {
315 if (oldstate->enabled) {
316 regmap_set_bits(fpc->regmap, FTM_OUTMASK,
318 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
319 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
325 ret = fsl_pwm_apply_config(fpc, pwm, newstate);
329 /* check if need to enable */
330 if (!oldstate->enabled) {
331 ret = clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
335 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
337 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
341 regmap_clear_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm));
345 mutex_unlock(&fpc->lock);
349 static const struct pwm_ops fsl_pwm_ops = {
350 .request = fsl_pwm_request,
351 .free = fsl_pwm_free,
352 .apply = fsl_pwm_apply,
353 .owner = THIS_MODULE,
356 static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
360 ret = clk_prepare_enable(fpc->ipg_clk);
364 regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
365 regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
366 regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
368 clk_disable_unprepare(fpc->ipg_clk);
373 static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
384 static const struct regmap_config fsl_pwm_regmap_config = {
389 .max_register = FTM_PWMLOAD,
390 .volatile_reg = fsl_pwm_volatile_reg,
391 .cache_type = REGCACHE_FLAT,
394 static int fsl_pwm_probe(struct platform_device *pdev)
396 struct fsl_pwm_chip *fpc;
400 fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
404 mutex_init(&fpc->lock);
406 fpc->soc = of_device_get_match_data(&pdev->dev);
407 fpc->chip.dev = &pdev->dev;
409 base = devm_platform_ioremap_resource(pdev, 0);
411 return PTR_ERR(base);
413 fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
414 &fsl_pwm_regmap_config);
415 if (IS_ERR(fpc->regmap)) {
416 dev_err(&pdev->dev, "regmap init failed\n");
417 return PTR_ERR(fpc->regmap);
420 fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
421 if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
422 dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
423 return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
426 fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
427 if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
428 return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
430 fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
431 if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
432 return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
434 fpc->clk[FSL_PWM_CLK_CNTEN] =
435 devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
436 if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
437 return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
440 * ipg_clk is the interface clock for the IP. If not provided, use the
441 * ftm_sys clock as the default.
443 fpc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
444 if (IS_ERR(fpc->ipg_clk))
445 fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS];
448 fpc->chip.ops = &fsl_pwm_ops;
451 ret = devm_pwmchip_add(&pdev->dev, &fpc->chip);
453 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
457 platform_set_drvdata(pdev, fpc);
459 return fsl_pwm_init(fpc);
462 #ifdef CONFIG_PM_SLEEP
463 static int fsl_pwm_suspend(struct device *dev)
465 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
468 regcache_cache_only(fpc->regmap, true);
469 regcache_mark_dirty(fpc->regmap);
471 for (i = 0; i < fpc->chip.npwm; i++) {
472 struct pwm_device *pwm = &fpc->chip.pwms[i];
474 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
477 clk_disable_unprepare(fpc->ipg_clk);
479 if (!pwm_is_enabled(pwm))
482 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
483 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
489 static int fsl_pwm_resume(struct device *dev)
491 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
494 for (i = 0; i < fpc->chip.npwm; i++) {
495 struct pwm_device *pwm = &fpc->chip.pwms[i];
497 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
500 clk_prepare_enable(fpc->ipg_clk);
502 if (!pwm_is_enabled(pwm))
505 clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
506 clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
509 /* restore all registers from cache */
510 regcache_cache_only(fpc->regmap, false);
511 regcache_sync(fpc->regmap);
517 static const struct dev_pm_ops fsl_pwm_pm_ops = {
518 SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
521 static const struct fsl_ftm_soc vf610_ftm_pwm = {
522 .has_enable_bits = false,
525 static const struct fsl_ftm_soc imx8qm_ftm_pwm = {
526 .has_enable_bits = true,
529 static const struct of_device_id fsl_pwm_dt_ids[] = {
530 { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm },
531 { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm },
534 MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
536 static struct platform_driver fsl_pwm_driver = {
538 .name = "fsl-ftm-pwm",
539 .of_match_table = fsl_pwm_dt_ids,
540 .pm = &fsl_pwm_pm_ops,
542 .probe = fsl_pwm_probe,
544 module_platform_driver(fsl_pwm_driver);
546 MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
547 MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
548 MODULE_ALIAS("platform:fsl-ftm-pwm");
549 MODULE_LICENSE("GPL");