11487f786SFrançois Tigeot /*
21487f786SFrançois Tigeot * Copyright © 2015 Intel Corporation
31487f786SFrançois Tigeot *
41487f786SFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
51487f786SFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
61487f786SFrançois Tigeot * to deal in the Software without restriction, including without limitation
71487f786SFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81487f786SFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
91487f786SFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
101487f786SFrançois Tigeot *
111487f786SFrançois Tigeot * The above copyright notice and this permission notice (including the next
121487f786SFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
131487f786SFrançois Tigeot * Software.
141487f786SFrançois Tigeot *
151487f786SFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161487f786SFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171487f786SFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
181487f786SFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191487f786SFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201487f786SFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
211487f786SFrançois Tigeot * IN THE SOFTWARE.
221487f786SFrançois Tigeot *
231487f786SFrançois Tigeot */
241487f786SFrançois Tigeot
251487f786SFrançois Tigeot #include "intel_drv.h"
261487f786SFrançois Tigeot
set_aux_backlight_enable(struct intel_dp * intel_dp,bool enable)271487f786SFrançois Tigeot static void set_aux_backlight_enable(struct intel_dp *intel_dp, bool enable)
281487f786SFrançois Tigeot {
291487f786SFrançois Tigeot uint8_t reg_val = 0;
301487f786SFrançois Tigeot
31*3f2dd94aSFrançois Tigeot /* Early return when display use other mechanism to enable backlight. */
32*3f2dd94aSFrançois Tigeot if (!(intel_dp->edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP))
33*3f2dd94aSFrançois Tigeot return;
34*3f2dd94aSFrançois Tigeot
351487f786SFrançois Tigeot if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_DISPLAY_CONTROL_REGISTER,
361487f786SFrançois Tigeot ®_val) < 0) {
371487f786SFrançois Tigeot DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
381487f786SFrançois Tigeot DP_EDP_DISPLAY_CONTROL_REGISTER);
391487f786SFrançois Tigeot return;
401487f786SFrançois Tigeot }
411487f786SFrançois Tigeot if (enable)
421487f786SFrançois Tigeot reg_val |= DP_EDP_BACKLIGHT_ENABLE;
431487f786SFrançois Tigeot else
441487f786SFrançois Tigeot reg_val &= ~(DP_EDP_BACKLIGHT_ENABLE);
451487f786SFrançois Tigeot
461487f786SFrançois Tigeot if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_EDP_DISPLAY_CONTROL_REGISTER,
471487f786SFrançois Tigeot reg_val) != 1) {
481487f786SFrançois Tigeot DRM_DEBUG_KMS("Failed to %s aux backlight\n",
491487f786SFrançois Tigeot enable ? "enable" : "disable");
501487f786SFrançois Tigeot }
511487f786SFrançois Tigeot }
521487f786SFrançois Tigeot
531487f786SFrançois Tigeot /*
541487f786SFrançois Tigeot * Read the current backlight value from DPCD register(s) based
551487f786SFrançois Tigeot * on if 8-bit(MSB) or 16-bit(MSB and LSB) values are supported
561487f786SFrançois Tigeot */
intel_dp_aux_get_backlight(struct intel_connector * connector)571487f786SFrançois Tigeot static uint32_t intel_dp_aux_get_backlight(struct intel_connector *connector)
581487f786SFrançois Tigeot {
591487f786SFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
601487f786SFrançois Tigeot uint8_t read_val[2] = { 0x0 };
611487f786SFrançois Tigeot uint16_t level = 0;
621487f786SFrançois Tigeot
631487f786SFrançois Tigeot if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,
641487f786SFrançois Tigeot &read_val, sizeof(read_val)) < 0) {
651487f786SFrançois Tigeot DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
661487f786SFrançois Tigeot DP_EDP_BACKLIGHT_BRIGHTNESS_MSB);
671487f786SFrançois Tigeot return 0;
681487f786SFrançois Tigeot }
691487f786SFrançois Tigeot level = read_val[0];
701487f786SFrançois Tigeot if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
711487f786SFrançois Tigeot level = (read_val[0] << 8 | read_val[1]);
721487f786SFrançois Tigeot
731487f786SFrançois Tigeot return level;
741487f786SFrançois Tigeot }
751487f786SFrançois Tigeot
761487f786SFrançois Tigeot /*
771487f786SFrançois Tigeot * Sends the current backlight level over the aux channel, checking if its using
781487f786SFrançois Tigeot * 8-bit or 16 bit value (MSB and LSB)
791487f786SFrançois Tigeot */
801487f786SFrançois Tigeot static void
intel_dp_aux_set_backlight(const struct drm_connector_state * conn_state,u32 level)81*3f2dd94aSFrançois Tigeot intel_dp_aux_set_backlight(const struct drm_connector_state *conn_state, u32 level)
821487f786SFrançois Tigeot {
83*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
841487f786SFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
851487f786SFrançois Tigeot uint8_t vals[2] = { 0x0 };
861487f786SFrançois Tigeot
871487f786SFrançois Tigeot vals[0] = level;
881487f786SFrançois Tigeot
891487f786SFrançois Tigeot /* Write the MSB and/or LSB */
901487f786SFrançois Tigeot if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT) {
911487f786SFrançois Tigeot vals[0] = (level & 0xFF00) >> 8;
921487f786SFrançois Tigeot vals[1] = (level & 0xFF);
931487f786SFrançois Tigeot }
941487f786SFrançois Tigeot if (drm_dp_dpcd_write(&intel_dp->aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,
951487f786SFrançois Tigeot vals, sizeof(vals)) < 0) {
961487f786SFrançois Tigeot DRM_DEBUG_KMS("Failed to write aux backlight level\n");
971487f786SFrançois Tigeot return;
981487f786SFrançois Tigeot }
991487f786SFrançois Tigeot }
1001487f786SFrançois Tigeot
101*3f2dd94aSFrançois Tigeot /*
102*3f2dd94aSFrançois Tigeot * Set PWM Frequency divider to match desired frequency in vbt.
103*3f2dd94aSFrançois Tigeot * The PWM Frequency is calculated as 27Mhz / (F x P).
104*3f2dd94aSFrançois Tigeot * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
105*3f2dd94aSFrançois Tigeot * EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
106*3f2dd94aSFrançois Tigeot * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
107*3f2dd94aSFrançois Tigeot * EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
108*3f2dd94aSFrançois Tigeot */
intel_dp_aux_set_pwm_freq(struct intel_connector * connector)109*3f2dd94aSFrançois Tigeot static bool intel_dp_aux_set_pwm_freq(struct intel_connector *connector)
1101487f786SFrançois Tigeot {
111*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1121487f786SFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
113*3f2dd94aSFrançois Tigeot int freq, fxp, fxp_min, fxp_max, fxp_actual, f = 1;
114*3f2dd94aSFrançois Tigeot u8 pn, pn_min, pn_max;
1151487f786SFrançois Tigeot
116*3f2dd94aSFrançois Tigeot /* Find desired value of (F x P)
117*3f2dd94aSFrançois Tigeot * Note that, if F x P is out of supported range, the maximum value or
118*3f2dd94aSFrançois Tigeot * minimum value will applied automatically. So no need to check that.
119*3f2dd94aSFrançois Tigeot */
120*3f2dd94aSFrançois Tigeot freq = dev_priv->vbt.backlight.pwm_freq_hz;
121*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n", freq);
122*3f2dd94aSFrançois Tigeot if (!freq) {
123*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Use panel default backlight frequency\n");
124*3f2dd94aSFrançois Tigeot return false;
1251487f786SFrançois Tigeot }
1261487f786SFrançois Tigeot
127*3f2dd94aSFrançois Tigeot fxp = DIV_ROUND_CLOSEST(KHz(DP_EDP_BACKLIGHT_FREQ_BASE_KHZ), freq);
128*3f2dd94aSFrançois Tigeot
129*3f2dd94aSFrançois Tigeot /* Use highest possible value of Pn for more granularity of brightness
130*3f2dd94aSFrançois Tigeot * adjustment while satifying the conditions below.
131*3f2dd94aSFrançois Tigeot * - Pn is in the range of Pn_min and Pn_max
132*3f2dd94aSFrançois Tigeot * - F is in the range of 1 and 255
133*3f2dd94aSFrançois Tigeot * - FxP is within 25% of desired value.
134*3f2dd94aSFrançois Tigeot * Note: 25% is arbitrary value and may need some tweak.
135*3f2dd94aSFrançois Tigeot */
136*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_readb(&intel_dp->aux,
137*3f2dd94aSFrançois Tigeot DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min) != 1) {
138*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to read pwmgen bit count cap min\n");
139*3f2dd94aSFrançois Tigeot return false;
140*3f2dd94aSFrançois Tigeot }
141*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_readb(&intel_dp->aux,
142*3f2dd94aSFrançois Tigeot DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max) != 1) {
143*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to read pwmgen bit count cap max\n");
144*3f2dd94aSFrançois Tigeot return false;
145*3f2dd94aSFrançois Tigeot }
146*3f2dd94aSFrançois Tigeot pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
147*3f2dd94aSFrançois Tigeot pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
148*3f2dd94aSFrançois Tigeot
149*3f2dd94aSFrançois Tigeot fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
150*3f2dd94aSFrançois Tigeot fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
151*3f2dd94aSFrançois Tigeot if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
152*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("VBT defined backlight frequency out of range\n");
153*3f2dd94aSFrançois Tigeot return false;
154*3f2dd94aSFrançois Tigeot }
155*3f2dd94aSFrançois Tigeot
156*3f2dd94aSFrançois Tigeot for (pn = pn_max; pn >= pn_min; pn--) {
157*3f2dd94aSFrançois Tigeot f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
158*3f2dd94aSFrançois Tigeot fxp_actual = f << pn;
159*3f2dd94aSFrançois Tigeot if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
160*3f2dd94aSFrançois Tigeot break;
161*3f2dd94aSFrançois Tigeot }
162*3f2dd94aSFrançois Tigeot
163*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_writeb(&intel_dp->aux,
164*3f2dd94aSFrançois Tigeot DP_EDP_PWMGEN_BIT_COUNT, pn) < 0) {
165*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to write aux pwmgen bit count\n");
166*3f2dd94aSFrançois Tigeot return false;
167*3f2dd94aSFrançois Tigeot }
168*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_writeb(&intel_dp->aux,
169*3f2dd94aSFrançois Tigeot DP_EDP_BACKLIGHT_FREQ_SET, (u8) f) < 0) {
170*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to write aux backlight freq\n");
171*3f2dd94aSFrançois Tigeot return false;
172*3f2dd94aSFrançois Tigeot }
173*3f2dd94aSFrançois Tigeot return true;
174*3f2dd94aSFrançois Tigeot }
175*3f2dd94aSFrançois Tigeot
intel_dp_aux_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)176*3f2dd94aSFrançois Tigeot static void intel_dp_aux_enable_backlight(const struct intel_crtc_state *crtc_state,
177*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
1781487f786SFrançois Tigeot {
179*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
180*3f2dd94aSFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
181*3f2dd94aSFrançois Tigeot uint8_t dpcd_buf, new_dpcd_buf, edp_backlight_mode;
182*3f2dd94aSFrançois Tigeot
183*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_readb(&intel_dp->aux,
184*3f2dd94aSFrançois Tigeot DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &dpcd_buf) != 1) {
185*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
186*3f2dd94aSFrançois Tigeot DP_EDP_BACKLIGHT_MODE_SET_REGISTER);
187*3f2dd94aSFrançois Tigeot return;
188*3f2dd94aSFrançois Tigeot }
189*3f2dd94aSFrançois Tigeot
190*3f2dd94aSFrançois Tigeot new_dpcd_buf = dpcd_buf;
191*3f2dd94aSFrançois Tigeot edp_backlight_mode = dpcd_buf & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK;
192*3f2dd94aSFrançois Tigeot
193*3f2dd94aSFrançois Tigeot switch (edp_backlight_mode) {
194*3f2dd94aSFrançois Tigeot case DP_EDP_BACKLIGHT_CONTROL_MODE_PWM:
195*3f2dd94aSFrançois Tigeot case DP_EDP_BACKLIGHT_CONTROL_MODE_PRESET:
196*3f2dd94aSFrançois Tigeot case DP_EDP_BACKLIGHT_CONTROL_MODE_PRODUCT:
197*3f2dd94aSFrançois Tigeot new_dpcd_buf &= ~DP_EDP_BACKLIGHT_CONTROL_MODE_MASK;
198*3f2dd94aSFrançois Tigeot new_dpcd_buf |= DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
199*3f2dd94aSFrançois Tigeot break;
200*3f2dd94aSFrançois Tigeot
201*3f2dd94aSFrançois Tigeot /* Do nothing when it is already DPCD mode */
202*3f2dd94aSFrançois Tigeot case DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD:
203*3f2dd94aSFrançois Tigeot default:
204*3f2dd94aSFrançois Tigeot break;
205*3f2dd94aSFrançois Tigeot }
206*3f2dd94aSFrançois Tigeot
207*3f2dd94aSFrançois Tigeot if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP)
208*3f2dd94aSFrançois Tigeot if (intel_dp_aux_set_pwm_freq(connector))
209*3f2dd94aSFrançois Tigeot new_dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
210*3f2dd94aSFrançois Tigeot
211*3f2dd94aSFrançois Tigeot if (new_dpcd_buf != dpcd_buf) {
212*3f2dd94aSFrançois Tigeot if (drm_dp_dpcd_writeb(&intel_dp->aux,
213*3f2dd94aSFrançois Tigeot DP_EDP_BACKLIGHT_MODE_SET_REGISTER, new_dpcd_buf) < 0) {
214*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Failed to write aux backlight mode\n");
215*3f2dd94aSFrançois Tigeot }
216*3f2dd94aSFrançois Tigeot }
217*3f2dd94aSFrançois Tigeot
218*3f2dd94aSFrançois Tigeot set_aux_backlight_enable(intel_dp, true);
219*3f2dd94aSFrançois Tigeot intel_dp_aux_set_backlight(conn_state, connector->panel.backlight.level);
220*3f2dd94aSFrançois Tigeot }
221*3f2dd94aSFrançois Tigeot
intel_dp_aux_disable_backlight(const struct drm_connector_state * old_conn_state)222*3f2dd94aSFrançois Tigeot static void intel_dp_aux_disable_backlight(const struct drm_connector_state *old_conn_state)
223*3f2dd94aSFrançois Tigeot {
224*3f2dd94aSFrançois Tigeot set_aux_backlight_enable(enc_to_intel_dp(old_conn_state->best_encoder), false);
2251487f786SFrançois Tigeot }
2261487f786SFrançois Tigeot
intel_dp_aux_setup_backlight(struct intel_connector * connector,enum i915_pipe pipe)2271487f786SFrançois Tigeot static int intel_dp_aux_setup_backlight(struct intel_connector *connector,
2281487f786SFrançois Tigeot enum i915_pipe pipe)
2291487f786SFrançois Tigeot {
2301487f786SFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
2311487f786SFrançois Tigeot struct intel_panel *panel = &connector->panel;
2321487f786SFrançois Tigeot
2331487f786SFrançois Tigeot if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
2341487f786SFrançois Tigeot panel->backlight.max = 0xFFFF;
2351487f786SFrançois Tigeot else
2361487f786SFrançois Tigeot panel->backlight.max = 0xFF;
2371487f786SFrançois Tigeot
2381487f786SFrançois Tigeot panel->backlight.min = 0;
2391487f786SFrançois Tigeot panel->backlight.level = intel_dp_aux_get_backlight(connector);
2401487f786SFrançois Tigeot
2411487f786SFrançois Tigeot panel->backlight.enabled = panel->backlight.level != 0;
2421487f786SFrançois Tigeot
2431487f786SFrançois Tigeot return 0;
2441487f786SFrançois Tigeot }
2451487f786SFrançois Tigeot
2461487f786SFrançois Tigeot static bool
intel_dp_aux_display_control_capable(struct intel_connector * connector)2471487f786SFrançois Tigeot intel_dp_aux_display_control_capable(struct intel_connector *connector)
2481487f786SFrançois Tigeot {
2491487f786SFrançois Tigeot struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
2501487f786SFrançois Tigeot
2511487f786SFrançois Tigeot /* Check the eDP Display control capabilities registers to determine if
2521487f786SFrançois Tigeot * the panel can support backlight control over the aux channel
2531487f786SFrançois Tigeot */
2541487f786SFrançois Tigeot if (intel_dp->edp_dpcd[1] & DP_EDP_TCON_BACKLIGHT_ADJUSTMENT_CAP &&
255*3f2dd94aSFrançois Tigeot (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP) &&
256*3f2dd94aSFrançois Tigeot !(intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP)) {
2571487f786SFrançois Tigeot DRM_DEBUG_KMS("AUX Backlight Control Supported!\n");
2581487f786SFrançois Tigeot return true;
2591487f786SFrançois Tigeot }
2601487f786SFrançois Tigeot return false;
2611487f786SFrançois Tigeot }
2621487f786SFrançois Tigeot
intel_dp_aux_init_backlight_funcs(struct intel_connector * intel_connector)2631487f786SFrançois Tigeot int intel_dp_aux_init_backlight_funcs(struct intel_connector *intel_connector)
2641487f786SFrançois Tigeot {
2651487f786SFrançois Tigeot struct intel_panel *panel = &intel_connector->panel;
2661487f786SFrançois Tigeot
267*3f2dd94aSFrançois Tigeot if (!i915_modparams.enable_dpcd_backlight)
2681487f786SFrançois Tigeot return -ENODEV;
2691487f786SFrançois Tigeot
2701487f786SFrançois Tigeot if (!intel_dp_aux_display_control_capable(intel_connector))
2711487f786SFrançois Tigeot return -ENODEV;
2721487f786SFrançois Tigeot
2731487f786SFrançois Tigeot panel->backlight.setup = intel_dp_aux_setup_backlight;
2741487f786SFrançois Tigeot panel->backlight.enable = intel_dp_aux_enable_backlight;
2751487f786SFrançois Tigeot panel->backlight.disable = intel_dp_aux_disable_backlight;
2761487f786SFrançois Tigeot panel->backlight.set = intel_dp_aux_set_backlight;
2771487f786SFrançois Tigeot panel->backlight.get = intel_dp_aux_get_backlight;
2781487f786SFrançois Tigeot
2791487f786SFrançois Tigeot return 0;
2801487f786SFrançois Tigeot }
281