Lines Matching full:pwm

11 #include <linux/pwm.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()
48 if (pwm >= 0) in alloc_pwms()
49 from = pwm; in alloc_pwms()
54 if (pwm >= 0 && start != pwm) in alloc_pwms()
68 struct pwm_device *pwm = &chip->pwms[i]; in free_pwms() local
70 radix_tree_delete(&pwm_tree, pwm->pwm); in free_pwms()
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()
109 if (!try_module_get(pwm->chip->ops->owner)) 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
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()
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
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()
209 * pwm_set_chip_data() - set private chip data for a PWM
210 * @pwm: PWM device
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()
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()
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
292 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); 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()
307 pwm->state.output_type = PWM_OUTPUT_FIXED; in pwmchip_add_with_polarity()
309 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); in pwmchip_add_with_polarity()
333 * pwmchip_add() - register a new PWM chip
334 * @chip: the PWM chip to add
336 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
348 * pwmchip_remove() - remove a PWM chip
349 * @chip: the PWM chip to remove
351 * Removes a PWM chip. This function may return busy if the PWM chip provides
352 * a PWM device that is still requested.
366 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_remove() local
368 if (test_bit(PWMF_REQUESTED, &pwm->flags)) { in pwmchip_remove()
388 * pwm_request() - request a PWM device
389 * @pwm: global PWM device index
390 * @label: PWM device label
394 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
397 struct pwm_device *pwm_request(int pwm, const char *label) in pwm_request() argument
402 if (pwm < 0 || pwm >= MAX_PWMS) in pwm_request()
407 dev = pwm_to_device(pwm); in pwm_request()
425 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
426 * @chip: PWM chip
427 * @index: per-chip index of the PWM to request
428 * @label: a literal description string of this PWM
430 * Returns: A pointer to the PWM device at the given index of the given PWM
432 * specified PWM chip or if the PWM device cannot be requested.
438 struct pwm_device *pwm; in pwm_request_from_chip() local
445 pwm = &chip->pwms[index]; in pwm_request_from_chip()
447 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
449 pwm = ERR_PTR(err); in pwm_request_from_chip()
452 return pwm; in pwm_request_from_chip()
457 * pwm_free() - free a PWM device
458 * @pwm: PWM device
462 void pwm_free(struct pwm_device *pwm) in pwm_free() argument
464 pwm_put(pwm); in pwm_free()
468 static void pwm_apply_state_debug(struct pwm_device *pwm, in pwm_apply_state_debug() argument
471 struct pwm_state *last = &pwm->last; in pwm_apply_state_debug()
472 struct pwm_chip *chip = pwm->chip; in pwm_apply_state_debug()
488 chip->ops->get_state(chip, pwm, &s1); in pwm_apply_state_debug()
489 trace_pwm_get(pwm, &s1); in pwm_apply_state_debug()
544 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_state_debug()
551 trace_pwm_apply(pwm, &s1); in pwm_apply_state_debug()
553 chip->ops->get_state(chip, pwm, last); in pwm_apply_state_debug()
554 trace_pwm_get(pwm, last); in pwm_apply_state_debug()
570 * pwm_apply_state() - atomically apply a new state to a PWM device
571 * @pwm: PWM device
574 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_state() argument
579 if (!pwm || !state || !state->period || in pwm_apply_state()
583 chip = pwm->chip; in pwm_apply_state()
585 if (state->period == pwm->state.period && in pwm_apply_state()
586 state->duty_cycle == pwm->state.duty_cycle && in pwm_apply_state()
587 state->polarity == pwm->state.polarity && in pwm_apply_state()
589 state->oneshot_count == pwm->state.oneshot_count && in pwm_apply_state()
591 state->enabled == pwm->state.enabled) in pwm_apply_state()
595 err = chip->ops->apply(chip, pwm, state); in pwm_apply_state()
599 trace_pwm_apply(pwm, state); in pwm_apply_state()
601 pwm->state = *state; in pwm_apply_state()
604 * only do this after pwm->state was applied as some in pwm_apply_state()
607 pwm_apply_state_debug(pwm, state); in pwm_apply_state()
612 if (state->polarity != pwm->state.polarity) { in pwm_apply_state()
617 * Changing the polarity of a running PWM is in pwm_apply_state()
618 * only allowed when the PWM driver implements in pwm_apply_state()
621 if (pwm->state.enabled) { in pwm_apply_state()
622 chip->ops->disable(chip, pwm); in pwm_apply_state()
623 pwm->state.enabled = false; in pwm_apply_state()
626 err = chip->ops->set_polarity(chip, pwm, in pwm_apply_state()
631 pwm->state.polarity = state->polarity; in pwm_apply_state()
634 if (state->period != pwm->state.period || in pwm_apply_state()
635 state->duty_cycle != pwm->state.duty_cycle) { in pwm_apply_state()
636 err = chip->ops->config(pwm->chip, pwm, in pwm_apply_state()
642 pwm->state.duty_cycle = state->duty_cycle; in pwm_apply_state()
643 pwm->state.period = state->period; in pwm_apply_state()
646 if (state->enabled != pwm->state.enabled) { in pwm_apply_state()
648 err = chip->ops->enable(chip, pwm); in pwm_apply_state()
652 chip->ops->disable(chip, pwm); in pwm_apply_state()
655 pwm->state.enabled = state->enabled; in pwm_apply_state()
664 * pwm_capture() - capture and report a PWM signal
665 * @pwm: PWM device
671 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
676 if (!pwm || !pwm->chip->ops) in pwm_capture()
679 if (!pwm->chip->ops->capture) in pwm_capture()
683 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); in pwm_capture()
691 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
692 * @pwm: PWM device
694 * This function will adjust the PWM config to the PWM arguments provided
695 * by the DT or PWM lookup table. This is particularly useful to adapt
698 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
703 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
704 pwm_get_state(pwm, &state); in pwm_adjust_config()
707 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
708 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
719 return pwm_apply_state(pwm, &state); in pwm_adjust_config()
723 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
724 * in PWM args. in pwm_adjust_config()
742 return pwm_apply_state(pwm, &state); in pwm_adjust_config()
764 struct pwm_device *pwm) in pwm_device_link_add() argument
770 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
771 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
774 dev_warn(pwm->chip->dev, in pwm_device_link_add()
779 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
782 dev_name(pwm->chip->dev)); in pwm_device_link_add()
790 * of_pwm_get() - request a PWM via the PWM framework
791 * @dev: device for PWM consumer
792 * @np: device node to get the PWM from
795 * Returns the PWM device parsed from the phandle and index specified in the
797 * Values parsed from the device tree are stored in the returned PWM device
800 * If con_id is NULL, the first PWM device listed in the "pwms" property will
801 * be requested. Otherwise the "pwm-names" property is used to do a reverse
802 * lookup of the PWM index. This also means that the "pwm-names" property
803 * becomes mandatory for devices that look up the PWM device via the con_id
806 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
812 struct pwm_device *pwm = NULL; in of_pwm_get() local
820 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
825 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
835 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
837 pwm = ERR_CAST(pc); in of_pwm_get()
841 pwm = pc->of_xlate(pc, &args); in of_pwm_get()
842 if (IS_ERR(pwm)) in of_pwm_get()
845 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
848 pwm_free(pwm); in of_pwm_get()
849 pwm = ERR_CAST(dl); in of_pwm_get()
855 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
859 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
865 pwm->label = con_id; in of_pwm_get()
870 return pwm; in of_pwm_get()
897 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
898 * @fwnode: firmware node to get the "pwm" property from
900 * Returns the PWM device parsed from the fwnode and index specified in the
902 * Values parsed from the device tree are stored in the returned PWM device
908 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
910 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
915 struct pwm_device *pwm = ERR_PTR(-ENODEV); in acpi_pwm_get() local
939 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
940 if (IS_ERR(pwm)) in acpi_pwm_get()
941 return pwm; in acpi_pwm_get()
943 pwm->args.period = args.args[1]; in acpi_pwm_get()
944 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
947 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
950 return pwm; in acpi_pwm_get()
954 * pwm_add_table() - register PWM device consumers
971 * pwm_remove_table() - unregister PWM device consumers
988 * pwm_get() - look up and request a PWM device
989 * @dev: device for PWM consumer
993 * a device tree, a PWM chip and a relative index is looked up via a table
996 * Once a PWM chip has been found the specified PWM device will be requested
999 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1005 struct pwm_device *pwm; in pwm_get() local
1019 pwm = acpi_pwm_get(dev->fwnode); in pwm_get()
1020 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1021 return pwm; in pwm_get()
1031 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1032 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1082 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1095 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1096 if (IS_ERR(pwm)) in pwm_get()
1097 return pwm; in pwm_get()
1099 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1101 pwm_free(pwm); in pwm_get()
1105 pwm->args.period = chosen->period; in pwm_get()
1106 pwm->args.polarity = chosen->polarity; in pwm_get()
1108 return pwm; in pwm_get()
1113 * pwm_put() - release a PWM device
1114 * @pwm: PWM device
1116 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1118 if (!pwm) in pwm_put()
1123 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1124 pr_warn("PWM device already freed\n"); in pwm_put()
1128 if (pwm->chip->ops->free) in pwm_put()
1129 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1131 pwm_set_chip_data(pwm, NULL); in pwm_put()
1132 pwm->label = NULL; in pwm_put()
1134 module_put(pwm->chip->ops->owner); in pwm_put()
1147 * @dev: device for PWM consumer
1150 * This function performs like pwm_get() but the acquired PWM device will
1153 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1158 struct pwm_device **ptr, *pwm; in devm_pwm_get() local
1164 pwm = pwm_get(dev, con_id); in devm_pwm_get()
1165 if (!IS_ERR(pwm)) { in devm_pwm_get()
1166 *ptr = pwm; in devm_pwm_get()
1172 return pwm; in devm_pwm_get()
1178 * @dev: device for PWM consumer
1179 * @np: device node to get the PWM from
1182 * This function performs like of_pwm_get() but the acquired PWM device will
1185 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1191 struct pwm_device **ptr, *pwm; in devm_of_pwm_get() local
1197 pwm = of_pwm_get(dev, np, con_id); in devm_of_pwm_get()
1198 if (!IS_ERR(pwm)) { in devm_of_pwm_get()
1199 *ptr = pwm; in devm_of_pwm_get()
1205 return pwm; in devm_of_pwm_get()
1210 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1211 * @dev: device for PWM consumer
1212 * @fwnode: firmware node to get the PWM from
1215 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1218 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1225 struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
1232 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
1234 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
1236 if (!IS_ERR(pwm)) { in devm_fwnode_pwm_get()
1237 *ptr = pwm; in devm_fwnode_pwm_get()
1243 return pwm; in devm_fwnode_pwm_get()
1259 * @dev: device for PWM consumer
1260 * @pwm: PWM device
1262 * Release a PWM previously allocated using devm_pwm_get(). Calling this
1266 void devm_pwm_put(struct device *dev, struct pwm_device *pwm) in devm_pwm_put() argument
1268 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm)); in devm_pwm_put()
1278 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
1281 pwm_get_state(pwm, &state); in pwm_dbg_show()
1283 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1285 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1324 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, in pwm_seq_show()
1345 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, in pwm_debugfs_init()