1 /*
2  * Copyright © 2006-2011 Intel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Authors:
18  *	Eric Anholt <eric@anholt.net>
19  *	Dave Airlie <airlied@linux.ie>
20  *	Jesse Barnes <jesse.barnes@intel.com>
21  */
22 
23 #include <linux/i2c.h>
24 #include <linux/dmi.h>
25 #include <drm/drmP.h>
26 
27 #include "intel_bios.h"
28 #include "psb_drv.h"
29 #include "psb_intel_drv.h"
30 #include "psb_intel_reg.h"
31 #include "power.h"
32 #include <linux/pm_runtime.h>
33 #include "cdv_device.h"
34 
35 /**
36  * LVDS I2C backlight control macros
37  */
38 #define BRIGHTNESS_MAX_LEVEL 100
39 #define BRIGHTNESS_MASK 0xFF
40 #define BLC_I2C_TYPE	0x01
41 #define BLC_PWM_TYPT	0x02
42 
43 #define BLC_POLARITY_NORMAL 0
44 #define BLC_POLARITY_INVERSE 1
45 
46 #define PSB_BLC_MAX_PWM_REG_FREQ       (0xFFFE)
47 #define PSB_BLC_MIN_PWM_REG_FREQ	(0x2)
48 #define PSB_BLC_PWM_PRECISION_FACTOR	(10)
49 #define PSB_BACKLIGHT_PWM_CTL_SHIFT	(16)
50 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
51 
52 struct cdv_intel_lvds_priv {
53 	/**
54 	 * Saved LVDO output states
55 	 */
56 	uint32_t savePP_ON;
57 	uint32_t savePP_OFF;
58 	uint32_t saveLVDS;
59 	uint32_t savePP_CONTROL;
60 	uint32_t savePP_CYCLE;
61 	uint32_t savePFIT_CONTROL;
62 	uint32_t savePFIT_PGM_RATIOS;
63 	uint32_t saveBLC_PWM_CTL;
64 };
65 
66 /*
67  * Returns the maximum level of the backlight duty cycle field.
68  */
cdv_intel_lvds_get_max_backlight(struct drm_device * dev)69 static u32 cdv_intel_lvds_get_max_backlight(struct drm_device *dev)
70 {
71 	struct drm_psb_private *dev_priv = dev->dev_private;
72 	u32 retval;
73 
74 	if (gma_power_begin(dev, false)) {
75 		retval = ((REG_READ(BLC_PWM_CTL) &
76 			  BACKLIGHT_MODULATION_FREQ_MASK) >>
77 			  BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
78 
79 		gma_power_end(dev);
80 	} else
81 		retval = ((dev_priv->saveBLC_PWM_CTL &
82 			  BACKLIGHT_MODULATION_FREQ_MASK) >>
83 			  BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
84 
85 	return retval;
86 }
87 
88 /*
89  * Set LVDS backlight level by I2C command
90  */
cdv_lvds_i2c_set_brightness(struct drm_device * dev,unsigned int level)91 static int cdv_lvds_i2c_set_brightness(struct drm_device *dev,
92 					unsigned int level)
93 {
94 	struct drm_psb_private *dev_priv = dev->dev_private;
95 	struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
96 	u8 out_buf[2];
97 	unsigned int blc_i2c_brightness;
98 
99 	struct i2c_msg msgs[] = {
100 		{
101 			.addr = lvds_i2c_bus->slave_addr,
102 			.flags = 0,
103 			.len = 2,
104 			.buf = out_buf,
105 		}
106 	};
107 
108 	blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
109 			     BRIGHTNESS_MASK /
110 			     BRIGHTNESS_MAX_LEVEL);
111 
112 	if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
113 		blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
114 
115 	out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
116 	out_buf[1] = (u8)blc_i2c_brightness;
117 
118 	if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1)
119 		return 0;
120 
121 	DRM_ERROR("I2C transfer error\n");
122 	return -1;
123 }
124 
125 
cdv_lvds_pwm_set_brightness(struct drm_device * dev,int level)126 static int cdv_lvds_pwm_set_brightness(struct drm_device *dev, int level)
127 {
128 	struct drm_psb_private *dev_priv = dev->dev_private;
129 
130 	u32 max_pwm_blc;
131 	u32 blc_pwm_duty_cycle;
132 
133 	max_pwm_blc = cdv_intel_lvds_get_max_backlight(dev);
134 
135 	/*BLC_PWM_CTL Should be initiated while backlight device init*/
136 	BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
137 
138 	blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
139 
140 	if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
141 		blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
142 
143 	blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
144 	REG_WRITE(BLC_PWM_CTL,
145 		  (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
146 		  (blc_pwm_duty_cycle));
147 
148 	return 0;
149 }
150 
151 /*
152  * Set LVDS backlight level either by I2C or PWM
153  */
cdv_intel_lvds_set_brightness(struct drm_device * dev,int level)154 void cdv_intel_lvds_set_brightness(struct drm_device *dev, int level)
155 {
156 	struct drm_psb_private *dev_priv = dev->dev_private;
157 
158 	if (!dev_priv->lvds_bl) {
159 		DRM_ERROR("NO LVDS Backlight Info\n");
160 		return;
161 	}
162 
163 	if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
164 		cdv_lvds_i2c_set_brightness(dev, level);
165 	else
166 		cdv_lvds_pwm_set_brightness(dev, level);
167 }
168 
169 /**
170  * Sets the backlight level.
171  *
172  * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
173  */
cdv_intel_lvds_set_backlight(struct drm_device * dev,int level)174 static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
175 {
176 	struct drm_psb_private *dev_priv = dev->dev_private;
177 	u32 blc_pwm_ctl;
178 
179 	if (gma_power_begin(dev, false)) {
180 		blc_pwm_ctl =
181 			REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
182 		REG_WRITE(BLC_PWM_CTL,
183 				(blc_pwm_ctl |
184 				(level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
185 		gma_power_end(dev);
186 	} else {
187 		blc_pwm_ctl = dev_priv->saveBLC_PWM_CTL &
188 				~BACKLIGHT_DUTY_CYCLE_MASK;
189 		dev_priv->saveBLC_PWM_CTL = (blc_pwm_ctl |
190 					(level << BACKLIGHT_DUTY_CYCLE_SHIFT));
191 	}
192 }
193 
194 /**
195  * Sets the power state for the panel.
196  */
cdv_intel_lvds_set_power(struct drm_device * dev,struct drm_encoder * encoder,bool on)197 static void cdv_intel_lvds_set_power(struct drm_device *dev,
198 				     struct drm_encoder *encoder, bool on)
199 {
200 	struct drm_psb_private *dev_priv = dev->dev_private;
201 	u32 pp_status;
202 
203 	if (!gma_power_begin(dev, true))
204 		return;
205 
206 	if (on) {
207 		REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
208 			  POWER_TARGET_ON);
209 		do {
210 			pp_status = REG_READ(PP_STATUS);
211 		} while ((pp_status & PP_ON) == 0);
212 
213 		cdv_intel_lvds_set_backlight(dev,
214 				dev_priv->mode_dev.backlight_duty_cycle);
215 	} else {
216 		cdv_intel_lvds_set_backlight(dev, 0);
217 
218 		REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
219 			  ~POWER_TARGET_ON);
220 		do {
221 			pp_status = REG_READ(PP_STATUS);
222 		} while (pp_status & PP_ON);
223 	}
224 	gma_power_end(dev);
225 }
226 
cdv_intel_lvds_encoder_dpms(struct drm_encoder * encoder,int mode)227 static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
228 {
229 	struct drm_device *dev = encoder->dev;
230 	if (mode == DRM_MODE_DPMS_ON)
231 		cdv_intel_lvds_set_power(dev, encoder, true);
232 	else
233 		cdv_intel_lvds_set_power(dev, encoder, false);
234 	/* XXX: We never power down the LVDS pairs. */
235 }
236 
cdv_intel_lvds_save(struct drm_connector * connector)237 static void cdv_intel_lvds_save(struct drm_connector *connector)
238 {
239 }
240 
cdv_intel_lvds_restore(struct drm_connector * connector)241 static void cdv_intel_lvds_restore(struct drm_connector *connector)
242 {
243 }
244 
cdv_intel_lvds_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)245 int cdv_intel_lvds_mode_valid(struct drm_connector *connector,
246 			      struct drm_display_mode *mode)
247 {
248 	struct drm_device *dev = connector->dev;
249 	struct drm_psb_private *dev_priv = dev->dev_private;
250 	struct drm_display_mode *fixed_mode =
251 					dev_priv->mode_dev.panel_fixed_mode;
252 
253 	/* just in case */
254 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
255 		return MODE_NO_DBLESCAN;
256 
257 	/* just in case */
258 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
259 		return MODE_NO_INTERLACE;
260 
261 	if (fixed_mode) {
262 		if (mode->hdisplay > fixed_mode->hdisplay)
263 			return MODE_PANEL;
264 		if (mode->vdisplay > fixed_mode->vdisplay)
265 			return MODE_PANEL;
266 	}
267 	return MODE_OK;
268 }
269 
cdv_intel_lvds_mode_fixup(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)270 bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
271 				  struct drm_display_mode *mode,
272 				  struct drm_display_mode *adjusted_mode)
273 {
274 	struct drm_device *dev = encoder->dev;
275 	struct drm_psb_private *dev_priv = dev->dev_private;
276 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
277 	struct drm_encoder *tmp_encoder;
278 	struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
279 
280 	/* Should never happen!! */
281 	list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
282 			    head) {
283 		if (tmp_encoder != encoder
284 		    && tmp_encoder->crtc == encoder->crtc) {
285 			printk(KERN_ERR "Can't enable LVDS and another "
286 			       "encoder on the same pipe\n");
287 			return false;
288 		}
289 	}
290 
291 	/*
292 	 * If we have timings from the BIOS for the panel, put them in
293 	 * to the adjusted mode.  The CRTC will be set up for this mode,
294 	 * with the panel scaling set up to source from the H/VDisplay
295 	 * of the original mode.
296 	 */
297 	if (panel_fixed_mode != NULL) {
298 		adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
299 		adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
300 		adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
301 		adjusted_mode->htotal = panel_fixed_mode->htotal;
302 		adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
303 		adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
304 		adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
305 		adjusted_mode->vtotal = panel_fixed_mode->vtotal;
306 		adjusted_mode->clock = panel_fixed_mode->clock;
307 		drm_mode_set_crtcinfo(adjusted_mode,
308 				      CRTC_INTERLACE_HALVE_V);
309 	}
310 
311 	/*
312 	 * XXX: It would be nice to support lower refresh rates on the
313 	 * panels to reduce power consumption, and perhaps match the
314 	 * user's requested refresh rate.
315 	 */
316 
317 	return true;
318 }
319 
cdv_intel_lvds_prepare(struct drm_encoder * encoder)320 static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
321 {
322 	struct drm_device *dev = encoder->dev;
323 	struct drm_psb_private *dev_priv = dev->dev_private;
324 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
325 
326 	if (!gma_power_begin(dev, true))
327 		return;
328 
329 	mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
330 	mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
331 					  BACKLIGHT_DUTY_CYCLE_MASK);
332 
333 	cdv_intel_lvds_set_power(dev, encoder, false);
334 
335 	gma_power_end(dev);
336 }
337 
cdv_intel_lvds_commit(struct drm_encoder * encoder)338 static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
339 {
340 	struct drm_device *dev = encoder->dev;
341 	struct drm_psb_private *dev_priv = dev->dev_private;
342 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
343 
344 	if (mode_dev->backlight_duty_cycle == 0)
345 		mode_dev->backlight_duty_cycle =
346 		    cdv_intel_lvds_get_max_backlight(dev);
347 
348 	cdv_intel_lvds_set_power(dev, encoder, true);
349 }
350 
cdv_intel_lvds_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)351 static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
352 				struct drm_display_mode *mode,
353 				struct drm_display_mode *adjusted_mode)
354 {
355 	struct drm_device *dev = encoder->dev;
356 	struct drm_psb_private *dev_priv = dev->dev_private;
357 	u32 pfit_control;
358 
359 	/*
360 	 * The LVDS pin pair will already have been turned on in the
361 	 * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
362 	 * settings.
363 	 */
364 
365 	/*
366 	 * Enable automatic panel scaling so that non-native modes fill the
367 	 * screen.  Should be enabled before the pipe is enabled, according to
368 	 * register description and PRM.
369 	 */
370 	if (mode->hdisplay != adjusted_mode->hdisplay ||
371 	    mode->vdisplay != adjusted_mode->vdisplay)
372 		pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
373 				HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
374 				HORIZ_INTERP_BILINEAR);
375 	else
376 		pfit_control = 0;
377 
378 	if (dev_priv->lvds_dither)
379 		pfit_control |= PANEL_8TO6_DITHER_ENABLE;
380 
381 	REG_WRITE(PFIT_CONTROL, pfit_control);
382 }
383 
384 /**
385  * Detect the LVDS connection.
386  *
387  * This always returns CONNECTOR_STATUS_CONNECTED.
388  * This connector should only have
389  * been set up if the LVDS was actually connected anyway.
390  */
cdv_intel_lvds_detect(struct drm_connector * connector,bool force)391 static enum drm_connector_status cdv_intel_lvds_detect(
392 				struct drm_connector *connector, bool force)
393 {
394 	return connector_status_connected;
395 }
396 
397 /**
398  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
399  */
cdv_intel_lvds_get_modes(struct drm_connector * connector)400 static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
401 {
402 	struct drm_device *dev = connector->dev;
403 	struct drm_psb_private *dev_priv = dev->dev_private;
404 	struct psb_intel_encoder *psb_intel_encoder =
405 					psb_intel_attached_encoder(connector);
406 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
407 	int ret;
408 
409 	ret = psb_intel_ddc_get_modes(connector, &psb_intel_encoder->i2c_bus->adapter);
410 
411 	if (ret)
412 		return ret;
413 
414 	/* Didn't get an EDID, so
415 	 * Set wide sync ranges so we get all modes
416 	 * handed to valid_mode for checking
417 	 */
418 	connector->display_info.min_vfreq = 0;
419 	connector->display_info.max_vfreq = 200;
420 	connector->display_info.min_hfreq = 0;
421 	connector->display_info.max_hfreq = 200;
422 	if (mode_dev->panel_fixed_mode != NULL) {
423 		struct drm_display_mode *mode =
424 		    drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
425 		drm_mode_probed_add(connector, mode);
426 		return 1;
427 	}
428 
429 	return 0;
430 }
431 
432 /**
433  * cdv_intel_lvds_destroy - unregister and free LVDS structures
434  * @connector: connector to free
435  *
436  * Unregister the DDC bus for this connector then free the driver private
437  * structure.
438  */
cdv_intel_lvds_destroy(struct drm_connector * connector)439 void cdv_intel_lvds_destroy(struct drm_connector *connector)
440 {
441 	struct psb_intel_encoder *psb_intel_encoder =
442 					psb_intel_attached_encoder(connector);
443 
444 	if (psb_intel_encoder->i2c_bus)
445 		psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
446 	drm_sysfs_connector_remove(connector);
447 	drm_connector_cleanup(connector);
448 	kfree(connector);
449 }
450 
cdv_intel_lvds_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t value)451 int cdv_intel_lvds_set_property(struct drm_connector *connector,
452 				       struct drm_property *property,
453 				       uint64_t value)
454 {
455 	struct drm_encoder *encoder = connector->encoder;
456 
457 	if (!strcmp(property->name, "scaling mode") && encoder) {
458 		struct psb_intel_crtc *crtc =
459 					to_psb_intel_crtc(encoder->crtc);
460 		uint64_t curValue;
461 
462 		if (!crtc)
463 			return -1;
464 
465 		switch (value) {
466 		case DRM_MODE_SCALE_FULLSCREEN:
467 			break;
468 		case DRM_MODE_SCALE_NO_SCALE:
469 			break;
470 		case DRM_MODE_SCALE_ASPECT:
471 			break;
472 		default:
473 			return -1;
474 		}
475 
476 		if (drm_connector_property_get_value(connector,
477 						     property,
478 						     &curValue))
479 			return -1;
480 
481 		if (curValue == value)
482 			return 0;
483 
484 		if (drm_connector_property_set_value(connector,
485 							property,
486 							value))
487 			return -1;
488 
489 		if (crtc->saved_mode.hdisplay != 0 &&
490 		    crtc->saved_mode.vdisplay != 0) {
491 			if (!drm_crtc_helper_set_mode(encoder->crtc,
492 						      &crtc->saved_mode,
493 						      encoder->crtc->x,
494 						      encoder->crtc->y,
495 						      encoder->crtc->fb))
496 				return -1;
497 		}
498 	} else if (!strcmp(property->name, "backlight") && encoder) {
499 		if (drm_connector_property_set_value(connector,
500 							property,
501 							value))
502 			return -1;
503 		else {
504 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
505 			struct drm_psb_private *dev_priv =
506 						encoder->dev->dev_private;
507 			struct backlight_device *bd =
508 						dev_priv->backlight_device;
509 			bd->props.brightness = value;
510 			backlight_update_status(bd);
511 #endif
512 		}
513 	} else if (!strcmp(property->name, "DPMS") && encoder) {
514 		struct drm_encoder_helper_funcs *helpers =
515 					encoder->helper_private;
516 		helpers->dpms(encoder, value);
517 	}
518 	return 0;
519 }
520 
521 static const struct drm_encoder_helper_funcs
522 					cdv_intel_lvds_helper_funcs = {
523 	.dpms = cdv_intel_lvds_encoder_dpms,
524 	.mode_fixup = cdv_intel_lvds_mode_fixup,
525 	.prepare = cdv_intel_lvds_prepare,
526 	.mode_set = cdv_intel_lvds_mode_set,
527 	.commit = cdv_intel_lvds_commit,
528 };
529 
530 static const struct drm_connector_helper_funcs
531 				cdv_intel_lvds_connector_helper_funcs = {
532 	.get_modes = cdv_intel_lvds_get_modes,
533 	.mode_valid = cdv_intel_lvds_mode_valid,
534 	.best_encoder = psb_intel_best_encoder,
535 };
536 
537 static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
538 	.dpms = drm_helper_connector_dpms,
539 	.save = cdv_intel_lvds_save,
540 	.restore = cdv_intel_lvds_restore,
541 	.detect = cdv_intel_lvds_detect,
542 	.fill_modes = drm_helper_probe_single_connector_modes,
543 	.set_property = cdv_intel_lvds_set_property,
544 	.destroy = cdv_intel_lvds_destroy,
545 };
546 
547 
cdv_intel_lvds_enc_destroy(struct drm_encoder * encoder)548 static void cdv_intel_lvds_enc_destroy(struct drm_encoder *encoder)
549 {
550 	drm_encoder_cleanup(encoder);
551 }
552 
553 const struct drm_encoder_funcs cdv_intel_lvds_enc_funcs = {
554 	.destroy = cdv_intel_lvds_enc_destroy,
555 };
556 
557 /**
558  * cdv_intel_lvds_init - setup LVDS connectors on this device
559  * @dev: drm device
560  *
561  * Create the connector, register the LVDS DDC bus, and try to figure out what
562  * modes we can display on the LVDS panel (if present).
563  */
cdv_intel_lvds_init(struct drm_device * dev,struct psb_intel_mode_device * mode_dev)564 void cdv_intel_lvds_init(struct drm_device *dev,
565 		     struct psb_intel_mode_device *mode_dev)
566 {
567 	struct psb_intel_encoder *psb_intel_encoder;
568 	struct psb_intel_connector *psb_intel_connector;
569 	struct cdv_intel_lvds_priv *lvds_priv;
570 	struct drm_connector *connector;
571 	struct drm_encoder *encoder;
572 	struct drm_display_mode *scan;
573 	struct drm_crtc *crtc;
574 	struct drm_psb_private *dev_priv = dev->dev_private;
575 	u32 lvds;
576 	int pipe;
577 
578 	psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
579 				    GFP_KERNEL);
580 	if (!psb_intel_encoder)
581 		return;
582 
583 	psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
584 				      GFP_KERNEL);
585 	if (!psb_intel_connector)
586 		goto failed_connector;
587 
588 	lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
589 	if (!lvds_priv)
590 		goto failed_lvds_priv;
591 
592 	psb_intel_encoder->dev_priv = lvds_priv;
593 
594 	connector = &psb_intel_connector->base;
595 	encoder = &psb_intel_encoder->base;
596 
597 
598 	drm_connector_init(dev, connector,
599 			   &cdv_intel_lvds_connector_funcs,
600 			   DRM_MODE_CONNECTOR_LVDS);
601 
602 	drm_encoder_init(dev, encoder,
603 			 &cdv_intel_lvds_enc_funcs,
604 			 DRM_MODE_ENCODER_LVDS);
605 
606 
607 	psb_intel_connector_attach_encoder(psb_intel_connector,
608 					   psb_intel_encoder);
609 	psb_intel_encoder->type = INTEL_OUTPUT_LVDS;
610 
611 	drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
612 	drm_connector_helper_add(connector,
613 				 &cdv_intel_lvds_connector_helper_funcs);
614 	connector->display_info.subpixel_order = SubPixelHorizontalRGB;
615 	connector->interlace_allowed = false;
616 	connector->doublescan_allowed = false;
617 
618 	/*Attach connector properties*/
619 	drm_connector_attach_property(connector,
620 				      dev->mode_config.scaling_mode_property,
621 				      DRM_MODE_SCALE_FULLSCREEN);
622 	drm_connector_attach_property(connector,
623 				      dev_priv->backlight_property,
624 				      BRIGHTNESS_MAX_LEVEL);
625 
626 	/**
627 	 * Set up I2C bus
628 	 * FIXME: distroy i2c_bus when exit
629 	 */
630 	psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
631 							 GPIOB,
632 							 "LVDSBLC_B");
633 	if (!psb_intel_encoder->i2c_bus) {
634 		dev_printk(KERN_ERR,
635 			&dev->pdev->dev, "I2C bus registration failed.\n");
636 		goto failed_blc_i2c;
637 	}
638 	psb_intel_encoder->i2c_bus->slave_addr = 0x2C;
639 	dev_priv->lvds_i2c_bus = psb_intel_encoder->i2c_bus;
640 
641 	/*
642 	 * LVDS discovery:
643 	 * 1) check for EDID on DDC
644 	 * 2) check for VBT data
645 	 * 3) check to see if LVDS is already on
646 	 *    if none of the above, no panel
647 	 * 4) make sure lid is open
648 	 *    if closed, act like it's not there for now
649 	 */
650 
651 	/* Set up the DDC bus. */
652 	psb_intel_encoder->ddc_bus = psb_intel_i2c_create(dev,
653 							 GPIOC,
654 							 "LVDSDDC_C");
655 	if (!psb_intel_encoder->ddc_bus) {
656 		dev_printk(KERN_ERR, &dev->pdev->dev,
657 			   "DDC bus registration " "failed.\n");
658 		goto failed_ddc;
659 	}
660 
661 	/*
662 	 * Attempt to get the fixed panel mode from DDC.  Assume that the
663 	 * preferred mode is the right one.
664 	 */
665 	psb_intel_ddc_get_modes(connector,
666 				&psb_intel_encoder->ddc_bus->adapter);
667 	list_for_each_entry(scan, &connector->probed_modes, head) {
668 		if (scan->type & DRM_MODE_TYPE_PREFERRED) {
669 			mode_dev->panel_fixed_mode =
670 			    drm_mode_duplicate(dev, scan);
671 			goto out;	/* FIXME: check for quirks */
672 		}
673 	}
674 
675 	/* Failed to get EDID, what about VBT? do we need this?*/
676 	if (dev_priv->lfp_lvds_vbt_mode) {
677 		mode_dev->panel_fixed_mode =
678 			drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
679 		if (mode_dev->panel_fixed_mode) {
680 			mode_dev->panel_fixed_mode->type |=
681 				DRM_MODE_TYPE_PREFERRED;
682 			goto out;	/* FIXME: check for quirks */
683 		}
684 	}
685 	/*
686 	 * If we didn't get EDID, try checking if the panel is already turned
687 	 * on.	If so, assume that whatever is currently programmed is the
688 	 * correct mode.
689 	 */
690 	lvds = REG_READ(LVDS);
691 	pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
692 	crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
693 
694 	if (crtc && (lvds & LVDS_PORT_EN)) {
695 		mode_dev->panel_fixed_mode =
696 		    cdv_intel_crtc_mode_get(dev, crtc);
697 		if (mode_dev->panel_fixed_mode) {
698 			mode_dev->panel_fixed_mode->type |=
699 			    DRM_MODE_TYPE_PREFERRED;
700 			goto out;	/* FIXME: check for quirks */
701 		}
702 	}
703 
704 	/* If we still don't have a mode after all that, give up. */
705 	if (!mode_dev->panel_fixed_mode) {
706 		DRM_DEBUG
707 			("Found no modes on the lvds, ignoring the LVDS\n");
708 		goto failed_find;
709 	}
710 
711 out:
712 	drm_sysfs_connector_add(connector);
713 	return;
714 
715 failed_find:
716 	printk(KERN_ERR "Failed find\n");
717 	if (psb_intel_encoder->ddc_bus)
718 		psb_intel_i2c_destroy(psb_intel_encoder->ddc_bus);
719 failed_ddc:
720 	printk(KERN_ERR "Failed DDC\n");
721 	if (psb_intel_encoder->i2c_bus)
722 		psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
723 failed_blc_i2c:
724 	printk(KERN_ERR "Failed BLC\n");
725 	drm_encoder_cleanup(encoder);
726 	drm_connector_cleanup(connector);
727 	kfree(lvds_priv);
728 failed_lvds_priv:
729 	kfree(psb_intel_connector);
730 failed_connector:
731 	kfree(psb_intel_encoder);
732 }
733