Linux Audio

Check our new training course

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