xref: /dflybsd-src/sys/dev/drm/i915/intel_dvo.c (revision bf0175970ba1c394f8039834a26ca6d163b23d0e)
1ba55f2f5SFrançois Tigeot /*
2ba55f2f5SFrançois Tigeot  * Copyright 2006 Dave Airlie <airlied@linux.ie>
3ba55f2f5SFrançois Tigeot  * Copyright © 2006-2007 Intel Corporation
4ba55f2f5SFrançois Tigeot  *
5ba55f2f5SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6ba55f2f5SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
7ba55f2f5SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
8ba55f2f5SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9ba55f2f5SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
10ba55f2f5SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
11ba55f2f5SFrançois Tigeot  *
12ba55f2f5SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
13ba55f2f5SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
14ba55f2f5SFrançois Tigeot  * Software.
15ba55f2f5SFrançois Tigeot  *
16ba55f2f5SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ba55f2f5SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ba55f2f5SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19ba55f2f5SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ba55f2f5SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21ba55f2f5SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22ba55f2f5SFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
23ba55f2f5SFrançois Tigeot  *
24ba55f2f5SFrançois Tigeot  * Authors:
25ba55f2f5SFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
26ba55f2f5SFrançois Tigeot  */
27ba55f2f5SFrançois Tigeot #include <linux/i2c.h>
28*bf017597SFrançois Tigeot #include <linux/slab.h>
29ba55f2f5SFrançois Tigeot #include <drm/drmP.h>
302c9916cdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
31ba55f2f5SFrançois Tigeot #include <drm/drm_crtc.h>
32ba55f2f5SFrançois Tigeot #include "intel_drv.h"
33ba55f2f5SFrançois Tigeot #include <drm/i915_drm.h>
34ba55f2f5SFrançois Tigeot #include "i915_drv.h"
35ba55f2f5SFrançois Tigeot #include "dvo.h"
36ba55f2f5SFrançois Tigeot 
37ba55f2f5SFrançois Tigeot #define SIL164_ADDR	0x38
38ba55f2f5SFrançois Tigeot #define CH7xxx_ADDR	0x76
39ba55f2f5SFrançois Tigeot #define TFP410_ADDR	0x38
40ba55f2f5SFrançois Tigeot #define NS2501_ADDR     0x38
41ba55f2f5SFrançois Tigeot 
42ba55f2f5SFrançois Tigeot static const struct intel_dvo_device intel_dvo_devices[] = {
43ba55f2f5SFrançois Tigeot 	{
44ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
45ba55f2f5SFrançois Tigeot 		.name = "sil164",
46ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
47aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
48ba55f2f5SFrançois Tigeot 		.slave_addr = SIL164_ADDR,
49ba55f2f5SFrançois Tigeot 		.dev_ops = &sil164_ops,
50ba55f2f5SFrançois Tigeot 	},
51ba55f2f5SFrançois Tigeot 	{
52ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
53ba55f2f5SFrançois Tigeot 		.name = "ch7xxx",
54ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
55aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
56ba55f2f5SFrançois Tigeot 		.slave_addr = CH7xxx_ADDR,
57ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7xxx_ops,
58ba55f2f5SFrançois Tigeot 	},
59ba55f2f5SFrançois Tigeot 	{
60ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
61ba55f2f5SFrançois Tigeot 		.name = "ch7xxx",
62ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
63aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
64ba55f2f5SFrançois Tigeot 		.slave_addr = 0x75, /* For some ch7010 */
65ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7xxx_ops,
66ba55f2f5SFrançois Tigeot 	},
67ba55f2f5SFrançois Tigeot 	{
68ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_LVDS,
69ba55f2f5SFrançois Tigeot 		.name = "ivch",
70ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOA,
71aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOA_SRCDIM,
72ba55f2f5SFrançois Tigeot 		.slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
73ba55f2f5SFrançois Tigeot 		.dev_ops = &ivch_ops,
74ba55f2f5SFrançois Tigeot 	},
75ba55f2f5SFrançois Tigeot 	{
76ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
77ba55f2f5SFrançois Tigeot 		.name = "tfp410",
78ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
79aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
80ba55f2f5SFrançois Tigeot 		.slave_addr = TFP410_ADDR,
81ba55f2f5SFrançois Tigeot 		.dev_ops = &tfp410_ops,
82ba55f2f5SFrançois Tigeot 	},
83ba55f2f5SFrançois Tigeot 	{
84ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_LVDS,
85ba55f2f5SFrançois Tigeot 		.name = "ch7017",
86ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
87aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
88ba55f2f5SFrançois Tigeot 		.slave_addr = 0x75,
8919c468b4SFrançois Tigeot 		.gpio = GMBUS_PIN_DPB,
90ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7017_ops,
91ba55f2f5SFrançois Tigeot 	},
92ba55f2f5SFrançois Tigeot 	{
93ba55f2f5SFrançois Tigeot 	        .type = INTEL_DVO_CHIP_TMDS,
94ba55f2f5SFrançois Tigeot 		.name = "ns2501",
951b13d190SFrançois Tigeot 		.dvo_reg = DVOB,
96aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOB_SRCDIM,
97ba55f2f5SFrançois Tigeot 		.slave_addr = NS2501_ADDR,
98ba55f2f5SFrançois Tigeot 		.dev_ops = &ns2501_ops,
99ba55f2f5SFrançois Tigeot        }
100ba55f2f5SFrançois Tigeot };
101ba55f2f5SFrançois Tigeot 
102ba55f2f5SFrançois Tigeot struct intel_dvo {
103ba55f2f5SFrançois Tigeot 	struct intel_encoder base;
104ba55f2f5SFrançois Tigeot 
105ba55f2f5SFrançois Tigeot 	struct intel_dvo_device dev;
106ba55f2f5SFrançois Tigeot 
107352ff8bdSFrançois Tigeot 	struct intel_connector *attached_connector;
108352ff8bdSFrançois Tigeot 
109ba55f2f5SFrançois Tigeot 	bool panel_wants_dither;
110ba55f2f5SFrançois Tigeot };
111ba55f2f5SFrançois Tigeot 
112ba55f2f5SFrançois Tigeot static struct intel_dvo *enc_to_dvo(struct intel_encoder *encoder)
113ba55f2f5SFrançois Tigeot {
114ba55f2f5SFrançois Tigeot 	return container_of(encoder, struct intel_dvo, base);
115ba55f2f5SFrançois Tigeot }
116ba55f2f5SFrançois Tigeot 
117ba55f2f5SFrançois Tigeot static struct intel_dvo *intel_attached_dvo(struct drm_connector *connector)
118ba55f2f5SFrançois Tigeot {
119ba55f2f5SFrançois Tigeot 	return enc_to_dvo(intel_attached_encoder(connector));
120ba55f2f5SFrançois Tigeot }
121ba55f2f5SFrançois Tigeot 
122ba55f2f5SFrançois Tigeot static bool intel_dvo_connector_get_hw_state(struct intel_connector *connector)
123ba55f2f5SFrançois Tigeot {
12424edb884SFrançois Tigeot 	struct drm_device *dev = connector->base.dev;
125303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
126ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(&connector->base);
12724edb884SFrançois Tigeot 	u32 tmp;
12824edb884SFrançois Tigeot 
12924edb884SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
13024edb884SFrançois Tigeot 
13124edb884SFrançois Tigeot 	if (!(tmp & DVO_ENABLE))
13224edb884SFrançois Tigeot 		return false;
133ba55f2f5SFrançois Tigeot 
134ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->get_hw_state(&intel_dvo->dev);
135ba55f2f5SFrançois Tigeot }
136ba55f2f5SFrançois Tigeot 
137ba55f2f5SFrançois Tigeot static bool intel_dvo_get_hw_state(struct intel_encoder *encoder,
138ba55f2f5SFrançois Tigeot 				   enum i915_pipe *pipe)
139ba55f2f5SFrançois Tigeot {
140ba55f2f5SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
141303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
142ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
143ba55f2f5SFrançois Tigeot 	u32 tmp;
144ba55f2f5SFrançois Tigeot 
145ba55f2f5SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
146ba55f2f5SFrançois Tigeot 
147ba55f2f5SFrançois Tigeot 	if (!(tmp & DVO_ENABLE))
148ba55f2f5SFrançois Tigeot 		return false;
149ba55f2f5SFrançois Tigeot 
150ba55f2f5SFrançois Tigeot 	*pipe = PORT_TO_PIPE(tmp);
151ba55f2f5SFrançois Tigeot 
152ba55f2f5SFrançois Tigeot 	return true;
153ba55f2f5SFrançois Tigeot }
154ba55f2f5SFrançois Tigeot 
155ba55f2f5SFrançois Tigeot static void intel_dvo_get_config(struct intel_encoder *encoder,
1562c9916cdSFrançois Tigeot 				 struct intel_crtc_state *pipe_config)
157ba55f2f5SFrançois Tigeot {
158303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
159ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
160ba55f2f5SFrançois Tigeot 	u32 tmp, flags = 0;
161ba55f2f5SFrançois Tigeot 
162ba55f2f5SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
163ba55f2f5SFrançois Tigeot 	if (tmp & DVO_HSYNC_ACTIVE_HIGH)
164ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PHSYNC;
165ba55f2f5SFrançois Tigeot 	else
166ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NHSYNC;
167ba55f2f5SFrançois Tigeot 	if (tmp & DVO_VSYNC_ACTIVE_HIGH)
168ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PVSYNC;
169ba55f2f5SFrançois Tigeot 	else
170ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NVSYNC;
171ba55f2f5SFrançois Tigeot 
1722c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= flags;
173ba55f2f5SFrançois Tigeot 
1742c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
175ba55f2f5SFrançois Tigeot }
176ba55f2f5SFrançois Tigeot 
177ba55f2f5SFrançois Tigeot static void intel_disable_dvo(struct intel_encoder *encoder)
178ba55f2f5SFrançois Tigeot {
179303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
180ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
181aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
182ba55f2f5SFrançois Tigeot 	u32 temp = I915_READ(dvo_reg);
183ba55f2f5SFrançois Tigeot 
184ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, false);
185ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
186ba55f2f5SFrançois Tigeot 	I915_READ(dvo_reg);
187ba55f2f5SFrançois Tigeot }
188ba55f2f5SFrançois Tigeot 
189ba55f2f5SFrançois Tigeot static void intel_enable_dvo(struct intel_encoder *encoder)
190ba55f2f5SFrançois Tigeot {
191303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
192ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
193ba55f2f5SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
194aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
195ba55f2f5SFrançois Tigeot 	u32 temp = I915_READ(dvo_reg);
196ba55f2f5SFrançois Tigeot 
197ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->mode_set(&intel_dvo->dev,
1982c9916cdSFrançois Tigeot 					 &crtc->config->base.mode,
1992c9916cdSFrançois Tigeot 					 &crtc->config->base.adjusted_mode);
200ba55f2f5SFrançois Tigeot 
2011b13d190SFrançois Tigeot 	I915_WRITE(dvo_reg, temp | DVO_ENABLE);
2021b13d190SFrançois Tigeot 	I915_READ(dvo_reg);
2031b13d190SFrançois Tigeot 
204ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, true);
205ba55f2f5SFrançois Tigeot }
206ba55f2f5SFrançois Tigeot 
207ba55f2f5SFrançois Tigeot static enum drm_mode_status
208ba55f2f5SFrançois Tigeot intel_dvo_mode_valid(struct drm_connector *connector,
209ba55f2f5SFrançois Tigeot 		     struct drm_display_mode *mode)
210ba55f2f5SFrançois Tigeot {
211ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
212352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
213352ff8bdSFrançois Tigeot 		to_intel_connector(connector)->panel.fixed_mode;
214352ff8bdSFrançois Tigeot 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
215352ff8bdSFrançois Tigeot 	int target_clock = mode->clock;
216ba55f2f5SFrançois Tigeot 
217ba55f2f5SFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
218ba55f2f5SFrançois Tigeot 		return MODE_NO_DBLESCAN;
219ba55f2f5SFrançois Tigeot 
220ba55f2f5SFrançois Tigeot 	/* XXX: Validate clock range */
221ba55f2f5SFrançois Tigeot 
222352ff8bdSFrançois Tigeot 	if (fixed_mode) {
223352ff8bdSFrançois Tigeot 		if (mode->hdisplay > fixed_mode->hdisplay)
224ba55f2f5SFrançois Tigeot 			return MODE_PANEL;
225352ff8bdSFrançois Tigeot 		if (mode->vdisplay > fixed_mode->vdisplay)
226ba55f2f5SFrançois Tigeot 			return MODE_PANEL;
227352ff8bdSFrançois Tigeot 
228352ff8bdSFrançois Tigeot 		target_clock = fixed_mode->clock;
229ba55f2f5SFrançois Tigeot 	}
230ba55f2f5SFrançois Tigeot 
231352ff8bdSFrançois Tigeot 	if (target_clock > max_dotclk)
232352ff8bdSFrançois Tigeot 		return MODE_CLOCK_HIGH;
233352ff8bdSFrançois Tigeot 
234ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->mode_valid(&intel_dvo->dev, mode);
235ba55f2f5SFrançois Tigeot }
236ba55f2f5SFrançois Tigeot 
237ba55f2f5SFrançois Tigeot static bool intel_dvo_compute_config(struct intel_encoder *encoder,
2382c9916cdSFrançois Tigeot 				     struct intel_crtc_state *pipe_config)
239ba55f2f5SFrançois Tigeot {
240ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
241352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
242352ff8bdSFrançois Tigeot 		intel_dvo->attached_connector->panel.fixed_mode;
2432c9916cdSFrançois Tigeot 	struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
244ba55f2f5SFrançois Tigeot 
245ba55f2f5SFrançois Tigeot 	/* If we have timings from the BIOS for the panel, put them in
246ba55f2f5SFrançois Tigeot 	 * to the adjusted mode.  The CRTC will be set up for this mode,
247ba55f2f5SFrançois Tigeot 	 * with the panel scaling set up to source from the H/VDisplay
248ba55f2f5SFrançois Tigeot 	 * of the original mode.
249ba55f2f5SFrançois Tigeot 	 */
250352ff8bdSFrançois Tigeot 	if (fixed_mode)
251352ff8bdSFrançois Tigeot 		intel_fixed_panel_mode(fixed_mode, adjusted_mode);
252ba55f2f5SFrançois Tigeot 
253ba55f2f5SFrançois Tigeot 	return true;
254ba55f2f5SFrançois Tigeot }
255ba55f2f5SFrançois Tigeot 
256ba55f2f5SFrançois Tigeot static void intel_dvo_pre_enable(struct intel_encoder *encoder)
257ba55f2f5SFrançois Tigeot {
258ba55f2f5SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
259303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
260ba55f2f5SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
261352ff8bdSFrançois Tigeot 	const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
262ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
263ba55f2f5SFrançois Tigeot 	int pipe = crtc->pipe;
264ba55f2f5SFrançois Tigeot 	u32 dvo_val;
265aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
266aee94f86SFrançois Tigeot 	i915_reg_t dvo_srcdim_reg = intel_dvo->dev.dvo_srcdim_reg;
267ba55f2f5SFrançois Tigeot 
268ba55f2f5SFrançois Tigeot 	/* Save the data order, since I don't know what it should be set to. */
269ba55f2f5SFrançois Tigeot 	dvo_val = I915_READ(dvo_reg) &
270ba55f2f5SFrançois Tigeot 		  (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
271ba55f2f5SFrançois Tigeot 	dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
272ba55f2f5SFrançois Tigeot 		   DVO_BLANK_ACTIVE_HIGH;
273ba55f2f5SFrançois Tigeot 
274ba55f2f5SFrançois Tigeot 	if (pipe == 1)
275ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_PIPE_B_SELECT;
276ba55f2f5SFrançois Tigeot 	dvo_val |= DVO_PIPE_STALL;
277ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
278ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
279ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
280ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
281ba55f2f5SFrançois Tigeot 
282ba55f2f5SFrançois Tigeot 	/*I915_WRITE(DVOB_SRCDIM,
283352ff8bdSFrançois Tigeot 	  (adjusted_mode->crtc_hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
284352ff8bdSFrançois Tigeot 	  (adjusted_mode->crtc_vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
285ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_srcdim_reg,
286352ff8bdSFrançois Tigeot 		   (adjusted_mode->crtc_hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
287352ff8bdSFrançois Tigeot 		   (adjusted_mode->crtc_vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
288ba55f2f5SFrançois Tigeot 	/*I915_WRITE(DVOB, dvo_val);*/
289ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_reg, dvo_val);
290ba55f2f5SFrançois Tigeot }
291ba55f2f5SFrançois Tigeot 
292ba55f2f5SFrançois Tigeot /**
293ba55f2f5SFrançois Tigeot  * Detect the output connection on our DVO device.
294ba55f2f5SFrançois Tigeot  *
295ba55f2f5SFrançois Tigeot  * Unimplemented.
296ba55f2f5SFrançois Tigeot  */
297ba55f2f5SFrançois Tigeot static enum drm_connector_status
298ba55f2f5SFrançois Tigeot intel_dvo_detect(struct drm_connector *connector, bool force)
299ba55f2f5SFrançois Tigeot {
300ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
301ba55f2f5SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
302ba55f2f5SFrançois Tigeot 		      connector->base.id, connector->name);
303ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->detect(&intel_dvo->dev);
304ba55f2f5SFrançois Tigeot }
305ba55f2f5SFrançois Tigeot 
306ba55f2f5SFrançois Tigeot static int intel_dvo_get_modes(struct drm_connector *connector)
307ba55f2f5SFrançois Tigeot {
308303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
309352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
310352ff8bdSFrançois Tigeot 		to_intel_connector(connector)->panel.fixed_mode;
311ba55f2f5SFrançois Tigeot 
312ba55f2f5SFrançois Tigeot 	/* We should probably have an i2c driver get_modes function for those
313ba55f2f5SFrançois Tigeot 	 * devices which will have a fixed set of modes determined by the chip
314ba55f2f5SFrançois Tigeot 	 * (TV-out, for example), but for now with just TMDS and LVDS,
315ba55f2f5SFrançois Tigeot 	 * that's not the case.
316ba55f2f5SFrançois Tigeot 	 */
317ba55f2f5SFrançois Tigeot 	intel_ddc_get_modes(connector,
31819c468b4SFrançois Tigeot 			    intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPC));
319ba55f2f5SFrançois Tigeot 	if (!list_empty(&connector->probed_modes))
320ba55f2f5SFrançois Tigeot 		return 1;
321ba55f2f5SFrançois Tigeot 
322352ff8bdSFrançois Tigeot 	if (fixed_mode) {
323ba55f2f5SFrançois Tigeot 		struct drm_display_mode *mode;
324352ff8bdSFrançois Tigeot 		mode = drm_mode_duplicate(connector->dev, fixed_mode);
325ba55f2f5SFrançois Tigeot 		if (mode) {
326ba55f2f5SFrançois Tigeot 			drm_mode_probed_add(connector, mode);
327ba55f2f5SFrançois Tigeot 			return 1;
328ba55f2f5SFrançois Tigeot 		}
329ba55f2f5SFrançois Tigeot 	}
330ba55f2f5SFrançois Tigeot 
331ba55f2f5SFrançois Tigeot 	return 0;
332ba55f2f5SFrançois Tigeot }
333ba55f2f5SFrançois Tigeot 
334ba55f2f5SFrançois Tigeot static void intel_dvo_destroy(struct drm_connector *connector)
335ba55f2f5SFrançois Tigeot {
336ba55f2f5SFrançois Tigeot 	drm_connector_cleanup(connector);
337352ff8bdSFrançois Tigeot 	intel_panel_fini(&to_intel_connector(connector)->panel);
338ba55f2f5SFrançois Tigeot 	kfree(connector);
339ba55f2f5SFrançois Tigeot }
340ba55f2f5SFrançois Tigeot 
341ba55f2f5SFrançois Tigeot static const struct drm_connector_funcs intel_dvo_connector_funcs = {
342a05eeebfSFrançois Tigeot 	.dpms = drm_atomic_helper_connector_dpms,
343ba55f2f5SFrançois Tigeot 	.detect = intel_dvo_detect,
3441487f786SFrançois Tigeot 	.late_register = intel_connector_register,
3451487f786SFrançois Tigeot 	.early_unregister = intel_connector_unregister,
346ba55f2f5SFrançois Tigeot 	.destroy = intel_dvo_destroy,
347ba55f2f5SFrançois Tigeot 	.fill_modes = drm_helper_probe_single_connector_modes,
3482c9916cdSFrançois Tigeot 	.atomic_get_property = intel_connector_atomic_get_property,
3492c9916cdSFrançois Tigeot 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
350477eb7f9SFrançois Tigeot 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
351ba55f2f5SFrançois Tigeot };
352ba55f2f5SFrançois Tigeot 
353ba55f2f5SFrançois Tigeot static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
354ba55f2f5SFrançois Tigeot 	.mode_valid = intel_dvo_mode_valid,
355ba55f2f5SFrançois Tigeot 	.get_modes = intel_dvo_get_modes,
356ba55f2f5SFrançois Tigeot };
357ba55f2f5SFrançois Tigeot 
358ba55f2f5SFrançois Tigeot static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
359ba55f2f5SFrançois Tigeot {
360ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(to_intel_encoder(encoder));
361ba55f2f5SFrançois Tigeot 
362ba55f2f5SFrançois Tigeot 	if (intel_dvo->dev.dev_ops->destroy)
363ba55f2f5SFrançois Tigeot 		intel_dvo->dev.dev_ops->destroy(&intel_dvo->dev);
364ba55f2f5SFrançois Tigeot 
365ba55f2f5SFrançois Tigeot 	intel_encoder_destroy(encoder);
366ba55f2f5SFrançois Tigeot }
367ba55f2f5SFrançois Tigeot 
368ba55f2f5SFrançois Tigeot static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
369ba55f2f5SFrançois Tigeot 	.destroy = intel_dvo_enc_destroy,
370ba55f2f5SFrançois Tigeot };
371ba55f2f5SFrançois Tigeot 
372ba55f2f5SFrançois Tigeot /**
373ba55f2f5SFrançois Tigeot  * Attempts to get a fixed panel timing for LVDS (currently only the i830).
374ba55f2f5SFrançois Tigeot  *
375ba55f2f5SFrançois Tigeot  * Other chips with DVO LVDS will need to extend this to deal with the LVDS
376ba55f2f5SFrançois Tigeot  * chip being on DVOB/C and having multiple pipes.
377ba55f2f5SFrançois Tigeot  */
378ba55f2f5SFrançois Tigeot static struct drm_display_mode *
379ba55f2f5SFrançois Tigeot intel_dvo_get_current_mode(struct drm_connector *connector)
380ba55f2f5SFrançois Tigeot {
381ba55f2f5SFrançois Tigeot 	struct drm_device *dev = connector->dev;
382303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
383ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
384ba55f2f5SFrançois Tigeot 	uint32_t dvo_val = I915_READ(intel_dvo->dev.dvo_reg);
385ba55f2f5SFrançois Tigeot 	struct drm_display_mode *mode = NULL;
386ba55f2f5SFrançois Tigeot 
387ba55f2f5SFrançois Tigeot 	/* If the DVO port is active, that'll be the LVDS, so we can pull out
388ba55f2f5SFrançois Tigeot 	 * its timings to get how the BIOS set up the panel.
389ba55f2f5SFrançois Tigeot 	 */
390ba55f2f5SFrançois Tigeot 	if (dvo_val & DVO_ENABLE) {
391ba55f2f5SFrançois Tigeot 		struct drm_crtc *crtc;
392ba55f2f5SFrançois Tigeot 		int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
393ba55f2f5SFrançois Tigeot 
394ba55f2f5SFrançois Tigeot 		crtc = intel_get_crtc_for_pipe(dev, pipe);
395ba55f2f5SFrançois Tigeot 		if (crtc) {
396ba55f2f5SFrançois Tigeot 			mode = intel_crtc_mode_get(dev, crtc);
397ba55f2f5SFrançois Tigeot 			if (mode) {
398ba55f2f5SFrançois Tigeot 				mode->type |= DRM_MODE_TYPE_PREFERRED;
399ba55f2f5SFrançois Tigeot 				if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
400ba55f2f5SFrançois Tigeot 					mode->flags |= DRM_MODE_FLAG_PHSYNC;
401ba55f2f5SFrançois Tigeot 				if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
402ba55f2f5SFrançois Tigeot 					mode->flags |= DRM_MODE_FLAG_PVSYNC;
403ba55f2f5SFrançois Tigeot 			}
404ba55f2f5SFrançois Tigeot 		}
405ba55f2f5SFrançois Tigeot 	}
406ba55f2f5SFrançois Tigeot 
407ba55f2f5SFrançois Tigeot 	return mode;
408ba55f2f5SFrançois Tigeot }
409ba55f2f5SFrançois Tigeot 
4101487f786SFrançois Tigeot static char intel_dvo_port_name(i915_reg_t dvo_reg)
4111487f786SFrançois Tigeot {
4121487f786SFrançois Tigeot 	if (i915_mmio_reg_equal(dvo_reg, DVOA))
4131487f786SFrançois Tigeot 		return 'A';
4141487f786SFrançois Tigeot 	else if (i915_mmio_reg_equal(dvo_reg, DVOB))
4151487f786SFrançois Tigeot 		return 'B';
4161487f786SFrançois Tigeot 	else if (i915_mmio_reg_equal(dvo_reg, DVOC))
4171487f786SFrançois Tigeot 		return 'C';
4181487f786SFrançois Tigeot 	else
4191487f786SFrançois Tigeot 		return '?';
4201487f786SFrançois Tigeot }
4211487f786SFrançois Tigeot 
422ba55f2f5SFrançois Tigeot void intel_dvo_init(struct drm_device *dev)
423ba55f2f5SFrançois Tigeot {
424303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
425ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder;
426ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo;
427ba55f2f5SFrançois Tigeot 	struct intel_connector *intel_connector;
428ba55f2f5SFrançois Tigeot 	int i;
429ba55f2f5SFrançois Tigeot 	int encoder_type = DRM_MODE_ENCODER_NONE;
430ba55f2f5SFrançois Tigeot 
431ba55f2f5SFrançois Tigeot 	intel_dvo = kzalloc(sizeof(*intel_dvo), GFP_KERNEL);
432ba55f2f5SFrançois Tigeot 	if (!intel_dvo)
433ba55f2f5SFrançois Tigeot 		return;
434ba55f2f5SFrançois Tigeot 
435477eb7f9SFrançois Tigeot 	intel_connector = intel_connector_alloc();
436ba55f2f5SFrançois Tigeot 	if (!intel_connector) {
437ba55f2f5SFrançois Tigeot 		kfree(intel_dvo);
438ba55f2f5SFrançois Tigeot 		return;
439ba55f2f5SFrançois Tigeot 	}
440ba55f2f5SFrançois Tigeot 
441352ff8bdSFrançois Tigeot 	intel_dvo->attached_connector = intel_connector;
442352ff8bdSFrançois Tigeot 
443ba55f2f5SFrançois Tigeot 	intel_encoder = &intel_dvo->base;
444ba55f2f5SFrançois Tigeot 
445ba55f2f5SFrançois Tigeot 	intel_encoder->disable = intel_disable_dvo;
446ba55f2f5SFrançois Tigeot 	intel_encoder->enable = intel_enable_dvo;
447ba55f2f5SFrançois Tigeot 	intel_encoder->get_hw_state = intel_dvo_get_hw_state;
448ba55f2f5SFrançois Tigeot 	intel_encoder->get_config = intel_dvo_get_config;
449ba55f2f5SFrançois Tigeot 	intel_encoder->compute_config = intel_dvo_compute_config;
450ba55f2f5SFrançois Tigeot 	intel_encoder->pre_enable = intel_dvo_pre_enable;
451ba55f2f5SFrançois Tigeot 	intel_connector->get_hw_state = intel_dvo_connector_get_hw_state;
452ba55f2f5SFrançois Tigeot 
453ba55f2f5SFrançois Tigeot 	/* Now, try to find a controller */
454ba55f2f5SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
455ba55f2f5SFrançois Tigeot 		struct drm_connector *connector = &intel_connector->base;
456ba55f2f5SFrançois Tigeot 		const struct intel_dvo_device *dvo = &intel_dvo_devices[i];
4575d302545SFrançois Tigeot 		struct i2c_adapter *i2c;
458ba55f2f5SFrançois Tigeot 		int gpio;
459ba55f2f5SFrançois Tigeot 		bool dvoinit;
46019c468b4SFrançois Tigeot 		enum i915_pipe pipe;
46119c468b4SFrançois Tigeot 		uint32_t dpll[I915_MAX_PIPES];
462ba55f2f5SFrançois Tigeot 
463ba55f2f5SFrançois Tigeot 		/* Allow the I2C driver info to specify the GPIO to be used in
464ba55f2f5SFrançois Tigeot 		 * special cases, but otherwise default to what's defined
465ba55f2f5SFrançois Tigeot 		 * in the spec.
466ba55f2f5SFrançois Tigeot 		 */
46719c468b4SFrançois Tigeot 		if (intel_gmbus_is_valid_pin(dev_priv, dvo->gpio))
468ba55f2f5SFrançois Tigeot 			gpio = dvo->gpio;
469ba55f2f5SFrançois Tigeot 		else if (dvo->type == INTEL_DVO_CHIP_LVDS)
47019c468b4SFrançois Tigeot 			gpio = GMBUS_PIN_SSC;
471ba55f2f5SFrançois Tigeot 		else
47219c468b4SFrançois Tigeot 			gpio = GMBUS_PIN_DPB;
473ba55f2f5SFrançois Tigeot 
474ba55f2f5SFrançois Tigeot 		/* Set up the I2C bus necessary for the chip we're probing.
475ba55f2f5SFrançois Tigeot 		 * It appears that everything is on GPIOE except for panels
476ba55f2f5SFrançois Tigeot 		 * on i830 laptops, which are on GPIOB (DVOA).
477ba55f2f5SFrançois Tigeot 		 */
478ba55f2f5SFrançois Tigeot 		i2c = intel_gmbus_get_adapter(dev_priv, gpio);
479ba55f2f5SFrançois Tigeot 
480ba55f2f5SFrançois Tigeot 		intel_dvo->dev = *dvo;
481ba55f2f5SFrançois Tigeot 
482ba55f2f5SFrançois Tigeot 		/* GMBUS NAK handling seems to be unstable, hence let the
483ba55f2f5SFrançois Tigeot 		 * transmitter detection run in bit banging mode for now.
484ba55f2f5SFrançois Tigeot 		 */
485ba55f2f5SFrançois Tigeot 		intel_gmbus_force_bit(i2c, true);
486ba55f2f5SFrançois Tigeot 
48719c468b4SFrançois Tigeot 		/* ns2501 requires the DVO 2x clock before it will
48819c468b4SFrançois Tigeot 		 * respond to i2c accesses, so make sure we have
48919c468b4SFrançois Tigeot 		 * have the clock enabled before we attempt to
49019c468b4SFrançois Tigeot 		 * initialize the device.
49119c468b4SFrançois Tigeot 		 */
49219c468b4SFrançois Tigeot 		for_each_pipe(dev_priv, pipe) {
49319c468b4SFrançois Tigeot 			dpll[pipe] = I915_READ(DPLL(pipe));
49419c468b4SFrançois Tigeot 			I915_WRITE(DPLL(pipe), dpll[pipe] | DPLL_DVO_2X_MODE);
49519c468b4SFrançois Tigeot 		}
49619c468b4SFrançois Tigeot 
497ba55f2f5SFrançois Tigeot 		dvoinit = dvo->dev_ops->init(&intel_dvo->dev, i2c);
498ba55f2f5SFrançois Tigeot 
49919c468b4SFrançois Tigeot 		/* restore the DVO 2x clock state to original */
50019c468b4SFrançois Tigeot 		for_each_pipe(dev_priv, pipe) {
50119c468b4SFrançois Tigeot 			I915_WRITE(DPLL(pipe), dpll[pipe]);
50219c468b4SFrançois Tigeot 		}
50319c468b4SFrançois Tigeot 
504ba55f2f5SFrançois Tigeot 		intel_gmbus_force_bit(i2c, false);
505ba55f2f5SFrançois Tigeot 
506ba55f2f5SFrançois Tigeot 		if (!dvoinit)
507ba55f2f5SFrançois Tigeot 			continue;
508ba55f2f5SFrançois Tigeot 
5091487f786SFrançois Tigeot 		drm_encoder_init(dev, &intel_encoder->base,
5101487f786SFrançois Tigeot 				 &intel_dvo_enc_funcs, encoder_type,
5111487f786SFrançois Tigeot 				 "DVO %c", intel_dvo_port_name(dvo->dvo_reg));
5121487f786SFrançois Tigeot 
513ba55f2f5SFrançois Tigeot 		intel_encoder->type = INTEL_OUTPUT_DVO;
514ba55f2f5SFrançois Tigeot 		intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
515ba55f2f5SFrançois Tigeot 		switch (dvo->type) {
516ba55f2f5SFrançois Tigeot 		case INTEL_DVO_CHIP_TMDS:
517ba55f2f5SFrançois Tigeot 			intel_encoder->cloneable = (1 << INTEL_OUTPUT_ANALOG) |
518ba55f2f5SFrançois Tigeot 				(1 << INTEL_OUTPUT_DVO);
519ba55f2f5SFrançois Tigeot 			drm_connector_init(dev, connector,
520ba55f2f5SFrançois Tigeot 					   &intel_dvo_connector_funcs,
521ba55f2f5SFrançois Tigeot 					   DRM_MODE_CONNECTOR_DVII);
522ba55f2f5SFrançois Tigeot 			encoder_type = DRM_MODE_ENCODER_TMDS;
523ba55f2f5SFrançois Tigeot 			break;
524ba55f2f5SFrançois Tigeot 		case INTEL_DVO_CHIP_LVDS:
525ba55f2f5SFrançois Tigeot 			intel_encoder->cloneable = 0;
526ba55f2f5SFrançois Tigeot 			drm_connector_init(dev, connector,
527ba55f2f5SFrançois Tigeot 					   &intel_dvo_connector_funcs,
528ba55f2f5SFrançois Tigeot 					   DRM_MODE_CONNECTOR_LVDS);
529ba55f2f5SFrançois Tigeot 			encoder_type = DRM_MODE_ENCODER_LVDS;
530ba55f2f5SFrançois Tigeot 			break;
531ba55f2f5SFrançois Tigeot 		}
532ba55f2f5SFrançois Tigeot 
533ba55f2f5SFrançois Tigeot 		drm_connector_helper_add(connector,
534ba55f2f5SFrançois Tigeot 					 &intel_dvo_connector_helper_funcs);
535ba55f2f5SFrançois Tigeot 		connector->display_info.subpixel_order = SubPixelHorizontalRGB;
536ba55f2f5SFrançois Tigeot 		connector->interlace_allowed = false;
537ba55f2f5SFrançois Tigeot 		connector->doublescan_allowed = false;
538ba55f2f5SFrançois Tigeot 
539ba55f2f5SFrançois Tigeot 		intel_connector_attach_encoder(intel_connector, intel_encoder);
540ba55f2f5SFrançois Tigeot 		if (dvo->type == INTEL_DVO_CHIP_LVDS) {
541ba55f2f5SFrançois Tigeot 			/* For our LVDS chipsets, we should hopefully be able
542ba55f2f5SFrançois Tigeot 			 * to dig the fixed panel mode out of the BIOS data.
543ba55f2f5SFrançois Tigeot 			 * However, it's in a different format from the BIOS
544ba55f2f5SFrançois Tigeot 			 * data on chipsets with integrated LVDS (stored in AIM
545ba55f2f5SFrançois Tigeot 			 * headers, likely), so for now, just get the current
546ba55f2f5SFrançois Tigeot 			 * mode being output through DVO.
547ba55f2f5SFrançois Tigeot 			 */
548352ff8bdSFrançois Tigeot 			intel_panel_init(&intel_connector->panel,
549352ff8bdSFrançois Tigeot 					 intel_dvo_get_current_mode(connector),
550352ff8bdSFrançois Tigeot 					 NULL);
551ba55f2f5SFrançois Tigeot 			intel_dvo->panel_wants_dither = true;
552ba55f2f5SFrançois Tigeot 		}
553ba55f2f5SFrançois Tigeot 
554ba55f2f5SFrançois Tigeot 		return;
555ba55f2f5SFrançois Tigeot 	}
556ba55f2f5SFrançois Tigeot 
557ba55f2f5SFrançois Tigeot 	kfree(intel_dvo);
558ba55f2f5SFrançois Tigeot 	kfree(intel_connector);
559ba55f2f5SFrançois Tigeot }
560