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