]> Pileus Git - ~andy/linux/blob - drivers/staging/gma500/psb_intel_lvds.c
gma500: prune some unused variables
[~andy/linux] / drivers / staging / gma500 / psb_intel_lvds.c
1 /*
2  * Copyright © 2006-2007 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 <drm/drm_crtc.h> */
25 /* #include <drm/drm_edid.h> */
26 #include <drm/drmP.h>
27
28 #include "psb_intel_bios.h"
29 #include "psb_drv.h"
30 #include "psb_intel_drv.h"
31 #include "psb_intel_reg.h"
32 #include "psb_powermgmt.h"
33 #include <linux/pm_runtime.h>
34
35 u32 CoreClock;
36 u32 PWMControlRegFreq;
37
38 /**
39  * LVDS I2C backlight control macros
40  */
41 #define BRIGHTNESS_MAX_LEVEL 100
42 #define BRIGHTNESS_MASK 0xFF
43 #define BLC_I2C_TYPE    0x01
44 #define BLC_PWM_TYPT    0x02
45
46 #define BLC_POLARITY_NORMAL 0
47 #define BLC_POLARITY_INVERSE 1
48
49 #define PSB_BLC_MAX_PWM_REG_FREQ       (0xFFFE)
50 #define PSB_BLC_MIN_PWM_REG_FREQ        (0x2)
51 #define PSB_BLC_PWM_PRECISION_FACTOR    (10)
52 #define PSB_BACKLIGHT_PWM_CTL_SHIFT     (16)
53 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
54
55 struct psb_intel_lvds_priv {
56         /**
57          * Saved LVDO output states
58          */
59         uint32_t savePP_ON;
60         uint32_t savePP_OFF;
61         uint32_t saveLVDS;
62         uint32_t savePP_CONTROL;
63         uint32_t savePP_CYCLE;
64         uint32_t savePFIT_CONTROL;
65         uint32_t savePFIT_PGM_RATIOS;
66         uint32_t saveBLC_PWM_CTL;
67 };
68
69 /* MRST defines end */
70
71 /**
72  * Returns the maximum level of the backlight duty cycle field.
73  */
74 static u32 psb_intel_lvds_get_max_backlight(struct drm_device *dev)
75 {
76         struct drm_psb_private *dev_priv = dev->dev_private;
77         u32 retVal;
78
79         if (gma_power_begin(dev, false)) {
80                 retVal = ((REG_READ(BLC_PWM_CTL) &
81                           BACKLIGHT_MODULATION_FREQ_MASK) >>
82                           BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
83
84                 gma_power_end(dev);
85         } else
86                 retVal = ((dev_priv->saveBLC_PWM_CTL &
87                           BACKLIGHT_MODULATION_FREQ_MASK) >>
88                           BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
89
90         return retVal;
91 }
92
93 /*
94  * Set LVDS backlight level by I2C command
95  *
96  * FIXME: at some point we need to both track this for PM and also
97  * disable runtime pm on MRST if the brightness is nil (ie blanked)
98  */
99 static int psb_lvds_i2c_set_brightness(struct drm_device *dev,
100                                         unsigned int level)
101 {
102         struct drm_psb_private *dev_priv =
103                 (struct drm_psb_private *)dev->dev_private;
104
105         struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
106         u8 out_buf[2];
107         unsigned int blc_i2c_brightness;
108
109         struct i2c_msg msgs[] = {
110                 {
111                         .addr = lvds_i2c_bus->slave_addr,
112                         .flags = 0,
113                         .len = 2,
114                         .buf = out_buf,
115                 }
116         };
117
118         blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
119                              BRIGHTNESS_MASK /
120                              BRIGHTNESS_MAX_LEVEL);
121
122         if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
123                 blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
124
125         out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
126         out_buf[1] = (u8)blc_i2c_brightness;
127
128         if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1) {
129                 DRM_DEBUG("I2C set brightness.(command, value) (%d, %d)\n",
130                         dev_priv->lvds_bl->brightnesscmd,
131                         blc_i2c_brightness);
132                 return 0;
133         }
134
135         DRM_ERROR("I2C transfer error\n");
136         return -1;
137 }
138
139
140 static int psb_lvds_pwm_set_brightness(struct drm_device *dev, int level)
141 {
142         struct drm_psb_private *dev_priv =
143                         (struct drm_psb_private *)dev->dev_private;
144
145         u32 max_pwm_blc;
146         u32 blc_pwm_duty_cycle;
147
148         max_pwm_blc = psb_intel_lvds_get_max_backlight(dev);
149
150         /*BLC_PWM_CTL Should be initiated while backlight device init*/
151         BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
152
153         blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
154
155         if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
156                 blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
157
158         blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
159         REG_WRITE(BLC_PWM_CTL,
160                   (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
161                   (blc_pwm_duty_cycle));
162
163         return 0;
164 }
165
166 /**
167  * Set LVDS backlight level either by I2C or PWM
168  */
169 void psb_intel_lvds_set_brightness(struct drm_device *dev, int level)
170 {
171         /*u32 blc_pwm_ctl;*/
172         struct drm_psb_private *dev_priv =
173                         (struct drm_psb_private *)dev->dev_private;
174
175         DRM_DEBUG("backlight level is %d\n", level);
176
177         if (!dev_priv->lvds_bl) {
178                 DRM_ERROR("NO LVDS Backlight Info\n");
179                 return;
180         }
181
182         if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
183                 psb_lvds_i2c_set_brightness(dev, level);
184         else
185                 psb_lvds_pwm_set_brightness(dev, level);
186 }
187
188 /**
189  * Sets the backlight level.
190  *
191  * \param level backlight level, from 0 to psb_intel_lvds_get_max_backlight().
192  */
193 static void psb_intel_lvds_set_backlight(struct drm_device *dev, int level)
194 {
195         struct drm_psb_private *dev_priv = dev->dev_private;
196         u32 blc_pwm_ctl;
197
198         if (gma_power_begin(dev, false)) {
199                 blc_pwm_ctl =
200                         REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
201                 REG_WRITE(BLC_PWM_CTL,
202                                 (blc_pwm_ctl |
203                                 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
204                 gma_power_end(dev);
205         } else {
206                 blc_pwm_ctl = dev_priv->saveBLC_PWM_CTL &
207                                 ~BACKLIGHT_DUTY_CYCLE_MASK;
208                 dev_priv->saveBLC_PWM_CTL = (blc_pwm_ctl |
209                                         (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
210         }
211 }
212
213 /**
214  * Sets the power state for the panel.
215  */
216 static void psb_intel_lvds_set_power(struct drm_device *dev,
217                                  struct psb_intel_output *output, bool on)
218 {
219         u32 pp_status;
220
221         if (!gma_power_begin(dev, true))
222                 return;
223
224         if (on) {
225                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
226                           POWER_TARGET_ON);
227                 do {
228                         pp_status = REG_READ(PP_STATUS);
229                 } while ((pp_status & PP_ON) == 0);
230
231                 psb_intel_lvds_set_backlight(dev,
232                                          output->
233                                          mode_dev->backlight_duty_cycle);
234         } else {
235                 psb_intel_lvds_set_backlight(dev, 0);
236
237                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
238                           ~POWER_TARGET_ON);
239                 do {
240                         pp_status = REG_READ(PP_STATUS);
241                 } while (pp_status & PP_ON);
242         }
243
244         gma_power_end(dev);
245 }
246
247 static void psb_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
248 {
249         struct drm_device *dev = encoder->dev;
250         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
251
252         if (mode == DRM_MODE_DPMS_ON)
253                 psb_intel_lvds_set_power(dev, output, true);
254         else
255                 psb_intel_lvds_set_power(dev, output, false);
256
257         /* XXX: We never power down the LVDS pairs. */
258 }
259
260 static void psb_intel_lvds_save(struct drm_connector *connector)
261 {
262         struct drm_device *dev = connector->dev;
263         struct drm_psb_private *dev_priv =
264                 (struct drm_psb_private *)dev->dev_private;
265         struct psb_intel_output *psb_intel_output =
266                 to_psb_intel_output(connector);
267         struct psb_intel_lvds_priv *lvds_priv =
268                 (struct psb_intel_lvds_priv *)psb_intel_output->dev_priv;
269
270         lvds_priv->savePP_ON = REG_READ(LVDSPP_ON);
271         lvds_priv->savePP_OFF = REG_READ(LVDSPP_OFF);
272         lvds_priv->saveLVDS = REG_READ(LVDS);
273         lvds_priv->savePP_CONTROL = REG_READ(PP_CONTROL);
274         lvds_priv->savePP_CYCLE = REG_READ(PP_CYCLE);
275         /*lvds_priv->savePP_DIVISOR = REG_READ(PP_DIVISOR);*/
276         lvds_priv->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
277         lvds_priv->savePFIT_CONTROL = REG_READ(PFIT_CONTROL);
278         lvds_priv->savePFIT_PGM_RATIOS = REG_READ(PFIT_PGM_RATIOS);
279
280         /*TODO: move backlight_duty_cycle to psb_intel_lvds_priv*/
281         dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
282                                                 BACKLIGHT_DUTY_CYCLE_MASK);
283
284         /*
285          * If the light is off at server startup,
286          * just make it full brightness
287          */
288         if (dev_priv->backlight_duty_cycle == 0)
289                 dev_priv->backlight_duty_cycle =
290                 psb_intel_lvds_get_max_backlight(dev);
291
292         DRM_DEBUG("(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
293                         lvds_priv->savePP_ON,
294                         lvds_priv->savePP_OFF,
295                         lvds_priv->saveLVDS,
296                         lvds_priv->savePP_CONTROL,
297                         lvds_priv->savePP_CYCLE,
298                         lvds_priv->saveBLC_PWM_CTL);
299 }
300
301 static void psb_intel_lvds_restore(struct drm_connector *connector)
302 {
303         struct drm_device *dev = connector->dev;
304         u32 pp_status;
305
306         /*struct drm_psb_private *dev_priv =
307                                 (struct drm_psb_private *)dev->dev_private;*/
308         struct psb_intel_output *psb_intel_output =
309                                         to_psb_intel_output(connector);
310         struct psb_intel_lvds_priv *lvds_priv =
311                 (struct psb_intel_lvds_priv *)psb_intel_output->dev_priv;
312
313         DRM_DEBUG("(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
314                         lvds_priv->savePP_ON,
315                         lvds_priv->savePP_OFF,
316                         lvds_priv->saveLVDS,
317                         lvds_priv->savePP_CONTROL,
318                         lvds_priv->savePP_CYCLE,
319                         lvds_priv->saveBLC_PWM_CTL);
320
321         REG_WRITE(BLC_PWM_CTL, lvds_priv->saveBLC_PWM_CTL);
322         REG_WRITE(PFIT_CONTROL, lvds_priv->savePFIT_CONTROL);
323         REG_WRITE(PFIT_PGM_RATIOS, lvds_priv->savePFIT_PGM_RATIOS);
324         REG_WRITE(LVDSPP_ON, lvds_priv->savePP_ON);
325         REG_WRITE(LVDSPP_OFF, lvds_priv->savePP_OFF);
326         /*REG_WRITE(PP_DIVISOR, lvds_priv->savePP_DIVISOR);*/
327         REG_WRITE(PP_CYCLE, lvds_priv->savePP_CYCLE);
328         REG_WRITE(PP_CONTROL, lvds_priv->savePP_CONTROL);
329         REG_WRITE(LVDS, lvds_priv->saveLVDS);
330
331         if (lvds_priv->savePP_CONTROL & POWER_TARGET_ON) {
332                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
333                         POWER_TARGET_ON);
334                 do {
335                         pp_status = REG_READ(PP_STATUS);
336                 } while ((pp_status & PP_ON) == 0);
337         } else {
338                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
339                         ~POWER_TARGET_ON);
340                 do {
341                         pp_status = REG_READ(PP_STATUS);
342                 } while (pp_status & PP_ON);
343         }
344 }
345
346 int psb_intel_lvds_mode_valid(struct drm_connector *connector,
347                                  struct drm_display_mode *mode)
348 {
349         struct psb_intel_output *psb_intel_output =
350                                 to_psb_intel_output(connector);
351         struct drm_display_mode *fixed_mode =
352             psb_intel_output->mode_dev->panel_fixed_mode;
353
354         PSB_DEBUG_ENTRY("\n");
355
356         if (psb_intel_output->type == INTEL_OUTPUT_MIPI2)
357                 fixed_mode = psb_intel_output->mode_dev->panel_fixed_mode2;
358
359         /* just in case */
360         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
361                 return MODE_NO_DBLESCAN;
362
363         /* just in case */
364         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
365                 return MODE_NO_INTERLACE;
366
367         if (fixed_mode) {
368                 if (mode->hdisplay > fixed_mode->hdisplay)
369                         return MODE_PANEL;
370                 if (mode->vdisplay > fixed_mode->vdisplay)
371                         return MODE_PANEL;
372         }
373         return MODE_OK;
374 }
375
376 bool psb_intel_lvds_mode_fixup(struct drm_encoder *encoder,
377                                   struct drm_display_mode *mode,
378                                   struct drm_display_mode *adjusted_mode)
379 {
380         struct psb_intel_mode_device *mode_dev =
381             enc_to_psb_intel_output(encoder)->mode_dev;
382         struct drm_device *dev = encoder->dev;
383         struct psb_intel_crtc *psb_intel_crtc =
384                                 to_psb_intel_crtc(encoder->crtc);
385         struct drm_encoder *tmp_encoder;
386         struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
387         struct psb_intel_output *psb_intel_output =
388                                         enc_to_psb_intel_output(encoder);
389
390         PSB_DEBUG_ENTRY("type = 0x%x, pipe = %d.\n",
391                         psb_intel_output->type, psb_intel_crtc->pipe);
392
393         if (psb_intel_output->type == INTEL_OUTPUT_MIPI2)
394                 panel_fixed_mode = mode_dev->panel_fixed_mode2;
395
396         /* PSB requires the LVDS is on pipe B, MRST has only one pipe anyway */
397         if (!IS_MRST(dev) && psb_intel_crtc->pipe == 0) {
398                 printk(KERN_ERR "Can't support LVDS on pipe A\n");
399                 return false;
400         }
401         if (IS_MRST(dev) && psb_intel_crtc->pipe != 0) {
402                 printk(KERN_ERR "Must use PIPE A\n");
403                 return false;
404         }
405         /* Should never happen!! */
406         list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
407                             head) {
408                 if (tmp_encoder != encoder
409                     && tmp_encoder->crtc == encoder->crtc) {
410                         printk(KERN_ERR "Can't enable LVDS and another "
411                                "encoder on the same pipe\n");
412                         return false;
413                 }
414         }
415
416         /*
417          * If we have timings from the BIOS for the panel, put them in
418          * to the adjusted mode.  The CRTC will be set up for this mode,
419          * with the panel scaling set up to source from the H/VDisplay
420          * of the original mode.
421          */
422         if (panel_fixed_mode != NULL) {
423                 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
424                 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
425                 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
426                 adjusted_mode->htotal = panel_fixed_mode->htotal;
427                 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
428                 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
429                 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
430                 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
431                 adjusted_mode->clock = panel_fixed_mode->clock;
432                 drm_mode_set_crtcinfo(adjusted_mode,
433                                       CRTC_INTERLACE_HALVE_V);
434         }
435
436         /*
437          * XXX: It would be nice to support lower refresh rates on the
438          * panels to reduce power consumption, and perhaps match the
439          * user's requested refresh rate.
440          */
441
442         return true;
443 }
444
445 void psb_intel_lvds_prepare(struct drm_encoder *encoder)
446 {
447         struct drm_device *dev = encoder->dev;
448         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
449         struct psb_intel_mode_device *mode_dev = output->mode_dev;
450
451         PSB_DEBUG_ENTRY("\n");
452
453         if (!gma_power_begin(dev, true))
454                 return;
455
456         mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
457         mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
458                                           BACKLIGHT_DUTY_CYCLE_MASK);
459
460         psb_intel_lvds_set_power(dev, output, false);
461
462         gma_power_end(dev);
463 }
464
465 void psb_intel_lvds_commit(struct drm_encoder *encoder)
466 {
467         struct drm_device *dev = encoder->dev;
468         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
469         struct psb_intel_mode_device *mode_dev = output->mode_dev;
470
471         PSB_DEBUG_ENTRY("\n");
472
473         if (mode_dev->backlight_duty_cycle == 0)
474                 mode_dev->backlight_duty_cycle =
475                     psb_intel_lvds_get_max_backlight(dev);
476
477         psb_intel_lvds_set_power(dev, output, true);
478 }
479
480 static void psb_intel_lvds_mode_set(struct drm_encoder *encoder,
481                                 struct drm_display_mode *mode,
482                                 struct drm_display_mode *adjusted_mode)
483 {
484         struct psb_intel_mode_device *mode_dev =
485             enc_to_psb_intel_output(encoder)->mode_dev;
486         struct drm_device *dev = encoder->dev;
487         u32 pfit_control;
488
489         /*
490          * The LVDS pin pair will already have been turned on in the
491          * psb_intel_crtc_mode_set since it has a large impact on the DPLL
492          * settings.
493          */
494
495         /*
496          * Enable automatic panel scaling so that non-native modes fill the
497          * screen.  Should be enabled before the pipe is enabled, according to
498          * register description and PRM.
499          */
500         if (mode->hdisplay != adjusted_mode->hdisplay ||
501             mode->vdisplay != adjusted_mode->vdisplay)
502                 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
503                                 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
504                                 HORIZ_INTERP_BILINEAR);
505         else
506                 pfit_control = 0;
507
508         if (mode_dev->panel_wants_dither)
509                 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
510
511         REG_WRITE(PFIT_CONTROL, pfit_control);
512 }
513
514 /**
515  * Detect the LVDS connection.
516  *
517  * This always returns CONNECTOR_STATUS_CONNECTED.
518  * This connector should only have
519  * been set up if the LVDS was actually connected anyway.
520  */
521 static enum drm_connector_status psb_intel_lvds_detect(struct drm_connector
522                                                    *connector, bool force)
523 {
524         return connector_status_connected;
525 }
526
527 /**
528  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
529  */
530 static int psb_intel_lvds_get_modes(struct drm_connector *connector)
531 {
532         struct drm_device *dev = connector->dev;
533         struct psb_intel_output *psb_intel_output =
534                                         to_psb_intel_output(connector);
535         struct psb_intel_mode_device *mode_dev =
536                                         psb_intel_output->mode_dev;
537         int ret = 0;
538
539         ret = psb_intel_ddc_get_modes(psb_intel_output);
540
541         if (ret)
542                 return ret;
543
544         /* Didn't get an EDID, so
545          * Set wide sync ranges so we get all modes
546          * handed to valid_mode for checking
547          */
548         connector->display_info.min_vfreq = 0;
549         connector->display_info.max_vfreq = 200;
550         connector->display_info.min_hfreq = 0;
551         connector->display_info.max_hfreq = 200;
552
553         if (mode_dev->panel_fixed_mode != NULL) {
554                 struct drm_display_mode *mode =
555                     drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
556                 drm_mode_probed_add(connector, mode);
557                 return 1;
558         }
559
560         return 0;
561 }
562
563 /**
564  * psb_intel_lvds_destroy - unregister and free LVDS structures
565  * @connector: connector to free
566  *
567  * Unregister the DDC bus for this connector then free the driver private
568  * structure.
569  */
570 void psb_intel_lvds_destroy(struct drm_connector *connector)
571 {
572         struct psb_intel_output *psb_intel_output =
573                                         to_psb_intel_output(connector);
574
575         if (psb_intel_output->ddc_bus)
576                 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
577         drm_sysfs_connector_remove(connector);
578         drm_connector_cleanup(connector);
579         kfree(connector);
580 }
581
582 int psb_intel_lvds_set_property(struct drm_connector *connector,
583                                        struct drm_property *property,
584                                        uint64_t value)
585 {
586         struct drm_encoder *pEncoder = connector->encoder;
587
588         PSB_DEBUG_ENTRY("\n");
589
590         if (!strcmp(property->name, "scaling mode") && pEncoder) {
591                 struct psb_intel_crtc *pPsbCrtc =
592                                         to_psb_intel_crtc(pEncoder->crtc);
593                 uint64_t curValue;
594
595                 PSB_DEBUG_ENTRY("scaling mode\n");
596
597                 if (!pPsbCrtc)
598                         goto set_prop_error;
599
600                 switch (value) {
601                 case DRM_MODE_SCALE_FULLSCREEN:
602                         break;
603                 case DRM_MODE_SCALE_NO_SCALE:
604                         break;
605                 case DRM_MODE_SCALE_ASPECT:
606                         break;
607                 default:
608                         goto set_prop_error;
609                 }
610
611                 if (drm_connector_property_get_value(connector,
612                                                      property,
613                                                      &curValue))
614                         goto set_prop_error;
615
616                 if (curValue == value)
617                         goto set_prop_done;
618
619                 if (drm_connector_property_set_value(connector,
620                                                         property,
621                                                         value))
622                         goto set_prop_error;
623
624                 if (pPsbCrtc->saved_mode.hdisplay != 0 &&
625                     pPsbCrtc->saved_mode.vdisplay != 0) {
626                         if (!drm_crtc_helper_set_mode(pEncoder->crtc,
627                                                       &pPsbCrtc->saved_mode,
628                                                       pEncoder->crtc->x,
629                                                       pEncoder->crtc->y,
630                                                       pEncoder->crtc->fb))
631                                 goto set_prop_error;
632                 }
633         } else if (!strcmp(property->name, "backlight") && pEncoder) {
634                 PSB_DEBUG_ENTRY("backlight\n");
635
636                 if (drm_connector_property_set_value(connector,
637                                                         property,
638                                                         value))
639                         goto set_prop_error;
640                 else {
641 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
642                         struct backlight_device bd;
643                         bd.props.brightness = value;
644                         psb_set_brightness(&bd);
645 #endif
646                 }
647         } else if (!strcmp(property->name, "DPMS") && pEncoder) {
648                 struct drm_encoder_helper_funcs *pEncHFuncs
649                                                 = pEncoder->helper_private;
650                 PSB_DEBUG_ENTRY("DPMS\n");
651                 pEncHFuncs->dpms(pEncoder, value);
652         }
653
654 set_prop_done:
655         return 0;
656 set_prop_error:
657         return -1;
658 }
659
660 static const struct drm_encoder_helper_funcs psb_intel_lvds_helper_funcs = {
661         .dpms = psb_intel_lvds_encoder_dpms,
662         .mode_fixup = psb_intel_lvds_mode_fixup,
663         .prepare = psb_intel_lvds_prepare,
664         .mode_set = psb_intel_lvds_mode_set,
665         .commit = psb_intel_lvds_commit,
666 };
667
668 const struct drm_connector_helper_funcs
669                                 psb_intel_lvds_connector_helper_funcs = {
670         .get_modes = psb_intel_lvds_get_modes,
671         .mode_valid = psb_intel_lvds_mode_valid,
672         .best_encoder = psb_intel_best_encoder,
673 };
674
675 const struct drm_connector_funcs psb_intel_lvds_connector_funcs = {
676         .dpms = drm_helper_connector_dpms,
677         .save = psb_intel_lvds_save,
678         .restore = psb_intel_lvds_restore,
679         .detect = psb_intel_lvds_detect,
680         .fill_modes = drm_helper_probe_single_connector_modes,
681         .set_property = psb_intel_lvds_set_property,
682         .destroy = psb_intel_lvds_destroy,
683 };
684
685
686 static void psb_intel_lvds_enc_destroy(struct drm_encoder *encoder)
687 {
688         drm_encoder_cleanup(encoder);
689 }
690
691 const struct drm_encoder_funcs psb_intel_lvds_enc_funcs = {
692         .destroy = psb_intel_lvds_enc_destroy,
693 };
694
695
696
697 /**
698  * psb_intel_lvds_init - setup LVDS connectors on this device
699  * @dev: drm device
700  *
701  * Create the connector, register the LVDS DDC bus, and try to figure out what
702  * modes we can display on the LVDS panel (if present).
703  */
704 void psb_intel_lvds_init(struct drm_device *dev,
705                      struct psb_intel_mode_device *mode_dev)
706 {
707         struct psb_intel_output *psb_intel_output;
708         struct psb_intel_lvds_priv *lvds_priv;
709         struct drm_connector *connector;
710         struct drm_encoder *encoder;
711         struct drm_display_mode *scan;  /* *modes, *bios_mode; */
712         struct drm_crtc *crtc;
713         struct drm_psb_private *dev_priv =
714                                 (struct drm_psb_private *)dev->dev_private;
715         u32 lvds;
716         int pipe;
717
718         psb_intel_output = kzalloc(sizeof(struct psb_intel_output), GFP_KERNEL);
719         if (!psb_intel_output)
720                 return;
721
722         lvds_priv = kzalloc(sizeof(struct psb_intel_lvds_priv), GFP_KERNEL);
723         if (!lvds_priv) {
724                 kfree(psb_intel_output);
725                 DRM_DEBUG("LVDS private allocation error\n");
726                 return;
727         }
728
729         psb_intel_output->dev_priv = lvds_priv;
730
731         psb_intel_output->mode_dev = mode_dev;
732         connector = &psb_intel_output->base;
733         encoder = &psb_intel_output->enc;
734         drm_connector_init(dev, &psb_intel_output->base,
735                            &psb_intel_lvds_connector_funcs,
736                            DRM_MODE_CONNECTOR_LVDS);
737
738         drm_encoder_init(dev, &psb_intel_output->enc,
739                          &psb_intel_lvds_enc_funcs,
740                          DRM_MODE_ENCODER_LVDS);
741
742         drm_mode_connector_attach_encoder(&psb_intel_output->base,
743                                           &psb_intel_output->enc);
744         psb_intel_output->type = INTEL_OUTPUT_LVDS;
745
746         drm_encoder_helper_add(encoder, &psb_intel_lvds_helper_funcs);
747         drm_connector_helper_add(connector,
748                                  &psb_intel_lvds_connector_helper_funcs);
749         connector->display_info.subpixel_order = SubPixelHorizontalRGB;
750         connector->interlace_allowed = false;
751         connector->doublescan_allowed = false;
752
753         /*Attach connector properties*/
754         drm_connector_attach_property(connector,
755                                       dev->mode_config.scaling_mode_property,
756                                       DRM_MODE_SCALE_FULLSCREEN);
757         drm_connector_attach_property(connector,
758                                       dev_priv->backlight_property,
759                                       BRIGHTNESS_MAX_LEVEL);
760
761         /**
762          * Set up I2C bus
763          * FIXME: distroy i2c_bus when exit
764          */
765         psb_intel_output->i2c_bus = psb_intel_i2c_create(dev,
766                                                          GPIOB,
767                                                          "LVDSBLC_B");
768         if (!psb_intel_output->i2c_bus) {
769                 dev_printk(KERN_ERR,
770                         &dev->pdev->dev, "I2C bus registration failed.\n");
771                 goto failed_blc_i2c;
772         }
773         psb_intel_output->i2c_bus->slave_addr = 0x2C;
774         dev_priv->lvds_i2c_bus =  psb_intel_output->i2c_bus;
775
776         /*
777          * LVDS discovery:
778          * 1) check for EDID on DDC
779          * 2) check for VBT data
780          * 3) check to see if LVDS is already on
781          *    if none of the above, no panel
782          * 4) make sure lid is open
783          *    if closed, act like it's not there for now
784          */
785
786         /* Set up the DDC bus. */
787         psb_intel_output->ddc_bus = psb_intel_i2c_create(dev,
788                                                          GPIOC,
789                                                          "LVDSDDC_C");
790         if (!psb_intel_output->ddc_bus) {
791                 dev_printk(KERN_ERR, &dev->pdev->dev,
792                            "DDC bus registration " "failed.\n");
793                 goto failed_ddc;
794         }
795
796         /*
797          * Attempt to get the fixed panel mode from DDC.  Assume that the
798          * preferred mode is the right one.
799          */
800         psb_intel_ddc_get_modes(psb_intel_output);
801         list_for_each_entry(scan, &connector->probed_modes, head) {
802                 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
803                         mode_dev->panel_fixed_mode =
804                             drm_mode_duplicate(dev, scan);
805                         goto out;       /* FIXME: check for quirks */
806                 }
807         }
808
809         /* Failed to get EDID, what about VBT? do we need this?*/
810         if (mode_dev->vbt_mode)
811                 mode_dev->panel_fixed_mode =
812                     drm_mode_duplicate(dev, mode_dev->vbt_mode);
813
814         if (!mode_dev->panel_fixed_mode)
815                 if (dev_priv->lfp_lvds_vbt_mode)
816                         mode_dev->panel_fixed_mode =
817                                 drm_mode_duplicate(dev,
818                                         dev_priv->lfp_lvds_vbt_mode);
819
820         /*
821          * If we didn't get EDID, try checking if the panel is already turned
822          * on.  If so, assume that whatever is currently programmed is the
823          * correct mode.
824          */
825         lvds = REG_READ(LVDS);
826         pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
827         crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
828
829         if (crtc && (lvds & LVDS_PORT_EN)) {
830                 mode_dev->panel_fixed_mode =
831                     psb_intel_crtc_mode_get(dev, crtc);
832                 if (mode_dev->panel_fixed_mode) {
833                         mode_dev->panel_fixed_mode->type |=
834                             DRM_MODE_TYPE_PREFERRED;
835                         goto out;       /* FIXME: check for quirks */
836                 }
837         }
838
839         /* If we still don't have a mode after all that, give up. */
840         if (!mode_dev->panel_fixed_mode) {
841                 DRM_DEBUG
842                         ("Found no modes on the lvds, ignoring the LVDS\n");
843                 goto failed_find;
844         }
845
846         /*
847          * Blacklist machines with BIOSes that list an LVDS panel without
848          * actually having one.
849          */
850 out:
851         drm_sysfs_connector_add(connector);
852
853         PSB_DEBUG_ENTRY("hdisplay = %d\n",
854                  mode_dev->panel_fixed_mode->hdisplay);
855         PSB_DEBUG_ENTRY(" vdisplay = %d\n",
856                  mode_dev->panel_fixed_mode->vdisplay);
857         PSB_DEBUG_ENTRY(" hsync_start = %d\n",
858                  mode_dev->panel_fixed_mode->hsync_start);
859         PSB_DEBUG_ENTRY(" hsync_end = %d\n",
860                  mode_dev->panel_fixed_mode->hsync_end);
861         PSB_DEBUG_ENTRY(" htotal = %d\n",
862                  mode_dev->panel_fixed_mode->htotal);
863         PSB_DEBUG_ENTRY(" vsync_start = %d\n",
864                  mode_dev->panel_fixed_mode->vsync_start);
865         PSB_DEBUG_ENTRY(" vsync_end = %d\n",
866                  mode_dev->panel_fixed_mode->vsync_end);
867         PSB_DEBUG_ENTRY(" vtotal = %d\n",
868                  mode_dev->panel_fixed_mode->vtotal);
869         PSB_DEBUG_ENTRY(" clock = %d\n",
870                  mode_dev->panel_fixed_mode->clock);
871
872         return;
873
874 failed_find:
875         if (psb_intel_output->ddc_bus)
876                 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
877 failed_ddc:
878         if (psb_intel_output->i2c_bus)
879                 psb_intel_i2c_destroy(psb_intel_output->i2c_bus);
880 failed_blc_i2c:
881         drm_encoder_cleanup(encoder);
882         drm_connector_cleanup(connector);
883         kfree(connector);
884 }
885