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/pwm.h>
12 #include <linux/radix-tree.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
21 #include <dt-bindings/pwm/pwm.h>
23 #define CREATE_TRACE_POINTS
24 #include <trace/events/pwm.h>
28 static DEFINE_MUTEX(pwm_lookup_lock);
29 static LIST_HEAD(pwm_lookup_list);
31 /* protects access to pwm_chips and allocated_pwms */
32 static DEFINE_MUTEX(pwm_lock);
34 static LIST_HEAD(pwm_chips);
35 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
37 /* Called with pwm_lock held */
38 static int alloc_pwms(unsigned int count)
42 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0,
45 if (start + count > MAX_PWMS)
48 bitmap_set(allocated_pwms, start, count);
53 /* Called with pwm_lock held */
54 static void free_pwms(struct pwm_chip *chip)
56 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
62 static struct pwm_chip *pwmchip_find_by_name(const char *name)
64 struct pwm_chip *chip;
69 mutex_lock(&pwm_lock);
71 list_for_each_entry(chip, &pwm_chips, list) {
72 const char *chip_name = dev_name(chip->dev);
74 if (chip_name && strcmp(chip_name, name) == 0) {
75 mutex_unlock(&pwm_lock);
80 mutex_unlock(&pwm_lock);
85 static int pwm_device_request(struct pwm_device *pwm, const char *label)
89 if (test_bit(PWMF_REQUESTED, &pwm->flags))
92 if (!try_module_get(pwm->chip->ops->owner))
95 if (pwm->chip->ops->request) {
96 err = pwm->chip->ops->request(pwm->chip, pwm);
98 module_put(pwm->chip->ops->owner);
103 if (pwm->chip->ops->get_state) {
105 * Zero-initialize state because most drivers are unaware of
106 * .usage_power. The other members of state are supposed to be
107 * set by lowlevel drivers. We still initialize the whole
108 * structure for simplicity even though this might paper over
109 * faulty implementations of .get_state().
111 struct pwm_state state = { 0, };
113 err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
114 trace_pwm_get(pwm, &state, err);
119 if (IS_ENABLED(CONFIG_PWM_DEBUG))
120 pwm->last = pwm->state;
123 set_bit(PWMF_REQUESTED, &pwm->flags);
130 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
132 struct pwm_device *pwm;
134 if (pc->of_pwm_n_cells < 2)
135 return ERR_PTR(-EINVAL);
137 /* flags in the third cell are optional */
138 if (args->args_count < 2)
139 return ERR_PTR(-EINVAL);
141 if (args->args[0] >= pc->npwm)
142 return ERR_PTR(-EINVAL);
144 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
148 pwm->args.period = args->args[1];
149 pwm->args.polarity = PWM_POLARITY_NORMAL;
151 if (pc->of_pwm_n_cells >= 3) {
152 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
153 pwm->args.polarity = PWM_POLARITY_INVERSED;
158 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
161 of_pwm_single_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
163 struct pwm_device *pwm;
165 if (pc->of_pwm_n_cells < 1)
166 return ERR_PTR(-EINVAL);
168 /* validate that one cell is specified, optionally with flags */
169 if (args->args_count != 1 && args->args_count != 2)
170 return ERR_PTR(-EINVAL);
172 pwm = pwm_request_from_chip(pc, 0, NULL);
176 pwm->args.period = args->args[0];
177 pwm->args.polarity = PWM_POLARITY_NORMAL;
179 if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED)
180 pwm->args.polarity = PWM_POLARITY_INVERSED;
184 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
186 static void of_pwmchip_add(struct pwm_chip *chip)
188 if (!chip->dev || !chip->dev->of_node)
191 if (!chip->of_xlate) {
194 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells",
198 chip->of_xlate = of_pwm_xlate_with_flags;
199 chip->of_pwm_n_cells = pwm_cells;
202 of_node_get(chip->dev->of_node);
205 static void of_pwmchip_remove(struct pwm_chip *chip)
208 of_node_put(chip->dev->of_node);
212 * pwm_set_chip_data() - set private chip data for a PWM
214 * @data: pointer to chip-specific data
216 * Returns: 0 on success or a negative error code on failure.
218 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
223 pwm->chip_data = data;
227 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
230 * pwm_get_chip_data() - get private chip data for a PWM
233 * Returns: A pointer to the chip-private data for the PWM device.
235 void *pwm_get_chip_data(struct pwm_device *pwm)
237 return pwm ? pwm->chip_data : NULL;
239 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
241 static bool pwm_ops_check(const struct pwm_chip *chip)
243 const struct pwm_ops *ops = chip->ops;
248 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
250 "Please implement the .get_state() callback\n");
256 * pwmchip_add() - register a new PWM chip
257 * @chip: the PWM chip to add
259 * Register a new PWM chip.
261 * Returns: 0 on success or a negative error code on failure.
263 int pwmchip_add(struct pwm_chip *chip)
265 struct pwm_device *pwm;
269 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
272 if (!pwm_ops_check(chip))
275 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
279 mutex_lock(&pwm_lock);
281 ret = alloc_pwms(chip->npwm);
283 mutex_unlock(&pwm_lock);
290 for (i = 0; i < chip->npwm; i++) {
291 pwm = &chip->pwms[i];
294 pwm->pwm = chip->base + i;
298 list_add(&chip->list, &pwm_chips);
300 mutex_unlock(&pwm_lock);
302 if (IS_ENABLED(CONFIG_OF))
303 of_pwmchip_add(chip);
305 pwmchip_sysfs_export(chip);
309 EXPORT_SYMBOL_GPL(pwmchip_add);
312 * pwmchip_remove() - remove a PWM chip
313 * @chip: the PWM chip to remove
315 * Removes a PWM chip. This function may return busy if the PWM chip provides
316 * a PWM device that is still requested.
318 * Returns: 0 on success or a negative error code on failure.
320 void pwmchip_remove(struct pwm_chip *chip)
322 pwmchip_sysfs_unexport(chip);
324 mutex_lock(&pwm_lock);
326 list_del_init(&chip->list);
328 if (IS_ENABLED(CONFIG_OF))
329 of_pwmchip_remove(chip);
333 mutex_unlock(&pwm_lock);
335 EXPORT_SYMBOL_GPL(pwmchip_remove);
337 static void devm_pwmchip_remove(void *data)
339 struct pwm_chip *chip = data;
341 pwmchip_remove(chip);
344 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
348 ret = pwmchip_add(chip);
352 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
354 EXPORT_SYMBOL_GPL(devm_pwmchip_add);
357 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
359 * @index: per-chip index of the PWM to request
360 * @label: a literal description string of this PWM
362 * Returns: A pointer to the PWM device at the given index of the given PWM
363 * chip. A negative error code is returned if the index is not valid for the
364 * specified PWM chip or if the PWM device cannot be requested.
366 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
370 struct pwm_device *pwm;
373 if (!chip || index >= chip->npwm)
374 return ERR_PTR(-EINVAL);
376 mutex_lock(&pwm_lock);
377 pwm = &chip->pwms[index];
379 err = pwm_device_request(pwm, label);
383 mutex_unlock(&pwm_lock);
386 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
388 static void pwm_apply_state_debug(struct pwm_device *pwm,
389 const struct pwm_state *state)
391 struct pwm_state *last = &pwm->last;
392 struct pwm_chip *chip = pwm->chip;
393 struct pwm_state s1 = { 0 }, s2 = { 0 };
396 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
399 /* No reasonable diagnosis possible without .get_state() */
400 if (!chip->ops->get_state)
404 * *state was just applied. Read out the hardware state and do some
408 err = chip->ops->get_state(chip, pwm, &s1);
409 trace_pwm_get(pwm, &s1, err);
411 /* If that failed there isn't much to debug */
415 * The lowlevel driver either ignored .polarity (which is a bug) or as
416 * best effort inverted .polarity and fixed .duty_cycle respectively.
417 * Undo this inversion and fixup for further tests.
419 if (s1.enabled && s1.polarity != state->polarity) {
420 s2.polarity = state->polarity;
421 s2.duty_cycle = s1.period - s1.duty_cycle;
422 s2.period = s1.period;
423 s2.enabled = s1.enabled;
428 if (s2.polarity != state->polarity &&
429 state->duty_cycle < state->period)
430 dev_warn(chip->dev, ".apply ignored .polarity\n");
432 if (state->enabled &&
433 last->polarity == state->polarity &&
434 last->period > s2.period &&
435 last->period <= state->period)
437 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
438 state->period, s2.period, last->period);
440 if (state->enabled && state->period < s2.period)
442 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
443 state->period, s2.period);
445 if (state->enabled &&
446 last->polarity == state->polarity &&
447 last->period == s2.period &&
448 last->duty_cycle > s2.duty_cycle &&
449 last->duty_cycle <= state->duty_cycle)
451 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
452 state->duty_cycle, state->period,
453 s2.duty_cycle, s2.period,
454 last->duty_cycle, last->period);
456 if (state->enabled && state->duty_cycle < s2.duty_cycle)
458 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
459 state->duty_cycle, state->period,
460 s2.duty_cycle, s2.period);
462 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
464 "requested disabled, but yielded enabled with duty > 0\n");
466 /* reapply the state that the driver reported being configured. */
467 err = chip->ops->apply(chip, pwm, &s1);
468 trace_pwm_apply(pwm, &s1, err);
471 dev_err(chip->dev, "failed to reapply current setting\n");
475 *last = (struct pwm_state){ 0 };
476 err = chip->ops->get_state(chip, pwm, last);
477 trace_pwm_get(pwm, last, err);
481 /* reapplication of the current state should give an exact match */
482 if (s1.enabled != last->enabled ||
483 s1.polarity != last->polarity ||
484 (s1.enabled && s1.period != last->period) ||
485 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
487 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
488 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
489 last->enabled, last->polarity, last->duty_cycle,
495 * pwm_apply_state() - atomically apply a new state to a PWM device
497 * @state: new state to apply
499 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
501 struct pwm_chip *chip;
505 * Some lowlevel driver's implementations of .apply() make use of
506 * mutexes, also with some drivers only returning when the new
507 * configuration is active calling pwm_apply_state() from atomic context
508 * is a bad idea. So make it explicit that calling this function might
513 if (!pwm || !state || !state->period ||
514 state->duty_cycle > state->period)
519 if (state->period == pwm->state.period &&
520 state->duty_cycle == pwm->state.duty_cycle &&
521 state->polarity == pwm->state.polarity &&
522 state->enabled == pwm->state.enabled &&
523 state->usage_power == pwm->state.usage_power)
526 err = chip->ops->apply(chip, pwm, state);
527 trace_pwm_apply(pwm, state, err);
534 * only do this after pwm->state was applied as some
535 * implementations of .get_state depend on this
537 pwm_apply_state_debug(pwm, state);
541 EXPORT_SYMBOL_GPL(pwm_apply_state);
544 * pwm_capture() - capture and report a PWM signal
546 * @result: structure to fill with capture result
547 * @timeout: time to wait, in milliseconds, before giving up on capture
549 * Returns: 0 on success or a negative error code on failure.
551 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
552 unsigned long timeout)
556 if (!pwm || !pwm->chip->ops)
559 if (!pwm->chip->ops->capture)
562 mutex_lock(&pwm_lock);
563 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
564 mutex_unlock(&pwm_lock);
568 EXPORT_SYMBOL_GPL(pwm_capture);
571 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
574 * This function will adjust the PWM config to the PWM arguments provided
575 * by the DT or PWM lookup table. This is particularly useful to adapt
576 * the bootloader config to the Linux one.
578 int pwm_adjust_config(struct pwm_device *pwm)
580 struct pwm_state state;
581 struct pwm_args pargs;
583 pwm_get_args(pwm, &pargs);
584 pwm_get_state(pwm, &state);
587 * If the current period is zero it means that either the PWM driver
588 * does not support initial state retrieval or the PWM has not yet
591 * In either case, we setup the new period and polarity, and assign a
595 state.duty_cycle = 0;
596 state.period = pargs.period;
597 state.polarity = pargs.polarity;
599 return pwm_apply_state(pwm, &state);
603 * Adjust the PWM duty cycle/period based on the period value provided
606 if (pargs.period != state.period) {
607 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
609 do_div(dutycycle, state.period);
610 state.duty_cycle = dutycycle;
611 state.period = pargs.period;
615 * If the polarity changed, we should also change the duty cycle.
617 if (pargs.polarity != state.polarity) {
618 state.polarity = pargs.polarity;
619 state.duty_cycle = state.period - state.duty_cycle;
622 return pwm_apply_state(pwm, &state);
624 EXPORT_SYMBOL_GPL(pwm_adjust_config);
626 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
628 struct pwm_chip *chip;
630 mutex_lock(&pwm_lock);
632 list_for_each_entry(chip, &pwm_chips, list)
633 if (chip->dev && device_match_fwnode(chip->dev, fwnode)) {
634 mutex_unlock(&pwm_lock);
638 mutex_unlock(&pwm_lock);
640 return ERR_PTR(-EPROBE_DEFER);
643 static struct device_link *pwm_device_link_add(struct device *dev,
644 struct pwm_device *pwm)
646 struct device_link *dl;
650 * No device for the PWM consumer has been provided. It may
651 * impact the PM sequence ordering: the PWM supplier may get
652 * suspended before the consumer.
654 dev_warn(pwm->chip->dev,
655 "No consumer device specified to create a link to\n");
659 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
661 dev_err(dev, "failed to create device link to %s\n",
662 dev_name(pwm->chip->dev));
663 return ERR_PTR(-EINVAL);
670 * of_pwm_get() - request a PWM via the PWM framework
671 * @dev: device for PWM consumer
672 * @np: device node to get the PWM from
673 * @con_id: consumer name
675 * Returns the PWM device parsed from the phandle and index specified in the
676 * "pwms" property of a device tree node or a negative error-code on failure.
677 * Values parsed from the device tree are stored in the returned PWM device
680 * If con_id is NULL, the first PWM device listed in the "pwms" property will
681 * be requested. Otherwise the "pwm-names" property is used to do a reverse
682 * lookup of the PWM index. This also means that the "pwm-names" property
683 * becomes mandatory for devices that look up the PWM device via the con_id
686 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
687 * error code on failure.
689 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
692 struct pwm_device *pwm = NULL;
693 struct of_phandle_args args;
694 struct device_link *dl;
700 index = of_property_match_string(np, "pwm-names", con_id);
702 return ERR_PTR(index);
705 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
708 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
712 pc = fwnode_to_pwmchip(of_fwnode_handle(args.np));
714 if (PTR_ERR(pc) != -EPROBE_DEFER)
715 pr_err("%s(): PWM chip not found\n", __func__);
721 pwm = pc->of_xlate(pc, &args);
725 dl = pwm_device_link_add(dev, pwm);
727 /* of_xlate ended up calling pwm_request_from_chip() */
734 * If a consumer name was not given, try to look it up from the
735 * "pwm-names" property if it exists. Otherwise use the name of
736 * the user device node.
739 err = of_property_read_string_index(np, "pwm-names", index,
748 of_node_put(args.np);
754 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
755 * @fwnode: firmware node to get the "pwms" property from
757 * Returns the PWM device parsed from the fwnode and index specified in the
758 * "pwms" property or a negative error-code on failure.
759 * Values parsed from the device tree are stored in the returned PWM device
762 * This is analogous to of_pwm_get() except con_id is not yet supported.
763 * ACPI entries must look like
764 * Package () {"pwms", Package ()
765 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
767 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
768 * error code on failure.
770 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
772 struct pwm_device *pwm;
773 struct fwnode_reference_args args;
774 struct pwm_chip *chip;
777 memset(&args, 0, sizeof(args));
779 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
784 return ERR_PTR(-EPROTO);
786 chip = fwnode_to_pwmchip(args.fwnode);
788 return ERR_CAST(chip);
790 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
794 pwm->args.period = args.args[1];
795 pwm->args.polarity = PWM_POLARITY_NORMAL;
797 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
798 pwm->args.polarity = PWM_POLARITY_INVERSED;
804 * pwm_add_table() - register PWM device consumers
805 * @table: array of consumers to register
806 * @num: number of consumers in table
808 void pwm_add_table(struct pwm_lookup *table, size_t num)
810 mutex_lock(&pwm_lookup_lock);
813 list_add_tail(&table->list, &pwm_lookup_list);
817 mutex_unlock(&pwm_lookup_lock);
821 * pwm_remove_table() - unregister PWM device consumers
822 * @table: array of consumers to unregister
823 * @num: number of consumers in table
825 void pwm_remove_table(struct pwm_lookup *table, size_t num)
827 mutex_lock(&pwm_lookup_lock);
830 list_del(&table->list);
834 mutex_unlock(&pwm_lookup_lock);
838 * pwm_get() - look up and request a PWM device
839 * @dev: device for PWM consumer
840 * @con_id: consumer name
842 * Lookup is first attempted using DT. If the device was not instantiated from
843 * a device tree, a PWM chip and a relative index is looked up via a table
844 * supplied by board setup code (see pwm_add_table()).
846 * Once a PWM chip has been found the specified PWM device will be requested
847 * and is ready to be used.
849 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
850 * error code on failure.
852 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
854 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
855 const char *dev_id = dev ? dev_name(dev) : NULL;
856 struct pwm_device *pwm;
857 struct pwm_chip *chip;
858 struct device_link *dl;
859 unsigned int best = 0;
860 struct pwm_lookup *p, *chosen = NULL;
864 /* look up via DT first */
865 if (is_of_node(fwnode))
866 return of_pwm_get(dev, to_of_node(fwnode), con_id);
868 /* then lookup via ACPI */
869 if (is_acpi_node(fwnode)) {
870 pwm = acpi_pwm_get(fwnode);
871 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
876 * We look up the provider in the static table typically provided by
877 * board setup code. We first try to lookup the consumer device by
878 * name. If the consumer device was passed in as NULL or if no match
879 * was found, we try to find the consumer by directly looking it up
882 * If a match is found, the provider PWM chip is looked up by name
883 * and a PWM device is requested using the PWM device per-chip index.
885 * The lookup algorithm was shamelessly taken from the clock
888 * We do slightly fuzzy matching here:
889 * An entry with a NULL ID is assumed to be a wildcard.
890 * If an entry has a device ID, it must match
891 * If an entry has a connection ID, it must match
892 * Then we take the most specific entry - with the following order
893 * of precedence: dev+con > dev only > con only.
895 mutex_lock(&pwm_lookup_lock);
897 list_for_each_entry(p, &pwm_lookup_list, list) {
901 if (!dev_id || strcmp(p->dev_id, dev_id))
908 if (!con_id || strcmp(p->con_id, con_id))
924 mutex_unlock(&pwm_lookup_lock);
927 return ERR_PTR(-ENODEV);
929 chip = pwmchip_find_by_name(chosen->provider);
932 * If the lookup entry specifies a module, load the module and retry
933 * the PWM chip lookup. This can be used to work around driver load
934 * ordering issues if driver's can't be made to properly support the
935 * deferred probe mechanism.
937 if (!chip && chosen->module) {
938 err = request_module(chosen->module);
940 chip = pwmchip_find_by_name(chosen->provider);
944 return ERR_PTR(-EPROBE_DEFER);
946 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
950 dl = pwm_device_link_add(dev, pwm);
956 pwm->args.period = chosen->period;
957 pwm->args.polarity = chosen->polarity;
961 EXPORT_SYMBOL_GPL(pwm_get);
964 * pwm_put() - release a PWM device
967 void pwm_put(struct pwm_device *pwm)
972 mutex_lock(&pwm_lock);
974 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
975 pr_warn("PWM device already freed\n");
979 if (pwm->chip->ops->free)
980 pwm->chip->ops->free(pwm->chip, pwm);
982 pwm_set_chip_data(pwm, NULL);
985 module_put(pwm->chip->ops->owner);
987 mutex_unlock(&pwm_lock);
989 EXPORT_SYMBOL_GPL(pwm_put);
991 static void devm_pwm_release(void *pwm)
997 * devm_pwm_get() - resource managed pwm_get()
998 * @dev: device for PWM consumer
999 * @con_id: consumer name
1001 * This function performs like pwm_get() but the acquired PWM device will
1002 * automatically be released on driver detach.
1004 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1005 * error code on failure.
1007 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1009 struct pwm_device *pwm;
1012 pwm = pwm_get(dev, con_id);
1016 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1018 return ERR_PTR(ret);
1022 EXPORT_SYMBOL_GPL(devm_pwm_get);
1025 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1026 * @dev: device for PWM consumer
1027 * @fwnode: firmware node to get the PWM from
1028 * @con_id: consumer name
1030 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1031 * acpi_pwm_get() for a detailed description.
1033 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1034 * error code on failure.
1036 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1037 struct fwnode_handle *fwnode,
1040 struct pwm_device *pwm = ERR_PTR(-ENODEV);
1043 if (is_of_node(fwnode))
1044 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1045 else if (is_acpi_node(fwnode))
1046 pwm = acpi_pwm_get(fwnode);
1050 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1052 return ERR_PTR(ret);
1056 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1058 #ifdef CONFIG_DEBUG_FS
1059 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1063 for (i = 0; i < chip->npwm; i++) {
1064 struct pwm_device *pwm = &chip->pwms[i];
1065 struct pwm_state state;
1067 pwm_get_state(pwm, &state);
1069 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1071 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1072 seq_puts(s, " requested");
1075 seq_puts(s, " enabled");
1077 seq_printf(s, " period: %llu ns", state.period);
1078 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1079 seq_printf(s, " polarity: %s",
1080 state.polarity ? "inverse" : "normal");
1082 if (state.usage_power)
1083 seq_puts(s, " usage_power");
1089 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1091 mutex_lock(&pwm_lock);
1094 return seq_list_start(&pwm_chips, *pos);
1097 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1101 return seq_list_next(v, &pwm_chips, pos);
1104 static void pwm_seq_stop(struct seq_file *s, void *v)
1106 mutex_unlock(&pwm_lock);
1109 static int pwm_seq_show(struct seq_file *s, void *v)
1111 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1113 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1114 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1115 dev_name(chip->dev), chip->npwm,
1116 (chip->npwm != 1) ? "s" : "");
1118 pwm_dbg_show(chip, s);
1123 static const struct seq_operations pwm_debugfs_sops = {
1124 .start = pwm_seq_start,
1125 .next = pwm_seq_next,
1126 .stop = pwm_seq_stop,
1127 .show = pwm_seq_show,
1130 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1132 static int __init pwm_debugfs_init(void)
1134 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1138 subsys_initcall(pwm_debugfs_init);
1139 #endif /* CONFIG_DEBUG_FS */