gma500: do a pass over the FIXME tags
[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         /* PSB requires the LVDS is on pipe B, MRST has only one pipe anyway */
386         if (!IS_MRST(dev) && psb_intel_crtc->pipe == 0) {
387                 printk(KERN_ERR "Can't support LVDS on pipe A\n");
388                 return false;
389         }
390         if (IS_MRST(dev) && psb_intel_crtc->pipe != 0) {
391                 printk(KERN_ERR "Must use PIPE A\n");
392                 return false;
393         }
394         /* Should never happen!! */
395         list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
396                             head) {
397                 if (tmp_encoder != encoder
398                     && tmp_encoder->crtc == encoder->crtc) {
399                         printk(KERN_ERR "Can't enable LVDS and another "
400                                "encoder on the same pipe\n");
401                         return false;
402                 }
403         }
404
405         /*
406          * If we have timings from the BIOS for the panel, put them in
407          * to the adjusted mode.  The CRTC will be set up for this mode,
408          * with the panel scaling set up to source from the H/VDisplay
409          * of the original mode.
410          */
411         if (panel_fixed_mode != NULL) {
412                 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
413                 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
414                 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
415                 adjusted_mode->htotal = panel_fixed_mode->htotal;
416                 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
417                 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
418                 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
419                 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
420                 adjusted_mode->clock = panel_fixed_mode->clock;
421                 drm_mode_set_crtcinfo(adjusted_mode,
422                                       CRTC_INTERLACE_HALVE_V);
423         }
424
425         /*
426          * XXX: It would be nice to support lower refresh rates on the
427          * panels to reduce power consumption, and perhaps match the
428          * user's requested refresh rate.
429          */
430
431         return true;
432 }
433
434 static void psb_intel_lvds_prepare(struct drm_encoder *encoder)
435 {
436         struct drm_device *dev = encoder->dev;
437         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
438         struct psb_intel_mode_device *mode_dev = output->mode_dev;
439
440         if (!gma_power_begin(dev, true))
441                 return;
442
443         mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
444         mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
445                                           BACKLIGHT_DUTY_CYCLE_MASK);
446
447         psb_intel_lvds_set_power(dev, output, false);
448
449         gma_power_end(dev);
450 }
451
452 static void psb_intel_lvds_commit(struct drm_encoder *encoder)
453 {
454         struct drm_device *dev = encoder->dev;
455         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
456         struct psb_intel_mode_device *mode_dev = output->mode_dev;
457
458         if (mode_dev->backlight_duty_cycle == 0)
459                 mode_dev->backlight_duty_cycle =
460                     psb_intel_lvds_get_max_backlight(dev);
461
462         psb_intel_lvds_set_power(dev, output, true);
463 }
464
465 static void psb_intel_lvds_mode_set(struct drm_encoder *encoder,
466                                 struct drm_display_mode *mode,
467                                 struct drm_display_mode *adjusted_mode)
468 {
469         struct drm_device *dev = encoder->dev;
470         struct drm_psb_private *dev_priv = dev->dev_private;
471         u32 pfit_control;
472
473         /*
474          * The LVDS pin pair will already have been turned on in the
475          * psb_intel_crtc_mode_set since it has a large impact on the DPLL
476          * settings.
477          */
478
479         /*
480          * Enable automatic panel scaling so that non-native modes fill the
481          * screen.  Should be enabled before the pipe is enabled, according to
482          * register description and PRM.
483          */
484         if (mode->hdisplay != adjusted_mode->hdisplay ||
485             mode->vdisplay != adjusted_mode->vdisplay)
486                 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
487                                 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
488                                 HORIZ_INTERP_BILINEAR);
489         else
490                 pfit_control = 0;
491
492         if (dev_priv->lvds_dither)
493                 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
494
495         REG_WRITE(PFIT_CONTROL, pfit_control);
496 }
497
498 /*
499  * Detect the LVDS connection.
500  *
501  * This always returns CONNECTOR_STATUS_CONNECTED.
502  * This connector should only have
503  * been set up if the LVDS was actually connected anyway.
504  */
505 static enum drm_connector_status psb_intel_lvds_detect(struct drm_connector
506                                                    *connector, bool force)
507 {
508         return connector_status_connected;
509 }
510
511 /*
512  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
513  */
514 static int psb_intel_lvds_get_modes(struct drm_connector *connector)
515 {
516         struct drm_device *dev = connector->dev;
517         struct psb_intel_output *psb_intel_output =
518                                         to_psb_intel_output(connector);
519         struct psb_intel_mode_device *mode_dev =
520                                         psb_intel_output->mode_dev;
521         int ret = 0;
522
523         if (!IS_MRST(dev))
524                 ret = psb_intel_ddc_get_modes(psb_intel_output);
525
526         if (ret)
527                 return ret;
528
529         /* Didn't get an EDID, so
530          * Set wide sync ranges so we get all modes
531          * handed to valid_mode for checking
532          */
533         connector->display_info.min_vfreq = 0;
534         connector->display_info.max_vfreq = 200;
535         connector->display_info.min_hfreq = 0;
536         connector->display_info.max_hfreq = 200;
537
538         if (mode_dev->panel_fixed_mode != NULL) {
539                 struct drm_display_mode *mode =
540                     drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
541                 drm_mode_probed_add(connector, mode);
542                 return 1;
543         }
544
545         return 0;
546 }
547
548 /**
549  * psb_intel_lvds_destroy - unregister and free LVDS structures
550  * @connector: connector to free
551  *
552  * Unregister the DDC bus for this connector then free the driver private
553  * structure.
554  */
555 void psb_intel_lvds_destroy(struct drm_connector *connector)
556 {
557         struct psb_intel_output *psb_intel_output =
558                                         to_psb_intel_output(connector);
559
560         if (psb_intel_output->ddc_bus)
561                 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
562         drm_sysfs_connector_remove(connector);
563         drm_connector_cleanup(connector);
564         kfree(connector);
565 }
566
567 int psb_intel_lvds_set_property(struct drm_connector *connector,
568                                        struct drm_property *property,
569                                        uint64_t value)
570 {
571         struct drm_encoder *encoder = connector->encoder;
572
573         if (!encoder)
574                 return -1;
575
576         if (!strcmp(property->name, "scaling mode")) {
577                 struct psb_intel_crtc *crtc =
578                                         to_psb_intel_crtc(encoder->crtc);
579                 uint64_t curval;
580
581                 if (!crtc)
582                         goto set_prop_error;
583
584                 switch (value) {
585                 case DRM_MODE_SCALE_FULLSCREEN:
586                         break;
587                 case DRM_MODE_SCALE_NO_SCALE:
588                         break;
589                 case DRM_MODE_SCALE_ASPECT:
590                         break;
591                 default:
592                         goto set_prop_error;
593                 }
594
595                 if (drm_connector_property_get_value(connector,
596                                                      property,
597                                                      &curval))
598                         goto set_prop_error;
599
600                 if (curval == value)
601                         goto set_prop_done;
602
603                 if (drm_connector_property_set_value(connector,
604                                                         property,
605                                                         value))
606                         goto set_prop_error;
607
608                 if (crtc->saved_mode.hdisplay != 0 &&
609                     crtc->saved_mode.vdisplay != 0) {
610                         if (!drm_crtc_helper_set_mode(encoder->crtc,
611                                                       &crtc->saved_mode,
612                                                       encoder->crtc->x,
613                                                       encoder->crtc->y,
614                                                       encoder->crtc->fb))
615                                 goto set_prop_error;
616                 }
617         } else if (!strcmp(property->name, "backlight")) {
618                 if (drm_connector_property_set_value(connector,
619                                                         property,
620                                                         value))
621                         goto set_prop_error;
622                 else {
623 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
624                         struct drm_psb_private *devp = encoder->dev->dev_private;
625                         struct backlight_device *bd = devp->backlight_device;
626                         if (bd) {
627                                 bd->props.brightness = value;
628                                 backlight_update_status(bd);
629                         }
630 #endif
631                 }
632         } else if (!strcmp(property->name, "DPMS")) {
633                 struct drm_encoder_helper_funcs *hfuncs
634                                                 = encoder->helper_private;
635                 hfuncs->dpms(encoder, value);
636         }
637
638 set_prop_done:
639         return 0;
640 set_prop_error:
641         return -1;
642 }
643
644 static const struct drm_encoder_helper_funcs psb_intel_lvds_helper_funcs = {
645         .dpms = psb_intel_lvds_encoder_dpms,
646         .mode_fixup = psb_intel_lvds_mode_fixup,
647         .prepare = psb_intel_lvds_prepare,
648         .mode_set = psb_intel_lvds_mode_set,
649         .commit = psb_intel_lvds_commit,
650 };
651
652 const struct drm_connector_helper_funcs
653                                 psb_intel_lvds_connector_helper_funcs = {
654         .get_modes = psb_intel_lvds_get_modes,
655         .mode_valid = psb_intel_lvds_mode_valid,
656         .best_encoder = psb_intel_best_encoder,
657 };
658
659 const struct drm_connector_funcs psb_intel_lvds_connector_funcs = {
660         .dpms = drm_helper_connector_dpms,
661         .save = psb_intel_lvds_save,
662         .restore = psb_intel_lvds_restore,
663         .detect = psb_intel_lvds_detect,
664         .fill_modes = drm_helper_probe_single_connector_modes,
665         .set_property = psb_intel_lvds_set_property,
666         .destroy = psb_intel_lvds_destroy,
667 };
668
669
670 static void psb_intel_lvds_enc_destroy(struct drm_encoder *encoder)
671 {
672         drm_encoder_cleanup(encoder);
673 }
674
675 const struct drm_encoder_funcs psb_intel_lvds_enc_funcs = {
676         .destroy = psb_intel_lvds_enc_destroy,
677 };
678
679
680
681 /**
682  * psb_intel_lvds_init - setup LVDS connectors on this device
683  * @dev: drm device
684  *
685  * Create the connector, register the LVDS DDC bus, and try to figure out what
686  * modes we can display on the LVDS panel (if present).
687  */
688 void psb_intel_lvds_init(struct drm_device *dev,
689                      struct psb_intel_mode_device *mode_dev)
690 {
691         struct psb_intel_output *psb_intel_output;
692         struct psb_intel_lvds_priv *lvds_priv;
693         struct drm_connector *connector;
694         struct drm_encoder *encoder;
695         struct drm_display_mode *scan;  /* *modes, *bios_mode; */
696         struct drm_crtc *crtc;
697         struct drm_psb_private *dev_priv =
698                                 (struct drm_psb_private *)dev->dev_private;
699         u32 lvds;
700         int pipe;
701
702         psb_intel_output = kzalloc(sizeof(struct psb_intel_output), GFP_KERNEL);
703         if (!psb_intel_output)
704                 return;
705
706         lvds_priv = kzalloc(sizeof(struct psb_intel_lvds_priv), GFP_KERNEL);
707         if (!lvds_priv) {
708                 kfree(psb_intel_output);
709                 dev_err(dev->dev, "LVDS private allocation error\n");
710                 return;
711         }
712
713         psb_intel_output->dev_priv = lvds_priv;
714
715         psb_intel_output->mode_dev = mode_dev;
716         connector = &psb_intel_output->base;
717         encoder = &psb_intel_output->enc;
718         drm_connector_init(dev, &psb_intel_output->base,
719                            &psb_intel_lvds_connector_funcs,
720                            DRM_MODE_CONNECTOR_LVDS);
721
722         drm_encoder_init(dev, &psb_intel_output->enc,
723                          &psb_intel_lvds_enc_funcs,
724                          DRM_MODE_ENCODER_LVDS);
725
726         drm_mode_connector_attach_encoder(&psb_intel_output->base,
727                                           &psb_intel_output->enc);
728         psb_intel_output->type = INTEL_OUTPUT_LVDS;
729
730         drm_encoder_helper_add(encoder, &psb_intel_lvds_helper_funcs);
731         drm_connector_helper_add(connector,
732                                  &psb_intel_lvds_connector_helper_funcs);
733         connector->display_info.subpixel_order = SubPixelHorizontalRGB;
734         connector->interlace_allowed = false;
735         connector->doublescan_allowed = false;
736
737         /*Attach connector properties*/
738         drm_connector_attach_property(connector,
739                                       dev->mode_config.scaling_mode_property,
740                                       DRM_MODE_SCALE_FULLSCREEN);
741         drm_connector_attach_property(connector,
742                                       dev_priv->backlight_property,
743                                       BRIGHTNESS_MAX_LEVEL);
744
745         /*
746          * Set up I2C bus
747          * FIXME: distroy i2c_bus when exit
748          */
749         psb_intel_output->i2c_bus = psb_intel_i2c_create(dev,
750                                                          GPIOB,
751                                                          "LVDSBLC_B");
752         if (!psb_intel_output->i2c_bus) {
753                 dev_printk(KERN_ERR,
754                         &dev->pdev->dev, "I2C bus registration failed.\n");
755                 goto failed_blc_i2c;
756         }
757         psb_intel_output->i2c_bus->slave_addr = 0x2C;
758         dev_priv->lvds_i2c_bus =  psb_intel_output->i2c_bus;
759
760         /*
761          * LVDS discovery:
762          * 1) check for EDID on DDC
763          * 2) check for VBT data
764          * 3) check to see if LVDS is already on
765          *    if none of the above, no panel
766          * 4) make sure lid is open
767          *    if closed, act like it's not there for now
768          */
769
770         /* Set up the DDC bus. */
771         psb_intel_output->ddc_bus = psb_intel_i2c_create(dev,
772                                                          GPIOC,
773                                                          "LVDSDDC_C");
774         if (!psb_intel_output->ddc_bus) {
775                 dev_printk(KERN_ERR, &dev->pdev->dev,
776                            "DDC bus registration " "failed.\n");
777                 goto failed_ddc;
778         }
779
780         /*
781          * Attempt to get the fixed panel mode from DDC.  Assume that the
782          * preferred mode is the right one.
783          */
784         psb_intel_ddc_get_modes(psb_intel_output);
785         list_for_each_entry(scan, &connector->probed_modes, head) {
786                 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
787                         mode_dev->panel_fixed_mode =
788                             drm_mode_duplicate(dev, scan);
789                         goto out;       /* FIXME: check for quirks */
790                 }
791         }
792
793         /* Failed to get EDID, what about VBT? do we need this? */
794         if (mode_dev->vbt_mode)
795                 mode_dev->panel_fixed_mode =
796                     drm_mode_duplicate(dev, mode_dev->vbt_mode);
797
798         if (!mode_dev->panel_fixed_mode)
799                 if (dev_priv->lfp_lvds_vbt_mode)
800                         mode_dev->panel_fixed_mode =
801                                 drm_mode_duplicate(dev,
802                                         dev_priv->lfp_lvds_vbt_mode);
803
804         /*
805          * If we didn't get EDID, try checking if the panel is already turned
806          * on.  If so, assume that whatever is currently programmed is the
807          * correct mode.
808          */
809         lvds = REG_READ(LVDS);
810         pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
811         crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
812
813         if (crtc && (lvds & LVDS_PORT_EN)) {
814                 mode_dev->panel_fixed_mode =
815                     psb_intel_crtc_mode_get(dev, crtc);
816                 if (mode_dev->panel_fixed_mode) {
817                         mode_dev->panel_fixed_mode->type |=
818                             DRM_MODE_TYPE_PREFERRED;
819                         goto out;       /* FIXME: check for quirks */
820                 }
821         }
822
823         /* If we still don't have a mode after all that, give up. */
824         if (!mode_dev->panel_fixed_mode) {
825                 dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
826                 goto failed_find;
827         }
828
829         /*
830          * Blacklist machines with BIOSes that list an LVDS panel without
831          * actually having one.
832          */
833 out:
834         drm_sysfs_connector_add(connector);
835         return;
836
837 failed_find:
838         if (psb_intel_output->ddc_bus)
839                 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
840 failed_ddc:
841         if (psb_intel_output->i2c_bus)
842                 psb_intel_i2c_destroy(psb_intel_output->i2c_bus);
843 failed_blc_i2c:
844         drm_encoder_cleanup(encoder);
845         drm_connector_cleanup(connector);
846         kfree(connector);
847 }
848