Lines Matching full:pwm
13 #include <linux/pwm.h>
22 #include <dt-bindings/pwm/pwm.h>
25 #include <trace/events/pwm.h>
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()
65 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
72 err = ops->request(chip, pwm); 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
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()
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
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()
156 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
158 return pwm; in of_pwm_single_xlate()
170 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", in of_pwmchip_add()
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
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.
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
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()
351 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
352 trace_pwm_get(pwm, &s1, err); 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()
419 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
420 trace_pwm_get(pwm, last, err); 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()
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()
542 if (!pwm->chip->ops->capture) 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()
605 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
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()
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
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()
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()
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
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
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
805 * pwm_remove_table() - unregister PWM device consumers
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()
917 * the PWM chip lookup. This can be used to work around driver load 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()
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()
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()
1106 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
1128 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_debugfs_init()