]>
Commit | Line | Data |
---|---|---|
0c2498f1 SH |
1 | /* |
2 | * Generic pwmlib implementation | |
3 | * | |
4 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> | |
f051c466 | 5 | * Copyright (C) 2011-2012 Avionic Design GmbH |
0c2498f1 SH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2, or (at your option) | |
10 | * any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; see the file COPYING. If not, write to | |
19 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/pwm.h> | |
f051c466 | 24 | #include <linux/radix-tree.h> |
0c2498f1 SH |
25 | #include <linux/list.h> |
26 | #include <linux/mutex.h> | |
27 | #include <linux/err.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/device.h> | |
62099abf TR |
30 | #include <linux/debugfs.h> |
31 | #include <linux/seq_file.h> | |
0c2498f1 | 32 | |
f051c466 | 33 | #define MAX_PWMS 1024 |
0c2498f1 SH |
34 | |
35 | static DEFINE_MUTEX(pwm_lock); | |
f051c466 TR |
36 | static LIST_HEAD(pwm_chips); |
37 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); | |
38 | static RADIX_TREE(pwm_tree, GFP_KERNEL); | |
0c2498f1 | 39 | |
f051c466 | 40 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
0c2498f1 | 41 | { |
f051c466 TR |
42 | return radix_tree_lookup(&pwm_tree, pwm); |
43 | } | |
44 | ||
45 | static int alloc_pwms(int pwm, unsigned int count) | |
46 | { | |
47 | unsigned int from = 0; | |
48 | unsigned int start; | |
49 | ||
50 | if (pwm >= MAX_PWMS) | |
51 | return -EINVAL; | |
52 | ||
53 | if (pwm >= 0) | |
54 | from = pwm; | |
0c2498f1 | 55 | |
f051c466 TR |
56 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, |
57 | count, 0); | |
58 | ||
59 | if (pwm >= 0 && start != pwm) | |
60 | return -EEXIST; | |
61 | ||
62 | if (start + count > MAX_PWMS) | |
63 | return -ENOSPC; | |
64 | ||
65 | return start; | |
66 | } | |
67 | ||
68 | static void free_pwms(struct pwm_chip *chip) | |
69 | { | |
70 | unsigned int i; | |
71 | ||
72 | for (i = 0; i < chip->npwm; i++) { | |
73 | struct pwm_device *pwm = &chip->pwms[i]; | |
74 | radix_tree_delete(&pwm_tree, pwm->pwm); | |
0c2498f1 SH |
75 | } |
76 | ||
f051c466 TR |
77 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
78 | ||
79 | kfree(chip->pwms); | |
80 | chip->pwms = NULL; | |
81 | } | |
82 | ||
83 | static int pwm_device_request(struct pwm_device *pwm, const char *label) | |
84 | { | |
85 | int err; | |
86 | ||
87 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
88 | return -EBUSY; | |
89 | ||
90 | if (!try_module_get(pwm->chip->ops->owner)) | |
91 | return -ENODEV; | |
92 | ||
93 | if (pwm->chip->ops->request) { | |
94 | err = pwm->chip->ops->request(pwm->chip, pwm); | |
95 | if (err) { | |
96 | module_put(pwm->chip->ops->owner); | |
97 | return err; | |
98 | } | |
99 | } | |
100 | ||
101 | set_bit(PWMF_REQUESTED, &pwm->flags); | |
102 | pwm->label = label; | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | /** | |
108 | * pwm_set_chip_data() - set private chip data for a PWM | |
109 | * @pwm: PWM device | |
110 | * @data: pointer to chip-specific data | |
111 | */ | |
112 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) | |
113 | { | |
114 | if (!pwm) | |
115 | return -EINVAL; | |
116 | ||
117 | pwm->chip_data = data; | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | /** | |
123 | * pwm_get_chip_data() - get private chip data for a PWM | |
124 | * @pwm: PWM device | |
125 | */ | |
126 | void *pwm_get_chip_data(struct pwm_device *pwm) | |
127 | { | |
128 | return pwm ? pwm->chip_data : NULL; | |
0c2498f1 SH |
129 | } |
130 | ||
131 | /** | |
132 | * pwmchip_add() - register a new PWM chip | |
133 | * @chip: the PWM chip to add | |
f051c466 TR |
134 | * |
135 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base | |
136 | * will be used. | |
0c2498f1 SH |
137 | */ |
138 | int pwmchip_add(struct pwm_chip *chip) | |
139 | { | |
140 | struct pwm_device *pwm; | |
f051c466 TR |
141 | unsigned int i; |
142 | int ret; | |
0c2498f1 | 143 | |
f051c466 TR |
144 | if (!chip || !chip->dev || !chip->ops || !chip->ops->config || |
145 | !chip->ops->enable || !chip->ops->disable) | |
146 | return -EINVAL; | |
0c2498f1 SH |
147 | |
148 | mutex_lock(&pwm_lock); | |
149 | ||
f051c466 TR |
150 | ret = alloc_pwms(chip->base, chip->npwm); |
151 | if (ret < 0) | |
152 | goto out; | |
153 | ||
154 | chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL); | |
155 | if (!chip->pwms) { | |
156 | ret = -ENOMEM; | |
0c2498f1 SH |
157 | goto out; |
158 | } | |
159 | ||
f051c466 TR |
160 | chip->base = ret; |
161 | ||
162 | for (i = 0; i < chip->npwm; i++) { | |
163 | pwm = &chip->pwms[i]; | |
164 | ||
165 | pwm->chip = chip; | |
166 | pwm->pwm = chip->base + i; | |
167 | pwm->hwpwm = i; | |
0c2498f1 | 168 | |
f051c466 TR |
169 | radix_tree_insert(&pwm_tree, pwm->pwm, pwm); |
170 | } | |
171 | ||
172 | bitmap_set(allocated_pwms, chip->base, chip->npwm); | |
173 | ||
174 | INIT_LIST_HEAD(&chip->list); | |
175 | list_add(&chip->list, &pwm_chips); | |
0c2498f1 | 176 | |
f051c466 TR |
177 | ret = 0; |
178 | ||
179 | out: | |
180 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
181 | return ret; |
182 | } | |
183 | EXPORT_SYMBOL_GPL(pwmchip_add); | |
184 | ||
185 | /** | |
186 | * pwmchip_remove() - remove a PWM chip | |
187 | * @chip: the PWM chip to remove | |
188 | * | |
189 | * Removes a PWM chip. This function may return busy if the PWM chip provides | |
190 | * a PWM device that is still requested. | |
191 | */ | |
192 | int pwmchip_remove(struct pwm_chip *chip) | |
193 | { | |
f051c466 | 194 | unsigned int i; |
0c2498f1 SH |
195 | int ret = 0; |
196 | ||
197 | mutex_lock(&pwm_lock); | |
198 | ||
f051c466 TR |
199 | for (i = 0; i < chip->npwm; i++) { |
200 | struct pwm_device *pwm = &chip->pwms[i]; | |
0c2498f1 | 201 | |
f051c466 TR |
202 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) { |
203 | ret = -EBUSY; | |
204 | goto out; | |
205 | } | |
0c2498f1 SH |
206 | } |
207 | ||
f051c466 TR |
208 | list_del_init(&chip->list); |
209 | free_pwms(chip); | |
0c2498f1 | 210 | |
0c2498f1 SH |
211 | out: |
212 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
213 | return ret; |
214 | } | |
215 | EXPORT_SYMBOL_GPL(pwmchip_remove); | |
216 | ||
217 | /** | |
218 | * pwm_request() - request a PWM device | |
219 | * @pwm_id: global PWM device index | |
220 | * @label: PWM device label | |
221 | */ | |
f051c466 | 222 | struct pwm_device *pwm_request(int pwm, const char *label) |
0c2498f1 | 223 | { |
f051c466 TR |
224 | struct pwm_device *dev; |
225 | int err; | |
226 | ||
227 | if (pwm < 0 || pwm >= MAX_PWMS) | |
228 | return ERR_PTR(-EINVAL); | |
0c2498f1 SH |
229 | |
230 | mutex_lock(&pwm_lock); | |
231 | ||
f051c466 TR |
232 | dev = pwm_to_device(pwm); |
233 | if (!dev) { | |
234 | dev = ERR_PTR(-EPROBE_DEFER); | |
0c2498f1 SH |
235 | goto out; |
236 | } | |
237 | ||
f051c466 TR |
238 | err = pwm_device_request(dev, label); |
239 | if (err < 0) | |
240 | dev = ERR_PTR(err); | |
0c2498f1 | 241 | |
f051c466 TR |
242 | out: |
243 | mutex_unlock(&pwm_lock); | |
0c2498f1 | 244 | |
f051c466 TR |
245 | return dev; |
246 | } | |
247 | EXPORT_SYMBOL_GPL(pwm_request); | |
0c2498f1 | 248 | |
f051c466 TR |
249 | /** |
250 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip | |
251 | * @chip: PWM chip | |
252 | * @index: per-chip index of the PWM to request | |
253 | * @label: a literal description string of this PWM | |
254 | * | |
255 | * Returns the PWM at the given index of the given PWM chip. A negative error | |
256 | * code is returned if the index is not valid for the specified PWM chip or | |
257 | * if the PWM device cannot be requested. | |
258 | */ | |
259 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
260 | unsigned int index, | |
261 | const char *label) | |
262 | { | |
263 | struct pwm_device *pwm; | |
264 | int err; | |
0c2498f1 | 265 | |
f051c466 TR |
266 | if (!chip || index >= chip->npwm) |
267 | return ERR_PTR(-EINVAL); | |
0c2498f1 | 268 | |
f051c466 TR |
269 | mutex_lock(&pwm_lock); |
270 | pwm = &chip->pwms[index]; | |
0c2498f1 | 271 | |
f051c466 TR |
272 | err = pwm_device_request(pwm, label); |
273 | if (err < 0) | |
274 | pwm = ERR_PTR(err); | |
275 | ||
276 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
277 | return pwm; |
278 | } | |
f051c466 | 279 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
0c2498f1 SH |
280 | |
281 | /** | |
282 | * pwm_free() - free a PWM device | |
283 | * @pwm: PWM device | |
284 | */ | |
285 | void pwm_free(struct pwm_device *pwm) | |
286 | { | |
287 | mutex_lock(&pwm_lock); | |
288 | ||
f051c466 | 289 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { |
0c2498f1 SH |
290 | pr_warning("PWM device already freed\n"); |
291 | goto out; | |
292 | } | |
293 | ||
f051c466 TR |
294 | if (pwm->chip->ops->free) |
295 | pwm->chip->ops->free(pwm->chip, pwm); | |
296 | ||
0c2498f1 SH |
297 | pwm->label = NULL; |
298 | ||
299 | module_put(pwm->chip->ops->owner); | |
300 | out: | |
301 | mutex_unlock(&pwm_lock); | |
302 | } | |
303 | EXPORT_SYMBOL_GPL(pwm_free); | |
304 | ||
305 | /** | |
306 | * pwm_config() - change a PWM device configuration | |
307 | * @pwm: PWM device | |
308 | * @duty_ns: "on" time (in nanoseconds) | |
309 | * @period_ns: duration (in nanoseconds) of one cycle | |
310 | */ | |
311 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) | |
312 | { | |
f051c466 TR |
313 | if (!pwm || period_ns == 0 || duty_ns > period_ns) |
314 | return -EINVAL; | |
315 | ||
316 | return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); | |
0c2498f1 SH |
317 | } |
318 | EXPORT_SYMBOL_GPL(pwm_config); | |
319 | ||
320 | /** | |
321 | * pwm_enable() - start a PWM output toggling | |
322 | * @pwm: PWM device | |
323 | */ | |
324 | int pwm_enable(struct pwm_device *pwm) | |
325 | { | |
f051c466 TR |
326 | if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags)) |
327 | return pwm->chip->ops->enable(pwm->chip, pwm); | |
0c2498f1 | 328 | |
f051c466 | 329 | return pwm ? 0 : -EINVAL; |
0c2498f1 SH |
330 | } |
331 | EXPORT_SYMBOL_GPL(pwm_enable); | |
332 | ||
333 | /** | |
334 | * pwm_disable() - stop a PWM output toggling | |
335 | * @pwm: PWM device | |
336 | */ | |
337 | void pwm_disable(struct pwm_device *pwm) | |
338 | { | |
f051c466 TR |
339 | if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags)) |
340 | pwm->chip->ops->disable(pwm->chip, pwm); | |
0c2498f1 SH |
341 | } |
342 | EXPORT_SYMBOL_GPL(pwm_disable); | |
62099abf TR |
343 | |
344 | #ifdef CONFIG_DEBUG_FS | |
345 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) | |
346 | { | |
347 | unsigned int i; | |
348 | ||
349 | for (i = 0; i < chip->npwm; i++) { | |
350 | struct pwm_device *pwm = &chip->pwms[i]; | |
351 | ||
352 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); | |
353 | ||
354 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
355 | seq_printf(s, " requested"); | |
356 | ||
357 | if (test_bit(PWMF_ENABLED, &pwm->flags)) | |
358 | seq_printf(s, " enabled"); | |
359 | ||
360 | seq_printf(s, "\n"); | |
361 | } | |
362 | } | |
363 | ||
364 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) | |
365 | { | |
366 | mutex_lock(&pwm_lock); | |
367 | s->private = ""; | |
368 | ||
369 | return seq_list_start(&pwm_chips, *pos); | |
370 | } | |
371 | ||
372 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
373 | { | |
374 | s->private = "\n"; | |
375 | ||
376 | return seq_list_next(v, &pwm_chips, pos); | |
377 | } | |
378 | ||
379 | static void pwm_seq_stop(struct seq_file *s, void *v) | |
380 | { | |
381 | mutex_unlock(&pwm_lock); | |
382 | } | |
383 | ||
384 | static int pwm_seq_show(struct seq_file *s, void *v) | |
385 | { | |
386 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); | |
387 | ||
388 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, | |
389 | chip->dev->bus ? chip->dev->bus->name : "no-bus", | |
390 | dev_name(chip->dev), chip->npwm, | |
391 | (chip->npwm != 1) ? "s" : ""); | |
392 | ||
393 | if (chip->ops->dbg_show) | |
394 | chip->ops->dbg_show(chip, s); | |
395 | else | |
396 | pwm_dbg_show(chip, s); | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
401 | static const struct seq_operations pwm_seq_ops = { | |
402 | .start = pwm_seq_start, | |
403 | .next = pwm_seq_next, | |
404 | .stop = pwm_seq_stop, | |
405 | .show = pwm_seq_show, | |
406 | }; | |
407 | ||
408 | static int pwm_seq_open(struct inode *inode, struct file *file) | |
409 | { | |
410 | return seq_open(file, &pwm_seq_ops); | |
411 | } | |
412 | ||
413 | static const struct file_operations pwm_debugfs_ops = { | |
414 | .owner = THIS_MODULE, | |
415 | .open = pwm_seq_open, | |
416 | .read = seq_read, | |
417 | .llseek = seq_lseek, | |
418 | .release = seq_release, | |
419 | }; | |
420 | ||
421 | static int __init pwm_debugfs_init(void) | |
422 | { | |
423 | debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, | |
424 | &pwm_debugfs_ops); | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | subsys_initcall(pwm_debugfs_init); | |
430 | #endif /* CONFIG_DEBUG_FS */ |