Skip to contentSkip to conversation

pwm: lpss: Add get_state callback

PATCH In review Hans de Goede 3 participants 7 messages Feb 27, 2017 – Mar 26, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 pwm: lpss: Add get_state callback

Add a get_state callback so that the initial state correctly reflects the actual hardware state.

---
Changes in v2:
-Rebase on top of linux-pwm/for-next
Changes in v3:
-Invert on_time_div 255 means 0 brightness not max brightness -Use do_div to avoid issues with 64 bit division on 32 bit archs -Use !!foo instead of foo? true:fale

---
 drivers/pwm/pwm-lpss.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
drivers/pwm/pwm-lpss.c +33 -0
index 0b549dc..6605777 100644
@@ -154,8 +154,41 @@ static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	return ret;
 }
 
+static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	unsigned long base_unit_range;
+	unsigned long long base_unit, freq, on_time_div;
+	u32 ctrl;
+
+	base_unit_range = BIT(lpwm->info->base_unit_bits);
+
+	ctrl = pwm_lpss_read(pwm);
+	on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
+	base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
+
+	freq = base_unit * lpwm->info->clk_rate;
+	do_div(freq, base_unit_range);
+	if (freq == 0)
+		state->period = NSEC_PER_SEC;
+	else
+		state->period = NSEC_PER_SEC / (unsigned long)freq;
+
+	on_time_div *= state->period;
+	do_div(on_time_div, 255);
+	state->duty_cycle = on_time_div;
+
+	state->polarity = PWM_POLARITY_NORMAL;
+	state->enabled = !!(ctrl & PWM_ENABLE);
+
+	if (state->enabled)
+		pm_runtime_get_sync(chip->dev);
+}
+
 static const struct pwm_ops pwm_lpss_ops = {
 	.apply = pwm_lpss_apply,
+	.get_state = pwm_lpss_get_state,
 	.owner = THIS_MODULE,
 };
 
Signature · 1 line
2.9.3
#1 lore raw
Quoted 12 lines · Hans de Goede

Add a get_state callback so that the initial state correctly reflects the actual hardware state.

