2 * Copyright 2008 Intel Corporation <hong.liu@intel.com>
3 * Copyright 2008 Red Hat <mjg@redhat.com>
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT. IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/sys/dev/drm2/i915/intel_opregion.c,v 1.1 2012/05/22 11:07:44 kib Exp $");
31 #include <dev/drm2/drmP.h>
32 #include <dev/drm2/i915/i915_drm.h>
33 #include <dev/drm2/i915/i915_drv.h>
34 #include <dev/drm2/i915/intel_drv.h>
39 #define OPREGION_HEADER_OFFSET 0
40 #define OPREGION_ACPI_OFFSET 0x100
41 #define ACPI_CLID 0x01ac /* current lid state indicator */
42 #define ACPI_CDCK 0x01b0 /* current docking state indicator */
43 #define OPREGION_SWSCI_OFFSET 0x200
44 #define OPREGION_ASLE_OFFSET 0x300
45 #define OPREGION_VBT_OFFSET 0x400
47 #define OPREGION_SIGNATURE "IntelGraphicsMem"
48 #define MBOX_ACPI (1<<0)
49 #define MBOX_SWSCI (1<<1)
50 #define MBOX_ASLE (1<<2)
52 struct opregion_header {
61 } __attribute__((packed));
63 /* OpRegion mailbox #1: public ACPI methods */
64 struct opregion_acpi {
65 u32 drdy; /* driver readiness */
66 u32 csts; /* notification status */
67 u32 cevt; /* current event */
69 u32 didl[8]; /* supported display devices ID list */
70 u32 cpdl[8]; /* currently presented display list */
71 u32 cadl[8]; /* currently active display list */
72 u32 nadl[8]; /* next active devices list */
73 u32 aslp; /* ASL sleep time-out */
74 u32 tidx; /* toggle table index */
75 u32 chpd; /* current hotplug enable indicator */
76 u32 clid; /* current lid state*/
77 u32 cdck; /* current docking state */
78 u32 sxsw; /* Sx state resume */
79 u32 evts; /* ASL supported events */
80 u32 cnot; /* current OS notification */
81 u32 nrdy; /* driver status */
83 } __attribute__((packed));
85 /* OpRegion mailbox #2: SWSCI */
86 struct opregion_swsci {
87 u32 scic; /* SWSCI command|status|data */
88 u32 parm; /* command parameters */
89 u32 dslp; /* driver sleep time-out */
91 } __attribute__((packed));
93 /* OpRegion mailbox #3: ASLE */
94 struct opregion_asle {
95 u32 ardy; /* driver readiness */
96 u32 aslc; /* ASLE interrupt command */
97 u32 tche; /* technology enabled indicator */
98 u32 alsi; /* current ALS illuminance reading */
99 u32 bclp; /* backlight brightness to set */
100 u32 pfit; /* panel fitting state */
101 u32 cblv; /* current brightness level */
102 u16 bclm[20]; /* backlight level duty cycle mapping table */
103 u32 cpfm; /* current panel fitting mode */
104 u32 epfm; /* enabled panel fitting modes */
105 u8 plut[74]; /* panel LUT and identifier */
106 u32 pfmb; /* PWM freq and min brightness */
108 } __attribute__((packed));
110 /* ASLE irq request bits */
111 #define ASLE_SET_ALS_ILLUM (1 << 0)
112 #define ASLE_SET_BACKLIGHT (1 << 1)
113 #define ASLE_SET_PFIT (1 << 2)
114 #define ASLE_SET_PWM_FREQ (1 << 3)
115 #define ASLE_REQ_MSK 0xf
117 /* response bits of ASLE irq request */
118 #define ASLE_ALS_ILLUM_FAILED (1<<10)
119 #define ASLE_BACKLIGHT_FAILED (1<<12)
120 #define ASLE_PFIT_FAILED (1<<14)
121 #define ASLE_PWM_FREQ_FAILED (1<<16)
123 /* ASLE backlight brightness to set */
124 #define ASLE_BCLP_VALID (1<<31)
125 #define ASLE_BCLP_MSK (~(1<<31))
127 /* ASLE panel fitting request */
128 #define ASLE_PFIT_VALID (1<<31)
129 #define ASLE_PFIT_CENTER (1<<0)
130 #define ASLE_PFIT_STRETCH_TEXT (1<<1)
131 #define ASLE_PFIT_STRETCH_GFX (1<<2)
133 /* PWM frequency and minimum brightness */
134 #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
135 #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
136 #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
137 #define ASLE_PFMB_PWM_VALID (1<<31)
139 #define ASLE_CBLV_VALID (1<<31)
141 #define ACPI_OTHER_OUTPUT (0<<8)
142 #define ACPI_VGA_OUTPUT (1<<8)
143 #define ACPI_TV_OUTPUT (2<<8)
144 #define ACPI_DIGITAL_OUTPUT (3<<8)
145 #define ACPI_LVDS_OUTPUT (4<<8)
148 static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct opregion_asle *asle = dev_priv->opregion.asle;
154 if (!(bclp & ASLE_BCLP_VALID))
155 return ASLE_BACKLIGHT_FAILED;
157 bclp &= ASLE_BCLP_MSK;
159 return ASLE_BACKLIGHT_FAILED;
161 max = intel_panel_get_max_backlight(dev);
162 intel_panel_set_backlight(dev, bclp * max / 255);
163 asle->cblv = (bclp*0x64)/0xff | ASLE_CBLV_VALID;
168 static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
170 /* alsi is the current ALS reading in lux. 0 indicates below sensor
171 range, 0xffff indicates above sensor range. 1-0xfffe are valid */
175 static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
177 struct drm_i915_private *dev_priv = dev->dev_private;
178 if (pfmb & ASLE_PFMB_PWM_VALID) {
179 u32 blc_pwm_ctl = I915_READ(BLC_PWM_CTL);
180 u32 pwm = pfmb & ASLE_PFMB_PWM_MASK;
181 blc_pwm_ctl &= BACKLIGHT_DUTY_CYCLE_MASK;
183 /* FIXME - what do we do with the PWM? */
188 static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
190 /* Panel fitting is currently controlled by the X code, so this is a
191 noop until modesetting support works fully */
192 if (!(pfit & ASLE_PFIT_VALID))
193 return ASLE_PFIT_FAILED;
197 void intel_opregion_asle_intr(struct drm_device *dev)
199 struct drm_i915_private *dev_priv = dev->dev_private;
200 struct opregion_asle *asle = dev_priv->opregion.asle;
207 asle_req = asle->aslc & ASLE_REQ_MSK;
210 DRM_DEBUG("non asle set request??\n");
214 if (asle_req & ASLE_SET_ALS_ILLUM)
215 asle_stat |= asle_set_als_illum(dev, asle->alsi);
217 if (asle_req & ASLE_SET_BACKLIGHT)
218 asle_stat |= asle_set_backlight(dev, asle->bclp);
220 if (asle_req & ASLE_SET_PFIT)
221 asle_stat |= asle_set_pfit(dev, asle->pfit);
223 if (asle_req & ASLE_SET_PWM_FREQ)
224 asle_stat |= asle_set_pwm_freq(dev, asle->pfmb);
226 asle->aslc = asle_stat;
229 void intel_opregion_gse_intr(struct drm_device *dev)
231 struct drm_i915_private *dev_priv = dev->dev_private;
232 struct opregion_asle *asle = dev_priv->opregion.asle;
239 asle_req = asle->aslc & ASLE_REQ_MSK;
242 DRM_DEBUG("non asle set request??\n");
246 if (asle_req & ASLE_SET_ALS_ILLUM) {
247 DRM_DEBUG("Illum is not supported\n");
248 asle_stat |= ASLE_ALS_ILLUM_FAILED;
251 if (asle_req & ASLE_SET_BACKLIGHT)
252 asle_stat |= asle_set_backlight(dev, asle->bclp);
254 if (asle_req & ASLE_SET_PFIT) {
255 DRM_DEBUG("Pfit is not supported\n");
256 asle_stat |= ASLE_PFIT_FAILED;
259 if (asle_req & ASLE_SET_PWM_FREQ) {
260 DRM_DEBUG("PWM freq is not supported\n");
261 asle_stat |= ASLE_PWM_FREQ_FAILED;
264 asle->aslc = asle_stat;
266 #define ASLE_ALS_EN (1<<0)
267 #define ASLE_BLC_EN (1<<1)
268 #define ASLE_PFIT_EN (1<<2)
269 #define ASLE_PFMB_EN (1<<3)
271 void intel_opregion_enable_asle(struct drm_device *dev)
273 struct drm_i915_private *dev_priv = dev->dev_private;
274 struct opregion_asle *asle = dev_priv->opregion.asle;
278 intel_enable_asle(dev);
280 asle->tche = ASLE_ALS_EN | ASLE_BLC_EN | ASLE_PFIT_EN |
286 #define ACPI_EV_DISPLAY_SWITCH (1<<0)
287 #define ACPI_EV_LID (1<<1)
288 #define ACPI_EV_DOCK (1<<2)
290 static struct intel_opregion *system_opregion;
292 static int intel_opregion_video_event(struct notifier_block *nb,
293 unsigned long val, void *data)
295 /* The only video events relevant to opregion are 0x80. These indicate
296 either a docking event, lid switch or display switch request. In
297 Linux, these are handled by the dock, button and video drivers.
299 struct opregion_acpi *acpi;
300 struct acpi_bus_event *event = data;
303 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
306 if (!system_opregion)
309 acpi = system_opregion->acpi;
311 if (event->type == 0x80 && !(acpi->cevt & 0x1))
319 static struct notifier_block intel_opregion_notifier = {
320 .notifier_call = intel_opregion_video_event,
324 * Initialise the DIDL field in opregion. This passes a list of devices to
325 * the firmware. Values are defined by section B.4.2 of the ACPI specification
329 static void intel_didl_outputs(struct drm_device *dev)
331 struct drm_i915_private *dev_priv = dev->dev_private;
332 struct intel_opregion *opregion = &dev_priv->opregion;
333 struct drm_connector *connector;
335 struct acpi_device *acpi_dev, *acpi_cdev, *acpi_video_bus = NULL;
336 unsigned long long device_id;
340 handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
341 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev)))
344 if (acpi_is_video_device(acpi_dev))
345 acpi_video_bus = acpi_dev;
347 list_for_each_entry(acpi_cdev, &acpi_dev->children, node) {
348 if (acpi_is_video_device(acpi_cdev)) {
349 acpi_video_bus = acpi_cdev;
355 if (!acpi_video_bus) {
356 printk(KERN_WARNING "No ACPI video bus found\n");
360 list_for_each_entry(acpi_cdev, &acpi_video_bus->children, node) {
362 dev_printk(KERN_ERR, &dev->pdev->dev,
363 "More than 8 outputs detected\n");
367 acpi_evaluate_integer(acpi_cdev->handle, "_ADR",
369 if (ACPI_SUCCESS(status)) {
372 opregion->acpi->didl[i] = (u32)(device_id & 0x0f0f);
378 /* If fewer than 8 outputs, the list must be null terminated */
380 opregion->acpi->didl[i] = 0;
385 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
386 int output_type = ACPI_OTHER_OUTPUT;
388 device_printf(dev->device,
389 "More than 8 outputs detected\n");
392 switch (connector->connector_type) {
393 case DRM_MODE_CONNECTOR_VGA:
394 case DRM_MODE_CONNECTOR_DVIA:
395 output_type = ACPI_VGA_OUTPUT;
397 case DRM_MODE_CONNECTOR_Composite:
398 case DRM_MODE_CONNECTOR_SVIDEO:
399 case DRM_MODE_CONNECTOR_Component:
400 case DRM_MODE_CONNECTOR_9PinDIN:
401 output_type = ACPI_TV_OUTPUT;
403 case DRM_MODE_CONNECTOR_DVII:
404 case DRM_MODE_CONNECTOR_DVID:
405 case DRM_MODE_CONNECTOR_DisplayPort:
406 case DRM_MODE_CONNECTOR_HDMIA:
407 case DRM_MODE_CONNECTOR_HDMIB:
408 output_type = ACPI_DIGITAL_OUTPUT;
410 case DRM_MODE_CONNECTOR_LVDS:
411 output_type = ACPI_LVDS_OUTPUT;
414 opregion->acpi->didl[i] |= (1<<31) | output_type | i;
420 void intel_opregion_init(struct drm_device *dev)
422 struct drm_i915_private *dev_priv = dev->dev_private;
423 struct intel_opregion *opregion = &dev_priv->opregion;
425 if (!opregion->header)
428 if (opregion->acpi) {
429 if (drm_core_check_feature(dev, DRIVER_MODESET))
430 intel_didl_outputs(dev);
432 /* Notify BIOS we are ready to handle ACPI video ext notifs.
433 * Right now, all the events are handled by the ACPI video module.
434 * We don't actually need to do anything with them. */
435 opregion->acpi->csts = 0;
436 opregion->acpi->drdy = 1;
438 system_opregion = opregion;
439 register_acpi_notifier(&intel_opregion_notifier);
443 intel_opregion_enable_asle(dev);
446 void intel_opregion_fini(struct drm_device *dev)
448 struct drm_i915_private *dev_priv = dev->dev_private;
449 struct intel_opregion *opregion = &dev_priv->opregion;
451 if (!opregion->header)
454 if (opregion->acpi) {
455 opregion->acpi->drdy = 0;
457 system_opregion = NULL;
458 unregister_acpi_notifier(&intel_opregion_notifier);
461 /* just clear all opregion memory pointers now */
462 iounmap(opregion->header);
463 opregion->header = NULL;
464 opregion->acpi = NULL;
465 opregion->swsci = NULL;
466 opregion->asle = NULL;
467 opregion->vbt = NULL;
471 intel_opregion_init(struct drm_device *dev)
478 intel_opregion_fini(struct drm_device *dev)
480 struct drm_i915_private *dev_priv;
481 struct intel_opregion *opregion;
483 dev_priv = dev->dev_private;
484 opregion = &dev_priv->opregion;
486 if (opregion->header == NULL)
489 pmap_unmapdev((vm_offset_t)opregion->header, OPREGION_SIZE);
490 opregion->header = NULL;
491 opregion->acpi = NULL;
492 opregion->swsci = NULL;
493 opregion->asle = NULL;
494 opregion->vbt = NULL;
498 int intel_opregion_setup(struct drm_device *dev)
500 struct drm_i915_private *dev_priv = dev->dev_private;
501 struct intel_opregion *opregion = &dev_priv->opregion;
506 asls = pci_read_config(dev->device, PCI_ASLS, 4);
507 DRM_DEBUG("graphic opregion physical addr: 0x%x\n", asls);
509 DRM_DEBUG("ACPI OpRegion not supported!\n");
513 base = (void *)pmap_mapbios(asls, OPREGION_SIZE);
517 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
518 DRM_DEBUG("opregion signature mismatch\n");
522 opregion->header = (struct opregion_header *)base;
523 opregion->vbt = base + OPREGION_VBT_OFFSET;
525 opregion->lid_state = (u32 *)(base + ACPI_CLID);
527 mboxes = opregion->header->mboxes;
528 if (mboxes & MBOX_ACPI) {
529 DRM_DEBUG("Public ACPI methods supported\n");
530 opregion->acpi = (struct opregion_acpi *)(base +
531 OPREGION_ACPI_OFFSET);
534 if (mboxes & MBOX_SWSCI) {
535 DRM_DEBUG("SWSCI supported\n");
536 opregion->swsci = (struct opregion_swsci *)(base +
537 OPREGION_SWSCI_OFFSET);
539 if (mboxes & MBOX_ASLE) {
540 DRM_DEBUG("ASLE supported\n");
541 opregion->asle = (struct opregion_asle *)(base +
542 OPREGION_ASLE_OFFSET);
548 pmap_unmapdev((vm_offset_t)base, OPREGION_SIZE);