Lines Matching full:pwm
12 #include <linux/pwm.h>
21 #include <dt-bindings/pwm/pwm.h>
24 #include <trace/events/pwm.h>
85 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
89 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
92 if (!try_module_get(pwm->chip->ops->owner)) in pwm_device_request()
95 if (pwm->chip->ops->request) { in pwm_device_request()
96 err = pwm->chip->ops->request(pwm->chip, pwm); in pwm_device_request()
98 module_put(pwm->chip->ops->owner); in pwm_device_request()
103 if (pwm->chip->ops->get_state) { in pwm_device_request()
113 err = pwm->chip->ops->get_state(pwm->chip, pwm, &state); in pwm_device_request()
114 trace_pwm_get(pwm, &state, err); in pwm_device_request()
117 pwm->state = state; in pwm_device_request()
120 pwm->last = pwm->state; in pwm_device_request()
123 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
124 pwm->label = label; in pwm_device_request()
132 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
144 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
145 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
146 return pwm; in of_pwm_xlate_with_flags()
148 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
149 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
153 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
156 return pwm; in of_pwm_xlate_with_flags()
163 struct pwm_device *pwm; in of_pwm_single_xlate() local
172 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
173 if (IS_ERR(pwm)) in of_pwm_single_xlate()
174 return pwm; in of_pwm_single_xlate()
176 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
177 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
180 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
182 return pwm; in of_pwm_single_xlate()
194 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", in of_pwmchip_add()
212 * pwm_set_chip_data() - set private chip data for a PWM
213 * @pwm: PWM device
218 int pwm_set_chip_data(struct pwm_device *pwm, void *data) in pwm_set_chip_data() argument
220 if (!pwm) in pwm_set_chip_data()
223 pwm->chip_data = data; in pwm_set_chip_data()
230 * pwm_get_chip_data() - get private chip data for a PWM
231 * @pwm: PWM device
233 * Returns: A pointer to the chip-private data for the PWM device.
235 void *pwm_get_chip_data(struct pwm_device *pwm) in pwm_get_chip_data() argument
237 return pwm ? pwm->chip_data : NULL; in pwm_get_chip_data()
256 * pwmchip_add() - register a new PWM chip
257 * @chip: the PWM chip to add
259 * Register a new PWM chip.
265 struct pwm_device *pwm; in pwmchip_add() local
275 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); in pwmchip_add()
291 pwm = &chip->pwms[i]; in pwmchip_add()
293 pwm->chip = chip; in pwmchip_add()
294 pwm->pwm = chip->base + i; in pwmchip_add()
295 pwm->hwpwm = i; in pwmchip_add()
312 * pwmchip_remove() - remove a PWM chip
313 * @chip: the PWM chip to remove
315 * Removes a PWM chip.
354 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
355 * @chip: PWM chip
356 * @index: per-chip index of the PWM to request
357 * @label: a literal description string of this PWM
359 * Returns: A pointer to the PWM device at the given index of the given PWM
361 * specified PWM chip or if the PWM device cannot be requested.
367 struct pwm_device *pwm; in pwm_request_from_chip() local
374 pwm = &chip->pwms[index]; in pwm_request_from_chip()
376 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
378 pwm = ERR_PTR(err); in pwm_request_from_chip()
381 return pwm; in pwm_request_from_chip()
385 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
388 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
389 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
405 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
406 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
464 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
465 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
473 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
474 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
492 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
493 * @pwm: PWM device
496 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
510 if (!pwm || !state || !state->period || in pwm_apply_might_sleep()
514 chip = pwm->chip; in pwm_apply_might_sleep()
516 if (state->period == pwm->state.period && in pwm_apply_might_sleep()
517 state->duty_cycle == pwm->state.duty_cycle && in pwm_apply_might_sleep()
518 state->polarity == pwm->state.polarity && in pwm_apply_might_sleep()
519 state->enabled == pwm->state.enabled && in pwm_apply_might_sleep()
520 state->usage_power == pwm->state.usage_power) in pwm_apply_might_sleep()
523 err = chip->ops->apply(chip, pwm, state); in pwm_apply_might_sleep()
524 trace_pwm_apply(pwm, state, err); in pwm_apply_might_sleep()
528 pwm->state = *state; in pwm_apply_might_sleep()
531 * only do this after pwm->state was applied as some in pwm_apply_might_sleep()
534 pwm_apply_debug(pwm, state); in pwm_apply_might_sleep()
541 * pwm_capture() - capture and report a PWM signal
542 * @pwm: PWM device
548 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
553 if (!pwm || !pwm->chip->ops) in pwm_capture()
556 if (!pwm->chip->ops->capture) in pwm_capture()
560 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); in pwm_capture()
568 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
569 * @pwm: PWM device
571 * This function will adjust the PWM config to the PWM arguments provided
572 * by the DT or PWM lookup table. This is particularly useful to adapt
575 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
580 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
581 pwm_get_state(pwm, &state); in pwm_adjust_config()
584 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
585 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
596 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
600 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
601 * in PWM args. in pwm_adjust_config()
619 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
641 struct pwm_device *pwm) in pwm_device_link_add() argument
647 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
648 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
651 dev_warn(pwm->chip->dev, in pwm_device_link_add()
656 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
659 dev_name(pwm->chip->dev)); in pwm_device_link_add()
667 * of_pwm_get() - request a PWM via the PWM framework
668 * @dev: device for PWM consumer
669 * @np: device node to get the PWM from
672 * Returns the PWM device parsed from the phandle and index specified in the
674 * Values parsed from the device tree are stored in the returned PWM device
677 * If con_id is NULL, the first PWM device listed in the "pwms" property will
678 * be requested. Otherwise the "pwm-names" property is used to do a reverse
679 * lookup of the PWM index. This also means that the "pwm-names" property
680 * becomes mandatory for devices that look up the PWM device via the con_id
683 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
689 struct pwm_device *pwm = NULL; in of_pwm_get() local
697 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
702 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
712 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
714 pwm = ERR_CAST(chip); in of_pwm_get()
718 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
719 if (IS_ERR(pwm)) in of_pwm_get()
722 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
725 pwm_put(pwm); in of_pwm_get()
726 pwm = ERR_CAST(dl); in of_pwm_get()
732 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
736 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
742 pwm->label = con_id; in of_pwm_get()
747 return pwm; in of_pwm_get()
751 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
754 * Returns the PWM device parsed from the fwnode and index specified in the
756 * Values parsed from the device tree are stored in the returned PWM device
762 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
764 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
769 struct pwm_device *pwm; in acpi_pwm_get() local
787 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
788 if (IS_ERR(pwm)) in acpi_pwm_get()
789 return pwm; in acpi_pwm_get()
791 pwm->args.period = args.args[1]; in acpi_pwm_get()
792 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
795 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
797 return pwm; in acpi_pwm_get()
801 * pwm_add_table() - register PWM device consumers
818 * pwm_remove_table() - unregister PWM device consumers
835 * pwm_get() - look up and request a PWM device
836 * @dev: device for PWM consumer
840 * a device tree, a PWM chip and a relative index is looked up via a table
843 * Once a PWM chip has been found the specified PWM device will be requested
846 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
853 struct pwm_device *pwm; in pwm_get() local
867 pwm = acpi_pwm_get(fwnode); in pwm_get()
868 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
869 return pwm; in pwm_get()
879 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
880 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
930 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
943 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
944 if (IS_ERR(pwm)) in pwm_get()
945 return pwm; in pwm_get()
947 dl = pwm_device_link_add(dev, pwm); in pwm_get()
949 pwm_put(pwm); in pwm_get()
953 pwm->args.period = chosen->period; in pwm_get()
954 pwm->args.polarity = chosen->polarity; in pwm_get()
956 return pwm; in pwm_get()
961 * pwm_put() - release a PWM device
962 * @pwm: PWM device
964 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
966 if (!pwm) in pwm_put()
971 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
972 pr_warn("PWM device already freed\n"); in pwm_put()
976 if (pwm->chip->ops->free) in pwm_put()
977 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
979 pwm_set_chip_data(pwm, NULL); in pwm_put()
980 pwm->label = NULL; in pwm_put()
982 module_put(pwm->chip->ops->owner); in pwm_put()
988 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
990 pwm_put(pwm); in devm_pwm_release()
995 * @dev: device for PWM consumer
998 * This function performs like pwm_get() but the acquired PWM device will
1001 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1006 struct pwm_device *pwm; in devm_pwm_get() local
1009 pwm = pwm_get(dev, con_id); in devm_pwm_get()
1010 if (IS_ERR(pwm)) in devm_pwm_get()
1011 return pwm; in devm_pwm_get()
1013 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
1017 return pwm; in devm_pwm_get()
1022 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1023 * @dev: device for PWM consumer
1024 * @fwnode: firmware node to get the PWM from
1027 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1030 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1037 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
1041 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
1043 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
1044 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
1045 return pwm; in devm_fwnode_pwm_get()
1047 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
1051 return pwm; in devm_fwnode_pwm_get()
1061 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
1064 pwm_get_state(pwm, &state); in pwm_dbg_show()
1066 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1068 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1110 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, in pwm_seq_show()
1131 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_debugfs_init()