4 * Copyright (C) 2012 Texas Instruments, Inc. - http://www.ti.com/
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pwm.h>
25 #include <linux/err.h>
26 #include <linux/clk.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/of_device.h>
29 #include <linux/pinctrl/consumer.h>
31 #include "pwm-tipwmss.h"
33 /* EHRPWM registers and bits definitions */
35 /* Time base module registers */
39 #define TBCTL_RUN_MASK (BIT(15) | BIT(14))
40 #define TBCTL_STOP_NEXT 0
41 #define TBCTL_STOP_ON_CYCLE BIT(14)
42 #define TBCTL_FREE_RUN (BIT(15) | BIT(14))
43 #define TBCTL_PRDLD_MASK BIT(3)
44 #define TBCTL_PRDLD_SHDW 0
45 #define TBCTL_PRDLD_IMDT BIT(3)
46 #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
48 #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
49 #define TBCTL_CTRMODE_UP 0
50 #define TBCTL_CTRMODE_DOWN BIT(0)
51 #define TBCTL_CTRMODE_UPDOWN BIT(1)
52 #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
54 #define TBCTL_HSPCLKDIV_SHIFT 7
55 #define TBCTL_CLKDIV_SHIFT 10
58 #define HSPCLKDIV_MAX 7
59 #define PERIOD_MAX 0xFFFF
61 /* compare module registers */
65 /* Action qualifier module registers */
71 #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
72 #define AQCTL_CBU_FRCLOW BIT(8)
73 #define AQCTL_CBU_FRCHIGH BIT(9)
74 #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
75 #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
76 #define AQCTL_CAU_FRCLOW BIT(4)
77 #define AQCTL_CAU_FRCHIGH BIT(5)
78 #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
79 #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
80 #define AQCTL_PRD_FRCLOW BIT(2)
81 #define AQCTL_PRD_FRCHIGH BIT(3)
82 #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
83 #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
84 #define AQCTL_ZRO_FRCLOW BIT(0)
85 #define AQCTL_ZRO_FRCHIGH BIT(1)
86 #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
88 #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
90 #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
92 #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
94 #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
97 #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
98 #define AQSFRC_RLDCSF_ZRO 0
99 #define AQSFRC_RLDCSF_PRD BIT(6)
100 #define AQSFRC_RLDCSF_ZROPRD BIT(7)
101 #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
103 #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
104 #define AQCSFRC_CSFB_FRCDIS 0
105 #define AQCSFRC_CSFB_FRCLOW BIT(2)
106 #define AQCSFRC_CSFB_FRCHIGH BIT(3)
107 #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
108 #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
109 #define AQCSFRC_CSFA_FRCDIS 0
110 #define AQCSFRC_CSFA_FRCLOW BIT(0)
111 #define AQCSFRC_CSFA_FRCHIGH BIT(1)
112 #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
114 #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
116 struct ehrpwm_context {
127 struct ehrpwm_pwm_chip {
128 struct pwm_chip chip;
129 unsigned int clk_rate;
130 void __iomem *mmio_base;
131 unsigned long period_cycles[NUM_PWM_CHANNEL];
132 enum pwm_polarity polarity[NUM_PWM_CHANNEL];
134 struct ehrpwm_context ctx;
137 static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
139 return container_of(chip, struct ehrpwm_pwm_chip, chip);
142 static u16 ehrpwm_read(void *base, int offset)
144 return readw(base + offset);
147 static void ehrpwm_write(void *base, int offset, unsigned int val)
149 writew(val & 0xFFFF, base + offset);
152 static void ehrpwm_modify(void *base, int offset,
153 unsigned short mask, unsigned short val)
155 unsigned short regval;
157 regval = readw(base + offset);
159 regval |= val & mask;
160 writew(regval, base + offset);
164 * set_prescale_div - Set up the prescaler divider function
165 * @rqst_prescaler: prescaler value min
166 * @prescale_div: prescaler value set
167 * @tb_clk_div: Time Base Control prescaler bits
169 static int set_prescale_div(unsigned long rqst_prescaler,
170 unsigned short *prescale_div, unsigned short *tb_clk_div)
172 unsigned int clkdiv, hspclkdiv;
174 for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
175 for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
178 * calculations for prescaler value :
179 * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
180 * HSPCLKDIVIDER = 2 ** hspclkdiv
181 * CLKDIVIDER = (1), if clkdiv == 0 *OR*
182 * (2 * clkdiv), if clkdiv != 0
184 * Configure prescale_div value such that period
185 * register value is less than 65535.
188 *prescale_div = (1 << clkdiv) *
189 (hspclkdiv ? (hspclkdiv * 2) : 1);
190 if (*prescale_div > rqst_prescaler) {
191 *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
192 (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
200 static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
203 unsigned short aqctl_val, aqctl_mask;
206 * Configure PWM output to HIGH/LOW level on counter
207 * reaches compare register value and LOW/HIGH level
208 * on counter value reaches period register value and
209 * zero value on counter
213 aqctl_mask = AQCTL_CBU_MASK;
215 if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
216 aqctl_val = AQCTL_CHANB_POLINVERSED;
218 aqctl_val = AQCTL_CHANB_POLNORMAL;
221 aqctl_mask = AQCTL_CAU_MASK;
223 if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
224 aqctl_val = AQCTL_CHANA_POLINVERSED;
226 aqctl_val = AQCTL_CHANA_POLNORMAL;
229 aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
230 ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
234 * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
235 * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
237 static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
238 int duty_ns, int period_ns)
240 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
241 unsigned long long c;
242 unsigned long period_cycles, duty_cycles;
243 unsigned short ps_divval, tb_divval;
246 if (period_ns > NSEC_PER_SEC)
251 do_div(c, NSEC_PER_SEC);
252 period_cycles = (unsigned long)c;
254 if (period_cycles < 1) {
260 do_div(c, NSEC_PER_SEC);
261 duty_cycles = (unsigned long)c;
265 * Period values should be same for multiple PWM channels as IP uses
266 * same period register for multiple channels.
268 for (i = 0; i < NUM_PWM_CHANNEL; i++) {
269 if (pc->period_cycles[i] &&
270 (pc->period_cycles[i] != period_cycles)) {
272 * Allow channel to reconfigure period if no other
273 * channels being configured.
278 dev_err(chip->dev, "Period value conflicts with channel %d\n",
284 pc->period_cycles[pwm->hwpwm] = period_cycles;
286 /* Configure clock prescaler to support Low frequency PWM wave */
287 if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
289 dev_err(chip->dev, "Unsupported values\n");
293 pm_runtime_get_sync(chip->dev);
295 /* Update clock prescaler values */
296 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
298 /* Update period & duty cycle with presacler division */
299 period_cycles = period_cycles / ps_divval;
300 duty_cycles = duty_cycles / ps_divval;
302 /* Configure shadow loading on Period register */
303 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
305 ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
307 /* Configure ehrpwm counter for up-count mode */
308 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
312 /* Channel 1 configured with compare B register */
315 /* Channel 0 configured with compare A register */
318 ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
320 pm_runtime_put_sync(chip->dev);
324 static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
325 struct pwm_device *pwm, enum pwm_polarity polarity)
327 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
329 /* Configuration of polarity in hardware delayed, do at enable */
330 pc->polarity[pwm->hwpwm] = polarity;
334 static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
336 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
337 unsigned short aqcsfrc_val, aqcsfrc_mask;
340 /* Leave clock enabled on enabling PWM */
341 pm_runtime_get_sync(chip->dev);
343 /* Disabling Action Qualifier on PWM output */
345 aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
346 aqcsfrc_mask = AQCSFRC_CSFB_MASK;
348 aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
349 aqcsfrc_mask = AQCSFRC_CSFA_MASK;
352 /* Changes to shadow mode */
353 ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
356 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
358 /* Channels polarity can be configured from action qualifier module */
359 configure_polarity(pc, pwm->hwpwm);
361 /* Enable TBCLK before enabling PWM device */
362 ret = clk_prepare_enable(pc->tbclk);
364 pr_err("Failed to enable TBCLK for %s\n",
365 dev_name(pc->chip.dev));
369 /* Enable time counter for free_run */
370 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_FREE_RUN);
374 static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
376 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
377 unsigned short aqcsfrc_val, aqcsfrc_mask;
379 /* Action Qualifier puts PWM output low forcefully */
381 aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
382 aqcsfrc_mask = AQCSFRC_CSFB_MASK;
384 aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
385 aqcsfrc_mask = AQCSFRC_CSFA_MASK;
389 * Changes to immediate action on Action Qualifier. This puts
390 * Action Qualifier control on PWM output from next TBCLK
392 ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
395 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
397 /* Disabling TBCLK on PWM disable */
398 clk_disable_unprepare(pc->tbclk);
400 /* Stop Time base counter */
401 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT);
403 /* Disable clock on PWM disable */
404 pm_runtime_put_sync(chip->dev);
407 static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
409 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
411 if (test_bit(PWMF_ENABLED, &pwm->flags)) {
412 dev_warn(chip->dev, "Removing PWM device without disabling\n");
413 pm_runtime_put_sync(chip->dev);
416 /* set period value to zero on free */
417 pc->period_cycles[pwm->hwpwm] = 0;
420 static const struct pwm_ops ehrpwm_pwm_ops = {
421 .free = ehrpwm_pwm_free,
422 .config = ehrpwm_pwm_config,
423 .set_polarity = ehrpwm_pwm_set_polarity,
424 .enable = ehrpwm_pwm_enable,
425 .disable = ehrpwm_pwm_disable,
426 .owner = THIS_MODULE,
429 static const struct of_device_id ehrpwm_of_match[] = {
430 { .compatible = "ti,am33xx-ehrpwm" },
433 MODULE_DEVICE_TABLE(of, ehrpwm_of_match);
435 static int ehrpwm_pwm_probe(struct platform_device *pdev)
440 struct ehrpwm_pwm_chip *pc;
442 struct pinctrl *pinctrl;
444 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
446 dev_warn(&pdev->dev, "unable to select pin group\n");
448 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
450 dev_err(&pdev->dev, "failed to allocate memory\n");
454 clk = devm_clk_get(&pdev->dev, "fck");
456 dev_err(&pdev->dev, "failed to get clock\n");
460 pc->clk_rate = clk_get_rate(clk);
462 dev_err(&pdev->dev, "failed to get clock rate\n");
466 pc->chip.dev = &pdev->dev;
467 pc->chip.ops = &ehrpwm_pwm_ops;
468 pc->chip.of_xlate = of_pwm_xlate_with_flags;
469 pc->chip.of_pwm_n_cells = 3;
471 pc->chip.npwm = NUM_PWM_CHANNEL;
473 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475 dev_err(&pdev->dev, "no memory resource defined\n");
479 pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
480 if (IS_ERR(pc->mmio_base))
481 return PTR_ERR(pc->mmio_base);
483 /* Acquire tbclk for Time Base EHRPWM submodule */
484 pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
485 if (IS_ERR(pc->tbclk)) {
486 dev_err(&pdev->dev, "Failed to get tbclk\n");
487 return PTR_ERR(pc->tbclk);
490 ret = pwmchip_add(&pc->chip);
492 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
496 pm_runtime_enable(&pdev->dev);
497 pm_runtime_get_sync(&pdev->dev);
499 status = pwmss_submodule_state_change(pdev->dev.parent,
501 if (!(status & PWMSS_EPWMCLK_EN_ACK)) {
502 dev_err(&pdev->dev, "PWMSS config space clock enable failed\n");
504 goto pwmss_clk_failure;
507 pm_runtime_put_sync(&pdev->dev);
509 platform_set_drvdata(pdev, pc);
513 pm_runtime_put_sync(&pdev->dev);
514 pm_runtime_disable(&pdev->dev);
515 pwmchip_remove(&pc->chip);
519 static int ehrpwm_pwm_remove(struct platform_device *pdev)
521 struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
523 pm_runtime_get_sync(&pdev->dev);
525 * Due to hardware misbehaviour, acknowledge of the stop_req
526 * is missing. Hence checking of the status bit skipped.
528 pwmss_submodule_state_change(pdev->dev.parent, PWMSS_EPWMCLK_STOP_REQ);
529 pm_runtime_put_sync(&pdev->dev);
531 pm_runtime_put_sync(&pdev->dev);
532 pm_runtime_disable(&pdev->dev);
533 return pwmchip_remove(&pc->chip);
536 void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc)
538 pm_runtime_get_sync(pc->chip.dev);
539 pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
540 pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
541 pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
542 pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
543 pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
544 pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
545 pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
546 pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
547 pm_runtime_put_sync(pc->chip.dev);
550 void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc)
552 ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
553 ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
554 ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
555 ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
556 ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
557 ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
558 ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
559 ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
562 static int ehrpwm_pwm_suspend(struct device *dev)
564 struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
567 ehrpwm_pwm_save_context(pc);
568 for (i = 0; i < pc->chip.npwm; i++) {
569 struct pwm_device *pwm = &pc->chip.pwms[i];
571 if (!test_bit(PWMF_ENABLED, &pwm->flags))
574 /* Disable explicitly if PWM is running */
575 pm_runtime_put_sync(dev);
580 static int ehrpwm_pwm_resume(struct device *dev)
582 struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
585 for (i = 0; i < pc->chip.npwm; i++) {
586 struct pwm_device *pwm = &pc->chip.pwms[i];
588 if (!test_bit(PWMF_ENABLED, &pwm->flags))
591 /* Enable explicitly if PWM was running */
592 pm_runtime_get_sync(dev);
594 ehrpwm_pwm_restore_context(pc);
598 static SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
601 static struct platform_driver ehrpwm_pwm_driver = {
604 .owner = THIS_MODULE,
605 .of_match_table = ehrpwm_of_match,
606 .pm = &ehrpwm_pwm_pm_ops,
608 .probe = ehrpwm_pwm_probe,
609 .remove = ehrpwm_pwm_remove,
612 module_platform_driver(ehrpwm_pwm_driver);
614 MODULE_DESCRIPTION("EHRPWM PWM driver");
615 MODULE_AUTHOR("Texas Instruments");
616 MODULE_LICENSE("GPL");