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