drivers/pwm/pwm-lpss.c +9 -0
+static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+                              struct pwm_state *state)
+{
+       struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+       unsigned long base_unit_range;
+       unsigned long long base_unit, freq, on_time_div;
+       u32 ctrl;
+
+       base_unit_range = BIT(lpwm->info->base_unit_bits);

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,

Quoted 10 lines
drivers/pwm/pwm-lpss.c +9 -0
+
+       ctrl = pwm_lpss_read(pwm);
+       on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
+       base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
+
+       freq = base_unit * lpwm->info->clk_rate;
+       do_div(freq, base_unit_range);
 
+       if (freq == 0)
+               state->period = NSEC_PER_SEC;

And this is wrong. IIRC base_unit = 0 *or* on_time_div = 255 means duty cycle = 100%.

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 176

Quoted 2 lines
drivers/pwm/pwm-lpss.c +2 -0
+       else
+               state->period = NSEC_PER_SEC / (unsigned long)freq;

I would stop doing versions so fast and give one more try with carefully chosen variable types and perhaps a bit straighter arithmetic here.

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,

Quoted 11 lines
drivers/pwm/pwm-lpss.c +10 -0
+
+       on_time_div *= state->period;
+       do_div(on_time_div, 255);
+       state->duty_cycle = on_time_div;
+
+       state->polarity = PWM_POLARITY_NORMAL;
+       state->enabled = !!(ctrl & PWM_ENABLE);
+
 
+       if (state->enabled)
+               pm_runtime_get_sync(chip->dev);

Since you are too fast (again), let me ask where is the balanced put for this call?
Second question, how ->get_state() is supposed to work if power is off when it enters it?

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi,

Quoted 21 lines · Andy Shevchenko
Quoted 19 lines · Hans de Goede

Add a get_state callback so that the initial state correctly reflects the actual hardware state.

drivers/pwm/pwm-lpss.c +16 -0
+static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+                              struct pwm_state *state)
+{
+       struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+       unsigned long base_unit_range;
+       unsigned long long base_unit, freq, on_time_div;
+       u32 ctrl;
+
+       base_unit_range = BIT(lpwm->info->base_unit_bits);
+
+       ctrl = pwm_lpss_read(pwm);
+       on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
+       base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
+
+       freq = base_unit * lpwm->info->clk_rate;
+       do_div(freq, base_unit_range);
Quoted 5 lines

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 174

Quoted 2 lines
drivers/pwm/pwm-lpss.c +2 -0
+       if (freq == 0)
+               state->period = NSEC_PER_SEC;

And this is wrong. IIRC base_unit = 0 *or* on_time_div = 255 means duty cycle = 100%.

Ok, so maybe we should do this then ?

	if (freq == 0) {
		state->period = NSEC_PER_SEC;
		state->duty_cycle = NSEC_PER_SEC;
		return;
	}

?

Quoted 7 lines

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 176

Quoted 3 lines
drivers/pwm/pwm-lpss.c +2 -0
+       else
+               state->period = NSEC_PER_SEC / (unsigned long)freq;

I would stop doing versions so fast and give one more try with carefully chosen variable types and perhaps a bit straighter arithmetic here.

I've carefully chosen the variable types, freq needs to be 64 bits for do_div usage. Here I'm going from freq (which has been divided by now) to a period time in nsec, for which:

	state->period = NSEC_PER_SEC / (unsigned long)freq;

Is as straight forward as one can get, the (unsigned long) cast is there to force a 32 bit division since freq is known to fit in 32 bits at this time.

Quoted 9 lines
Quoted 8 lines
drivers/pwm/pwm-lpss.c +8 -0
+
+       on_time_div *= state->period;
+       do_div(on_time_div, 255);
+       state->duty_cycle = on_time_div;
+
+       state->polarity = PWM_POLARITY_NORMAL;
+       state->enabled = !!(ctrl & PWM_ENABLE);
+
Quoted 5 lines

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 186

Quoted 2 lines
drivers/pwm/pwm-lpss.c +2 -0
+       if (state->enabled)
+               pm_runtime_get_sync(chip->dev);

Since you are too fast (again), let me ask where is the balanced put for this call?

As I explained in my earlier mail this is balanced by the disable path in apply(). But I guess what you are getting at is what if the driver gets removed while the pwm is enabled? It seems that we then indeed leak our runtime-pm ref, note that is a pre-existing bug.

Anyways since we now may get a pm-ref during probe I will add the balancing (and equally conditional) put of that ref to pwm_lpss_remove().

Quoted 2 lines

Second question, how ->get_state() is supposed to work if power is off when it enters it?

get_state only gets called on probe and the device is guaranteed to be powered on during probe().

Regards,

Hans

#3 lore raw
Quoted 23 lines · Hans de Goede

Hi,

Quoted 17 lines · Andy Shevchenko
Quoted 11 lines · Hans de Goede

+       base_unit_range = BIT(lpwm->info->base_unit_bits); +
+       ctrl = pwm_lpss_read(pwm);
+       on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK); +       base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
+
+       freq = base_unit * lpwm->info->clk_rate; +       do_div(freq, base_unit_range);
+       if (freq == 0)
+               state->period = NSEC_PER_SEC;

And this is wrong. IIRC base_unit = 0 *or* on_time_div = 255 means duty cycle = 100%.

Ok, so maybe we should do this then ?

Quoted 7 lines
	if (freq == 0) {
		state->period = NSEC_PER_SEC;
		state->duty_cycle = NSEC_PER_SEC;
		return;
	}

?

But the period is not that one. Can we assign them to 0?

Quoted 13 lines
Quoted 7 lines
Quoted 3 lines

+       else
+               state->period = NSEC_PER_SEC / (unsigned long)freq;

I would stop doing versions so fast and give one more try with carefully chosen variable types and perhaps a bit straighter arithmetic here.

I've carefully chosen the variable types, freq needs to be 64 bits for do_div usage. Here I'm going from freq (which has been divided by now) to a period time in nsec, for which:

Quoted 6 lines
	state->period = NSEC_PER_SEC / (unsigned long)freq;

Is as straight forward as one can get, the (unsigned long) cast is there to force a 32 bit division since freq is known to fit in 32 bits at this time.

OK!

Quoted 15 lines
Quoted 14 lines

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 186

