Merge branch 'fix/hda' into for-linus
[linux-2.6] / drivers / gpu / drm / i915 / intel_lvds.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *      Eric Anholt <eric@anholt.net>
26  *      Dave Airlie <airlied@linux.ie>
27  *      Jesse Barnes <jesse.barnes@intel.com>
28  */
29
30 #include <linux/dmi.h>
31 #include <linux/i2c.h>
32 #include "drmP.h"
33 #include "drm.h"
34 #include "drm_crtc.h"
35 #include "drm_edid.h"
36 #include "intel_drv.h"
37 #include "i915_drm.h"
38 #include "i915_drv.h"
39
40 /**
41  * Sets the backlight level.
42  *
43  * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
44  */
45 static void intel_lvds_set_backlight(struct drm_device *dev, int level)
46 {
47         struct drm_i915_private *dev_priv = dev->dev_private;
48         u32 blc_pwm_ctl;
49
50         blc_pwm_ctl = I915_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
51         I915_WRITE(BLC_PWM_CTL, (blc_pwm_ctl |
52                                  (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
53 }
54
55 /**
56  * Returns the maximum level of the backlight duty cycle field.
57  */
58 static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
59 {
60         struct drm_i915_private *dev_priv = dev->dev_private;
61
62         return ((I915_READ(BLC_PWM_CTL) & BACKLIGHT_MODULATION_FREQ_MASK) >>
63                 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
64 }
65
66 /**
67  * Sets the power state for the panel.
68  */
69 static void intel_lvds_set_power(struct drm_device *dev, bool on)
70 {
71         struct drm_i915_private *dev_priv = dev->dev_private;
72         u32 pp_status;
73
74         if (on) {
75                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
76                            POWER_TARGET_ON);
77                 do {
78                         pp_status = I915_READ(PP_STATUS);
79                 } while ((pp_status & PP_ON) == 0);
80
81                 intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
82         } else {
83                 intel_lvds_set_backlight(dev, 0);
84
85                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) &
86                            ~POWER_TARGET_ON);
87                 do {
88                         pp_status = I915_READ(PP_STATUS);
89                 } while (pp_status & PP_ON);
90         }
91 }
92
93 static void intel_lvds_dpms(struct drm_encoder *encoder, int mode)
94 {
95         struct drm_device *dev = encoder->dev;
96
97         if (mode == DRM_MODE_DPMS_ON)
98                 intel_lvds_set_power(dev, true);
99         else
100                 intel_lvds_set_power(dev, false);
101
102         /* XXX: We never power down the LVDS pairs. */
103 }
104
105 static void intel_lvds_save(struct drm_connector *connector)
106 {
107         struct drm_device *dev = connector->dev;
108         struct drm_i915_private *dev_priv = dev->dev_private;
109
110         dev_priv->savePP_ON = I915_READ(PP_ON_DELAYS);
111         dev_priv->savePP_OFF = I915_READ(PP_OFF_DELAYS);
112         dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
113         dev_priv->savePP_DIVISOR = I915_READ(PP_DIVISOR);
114         dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
115         dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
116                                        BACKLIGHT_DUTY_CYCLE_MASK);
117
118         /*
119          * If the light is off at server startup, just make it full brightness
120          */
121         if (dev_priv->backlight_duty_cycle == 0)
122                 dev_priv->backlight_duty_cycle =
123                         intel_lvds_get_max_backlight(dev);
124 }
125
126 static void intel_lvds_restore(struct drm_connector *connector)
127 {
128         struct drm_device *dev = connector->dev;
129         struct drm_i915_private *dev_priv = dev->dev_private;
130
131         I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
132         I915_WRITE(PP_ON_DELAYS, dev_priv->savePP_ON);
133         I915_WRITE(PP_OFF_DELAYS, dev_priv->savePP_OFF);
134         I915_WRITE(PP_DIVISOR, dev_priv->savePP_DIVISOR);
135         I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
136         if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
137                 intel_lvds_set_power(dev, true);
138         else
139                 intel_lvds_set_power(dev, false);
140 }
141
142 static int intel_lvds_mode_valid(struct drm_connector *connector,
143                                  struct drm_display_mode *mode)
144 {
145         struct drm_device *dev = connector->dev;
146         struct drm_i915_private *dev_priv = dev->dev_private;
147         struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
148
149         if (fixed_mode) {
150                 if (mode->hdisplay > fixed_mode->hdisplay)
151                         return MODE_PANEL;
152                 if (mode->vdisplay > fixed_mode->vdisplay)
153                         return MODE_PANEL;
154         }
155
156         return MODE_OK;
157 }
158
159 static bool intel_lvds_mode_fixup(struct drm_encoder *encoder,
160                                   struct drm_display_mode *mode,
161                                   struct drm_display_mode *adjusted_mode)
162 {
163         struct drm_device *dev = encoder->dev;
164         struct drm_i915_private *dev_priv = dev->dev_private;
165         struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
166         struct drm_encoder *tmp_encoder;
167
168         /* Should never happen!! */
169         if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
170                 printk(KERN_ERR "Can't support LVDS on pipe A\n");
171                 return false;
172         }
173
174         /* Should never happen!! */
175         list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list, head) {
176                 if (tmp_encoder != encoder && tmp_encoder->crtc == encoder->crtc) {
177                         printk(KERN_ERR "Can't enable LVDS and another "
178                                "encoder on the same pipe\n");
179                         return false;
180                 }
181         }
182
183         /*
184          * If we have timings from the BIOS for the panel, put them in
185          * to the adjusted mode.  The CRTC will be set up for this mode,
186          * with the panel scaling set up to source from the H/VDisplay
187          * of the original mode.
188          */
189         if (dev_priv->panel_fixed_mode != NULL) {
190                 adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
191                 adjusted_mode->hsync_start =
192                         dev_priv->panel_fixed_mode->hsync_start;
193                 adjusted_mode->hsync_end =
194                         dev_priv->panel_fixed_mode->hsync_end;
195                 adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
196                 adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
197                 adjusted_mode->vsync_start =
198                         dev_priv->panel_fixed_mode->vsync_start;
199                 adjusted_mode->vsync_end =
200                         dev_priv->panel_fixed_mode->vsync_end;
201                 adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
202                 adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
203                 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
204         }
205
206         /*
207          * XXX: It would be nice to support lower refresh rates on the
208          * panels to reduce power consumption, and perhaps match the
209          * user's requested refresh rate.
210          */
211
212         return true;
213 }
214
215 static void intel_lvds_prepare(struct drm_encoder *encoder)
216 {
217         struct drm_device *dev = encoder->dev;
218         struct drm_i915_private *dev_priv = dev->dev_private;
219
220         dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
221         dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
222                                        BACKLIGHT_DUTY_CYCLE_MASK);
223
224         intel_lvds_set_power(dev, false);
225 }
226
227 static void intel_lvds_commit( struct drm_encoder *encoder)
228 {
229         struct drm_device *dev = encoder->dev;
230         struct drm_i915_private *dev_priv = dev->dev_private;
231
232         if (dev_priv->backlight_duty_cycle == 0)
233                 dev_priv->backlight_duty_cycle =
234                         intel_lvds_get_max_backlight(dev);
235
236         intel_lvds_set_power(dev, true);
237 }
238
239 static void intel_lvds_mode_set(struct drm_encoder *encoder,
240                                 struct drm_display_mode *mode,
241                                 struct drm_display_mode *adjusted_mode)
242 {
243         struct drm_device *dev = encoder->dev;
244         struct drm_i915_private *dev_priv = dev->dev_private;
245         struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
246         u32 pfit_control;
247
248         /*
249          * The LVDS pin pair will already have been turned on in the
250          * intel_crtc_mode_set since it has a large impact on the DPLL
251          * settings.
252          */
253
254         /*
255          * Enable automatic panel scaling so that non-native modes fill the
256          * screen.  Should be enabled before the pipe is enabled, according to
257          * register description and PRM.
258          */
259         if (mode->hdisplay != adjusted_mode->hdisplay ||
260             mode->vdisplay != adjusted_mode->vdisplay)
261                 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
262                                 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
263                                 HORIZ_INTERP_BILINEAR);
264         else
265                 pfit_control = 0;
266
267         if (!IS_I965G(dev)) {
268                 if (dev_priv->panel_wants_dither || dev_priv->lvds_dither)
269                         pfit_control |= PANEL_8TO6_DITHER_ENABLE;
270         }
271         else
272                 pfit_control |= intel_crtc->pipe << PFIT_PIPE_SHIFT;
273
274         I915_WRITE(PFIT_CONTROL, pfit_control);
275 }
276
277 /**
278  * Detect the LVDS connection.
279  *
280  * This always returns CONNECTOR_STATUS_CONNECTED.  This connector should only have
281  * been set up if the LVDS was actually connected anyway.
282  */
283 static enum drm_connector_status intel_lvds_detect(struct drm_connector *connector)
284 {
285         return connector_status_connected;
286 }
287
288 /**
289  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
290  */
291 static int intel_lvds_get_modes(struct drm_connector *connector)
292 {
293         struct drm_device *dev = connector->dev;
294         struct intel_output *intel_output = to_intel_output(connector);
295         struct drm_i915_private *dev_priv = dev->dev_private;
296         int ret = 0;
297
298         ret = intel_ddc_get_modes(intel_output);
299
300         if (ret)
301                 return ret;
302
303         /* Didn't get an EDID, so
304          * Set wide sync ranges so we get all modes
305          * handed to valid_mode for checking
306          */
307         connector->display_info.min_vfreq = 0;
308         connector->display_info.max_vfreq = 200;
309         connector->display_info.min_hfreq = 0;
310         connector->display_info.max_hfreq = 200;
311
312         if (dev_priv->panel_fixed_mode != NULL) {
313                 struct drm_display_mode *mode;
314
315                 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
316                 drm_mode_probed_add(connector, mode);
317
318                 return 1;
319         }
320
321         return 0;
322 }
323
324 /**
325  * intel_lvds_destroy - unregister and free LVDS structures
326  * @connector: connector to free
327  *
328  * Unregister the DDC bus for this connector then free the driver private
329  * structure.
330  */
331 static void intel_lvds_destroy(struct drm_connector *connector)
332 {
333         struct intel_output *intel_output = to_intel_output(connector);
334
335         if (intel_output->ddc_bus)
336                 intel_i2c_destroy(intel_output->ddc_bus);
337         drm_sysfs_connector_remove(connector);
338         drm_connector_cleanup(connector);
339         kfree(connector);
340 }
341
342 static int intel_lvds_set_property(struct drm_connector *connector,
343                                    struct drm_property *property,
344                                    uint64_t value)
345 {
346         struct drm_device *dev = connector->dev;
347
348         if (property == dev->mode_config.dpms_property && connector->encoder)
349                 intel_lvds_dpms(connector->encoder, (uint32_t)(value & 0xf));
350
351         return 0;
352 }
353
354 static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
355         .dpms = intel_lvds_dpms,
356         .mode_fixup = intel_lvds_mode_fixup,
357         .prepare = intel_lvds_prepare,
358         .mode_set = intel_lvds_mode_set,
359         .commit = intel_lvds_commit,
360 };
361
362 static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
363         .get_modes = intel_lvds_get_modes,
364         .mode_valid = intel_lvds_mode_valid,
365         .best_encoder = intel_best_encoder,
366 };
367
368 static const struct drm_connector_funcs intel_lvds_connector_funcs = {
369         .save = intel_lvds_save,
370         .restore = intel_lvds_restore,
371         .detect = intel_lvds_detect,
372         .fill_modes = drm_helper_probe_single_connector_modes,
373         .set_property = intel_lvds_set_property,
374         .destroy = intel_lvds_destroy,
375 };
376
377
378 static void intel_lvds_enc_destroy(struct drm_encoder *encoder)
379 {
380         drm_encoder_cleanup(encoder);
381 }
382
383 static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
384         .destroy = intel_lvds_enc_destroy,
385 };
386
387 static int __init intel_no_lvds_dmi_callback(const struct dmi_system_id *id)
388 {
389         DRM_DEBUG("Skipping LVDS initialization for %s\n", id->ident);
390         return 1;
391 }
392
393 /* These systems claim to have LVDS, but really don't */
394 static const struct dmi_system_id __initdata intel_no_lvds[] = {
395         {
396                 .callback = intel_no_lvds_dmi_callback,
397                 .ident = "Apple Mac Mini (Core series)",
398                 .matches = {
399                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
400                         DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
401                 },
402         },
403         {
404                 .callback = intel_no_lvds_dmi_callback,
405                 .ident = "Apple Mac Mini (Core 2 series)",
406                 .matches = {
407                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
408                         DMI_MATCH(DMI_PRODUCT_NAME, "Macmini2,1"),
409                 },
410         },
411         {
412                 .callback = intel_no_lvds_dmi_callback,
413                 .ident = "MSI IM-945GSE-A",
414                 .matches = {
415                         DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
416                         DMI_MATCH(DMI_PRODUCT_NAME, "A9830IMS"),
417                 },
418         },
419         {
420                 .callback = intel_no_lvds_dmi_callback,
421                 .ident = "Dell Studio Hybrid",
422                 .matches = {
423                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
424                         DMI_MATCH(DMI_PRODUCT_NAME, "Studio Hybrid 140g"),
425                 },
426         },
427
428         /* FIXME: add a check for the Aopen Mini PC */
429
430         { }     /* terminating entry */
431 };
432
433 /**
434  * intel_lvds_init - setup LVDS connectors on this device
435  * @dev: drm device
436  *
437  * Create the connector, register the LVDS DDC bus, and try to figure out what
438  * modes we can display on the LVDS panel (if present).
439  */
440 void intel_lvds_init(struct drm_device *dev)
441 {
442         struct drm_i915_private *dev_priv = dev->dev_private;
443         struct intel_output *intel_output;
444         struct drm_connector *connector;
445         struct drm_encoder *encoder;
446         struct drm_display_mode *scan; /* *modes, *bios_mode; */
447         struct drm_crtc *crtc;
448         u32 lvds;
449         int pipe;
450
451         /* Skip init on machines we know falsely report LVDS */
452         if (dmi_check_system(intel_no_lvds))
453                 return;
454
455         intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
456         if (!intel_output) {
457                 return;
458         }
459
460         connector = &intel_output->base;
461         encoder = &intel_output->enc;
462         drm_connector_init(dev, &intel_output->base, &intel_lvds_connector_funcs,
463                            DRM_MODE_CONNECTOR_LVDS);
464
465         drm_encoder_init(dev, &intel_output->enc, &intel_lvds_enc_funcs,
466                          DRM_MODE_ENCODER_LVDS);
467
468         drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
469         intel_output->type = INTEL_OUTPUT_LVDS;
470
471         drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
472         drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
473         connector->display_info.subpixel_order = SubPixelHorizontalRGB;
474         connector->interlace_allowed = false;
475         connector->doublescan_allowed = false;
476
477
478         /*
479          * LVDS discovery:
480          * 1) check for EDID on DDC
481          * 2) check for VBT data
482          * 3) check to see if LVDS is already on
483          *    if none of the above, no panel
484          * 4) make sure lid is open
485          *    if closed, act like it's not there for now
486          */
487
488         /* Set up the DDC bus. */
489         intel_output->ddc_bus = intel_i2c_create(dev, GPIOC, "LVDSDDC_C");
490         if (!intel_output->ddc_bus) {
491                 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
492                            "failed.\n");
493                 goto failed;
494         }
495
496         /*
497          * Attempt to get the fixed panel mode from DDC.  Assume that the
498          * preferred mode is the right one.
499          */
500         intel_ddc_get_modes(intel_output);
501
502         list_for_each_entry(scan, &connector->probed_modes, head) {
503                 mutex_lock(&dev->mode_config.mutex);
504                 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
505                         dev_priv->panel_fixed_mode =
506                                 drm_mode_duplicate(dev, scan);
507                         mutex_unlock(&dev->mode_config.mutex);
508                         goto out;
509                 }
510                 mutex_unlock(&dev->mode_config.mutex);
511         }
512
513         /* Failed to get EDID, what about VBT? */
514         if (dev_priv->lfp_lvds_vbt_mode) {
515                 mutex_lock(&dev->mode_config.mutex);
516                 dev_priv->panel_fixed_mode =
517                         drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
518                 mutex_unlock(&dev->mode_config.mutex);
519                 if (dev_priv->panel_fixed_mode) {
520                         dev_priv->panel_fixed_mode->type |=
521                                 DRM_MODE_TYPE_PREFERRED;
522                         goto out;
523                 }
524         }
525
526         /*
527          * If we didn't get EDID, try checking if the panel is already turned
528          * on.  If so, assume that whatever is currently programmed is the
529          * correct mode.
530          */
531         lvds = I915_READ(LVDS);
532         pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
533         crtc = intel_get_crtc_from_pipe(dev, pipe);
534
535         if (crtc && (lvds & LVDS_PORT_EN)) {
536                 dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
537                 if (dev_priv->panel_fixed_mode) {
538                         dev_priv->panel_fixed_mode->type |=
539                                 DRM_MODE_TYPE_PREFERRED;
540                         goto out;
541                 }
542         }
543
544         /* If we still don't have a mode after all that, give up. */
545         if (!dev_priv->panel_fixed_mode)
546                 goto failed;
547
548 out:
549         drm_sysfs_connector_add(connector);
550         return;
551
552 failed:
553         DRM_DEBUG("No LVDS modes found, disabling.\n");
554         if (intel_output->ddc_bus)
555                 intel_i2c_destroy(intel_output->ddc_bus);
556         drm_connector_cleanup(connector);
557         kfree(connector);
558 }