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