Commit | Line | Data |
---|---|---|
166091b1 SH |
1 | /* |
2 | * simple driver for PWM (Pulse Width Modulator) controller | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com> | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/platform_device.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/clk.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/pwm.h> | |
c010dba8 HS |
18 | #include <mach/hardware.h> |
19 | ||
20 | ||
21 | /* i.MX1 and i.MX21 share the same PWM function block: */ | |
22 | ||
23 | #define MX1_PWMC 0x00 /* PWM Control Register */ | |
24 | #define MX1_PWMS 0x04 /* PWM Sample Register */ | |
25 | #define MX1_PWMP 0x08 /* PWM Period Register */ | |
26 | ||
27 | ||
28 | /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */ | |
29 | ||
30 | #define MX3_PWMCR 0x00 /* PWM Control Register */ | |
31 | #define MX3_PWMSAR 0x0C /* PWM Sample Register */ | |
32 | #define MX3_PWMPR 0x10 /* PWM Period Register */ | |
33 | #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4) | |
34 | #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16) | |
35 | #define MX3_PWMCR_EN (1 << 0) | |
36 | ||
166091b1 | 37 | |
166091b1 SH |
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_enabled; | |
47 | void __iomem *mmio_base; | |
48 | ||
49 | unsigned int use_count; | |
50 | unsigned int pwm_id; | |
51 | }; | |
52 | ||
53 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) | |
54 | { | |
166091b1 SH |
55 | if (pwm == NULL || period_ns == 0 || duty_ns > period_ns) |
56 | return -EINVAL; | |
57 | ||
c010dba8 HS |
58 | if (cpu_is_mx27() || cpu_is_mx3()) { |
59 | unsigned long long c; | |
60 | unsigned long period_cycles, duty_cycles, prescale; | |
61 | c = clk_get_rate(pwm->clk); | |
62 | c = c * period_ns; | |
63 | do_div(c, 1000000000); | |
64 | period_cycles = c; | |
65 | ||
66 | prescale = period_cycles / 0x10000 + 1; | |
67 | ||
68 | period_cycles /= prescale; | |
69 | c = (unsigned long long)period_cycles * duty_ns; | |
70 | do_div(c, period_ns); | |
71 | duty_cycles = c; | |
72 | ||
73 | writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR); | |
74 | writel(period_cycles, pwm->mmio_base + MX3_PWMPR); | |
5ef881bb | 75 | writel(MX3_PWMCR_PRESCALER(prescale) | |
c010dba8 HS |
76 | MX3_PWMCR_CLKSRC_IPG_HIGH | MX3_PWMCR_EN, |
77 | pwm->mmio_base + MX3_PWMCR); | |
78 | } else if (cpu_is_mx1() || cpu_is_mx21()) { | |
79 | /* The PWM subsystem allows for exact frequencies. However, | |
80 | * I cannot connect a scope on my device to the PWM line and | |
81 | * thus cannot provide the program the PWM controller | |
82 | * exactly. Instead, I'm relying on the fact that the | |
83 | * Bootloader (u-boot or WinCE+haret) has programmed the PWM | |
84 | * function group already. So I'll just modify the PWM sample | |
85 | * register to follow the ratio of duty_ns vs. period_ns | |
86 | * accordingly. | |
87 | * | |
88 | * This is good enought for programming the brightness of | |
89 | * the LCD backlight. | |
90 | * | |
91 | * The real implementation would divide PERCLK[0] first by | |
92 | * both the prescaler (/1 .. /128) and then by CLKSEL | |
93 | * (/2 .. /16). | |
94 | */ | |
95 | u32 max = readl(pwm->mmio_base + MX1_PWMP); | |
96 | u32 p = max * duty_ns / period_ns; | |
97 | writel(max - p, pwm->mmio_base + MX1_PWMS); | |
98 | } else { | |
99 | BUG(); | |
100 | } | |
166091b1 SH |
101 | |
102 | return 0; | |
103 | } | |
104 | EXPORT_SYMBOL(pwm_config); | |
105 | ||
106 | int pwm_enable(struct pwm_device *pwm) | |
107 | { | |
108 | int rc = 0; | |
109 | ||
110 | if (!pwm->clk_enabled) { | |
111 | rc = clk_enable(pwm->clk); | |
112 | if (!rc) | |
113 | pwm->clk_enabled = 1; | |
114 | } | |
115 | return rc; | |
116 | } | |
117 | EXPORT_SYMBOL(pwm_enable); | |
118 | ||
119 | void pwm_disable(struct pwm_device *pwm) | |
120 | { | |
5ef881bb SH |
121 | writel(0, pwm->mmio_base + MX3_PWMCR); |
122 | ||
166091b1 SH |
123 | if (pwm->clk_enabled) { |
124 | clk_disable(pwm->clk); | |
125 | pwm->clk_enabled = 0; | |
126 | } | |
127 | } | |
128 | EXPORT_SYMBOL(pwm_disable); | |
129 | ||
130 | static DEFINE_MUTEX(pwm_lock); | |
131 | static LIST_HEAD(pwm_list); | |
132 | ||
133 | struct pwm_device *pwm_request(int pwm_id, const char *label) | |
134 | { | |
135 | struct pwm_device *pwm; | |
136 | int found = 0; | |
137 | ||
138 | mutex_lock(&pwm_lock); | |
139 | ||
140 | list_for_each_entry(pwm, &pwm_list, node) { | |
141 | if (pwm->pwm_id == pwm_id) { | |
142 | found = 1; | |
143 | break; | |
144 | } | |
145 | } | |
146 | ||
147 | if (found) { | |
148 | if (pwm->use_count == 0) { | |
149 | pwm->use_count++; | |
150 | pwm->label = label; | |
151 | } else | |
152 | pwm = ERR_PTR(-EBUSY); | |
153 | } else | |
154 | pwm = ERR_PTR(-ENOENT); | |
155 | ||
156 | mutex_unlock(&pwm_lock); | |
157 | return pwm; | |
158 | } | |
159 | EXPORT_SYMBOL(pwm_request); | |
160 | ||
161 | void pwm_free(struct pwm_device *pwm) | |
162 | { | |
163 | mutex_lock(&pwm_lock); | |
164 | ||
165 | if (pwm->use_count) { | |
166 | pwm->use_count--; | |
167 | pwm->label = NULL; | |
168 | } else | |
169 | pr_warning("PWM device already freed\n"); | |
170 | ||
171 | mutex_unlock(&pwm_lock); | |
172 | } | |
173 | EXPORT_SYMBOL(pwm_free); | |
174 | ||
175 | static int __devinit mxc_pwm_probe(struct platform_device *pdev) | |
176 | { | |
177 | struct pwm_device *pwm; | |
178 | struct resource *r; | |
179 | int ret = 0; | |
180 | ||
181 | pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); | |
182 | if (pwm == NULL) { | |
183 | dev_err(&pdev->dev, "failed to allocate memory\n"); | |
184 | return -ENOMEM; | |
185 | } | |
186 | ||
187 | pwm->clk = clk_get(&pdev->dev, "pwm"); | |
188 | ||
189 | if (IS_ERR(pwm->clk)) { | |
190 | ret = PTR_ERR(pwm->clk); | |
191 | goto err_free; | |
192 | } | |
193 | ||
194 | pwm->clk_enabled = 0; | |
195 | ||
196 | pwm->use_count = 0; | |
197 | pwm->pwm_id = pdev->id; | |
198 | pwm->pdev = pdev; | |
199 | ||
200 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
201 | if (r == NULL) { | |
202 | dev_err(&pdev->dev, "no memory resource defined\n"); | |
203 | ret = -ENODEV; | |
204 | goto err_free_clk; | |
205 | } | |
206 | ||
207 | r = request_mem_region(r->start, r->end - r->start + 1, pdev->name); | |
208 | if (r == NULL) { | |
209 | dev_err(&pdev->dev, "failed to request memory resource\n"); | |
210 | ret = -EBUSY; | |
211 | goto err_free_clk; | |
212 | } | |
213 | ||
214 | pwm->mmio_base = ioremap(r->start, r->end - r->start + 1); | |
215 | if (pwm->mmio_base == NULL) { | |
216 | dev_err(&pdev->dev, "failed to ioremap() registers\n"); | |
217 | ret = -ENODEV; | |
218 | goto err_free_mem; | |
219 | } | |
220 | ||
221 | mutex_lock(&pwm_lock); | |
222 | list_add_tail(&pwm->node, &pwm_list); | |
223 | mutex_unlock(&pwm_lock); | |
224 | ||
225 | platform_set_drvdata(pdev, pwm); | |
226 | return 0; | |
227 | ||
228 | err_free_mem: | |
229 | release_mem_region(r->start, r->end - r->start + 1); | |
230 | err_free_clk: | |
231 | clk_put(pwm->clk); | |
232 | err_free: | |
233 | kfree(pwm); | |
234 | return ret; | |
235 | } | |
236 | ||
237 | static int __devexit mxc_pwm_remove(struct platform_device *pdev) | |
238 | { | |
239 | struct pwm_device *pwm; | |
240 | struct resource *r; | |
241 | ||
242 | pwm = platform_get_drvdata(pdev); | |
243 | if (pwm == NULL) | |
244 | return -ENODEV; | |
245 | ||
246 | mutex_lock(&pwm_lock); | |
247 | list_del(&pwm->node); | |
248 | mutex_unlock(&pwm_lock); | |
249 | ||
250 | iounmap(pwm->mmio_base); | |
251 | ||
252 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
253 | release_mem_region(r->start, r->end - r->start + 1); | |
254 | ||
255 | clk_put(pwm->clk); | |
256 | ||
257 | kfree(pwm); | |
258 | return 0; | |
259 | } | |
260 | ||
261 | static struct platform_driver mxc_pwm_driver = { | |
262 | .driver = { | |
263 | .name = "mxc_pwm", | |
264 | }, | |
265 | .probe = mxc_pwm_probe, | |
266 | .remove = __devexit_p(mxc_pwm_remove), | |
267 | }; | |
268 | ||
269 | static int __init mxc_pwm_init(void) | |
270 | { | |
271 | return platform_driver_register(&mxc_pwm_driver); | |
272 | } | |
273 | arch_initcall(mxc_pwm_init); | |
274 | ||
275 | static void __exit mxc_pwm_exit(void) | |
276 | { | |
277 | platform_driver_unregister(&mxc_pwm_driver); | |
278 | } | |
279 | module_exit(mxc_pwm_exit); | |
280 | ||
281 | MODULE_LICENSE("GPL v2"); | |
282 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |