blob: 61d69c214508e68fe61a349d3bd4a60c132bffb7 [file] [log] [blame]
Jani Nikula4e646492013-08-27 15:12:20 +03001/*
2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Author: Jani Nikula <jani.nikula@intel.com>
24 */
25
26#include <drm/drmP.h>
Matt Roperc6f95f22015-01-22 16:50:32 -080027#include <drm/drm_atomic_helper.h>
Jani Nikula4e646492013-08-27 15:12:20 +030028#include <drm/drm_crtc.h>
29#include <drm/drm_edid.h>
30#include <drm/i915_drm.h>
Jani Nikula593e0622015-01-23 15:30:56 +020031#include <drm/drm_panel.h>
Jani Nikula7e9804f2015-01-16 14:27:23 +020032#include <drm/drm_mipi_dsi.h>
Jani Nikula4e646492013-08-27 15:12:20 +030033#include <linux/slab.h>
Shobhit Kumarfc45e822015-06-26 14:32:09 +053034#include <linux/gpio/consumer.h>
Jani Nikula4e646492013-08-27 15:12:20 +030035#include "i915_drv.h"
36#include "intel_drv.h"
37#include "intel_dsi.h"
Jani Nikula4e646492013-08-27 15:12:20 +030038
Jani Nikula593e0622015-01-23 15:30:56 +020039static const struct {
40 u16 panel_id;
41 struct drm_panel * (*init)(struct intel_dsi *intel_dsi, u16 panel_id);
42} intel_dsi_drivers[] = {
Shobhit Kumar2ab8b452014-05-23 21:35:27 +053043 {
44 .panel_id = MIPI_DSI_GENERIC_PANEL_ID,
Jani Nikula593e0622015-01-23 15:30:56 +020045 .init = vbt_panel_init,
Shobhit Kumar2ab8b452014-05-23 21:35:27 +053046 },
Jani Nikula4e646492013-08-27 15:12:20 +030047};
48
Jani Nikula7f6a6a42015-01-16 14:27:19 +020049static void wait_for_dsi_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
Jani Nikula3b1808b2015-01-16 14:27:18 +020050{
51 struct drm_encoder *encoder = &intel_dsi->base.base;
52 struct drm_device *dev = encoder->dev;
53 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3b1808b2015-01-16 14:27:18 +020054 u32 mask;
55
56 mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
57 LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
58
59 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & mask) == mask, 100))
60 DRM_ERROR("DPI FIFOs are not empty\n");
61}
62
Jani Nikula7e9804f2015-01-16 14:27:23 +020063static void write_data(struct drm_i915_private *dev_priv, u32 reg,
64 const u8 *data, u32 len)
65{
66 u32 i, j;
67
68 for (i = 0; i < len; i += 4) {
69 u32 val = 0;
70
71 for (j = 0; j < min_t(u32, len - i, 4); j++)
72 val |= *data++ << 8 * j;
73
74 I915_WRITE(reg, val);
75 }
76}
77
78static void read_data(struct drm_i915_private *dev_priv, u32 reg,
79 u8 *data, u32 len)
80{
81 u32 i, j;
82
83 for (i = 0; i < len; i += 4) {
84 u32 val = I915_READ(reg);
85
86 for (j = 0; j < min_t(u32, len - i, 4); j++)
87 *data++ = val >> 8 * j;
88 }
89}
90
91static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
92 const struct mipi_dsi_msg *msg)
93{
94 struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
95 struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
96 struct drm_i915_private *dev_priv = dev->dev_private;
97 enum port port = intel_dsi_host->port;
98 struct mipi_dsi_packet packet;
99 ssize_t ret;
100 const u8 *header, *data;
101 u32 data_reg, data_mask, ctrl_reg, ctrl_mask;
102
103 ret = mipi_dsi_create_packet(&packet, msg);
104 if (ret < 0)
105 return ret;
106
107 header = packet.header;
108 data = packet.payload;
109
110 if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
111 data_reg = MIPI_LP_GEN_DATA(port);
112 data_mask = LP_DATA_FIFO_FULL;
113 ctrl_reg = MIPI_LP_GEN_CTRL(port);
114 ctrl_mask = LP_CTRL_FIFO_FULL;
115 } else {
116 data_reg = MIPI_HS_GEN_DATA(port);
117 data_mask = HS_DATA_FIFO_FULL;
118 ctrl_reg = MIPI_HS_GEN_CTRL(port);
119 ctrl_mask = HS_CTRL_FIFO_FULL;
120 }
121
122 /* note: this is never true for reads */
123 if (packet.payload_length) {
124
125 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & data_mask) == 0, 50))
126 DRM_ERROR("Timeout waiting for HS/LP DATA FIFO !full\n");
127
128 write_data(dev_priv, data_reg, packet.payload,
129 packet.payload_length);
130 }
131
132 if (msg->rx_len) {
133 I915_WRITE(MIPI_INTR_STAT(port), GEN_READ_DATA_AVAIL);
134 }
135
136 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & ctrl_mask) == 0, 50)) {
137 DRM_ERROR("Timeout waiting for HS/LP CTRL FIFO !full\n");
138 }
139
140 I915_WRITE(ctrl_reg, header[2] << 16 | header[1] << 8 | header[0]);
141
142 /* ->rx_len is set only for reads */
143 if (msg->rx_len) {
144 data_mask = GEN_READ_DATA_AVAIL;
145 if (wait_for((I915_READ(MIPI_INTR_STAT(port)) & data_mask) == data_mask, 50))
146 DRM_ERROR("Timeout waiting for read data.\n");
147
148 read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
149 }
150
151 /* XXX: fix for reads and writes */
152 return 4 + packet.payload_length;
153}
154
155static int intel_dsi_host_attach(struct mipi_dsi_host *host,
156 struct mipi_dsi_device *dsi)
157{
158 return 0;
159}
160
161static int intel_dsi_host_detach(struct mipi_dsi_host *host,
162 struct mipi_dsi_device *dsi)
163{
164 return 0;
165}
166
167static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
168 .attach = intel_dsi_host_attach,
169 .detach = intel_dsi_host_detach,
170 .transfer = intel_dsi_host_transfer,
171};
172
173static struct intel_dsi_host *intel_dsi_host_init(struct intel_dsi *intel_dsi,
174 enum port port)
175{
176 struct intel_dsi_host *host;
177 struct mipi_dsi_device *device;
178
179 host = kzalloc(sizeof(*host), GFP_KERNEL);
180 if (!host)
181 return NULL;
182
183 host->base.ops = &intel_dsi_host_ops;
184 host->intel_dsi = intel_dsi;
185 host->port = port;
186
187 /*
188 * We should call mipi_dsi_host_register(&host->base) here, but we don't
189 * have a host->dev, and we don't have OF stuff either. So just use the
190 * dsi framework as a library and hope for the best. Create the dsi
191 * devices by ourselves here too. Need to be careful though, because we
192 * don't initialize any of the driver model devices here.
193 */
194 device = kzalloc(sizeof(*device), GFP_KERNEL);
195 if (!device) {
196 kfree(host);
197 return NULL;
198 }
199
200 device->host = &host->base;
201 host->device = device;
202
203 return host;
204}
205
Jani Nikulaa2581a92015-01-16 14:27:26 +0200206/*
207 * send a video mode command
208 *
209 * XXX: commands with data in MIPI_DPI_DATA?
210 */
211static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
212 enum port port)
213{
214 struct drm_encoder *encoder = &intel_dsi->base.base;
215 struct drm_device *dev = encoder->dev;
216 struct drm_i915_private *dev_priv = dev->dev_private;
217 u32 mask;
218
219 /* XXX: pipe, hs */
220 if (hs)
221 cmd &= ~DPI_LP_MODE;
222 else
223 cmd |= DPI_LP_MODE;
224
225 /* clear bit */
226 I915_WRITE(MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
227
228 /* XXX: old code skips write if control unchanged */
229 if (cmd == I915_READ(MIPI_DPI_CONTROL(port)))
230 DRM_ERROR("Same special packet %02x twice in a row.\n", cmd);
231
232 I915_WRITE(MIPI_DPI_CONTROL(port), cmd);
233
234 mask = SPL_PKT_SENT_INTERRUPT;
235 if (wait_for((I915_READ(MIPI_INTR_STAT(port)) & mask) == mask, 100))
236 DRM_ERROR("Video mode command 0x%08x send failed.\n", cmd);
237
238 return 0;
239}
240
Shobhit Kumare9fe51c2013-12-10 12:14:55 +0530241static void band_gap_reset(struct drm_i915_private *dev_priv)
Shobhit Kumar4ce8c9a2013-08-27 15:12:24 +0300242{
Ville Syrjäläa5805162015-05-26 20:42:30 +0300243 mutex_lock(&dev_priv->sb_lock);
Shobhit Kumar4ce8c9a2013-08-27 15:12:24 +0300244
Shobhit Kumare9fe51c2013-12-10 12:14:55 +0530245 vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
246 vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
247 vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
248 udelay(150);
249 vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
250 vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
Shobhit Kumar4ce8c9a2013-08-27 15:12:24 +0300251
Ville Syrjäläa5805162015-05-26 20:42:30 +0300252 mutex_unlock(&dev_priv->sb_lock);
Shobhit Kumar4ce8c9a2013-08-27 15:12:24 +0300253}
254
Jani Nikula4e646492013-08-27 15:12:20 +0300255static inline bool is_vid_mode(struct intel_dsi *intel_dsi)
256{
Shobhit Kumardfba2e22014-04-14 11:18:24 +0530257 return intel_dsi->operation_mode == INTEL_DSI_VIDEO_MODE;
Jani Nikula4e646492013-08-27 15:12:20 +0300258}
259
260static inline bool is_cmd_mode(struct intel_dsi *intel_dsi)
261{
Shobhit Kumardfba2e22014-04-14 11:18:24 +0530262 return intel_dsi->operation_mode == INTEL_DSI_COMMAND_MODE;
Jani Nikula4e646492013-08-27 15:12:20 +0300263}
264
Jani Nikula4e646492013-08-27 15:12:20 +0300265static bool intel_dsi_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200266 struct intel_crtc_state *config)
Jani Nikula4e646492013-08-27 15:12:20 +0300267{
268 struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
269 base);
270 struct intel_connector *intel_connector = intel_dsi->attached_connector;
271 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200272 struct drm_display_mode *adjusted_mode = &config->base.adjusted_mode;
Jani Nikula4e646492013-08-27 15:12:20 +0300273
274 DRM_DEBUG_KMS("\n");
275
276 if (fixed_mode)
277 intel_fixed_panel_mode(fixed_mode, adjusted_mode);
278
Shobhit Kumarf573de52014-07-30 20:32:37 +0530279 /* DSI uses short packets for sync events, so clear mode flags for DSI */
280 adjusted_mode->flags = 0;
281
Jani Nikula4e646492013-08-27 15:12:20 +0300282 return true;
283}
284
Gaurav K Singh5505a242014-12-04 10:58:47 +0530285static void intel_dsi_port_enable(struct intel_encoder *encoder)
286{
287 struct drm_device *dev = encoder->base.dev;
288 struct drm_i915_private *dev_priv = dev->dev_private;
289 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
290 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Gaurav K Singh369602d2014-12-05 14:09:28 +0530291 enum port port;
Gaurav K Singh5505a242014-12-04 10:58:47 +0530292 u32 temp;
293
Gaurav K Singha9da9bc2014-12-05 14:13:41 +0530294 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
295 temp = I915_READ(VLV_CHICKEN_3);
296 temp &= ~PIXEL_OVERLAP_CNT_MASK |
297 intel_dsi->pixel_overlap <<
298 PIXEL_OVERLAP_CNT_SHIFT;
299 I915_WRITE(VLV_CHICKEN_3, temp);
300 }
301
Gaurav K Singh369602d2014-12-05 14:09:28 +0530302 for_each_dsi_port(port, intel_dsi->ports) {
303 temp = I915_READ(MIPI_PORT_CTRL(port));
304 temp &= ~LANE_CONFIGURATION_MASK;
305 temp &= ~DUAL_LINK_MODE_MASK;
306
307 if (intel_dsi->ports == ((1 << PORT_A) | (1 << PORT_C))) {
308 temp |= (intel_dsi->dual_link - 1)
309 << DUAL_LINK_MODE_SHIFT;
310 temp |= intel_crtc->pipe ?
311 LANE_CONFIGURATION_DUAL_LINK_B :
312 LANE_CONFIGURATION_DUAL_LINK_A;
313 }
314 /* assert ip_tg_enable signal */
315 I915_WRITE(MIPI_PORT_CTRL(port), temp | DPI_ENABLE);
316 POSTING_READ(MIPI_PORT_CTRL(port));
317 }
Gaurav K Singh5505a242014-12-04 10:58:47 +0530318}
319
320static void intel_dsi_port_disable(struct intel_encoder *encoder)
321{
322 struct drm_device *dev = encoder->base.dev;
323 struct drm_i915_private *dev_priv = dev->dev_private;
Gaurav K Singh369602d2014-12-05 14:09:28 +0530324 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
325 enum port port;
Gaurav K Singh5505a242014-12-04 10:58:47 +0530326 u32 temp;
327
Gaurav K Singh369602d2014-12-05 14:09:28 +0530328 for_each_dsi_port(port, intel_dsi->ports) {
329 /* de-assert ip_tg_enable signal */
330 temp = I915_READ(MIPI_PORT_CTRL(port));
331 I915_WRITE(MIPI_PORT_CTRL(port), temp & ~DPI_ENABLE);
332 POSTING_READ(MIPI_PORT_CTRL(port));
333 }
Gaurav K Singh5505a242014-12-04 10:58:47 +0530334}
335
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530336static void intel_dsi_device_ready(struct intel_encoder *encoder)
337{
338 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530339 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
340 enum port port;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530341 u32 val;
342
343 DRM_DEBUG_KMS("\n");
344
Ville Syrjäläa5805162015-05-26 20:42:30 +0300345 mutex_lock(&dev_priv->sb_lock);
Shobhit Kumar2095f9f2014-04-09 13:59:30 +0530346 /* program rcomp for compliance, reduce from 50 ohms to 45 ohms
347 * needed everytime after power gate */
348 vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
Ville Syrjäläa5805162015-05-26 20:42:30 +0300349 mutex_unlock(&dev_priv->sb_lock);
Shobhit Kumar2095f9f2014-04-09 13:59:30 +0530350
351 /* bandgap reset is needed after everytime we do power gate */
352 band_gap_reset(dev_priv);
353
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530354 for_each_dsi_port(port, intel_dsi->ports) {
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530355
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530356 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_ENTER);
357 usleep_range(2500, 3000);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530358
Gaurav K Singhbf344e82014-12-07 16:13:54 +0530359 /* Enable MIPI PHY transparent latch
360 * Common bit for both MIPI Port A & MIPI Port C
361 * No similar bit in MIPI Port C reg
362 */
Shobhit Kumar4ba7d932015-02-05 17:08:45 +0530363 val = I915_READ(MIPI_PORT_CTRL(PORT_A));
Gaurav K Singhbf344e82014-12-07 16:13:54 +0530364 I915_WRITE(MIPI_PORT_CTRL(PORT_A), val | LP_OUTPUT_HOLD);
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530365 usleep_range(1000, 1500);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530366
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530367 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_EXIT);
368 usleep_range(2500, 3000);
369
370 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY);
371 usleep_range(2500, 3000);
372 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530373}
Jani Nikula4e646492013-08-27 15:12:20 +0300374
375static void intel_dsi_enable(struct intel_encoder *encoder)
376{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530377 struct drm_device *dev = encoder->base.dev;
378 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4e646492013-08-27 15:12:20 +0300379 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Jani Nikula4934b652015-01-22 15:01:35 +0200380 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300381
382 DRM_DEBUG_KMS("\n");
383
Jani Nikula4934b652015-01-22 15:01:35 +0200384 if (is_cmd_mode(intel_dsi)) {
385 for_each_dsi_port(port, intel_dsi->ports)
386 I915_WRITE(MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
387 } else {
Jani Nikula4e646492013-08-27 15:12:20 +0300388 msleep(20); /* XXX */
Jani Nikulaf03e4172015-01-16 14:27:16 +0200389 for_each_dsi_port(port, intel_dsi->ports)
Jani Nikulaa2581a92015-01-16 14:27:26 +0200390 dpi_send_cmd(intel_dsi, TURN_ON, false, port);
Jani Nikula4e646492013-08-27 15:12:20 +0300391 msleep(100);
392
Jani Nikula593e0622015-01-23 15:30:56 +0200393 drm_panel_enable(intel_dsi->panel);
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530394
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200395 for_each_dsi_port(port, intel_dsi->ports)
396 wait_for_dsi_fifo_empty(intel_dsi, port);
Shobhit Kumar13813082014-07-12 17:17:22 +0530397
Gaurav K Singh5505a242014-12-04 10:58:47 +0530398 intel_dsi_port_enable(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300399 }
Shobhit Kumarb029e662015-06-26 14:32:10 +0530400
401 intel_panel_enable_backlight(intel_dsi->attached_connector);
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530402}
Jani Nikula4e646492013-08-27 15:12:20 +0300403
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530404static void intel_dsi_pre_enable(struct intel_encoder *encoder)
405{
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530406 struct drm_device *dev = encoder->base.dev;
407 struct drm_i915_private *dev_priv = dev->dev_private;
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530408 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530409 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
410 enum pipe pipe = intel_crtc->pipe;
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200411 enum port port;
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530412 u32 tmp;
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530413
414 DRM_DEBUG_KMS("\n");
415
Shobhit Kumarfc45e822015-06-26 14:32:09 +0530416 /* Panel Enable over CRC PMIC */
417 if (intel_dsi->gpio_panel)
418 gpiod_set_value_cansleep(intel_dsi->gpio_panel, 1);
419
420 msleep(intel_dsi->panel_on_delay);
421
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530422 /* Disable DPOunit clock gating, can stall pipe
423 * and we need DPLL REFA always enabled */
424 tmp = I915_READ(DPLL(pipe));
Ville Syrjälä60bfe442015-06-29 15:25:49 +0300425 tmp |= DPLL_REF_CLK_ENABLE_VLV;
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530426 I915_WRITE(DPLL(pipe), tmp);
427
Shobhit Kumarf573de52014-07-30 20:32:37 +0530428 /* update the hw state for DPLL */
Ville Syrjälä60bfe442015-06-29 15:25:49 +0300429 intel_crtc->config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
430 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530431
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530432 tmp = I915_READ(DSPCLK_GATE_D);
433 tmp |= DPOUNIT_CLOCK_GATE_DISABLE;
434 I915_WRITE(DSPCLK_GATE_D, tmp);
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530435
436 /* put device in ready state */
437 intel_dsi_device_ready(encoder);
438
Jani Nikula593e0622015-01-23 15:30:56 +0200439 drm_panel_prepare(intel_dsi->panel);
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530440
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200441 for_each_dsi_port(port, intel_dsi->ports)
442 wait_for_dsi_fifo_empty(intel_dsi, port);
Shobhit Kumar13813082014-07-12 17:17:22 +0530443
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530444 /* Enable port in pre-enable phase itself because as per hw team
445 * recommendation, port should be enabled befor plane & pipe */
446 intel_dsi_enable(encoder);
447}
448
449static void intel_dsi_enable_nop(struct intel_encoder *encoder)
450{
451 DRM_DEBUG_KMS("\n");
452
453 /* for DSI port enable has to be done before pipe
454 * and plane enable, so port enable is done in
455 * pre_enable phase itself unlike other encoders
456 */
Jani Nikula4e646492013-08-27 15:12:20 +0300457}
458
Imre Deakc315faf2014-05-27 19:00:09 +0300459static void intel_dsi_pre_disable(struct intel_encoder *encoder)
460{
461 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Jani Nikulaf03e4172015-01-16 14:27:16 +0200462 enum port port;
Imre Deakc315faf2014-05-27 19:00:09 +0300463
464 DRM_DEBUG_KMS("\n");
465
Shobhit Kumarb029e662015-06-26 14:32:10 +0530466 intel_panel_disable_backlight(intel_dsi->attached_connector);
467
Imre Deakc315faf2014-05-27 19:00:09 +0300468 if (is_vid_mode(intel_dsi)) {
469 /* Send Shutdown command to the panel in LP mode */
Jani Nikulaf03e4172015-01-16 14:27:16 +0200470 for_each_dsi_port(port, intel_dsi->ports)
Jani Nikulaa2581a92015-01-16 14:27:26 +0200471 dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
Imre Deakc315faf2014-05-27 19:00:09 +0300472 msleep(10);
473 }
474}
475
Jani Nikula4e646492013-08-27 15:12:20 +0300476static void intel_dsi_disable(struct intel_encoder *encoder)
477{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530478 struct drm_device *dev = encoder->base.dev;
479 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4e646492013-08-27 15:12:20 +0300480 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530481 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300482 u32 temp;
483
484 DRM_DEBUG_KMS("\n");
485
Jani Nikula4e646492013-08-27 15:12:20 +0300486 if (is_vid_mode(intel_dsi)) {
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200487 for_each_dsi_port(port, intel_dsi->ports)
488 wait_for_dsi_fifo_empty(intel_dsi, port);
Shobhit Kumar13813082014-07-12 17:17:22 +0530489
Gaurav K Singh5505a242014-12-04 10:58:47 +0530490 intel_dsi_port_disable(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300491 msleep(2);
492 }
493
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530494 for_each_dsi_port(port, intel_dsi->ports) {
495 /* Panel commands can be sent when clock is in LP11 */
496 I915_WRITE(MIPI_DEVICE_READY(port), 0x0);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530497
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530498 temp = I915_READ(MIPI_CTRL(port));
499 temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
500 I915_WRITE(MIPI_CTRL(port), temp |
501 intel_dsi->escape_clk_div <<
502 ESCAPE_CLOCK_DIVIDER_SHIFT);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530503
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530504 I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530505
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530506 temp = I915_READ(MIPI_DSI_FUNC_PRG(port));
507 temp &= ~VID_MODE_FORMAT_MASK;
508 I915_WRITE(MIPI_DSI_FUNC_PRG(port), temp);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530509
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530510 I915_WRITE(MIPI_DEVICE_READY(port), 0x1);
511 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530512 /* if disable packets are sent before sending shutdown packet then in
513 * some next enable sequence send turn on packet error is observed */
Jani Nikula593e0622015-01-23 15:30:56 +0200514 drm_panel_disable(intel_dsi->panel);
Shobhit Kumar13813082014-07-12 17:17:22 +0530515
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200516 for_each_dsi_port(port, intel_dsi->ports)
517 wait_for_dsi_fifo_empty(intel_dsi, port);
Jani Nikula4e646492013-08-27 15:12:20 +0300518}
519
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530520static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
Jani Nikula4e646492013-08-27 15:12:20 +0300521{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530522 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530523 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
524 enum port port;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530525 u32 val;
526
Jani Nikula4e646492013-08-27 15:12:20 +0300527 DRM_DEBUG_KMS("\n");
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530528 for_each_dsi_port(port, intel_dsi->ports) {
ymohanmabe4fc042013-08-27 23:40:56 +0300529
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530530 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
531 ULPS_STATE_ENTER);
532 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530533
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530534 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
535 ULPS_STATE_EXIT);
536 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530537
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530538 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
539 ULPS_STATE_ENTER);
540 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530541
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530542 /* Wait till Clock lanes are in LP-00 state for MIPI Port A
543 * only. MIPI Port C has no similar bit for checking
544 */
545 if (wait_for(((I915_READ(MIPI_PORT_CTRL(PORT_A)) & AFE_LATCHOUT)
546 == 0x00000), 30))
547 DRM_ERROR("DSI LP not going Low\n");
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530548
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530549 /* Disable MIPI PHY transparent latch
550 * Common bit for both MIPI Port A & MIPI Port C
551 */
Shobhit Kumar4ba7d932015-02-05 17:08:45 +0530552 val = I915_READ(MIPI_PORT_CTRL(PORT_A));
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530553 I915_WRITE(MIPI_PORT_CTRL(PORT_A), val & ~LP_OUTPUT_HOLD);
554 usleep_range(1000, 1500);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530555
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530556 I915_WRITE(MIPI_DEVICE_READY(port), 0x00);
557 usleep_range(2000, 2500);
558 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530559
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530560 intel_disable_dsi_pll(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300561}
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530562
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530563static void intel_dsi_post_disable(struct intel_encoder *encoder)
564{
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530565 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530566 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530567 u32 val;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530568
569 DRM_DEBUG_KMS("\n");
570
Imre Deakc315faf2014-05-27 19:00:09 +0300571 intel_dsi_disable(encoder);
572
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530573 intel_dsi_clear_device_ready(encoder);
574
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530575 val = I915_READ(DSPCLK_GATE_D);
576 val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
577 I915_WRITE(DSPCLK_GATE_D, val);
578
Jani Nikula593e0622015-01-23 15:30:56 +0200579 drm_panel_unprepare(intel_dsi->panel);
Shobhit Kumardf38e652014-04-14 11:18:26 +0530580
581 msleep(intel_dsi->panel_off_delay);
582 msleep(intel_dsi->panel_pwr_cycle_delay);
Shobhit Kumarfc45e822015-06-26 14:32:09 +0530583
584 /* Panel Disable over CRC PMIC */
585 if (intel_dsi->gpio_panel)
586 gpiod_set_value_cansleep(intel_dsi->gpio_panel, 0);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530587}
Jani Nikula4e646492013-08-27 15:12:20 +0300588
589static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
590 enum pipe *pipe)
591{
592 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530593 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
594 struct drm_device *dev = encoder->base.dev;
Imre Deak6d129be2014-03-05 16:20:54 +0200595 enum intel_display_power_domain power_domain;
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530596 u32 dpi_enabled, func;
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200597 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300598
599 DRM_DEBUG_KMS("\n");
600
Imre Deak6d129be2014-03-05 16:20:54 +0200601 power_domain = intel_display_port_power_domain(encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200602 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +0200603 return false;
604
Jani Nikula4e646492013-08-27 15:12:20 +0300605 /* XXX: this only works for one DSI output */
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530606 for_each_dsi_port(port, intel_dsi->ports) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200607 func = I915_READ(MIPI_DSI_FUNC_PRG(port));
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530608 dpi_enabled = I915_READ(MIPI_PORT_CTRL(port)) &
609 DPI_ENABLE;
Jani Nikula4e646492013-08-27 15:12:20 +0300610
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530611 /* Due to some hardware limitations on BYT, MIPI Port C DPI
612 * Enable bit does not get set. To check whether DSI Port C
613 * was enabled in BIOS, check the Pipe B enable bit
614 */
615 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
616 (port == PORT_C))
617 dpi_enabled = I915_READ(PIPECONF(PIPE_B)) &
618 PIPECONF_ENABLE;
619
620 if (dpi_enabled || (func & CMD_MODE_DATA_WIDTH_MASK)) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200621 if (I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY) {
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530622 *pipe = port == PORT_A ? PIPE_A : PIPE_B;
Jani Nikula4e646492013-08-27 15:12:20 +0300623 return true;
624 }
625 }
626 }
627
628 return false;
629}
630
631static void intel_dsi_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200632 struct intel_crtc_state *pipe_config)
Jani Nikula4e646492013-08-27 15:12:20 +0300633{
Shobhit Kumarf573de52014-07-30 20:32:37 +0530634 u32 pclk;
Jani Nikula4e646492013-08-27 15:12:20 +0300635 DRM_DEBUG_KMS("\n");
636
Shobhit Kumarf573de52014-07-30 20:32:37 +0530637 /*
638 * DPLL_MD is not used in case of DSI, reading will get some default value
639 * set dpll_md = 0
640 */
641 pipe_config->dpll_hw_state.dpll_md = 0;
642
643 pclk = vlv_get_dsi_pclk(encoder, pipe_config->pipe_bpp);
644 if (!pclk)
645 return;
646
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200647 pipe_config->base.adjusted_mode.crtc_clock = pclk;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530648 pipe_config->port_clock = pclk;
Jani Nikula4e646492013-08-27 15:12:20 +0300649}
650
Damien Lespiauc19de8e2013-11-28 15:29:18 +0000651static enum drm_mode_status
652intel_dsi_mode_valid(struct drm_connector *connector,
653 struct drm_display_mode *mode)
Jani Nikula4e646492013-08-27 15:12:20 +0300654{
655 struct intel_connector *intel_connector = to_intel_connector(connector);
656 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Mika Kahola759a1e92015-08-18 14:37:01 +0300657 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
Jani Nikula4e646492013-08-27 15:12:20 +0300658
659 DRM_DEBUG_KMS("\n");
660
661 if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
662 DRM_DEBUG_KMS("MODE_NO_DBLESCAN\n");
663 return MODE_NO_DBLESCAN;
664 }
665
666 if (fixed_mode) {
667 if (mode->hdisplay > fixed_mode->hdisplay)
668 return MODE_PANEL;
669 if (mode->vdisplay > fixed_mode->vdisplay)
670 return MODE_PANEL;
Mika Kahola759a1e92015-08-18 14:37:01 +0300671 if (fixed_mode->clock > max_dotclk)
672 return MODE_CLOCK_HIGH;
Jani Nikula4e646492013-08-27 15:12:20 +0300673 }
674
Jani Nikula36d21f42015-01-16 14:27:20 +0200675 return MODE_OK;
Jani Nikula4e646492013-08-27 15:12:20 +0300676}
677
678/* return txclkesc cycles in terms of divider and duration in us */
679static u16 txclkesc(u32 divider, unsigned int us)
680{
681 switch (divider) {
682 case ESCAPE_CLOCK_DIVIDER_1:
683 default:
684 return 20 * us;
685 case ESCAPE_CLOCK_DIVIDER_2:
686 return 10 * us;
687 case ESCAPE_CLOCK_DIVIDER_4:
688 return 5 * us;
689 }
690}
691
692/* return pixels in terms of txbyteclkhs */
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530693static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
694 u16 burst_mode_ratio)
Jani Nikula4e646492013-08-27 15:12:20 +0300695{
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530696 return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200697 8 * 100), lane_count);
Jani Nikula4e646492013-08-27 15:12:20 +0300698}
699
700static void set_dsi_timings(struct drm_encoder *encoder,
701 const struct drm_display_mode *mode)
702{
703 struct drm_device *dev = encoder->dev;
704 struct drm_i915_private *dev_priv = dev->dev_private;
705 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
706 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530707 enum port port;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200708 unsigned int bpp = intel_crtc->config->pipe_bpp;
Jani Nikula4e646492013-08-27 15:12:20 +0300709 unsigned int lane_count = intel_dsi->lane_count;
710
711 u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
712
713 hactive = mode->hdisplay;
714 hfp = mode->hsync_start - mode->hdisplay;
715 hsync = mode->hsync_end - mode->hsync_start;
716 hbp = mode->htotal - mode->hsync_end;
717
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530718 if (intel_dsi->dual_link) {
719 hactive /= 2;
720 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
721 hactive += intel_dsi->pixel_overlap;
722 hfp /= 2;
723 hsync /= 2;
724 hbp /= 2;
725 }
726
Jani Nikula4e646492013-08-27 15:12:20 +0300727 vfp = mode->vsync_start - mode->vdisplay;
728 vsync = mode->vsync_end - mode->vsync_start;
729 vbp = mode->vtotal - mode->vsync_end;
730
731 /* horizontal values are in terms of high speed byte clock */
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530732 hactive = txbyteclkhs(hactive, bpp, lane_count,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200733 intel_dsi->burst_mode_ratio);
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530734 hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
735 hsync = txbyteclkhs(hsync, bpp, lane_count,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200736 intel_dsi->burst_mode_ratio);
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530737 hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
Jani Nikula4e646492013-08-27 15:12:20 +0300738
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530739 for_each_dsi_port(port, intel_dsi->ports) {
Shashank Sharmad2e08c02015-09-01 19:41:40 +0530740 if (IS_BROXTON(dev)) {
741 /*
742 * Program hdisplay and vdisplay on MIPI transcoder.
743 * This is different from calculated hactive and
744 * vactive, as they are calculated per channel basis,
745 * whereas these values should be based on resolution.
746 */
747 I915_WRITE(BXT_MIPI_TRANS_HACTIVE(port),
748 mode->hdisplay);
749 I915_WRITE(BXT_MIPI_TRANS_VACTIVE(port),
750 mode->vdisplay);
751 I915_WRITE(BXT_MIPI_TRANS_VTOTAL(port),
752 mode->vtotal);
753 }
754
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530755 I915_WRITE(MIPI_HACTIVE_AREA_COUNT(port), hactive);
756 I915_WRITE(MIPI_HFP_COUNT(port), hfp);
Jani Nikula4e646492013-08-27 15:12:20 +0300757
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530758 /* meaningful for video mode non-burst sync pulse mode only,
759 * can be zero for non-burst sync events and burst modes */
760 I915_WRITE(MIPI_HSYNC_PADDING_COUNT(port), hsync);
761 I915_WRITE(MIPI_HBP_COUNT(port), hbp);
Jani Nikula4e646492013-08-27 15:12:20 +0300762
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530763 /* vertical values are in terms of lines */
764 I915_WRITE(MIPI_VFP_COUNT(port), vfp);
765 I915_WRITE(MIPI_VSYNC_PADDING_COUNT(port), vsync);
766 I915_WRITE(MIPI_VBP_COUNT(port), vbp);
767 }
Jani Nikula4e646492013-08-27 15:12:20 +0300768}
769
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200770static void intel_dsi_prepare(struct intel_encoder *intel_encoder)
Jani Nikula4e646492013-08-27 15:12:20 +0300771{
772 struct drm_encoder *encoder = &intel_encoder->base;
773 struct drm_device *dev = encoder->dev;
774 struct drm_i915_private *dev_priv = dev->dev_private;
775 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
776 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
777 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200778 &intel_crtc->config->base.adjusted_mode;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530779 enum port port;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200780 unsigned int bpp = intel_crtc->config->pipe_bpp;
Jani Nikula4e646492013-08-27 15:12:20 +0300781 u32 val, tmp;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530782 u16 mode_hdisplay;
Jani Nikula4e646492013-08-27 15:12:20 +0300783
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200784 DRM_DEBUG_KMS("pipe %c\n", pipe_name(intel_crtc->pipe));
Jani Nikula4e646492013-08-27 15:12:20 +0300785
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530786 mode_hdisplay = adjusted_mode->hdisplay;
Jani Nikula4e646492013-08-27 15:12:20 +0300787
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530788 if (intel_dsi->dual_link) {
789 mode_hdisplay /= 2;
790 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
791 mode_hdisplay += intel_dsi->pixel_overlap;
792 }
Jani Nikula4e646492013-08-27 15:12:20 +0300793
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530794 for_each_dsi_port(port, intel_dsi->ports) {
Shashank Sharmad2e08c02015-09-01 19:41:40 +0530795 if (IS_VALLEYVIEW(dev)) {
796 /*
797 * escape clock divider, 20MHz, shared for A and C.
798 * device ready must be off when doing this! txclkesc?
799 */
800 tmp = I915_READ(MIPI_CTRL(PORT_A));
801 tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
802 I915_WRITE(MIPI_CTRL(PORT_A), tmp |
803 ESCAPE_CLOCK_DIVIDER_1);
Jani Nikula4e646492013-08-27 15:12:20 +0300804
Shashank Sharmad2e08c02015-09-01 19:41:40 +0530805 /* read request priority is per pipe */
806 tmp = I915_READ(MIPI_CTRL(port));
807 tmp &= ~READ_REQUEST_PRIORITY_MASK;
808 I915_WRITE(MIPI_CTRL(port), tmp |
809 READ_REQUEST_PRIORITY_HIGH);
810 } else if (IS_BROXTON(dev)) {
811 /*
812 * FIXME:
813 * BXT can connect any PIPE to any MIPI port.
814 * Select the pipe based on the MIPI port read from
815 * VBT for now. Pick PIPE A for MIPI port A and C
816 * for port C.
817 */
818 tmp = I915_READ(MIPI_CTRL(port));
819 tmp &= ~BXT_PIPE_SELECT_MASK;
820
821 if (port == PORT_A)
822 tmp |= BXT_PIPE_SELECT_A;
823 else if (port == PORT_C)
824 tmp |= BXT_PIPE_SELECT_C;
825
826 I915_WRITE(MIPI_CTRL(port), tmp);
827 }
Jani Nikula4e646492013-08-27 15:12:20 +0300828
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530829 /* XXX: why here, why like this? handling in irq handler?! */
830 I915_WRITE(MIPI_INTR_STAT(port), 0xffffffff);
831 I915_WRITE(MIPI_INTR_EN(port), 0xffffffff);
832
833 I915_WRITE(MIPI_DPHY_PARAM(port), intel_dsi->dphy_reg);
834
835 I915_WRITE(MIPI_DPI_RESOLUTION(port),
836 adjusted_mode->vdisplay << VERTICAL_ADDRESS_SHIFT |
837 mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
838 }
Jani Nikula4e646492013-08-27 15:12:20 +0300839
840 set_dsi_timings(encoder, adjusted_mode);
841
842 val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
843 if (is_cmd_mode(intel_dsi)) {
844 val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
845 val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
846 } else {
847 val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
848
849 /* XXX: cross-check bpp vs. pixel format? */
850 val |= intel_dsi->pixel_format;
851 }
Jani Nikula4e646492013-08-27 15:12:20 +0300852
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530853 tmp = 0;
Shobhit Kumarf1c79f12014-04-09 13:59:33 +0530854 if (intel_dsi->eotp_pkt == 0)
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530855 tmp |= EOT_DISABLE;
Shobhit Kumarf1c79f12014-04-09 13:59:33 +0530856 if (intel_dsi->clock_stop)
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530857 tmp |= CLOCKSTOP;
Jani Nikula4e646492013-08-27 15:12:20 +0300858
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530859 for_each_dsi_port(port, intel_dsi->ports) {
860 I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
Jani Nikula4e646492013-08-27 15:12:20 +0300861
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530862 /* timeouts for recovery. one frame IIUC. if counter expires,
863 * EOT and stop state. */
Shobhit Kumarcf4dbd22014-04-14 11:18:25 +0530864
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530865 /*
866 * In burst mode, value greater than one DPI line Time in byte
867 * clock (txbyteclkhs) To timeout this timer 1+ of the above
868 * said value is recommended.
869 *
870 * In non-burst mode, Value greater than one DPI frame time in
871 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
872 * said value is recommended.
873 *
874 * In DBI only mode, value greater than one DBI frame time in
875 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
876 * said value is recommended.
877 */
Jani Nikula4e646492013-08-27 15:12:20 +0300878
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530879 if (is_vid_mode(intel_dsi) &&
880 intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
881 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
882 txbyteclkhs(adjusted_mode->htotal, bpp,
883 intel_dsi->lane_count,
884 intel_dsi->burst_mode_ratio) + 1);
885 } else {
886 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
887 txbyteclkhs(adjusted_mode->vtotal *
888 adjusted_mode->htotal,
889 bpp, intel_dsi->lane_count,
890 intel_dsi->burst_mode_ratio) + 1);
891 }
892 I915_WRITE(MIPI_LP_RX_TIMEOUT(port), intel_dsi->lp_rx_timeout);
893 I915_WRITE(MIPI_TURN_AROUND_TIMEOUT(port),
894 intel_dsi->turn_arnd_val);
895 I915_WRITE(MIPI_DEVICE_RESET_TIMER(port),
896 intel_dsi->rst_timer_val);
Jani Nikula4e646492013-08-27 15:12:20 +0300897
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530898 /* dphy stuff */
Jani Nikula4e646492013-08-27 15:12:20 +0300899
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530900 /* in terms of low power clock */
901 I915_WRITE(MIPI_INIT_COUNT(port),
902 txclkesc(intel_dsi->escape_clk_div, 100));
Jani Nikula4e646492013-08-27 15:12:20 +0300903
Shashank Sharmad2e08c02015-09-01 19:41:40 +0530904 if (IS_BROXTON(dev) && (!intel_dsi->dual_link)) {
905 /*
906 * BXT spec says write MIPI_INIT_COUNT for
907 * both the ports, even if only one is
908 * getting used. So write the other port
909 * if not in dual link mode.
910 */
911 I915_WRITE(MIPI_INIT_COUNT(port ==
912 PORT_A ? PORT_C : PORT_A),
913 intel_dsi->init_count);
914 }
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530915
916 /* recovery disables */
Shobhit Kumar87c54d02015-02-03 12:17:35 +0530917 I915_WRITE(MIPI_EOT_DISABLE(port), tmp);
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530918
919 /* in terms of low power clock */
920 I915_WRITE(MIPI_INIT_COUNT(port), intel_dsi->init_count);
921
922 /* in terms of txbyteclkhs. actual high to low switch +
923 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
924 *
925 * XXX: write MIPI_STOP_STATE_STALL?
926 */
927 I915_WRITE(MIPI_HIGH_LOW_SWITCH_COUNT(port),
928 intel_dsi->hs_to_lp_count);
929
930 /* XXX: low power clock equivalence in terms of byte clock.
931 * the number of byte clocks occupied in one low power clock.
932 * based on txbyteclkhs and txclkesc.
933 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
934 * ) / 105.???
935 */
936 I915_WRITE(MIPI_LP_BYTECLK(port), intel_dsi->lp_byte_clk);
937
938 /* the bw essential for transmitting 16 long packets containing
939 * 252 bytes meant for dcs write memory command is programmed in
940 * this register in terms of byte clocks. based on dsi transfer
941 * rate and the number of lanes configured the time taken to
942 * transmit 16 long packets in a dsi stream varies. */
943 I915_WRITE(MIPI_DBI_BW_CTRL(port), intel_dsi->bw_timer);
944
945 I915_WRITE(MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
946 intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT |
947 intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
948
949 if (is_vid_mode(intel_dsi))
950 /* Some panels might have resolution which is not a
951 * multiple of 64 like 1366 x 768. Enable RANDOM
952 * resolution support for such panels by default */
953 I915_WRITE(MIPI_VIDEO_MODE_FORMAT(port),
954 intel_dsi->video_frmt_cfg_bits |
955 intel_dsi->video_mode_format |
956 IP_TG_CONFIG |
957 RANDOM_DPI_DISPLAY_RESOLUTION);
958 }
Jani Nikula4e646492013-08-27 15:12:20 +0300959}
960
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200961static void intel_dsi_pre_pll_enable(struct intel_encoder *encoder)
962{
963 DRM_DEBUG_KMS("\n");
964
965 intel_dsi_prepare(encoder);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530966 intel_enable_dsi_pll(encoder);
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200967
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200968}
969
Jani Nikula4e646492013-08-27 15:12:20 +0300970static enum drm_connector_status
971intel_dsi_detect(struct drm_connector *connector, bool force)
972{
Jani Nikula36d21f42015-01-16 14:27:20 +0200973 return connector_status_connected;
Jani Nikula4e646492013-08-27 15:12:20 +0300974}
975
976static int intel_dsi_get_modes(struct drm_connector *connector)
977{
978 struct intel_connector *intel_connector = to_intel_connector(connector);
979 struct drm_display_mode *mode;
980
981 DRM_DEBUG_KMS("\n");
982
983 if (!intel_connector->panel.fixed_mode) {
984 DRM_DEBUG_KMS("no fixed mode\n");
985 return 0;
986 }
987
988 mode = drm_mode_duplicate(connector->dev,
989 intel_connector->panel.fixed_mode);
990 if (!mode) {
991 DRM_DEBUG_KMS("drm_mode_duplicate failed\n");
992 return 0;
993 }
994
995 drm_mode_probed_add(connector, mode);
996 return 1;
997}
998
Jani Nikula593e0622015-01-23 15:30:56 +0200999static void intel_dsi_connector_destroy(struct drm_connector *connector)
Jani Nikula4e646492013-08-27 15:12:20 +03001000{
1001 struct intel_connector *intel_connector = to_intel_connector(connector);
1002
1003 DRM_DEBUG_KMS("\n");
1004 intel_panel_fini(&intel_connector->panel);
Jani Nikula4e646492013-08-27 15:12:20 +03001005 drm_connector_cleanup(connector);
1006 kfree(connector);
1007}
1008
Jani Nikula593e0622015-01-23 15:30:56 +02001009static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
1010{
1011 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1012
1013 if (intel_dsi->panel) {
1014 drm_panel_detach(intel_dsi->panel);
1015 /* XXX: Logically this call belongs in the panel driver. */
1016 drm_panel_remove(intel_dsi->panel);
1017 }
Shobhit Kumarfc45e822015-06-26 14:32:09 +05301018
1019 /* dispose of the gpios */
1020 if (intel_dsi->gpio_panel)
1021 gpiod_put(intel_dsi->gpio_panel);
1022
Jani Nikula593e0622015-01-23 15:30:56 +02001023 intel_encoder_destroy(encoder);
1024}
1025
Jani Nikula4e646492013-08-27 15:12:20 +03001026static const struct drm_encoder_funcs intel_dsi_funcs = {
Jani Nikula593e0622015-01-23 15:30:56 +02001027 .destroy = intel_dsi_encoder_destroy,
Jani Nikula4e646492013-08-27 15:12:20 +03001028};
1029
1030static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1031 .get_modes = intel_dsi_get_modes,
1032 .mode_valid = intel_dsi_mode_valid,
1033 .best_encoder = intel_best_encoder,
1034};
1035
1036static const struct drm_connector_funcs intel_dsi_connector_funcs = {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02001037 .dpms = drm_atomic_helper_connector_dpms,
Jani Nikula4e646492013-08-27 15:12:20 +03001038 .detect = intel_dsi_detect,
Jani Nikula593e0622015-01-23 15:30:56 +02001039 .destroy = intel_dsi_connector_destroy,
Jani Nikula4e646492013-08-27 15:12:20 +03001040 .fill_modes = drm_helper_probe_single_connector_modes,
Matt Roper2545e4a2015-01-22 16:51:27 -08001041 .atomic_get_property = intel_connector_atomic_get_property,
Matt Roperc6f95f22015-01-22 16:50:32 -08001042 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Ander Conselvan de Oliveira98969722015-03-20 16:18:06 +02001043 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Jani Nikula4e646492013-08-27 15:12:20 +03001044};
1045
Damien Lespiau4328633d2014-05-28 12:30:56 +01001046void intel_dsi_init(struct drm_device *dev)
Jani Nikula4e646492013-08-27 15:12:20 +03001047{
1048 struct intel_dsi *intel_dsi;
1049 struct intel_encoder *intel_encoder;
1050 struct drm_encoder *encoder;
1051 struct intel_connector *intel_connector;
1052 struct drm_connector *connector;
Jani Nikula593e0622015-01-23 15:30:56 +02001053 struct drm_display_mode *scan, *fixed_mode = NULL;
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +05301054 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7e9804f2015-01-16 14:27:23 +02001055 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +03001056 unsigned int i;
1057
1058 DRM_DEBUG_KMS("\n");
1059
Shobhit Kumar3e6bd012014-05-27 19:33:59 +05301060 /* There is no detection method for MIPI so rely on VBT */
1061 if (!dev_priv->vbt.has_mipi)
Damien Lespiau4328633d2014-05-28 12:30:56 +01001062 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001063
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +05301064 if (IS_VALLEYVIEW(dev)) {
1065 dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
1066 } else {
1067 DRM_ERROR("Unsupported Mipi device to reg base");
Christoph Jaeger868d6652014-06-13 21:51:22 +02001068 return;
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +05301069 }
1070
Jani Nikula4e646492013-08-27 15:12:20 +03001071 intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1072 if (!intel_dsi)
Damien Lespiau4328633d2014-05-28 12:30:56 +01001073 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001074
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03001075 intel_connector = intel_connector_alloc();
Jani Nikula4e646492013-08-27 15:12:20 +03001076 if (!intel_connector) {
1077 kfree(intel_dsi);
Damien Lespiau4328633d2014-05-28 12:30:56 +01001078 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001079 }
1080
1081 intel_encoder = &intel_dsi->base;
1082 encoder = &intel_encoder->base;
1083 intel_dsi->attached_connector = intel_connector;
1084
Jani Nikula4e646492013-08-27 15:12:20 +03001085 connector = &intel_connector->base;
1086
1087 drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI);
1088
1089 /* XXX: very likely not all of these are needed */
Jani Nikula4e646492013-08-27 15:12:20 +03001090 intel_encoder->compute_config = intel_dsi_compute_config;
1091 intel_encoder->pre_pll_enable = intel_dsi_pre_pll_enable;
1092 intel_encoder->pre_enable = intel_dsi_pre_enable;
Shobhit Kumar2634fd72014-04-09 13:59:31 +05301093 intel_encoder->enable = intel_dsi_enable_nop;
Imre Deakc315faf2014-05-27 19:00:09 +03001094 intel_encoder->disable = intel_dsi_pre_disable;
Jani Nikula4e646492013-08-27 15:12:20 +03001095 intel_encoder->post_disable = intel_dsi_post_disable;
1096 intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1097 intel_encoder->get_config = intel_dsi_get_config;
1098
1099 intel_connector->get_hw_state = intel_connector_get_hw_state;
Imre Deak4932e2c2014-02-11 17:12:48 +02001100 intel_connector->unregister = intel_connector_unregister;
Jani Nikula4e646492013-08-27 15:12:20 +03001101
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001102 /* Pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI port C */
Gaurav K Singh82425782015-08-03 15:45:32 +05301103 if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIA) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001104 intel_encoder->crtc_mask = (1 << PIPE_A);
Jani Nikula17af40a2014-11-14 16:54:22 +02001105 intel_dsi->ports = (1 << PORT_A);
1106 } else if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIC) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001107 intel_encoder->crtc_mask = (1 << PIPE_B);
Jani Nikula17af40a2014-11-14 16:54:22 +02001108 intel_dsi->ports = (1 << PORT_C);
1109 }
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001110
Gaurav K Singh82425782015-08-03 15:45:32 +05301111 if (dev_priv->vbt.dsi.config->dual_link)
1112 intel_dsi->ports = ((1 << PORT_A) | (1 << PORT_C));
1113
Jani Nikula7e9804f2015-01-16 14:27:23 +02001114 /* Create a DSI host (and a device) for each port. */
1115 for_each_dsi_port(port, intel_dsi->ports) {
1116 struct intel_dsi_host *host;
1117
1118 host = intel_dsi_host_init(intel_dsi, port);
1119 if (!host)
1120 goto err;
1121
1122 intel_dsi->dsi_hosts[port] = host;
1123 }
1124
Jani Nikula593e0622015-01-23 15:30:56 +02001125 for (i = 0; i < ARRAY_SIZE(intel_dsi_drivers); i++) {
1126 intel_dsi->panel = intel_dsi_drivers[i].init(intel_dsi,
1127 intel_dsi_drivers[i].panel_id);
1128 if (intel_dsi->panel)
Jani Nikula4e646492013-08-27 15:12:20 +03001129 break;
1130 }
1131
Jani Nikula593e0622015-01-23 15:30:56 +02001132 if (!intel_dsi->panel) {
Jani Nikula4e646492013-08-27 15:12:20 +03001133 DRM_DEBUG_KMS("no device found\n");
1134 goto err;
1135 }
1136
Shobhit Kumarfc45e822015-06-26 14:32:09 +05301137 /*
1138 * In case of BYT with CRC PMIC, we need to use GPIO for
1139 * Panel control.
1140 */
1141 if (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC) {
1142 intel_dsi->gpio_panel =
1143 gpiod_get(dev->dev, "panel", GPIOD_OUT_HIGH);
1144
1145 if (IS_ERR(intel_dsi->gpio_panel)) {
1146 DRM_ERROR("Failed to own gpio for panel control\n");
1147 intel_dsi->gpio_panel = NULL;
1148 }
1149 }
1150
Jani Nikula4e646492013-08-27 15:12:20 +03001151 intel_encoder->type = INTEL_OUTPUT_DSI;
Ville Syrjäläbc079e82014-03-03 16:15:28 +02001152 intel_encoder->cloneable = 0;
Jani Nikula4e646492013-08-27 15:12:20 +03001153 drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
1154 DRM_MODE_CONNECTOR_DSI);
1155
1156 drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1157
1158 connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1159 connector->interlace_allowed = false;
1160 connector->doublescan_allowed = false;
1161
1162 intel_connector_attach_encoder(intel_connector, intel_encoder);
1163
Thomas Wood34ea3d32014-05-29 16:57:41 +01001164 drm_connector_register(connector);
Jani Nikula4e646492013-08-27 15:12:20 +03001165
Jani Nikula593e0622015-01-23 15:30:56 +02001166 drm_panel_attach(intel_dsi->panel, connector);
1167
1168 mutex_lock(&dev->mode_config.mutex);
1169 drm_panel_get_modes(intel_dsi->panel);
1170 list_for_each_entry(scan, &connector->probed_modes, head) {
1171 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
1172 fixed_mode = drm_mode_duplicate(dev, scan);
1173 break;
1174 }
1175 }
1176 mutex_unlock(&dev->mode_config.mutex);
1177
Jani Nikula4e646492013-08-27 15:12:20 +03001178 if (!fixed_mode) {
1179 DRM_DEBUG_KMS("no fixed mode\n");
1180 goto err;
1181 }
1182
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301183 intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
Shobhit Kumarb029e662015-06-26 14:32:10 +05301184 intel_panel_setup_backlight(connector, INVALID_PIPE);
Jani Nikula4e646492013-08-27 15:12:20 +03001185
Damien Lespiau4328633d2014-05-28 12:30:56 +01001186 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001187
1188err:
1189 drm_encoder_cleanup(&intel_encoder->base);
1190 kfree(intel_dsi);
1191 kfree(intel_connector);
Jani Nikula4e646492013-08-27 15:12:20 +03001192}