Lines Matching +full:- +full:pwm

1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2011-2012 Avionic Design GmbH
13 #include <linux/pwm.h>
22 #include <dt-bindings/pwm/pwm.h>
25 #include <trace/events/pwm.h>
46 const char *chip_name = dev_name(chip->dev); in pwmchip_find_by_name()
59 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
62 struct pwm_chip *chip = pwm->chip; in pwm_device_request()
63 const struct pwm_ops *ops = chip->ops; in pwm_device_request()
65 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
66 return -EBUSY; in pwm_device_request()
68 if (!try_module_get(chip->owner)) in pwm_device_request()
69 return -ENODEV; in pwm_device_request()
71 if (ops->request) { in pwm_device_request()
72 err = ops->request(chip, pwm); in pwm_device_request()
74 module_put(chip->owner); in pwm_device_request()
79 if (ops->get_state) { in pwm_device_request()
81 * Zero-initialize state because most drivers are unaware of in pwm_device_request()
89 err = ops->get_state(chip, pwm, &state); in pwm_device_request()
90 trace_pwm_get(pwm, &state, err); in pwm_device_request()
93 pwm->state = state; in pwm_device_request()
96 pwm->last = pwm->state; in pwm_device_request()
99 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
100 pwm->label = label; in pwm_device_request()
108 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
110 if (chip->of_pwm_n_cells < 2) in of_pwm_xlate_with_flags()
111 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
114 if (args->args_count < 2) in of_pwm_xlate_with_flags()
115 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
117 if (args->args[0] >= chip->npwm) in of_pwm_xlate_with_flags()
118 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
120 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
121 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
122 return pwm; in of_pwm_xlate_with_flags()
124 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
125 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
127 if (chip->of_pwm_n_cells >= 3) { in of_pwm_xlate_with_flags()
128 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
129 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
132 return pwm; in of_pwm_xlate_with_flags()
139 struct pwm_device *pwm; in of_pwm_single_xlate() local
141 if (chip->of_pwm_n_cells < 1) in of_pwm_single_xlate()
142 return ERR_PTR(-EINVAL); in of_pwm_single_xlate()
145 if (args->args_count != 1 && args->args_count != 2) in of_pwm_single_xlate()
146 return ERR_PTR(-EINVAL); in of_pwm_single_xlate()
148 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
149 if (IS_ERR(pwm)) in of_pwm_single_xlate()
150 return pwm; in of_pwm_single_xlate()
152 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
153 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
155 if (args->args_count == 2 && args->args[1] & PWM_POLARITY_INVERTED) in of_pwm_single_xlate()
156 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
158 return pwm; in of_pwm_single_xlate()
164 if (!chip->dev || !chip->dev->of_node) in of_pwmchip_add()
167 if (!chip->of_xlate) { in of_pwmchip_add()
170 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", in of_pwmchip_add()
174 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
175 chip->of_pwm_n_cells = pwm_cells; in of_pwmchip_add()
178 of_node_get(chip->dev->of_node); in of_pwmchip_add()
183 if (chip->dev) in of_pwmchip_remove()
184 of_node_put(chip->dev->of_node); in of_pwmchip_remove()
189 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
191 if (!ops->apply) in pwm_ops_check()
194 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
195 dev_warn(chip->dev, in pwm_ops_check()
202 * __pwmchip_add() - register a new PWM chip
203 * @chip: the PWM chip to add
206 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
216 if (!chip || !chip->dev || !chip->ops || !chip->npwm) in __pwmchip_add()
217 return -EINVAL; in __pwmchip_add()
220 return -EINVAL; in __pwmchip_add()
222 chip->owner = owner; in __pwmchip_add()
224 chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); in __pwmchip_add()
225 if (!chip->pwms) in __pwmchip_add()
226 return -ENOMEM; in __pwmchip_add()
233 kfree(chip->pwms); in __pwmchip_add()
237 chip->id = ret; in __pwmchip_add()
239 for (i = 0; i < chip->npwm; i++) { in __pwmchip_add()
240 struct pwm_device *pwm = &chip->pwms[i]; in __pwmchip_add() local
242 pwm->chip = chip; in __pwmchip_add()
243 pwm->hwpwm = i; in __pwmchip_add()
258 * pwmchip_remove() - remove a PWM chip
259 * @chip: the PWM chip to remove
261 * Removes a PWM chip.
272 idr_remove(&pwm_chips, chip->id); in pwmchip_remove()
276 kfree(chip->pwms); in pwmchip_remove()
300 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
301 * @chip: PWM chip
302 * @index: per-chip index of the PWM to request
303 * @label: a literal description string of this PWM
305 * Returns: A pointer to the PWM device at the given index of the given PWM
307 * specified PWM chip or if the PWM device cannot be requested.
313 struct pwm_device *pwm; in pwm_request_from_chip() local
316 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
317 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
320 pwm = &chip->pwms[index]; in pwm_request_from_chip()
322 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
324 pwm = ERR_PTR(err); in pwm_request_from_chip()
327 return pwm; in pwm_request_from_chip()
331 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
334 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
335 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
343 if (!chip->ops->get_state) in pwm_apply_debug()
351 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
352 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
362 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_debug()
363 s2.polarity = state->polarity; in pwm_apply_debug()
364 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_debug()
371 if (s2.polarity != state->polarity && in pwm_apply_debug()
372 state->duty_cycle < state->period) in pwm_apply_debug()
373 dev_warn(chip->dev, ".apply ignored .polarity\n"); in pwm_apply_debug()
375 if (state->enabled && in pwm_apply_debug()
376 last->polarity == state->polarity && in pwm_apply_debug()
377 last->period > s2.period && in pwm_apply_debug()
378 last->period <= state->period) in pwm_apply_debug()
379 dev_warn(chip->dev, in pwm_apply_debug()
381 state->period, s2.period, last->period); in pwm_apply_debug()
383 if (state->enabled && state->period < s2.period) in pwm_apply_debug()
384 dev_warn(chip->dev, in pwm_apply_debug()
386 state->period, s2.period); in pwm_apply_debug()
388 if (state->enabled && in pwm_apply_debug()
389 last->polarity == state->polarity && in pwm_apply_debug()
390 last->period == s2.period && in pwm_apply_debug()
391 last->duty_cycle > s2.duty_cycle && in pwm_apply_debug()
392 last->duty_cycle <= state->duty_cycle) in pwm_apply_debug()
393 dev_warn(chip->dev, in pwm_apply_debug()
395 state->duty_cycle, state->period, in pwm_apply_debug()
397 last->duty_cycle, last->period); in pwm_apply_debug()
399 if (state->enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_debug()
400 dev_warn(chip->dev, in pwm_apply_debug()
402 state->duty_cycle, state->period, in pwm_apply_debug()
405 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_debug()
406 dev_warn(chip->dev, in pwm_apply_debug()
410 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
411 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
414 dev_err(chip->dev, "failed to reapply current setting\n"); in pwm_apply_debug()
419 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
420 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
425 if (s1.enabled != last->enabled || in pwm_apply_debug()
426 s1.polarity != last->polarity || in pwm_apply_debug()
427 (s1.enabled && s1.period != last->period) || in pwm_apply_debug()
428 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_debug()
429 dev_err(chip->dev, in pwm_apply_debug()
430 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_debug()
432 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_debug()
433 last->period); in pwm_apply_debug()
438 * __pwm_apply() - atomically apply a new state to a PWM device
439 * @pwm: PWM device
442 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) in __pwm_apply() argument
447 if (!pwm || !state || !state->period || in __pwm_apply()
448 state->duty_cycle > state->period) in __pwm_apply()
449 return -EINVAL; in __pwm_apply()
451 chip = pwm->chip; in __pwm_apply()
453 if (state->period == pwm->state.period && in __pwm_apply()
454 state->duty_cycle == pwm->state.duty_cycle && in __pwm_apply()
455 state->polarity == pwm->state.polarity && in __pwm_apply()
456 state->enabled == pwm->state.enabled && in __pwm_apply()
457 state->usage_power == pwm->state.usage_power) in __pwm_apply()
460 err = chip->ops->apply(chip, pwm, state); in __pwm_apply()
461 trace_pwm_apply(pwm, state, err); in __pwm_apply()
465 pwm->state = *state; in __pwm_apply()
468 * only do this after pwm->state was applied as some in __pwm_apply()
471 pwm_apply_debug(pwm, state); in __pwm_apply()
477 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
479 * @pwm: PWM device
482 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
495 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) { in pwm_apply_might_sleep()
501 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
504 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
512 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
513 * Not all PWM devices support this function, check with pwm_might_sleep().
514 * @pwm: PWM device
517 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_atomic() argument
519 WARN_ONCE(!pwm->chip->atomic, in pwm_apply_atomic()
520 "sleeping PWM driver used in atomic context\n"); in pwm_apply_atomic()
522 return __pwm_apply(pwm, state); in pwm_apply_atomic()
527 * pwm_capture() - capture and report a PWM signal
528 * @pwm: PWM device
534 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
539 if (!pwm || !pwm->chip->ops) in pwm_capture()
540 return -EINVAL; in pwm_capture()
542 if (!pwm->chip->ops->capture) in pwm_capture()
543 return -ENOSYS; in pwm_capture()
546 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); in pwm_capture()
554 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
555 * @pwm: PWM device
557 * This function will adjust the PWM config to the PWM arguments provided
558 * by the DT or PWM lookup table. This is particularly useful to adapt
561 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
566 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
567 pwm_get_state(pwm, &state); in pwm_adjust_config()
570 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
571 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
582 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
586 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
587 * in PWM args. in pwm_adjust_config()
602 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
605 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
617 if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { in fwnode_to_pwmchip()
624 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
628 struct pwm_device *pwm) in pwm_device_link_add() argument
634 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
635 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
638 dev_warn(pwm->chip->dev, in pwm_device_link_add()
643 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
646 dev_name(pwm->chip->dev)); in pwm_device_link_add()
647 return ERR_PTR(-EINVAL); in pwm_device_link_add()
654 * of_pwm_get() - request a PWM via the PWM framework
655 * @dev: device for PWM consumer
656 * @np: device node to get the PWM from
659 * Returns the PWM device parsed from the phandle and index specified in the
660 * "pwms" property of a device tree node or a negative error-code on failure.
661 * Values parsed from the device tree are stored in the returned PWM device
664 * If con_id is NULL, the first PWM device listed in the "pwms" property will
665 * be requested. Otherwise the "pwm-names" property is used to do a reverse
666 * lookup of the PWM index. This also means that the "pwm-names" property
667 * becomes mandatory for devices that look up the PWM device via the con_id
670 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
676 struct pwm_device *pwm = NULL; in of_pwm_get() local
684 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
689 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
698 if (PTR_ERR(chip) != -EPROBE_DEFER) in of_pwm_get()
699 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
701 pwm = ERR_CAST(chip); in of_pwm_get()
705 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
706 if (IS_ERR(pwm)) in of_pwm_get()
709 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
712 pwm_put(pwm); in of_pwm_get()
713 pwm = ERR_CAST(dl); in of_pwm_get()
719 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
723 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
726 con_id = np->name; in of_pwm_get()
729 pwm->label = con_id; in of_pwm_get()
734 return pwm; in of_pwm_get()
738 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
741 * Returns the PWM device parsed from the fwnode and index specified in the
742 * "pwms" property or a negative error-code on failure.
743 * Values parsed from the device tree are stored in the returned PWM device
749 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
751 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
756 struct pwm_device *pwm; in acpi_pwm_get() local
768 return ERR_PTR(-EPROTO); in acpi_pwm_get()
774 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
775 if (IS_ERR(pwm)) in acpi_pwm_get()
776 return pwm; in acpi_pwm_get()
778 pwm->args.period = args.args[1]; in acpi_pwm_get()
779 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
782 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
784 return pwm; in acpi_pwm_get()
788 * pwm_add_table() - register PWM device consumers
796 while (num--) { in pwm_add_table()
797 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
805 * pwm_remove_table() - unregister PWM device consumers
813 while (num--) { in pwm_remove_table()
814 list_del(&table->list); in pwm_remove_table()
822 * pwm_get() - look up and request a PWM device
823 * @dev: device for PWM consumer
827 * a device tree, a PWM chip and a relative index is looked up via a table
830 * Once a PWM chip has been found the specified PWM device will be requested
833 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
840 struct pwm_device *pwm; in pwm_get() local
854 pwm = acpi_pwm_get(fwnode); in pwm_get()
855 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
856 return pwm; in pwm_get()
866 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
867 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
876 * Then we take the most specific entry - with the following order in pwm_get()
884 if (p->dev_id) { in pwm_get()
885 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
891 if (p->con_id) { in pwm_get()
892 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
911 return ERR_PTR(-ENODEV); in pwm_get()
913 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
917 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
921 if (!chip && chosen->module) { in pwm_get()
922 err = request_module(chosen->module); in pwm_get()
924 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
928 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
930 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
931 if (IS_ERR(pwm)) in pwm_get()
932 return pwm; in pwm_get()
934 dl = pwm_device_link_add(dev, pwm); in pwm_get()
936 pwm_put(pwm); in pwm_get()
940 pwm->args.period = chosen->period; in pwm_get()
941 pwm->args.polarity = chosen->polarity; in pwm_get()
943 return pwm; in pwm_get()
948 * pwm_put() - release a PWM device
949 * @pwm: PWM device
951 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
953 if (!pwm) in pwm_put()
958 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
959 pr_warn("PWM device already freed\n"); in pwm_put()
963 if (pwm->chip->ops->free) in pwm_put()
964 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
966 pwm->label = NULL; in pwm_put()
968 module_put(pwm->chip->owner); in pwm_put()
974 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
976 pwm_put(pwm); in devm_pwm_release()
980 * devm_pwm_get() - resource managed pwm_get()
981 * @dev: device for PWM consumer
984 * This function performs like pwm_get() but the acquired PWM device will
987 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
992 struct pwm_device *pwm; in devm_pwm_get() local
995 pwm = pwm_get(dev, con_id); in devm_pwm_get()
996 if (IS_ERR(pwm)) in devm_pwm_get()
997 return pwm; in devm_pwm_get()
999 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
1003 return pwm; in devm_pwm_get()
1008 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1009 * @dev: device for PWM consumer
1010 * @fwnode: firmware node to get the PWM from
1013 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1016 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1023 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
1027 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
1029 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
1030 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
1031 return pwm; in devm_fwnode_pwm_get()
1033 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
1037 return pwm; in devm_fwnode_pwm_get()
1046 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
1047 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
1050 pwm_get_state(pwm, &state); in pwm_dbg_show()
1052 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1054 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1078 s->private = ""; in pwm_seq_start()
1090 s->private = "\n"; in pwm_seq_next()
1106 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
1107 (char *)s->private, chip->id, in pwm_seq_show()
1108 chip->dev->bus ? chip->dev->bus->name : "no-bus", in pwm_seq_show()
1109 dev_name(chip->dev), chip->npwm, in pwm_seq_show()
1110 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
1128 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_debugfs_init()