Lines Matching +full:- +full:pwm

1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2011-2012 Avionic Design GmbH
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()
46 if (!chip->ops->get_state) 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()
65 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_debug()
66 s2.polarity = state->polarity; in pwm_apply_debug()
67 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_debug()
74 if (s2.polarity != state->polarity && in pwm_apply_debug()
75 state->duty_cycle < state->period) in pwm_apply_debug()
78 if (state->enabled && in pwm_apply_debug()
79 last->polarity == state->polarity && in pwm_apply_debug()
80 last->period > s2.period && in pwm_apply_debug()
81 last->period <= state->period) in pwm_apply_debug()
84 state->period, s2.period, last->period); in pwm_apply_debug()
86 if (state->enabled && state->period < s2.period) in pwm_apply_debug()
89 state->period, s2.period); in pwm_apply_debug()
91 if (state->enabled && in pwm_apply_debug()
92 last->polarity == state->polarity && in pwm_apply_debug()
93 last->period == s2.period && in pwm_apply_debug()
94 last->duty_cycle > s2.duty_cycle && in pwm_apply_debug()
95 last->duty_cycle <= state->duty_cycle) in pwm_apply_debug()
98 state->duty_cycle, state->period, in pwm_apply_debug()
100 last->duty_cycle, last->period); in pwm_apply_debug()
102 if (state->enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_debug()
105 state->duty_cycle, state->period, in pwm_apply_debug()
108 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 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()
128 if (s1.enabled != last->enabled || in pwm_apply_debug()
129 s1.polarity != last->polarity || in pwm_apply_debug()
130 (s1.enabled && s1.period != last->period) || in pwm_apply_debug()
131 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_debug()
133 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_debug()
135 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_debug()
136 last->period); in pwm_apply_debug()
147 if (state->enabled) in pwm_state_valid()
150 if (!state->period) in pwm_state_valid()
153 if (state->duty_cycle > state->period) in pwm_state_valid()
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()
170 return -EINVAL; in __pwm_apply()
181 if (!pwm_state_valid(&pwm->state)) { in __pwm_apply()
182 pwm->state = *state; in __pwm_apply()
186 return -EINVAL; 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()
313 state.duty_cycle = state.period - state.duty_cycle; 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()
332 const struct pwm_ops *ops = chip->ops; in pwm_capture()
334 if (!ops->capture) in pwm_capture()
335 return -ENOSYS; 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()
366 const struct pwm_ops *ops = chip->ops; in pwm_device_request()
368 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
369 return -EBUSY; in pwm_device_request()
371 if (!try_module_get(chip->owner)) in pwm_device_request()
372 return -ENODEV; in pwm_device_request()
374 if (!get_device(&chip->dev)) { in pwm_device_request()
375 err = -ENODEV; in pwm_device_request()
379 if (ops->request) { in pwm_device_request()
380 err = ops->request(chip, pwm); in pwm_device_request()
382 put_device(&chip->dev); in pwm_device_request()
384 module_put(chip->owner); in pwm_device_request()
389 if (ops->get_state) { in pwm_device_request()
391 * Zero-initialize state because most drivers are unaware of 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
432 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
433 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
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
452 if (args->args_count < 1) in of_pwm_xlate_with_flags()
453 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
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()
459 if (args->args_count > 1) 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()
463 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 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()
479 if (args->args_count > 0) 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()
483 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 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
540 guard(mutex)(&export->lock); in period_store()
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
575 guard(mutex)(&export->lock); in duty_cycle_store()
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
609 guard(mutex)(&export->lock); in enable_store()
611 pwm_get_state(pwm, &state); in enable_store()
621 return -EINVAL; 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
667 return -EINVAL; in polarity_store()
669 guard(mutex)(&export->lock); in polarity_store()
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()
723 return -EBUSY; in pwm_export_child()
727 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
728 return -ENOMEM; in pwm_export_child()
731 export->pwm = pwm; in pwm_export_child()
732 mutex_init(&export->lock); in pwm_export_child()
734 export->pwm_dev.release = pwm_export_release; in pwm_export_child()
735 export->pwm_dev.parent = pwmchip_dev; in pwm_export_child()
736 export->pwm_dev.devt = MKDEV(0, 0); in pwm_export_child()
737 export->pwm_dev.groups = pwm_groups; in pwm_export_child()
738 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); in pwm_export_child()
740 ret = device_register(&export->pwm_dev); in pwm_export_child()
742 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
743 put_device(&export->pwm_dev); in pwm_export_child()
747 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
749 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); 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()
766 return -ENODEV; in pwm_unexport_child()
768 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_unexport_child()
770 return -ENODEV; in pwm_unexport_child()
772 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
774 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_unexport_child()
780 pwm_put(pwm); in pwm_unexport_child()
790 struct pwm_device *pwm; in export_store() local
798 if (hwpwm >= chip->npwm) in export_store()
799 return -ENODEV; in export_store()
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()
825 if (hwpwm >= chip->npwm) in unexport_store()
826 return -ENODEV; in unexport_store()
828 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); in unexport_store()
839 return sysfs_emit(buf, "%u\n", chip->npwm); in npwm_show()
851 /* takes export->lock on success */
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()
869 mutex_lock(&export->lock); 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()
882 mutex_unlock(&export->lock); 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()
903 if (!export->suspend.enabled) { in pwm_class_resume_npwm()
905 mutex_unlock(&export->lock); in pwm_class_resume_npwm()
909 state.enabled = export->suspend.enabled; in pwm_class_resume_npwm()
910 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
924 for (i = 0; i < chip->npwm; i++) { in pwm_class_suspend()
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()
937 export->suspend = state; in pwm_class_suspend()
940 mutex_unlock(&export->lock); 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()
963 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); in pwm_class_resume()
969 .name = "pwm",
978 for (i = 0; i < chip->npwm; i++) { in pwmchip_sysfs_unexport()
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()
990 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); in pwmchip_priv()
996 put_device(&chip->dev); in pwmchip_put()
1019 return ERR_PTR(-ENOMEM); in pwmchip_alloc()
1021 chip->npwm = npwm; in pwmchip_alloc()
1022 chip->uses_pwmchip_alloc = true; in pwmchip_alloc()
1024 pwmchip_dev = &chip->dev; in pwmchip_alloc()
1026 pwmchip_dev->class = &pwm_class; in pwmchip_alloc()
1027 pwmchip_dev->parent = parent; in pwmchip_alloc()
1028 pwmchip_dev->release = pwmchip_release; in pwmchip_alloc()
1032 for (i = 0; i < chip->npwm; i++) { in pwmchip_alloc()
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()
1068 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) in of_pwmchip_add()
1071 if (!chip->of_xlate) in of_pwmchip_add()
1072 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
1074 of_node_get(pwmchip_parent(chip)->of_node); in of_pwmchip_add()
1080 of_node_put(pwmchip_parent(chip)->of_node); in of_pwmchip_remove()
1085 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
1087 if (!ops->apply) in pwm_ops_check()
1090 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
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
1111 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) in __pwmchip_add()
1112 return -EINVAL; in __pwmchip_add()
1120 if (!chip->uses_pwmchip_alloc) in __pwmchip_add()
1121 return -EINVAL; in __pwmchip_add()
1124 return -EINVAL; in __pwmchip_add()
1126 chip->owner = owner; in __pwmchip_add()
1134 chip->id = ret; in __pwmchip_add()
1136 dev_set_name(&chip->dev, "pwmchip%u", chip->id); in __pwmchip_add()
1141 ret = device_add(&chip->dev); in __pwmchip_add()
1151 idr_remove(&pwm_chips, chip->id); in __pwmchip_add()
1158 * pwmchip_remove() - remove a PWM chip
1159 * @chip: the PWM chip to remove
1161 * Removes a PWM chip.
1171 idr_remove(&pwm_chips, chip->id); in pwmchip_remove()
1173 device_del(&chip->dev); in pwmchip_remove()
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()
1216 return ERR_PTR(-EINVAL); in pwm_device_link_add()
1233 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
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
1243 * "pwms" property of a device tree node or a negative error-code on failure.
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()
1281 if (PTR_ERR(chip) != -EPROBE_DEFER) 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()
1309 con_id = np->name; 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
1325 * "pwms" property or a negative error-code on failure.
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
1351 return ERR_PTR(-EPROTO); in acpi_pwm_get()
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
1382 while (num--) { in pwm_add_table()
1383 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
1389 * pwm_remove_table() - unregister PWM device consumers
1397 while (num--) { in pwm_remove_table()
1398 list_del(&table->list); in pwm_remove_table()
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()
1458 * Then we take the most specific entry - with the following order in pwm_get()
1465 if (p->dev_id) { in pwm_get()
1466 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
1472 if (p->con_id) { in pwm_get()
1473 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1490 return ERR_PTR(-ENODEV); in pwm_get()
1492 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1496 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1500 if (!chip && chosen->module) { in pwm_get()
1501 err = request_module(chosen->module); in pwm_get()
1503 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1507 return ERR_PTR(-EPROBE_DEFER); 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()
1546 if (chip->ops->free) in pwm_put()
1547 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1549 pwm->label = NULL; in pwm_put()
1551 put_device(&chip->dev); in pwm_put()
1553 module_put(chip->owner); in pwm_put()
1557 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
1559 pwm_put(pwm); in devm_pwm_release()
1563 * devm_pwm_get() - resource managed pwm_get()
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()
1628 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
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()
1660 s->private = ""; in pwm_seq_start()
1672 s->private = "\n"; in pwm_seq_next()
1688 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
1689 (char *)s->private, chip->id, in pwm_seq_show()
1690 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", in pwm_seq_show()
1691 dev_name(pwmchip_parent(chip)), chip->npwm, in pwm_seq_show()
1692 (chip->npwm != 1) ? "s" : ""); 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()