gma500: opregion and ACPI
[linux-2.6/btrfs-unstable.git] / drivers / gpu / drm / gma500 / opregion.c
blob483e0b28c7aade840009bc71c3161bfbf83d6748
1 /*
2 * Copyright 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #ifdef CONFIG_ACPI
25 #include <linux/acpi.h>
26 #include <linux/acpi_io.h>
27 #endif
28 #include "psb_drv.h"
29 #include "psb_intel_reg.h"
31 #define PCI_ASLE 0xe4
32 #define PCI_ASLS 0xfc
34 #define OPREGION_HEADER_OFFSET 0
35 #define OPREGION_ACPI_OFFSET 0x100
36 #define ACPI_CLID 0x01ac /* current lid state indicator */
37 #define ACPI_CDCK 0x01b0 /* current docking state indicator */
38 #define OPREGION_SWSCI_OFFSET 0x200
39 #define OPREGION_ASLE_OFFSET 0x300
40 #define OPREGION_VBT_OFFSET 0x400
42 #define OPREGION_SIGNATURE "IntelGraphicsMem"
43 #define MBOX_ACPI (1<<0)
44 #define MBOX_SWSCI (1<<1)
45 #define MBOX_ASLE (1<<2)
47 struct opregion_header {
48 u8 signature[16];
49 u32 size;
50 u32 opregion_ver;
51 u8 bios_ver[32];
52 u8 vbios_ver[16];
53 u8 driver_ver[16];
54 u32 mboxes;
55 u8 reserved[164];
56 } __packed;
58 /* OpRegion mailbox #1: public ACPI methods */
59 struct opregion_acpi {
60 u32 drdy; /* driver readiness */
61 u32 csts; /* notification status */
62 u32 cevt; /* current event */
63 u8 rsvd1[20];
64 u32 didl[8]; /* supported display devices ID list */
65 u32 cpdl[8]; /* currently presented display list */
66 u32 cadl[8]; /* currently active display list */
67 u32 nadl[8]; /* next active devices list */
68 u32 aslp; /* ASL sleep time-out */
69 u32 tidx; /* toggle table index */
70 u32 chpd; /* current hotplug enable indicator */
71 u32 clid; /* current lid state*/
72 u32 cdck; /* current docking state */
73 u32 sxsw; /* Sx state resume */
74 u32 evts; /* ASL supported events */
75 u32 cnot; /* current OS notification */
76 u32 nrdy; /* driver status */
77 u8 rsvd2[60];
78 } __packed;
80 /* OpRegion mailbox #2: SWSCI */
81 struct opregion_swsci {
82 /*FIXME: add it later*/
83 } __packed;
85 /* OpRegion mailbox #3: ASLE */
86 struct opregion_asle {
87 u32 ardy; /* driver readiness */
88 u32 aslc; /* ASLE interrupt command */
89 u32 tche; /* technology enabled indicator */
90 u32 alsi; /* current ALS illuminance reading */
91 u32 bclp; /* backlight brightness to set */
92 u32 pfit; /* panel fitting state */
93 u32 cblv; /* current brightness level */
94 u16 bclm[20]; /* backlight level duty cycle mapping table */
95 u32 cpfm; /* current panel fitting mode */
96 u32 epfm; /* enabled panel fitting modes */
97 u8 plut[74]; /* panel LUT and identifier */
98 u32 pfmb; /* PWM freq and min brightness */
99 u8 rsvd[102];
100 } __packed;
102 /* ASLE irq request bits */
103 #define ASLE_SET_ALS_ILLUM (1 << 0)
104 #define ASLE_SET_BACKLIGHT (1 << 1)
105 #define ASLE_SET_PFIT (1 << 2)
106 #define ASLE_SET_PWM_FREQ (1 << 3)
107 #define ASLE_REQ_MSK 0xf
109 /* response bits of ASLE irq request */
110 #define ASLE_ALS_ILLUM_FAILED (1<<10)
111 #define ASLE_BACKLIGHT_FAILED (1<<12)
112 #define ASLE_PFIT_FAILED (1<<14)
113 #define ASLE_PWM_FREQ_FAILED (1<<16)
115 /* ASLE backlight brightness to set */
116 #define ASLE_BCLP_VALID (1<<31)
117 #define ASLE_BCLP_MSK (~(1<<31))
119 /* ASLE panel fitting request */
120 #define ASLE_PFIT_VALID (1<<31)
121 #define ASLE_PFIT_CENTER (1<<0)
122 #define ASLE_PFIT_STRETCH_TEXT (1<<1)
123 #define ASLE_PFIT_STRETCH_GFX (1<<2)
125 /* response bits of ASLE irq request */
126 #define ASLE_ALS_ILLUM_FAILED (1<<10)
127 #define ASLE_BACKLIGHT_FAILED (1<<12)
128 #define ASLE_PFIT_FAILED (1<<14)
129 #define ASLE_PWM_FREQ_FAILED (1<<16)
131 /* ASLE backlight brightness to set */
132 #define ASLE_BCLP_VALID (1<<31)
133 #define ASLE_BCLP_MSK (~(1<<31))
135 /* ASLE panel fitting request */
136 #define ASLE_PFIT_VALID (1<<31)
137 #define ASLE_PFIT_CENTER (1<<0)
138 #define ASLE_PFIT_STRETCH_TEXT (1<<1)
139 #define ASLE_PFIT_STRETCH_GFX (1<<2)
141 /* PWM frequency and minimum brightness */
142 #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
143 #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
144 #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
145 #define ASLE_PFMB_PWM_VALID (1<<31)
147 #define ASLE_CBLV_VALID (1<<31)
149 static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
151 struct drm_psb_private *dev_priv = dev->dev_private;
152 struct opregion_asle *asle = dev_priv->opregion.asle;
153 struct backlight_device *bd = dev_priv->backlight_device;
154 u32 max;
156 DRM_DEBUG_DRIVER("asle set backlight %x\n", bclp);
158 if (!(bclp & ASLE_BCLP_VALID))
159 return ASLE_BACKLIGHT_FAILED;
161 if (bd == NULL)
162 return ASLE_BACKLIGHT_FAILED;
164 bclp &= ASLE_BCLP_MSK;
165 if (bclp > 255)
166 return ASLE_BACKLIGHT_FAILED;
168 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
169 max = bd->props.max_brightness;
170 bd->props.brightness = bclp * max / 255;
171 backlight_update_status(bd);
172 #endif
173 asle->cblv = (bclp * 0x64) / 0xff | ASLE_CBLV_VALID;
175 return 0;
178 void psb_intel_opregion_asle_intr(struct drm_device *dev)
180 struct drm_psb_private *dev_priv = dev->dev_private;
181 struct opregion_asle *asle = dev_priv->opregion.asle;
182 u32 asle_stat = 0;
183 u32 asle_req;
185 if (!asle)
186 return;
188 asle_req = asle->aslc & ASLE_REQ_MSK;
189 if (!asle_req) {
190 DRM_DEBUG_DRIVER("non asle set request??\n");
191 return;
194 if (asle_req & ASLE_SET_BACKLIGHT)
195 asle_stat |= asle_set_backlight(dev, asle->bclp);
197 asle->aslc = asle_stat;
200 #define ASLE_ALS_EN (1<<0)
201 #define ASLE_BLC_EN (1<<1)
202 #define ASLE_PFIT_EN (1<<2)
203 #define ASLE_PFMB_EN (1<<3)
205 void psb_intel_opregion_enable_asle(struct drm_device *dev)
207 struct drm_psb_private *dev_priv = dev->dev_private;
208 struct opregion_asle *asle = dev_priv->opregion.asle;
210 if (asle) {
211 /* Don't do this on Medfield or other non PC like devices, they
212 use the bit for something different altogether */
213 psb_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
214 psb_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
216 asle->tche = ASLE_ALS_EN | ASLE_BLC_EN | ASLE_PFIT_EN
217 | ASLE_PFMB_EN;
218 asle->ardy = 1;
222 #define ACPI_EV_DISPLAY_SWITCH (1<<0)
223 #define ACPI_EV_LID (1<<1)
224 #define ACPI_EV_DOCK (1<<2)
226 static struct psb_intel_opregion *system_opregion;
228 static int psb_intel_opregion_video_event(struct notifier_block *nb,
229 unsigned long val, void *data)
231 /* The only video events relevant to opregion are 0x80. These indicate
232 either a docking event, lid switch or display switch request. In
233 Linux, these are handled by the dock, button and video drivers.
234 We might want to fix the video driver to be opregion-aware in
235 future, but right now we just indicate to the firmware that the
236 request has been handled */
238 struct opregion_acpi *acpi;
240 if (!system_opregion)
241 return NOTIFY_DONE;
243 acpi = system_opregion->acpi;
244 acpi->csts = 0;
246 return NOTIFY_OK;
249 static struct notifier_block psb_intel_opregion_notifier = {
250 .notifier_call = psb_intel_opregion_video_event,
253 void psb_intel_opregion_init(struct drm_device *dev)
255 struct drm_psb_private *dev_priv = dev->dev_private;
256 struct psb_intel_opregion *opregion = &dev_priv->opregion;
258 if (!opregion->header)
259 return;
261 if (opregion->acpi) {
262 /* Notify BIOS we are ready to handle ACPI video ext notifs.
263 * Right now, all the events are handled by the ACPI video
264 * module. We don't actually need to do anything with them. */
265 opregion->acpi->csts = 0;
266 opregion->acpi->drdy = 1;
268 system_opregion = opregion;
269 register_acpi_notifier(&psb_intel_opregion_notifier);
272 if (opregion->asle)
273 psb_intel_opregion_enable_asle(dev);
276 void psb_intel_opregion_fini(struct drm_device *dev)
278 struct drm_psb_private *dev_priv = dev->dev_private;
279 struct psb_intel_opregion *opregion = &dev_priv->opregion;
281 if (!opregion->header)
282 return;
284 if (opregion->acpi) {
285 opregion->acpi->drdy = 0;
287 system_opregion = NULL;
288 unregister_acpi_notifier(&psb_intel_opregion_notifier);
291 /* just clear all opregion memory pointers now */
292 iounmap(opregion->header);
293 opregion->header = NULL;
294 opregion->acpi = NULL;
295 opregion->swsci = NULL;
296 opregion->asle = NULL;
297 opregion->vbt = NULL;
300 int psb_intel_opregion_setup(struct drm_device *dev)
302 struct drm_psb_private *dev_priv = dev->dev_private;
303 struct psb_intel_opregion *opregion = &dev_priv->opregion;
304 u32 opregion_phy, mboxes;
305 void *base;
306 int err = 0;
308 pci_read_config_dword(dev->pdev, PCI_ASLS, &opregion_phy);
309 if (opregion_phy == 0) {
310 DRM_DEBUG_DRIVER("ACPI Opregion not supported\n");
311 return -ENOTSUPP;
313 DRM_DEBUG("OpRegion detected at 0x%8x\n", opregion_phy);
314 #ifdef CONFIG_ACPI
315 base = acpi_os_ioremap(opregion_phy, 8*1024);
316 #else
317 base = ioremap(opregion_phy, 8*1024);
318 #endif
319 if (!base)
320 return -ENOMEM;
322 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
323 DRM_DEBUG_DRIVER("opregion signature mismatch\n");
324 err = -EINVAL;
325 goto err_out;
328 opregion->header = base;
329 opregion->vbt = base + OPREGION_VBT_OFFSET;
331 opregion->lid_state = base + ACPI_CLID;
333 mboxes = opregion->header->mboxes;
334 if (mboxes & MBOX_ACPI) {
335 DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
336 opregion->acpi = base + OPREGION_ACPI_OFFSET;
339 if (mboxes & MBOX_ASLE) {
340 DRM_DEBUG_DRIVER("ASLE supported\n");
341 opregion->asle = base + OPREGION_ASLE_OFFSET;
344 return 0;
346 err_out:
347 iounmap(base);
348 return err;