blob: 7a75aaf200e3a703461d642d4ddc3ee27fcecea6 [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>
34#include "i915_drv.h"
35#include "intel_drv.h"
36#include "intel_dsi.h"
37#include "intel_dsi_cmd.h"
38
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{
243 mutex_lock(&dev_priv->dpio_lock);
244
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
252 mutex_unlock(&dev_priv->dpio_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
265static void intel_dsi_hot_plug(struct intel_encoder *encoder)
266{
267 DRM_DEBUG_KMS("\n");
268}
269
270static bool intel_dsi_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200271 struct intel_crtc_state *config)
Jani Nikula4e646492013-08-27 15:12:20 +0300272{
273 struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
274 base);
275 struct intel_connector *intel_connector = intel_dsi->attached_connector;
276 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200277 struct drm_display_mode *adjusted_mode = &config->base.adjusted_mode;
Jani Nikula4e646492013-08-27 15:12:20 +0300278
279 DRM_DEBUG_KMS("\n");
280
281 if (fixed_mode)
282 intel_fixed_panel_mode(fixed_mode, adjusted_mode);
283
Shobhit Kumarf573de52014-07-30 20:32:37 +0530284 /* DSI uses short packets for sync events, so clear mode flags for DSI */
285 adjusted_mode->flags = 0;
286
Jani Nikula4e646492013-08-27 15:12:20 +0300287 return true;
288}
289
Gaurav K Singh5505a242014-12-04 10:58:47 +0530290static void intel_dsi_port_enable(struct intel_encoder *encoder)
291{
292 struct drm_device *dev = encoder->base.dev;
293 struct drm_i915_private *dev_priv = dev->dev_private;
294 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
295 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Gaurav K Singh369602d2014-12-05 14:09:28 +0530296 enum port port;
Gaurav K Singh5505a242014-12-04 10:58:47 +0530297 u32 temp;
298
Gaurav K Singha9da9bc2014-12-05 14:13:41 +0530299 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
300 temp = I915_READ(VLV_CHICKEN_3);
301 temp &= ~PIXEL_OVERLAP_CNT_MASK |
302 intel_dsi->pixel_overlap <<
303 PIXEL_OVERLAP_CNT_SHIFT;
304 I915_WRITE(VLV_CHICKEN_3, temp);
305 }
306
Gaurav K Singh369602d2014-12-05 14:09:28 +0530307 for_each_dsi_port(port, intel_dsi->ports) {
308 temp = I915_READ(MIPI_PORT_CTRL(port));
309 temp &= ~LANE_CONFIGURATION_MASK;
310 temp &= ~DUAL_LINK_MODE_MASK;
311
312 if (intel_dsi->ports == ((1 << PORT_A) | (1 << PORT_C))) {
313 temp |= (intel_dsi->dual_link - 1)
314 << DUAL_LINK_MODE_SHIFT;
315 temp |= intel_crtc->pipe ?
316 LANE_CONFIGURATION_DUAL_LINK_B :
317 LANE_CONFIGURATION_DUAL_LINK_A;
318 }
319 /* assert ip_tg_enable signal */
320 I915_WRITE(MIPI_PORT_CTRL(port), temp | DPI_ENABLE);
321 POSTING_READ(MIPI_PORT_CTRL(port));
322 }
Gaurav K Singh5505a242014-12-04 10:58:47 +0530323}
324
325static void intel_dsi_port_disable(struct intel_encoder *encoder)
326{
327 struct drm_device *dev = encoder->base.dev;
328 struct drm_i915_private *dev_priv = dev->dev_private;
Gaurav K Singh369602d2014-12-05 14:09:28 +0530329 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
330 enum port port;
Gaurav K Singh5505a242014-12-04 10:58:47 +0530331 u32 temp;
332
Gaurav K Singh369602d2014-12-05 14:09:28 +0530333 for_each_dsi_port(port, intel_dsi->ports) {
334 /* de-assert ip_tg_enable signal */
335 temp = I915_READ(MIPI_PORT_CTRL(port));
336 I915_WRITE(MIPI_PORT_CTRL(port), temp & ~DPI_ENABLE);
337 POSTING_READ(MIPI_PORT_CTRL(port));
338 }
Gaurav K Singh5505a242014-12-04 10:58:47 +0530339}
340
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530341static void intel_dsi_device_ready(struct intel_encoder *encoder)
342{
343 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530344 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
345 enum port port;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530346 u32 val;
347
348 DRM_DEBUG_KMS("\n");
349
Shobhit Kumar2095f9f2014-04-09 13:59:30 +0530350 mutex_lock(&dev_priv->dpio_lock);
351 /* program rcomp for compliance, reduce from 50 ohms to 45 ohms
352 * needed everytime after power gate */
353 vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
354 mutex_unlock(&dev_priv->dpio_lock);
355
356 /* bandgap reset is needed after everytime we do power gate */
357 band_gap_reset(dev_priv);
358
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530359 for_each_dsi_port(port, intel_dsi->ports) {
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530360
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530361 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_ENTER);
362 usleep_range(2500, 3000);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530363
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530364 val = I915_READ(MIPI_PORT_CTRL(port));
Gaurav K Singhbf344e82014-12-07 16:13:54 +0530365
366 /* Enable MIPI PHY transparent latch
367 * Common bit for both MIPI Port A & MIPI Port C
368 * No similar bit in MIPI Port C reg
369 */
370 I915_WRITE(MIPI_PORT_CTRL(PORT_A), val | LP_OUTPUT_HOLD);
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530371 usleep_range(1000, 1500);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530372
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530373 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_EXIT);
374 usleep_range(2500, 3000);
375
376 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY);
377 usleep_range(2500, 3000);
378 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530379}
Jani Nikula4e646492013-08-27 15:12:20 +0300380
381static void intel_dsi_enable(struct intel_encoder *encoder)
382{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530383 struct drm_device *dev = encoder->base.dev;
384 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4e646492013-08-27 15:12:20 +0300385 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Jani Nikula4934b652015-01-22 15:01:35 +0200386 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300387
388 DRM_DEBUG_KMS("\n");
389
Jani Nikula4934b652015-01-22 15:01:35 +0200390 if (is_cmd_mode(intel_dsi)) {
391 for_each_dsi_port(port, intel_dsi->ports)
392 I915_WRITE(MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
393 } else {
Jani Nikula4e646492013-08-27 15:12:20 +0300394 msleep(20); /* XXX */
Jani Nikulaf03e4172015-01-16 14:27:16 +0200395 for_each_dsi_port(port, intel_dsi->ports)
Jani Nikulaa2581a92015-01-16 14:27:26 +0200396 dpi_send_cmd(intel_dsi, TURN_ON, false, port);
Jani Nikula4e646492013-08-27 15:12:20 +0300397 msleep(100);
398
Jani Nikula593e0622015-01-23 15:30:56 +0200399 drm_panel_enable(intel_dsi->panel);
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530400
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200401 for_each_dsi_port(port, intel_dsi->ports)
402 wait_for_dsi_fifo_empty(intel_dsi, port);
Shobhit Kumar13813082014-07-12 17:17:22 +0530403
Gaurav K Singh5505a242014-12-04 10:58:47 +0530404 intel_dsi_port_enable(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300405 }
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530406}
Jani Nikula4e646492013-08-27 15:12:20 +0300407
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530408static void intel_dsi_pre_enable(struct intel_encoder *encoder)
409{
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530410 struct drm_device *dev = encoder->base.dev;
411 struct drm_i915_private *dev_priv = dev->dev_private;
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530412 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530413 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
414 enum pipe pipe = intel_crtc->pipe;
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200415 enum port port;
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530416 u32 tmp;
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530417
418 DRM_DEBUG_KMS("\n");
419
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530420 /* Disable DPOunit clock gating, can stall pipe
421 * and we need DPLL REFA always enabled */
422 tmp = I915_READ(DPLL(pipe));
423 tmp |= DPLL_REFA_CLK_ENABLE_VLV;
424 I915_WRITE(DPLL(pipe), tmp);
425
Shobhit Kumarf573de52014-07-30 20:32:37 +0530426 /* update the hw state for DPLL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200427 intel_crtc->config->dpll_hw_state.dpll = DPLL_INTEGRATED_CLOCK_VLV |
Daniel Vetter7f3de832014-07-30 22:34:27 +0200428 DPLL_REFA_CLK_ENABLE_VLV;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530429
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530430 tmp = I915_READ(DSPCLK_GATE_D);
431 tmp |= DPOUNIT_CLOCK_GATE_DISABLE;
432 I915_WRITE(DSPCLK_GATE_D, tmp);
Shobhit Kumar2634fd72014-04-09 13:59:31 +0530433
434 /* put device in ready state */
435 intel_dsi_device_ready(encoder);
436
Shobhit Kumardf38e652014-04-14 11:18:26 +0530437 msleep(intel_dsi->panel_on_delay);
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
466 if (is_vid_mode(intel_dsi)) {
467 /* Send Shutdown command to the panel in LP mode */
Jani Nikulaf03e4172015-01-16 14:27:16 +0200468 for_each_dsi_port(port, intel_dsi->ports)
Jani Nikulaa2581a92015-01-16 14:27:26 +0200469 dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
Imre Deakc315faf2014-05-27 19:00:09 +0300470 msleep(10);
471 }
472}
473
Jani Nikula4e646492013-08-27 15:12:20 +0300474static void intel_dsi_disable(struct intel_encoder *encoder)
475{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530476 struct drm_device *dev = encoder->base.dev;
477 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4e646492013-08-27 15:12:20 +0300478 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530479 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300480 u32 temp;
481
482 DRM_DEBUG_KMS("\n");
483
Jani Nikula4e646492013-08-27 15:12:20 +0300484 if (is_vid_mode(intel_dsi)) {
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200485 for_each_dsi_port(port, intel_dsi->ports)
486 wait_for_dsi_fifo_empty(intel_dsi, port);
Shobhit Kumar13813082014-07-12 17:17:22 +0530487
Gaurav K Singh5505a242014-12-04 10:58:47 +0530488 intel_dsi_port_disable(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300489 msleep(2);
490 }
491
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530492 for_each_dsi_port(port, intel_dsi->ports) {
493 /* Panel commands can be sent when clock is in LP11 */
494 I915_WRITE(MIPI_DEVICE_READY(port), 0x0);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530495
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530496 temp = I915_READ(MIPI_CTRL(port));
497 temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
498 I915_WRITE(MIPI_CTRL(port), temp |
499 intel_dsi->escape_clk_div <<
500 ESCAPE_CLOCK_DIVIDER_SHIFT);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530501
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530502 I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530503
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530504 temp = I915_READ(MIPI_DSI_FUNC_PRG(port));
505 temp &= ~VID_MODE_FORMAT_MASK;
506 I915_WRITE(MIPI_DSI_FUNC_PRG(port), temp);
Shobhit Kumar339023e2014-04-09 13:59:34 +0530507
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530508 I915_WRITE(MIPI_DEVICE_READY(port), 0x1);
509 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530510 /* if disable packets are sent before sending shutdown packet then in
511 * some next enable sequence send turn on packet error is observed */
Jani Nikula593e0622015-01-23 15:30:56 +0200512 drm_panel_disable(intel_dsi->panel);
Shobhit Kumar13813082014-07-12 17:17:22 +0530513
Jani Nikula7f6a6a42015-01-16 14:27:19 +0200514 for_each_dsi_port(port, intel_dsi->ports)
515 wait_for_dsi_fifo_empty(intel_dsi, port);
Jani Nikula4e646492013-08-27 15:12:20 +0300516}
517
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530518static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
Jani Nikula4e646492013-08-27 15:12:20 +0300519{
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530520 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530521 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
522 enum port port;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530523 u32 val;
524
Jani Nikula4e646492013-08-27 15:12:20 +0300525 DRM_DEBUG_KMS("\n");
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530526 for_each_dsi_port(port, intel_dsi->ports) {
ymohanmabe4fc042013-08-27 23:40:56 +0300527
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530528 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
529 ULPS_STATE_ENTER);
530 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530531
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530532 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
533 ULPS_STATE_EXIT);
534 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530535
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530536 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
537 ULPS_STATE_ENTER);
538 usleep_range(2000, 2500);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530539
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530540 /* Wait till Clock lanes are in LP-00 state for MIPI Port A
541 * only. MIPI Port C has no similar bit for checking
542 */
543 if (wait_for(((I915_READ(MIPI_PORT_CTRL(PORT_A)) & AFE_LATCHOUT)
544 == 0x00000), 30))
545 DRM_ERROR("DSI LP not going Low\n");
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530546
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530547 val = I915_READ(MIPI_PORT_CTRL(port));
548 /* Disable MIPI PHY transparent latch
549 * Common bit for both MIPI Port A & MIPI Port C
550 */
551 I915_WRITE(MIPI_PORT_CTRL(PORT_A), val & ~LP_OUTPUT_HOLD);
552 usleep_range(1000, 1500);
Shobhit Kumaraceb3652014-07-03 16:35:41 +0530553
Gaurav K Singh384f02a2014-12-05 14:22:44 +0530554 I915_WRITE(MIPI_DEVICE_READY(port), 0x00);
555 usleep_range(2000, 2500);
556 }
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530557
ymohanmabe4fc042013-08-27 23:40:56 +0300558 vlv_disable_dsi_pll(encoder);
Jani Nikula4e646492013-08-27 15:12:20 +0300559}
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530560
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530561static void intel_dsi_post_disable(struct intel_encoder *encoder)
562{
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530563 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530564 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530565 u32 val;
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530566
567 DRM_DEBUG_KMS("\n");
568
Imre Deakc315faf2014-05-27 19:00:09 +0300569 intel_dsi_disable(encoder);
570
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530571 intel_dsi_clear_device_ready(encoder);
572
Shobhit Kumar20e5bf62014-04-09 13:59:32 +0530573 val = I915_READ(DSPCLK_GATE_D);
574 val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
575 I915_WRITE(DSPCLK_GATE_D, val);
576
Jani Nikula593e0622015-01-23 15:30:56 +0200577 drm_panel_unprepare(intel_dsi->panel);
Shobhit Kumardf38e652014-04-14 11:18:26 +0530578
579 msleep(intel_dsi->panel_off_delay);
580 msleep(intel_dsi->panel_pwr_cycle_delay);
Shobhit Kumar1dbd7cb2013-12-11 17:52:05 +0530581}
Jani Nikula4e646492013-08-27 15:12:20 +0300582
583static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
584 enum pipe *pipe)
585{
586 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530587 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
588 struct drm_device *dev = encoder->base.dev;
Imre Deak6d129be2014-03-05 16:20:54 +0200589 enum intel_display_power_domain power_domain;
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530590 u32 dpi_enabled, func;
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200591 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300592
593 DRM_DEBUG_KMS("\n");
594
Imre Deak6d129be2014-03-05 16:20:54 +0200595 power_domain = intel_display_port_power_domain(encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200596 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +0200597 return false;
598
Jani Nikula4e646492013-08-27 15:12:20 +0300599 /* XXX: this only works for one DSI output */
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530600 for_each_dsi_port(port, intel_dsi->ports) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200601 func = I915_READ(MIPI_DSI_FUNC_PRG(port));
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530602 dpi_enabled = I915_READ(MIPI_PORT_CTRL(port)) &
603 DPI_ENABLE;
Jani Nikula4e646492013-08-27 15:12:20 +0300604
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530605 /* Due to some hardware limitations on BYT, MIPI Port C DPI
606 * Enable bit does not get set. To check whether DSI Port C
607 * was enabled in BIOS, check the Pipe B enable bit
608 */
609 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
610 (port == PORT_C))
611 dpi_enabled = I915_READ(PIPECONF(PIPE_B)) &
612 PIPECONF_ENABLE;
613
614 if (dpi_enabled || (func & CMD_MODE_DATA_WIDTH_MASK)) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200615 if (I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY) {
Gaurav K Singhc0beefd2014-12-09 10:59:20 +0530616 *pipe = port == PORT_A ? PIPE_A : PIPE_B;
Jani Nikula4e646492013-08-27 15:12:20 +0300617 return true;
618 }
619 }
620 }
621
622 return false;
623}
624
625static void intel_dsi_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200626 struct intel_crtc_state *pipe_config)
Jani Nikula4e646492013-08-27 15:12:20 +0300627{
Shobhit Kumarf573de52014-07-30 20:32:37 +0530628 u32 pclk;
Jani Nikula4e646492013-08-27 15:12:20 +0300629 DRM_DEBUG_KMS("\n");
630
Shobhit Kumarf573de52014-07-30 20:32:37 +0530631 /*
632 * DPLL_MD is not used in case of DSI, reading will get some default value
633 * set dpll_md = 0
634 */
635 pipe_config->dpll_hw_state.dpll_md = 0;
636
637 pclk = vlv_get_dsi_pclk(encoder, pipe_config->pipe_bpp);
638 if (!pclk)
639 return;
640
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200641 pipe_config->base.adjusted_mode.crtc_clock = pclk;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530642 pipe_config->port_clock = pclk;
Jani Nikula4e646492013-08-27 15:12:20 +0300643}
644
Damien Lespiauc19de8e2013-11-28 15:29:18 +0000645static enum drm_mode_status
646intel_dsi_mode_valid(struct drm_connector *connector,
647 struct drm_display_mode *mode)
Jani Nikula4e646492013-08-27 15:12:20 +0300648{
649 struct intel_connector *intel_connector = to_intel_connector(connector);
650 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Jani Nikula4e646492013-08-27 15:12:20 +0300651
652 DRM_DEBUG_KMS("\n");
653
654 if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
655 DRM_DEBUG_KMS("MODE_NO_DBLESCAN\n");
656 return MODE_NO_DBLESCAN;
657 }
658
659 if (fixed_mode) {
660 if (mode->hdisplay > fixed_mode->hdisplay)
661 return MODE_PANEL;
662 if (mode->vdisplay > fixed_mode->vdisplay)
663 return MODE_PANEL;
664 }
665
Jani Nikula36d21f42015-01-16 14:27:20 +0200666 return MODE_OK;
Jani Nikula4e646492013-08-27 15:12:20 +0300667}
668
669/* return txclkesc cycles in terms of divider and duration in us */
670static u16 txclkesc(u32 divider, unsigned int us)
671{
672 switch (divider) {
673 case ESCAPE_CLOCK_DIVIDER_1:
674 default:
675 return 20 * us;
676 case ESCAPE_CLOCK_DIVIDER_2:
677 return 10 * us;
678 case ESCAPE_CLOCK_DIVIDER_4:
679 return 5 * us;
680 }
681}
682
683/* return pixels in terms of txbyteclkhs */
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530684static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
685 u16 burst_mode_ratio)
Jani Nikula4e646492013-08-27 15:12:20 +0300686{
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530687 return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200688 8 * 100), lane_count);
Jani Nikula4e646492013-08-27 15:12:20 +0300689}
690
691static void set_dsi_timings(struct drm_encoder *encoder,
692 const struct drm_display_mode *mode)
693{
694 struct drm_device *dev = encoder->dev;
695 struct drm_i915_private *dev_priv = dev->dev_private;
696 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
697 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530698 enum port port;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200699 unsigned int bpp = intel_crtc->config->pipe_bpp;
Jani Nikula4e646492013-08-27 15:12:20 +0300700 unsigned int lane_count = intel_dsi->lane_count;
701
702 u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
703
704 hactive = mode->hdisplay;
705 hfp = mode->hsync_start - mode->hdisplay;
706 hsync = mode->hsync_end - mode->hsync_start;
707 hbp = mode->htotal - mode->hsync_end;
708
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530709 if (intel_dsi->dual_link) {
710 hactive /= 2;
711 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
712 hactive += intel_dsi->pixel_overlap;
713 hfp /= 2;
714 hsync /= 2;
715 hbp /= 2;
716 }
717
Jani Nikula4e646492013-08-27 15:12:20 +0300718 vfp = mode->vsync_start - mode->vdisplay;
719 vsync = mode->vsync_end - mode->vsync_start;
720 vbp = mode->vtotal - mode->vsync_end;
721
722 /* horizontal values are in terms of high speed byte clock */
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530723 hactive = txbyteclkhs(hactive, bpp, lane_count,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200724 intel_dsi->burst_mode_ratio);
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530725 hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
726 hsync = txbyteclkhs(hsync, bpp, lane_count,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200727 intel_dsi->burst_mode_ratio);
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530728 hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
Jani Nikula4e646492013-08-27 15:12:20 +0300729
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530730 for_each_dsi_port(port, intel_dsi->ports) {
731 I915_WRITE(MIPI_HACTIVE_AREA_COUNT(port), hactive);
732 I915_WRITE(MIPI_HFP_COUNT(port), hfp);
Jani Nikula4e646492013-08-27 15:12:20 +0300733
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530734 /* meaningful for video mode non-burst sync pulse mode only,
735 * can be zero for non-burst sync events and burst modes */
736 I915_WRITE(MIPI_HSYNC_PADDING_COUNT(port), hsync);
737 I915_WRITE(MIPI_HBP_COUNT(port), hbp);
Jani Nikula4e646492013-08-27 15:12:20 +0300738
Gaurav K Singhaa102d22014-12-04 10:58:54 +0530739 /* vertical values are in terms of lines */
740 I915_WRITE(MIPI_VFP_COUNT(port), vfp);
741 I915_WRITE(MIPI_VSYNC_PADDING_COUNT(port), vsync);
742 I915_WRITE(MIPI_VBP_COUNT(port), vbp);
743 }
Jani Nikula4e646492013-08-27 15:12:20 +0300744}
745
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200746static void intel_dsi_prepare(struct intel_encoder *intel_encoder)
Jani Nikula4e646492013-08-27 15:12:20 +0300747{
748 struct drm_encoder *encoder = &intel_encoder->base;
749 struct drm_device *dev = encoder->dev;
750 struct drm_i915_private *dev_priv = dev->dev_private;
751 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
752 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
753 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200754 &intel_crtc->config->base.adjusted_mode;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530755 enum port port;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200756 unsigned int bpp = intel_crtc->config->pipe_bpp;
Jani Nikula4e646492013-08-27 15:12:20 +0300757 u32 val, tmp;
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530758 u16 mode_hdisplay;
Jani Nikula4e646492013-08-27 15:12:20 +0300759
Jani Nikulae7d7cad2014-11-14 16:54:21 +0200760 DRM_DEBUG_KMS("pipe %c\n", pipe_name(intel_crtc->pipe));
Jani Nikula4e646492013-08-27 15:12:20 +0300761
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530762 mode_hdisplay = adjusted_mode->hdisplay;
Jani Nikula4e646492013-08-27 15:12:20 +0300763
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530764 if (intel_dsi->dual_link) {
765 mode_hdisplay /= 2;
766 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
767 mode_hdisplay += intel_dsi->pixel_overlap;
768 }
Jani Nikula4e646492013-08-27 15:12:20 +0300769
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530770 for_each_dsi_port(port, intel_dsi->ports) {
771 /* escape clock divider, 20MHz, shared for A and C.
772 * device ready must be off when doing this! txclkesc? */
773 tmp = I915_READ(MIPI_CTRL(PORT_A));
774 tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
775 I915_WRITE(MIPI_CTRL(PORT_A), tmp | ESCAPE_CLOCK_DIVIDER_1);
Jani Nikula4e646492013-08-27 15:12:20 +0300776
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530777 /* read request priority is per pipe */
778 tmp = I915_READ(MIPI_CTRL(port));
779 tmp &= ~READ_REQUEST_PRIORITY_MASK;
780 I915_WRITE(MIPI_CTRL(port), tmp | READ_REQUEST_PRIORITY_HIGH);
Jani Nikula4e646492013-08-27 15:12:20 +0300781
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530782 /* XXX: why here, why like this? handling in irq handler?! */
783 I915_WRITE(MIPI_INTR_STAT(port), 0xffffffff);
784 I915_WRITE(MIPI_INTR_EN(port), 0xffffffff);
785
786 I915_WRITE(MIPI_DPHY_PARAM(port), intel_dsi->dphy_reg);
787
788 I915_WRITE(MIPI_DPI_RESOLUTION(port),
789 adjusted_mode->vdisplay << VERTICAL_ADDRESS_SHIFT |
790 mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
791 }
Jani Nikula4e646492013-08-27 15:12:20 +0300792
793 set_dsi_timings(encoder, adjusted_mode);
794
795 val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
796 if (is_cmd_mode(intel_dsi)) {
797 val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
798 val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
799 } else {
800 val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
801
802 /* XXX: cross-check bpp vs. pixel format? */
803 val |= intel_dsi->pixel_format;
804 }
Jani Nikula4e646492013-08-27 15:12:20 +0300805
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530806 tmp = 0;
Shobhit Kumarf1c79f12014-04-09 13:59:33 +0530807 if (intel_dsi->eotp_pkt == 0)
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530808 tmp |= EOT_DISABLE;
Shobhit Kumarf1c79f12014-04-09 13:59:33 +0530809 if (intel_dsi->clock_stop)
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530810 tmp |= CLOCKSTOP;
Jani Nikula4e646492013-08-27 15:12:20 +0300811
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530812 for_each_dsi_port(port, intel_dsi->ports) {
813 I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
Jani Nikula4e646492013-08-27 15:12:20 +0300814
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530815 /* timeouts for recovery. one frame IIUC. if counter expires,
816 * EOT and stop state. */
Shobhit Kumarcf4dbd22014-04-14 11:18:25 +0530817
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530818 /*
819 * In burst mode, value greater than one DPI line Time in byte
820 * clock (txbyteclkhs) To timeout this timer 1+ of the above
821 * said value is recommended.
822 *
823 * In non-burst mode, Value greater than one DPI frame time in
824 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
825 * said value is recommended.
826 *
827 * In DBI only mode, value greater than one DBI frame time in
828 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
829 * said value is recommended.
830 */
Jani Nikula4e646492013-08-27 15:12:20 +0300831
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530832 if (is_vid_mode(intel_dsi) &&
833 intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
834 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
835 txbyteclkhs(adjusted_mode->htotal, bpp,
836 intel_dsi->lane_count,
837 intel_dsi->burst_mode_ratio) + 1);
838 } else {
839 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
840 txbyteclkhs(adjusted_mode->vtotal *
841 adjusted_mode->htotal,
842 bpp, intel_dsi->lane_count,
843 intel_dsi->burst_mode_ratio) + 1);
844 }
845 I915_WRITE(MIPI_LP_RX_TIMEOUT(port), intel_dsi->lp_rx_timeout);
846 I915_WRITE(MIPI_TURN_AROUND_TIMEOUT(port),
847 intel_dsi->turn_arnd_val);
848 I915_WRITE(MIPI_DEVICE_RESET_TIMER(port),
849 intel_dsi->rst_timer_val);
Jani Nikula4e646492013-08-27 15:12:20 +0300850
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530851 /* dphy stuff */
Jani Nikula4e646492013-08-27 15:12:20 +0300852
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530853 /* in terms of low power clock */
854 I915_WRITE(MIPI_INIT_COUNT(port),
855 txclkesc(intel_dsi->escape_clk_div, 100));
Jani Nikula4e646492013-08-27 15:12:20 +0300856
Gaurav K Singh24ee0e62014-12-05 14:24:21 +0530857
858 /* recovery disables */
859 I915_WRITE(MIPI_EOT_DISABLE(port), val);
860
861 /* in terms of low power clock */
862 I915_WRITE(MIPI_INIT_COUNT(port), intel_dsi->init_count);
863
864 /* in terms of txbyteclkhs. actual high to low switch +
865 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
866 *
867 * XXX: write MIPI_STOP_STATE_STALL?
868 */
869 I915_WRITE(MIPI_HIGH_LOW_SWITCH_COUNT(port),
870 intel_dsi->hs_to_lp_count);
871
872 /* XXX: low power clock equivalence in terms of byte clock.
873 * the number of byte clocks occupied in one low power clock.
874 * based on txbyteclkhs and txclkesc.
875 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
876 * ) / 105.???
877 */
878 I915_WRITE(MIPI_LP_BYTECLK(port), intel_dsi->lp_byte_clk);
879
880 /* the bw essential for transmitting 16 long packets containing
881 * 252 bytes meant for dcs write memory command is programmed in
882 * this register in terms of byte clocks. based on dsi transfer
883 * rate and the number of lanes configured the time taken to
884 * transmit 16 long packets in a dsi stream varies. */
885 I915_WRITE(MIPI_DBI_BW_CTRL(port), intel_dsi->bw_timer);
886
887 I915_WRITE(MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
888 intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT |
889 intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
890
891 if (is_vid_mode(intel_dsi))
892 /* Some panels might have resolution which is not a
893 * multiple of 64 like 1366 x 768. Enable RANDOM
894 * resolution support for such panels by default */
895 I915_WRITE(MIPI_VIDEO_MODE_FORMAT(port),
896 intel_dsi->video_frmt_cfg_bits |
897 intel_dsi->video_mode_format |
898 IP_TG_CONFIG |
899 RANDOM_DPI_DISPLAY_RESOLUTION);
900 }
Jani Nikula4e646492013-08-27 15:12:20 +0300901}
902
Daniel Vetter07e4fb92014-04-24 23:54:59 +0200903static void intel_dsi_pre_pll_enable(struct intel_encoder *encoder)
904{
905 DRM_DEBUG_KMS("\n");
906
907 intel_dsi_prepare(encoder);
908
909 vlv_enable_dsi_pll(encoder);
910}
911
Jani Nikula4e646492013-08-27 15:12:20 +0300912static enum drm_connector_status
913intel_dsi_detect(struct drm_connector *connector, bool force)
914{
Jani Nikula36d21f42015-01-16 14:27:20 +0200915 return connector_status_connected;
Jani Nikula4e646492013-08-27 15:12:20 +0300916}
917
918static int intel_dsi_get_modes(struct drm_connector *connector)
919{
920 struct intel_connector *intel_connector = to_intel_connector(connector);
921 struct drm_display_mode *mode;
922
923 DRM_DEBUG_KMS("\n");
924
925 if (!intel_connector->panel.fixed_mode) {
926 DRM_DEBUG_KMS("no fixed mode\n");
927 return 0;
928 }
929
930 mode = drm_mode_duplicate(connector->dev,
931 intel_connector->panel.fixed_mode);
932 if (!mode) {
933 DRM_DEBUG_KMS("drm_mode_duplicate failed\n");
934 return 0;
935 }
936
937 drm_mode_probed_add(connector, mode);
938 return 1;
939}
940
Jani Nikula593e0622015-01-23 15:30:56 +0200941static void intel_dsi_connector_destroy(struct drm_connector *connector)
Jani Nikula4e646492013-08-27 15:12:20 +0300942{
943 struct intel_connector *intel_connector = to_intel_connector(connector);
944
945 DRM_DEBUG_KMS("\n");
946 intel_panel_fini(&intel_connector->panel);
Jani Nikula4e646492013-08-27 15:12:20 +0300947 drm_connector_cleanup(connector);
948 kfree(connector);
949}
950
Jani Nikula593e0622015-01-23 15:30:56 +0200951static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
952{
953 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
954
955 if (intel_dsi->panel) {
956 drm_panel_detach(intel_dsi->panel);
957 /* XXX: Logically this call belongs in the panel driver. */
958 drm_panel_remove(intel_dsi->panel);
959 }
960 intel_encoder_destroy(encoder);
961}
962
Jani Nikula4e646492013-08-27 15:12:20 +0300963static const struct drm_encoder_funcs intel_dsi_funcs = {
Jani Nikula593e0622015-01-23 15:30:56 +0200964 .destroy = intel_dsi_encoder_destroy,
Jani Nikula4e646492013-08-27 15:12:20 +0300965};
966
967static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
968 .get_modes = intel_dsi_get_modes,
969 .mode_valid = intel_dsi_mode_valid,
970 .best_encoder = intel_best_encoder,
971};
972
973static const struct drm_connector_funcs intel_dsi_connector_funcs = {
974 .dpms = intel_connector_dpms,
975 .detect = intel_dsi_detect,
Jani Nikula593e0622015-01-23 15:30:56 +0200976 .destroy = intel_dsi_connector_destroy,
Jani Nikula4e646492013-08-27 15:12:20 +0300977 .fill_modes = drm_helper_probe_single_connector_modes,
Matt Roper2545e4a2015-01-22 16:51:27 -0800978 .atomic_get_property = intel_connector_atomic_get_property,
Matt Roperc6f95f22015-01-22 16:50:32 -0800979 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Jani Nikula4e646492013-08-27 15:12:20 +0300980};
981
Damien Lespiau4328633d2014-05-28 12:30:56 +0100982void intel_dsi_init(struct drm_device *dev)
Jani Nikula4e646492013-08-27 15:12:20 +0300983{
984 struct intel_dsi *intel_dsi;
985 struct intel_encoder *intel_encoder;
986 struct drm_encoder *encoder;
987 struct intel_connector *intel_connector;
988 struct drm_connector *connector;
Jani Nikula593e0622015-01-23 15:30:56 +0200989 struct drm_display_mode *scan, *fixed_mode = NULL;
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +0530990 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7e9804f2015-01-16 14:27:23 +0200991 enum port port;
Jani Nikula4e646492013-08-27 15:12:20 +0300992 unsigned int i;
993
994 DRM_DEBUG_KMS("\n");
995
Shobhit Kumar3e6bd012014-05-27 19:33:59 +0530996 /* There is no detection method for MIPI so rely on VBT */
997 if (!dev_priv->vbt.has_mipi)
Damien Lespiau4328633d2014-05-28 12:30:56 +0100998 return;
Jani Nikula4e646492013-08-27 15:12:20 +0300999
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +05301000 if (IS_VALLEYVIEW(dev)) {
1001 dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
1002 } else {
1003 DRM_ERROR("Unsupported Mipi device to reg base");
Christoph Jaeger868d6652014-06-13 21:51:22 +02001004 return;
Shashank Sharmab6fdd0f2014-05-19 20:54:03 +05301005 }
1006
Jani Nikula4e646492013-08-27 15:12:20 +03001007 intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1008 if (!intel_dsi)
Damien Lespiau4328633d2014-05-28 12:30:56 +01001009 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001010
1011 intel_connector = kzalloc(sizeof(*intel_connector), GFP_KERNEL);
1012 if (!intel_connector) {
1013 kfree(intel_dsi);
Damien Lespiau4328633d2014-05-28 12:30:56 +01001014 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001015 }
1016
1017 intel_encoder = &intel_dsi->base;
1018 encoder = &intel_encoder->base;
1019 intel_dsi->attached_connector = intel_connector;
1020
Jani Nikula4e646492013-08-27 15:12:20 +03001021 connector = &intel_connector->base;
1022
1023 drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI);
1024
1025 /* XXX: very likely not all of these are needed */
1026 intel_encoder->hot_plug = intel_dsi_hot_plug;
1027 intel_encoder->compute_config = intel_dsi_compute_config;
1028 intel_encoder->pre_pll_enable = intel_dsi_pre_pll_enable;
1029 intel_encoder->pre_enable = intel_dsi_pre_enable;
Shobhit Kumar2634fd72014-04-09 13:59:31 +05301030 intel_encoder->enable = intel_dsi_enable_nop;
Imre Deakc315faf2014-05-27 19:00:09 +03001031 intel_encoder->disable = intel_dsi_pre_disable;
Jani Nikula4e646492013-08-27 15:12:20 +03001032 intel_encoder->post_disable = intel_dsi_post_disable;
1033 intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1034 intel_encoder->get_config = intel_dsi_get_config;
1035
1036 intel_connector->get_hw_state = intel_connector_get_hw_state;
Imre Deak4932e2c2014-02-11 17:12:48 +02001037 intel_connector->unregister = intel_connector_unregister;
Jani Nikula4e646492013-08-27 15:12:20 +03001038
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001039 /* Pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI port C */
Jani Nikula7e9804f2015-01-16 14:27:23 +02001040 if (dev_priv->vbt.dsi.config->dual_link) {
1041 /* XXX: does dual link work on either pipe? */
1042 intel_encoder->crtc_mask = (1 << PIPE_A);
1043 intel_dsi->ports = ((1 << PORT_A) | (1 << PORT_C));
1044 } else if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIA) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001045 intel_encoder->crtc_mask = (1 << PIPE_A);
Jani Nikula17af40a2014-11-14 16:54:22 +02001046 intel_dsi->ports = (1 << PORT_A);
1047 } else if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIC) {
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001048 intel_encoder->crtc_mask = (1 << PIPE_B);
Jani Nikula17af40a2014-11-14 16:54:22 +02001049 intel_dsi->ports = (1 << PORT_C);
1050 }
Jani Nikulae7d7cad2014-11-14 16:54:21 +02001051
Jani Nikula7e9804f2015-01-16 14:27:23 +02001052 /* Create a DSI host (and a device) for each port. */
1053 for_each_dsi_port(port, intel_dsi->ports) {
1054 struct intel_dsi_host *host;
1055
1056 host = intel_dsi_host_init(intel_dsi, port);
1057 if (!host)
1058 goto err;
1059
1060 intel_dsi->dsi_hosts[port] = host;
1061 }
1062
Jani Nikula593e0622015-01-23 15:30:56 +02001063 for (i = 0; i < ARRAY_SIZE(intel_dsi_drivers); i++) {
1064 intel_dsi->panel = intel_dsi_drivers[i].init(intel_dsi,
1065 intel_dsi_drivers[i].panel_id);
1066 if (intel_dsi->panel)
Jani Nikula4e646492013-08-27 15:12:20 +03001067 break;
1068 }
1069
Jani Nikula593e0622015-01-23 15:30:56 +02001070 if (!intel_dsi->panel) {
Jani Nikula4e646492013-08-27 15:12:20 +03001071 DRM_DEBUG_KMS("no device found\n");
1072 goto err;
1073 }
1074
1075 intel_encoder->type = INTEL_OUTPUT_DSI;
Ville Syrjäläbc079e82014-03-03 16:15:28 +02001076 intel_encoder->cloneable = 0;
Jani Nikula4e646492013-08-27 15:12:20 +03001077 drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
1078 DRM_MODE_CONNECTOR_DSI);
1079
1080 drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1081
1082 connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1083 connector->interlace_allowed = false;
1084 connector->doublescan_allowed = false;
1085
1086 intel_connector_attach_encoder(intel_connector, intel_encoder);
1087
Thomas Wood34ea3d32014-05-29 16:57:41 +01001088 drm_connector_register(connector);
Jani Nikula4e646492013-08-27 15:12:20 +03001089
Jani Nikula593e0622015-01-23 15:30:56 +02001090 drm_panel_attach(intel_dsi->panel, connector);
1091
1092 mutex_lock(&dev->mode_config.mutex);
1093 drm_panel_get_modes(intel_dsi->panel);
1094 list_for_each_entry(scan, &connector->probed_modes, head) {
1095 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
1096 fixed_mode = drm_mode_duplicate(dev, scan);
1097 break;
1098 }
1099 }
1100 mutex_unlock(&dev->mode_config.mutex);
1101
Jani Nikula4e646492013-08-27 15:12:20 +03001102 if (!fixed_mode) {
1103 DRM_DEBUG_KMS("no fixed mode\n");
1104 goto err;
1105 }
1106
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301107 intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
Jani Nikula4e646492013-08-27 15:12:20 +03001108
Damien Lespiau4328633d2014-05-28 12:30:56 +01001109 return;
Jani Nikula4e646492013-08-27 15:12:20 +03001110
1111err:
1112 drm_encoder_cleanup(&intel_encoder->base);
1113 kfree(intel_dsi);
1114 kfree(intel_connector);
Jani Nikula4e646492013-08-27 15:12:20 +03001115}