@@ -271,6 +271,7 @@ int pwmchip_add(struct pwm_chip *chip)
pwm->chip = chip;
pwm->pwm = chip->base + i;
pwm->hwpwm = i;
+ pwm->state.alignment = 0;
radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
}
@@ -535,7 +536,8 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
int err;
if (!pwm || !state || !state->period ||
- state->duty_cycle > state->period)
+ state->duty_cycle > state->period ||
+ state->alignment >= state->period)
return -EINVAL;
chip = pwm->chip;
@@ -544,7 +546,8 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
state->duty_cycle == pwm->state.duty_cycle &&
state->polarity == pwm->state.polarity &&
state->enabled == pwm->state.enabled &&
- state->usage_power == pwm->state.usage_power)
+ state->usage_power == pwm->state.usage_power &&
+ state->alignment == pwm->state.alignment)
return 0;
if (chip->ops->apply) {
@@ -33,6 +33,41 @@ static struct pwm_device *child_to_pwm_device(struct device *child)
return export->pwm;
}
+static ssize_t alignment_show(struct device *child,
+ struct device_attribute *attr,
+ char *buf)
+{
+ const struct pwm_device *pwm = child_to_pwm_device(child);
+ struct pwm_state state;
+
+ pwm_get_state(pwm, &state);
+
+ return sprintf(buf, "%llu\n", state.alignment);
+}
+
+static ssize_t alignment_store(struct device *child,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ struct pwm_export *export = child_to_pwm_export(child);
+ struct pwm_device *pwm = export->pwm;
+ struct pwm_state state;
+ unsigned int val;
+ int ret;
+
+ ret = kstrtouint(buf, 0, &val);
+ if (ret)
+ return ret;
+
+ mutex_lock(&export->lock);
+ pwm_get_state(pwm, &state);
+ state.alignment = val;
+ ret = pwm_apply_state(pwm, &state);
+ mutex_unlock(&export->lock);
+
+ return ret ? : size;
+}
+
static ssize_t period_show(struct device *child,
struct device_attribute *attr,
char *buf)
@@ -219,6 +254,7 @@ static DEVICE_ATTR_RW(period);
static DEVICE_ATTR_RW(duty_cycle);
static DEVICE_ATTR_RW(enable);
static DEVICE_ATTR_RW(polarity);
+static DEVICE_ATTR_RW(alignment);
static DEVICE_ATTR_RO(capture);
static struct attribute *pwm_attrs[] = {
@@ -226,6 +262,7 @@ static struct attribute *pwm_attrs[] = {
&dev_attr_duty_cycle.attr,
&dev_attr_enable.attr,
&dev_attr_polarity.attr,
+ &dev_attr_alignment.attr,
&dev_attr_capture.attr,
NULL
};
@@ -59,6 +59,7 @@ enum {
* output but has more freedom regarding signal form.
* If supported, the signal can be optimized, for example to
* improve EMI by phase shifting individual channels.
+ * @alignment: offset in ns to device clock second
*/
struct pwm_state {
u64 period;
@@ -66,6 +67,7 @@ struct pwm_state {
enum pwm_polarity polarity;
bool enabled;
bool usage_power;
+ u64 alignment;
};
/**