Linux 4.19.133
[linux/fpc-iii.git] / drivers / pwm / pwm-fsl-ftm.c
blob883378d055c64e5887371b077d7c62f70d6a8920
1 /*
2 * Freescale FlexTimer Module (FTM) PWM Driver
4 * Copyright 2012-2013 Freescale Semiconductor, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm.h>
22 #include <linux/pwm.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
26 #define FTM_SC 0x00
27 #define FTM_SC_CLK_MASK_SHIFT 3
28 #define FTM_SC_CLK_MASK (3 << FTM_SC_CLK_MASK_SHIFT)
29 #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
30 #define FTM_SC_PS_MASK 0x7
32 #define FTM_CNT 0x04
33 #define FTM_MOD 0x08
35 #define FTM_CSC_BASE 0x0C
36 #define FTM_CSC_MSB BIT(5)
37 #define FTM_CSC_MSA BIT(4)
38 #define FTM_CSC_ELSB BIT(3)
39 #define FTM_CSC_ELSA BIT(2)
40 #define FTM_CSC(_channel) (FTM_CSC_BASE + ((_channel) * 8))
42 #define FTM_CV_BASE 0x10
43 #define FTM_CV(_channel) (FTM_CV_BASE + ((_channel) * 8))
45 #define FTM_CNTIN 0x4C
46 #define FTM_STATUS 0x50
48 #define FTM_MODE 0x54
49 #define FTM_MODE_FTMEN BIT(0)
50 #define FTM_MODE_INIT BIT(2)
51 #define FTM_MODE_PWMSYNC BIT(3)
53 #define FTM_SYNC 0x58
54 #define FTM_OUTINIT 0x5C
55 #define FTM_OUTMASK 0x60
56 #define FTM_COMBINE 0x64
57 #define FTM_DEADTIME 0x68
58 #define FTM_EXTTRIG 0x6C
59 #define FTM_POL 0x70
60 #define FTM_FMS 0x74
61 #define FTM_FILTER 0x78
62 #define FTM_FLTCTRL 0x7C
63 #define FTM_QDCTRL 0x80
64 #define FTM_CONF 0x84
65 #define FTM_FLTPOL 0x88
66 #define FTM_SYNCONF 0x8C
67 #define FTM_INVCTRL 0x90
68 #define FTM_SWOCTRL 0x94
69 #define FTM_PWMLOAD 0x98
71 enum fsl_pwm_clk {
72 FSL_PWM_CLK_SYS,
73 FSL_PWM_CLK_FIX,
74 FSL_PWM_CLK_EXT,
75 FSL_PWM_CLK_CNTEN,
76 FSL_PWM_CLK_MAX
79 struct fsl_ftm_soc {
80 bool has_enable_bits;
83 struct fsl_pwm_chip {
84 struct pwm_chip chip;
86 struct mutex lock;
88 unsigned int cnt_select;
89 unsigned int clk_ps;
91 struct regmap *regmap;
93 int period_ns;
95 struct clk *ipg_clk;
96 struct clk *clk[FSL_PWM_CLK_MAX];
98 const struct fsl_ftm_soc *soc;
101 static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
103 return container_of(chip, struct fsl_pwm_chip, chip);
106 static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
108 int ret;
109 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
111 ret = clk_prepare_enable(fpc->ipg_clk);
112 if (!ret && fpc->soc->has_enable_bits) {
113 mutex_lock(&fpc->lock);
114 regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16),
115 BIT(pwm->hwpwm + 16));
116 mutex_unlock(&fpc->lock);
119 return ret;
122 static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
124 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
126 if (fpc->soc->has_enable_bits) {
127 mutex_lock(&fpc->lock);
128 regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16),
130 mutex_unlock(&fpc->lock);
133 clk_disable_unprepare(fpc->ipg_clk);
136 static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc,
137 enum fsl_pwm_clk index)
139 unsigned long sys_rate, cnt_rate;
140 unsigned long long ratio;
142 sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]);
143 if (!sys_rate)
144 return -EINVAL;
146 cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
147 if (!cnt_rate)
148 return -EINVAL;
150 switch (index) {
151 case FSL_PWM_CLK_SYS:
152 fpc->clk_ps = 1;
153 break;
154 case FSL_PWM_CLK_FIX:
155 ratio = 2 * cnt_rate - 1;
156 do_div(ratio, sys_rate);
157 fpc->clk_ps = ratio;
158 break;
159 case FSL_PWM_CLK_EXT:
160 ratio = 4 * cnt_rate - 1;
161 do_div(ratio, sys_rate);
162 fpc->clk_ps = ratio;
163 break;
164 default:
165 return -EINVAL;
168 return 0;
171 static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc,
172 unsigned long period_ns)
174 unsigned long long c, c0;
176 c = clk_get_rate(fpc->clk[fpc->cnt_select]);
177 c = c * period_ns;
178 do_div(c, 1000000000UL);
180 do {
181 c0 = c;
182 do_div(c0, (1 << fpc->clk_ps));
183 if (c0 <= 0xFFFF)
184 return (unsigned long)c0;
185 } while (++fpc->clk_ps < 8);
187 return 0;
190 static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc,
191 unsigned long period_ns,
192 enum fsl_pwm_clk index)
194 int ret;
196 ret = fsl_pwm_calculate_default_ps(fpc, index);
197 if (ret) {
198 dev_err(fpc->chip.dev,
199 "failed to calculate default prescaler: %d\n",
200 ret);
201 return 0;
204 return fsl_pwm_calculate_cycles(fpc, period_ns);
207 static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
208 unsigned long period_ns)
210 enum fsl_pwm_clk m0, m1;
211 unsigned long fix_rate, ext_rate, cycles;
213 cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns,
214 FSL_PWM_CLK_SYS);
215 if (cycles) {
216 fpc->cnt_select = FSL_PWM_CLK_SYS;
217 return cycles;
220 fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
221 ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
223 if (fix_rate > ext_rate) {
224 m0 = FSL_PWM_CLK_FIX;
225 m1 = FSL_PWM_CLK_EXT;
226 } else {
227 m0 = FSL_PWM_CLK_EXT;
228 m1 = FSL_PWM_CLK_FIX;
231 cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
232 if (cycles) {
233 fpc->cnt_select = m0;
234 return cycles;
237 fpc->cnt_select = m1;
239 return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1);
242 static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
243 unsigned long period_ns,
244 unsigned long duty_ns)
246 unsigned long long duty;
247 u32 val;
249 regmap_read(fpc->regmap, FTM_MOD, &val);
250 duty = (unsigned long long)duty_ns * (val + 1);
251 do_div(duty, period_ns);
253 return (unsigned long)duty;
256 static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
257 int duty_ns, int period_ns)
259 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
260 u32 period, duty;
262 mutex_lock(&fpc->lock);
265 * The Freescale FTM controller supports only a single period for
266 * all PWM channels, therefore incompatible changes need to be
267 * refused.
269 if (fpc->period_ns && fpc->period_ns != period_ns) {
270 dev_err(fpc->chip.dev,
271 "conflicting period requested for PWM %u\n",
272 pwm->hwpwm);
273 mutex_unlock(&fpc->lock);
274 return -EBUSY;
277 if (!fpc->period_ns && duty_ns) {
278 period = fsl_pwm_calculate_period(fpc, period_ns);
279 if (!period) {
280 dev_err(fpc->chip.dev, "failed to calculate period\n");
281 mutex_unlock(&fpc->lock);
282 return -EINVAL;
285 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
286 fpc->clk_ps);
287 regmap_write(fpc->regmap, FTM_MOD, period - 1);
289 fpc->period_ns = period_ns;
292 mutex_unlock(&fpc->lock);
294 duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns);
296 regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
297 FTM_CSC_MSB | FTM_CSC_ELSB);
298 regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
300 return 0;
303 static int fsl_pwm_set_polarity(struct pwm_chip *chip,
304 struct pwm_device *pwm,
305 enum pwm_polarity polarity)
307 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
308 u32 val;
310 regmap_read(fpc->regmap, FTM_POL, &val);
312 if (polarity == PWM_POLARITY_INVERSED)
313 val |= BIT(pwm->hwpwm);
314 else
315 val &= ~BIT(pwm->hwpwm);
317 regmap_write(fpc->regmap, FTM_POL, val);
319 return 0;
322 static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
324 int ret;
326 /* select counter clock source */
327 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
328 FTM_SC_CLK(fpc->cnt_select));
330 ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]);
331 if (ret)
332 return ret;
334 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
335 if (ret) {
336 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
337 return ret;
340 return 0;
343 static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
345 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
346 int ret;
348 mutex_lock(&fpc->lock);
349 regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 0);
351 ret = fsl_counter_clock_enable(fpc);
352 mutex_unlock(&fpc->lock);
354 return ret;
357 static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
359 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
360 u32 val;
362 mutex_lock(&fpc->lock);
363 regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
364 BIT(pwm->hwpwm));
366 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
367 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
369 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
370 if ((val & 0xFF) == 0xFF)
371 fpc->period_ns = 0;
373 mutex_unlock(&fpc->lock);
376 static const struct pwm_ops fsl_pwm_ops = {
377 .request = fsl_pwm_request,
378 .free = fsl_pwm_free,
379 .config = fsl_pwm_config,
380 .set_polarity = fsl_pwm_set_polarity,
381 .enable = fsl_pwm_enable,
382 .disable = fsl_pwm_disable,
383 .owner = THIS_MODULE,
386 static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
388 int ret;
390 ret = clk_prepare_enable(fpc->ipg_clk);
391 if (ret)
392 return ret;
394 regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
395 regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
396 regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
398 clk_disable_unprepare(fpc->ipg_clk);
400 return 0;
403 static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
405 switch (reg) {
406 case FTM_CNT:
407 return true;
409 return false;
412 static const struct regmap_config fsl_pwm_regmap_config = {
413 .reg_bits = 32,
414 .reg_stride = 4,
415 .val_bits = 32,
417 .max_register = FTM_PWMLOAD,
418 .volatile_reg = fsl_pwm_volatile_reg,
419 .cache_type = REGCACHE_FLAT,
422 static int fsl_pwm_probe(struct platform_device *pdev)
424 struct fsl_pwm_chip *fpc;
425 struct resource *res;
426 void __iomem *base;
427 int ret;
429 fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
430 if (!fpc)
431 return -ENOMEM;
433 mutex_init(&fpc->lock);
435 fpc->soc = of_device_get_match_data(&pdev->dev);
436 fpc->chip.dev = &pdev->dev;
438 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
439 base = devm_ioremap_resource(&pdev->dev, res);
440 if (IS_ERR(base))
441 return PTR_ERR(base);
443 fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
444 &fsl_pwm_regmap_config);
445 if (IS_ERR(fpc->regmap)) {
446 dev_err(&pdev->dev, "regmap init failed\n");
447 return PTR_ERR(fpc->regmap);
450 fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
451 if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
452 dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
453 return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
456 fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
457 if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
458 return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
460 fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
461 if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
462 return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
464 fpc->clk[FSL_PWM_CLK_CNTEN] =
465 devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
466 if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
467 return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
470 * ipg_clk is the interface clock for the IP. If not provided, use the
471 * ftm_sys clock as the default.
473 fpc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
474 if (IS_ERR(fpc->ipg_clk))
475 fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS];
478 fpc->chip.ops = &fsl_pwm_ops;
479 fpc->chip.of_xlate = of_pwm_xlate_with_flags;
480 fpc->chip.of_pwm_n_cells = 3;
481 fpc->chip.base = -1;
482 fpc->chip.npwm = 8;
484 ret = pwmchip_add(&fpc->chip);
485 if (ret < 0) {
486 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
487 return ret;
490 platform_set_drvdata(pdev, fpc);
492 return fsl_pwm_init(fpc);
495 static int fsl_pwm_remove(struct platform_device *pdev)
497 struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev);
499 return pwmchip_remove(&fpc->chip);
502 #ifdef CONFIG_PM_SLEEP
503 static int fsl_pwm_suspend(struct device *dev)
505 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
506 int i;
508 regcache_cache_only(fpc->regmap, true);
509 regcache_mark_dirty(fpc->regmap);
511 for (i = 0; i < fpc->chip.npwm; i++) {
512 struct pwm_device *pwm = &fpc->chip.pwms[i];
514 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
515 continue;
517 clk_disable_unprepare(fpc->ipg_clk);
519 if (!pwm_is_enabled(pwm))
520 continue;
522 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
523 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
526 return 0;
529 static int fsl_pwm_resume(struct device *dev)
531 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
532 int i;
534 for (i = 0; i < fpc->chip.npwm; i++) {
535 struct pwm_device *pwm = &fpc->chip.pwms[i];
537 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
538 continue;
540 clk_prepare_enable(fpc->ipg_clk);
542 if (!pwm_is_enabled(pwm))
543 continue;
545 clk_prepare_enable(fpc->clk[fpc->cnt_select]);
546 clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
549 /* restore all registers from cache */
550 regcache_cache_only(fpc->regmap, false);
551 regcache_sync(fpc->regmap);
553 return 0;
555 #endif
557 static const struct dev_pm_ops fsl_pwm_pm_ops = {
558 SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
561 static const struct fsl_ftm_soc vf610_ftm_pwm = {
562 .has_enable_bits = false,
565 static const struct fsl_ftm_soc imx8qm_ftm_pwm = {
566 .has_enable_bits = true,
569 static const struct of_device_id fsl_pwm_dt_ids[] = {
570 { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm },
571 { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm },
572 { /* sentinel */ }
574 MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
576 static struct platform_driver fsl_pwm_driver = {
577 .driver = {
578 .name = "fsl-ftm-pwm",
579 .of_match_table = fsl_pwm_dt_ids,
580 .pm = &fsl_pwm_pm_ops,
582 .probe = fsl_pwm_probe,
583 .remove = fsl_pwm_remove,
585 module_platform_driver(fsl_pwm_driver);
587 MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
588 MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
589 MODULE_ALIAS("platform:fsl-ftm-pwm");
590 MODULE_LICENSE("GPL");