2 * simple driver for PWM (Pulse Width Modulator) controller
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/err.h>
15 #include <linux/clk.h>
17 #include <linux/pwm.h>
18 #include <mach/hardware.h>
21 /* i.MX1 and i.MX21 share the same PWM function block: */
23 #define MX1_PWMC 0x00 /* PWM Control Register */
24 #define MX1_PWMS 0x04 /* PWM Sample Register */
25 #define MX1_PWMP 0x08 /* PWM Period Register */
28 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
30 #define MX3_PWMCR 0x00 /* PWM Control Register */
31 #define MX3_PWMSAR 0x0C /* PWM Sample Register */
32 #define MX3_PWMPR 0x10 /* PWM Period Register */
33 #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
34 #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
35 #define MX3_PWMCR_EN (1 << 0)
40 struct list_head node;
41 struct platform_device *pdev;
47 void __iomem *mmio_base;
49 unsigned int use_count;
53 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
55 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
58 if (cpu_is_mx27() || cpu_is_mx3()) {
60 unsigned long period_cycles, duty_cycles, prescale;
61 c = clk_get_rate(pwm->clk);
63 do_div(c, 1000000000);
66 prescale = period_cycles / 0x10000 + 1;
68 period_cycles /= prescale;
69 c = (unsigned long long)period_cycles * duty_ns;
73 writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
74 writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
75 writel(MX3_PWMCR_PRESCALER(prescale - 1) |
76 MX3_PWMCR_CLKSRC_IPG_HIGH | MX3_PWMCR_EN,
77 pwm->mmio_base + MX3_PWMCR);
78 } else if (cpu_is_mx1() || cpu_is_mx21()) {
79 /* The PWM subsystem allows for exact frequencies. However,
80 * I cannot connect a scope on my device to the PWM line and
81 * thus cannot provide the program the PWM controller
82 * exactly. Instead, I'm relying on the fact that the
83 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
84 * function group already. So I'll just modify the PWM sample
85 * register to follow the ratio of duty_ns vs. period_ns
88 * This is good enought for programming the brightness of
91 * The real implementation would divide PERCLK[0] first by
92 * both the prescaler (/1 .. /128) and then by CLKSEL
95 u32 max = readl(pwm->mmio_base + MX1_PWMP);
96 u32 p = max * duty_ns / period_ns;
97 writel(max - p, pwm->mmio_base + MX1_PWMS);
104 EXPORT_SYMBOL(pwm_config);
106 int pwm_enable(struct pwm_device *pwm)
110 if (!pwm->clk_enabled) {
111 rc = clk_enable(pwm->clk);
113 pwm->clk_enabled = 1;
117 EXPORT_SYMBOL(pwm_enable);
119 void pwm_disable(struct pwm_device *pwm)
121 if (pwm->clk_enabled) {
122 clk_disable(pwm->clk);
123 pwm->clk_enabled = 0;
126 EXPORT_SYMBOL(pwm_disable);
128 static DEFINE_MUTEX(pwm_lock);
129 static LIST_HEAD(pwm_list);
131 struct pwm_device *pwm_request(int pwm_id, const char *label)
133 struct pwm_device *pwm;
136 mutex_lock(&pwm_lock);
138 list_for_each_entry(pwm, &pwm_list, node) {
139 if (pwm->pwm_id == pwm_id) {
146 if (pwm->use_count == 0) {
150 pwm = ERR_PTR(-EBUSY);
152 pwm = ERR_PTR(-ENOENT);
154 mutex_unlock(&pwm_lock);
157 EXPORT_SYMBOL(pwm_request);
159 void pwm_free(struct pwm_device *pwm)
161 mutex_lock(&pwm_lock);
163 if (pwm->use_count) {
167 pr_warning("PWM device already freed\n");
169 mutex_unlock(&pwm_lock);
171 EXPORT_SYMBOL(pwm_free);
173 static int __devinit mxc_pwm_probe(struct platform_device *pdev)
175 struct pwm_device *pwm;
179 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
181 dev_err(&pdev->dev, "failed to allocate memory\n");
185 pwm->clk = clk_get(&pdev->dev, "pwm");
187 if (IS_ERR(pwm->clk)) {
188 ret = PTR_ERR(pwm->clk);
192 pwm->clk_enabled = 0;
195 pwm->pwm_id = pdev->id;
198 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 dev_err(&pdev->dev, "no memory resource defined\n");
205 r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
207 dev_err(&pdev->dev, "failed to request memory resource\n");
212 pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
213 if (pwm->mmio_base == NULL) {
214 dev_err(&pdev->dev, "failed to ioremap() registers\n");
219 mutex_lock(&pwm_lock);
220 list_add_tail(&pwm->node, &pwm_list);
221 mutex_unlock(&pwm_lock);
223 platform_set_drvdata(pdev, pwm);
227 release_mem_region(r->start, r->end - r->start + 1);
235 static int __devexit mxc_pwm_remove(struct platform_device *pdev)
237 struct pwm_device *pwm;
240 pwm = platform_get_drvdata(pdev);
244 mutex_lock(&pwm_lock);
245 list_del(&pwm->node);
246 mutex_unlock(&pwm_lock);
248 iounmap(pwm->mmio_base);
250 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
251 release_mem_region(r->start, r->end - r->start + 1);
259 static struct platform_driver mxc_pwm_driver = {
263 .probe = mxc_pwm_probe,
264 .remove = __devexit_p(mxc_pwm_remove),
267 static int __init mxc_pwm_init(void)
269 return platform_driver_register(&mxc_pwm_driver);
271 arch_initcall(mxc_pwm_init);
273 static void __exit mxc_pwm_exit(void)
275 platform_driver_unregister(&mxc_pwm_driver);
277 module_exit(mxc_pwm_exit);
279 MODULE_LICENSE("GPL v2");
280 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");