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(unsigned int count)
44 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0,
47 if (start + count > MAX_PWMS)
53 static void free_pwms(struct pwm_chip *chip)
57 for (i = 0; i < chip->npwm; i++) {
58 struct pwm_device *pwm = &chip->pwms[i];
60 radix_tree_delete(&pwm_tree, pwm->pwm);
63 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
69 static struct pwm_chip *pwmchip_find_by_name(const char *name)
71 struct pwm_chip *chip;
76 mutex_lock(&pwm_lock);
78 list_for_each_entry(chip, &pwm_chips, list) {
79 const char *chip_name = dev_name(chip->dev);
81 if (chip_name && strcmp(chip_name, name) == 0) {
82 mutex_unlock(&pwm_lock);
87 mutex_unlock(&pwm_lock);
92 static int pwm_device_request(struct pwm_device *pwm, const char *label)
96 if (test_bit(PWMF_REQUESTED, &pwm->flags))
99 if (!try_module_get(pwm->chip->ops->owner))
102 if (pwm->chip->ops->request) {
103 err = pwm->chip->ops->request(pwm->chip, pwm);
105 module_put(pwm->chip->ops->owner);
110 if (pwm->chip->ops->get_state) {
111 pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state);
112 trace_pwm_get(pwm, &pwm->state);
114 if (IS_ENABLED(CONFIG_PWM_DEBUG))
115 pwm->last = pwm->state;
118 set_bit(PWMF_REQUESTED, &pwm->flags);
125 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
127 struct pwm_device *pwm;
129 /* check, whether the driver supports a third cell for flags */
130 if (pc->of_pwm_n_cells < 3)
131 return ERR_PTR(-EINVAL);
133 /* flags in the third cell are optional */
134 if (args->args_count < 2)
135 return ERR_PTR(-EINVAL);
137 if (args->args[0] >= pc->npwm)
138 return ERR_PTR(-EINVAL);
140 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
144 pwm->args.period = args->args[1];
145 pwm->args.polarity = PWM_POLARITY_NORMAL;
147 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
148 pwm->args.polarity = PWM_POLARITY_INVERSED;
152 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
154 static struct pwm_device *
155 of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
157 struct pwm_device *pwm;
159 /* sanity check driver support */
160 if (pc->of_pwm_n_cells < 2)
161 return ERR_PTR(-EINVAL);
163 /* all cells are required */
164 if (args->args_count != pc->of_pwm_n_cells)
165 return ERR_PTR(-EINVAL);
167 if (args->args[0] >= pc->npwm)
168 return ERR_PTR(-EINVAL);
170 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
174 pwm->args.period = args->args[1];
179 static void of_pwmchip_add(struct pwm_chip *chip)
181 if (!chip->dev || !chip->dev->of_node)
184 if (!chip->of_xlate) {
185 chip->of_xlate = of_pwm_simple_xlate;
186 chip->of_pwm_n_cells = 2;
189 of_node_get(chip->dev->of_node);
192 static void of_pwmchip_remove(struct pwm_chip *chip)
195 of_node_put(chip->dev->of_node);
199 * pwm_set_chip_data() - set private chip data for a PWM
201 * @data: pointer to chip-specific data
203 * Returns: 0 on success or a negative error code on failure.
205 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
210 pwm->chip_data = data;
214 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
217 * pwm_get_chip_data() - get private chip data for a PWM
220 * Returns: A pointer to the chip-private data for the PWM device.
222 void *pwm_get_chip_data(struct pwm_device *pwm)
224 return pwm ? pwm->chip_data : NULL;
226 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
228 static bool pwm_ops_check(const struct pwm_chip *chip)
231 const struct pwm_ops *ops = chip->ops;
233 /* driver supports legacy, non-atomic operation */
234 if (ops->config && ops->enable && ops->disable) {
235 if (IS_ENABLED(CONFIG_PWM_DEBUG))
237 "Driver needs updating to atomic API\n");
245 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
247 "Please implement the .get_state() callback\n");
253 * pwmchip_add() - register a new PWM chip
254 * @chip: the PWM chip to add
256 * Register a new PWM chip.
258 * Returns: 0 on success or a negative error code on failure.
260 int pwmchip_add(struct pwm_chip *chip)
262 struct pwm_device *pwm;
266 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
269 if (!pwm_ops_check(chip))
272 mutex_lock(&pwm_lock);
274 ret = alloc_pwms(chip->npwm);
280 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;
293 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
296 bitmap_set(allocated_pwms, chip->base, chip->npwm);
298 INIT_LIST_HEAD(&chip->list);
299 list_add(&chip->list, &pwm_chips);
303 if (IS_ENABLED(CONFIG_OF))
304 of_pwmchip_add(chip);
307 mutex_unlock(&pwm_lock);
310 pwmchip_sysfs_export(chip);
314 EXPORT_SYMBOL_GPL(pwmchip_add);
317 * pwmchip_remove() - remove a PWM chip
318 * @chip: the PWM chip to remove
320 * Removes a PWM chip. This function may return busy if the PWM chip provides
321 * a PWM device that is still requested.
323 * Returns: 0 on success or a negative error code on failure.
325 int pwmchip_remove(struct pwm_chip *chip)
330 pwmchip_sysfs_unexport(chip);
332 mutex_lock(&pwm_lock);
334 for (i = 0; i < chip->npwm; i++) {
335 struct pwm_device *pwm = &chip->pwms[i];
337 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
343 list_del_init(&chip->list);
345 if (IS_ENABLED(CONFIG_OF))
346 of_pwmchip_remove(chip);
351 mutex_unlock(&pwm_lock);
354 EXPORT_SYMBOL_GPL(pwmchip_remove);
357 * pwm_request() - request a PWM device
358 * @pwm: global PWM device index
359 * @label: PWM device label
361 * This function is deprecated, use pwm_get() instead.
363 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
366 struct pwm_device *pwm_request(int pwm, const char *label)
368 struct pwm_device *dev;
371 if (pwm < 0 || pwm >= MAX_PWMS)
372 return ERR_PTR(-EINVAL);
374 mutex_lock(&pwm_lock);
376 dev = pwm_to_device(pwm);
378 dev = ERR_PTR(-EPROBE_DEFER);
382 err = pwm_device_request(dev, label);
387 mutex_unlock(&pwm_lock);
391 EXPORT_SYMBOL_GPL(pwm_request);
394 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
396 * @index: per-chip index of the PWM to request
397 * @label: a literal description string of this PWM
399 * Returns: A pointer to the PWM device at the given index of the given PWM
400 * chip. A negative error code is returned if the index is not valid for the
401 * specified PWM chip or if the PWM device cannot be requested.
403 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
407 struct pwm_device *pwm;
410 if (!chip || index >= chip->npwm)
411 return ERR_PTR(-EINVAL);
413 mutex_lock(&pwm_lock);
414 pwm = &chip->pwms[index];
416 err = pwm_device_request(pwm, label);
420 mutex_unlock(&pwm_lock);
423 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
426 * pwm_free() - free a PWM device
429 * This function is deprecated, use pwm_put() instead.
431 void pwm_free(struct pwm_device *pwm)
435 EXPORT_SYMBOL_GPL(pwm_free);
437 static void pwm_apply_state_debug(struct pwm_device *pwm,
438 const struct pwm_state *state)
440 struct pwm_state *last = &pwm->last;
441 struct pwm_chip *chip = pwm->chip;
442 struct pwm_state s1, s2;
445 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
448 /* No reasonable diagnosis possible without .get_state() */
449 if (!chip->ops->get_state)
453 * *state was just applied. Read out the hardware state and do some
457 chip->ops->get_state(chip, pwm, &s1);
458 trace_pwm_get(pwm, &s1);
461 * The lowlevel driver either ignored .polarity (which is a bug) or as
462 * best effort inverted .polarity and fixed .duty_cycle respectively.
463 * Undo this inversion and fixup for further tests.
465 if (s1.enabled && s1.polarity != state->polarity) {
466 s2.polarity = state->polarity;
467 s2.duty_cycle = s1.period - s1.duty_cycle;
468 s2.period = s1.period;
469 s2.enabled = s1.enabled;
474 if (s2.polarity != state->polarity &&
475 state->duty_cycle < state->period)
476 dev_warn(chip->dev, ".apply ignored .polarity\n");
478 if (state->enabled &&
479 last->polarity == state->polarity &&
480 last->period > s2.period &&
481 last->period <= state->period)
483 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
484 state->period, s2.period, last->period);
486 if (state->enabled && state->period < s2.period)
488 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
489 state->period, s2.period);
491 if (state->enabled &&
492 last->polarity == state->polarity &&
493 last->period == s2.period &&
494 last->duty_cycle > s2.duty_cycle &&
495 last->duty_cycle <= state->duty_cycle)
497 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
498 state->duty_cycle, state->period,
499 s2.duty_cycle, s2.period,
500 last->duty_cycle, last->period);
502 if (state->enabled && state->duty_cycle < s2.duty_cycle)
504 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
505 state->duty_cycle, state->period,
506 s2.duty_cycle, s2.period);
508 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
510 "requested disabled, but yielded enabled with duty > 0\n");
512 /* reapply the state that the driver reported being configured. */
513 err = chip->ops->apply(chip, pwm, &s1);
516 dev_err(chip->dev, "failed to reapply current setting\n");
520 trace_pwm_apply(pwm, &s1);
522 chip->ops->get_state(chip, pwm, last);
523 trace_pwm_get(pwm, last);
525 /* reapplication of the current state should give an exact match */
526 if (s1.enabled != last->enabled ||
527 s1.polarity != last->polarity ||
528 (s1.enabled && s1.period != last->period) ||
529 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
531 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
532 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
533 last->enabled, last->polarity, last->duty_cycle,
539 * pwm_apply_state() - atomically apply a new state to a PWM device
541 * @state: new state to apply
543 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
545 struct pwm_chip *chip;
548 if (!pwm || !state || !state->period ||
549 state->duty_cycle > state->period)
554 if (state->period == pwm->state.period &&
555 state->duty_cycle == pwm->state.duty_cycle &&
556 state->polarity == pwm->state.polarity &&
557 state->enabled == pwm->state.enabled)
560 if (chip->ops->apply) {
561 err = chip->ops->apply(chip, pwm, state);
565 trace_pwm_apply(pwm, state);
570 * only do this after pwm->state was applied as some
571 * implementations of .get_state depend on this
573 pwm_apply_state_debug(pwm, state);
576 * FIXME: restore the initial state in case of error.
578 if (state->polarity != pwm->state.polarity) {
579 if (!chip->ops->set_polarity)
583 * Changing the polarity of a running PWM is
584 * only allowed when the PWM driver implements
587 if (pwm->state.enabled) {
588 chip->ops->disable(chip, pwm);
589 pwm->state.enabled = false;
592 err = chip->ops->set_polarity(chip, pwm,
597 pwm->state.polarity = state->polarity;
600 if (state->period != pwm->state.period ||
601 state->duty_cycle != pwm->state.duty_cycle) {
602 err = chip->ops->config(pwm->chip, pwm,
608 pwm->state.duty_cycle = state->duty_cycle;
609 pwm->state.period = state->period;
612 if (state->enabled != pwm->state.enabled) {
613 if (state->enabled) {
614 err = chip->ops->enable(chip, pwm);
618 chip->ops->disable(chip, pwm);
621 pwm->state.enabled = state->enabled;
627 EXPORT_SYMBOL_GPL(pwm_apply_state);
630 * pwm_capture() - capture and report a PWM signal
632 * @result: structure to fill with capture result
633 * @timeout: time to wait, in milliseconds, before giving up on capture
635 * Returns: 0 on success or a negative error code on failure.
637 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
638 unsigned long timeout)
642 if (!pwm || !pwm->chip->ops)
645 if (!pwm->chip->ops->capture)
648 mutex_lock(&pwm_lock);
649 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
650 mutex_unlock(&pwm_lock);
654 EXPORT_SYMBOL_GPL(pwm_capture);
657 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
660 * This function will adjust the PWM config to the PWM arguments provided
661 * by the DT or PWM lookup table. This is particularly useful to adapt
662 * the bootloader config to the Linux one.
664 int pwm_adjust_config(struct pwm_device *pwm)
666 struct pwm_state state;
667 struct pwm_args pargs;
669 pwm_get_args(pwm, &pargs);
670 pwm_get_state(pwm, &state);
673 * If the current period is zero it means that either the PWM driver
674 * does not support initial state retrieval or the PWM has not yet
677 * In either case, we setup the new period and polarity, and assign a
681 state.duty_cycle = 0;
682 state.period = pargs.period;
683 state.polarity = pargs.polarity;
685 return pwm_apply_state(pwm, &state);
689 * Adjust the PWM duty cycle/period based on the period value provided
692 if (pargs.period != state.period) {
693 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
695 do_div(dutycycle, state.period);
696 state.duty_cycle = dutycycle;
697 state.period = pargs.period;
701 * If the polarity changed, we should also change the duty cycle.
703 if (pargs.polarity != state.polarity) {
704 state.polarity = pargs.polarity;
705 state.duty_cycle = state.period - state.duty_cycle;
708 return pwm_apply_state(pwm, &state);
710 EXPORT_SYMBOL_GPL(pwm_adjust_config);
712 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
714 struct pwm_chip *chip;
716 mutex_lock(&pwm_lock);
718 list_for_each_entry(chip, &pwm_chips, list)
719 if (chip->dev && chip->dev->of_node == np) {
720 mutex_unlock(&pwm_lock);
724 mutex_unlock(&pwm_lock);
726 return ERR_PTR(-EPROBE_DEFER);
729 static struct device_link *pwm_device_link_add(struct device *dev,
730 struct pwm_device *pwm)
732 struct device_link *dl;
736 * No device for the PWM consumer has been provided. It may
737 * impact the PM sequence ordering: the PWM supplier may get
738 * suspended before the consumer.
740 dev_warn(pwm->chip->dev,
741 "No consumer device specified to create a link to\n");
745 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
747 dev_err(dev, "failed to create device link to %s\n",
748 dev_name(pwm->chip->dev));
749 return ERR_PTR(-EINVAL);
756 * of_pwm_get() - request a PWM via the PWM framework
757 * @dev: device for PWM consumer
758 * @np: device node to get the PWM from
759 * @con_id: consumer name
761 * Returns the PWM device parsed from the phandle and index specified in the
762 * "pwms" property of a device tree node or a negative error-code on failure.
763 * Values parsed from the device tree are stored in the returned PWM device
766 * If con_id is NULL, the first PWM device listed in the "pwms" property will
767 * be requested. Otherwise the "pwm-names" property is used to do a reverse
768 * lookup of the PWM index. This also means that the "pwm-names" property
769 * becomes mandatory for devices that look up the PWM device via the con_id
772 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
773 * error code on failure.
775 struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
778 struct pwm_device *pwm = NULL;
779 struct of_phandle_args args;
780 struct device_link *dl;
786 index = of_property_match_string(np, "pwm-names", con_id);
788 return ERR_PTR(index);
791 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
794 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
798 pc = of_node_to_pwmchip(args.np);
800 if (PTR_ERR(pc) != -EPROBE_DEFER)
801 pr_err("%s(): PWM chip not found\n", __func__);
807 pwm = pc->of_xlate(pc, &args);
811 dl = pwm_device_link_add(dev, pwm);
813 /* of_xlate ended up calling pwm_request_from_chip() */
820 * If a consumer name was not given, try to look it up from the
821 * "pwm-names" property if it exists. Otherwise use the name of
822 * the user device node.
825 err = of_property_read_string_index(np, "pwm-names", index,
834 of_node_put(args.np);
838 EXPORT_SYMBOL_GPL(of_pwm_get);
840 #if IS_ENABLED(CONFIG_ACPI)
841 static struct pwm_chip *device_to_pwmchip(struct device *dev)
843 struct pwm_chip *chip;
845 mutex_lock(&pwm_lock);
847 list_for_each_entry(chip, &pwm_chips, list) {
848 struct acpi_device *adev = ACPI_COMPANION(chip->dev);
850 if ((chip->dev == dev) || (adev && &adev->dev == dev)) {
851 mutex_unlock(&pwm_lock);
856 mutex_unlock(&pwm_lock);
858 return ERR_PTR(-EPROBE_DEFER);
863 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
864 * @fwnode: firmware node to get the "pwm" property from
866 * Returns the PWM device parsed from the fwnode and index specified in the
867 * "pwms" property or a negative error-code on failure.
868 * Values parsed from the device tree are stored in the returned PWM device
871 * This is analogous to of_pwm_get() except con_id is not yet supported.
872 * ACPI entries must look like
873 * Package () {"pwms", Package ()
874 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
876 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
877 * error code on failure.
879 static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode)
881 struct pwm_device *pwm = ERR_PTR(-ENODEV);
882 #if IS_ENABLED(CONFIG_ACPI)
883 struct fwnode_reference_args args;
884 struct acpi_device *acpi;
885 struct pwm_chip *chip;
888 memset(&args, 0, sizeof(args));
890 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
894 acpi = to_acpi_device_node(args.fwnode);
896 return ERR_PTR(-EINVAL);
899 return ERR_PTR(-EPROTO);
901 chip = device_to_pwmchip(&acpi->dev);
903 return ERR_CAST(chip);
905 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
909 pwm->args.period = args.args[1];
910 pwm->args.polarity = PWM_POLARITY_NORMAL;
912 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
913 pwm->args.polarity = PWM_POLARITY_INVERSED;
920 * pwm_add_table() - register PWM device consumers
921 * @table: array of consumers to register
922 * @num: number of consumers in table
924 void pwm_add_table(struct pwm_lookup *table, size_t num)
926 mutex_lock(&pwm_lookup_lock);
929 list_add_tail(&table->list, &pwm_lookup_list);
933 mutex_unlock(&pwm_lookup_lock);
937 * pwm_remove_table() - unregister PWM device consumers
938 * @table: array of consumers to unregister
939 * @num: number of consumers in table
941 void pwm_remove_table(struct pwm_lookup *table, size_t num)
943 mutex_lock(&pwm_lookup_lock);
946 list_del(&table->list);
950 mutex_unlock(&pwm_lookup_lock);
954 * pwm_get() - look up and request a PWM device
955 * @dev: device for PWM consumer
956 * @con_id: consumer name
958 * Lookup is first attempted using DT. If the device was not instantiated from
959 * a device tree, a PWM chip and a relative index is looked up via a table
960 * supplied by board setup code (see pwm_add_table()).
962 * Once a PWM chip has been found the specified PWM device will be requested
963 * and is ready to be used.
965 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
966 * error code on failure.
968 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
970 const char *dev_id = dev ? dev_name(dev) : NULL;
971 struct pwm_device *pwm;
972 struct pwm_chip *chip;
973 struct device_link *dl;
974 unsigned int best = 0;
975 struct pwm_lookup *p, *chosen = NULL;
979 /* look up via DT first */
980 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
981 return of_pwm_get(dev, dev->of_node, con_id);
983 /* then lookup via ACPI */
984 if (dev && is_acpi_node(dev->fwnode)) {
985 pwm = acpi_pwm_get(dev->fwnode);
986 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
991 * We look up the provider in the static table typically provided by
992 * board setup code. We first try to lookup the consumer device by
993 * name. If the consumer device was passed in as NULL or if no match
994 * was found, we try to find the consumer by directly looking it up
997 * If a match is found, the provider PWM chip is looked up by name
998 * and a PWM device is requested using the PWM device per-chip index.
1000 * The lookup algorithm was shamelessly taken from the clock
1003 * We do slightly fuzzy matching here:
1004 * An entry with a NULL ID is assumed to be a wildcard.
1005 * If an entry has a device ID, it must match
1006 * If an entry has a connection ID, it must match
1007 * Then we take the most specific entry - with the following order
1008 * of precedence: dev+con > dev only > con only.
1010 mutex_lock(&pwm_lookup_lock);
1012 list_for_each_entry(p, &pwm_lookup_list, list) {
1016 if (!dev_id || strcmp(p->dev_id, dev_id))
1023 if (!con_id || strcmp(p->con_id, con_id))
1039 mutex_unlock(&pwm_lookup_lock);
1042 return ERR_PTR(-ENODEV);
1044 chip = pwmchip_find_by_name(chosen->provider);
1047 * If the lookup entry specifies a module, load the module and retry
1048 * the PWM chip lookup. This can be used to work around driver load
1049 * ordering issues if driver's can't be made to properly support the
1050 * deferred probe mechanism.
1052 if (!chip && chosen->module) {
1053 err = request_module(chosen->module);
1055 chip = pwmchip_find_by_name(chosen->provider);
1059 return ERR_PTR(-EPROBE_DEFER);
1061 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1065 dl = pwm_device_link_add(dev, pwm);
1068 return ERR_CAST(dl);
1071 pwm->args.period = chosen->period;
1072 pwm->args.polarity = chosen->polarity;
1076 EXPORT_SYMBOL_GPL(pwm_get);
1079 * pwm_put() - release a PWM device
1082 void pwm_put(struct pwm_device *pwm)
1087 mutex_lock(&pwm_lock);
1089 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1090 pr_warn("PWM device already freed\n");
1094 if (pwm->chip->ops->free)
1095 pwm->chip->ops->free(pwm->chip, pwm);
1097 pwm_set_chip_data(pwm, NULL);
1100 module_put(pwm->chip->ops->owner);
1102 mutex_unlock(&pwm_lock);
1104 EXPORT_SYMBOL_GPL(pwm_put);
1106 static void devm_pwm_release(struct device *dev, void *res)
1108 pwm_put(*(struct pwm_device **)res);
1112 * devm_pwm_get() - resource managed pwm_get()
1113 * @dev: device for PWM consumer
1114 * @con_id: consumer name
1116 * This function performs like pwm_get() but the acquired PWM device will
1117 * automatically be released on driver detach.
1119 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1120 * error code on failure.
1122 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1124 struct pwm_device **ptr, *pwm;
1126 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1128 return ERR_PTR(-ENOMEM);
1130 pwm = pwm_get(dev, con_id);
1133 devres_add(dev, ptr);
1140 EXPORT_SYMBOL_GPL(devm_pwm_get);
1143 * devm_of_pwm_get() - resource managed of_pwm_get()
1144 * @dev: device for PWM consumer
1145 * @np: device node to get the PWM from
1146 * @con_id: consumer name
1148 * This function performs like of_pwm_get() but the acquired PWM device will
1149 * automatically be released on driver detach.
1151 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1152 * error code on failure.
1154 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
1157 struct pwm_device **ptr, *pwm;
1159 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1161 return ERR_PTR(-ENOMEM);
1163 pwm = of_pwm_get(dev, np, con_id);
1166 devres_add(dev, ptr);
1173 EXPORT_SYMBOL_GPL(devm_of_pwm_get);
1176 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1177 * @dev: device for PWM consumer
1178 * @fwnode: firmware node to get the PWM from
1179 * @con_id: consumer name
1181 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1182 * acpi_pwm_get() for a detailed description.
1184 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1185 * error code on failure.
1187 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1188 struct fwnode_handle *fwnode,
1191 struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV);
1193 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1195 return ERR_PTR(-ENOMEM);
1197 if (is_of_node(fwnode))
1198 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1199 else if (is_acpi_node(fwnode))
1200 pwm = acpi_pwm_get(fwnode);
1204 devres_add(dev, ptr);
1211 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1213 static int devm_pwm_match(struct device *dev, void *res, void *data)
1215 struct pwm_device **p = res;
1217 if (WARN_ON(!p || !*p))
1224 * devm_pwm_put() - resource managed pwm_put()
1225 * @dev: device for PWM consumer
1228 * Release a PWM previously allocated using devm_pwm_get(). Calling this
1229 * function is usually not needed because devm-allocated resources are
1230 * automatically released on driver detach.
1232 void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
1234 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
1236 EXPORT_SYMBOL_GPL(devm_pwm_put);
1238 #ifdef CONFIG_DEBUG_FS
1239 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1243 for (i = 0; i < chip->npwm; i++) {
1244 struct pwm_device *pwm = &chip->pwms[i];
1245 struct pwm_state state;
1247 pwm_get_state(pwm, &state);
1249 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1251 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1252 seq_puts(s, " requested");
1255 seq_puts(s, " enabled");
1257 seq_printf(s, " period: %llu ns", state.period);
1258 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1259 seq_printf(s, " polarity: %s",
1260 state.polarity ? "inverse" : "normal");
1266 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1268 mutex_lock(&pwm_lock);
1271 return seq_list_start(&pwm_chips, *pos);
1274 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1278 return seq_list_next(v, &pwm_chips, pos);
1281 static void pwm_seq_stop(struct seq_file *s, void *v)
1283 mutex_unlock(&pwm_lock);
1286 static int pwm_seq_show(struct seq_file *s, void *v)
1288 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1290 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1291 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1292 dev_name(chip->dev), chip->npwm,
1293 (chip->npwm != 1) ? "s" : "");
1295 pwm_dbg_show(chip, s);
1300 static const struct seq_operations pwm_debugfs_sops = {
1301 .start = pwm_seq_start,
1302 .next = pwm_seq_next,
1303 .stop = pwm_seq_stop,
1304 .show = pwm_seq_show,
1307 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1309 static int __init pwm_debugfs_init(void)
1311 debugfs_create_file("pwm", S_IFREG | 0444, NULL, NULL,
1316 subsys_initcall(pwm_debugfs_init);
1317 #endif /* CONFIG_DEBUG_FS */