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)
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>
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;
58 start
= bitmap_find_next_zero_area(allocated_pwms
, MAX_PWMS
, from
,
61 if (pwm
>= 0 && start
!= pwm
)
64 if (start
+ count
> MAX_PWMS
)
70 static void free_pwms(struct pwm_chip
*chip
)
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
);
85 static struct pwm_chip
*pwmchip_find_by_name(const char *name
)
87 struct pwm_chip
*chip
;
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
);
103 mutex_unlock(&pwm_lock
);
108 static int pwm_device_request(struct pwm_device
*pwm
, const char *label
)
112 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
115 if (!try_module_get(pwm
->chip
->ops
->owner
))
118 if (pwm
->chip
->ops
->request
) {
119 err
= pwm
->chip
->ops
->request(pwm
->chip
, pwm
);
121 module_put(pwm
->chip
->ops
->owner
);
126 set_bit(PWMF_REQUESTED
, &pwm
->flags
);
133 * pwm_set_chip_data() - set private chip data for a PWM
135 * @data: pointer to chip-specific data
137 int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
)
142 pwm
->chip_data
= data
;
148 * pwm_get_chip_data() - get private chip data for a PWM
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
163 int pwmchip_add(struct pwm_chip
*chip
)
165 struct pwm_device
*pwm
;
169 if (!chip
|| !chip
->dev
|| !chip
->ops
|| !chip
->ops
->config
||
170 !chip
->ops
->enable
|| !chip
->ops
->disable
)
173 mutex_lock(&pwm_lock
);
175 ret
= alloc_pwms(chip
->base
, chip
->npwm
);
179 chip
->pwms
= kzalloc(chip
->npwm
* sizeof(*pwm
), GFP_KERNEL
);
187 for (i
= 0; i
< chip
->npwm
; i
++) {
188 pwm
= &chip
->pwms
[i
];
191 pwm
->pwm
= chip
->base
+ 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
);
205 mutex_unlock(&pwm_lock
);
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
)
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
)) {
233 list_del_init(&chip
->list
);
237 mutex_unlock(&pwm_lock
);
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
;
254 if (pwm
< 0 || pwm
>= MAX_PWMS
)
255 return ERR_PTR(-EINVAL
);
257 mutex_lock(&pwm_lock
);
259 dev
= pwm_to_device(pwm
);
261 dev
= ERR_PTR(-EPROBE_DEFER
);
265 err
= pwm_device_request(dev
, label
);
270 mutex_unlock(&pwm_lock
);
274 EXPORT_SYMBOL_GPL(pwm_request
);
277 * pwm_request_from_chip() - request a PWM device relative to a 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
,
290 struct pwm_device
*pwm
;
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
);
303 mutex_unlock(&pwm_lock
);
306 EXPORT_SYMBOL_GPL(pwm_request_from_chip
);
309 * pwm_free() - free a PWM device
312 * This function is deprecated, use pwm_put() instead.
314 void pwm_free(struct pwm_device
*pwm
)
318 EXPORT_SYMBOL_GPL(pwm_free
);
321 * pwm_config() - change a PWM device configuration
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
)
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
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
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
);
369 list_add_tail(&table
->list
, &pwm_lookup_list
);
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
;
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
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
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
) {
423 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
430 if (!con_id
|| strcmp(p
->con_id
, con_id
))
437 chip
= pwmchip_find_by_name(p
->provider
);
448 pwm
= pwm_request_from_chip(chip
, index
, con_id
?: dev_id
);
450 mutex_unlock(&pwm_lookup_lock
);
454 EXPORT_SYMBOL_GPL(pwm_get
);
457 * pwm_put() - release a PWM device
460 void pwm_put(struct pwm_device
*pwm
)
465 mutex_lock(&pwm_lock
);
467 if (!test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
468 pr_warning("PWM device already freed\n");
472 if (pwm
->chip
->ops
->free
)
473 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
477 module_put(pwm
->chip
->ops
->owner
);
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
)
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");
503 static void *pwm_seq_start(struct seq_file
*s
, loff_t
*pos
)
505 mutex_lock(&pwm_lock
);
508 return seq_list_start(&pwm_chips
, *pos
);
511 static void *pwm_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
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
);
535 pwm_dbg_show(chip
, s
);
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
,
557 .release
= seq_release
,
560 static int __init
pwm_debugfs_init(void)
562 debugfs_create_file("pwm", S_IFREG
| S_IRUGO
, NULL
, NULL
,
568 subsys_initcall(pwm_debugfs_init
);
569 #endif /* CONFIG_DEBUG_FS */