Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Freescale FlexTimer Module (FTM) PWM Driver
4 *
5 * Copyright 2012-2013 Freescale Semiconductor, Inc.
6 */
7
8#include <linux/clk.h>
9#include <linux/err.h>
10#include <linux/io.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/of_address.h>
15#include <linux/of_device.h>
16#include <linux/platform_device.h>
17#include <linux/pm.h>
18#include <linux/pwm.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
21#include <linux/fsl/ftm.h>
22
23#define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
24
25enum fsl_pwm_clk {
26 FSL_PWM_CLK_SYS,
27 FSL_PWM_CLK_FIX,
28 FSL_PWM_CLK_EXT,
29 FSL_PWM_CLK_CNTEN,
30 FSL_PWM_CLK_MAX
31};
32
33struct fsl_ftm_soc {
34 bool has_enable_bits;
35};
36
37struct fsl_pwm_periodcfg {
38 enum fsl_pwm_clk clk_select;
39 unsigned int clk_ps;
40 unsigned int mod_period;
41};
42
43struct fsl_pwm_chip {
44 struct pwm_chip chip;
45 struct mutex lock;
46 struct regmap *regmap;
47
48 /* This value is valid iff a pwm is running */
49 struct fsl_pwm_periodcfg period;
50
51 struct clk *ipg_clk;
52 struct clk *clk[FSL_PWM_CLK_MAX];
53
54 const struct fsl_ftm_soc *soc;
55};
56
57static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
58{
59 return container_of(chip, struct fsl_pwm_chip, chip);
60}
61
62static void ftm_clear_write_protection(struct fsl_pwm_chip *fpc)
63{
64 u32 val;
65
66 regmap_read(fpc->regmap, FTM_FMS, &val);
67 if (val & FTM_FMS_WPEN)
68 regmap_set_bits(fpc->regmap, FTM_MODE, FTM_MODE_WPDIS);
69}
70
71static void ftm_set_write_protection(struct fsl_pwm_chip *fpc)
72{
73 regmap_set_bits(fpc->regmap, FTM_FMS, FTM_FMS_WPEN);
74}
75
76static bool fsl_pwm_periodcfg_are_equal(const struct fsl_pwm_periodcfg *a,
77 const struct fsl_pwm_periodcfg *b)
78{
79 if (a->clk_select != b->clk_select)
80 return false;
81 if (a->clk_ps != b->clk_ps)
82 return false;
83 if (a->mod_period != b->mod_period)
84 return false;
85 return true;
86}
87
88static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
89{
90 int ret;
91 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
92
93 ret = clk_prepare_enable(fpc->ipg_clk);
94 if (!ret && fpc->soc->has_enable_bits) {
95 mutex_lock(&fpc->lock);
96 regmap_set_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16));
97 mutex_unlock(&fpc->lock);
98 }
99
100 return ret;
101}
102
103static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
104{
105 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
106
107 if (fpc->soc->has_enable_bits) {
108 mutex_lock(&fpc->lock);
109 regmap_clear_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16));
110 mutex_unlock(&fpc->lock);
111 }
112
113 clk_disable_unprepare(fpc->ipg_clk);
114}
115
116static unsigned int fsl_pwm_ticks_to_ns(struct fsl_pwm_chip *fpc,
117 unsigned int ticks)
118{
119 unsigned long rate;
120 unsigned long long exval;
121
122 rate = clk_get_rate(fpc->clk[fpc->period.clk_select]);
123 exval = ticks;
124 exval *= 1000000000UL;
125 do_div(exval, rate >> fpc->period.clk_ps);
126 return exval;
127}
128
129static bool fsl_pwm_calculate_period_clk(struct fsl_pwm_chip *fpc,
130 unsigned int period_ns,
131 enum fsl_pwm_clk index,
132 struct fsl_pwm_periodcfg *periodcfg
133 )
134{
135 unsigned long long c;
136 unsigned int ps;
137
138 c = clk_get_rate(fpc->clk[index]);
139 c = c * period_ns;
140 do_div(c, 1000000000UL);
141
142 if (c == 0)
143 return false;
144
145 for (ps = 0; ps < 8 ; ++ps, c >>= 1) {
146 if (c <= 0x10000) {
147 periodcfg->clk_select = index;
148 periodcfg->clk_ps = ps;
149 periodcfg->mod_period = c - 1;
150 return true;
151 }
152 }
153 return false;
154}
155
156static bool fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
157 unsigned int period_ns,
158 struct fsl_pwm_periodcfg *periodcfg)
159{
160 enum fsl_pwm_clk m0, m1;
161 unsigned long fix_rate, ext_rate;
162 bool ret;
163
164 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, FSL_PWM_CLK_SYS,
165 periodcfg);
166 if (ret)
167 return true;
168
169 fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
170 ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
171
172 if (fix_rate > ext_rate) {
173 m0 = FSL_PWM_CLK_FIX;
174 m1 = FSL_PWM_CLK_EXT;
175 } else {
176 m0 = FSL_PWM_CLK_EXT;
177 m1 = FSL_PWM_CLK_FIX;
178 }
179
180 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, m0, periodcfg);
181 if (ret)
182 return true;
183
184 return fsl_pwm_calculate_period_clk(fpc, period_ns, m1, periodcfg);
185}
186
187static unsigned int fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
188 unsigned int duty_ns)
189{
190 unsigned long long duty;
191
192 unsigned int period = fpc->period.mod_period + 1;
193 unsigned int period_ns = fsl_pwm_ticks_to_ns(fpc, period);
194
195 duty = (unsigned long long)duty_ns * period;
196 do_div(duty, period_ns);
197
198 return (unsigned int)duty;
199}
200
201static bool fsl_pwm_is_any_pwm_enabled(struct fsl_pwm_chip *fpc,
202 struct pwm_device *pwm)
203{
204 u32 val;
205
206 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
207 if (~val & 0xFF)
208 return true;
209 else
210 return false;
211}
212
213static bool fsl_pwm_is_other_pwm_enabled(struct fsl_pwm_chip *fpc,
214 struct pwm_device *pwm)
215{
216 u32 val;
217
218 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
219 if (~(val | BIT(pwm->hwpwm)) & 0xFF)
220 return true;
221 else
222 return false;
223}
224
225static int fsl_pwm_apply_config(struct fsl_pwm_chip *fpc,
226 struct pwm_device *pwm,
227 const struct pwm_state *newstate)
228{
229 unsigned int duty;
230 u32 reg_polarity;
231
232 struct fsl_pwm_periodcfg periodcfg;
233 bool do_write_period = false;
234
235 if (!fsl_pwm_calculate_period(fpc, newstate->period, &periodcfg)) {
236 dev_err(fpc->chip.dev, "failed to calculate new period\n");
237 return -EINVAL;
238 }
239
240 if (!fsl_pwm_is_any_pwm_enabled(fpc, pwm))
241 do_write_period = true;
242 /*
243 * The Freescale FTM controller supports only a single period for
244 * all PWM channels, therefore verify if the newly computed period
245 * is different than the current period being used. In such case
246 * we allow to change the period only if no other pwm is running.
247 */
248 else if (!fsl_pwm_periodcfg_are_equal(&fpc->period, &periodcfg)) {
249 if (fsl_pwm_is_other_pwm_enabled(fpc, pwm)) {
250 dev_err(fpc->chip.dev,
251 "Cannot change period for PWM %u, disable other PWMs first\n",
252 pwm->hwpwm);
253 return -EBUSY;
254 }
255 if (fpc->period.clk_select != periodcfg.clk_select) {
256 int ret;
257 enum fsl_pwm_clk oldclk = fpc->period.clk_select;
258 enum fsl_pwm_clk newclk = periodcfg.clk_select;
259
260 ret = clk_prepare_enable(fpc->clk[newclk]);
261 if (ret)
262 return ret;
263 clk_disable_unprepare(fpc->clk[oldclk]);
264 }
265 do_write_period = true;
266 }
267
268 ftm_clear_write_protection(fpc);
269
270 if (do_write_period) {
271 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
272 FTM_SC_CLK(periodcfg.clk_select));
273 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
274 periodcfg.clk_ps);
275 regmap_write(fpc->regmap, FTM_MOD, periodcfg.mod_period);
276
277 fpc->period = periodcfg;
278 }
279
280 duty = fsl_pwm_calculate_duty(fpc, newstate->duty_cycle);
281
282 regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
283 FTM_CSC_MSB | FTM_CSC_ELSB);
284 regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
285
286 reg_polarity = 0;
287 if (newstate->polarity == PWM_POLARITY_INVERSED)
288 reg_polarity = BIT(pwm->hwpwm);
289
290 regmap_update_bits(fpc->regmap, FTM_POL, BIT(pwm->hwpwm), reg_polarity);
291
292 ftm_set_write_protection(fpc);
293
294 return 0;
295}
296
297static int fsl_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
298 const struct pwm_state *newstate)
299{
300 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
301 struct pwm_state *oldstate = &pwm->state;
302 int ret = 0;
303
304 /*
305 * oldstate to newstate : action
306 *
307 * disabled to disabled : ignore
308 * enabled to disabled : disable
309 * enabled to enabled : update settings
310 * disabled to enabled : update settings + enable
311 */
312
313 mutex_lock(&fpc->lock);
314
315 if (!newstate->enabled) {
316 if (oldstate->enabled) {
317 regmap_set_bits(fpc->regmap, FTM_OUTMASK,
318 BIT(pwm->hwpwm));
319 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
320 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
321 }
322
323 goto end_mutex;
324 }
325
326 ret = fsl_pwm_apply_config(fpc, pwm, newstate);
327 if (ret)
328 goto end_mutex;
329
330 /* check if need to enable */
331 if (!oldstate->enabled) {
332 ret = clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
333 if (ret)
334 goto end_mutex;
335
336 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
337 if (ret) {
338 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
339 goto end_mutex;
340 }
341
342 regmap_clear_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm));
343 }
344
345end_mutex:
346 mutex_unlock(&fpc->lock);
347 return ret;
348}
349
350static const struct pwm_ops fsl_pwm_ops = {
351 .request = fsl_pwm_request,
352 .free = fsl_pwm_free,
353 .apply = fsl_pwm_apply,
354 .owner = THIS_MODULE,
355};
356
357static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
358{
359 int ret;
360
361 ret = clk_prepare_enable(fpc->ipg_clk);
362 if (ret)
363 return ret;
364
365 regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
366 regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
367 regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
368
369 clk_disable_unprepare(fpc->ipg_clk);
370
371 return 0;
372}
373
374static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
375{
376 switch (reg) {
377 case FTM_FMS:
378 case FTM_MODE:
379 case FTM_CNT:
380 return true;
381 }
382 return false;
383}
384
385static const struct regmap_config fsl_pwm_regmap_config = {
386 .reg_bits = 32,
387 .reg_stride = 4,
388 .val_bits = 32,
389
390 .max_register = FTM_PWMLOAD,
391 .volatile_reg = fsl_pwm_volatile_reg,
392 .cache_type = REGCACHE_FLAT,
393};
394
395static int fsl_pwm_probe(struct platform_device *pdev)
396{
397 struct fsl_pwm_chip *fpc;
398 void __iomem *base;
399 int ret;
400
401 fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
402 if (!fpc)
403 return -ENOMEM;
404
405 mutex_init(&fpc->lock);
406
407 fpc->soc = of_device_get_match_data(&pdev->dev);
408 fpc->chip.dev = &pdev->dev;
409
410 base = devm_platform_ioremap_resource(pdev, 0);
411 if (IS_ERR(base))
412 return PTR_ERR(base);
413
414 fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
415 &fsl_pwm_regmap_config);
416 if (IS_ERR(fpc->regmap)) {
417 dev_err(&pdev->dev, "regmap init failed\n");
418 return PTR_ERR(fpc->regmap);
419 }
420
421 fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
422 if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
423 dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
424 return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
425 }
426
427 fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
428 if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
429 return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
430
431 fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
432 if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
433 return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
434
435 fpc->clk[FSL_PWM_CLK_CNTEN] =
436 devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
437 if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
438 return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
439
440 /*
441 * ipg_clk is the interface clock for the IP. If not provided, use the
442 * ftm_sys clock as the default.
443 */
444 fpc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
445 if (IS_ERR(fpc->ipg_clk))
446 fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS];
447
448
449 fpc->chip.ops = &fsl_pwm_ops;
450 fpc->chip.npwm = 8;
451
452 ret = devm_pwmchip_add(&pdev->dev, &fpc->chip);
453 if (ret < 0) {
454 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
455 return ret;
456 }
457
458 platform_set_drvdata(pdev, fpc);
459
460 return fsl_pwm_init(fpc);
461}
462
463#ifdef CONFIG_PM_SLEEP
464static int fsl_pwm_suspend(struct device *dev)
465{
466 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
467 int i;
468
469 regcache_cache_only(fpc->regmap, true);
470 regcache_mark_dirty(fpc->regmap);
471
472 for (i = 0; i < fpc->chip.npwm; i++) {
473 struct pwm_device *pwm = &fpc->chip.pwms[i];
474
475 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
476 continue;
477
478 clk_disable_unprepare(fpc->ipg_clk);
479
480 if (!pwm_is_enabled(pwm))
481 continue;
482
483 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
484 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
485 }
486
487 return 0;
488}
489
490static int fsl_pwm_resume(struct device *dev)
491{
492 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
493 int i;
494
495 for (i = 0; i < fpc->chip.npwm; i++) {
496 struct pwm_device *pwm = &fpc->chip.pwms[i];
497
498 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
499 continue;
500
501 clk_prepare_enable(fpc->ipg_clk);
502
503 if (!pwm_is_enabled(pwm))
504 continue;
505
506 clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
507 clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
508 }
509
510 /* restore all registers from cache */
511 regcache_cache_only(fpc->regmap, false);
512 regcache_sync(fpc->regmap);
513
514 return 0;
515}
516#endif
517
518static const struct dev_pm_ops fsl_pwm_pm_ops = {
519 SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
520};
521
522static const struct fsl_ftm_soc vf610_ftm_pwm = {
523 .has_enable_bits = false,
524};
525
526static const struct fsl_ftm_soc imx8qm_ftm_pwm = {
527 .has_enable_bits = true,
528};
529
530static const struct of_device_id fsl_pwm_dt_ids[] = {
531 { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm },
532 { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm },
533 { /* sentinel */ }
534};
535MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
536
537static struct platform_driver fsl_pwm_driver = {
538 .driver = {
539 .name = "fsl-ftm-pwm",
540 .of_match_table = fsl_pwm_dt_ids,
541 .pm = &fsl_pwm_pm_ops,
542 },
543 .probe = fsl_pwm_probe,
544};
545module_platform_driver(fsl_pwm_driver);
546
547MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
548MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
549MODULE_ALIAS("platform:fsl-ftm-pwm");
550MODULE_LICENSE("GPL");
1/*
2 * Freescale FlexTimer Module (FTM) PWM Driver
3 *
4 * Copyright 2012-2013 Freescale Semiconductor, Inc.
5 *
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.
10 */
11
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/platform_device.h>
20#include <linux/pwm.h>
21#include <linux/slab.h>
22
23#define FTM_SC 0x00
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
29
30#define FTM_CNT 0x04
31#define FTM_MOD 0x08
32
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))
39
40#define FTM_CV_BASE 0x10
41#define FTM_CV(_channel) (FTM_CV_BASE + ((_channel) * 8))
42
43#define FTM_CNTIN 0x4C
44#define FTM_STATUS 0x50
45
46#define FTM_MODE 0x54
47#define FTM_MODE_FTMEN BIT(0)
48#define FTM_MODE_INIT BIT(2)
49#define FTM_MODE_PWMSYNC BIT(3)
50
51#define FTM_SYNC 0x58
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
57#define FTM_POL 0x70
58#define FTM_FMS 0x74
59#define FTM_FILTER 0x78
60#define FTM_FLTCTRL 0x7C
61#define FTM_QDCTRL 0x80
62#define FTM_CONF 0x84
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
68
69enum fsl_pwm_clk {
70 FSL_PWM_CLK_SYS,
71 FSL_PWM_CLK_FIX,
72 FSL_PWM_CLK_EXT,
73 FSL_PWM_CLK_CNTEN,
74 FSL_PWM_CLK_MAX
75};
76
77struct fsl_pwm_chip {
78 struct pwm_chip chip;
79
80 struct mutex lock;
81
82 unsigned int use_count;
83 unsigned int cnt_select;
84 unsigned int clk_ps;
85
86 void __iomem *base;
87
88 int period_ns;
89
90 struct clk *clk[FSL_PWM_CLK_MAX];
91};
92
93static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
94{
95 return container_of(chip, struct fsl_pwm_chip, chip);
96}
97
98static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
99{
100 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
101
102 return clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
103}
104
105static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
106{
107 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
108
109 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
110}
111
112static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc,
113 enum fsl_pwm_clk index)
114{
115 unsigned long sys_rate, cnt_rate;
116 unsigned long long ratio;
117
118 sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]);
119 if (!sys_rate)
120 return -EINVAL;
121
122 cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
123 if (!cnt_rate)
124 return -EINVAL;
125
126 switch (index) {
127 case FSL_PWM_CLK_SYS:
128 fpc->clk_ps = 1;
129 break;
130 case FSL_PWM_CLK_FIX:
131 ratio = 2 * cnt_rate - 1;
132 do_div(ratio, sys_rate);
133 fpc->clk_ps = ratio;
134 break;
135 case FSL_PWM_CLK_EXT:
136 ratio = 4 * cnt_rate - 1;
137 do_div(ratio, sys_rate);
138 fpc->clk_ps = ratio;
139 break;
140 default:
141 return -EINVAL;
142 }
143
144 return 0;
145}
146
147static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc,
148 unsigned long period_ns)
149{
150 unsigned long long c, c0;
151
152 c = clk_get_rate(fpc->clk[fpc->cnt_select]);
153 c = c * period_ns;
154 do_div(c, 1000000000UL);
155
156 do {
157 c0 = c;
158 do_div(c0, (1 << fpc->clk_ps));
159 if (c0 <= 0xFFFF)
160 return (unsigned long)c0;
161 } while (++fpc->clk_ps < 8);
162
163 return 0;
164}
165
166static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc,
167 unsigned long period_ns,
168 enum fsl_pwm_clk index)
169{
170 int ret;
171
172 ret = fsl_pwm_calculate_default_ps(fpc, index);
173 if (ret) {
174 dev_err(fpc->chip.dev,
175 "failed to calculate default prescaler: %d\n",
176 ret);
177 return 0;
178 }
179
180 return fsl_pwm_calculate_cycles(fpc, period_ns);
181}
182
183static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
184 unsigned long period_ns)
185{
186 enum fsl_pwm_clk m0, m1;
187 unsigned long fix_rate, ext_rate, cycles;
188
189 cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns,
190 FSL_PWM_CLK_SYS);
191 if (cycles) {
192 fpc->cnt_select = FSL_PWM_CLK_SYS;
193 return cycles;
194 }
195
196 fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
197 ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
198
199 if (fix_rate > ext_rate) {
200 m0 = FSL_PWM_CLK_FIX;
201 m1 = FSL_PWM_CLK_EXT;
202 } else {
203 m0 = FSL_PWM_CLK_EXT;
204 m1 = FSL_PWM_CLK_FIX;
205 }
206
207 cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
208 if (cycles) {
209 fpc->cnt_select = m0;
210 return cycles;
211 }
212
213 fpc->cnt_select = m1;
214
215 return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1);
216}
217
218static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
219 unsigned long period_ns,
220 unsigned long duty_ns)
221{
222 unsigned long long val, duty;
223
224 val = readl(fpc->base + FTM_MOD);
225 duty = duty_ns * (val + 1);
226 do_div(duty, period_ns);
227
228 return (unsigned long)duty;
229}
230
231static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
232 int duty_ns, int period_ns)
233{
234 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
235 u32 val, period, duty;
236
237 mutex_lock(&fpc->lock);
238
239 /*
240 * The Freescale FTM controller supports only a single period for
241 * all PWM channels, therefore incompatible changes need to be
242 * refused.
243 */
244 if (fpc->period_ns && fpc->period_ns != period_ns) {
245 dev_err(fpc->chip.dev,
246 "conflicting period requested for PWM %u\n",
247 pwm->hwpwm);
248 mutex_unlock(&fpc->lock);
249 return -EBUSY;
250 }
251
252 if (!fpc->period_ns && duty_ns) {
253 period = fsl_pwm_calculate_period(fpc, period_ns);
254 if (!period) {
255 dev_err(fpc->chip.dev, "failed to calculate period\n");
256 mutex_unlock(&fpc->lock);
257 return -EINVAL;
258 }
259
260 val = readl(fpc->base + FTM_SC);
261 val &= ~(FTM_SC_PS_MASK << FTM_SC_PS_SHIFT);
262 val |= fpc->clk_ps;
263 writel(val, fpc->base + FTM_SC);
264 writel(period - 1, fpc->base + FTM_MOD);
265
266 fpc->period_ns = period_ns;
267 }
268
269 mutex_unlock(&fpc->lock);
270
271 duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns);
272
273 writel(FTM_CSC_MSB | FTM_CSC_ELSB, fpc->base + FTM_CSC(pwm->hwpwm));
274 writel(duty, fpc->base + FTM_CV(pwm->hwpwm));
275
276 return 0;
277}
278
279static int fsl_pwm_set_polarity(struct pwm_chip *chip,
280 struct pwm_device *pwm,
281 enum pwm_polarity polarity)
282{
283 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
284 u32 val;
285
286 val = readl(fpc->base + FTM_POL);
287
288 if (polarity == PWM_POLARITY_INVERSED)
289 val |= BIT(pwm->hwpwm);
290 else
291 val &= ~BIT(pwm->hwpwm);
292
293 writel(val, fpc->base + FTM_POL);
294
295 return 0;
296}
297
298static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
299{
300 u32 val;
301 int ret;
302
303 if (fpc->use_count != 0)
304 return 0;
305
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);
311
312 ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]);
313 if (ret)
314 return ret;
315
316 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
317 if (ret) {
318 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
319 return ret;
320 }
321
322 fpc->use_count++;
323
324 return 0;
325}
326
327static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
328{
329 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
330 u32 val;
331 int ret;
332
333 mutex_lock(&fpc->lock);
334 val = readl(fpc->base + FTM_OUTMASK);
335 val &= ~BIT(pwm->hwpwm);
336 writel(val, fpc->base + FTM_OUTMASK);
337
338 ret = fsl_counter_clock_enable(fpc);
339 mutex_unlock(&fpc->lock);
340
341 return ret;
342}
343
344static void fsl_counter_clock_disable(struct fsl_pwm_chip *fpc)
345{
346 u32 val;
347
348 /*
349 * already disabled, do nothing
350 */
351 if (fpc->use_count == 0)
352 return;
353
354 /* there are still users, so can't disable yet */
355 if (--fpc->use_count > 0)
356 return;
357
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);
362
363 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
364 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
365}
366
367static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
368{
369 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
370 u32 val;
371
372 mutex_lock(&fpc->lock);
373 val = readl(fpc->base + FTM_OUTMASK);
374 val |= BIT(pwm->hwpwm);
375 writel(val, fpc->base + FTM_OUTMASK);
376
377 fsl_counter_clock_disable(fpc);
378
379 val = readl(fpc->base + FTM_OUTMASK);
380
381 if ((val & 0xFF) == 0xFF)
382 fpc->period_ns = 0;
383
384 mutex_unlock(&fpc->lock);
385}
386
387static 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,
395};
396
397static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
398{
399 int ret;
400
401 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
402 if (ret)
403 return ret;
404
405 writel(0x00, fpc->base + FTM_CNTIN);
406 writel(0x00, fpc->base + FTM_OUTINIT);
407 writel(0xFF, fpc->base + FTM_OUTMASK);
408
409 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
410
411 return 0;
412}
413
414static int fsl_pwm_probe(struct platform_device *pdev)
415{
416 struct fsl_pwm_chip *fpc;
417 struct resource *res;
418 int ret;
419
420 fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
421 if (!fpc)
422 return -ENOMEM;
423
424 mutex_init(&fpc->lock);
425
426 fpc->chip.dev = &pdev->dev;
427
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);
432
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]);
437 }
438
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]);
442
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]);
446
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]);
451
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;
455 fpc->chip.base = -1;
456 fpc->chip.npwm = 8;
457
458 ret = pwmchip_add(&fpc->chip);
459 if (ret < 0) {
460 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
461 return ret;
462 }
463
464 platform_set_drvdata(pdev, fpc);
465
466 return fsl_pwm_init(fpc);
467}
468
469static int fsl_pwm_remove(struct platform_device *pdev)
470{
471 struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev);
472
473 return pwmchip_remove(&fpc->chip);
474}
475
476static const struct of_device_id fsl_pwm_dt_ids[] = {
477 { .compatible = "fsl,vf610-ftm-pwm", },
478 { /* sentinel */ }
479};
480MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
481
482static struct platform_driver fsl_pwm_driver = {
483 .driver = {
484 .name = "fsl-ftm-pwm",
485 .of_match_table = fsl_pwm_dt_ids,
486 },
487 .probe = fsl_pwm_probe,
488 .remove = fsl_pwm_remove,
489};
490module_platform_driver(fsl_pwm_driver);
491
492MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
493MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
494MODULE_ALIAS("platform:fsl-ftm-pwm");
495MODULE_LICENSE("GPL");