Merge tag 'hisi-fixes-for-4.14' of git://github.com/hisilicon/linux-hisi into next...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / intel_dp_aux_backlight.c
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include "intel_drv.h"
26
27 static void set_aux_backlight_enable(struct intel_dp *intel_dp, bool enable)
28 {
29         uint8_t reg_val = 0;
30
31         /* Early return when display use other mechanism to enable backlight. */
32         if (!(intel_dp->edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP))
33                 return;
34
35         if (drm_dp_dpcd_readb(&intel_dp->aux, DP_EDP_DISPLAY_CONTROL_REGISTER,
36                               &reg_val) < 0) {
37                 DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
38                               DP_EDP_DISPLAY_CONTROL_REGISTER);
39                 return;
40         }
41         if (enable)
42                 reg_val |= DP_EDP_BACKLIGHT_ENABLE;
43         else
44                 reg_val &= ~(DP_EDP_BACKLIGHT_ENABLE);
45
46         if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_EDP_DISPLAY_CONTROL_REGISTER,
47                                reg_val) != 1) {
48                 DRM_DEBUG_KMS("Failed to %s aux backlight\n",
49                               enable ? "enable" : "disable");
50         }
51 }
52
53 /*
54  * Read the current backlight value from DPCD register(s) based
55  * on if 8-bit(MSB) or 16-bit(MSB and LSB) values are supported
56  */
57 static uint32_t intel_dp_aux_get_backlight(struct intel_connector *connector)
58 {
59         struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
60         uint8_t read_val[2] = { 0x0 };
61         uint16_t level = 0;
62
63         if (drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,
64                              &read_val, sizeof(read_val)) < 0) {
65                 DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
66                               DP_EDP_BACKLIGHT_BRIGHTNESS_MSB);
67                 return 0;
68         }
69         level = read_val[0];
70         if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
71                 level = (read_val[0] << 8 | read_val[1]);
72
73         return level;
74 }
75
76 /*
77  * Sends the current backlight level over the aux channel, checking if its using
78  * 8-bit or 16 bit value (MSB and LSB)
79  */
80 static void
81 intel_dp_aux_set_backlight(const struct drm_connector_state *conn_state, u32 level)
82 {
83         struct intel_connector *connector = to_intel_connector(conn_state->connector);
84         struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
85         uint8_t vals[2] = { 0x0 };
86
87         vals[0] = level;
88
89         /* Write the MSB and/or LSB */
90         if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT) {
91                 vals[0] = (level & 0xFF00) >> 8;
92                 vals[1] = (level & 0xFF);
93         }
94         if (drm_dp_dpcd_write(&intel_dp->aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,
95                               vals, sizeof(vals)) < 0) {
96                 DRM_DEBUG_KMS("Failed to write aux backlight level\n");
97                 return;
98         }
99 }
100
101 static void intel_dp_aux_enable_backlight(const struct intel_crtc_state *crtc_state,
102                                           const struct drm_connector_state *conn_state)
103 {
104         struct intel_connector *connector = to_intel_connector(conn_state->connector);
105         struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
106         uint8_t dpcd_buf = 0;
107         uint8_t edp_backlight_mode = 0;
108
109         if (drm_dp_dpcd_readb(&intel_dp->aux,
110                         DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &dpcd_buf) != 1) {
111                 DRM_DEBUG_KMS("Failed to read DPCD register 0x%x\n",
112                               DP_EDP_BACKLIGHT_MODE_SET_REGISTER);
113                 return;
114         }
115
116         edp_backlight_mode = dpcd_buf & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK;
117
118         switch (edp_backlight_mode) {
119         case DP_EDP_BACKLIGHT_CONTROL_MODE_PWM:
120         case DP_EDP_BACKLIGHT_CONTROL_MODE_PRESET:
121         case DP_EDP_BACKLIGHT_CONTROL_MODE_PRODUCT:
122                 dpcd_buf &= ~DP_EDP_BACKLIGHT_CONTROL_MODE_MASK;
123                 dpcd_buf |= DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
124                 if (drm_dp_dpcd_writeb(&intel_dp->aux,
125                         DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf) < 0) {
126                         DRM_DEBUG_KMS("Failed to write aux backlight mode\n");
127                 }
128                 break;
129
130         /* Do nothing when it is already DPCD mode */
131         case DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD:
132         default:
133                 break;
134         }
135
136         set_aux_backlight_enable(intel_dp, true);
137         intel_dp_aux_set_backlight(conn_state, connector->panel.backlight.level);
138 }
139
140 static void intel_dp_aux_disable_backlight(const struct drm_connector_state *old_conn_state)
141 {
142         set_aux_backlight_enable(enc_to_intel_dp(old_conn_state->best_encoder), false);
143 }
144
145 static int intel_dp_aux_setup_backlight(struct intel_connector *connector,
146                                         enum pipe pipe)
147 {
148         struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
149         struct intel_panel *panel = &connector->panel;
150
151         if (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
152                 panel->backlight.max = 0xFFFF;
153         else
154                 panel->backlight.max = 0xFF;
155
156         panel->backlight.min = 0;
157         panel->backlight.level = intel_dp_aux_get_backlight(connector);
158
159         panel->backlight.enabled = panel->backlight.level != 0;
160
161         return 0;
162 }
163
164 static bool
165 intel_dp_aux_display_control_capable(struct intel_connector *connector)
166 {
167         struct intel_dp *intel_dp = enc_to_intel_dp(&connector->encoder->base);
168
169         /* Check the eDP Display control capabilities registers to determine if
170          * the panel can support backlight control over the aux channel
171          */
172         if (intel_dp->edp_dpcd[1] & DP_EDP_TCON_BACKLIGHT_ADJUSTMENT_CAP &&
173             (intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP) &&
174             !(intel_dp->edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP)) {
175                 DRM_DEBUG_KMS("AUX Backlight Control Supported!\n");
176                 return true;
177         }
178         return false;
179 }
180
181 int intel_dp_aux_init_backlight_funcs(struct intel_connector *intel_connector)
182 {
183         struct intel_panel *panel = &intel_connector->panel;
184
185         if (!i915.enable_dpcd_backlight)
186                 return -ENODEV;
187
188         if (!intel_dp_aux_display_control_capable(intel_connector))
189                 return -ENODEV;
190
191         panel->backlight.setup = intel_dp_aux_setup_backlight;
192         panel->backlight.enable = intel_dp_aux_enable_backlight;
193         panel->backlight.disable = intel_dp_aux_disable_backlight;
194         panel->backlight.set = intel_dp_aux_set_backlight;
195         panel->backlight.get = intel_dp_aux_get_backlight;
196
197         return 0;
198 }