Merge branch 'linux-tegra-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / pwm.c
1 /*
2  * arch/arm/mach-tegra/pwm.c
3  *
4  * Tegra pulse-width-modulation controller driver
5  *
6  * Copyright (c) 2010, NVIDIA Corporation.
7  * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22  */
23
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/io.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/platform_device.h>
30 #include <linux/pwm.h>
31 #include <linux/slab.h>
32
33 #define PWM_ENABLE      (1 << 31)
34 #define PWM_DUTY_WIDTH  8
35 #define PWM_DUTY_SHIFT  16
36 #define PWM_SCALE_WIDTH 13
37 #define PWM_SCALE_SHIFT 0
38
39 struct pwm_device {
40         struct list_head        node;
41         struct platform_device  *pdev;
42
43         const char              *label;
44         struct clk              *clk;
45
46         int                     clk_enb;
47         void __iomem            *mmio_base;
48
49         unsigned int            in_use;
50         unsigned int            id;
51 };
52
53 static DEFINE_MUTEX(pwm_lock);
54 static LIST_HEAD(pwm_list);
55
56 static inline int pwm_writel(struct pwm_device *pwm, unsigned long val)
57 {
58         int rc;
59
60         rc = clk_enable(pwm->clk);
61         if (WARN_ON(rc))
62                 return rc;
63         writel(val, pwm->mmio_base);
64         clk_disable(pwm->clk);
65         return 0;
66 }
67
68 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
69 {
70         unsigned long long c;
71         unsigned long rate, hz;
72         u32 val = 0;
73
74         /* convert from duty_ns / period_ns to a fixed number of duty
75          * ticks per (1 << PWM_DUTY_WIDTH) cycles. */
76         c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1);
77         do_div(c, period_ns);
78
79         val = (u32)c << PWM_DUTY_SHIFT;
80
81         /* compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
82          * cycles at the PWM clock rate will take period_ns nanoseconds. */
83         rate = clk_get_rate(pwm->clk) >> PWM_DUTY_WIDTH;
84         hz = 1000000000ul / period_ns;
85
86         rate = (rate + (hz / 2)) / hz;
87
88         if (rate >> PWM_SCALE_WIDTH)
89                 return -EINVAL;
90
91         val |= (rate << PWM_SCALE_SHIFT);
92
93         /* the struct clk may be shared across multiple PWM devices, so
94          * only enable the PWM if this device has been enabled */
95         if (pwm->clk_enb)
96                 val |= PWM_ENABLE;
97
98         return pwm_writel(pwm, val);
99 }
100 EXPORT_SYMBOL(pwm_config);
101
102 int pwm_enable(struct pwm_device *pwm)
103 {
104         int rc = 0;
105
106         mutex_lock(&pwm_lock);
107         if (!pwm->clk_enb) {
108                 rc = clk_enable(pwm->clk);
109                 if (!rc) {
110                         u32 val = readl(pwm->mmio_base);
111                         writel(val | PWM_ENABLE, pwm->mmio_base);
112                         pwm->clk_enb = 1;
113                 }
114         }
115         mutex_unlock(&pwm_lock);
116
117         return rc;
118 }
119 EXPORT_SYMBOL(pwm_enable);
120
121 void pwm_disable(struct pwm_device *pwm)
122 {
123         mutex_lock(&pwm_lock);
124         if (pwm->clk_enb) {
125                 u32 val = readl(pwm->mmio_base);
126                 writel(val & ~PWM_ENABLE, pwm->mmio_base);
127                 clk_disable(pwm->clk);
128                 pwm->clk_enb = 0;
129         } else
130                 dev_warn(&pwm->pdev->dev, "%s called on disabled PWM\n",
131                          __func__);
132         mutex_unlock(&pwm_lock);
133 }
134 EXPORT_SYMBOL(pwm_disable);
135
136 struct pwm_device *pwm_request(int pwm_id, const char *label)
137 {
138         struct pwm_device *pwm;
139         int found = 0;
140
141         mutex_lock(&pwm_lock);
142
143         list_for_each_entry(pwm, &pwm_list, node) {
144                 if (pwm->id == pwm_id) {
145                         found = 1;
146                         break;
147                 }
148         }
149
150         if (found) {
151                 if (!pwm->in_use) {
152                         pwm->in_use = 1;
153                         pwm->label = label;
154                 } else
155                         pwm = ERR_PTR(-EBUSY);
156         } else
157                 pwm = ERR_PTR(-ENOENT);
158
159         mutex_unlock(&pwm_lock);
160
161         return pwm;
162 }
163 EXPORT_SYMBOL(pwm_request);
164
165 void pwm_free(struct pwm_device *pwm)
166 {
167         mutex_lock(&pwm_lock);
168         if (pwm->in_use) {
169                 pwm->in_use = 0;
170                 pwm->label = NULL;
171         } else
172                 dev_warn(&pwm->pdev->dev, "PWM device already freed\n");
173
174         mutex_unlock(&pwm_lock);
175 }
176 EXPORT_SYMBOL(pwm_free);
177
178 static int tegra_pwm_probe(struct platform_device *pdev)
179 {
180         struct pwm_device *pwm;
181         struct resource *r;
182         int ret;
183
184         pwm = kzalloc(sizeof(*pwm), GFP_KERNEL);
185         if (!pwm) {
186                 dev_err(&pdev->dev, "failed to allocate memory\n");
187                 return -ENOMEM;
188         }
189         pwm->clk = clk_get(&pdev->dev, NULL);
190
191         if (IS_ERR(pwm->clk)) {
192                 ret = PTR_ERR(pwm->clk);
193                 goto err_free;
194         }
195
196         pwm->clk_enb = 0;
197         pwm->in_use = 0;
198         pwm->id = pdev->id;
199         pwm->pdev = pdev;
200
201         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
202         if (!r) {
203                 dev_err(&pdev->dev, "no memory resources defined\n");
204                 ret = -ENODEV;
205                 goto err_put_clk;
206         }
207
208         r = request_mem_region(r->start, resource_size(r), pdev->name);
209         if (!r) {
210                 dev_err(&pdev->dev, "failed to request memory\n");
211                 ret = -EBUSY;
212                 goto err_put_clk;
213         }
214
215         pwm->mmio_base = ioremap(r->start, resource_size(r));
216         if (!pwm->mmio_base) {
217                 dev_err(&pdev->dev, "failed to ioremap() region\n");
218                 ret = -ENODEV;
219                 goto err_free_mem;
220         }
221
222         platform_set_drvdata(pdev, pwm);
223
224         mutex_lock(&pwm_lock);
225         list_add_tail(&pwm->node, &pwm_list);
226         mutex_unlock(&pwm_lock);
227
228         return 0;
229
230 err_free_mem:
231         release_mem_region(r->start, resource_size(r));
232 err_put_clk:
233         clk_put(pwm->clk);
234 err_free:
235         kfree(pwm);
236         return ret;
237 }
238
239 static int __devexit tegra_pwm_remove(struct platform_device *pdev)
240 {
241         struct pwm_device *pwm = platform_get_drvdata(pdev);
242         struct resource *r;
243         int rc;
244
245         if (WARN_ON(!pwm))
246                 return -ENODEV;
247
248         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249
250         mutex_lock(&pwm_lock);
251         if (pwm->in_use) {
252                 mutex_unlock(&pwm_lock);
253                 return -EBUSY;
254         }
255         list_del(&pwm->node);
256         mutex_unlock(&pwm_lock);
257
258         rc = pwm_writel(pwm, 0);
259
260         iounmap(pwm->mmio_base);
261         release_mem_region(r->start, resource_size(r));
262
263         if (pwm->clk_enb)
264                 clk_disable(pwm->clk);
265
266         clk_put(pwm->clk);
267
268         kfree(pwm);
269         return rc;
270 }
271
272 static struct platform_driver tegra_pwm_driver = {
273         .driver         = {
274                 .name   = "tegra_pwm",
275         },
276         .probe          = tegra_pwm_probe,
277         .remove         = __devexit_p(tegra_pwm_remove),
278 };
279
280 static int __init tegra_pwm_init(void)
281 {
282         return platform_driver_register(&tegra_pwm_driver);
283 }
284 subsys_initcall(tegra_pwm_init);
285
286 static void __exit tegra_pwm_exit(void)
287 {
288         platform_driver_unregister(&tegra_pwm_driver);
289 }
290 module_exit(tegra_pwm_exit);
291
292 MODULE_LICENSE("GPL v2");
293 MODULE_AUTHOR("NVIDIA Corporation");