Merge tag 'drm-misc-next-2022-06-23' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-microblaze.git] / drivers / gpu / drm / gma500 / psb_intel_lvds.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright © 2006-2007 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/i2c.h>
12 #include <linux/pm_runtime.h>
13
14 #include <drm/drm_simple_kms_helper.h>
15
16 #include "intel_bios.h"
17 #include "power.h"
18 #include "psb_drv.h"
19 #include "psb_intel_drv.h"
20 #include "psb_intel_reg.h"
21
22 /*
23  * LVDS I2C backlight control macros
24  */
25 #define BRIGHTNESS_MAX_LEVEL 100
26 #define BRIGHTNESS_MASK 0xFF
27 #define BLC_I2C_TYPE    0x01
28 #define BLC_PWM_TYPT    0x02
29
30 #define BLC_POLARITY_NORMAL 0
31 #define BLC_POLARITY_INVERSE 1
32
33 #define PSB_BLC_MAX_PWM_REG_FREQ       (0xFFFE)
34 #define PSB_BLC_MIN_PWM_REG_FREQ        (0x2)
35 #define PSB_BLC_PWM_PRECISION_FACTOR    (10)
36 #define PSB_BACKLIGHT_PWM_CTL_SHIFT     (16)
37 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
38
39 struct psb_intel_lvds_priv {
40         /*
41          * Saved LVDO output states
42          */
43         uint32_t savePP_ON;
44         uint32_t savePP_OFF;
45         uint32_t saveLVDS;
46         uint32_t savePP_CONTROL;
47         uint32_t savePP_CYCLE;
48         uint32_t savePFIT_CONTROL;
49         uint32_t savePFIT_PGM_RATIOS;
50         uint32_t saveBLC_PWM_CTL;
51
52         struct gma_i2c_chan *i2c_bus;
53 };
54
55
56 /*
57  * Returns the maximum level of the backlight duty cycle field.
58  */
59 static u32 psb_intel_lvds_get_max_backlight(struct drm_device *dev)
60 {
61         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
62         u32 ret;
63
64         if (gma_power_begin(dev, false)) {
65                 ret = REG_READ(BLC_PWM_CTL);
66                 gma_power_end(dev);
67         } else /* Powered off, use the saved value */
68                 ret = dev_priv->regs.saveBLC_PWM_CTL;
69
70         /* Top 15bits hold the frequency mask */
71         ret = (ret &  BACKLIGHT_MODULATION_FREQ_MASK) >>
72                                         BACKLIGHT_MODULATION_FREQ_SHIFT;
73
74         ret *= 2;       /* Return a 16bit range as needed for setting */
75         if (ret == 0)
76                 dev_err(dev->dev, "BL bug: Reg %08x save %08X\n",
77                         REG_READ(BLC_PWM_CTL), dev_priv->regs.saveBLC_PWM_CTL);
78         return ret;
79 }
80
81 /*
82  * Set LVDS backlight level by I2C command
83  *
84  * FIXME: at some point we need to both track this for PM and also
85  * disable runtime pm on MRST if the brightness is nil (ie blanked)
86  */
87 static int psb_lvds_i2c_set_brightness(struct drm_device *dev,
88                                         unsigned int level)
89 {
90         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
91
92         struct gma_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
93         u8 out_buf[2];
94         unsigned int blc_i2c_brightness;
95
96         struct i2c_msg msgs[] = {
97                 {
98                         .addr = lvds_i2c_bus->slave_addr,
99                         .flags = 0,
100                         .len = 2,
101                         .buf = out_buf,
102                 }
103         };
104
105         blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
106                              BRIGHTNESS_MASK /
107                              BRIGHTNESS_MAX_LEVEL);
108
109         if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
110                 blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
111
112         out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
113         out_buf[1] = (u8)blc_i2c_brightness;
114
115         if (i2c_transfer(&lvds_i2c_bus->base, msgs, 1) == 1) {
116                 dev_dbg(dev->dev, "I2C set brightness.(command, value) (%d, %d)\n",
117                         dev_priv->lvds_bl->brightnesscmd,
118                         blc_i2c_brightness);
119                 return 0;
120         }
121
122         dev_err(dev->dev, "I2C transfer error\n");
123         return -1;
124 }
125
126
127 static int psb_lvds_pwm_set_brightness(struct drm_device *dev, int level)
128 {
129         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
130
131         u32 max_pwm_blc;
132         u32 blc_pwm_duty_cycle;
133
134         max_pwm_blc = psb_intel_lvds_get_max_backlight(dev);
135
136         /*BLC_PWM_CTL Should be initiated while backlight device init*/
137         BUG_ON(max_pwm_blc == 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         dev_info(dev->dev, "Backlight lvds set brightness %08x\n",
150                   (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
151                   (blc_pwm_duty_cycle));
152
153         return 0;
154 }
155
156 /*
157  * Set LVDS backlight level either by I2C or PWM
158  */
159 void psb_intel_lvds_set_brightness(struct drm_device *dev, int level)
160 {
161         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
162
163         dev_dbg(dev->dev, "backlight level is %d\n", level);
164
165         if (!dev_priv->lvds_bl) {
166                 dev_err(dev->dev, "NO LVDS backlight info\n");
167                 return;
168         }
169
170         if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
171                 psb_lvds_i2c_set_brightness(dev, level);
172         else
173                 psb_lvds_pwm_set_brightness(dev, level);
174 }
175
176 /*
177  * Sets the backlight level.
178  *
179  * level: backlight level, from 0 to psb_intel_lvds_get_max_backlight().
180  */
181 static void psb_intel_lvds_set_backlight(struct drm_device *dev, int level)
182 {
183         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
184         u32 blc_pwm_ctl;
185
186         if (gma_power_begin(dev, false)) {
187                 blc_pwm_ctl = REG_READ(BLC_PWM_CTL);
188                 blc_pwm_ctl &= ~BACKLIGHT_DUTY_CYCLE_MASK;
189                 REG_WRITE(BLC_PWM_CTL,
190                                 (blc_pwm_ctl |
191                                 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
192                 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
193                                         (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
194                 gma_power_end(dev);
195         } else {
196                 blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
197                                 ~BACKLIGHT_DUTY_CYCLE_MASK;
198                 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
199                                         (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
200         }
201 }
202
203 /*
204  * Sets the power state for the panel.
205  */
206 static void psb_intel_lvds_set_power(struct drm_device *dev, bool on)
207 {
208         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
209         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
210         u32 pp_status;
211
212         if (!gma_power_begin(dev, true)) {
213                 dev_err(dev->dev, "set power, chip off!\n");
214                 return;
215         }
216
217         if (on) {
218                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
219                           POWER_TARGET_ON);
220                 do {
221                         pp_status = REG_READ(PP_STATUS);
222                 } while ((pp_status & PP_ON) == 0);
223
224                 psb_intel_lvds_set_backlight(dev,
225                                              mode_dev->backlight_duty_cycle);
226         } else {
227                 psb_intel_lvds_set_backlight(dev, 0);
228
229                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
230                           ~POWER_TARGET_ON);
231                 do {
232                         pp_status = REG_READ(PP_STATUS);
233                 } while (pp_status & PP_ON);
234         }
235
236         gma_power_end(dev);
237 }
238
239 static void psb_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
240 {
241         struct drm_device *dev = encoder->dev;
242
243         if (mode == DRM_MODE_DPMS_ON)
244                 psb_intel_lvds_set_power(dev, true);
245         else
246                 psb_intel_lvds_set_power(dev, false);
247
248         /* XXX: We never power down the LVDS pairs. */
249 }
250
251 static void psb_intel_lvds_save(struct drm_connector *connector)
252 {
253         struct drm_device *dev = connector->dev;
254         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
255         struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
256         struct psb_intel_lvds_priv *lvds_priv =
257                 (struct psb_intel_lvds_priv *)gma_encoder->dev_priv;
258
259         lvds_priv->savePP_ON = REG_READ(LVDSPP_ON);
260         lvds_priv->savePP_OFF = REG_READ(LVDSPP_OFF);
261         lvds_priv->saveLVDS = REG_READ(LVDS);
262         lvds_priv->savePP_CONTROL = REG_READ(PP_CONTROL);
263         lvds_priv->savePP_CYCLE = REG_READ(PP_CYCLE);
264         /*lvds_priv->savePP_DIVISOR = REG_READ(PP_DIVISOR);*/
265         lvds_priv->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
266         lvds_priv->savePFIT_CONTROL = REG_READ(PFIT_CONTROL);
267         lvds_priv->savePFIT_PGM_RATIOS = REG_READ(PFIT_PGM_RATIOS);
268
269         /*TODO: move backlight_duty_cycle to psb_intel_lvds_priv*/
270         dev_priv->backlight_duty_cycle = (dev_priv->regs.saveBLC_PWM_CTL &
271                                                 BACKLIGHT_DUTY_CYCLE_MASK);
272
273         /*
274          * If the light is off at server startup,
275          * just make it full brightness
276          */
277         if (dev_priv->backlight_duty_cycle == 0)
278                 dev_priv->backlight_duty_cycle =
279                 psb_intel_lvds_get_max_backlight(dev);
280
281         dev_dbg(dev->dev, "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
282                         lvds_priv->savePP_ON,
283                         lvds_priv->savePP_OFF,
284                         lvds_priv->saveLVDS,
285                         lvds_priv->savePP_CONTROL,
286                         lvds_priv->savePP_CYCLE,
287                         lvds_priv->saveBLC_PWM_CTL);
288 }
289
290 static void psb_intel_lvds_restore(struct drm_connector *connector)
291 {
292         struct drm_device *dev = connector->dev;
293         u32 pp_status;
294         struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
295         struct psb_intel_lvds_priv *lvds_priv =
296                 (struct psb_intel_lvds_priv *)gma_encoder->dev_priv;
297
298         dev_dbg(dev->dev, "(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
299                         lvds_priv->savePP_ON,
300                         lvds_priv->savePP_OFF,
301                         lvds_priv->saveLVDS,
302                         lvds_priv->savePP_CONTROL,
303                         lvds_priv->savePP_CYCLE,
304                         lvds_priv->saveBLC_PWM_CTL);
305
306         REG_WRITE(BLC_PWM_CTL, lvds_priv->saveBLC_PWM_CTL);
307         REG_WRITE(PFIT_CONTROL, lvds_priv->savePFIT_CONTROL);
308         REG_WRITE(PFIT_PGM_RATIOS, lvds_priv->savePFIT_PGM_RATIOS);
309         REG_WRITE(LVDSPP_ON, lvds_priv->savePP_ON);
310         REG_WRITE(LVDSPP_OFF, lvds_priv->savePP_OFF);
311         /*REG_WRITE(PP_DIVISOR, lvds_priv->savePP_DIVISOR);*/
312         REG_WRITE(PP_CYCLE, lvds_priv->savePP_CYCLE);
313         REG_WRITE(PP_CONTROL, lvds_priv->savePP_CONTROL);
314         REG_WRITE(LVDS, lvds_priv->saveLVDS);
315
316         if (lvds_priv->savePP_CONTROL & POWER_TARGET_ON) {
317                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
318                         POWER_TARGET_ON);
319                 do {
320                         pp_status = REG_READ(PP_STATUS);
321                 } while ((pp_status & PP_ON) == 0);
322         } else {
323                 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
324                         ~POWER_TARGET_ON);
325                 do {
326                         pp_status = REG_READ(PP_STATUS);
327                 } while (pp_status & PP_ON);
328         }
329 }
330
331 enum drm_mode_status psb_intel_lvds_mode_valid(struct drm_connector *connector,
332                                  struct drm_display_mode *mode)
333 {
334         struct drm_psb_private *dev_priv = to_drm_psb_private(connector->dev);
335         struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
336         struct drm_display_mode *fixed_mode =
337                                         dev_priv->mode_dev.panel_fixed_mode;
338
339         if (gma_encoder->type == INTEL_OUTPUT_MIPI2)
340                 fixed_mode = dev_priv->mode_dev.panel_fixed_mode2;
341
342         /* just in case */
343         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
344                 return MODE_NO_DBLESCAN;
345
346         /* just in case */
347         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
348                 return MODE_NO_INTERLACE;
349
350         if (fixed_mode) {
351                 if (mode->hdisplay > fixed_mode->hdisplay)
352                         return MODE_PANEL;
353                 if (mode->vdisplay > fixed_mode->vdisplay)
354                         return MODE_PANEL;
355         }
356         return MODE_OK;
357 }
358
359 bool psb_intel_lvds_mode_fixup(struct drm_encoder *encoder,
360                                   const struct drm_display_mode *mode,
361                                   struct drm_display_mode *adjusted_mode)
362 {
363         struct drm_device *dev = encoder->dev;
364         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
365         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
366         struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
367         struct drm_encoder *tmp_encoder;
368         struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
369         struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
370
371         if (gma_encoder->type == INTEL_OUTPUT_MIPI2)
372                 panel_fixed_mode = mode_dev->panel_fixed_mode2;
373
374         /* PSB requires the LVDS is on pipe B, MRST has only one pipe anyway */
375         if (!IS_MRST(dev) && gma_crtc->pipe == 0) {
376                 pr_err("Can't support LVDS on pipe A\n");
377                 return false;
378         }
379         if (IS_MRST(dev) && gma_crtc->pipe != 0) {
380                 pr_err("Must use PIPE A\n");
381                 return false;
382         }
383         /* Should never happen!! */
384         list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
385                             head) {
386                 if (tmp_encoder != encoder
387                     && tmp_encoder->crtc == encoder->crtc) {
388                         pr_err("Can't enable LVDS and another encoder on the same pipe\n");
389                         return false;
390                 }
391         }
392
393         /*
394          * If we have timings from the BIOS for the panel, put them in
395          * to the adjusted mode.  The CRTC will be set up for this mode,
396          * with the panel scaling set up to source from the H/VDisplay
397          * of the original mode.
398          */
399         if (panel_fixed_mode != NULL) {
400                 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
401                 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
402                 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
403                 adjusted_mode->htotal = panel_fixed_mode->htotal;
404                 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
405                 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
406                 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
407                 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
408                 adjusted_mode->clock = panel_fixed_mode->clock;
409                 drm_mode_set_crtcinfo(adjusted_mode,
410                                       CRTC_INTERLACE_HALVE_V);
411         }
412
413         /*
414          * XXX: It would be nice to support lower refresh rates on the
415          * panels to reduce power consumption, and perhaps match the
416          * user's requested refresh rate.
417          */
418
419         return true;
420 }
421
422 static void psb_intel_lvds_prepare(struct drm_encoder *encoder)
423 {
424         struct drm_device *dev = encoder->dev;
425         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
426         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
427
428         if (!gma_power_begin(dev, true))
429                 return;
430
431         mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
432         mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
433                                           BACKLIGHT_DUTY_CYCLE_MASK);
434
435         psb_intel_lvds_set_power(dev, false);
436
437         gma_power_end(dev);
438 }
439
440 static void psb_intel_lvds_commit(struct drm_encoder *encoder)
441 {
442         struct drm_device *dev = encoder->dev;
443         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
444         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
445
446         if (mode_dev->backlight_duty_cycle == 0)
447                 mode_dev->backlight_duty_cycle =
448                     psb_intel_lvds_get_max_backlight(dev);
449
450         psb_intel_lvds_set_power(dev, true);
451 }
452
453 static void psb_intel_lvds_mode_set(struct drm_encoder *encoder,
454                                 struct drm_display_mode *mode,
455                                 struct drm_display_mode *adjusted_mode)
456 {
457         struct drm_device *dev = encoder->dev;
458         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
459         u32 pfit_control;
460
461         /*
462          * The LVDS pin pair will already have been turned on in the
463          * psb_intel_crtc_mode_set since it has a large impact on the DPLL
464          * settings.
465          */
466
467         /*
468          * Enable automatic panel scaling so that non-native modes fill the
469          * screen.  Should be enabled before the pipe is enabled, according to
470          * register description and PRM.
471          */
472         if (mode->hdisplay != adjusted_mode->hdisplay ||
473             mode->vdisplay != adjusted_mode->vdisplay)
474                 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
475                                 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
476                                 HORIZ_INTERP_BILINEAR);
477         else
478                 pfit_control = 0;
479
480         if (dev_priv->lvds_dither)
481                 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
482
483         REG_WRITE(PFIT_CONTROL, pfit_control);
484 }
485
486 /*
487  * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
488  */
489 static int psb_intel_lvds_get_modes(struct drm_connector *connector)
490 {
491         struct drm_device *dev = connector->dev;
492         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
493         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
494         int ret = 0;
495
496         if (!IS_MRST(dev))
497                 ret = psb_intel_ddc_get_modes(connector, connector->ddc);
498
499         if (ret)
500                 return ret;
501
502         if (mode_dev->panel_fixed_mode != NULL) {
503                 struct drm_display_mode *mode =
504                     drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
505                 drm_mode_probed_add(connector, mode);
506                 return 1;
507         }
508
509         return 0;
510 }
511
512 void psb_intel_lvds_destroy(struct drm_connector *connector)
513 {
514         struct gma_connector *gma_connector = to_gma_connector(connector);
515         struct gma_i2c_chan *ddc_bus = to_gma_i2c_chan(connector->ddc);
516
517         gma_i2c_destroy(ddc_bus);
518         drm_connector_cleanup(connector);
519         kfree(gma_connector);
520 }
521
522 int psb_intel_lvds_set_property(struct drm_connector *connector,
523                                        struct drm_property *property,
524                                        uint64_t value)
525 {
526         struct drm_encoder *encoder = connector->encoder;
527
528         if (!encoder)
529                 return -1;
530
531         if (!strcmp(property->name, "scaling mode")) {
532                 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
533                 uint64_t curval;
534
535                 if (!crtc)
536                         goto set_prop_error;
537
538                 switch (value) {
539                 case DRM_MODE_SCALE_FULLSCREEN:
540                         break;
541                 case DRM_MODE_SCALE_NO_SCALE:
542                         break;
543                 case DRM_MODE_SCALE_ASPECT:
544                         break;
545                 default:
546                         goto set_prop_error;
547                 }
548
549                 if (drm_object_property_get_value(&connector->base,
550                                                      property,
551                                                      &curval))
552                         goto set_prop_error;
553
554                 if (curval == value)
555                         goto set_prop_done;
556
557                 if (drm_object_property_set_value(&connector->base,
558                                                         property,
559                                                         value))
560                         goto set_prop_error;
561
562                 if (crtc->saved_mode.hdisplay != 0 &&
563                     crtc->saved_mode.vdisplay != 0) {
564                         if (!drm_crtc_helper_set_mode(encoder->crtc,
565                                                       &crtc->saved_mode,
566                                                       encoder->crtc->x,
567                                                       encoder->crtc->y,
568                                                       encoder->crtc->primary->fb))
569                                 goto set_prop_error;
570                 }
571         } else if (!strcmp(property->name, "backlight")) {
572                 if (drm_object_property_set_value(&connector->base,
573                                                         property,
574                                                         value))
575                         goto set_prop_error;
576                 else
577                         gma_backlight_set(encoder->dev, value);
578         } else if (!strcmp(property->name, "DPMS")) {
579                 const struct drm_encoder_helper_funcs *hfuncs
580                                                 = encoder->helper_private;
581                 hfuncs->dpms(encoder, value);
582         }
583
584 set_prop_done:
585         return 0;
586 set_prop_error:
587         return -1;
588 }
589
590 static const struct drm_encoder_helper_funcs psb_intel_lvds_helper_funcs = {
591         .dpms = psb_intel_lvds_encoder_dpms,
592         .mode_fixup = psb_intel_lvds_mode_fixup,
593         .prepare = psb_intel_lvds_prepare,
594         .mode_set = psb_intel_lvds_mode_set,
595         .commit = psb_intel_lvds_commit,
596 };
597
598 const struct drm_connector_helper_funcs
599                                 psb_intel_lvds_connector_helper_funcs = {
600         .get_modes = psb_intel_lvds_get_modes,
601         .mode_valid = psb_intel_lvds_mode_valid,
602         .best_encoder = gma_best_encoder,
603 };
604
605 const struct drm_connector_funcs psb_intel_lvds_connector_funcs = {
606         .dpms = drm_helper_connector_dpms,
607         .fill_modes = drm_helper_probe_single_connector_modes,
608         .set_property = psb_intel_lvds_set_property,
609         .destroy = psb_intel_lvds_destroy,
610 };
611
612 /**
613  * psb_intel_lvds_init - setup LVDS connectors on this device
614  * @dev: drm device
615  * @mode_dev: mode device
616  *
617  * Create the connector, register the LVDS DDC bus, and try to figure out what
618  * modes we can display on the LVDS panel (if present).
619  */
620 void psb_intel_lvds_init(struct drm_device *dev,
621                          struct psb_intel_mode_device *mode_dev)
622 {
623         struct gma_encoder *gma_encoder;
624         struct gma_connector *gma_connector;
625         struct psb_intel_lvds_priv *lvds_priv;
626         struct drm_connector *connector;
627         struct drm_encoder *encoder;
628         struct drm_display_mode *scan;  /* *modes, *bios_mode; */
629         struct drm_crtc *crtc;
630         struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
631         struct gma_i2c_chan *ddc_bus;
632         u32 lvds;
633         int pipe;
634         int ret;
635
636         gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
637         if (!gma_encoder) {
638                 dev_err(dev->dev, "gma_encoder allocation error\n");
639                 return;
640         }
641         encoder = &gma_encoder->base;
642
643         gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
644         if (!gma_connector) {
645                 dev_err(dev->dev, "gma_connector allocation error\n");
646                 goto err_free_encoder;
647         }
648
649         lvds_priv = kzalloc(sizeof(struct psb_intel_lvds_priv), GFP_KERNEL);
650         if (!lvds_priv) {
651                 dev_err(dev->dev, "LVDS private allocation error\n");
652                 goto err_free_connector;
653         }
654
655         gma_encoder->dev_priv = lvds_priv;
656
657         connector = &gma_connector->base;
658         gma_connector->save = psb_intel_lvds_save;
659         gma_connector->restore = psb_intel_lvds_restore;
660
661         /* Set up the DDC bus. */
662         ddc_bus = gma_i2c_create(dev, GPIOC, "LVDSDDC_C");
663         if (!ddc_bus) {
664                 dev_printk(KERN_ERR, dev->dev,
665                            "DDC bus registration " "failed.\n");
666                 goto err_free_lvds_priv;
667         }
668
669         ret = drm_connector_init_with_ddc(dev, connector,
670                                           &psb_intel_lvds_connector_funcs,
671                                           DRM_MODE_CONNECTOR_LVDS,
672                                           &ddc_bus->base);
673         if (ret)
674                 goto err_ddc_destroy;
675
676         ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
677         if (ret)
678                 goto err_connector_cleanup;
679
680         gma_connector_attach_encoder(gma_connector, gma_encoder);
681         gma_encoder->type = INTEL_OUTPUT_LVDS;
682
683         drm_encoder_helper_add(encoder, &psb_intel_lvds_helper_funcs);
684         drm_connector_helper_add(connector,
685                                  &psb_intel_lvds_connector_helper_funcs);
686         connector->display_info.subpixel_order = SubPixelHorizontalRGB;
687         connector->interlace_allowed = false;
688         connector->doublescan_allowed = false;
689
690         /*Attach connector properties*/
691         drm_object_attach_property(&connector->base,
692                                       dev->mode_config.scaling_mode_property,
693                                       DRM_MODE_SCALE_FULLSCREEN);
694         drm_object_attach_property(&connector->base,
695                                       dev_priv->backlight_property,
696                                       BRIGHTNESS_MAX_LEVEL);
697
698         /*
699          * Set up I2C bus
700          * FIXME: distroy i2c_bus when exit
701          */
702         lvds_priv->i2c_bus = gma_i2c_create(dev, GPIOB, "LVDSBLC_B");
703         if (!lvds_priv->i2c_bus) {
704                 dev_printk(KERN_ERR,
705                         dev->dev, "I2C bus registration failed.\n");
706                 goto err_encoder_cleanup;
707         }
708         lvds_priv->i2c_bus->slave_addr = 0x2C;
709         dev_priv->lvds_i2c_bus =  lvds_priv->i2c_bus;
710
711         /*
712          * LVDS discovery:
713          * 1) check for EDID on DDC
714          * 2) check for VBT data
715          * 3) check to see if LVDS is already on
716          *    if none of the above, no panel
717          * 4) make sure lid is open
718          *    if closed, act like it's not there for now
719          */
720
721         /*
722          * Attempt to get the fixed panel mode from DDC.  Assume that the
723          * preferred mode is the right one.
724          */
725         mutex_lock(&dev->mode_config.mutex);
726         psb_intel_ddc_get_modes(connector, &ddc_bus->base);
727
728         list_for_each_entry(scan, &connector->probed_modes, head) {
729                 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
730                         mode_dev->panel_fixed_mode =
731                             drm_mode_duplicate(dev, scan);
732                         DRM_DEBUG_KMS("Using mode from DDC\n");
733                         goto out;       /* FIXME: check for quirks */
734                 }
735         }
736
737         /* Failed to get EDID, what about VBT? do we need this? */
738         if (dev_priv->lfp_lvds_vbt_mode) {
739                 mode_dev->panel_fixed_mode =
740                         drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
741
742                 if (mode_dev->panel_fixed_mode) {
743                         mode_dev->panel_fixed_mode->type |=
744                                 DRM_MODE_TYPE_PREFERRED;
745                         DRM_DEBUG_KMS("Using mode from VBT\n");
746                         goto out;
747                 }
748         }
749
750         /*
751          * If we didn't get EDID, try checking if the panel is already turned
752          * on.  If so, assume that whatever is currently programmed is the
753          * correct mode.
754          */
755         lvds = REG_READ(LVDS);
756         pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
757         crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
758
759         if (crtc && (lvds & LVDS_PORT_EN)) {
760                 mode_dev->panel_fixed_mode =
761                     psb_intel_crtc_mode_get(dev, crtc);
762                 if (mode_dev->panel_fixed_mode) {
763                         mode_dev->panel_fixed_mode->type |=
764                             DRM_MODE_TYPE_PREFERRED;
765                         DRM_DEBUG_KMS("Using pre-programmed mode\n");
766                         goto out;       /* FIXME: check for quirks */
767                 }
768         }
769
770         /* If we still don't have a mode after all that, give up. */
771         if (!mode_dev->panel_fixed_mode) {
772                 dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
773                 goto err_unlock;
774         }
775
776         /*
777          * Blacklist machines with BIOSes that list an LVDS panel without
778          * actually having one.
779          */
780 out:
781         mutex_unlock(&dev->mode_config.mutex);
782         return;
783
784 err_unlock:
785         mutex_unlock(&dev->mode_config.mutex);
786         gma_i2c_destroy(lvds_priv->i2c_bus);
787 err_encoder_cleanup:
788         drm_encoder_cleanup(encoder);
789 err_connector_cleanup:
790         drm_connector_cleanup(connector);
791 err_ddc_destroy:
792         gma_i2c_destroy(ddc_bus);
793 err_free_lvds_priv:
794         kfree(lvds_priv);
795 err_free_connector:
796         kfree(gma_connector);
797 err_free_encoder:
798         kfree(gma_encoder);
799 }
800