drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / pwm / pwm-fsl-ftm.c
blobf21ea1b971166545016d5badf0e49706acb3d93d
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>
25 #include <linux/fsl/ftm.h>
27 #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
29 enum fsl_pwm_clk {
30 FSL_PWM_CLK_SYS,
31 FSL_PWM_CLK_FIX,
32 FSL_PWM_CLK_EXT,
33 FSL_PWM_CLK_CNTEN,
34 FSL_PWM_CLK_MAX
37 struct fsl_ftm_soc {
38 bool has_enable_bits;
41 struct fsl_pwm_chip {
42 struct pwm_chip chip;
44 struct mutex lock;
46 unsigned int cnt_select;
47 unsigned int clk_ps;
49 struct regmap *regmap;
51 int period_ns;
53 struct clk *ipg_clk;
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)
66 int ret;
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);
77 return ret;
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),
87 0);
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]);
101 if (!sys_rate)
102 return -EINVAL;
104 cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
105 if (!cnt_rate)
106 return -EINVAL;
108 switch (index) {
109 case FSL_PWM_CLK_SYS:
110 fpc->clk_ps = 1;
111 break;
112 case FSL_PWM_CLK_FIX:
113 ratio = 2 * cnt_rate - 1;
114 do_div(ratio, sys_rate);
115 fpc->clk_ps = ratio;
116 break;
117 case FSL_PWM_CLK_EXT:
118 ratio = 4 * cnt_rate - 1;
119 do_div(ratio, sys_rate);
120 fpc->clk_ps = ratio;
121 break;
122 default:
123 return -EINVAL;
126 return 0;
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]);
135 c = c * period_ns;
136 do_div(c, 1000000000UL);
138 do {
139 c0 = c;
140 do_div(c0, (1 << fpc->clk_ps));
141 if (c0 <= 0xFFFF)
142 return (unsigned long)c0;
143 } while (++fpc->clk_ps < 8);
145 return 0;
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)
152 int ret;
154 ret = fsl_pwm_calculate_default_ps(fpc, index);
155 if (ret) {
156 dev_err(fpc->chip.dev,
157 "failed to calculate default prescaler: %d\n",
158 ret);
159 return 0;
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,
172 FSL_PWM_CLK_SYS);
173 if (cycles) {
174 fpc->cnt_select = FSL_PWM_CLK_SYS;
175 return cycles;
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;
184 } else {
185 m0 = FSL_PWM_CLK_EXT;
186 m1 = FSL_PWM_CLK_FIX;
189 cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
190 if (cycles) {
191 fpc->cnt_select = m0;
192 return cycles;
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;
205 u32 val;
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);
218 u32 period, duty;
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
225 * refused.
227 if (fpc->period_ns && fpc->period_ns != period_ns) {
228 dev_err(fpc->chip.dev,
229 "conflicting period requested for PWM %u\n",
230 pwm->hwpwm);
231 mutex_unlock(&fpc->lock);
232 return -EBUSY;
235 if (!fpc->period_ns && duty_ns) {
236 period = fsl_pwm_calculate_period(fpc, period_ns);
237 if (!period) {
238 dev_err(fpc->chip.dev, "failed to calculate period\n");
239 mutex_unlock(&fpc->lock);
240 return -EINVAL;
243 regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
244 fpc->clk_ps);
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);
258 return 0;
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);
266 u32 val;
268 regmap_read(fpc->regmap, FTM_POL, &val);
270 if (polarity == PWM_POLARITY_INVERSED)
271 val |= BIT(pwm->hwpwm);
272 else
273 val &= ~BIT(pwm->hwpwm);
275 regmap_write(fpc->regmap, FTM_POL, val);
277 return 0;
280 static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
282 int ret;
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]);
289 if (ret)
290 return ret;
292 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
293 if (ret) {
294 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
295 return ret;
298 return 0;
301 static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
303 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
304 int ret;
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);
312 return ret;
315 static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
317 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
318 u32 val;
320 mutex_lock(&fpc->lock);
321 regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
322 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)
329 fpc->period_ns = 0;
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)
346 int ret;
348 ret = clk_prepare_enable(fpc->ipg_clk);
349 if (ret)
350 return ret;
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);
358 return 0;
361 static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
363 switch (reg) {
364 case FTM_CNT:
365 return true;
367 return false;
370 static const struct regmap_config fsl_pwm_regmap_config = {
371 .reg_bits = 32,
372 .reg_stride = 4,
373 .val_bits = 32,
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;
384 void __iomem *base;
385 int ret;
387 fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
388 if (!fpc)
389 return -ENOMEM;
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);
398 if (IS_ERR(base))
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;
439 fpc->chip.base = -1;
440 fpc->chip.npwm = 8;
442 ret = pwmchip_add(&fpc->chip);
443 if (ret < 0) {
444 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
445 return 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);
464 int i;
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))
473 continue;
475 clk_disable_unprepare(fpc->ipg_clk);
477 if (!pwm_is_enabled(pwm))
478 continue;
480 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
481 clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
484 return 0;
487 static int fsl_pwm_resume(struct device *dev)
489 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
490 int i;
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))
496 continue;
498 clk_prepare_enable(fpc->ipg_clk);
500 if (!pwm_is_enabled(pwm))
501 continue;
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);
511 return 0;
513 #endif
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 },
530 { /* sentinel */ }
532 MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
534 static struct platform_driver fsl_pwm_driver = {
535 .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");