^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * A simple sysfs interface for the generic PWM framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kdev_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct pwm_export {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct device child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct pwm_state suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct pwm_export *child_to_pwm_export(struct device *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return container_of(child, struct pwm_export, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct pwm_device *child_to_pwm_device(struct device *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static ssize_t period_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sprintf(buf, "%llu\n", state.period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static ssize_t period_store(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct pwm_device *pwm = export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = kstrtou64(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) state.period = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = pwm_apply_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static ssize_t duty_cycle_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return sprintf(buf, "%llu\n", state.duty_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static ssize_t duty_cycle_store(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct pwm_device *pwm = export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = kstrtou64(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) state.duty_cycle = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ret = pwm_apply_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static ssize_t oneshot_count_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return sprintf(buf, "%llu\n", state.oneshot_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static ssize_t oneshot_count_store(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct pwm_device *pwm = export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = kstrtouint(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) state.oneshot_count = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = pwm_apply_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static ssize_t enable_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return sprintf(buf, "%d\n", state.enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static ssize_t enable_store(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct pwm_device *pwm = export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int val, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = kstrtoint(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) state.enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = pwm_apply_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static ssize_t polarity_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) const char *polarity = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) switch (state.polarity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) case PWM_POLARITY_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) polarity = "normal";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) case PWM_POLARITY_INVERSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) polarity = "inversed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return sprintf(buf, "%s\n", polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static ssize_t polarity_store(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct pwm_device *pwm = export->pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) enum pwm_polarity polarity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (sysfs_streq(buf, "normal"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) polarity = PWM_POLARITY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) else if (sysfs_streq(buf, "inversed"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) polarity = PWM_POLARITY_INVERSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) state.polarity = polarity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = pwm_apply_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static ssize_t capture_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct pwm_capture result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static ssize_t output_type_show(struct device *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) const struct pwm_device *pwm = child_to_pwm_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) const char *output_type = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pwm_get_state(pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) switch (state.output_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) case PWM_OUTPUT_FIXED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) output_type = "fixed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case PWM_OUTPUT_MODULATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) output_type = "modulated";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static DEVICE_ATTR_RW(period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static DEVICE_ATTR_RW(duty_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static DEVICE_ATTR_RW(oneshot_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static DEVICE_ATTR_RW(enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static DEVICE_ATTR_RW(polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static DEVICE_ATTR_RO(capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static DEVICE_ATTR_RO(output_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static struct attribute *pwm_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) &dev_attr_period.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) &dev_attr_duty_cycle.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) &dev_attr_oneshot_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) &dev_attr_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) &dev_attr_polarity.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) &dev_attr_capture.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) &dev_attr_output_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ATTRIBUTE_GROUPS(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void pwm_export_release(struct device *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct pwm_export *export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) kfree(export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct pwm_export *export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) char *pwm_prop[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) export = kzalloc(sizeof(*export), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) clear_bit(PWMF_EXPORTED, &pwm->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) export->pwm = pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) mutex_init(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) export->child.release = pwm_export_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) export->child.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) export->child.devt = MKDEV(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) export->child.groups = pwm_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = device_register(&export->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) clear_bit(PWMF_EXPORTED, &pwm->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) put_device(&export->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) export = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) pwm_prop[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) kfree(pwm_prop[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int pwm_unexport_match(struct device *child, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return child_to_pwm_device(child) == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct device *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) char *pwm_prop[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) child = device_find_child(parent, pwm, pwm_unexport_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) pwm_prop[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) kfree(pwm_prop[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* for device_find_child() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) put_device(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) device_unregister(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) pwm_put(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static ssize_t export_store(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) unsigned int hwpwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ret = kstrtouint(buf, 0, &hwpwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (hwpwm >= chip->npwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (IS_ERR(pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return PTR_ERR(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = pwm_export_child(parent, pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pwm_put(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return ret ? : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static DEVICE_ATTR_WO(export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static ssize_t unexport_store(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) unsigned int hwpwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) ret = kstrtouint(buf, 0, &hwpwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (hwpwm >= chip->npwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return ret ? : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static DEVICE_ATTR_WO(unexport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) const struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return sprintf(buf, "%u\n", chip->npwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static DEVICE_ATTR_RO(npwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static struct attribute *pwm_chip_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) &dev_attr_export.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) &dev_attr_unexport.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) &dev_attr_npwm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) ATTRIBUTE_GROUPS(pwm_chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* takes export->lock on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static struct pwm_export *pwm_class_get_state(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct pwm_device *pwm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct pwm_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct device *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct pwm_export *export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (!test_bit(PWMF_EXPORTED, &pwm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) child = device_find_child(parent, pwm, pwm_unexport_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (!child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) export = child_to_pwm_export(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) put_device(child); /* for device_find_child() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) mutex_lock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) pwm_get_state(pwm, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static int pwm_class_apply_state(struct pwm_export *export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) struct pwm_device *pwm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct pwm_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int ret = pwm_apply_state(pwm, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /* release lock taken in pwm_class_get_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) mutex_unlock(&export->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) for (i = 0; i < npwm; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct pwm_device *pwm = &chip->pwms[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct pwm_export *export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) export = pwm_class_get_state(parent, pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) state.enabled = export->suspend.enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ret = pwm_class_apply_state(export, pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static int __maybe_unused pwm_class_suspend(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) for (i = 0; i < chip->npwm; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct pwm_device *pwm = &chip->pwms[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct pwm_export *export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) export = pwm_class_get_state(parent, pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) export->suspend = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) ret = pwm_class_apply_state(export, pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * roll back the PWM devices that were disabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * this suspend function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) pwm_class_resume_npwm(parent, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) static int __maybe_unused pwm_class_resume(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct pwm_chip *chip = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return pwm_class_resume_npwm(parent, chip->npwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static struct class pwm_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) .name = "pwm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) .dev_groups = pwm_chip_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) .pm = &pwm_class_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static int pwmchip_sysfs_match(struct device *parent, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return dev_get_drvdata(parent) == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) void pwmchip_sysfs_export(struct pwm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * If device_create() fails the pwm_chip is still usable by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * the kernel it's just not exported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) "pwmchip%d", chip->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (IS_ERR(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) dev_warn(chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) "device_create failed for pwm_chip sysfs export\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void pwmchip_sysfs_unexport(struct pwm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) parent = class_find_device(&pwm_class, NULL, chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) pwmchip_sysfs_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) for (i = 0; i < chip->npwm; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct pwm_device *pwm = &chip->pwms[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (test_bit(PWMF_EXPORTED, &pwm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) pwm_unexport_child(parent, pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) put_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) device_unregister(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static int __init pwm_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return class_register(&pwm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) subsys_initcall(pwm_sysfs_init);