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>
24 #include <linux/err.h>
25 #include <linux/clk.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pwm.h>
29 /* ECAP registers and bits definitions */
35 #define ECCTL2_APWM_POL_LOW BIT(10)
36 #define ECCTL2_APWM_MODE BIT(9)
37 #define ECCTL2_SYNC_SEL_DISA (BIT(7) | BIT(6))
38 #define ECCTL2_TSCTR_FREERUN BIT(4)
40 struct ecap_pwm_chip {
42 unsigned int clk_rate;
43 void __iomem *mmio_base;
46 static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip)
48 return container_of(chip, struct ecap_pwm_chip, chip);
52 * period_ns = 10^9 * period_cycles / PWM_CLK_RATE
53 * duty_ns = 10^9 * duty_cycles / PWM_CLK_RATE
55 static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
56 int duty_ns, int period_ns)
58 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
60 unsigned long period_cycles, duty_cycles;
63 if (period_ns > NSEC_PER_SEC)
68 do_div(c, NSEC_PER_SEC);
69 period_cycles = (unsigned long)c;
71 if (period_cycles < 1) {
77 do_div(c, NSEC_PER_SEC);
78 duty_cycles = (unsigned long)c;
81 pm_runtime_get_sync(pc->chip.dev);
83 reg_val = readw(pc->mmio_base + ECCTL2);
85 /* Configure APWM mode & disable sync option */
86 reg_val |= ECCTL2_APWM_MODE | ECCTL2_SYNC_SEL_DISA;
88 writew(reg_val, pc->mmio_base + ECCTL2);
90 if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
91 /* Update active registers if not running */
92 writel(duty_cycles, pc->mmio_base + CAP2);
93 writel(period_cycles, pc->mmio_base + CAP1);
96 * Update shadow registers to configure period and
97 * compare values. This helps current PWM period to
98 * complete on reconfiguring
100 writel(duty_cycles, pc->mmio_base + CAP4);
101 writel(period_cycles, pc->mmio_base + CAP3);
104 if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
105 reg_val = readw(pc->mmio_base + ECCTL2);
106 /* Disable APWM mode to put APWM output Low */
107 reg_val &= ~ECCTL2_APWM_MODE;
108 writew(reg_val, pc->mmio_base + ECCTL2);
111 pm_runtime_put_sync(pc->chip.dev);
115 static int ecap_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
116 enum pwm_polarity polarity)
118 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
119 unsigned short reg_val;
121 pm_runtime_get_sync(pc->chip.dev);
122 reg_val = readw(pc->mmio_base + ECCTL2);
123 if (polarity == PWM_POLARITY_INVERSED)
124 /* Duty cycle defines LOW period of PWM */
125 reg_val |= ECCTL2_APWM_POL_LOW;
127 /* Duty cycle defines HIGH period of PWM */
128 reg_val &= ~ECCTL2_APWM_POL_LOW;
130 writew(reg_val, pc->mmio_base + ECCTL2);
131 pm_runtime_put_sync(pc->chip.dev);
135 static int ecap_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
137 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
138 unsigned int reg_val;
140 /* Leave clock enabled on enabling PWM */
141 pm_runtime_get_sync(pc->chip.dev);
144 * Enable 'Free run Time stamp counter mode' to start counter
145 * and 'APWM mode' to enable APWM output
147 reg_val = readw(pc->mmio_base + ECCTL2);
148 reg_val |= ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE;
149 writew(reg_val, pc->mmio_base + ECCTL2);
153 static void ecap_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
155 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
156 unsigned int reg_val;
159 * Disable 'Free run Time stamp counter mode' to stop counter
160 * and 'APWM mode' to put APWM output to low
162 reg_val = readw(pc->mmio_base + ECCTL2);
163 reg_val &= ~(ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE);
164 writew(reg_val, pc->mmio_base + ECCTL2);
166 /* Disable clock on PWM disable */
167 pm_runtime_put_sync(pc->chip.dev);
170 static void ecap_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
172 if (test_bit(PWMF_ENABLED, &pwm->flags)) {
173 dev_warn(chip->dev, "Removing PWM device without disabling\n");
174 pm_runtime_put_sync(chip->dev);
178 static const struct pwm_ops ecap_pwm_ops = {
179 .free = ecap_pwm_free,
180 .config = ecap_pwm_config,
181 .set_polarity = ecap_pwm_set_polarity,
182 .enable = ecap_pwm_enable,
183 .disable = ecap_pwm_disable,
184 .owner = THIS_MODULE,
187 static int __devinit ecap_pwm_probe(struct platform_device *pdev)
192 struct ecap_pwm_chip *pc;
194 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
196 dev_err(&pdev->dev, "failed to allocate memory\n");
200 clk = devm_clk_get(&pdev->dev, "fck");
202 dev_err(&pdev->dev, "failed to get clock\n");
206 pc->clk_rate = clk_get_rate(clk);
208 dev_err(&pdev->dev, "failed to get clock rate\n");
212 pc->chip.dev = &pdev->dev;
213 pc->chip.ops = &ecap_pwm_ops;
217 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
219 dev_err(&pdev->dev, "no memory resource defined\n");
223 pc->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
225 return -EADDRNOTAVAIL;
227 ret = pwmchip_add(&pc->chip);
229 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
233 pm_runtime_enable(&pdev->dev);
234 platform_set_drvdata(pdev, pc);
238 static int __devexit ecap_pwm_remove(struct platform_device *pdev)
240 struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);
242 pm_runtime_put_sync(&pdev->dev);
243 pm_runtime_disable(&pdev->dev);
244 return pwmchip_remove(&pc->chip);
247 static struct platform_driver ecap_pwm_driver = {
251 .probe = ecap_pwm_probe,
252 .remove = __devexit_p(ecap_pwm_remove),
255 module_platform_driver(ecap_pwm_driver);
257 MODULE_DESCRIPTION("ECAP PWM driver");
258 MODULE_AUTHOR("Texas Instruments");
259 MODULE_LICENSE("GPL");