Merge branch 'for-3.6/core' of git://git.kernel.dk/linux-block
[deliverable/linux.git] / drivers / pwm / pwm-vt8500.c
CommitLineData
21f47fbc 1/*
261995dd 2 * drivers/pwm/pwm-vt8500.c
21f47fbc
AC
3 *
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
5 *
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.
9 *
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.
14 */
15
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>
21#include <linux/io.h>
22#include <linux/pwm.h>
23#include <linux/delay.h>
24
25#include <asm/div64.h>
26
27#define VT8500_NR_PWMS 4
28
a245cceb
SH
29struct vt8500_chip {
30 struct pwm_chip chip;
31 void __iomem *base;
21f47fbc
AC
32};
33
a245cceb
SH
34#define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
35
21f47fbc
AC
36#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
37static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
38{
39 int loops = msecs_to_loops(10);
40 while ((readb(reg) & bitmask) && --loops)
41 cpu_relax();
42
43 if (unlikely(!loops))
44 pr_warning("Waiting for status bits 0x%x to clear timed out\n",
45 bitmask);
46}
47
a245cceb
SH
48static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
49 int duty_ns, int period_ns)
21f47fbc 50{
a245cceb 51 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
21f47fbc
AC
52 unsigned long long c;
53 unsigned long period_cycles, prescale, pv, dc;
54
21f47fbc
AC
55 c = 25000000/2; /* wild guess --- need to implement clocks */
56 c = c * period_ns;
57 do_div(c, 1000000000);
58 period_cycles = c;
59
60 if (period_cycles < 1)
61 period_cycles = 1;
62 prescale = (period_cycles - 1) / 4096;
63 pv = period_cycles / (prescale + 1) - 1;
64 if (pv > 4095)
65 pv = 4095;
66
67 if (prescale > 1023)
68 return -EINVAL;
69
70 c = (unsigned long long)pv * duty_ns;
71 do_div(c, period_ns);
72 dc = c;
73
a245cceb
SH
74 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
75 writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
21f47fbc 76
a245cceb
SH
77 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
78 writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
21f47fbc 79
a245cceb
SH
80 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
81 writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
21f47fbc
AC
82
83 return 0;
84}
21f47fbc 85
a245cceb 86static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
21f47fbc 87{
a245cceb 88 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
21f47fbc 89
a245cceb
SH
90 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
91 writel(5, vt8500->base + (pwm->hwpwm << 4));
92 return 0;
21f47fbc 93}
21f47fbc 94
a245cceb 95static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
21f47fbc 96{
a245cceb 97 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
21f47fbc 98
a245cceb
SH
99 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
100 writel(0, vt8500->base + (pwm->hwpwm << 4));
21f47fbc 101}
21f47fbc 102
a245cceb
SH
103static 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,
108};
21f47fbc
AC
109
110static int __devinit pwm_probe(struct platform_device *pdev)
111{
a245cceb 112 struct vt8500_chip *chip;
21f47fbc 113 struct resource *r;
a245cceb 114 int ret;
21f47fbc 115
261995dd 116 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
a245cceb 117 if (chip == NULL) {
21f47fbc
AC
118 dev_err(&pdev->dev, "failed to allocate memory\n");
119 return -ENOMEM;
120 }
121
a245cceb
SH
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;
21f47fbc
AC
126
127 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 if (r == NULL) {
129 dev_err(&pdev->dev, "no memory resource defined\n");
261995dd 130 return -ENODEV;
21f47fbc
AC
131 }
132
261995dd
AL
133 chip->base = devm_request_and_ioremap(&pdev->dev, r);
134 if (chip->base == NULL)
135 return -EADDRNOTAVAIL;
21f47fbc 136
a245cceb
SH
137 ret = pwmchip_add(&chip->chip);
138 if (ret < 0)
261995dd 139 return ret;
21f47fbc 140
a245cceb
SH
141 platform_set_drvdata(pdev, chip);
142 return ret;
21f47fbc
AC
143}
144
145static int __devexit pwm_remove(struct platform_device *pdev)
146{
a245cceb 147 struct vt8500_chip *chip;
21f47fbc 148
a245cceb
SH
149 chip = platform_get_drvdata(pdev);
150 if (chip == NULL)
21f47fbc
AC
151 return -ENODEV;
152
261995dd 153 return pwmchip_remove(&chip->chip);
21f47fbc
AC
154}
155
156static struct platform_driver pwm_driver = {
157 .driver = {
158 .name = "vt8500-pwm",
159 .owner = THIS_MODULE,
160 },
161 .probe = pwm_probe,
162 .remove = __devexit_p(pwm_remove),
163};
164
165static int __init pwm_init(void)
166{
167 return platform_driver_register(&pwm_driver);
168}
169arch_initcall(pwm_init);
170
171static void __exit pwm_exit(void)
172{
173 platform_driver_unregister(&pwm_driver);
174}
175module_exit(pwm_exit);
176
177MODULE_LICENSE("GPL");
This page took 0.116288 seconds and 5 git commands to generate.