@@ -27,7 +27,11 @@ struct pwm_sysfs {
uint64_t period;
uint64_t duty_cycle;
bool inverted_polarity;
- bool cache_valid;
+
+ bool enabled_cache_valid;
+ bool period_cache_valid;
+ bool duty_cycle_cache_valid;
+ bool polarity_cache_valid;
};
struct pwm_chip_sysfs {
@@ -117,7 +121,10 @@ static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
chip_sysfs->pwms[offset] = pwm_sysfs;
- pwm_sysfs->cache_valid = false;
+ pwm_sysfs->enabled_cache_valid = false;
+ pwm_sysfs->period_cache_valid = false;
+ pwm_sysfs->duty_cycle_cache_valid = false;
+ pwm_sysfs->polarity_cache_valid = false;
return pwm;
}
@@ -208,7 +215,7 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
return 0;
}
- if (!pwm_sysfs->cache_valid ||
+ if (!pwm_sysfs->polarity_cache_valid ||
(wf->duty_offset_ns >= wf->period_length_ns - wf->duty_length_ns) != pwm_sysfs->inverted_polarity) {
if (wf->duty_length_ns == wf->period_length_ns || wf->duty_length_ns == 0) {
/*
@@ -221,7 +228,7 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
* minimize sysfs access, keep the current polarity in
* this case.
*/
- if (!pwm_sysfs->cache_valid) {
+ if (!pwm_sysfs->polarity_cache_valid) {
char buf[20];
ret = pwm_chip_sysfs_read_prop(pwm_sysfs, "polarity", buf, sizeof(buf));
@@ -242,7 +249,9 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
pwm_sysfs->inverted_polarity = true;
}
+
}
+ pwm_sysfs->polarity_cache_valid = true;
if (pwm_sysfs->inverted_polarity)
duty_cycle = wf->period_length_ns - wf->duty_length_ns;
@@ -254,9 +263,9 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
* duty_cycle cannot be done in a single step write period first if
* period increases and write duty_cycle first if period decreases.
*/
- if (!pwm_sysfs->cache_valid ||
+ if (!pwm_sysfs->period_cache_valid || !pwm_sysfs->duty_cycle_cache_valid ||
pwm_sysfs->period <= wf->period_length_ns) {
- if (!pwm_sysfs->cache_valid ||
+ if (!pwm_sysfs->period_cache_valid ||
pwm_sysfs->period != wf->period_length_ns) {
ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "period",
"%" PRIu64 "\n", wf->period_length_ns);
@@ -264,8 +273,9 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
return ret;
pwm_sysfs->period = wf->period_length_ns;
}
+ pwm_sysfs->period_cache_valid = true;
- if (!pwm_sysfs->cache_valid ||
+ if (!pwm_sysfs->duty_cycle_cache_valid ||
pwm_sysfs->duty_cycle != wf->duty_length_ns) {
ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
"%" PRIu64 "\n", duty_cycle);
@@ -273,6 +283,7 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
return ret;
pwm_sysfs->duty_cycle = duty_cycle;
}
+ pwm_sysfs->duty_cycle_cache_valid = true;
} else {
if (pwm_sysfs->duty_cycle != wf->duty_length_ns) {
ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
@@ -281,6 +292,7 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
return ret;
pwm_sysfs->duty_cycle = duty_cycle;
}
+ pwm_sysfs->duty_cycle_cache_valid = true;
/*
* It's already known that
@@ -292,15 +304,16 @@ static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
if (ret)
return ret;
pwm_sysfs->period = wf->period_length_ns;
+ pwm_sysfs->period_cache_valid = true;
}
- if (!pwm_sysfs->cache_valid || !pwm_sysfs->enabled) {
+ if (!pwm_sysfs->enabled_cache_valid || !pwm_sysfs->enabled) {
ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "enable", "1\n");
if (ret)
return ret;
pwm_sysfs->enabled = true;
}
- pwm_sysfs->cache_valid = true;
+ pwm_sysfs->enabled_cache_valid = true;
return 0;
}
To save a few slow sysfs write accesses, track the validity of the four sysfs properties separately. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com> --- sysfs.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-)