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/acpi.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
13 #include <linux/pwm.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
22 #include <dt-bindings/pwm/pwm.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/pwm.h>
27 static DEFINE_MUTEX(pwm_lookup_lock);
28 static LIST_HEAD(pwm_lookup_list);
30 /* protects access to pwm_chips */
31 static DEFINE_MUTEX(pwm_lock);
33 static DEFINE_IDR(pwm_chips);
35 static struct pwm_chip *pwmchip_find_by_name(const char *name)
37 struct pwm_chip *chip;
38 unsigned long id, tmp;
43 mutex_lock(&pwm_lock);
45 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
46 const char *chip_name = dev_name(chip->dev);
48 if (chip_name && strcmp(chip_name, name) == 0) {
49 mutex_unlock(&pwm_lock);
54 mutex_unlock(&pwm_lock);
59 static int pwm_device_request(struct pwm_device *pwm, const char *label)
63 if (test_bit(PWMF_REQUESTED, &pwm->flags))
66 if (!try_module_get(pwm->chip->owner))
69 if (pwm->chip->ops->request) {
70 err = pwm->chip->ops->request(pwm->chip, pwm);
72 module_put(pwm->chip->owner);
77 if (pwm->chip->ops->get_state) {
79 * Zero-initialize state because most drivers are unaware of
80 * .usage_power. The other members of state are supposed to be
81 * set by lowlevel drivers. We still initialize the whole
82 * structure for simplicity even though this might paper over
83 * faulty implementations of .get_state().
85 struct pwm_state state = { 0, };
87 err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
88 trace_pwm_get(pwm, &state, err);
93 if (IS_ENABLED(CONFIG_PWM_DEBUG))
94 pwm->last = pwm->state;
97 set_bit(PWMF_REQUESTED, &pwm->flags);
104 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
106 struct pwm_device *pwm;
108 if (chip->of_pwm_n_cells < 2)
109 return ERR_PTR(-EINVAL);
111 /* flags in the third cell are optional */
112 if (args->args_count < 2)
113 return ERR_PTR(-EINVAL);
115 if (args->args[0] >= chip->npwm)
116 return ERR_PTR(-EINVAL);
118 pwm = pwm_request_from_chip(chip, args->args[0], NULL);
122 pwm->args.period = args->args[1];
123 pwm->args.polarity = PWM_POLARITY_NORMAL;
125 if (chip->of_pwm_n_cells >= 3) {
126 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
127 pwm->args.polarity = PWM_POLARITY_INVERSED;
132 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
135 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
137 struct pwm_device *pwm;
139 if (chip->of_pwm_n_cells < 1)
140 return ERR_PTR(-EINVAL);
142 /* validate that one cell is specified, optionally with flags */
143 if (args->args_count != 1 && args->args_count != 2)
144 return ERR_PTR(-EINVAL);
146 pwm = pwm_request_from_chip(chip, 0, NULL);
150 pwm->args.period = args->args[0];
151 pwm->args.polarity = PWM_POLARITY_NORMAL;
153 if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED)
154 pwm->args.polarity = PWM_POLARITY_INVERSED;
158 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
160 static void of_pwmchip_add(struct pwm_chip *chip)
162 if (!chip->dev || !chip->dev->of_node)
165 if (!chip->of_xlate) {
168 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells",
172 chip->of_xlate = of_pwm_xlate_with_flags;
173 chip->of_pwm_n_cells = pwm_cells;
176 of_node_get(chip->dev->of_node);
179 static void of_pwmchip_remove(struct pwm_chip *chip)
182 of_node_put(chip->dev->of_node);
185 static bool pwm_ops_check(const struct pwm_chip *chip)
187 const struct pwm_ops *ops = chip->ops;
192 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
194 "Please implement the .get_state() callback\n");
200 * __pwmchip_add() - register a new PWM chip
201 * @chip: the PWM chip to add
202 * @owner: reference to the module providing the chip.
204 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
205 * pwmchip_add wrapper to do this right.
207 * Returns: 0 on success or a negative error code on failure.
209 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
211 struct pwm_device *pwm;
215 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
218 if (!pwm_ops_check(chip))
223 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
227 mutex_lock(&pwm_lock);
229 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
231 mutex_unlock(&pwm_lock);
238 for (i = 0; i < chip->npwm; i++) {
239 pwm = &chip->pwms[i];
245 mutex_unlock(&pwm_lock);
247 if (IS_ENABLED(CONFIG_OF))
248 of_pwmchip_add(chip);
250 pwmchip_sysfs_export(chip);
254 EXPORT_SYMBOL_GPL(__pwmchip_add);
257 * pwmchip_remove() - remove a PWM chip
258 * @chip: the PWM chip to remove
260 * Removes a PWM chip.
262 void pwmchip_remove(struct pwm_chip *chip)
264 pwmchip_sysfs_unexport(chip);
266 if (IS_ENABLED(CONFIG_OF))
267 of_pwmchip_remove(chip);
269 mutex_lock(&pwm_lock);
271 idr_remove(&pwm_chips, chip->id);
273 mutex_unlock(&pwm_lock);
277 EXPORT_SYMBOL_GPL(pwmchip_remove);
279 static void devm_pwmchip_remove(void *data)
281 struct pwm_chip *chip = data;
283 pwmchip_remove(chip);
286 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
290 ret = __pwmchip_add(chip, owner);
294 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
296 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
299 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
301 * @index: per-chip index of the PWM to request
302 * @label: a literal description string of this PWM
304 * Returns: A pointer to the PWM device at the given index of the given PWM
305 * chip. A negative error code is returned if the index is not valid for the
306 * specified PWM chip or if the PWM device cannot be requested.
308 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
312 struct pwm_device *pwm;
315 if (!chip || index >= chip->npwm)
316 return ERR_PTR(-EINVAL);
318 mutex_lock(&pwm_lock);
319 pwm = &chip->pwms[index];
321 err = pwm_device_request(pwm, label);
325 mutex_unlock(&pwm_lock);
328 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
330 static void pwm_apply_state_debug(struct pwm_device *pwm,
331 const struct pwm_state *state)
333 struct pwm_state *last = &pwm->last;
334 struct pwm_chip *chip = pwm->chip;
335 struct pwm_state s1 = { 0 }, s2 = { 0 };
338 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
341 /* No reasonable diagnosis possible without .get_state() */
342 if (!chip->ops->get_state)
346 * *state was just applied. Read out the hardware state and do some
350 err = chip->ops->get_state(chip, pwm, &s1);
351 trace_pwm_get(pwm, &s1, err);
353 /* If that failed there isn't much to debug */
357 * The lowlevel driver either ignored .polarity (which is a bug) or as
358 * best effort inverted .polarity and fixed .duty_cycle respectively.
359 * Undo this inversion and fixup for further tests.
361 if (s1.enabled && s1.polarity != state->polarity) {
362 s2.polarity = state->polarity;
363 s2.duty_cycle = s1.period - s1.duty_cycle;
364 s2.period = s1.period;
365 s2.enabled = s1.enabled;
370 if (s2.polarity != state->polarity &&
371 state->duty_cycle < state->period)
372 dev_warn(chip->dev, ".apply ignored .polarity\n");
374 if (state->enabled &&
375 last->polarity == state->polarity &&
376 last->period > s2.period &&
377 last->period <= state->period)
379 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
380 state->period, s2.period, last->period);
382 if (state->enabled && state->period < s2.period)
384 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
385 state->period, s2.period);
387 if (state->enabled &&
388 last->polarity == state->polarity &&
389 last->period == s2.period &&
390 last->duty_cycle > s2.duty_cycle &&
391 last->duty_cycle <= state->duty_cycle)
393 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
394 state->duty_cycle, state->period,
395 s2.duty_cycle, s2.period,
396 last->duty_cycle, last->period);
398 if (state->enabled && state->duty_cycle < s2.duty_cycle)
400 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
401 state->duty_cycle, state->period,
402 s2.duty_cycle, s2.period);
404 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
406 "requested disabled, but yielded enabled with duty > 0\n");
408 /* reapply the state that the driver reported being configured. */
409 err = chip->ops->apply(chip, pwm, &s1);
410 trace_pwm_apply(pwm, &s1, err);
413 dev_err(chip->dev, "failed to reapply current setting\n");
417 *last = (struct pwm_state){ 0 };
418 err = chip->ops->get_state(chip, pwm, last);
419 trace_pwm_get(pwm, last, err);
423 /* reapplication of the current state should give an exact match */
424 if (s1.enabled != last->enabled ||
425 s1.polarity != last->polarity ||
426 (s1.enabled && s1.period != last->period) ||
427 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
429 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
430 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
431 last->enabled, last->polarity, last->duty_cycle,
437 * pwm_apply_state() - atomically apply a new state to a PWM device
439 * @state: new state to apply
441 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
443 struct pwm_chip *chip;
447 * Some lowlevel driver's implementations of .apply() make use of
448 * mutexes, also with some drivers only returning when the new
449 * configuration is active calling pwm_apply_state() from atomic context
450 * is a bad idea. So make it explicit that calling this function might
455 if (!pwm || !state || !state->period ||
456 state->duty_cycle > state->period)
461 if (state->period == pwm->state.period &&
462 state->duty_cycle == pwm->state.duty_cycle &&
463 state->polarity == pwm->state.polarity &&
464 state->enabled == pwm->state.enabled &&
465 state->usage_power == pwm->state.usage_power)
468 err = chip->ops->apply(chip, pwm, state);
469 trace_pwm_apply(pwm, state, err);
476 * only do this after pwm->state was applied as some
477 * implementations of .get_state depend on this
479 pwm_apply_state_debug(pwm, state);
483 EXPORT_SYMBOL_GPL(pwm_apply_state);
486 * pwm_capture() - capture and report a PWM signal
488 * @result: structure to fill with capture result
489 * @timeout: time to wait, in milliseconds, before giving up on capture
491 * Returns: 0 on success or a negative error code on failure.
493 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
494 unsigned long timeout)
498 if (!pwm || !pwm->chip->ops)
501 if (!pwm->chip->ops->capture)
504 mutex_lock(&pwm_lock);
505 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
506 mutex_unlock(&pwm_lock);
510 EXPORT_SYMBOL_GPL(pwm_capture);
513 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
516 * This function will adjust the PWM config to the PWM arguments provided
517 * by the DT or PWM lookup table. This is particularly useful to adapt
518 * the bootloader config to the Linux one.
520 int pwm_adjust_config(struct pwm_device *pwm)
522 struct pwm_state state;
523 struct pwm_args pargs;
525 pwm_get_args(pwm, &pargs);
526 pwm_get_state(pwm, &state);
529 * If the current period is zero it means that either the PWM driver
530 * does not support initial state retrieval or the PWM has not yet
533 * In either case, we setup the new period and polarity, and assign a
537 state.duty_cycle = 0;
538 state.period = pargs.period;
539 state.polarity = pargs.polarity;
541 return pwm_apply_state(pwm, &state);
545 * Adjust the PWM duty cycle/period based on the period value provided
548 if (pargs.period != state.period) {
549 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
551 do_div(dutycycle, state.period);
552 state.duty_cycle = dutycycle;
553 state.period = pargs.period;
557 * If the polarity changed, we should also change the duty cycle.
559 if (pargs.polarity != state.polarity) {
560 state.polarity = pargs.polarity;
561 state.duty_cycle = state.period - state.duty_cycle;
564 return pwm_apply_state(pwm, &state);
566 EXPORT_SYMBOL_GPL(pwm_adjust_config);
568 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
570 struct pwm_chip *chip;
571 unsigned long id, tmp;
573 mutex_lock(&pwm_lock);
575 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
576 if (chip->dev && device_match_fwnode(chip->dev, fwnode)) {
577 mutex_unlock(&pwm_lock);
581 mutex_unlock(&pwm_lock);
583 return ERR_PTR(-EPROBE_DEFER);
586 static struct device_link *pwm_device_link_add(struct device *dev,
587 struct pwm_device *pwm)
589 struct device_link *dl;
593 * No device for the PWM consumer has been provided. It may
594 * impact the PM sequence ordering: the PWM supplier may get
595 * suspended before the consumer.
597 dev_warn(pwm->chip->dev,
598 "No consumer device specified to create a link to\n");
602 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
604 dev_err(dev, "failed to create device link to %s\n",
605 dev_name(pwm->chip->dev));
606 return ERR_PTR(-EINVAL);
613 * of_pwm_get() - request a PWM via the PWM framework
614 * @dev: device for PWM consumer
615 * @np: device node to get the PWM from
616 * @con_id: consumer name
618 * Returns the PWM device parsed from the phandle and index specified in the
619 * "pwms" property of a device tree node or a negative error-code on failure.
620 * Values parsed from the device tree are stored in the returned PWM device
623 * If con_id is NULL, the first PWM device listed in the "pwms" property will
624 * be requested. Otherwise the "pwm-names" property is used to do a reverse
625 * lookup of the PWM index. This also means that the "pwm-names" property
626 * becomes mandatory for devices that look up the PWM device via the con_id
629 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
630 * error code on failure.
632 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
635 struct pwm_device *pwm = NULL;
636 struct of_phandle_args args;
637 struct device_link *dl;
638 struct pwm_chip *chip;
643 index = of_property_match_string(np, "pwm-names", con_id);
645 return ERR_PTR(index);
648 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
651 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
655 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
657 if (PTR_ERR(chip) != -EPROBE_DEFER)
658 pr_err("%s(): PWM chip not found\n", __func__);
660 pwm = ERR_CAST(chip);
664 pwm = chip->of_xlate(chip, &args);
668 dl = pwm_device_link_add(dev, pwm);
670 /* of_xlate ended up calling pwm_request_from_chip() */
677 * If a consumer name was not given, try to look it up from the
678 * "pwm-names" property if it exists. Otherwise use the name of
679 * the user device node.
682 err = of_property_read_string_index(np, "pwm-names", index,
691 of_node_put(args.np);
697 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
698 * @fwnode: firmware node to get the "pwms" property from
700 * Returns the PWM device parsed from the fwnode and index specified in the
701 * "pwms" property or a negative error-code on failure.
702 * Values parsed from the device tree are stored in the returned PWM device
705 * This is analogous to of_pwm_get() except con_id is not yet supported.
706 * ACPI entries must look like
707 * Package () {"pwms", Package ()
708 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
710 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
711 * error code on failure.
713 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
715 struct pwm_device *pwm;
716 struct fwnode_reference_args args;
717 struct pwm_chip *chip;
720 memset(&args, 0, sizeof(args));
722 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
727 return ERR_PTR(-EPROTO);
729 chip = fwnode_to_pwmchip(args.fwnode);
731 return ERR_CAST(chip);
733 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
737 pwm->args.period = args.args[1];
738 pwm->args.polarity = PWM_POLARITY_NORMAL;
740 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
741 pwm->args.polarity = PWM_POLARITY_INVERSED;
747 * pwm_add_table() - register PWM device consumers
748 * @table: array of consumers to register
749 * @num: number of consumers in table
751 void pwm_add_table(struct pwm_lookup *table, size_t num)
753 mutex_lock(&pwm_lookup_lock);
756 list_add_tail(&table->list, &pwm_lookup_list);
760 mutex_unlock(&pwm_lookup_lock);
764 * pwm_remove_table() - unregister PWM device consumers
765 * @table: array of consumers to unregister
766 * @num: number of consumers in table
768 void pwm_remove_table(struct pwm_lookup *table, size_t num)
770 mutex_lock(&pwm_lookup_lock);
773 list_del(&table->list);
777 mutex_unlock(&pwm_lookup_lock);
781 * pwm_get() - look up and request a PWM device
782 * @dev: device for PWM consumer
783 * @con_id: consumer name
785 * Lookup is first attempted using DT. If the device was not instantiated from
786 * a device tree, a PWM chip and a relative index is looked up via a table
787 * supplied by board setup code (see pwm_add_table()).
789 * Once a PWM chip has been found the specified PWM device will be requested
790 * and is ready to be used.
792 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
793 * error code on failure.
795 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
797 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
798 const char *dev_id = dev ? dev_name(dev) : NULL;
799 struct pwm_device *pwm;
800 struct pwm_chip *chip;
801 struct device_link *dl;
802 unsigned int best = 0;
803 struct pwm_lookup *p, *chosen = NULL;
807 /* look up via DT first */
808 if (is_of_node(fwnode))
809 return of_pwm_get(dev, to_of_node(fwnode), con_id);
811 /* then lookup via ACPI */
812 if (is_acpi_node(fwnode)) {
813 pwm = acpi_pwm_get(fwnode);
814 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
819 * We look up the provider in the static table typically provided by
820 * board setup code. We first try to lookup the consumer device by
821 * name. If the consumer device was passed in as NULL or if no match
822 * was found, we try to find the consumer by directly looking it up
825 * If a match is found, the provider PWM chip is looked up by name
826 * and a PWM device is requested using the PWM device per-chip index.
828 * The lookup algorithm was shamelessly taken from the clock
831 * We do slightly fuzzy matching here:
832 * An entry with a NULL ID is assumed to be a wildcard.
833 * If an entry has a device ID, it must match
834 * If an entry has a connection ID, it must match
835 * Then we take the most specific entry - with the following order
836 * of precedence: dev+con > dev only > con only.
838 mutex_lock(&pwm_lookup_lock);
840 list_for_each_entry(p, &pwm_lookup_list, list) {
844 if (!dev_id || strcmp(p->dev_id, dev_id))
851 if (!con_id || strcmp(p->con_id, con_id))
867 mutex_unlock(&pwm_lookup_lock);
870 return ERR_PTR(-ENODEV);
872 chip = pwmchip_find_by_name(chosen->provider);
875 * If the lookup entry specifies a module, load the module and retry
876 * the PWM chip lookup. This can be used to work around driver load
877 * ordering issues if driver's can't be made to properly support the
878 * deferred probe mechanism.
880 if (!chip && chosen->module) {
881 err = request_module(chosen->module);
883 chip = pwmchip_find_by_name(chosen->provider);
887 return ERR_PTR(-EPROBE_DEFER);
889 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
893 dl = pwm_device_link_add(dev, pwm);
899 pwm->args.period = chosen->period;
900 pwm->args.polarity = chosen->polarity;
904 EXPORT_SYMBOL_GPL(pwm_get);
907 * pwm_put() - release a PWM device
910 void pwm_put(struct pwm_device *pwm)
915 mutex_lock(&pwm_lock);
917 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
918 pr_warn("PWM device already freed\n");
922 if (pwm->chip->ops->free)
923 pwm->chip->ops->free(pwm->chip, pwm);
927 module_put(pwm->chip->owner);
929 mutex_unlock(&pwm_lock);
931 EXPORT_SYMBOL_GPL(pwm_put);
933 static void devm_pwm_release(void *pwm)
939 * devm_pwm_get() - resource managed pwm_get()
940 * @dev: device for PWM consumer
941 * @con_id: consumer name
943 * This function performs like pwm_get() but the acquired PWM device will
944 * automatically be released on driver detach.
946 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
947 * error code on failure.
949 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
951 struct pwm_device *pwm;
954 pwm = pwm_get(dev, con_id);
958 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
964 EXPORT_SYMBOL_GPL(devm_pwm_get);
967 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
968 * @dev: device for PWM consumer
969 * @fwnode: firmware node to get the PWM from
970 * @con_id: consumer name
972 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
973 * acpi_pwm_get() for a detailed description.
975 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
976 * error code on failure.
978 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
979 struct fwnode_handle *fwnode,
982 struct pwm_device *pwm = ERR_PTR(-ENODEV);
985 if (is_of_node(fwnode))
986 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
987 else if (is_acpi_node(fwnode))
988 pwm = acpi_pwm_get(fwnode);
992 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
998 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1000 #ifdef CONFIG_DEBUG_FS
1001 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1005 for (i = 0; i < chip->npwm; i++) {
1006 struct pwm_device *pwm = &chip->pwms[i];
1007 struct pwm_state state;
1009 pwm_get_state(pwm, &state);
1011 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1013 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1014 seq_puts(s, " requested");
1017 seq_puts(s, " enabled");
1019 seq_printf(s, " period: %llu ns", state.period);
1020 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1021 seq_printf(s, " polarity: %s",
1022 state.polarity ? "inverse" : "normal");
1024 if (state.usage_power)
1025 seq_puts(s, " usage_power");
1031 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1033 unsigned long id = *pos;
1036 mutex_lock(&pwm_lock);
1039 ret = idr_get_next_ul(&pwm_chips, &id);
1044 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1046 unsigned long id = *pos + 1;
1051 ret = idr_get_next_ul(&pwm_chips, &id);
1056 static void pwm_seq_stop(struct seq_file *s, void *v)
1058 mutex_unlock(&pwm_lock);
1061 static int pwm_seq_show(struct seq_file *s, void *v)
1063 struct pwm_chip *chip = v;
1065 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1066 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1067 dev_name(chip->dev), chip->npwm,
1068 (chip->npwm != 1) ? "s" : "");
1070 pwm_dbg_show(chip, s);
1075 static const struct seq_operations pwm_debugfs_sops = {
1076 .start = pwm_seq_start,
1077 .next = pwm_seq_next,
1078 .stop = pwm_seq_stop,
1079 .show = pwm_seq_show,
1082 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1084 static int __init pwm_debugfs_init(void)
1086 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1090 subsys_initcall(pwm_debugfs_init);
1091 #endif /* CONFIG_DEBUG_FS */