sched/preempt, locking: Rework local_bh_{dis,en}able()
[linux-2.6/btrfs-unstable.git] / drivers / pwm / pwm-pxa.c
bloba4d2164aaf55836cc662784f1e9a6f071e6d8be5
1 /*
2 * drivers/pwm/pwm-pxa.c
4 * simple driver for PWM (Pulse Width Modulator) controller
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * 2008-02-13 initial version
11 * eric miao <eric.miao@marvell.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/pwm.h>
23 #include <asm/div64.h>
25 #define HAS_SECONDARY_PWM 0x10
27 static const struct platform_device_id pwm_id_table[] = {
28 /* PWM has_secondary_pwm? */
29 { "pxa25x-pwm", 0 },
30 { "pxa27x-pwm", HAS_SECONDARY_PWM },
31 { "pxa168-pwm", 0 },
32 { "pxa910-pwm", 0 },
33 { },
35 MODULE_DEVICE_TABLE(platform, pwm_id_table);
37 /* PWM registers and bits definitions */
38 #define PWMCR (0x00)
39 #define PWMDCR (0x04)
40 #define PWMPCR (0x08)
42 #define PWMCR_SD (1 << 6)
43 #define PWMDCR_FD (1 << 10)
45 struct pxa_pwm_chip {
46 struct pwm_chip chip;
47 struct device *dev;
49 struct clk *clk;
50 void __iomem *mmio_base;
53 static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
55 return container_of(chip, struct pxa_pwm_chip, chip);
59 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
60 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
62 static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
63 int duty_ns, int period_ns)
65 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
66 unsigned long long c;
67 unsigned long period_cycles, prescale, pv, dc;
68 unsigned long offset;
69 int rc;
71 offset = pwm->hwpwm ? 0x10 : 0;
73 c = clk_get_rate(pc->clk);
74 c = c * period_ns;
75 do_div(c, 1000000000);
76 period_cycles = c;
78 if (period_cycles < 1)
79 period_cycles = 1;
80 prescale = (period_cycles - 1) / 1024;
81 pv = period_cycles / (prescale + 1) - 1;
83 if (prescale > 63)
84 return -EINVAL;
86 if (duty_ns == period_ns)
87 dc = PWMDCR_FD;
88 else
89 dc = (pv + 1) * duty_ns / period_ns;
91 /* NOTE: the clock to PWM has to be enabled first
92 * before writing to the registers
94 rc = clk_prepare_enable(pc->clk);
95 if (rc < 0)
96 return rc;
98 writel(prescale, pc->mmio_base + offset + PWMCR);
99 writel(dc, pc->mmio_base + offset + PWMDCR);
100 writel(pv, pc->mmio_base + offset + PWMPCR);
102 clk_disable_unprepare(pc->clk);
103 return 0;
106 static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
108 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
110 return clk_prepare_enable(pc->clk);
113 static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
115 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
117 clk_disable_unprepare(pc->clk);
120 static struct pwm_ops pxa_pwm_ops = {
121 .config = pxa_pwm_config,
122 .enable = pxa_pwm_enable,
123 .disable = pxa_pwm_disable,
124 .owner = THIS_MODULE,
127 static int pwm_probe(struct platform_device *pdev)
129 const struct platform_device_id *id = platform_get_device_id(pdev);
130 struct pxa_pwm_chip *pwm;
131 struct resource *r;
132 int ret = 0;
134 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
135 if (pwm == NULL) {
136 dev_err(&pdev->dev, "failed to allocate memory\n");
137 return -ENOMEM;
140 pwm->clk = devm_clk_get(&pdev->dev, NULL);
141 if (IS_ERR(pwm->clk))
142 return PTR_ERR(pwm->clk);
144 pwm->chip.dev = &pdev->dev;
145 pwm->chip.ops = &pxa_pwm_ops;
146 pwm->chip.base = -1;
147 pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
149 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
151 if (IS_ERR(pwm->mmio_base))
152 return PTR_ERR(pwm->mmio_base);
154 ret = pwmchip_add(&pwm->chip);
155 if (ret < 0) {
156 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
157 return ret;
160 platform_set_drvdata(pdev, pwm);
161 return 0;
164 static int pwm_remove(struct platform_device *pdev)
166 struct pxa_pwm_chip *chip;
168 chip = platform_get_drvdata(pdev);
169 if (chip == NULL)
170 return -ENODEV;
172 return pwmchip_remove(&chip->chip);
175 static struct platform_driver pwm_driver = {
176 .driver = {
177 .name = "pxa25x-pwm",
178 .owner = THIS_MODULE,
180 .probe = pwm_probe,
181 .remove = pwm_remove,
182 .id_table = pwm_id_table,
185 module_platform_driver(pwm_driver);
187 MODULE_LICENSE("GPL v2");