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