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