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>
25 #include <linux/fsl/ftm.h>
27 #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
46 unsigned int cnt_select
;
49 struct regmap
*regmap
;
54 struct clk
*clk
[FSL_PWM_CLK_MAX
];
56 const struct fsl_ftm_soc
*soc
;
59 static inline struct fsl_pwm_chip
*to_fsl_chip(struct pwm_chip
*chip
)
61 return container_of(chip
, struct fsl_pwm_chip
, chip
);
64 static int fsl_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
67 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
69 ret
= clk_prepare_enable(fpc
->ipg_clk
);
70 if (!ret
&& fpc
->soc
->has_enable_bits
) {
71 mutex_lock(&fpc
->lock
);
72 regmap_update_bits(fpc
->regmap
, FTM_SC
, BIT(pwm
->hwpwm
+ 16),
73 BIT(pwm
->hwpwm
+ 16));
74 mutex_unlock(&fpc
->lock
);
80 static void fsl_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
82 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
84 if (fpc
->soc
->has_enable_bits
) {
85 mutex_lock(&fpc
->lock
);
86 regmap_update_bits(fpc
->regmap
, FTM_SC
, BIT(pwm
->hwpwm
+ 16),
88 mutex_unlock(&fpc
->lock
);
91 clk_disable_unprepare(fpc
->ipg_clk
);
94 static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip
*fpc
,
95 enum fsl_pwm_clk index
)
97 unsigned long sys_rate
, cnt_rate
;
98 unsigned long long ratio
;
100 sys_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_SYS
]);
104 cnt_rate
= clk_get_rate(fpc
->clk
[fpc
->cnt_select
]);
109 case FSL_PWM_CLK_SYS
:
112 case FSL_PWM_CLK_FIX
:
113 ratio
= 2 * cnt_rate
- 1;
114 do_div(ratio
, sys_rate
);
117 case FSL_PWM_CLK_EXT
:
118 ratio
= 4 * cnt_rate
- 1;
119 do_div(ratio
, sys_rate
);
129 static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip
*fpc
,
130 unsigned long period_ns
)
132 unsigned long long c
, c0
;
134 c
= clk_get_rate(fpc
->clk
[fpc
->cnt_select
]);
136 do_div(c
, 1000000000UL);
140 do_div(c0
, (1 << fpc
->clk_ps
));
142 return (unsigned long)c0
;
143 } while (++fpc
->clk_ps
< 8);
148 static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip
*fpc
,
149 unsigned long period_ns
,
150 enum fsl_pwm_clk index
)
154 ret
= fsl_pwm_calculate_default_ps(fpc
, index
);
156 dev_err(fpc
->chip
.dev
,
157 "failed to calculate default prescaler: %d\n",
162 return fsl_pwm_calculate_cycles(fpc
, period_ns
);
165 static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip
*fpc
,
166 unsigned long period_ns
)
168 enum fsl_pwm_clk m0
, m1
;
169 unsigned long fix_rate
, ext_rate
, cycles
;
171 cycles
= fsl_pwm_calculate_period_cycles(fpc
, period_ns
,
174 fpc
->cnt_select
= FSL_PWM_CLK_SYS
;
178 fix_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_FIX
]);
179 ext_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_EXT
]);
181 if (fix_rate
> ext_rate
) {
182 m0
= FSL_PWM_CLK_FIX
;
183 m1
= FSL_PWM_CLK_EXT
;
185 m0
= FSL_PWM_CLK_EXT
;
186 m1
= FSL_PWM_CLK_FIX
;
189 cycles
= fsl_pwm_calculate_period_cycles(fpc
, period_ns
, m0
);
191 fpc
->cnt_select
= m0
;
195 fpc
->cnt_select
= m1
;
197 return fsl_pwm_calculate_period_cycles(fpc
, period_ns
, m1
);
200 static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip
*fpc
,
201 unsigned long period_ns
,
202 unsigned long duty_ns
)
204 unsigned long long duty
;
207 regmap_read(fpc
->regmap
, FTM_MOD
, &val
);
208 duty
= (unsigned long long)duty_ns
* (val
+ 1);
209 do_div(duty
, period_ns
);
211 return (unsigned long)duty
;
214 static int fsl_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
215 int duty_ns
, int period_ns
)
217 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
220 mutex_lock(&fpc
->lock
);
223 * The Freescale FTM controller supports only a single period for
224 * all PWM channels, therefore incompatible changes need to be
227 if (fpc
->period_ns
&& fpc
->period_ns
!= period_ns
) {
228 dev_err(fpc
->chip
.dev
,
229 "conflicting period requested for PWM %u\n",
231 mutex_unlock(&fpc
->lock
);
235 if (!fpc
->period_ns
&& duty_ns
) {
236 period
= fsl_pwm_calculate_period(fpc
, period_ns
);
238 dev_err(fpc
->chip
.dev
, "failed to calculate period\n");
239 mutex_unlock(&fpc
->lock
);
243 regmap_update_bits(fpc
->regmap
, FTM_SC
, FTM_SC_PS_MASK
,
245 regmap_write(fpc
->regmap
, FTM_MOD
, period
- 1);
247 fpc
->period_ns
= period_ns
;
250 mutex_unlock(&fpc
->lock
);
252 duty
= fsl_pwm_calculate_duty(fpc
, period_ns
, duty_ns
);
254 regmap_write(fpc
->regmap
, FTM_CSC(pwm
->hwpwm
),
255 FTM_CSC_MSB
| FTM_CSC_ELSB
);
256 regmap_write(fpc
->regmap
, FTM_CV(pwm
->hwpwm
), duty
);
261 static int fsl_pwm_set_polarity(struct pwm_chip
*chip
,
262 struct pwm_device
*pwm
,
263 enum pwm_polarity polarity
)
265 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
268 regmap_read(fpc
->regmap
, FTM_POL
, &val
);
270 if (polarity
== PWM_POLARITY_INVERSED
)
271 val
|= BIT(pwm
->hwpwm
);
273 val
&= ~BIT(pwm
->hwpwm
);
275 regmap_write(fpc
->regmap
, FTM_POL
, val
);
280 static int fsl_counter_clock_enable(struct fsl_pwm_chip
*fpc
)
284 /* select counter clock source */
285 regmap_update_bits(fpc
->regmap
, FTM_SC
, FTM_SC_CLK_MASK
,
286 FTM_SC_CLK(fpc
->cnt_select
));
288 ret
= clk_prepare_enable(fpc
->clk
[fpc
->cnt_select
]);
292 ret
= clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
294 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
301 static int fsl_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
303 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
306 mutex_lock(&fpc
->lock
);
307 regmap_update_bits(fpc
->regmap
, FTM_OUTMASK
, BIT(pwm
->hwpwm
), 0);
309 ret
= fsl_counter_clock_enable(fpc
);
310 mutex_unlock(&fpc
->lock
);
315 static void fsl_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
317 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
320 mutex_lock(&fpc
->lock
);
321 regmap_update_bits(fpc
->regmap
, FTM_OUTMASK
, BIT(pwm
->hwpwm
),
324 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
325 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
327 regmap_read(fpc
->regmap
, FTM_OUTMASK
, &val
);
328 if ((val
& 0xFF) == 0xFF)
331 mutex_unlock(&fpc
->lock
);
334 static const struct pwm_ops fsl_pwm_ops
= {
335 .request
= fsl_pwm_request
,
336 .free
= fsl_pwm_free
,
337 .config
= fsl_pwm_config
,
338 .set_polarity
= fsl_pwm_set_polarity
,
339 .enable
= fsl_pwm_enable
,
340 .disable
= fsl_pwm_disable
,
341 .owner
= THIS_MODULE
,
344 static int fsl_pwm_init(struct fsl_pwm_chip
*fpc
)
348 ret
= clk_prepare_enable(fpc
->ipg_clk
);
352 regmap_write(fpc
->regmap
, FTM_CNTIN
, 0x00);
353 regmap_write(fpc
->regmap
, FTM_OUTINIT
, 0x00);
354 regmap_write(fpc
->regmap
, FTM_OUTMASK
, 0xFF);
356 clk_disable_unprepare(fpc
->ipg_clk
);
361 static bool fsl_pwm_volatile_reg(struct device
*dev
, unsigned int reg
)
370 static const struct regmap_config fsl_pwm_regmap_config
= {
375 .max_register
= FTM_PWMLOAD
,
376 .volatile_reg
= fsl_pwm_volatile_reg
,
377 .cache_type
= REGCACHE_FLAT
,
380 static int fsl_pwm_probe(struct platform_device
*pdev
)
382 struct fsl_pwm_chip
*fpc
;
383 struct resource
*res
;
387 fpc
= devm_kzalloc(&pdev
->dev
, sizeof(*fpc
), GFP_KERNEL
);
391 mutex_init(&fpc
->lock
);
393 fpc
->soc
= of_device_get_match_data(&pdev
->dev
);
394 fpc
->chip
.dev
= &pdev
->dev
;
396 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
397 base
= devm_ioremap_resource(&pdev
->dev
, res
);
399 return PTR_ERR(base
);
401 fpc
->regmap
= devm_regmap_init_mmio_clk(&pdev
->dev
, "ftm_sys", base
,
402 &fsl_pwm_regmap_config
);
403 if (IS_ERR(fpc
->regmap
)) {
404 dev_err(&pdev
->dev
, "regmap init failed\n");
405 return PTR_ERR(fpc
->regmap
);
408 fpc
->clk
[FSL_PWM_CLK_SYS
] = devm_clk_get(&pdev
->dev
, "ftm_sys");
409 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_SYS
])) {
410 dev_err(&pdev
->dev
, "failed to get \"ftm_sys\" clock\n");
411 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_SYS
]);
414 fpc
->clk
[FSL_PWM_CLK_FIX
] = devm_clk_get(fpc
->chip
.dev
, "ftm_fix");
415 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_FIX
]))
416 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_FIX
]);
418 fpc
->clk
[FSL_PWM_CLK_EXT
] = devm_clk_get(fpc
->chip
.dev
, "ftm_ext");
419 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_EXT
]))
420 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_EXT
]);
422 fpc
->clk
[FSL_PWM_CLK_CNTEN
] =
423 devm_clk_get(fpc
->chip
.dev
, "ftm_cnt_clk_en");
424 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_CNTEN
]))
425 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
428 * ipg_clk is the interface clock for the IP. If not provided, use the
429 * ftm_sys clock as the default.
431 fpc
->ipg_clk
= devm_clk_get(&pdev
->dev
, "ipg");
432 if (IS_ERR(fpc
->ipg_clk
))
433 fpc
->ipg_clk
= fpc
->clk
[FSL_PWM_CLK_SYS
];
436 fpc
->chip
.ops
= &fsl_pwm_ops
;
437 fpc
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
438 fpc
->chip
.of_pwm_n_cells
= 3;
442 ret
= pwmchip_add(&fpc
->chip
);
444 dev_err(&pdev
->dev
, "failed to add PWM chip: %d\n", ret
);
448 platform_set_drvdata(pdev
, fpc
);
450 return fsl_pwm_init(fpc
);
453 static int fsl_pwm_remove(struct platform_device
*pdev
)
455 struct fsl_pwm_chip
*fpc
= platform_get_drvdata(pdev
);
457 return pwmchip_remove(&fpc
->chip
);
460 #ifdef CONFIG_PM_SLEEP
461 static int fsl_pwm_suspend(struct device
*dev
)
463 struct fsl_pwm_chip
*fpc
= dev_get_drvdata(dev
);
466 regcache_cache_only(fpc
->regmap
, true);
467 regcache_mark_dirty(fpc
->regmap
);
469 for (i
= 0; i
< fpc
->chip
.npwm
; i
++) {
470 struct pwm_device
*pwm
= &fpc
->chip
.pwms
[i
];
472 if (!test_bit(PWMF_REQUESTED
, &pwm
->flags
))
475 clk_disable_unprepare(fpc
->ipg_clk
);
477 if (!pwm_is_enabled(pwm
))
480 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
481 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
487 static int fsl_pwm_resume(struct device
*dev
)
489 struct fsl_pwm_chip
*fpc
= dev_get_drvdata(dev
);
492 for (i
= 0; i
< fpc
->chip
.npwm
; i
++) {
493 struct pwm_device
*pwm
= &fpc
->chip
.pwms
[i
];
495 if (!test_bit(PWMF_REQUESTED
, &pwm
->flags
))
498 clk_prepare_enable(fpc
->ipg_clk
);
500 if (!pwm_is_enabled(pwm
))
503 clk_prepare_enable(fpc
->clk
[fpc
->cnt_select
]);
504 clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
507 /* restore all registers from cache */
508 regcache_cache_only(fpc
->regmap
, false);
509 regcache_sync(fpc
->regmap
);
515 static const struct dev_pm_ops fsl_pwm_pm_ops
= {
516 SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend
, fsl_pwm_resume
)
519 static const struct fsl_ftm_soc vf610_ftm_pwm
= {
520 .has_enable_bits
= false,
523 static const struct fsl_ftm_soc imx8qm_ftm_pwm
= {
524 .has_enable_bits
= true,
527 static const struct of_device_id fsl_pwm_dt_ids
[] = {
528 { .compatible
= "fsl,vf610-ftm-pwm", .data
= &vf610_ftm_pwm
},
529 { .compatible
= "fsl,imx8qm-ftm-pwm", .data
= &imx8qm_ftm_pwm
},
532 MODULE_DEVICE_TABLE(of
, fsl_pwm_dt_ids
);
534 static struct platform_driver fsl_pwm_driver
= {
536 .name
= "fsl-ftm-pwm",
537 .of_match_table
= fsl_pwm_dt_ids
,
538 .pm
= &fsl_pwm_pm_ops
,
540 .probe
= fsl_pwm_probe
,
541 .remove
= fsl_pwm_remove
,
543 module_platform_driver(fsl_pwm_driver
);
545 MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
546 MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
547 MODULE_ALIAS("platform:fsl-ftm-pwm");
548 MODULE_LICENSE("GPL");