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 | 34 | |
8138d2dd TR |
35 | static DEFINE_MUTEX(pwm_lookup_lock); |
36 | static LIST_HEAD(pwm_lookup_list); | |
0c2498f1 | 37 | static DEFINE_MUTEX(pwm_lock); |
f051c466 TR |
38 | static LIST_HEAD(pwm_chips); |
39 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); | |
40 | static RADIX_TREE(pwm_tree, GFP_KERNEL); | |
0c2498f1 | 41 | |
f051c466 | 42 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
0c2498f1 | 43 | { |
f051c466 TR |
44 | return radix_tree_lookup(&pwm_tree, pwm); |
45 | } | |
46 | ||
47 | static int alloc_pwms(int pwm, unsigned int count) | |
48 | { | |
49 | unsigned int from = 0; | |
50 | unsigned int start; | |
51 | ||
52 | if (pwm >= MAX_PWMS) | |
53 | return -EINVAL; | |
54 | ||
55 | if (pwm >= 0) | |
56 | from = pwm; | |
0c2498f1 | 57 | |
f051c466 TR |
58 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, |
59 | count, 0); | |
60 | ||
61 | if (pwm >= 0 && start != pwm) | |
62 | return -EEXIST; | |
63 | ||
64 | if (start + count > MAX_PWMS) | |
65 | return -ENOSPC; | |
66 | ||
67 | return start; | |
68 | } | |
69 | ||
70 | static void free_pwms(struct pwm_chip *chip) | |
71 | { | |
72 | unsigned int i; | |
73 | ||
74 | for (i = 0; i < chip->npwm; i++) { | |
75 | struct pwm_device *pwm = &chip->pwms[i]; | |
76 | radix_tree_delete(&pwm_tree, pwm->pwm); | |
0c2498f1 SH |
77 | } |
78 | ||
f051c466 TR |
79 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
80 | ||
81 | kfree(chip->pwms); | |
82 | chip->pwms = NULL; | |
83 | } | |
84 | ||
8138d2dd TR |
85 | static struct pwm_chip *pwmchip_find_by_name(const char *name) |
86 | { | |
87 | struct pwm_chip *chip; | |
88 | ||
89 | if (!name) | |
90 | return NULL; | |
91 | ||
92 | mutex_lock(&pwm_lock); | |
93 | ||
94 | list_for_each_entry(chip, &pwm_chips, list) { | |
95 | const char *chip_name = dev_name(chip->dev); | |
96 | ||
97 | if (chip_name && strcmp(chip_name, name) == 0) { | |
98 | mutex_unlock(&pwm_lock); | |
99 | return chip; | |
100 | } | |
101 | } | |
102 | ||
103 | mutex_unlock(&pwm_lock); | |
104 | ||
105 | return NULL; | |
106 | } | |
107 | ||
f051c466 TR |
108 | static int pwm_device_request(struct pwm_device *pwm, const char *label) |
109 | { | |
110 | int err; | |
111 | ||
112 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
113 | return -EBUSY; | |
114 | ||
115 | if (!try_module_get(pwm->chip->ops->owner)) | |
116 | return -ENODEV; | |
117 | ||
118 | if (pwm->chip->ops->request) { | |
119 | err = pwm->chip->ops->request(pwm->chip, pwm); | |
120 | if (err) { | |
121 | module_put(pwm->chip->ops->owner); | |
122 | return err; | |
123 | } | |
124 | } | |
125 | ||
126 | set_bit(PWMF_REQUESTED, &pwm->flags); | |
127 | pwm->label = label; | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | /** | |
133 | * pwm_set_chip_data() - set private chip data for a PWM | |
134 | * @pwm: PWM device | |
135 | * @data: pointer to chip-specific data | |
136 | */ | |
137 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) | |
138 | { | |
139 | if (!pwm) | |
140 | return -EINVAL; | |
141 | ||
142 | pwm->chip_data = data; | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | /** | |
148 | * pwm_get_chip_data() - get private chip data for a PWM | |
149 | * @pwm: PWM device | |
150 | */ | |
151 | void *pwm_get_chip_data(struct pwm_device *pwm) | |
152 | { | |
153 | return pwm ? pwm->chip_data : NULL; | |
0c2498f1 SH |
154 | } |
155 | ||
156 | /** | |
157 | * pwmchip_add() - register a new PWM chip | |
158 | * @chip: the PWM chip to add | |
f051c466 TR |
159 | * |
160 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base | |
161 | * will be used. | |
0c2498f1 SH |
162 | */ |
163 | int pwmchip_add(struct pwm_chip *chip) | |
164 | { | |
165 | struct pwm_device *pwm; | |
f051c466 TR |
166 | unsigned int i; |
167 | int ret; | |
0c2498f1 | 168 | |
f051c466 TR |
169 | if (!chip || !chip->dev || !chip->ops || !chip->ops->config || |
170 | !chip->ops->enable || !chip->ops->disable) | |
171 | return -EINVAL; | |
0c2498f1 SH |
172 | |
173 | mutex_lock(&pwm_lock); | |
174 | ||
f051c466 TR |
175 | ret = alloc_pwms(chip->base, chip->npwm); |
176 | if (ret < 0) | |
177 | goto out; | |
178 | ||
179 | chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL); | |
180 | if (!chip->pwms) { | |
181 | ret = -ENOMEM; | |
0c2498f1 SH |
182 | goto out; |
183 | } | |
184 | ||
f051c466 TR |
185 | chip->base = ret; |
186 | ||
187 | for (i = 0; i < chip->npwm; i++) { | |
188 | pwm = &chip->pwms[i]; | |
189 | ||
190 | pwm->chip = chip; | |
191 | pwm->pwm = chip->base + i; | |
192 | pwm->hwpwm = i; | |
0c2498f1 | 193 | |
f051c466 TR |
194 | radix_tree_insert(&pwm_tree, pwm->pwm, pwm); |
195 | } | |
196 | ||
197 | bitmap_set(allocated_pwms, chip->base, chip->npwm); | |
198 | ||
199 | INIT_LIST_HEAD(&chip->list); | |
200 | list_add(&chip->list, &pwm_chips); | |
0c2498f1 | 201 | |
f051c466 TR |
202 | ret = 0; |
203 | ||
204 | out: | |
205 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
206 | return ret; |
207 | } | |
208 | EXPORT_SYMBOL_GPL(pwmchip_add); | |
209 | ||
210 | /** | |
211 | * pwmchip_remove() - remove a PWM chip | |
212 | * @chip: the PWM chip to remove | |
213 | * | |
214 | * Removes a PWM chip. This function may return busy if the PWM chip provides | |
215 | * a PWM device that is still requested. | |
216 | */ | |
217 | int pwmchip_remove(struct pwm_chip *chip) | |
218 | { | |
f051c466 | 219 | unsigned int i; |
0c2498f1 SH |
220 | int ret = 0; |
221 | ||
222 | mutex_lock(&pwm_lock); | |
223 | ||
f051c466 TR |
224 | for (i = 0; i < chip->npwm; i++) { |
225 | struct pwm_device *pwm = &chip->pwms[i]; | |
0c2498f1 | 226 | |
f051c466 TR |
227 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) { |
228 | ret = -EBUSY; | |
229 | goto out; | |
230 | } | |
0c2498f1 SH |
231 | } |
232 | ||
f051c466 TR |
233 | list_del_init(&chip->list); |
234 | free_pwms(chip); | |
0c2498f1 | 235 | |
0c2498f1 SH |
236 | out: |
237 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
238 | return ret; |
239 | } | |
240 | EXPORT_SYMBOL_GPL(pwmchip_remove); | |
241 | ||
242 | /** | |
243 | * pwm_request() - request a PWM device | |
244 | * @pwm_id: global PWM device index | |
245 | * @label: PWM device label | |
8138d2dd TR |
246 | * |
247 | * This function is deprecated, use pwm_get() instead. | |
0c2498f1 | 248 | */ |
f051c466 | 249 | struct pwm_device *pwm_request(int pwm, const char *label) |
0c2498f1 | 250 | { |
f051c466 TR |
251 | struct pwm_device *dev; |
252 | int err; | |
253 | ||
254 | if (pwm < 0 || pwm >= MAX_PWMS) | |
255 | return ERR_PTR(-EINVAL); | |
0c2498f1 SH |
256 | |
257 | mutex_lock(&pwm_lock); | |
258 | ||
f051c466 TR |
259 | dev = pwm_to_device(pwm); |
260 | if (!dev) { | |
261 | dev = ERR_PTR(-EPROBE_DEFER); | |
0c2498f1 SH |
262 | goto out; |
263 | } | |
264 | ||
f051c466 TR |
265 | err = pwm_device_request(dev, label); |
266 | if (err < 0) | |
267 | dev = ERR_PTR(err); | |
0c2498f1 | 268 | |
f051c466 TR |
269 | out: |
270 | mutex_unlock(&pwm_lock); | |
0c2498f1 | 271 | |
f051c466 TR |
272 | return dev; |
273 | } | |
274 | EXPORT_SYMBOL_GPL(pwm_request); | |
0c2498f1 | 275 | |
f051c466 TR |
276 | /** |
277 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip | |
278 | * @chip: PWM chip | |
279 | * @index: per-chip index of the PWM to request | |
280 | * @label: a literal description string of this PWM | |
281 | * | |
282 | * Returns the PWM at the given index of the given PWM chip. A negative error | |
283 | * code is returned if the index is not valid for the specified PWM chip or | |
284 | * if the PWM device cannot be requested. | |
285 | */ | |
286 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
287 | unsigned int index, | |
288 | const char *label) | |
289 | { | |
290 | struct pwm_device *pwm; | |
291 | int err; | |
0c2498f1 | 292 | |
f051c466 TR |
293 | if (!chip || index >= chip->npwm) |
294 | return ERR_PTR(-EINVAL); | |
0c2498f1 | 295 | |
f051c466 TR |
296 | mutex_lock(&pwm_lock); |
297 | pwm = &chip->pwms[index]; | |
0c2498f1 | 298 | |
f051c466 TR |
299 | err = pwm_device_request(pwm, label); |
300 | if (err < 0) | |
301 | pwm = ERR_PTR(err); | |
302 | ||
303 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
304 | return pwm; |
305 | } | |
f051c466 | 306 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
0c2498f1 SH |
307 | |
308 | /** | |
309 | * pwm_free() - free a PWM device | |
310 | * @pwm: PWM device | |
8138d2dd TR |
311 | * |
312 | * This function is deprecated, use pwm_put() instead. | |
0c2498f1 SH |
313 | */ |
314 | void pwm_free(struct pwm_device *pwm) | |
315 | { | |
8138d2dd | 316 | pwm_put(pwm); |
0c2498f1 SH |
317 | } |
318 | EXPORT_SYMBOL_GPL(pwm_free); | |
319 | ||
320 | /** | |
321 | * pwm_config() - change a PWM device configuration | |
322 | * @pwm: PWM device | |
323 | * @duty_ns: "on" time (in nanoseconds) | |
324 | * @period_ns: duration (in nanoseconds) of one cycle | |
325 | */ | |
326 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) | |
327 | { | |
f051c466 TR |
328 | if (!pwm || period_ns == 0 || duty_ns > period_ns) |
329 | return -EINVAL; | |
330 | ||
331 | return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); | |
0c2498f1 SH |
332 | } |
333 | EXPORT_SYMBOL_GPL(pwm_config); | |
334 | ||
335 | /** | |
336 | * pwm_enable() - start a PWM output toggling | |
337 | * @pwm: PWM device | |
338 | */ | |
339 | int pwm_enable(struct pwm_device *pwm) | |
340 | { | |
f051c466 TR |
341 | if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags)) |
342 | return pwm->chip->ops->enable(pwm->chip, pwm); | |
0c2498f1 | 343 | |
f051c466 | 344 | return pwm ? 0 : -EINVAL; |
0c2498f1 SH |
345 | } |
346 | EXPORT_SYMBOL_GPL(pwm_enable); | |
347 | ||
348 | /** | |
349 | * pwm_disable() - stop a PWM output toggling | |
350 | * @pwm: PWM device | |
351 | */ | |
352 | void pwm_disable(struct pwm_device *pwm) | |
353 | { | |
f051c466 TR |
354 | if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags)) |
355 | pwm->chip->ops->disable(pwm->chip, pwm); | |
0c2498f1 SH |
356 | } |
357 | EXPORT_SYMBOL_GPL(pwm_disable); | |
62099abf | 358 | |
8138d2dd TR |
359 | /** |
360 | * pwm_add_table() - register PWM device consumers | |
361 | * @table: array of consumers to register | |
362 | * @num: number of consumers in table | |
363 | */ | |
364 | void __init pwm_add_table(struct pwm_lookup *table, size_t num) | |
365 | { | |
366 | mutex_lock(&pwm_lookup_lock); | |
367 | ||
368 | while (num--) { | |
369 | list_add_tail(&table->list, &pwm_lookup_list); | |
370 | table++; | |
371 | } | |
372 | ||
373 | mutex_unlock(&pwm_lookup_lock); | |
374 | } | |
375 | ||
376 | /** | |
377 | * pwm_get() - look up and request a PWM device | |
378 | * @dev: device for PWM consumer | |
379 | * @con_id: consumer name | |
380 | * | |
381 | * Look up a PWM chip and a relative index via a table supplied by board setup | |
382 | * code (see pwm_add_table()). | |
383 | * | |
384 | * Once a PWM chip has been found the specified PWM device will be requested | |
385 | * and is ready to be used. | |
386 | */ | |
387 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) | |
388 | { | |
389 | struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER); | |
390 | const char *dev_id = dev ? dev_name(dev): NULL; | |
391 | struct pwm_chip *chip = NULL; | |
392 | unsigned int best = 0; | |
393 | struct pwm_lookup *p; | |
394 | unsigned int index; | |
395 | unsigned int match; | |
396 | ||
397 | /* | |
398 | * We look up the provider in the static table typically provided by | |
399 | * board setup code. We first try to lookup the consumer device by | |
400 | * name. If the consumer device was passed in as NULL or if no match | |
401 | * was found, we try to find the consumer by directly looking it up | |
402 | * by name. | |
403 | * | |
404 | * If a match is found, the provider PWM chip is looked up by name | |
405 | * and a PWM device is requested using the PWM device per-chip index. | |
406 | * | |
407 | * The lookup algorithm was shamelessly taken from the clock | |
408 | * framework: | |
409 | * | |
410 | * We do slightly fuzzy matching here: | |
411 | * An entry with a NULL ID is assumed to be a wildcard. | |
412 | * If an entry has a device ID, it must match | |
413 | * If an entry has a connection ID, it must match | |
414 | * Then we take the most specific entry - with the following order | |
415 | * of precedence: dev+con > dev only > con only. | |
416 | */ | |
417 | mutex_lock(&pwm_lookup_lock); | |
418 | ||
419 | list_for_each_entry(p, &pwm_lookup_list, list) { | |
420 | match = 0; | |
421 | ||
422 | if (p->dev_id) { | |
423 | if (!dev_id || strcmp(p->dev_id, dev_id)) | |
424 | continue; | |
425 | ||
426 | match += 2; | |
427 | } | |
428 | ||
429 | if (p->con_id) { | |
430 | if (!con_id || strcmp(p->con_id, con_id)) | |
431 | continue; | |
432 | ||
433 | match += 1; | |
434 | } | |
435 | ||
436 | if (match > best) { | |
437 | chip = pwmchip_find_by_name(p->provider); | |
438 | index = p->index; | |
439 | ||
440 | if (match != 3) | |
441 | best = match; | |
442 | else | |
443 | break; | |
444 | } | |
445 | } | |
446 | ||
447 | if (chip) | |
448 | pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id); | |
449 | ||
450 | mutex_unlock(&pwm_lookup_lock); | |
451 | ||
452 | return pwm; | |
453 | } | |
454 | EXPORT_SYMBOL_GPL(pwm_get); | |
455 | ||
456 | /** | |
457 | * pwm_put() - release a PWM device | |
458 | * @pwm: PWM device | |
459 | */ | |
460 | void pwm_put(struct pwm_device *pwm) | |
461 | { | |
462 | if (!pwm) | |
463 | return; | |
464 | ||
465 | mutex_lock(&pwm_lock); | |
466 | ||
467 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { | |
468 | pr_warning("PWM device already freed\n"); | |
469 | goto out; | |
470 | } | |
471 | ||
472 | if (pwm->chip->ops->free) | |
473 | pwm->chip->ops->free(pwm->chip, pwm); | |
474 | ||
475 | pwm->label = NULL; | |
476 | ||
477 | module_put(pwm->chip->ops->owner); | |
478 | out: | |
479 | mutex_unlock(&pwm_lock); | |
480 | } | |
481 | EXPORT_SYMBOL_GPL(pwm_put); | |
482 | ||
62099abf TR |
483 | #ifdef CONFIG_DEBUG_FS |
484 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) | |
485 | { | |
486 | unsigned int i; | |
487 | ||
488 | for (i = 0; i < chip->npwm; i++) { | |
489 | struct pwm_device *pwm = &chip->pwms[i]; | |
490 | ||
491 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); | |
492 | ||
493 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
494 | seq_printf(s, " requested"); | |
495 | ||
496 | if (test_bit(PWMF_ENABLED, &pwm->flags)) | |
497 | seq_printf(s, " enabled"); | |
498 | ||
499 | seq_printf(s, "\n"); | |
500 | } | |
501 | } | |
502 | ||
503 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) | |
504 | { | |
505 | mutex_lock(&pwm_lock); | |
506 | s->private = ""; | |
507 | ||
508 | return seq_list_start(&pwm_chips, *pos); | |
509 | } | |
510 | ||
511 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
512 | { | |
513 | s->private = "\n"; | |
514 | ||
515 | return seq_list_next(v, &pwm_chips, pos); | |
516 | } | |
517 | ||
518 | static void pwm_seq_stop(struct seq_file *s, void *v) | |
519 | { | |
520 | mutex_unlock(&pwm_lock); | |
521 | } | |
522 | ||
523 | static int pwm_seq_show(struct seq_file *s, void *v) | |
524 | { | |
525 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); | |
526 | ||
527 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, | |
528 | chip->dev->bus ? chip->dev->bus->name : "no-bus", | |
529 | dev_name(chip->dev), chip->npwm, | |
530 | (chip->npwm != 1) ? "s" : ""); | |
531 | ||
532 | if (chip->ops->dbg_show) | |
533 | chip->ops->dbg_show(chip, s); | |
534 | else | |
535 | pwm_dbg_show(chip, s); | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | static const struct seq_operations pwm_seq_ops = { | |
541 | .start = pwm_seq_start, | |
542 | .next = pwm_seq_next, | |
543 | .stop = pwm_seq_stop, | |
544 | .show = pwm_seq_show, | |
545 | }; | |
546 | ||
547 | static int pwm_seq_open(struct inode *inode, struct file *file) | |
548 | { | |
549 | return seq_open(file, &pwm_seq_ops); | |
550 | } | |
551 | ||
552 | static const struct file_operations pwm_debugfs_ops = { | |
553 | .owner = THIS_MODULE, | |
554 | .open = pwm_seq_open, | |
555 | .read = seq_read, | |
556 | .llseek = seq_lseek, | |
557 | .release = seq_release, | |
558 | }; | |
559 | ||
560 | static int __init pwm_debugfs_init(void) | |
561 | { | |
562 | debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, | |
563 | &pwm_debugfs_ops); | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | subsys_initcall(pwm_debugfs_init); | |
569 | #endif /* CONFIG_DEBUG_FS */ |