18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * A simple sysfs interface for the generic PWM framework 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on previous work by Lars Poeschel <poeschel@lemonage.de> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/device.h> 118c2ecf20Sopenharmony_ci#include <linux/mutex.h> 128c2ecf20Sopenharmony_ci#include <linux/err.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/kdev_t.h> 158c2ecf20Sopenharmony_ci#include <linux/pwm.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistruct pwm_export { 188c2ecf20Sopenharmony_ci struct device child; 198c2ecf20Sopenharmony_ci struct pwm_device *pwm; 208c2ecf20Sopenharmony_ci struct mutex lock; 218c2ecf20Sopenharmony_ci struct pwm_state suspend; 228c2ecf20Sopenharmony_ci}; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic struct pwm_export *child_to_pwm_export(struct device *child) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci return container_of(child, struct pwm_export, child); 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic struct pwm_device *child_to_pwm_device(struct device *child) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci return export->pwm; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic ssize_t period_show(struct device *child, 378c2ecf20Sopenharmony_ci struct device_attribute *attr, 388c2ecf20Sopenharmony_ci char *buf) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci const struct pwm_device *pwm = child_to_pwm_device(child); 418c2ecf20Sopenharmony_ci struct pwm_state state; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci return sprintf(buf, "%llu\n", state.period); 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic ssize_t period_store(struct device *child, 498c2ecf20Sopenharmony_ci struct device_attribute *attr, 508c2ecf20Sopenharmony_ci const char *buf, size_t size) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 538c2ecf20Sopenharmony_ci struct pwm_device *pwm = export->pwm; 548c2ecf20Sopenharmony_ci struct pwm_state state; 558c2ecf20Sopenharmony_ci u64 val; 568c2ecf20Sopenharmony_ci int ret; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci ret = kstrtou64(buf, 0, &val); 598c2ecf20Sopenharmony_ci if (ret) 608c2ecf20Sopenharmony_ci return ret; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci mutex_lock(&export->lock); 638c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 648c2ecf20Sopenharmony_ci state.period = val; 658c2ecf20Sopenharmony_ci ret = pwm_apply_state(pwm, &state); 668c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci return ret ? : size; 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic ssize_t duty_cycle_show(struct device *child, 728c2ecf20Sopenharmony_ci struct device_attribute *attr, 738c2ecf20Sopenharmony_ci char *buf) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci const struct pwm_device *pwm = child_to_pwm_device(child); 768c2ecf20Sopenharmony_ci struct pwm_state state; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return sprintf(buf, "%llu\n", state.duty_cycle); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic ssize_t duty_cycle_store(struct device *child, 848c2ecf20Sopenharmony_ci struct device_attribute *attr, 858c2ecf20Sopenharmony_ci const char *buf, size_t size) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 888c2ecf20Sopenharmony_ci struct pwm_device *pwm = export->pwm; 898c2ecf20Sopenharmony_ci struct pwm_state state; 908c2ecf20Sopenharmony_ci u64 val; 918c2ecf20Sopenharmony_ci int ret; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci ret = kstrtou64(buf, 0, &val); 948c2ecf20Sopenharmony_ci if (ret) 958c2ecf20Sopenharmony_ci return ret; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci mutex_lock(&export->lock); 988c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 998c2ecf20Sopenharmony_ci state.duty_cycle = val; 1008c2ecf20Sopenharmony_ci ret = pwm_apply_state(pwm, &state); 1018c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return ret ? : size; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic ssize_t enable_show(struct device *child, 1078c2ecf20Sopenharmony_ci struct device_attribute *attr, 1088c2ecf20Sopenharmony_ci char *buf) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci const struct pwm_device *pwm = child_to_pwm_device(child); 1118c2ecf20Sopenharmony_ci struct pwm_state state; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", state.enabled); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic ssize_t enable_store(struct device *child, 1198c2ecf20Sopenharmony_ci struct device_attribute *attr, 1208c2ecf20Sopenharmony_ci const char *buf, size_t size) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 1238c2ecf20Sopenharmony_ci struct pwm_device *pwm = export->pwm; 1248c2ecf20Sopenharmony_ci struct pwm_state state; 1258c2ecf20Sopenharmony_ci int val, ret; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 0, &val); 1288c2ecf20Sopenharmony_ci if (ret) 1298c2ecf20Sopenharmony_ci return ret; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci mutex_lock(&export->lock); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci switch (val) { 1368c2ecf20Sopenharmony_ci case 0: 1378c2ecf20Sopenharmony_ci state.enabled = false; 1388c2ecf20Sopenharmony_ci break; 1398c2ecf20Sopenharmony_ci case 1: 1408c2ecf20Sopenharmony_ci state.enabled = true; 1418c2ecf20Sopenharmony_ci break; 1428c2ecf20Sopenharmony_ci default: 1438c2ecf20Sopenharmony_ci ret = -EINVAL; 1448c2ecf20Sopenharmony_ci goto unlock; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci ret = pwm_apply_state(pwm, &state); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciunlock: 1508c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 1518c2ecf20Sopenharmony_ci return ret ? : size; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cistatic ssize_t polarity_show(struct device *child, 1558c2ecf20Sopenharmony_ci struct device_attribute *attr, 1568c2ecf20Sopenharmony_ci char *buf) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci const struct pwm_device *pwm = child_to_pwm_device(child); 1598c2ecf20Sopenharmony_ci const char *polarity = "unknown"; 1608c2ecf20Sopenharmony_ci struct pwm_state state; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci switch (state.polarity) { 1658c2ecf20Sopenharmony_ci case PWM_POLARITY_NORMAL: 1668c2ecf20Sopenharmony_ci polarity = "normal"; 1678c2ecf20Sopenharmony_ci break; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci case PWM_POLARITY_INVERSED: 1708c2ecf20Sopenharmony_ci polarity = "inversed"; 1718c2ecf20Sopenharmony_ci break; 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", polarity); 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic ssize_t polarity_store(struct device *child, 1788c2ecf20Sopenharmony_ci struct device_attribute *attr, 1798c2ecf20Sopenharmony_ci const char *buf, size_t size) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 1828c2ecf20Sopenharmony_ci struct pwm_device *pwm = export->pwm; 1838c2ecf20Sopenharmony_ci enum pwm_polarity polarity; 1848c2ecf20Sopenharmony_ci struct pwm_state state; 1858c2ecf20Sopenharmony_ci int ret; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci if (sysfs_streq(buf, "normal")) 1888c2ecf20Sopenharmony_ci polarity = PWM_POLARITY_NORMAL; 1898c2ecf20Sopenharmony_ci else if (sysfs_streq(buf, "inversed")) 1908c2ecf20Sopenharmony_ci polarity = PWM_POLARITY_INVERSED; 1918c2ecf20Sopenharmony_ci else 1928c2ecf20Sopenharmony_ci return -EINVAL; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci mutex_lock(&export->lock); 1958c2ecf20Sopenharmony_ci pwm_get_state(pwm, &state); 1968c2ecf20Sopenharmony_ci state.polarity = polarity; 1978c2ecf20Sopenharmony_ci ret = pwm_apply_state(pwm, &state); 1988c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci return ret ? : size; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic ssize_t capture_show(struct device *child, 2048c2ecf20Sopenharmony_ci struct device_attribute *attr, 2058c2ecf20Sopenharmony_ci char *buf) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci struct pwm_device *pwm = child_to_pwm_device(child); 2088c2ecf20Sopenharmony_ci struct pwm_capture result; 2098c2ecf20Sopenharmony_ci int ret; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); 2128c2ecf20Sopenharmony_ci if (ret) 2138c2ecf20Sopenharmony_ci return ret; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci return sprintf(buf, "%u %u\n", result.period, result.duty_cycle); 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(period); 2198c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(duty_cycle); 2208c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(enable); 2218c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(polarity); 2228c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(capture); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic struct attribute *pwm_attrs[] = { 2258c2ecf20Sopenharmony_ci &dev_attr_period.attr, 2268c2ecf20Sopenharmony_ci &dev_attr_duty_cycle.attr, 2278c2ecf20Sopenharmony_ci &dev_attr_enable.attr, 2288c2ecf20Sopenharmony_ci &dev_attr_polarity.attr, 2298c2ecf20Sopenharmony_ci &dev_attr_capture.attr, 2308c2ecf20Sopenharmony_ci NULL 2318c2ecf20Sopenharmony_ci}; 2328c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(pwm); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic void pwm_export_release(struct device *child) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct pwm_export *export = child_to_pwm_export(child); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci kfree(export); 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic int pwm_export_child(struct device *parent, struct pwm_device *pwm) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct pwm_export *export; 2448c2ecf20Sopenharmony_ci char *pwm_prop[2]; 2458c2ecf20Sopenharmony_ci int ret; 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) 2488c2ecf20Sopenharmony_ci return -EBUSY; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci export = kzalloc(sizeof(*export), GFP_KERNEL); 2518c2ecf20Sopenharmony_ci if (!export) { 2528c2ecf20Sopenharmony_ci clear_bit(PWMF_EXPORTED, &pwm->flags); 2538c2ecf20Sopenharmony_ci return -ENOMEM; 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci export->pwm = pwm; 2578c2ecf20Sopenharmony_ci mutex_init(&export->lock); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci export->child.release = pwm_export_release; 2608c2ecf20Sopenharmony_ci export->child.parent = parent; 2618c2ecf20Sopenharmony_ci export->child.devt = MKDEV(0, 0); 2628c2ecf20Sopenharmony_ci export->child.groups = pwm_groups; 2638c2ecf20Sopenharmony_ci dev_set_name(&export->child, "pwm%u", pwm->hwpwm); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci ret = device_register(&export->child); 2668c2ecf20Sopenharmony_ci if (ret) { 2678c2ecf20Sopenharmony_ci clear_bit(PWMF_EXPORTED, &pwm->flags); 2688c2ecf20Sopenharmony_ci put_device(&export->child); 2698c2ecf20Sopenharmony_ci export = NULL; 2708c2ecf20Sopenharmony_ci return ret; 2718c2ecf20Sopenharmony_ci } 2728c2ecf20Sopenharmony_ci pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); 2738c2ecf20Sopenharmony_ci pwm_prop[1] = NULL; 2748c2ecf20Sopenharmony_ci kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); 2758c2ecf20Sopenharmony_ci kfree(pwm_prop[0]); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci return 0; 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic int pwm_unexport_match(struct device *child, void *data) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci return child_to_pwm_device(child) == data; 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic int pwm_unexport_child(struct device *parent, struct pwm_device *pwm) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci struct device *child; 2888c2ecf20Sopenharmony_ci char *pwm_prop[2]; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) 2918c2ecf20Sopenharmony_ci return -ENODEV; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci child = device_find_child(parent, pwm, pwm_unexport_match); 2948c2ecf20Sopenharmony_ci if (!child) 2958c2ecf20Sopenharmony_ci return -ENODEV; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); 2988c2ecf20Sopenharmony_ci pwm_prop[1] = NULL; 2998c2ecf20Sopenharmony_ci kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop); 3008c2ecf20Sopenharmony_ci kfree(pwm_prop[0]); 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci /* for device_find_child() */ 3038c2ecf20Sopenharmony_ci put_device(child); 3048c2ecf20Sopenharmony_ci device_unregister(child); 3058c2ecf20Sopenharmony_ci pwm_put(pwm); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci return 0; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic ssize_t export_store(struct device *parent, 3118c2ecf20Sopenharmony_ci struct device_attribute *attr, 3128c2ecf20Sopenharmony_ci const char *buf, size_t len) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct pwm_chip *chip = dev_get_drvdata(parent); 3158c2ecf20Sopenharmony_ci struct pwm_device *pwm; 3168c2ecf20Sopenharmony_ci unsigned int hwpwm; 3178c2ecf20Sopenharmony_ci int ret; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci ret = kstrtouint(buf, 0, &hwpwm); 3208c2ecf20Sopenharmony_ci if (ret < 0) 3218c2ecf20Sopenharmony_ci return ret; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci if (hwpwm >= chip->npwm) 3248c2ecf20Sopenharmony_ci return -ENODEV; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); 3278c2ecf20Sopenharmony_ci if (IS_ERR(pwm)) 3288c2ecf20Sopenharmony_ci return PTR_ERR(pwm); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci ret = pwm_export_child(parent, pwm); 3318c2ecf20Sopenharmony_ci if (ret < 0) 3328c2ecf20Sopenharmony_ci pwm_put(pwm); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return ret ? : len; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(export); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic ssize_t unexport_store(struct device *parent, 3398c2ecf20Sopenharmony_ci struct device_attribute *attr, 3408c2ecf20Sopenharmony_ci const char *buf, size_t len) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci struct pwm_chip *chip = dev_get_drvdata(parent); 3438c2ecf20Sopenharmony_ci unsigned int hwpwm; 3448c2ecf20Sopenharmony_ci int ret; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci ret = kstrtouint(buf, 0, &hwpwm); 3478c2ecf20Sopenharmony_ci if (ret < 0) 3488c2ecf20Sopenharmony_ci return ret; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci if (hwpwm >= chip->npwm) 3518c2ecf20Sopenharmony_ci return -ENODEV; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci return ret ? : len; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(unexport); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic ssize_t npwm_show(struct device *parent, struct device_attribute *attr, 3608c2ecf20Sopenharmony_ci char *buf) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci const struct pwm_chip *chip = dev_get_drvdata(parent); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", chip->npwm); 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(npwm); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_cistatic struct attribute *pwm_chip_attrs[] = { 3698c2ecf20Sopenharmony_ci &dev_attr_export.attr, 3708c2ecf20Sopenharmony_ci &dev_attr_unexport.attr, 3718c2ecf20Sopenharmony_ci &dev_attr_npwm.attr, 3728c2ecf20Sopenharmony_ci NULL, 3738c2ecf20Sopenharmony_ci}; 3748c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(pwm_chip); 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci/* takes export->lock on success */ 3778c2ecf20Sopenharmony_cistatic struct pwm_export *pwm_class_get_state(struct device *parent, 3788c2ecf20Sopenharmony_ci struct pwm_device *pwm, 3798c2ecf20Sopenharmony_ci struct pwm_state *state) 3808c2ecf20Sopenharmony_ci{ 3818c2ecf20Sopenharmony_ci struct device *child; 3828c2ecf20Sopenharmony_ci struct pwm_export *export; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci if (!test_bit(PWMF_EXPORTED, &pwm->flags)) 3858c2ecf20Sopenharmony_ci return NULL; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci child = device_find_child(parent, pwm, pwm_unexport_match); 3888c2ecf20Sopenharmony_ci if (!child) 3898c2ecf20Sopenharmony_ci return NULL; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci export = child_to_pwm_export(child); 3928c2ecf20Sopenharmony_ci put_device(child); /* for device_find_child() */ 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci mutex_lock(&export->lock); 3958c2ecf20Sopenharmony_ci pwm_get_state(pwm, state); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci return export; 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cistatic int pwm_class_apply_state(struct pwm_export *export, 4018c2ecf20Sopenharmony_ci struct pwm_device *pwm, 4028c2ecf20Sopenharmony_ci struct pwm_state *state) 4038c2ecf20Sopenharmony_ci{ 4048c2ecf20Sopenharmony_ci int ret = pwm_apply_state(pwm, state); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci /* release lock taken in pwm_class_get_state */ 4078c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci return ret; 4108c2ecf20Sopenharmony_ci} 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_cistatic int pwm_class_resume_npwm(struct device *parent, unsigned int npwm) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci struct pwm_chip *chip = dev_get_drvdata(parent); 4158c2ecf20Sopenharmony_ci unsigned int i; 4168c2ecf20Sopenharmony_ci int ret = 0; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci for (i = 0; i < npwm; i++) { 4198c2ecf20Sopenharmony_ci struct pwm_device *pwm = &chip->pwms[i]; 4208c2ecf20Sopenharmony_ci struct pwm_state state; 4218c2ecf20Sopenharmony_ci struct pwm_export *export; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci export = pwm_class_get_state(parent, pwm, &state); 4248c2ecf20Sopenharmony_ci if (!export) 4258c2ecf20Sopenharmony_ci continue; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci /* If pwmchip was not enabled before suspend, do nothing. */ 4288c2ecf20Sopenharmony_ci if (!export->suspend.enabled) { 4298c2ecf20Sopenharmony_ci /* release lock taken in pwm_class_get_state */ 4308c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 4318c2ecf20Sopenharmony_ci continue; 4328c2ecf20Sopenharmony_ci } 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci state.enabled = export->suspend.enabled; 4358c2ecf20Sopenharmony_ci ret = pwm_class_apply_state(export, pwm, &state); 4368c2ecf20Sopenharmony_ci if (ret < 0) 4378c2ecf20Sopenharmony_ci break; 4388c2ecf20Sopenharmony_ci } 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci return ret; 4418c2ecf20Sopenharmony_ci} 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_cistatic int __maybe_unused pwm_class_suspend(struct device *parent) 4448c2ecf20Sopenharmony_ci{ 4458c2ecf20Sopenharmony_ci struct pwm_chip *chip = dev_get_drvdata(parent); 4468c2ecf20Sopenharmony_ci unsigned int i; 4478c2ecf20Sopenharmony_ci int ret = 0; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci for (i = 0; i < chip->npwm; i++) { 4508c2ecf20Sopenharmony_ci struct pwm_device *pwm = &chip->pwms[i]; 4518c2ecf20Sopenharmony_ci struct pwm_state state; 4528c2ecf20Sopenharmony_ci struct pwm_export *export; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci export = pwm_class_get_state(parent, pwm, &state); 4558c2ecf20Sopenharmony_ci if (!export) 4568c2ecf20Sopenharmony_ci continue; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci /* 4598c2ecf20Sopenharmony_ci * If pwmchip was not enabled before suspend, save 4608c2ecf20Sopenharmony_ci * state for resume time and do nothing else. 4618c2ecf20Sopenharmony_ci */ 4628c2ecf20Sopenharmony_ci export->suspend = state; 4638c2ecf20Sopenharmony_ci if (!state.enabled) { 4648c2ecf20Sopenharmony_ci /* release lock taken in pwm_class_get_state */ 4658c2ecf20Sopenharmony_ci mutex_unlock(&export->lock); 4668c2ecf20Sopenharmony_ci continue; 4678c2ecf20Sopenharmony_ci } 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci state.enabled = false; 4708c2ecf20Sopenharmony_ci ret = pwm_class_apply_state(export, pwm, &state); 4718c2ecf20Sopenharmony_ci if (ret < 0) { 4728c2ecf20Sopenharmony_ci /* 4738c2ecf20Sopenharmony_ci * roll back the PWM devices that were disabled by 4748c2ecf20Sopenharmony_ci * this suspend function. 4758c2ecf20Sopenharmony_ci */ 4768c2ecf20Sopenharmony_ci pwm_class_resume_npwm(parent, i); 4778c2ecf20Sopenharmony_ci break; 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic int __maybe_unused pwm_class_resume(struct device *parent) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci struct pwm_chip *chip = dev_get_drvdata(parent); 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci return pwm_class_resume_npwm(parent, chip->npwm); 4898c2ecf20Sopenharmony_ci} 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic struct class pwm_class = { 4948c2ecf20Sopenharmony_ci .name = "pwm", 4958c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 4968c2ecf20Sopenharmony_ci .dev_groups = pwm_chip_groups, 4978c2ecf20Sopenharmony_ci .pm = &pwm_class_pm_ops, 4988c2ecf20Sopenharmony_ci}; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_cistatic int pwmchip_sysfs_match(struct device *parent, const void *data) 5018c2ecf20Sopenharmony_ci{ 5028c2ecf20Sopenharmony_ci return dev_get_drvdata(parent) == data; 5038c2ecf20Sopenharmony_ci} 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_civoid pwmchip_sysfs_export(struct pwm_chip *chip) 5068c2ecf20Sopenharmony_ci{ 5078c2ecf20Sopenharmony_ci struct device *parent; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci /* 5108c2ecf20Sopenharmony_ci * If device_create() fails the pwm_chip is still usable by 5118c2ecf20Sopenharmony_ci * the kernel it's just not exported. 5128c2ecf20Sopenharmony_ci */ 5138c2ecf20Sopenharmony_ci parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, 5148c2ecf20Sopenharmony_ci "pwmchip%d", chip->base); 5158c2ecf20Sopenharmony_ci if (IS_ERR(parent)) { 5168c2ecf20Sopenharmony_ci dev_warn(chip->dev, 5178c2ecf20Sopenharmony_ci "device_create failed for pwm_chip sysfs export\n"); 5188c2ecf20Sopenharmony_ci } 5198c2ecf20Sopenharmony_ci} 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_civoid pwmchip_sysfs_unexport(struct pwm_chip *chip) 5228c2ecf20Sopenharmony_ci{ 5238c2ecf20Sopenharmony_ci struct device *parent; 5248c2ecf20Sopenharmony_ci unsigned int i; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci parent = class_find_device(&pwm_class, NULL, chip, 5278c2ecf20Sopenharmony_ci pwmchip_sysfs_match); 5288c2ecf20Sopenharmony_ci if (!parent) 5298c2ecf20Sopenharmony_ci return; 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci for (i = 0; i < chip->npwm; i++) { 5328c2ecf20Sopenharmony_ci struct pwm_device *pwm = &chip->pwms[i]; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci if (test_bit(PWMF_EXPORTED, &pwm->flags)) 5358c2ecf20Sopenharmony_ci pwm_unexport_child(parent, pwm); 5368c2ecf20Sopenharmony_ci } 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci put_device(parent); 5398c2ecf20Sopenharmony_ci device_unregister(parent); 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_cistatic int __init pwm_sysfs_init(void) 5438c2ecf20Sopenharmony_ci{ 5448c2ecf20Sopenharmony_ci return class_register(&pwm_class); 5458c2ecf20Sopenharmony_ci} 5468c2ecf20Sopenharmony_cisubsys_initcall(pwm_sysfs_init); 547