arm:omap:pwm - eCAP & ePWM driver in AM335X
[sitara-epos/sitara-epos-kernel.git] / drivers / pwm / ecap.c
1 /*
2  * eCAP driver for PWM output generation
3  *
4  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/pwm/pwm.h>
22 #include <linux/slab.h>
24 #include <plat/clock.h>
25 #include <plat/config_pwm.h>
27 #define TIMER_CTR_REG                   0x0
28 #define CAPTURE_2_REG                   0x0c
29 #define CAPTURE_3_REG                   0x10
30 #define CAPTURE_4_REG                   0x14
31 #define CAPTURE_CTRL2_REG               0x2A
33 #define ECTRL2_SYNCOSEL_MASK            (0x03 << 6)
35 #define ECTRL2_MDSL_ECAP                BIT(9)
36 #define ECTRL2_CTRSTP_FREERUN           BIT(4)
37 #define ECTRL2_PLSL_LOW                 BIT(10)
38 #define ECTRL2_SYNC_EN                  BIT(5)
40 struct ecap_pwm {
41         struct pwm_device pwm;
42         struct pwm_device_ops ops;
43         spinlock_t      lock;
44         struct clk      *clk;
45         int     clk_enabled;
46         void __iomem    *mmio_base;
47         u8 version;
48 };
50 static inline struct ecap_pwm *to_ecap_pwm(const struct pwm_device *p)
51 {
52         return pwm_get_drvdata(p);
53 }
55 static int ecap_pwm_stop(struct pwm_device *p)
56 {
57         unsigned long flags;
58         struct ecap_pwm *ep = to_ecap_pwm(p);
60         clk_enable(ep->clk);
62         spin_lock_irqsave(&ep->lock, flags);
63         __raw_writew(__raw_readw(ep->mmio_base + CAPTURE_CTRL2_REG) &
64                 ~BIT(4), ep->mmio_base + CAPTURE_CTRL2_REG);
65         spin_unlock_irqrestore(&ep->lock, flags);
67         clk_disable(ep->clk);
68         if (ep->clk_enabled) {
69                 clk_disable(ep->clk);
70                 ep->clk_enabled = 0;
71         }
72         clear_bit(FLAG_RUNNING, &p->flags);
74         return 0;
75 }
77 static int ecap_pwm_start(struct pwm_device *p)
78 {
79         int ret = 0;
80         unsigned long flags;
81         struct ecap_pwm *ep = to_ecap_pwm(p);
83         clk_enable(ep->clk);
84         spin_lock_irqsave(&ep->lock, flags);
85         __raw_writew(__raw_readw(ep->mmio_base + CAPTURE_CTRL2_REG) |
86                 BIT(4), ep->mmio_base + CAPTURE_CTRL2_REG);
87         spin_unlock_irqrestore(&ep->lock, flags);
89         clk_disable(ep->clk);
90         if (!ep->clk_enabled) {
91                 ret = clk_enable(ep->clk);
92                 if (ret)
93                         return ret;
94                 ep->clk_enabled = 1;
95         }
96         set_bit(FLAG_RUNNING, &p->flags);
98         return ret;
99 }
101 static int ecap_pwm_set_polarity(struct pwm_device *p, char pol)
103         unsigned long flags;
104         struct ecap_pwm *ep = to_ecap_pwm(p);
106         clk_enable(ep->clk);
108         spin_lock_irqsave(&ep->lock, flags);
109          __raw_writew((__raw_readw(ep->mmio_base + CAPTURE_CTRL2_REG) &
110                  ~BIT(10)) | (!pol << 10), ep->mmio_base + CAPTURE_CTRL2_REG);
111         spin_unlock_irqrestore(&ep->lock, flags);
113         clk_disable(ep->clk);
114         return 0;
117 static int ecap_pwm_config_period(struct pwm_device *p)
119         unsigned long flags;
120         struct ecap_pwm *ep = to_ecap_pwm(p);
122          clk_enable(ep->clk);
124         spin_lock_irqsave(&ep->lock, flags);
125         __raw_writel((p->period_ticks) - 1, ep->mmio_base + CAPTURE_3_REG);
126         __raw_writew(ECTRL2_MDSL_ECAP | ECTRL2_SYNCOSEL_MASK |
127                  ECTRL2_CTRSTP_FREERUN, ep->mmio_base + CAPTURE_CTRL2_REG);
128         spin_unlock_irqrestore(&ep->lock, flags);
130         clk_disable(ep->clk);
131         return 0;
134 static int ecap_pwm_config_duty(struct pwm_device *p)
136         unsigned long flags;
137         struct ecap_pwm *ep = to_ecap_pwm(p);
139         clk_enable(ep->clk);
141         spin_lock_irqsave(&ep->lock, flags);
142         __raw_writew(ECTRL2_MDSL_ECAP | ECTRL2_SYNCOSEL_MASK |
143          ECTRL2_CTRSTP_FREERUN, ep->mmio_base + CAPTURE_CTRL2_REG);
144         if (p->duty_ticks > 0) {
145                 __raw_writel(p->duty_ticks, ep->mmio_base + CAPTURE_4_REG);
146         } else {
147         __raw_writel(p->duty_ticks, ep->mmio_base + CAPTURE_2_REG);
148         __raw_writel(0, ep->mmio_base + TIMER_CTR_REG);
149         }
150         spin_unlock_irqrestore(&ep->lock, flags);
152         clk_disable(ep->clk);
153         return 0;
156 static int ecap_pwm_config(struct pwm_device *p,
157                                 struct pwm_config *c)
159         int ret = 0;
160         switch (c->config_mask) {
162         case BIT(PWM_CONFIG_DUTY_TICKS):
163                 p->duty_ticks = c->duty_ticks;
164                 ret = ecap_pwm_config_duty(p);
165                 break;
167         case BIT(PWM_CONFIG_PERIOD_TICKS):
168                 p->period_ticks = c->period_ticks;
169                 ret = ecap_pwm_config_period(p);
170                 break;
172         case BIT(PWM_CONFIG_POLARITY):
173                 ret = ecap_pwm_set_polarity(p, c->polarity);
174                 break;
176         case BIT(PWM_CONFIG_START):
177                 ret = ecap_pwm_start(p);
178                 break;
180         case BIT(PWM_CONFIG_STOP):
181                 ret = ecap_pwm_stop(p);
182                 break;
183         }
185         return ret;
188 static int ecap_pwm_request(struct pwm_device *p)
190         struct ecap_pwm *ep = to_ecap_pwm(p);
192         p->tick_hz = clk_get_rate(ep->clk);
193         return 0;
196 static int ecap_frequency_transition_cb(struct pwm_device *p)
198         struct ecap_pwm *ep = to_ecap_pwm(p);
199         unsigned long duty_ns;
201         p->tick_hz = clk_get_rate(ep->clk);
202         duty_ns = p->duty_ns;
203         if (pwm_is_running(p)) {
204                 pwm_stop(p);
205                 pwm_set_duty_ns(p, 0);
206                 pwm_set_period_ns(p, p->period_ns);
207                 pwm_set_duty_ns(p, duty_ns);
208                 pwm_start(p);
209         } else {
210                 pwm_set_duty_ns(p, 0);
211                 pwm_set_period_ns(p, p->period_ns);
212                 pwm_set_duty_ns(p, duty_ns);
213         }
214                 return 0;
217 static int ecap_probe(struct platform_device *pdev)
219         struct ecap_pwm *ep = NULL;
220         struct resource *r;
221         int ret = 0;
222         int val;
223         char con_id[PWM_CON_ID_STRING_LENGTH] = "epwmss";
224         struct pwmss_platform_data *pdata = (&pdev->dev)->platform_data;
226         ep = kzalloc(sizeof(*ep), GFP_KERNEL);
228         if (!ep) {
229                 dev_err(&pdev->dev, "failed to allocate memory\n");
230                 ret = -ENOMEM;
231                 goto err_mem_failure;
232         }
234         ep->version = pdata->version;
236         if (ep->version == PWM_VERSION_1) {
237                 sprintf(con_id, "%s%d_%s", con_id, pdev->id, "fck");
238                 ep->clk = clk_get(&pdev->dev, con_id);
239         } else
240                 ep->clk = clk_get(&pdev->dev, "ecap");
242         if (IS_ERR(ep->clk)) {
243                 ret = PTR_ERR(ep->clk);
244                 goto err_clock_failure;
245         }
247         if (ep->version == PWM_VERSION_1) {
248                 down(&pdata->config_semaphore);
249                 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
251                 if (!r) {
252                         dev_err(&pdev->dev, "no memory resource defined\n");
253                         ret = -ENOMEM;
254                         up(&pdata->config_semaphore);
255                         goto err_resource_mem_failure;
256                 }
258                 r = request_mem_region(r->start, resource_size(r), pdev->name);
260                 if (!r) {
262                         if (pdata->config_mem_base) {
263                                 goto set_bit;
264                         } else {
265                                 dev_err(&pdev->dev,
266                                         "failed to request memory resource\n");
267                                 ret = -EBUSY;
268                                 up(&pdata->config_semaphore);
269                                 goto err_request_mem_failure;
270                         }
271                 }
273                 pdata->config_mem_base = ioremap(r->start, resource_size(r));
275                 if (!pdata->config_mem_base) {
277                         dev_err(&pdev->dev, "failed to ioremap() registers\n");
278                         ret = -ENODEV;
279                         up(&pdata->config_semaphore);
280                         goto err_free_mem_config;
281                 }
283 set_bit:
284                 pdata->pwmss_module_usage_count++;
285                 clk_enable(ep->clk);
286                 val = __raw_readw(pdata->config_mem_base + PWMSS_CLKCONFIG);
287                 val |= BIT(ECAP_CLK_EN);
288                 __raw_writew(val, pdata->config_mem_base + PWMSS_CLKCONFIG);
289                 clk_disable(ep->clk);
290                 up(&pdata->config_semaphore);
291         }
293         spin_lock_init(&ep->lock);
294         ep->ops.config = ecap_pwm_config;
295         ep->ops.request = ecap_pwm_request;
296         ep->ops.freq_transition_notifier_cb = ecap_frequency_transition_cb;
298         if (ep->version == PWM_VERSION_1)
299                 r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
300         else
301                 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303         if (!r) {
304                 dev_err(&pdev->dev, "no memory resource defined\n");
305                 ret = -ENODEV;
306                 goto err_resource_mem2_failiure;
307         }
309         r = request_mem_region(r->start, resource_size(r), pdev->name);
310         if (!r) {
311                 dev_err(&pdev->dev, "failed to request memory resource\n");
312                 ret = -EBUSY;
313                 goto err_request_mem2_failure;
314         }
316         ep->mmio_base = ioremap(r->start, resource_size(r));
317         if (!ep->mmio_base) {
318                 dev_err(&pdev->dev, "failed to ioremap() registers\n");
319                 ret = -ENODEV;
320                 goto err_free_mem2;
321         }
323         ep->pwm.ops = &ep->ops;
324         pwm_set_drvdata(&ep->pwm, ep);
325         ret =  pwm_register(&ep->pwm, &pdev->dev, -1);
326         platform_set_drvdata(pdev, ep);
327         return 0;
329 err_free_mem2:
330         release_mem_region(r->start, resource_size(r));
331 err_request_mem2_failure:
332 err_resource_mem2_failiure:
333         if (ep->version == PWM_VERSION_1) {
334                 down(&pdata->config_semaphore);
335                 pdata->pwmss_module_usage_count--;
337                 if (!pdata->pwmss_module_usage_count) {
338                         iounmap(pdata->config_mem_base);
339                         pdata->config_mem_base = NULL;
340                 }
341                 up(&pdata->config_semaphore);
342         }
343 err_free_mem_config:
344         if (ep->version == PWM_VERSION_1) {
345                 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
346                 release_mem_region(r->start, resource_size(r));
347         }
348 err_request_mem_failure:
349 err_resource_mem_failure:
350         clk_put(ep->clk);
351 err_clock_failure:
352         kfree(ep);
353 err_mem_failure:
354         return ret;
357 #ifdef CONFIG_PM
358 static int ecap_suspend(struct platform_device *pdev, pm_message_t state)
360         struct ecap_pwm *ep = platform_get_drvdata(pdev);
362         if (ep->clk->usecount > 0)
363                 clk_disable(ep->clk);
365         return 0;
368 static int ecap_resume(struct platform_device *pdev)
370         struct ecap_pwm *ep = platform_get_drvdata(pdev);
372         clk_enable(ep->clk);
374         return 0;
377 #else
378 #define ecap_suspend NULL
379 #define ecap_resume NULL
380 #endif
382 static int __devexit ecap_remove(struct platform_device *pdev)
384         struct ecap_pwm *ep = platform_get_drvdata(pdev);
385         struct resource *r;
386         struct pwmss_platform_data *pdata;
387         int val;
389         if (ep->version == PWM_VERSION_1) {
390                 pdata = (&pdev->dev)->platform_data;
391                 down(&pdata->config_semaphore);
392                 pdata->pwmss_module_usage_count--;
393                 val = __raw_readw(pdata->config_mem_base + PWMSS_CLKCONFIG);
394                 val &= ~BIT(ECAP_CLK_EN);
395                 __raw_writew(val, pdata->config_mem_base + PWMSS_CLKCONFIG);
397                 if (!pdata->pwmss_module_usage_count) {
398                         iounmap(pdata->config_mem_base);
399                         pdata->config_mem_base = NULL;
400                         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
401                         release_mem_region(r->start, resource_size(r));
402                 }
403                 up(&pdata->config_semaphore);
404         }
406         pwm_unregister(&ep->pwm);
407         iounmap(ep->mmio_base);
409         if (ep->version == PWM_VERSION_1)
410                 r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
411         else
412                 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413         release_mem_region(r->start, resource_size(r));
414         platform_set_drvdata(pdev, NULL);
415         clk_put(ep->clk);
416         kfree(ep);
418         return 0;
421 static struct platform_driver ecap_driver = {
422         .driver = {
423                 .name   = "ecap",
424                 .owner  = THIS_MODULE,
425         },
426         .probe          = ecap_probe,
427         .remove         = __devexit_p(ecap_remove),
428         .suspend        = ecap_suspend,
429         .resume         = ecap_resume,
430 };
432 static int __init ecap_init(void)
434         return platform_driver_register(&ecap_driver);
437 static void __exit ecap_exit(void)
439         platform_driver_unregister(&ecap_driver);
442 module_init(ecap_init);
443 module_exit(ecap_exit);
445 MODULE_AUTHOR("Texas Instruments");
446 MODULE_DESCRIPTION("Driver for Davinci eCAP peripheral");
447 MODULE_LICENSE("GPL v2");
448 MODULE_ALIAS("platform:ecap");