2 * drivers/pwm/pwm-vt8500.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
22 #include <linux/pwm.h>
23 #include <linux/delay.h>
25 #include <asm/div64.h>
27 #define VT8500_NR_PWMS 4
34 #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
36 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
37 static inline void pwm_busy_wait(void __iomem
*reg
, u8 bitmask
)
39 int loops
= msecs_to_loops(10);
40 while ((readb(reg
) & bitmask
) && --loops
)
44 pr_warning("Waiting for status bits 0x%x to clear timed out\n",
48 static int vt8500_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
49 int duty_ns
, int period_ns
)
51 struct vt8500_chip
*vt8500
= to_vt8500_chip(chip
);
53 unsigned long period_cycles
, prescale
, pv
, dc
;
55 c
= 25000000/2; /* wild guess --- need to implement clocks */
57 do_div(c
, 1000000000);
60 if (period_cycles
< 1)
62 prescale
= (period_cycles
- 1) / 4096;
63 pv
= period_cycles
/ (prescale
+ 1) - 1;
70 c
= (unsigned long long)pv
* duty_ns
;
74 pwm_busy_wait(vt8500
->base
+ 0x40 + pwm
->hwpwm
, (1 << 1));
75 writel(prescale
, vt8500
->base
+ 0x4 + (pwm
->hwpwm
<< 4));
77 pwm_busy_wait(vt8500
->base
+ 0x40 + pwm
->hwpwm
, (1 << 2));
78 writel(pv
, vt8500
->base
+ 0x8 + (pwm
->hwpwm
<< 4));
80 pwm_busy_wait(vt8500
->base
+ 0x40 + pwm
->hwpwm
, (1 << 3));
81 writel(dc
, vt8500
->base
+ 0xc + (pwm
->hwpwm
<< 4));
86 static int vt8500_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
88 struct vt8500_chip
*vt8500
= to_vt8500_chip(chip
);
90 pwm_busy_wait(vt8500
->base
+ 0x40 + pwm
->hwpwm
, (1 << 0));
91 writel(5, vt8500
->base
+ (pwm
->hwpwm
<< 4));
95 static void vt8500_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
97 struct vt8500_chip
*vt8500
= to_vt8500_chip(chip
);
99 pwm_busy_wait(vt8500
->base
+ 0x40 + pwm
->hwpwm
, (1 << 0));
100 writel(0, vt8500
->base
+ (pwm
->hwpwm
<< 4));
103 static struct pwm_ops vt8500_pwm_ops
= {
104 .enable
= vt8500_pwm_enable
,
105 .disable
= vt8500_pwm_disable
,
106 .config
= vt8500_pwm_config
,
107 .owner
= THIS_MODULE
,
110 static int __devinit
pwm_probe(struct platform_device
*pdev
)
112 struct vt8500_chip
*chip
;
116 chip
= devm_kzalloc(&pdev
->dev
, sizeof(*chip
), GFP_KERNEL
);
118 dev_err(&pdev
->dev
, "failed to allocate memory\n");
122 chip
->chip
.dev
= &pdev
->dev
;
123 chip
->chip
.ops
= &vt8500_pwm_ops
;
124 chip
->chip
.base
= -1;
125 chip
->chip
.npwm
= VT8500_NR_PWMS
;
127 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
129 dev_err(&pdev
->dev
, "no memory resource defined\n");
133 chip
->base
= devm_request_and_ioremap(&pdev
->dev
, r
);
134 if (chip
->base
== NULL
)
135 return -EADDRNOTAVAIL
;
137 ret
= pwmchip_add(&chip
->chip
);
141 platform_set_drvdata(pdev
, chip
);
145 static int __devexit
pwm_remove(struct platform_device
*pdev
)
147 struct vt8500_chip
*chip
;
149 chip
= platform_get_drvdata(pdev
);
153 return pwmchip_remove(&chip
->chip
);
156 static struct platform_driver pwm_driver
= {
158 .name
= "vt8500-pwm",
159 .owner
= THIS_MODULE
,
162 .remove
= __devexit_p(pwm_remove
),
165 static int __init
pwm_init(void)
167 return platform_driver_register(&pwm_driver
);
169 arch_initcall(pwm_init
);
171 static void __exit
pwm_exit(void)
173 platform_driver_unregister(&pwm_driver
);
175 module_exit(pwm_exit
);
177 MODULE_LICENSE("GPL");