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