blob: c87b179eadfdcb6e39ce595f931e7c896bdfb13b [file] [log] [blame]
Alan Cox6a227d52011-11-03 18:22:37 +00001/*
2 * Copyright © 2006-2011 Intel Corporation
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Authors:
18 * Eric Anholt <eric@anholt.net>
19 * Dave Airlie <airlied@linux.ie>
20 * Jesse Barnes <jesse.barnes@intel.com>
21 */
22
23#include <linux/i2c.h>
24#include <linux/dmi.h>
25#include <drm/drmP.h>
26
27#include "intel_bios.h"
28#include "psb_drv.h"
29#include "psb_intel_drv.h"
30#include "psb_intel_reg.h"
31#include "power.h"
32#include <linux/pm_runtime.h>
33#include "cdv_device.h"
34
35/**
36 * LVDS I2C backlight control macros
37 */
38#define BRIGHTNESS_MAX_LEVEL 100
39#define BRIGHTNESS_MASK 0xFF
40#define BLC_I2C_TYPE 0x01
41#define BLC_PWM_TYPT 0x02
42
43#define BLC_POLARITY_NORMAL 0
44#define BLC_POLARITY_INVERSE 1
45
46#define PSB_BLC_MAX_PWM_REG_FREQ (0xFFFE)
47#define PSB_BLC_MIN_PWM_REG_FREQ (0x2)
48#define PSB_BLC_PWM_PRECISION_FACTOR (10)
49#define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
50#define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
51
52struct cdv_intel_lvds_priv {
53 /**
54 * Saved LVDO output states
55 */
56 uint32_t savePP_ON;
57 uint32_t savePP_OFF;
58 uint32_t saveLVDS;
59 uint32_t savePP_CONTROL;
60 uint32_t savePP_CYCLE;
61 uint32_t savePFIT_CONTROL;
62 uint32_t savePFIT_PGM_RATIOS;
63 uint32_t saveBLC_PWM_CTL;
64};
65
66/*
67 * Returns the maximum level of the backlight duty cycle field.
68 */
69static u32 cdv_intel_lvds_get_max_backlight(struct drm_device *dev)
70{
71 struct drm_psb_private *dev_priv = dev->dev_private;
72 u32 retval;
73
74 if (gma_power_begin(dev, false)) {
75 retval = ((REG_READ(BLC_PWM_CTL) &
76 BACKLIGHT_MODULATION_FREQ_MASK) >>
77 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
78
79 gma_power_end(dev);
80 } else
Alan Cox648a8e32012-03-08 16:00:31 +000081 retval = ((dev_priv->regs.saveBLC_PWM_CTL &
Alan Cox6a227d52011-11-03 18:22:37 +000082 BACKLIGHT_MODULATION_FREQ_MASK) >>
83 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
84
85 return retval;
86}
87
Kirill A. Shutemov062d0542012-03-08 16:11:02 +000088#if 0
Alan Cox6a227d52011-11-03 18:22:37 +000089/*
90 * Set LVDS backlight level by I2C command
91 */
92static int cdv_lvds_i2c_set_brightness(struct drm_device *dev,
93 unsigned int level)
94{
95 struct drm_psb_private *dev_priv = dev->dev_private;
96 struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
97 u8 out_buf[2];
98 unsigned int blc_i2c_brightness;
99
100 struct i2c_msg msgs[] = {
101 {
102 .addr = lvds_i2c_bus->slave_addr,
103 .flags = 0,
104 .len = 2,
105 .buf = out_buf,
106 }
107 };
108
109 blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
110 BRIGHTNESS_MASK /
111 BRIGHTNESS_MAX_LEVEL);
112
113 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
114 blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
115
116 out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
117 out_buf[1] = (u8)blc_i2c_brightness;
118
119 if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1)
120 return 0;
121
122 DRM_ERROR("I2C transfer error\n");
123 return -1;
124}
125
126
127static int cdv_lvds_pwm_set_brightness(struct drm_device *dev, int level)
128{
129 struct drm_psb_private *dev_priv = dev->dev_private;
130
131 u32 max_pwm_blc;
132 u32 blc_pwm_duty_cycle;
133
134 max_pwm_blc = cdv_intel_lvds_get_max_backlight(dev);
135
136 /*BLC_PWM_CTL Should be initiated while backlight device init*/
137 BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
138
139 blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
140
141 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
142 blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
143
144 blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
145 REG_WRITE(BLC_PWM_CTL,
146 (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
147 (blc_pwm_duty_cycle));
148
149 return 0;
150}
151
152/*
153 * Set LVDS backlight level either by I2C or PWM
154 */
155void cdv_intel_lvds_set_brightness(struct drm_device *dev, int level)
156{
157 struct drm_psb_private *dev_priv = dev->dev_private;
158
159 if (!dev_priv->lvds_bl) {
160 DRM_ERROR("NO LVDS Backlight Info\n");
161 return;
162 }
163
164 if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
165 cdv_lvds_i2c_set_brightness(dev, level);
166 else
167 cdv_lvds_pwm_set_brightness(dev, level);
168}
Kirill A. Shutemov062d0542012-03-08 16:11:02 +0000169#endif
Alan Cox6a227d52011-11-03 18:22:37 +0000170
171/**
172 * Sets the backlight level.
173 *
174 * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
175 */
176static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
177{
178 struct drm_psb_private *dev_priv = dev->dev_private;
179 u32 blc_pwm_ctl;
180
181 if (gma_power_begin(dev, false)) {
182 blc_pwm_ctl =
183 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
184 REG_WRITE(BLC_PWM_CTL,
185 (blc_pwm_ctl |
186 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
187 gma_power_end(dev);
188 } else {
Alan Cox648a8e32012-03-08 16:00:31 +0000189 blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
Alan Cox6a227d52011-11-03 18:22:37 +0000190 ~BACKLIGHT_DUTY_CYCLE_MASK;
Alan Cox648a8e32012-03-08 16:00:31 +0000191 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
Alan Cox6a227d52011-11-03 18:22:37 +0000192 (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
193 }
194}
195
196/**
197 * Sets the power state for the panel.
198 */
199static void cdv_intel_lvds_set_power(struct drm_device *dev,
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000200 struct drm_encoder *encoder, bool on)
Alan Cox6a227d52011-11-03 18:22:37 +0000201{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000202 struct drm_psb_private *dev_priv = dev->dev_private;
Alan Cox6a227d52011-11-03 18:22:37 +0000203 u32 pp_status;
204
205 if (!gma_power_begin(dev, true))
206 return;
207
208 if (on) {
209 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
210 POWER_TARGET_ON);
211 do {
212 pp_status = REG_READ(PP_STATUS);
213 } while ((pp_status & PP_ON) == 0);
214
215 cdv_intel_lvds_set_backlight(dev,
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000216 dev_priv->mode_dev.backlight_duty_cycle);
Alan Cox6a227d52011-11-03 18:22:37 +0000217 } else {
218 cdv_intel_lvds_set_backlight(dev, 0);
219
220 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
221 ~POWER_TARGET_ON);
222 do {
223 pp_status = REG_READ(PP_STATUS);
224 } while (pp_status & PP_ON);
225 }
226 gma_power_end(dev);
227}
228
229static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
230{
231 struct drm_device *dev = encoder->dev;
Alan Cox6a227d52011-11-03 18:22:37 +0000232 if (mode == DRM_MODE_DPMS_ON)
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000233 cdv_intel_lvds_set_power(dev, encoder, true);
Alan Cox6a227d52011-11-03 18:22:37 +0000234 else
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000235 cdv_intel_lvds_set_power(dev, encoder, false);
Alan Cox6a227d52011-11-03 18:22:37 +0000236 /* XXX: We never power down the LVDS pairs. */
237}
238
239static void cdv_intel_lvds_save(struct drm_connector *connector)
240{
241}
242
243static void cdv_intel_lvds_restore(struct drm_connector *connector)
244{
245}
246
Kirill A. Shutemovbc11da72012-03-08 16:10:50 +0000247static int cdv_intel_lvds_mode_valid(struct drm_connector *connector,
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000248 struct drm_display_mode *mode)
Alan Cox6a227d52011-11-03 18:22:37 +0000249{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000250 struct drm_device *dev = connector->dev;
251 struct drm_psb_private *dev_priv = dev->dev_private;
Alan Cox6a227d52011-11-03 18:22:37 +0000252 struct drm_display_mode *fixed_mode =
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000253 dev_priv->mode_dev.panel_fixed_mode;
Alan Cox6a227d52011-11-03 18:22:37 +0000254
255 /* just in case */
256 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
257 return MODE_NO_DBLESCAN;
258
259 /* just in case */
260 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
261 return MODE_NO_INTERLACE;
262
263 if (fixed_mode) {
264 if (mode->hdisplay > fixed_mode->hdisplay)
265 return MODE_PANEL;
266 if (mode->vdisplay > fixed_mode->vdisplay)
267 return MODE_PANEL;
268 }
269 return MODE_OK;
270}
271
Kirill A. Shutemovbc11da72012-03-08 16:10:50 +0000272static bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
Alan Cox6a227d52011-11-03 18:22:37 +0000273 struct drm_display_mode *mode,
274 struct drm_display_mode *adjusted_mode)
275{
Alan Cox6a227d52011-11-03 18:22:37 +0000276 struct drm_device *dev = encoder->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000277 struct drm_psb_private *dev_priv = dev->dev_private;
278 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
Alan Cox6a227d52011-11-03 18:22:37 +0000279 struct drm_encoder *tmp_encoder;
280 struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
281
282 /* Should never happen!! */
283 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
284 head) {
285 if (tmp_encoder != encoder
286 && tmp_encoder->crtc == encoder->crtc) {
287 printk(KERN_ERR "Can't enable LVDS and another "
288 "encoder on the same pipe\n");
289 return false;
290 }
291 }
292
293 /*
294 * If we have timings from the BIOS for the panel, put them in
295 * to the adjusted mode. The CRTC will be set up for this mode,
296 * with the panel scaling set up to source from the H/VDisplay
297 * of the original mode.
298 */
299 if (panel_fixed_mode != NULL) {
300 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
301 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
302 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
303 adjusted_mode->htotal = panel_fixed_mode->htotal;
304 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
305 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
306 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
307 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
308 adjusted_mode->clock = panel_fixed_mode->clock;
309 drm_mode_set_crtcinfo(adjusted_mode,
310 CRTC_INTERLACE_HALVE_V);
311 }
312
313 /*
314 * XXX: It would be nice to support lower refresh rates on the
315 * panels to reduce power consumption, and perhaps match the
316 * user's requested refresh rate.
317 */
318
319 return true;
320}
321
322static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
323{
324 struct drm_device *dev = encoder->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000325 struct drm_psb_private *dev_priv = dev->dev_private;
326 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
Alan Cox6a227d52011-11-03 18:22:37 +0000327
328 if (!gma_power_begin(dev, true))
329 return;
330
331 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
332 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
333 BACKLIGHT_DUTY_CYCLE_MASK);
334
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000335 cdv_intel_lvds_set_power(dev, encoder, false);
Alan Cox6a227d52011-11-03 18:22:37 +0000336
337 gma_power_end(dev);
338}
339
340static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
341{
342 struct drm_device *dev = encoder->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000343 struct drm_psb_private *dev_priv = dev->dev_private;
344 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
Alan Cox6a227d52011-11-03 18:22:37 +0000345
346 if (mode_dev->backlight_duty_cycle == 0)
347 mode_dev->backlight_duty_cycle =
348 cdv_intel_lvds_get_max_backlight(dev);
349
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000350 cdv_intel_lvds_set_power(dev, encoder, true);
Alan Cox6a227d52011-11-03 18:22:37 +0000351}
352
353static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
354 struct drm_display_mode *mode,
355 struct drm_display_mode *adjusted_mode)
356{
357 struct drm_device *dev = encoder->dev;
358 struct drm_psb_private *dev_priv = dev->dev_private;
359 u32 pfit_control;
360
361 /*
362 * The LVDS pin pair will already have been turned on in the
363 * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
364 * settings.
365 */
366
367 /*
368 * Enable automatic panel scaling so that non-native modes fill the
369 * screen. Should be enabled before the pipe is enabled, according to
370 * register description and PRM.
371 */
372 if (mode->hdisplay != adjusted_mode->hdisplay ||
373 mode->vdisplay != adjusted_mode->vdisplay)
374 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
375 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
376 HORIZ_INTERP_BILINEAR);
377 else
378 pfit_control = 0;
379
380 if (dev_priv->lvds_dither)
381 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
382
383 REG_WRITE(PFIT_CONTROL, pfit_control);
384}
385
386/**
387 * Detect the LVDS connection.
388 *
389 * This always returns CONNECTOR_STATUS_CONNECTED.
390 * This connector should only have
391 * been set up if the LVDS was actually connected anyway.
392 */
393static enum drm_connector_status cdv_intel_lvds_detect(
394 struct drm_connector *connector, bool force)
395{
396 return connector_status_connected;
397}
398
399/**
400 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
401 */
402static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
403{
404 struct drm_device *dev = connector->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000405 struct drm_psb_private *dev_priv = dev->dev_private;
406 struct psb_intel_encoder *psb_intel_encoder =
407 psb_intel_attached_encoder(connector);
408 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
Alan Cox6a227d52011-11-03 18:22:37 +0000409 int ret;
410
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000411 ret = psb_intel_ddc_get_modes(connector, &psb_intel_encoder->i2c_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000412
413 if (ret)
414 return ret;
415
416 /* Didn't get an EDID, so
417 * Set wide sync ranges so we get all modes
418 * handed to valid_mode for checking
419 */
420 connector->display_info.min_vfreq = 0;
421 connector->display_info.max_vfreq = 200;
422 connector->display_info.min_hfreq = 0;
423 connector->display_info.max_hfreq = 200;
424 if (mode_dev->panel_fixed_mode != NULL) {
425 struct drm_display_mode *mode =
426 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
427 drm_mode_probed_add(connector, mode);
428 return 1;
429 }
430
431 return 0;
432}
433
434/**
435 * cdv_intel_lvds_destroy - unregister and free LVDS structures
436 * @connector: connector to free
437 *
438 * Unregister the DDC bus for this connector then free the driver private
439 * structure.
440 */
Kirill A. Shutemovbc11da72012-03-08 16:10:50 +0000441static void cdv_intel_lvds_destroy(struct drm_connector *connector)
Alan Cox6a227d52011-11-03 18:22:37 +0000442{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000443 struct psb_intel_encoder *psb_intel_encoder =
444 psb_intel_attached_encoder(connector);
Alan Cox6a227d52011-11-03 18:22:37 +0000445
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000446 if (psb_intel_encoder->i2c_bus)
447 psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
Alan Cox6a227d52011-11-03 18:22:37 +0000448 drm_sysfs_connector_remove(connector);
449 drm_connector_cleanup(connector);
450 kfree(connector);
451}
452
Kirill A. Shutemovbc11da72012-03-08 16:10:50 +0000453static int cdv_intel_lvds_set_property(struct drm_connector *connector,
Alan Cox6a227d52011-11-03 18:22:37 +0000454 struct drm_property *property,
455 uint64_t value)
456{
457 struct drm_encoder *encoder = connector->encoder;
458
459 if (!strcmp(property->name, "scaling mode") && encoder) {
460 struct psb_intel_crtc *crtc =
461 to_psb_intel_crtc(encoder->crtc);
462 uint64_t curValue;
463
464 if (!crtc)
465 return -1;
466
467 switch (value) {
468 case DRM_MODE_SCALE_FULLSCREEN:
469 break;
470 case DRM_MODE_SCALE_NO_SCALE:
471 break;
472 case DRM_MODE_SCALE_ASPECT:
473 break;
474 default:
475 return -1;
476 }
477
478 if (drm_connector_property_get_value(connector,
479 property,
480 &curValue))
481 return -1;
482
483 if (curValue == value)
484 return 0;
485
486 if (drm_connector_property_set_value(connector,
487 property,
488 value))
489 return -1;
490
491 if (crtc->saved_mode.hdisplay != 0 &&
492 crtc->saved_mode.vdisplay != 0) {
493 if (!drm_crtc_helper_set_mode(encoder->crtc,
494 &crtc->saved_mode,
495 encoder->crtc->x,
496 encoder->crtc->y,
497 encoder->crtc->fb))
498 return -1;
499 }
500 } else if (!strcmp(property->name, "backlight") && encoder) {
501 if (drm_connector_property_set_value(connector,
502 property,
503 value))
504 return -1;
505 else {
506#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
507 struct drm_psb_private *dev_priv =
508 encoder->dev->dev_private;
509 struct backlight_device *bd =
510 dev_priv->backlight_device;
511 bd->props.brightness = value;
512 backlight_update_status(bd);
513#endif
514 }
515 } else if (!strcmp(property->name, "DPMS") && encoder) {
516 struct drm_encoder_helper_funcs *helpers =
517 encoder->helper_private;
518 helpers->dpms(encoder, value);
519 }
520 return 0;
521}
522
523static const struct drm_encoder_helper_funcs
524 cdv_intel_lvds_helper_funcs = {
525 .dpms = cdv_intel_lvds_encoder_dpms,
526 .mode_fixup = cdv_intel_lvds_mode_fixup,
527 .prepare = cdv_intel_lvds_prepare,
528 .mode_set = cdv_intel_lvds_mode_set,
529 .commit = cdv_intel_lvds_commit,
530};
531
532static const struct drm_connector_helper_funcs
533 cdv_intel_lvds_connector_helper_funcs = {
534 .get_modes = cdv_intel_lvds_get_modes,
535 .mode_valid = cdv_intel_lvds_mode_valid,
536 .best_encoder = psb_intel_best_encoder,
537};
538
539static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
540 .dpms = drm_helper_connector_dpms,
541 .save = cdv_intel_lvds_save,
542 .restore = cdv_intel_lvds_restore,
543 .detect = cdv_intel_lvds_detect,
544 .fill_modes = drm_helper_probe_single_connector_modes,
545 .set_property = cdv_intel_lvds_set_property,
546 .destroy = cdv_intel_lvds_destroy,
547};
548
549
550static void cdv_intel_lvds_enc_destroy(struct drm_encoder *encoder)
551{
552 drm_encoder_cleanup(encoder);
553}
554
555const struct drm_encoder_funcs cdv_intel_lvds_enc_funcs = {
556 .destroy = cdv_intel_lvds_enc_destroy,
557};
558
Alan Cox1b2db4c2012-04-25 14:37:27 +0100559/*
560 * Enumerate the child dev array parsed from VBT to check whether
561 * the LVDS is present.
562 * If it is present, return 1.
563 * If it is not present, return false.
564 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
565 */
566static bool lvds_is_present_in_vbt(struct drm_device *dev,
567 u8 *i2c_pin)
568{
569 struct drm_psb_private *dev_priv = dev->dev_private;
570 int i;
571
572 if (!dev_priv->child_dev_num)
573 return true;
574
575 for (i = 0; i < dev_priv->child_dev_num; i++) {
576 struct child_device_config *child = dev_priv->child_dev + i;
577
578 /* If the device type is not LFP, continue.
579 * We have to check both the new identifiers as well as the
580 * old for compatibility with some BIOSes.
581 */
582 if (child->device_type != DEVICE_TYPE_INT_LFP &&
583 child->device_type != DEVICE_TYPE_LFP)
584 continue;
585
586 if (child->i2c_pin)
587 *i2c_pin = child->i2c_pin;
588
589 /* However, we cannot trust the BIOS writers to populate
590 * the VBT correctly. Since LVDS requires additional
591 * information from AIM blocks, a non-zero addin offset is
592 * a good indicator that the LVDS is actually present.
593 */
594 if (child->addin_offset)
595 return true;
596
597 /* But even then some BIOS writers perform some black magic
598 * and instantiate the device without reference to any
599 * additional data. Trust that if the VBT was written into
600 * the OpRegion then they have validated the LVDS's existence.
601 */
602 if (dev_priv->opregion.vbt)
603 return true;
604 }
605
606 return false;
607}
608
Alan Cox6a227d52011-11-03 18:22:37 +0000609/**
610 * cdv_intel_lvds_init - setup LVDS connectors on this device
611 * @dev: drm device
612 *
613 * Create the connector, register the LVDS DDC bus, and try to figure out what
614 * modes we can display on the LVDS panel (if present).
615 */
616void cdv_intel_lvds_init(struct drm_device *dev,
617 struct psb_intel_mode_device *mode_dev)
618{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000619 struct psb_intel_encoder *psb_intel_encoder;
620 struct psb_intel_connector *psb_intel_connector;
Alan Cox6a227d52011-11-03 18:22:37 +0000621 struct cdv_intel_lvds_priv *lvds_priv;
622 struct drm_connector *connector;
623 struct drm_encoder *encoder;
624 struct drm_display_mode *scan;
625 struct drm_crtc *crtc;
626 struct drm_psb_private *dev_priv = dev->dev_private;
627 u32 lvds;
628 int pipe;
Alan Cox1b2db4c2012-04-25 14:37:27 +0100629 u8 pin;
630
631 pin = GMBUS_PORT_PANEL;
632 if (!lvds_is_present_in_vbt(dev, &pin)) {
633 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
634 return;
635 }
Alan Cox6a227d52011-11-03 18:22:37 +0000636
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000637 psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
638 GFP_KERNEL);
639 if (!psb_intel_encoder)
Alan Cox6a227d52011-11-03 18:22:37 +0000640 return;
641
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000642 psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
643 GFP_KERNEL);
644 if (!psb_intel_connector)
645 goto failed_connector;
Alan Cox6a227d52011-11-03 18:22:37 +0000646
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000647 lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
648 if (!lvds_priv)
649 goto failed_lvds_priv;
Alan Cox6a227d52011-11-03 18:22:37 +0000650
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000651 psb_intel_encoder->dev_priv = lvds_priv;
652
653 connector = &psb_intel_connector->base;
654 encoder = &psb_intel_encoder->base;
Alan Cox6a227d52011-11-03 18:22:37 +0000655
656
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000657 drm_connector_init(dev, connector,
Alan Cox6a227d52011-11-03 18:22:37 +0000658 &cdv_intel_lvds_connector_funcs,
659 DRM_MODE_CONNECTOR_LVDS);
660
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000661 drm_encoder_init(dev, encoder,
Alan Cox6a227d52011-11-03 18:22:37 +0000662 &cdv_intel_lvds_enc_funcs,
663 DRM_MODE_ENCODER_LVDS);
664
665
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000666 psb_intel_connector_attach_encoder(psb_intel_connector,
667 psb_intel_encoder);
668 psb_intel_encoder->type = INTEL_OUTPUT_LVDS;
Alan Cox6a227d52011-11-03 18:22:37 +0000669
670 drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
671 drm_connector_helper_add(connector,
672 &cdv_intel_lvds_connector_helper_funcs);
673 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
674 connector->interlace_allowed = false;
675 connector->doublescan_allowed = false;
676
677 /*Attach connector properties*/
678 drm_connector_attach_property(connector,
679 dev->mode_config.scaling_mode_property,
680 DRM_MODE_SCALE_FULLSCREEN);
681 drm_connector_attach_property(connector,
682 dev_priv->backlight_property,
683 BRIGHTNESS_MAX_LEVEL);
684
685 /**
686 * Set up I2C bus
687 * FIXME: distroy i2c_bus when exit
688 */
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000689 psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
Alan Cox6a227d52011-11-03 18:22:37 +0000690 GPIOB,
691 "LVDSBLC_B");
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000692 if (!psb_intel_encoder->i2c_bus) {
Alan Cox6a227d52011-11-03 18:22:37 +0000693 dev_printk(KERN_ERR,
694 &dev->pdev->dev, "I2C bus registration failed.\n");
695 goto failed_blc_i2c;
696 }
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000697 psb_intel_encoder->i2c_bus->slave_addr = 0x2C;
698 dev_priv->lvds_i2c_bus = psb_intel_encoder->i2c_bus;
Alan Cox6a227d52011-11-03 18:22:37 +0000699
700 /*
701 * LVDS discovery:
702 * 1) check for EDID on DDC
703 * 2) check for VBT data
704 * 3) check to see if LVDS is already on
705 * if none of the above, no panel
706 * 4) make sure lid is open
707 * if closed, act like it's not there for now
708 */
709
710 /* Set up the DDC bus. */
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000711 psb_intel_encoder->ddc_bus = psb_intel_i2c_create(dev,
Alan Cox6a227d52011-11-03 18:22:37 +0000712 GPIOC,
713 "LVDSDDC_C");
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000714 if (!psb_intel_encoder->ddc_bus) {
Alan Cox6a227d52011-11-03 18:22:37 +0000715 dev_printk(KERN_ERR, &dev->pdev->dev,
716 "DDC bus registration " "failed.\n");
717 goto failed_ddc;
718 }
719
720 /*
721 * Attempt to get the fixed panel mode from DDC. Assume that the
722 * preferred mode is the right one.
723 */
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000724 psb_intel_ddc_get_modes(connector,
725 &psb_intel_encoder->ddc_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000726 list_for_each_entry(scan, &connector->probed_modes, head) {
727 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
728 mode_dev->panel_fixed_mode =
729 drm_mode_duplicate(dev, scan);
730 goto out; /* FIXME: check for quirks */
731 }
732 }
733
734 /* Failed to get EDID, what about VBT? do we need this?*/
735 if (dev_priv->lfp_lvds_vbt_mode) {
736 mode_dev->panel_fixed_mode =
737 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
738 if (mode_dev->panel_fixed_mode) {
739 mode_dev->panel_fixed_mode->type |=
740 DRM_MODE_TYPE_PREFERRED;
741 goto out; /* FIXME: check for quirks */
742 }
743 }
744 /*
745 * If we didn't get EDID, try checking if the panel is already turned
746 * on. If so, assume that whatever is currently programmed is the
747 * correct mode.
748 */
749 lvds = REG_READ(LVDS);
750 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
751 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
752
753 if (crtc && (lvds & LVDS_PORT_EN)) {
754 mode_dev->panel_fixed_mode =
755 cdv_intel_crtc_mode_get(dev, crtc);
756 if (mode_dev->panel_fixed_mode) {
757 mode_dev->panel_fixed_mode->type |=
758 DRM_MODE_TYPE_PREFERRED;
759 goto out; /* FIXME: check for quirks */
760 }
761 }
762
763 /* If we still don't have a mode after all that, give up. */
764 if (!mode_dev->panel_fixed_mode) {
765 DRM_DEBUG
766 ("Found no modes on the lvds, ignoring the LVDS\n");
767 goto failed_find;
768 }
769
770out:
771 drm_sysfs_connector_add(connector);
772 return;
773
774failed_find:
775 printk(KERN_ERR "Failed find\n");
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000776 if (psb_intel_encoder->ddc_bus)
777 psb_intel_i2c_destroy(psb_intel_encoder->ddc_bus);
Alan Cox6a227d52011-11-03 18:22:37 +0000778failed_ddc:
779 printk(KERN_ERR "Failed DDC\n");
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000780 if (psb_intel_encoder->i2c_bus)
781 psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
Alan Cox6a227d52011-11-03 18:22:37 +0000782failed_blc_i2c:
783 printk(KERN_ERR "Failed BLC\n");
784 drm_encoder_cleanup(encoder);
785 drm_connector_cleanup(connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000786 kfree(lvds_priv);
787failed_lvds_priv:
788 kfree(psb_intel_connector);
789failed_connector:
790 kfree(psb_intel_encoder);
Alan Cox6a227d52011-11-03 18:22:37 +0000791}