Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux...
[linux-2.6] / drivers / gpu / drm / i915 / intel_dvo.c
1 /*
2  * Copyright 2006 Dave Airlie <airlied@linux.ie>
3  * Copyright © 2006-2007 Intel Corporation
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  */
27 #include <linux/i2c.h>
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc.h"
31 #include "intel_drv.h"
32 #include "i915_drm.h"
33 #include "i915_drv.h"
34 #include "dvo.h"
35
36 #define SIL164_ADDR     0x38
37 #define CH7xxx_ADDR     0x76
38 #define TFP410_ADDR     0x38
39
40 static struct intel_dvo_device intel_dvo_devices[] = {
41         {
42                 .type = INTEL_DVO_CHIP_TMDS,
43                 .name = "sil164",
44                 .dvo_reg = DVOC,
45                 .slave_addr = SIL164_ADDR,
46                 .dev_ops = &sil164_ops,
47         },
48         {
49                 .type = INTEL_DVO_CHIP_TMDS,
50                 .name = "ch7xxx",
51                 .dvo_reg = DVOC,
52                 .slave_addr = CH7xxx_ADDR,
53                 .dev_ops = &ch7xxx_ops,
54         },
55         {
56                 .type = INTEL_DVO_CHIP_LVDS,
57                 .name = "ivch",
58                 .dvo_reg = DVOA,
59                 .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
60                 .dev_ops = &ivch_ops,
61         },
62         {
63                 .type = INTEL_DVO_CHIP_TMDS,
64                 .name = "tfp410",
65                 .dvo_reg = DVOC,
66                 .slave_addr = TFP410_ADDR,
67                 .dev_ops = &tfp410_ops,
68         },
69         {
70                 .type = INTEL_DVO_CHIP_LVDS,
71                 .name = "ch7017",
72                 .dvo_reg = DVOC,
73                 .slave_addr = 0x75,
74                 .gpio = GPIOE,
75                 .dev_ops = &ch7017_ops,
76         }
77 };
78
79 static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
80 {
81         struct drm_i915_private *dev_priv = encoder->dev->dev_private;
82         struct intel_output *intel_output = enc_to_intel_output(encoder);
83         struct intel_dvo_device *dvo = intel_output->dev_priv;
84         u32 dvo_reg = dvo->dvo_reg;
85         u32 temp = I915_READ(dvo_reg);
86
87         if (mode == DRM_MODE_DPMS_ON) {
88                 I915_WRITE(dvo_reg, temp | DVO_ENABLE);
89                 I915_READ(dvo_reg);
90                 dvo->dev_ops->dpms(dvo, mode);
91         } else {
92                 dvo->dev_ops->dpms(dvo, mode);
93                 I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
94                 I915_READ(dvo_reg);
95         }
96 }
97
98 static void intel_dvo_save(struct drm_connector *connector)
99 {
100         struct drm_i915_private *dev_priv = connector->dev->dev_private;
101         struct intel_output *intel_output = to_intel_output(connector);
102         struct intel_dvo_device *dvo = intel_output->dev_priv;
103
104         /* Each output should probably just save the registers it touches,
105          * but for now, use more overkill.
106          */
107         dev_priv->saveDVOA = I915_READ(DVOA);
108         dev_priv->saveDVOB = I915_READ(DVOB);
109         dev_priv->saveDVOC = I915_READ(DVOC);
110
111         dvo->dev_ops->save(dvo);
112 }
113
114 static void intel_dvo_restore(struct drm_connector *connector)
115 {
116         struct drm_i915_private *dev_priv = connector->dev->dev_private;
117         struct intel_output *intel_output = to_intel_output(connector);
118         struct intel_dvo_device *dvo = intel_output->dev_priv;
119
120         dvo->dev_ops->restore(dvo);
121
122         I915_WRITE(DVOA, dev_priv->saveDVOA);
123         I915_WRITE(DVOB, dev_priv->saveDVOB);
124         I915_WRITE(DVOC, dev_priv->saveDVOC);
125 }
126
127 static int intel_dvo_mode_valid(struct drm_connector *connector,
128                                 struct drm_display_mode *mode)
129 {
130         struct intel_output *intel_output = to_intel_output(connector);
131         struct intel_dvo_device *dvo = intel_output->dev_priv;
132
133         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
134                 return MODE_NO_DBLESCAN;
135
136         /* XXX: Validate clock range */
137
138         if (dvo->panel_fixed_mode) {
139                 if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
140                         return MODE_PANEL;
141                 if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
142                         return MODE_PANEL;
143         }
144
145         return dvo->dev_ops->mode_valid(dvo, mode);
146 }
147
148 static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
149                                  struct drm_display_mode *mode,
150                                  struct drm_display_mode *adjusted_mode)
151 {
152         struct intel_output *intel_output = enc_to_intel_output(encoder);
153         struct intel_dvo_device *dvo = intel_output->dev_priv;
154
155         /* If we have timings from the BIOS for the panel, put them in
156          * to the adjusted mode.  The CRTC will be set up for this mode,
157          * with the panel scaling set up to source from the H/VDisplay
158          * of the original mode.
159          */
160         if (dvo->panel_fixed_mode != NULL) {
161 #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
162                 C(hdisplay);
163                 C(hsync_start);
164                 C(hsync_end);
165                 C(htotal);
166                 C(vdisplay);
167                 C(vsync_start);
168                 C(vsync_end);
169                 C(vtotal);
170                 C(clock);
171                 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
172 #undef C
173         }
174
175         if (dvo->dev_ops->mode_fixup)
176                 return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
177
178         return true;
179 }
180
181 static void intel_dvo_mode_set(struct drm_encoder *encoder,
182                                struct drm_display_mode *mode,
183                                struct drm_display_mode *adjusted_mode)
184 {
185         struct drm_device *dev = encoder->dev;
186         struct drm_i915_private *dev_priv = dev->dev_private;
187         struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
188         struct intel_output *intel_output = enc_to_intel_output(encoder);
189         struct intel_dvo_device *dvo = intel_output->dev_priv;
190         int pipe = intel_crtc->pipe;
191         u32 dvo_val;
192         u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
193         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
194
195         switch (dvo_reg) {
196         case DVOA:
197         default:
198                 dvo_srcdim_reg = DVOA_SRCDIM;
199                 break;
200         case DVOB:
201                 dvo_srcdim_reg = DVOB_SRCDIM;
202                 break;
203         case DVOC:
204                 dvo_srcdim_reg = DVOC_SRCDIM;
205                 break;
206         }
207
208         dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
209
210         /* Save the data order, since I don't know what it should be set to. */
211         dvo_val = I915_READ(dvo_reg) &
212                   (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
213         dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
214                    DVO_BLANK_ACTIVE_HIGH;
215
216         if (pipe == 1)
217                 dvo_val |= DVO_PIPE_B_SELECT;
218         dvo_val |= DVO_PIPE_STALL;
219         if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
220                 dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
221         if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
222                 dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
223
224         I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
225
226         /*I915_WRITE(DVOB_SRCDIM,
227           (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
228           (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
229         I915_WRITE(dvo_srcdim_reg,
230                    (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
231                    (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
232         /*I915_WRITE(DVOB, dvo_val);*/
233         I915_WRITE(dvo_reg, dvo_val);
234 }
235
236 /**
237  * Detect the output connection on our DVO device.
238  *
239  * Unimplemented.
240  */
241 static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
242 {
243         struct intel_output *intel_output = to_intel_output(connector);
244         struct intel_dvo_device *dvo = intel_output->dev_priv;
245
246         return dvo->dev_ops->detect(dvo);
247 }
248
249 static int intel_dvo_get_modes(struct drm_connector *connector)
250 {
251         struct intel_output *intel_output = to_intel_output(connector);
252         struct intel_dvo_device *dvo = intel_output->dev_priv;
253
254         /* We should probably have an i2c driver get_modes function for those
255          * devices which will have a fixed set of modes determined by the chip
256          * (TV-out, for example), but for now with just TMDS and LVDS,
257          * that's not the case.
258          */
259         intel_ddc_get_modes(intel_output);
260         if (!list_empty(&connector->probed_modes))
261                 return 1;
262
263
264         if (dvo->panel_fixed_mode != NULL) {
265                 struct drm_display_mode *mode;
266                 mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
267                 if (mode) {
268                         drm_mode_probed_add(connector, mode);
269                         return 1;
270                 }
271         }
272         return 0;
273 }
274
275 static void intel_dvo_destroy (struct drm_connector *connector)
276 {
277         struct intel_output *intel_output = to_intel_output(connector);
278         struct intel_dvo_device *dvo = intel_output->dev_priv;
279
280         if (dvo) {
281                 if (dvo->dev_ops->destroy)
282                         dvo->dev_ops->destroy(dvo);
283                 if (dvo->panel_fixed_mode)
284                         kfree(dvo->panel_fixed_mode);
285                 /* no need, in i830_dvoices[] now */
286                 //kfree(dvo);
287         }
288         if (intel_output->i2c_bus)
289                 intel_i2c_destroy(intel_output->i2c_bus);
290         if (intel_output->ddc_bus)
291                 intel_i2c_destroy(intel_output->ddc_bus);
292         drm_sysfs_connector_remove(connector);
293         drm_connector_cleanup(connector);
294         kfree(intel_output);
295 }
296
297 #ifdef RANDR_GET_CRTC_INTERFACE
298 static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
299 {
300         struct drm_device *dev = connector->dev;
301         struct drm_i915_private *dev_priv = dev->dev_private;
302         struct intel_output *intel_output = to_intel_output(connector);
303         struct intel_dvo_device *dvo = intel_output->dev_priv;
304         int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
305
306         return intel_pipe_to_crtc(pScrn, pipe);
307 }
308 #endif
309
310 static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
311         .dpms = intel_dvo_dpms,
312         .mode_fixup = intel_dvo_mode_fixup,
313         .prepare = intel_encoder_prepare,
314         .mode_set = intel_dvo_mode_set,
315         .commit = intel_encoder_commit,
316 };
317
318 static const struct drm_connector_funcs intel_dvo_connector_funcs = {
319         .save = intel_dvo_save,
320         .restore = intel_dvo_restore,
321         .detect = intel_dvo_detect,
322         .destroy = intel_dvo_destroy,
323         .fill_modes = drm_helper_probe_single_connector_modes,
324 };
325
326 static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
327         .mode_valid = intel_dvo_mode_valid,
328         .get_modes = intel_dvo_get_modes,
329         .best_encoder = intel_best_encoder,
330 };
331
332 static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
333 {
334         drm_encoder_cleanup(encoder);
335 }
336
337 static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
338         .destroy = intel_dvo_enc_destroy,
339 };
340
341
342 /**
343  * Attempts to get a fixed panel timing for LVDS (currently only the i830).
344  *
345  * Other chips with DVO LVDS will need to extend this to deal with the LVDS
346  * chip being on DVOB/C and having multiple pipes.
347  */
348 static struct drm_display_mode *
349 intel_dvo_get_current_mode (struct drm_connector *connector)
350 {
351         struct drm_device *dev = connector->dev;
352         struct drm_i915_private *dev_priv = dev->dev_private;
353         struct intel_output *intel_output = to_intel_output(connector);
354         struct intel_dvo_device *dvo = intel_output->dev_priv;
355         uint32_t dvo_reg = dvo->dvo_reg;
356         uint32_t dvo_val = I915_READ(dvo_reg);
357         struct drm_display_mode *mode = NULL;
358
359         /* If the DVO port is active, that'll be the LVDS, so we can pull out
360          * its timings to get how the BIOS set up the panel.
361          */
362         if (dvo_val & DVO_ENABLE) {
363                 struct drm_crtc *crtc;
364                 int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
365
366                 crtc = intel_get_crtc_from_pipe(dev, pipe);
367                 if (crtc) {
368                         mode = intel_crtc_mode_get(dev, crtc);
369
370                         if (mode) {
371                                 mode->type |= DRM_MODE_TYPE_PREFERRED;
372                                 if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
373                                         mode->flags |= DRM_MODE_FLAG_PHSYNC;
374                                 if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
375                                         mode->flags |= DRM_MODE_FLAG_PVSYNC;
376                         }
377                 }
378         }
379         return mode;
380 }
381
382 void intel_dvo_init(struct drm_device *dev)
383 {
384         struct intel_output *intel_output;
385         struct intel_dvo_device *dvo;
386         struct intel_i2c_chan *i2cbus = NULL;
387         int ret = 0;
388         int i;
389         int gpio_inited = 0;
390         int encoder_type = DRM_MODE_ENCODER_NONE;
391         intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL);
392         if (!intel_output)
393                 return;
394
395         /* Set up the DDC bus */
396         intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
397         if (!intel_output->ddc_bus)
398                 goto free_intel;
399
400         /* Now, try to find a controller */
401         for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
402                 struct drm_connector *connector = &intel_output->base;
403                 int gpio;
404
405                 dvo = &intel_dvo_devices[i];
406
407                 /* Allow the I2C driver info to specify the GPIO to be used in
408                  * special cases, but otherwise default to what's defined
409                  * in the spec.
410                  */
411                 if (dvo->gpio != 0)
412                         gpio = dvo->gpio;
413                 else if (dvo->type == INTEL_DVO_CHIP_LVDS)
414                         gpio = GPIOB;
415                 else
416                         gpio = GPIOE;
417
418                 /* Set up the I2C bus necessary for the chip we're probing.
419                  * It appears that everything is on GPIOE except for panels
420                  * on i830 laptops, which are on GPIOB (DVOA).
421                  */
422                 if (gpio_inited != gpio) {
423                         if (i2cbus != NULL)
424                                 intel_i2c_destroy(i2cbus);
425                         if (!(i2cbus = intel_i2c_create(dev, gpio,
426                                 gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
427                                 continue;
428                         }
429                         gpio_inited = gpio;
430                 }
431
432                 if (dvo->dev_ops!= NULL)
433                         ret = dvo->dev_ops->init(dvo, i2cbus);
434                 else
435                         ret = false;
436
437                 if (!ret)
438                         continue;
439
440                 intel_output->type = INTEL_OUTPUT_DVO;
441                 switch (dvo->type) {
442                 case INTEL_DVO_CHIP_TMDS:
443                         drm_connector_init(dev, connector,
444                                            &intel_dvo_connector_funcs,
445                                            DRM_MODE_CONNECTOR_DVII);
446                         encoder_type = DRM_MODE_ENCODER_TMDS;
447                         break;
448                 case INTEL_DVO_CHIP_LVDS:
449                         drm_connector_init(dev, connector,
450                                            &intel_dvo_connector_funcs,
451                                            DRM_MODE_CONNECTOR_LVDS);
452                         encoder_type = DRM_MODE_ENCODER_LVDS;
453                         break;
454                 }
455
456                 drm_connector_helper_add(connector,
457                                          &intel_dvo_connector_helper_funcs);
458                 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
459                 connector->interlace_allowed = false;
460                 connector->doublescan_allowed = false;
461
462                 intel_output->dev_priv = dvo;
463                 intel_output->i2c_bus = i2cbus;
464
465                 drm_encoder_init(dev, &intel_output->enc,
466                                  &intel_dvo_enc_funcs, encoder_type);
467                 drm_encoder_helper_add(&intel_output->enc,
468                                        &intel_dvo_helper_funcs);
469
470                 drm_mode_connector_attach_encoder(&intel_output->base,
471                                                   &intel_output->enc);
472                 if (dvo->type == INTEL_DVO_CHIP_LVDS) {
473                         /* For our LVDS chipsets, we should hopefully be able
474                          * to dig the fixed panel mode out of the BIOS data.
475                          * However, it's in a different format from the BIOS
476                          * data on chipsets with integrated LVDS (stored in AIM
477                          * headers, likely), so for now, just get the current
478                          * mode being output through DVO.
479                          */
480                         dvo->panel_fixed_mode =
481                                 intel_dvo_get_current_mode(connector);
482                         dvo->panel_wants_dither = true;
483                 }
484
485                 drm_sysfs_connector_add(connector);
486                 return;
487         }
488
489         intel_i2c_destroy(intel_output->ddc_bus);
490         /* Didn't find a chip, so tear down. */
491         if (i2cbus != NULL)
492                 intel_i2c_destroy(i2cbus);
493 free_intel:
494         kfree(intel_output);
495 }