Lines Matching full:pwm

9 #define DEFAULT_SYMBOL_NAMESPACE PWM
15 #include <linux/pwm.h>
24 #include <dt-bindings/pwm/pwm.h>
27 #include <trace/events/pwm.h>
34 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
37 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
38 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
54 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
55 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
113 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
114 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
122 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
123 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
160 * __pwm_apply() - atomically apply a new state to a PWM device
161 * @pwm: PWM device
164 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) in __pwm_apply() argument
169 if (!pwm || !state) in __pwm_apply()
181 if (!pwm_state_valid(&pwm->state)) { in __pwm_apply()
182 pwm->state = *state; in __pwm_apply()
189 chip = pwm->chip; in __pwm_apply()
191 if (state->period == pwm->state.period && in __pwm_apply()
192 state->duty_cycle == pwm->state.duty_cycle && in __pwm_apply()
193 state->polarity == pwm->state.polarity && in __pwm_apply()
194 state->enabled == pwm->state.enabled && in __pwm_apply()
195 state->usage_power == pwm->state.usage_power) in __pwm_apply()
198 err = chip->ops->apply(chip, pwm, state); in __pwm_apply()
199 trace_pwm_apply(pwm, state, err); in __pwm_apply()
203 pwm->state = *state; in __pwm_apply()
206 * only do this after pwm->state was applied as some in __pwm_apply()
209 pwm_apply_debug(pwm, state); in __pwm_apply()
215 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
217 * @pwm: PWM device
220 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
233 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) { in pwm_apply_might_sleep()
239 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
242 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
250 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
251 * Not all PWM devices support this function, check with pwm_might_sleep().
252 * @pwm: PWM device
255 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_atomic() argument
257 WARN_ONCE(!pwm->chip->atomic, in pwm_apply_atomic()
258 "sleeping PWM driver used in atomic context\n"); in pwm_apply_atomic()
260 return __pwm_apply(pwm, state); in pwm_apply_atomic()
265 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
266 * @pwm: PWM device
268 * This function will adjust the PWM config to the PWM arguments provided
269 * by the DT or PWM lookup table. This is particularly useful to adapt
272 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
277 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
278 pwm_get_state(pwm, &state); in pwm_adjust_config()
281 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
282 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
293 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
297 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
298 * in PWM args. in pwm_adjust_config()
316 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
321 * pwm_capture() - capture and report a PWM signal
322 * @pwm: PWM device
328 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
331 struct pwm_chip *chip = pwm->chip; in pwm_capture()
339 return ops->capture(chip, pwm, result, timeout); in pwm_capture()
362 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
365 struct pwm_chip *chip = pwm->chip; in pwm_device_request()
368 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
380 err = ops->request(chip, pwm); in pwm_device_request()
399 err = ops->get_state(chip, pwm, &state); in pwm_device_request()
400 trace_pwm_get(pwm, &state, err); in pwm_device_request()
403 pwm->state = state; in pwm_device_request()
406 pwm->last = pwm->state; in pwm_device_request()
409 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
410 pwm->label = label; in pwm_device_request()
416 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
417 * @chip: PWM chip
418 * @index: per-chip index of the PWM to request
419 * @label: a literal description string of this PWM
421 * Returns: A pointer to the PWM device at the given index of the given PWM
423 * specified PWM chip or if the PWM device cannot be requested.
429 struct pwm_device *pwm; in pwm_request_from_chip() local
437 pwm = &chip->pwms[index]; in pwm_request_from_chip()
439 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
443 return pwm; in pwm_request_from_chip()
449 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
455 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
456 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
457 return pwm; in of_pwm_xlate_with_flags()
460 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
462 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
464 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
466 return pwm; in of_pwm_xlate_with_flags()
473 struct pwm_device *pwm; in of_pwm_single_xlate() local
475 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
476 if (IS_ERR(pwm)) in of_pwm_single_xlate()
477 return pwm; in of_pwm_single_xlate()
480 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
482 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
484 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
486 return pwm; in of_pwm_single_xlate()
492 struct pwm_device *pwm; member
511 return export->pwm; in pwm_from_dev()
518 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in period_show() local
521 pwm_get_state(pwm, &state); in period_show()
531 struct pwm_device *pwm = export->pwm; in period_store() local
542 pwm_get_state(pwm, &state); in period_store()
544 ret = pwm_apply_might_sleep(pwm, &state); in period_store()
553 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in duty_cycle_show() local
556 pwm_get_state(pwm, &state); in duty_cycle_show()
566 struct pwm_device *pwm = export->pwm; in duty_cycle_store() local
577 pwm_get_state(pwm, &state); in duty_cycle_store()
579 ret = pwm_apply_might_sleep(pwm, &state); in duty_cycle_store()
588 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in enable_show() local
591 pwm_get_state(pwm, &state); in enable_show()
601 struct pwm_device *pwm = export->pwm; in enable_store() local
611 pwm_get_state(pwm, &state); in enable_store()
624 ret = pwm_apply_might_sleep(pwm, &state); in enable_store()
633 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in polarity_show() local
637 pwm_get_state(pwm, &state); in polarity_show()
657 struct pwm_device *pwm = export->pwm; in polarity_store() local
671 pwm_get_state(pwm, &state); in polarity_store()
673 ret = pwm_apply_might_sleep(pwm, &state); in polarity_store()
682 struct pwm_device *pwm = pwm_from_dev(pwm_dev); in capture_show() local
686 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); in capture_show()
707 ATTRIBUTE_GROUPS(pwm);
716 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_export_child() argument
722 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_export_child()
727 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
731 export->pwm = pwm; in pwm_export_child()
738 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); in pwm_export_child()
742 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
747 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
760 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_unexport_child() argument
765 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_unexport_child()
768 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_unexport_child()
772 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
780 pwm_put(pwm); in pwm_unexport_child()
790 struct pwm_device *pwm; in export_store() local
801 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); in export_store()
802 if (IS_ERR(pwm)) in export_store()
803 return PTR_ERR(pwm); in export_store()
805 ret = pwm_export_child(pwmchip_dev, pwm); in export_store()
807 pwm_put(pwm); in export_store()
853 struct pwm_device *pwm, in pwm_class_get_state() argument
859 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_class_get_state()
862 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_class_get_state()
870 pwm_get_state(pwm, state); in pwm_class_get_state()
876 struct pwm_device *pwm, in pwm_class_apply_state() argument
879 int ret = pwm_apply_might_sleep(pwm, state); in pwm_class_apply_state()
894 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_resume_npwm() local
898 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_resume_npwm()
910 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
925 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_suspend() local
929 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_suspend()
945 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_suspend()
948 * roll back the PWM devices that were disabled by in pwm_class_suspend()
969 .name = "pwm",
979 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_sysfs_unexport() local
981 if (test_bit(PWMF_EXPORTED, &pwm->flags)) in pwmchip_sysfs_unexport()
982 pwm_unexport_child(&chip->dev, pwm); in pwmchip_sysfs_unexport()
1033 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_alloc() local
1034 pwm->chip = chip; in pwmchip_alloc()
1035 pwm->hwpwm = i; in pwmchip_alloc()
1098 * __pwmchip_add() - register a new PWM chip
1099 * @chip: the PWM chip to add
1102 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1158 * pwmchip_remove() - remove a PWM chip
1159 * @chip: the PWM chip to remove
1161 * Removes a PWM chip.
1197 struct pwm_device *pwm) in pwm_device_link_add() argument
1203 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
1204 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
1207 dev_warn(pwmchip_parent(pwm->chip), in pwm_device_link_add()
1212 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
1215 dev_name(pwmchip_parent(pwm->chip))); in pwm_device_link_add()
1237 * of_pwm_get() - request a PWM via the PWM framework
1238 * @dev: device for PWM consumer
1239 * @np: device node to get the PWM from
1242 * Returns the PWM device parsed from the phandle and index specified in the
1244 * Values parsed from the device tree are stored in the returned PWM device
1247 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1248 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1249 * lookup of the PWM index. This also means that the "pwm-names" property
1250 * becomes mandatory for devices that look up the PWM device via the con_id
1253 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1259 struct pwm_device *pwm = NULL; in of_pwm_get() local
1267 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
1272 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
1282 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
1284 pwm = ERR_CAST(chip); in of_pwm_get()
1288 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
1289 if (IS_ERR(pwm)) in of_pwm_get()
1292 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
1295 pwm_put(pwm); in of_pwm_get()
1296 pwm = ERR_CAST(dl); in of_pwm_get()
1302 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
1306 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
1312 pwm->label = con_id; in of_pwm_get()
1317 return pwm; in of_pwm_get()
1321 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1324 * Returns the PWM device parsed from the fwnode and index specified in the
1326 * Values parsed from the device tree are stored in the returned PWM device
1332 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1334 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1339 struct pwm_device *pwm; in acpi_pwm_get() local
1357 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
1358 if (IS_ERR(pwm)) in acpi_pwm_get()
1359 return pwm; in acpi_pwm_get()
1361 pwm->args.period = args.args[1]; in acpi_pwm_get()
1362 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
1365 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
1367 return pwm; in acpi_pwm_get()
1374 * pwm_add_table() - register PWM device consumers
1389 * pwm_remove_table() - unregister PWM device consumers
1404 * pwm_get() - look up and request a PWM device
1405 * @dev: device for PWM consumer
1409 * a device tree, a PWM chip and a relative index is looked up via a table
1412 * Once a PWM chip has been found the specified PWM device will be requested
1415 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1422 struct pwm_device *pwm; in pwm_get() local
1436 pwm = acpi_pwm_get(fwnode); in pwm_get()
1437 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1438 return pwm; in pwm_get()
1448 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1449 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1496 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1509 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1510 if (IS_ERR(pwm)) in pwm_get()
1511 return pwm; in pwm_get()
1513 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1515 pwm_put(pwm); in pwm_get()
1519 pwm->args.period = chosen->period; in pwm_get()
1520 pwm->args.polarity = chosen->polarity; in pwm_get()
1522 return pwm; in pwm_get()
1527 * pwm_put() - release a PWM device
1528 * @pwm: PWM device
1530 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1534 if (!pwm) in pwm_put()
1537 chip = pwm->chip; in pwm_put()
1541 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1542 pr_warn("PWM device already freed\n"); in pwm_put()
1547 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1549 pwm->label = NULL; in pwm_put()
1557 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
1559 pwm_put(pwm); in devm_pwm_release()
1564 * @dev: device for PWM consumer
1567 * This function performs like pwm_get() but the acquired PWM device will
1570 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1575 struct pwm_device *pwm; in devm_pwm_get() local
1578 pwm = pwm_get(dev, con_id); in devm_pwm_get()
1579 if (IS_ERR(pwm)) in devm_pwm_get()
1580 return pwm; in devm_pwm_get()
1582 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
1586 return pwm; in devm_pwm_get()
1591 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1592 * @dev: device for PWM consumer
1593 * @fwnode: firmware node to get the PWM from
1596 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1599 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1606 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
1610 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
1612 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
1613 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
1614 return pwm; in devm_fwnode_pwm_get()
1616 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
1620 return pwm; in devm_fwnode_pwm_get()
1629 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
1632 pwm_get_state(pwm, &state); in pwm_dbg_show()
1634 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1636 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1688 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
1714 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); in pwm_init()
1719 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_init()