1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic pwmlib implementation
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
6 * Copyright (C) 2011-2012 Avionic Design GmbH
9 #include <linux/module.h>
10 #include <linux/pwm.h>
11 #include <linux/radix-tree.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
20 #include <dt-bindings/pwm/pwm.h>
24 static DEFINE_MUTEX(pwm_lookup_lock
);
25 static LIST_HEAD(pwm_lookup_list
);
26 static DEFINE_MUTEX(pwm_lock
);
27 static LIST_HEAD(pwm_chips
);
28 static DECLARE_BITMAP(allocated_pwms
, MAX_PWMS
);
29 static RADIX_TREE(pwm_tree
, GFP_KERNEL
);
31 static struct pwm_device
*pwm_to_device(unsigned int pwm
)
33 return radix_tree_lookup(&pwm_tree
, pwm
);
36 static int alloc_pwms(int pwm
, unsigned int count
)
38 unsigned int from
= 0;
47 start
= bitmap_find_next_zero_area(allocated_pwms
, MAX_PWMS
, from
,
50 if (pwm
>= 0 && start
!= pwm
)
53 if (start
+ count
> MAX_PWMS
)
59 static void free_pwms(struct pwm_chip
*chip
)
63 for (i
= 0; i
< chip
->npwm
; i
++) {
64 struct pwm_device
*pwm
= &chip
->pwms
[i
];
66 radix_tree_delete(&pwm_tree
, pwm
->pwm
);
69 bitmap_clear(allocated_pwms
, chip
->base
, chip
->npwm
);
75 static struct pwm_chip
*pwmchip_find_by_name(const char *name
)
77 struct pwm_chip
*chip
;
82 mutex_lock(&pwm_lock
);
84 list_for_each_entry(chip
, &pwm_chips
, list
) {
85 const char *chip_name
= dev_name(chip
->dev
);
87 if (chip_name
&& strcmp(chip_name
, name
) == 0) {
88 mutex_unlock(&pwm_lock
);
93 mutex_unlock(&pwm_lock
);
98 static int pwm_device_request(struct pwm_device
*pwm
, const char *label
)
102 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
105 if (!try_module_get(pwm
->chip
->ops
->owner
))
108 if (pwm
->chip
->ops
->request
) {
109 err
= pwm
->chip
->ops
->request(pwm
->chip
, pwm
);
111 module_put(pwm
->chip
->ops
->owner
);
116 set_bit(PWMF_REQUESTED
, &pwm
->flags
);
123 of_pwm_xlate_with_flags(struct pwm_chip
*pc
, const struct of_phandle_args
*args
)
125 struct pwm_device
*pwm
;
127 /* check, whether the driver supports a third cell for flags */
128 if (pc
->of_pwm_n_cells
< 3)
129 return ERR_PTR(-EINVAL
);
131 /* flags in the third cell are optional */
132 if (args
->args_count
< 2)
133 return ERR_PTR(-EINVAL
);
135 if (args
->args
[0] >= pc
->npwm
)
136 return ERR_PTR(-EINVAL
);
138 pwm
= pwm_request_from_chip(pc
, args
->args
[0], NULL
);
142 pwm
->args
.period
= args
->args
[1];
143 pwm
->args
.polarity
= PWM_POLARITY_NORMAL
;
145 if (args
->args_count
> 2 && args
->args
[2] & PWM_POLARITY_INVERTED
)
146 pwm
->args
.polarity
= PWM_POLARITY_INVERSED
;
150 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags
);
152 static struct pwm_device
*
153 of_pwm_simple_xlate(struct pwm_chip
*pc
, const struct of_phandle_args
*args
)
155 struct pwm_device
*pwm
;
157 /* sanity check driver support */
158 if (pc
->of_pwm_n_cells
< 2)
159 return ERR_PTR(-EINVAL
);
161 /* all cells are required */
162 if (args
->args_count
!= pc
->of_pwm_n_cells
)
163 return ERR_PTR(-EINVAL
);
165 if (args
->args
[0] >= pc
->npwm
)
166 return ERR_PTR(-EINVAL
);
168 pwm
= pwm_request_from_chip(pc
, args
->args
[0], NULL
);
172 pwm
->args
.period
= args
->args
[1];
177 static void of_pwmchip_add(struct pwm_chip
*chip
)
179 if (!chip
->dev
|| !chip
->dev
->of_node
)
182 if (!chip
->of_xlate
) {
183 chip
->of_xlate
= of_pwm_simple_xlate
;
184 chip
->of_pwm_n_cells
= 2;
187 of_node_get(chip
->dev
->of_node
);
190 static void of_pwmchip_remove(struct pwm_chip
*chip
)
193 of_node_put(chip
->dev
->of_node
);
197 * pwm_set_chip_data() - set private chip data for a PWM
199 * @data: pointer to chip-specific data
201 * Returns: 0 on success or a negative error code on failure.
203 int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
)
208 pwm
->chip_data
= data
;
212 EXPORT_SYMBOL_GPL(pwm_set_chip_data
);
215 * pwm_get_chip_data() - get private chip data for a PWM
218 * Returns: A pointer to the chip-private data for the PWM device.
220 void *pwm_get_chip_data(struct pwm_device
*pwm
)
222 return pwm
? pwm
->chip_data
: NULL
;
224 EXPORT_SYMBOL_GPL(pwm_get_chip_data
);
226 static bool pwm_ops_check(const struct pwm_ops
*ops
)
228 /* driver supports legacy, non-atomic operation */
229 if (ops
->config
&& ops
->enable
&& ops
->disable
)
232 /* driver supports atomic operation */
240 * pwmchip_add_with_polarity() - register a new PWM chip
241 * @chip: the PWM chip to add
242 * @polarity: initial polarity of PWM channels
244 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
245 * will be used. The initial polarity for all channels is specified by the
246 * @polarity parameter.
248 * Returns: 0 on success or a negative error code on failure.
250 int pwmchip_add_with_polarity(struct pwm_chip
*chip
,
251 enum pwm_polarity polarity
)
253 struct pwm_device
*pwm
;
257 if (!chip
|| !chip
->dev
|| !chip
->ops
|| !chip
->npwm
)
260 if (!pwm_ops_check(chip
->ops
))
263 mutex_lock(&pwm_lock
);
265 ret
= alloc_pwms(chip
->base
, chip
->npwm
);
269 chip
->pwms
= kcalloc(chip
->npwm
, sizeof(*pwm
), GFP_KERNEL
);
277 for (i
= 0; i
< chip
->npwm
; i
++) {
278 pwm
= &chip
->pwms
[i
];
281 pwm
->pwm
= chip
->base
+ i
;
283 pwm
->state
.polarity
= polarity
;
285 if (chip
->ops
->get_state
)
286 chip
->ops
->get_state(chip
, pwm
, &pwm
->state
);
288 radix_tree_insert(&pwm_tree
, pwm
->pwm
, pwm
);
291 bitmap_set(allocated_pwms
, chip
->base
, chip
->npwm
);
293 INIT_LIST_HEAD(&chip
->list
);
294 list_add(&chip
->list
, &pwm_chips
);
298 if (IS_ENABLED(CONFIG_OF
))
299 of_pwmchip_add(chip
);
302 mutex_unlock(&pwm_lock
);
305 pwmchip_sysfs_export(chip
);
309 EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity
);
312 * pwmchip_add() - register a new PWM chip
313 * @chip: the PWM chip to add
315 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
316 * will be used. The initial polarity for all channels is normal.
318 * Returns: 0 on success or a negative error code on failure.
320 int pwmchip_add(struct pwm_chip
*chip
)
322 return pwmchip_add_with_polarity(chip
, PWM_POLARITY_NORMAL
);
324 EXPORT_SYMBOL_GPL(pwmchip_add
);
327 * pwmchip_remove() - remove a PWM chip
328 * @chip: the PWM chip to remove
330 * Removes a PWM chip. This function may return busy if the PWM chip provides
331 * a PWM device that is still requested.
333 * Returns: 0 on success or a negative error code on failure.
335 int pwmchip_remove(struct pwm_chip
*chip
)
340 pwmchip_sysfs_unexport(chip
);
342 mutex_lock(&pwm_lock
);
344 for (i
= 0; i
< chip
->npwm
; i
++) {
345 struct pwm_device
*pwm
= &chip
->pwms
[i
];
347 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
353 list_del_init(&chip
->list
);
355 if (IS_ENABLED(CONFIG_OF
))
356 of_pwmchip_remove(chip
);
361 mutex_unlock(&pwm_lock
);
364 EXPORT_SYMBOL_GPL(pwmchip_remove
);
367 * pwm_request() - request a PWM device
368 * @pwm: global PWM device index
369 * @label: PWM device label
371 * This function is deprecated, use pwm_get() instead.
373 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
376 struct pwm_device
*pwm_request(int pwm
, const char *label
)
378 struct pwm_device
*dev
;
381 if (pwm
< 0 || pwm
>= MAX_PWMS
)
382 return ERR_PTR(-EINVAL
);
384 mutex_lock(&pwm_lock
);
386 dev
= pwm_to_device(pwm
);
388 dev
= ERR_PTR(-EPROBE_DEFER
);
392 err
= pwm_device_request(dev
, label
);
397 mutex_unlock(&pwm_lock
);
401 EXPORT_SYMBOL_GPL(pwm_request
);
404 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
406 * @index: per-chip index of the PWM to request
407 * @label: a literal description string of this PWM
409 * Returns: A pointer to the PWM device at the given index of the given PWM
410 * chip. A negative error code is returned if the index is not valid for the
411 * specified PWM chip or if the PWM device cannot be requested.
413 struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
417 struct pwm_device
*pwm
;
420 if (!chip
|| index
>= chip
->npwm
)
421 return ERR_PTR(-EINVAL
);
423 mutex_lock(&pwm_lock
);
424 pwm
= &chip
->pwms
[index
];
426 err
= pwm_device_request(pwm
, label
);
430 mutex_unlock(&pwm_lock
);
433 EXPORT_SYMBOL_GPL(pwm_request_from_chip
);
436 * pwm_free() - free a PWM device
439 * This function is deprecated, use pwm_put() instead.
441 void pwm_free(struct pwm_device
*pwm
)
445 EXPORT_SYMBOL_GPL(pwm_free
);
448 * pwm_apply_state() - atomically apply a new state to a PWM device
450 * @state: new state to apply. This can be adjusted by the PWM driver
451 * if the requested config is not achievable, for example,
452 * ->duty_cycle and ->period might be approximated.
454 int pwm_apply_state(struct pwm_device
*pwm
, struct pwm_state
*state
)
458 if (!pwm
|| !state
|| !state
->period
||
459 state
->duty_cycle
> state
->period
)
462 if (state
->period
== pwm
->state
.period
&&
463 state
->duty_cycle
== pwm
->state
.duty_cycle
&&
464 state
->polarity
== pwm
->state
.polarity
&&
465 state
->enabled
== pwm
->state
.enabled
)
468 if (pwm
->chip
->ops
->apply
) {
469 err
= pwm
->chip
->ops
->apply(pwm
->chip
, pwm
, state
);
476 * FIXME: restore the initial state in case of error.
478 if (state
->polarity
!= pwm
->state
.polarity
) {
479 if (!pwm
->chip
->ops
->set_polarity
)
483 * Changing the polarity of a running PWM is
484 * only allowed when the PWM driver implements
487 if (pwm
->state
.enabled
) {
488 pwm
->chip
->ops
->disable(pwm
->chip
, pwm
);
489 pwm
->state
.enabled
= false;
492 err
= pwm
->chip
->ops
->set_polarity(pwm
->chip
, pwm
,
497 pwm
->state
.polarity
= state
->polarity
;
500 if (state
->period
!= pwm
->state
.period
||
501 state
->duty_cycle
!= pwm
->state
.duty_cycle
) {
502 err
= pwm
->chip
->ops
->config(pwm
->chip
, pwm
,
508 pwm
->state
.duty_cycle
= state
->duty_cycle
;
509 pwm
->state
.period
= state
->period
;
512 if (state
->enabled
!= pwm
->state
.enabled
) {
513 if (state
->enabled
) {
514 err
= pwm
->chip
->ops
->enable(pwm
->chip
, pwm
);
518 pwm
->chip
->ops
->disable(pwm
->chip
, pwm
);
521 pwm
->state
.enabled
= state
->enabled
;
527 EXPORT_SYMBOL_GPL(pwm_apply_state
);
530 * pwm_capture() - capture and report a PWM signal
532 * @result: structure to fill with capture result
533 * @timeout: time to wait, in milliseconds, before giving up on capture
535 * Returns: 0 on success or a negative error code on failure.
537 int pwm_capture(struct pwm_device
*pwm
, struct pwm_capture
*result
,
538 unsigned long timeout
)
542 if (!pwm
|| !pwm
->chip
->ops
)
545 if (!pwm
->chip
->ops
->capture
)
548 mutex_lock(&pwm_lock
);
549 err
= pwm
->chip
->ops
->capture(pwm
->chip
, pwm
, result
, timeout
);
550 mutex_unlock(&pwm_lock
);
554 EXPORT_SYMBOL_GPL(pwm_capture
);
557 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
560 * This function will adjust the PWM config to the PWM arguments provided
561 * by the DT or PWM lookup table. This is particularly useful to adapt
562 * the bootloader config to the Linux one.
564 int pwm_adjust_config(struct pwm_device
*pwm
)
566 struct pwm_state state
;
567 struct pwm_args pargs
;
569 pwm_get_args(pwm
, &pargs
);
570 pwm_get_state(pwm
, &state
);
573 * If the current period is zero it means that either the PWM driver
574 * does not support initial state retrieval or the PWM has not yet
577 * In either case, we setup the new period and polarity, and assign a
581 state
.duty_cycle
= 0;
582 state
.period
= pargs
.period
;
583 state
.polarity
= pargs
.polarity
;
585 return pwm_apply_state(pwm
, &state
);
589 * Adjust the PWM duty cycle/period based on the period value provided
592 if (pargs
.period
!= state
.period
) {
593 u64 dutycycle
= (u64
)state
.duty_cycle
* pargs
.period
;
595 do_div(dutycycle
, state
.period
);
596 state
.duty_cycle
= dutycycle
;
597 state
.period
= pargs
.period
;
601 * If the polarity changed, we should also change the duty cycle.
603 if (pargs
.polarity
!= state
.polarity
) {
604 state
.polarity
= pargs
.polarity
;
605 state
.duty_cycle
= state
.period
- state
.duty_cycle
;
608 return pwm_apply_state(pwm
, &state
);
610 EXPORT_SYMBOL_GPL(pwm_adjust_config
);
612 static struct pwm_chip
*of_node_to_pwmchip(struct device_node
*np
)
614 struct pwm_chip
*chip
;
616 mutex_lock(&pwm_lock
);
618 list_for_each_entry(chip
, &pwm_chips
, list
)
619 if (chip
->dev
&& chip
->dev
->of_node
== np
) {
620 mutex_unlock(&pwm_lock
);
624 mutex_unlock(&pwm_lock
);
626 return ERR_PTR(-EPROBE_DEFER
);
630 * of_pwm_get() - request a PWM via the PWM framework
631 * @np: device node to get the PWM from
632 * @con_id: consumer name
634 * Returns the PWM device parsed from the phandle and index specified in the
635 * "pwms" property of a device tree node or a negative error-code on failure.
636 * Values parsed from the device tree are stored in the returned PWM device
639 * If con_id is NULL, the first PWM device listed in the "pwms" property will
640 * be requested. Otherwise the "pwm-names" property is used to do a reverse
641 * lookup of the PWM index. This also means that the "pwm-names" property
642 * becomes mandatory for devices that look up the PWM device via the con_id
645 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
646 * error code on failure.
648 struct pwm_device
*of_pwm_get(struct device_node
*np
, const char *con_id
)
650 struct pwm_device
*pwm
= NULL
;
651 struct of_phandle_args args
;
657 index
= of_property_match_string(np
, "pwm-names", con_id
);
659 return ERR_PTR(index
);
662 err
= of_parse_phandle_with_args(np
, "pwms", "#pwm-cells", index
,
665 pr_err("%s(): can't parse \"pwms\" property\n", __func__
);
669 pc
= of_node_to_pwmchip(args
.np
);
671 if (PTR_ERR(pc
) != -EPROBE_DEFER
)
672 pr_err("%s(): PWM chip not found\n", __func__
);
678 pwm
= pc
->of_xlate(pc
, &args
);
683 * If a consumer name was not given, try to look it up from the
684 * "pwm-names" property if it exists. Otherwise use the name of
685 * the user device node.
688 err
= of_property_read_string_index(np
, "pwm-names", index
,
697 of_node_put(args
.np
);
701 EXPORT_SYMBOL_GPL(of_pwm_get
);
704 * pwm_add_table() - register PWM device consumers
705 * @table: array of consumers to register
706 * @num: number of consumers in table
708 void pwm_add_table(struct pwm_lookup
*table
, size_t num
)
710 mutex_lock(&pwm_lookup_lock
);
713 list_add_tail(&table
->list
, &pwm_lookup_list
);
717 mutex_unlock(&pwm_lookup_lock
);
721 * pwm_remove_table() - unregister PWM device consumers
722 * @table: array of consumers to unregister
723 * @num: number of consumers in table
725 void pwm_remove_table(struct pwm_lookup
*table
, size_t num
)
727 mutex_lock(&pwm_lookup_lock
);
730 list_del(&table
->list
);
734 mutex_unlock(&pwm_lookup_lock
);
738 * pwm_get() - look up and request a PWM device
739 * @dev: device for PWM consumer
740 * @con_id: consumer name
742 * Lookup is first attempted using DT. If the device was not instantiated from
743 * a device tree, a PWM chip and a relative index is looked up via a table
744 * supplied by board setup code (see pwm_add_table()).
746 * Once a PWM chip has been found the specified PWM device will be requested
747 * and is ready to be used.
749 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
750 * error code on failure.
752 struct pwm_device
*pwm_get(struct device
*dev
, const char *con_id
)
754 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
755 struct pwm_device
*pwm
;
756 struct pwm_chip
*chip
;
757 unsigned int best
= 0;
758 struct pwm_lookup
*p
, *chosen
= NULL
;
762 /* look up via DT first */
763 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
)
764 return of_pwm_get(dev
->of_node
, con_id
);
767 * We look up the provider in the static table typically provided by
768 * board setup code. We first try to lookup the consumer device by
769 * name. If the consumer device was passed in as NULL or if no match
770 * was found, we try to find the consumer by directly looking it up
773 * If a match is found, the provider PWM chip is looked up by name
774 * and a PWM device is requested using the PWM device per-chip index.
776 * The lookup algorithm was shamelessly taken from the clock
779 * We do slightly fuzzy matching here:
780 * An entry with a NULL ID is assumed to be a wildcard.
781 * If an entry has a device ID, it must match
782 * If an entry has a connection ID, it must match
783 * Then we take the most specific entry - with the following order
784 * of precedence: dev+con > dev only > con only.
786 mutex_lock(&pwm_lookup_lock
);
788 list_for_each_entry(p
, &pwm_lookup_list
, list
) {
792 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
799 if (!con_id
|| strcmp(p
->con_id
, con_id
))
815 mutex_unlock(&pwm_lookup_lock
);
818 return ERR_PTR(-ENODEV
);
820 chip
= pwmchip_find_by_name(chosen
->provider
);
823 * If the lookup entry specifies a module, load the module and retry
824 * the PWM chip lookup. This can be used to work around driver load
825 * ordering issues if driver's can't be made to properly support the
826 * deferred probe mechanism.
828 if (!chip
&& chosen
->module
) {
829 err
= request_module(chosen
->module
);
831 chip
= pwmchip_find_by_name(chosen
->provider
);
835 return ERR_PTR(-EPROBE_DEFER
);
837 pwm
= pwm_request_from_chip(chip
, chosen
->index
, con_id
?: dev_id
);
841 pwm
->args
.period
= chosen
->period
;
842 pwm
->args
.polarity
= chosen
->polarity
;
846 EXPORT_SYMBOL_GPL(pwm_get
);
849 * pwm_put() - release a PWM device
852 void pwm_put(struct pwm_device
*pwm
)
857 mutex_lock(&pwm_lock
);
859 if (!test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
860 pr_warn("PWM device already freed\n");
864 if (pwm
->chip
->ops
->free
)
865 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
867 pwm_set_chip_data(pwm
, NULL
);
870 module_put(pwm
->chip
->ops
->owner
);
872 mutex_unlock(&pwm_lock
);
874 EXPORT_SYMBOL_GPL(pwm_put
);
876 static void devm_pwm_release(struct device
*dev
, void *res
)
878 pwm_put(*(struct pwm_device
**)res
);
882 * devm_pwm_get() - resource managed pwm_get()
883 * @dev: device for PWM consumer
884 * @con_id: consumer name
886 * This function performs like pwm_get() but the acquired PWM device will
887 * automatically be released on driver detach.
889 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
890 * error code on failure.
892 struct pwm_device
*devm_pwm_get(struct device
*dev
, const char *con_id
)
894 struct pwm_device
**ptr
, *pwm
;
896 ptr
= devres_alloc(devm_pwm_release
, sizeof(*ptr
), GFP_KERNEL
);
898 return ERR_PTR(-ENOMEM
);
900 pwm
= pwm_get(dev
, con_id
);
903 devres_add(dev
, ptr
);
910 EXPORT_SYMBOL_GPL(devm_pwm_get
);
913 * devm_of_pwm_get() - resource managed of_pwm_get()
914 * @dev: device for PWM consumer
915 * @np: device node to get the PWM from
916 * @con_id: consumer name
918 * This function performs like of_pwm_get() but the acquired PWM device will
919 * automatically be released on driver detach.
921 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
922 * error code on failure.
924 struct pwm_device
*devm_of_pwm_get(struct device
*dev
, struct device_node
*np
,
927 struct pwm_device
**ptr
, *pwm
;
929 ptr
= devres_alloc(devm_pwm_release
, sizeof(*ptr
), GFP_KERNEL
);
931 return ERR_PTR(-ENOMEM
);
933 pwm
= of_pwm_get(np
, con_id
);
936 devres_add(dev
, ptr
);
943 EXPORT_SYMBOL_GPL(devm_of_pwm_get
);
945 static int devm_pwm_match(struct device
*dev
, void *res
, void *data
)
947 struct pwm_device
**p
= res
;
949 if (WARN_ON(!p
|| !*p
))
956 * devm_pwm_put() - resource managed pwm_put()
957 * @dev: device for PWM consumer
960 * Release a PWM previously allocated using devm_pwm_get(). Calling this
961 * function is usually not needed because devm-allocated resources are
962 * automatically released on driver detach.
964 void devm_pwm_put(struct device
*dev
, struct pwm_device
*pwm
)
966 WARN_ON(devres_release(dev
, devm_pwm_release
, devm_pwm_match
, pwm
));
968 EXPORT_SYMBOL_GPL(devm_pwm_put
);
970 #ifdef CONFIG_DEBUG_FS
971 static void pwm_dbg_show(struct pwm_chip
*chip
, struct seq_file
*s
)
975 for (i
= 0; i
< chip
->npwm
; i
++) {
976 struct pwm_device
*pwm
= &chip
->pwms
[i
];
977 struct pwm_state state
;
979 pwm_get_state(pwm
, &state
);
981 seq_printf(s
, " pwm-%-3d (%-20.20s):", i
, pwm
->label
);
983 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
984 seq_puts(s
, " requested");
987 seq_puts(s
, " enabled");
989 seq_printf(s
, " period: %u ns", state
.period
);
990 seq_printf(s
, " duty: %u ns", state
.duty_cycle
);
991 seq_printf(s
, " polarity: %s",
992 state
.polarity
? "inverse" : "normal");
998 static void *pwm_seq_start(struct seq_file
*s
, loff_t
*pos
)
1000 mutex_lock(&pwm_lock
);
1003 return seq_list_start(&pwm_chips
, *pos
);
1006 static void *pwm_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1010 return seq_list_next(v
, &pwm_chips
, pos
);
1013 static void pwm_seq_stop(struct seq_file
*s
, void *v
)
1015 mutex_unlock(&pwm_lock
);
1018 static int pwm_seq_show(struct seq_file
*s
, void *v
)
1020 struct pwm_chip
*chip
= list_entry(v
, struct pwm_chip
, list
);
1022 seq_printf(s
, "%s%s/%s, %d PWM device%s\n", (char *)s
->private,
1023 chip
->dev
->bus
? chip
->dev
->bus
->name
: "no-bus",
1024 dev_name(chip
->dev
), chip
->npwm
,
1025 (chip
->npwm
!= 1) ? "s" : "");
1027 pwm_dbg_show(chip
, s
);
1032 static const struct seq_operations pwm_seq_ops
= {
1033 .start
= pwm_seq_start
,
1034 .next
= pwm_seq_next
,
1035 .stop
= pwm_seq_stop
,
1036 .show
= pwm_seq_show
,
1039 static int pwm_seq_open(struct inode
*inode
, struct file
*file
)
1041 return seq_open(file
, &pwm_seq_ops
);
1044 static const struct file_operations pwm_debugfs_ops
= {
1045 .owner
= THIS_MODULE
,
1046 .open
= pwm_seq_open
,
1048 .llseek
= seq_lseek
,
1049 .release
= seq_release
,
1052 static int __init
pwm_debugfs_init(void)
1054 debugfs_create_file("pwm", S_IFREG
| S_IRUGO
, NULL
, NULL
,
1059 subsys_initcall(pwm_debugfs_init
);
1060 #endif /* CONFIG_DEBUG_FS */