gma500: continue abstracting platform specific code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / gma500 / psb_intel_lvds.c
blob41b96d2ee6625a6e9e82bb5ae68f5c0f17c658ce
1 /*
2 * Copyright © 2006-2007 Intel Corporation
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.
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.
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.
17 * Authors:
18 * Eric Anholt <eric@anholt.net>
19 * Dave Airlie <airlied@linux.ie>
20 * Jesse Barnes <jesse.barnes@intel.com>
23 #include <linux/i2c.h>
24 /* #include <drm/drm_crtc.h> */
25 /* #include <drm/drm_edid.h> */
26 #include <drm/drmP.h>
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>
35 u32 CoreClock;
36 u32 PWMControlRegFreq;
38 /**
39 * LVDS I2C backlight control macros
41 #define BRIGHTNESS_MAX_LEVEL 100
42 #define BRIGHTNESS_MASK 0xFF
43 #define BLC_I2C_TYPE 0x01
44 #define BLC_PWM_TYPT 0x02
46 #define BLC_POLARITY_NORMAL 0
47 #define BLC_POLARITY_INVERSE 1
49 #define PSB_BLC_MAX_PWM_REG_FREQ (0xFFFE)
50 #define PSB_BLC_MIN_PWM_REG_FREQ (0x2)
51 #define PSB_BLC_PWM_PRECISION_FACTOR (10)
52 #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
53 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
55 struct psb_intel_lvds_priv {
56 /**
57 * Saved LVDO output states
59 uint32_t savePP_ON;
60 uint32_t savePP_OFF;
61 uint32_t saveLVDS;
62 uint32_t savePP_CONTROL;
63 uint32_t savePP_CYCLE;
64 uint32_t savePFIT_CONTROL;
65 uint32_t savePFIT_PGM_RATIOS;
66 uint32_t saveBLC_PWM_CTL;
69 /* MRST defines end */
71 /**
72 * Returns the maximum level of the backlight duty cycle field.
74 static u32 psb_intel_lvds_get_max_backlight(struct drm_device *dev)
76 struct drm_psb_private *dev_priv = dev->dev_private;
77 u32 retVal;
79 if (gma_power_begin(dev, false)) {
80 retVal = ((REG_READ(BLC_PWM_CTL) &
81 BACKLIGHT_MODULATION_FREQ_MASK) >>
82 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
84 gma_power_end(dev);
85 } else
86 retVal = ((dev_priv->saveBLC_PWM_CTL &
87 BACKLIGHT_MODULATION_FREQ_MASK) >>
88 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
90 return retVal;
94 * Set LVDS backlight level by I2C command
96 * FIXME: at some point we need to both track this for PM and also
97 * disable runtime pm on MRST if the brightness is nil (ie blanked)
99 static int psb_lvds_i2c_set_brightness(struct drm_device *dev,
100 unsigned int level)
102 struct drm_psb_private *dev_priv =
103 (struct drm_psb_private *)dev->dev_private;
105 struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
106 u8 out_buf[2];
107 unsigned int blc_i2c_brightness;
109 struct i2c_msg msgs[] = {
111 .addr = lvds_i2c_bus->slave_addr,
112 .flags = 0,
113 .len = 2,
114 .buf = out_buf,
118 blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
119 BRIGHTNESS_MASK /
120 BRIGHTNESS_MAX_LEVEL);
122 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
123 blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
125 out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
126 out_buf[1] = (u8)blc_i2c_brightness;
128 if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1) {
129 dev_dbg(dev->dev, "I2C set brightness.(command, value) (%d, %d)\n",
130 dev_priv->lvds_bl->brightnesscmd,
131 blc_i2c_brightness);
132 return 0;
135 dev_err(dev->dev, "I2C transfer error\n");
136 return -1;
140 static int psb_lvds_pwm_set_brightness(struct drm_device *dev, int level)
142 struct drm_psb_private *dev_priv =
143 (struct drm_psb_private *)dev->dev_private;
145 u32 max_pwm_blc;
146 u32 blc_pwm_duty_cycle;
148 max_pwm_blc = psb_intel_lvds_get_max_backlight(dev);
150 /*BLC_PWM_CTL Should be initiated while backlight device init*/
151 BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
153 blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
155 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
156 blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
158 blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
159 REG_WRITE(BLC_PWM_CTL,
160 (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
161 (blc_pwm_duty_cycle));
163 return 0;
167 * Set LVDS backlight level either by I2C or PWM
169 void psb_intel_lvds_set_brightness(struct drm_device *dev, int level)
171 /*u32 blc_pwm_ctl;*/
172 struct drm_psb_private *dev_priv =
173 (struct drm_psb_private *)dev->dev_private;
175 dev_dbg(dev->dev, "backlight level is %d\n", level);
177 if (!dev_priv->lvds_bl) {
178 dev_err(dev->dev, "NO LVDS Backlight Info\n");
179 return;
182 if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
183 psb_lvds_i2c_set_brightness(dev, level);
184 else
185 psb_lvds_pwm_set_brightness(dev, level);
189 * Sets the backlight level.
191 * \param level backlight level, from 0 to psb_intel_lvds_get_max_backlight().
193 static void psb_intel_lvds_set_backlight(struct drm_device *dev, int level)
195 struct drm_psb_private *dev_priv = dev->dev_private;
196 u32 blc_pwm_ctl;
198 if (gma_power_begin(dev, false)) {
199 blc_pwm_ctl =
200 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
201 REG_WRITE(BLC_PWM_CTL,
202 (blc_pwm_ctl |
203 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
204 gma_power_end(dev);
205 } else {
206 blc_pwm_ctl = dev_priv->saveBLC_PWM_CTL &
207 ~BACKLIGHT_DUTY_CYCLE_MASK;
208 dev_priv->saveBLC_PWM_CTL = (blc_pwm_ctl |
209 (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
214 * Sets the power state for the panel.
216 static void psb_intel_lvds_set_power(struct drm_device *dev,
217 struct psb_intel_output *output, bool on)
219 u32 pp_status;
221 if (!gma_power_begin(dev, true))
222 return;
224 if (on) {
225 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
226 POWER_TARGET_ON);
227 do {
228 pp_status = REG_READ(PP_STATUS);
229 } while ((pp_status & PP_ON) == 0);
231 psb_intel_lvds_set_backlight(dev,
232 output->
233 mode_dev->backlight_duty_cycle);
234 } else {
235 psb_intel_lvds_set_backlight(dev, 0);
237 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
238 ~POWER_TARGET_ON);
239 do {
240 pp_status = REG_READ(PP_STATUS);
241 } while (pp_status & PP_ON);
244 gma_power_end(dev);
247 static void psb_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
249 struct drm_device *dev = encoder->dev;
250 struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
252 if (mode == DRM_MODE_DPMS_ON)
253 psb_intel_lvds_set_power(dev, output, true);
254 else
255 psb_intel_lvds_set_power(dev, output, false);
257 /* XXX: We never power down the LVDS pairs. */
260 static void psb_intel_lvds_save(struct drm_connector *connector)
262 struct drm_device *dev = connector->dev;
263 struct drm_psb_private *dev_priv =
264 (struct drm_psb_private *)dev->dev_private;
265 struct psb_intel_output *psb_intel_output =
266 to_psb_intel_output(connector);
267 struct psb_intel_lvds_priv *lvds_priv =
268 (struct psb_intel_lvds_priv *)psb_intel_output->dev_priv;
270 lvds_priv->savePP_ON = REG_READ(LVDSPP_ON);
271 lvds_priv->savePP_OFF = REG_READ(LVDSPP_OFF);
272 lvds_priv->saveLVDS = REG_READ(LVDS);
273 lvds_priv->savePP_CONTROL = REG_READ(PP_CONTROL);
274 lvds_priv->savePP_CYCLE = REG_READ(PP_CYCLE);
275 /*lvds_priv->savePP_DIVISOR = REG_READ(PP_DIVISOR);*/
276 lvds_priv->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
277 lvds_priv->savePFIT_CONTROL = REG_READ(PFIT_CONTROL);
278 lvds_priv->savePFIT_PGM_RATIOS = REG_READ(PFIT_PGM_RATIOS);
280 /*TODO: move backlight_duty_cycle to psb_intel_lvds_priv*/
281 dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
282 BACKLIGHT_DUTY_CYCLE_MASK);
285 * If the light is off at server startup,
286 * just make it full brightness
288 if (dev_priv->backlight_duty_cycle == 0)
289 dev_priv->backlight_duty_cycle =
290 psb_intel_lvds_get_max_backlight(dev);
292 dev_dbg(dev->dev, "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
293 lvds_priv->savePP_ON,
294 lvds_priv->savePP_OFF,
295 lvds_priv->saveLVDS,
296 lvds_priv->savePP_CONTROL,
297 lvds_priv->savePP_CYCLE,
298 lvds_priv->saveBLC_PWM_CTL);
301 static void psb_intel_lvds_restore(struct drm_connector *connector)
303 struct drm_device *dev = connector->dev;
304 u32 pp_status;
306 /*struct drm_psb_private *dev_priv =
307 (struct drm_psb_private *)dev->dev_private;*/
308 struct psb_intel_output *psb_intel_output =
309 to_psb_intel_output(connector);
310 struct psb_intel_lvds_priv *lvds_priv =
311 (struct psb_intel_lvds_priv *)psb_intel_output->dev_priv;
313 dev_dbg(dev->dev, "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
314 lvds_priv->savePP_ON,
315 lvds_priv->savePP_OFF,
316 lvds_priv->saveLVDS,
317 lvds_priv->savePP_CONTROL,
318 lvds_priv->savePP_CYCLE,
319 lvds_priv->saveBLC_PWM_CTL);
321 REG_WRITE(BLC_PWM_CTL, lvds_priv->saveBLC_PWM_CTL);
322 REG_WRITE(PFIT_CONTROL, lvds_priv->savePFIT_CONTROL);
323 REG_WRITE(PFIT_PGM_RATIOS, lvds_priv->savePFIT_PGM_RATIOS);
324 REG_WRITE(LVDSPP_ON, lvds_priv->savePP_ON);
325 REG_WRITE(LVDSPP_OFF, lvds_priv->savePP_OFF);
326 /*REG_WRITE(PP_DIVISOR, lvds_priv->savePP_DIVISOR);*/
327 REG_WRITE(PP_CYCLE, lvds_priv->savePP_CYCLE);
328 REG_WRITE(PP_CONTROL, lvds_priv->savePP_CONTROL);
329 REG_WRITE(LVDS, lvds_priv->saveLVDS);
331 if (lvds_priv->savePP_CONTROL & POWER_TARGET_ON) {
332 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
333 POWER_TARGET_ON);
334 do {
335 pp_status = REG_READ(PP_STATUS);
336 } while ((pp_status & PP_ON) == 0);
337 } else {
338 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
339 ~POWER_TARGET_ON);
340 do {
341 pp_status = REG_READ(PP_STATUS);
342 } while (pp_status & PP_ON);
346 int psb_intel_lvds_mode_valid(struct drm_connector *connector,
347 struct drm_display_mode *mode)
349 struct psb_intel_output *psb_intel_output =
350 to_psb_intel_output(connector);
351 struct drm_display_mode *fixed_mode =
352 psb_intel_output->mode_dev->panel_fixed_mode;
354 if (psb_intel_output->type == INTEL_OUTPUT_MIPI2)
355 fixed_mode = psb_intel_output->mode_dev->panel_fixed_mode2;
357 /* just in case */
358 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
359 return MODE_NO_DBLESCAN;
361 /* just in case */
362 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
363 return MODE_NO_INTERLACE;
365 if (fixed_mode) {
366 if (mode->hdisplay > fixed_mode->hdisplay)
367 return MODE_PANEL;
368 if (mode->vdisplay > fixed_mode->vdisplay)
369 return MODE_PANEL;
371 return MODE_OK;
374 bool psb_intel_lvds_mode_fixup(struct drm_encoder *encoder,
375 struct drm_display_mode *mode,
376 struct drm_display_mode *adjusted_mode)
378 struct psb_intel_mode_device *mode_dev =
379 enc_to_psb_intel_output(encoder)->mode_dev;
380 struct drm_device *dev = encoder->dev;
381 struct psb_intel_crtc *psb_intel_crtc =
382 to_psb_intel_crtc(encoder->crtc);
383 struct drm_encoder *tmp_encoder;
384 struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
385 struct psb_intel_output *psb_intel_output =
386 enc_to_psb_intel_output(encoder);
388 if (psb_intel_output->type == INTEL_OUTPUT_MIPI2)
389 panel_fixed_mode = mode_dev->panel_fixed_mode2;
391 /* FIXME: review for Medfield */
392 /* PSB requires the LVDS is on pipe B, MRST has only one pipe anyway */
393 if (!IS_MRST(dev) && psb_intel_crtc->pipe == 0) {
394 printk(KERN_ERR "Can't support LVDS on pipe A\n");
395 return false;
397 if (IS_MRST(dev) && psb_intel_crtc->pipe != 0) {
398 printk(KERN_ERR "Must use PIPE A\n");
399 return false;
401 /* Should never happen!! */
402 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
403 head) {
404 if (tmp_encoder != encoder
405 && tmp_encoder->crtc == encoder->crtc) {
406 printk(KERN_ERR "Can't enable LVDS and another "
407 "encoder on the same pipe\n");
408 return false;
413 * If we have timings from the BIOS for the panel, put them in
414 * to the adjusted mode. The CRTC will be set up for this mode,
415 * with the panel scaling set up to source from the H/VDisplay
416 * of the original mode.
418 if (panel_fixed_mode != NULL) {
419 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
420 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
421 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
422 adjusted_mode->htotal = panel_fixed_mode->htotal;
423 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
424 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
425 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
426 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
427 adjusted_mode->clock = panel_fixed_mode->clock;
428 drm_mode_set_crtcinfo(adjusted_mode,
429 CRTC_INTERLACE_HALVE_V);
433 * XXX: It would be nice to support lower refresh rates on the
434 * panels to reduce power consumption, and perhaps match the
435 * user's requested refresh rate.
438 return true;
441 void psb_intel_lvds_prepare(struct drm_encoder *encoder)
443 struct drm_device *dev = encoder->dev;
444 struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
445 struct psb_intel_mode_device *mode_dev = output->mode_dev;
447 if (!gma_power_begin(dev, true))
448 return;
450 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
451 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
452 BACKLIGHT_DUTY_CYCLE_MASK);
454 psb_intel_lvds_set_power(dev, output, false);
456 gma_power_end(dev);
459 void psb_intel_lvds_commit(struct drm_encoder *encoder)
461 struct drm_device *dev = encoder->dev;
462 struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
463 struct psb_intel_mode_device *mode_dev = output->mode_dev;
465 if (mode_dev->backlight_duty_cycle == 0)
466 mode_dev->backlight_duty_cycle =
467 psb_intel_lvds_get_max_backlight(dev);
469 psb_intel_lvds_set_power(dev, output, true);
472 static void psb_intel_lvds_mode_set(struct drm_encoder *encoder,
473 struct drm_display_mode *mode,
474 struct drm_display_mode *adjusted_mode)
476 struct psb_intel_mode_device *mode_dev =
477 enc_to_psb_intel_output(encoder)->mode_dev;
478 struct drm_device *dev = encoder->dev;
479 u32 pfit_control;
482 * The LVDS pin pair will already have been turned on in the
483 * psb_intel_crtc_mode_set since it has a large impact on the DPLL
484 * settings.
488 * Enable automatic panel scaling so that non-native modes fill the
489 * screen. Should be enabled before the pipe is enabled, according to
490 * register description and PRM.
492 if (mode->hdisplay != adjusted_mode->hdisplay ||
493 mode->vdisplay != adjusted_mode->vdisplay)
494 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
495 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
496 HORIZ_INTERP_BILINEAR);
497 else
498 pfit_control = 0;
500 if (mode_dev->panel_wants_dither)
501 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
503 REG_WRITE(PFIT_CONTROL, pfit_control);
507 * Detect the LVDS connection.
509 * This always returns CONNECTOR_STATUS_CONNECTED.
510 * This connector should only have
511 * been set up if the LVDS was actually connected anyway.
513 static enum drm_connector_status psb_intel_lvds_detect(struct drm_connector
514 *connector, bool force)
516 return connector_status_connected;
520 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
522 static int psb_intel_lvds_get_modes(struct drm_connector *connector)
524 struct drm_device *dev = connector->dev;
525 struct psb_intel_output *psb_intel_output =
526 to_psb_intel_output(connector);
527 struct psb_intel_mode_device *mode_dev =
528 psb_intel_output->mode_dev;
529 int ret = 0;
531 ret = psb_intel_ddc_get_modes(psb_intel_output);
533 if (ret)
534 return ret;
536 /* Didn't get an EDID, so
537 * Set wide sync ranges so we get all modes
538 * handed to valid_mode for checking
540 connector->display_info.min_vfreq = 0;
541 connector->display_info.max_vfreq = 200;
542 connector->display_info.min_hfreq = 0;
543 connector->display_info.max_hfreq = 200;
545 if (mode_dev->panel_fixed_mode != NULL) {
546 struct drm_display_mode *mode =
547 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
548 drm_mode_probed_add(connector, mode);
549 return 1;
552 return 0;
556 * psb_intel_lvds_destroy - unregister and free LVDS structures
557 * @connector: connector to free
559 * Unregister the DDC bus for this connector then free the driver private
560 * structure.
562 void psb_intel_lvds_destroy(struct drm_connector *connector)
564 struct psb_intel_output *psb_intel_output =
565 to_psb_intel_output(connector);
567 if (psb_intel_output->ddc_bus)
568 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
569 drm_sysfs_connector_remove(connector);
570 drm_connector_cleanup(connector);
571 kfree(connector);
574 int psb_intel_lvds_set_property(struct drm_connector *connector,
575 struct drm_property *property,
576 uint64_t value)
578 struct drm_encoder *encoder = connector->encoder;
579 struct drm_psb_private *dev_priv = encoder->dev->dev_private;
581 if (!strcmp(property->name, "scaling mode") && encoder) {
582 struct psb_intel_crtc *pPsbCrtc =
583 to_psb_intel_crtc(encoder->crtc);
584 uint64_t curValue;
586 if (!pPsbCrtc)
587 goto set_prop_error;
589 switch (value) {
590 case DRM_MODE_SCALE_FULLSCREEN:
591 break;
592 case DRM_MODE_SCALE_NO_SCALE:
593 break;
594 case DRM_MODE_SCALE_ASPECT:
595 break;
596 default:
597 goto set_prop_error;
600 if (drm_connector_property_get_value(connector,
601 property,
602 &curValue))
603 goto set_prop_error;
605 if (curValue == value)
606 goto set_prop_done;
608 if (drm_connector_property_set_value(connector,
609 property,
610 value))
611 goto set_prop_error;
613 if (pPsbCrtc->saved_mode.hdisplay != 0 &&
614 pPsbCrtc->saved_mode.vdisplay != 0) {
615 if (!drm_crtc_helper_set_mode(encoder->crtc,
616 &pPsbCrtc->saved_mode,
617 encoder->crtc->x,
618 encoder->crtc->y,
619 encoder->crtc->fb))
620 goto set_prop_error;
622 } else if (!strcmp(property->name, "backlight") && encoder) {
623 if (drm_connector_property_set_value(connector,
624 property,
625 value))
626 goto set_prop_error;
627 else {
628 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
629 struct backlight_device *bd = dev_priv->backlight_device;
630 if (bd) {
631 bd->props.brightness = value;
632 backlight_update_status(bd);
634 #endif
636 } else if (!strcmp(property->name, "DPMS") && encoder) {
637 struct drm_encoder_helper_funcs *pEncHFuncs
638 = encoder->helper_private;
639 pEncHFuncs->dpms(encoder, value);
642 set_prop_done:
643 return 0;
644 set_prop_error:
645 return -1;
648 static const struct drm_encoder_helper_funcs psb_intel_lvds_helper_funcs = {
649 .dpms = psb_intel_lvds_encoder_dpms,
650 .mode_fixup = psb_intel_lvds_mode_fixup,
651 .prepare = psb_intel_lvds_prepare,
652 .mode_set = psb_intel_lvds_mode_set,
653 .commit = psb_intel_lvds_commit,
656 const struct drm_connector_helper_funcs
657 psb_intel_lvds_connector_helper_funcs = {
658 .get_modes = psb_intel_lvds_get_modes,
659 .mode_valid = psb_intel_lvds_mode_valid,
660 .best_encoder = psb_intel_best_encoder,
663 const struct drm_connector_funcs psb_intel_lvds_connector_funcs = {
664 .dpms = drm_helper_connector_dpms,
665 .save = psb_intel_lvds_save,
666 .restore = psb_intel_lvds_restore,
667 .detect = psb_intel_lvds_detect,
668 .fill_modes = drm_helper_probe_single_connector_modes,
669 .set_property = psb_intel_lvds_set_property,
670 .destroy = psb_intel_lvds_destroy,
674 static void psb_intel_lvds_enc_destroy(struct drm_encoder *encoder)
676 drm_encoder_cleanup(encoder);
679 const struct drm_encoder_funcs psb_intel_lvds_enc_funcs = {
680 .destroy = psb_intel_lvds_enc_destroy,
686 * psb_intel_lvds_init - setup LVDS connectors on this device
687 * @dev: drm device
689 * Create the connector, register the LVDS DDC bus, and try to figure out what
690 * modes we can display on the LVDS panel (if present).
692 void psb_intel_lvds_init(struct drm_device *dev,
693 struct psb_intel_mode_device *mode_dev)
695 struct psb_intel_output *psb_intel_output;
696 struct psb_intel_lvds_priv *lvds_priv;
697 struct drm_connector *connector;
698 struct drm_encoder *encoder;
699 struct drm_display_mode *scan; /* *modes, *bios_mode; */
700 struct drm_crtc *crtc;
701 struct drm_psb_private *dev_priv =
702 (struct drm_psb_private *)dev->dev_private;
703 u32 lvds;
704 int pipe;
706 psb_intel_output = kzalloc(sizeof(struct psb_intel_output), GFP_KERNEL);
707 if (!psb_intel_output)
708 return;
710 lvds_priv = kzalloc(sizeof(struct psb_intel_lvds_priv), GFP_KERNEL);
711 if (!lvds_priv) {
712 kfree(psb_intel_output);
713 dev_err(dev->dev, "LVDS private allocation error\n");
714 return;
717 psb_intel_output->dev_priv = lvds_priv;
719 psb_intel_output->mode_dev = mode_dev;
720 connector = &psb_intel_output->base;
721 encoder = &psb_intel_output->enc;
722 drm_connector_init(dev, &psb_intel_output->base,
723 &psb_intel_lvds_connector_funcs,
724 DRM_MODE_CONNECTOR_LVDS);
726 drm_encoder_init(dev, &psb_intel_output->enc,
727 &psb_intel_lvds_enc_funcs,
728 DRM_MODE_ENCODER_LVDS);
730 drm_mode_connector_attach_encoder(&psb_intel_output->base,
731 &psb_intel_output->enc);
732 psb_intel_output->type = INTEL_OUTPUT_LVDS;
734 drm_encoder_helper_add(encoder, &psb_intel_lvds_helper_funcs);
735 drm_connector_helper_add(connector,
736 &psb_intel_lvds_connector_helper_funcs);
737 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
738 connector->interlace_allowed = false;
739 connector->doublescan_allowed = false;
741 /*Attach connector properties*/
742 drm_connector_attach_property(connector,
743 dev->mode_config.scaling_mode_property,
744 DRM_MODE_SCALE_FULLSCREEN);
745 drm_connector_attach_property(connector,
746 dev_priv->backlight_property,
747 BRIGHTNESS_MAX_LEVEL);
750 * Set up I2C bus
751 * FIXME: distroy i2c_bus when exit
753 psb_intel_output->i2c_bus = psb_intel_i2c_create(dev,
754 GPIOB,
755 "LVDSBLC_B");
756 if (!psb_intel_output->i2c_bus) {
757 dev_printk(KERN_ERR,
758 &dev->pdev->dev, "I2C bus registration failed.\n");
759 goto failed_blc_i2c;
761 psb_intel_output->i2c_bus->slave_addr = 0x2C;
762 dev_priv->lvds_i2c_bus = psb_intel_output->i2c_bus;
765 * LVDS discovery:
766 * 1) check for EDID on DDC
767 * 2) check for VBT data
768 * 3) check to see if LVDS is already on
769 * if none of the above, no panel
770 * 4) make sure lid is open
771 * if closed, act like it's not there for now
774 /* Set up the DDC bus. */
775 psb_intel_output->ddc_bus = psb_intel_i2c_create(dev,
776 GPIOC,
777 "LVDSDDC_C");
778 if (!psb_intel_output->ddc_bus) {
779 dev_printk(KERN_ERR, &dev->pdev->dev,
780 "DDC bus registration " "failed.\n");
781 goto failed_ddc;
785 * Attempt to get the fixed panel mode from DDC. Assume that the
786 * preferred mode is the right one.
788 psb_intel_ddc_get_modes(psb_intel_output);
789 list_for_each_entry(scan, &connector->probed_modes, head) {
790 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
791 mode_dev->panel_fixed_mode =
792 drm_mode_duplicate(dev, scan);
793 goto out; /* FIXME: check for quirks */
797 /* Failed to get EDID, what about VBT? do we need this?*/
798 if (mode_dev->vbt_mode)
799 mode_dev->panel_fixed_mode =
800 drm_mode_duplicate(dev, mode_dev->vbt_mode);
802 if (!mode_dev->panel_fixed_mode)
803 if (dev_priv->lfp_lvds_vbt_mode)
804 mode_dev->panel_fixed_mode =
805 drm_mode_duplicate(dev,
806 dev_priv->lfp_lvds_vbt_mode);
809 * If we didn't get EDID, try checking if the panel is already turned
810 * on. If so, assume that whatever is currently programmed is the
811 * correct mode.
813 lvds = REG_READ(LVDS);
814 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
815 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
817 if (crtc && (lvds & LVDS_PORT_EN)) {
818 mode_dev->panel_fixed_mode =
819 psb_intel_crtc_mode_get(dev, crtc);
820 if (mode_dev->panel_fixed_mode) {
821 mode_dev->panel_fixed_mode->type |=
822 DRM_MODE_TYPE_PREFERRED;
823 goto out; /* FIXME: check for quirks */
827 /* If we still don't have a mode after all that, give up. */
828 if (!mode_dev->panel_fixed_mode) {
829 dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
830 goto failed_find;
834 * Blacklist machines with BIOSes that list an LVDS panel without
835 * actually having one.
837 out:
838 drm_sysfs_connector_add(connector);
839 return;
841 failed_find:
842 if (psb_intel_output->ddc_bus)
843 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
844 failed_ddc:
845 if (psb_intel_output->i2c_bus)
846 psb_intel_i2c_destroy(psb_intel_output->i2c_bus);
847 failed_blc_i2c:
848 drm_encoder_cleanup(encoder);
849 drm_connector_cleanup(connector);
850 kfree(connector);