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>
21 #include <linux/pwm.h>
23 #include <asm/div64.h>
25 #define HAS_SECONDARY_PWM 0x10
26 #define PWM_ID_BASE(d) ((d) & 0xf)
28 static const struct platform_device_id pwm_id_table
[] = {
29 /* PWM has_secondary_pwm? */
31 { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM
},
36 MODULE_DEVICE_TABLE(platform
, pwm_id_table
);
38 /* PWM registers and bits definitions */
43 #define PWMCR_SD (1 << 6)
44 #define PWMDCR_FD (1 << 10)
52 void __iomem
*mmio_base
;
55 static inline struct pxa_pwm_chip
*to_pxa_pwm_chip(struct pwm_chip
*chip
)
57 return container_of(chip
, struct pxa_pwm_chip
, chip
);
61 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
62 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
64 static int pxa_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
65 int duty_ns
, int period_ns
)
67 struct pxa_pwm_chip
*pc
= to_pxa_pwm_chip(chip
);
69 unsigned long period_cycles
, prescale
, pv
, dc
;
73 if (period_ns
== 0 || duty_ns
> period_ns
)
76 offset
= pwm
->hwpwm
? 0x10 : 0;
78 c
= clk_get_rate(pc
->clk
);
80 do_div(c
, 1000000000);
83 if (period_cycles
< 1)
85 prescale
= (period_cycles
- 1) / 1024;
86 pv
= period_cycles
/ (prescale
+ 1) - 1;
91 if (duty_ns
== period_ns
)
94 dc
= (pv
+ 1) * duty_ns
/ period_ns
;
96 /* NOTE: the clock to PWM has to be enabled first
97 * before writing to the registers
99 rc
= clk_prepare_enable(pc
->clk
);
103 writel(prescale
, pc
->mmio_base
+ offset
+ PWMCR
);
104 writel(dc
, pc
->mmio_base
+ offset
+ PWMDCR
);
105 writel(pv
, pc
->mmio_base
+ offset
+ PWMPCR
);
107 clk_disable_unprepare(pc
->clk
);
111 static int pxa_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
113 struct pxa_pwm_chip
*pc
= to_pxa_pwm_chip(chip
);
116 if (!pc
->clk_enabled
) {
117 rc
= clk_prepare_enable(pc
->clk
);
124 static void pxa_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
126 struct pxa_pwm_chip
*pc
= to_pxa_pwm_chip(chip
);
128 if (pc
->clk_enabled
) {
129 clk_disable_unprepare(pc
->clk
);
134 static struct pwm_ops pxa_pwm_ops
= {
135 .config
= pxa_pwm_config
,
136 .enable
= pxa_pwm_enable
,
137 .disable
= pxa_pwm_disable
,
138 .owner
= THIS_MODULE
,
141 static int __devinit
pwm_probe(struct platform_device
*pdev
)
143 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
144 struct pxa_pwm_chip
*pwm
;
148 pwm
= devm_kzalloc(&pdev
->dev
, sizeof(*pwm
), GFP_KERNEL
);
150 dev_err(&pdev
->dev
, "failed to allocate memory\n");
154 pwm
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
155 if (IS_ERR(pwm
->clk
))
156 return PTR_ERR(pwm
->clk
);
158 pwm
->clk_enabled
= 0;
160 pwm
->chip
.dev
= &pdev
->dev
;
161 pwm
->chip
.ops
= &pxa_pwm_ops
;
163 pwm
->chip
.npwm
= (id
->driver_data
& HAS_SECONDARY_PWM
) ? 2 : 1;
165 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
167 dev_err(&pdev
->dev
, "no memory resource defined\n");
171 pwm
->mmio_base
= devm_request_and_ioremap(&pdev
->dev
, r
);
172 if (pwm
->mmio_base
== NULL
)
173 return -EADDRNOTAVAIL
;
175 ret
= pwmchip_add(&pwm
->chip
);
177 dev_err(&pdev
->dev
, "pwmchip_add() failed: %d\n", ret
);
181 platform_set_drvdata(pdev
, pwm
);
185 static int __devexit
pwm_remove(struct platform_device
*pdev
)
187 struct pxa_pwm_chip
*chip
;
189 chip
= platform_get_drvdata(pdev
);
193 return pwmchip_remove(&chip
->chip
);
196 static struct platform_driver pwm_driver
= {
198 .name
= "pxa25x-pwm",
199 .owner
= THIS_MODULE
,
202 .remove
= __devexit_p(pwm_remove
),
203 .id_table
= pwm_id_table
,
206 static int __init
pwm_init(void)
208 return platform_driver_register(&pwm_driver
);
210 arch_initcall(pwm_init
);
212 static void __exit
pwm_exit(void)
214 platform_driver_unregister(&pwm_driver
);
216 module_exit(pwm_exit
);
218 MODULE_LICENSE("GPL v2");