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