Lines Matching +full:- +full:pwm
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A simple sysfs interface for the generic PWM framework
15 #include <linux/pwm.h>
19 struct pwm_device *pwm; member
33 return export->pwm; in child_to_pwm_device()
40 const struct pwm_device *pwm = child_to_pwm_device(child); in period_show() local
43 pwm_get_state(pwm, &state); in period_show()
53 struct pwm_device *pwm = export->pwm; in period_store() local
62 mutex_lock(&export->lock); in period_store()
63 pwm_get_state(pwm, &state); in period_store()
65 ret = pwm_apply_might_sleep(pwm, &state); in period_store()
66 mutex_unlock(&export->lock); in period_store()
75 const struct pwm_device *pwm = child_to_pwm_device(child); in duty_cycle_show() local
78 pwm_get_state(pwm, &state); in duty_cycle_show()
88 struct pwm_device *pwm = export->pwm; in duty_cycle_store() local
97 mutex_lock(&export->lock); in duty_cycle_store()
98 pwm_get_state(pwm, &state); in duty_cycle_store()
100 ret = pwm_apply_might_sleep(pwm, &state); in duty_cycle_store()
101 mutex_unlock(&export->lock); in duty_cycle_store()
110 const struct pwm_device *pwm = child_to_pwm_device(child); in enable_show() local
113 pwm_get_state(pwm, &state); in enable_show()
123 struct pwm_device *pwm = export->pwm; in enable_store() local
131 mutex_lock(&export->lock); in enable_store()
133 pwm_get_state(pwm, &state); in enable_store()
143 ret = -EINVAL; in enable_store()
147 ret = pwm_apply_might_sleep(pwm, &state); in enable_store()
150 mutex_unlock(&export->lock); in enable_store()
158 const struct pwm_device *pwm = child_to_pwm_device(child); in polarity_show() local
162 pwm_get_state(pwm, &state); in polarity_show()
182 struct pwm_device *pwm = export->pwm; in polarity_store() local
192 return -EINVAL; in polarity_store()
194 mutex_lock(&export->lock); in polarity_store()
195 pwm_get_state(pwm, &state); in polarity_store()
197 ret = pwm_apply_might_sleep(pwm, &state); in polarity_store()
198 mutex_unlock(&export->lock); in polarity_store()
207 struct pwm_device *pwm = child_to_pwm_device(child); in capture_show() local
211 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); in capture_show()
232 ATTRIBUTE_GROUPS(pwm);
241 static int pwm_export_child(struct device *parent, struct pwm_device *pwm) in pwm_export_child() argument
247 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_export_child()
248 return -EBUSY; in pwm_export_child()
252 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
253 return -ENOMEM; in pwm_export_child()
256 export->pwm = pwm; in pwm_export_child()
257 mutex_init(&export->lock); in pwm_export_child()
259 export->child.release = pwm_export_release; in pwm_export_child()
260 export->child.parent = parent; in pwm_export_child()
261 export->child.devt = MKDEV(0, 0); in pwm_export_child()
262 export->child.groups = pwm_groups; in pwm_export_child()
263 dev_set_name(&export->child, "pwm%u", pwm->hwpwm); in pwm_export_child()
265 ret = device_register(&export->child); in pwm_export_child()
267 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
268 put_device(&export->child); in pwm_export_child()
272 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
274 kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); in pwm_export_child()
285 static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm) in pwm_unexport_child() argument
290 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_unexport_child()
291 return -ENODEV; in pwm_unexport_child()
293 child = device_find_child(parent, pwm, pwm_unexport_match); in pwm_unexport_child()
295 return -ENODEV; in pwm_unexport_child()
297 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
299 kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); in pwm_unexport_child()
305 pwm_put(pwm); in pwm_unexport_child()
315 struct pwm_device *pwm; in export_store() local
323 if (hwpwm >= chip->npwm) in export_store()
324 return -ENODEV; in export_store()
326 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); in export_store()
327 if (IS_ERR(pwm)) in export_store()
328 return PTR_ERR(pwm); in export_store()
330 ret = pwm_export_child(parent, pwm); in export_store()
332 pwm_put(pwm); in export_store()
350 if (hwpwm >= chip->npwm) in unexport_store()
351 return -ENODEV; in unexport_store()
353 ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]); in unexport_store()
364 return sysfs_emit(buf, "%u\n", chip->npwm); in npwm_show()
376 /* takes export->lock on success */
378 struct pwm_device *pwm, in pwm_class_get_state() argument
384 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_class_get_state()
387 child = device_find_child(parent, pwm, pwm_unexport_match); in pwm_class_get_state()
394 mutex_lock(&export->lock); in pwm_class_get_state()
395 pwm_get_state(pwm, state); in pwm_class_get_state()
401 struct pwm_device *pwm, in pwm_class_apply_state() argument
404 int ret = pwm_apply_might_sleep(pwm, state); in pwm_class_apply_state()
407 mutex_unlock(&export->lock); in pwm_class_apply_state()
419 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_resume_npwm() local
423 export = pwm_class_get_state(parent, pwm, &state); in pwm_class_resume_npwm()
428 if (!export->suspend.enabled) { in pwm_class_resume_npwm()
430 mutex_unlock(&export->lock); in pwm_class_resume_npwm()
434 state.enabled = export->suspend.enabled; in pwm_class_resume_npwm()
435 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
449 for (i = 0; i < chip->npwm; i++) { in pwm_class_suspend()
450 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_suspend() local
454 export = pwm_class_get_state(parent, pwm, &state); in pwm_class_suspend()
462 export->suspend = state; in pwm_class_suspend()
465 mutex_unlock(&export->lock); in pwm_class_suspend()
470 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_suspend()
473 * roll back the PWM devices that were disabled by in pwm_class_suspend()
488 return pwm_class_resume_npwm(parent, chip->npwm); in pwm_class_resume()
494 .name = "pwm",
512 parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, in pwmchip_sysfs_export()
513 "pwmchip%d", chip->id); in pwmchip_sysfs_export()
515 dev_warn(chip->dev, in pwmchip_sysfs_export()
530 for (i = 0; i < chip->npwm; i++) { in pwmchip_sysfs_unexport()
531 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_sysfs_unexport() local
533 if (test_bit(PWMF_EXPORTED, &pwm->flags)) in pwmchip_sysfs_unexport()
534 pwm_unexport_child(parent, pwm); in pwmchip_sysfs_unexport()