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/platform_device.h>
20 #include <linux/pwm.h>
21 #include <linux/slab.h>
24 #define FTM_SC_CLK_MASK 0x3
25 #define FTM_SC_CLK_SHIFT 3
26 #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_SHIFT)
27 #define FTM_SC_PS_MASK 0x7
28 #define FTM_SC_PS_SHIFT 0
33 #define FTM_CSC_BASE 0x0C
34 #define FTM_CSC_MSB BIT(5)
35 #define FTM_CSC_MSA BIT(4)
36 #define FTM_CSC_ELSB BIT(3)
37 #define FTM_CSC_ELSA BIT(2)
38 #define FTM_CSC(_channel) (FTM_CSC_BASE + ((_channel) * 8))
40 #define FTM_CV_BASE 0x10
41 #define FTM_CV(_channel) (FTM_CV_BASE + ((_channel) * 8))
43 #define FTM_CNTIN 0x4C
44 #define FTM_STATUS 0x50
47 #define FTM_MODE_FTMEN BIT(0)
48 #define FTM_MODE_INIT BIT(2)
49 #define FTM_MODE_PWMSYNC BIT(3)
52 #define FTM_OUTINIT 0x5C
53 #define FTM_OUTMASK 0x60
54 #define FTM_COMBINE 0x64
55 #define FTM_DEADTIME 0x68
56 #define FTM_EXTTRIG 0x6C
59 #define FTM_FILTER 0x78
60 #define FTM_FLTCTRL 0x7C
61 #define FTM_QDCTRL 0x80
63 #define FTM_FLTPOL 0x88
64 #define FTM_SYNCONF 0x8C
65 #define FTM_INVCTRL 0x90
66 #define FTM_SWOCTRL 0x94
67 #define FTM_PWMLOAD 0x98
82 unsigned int use_count
;
83 unsigned int cnt_select
;
90 struct clk
*clk
[FSL_PWM_CLK_MAX
];
93 static inline struct fsl_pwm_chip
*to_fsl_chip(struct pwm_chip
*chip
)
95 return container_of(chip
, struct fsl_pwm_chip
, chip
);
98 static int fsl_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
100 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
102 return clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_SYS
]);
105 static void fsl_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
107 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
109 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_SYS
]);
112 static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip
*fpc
,
113 enum fsl_pwm_clk index
)
115 unsigned long sys_rate
, cnt_rate
;
116 unsigned long long ratio
;
118 sys_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_SYS
]);
122 cnt_rate
= clk_get_rate(fpc
->clk
[fpc
->cnt_select
]);
127 case FSL_PWM_CLK_SYS
:
130 case FSL_PWM_CLK_FIX
:
131 ratio
= 2 * cnt_rate
- 1;
132 do_div(ratio
, sys_rate
);
135 case FSL_PWM_CLK_EXT
:
136 ratio
= 4 * cnt_rate
- 1;
137 do_div(ratio
, sys_rate
);
147 static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip
*fpc
,
148 unsigned long period_ns
)
150 unsigned long long c
, c0
;
152 c
= clk_get_rate(fpc
->clk
[fpc
->cnt_select
]);
154 do_div(c
, 1000000000UL);
158 do_div(c0
, (1 << fpc
->clk_ps
));
160 return (unsigned long)c0
;
161 } while (++fpc
->clk_ps
< 8);
166 static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip
*fpc
,
167 unsigned long period_ns
,
168 enum fsl_pwm_clk index
)
172 ret
= fsl_pwm_calculate_default_ps(fpc
, index
);
174 dev_err(fpc
->chip
.dev
,
175 "failed to calculate default prescaler: %d\n",
180 return fsl_pwm_calculate_cycles(fpc
, period_ns
);
183 static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip
*fpc
,
184 unsigned long period_ns
)
186 enum fsl_pwm_clk m0
, m1
;
187 unsigned long fix_rate
, ext_rate
, cycles
;
189 cycles
= fsl_pwm_calculate_period_cycles(fpc
, period_ns
,
192 fpc
->cnt_select
= FSL_PWM_CLK_SYS
;
196 fix_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_FIX
]);
197 ext_rate
= clk_get_rate(fpc
->clk
[FSL_PWM_CLK_EXT
]);
199 if (fix_rate
> ext_rate
) {
200 m0
= FSL_PWM_CLK_FIX
;
201 m1
= FSL_PWM_CLK_EXT
;
203 m0
= FSL_PWM_CLK_EXT
;
204 m1
= FSL_PWM_CLK_FIX
;
207 cycles
= fsl_pwm_calculate_period_cycles(fpc
, period_ns
, m0
);
209 fpc
->cnt_select
= m0
;
213 fpc
->cnt_select
= m1
;
215 return fsl_pwm_calculate_period_cycles(fpc
, period_ns
, m1
);
218 static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip
*fpc
,
219 unsigned long period_ns
,
220 unsigned long duty_ns
)
222 unsigned long long val
, duty
;
224 val
= readl(fpc
->base
+ FTM_MOD
);
225 duty
= duty_ns
* (val
+ 1);
226 do_div(duty
, period_ns
);
228 return (unsigned long)duty
;
231 static int fsl_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
232 int duty_ns
, int period_ns
)
234 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
235 u32 val
, period
, duty
;
237 mutex_lock(&fpc
->lock
);
240 * The Freescale FTM controller supports only a single period for
241 * all PWM channels, therefore incompatible changes need to be
244 if (fpc
->period_ns
&& fpc
->period_ns
!= period_ns
) {
245 dev_err(fpc
->chip
.dev
,
246 "conflicting period requested for PWM %u\n",
248 mutex_unlock(&fpc
->lock
);
252 if (!fpc
->period_ns
&& duty_ns
) {
253 period
= fsl_pwm_calculate_period(fpc
, period_ns
);
255 dev_err(fpc
->chip
.dev
, "failed to calculate period\n");
256 mutex_unlock(&fpc
->lock
);
260 val
= readl(fpc
->base
+ FTM_SC
);
261 val
&= ~(FTM_SC_PS_MASK
<< FTM_SC_PS_SHIFT
);
263 writel(val
, fpc
->base
+ FTM_SC
);
264 writel(period
- 1, fpc
->base
+ FTM_MOD
);
266 fpc
->period_ns
= period_ns
;
269 mutex_unlock(&fpc
->lock
);
271 duty
= fsl_pwm_calculate_duty(fpc
, period_ns
, duty_ns
);
273 writel(FTM_CSC_MSB
| FTM_CSC_ELSB
, fpc
->base
+ FTM_CSC(pwm
->hwpwm
));
274 writel(duty
, fpc
->base
+ FTM_CV(pwm
->hwpwm
));
279 static int fsl_pwm_set_polarity(struct pwm_chip
*chip
,
280 struct pwm_device
*pwm
,
281 enum pwm_polarity polarity
)
283 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
286 val
= readl(fpc
->base
+ FTM_POL
);
288 if (polarity
== PWM_POLARITY_INVERSED
)
289 val
|= BIT(pwm
->hwpwm
);
291 val
&= ~BIT(pwm
->hwpwm
);
293 writel(val
, fpc
->base
+ FTM_POL
);
298 static int fsl_counter_clock_enable(struct fsl_pwm_chip
*fpc
)
303 if (fpc
->use_count
!= 0)
306 /* select counter clock source */
307 val
= readl(fpc
->base
+ FTM_SC
);
308 val
&= ~(FTM_SC_CLK_MASK
<< FTM_SC_CLK_SHIFT
);
309 val
|= FTM_SC_CLK(fpc
->cnt_select
);
310 writel(val
, fpc
->base
+ FTM_SC
);
312 ret
= clk_prepare_enable(fpc
->clk
[fpc
->cnt_select
]);
316 ret
= clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
318 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
327 static int fsl_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
329 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
333 mutex_lock(&fpc
->lock
);
334 val
= readl(fpc
->base
+ FTM_OUTMASK
);
335 val
&= ~BIT(pwm
->hwpwm
);
336 writel(val
, fpc
->base
+ FTM_OUTMASK
);
338 ret
= fsl_counter_clock_enable(fpc
);
339 mutex_unlock(&fpc
->lock
);
344 static void fsl_counter_clock_disable(struct fsl_pwm_chip
*fpc
)
349 * already disabled, do nothing
351 if (fpc
->use_count
== 0)
354 /* there are still users, so can't disable yet */
355 if (--fpc
->use_count
> 0)
358 /* no users left, disable PWM counter clock */
359 val
= readl(fpc
->base
+ FTM_SC
);
360 val
&= ~(FTM_SC_CLK_MASK
<< FTM_SC_CLK_SHIFT
);
361 writel(val
, fpc
->base
+ FTM_SC
);
363 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
364 clk_disable_unprepare(fpc
->clk
[fpc
->cnt_select
]);
367 static void fsl_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
369 struct fsl_pwm_chip
*fpc
= to_fsl_chip(chip
);
372 mutex_lock(&fpc
->lock
);
373 val
= readl(fpc
->base
+ FTM_OUTMASK
);
374 val
|= BIT(pwm
->hwpwm
);
375 writel(val
, fpc
->base
+ FTM_OUTMASK
);
377 fsl_counter_clock_disable(fpc
);
379 val
= readl(fpc
->base
+ FTM_OUTMASK
);
381 if ((val
& 0xFF) == 0xFF)
384 mutex_unlock(&fpc
->lock
);
387 static const struct pwm_ops fsl_pwm_ops
= {
388 .request
= fsl_pwm_request
,
389 .free
= fsl_pwm_free
,
390 .config
= fsl_pwm_config
,
391 .set_polarity
= fsl_pwm_set_polarity
,
392 .enable
= fsl_pwm_enable
,
393 .disable
= fsl_pwm_disable
,
394 .owner
= THIS_MODULE
,
397 static int fsl_pwm_init(struct fsl_pwm_chip
*fpc
)
401 ret
= clk_prepare_enable(fpc
->clk
[FSL_PWM_CLK_SYS
]);
405 writel(0x00, fpc
->base
+ FTM_CNTIN
);
406 writel(0x00, fpc
->base
+ FTM_OUTINIT
);
407 writel(0xFF, fpc
->base
+ FTM_OUTMASK
);
409 clk_disable_unprepare(fpc
->clk
[FSL_PWM_CLK_SYS
]);
414 static int fsl_pwm_probe(struct platform_device
*pdev
)
416 struct fsl_pwm_chip
*fpc
;
417 struct resource
*res
;
420 fpc
= devm_kzalloc(&pdev
->dev
, sizeof(*fpc
), GFP_KERNEL
);
424 mutex_init(&fpc
->lock
);
426 fpc
->chip
.dev
= &pdev
->dev
;
428 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
429 fpc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
430 if (IS_ERR(fpc
->base
))
431 return PTR_ERR(fpc
->base
);
433 fpc
->clk
[FSL_PWM_CLK_SYS
] = devm_clk_get(&pdev
->dev
, "ftm_sys");
434 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_SYS
])) {
435 dev_err(&pdev
->dev
, "failed to get \"ftm_sys\" clock\n");
436 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_SYS
]);
439 fpc
->clk
[FSL_PWM_CLK_FIX
] = devm_clk_get(fpc
->chip
.dev
, "ftm_fix");
440 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_FIX
]))
441 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_FIX
]);
443 fpc
->clk
[FSL_PWM_CLK_EXT
] = devm_clk_get(fpc
->chip
.dev
, "ftm_ext");
444 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_EXT
]))
445 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_EXT
]);
447 fpc
->clk
[FSL_PWM_CLK_CNTEN
] =
448 devm_clk_get(fpc
->chip
.dev
, "ftm_cnt_clk_en");
449 if (IS_ERR(fpc
->clk
[FSL_PWM_CLK_CNTEN
]))
450 return PTR_ERR(fpc
->clk
[FSL_PWM_CLK_CNTEN
]);
452 fpc
->chip
.ops
= &fsl_pwm_ops
;
453 fpc
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
454 fpc
->chip
.of_pwm_n_cells
= 3;
458 ret
= pwmchip_add(&fpc
->chip
);
460 dev_err(&pdev
->dev
, "failed to add PWM chip: %d\n", ret
);
464 platform_set_drvdata(pdev
, fpc
);
466 return fsl_pwm_init(fpc
);
469 static int fsl_pwm_remove(struct platform_device
*pdev
)
471 struct fsl_pwm_chip
*fpc
= platform_get_drvdata(pdev
);
473 return pwmchip_remove(&fpc
->chip
);
476 static const struct of_device_id fsl_pwm_dt_ids
[] = {
477 { .compatible
= "fsl,vf610-ftm-pwm", },
480 MODULE_DEVICE_TABLE(of
, fsl_pwm_dt_ids
);
482 static struct platform_driver fsl_pwm_driver
= {
484 .name
= "fsl-ftm-pwm",
485 .of_match_table
= fsl_pwm_dt_ids
,
487 .probe
= fsl_pwm_probe
,
488 .remove
= fsl_pwm_remove
,
490 module_platform_driver(fsl_pwm_driver
);
492 MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
493 MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
494 MODULE_ALIAS("platform:fsl-ftm-pwm");
495 MODULE_LICENSE("GPL");