1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2018 Intel Corporation
4  */
5 
6 #include <drm/drm_mipi_dsi.h>
7 #include "intel_dsi.h"
8 #include "intel_panel.h"
9 
10 int intel_dsi_bitrate(const struct intel_dsi *intel_dsi)
11 {
12 	int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
13 
14 	if (WARN_ON(bpp < 0))
15 		bpp = 16;
16 
17 	return intel_dsi->pclk * bpp / intel_dsi->lane_count;
18 }
19 
20 int intel_dsi_tlpx_ns(const struct intel_dsi *intel_dsi)
21 {
22 	switch (intel_dsi->escape_clk_div) {
23 	default:
24 	case 0:
25 		return 50;
26 	case 1:
27 		return 100;
28 	case 2:
29 		return 200;
30 	}
31 }
32 
33 int intel_dsi_get_modes(struct drm_connector *connector)
34 {
35 	struct drm_i915_private *i915 = to_i915(connector->dev);
36 	struct intel_connector *intel_connector = to_intel_connector(connector);
37 	struct drm_display_mode *mode;
38 
39 	drm_dbg_kms(&i915->drm, "\n");
40 
41 	if (!intel_connector->panel.fixed_mode) {
42 		drm_dbg_kms(&i915->drm, "no fixed mode\n");
43 		return 0;
44 	}
45 
46 	mode = drm_mode_duplicate(connector->dev,
47 				  intel_connector->panel.fixed_mode);
48 	if (!mode) {
49 		drm_dbg_kms(&i915->drm, "drm_mode_duplicate failed\n");
50 		return 0;
51 	}
52 
53 	drm_mode_probed_add(connector, mode);
54 	return 1;
55 }
56 
57 enum drm_mode_status intel_dsi_mode_valid(struct drm_connector *connector,
58 					  struct drm_display_mode *mode)
59 {
60 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
61 	struct intel_connector *intel_connector = to_intel_connector(connector);
62 	const struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
63 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
64 	enum drm_mode_status status;
65 
66 	drm_dbg_kms(&dev_priv->drm, "\n");
67 
68 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
69 		return MODE_NO_DBLESCAN;
70 
71 	status = intel_panel_mode_valid(intel_connector, mode);
72 	if (status != MODE_OK)
73 		return status;
74 
75 	if (fixed_mode->clock > max_dotclk)
76 		return MODE_CLOCK_HIGH;
77 
78 	return intel_mode_valid_max_plane_size(dev_priv, mode, false);
79 }
80 
81 struct intel_dsi_host *intel_dsi_host_init(struct intel_dsi *intel_dsi,
82 					   const struct mipi_dsi_host_ops *funcs,
83 					   enum port port)
84 {
85 	struct intel_dsi_host *host;
86 	struct mipi_dsi_device *device;
87 
88 	host = kzalloc(sizeof(*host), GFP_KERNEL);
89 	if (!host)
90 		return NULL;
91 
92 	host->base.ops = funcs;
93 	host->intel_dsi = intel_dsi;
94 	host->port = port;
95 
96 	/*
97 	 * We should call mipi_dsi_host_register(&host->base) here, but we don't
98 	 * have a host->dev, and we don't have OF stuff either. So just use the
99 	 * dsi framework as a library and hope for the best. Create the dsi
100 	 * devices by ourselves here too. Need to be careful though, because we
101 	 * don't initialize any of the driver model devices here.
102 	 */
103 	device = kzalloc(sizeof(*device), GFP_KERNEL);
104 	if (!device) {
105 		kfree(host);
106 		return NULL;
107 	}
108 
109 	device->host = &host->base;
110 	host->device = device;
111 
112 	return host;
113 }
114 
115 enum drm_panel_orientation
116 intel_dsi_get_panel_orientation(struct intel_connector *connector)
117 {
118 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
119 	enum drm_panel_orientation orientation;
120 
121 	orientation = dev_priv->vbt.dsi.orientation;
122 	if (orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
123 		return orientation;
124 
125 	orientation = dev_priv->vbt.orientation;
126 	if (orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
127 		return orientation;
128 
129 	return DRM_MODE_PANEL_ORIENTATION_NORMAL;
130 }
131