Lines Matching +full:- +full:pwm

1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2011-2012 Avionic Design GmbH
11 #include <linux/pwm.h>
12 #include <linux/radix-tree.h>
21 #include <dt-bindings/pwm/pwm.h>
24 #include <trace/events/pwm.h>
35 static struct pwm_device *pwm_to_device(unsigned int pwm) in pwm_to_device() argument
37 return radix_tree_lookup(&pwm_tree, pwm); in pwm_to_device()
40 static int alloc_pwms(int pwm, unsigned int count) in alloc_pwms() argument
45 if (pwm >= MAX_PWMS) in alloc_pwms()
46 return -EINVAL; in alloc_pwms()
48 if (pwm >= 0) in alloc_pwms()
49 from = pwm; in alloc_pwms()
54 if (pwm >= 0 && start != pwm) in alloc_pwms()
55 return -EEXIST; in alloc_pwms()
58 return -ENOSPC; in alloc_pwms()
67 for (i = 0; i < chip->npwm; i++) { in free_pwms()
68 struct pwm_device *pwm = &chip->pwms[i]; in free_pwms() local
70 radix_tree_delete(&pwm_tree, pwm->pwm); in free_pwms()
73 bitmap_clear(allocated_pwms, chip->base, chip->npwm); in free_pwms()
75 kfree(chip->pwms); in free_pwms()
76 chip->pwms = NULL; in free_pwms()
89 const char *chip_name = dev_name(chip->dev); in pwmchip_find_by_name()
102 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
106 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
107 return -EBUSY; in pwm_device_request()
109 if (!try_module_get(pwm->chip->ops->owner)) in pwm_device_request()
110 return -ENODEV; in pwm_device_request()
112 if (pwm->chip->ops->request) { in pwm_device_request()
113 err = pwm->chip->ops->request(pwm->chip, pwm); in pwm_device_request()
115 module_put(pwm->chip->ops->owner); in pwm_device_request()
120 if (pwm->chip->ops->get_state) { in pwm_device_request()
121 pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state); in pwm_device_request()
122 trace_pwm_get(pwm, &pwm->state); in pwm_device_request()
125 pwm->last = pwm->state; in pwm_device_request()
128 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
129 pwm->label = label; in pwm_device_request()
137 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
140 if (pc->of_pwm_n_cells < 3) in of_pwm_xlate_with_flags()
141 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
144 if (args->args_count < 2) in of_pwm_xlate_with_flags()
145 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
147 if (args->args[0] >= pc->npwm) in of_pwm_xlate_with_flags()
148 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
150 pwm = pwm_request_from_chip(pc, args->args[0], NULL); in of_pwm_xlate_with_flags()
151 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
152 return pwm; in of_pwm_xlate_with_flags()
154 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
155 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
157 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
158 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
160 return pwm; in of_pwm_xlate_with_flags()
167 struct pwm_device *pwm; in of_pwm_simple_xlate() local
170 if (pc->of_pwm_n_cells < 2) in of_pwm_simple_xlate()
171 return ERR_PTR(-EINVAL); in of_pwm_simple_xlate()
174 if (args->args_count != pc->of_pwm_n_cells) in of_pwm_simple_xlate()
175 return ERR_PTR(-EINVAL); in of_pwm_simple_xlate()
177 if (args->args[0] >= pc->npwm) in of_pwm_simple_xlate()
178 return ERR_PTR(-EINVAL); in of_pwm_simple_xlate()
180 pwm = pwm_request_from_chip(pc, args->args[0], NULL); in of_pwm_simple_xlate()
181 if (IS_ERR(pwm)) in of_pwm_simple_xlate()
182 return pwm; in of_pwm_simple_xlate()
184 pwm->args.period = args->args[1]; in of_pwm_simple_xlate()
186 return pwm; in of_pwm_simple_xlate()
191 if (!chip->dev || !chip->dev->of_node) in of_pwmchip_add()
194 if (!chip->of_xlate) { in of_pwmchip_add()
195 chip->of_xlate = of_pwm_simple_xlate; in of_pwmchip_add()
196 chip->of_pwm_n_cells = 2; in of_pwmchip_add()
199 of_node_get(chip->dev->of_node); in of_pwmchip_add()
204 if (chip->dev) in of_pwmchip_remove()
205 of_node_put(chip->dev->of_node); in of_pwmchip_remove()
209 * pwm_set_chip_data() - set private chip data for a PWM
210 * @pwm: PWM device
211 * @data: pointer to chip-specific data
215 int pwm_set_chip_data(struct pwm_device *pwm, void *data) in pwm_set_chip_data() argument
217 if (!pwm) in pwm_set_chip_data()
218 return -EINVAL; in pwm_set_chip_data()
220 pwm->chip_data = data; in pwm_set_chip_data()
227 * pwm_get_chip_data() - get private chip data for a PWM
228 * @pwm: PWM device
230 * Returns: A pointer to the chip-private data for the PWM device.
232 void *pwm_get_chip_data(struct pwm_device *pwm) in pwm_get_chip_data() argument
234 return pwm ? pwm->chip_data : NULL; in pwm_get_chip_data()
241 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
243 /* driver supports legacy, non-atomic operation */ in pwm_ops_check()
244 if (ops->config && ops->enable && ops->disable) { in pwm_ops_check()
246 dev_warn(chip->dev, in pwm_ops_check()
252 if (!ops->apply) in pwm_ops_check()
255 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
256 dev_warn(chip->dev, in pwm_ops_check()
263 * pwmchip_add_with_polarity() - register a new PWM chip
264 * @chip: the PWM chip to add
265 * @polarity: initial polarity of PWM channels
267 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
276 struct pwm_device *pwm; in pwmchip_add_with_polarity() local
280 if (!chip || !chip->dev || !chip->ops || !chip->npwm) in pwmchip_add_with_polarity()
281 return -EINVAL; in pwmchip_add_with_polarity()
284 return -EINVAL; in pwmchip_add_with_polarity()
288 ret = alloc_pwms(chip->base, chip->npwm); in pwmchip_add_with_polarity()
292 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); in pwmchip_add_with_polarity()
293 if (!chip->pwms) { in pwmchip_add_with_polarity()
294 ret = -ENOMEM; in pwmchip_add_with_polarity()
298 chip->base = ret; in pwmchip_add_with_polarity()
300 for (i = 0; i < chip->npwm; i++) { in pwmchip_add_with_polarity()
301 pwm = &chip->pwms[i]; in pwmchip_add_with_polarity()
303 pwm->chip = chip; in pwmchip_add_with_polarity()
304 pwm->pwm = chip->base + i; in pwmchip_add_with_polarity()
305 pwm->hwpwm = i; in pwmchip_add_with_polarity()
306 pwm->state.polarity = polarity; in pwmchip_add_with_polarity()
308 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); in pwmchip_add_with_polarity()
311 bitmap_set(allocated_pwms, chip->base, chip->npwm); in pwmchip_add_with_polarity()
313 INIT_LIST_HEAD(&chip->list); in pwmchip_add_with_polarity()
314 list_add(&chip->list, &pwm_chips); in pwmchip_add_with_polarity()
332 * pwmchip_add() - register a new PWM chip
333 * @chip: the PWM chip to add
335 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
347 * pwmchip_remove() - remove a PWM chip
348 * @chip: the PWM chip to remove
350 * Removes a PWM chip. This function may return busy if the PWM chip provides
351 * a PWM device that is still requested.
364 for (i = 0; i < chip->npwm; i++) { in pwmchip_remove()
365 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_remove() local
367 if (test_bit(PWMF_REQUESTED, &pwm->flags)) { in pwmchip_remove()
368 ret = -EBUSY; in pwmchip_remove()
373 list_del_init(&chip->list); in pwmchip_remove()
387 * pwm_request() - request a PWM device
388 * @pwm: global PWM device index
389 * @label: PWM device label
393 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
396 struct pwm_device *pwm_request(int pwm, const char *label) in pwm_request() argument
401 if (pwm < 0 || pwm >= MAX_PWMS) in pwm_request()
402 return ERR_PTR(-EINVAL); in pwm_request()
406 dev = pwm_to_device(pwm); in pwm_request()
408 dev = ERR_PTR(-EPROBE_DEFER); in pwm_request()
424 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
425 * @chip: PWM chip
426 * @index: per-chip index of the PWM to request
427 * @label: a literal description string of this PWM
429 * Returns: A pointer to the PWM device at the given index of the given PWM
431 * specified PWM chip or if the PWM device cannot be requested.
437 struct pwm_device *pwm; in pwm_request_from_chip() local
440 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
441 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
444 pwm = &chip->pwms[index]; in pwm_request_from_chip()
446 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
448 pwm = ERR_PTR(err); in pwm_request_from_chip()
451 return pwm; in pwm_request_from_chip()
456 * pwm_free() - free a PWM device
457 * @pwm: PWM device
461 void pwm_free(struct pwm_device *pwm) in pwm_free() argument
463 pwm_put(pwm); in pwm_free()
467 static void pwm_apply_state_debug(struct pwm_device *pwm, in pwm_apply_state_debug() argument
470 struct pwm_state *last = &pwm->last; in pwm_apply_state_debug()
471 struct pwm_chip *chip = pwm->chip; in pwm_apply_state_debug()
479 if (!chip->ops->get_state) in pwm_apply_state_debug()
487 chip->ops->get_state(chip, pwm, &s1); in pwm_apply_state_debug()
488 trace_pwm_get(pwm, &s1); in pwm_apply_state_debug()
495 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_state_debug()
496 s2.polarity = state->polarity; in pwm_apply_state_debug()
497 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_state_debug()
504 if (s2.polarity != state->polarity && in pwm_apply_state_debug()
505 state->duty_cycle < state->period) in pwm_apply_state_debug()
506 dev_warn(chip->dev, ".apply ignored .polarity\n"); in pwm_apply_state_debug()
508 if (state->enabled && in pwm_apply_state_debug()
509 last->polarity == state->polarity && in pwm_apply_state_debug()
510 last->period > s2.period && in pwm_apply_state_debug()
511 last->period <= state->period) in pwm_apply_state_debug()
512 dev_warn(chip->dev, in pwm_apply_state_debug()
514 state->period, s2.period, last->period); in pwm_apply_state_debug()
516 if (state->enabled && state->period < s2.period) in pwm_apply_state_debug()
517 dev_warn(chip->dev, in pwm_apply_state_debug()
519 state->period, s2.period); in pwm_apply_state_debug()
521 if (state->enabled && in pwm_apply_state_debug()
522 last->polarity == state->polarity && in pwm_apply_state_debug()
523 last->period == s2.period && in pwm_apply_state_debug()
524 last->duty_cycle > s2.duty_cycle && in pwm_apply_state_debug()
525 last->duty_cycle <= state->duty_cycle) in pwm_apply_state_debug()
526 dev_warn(chip->dev, in pwm_apply_state_debug()
528 state->duty_cycle, state->period, in pwm_apply_state_debug()
530 last->duty_cycle, last->period); in pwm_apply_state_debug()
532 if (state->enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_state_debug()
533 dev_warn(chip->dev, in pwm_apply_state_debug()
535 state->duty_cycle, state->period, in pwm_apply_state_debug()
538 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_state_debug()
539 dev_warn(chip->dev, in pwm_apply_state_debug()
543 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_state_debug()
546 dev_err(chip->dev, "failed to reapply current setting\n"); in pwm_apply_state_debug()
550 trace_pwm_apply(pwm, &s1); in pwm_apply_state_debug()
552 chip->ops->get_state(chip, pwm, last); in pwm_apply_state_debug()
553 trace_pwm_get(pwm, last); in pwm_apply_state_debug()
556 if (s1.enabled != last->enabled || in pwm_apply_state_debug()
557 s1.polarity != last->polarity || in pwm_apply_state_debug()
558 (s1.enabled && s1.period != last->period) || in pwm_apply_state_debug()
559 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_state_debug()
560 dev_err(chip->dev, in pwm_apply_state_debug()
561 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_state_debug()
563 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_state_debug()
564 last->period); in pwm_apply_state_debug()
569 * pwm_apply_state() - atomically apply a new state to a PWM device
570 * @pwm: PWM device
573 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_state() argument
578 if (!pwm || !state || !state->period || in pwm_apply_state()
579 state->duty_cycle > state->period) in pwm_apply_state()
580 return -EINVAL; in pwm_apply_state()
582 chip = pwm->chip; in pwm_apply_state()
584 if (state->period == pwm->state.period && in pwm_apply_state()
585 state->duty_cycle == pwm->state.duty_cycle && in pwm_apply_state()
586 state->polarity == pwm->state.polarity && in pwm_apply_state()
587 state->enabled == pwm->state.enabled) in pwm_apply_state()
590 if (chip->ops->apply) { in pwm_apply_state()
591 err = chip->ops->apply(chip, pwm, state); in pwm_apply_state()
595 trace_pwm_apply(pwm, state); in pwm_apply_state()
597 pwm->state = *state; in pwm_apply_state()
600 * only do this after pwm->state was applied as some in pwm_apply_state()
603 pwm_apply_state_debug(pwm, state); in pwm_apply_state()
608 if (state->polarity != pwm->state.polarity) { in pwm_apply_state()
609 if (!chip->ops->set_polarity) in pwm_apply_state()
610 return -ENOTSUPP; in pwm_apply_state()
613 * Changing the polarity of a running PWM is in pwm_apply_state()
614 * only allowed when the PWM driver implements in pwm_apply_state()
615 * ->apply(). in pwm_apply_state()
617 if (pwm->state.enabled) { in pwm_apply_state()
618 chip->ops->disable(chip, pwm); in pwm_apply_state()
619 pwm->state.enabled = false; in pwm_apply_state()
622 err = chip->ops->set_polarity(chip, pwm, in pwm_apply_state()
623 state->polarity); in pwm_apply_state()
627 pwm->state.polarity = state->polarity; in pwm_apply_state()
630 if (state->period != pwm->state.period || in pwm_apply_state()
631 state->duty_cycle != pwm->state.duty_cycle) { in pwm_apply_state()
632 err = chip->ops->config(pwm->chip, pwm, in pwm_apply_state()
633 state->duty_cycle, in pwm_apply_state()
634 state->period); in pwm_apply_state()
638 pwm->state.duty_cycle = state->duty_cycle; in pwm_apply_state()
639 pwm->state.period = state->period; in pwm_apply_state()
642 if (state->enabled != pwm->state.enabled) { in pwm_apply_state()
643 if (state->enabled) { in pwm_apply_state()
644 err = chip->ops->enable(chip, pwm); in pwm_apply_state()
648 chip->ops->disable(chip, pwm); in pwm_apply_state()
651 pwm->state.enabled = state->enabled; in pwm_apply_state()
660 * pwm_capture() - capture and report a PWM signal
661 * @pwm: PWM device
667 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
672 if (!pwm || !pwm->chip->ops) in pwm_capture()
673 return -EINVAL; in pwm_capture()
675 if (!pwm->chip->ops->capture) in pwm_capture()
676 return -ENOSYS; in pwm_capture()
679 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); in pwm_capture()
687 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
688 * @pwm: PWM device
690 * This function will adjust the PWM config to the PWM arguments provided
691 * by the DT or PWM lookup table. This is particularly useful to adapt
694 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
699 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
700 pwm_get_state(pwm, &state); in pwm_adjust_config()
703 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
704 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
715 return pwm_apply_state(pwm, &state); in pwm_adjust_config()
719 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
720 * in PWM args. in pwm_adjust_config()
735 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
738 return pwm_apply_state(pwm, &state); in pwm_adjust_config()
749 if (chip->dev && chip->dev->of_node == np) { in of_node_to_pwmchip()
756 return ERR_PTR(-EPROBE_DEFER); in of_node_to_pwmchip()
760 struct pwm_device *pwm) in pwm_device_link_add() argument
766 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
767 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
770 dev_warn(pwm->chip->dev, in pwm_device_link_add()
775 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
778 dev_name(pwm->chip->dev)); in pwm_device_link_add()
779 return ERR_PTR(-EINVAL); in pwm_device_link_add()
786 * of_pwm_get() - request a PWM via the PWM framework
787 * @dev: device for PWM consumer
788 * @np: device node to get the PWM from
791 * Returns the PWM device parsed from the phandle and index specified in the
792 * "pwms" property of a device tree node or a negative error-code on failure.
793 * Values parsed from the device tree are stored in the returned PWM device
796 * If con_id is NULL, the first PWM device listed in the "pwms" property will
797 * be requested. Otherwise the "pwm-names" property is used to do a reverse
798 * lookup of the PWM index. This also means that the "pwm-names" property
799 * becomes mandatory for devices that look up the PWM device via the con_id
802 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
808 struct pwm_device *pwm = NULL; in of_pwm_get() local
816 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
821 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
830 if (PTR_ERR(pc) != -EPROBE_DEFER) in of_pwm_get()
831 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
833 pwm = ERR_CAST(pc); in of_pwm_get()
837 pwm = pc->of_xlate(pc, &args); in of_pwm_get()
838 if (IS_ERR(pwm)) in of_pwm_get()
841 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
844 pwm_free(pwm); in of_pwm_get()
845 pwm = ERR_CAST(dl); in of_pwm_get()
851 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
855 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
858 con_id = np->name; in of_pwm_get()
861 pwm->label = con_id; in of_pwm_get()
866 return pwm; in of_pwm_get()
878 struct acpi_device *adev = ACPI_COMPANION(chip->dev); in device_to_pwmchip()
880 if ((chip->dev == dev) || (adev && &adev->dev == dev)) { in device_to_pwmchip()
888 return ERR_PTR(-EPROBE_DEFER); in device_to_pwmchip()
893 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
894 * @fwnode: firmware node to get the "pwm" property from
896 * Returns the PWM device parsed from the fwnode and index specified in the
897 * "pwms" property or a negative error-code on failure.
898 * Values parsed from the device tree are stored in the returned PWM device
904 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
906 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
911 struct pwm_device *pwm = ERR_PTR(-ENODEV); in acpi_pwm_get() local
926 return ERR_PTR(-EINVAL); in acpi_pwm_get()
929 return ERR_PTR(-EPROTO); in acpi_pwm_get()
931 chip = device_to_pwmchip(&acpi->dev); in acpi_pwm_get()
935 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
936 if (IS_ERR(pwm)) in acpi_pwm_get()
937 return pwm; in acpi_pwm_get()
939 pwm->args.period = args.args[1]; in acpi_pwm_get()
940 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
943 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
946 return pwm; in acpi_pwm_get()
950 * pwm_add_table() - register PWM device consumers
958 while (num--) { in pwm_add_table()
959 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
967 * pwm_remove_table() - unregister PWM device consumers
975 while (num--) { in pwm_remove_table()
976 list_del(&table->list); in pwm_remove_table()
984 * pwm_get() - look up and request a PWM device
985 * @dev: device for PWM consumer
989 * a device tree, a PWM chip and a relative index is looked up via a table
992 * Once a PWM chip has been found the specified PWM device will be requested
995 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1001 struct pwm_device *pwm; in pwm_get() local
1010 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) in pwm_get()
1011 return of_pwm_get(dev, dev->of_node, con_id); in pwm_get()
1014 if (dev && is_acpi_node(dev->fwnode)) { in pwm_get()
1015 pwm = acpi_pwm_get(dev->fwnode); in pwm_get()
1016 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1017 return pwm; in pwm_get()
1027 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1028 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1037 * Then we take the most specific entry - with the following order in pwm_get()
1045 if (p->dev_id) { in pwm_get()
1046 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
1052 if (p->con_id) { in pwm_get()
1053 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1072 return ERR_PTR(-ENODEV); in pwm_get()
1074 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1078 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1082 if (!chip && chosen->module) { in pwm_get()
1083 err = request_module(chosen->module); in pwm_get()
1085 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1089 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
1091 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1092 if (IS_ERR(pwm)) in pwm_get()
1093 return pwm; in pwm_get()
1095 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1097 pwm_free(pwm); in pwm_get()
1101 pwm->args.period = chosen->period; in pwm_get()
1102 pwm->args.polarity = chosen->polarity; in pwm_get()
1104 return pwm; in pwm_get()
1109 * pwm_put() - release a PWM device
1110 * @pwm: PWM device
1112 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1114 if (!pwm) in pwm_put()
1119 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1120 pr_warn("PWM device already freed\n"); in pwm_put()
1124 if (pwm->chip->ops->free) in pwm_put()
1125 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1127 pwm_set_chip_data(pwm, NULL); in pwm_put()
1128 pwm->label = NULL; in pwm_put()
1130 module_put(pwm->chip->ops->owner); in pwm_put()
1142 * devm_pwm_get() - resource managed pwm_get()
1143 * @dev: device for PWM consumer
1146 * This function performs like pwm_get() but the acquired PWM device will
1149 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1154 struct pwm_device **ptr, *pwm; in devm_pwm_get() local
1158 return ERR_PTR(-ENOMEM); in devm_pwm_get()
1160 pwm = pwm_get(dev, con_id); in devm_pwm_get()
1161 if (!IS_ERR(pwm)) { in devm_pwm_get()
1162 *ptr = pwm; in devm_pwm_get()
1168 return pwm; in devm_pwm_get()
1173 * devm_of_pwm_get() - resource managed of_pwm_get()
1174 * @dev: device for PWM consumer
1175 * @np: device node to get the PWM from
1178 * This function performs like of_pwm_get() but the acquired PWM device will
1181 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1187 struct pwm_device **ptr, *pwm; in devm_of_pwm_get() local
1191 return ERR_PTR(-ENOMEM); in devm_of_pwm_get()
1193 pwm = of_pwm_get(dev, np, con_id); in devm_of_pwm_get()
1194 if (!IS_ERR(pwm)) { in devm_of_pwm_get()
1195 *ptr = pwm; in devm_of_pwm_get()
1201 return pwm; in devm_of_pwm_get()
1206 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1207 * @dev: device for PWM consumer
1208 * @fwnode: firmware node to get the PWM from
1211 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1214 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1221 struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
1225 return ERR_PTR(-ENOMEM); in devm_fwnode_pwm_get()
1228 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
1230 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
1232 if (!IS_ERR(pwm)) { in devm_fwnode_pwm_get()
1233 *ptr = pwm; in devm_fwnode_pwm_get()
1239 return pwm; in devm_fwnode_pwm_get()
1254 * devm_pwm_put() - resource managed pwm_put()
1255 * @dev: device for PWM consumer
1256 * @pwm: PWM device
1258 * Release a PWM previously allocated using devm_pwm_get(). Calling this
1259 * function is usually not needed because devm-allocated resources are
1262 void devm_pwm_put(struct device *dev, struct pwm_device *pwm) in devm_pwm_put() argument
1264 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm)); in devm_pwm_put()
1273 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
1274 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
1277 pwm_get_state(pwm, &state); in pwm_dbg_show()
1279 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1281 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1299 s->private = ""; in pwm_seq_start()
1306 s->private = "\n"; in pwm_seq_next()
1320 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, in pwm_seq_show()
1321 chip->dev->bus ? chip->dev->bus->name : "no-bus", in pwm_seq_show()
1322 dev_name(chip->dev), chip->npwm, in pwm_seq_show()
1323 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
1341 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, in pwm_debugfs_init()