Avoid beyond bounds copy while caching ACL
[zen-stable.git] / arch / unicore32 / kernel / pwm.c
blob4615d51e3ba608b06220a12d9462faab00deca6a
1 /*
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>
20 #include <linux/io.h>
21 #include <linux/pwm.h>
23 #include <asm/div64.h>
24 #include <mach/hardware.h>
26 struct pwm_device {
27 struct list_head node;
28 struct platform_device *pdev;
30 const char *label;
31 struct clk *clk;
32 int clk_enabled;
34 unsigned int use_count;
35 unsigned int pwm_id;
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)
44 unsigned long long c;
45 unsigned long period_cycles, prescale, pv, dc;
47 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
48 return -EINVAL;
50 c = clk_get_rate(pwm->clk);
51 c = c * period_ns;
52 do_div(c, 1000000000);
53 period_cycles = c;
55 if (period_cycles < 1)
56 period_cycles = 1;
57 prescale = (period_cycles - 1) / 1024;
58 pv = period_cycles / (prescale + 1) - 1;
60 if (prescale > 63)
61 return -EINVAL;
63 if (duty_ns == period_ns)
64 dc = OST_PWMDCCR_FDCYCLE;
65 else
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
71 clk_enable(pwm->clk);
72 OST_PWMPWCR = prescale;
73 OST_PWMDCCR = pv - dc;
74 OST_PWMPCR = pv;
75 clk_disable(pwm->clk);
77 return 0;
79 EXPORT_SYMBOL(pwm_config);
81 int pwm_enable(struct pwm_device *pwm)
83 int rc = 0;
85 if (!pwm->clk_enabled) {
86 rc = clk_enable(pwm->clk);
87 if (!rc)
88 pwm->clk_enabled = 1;
90 return rc;
92 EXPORT_SYMBOL(pwm_enable);
94 void pwm_disable(struct pwm_device *pwm)
96 if (pwm->clk_enabled) {
97 clk_disable(pwm->clk);
98 pwm->clk_enabled = 0;
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;
109 int found = 0;
111 mutex_lock(&pwm_lock);
113 list_for_each_entry(pwm, &pwm_list, node) {
114 if (pwm->pwm_id == pwm_id) {
115 found = 1;
116 break;
120 if (found) {
121 if (pwm->use_count == 0) {
122 pwm->use_count++;
123 pwm->label = label;
124 } else
125 pwm = ERR_PTR(-EBUSY);
126 } else
127 pwm = ERR_PTR(-ENOENT);
129 mutex_unlock(&pwm_lock);
130 return pwm;
132 EXPORT_SYMBOL(pwm_request);
134 void pwm_free(struct pwm_device *pwm)
136 mutex_lock(&pwm_lock);
138 if (pwm->use_count) {
139 pwm->use_count--;
140 pwm->label = NULL;
141 } else
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;
159 struct resource *r;
160 int ret = 0;
162 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
163 if (pwm == NULL) {
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);
171 goto err_free;
173 pwm->clk_enabled = 0;
175 pwm->use_count = 0;
176 pwm->pwm_id = pwm_id;
177 pwm->pdev = pdev;
179 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 if (r == NULL) {
181 dev_err(&pdev->dev, "no memory resource defined\n");
182 ret = -ENODEV;
183 goto err_free_clk;
186 r = request_mem_region(r->start, resource_size(r), pdev->name);
187 if (r == NULL) {
188 dev_err(&pdev->dev, "failed to request memory resource\n");
189 ret = -EBUSY;
190 goto err_free_clk;
193 __add_pwm(pwm);
194 platform_set_drvdata(pdev, pwm);
195 return pwm;
197 err_free_clk:
198 clk_put(pwm->clk);
199 err_free:
200 kfree(pwm);
201 return ERR_PTR(ret);
204 static int __devinit puv3_pwm_probe(struct platform_device *pdev)
206 struct pwm_device *pwm = pwm_probe(pdev, pdev->id, NULL);
208 if (IS_ERR(pwm))
209 return PTR_ERR(pwm);
211 return 0;
214 static int __devexit pwm_remove(struct platform_device *pdev)
216 struct pwm_device *pwm;
217 struct resource *r;
219 pwm = platform_get_drvdata(pdev);
220 if (pwm == NULL)
221 return -ENODEV;
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));
230 clk_put(pwm->clk);
231 kfree(pwm);
232 return 0;
235 static struct platform_driver puv3_pwm_driver = {
236 .driver = {
237 .name = "PKUnity-v3-PWM",
239 .probe = puv3_pwm_probe,
240 .remove = __devexit_p(pwm_remove),
243 static int __init pwm_init(void)
245 int ret = 0;
247 ret = platform_driver_register(&puv3_pwm_driver);
248 if (ret) {
249 printk(KERN_ERR "failed to register puv3_pwm_driver\n");
250 return ret;
253 return ret;
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");