pwm: Add table-based lookup for static mappings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pwm / core.c
bloba2af599e61ae3434b174286df47eb622742724da
1 /*
2 * Generic pwmlib implementation
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
5 * Copyright (C) 2011-2012 Avionic Design GmbH
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.
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.
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.
22 #include <linux/module.h>
23 #include <linux/pwm.h>
24 #include <linux/radix-tree.h>
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>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
33 #define MAX_PWMS 1024
35 static DEFINE_MUTEX(pwm_lookup_lock);
36 static LIST_HEAD(pwm_lookup_list);
37 static DEFINE_MUTEX(pwm_lock);
38 static LIST_HEAD(pwm_chips);
39 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
40 static RADIX_TREE(pwm_tree, GFP_KERNEL);
42 static struct pwm_device *pwm_to_device(unsigned int pwm)
44 return radix_tree_lookup(&pwm_tree, pwm);
47 static int alloc_pwms(int pwm, unsigned int count)
49 unsigned int from = 0;
50 unsigned int start;
52 if (pwm >= MAX_PWMS)
53 return -EINVAL;
55 if (pwm >= 0)
56 from = pwm;
58 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
59 count, 0);
61 if (pwm >= 0 && start != pwm)
62 return -EEXIST;
64 if (start + count > MAX_PWMS)
65 return -ENOSPC;
67 return start;
70 static void free_pwms(struct pwm_chip *chip)
72 unsigned int i;
74 for (i = 0; i < chip->npwm; i++) {
75 struct pwm_device *pwm = &chip->pwms[i];
76 radix_tree_delete(&pwm_tree, pwm->pwm);
79 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
81 kfree(chip->pwms);
82 chip->pwms = NULL;
85 static struct pwm_chip *pwmchip_find_by_name(const char *name)
87 struct pwm_chip *chip;
89 if (!name)
90 return NULL;
92 mutex_lock(&pwm_lock);
94 list_for_each_entry(chip, &pwm_chips, list) {
95 const char *chip_name = dev_name(chip->dev);
97 if (chip_name && strcmp(chip_name, name) == 0) {
98 mutex_unlock(&pwm_lock);
99 return chip;
103 mutex_unlock(&pwm_lock);
105 return NULL;
108 static int pwm_device_request(struct pwm_device *pwm, const char *label)
110 int err;
112 if (test_bit(PWMF_REQUESTED, &pwm->flags))
113 return -EBUSY;
115 if (!try_module_get(pwm->chip->ops->owner))
116 return -ENODEV;
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;
126 set_bit(PWMF_REQUESTED, &pwm->flags);
127 pwm->label = label;
129 return 0;
133 * pwm_set_chip_data() - set private chip data for a PWM
134 * @pwm: PWM device
135 * @data: pointer to chip-specific data
137 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
139 if (!pwm)
140 return -EINVAL;
142 pwm->chip_data = data;
144 return 0;
148 * pwm_get_chip_data() - get private chip data for a PWM
149 * @pwm: PWM device
151 void *pwm_get_chip_data(struct pwm_device *pwm)
153 return pwm ? pwm->chip_data : NULL;
157 * pwmchip_add() - register a new PWM chip
158 * @chip: the PWM chip to add
160 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
161 * will be used.
163 int pwmchip_add(struct pwm_chip *chip)
165 struct pwm_device *pwm;
166 unsigned int i;
167 int ret;
169 if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
170 !chip->ops->enable || !chip->ops->disable)
171 return -EINVAL;
173 mutex_lock(&pwm_lock);
175 ret = alloc_pwms(chip->base, chip->npwm);
176 if (ret < 0)
177 goto out;
179 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
180 if (!chip->pwms) {
181 ret = -ENOMEM;
182 goto out;
185 chip->base = ret;
187 for (i = 0; i < chip->npwm; i++) {
188 pwm = &chip->pwms[i];
190 pwm->chip = chip;
191 pwm->pwm = chip->base + i;
192 pwm->hwpwm = i;
194 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
197 bitmap_set(allocated_pwms, chip->base, chip->npwm);
199 INIT_LIST_HEAD(&chip->list);
200 list_add(&chip->list, &pwm_chips);
202 ret = 0;
204 out:
205 mutex_unlock(&pwm_lock);
206 return ret;
208 EXPORT_SYMBOL_GPL(pwmchip_add);
211 * pwmchip_remove() - remove a PWM chip
212 * @chip: the PWM chip to remove
214 * Removes a PWM chip. This function may return busy if the PWM chip provides
215 * a PWM device that is still requested.
217 int pwmchip_remove(struct pwm_chip *chip)
219 unsigned int i;
220 int ret = 0;
222 mutex_lock(&pwm_lock);
224 for (i = 0; i < chip->npwm; i++) {
225 struct pwm_device *pwm = &chip->pwms[i];
227 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
228 ret = -EBUSY;
229 goto out;
233 list_del_init(&chip->list);
234 free_pwms(chip);
236 out:
237 mutex_unlock(&pwm_lock);
238 return ret;
240 EXPORT_SYMBOL_GPL(pwmchip_remove);
243 * pwm_request() - request a PWM device
244 * @pwm_id: global PWM device index
245 * @label: PWM device label
247 * This function is deprecated, use pwm_get() instead.
249 struct pwm_device *pwm_request(int pwm, const char *label)
251 struct pwm_device *dev;
252 int err;
254 if (pwm < 0 || pwm >= MAX_PWMS)
255 return ERR_PTR(-EINVAL);
257 mutex_lock(&pwm_lock);
259 dev = pwm_to_device(pwm);
260 if (!dev) {
261 dev = ERR_PTR(-EPROBE_DEFER);
262 goto out;
265 err = pwm_device_request(dev, label);
266 if (err < 0)
267 dev = ERR_PTR(err);
269 out:
270 mutex_unlock(&pwm_lock);
272 return dev;
274 EXPORT_SYMBOL_GPL(pwm_request);
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
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.
286 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
287 unsigned int index,
288 const char *label)
290 struct pwm_device *pwm;
291 int err;
293 if (!chip || index >= chip->npwm)
294 return ERR_PTR(-EINVAL);
296 mutex_lock(&pwm_lock);
297 pwm = &chip->pwms[index];
299 err = pwm_device_request(pwm, label);
300 if (err < 0)
301 pwm = ERR_PTR(err);
303 mutex_unlock(&pwm_lock);
304 return pwm;
306 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
309 * pwm_free() - free a PWM device
310 * @pwm: PWM device
312 * This function is deprecated, use pwm_put() instead.
314 void pwm_free(struct pwm_device *pwm)
316 pwm_put(pwm);
318 EXPORT_SYMBOL_GPL(pwm_free);
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
326 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
328 if (!pwm || period_ns == 0 || duty_ns > period_ns)
329 return -EINVAL;
331 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
333 EXPORT_SYMBOL_GPL(pwm_config);
336 * pwm_enable() - start a PWM output toggling
337 * @pwm: PWM device
339 int pwm_enable(struct pwm_device *pwm)
341 if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
342 return pwm->chip->ops->enable(pwm->chip, pwm);
344 return pwm ? 0 : -EINVAL;
346 EXPORT_SYMBOL_GPL(pwm_enable);
349 * pwm_disable() - stop a PWM output toggling
350 * @pwm: PWM device
352 void pwm_disable(struct pwm_device *pwm)
354 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
355 pwm->chip->ops->disable(pwm->chip, pwm);
357 EXPORT_SYMBOL_GPL(pwm_disable);
360 * pwm_add_table() - register PWM device consumers
361 * @table: array of consumers to register
362 * @num: number of consumers in table
364 void __init pwm_add_table(struct pwm_lookup *table, size_t num)
366 mutex_lock(&pwm_lookup_lock);
368 while (num--) {
369 list_add_tail(&table->list, &pwm_lookup_list);
370 table++;
373 mutex_unlock(&pwm_lookup_lock);
377 * pwm_get() - look up and request a PWM device
378 * @dev: device for PWM consumer
379 * @con_id: consumer name
381 * Look up a PWM chip and a relative index via a table supplied by board setup
382 * code (see pwm_add_table()).
384 * Once a PWM chip has been found the specified PWM device will be requested
385 * and is ready to be used.
387 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
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;
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.
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.
407 * The lookup algorithm was shamelessly taken from the clock
408 * framework:
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.
417 mutex_lock(&pwm_lookup_lock);
419 list_for_each_entry(p, &pwm_lookup_list, list) {
420 match = 0;
422 if (p->dev_id) {
423 if (!dev_id || strcmp(p->dev_id, dev_id))
424 continue;
426 match += 2;
429 if (p->con_id) {
430 if (!con_id || strcmp(p->con_id, con_id))
431 continue;
433 match += 1;
436 if (match > best) {
437 chip = pwmchip_find_by_name(p->provider);
438 index = p->index;
440 if (match != 3)
441 best = match;
442 else
443 break;
447 if (chip)
448 pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
450 mutex_unlock(&pwm_lookup_lock);
452 return pwm;
454 EXPORT_SYMBOL_GPL(pwm_get);
457 * pwm_put() - release a PWM device
458 * @pwm: PWM device
460 void pwm_put(struct pwm_device *pwm)
462 if (!pwm)
463 return;
465 mutex_lock(&pwm_lock);
467 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
468 pr_warning("PWM device already freed\n");
469 goto out;
472 if (pwm->chip->ops->free)
473 pwm->chip->ops->free(pwm->chip, pwm);
475 pwm->label = NULL;
477 module_put(pwm->chip->ops->owner);
478 out:
479 mutex_unlock(&pwm_lock);
481 EXPORT_SYMBOL_GPL(pwm_put);
483 #ifdef CONFIG_DEBUG_FS
484 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
486 unsigned int i;
488 for (i = 0; i < chip->npwm; i++) {
489 struct pwm_device *pwm = &chip->pwms[i];
491 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
493 if (test_bit(PWMF_REQUESTED, &pwm->flags))
494 seq_printf(s, " requested");
496 if (test_bit(PWMF_ENABLED, &pwm->flags))
497 seq_printf(s, " enabled");
499 seq_printf(s, "\n");
503 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
505 mutex_lock(&pwm_lock);
506 s->private = "";
508 return seq_list_start(&pwm_chips, *pos);
511 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
513 s->private = "\n";
515 return seq_list_next(v, &pwm_chips, pos);
518 static void pwm_seq_stop(struct seq_file *s, void *v)
520 mutex_unlock(&pwm_lock);
523 static int pwm_seq_show(struct seq_file *s, void *v)
525 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
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" : "");
532 if (chip->ops->dbg_show)
533 chip->ops->dbg_show(chip, s);
534 else
535 pwm_dbg_show(chip, s);
537 return 0;
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,
547 static int pwm_seq_open(struct inode *inode, struct file *file)
549 return seq_open(file, &pwm_seq_ops);
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,
560 static int __init pwm_debugfs_init(void)
562 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
563 &pwm_debugfs_ops);
565 return 0;
568 subsys_initcall(pwm_debugfs_init);
569 #endif /* CONFIG_DEBUG_FS */