<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 3, 2017 at 7:12 AM, Jani Nikula <span dir="ltr"><<a href="mailto:jani.nikula@linux.intel.com" target="_blank" class="cremed">jani.nikula@linux.intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tue, 18 Apr 2017, Puthikorn Voravootivat <<a href="mailto:puthik@chromium.org" class="cremed">puthik@chromium.org</a>> wrote:<br>
> Read desired PWM frequency from panel vbt and calculate the<br>
> value for divider in DPCD address 0x724 and 0x728 to match<br>
> that frequency as close as possible.<br>
><br>
> Signed-off-by: Puthikorn Voravootivat <<a href="mailto:puthik@chromium.org" class="cremed">puthik@chromium.org</a>><br>
> ---<br>
> drivers/gpu/drm/i915/intel_dp_<wbr>aux_backlight.c | 56 +++++++++++++++++++++++++++<br>
> 1 file changed, 56 insertions(+)<br>
><br>
> diff --git a/drivers/gpu/drm/i915/intel_<wbr>dp_aux_backlight.c b/drivers/gpu/drm/i915/intel_<wbr>dp_aux_backlight.c<br>
> index f99cf0a6ae44..9adc77bfb515 100644<br>
> --- a/drivers/gpu/drm/i915/intel_<wbr>dp_aux_backlight.c<br>
> +++ b/drivers/gpu/drm/i915/intel_<wbr>dp_aux_backlight.c<br>
> @@ -111,12 +111,60 @@ intel_dp_aux_set_dynamic_<wbr>backlight_percent(struct intel_dp *intel_dp,<br>
> dbc, sizeof(dbc));<br>
> }<br>
><br>
> +/*<br>
> + * Set PWM Frequency divider to match desired frequency in vbt.<br>
> + * The PWM Frequency is calculated as 27Mhz / (F x P).<br>
> + * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the<br>
> + * EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)<br>
> + * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the<br>
> + * EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)<br>
> + */<br>
> +static void intel_dp_aux_set_pwm_freq(<wbr>struct intel_connector *connector)<br>
> +{<br>
> + struct drm_i915_private *dev_priv = to_i915(connector->base.dev);<br>
> + struct intel_dp *intel_dp = enc_to_intel_dp(&connector-><wbr>encoder->base);<br>
> + int freq, fxp, f;<br>
> + u8 pn, pn_min, pn_max;<br>
> +<br>
> + /* Find desired value of (F x P)<br>
> + * Note that, if F x P is out of supported range, the maximum value or<br>
> + * minimum value will applied automatically. So no need to check that.<br>
> + */<br>
> + freq = dev_priv->vbt.backlight.pwm_<wbr>freq_hz;<br>
> + fxp = DP_EDP_BACKLIGHT_FREQ_BASE / freq;<br>
> +<br>
> + /* Use lowest possible value of Pn to try to make F to be between 1 and<br>
> + * 255 while still in the range Pn_min and Pn_max<br>
> + */<br>
> + if (!drm_dp_dpcd_readb(&intel_dp-<wbr>>aux,<br>
> + DP_EDP_PWMGEN_BIT_COUNT_CAP_<wbr>MIN, &pn_min)) {<br>
> + return;<br>
> + }<br>
> + if (!drm_dp_dpcd_readb(&intel_dp-<wbr>>aux,<br>
> + DP_EDP_PWMGEN_BIT_COUNT_CAP_<wbr>MAX, &pn_max)) {<br>
> + return;<br>
> + }<br>
> + pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;<br>
> + pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;<br>
> + f = fxp / (1 << pn_min);<br>
> + for (pn = pn_min; pn < pn_max && f > 255; pn++)<br>
<br>
</div></div>pn <= pn_max<br></blockquote><div>|pn < pn_max| is correct because if f is very high it is possible to get pn == pn_max+1 if we use |pn <= pn_max|</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> + f /= 2;<br>
> +<br>
> + /* Cap F to be in the range between 1 and 255. */<br>
> + f = min(f, 255);<br>
> + f = max(f, 1);<br>
<br>
</span>See clamp().<br></blockquote><div>Will use this in next patch set.</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
All in all the above is a rather complicated way to figure out how many<br>
bits you have to shift (F * P) right to fit it in 8 bits. Observe that<br>
F = (27 MHz / pwm_frew_hz) >> Pn.</blockquote><div>Agree that it is quite complicate way but I can't think of more simple way for this that still</div><div>won't get weird result if f is very high or very low. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
BR,<br>
Jani.<br>
<div><div class="h5"><br>
> +<br>
> + drm_dp_dpcd_writeb(&intel_dp-><wbr>aux, DP_EDP_PWMGEN_BIT_COUNT, pn);<br>
> + drm_dp_dpcd_writeb(&intel_dp-><wbr>aux, DP_EDP_BACKLIGHT_FREQ_SET, (u8) f);<br>
> +}<br>
> +<br>
> static void intel_dp_aux_enable_backlight(<wbr>struct intel_connector *connector)<br>
> {<br>
> struct intel_dp *intel_dp = enc_to_intel_dp(&connector-><wbr>encoder->base);<br>
> uint8_t dpcd_buf = 0;<br>
> uint8_t new_dpcd_buf = 0;<br>
> uint8_t edp_backlight_mode = 0;<br>
> + bool freq_cap;<br>
><br>
> set_aux_backlight_enable(<wbr>intel_dp, true);<br>
><br>
> @@ -147,10 +195,18 @@ static void intel_dp_aux_enable_backlight(<wbr>struct intel_connector *connector)<br>
> intel_dp_aux_set_dynamic_<wbr>backlight_percent(intel_dp, 0, 100);<br>
> }<br>
><br>
> + freq_cap = intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_<wbr>CAP;<br>
> + if (freq_cap)<br>
> + new_dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_<wbr>ENABLE;<br>
> +<br>
> if (new_dpcd_buf != dpcd_buf) {<br>
> drm_dp_dpcd_writeb(&intel_dp-><wbr>aux,<br>
> DP_EDP_BACKLIGHT_MODE_SET_<wbr>REGISTER, new_dpcd_buf);<br>
> }<br>
> +<br>
> + if (freq_cap)<br>
> + intel_dp_aux_set_pwm_freq(<wbr>connector);<br>
> +<br>
> intel_dp_aux_set_backlight(<wbr>connector, connector->panel.backlight.<wbr>level);<br>
> }<br>
<br>
--<br>
</div></div>Jani Nikula, Intel Open Source Technology Center<br>
</blockquote></div><br></div></div>