xref: /dflybsd-src/sys/dev/drm/i915/intel_dvo.c (revision aee94f86171368465eaa15d649743f13cea3363a)
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>
28ba55f2f5SFrançois Tigeot #include <drm/drmP.h>
292c9916cdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
30ba55f2f5SFrançois Tigeot #include <drm/drm_crtc.h>
31ba55f2f5SFrançois Tigeot #include "intel_drv.h"
32ba55f2f5SFrançois Tigeot #include <drm/i915_drm.h>
33ba55f2f5SFrançois Tigeot #include "i915_drv.h"
34ba55f2f5SFrançois Tigeot #include "dvo.h"
35ba55f2f5SFrançois Tigeot 
36ba55f2f5SFrançois Tigeot #define SIL164_ADDR	0x38
37ba55f2f5SFrançois Tigeot #define CH7xxx_ADDR	0x76
38ba55f2f5SFrançois Tigeot #define TFP410_ADDR	0x38
39ba55f2f5SFrançois Tigeot #define NS2501_ADDR     0x38
40ba55f2f5SFrançois Tigeot 
41ba55f2f5SFrançois Tigeot static const struct intel_dvo_device intel_dvo_devices[] = {
42ba55f2f5SFrançois Tigeot 	{
43ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
44ba55f2f5SFrançois Tigeot 		.name = "sil164",
45ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
46*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
47ba55f2f5SFrançois Tigeot 		.slave_addr = SIL164_ADDR,
48ba55f2f5SFrançois Tigeot 		.dev_ops = &sil164_ops,
49ba55f2f5SFrançois Tigeot 	},
50ba55f2f5SFrançois Tigeot 	{
51ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
52ba55f2f5SFrançois Tigeot 		.name = "ch7xxx",
53ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
54*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
55ba55f2f5SFrançois Tigeot 		.slave_addr = CH7xxx_ADDR,
56ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7xxx_ops,
57ba55f2f5SFrançois Tigeot 	},
58ba55f2f5SFrançois Tigeot 	{
59ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
60ba55f2f5SFrançois Tigeot 		.name = "ch7xxx",
61ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
62*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
63ba55f2f5SFrançois Tigeot 		.slave_addr = 0x75, /* For some ch7010 */
64ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7xxx_ops,
65ba55f2f5SFrançois Tigeot 	},
66ba55f2f5SFrançois Tigeot 	{
67ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_LVDS,
68ba55f2f5SFrançois Tigeot 		.name = "ivch",
69ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOA,
70*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOA_SRCDIM,
71ba55f2f5SFrançois Tigeot 		.slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
72ba55f2f5SFrançois Tigeot 		.dev_ops = &ivch_ops,
73ba55f2f5SFrançois Tigeot 	},
74ba55f2f5SFrançois Tigeot 	{
75ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_TMDS,
76ba55f2f5SFrançois Tigeot 		.name = "tfp410",
77ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
78*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
79ba55f2f5SFrançois Tigeot 		.slave_addr = TFP410_ADDR,
80ba55f2f5SFrançois Tigeot 		.dev_ops = &tfp410_ops,
81ba55f2f5SFrançois Tigeot 	},
82ba55f2f5SFrançois Tigeot 	{
83ba55f2f5SFrançois Tigeot 		.type = INTEL_DVO_CHIP_LVDS,
84ba55f2f5SFrançois Tigeot 		.name = "ch7017",
85ba55f2f5SFrançois Tigeot 		.dvo_reg = DVOC,
86*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOC_SRCDIM,
87ba55f2f5SFrançois Tigeot 		.slave_addr = 0x75,
8819c468b4SFrançois Tigeot 		.gpio = GMBUS_PIN_DPB,
89ba55f2f5SFrançois Tigeot 		.dev_ops = &ch7017_ops,
90ba55f2f5SFrançois Tigeot 	},
91ba55f2f5SFrançois Tigeot 	{
92ba55f2f5SFrançois Tigeot 	        .type = INTEL_DVO_CHIP_TMDS,
93ba55f2f5SFrançois Tigeot 		.name = "ns2501",
941b13d190SFrançois Tigeot 		.dvo_reg = DVOB,
95*aee94f86SFrançois Tigeot 		.dvo_srcdim_reg = DVOB_SRCDIM,
96ba55f2f5SFrançois Tigeot 		.slave_addr = NS2501_ADDR,
97ba55f2f5SFrançois Tigeot 		.dev_ops = &ns2501_ops,
98ba55f2f5SFrançois Tigeot        }
99ba55f2f5SFrançois Tigeot };
100ba55f2f5SFrançois Tigeot 
101ba55f2f5SFrançois Tigeot struct intel_dvo {
102ba55f2f5SFrançois Tigeot 	struct intel_encoder base;
103ba55f2f5SFrançois Tigeot 
104ba55f2f5SFrançois Tigeot 	struct intel_dvo_device dev;
105ba55f2f5SFrançois Tigeot 
106352ff8bdSFrançois Tigeot 	struct intel_connector *attached_connector;
107352ff8bdSFrançois Tigeot 
108ba55f2f5SFrançois Tigeot 	bool panel_wants_dither;
109ba55f2f5SFrançois Tigeot };
110ba55f2f5SFrançois Tigeot 
111ba55f2f5SFrançois Tigeot static struct intel_dvo *enc_to_dvo(struct intel_encoder *encoder)
112ba55f2f5SFrançois Tigeot {
113ba55f2f5SFrançois Tigeot 	return container_of(encoder, struct intel_dvo, base);
114ba55f2f5SFrançois Tigeot }
115ba55f2f5SFrançois Tigeot 
116ba55f2f5SFrançois Tigeot static struct intel_dvo *intel_attached_dvo(struct drm_connector *connector)
117ba55f2f5SFrançois Tigeot {
118ba55f2f5SFrançois Tigeot 	return enc_to_dvo(intel_attached_encoder(connector));
119ba55f2f5SFrançois Tigeot }
120ba55f2f5SFrançois Tigeot 
121ba55f2f5SFrançois Tigeot static bool intel_dvo_connector_get_hw_state(struct intel_connector *connector)
122ba55f2f5SFrançois Tigeot {
12324edb884SFrançois Tigeot 	struct drm_device *dev = connector->base.dev;
12424edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
125ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(&connector->base);
12624edb884SFrançois Tigeot 	u32 tmp;
12724edb884SFrançois Tigeot 
12824edb884SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
12924edb884SFrançois Tigeot 
13024edb884SFrançois Tigeot 	if (!(tmp & DVO_ENABLE))
13124edb884SFrançois Tigeot 		return false;
132ba55f2f5SFrançois Tigeot 
133ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->get_hw_state(&intel_dvo->dev);
134ba55f2f5SFrançois Tigeot }
135ba55f2f5SFrançois Tigeot 
136ba55f2f5SFrançois Tigeot static bool intel_dvo_get_hw_state(struct intel_encoder *encoder,
137ba55f2f5SFrançois Tigeot 				   enum i915_pipe *pipe)
138ba55f2f5SFrançois Tigeot {
139ba55f2f5SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
140ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
141ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
142ba55f2f5SFrançois Tigeot 	u32 tmp;
143ba55f2f5SFrançois Tigeot 
144ba55f2f5SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
145ba55f2f5SFrançois Tigeot 
146ba55f2f5SFrançois Tigeot 	if (!(tmp & DVO_ENABLE))
147ba55f2f5SFrançois Tigeot 		return false;
148ba55f2f5SFrançois Tigeot 
149ba55f2f5SFrançois Tigeot 	*pipe = PORT_TO_PIPE(tmp);
150ba55f2f5SFrançois Tigeot 
151ba55f2f5SFrançois Tigeot 	return true;
152ba55f2f5SFrançois Tigeot }
153ba55f2f5SFrançois Tigeot 
154ba55f2f5SFrançois Tigeot static void intel_dvo_get_config(struct intel_encoder *encoder,
1552c9916cdSFrançois Tigeot 				 struct intel_crtc_state *pipe_config)
156ba55f2f5SFrançois Tigeot {
157ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
158ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
159ba55f2f5SFrançois Tigeot 	u32 tmp, flags = 0;
160ba55f2f5SFrançois Tigeot 
161ba55f2f5SFrançois Tigeot 	tmp = I915_READ(intel_dvo->dev.dvo_reg);
162ba55f2f5SFrançois Tigeot 	if (tmp & DVO_HSYNC_ACTIVE_HIGH)
163ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PHSYNC;
164ba55f2f5SFrançois Tigeot 	else
165ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NHSYNC;
166ba55f2f5SFrançois Tigeot 	if (tmp & DVO_VSYNC_ACTIVE_HIGH)
167ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_PVSYNC;
168ba55f2f5SFrançois Tigeot 	else
169ba55f2f5SFrançois Tigeot 		flags |= DRM_MODE_FLAG_NVSYNC;
170ba55f2f5SFrançois Tigeot 
1712c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.flags |= flags;
172ba55f2f5SFrançois Tigeot 
1732c9916cdSFrançois Tigeot 	pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
174ba55f2f5SFrançois Tigeot }
175ba55f2f5SFrançois Tigeot 
176ba55f2f5SFrançois Tigeot static void intel_disable_dvo(struct intel_encoder *encoder)
177ba55f2f5SFrançois Tigeot {
178ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
179ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
180*aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
181ba55f2f5SFrançois Tigeot 	u32 temp = I915_READ(dvo_reg);
182ba55f2f5SFrançois Tigeot 
183ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, false);
184ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
185ba55f2f5SFrançois Tigeot 	I915_READ(dvo_reg);
186ba55f2f5SFrançois Tigeot }
187ba55f2f5SFrançois Tigeot 
188ba55f2f5SFrançois Tigeot static void intel_enable_dvo(struct intel_encoder *encoder)
189ba55f2f5SFrançois Tigeot {
190ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
191ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
192ba55f2f5SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
193*aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
194ba55f2f5SFrançois Tigeot 	u32 temp = I915_READ(dvo_reg);
195ba55f2f5SFrançois Tigeot 
196ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->mode_set(&intel_dvo->dev,
1972c9916cdSFrançois Tigeot 					 &crtc->config->base.mode,
1982c9916cdSFrançois Tigeot 					 &crtc->config->base.adjusted_mode);
199ba55f2f5SFrançois Tigeot 
2001b13d190SFrançois Tigeot 	I915_WRITE(dvo_reg, temp | DVO_ENABLE);
2011b13d190SFrançois Tigeot 	I915_READ(dvo_reg);
2021b13d190SFrançois Tigeot 
203ba55f2f5SFrançois Tigeot 	intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, true);
204ba55f2f5SFrançois Tigeot }
205ba55f2f5SFrançois Tigeot 
206ba55f2f5SFrançois Tigeot static enum drm_mode_status
207ba55f2f5SFrançois Tigeot intel_dvo_mode_valid(struct drm_connector *connector,
208ba55f2f5SFrançois Tigeot 		     struct drm_display_mode *mode)
209ba55f2f5SFrançois Tigeot {
210ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
211352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
212352ff8bdSFrançois Tigeot 		to_intel_connector(connector)->panel.fixed_mode;
213352ff8bdSFrançois Tigeot 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
214352ff8bdSFrançois Tigeot 	int target_clock = mode->clock;
215ba55f2f5SFrançois Tigeot 
216ba55f2f5SFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
217ba55f2f5SFrançois Tigeot 		return MODE_NO_DBLESCAN;
218ba55f2f5SFrançois Tigeot 
219ba55f2f5SFrançois Tigeot 	/* XXX: Validate clock range */
220ba55f2f5SFrançois Tigeot 
221352ff8bdSFrançois Tigeot 	if (fixed_mode) {
222352ff8bdSFrançois Tigeot 		if (mode->hdisplay > fixed_mode->hdisplay)
223ba55f2f5SFrançois Tigeot 			return MODE_PANEL;
224352ff8bdSFrançois Tigeot 		if (mode->vdisplay > fixed_mode->vdisplay)
225ba55f2f5SFrançois Tigeot 			return MODE_PANEL;
226352ff8bdSFrançois Tigeot 
227352ff8bdSFrançois Tigeot 		target_clock = fixed_mode->clock;
228ba55f2f5SFrançois Tigeot 	}
229ba55f2f5SFrançois Tigeot 
230352ff8bdSFrançois Tigeot 	if (target_clock > max_dotclk)
231352ff8bdSFrançois Tigeot 		return MODE_CLOCK_HIGH;
232352ff8bdSFrançois Tigeot 
233ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->mode_valid(&intel_dvo->dev, mode);
234ba55f2f5SFrançois Tigeot }
235ba55f2f5SFrançois Tigeot 
236ba55f2f5SFrançois Tigeot static bool intel_dvo_compute_config(struct intel_encoder *encoder,
2372c9916cdSFrançois Tigeot 				     struct intel_crtc_state *pipe_config)
238ba55f2f5SFrançois Tigeot {
239ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
240352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
241352ff8bdSFrançois Tigeot 		intel_dvo->attached_connector->panel.fixed_mode;
2422c9916cdSFrançois Tigeot 	struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
243ba55f2f5SFrançois Tigeot 
244ba55f2f5SFrançois Tigeot 	/* If we have timings from the BIOS for the panel, put them in
245ba55f2f5SFrançois Tigeot 	 * to the adjusted mode.  The CRTC will be set up for this mode,
246ba55f2f5SFrançois Tigeot 	 * with the panel scaling set up to source from the H/VDisplay
247ba55f2f5SFrançois Tigeot 	 * of the original mode.
248ba55f2f5SFrançois Tigeot 	 */
249352ff8bdSFrançois Tigeot 	if (fixed_mode)
250352ff8bdSFrançois Tigeot 		intel_fixed_panel_mode(fixed_mode, adjusted_mode);
251ba55f2f5SFrançois Tigeot 
252ba55f2f5SFrançois Tigeot 	return true;
253ba55f2f5SFrançois Tigeot }
254ba55f2f5SFrançois Tigeot 
255ba55f2f5SFrançois Tigeot static void intel_dvo_pre_enable(struct intel_encoder *encoder)
256ba55f2f5SFrançois Tigeot {
257ba55f2f5SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
258ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
259ba55f2f5SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
260352ff8bdSFrançois Tigeot 	const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
261ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
262ba55f2f5SFrançois Tigeot 	int pipe = crtc->pipe;
263ba55f2f5SFrançois Tigeot 	u32 dvo_val;
264*aee94f86SFrançois Tigeot 	i915_reg_t dvo_reg = intel_dvo->dev.dvo_reg;
265*aee94f86SFrançois Tigeot 	i915_reg_t dvo_srcdim_reg = intel_dvo->dev.dvo_srcdim_reg;
266ba55f2f5SFrançois Tigeot 
267ba55f2f5SFrançois Tigeot 	/* Save the data order, since I don't know what it should be set to. */
268ba55f2f5SFrançois Tigeot 	dvo_val = I915_READ(dvo_reg) &
269ba55f2f5SFrançois Tigeot 		  (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
270ba55f2f5SFrançois Tigeot 	dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
271ba55f2f5SFrançois Tigeot 		   DVO_BLANK_ACTIVE_HIGH;
272ba55f2f5SFrançois Tigeot 
273ba55f2f5SFrançois Tigeot 	if (pipe == 1)
274ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_PIPE_B_SELECT;
275ba55f2f5SFrançois Tigeot 	dvo_val |= DVO_PIPE_STALL;
276ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
277ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
278ba55f2f5SFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
279ba55f2f5SFrançois Tigeot 		dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
280ba55f2f5SFrançois Tigeot 
281ba55f2f5SFrançois Tigeot 	/*I915_WRITE(DVOB_SRCDIM,
282352ff8bdSFrançois Tigeot 	  (adjusted_mode->crtc_hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
283352ff8bdSFrançois Tigeot 	  (adjusted_mode->crtc_vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
284ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_srcdim_reg,
285352ff8bdSFrançois Tigeot 		   (adjusted_mode->crtc_hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
286352ff8bdSFrançois Tigeot 		   (adjusted_mode->crtc_vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
287ba55f2f5SFrançois Tigeot 	/*I915_WRITE(DVOB, dvo_val);*/
288ba55f2f5SFrançois Tigeot 	I915_WRITE(dvo_reg, dvo_val);
289ba55f2f5SFrançois Tigeot }
290ba55f2f5SFrançois Tigeot 
291ba55f2f5SFrançois Tigeot /**
292ba55f2f5SFrançois Tigeot  * Detect the output connection on our DVO device.
293ba55f2f5SFrançois Tigeot  *
294ba55f2f5SFrançois Tigeot  * Unimplemented.
295ba55f2f5SFrançois Tigeot  */
296ba55f2f5SFrançois Tigeot static enum drm_connector_status
297ba55f2f5SFrançois Tigeot intel_dvo_detect(struct drm_connector *connector, bool force)
298ba55f2f5SFrançois Tigeot {
299ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
300ba55f2f5SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
301ba55f2f5SFrançois Tigeot 		      connector->base.id, connector->name);
302ba55f2f5SFrançois Tigeot 	return intel_dvo->dev.dev_ops->detect(&intel_dvo->dev);
303ba55f2f5SFrançois Tigeot }
304ba55f2f5SFrançois Tigeot 
305ba55f2f5SFrançois Tigeot static int intel_dvo_get_modes(struct drm_connector *connector)
306ba55f2f5SFrançois Tigeot {
307ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = connector->dev->dev_private;
308352ff8bdSFrançois Tigeot 	const struct drm_display_mode *fixed_mode =
309352ff8bdSFrançois Tigeot 		to_intel_connector(connector)->panel.fixed_mode;
310ba55f2f5SFrançois Tigeot 
311ba55f2f5SFrançois Tigeot 	/* We should probably have an i2c driver get_modes function for those
312ba55f2f5SFrançois Tigeot 	 * devices which will have a fixed set of modes determined by the chip
313ba55f2f5SFrançois Tigeot 	 * (TV-out, for example), but for now with just TMDS and LVDS,
314ba55f2f5SFrançois Tigeot 	 * that's not the case.
315ba55f2f5SFrançois Tigeot 	 */
316ba55f2f5SFrançois Tigeot 	intel_ddc_get_modes(connector,
31719c468b4SFrançois Tigeot 			    intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPC));
318ba55f2f5SFrançois Tigeot 	if (!list_empty(&connector->probed_modes))
319ba55f2f5SFrançois Tigeot 		return 1;
320ba55f2f5SFrançois Tigeot 
321352ff8bdSFrançois Tigeot 	if (fixed_mode) {
322ba55f2f5SFrançois Tigeot 		struct drm_display_mode *mode;
323352ff8bdSFrançois Tigeot 		mode = drm_mode_duplicate(connector->dev, fixed_mode);
324ba55f2f5SFrançois Tigeot 		if (mode) {
325ba55f2f5SFrançois Tigeot 			drm_mode_probed_add(connector, mode);
326ba55f2f5SFrançois Tigeot 			return 1;
327ba55f2f5SFrançois Tigeot 		}
328ba55f2f5SFrançois Tigeot 	}
329ba55f2f5SFrançois Tigeot 
330ba55f2f5SFrançois Tigeot 	return 0;
331ba55f2f5SFrançois Tigeot }
332ba55f2f5SFrançois Tigeot 
333ba55f2f5SFrançois Tigeot static void intel_dvo_destroy(struct drm_connector *connector)
334ba55f2f5SFrançois Tigeot {
335ba55f2f5SFrançois Tigeot 	drm_connector_cleanup(connector);
336352ff8bdSFrançois Tigeot 	intel_panel_fini(&to_intel_connector(connector)->panel);
337ba55f2f5SFrançois Tigeot 	kfree(connector);
338ba55f2f5SFrançois Tigeot }
339ba55f2f5SFrançois Tigeot 
340ba55f2f5SFrançois Tigeot static const struct drm_connector_funcs intel_dvo_connector_funcs = {
341a05eeebfSFrançois Tigeot 	.dpms = drm_atomic_helper_connector_dpms,
342ba55f2f5SFrançois Tigeot 	.detect = intel_dvo_detect,
343ba55f2f5SFrançois Tigeot 	.destroy = intel_dvo_destroy,
344ba55f2f5SFrançois Tigeot 	.fill_modes = drm_helper_probe_single_connector_modes,
3452c9916cdSFrançois Tigeot 	.atomic_get_property = intel_connector_atomic_get_property,
3462c9916cdSFrançois Tigeot 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
347477eb7f9SFrançois Tigeot 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
348ba55f2f5SFrançois Tigeot };
349ba55f2f5SFrançois Tigeot 
350ba55f2f5SFrançois Tigeot static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
351ba55f2f5SFrançois Tigeot 	.mode_valid = intel_dvo_mode_valid,
352ba55f2f5SFrançois Tigeot 	.get_modes = intel_dvo_get_modes,
353ba55f2f5SFrançois Tigeot 	.best_encoder = intel_best_encoder,
354ba55f2f5SFrançois Tigeot };
355ba55f2f5SFrançois Tigeot 
356ba55f2f5SFrançois Tigeot static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
357ba55f2f5SFrançois Tigeot {
358ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = enc_to_dvo(to_intel_encoder(encoder));
359ba55f2f5SFrançois Tigeot 
360ba55f2f5SFrançois Tigeot 	if (intel_dvo->dev.dev_ops->destroy)
361ba55f2f5SFrançois Tigeot 		intel_dvo->dev.dev_ops->destroy(&intel_dvo->dev);
362ba55f2f5SFrançois Tigeot 
363ba55f2f5SFrançois Tigeot 	intel_encoder_destroy(encoder);
364ba55f2f5SFrançois Tigeot }
365ba55f2f5SFrançois Tigeot 
366ba55f2f5SFrançois Tigeot static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
367ba55f2f5SFrançois Tigeot 	.destroy = intel_dvo_enc_destroy,
368ba55f2f5SFrançois Tigeot };
369ba55f2f5SFrançois Tigeot 
370ba55f2f5SFrançois Tigeot /**
371ba55f2f5SFrançois Tigeot  * Attempts to get a fixed panel timing for LVDS (currently only the i830).
372ba55f2f5SFrançois Tigeot  *
373ba55f2f5SFrançois Tigeot  * Other chips with DVO LVDS will need to extend this to deal with the LVDS
374ba55f2f5SFrançois Tigeot  * chip being on DVOB/C and having multiple pipes.
375ba55f2f5SFrançois Tigeot  */
376ba55f2f5SFrançois Tigeot static struct drm_display_mode *
377ba55f2f5SFrançois Tigeot intel_dvo_get_current_mode(struct drm_connector *connector)
378ba55f2f5SFrançois Tigeot {
379ba55f2f5SFrançois Tigeot 	struct drm_device *dev = connector->dev;
380ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
381ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
382ba55f2f5SFrançois Tigeot 	uint32_t dvo_val = I915_READ(intel_dvo->dev.dvo_reg);
383ba55f2f5SFrançois Tigeot 	struct drm_display_mode *mode = NULL;
384ba55f2f5SFrançois Tigeot 
385ba55f2f5SFrançois Tigeot 	/* If the DVO port is active, that'll be the LVDS, so we can pull out
386ba55f2f5SFrançois Tigeot 	 * its timings to get how the BIOS set up the panel.
387ba55f2f5SFrançois Tigeot 	 */
388ba55f2f5SFrançois Tigeot 	if (dvo_val & DVO_ENABLE) {
389ba55f2f5SFrançois Tigeot 		struct drm_crtc *crtc;
390ba55f2f5SFrançois Tigeot 		int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
391ba55f2f5SFrançois Tigeot 
392ba55f2f5SFrançois Tigeot 		crtc = intel_get_crtc_for_pipe(dev, pipe);
393ba55f2f5SFrançois Tigeot 		if (crtc) {
394ba55f2f5SFrançois Tigeot 			mode = intel_crtc_mode_get(dev, crtc);
395ba55f2f5SFrançois Tigeot 			if (mode) {
396ba55f2f5SFrançois Tigeot 				mode->type |= DRM_MODE_TYPE_PREFERRED;
397ba55f2f5SFrançois Tigeot 				if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
398ba55f2f5SFrançois Tigeot 					mode->flags |= DRM_MODE_FLAG_PHSYNC;
399ba55f2f5SFrançois Tigeot 				if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
400ba55f2f5SFrançois Tigeot 					mode->flags |= DRM_MODE_FLAG_PVSYNC;
401ba55f2f5SFrançois Tigeot 			}
402ba55f2f5SFrançois Tigeot 		}
403ba55f2f5SFrançois Tigeot 	}
404ba55f2f5SFrançois Tigeot 
405ba55f2f5SFrançois Tigeot 	return mode;
406ba55f2f5SFrançois Tigeot }
407ba55f2f5SFrançois Tigeot 
408ba55f2f5SFrançois Tigeot void intel_dvo_init(struct drm_device *dev)
409ba55f2f5SFrançois Tigeot {
410ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
411ba55f2f5SFrançois Tigeot 	struct intel_encoder *intel_encoder;
412ba55f2f5SFrançois Tigeot 	struct intel_dvo *intel_dvo;
413ba55f2f5SFrançois Tigeot 	struct intel_connector *intel_connector;
414ba55f2f5SFrançois Tigeot 	int i;
415ba55f2f5SFrançois Tigeot 	int encoder_type = DRM_MODE_ENCODER_NONE;
416ba55f2f5SFrançois Tigeot 
417ba55f2f5SFrançois Tigeot 	intel_dvo = kzalloc(sizeof(*intel_dvo), GFP_KERNEL);
418ba55f2f5SFrançois Tigeot 	if (!intel_dvo)
419ba55f2f5SFrançois Tigeot 		return;
420ba55f2f5SFrançois Tigeot 
421477eb7f9SFrançois Tigeot 	intel_connector = intel_connector_alloc();
422ba55f2f5SFrançois Tigeot 	if (!intel_connector) {
423ba55f2f5SFrançois Tigeot 		kfree(intel_dvo);
424ba55f2f5SFrançois Tigeot 		return;
425ba55f2f5SFrançois Tigeot 	}
426ba55f2f5SFrançois Tigeot 
427352ff8bdSFrançois Tigeot 	intel_dvo->attached_connector = intel_connector;
428352ff8bdSFrançois Tigeot 
429ba55f2f5SFrançois Tigeot 	intel_encoder = &intel_dvo->base;
430ba55f2f5SFrançois Tigeot 	drm_encoder_init(dev, &intel_encoder->base,
431*aee94f86SFrançois Tigeot 			 &intel_dvo_enc_funcs, encoder_type, NULL);
432ba55f2f5SFrançois Tigeot 
433ba55f2f5SFrançois Tigeot 	intel_encoder->disable = intel_disable_dvo;
434ba55f2f5SFrançois Tigeot 	intel_encoder->enable = intel_enable_dvo;
435ba55f2f5SFrançois Tigeot 	intel_encoder->get_hw_state = intel_dvo_get_hw_state;
436ba55f2f5SFrançois Tigeot 	intel_encoder->get_config = intel_dvo_get_config;
437ba55f2f5SFrançois Tigeot 	intel_encoder->compute_config = intel_dvo_compute_config;
438ba55f2f5SFrançois Tigeot 	intel_encoder->pre_enable = intel_dvo_pre_enable;
439ba55f2f5SFrançois Tigeot 	intel_connector->get_hw_state = intel_dvo_connector_get_hw_state;
440ba55f2f5SFrançois Tigeot 	intel_connector->unregister = intel_connector_unregister;
441ba55f2f5SFrançois Tigeot 
442ba55f2f5SFrançois Tigeot 	/* Now, try to find a controller */
443ba55f2f5SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
444ba55f2f5SFrançois Tigeot 		struct drm_connector *connector = &intel_connector->base;
445ba55f2f5SFrançois Tigeot 		const struct intel_dvo_device *dvo = &intel_dvo_devices[i];
4465d302545SFrançois Tigeot 		struct i2c_adapter *i2c;
447ba55f2f5SFrançois Tigeot 		int gpio;
448ba55f2f5SFrançois Tigeot 		bool dvoinit;
44919c468b4SFrançois Tigeot 		enum i915_pipe pipe;
45019c468b4SFrançois Tigeot 		uint32_t dpll[I915_MAX_PIPES];
451ba55f2f5SFrançois Tigeot 
452ba55f2f5SFrançois Tigeot 		/* Allow the I2C driver info to specify the GPIO to be used in
453ba55f2f5SFrançois Tigeot 		 * special cases, but otherwise default to what's defined
454ba55f2f5SFrançois Tigeot 		 * in the spec.
455ba55f2f5SFrançois Tigeot 		 */
45619c468b4SFrançois Tigeot 		if (intel_gmbus_is_valid_pin(dev_priv, dvo->gpio))
457ba55f2f5SFrançois Tigeot 			gpio = dvo->gpio;
458ba55f2f5SFrançois Tigeot 		else if (dvo->type == INTEL_DVO_CHIP_LVDS)
45919c468b4SFrançois Tigeot 			gpio = GMBUS_PIN_SSC;
460ba55f2f5SFrançois Tigeot 		else
46119c468b4SFrançois Tigeot 			gpio = GMBUS_PIN_DPB;
462ba55f2f5SFrançois Tigeot 
463ba55f2f5SFrançois Tigeot 		/* Set up the I2C bus necessary for the chip we're probing.
464ba55f2f5SFrançois Tigeot 		 * It appears that everything is on GPIOE except for panels
465ba55f2f5SFrançois Tigeot 		 * on i830 laptops, which are on GPIOB (DVOA).
466ba55f2f5SFrançois Tigeot 		 */
467ba55f2f5SFrançois Tigeot 		i2c = intel_gmbus_get_adapter(dev_priv, gpio);
468ba55f2f5SFrançois Tigeot 
469ba55f2f5SFrançois Tigeot 		intel_dvo->dev = *dvo;
470ba55f2f5SFrançois Tigeot 
471ba55f2f5SFrançois Tigeot 		/* GMBUS NAK handling seems to be unstable, hence let the
472ba55f2f5SFrançois Tigeot 		 * transmitter detection run in bit banging mode for now.
473ba55f2f5SFrançois Tigeot 		 */
474ba55f2f5SFrançois Tigeot 		intel_gmbus_force_bit(i2c, true);
475ba55f2f5SFrançois Tigeot 
47619c468b4SFrançois Tigeot 		/* ns2501 requires the DVO 2x clock before it will
47719c468b4SFrançois Tigeot 		 * respond to i2c accesses, so make sure we have
47819c468b4SFrançois Tigeot 		 * have the clock enabled before we attempt to
47919c468b4SFrançois Tigeot 		 * initialize the device.
48019c468b4SFrançois Tigeot 		 */
48119c468b4SFrançois Tigeot 		for_each_pipe(dev_priv, pipe) {
48219c468b4SFrançois Tigeot 			dpll[pipe] = I915_READ(DPLL(pipe));
48319c468b4SFrançois Tigeot 			I915_WRITE(DPLL(pipe), dpll[pipe] | DPLL_DVO_2X_MODE);
48419c468b4SFrançois Tigeot 		}
48519c468b4SFrançois Tigeot 
486ba55f2f5SFrançois Tigeot 		dvoinit = dvo->dev_ops->init(&intel_dvo->dev, i2c);
487ba55f2f5SFrançois Tigeot 
48819c468b4SFrançois Tigeot 		/* restore the DVO 2x clock state to original */
48919c468b4SFrançois Tigeot 		for_each_pipe(dev_priv, pipe) {
49019c468b4SFrançois Tigeot 			I915_WRITE(DPLL(pipe), dpll[pipe]);
49119c468b4SFrançois Tigeot 		}
49219c468b4SFrançois Tigeot 
493ba55f2f5SFrançois Tigeot 		intel_gmbus_force_bit(i2c, false);
494ba55f2f5SFrançois Tigeot 
495ba55f2f5SFrançois Tigeot 		if (!dvoinit)
496ba55f2f5SFrançois Tigeot 			continue;
497ba55f2f5SFrançois Tigeot 
498ba55f2f5SFrançois Tigeot 		intel_encoder->type = INTEL_OUTPUT_DVO;
499ba55f2f5SFrançois Tigeot 		intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
500ba55f2f5SFrançois Tigeot 		switch (dvo->type) {
501ba55f2f5SFrançois Tigeot 		case INTEL_DVO_CHIP_TMDS:
502ba55f2f5SFrançois Tigeot 			intel_encoder->cloneable = (1 << INTEL_OUTPUT_ANALOG) |
503ba55f2f5SFrançois Tigeot 				(1 << INTEL_OUTPUT_DVO);
504ba55f2f5SFrançois Tigeot 			drm_connector_init(dev, connector,
505ba55f2f5SFrançois Tigeot 					   &intel_dvo_connector_funcs,
506ba55f2f5SFrançois Tigeot 					   DRM_MODE_CONNECTOR_DVII);
507ba55f2f5SFrançois Tigeot 			encoder_type = DRM_MODE_ENCODER_TMDS;
508ba55f2f5SFrançois Tigeot 			break;
509ba55f2f5SFrançois Tigeot 		case INTEL_DVO_CHIP_LVDS:
510ba55f2f5SFrançois Tigeot 			intel_encoder->cloneable = 0;
511ba55f2f5SFrançois Tigeot 			drm_connector_init(dev, connector,
512ba55f2f5SFrançois Tigeot 					   &intel_dvo_connector_funcs,
513ba55f2f5SFrançois Tigeot 					   DRM_MODE_CONNECTOR_LVDS);
514ba55f2f5SFrançois Tigeot 			encoder_type = DRM_MODE_ENCODER_LVDS;
515ba55f2f5SFrançois Tigeot 			break;
516ba55f2f5SFrançois Tigeot 		}
517ba55f2f5SFrançois Tigeot 
518ba55f2f5SFrançois Tigeot 		drm_connector_helper_add(connector,
519ba55f2f5SFrançois Tigeot 					 &intel_dvo_connector_helper_funcs);
520ba55f2f5SFrançois Tigeot 		connector->display_info.subpixel_order = SubPixelHorizontalRGB;
521ba55f2f5SFrançois Tigeot 		connector->interlace_allowed = false;
522ba55f2f5SFrançois Tigeot 		connector->doublescan_allowed = false;
523ba55f2f5SFrançois Tigeot 
524ba55f2f5SFrançois Tigeot 		intel_connector_attach_encoder(intel_connector, intel_encoder);
525ba55f2f5SFrançois Tigeot 		if (dvo->type == INTEL_DVO_CHIP_LVDS) {
526ba55f2f5SFrançois Tigeot 			/* For our LVDS chipsets, we should hopefully be able
527ba55f2f5SFrançois Tigeot 			 * to dig the fixed panel mode out of the BIOS data.
528ba55f2f5SFrançois Tigeot 			 * However, it's in a different format from the BIOS
529ba55f2f5SFrançois Tigeot 			 * data on chipsets with integrated LVDS (stored in AIM
530ba55f2f5SFrançois Tigeot 			 * headers, likely), so for now, just get the current
531ba55f2f5SFrançois Tigeot 			 * mode being output through DVO.
532ba55f2f5SFrançois Tigeot 			 */
533352ff8bdSFrançois Tigeot 			intel_panel_init(&intel_connector->panel,
534352ff8bdSFrançois Tigeot 					 intel_dvo_get_current_mode(connector),
535352ff8bdSFrançois Tigeot 					 NULL);
536ba55f2f5SFrançois Tigeot 			intel_dvo->panel_wants_dither = true;
537ba55f2f5SFrançois Tigeot 		}
538ba55f2f5SFrançois Tigeot 
539c6f73aabSFrançois Tigeot 		drm_connector_register(connector);
540ba55f2f5SFrançois Tigeot 		return;
541ba55f2f5SFrançois Tigeot 	}
542ba55f2f5SFrançois Tigeot 
543ba55f2f5SFrançois Tigeot 	drm_encoder_cleanup(&intel_encoder->base);
544ba55f2f5SFrançois Tigeot 	kfree(intel_dvo);
545ba55f2f5SFrançois Tigeot 	kfree(intel_connector);
546ba55f2f5SFrançois Tigeot }
547