Quoted 11 lines
drivers/pwm/pwm-lpss.c +10 -0
+
+       on_time_div *= state->period;
+       do_div(on_time_div, 255);
+       state->duty_cycle = on_time_div;
+
+       state->polarity = PWM_POLARITY_NORMAL;
+       state->enabled = !!(ctrl & PWM_ENABLE);
+
+       if (state->enabled)
+               pm_runtime_get_sync(chip->dev);

Since you are too fast (again), let me ask where is the balanced put for this call?

Quoted 5 lines

As I explained in my earlier mail this is balanced by the disable path in apply(). But I guess what you are getting at is what if the driver gets removed while the pwm is enabled? It seems that we then indeed leak our runtime-pm ref, note that is a pre-existing bug.

I see.

Quoted 10 lines

Anyways since we now may get a pm-ref during probe I will add the balancing (and equally conditional) put of that ref to pwm_lpss_remove().

Quoted 3 lines

Second question, how ->get_state() is supposed to work if power is off
when it enters it?

get_state only gets called on probe and the device is guaranteed to be powered on during probe().

Ah, seems I now understand what this call back actually does.

So, the name of the callback a bit confusing, to me it sounds rather "get_to_[initial_]state()".

Thus, would it be better to split ->apply() to get a common part for both?

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#4 lore raw

Hi,

Quoted 30 lines · Andy Shevchenko
Quoted 29 lines · Hans de Goede

Hi,

Quoted 17 lines · Andy Shevchenko

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 174

Quoted 11 lines · Hans de Goede
drivers/pwm/pwm-lpss.c +5 -0
+       base_unit_range = BIT(lpwm->info->base_unit_bits);
+
+       ctrl = pwm_lpss_read(pwm);
+       on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
+       base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) &

(base_unit_range - 1);

drivers/pwm/pwm-lpss.c +5 -0
+
+       freq = base_unit * lpwm->info->clk_rate;
+       do_div(freq, base_unit_range);
+       if (freq == 0)
+               state->period = NSEC_PER_SEC;

And this is wrong. IIRC base_unit = 0 *or* on_time_div = 255 means duty cycle = 100%.

Ok, so maybe we should do this then ?

	if (freq == 0) {
		state->period = NSEC_PER_SEC;
		state->duty_cycle = NSEC_PER_SEC;
		return;
	}
Quoted 3 lines
Quoted 1 line

?

But the period is not that one. Can we assign them to 0?

That will cause a divide by 0 in apply() when a user calls apply without changing / setting period.

Quoted 57 lines
Quoted 14 lines
Quoted 8 lines
Quoted 4 lines
(quoted diff) +2 -0
+       else
+               state->period = NSEC_PER_SEC / (unsigned

long)freq;

I would stop doing versions so fast and give one more try with carefully chosen variable types and perhaps a bit straighter arithmetic here.

I've carefully chosen the variable types, freq needs to be 64 bits for do_div usage. Here I'm going from freq (which has been divided by now) to a period time in nsec, for which:

Quoted 6 lines
	state->period = NSEC_PER_SEC / (unsigned long)freq;

Is as straight forward as one can get, the (unsigned long) cast is there to force a 32 bit division since freq is known to fit in 32 bits at this time.

OK!

Quoted 15 lines
Quoted 14 lines

drivers/pwm/pwm-lpss.c static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 186

Quoted 11 lines
drivers/pwm/pwm-lpss.c +10 -0
+
+       on_time_div *= state->period;
+       do_div(on_time_div, 255);
+       state->duty_cycle = on_time_div;
+
+       state->polarity = PWM_POLARITY_NORMAL;
+       state->enabled = !!(ctrl & PWM_ENABLE);
+
+       if (state->enabled)
+               pm_runtime_get_sync(chip->dev);

Since you are too fast (again), let me ask where is the balanced put for this call?

Quoted 5 lines

As I explained in my earlier mail this is balanced by the disable path in apply(). But I guess what you are getting at is what if the driver gets removed while the pwm is enabled? It seems that we then indeed leak our runtime-pm ref, note that is a pre-existing bug.

I see.

Quoted 10 lines

Anyways since we now may get a pm-ref during probe I will add the balancing (and equally conditional) put of that ref to pwm_lpss_remove().

Quoted 3 lines

