2 * Copyright © 2013 Intel Corporation
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:
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
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.
23 * Author: Jani Nikula <jani.nikula@intel.com>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_crtc.h>
29 #include <drm/drm_edid.h>
30 #include <drm/i915_drm.h>
31 #include <drm/drm_panel.h>
32 #include <drm/drm_mipi_dsi.h>
33 #include <linux/slab.h>
34 #include <linux/gpio/consumer.h>
36 #include "intel_drv.h"
37 #include "intel_dsi.h"
41 struct drm_panel * (*init)(struct intel_dsi *intel_dsi, u16 panel_id);
42 } intel_dsi_drivers[] = {
44 .panel_id = MIPI_DSI_GENERIC_PANEL_ID,
45 .init = vbt_panel_init,
49 static void wait_for_dsi_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
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;
56 mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
57 LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
59 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & mask) == mask, 100))
60 DRM_ERROR("DPI FIFOs are not empty\n");
63 static void write_data(struct drm_i915_private *dev_priv,
65 const u8 *data, u32 len)
69 for (i = 0; i < len; i += 4) {
72 for (j = 0; j < min_t(u32, len - i, 4); j++)
73 val |= *data++ << 8 * j;
79 static void read_data(struct drm_i915_private *dev_priv,
85 for (i = 0; i < len; i += 4) {
86 u32 val = I915_READ(reg);
88 for (j = 0; j < min_t(u32, len - i, 4); j++)
89 *data++ = val >> 8 * j;
93 static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
94 const struct mipi_dsi_msg *msg)
96 struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
97 struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
98 struct drm_i915_private *dev_priv = dev->dev_private;
99 enum port port = intel_dsi_host->port;
100 struct mipi_dsi_packet packet;
102 const u8 *header, *data;
103 i915_reg_t data_reg, ctrl_reg;
104 u32 data_mask, ctrl_mask;
106 ret = mipi_dsi_create_packet(&packet, msg);
110 header = packet.header;
111 data = packet.payload;
113 if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
114 data_reg = MIPI_LP_GEN_DATA(port);
115 data_mask = LP_DATA_FIFO_FULL;
116 ctrl_reg = MIPI_LP_GEN_CTRL(port);
117 ctrl_mask = LP_CTRL_FIFO_FULL;
119 data_reg = MIPI_HS_GEN_DATA(port);
120 data_mask = HS_DATA_FIFO_FULL;
121 ctrl_reg = MIPI_HS_GEN_CTRL(port);
122 ctrl_mask = HS_CTRL_FIFO_FULL;
125 /* note: this is never true for reads */
126 if (packet.payload_length) {
128 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & data_mask) == 0, 50))
129 DRM_ERROR("Timeout waiting for HS/LP DATA FIFO !full\n");
131 write_data(dev_priv, data_reg, packet.payload,
132 packet.payload_length);
136 I915_WRITE(MIPI_INTR_STAT(port), GEN_READ_DATA_AVAIL);
139 if (wait_for((I915_READ(MIPI_GEN_FIFO_STAT(port)) & ctrl_mask) == 0, 50)) {
140 DRM_ERROR("Timeout waiting for HS/LP CTRL FIFO !full\n");
143 I915_WRITE(ctrl_reg, header[2] << 16 | header[1] << 8 | header[0]);
145 /* ->rx_len is set only for reads */
147 data_mask = GEN_READ_DATA_AVAIL;
148 if (wait_for((I915_READ(MIPI_INTR_STAT(port)) & data_mask) == data_mask, 50))
149 DRM_ERROR("Timeout waiting for read data.\n");
151 read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
154 /* XXX: fix for reads and writes */
155 return 4 + packet.payload_length;
158 static int intel_dsi_host_attach(struct mipi_dsi_host *host,
159 struct mipi_dsi_device *dsi)
164 static int intel_dsi_host_detach(struct mipi_dsi_host *host,
165 struct mipi_dsi_device *dsi)
170 static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
171 .attach = intel_dsi_host_attach,
172 .detach = intel_dsi_host_detach,
173 .transfer = intel_dsi_host_transfer,
176 static struct intel_dsi_host *intel_dsi_host_init(struct intel_dsi *intel_dsi,
179 struct intel_dsi_host *host;
180 struct mipi_dsi_device *device;
182 host = kzalloc(sizeof(*host), GFP_KERNEL);
186 host->base.ops = &intel_dsi_host_ops;
187 host->intel_dsi = intel_dsi;
191 * We should call mipi_dsi_host_register(&host->base) here, but we don't
192 * have a host->dev, and we don't have OF stuff either. So just use the
193 * dsi framework as a library and hope for the best. Create the dsi
194 * devices by ourselves here too. Need to be careful though, because we
195 * don't initialize any of the driver model devices here.
197 device = kzalloc(sizeof(*device), GFP_KERNEL);
203 device->host = &host->base;
204 host->device = device;
210 * send a video mode command
212 * XXX: commands with data in MIPI_DPI_DATA?
214 static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
217 struct drm_encoder *encoder = &intel_dsi->base.base;
218 struct drm_device *dev = encoder->dev;
219 struct drm_i915_private *dev_priv = dev->dev_private;
229 I915_WRITE(MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
231 /* XXX: old code skips write if control unchanged */
232 if (cmd == I915_READ(MIPI_DPI_CONTROL(port)))
233 DRM_ERROR("Same special packet %02x twice in a row.\n", cmd);
235 I915_WRITE(MIPI_DPI_CONTROL(port), cmd);
237 mask = SPL_PKT_SENT_INTERRUPT;
238 if (wait_for((I915_READ(MIPI_INTR_STAT(port)) & mask) == mask, 100))
239 DRM_ERROR("Video mode command 0x%08x send failed.\n", cmd);
244 static void band_gap_reset(struct drm_i915_private *dev_priv)
246 mutex_lock(&dev_priv->sb_lock);
248 vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
249 vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
250 vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
252 vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
253 vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
255 mutex_unlock(&dev_priv->sb_lock);
258 static inline bool is_vid_mode(struct intel_dsi *intel_dsi)
260 return intel_dsi->operation_mode == INTEL_DSI_VIDEO_MODE;
263 static inline bool is_cmd_mode(struct intel_dsi *intel_dsi)
265 return intel_dsi->operation_mode == INTEL_DSI_COMMAND_MODE;
268 static bool intel_dsi_compute_config(struct intel_encoder *encoder,
269 struct intel_crtc_state *pipe_config)
271 struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
273 struct intel_connector *intel_connector = intel_dsi->attached_connector;
274 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
275 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
279 pipe_config->has_dsi_encoder = true;
282 intel_fixed_panel_mode(fixed_mode, adjusted_mode);
284 /* DSI uses short packets for sync events, so clear mode flags for DSI */
285 adjusted_mode->flags = 0;
290 static void bxt_dsi_device_ready(struct intel_encoder *encoder)
292 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
293 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
299 /* Exit Low power state in 4 steps*/
300 for_each_dsi_port(port, intel_dsi->ports) {
302 /* 1. Enable MIPI PHY transparent latch */
303 val = I915_READ(BXT_MIPI_PORT_CTRL(port));
304 I915_WRITE(BXT_MIPI_PORT_CTRL(port), val | LP_OUTPUT_HOLD);
305 usleep_range(2000, 2500);
308 val = I915_READ(MIPI_DEVICE_READY(port));
309 val &= ~ULPS_STATE_MASK;
310 val |= (ULPS_STATE_ENTER | DEVICE_READY);
311 I915_WRITE(MIPI_DEVICE_READY(port), val);
315 val = I915_READ(MIPI_DEVICE_READY(port));
316 val &= ~ULPS_STATE_MASK;
317 val |= (ULPS_STATE_EXIT | DEVICE_READY);
318 I915_WRITE(MIPI_DEVICE_READY(port), val);
319 usleep_range(1000, 1500);
321 /* Clear ULPS and set device ready */
322 val = I915_READ(MIPI_DEVICE_READY(port));
323 val &= ~ULPS_STATE_MASK;
325 I915_WRITE(MIPI_DEVICE_READY(port), val);
329 static void vlv_dsi_device_ready(struct intel_encoder *encoder)
331 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
332 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
338 mutex_lock(&dev_priv->sb_lock);
339 /* program rcomp for compliance, reduce from 50 ohms to 45 ohms
340 * needed everytime after power gate */
341 vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
342 mutex_unlock(&dev_priv->sb_lock);
344 /* bandgap reset is needed after everytime we do power gate */
345 band_gap_reset(dev_priv);
347 for_each_dsi_port(port, intel_dsi->ports) {
349 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_ENTER);
350 usleep_range(2500, 3000);
352 /* Enable MIPI PHY transparent latch
353 * Common bit for both MIPI Port A & MIPI Port C
354 * No similar bit in MIPI Port C reg
356 val = I915_READ(MIPI_PORT_CTRL(PORT_A));
357 I915_WRITE(MIPI_PORT_CTRL(PORT_A), val | LP_OUTPUT_HOLD);
358 usleep_range(1000, 1500);
360 I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_EXIT);
361 usleep_range(2500, 3000);
363 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY);
364 usleep_range(2500, 3000);
368 static void intel_dsi_device_ready(struct intel_encoder *encoder)
370 struct drm_device *dev = encoder->base.dev;
372 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
373 vlv_dsi_device_ready(encoder);
374 else if (IS_BROXTON(dev))
375 bxt_dsi_device_ready(encoder);
378 static void intel_dsi_port_enable(struct intel_encoder *encoder)
380 struct drm_device *dev = encoder->base.dev;
381 struct drm_i915_private *dev_priv = dev->dev_private;
382 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
383 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
386 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
389 temp = I915_READ(VLV_CHICKEN_3);
390 temp &= ~PIXEL_OVERLAP_CNT_MASK |
391 intel_dsi->pixel_overlap <<
392 PIXEL_OVERLAP_CNT_SHIFT;
393 I915_WRITE(VLV_CHICKEN_3, temp);
396 for_each_dsi_port(port, intel_dsi->ports) {
397 i915_reg_t port_ctrl = IS_BROXTON(dev) ?
398 BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
401 temp = I915_READ(port_ctrl);
403 temp &= ~LANE_CONFIGURATION_MASK;
404 temp &= ~DUAL_LINK_MODE_MASK;
406 if (intel_dsi->ports == ((1 << PORT_A) | (1 << PORT_C))) {
407 temp |= (intel_dsi->dual_link - 1)
408 << DUAL_LINK_MODE_SHIFT;
409 temp |= intel_crtc->pipe ?
410 LANE_CONFIGURATION_DUAL_LINK_B :
411 LANE_CONFIGURATION_DUAL_LINK_A;
413 /* assert ip_tg_enable signal */
414 I915_WRITE(port_ctrl, temp | DPI_ENABLE);
415 POSTING_READ(port_ctrl);
419 static void intel_dsi_port_disable(struct intel_encoder *encoder)
421 struct drm_device *dev = encoder->base.dev;
422 struct drm_i915_private *dev_priv = dev->dev_private;
423 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
426 for_each_dsi_port(port, intel_dsi->ports) {
427 i915_reg_t port_ctrl = IS_BROXTON(dev) ?
428 BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
431 /* de-assert ip_tg_enable signal */
432 temp = I915_READ(port_ctrl);
433 I915_WRITE(port_ctrl, temp & ~DPI_ENABLE);
434 POSTING_READ(port_ctrl);
438 static void intel_dsi_enable(struct intel_encoder *encoder)
440 struct drm_device *dev = encoder->base.dev;
441 struct drm_i915_private *dev_priv = dev->dev_private;
442 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
447 if (is_cmd_mode(intel_dsi)) {
448 for_each_dsi_port(port, intel_dsi->ports)
449 I915_WRITE(MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
451 msleep(20); /* XXX */
452 for_each_dsi_port(port, intel_dsi->ports)
453 dpi_send_cmd(intel_dsi, TURN_ON, false, port);
456 drm_panel_enable(intel_dsi->panel);
458 for_each_dsi_port(port, intel_dsi->ports)
459 wait_for_dsi_fifo_empty(intel_dsi, port);
461 intel_dsi_port_enable(encoder);
464 intel_panel_enable_backlight(intel_dsi->attached_connector);
467 static void intel_dsi_prepare(struct intel_encoder *intel_encoder);
469 static void intel_dsi_pre_enable(struct intel_encoder *encoder)
471 struct drm_device *dev = encoder->base.dev;
472 struct drm_i915_private *dev_priv = dev->dev_private;
473 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
474 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
475 enum pipe pipe = intel_crtc->pipe;
481 intel_enable_dsi_pll(encoder);
482 intel_dsi_prepare(encoder);
484 /* Panel Enable over CRC PMIC */
485 if (intel_dsi->gpio_panel)
486 gpiod_set_value_cansleep(intel_dsi->gpio_panel, 1);
488 msleep(intel_dsi->panel_on_delay);
490 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
492 * Disable DPOunit clock gating, can stall pipe
493 * and we need DPLL REFA always enabled
495 tmp = I915_READ(DPLL(pipe));
496 tmp |= DPLL_REF_CLK_ENABLE_VLV;
497 I915_WRITE(DPLL(pipe), tmp);
499 /* update the hw state for DPLL */
500 intel_crtc->config->dpll_hw_state.dpll =
501 DPLL_INTEGRATED_REF_CLK_VLV |
502 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
504 tmp = I915_READ(DSPCLK_GATE_D);
505 tmp |= DPOUNIT_CLOCK_GATE_DISABLE;
506 I915_WRITE(DSPCLK_GATE_D, tmp);
509 /* put device in ready state */
510 intel_dsi_device_ready(encoder);
512 drm_panel_prepare(intel_dsi->panel);
514 for_each_dsi_port(port, intel_dsi->ports)
515 wait_for_dsi_fifo_empty(intel_dsi, port);
517 /* Enable port in pre-enable phase itself because as per hw team
518 * recommendation, port should be enabled befor plane & pipe */
519 intel_dsi_enable(encoder);
522 static void intel_dsi_enable_nop(struct intel_encoder *encoder)
526 /* for DSI port enable has to be done before pipe
527 * and plane enable, so port enable is done in
528 * pre_enable phase itself unlike other encoders
532 static void intel_dsi_pre_disable(struct intel_encoder *encoder)
534 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
539 intel_panel_disable_backlight(intel_dsi->attached_connector);
541 if (is_vid_mode(intel_dsi)) {
542 /* Send Shutdown command to the panel in LP mode */
543 for_each_dsi_port(port, intel_dsi->ports)
544 dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
549 static void intel_dsi_disable(struct intel_encoder *encoder)
551 struct drm_device *dev = encoder->base.dev;
552 struct drm_i915_private *dev_priv = dev->dev_private;
553 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
559 if (is_vid_mode(intel_dsi)) {
560 for_each_dsi_port(port, intel_dsi->ports)
561 wait_for_dsi_fifo_empty(intel_dsi, port);
563 intel_dsi_port_disable(encoder);
567 for_each_dsi_port(port, intel_dsi->ports) {
568 /* Panel commands can be sent when clock is in LP11 */
569 I915_WRITE(MIPI_DEVICE_READY(port), 0x0);
571 intel_dsi_reset_clocks(encoder, port);
572 I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
574 temp = I915_READ(MIPI_DSI_FUNC_PRG(port));
575 temp &= ~VID_MODE_FORMAT_MASK;
576 I915_WRITE(MIPI_DSI_FUNC_PRG(port), temp);
578 I915_WRITE(MIPI_DEVICE_READY(port), 0x1);
580 /* if disable packets are sent before sending shutdown packet then in
581 * some next enable sequence send turn on packet error is observed */
582 drm_panel_disable(intel_dsi->panel);
584 for_each_dsi_port(port, intel_dsi->ports)
585 wait_for_dsi_fifo_empty(intel_dsi, port);
588 static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
590 struct drm_device *dev = encoder->base.dev;
591 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
592 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
596 for_each_dsi_port(port, intel_dsi->ports) {
597 /* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
598 i915_reg_t port_ctrl = IS_BROXTON(dev) ?
599 BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
602 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
604 usleep_range(2000, 2500);
606 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
608 usleep_range(2000, 2500);
610 I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
612 usleep_range(2000, 2500);
614 /* Wait till Clock lanes are in LP-00 state for MIPI Port A
615 * only. MIPI Port C has no similar bit for checking
617 if (wait_for(((I915_READ(port_ctrl) & AFE_LATCHOUT)
619 DRM_ERROR("DSI LP not going Low\n");
621 /* Disable MIPI PHY transparent latch */
622 val = I915_READ(port_ctrl);
623 I915_WRITE(port_ctrl, val & ~LP_OUTPUT_HOLD);
624 usleep_range(1000, 1500);
626 I915_WRITE(MIPI_DEVICE_READY(port), 0x00);
627 usleep_range(2000, 2500);
630 intel_disable_dsi_pll(encoder);
633 static void intel_dsi_post_disable(struct intel_encoder *encoder)
635 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
636 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
641 intel_dsi_disable(encoder);
643 intel_dsi_clear_device_ready(encoder);
645 val = I915_READ(DSPCLK_GATE_D);
646 val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
647 I915_WRITE(DSPCLK_GATE_D, val);
649 drm_panel_unprepare(intel_dsi->panel);
651 msleep(intel_dsi->panel_off_delay);
652 msleep(intel_dsi->panel_pwr_cycle_delay);
654 /* Panel Disable over CRC PMIC */
655 if (intel_dsi->gpio_panel)
656 gpiod_set_value_cansleep(intel_dsi->gpio_panel, 0);
659 static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
662 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
663 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
664 struct drm_device *dev = encoder->base.dev;
665 enum intel_display_power_domain power_domain;
670 power_domain = intel_display_port_power_domain(encoder);
671 if (!intel_display_power_is_enabled(dev_priv, power_domain))
674 /* XXX: this only works for one DSI output */
675 for_each_dsi_port(port, intel_dsi->ports) {
676 i915_reg_t ctrl_reg = IS_BROXTON(dev) ?
677 BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
678 u32 dpi_enabled, func;
680 func = I915_READ(MIPI_DSI_FUNC_PRG(port));
681 dpi_enabled = I915_READ(ctrl_reg) & DPI_ENABLE;
683 /* Due to some hardware limitations on BYT, MIPI Port C DPI
684 * Enable bit does not get set. To check whether DSI Port C
685 * was enabled in BIOS, check the Pipe B enable bit
687 if (IS_VALLEYVIEW(dev) && port == PORT_C)
688 dpi_enabled = I915_READ(PIPECONF(PIPE_B)) &
691 if (dpi_enabled || (func & CMD_MODE_DATA_WIDTH_MASK)) {
692 if (I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY) {
693 *pipe = port == PORT_A ? PIPE_A : PIPE_B;
702 static void intel_dsi_get_config(struct intel_encoder *encoder,
703 struct intel_crtc_state *pipe_config)
708 pipe_config->has_dsi_encoder = true;
711 * DPLL_MD is not used in case of DSI, reading will get some default value
714 pipe_config->dpll_hw_state.dpll_md = 0;
716 pclk = intel_dsi_get_pclk(encoder, pipe_config->pipe_bpp);
720 pipe_config->base.adjusted_mode.crtc_clock = pclk;
721 pipe_config->port_clock = pclk;
724 static enum drm_mode_status
725 intel_dsi_mode_valid(struct drm_connector *connector,
726 struct drm_display_mode *mode)
728 struct intel_connector *intel_connector = to_intel_connector(connector);
729 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
730 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
734 if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
735 DRM_DEBUG_KMS("MODE_NO_DBLESCAN\n");
736 return MODE_NO_DBLESCAN;
740 if (mode->hdisplay > fixed_mode->hdisplay)
742 if (mode->vdisplay > fixed_mode->vdisplay)
744 if (fixed_mode->clock > max_dotclk)
745 return MODE_CLOCK_HIGH;
751 /* return txclkesc cycles in terms of divider and duration in us */
752 static u16 txclkesc(u32 divider, unsigned int us)
755 case ESCAPE_CLOCK_DIVIDER_1:
758 case ESCAPE_CLOCK_DIVIDER_2:
760 case ESCAPE_CLOCK_DIVIDER_4:
765 /* return pixels in terms of txbyteclkhs */
766 static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
767 u16 burst_mode_ratio)
769 return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
770 8 * 100), lane_count);
773 static void set_dsi_timings(struct drm_encoder *encoder,
774 const struct drm_display_mode *adjusted_mode)
776 struct drm_device *dev = encoder->dev;
777 struct drm_i915_private *dev_priv = dev->dev_private;
778 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
779 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
781 unsigned int bpp = intel_crtc->config->pipe_bpp;
782 unsigned int lane_count = intel_dsi->lane_count;
784 u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
786 hactive = adjusted_mode->crtc_hdisplay;
787 hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
788 hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
789 hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
791 if (intel_dsi->dual_link) {
793 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
794 hactive += intel_dsi->pixel_overlap;
800 vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
801 vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
802 vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
804 /* horizontal values are in terms of high speed byte clock */
805 hactive = txbyteclkhs(hactive, bpp, lane_count,
806 intel_dsi->burst_mode_ratio);
807 hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
808 hsync = txbyteclkhs(hsync, bpp, lane_count,
809 intel_dsi->burst_mode_ratio);
810 hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
812 for_each_dsi_port(port, intel_dsi->ports) {
813 if (IS_BROXTON(dev)) {
815 * Program hdisplay and vdisplay on MIPI transcoder.
816 * This is different from calculated hactive and
817 * vactive, as they are calculated per channel basis,
818 * whereas these values should be based on resolution.
820 I915_WRITE(BXT_MIPI_TRANS_HACTIVE(port),
821 adjusted_mode->crtc_hdisplay);
822 I915_WRITE(BXT_MIPI_TRANS_VACTIVE(port),
823 adjusted_mode->crtc_vdisplay);
824 I915_WRITE(BXT_MIPI_TRANS_VTOTAL(port),
825 adjusted_mode->crtc_vtotal);
828 I915_WRITE(MIPI_HACTIVE_AREA_COUNT(port), hactive);
829 I915_WRITE(MIPI_HFP_COUNT(port), hfp);
831 /* meaningful for video mode non-burst sync pulse mode only,
832 * can be zero for non-burst sync events and burst modes */
833 I915_WRITE(MIPI_HSYNC_PADDING_COUNT(port), hsync);
834 I915_WRITE(MIPI_HBP_COUNT(port), hbp);
836 /* vertical values are in terms of lines */
837 I915_WRITE(MIPI_VFP_COUNT(port), vfp);
838 I915_WRITE(MIPI_VSYNC_PADDING_COUNT(port), vsync);
839 I915_WRITE(MIPI_VBP_COUNT(port), vbp);
843 static void intel_dsi_prepare(struct intel_encoder *intel_encoder)
845 struct drm_encoder *encoder = &intel_encoder->base;
846 struct drm_device *dev = encoder->dev;
847 struct drm_i915_private *dev_priv = dev->dev_private;
848 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
849 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
850 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
852 unsigned int bpp = intel_crtc->config->pipe_bpp;
856 DRM_DEBUG_KMS("pipe %c\n", pipe_name(intel_crtc->pipe));
858 mode_hdisplay = adjusted_mode->crtc_hdisplay;
860 if (intel_dsi->dual_link) {
862 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
863 mode_hdisplay += intel_dsi->pixel_overlap;
866 for_each_dsi_port(port, intel_dsi->ports) {
867 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
869 * escape clock divider, 20MHz, shared for A and C.
870 * device ready must be off when doing this! txclkesc?
872 tmp = I915_READ(MIPI_CTRL(PORT_A));
873 tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
874 I915_WRITE(MIPI_CTRL(PORT_A), tmp |
875 ESCAPE_CLOCK_DIVIDER_1);
877 /* read request priority is per pipe */
878 tmp = I915_READ(MIPI_CTRL(port));
879 tmp &= ~READ_REQUEST_PRIORITY_MASK;
880 I915_WRITE(MIPI_CTRL(port), tmp |
881 READ_REQUEST_PRIORITY_HIGH);
882 } else if (IS_BROXTON(dev)) {
883 enum pipe pipe = intel_crtc->pipe;
885 tmp = I915_READ(MIPI_CTRL(port));
886 tmp &= ~BXT_PIPE_SELECT_MASK;
888 tmp |= BXT_PIPE_SELECT(pipe);
889 I915_WRITE(MIPI_CTRL(port), tmp);
892 /* XXX: why here, why like this? handling in irq handler?! */
893 I915_WRITE(MIPI_INTR_STAT(port), 0xffffffff);
894 I915_WRITE(MIPI_INTR_EN(port), 0xffffffff);
896 I915_WRITE(MIPI_DPHY_PARAM(port), intel_dsi->dphy_reg);
898 I915_WRITE(MIPI_DPI_RESOLUTION(port),
899 adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT |
900 mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
903 set_dsi_timings(encoder, adjusted_mode);
905 val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
906 if (is_cmd_mode(intel_dsi)) {
907 val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
908 val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
910 val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
912 /* XXX: cross-check bpp vs. pixel format? */
913 val |= intel_dsi->pixel_format;
917 if (intel_dsi->eotp_pkt == 0)
919 if (intel_dsi->clock_stop)
922 for_each_dsi_port(port, intel_dsi->ports) {
923 I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
925 /* timeouts for recovery. one frame IIUC. if counter expires,
926 * EOT and stop state. */
929 * In burst mode, value greater than one DPI line Time in byte
930 * clock (txbyteclkhs) To timeout this timer 1+ of the above
931 * said value is recommended.
933 * In non-burst mode, Value greater than one DPI frame time in
934 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
935 * said value is recommended.
937 * In DBI only mode, value greater than one DBI frame time in
938 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
939 * said value is recommended.
942 if (is_vid_mode(intel_dsi) &&
943 intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
944 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
945 txbyteclkhs(adjusted_mode->crtc_htotal, bpp,
946 intel_dsi->lane_count,
947 intel_dsi->burst_mode_ratio) + 1);
949 I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
950 txbyteclkhs(adjusted_mode->crtc_vtotal *
951 adjusted_mode->crtc_htotal,
952 bpp, intel_dsi->lane_count,
953 intel_dsi->burst_mode_ratio) + 1);
955 I915_WRITE(MIPI_LP_RX_TIMEOUT(port), intel_dsi->lp_rx_timeout);
956 I915_WRITE(MIPI_TURN_AROUND_TIMEOUT(port),
957 intel_dsi->turn_arnd_val);
958 I915_WRITE(MIPI_DEVICE_RESET_TIMER(port),
959 intel_dsi->rst_timer_val);
963 /* in terms of low power clock */
964 I915_WRITE(MIPI_INIT_COUNT(port),
965 txclkesc(intel_dsi->escape_clk_div, 100));
967 if (IS_BROXTON(dev) && (!intel_dsi->dual_link)) {
969 * BXT spec says write MIPI_INIT_COUNT for
970 * both the ports, even if only one is
971 * getting used. So write the other port
972 * if not in dual link mode.
974 I915_WRITE(MIPI_INIT_COUNT(port ==
975 PORT_A ? PORT_C : PORT_A),
976 intel_dsi->init_count);
979 /* recovery disables */
980 I915_WRITE(MIPI_EOT_DISABLE(port), tmp);
982 /* in terms of low power clock */
983 I915_WRITE(MIPI_INIT_COUNT(port), intel_dsi->init_count);
985 /* in terms of txbyteclkhs. actual high to low switch +
986 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
988 * XXX: write MIPI_STOP_STATE_STALL?
990 I915_WRITE(MIPI_HIGH_LOW_SWITCH_COUNT(port),
991 intel_dsi->hs_to_lp_count);
993 /* XXX: low power clock equivalence in terms of byte clock.
994 * the number of byte clocks occupied in one low power clock.
995 * based on txbyteclkhs and txclkesc.
996 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
999 I915_WRITE(MIPI_LP_BYTECLK(port), intel_dsi->lp_byte_clk);
1001 /* the bw essential for transmitting 16 long packets containing
1002 * 252 bytes meant for dcs write memory command is programmed in
1003 * this register in terms of byte clocks. based on dsi transfer
1004 * rate and the number of lanes configured the time taken to
1005 * transmit 16 long packets in a dsi stream varies. */
1006 I915_WRITE(MIPI_DBI_BW_CTRL(port), intel_dsi->bw_timer);
1008 I915_WRITE(MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
1009 intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT |
1010 intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
1012 if (is_vid_mode(intel_dsi))
1013 /* Some panels might have resolution which is not a
1014 * multiple of 64 like 1366 x 768. Enable RANDOM
1015 * resolution support for such panels by default */
1016 I915_WRITE(MIPI_VIDEO_MODE_FORMAT(port),
1017 intel_dsi->video_frmt_cfg_bits |
1018 intel_dsi->video_mode_format |
1020 RANDOM_DPI_DISPLAY_RESOLUTION);
1024 static enum drm_connector_status
1025 intel_dsi_detect(struct drm_connector *connector, bool force)
1027 return connector_status_connected;
1030 static int intel_dsi_get_modes(struct drm_connector *connector)
1032 struct intel_connector *intel_connector = to_intel_connector(connector);
1033 struct drm_display_mode *mode;
1035 DRM_DEBUG_KMS("\n");
1037 if (!intel_connector->panel.fixed_mode) {
1038 DRM_DEBUG_KMS("no fixed mode\n");
1042 mode = drm_mode_duplicate(connector->dev,
1043 intel_connector->panel.fixed_mode);
1045 DRM_DEBUG_KMS("drm_mode_duplicate failed\n");
1049 drm_mode_probed_add(connector, mode);
1053 static void intel_dsi_connector_destroy(struct drm_connector *connector)
1055 struct intel_connector *intel_connector = to_intel_connector(connector);
1057 DRM_DEBUG_KMS("\n");
1058 intel_panel_fini(&intel_connector->panel);
1059 drm_connector_cleanup(connector);
1063 static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
1065 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1067 if (intel_dsi->panel) {
1068 drm_panel_detach(intel_dsi->panel);
1069 /* XXX: Logically this call belongs in the panel driver. */
1070 drm_panel_remove(intel_dsi->panel);
1073 /* dispose of the gpios */
1074 if (intel_dsi->gpio_panel)
1075 gpiod_put(intel_dsi->gpio_panel);
1077 intel_encoder_destroy(encoder);
1080 static const struct drm_encoder_funcs intel_dsi_funcs = {
1081 .destroy = intel_dsi_encoder_destroy,
1084 static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1085 .get_modes = intel_dsi_get_modes,
1086 .mode_valid = intel_dsi_mode_valid,
1087 .best_encoder = intel_best_encoder,
1090 static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1091 .dpms = drm_atomic_helper_connector_dpms,
1092 .detect = intel_dsi_detect,
1093 .destroy = intel_dsi_connector_destroy,
1094 .fill_modes = drm_helper_probe_single_connector_modes,
1095 .atomic_get_property = intel_connector_atomic_get_property,
1096 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1097 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1100 void intel_dsi_init(struct drm_device *dev)
1102 struct intel_dsi *intel_dsi;
1103 struct intel_encoder *intel_encoder;
1104 struct drm_encoder *encoder;
1105 struct intel_connector *intel_connector;
1106 struct drm_connector *connector;
1107 struct drm_display_mode *scan, *fixed_mode = NULL;
1108 struct drm_i915_private *dev_priv = dev->dev_private;
1112 DRM_DEBUG_KMS("\n");
1114 /* There is no detection method for MIPI so rely on VBT */
1115 if (!dev_priv->vbt.has_mipi)
1118 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
1119 dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
1121 DRM_ERROR("Unsupported Mipi device to reg base");
1125 intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1129 intel_connector = intel_connector_alloc();
1130 if (!intel_connector) {
1135 intel_encoder = &intel_dsi->base;
1136 encoder = &intel_encoder->base;
1137 intel_dsi->attached_connector = intel_connector;
1139 connector = &intel_connector->base;
1141 drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
1144 intel_encoder->compute_config = intel_dsi_compute_config;
1145 intel_encoder->pre_enable = intel_dsi_pre_enable;
1146 intel_encoder->enable = intel_dsi_enable_nop;
1147 intel_encoder->disable = intel_dsi_pre_disable;
1148 intel_encoder->post_disable = intel_dsi_post_disable;
1149 intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1150 intel_encoder->get_config = intel_dsi_get_config;
1152 intel_connector->get_hw_state = intel_connector_get_hw_state;
1153 intel_connector->unregister = intel_connector_unregister;
1155 /* Pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI port C */
1156 if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIA) {
1157 intel_encoder->crtc_mask = (1 << PIPE_A);
1158 intel_dsi->ports = (1 << PORT_A);
1159 } else if (dev_priv->vbt.dsi.port == DVO_PORT_MIPIC) {
1160 intel_encoder->crtc_mask = (1 << PIPE_B);
1161 intel_dsi->ports = (1 << PORT_C);
1164 if (dev_priv->vbt.dsi.config->dual_link)
1165 intel_dsi->ports = ((1 << PORT_A) | (1 << PORT_C));
1167 /* Create a DSI host (and a device) for each port. */
1168 for_each_dsi_port(port, intel_dsi->ports) {
1169 struct intel_dsi_host *host;
1171 host = intel_dsi_host_init(intel_dsi, port);
1175 intel_dsi->dsi_hosts[port] = host;
1178 for (i = 0; i < ARRAY_SIZE(intel_dsi_drivers); i++) {
1179 intel_dsi->panel = intel_dsi_drivers[i].init(intel_dsi,
1180 intel_dsi_drivers[i].panel_id);
1181 if (intel_dsi->panel)
1185 if (!intel_dsi->panel) {
1186 DRM_DEBUG_KMS("no device found\n");
1191 * In case of BYT with CRC PMIC, we need to use GPIO for
1194 if (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC) {
1195 intel_dsi->gpio_panel =
1196 gpiod_get(dev->dev, "panel", GPIOD_OUT_HIGH);
1198 if (IS_ERR(intel_dsi->gpio_panel)) {
1199 DRM_ERROR("Failed to own gpio for panel control\n");
1200 intel_dsi->gpio_panel = NULL;
1204 intel_encoder->type = INTEL_OUTPUT_DSI;
1205 intel_encoder->cloneable = 0;
1206 drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
1207 DRM_MODE_CONNECTOR_DSI);
1209 drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1211 connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1212 connector->interlace_allowed = false;
1213 connector->doublescan_allowed = false;
1215 intel_connector_attach_encoder(intel_connector, intel_encoder);
1217 drm_connector_register(connector);
1219 drm_panel_attach(intel_dsi->panel, connector);
1221 mutex_lock(&dev->mode_config.mutex);
1222 drm_panel_get_modes(intel_dsi->panel);
1223 list_for_each_entry(scan, &connector->probed_modes, head) {
1224 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
1225 fixed_mode = drm_mode_duplicate(dev, scan);
1229 mutex_unlock(&dev->mode_config.mutex);
1232 DRM_DEBUG_KMS("no fixed mode\n");
1236 intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
1237 intel_panel_setup_backlight(connector, INVALID_PIPE);
1242 drm_encoder_cleanup(&intel_encoder->base);
1244 kfree(intel_connector);