xref: /openbmc/linux/drivers/gpu/drm/gma500/opregion.c (revision a8fe58ce)
1 /*
2  * Copyright 2011 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  *
23  */
24 #include <linux/acpi.h>
25 #include "psb_drv.h"
26 #include "psb_intel_reg.h"
27 
28 #define PCI_ASLE 0xe4
29 #define PCI_ASLS 0xfc
30 
31 #define OPREGION_HEADER_OFFSET 0
32 #define OPREGION_ACPI_OFFSET   0x100
33 #define   ACPI_CLID 0x01ac /* current lid state indicator */
34 #define   ACPI_CDCK 0x01b0 /* current docking state indicator */
35 #define OPREGION_SWSCI_OFFSET  0x200
36 #define OPREGION_ASLE_OFFSET   0x300
37 #define OPREGION_VBT_OFFSET    0x400
38 
39 #define OPREGION_SIGNATURE "IntelGraphicsMem"
40 #define MBOX_ACPI      (1<<0)
41 #define MBOX_SWSCI     (1<<1)
42 #define MBOX_ASLE      (1<<2)
43 
44 struct opregion_header {
45 	u8 signature[16];
46 	u32 size;
47 	u32 opregion_ver;
48 	u8 bios_ver[32];
49 	u8 vbios_ver[16];
50 	u8 driver_ver[16];
51 	u32 mboxes;
52 	u8 reserved[164];
53 } __packed;
54 
55 /* OpRegion mailbox #1: public ACPI methods */
56 struct opregion_acpi {
57 	u32 drdy;	/* driver readiness */
58 	u32 csts;	/* notification status */
59 	u32 cevt;	/* current event */
60 	u8 rsvd1[20];
61 	u32 didl[8];	/* supported display devices ID list */
62 	u32 cpdl[8];	/* currently presented display list */
63 	u32 cadl[8];	/* currently active display list */
64 	u32 nadl[8];	/* next active devices list */
65 	u32 aslp;	/* ASL sleep time-out */
66 	u32 tidx;	/* toggle table index */
67 	u32 chpd;	/* current hotplug enable indicator */
68 	u32 clid;	/* current lid state*/
69 	u32 cdck;	/* current docking state */
70 	u32 sxsw;	/* Sx state resume */
71 	u32 evts;	/* ASL supported events */
72 	u32 cnot;	/* current OS notification */
73 	u32 nrdy;	/* driver status */
74 	u8 rsvd2[60];
75 } __packed;
76 
77 /* OpRegion mailbox #2: SWSCI */
78 struct opregion_swsci {
79 	/*FIXME: add it later*/
80 } __packed;
81 
82 /* OpRegion mailbox #3: ASLE */
83 struct opregion_asle {
84 	u32 ardy;	/* driver readiness */
85 	u32 aslc;	/* ASLE interrupt command */
86 	u32 tche;	/* technology enabled indicator */
87 	u32 alsi;	/* current ALS illuminance reading */
88 	u32 bclp;	/* backlight brightness to set */
89 	u32 pfit;	/* panel fitting state */
90 	u32 cblv;	/* current brightness level */
91 	u16 bclm[20];	/* backlight level duty cycle mapping table */
92 	u32 cpfm;	/* current panel fitting mode */
93 	u32 epfm;	/* enabled panel fitting modes */
94 	u8 plut[74];	/* panel LUT and identifier */
95 	u32 pfmb;	/* PWM freq and min brightness */
96 	u8 rsvd[102];
97 } __packed;
98 
99 /* ASLE irq request bits */
100 #define ASLE_SET_ALS_ILLUM     (1 << 0)
101 #define ASLE_SET_BACKLIGHT     (1 << 1)
102 #define ASLE_SET_PFIT          (1 << 2)
103 #define ASLE_SET_PWM_FREQ      (1 << 3)
104 #define ASLE_REQ_MSK           0xf
105 
106 /* response bits of ASLE irq request */
107 #define ASLE_ALS_ILLUM_FAILED   (1<<10)
108 #define ASLE_BACKLIGHT_FAILED   (1<<12)
109 #define ASLE_PFIT_FAILED        (1<<14)
110 #define ASLE_PWM_FREQ_FAILED    (1<<16)
111 
112 /* ASLE backlight brightness to set */
113 #define ASLE_BCLP_VALID                (1<<31)
114 #define ASLE_BCLP_MSK          (~(1<<31))
115 
116 /* ASLE panel fitting request */
117 #define ASLE_PFIT_VALID         (1<<31)
118 #define ASLE_PFIT_CENTER (1<<0)
119 #define ASLE_PFIT_STRETCH_TEXT (1<<1)
120 #define ASLE_PFIT_STRETCH_GFX (1<<2)
121 
122 /* response bits of ASLE irq request */
123 #define ASLE_ALS_ILLUM_FAILED	(1<<10)
124 #define ASLE_BACKLIGHT_FAILED	(1<<12)
125 #define ASLE_PFIT_FAILED	(1<<14)
126 #define ASLE_PWM_FREQ_FAILED	(1<<16)
127 
128 /* ASLE backlight brightness to set */
129 #define ASLE_BCLP_VALID                (1<<31)
130 #define ASLE_BCLP_MSK          (~(1<<31))
131 
132 /* ASLE panel fitting request */
133 #define ASLE_PFIT_VALID         (1<<31)
134 #define ASLE_PFIT_CENTER (1<<0)
135 #define ASLE_PFIT_STRETCH_TEXT (1<<1)
136 #define ASLE_PFIT_STRETCH_GFX (1<<2)
137 
138 /* PWM frequency and minimum brightness */
139 #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
140 #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
141 #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
142 #define ASLE_PFMB_PWM_VALID (1<<31)
143 
144 #define ASLE_CBLV_VALID         (1<<31)
145 
146 static struct psb_intel_opregion *system_opregion;
147 
148 static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
149 {
150 	struct drm_psb_private *dev_priv = dev->dev_private;
151 	struct opregion_asle *asle = dev_priv->opregion.asle;
152 	struct backlight_device *bd = dev_priv->backlight_device;
153 
154 	DRM_DEBUG_DRIVER("asle set backlight %x\n", bclp);
155 
156 	if (!(bclp & ASLE_BCLP_VALID))
157 		return ASLE_BACKLIGHT_FAILED;
158 
159 	if (bd == NULL)
160 		return ASLE_BACKLIGHT_FAILED;
161 
162 	bclp &= ASLE_BCLP_MSK;
163 	if (bclp > 255)
164 		return ASLE_BACKLIGHT_FAILED;
165 
166 	if (config_enabled(CONFIG_BACKLIGHT_CLASS_DEVICE)) {
167 		int max = bd->props.max_brightness;
168 		gma_backlight_set(dev, bclp * max / 255);
169 	}
170 
171 	asle->cblv = (bclp * 0x64) / 0xff | ASLE_CBLV_VALID;
172 
173 	return 0;
174 }
175 
176 static void psb_intel_opregion_asle_work(struct work_struct *work)
177 {
178 	struct psb_intel_opregion *opregion =
179 		container_of(work, struct psb_intel_opregion, asle_work);
180 	struct drm_psb_private *dev_priv =
181 		container_of(opregion, struct drm_psb_private, opregion);
182 	struct opregion_asle *asle = opregion->asle;
183 	u32 asle_stat = 0;
184 	u32 asle_req;
185 
186 	if (!asle)
187 		return;
188 
189 	asle_req = asle->aslc & ASLE_REQ_MSK;
190 	if (!asle_req) {
191 		DRM_DEBUG_DRIVER("non asle set request??\n");
192 		return;
193 	}
194 
195 	if (asle_req & ASLE_SET_BACKLIGHT)
196 		asle_stat |= asle_set_backlight(dev_priv->dev, asle->bclp);
197 
198 	asle->aslc = asle_stat;
199 
200 }
201 
202 void psb_intel_opregion_asle_intr(struct drm_device *dev)
203 {
204 	struct drm_psb_private *dev_priv = dev->dev_private;
205 
206 	if (dev_priv->opregion.asle)
207 		schedule_work(&dev_priv->opregion.asle_work);
208 }
209 
210 #define ASLE_ALS_EN    (1<<0)
211 #define ASLE_BLC_EN    (1<<1)
212 #define ASLE_PFIT_EN   (1<<2)
213 #define ASLE_PFMB_EN   (1<<3)
214 
215 void psb_intel_opregion_enable_asle(struct drm_device *dev)
216 {
217 	struct drm_psb_private *dev_priv = dev->dev_private;
218 	struct opregion_asle *asle = dev_priv->opregion.asle;
219 
220 	if (asle && system_opregion ) {
221 		/* Don't do this on Medfield or other non PC like devices, they
222 		   use the bit for something different altogether */
223 		psb_enable_pipestat(dev_priv, 0, PIPE_LEGACY_BLC_EVENT_ENABLE);
224 		psb_enable_pipestat(dev_priv, 1, PIPE_LEGACY_BLC_EVENT_ENABLE);
225 
226 		asle->tche = ASLE_ALS_EN | ASLE_BLC_EN | ASLE_PFIT_EN
227 								| ASLE_PFMB_EN;
228 		asle->ardy = 1;
229 	}
230 }
231 
232 #define ACPI_EV_DISPLAY_SWITCH (1<<0)
233 #define ACPI_EV_LID            (1<<1)
234 #define ACPI_EV_DOCK           (1<<2)
235 
236 
237 static int psb_intel_opregion_video_event(struct notifier_block *nb,
238 					  unsigned long val, void *data)
239 {
240 	/* The only video events relevant to opregion are 0x80. These indicate
241 	   either a docking event, lid switch or display switch request. In
242 	   Linux, these are handled by the dock, button and video drivers.
243 	   We might want to fix the video driver to be opregion-aware in
244 	   future, but right now we just indicate to the firmware that the
245 	   request has been handled */
246 
247 	struct opregion_acpi *acpi;
248 
249 	if (!system_opregion)
250 		return NOTIFY_DONE;
251 
252 	acpi = system_opregion->acpi;
253 	acpi->csts = 0;
254 
255 	return NOTIFY_OK;
256 }
257 
258 static struct notifier_block psb_intel_opregion_notifier = {
259 	.notifier_call = psb_intel_opregion_video_event,
260 };
261 
262 void psb_intel_opregion_init(struct drm_device *dev)
263 {
264 	struct drm_psb_private *dev_priv = dev->dev_private;
265 	struct psb_intel_opregion *opregion = &dev_priv->opregion;
266 
267 	if (!opregion->header)
268 		return;
269 
270 	if (opregion->acpi) {
271 		/* Notify BIOS we are ready to handle ACPI video ext notifs.
272 		 * Right now, all the events are handled by the ACPI video
273 		 * module. We don't actually need to do anything with them. */
274 		opregion->acpi->csts = 0;
275 		opregion->acpi->drdy = 1;
276 
277 		system_opregion = opregion;
278 		register_acpi_notifier(&psb_intel_opregion_notifier);
279 	}
280 }
281 
282 void psb_intel_opregion_fini(struct drm_device *dev)
283 {
284 	struct drm_psb_private *dev_priv = dev->dev_private;
285 	struct psb_intel_opregion *opregion = &dev_priv->opregion;
286 
287 	if (!opregion->header)
288 		return;
289 
290 	if (opregion->acpi) {
291 		opregion->acpi->drdy = 0;
292 
293 		system_opregion = NULL;
294 		unregister_acpi_notifier(&psb_intel_opregion_notifier);
295 	}
296 
297 	cancel_work_sync(&opregion->asle_work);
298 
299 	/* just clear all opregion memory pointers now */
300 	iounmap(opregion->header);
301 	opregion->header = NULL;
302 	opregion->acpi = NULL;
303 	opregion->swsci = NULL;
304 	opregion->asle = NULL;
305 	opregion->vbt = NULL;
306 }
307 
308 int psb_intel_opregion_setup(struct drm_device *dev)
309 {
310 	struct drm_psb_private *dev_priv = dev->dev_private;
311 	struct psb_intel_opregion *opregion = &dev_priv->opregion;
312 	u32 opregion_phy, mboxes;
313 	void __iomem *base;
314 	int err = 0;
315 
316 	pci_read_config_dword(dev->pdev, PCI_ASLS, &opregion_phy);
317 	if (opregion_phy == 0) {
318 		DRM_DEBUG_DRIVER("ACPI Opregion not supported\n");
319 		return -ENOTSUPP;
320 	}
321 
322 	INIT_WORK(&opregion->asle_work, psb_intel_opregion_asle_work);
323 
324 	DRM_DEBUG("OpRegion detected at 0x%8x\n", opregion_phy);
325 	base = acpi_os_ioremap(opregion_phy, 8*1024);
326 	if (!base)
327 		return -ENOMEM;
328 
329 	if (memcmp(base, OPREGION_SIGNATURE, 16)) {
330 		DRM_DEBUG_DRIVER("opregion signature mismatch\n");
331 		err = -EINVAL;
332 		goto err_out;
333 	}
334 
335 	opregion->header = base;
336 	opregion->vbt = base + OPREGION_VBT_OFFSET;
337 
338 	opregion->lid_state = base + ACPI_CLID;
339 
340 	mboxes = opregion->header->mboxes;
341 	if (mboxes & MBOX_ACPI) {
342 		DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
343 		opregion->acpi = base + OPREGION_ACPI_OFFSET;
344 	}
345 
346 	if (mboxes & MBOX_ASLE) {
347 		DRM_DEBUG_DRIVER("ASLE supported\n");
348 		opregion->asle = base + OPREGION_ASLE_OFFSET;
349 	}
350 
351 	return 0;
352 
353 err_out:
354 	iounmap(base);
355 	return err;
356 }
357 
358