1 /**************************************************************************
2 * Copyright (c) 2011, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 **************************************************************************/
20 #include <linux/backlight.h>
26 #include "psb_intel_reg.h"
27 #include "intel_bios.h"
28 #include "cdv_device.h"
30 #define VGA_SR_INDEX 0x3c4
31 #define VGA_SR_DATA 0x3c5
33 static void cdv_disable_vga(struct drm_device
*dev
)
40 outb(1, VGA_SR_INDEX
);
41 sr1
= inb(VGA_SR_DATA
);
42 outb(sr1
| 1<<5, VGA_SR_DATA
);
45 REG_WRITE(vga_reg
, VGA_DISP_DISABLE
);
49 static int cdv_output_init(struct drm_device
*dev
)
51 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
53 drm_mode_create_scaling_mode_property(dev
);
57 cdv_intel_crt_init(dev
, &dev_priv
->mode_dev
);
58 cdv_intel_lvds_init(dev
, &dev_priv
->mode_dev
);
60 /* These bits indicate HDMI not SDVO on CDV */
61 if (REG_READ(SDVOB
) & SDVO_DETECTED
)
62 cdv_hdmi_init(dev
, &dev_priv
->mode_dev
, SDVOB
);
63 if (REG_READ(SDVOC
) & SDVO_DETECTED
)
64 cdv_hdmi_init(dev
, &dev_priv
->mode_dev
, SDVOC
);
68 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
71 * Cedartrail Backlght Interfaces
74 static struct backlight_device
*cdv_backlight_device
;
76 static int cdv_backlight_combination_mode(struct drm_device
*dev
)
78 return REG_READ(BLC_PWM_CTL2
) & PWM_LEGACY_MODE
;
81 static u32
cdv_get_max_backlight(struct drm_device
*dev
)
83 u32 max
= REG_READ(BLC_PWM_CTL
);
86 DRM_DEBUG_KMS("LVDS Panel PWM value is 0!\n");
87 /* i915 does this, I believe which means that we should not
88 * smash PWM control as firmware will take control of it. */
93 if (cdv_backlight_combination_mode(dev
))
98 static int cdv_get_brightness(struct backlight_device
*bd
)
100 struct drm_device
*dev
= bl_get_data(bd
);
101 u32 val
= REG_READ(BLC_PWM_CTL
) & BACKLIGHT_DUTY_CYCLE_MASK
;
103 if (cdv_backlight_combination_mode(dev
)) {
107 pci_read_config_byte(dev
->pdev
, 0xF4, &lbpc
);
110 return (val
* 100)/cdv_get_max_backlight(dev
);
114 static int cdv_set_brightness(struct backlight_device
*bd
)
116 struct drm_device
*dev
= bl_get_data(bd
);
117 int level
= bd
->props
.brightness
;
120 /* Percentage 1-100% being valid */
124 level
*= cdv_get_max_backlight(dev
);
127 if (cdv_backlight_combination_mode(dev
)) {
128 u32 max
= cdv_get_max_backlight(dev
);
131 lbpc
= level
* 0xfe / max
+ 1;
134 pci_write_config_byte(dev
->pdev
, 0xF4, lbpc
);
137 blc_pwm_ctl
= REG_READ(BLC_PWM_CTL
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
138 REG_WRITE(BLC_PWM_CTL
, (blc_pwm_ctl
|
139 (level
<< BACKLIGHT_DUTY_CYCLE_SHIFT
)));
143 static const struct backlight_ops cdv_ops
= {
144 .get_brightness
= cdv_get_brightness
,
145 .update_status
= cdv_set_brightness
,
148 static int cdv_backlight_init(struct drm_device
*dev
)
150 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
151 struct backlight_properties props
;
153 memset(&props
, 0, sizeof(struct backlight_properties
));
154 props
.max_brightness
= 100;
155 props
.type
= BACKLIGHT_PLATFORM
;
157 cdv_backlight_device
= backlight_device_register("psb-bl",
158 NULL
, (void *)dev
, &cdv_ops
, &props
);
159 if (IS_ERR(cdv_backlight_device
))
160 return PTR_ERR(cdv_backlight_device
);
162 cdv_backlight_device
->props
.brightness
=
163 cdv_get_brightness(cdv_backlight_device
);
164 backlight_update_status(cdv_backlight_device
);
165 dev_priv
->backlight_device
= cdv_backlight_device
;
172 * Provide the Cedarview specific chip logic and low level methods
173 * for power management
175 * FIXME: we need to implement the apm/ospm base management bits
176 * for this and the MID devices.
179 static inline u32
CDV_MSG_READ32(uint port
, uint offset
)
181 int mcr
= (0x10<<24) | (port
<< 16) | (offset
<< 8);
182 uint32_t ret_val
= 0;
183 struct pci_dev
*pci_root
= pci_get_bus_and_slot(0, 0);
184 pci_write_config_dword(pci_root
, 0xD0, mcr
);
185 pci_read_config_dword(pci_root
, 0xD4, &ret_val
);
186 pci_dev_put(pci_root
);
190 static inline void CDV_MSG_WRITE32(uint port
, uint offset
, u32 value
)
192 int mcr
= (0x11<<24) | (port
<< 16) | (offset
<< 8) | 0xF0;
193 struct pci_dev
*pci_root
= pci_get_bus_and_slot(0, 0);
194 pci_write_config_dword(pci_root
, 0xD4, value
);
195 pci_write_config_dword(pci_root
, 0xD0, mcr
);
196 pci_dev_put(pci_root
);
199 #define PSB_PM_SSC 0x20
200 #define PSB_PM_SSS 0x30
201 #define PSB_PWRGT_GFX_ON 0x02
202 #define PSB_PWRGT_GFX_OFF 0x01
203 #define PSB_PWRGT_GFX_D0 0x00
204 #define PSB_PWRGT_GFX_D3 0x03
206 static void cdv_init_pm(struct drm_device
*dev
)
208 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
212 dev_priv
->apm_base
= CDV_MSG_READ32(PSB_PUNIT_PORT
,
214 dev_priv
->ospm_base
= CDV_MSG_READ32(PSB_PUNIT_PORT
,
215 PSB_OSPMBA
) & 0xFFFF;
218 pwr_cnt
= inl(dev_priv
->apm_base
+ PSB_APM_CMD
);
221 pwr_cnt
&= ~PSB_PWRGT_GFX_MASK
;
222 pwr_cnt
|= PSB_PWRGT_GFX_ON
;
223 outl(pwr_cnt
, dev_priv
->apm_base
+ PSB_APM_CMD
);
225 /* Wait for the GPU power */
226 for (i
= 0; i
< 5; i
++) {
227 u32 pwr_sts
= inl(dev_priv
->apm_base
+ PSB_APM_STS
);
228 if ((pwr_sts
& PSB_PWRGT_GFX_MASK
) == 0)
232 dev_err(dev
->dev
, "GPU: power management timed out.\n");
235 static void cdv_errata(struct drm_device
*dev
)
237 /* Disable bonus launch.
238 * CPU and GPU competes for memory and display misses updates and
239 * flickers. Worst with dual core, dual displays.
241 * Fixes were done to Win 7 gfx driver to disable a feature called
242 * Bonus Launch to work around the issue, by degrading
245 CDV_MSG_WRITE32(3, 0x30, 0x08027108);
249 * cdv_save_display_registers - save registers lost on suspend
250 * @dev: our DRM device
252 * Save the state we need in order to be able to restore the interface
253 * upon resume from suspend
255 static int cdv_save_display_registers(struct drm_device
*dev
)
257 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
258 struct psb_save_area
*regs
= &dev_priv
->regs
;
259 struct drm_connector
*connector
;
261 dev_dbg(dev
->dev
, "Saving GPU registers.\n");
263 pci_read_config_byte(dev
->pdev
, 0xF4, ®s
->cdv
.saveLBB
);
265 regs
->cdv
.saveDSPCLK_GATE_D
= REG_READ(DSPCLK_GATE_D
);
266 regs
->cdv
.saveRAMCLK_GATE_D
= REG_READ(RAMCLK_GATE_D
);
268 regs
->cdv
.saveDSPARB
= REG_READ(DSPARB
);
269 regs
->cdv
.saveDSPFW
[0] = REG_READ(DSPFW1
);
270 regs
->cdv
.saveDSPFW
[1] = REG_READ(DSPFW2
);
271 regs
->cdv
.saveDSPFW
[2] = REG_READ(DSPFW3
);
272 regs
->cdv
.saveDSPFW
[3] = REG_READ(DSPFW4
);
273 regs
->cdv
.saveDSPFW
[4] = REG_READ(DSPFW5
);
274 regs
->cdv
.saveDSPFW
[5] = REG_READ(DSPFW6
);
276 regs
->cdv
.saveADPA
= REG_READ(ADPA
);
278 regs
->cdv
.savePP_CONTROL
= REG_READ(PP_CONTROL
);
279 regs
->cdv
.savePFIT_PGM_RATIOS
= REG_READ(PFIT_PGM_RATIOS
);
280 regs
->saveBLC_PWM_CTL
= REG_READ(BLC_PWM_CTL
);
281 regs
->saveBLC_PWM_CTL2
= REG_READ(BLC_PWM_CTL2
);
282 regs
->cdv
.saveLVDS
= REG_READ(LVDS
);
284 regs
->cdv
.savePFIT_CONTROL
= REG_READ(PFIT_CONTROL
);
286 regs
->cdv
.savePP_ON_DELAYS
= REG_READ(PP_ON_DELAYS
);
287 regs
->cdv
.savePP_OFF_DELAYS
= REG_READ(PP_OFF_DELAYS
);
288 regs
->cdv
.savePP_CYCLE
= REG_READ(PP_CYCLE
);
290 regs
->cdv
.saveVGACNTRL
= REG_READ(VGACNTRL
);
292 regs
->cdv
.saveIER
= REG_READ(PSB_INT_ENABLE_R
);
293 regs
->cdv
.saveIMR
= REG_READ(PSB_INT_MASK_R
);
295 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
296 connector
->funcs
->dpms(connector
, DRM_MODE_DPMS_OFF
);
302 * cdv_restore_display_registers - restore lost register state
303 * @dev: our DRM device
305 * Restore register state that was lost during suspend and resume.
309 static int cdv_restore_display_registers(struct drm_device
*dev
)
311 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
312 struct psb_save_area
*regs
= &dev_priv
->regs
;
313 struct drm_connector
*connector
;
316 pci_write_config_byte(dev
->pdev
, 0xF4, regs
->cdv
.saveLBB
);
318 REG_WRITE(DSPCLK_GATE_D
, regs
->cdv
.saveDSPCLK_GATE_D
);
319 REG_WRITE(RAMCLK_GATE_D
, regs
->cdv
.saveRAMCLK_GATE_D
);
321 /* BIOS does below anyway */
322 REG_WRITE(DPIO_CFG
, 0);
323 REG_WRITE(DPIO_CFG
, DPIO_MODE_SELECT_0
| DPIO_CMN_RESET_N
);
325 temp
= REG_READ(DPLL_A
);
326 if ((temp
& DPLL_SYNCLOCK_ENABLE
) == 0) {
327 REG_WRITE(DPLL_A
, temp
| DPLL_SYNCLOCK_ENABLE
);
331 temp
= REG_READ(DPLL_B
);
332 if ((temp
& DPLL_SYNCLOCK_ENABLE
) == 0) {
333 REG_WRITE(DPLL_B
, temp
| DPLL_SYNCLOCK_ENABLE
);
339 REG_WRITE(DSPFW1
, regs
->cdv
.saveDSPFW
[0]);
340 REG_WRITE(DSPFW2
, regs
->cdv
.saveDSPFW
[1]);
341 REG_WRITE(DSPFW3
, regs
->cdv
.saveDSPFW
[2]);
342 REG_WRITE(DSPFW4
, regs
->cdv
.saveDSPFW
[3]);
343 REG_WRITE(DSPFW5
, regs
->cdv
.saveDSPFW
[4]);
344 REG_WRITE(DSPFW6
, regs
->cdv
.saveDSPFW
[5]);
346 REG_WRITE(DSPARB
, regs
->cdv
.saveDSPARB
);
347 REG_WRITE(ADPA
, regs
->cdv
.saveADPA
);
349 REG_WRITE(BLC_PWM_CTL2
, regs
->saveBLC_PWM_CTL2
);
350 REG_WRITE(LVDS
, regs
->cdv
.saveLVDS
);
351 REG_WRITE(PFIT_CONTROL
, regs
->cdv
.savePFIT_CONTROL
);
352 REG_WRITE(PFIT_PGM_RATIOS
, regs
->cdv
.savePFIT_PGM_RATIOS
);
353 REG_WRITE(BLC_PWM_CTL
, regs
->saveBLC_PWM_CTL
);
354 REG_WRITE(PP_ON_DELAYS
, regs
->cdv
.savePP_ON_DELAYS
);
355 REG_WRITE(PP_OFF_DELAYS
, regs
->cdv
.savePP_OFF_DELAYS
);
356 REG_WRITE(PP_CYCLE
, regs
->cdv
.savePP_CYCLE
);
357 REG_WRITE(PP_CONTROL
, regs
->cdv
.savePP_CONTROL
);
359 REG_WRITE(VGACNTRL
, regs
->cdv
.saveVGACNTRL
);
361 REG_WRITE(PSB_INT_ENABLE_R
, regs
->cdv
.saveIER
);
362 REG_WRITE(PSB_INT_MASK_R
, regs
->cdv
.saveIMR
);
364 /* Fix arbitration bug */
367 drm_mode_config_reset(dev
);
369 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
370 connector
->funcs
->dpms(connector
, DRM_MODE_DPMS_ON
);
372 /* Resume the modeset for every activated CRTC */
373 drm_helper_resume_force_mode(dev
);
377 static int cdv_power_down(struct drm_device
*dev
)
379 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
380 u32 pwr_cnt
, pwr_mask
, pwr_sts
;
383 pwr_cnt
= inl(dev_priv
->apm_base
+ PSB_APM_CMD
);
384 pwr_cnt
&= ~PSB_PWRGT_GFX_MASK
;
385 pwr_cnt
|= PSB_PWRGT_GFX_OFF
;
386 pwr_mask
= PSB_PWRGT_GFX_MASK
;
388 outl(pwr_cnt
, dev_priv
->apm_base
+ PSB_APM_CMD
);
391 pwr_sts
= inl(dev_priv
->apm_base
+ PSB_APM_STS
);
392 if ((pwr_sts
& pwr_mask
) == PSB_PWRGT_GFX_D3
)
399 static int cdv_power_up(struct drm_device
*dev
)
401 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
402 u32 pwr_cnt
, pwr_mask
, pwr_sts
;
405 pwr_cnt
= inl(dev_priv
->apm_base
+ PSB_APM_CMD
);
406 pwr_cnt
&= ~PSB_PWRGT_GFX_MASK
;
407 pwr_cnt
|= PSB_PWRGT_GFX_ON
;
408 pwr_mask
= PSB_PWRGT_GFX_MASK
;
410 outl(pwr_cnt
, dev_priv
->apm_base
+ PSB_APM_CMD
);
413 pwr_sts
= inl(dev_priv
->apm_base
+ PSB_APM_STS
);
414 if ((pwr_sts
& pwr_mask
) == PSB_PWRGT_GFX_D0
)
421 /* FIXME ? - shared with Poulsbo */
422 static void cdv_get_core_freq(struct drm_device
*dev
)
425 struct pci_dev
*pci_root
= pci_get_bus_and_slot(0, 0);
426 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
428 pci_write_config_dword(pci_root
, 0xD0, 0xD0050300);
429 pci_read_config_dword(pci_root
, 0xD4, &clock
);
430 pci_dev_put(pci_root
);
432 switch (clock
& 0x07) {
434 dev_priv
->core_freq
= 100;
437 dev_priv
->core_freq
= 133;
440 dev_priv
->core_freq
= 150;
443 dev_priv
->core_freq
= 178;
446 dev_priv
->core_freq
= 200;
451 dev_priv
->core_freq
= 266;
453 dev_priv
->core_freq
= 0;
457 static void cdv_hotplug_work_func(struct work_struct
*work
)
459 struct drm_psb_private
*dev_priv
= container_of(work
, struct drm_psb_private
,
461 struct drm_device
*dev
= dev_priv
->dev
;
463 /* Just fire off a uevent and let userspace tell us what to do */
464 drm_helper_hpd_irq_event(dev
);
467 /* The core driver has received a hotplug IRQ. We are in IRQ context
468 so extract the needed information and kick off queued processing */
470 static int cdv_hotplug_event(struct drm_device
*dev
)
472 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
473 schedule_work(&dev_priv
->hotplug_work
);
474 REG_WRITE(PORT_HOTPLUG_STAT
, REG_READ(PORT_HOTPLUG_STAT
));
478 static void cdv_hotplug_enable(struct drm_device
*dev
, bool on
)
481 u32 hotplug
= REG_READ(PORT_HOTPLUG_EN
);
482 hotplug
|= HDMIB_HOTPLUG_INT_EN
| HDMIC_HOTPLUG_INT_EN
|
483 HDMID_HOTPLUG_INT_EN
| CRT_HOTPLUG_INT_EN
;
484 REG_WRITE(PORT_HOTPLUG_EN
, hotplug
);
486 REG_WRITE(PORT_HOTPLUG_EN
, 0);
487 REG_WRITE(PORT_HOTPLUG_STAT
, REG_READ(PORT_HOTPLUG_STAT
));
492 static const struct psb_offset cdv_regmap
[2] = {
500 .dpll_md
= DPLL_A_MD
,
507 .stride
= DSPASTRIDE
,
514 .linoff
= DSPALINOFF
,
515 .tileoff
= DSPATILEOFF
,
516 .palette
= PALETTE_A
,
525 .dpll_md
= DPLL_B_MD
,
532 .stride
= DSPBSTRIDE
,
539 .linoff
= DSPBLINOFF
,
540 .tileoff
= DSPBTILEOFF
,
541 .palette
= PALETTE_B
,
545 static int cdv_chip_setup(struct drm_device
*dev
)
547 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
548 INIT_WORK(&dev_priv
->hotplug_work
, cdv_hotplug_work_func
);
550 if (pci_enable_msi(dev
->pdev
))
551 dev_warn(dev
->dev
, "Enabling MSI failed!\n");
552 dev_priv
->regmap
= cdv_regmap
;
553 cdv_get_core_freq(dev
);
554 psb_intel_opregion_init(dev
);
555 psb_intel_init_bios(dev
);
556 cdv_hotplug_enable(dev
, false);
560 /* CDV is much like Poulsbo but has MID like SGX offsets and PM */
562 const struct psb_ops cdv_chip_ops
= {
563 .name
= "GMA3600/3650",
567 .hdmi_mask
= (1 << 0) | (1 << 1),
568 .lvds_mask
= (1 << 1),
569 .cursor_needs_phys
= 0,
570 .sgx_offset
= MRST_SGX_OFFSET
,
571 .chip_setup
= cdv_chip_setup
,
572 .errata
= cdv_errata
,
574 .crtc_helper
= &cdv_intel_helper_funcs
,
575 .crtc_funcs
= &cdv_intel_crtc_funcs
,
577 .output_init
= cdv_output_init
,
578 .hotplug
= cdv_hotplug_event
,
579 .hotplug_enable
= cdv_hotplug_enable
,
581 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
582 .backlight_init
= cdv_backlight_init
,
585 .init_pm
= cdv_init_pm
,
586 .save_regs
= cdv_save_display_registers
,
587 .restore_regs
= cdv_restore_display_registers
,
588 .power_down
= cdv_power_down
,
589 .power_up
= cdv_power_up
,