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);
30 static DEFINE_MUTEX(pwm_lock);
31 static LIST_HEAD(pwm_chips);
32 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
33 static RADIX_TREE(pwm_tree, GFP_KERNEL);
35 static struct pwm_device *pwm_to_device(unsigned int pwm)
37 return radix_tree_lookup(&pwm_tree, pwm);
40 static int alloc_pwms(int pwm, unsigned int count)
42 unsigned int from = 0;
51 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
54 if (pwm >= 0 && start != pwm)
57 if (start + count > MAX_PWMS)
63 static void free_pwms(struct pwm_chip *chip)
67 for (i = 0; i < chip->npwm; i++) {
68 struct pwm_device *pwm = &chip->pwms[i];
70 radix_tree_delete(&pwm_tree, pwm->pwm);
73 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
79 static struct pwm_chip *pwmchip_find_by_name(const char *name)
81 struct pwm_chip *chip;
86 mutex_lock(&pwm_lock);
88 list_for_each_entry(chip, &pwm_chips, list) {
89 const char *chip_name = dev_name(chip->dev);
91 if (chip_name && strcmp(chip_name, name) == 0) {
92 mutex_unlock(&pwm_lock);
97 mutex_unlock(&pwm_lock);
102 static int pwm_device_request(struct pwm_device *pwm, const char *label)
106 if (test_bit(PWMF_REQUESTED, &pwm->flags))
109 if (!try_module_get(pwm->chip->ops->owner))
112 if (pwm->chip->ops->request) {
113 err = pwm->chip->ops->request(pwm->chip, pwm);
115 module_put(pwm->chip->ops->owner);
120 if (pwm->chip->ops->get_state) {
121 pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state);
122 trace_pwm_get(pwm, &pwm->state);
125 set_bit(PWMF_REQUESTED, &pwm->flags);
132 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
134 struct pwm_device *pwm;
136 /* check, whether the driver supports a third cell for flags */
137 if (pc->of_pwm_n_cells < 3)
138 return ERR_PTR(-EINVAL);
140 /* flags in the third cell are optional */
141 if (args->args_count < 2)
142 return ERR_PTR(-EINVAL);
144 if (args->args[0] >= pc->npwm)
145 return ERR_PTR(-EINVAL);
147 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
151 pwm->args.period = args->args[1];
152 pwm->args.polarity = PWM_POLARITY_NORMAL;
154 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
155 pwm->args.polarity = PWM_POLARITY_INVERSED;
159 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
161 static struct pwm_device *
162 of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
164 struct pwm_device *pwm;
166 /* sanity check driver support */
167 if (pc->of_pwm_n_cells < 2)
168 return ERR_PTR(-EINVAL);
170 /* all cells are required */
171 if (args->args_count != pc->of_pwm_n_cells)
172 return ERR_PTR(-EINVAL);
174 if (args->args[0] >= pc->npwm)
175 return ERR_PTR(-EINVAL);
177 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
181 pwm->args.period = args->args[1];
186 static void of_pwmchip_add(struct pwm_chip *chip)
188 if (!chip->dev || !chip->dev->of_node)
191 if (!chip->of_xlate) {
192 chip->of_xlate = of_pwm_simple_xlate;
193 chip->of_pwm_n_cells = 2;
196 of_node_get(chip->dev->of_node);
199 static void of_pwmchip_remove(struct pwm_chip *chip)
202 of_node_put(chip->dev->of_node);
206 * pwm_set_chip_data() - set private chip data for a PWM
208 * @data: pointer to chip-specific data
210 * Returns: 0 on success or a negative error code on failure.
212 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
217 pwm->chip_data = data;
221 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
224 * pwm_get_chip_data() - get private chip data for a PWM
227 * Returns: A pointer to the chip-private data for the PWM device.
229 void *pwm_get_chip_data(struct pwm_device *pwm)
231 return pwm ? pwm->chip_data : NULL;
233 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
235 static bool pwm_ops_check(const struct pwm_ops *ops)
237 /* driver supports legacy, non-atomic operation */
238 if (ops->config && ops->enable && ops->disable)
241 /* driver supports atomic operation */
249 * pwmchip_add_with_polarity() - register a new PWM chip
250 * @chip: the PWM chip to add
251 * @polarity: initial polarity of PWM channels
253 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
254 * will be used. The initial polarity for all channels is specified by the
255 * @polarity parameter.
257 * Returns: 0 on success or a negative error code on failure.
259 int pwmchip_add_with_polarity(struct pwm_chip *chip,
260 enum pwm_polarity polarity)
262 struct pwm_device *pwm;
266 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
269 if (!pwm_ops_check(chip->ops))
272 mutex_lock(&pwm_lock);
274 ret = alloc_pwms(chip->base, chip->npwm);
278 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
286 for (i = 0; i < chip->npwm; i++) {
287 pwm = &chip->pwms[i];
290 pwm->pwm = chip->base + i;
292 pwm->state.polarity = polarity;
294 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
297 bitmap_set(allocated_pwms, chip->base, chip->npwm);
299 INIT_LIST_HEAD(&chip->list);
300 list_add(&chip->list, &pwm_chips);
304 if (IS_ENABLED(CONFIG_OF))
305 of_pwmchip_add(chip);
308 mutex_unlock(&pwm_lock);
311 pwmchip_sysfs_export(chip);
315 EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
318 * pwmchip_add() - register a new PWM chip
319 * @chip: the PWM chip to add
321 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
322 * will be used. The initial polarity for all channels is normal.
324 * Returns: 0 on success or a negative error code on failure.
326 int pwmchip_add(struct pwm_chip *chip)
328 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
330 EXPORT_SYMBOL_GPL(pwmchip_add);
333 * pwmchip_remove() - remove a PWM chip
334 * @chip: the PWM chip to remove
336 * Removes a PWM chip. This function may return busy if the PWM chip provides
337 * a PWM device that is still requested.
339 * Returns: 0 on success or a negative error code on failure.
341 int pwmchip_remove(struct pwm_chip *chip)
346 pwmchip_sysfs_unexport(chip);
348 mutex_lock(&pwm_lock);
350 for (i = 0; i < chip->npwm; i++) {
351 struct pwm_device *pwm = &chip->pwms[i];
353 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
359 list_del_init(&chip->list);
361 if (IS_ENABLED(CONFIG_OF))
362 of_pwmchip_remove(chip);
367 mutex_unlock(&pwm_lock);
370 EXPORT_SYMBOL_GPL(pwmchip_remove);
373 * pwm_request() - request a PWM device
374 * @pwm: global PWM device index
375 * @label: PWM device label
377 * This function is deprecated, use pwm_get() instead.
379 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
382 struct pwm_device *pwm_request(int pwm, const char *label)
384 struct pwm_device *dev;
387 if (pwm < 0 || pwm >= MAX_PWMS)
388 return ERR_PTR(-EINVAL);
390 mutex_lock(&pwm_lock);
392 dev = pwm_to_device(pwm);
394 dev = ERR_PTR(-EPROBE_DEFER);
398 err = pwm_device_request(dev, label);
403 mutex_unlock(&pwm_lock);
407 EXPORT_SYMBOL_GPL(pwm_request);
410 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
412 * @index: per-chip index of the PWM to request
413 * @label: a literal description string of this PWM
415 * Returns: A pointer to the PWM device at the given index of the given PWM
416 * chip. A negative error code is returned if the index is not valid for the
417 * specified PWM chip or if the PWM device cannot be requested.
419 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
423 struct pwm_device *pwm;
426 if (!chip || index >= chip->npwm)
427 return ERR_PTR(-EINVAL);
429 mutex_lock(&pwm_lock);
430 pwm = &chip->pwms[index];
432 err = pwm_device_request(pwm, label);
436 mutex_unlock(&pwm_lock);
439 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
442 * pwm_free() - free a PWM device
445 * This function is deprecated, use pwm_put() instead.
447 void pwm_free(struct pwm_device *pwm)
451 EXPORT_SYMBOL_GPL(pwm_free);
454 * pwm_apply_state() - atomically apply a new state to a PWM device
456 * @state: new state to apply
458 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
460 struct pwm_chip *chip;
463 if (!pwm || !state || !state->period ||
464 state->duty_cycle > state->period)
469 if (state->period == pwm->state.period &&
470 state->duty_cycle == pwm->state.duty_cycle &&
471 state->polarity == pwm->state.polarity &&
472 state->enabled == pwm->state.enabled)
475 if (chip->ops->apply) {
476 err = chip->ops->apply(chip, pwm, state);
480 trace_pwm_apply(pwm, state);
485 * FIXME: restore the initial state in case of error.
487 if (state->polarity != pwm->state.polarity) {
488 if (!chip->ops->set_polarity)
492 * Changing the polarity of a running PWM is
493 * only allowed when the PWM driver implements
496 if (pwm->state.enabled) {
497 chip->ops->disable(chip, pwm);
498 pwm->state.enabled = false;
501 err = chip->ops->set_polarity(chip, pwm,
506 pwm->state.polarity = state->polarity;
509 if (state->period != pwm->state.period ||
510 state->duty_cycle != pwm->state.duty_cycle) {
511 err = chip->ops->config(pwm->chip, pwm,
517 pwm->state.duty_cycle = state->duty_cycle;
518 pwm->state.period = state->period;
521 if (state->enabled != pwm->state.enabled) {
522 if (state->enabled) {
523 err = chip->ops->enable(chip, pwm);
527 chip->ops->disable(chip, pwm);
530 pwm->state.enabled = state->enabled;
536 EXPORT_SYMBOL_GPL(pwm_apply_state);
539 * pwm_capture() - capture and report a PWM signal
541 * @result: structure to fill with capture result
542 * @timeout: time to wait, in milliseconds, before giving up on capture
544 * Returns: 0 on success or a negative error code on failure.
546 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
547 unsigned long timeout)
551 if (!pwm || !pwm->chip->ops)
554 if (!pwm->chip->ops->capture)
557 mutex_lock(&pwm_lock);
558 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
559 mutex_unlock(&pwm_lock);
563 EXPORT_SYMBOL_GPL(pwm_capture);
566 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
569 * This function will adjust the PWM config to the PWM arguments provided
570 * by the DT or PWM lookup table. This is particularly useful to adapt
571 * the bootloader config to the Linux one.
573 int pwm_adjust_config(struct pwm_device *pwm)
575 struct pwm_state state;
576 struct pwm_args pargs;
578 pwm_get_args(pwm, &pargs);
579 pwm_get_state(pwm, &state);
582 * If the current period is zero it means that either the PWM driver
583 * does not support initial state retrieval or the PWM has not yet
586 * In either case, we setup the new period and polarity, and assign a
590 state.duty_cycle = 0;
591 state.period = pargs.period;
592 state.polarity = pargs.polarity;
594 return pwm_apply_state(pwm, &state);
598 * Adjust the PWM duty cycle/period based on the period value provided
601 if (pargs.period != state.period) {
602 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
604 do_div(dutycycle, state.period);
605 state.duty_cycle = dutycycle;
606 state.period = pargs.period;
610 * If the polarity changed, we should also change the duty cycle.
612 if (pargs.polarity != state.polarity) {
613 state.polarity = pargs.polarity;
614 state.duty_cycle = state.period - state.duty_cycle;
617 return pwm_apply_state(pwm, &state);
619 EXPORT_SYMBOL_GPL(pwm_adjust_config);
621 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
623 struct pwm_chip *chip;
625 mutex_lock(&pwm_lock);
627 list_for_each_entry(chip, &pwm_chips, list)
628 if (chip->dev && chip->dev->of_node == np) {
629 mutex_unlock(&pwm_lock);
633 mutex_unlock(&pwm_lock);
635 return ERR_PTR(-EPROBE_DEFER);
638 static struct device_link *pwm_device_link_add(struct device *dev,
639 struct pwm_device *pwm)
641 struct device_link *dl;
645 * No device for the PWM consumer has been provided. It may
646 * impact the PM sequence ordering: the PWM supplier may get
647 * suspended before the consumer.
649 dev_warn(pwm->chip->dev,
650 "No consumer device specified to create a link to\n");
654 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
656 dev_err(dev, "failed to create device link to %s\n",
657 dev_name(pwm->chip->dev));
658 return ERR_PTR(-EINVAL);
665 * of_pwm_get() - request a PWM via the PWM framework
666 * @dev: device for PWM consumer
667 * @np: device node to get the PWM from
668 * @con_id: consumer name
670 * Returns the PWM device parsed from the phandle and index specified in the
671 * "pwms" property of a device tree node or a negative error-code on failure.
672 * Values parsed from the device tree are stored in the returned PWM device
675 * If con_id is NULL, the first PWM device listed in the "pwms" property will
676 * be requested. Otherwise the "pwm-names" property is used to do a reverse
677 * lookup of the PWM index. This also means that the "pwm-names" property
678 * becomes mandatory for devices that look up the PWM device via the con_id
681 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
682 * error code on failure.
684 struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
687 struct pwm_device *pwm = NULL;
688 struct of_phandle_args args;
689 struct device_link *dl;
695 index = of_property_match_string(np, "pwm-names", con_id);
697 return ERR_PTR(index);
700 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
703 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
707 pc = of_node_to_pwmchip(args.np);
709 if (PTR_ERR(pc) != -EPROBE_DEFER)
710 pr_err("%s(): PWM chip not found\n", __func__);
716 pwm = pc->of_xlate(pc, &args);
720 dl = pwm_device_link_add(dev, pwm);
722 /* of_xlate ended up calling pwm_request_from_chip() */
729 * If a consumer name was not given, try to look it up from the
730 * "pwm-names" property if it exists. Otherwise use the name of
731 * the user device node.
734 err = of_property_read_string_index(np, "pwm-names", index,
743 of_node_put(args.np);
747 EXPORT_SYMBOL_GPL(of_pwm_get);
749 #if IS_ENABLED(CONFIG_ACPI)
750 static struct pwm_chip *device_to_pwmchip(struct device *dev)
752 struct pwm_chip *chip;
754 mutex_lock(&pwm_lock);
756 list_for_each_entry(chip, &pwm_chips, list) {
757 struct acpi_device *adev = ACPI_COMPANION(chip->dev);
759 if ((chip->dev == dev) || (adev && &adev->dev == dev)) {
760 mutex_unlock(&pwm_lock);
765 mutex_unlock(&pwm_lock);
767 return ERR_PTR(-EPROBE_DEFER);
772 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
773 * @fwnode: firmware node to get the "pwm" property from
775 * Returns the PWM device parsed from the fwnode and index specified in the
776 * "pwms" property or a negative error-code on failure.
777 * Values parsed from the device tree are stored in the returned PWM device
780 * This is analogous to of_pwm_get() except con_id is not yet supported.
781 * ACPI entries must look like
782 * Package () {"pwms", Package ()
783 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
785 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
786 * error code on failure.
788 static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode)
790 struct pwm_device *pwm = ERR_PTR(-ENODEV);
791 #if IS_ENABLED(CONFIG_ACPI)
792 struct fwnode_reference_args args;
793 struct acpi_device *acpi;
794 struct pwm_chip *chip;
797 memset(&args, 0, sizeof(args));
799 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
803 acpi = to_acpi_device_node(args.fwnode);
805 return ERR_PTR(-EINVAL);
808 return ERR_PTR(-EPROTO);
810 chip = device_to_pwmchip(&acpi->dev);
812 return ERR_CAST(chip);
814 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
818 pwm->args.period = args.args[1];
819 pwm->args.polarity = PWM_POLARITY_NORMAL;
821 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
822 pwm->args.polarity = PWM_POLARITY_INVERSED;
829 * pwm_add_table() - register PWM device consumers
830 * @table: array of consumers to register
831 * @num: number of consumers in table
833 void pwm_add_table(struct pwm_lookup *table, size_t num)
835 mutex_lock(&pwm_lookup_lock);
838 list_add_tail(&table->list, &pwm_lookup_list);
842 mutex_unlock(&pwm_lookup_lock);
846 * pwm_remove_table() - unregister PWM device consumers
847 * @table: array of consumers to unregister
848 * @num: number of consumers in table
850 void pwm_remove_table(struct pwm_lookup *table, size_t num)
852 mutex_lock(&pwm_lookup_lock);
855 list_del(&table->list);
859 mutex_unlock(&pwm_lookup_lock);
863 * pwm_get() - look up and request a PWM device
864 * @dev: device for PWM consumer
865 * @con_id: consumer name
867 * Lookup is first attempted using DT. If the device was not instantiated from
868 * a device tree, a PWM chip and a relative index is looked up via a table
869 * supplied by board setup code (see pwm_add_table()).
871 * Once a PWM chip has been found the specified PWM device will be requested
872 * and is ready to be used.
874 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
875 * error code on failure.
877 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
879 const char *dev_id = dev ? dev_name(dev) : NULL;
880 struct pwm_device *pwm;
881 struct pwm_chip *chip;
882 struct device_link *dl;
883 unsigned int best = 0;
884 struct pwm_lookup *p, *chosen = NULL;
888 /* look up via DT first */
889 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
890 return of_pwm_get(dev, dev->of_node, con_id);
892 /* then lookup via ACPI */
893 if (dev && is_acpi_node(dev->fwnode)) {
894 pwm = acpi_pwm_get(dev->fwnode);
895 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
900 * We look up the provider in the static table typically provided by
901 * board setup code. We first try to lookup the consumer device by
902 * name. If the consumer device was passed in as NULL or if no match
903 * was found, we try to find the consumer by directly looking it up
906 * If a match is found, the provider PWM chip is looked up by name
907 * and a PWM device is requested using the PWM device per-chip index.
909 * The lookup algorithm was shamelessly taken from the clock
912 * We do slightly fuzzy matching here:
913 * An entry with a NULL ID is assumed to be a wildcard.
914 * If an entry has a device ID, it must match
915 * If an entry has a connection ID, it must match
916 * Then we take the most specific entry - with the following order
917 * of precedence: dev+con > dev only > con only.
919 mutex_lock(&pwm_lookup_lock);
921 list_for_each_entry(p, &pwm_lookup_list, list) {
925 if (!dev_id || strcmp(p->dev_id, dev_id))
932 if (!con_id || strcmp(p->con_id, con_id))
948 mutex_unlock(&pwm_lookup_lock);
951 return ERR_PTR(-ENODEV);
953 chip = pwmchip_find_by_name(chosen->provider);
956 * If the lookup entry specifies a module, load the module and retry
957 * the PWM chip lookup. This can be used to work around driver load
958 * ordering issues if driver's can't be made to properly support the
959 * deferred probe mechanism.
961 if (!chip && chosen->module) {
962 err = request_module(chosen->module);
964 chip = pwmchip_find_by_name(chosen->provider);
968 return ERR_PTR(-EPROBE_DEFER);
970 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
974 dl = pwm_device_link_add(dev, pwm);
980 pwm->args.period = chosen->period;
981 pwm->args.polarity = chosen->polarity;
985 EXPORT_SYMBOL_GPL(pwm_get);
988 * pwm_put() - release a PWM device
991 void pwm_put(struct pwm_device *pwm)
996 mutex_lock(&pwm_lock);
998 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
999 pr_warn("PWM device already freed\n");
1003 if (pwm->chip->ops->free)
1004 pwm->chip->ops->free(pwm->chip, pwm);
1006 pwm_set_chip_data(pwm, NULL);
1009 module_put(pwm->chip->ops->owner);
1011 mutex_unlock(&pwm_lock);
1013 EXPORT_SYMBOL_GPL(pwm_put);
1015 static void devm_pwm_release(struct device *dev, void *res)
1017 pwm_put(*(struct pwm_device **)res);
1021 * devm_pwm_get() - resource managed pwm_get()
1022 * @dev: device for PWM consumer
1023 * @con_id: consumer name
1025 * This function performs like pwm_get() but the acquired PWM device will
1026 * automatically be released on driver detach.
1028 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1029 * error code on failure.
1031 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1033 struct pwm_device **ptr, *pwm;
1035 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1037 return ERR_PTR(-ENOMEM);
1039 pwm = pwm_get(dev, con_id);
1042 devres_add(dev, ptr);
1049 EXPORT_SYMBOL_GPL(devm_pwm_get);
1052 * devm_of_pwm_get() - resource managed of_pwm_get()
1053 * @dev: device for PWM consumer
1054 * @np: device node to get the PWM from
1055 * @con_id: consumer name
1057 * This function performs like of_pwm_get() but the acquired PWM device will
1058 * automatically be released on driver detach.
1060 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1061 * error code on failure.
1063 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
1066 struct pwm_device **ptr, *pwm;
1068 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1070 return ERR_PTR(-ENOMEM);
1072 pwm = of_pwm_get(dev, np, con_id);
1075 devres_add(dev, ptr);
1082 EXPORT_SYMBOL_GPL(devm_of_pwm_get);
1085 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1086 * @dev: device for PWM consumer
1087 * @fwnode: firmware node to get the PWM from
1088 * @con_id: consumer name
1090 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1091 * acpi_pwm_get() for a detailed description.
1093 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1094 * error code on failure.
1096 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1097 struct fwnode_handle *fwnode,
1100 struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV);
1102 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1104 return ERR_PTR(-ENOMEM);
1106 if (is_of_node(fwnode))
1107 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1108 else if (is_acpi_node(fwnode))
1109 pwm = acpi_pwm_get(fwnode);
1113 devres_add(dev, ptr);
1120 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1122 static int devm_pwm_match(struct device *dev, void *res, void *data)
1124 struct pwm_device **p = res;
1126 if (WARN_ON(!p || !*p))
1133 * devm_pwm_put() - resource managed pwm_put()
1134 * @dev: device for PWM consumer
1137 * Release a PWM previously allocated using devm_pwm_get(). Calling this
1138 * function is usually not needed because devm-allocated resources are
1139 * automatically released on driver detach.
1141 void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
1143 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
1145 EXPORT_SYMBOL_GPL(devm_pwm_put);
1147 #ifdef CONFIG_DEBUG_FS
1148 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1152 for (i = 0; i < chip->npwm; i++) {
1153 struct pwm_device *pwm = &chip->pwms[i];
1154 struct pwm_state state;
1156 pwm_get_state(pwm, &state);
1158 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1160 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1161 seq_puts(s, " requested");
1164 seq_puts(s, " enabled");
1166 seq_printf(s, " period: %u ns", state.period);
1167 seq_printf(s, " duty: %u ns", state.duty_cycle);
1168 seq_printf(s, " polarity: %s",
1169 state.polarity ? "inverse" : "normal");
1175 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1177 mutex_lock(&pwm_lock);
1180 return seq_list_start(&pwm_chips, *pos);
1183 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1187 return seq_list_next(v, &pwm_chips, pos);
1190 static void pwm_seq_stop(struct seq_file *s, void *v)
1192 mutex_unlock(&pwm_lock);
1195 static int pwm_seq_show(struct seq_file *s, void *v)
1197 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1199 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1200 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1201 dev_name(chip->dev), chip->npwm,
1202 (chip->npwm != 1) ? "s" : "");
1204 pwm_dbg_show(chip, s);
1209 static const struct seq_operations pwm_seq_ops = {
1210 .start = pwm_seq_start,
1211 .next = pwm_seq_next,
1212 .stop = pwm_seq_stop,
1213 .show = pwm_seq_show,
1216 static int pwm_seq_open(struct inode *inode, struct file *file)
1218 return seq_open(file, &pwm_seq_ops);
1221 static const struct file_operations pwm_debugfs_ops = {
1222 .owner = THIS_MODULE,
1223 .open = pwm_seq_open,
1225 .llseek = seq_lseek,
1226 .release = seq_release,
1229 static int __init pwm_debugfs_init(void)
1231 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
1236 subsys_initcall(pwm_debugfs_init);
1237 #endif /* CONFIG_DEBUG_FS */