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>
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>
22 #include <linux/pwm.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
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
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
49 #define FTM_MODE_FTMEN BIT(0)
50 #define FTM_MODE_INIT BIT(2)
51 #define FTM_MODE_PWMSYNC BIT(3)
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
61 #define FTM_FILTER 0x78
62 #define FTM_FLTCTRL 0x7C
63 #define FTM_QDCTRL 0x80
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
88 unsigned int cnt_select
;
91 struct regmap
*regmap
;
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
)
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
);
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
]);
146 cnt_rate
= clk_get_rate(fpc
->clk
[fpc
->cnt_select
]);
151 case FSL_PWM_CLK_SYS
:
154 case FSL_PWM_CLK_FIX
:
155 ratio
= 2 * cnt_rate
- 1;
156 do_div(ratio
, sys_rate
);
159 case FSL_PWM_CLK_EXT
:
160 ratio
= 4 * cnt_rate
- 1;
161 do_div(ratio
, sys_rate
);
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
]);
178 do_div(c
, 1000000000UL);
182 do_div(c0
, (1 << fpc
->clk_ps
));
184 return (unsigned long)c0
;
185 } while (++fpc
->clk_ps
< 8);
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
)
196 ret
= fsl_pwm_calculate_default_ps(fpc
, index
);
198 dev_err(fpc
->chip
.dev
,
199 "failed to calculate default prescaler: %d\n",
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
,
216 fpc
->cnt_select
= FSL_PWM_CLK_SYS
;
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
;
227 m0
= FSL_PWM_CLK_EXT
;
228 m1
= FSL_PWM_CLK_FIX
;
231 cycles
= fsl_pwm_calculate_period_cycles(fpc
, period_ns
, m0
);
233 fpc
->cnt_select
= m0
;
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
;
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
);
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
269 if (fpc
->period_ns
&& fpc
->period_ns
!= period_ns
) {
270 dev_err(fpc
->chip
.dev
,
271 "conflicting period requested for PWM %u\n",
273 mutex_unlock(&fpc
->lock
);
277 if (!fpc
->period_ns
&& duty_ns
) {
278 period
= fsl_pwm_calculate_period(fpc
, period_ns
);
280 dev_err(fpc
->chip
.dev
, "failed to calculate period\n");
281 mutex_unlock(&fpc
->lock
);
285 regmap_update_bits(fpc
->regmap
, FTM_SC
, FTM_SC_PS_MASK
,
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
);
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
);
310 regmap_read(fpc
->regmap
, FTM_POL
, &val
);
312 if (polarity
== PWM_POLARITY_INVERSED
)
313 val
|= BIT(pwm
->hwpwm
);
315 val
&= ~BIT(pwm
->hwpwm
);
317 regmap_write(fpc
->regmap
, FTM_POL
, val
);
322 static int fsl_counter_clock_enable(struct fsl_pwm_chip
*fpc
)
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
]);
334 ret
= clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
336 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
343 static int fsl_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
345 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
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
);
357 static void fsl_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
359 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
362 mutex_lock(&fpc
->lock
);
363 regmap_update_bits(fpc
->regmap
, FTM_OUTMASK
, 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)
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
)
390 ret
= clk_prepare_enable(fpc
->ipg_clk
);
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
);
403 static bool fsl_pwm_volatile_reg(struct device
*dev
, unsigned int reg
)
412 static const struct regmap_config fsl_pwm_regmap_config
= {
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
;
429 fpc
= devm_kzalloc(&pdev
->dev
, sizeof(*fpc
), GFP_KERNEL
);
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
);
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;
484 ret
= pwmchip_add(&fpc
->chip
);
486 dev_err(&pdev
->dev
, "failed to add PWM chip: %d\n", 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
);
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
))
517 clk_disable_unprepare(fpc
->ipg_clk
);
519 if (!pwm_is_enabled(pwm
))
522 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
523 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
529 static int fsl_pwm_resume(struct device
*dev
)
531 struct fsl_pwm_chip
*fpc
= dev_get_drvdata(dev
);
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
))
540 clk_prepare_enable(fpc
->ipg_clk
);
542 if (!pwm_is_enabled(pwm
))
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
);
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
},
574 MODULE_DEVICE_TABLE(of
, fsl_pwm_dt_ids
);
576 static struct platform_driver fsl_pwm_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");