xref: /freebsd/sys/dev/acpi_support/acpi_panasonic.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*-
2  * Copyright (c) 2003 OGAWA Takaya <t-ogawa@triaez.kaisei.org>
3  * Copyright (c) 2004 TAKAHASHI Yoshihiro <nyan@FreeBSD.org>
4  * All rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/power.h>
39 
40 #include <contrib/dev/acpica/include/acpi.h>
41 
42 #include <dev/acpica/acpivar.h>
43 
44 #define _COMPONENT	ACPI_OEM
45 ACPI_MODULE_NAME("Panasonic")
46 
47 /* Debug */
48 #undef	ACPI_PANASONIC_DEBUG
49 
50 /* Operations */
51 #define	HKEY_SET	0
52 #define	HKEY_GET	1
53 
54 /* Functions */
55 #define	HKEY_REG_LCD_BRIGHTNESS_MAX_AC	0x02
56 #define	HKEY_REG_LCD_BRIGHTNESS_MIN_AC	0x03
57 #define	HKEY_REG_LCD_BRIGHTNESS_AC	0x04
58 #define	HKEY_REG_LCD_BRIGHTNESS_MAX_DC	0x05
59 #define	HKEY_REG_LCD_BRIGHTNESS_MIN_DC	0x06
60 #define	HKEY_REG_LCD_BRIGHTNESS_DC	0x07
61 #define	HKEY_REG_SOUND_MUTE		0x08
62 
63 /* Field definitions */
64 #define	HKEY_LCD_BRIGHTNESS_BITS	4
65 #define	HKEY_LCD_BRIGHTNESS_DIV		((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
66 
67 struct acpi_panasonic_softc {
68 	device_t	dev;
69 	ACPI_HANDLE	handle;
70 
71 	struct sysctl_ctx_list	sysctl_ctx;
72 	struct sysctl_oid	*sysctl_tree;
73 
74 	eventhandler_tag	power_evh;
75 };
76 
77 /* Prototype for HKEY functions for getting/setting a value. */
78 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
79 
80 static int	acpi_panasonic_probe(device_t dev);
81 static int	acpi_panasonic_attach(device_t dev);
82 static int	acpi_panasonic_detach(device_t dev);
83 static int	acpi_panasonic_shutdown(device_t dev);
84 static int	acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
85 static UINT64	acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index);
86 static void	acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index,
87 		    UINT64 val);
88 static int	acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
89 		    ACPI_HANDLE h, UINT32 *arg);
90 static void	acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
91 		    ACPI_HANDLE h, UINT32 key);
92 static void	acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
93 		    void *context);
94 static void	acpi_panasonic_power_profile(void *arg);
95 
96 static hkey_fn_t	hkey_lcd_brightness_max;
97 static hkey_fn_t	hkey_lcd_brightness_min;
98 static hkey_fn_t	hkey_lcd_brightness;
99 static hkey_fn_t	hkey_sound_mute;
100 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
101 
102 /* Table of sysctl names and HKEY functions to call. */
103 static struct {
104 	char		*name;
105 	hkey_fn_t	*handler;
106 } sysctl_table[] = {
107 	/* name,		handler */
108 	{"lcd_brightness_max",	hkey_lcd_brightness_max},
109 	{"lcd_brightness_min",	hkey_lcd_brightness_min},
110 	{"lcd_brightness",	hkey_lcd_brightness},
111 	{"sound_mute",		hkey_sound_mute},
112 	{NULL, NULL}
113 };
114 
115 static device_method_t acpi_panasonic_methods[] = {
116 	DEVMETHOD(device_probe,		acpi_panasonic_probe),
117 	DEVMETHOD(device_attach,	acpi_panasonic_attach),
118 	DEVMETHOD(device_detach,	acpi_panasonic_detach),
119 	DEVMETHOD(device_shutdown,	acpi_panasonic_shutdown),
120 
121 	DEVMETHOD_END
122 };
123 
124 static driver_t acpi_panasonic_driver = {
125 	"acpi_panasonic",
126 	acpi_panasonic_methods,
127 	sizeof(struct acpi_panasonic_softc),
128 };
129 
130 static devclass_t acpi_panasonic_devclass;
131 
132 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
133     acpi_panasonic_devclass, 0, 0);
134 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
135 
136 static int
137 acpi_panasonic_probe(device_t dev)
138 {
139 	static char *mat_ids[] = { "MAT0019", NULL };
140 
141 	if (acpi_disabled("panasonic") ||
142 	    ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids) == NULL ||
143 	    device_get_unit(dev) != 0)
144 		return (ENXIO);
145 
146 	device_set_desc(dev, "Panasonic Notebook Hotkeys");
147 	return (0);
148 }
149 
150 static int
151 acpi_panasonic_attach(device_t dev)
152 {
153 	struct acpi_panasonic_softc *sc;
154 	struct acpi_softc *acpi_sc;
155 	ACPI_STATUS status;
156 	int i;
157 
158 	sc = device_get_softc(dev);
159 	sc->dev = dev;
160 	sc->handle = acpi_get_handle(dev);
161 
162 	acpi_sc = acpi_device_get_parent_softc(dev);
163 
164 	/* Build sysctl tree */
165 	sysctl_ctx_init(&sc->sysctl_ctx);
166 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
167 	    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
168 	    "panasonic", CTLFLAG_RD, 0, "");
169 	for (i = 0; sysctl_table[i].name != NULL; i++) {
170 		SYSCTL_ADD_PROC(&sc->sysctl_ctx,
171 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
172 		    sysctl_table[i].name,
173 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
174 		    sc, i, acpi_panasonic_sysctl, "I", "");
175 	}
176 
177 #if 0
178 	/* Activate hotkeys */
179 	status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
180 	if (ACPI_FAILURE(status)) {
181 		device_printf(dev, "enable FN keys failed\n");
182 		sysctl_ctx_free(&sc->sysctl_ctx);
183 		return (ENXIO);
184 	}
185 #endif
186 
187 	/* Handle notifies */
188 	status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
189 	    acpi_panasonic_notify, sc);
190 	if (ACPI_FAILURE(status)) {
191 		device_printf(dev, "couldn't install notify handler - %s\n",
192 		    AcpiFormatException(status));
193 		sysctl_ctx_free(&sc->sysctl_ctx);
194 		return (ENXIO);
195 	}
196 
197 	/* Install power profile event handler */
198 	sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
199 	    acpi_panasonic_power_profile, sc->handle, 0);
200 
201 	return (0);
202 }
203 
204 static int
205 acpi_panasonic_detach(device_t dev)
206 {
207 	struct acpi_panasonic_softc *sc;
208 
209 	sc = device_get_softc(dev);
210 
211 	/* Remove power profile event handler */
212 	EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
213 
214 	/* Remove notify handler */
215 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
216 	    acpi_panasonic_notify);
217 
218 	/* Free sysctl tree */
219 	sysctl_ctx_free(&sc->sysctl_ctx);
220 
221 	return (0);
222 }
223 
224 static int
225 acpi_panasonic_shutdown(device_t dev)
226 {
227 	struct acpi_panasonic_softc *sc;
228 	int mute;
229 
230 	/* Mute the main audio during reboot to prevent static burst to speaker. */
231 	sc = device_get_softc(dev);
232 	mute = 1;
233 	hkey_sound_mute(sc->handle, HKEY_SET, &mute);
234 	return (0);
235 }
236 
237 static int
238 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
239 {
240 	struct acpi_panasonic_softc *sc;
241 	UINT32 arg;
242 	int function, error;
243 	hkey_fn_t *handler;
244 
245 	sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
246 	function = oidp->oid_arg2;
247 	handler = sysctl_table[function].handler;
248 
249 	/* Get the current value from the appropriate function. */
250 	ACPI_SERIAL_BEGIN(panasonic);
251 	error = handler(sc->handle, HKEY_GET, &arg);
252 	if (error != 0)
253 		goto out;
254 
255 	/* Send the current value to the user and return if no new value. */
256 	error = sysctl_handle_int(oidp, &arg, 0, req);
257 	if (error != 0 || req->newptr == NULL)
258 		goto out;
259 
260 	/* Set the new value via the appropriate function. */
261 	error = handler(sc->handle, HKEY_SET, &arg);
262 
263 out:
264 	ACPI_SERIAL_END(panasonic);
265 	return (error);
266 }
267 
268 static UINT64
269 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index)
270 {
271 	ACPI_BUFFER buf;
272 	ACPI_OBJECT *res;
273 	UINT64 ret;
274 
275 	ACPI_SERIAL_ASSERT(panasonic);
276 	ret = -1;
277 	buf.Length = ACPI_ALLOCATE_BUFFER;
278 	buf.Pointer = NULL;
279 	AcpiEvaluateObject(h, "SINF", NULL, &buf);
280 	res = (ACPI_OBJECT *)buf.Pointer;
281 	if (res->Type == ACPI_TYPE_PACKAGE)
282 		ret = res->Package.Elements[index].Integer.Value;
283 	AcpiOsFree(buf.Pointer);
284 
285 	return (ret);
286 }
287 
288 static void
289 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val)
290 {
291 	ACPI_OBJECT_LIST args;
292 	ACPI_OBJECT obj[2];
293 
294 	ACPI_SERIAL_ASSERT(panasonic);
295 	obj[0].Type = ACPI_TYPE_INTEGER;
296 	obj[0].Integer.Value = index;
297 	obj[1].Type = ACPI_TYPE_INTEGER;
298 	obj[1].Integer.Value = val;
299 	args.Count = 2;
300 	args.Pointer = obj;
301 	AcpiEvaluateObject(h, "SSET", &args, NULL);
302 }
303 
304 static int
305 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
306 {
307 	int reg;
308 
309 	ACPI_SERIAL_ASSERT(panasonic);
310 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
311 	    HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
312 
313 	switch (op) {
314 	case HKEY_SET:
315 		return (EPERM);
316 		break;
317 	case HKEY_GET:
318 		*val = acpi_panasonic_sinf(h, reg);
319 		break;
320 	}
321 
322 	return (0);
323 }
324 
325 static int
326 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
327 {
328 	int reg;
329 
330 	ACPI_SERIAL_ASSERT(panasonic);
331 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
332 	    HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
333 
334 	switch (op) {
335 	case HKEY_SET:
336 		return (EPERM);
337 		break;
338 	case HKEY_GET:
339 		*val = acpi_panasonic_sinf(h, reg);
340 		break;
341 	}
342 
343 	return (0);
344 }
345 
346 static int
347 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
348 {
349 	int reg;
350 	UINT32 max, min;
351 
352 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
353 	    HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
354 
355 	ACPI_SERIAL_ASSERT(panasonic);
356 	switch (op) {
357 	case HKEY_SET:
358 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
359 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
360 		if (*val < min || *val > max)
361 			return (EINVAL);
362 		acpi_panasonic_sset(h, reg, *val);
363 		break;
364 	case HKEY_GET:
365 		*val = acpi_panasonic_sinf(h, reg);
366 		break;
367 	}
368 
369 	return (0);
370 }
371 
372 static int
373 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
374 {
375 
376 	ACPI_SERIAL_ASSERT(panasonic);
377 	switch (op) {
378 	case HKEY_SET:
379 		if (*val != 0 && *val != 1)
380 			return (EINVAL);
381 		acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
382 		break;
383 	case HKEY_GET:
384 		*val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
385 		break;
386 	}
387 
388 	return (0);
389 }
390 
391 static int
392 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
393     UINT32 *arg)
394 {
395 	ACPI_BUFFER buf;
396 	ACPI_OBJECT *res;
397 	UINT64 val;
398 	int status;
399 
400 	ACPI_SERIAL_ASSERT(panasonic);
401 	status = ENXIO;
402 
403 	buf.Length = ACPI_ALLOCATE_BUFFER;
404 	buf.Pointer = NULL;
405 	AcpiEvaluateObject(h, "HINF", NULL, &buf);
406 	res = (ACPI_OBJECT *)buf.Pointer;
407 	if (res->Type != ACPI_TYPE_INTEGER) {
408 		device_printf(sc->dev, "HINF returned non-integer\n");
409 		goto end;
410 	}
411 	val = res->Integer.Value;
412 #ifdef ACPI_PANASONIC_DEBUG
413 	device_printf(sc->dev, "%s button Fn+F%d\n",
414 		      (val & 0x80) ? "Pressed" : "Released",
415 		      (int)(val & 0x7f));
416 #endif
417 	if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
418 		*arg = val;
419 		status = 0;
420 	}
421 end:
422 	if (buf.Pointer)
423 		AcpiOsFree(buf.Pointer);
424 
425 	return (status);
426 }
427 
428 static void
429 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
430     UINT32 key)
431 {
432 	struct acpi_softc *acpi_sc;
433 	int arg, max, min;
434 
435 	acpi_sc = acpi_device_get_parent_softc(sc->dev);
436 
437 	ACPI_SERIAL_ASSERT(panasonic);
438 	switch (key) {
439 	case 1:
440 		/* Decrease LCD brightness. */
441 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
442 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
443 		hkey_lcd_brightness(h, HKEY_GET, &arg);
444 		arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
445 		if (arg < min)
446 			arg = min;
447 		else if (arg > max)
448 			arg = max;
449 		hkey_lcd_brightness(h, HKEY_SET, &arg);
450 		break;
451 	case 2:
452 		/* Increase LCD brightness. */
453 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
454 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
455 		hkey_lcd_brightness(h, HKEY_GET, &arg);
456 		arg += max / HKEY_LCD_BRIGHTNESS_DIV;
457 		if (arg < min)
458 			arg = min;
459 		else if (arg > max)
460 			arg = max;
461 		hkey_lcd_brightness(h, HKEY_SET, &arg);
462 		break;
463 	case 4:
464 		/* Toggle sound mute. */
465 		hkey_sound_mute(h, HKEY_GET, &arg);
466 		if (arg)
467 			arg = 0;
468 		else
469 			arg = 1;
470 		hkey_sound_mute(h, HKEY_SET, &arg);
471 		break;
472 	case 7:
473 		/* Suspend. */
474 		acpi_event_sleep_button_sleep(acpi_sc);
475 		break;
476 	}
477 }
478 
479 static void
480 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
481 {
482 	struct acpi_panasonic_softc *sc;
483 	UINT32 key = 0;
484 
485 	sc = (struct acpi_panasonic_softc *)context;
486 
487 	switch (notify) {
488 	case 0x80:
489 		ACPI_SERIAL_BEGIN(panasonic);
490 		if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
491 			acpi_panasonic_hkey_action(sc, h, key);
492 			acpi_UserNotify("Panasonic", h, (uint8_t)key);
493 		}
494 		ACPI_SERIAL_END(panasonic);
495 		break;
496 	default:
497 		device_printf(sc->dev, "unknown notify: %#x\n", notify);
498 		break;
499 	}
500 }
501 
502 static void
503 acpi_panasonic_power_profile(void *arg)
504 {
505 	ACPI_HANDLE handle;
506 	UINT32 brightness;
507 
508 	handle = (ACPI_HANDLE)arg;
509 
510 	/* Reset current brightness according to new power state. */
511 	ACPI_SERIAL_BEGIN(panasonic);
512 	hkey_lcd_brightness(handle, HKEY_GET, &brightness);
513 	hkey_lcd_brightness(handle, HKEY_SET, &brightness);
514 	ACPI_SERIAL_END(panasonic);
515 }
516