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
27 static const struct platform_device_id pwm_id_table
[] = {
28 /* PWM has_secondary_pwm? */
30 { "pxa27x-pwm", HAS_SECONDARY_PWM
},
35 MODULE_DEVICE_TABLE(platform
, pwm_id_table
);
37 /* PWM registers and bits definitions */
42 #define PWMCR_SD (1 << 6)
43 #define PWMDCR_FD (1 << 10)
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
);
67 unsigned long period_cycles
, prescale
, pv
, dc
;
71 offset
= pwm
->hwpwm
? 0x10 : 0;
73 c
= clk_get_rate(pc
->clk
);
75 do_div(c
, 1000000000);
78 if (period_cycles
< 1)
80 prescale
= (period_cycles
- 1) / 1024;
81 pv
= period_cycles
/ (prescale
+ 1) - 1;
86 if (duty_ns
== period_ns
)
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
);
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
);
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
;
134 pwm
= devm_kzalloc(&pdev
->dev
, sizeof(*pwm
), GFP_KERNEL
);
136 dev_err(&pdev
->dev
, "failed to allocate memory\n");
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
;
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
);
156 dev_err(&pdev
->dev
, "pwmchip_add() failed: %d\n", ret
);
160 platform_set_drvdata(pdev
, pwm
);
164 static int pwm_remove(struct platform_device
*pdev
)
166 struct pxa_pwm_chip
*chip
;
168 chip
= platform_get_drvdata(pdev
);
172 return pwmchip_remove(&chip
->chip
);
175 static struct platform_driver pwm_driver
= {
177 .name
= "pxa25x-pwm",
178 .owner
= THIS_MODULE
,
181 .remove
= pwm_remove
,
182 .id_table
= pwm_id_table
,
185 module_platform_driver(pwm_driver
);
187 MODULE_LICENSE("GPL v2");