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