2 * linux/arch/unicore32/kernel/pwm.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
7 * Copyright (C) 2001-2010 Guan Xuetao
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
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>
24 #include <mach/hardware.h>
27 struct list_head node
;
28 struct platform_device
*pdev
;
34 unsigned int use_count
;
39 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
40 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
42 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
45 unsigned long period_cycles
, prescale
, pv
, dc
;
47 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
50 c
= clk_get_rate(pwm
->clk
);
52 do_div(c
, 1000000000);
55 if (period_cycles
< 1)
57 prescale
= (period_cycles
- 1) / 1024;
58 pv
= period_cycles
/ (prescale
+ 1) - 1;
63 if (duty_ns
== period_ns
)
64 dc
= OST_PWMDCCR_FDCYCLE
;
66 dc
= (pv
+ 1) * duty_ns
/ period_ns
;
68 /* NOTE: the clock to PWM has to be enabled first
69 * before writing to the registers
72 OST_PWMPWCR
= prescale
;
73 OST_PWMDCCR
= pv
- dc
;
75 clk_disable(pwm
->clk
);
79 EXPORT_SYMBOL(pwm_config
);
81 int pwm_enable(struct pwm_device
*pwm
)
85 if (!pwm
->clk_enabled
) {
86 rc
= clk_enable(pwm
->clk
);
92 EXPORT_SYMBOL(pwm_enable
);
94 void pwm_disable(struct pwm_device
*pwm
)
96 if (pwm
->clk_enabled
) {
97 clk_disable(pwm
->clk
);
101 EXPORT_SYMBOL(pwm_disable
);
103 static DEFINE_MUTEX(pwm_lock
);
104 static LIST_HEAD(pwm_list
);
106 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
108 struct pwm_device
*pwm
;
111 mutex_lock(&pwm_lock
);
113 list_for_each_entry(pwm
, &pwm_list
, node
) {
114 if (pwm
->pwm_id
== pwm_id
) {
121 if (pwm
->use_count
== 0) {
125 pwm
= ERR_PTR(-EBUSY
);
127 pwm
= ERR_PTR(-ENOENT
);
129 mutex_unlock(&pwm_lock
);
132 EXPORT_SYMBOL(pwm_request
);
134 void pwm_free(struct pwm_device
*pwm
)
136 mutex_lock(&pwm_lock
);
138 if (pwm
->use_count
) {
142 pr_warning("PWM device already freed\n");
144 mutex_unlock(&pwm_lock
);
146 EXPORT_SYMBOL(pwm_free
);
148 static inline void __add_pwm(struct pwm_device
*pwm
)
150 mutex_lock(&pwm_lock
);
151 list_add_tail(&pwm
->node
, &pwm_list
);
152 mutex_unlock(&pwm_lock
);
155 static struct pwm_device
*pwm_probe(struct platform_device
*pdev
,
156 unsigned int pwm_id
, struct pwm_device
*parent_pwm
)
158 struct pwm_device
*pwm
;
162 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
164 dev_err(&pdev
->dev
, "failed to allocate memory\n");
165 return ERR_PTR(-ENOMEM
);
168 pwm
->clk
= clk_get(NULL
, "OST_CLK");
169 if (IS_ERR(pwm
->clk
)) {
170 ret
= PTR_ERR(pwm
->clk
);
173 pwm
->clk_enabled
= 0;
176 pwm
->pwm_id
= pwm_id
;
179 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
181 dev_err(&pdev
->dev
, "no memory resource defined\n");
186 r
= request_mem_region(r
->start
, resource_size(r
), pdev
->name
);
188 dev_err(&pdev
->dev
, "failed to request memory resource\n");
194 platform_set_drvdata(pdev
, pwm
);
204 static int __devinit
puv3_pwm_probe(struct platform_device
*pdev
)
206 struct pwm_device
*pwm
= pwm_probe(pdev
, pdev
->id
, NULL
);
214 static int __devexit
pwm_remove(struct platform_device
*pdev
)
216 struct pwm_device
*pwm
;
219 pwm
= platform_get_drvdata(pdev
);
223 mutex_lock(&pwm_lock
);
224 list_del(&pwm
->node
);
225 mutex_unlock(&pwm_lock
);
227 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
228 release_mem_region(r
->start
, resource_size(r
));
235 static struct platform_driver puv3_pwm_driver
= {
237 .name
= "PKUnity-v3-PWM",
239 .probe
= puv3_pwm_probe
,
240 .remove
= __devexit_p(pwm_remove
),
243 static int __init
pwm_init(void)
247 ret
= platform_driver_register(&puv3_pwm_driver
);
249 printk(KERN_ERR
"failed to register puv3_pwm_driver\n");
255 arch_initcall(pwm_init
);
257 static void __exit
pwm_exit(void)
259 platform_driver_unregister(&puv3_pwm_driver
);
261 module_exit(pwm_exit
);
263 MODULE_LICENSE("GPL v2");