Move i2c init back to where it belongs and add i2c unregistration in *_destroy.
[platform/upstream/libdrm.git] / linux-core / 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/i2c.h>
31 #include "drmP.h"
32 #include "drm.h"
33 #include "drm_crtc.h"
34 #include "drm_edid.h"
35 #include "intel_drv.h"
36 #include "i915_drm.h"
37 #include "i915_drv.h"
38
39 /**
40  * Sets the backlight level.
41  *
42  * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
43  */
44 static void intel_lvds_set_backlight(struct drm_device *dev, int level)
45 {
46         drm_i915_private_t *dev_priv = dev->dev_private;
47         u32 blc_pwm_ctl;
48
49         blc_pwm_ctl = I915_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
50         I915_WRITE(BLC_PWM_CTL, (blc_pwm_ctl |
51                                  (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
52 }
53
54 /**
55  * Returns the maximum level of the backlight duty cycle field.
56  */
57 static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
58 {
59         drm_i915_private_t *dev_priv = dev->dev_private;
60     
61         return ((I915_READ(BLC_PWM_CTL) & BACKLIGHT_MODULATION_FREQ_MASK) >>
62                 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
63 }
64
65 /**
66  * Sets the power state for the panel.
67  */
68 static void intel_lvds_set_power(struct drm_device *dev, bool on)
69 {
70         drm_i915_private_t *dev_priv = dev->dev_private;
71         u32 pp_status;
72
73         if (on) {
74                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
75                            POWER_TARGET_ON);
76                 do {
77                         pp_status = I915_READ(PP_STATUS);
78                 } while ((pp_status & PP_ON) == 0);
79
80                 intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
81         } else {
82                 intel_lvds_set_backlight(dev, 0);
83
84                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) &
85                            ~POWER_TARGET_ON);
86                 do {
87                         pp_status = I915_READ(PP_STATUS);
88                 } while (pp_status & PP_ON);
89         }
90 }
91
92 static void intel_lvds_dpms(struct drm_output *output, int mode)
93 {
94         struct drm_device *dev = output->dev;
95
96         if (mode == DPMSModeOn)
97                 intel_lvds_set_power(dev, true);
98         else
99                 intel_lvds_set_power(dev, false);
100
101         /* XXX: We never power down the LVDS pairs. */
102 }
103
104 static void intel_lvds_save(struct drm_output *output)
105 {
106         struct drm_device *dev = output->dev;
107         drm_i915_private_t *dev_priv = dev->dev_private;
108
109         dev_priv->savePP_ON = I915_READ(LVDSPP_ON);
110         dev_priv->savePP_OFF = I915_READ(LVDSPP_OFF);
111         dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
112         dev_priv->savePP_CYCLE = I915_READ(PP_CYCLE);
113         dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
114         dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
115                                        BACKLIGHT_DUTY_CYCLE_MASK);
116
117         /*
118          * If the light is off at server startup, just make it full brightness
119          */
120         if (dev_priv->backlight_duty_cycle == 0)
121                 dev_priv->backlight_duty_cycle =
122                         intel_lvds_get_max_backlight(dev);
123 }
124
125 static void intel_lvds_restore(struct drm_output *output)
126 {
127         struct drm_device *dev = output->dev;
128         drm_i915_private_t *dev_priv = dev->dev_private;
129
130         I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
131         I915_WRITE(LVDSPP_ON, dev_priv->savePP_ON);
132         I915_WRITE(LVDSPP_OFF, dev_priv->savePP_OFF);
133         I915_WRITE(PP_CYCLE, dev_priv->savePP_CYCLE);
134         I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
135         if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
136                 intel_lvds_set_power(dev, true);
137         else
138                 intel_lvds_set_power(dev, false);
139 }
140
141 static int intel_lvds_mode_valid(struct drm_output *output,
142                                  struct drm_display_mode *mode)
143 {
144         struct drm_device *dev = output->dev;
145         drm_i915_private_t *dev_priv = dev->dev_private;
146         struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
147
148         if (fixed_mode) {
149                 if (mode->hdisplay > fixed_mode->hdisplay)
150                         return MODE_PANEL;
151                 if (mode->vdisplay > fixed_mode->vdisplay)
152                         return MODE_PANEL;
153         }
154
155         return MODE_OK;
156 }
157
158 static bool intel_lvds_mode_fixup(struct drm_output *output,
159                                   struct drm_display_mode *mode,
160                                   struct drm_display_mode *adjusted_mode)
161 {
162         struct drm_device *dev = output->dev;
163         drm_i915_private_t *dev_priv = dev->dev_private;
164         struct intel_crtc *intel_crtc = output->crtc->driver_private;
165         struct drm_output *tmp_output;
166
167 #if 0 /* FIXME: Check for other outputs on this pipe */
168         spin_lock(&dev->mode_config.config_lock);
169         list_for_each_entry(tmp_output, &dev->mode_config.output_list, head) {
170                 if (tmp_output != output && tmp_output->crtc == output->crtc) {
171                         printk(KERN_ERR "Can't enable LVDS and another "
172                                "output on the same pipe\n");
173                         return false;
174                 }
175         }
176         spin_lock(&dev->mode_config.config_lock);
177 #endif
178
179         if (intel_crtc->pipe == 0) {
180                 printk(KERN_ERR "Can't support LVDS on pipe A\n");
181                 return false;
182         }
183
184         /*
185          * If we have timings from the BIOS for the panel, put them in
186          * to the adjusted mode.  The CRTC will be set up for this mode,
187          * with the panel scaling set up to source from the H/VDisplay
188          * of the original mode.
189          */
190         if (dev_priv->panel_fixed_mode != NULL) {
191                 adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
192                 adjusted_mode->hsync_start =
193                         dev_priv->panel_fixed_mode->hsync_start;
194                 adjusted_mode->hsync_end =
195                         dev_priv->panel_fixed_mode->hsync_end;
196                 adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
197                 adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
198                 adjusted_mode->vsync_start =
199                         dev_priv->panel_fixed_mode->vsync_start;
200                 adjusted_mode->vsync_end =
201                         dev_priv->panel_fixed_mode->vsync_end;
202                 adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
203                 adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
204                 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
205         }
206
207         /*
208          * XXX: It would be nice to support lower refresh rates on the
209          * panels to reduce power consumption, and perhaps match the
210          * user's requested refresh rate.
211          */
212
213         return true;
214 }
215
216 static void intel_lvds_mode_set(struct drm_output *output,
217                                 struct drm_display_mode *mode,
218                                 struct drm_display_mode *adjusted_mode)
219 {
220         struct drm_device *dev = output->dev;
221         drm_i915_private_t *dev_priv = dev->dev_private;
222         struct intel_crtc *intel_crtc = output->crtc->driver_private;
223         u32 pfit_control;
224
225         /*
226          * The LVDS pin pair will already have been turned on in the
227          * intel_crtc_mode_set since it has a large impact on the DPLL
228          * settings.
229          */
230
231         /*
232          * Enable automatic panel scaling so that non-native modes fill the
233          * screen.  Should be enabled before the pipe is enabled, according to
234          * register description and PRM.
235          */
236         pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
237                         VERT_INTERP_BILINEAR | HORIZ_INTERP_BILINEAR);
238
239         if (!IS_I965G(dev)) {
240                 if (dev_priv->panel_wants_dither)
241                         pfit_control |= PANEL_8TO6_DITHER_ENABLE;
242         }
243         else
244                 pfit_control |= intel_crtc->pipe << PFIT_PIPE_SHIFT;
245
246         I915_WRITE(PFIT_CONTROL, pfit_control);
247 }
248
249 /**
250  * Detect the LVDS connection.
251  *
252  * This always returns OUTPUT_STATUS_CONNECTED.  This output should only have
253  * been set up if the LVDS was actually connected anyway.
254  */
255 static enum drm_output_status intel_lvds_detect(struct drm_output *output)
256 {
257         return output_status_connected;
258 }
259
260 /**
261  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
262  */
263 static int intel_lvds_get_modes(struct drm_output *output)
264 {
265         struct intel_output *intel_output = output->driver_private;
266         struct drm_device *dev = output->dev;
267         drm_i915_private_t *dev_priv = dev->dev_private;
268         struct edid *edid_info;
269         int ret = 0;
270
271         ret = intel_ddc_get_modes(output);
272
273         if (ret)
274                 return ret;
275
276         /* Didn't get an EDID */
277         if (!output->monitor_info) {
278                 struct detailed_data_monitor_range *edid_range;
279                 edid_info = kzalloc(sizeof(*output->monitor_info), GFP_KERNEL);
280                 if (!edid_info)
281                         goto out;
282
283                 edid_info->detailed_timings[0].data.other_data.type =
284                         EDID_DETAIL_MONITOR_RANGE;
285                 edid_range = &edid_info->detailed_timings[0].data.other_data.data.range;
286
287                 /* Set wide sync ranges so we get all modes
288                  * handed to valid_mode for checking
289                  */
290                 edid_range->min_vfreq = 0;
291                 edid_range->max_vfreq = 200;
292                 edid_range->min_hfreq_khz = 0;
293                 edid_range->max_hfreq_khz = 200;
294                 output->monitor_info = edid_info;
295         }
296
297 out:
298         if (dev_priv->panel_fixed_mode != NULL) {
299                 struct drm_display_mode *mode =
300                         drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
301                 drm_mode_probed_add(output, mode);
302                 return 1;
303         }
304
305         return 0;
306 }
307
308 /**
309  * intel_lvds_destroy - unregister and free LVDS structures
310  * @output: output to free
311  *
312  * Unregister the DDC bus for this output then free the driver private
313  * structure.
314  */
315 static void intel_lvds_destroy(struct drm_output *output)
316 {
317         struct intel_output *intel_output = output->driver_private;
318
319         intel_i2c_destroy(intel_output->ddc_bus);
320
321         if (output->driver_private)
322                 kfree(output->driver_private);
323 }
324
325 static const struct drm_output_funcs intel_lvds_output_funcs = {
326         .dpms = intel_lvds_dpms,
327         .save = intel_lvds_save,
328         .restore = intel_lvds_restore,
329         .mode_valid = intel_lvds_mode_valid,
330         .mode_fixup = intel_lvds_mode_fixup,
331         .prepare = intel_output_prepare,
332         .mode_set = intel_lvds_mode_set,
333         .commit = intel_output_commit,
334         .detect = intel_lvds_detect,
335         .get_modes = intel_lvds_get_modes,
336         .cleanup = intel_lvds_destroy
337 };
338
339 /**
340  * intel_lvds_init - setup LVDS outputs on this device
341  * @dev: drm device
342  *
343  * Create the output, register the LVDS DDC bus, and try to figure out what
344  * modes we can display on the LVDS panel (if present).
345  */
346 void intel_lvds_init(struct drm_device *dev)
347 {
348         drm_i915_private_t *dev_priv = dev->dev_private;
349         struct drm_output *output;
350         struct intel_output *intel_output;
351         struct drm_display_mode *scan; /* *modes, *bios_mode; */
352
353         output = drm_output_create(dev, &intel_lvds_output_funcs, "LVDS");
354         if (!output)
355                 return;
356
357         intel_output = kmalloc(sizeof(struct intel_output), GFP_KERNEL);
358         if (!intel_output) {
359                 drm_output_destroy(output);
360                 return;
361         }
362
363         intel_output->type = INTEL_OUTPUT_LVDS;
364         output->driver_private = intel_output;
365         output->subpixel_order = SubPixelHorizontalRGB;
366         output->interlace_allowed = FALSE;
367         output->doublescan_allowed = FALSE;
368
369         /* Set up the DDC bus. */
370         intel_output->ddc_bus = intel_i2c_create(dev, GPIOC, "LVDSDDC_C");
371         if (!intel_output->ddc_bus) {
372                 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
373                            "failed.\n");
374                 return;
375         }
376
377         /*
378          * Attempt to get the fixed panel mode from DDC.  Assume that the
379          * preferred mode is the right one.
380          */
381         intel_ddc_get_modes(output);
382
383         list_for_each_entry(scan, &output->probed_modes, head) {
384                 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
385                         dev_priv->panel_fixed_mode = 
386                                 drm_mode_duplicate(dev, scan);
387                         break;
388                 }
389         }
390
391 #if 0
392         /*
393          * If we didn't get EDID, try checking if the panel is already turned
394          * on.  If so, assume that whatever is currently programmed is the
395          * correct mode.
396          */
397         if (!dev_priv->panel_fixed_mode) {
398                 u32 lvds = I915_READ(LVDS);
399                 int pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
400                 struct drm_mode_config *mode_config = &dev->mode_config;
401                 struct drm_crtc *crtc;
402                 /* FIXME: need drm_crtc_from_pipe */
403                 //crtc = drm_crtc_from_pipe(mode_config, pipe);
404                 
405                 if (lvds & LVDS_PORT_EN && 0) {
406                         dev_priv->panel_fixed_mode =
407                                 intel_crtc_mode_get(dev, crtc);
408                         if (dev_priv->panel_fixed_mode)
409                                 dev_priv->panel_fixed_mode->type |=
410                                         DRM_MODE_TYPE_PREFERRED;
411                 }
412         }
413
414 /* No BIOS poking yet... */
415         /* Get the LVDS fixed mode out of the BIOS.  We should support LVDS
416          * with the BIOS being unavailable or broken, but lack the
417          * configuration options for now.
418          */
419         bios_mode = intel_bios_get_panel_mode(pScrn);
420         if (bios_mode != NULL) {
421                 if (dev_priv->panel_fixed_mode != NULL) {
422                         if (dev_priv->debug_modes &&
423                             !xf86ModesEqual(dev_priv->panel_fixed_mode,
424                                             bios_mode))
425                         {
426                                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
427                                            "BIOS panel mode data doesn't match probed data, "
428                                            "continuing with probed.\n");
429                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIOS mode:\n");
430                                 xf86PrintModeline(pScrn->scrnIndex, bios_mode);
431                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "probed mode:\n");
432                                 xf86PrintModeline(pScrn->scrnIndex, dev_priv->panel_fixed_mode);
433                                 xfree(bios_mode->name);
434                                 xfree(bios_mode);
435                         }
436                 }  else {
437                         dev_priv->panel_fixed_mode = bios_mode;
438                 }
439         } else {
440                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
441                            "Couldn't detect panel mode.  Disabling panel\n");
442                 goto disable_exit;
443         }
444
445         /* Blacklist machines with BIOSes that list an LVDS panel without actually
446          * having one.
447          */
448         if (dev_priv->PciInfo->chipType == PCI_CHIP_I945_GM) {
449                 if (dev_priv->PciInfo->subsysVendor == 0xa0a0)  /* aopen mini pc */
450                         goto disable_exit;
451
452                 if ((dev_priv->PciInfo->subsysVendor == 0x8086) &&
453                     (dev_priv->PciInfo->subsysCard == 0x7270)) {
454                         /* It's a Mac Mini or Macbook Pro.
455                          *
456                          * Apple hardware is out to get us.  The macbook pro has a real
457                          * LVDS panel, but the mac mini does not, and they have the same
458                          * device IDs.  We'll distinguish by panel size, on the assumption
459                          * that Apple isn't about to make any machines with an 800x600
460                          * display.
461                          */
462
463                         if (dev_priv->panel_fixed_mode != NULL &&
464                             dev_priv->panel_fixed_mode->HDisplay == 800 &&
465                             dev_priv->panel_fixed_mode->VDisplay == 600)
466                         {
467                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
468                                            "Suspected Mac Mini, ignoring the LVDS\n");
469                                 goto disable_exit;
470                         }
471                 }
472         }
473
474 #endif
475         return;
476 }