Second question, how ->get_state() is supposed to work if power is off
when it enters it?

get_state only gets called on probe and the device is guaranteed to be powered on during probe().

Quoted 7 lines

Ah, seems I now understand what this call back actually does.

So, the name of the callback a bit confusing, to me it sounds rather "get_to_[initial_]state()".

Thus, would it be better to split ->apply() to get a common part for both?

I don't understand what you mean here ? Ah I guess you mean split out the pm_runtime_get / put stuff. I don't think that is going to work well as apply compares old and new state and we do not have old state when get_state gets called.

Regards,

Hans

Quoted 1 line
#5 lore raw
Quoted 19 lines · Hans de Goede

Hi,

Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Hans de Goede

Hi,

Quoted 3 lines · Andy Shevchenko

On Mon, Feb 27, 2017 at 6:59 PM, Hans de Goede <hdegoede@redhat. com>
wrote:

Ok, so maybe we should do this then ?

	if (freq == 0) {
		state->period = NSEC_PER_SEC;
		state->duty_cycle = NSEC_PER_SEC;
		return;
	}
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

?

But the period is not that one. Can we assign them to 0?

That will cause a divide by 0 in apply() when a user calls apply without changing / setting period.

I have to look closer to this one. Perhaps next week together with yours other change.

Quoted 18 lines
Quoted 18 lines
Quoted 4 lines

Anyways since we now may get a pm-ref during probe I will add the balancing (and equally conditional) put of that ref to pwm_lpss_remove().

Quoted 8 lines
Quoted 4 lines

Second question, how ->get_state() is supposed to work if power is
off
when it enters it?

get_state only gets called on probe and the device is guaranteed to
be powered on during probe().

Ah, seems I now understand what this call back actually does.

So, the name of the callback a bit confusing, to me it sounds rather "get_to_[initial_]state()".

Quoted 7 lines
Quoted 2 lines

Thus, would it be better to split ->apply() to get a common part for both?

I don't understand what you mean here ? Ah I guess you mean split out the pm_runtime_get / put stuff. I don't think that is going to work well as apply compares old and new state and we do not have old state when get_state gets called.

I mean to have similar flow in ->get_state() and ->apply() when we enable the device.

I don't like ->get_state() name as I mentioned before. If its functionality is "get *to* (initial) state", that what we need to implement.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#6 lore raw

Hi,

Quoted 22 lines · Andy Shevchenko
Quoted 21 lines · Hans de Goede

Hi,

Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · Hans de Goede

Hi,

Quoted 3 lines · Andy Shevchenko

On Mon, Feb 27, 2017 at 6:59 PM, Hans de Goede <hdegoede@redhat. com>
wrote:

Ok, so maybe we should do this then ?

	if (freq == 0) {
		state->period = NSEC_PER_SEC;
		state->duty_cycle = NSEC_PER_SEC;
		return;
	}

?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

But the period is not that one. Can we assign them to 0?

That will cause a divide by 0 in apply() when a user calls apply without changing / setting period.

I have to look closer to this one. Perhaps next week together with yours other change.

Ok, note this one has no hurry.

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 4 lines

Anyways since we now may get a pm-ref during probe I will add the balancing (and equally conditional) put of that ref to pwm_lpss_remove().

Quoted 8 lines
Quoted 4 lines

Second question, how ->get_state() is supposed to work if power is
off
when it enters it?

get_state only gets called on probe and the device is guaranteed to
be powered on during probe().

Ah, seems I now understand what this call back actually does.

So, the name of the callback a bit confusing, to me it sounds rather "get_to_[initial_]state()".

Quoted 7 lines
Quoted 2 lines

Thus, would it be better to split ->apply() to get a common part for both?

I don't understand what you mean here ? Ah I guess you mean split out the pm_runtime_get / put stuff. I don't think that is going to work well as apply compares old and new state and we do not have old state when get_state gets called.

Quoted 6 lines

I mean to have similar flow in ->get_state() and ->apply() when we enable the device.

I don't like ->get_state() name as I mentioned before. If its functionality is "get *to* (initial) state", that what we need to implement.

The way this works is part of the pwm core, also I still do not understand what you're trying to say here, can you give some pseudo-code as example how you want to see things split ?

Regards,

Hans