]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/platform/x86/fujitsu-laptop.c
platform/x86: fujitsu-laptop: switch to a managed hotkey input device
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / fujitsu-laptop.c
1 /*-*-linux-c-*-*/
2
3 /*
4 Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@just42.net>
5 Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
6 Copyright (C) 2008 Tony Vroon <tony@linx.net>
7 Based on earlier work:
8 Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
9 Adrian Yee <brewt-fujitsu@brewt.org>
10
11 Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
12 by its respective authors.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 02110-1301, USA.
28 */
29
30 /*
31 * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
32 * features made available on a range of Fujitsu laptops including the
33 * P2xxx/P5xxx/S6xxx/S7xxx series.
34 *
35 * This driver implements a vendor-specific backlight control interface for
36 * Fujitsu laptops and provides support for hotkeys present on certain Fujitsu
37 * laptops.
38 *
39 * This driver has been tested on a Fujitsu Lifebook S6410, S7020 and
40 * P8010. It should work on most P-series and S-series Lifebooks, but
41 * YMMV.
42 *
43 * The module parameter use_alt_lcd_levels switches between different ACPI
44 * brightness controls which are used by different Fujitsu laptops. In most
45 * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
46 * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
47 *
48 */
49
50 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/init.h>
55 #include <linux/acpi.h>
56 #include <linux/dmi.h>
57 #include <linux/backlight.h>
58 #include <linux/fb.h>
59 #include <linux/input.h>
60 #include <linux/input/sparse-keymap.h>
61 #include <linux/kfifo.h>
62 #include <linux/platform_device.h>
63 #include <linux/slab.h>
64 #if IS_ENABLED(CONFIG_LEDS_CLASS)
65 #include <linux/leds.h>
66 #endif
67 #include <acpi/video.h>
68
69 #define FUJITSU_DRIVER_VERSION "0.6.0"
70
71 #define FUJITSU_LCD_N_LEVELS 8
72
73 #define ACPI_FUJITSU_CLASS "fujitsu"
74 #define ACPI_FUJITSU_BL_HID "FUJ02B1"
75 #define ACPI_FUJITSU_BL_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI brightness driver"
76 #define ACPI_FUJITSU_BL_DEVICE_NAME "Fujitsu FUJ02B1"
77 #define ACPI_FUJITSU_LAPTOP_HID "FUJ02E3"
78 #define ACPI_FUJITSU_LAPTOP_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
79 #define ACPI_FUJITSU_LAPTOP_DEVICE_NAME "Fujitsu FUJ02E3"
80
81 #define ACPI_FUJITSU_NOTIFY_CODE1 0x80
82
83 /* FUNC interface - command values */
84 #define FUNC_FLAGS 0x1000
85 #define FUNC_LEDS 0x1001
86 #define FUNC_BUTTONS 0x1002
87 #define FUNC_BACKLIGHT 0x1004
88
89 /* FUNC interface - responses */
90 #define UNSUPPORTED_CMD 0x80000000
91
92 /* FUNC interface - status flags */
93 #define FLAG_RFKILL 0x020
94 #define FLAG_LID 0x100
95 #define FLAG_DOCK 0x200
96
97 #if IS_ENABLED(CONFIG_LEDS_CLASS)
98 /* FUNC interface - LED control */
99 #define FUNC_LED_OFF 0x1
100 #define FUNC_LED_ON 0x30001
101 #define KEYBOARD_LAMPS 0x100
102 #define LOGOLAMP_POWERON 0x2000
103 #define LOGOLAMP_ALWAYS 0x4000
104 #define RADIO_LED_ON 0x20
105 #define ECO_LED 0x10000
106 #define ECO_LED_ON 0x80000
107 #endif
108
109 /* Hotkey details */
110 #define KEY1_CODE 0x410 /* codes for the keys in the GIRB register */
111 #define KEY2_CODE 0x411
112 #define KEY3_CODE 0x412
113 #define KEY4_CODE 0x413
114 #define KEY5_CODE 0x420
115
116 #define MAX_HOTKEY_RINGBUFFER_SIZE 100
117 #define RINGBUFFERSIZE 40
118
119 /* Debugging */
120 #define FUJLAPTOP_DBG_ERROR 0x0001
121 #define FUJLAPTOP_DBG_WARN 0x0002
122 #define FUJLAPTOP_DBG_INFO 0x0004
123 #define FUJLAPTOP_DBG_TRACE 0x0008
124
125 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
126 #define vdbg_printk(a_dbg_level, format, arg...) \
127 do { if (dbg_level & a_dbg_level) \
128 printk(KERN_DEBUG pr_fmt("%s: " format), __func__, ## arg); \
129 } while (0)
130 #else
131 #define vdbg_printk(a_dbg_level, format, arg...) \
132 do { } while (0)
133 #endif
134
135 /* Device controlling the backlight and associated keys */
136 struct fujitsu_bl {
137 acpi_handle acpi_handle;
138 struct acpi_device *dev;
139 struct input_dev *input;
140 char phys[32];
141 struct backlight_device *bl_device;
142 int keycode1, keycode2, keycode3, keycode4, keycode5;
143
144 unsigned int max_brightness;
145 unsigned int brightness_changed;
146 unsigned int brightness_level;
147 };
148
149 static struct fujitsu_bl *fujitsu_bl;
150 static int use_alt_lcd_levels = -1;
151 static int disable_brightness_adjust = -1;
152
153 /* Device used to access hotkeys and other features on the laptop */
154 struct fujitsu_laptop {
155 acpi_handle acpi_handle;
156 struct acpi_device *dev;
157 struct input_dev *input;
158 char phys[32];
159 struct platform_device *pf_device;
160 struct kfifo fifo;
161 spinlock_t fifo_lock;
162 int flags_supported;
163 int flags_state;
164 int logolamp_registered;
165 int kblamps_registered;
166 int radio_led_registered;
167 int eco_led_registered;
168 };
169
170 static struct fujitsu_laptop *fujitsu_laptop;
171
172 #if IS_ENABLED(CONFIG_LEDS_CLASS)
173 static enum led_brightness logolamp_get(struct led_classdev *cdev);
174 static int logolamp_set(struct led_classdev *cdev,
175 enum led_brightness brightness);
176
177 static struct led_classdev logolamp_led = {
178 .name = "fujitsu::logolamp",
179 .brightness_get = logolamp_get,
180 .brightness_set_blocking = logolamp_set
181 };
182
183 static enum led_brightness kblamps_get(struct led_classdev *cdev);
184 static int kblamps_set(struct led_classdev *cdev,
185 enum led_brightness brightness);
186
187 static struct led_classdev kblamps_led = {
188 .name = "fujitsu::kblamps",
189 .brightness_get = kblamps_get,
190 .brightness_set_blocking = kblamps_set
191 };
192
193 static enum led_brightness radio_led_get(struct led_classdev *cdev);
194 static int radio_led_set(struct led_classdev *cdev,
195 enum led_brightness brightness);
196
197 static struct led_classdev radio_led = {
198 .name = "fujitsu::radio_led",
199 .default_trigger = "rfkill-any",
200 .brightness_get = radio_led_get,
201 .brightness_set_blocking = radio_led_set
202 };
203
204 static enum led_brightness eco_led_get(struct led_classdev *cdev);
205 static int eco_led_set(struct led_classdev *cdev,
206 enum led_brightness brightness);
207
208 static struct led_classdev eco_led = {
209 .name = "fujitsu::eco_led",
210 .brightness_get = eco_led_get,
211 .brightness_set_blocking = eco_led_set
212 };
213 #endif
214
215 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
216 static u32 dbg_level = 0x03;
217 #endif
218
219 /* Fujitsu ACPI interface function */
220
221 static int call_fext_func(int cmd, int arg0, int arg1, int arg2)
222 {
223 acpi_status status = AE_OK;
224 union acpi_object params[4] = {
225 { .type = ACPI_TYPE_INTEGER },
226 { .type = ACPI_TYPE_INTEGER },
227 { .type = ACPI_TYPE_INTEGER },
228 { .type = ACPI_TYPE_INTEGER }
229 };
230 struct acpi_object_list arg_list = { 4, &params[0] };
231 unsigned long long value;
232 acpi_handle handle = NULL;
233
234 status = acpi_get_handle(fujitsu_laptop->acpi_handle, "FUNC", &handle);
235 if (ACPI_FAILURE(status)) {
236 vdbg_printk(FUJLAPTOP_DBG_ERROR,
237 "FUNC interface is not present\n");
238 return -ENODEV;
239 }
240
241 params[0].integer.value = cmd;
242 params[1].integer.value = arg0;
243 params[2].integer.value = arg1;
244 params[3].integer.value = arg2;
245
246 status = acpi_evaluate_integer(handle, NULL, &arg_list, &value);
247 if (ACPI_FAILURE(status)) {
248 vdbg_printk(FUJLAPTOP_DBG_WARN,
249 "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) call failed\n",
250 cmd, arg0, arg1, arg2);
251 return -ENODEV;
252 }
253
254 vdbg_printk(FUJLAPTOP_DBG_TRACE,
255 "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
256 cmd, arg0, arg1, arg2, (int)value);
257 return value;
258 }
259
260 #if IS_ENABLED(CONFIG_LEDS_CLASS)
261 /* LED class callbacks */
262
263 static int logolamp_set(struct led_classdev *cdev,
264 enum led_brightness brightness)
265 {
266 int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
267 int ret;
268
269 if (brightness < LED_HALF)
270 poweron = FUNC_LED_OFF;
271
272 if (brightness < LED_FULL)
273 always = FUNC_LED_OFF;
274
275 ret = call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
276 if (ret < 0)
277 return ret;
278
279 return call_fext_func(FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
280 }
281
282 static int kblamps_set(struct led_classdev *cdev,
283 enum led_brightness brightness)
284 {
285 if (brightness >= LED_FULL)
286 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS, FUNC_LED_ON);
287 else
288 return call_fext_func(FUNC_LEDS, 0x1, KEYBOARD_LAMPS, FUNC_LED_OFF);
289 }
290
291 static int radio_led_set(struct led_classdev *cdev,
292 enum led_brightness brightness)
293 {
294 if (brightness >= LED_FULL)
295 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, RADIO_LED_ON);
296 else
297 return call_fext_func(FUNC_FLAGS, 0x5, RADIO_LED_ON, 0x0);
298 }
299
300 static int eco_led_set(struct led_classdev *cdev,
301 enum led_brightness brightness)
302 {
303 int curr;
304
305 curr = call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0);
306 if (brightness >= LED_FULL)
307 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED, curr | ECO_LED_ON);
308 else
309 return call_fext_func(FUNC_LEDS, 0x1, ECO_LED, curr & ~ECO_LED_ON);
310 }
311
312 static enum led_brightness logolamp_get(struct led_classdev *cdev)
313 {
314 int ret;
315
316 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
317 if (ret == FUNC_LED_ON)
318 return LED_FULL;
319
320 ret = call_fext_func(FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
321 if (ret == FUNC_LED_ON)
322 return LED_HALF;
323
324 return LED_OFF;
325 }
326
327 static enum led_brightness kblamps_get(struct led_classdev *cdev)
328 {
329 enum led_brightness brightness = LED_OFF;
330
331 if (call_fext_func(FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
332 brightness = LED_FULL;
333
334 return brightness;
335 }
336
337 static enum led_brightness radio_led_get(struct led_classdev *cdev)
338 {
339 enum led_brightness brightness = LED_OFF;
340
341 if (call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
342 brightness = LED_FULL;
343
344 return brightness;
345 }
346
347 static enum led_brightness eco_led_get(struct led_classdev *cdev)
348 {
349 enum led_brightness brightness = LED_OFF;
350
351 if (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
352 brightness = LED_FULL;
353
354 return brightness;
355 }
356 #endif
357
358 /* Hardware access for LCD brightness control */
359
360 static int set_lcd_level(int level)
361 {
362 acpi_status status = AE_OK;
363 acpi_handle handle = NULL;
364
365 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBLL [%d]\n",
366 level);
367
368 if (level < 0 || level >= fujitsu_bl->max_brightness)
369 return -EINVAL;
370
371 status = acpi_get_handle(fujitsu_bl->acpi_handle, "SBLL", &handle);
372 if (ACPI_FAILURE(status)) {
373 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBLL not present\n");
374 return -ENODEV;
375 }
376
377
378 status = acpi_execute_simple_method(handle, NULL, level);
379 if (ACPI_FAILURE(status))
380 return -ENODEV;
381
382 return 0;
383 }
384
385 static int set_lcd_level_alt(int level)
386 {
387 acpi_status status = AE_OK;
388 acpi_handle handle = NULL;
389
390 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBL2 [%d]\n",
391 level);
392
393 if (level < 0 || level >= fujitsu_bl->max_brightness)
394 return -EINVAL;
395
396 status = acpi_get_handle(fujitsu_bl->acpi_handle, "SBL2", &handle);
397 if (ACPI_FAILURE(status)) {
398 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBL2 not present\n");
399 return -ENODEV;
400 }
401
402 status = acpi_execute_simple_method(handle, NULL, level);
403 if (ACPI_FAILURE(status))
404 return -ENODEV;
405
406 return 0;
407 }
408
409 static int get_lcd_level(void)
410 {
411 unsigned long long state = 0;
412 acpi_status status = AE_OK;
413
414 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
415
416 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "GBLL", NULL,
417 &state);
418 if (ACPI_FAILURE(status))
419 return 0;
420
421 fujitsu_bl->brightness_level = state & 0x0fffffff;
422
423 if (state & 0x80000000)
424 fujitsu_bl->brightness_changed = 1;
425 else
426 fujitsu_bl->brightness_changed = 0;
427
428 return fujitsu_bl->brightness_level;
429 }
430
431 static int get_max_brightness(void)
432 {
433 unsigned long long state = 0;
434 acpi_status status = AE_OK;
435
436 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
437
438 status = acpi_evaluate_integer(fujitsu_bl->acpi_handle, "RBLL", NULL,
439 &state);
440 if (ACPI_FAILURE(status))
441 return -1;
442
443 fujitsu_bl->max_brightness = state;
444
445 return fujitsu_bl->max_brightness;
446 }
447
448 /* Backlight device stuff */
449
450 static int bl_get_brightness(struct backlight_device *b)
451 {
452 return get_lcd_level();
453 }
454
455 static int bl_update_status(struct backlight_device *b)
456 {
457 int ret;
458 if (b->props.power == FB_BLANK_POWERDOWN)
459 ret = call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x3);
460 else
461 ret = call_fext_func(FUNC_BACKLIGHT, 0x1, 0x4, 0x0);
462 if (ret != 0)
463 vdbg_printk(FUJLAPTOP_DBG_ERROR,
464 "Unable to adjust backlight power, error code %i\n",
465 ret);
466
467 if (use_alt_lcd_levels)
468 ret = set_lcd_level_alt(b->props.brightness);
469 else
470 ret = set_lcd_level(b->props.brightness);
471 if (ret != 0)
472 vdbg_printk(FUJLAPTOP_DBG_ERROR,
473 "Unable to adjust LCD brightness, error code %i\n",
474 ret);
475 return ret;
476 }
477
478 static const struct backlight_ops fujitsu_bl_ops = {
479 .get_brightness = bl_get_brightness,
480 .update_status = bl_update_status,
481 };
482
483 static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
484 char *buf)
485 {
486 if (!(fujitsu_laptop->flags_supported & FLAG_LID))
487 return sprintf(buf, "unknown\n");
488 if (fujitsu_laptop->flags_state & FLAG_LID)
489 return sprintf(buf, "open\n");
490 else
491 return sprintf(buf, "closed\n");
492 }
493
494 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
495 char *buf)
496 {
497 if (!(fujitsu_laptop->flags_supported & FLAG_DOCK))
498 return sprintf(buf, "unknown\n");
499 if (fujitsu_laptop->flags_state & FLAG_DOCK)
500 return sprintf(buf, "docked\n");
501 else
502 return sprintf(buf, "undocked\n");
503 }
504
505 static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
506 char *buf)
507 {
508 if (!(fujitsu_laptop->flags_supported & FLAG_RFKILL))
509 return sprintf(buf, "unknown\n");
510 if (fujitsu_laptop->flags_state & FLAG_RFKILL)
511 return sprintf(buf, "on\n");
512 else
513 return sprintf(buf, "killed\n");
514 }
515
516 static DEVICE_ATTR_RO(lid);
517 static DEVICE_ATTR_RO(dock);
518 static DEVICE_ATTR_RO(radios);
519
520 static struct attribute *fujitsu_pf_attributes[] = {
521 &dev_attr_lid.attr,
522 &dev_attr_dock.attr,
523 &dev_attr_radios.attr,
524 NULL
525 };
526
527 static struct attribute_group fujitsu_pf_attribute_group = {
528 .attrs = fujitsu_pf_attributes
529 };
530
531 static struct platform_driver fujitsu_pf_driver = {
532 .driver = {
533 .name = "fujitsu-laptop",
534 }
535 };
536
537 static void __init dmi_check_cb_common(const struct dmi_system_id *id)
538 {
539 pr_info("Identified laptop model '%s'\n", id->ident);
540 }
541
542 static int __init dmi_check_cb_s6410(const struct dmi_system_id *id)
543 {
544 dmi_check_cb_common(id);
545 fujitsu_bl->keycode1 = KEY_SCREENLOCK; /* "Lock" */
546 fujitsu_bl->keycode2 = KEY_HELP; /* "Mobility Center" */
547 return 1;
548 }
549
550 static int __init dmi_check_cb_s6420(const struct dmi_system_id *id)
551 {
552 dmi_check_cb_common(id);
553 fujitsu_bl->keycode1 = KEY_SCREENLOCK; /* "Lock" */
554 fujitsu_bl->keycode2 = KEY_HELP; /* "Mobility Center" */
555 return 1;
556 }
557
558 static int __init dmi_check_cb_p8010(const struct dmi_system_id *id)
559 {
560 dmi_check_cb_common(id);
561 fujitsu_bl->keycode1 = KEY_HELP; /* "Support" */
562 fujitsu_bl->keycode3 = KEY_SWITCHVIDEOMODE; /* "Presentation" */
563 fujitsu_bl->keycode4 = KEY_WWW; /* "Internet" */
564 return 1;
565 }
566
567 static const struct dmi_system_id fujitsu_dmi_table[] __initconst = {
568 {
569 .ident = "Fujitsu Siemens S6410",
570 .matches = {
571 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
572 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
573 },
574 .callback = dmi_check_cb_s6410},
575 {
576 .ident = "Fujitsu Siemens S6420",
577 .matches = {
578 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
579 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
580 },
581 .callback = dmi_check_cb_s6420},
582 {
583 .ident = "Fujitsu LifeBook P8010",
584 .matches = {
585 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
586 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
587 },
588 .callback = dmi_check_cb_p8010},
589 {}
590 };
591
592 /* ACPI device for LCD brightness control */
593
594 static const struct key_entry keymap_backlight[] = {
595 { KE_KEY, true, { KEY_BRIGHTNESSUP } },
596 { KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
597 { KE_END, 0 }
598 };
599
600 static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
601 {
602 struct fujitsu_bl *fujitsu_bl = acpi_driver_data(device);
603 int ret;
604
605 fujitsu_bl->input = devm_input_allocate_device(&device->dev);
606 if (!fujitsu_bl->input)
607 return -ENOMEM;
608
609 snprintf(fujitsu_bl->phys, sizeof(fujitsu_bl->phys),
610 "%s/video/input0", acpi_device_hid(device));
611
612 fujitsu_bl->input->name = acpi_device_name(device);
613 fujitsu_bl->input->phys = fujitsu_bl->phys;
614 fujitsu_bl->input->id.bustype = BUS_HOST;
615 fujitsu_bl->input->id.product = 0x06;
616
617 ret = sparse_keymap_setup(fujitsu_bl->input, keymap_backlight, NULL);
618 if (ret)
619 return ret;
620
621 return input_register_device(fujitsu_bl->input);
622 }
623
624 static int fujitsu_backlight_register(void)
625 {
626 struct backlight_properties props = {
627 .brightness = fujitsu_bl->brightness_level,
628 .max_brightness = fujitsu_bl->max_brightness - 1,
629 .type = BACKLIGHT_PLATFORM
630 };
631 struct backlight_device *bd;
632
633 bd = backlight_device_register("fujitsu-laptop", NULL, NULL,
634 &fujitsu_bl_ops, &props);
635 if (IS_ERR(bd))
636 return PTR_ERR(bd);
637
638 fujitsu_bl->bl_device = bd;
639
640 return 0;
641 }
642
643 static int acpi_fujitsu_bl_add(struct acpi_device *device)
644 {
645 int state = 0;
646 int error;
647
648 if (!device)
649 return -EINVAL;
650
651 fujitsu_bl->acpi_handle = device->handle;
652 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_BL_DEVICE_NAME);
653 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
654 device->driver_data = fujitsu_bl;
655
656 error = acpi_fujitsu_bl_input_setup(device);
657 if (error)
658 return error;
659
660 error = acpi_bus_update_power(fujitsu_bl->acpi_handle, &state);
661 if (error) {
662 pr_err("Error reading power state\n");
663 return error;
664 }
665
666 pr_info("ACPI: %s [%s] (%s)\n",
667 acpi_device_name(device), acpi_device_bid(device),
668 !device->power.state ? "on" : "off");
669
670 fujitsu_bl->dev = device;
671
672 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
673 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
674 if (ACPI_FAILURE
675 (acpi_evaluate_object
676 (device->handle, METHOD_NAME__INI, NULL, NULL)))
677 pr_err("_INI Method failed\n");
678 }
679
680 if (use_alt_lcd_levels == -1) {
681 if (acpi_has_method(NULL, "\\_SB.PCI0.LPCB.FJEX.SBL2"))
682 use_alt_lcd_levels = 1;
683 else
684 use_alt_lcd_levels = 0;
685 vdbg_printk(FUJLAPTOP_DBG_TRACE, "auto-detected usealt as %i\n",
686 use_alt_lcd_levels);
687 }
688
689 /* do config (detect defaults) */
690 use_alt_lcd_levels = use_alt_lcd_levels == 1 ? 1 : 0;
691 disable_brightness_adjust = disable_brightness_adjust == 1 ? 1 : 0;
692 vdbg_printk(FUJLAPTOP_DBG_INFO,
693 "config: [alt interface: %d], [adjust disable: %d]\n",
694 use_alt_lcd_levels, disable_brightness_adjust);
695
696 if (get_max_brightness() <= 0)
697 fujitsu_bl->max_brightness = FUJITSU_LCD_N_LEVELS;
698 get_lcd_level();
699
700 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
701 error = fujitsu_backlight_register();
702 if (error)
703 return error;
704 }
705
706 return 0;
707 }
708
709 static int acpi_fujitsu_bl_remove(struct acpi_device *device)
710 {
711 struct fujitsu_bl *fujitsu_bl = acpi_driver_data(device);
712
713 backlight_device_unregister(fujitsu_bl->bl_device);
714
715 fujitsu_bl->acpi_handle = NULL;
716
717 return 0;
718 }
719
720 /* Brightness notify */
721
722 static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
723 {
724 struct input_dev *input;
725 int oldb, newb;
726
727 input = fujitsu_bl->input;
728
729 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
730 vdbg_printk(FUJLAPTOP_DBG_WARN,
731 "unsupported event [0x%x]\n", event);
732 sparse_keymap_report_event(input, -1, 1, true);
733 return;
734 }
735
736 oldb = fujitsu_bl->brightness_level;
737 get_lcd_level();
738 newb = fujitsu_bl->brightness_level;
739
740 vdbg_printk(FUJLAPTOP_DBG_TRACE,
741 "brightness button event [%i -> %i (%i)]\n",
742 oldb, newb, fujitsu_bl->brightness_changed);
743
744 if (oldb == newb)
745 return;
746
747 if (disable_brightness_adjust != 1) {
748 if (use_alt_lcd_levels)
749 set_lcd_level_alt(newb);
750 else
751 set_lcd_level(newb);
752 }
753
754 sparse_keymap_report_event(input, oldb < newb, 1, true);
755 }
756
757 /* ACPI device for hotkey handling */
758
759 static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
760 {
761 struct fujitsu_laptop *fujitsu_laptop = acpi_driver_data(device);
762
763 fujitsu_laptop->input = devm_input_allocate_device(&device->dev);
764 if (!fujitsu_laptop->input)
765 return -ENOMEM;
766
767 snprintf(fujitsu_laptop->phys, sizeof(fujitsu_laptop->phys),
768 "%s/video/input0", acpi_device_hid(device));
769
770 fujitsu_laptop->input->name = acpi_device_name(device);
771 fujitsu_laptop->input->phys = fujitsu_laptop->phys;
772 fujitsu_laptop->input->id.bustype = BUS_HOST;
773 fujitsu_laptop->input->id.product = 0x06;
774
775 set_bit(EV_KEY, fujitsu_laptop->input->evbit);
776 set_bit(fujitsu_bl->keycode1, fujitsu_laptop->input->keybit);
777 set_bit(fujitsu_bl->keycode2, fujitsu_laptop->input->keybit);
778 set_bit(fujitsu_bl->keycode3, fujitsu_laptop->input->keybit);
779 set_bit(fujitsu_bl->keycode4, fujitsu_laptop->input->keybit);
780 set_bit(fujitsu_bl->keycode5, fujitsu_laptop->input->keybit);
781 set_bit(KEY_TOUCHPAD_TOGGLE, fujitsu_laptop->input->keybit);
782 set_bit(KEY_UNKNOWN, fujitsu_laptop->input->keybit);
783
784 return input_register_device(fujitsu_laptop->input);
785 }
786
787 static int fujitsu_laptop_platform_add(void)
788 {
789 int ret;
790
791 fujitsu_laptop->pf_device = platform_device_alloc("fujitsu-laptop", -1);
792 if (!fujitsu_laptop->pf_device)
793 return -ENOMEM;
794
795 ret = platform_device_add(fujitsu_laptop->pf_device);
796 if (ret)
797 goto err_put_platform_device;
798
799 ret = sysfs_create_group(&fujitsu_laptop->pf_device->dev.kobj,
800 &fujitsu_pf_attribute_group);
801 if (ret)
802 goto err_del_platform_device;
803
804 return 0;
805
806 err_del_platform_device:
807 platform_device_del(fujitsu_laptop->pf_device);
808 err_put_platform_device:
809 platform_device_put(fujitsu_laptop->pf_device);
810
811 return ret;
812 }
813
814 static void fujitsu_laptop_platform_remove(void)
815 {
816 sysfs_remove_group(&fujitsu_laptop->pf_device->dev.kobj,
817 &fujitsu_pf_attribute_group);
818 platform_device_unregister(fujitsu_laptop->pf_device);
819 }
820
821 static int acpi_fujitsu_laptop_add(struct acpi_device *device)
822 {
823 int result = 0;
824 int state = 0;
825 int error;
826 int i;
827
828 if (!device)
829 return -EINVAL;
830
831 fujitsu_laptop->acpi_handle = device->handle;
832 sprintf(acpi_device_name(device), "%s",
833 ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
834 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
835 device->driver_data = fujitsu_laptop;
836
837 /* kfifo */
838 spin_lock_init(&fujitsu_laptop->fifo_lock);
839 error = kfifo_alloc(&fujitsu_laptop->fifo, RINGBUFFERSIZE * sizeof(int),
840 GFP_KERNEL);
841 if (error) {
842 pr_err("kfifo_alloc failed\n");
843 goto err_stop;
844 }
845
846 error = acpi_fujitsu_laptop_input_setup(device);
847 if (error)
848 goto err_free_fifo;
849
850 error = acpi_bus_update_power(fujitsu_laptop->acpi_handle, &state);
851 if (error) {
852 pr_err("Error reading power state\n");
853 goto err_free_fifo;
854 }
855
856 pr_info("ACPI: %s [%s] (%s)\n",
857 acpi_device_name(device), acpi_device_bid(device),
858 !device->power.state ? "on" : "off");
859
860 fujitsu_laptop->dev = device;
861
862 if (acpi_has_method(device->handle, METHOD_NAME__INI)) {
863 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
864 if (ACPI_FAILURE
865 (acpi_evaluate_object
866 (device->handle, METHOD_NAME__INI, NULL, NULL)))
867 pr_err("_INI Method failed\n");
868 }
869
870 i = 0;
871 while (call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
872 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
873 ; /* No action, result is discarded */
874 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
875
876 fujitsu_laptop->flags_supported =
877 call_fext_func(FUNC_FLAGS, 0x0, 0x0, 0x0);
878
879 /* Make sure our bitmask of supported functions is cleared if the
880 RFKILL function block is not implemented, like on the S7020. */
881 if (fujitsu_laptop->flags_supported == UNSUPPORTED_CMD)
882 fujitsu_laptop->flags_supported = 0;
883
884 if (fujitsu_laptop->flags_supported)
885 fujitsu_laptop->flags_state =
886 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
887
888 /* Suspect this is a keymap of the application panel, print it */
889 pr_info("BTNI: [0x%x]\n", call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0));
890
891 /* Sync backlight power status */
892 if (fujitsu_bl->bl_device &&
893 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
894 if (call_fext_func(FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
895 fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
896 else
897 fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
898 }
899
900 error = fujitsu_laptop_platform_add();
901 if (error)
902 goto err_free_fifo;
903
904 #if IS_ENABLED(CONFIG_LEDS_CLASS)
905 if (call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
906 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
907 &logolamp_led);
908 if (result == 0) {
909 fujitsu_laptop->logolamp_registered = 1;
910 } else {
911 pr_err("Could not register LED handler for logo lamp, error %i\n",
912 result);
913 }
914 }
915
916 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
917 (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
918 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
919 &kblamps_led);
920 if (result == 0) {
921 fujitsu_laptop->kblamps_registered = 1;
922 } else {
923 pr_err("Could not register LED handler for keyboard lamps, error %i\n",
924 result);
925 }
926 }
927
928 /*
929 * BTNI bit 24 seems to indicate the presence of a radio toggle
930 * button in place of a slide switch, and all such machines appear
931 * to also have an RF LED. Therefore use bit 24 as an indicator
932 * that an RF LED is present.
933 */
934 if (call_fext_func(FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
935 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
936 &radio_led);
937 if (result == 0) {
938 fujitsu_laptop->radio_led_registered = 1;
939 } else {
940 pr_err("Could not register LED handler for radio LED, error %i\n",
941 result);
942 }
943 }
944
945 /* Support for eco led is not always signaled in bit corresponding
946 * to the bit used to control the led. According to the DSDT table,
947 * bit 14 seems to indicate presence of said led as well.
948 * Confirm by testing the status.
949 */
950 if ((call_fext_func(FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
951 (call_fext_func(FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
952 result = led_classdev_register(&fujitsu_laptop->pf_device->dev,
953 &eco_led);
954 if (result == 0) {
955 fujitsu_laptop->eco_led_registered = 1;
956 } else {
957 pr_err("Could not register LED handler for eco LED, error %i\n",
958 result);
959 }
960 }
961 #endif
962
963 return result;
964
965 err_free_fifo:
966 kfifo_free(&fujitsu_laptop->fifo);
967 err_stop:
968 return error;
969 }
970
971 static int acpi_fujitsu_laptop_remove(struct acpi_device *device)
972 {
973 struct fujitsu_laptop *fujitsu_laptop = acpi_driver_data(device);
974
975 #if IS_ENABLED(CONFIG_LEDS_CLASS)
976 if (fujitsu_laptop->logolamp_registered)
977 led_classdev_unregister(&logolamp_led);
978
979 if (fujitsu_laptop->kblamps_registered)
980 led_classdev_unregister(&kblamps_led);
981
982 if (fujitsu_laptop->radio_led_registered)
983 led_classdev_unregister(&radio_led);
984
985 if (fujitsu_laptop->eco_led_registered)
986 led_classdev_unregister(&eco_led);
987 #endif
988
989 fujitsu_laptop_platform_remove();
990
991 kfifo_free(&fujitsu_laptop->fifo);
992
993 fujitsu_laptop->acpi_handle = NULL;
994
995 return 0;
996 }
997
998 static void acpi_fujitsu_laptop_press(int keycode)
999 {
1000 struct input_dev *input = fujitsu_laptop->input;
1001 int status;
1002
1003 status = kfifo_in_locked(&fujitsu_laptop->fifo,
1004 (unsigned char *)&keycode, sizeof(keycode),
1005 &fujitsu_laptop->fifo_lock);
1006 if (status != sizeof(keycode)) {
1007 vdbg_printk(FUJLAPTOP_DBG_WARN,
1008 "Could not push keycode [0x%x]\n", keycode);
1009 return;
1010 }
1011 input_report_key(input, keycode, 1);
1012 input_sync(input);
1013 vdbg_printk(FUJLAPTOP_DBG_TRACE,
1014 "Push keycode into ringbuffer [%d]\n", keycode);
1015 }
1016
1017 static void acpi_fujitsu_laptop_release(void)
1018 {
1019 struct input_dev *input = fujitsu_laptop->input;
1020 int keycode, status;
1021
1022 while (true) {
1023 status = kfifo_out_locked(&fujitsu_laptop->fifo,
1024 (unsigned char *)&keycode,
1025 sizeof(keycode),
1026 &fujitsu_laptop->fifo_lock);
1027 if (status != sizeof(keycode))
1028 return;
1029 input_report_key(input, keycode, 0);
1030 input_sync(input);
1031 vdbg_printk(FUJLAPTOP_DBG_TRACE,
1032 "Pop keycode from ringbuffer [%d]\n", keycode);
1033 }
1034 }
1035
1036 static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
1037 {
1038 struct input_dev *input;
1039 int keycode;
1040 unsigned int irb = 1;
1041 int i;
1042
1043 input = fujitsu_laptop->input;
1044
1045 if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
1046 keycode = KEY_UNKNOWN;
1047 vdbg_printk(FUJLAPTOP_DBG_WARN,
1048 "Unsupported event [0x%x]\n", event);
1049 input_report_key(input, keycode, 1);
1050 input_sync(input);
1051 input_report_key(input, keycode, 0);
1052 input_sync(input);
1053 return;
1054 }
1055
1056 if (fujitsu_laptop->flags_supported)
1057 fujitsu_laptop->flags_state =
1058 call_fext_func(FUNC_FLAGS, 0x4, 0x0, 0x0);
1059
1060 i = 0;
1061 while ((irb =
1062 call_fext_func(FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0
1063 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) {
1064 switch (irb & 0x4ff) {
1065 case KEY1_CODE:
1066 keycode = fujitsu_bl->keycode1;
1067 break;
1068 case KEY2_CODE:
1069 keycode = fujitsu_bl->keycode2;
1070 break;
1071 case KEY3_CODE:
1072 keycode = fujitsu_bl->keycode3;
1073 break;
1074 case KEY4_CODE:
1075 keycode = fujitsu_bl->keycode4;
1076 break;
1077 case KEY5_CODE:
1078 keycode = fujitsu_bl->keycode5;
1079 break;
1080 case 0:
1081 keycode = 0;
1082 break;
1083 default:
1084 vdbg_printk(FUJLAPTOP_DBG_WARN,
1085 "Unknown GIRB result [%x]\n", irb);
1086 keycode = -1;
1087 break;
1088 }
1089
1090 if (keycode > 0)
1091 acpi_fujitsu_laptop_press(keycode);
1092 else if (keycode == 0)
1093 acpi_fujitsu_laptop_release();
1094 }
1095
1096 /* On some models (first seen on the Skylake-based Lifebook
1097 * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
1098 * handled in software; its state is queried using FUNC_FLAGS
1099 */
1100 if ((fujitsu_laptop->flags_supported & BIT(26)) &&
1101 (call_fext_func(FUNC_FLAGS, 0x1, 0x0, 0x0) & BIT(26))) {
1102 keycode = KEY_TOUCHPAD_TOGGLE;
1103 input_report_key(input, keycode, 1);
1104 input_sync(input);
1105 input_report_key(input, keycode, 0);
1106 input_sync(input);
1107 }
1108
1109 }
1110
1111 /* Initialization */
1112
1113 static const struct acpi_device_id fujitsu_bl_device_ids[] = {
1114 {ACPI_FUJITSU_BL_HID, 0},
1115 {"", 0},
1116 };
1117
1118 static struct acpi_driver acpi_fujitsu_bl_driver = {
1119 .name = ACPI_FUJITSU_BL_DRIVER_NAME,
1120 .class = ACPI_FUJITSU_CLASS,
1121 .ids = fujitsu_bl_device_ids,
1122 .ops = {
1123 .add = acpi_fujitsu_bl_add,
1124 .remove = acpi_fujitsu_bl_remove,
1125 .notify = acpi_fujitsu_bl_notify,
1126 },
1127 };
1128
1129 static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
1130 {ACPI_FUJITSU_LAPTOP_HID, 0},
1131 {"", 0},
1132 };
1133
1134 static struct acpi_driver acpi_fujitsu_laptop_driver = {
1135 .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
1136 .class = ACPI_FUJITSU_CLASS,
1137 .ids = fujitsu_laptop_device_ids,
1138 .ops = {
1139 .add = acpi_fujitsu_laptop_add,
1140 .remove = acpi_fujitsu_laptop_remove,
1141 .notify = acpi_fujitsu_laptop_notify,
1142 },
1143 };
1144
1145 static const struct acpi_device_id fujitsu_ids[] __used = {
1146 {ACPI_FUJITSU_BL_HID, 0},
1147 {ACPI_FUJITSU_LAPTOP_HID, 0},
1148 {"", 0}
1149 };
1150 MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
1151
1152 static int __init fujitsu_init(void)
1153 {
1154 int ret;
1155
1156 if (acpi_disabled)
1157 return -ENODEV;
1158
1159 fujitsu_bl = kzalloc(sizeof(struct fujitsu_bl), GFP_KERNEL);
1160 if (!fujitsu_bl)
1161 return -ENOMEM;
1162 fujitsu_bl->keycode1 = KEY_PROG1;
1163 fujitsu_bl->keycode2 = KEY_PROG2;
1164 fujitsu_bl->keycode3 = KEY_PROG3;
1165 fujitsu_bl->keycode4 = KEY_PROG4;
1166 fujitsu_bl->keycode5 = KEY_RFKILL;
1167 dmi_check_system(fujitsu_dmi_table);
1168
1169 ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
1170 if (ret)
1171 goto err_free_fujitsu_bl;
1172
1173 /* Register platform stuff */
1174
1175 ret = platform_driver_register(&fujitsu_pf_driver);
1176 if (ret)
1177 goto err_unregister_acpi;
1178
1179 /* Register laptop driver */
1180
1181 fujitsu_laptop = kzalloc(sizeof(struct fujitsu_laptop), GFP_KERNEL);
1182 if (!fujitsu_laptop) {
1183 ret = -ENOMEM;
1184 goto err_unregister_platform_driver;
1185 }
1186
1187 ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
1188 if (ret)
1189 goto err_free_fujitsu_laptop;
1190
1191 pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
1192
1193 return 0;
1194
1195 err_free_fujitsu_laptop:
1196 kfree(fujitsu_laptop);
1197 err_unregister_platform_driver:
1198 platform_driver_unregister(&fujitsu_pf_driver);
1199 err_unregister_acpi:
1200 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
1201 err_free_fujitsu_bl:
1202 kfree(fujitsu_bl);
1203
1204 return ret;
1205 }
1206
1207 static void __exit fujitsu_cleanup(void)
1208 {
1209 acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
1210
1211 kfree(fujitsu_laptop);
1212
1213 platform_driver_unregister(&fujitsu_pf_driver);
1214
1215 acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
1216
1217 kfree(fujitsu_bl);
1218
1219 pr_info("driver unloaded\n");
1220 }
1221
1222 module_init(fujitsu_init);
1223 module_exit(fujitsu_cleanup);
1224
1225 module_param(use_alt_lcd_levels, uint, 0644);
1226 MODULE_PARM_DESC(use_alt_lcd_levels,
1227 "Use alternative interface for lcd_levels (needed for Lifebook s6410).");
1228 module_param(disable_brightness_adjust, uint, 0644);
1229 MODULE_PARM_DESC(disable_brightness_adjust, "Disable brightness adjustment .");
1230 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1231 module_param_named(debug, dbg_level, uint, 0644);
1232 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1233 #endif
1234
1235 MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
1236 MODULE_DESCRIPTION("Fujitsu laptop extras support");
1237 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1238 MODULE_LICENSE("GPL");